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