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