blob: 509b02c0782b36dbeb23a2aa0e312497fccc3be2 [file] [log] [blame]
Chris Lattnerf4601652007-11-22 20:49:04 +00001//===- TGParser.h - Parser for TableGen Files -------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4601652007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This class represents the Parser for tablegen files.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef TGPARSER_H
15#define TGPARSER_H
16
David Greene917924d2011-10-19 13:02:39 +000017#include "llvm/TableGen/Record.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000018#include "TGLexer.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000019#include "llvm/TableGen/Error.h"
Benjamin Kramerd1e17032010-09-27 17:42:11 +000020#include "llvm/ADT/Twine.h"
Chris Lattner099e1982009-06-21 03:36:54 +000021#include "llvm/Support/SourceMgr.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000022#include <map>
23
24namespace llvm {
25 class Record;
26 class RecordVal;
Chris Lattner67db8832010-12-13 00:23:57 +000027 class RecordKeeper;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +000028 class RecTy;
David Greeneafd54262011-07-13 22:25:51 +000029 class Init;
Chris Lattnerf4601652007-11-22 20:49:04 +000030 struct MultiClass;
Sebastian Redl48fe6352009-03-19 23:26:52 +000031 struct SubClassReference;
David Greenede444af2009-04-22 16:42:54 +000032 struct SubMultiClassReference;
Chris Lattnerf4601652007-11-22 20:49:04 +000033
34 struct LetRecord {
35 std::string Name;
36 std::vector<unsigned> Bits;
David Greene05bce0b2011-07-29 22:43:06 +000037 Init *Value;
Chris Lattner1e3a8a42009-06-21 03:39:35 +000038 SMLoc Loc;
David Greene05bce0b2011-07-29 22:43:06 +000039 LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
Eric Christopherd568b3f2011-07-11 23:06:52 +000040 SMLoc L)
Chris Lattnerf4601652007-11-22 20:49:04 +000041 : Name(N), Bits(B), Value(V), Loc(L) {
42 }
43 };
44
David Greenecebb4ee2012-02-22 16:09:41 +000045 /// ForeachLoop - Record the iteration state associated with a for loop.
46 /// This is used to instantiate items in the loop body.
47 struct ForeachLoop {
48 Init *IterVar;
49 Init *ListValue;
50
51 ForeachLoop(Init *IVar, Init *LValue) : IterVar(IVar), ListValue(LValue) {};
52 };
53
Chris Lattnerf4601652007-11-22 20:49:04 +000054class TGParser {
55 TGLexer Lex;
56 std::vector<std::vector<LetRecord> > LetStack;
57 std::map<std::string, MultiClass*> MultiClasses;
58
David Greenecebb4ee2012-02-22 16:09:41 +000059 /// Loops - Keep track of any foreach loops we are within.
60 ///
61 typedef std::vector<ForeachLoop> LoopVector;
62 LoopVector Loops;
63
Chris Lattnerf4601652007-11-22 20:49:04 +000064 /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
65 /// current value.
66 MultiClass *CurMultiClass;
Chris Lattner67db8832010-12-13 00:23:57 +000067
68 // Record tracker
Chris Lattner9c6b60e2010-12-15 04:48:22 +000069 RecordKeeper &Records;
David Greenef3744a02011-10-19 13:04:20 +000070
71 // A "named boolean" indicating how to parse identifiers. Usually
72 // identifiers map to some existing object but in special cases
73 // (e.g. parsing def names) no such object exists yet because we are
74 // in the middle of creating in. For those situations, allow the
75 // parser to ignore missing object errors.
76 enum IDParseMode {
David Greenecebb4ee2012-02-22 16:09:41 +000077 ParseValueMode, // We are parsing a value we expect to look up.
78 ParseNameMode, // We are parsing a name of an object that does not yet
79 // exist.
80 ParseForeachMode // We are parsing a foreach init.
David Greenef3744a02011-10-19 13:04:20 +000081 };
82
Chris Lattnerf4601652007-11-22 20:49:04 +000083public:
Chris Lattner9c6b60e2010-12-15 04:48:22 +000084 TGParser(SourceMgr &SrcMgr, RecordKeeper &records) :
Chris Lattner67db8832010-12-13 00:23:57 +000085 Lex(SrcMgr), CurMultiClass(0), Records(records) {}
Chris Lattnerf4601652007-11-22 20:49:04 +000086
Chris Lattnerf4601652007-11-22 20:49:04 +000087 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
88 /// routines return true on error, or false on success.
89 bool ParseFile();
90
Benjamin Kramerd1e17032010-09-27 17:42:11 +000091 bool Error(SMLoc L, const Twine &Msg) const {
Jim Grosbach0b6a44a2011-06-21 22:55:50 +000092 PrintError(L, Msg);
Chris Lattnerf4601652007-11-22 20:49:04 +000093 return true;
94 }
Benjamin Kramerd1e17032010-09-27 17:42:11 +000095 bool TokError(const Twine &Msg) const {
Chris Lattnerf4601652007-11-22 20:49:04 +000096 return Error(Lex.getLoc(), Msg);
97 }
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +000098 const std::vector<std::string> &getDependencies() const {
99 return Lex.getDependencies();
100 }
David Greenecebb4ee2012-02-22 16:09:41 +0000101
Chris Lattnerf4601652007-11-22 20:49:04 +0000102private: // Semantic analysis methods.
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000103 bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
David Greene917924d2011-10-19 13:02:39 +0000104 bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
David Greene05bce0b2011-07-29 22:43:06 +0000105 const std::vector<unsigned> &BitList, Init *V);
David Greene917924d2011-10-19 13:02:39 +0000106 bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
107 const std::vector<unsigned> &BitList, Init *V) {
108 return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V);
109 }
Cedric Venetaff9c272009-02-14 16:06:42 +0000110 bool AddSubClass(Record *Rec, SubClassReference &SubClass);
Bob Wilson440548d2009-04-30 18:26:19 +0000111 bool AddSubMultiClass(MultiClass *CurMC,
112 SubMultiClassReference &SubMultiClass);
Chris Lattnerf4601652007-11-22 20:49:04 +0000113
David Greenecebb4ee2012-02-22 16:09:41 +0000114 // IterRecord: Map an iterator name to a value.
115 struct IterRecord {
116 Init *IterVar;
117 Init *IterValue;
118 IterRecord(Init *Var, Init *Val) : IterVar(Var), IterValue(Val) {}
119 };
120
121 // IterSet: The set of all iterator values at some point in the
122 // iteration space.
123 typedef std::vector<IterRecord> IterSet;
124
125 bool ProcessForeachDefs(Record *CurRec, MultiClass *CurMultiClass,
126 SMLoc Loc);
127 bool ProcessForeachDefs(Record *CurRec, MultiClass *CurMultiClass,
128 SMLoc Loc, IterSet &IterVals, ForeachLoop &CurLoop,
129 LoopVector::iterator NextLoop);
130
Chris Lattnerf4601652007-11-22 20:49:04 +0000131private: // Parser methods.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +0000132 bool ParseObjectList(MultiClass *MC = 0);
133 bool ParseObject(MultiClass *MC);
Chris Lattnerf4601652007-11-22 20:49:04 +0000134 bool ParseClass();
135 bool ParseMultiClass();
David Greenee499a2d2011-10-05 22:42:07 +0000136 Record *InstantiateMulticlassDef(MultiClass &MC,
137 Record *DefProto,
David Greene7be867e2011-10-19 13:04:31 +0000138 Init *DefmPrefix,
David Greenee499a2d2011-10-05 22:42:07 +0000139 SMLoc DefmPrefixLoc);
140 bool ResolveMulticlassDefArgs(MultiClass &MC,
141 Record *DefProto,
142 SMLoc DefmPrefixLoc,
143 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +0000144 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +0000145 std::vector<Init *> &TemplateVals,
146 bool DeleteArgs);
147 bool ResolveMulticlassDef(MultiClass &MC,
148 Record *CurRec,
149 Record *DefProto,
150 SMLoc DefmPrefixLoc);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +0000151 bool ParseDefm(MultiClass *CurMultiClass);
152 bool ParseDef(MultiClass *CurMultiClass);
David Greenecebb4ee2012-02-22 16:09:41 +0000153 bool ParseForeach(MultiClass *CurMultiClass);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +0000154 bool ParseTopLevelLet(MultiClass *CurMultiClass);
Chris Lattnerf4601652007-11-22 20:49:04 +0000155 std::vector<LetRecord> ParseLetList();
156
Chris Lattnerf4601652007-11-22 20:49:04 +0000157 bool ParseObjectBody(Record *CurRec);
158 bool ParseBody(Record *CurRec);
159 bool ParseBodyItem(Record *CurRec);
160
161 bool ParseTemplateArgList(Record *CurRec);
David Greenee22b3212011-10-19 13:02:42 +0000162 Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
David Greenecebb4ee2012-02-22 16:09:41 +0000163 Init *ParseForeachDeclaration(Init *&ForeachListValue);
Chris Lattnerf4601652007-11-22 20:49:04 +0000164
165 SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
Bob Wilson440548d2009-04-30 18:26:19 +0000166 SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
Chris Lattnerf4601652007-11-22 20:49:04 +0000167
David Greenef3744a02011-10-19 13:04:20 +0000168 Init *ParseIDValue(Record *CurRec, IDParseMode Mode = ParseValueMode);
169 Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc,
170 IDParseMode Mode = ParseValueMode);
171 Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0,
172 IDParseMode Mode = ParseValueMode);
173 Init *ParseValue(Record *CurRec, RecTy *ItemType = 0,
174 IDParseMode Mode = ParseValueMode);
David Greene05bce0b2011-07-29 22:43:06 +0000175 std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0, RecTy *EltTy = 0);
176 std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
Chris Lattnerf4601652007-11-22 20:49:04 +0000177 bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
178 bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
179 std::vector<unsigned> ParseRangeList();
180 bool ParseRangePiece(std::vector<unsigned> &Ranges);
181 RecTy *ParseType();
David Greene05bce0b2011-07-29 22:43:06 +0000182 Init *ParseOperation(Record *CurRec);
David Greened418c1b2009-05-14 20:54:48 +0000183 RecTy *ParseOperatorType();
David Greenea9e07dd2011-10-19 13:04:29 +0000184 Init *ParseObjectName(MultiClass *CurMultiClass);
Chris Lattnerf4601652007-11-22 20:49:04 +0000185 Record *ParseClassID();
David Greenede444af2009-04-22 16:42:54 +0000186 MultiClass *ParseMultiClassID();
Chris Lattnerf4601652007-11-22 20:49:04 +0000187 Record *ParseDefmID();
188};
189
190} // end namespace llvm
191
192#endif