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 //===----------------------------------------------------------------------===//
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(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(VarInit *IVar, ListInit *LValue)
52 : IterVar(IVar), ListValue(LValue) {}
57 std::vector<std::vector<LetRecord> > LetStack;
58 std::map<std::string, MultiClass*> MultiClasses;
60 /// Loops - Keep track of any foreach loops we are within.
62 typedef std::vector<ForeachLoop> LoopVector;
65 /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
67 MultiClass *CurMultiClass;
70 RecordKeeper &Records;
72 // A "named boolean" indicating how to parse identifiers. Usually
73 // identifiers map to some existing object but in special cases
74 // (e.g. parsing def names) no such object exists yet because we are
75 // in the middle of creating in. For those situations, allow the
76 // parser to ignore missing object errors.
78 ParseValueMode, // We are parsing a value we expect to look up.
79 ParseNameMode, // We are parsing a name of an object that does not yet
81 ParseForeachMode // We are parsing a foreach init.
85 TGParser(SourceMgr &SrcMgr, RecordKeeper &records) :
86 Lex(SrcMgr), CurMultiClass(0), Records(records) {}
88 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
89 /// routines return true on error, or false on success.
92 bool Error(SMLoc L, const Twine &Msg) const {
96 bool TokError(const Twine &Msg) const {
97 return Error(Lex.getLoc(), Msg);
99 const TGLexer::DependenciesMapTy &getDependencies() const {
100 return Lex.getDependencies();
103 private: // Semantic analysis methods.
104 bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
105 bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
106 const std::vector<unsigned> &BitList, Init *V);
107 bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
108 const std::vector<unsigned> &BitList, Init *V) {
109 return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V);
111 bool AddSubClass(Record *Rec, SubClassReference &SubClass);
112 bool AddSubMultiClass(MultiClass *CurMC,
113 SubMultiClassReference &SubMultiClass);
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 = 0);
131 bool ParseObject(MultiClass *MC);
133 bool ParseMultiClass();
134 Record *InstantiateMulticlassDef(MultiClass &MC,
137 SMRange DefmPrefixRange);
138 bool ResolveMulticlassDefArgs(MultiClass &MC,
142 const std::vector<Init *> &TArgs,
143 std::vector<Init *> &TemplateVals,
145 bool ResolveMulticlassDef(MultiClass &MC,
148 SMLoc DefmPrefixLoc);
149 bool ParseDefm(MultiClass *CurMultiClass);
150 bool ParseDef(MultiClass *CurMultiClass);
151 bool ParseForeach(MultiClass *CurMultiClass);
152 bool ParseTopLevelLet(MultiClass *CurMultiClass);
153 std::vector<LetRecord> ParseLetList();
155 bool ParseObjectBody(Record *CurRec);
156 bool ParseBody(Record *CurRec);
157 bool ParseBodyItem(Record *CurRec);
159 bool ParseTemplateArgList(Record *CurRec);
160 Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
161 VarInit *ParseForeachDeclaration(ListInit *&ForeachListValue);
163 SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
164 SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
166 Init *ParseIDValue(Record *CurRec, IDParseMode Mode = ParseValueMode);
167 Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc,
168 IDParseMode Mode = ParseValueMode);
169 Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0,
170 IDParseMode Mode = ParseValueMode);
171 Init *ParseValue(Record *CurRec, RecTy *ItemType = 0,
172 IDParseMode Mode = ParseValueMode);
173 std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0,
175 std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
176 bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
177 bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
178 std::vector<unsigned> ParseRangeList();
179 bool ParseRangePiece(std::vector<unsigned> &Ranges);
181 Init *ParseOperation(Record *CurRec);
182 RecTy *ParseOperatorType();
183 Init *ParseObjectName(MultiClass *CurMultiClass);
184 Record *ParseClassID();
185 MultiClass *ParseMultiClassID();
186 bool ApplyLetStack(Record *CurRec);
189 } // end namespace llvm