blob: 2732680d2fbfb10e274bbb074bf813c9584068af [file] [log] [blame]
Chris Lattnere62c1182002-12-02 01:23:04 +00001//===-- FileParser.y - Parser for TableGen files ----------------*- C++ -*-===//
John Criswellaefb6662003-10-21 15:29:18 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnere62c1182002-12-02 01:23:04 +00009//
10// This file implements the bison parser for Table Generator files...
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattnere62c1182002-12-02 01:23:04 +000013
14%{
15#include "Record.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000016#include "llvm/ADT/StringExtras.h"
Bill Wendlingf5da1332006-12-07 22:21:48 +000017#include "llvm/Support/Streams.h"
Chris Lattnere62c1182002-12-02 01:23:04 +000018#include <algorithm>
Chris Lattnerfc06bf02003-07-30 04:26:44 +000019#include <cstdio>
Chris Lattnere62c1182002-12-02 01:23:04 +000020#define YYERROR_VERBOSE 1
21
22int yyerror(const char *ErrorMsg);
23int yylex();
Brian Gaeked0fde302003-11-11 22:41:34 +000024
25namespace llvm {
Chris Lattner12069862006-09-01 21:13:49 +000026 struct MultiClass {
27 Record Rec; // Placeholder for template args and Name.
28 std::vector<Record*> DefPrototypes;
29
30 MultiClass(const std::string &Name) : Rec(Name) {}
31 };
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner12069862006-09-01 21:13:49 +000033
34static std::map<std::string, MultiClass*> MultiClasses;
35
Chris Lattnere62c1182002-12-02 01:23:04 +000036extern int Filelineno;
Chris Lattner12069862006-09-01 21:13:49 +000037static MultiClass *CurMultiClass = 0; // Set while parsing a multiclass.
38static std::string *CurDefmPrefix = 0; // Set while parsing defm.
Chris Lattnere62c1182002-12-02 01:23:04 +000039static Record *CurRec = 0;
Chris Lattner7dda3952005-04-19 03:36:21 +000040static bool ParsingTemplateArgs = false;
Chris Lattnere62c1182002-12-02 01:23:04 +000041
42typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
43
Chris Lattner42aa89e2003-08-04 04:56:53 +000044struct LetRecord {
Chris Lattner60094252003-08-03 13:58:01 +000045 std::string Name;
46 std::vector<unsigned> Bits;
47 Init *Value;
48 bool HasBits;
Chris Lattner42aa89e2003-08-04 04:56:53 +000049 LetRecord(const std::string &N, std::vector<unsigned> *B, Init *V)
Chris Lattner60094252003-08-03 13:58:01 +000050 : Name(N), Value(V), HasBits(B != 0) {
51 if (HasBits) Bits = *B;
52 }
53};
54
Chris Lattner42aa89e2003-08-04 04:56:53 +000055static std::vector<std::vector<LetRecord> > LetStack;
Chris Lattner60094252003-08-03 13:58:01 +000056
Chris Lattnere62c1182002-12-02 01:23:04 +000057
Chris Lattner7dff0532003-07-30 20:56:47 +000058extern std::ostream &err();
Chris Lattnere62c1182002-12-02 01:23:04 +000059
Chris Lattner12069862006-09-01 21:13:49 +000060/// getActiveRec - If inside a def/class definition, return the def/class.
61/// Otherwise, if within a multidef, return it.
62static Record *getActiveRec() {
63 return CurRec ? CurRec : &CurMultiClass->Rec;
64}
65
Chris Lattnere62c1182002-12-02 01:23:04 +000066static void addValue(const RecordVal &RV) {
Chris Lattner12069862006-09-01 21:13:49 +000067 Record *TheRec = getActiveRec();
68
69 if (RecordVal *ERV = TheRec->getValue(RV.getName())) {
Chris Lattner60094252003-08-03 13:58:01 +000070 // The value already exists in the class, treat this as a set...
71 if (ERV->setValue(RV.getValue())) {
72 err() << "New definition of '" << RV.getName() << "' of type '"
73 << *RV.getType() << "' is incompatible with previous "
74 << "definition of type '" << *ERV->getType() << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000075 exit(1);
Chris Lattner60094252003-08-03 13:58:01 +000076 }
77 } else {
Chris Lattner12069862006-09-01 21:13:49 +000078 TheRec->addValue(RV);
Chris Lattnere62c1182002-12-02 01:23:04 +000079 }
Chris Lattnere62c1182002-12-02 01:23:04 +000080}
81
82static void addSuperClass(Record *SC) {
83 if (CurRec->isSubClassOf(SC)) {
84 err() << "Already subclass of '" << SC->getName() << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000085 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +000086 }
87 CurRec->addSuperClass(SC);
88}
89
90static void setValue(const std::string &ValName,
Chris Lattner273d4632006-01-31 06:02:35 +000091 std::vector<unsigned> *BitList, Init *V) {
Chris Lattner81d50ad2004-02-28 17:31:28 +000092 if (!V) return;
Chris Lattnere62c1182002-12-02 01:23:04 +000093
Chris Lattner85899b82006-10-07 07:14:48 +000094 Record *TheRec = getActiveRec();
95 RecordVal *RV = TheRec->getValue(ValName);
Chris Lattnere62c1182002-12-02 01:23:04 +000096 if (RV == 0) {
97 err() << "Value '" << ValName << "' unknown!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000098 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +000099 }
Chris Lattner81d50ad2004-02-28 17:31:28 +0000100
101 // Do not allow assignments like 'X = X'. This will just cause infinite loops
102 // in the resolution machinery.
103 if (!BitList)
104 if (VarInit *VI = dynamic_cast<VarInit*>(V))
105 if (VI->getName() == ValName)
106 return;
Chris Lattnere62c1182002-12-02 01:23:04 +0000107
108 // If we are assigning to a subset of the bits in the value... then we must be
109 // assigning to a field of BitsRecTy, which must have a BitsInit
110 // initializer...
111 //
112 if (BitList) {
113 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
114 if (CurVal == 0) {
115 err() << "Value '" << ValName << "' is not a bits type!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000116 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000117 }
118
119 // Convert the incoming value to a bits type of the appropriate size...
120 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
121 if (BI == 0) {
122 V->convertInitializerTo(new BitsRecTy(BitList->size()));
123 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000124 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000125 }
126
127 // We should have a BitsInit type now...
Bill Wendlingf5da1332006-12-07 22:21:48 +0000128 assert(dynamic_cast<BitsInit*>(BI) != 0 || (cerr << *BI).stream() == 0);
Chris Lattnere62c1182002-12-02 01:23:04 +0000129 BitsInit *BInit = (BitsInit*)BI;
130
131 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
132
Chris Lattnerade0de92002-12-06 03:55:39 +0000133 // Loop over bits, assigning values as appropriate...
Chris Lattnere62c1182002-12-02 01:23:04 +0000134 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
135 unsigned Bit = (*BitList)[i];
Chris Lattner28c2d402002-12-06 04:42:16 +0000136 if (NewVal->getBit(Bit)) {
137 err() << "Cannot set bit #" << Bit << " of value '" << ValName
Chris Lattnerade0de92002-12-06 03:55:39 +0000138 << "' more than once!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000139 exit(1);
Chris Lattnerade0de92002-12-06 03:55:39 +0000140 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000141 NewVal->setBit(Bit, BInit->getBit(i));
142 }
Chris Lattnerade0de92002-12-06 03:55:39 +0000143
144 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
145 if (NewVal->getBit(i) == 0)
146 NewVal->setBit(i, CurVal->getBit(i));
147
Chris Lattnere62c1182002-12-02 01:23:04 +0000148 V = NewVal;
149 }
150
151 if (RV->setValue(V)) {
152 err() << "Value '" << ValName << "' of type '" << *RV->getType()
153 << "' is incompatible with initializer '" << *V << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000154 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000155 }
156}
157
Chris Lattner7dda3952005-04-19 03:36:21 +0000158// addSubClass - Add SC as a subclass to CurRec, resolving TemplateArgs as SC's
159// template arguments.
Chris Lattnere62c1182002-12-02 01:23:04 +0000160static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
161 // Add all of the values in the subclass into the current class...
162 const std::vector<RecordVal> &Vals = SC->getValues();
163 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
164 addValue(Vals[i]);
165
166 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
167
168 // Ensure that an appropriate number of template arguments are specified...
169 if (TArgs.size() < TemplateArgs.size()) {
Misha Brukmane3d333e2003-05-20 23:45:36 +0000170 err() << "ERROR: More template args specified than expected!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000171 exit(1);
Chris Lattner12069862006-09-01 21:13:49 +0000172 }
173
174 // Loop over all of the template arguments, setting them to the specified
175 // value or leaving them as the default if necessary.
176 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
177 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
178 // Set it now.
179 setValue(TArgs[i], 0, TemplateArgs[i]);
Chris Lattner7dda3952005-04-19 03:36:21 +0000180
Chris Lattner12069862006-09-01 21:13:49 +0000181 // Resolve it next.
182 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
183
184
185 // Now remove it.
186 CurRec->removeValue(TArgs[i]);
Chris Lattner7dda3952005-04-19 03:36:21 +0000187
Chris Lattner12069862006-09-01 21:13:49 +0000188 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
189 err() << "ERROR: Value not specified for template argument #"
190 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
191 << "'!\n";
192 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000193 }
194 }
195
Chris Lattnere62c1182002-12-02 01:23:04 +0000196 // Since everything went well, we can now set the "superclass" list for the
197 // current record.
Chris Lattner12069862006-09-01 21:13:49 +0000198 const std::vector<Record*> &SCs = SC->getSuperClasses();
Chris Lattnere62c1182002-12-02 01:23:04 +0000199 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
200 addSuperClass(SCs[i]);
201 addSuperClass(SC);
202}
203
Brian Gaeked0fde302003-11-11 22:41:34 +0000204} // End llvm namespace
205
206using namespace llvm;
Chris Lattnere62c1182002-12-02 01:23:04 +0000207
208%}
209
210%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000211 std::string* StrVal;
212 int IntVal;
213 llvm::RecTy* Ty;
214 llvm::Init* Initializer;
215 std::vector<llvm::Init*>* FieldList;
216 std::vector<unsigned>* BitList;
217 llvm::Record* Rec;
Chris Lattner12069862006-09-01 21:13:49 +0000218 std::vector<llvm::Record*>* RecList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000219 SubClassRefTy* SubClassRef;
220 std::vector<SubClassRefTy>* SubClassList;
221 std::vector<std::pair<llvm::Init*, std::string> >* DagValueList;
Chris Lattnere62c1182002-12-02 01:23:04 +0000222};
223
Chris Lattner12069862006-09-01 21:13:49 +0000224%token INT BIT STRING BITS LIST CODE DAG CLASS DEF MULTICLASS DEFM FIELD LET IN
Chris Lattner711e5d92006-03-31 21:53:49 +0000225%token SHLTOK SRATOK SRLTOK STRCONCATTOK
Chris Lattnere62c1182002-12-02 01:23:04 +0000226%token <IntVal> INTVAL
Chris Lattnerfb9ea582003-08-10 22:04:25 +0000227%token <StrVal> ID VARNAME STRVAL CODEFRAGMENT
Chris Lattnere62c1182002-12-02 01:23:04 +0000228
229%type <Ty> Type
Chris Lattner12069862006-09-01 21:13:49 +0000230%type <Rec> ClassInst DefInst MultiClassDef ObjectBody ClassID
231%type <RecList> MultiClassBody
Chris Lattnere62c1182002-12-02 01:23:04 +0000232
233%type <SubClassRef> SubClassRef
234%type <SubClassList> ClassList ClassListNE
235%type <IntVal> OptPrefix
Chris Lattner8c063182006-03-30 22:50:40 +0000236%type <Initializer> Value OptValue IDValue
Chris Lattnerbc21c342003-08-04 20:44:43 +0000237%type <DagValueList> DagArgList DagArgListNE
Chris Lattnere62c1182002-12-02 01:23:04 +0000238%type <FieldList> ValueList ValueListNE
239%type <BitList> BitList OptBitList RBitList
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000240%type <StrVal> Declaration OptID OptVarName ObjectName
Chris Lattnere62c1182002-12-02 01:23:04 +0000241
242%start File
Brian Gaeked0fde302003-11-11 22:41:34 +0000243
Chris Lattnere62c1182002-12-02 01:23:04 +0000244%%
245
246ClassID : ID {
Chris Lattner12069862006-09-01 21:13:49 +0000247 if (CurDefmPrefix) {
248 // If CurDefmPrefix is set, we're parsing a defm, which means that this is
249 // actually the name of a multiclass.
250 MultiClass *MC = MultiClasses[*$1];
251 if (MC == 0) {
252 err() << "Couldn't find class '" << *$1 << "'!\n";
253 exit(1);
254 }
255 $$ = &MC->Rec;
256 } else {
257 $$ = Records.getClass(*$1);
258 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000259 if ($$ == 0) {
260 err() << "Couldn't find class '" << *$1 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000261 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000262 }
263 delete $1;
264 };
265
Chris Lattnere62c1182002-12-02 01:23:04 +0000266
267// TableGen types...
268Type : STRING { // string type
269 $$ = new StringRecTy();
270 } | BIT { // bit type
271 $$ = new BitRecTy();
272 } | BITS '<' INTVAL '>' { // bits<x> type
273 $$ = new BitsRecTy($3);
274 } | INT { // int type
275 $$ = new IntRecTy();
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000276 } | LIST '<' Type '>' { // list<x> type
Chris Lattnere62c1182002-12-02 01:23:04 +0000277 $$ = new ListRecTy($3);
Chris Lattnerf05760d2003-07-30 21:47:42 +0000278 } | CODE { // code type
279 $$ = new CodeRecTy();
Chris Lattner40f71132003-08-04 04:50:57 +0000280 } | DAG { // dag type
281 $$ = new DagRecTy();
Chris Lattnere62c1182002-12-02 01:23:04 +0000282 } | ClassID { // Record Type
283 $$ = new RecordRecTy($1);
284 };
285
286OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
287
288OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
289
Chris Lattner8c063182006-03-30 22:50:40 +0000290IDValue : ID {
291 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
292 $$ = new VarInit(*$1, RV->getType());
293 } else if (CurRec && CurRec->isTemplateArg(CurRec->getName()+":"+*$1)) {
294 const RecordVal *RV = CurRec->getValue(CurRec->getName()+":"+*$1);
295 assert(RV && "Template arg doesn't exist??");
296 $$ = new VarInit(CurRec->getName()+":"+*$1, RV->getType());
Chris Lattner12069862006-09-01 21:13:49 +0000297 } else if (CurMultiClass &&
298 CurMultiClass->Rec.isTemplateArg(CurMultiClass->Rec.getName()+"::"+*$1)) {
299 std::string Name = CurMultiClass->Rec.getName()+"::"+*$1;
300 const RecordVal *RV = CurMultiClass->Rec.getValue(Name);
301 assert(RV && "Template arg doesn't exist??");
302 $$ = new VarInit(Name, RV->getType());
Chris Lattner8c063182006-03-30 22:50:40 +0000303 } else if (Record *D = Records.getDef(*$1)) {
304 $$ = new DefInit(D);
305 } else {
306 err() << "Variable not defined: '" << *$1 << "'!\n";
307 exit(1);
308 }
309
310 delete $1;
311};
312
313Value : IDValue {
314 $$ = $1;
315 } | INTVAL {
Chris Lattnere62c1182002-12-02 01:23:04 +0000316 $$ = new IntInit($1);
317 } | STRVAL {
318 $$ = new StringInit(*$1);
319 delete $1;
Chris Lattnere3a1d052003-07-30 22:15:58 +0000320 } | CODEFRAGMENT {
321 $$ = new CodeInit(*$1);
322 delete $1;
Chris Lattnere62c1182002-12-02 01:23:04 +0000323 } | '?' {
324 $$ = new UnsetInit();
325 } | '{' ValueList '}' {
326 BitsInit *Init = new BitsInit($2->size());
327 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
328 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
329 if (Bit == 0) {
Chris Lattner69b545e2005-09-08 18:22:35 +0000330 err() << "Element #" << i << " (" << *(*$2)[i]
331 << ") is not convertable to a bit!\n";
332 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000333 }
334 Init->setBit($2->size()-i-1, Bit);
335 }
336 $$ = Init;
337 delete $2;
Chris Lattnera1207a52005-09-08 18:48:23 +0000338 } | ID '<' ValueListNE '>' {
339 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
340 // a new anonymous definition, deriving from CLASS<initvalslist> with no
341 // body.
342 Record *Class = Records.getClass(*$1);
343 if (!Class) {
344 err() << "Expected a class, got '" << *$1 << "'!\n";
345 exit(1);
346 }
347 delete $1;
348
349 static unsigned AnonCounter = 0;
350 Record *OldRec = CurRec; // Save CurRec.
351
352 // Create the new record, set it as CurRec temporarily.
353 CurRec = new Record("anonymous.val."+utostr(AnonCounter++));
354 addSubClass(Class, *$3); // Add info about the subclass to CurRec.
355 delete $3; // Free up the template args.
356
357 CurRec->resolveReferences();
358
359 Records.addDef(CurRec);
360
361 // The result of the expression is a reference to the new record.
362 $$ = new DefInit(CurRec);
363
364 // Restore the old CurRec
365 CurRec = OldRec;
Chris Lattnere62c1182002-12-02 01:23:04 +0000366 } | Value '{' BitList '}' {
367 $$ = $1->convertInitializerBitRange(*$3);
368 if ($$ == 0) {
369 err() << "Invalid bit range for value '" << *$1 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000370 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000371 }
372 delete $3;
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000373 } | '[' ValueList ']' {
Chris Lattnere62c1182002-12-02 01:23:04 +0000374 $$ = new ListInit(*$2);
375 delete $2;
Chris Lattner34a77692002-12-02 16:43:43 +0000376 } | Value '.' ID {
377 if (!$1->getFieldType(*$3)) {
378 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000379 exit(1);
Chris Lattner34a77692002-12-02 16:43:43 +0000380 }
381 $$ = new FieldInit($1, *$3);
382 delete $3;
Chris Lattner8c063182006-03-30 22:50:40 +0000383 } | '(' IDValue DagArgList ')' {
384 $$ = new DagInit($2, *$3);
385 delete $3;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000386 } | Value '[' BitList ']' {
387 std::reverse($3->begin(), $3->end());
388 $$ = $1->convertInitListSlice(*$3);
389 if ($$ == 0) {
390 err() << "Invalid list slice for value '" << *$1 << "'!\n";
391 exit(1);
392 }
393 delete $3;
Chris Lattnerb9266f82005-04-19 01:11:03 +0000394 } | SHLTOK '(' Value ',' Value ')' {
Chris Lattner711e5d92006-03-31 21:53:49 +0000395 $$ = (new BinOpInit(BinOpInit::SHL, $3, $5))->Fold();
Chris Lattnerb9266f82005-04-19 01:11:03 +0000396 } | SRATOK '(' Value ',' Value ')' {
Chris Lattner711e5d92006-03-31 21:53:49 +0000397 $$ = (new BinOpInit(BinOpInit::SRA, $3, $5))->Fold();
Chris Lattnerb9266f82005-04-19 01:11:03 +0000398 } | SRLTOK '(' Value ',' Value ')' {
Chris Lattner711e5d92006-03-31 21:53:49 +0000399 $$ = (new BinOpInit(BinOpInit::SRL, $3, $5))->Fold();
400 } | STRCONCATTOK '(' Value ',' Value ')' {
401 $$ = (new BinOpInit(BinOpInit::STRCONCAT, $3, $5))->Fold();
Chris Lattnere62c1182002-12-02 01:23:04 +0000402 };
403
Chris Lattner91290d72003-08-10 22:14:13 +0000404OptVarName : /* empty */ {
405 $$ = new std::string();
Chris Lattnerbc21c342003-08-04 20:44:43 +0000406 }
Chris Lattner91290d72003-08-10 22:14:13 +0000407 | ':' VARNAME {
408 $$ = $2;
409 };
410
411DagArgListNE : Value OptVarName {
412 $$ = new std::vector<std::pair<Init*, std::string> >();
413 $$->push_back(std::make_pair($1, *$2));
414 delete $2;
415 }
416 | DagArgListNE ',' Value OptVarName {
417 $1->push_back(std::make_pair($3, *$4));
418 delete $4;
419 $$ = $1;
Chris Lattnerbc21c342003-08-04 20:44:43 +0000420 };
421
422DagArgList : /*empty*/ {
Chris Lattner91290d72003-08-10 22:14:13 +0000423 $$ = new std::vector<std::pair<Init*, std::string> >();
Chris Lattnerbc21c342003-08-04 20:44:43 +0000424 }
425 | DagArgListNE { $$ = $1; };
426
427
Chris Lattnere62c1182002-12-02 01:23:04 +0000428RBitList : INTVAL {
429 $$ = new std::vector<unsigned>();
430 $$->push_back($1);
431 } | INTVAL '-' INTVAL {
Chris Lattnerb0fef642004-07-26 23:21:34 +0000432 if ($1 < 0 || $3 < 0) {
433 err() << "Invalid range: " << $1 << "-" << $3 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000434 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000435 }
436 $$ = new std::vector<unsigned>();
Chris Lattnerb0fef642004-07-26 23:21:34 +0000437 if ($1 < $3) {
438 for (int i = $1; i <= $3; ++i)
439 $$->push_back(i);
440 } else {
441 for (int i = $1; i >= $3; --i)
442 $$->push_back(i);
443 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000444 } | INTVAL INTVAL {
445 $2 = -$2;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000446 if ($1 < 0 || $2 < 0) {
447 err() << "Invalid range: " << $1 << "-" << $2 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000448 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000449 }
450 $$ = new std::vector<unsigned>();
Chris Lattnerb0fef642004-07-26 23:21:34 +0000451 if ($1 < $2) {
452 for (int i = $1; i <= $2; ++i)
453 $$->push_back(i);
454 } else {
455 for (int i = $1; i >= $2; --i)
456 $$->push_back(i);
457 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000458 } | RBitList ',' INTVAL {
459 ($$=$1)->push_back($3);
460 } | RBitList ',' INTVAL '-' INTVAL {
Chris Lattnerb0fef642004-07-26 23:21:34 +0000461 if ($3 < 0 || $5 < 0) {
462 err() << "Invalid range: " << $3 << "-" << $5 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000463 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000464 }
465 $$ = $1;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000466 if ($3 < $5) {
467 for (int i = $3; i <= $5; ++i)
468 $$->push_back(i);
469 } else {
470 for (int i = $3; i >= $5; --i)
471 $$->push_back(i);
472 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000473 } | RBitList ',' INTVAL INTVAL {
474 $4 = -$4;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000475 if ($3 < 0 || $4 < 0) {
476 err() << "Invalid range: " << $3 << "-" << $4 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000477 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000478 }
479 $$ = $1;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000480 if ($3 < $4) {
481 for (int i = $3; i <= $4; ++i)
482 $$->push_back(i);
483 } else {
484 for (int i = $3; i >= $4; --i)
485 $$->push_back(i);
486 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000487 };
488
489BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
490
491OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
492
493
494
495ValueList : /*empty*/ {
496 $$ = new std::vector<Init*>();
497 } | ValueListNE {
498 $$ = $1;
499 };
500
501ValueListNE : Value {
502 $$ = new std::vector<Init*>();
503 $$->push_back($1);
504 } | ValueListNE ',' Value {
505 ($$ = $1)->push_back($3);
506 };
507
508Declaration : OptPrefix Type ID OptValue {
Chris Lattner7dda3952005-04-19 03:36:21 +0000509 std::string DecName = *$3;
Chris Lattner12069862006-09-01 21:13:49 +0000510 if (ParsingTemplateArgs) {
511 if (CurRec) {
512 DecName = CurRec->getName() + ":" + DecName;
513 } else {
514 assert(CurMultiClass);
515 }
516 if (CurMultiClass)
517 DecName = CurMultiClass->Rec.getName() + "::" + DecName;
518 }
Chris Lattner7dda3952005-04-19 03:36:21 +0000519
520 addValue(RecordVal(DecName, $2, $1));
521 setValue(DecName, 0, $4);
522 $$ = new std::string(DecName);
Chris Lattnere62c1182002-12-02 01:23:04 +0000523};
524
525BodyItem : Declaration ';' {
526 delete $1;
Chris Lattner42aa89e2003-08-04 04:56:53 +0000527} | LET ID OptBitList '=' Value ';' {
Chris Lattnere62c1182002-12-02 01:23:04 +0000528 setValue(*$2, $3, $5);
529 delete $2;
530 delete $3;
531};
532
533BodyList : /*empty*/ | BodyList BodyItem;
534Body : ';' | '{' BodyList '}';
535
536SubClassRef : ClassID {
537 $$ = new SubClassRefTy($1, new std::vector<Init*>());
538 } | ClassID '<' ValueListNE '>' {
539 $$ = new SubClassRefTy($1, $3);
540 };
541
542ClassListNE : SubClassRef {
543 $$ = new std::vector<SubClassRefTy>();
544 $$->push_back(*$1);
545 delete $1;
546 }
547 | ClassListNE ',' SubClassRef {
548 ($$=$1)->push_back(*$3);
549 delete $3;
550 };
551
552ClassList : /*empty */ {
553 $$ = new std::vector<SubClassRefTy>();
554 }
555 | ':' ClassListNE {
556 $$ = $2;
557 };
558
559DeclListNE : Declaration {
Chris Lattner12069862006-09-01 21:13:49 +0000560 getActiveRec()->addTemplateArg(*$1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000561 delete $1;
562} | DeclListNE ',' Declaration {
Chris Lattner12069862006-09-01 21:13:49 +0000563 getActiveRec()->addTemplateArg(*$3);
Chris Lattnere62c1182002-12-02 01:23:04 +0000564 delete $3;
565};
566
567TemplateArgList : '<' DeclListNE '>' {};
568OptTemplateArgList : /*empty*/ | TemplateArgList;
569
Chris Lattnerfc06bf02003-07-30 04:26:44 +0000570OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
571
Chris Lattnerbe88b502005-09-30 04:10:49 +0000572ObjectName : OptID {
573 static unsigned AnonCounter = 0;
574 if ($1->empty())
575 *$1 = "anonymous."+utostr(AnonCounter++);
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000576 $$ = $1;
Chris Lattnerbe88b502005-09-30 04:10:49 +0000577};
578
579ClassName : ObjectName {
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000580 // If a class of this name already exists, it must be a forward ref.
581 if ((CurRec = Records.getClass(*$1))) {
582 // If the body was previously defined, this is an error.
583 if (!CurRec->getValues().empty() ||
584 !CurRec->getSuperClasses().empty() ||
585 !CurRec->getTemplateArgs().empty()) {
586 err() << "Class '" << CurRec->getName() << "' already defined!\n";
587 exit(1);
588 }
589 } else {
590 // If this is the first reference to this class, create and add it.
591 CurRec = new Record(*$1);
592 Records.addClass(CurRec);
Chris Lattnerbe88b502005-09-30 04:10:49 +0000593 }
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000594 delete $1;
Chris Lattnerbe88b502005-09-30 04:10:49 +0000595};
596
597DefName : ObjectName {
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000598 CurRec = new Record(*$1);
599 delete $1;
600
Chris Lattner12069862006-09-01 21:13:49 +0000601 if (!CurMultiClass) {
602 // Top-level def definition.
603
604 // Ensure redefinition doesn't happen.
605 if (Records.getDef(CurRec->getName())) {
606 err() << "def '" << CurRec->getName() << "' already defined!\n";
607 exit(1);
608 }
609 Records.addDef(CurRec);
610 } else {
611 // Otherwise, a def inside a multiclass, add it to the multiclass.
612 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
613 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
614 err() << "def '" << CurRec->getName()
615 << "' already defined in this multiclass!\n";
616 exit(1);
617 }
618 CurMultiClass->DefPrototypes.push_back(CurRec);
Chris Lattnerbe88b502005-09-30 04:10:49 +0000619 }
Chris Lattnerbe88b502005-09-30 04:10:49 +0000620};
621
Chris Lattner583a0242005-09-30 04:42:31 +0000622ObjectBody : ClassList {
Chris Lattner583a0242005-09-30 04:42:31 +0000623 for (unsigned i = 0, e = $1->size(); i != e; ++i) {
624 addSubClass((*$1)[i].first, *(*$1)[i].second);
Chris Lattnerbfce0562003-07-30 04:56:05 +0000625 // Delete the template arg values for the class
Chris Lattner583a0242005-09-30 04:42:31 +0000626 delete (*$1)[i].second;
Chris Lattnerbfce0562003-07-30 04:56:05 +0000627 }
Chris Lattner1ceb6c82006-10-11 18:12:44 +0000628 delete $1; // Delete the class list.
Chris Lattner583a0242005-09-30 04:42:31 +0000629
Chris Lattner1ceb6c82006-10-11 18:12:44 +0000630 // Process any variables on the let stack.
Chris Lattner69b545e2005-09-08 18:22:35 +0000631 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
Chris Lattner42aa89e2003-08-04 04:56:53 +0000632 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
633 setValue(LetStack[i][j].Name,
634 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
635 LetStack[i][j].Value);
Chris Lattnere62c1182002-12-02 01:23:04 +0000636 } Body {
Chris Lattner7dda3952005-04-19 03:36:21 +0000637 $$ = CurRec;
638 CurRec = 0;
639 };
Chris Lattnere62c1182002-12-02 01:23:04 +0000640
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000641ClassInst : CLASS ClassName {
642 ParsingTemplateArgs = true;
643 } OptTemplateArgList {
644 ParsingTemplateArgs = false;
645 } ObjectBody {
646 $$ = $6;
Chris Lattner583a0242005-09-30 04:42:31 +0000647 };
Chris Lattnere62c1182002-12-02 01:23:04 +0000648
Chris Lattnerbe88b502005-09-30 04:10:49 +0000649DefInst : DEF DefName ObjectBody {
Chris Lattner7717d092006-09-01 21:59:03 +0000650 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
651 $3->resolveReferences();
Chris Lattner7dda3952005-04-19 03:36:21 +0000652
Chris Lattnera1207a52005-09-08 18:48:23 +0000653 // If ObjectBody has template arguments, it's an error.
Chris Lattner583a0242005-09-30 04:42:31 +0000654 assert($3->getTemplateArgs().empty() && "How'd this get template args?");
Chris Lattnerbe88b502005-09-30 04:10:49 +0000655 $$ = $3;
Chris Lattnere62c1182002-12-02 01:23:04 +0000656};
657
Chris Lattner12069862006-09-01 21:13:49 +0000658// MultiClassDef - A def instance specified inside a multiclass.
659MultiClassDef : DefInst {
660 $$ = $1;
661 // Copy the template arguments for the multiclass into the def.
662 const std::vector<std::string> &TArgs = CurMultiClass->Rec.getTemplateArgs();
663
664 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
665 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
666 assert(RV && "Template arg doesn't exist?");
667 $$->addValue(*RV);
668 }
669};
Chris Lattnere62c1182002-12-02 01:23:04 +0000670
Chris Lattner12069862006-09-01 21:13:49 +0000671// MultiClassBody - Sequence of def's that are instantiated when a multiclass is
672// used.
673MultiClassBody : MultiClassDef {
674 $$ = new std::vector<Record*>();
675 $$->push_back($1);
676} | MultiClassBody MultiClassDef {
677 $$->push_back($2);
678};
679
680MultiClassName : ID {
681 MultiClass *&MCE = MultiClasses[*$1];
682 if (MCE) {
683 err() << "multiclass '" << *$1 << "' already defined!\n";
684 exit(1);
685 }
686 MCE = CurMultiClass = new MultiClass(*$1);
687 delete $1;
688};
689
690// MultiClass - Multiple definitions.
691MultiClassInst : MULTICLASS MultiClassName {
692 ParsingTemplateArgs = true;
693 } OptTemplateArgList {
694 ParsingTemplateArgs = false;
695 }'{' MultiClassBody '}' {
696 CurMultiClass = 0;
697};
698
699// DefMInst - Instantiate a multiclass.
700DefMInst : DEFM ID { CurDefmPrefix = $2; } ':' SubClassRef ';' {
701 // To instantiate a multiclass, we need to first get the multiclass, then
702 // instantiate each def contained in the multiclass with the SubClassRef
703 // template parameters.
704 MultiClass *MC = MultiClasses[$5->first->getName()];
705 assert(MC && "Didn't lookup multiclass correctly?");
706 std::vector<Init*> &TemplateVals = *$5->second;
707 delete $5;
708
709 // Verify that the correct number of template arguments were specified.
710 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
711 if (TArgs.size() < TemplateVals.size()) {
712 err() << "ERROR: More template args specified than multiclass expects!\n";
713 exit(1);
714 }
715
716 // Loop over all the def's in the multiclass, instantiating each one.
717 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
718 Record *DefProto = MC->DefPrototypes[i];
719
720 // Add the suffix to the defm name to get the new name.
721 assert(CurRec == 0 && "A def is current?");
722 CurRec = new Record(*$2 + DefProto->getName());
723
724 addSubClass(DefProto, std::vector<Init*>());
725
726 // Loop over all of the template arguments, setting them to the specified
727 // value or leaving them as the default if necessary.
728 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
729 if (i < TemplateVals.size()) { // A value is specified for this temp-arg?
730 // Set it now.
731 setValue(TArgs[i], 0, TemplateVals[i]);
732
733 // Resolve it next.
734 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
735
736 // Now remove it.
737 CurRec->removeValue(TArgs[i]);
738
739 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
740 err() << "ERROR: Value not specified for template argument #"
741 << i << " (" << TArgs[i] << ") of multiclassclass '"
742 << MC->Rec.getName() << "'!\n";
743 exit(1);
744 }
745 }
746
Chris Lattner1ceb6c82006-10-11 18:12:44 +0000747 // If the mdef is inside a 'let' expression, add to each def.
748 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
749 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
750 setValue(LetStack[i][j].Name,
751 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
752 LetStack[i][j].Value);
753
754
Chris Lattner12069862006-09-01 21:13:49 +0000755 // Ensure redefinition doesn't happen.
756 if (Records.getDef(CurRec->getName())) {
757 err() << "def '" << CurRec->getName() << "' already defined, "
758 << "instantiating defm '" << *$2 << "' with subdef '"
759 << DefProto->getName() << "'!\n";
760 exit(1);
761 }
762 Records.addDef(CurRec);
Chris Lattner7717d092006-09-01 21:59:03 +0000763
764 CurRec->resolveReferences();
765
Chris Lattner12069862006-09-01 21:13:49 +0000766 CurRec = 0;
767 }
768
769 delete &TemplateVals;
770 delete $2;
Chris Lattnercce56af2006-09-01 22:07:27 +0000771 CurDefmPrefix = 0;
Chris Lattner12069862006-09-01 21:13:49 +0000772};
773
774Object : ClassInst {} | DefInst {};
775Object : MultiClassInst | DefMInst;
Chris Lattnere62c1182002-12-02 01:23:04 +0000776
Chris Lattner42aa89e2003-08-04 04:56:53 +0000777LETItem : ID OptBitList '=' Value {
778 LetStack.back().push_back(LetRecord(*$1, $2, $4));
Chris Lattner60094252003-08-03 13:58:01 +0000779 delete $1; delete $2;
Chris Lattner2e724542003-07-28 03:49:40 +0000780};
781
Chris Lattner42aa89e2003-08-04 04:56:53 +0000782LETList : LETItem | LETList ',' LETItem;
Chris Lattner60094252003-08-03 13:58:01 +0000783
Chris Lattner42aa89e2003-08-04 04:56:53 +0000784// LETCommand - A 'LET' statement start...
785LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
Chris Lattner60094252003-08-03 13:58:01 +0000786
Chris Lattner2e724542003-07-28 03:49:40 +0000787// Support Set commands wrapping objects... both with and without braces.
Chris Lattner42aa89e2003-08-04 04:56:53 +0000788Object : LETCommand '{' ObjectList '}' {
789 LetStack.pop_back();
Chris Lattner2e724542003-07-28 03:49:40 +0000790 }
Chris Lattner42aa89e2003-08-04 04:56:53 +0000791 | LETCommand Object {
792 LetStack.pop_back();
Chris Lattnere62c1182002-12-02 01:23:04 +0000793 };
794
795ObjectList : Object {} | ObjectList Object {};
796
Chris Lattner12069862006-09-01 21:13:49 +0000797File : ObjectList;
Chris Lattnere62c1182002-12-02 01:23:04 +0000798
799%%
800
801int yyerror(const char *ErrorMsg) {
802 err() << "Error parsing: " << ErrorMsg << "\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000803 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000804}