blob: 431fcfcc624edc72ba0cb31a50e9ea88cf645c0b [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
21static std::vector<std::pair<std::pair<std::string, std::vector<unsigned>*>,
22 Init*> > SetStack;
23
Chris Lattner7dff0532003-07-30 20:56:47 +000024extern std::ostream &err();
Chris Lattnere62c1182002-12-02 01:23:04 +000025
26static void addValue(const RecordVal &RV) {
27 if (CurRec->getValue(RV.getName())) {
28 err() << "Value '" << RV.getName() << "' multiply defined!\n";
29 abort();
30 }
31
32 CurRec->addValue(RV);
33}
34
35static void addSuperClass(Record *SC) {
36 if (CurRec->isSubClassOf(SC)) {
37 err() << "Already subclass of '" << SC->getName() << "'!\n";
38 abort();
39 }
40 CurRec->addSuperClass(SC);
41}
42
43static void setValue(const std::string &ValName,
44 std::vector<unsigned> *BitList, Init *V) {
45 if (!V) return ;
46
47 RecordVal *RV = CurRec->getValue(ValName);
48 if (RV == 0) {
49 err() << "Value '" << ValName << "' unknown!\n";
50 abort();
51 }
52
53 // If we are assigning to a subset of the bits in the value... then we must be
54 // assigning to a field of BitsRecTy, which must have a BitsInit
55 // initializer...
56 //
57 if (BitList) {
58 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
59 if (CurVal == 0) {
60 err() << "Value '" << ValName << "' is not a bits type!\n";
61 abort();
62 }
63
64 // Convert the incoming value to a bits type of the appropriate size...
65 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
66 if (BI == 0) {
67 V->convertInitializerTo(new BitsRecTy(BitList->size()));
68 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
69 abort();
70 }
71
72 // We should have a BitsInit type now...
73 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
74 BitsInit *BInit = (BitsInit*)BI;
75
76 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
77
Chris Lattnerade0de92002-12-06 03:55:39 +000078 // Loop over bits, assigning values as appropriate...
Chris Lattnere62c1182002-12-02 01:23:04 +000079 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
80 unsigned Bit = (*BitList)[i];
Chris Lattner28c2d402002-12-06 04:42:16 +000081 if (NewVal->getBit(Bit)) {
82 err() << "Cannot set bit #" << Bit << " of value '" << ValName
Chris Lattnerade0de92002-12-06 03:55:39 +000083 << "' more than once!\n";
84 abort();
85 }
Chris Lattnere62c1182002-12-02 01:23:04 +000086 NewVal->setBit(Bit, BInit->getBit(i));
87 }
Chris Lattnerade0de92002-12-06 03:55:39 +000088
89 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
90 if (NewVal->getBit(i) == 0)
91 NewVal->setBit(i, CurVal->getBit(i));
92
Chris Lattnere62c1182002-12-02 01:23:04 +000093 V = NewVal;
94 }
95
96 if (RV->setValue(V)) {
97 err() << "Value '" << ValName << "' of type '" << *RV->getType()
98 << "' is incompatible with initializer '" << *V << "'!\n";
99 abort();
100 }
101}
102
103static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
104 // Add all of the values in the subclass into the current class...
105 const std::vector<RecordVal> &Vals = SC->getValues();
106 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
107 addValue(Vals[i]);
108
109 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
110
111 // Ensure that an appropriate number of template arguments are specified...
112 if (TArgs.size() < TemplateArgs.size()) {
Misha Brukmane3d333e2003-05-20 23:45:36 +0000113 err() << "ERROR: More template args specified than expected!\n";
Chris Lattnere62c1182002-12-02 01:23:04 +0000114 abort();
115 } else { // This class expects template arguments...
116 // Loop over all of the template arguments, setting them to the specified
117 // value or leaving them as the default as neccesary.
118 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
119 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
120 // Set it now.
121 setValue(TArgs[i], 0, TemplateArgs[i]);
122 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
123 err() << "ERROR: Value not specified for template argument #"
124 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
125 << "'!\n";
126 abort();
127 }
128 }
129 }
130
131
132 // Since everything went well, we can now set the "superclass" list for the
133 // current record.
134 const std::vector<Record*> &SCs = SC->getSuperClasses();
135 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
136 addSuperClass(SCs[i]);
137 addSuperClass(SC);
138}
139
140
141%}
142
143%union {
144 std::string *StrVal;
145 int IntVal;
146 RecTy *Ty;
147 Init *Initializer;
148 std::vector<Init*> *FieldList;
149 std::vector<Record*> *RecPtr;
150 std::vector<unsigned>*BitList;
151 Record *Rec;
152 SubClassRefTy *SubClassRef;
153 std::vector<SubClassRefTy> *SubClassList;
154};
155
156%token INT BIT STRING BITS LIST CLASS DEF FIELD SET IN
157%token <IntVal> INTVAL
158%token <StrVal> ID STRVAL
159
160%type <Ty> Type
161%type <RecPtr> DefList DefListNE
162%type <Rec> ClassInst DefInst Object ObjectBody ClassID DefID
163
164%type <SubClassRef> SubClassRef
165%type <SubClassList> ClassList ClassListNE
166%type <IntVal> OptPrefix
167%type <Initializer> Value OptValue
168%type <FieldList> ValueList ValueListNE
169%type <BitList> BitList OptBitList RBitList
Chris Lattnerfc06bf02003-07-30 04:26:44 +0000170%type <StrVal> Declaration OptID
Chris Lattnere62c1182002-12-02 01:23:04 +0000171
172%start File
173%%
174
175ClassID : ID {
176 $$ = Records.getClass(*$1);
177 if ($$ == 0) {
178 err() << "Couldn't find class '" << *$1 << "'!\n";
179 abort();
180 }
181 delete $1;
182 };
183
184DefID : ID {
185 $$ = Records.getDef(*$1);
186 if ($$ == 0) {
187 err() << "Couldn't find def '" << *$1 << "'!\n";
188 abort();
189 }
190 delete $1;
191 };
192
193
194// TableGen types...
195Type : STRING { // string type
196 $$ = new StringRecTy();
197 } | BIT { // bit type
198 $$ = new BitRecTy();
199 } | BITS '<' INTVAL '>' { // bits<x> type
200 $$ = new BitsRecTy($3);
201 } | INT { // int type
202 $$ = new IntRecTy();
203 } | LIST '<' ClassID '>' { // list<x> type
204 $$ = new ListRecTy($3);
205 } | ClassID { // Record Type
206 $$ = new RecordRecTy($1);
207 };
208
209OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
210
211OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
212
213Value : INTVAL {
214 $$ = new IntInit($1);
215 } | STRVAL {
216 $$ = new StringInit(*$1);
217 delete $1;
218 } | '?' {
219 $$ = new UnsetInit();
220 } | '{' ValueList '}' {
221 BitsInit *Init = new BitsInit($2->size());
222 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
223 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
224 if (Bit == 0) {
225 err() << "Element #" << i << " (" << *(*$2)[i]
226 << ") is not convertable to a bit!\n";
227 abort();
228 }
229 Init->setBit($2->size()-i-1, Bit);
230 }
231 $$ = Init;
232 delete $2;
233 } | ID {
Chris Lattner18226e02003-07-30 05:17:35 +0000234 if (CurRec == 0) {
235 err() << "Def/Class name '" << *$1 << "' not allowed here!\n";
236 abort();
237 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000238 if (const RecordVal *RV = CurRec->getValue(*$1)) {
239 $$ = new VarInit(*$1, RV->getType());
240 } else if (Record *D = Records.getDef(*$1)) {
241 $$ = new DefInit(D);
242 } else {
243 err() << "Variable not defined: '" << *$1 << "'!\n";
244 abort();
245 }
246
247 delete $1;
248 } | Value '{' BitList '}' {
249 $$ = $1->convertInitializerBitRange(*$3);
250 if ($$ == 0) {
251 err() << "Invalid bit range for value '" << *$1 << "'!\n";
252 abort();
253 }
254 delete $3;
255 } | '[' DefList ']' {
256 $$ = new ListInit(*$2);
257 delete $2;
Chris Lattner34a77692002-12-02 16:43:43 +0000258 } | Value '.' ID {
259 if (!$1->getFieldType(*$3)) {
260 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
261 abort();
262 }
263 $$ = new FieldInit($1, *$3);
264 delete $3;
Chris Lattnere62c1182002-12-02 01:23:04 +0000265 };
266
267DefList : /*empty */ {
268 $$ = new std::vector<Record*>();
269 } | DefListNE {
270 $$ = $1;
271 };
272DefListNE : DefID {
273 $$ = new std::vector<Record*>();
274 $$->push_back($1);
275 } | DefListNE ',' DefID {
276 ($$=$1)->push_back($3);
277 };
278
279
280RBitList : INTVAL {
281 $$ = new std::vector<unsigned>();
282 $$->push_back($1);
283 } | INTVAL '-' INTVAL {
284 if ($1 < $3 || $1 < 0 || $3 < 0) {
285 err() << "Invalid bit range: " << $1 << "-" << $3 << "!\n";
286 abort();
287 }
288 $$ = new std::vector<unsigned>();
289 for (int i = $1; i >= $3; --i)
290 $$->push_back(i);
291 } | INTVAL INTVAL {
292 $2 = -$2;
293 if ($1 < $2 || $1 < 0 || $2 < 0) {
294 err() << "Invalid bit range: " << $1 << "-" << $2 << "!\n";
295 abort();
296 }
297 $$ = new std::vector<unsigned>();
298 for (int i = $1; i >= $2; --i)
299 $$->push_back(i);
300 } | RBitList ',' INTVAL {
301 ($$=$1)->push_back($3);
302 } | RBitList ',' INTVAL '-' INTVAL {
303 if ($3 < $5 || $3 < 0 || $5 < 0) {
304 err() << "Invalid bit range: " << $3 << "-" << $5 << "!\n";
305 abort();
306 }
307 $$ = $1;
308 for (int i = $3; i >= $5; --i)
309 $$->push_back(i);
310 } | RBitList ',' INTVAL INTVAL {
311 $4 = -$4;
312 if ($3 < $4 || $3 < 0 || $4 < 0) {
313 err() << "Invalid bit range: " << $3 << "-" << $4 << "!\n";
314 abort();
315 }
316 $$ = $1;
317 for (int i = $3; i >= $4; --i)
318 $$->push_back(i);
319 };
320
321BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
322
323OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
324
325
326
327ValueList : /*empty*/ {
328 $$ = new std::vector<Init*>();
329 } | ValueListNE {
330 $$ = $1;
331 };
332
333ValueListNE : Value {
334 $$ = new std::vector<Init*>();
335 $$->push_back($1);
336 } | ValueListNE ',' Value {
337 ($$ = $1)->push_back($3);
338 };
339
340Declaration : OptPrefix Type ID OptValue {
341 addValue(RecordVal(*$3, $2, $1));
342 setValue(*$3, 0, $4);
343 $$ = $3;
344};
345
346BodyItem : Declaration ';' {
347 delete $1;
348} | SET ID OptBitList '=' Value ';' {
349 setValue(*$2, $3, $5);
350 delete $2;
351 delete $3;
352};
353
354BodyList : /*empty*/ | BodyList BodyItem;
355Body : ';' | '{' BodyList '}';
356
357SubClassRef : ClassID {
358 $$ = new SubClassRefTy($1, new std::vector<Init*>());
359 } | ClassID '<' ValueListNE '>' {
360 $$ = new SubClassRefTy($1, $3);
361 };
362
363ClassListNE : SubClassRef {
364 $$ = new std::vector<SubClassRefTy>();
365 $$->push_back(*$1);
366 delete $1;
367 }
368 | ClassListNE ',' SubClassRef {
369 ($$=$1)->push_back(*$3);
370 delete $3;
371 };
372
373ClassList : /*empty */ {
374 $$ = new std::vector<SubClassRefTy>();
375 }
376 | ':' ClassListNE {
377 $$ = $2;
378 };
379
380DeclListNE : Declaration {
381 CurRec->addTemplateArg(*$1);
382 delete $1;
383} | DeclListNE ',' Declaration {
384 CurRec->addTemplateArg(*$3);
385 delete $3;
386};
387
388TemplateArgList : '<' DeclListNE '>' {};
389OptTemplateArgList : /*empty*/ | TemplateArgList;
390
Chris Lattnerfc06bf02003-07-30 04:26:44 +0000391OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
392
393ObjectBody : OptID {
394 static unsigned AnonCounter = 0;
395 if ($1->empty())
396 *$1 = "anonymous."+utostr(AnonCounter++);
Chris Lattnere62c1182002-12-02 01:23:04 +0000397 CurRec = new Record(*$1);
398 delete $1;
399 } OptTemplateArgList ClassList {
400 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
401 addSubClass((*$4)[i].first, *(*$4)[i].second);
Chris Lattnerbfce0562003-07-30 04:56:05 +0000402 // Delete the template arg values for the class
403 delete (*$4)[i].second;
404 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000405
406 // Process any variables on the set stack...
407 for (unsigned i = 0, e = SetStack.size(); i != e; ++i)
408 setValue(SetStack[i].first.first, SetStack[i].first.second,
409 SetStack[i].second);
410 } Body {
411 CurRec->resolveReferences();
Chris Lattnerbfce0562003-07-30 04:56:05 +0000412
413 // Now that all of the references have been resolved, we can delete template
414 // arguments for superclasses, so they don't pollute our record, and so that
415 // their names won't conflict with later uses of the name...
416 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
417 Record *SuperClass = (*$4)[i].first;
418 for (unsigned i = 0, e = SuperClass->getTemplateArgs().size(); i != e; ++i)
419 CurRec->removeValue(SuperClass->getTemplateArgs()[i]);
420 }
421 delete $4; // Delete the class list...
422
Chris Lattnere62c1182002-12-02 01:23:04 +0000423 $$ = CurRec;
424 CurRec = 0;
425};
426
427ClassInst : CLASS ObjectBody {
428 if (Records.getClass($2->getName())) {
429 err() << "Class '" << $2->getName() << "' already defined!\n";
430 abort();
431 }
432 Records.addClass($$ = $2);
433};
434
435DefInst : DEF ObjectBody {
Chris Lattner554af5c2003-07-30 04:31:17 +0000436 if (!$2->getTemplateArgs().empty()) {
437 err() << "Def '" << $2->getName()
438 << "' is not permitted to have template arguments!\n";
439 abort();
440 }
441 // If ObjectBody has template arguments, it's an error.
Chris Lattnere62c1182002-12-02 01:23:04 +0000442 if (Records.getDef($2->getName())) {
443 err() << "Def '" << $2->getName() << "' already defined!\n";
444 abort();
445 }
446 Records.addDef($$ = $2);
447};
448
449
450Object : ClassInst | DefInst;
451
Chris Lattner2e724542003-07-28 03:49:40 +0000452// SETCommand - A 'SET' statement start...
453SETCommand : SET ID OptBitList '=' Value IN {
454 SetStack.push_back(std::make_pair(std::make_pair(*$2, $3), $5));
455 delete $2;
456};
457
458// Support Set commands wrapping objects... both with and without braces.
459Object : SETCommand '{' ObjectList '}' {
460 delete SetStack.back().first.second; // Delete OptBitList
461 SetStack.pop_back();
462 }
463 | SETCommand Object {
464 delete SetStack.back().first.second; // Delete OptBitList
465 SetStack.pop_back();
Chris Lattnere62c1182002-12-02 01:23:04 +0000466 };
467
468ObjectList : Object {} | ObjectList Object {};
469
470File : ObjectList {};
471
472%%
473
474int yyerror(const char *ErrorMsg) {
475 err() << "Error parsing: " << ErrorMsg << "\n";
476 abort();
477}