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