LLVM  10.0.0svn
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/ADT/Twine.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include "llvm/TableGen/Error.h"
20 #include "llvm/TableGen/Record.h"
21 #include <map>
22 
23 namespace llvm {
24  class Record;
25  class RecordVal;
26  class RecordKeeper;
27  class RecTy;
28  class Init;
29  struct ForeachLoop;
30  struct MultiClass;
31  struct SubClassReference;
32  struct SubMultiClassReference;
33 
34  struct LetRecord {
36  std::vector<unsigned> Bits;
40  : Name(N), Bits(B), Value(V), Loc(L) {
41  }
42  };
43 
44  /// RecordsEntry - Can be either a record or a foreach loop.
45  struct RecordsEntry {
46  std::unique_ptr<Record> Rec;
47  std::unique_ptr<ForeachLoop> Loop;
48 
49  void dump() const;
50 
52  RecordsEntry(std::unique_ptr<Record> Rec) : Rec(std::move(Rec)) {}
53  RecordsEntry(std::unique_ptr<ForeachLoop> Loop)
54  : Loop(std::move(Loop)) {}
55  };
56 
57  /// ForeachLoop - Record the iteration state associated with a for loop.
58  /// This is used to instantiate items in the loop body.
59  struct ForeachLoop {
63  std::vector<RecordsEntry> Entries;
64 
65  void dump() const;
66 
67  ForeachLoop(SMLoc Loc, VarInit *IVar, Init *LValue)
68  : Loc(Loc), IterVar(IVar), ListValue(LValue) {}
69  };
70 
71  struct DefsetRecord {
75  };
76 
77 struct MultiClass {
78  Record Rec; // Placeholder for template args and Name.
79  std::vector<RecordsEntry> Entries;
80 
81  void dump() const;
82 
84  Rec(Name, Loc, Records) {}
85 };
86 
87 class TGParser {
88  TGLexer Lex;
89  std::vector<SmallVector<LetRecord, 4>> LetStack;
90  std::map<std::string, std::unique_ptr<MultiClass>> MultiClasses;
91 
92  /// Loops - Keep track of any foreach loops we are within.
93  ///
94  std::vector<std::unique_ptr<ForeachLoop>> Loops;
95 
97 
98  /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
99  /// current value.
100  MultiClass *CurMultiClass;
101 
102  // Record tracker
103  RecordKeeper &Records;
104 
105  // A "named boolean" indicating how to parse identifiers. Usually
106  // identifiers map to some existing object but in special cases
107  // (e.g. parsing def names) no such object exists yet because we are
108  // in the middle of creating in. For those situations, allow the
109  // parser to ignore missing object errors.
110  enum IDParseMode {
111  ParseValueMode, // We are parsing a value we expect to look up.
112  ParseNameMode, // We are parsing a name of an object that does not yet
113  // exist.
114  };
115 
116 public:
118  RecordKeeper &records)
119  : Lex(SrcMgr, Macros), CurMultiClass(nullptr), Records(records) {}
120 
121  /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
122  /// routines return true on error, or false on success.
123  bool ParseFile();
124 
125  bool Error(SMLoc L, const Twine &Msg) const {
126  PrintError(L, Msg);
127  return true;
128  }
129  bool TokError(const Twine &Msg) const {
130  return Error(Lex.getLoc(), Msg);
131  }
133  return Lex.getDependencies();
134  }
135 
136 private: // Semantic analysis methods.
137  bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
138  bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
139  ArrayRef<unsigned> BitList, Init *V,
140  bool AllowSelfAssignment = false);
141  bool AddSubClass(Record *Rec, SubClassReference &SubClass);
142  bool AddSubClass(RecordsEntry &Entry, SubClassReference &SubClass);
143  bool AddSubMultiClass(MultiClass *CurMC,
144  SubMultiClassReference &SubMultiClass);
145 
147 
148  bool addEntry(RecordsEntry E);
149  bool resolve(const ForeachLoop &Loop, SubstStack &Stack, bool Final,
150  std::vector<RecordsEntry> *Dest, SMLoc *Loc = nullptr);
151  bool resolve(const std::vector<RecordsEntry> &Source, SubstStack &Substs,
152  bool Final, std::vector<RecordsEntry> *Dest,
153  SMLoc *Loc = nullptr);
154  bool addDefOne(std::unique_ptr<Record> Rec);
155 
156 private: // Parser methods.
157  bool ParseObjectList(MultiClass *MC = nullptr);
158  bool ParseObject(MultiClass *MC);
159  bool ParseClass();
160  bool ParseMultiClass();
161  bool ParseDefm(MultiClass *CurMultiClass);
162  bool ParseDef(MultiClass *CurMultiClass);
163  bool ParseDefset();
164  bool ParseForeach(MultiClass *CurMultiClass);
165  bool ParseTopLevelLet(MultiClass *CurMultiClass);
166  void ParseLetList(SmallVectorImpl<LetRecord> &Result);
167 
168  bool ParseObjectBody(Record *CurRec);
169  bool ParseBody(Record *CurRec);
170  bool ParseBodyItem(Record *CurRec);
171 
172  bool ParseTemplateArgList(Record *CurRec);
173  Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
174  VarInit *ParseForeachDeclaration(Init *&ForeachListValue);
175 
176  SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
177  SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
178 
179  Init *ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
180  IDParseMode Mode = ParseValueMode);
181  Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = nullptr,
182  IDParseMode Mode = ParseValueMode);
183  Init *ParseValue(Record *CurRec, RecTy *ItemType = nullptr,
184  IDParseMode Mode = ParseValueMode);
185  void ParseValueList(SmallVectorImpl<llvm::Init*> &Result, Record *CurRec,
186  Record *ArgsRec = nullptr, RecTy *EltTy = nullptr);
187  void ParseDagArgList(
188  SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
189  Record *CurRec);
190  bool ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges);
191  bool ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges);
192  void ParseRangeList(SmallVectorImpl<unsigned> &Result);
193  bool ParseRangePiece(SmallVectorImpl<unsigned> &Ranges,
194  TypedInit *FirstItem = nullptr);
195  RecTy *ParseType();
196  Init *ParseOperation(Record *CurRec, RecTy *ItemType);
197  Init *ParseOperationCond(Record *CurRec, RecTy *ItemType);
198  RecTy *ParseOperatorType();
199  Init *ParseObjectName(MultiClass *CurMultiClass);
200  Record *ParseClassID();
201  MultiClass *ParseMultiClassID();
202  bool ApplyLetStack(Record *CurRec);
203  bool ApplyLetStack(RecordsEntry &Entry);
204 };
205 
206 } // end namespace llvm
207 
208 #endif
SMLoc getLoc() const
Definition: TGLexer.cpp:63
SI Whole Quad Mode
This class represents lattice values for constants.
Definition: AllocatorList.h:23
VarInit * IterVar
Definition: TGParser.h:61
static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF)
Definition: Execution.cpp:41
std::unique_ptr< ForeachLoop > Loop
Definition: TGParser.h:47
SourceMgr SrcMgr
Definition: Error.cpp:23
std::unique_ptr< Record > Rec
Definition: TGParser.h:46
RecordsEntry(std::unique_ptr< Record > Rec)
Definition: TGParser.h:52
C::iterator addEntry(C &Container, StringRef InstallName)
Hexagon Hardware Loops
Definition: BitVector.h:937
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
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:41
LetRecord(StringInit *N, ArrayRef< unsigned > B, Init *V, SMLoc L)
Definition: TGParser.h:39
ForeachLoop(SMLoc Loc, VarInit *IVar, Init *LValue)
Definition: TGParser.h:67
std::vector< RecordsEntry > Entries
Definition: TGParser.h:79
MultiClass(StringRef Name, SMLoc Loc, RecordKeeper &Records)
Definition: TGParser.h:83
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
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:45
StringInit * Name
Definition: TGParser.h:35
Init * ListValue
Definition: TGParser.h:62
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:41
std::vector< unsigned > Bits
Definition: TGParser.h:36
bool Error(SMLoc L, const Twine &Msg) const
Definition: TGParser.h:125
ForeachLoop - Record the iteration state associated with a for loop.
Definition: TGParser.h:59
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
const TGLexer::DependenciesMapTy & getDependencies() const
Definition: TGParser.h:132
bool TokError(const Twine &Msg) const
Definition: TGParser.h:129
std::map< std::string, SMLoc > DependenciesMapTy
Definition: TGLexer.h:90
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:509
SmallVector< Init *, 16 > Elements
Definition: TGParser.h:74
#define N
&#39;Opcode&#39; - Represent a reference to an entire variable object.
Definition: Record.h:1068
TGParser(SourceMgr &SrcMgr, ArrayRef< std::string > Macros, RecordKeeper &records)
Definition: TGParser.h:117
RecordsEntry(std::unique_ptr< ForeachLoop > Loop)
Definition: TGParser.h:53
const DependenciesMapTy & getDependencies() const
Definition: TGLexer.h:102
std::vector< RecordsEntry > Entries
Definition: TGParser.h:63
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Init * Value
Definition: TGParser.h:37
Represents a location in source code.
Definition: SMLoc.h:23
void PrintError(ArrayRef< SMLoc > ErrorLoc, const Twine &Msg)
Definition: Error.cpp:58
TGLexer - TableGen Lexer class.
Definition: TGLexer.h:73