blob: 7144c915c3bbe720f128d03df8ef840cbb3edf37 [file] [log] [blame]
Chris Lattnere62c1182002-12-02 01:23:04 +00001//===-- FileParser.y - Parser for TableGen files ----------------*- C++ -*-===//
2//
3// This file implements the bison parser for Table Generator files...
4//
5//===------------------------------------------------------------------------=//
6
7%{
8#include "Record.h"
Chris Lattnerfc06bf02003-07-30 04:26:44 +00009#include "Support/StringExtras.h"
Chris Lattnere62c1182002-12-02 01:23:04 +000010#include <algorithm>
Chris Lattnerfc06bf02003-07-30 04:26:44 +000011#include <cstdio>
Chris Lattnere62c1182002-12-02 01:23:04 +000012#define YYERROR_VERBOSE 1
13
14int yyerror(const char *ErrorMsg);
15int yylex();
Chris Lattnere62c1182002-12-02 01:23:04 +000016extern int Filelineno;
Chris Lattnere62c1182002-12-02 01:23:04 +000017static Record *CurRec = 0;
18
19typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
20
Chris Lattner60094252003-08-03 13:58:01 +000021struct SetRecord {
22 std::string Name;
23 std::vector<unsigned> Bits;
24 Init *Value;
25 bool HasBits;
26 SetRecord(const std::string &N, std::vector<unsigned> *B, Init *V)
27 : Name(N), Value(V), HasBits(B != 0) {
28 if (HasBits) Bits = *B;
29 }
30};
31
32static std::vector<std::vector<SetRecord> > SetStack;
33
Chris Lattnere62c1182002-12-02 01:23:04 +000034
Chris Lattner7dff0532003-07-30 20:56:47 +000035extern std::ostream &err();
Chris Lattnere62c1182002-12-02 01:23:04 +000036
37static void addValue(const RecordVal &RV) {
Chris Lattner60094252003-08-03 13:58:01 +000038 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
39 // The value already exists in the class, treat this as a set...
40 if (ERV->setValue(RV.getValue())) {
41 err() << "New definition of '" << RV.getName() << "' of type '"
42 << *RV.getType() << "' is incompatible with previous "
43 << "definition of type '" << *ERV->getType() << "'!\n";
44 abort();
45 }
46 } else {
47 CurRec->addValue(RV);
Chris Lattnere62c1182002-12-02 01:23:04 +000048 }
Chris Lattnere62c1182002-12-02 01:23:04 +000049}
50
51static void addSuperClass(Record *SC) {
52 if (CurRec->isSubClassOf(SC)) {
53 err() << "Already subclass of '" << SC->getName() << "'!\n";
54 abort();
55 }
56 CurRec->addSuperClass(SC);
57}
58
59static void setValue(const std::string &ValName,
60 std::vector<unsigned> *BitList, Init *V) {
61 if (!V) return ;
62
63 RecordVal *RV = CurRec->getValue(ValName);
64 if (RV == 0) {
65 err() << "Value '" << ValName << "' unknown!\n";
66 abort();
67 }
68
69 // If we are assigning to a subset of the bits in the value... then we must be
70 // assigning to a field of BitsRecTy, which must have a BitsInit
71 // initializer...
72 //
73 if (BitList) {
74 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
75 if (CurVal == 0) {
76 err() << "Value '" << ValName << "' is not a bits type!\n";
77 abort();
78 }
79
80 // Convert the incoming value to a bits type of the appropriate size...
81 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
82 if (BI == 0) {
83 V->convertInitializerTo(new BitsRecTy(BitList->size()));
84 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
85 abort();
86 }
87
88 // We should have a BitsInit type now...
89 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
90 BitsInit *BInit = (BitsInit*)BI;
91
92 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
93
Chris Lattnerade0de92002-12-06 03:55:39 +000094 // Loop over bits, assigning values as appropriate...
Chris Lattnere62c1182002-12-02 01:23:04 +000095 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
96 unsigned Bit = (*BitList)[i];
Chris Lattner28c2d402002-12-06 04:42:16 +000097 if (NewVal->getBit(Bit)) {
98 err() << "Cannot set bit #" << Bit << " of value '" << ValName
Chris Lattnerade0de92002-12-06 03:55:39 +000099 << "' more than once!\n";
100 abort();
101 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000102 NewVal->setBit(Bit, BInit->getBit(i));
103 }
Chris Lattnerade0de92002-12-06 03:55:39 +0000104
105 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
106 if (NewVal->getBit(i) == 0)
107 NewVal->setBit(i, CurVal->getBit(i));
108
Chris Lattnere62c1182002-12-02 01:23:04 +0000109 V = NewVal;
110 }
111
112 if (RV->setValue(V)) {
113 err() << "Value '" << ValName << "' of type '" << *RV->getType()
114 << "' is incompatible with initializer '" << *V << "'!\n";
115 abort();
116 }
117}
118
119static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
120 // Add all of the values in the subclass into the current class...
121 const std::vector<RecordVal> &Vals = SC->getValues();
122 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
123 addValue(Vals[i]);
124
125 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
126
127 // Ensure that an appropriate number of template arguments are specified...
128 if (TArgs.size() < TemplateArgs.size()) {
Misha Brukmane3d333e2003-05-20 23:45:36 +0000129 err() << "ERROR: More template args specified than expected!\n";
Chris Lattnere62c1182002-12-02 01:23:04 +0000130 abort();
131 } else { // This class expects template arguments...
132 // Loop over all of the template arguments, setting them to the specified
133 // value or leaving them as the default as neccesary.
134 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
135 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
136 // Set it now.
137 setValue(TArgs[i], 0, TemplateArgs[i]);
138 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
139 err() << "ERROR: Value not specified for template argument #"
140 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
141 << "'!\n";
142 abort();
143 }
144 }
145 }
146
147
148 // Since everything went well, we can now set the "superclass" list for the
149 // current record.
150 const std::vector<Record*> &SCs = SC->getSuperClasses();
151 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
152 addSuperClass(SCs[i]);
153 addSuperClass(SC);
154}
155
156
157%}
158
159%union {
160 std::string *StrVal;
161 int IntVal;
162 RecTy *Ty;
163 Init *Initializer;
164 std::vector<Init*> *FieldList;
165 std::vector<Record*> *RecPtr;
166 std::vector<unsigned>*BitList;
167 Record *Rec;
168 SubClassRefTy *SubClassRef;
169 std::vector<SubClassRefTy> *SubClassList;
170};
171
Chris Lattnerf05760d2003-07-30 21:47:42 +0000172%token INT BIT STRING BITS LIST CODE CLASS DEF FIELD SET IN
Chris Lattnere62c1182002-12-02 01:23:04 +0000173%token <IntVal> INTVAL
Chris Lattnere3a1d052003-07-30 22:15:58 +0000174%token <StrVal> ID STRVAL CODEFRAGMENT
Chris Lattnere62c1182002-12-02 01:23:04 +0000175
176%type <Ty> Type
177%type <RecPtr> DefList DefListNE
178%type <Rec> ClassInst DefInst Object ObjectBody ClassID DefID
179
180%type <SubClassRef> SubClassRef
181%type <SubClassList> ClassList ClassListNE
182%type <IntVal> OptPrefix
183%type <Initializer> Value OptValue
184%type <FieldList> ValueList ValueListNE
185%type <BitList> BitList OptBitList RBitList
Chris Lattnerfc06bf02003-07-30 04:26:44 +0000186%type <StrVal> Declaration OptID
Chris Lattnere62c1182002-12-02 01:23:04 +0000187
188%start File
189%%
190
191ClassID : ID {
192 $$ = Records.getClass(*$1);
193 if ($$ == 0) {
194 err() << "Couldn't find class '" << *$1 << "'!\n";
195 abort();
196 }
197 delete $1;
198 };
199
200DefID : ID {
201 $$ = Records.getDef(*$1);
202 if ($$ == 0) {
203 err() << "Couldn't find def '" << *$1 << "'!\n";
204 abort();
205 }
206 delete $1;
207 };
208
209
210// TableGen types...
211Type : STRING { // string type
212 $$ = new StringRecTy();
213 } | BIT { // bit type
214 $$ = new BitRecTy();
215 } | BITS '<' INTVAL '>' { // bits<x> type
216 $$ = new BitsRecTy($3);
217 } | INT { // int type
218 $$ = new IntRecTy();
219 } | LIST '<' ClassID '>' { // list<x> type
220 $$ = new ListRecTy($3);
Chris Lattnerf05760d2003-07-30 21:47:42 +0000221 } | CODE { // code type
222 $$ = new CodeRecTy();
Chris Lattnere62c1182002-12-02 01:23:04 +0000223 } | ClassID { // Record Type
224 $$ = new RecordRecTy($1);
225 };
226
227OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
228
229OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
230
231Value : INTVAL {
232 $$ = new IntInit($1);
233 } | STRVAL {
234 $$ = new StringInit(*$1);
235 delete $1;
Chris Lattnere3a1d052003-07-30 22:15:58 +0000236 } | CODEFRAGMENT {
237 $$ = new CodeInit(*$1);
238 delete $1;
Chris Lattnere62c1182002-12-02 01:23:04 +0000239 } | '?' {
240 $$ = new UnsetInit();
241 } | '{' ValueList '}' {
242 BitsInit *Init = new BitsInit($2->size());
243 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
244 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
245 if (Bit == 0) {
246 err() << "Element #" << i << " (" << *(*$2)[i]
247 << ") is not convertable to a bit!\n";
248 abort();
249 }
250 Init->setBit($2->size()-i-1, Bit);
251 }
252 $$ = Init;
253 delete $2;
254 } | ID {
Chris Lattner18226e02003-07-30 05:17:35 +0000255 if (CurRec == 0) {
256 err() << "Def/Class name '" << *$1 << "' not allowed here!\n";
257 abort();
258 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000259 if (const RecordVal *RV = CurRec->getValue(*$1)) {
260 $$ = new VarInit(*$1, RV->getType());
261 } else if (Record *D = Records.getDef(*$1)) {
262 $$ = new DefInit(D);
263 } else {
264 err() << "Variable not defined: '" << *$1 << "'!\n";
265 abort();
266 }
267
268 delete $1;
269 } | Value '{' BitList '}' {
270 $$ = $1->convertInitializerBitRange(*$3);
271 if ($$ == 0) {
272 err() << "Invalid bit range for value '" << *$1 << "'!\n";
273 abort();
274 }
275 delete $3;
276 } | '[' DefList ']' {
277 $$ = new ListInit(*$2);
278 delete $2;
Chris Lattner34a77692002-12-02 16:43:43 +0000279 } | Value '.' ID {
280 if (!$1->getFieldType(*$3)) {
281 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
282 abort();
283 }
284 $$ = new FieldInit($1, *$3);
285 delete $3;
Chris Lattnere62c1182002-12-02 01:23:04 +0000286 };
287
288DefList : /*empty */ {
289 $$ = new std::vector<Record*>();
290 } | DefListNE {
291 $$ = $1;
292 };
293DefListNE : DefID {
294 $$ = new std::vector<Record*>();
295 $$->push_back($1);
296 } | DefListNE ',' DefID {
297 ($$=$1)->push_back($3);
298 };
299
300
301RBitList : INTVAL {
302 $$ = new std::vector<unsigned>();
303 $$->push_back($1);
304 } | INTVAL '-' INTVAL {
305 if ($1 < $3 || $1 < 0 || $3 < 0) {
306 err() << "Invalid bit range: " << $1 << "-" << $3 << "!\n";
307 abort();
308 }
309 $$ = new std::vector<unsigned>();
310 for (int i = $1; i >= $3; --i)
311 $$->push_back(i);
312 } | INTVAL INTVAL {
313 $2 = -$2;
314 if ($1 < $2 || $1 < 0 || $2 < 0) {
315 err() << "Invalid bit range: " << $1 << "-" << $2 << "!\n";
316 abort();
317 }
318 $$ = new std::vector<unsigned>();
319 for (int i = $1; i >= $2; --i)
320 $$->push_back(i);
321 } | RBitList ',' INTVAL {
322 ($$=$1)->push_back($3);
323 } | RBitList ',' INTVAL '-' INTVAL {
324 if ($3 < $5 || $3 < 0 || $5 < 0) {
325 err() << "Invalid bit range: " << $3 << "-" << $5 << "!\n";
326 abort();
327 }
328 $$ = $1;
329 for (int i = $3; i >= $5; --i)
330 $$->push_back(i);
331 } | RBitList ',' INTVAL INTVAL {
332 $4 = -$4;
333 if ($3 < $4 || $3 < 0 || $4 < 0) {
334 err() << "Invalid bit range: " << $3 << "-" << $4 << "!\n";
335 abort();
336 }
337 $$ = $1;
338 for (int i = $3; i >= $4; --i)
339 $$->push_back(i);
340 };
341
342BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
343
344OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
345
346
347
348ValueList : /*empty*/ {
349 $$ = new std::vector<Init*>();
350 } | ValueListNE {
351 $$ = $1;
352 };
353
354ValueListNE : Value {
355 $$ = new std::vector<Init*>();
356 $$->push_back($1);
357 } | ValueListNE ',' Value {
358 ($$ = $1)->push_back($3);
359 };
360
361Declaration : OptPrefix Type ID OptValue {
362 addValue(RecordVal(*$3, $2, $1));
363 setValue(*$3, 0, $4);
364 $$ = $3;
365};
366
367BodyItem : Declaration ';' {
368 delete $1;
369} | SET ID OptBitList '=' Value ';' {
370 setValue(*$2, $3, $5);
371 delete $2;
372 delete $3;
373};
374
375BodyList : /*empty*/ | BodyList BodyItem;
376Body : ';' | '{' BodyList '}';
377
378SubClassRef : ClassID {
379 $$ = new SubClassRefTy($1, new std::vector<Init*>());
380 } | ClassID '<' ValueListNE '>' {
381 $$ = new SubClassRefTy($1, $3);
382 };
383
384ClassListNE : SubClassRef {
385 $$ = new std::vector<SubClassRefTy>();
386 $$->push_back(*$1);
387 delete $1;
388 }
389 | ClassListNE ',' SubClassRef {
390 ($$=$1)->push_back(*$3);
391 delete $3;
392 };
393
394ClassList : /*empty */ {
395 $$ = new std::vector<SubClassRefTy>();
396 }
397 | ':' ClassListNE {
398 $$ = $2;
399 };
400
401DeclListNE : Declaration {
402 CurRec->addTemplateArg(*$1);
403 delete $1;
404} | DeclListNE ',' Declaration {
405 CurRec->addTemplateArg(*$3);
406 delete $3;
407};
408
409TemplateArgList : '<' DeclListNE '>' {};
410OptTemplateArgList : /*empty*/ | TemplateArgList;
411
Chris Lattnerfc06bf02003-07-30 04:26:44 +0000412OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
413
414ObjectBody : OptID {
415 static unsigned AnonCounter = 0;
416 if ($1->empty())
417 *$1 = "anonymous."+utostr(AnonCounter++);
Chris Lattnere62c1182002-12-02 01:23:04 +0000418 CurRec = new Record(*$1);
419 delete $1;
420 } OptTemplateArgList ClassList {
421 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
422 addSubClass((*$4)[i].first, *(*$4)[i].second);
Chris Lattnerbfce0562003-07-30 04:56:05 +0000423 // Delete the template arg values for the class
424 delete (*$4)[i].second;
425 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000426
427 // Process any variables on the set stack...
428 for (unsigned i = 0, e = SetStack.size(); i != e; ++i)
Chris Lattner60094252003-08-03 13:58:01 +0000429 for (unsigned j = 0, e = SetStack[i].size(); j != e; ++j)
430 setValue(SetStack[i][j].Name,
431 SetStack[i][j].HasBits ? &SetStack[i][j].Bits : 0,
432 SetStack[i][j].Value);
Chris Lattnere62c1182002-12-02 01:23:04 +0000433 } Body {
434 CurRec->resolveReferences();
Chris Lattnerbfce0562003-07-30 04:56:05 +0000435
436 // Now that all of the references have been resolved, we can delete template
437 // arguments for superclasses, so they don't pollute our record, and so that
438 // their names won't conflict with later uses of the name...
439 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
440 Record *SuperClass = (*$4)[i].first;
441 for (unsigned i = 0, e = SuperClass->getTemplateArgs().size(); i != e; ++i)
442 CurRec->removeValue(SuperClass->getTemplateArgs()[i]);
443 }
444 delete $4; // Delete the class list...
445
Chris Lattnere62c1182002-12-02 01:23:04 +0000446 $$ = CurRec;
447 CurRec = 0;
448};
449
450ClassInst : CLASS ObjectBody {
451 if (Records.getClass($2->getName())) {
452 err() << "Class '" << $2->getName() << "' already defined!\n";
453 abort();
454 }
455 Records.addClass($$ = $2);
456};
457
458DefInst : DEF ObjectBody {
Chris Lattner554af5c2003-07-30 04:31:17 +0000459 if (!$2->getTemplateArgs().empty()) {
460 err() << "Def '" << $2->getName()
461 << "' is not permitted to have template arguments!\n";
462 abort();
463 }
464 // If ObjectBody has template arguments, it's an error.
Chris Lattnere62c1182002-12-02 01:23:04 +0000465 if (Records.getDef($2->getName())) {
466 err() << "Def '" << $2->getName() << "' already defined!\n";
467 abort();
468 }
469 Records.addDef($$ = $2);
470};
471
472
473Object : ClassInst | DefInst;
474
Chris Lattner60094252003-08-03 13:58:01 +0000475SETItem : ID OptBitList '=' Value {
476 SetStack.back().push_back(SetRecord(*$1, $2, $4));
477 delete $1; delete $2;
Chris Lattner2e724542003-07-28 03:49:40 +0000478};
479
Chris Lattner60094252003-08-03 13:58:01 +0000480SETList : SETItem | SETList ',' SETItem;
481
482// SETCommand - A 'SET' statement start...
483SETCommand : SET { SetStack.push_back(std::vector<SetRecord>()); } SETList IN;
484
Chris Lattner2e724542003-07-28 03:49:40 +0000485// Support Set commands wrapping objects... both with and without braces.
486Object : SETCommand '{' ObjectList '}' {
Chris Lattner2e724542003-07-28 03:49:40 +0000487 SetStack.pop_back();
488 }
489 | SETCommand Object {
Chris Lattner2e724542003-07-28 03:49:40 +0000490 SetStack.pop_back();
Chris Lattnere62c1182002-12-02 01:23:04 +0000491 };
492
493ObjectList : Object {} | ObjectList Object {};
494
495File : ObjectList {};
496
497%%
498
499int yyerror(const char *ErrorMsg) {
500 err() << "Error parsing: " << ErrorMsg << "\n";
501 abort();
502}