LLVM 17.0.0git
TGParser.h
Go to the documentation of this file.
1//===- TGParser.h - Parser for TableGen Files -------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This class represents the Parser for tablegen files.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TABLEGEN_TGPARSER_H
14#define LLVM_LIB_TABLEGEN_TGPARSER_H
15
16#include "TGLexer.h"
17#include "llvm/TableGen/Error.h"
19#include <map>
20
21namespace llvm {
22 class SourceMgr;
23 class Twine;
24 struct ForeachLoop;
25 struct MultiClass;
26 struct SubClassReference;
27 struct SubMultiClassReference;
28
29 struct LetRecord {
31 std::vector<unsigned> Bits;
35 : Name(N), Bits(B), Value(V), Loc(L) {
36 }
37 };
38
39 /// RecordsEntry - Holds exactly one of a Record, ForeachLoop, or
40 /// AssertionInfo.
41 struct RecordsEntry {
42 std::unique_ptr<Record> Rec;
43 std::unique_ptr<ForeachLoop> Loop;
44 std::unique_ptr<Record::AssertionInfo> Assertion;
45
46 void dump() const;
47
48 RecordsEntry() = default;
49 RecordsEntry(std::unique_ptr<Record> Rec) : Rec(std::move(Rec)) {}
50 RecordsEntry(std::unique_ptr<ForeachLoop> Loop)
51 : Loop(std::move(Loop)) {}
52 RecordsEntry(std::unique_ptr<Record::AssertionInfo> Assertion)
54 };
55
56 /// ForeachLoop - Record the iteration state associated with a for loop.
57 /// This is used to instantiate items in the loop body.
58 ///
59 /// IterVar is allowed to be null, in which case no iteration variable is
60 /// defined in the loop at all. (This happens when a ForeachLoop is
61 /// constructed by desugaring an if statement.)
62 struct ForeachLoop {
66 std::vector<RecordsEntry> Entries;
67
68 void dump() const;
69
71 : Loc(Loc), IterVar(IVar), ListValue(LValue) {}
72 };
73
74 struct DefsetRecord {
76 RecTy *EltTy = nullptr;
78 };
79
80 struct MultiClass {
81 Record Rec; // Placeholder for template args and Name.
82 std::vector<RecordsEntry> Entries;
83
84 void dump() const;
85
87 : Rec(Name, Loc, Records) {}
88 };
89
90 class TGVarScope {
91 public:
93
94 private:
95 ScopeKind Kind;
96 std::unique_ptr<TGVarScope> Parent;
97 // A scope to hold variable definitions from defvar.
98 std::map<std::string, Init *, std::less<>> Vars;
99 Record *CurRec = nullptr;
100 ForeachLoop *CurLoop = nullptr;
101 MultiClass *CurMultiClass = nullptr;
102
103 public:
104 TGVarScope(std::unique_ptr<TGVarScope> Parent)
105 : Kind(SK_Local), Parent(std::move(Parent)) {}
106 TGVarScope(std::unique_ptr<TGVarScope> Parent, Record *Rec)
107 : Kind(SK_Record), Parent(std::move(Parent)), CurRec(Rec) {}
108 TGVarScope(std::unique_ptr<TGVarScope> Parent, ForeachLoop *Loop)
109 : Kind(SK_ForeachLoop), Parent(std::move(Parent)), CurLoop(Loop) {}
110 TGVarScope(std::unique_ptr<TGVarScope> Parent, MultiClass *Multiclass)
111 : Kind(SK_MultiClass), Parent(std::move(Parent)),
112 CurMultiClass(Multiclass) {}
113
114 std::unique_ptr<TGVarScope> extractParent() {
115 // This is expected to be called just before we are destructed, so
116 // it doesn't much matter what state we leave 'parent' in.
117 return std::move(Parent);
118 }
119
120 Init *getVar(RecordKeeper &Records, MultiClass *ParsingMultiClass,
121 StringInit *Name, SMRange NameLoc,
122 bool TrackReferenceLocs) const;
123
125 // When we check whether a variable is already defined, for the purpose of
126 // reporting an error on redefinition, we don't look up to the parent
127 // scope, because it's all right to shadow an outer definition with an
128 // inner one.
129 return Vars.find(Name) != Vars.end();
130 }
131
133 bool Ins = Vars.insert(std::make_pair(std::string(Name), I)).second;
134 (void)Ins;
135 assert(Ins && "Local variable already exists");
136 }
137
138 bool isOutermost() const { return Parent == nullptr; }
139 };
140
141class TGParser {
142 TGLexer Lex;
143 std::vector<SmallVector<LetRecord, 4>> LetStack;
144 std::map<std::string, std::unique_ptr<MultiClass>> MultiClasses;
145
146 /// Loops - Keep track of any foreach loops we are within.
147 ///
148 std::vector<std::unique_ptr<ForeachLoop>> Loops;
149
151
152 /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
153 /// current value.
154 MultiClass *CurMultiClass;
155
156 /// CurScope - Innermost of the current nested scopes for 'defvar' variables.
157 std::unique_ptr<TGVarScope> CurScope;
158
159 // Record tracker
160 RecordKeeper &Records;
161
162 // A "named boolean" indicating how to parse identifiers. Usually
163 // identifiers map to some existing object but in special cases
164 // (e.g. parsing def names) no such object exists yet because we are
165 // in the middle of creating in. For those situations, allow the
166 // parser to ignore missing object errors.
167 enum IDParseMode {
168 ParseValueMode, // We are parsing a value we expect to look up.
169 ParseNameMode, // We are parsing a name of an object that does not yet
170 // exist.
171 };
172
173 bool NoWarnOnUnusedTemplateArgs = false;
174 bool TrackReferenceLocs = false;
175
176public:
178 const bool NoWarnOnUnusedTemplateArgs = false,
179 const bool TrackReferenceLocs = false)
180 : Lex(SM, Macros), CurMultiClass(nullptr), Records(records),
181 NoWarnOnUnusedTemplateArgs(NoWarnOnUnusedTemplateArgs),
182 TrackReferenceLocs(TrackReferenceLocs) {}
183
184 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
185 /// routines return true on error, or false on success.
186 bool ParseFile();
187
188 bool Error(SMLoc L, const Twine &Msg) const {
189 PrintError(L, Msg);
190 return true;
191 }
192 bool TokError(const Twine &Msg) const {
193 return Error(Lex.getLoc(), Msg);
194 }
196 return Lex.getDependencies();
197 }
198
200 CurScope = std::make_unique<TGVarScope>(std::move(CurScope));
201 // Returns a pointer to the new scope, so that the caller can pass it back
202 // to PopScope which will check by assertion that the pushes and pops
203 // match up properly.
204 return CurScope.get();
205 }
207 CurScope = std::make_unique<TGVarScope>(std::move(CurScope), Rec);
208 return CurScope.get();
209 }
211 CurScope = std::make_unique<TGVarScope>(std::move(CurScope), Loop);
212 return CurScope.get();
213 }
215 CurScope = std::make_unique<TGVarScope>(std::move(CurScope), Multiclass);
216 return CurScope.get();
217 }
218 void PopScope(TGVarScope *ExpectedStackTop) {
219 assert(ExpectedStackTop == CurScope.get() &&
220 "Mismatched pushes and pops of local variable scopes");
221 CurScope = CurScope->extractParent();
222 }
223
224private: // Semantic analysis methods.
225 bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
226 /// Set the value of a RecordVal within the given record. If `OverrideDefLoc`
227 /// is set, the provided location overrides any existing location of the
228 /// RecordVal.
229 bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
230 ArrayRef<unsigned> BitList, Init *V,
231 bool AllowSelfAssignment = false, bool OverrideDefLoc = true);
232 bool AddSubClass(Record *Rec, SubClassReference &SubClass);
233 bool AddSubClass(RecordsEntry &Entry, SubClassReference &SubClass);
234 bool AddSubMultiClass(MultiClass *CurMC,
235 SubMultiClassReference &SubMultiClass);
236
237 using SubstStack = SmallVector<std::pair<Init *, Init *>, 8>;
238
239 bool addEntry(RecordsEntry E);
240 bool resolve(const ForeachLoop &Loop, SubstStack &Stack, bool Final,
241 std::vector<RecordsEntry> *Dest, SMLoc *Loc = nullptr);
242 bool resolve(const std::vector<RecordsEntry> &Source, SubstStack &Substs,
243 bool Final, std::vector<RecordsEntry> *Dest,
244 SMLoc *Loc = nullptr);
245 bool addDefOne(std::unique_ptr<Record> Rec);
246
247private: // Parser methods.
248 bool consume(tgtok::TokKind K);
249 bool ParseObjectList(MultiClass *MC = nullptr);
250 bool ParseObject(MultiClass *MC);
251 bool ParseClass();
252 bool ParseMultiClass();
253 bool ParseDefm(MultiClass *CurMultiClass);
254 bool ParseDef(MultiClass *CurMultiClass);
255 bool ParseDefset();
256 bool ParseDefvar(Record *CurRec = nullptr);
257 bool ParseForeach(MultiClass *CurMultiClass);
258 bool ParseIf(MultiClass *CurMultiClass);
259 bool ParseIfBody(MultiClass *CurMultiClass, StringRef Kind);
260 bool ParseAssert(MultiClass *CurMultiClass, Record *CurRec = nullptr);
261 bool ParseTopLevelLet(MultiClass *CurMultiClass);
262 void ParseLetList(SmallVectorImpl<LetRecord> &Result);
263
264 bool ParseObjectBody(Record *CurRec);
265 bool ParseBody(Record *CurRec);
266 bool ParseBodyItem(Record *CurRec);
267
268 bool ParseTemplateArgList(Record *CurRec);
269 Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
270 VarInit *ParseForeachDeclaration(Init *&ForeachListValue);
271
272 SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
273 SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
274
275 Init *ParseIDValue(Record *CurRec, StringInit *Name, SMRange NameLoc,
276 IDParseMode Mode = ParseValueMode);
277 Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = nullptr,
278 IDParseMode Mode = ParseValueMode);
279 Init *ParseValue(Record *CurRec, RecTy *ItemType = nullptr,
280 IDParseMode Mode = ParseValueMode);
281 void ParseValueList(SmallVectorImpl<llvm::Init*> &Result,
282 Record *CurRec, RecTy *ItemType = nullptr);
283 bool ParseTemplateArgValueList(SmallVectorImpl<llvm::Init *> &Result,
284 Record *CurRec, Record *ArgsRec);
285 void ParseDagArgList(
286 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
287 Record *CurRec);
288 bool ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges);
289 bool ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges);
290 TypedInit *ParseSliceElement(Record *CurRec);
291 TypedInit *ParseSliceElements(Record *CurRec, bool Single = false);
292 void ParseRangeList(SmallVectorImpl<unsigned> &Result);
293 bool ParseRangePiece(SmallVectorImpl<unsigned> &Ranges,
294 TypedInit *FirstItem = nullptr);
295 RecTy *ParseType();
296 Init *ParseOperation(Record *CurRec, RecTy *ItemType);
297 Init *ParseOperationSubstr(Record *CurRec, RecTy *ItemType);
298 Init *ParseOperationFind(Record *CurRec, RecTy *ItemType);
299 Init *ParseOperationForEachFilter(Record *CurRec, RecTy *ItemType);
300 Init *ParseOperationCond(Record *CurRec, RecTy *ItemType);
301 RecTy *ParseOperatorType();
302 Init *ParseObjectName(MultiClass *CurMultiClass);
303 Record *ParseClassID();
304 MultiClass *ParseMultiClassID();
305 bool ApplyLetStack(Record *CurRec);
306 bool ApplyLetStack(RecordsEntry &Entry);
307 bool CheckTemplateArgValues(SmallVectorImpl<llvm::Init *> &Values,
308 SMLoc Loc, Record *ArgsRec);
309};
310
311} // end namespace llvm
312
313#endif
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
static cl::opt< RegAllocEvictionAdvisorAnalysis::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Development, "development", "for training")))
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:47
This class represents a field in a record, including its name, type, value, and source location.
Definition: Record.h:1473
Represents a location in source code.
Definition: SMLoc.h:23
Represents a range in source code.
Definition: SMLoc.h:48
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
"foo" - Represent an initialization by a string value.
Definition: Record.h:627
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
TGLexer - TableGen Lexer class.
Definition: TGLexer.h:163
SMLoc getLoc() const
Definition: TGLexer.cpp:62
std::set< std::string > DependenciesSetTy
Definition: TGLexer.h:180
const DependenciesSetTy & getDependencies() const
Definition: TGLexer.h:193
void PopScope(TGVarScope *ExpectedStackTop)
Definition: TGParser.h:218
const TGLexer::DependenciesSetTy & getDependencies() const
Definition: TGParser.h:195
bool Error(SMLoc L, const Twine &Msg) const
Definition: TGParser.h:188
TGVarScope * PushScope(ForeachLoop *Loop)
Definition: TGParser.h:210
TGVarScope * PushScope(Record *Rec)
Definition: TGParser.h:206
bool TokError(const Twine &Msg) const
Definition: TGParser.h:192
TGParser(SourceMgr &SM, ArrayRef< std::string > Macros, RecordKeeper &records, const bool NoWarnOnUnusedTemplateArgs=false, const bool TrackReferenceLocs=false)
Definition: TGParser.h:177
TGVarScope * PushScope(MultiClass *Multiclass)
Definition: TGParser.h:214
bool ParseFile()
ParseFile - Main entrypoint for parsing a tblgen file.
Definition: TGParser.cpp:4249
TGVarScope * PushScope()
Definition: TGParser.h:199
Init * getVar(RecordKeeper &Records, MultiClass *ParsingMultiClass, StringInit *Name, SMRange NameLoc, bool TrackReferenceLocs) const
Definition: TGParser.cpp:142
TGVarScope(std::unique_ptr< TGVarScope > Parent, Record *Rec)
Definition: TGParser.h:106
std::unique_ptr< TGVarScope > extractParent()
Definition: TGParser.h:114
bool isOutermost() const
Definition: TGParser.h:138
TGVarScope(std::unique_ptr< TGVarScope > Parent, ForeachLoop *Loop)
Definition: TGParser.h:108
void addVar(StringRef Name, Init *I)
Definition: TGParser.h:132
bool varAlreadyDefined(StringRef Name) const
Definition: TGParser.h:124
TGVarScope(std::unique_ptr< TGVarScope > Parent, MultiClass *Multiclass)
Definition: TGParser.h:110
TGVarScope(std::unique_ptr< TGVarScope > Parent)
Definition: TGParser.h:104
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
This is the common superclass of types that have a specific, explicit type, stored in ValueTy.
Definition: Record.h:411
LLVM Value Representation.
Definition: Value.h:74
'Opcode' - Represent a reference to an entire variable object.
Definition: Record.h:1172
@ MultiClass
Definition: TGLexer.h:79
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void PrintError(const Twine &Msg)
Definition: Error.cpp:101
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1946
Definition: BitVector.h:858
#define N
SmallVector< Init *, 16 > Elements
Definition: TGParser.h:77
ForeachLoop - Record the iteration state associated with a for loop.
Definition: TGParser.h:62
std::vector< RecordsEntry > Entries
Definition: TGParser.h:66
VarInit * IterVar
Definition: TGParser.h:64
ForeachLoop(SMLoc Loc, VarInit *IVar, Init *LValue)
Definition: TGParser.h:70
void dump() const
Definition: TGParser.cpp:4306
Init * ListValue
Definition: TGParser.h:65
StringInit * Name
Definition: TGParser.h:30
std::vector< unsigned > Bits
Definition: TGParser.h:31
LetRecord(StringInit *N, ArrayRef< unsigned > B, Init *V, SMLoc L)
Definition: TGParser.h:34
Init * Value
Definition: TGParser.h:32
std::vector< RecordsEntry > Entries
Definition: TGParser.h:82
void dump() const
Definition: TGParser.cpp:4316
MultiClass(StringRef Name, SMLoc Loc, RecordKeeper &Records)
Definition: TGParser.h:86
RecordsEntry - Holds exactly one of a Record, ForeachLoop, or AssertionInfo.
Definition: TGParser.h:41
RecordsEntry()=default
RecordsEntry(std::unique_ptr< ForeachLoop > Loop)
Definition: TGParser.h:50
std::unique_ptr< ForeachLoop > Loop
Definition: TGParser.h:43
std::unique_ptr< Record::AssertionInfo > Assertion
Definition: TGParser.h:44
void dump() const
Definition: TGParser.cpp:4299
RecordsEntry(std::unique_ptr< Record > Rec)
Definition: TGParser.h:49
std::unique_ptr< Record > Rec
Definition: TGParser.h:42
RecordsEntry(std::unique_ptr< Record::AssertionInfo > Assertion)
Definition: TGParser.h:52