blob: 8ae2ef9ff1b2fbf9fef144e0fcd1385498554e8f [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
93 RecordVal *RV = CurRec->getValue(ValName);
94 if (RV == 0) {
95 err() << "Value '" << ValName << "' unknown!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000096 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +000097 }
Chris Lattner81d50ad2004-02-28 17:31:28 +000098
99 // Do not allow assignments like 'X = X'. This will just cause infinite loops
100 // in the resolution machinery.
101 if (!BitList)
102 if (VarInit *VI = dynamic_cast<VarInit*>(V))
103 if (VI->getName() == ValName)
104 return;
Chris Lattnere62c1182002-12-02 01:23:04 +0000105
106 // If we are assigning to a subset of the bits in the value... then we must be
107 // assigning to a field of BitsRecTy, which must have a BitsInit
108 // initializer...
109 //
110 if (BitList) {
111 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
112 if (CurVal == 0) {
113 err() << "Value '" << ValName << "' is not a bits type!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000114 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000115 }
116
117 // Convert the incoming value to a bits type of the appropriate size...
118 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
119 if (BI == 0) {
120 V->convertInitializerTo(new BitsRecTy(BitList->size()));
121 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000122 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000123 }
124
125 // We should have a BitsInit type now...
126 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
127 BitsInit *BInit = (BitsInit*)BI;
128
129 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
130
Chris Lattnerade0de92002-12-06 03:55:39 +0000131 // Loop over bits, assigning values as appropriate...
Chris Lattnere62c1182002-12-02 01:23:04 +0000132 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
133 unsigned Bit = (*BitList)[i];
Chris Lattner28c2d402002-12-06 04:42:16 +0000134 if (NewVal->getBit(Bit)) {
135 err() << "Cannot set bit #" << Bit << " of value '" << ValName
Chris Lattnerade0de92002-12-06 03:55:39 +0000136 << "' more than once!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000137 exit(1);
Chris Lattnerade0de92002-12-06 03:55:39 +0000138 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000139 NewVal->setBit(Bit, BInit->getBit(i));
140 }
Chris Lattnerade0de92002-12-06 03:55:39 +0000141
142 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
143 if (NewVal->getBit(i) == 0)
144 NewVal->setBit(i, CurVal->getBit(i));
145
Chris Lattnere62c1182002-12-02 01:23:04 +0000146 V = NewVal;
147 }
148
149 if (RV->setValue(V)) {
150 err() << "Value '" << ValName << "' of type '" << *RV->getType()
151 << "' is incompatible with initializer '" << *V << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000152 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000153 }
154}
155
Chris Lattner7dda3952005-04-19 03:36:21 +0000156// addSubClass - Add SC as a subclass to CurRec, resolving TemplateArgs as SC's
157// template arguments.
Chris Lattnere62c1182002-12-02 01:23:04 +0000158static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
159 // Add all of the values in the subclass into the current class...
160 const std::vector<RecordVal> &Vals = SC->getValues();
161 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
162 addValue(Vals[i]);
163
164 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
165
166 // Ensure that an appropriate number of template arguments are specified...
167 if (TArgs.size() < TemplateArgs.size()) {
Misha Brukmane3d333e2003-05-20 23:45:36 +0000168 err() << "ERROR: More template args specified than expected!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000169 exit(1);
Chris Lattner12069862006-09-01 21:13:49 +0000170 }
171
172 // Loop over all of the template arguments, setting them to the specified
173 // value or leaving them as the default if necessary.
174 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
175 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
176 // Set it now.
177 setValue(TArgs[i], 0, TemplateArgs[i]);
Chris Lattner7dda3952005-04-19 03:36:21 +0000178
Chris Lattner12069862006-09-01 21:13:49 +0000179 // Resolve it next.
180 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
181
182
183 // Now remove it.
184 CurRec->removeValue(TArgs[i]);
Chris Lattner7dda3952005-04-19 03:36:21 +0000185
Chris Lattner12069862006-09-01 21:13:49 +0000186 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
187 err() << "ERROR: Value not specified for template argument #"
188 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
189 << "'!\n";
190 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000191 }
192 }
193
Chris Lattnere62c1182002-12-02 01:23:04 +0000194 // Since everything went well, we can now set the "superclass" list for the
195 // current record.
Chris Lattner12069862006-09-01 21:13:49 +0000196 const std::vector<Record*> &SCs = SC->getSuperClasses();
Chris Lattnere62c1182002-12-02 01:23:04 +0000197 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
198 addSuperClass(SCs[i]);
199 addSuperClass(SC);
200}
201
Brian Gaeked0fde302003-11-11 22:41:34 +0000202} // End llvm namespace
203
204using namespace llvm;
Chris Lattnere62c1182002-12-02 01:23:04 +0000205
206%}
207
208%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000209 std::string* StrVal;
210 int IntVal;
211 llvm::RecTy* Ty;
212 llvm::Init* Initializer;
213 std::vector<llvm::Init*>* FieldList;
214 std::vector<unsigned>* BitList;
215 llvm::Record* Rec;
Chris Lattner12069862006-09-01 21:13:49 +0000216 std::vector<llvm::Record*>* RecList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000217 SubClassRefTy* SubClassRef;
218 std::vector<SubClassRefTy>* SubClassList;
219 std::vector<std::pair<llvm::Init*, std::string> >* DagValueList;
Chris Lattnere62c1182002-12-02 01:23:04 +0000220};
221
Chris Lattner12069862006-09-01 21:13:49 +0000222%token INT BIT STRING BITS LIST CODE DAG CLASS DEF MULTICLASS DEFM FIELD LET IN
Chris Lattner711e5d92006-03-31 21:53:49 +0000223%token SHLTOK SRATOK SRLTOK STRCONCATTOK
Chris Lattnere62c1182002-12-02 01:23:04 +0000224%token <IntVal> INTVAL
Chris Lattnerfb9ea582003-08-10 22:04:25 +0000225%token <StrVal> ID VARNAME STRVAL CODEFRAGMENT
Chris Lattnere62c1182002-12-02 01:23:04 +0000226
227%type <Ty> Type
Chris Lattner12069862006-09-01 21:13:49 +0000228%type <Rec> ClassInst DefInst MultiClassDef ObjectBody ClassID
229%type <RecList> MultiClassBody
Chris Lattnere62c1182002-12-02 01:23:04 +0000230
231%type <SubClassRef> SubClassRef
232%type <SubClassList> ClassList ClassListNE
233%type <IntVal> OptPrefix
Chris Lattner8c063182006-03-30 22:50:40 +0000234%type <Initializer> Value OptValue IDValue
Chris Lattnerbc21c342003-08-04 20:44:43 +0000235%type <DagValueList> DagArgList DagArgListNE
Chris Lattnere62c1182002-12-02 01:23:04 +0000236%type <FieldList> ValueList ValueListNE
237%type <BitList> BitList OptBitList RBitList
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000238%type <StrVal> Declaration OptID OptVarName ObjectName
Chris Lattnere62c1182002-12-02 01:23:04 +0000239
240%start File
Brian Gaeked0fde302003-11-11 22:41:34 +0000241
Chris Lattnere62c1182002-12-02 01:23:04 +0000242%%
243
244ClassID : ID {
Chris Lattner12069862006-09-01 21:13:49 +0000245 if (CurDefmPrefix) {
246 // If CurDefmPrefix is set, we're parsing a defm, which means that this is
247 // actually the name of a multiclass.
248 MultiClass *MC = MultiClasses[*$1];
249 if (MC == 0) {
250 err() << "Couldn't find class '" << *$1 << "'!\n";
251 exit(1);
252 }
253 $$ = &MC->Rec;
254 } else {
255 $$ = Records.getClass(*$1);
256 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000257 if ($$ == 0) {
258 err() << "Couldn't find class '" << *$1 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000259 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000260 }
261 delete $1;
262 };
263
Chris Lattnere62c1182002-12-02 01:23:04 +0000264
265// TableGen types...
266Type : STRING { // string type
267 $$ = new StringRecTy();
268 } | BIT { // bit type
269 $$ = new BitRecTy();
270 } | BITS '<' INTVAL '>' { // bits<x> type
271 $$ = new BitsRecTy($3);
272 } | INT { // int type
273 $$ = new IntRecTy();
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000274 } | LIST '<' Type '>' { // list<x> type
Chris Lattnere62c1182002-12-02 01:23:04 +0000275 $$ = new ListRecTy($3);
Chris Lattnerf05760d2003-07-30 21:47:42 +0000276 } | CODE { // code type
277 $$ = new CodeRecTy();
Chris Lattner40f71132003-08-04 04:50:57 +0000278 } | DAG { // dag type
279 $$ = new DagRecTy();
Chris Lattnere62c1182002-12-02 01:23:04 +0000280 } | ClassID { // Record Type
281 $$ = new RecordRecTy($1);
282 };
283
284OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
285
286OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
287
Chris Lattner8c063182006-03-30 22:50:40 +0000288IDValue : ID {
289 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
290 $$ = new VarInit(*$1, RV->getType());
291 } else if (CurRec && CurRec->isTemplateArg(CurRec->getName()+":"+*$1)) {
292 const RecordVal *RV = CurRec->getValue(CurRec->getName()+":"+*$1);
293 assert(RV && "Template arg doesn't exist??");
294 $$ = new VarInit(CurRec->getName()+":"+*$1, RV->getType());
Chris Lattner12069862006-09-01 21:13:49 +0000295 } else if (CurMultiClass &&
296 CurMultiClass->Rec.isTemplateArg(CurMultiClass->Rec.getName()+"::"+*$1)) {
297 std::string Name = CurMultiClass->Rec.getName()+"::"+*$1;
298 const RecordVal *RV = CurMultiClass->Rec.getValue(Name);
299 assert(RV && "Template arg doesn't exist??");
300 $$ = new VarInit(Name, RV->getType());
Chris Lattner8c063182006-03-30 22:50:40 +0000301 } else if (Record *D = Records.getDef(*$1)) {
302 $$ = new DefInit(D);
303 } else {
304 err() << "Variable not defined: '" << *$1 << "'!\n";
305 exit(1);
306 }
307
308 delete $1;
309};
310
311Value : IDValue {
312 $$ = $1;
313 } | INTVAL {
Chris Lattnere62c1182002-12-02 01:23:04 +0000314 $$ = new IntInit($1);
315 } | STRVAL {
316 $$ = new StringInit(*$1);
317 delete $1;
Chris Lattnere3a1d052003-07-30 22:15:58 +0000318 } | CODEFRAGMENT {
319 $$ = new CodeInit(*$1);
320 delete $1;
Chris Lattnere62c1182002-12-02 01:23:04 +0000321 } | '?' {
322 $$ = new UnsetInit();
323 } | '{' ValueList '}' {
324 BitsInit *Init = new BitsInit($2->size());
325 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
326 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
327 if (Bit == 0) {
Chris Lattner69b545e2005-09-08 18:22:35 +0000328 err() << "Element #" << i << " (" << *(*$2)[i]
329 << ") is not convertable to a bit!\n";
330 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000331 }
332 Init->setBit($2->size()-i-1, Bit);
333 }
334 $$ = Init;
335 delete $2;
Chris Lattnera1207a52005-09-08 18:48:23 +0000336 } | ID '<' ValueListNE '>' {
337 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
338 // a new anonymous definition, deriving from CLASS<initvalslist> with no
339 // body.
340 Record *Class = Records.getClass(*$1);
341 if (!Class) {
342 err() << "Expected a class, got '" << *$1 << "'!\n";
343 exit(1);
344 }
345 delete $1;
346
347 static unsigned AnonCounter = 0;
348 Record *OldRec = CurRec; // Save CurRec.
349
350 // Create the new record, set it as CurRec temporarily.
351 CurRec = new Record("anonymous.val."+utostr(AnonCounter++));
352 addSubClass(Class, *$3); // Add info about the subclass to CurRec.
353 delete $3; // Free up the template args.
354
355 CurRec->resolveReferences();
356
357 Records.addDef(CurRec);
358
359 // The result of the expression is a reference to the new record.
360 $$ = new DefInit(CurRec);
361
362 // Restore the old CurRec
363 CurRec = OldRec;
Chris Lattnere62c1182002-12-02 01:23:04 +0000364 } | Value '{' BitList '}' {
365 $$ = $1->convertInitializerBitRange(*$3);
366 if ($$ == 0) {
367 err() << "Invalid bit range for value '" << *$1 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000368 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000369 }
370 delete $3;
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000371 } | '[' ValueList ']' {
Chris Lattnere62c1182002-12-02 01:23:04 +0000372 $$ = new ListInit(*$2);
373 delete $2;
Chris Lattner34a77692002-12-02 16:43:43 +0000374 } | Value '.' ID {
375 if (!$1->getFieldType(*$3)) {
376 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000377 exit(1);
Chris Lattner34a77692002-12-02 16:43:43 +0000378 }
379 $$ = new FieldInit($1, *$3);
380 delete $3;
Chris Lattner8c063182006-03-30 22:50:40 +0000381 } | '(' IDValue DagArgList ')' {
382 $$ = new DagInit($2, *$3);
383 delete $3;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000384 } | Value '[' BitList ']' {
385 std::reverse($3->begin(), $3->end());
386 $$ = $1->convertInitListSlice(*$3);
387 if ($$ == 0) {
388 err() << "Invalid list slice for value '" << *$1 << "'!\n";
389 exit(1);
390 }
391 delete $3;
Chris Lattnerb9266f82005-04-19 01:11:03 +0000392 } | SHLTOK '(' Value ',' Value ')' {
Chris Lattner711e5d92006-03-31 21:53:49 +0000393 $$ = (new BinOpInit(BinOpInit::SHL, $3, $5))->Fold();
Chris Lattnerb9266f82005-04-19 01:11:03 +0000394 } | SRATOK '(' Value ',' Value ')' {
Chris Lattner711e5d92006-03-31 21:53:49 +0000395 $$ = (new BinOpInit(BinOpInit::SRA, $3, $5))->Fold();
Chris Lattnerb9266f82005-04-19 01:11:03 +0000396 } | SRLTOK '(' Value ',' Value ')' {
Chris Lattner711e5d92006-03-31 21:53:49 +0000397 $$ = (new BinOpInit(BinOpInit::SRL, $3, $5))->Fold();
398 } | STRCONCATTOK '(' Value ',' Value ')' {
399 $$ = (new BinOpInit(BinOpInit::STRCONCAT, $3, $5))->Fold();
Chris Lattnere62c1182002-12-02 01:23:04 +0000400 };
401
Chris Lattner91290d72003-08-10 22:14:13 +0000402OptVarName : /* empty */ {
403 $$ = new std::string();
Chris Lattnerbc21c342003-08-04 20:44:43 +0000404 }
Chris Lattner91290d72003-08-10 22:14:13 +0000405 | ':' VARNAME {
406 $$ = $2;
407 };
408
409DagArgListNE : Value OptVarName {
410 $$ = new std::vector<std::pair<Init*, std::string> >();
411 $$->push_back(std::make_pair($1, *$2));
412 delete $2;
413 }
414 | DagArgListNE ',' Value OptVarName {
415 $1->push_back(std::make_pair($3, *$4));
416 delete $4;
417 $$ = $1;
Chris Lattnerbc21c342003-08-04 20:44:43 +0000418 };
419
420DagArgList : /*empty*/ {
Chris Lattner91290d72003-08-10 22:14:13 +0000421 $$ = new std::vector<std::pair<Init*, std::string> >();
Chris Lattnerbc21c342003-08-04 20:44:43 +0000422 }
423 | DagArgListNE { $$ = $1; };
424
425
Chris Lattnere62c1182002-12-02 01:23:04 +0000426RBitList : INTVAL {
427 $$ = new std::vector<unsigned>();
428 $$->push_back($1);
429 } | INTVAL '-' INTVAL {
Chris Lattnerb0fef642004-07-26 23:21:34 +0000430 if ($1 < 0 || $3 < 0) {
431 err() << "Invalid range: " << $1 << "-" << $3 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000432 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000433 }
434 $$ = new std::vector<unsigned>();
Chris Lattnerb0fef642004-07-26 23:21:34 +0000435 if ($1 < $3) {
436 for (int i = $1; i <= $3; ++i)
437 $$->push_back(i);
438 } else {
439 for (int i = $1; i >= $3; --i)
440 $$->push_back(i);
441 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000442 } | INTVAL INTVAL {
443 $2 = -$2;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000444 if ($1 < 0 || $2 < 0) {
445 err() << "Invalid range: " << $1 << "-" << $2 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000446 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000447 }
448 $$ = new std::vector<unsigned>();
Chris Lattnerb0fef642004-07-26 23:21:34 +0000449 if ($1 < $2) {
450 for (int i = $1; i <= $2; ++i)
451 $$->push_back(i);
452 } else {
453 for (int i = $1; i >= $2; --i)
454 $$->push_back(i);
455 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000456 } | RBitList ',' INTVAL {
457 ($$=$1)->push_back($3);
458 } | RBitList ',' INTVAL '-' INTVAL {
Chris Lattnerb0fef642004-07-26 23:21:34 +0000459 if ($3 < 0 || $5 < 0) {
460 err() << "Invalid range: " << $3 << "-" << $5 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000461 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000462 }
463 $$ = $1;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000464 if ($3 < $5) {
465 for (int i = $3; i <= $5; ++i)
466 $$->push_back(i);
467 } else {
468 for (int i = $3; i >= $5; --i)
469 $$->push_back(i);
470 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000471 } | RBitList ',' INTVAL INTVAL {
472 $4 = -$4;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000473 if ($3 < 0 || $4 < 0) {
474 err() << "Invalid range: " << $3 << "-" << $4 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000475 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000476 }
477 $$ = $1;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000478 if ($3 < $4) {
479 for (int i = $3; i <= $4; ++i)
480 $$->push_back(i);
481 } else {
482 for (int i = $3; i >= $4; --i)
483 $$->push_back(i);
484 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000485 };
486
487BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
488
489OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
490
491
492
493ValueList : /*empty*/ {
494 $$ = new std::vector<Init*>();
495 } | ValueListNE {
496 $$ = $1;
497 };
498
499ValueListNE : Value {
500 $$ = new std::vector<Init*>();
501 $$->push_back($1);
502 } | ValueListNE ',' Value {
503 ($$ = $1)->push_back($3);
504 };
505
506Declaration : OptPrefix Type ID OptValue {
Chris Lattner7dda3952005-04-19 03:36:21 +0000507 std::string DecName = *$3;
Chris Lattner12069862006-09-01 21:13:49 +0000508 if (ParsingTemplateArgs) {
509 if (CurRec) {
510 DecName = CurRec->getName() + ":" + DecName;
511 } else {
512 assert(CurMultiClass);
513 }
514 if (CurMultiClass)
515 DecName = CurMultiClass->Rec.getName() + "::" + DecName;
516 }
Chris Lattner7dda3952005-04-19 03:36:21 +0000517
518 addValue(RecordVal(DecName, $2, $1));
519 setValue(DecName, 0, $4);
520 $$ = new std::string(DecName);
Chris Lattnere62c1182002-12-02 01:23:04 +0000521};
522
523BodyItem : Declaration ';' {
524 delete $1;
Chris Lattner42aa89e2003-08-04 04:56:53 +0000525} | LET ID OptBitList '=' Value ';' {
Chris Lattnere62c1182002-12-02 01:23:04 +0000526 setValue(*$2, $3, $5);
527 delete $2;
528 delete $3;
529};
530
531BodyList : /*empty*/ | BodyList BodyItem;
532Body : ';' | '{' BodyList '}';
533
534SubClassRef : ClassID {
535 $$ = new SubClassRefTy($1, new std::vector<Init*>());
536 } | ClassID '<' ValueListNE '>' {
537 $$ = new SubClassRefTy($1, $3);
538 };
539
540ClassListNE : SubClassRef {
541 $$ = new std::vector<SubClassRefTy>();
542 $$->push_back(*$1);
543 delete $1;
544 }
545 | ClassListNE ',' SubClassRef {
546 ($$=$1)->push_back(*$3);
547 delete $3;
548 };
549
550ClassList : /*empty */ {
551 $$ = new std::vector<SubClassRefTy>();
552 }
553 | ':' ClassListNE {
554 $$ = $2;
555 };
556
557DeclListNE : Declaration {
Chris Lattner12069862006-09-01 21:13:49 +0000558 getActiveRec()->addTemplateArg(*$1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000559 delete $1;
560} | DeclListNE ',' Declaration {
Chris Lattner12069862006-09-01 21:13:49 +0000561 getActiveRec()->addTemplateArg(*$3);
Chris Lattnere62c1182002-12-02 01:23:04 +0000562 delete $3;
563};
564
565TemplateArgList : '<' DeclListNE '>' {};
566OptTemplateArgList : /*empty*/ | TemplateArgList;
567
Chris Lattnerfc06bf02003-07-30 04:26:44 +0000568OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
569
Chris Lattnerbe88b502005-09-30 04:10:49 +0000570ObjectName : OptID {
571 static unsigned AnonCounter = 0;
572 if ($1->empty())
573 *$1 = "anonymous."+utostr(AnonCounter++);
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000574 $$ = $1;
Chris Lattnerbe88b502005-09-30 04:10:49 +0000575};
576
577ClassName : ObjectName {
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000578 // If a class of this name already exists, it must be a forward ref.
579 if ((CurRec = Records.getClass(*$1))) {
580 // If the body was previously defined, this is an error.
581 if (!CurRec->getValues().empty() ||
582 !CurRec->getSuperClasses().empty() ||
583 !CurRec->getTemplateArgs().empty()) {
584 err() << "Class '" << CurRec->getName() << "' already defined!\n";
585 exit(1);
586 }
587 } else {
588 // If this is the first reference to this class, create and add it.
589 CurRec = new Record(*$1);
590 Records.addClass(CurRec);
Chris Lattnerbe88b502005-09-30 04:10:49 +0000591 }
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000592 delete $1;
Chris Lattnerbe88b502005-09-30 04:10:49 +0000593};
594
595DefName : ObjectName {
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000596 CurRec = new Record(*$1);
597 delete $1;
598
Chris Lattner12069862006-09-01 21:13:49 +0000599 if (!CurMultiClass) {
600 // Top-level def definition.
601
602 // Ensure redefinition doesn't happen.
603 if (Records.getDef(CurRec->getName())) {
604 err() << "def '" << CurRec->getName() << "' already defined!\n";
605 exit(1);
606 }
607 Records.addDef(CurRec);
608 } else {
609 // Otherwise, a def inside a multiclass, add it to the multiclass.
610 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
611 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
612 err() << "def '" << CurRec->getName()
613 << "' already defined in this multiclass!\n";
614 exit(1);
615 }
616 CurMultiClass->DefPrototypes.push_back(CurRec);
Chris Lattnerbe88b502005-09-30 04:10:49 +0000617 }
Chris Lattnerbe88b502005-09-30 04:10:49 +0000618};
619
Chris Lattner583a0242005-09-30 04:42:31 +0000620ObjectBody : ClassList {
Chris Lattner583a0242005-09-30 04:42:31 +0000621 for (unsigned i = 0, e = $1->size(); i != e; ++i) {
622 addSubClass((*$1)[i].first, *(*$1)[i].second);
Chris Lattnerbfce0562003-07-30 04:56:05 +0000623 // Delete the template arg values for the class
Chris Lattner583a0242005-09-30 04:42:31 +0000624 delete (*$1)[i].second;
Chris Lattnerbfce0562003-07-30 04:56:05 +0000625 }
Chris Lattner583a0242005-09-30 04:42:31 +0000626 delete $1; // Delete the class list...
627
Chris Lattner69b545e2005-09-08 18:22:35 +0000628 // Process any variables on the set stack...
629 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
Chris Lattner42aa89e2003-08-04 04:56:53 +0000630 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
631 setValue(LetStack[i][j].Name,
632 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
633 LetStack[i][j].Value);
Chris Lattnere62c1182002-12-02 01:23:04 +0000634 } Body {
Chris Lattner7dda3952005-04-19 03:36:21 +0000635 $$ = CurRec;
636 CurRec = 0;
637 };
Chris Lattnere62c1182002-12-02 01:23:04 +0000638
Chris Lattnerc3bec0e2005-09-30 04:53:04 +0000639ClassInst : CLASS ClassName {
640 ParsingTemplateArgs = true;
641 } OptTemplateArgList {
642 ParsingTemplateArgs = false;
643 } ObjectBody {
644 $$ = $6;
Chris Lattner583a0242005-09-30 04:42:31 +0000645 };
Chris Lattnere62c1182002-12-02 01:23:04 +0000646
Chris Lattnerbe88b502005-09-30 04:10:49 +0000647DefInst : DEF DefName ObjectBody {
648 $3->resolveReferences();
Chris Lattner7dda3952005-04-19 03:36:21 +0000649
Chris Lattnera1207a52005-09-08 18:48:23 +0000650 // If ObjectBody has template arguments, it's an error.
Chris Lattner583a0242005-09-30 04:42:31 +0000651 assert($3->getTemplateArgs().empty() && "How'd this get template args?");
Chris Lattnerbe88b502005-09-30 04:10:49 +0000652 $$ = $3;
Chris Lattnere62c1182002-12-02 01:23:04 +0000653};
654
Chris Lattner12069862006-09-01 21:13:49 +0000655// MultiClassDef - A def instance specified inside a multiclass.
656MultiClassDef : DefInst {
657 $$ = $1;
658 // Copy the template arguments for the multiclass into the def.
659 const std::vector<std::string> &TArgs = CurMultiClass->Rec.getTemplateArgs();
660
661 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
662 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
663 assert(RV && "Template arg doesn't exist?");
664 $$->addValue(*RV);
665 }
666};
Chris Lattnere62c1182002-12-02 01:23:04 +0000667
Chris Lattner12069862006-09-01 21:13:49 +0000668// MultiClassBody - Sequence of def's that are instantiated when a multiclass is
669// used.
670MultiClassBody : MultiClassDef {
671 $$ = new std::vector<Record*>();
672 $$->push_back($1);
673} | MultiClassBody MultiClassDef {
674 $$->push_back($2);
675};
676
677MultiClassName : ID {
678 MultiClass *&MCE = MultiClasses[*$1];
679 if (MCE) {
680 err() << "multiclass '" << *$1 << "' already defined!\n";
681 exit(1);
682 }
683 MCE = CurMultiClass = new MultiClass(*$1);
684 delete $1;
685};
686
687// MultiClass - Multiple definitions.
688MultiClassInst : MULTICLASS MultiClassName {
689 ParsingTemplateArgs = true;
690 } OptTemplateArgList {
691 ParsingTemplateArgs = false;
692 }'{' MultiClassBody '}' {
693 CurMultiClass = 0;
694};
695
696// DefMInst - Instantiate a multiclass.
697DefMInst : DEFM ID { CurDefmPrefix = $2; } ':' SubClassRef ';' {
698 // To instantiate a multiclass, we need to first get the multiclass, then
699 // instantiate each def contained in the multiclass with the SubClassRef
700 // template parameters.
701 MultiClass *MC = MultiClasses[$5->first->getName()];
702 assert(MC && "Didn't lookup multiclass correctly?");
703 std::vector<Init*> &TemplateVals = *$5->second;
704 delete $5;
705
706 // Verify that the correct number of template arguments were specified.
707 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
708 if (TArgs.size() < TemplateVals.size()) {
709 err() << "ERROR: More template args specified than multiclass expects!\n";
710 exit(1);
711 }
712
713 // Loop over all the def's in the multiclass, instantiating each one.
714 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
715 Record *DefProto = MC->DefPrototypes[i];
716
717 // Add the suffix to the defm name to get the new name.
718 assert(CurRec == 0 && "A def is current?");
719 CurRec = new Record(*$2 + DefProto->getName());
720
721 addSubClass(DefProto, std::vector<Init*>());
722
723 // Loop over all of the template arguments, setting them to the specified
724 // value or leaving them as the default if necessary.
725 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
726 if (i < TemplateVals.size()) { // A value is specified for this temp-arg?
727 // Set it now.
728 setValue(TArgs[i], 0, TemplateVals[i]);
729
730 // Resolve it next.
731 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
732
733 // Now remove it.
734 CurRec->removeValue(TArgs[i]);
735
736 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
737 err() << "ERROR: Value not specified for template argument #"
738 << i << " (" << TArgs[i] << ") of multiclassclass '"
739 << MC->Rec.getName() << "'!\n";
740 exit(1);
741 }
742 }
743
744 // Ensure redefinition doesn't happen.
745 if (Records.getDef(CurRec->getName())) {
746 err() << "def '" << CurRec->getName() << "' already defined, "
747 << "instantiating defm '" << *$2 << "' with subdef '"
748 << DefProto->getName() << "'!\n";
749 exit(1);
750 }
751 Records.addDef(CurRec);
752 CurRec = 0;
753 }
754
755 delete &TemplateVals;
756 delete $2;
757};
758
759Object : ClassInst {} | DefInst {};
760Object : MultiClassInst | DefMInst;
Chris Lattnere62c1182002-12-02 01:23:04 +0000761
Chris Lattner42aa89e2003-08-04 04:56:53 +0000762LETItem : ID OptBitList '=' Value {
763 LetStack.back().push_back(LetRecord(*$1, $2, $4));
Chris Lattner60094252003-08-03 13:58:01 +0000764 delete $1; delete $2;
Chris Lattner2e724542003-07-28 03:49:40 +0000765};
766
Chris Lattner42aa89e2003-08-04 04:56:53 +0000767LETList : LETItem | LETList ',' LETItem;
Chris Lattner60094252003-08-03 13:58:01 +0000768
Chris Lattner42aa89e2003-08-04 04:56:53 +0000769// LETCommand - A 'LET' statement start...
770LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
Chris Lattner60094252003-08-03 13:58:01 +0000771
Chris Lattner2e724542003-07-28 03:49:40 +0000772// Support Set commands wrapping objects... both with and without braces.
Chris Lattner42aa89e2003-08-04 04:56:53 +0000773Object : LETCommand '{' ObjectList '}' {
774 LetStack.pop_back();
Chris Lattner2e724542003-07-28 03:49:40 +0000775 }
Chris Lattner42aa89e2003-08-04 04:56:53 +0000776 | LETCommand Object {
777 LetStack.pop_back();
Chris Lattnere62c1182002-12-02 01:23:04 +0000778 };
779
780ObjectList : Object {} | ObjectList Object {};
781
Chris Lattner12069862006-09-01 21:13:49 +0000782File : ObjectList;
Chris Lattnere62c1182002-12-02 01:23:04 +0000783
784%%
785
786int yyerror(const char *ErrorMsg) {
787 err() << "Error parsing: " << ErrorMsg << "\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000788 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000789}