blob: 55938f47ea7e4be7a322132b50fc475ae43326e1 [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 {
Chris Lattner18226e02003-07-30 05:17:35 +0000248 if (CurRec == 0) {
249 err() << "Def/Class name '" << *$1 << "' not allowed here!\n";
250 abort();
251 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000252 if (const RecordVal *RV = CurRec->getValue(*$1)) {
253 $$ = new VarInit(*$1, RV->getType());
254 } else if (Record *D = Records.getDef(*$1)) {
255 $$ = new DefInit(D);
256 } else {
257 err() << "Variable not defined: '" << *$1 << "'!\n";
258 abort();
259 }
260
261 delete $1;
262 } | Value '{' BitList '}' {
263 $$ = $1->convertInitializerBitRange(*$3);
264 if ($$ == 0) {
265 err() << "Invalid bit range for value '" << *$1 << "'!\n";
266 abort();
267 }
268 delete $3;
269 } | '[' DefList ']' {
270 $$ = new ListInit(*$2);
271 delete $2;
Chris Lattner34a77692002-12-02 16:43:43 +0000272 } | Value '.' ID {
273 if (!$1->getFieldType(*$3)) {
274 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
275 abort();
276 }
277 $$ = new FieldInit($1, *$3);
278 delete $3;
Chris Lattnere62c1182002-12-02 01:23:04 +0000279 };
280
281DefList : /*empty */ {
282 $$ = new std::vector<Record*>();
283 } | DefListNE {
284 $$ = $1;
285 };
286DefListNE : DefID {
287 $$ = new std::vector<Record*>();
288 $$->push_back($1);
289 } | DefListNE ',' DefID {
290 ($$=$1)->push_back($3);
291 };
292
293
294RBitList : INTVAL {
295 $$ = new std::vector<unsigned>();
296 $$->push_back($1);
297 } | INTVAL '-' INTVAL {
298 if ($1 < $3 || $1 < 0 || $3 < 0) {
299 err() << "Invalid bit range: " << $1 << "-" << $3 << "!\n";
300 abort();
301 }
302 $$ = new std::vector<unsigned>();
303 for (int i = $1; i >= $3; --i)
304 $$->push_back(i);
305 } | INTVAL INTVAL {
306 $2 = -$2;
307 if ($1 < $2 || $1 < 0 || $2 < 0) {
308 err() << "Invalid bit range: " << $1 << "-" << $2 << "!\n";
309 abort();
310 }
311 $$ = new std::vector<unsigned>();
312 for (int i = $1; i >= $2; --i)
313 $$->push_back(i);
314 } | RBitList ',' INTVAL {
315 ($$=$1)->push_back($3);
316 } | RBitList ',' INTVAL '-' INTVAL {
317 if ($3 < $5 || $3 < 0 || $5 < 0) {
318 err() << "Invalid bit range: " << $3 << "-" << $5 << "!\n";
319 abort();
320 }
321 $$ = $1;
322 for (int i = $3; i >= $5; --i)
323 $$->push_back(i);
324 } | RBitList ',' INTVAL INTVAL {
325 $4 = -$4;
326 if ($3 < $4 || $3 < 0 || $4 < 0) {
327 err() << "Invalid bit range: " << $3 << "-" << $4 << "!\n";
328 abort();
329 }
330 $$ = $1;
331 for (int i = $3; i >= $4; --i)
332 $$->push_back(i);
333 };
334
335BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
336
337OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
338
339
340
341ValueList : /*empty*/ {
342 $$ = new std::vector<Init*>();
343 } | ValueListNE {
344 $$ = $1;
345 };
346
347ValueListNE : Value {
348 $$ = new std::vector<Init*>();
349 $$->push_back($1);
350 } | ValueListNE ',' Value {
351 ($$ = $1)->push_back($3);
352 };
353
354Declaration : OptPrefix Type ID OptValue {
355 addValue(RecordVal(*$3, $2, $1));
356 setValue(*$3, 0, $4);
357 $$ = $3;
358};
359
360BodyItem : Declaration ';' {
361 delete $1;
362} | SET ID OptBitList '=' Value ';' {
363 setValue(*$2, $3, $5);
364 delete $2;
365 delete $3;
366};
367
368BodyList : /*empty*/ | BodyList BodyItem;
369Body : ';' | '{' BodyList '}';
370
371SubClassRef : ClassID {
372 $$ = new SubClassRefTy($1, new std::vector<Init*>());
373 } | ClassID '<' ValueListNE '>' {
374 $$ = new SubClassRefTy($1, $3);
375 };
376
377ClassListNE : SubClassRef {
378 $$ = new std::vector<SubClassRefTy>();
379 $$->push_back(*$1);
380 delete $1;
381 }
382 | ClassListNE ',' SubClassRef {
383 ($$=$1)->push_back(*$3);
384 delete $3;
385 };
386
387ClassList : /*empty */ {
388 $$ = new std::vector<SubClassRefTy>();
389 }
390 | ':' ClassListNE {
391 $$ = $2;
392 };
393
394DeclListNE : Declaration {
395 CurRec->addTemplateArg(*$1);
396 delete $1;
397} | DeclListNE ',' Declaration {
398 CurRec->addTemplateArg(*$3);
399 delete $3;
400};
401
402TemplateArgList : '<' DeclListNE '>' {};
403OptTemplateArgList : /*empty*/ | TemplateArgList;
404
Chris Lattnerfc06bf02003-07-30 04:26:44 +0000405OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
406
407ObjectBody : OptID {
408 static unsigned AnonCounter = 0;
409 if ($1->empty())
410 *$1 = "anonymous."+utostr(AnonCounter++);
Chris Lattnere62c1182002-12-02 01:23:04 +0000411 CurRec = new Record(*$1);
412 delete $1;
413 } OptTemplateArgList ClassList {
414 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
415 addSubClass((*$4)[i].first, *(*$4)[i].second);
Chris Lattnerbfce0562003-07-30 04:56:05 +0000416 // Delete the template arg values for the class
417 delete (*$4)[i].second;
418 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000419
420 // Process any variables on the set stack...
421 for (unsigned i = 0, e = SetStack.size(); i != e; ++i)
422 setValue(SetStack[i].first.first, SetStack[i].first.second,
423 SetStack[i].second);
424 } Body {
425 CurRec->resolveReferences();
Chris Lattnerbfce0562003-07-30 04:56:05 +0000426
427 // Now that all of the references have been resolved, we can delete template
428 // arguments for superclasses, so they don't pollute our record, and so that
429 // their names won't conflict with later uses of the name...
430 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
431 Record *SuperClass = (*$4)[i].first;
432 for (unsigned i = 0, e = SuperClass->getTemplateArgs().size(); i != e; ++i)
433 CurRec->removeValue(SuperClass->getTemplateArgs()[i]);
434 }
435 delete $4; // Delete the class list...
436
Chris Lattnere62c1182002-12-02 01:23:04 +0000437 $$ = CurRec;
438 CurRec = 0;
439};
440
441ClassInst : CLASS ObjectBody {
442 if (Records.getClass($2->getName())) {
443 err() << "Class '" << $2->getName() << "' already defined!\n";
444 abort();
445 }
446 Records.addClass($$ = $2);
447};
448
449DefInst : DEF ObjectBody {
Chris Lattner554af5c2003-07-30 04:31:17 +0000450 if (!$2->getTemplateArgs().empty()) {
451 err() << "Def '" << $2->getName()
452 << "' is not permitted to have template arguments!\n";
453 abort();
454 }
455 // If ObjectBody has template arguments, it's an error.
Chris Lattnere62c1182002-12-02 01:23:04 +0000456 if (Records.getDef($2->getName())) {
457 err() << "Def '" << $2->getName() << "' already defined!\n";
458 abort();
459 }
460 Records.addDef($$ = $2);
461};
462
463
464Object : ClassInst | DefInst;
465
Chris Lattner2e724542003-07-28 03:49:40 +0000466// SETCommand - A 'SET' statement start...
467SETCommand : SET ID OptBitList '=' Value IN {
468 SetStack.push_back(std::make_pair(std::make_pair(*$2, $3), $5));
469 delete $2;
470};
471
472// Support Set commands wrapping objects... both with and without braces.
473Object : SETCommand '{' ObjectList '}' {
474 delete SetStack.back().first.second; // Delete OptBitList
475 SetStack.pop_back();
476 }
477 | SETCommand Object {
478 delete SetStack.back().first.second; // Delete OptBitList
479 SetStack.pop_back();
Chris Lattnere62c1182002-12-02 01:23:04 +0000480 };
481
482ObjectList : Object {} | ObjectList Object {};
483
484File : ObjectList {};
485
486%%
487
488int yyerror(const char *ErrorMsg) {
489 err() << "Error parsing: " << ErrorMsg << "\n";
490 abort();
491}