Chris Lattner | df98617 | 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 | |
| 14 | #ifndef LLVM_ASMPARSER_LLPARSER_H |
| 15 | #define LLVM_ASMPARSER_LLPARSER_H |
| 16 | |
| 17 | #include "LLLexer.h" |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | 0b8c9a8 | 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" |
Chris Lattner | 5660846 | 2009-12-28 08:20:46 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ValueHandle.h" |
Chris Lattner | e80250e | 2009-12-29 21:43:58 +0000 | [diff] [blame] | 26 | #include <map> |
Chris Lattner | df98617 | 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; |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 37 | class MDString; |
| 38 | class MDNode; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 39 | class StructType; |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 09d9ef4 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 41 | /// ValID - Represents a reference of a definition of some sort with no type. |
| 42 | /// There are several cases where we have to parse the value but where the |
| 43 | /// type can depend on later context. This may either be a numeric reference |
| 44 | /// or a symbolic (%var) reference. This is just a discriminated union. |
Benjamin Kramer | 55c06ae | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 45 | struct ValID { |
Chris Lattner | 09d9ef4 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 46 | enum { |
| 47 | t_LocalID, t_GlobalID, // ID in UIntVal. |
| 48 | t_LocalName, t_GlobalName, // Name in StrVal. |
| 49 | t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal. |
| 50 | t_Null, t_Undef, t_Zero, // No value. |
| 51 | t_EmptyArray, // No value: [] |
| 52 | t_Constant, // Value in ConstantVal. |
| 53 | t_InlineAsm, // Value in StrVal/StrVal2/UIntVal. |
Chris Lattner | 287881d | 2009-12-30 02:11:14 +0000 | [diff] [blame] | 54 | t_MDNode, // Value in MDNodeVal. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 55 | t_MDString, // Value in MDStringVal. |
| 56 | t_ConstantStruct, // Value in ConstantStructElts. |
| 57 | t_PackedConstantStruct // Value in ConstantStructElts. |
Chris Lattner | 09d9ef4 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 58 | } Kind; |
Michael Ilseman | 407a616 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 09d9ef4 | 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; |
Chris Lattner | 287881d | 2009-12-30 02:11:14 +0000 | [diff] [blame] | 66 | MDNode *MDNodeVal; |
| 67 | MDString *MDStringVal; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 68 | Constant **ConstantStructElts; |
Michael Ilseman | 407a616 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 70 | ValID() : Kind(t_LocalID), APFloatVal(0.0) {} |
| 71 | ~ValID() { |
| 72 | if (Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) |
| 73 | delete [] ConstantStructElts; |
| 74 | } |
Michael Ilseman | 407a616 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 09d9ef4 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 76 | bool operator<(const ValID &RHS) const { |
| 77 | if (Kind == t_LocalID || Kind == t_GlobalID) |
| 78 | return UIntVal < RHS.UIntVal; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 79 | assert((Kind == t_LocalName || Kind == t_GlobalName || |
Michael Ilseman | 407a616 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 80 | Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) && |
Chris Lattner | 09d9ef4 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 81 | "Ordering not defined for this ValID kind yet"); |
| 82 | return StrVal < RHS.StrVal; |
| 83 | } |
| 84 | }; |
Michael Ilseman | 407a616 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 85 | |
Benjamin Kramer | 55c06ae | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 86 | class LLParser { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 87 | public: |
| 88 | typedef LLLexer::LocTy LocTy; |
| 89 | private: |
Chris Lattner | 4ba9d9b | 2010-04-07 04:08:57 +0000 | [diff] [blame] | 90 | LLVMContext &Context; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 91 | LLLexer Lex; |
| 92 | Module *M; |
Michael Ilseman | 407a616 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 449c310 | 2010-04-01 05:14:45 +0000 | [diff] [blame] | 94 | // Instruction metadata resolution. Each instruction can have a list of |
| 95 | // MDRef info associated with them. |
Dan Gohman | 41d6ab4 | 2010-08-24 14:31:06 +0000 | [diff] [blame] | 96 | // |
| 97 | // The simpler approach of just creating temporary MDNodes and then calling |
| 98 | // RAUW on them when the definition is processed doesn't work because some |
| 99 | // instruction metadata kinds, such as dbg, get stored in the IR in an |
| 100 | // "optimized" format which doesn't participate in the normal value use |
| 101 | // lists. This means that RAUW doesn't work, even on temporary MDNodes |
| 102 | // which otherwise support RAUW. Instead, we defer resolving MDNode |
| 103 | // references until the definitions have been processed. |
Chris Lattner | 449c310 | 2010-04-01 05:14:45 +0000 | [diff] [blame] | 104 | struct MDRef { |
| 105 | SMLoc Loc; |
| 106 | unsigned MDKind, MDSlot; |
| 107 | }; |
| 108 | DenseMap<Instruction*, std::vector<MDRef> > ForwardRefInstMetadata; |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 109 | |
Manman Ren | 804f034 | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 110 | SmallVector<Instruction*, 64> InstsWithTBAATag; |
| 111 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 112 | // Type resolution handling data structures. The location is set when we |
| 113 | // have processed a use of the type but not a definition yet. |
| 114 | StringMap<std::pair<Type*, LocTy> > NamedTypes; |
| 115 | std::vector<std::pair<Type*, LocTy> > NumberedTypes; |
Michael Ilseman | 407a616 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 0834e6a | 2009-12-30 04:51:58 +0000 | [diff] [blame] | 117 | std::vector<TrackingVH<MDNode> > NumberedMetadata; |
Chris Lattner | e80250e | 2009-12-29 21:43:58 +0000 | [diff] [blame] | 118 | std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 119 | |
| 120 | // Global Value reference information. |
| 121 | std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals; |
| 122 | std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs; |
| 123 | std::vector<GlobalValue*> NumberedVals; |
Michael Ilseman | 407a616 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 09d9ef4 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 125 | // References to blockaddress. The key is the function ValID, the value is |
| 126 | // a list of references to blocks in that function. |
| 127 | std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > > |
| 128 | ForwardRefBlockAddresses; |
Michael Ilseman | 407a616 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 129 | |
Bill Wendling | 95ce4c2 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 130 | // Attribute builder reference information. |
Bill Wendling | baad55c | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 131 | std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups; |
| 132 | std::map<unsigned, AttrBuilder> NumberedAttrBuilders; |
Bill Wendling | 95ce4c2 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 133 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 134 | public: |
Michael Ilseman | 407a616 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 135 | LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) : |
Victor Hernandez | 13ad5aa | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 136 | Context(m->getContext()), Lex(F, SM, Err, m->getContext()), |
Chris Lattner | f3a789d | 2011-06-17 03:16:47 +0000 | [diff] [blame] | 137 | M(m) {} |
Chris Lattner | ad7d1e2 | 2009-01-04 20:44:11 +0000 | [diff] [blame] | 138 | bool Run(); |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 140 | LLVMContext &getContext() { return Context; } |
Owen Anderson | b43eae7 | 2009-07-02 17:04:01 +0000 | [diff] [blame] | 141 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 142 | private: |
| 143 | |
Benjamin Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 144 | bool Error(LocTy L, const Twine &Msg) const { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 145 | return Lex.Error(L, Msg); |
| 146 | } |
Benjamin Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 147 | bool TokError(const Twine &Msg) const { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 148 | return Error(Lex.getLoc(), Msg); |
| 149 | } |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 150 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 151 | /// GetGlobalVal - Get a value with the specified name or ID, creating a |
| 152 | /// forward reference record if needed. This can return null if the value |
| 153 | /// exists but does not have the right type. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 154 | GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc); |
| 155 | GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc); |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 156 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 157 | // Helper Routines. |
| 158 | bool ParseToken(lltok::Kind T, const char *ErrMsg); |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 159 | bool EatIfPresent(lltok::Kind T) { |
| 160 | if (Lex.getKind() != T) return false; |
| 161 | Lex.Lex(); |
| 162 | return true; |
| 163 | } |
Michael Ilseman | 15c13d3 | 2012-11-27 00:42:44 +0000 | [diff] [blame] | 164 | |
| 165 | FastMathFlags EatFastMathFlagsIfPresent() { |
| 166 | FastMathFlags FMF; |
| 167 | while (true) |
| 168 | switch (Lex.getKind()) { |
Michael Ilseman | 1638b83 | 2012-12-09 21:12:04 +0000 | [diff] [blame] | 169 | case lltok::kw_fast: FMF.setUnsafeAlgebra(); Lex.Lex(); continue; |
| 170 | case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue; |
| 171 | case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue; |
| 172 | case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue; |
| 173 | case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue; |
Michael Ilseman | 15c13d3 | 2012-11-27 00:42:44 +0000 | [diff] [blame] | 174 | default: return FMF; |
| 175 | } |
| 176 | return FMF; |
| 177 | } |
| 178 | |
Rafael Espindola | d72479c | 2011-01-13 01:30:30 +0000 | [diff] [blame] | 179 | bool ParseOptionalToken(lltok::Kind T, bool &Present, LocTy *Loc = 0) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 180 | if (Lex.getKind() != T) { |
| 181 | Present = false; |
| 182 | } else { |
Rafael Espindola | d72479c | 2011-01-13 01:30:30 +0000 | [diff] [blame] | 183 | if (Loc) |
| 184 | *Loc = Lex.getLoc(); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 185 | Lex.Lex(); |
| 186 | Present = true; |
| 187 | } |
| 188 | return false; |
| 189 | } |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 190 | bool ParseStringConstant(std::string &Result); |
| 191 | bool ParseUInt32(unsigned &Val); |
| 192 | bool ParseUInt32(unsigned &Val, LocTy &Loc) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 193 | Loc = Lex.getLoc(); |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 194 | return ParseUInt32(Val); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 195 | } |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 196 | |
| 197 | bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM); |
| 198 | bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 199 | bool ParseOptionalAddrSpace(unsigned &AddrSpace); |
Bill Wendling | e01b81b | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 200 | bool ParseOptionalParamAttrs(AttrBuilder &B); |
| 201 | bool ParseOptionalReturnAttrs(AttrBuilder &B); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 202 | bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage); |
| 203 | bool ParseOptionalLinkage(unsigned &Linkage) { |
| 204 | bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage); |
| 205 | } |
| 206 | bool ParseOptionalVisibility(unsigned &Visibility); |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 207 | bool ParseOptionalCallingConv(CallingConv::ID &CC); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 208 | bool ParseOptionalAlignment(unsigned &Alignment); |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 209 | bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope, |
| 210 | AtomicOrdering &Ordering); |
Charles Davis | 1e063d1 | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 211 | bool ParseOptionalStackAlignment(unsigned &Alignment); |
Chris Lattner | c3a6c5c | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 212 | bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma); |
Chris Lattner | 628c13a | 2009-12-30 05:14:00 +0000 | [diff] [blame] | 213 | bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma); |
| 214 | bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) { |
| 215 | bool AteExtraComma; |
| 216 | if (ParseIndexList(Indices, AteExtraComma)) return true; |
| 217 | if (AteExtraComma) |
| 218 | return TokError("expected index"); |
| 219 | return false; |
| 220 | } |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 221 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 222 | // Top-Level Entities |
| 223 | bool ParseTopLevelEntities(); |
| 224 | bool ValidateEndOfModule(); |
| 225 | bool ParseTargetDefinition(); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 226 | bool ParseModuleAsm(); |
Bill Wendling | 3defc0b | 2012-11-28 08:41:48 +0000 | [diff] [blame] | 227 | bool ParseDepLibs(); // FIXME: Remove in 4.0. |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 228 | bool ParseUnnamedType(); |
| 229 | bool ParseNamedType(); |
| 230 | bool ParseDeclare(); |
| 231 | bool ParseDefine(); |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 232 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 233 | bool ParseGlobalType(bool &IsConstant); |
Dan Gohman | 3845e50 | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 234 | bool ParseUnnamedGlobal(); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 235 | bool ParseNamedGlobal(); |
| 236 | bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage, |
| 237 | bool HasLinkage, unsigned Visibility); |
| 238 | bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility); |
Devang Patel | 923078c | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 239 | bool ParseStandaloneMetadata(); |
Devang Patel | eff2ab6 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 240 | bool ParseNamedMetadata(); |
Chris Lattner | 442ffa1 | 2009-12-29 21:53:55 +0000 | [diff] [blame] | 241 | bool ParseMDString(MDString *&Result); |
Chris Lattner | 4a72efc | 2009-12-30 04:15:23 +0000 | [diff] [blame] | 242 | bool ParseMDNodeID(MDNode *&Result); |
Chris Lattner | 449c310 | 2010-04-01 05:14:45 +0000 | [diff] [blame] | 243 | bool ParseMDNodeID(MDNode *&Result, unsigned &SlotNo); |
Bill Wendling | 95ce4c2 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 244 | bool ParseUnnamedAttrGrp(); |
Bill Wendling | baad55c | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 245 | bool ParseFnAttributeValuePairs(AttrBuilder &B, |
| 246 | std::vector<unsigned> &FwdRefAttrGrps, |
Michael Gottesman | 2253a2f | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 247 | bool inAttrGrp, LocTy &BuiltinLoc); |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 248 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 249 | // Type Parsing. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 250 | bool ParseType(Type *&Result, bool AllowVoid = false); |
| 251 | bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 252 | Loc = Lex.getLoc(); |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 253 | return ParseType(Result, AllowVoid); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 254 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 255 | bool ParseAnonStructType(Type *&Result, bool Packed); |
| 256 | bool ParseStructBody(SmallVectorImpl<Type*> &Body); |
| 257 | bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name, |
| 258 | std::pair<Type*, LocTy> &Entry, |
| 259 | Type *&ResultTy); |
| 260 | |
| 261 | bool ParseArrayVectorType(Type *&Result, bool isVector); |
| 262 | bool ParseFunctionType(Type *&Result); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 263 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 264 | // Function Semantic Analysis. |
| 265 | class PerFunctionState { |
| 266 | LLParser &P; |
| 267 | Function &F; |
| 268 | std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals; |
| 269 | std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs; |
| 270 | std::vector<Value*> NumberedVals; |
Michael Ilseman | 407a616 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 09d9ef4 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 272 | /// FunctionNumber - If this is an unnamed function, this is the slot |
| 273 | /// number of it, otherwise it is -1. |
| 274 | int FunctionNumber; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 275 | public: |
Chris Lattner | 09d9ef4 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 276 | PerFunctionState(LLParser &p, Function &f, int FunctionNumber); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 277 | ~PerFunctionState(); |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 278 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 279 | Function &getFunction() const { return F; } |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 280 | |
Chris Lattner | 09d9ef4 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 281 | bool FinishFunction(); |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 282 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 283 | /// GetVal - Get a value with the specified name or ID, creating a |
| 284 | /// forward reference record if needed. This can return null if the value |
| 285 | /// exists but does not have the right type. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 286 | Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc); |
| 287 | Value *GetVal(unsigned ID, Type *Ty, LocTy Loc); |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 288 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 289 | /// SetInstName - After an instruction is parsed and inserted into its |
| 290 | /// basic block, this installs its name. |
| 291 | bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc, |
| 292 | Instruction *Inst); |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 293 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 294 | /// GetBB - Get a basic block with the specified name or ID, creating a |
| 295 | /// forward reference record if needed. This can return null if the value |
| 296 | /// is not a BasicBlock. |
| 297 | BasicBlock *GetBB(const std::string &Name, LocTy Loc); |
| 298 | BasicBlock *GetBB(unsigned ID, LocTy Loc); |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 299 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 300 | /// DefineBB - Define the specified basic block, which is either named or |
| 301 | /// unnamed. If there is an error, this returns null otherwise it returns |
| 302 | /// the block being defined. |
| 303 | BasicBlock *DefineBB(const std::string &Name, LocTy Loc); |
| 304 | }; |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 305 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 306 | bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V, |
Victor Hernandez | 92f238d | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 307 | PerFunctionState *PFS); |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 308 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 309 | bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS); |
| 310 | bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 311 | return ParseValue(Ty, V, &PFS); |
| 312 | } |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 313 | bool ParseValue(Type *Ty, Value *&V, LocTy &Loc, |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 314 | PerFunctionState &PFS) { |
| 315 | Loc = Lex.getLoc(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 316 | return ParseValue(Ty, V, &PFS); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 317 | } |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 319 | bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS); |
| 320 | bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) { |
| 321 | return ParseTypeAndValue(V, &PFS); |
| 322 | } |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 323 | bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) { |
| 324 | Loc = Lex.getLoc(); |
| 325 | return ParseTypeAndValue(V, PFS); |
| 326 | } |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 327 | bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, |
| 328 | PerFunctionState &PFS); |
| 329 | bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) { |
| 330 | LocTy Loc; |
| 331 | return ParseTypeAndBasicBlock(BB, Loc, PFS); |
| 332 | } |
Victor Hernandez | 1971556 | 2009-12-03 23:40:58 +0000 | [diff] [blame] | 333 | |
Chris Lattner | fdfeb69 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 334 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 335 | struct ParamInfo { |
| 336 | LocTy Loc; |
| 337 | Value *V; |
Bill Wendling | 73dee18 | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 338 | AttributeSet Attrs; |
| 339 | ParamInfo(LocTy loc, Value *v, AttributeSet attrs) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 340 | : Loc(loc), V(v), Attrs(attrs) {} |
| 341 | }; |
| 342 | bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, |
| 343 | PerFunctionState &PFS); |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 344 | |
Victor Hernandez | bf170d4 | 2010-01-05 22:22:14 +0000 | [diff] [blame] | 345 | // Constant Parsing. |
| 346 | bool ParseValID(ValID &ID, PerFunctionState *PFS = NULL); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 347 | bool ParseGlobalValue(Type *Ty, Constant *&V); |
Victor Hernandez | bf170d4 | 2010-01-05 22:22:14 +0000 | [diff] [blame] | 348 | bool ParseGlobalTypeAndValue(Constant *&V); |
| 349 | bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts); |
Dan Gohman | 309b3af | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 350 | bool ParseMetadataListValue(ValID &ID, PerFunctionState *PFS); |
Dan Gohman | 8344803 | 2010-07-14 18:26:50 +0000 | [diff] [blame] | 351 | bool ParseMetadataValue(ValID &ID, PerFunctionState *PFS); |
Victor Hernandez | 24e64df | 2010-01-10 07:14:18 +0000 | [diff] [blame] | 352 | bool ParseMDNodeVector(SmallVectorImpl<Value*> &, PerFunctionState *PFS); |
Dan Gohman | 9d072f5 | 2010-08-24 02:05:17 +0000 | [diff] [blame] | 353 | bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS); |
Victor Hernandez | bf170d4 | 2010-01-05 22:22:14 +0000 | [diff] [blame] | 354 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 355 | // Function Parsing. |
| 356 | struct ArgInfo { |
| 357 | LocTy Loc; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 358 | Type *Ty; |
Bill Wendling | 73dee18 | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 359 | AttributeSet Attrs; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 360 | std::string Name; |
Bill Wendling | 73dee18 | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 361 | ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N) |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 362 | : Loc(L), Ty(ty), Attrs(Attr), Name(N) {} |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 363 | }; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 364 | bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 365 | bool ParseFunctionHeader(Function *&Fn, bool isDefine); |
| 366 | bool ParseFunctionBody(Function &Fn); |
| 367 | bool ParseBasicBlock(PerFunctionState &PFS); |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 368 | |
Chris Lattner | f1bc7ce | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 369 | // Instruction Parsing. Each instruction parsing routine can return with a |
| 370 | // normal result, an error result, or return having eaten an extra comma. |
| 371 | enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 }; |
| 372 | int ParseInstruction(Instruction *&Inst, BasicBlock *BB, |
| 373 | PerFunctionState &PFS); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 374 | bool ParseCmpPredicate(unsigned &Pred, unsigned Opc); |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 375 | |
Chris Lattner | 437544f | 2011-06-17 06:49:41 +0000 | [diff] [blame] | 376 | bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 377 | bool ParseBr(Instruction *&Inst, PerFunctionState &PFS); |
| 378 | bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS); |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 379 | bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 380 | bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS); |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 381 | bool ParseResume(Instruction *&Inst, PerFunctionState &PFS); |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 382 | |
Chris Lattner | e914b59 | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 383 | bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc, |
| 384 | unsigned OperandType); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 385 | bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc); |
| 386 | bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc); |
| 387 | bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc); |
| 388 | bool ParseSelect(Instruction *&I, PerFunctionState &PFS); |
Chris Lattner | 0088a5c | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 389 | bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 390 | bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS); |
| 391 | bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS); |
| 392 | bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS); |
Chris Lattner | a7d7f2c | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 393 | int ParsePHI(Instruction *&I, PerFunctionState &PFS); |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 394 | bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 395 | bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail); |
Chris Lattner | f3a789d | 2011-06-17 03:16:47 +0000 | [diff] [blame] | 396 | int ParseAlloc(Instruction *&I, PerFunctionState &PFS); |
Chris Lattner | fbe910e | 2011-11-27 06:56:53 +0000 | [diff] [blame] | 397 | int ParseLoad(Instruction *&I, PerFunctionState &PFS); |
| 398 | int ParseStore(Instruction *&I, PerFunctionState &PFS); |
Eli Friedman | f03bb26 | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 399 | int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS); |
| 400 | int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS); |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 401 | int ParseFence(Instruction *&I, PerFunctionState &PFS); |
Chris Lattner | a7d7f2c | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 402 | int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS); |
| 403 | int ParseExtractValue(Instruction *&I, PerFunctionState &PFS); |
| 404 | int ParseInsertValue(Instruction *&I, PerFunctionState &PFS); |
Michael Ilseman | 407a616 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 405 | |
| 406 | bool ResolveForwardRefBlockAddresses(Function *TheFn, |
Chris Lattner | 09d9ef4 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 407 | std::vector<std::pair<ValID, GlobalValue*> > &Refs, |
| 408 | PerFunctionState *PFS); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 409 | }; |
| 410 | } // End llvm namespace |
| 411 | |
| 412 | #endif |