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