LLVM API Documentation
00001 //===- TGParser.h - Parser for TableGen Files -------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This class represents the Parser for tablegen files. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_LIB_TABLEGEN_TGPARSER_H 00015 #define LLVM_LIB_TABLEGEN_TGPARSER_H 00016 00017 #include "TGLexer.h" 00018 #include "llvm/ADT/Twine.h" 00019 #include "llvm/Support/SourceMgr.h" 00020 #include "llvm/TableGen/Error.h" 00021 #include "llvm/TableGen/Record.h" 00022 #include <map> 00023 00024 namespace llvm { 00025 class Record; 00026 class RecordVal; 00027 class RecordKeeper; 00028 class RecTy; 00029 class Init; 00030 struct MultiClass; 00031 struct SubClassReference; 00032 struct SubMultiClassReference; 00033 00034 struct LetRecord { 00035 std::string Name; 00036 std::vector<unsigned> Bits; 00037 Init *Value; 00038 SMLoc Loc; 00039 LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V, 00040 SMLoc L) 00041 : Name(N), Bits(B), Value(V), Loc(L) { 00042 } 00043 }; 00044 00045 /// ForeachLoop - Record the iteration state associated with a for loop. 00046 /// This is used to instantiate items in the loop body. 00047 struct ForeachLoop { 00048 VarInit *IterVar; 00049 ListInit *ListValue; 00050 00051 ForeachLoop(VarInit *IVar, ListInit *LValue) 00052 : IterVar(IVar), ListValue(LValue) {} 00053 }; 00054 00055 class TGParser { 00056 TGLexer Lex; 00057 std::vector<std::vector<LetRecord> > LetStack; 00058 std::map<std::string, MultiClass*> MultiClasses; 00059 00060 /// Loops - Keep track of any foreach loops we are within. 00061 /// 00062 typedef std::vector<ForeachLoop> LoopVector; 00063 LoopVector Loops; 00064 00065 /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the 00066 /// current value. 00067 MultiClass *CurMultiClass; 00068 00069 // Record tracker 00070 RecordKeeper &Records; 00071 00072 unsigned AnonCounter; 00073 00074 // A "named boolean" indicating how to parse identifiers. Usually 00075 // identifiers map to some existing object but in special cases 00076 // (e.g. parsing def names) no such object exists yet because we are 00077 // in the middle of creating in. For those situations, allow the 00078 // parser to ignore missing object errors. 00079 enum IDParseMode { 00080 ParseValueMode, // We are parsing a value we expect to look up. 00081 ParseNameMode, // We are parsing a name of an object that does not yet 00082 // exist. 00083 ParseForeachMode // We are parsing a foreach init. 00084 }; 00085 00086 public: 00087 TGParser(SourceMgr &SrcMgr, RecordKeeper &records) 00088 : Lex(SrcMgr), CurMultiClass(nullptr), Records(records), AnonCounter(0) {} 00089 00090 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser 00091 /// routines return true on error, or false on success. 00092 bool ParseFile(); 00093 00094 bool Error(SMLoc L, const Twine &Msg) const { 00095 PrintError(L, Msg); 00096 return true; 00097 } 00098 bool TokError(const Twine &Msg) const { 00099 return Error(Lex.getLoc(), Msg); 00100 } 00101 const TGLexer::DependenciesMapTy &getDependencies() const { 00102 return Lex.getDependencies(); 00103 } 00104 00105 private: // Semantic analysis methods. 00106 bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV); 00107 bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName, 00108 const std::vector<unsigned> &BitList, Init *V); 00109 bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName, 00110 const std::vector<unsigned> &BitList, Init *V) { 00111 return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V); 00112 } 00113 bool AddSubClass(Record *Rec, SubClassReference &SubClass); 00114 bool AddSubMultiClass(MultiClass *CurMC, 00115 SubMultiClassReference &SubMultiClass); 00116 00117 std::string GetNewAnonymousName(); 00118 00119 // IterRecord: Map an iterator name to a value. 00120 struct IterRecord { 00121 VarInit *IterVar; 00122 Init *IterValue; 00123 IterRecord(VarInit *Var, Init *Val) : IterVar(Var), IterValue(Val) {} 00124 }; 00125 00126 // IterSet: The set of all iterator values at some point in the 00127 // iteration space. 00128 typedef std::vector<IterRecord> IterSet; 00129 00130 bool ProcessForeachDefs(Record *CurRec, SMLoc Loc); 00131 bool ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals); 00132 00133 private: // Parser methods. 00134 bool ParseObjectList(MultiClass *MC = nullptr); 00135 bool ParseObject(MultiClass *MC); 00136 bool ParseClass(); 00137 bool ParseMultiClass(); 00138 Record *InstantiateMulticlassDef(MultiClass &MC, 00139 Record *DefProto, 00140 Init *&DefmPrefix, 00141 SMRange DefmPrefixRange); 00142 bool ResolveMulticlassDefArgs(MultiClass &MC, 00143 Record *DefProto, 00144 SMLoc DefmPrefixLoc, 00145 SMLoc SubClassLoc, 00146 const std::vector<Init *> &TArgs, 00147 std::vector<Init *> &TemplateVals, 00148 bool DeleteArgs); 00149 bool ResolveMulticlassDef(MultiClass &MC, 00150 Record *CurRec, 00151 Record *DefProto, 00152 SMLoc DefmPrefixLoc); 00153 bool ParseDefm(MultiClass *CurMultiClass); 00154 bool ParseDef(MultiClass *CurMultiClass); 00155 bool ParseForeach(MultiClass *CurMultiClass); 00156 bool ParseTopLevelLet(MultiClass *CurMultiClass); 00157 std::vector<LetRecord> ParseLetList(); 00158 00159 bool ParseObjectBody(Record *CurRec); 00160 bool ParseBody(Record *CurRec); 00161 bool ParseBodyItem(Record *CurRec); 00162 00163 bool ParseTemplateArgList(Record *CurRec); 00164 Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs); 00165 VarInit *ParseForeachDeclaration(ListInit *&ForeachListValue); 00166 00167 SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm); 00168 SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC); 00169 00170 Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc, 00171 IDParseMode Mode = ParseValueMode); 00172 Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = nullptr, 00173 IDParseMode Mode = ParseValueMode); 00174 Init *ParseValue(Record *CurRec, RecTy *ItemType = nullptr, 00175 IDParseMode Mode = ParseValueMode); 00176 std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = nullptr, 00177 RecTy *EltTy = nullptr); 00178 std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *); 00179 bool ParseOptionalRangeList(std::vector<unsigned> &Ranges); 00180 bool ParseOptionalBitList(std::vector<unsigned> &Ranges); 00181 std::vector<unsigned> ParseRangeList(); 00182 bool ParseRangePiece(std::vector<unsigned> &Ranges); 00183 RecTy *ParseType(); 00184 Init *ParseOperation(Record *CurRec, RecTy *ItemType); 00185 RecTy *ParseOperatorType(); 00186 Init *ParseObjectName(MultiClass *CurMultiClass); 00187 Record *ParseClassID(); 00188 MultiClass *ParseMultiClassID(); 00189 bool ApplyLetStack(Record *CurRec); 00190 }; 00191 00192 } // end namespace llvm 00193 00194 #endif