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