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 
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 ParseTopLevelLet(MultiClass *CurMultiClass);
226  void ParseLetList(SmallVectorImpl<LetRecord> &Result);
227 
228  bool ParseObjectBody(Record *CurRec);
229  bool ParseBody(Record *CurRec);
230  bool ParseBodyItem(Record *CurRec);
231 
232  bool ParseTemplateArgList(Record *CurRec);
233  Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
234  VarInit *ParseForeachDeclaration(Init *&ForeachListValue);
235 
236  SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
237  SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
238 
239  Init *ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
240  IDParseMode Mode = ParseValueMode);
241  Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = nullptr,
242  IDParseMode Mode = ParseValueMode);
243  Init *ParseValue(Record *CurRec, RecTy *ItemType = nullptr,
244  IDParseMode Mode = ParseValueMode);
245  void ParseValueList(SmallVectorImpl<llvm::Init*> &Result, Record *CurRec,
246  Record *ArgsRec = nullptr, RecTy *EltTy = nullptr);
247  void ParseDagArgList(
248  SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
249  Record *CurRec);
250  bool ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges);
251  bool ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges);
252  void ParseRangeList(SmallVectorImpl<unsigned> &Result);
253  bool ParseRangePiece(SmallVectorImpl<unsigned> &Ranges,
254  TypedInit *FirstItem = nullptr);
255  RecTy *ParseType();
256  Init *ParseOperation(Record *CurRec, RecTy *ItemType);
257  Init *ParseOperationCond(Record *CurRec, RecTy *ItemType);
258  RecTy *ParseOperatorType();
259  Init *ParseObjectName(MultiClass *CurMultiClass);
260  Record *ParseClassID();
261  MultiClass *ParseMultiClassID();
262  bool ApplyLetStack(Record *CurRec);
263  bool ApplyLetStack(RecordsEntry &Entry);
264 };
265 
266 } // end namespace llvm
267 
268 #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
static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF)
Definition: Execution.cpp:41
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
Hexagon Hardware Loops
Definition: BitVector.h:959
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 super-class of types that have a specific, explicit, type.
Definition: Record.h:425
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")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
* 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:594
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:104
Init * ListValue
Definition: TGParser.h:61
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:91
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 &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:883
bool TokError(const Twine &Msg) const
Definition: TGParser.h:172
void addVar(StringRef Name, Init *I)
Definition: TGParser.h:109
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:516
SmallVector< Init *, 16 > Elements
Definition: TGParser.h:73
#define I(x, y, z)
Definition: MD5.cpp:59
#define N
&#39;Opcode&#39; - Represent a reference to an entire variable object.
Definition: Record.h:1072
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
std::vector< RecordsEntry > Entries
Definition: TGParser.h:62
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
Init * Value
Definition: TGParser.h:32
Represents a location in source code.
Definition: SMLoc.h:23
void PopLocalScope(TGLocalVarScope *ExpectedStackTop)
Definition: TGParser.h:186
void PrintError(ArrayRef< SMLoc > ErrorLoc, const Twine &Msg)
Definition: Error.cpp:58
TGLexer - TableGen Lexer class.
Definition: TGLexer.h:74