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