blob: 8e564564bdef4e6a8240bd07b491bb774319afbb [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"
Chris Lattnere62c1182002-12-02 01:23:04 +000017#include <algorithm>
Chris Lattnerfc06bf02003-07-30 04:26:44 +000018#include <cstdio>
Chris Lattnere62c1182002-12-02 01:23:04 +000019#define YYERROR_VERBOSE 1
20
21int yyerror(const char *ErrorMsg);
22int yylex();
Brian Gaeked0fde302003-11-11 22:41:34 +000023
24namespace llvm {
Chris Lattner12069862006-09-01 21:13:49 +000025 struct MultiClass {
26 Record Rec; // Placeholder for template args and Name.
27 std::vector<Record*> DefPrototypes;
28
29 MultiClass(const std::string &Name) : Rec(Name) {}
30 };
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Chris Lattner12069862006-09-01 21:13:49 +000032
33static std::map<std::string, MultiClass*> MultiClasses;
34
Chris Lattnere62c1182002-12-02 01:23:04 +000035extern int Filelineno;
Chris Lattner12069862006-09-01 21:13:49 +000036static MultiClass *CurMultiClass = 0; // Set while parsing a multiclass.
37static std::string *CurDefmPrefix = 0; // Set while parsing defm.
Chris Lattnere62c1182002-12-02 01:23:04 +000038static Record *CurRec = 0;
Chris Lattner7dda3952005-04-19 03:36:21 +000039static bool ParsingTemplateArgs = false;
Chris Lattnere62c1182002-12-02 01:23:04 +000040
41typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
42
Chris Lattner42aa89e2003-08-04 04:56:53 +000043struct LetRecord {
Chris Lattner60094252003-08-03 13:58:01 +000044 std::string Name;
45 std::vector<unsigned> Bits;
46 Init *Value;
47 bool HasBits;
Chris Lattner42aa89e2003-08-04 04:56:53 +000048 LetRecord(const std::string &N, std::vector<unsigned> *B, Init *V)
Chris Lattner60094252003-08-03 13:58:01 +000049 : Name(N), Value(V), HasBits(B != 0) {
50 if (HasBits) Bits = *B;
51 }
52};
53
Chris Lattner42aa89e2003-08-04 04:56:53 +000054static std::vector<std::vector<LetRecord> > LetStack;
Chris Lattner60094252003-08-03 13:58:01 +000055
Chris Lattnere62c1182002-12-02 01:23:04 +000056
Chris Lattner7dff0532003-07-30 20:56:47 +000057extern std::ostream &err();
Chris Lattnere62c1182002-12-02 01:23:04 +000058
Chris Lattner12069862006-09-01 21:13:49 +000059/// getActiveRec - If inside a def/class definition, return the def/class.
60/// Otherwise, if within a multidef, return it.
61static Record *getActiveRec() {
62 return CurRec ? CurRec : &CurMultiClass->Rec;
63}
64
Chris Lattnere62c1182002-12-02 01:23:04 +000065static void addValue(const RecordVal &RV) {
Chris Lattner12069862006-09-01 21:13:49 +000066 Record *TheRec = getActiveRec();
67
68 if (RecordVal *ERV = TheRec->getValue(RV.getName())) {
Chris Lattner60094252003-08-03 13:58:01 +000069 // The value already exists in the class, treat this as a set...
70 if (ERV->setValue(RV.getValue())) {
71 err() << "New definition of '" << RV.getName() << "' of type '"
72 << *RV.getType() << "' is incompatible with previous "
73 << "definition of type '" << *ERV->getType() << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000074 exit(1);
Chris Lattner60094252003-08-03 13:58:01 +000075 }
76 } else {
Chris Lattner12069862006-09-01 21:13:49 +000077 TheRec->addValue(RV);
Chris Lattnere62c1182002-12-02 01:23:04 +000078 }
Chris Lattnere62c1182002-12-02 01:23:04 +000079}
80
81static void addSuperClass(Record *SC) {
82 if (CurRec->isSubClassOf(SC)) {
83 err() << "Already subclass of '" << SC->getName() << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000084 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +000085 }
86 CurRec->addSuperClass(SC);
87}
88
89static void setValue(const std::string &ValName,
Chris Lattner273d4632006-01-31 06:02:35 +000090 std::vector<unsigned> *BitList, Init *V) {
Chris Lattner81d50ad2004-02-28 17:31:28 +000091 if (!V) return;
Chris Lattnere62c1182002-12-02 01:23:04 +000092
Chris Lattner85899b82006-10-07 07:14:48 +000093 Record *TheRec = getActiveRec();
94 RecordVal *RV = TheRec->getValue(ValName);
Chris Lattnere62c1182002-12-02 01:23:04 +000095 if (RV == 0) {
96 err() << "Value '" << ValName << "' unknown!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000097 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +000098 }
Chris Lattner81d50ad2004-02-28 17:31:28 +000099
100 // Do not allow assignments like 'X = X'. This will just cause infinite loops
101 // in the resolution machinery.
102 if (!BitList)
103 if (VarInit *VI = dynamic_cast<VarInit*>(V))
104 if (VI->getName() == ValName)
105 return;
Chris Lattnere62c1182002-12-02 01:23:04 +0000106
107 // If we are assigning to a subset of the bits in the value... then we must be
108 // assigning to a field of BitsRecTy, which must have a BitsInit
109 // initializer...
110 //
111 if (BitList) {
112 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
113 if (CurVal == 0) {
114 err() << "Value '" << ValName << "' is not a bits type!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000115 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000116 }
117
118 // Convert the incoming value to a bits type of the appropriate size...
119 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
120 if (BI == 0) {
121 V->convertInitializerTo(new BitsRecTy(BitList->size()));
122 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000123 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000124 }
125
126 // We should have a BitsInit type now...
127 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
128 BitsInit *BInit = (BitsInit*)BI;
129
130 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
131
Chris Lattnerade0de92002-12-06 03:55:39 +0000132 // Loop over bits, assigning values as appropriate...
Chris Lattnere62c1182002-12-02 01:23:04 +0000133 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
134 unsigned Bit = (*BitList)[i];
Chris Lattner28c2d402002-12-06 04:42:16 +0000135 if (NewVal->getBit(Bit)) {
136 err() << "Cannot set bit #" << Bit << " of value '" << ValName
Chris Lattnerade0de92002-12-06 03:55:39 +0000137 << "' more than once!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000138 exit(1);
Chris Lattnerade0de92002-12-06 03:55:39 +0000139 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000140 NewVal->setBit(Bit, BInit->getBit(i));
141 }
Chris Lattnerade0de92002-12-06 03:55:39 +0000142
143 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
144 if (NewVal->getBit(i) == 0)
145 NewVal->setBit(i, CurVal->getBit(i));
146
Chris Lattnere62c1182002-12-02 01:23:04 +0000147 V = NewVal;
148 }
149
150 if (RV->setValue(V)) {
151 err() << "Value '" << ValName << "' of type '" << *RV->getType()
152 << "' is incompatible with initializer '" << *V << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000153 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000154 }
155}
156
Chris Lattner7dda3952005-04-19 03:36:21 +0000157// addSubClass - Add SC as a subclass to CurRec, resolving TemplateArgs as SC's
158// template arguments.
Chris Lattnere62c1182002-12-02 01:23:04 +0000159static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
160 // Add all of the values in the subclass into the current class...
161 const std::vector<RecordVal> &Vals = SC->getValues();
162 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
163 addValue(Vals[i]);
164
165 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
166
167 // Ensure that an appropriate number of template arguments are specified...
168 if (TArgs.size() < TemplateArgs.size()) {
Misha Brukmane3d333e2003-05-20 23:45:36 +0000169 err() << "ERROR: More template args specified than expected!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000170 exit(1);
Chris Lattner12069862006-09-01 21:13:49 +0000171 }
172
173 // Loop over all of the template arguments, setting them to the specified
174 // value or leaving them as the default if necessary.
175 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
176 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
177 // Set it now.
178 setValue(TArgs[i], 0, TemplateArgs[i]);
Chris Lattner7dda3952005-04-19 03:36:21 +0000179
Chris Lattner12069862006-09-01 21:13:49 +0000180 // Resolve it next.
181 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
182
183
184 // Now remove it.
185 CurRec->removeValue(TArgs[i]);
Chris Lattner7dda3952005-04-19 03:36:21 +0000186
Chris Lattner12069862006-09-01 21:13:49 +0000187 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
188 err() << "ERROR: Value not specified for template argument #"
189 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
190 << "'!\n";
191 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000192 }
193 }
194
Chris Lattnere62c1182002-12-02 01:23:04 +0000195 // Since everything went well, we can now set the "superclass" list for the
196 // current record.
Chris Lattner12069862006-09-01 21:13:49 +0000197 const std::vector<Record*> &SCs = SC->getSuperClasses();
Chris Lattnere62c1182002-12-02 01:23:04 +0000198 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
199 addSuperClass(SCs[i]);
200 addSuperClass(SC);
201}
202
Brian Gaeked0fde302003-11-11 22:41:34 +0000203} // End llvm namespace
204
205using namespace llvm;
Chris Lattnere62c1182002-12-02 01:23:04 +0000206
207%}
208
209%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000210 std::string* StrVal;
211 int IntVal;
212 llvm::RecTy* Ty;
213 llvm::Init* Initializer;
214 std::vector<llvm::Init*>* FieldList;
215 std::vector<unsigned>* BitList;
216 llvm::Record* Rec;
Chris Lattner12069862006-09-01 21:13:49 +0000217 std::vector<llvm::Record*>* RecList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000218 SubClassRefTy* SubClassRef;
219 std::vector<SubClassRefTy>* SubClassList;
220 std::vector<std::pair<llvm::Init*, std::string> >* DagValueList;
Chris Lattnere62c1182002-12-02 01:23:04 +0000221};
222
Chris Lattner12069862006-09-01 21:13:49 +0000223%token INT BIT STRING BITS LIST CODE DAG CLASS DEF MULTICLASS DEFM FIELD LET IN
Chris Lattner711e5d92006-03-31 21:53:49 +0000224%token SHLTOK SRATOK SRLTOK STRCONCATTOK
Chris Lattnere62c1182002-12-02 01:23:04 +0000225%token <IntVal> INTVAL
Chris Lattnerfb9ea582003-08-10 22:04:25 +0000226%token <StrVal> ID VARNAME STRVAL CODEFRAGMENT
Chris Lattnere62c1182002-12-02 01:23:04 +0000227
228%type <Ty> Type
Chris Lattner12069862006-09-01 21:13:49 +0000229%type <Rec> ClassInst DefInst MultiClassDef ObjectBody ClassID
230%type <RecList> MultiClassBody
Chris Lattnere62c1182002-12-02 01:23:04 +0000231
232%type <SubClassRef> SubClassRef
233%type <SubClassList> ClassList ClassListNE
234%type <IntVal> OptPrefix
Chris Lattner8c063182006-03-30 22:50:40 +0000235%type <Initializer> Value OptValue IDValue
Chris Lattnerbc21c342003-08-04 20:44:43 +0000236%type <DagValueList> DagArgList DagArgListNE
Chris Lattnere62c1182002-12-02 01:23:04 +0000237%type <FieldList> ValueList ValueListNE
238%type <BitList> BitList OptBitList RBitList
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000239%type <StrVal> Declaration OptID OptVarName ObjectName
Chris Lattnere62c1182002-12-02 01:23:04 +0000240
241%start File
Brian Gaeked0fde302003-11-11 22:41:34 +0000242
Chris Lattnere62c1182002-12-02 01:23:04 +0000243%%
244
245ClassID : ID {
Chris Lattner12069862006-09-01 21:13:49 +0000246 if (CurDefmPrefix) {
247 // If CurDefmPrefix is set, we're parsing a defm, which means that this is
248 // actually the name of a multiclass.
249 MultiClass *MC = MultiClasses[*$1];
250 if (MC == 0) {
251 err() << "Couldn't find class '" << *$1 << "'!\n";
252 exit(1);
253 }
254 $$ = &MC->Rec;
255 } else {
256 $$ = Records.getClass(*$1);
257 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000258 if ($$ == 0) {
259 err() << "Couldn't find class '" << *$1 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000260 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000261 }
262 delete $1;
263 };
264
Chris Lattnere62c1182002-12-02 01:23:04 +0000265
266// TableGen types...
267Type : STRING { // string type
268 $$ = new StringRecTy();
269 } | BIT { // bit type
270 $$ = new BitRecTy();
271 } | BITS '<' INTVAL '>' { // bits<x> type
272 $$ = new BitsRecTy($3);
273 } | INT { // int type
274 $$ = new IntRecTy();
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000275 } | LIST '<' Type '>' { // list<x> type
Chris Lattnere62c1182002-12-02 01:23:04 +0000276 $$ = new ListRecTy($3);
Chris Lattnerf05760d2003-07-30 21:47:42 +0000277 } | CODE { // code type
278 $$ = new CodeRecTy();
Chris Lattner40f71132003-08-04 04:50:57 +0000279 } | DAG { // dag type
280 $$ = new DagRecTy();
Chris Lattnere62c1182002-12-02 01:23:04 +0000281 } | ClassID { // Record Type
282 $$ = new RecordRecTy($1);
283 };
284
285OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
286
287OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
288
Chris Lattner8c063182006-03-30 22:50:40 +0000289IDValue : ID {
290 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
291 $$ = new VarInit(*$1, RV->getType());
292 } else if (CurRec && CurRec->isTemplateArg(CurRec->getName()+":"+*$1)) {
293 const RecordVal *RV = CurRec->getValue(CurRec->getName()+":"+*$1);
294 assert(RV && "Template arg doesn't exist??");
295 $$ = new VarInit(CurRec->getName()+":"+*$1, RV->getType());
Chris Lattner12069862006-09-01 21:13:49 +0000296 } else if (CurMultiClass &&
297 CurMultiClass->Rec.isTemplateArg(CurMultiClass->Rec.getName()+"::"+*$1)) {
298 std::string Name = CurMultiClass->Rec.getName()+"::"+*$1;
299 const RecordVal *RV = CurMultiClass->Rec.getValue(Name);
300 assert(RV && "Template arg doesn't exist??");
301 $$ = new VarInit(Name, RV->getType());
Chris Lattner8c063182006-03-30 22:50:40 +0000302 } else if (Record *D = Records.getDef(*$1)) {
303 $$ = new DefInit(D);
304 } else {
305 err() << "Variable not defined: '" << *$1 << "'!\n";
306 exit(1);
307 }
308
309 delete $1;
310};
311
312Value : IDValue {
313 $$ = $1;
314 } | INTVAL {
Chris Lattnere62c1182002-12-02 01:23:04 +0000315 $$ = new IntInit($1);
316 } | STRVAL {
317 $$ = new StringInit(*$1);
318 delete $1;
Chris Lattnere3a1d052003-07-30 22:15:58 +0000319 } | CODEFRAGMENT {
320 $$ = new CodeInit(*$1);
321 delete $1;
Chris Lattnere62c1182002-12-02 01:23:04 +0000322 } | '?' {
323 $$ = new UnsetInit();
324 } | '{' ValueList '}' {
325 BitsInit *Init = new BitsInit($2->size());
326 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
327 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
328 if (Bit == 0) {
Chris Lattner69b545e2005-09-08 18:22:35 +0000329 err() << "Element #" << i << " (" << *(*$2)[i]
330 << ") is not convertable to a bit!\n";
331 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000332 }
333 Init->setBit($2->size()-i-1, Bit);
334 }
335 $$ = Init;
336 delete $2;
Chris Lattnera1207a52005-09-08 18:48:23 +0000337 } | ID '<' ValueListNE '>' {
338 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
339 // a new anonymous definition, deriving from CLASS<initvalslist> with no
340 // body.
341 Record *Class = Records.getClass(*$1);
342 if (!Class) {
343 err() << "Expected a class, got '" << *$1 << "'!\n";
344 exit(1);
345 }
346 delete $1;
347
348 static unsigned AnonCounter = 0;
349 Record *OldRec = CurRec; // Save CurRec.
350
351 // Create the new record, set it as CurRec temporarily.
352 CurRec = new Record("anonymous.val."+utostr(AnonCounter++));
353 addSubClass(Class, *$3); // Add info about the subclass to CurRec.
354 delete $3; // Free up the template args.
355
356 CurRec->resolveReferences();
357
358 Records.addDef(CurRec);
359
360 // The result of the expression is a reference to the new record.
361 $$ = new DefInit(CurRec);
362
363 // Restore the old CurRec
364 CurRec = OldRec;
Chris Lattnere62c1182002-12-02 01:23:04 +0000365 } | Value '{' BitList '}' {
366 $$ = $1->convertInitializerBitRange(*$3);
367 if ($$ == 0) {
368 err() << "Invalid bit range for value '" << *$1 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000369 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000370 }
371 delete $3;
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000372 } | '[' ValueList ']' {
Chris Lattnere62c1182002-12-02 01:23:04 +0000373 $$ = new ListInit(*$2);
374 delete $2;
Chris Lattner34a77692002-12-02 16:43:43 +0000375 } | Value '.' ID {
376 if (!$1->getFieldType(*$3)) {
377 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000378 exit(1);
Chris Lattner34a77692002-12-02 16:43:43 +0000379 }
380 $$ = new FieldInit($1, *$3);
381 delete $3;
Chris Lattner8c063182006-03-30 22:50:40 +0000382 } | '(' IDValue DagArgList ')' {
383 $$ = new DagInit($2, *$3);
384 delete $3;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000385 } | Value '[' BitList ']' {
386 std::reverse($3->begin(), $3->end());
387 $$ = $1->convertInitListSlice(*$3);
388 if ($$ == 0) {
389 err() << "Invalid list slice for value '" << *$1 << "'!\n";
390 exit(1);
391 }
392 delete $3;
Chris Lattnerb9266f82005-04-19 01:11:03 +0000393 } | SHLTOK '(' Value ',' Value ')' {
Chris Lattner711e5d92006-03-31 21:53:49 +0000394 $$ = (new BinOpInit(BinOpInit::SHL, $3, $5))->Fold();
Chris Lattnerb9266f82005-04-19 01:11:03 +0000395 } | SRATOK '(' Value ',' Value ')' {
Chris Lattner711e5d92006-03-31 21:53:49 +0000396 $$ = (new BinOpInit(BinOpInit::SRA, $3, $5))->Fold();
Chris Lattnerb9266f82005-04-19 01:11:03 +0000397 } | SRLTOK '(' Value ',' Value ')' {
Chris Lattner711e5d92006-03-31 21:53:49 +0000398 $$ = (new BinOpInit(BinOpInit::SRL, $3, $5))->Fold();
399 } | STRCONCATTOK '(' Value ',' Value ')' {
400 $$ = (new BinOpInit(BinOpInit::STRCONCAT, $3, $5))->Fold();
Chris Lattnere62c1182002-12-02 01:23:04 +0000401 };
402
Chris Lattner91290d72003-08-10 22:14:13 +0000403OptVarName : /* empty */ {
404 $$ = new std::string();
Chris Lattnerbc21c342003-08-04 20:44:43 +0000405 }
Chris Lattner91290d72003-08-10 22:14:13 +0000406 | ':' VARNAME {
407 $$ = $2;
408 };
409
410DagArgListNE : Value OptVarName {
411 $$ = new std::vector<std::pair<Init*, std::string> >();
412 $$->push_back(std::make_pair($1, *$2));
413 delete $2;
414 }
415 | DagArgListNE ',' Value OptVarName {
416 $1->push_back(std::make_pair($3, *$4));
417 delete $4;
418 $$ = $1;
Chris Lattnerbc21c342003-08-04 20:44:43 +0000419 };
420
421DagArgList : /*empty*/ {
Chris Lattner91290d72003-08-10 22:14:13 +0000422 $$ = new std::vector<std::pair<Init*, std::string> >();
Chris Lattnerbc21c342003-08-04 20:44:43 +0000423 }
424 | DagArgListNE { $$ = $1; };
425
426
Chris Lattnere62c1182002-12-02 01:23:04 +0000427RBitList : INTVAL {
428 $$ = new std::vector<unsigned>();
429 $$->push_back($1);
430 } | INTVAL '-' INTVAL {
Chris Lattnerb0fef642004-07-26 23:21:34 +0000431 if ($1 < 0 || $3 < 0) {
432 err() << "Invalid range: " << $1 << "-" << $3 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000433 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000434 }
435 $$ = new std::vector<unsigned>();
Chris Lattnerb0fef642004-07-26 23:21:34 +0000436 if ($1 < $3) {
437 for (int i = $1; i <= $3; ++i)
438 $$->push_back(i);
439 } else {
440 for (int i = $1; i >= $3; --i)
441 $$->push_back(i);
442 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000443 } | INTVAL INTVAL {
444 $2 = -$2;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000445 if ($1 < 0 || $2 < 0) {
446 err() << "Invalid range: " << $1 << "-" << $2 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000447 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000448 }
449 $$ = new std::vector<unsigned>();
Chris Lattnerb0fef642004-07-26 23:21:34 +0000450 if ($1 < $2) {
451 for (int i = $1; i <= $2; ++i)
452 $$->push_back(i);
453 } else {
454 for (int i = $1; i >= $2; --i)
455 $$->push_back(i);
456 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000457 } | RBitList ',' INTVAL {
458 ($$=$1)->push_back($3);
459 } | RBitList ',' INTVAL '-' INTVAL {
Chris Lattnerb0fef642004-07-26 23:21:34 +0000460 if ($3 < 0 || $5 < 0) {
461 err() << "Invalid range: " << $3 << "-" << $5 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000462 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000463 }
464 $$ = $1;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000465 if ($3 < $5) {
466 for (int i = $3; i <= $5; ++i)
467 $$->push_back(i);
468 } else {
469 for (int i = $3; i >= $5; --i)
470 $$->push_back(i);
471 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000472 } | RBitList ',' INTVAL INTVAL {
473 $4 = -$4;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000474 if ($3 < 0 || $4 < 0) {
475 err() << "Invalid range: " << $3 << "-" << $4 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000476 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000477 }
478 $$ = $1;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000479 if ($3 < $4) {
480 for (int i = $3; i <= $4; ++i)
481 $$->push_back(i);
482 } else {
483 for (int i = $3; i >= $4; --i)
484 $$->push_back(i);
485 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000486 };
487
488BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
489
490OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
491
492
493
494ValueList : /*empty*/ {
495 $$ = new std::vector<Init*>();
496 } | ValueListNE {
497 $$ = $1;
498 };
499
500ValueListNE : Value {
501 $$ = new std::vector<Init*>();
502 $$->push_back($1);
503 } | ValueListNE ',' Value {
504 ($$ = $1)->push_back($3);
505 };
506
507Declaration : OptPrefix Type ID OptValue {
Chris Lattner7dda3952005-04-19 03:36:21 +0000508 std::string DecName = *$3;
Chris Lattner12069862006-09-01 21:13:49 +0000509 if (ParsingTemplateArgs) {
510 if (CurRec) {
511 DecName = CurRec->getName() + ":" + DecName;
512 } else {
513 assert(CurMultiClass);
514 }
515 if (CurMultiClass)
516 DecName = CurMultiClass->Rec.getName() + "::" + DecName;
517 }
Chris Lattner7dda3952005-04-19 03:36:21 +0000518
519 addValue(RecordVal(DecName, $2, $1));
520 setValue(DecName, 0, $4);
521 $$ = new std::string(DecName);
Chris Lattnere62c1182002-12-02 01:23:04 +0000522};
523
524BodyItem : Declaration ';' {
525 delete $1;
Chris Lattner42aa89e2003-08-04 04:56:53 +0000526} | LET ID OptBitList '=' Value ';' {
Chris Lattnere62c1182002-12-02 01:23:04 +0000527 setValue(*$2, $3, $5);
528 delete $2;
529 delete $3;
530};
531
532BodyList : /*empty*/ | BodyList BodyItem;
533Body : ';' | '{' BodyList '}';
534
535SubClassRef : ClassID {
536 $$ = new SubClassRefTy($1, new std::vector<Init*>());
537 } | ClassID '<' ValueListNE '>' {
538 $$ = new SubClassRefTy($1, $3);
539 };
540
541ClassListNE : SubClassRef {
542 $$ = new std::vector<SubClassRefTy>();
543 $$->push_back(*$1);
544 delete $1;
545 }
546 | ClassListNE ',' SubClassRef {
547 ($$=$1)->push_back(*$3);
548 delete $3;
549 };
550
551ClassList : /*empty */ {
552 $$ = new std::vector<SubClassRefTy>();
553 }
554 | ':' ClassListNE {
555 $$ = $2;
556 };
557
558DeclListNE : Declaration {
Chris Lattner12069862006-09-01 21:13:49 +0000559 getActiveRec()->addTemplateArg(*$1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000560 delete $1;
561} | DeclListNE ',' Declaration {
Chris Lattner12069862006-09-01 21:13:49 +0000562 getActiveRec()->addTemplateArg(*$3);
Chris Lattnere62c1182002-12-02 01:23:04 +0000563 delete $3;
564};
565
566TemplateArgList : '<' DeclListNE '>' {};
567OptTemplateArgList : /*empty*/ | TemplateArgList;
568
Chris Lattnerfc06bf02003-07-30 04:26:44 +0000569OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
570
Chris Lattnerbe88b502005-09-30 04:10:49 +0000571ObjectName : OptID {
572 static unsigned AnonCounter = 0;
573 if ($1->empty())
574 *$1 = "anonymous."+utostr(AnonCounter++);
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000575 $$ = $1;
Chris Lattnerbe88b502005-09-30 04:10:49 +0000576};
577
578ClassName : ObjectName {
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000579 // If a class of this name already exists, it must be a forward ref.
580 if ((CurRec = Records.getClass(*$1))) {
581 // If the body was previously defined, this is an error.
582 if (!CurRec->getValues().empty() ||
583 !CurRec->getSuperClasses().empty() ||
584 !CurRec->getTemplateArgs().empty()) {
585 err() << "Class '" << CurRec->getName() << "' already defined!\n";
586 exit(1);
587 }
588 } else {
589 // If this is the first reference to this class, create and add it.
590 CurRec = new Record(*$1);
591 Records.addClass(CurRec);
Chris Lattnerbe88b502005-09-30 04:10:49 +0000592 }
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000593 delete $1;
Chris Lattnerbe88b502005-09-30 04:10:49 +0000594};
595
596DefName : ObjectName {
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000597 CurRec = new Record(*$1);
598 delete $1;
599
Chris Lattner12069862006-09-01 21:13:49 +0000600 if (!CurMultiClass) {
601 // Top-level def definition.
602
603 // Ensure redefinition doesn't happen.
604 if (Records.getDef(CurRec->getName())) {
605 err() << "def '" << CurRec->getName() << "' already defined!\n";
606 exit(1);
607 }
608 Records.addDef(CurRec);
609 } else {
610 // Otherwise, a def inside a multiclass, add it to the multiclass.
611 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
612 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
613 err() << "def '" << CurRec->getName()
614 << "' already defined in this multiclass!\n";
615 exit(1);
616 }
617 CurMultiClass->DefPrototypes.push_back(CurRec);
Chris Lattnerbe88b502005-09-30 04:10:49 +0000618 }
Chris Lattnerbe88b502005-09-30 04:10:49 +0000619};
620
Chris Lattner583a0242005-09-30 04:42:31 +0000621ObjectBody : ClassList {
Chris Lattner583a0242005-09-30 04:42:31 +0000622 for (unsigned i = 0, e = $1->size(); i != e; ++i) {
623 addSubClass((*$1)[i].first, *(*$1)[i].second);
Chris Lattnerbfce0562003-07-30 04:56:05 +0000624 // Delete the template arg values for the class
Chris Lattner583a0242005-09-30 04:42:31 +0000625 delete (*$1)[i].second;
Chris Lattnerbfce0562003-07-30 04:56:05 +0000626 }
Chris Lattner1ceb6c82006-10-11 18:12:44 +0000627 delete $1; // Delete the class list.
Chris Lattner583a0242005-09-30 04:42:31 +0000628
Chris Lattner1ceb6c82006-10-11 18:12:44 +0000629 // Process any variables on the let stack.
Chris Lattner69b545e2005-09-08 18:22:35 +0000630 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
Chris Lattner42aa89e2003-08-04 04:56:53 +0000631 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
632 setValue(LetStack[i][j].Name,
633 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
634 LetStack[i][j].Value);
Chris Lattnere62c1182002-12-02 01:23:04 +0000635 } Body {
Chris Lattner7dda3952005-04-19 03:36:21 +0000636 $$ = CurRec;
637 CurRec = 0;
638 };
Chris Lattnere62c1182002-12-02 01:23:04 +0000639
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000640ClassInst : CLASS ClassName {
641 ParsingTemplateArgs = true;
642 } OptTemplateArgList {
643 ParsingTemplateArgs = false;
644 } ObjectBody {
645 $$ = $6;
Chris Lattner583a0242005-09-30 04:42:31 +0000646 };
Chris Lattnere62c1182002-12-02 01:23:04 +0000647
Chris Lattnerbe88b502005-09-30 04:10:49 +0000648DefInst : DEF DefName ObjectBody {
Chris Lattner7717d092006-09-01 21:59:03 +0000649 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
650 $3->resolveReferences();
Chris Lattner7dda3952005-04-19 03:36:21 +0000651
Chris Lattnera1207a52005-09-08 18:48:23 +0000652 // If ObjectBody has template arguments, it's an error.
Chris Lattner583a0242005-09-30 04:42:31 +0000653 assert($3->getTemplateArgs().empty() && "How'd this get template args?");
Chris Lattnerbe88b502005-09-30 04:10:49 +0000654 $$ = $3;
Chris Lattnere62c1182002-12-02 01:23:04 +0000655};
656
Chris Lattner12069862006-09-01 21:13:49 +0000657// MultiClassDef - A def instance specified inside a multiclass.
658MultiClassDef : DefInst {
659 $$ = $1;
660 // Copy the template arguments for the multiclass into the def.
661 const std::vector<std::string> &TArgs = CurMultiClass->Rec.getTemplateArgs();
662
663 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
664 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
665 assert(RV && "Template arg doesn't exist?");
666 $$->addValue(*RV);
667 }
668};
Chris Lattnere62c1182002-12-02 01:23:04 +0000669
Chris Lattner12069862006-09-01 21:13:49 +0000670// MultiClassBody - Sequence of def's that are instantiated when a multiclass is
671// used.
672MultiClassBody : MultiClassDef {
673 $$ = new std::vector<Record*>();
674 $$->push_back($1);
675} | MultiClassBody MultiClassDef {
676 $$->push_back($2);
677};
678
679MultiClassName : ID {
680 MultiClass *&MCE = MultiClasses[*$1];
681 if (MCE) {
682 err() << "multiclass '" << *$1 << "' already defined!\n";
683 exit(1);
684 }
685 MCE = CurMultiClass = new MultiClass(*$1);
686 delete $1;
687};
688
689// MultiClass - Multiple definitions.
690MultiClassInst : MULTICLASS MultiClassName {
691 ParsingTemplateArgs = true;
692 } OptTemplateArgList {
693 ParsingTemplateArgs = false;
694 }'{' MultiClassBody '}' {
695 CurMultiClass = 0;
696};
697
698// DefMInst - Instantiate a multiclass.
699DefMInst : DEFM ID { CurDefmPrefix = $2; } ':' SubClassRef ';' {
700 // To instantiate a multiclass, we need to first get the multiclass, then
701 // instantiate each def contained in the multiclass with the SubClassRef
702 // template parameters.
703 MultiClass *MC = MultiClasses[$5->first->getName()];
704 assert(MC && "Didn't lookup multiclass correctly?");
705 std::vector<Init*> &TemplateVals = *$5->second;
706 delete $5;
707
708 // Verify that the correct number of template arguments were specified.
709 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
710 if (TArgs.size() < TemplateVals.size()) {
711 err() << "ERROR: More template args specified than multiclass expects!\n";
712 exit(1);
713 }
714
715 // Loop over all the def's in the multiclass, instantiating each one.
716 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
717 Record *DefProto = MC->DefPrototypes[i];
718
719 // Add the suffix to the defm name to get the new name.
720 assert(CurRec == 0 && "A def is current?");
721 CurRec = new Record(*$2 + DefProto->getName());
722
723 addSubClass(DefProto, std::vector<Init*>());
724
725 // Loop over all of the template arguments, setting them to the specified
726 // value or leaving them as the default if necessary.
727 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
728 if (i < TemplateVals.size()) { // A value is specified for this temp-arg?
729 // Set it now.
730 setValue(TArgs[i], 0, TemplateVals[i]);
731
732 // Resolve it next.
733 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
734
735 // Now remove it.
736 CurRec->removeValue(TArgs[i]);
737
738 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
739 err() << "ERROR: Value not specified for template argument #"
740 << i << " (" << TArgs[i] << ") of multiclassclass '"
741 << MC->Rec.getName() << "'!\n";
742 exit(1);
743 }
744 }
745
Chris Lattner1ceb6c82006-10-11 18:12:44 +0000746 // If the mdef is inside a 'let' expression, add to each def.
747 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
748 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
749 setValue(LetStack[i][j].Name,
750 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
751 LetStack[i][j].Value);
752
753
Chris Lattner12069862006-09-01 21:13:49 +0000754 // Ensure redefinition doesn't happen.
755 if (Records.getDef(CurRec->getName())) {
756 err() << "def '" << CurRec->getName() << "' already defined, "
757 << "instantiating defm '" << *$2 << "' with subdef '"
758 << DefProto->getName() << "'!\n";
759 exit(1);
760 }
761 Records.addDef(CurRec);
Chris Lattner7717d092006-09-01 21:59:03 +0000762
763 CurRec->resolveReferences();
764
Chris Lattner12069862006-09-01 21:13:49 +0000765 CurRec = 0;
766 }
767
768 delete &TemplateVals;
769 delete $2;
Chris Lattnercce56af2006-09-01 22:07:27 +0000770 CurDefmPrefix = 0;
Chris Lattner12069862006-09-01 21:13:49 +0000771};
772
773Object : ClassInst {} | DefInst {};
774Object : MultiClassInst | DefMInst;
Chris Lattnere62c1182002-12-02 01:23:04 +0000775
Chris Lattner42aa89e2003-08-04 04:56:53 +0000776LETItem : ID OptBitList '=' Value {
777 LetStack.back().push_back(LetRecord(*$1, $2, $4));
Chris Lattner60094252003-08-03 13:58:01 +0000778 delete $1; delete $2;
Chris Lattner2e724542003-07-28 03:49:40 +0000779};
780
Chris Lattner42aa89e2003-08-04 04:56:53 +0000781LETList : LETItem | LETList ',' LETItem;
Chris Lattner60094252003-08-03 13:58:01 +0000782
Chris Lattner42aa89e2003-08-04 04:56:53 +0000783// LETCommand - A 'LET' statement start...
784LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
Chris Lattner60094252003-08-03 13:58:01 +0000785
Chris Lattner2e724542003-07-28 03:49:40 +0000786// Support Set commands wrapping objects... both with and without braces.
Chris Lattner42aa89e2003-08-04 04:56:53 +0000787Object : LETCommand '{' ObjectList '}' {
788 LetStack.pop_back();
Chris Lattner2e724542003-07-28 03:49:40 +0000789 }
Chris Lattner42aa89e2003-08-04 04:56:53 +0000790 | LETCommand Object {
791 LetStack.pop_back();
Chris Lattnere62c1182002-12-02 01:23:04 +0000792 };
793
794ObjectList : Object {} | ObjectList Object {};
795
Chris Lattner12069862006-09-01 21:13:49 +0000796File : ObjectList;
Chris Lattnere62c1182002-12-02 01:23:04 +0000797
798%%
799
800int yyerror(const char *ErrorMsg) {
801 err() << "Error parsing: " << ErrorMsg << "\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000802 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000803}