Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1 | //===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the parser class for .ll files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 14 | #ifndef LLVM_LIB_ASMPARSER_LLPARSER_H |
| 15 | #define LLVM_LIB_ASMPARSER_LLPARSER_H |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 16 | |
| 17 | #include "LLLexer.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Attributes.h" |
| 21 | #include "llvm/IR/Instructions.h" |
| 22 | #include "llvm/IR/Module.h" |
| 23 | #include "llvm/IR/Operator.h" |
| 24 | #include "llvm/IR/Type.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 25 | #include "llvm/IR/ValueHandle.h" |
Chris Lattner | 218b22f | 2009-12-29 21:43:58 +0000 | [diff] [blame] | 26 | #include <map> |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 27 | |
| 28 | namespace llvm { |
| 29 | class Module; |
| 30 | class OpaqueType; |
| 31 | class Function; |
| 32 | class Value; |
| 33 | class BasicBlock; |
| 34 | class Instruction; |
| 35 | class Constant; |
| 36 | class GlobalValue; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 37 | class Comdat; |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 38 | class MDString; |
| 39 | class MDNode; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 40 | class StructType; |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 42 | /// ValID - Represents a reference of a definition of some sort with no type. |
| 43 | /// There are several cases where we have to parse the value but where the |
| 44 | /// type can depend on later context. This may either be a numeric reference |
| 45 | /// or a symbolic (%var) reference. This is just a discriminated union. |
Benjamin Kramer | 079b96e | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 46 | struct ValID { |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 47 | enum { |
| 48 | t_LocalID, t_GlobalID, // ID in UIntVal. |
| 49 | t_LocalName, t_GlobalName, // Name in StrVal. |
| 50 | t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal. |
| 51 | t_Null, t_Undef, t_Zero, // No value. |
| 52 | t_EmptyArray, // No value: [] |
| 53 | t_Constant, // Value in ConstantVal. |
| 54 | t_InlineAsm, // Value in StrVal/StrVal2/UIntVal. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 55 | t_Metadata, // Value in MetadataVal. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 56 | t_ConstantStruct, // Value in ConstantStructElts. |
| 57 | t_PackedConstantStruct // Value in ConstantStructElts. |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 58 | } Kind; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 60 | LLLexer::LocTy Loc; |
| 61 | unsigned UIntVal; |
| 62 | std::string StrVal, StrVal2; |
| 63 | APSInt APSIntVal; |
| 64 | APFloat APFloatVal; |
| 65 | Constant *ConstantVal; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 66 | MetadataAsValue *MetadataVal; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 67 | Constant **ConstantStructElts; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 68 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 69 | ValID() : Kind(t_LocalID), APFloatVal(0.0) {} |
| 70 | ~ValID() { |
| 71 | if (Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) |
| 72 | delete [] ConstantStructElts; |
| 73 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 75 | bool operator<(const ValID &RHS) const { |
| 76 | if (Kind == t_LocalID || Kind == t_GlobalID) |
| 77 | return UIntVal < RHS.UIntVal; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 78 | assert((Kind == t_LocalName || Kind == t_GlobalName || |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 79 | Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) && |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 80 | "Ordering not defined for this ValID kind yet"); |
| 81 | return StrVal < RHS.StrVal; |
| 82 | } |
| 83 | }; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 84 | |
Benjamin Kramer | 079b96e | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 85 | class LLParser { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 86 | public: |
| 87 | typedef LLLexer::LocTy LocTy; |
| 88 | private: |
Chris Lattner | 2e664bd | 2010-04-07 04:08:57 +0000 | [diff] [blame] | 89 | LLVMContext &Context; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 90 | LLLexer Lex; |
| 91 | Module *M; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 8eff015 | 2010-04-01 05:14:45 +0000 | [diff] [blame] | 93 | // Instruction metadata resolution. Each instruction can have a list of |
| 94 | // MDRef info associated with them. |
Dan Gohman | 7c7f13a | 2010-08-24 14:31:06 +0000 | [diff] [blame] | 95 | // |
| 96 | // The simpler approach of just creating temporary MDNodes and then calling |
| 97 | // RAUW on them when the definition is processed doesn't work because some |
| 98 | // instruction metadata kinds, such as dbg, get stored in the IR in an |
| 99 | // "optimized" format which doesn't participate in the normal value use |
| 100 | // lists. This means that RAUW doesn't work, even on temporary MDNodes |
| 101 | // which otherwise support RAUW. Instead, we defer resolving MDNode |
| 102 | // references until the definitions have been processed. |
Chris Lattner | 8eff015 | 2010-04-01 05:14:45 +0000 | [diff] [blame] | 103 | struct MDRef { |
| 104 | SMLoc Loc; |
| 105 | unsigned MDKind, MDSlot; |
| 106 | }; |
| 107 | DenseMap<Instruction*, std::vector<MDRef> > ForwardRefInstMetadata; |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 108 | |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 109 | SmallVector<Instruction*, 64> InstsWithTBAATag; |
| 110 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 111 | // Type resolution handling data structures. The location is set when we |
| 112 | // have processed a use of the type but not a definition yet. |
| 113 | StringMap<std::pair<Type*, LocTy> > NamedTypes; |
| 114 | std::vector<std::pair<Type*, LocTy> > NumberedTypes; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 115 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 116 | std::vector<TrackingMDNodeRef> NumberedMetadata; |
| 117 | std::map<unsigned, std::pair<MDNodeFwdDecl *, LocTy>> ForwardRefMDNodes; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 118 | |
| 119 | // Global Value reference information. |
| 120 | std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals; |
| 121 | std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs; |
| 122 | std::vector<GlobalValue*> NumberedVals; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 123 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 124 | // Comdat forward reference information. |
| 125 | std::map<std::string, LocTy> ForwardRefComdats; |
| 126 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 127 | // References to blockaddress. The key is the function ValID, the value is |
| 128 | // a list of references to blocks in that function. |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 129 | std::map<ValID, std::map<ValID, GlobalValue *>> ForwardRefBlockAddresses; |
| 130 | class PerFunctionState; |
| 131 | /// Reference to per-function state to allow basic blocks to be |
| 132 | /// forward-referenced by blockaddress instructions within the same |
| 133 | /// function. |
| 134 | PerFunctionState *BlockAddressPFS; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 135 | |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 136 | // Attribute builder reference information. |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 137 | std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups; |
| 138 | std::map<unsigned, AttrBuilder> NumberedAttrBuilders; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 139 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 140 | public: |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 141 | LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *m) |
| 142 | : Context(m->getContext()), Lex(F, SM, Err, m->getContext()), M(m), |
| 143 | BlockAddressPFS(nullptr) {} |
Chris Lattner | ad6f335 | 2009-01-04 20:44:11 +0000 | [diff] [blame] | 144 | bool Run(); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 145 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 146 | LLVMContext &getContext() { return Context; } |
Owen Anderson | 09063ce | 2009-07-02 17:04:01 +0000 | [diff] [blame] | 147 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 148 | private: |
| 149 | |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 150 | bool Error(LocTy L, const Twine &Msg) const { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 151 | return Lex.Error(L, Msg); |
| 152 | } |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 153 | bool TokError(const Twine &Msg) const { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 154 | return Error(Lex.getLoc(), Msg); |
| 155 | } |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 156 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 157 | /// GetGlobalVal - Get a value with the specified name or ID, creating a |
| 158 | /// forward reference record if needed. This can return null if the value |
| 159 | /// exists but does not have the right type. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 160 | GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc); |
| 161 | GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 162 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 163 | /// Get a Comdat with the specified name, creating a forward reference |
| 164 | /// record if needed. |
| 165 | Comdat *getComdat(const std::string &N, LocTy Loc); |
| 166 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 167 | // Helper Routines. |
| 168 | bool ParseToken(lltok::Kind T, const char *ErrMsg); |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 169 | bool EatIfPresent(lltok::Kind T) { |
| 170 | if (Lex.getKind() != T) return false; |
| 171 | Lex.Lex(); |
| 172 | return true; |
| 173 | } |
Michael Ilseman | 9205317 | 2012-11-27 00:42:44 +0000 | [diff] [blame] | 174 | |
| 175 | FastMathFlags EatFastMathFlagsIfPresent() { |
| 176 | FastMathFlags FMF; |
| 177 | while (true) |
| 178 | switch (Lex.getKind()) { |
Michael Ilseman | 65f1435 | 2012-12-09 21:12:04 +0000 | [diff] [blame] | 179 | case lltok::kw_fast: FMF.setUnsafeAlgebra(); Lex.Lex(); continue; |
| 180 | case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue; |
| 181 | case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue; |
| 182 | case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue; |
| 183 | case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue; |
Michael Ilseman | 9205317 | 2012-11-27 00:42:44 +0000 | [diff] [blame] | 184 | default: return FMF; |
| 185 | } |
| 186 | return FMF; |
| 187 | } |
| 188 | |
Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 189 | bool ParseOptionalToken(lltok::Kind T, bool &Present, |
| 190 | LocTy *Loc = nullptr) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 191 | if (Lex.getKind() != T) { |
| 192 | Present = false; |
| 193 | } else { |
Rafael Espindola | 026d152 | 2011-01-13 01:30:30 +0000 | [diff] [blame] | 194 | if (Loc) |
| 195 | *Loc = Lex.getLoc(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 196 | Lex.Lex(); |
| 197 | Present = true; |
| 198 | } |
| 199 | return false; |
| 200 | } |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 201 | bool ParseStringConstant(std::string &Result); |
| 202 | bool ParseUInt32(unsigned &Val); |
| 203 | bool ParseUInt32(unsigned &Val, LocTy &Loc) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 204 | Loc = Lex.getLoc(); |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 205 | return ParseUInt32(Val); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 206 | } |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 207 | bool ParseUInt64(uint64_t &Val); |
| 208 | bool ParseUInt64(uint64_t &Val, LocTy &Loc) { |
| 209 | Loc = Lex.getLoc(); |
| 210 | return ParseUInt64(Val); |
| 211 | } |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 212 | |
| 213 | bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM); |
| 214 | bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM); |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 215 | bool parseOptionalUnnamedAddr(bool &UnnamedAddr) { |
| 216 | return ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr); |
| 217 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 218 | bool ParseOptionalAddrSpace(unsigned &AddrSpace); |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 219 | bool ParseOptionalParamAttrs(AttrBuilder &B); |
| 220 | bool ParseOptionalReturnAttrs(AttrBuilder &B); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 221 | bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage); |
| 222 | bool ParseOptionalLinkage(unsigned &Linkage) { |
| 223 | bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage); |
| 224 | } |
| 225 | bool ParseOptionalVisibility(unsigned &Visibility); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 226 | bool ParseOptionalDLLStorageClass(unsigned &DLLStorageClass); |
Alexey Samsonov | 17a9cff | 2014-09-10 18:00:17 +0000 | [diff] [blame] | 227 | bool ParseOptionalCallingConv(unsigned &CC); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 228 | bool ParseOptionalAlignment(unsigned &Alignment); |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 229 | bool ParseOptionalDereferenceableBytes(uint64_t &Bytes); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 230 | bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope, |
| 231 | AtomicOrdering &Ordering); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 232 | bool ParseOrdering(AtomicOrdering &Ordering); |
Charles Davis | be5557e | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 233 | bool ParseOptionalStackAlignment(unsigned &Alignment); |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 234 | bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma); |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 235 | bool ParseOptionalCommaInAlloca(bool &IsInAlloca); |
Chris Lattner | 28f1eeb | 2009-12-30 05:14:00 +0000 | [diff] [blame] | 236 | bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma); |
| 237 | bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) { |
| 238 | bool AteExtraComma; |
| 239 | if (ParseIndexList(Indices, AteExtraComma)) return true; |
| 240 | if (AteExtraComma) |
| 241 | return TokError("expected index"); |
| 242 | return false; |
| 243 | } |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 244 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 245 | // Top-Level Entities |
| 246 | bool ParseTopLevelEntities(); |
| 247 | bool ValidateEndOfModule(); |
| 248 | bool ParseTargetDefinition(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 249 | bool ParseModuleAsm(); |
Bill Wendling | 706d3d6 | 2012-11-28 08:41:48 +0000 | [diff] [blame] | 250 | bool ParseDepLibs(); // FIXME: Remove in 4.0. |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 251 | bool ParseUnnamedType(); |
| 252 | bool ParseNamedType(); |
| 253 | bool ParseDeclare(); |
| 254 | bool ParseDefine(); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 255 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 256 | bool ParseGlobalType(bool &IsConstant); |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 257 | bool ParseUnnamedGlobal(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 258 | bool ParseNamedGlobal(); |
| 259 | bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage, |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 260 | bool HasLinkage, unsigned Visibility, |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 261 | unsigned DLLStorageClass, |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 262 | GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr); |
Rafael Espindola | 464fe02 | 2014-07-30 22:51:54 +0000 | [diff] [blame] | 263 | bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Linkage, |
| 264 | unsigned Visibility, unsigned DLLStorageClass, |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 265 | GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 266 | bool parseComdat(); |
Devang Patel | 39e64d4 | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 267 | bool ParseStandaloneMetadata(); |
Devang Patel | be62697 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 268 | bool ParseNamedMetadata(); |
Chris Lattner | 1797fc7 | 2009-12-29 21:53:55 +0000 | [diff] [blame] | 269 | bool ParseMDString(MDString *&Result); |
Chris Lattner | 6dac02a | 2009-12-30 04:15:23 +0000 | [diff] [blame] | 270 | bool ParseMDNodeID(MDNode *&Result); |
Chris Lattner | 8eff015 | 2010-04-01 05:14:45 +0000 | [diff] [blame] | 271 | bool ParseMDNodeID(MDNode *&Result, unsigned &SlotNo); |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 272 | bool ParseUnnamedAttrGrp(); |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 273 | bool ParseFnAttributeValuePairs(AttrBuilder &B, |
| 274 | std::vector<unsigned> &FwdRefAttrGrps, |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 275 | bool inAttrGrp, LocTy &BuiltinLoc); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 276 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 277 | // Type Parsing. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 278 | bool ParseType(Type *&Result, bool AllowVoid = false); |
| 279 | bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 280 | Loc = Lex.getLoc(); |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 281 | return ParseType(Result, AllowVoid); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 282 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 283 | bool ParseAnonStructType(Type *&Result, bool Packed); |
| 284 | bool ParseStructBody(SmallVectorImpl<Type*> &Body); |
| 285 | bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name, |
| 286 | std::pair<Type*, LocTy> &Entry, |
| 287 | Type *&ResultTy); |
| 288 | |
| 289 | bool ParseArrayVectorType(Type *&Result, bool isVector); |
| 290 | bool ParseFunctionType(Type *&Result); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 291 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 292 | // Function Semantic Analysis. |
| 293 | class PerFunctionState { |
| 294 | LLParser &P; |
| 295 | Function &F; |
| 296 | std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals; |
| 297 | std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs; |
| 298 | std::vector<Value*> NumberedVals; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 300 | /// FunctionNumber - If this is an unnamed function, this is the slot |
| 301 | /// number of it, otherwise it is -1. |
| 302 | int FunctionNumber; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 303 | public: |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 304 | PerFunctionState(LLParser &p, Function &f, int FunctionNumber); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 305 | ~PerFunctionState(); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 306 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 307 | Function &getFunction() const { return F; } |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 309 | bool FinishFunction(); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 310 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 311 | /// GetVal - Get a value with the specified name or ID, creating a |
| 312 | /// forward reference record if needed. This can return null if the value |
| 313 | /// exists but does not have the right type. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 314 | Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc); |
| 315 | Value *GetVal(unsigned ID, Type *Ty, LocTy Loc); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 316 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 317 | /// SetInstName - After an instruction is parsed and inserted into its |
| 318 | /// basic block, this installs its name. |
| 319 | bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc, |
| 320 | Instruction *Inst); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 321 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 322 | /// GetBB - Get a basic block with the specified name or ID, creating a |
| 323 | /// forward reference record if needed. This can return null if the value |
| 324 | /// is not a BasicBlock. |
| 325 | BasicBlock *GetBB(const std::string &Name, LocTy Loc); |
| 326 | BasicBlock *GetBB(unsigned ID, LocTy Loc); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 327 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 328 | /// DefineBB - Define the specified basic block, which is either named or |
| 329 | /// unnamed. If there is an error, this returns null otherwise it returns |
| 330 | /// the block being defined. |
| 331 | BasicBlock *DefineBB(const std::string &Name, LocTy Loc); |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 332 | |
| 333 | bool resolveForwardRefBlockAddresses(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 334 | }; |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 335 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 336 | bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V, |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 337 | PerFunctionState *PFS); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 338 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 339 | bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS); |
| 340 | bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 341 | return ParseValue(Ty, V, &PFS); |
| 342 | } |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 343 | bool ParseValue(Type *Ty, Value *&V, LocTy &Loc, |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 344 | PerFunctionState &PFS) { |
| 345 | Loc = Lex.getLoc(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 346 | return ParseValue(Ty, V, &PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 347 | } |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 348 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 349 | bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS); |
| 350 | bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) { |
| 351 | return ParseTypeAndValue(V, &PFS); |
| 352 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 353 | bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) { |
| 354 | Loc = Lex.getLoc(); |
| 355 | return ParseTypeAndValue(V, PFS); |
| 356 | } |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 357 | bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, |
| 358 | PerFunctionState &PFS); |
| 359 | bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) { |
| 360 | LocTy Loc; |
| 361 | return ParseTypeAndBasicBlock(BB, Loc, PFS); |
| 362 | } |
Victor Hernandez | fa23223 | 2009-12-03 23:40:58 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 392be58 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 364 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 365 | struct ParamInfo { |
| 366 | LocTy Loc; |
| 367 | Value *V; |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 368 | AttributeSet Attrs; |
| 369 | ParamInfo(LocTy loc, Value *v, AttributeSet attrs) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 370 | : Loc(loc), V(v), Attrs(attrs) {} |
| 371 | }; |
| 372 | bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, |
Reid Kleckner | 8349864 | 2014-08-26 00:33:28 +0000 | [diff] [blame] | 373 | PerFunctionState &PFS, |
| 374 | bool IsMustTailCall = false, |
| 375 | bool InVarArgsFunc = false); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 376 | |
Victor Hernandez | dc6e65a | 2010-01-05 22:22:14 +0000 | [diff] [blame] | 377 | // Constant Parsing. |
Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 378 | bool ParseValID(ValID &ID, PerFunctionState *PFS = nullptr); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 379 | bool ParseGlobalValue(Type *Ty, Constant *&V); |
Victor Hernandez | dc6e65a | 2010-01-05 22:22:14 +0000 | [diff] [blame] | 380 | bool ParseGlobalTypeAndValue(Constant *&V); |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 381 | bool ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 382 | bool parseOptionalComdat(Comdat *&C); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 383 | bool ParseMetadataAsValue(ValID &ID, PerFunctionState *PFS); |
| 384 | bool ParseMetadata(Metadata *&MD, PerFunctionState *PFS); |
| 385 | bool ParseMDNode(MDNode *&MD); |
| 386 | bool ParseMDNodeOrLocal(Metadata *&MD, PerFunctionState *PFS); |
| 387 | bool ParseMDNodeVector(SmallVectorImpl<Metadata *> &, |
| 388 | PerFunctionState *PFS); |
Dan Gohman | 338d9a4 | 2010-08-24 02:05:17 +0000 | [diff] [blame] | 389 | bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS); |
Victor Hernandez | dc6e65a | 2010-01-05 22:22:14 +0000 | [diff] [blame] | 390 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 391 | // Function Parsing. |
| 392 | struct ArgInfo { |
| 393 | LocTy Loc; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 394 | Type *Ty; |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 395 | AttributeSet Attrs; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 396 | std::string Name; |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 397 | ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 398 | : Loc(L), Ty(ty), Attrs(Attr), Name(N) {} |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 399 | }; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 400 | bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 401 | bool ParseFunctionHeader(Function *&Fn, bool isDefine); |
| 402 | bool ParseFunctionBody(Function &Fn); |
| 403 | bool ParseBasicBlock(PerFunctionState &PFS); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 404 | |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 405 | enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail }; |
| 406 | |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 407 | // Instruction Parsing. Each instruction parsing routine can return with a |
| 408 | // normal result, an error result, or return having eaten an extra comma. |
| 409 | enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 }; |
| 410 | int ParseInstruction(Instruction *&Inst, BasicBlock *BB, |
| 411 | PerFunctionState &PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 412 | bool ParseCmpPredicate(unsigned &Pred, unsigned Opc); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 33de427 | 2011-06-17 06:49:41 +0000 | [diff] [blame] | 414 | bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 415 | bool ParseBr(Instruction *&Inst, PerFunctionState &PFS); |
| 416 | bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS); |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 417 | bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 418 | bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS); |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 419 | bool ParseResume(Instruction *&Inst, PerFunctionState &PFS); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 420 | |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 421 | bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc, |
| 422 | unsigned OperandType); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 423 | bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc); |
| 424 | bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc); |
| 425 | bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc); |
| 426 | bool ParseSelect(Instruction *&I, PerFunctionState &PFS); |
Chris Lattner | b55ab54 | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 427 | bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 428 | bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS); |
| 429 | bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS); |
| 430 | bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS); |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 431 | int ParsePHI(Instruction *&I, PerFunctionState &PFS); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 432 | bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS); |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 433 | bool ParseCall(Instruction *&I, PerFunctionState &PFS, |
| 434 | CallInst::TailCallKind IsTail); |
Chris Lattner | 7810372 | 2011-06-17 03:16:47 +0000 | [diff] [blame] | 435 | int ParseAlloc(Instruction *&I, PerFunctionState &PFS); |
Chris Lattner | bc63929 | 2011-11-27 06:56:53 +0000 | [diff] [blame] | 436 | int ParseLoad(Instruction *&I, PerFunctionState &PFS); |
| 437 | int ParseStore(Instruction *&I, PerFunctionState &PFS); |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 438 | int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS); |
| 439 | int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 440 | int ParseFence(Instruction *&I, PerFunctionState &PFS); |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 441 | int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS); |
| 442 | int ParseExtractValue(Instruction *&I, PerFunctionState &PFS); |
| 443 | int ParseInsertValue(Instruction *&I, PerFunctionState &PFS); |
Duncan P. N. Exon Smith | 0a448fb | 2014-08-19 21:30:15 +0000 | [diff] [blame] | 444 | |
| 445 | // Use-list order directives. |
| 446 | bool ParseUseListOrder(PerFunctionState *PFS = nullptr); |
| 447 | bool ParseUseListOrderBB(); |
| 448 | bool ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes); |
| 449 | bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 450 | }; |
| 451 | } // End llvm namespace |
| 452 | |
| 453 | #endif |