1 //===-- FileParser.y - Parser for TableGen files ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the bison parser for Table Generator files...
12 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/StringExtras.h"
19 #define YYERROR_VERBOSE 1
21 int yyerror(const char *ErrorMsg);
26 Record Rec; // Placeholder for template args and Name.
27 std::vector<Record*> DefPrototypes;
29 MultiClass(const std::string &Name) : Rec(Name) {}
33 static std::map<std::string, MultiClass*> MultiClasses;
35 extern int Filelineno;
36 static MultiClass *CurMultiClass = 0; // Set while parsing a multiclass.
37 static std::string *CurDefmPrefix = 0; // Set while parsing defm.
38 static Record *CurRec = 0;
39 static bool ParsingTemplateArgs = false;
41 typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
45 std::vector<unsigned> Bits;
48 LetRecord(const std::string &N, std::vector<unsigned> *B, Init *V)
49 : Name(N), Value(V), HasBits(B != 0) {
50 if (HasBits) Bits = *B;
54 static std::vector<std::vector<LetRecord> > LetStack;
57 extern std::ostream &err();
59 /// getActiveRec - If inside a def/class definition, return the def/class.
60 /// Otherwise, if within a multidef, return it.
61 static Record *getActiveRec() {
62 return CurRec ? CurRec : &CurMultiClass->Rec;
65 static void addValue(const RecordVal &RV) {
66 Record *TheRec = getActiveRec();
68 if (RecordVal *ERV = TheRec->getValue(RV.getName())) {
69 // The value already exists in the class, treat this as a set...
70 if (ERV->setValue(RV.getValue())) {
71 err() << "New definition of '" << RV.getName() << "' of type '"
72 << *RV.getType() << "' is incompatible with previous "
73 << "definition of type '" << *ERV->getType() << "'!\n";
81 static void addSuperClass(Record *SC) {
82 if (CurRec->isSubClassOf(SC)) {
83 err() << "Already subclass of '" << SC->getName() << "'!\n";
86 CurRec->addSuperClass(SC);
89 static void setValue(const std::string &ValName,
90 std::vector<unsigned> *BitList, Init *V) {
93 RecordVal *RV = CurRec->getValue(ValName);
95 err() << "Value '" << ValName << "' unknown!\n";
99 // Do not allow assignments like 'X = X'. This will just cause infinite loops
100 // in the resolution machinery.
102 if (VarInit *VI = dynamic_cast<VarInit*>(V))
103 if (VI->getName() == ValName)
106 // If we are assigning to a subset of the bits in the value... then we must be
107 // assigning to a field of BitsRecTy, which must have a BitsInit
111 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
113 err() << "Value '" << ValName << "' is not a bits type!\n";
117 // Convert the incoming value to a bits type of the appropriate size...
118 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
120 V->convertInitializerTo(new BitsRecTy(BitList->size()));
121 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
125 // We should have a BitsInit type now...
126 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
127 BitsInit *BInit = (BitsInit*)BI;
129 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
131 // Loop over bits, assigning values as appropriate...
132 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
133 unsigned Bit = (*BitList)[i];
134 if (NewVal->getBit(Bit)) {
135 err() << "Cannot set bit #" << Bit << " of value '" << ValName
136 << "' more than once!\n";
139 NewVal->setBit(Bit, BInit->getBit(i));
142 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
143 if (NewVal->getBit(i) == 0)
144 NewVal->setBit(i, CurVal->getBit(i));
149 if (RV->setValue(V)) {
150 err() << "Value '" << ValName << "' of type '" << *RV->getType()
151 << "' is incompatible with initializer '" << *V << "'!\n";
156 // addSubClass - Add SC as a subclass to CurRec, resolving TemplateArgs as SC's
157 // template arguments.
158 static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
159 // Add all of the values in the subclass into the current class...
160 const std::vector<RecordVal> &Vals = SC->getValues();
161 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
164 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
166 // Ensure that an appropriate number of template arguments are specified...
167 if (TArgs.size() < TemplateArgs.size()) {
168 err() << "ERROR: More template args specified than expected!\n";
172 // Loop over all of the template arguments, setting them to the specified
173 // value or leaving them as the default if necessary.
174 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
175 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
177 setValue(TArgs[i], 0, TemplateArgs[i]);
180 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
184 CurRec->removeValue(TArgs[i]);
186 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
187 err() << "ERROR: Value not specified for template argument #"
188 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
194 // Since everything went well, we can now set the "superclass" list for the
196 const std::vector<Record*> &SCs = SC->getSuperClasses();
197 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
198 addSuperClass(SCs[i]);
202 } // End llvm namespace
204 using namespace llvm;
212 llvm::Init* Initializer;
213 std::vector<llvm::Init*>* FieldList;
214 std::vector<unsigned>* BitList;
216 std::vector<llvm::Record*>* RecList;
217 SubClassRefTy* SubClassRef;
218 std::vector<SubClassRefTy>* SubClassList;
219 std::vector<std::pair<llvm::Init*, std::string> >* DagValueList;
222 %token INT BIT STRING BITS LIST CODE DAG CLASS DEF MULTICLASS DEFM FIELD LET IN
223 %token SHLTOK SRATOK SRLTOK STRCONCATTOK
224 %token <IntVal> INTVAL
225 %token <StrVal> ID VARNAME STRVAL CODEFRAGMENT
228 %type <Rec> ClassInst DefInst MultiClassDef ObjectBody ClassID
229 %type <RecList> MultiClassBody
231 %type <SubClassRef> SubClassRef
232 %type <SubClassList> ClassList ClassListNE
233 %type <IntVal> OptPrefix
234 %type <Initializer> Value OptValue IDValue
235 %type <DagValueList> DagArgList DagArgListNE
236 %type <FieldList> ValueList ValueListNE
237 %type <BitList> BitList OptBitList RBitList
238 %type <StrVal> Declaration OptID OptVarName ObjectName
246 // If CurDefmPrefix is set, we're parsing a defm, which means that this is
247 // actually the name of a multiclass.
248 MultiClass *MC = MultiClasses[*$1];
250 err() << "Couldn't find class '" << *$1 << "'!\n";
255 $$ = Records.getClass(*$1);
258 err() << "Couldn't find class '" << *$1 << "'!\n";
266 Type : STRING { // string type
267 $$ = new StringRecTy();
268 } | BIT { // bit type
270 } | BITS '<' INTVAL '>' { // bits<x> type
271 $$ = new BitsRecTy($3);
272 } | INT { // int type
274 } | LIST '<' Type '>' { // list<x> type
275 $$ = new ListRecTy($3);
276 } | CODE { // code type
277 $$ = new CodeRecTy();
278 } | DAG { // dag type
280 } | ClassID { // Record Type
281 $$ = new RecordRecTy($1);
284 OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
286 OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
289 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
290 $$ = new VarInit(*$1, RV->getType());
291 } else if (CurRec && CurRec->isTemplateArg(CurRec->getName()+":"+*$1)) {
292 const RecordVal *RV = CurRec->getValue(CurRec->getName()+":"+*$1);
293 assert(RV && "Template arg doesn't exist??");
294 $$ = new VarInit(CurRec->getName()+":"+*$1, RV->getType());
295 } else if (CurMultiClass &&
296 CurMultiClass->Rec.isTemplateArg(CurMultiClass->Rec.getName()+"::"+*$1)) {
297 std::string Name = CurMultiClass->Rec.getName()+"::"+*$1;
298 const RecordVal *RV = CurMultiClass->Rec.getValue(Name);
299 assert(RV && "Template arg doesn't exist??");
300 $$ = new VarInit(Name, RV->getType());
301 } else if (Record *D = Records.getDef(*$1)) {
304 err() << "Variable not defined: '" << *$1 << "'!\n";
314 $$ = new IntInit($1);
316 $$ = new StringInit(*$1);
319 $$ = new CodeInit(*$1);
322 $$ = new UnsetInit();
323 } | '{' ValueList '}' {
324 BitsInit *Init = new BitsInit($2->size());
325 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
326 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
328 err() << "Element #" << i << " (" << *(*$2)[i]
329 << ") is not convertable to a bit!\n";
332 Init->setBit($2->size()-i-1, Bit);
336 } | ID '<' ValueListNE '>' {
337 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
338 // a new anonymous definition, deriving from CLASS<initvalslist> with no
340 Record *Class = Records.getClass(*$1);
342 err() << "Expected a class, got '" << *$1 << "'!\n";
347 static unsigned AnonCounter = 0;
348 Record *OldRec = CurRec; // Save CurRec.
350 // Create the new record, set it as CurRec temporarily.
351 CurRec = new Record("anonymous.val."+utostr(AnonCounter++));
352 addSubClass(Class, *$3); // Add info about the subclass to CurRec.
353 delete $3; // Free up the template args.
355 CurRec->resolveReferences();
357 Records.addDef(CurRec);
359 // The result of the expression is a reference to the new record.
360 $$ = new DefInit(CurRec);
362 // Restore the old CurRec
364 } | Value '{' BitList '}' {
365 $$ = $1->convertInitializerBitRange(*$3);
367 err() << "Invalid bit range for value '" << *$1 << "'!\n";
371 } | '[' ValueList ']' {
372 $$ = new ListInit(*$2);
375 if (!$1->getFieldType(*$3)) {
376 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
379 $$ = new FieldInit($1, *$3);
381 } | '(' IDValue DagArgList ')' {
382 $$ = new DagInit($2, *$3);
384 } | Value '[' BitList ']' {
385 std::reverse($3->begin(), $3->end());
386 $$ = $1->convertInitListSlice(*$3);
388 err() << "Invalid list slice for value '" << *$1 << "'!\n";
392 } | SHLTOK '(' Value ',' Value ')' {
393 $$ = (new BinOpInit(BinOpInit::SHL, $3, $5))->Fold();
394 } | SRATOK '(' Value ',' Value ')' {
395 $$ = (new BinOpInit(BinOpInit::SRA, $3, $5))->Fold();
396 } | SRLTOK '(' Value ',' Value ')' {
397 $$ = (new BinOpInit(BinOpInit::SRL, $3, $5))->Fold();
398 } | STRCONCATTOK '(' Value ',' Value ')' {
399 $$ = (new BinOpInit(BinOpInit::STRCONCAT, $3, $5))->Fold();
402 OptVarName : /* empty */ {
403 $$ = new std::string();
409 DagArgListNE : Value OptVarName {
410 $$ = new std::vector<std::pair<Init*, std::string> >();
411 $$->push_back(std::make_pair($1, *$2));
414 | DagArgListNE ',' Value OptVarName {
415 $1->push_back(std::make_pair($3, *$4));
420 DagArgList : /*empty*/ {
421 $$ = new std::vector<std::pair<Init*, std::string> >();
423 | DagArgListNE { $$ = $1; };
427 $$ = new std::vector<unsigned>();
429 } | INTVAL '-' INTVAL {
430 if ($1 < 0 || $3 < 0) {
431 err() << "Invalid range: " << $1 << "-" << $3 << "!\n";
434 $$ = new std::vector<unsigned>();
436 for (int i = $1; i <= $3; ++i)
439 for (int i = $1; i >= $3; --i)
444 if ($1 < 0 || $2 < 0) {
445 err() << "Invalid range: " << $1 << "-" << $2 << "!\n";
448 $$ = new std::vector<unsigned>();
450 for (int i = $1; i <= $2; ++i)
453 for (int i = $1; i >= $2; --i)
456 } | RBitList ',' INTVAL {
457 ($$=$1)->push_back($3);
458 } | RBitList ',' INTVAL '-' INTVAL {
459 if ($3 < 0 || $5 < 0) {
460 err() << "Invalid range: " << $3 << "-" << $5 << "!\n";
465 for (int i = $3; i <= $5; ++i)
468 for (int i = $3; i >= $5; --i)
471 } | RBitList ',' INTVAL INTVAL {
473 if ($3 < 0 || $4 < 0) {
474 err() << "Invalid range: " << $3 << "-" << $4 << "!\n";
479 for (int i = $3; i <= $4; ++i)
482 for (int i = $3; i >= $4; --i)
487 BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
489 OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
493 ValueList : /*empty*/ {
494 $$ = new std::vector<Init*>();
499 ValueListNE : Value {
500 $$ = new std::vector<Init*>();
502 } | ValueListNE ',' Value {
503 ($$ = $1)->push_back($3);
506 Declaration : OptPrefix Type ID OptValue {
507 std::string DecName = *$3;
508 if (ParsingTemplateArgs) {
510 DecName = CurRec->getName() + ":" + DecName;
512 assert(CurMultiClass);
515 DecName = CurMultiClass->Rec.getName() + "::" + DecName;
518 addValue(RecordVal(DecName, $2, $1));
519 setValue(DecName, 0, $4);
520 $$ = new std::string(DecName);
523 BodyItem : Declaration ';' {
525 } | LET ID OptBitList '=' Value ';' {
526 setValue(*$2, $3, $5);
531 BodyList : /*empty*/ | BodyList BodyItem;
532 Body : ';' | '{' BodyList '}';
534 SubClassRef : ClassID {
535 $$ = new SubClassRefTy($1, new std::vector<Init*>());
536 } | ClassID '<' ValueListNE '>' {
537 $$ = new SubClassRefTy($1, $3);
540 ClassListNE : SubClassRef {
541 $$ = new std::vector<SubClassRefTy>();
545 | ClassListNE ',' SubClassRef {
546 ($$=$1)->push_back(*$3);
550 ClassList : /*empty */ {
551 $$ = new std::vector<SubClassRefTy>();
557 DeclListNE : Declaration {
558 getActiveRec()->addTemplateArg(*$1);
560 } | DeclListNE ',' Declaration {
561 getActiveRec()->addTemplateArg(*$3);
565 TemplateArgList : '<' DeclListNE '>' {};
566 OptTemplateArgList : /*empty*/ | TemplateArgList;
568 OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
571 static unsigned AnonCounter = 0;
573 *$1 = "anonymous."+utostr(AnonCounter++);
577 ClassName : ObjectName {
578 // If a class of this name already exists, it must be a forward ref.
579 if ((CurRec = Records.getClass(*$1))) {
580 // If the body was previously defined, this is an error.
581 if (!CurRec->getValues().empty() ||
582 !CurRec->getSuperClasses().empty() ||
583 !CurRec->getTemplateArgs().empty()) {
584 err() << "Class '" << CurRec->getName() << "' already defined!\n";
588 // If this is the first reference to this class, create and add it.
589 CurRec = new Record(*$1);
590 Records.addClass(CurRec);
595 DefName : ObjectName {
596 CurRec = new Record(*$1);
599 if (!CurMultiClass) {
600 // Top-level def definition.
602 // Ensure redefinition doesn't happen.
603 if (Records.getDef(CurRec->getName())) {
604 err() << "def '" << CurRec->getName() << "' already defined!\n";
607 Records.addDef(CurRec);
609 // Otherwise, a def inside a multiclass, add it to the multiclass.
610 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
611 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
612 err() << "def '" << CurRec->getName()
613 << "' already defined in this multiclass!\n";
616 CurMultiClass->DefPrototypes.push_back(CurRec);
620 ObjectBody : ClassList {
621 for (unsigned i = 0, e = $1->size(); i != e; ++i) {
622 addSubClass((*$1)[i].first, *(*$1)[i].second);
623 // Delete the template arg values for the class
624 delete (*$1)[i].second;
626 delete $1; // Delete the class list...
628 // Process any variables on the set stack...
629 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
630 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
631 setValue(LetStack[i][j].Name,
632 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
633 LetStack[i][j].Value);
639 ClassInst : CLASS ClassName {
640 ParsingTemplateArgs = true;
641 } OptTemplateArgList {
642 ParsingTemplateArgs = false;
647 DefInst : DEF DefName ObjectBody {
648 $3->resolveReferences();
650 // If ObjectBody has template arguments, it's an error.
651 assert($3->getTemplateArgs().empty() && "How'd this get template args?");
655 // MultiClassDef - A def instance specified inside a multiclass.
656 MultiClassDef : DefInst {
658 // Copy the template arguments for the multiclass into the def.
659 const std::vector<std::string> &TArgs = CurMultiClass->Rec.getTemplateArgs();
661 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
662 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
663 assert(RV && "Template arg doesn't exist?");
668 // MultiClassBody - Sequence of def's that are instantiated when a multiclass is
670 MultiClassBody : MultiClassDef {
671 $$ = new std::vector<Record*>();
673 } | MultiClassBody MultiClassDef {
677 MultiClassName : ID {
678 MultiClass *&MCE = MultiClasses[*$1];
680 err() << "multiclass '" << *$1 << "' already defined!\n";
683 MCE = CurMultiClass = new MultiClass(*$1);
687 // MultiClass - Multiple definitions.
688 MultiClassInst : MULTICLASS MultiClassName {
689 ParsingTemplateArgs = true;
690 } OptTemplateArgList {
691 ParsingTemplateArgs = false;
692 }'{' MultiClassBody '}' {
696 // DefMInst - Instantiate a multiclass.
697 DefMInst : DEFM ID { CurDefmPrefix = $2; } ':' SubClassRef ';' {
698 // To instantiate a multiclass, we need to first get the multiclass, then
699 // instantiate each def contained in the multiclass with the SubClassRef
700 // template parameters.
701 MultiClass *MC = MultiClasses[$5->first->getName()];
702 assert(MC && "Didn't lookup multiclass correctly?");
703 std::vector<Init*> &TemplateVals = *$5->second;
706 // Verify that the correct number of template arguments were specified.
707 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
708 if (TArgs.size() < TemplateVals.size()) {
709 err() << "ERROR: More template args specified than multiclass expects!\n";
713 // Loop over all the def's in the multiclass, instantiating each one.
714 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
715 Record *DefProto = MC->DefPrototypes[i];
717 // Add the suffix to the defm name to get the new name.
718 assert(CurRec == 0 && "A def is current?");
719 CurRec = new Record(*$2 + DefProto->getName());
721 addSubClass(DefProto, std::vector<Init*>());
723 // Loop over all of the template arguments, setting them to the specified
724 // value or leaving them as the default if necessary.
725 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
726 if (i < TemplateVals.size()) { // A value is specified for this temp-arg?
728 setValue(TArgs[i], 0, TemplateVals[i]);
731 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
734 CurRec->removeValue(TArgs[i]);
736 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
737 err() << "ERROR: Value not specified for template argument #"
738 << i << " (" << TArgs[i] << ") of multiclassclass '"
739 << MC->Rec.getName() << "'!\n";
744 // Ensure redefinition doesn't happen.
745 if (Records.getDef(CurRec->getName())) {
746 err() << "def '" << CurRec->getName() << "' already defined, "
747 << "instantiating defm '" << *$2 << "' with subdef '"
748 << DefProto->getName() << "'!\n";
751 Records.addDef(CurRec);
755 delete &TemplateVals;
759 Object : ClassInst {} | DefInst {};
760 Object : MultiClassInst | DefMInst;
762 LETItem : ID OptBitList '=' Value {
763 LetStack.back().push_back(LetRecord(*$1, $2, $4));
764 delete $1; delete $2;
767 LETList : LETItem | LETList ',' LETItem;
769 // LETCommand - A 'LET' statement start...
770 LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
772 // Support Set commands wrapping objects... both with and without braces.
773 Object : LETCommand '{' ObjectList '}' {
776 | LETCommand Object {
780 ObjectList : Object {} | ObjectList Object {};
786 int yyerror(const char *ErrorMsg) {
787 err() << "Error parsing: " << ErrorMsg << "\n";