LLParser.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. #ifndef LLVM_LIB_ASMPARSER_LLPARSER_H
  14. #define LLVM_LIB_ASMPARSER_LLPARSER_H
  15. #include "LLLexer.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/StringMap.h"
  18. #include "llvm/IR/Attributes.h"
  19. #include "llvm/IR/Instructions.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/IR/Operator.h"
  22. #include "llvm/IR/Type.h"
  23. #include "llvm/IR/ValueHandle.h"
  24. #include <map>
  25. namespace llvm {
  26. class Module;
  27. class OpaqueType;
  28. class Function;
  29. class Value;
  30. class BasicBlock;
  31. class Instruction;
  32. class Constant;
  33. class GlobalValue;
  34. class Comdat;
  35. class MDString;
  36. class MDNode;
  37. struct SlotMapping;
  38. class StructType;
  39. /// ValID - Represents a reference of a definition of some sort with no type.
  40. /// There are several cases where we have to parse the value but where the
  41. /// type can depend on later context. This may either be a numeric reference
  42. /// or a symbolic (%var) reference. This is just a discriminated union.
  43. struct ValID {
  44. enum {
  45. t_LocalID, t_GlobalID, // ID in UIntVal.
  46. t_LocalName, t_GlobalName, // Name in StrVal.
  47. t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal.
  48. t_Null, t_Undef, t_Zero, // No value.
  49. t_EmptyArray, // No value: []
  50. t_Constant, // Value in ConstantVal.
  51. t_InlineAsm, // Value in StrVal/StrVal2/UIntVal.
  52. t_ConstantStruct, // Value in ConstantStructElts.
  53. t_PackedConstantStruct // Value in ConstantStructElts.
  54. } Kind;
  55. LLLexer::LocTy Loc;
  56. unsigned UIntVal;
  57. std::string StrVal, StrVal2;
  58. APSInt APSIntVal;
  59. APFloat APFloatVal;
  60. Constant *ConstantVal;
  61. Constant **ConstantStructElts;
  62. ValID() : Kind(t_LocalID), APFloatVal(0.0) {}
  63. ~ValID() {
  64. if (Kind == t_ConstantStruct || Kind == t_PackedConstantStruct)
  65. delete [] ConstantStructElts;
  66. }
  67. bool operator<(const ValID &RHS) const {
  68. if (Kind == t_LocalID || Kind == t_GlobalID)
  69. return UIntVal < RHS.UIntVal;
  70. assert((Kind == t_LocalName || Kind == t_GlobalName ||
  71. Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) &&
  72. "Ordering not defined for this ValID kind yet");
  73. return StrVal < RHS.StrVal;
  74. }
  75. };
  76. class LLParser {
  77. public:
  78. typedef LLLexer::LocTy LocTy;
  79. private:
  80. LLVMContext &Context;
  81. LLLexer Lex;
  82. Module *M;
  83. SlotMapping *Slots;
  84. // Instruction metadata resolution. Each instruction can have a list of
  85. // MDRef info associated with them.
  86. //
  87. // The simpler approach of just creating temporary MDNodes and then calling
  88. // RAUW on them when the definition is processed doesn't work because some
  89. // instruction metadata kinds, such as dbg, get stored in the IR in an
  90. // "optimized" format which doesn't participate in the normal value use
  91. // lists. This means that RAUW doesn't work, even on temporary MDNodes
  92. // which otherwise support RAUW. Instead, we defer resolving MDNode
  93. // references until the definitions have been processed.
  94. struct MDRef {
  95. SMLoc Loc;
  96. unsigned MDKind, MDSlot;
  97. };
  98. SmallVector<Instruction*, 64> InstsWithTBAATag;
  99. // Type resolution handling data structures. The location is set when we
  100. // have processed a use of the type but not a definition yet.
  101. StringMap<std::pair<Type*, LocTy> > NamedTypes;
  102. std::map<unsigned, std::pair<Type*, LocTy> > NumberedTypes;
  103. std::map<unsigned, TrackingMDNodeRef> NumberedMetadata;
  104. std::map<unsigned, std::pair<TempMDTuple, LocTy>> ForwardRefMDNodes;
  105. // Global Value reference information.
  106. std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
  107. std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
  108. std::vector<GlobalValue*> NumberedVals;
  109. // Comdat forward reference information.
  110. std::map<std::string, LocTy> ForwardRefComdats;
  111. // References to blockaddress. The key is the function ValID, the value is
  112. // a list of references to blocks in that function.
  113. std::map<ValID, std::map<ValID, GlobalValue *>> ForwardRefBlockAddresses;
  114. class PerFunctionState;
  115. /// Reference to per-function state to allow basic blocks to be
  116. /// forward-referenced by blockaddress instructions within the same
  117. /// function.
  118. PerFunctionState *BlockAddressPFS;
  119. // Attribute builder reference information.
  120. std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups;
  121. std::map<unsigned, AttrBuilder> NumberedAttrBuilders;
  122. public:
  123. LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *M,
  124. SlotMapping *Slots = nullptr)
  125. : Context(M->getContext()), Lex(F, SM, Err, M->getContext()), M(M),
  126. Slots(Slots), BlockAddressPFS(nullptr) {}
  127. bool Run();
  128. LLVMContext &getContext() { return Context; }
  129. private:
  130. bool Error(LocTy L, const Twine &Msg) const {
  131. return Lex.Error(L, Msg);
  132. }
  133. bool TokError(const Twine &Msg) const {
  134. return Error(Lex.getLoc(), Msg);
  135. }
  136. /// GetGlobalVal - Get a value with the specified name or ID, creating a
  137. /// forward reference record if needed. This can return null if the value
  138. /// exists but does not have the right type.
  139. GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc);
  140. GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc);
  141. /// Get a Comdat with the specified name, creating a forward reference
  142. /// record if needed.
  143. Comdat *getComdat(const std::string &N, LocTy Loc);
  144. // Helper Routines.
  145. bool ParseToken(lltok::Kind T, const char *ErrMsg);
  146. bool EatIfPresent(lltok::Kind T) {
  147. if (Lex.getKind() != T) return false;
  148. Lex.Lex();
  149. return true;
  150. }
  151. FastMathFlags EatFastMathFlagsIfPresent() {
  152. FastMathFlags FMF;
  153. while (true)
  154. switch (Lex.getKind()) {
  155. case lltok::kw_fast: FMF.setUnsafeAlgebra(); Lex.Lex(); continue;
  156. case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue;
  157. case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue;
  158. case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue;
  159. case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue;
  160. default: return FMF;
  161. }
  162. return FMF;
  163. }
  164. bool ParseOptionalToken(lltok::Kind T, bool &Present,
  165. LocTy *Loc = nullptr) {
  166. if (Lex.getKind() != T) {
  167. Present = false;
  168. } else {
  169. if (Loc)
  170. *Loc = Lex.getLoc();
  171. Lex.Lex();
  172. Present = true;
  173. }
  174. return false;
  175. }
  176. bool ParseStringConstant(std::string &Result);
  177. bool ParseUInt32(unsigned &Val);
  178. bool ParseUInt32(unsigned &Val, LocTy &Loc) {
  179. Loc = Lex.getLoc();
  180. return ParseUInt32(Val);
  181. }
  182. bool ParseUInt64(uint64_t &Val);
  183. bool ParseUInt64(uint64_t &Val, LocTy &Loc) {
  184. Loc = Lex.getLoc();
  185. return ParseUInt64(Val);
  186. }
  187. bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
  188. bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
  189. bool parseOptionalUnnamedAddr(bool &UnnamedAddr) {
  190. return ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr);
  191. }
  192. bool ParseOptionalAddrSpace(unsigned &AddrSpace);
  193. bool ParseOptionalParamAttrs(AttrBuilder &B);
  194. bool ParseOptionalReturnAttrs(AttrBuilder &B);
  195. bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
  196. bool ParseOptionalLinkage(unsigned &Linkage) {
  197. bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
  198. }
  199. bool ParseOptionalVisibility(unsigned &Visibility);
  200. bool ParseOptionalDLLStorageClass(unsigned &DLLStorageClass);
  201. bool ParseOptionalCallingConv(unsigned &CC);
  202. bool ParseOptionalAlignment(unsigned &Alignment);
  203. bool ParseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes);
  204. bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
  205. AtomicOrdering &Ordering);
  206. bool ParseOrdering(AtomicOrdering &Ordering);
  207. bool ParseOptionalStackAlignment(unsigned &Alignment);
  208. bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
  209. bool ParseOptionalCommaInAlloca(bool &IsInAlloca);
  210. bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma);
  211. bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
  212. bool AteExtraComma;
  213. if (ParseIndexList(Indices, AteExtraComma)) return true;
  214. if (AteExtraComma)
  215. return TokError("expected index");
  216. return false;
  217. }
  218. // Top-Level Entities
  219. bool ParseTopLevelEntities();
  220. bool ValidateEndOfModule();
  221. bool ParseTargetDefinition();
  222. bool ParseModuleAsm();
  223. bool ParseDepLibs(); // FIXME: Remove in 4.0.
  224. bool ParseUnnamedType();
  225. bool ParseNamedType();
  226. bool ParseDeclare();
  227. bool ParseDefine();
  228. bool ParseGlobalType(bool &IsConstant);
  229. bool ParseUnnamedGlobal();
  230. bool ParseNamedGlobal();
  231. bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
  232. bool HasLinkage, unsigned Visibility,
  233. unsigned DLLStorageClass,
  234. GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
  235. bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Linkage,
  236. unsigned Visibility, unsigned DLLStorageClass,
  237. GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
  238. bool parseComdat();
  239. bool ParseStandaloneMetadata();
  240. bool ParseNamedMetadata();
  241. bool ParseMDString(MDString *&Result);
  242. bool ParseMDNodeID(MDNode *&Result);
  243. bool ParseUnnamedAttrGrp();
  244. bool ParseFnAttributeValuePairs(AttrBuilder &B,
  245. std::vector<unsigned> &FwdRefAttrGrps,
  246. bool inAttrGrp, LocTy &BuiltinLoc);
  247. // Type Parsing.
  248. bool ParseType(Type *&Result, const Twine &Msg, bool AllowVoid = false);
  249. bool ParseType(Type *&Result, bool AllowVoid = false) {
  250. return ParseType(Result, "expected type", AllowVoid);
  251. }
  252. bool ParseType(Type *&Result, const Twine &Msg, LocTy &Loc,
  253. bool AllowVoid = false) {
  254. Loc = Lex.getLoc();
  255. return ParseType(Result, Msg, AllowVoid);
  256. }
  257. bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
  258. Loc = Lex.getLoc();
  259. return ParseType(Result, AllowVoid);
  260. }
  261. bool ParseAnonStructType(Type *&Result, bool Packed);
  262. bool ParseStructBody(SmallVectorImpl<Type*> &Body);
  263. bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
  264. std::pair<Type*, LocTy> &Entry,
  265. Type *&ResultTy);
  266. bool ParseArrayVectorType(Type *&Result, bool isVector);
  267. bool ParseFunctionType(Type *&Result);
  268. // Function Semantic Analysis.
  269. class PerFunctionState {
  270. LLParser &P;
  271. Function &F;
  272. std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
  273. std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
  274. std::vector<Value*> NumberedVals;
  275. /// FunctionNumber - If this is an unnamed function, this is the slot
  276. /// number of it, otherwise it is -1.
  277. int FunctionNumber;
  278. public:
  279. PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
  280. ~PerFunctionState();
  281. Function &getFunction() const { return F; }
  282. bool FinishFunction();
  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.
  286. Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc);
  287. Value *GetVal(unsigned ID, Type *Ty, LocTy Loc);
  288. /// SetInstName - After an instruction is parsed and inserted into its
  289. /// basic block, this installs its name.
  290. bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
  291. Instruction *Inst);
  292. /// GetBB - Get a basic block with the specified name or ID, creating a
  293. /// forward reference record if needed. This can return null if the value
  294. /// is not a BasicBlock.
  295. BasicBlock *GetBB(const std::string &Name, LocTy Loc);
  296. BasicBlock *GetBB(unsigned ID, LocTy Loc);
  297. /// DefineBB - Define the specified basic block, which is either named or
  298. /// unnamed. If there is an error, this returns null otherwise it returns
  299. /// the block being defined.
  300. BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
  301. bool resolveForwardRefBlockAddresses();
  302. };
  303. bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
  304. PerFunctionState *PFS);
  305. bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
  306. bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
  307. return ParseValue(Ty, V, &PFS);
  308. }
  309. bool ParseValue(Type *Ty, Value *&V, LocTy &Loc,
  310. PerFunctionState &PFS) {
  311. Loc = Lex.getLoc();
  312. return ParseValue(Ty, V, &PFS);
  313. }
  314. bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS);
  315. bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
  316. return ParseTypeAndValue(V, &PFS);
  317. }
  318. bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
  319. Loc = Lex.getLoc();
  320. return ParseTypeAndValue(V, PFS);
  321. }
  322. bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
  323. PerFunctionState &PFS);
  324. bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
  325. LocTy Loc;
  326. return ParseTypeAndBasicBlock(BB, Loc, PFS);
  327. }
  328. struct ParamInfo {
  329. LocTy Loc;
  330. Value *V;
  331. AttributeSet Attrs;
  332. ParamInfo(LocTy loc, Value *v, AttributeSet attrs)
  333. : Loc(loc), V(v), Attrs(attrs) {}
  334. };
  335. bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
  336. PerFunctionState &PFS,
  337. bool IsMustTailCall = false,
  338. bool InVarArgsFunc = false);
  339. // Constant Parsing.
  340. bool ParseValID(ValID &ID, PerFunctionState *PFS = nullptr);
  341. bool ParseGlobalValue(Type *Ty, Constant *&V);
  342. bool ParseGlobalTypeAndValue(Constant *&V);
  343. bool ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts);
  344. bool parseOptionalComdat(StringRef GlobalName, Comdat *&C);
  345. bool ParseMetadataAsValue(Value *&V, PerFunctionState &PFS);
  346. bool ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
  347. PerFunctionState *PFS);
  348. bool ParseMetadata(Metadata *&MD, PerFunctionState *PFS);
  349. bool ParseMDTuple(MDNode *&MD, bool IsDistinct = false);
  350. bool ParseMDNode(MDNode *&MD);
  351. bool ParseMDNodeTail(MDNode *&MD);
  352. bool ParseMDNodeVector(SmallVectorImpl<Metadata *> &MDs);
  353. bool ParseMetadataAttachment(unsigned &Kind, MDNode *&MD);
  354. bool ParseInstructionMetadata(Instruction &Inst);
  355. bool ParseOptionalFunctionMetadata(Function &F);
  356. template <class FieldTy>
  357. bool ParseMDField(LocTy Loc, StringRef Name, FieldTy &Result);
  358. template <class FieldTy> bool ParseMDField(StringRef Name, FieldTy &Result);
  359. template <class ParserTy>
  360. bool ParseMDFieldsImplBody(ParserTy parseField);
  361. template <class ParserTy>
  362. bool ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc);
  363. bool ParseSpecializedMDNode(MDNode *&N, bool IsDistinct = false);
  364. #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
  365. bool Parse##CLASS(MDNode *&Result, bool IsDistinct);
  366. #include "llvm/IR/Metadata.def"
  367. // Function Parsing.
  368. struct ArgInfo {
  369. LocTy Loc;
  370. Type *Ty;
  371. AttributeSet Attrs;
  372. std::string Name;
  373. ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N)
  374. : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
  375. };
  376. bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg);
  377. bool ParseFunctionHeader(Function *&Fn, bool isDefine);
  378. bool ParseFunctionBody(Function &Fn);
  379. bool ParseBasicBlock(PerFunctionState &PFS);
  380. enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
  381. // Instruction Parsing. Each instruction parsing routine can return with a
  382. // normal result, an error result, or return having eaten an extra comma.
  383. enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
  384. int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
  385. PerFunctionState &PFS);
  386. bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
  387. bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
  388. bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
  389. bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
  390. bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
  391. bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
  392. bool ParseResume(Instruction *&Inst, PerFunctionState &PFS);
  393. bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
  394. unsigned OperandType);
  395. bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
  396. bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
  397. bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
  398. bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
  399. bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
  400. bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
  401. bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
  402. bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
  403. int ParsePHI(Instruction *&I, PerFunctionState &PFS);
  404. bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS);
  405. bool ParseCall(Instruction *&I, PerFunctionState &PFS,
  406. CallInst::TailCallKind IsTail);
  407. int ParseAlloc(Instruction *&I, PerFunctionState &PFS);
  408. int ParseLoad(Instruction *&I, PerFunctionState &PFS);
  409. int ParseStore(Instruction *&I, PerFunctionState &PFS);
  410. int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS);
  411. int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS);
  412. int ParseFence(Instruction *&I, PerFunctionState &PFS);
  413. int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
  414. int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
  415. int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
  416. // Use-list order directives.
  417. bool ParseUseListOrder(PerFunctionState *PFS = nullptr);
  418. bool ParseUseListOrderBB();
  419. bool ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes);
  420. bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc);
  421. };
  422. } // End llvm namespace
  423. #endif