TGParser.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //===- TGParser.h - Parser for TableGen Files -------------------*- 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 class represents the Parser for tablegen files.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_TABLEGEN_TGPARSER_H
  14. #define LLVM_LIB_TABLEGEN_TGPARSER_H
  15. #include "TGLexer.h"
  16. #include "llvm/ADT/Twine.h"
  17. #include "llvm/Support/SourceMgr.h"
  18. #include "llvm/TableGen/Error.h"
  19. #include "llvm/TableGen/Record.h"
  20. #include <map>
  21. namespace llvm {
  22. class Record;
  23. class RecordVal;
  24. class RecordKeeper;
  25. class RecTy;
  26. class Init;
  27. struct MultiClass;
  28. struct SubClassReference;
  29. struct SubMultiClassReference;
  30. struct LetRecord {
  31. std::string Name;
  32. std::vector<unsigned> Bits;
  33. Init *Value;
  34. SMLoc Loc;
  35. LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
  36. SMLoc L)
  37. : Name(N), Bits(B), Value(V), Loc(L) {
  38. }
  39. };
  40. /// ForeachLoop - Record the iteration state associated with a for loop.
  41. /// This is used to instantiate items in the loop body.
  42. struct ForeachLoop {
  43. VarInit *IterVar;
  44. ListInit *ListValue;
  45. ForeachLoop(VarInit *IVar, ListInit *LValue)
  46. : IterVar(IVar), ListValue(LValue) {}
  47. };
  48. class TGParser {
  49. TGLexer Lex;
  50. std::vector<std::vector<LetRecord> > LetStack;
  51. std::map<std::string, std::unique_ptr<MultiClass>> MultiClasses;
  52. /// Loops - Keep track of any foreach loops we are within.
  53. ///
  54. typedef std::vector<ForeachLoop> LoopVector;
  55. LoopVector Loops;
  56. /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
  57. /// current value.
  58. MultiClass *CurMultiClass;
  59. // Record tracker
  60. RecordKeeper &Records;
  61. unsigned AnonCounter;
  62. // A "named boolean" indicating how to parse identifiers. Usually
  63. // identifiers map to some existing object but in special cases
  64. // (e.g. parsing def names) no such object exists yet because we are
  65. // in the middle of creating in. For those situations, allow the
  66. // parser to ignore missing object errors.
  67. enum IDParseMode {
  68. ParseValueMode, // We are parsing a value we expect to look up.
  69. ParseNameMode, // We are parsing a name of an object that does not yet
  70. // exist.
  71. ParseForeachMode // We are parsing a foreach init.
  72. };
  73. public:
  74. TGParser(SourceMgr &SrcMgr, RecordKeeper &records)
  75. : Lex(SrcMgr), CurMultiClass(nullptr), Records(records), AnonCounter(0) {}
  76. /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
  77. /// routines return true on error, or false on success.
  78. bool ParseFile();
  79. bool Error(SMLoc L, const Twine &Msg) const {
  80. PrintError(L, Msg);
  81. return true;
  82. }
  83. bool TokError(const Twine &Msg) const {
  84. return Error(Lex.getLoc(), Msg);
  85. }
  86. const TGLexer::DependenciesMapTy &getDependencies() const {
  87. return Lex.getDependencies();
  88. }
  89. private: // Semantic analysis methods.
  90. bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
  91. bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
  92. const std::vector<unsigned> &BitList, Init *V);
  93. bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
  94. const std::vector<unsigned> &BitList, Init *V) {
  95. return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V);
  96. }
  97. bool AddSubClass(Record *Rec, SubClassReference &SubClass);
  98. bool AddSubMultiClass(MultiClass *CurMC,
  99. SubMultiClassReference &SubMultiClass);
  100. std::string GetNewAnonymousName();
  101. // IterRecord: Map an iterator name to a value.
  102. struct IterRecord {
  103. VarInit *IterVar;
  104. Init *IterValue;
  105. IterRecord(VarInit *Var, Init *Val) : IterVar(Var), IterValue(Val) {}
  106. };
  107. // IterSet: The set of all iterator values at some point in the
  108. // iteration space.
  109. typedef std::vector<IterRecord> IterSet;
  110. bool ProcessForeachDefs(Record *CurRec, SMLoc Loc);
  111. bool ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals);
  112. private: // Parser methods.
  113. bool ParseObjectList(MultiClass *MC = nullptr);
  114. bool ParseObject(MultiClass *MC);
  115. bool ParseClass();
  116. bool ParseMultiClass();
  117. Record *InstantiateMulticlassDef(MultiClass &MC,
  118. Record *DefProto,
  119. Init *&DefmPrefix,
  120. SMRange DefmPrefixRange,
  121. const std::vector<Init *> &TArgs,
  122. std::vector<Init *> &TemplateVals);
  123. bool ResolveMulticlassDefArgs(MultiClass &MC,
  124. Record *DefProto,
  125. SMLoc DefmPrefixLoc,
  126. SMLoc SubClassLoc,
  127. const std::vector<Init *> &TArgs,
  128. std::vector<Init *> &TemplateVals,
  129. bool DeleteArgs);
  130. bool ResolveMulticlassDef(MultiClass &MC,
  131. Record *CurRec,
  132. Record *DefProto,
  133. SMLoc DefmPrefixLoc);
  134. bool ParseDefm(MultiClass *CurMultiClass);
  135. bool ParseDef(MultiClass *CurMultiClass);
  136. bool ParseForeach(MultiClass *CurMultiClass);
  137. bool ParseTopLevelLet(MultiClass *CurMultiClass);
  138. std::vector<LetRecord> ParseLetList();
  139. bool ParseObjectBody(Record *CurRec);
  140. bool ParseBody(Record *CurRec);
  141. bool ParseBodyItem(Record *CurRec);
  142. bool ParseTemplateArgList(Record *CurRec);
  143. Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
  144. VarInit *ParseForeachDeclaration(ListInit *&ForeachListValue);
  145. SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
  146. SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
  147. Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc,
  148. IDParseMode Mode = ParseValueMode);
  149. Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = nullptr,
  150. IDParseMode Mode = ParseValueMode);
  151. Init *ParseValue(Record *CurRec, RecTy *ItemType = nullptr,
  152. IDParseMode Mode = ParseValueMode);
  153. std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = nullptr,
  154. RecTy *EltTy = nullptr);
  155. std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
  156. bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
  157. bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
  158. std::vector<unsigned> ParseRangeList();
  159. bool ParseRangePiece(std::vector<unsigned> &Ranges);
  160. RecTy *ParseType();
  161. Init *ParseOperation(Record *CurRec, RecTy *ItemType);
  162. RecTy *ParseOperatorType();
  163. Init *ParseObjectName(MultiClass *CurMultiClass);
  164. Record *ParseClassID();
  165. MultiClass *ParseMultiClassID();
  166. bool ApplyLetStack(Record *CurRec);
  167. };
  168. } // end namespace llvm
  169. #endif