1 //===- TGParser.h - Parser for TableGen Files -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class represents the Parser for tablegen files.
12 //===----------------------------------------------------------------------===//
17 #include "llvm/TableGen/Record.h"
19 #include "llvm/TableGen/Error.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/SourceMgr.h"
31 struct SubClassReference;
32 struct SubMultiClassReference;
36 std::vector<unsigned> Bits;
39 LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
41 : Name(N), Bits(B), Value(V), Loc(L) {
47 std::vector<std::vector<LetRecord> > LetStack;
48 std::map<std::string, MultiClass*> MultiClasses;
50 /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
52 MultiClass *CurMultiClass;
55 RecordKeeper &Records;
57 // A "named boolean" indicating how to parse identifiers. Usually
58 // identifiers map to some existing object but in special cases
59 // (e.g. parsing def names) no such object exists yet because we are
60 // in the middle of creating in. For those situations, allow the
61 // parser to ignore missing object errors.
63 ParseValueMode, // We are parsing a value we expect to look up.
64 ParseNameMode // We are parsing a name of an object that does not yet exist.
68 TGParser(SourceMgr &SrcMgr, RecordKeeper &records) :
69 Lex(SrcMgr), CurMultiClass(0), Records(records) {}
71 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
72 /// routines return true on error, or false on success.
75 bool Error(SMLoc L, const Twine &Msg) const {
79 bool TokError(const Twine &Msg) const {
80 return Error(Lex.getLoc(), Msg);
82 const std::vector<std::string> &getDependencies() const {
83 return Lex.getDependencies();
85 private: // Semantic analysis methods.
86 bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
87 bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
88 const std::vector<unsigned> &BitList, Init *V);
89 bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
90 const std::vector<unsigned> &BitList, Init *V) {
91 return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V);
93 bool AddSubClass(Record *Rec, SubClassReference &SubClass);
94 bool AddSubMultiClass(MultiClass *CurMC,
95 SubMultiClassReference &SubMultiClass);
97 private: // Parser methods.
98 bool ParseObjectList(MultiClass *MC = 0);
99 bool ParseObject(MultiClass *MC);
101 bool ParseMultiClass();
102 Record *InstantiateMulticlassDef(MultiClass &MC,
105 SMLoc DefmPrefixLoc);
106 bool ResolveMulticlassDefArgs(MultiClass &MC,
110 const std::vector<Init *> &TArgs,
111 std::vector<Init *> &TemplateVals,
113 bool ResolveMulticlassDef(MultiClass &MC,
116 SMLoc DefmPrefixLoc);
117 bool ParseDefm(MultiClass *CurMultiClass);
118 bool ParseDef(MultiClass *CurMultiClass);
119 bool ParseTopLevelLet(MultiClass *CurMultiClass);
120 std::vector<LetRecord> ParseLetList();
122 bool ParseObjectBody(Record *CurRec);
123 bool ParseBody(Record *CurRec);
124 bool ParseBodyItem(Record *CurRec);
126 bool ParseTemplateArgList(Record *CurRec);
127 Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
129 SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
130 SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
132 Init *ParseIDValue(Record *CurRec, IDParseMode Mode = ParseValueMode);
133 Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc,
134 IDParseMode Mode = ParseValueMode);
135 Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0,
136 IDParseMode Mode = ParseValueMode);
137 Init *ParseValue(Record *CurRec, RecTy *ItemType = 0,
138 IDParseMode Mode = ParseValueMode);
139 std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0, RecTy *EltTy = 0);
140 std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
141 bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
142 bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
143 std::vector<unsigned> ParseRangeList();
144 bool ParseRangePiece(std::vector<unsigned> &Ranges);
146 Init *ParseOperation(Record *CurRec);
147 RecTy *ParseOperatorType();
148 Init *ParseObjectName(MultiClass *CurMultiClass);
149 Record *ParseClassID();
150 MultiClass *ParseMultiClassID();
151 Record *ParseDefmID();
154 } // end namespace llvm