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(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, std::unique_ptr<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;
74 // A "named boolean" indicating how to parse identifiers. Usually
75 // identifiers map to some existing object but in special cases
76 // (e.g. parsing def names) no such object exists yet because we are
77 // in the middle of creating in. For those situations, allow the
78 // parser to ignore missing object errors.
80 ParseValueMode, // We are parsing a value we expect to look up.
81 ParseNameMode, // We are parsing a name of an object that does not yet
83 ParseForeachMode // We are parsing a foreach init.
87 TGParser(SourceMgr &SrcMgr, RecordKeeper &records)
88 : Lex(SrcMgr), CurMultiClass(nullptr), Records(records), AnonCounter(0) {}
90 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
91 /// routines return true on error, or false on success.
94 bool Error(SMLoc L, const Twine &Msg) const {
98 bool TokError(const Twine &Msg) const {
99 return Error(Lex.getLoc(), Msg);
101 const TGLexer::DependenciesMapTy &getDependencies() const {
102 return Lex.getDependencies();
105 private: // Semantic analysis methods.
106 bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
107 bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
108 ArrayRef<unsigned> BitList, Init *V,
109 bool AllowSelfAssignment = false);
110 bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
111 ArrayRef<unsigned> BitList, Init *V,
112 bool AllowSelfAssignment = false) {
113 return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V,
114 AllowSelfAssignment);
116 bool AddSubClass(Record *Rec, SubClassReference &SubClass);
117 bool AddSubMultiClass(MultiClass *CurMC,
118 SubMultiClassReference &SubMultiClass);
120 std::string GetNewAnonymousName();
122 // IterRecord: Map an iterator name to a value.
126 IterRecord(VarInit *Var, Init *Val) : IterVar(Var), IterValue(Val) {}
129 // IterSet: The set of all iterator values at some point in the
131 typedef std::vector<IterRecord> IterSet;
133 bool ProcessForeachDefs(Record *CurRec, SMLoc Loc);
134 bool ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals);
136 private: // Parser methods.
137 bool ParseObjectList(MultiClass *MC = nullptr);
138 bool ParseObject(MultiClass *MC);
140 bool ParseMultiClass();
141 Record *InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
142 Init *&DefmPrefix, SMRange DefmPrefixRange,
143 ArrayRef<Init *> TArgs,
144 std::vector<Init *> &TemplateVals);
145 bool ResolveMulticlassDefArgs(MultiClass &MC, Record *DefProto,
146 SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
147 ArrayRef<Init *> TArgs,
148 std::vector<Init *> &TemplateVals,
150 bool ResolveMulticlassDef(MultiClass &MC,
153 SMLoc DefmPrefixLoc);
154 bool ParseDefm(MultiClass *CurMultiClass);
155 bool ParseDef(MultiClass *CurMultiClass);
156 bool ParseForeach(MultiClass *CurMultiClass);
157 bool ParseTopLevelLet(MultiClass *CurMultiClass);
158 std::vector<LetRecord> ParseLetList();
160 bool ParseObjectBody(Record *CurRec);
161 bool ParseBody(Record *CurRec);
162 bool ParseBodyItem(Record *CurRec);
164 bool ParseTemplateArgList(Record *CurRec);
165 Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
166 VarInit *ParseForeachDeclaration(ListInit *&ForeachListValue);
168 SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
169 SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
171 Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc,
172 IDParseMode Mode = ParseValueMode);
173 Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = nullptr,
174 IDParseMode Mode = ParseValueMode);
175 Init *ParseValue(Record *CurRec, RecTy *ItemType = nullptr,
176 IDParseMode Mode = ParseValueMode);
177 std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = nullptr,
178 RecTy *EltTy = nullptr);
179 std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
180 bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
181 bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
182 std::vector<unsigned> ParseRangeList();
183 bool ParseRangePiece(std::vector<unsigned> &Ranges);
185 Init *ParseOperation(Record *CurRec, RecTy *ItemType);
186 RecTy *ParseOperatorType();
187 Init *ParseObjectName(MultiClass *CurMultiClass);
188 Record *ParseClassID();
189 MultiClass *ParseMultiClassID();
190 bool ApplyLetStack(Record *CurRec);
193 } // end namespace llvm