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