blob: 3d2c72cf773a13bec274eb19334bbaf9e58a7416 [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 {
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +000048 VarInit *IterVar;
49 ListInit *ListValue;
David Greenecebb4ee2012-02-22 16:09:41 +000050
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +000051 ForeachLoop(VarInit *IVar, ListInit *LValue)
52 : IterVar(IVar), ListValue(LValue) {}
David Greenecebb4ee2012-02-22 16:09:41 +000053 };
54
Chris Lattnerf4601652007-11-22 20:49:04 +000055class TGParser {
56 TGLexer Lex;
57 std::vector<std::vector<LetRecord> > LetStack;
58 std::map<std::string, MultiClass*> MultiClasses;
59
David Greenecebb4ee2012-02-22 16:09:41 +000060 /// Loops - Keep track of any foreach loops we are within.
61 ///
62 typedef std::vector<ForeachLoop> LoopVector;
63 LoopVector Loops;
64
Chris Lattnerf4601652007-11-22 20:49:04 +000065 /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
66 /// current value.
67 MultiClass *CurMultiClass;
Chris Lattner67db8832010-12-13 00:23:57 +000068
69 // Record tracker
Chris Lattner9c6b60e2010-12-15 04:48:22 +000070 RecordKeeper &Records;
David Greenef3744a02011-10-19 13:04:20 +000071
72 // A "named boolean" indicating how to parse identifiers. Usually
73 // identifiers map to some existing object but in special cases
74 // (e.g. parsing def names) no such object exists yet because we are
75 // in the middle of creating in. For those situations, allow the
76 // parser to ignore missing object errors.
77 enum IDParseMode {
David Greenecebb4ee2012-02-22 16:09:41 +000078 ParseValueMode, // We are parsing a value we expect to look up.
79 ParseNameMode, // We are parsing a name of an object that does not yet
80 // exist.
81 ParseForeachMode // We are parsing a foreach init.
David Greenef3744a02011-10-19 13:04:20 +000082 };
83
Chris Lattnerf4601652007-11-22 20:49:04 +000084public:
Chris Lattner9c6b60e2010-12-15 04:48:22 +000085 TGParser(SourceMgr &SrcMgr, RecordKeeper &records) :
Chris Lattner67db8832010-12-13 00:23:57 +000086 Lex(SrcMgr), CurMultiClass(0), Records(records) {}
Chris Lattnerf4601652007-11-22 20:49:04 +000087
Chris Lattnerf4601652007-11-22 20:49:04 +000088 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
89 /// routines return true on error, or false on success.
90 bool ParseFile();
91
Benjamin Kramerd1e17032010-09-27 17:42:11 +000092 bool Error(SMLoc L, const Twine &Msg) const {
Jim Grosbach0b6a44a2011-06-21 22:55:50 +000093 PrintError(L, Msg);
Chris Lattnerf4601652007-11-22 20:49:04 +000094 return true;
95 }
Benjamin Kramerd1e17032010-09-27 17:42:11 +000096 bool TokError(const Twine &Msg) const {
Chris Lattnerf4601652007-11-22 20:49:04 +000097 return Error(Lex.getLoc(), Msg);
98 }
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +000099 const std::vector<std::string> &getDependencies() const {
100 return Lex.getDependencies();
101 }
David Greenecebb4ee2012-02-22 16:09:41 +0000102
Chris Lattnerf4601652007-11-22 20:49:04 +0000103private: // Semantic analysis methods.
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000104 bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
David Greene917924d2011-10-19 13:02:39 +0000105 bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
David Greene05bce0b2011-07-29 22:43:06 +0000106 const std::vector<unsigned> &BitList, Init *V);
David Greene917924d2011-10-19 13:02:39 +0000107 bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
108 const std::vector<unsigned> &BitList, Init *V) {
109 return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V);
110 }
Cedric Venetaff9c272009-02-14 16:06:42 +0000111 bool AddSubClass(Record *Rec, SubClassReference &SubClass);
Bob Wilson440548d2009-04-30 18:26:19 +0000112 bool AddSubMultiClass(MultiClass *CurMC,
113 SubMultiClassReference &SubMultiClass);
Chris Lattnerf4601652007-11-22 20:49:04 +0000114
David Greenecebb4ee2012-02-22 16:09:41 +0000115 // IterRecord: Map an iterator name to a value.
116 struct IterRecord {
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000117 VarInit *IterVar;
David Greenecebb4ee2012-02-22 16:09:41 +0000118 Init *IterValue;
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000119 IterRecord(VarInit *Var, Init *Val) : IterVar(Var), IterValue(Val) {}
David Greenecebb4ee2012-02-22 16:09:41 +0000120 };
121
122 // IterSet: The set of all iterator values at some point in the
123 // iteration space.
124 typedef std::vector<IterRecord> IterSet;
125
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000126 bool ProcessForeachDefs(Record *CurRec, SMLoc Loc);
127 bool ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals);
David Greenecebb4ee2012-02-22 16:09:41 +0000128
Chris Lattnerf4601652007-11-22 20:49:04 +0000129private: // Parser methods.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +0000130 bool ParseObjectList(MultiClass *MC = 0);
131 bool ParseObject(MultiClass *MC);
Chris Lattnerf4601652007-11-22 20:49:04 +0000132 bool ParseClass();
133 bool ParseMultiClass();
David Greenee499a2d2011-10-05 22:42:07 +0000134 Record *InstantiateMulticlassDef(MultiClass &MC,
135 Record *DefProto,
David Greene7be867e2011-10-19 13:04:31 +0000136 Init *DefmPrefix,
David Greenee499a2d2011-10-05 22:42:07 +0000137 SMLoc DefmPrefixLoc);
138 bool ResolveMulticlassDefArgs(MultiClass &MC,
139 Record *DefProto,
140 SMLoc DefmPrefixLoc,
141 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +0000142 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +0000143 std::vector<Init *> &TemplateVals,
144 bool DeleteArgs);
145 bool ResolveMulticlassDef(MultiClass &MC,
146 Record *CurRec,
147 Record *DefProto,
148 SMLoc DefmPrefixLoc);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +0000149 bool ParseDefm(MultiClass *CurMultiClass);
150 bool ParseDef(MultiClass *CurMultiClass);
David Greenecebb4ee2012-02-22 16:09:41 +0000151 bool ParseForeach(MultiClass *CurMultiClass);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +0000152 bool ParseTopLevelLet(MultiClass *CurMultiClass);
Chris Lattnerf4601652007-11-22 20:49:04 +0000153 std::vector<LetRecord> ParseLetList();
154
Chris Lattnerf4601652007-11-22 20:49:04 +0000155 bool ParseObjectBody(Record *CurRec);
156 bool ParseBody(Record *CurRec);
157 bool ParseBodyItem(Record *CurRec);
158
159 bool ParseTemplateArgList(Record *CurRec);
David Greenee22b3212011-10-19 13:02:42 +0000160 Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +0000161 VarInit *ParseForeachDeclaration(ListInit *&ForeachListValue);
Chris Lattnerf4601652007-11-22 20:49:04 +0000162
163 SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
Bob Wilson440548d2009-04-30 18:26:19 +0000164 SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
Chris Lattnerf4601652007-11-22 20:49:04 +0000165
David Greenef3744a02011-10-19 13:04:20 +0000166 Init *ParseIDValue(Record *CurRec, IDParseMode Mode = ParseValueMode);
167 Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc,
168 IDParseMode Mode = ParseValueMode);
169 Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0,
170 IDParseMode Mode = ParseValueMode);
171 Init *ParseValue(Record *CurRec, RecTy *ItemType = 0,
172 IDParseMode Mode = ParseValueMode);
David Greene05bce0b2011-07-29 22:43:06 +0000173 std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0, RecTy *EltTy = 0);
174 std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
Chris Lattnerf4601652007-11-22 20:49:04 +0000175 bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
176 bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
177 std::vector<unsigned> ParseRangeList();
178 bool ParseRangePiece(std::vector<unsigned> &Ranges);
179 RecTy *ParseType();
David Greene05bce0b2011-07-29 22:43:06 +0000180 Init *ParseOperation(Record *CurRec);
David Greened418c1b2009-05-14 20:54:48 +0000181 RecTy *ParseOperatorType();
David Greenea9e07dd2011-10-19 13:04:29 +0000182 Init *ParseObjectName(MultiClass *CurMultiClass);
Chris Lattnerf4601652007-11-22 20:49:04 +0000183 Record *ParseClassID();
David Greenede444af2009-04-22 16:42:54 +0000184 MultiClass *ParseMultiClassID();
Chris Lattnerf4601652007-11-22 20:49:04 +0000185 Record *ParseDefmID();
186};
187
188} // end namespace llvm
189
190#endif