blob: 11fda1ded09038737fb8a9385307631370afa8ab [file] [log] [blame]
Chris Lattnerbdd3c162006-02-15 07:24:01 +00001//===-- FileParser.y - Parser for TableGen files ----------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the bison parser for Table Generator files...
11//
12//===----------------------------------------------------------------------===//
13
14%{
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
17#include <algorithm>
18#include <cstdio>
19#define YYERROR_VERBOSE 1
20
21int yyerror(const char *ErrorMsg);
22int yylex();
23
24namespace llvm {
25
26extern int Filelineno;
27static Record *CurRec = 0;
28static bool ParsingTemplateArgs = false;
29
30typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
31
32struct LetRecord {
33 std::string Name;
34 std::vector<unsigned> Bits;
35 Init *Value;
36 bool HasBits;
37 LetRecord(const std::string &N, std::vector<unsigned> *B, Init *V)
38 : Name(N), Value(V), HasBits(B != 0) {
39 if (HasBits) Bits = *B;
40 }
41};
42
43static std::vector<std::vector<LetRecord> > LetStack;
44
45
46extern std::ostream &err();
47
48static void addValue(const RecordVal &RV) {
49 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
50 // The value already exists in the class, treat this as a set...
51 if (ERV->setValue(RV.getValue())) {
52 err() << "New definition of '" << RV.getName() << "' of type '"
53 << *RV.getType() << "' is incompatible with previous "
54 << "definition of type '" << *ERV->getType() << "'!\n";
55 exit(1);
56 }
57 } else {
58 CurRec->addValue(RV);
59 }
60}
61
62static void addSuperClass(Record *SC) {
63 if (CurRec->isSubClassOf(SC)) {
64 err() << "Already subclass of '" << SC->getName() << "'!\n";
65 exit(1);
66 }
67 CurRec->addSuperClass(SC);
68}
69
70static void setValue(const std::string &ValName,
71 std::vector<unsigned> *BitList, Init *V) {
72 if (!V) return;
73
74 RecordVal *RV = CurRec->getValue(ValName);
75 if (RV == 0) {
76 err() << "Value '" << ValName << "' unknown!\n";
77 exit(1);
78 }
79
80 // Do not allow assignments like 'X = X'. This will just cause infinite loops
81 // in the resolution machinery.
82 if (!BitList)
83 if (VarInit *VI = dynamic_cast<VarInit*>(V))
84 if (VI->getName() == ValName)
85 return;
86
87 // If we are assigning to a subset of the bits in the value... then we must be
88 // assigning to a field of BitsRecTy, which must have a BitsInit
89 // initializer...
90 //
91 if (BitList) {
92 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
93 if (CurVal == 0) {
94 err() << "Value '" << ValName << "' is not a bits type!\n";
95 exit(1);
96 }
97
98 // Convert the incoming value to a bits type of the appropriate size...
99 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
100 if (BI == 0) {
101 V->convertInitializerTo(new BitsRecTy(BitList->size()));
102 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
103 exit(1);
104 }
105
106 // We should have a BitsInit type now...
107 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
108 BitsInit *BInit = (BitsInit*)BI;
109
110 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
111
112 // Loop over bits, assigning values as appropriate...
113 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
114 unsigned Bit = (*BitList)[i];
115 if (NewVal->getBit(Bit)) {
116 err() << "Cannot set bit #" << Bit << " of value '" << ValName
117 << "' more than once!\n";
118 exit(1);
119 }
120 NewVal->setBit(Bit, BInit->getBit(i));
121 }
122
123 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
124 if (NewVal->getBit(i) == 0)
125 NewVal->setBit(i, CurVal->getBit(i));
126
127 V = NewVal;
128 }
129
130 if (RV->setValue(V)) {
131 err() << "Value '" << ValName << "' of type '" << *RV->getType()
132 << "' is incompatible with initializer '" << *V << "'!\n";
133 exit(1);
134 }
135}
136
137// addSubClass - Add SC as a subclass to CurRec, resolving TemplateArgs as SC's
138// template arguments.
139static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
140 // Add all of the values in the subclass into the current class...
141 const std::vector<RecordVal> &Vals = SC->getValues();
142 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
143 addValue(Vals[i]);
144
145 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
146
147 // Ensure that an appropriate number of template arguments are specified...
148 if (TArgs.size() < TemplateArgs.size()) {
149 err() << "ERROR: More template args specified than expected!\n";
150 exit(1);
151 } else { // This class expects template arguments...
152 // Loop over all of the template arguments, setting them to the specified
153 // value or leaving them as the default if necessary.
154 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
155 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
156 // Set it now.
157 setValue(TArgs[i], 0, TemplateArgs[i]);
158
159 // Resolve it next.
160 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
161
162
163 // Now remove it.
164 CurRec->removeValue(TArgs[i]);
165
166 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
167 err() << "ERROR: Value not specified for template argument #"
168 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
169 << "'!\n";
170 exit(1);
171 }
172 }
173 }
174
175 // Since everything went well, we can now set the "superclass" list for the
176 // current record.
177 const std::vector<Record*> &SCs = SC->getSuperClasses();
178 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
179 addSuperClass(SCs[i]);
180 addSuperClass(SC);
181}
182
183} // End llvm namespace
184
185using namespace llvm;
186
187%}
188
189%union {
190 std::string* StrVal;
191 int IntVal;
192 llvm::RecTy* Ty;
193 llvm::Init* Initializer;
194 std::vector<llvm::Init*>* FieldList;
195 std::vector<unsigned>* BitList;
196 llvm::Record* Rec;
197 SubClassRefTy* SubClassRef;
198 std::vector<SubClassRefTy>* SubClassList;
199 std::vector<std::pair<llvm::Init*, std::string> >* DagValueList;
200};
201
202%token INT BIT STRING BITS LIST CODE DAG CLASS DEF FIELD LET IN
203%token SHLTOK SRATOK SRLTOK
204%token <IntVal> INTVAL
205%token <StrVal> ID VARNAME STRVAL CODEFRAGMENT
206
207%type <Ty> Type
208%type <Rec> ClassInst DefInst Object ObjectBody ClassID
209
210%type <SubClassRef> SubClassRef
211%type <SubClassList> ClassList ClassListNE
212%type <IntVal> OptPrefix
Chris Lattner81779692006-03-30 22:51:12 +0000213%type <Initializer> Value OptValue IDValue
Chris Lattnerbdd3c162006-02-15 07:24:01 +0000214%type <DagValueList> DagArgList DagArgListNE
215%type <FieldList> ValueList ValueListNE
216%type <BitList> BitList OptBitList RBitList
217%type <StrVal> Declaration OptID OptVarName ObjectName
218
219%start File
220
221%%
222
223ClassID : ID {
224 $$ = Records.getClass(*$1);
225 if ($$ == 0) {
226 err() << "Couldn't find class '" << *$1 << "'!\n";
227 exit(1);
228 }
229 delete $1;
230 };
231
232
233// TableGen types...
234Type : STRING { // string type
235 $$ = new StringRecTy();
236 } | BIT { // bit type
237 $$ = new BitRecTy();
238 } | BITS '<' INTVAL '>' { // bits<x> type
239 $$ = new BitsRecTy($3);
240 } | INT { // int type
241 $$ = new IntRecTy();
242 } | LIST '<' Type '>' { // list<x> type
243 $$ = new ListRecTy($3);
244 } | CODE { // code type
245 $$ = new CodeRecTy();
246 } | DAG { // dag type
247 $$ = new DagRecTy();
248 } | ClassID { // Record Type
249 $$ = new RecordRecTy($1);
250 };
251
252OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
253
254OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
255
Chris Lattner81779692006-03-30 22:51:12 +0000256IDValue : ID {
257 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
258 $$ = new VarInit(*$1, RV->getType());
259 } else if (CurRec && CurRec->isTemplateArg(CurRec->getName()+":"+*$1)) {
260 const RecordVal *RV = CurRec->getValue(CurRec->getName()+":"+*$1);
261 assert(RV && "Template arg doesn't exist??");
262 $$ = new VarInit(CurRec->getName()+":"+*$1, RV->getType());
263 } else if (Record *D = Records.getDef(*$1)) {
264 $$ = new DefInit(D);
265 } else {
266 err() << "Variable not defined: '" << *$1 << "'!\n";
267 exit(1);
268 }
269
270 delete $1;
271};
272
273Value : IDValue {
274 $$ = $1;
275 } | INTVAL {
Chris Lattnerbdd3c162006-02-15 07:24:01 +0000276 $$ = new IntInit($1);
277 } | STRVAL {
278 $$ = new StringInit(*$1);
279 delete $1;
280 } | CODEFRAGMENT {
281 $$ = new CodeInit(*$1);
282 delete $1;
283 } | '?' {
284 $$ = new UnsetInit();
285 } | '{' ValueList '}' {
286 BitsInit *Init = new BitsInit($2->size());
287 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
288 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
289 if (Bit == 0) {
290 err() << "Element #" << i << " (" << *(*$2)[i]
291 << ") is not convertable to a bit!\n";
292 exit(1);
293 }
294 Init->setBit($2->size()-i-1, Bit);
295 }
296 $$ = Init;
297 delete $2;
298 } | ID '<' ValueListNE '>' {
299 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
300 // a new anonymous definition, deriving from CLASS<initvalslist> with no
301 // body.
302 Record *Class = Records.getClass(*$1);
303 if (!Class) {
304 err() << "Expected a class, got '" << *$1 << "'!\n";
305 exit(1);
306 }
307 delete $1;
308
309 static unsigned AnonCounter = 0;
310 Record *OldRec = CurRec; // Save CurRec.
311
312 // Create the new record, set it as CurRec temporarily.
313 CurRec = new Record("anonymous.val."+utostr(AnonCounter++));
314 addSubClass(Class, *$3); // Add info about the subclass to CurRec.
315 delete $3; // Free up the template args.
316
317 CurRec->resolveReferences();
318
319 Records.addDef(CurRec);
320
321 // The result of the expression is a reference to the new record.
322 $$ = new DefInit(CurRec);
323
324 // Restore the old CurRec
325 CurRec = OldRec;
Chris Lattnerbdd3c162006-02-15 07:24:01 +0000326 } | Value '{' BitList '}' {
327 $$ = $1->convertInitializerBitRange(*$3);
328 if ($$ == 0) {
329 err() << "Invalid bit range for value '" << *$1 << "'!\n";
330 exit(1);
331 }
332 delete $3;
333 } | '[' ValueList ']' {
334 $$ = new ListInit(*$2);
335 delete $2;
336 } | Value '.' ID {
337 if (!$1->getFieldType(*$3)) {
338 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
339 exit(1);
340 }
341 $$ = new FieldInit($1, *$3);
342 delete $3;
Chris Lattner81779692006-03-30 22:51:12 +0000343 } | '(' IDValue DagArgList ')' {
344 $$ = new DagInit($2, *$3);
345 delete $3;
Chris Lattnerbdd3c162006-02-15 07:24:01 +0000346 } | Value '[' BitList ']' {
347 std::reverse($3->begin(), $3->end());
348 $$ = $1->convertInitListSlice(*$3);
349 if ($$ == 0) {
350 err() << "Invalid list slice for value '" << *$1 << "'!\n";
351 exit(1);
352 }
353 delete $3;
354 } | SHLTOK '(' Value ',' Value ')' {
355 $$ = $3->getBinaryOp(Init::SHL, $5);
356 if ($$ == 0) {
357 err() << "Cannot shift values '" << *$3 << "' and '" << *$5 << "'!\n";
358 exit(1);
359 }
360 } | SRATOK '(' Value ',' Value ')' {
361 $$ = $3->getBinaryOp(Init::SRA, $5);
362 if ($$ == 0) {
363 err() << "Cannot shift values '" << *$3 << "' and '" << *$5 << "'!\n";
364 exit(1);
365 }
366 } | SRLTOK '(' Value ',' Value ')' {
367 $$ = $3->getBinaryOp(Init::SRL, $5);
368 if ($$ == 0) {
369 err() << "Cannot shift values '" << *$3 << "' and '" << *$5 << "'!\n";
370 exit(1);
371 }
372 };
373
374OptVarName : /* empty */ {
375 $$ = new std::string();
376 }
377 | ':' VARNAME {
378 $$ = $2;
379 };
380
381DagArgListNE : Value OptVarName {
382 $$ = new std::vector<std::pair<Init*, std::string> >();
383 $$->push_back(std::make_pair($1, *$2));
384 delete $2;
385 }
386 | DagArgListNE ',' Value OptVarName {
387 $1->push_back(std::make_pair($3, *$4));
388 delete $4;
389 $$ = $1;
390 };
391
392DagArgList : /*empty*/ {
393 $$ = new std::vector<std::pair<Init*, std::string> >();
394 }
395 | DagArgListNE { $$ = $1; };
396
397
398RBitList : INTVAL {
399 $$ = new std::vector<unsigned>();
400 $$->push_back($1);
401 } | INTVAL '-' INTVAL {
402 if ($1 < 0 || $3 < 0) {
403 err() << "Invalid range: " << $1 << "-" << $3 << "!\n";
404 exit(1);
405 }
406 $$ = new std::vector<unsigned>();
407 if ($1 < $3) {
408 for (int i = $1; i <= $3; ++i)
409 $$->push_back(i);
410 } else {
411 for (int i = $1; i >= $3; --i)
412 $$->push_back(i);
413 }
414 } | INTVAL INTVAL {
415 $2 = -$2;
416 if ($1 < 0 || $2 < 0) {
417 err() << "Invalid range: " << $1 << "-" << $2 << "!\n";
418 exit(1);
419 }
420 $$ = new std::vector<unsigned>();
421 if ($1 < $2) {
422 for (int i = $1; i <= $2; ++i)
423 $$->push_back(i);
424 } else {
425 for (int i = $1; i >= $2; --i)
426 $$->push_back(i);
427 }
428 } | RBitList ',' INTVAL {
429 ($$=$1)->push_back($3);
430 } | RBitList ',' INTVAL '-' INTVAL {
431 if ($3 < 0 || $5 < 0) {
432 err() << "Invalid range: " << $3 << "-" << $5 << "!\n";
433 exit(1);
434 }
435 $$ = $1;
436 if ($3 < $5) {
437 for (int i = $3; i <= $5; ++i)
438 $$->push_back(i);
439 } else {
440 for (int i = $3; i >= $5; --i)
441 $$->push_back(i);
442 }
443 } | RBitList ',' INTVAL INTVAL {
444 $4 = -$4;
445 if ($3 < 0 || $4 < 0) {
446 err() << "Invalid range: " << $3 << "-" << $4 << "!\n";
447 exit(1);
448 }
449 $$ = $1;
450 if ($3 < $4) {
451 for (int i = $3; i <= $4; ++i)
452 $$->push_back(i);
453 } else {
454 for (int i = $3; i >= $4; --i)
455 $$->push_back(i);
456 }
457 };
458
459BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
460
461OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
462
463
464
465ValueList : /*empty*/ {
466 $$ = new std::vector<Init*>();
467 } | ValueListNE {
468 $$ = $1;
469 };
470
471ValueListNE : Value {
472 $$ = new std::vector<Init*>();
473 $$->push_back($1);
474 } | ValueListNE ',' Value {
475 ($$ = $1)->push_back($3);
476 };
477
478Declaration : OptPrefix Type ID OptValue {
479 std::string DecName = *$3;
480 if (ParsingTemplateArgs)
481 DecName = CurRec->getName() + ":" + DecName;
482
483 addValue(RecordVal(DecName, $2, $1));
484 setValue(DecName, 0, $4);
485 $$ = new std::string(DecName);
486};
487
488BodyItem : Declaration ';' {
489 delete $1;
490} | LET ID OptBitList '=' Value ';' {
491 setValue(*$2, $3, $5);
492 delete $2;
493 delete $3;
494};
495
496BodyList : /*empty*/ | BodyList BodyItem;
497Body : ';' | '{' BodyList '}';
498
499SubClassRef : ClassID {
500 $$ = new SubClassRefTy($1, new std::vector<Init*>());
501 } | ClassID '<' ValueListNE '>' {
502 $$ = new SubClassRefTy($1, $3);
503 };
504
505ClassListNE : SubClassRef {
506 $$ = new std::vector<SubClassRefTy>();
507 $$->push_back(*$1);
508 delete $1;
509 }
510 | ClassListNE ',' SubClassRef {
511 ($$=$1)->push_back(*$3);
512 delete $3;
513 };
514
515ClassList : /*empty */ {
516 $$ = new std::vector<SubClassRefTy>();
517 }
518 | ':' ClassListNE {
519 $$ = $2;
520 };
521
522DeclListNE : Declaration {
523 CurRec->addTemplateArg(*$1);
524 delete $1;
525} | DeclListNE ',' Declaration {
526 CurRec->addTemplateArg(*$3);
527 delete $3;
528};
529
530TemplateArgList : '<' DeclListNE '>' {};
531OptTemplateArgList : /*empty*/ | TemplateArgList;
532
533OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
534
535ObjectName : OptID {
536 static unsigned AnonCounter = 0;
537 if ($1->empty())
538 *$1 = "anonymous."+utostr(AnonCounter++);
539 $$ = $1;
540};
541
542ClassName : ObjectName {
543 // If a class of this name already exists, it must be a forward ref.
544 if ((CurRec = Records.getClass(*$1))) {
545 // If the body was previously defined, this is an error.
546 if (!CurRec->getValues().empty() ||
547 !CurRec->getSuperClasses().empty() ||
548 !CurRec->getTemplateArgs().empty()) {
549 err() << "Class '" << CurRec->getName() << "' already defined!\n";
550 exit(1);
551 }
552 } else {
553 // If this is the first reference to this class, create and add it.
554 CurRec = new Record(*$1);
555 Records.addClass(CurRec);
556 }
557 delete $1;
558};
559
560DefName : ObjectName {
561 CurRec = new Record(*$1);
562 delete $1;
563
564 // Ensure redefinition doesn't happen.
565 if (Records.getDef(CurRec->getName())) {
566 err() << "Def '" << CurRec->getName() << "' already defined!\n";
567 exit(1);
568 }
569 Records.addDef(CurRec);
570};
571
572ObjectBody : ClassList {
573 for (unsigned i = 0, e = $1->size(); i != e; ++i) {
574 addSubClass((*$1)[i].first, *(*$1)[i].second);
575 // Delete the template arg values for the class
576 delete (*$1)[i].second;
577 }
578 delete $1; // Delete the class list...
579
580 // Process any variables on the set stack...
581 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
582 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
583 setValue(LetStack[i][j].Name,
584 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
585 LetStack[i][j].Value);
586 } Body {
587 $$ = CurRec;
588 CurRec = 0;
589 };
590
591ClassInst : CLASS ClassName {
592 ParsingTemplateArgs = true;
593 } OptTemplateArgList {
594 ParsingTemplateArgs = false;
595 } ObjectBody {
596 $$ = $6;
597 };
598
599DefInst : DEF DefName ObjectBody {
600 $3->resolveReferences();
601
602 // If ObjectBody has template arguments, it's an error.
603 assert($3->getTemplateArgs().empty() && "How'd this get template args?");
604 $$ = $3;
605};
606
607
608Object : ClassInst | DefInst;
609
610LETItem : ID OptBitList '=' Value {
611 LetStack.back().push_back(LetRecord(*$1, $2, $4));
612 delete $1; delete $2;
613};
614
615LETList : LETItem | LETList ',' LETItem;
616
617// LETCommand - A 'LET' statement start...
618LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
619
620// Support Set commands wrapping objects... both with and without braces.
621Object : LETCommand '{' ObjectList '}' {
622 LetStack.pop_back();
623 }
624 | LETCommand Object {
625 LetStack.pop_back();
626 };
627
628ObjectList : Object {} | ObjectList Object {};
629
630File : ObjectList {};
631
632%%
633
634int yyerror(const char *ErrorMsg) {
635 err() << "Error parsing: " << ErrorMsg << "\n";
636 exit(1);
637}