1 //===- TGParser.h - Parser for TableGen Files -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class represents the Parser for tablegen files.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_TABLEGEN_TGPARSER_H
15 #define LLVM_LIB_TABLEGEN_TGPARSER_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"
31 struct SubClassReference;
32 struct SubMultiClassReference;
36 std::vector<unsigned> Bits;
39 LetRecord(StringInit *N, ArrayRef<unsigned> B, Init *V, SMLoc L)
40 : Name(N), Bits(B), Value(V), Loc(L) {
44 /// ForeachLoop - Record the iteration state associated with a for loop.
45 /// This is used to instantiate items in the loop body.
50 ForeachLoop(VarInit *IVar, ListInit *LValue)
51 : IterVar(IVar), ListValue(LValue) {}
56 std::vector<SmallVector<LetRecord, 4>> LetStack;
57 std::map<std::string, std::unique_ptr<MultiClass>> MultiClasses;
59 /// Loops - Keep track of any foreach loops we are within.
61 typedef std::vector<ForeachLoop> LoopVector;
64 /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
66 MultiClass *CurMultiClass;
69 RecordKeeper &Records;
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.
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
82 ParseForeachMode // We are parsing a foreach init.
86 TGParser(SourceMgr &SrcMgr, RecordKeeper &records)
87 : Lex(SrcMgr), CurMultiClass(nullptr), Records(records), AnonCounter(0) {}
89 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
90 /// routines return true on error, or false on success.
93 bool Error(SMLoc L, const Twine &Msg) const {
97 bool TokError(const Twine &Msg) const {
98 return Error(Lex.getLoc(), Msg);
100 const TGLexer::DependenciesMapTy &getDependencies() const {
101 return Lex.getDependencies();
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);
113 std::string GetNewAnonymousName();
115 // IterRecord: Map an iterator name to a value.
119 IterRecord(VarInit *Var, Init *Val) : IterVar(Var), IterValue(Val) {}
122 // IterSet: The set of all iterator values at some point in the
124 typedef std::vector<IterRecord> IterSet;
126 bool ProcessForeachDefs(Record *CurRec, SMLoc Loc);
127 bool ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals);
129 private: // Parser methods.
130 bool ParseObjectList(MultiClass *MC = nullptr);
131 bool ParseObject(MultiClass *MC);
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,
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);
152 bool ParseObjectBody(Record *CurRec);
153 bool ParseBody(Record *CurRec);
154 bool ParseBodyItem(Record *CurRec);
156 bool ParseTemplateArgList(Record *CurRec);
157 Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
158 VarInit *ParseForeachDeclaration(ListInit *&ForeachListValue);
160 SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
161 SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
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,
174 bool ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges);
175 bool ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges);
176 void ParseRangeList(SmallVectorImpl<unsigned> &Result);
177 bool ParseRangePiece(SmallVectorImpl<unsigned> &Ranges);
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);
187 } // end namespace llvm