LLVM  3.7.0
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 MultiClass;
31  struct SubClassReference;
32  struct SubMultiClassReference;
33 
34  struct LetRecord {
35  std::string Name;
36  std::vector<unsigned> Bits;
39  LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
40  SMLoc L)
41  : Name(N), Bits(B), Value(V), Loc(L) {
42  }
43  };
44 
45  /// ForeachLoop - Record the iteration state associated with a for loop.
46  /// This is used to instantiate items in the loop body.
47  struct ForeachLoop {
50 
51  ForeachLoop(VarInit *IVar, ListInit *LValue)
52  : IterVar(IVar), ListValue(LValue) {}
53  };
54 
55 class TGParser {
56  TGLexer Lex;
57  std::vector<std::vector<LetRecord> > LetStack;
58  std::map<std::string, std::unique_ptr<MultiClass>> MultiClasses;
59 
60  /// Loops - Keep track of any foreach loops we are within.
61  ///
62  typedef std::vector<ForeachLoop> LoopVector;
63  LoopVector Loops;
64 
65  /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
66  /// current value.
67  MultiClass *CurMultiClass;
68 
69  // Record tracker
70  RecordKeeper &Records;
71 
72  unsigned AnonCounter;
73 
74  // A "named boolean" indicating how to parse identifiers. Usually
75  // identifiers map to some existing object but in special cases
76  // (e.g. parsing def names) no such object exists yet because we are
77  // in the middle of creating in. For those situations, allow the
78  // parser to ignore missing object errors.
79  enum IDParseMode {
80  ParseValueMode, // We are parsing a value we expect to look up.
81  ParseNameMode, // We are parsing a name of an object that does not yet
82  // exist.
83  ParseForeachMode // We are parsing a foreach init.
84  };
85 
86 public:
88  : Lex(SrcMgr), CurMultiClass(nullptr), Records(records), AnonCounter(0) {}
89 
90  /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
91  /// routines return true on error, or false on success.
92  bool ParseFile();
93 
94  bool Error(SMLoc L, const Twine &Msg) const {
95  PrintError(L, Msg);
96  return true;
97  }
98  bool TokError(const Twine &Msg) const {
99  return Error(Lex.getLoc(), Msg);
100  }
102  return Lex.getDependencies();
103  }
104 
105 private: // Semantic analysis methods.
106  bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
107  bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
108  const std::vector<unsigned> &BitList, Init *V);
109  bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
110  const std::vector<unsigned> &BitList, Init *V) {
111  return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V);
112  }
113  bool AddSubClass(Record *Rec, SubClassReference &SubClass);
114  bool AddSubMultiClass(MultiClass *CurMC,
115  SubMultiClassReference &SubMultiClass);
116 
117  std::string GetNewAnonymousName();
118 
119  // IterRecord: Map an iterator name to a value.
120  struct IterRecord {
121  VarInit *IterVar;
122  Init *IterValue;
123  IterRecord(VarInit *Var, Init *Val) : IterVar(Var), IterValue(Val) {}
124  };
125 
126  // IterSet: The set of all iterator values at some point in the
127  // iteration space.
128  typedef std::vector<IterRecord> IterSet;
129 
130  bool ProcessForeachDefs(Record *CurRec, SMLoc Loc);
131  bool ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals);
132 
133 private: // Parser methods.
134  bool ParseObjectList(MultiClass *MC = nullptr);
135  bool ParseObject(MultiClass *MC);
136  bool ParseClass();
137  bool ParseMultiClass();
138  Record *InstantiateMulticlassDef(MultiClass &MC,
139  Record *DefProto,
140  Init *&DefmPrefix,
141  SMRange DefmPrefixRange,
142  const std::vector<Init *> &TArgs,
143  std::vector<Init *> &TemplateVals);
144  bool ResolveMulticlassDefArgs(MultiClass &MC,
145  Record *DefProto,
146  SMLoc DefmPrefixLoc,
147  SMLoc SubClassLoc,
148  const std::vector<Init *> &TArgs,
149  std::vector<Init *> &TemplateVals,
150  bool DeleteArgs);
151  bool ResolveMulticlassDef(MultiClass &MC,
152  Record *CurRec,
153  Record *DefProto,
154  SMLoc DefmPrefixLoc);
155  bool ParseDefm(MultiClass *CurMultiClass);
156  bool ParseDef(MultiClass *CurMultiClass);
157  bool ParseForeach(MultiClass *CurMultiClass);
158  bool ParseTopLevelLet(MultiClass *CurMultiClass);
159  std::vector<LetRecord> ParseLetList();
160 
161  bool ParseObjectBody(Record *CurRec);
162  bool ParseBody(Record *CurRec);
163  bool ParseBodyItem(Record *CurRec);
164 
165  bool ParseTemplateArgList(Record *CurRec);
166  Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
167  VarInit *ParseForeachDeclaration(ListInit *&ForeachListValue);
168 
169  SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
170  SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
171 
172  Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc,
173  IDParseMode Mode = ParseValueMode);
174  Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = nullptr,
175  IDParseMode Mode = ParseValueMode);
176  Init *ParseValue(Record *CurRec, RecTy *ItemType = nullptr,
177  IDParseMode Mode = ParseValueMode);
178  std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = nullptr,
179  RecTy *EltTy = nullptr);
180  std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
181  bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
182  bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
183  std::vector<unsigned> ParseRangeList();
184  bool ParseRangePiece(std::vector<unsigned> &Ranges);
185  RecTy *ParseType();
186  Init *ParseOperation(Record *CurRec, RecTy *ItemType);
187  RecTy *ParseOperatorType();
188  Init *ParseObjectName(MultiClass *CurMultiClass);
189  Record *ParseClassID();
190  MultiClass *ParseMultiClassID();
191  bool ApplyLetStack(Record *CurRec);
192 };
193 
194 } // end namespace llvm
195 
196 #endif
VarInit * IterVar
Definition: TGParser.h:48
ListInit - [AL, AH, CL] - Represent a list of defs.
Definition: Record.h:584
SourceMgr SrcMgr
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
const DependenciesMapTy & getDependencies() const
Definition: TGLexer.h:95
ForeachLoop(VarInit *IVar, ListInit *LValue)
Definition: TGParser.h:51
const TGLexer::DependenciesMapTy & getDependencies() const
Definition: TGParser.h:101
LetRecord(const std::string &N, const std::vector< unsigned > &B, Init *V, SMLoc L)
Definition: TGParser.h:39
std::string Name
Definition: TGParser.h:35
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:35
std::vector< unsigned > Bits
Definition: TGParser.h:36
ForeachLoop - Record the iteration state associated with a for loop.
Definition: TGParser.h:47
bool Error(SMLoc L, const Twine &Msg) const
Definition: TGParser.h:94
static StringInit * get(StringRef)
Definition: Record.cpp:453
ListInit * ListValue
Definition: TGParser.h:49
SI Fix SGPR Live Ranges
std::map< std::string, SMLoc > DependenciesMapTy
Definition: TGLexer.h:83
#define N
VarInit - 'Opcode' - Represent a reference to an entire variable object.
Definition: Record.h:836
bool ParseFile()
ParseFile - Main entrypoint for parsing a tblgen file.
TGParser(SourceMgr &SrcMgr, RecordKeeper &records)
Definition: TGParser.h:87
LLVM Value Representation.
Definition: Value.h:69
Init * Value
Definition: TGParser.h:37
Represents a location in source code.
Definition: SMLoc.h:23
bool TokError(const Twine &Msg) const
Definition: TGParser.h:98
SMLoc getLoc() const
Definition: TGLexer.cpp:36
void PrintError(ArrayRef< SMLoc > ErrorLoc, const Twine &Msg)
TGLexer - TableGen Lexer class.
Definition: TGLexer.h:66