TGParser.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //===- TGParser.h - Parser for TableGen Files -------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This class represents the Parser for tablegen files.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_TABLEGEN_TGPARSER_H
  13. #define LLVM_LIB_TABLEGEN_TGPARSER_H
  14. #include "TGLexer.h"
  15. #include "llvm/TableGen/Error.h"
  16. #include "llvm/TableGen/Record.h"
  17. #include <map>
  18. namespace llvm {
  19. class SourceMgr;
  20. class Twine;
  21. struct ForeachLoop;
  22. struct MultiClass;
  23. struct SubClassReference;
  24. struct SubMultiClassReference;
  25. struct LetRecord {
  26. StringInit *Name;
  27. std::vector<unsigned> Bits;
  28. Init *Value;
  29. SMLoc Loc;
  30. LetRecord(StringInit *N, ArrayRef<unsigned> B, Init *V, SMLoc L)
  31. : Name(N), Bits(B), Value(V), Loc(L) {
  32. }
  33. };
  34. /// RecordsEntry - Holds exactly one of a Record, ForeachLoop, or
  35. /// AssertionInfo.
  36. struct RecordsEntry {
  37. std::unique_ptr<Record> Rec;
  38. std::unique_ptr<ForeachLoop> Loop;
  39. std::unique_ptr<Record::AssertionInfo> Assertion;
  40. void dump() const;
  41. RecordsEntry() {}
  42. RecordsEntry(std::unique_ptr<Record> Rec) : Rec(std::move(Rec)) {}
  43. RecordsEntry(std::unique_ptr<ForeachLoop> Loop)
  44. : Loop(std::move(Loop)) {}
  45. RecordsEntry(std::unique_ptr<Record::AssertionInfo> Assertion)
  46. : Assertion(std::move(Assertion)) {}
  47. };
  48. /// ForeachLoop - Record the iteration state associated with a for loop.
  49. /// This is used to instantiate items in the loop body.
  50. ///
  51. /// IterVar is allowed to be null, in which case no iteration variable is
  52. /// defined in the loop at all. (This happens when a ForeachLoop is
  53. /// constructed by desugaring an if statement.)
  54. struct ForeachLoop {
  55. SMLoc Loc;
  56. VarInit *IterVar;
  57. Init *ListValue;
  58. std::vector<RecordsEntry> Entries;
  59. void dump() const;
  60. ForeachLoop(SMLoc Loc, VarInit *IVar, Init *LValue)
  61. : Loc(Loc), IterVar(IVar), ListValue(LValue) {}
  62. };
  63. struct DefsetRecord {
  64. SMLoc Loc;
  65. RecTy *EltTy = nullptr;
  66. SmallVector<Init *, 16> Elements;
  67. };
  68. class TGLocalVarScope {
  69. // A scope to hold local variable definitions from defvar.
  70. std::map<std::string, Init *, std::less<>> vars;
  71. std::unique_ptr<TGLocalVarScope> parent;
  72. public:
  73. TGLocalVarScope() = default;
  74. TGLocalVarScope(std::unique_ptr<TGLocalVarScope> parent)
  75. : parent(std::move(parent)) {}
  76. std::unique_ptr<TGLocalVarScope> extractParent() {
  77. // This is expected to be called just before we are destructed, so
  78. // it doesn't much matter what state we leave 'parent' in.
  79. return std::move(parent);
  80. }
  81. Init *getVar(StringRef Name) const {
  82. auto It = vars.find(Name);
  83. if (It != vars.end())
  84. return It->second;
  85. if (parent)
  86. return parent->getVar(Name);
  87. return nullptr;
  88. }
  89. bool varAlreadyDefined(StringRef Name) const {
  90. // When we check whether a variable is already defined, for the purpose of
  91. // reporting an error on redefinition, we don't look up to the parent
  92. // scope, because it's all right to shadow an outer definition with an
  93. // inner one.
  94. return vars.find(Name) != vars.end();
  95. }
  96. void addVar(StringRef Name, Init *I) {
  97. bool Ins = vars.insert(std::make_pair(std::string(Name), I)).second;
  98. (void)Ins;
  99. assert(Ins && "Local variable already exists");
  100. }
  101. };
  102. struct MultiClass {
  103. Record Rec; // Placeholder for template args and Name.
  104. std::vector<RecordsEntry> Entries;
  105. void dump() const;
  106. MultiClass(StringRef Name, SMLoc Loc, RecordKeeper &Records) :
  107. Rec(Name, Loc, Records) {}
  108. };
  109. class TGParser {
  110. TGLexer Lex;
  111. std::vector<SmallVector<LetRecord, 4>> LetStack;
  112. std::map<std::string, std::unique_ptr<MultiClass>> MultiClasses;
  113. /// Loops - Keep track of any foreach loops we are within.
  114. ///
  115. std::vector<std::unique_ptr<ForeachLoop>> Loops;
  116. SmallVector<DefsetRecord *, 2> Defsets;
  117. /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
  118. /// current value.
  119. MultiClass *CurMultiClass;
  120. /// CurLocalScope - Innermost of the current nested scopes for 'defvar' local
  121. /// variables.
  122. std::unique_ptr<TGLocalVarScope> CurLocalScope;
  123. // Record tracker
  124. RecordKeeper &Records;
  125. // A "named boolean" indicating how to parse identifiers. Usually
  126. // identifiers map to some existing object but in special cases
  127. // (e.g. parsing def names) no such object exists yet because we are
  128. // in the middle of creating in. For those situations, allow the
  129. // parser to ignore missing object errors.
  130. enum IDParseMode {
  131. ParseValueMode, // We are parsing a value we expect to look up.
  132. ParseNameMode, // We are parsing a name of an object that does not yet
  133. // exist.
  134. };
  135. bool NoWarnOnUnusedTemplateArgs = false;
  136. public:
  137. TGParser(SourceMgr &SM, ArrayRef<std::string> Macros, RecordKeeper &records,
  138. const bool NoWarnOnUnusedTemplateArgs = false)
  139. : Lex(SM, Macros), CurMultiClass(nullptr), Records(records),
  140. NoWarnOnUnusedTemplateArgs(NoWarnOnUnusedTemplateArgs) {}
  141. /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
  142. /// routines return true on error, or false on success.
  143. bool ParseFile();
  144. bool Error(SMLoc L, const Twine &Msg) const {
  145. PrintError(L, Msg);
  146. return true;
  147. }
  148. bool TokError(const Twine &Msg) const {
  149. return Error(Lex.getLoc(), Msg);
  150. }
  151. const TGLexer::DependenciesSetTy &getDependencies() const {
  152. return Lex.getDependencies();
  153. }
  154. TGLocalVarScope *PushLocalScope() {
  155. CurLocalScope = std::make_unique<TGLocalVarScope>(std::move(CurLocalScope));
  156. // Returns a pointer to the new scope, so that the caller can pass it back
  157. // to PopLocalScope which will check by assertion that the pushes and pops
  158. // match up properly.
  159. return CurLocalScope.get();
  160. }
  161. void PopLocalScope(TGLocalVarScope *ExpectedStackTop) {
  162. assert(ExpectedStackTop == CurLocalScope.get() &&
  163. "Mismatched pushes and pops of local variable scopes");
  164. CurLocalScope = CurLocalScope->extractParent();
  165. }
  166. private: // Semantic analysis methods.
  167. bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
  168. bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
  169. ArrayRef<unsigned> BitList, Init *V,
  170. bool AllowSelfAssignment = false);
  171. bool AddSubClass(Record *Rec, SubClassReference &SubClass);
  172. bool AddSubClass(RecordsEntry &Entry, SubClassReference &SubClass);
  173. bool AddSubMultiClass(MultiClass *CurMC,
  174. SubMultiClassReference &SubMultiClass);
  175. using SubstStack = SmallVector<std::pair<Init *, Init *>, 8>;
  176. bool addEntry(RecordsEntry E);
  177. bool resolve(const ForeachLoop &Loop, SubstStack &Stack, bool Final,
  178. std::vector<RecordsEntry> *Dest, SMLoc *Loc = nullptr);
  179. bool resolve(const std::vector<RecordsEntry> &Source, SubstStack &Substs,
  180. bool Final, std::vector<RecordsEntry> *Dest,
  181. SMLoc *Loc = nullptr);
  182. bool addDefOne(std::unique_ptr<Record> Rec);
  183. private: // Parser methods.
  184. bool consume(tgtok::TokKind K);
  185. bool ParseObjectList(MultiClass *MC = nullptr);
  186. bool ParseObject(MultiClass *MC);
  187. bool ParseClass();
  188. bool ParseMultiClass();
  189. bool ParseDefm(MultiClass *CurMultiClass);
  190. bool ParseDef(MultiClass *CurMultiClass);
  191. bool ParseDefset();
  192. bool ParseDefvar();
  193. bool ParseForeach(MultiClass *CurMultiClass);
  194. bool ParseIf(MultiClass *CurMultiClass);
  195. bool ParseIfBody(MultiClass *CurMultiClass, StringRef Kind);
  196. bool ParseAssert(MultiClass *CurMultiClass, Record *CurRec = nullptr);
  197. bool ParseTopLevelLet(MultiClass *CurMultiClass);
  198. void ParseLetList(SmallVectorImpl<LetRecord> &Result);
  199. bool ParseObjectBody(Record *CurRec);
  200. bool ParseBody(Record *CurRec);
  201. bool ParseBodyItem(Record *CurRec);
  202. bool ParseTemplateArgList(Record *CurRec);
  203. Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
  204. VarInit *ParseForeachDeclaration(Init *&ForeachListValue);
  205. SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
  206. SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
  207. Init *ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
  208. IDParseMode Mode = ParseValueMode);
  209. Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = nullptr,
  210. IDParseMode Mode = ParseValueMode);
  211. Init *ParseValue(Record *CurRec, RecTy *ItemType = nullptr,
  212. IDParseMode Mode = ParseValueMode);
  213. void ParseValueList(SmallVectorImpl<llvm::Init*> &Result,
  214. Record *CurRec, RecTy *ItemType = nullptr);
  215. bool ParseTemplateArgValueList(SmallVectorImpl<llvm::Init *> &Result,
  216. Record *CurRec, Record *ArgsRec);
  217. void ParseDagArgList(
  218. SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
  219. Record *CurRec);
  220. bool ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges);
  221. bool ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges);
  222. void ParseRangeList(SmallVectorImpl<unsigned> &Result);
  223. bool ParseRangePiece(SmallVectorImpl<unsigned> &Ranges,
  224. TypedInit *FirstItem = nullptr);
  225. RecTy *ParseType();
  226. Init *ParseOperation(Record *CurRec, RecTy *ItemType);
  227. Init *ParseOperationSubstr(Record *CurRec, RecTy *ItemType);
  228. Init *ParseOperationFind(Record *CurRec, RecTy *ItemType);
  229. Init *ParseOperationForEachFilter(Record *CurRec, RecTy *ItemType);
  230. Init *ParseOperationCond(Record *CurRec, RecTy *ItemType);
  231. RecTy *ParseOperatorType();
  232. Init *ParseObjectName(MultiClass *CurMultiClass);
  233. Record *ParseClassID();
  234. MultiClass *ParseMultiClassID();
  235. bool ApplyLetStack(Record *CurRec);
  236. bool ApplyLetStack(RecordsEntry &Entry);
  237. bool CheckTemplateArgValues(SmallVectorImpl<llvm::Init *> &Values,
  238. SMLoc Loc, Record *ArgsRec);
  239. };
  240. } // end namespace llvm
  241. #endif