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 //===----------------------------------------------------------------------===//
17 #include "llvm/TableGen/Record.h"
19 #include "llvm/TableGen/Error.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/SourceMgr.h"
31 struct SubClassReference;
32 struct SubMultiClassReference;
36 std::vector<unsigned> Bits;
39 LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
41 : Name(N), Bits(B), Value(V), Loc(L) {
45 /// ForeachLoop - Record the iteration state associated with a for loop.
46 /// This is used to instantiate items in the loop body.
51 ForeachLoop(Init *IVar, Init *LValue) : IterVar(IVar), ListValue(LValue) {};
56 std::vector<std::vector<LetRecord> > LetStack;
57 std::map<std::string, 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;
71 // A "named boolean" indicating how to parse identifiers. Usually
72 // identifiers map to some existing object but in special cases
73 // (e.g. parsing def names) no such object exists yet because we are
74 // in the middle of creating in. For those situations, allow the
75 // parser to ignore missing object errors.
77 ParseValueMode, // We are parsing a value we expect to look up.
78 ParseNameMode, // We are parsing a name of an object that does not yet
80 ParseForeachMode // We are parsing a foreach init.
84 TGParser(SourceMgr &SrcMgr, RecordKeeper &records) :
85 Lex(SrcMgr), CurMultiClass(0), Records(records) {}
87 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
88 /// routines return true on error, or false on success.
91 bool Error(SMLoc L, const Twine &Msg) const {
95 bool TokError(const Twine &Msg) const {
96 return Error(Lex.getLoc(), Msg);
98 const std::vector<std::string> &getDependencies() const {
99 return Lex.getDependencies();
102 private: // Semantic analysis methods.
103 bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
104 bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
105 const std::vector<unsigned> &BitList, Init *V);
106 bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
107 const std::vector<unsigned> &BitList, Init *V) {
108 return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V);
110 bool AddSubClass(Record *Rec, SubClassReference &SubClass);
111 bool AddSubMultiClass(MultiClass *CurMC,
112 SubMultiClassReference &SubMultiClass);
114 // IterRecord: Map an iterator name to a value.
118 IterRecord(Init *Var, Init *Val) : IterVar(Var), IterValue(Val) {}
121 // IterSet: The set of all iterator values at some point in the
123 typedef std::vector<IterRecord> IterSet;
125 bool ProcessForeachDefs(Record *CurRec, MultiClass *CurMultiClass,
127 bool ProcessForeachDefs(Record *CurRec, MultiClass *CurMultiClass,
128 SMLoc Loc, IterSet &IterVals, ForeachLoop &CurLoop,
129 LoopVector::iterator NextLoop);
131 private: // Parser methods.
132 bool ParseObjectList(MultiClass *MC = 0);
133 bool ParseObject(MultiClass *MC);
135 bool ParseMultiClass();
136 Record *InstantiateMulticlassDef(MultiClass &MC,
139 SMLoc DefmPrefixLoc);
140 bool ResolveMulticlassDefArgs(MultiClass &MC,
144 const std::vector<Init *> &TArgs,
145 std::vector<Init *> &TemplateVals,
147 bool ResolveMulticlassDef(MultiClass &MC,
150 SMLoc DefmPrefixLoc);
151 bool ParseDefm(MultiClass *CurMultiClass);
152 bool ParseDef(MultiClass *CurMultiClass);
153 bool ParseForeach(MultiClass *CurMultiClass);
154 bool ParseTopLevelLet(MultiClass *CurMultiClass);
155 std::vector<LetRecord> ParseLetList();
157 bool ParseObjectBody(Record *CurRec);
158 bool ParseBody(Record *CurRec);
159 bool ParseBodyItem(Record *CurRec);
161 bool ParseTemplateArgList(Record *CurRec);
162 Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
163 Init *ParseForeachDeclaration(Init *&ForeachListValue);
165 SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
166 SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
168 Init *ParseIDValue(Record *CurRec, IDParseMode Mode = ParseValueMode);
169 Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc,
170 IDParseMode Mode = ParseValueMode);
171 Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0,
172 IDParseMode Mode = ParseValueMode);
173 Init *ParseValue(Record *CurRec, RecTy *ItemType = 0,
174 IDParseMode Mode = ParseValueMode);
175 std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0, RecTy *EltTy = 0);
176 std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
177 bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
178 bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
179 std::vector<unsigned> ParseRangeList();
180 bool ParseRangePiece(std::vector<unsigned> &Ranges);
182 Init *ParseOperation(Record *CurRec);
183 RecTy *ParseOperatorType();
184 Init *ParseObjectName(MultiClass *CurMultiClass);
185 Record *ParseClassID();
186 MultiClass *ParseMultiClassID();
187 Record *ParseDefmID();
190 } // end namespace llvm