blob: 0c5b240d3079293c0ebff80b5c8de4fefebf15d3 [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;
Chris Lattnere62c1182002-12-02 01:23:04 +0000165 std::vector<unsigned>*BitList;
166 Record *Rec;
167 SubClassRefTy *SubClassRef;
168 std::vector<SubClassRefTy> *SubClassList;
169};
170
Chris Lattner40f71132003-08-04 04:50:57 +0000171%token INT BIT STRING BITS LIST CODE DAG CLASS DEF FIELD SET IN
Chris Lattnere62c1182002-12-02 01:23:04 +0000172%token <IntVal> INTVAL
Chris Lattnere3a1d052003-07-30 22:15:58 +0000173%token <StrVal> ID STRVAL CODEFRAGMENT
Chris Lattnere62c1182002-12-02 01:23:04 +0000174
175%type <Ty> Type
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000176%type <Rec> ClassInst DefInst Object ObjectBody ClassID
Chris Lattnere62c1182002-12-02 01:23:04 +0000177
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
Chris Lattnere62c1182002-12-02 01:23:04 +0000198
199// TableGen types...
200Type : STRING { // string type
201 $$ = new StringRecTy();
202 } | BIT { // bit type
203 $$ = new BitRecTy();
204 } | BITS '<' INTVAL '>' { // bits<x> type
205 $$ = new BitsRecTy($3);
206 } | INT { // int type
207 $$ = new IntRecTy();
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000208 } | LIST '<' Type '>' { // list<x> type
Chris Lattnere62c1182002-12-02 01:23:04 +0000209 $$ = new ListRecTy($3);
Chris Lattnerf05760d2003-07-30 21:47:42 +0000210 } | CODE { // code type
211 $$ = new CodeRecTy();
Chris Lattner40f71132003-08-04 04:50:57 +0000212 } | DAG { // dag type
213 $$ = new DagRecTy();
Chris Lattnere62c1182002-12-02 01:23:04 +0000214 } | ClassID { // Record Type
215 $$ = new RecordRecTy($1);
216 };
217
218OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
219
220OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
221
222Value : INTVAL {
223 $$ = new IntInit($1);
224 } | STRVAL {
225 $$ = new StringInit(*$1);
226 delete $1;
Chris Lattnere3a1d052003-07-30 22:15:58 +0000227 } | CODEFRAGMENT {
228 $$ = new CodeInit(*$1);
229 delete $1;
Chris Lattnere62c1182002-12-02 01:23:04 +0000230 } | '?' {
231 $$ = new UnsetInit();
232 } | '{' ValueList '}' {
233 BitsInit *Init = new BitsInit($2->size());
234 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
235 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
236 if (Bit == 0) {
237 err() << "Element #" << i << " (" << *(*$2)[i]
238 << ") is not convertable to a bit!\n";
239 abort();
240 }
241 Init->setBit($2->size()-i-1, Bit);
242 }
243 $$ = Init;
244 delete $2;
245 } | ID {
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000246 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
Chris Lattnere62c1182002-12-02 01:23:04 +0000247 $$ = new VarInit(*$1, RV->getType());
248 } else if (Record *D = Records.getDef(*$1)) {
249 $$ = new DefInit(D);
250 } else {
251 err() << "Variable not defined: '" << *$1 << "'!\n";
252 abort();
253 }
254
255 delete $1;
256 } | Value '{' BitList '}' {
257 $$ = $1->convertInitializerBitRange(*$3);
258 if ($$ == 0) {
259 err() << "Invalid bit range for value '" << *$1 << "'!\n";
260 abort();
261 }
262 delete $3;
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000263 } | '[' ValueList ']' {
Chris Lattnere62c1182002-12-02 01:23:04 +0000264 $$ = new ListInit(*$2);
265 delete $2;
Chris Lattner34a77692002-12-02 16:43:43 +0000266 } | Value '.' ID {
267 if (!$1->getFieldType(*$3)) {
268 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
269 abort();
270 }
271 $$ = new FieldInit($1, *$3);
272 delete $3;
Chris Lattnere62c1182002-12-02 01:23:04 +0000273 };
274
Chris Lattnere62c1182002-12-02 01:23:04 +0000275RBitList : INTVAL {
276 $$ = new std::vector<unsigned>();
277 $$->push_back($1);
278 } | INTVAL '-' INTVAL {
279 if ($1 < $3 || $1 < 0 || $3 < 0) {
280 err() << "Invalid bit range: " << $1 << "-" << $3 << "!\n";
281 abort();
282 }
283 $$ = new std::vector<unsigned>();
284 for (int i = $1; i >= $3; --i)
285 $$->push_back(i);
286 } | INTVAL INTVAL {
287 $2 = -$2;
288 if ($1 < $2 || $1 < 0 || $2 < 0) {
289 err() << "Invalid bit range: " << $1 << "-" << $2 << "!\n";
290 abort();
291 }
292 $$ = new std::vector<unsigned>();
293 for (int i = $1; i >= $2; --i)
294 $$->push_back(i);
295 } | RBitList ',' INTVAL {
296 ($$=$1)->push_back($3);
297 } | RBitList ',' INTVAL '-' INTVAL {
298 if ($3 < $5 || $3 < 0 || $5 < 0) {
299 err() << "Invalid bit range: " << $3 << "-" << $5 << "!\n";
300 abort();
301 }
302 $$ = $1;
303 for (int i = $3; i >= $5; --i)
304 $$->push_back(i);
305 } | RBitList ',' INTVAL INTVAL {
306 $4 = -$4;
307 if ($3 < $4 || $3 < 0 || $4 < 0) {
308 err() << "Invalid bit range: " << $3 << "-" << $4 << "!\n";
309 abort();
310 }
311 $$ = $1;
312 for (int i = $3; i >= $4; --i)
313 $$->push_back(i);
314 };
315
316BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
317
318OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
319
320
321
322ValueList : /*empty*/ {
323 $$ = new std::vector<Init*>();
324 } | ValueListNE {
325 $$ = $1;
326 };
327
328ValueListNE : Value {
329 $$ = new std::vector<Init*>();
330 $$->push_back($1);
331 } | ValueListNE ',' Value {
332 ($$ = $1)->push_back($3);
333 };
334
335Declaration : OptPrefix Type ID OptValue {
336 addValue(RecordVal(*$3, $2, $1));
337 setValue(*$3, 0, $4);
338 $$ = $3;
339};
340
341BodyItem : Declaration ';' {
342 delete $1;
343} | SET ID OptBitList '=' Value ';' {
344 setValue(*$2, $3, $5);
345 delete $2;
346 delete $3;
347};
348
349BodyList : /*empty*/ | BodyList BodyItem;
350Body : ';' | '{' BodyList '}';
351
352SubClassRef : ClassID {
353 $$ = new SubClassRefTy($1, new std::vector<Init*>());
354 } | ClassID '<' ValueListNE '>' {
355 $$ = new SubClassRefTy($1, $3);
356 };
357
358ClassListNE : SubClassRef {
359 $$ = new std::vector<SubClassRefTy>();
360 $$->push_back(*$1);
361 delete $1;
362 }
363 | ClassListNE ',' SubClassRef {
364 ($$=$1)->push_back(*$3);
365 delete $3;
366 };
367
368ClassList : /*empty */ {
369 $$ = new std::vector<SubClassRefTy>();
370 }
371 | ':' ClassListNE {
372 $$ = $2;
373 };
374
375DeclListNE : Declaration {
376 CurRec->addTemplateArg(*$1);
377 delete $1;
378} | DeclListNE ',' Declaration {
379 CurRec->addTemplateArg(*$3);
380 delete $3;
381};
382
383TemplateArgList : '<' DeclListNE '>' {};
384OptTemplateArgList : /*empty*/ | TemplateArgList;
385
Chris Lattnerfc06bf02003-07-30 04:26:44 +0000386OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
387
388ObjectBody : OptID {
389 static unsigned AnonCounter = 0;
390 if ($1->empty())
391 *$1 = "anonymous."+utostr(AnonCounter++);
Chris Lattnere62c1182002-12-02 01:23:04 +0000392 CurRec = new Record(*$1);
393 delete $1;
394 } OptTemplateArgList ClassList {
395 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
396 addSubClass((*$4)[i].first, *(*$4)[i].second);
Chris Lattnerbfce0562003-07-30 04:56:05 +0000397 // Delete the template arg values for the class
398 delete (*$4)[i].second;
399 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000400
401 // Process any variables on the set stack...
402 for (unsigned i = 0, e = SetStack.size(); i != e; ++i)
Chris Lattner60094252003-08-03 13:58:01 +0000403 for (unsigned j = 0, e = SetStack[i].size(); j != e; ++j)
404 setValue(SetStack[i][j].Name,
405 SetStack[i][j].HasBits ? &SetStack[i][j].Bits : 0,
406 SetStack[i][j].Value);
Chris Lattnere62c1182002-12-02 01:23:04 +0000407 } Body {
408 CurRec->resolveReferences();
Chris Lattnerbfce0562003-07-30 04:56:05 +0000409
410 // Now that all of the references have been resolved, we can delete template
411 // arguments for superclasses, so they don't pollute our record, and so that
412 // their names won't conflict with later uses of the name...
413 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
414 Record *SuperClass = (*$4)[i].first;
415 for (unsigned i = 0, e = SuperClass->getTemplateArgs().size(); i != e; ++i)
416 CurRec->removeValue(SuperClass->getTemplateArgs()[i]);
417 }
418 delete $4; // Delete the class list...
419
Chris Lattnere62c1182002-12-02 01:23:04 +0000420 $$ = CurRec;
421 CurRec = 0;
422};
423
424ClassInst : CLASS ObjectBody {
425 if (Records.getClass($2->getName())) {
426 err() << "Class '" << $2->getName() << "' already defined!\n";
427 abort();
428 }
429 Records.addClass($$ = $2);
430};
431
432DefInst : DEF ObjectBody {
Chris Lattner554af5c2003-07-30 04:31:17 +0000433 if (!$2->getTemplateArgs().empty()) {
434 err() << "Def '" << $2->getName()
435 << "' is not permitted to have template arguments!\n";
436 abort();
437 }
438 // If ObjectBody has template arguments, it's an error.
Chris Lattnere62c1182002-12-02 01:23:04 +0000439 if (Records.getDef($2->getName())) {
440 err() << "Def '" << $2->getName() << "' already defined!\n";
441 abort();
442 }
443 Records.addDef($$ = $2);
444};
445
446
447Object : ClassInst | DefInst;
448
Chris Lattner60094252003-08-03 13:58:01 +0000449SETItem : ID OptBitList '=' Value {
450 SetStack.back().push_back(SetRecord(*$1, $2, $4));
451 delete $1; delete $2;
Chris Lattner2e724542003-07-28 03:49:40 +0000452};
453
Chris Lattner60094252003-08-03 13:58:01 +0000454SETList : SETItem | SETList ',' SETItem;
455
456// SETCommand - A 'SET' statement start...
457SETCommand : SET { SetStack.push_back(std::vector<SetRecord>()); } SETList IN;
458
Chris Lattner2e724542003-07-28 03:49:40 +0000459// Support Set commands wrapping objects... both with and without braces.
460Object : SETCommand '{' ObjectList '}' {
Chris Lattner2e724542003-07-28 03:49:40 +0000461 SetStack.pop_back();
462 }
463 | SETCommand Object {
Chris Lattner2e724542003-07-28 03:49:40 +0000464 SetStack.pop_back();
Chris Lattnere62c1182002-12-02 01:23:04 +0000465 };
466
467ObjectList : Object {} | ObjectList Object {};
468
469File : ObjectList {};
470
471%%
472
473int yyerror(const char *ErrorMsg) {
474 err() << "Error parsing: " << ErrorMsg << "\n";
475 abort();
476}