LLVM  16.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"
18 #include "llvm/TableGen/Record.h"
19 #include <map>
20 
21 namespace 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)
53  : Assertion(std::move(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 
70  ForeachLoop(SMLoc Loc, VarInit *IVar, Init *LValue)
71  : Loc(Loc), IterVar(IVar), ListValue(LValue) {}
72  };
73 
74  struct DefsetRecord {
76  RecTy *EltTy = nullptr;
78  };
79 
81  // A scope to hold local variable definitions from defvar.
82  std::map<std::string, Init *, std::less<>> vars;
83  std::unique_ptr<TGLocalVarScope> parent;
84 
85 public:
86  TGLocalVarScope() = default;
87  TGLocalVarScope(std::unique_ptr<TGLocalVarScope> parent)
88  : parent(std::move(parent)) {}
89 
90  std::unique_ptr<TGLocalVarScope> extractParent() {
91  // This is expected to be called just before we are destructed, so
92  // it doesn't much matter what state we leave 'parent' in.
93  return std::move(parent);
94  }
95 
96  Init *getVar(StringRef Name) const {
97  auto It = vars.find(Name);
98  if (It != vars.end())
99  return It->second;
100  if (parent)
101  return parent->getVar(Name);
102  return nullptr;
103  }
104 
105  bool varAlreadyDefined(StringRef Name) const {
106  // When we check whether a variable is already defined, for the purpose of
107  // reporting an error on redefinition, we don't look up to the parent
108  // scope, because it's all right to shadow an outer definition with an
109  // inner one.
110  return vars.find(Name) != vars.end();
111  }
112 
113  void addVar(StringRef Name, Init *I) {
114  bool Ins = vars.insert(std::make_pair(std::string(Name), I)).second;
115  (void)Ins;
116  assert(Ins && "Local variable already exists");
117  }
118 };
119 
120 struct MultiClass {
121  Record Rec; // Placeholder for template args and Name.
122  std::vector<RecordsEntry> Entries;
123 
124  void dump() const;
125 
126  MultiClass(StringRef Name, SMLoc Loc, RecordKeeper &Records) :
127  Rec(Name, Loc, Records) {}
128 };
129 
130 class TGParser {
131  TGLexer Lex;
132  std::vector<SmallVector<LetRecord, 4>> LetStack;
133  std::map<std::string, std::unique_ptr<MultiClass>> MultiClasses;
134 
135  /// Loops - Keep track of any foreach loops we are within.
136  ///
137  std::vector<std::unique_ptr<ForeachLoop>> Loops;
138 
140 
141  /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
142  /// current value.
143  MultiClass *CurMultiClass;
144 
145  /// CurLocalScope - Innermost of the current nested scopes for 'defvar' local
146  /// variables.
147  std::unique_ptr<TGLocalVarScope> CurLocalScope;
148 
149  // Record tracker
150  RecordKeeper &Records;
151 
152  // A "named boolean" indicating how to parse identifiers. Usually
153  // identifiers map to some existing object but in special cases
154  // (e.g. parsing def names) no such object exists yet because we are
155  // in the middle of creating in. For those situations, allow the
156  // parser to ignore missing object errors.
157  enum IDParseMode {
158  ParseValueMode, // We are parsing a value we expect to look up.
159  ParseNameMode, // We are parsing a name of an object that does not yet
160  // exist.
161  };
162 
163  bool NoWarnOnUnusedTemplateArgs = false;
164  bool TrackReferenceLocs = false;
165 
166 public:
168  const bool NoWarnOnUnusedTemplateArgs = false,
169  const bool TrackReferenceLocs = false)
170  : Lex(SM, Macros), CurMultiClass(nullptr), Records(records),
171  NoWarnOnUnusedTemplateArgs(NoWarnOnUnusedTemplateArgs),
172  TrackReferenceLocs(TrackReferenceLocs) {}
173 
174  /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
175  /// routines return true on error, or false on success.
176  bool ParseFile();
177 
178  bool Error(SMLoc L, const Twine &Msg) const {
179  PrintError(L, Msg);
180  return true;
181  }
182  bool TokError(const Twine &Msg) const {
183  return Error(Lex.getLoc(), Msg);
184  }
186  return Lex.getDependencies();
187  }
188 
190  CurLocalScope = std::make_unique<TGLocalVarScope>(std::move(CurLocalScope));
191  // Returns a pointer to the new scope, so that the caller can pass it back
192  // to PopLocalScope which will check by assertion that the pushes and pops
193  // match up properly.
194  return CurLocalScope.get();
195  }
196  void PopLocalScope(TGLocalVarScope *ExpectedStackTop) {
197  assert(ExpectedStackTop == CurLocalScope.get() &&
198  "Mismatched pushes and pops of local variable scopes");
199  CurLocalScope = CurLocalScope->extractParent();
200  }
201 
202 private: // Semantic analysis methods.
203  bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
204  /// Set the value of a RecordVal within the given record. If `OverrideDefLoc`
205  /// is set, the provided location overrides any existing location of the
206  /// RecordVal.
207  bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
208  ArrayRef<unsigned> BitList, Init *V,
209  bool AllowSelfAssignment = false, bool OverrideDefLoc = true);
210  bool AddSubClass(Record *Rec, SubClassReference &SubClass);
211  bool AddSubClass(RecordsEntry &Entry, SubClassReference &SubClass);
212  bool AddSubMultiClass(MultiClass *CurMC,
213  SubMultiClassReference &SubMultiClass);
214 
215  using SubstStack = SmallVector<std::pair<Init *, Init *>, 8>;
216 
217  bool addEntry(RecordsEntry E);
218  bool resolve(const ForeachLoop &Loop, SubstStack &Stack, bool Final,
219  std::vector<RecordsEntry> *Dest, SMLoc *Loc = nullptr);
220  bool resolve(const std::vector<RecordsEntry> &Source, SubstStack &Substs,
221  bool Final, std::vector<RecordsEntry> *Dest,
222  SMLoc *Loc = nullptr);
223  bool addDefOne(std::unique_ptr<Record> Rec);
224 
225 private: // Parser methods.
226  bool consume(tgtok::TokKind K);
227  bool ParseObjectList(MultiClass *MC = nullptr);
228  bool ParseObject(MultiClass *MC);
229  bool ParseClass();
230  bool ParseMultiClass();
231  bool ParseDefm(MultiClass *CurMultiClass);
232  bool ParseDef(MultiClass *CurMultiClass);
233  bool ParseDefset();
234  bool ParseDefvar();
235  bool ParseForeach(MultiClass *CurMultiClass);
236  bool ParseIf(MultiClass *CurMultiClass);
237  bool ParseIfBody(MultiClass *CurMultiClass, StringRef Kind);
238  bool ParseAssert(MultiClass *CurMultiClass, Record *CurRec = nullptr);
239  bool ParseTopLevelLet(MultiClass *CurMultiClass);
240  void ParseLetList(SmallVectorImpl<LetRecord> &Result);
241 
242  bool ParseObjectBody(Record *CurRec);
243  bool ParseBody(Record *CurRec);
244  bool ParseBodyItem(Record *CurRec);
245 
246  bool ParseTemplateArgList(Record *CurRec);
247  Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
248  VarInit *ParseForeachDeclaration(Init *&ForeachListValue);
249 
250  SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
251  SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
252 
253  Init *ParseIDValue(Record *CurRec, StringInit *Name, SMRange NameLoc,
254  IDParseMode Mode = ParseValueMode);
255  Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = nullptr,
256  IDParseMode Mode = ParseValueMode);
257  Init *ParseValue(Record *CurRec, RecTy *ItemType = nullptr,
258  IDParseMode Mode = ParseValueMode);
259  void ParseValueList(SmallVectorImpl<llvm::Init*> &Result,
260  Record *CurRec, RecTy *ItemType = nullptr);
261  bool ParseTemplateArgValueList(SmallVectorImpl<llvm::Init *> &Result,
262  Record *CurRec, Record *ArgsRec);
263  void ParseDagArgList(
264  SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
265  Record *CurRec);
266  bool ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges);
267  bool ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges);
268  void ParseRangeList(SmallVectorImpl<unsigned> &Result);
269  bool ParseRangePiece(SmallVectorImpl<unsigned> &Ranges,
270  TypedInit *FirstItem = nullptr);
271  RecTy *ParseType();
272  Init *ParseOperation(Record *CurRec, RecTy *ItemType);
273  Init *ParseOperationSubstr(Record *CurRec, RecTy *ItemType);
274  Init *ParseOperationFind(Record *CurRec, RecTy *ItemType);
275  Init *ParseOperationForEachFilter(Record *CurRec, RecTy *ItemType);
276  Init *ParseOperationCond(Record *CurRec, RecTy *ItemType);
277  RecTy *ParseOperatorType();
278  Init *ParseObjectName(MultiClass *CurMultiClass);
279  Record *ParseClassID();
280  MultiClass *ParseMultiClassID();
281  bool ApplyLetStack(Record *CurRec);
282  bool ApplyLetStack(RecordsEntry &Entry);
283  bool CheckTemplateArgValues(SmallVectorImpl<llvm::Init *> &Values,
284  SMLoc Loc, Record *ArgsRec);
285 };
286 
287 } // end namespace llvm
288 
289 #endif
llvm::RecordsEntry::RecordsEntry
RecordsEntry(std::unique_ptr< ForeachLoop > Loop)
Definition: TGParser.h:50
llvm::ForeachLoop::dump
void dump() const
Definition: TGParser.cpp:3896
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::RecordKeeper
Definition: Record.h:1888
llvm::TGParser::TokError
bool TokError(const Twine &Msg) const
Definition: TGParser.h:182
llvm::RecordsEntry::RecordsEntry
RecordsEntry(std::unique_ptr< Record > Rec)
Definition: TGParser.h:49
llvm::Loop
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:547
llvm::MultiClass::Rec
Record Rec
Definition: TGParser.h:121
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::LetRecord::Loc
SMLoc Loc
Definition: TGParser.h:33
llvm::TGParser::PushLocalScope
TGLocalVarScope * PushLocalScope()
Definition: TGParser.h:189
llvm::TGLocalVarScope::varAlreadyDefined
bool varAlreadyDefined(StringRef Name) const
Definition: TGParser.h:105
TGLexer.h
ReferenceKind::LValue
@ LValue
llvm::VarInit
'Opcode' - Represent a reference to an entire variable object.
Definition: Record.h:1155
llvm::RecTy
Definition: Record.h:57
llvm::MultiClass::MultiClass
MultiClass(StringRef Name, SMLoc Loc, RecordKeeper &Records)
Definition: TGParser.h:126
llvm::DefsetRecord::Elements
SmallVector< Init *, 16 > Elements
Definition: TGParser.h:77
llvm::TGLexer::getDependencies
const DependenciesSetTy & getDependencies() const
Definition: TGLexer.h:111
llvm::TGLocalVarScope::extractParent
std::unique_ptr< TGLocalVarScope > extractParent()
Definition: TGParser.h:90
llvm::RecordsEntry::dump
void dump() const
Definition: TGParser.cpp:3889
llvm::MultiClass::dump
void dump() const
Definition: TGParser.cpp:3906
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::TGLexer
TGLexer - TableGen Lexer class.
Definition: TGLexer.h:81
llvm::RecordsEntry
RecordsEntry - Holds exactly one of a Record, ForeachLoop, or AssertionInfo.
Definition: TGParser.h:41
llvm::TGParser::Error
bool Error(SMLoc L, const Twine &Msg) const
Definition: TGParser.h:178
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::ForeachLoop
ForeachLoop - Record the iteration state associated with a for loop.
Definition: TGParser.h:62
llvm::ForeachLoop::Loc
SMLoc Loc
Definition: TGParser.h:63
llvm::SubMultiClassReference
Definition: TGParser.cpp:47
Error.h
llvm::RecordsEntry::RecordsEntry
RecordsEntry(std::unique_ptr< Record::AssertionInfo > Assertion)
Definition: TGParser.h:52
llvm::TGParser
Definition: TGParser.h:130
llvm::ForeachLoop::ListValue
Init * ListValue
Definition: TGParser.h:65
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::SubClassReference
Definition: TGParser.cpp:37
llvm::MultiClass
Definition: TGParser.h:120
llvm::TGLocalVarScope::TGLocalVarScope
TGLocalVarScope()=default
llvm::TGLocalVarScope::TGLocalVarScope
TGLocalVarScope(std::unique_ptr< TGLocalVarScope > parent)
Definition: TGParser.h:87
llvm::RecordsEntry::Loop
std::unique_ptr< ForeachLoop > Loop
Definition: TGParser.h:43
llvm::LetRecord::Name
StringInit * Name
Definition: TGParser.h:30
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
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:1861
llvm::TGLocalVarScope::getVar
Init * getVar(StringRef Name) const
Definition: TGParser.h:96
llvm::TGLocalVarScope
Definition: TGParser.h:80
Mode
SI Whole Quad Mode
Definition: SIWholeQuadMode.cpp:264
llvm::Record
Definition: Record.h:1573
llvm::RecordsEntry::Rec
std::unique_ptr< Record > Rec
Definition: TGParser.h:42
llvm::StringInit
"foo" - Represent an initialization by a string value.
Definition: Record.h:638
llvm::ForeachLoop::Entries
std::vector< RecordsEntry > Entries
Definition: TGParser.h:66
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:99
llvm::ArrayRef< unsigned >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::SourceMgr
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
llvm::LetRecord
Definition: TGParser.h:29
llvm::Init
Definition: Record.h:281
llvm::AMDGPU::SendMsg::Msg
const CustomOperand< const MCSubtargetInfo & > Msg[]
Definition: AMDGPUAsmUtils.cpp:39
llvm::LetRecord::LetRecord
LetRecord(StringInit *N, ArrayRef< unsigned > B, Init *V, SMLoc L)
Definition: TGParser.h:34
llvm::RecordsEntry::Assertion
std::unique_ptr< Record::AssertionInfo > Assertion
Definition: TGParser.h:44
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::TGLocalVarScope::addVar
void addVar(StringRef Name, Init *I)
Definition: TGParser.h:113
std
Definition: BitVector.h:851
llvm::TGParser::TGParser
TGParser(SourceMgr &SM, ArrayRef< std::string > Macros, RecordKeeper &records, const bool NoWarnOnUnusedTemplateArgs=false, const bool TrackReferenceLocs=false)
Definition: TGParser.h:167
llvm::ForeachLoop::ForeachLoop
ForeachLoop(SMLoc Loc, VarInit *IVar, Init *LValue)
Definition: TGParser.h:70
llvm::TGParser::getDependencies
const TGLexer::DependenciesSetTy & getDependencies() const
Definition: TGParser.h:185
llvm::ForeachLoop::IterVar
VarInit * IterVar
Definition: TGParser.h:64
llvm::LetRecord::Value
Init * Value
Definition: TGParser.h:32
llvm::MultiClass::Entries
std::vector< RecordsEntry > Entries
Definition: TGParser.h:122
llvm::DefsetRecord::Loc
SMLoc Loc
Definition: TGParser.h:75
llvm::PrintError
void PrintError(const Twine &Msg)
Definition: Error.cpp:101
llvm::DefsetRecord
Definition: TGParser.h:74
llvm::SMRange
Represents a range in source code.
Definition: SMLoc.h:48
N
#define N
llvm::tgtok::MultiClass
@ MultiClass
Definition: TGLexer.h:51
llvm::tgtok::TokKind
TokKind
Definition: TGLexer.h:32
llvm::RecordsEntry::RecordsEntry
RecordsEntry()=default
llvm::LetRecord::Bits
std::vector< unsigned > Bits
Definition: TGParser.h:31
llvm::MipsISD::Ins
@ Ins
Definition: MipsISelLowering.h:160
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
llvm::TGLexer::DependenciesSetTy
std::set< std::string > DependenciesSetTy
Definition: TGLexer.h:98
llvm::TGLexer::getLoc
SMLoc getLoc() const
Definition: TGLexer.cpp:62
llvm::RecordVal
This class represents a field in a record, including its name, type, value, and source location.
Definition: Record.h:1485
Record.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::TGParser::PopLocalScope
void PopLocalScope(TGLocalVarScope *ExpectedStackTop)
Definition: TGParser.h:196
llvm::DefsetRecord::EltTy
RecTy * EltTy
Definition: TGParser.h:76
llvm::TGParser::ParseFile
bool ParseFile()
ParseFile - Main entrypoint for parsing a tblgen file.
Definition: TGParser.cpp:3842
llvm::TypedInit
This is the common superclass of types that have a specific, explicit type, stored in ValueTy.
Definition: Record.h:421