• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
36     StringInit *Name;
37     std::vector<unsigned> Bits;
38     Init *Value;
39     SMLoc Loc;
LetRecordLetRecord40     LetRecord(StringInit *N, ArrayRef<unsigned> B, Init *V, SMLoc L)
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 
RecordsEntryRecordsEntry52     RecordsEntry() {}
RecordsEntryRecordsEntry53     RecordsEntry(std::unique_ptr<Record> Rec) : Rec(std::move(Rec)) {}
RecordsEntryRecordsEntry54     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 {
61     SMLoc Loc;
62     VarInit *IterVar;
63     Init *ListValue;
64     std::vector<RecordsEntry> Entries;
65 
66     void dump() const;
67 
ForeachLoopForeachLoop68     ForeachLoop(SMLoc Loc, VarInit *IVar, Init *LValue)
69       : Loc(Loc), IterVar(IVar), ListValue(LValue) {}
70   };
71 
72   struct DefsetRecord {
73     SMLoc Loc;
74     RecTy *EltTy;
75     SmallVector<Init *, 16> Elements;
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 
MultiClassMultiClass84   MultiClass(StringRef Name, SMLoc Loc, RecordKeeper &Records) :
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 
97   SmallVector<DefsetRecord *, 2> Defsets;
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:
TGParser(SourceMgr & SrcMgr,RecordKeeper & records)118   TGParser(SourceMgr &SrcMgr, RecordKeeper &records)
119       : Lex(SrcMgr), 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 
Error(SMLoc L,const Twine & Msg)125   bool Error(SMLoc L, const Twine &Msg) const {
126     PrintError(L, Msg);
127     return true;
128   }
TokError(const Twine & Msg)129   bool TokError(const Twine &Msg) const {
130     return Error(Lex.getLoc(), Msg);
131   }
getDependencies()132   const TGLexer::DependenciesMapTy &getDependencies() const {
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 
146   using SubstStack = SmallVector<std::pair<Init *, Init *>, 8>;
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   RecTy *ParseType();
195   Init *ParseOperation(Record *CurRec, RecTy *ItemType);
196   RecTy *ParseOperatorType();
197   Init *ParseObjectName(MultiClass *CurMultiClass);
198   Record *ParseClassID();
199   MultiClass *ParseMultiClassID();
200   bool ApplyLetStack(Record *CurRec);
201   bool ApplyLetStack(RecordsEntry &Entry);
202 };
203 
204 } // end namespace llvm
205 
206 #endif
207