| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1 | //===-- FileParser.y - Parser for TableGen files ----------------*- C++ -*-===// |
| John Criswell | 9583cfa | 2003-10-21 15:29:18 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the bison parser for Table Generator files... |
| 11 | // |
| Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 13 | |
| 14 | %{ |
| 15 | #include "Record.h" |
| 16 | #include "Support/StringExtras.h" |
| 17 | #include <algorithm> |
| 18 | #include <cstdio> |
| 19 | #define YYERROR_VERBOSE 1 |
| 20 | |
| 21 | int yyerror(const char *ErrorMsg); |
| 22 | int yylex(); |
| 23 | extern int Filelineno; |
| 24 | static Record *CurRec = 0; |
| 25 | |
| 26 | typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy; |
| 27 | |
| 28 | struct LetRecord { |
| 29 | std::string Name; |
| 30 | std::vector<unsigned> Bits; |
| 31 | Init *Value; |
| 32 | bool HasBits; |
| 33 | LetRecord(const std::string &N, std::vector<unsigned> *B, Init *V) |
| 34 | : Name(N), Value(V), HasBits(B != 0) { |
| 35 | if (HasBits) Bits = *B; |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | static std::vector<std::vector<LetRecord> > LetStack; |
| 40 | |
| 41 | |
| 42 | extern std::ostream &err(); |
| 43 | |
| 44 | static void addValue(const RecordVal &RV) { |
| 45 | if (RecordVal *ERV = CurRec->getValue(RV.getName())) { |
| 46 | // The value already exists in the class, treat this as a set... |
| 47 | if (ERV->setValue(RV.getValue())) { |
| 48 | err() << "New definition of '" << RV.getName() << "' of type '" |
| 49 | << *RV.getType() << "' is incompatible with previous " |
| 50 | << "definition of type '" << *ERV->getType() << "'!\n"; |
| 51 | abort(); |
| 52 | } |
| 53 | } else { |
| 54 | CurRec->addValue(RV); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static void addSuperClass(Record *SC) { |
| 59 | if (CurRec->isSubClassOf(SC)) { |
| 60 | err() << "Already subclass of '" << SC->getName() << "'!\n"; |
| 61 | abort(); |
| 62 | } |
| 63 | CurRec->addSuperClass(SC); |
| 64 | } |
| 65 | |
| 66 | static void setValue(const std::string &ValName, |
| 67 | std::vector<unsigned> *BitList, Init *V) { |
| 68 | if (!V) return ; |
| 69 | |
| 70 | RecordVal *RV = CurRec->getValue(ValName); |
| 71 | if (RV == 0) { |
| 72 | err() << "Value '" << ValName << "' unknown!\n"; |
| 73 | abort(); |
| 74 | } |
| 75 | |
| 76 | // If we are assigning to a subset of the bits in the value... then we must be |
| 77 | // assigning to a field of BitsRecTy, which must have a BitsInit |
| 78 | // initializer... |
| 79 | // |
| 80 | if (BitList) { |
| 81 | BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue()); |
| 82 | if (CurVal == 0) { |
| 83 | err() << "Value '" << ValName << "' is not a bits type!\n"; |
| 84 | abort(); |
| 85 | } |
| 86 | |
| 87 | // Convert the incoming value to a bits type of the appropriate size... |
| 88 | Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size())); |
| 89 | if (BI == 0) { |
| 90 | V->convertInitializerTo(new BitsRecTy(BitList->size())); |
| 91 | err() << "Initializer '" << *V << "' not compatible with bit range!\n"; |
| 92 | abort(); |
| 93 | } |
| 94 | |
| 95 | // We should have a BitsInit type now... |
| 96 | assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0); |
| 97 | BitsInit *BInit = (BitsInit*)BI; |
| 98 | |
| 99 | BitsInit *NewVal = new BitsInit(CurVal->getNumBits()); |
| 100 | |
| 101 | // Loop over bits, assigning values as appropriate... |
| 102 | for (unsigned i = 0, e = BitList->size(); i != e; ++i) { |
| 103 | unsigned Bit = (*BitList)[i]; |
| 104 | if (NewVal->getBit(Bit)) { |
| 105 | err() << "Cannot set bit #" << Bit << " of value '" << ValName |
| 106 | << "' more than once!\n"; |
| 107 | abort(); |
| 108 | } |
| 109 | NewVal->setBit(Bit, BInit->getBit(i)); |
| 110 | } |
| 111 | |
| 112 | for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i) |
| 113 | if (NewVal->getBit(i) == 0) |
| 114 | NewVal->setBit(i, CurVal->getBit(i)); |
| 115 | |
| 116 | V = NewVal; |
| 117 | } |
| 118 | |
| 119 | if (RV->setValue(V)) { |
| 120 | err() << "Value '" << ValName << "' of type '" << *RV->getType() |
| 121 | << "' is incompatible with initializer '" << *V << "'!\n"; |
| 122 | abort(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) { |
| 127 | // Add all of the values in the subclass into the current class... |
| 128 | const std::vector<RecordVal> &Vals = SC->getValues(); |
| 129 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) |
| 130 | addValue(Vals[i]); |
| 131 | |
| 132 | const std::vector<std::string> &TArgs = SC->getTemplateArgs(); |
| 133 | |
| 134 | // Ensure that an appropriate number of template arguments are specified... |
| 135 | if (TArgs.size() < TemplateArgs.size()) { |
| 136 | err() << "ERROR: More template args specified than expected!\n"; |
| 137 | abort(); |
| 138 | } else { // This class expects template arguments... |
| 139 | // Loop over all of the template arguments, setting them to the specified |
| 140 | // value or leaving them as the default as necessary. |
| 141 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
| 142 | if (i < TemplateArgs.size()) { // A value is specified for this temp-arg? |
| 143 | // Set it now. |
| 144 | setValue(TArgs[i], 0, TemplateArgs[i]); |
| 145 | } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { |
| 146 | err() << "ERROR: Value not specified for template argument #" |
| 147 | << i << " (" << TArgs[i] << ") of subclass '" << SC->getName() |
| 148 | << "'!\n"; |
| 149 | abort(); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | |
| 155 | // Since everything went well, we can now set the "superclass" list for the |
| 156 | // current record. |
| 157 | const std::vector<Record*> &SCs = SC->getSuperClasses(); |
| 158 | for (unsigned i = 0, e = SCs.size(); i != e; ++i) |
| 159 | addSuperClass(SCs[i]); |
| 160 | addSuperClass(SC); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | %} |
| 165 | |
| 166 | %union { |
| 167 | std::string *StrVal; |
| 168 | int IntVal; |
| 169 | RecTy *Ty; |
| 170 | Init *Initializer; |
| 171 | std::vector<Init*> *FieldList; |
| 172 | std::vector<unsigned>*BitList; |
| 173 | Record *Rec; |
| 174 | SubClassRefTy *SubClassRef; |
| 175 | std::vector<SubClassRefTy> *SubClassList; |
| 176 | std::vector<std::pair<Init*, std::string> > *DagValueList; |
| 177 | }; |
| 178 | |
| 179 | %token INT BIT STRING BITS LIST CODE DAG CLASS DEF FIELD LET IN |
| 180 | %token <IntVal> INTVAL |
| 181 | %token <StrVal> ID VARNAME STRVAL CODEFRAGMENT |
| 182 | |
| 183 | %type <Ty> Type |
| 184 | %type <Rec> ClassInst DefInst Object ObjectBody ClassID |
| 185 | |
| 186 | %type <SubClassRef> SubClassRef |
| 187 | %type <SubClassList> ClassList ClassListNE |
| 188 | %type <IntVal> OptPrefix |
| 189 | %type <Initializer> Value OptValue |
| 190 | %type <DagValueList> DagArgList DagArgListNE |
| 191 | %type <FieldList> ValueList ValueListNE |
| 192 | %type <BitList> BitList OptBitList RBitList |
| 193 | %type <StrVal> Declaration OptID OptVarName |
| 194 | |
| 195 | %start File |
| 196 | %% |
| 197 | |
| 198 | ClassID : ID { |
| 199 | $$ = Records.getClass(*$1); |
| 200 | if ($$ == 0) { |
| 201 | err() << "Couldn't find class '" << *$1 << "'!\n"; |
| 202 | abort(); |
| 203 | } |
| 204 | delete $1; |
| 205 | }; |
| 206 | |
| 207 | |
| 208 | // TableGen types... |
| 209 | Type : 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 '<' Type '>' { // list<x> type |
| 218 | $$ = new ListRecTy($3); |
| 219 | } | CODE { // code type |
| 220 | $$ = new CodeRecTy(); |
| 221 | } | DAG { // dag type |
| 222 | $$ = new DagRecTy(); |
| 223 | } | ClassID { // Record Type |
| 224 | $$ = new RecordRecTy($1); |
| 225 | }; |
| 226 | |
| 227 | OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; }; |
| 228 | |
| 229 | OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; }; |
| 230 | |
| 231 | Value : INTVAL { |
| 232 | $$ = new IntInit($1); |
| 233 | } | STRVAL { |
| 234 | $$ = new StringInit(*$1); |
| 235 | delete $1; |
| 236 | } | CODEFRAGMENT { |
| 237 | $$ = new CodeInit(*$1); |
| 238 | delete $1; |
| 239 | } | '?' { |
| 240 | $$ = new UnsetInit(); |
| 241 | } | '{' ValueList '}' { |
| 242 | BitsInit *Init = new BitsInit($2->size()); |
| 243 | for (unsigned i = 0, e = $2->size(); i != e; ++i) { |
| 244 | struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy()); |
| 245 | if (Bit == 0) { |
| 246 | err() << "Element #" << i << " (" << *(*$2)[i] |
| 247 | << ") is not convertable to a bit!\n"; |
| 248 | abort(); |
| 249 | } |
| 250 | Init->setBit($2->size()-i-1, Bit); |
| 251 | } |
| 252 | $$ = Init; |
| 253 | delete $2; |
| 254 | } | ID { |
| 255 | if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) { |
| 256 | $$ = new VarInit(*$1, RV->getType()); |
| 257 | } else if (Record *D = Records.getDef(*$1)) { |
| 258 | $$ = new DefInit(D); |
| 259 | } else { |
| 260 | err() << "Variable not defined: '" << *$1 << "'!\n"; |
| 261 | abort(); |
| 262 | } |
| 263 | |
| 264 | delete $1; |
| 265 | } | Value '{' BitList '}' { |
| 266 | $$ = $1->convertInitializerBitRange(*$3); |
| 267 | if ($$ == 0) { |
| 268 | err() << "Invalid bit range for value '" << *$1 << "'!\n"; |
| 269 | abort(); |
| 270 | } |
| 271 | delete $3; |
| 272 | } | '[' ValueList ']' { |
| 273 | $$ = new ListInit(*$2); |
| 274 | delete $2; |
| 275 | } | Value '.' ID { |
| 276 | if (!$1->getFieldType(*$3)) { |
| 277 | err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n"; |
| 278 | abort(); |
| 279 | } |
| 280 | $$ = new FieldInit($1, *$3); |
| 281 | delete $3; |
| 282 | } | '(' ID DagArgList ')' { |
| 283 | Record *D = Records.getDef(*$2); |
| 284 | if (D == 0) { |
| 285 | err() << "Invalid def '" << *$2 << "'!\n"; |
| 286 | abort(); |
| 287 | } |
| 288 | $$ = new DagInit(D, *$3); |
| 289 | delete $2; delete $3; |
| 290 | }; |
| 291 | |
| 292 | OptVarName : /* empty */ { |
| 293 | $$ = new std::string(); |
| 294 | } |
| 295 | | ':' VARNAME { |
| 296 | $$ = $2; |
| 297 | }; |
| 298 | |
| 299 | DagArgListNE : Value OptVarName { |
| 300 | $$ = new std::vector<std::pair<Init*, std::string> >(); |
| 301 | $$->push_back(std::make_pair($1, *$2)); |
| 302 | delete $2; |
| 303 | } |
| 304 | | DagArgListNE ',' Value OptVarName { |
| 305 | $1->push_back(std::make_pair($3, *$4)); |
| 306 | delete $4; |
| 307 | $$ = $1; |
| 308 | }; |
| 309 | |
| 310 | DagArgList : /*empty*/ { |
| 311 | $$ = new std::vector<std::pair<Init*, std::string> >(); |
| 312 | } |
| 313 | | DagArgListNE { $$ = $1; }; |
| 314 | |
| 315 | |
| 316 | RBitList : INTVAL { |
| 317 | $$ = new std::vector<unsigned>(); |
| 318 | $$->push_back($1); |
| 319 | } | INTVAL '-' INTVAL { |
| 320 | if ($1 < $3 || $1 < 0 || $3 < 0) { |
| 321 | err() << "Invalid bit range: " << $1 << "-" << $3 << "!\n"; |
| 322 | abort(); |
| 323 | } |
| 324 | $$ = new std::vector<unsigned>(); |
| 325 | for (int i = $1; i >= $3; --i) |
| 326 | $$->push_back(i); |
| 327 | } | INTVAL INTVAL { |
| 328 | $2 = -$2; |
| 329 | if ($1 < $2 || $1 < 0 || $2 < 0) { |
| 330 | err() << "Invalid bit range: " << $1 << "-" << $2 << "!\n"; |
| 331 | abort(); |
| 332 | } |
| 333 | $$ = new std::vector<unsigned>(); |
| 334 | for (int i = $1; i >= $2; --i) |
| 335 | $$->push_back(i); |
| 336 | } | RBitList ',' INTVAL { |
| 337 | ($$=$1)->push_back($3); |
| 338 | } | RBitList ',' INTVAL '-' INTVAL { |
| 339 | if ($3 < $5 || $3 < 0 || $5 < 0) { |
| 340 | err() << "Invalid bit range: " << $3 << "-" << $5 << "!\n"; |
| 341 | abort(); |
| 342 | } |
| 343 | $$ = $1; |
| 344 | for (int i = $3; i >= $5; --i) |
| 345 | $$->push_back(i); |
| 346 | } | RBitList ',' INTVAL INTVAL { |
| 347 | $4 = -$4; |
| 348 | if ($3 < $4 || $3 < 0 || $4 < 0) { |
| 349 | err() << "Invalid bit range: " << $3 << "-" << $4 << "!\n"; |
| 350 | abort(); |
| 351 | } |
| 352 | $$ = $1; |
| 353 | for (int i = $3; i >= $4; --i) |
| 354 | $$->push_back(i); |
| 355 | }; |
| 356 | |
| 357 | BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); }; |
| 358 | |
| 359 | OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; }; |
| 360 | |
| 361 | |
| 362 | |
| 363 | ValueList : /*empty*/ { |
| 364 | $$ = new std::vector<Init*>(); |
| 365 | } | ValueListNE { |
| 366 | $$ = $1; |
| 367 | }; |
| 368 | |
| 369 | ValueListNE : Value { |
| 370 | $$ = new std::vector<Init*>(); |
| 371 | $$->push_back($1); |
| 372 | } | ValueListNE ',' Value { |
| 373 | ($$ = $1)->push_back($3); |
| 374 | }; |
| 375 | |
| 376 | Declaration : OptPrefix Type ID OptValue { |
| 377 | addValue(RecordVal(*$3, $2, $1)); |
| 378 | setValue(*$3, 0, $4); |
| 379 | $$ = $3; |
| 380 | }; |
| 381 | |
| 382 | BodyItem : Declaration ';' { |
| 383 | delete $1; |
| 384 | } | LET ID OptBitList '=' Value ';' { |
| 385 | setValue(*$2, $3, $5); |
| 386 | delete $2; |
| 387 | delete $3; |
| 388 | }; |
| 389 | |
| 390 | BodyList : /*empty*/ | BodyList BodyItem; |
| 391 | Body : ';' | '{' BodyList '}'; |
| 392 | |
| 393 | SubClassRef : ClassID { |
| 394 | $$ = new SubClassRefTy($1, new std::vector<Init*>()); |
| 395 | } | ClassID '<' ValueListNE '>' { |
| 396 | $$ = new SubClassRefTy($1, $3); |
| 397 | }; |
| 398 | |
| 399 | ClassListNE : SubClassRef { |
| 400 | $$ = new std::vector<SubClassRefTy>(); |
| 401 | $$->push_back(*$1); |
| 402 | delete $1; |
| 403 | } |
| 404 | | ClassListNE ',' SubClassRef { |
| 405 | ($$=$1)->push_back(*$3); |
| 406 | delete $3; |
| 407 | }; |
| 408 | |
| 409 | ClassList : /*empty */ { |
| 410 | $$ = new std::vector<SubClassRefTy>(); |
| 411 | } |
| 412 | | ':' ClassListNE { |
| 413 | $$ = $2; |
| 414 | }; |
| 415 | |
| 416 | DeclListNE : Declaration { |
| 417 | CurRec->addTemplateArg(*$1); |
| 418 | delete $1; |
| 419 | } | DeclListNE ',' Declaration { |
| 420 | CurRec->addTemplateArg(*$3); |
| 421 | delete $3; |
| 422 | }; |
| 423 | |
| 424 | TemplateArgList : '<' DeclListNE '>' {}; |
| 425 | OptTemplateArgList : /*empty*/ | TemplateArgList; |
| 426 | |
| 427 | OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); }; |
| 428 | |
| 429 | ObjectBody : OptID { |
| 430 | static unsigned AnonCounter = 0; |
| 431 | if ($1->empty()) |
| 432 | *$1 = "anonymous."+utostr(AnonCounter++); |
| 433 | CurRec = new Record(*$1); |
| 434 | delete $1; |
| 435 | } OptTemplateArgList ClassList { |
| 436 | for (unsigned i = 0, e = $4->size(); i != e; ++i) { |
| 437 | addSubClass((*$4)[i].first, *(*$4)[i].second); |
| 438 | // Delete the template arg values for the class |
| 439 | delete (*$4)[i].second; |
| 440 | } |
| 441 | |
| 442 | // Process any variables on the set stack... |
| 443 | for (unsigned i = 0, e = LetStack.size(); i != e; ++i) |
| 444 | for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) |
| 445 | setValue(LetStack[i][j].Name, |
| 446 | LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0, |
| 447 | LetStack[i][j].Value); |
| 448 | } Body { |
| 449 | CurRec->resolveReferences(); |
| 450 | |
| 451 | // Now that all of the references have been resolved, we can delete template |
| 452 | // arguments for superclasses, so they don't pollute our record, and so that |
| 453 | // their names won't conflict with later uses of the name... |
| 454 | for (unsigned i = 0, e = $4->size(); i != e; ++i) { |
| 455 | Record *SuperClass = (*$4)[i].first; |
| 456 | for (unsigned i = 0, e = SuperClass->getTemplateArgs().size(); i != e; ++i) |
| 457 | CurRec->removeValue(SuperClass->getTemplateArgs()[i]); |
| 458 | } |
| 459 | delete $4; // Delete the class list... |
| 460 | |
| 461 | $$ = CurRec; |
| 462 | CurRec = 0; |
| 463 | }; |
| 464 | |
| 465 | ClassInst : CLASS ObjectBody { |
| 466 | if (Records.getClass($2->getName())) { |
| 467 | err() << "Class '" << $2->getName() << "' already defined!\n"; |
| 468 | abort(); |
| 469 | } |
| 470 | Records.addClass($$ = $2); |
| 471 | }; |
| 472 | |
| 473 | DefInst : DEF ObjectBody { |
| 474 | if (!$2->getTemplateArgs().empty()) { |
| 475 | err() << "Def '" << $2->getName() |
| 476 | << "' is not permitted to have template arguments!\n"; |
| 477 | abort(); |
| 478 | } |
| 479 | // If ObjectBody has template arguments, it's an error. |
| 480 | if (Records.getDef($2->getName())) { |
| 481 | err() << "Def '" << $2->getName() << "' already defined!\n"; |
| 482 | abort(); |
| 483 | } |
| 484 | Records.addDef($$ = $2); |
| 485 | }; |
| 486 | |
| 487 | |
| 488 | Object : ClassInst | DefInst; |
| 489 | |
| 490 | LETItem : ID OptBitList '=' Value { |
| 491 | LetStack.back().push_back(LetRecord(*$1, $2, $4)); |
| 492 | delete $1; delete $2; |
| 493 | }; |
| 494 | |
| 495 | LETList : LETItem | LETList ',' LETItem; |
| 496 | |
| 497 | // LETCommand - A 'LET' statement start... |
| 498 | LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN; |
| 499 | |
| 500 | // Support Set commands wrapping objects... both with and without braces. |
| 501 | Object : LETCommand '{' ObjectList '}' { |
| 502 | LetStack.pop_back(); |
| 503 | } |
| 504 | | LETCommand Object { |
| 505 | LetStack.pop_back(); |
| 506 | }; |
| 507 | |
| 508 | ObjectList : Object {} | ObjectList Object {}; |
| 509 | |
| 510 | File : ObjectList {}; |
| 511 | |
| 512 | %% |
| 513 | |
| 514 | int yyerror(const char *ErrorMsg) { |
| 515 | err() << "Error parsing: " << ErrorMsg << "\n"; |
| 516 | abort(); |
| 517 | } |