Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 1 | //===-- 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 | |
| 21 | int yyerror(const char *ErrorMsg); |
| 22 | int yylex(); |
| 23 | |
| 24 | namespace llvm { |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 25 | struct MultiClass { |
| 26 | Record Rec; // Placeholder for template args and Name. |
| 27 | std::vector<Record*> DefPrototypes; |
| 28 | |
| 29 | MultiClass(const std::string &Name) : Rec(Name) {} |
| 30 | }; |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 32 | |
| 33 | static std::map<std::string, MultiClass*> MultiClasses; |
| 34 | |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 35 | extern int Filelineno; |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 36 | static MultiClass *CurMultiClass = 0; // Set while parsing a multiclass. |
| 37 | static std::string *CurDefmPrefix = 0; // Set while parsing defm. |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 38 | static Record *CurRec = 0; |
| 39 | static bool ParsingTemplateArgs = false; |
| 40 | |
| 41 | typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy; |
| 42 | |
| 43 | struct LetRecord { |
| 44 | std::string Name; |
| 45 | std::vector<unsigned> Bits; |
| 46 | Init *Value; |
| 47 | bool HasBits; |
| 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; |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | static std::vector<std::vector<LetRecord> > LetStack; |
| 55 | |
| 56 | |
| 57 | extern std::ostream &err(); |
| 58 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 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; |
| 63 | } |
| 64 | |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 65 | static void addValue(const RecordVal &RV) { |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 66 | Record *TheRec = getActiveRec(); |
| 67 | |
| 68 | if (RecordVal *ERV = TheRec->getValue(RV.getName())) { |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 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"; |
| 74 | exit(1); |
| 75 | } |
| 76 | } else { |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 77 | TheRec->addValue(RV); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | static void addSuperClass(Record *SC) { |
| 82 | if (CurRec->isSubClassOf(SC)) { |
| 83 | err() << "Already subclass of '" << SC->getName() << "'!\n"; |
| 84 | exit(1); |
| 85 | } |
| 86 | CurRec->addSuperClass(SC); |
| 87 | } |
| 88 | |
| 89 | static void setValue(const std::string &ValName, |
| 90 | std::vector<unsigned> *BitList, Init *V) { |
| 91 | if (!V) return; |
| 92 | |
| 93 | RecordVal *RV = CurRec->getValue(ValName); |
| 94 | if (RV == 0) { |
| 95 | err() << "Value '" << ValName << "' unknown!\n"; |
| 96 | exit(1); |
| 97 | } |
| 98 | |
| 99 | // Do not allow assignments like 'X = X'. This will just cause infinite loops |
| 100 | // in the resolution machinery. |
| 101 | if (!BitList) |
| 102 | if (VarInit *VI = dynamic_cast<VarInit*>(V)) |
| 103 | if (VI->getName() == ValName) |
| 104 | return; |
| 105 | |
| 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 |
| 108 | // initializer... |
| 109 | // |
| 110 | if (BitList) { |
| 111 | BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue()); |
| 112 | if (CurVal == 0) { |
| 113 | err() << "Value '" << ValName << "' is not a bits type!\n"; |
| 114 | exit(1); |
| 115 | } |
| 116 | |
| 117 | // Convert the incoming value to a bits type of the appropriate size... |
| 118 | Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size())); |
| 119 | if (BI == 0) { |
| 120 | V->convertInitializerTo(new BitsRecTy(BitList->size())); |
| 121 | err() << "Initializer '" << *V << "' not compatible with bit range!\n"; |
| 122 | exit(1); |
| 123 | } |
| 124 | |
| 125 | // We should have a BitsInit type now... |
| 126 | assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0); |
| 127 | BitsInit *BInit = (BitsInit*)BI; |
| 128 | |
| 129 | BitsInit *NewVal = new BitsInit(CurVal->getNumBits()); |
| 130 | |
| 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"; |
| 137 | exit(1); |
| 138 | } |
| 139 | NewVal->setBit(Bit, BInit->getBit(i)); |
| 140 | } |
| 141 | |
| 142 | for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i) |
| 143 | if (NewVal->getBit(i) == 0) |
| 144 | NewVal->setBit(i, CurVal->getBit(i)); |
| 145 | |
| 146 | V = NewVal; |
| 147 | } |
| 148 | |
| 149 | if (RV->setValue(V)) { |
| 150 | err() << "Value '" << ValName << "' of type '" << *RV->getType() |
| 151 | << "' is incompatible with initializer '" << *V << "'!\n"; |
| 152 | exit(1); |
| 153 | } |
| 154 | } |
| 155 | |
| 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) |
| 162 | addValue(Vals[i]); |
| 163 | |
| 164 | const std::vector<std::string> &TArgs = SC->getTemplateArgs(); |
| 165 | |
| 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"; |
| 169 | exit(1); |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 170 | } |
| 171 | |
| 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? |
| 176 | // Set it now. |
| 177 | setValue(TArgs[i], 0, TemplateArgs[i]); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 179 | // Resolve it next. |
| 180 | CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); |
| 181 | |
| 182 | |
| 183 | // Now remove it. |
| 184 | CurRec->removeValue(TArgs[i]); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 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() |
| 189 | << "'!\n"; |
| 190 | exit(1); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | // Since everything went well, we can now set the "superclass" list for the |
| 195 | // current record. |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 196 | const std::vector<Record*> &SCs = SC->getSuperClasses(); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 197 | for (unsigned i = 0, e = SCs.size(); i != e; ++i) |
| 198 | addSuperClass(SCs[i]); |
| 199 | addSuperClass(SC); |
| 200 | } |
| 201 | |
| 202 | } // End llvm namespace |
| 203 | |
| 204 | using namespace llvm; |
| 205 | |
| 206 | %} |
| 207 | |
| 208 | %union { |
| 209 | std::string* StrVal; |
| 210 | int IntVal; |
| 211 | llvm::RecTy* Ty; |
| 212 | llvm::Init* Initializer; |
| 213 | std::vector<llvm::Init*>* FieldList; |
| 214 | std::vector<unsigned>* BitList; |
| 215 | llvm::Record* Rec; |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 216 | std::vector<llvm::Record*>* RecList; |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 217 | SubClassRefTy* SubClassRef; |
| 218 | std::vector<SubClassRefTy>* SubClassList; |
| 219 | std::vector<std::pair<llvm::Init*, std::string> >* DagValueList; |
| 220 | }; |
| 221 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 222 | %token INT BIT STRING BITS LIST CODE DAG CLASS DEF MULTICLASS DEFM FIELD LET IN |
Chris Lattner | b831691 | 2006-03-31 21:54:11 +0000 | [diff] [blame] | 223 | %token SHLTOK SRATOK SRLTOK STRCONCATTOK |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 224 | %token <IntVal> INTVAL |
| 225 | %token <StrVal> ID VARNAME STRVAL CODEFRAGMENT |
| 226 | |
| 227 | %type <Ty> Type |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 228 | %type <Rec> ClassInst DefInst MultiClassDef ObjectBody ClassID |
| 229 | %type <RecList> MultiClassBody |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 230 | |
| 231 | %type <SubClassRef> SubClassRef |
| 232 | %type <SubClassList> ClassList ClassListNE |
| 233 | %type <IntVal> OptPrefix |
Chris Lattner | 8177969 | 2006-03-30 22:51:12 +0000 | [diff] [blame] | 234 | %type <Initializer> Value OptValue IDValue |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 235 | %type <DagValueList> DagArgList DagArgListNE |
| 236 | %type <FieldList> ValueList ValueListNE |
| 237 | %type <BitList> BitList OptBitList RBitList |
| 238 | %type <StrVal> Declaration OptID OptVarName ObjectName |
| 239 | |
| 240 | %start File |
| 241 | |
| 242 | %% |
| 243 | |
| 244 | ClassID : ID { |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 245 | if (CurDefmPrefix) { |
| 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]; |
| 249 | if (MC == 0) { |
| 250 | err() << "Couldn't find class '" << *$1 << "'!\n"; |
| 251 | exit(1); |
| 252 | } |
| 253 | $$ = &MC->Rec; |
| 254 | } else { |
| 255 | $$ = Records.getClass(*$1); |
| 256 | } |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 257 | if ($$ == 0) { |
| 258 | err() << "Couldn't find class '" << *$1 << "'!\n"; |
| 259 | exit(1); |
| 260 | } |
| 261 | delete $1; |
| 262 | }; |
| 263 | |
| 264 | |
| 265 | // TableGen types... |
| 266 | Type : STRING { // string type |
| 267 | $$ = new StringRecTy(); |
| 268 | } | BIT { // bit type |
| 269 | $$ = new BitRecTy(); |
| 270 | } | BITS '<' INTVAL '>' { // bits<x> type |
| 271 | $$ = new BitsRecTy($3); |
| 272 | } | INT { // int type |
| 273 | $$ = new IntRecTy(); |
| 274 | } | LIST '<' Type '>' { // list<x> type |
| 275 | $$ = new ListRecTy($3); |
| 276 | } | CODE { // code type |
| 277 | $$ = new CodeRecTy(); |
| 278 | } | DAG { // dag type |
| 279 | $$ = new DagRecTy(); |
| 280 | } | ClassID { // Record Type |
| 281 | $$ = new RecordRecTy($1); |
| 282 | }; |
| 283 | |
| 284 | OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; }; |
| 285 | |
| 286 | OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; }; |
| 287 | |
Chris Lattner | 8177969 | 2006-03-30 22:51:12 +0000 | [diff] [blame] | 288 | IDValue : ID { |
| 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()); |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 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()); |
Chris Lattner | 8177969 | 2006-03-30 22:51:12 +0000 | [diff] [blame] | 301 | } else if (Record *D = Records.getDef(*$1)) { |
| 302 | $$ = new DefInit(D); |
| 303 | } else { |
| 304 | err() << "Variable not defined: '" << *$1 << "'!\n"; |
| 305 | exit(1); |
| 306 | } |
| 307 | |
| 308 | delete $1; |
| 309 | }; |
| 310 | |
| 311 | Value : IDValue { |
| 312 | $$ = $1; |
| 313 | } | INTVAL { |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 314 | $$ = new IntInit($1); |
| 315 | } | STRVAL { |
| 316 | $$ = new StringInit(*$1); |
| 317 | delete $1; |
| 318 | } | CODEFRAGMENT { |
| 319 | $$ = new CodeInit(*$1); |
| 320 | delete $1; |
| 321 | } | '?' { |
| 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()); |
| 327 | if (Bit == 0) { |
| 328 | err() << "Element #" << i << " (" << *(*$2)[i] |
| 329 | << ") is not convertable to a bit!\n"; |
| 330 | exit(1); |
| 331 | } |
| 332 | Init->setBit($2->size()-i-1, Bit); |
| 333 | } |
| 334 | $$ = Init; |
| 335 | delete $2; |
| 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 |
| 339 | // body. |
| 340 | Record *Class = Records.getClass(*$1); |
| 341 | if (!Class) { |
| 342 | err() << "Expected a class, got '" << *$1 << "'!\n"; |
| 343 | exit(1); |
| 344 | } |
| 345 | delete $1; |
| 346 | |
| 347 | static unsigned AnonCounter = 0; |
| 348 | Record *OldRec = CurRec; // Save CurRec. |
| 349 | |
| 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. |
| 354 | |
| 355 | CurRec->resolveReferences(); |
| 356 | |
| 357 | Records.addDef(CurRec); |
| 358 | |
| 359 | // The result of the expression is a reference to the new record. |
| 360 | $$ = new DefInit(CurRec); |
| 361 | |
| 362 | // Restore the old CurRec |
| 363 | CurRec = OldRec; |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 364 | } | Value '{' BitList '}' { |
| 365 | $$ = $1->convertInitializerBitRange(*$3); |
| 366 | if ($$ == 0) { |
| 367 | err() << "Invalid bit range for value '" << *$1 << "'!\n"; |
| 368 | exit(1); |
| 369 | } |
| 370 | delete $3; |
| 371 | } | '[' ValueList ']' { |
| 372 | $$ = new ListInit(*$2); |
| 373 | delete $2; |
| 374 | } | Value '.' ID { |
| 375 | if (!$1->getFieldType(*$3)) { |
| 376 | err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n"; |
| 377 | exit(1); |
| 378 | } |
| 379 | $$ = new FieldInit($1, *$3); |
| 380 | delete $3; |
Chris Lattner | 8177969 | 2006-03-30 22:51:12 +0000 | [diff] [blame] | 381 | } | '(' IDValue DagArgList ')' { |
| 382 | $$ = new DagInit($2, *$3); |
| 383 | delete $3; |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 384 | } | Value '[' BitList ']' { |
| 385 | std::reverse($3->begin(), $3->end()); |
| 386 | $$ = $1->convertInitListSlice(*$3); |
| 387 | if ($$ == 0) { |
| 388 | err() << "Invalid list slice for value '" << *$1 << "'!\n"; |
| 389 | exit(1); |
| 390 | } |
| 391 | delete $3; |
| 392 | } | SHLTOK '(' Value ',' Value ')' { |
Chris Lattner | b831691 | 2006-03-31 21:54:11 +0000 | [diff] [blame] | 393 | $$ = (new BinOpInit(BinOpInit::SHL, $3, $5))->Fold(); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 394 | } | SRATOK '(' Value ',' Value ')' { |
Chris Lattner | b831691 | 2006-03-31 21:54:11 +0000 | [diff] [blame] | 395 | $$ = (new BinOpInit(BinOpInit::SRA, $3, $5))->Fold(); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 396 | } | SRLTOK '(' Value ',' Value ')' { |
Chris Lattner | b831691 | 2006-03-31 21:54:11 +0000 | [diff] [blame] | 397 | $$ = (new BinOpInit(BinOpInit::SRL, $3, $5))->Fold(); |
| 398 | } | STRCONCATTOK '(' Value ',' Value ')' { |
| 399 | $$ = (new BinOpInit(BinOpInit::STRCONCAT, $3, $5))->Fold(); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | OptVarName : /* empty */ { |
| 403 | $$ = new std::string(); |
| 404 | } |
| 405 | | ':' VARNAME { |
| 406 | $$ = $2; |
| 407 | }; |
| 408 | |
| 409 | DagArgListNE : Value OptVarName { |
| 410 | $$ = new std::vector<std::pair<Init*, std::string> >(); |
| 411 | $$->push_back(std::make_pair($1, *$2)); |
| 412 | delete $2; |
| 413 | } |
| 414 | | DagArgListNE ',' Value OptVarName { |
| 415 | $1->push_back(std::make_pair($3, *$4)); |
| 416 | delete $4; |
| 417 | $$ = $1; |
| 418 | }; |
| 419 | |
| 420 | DagArgList : /*empty*/ { |
| 421 | $$ = new std::vector<std::pair<Init*, std::string> >(); |
| 422 | } |
| 423 | | DagArgListNE { $$ = $1; }; |
| 424 | |
| 425 | |
| 426 | RBitList : INTVAL { |
| 427 | $$ = new std::vector<unsigned>(); |
| 428 | $$->push_back($1); |
| 429 | } | INTVAL '-' INTVAL { |
| 430 | if ($1 < 0 || $3 < 0) { |
| 431 | err() << "Invalid range: " << $1 << "-" << $3 << "!\n"; |
| 432 | exit(1); |
| 433 | } |
| 434 | $$ = new std::vector<unsigned>(); |
| 435 | if ($1 < $3) { |
| 436 | for (int i = $1; i <= $3; ++i) |
| 437 | $$->push_back(i); |
| 438 | } else { |
| 439 | for (int i = $1; i >= $3; --i) |
| 440 | $$->push_back(i); |
| 441 | } |
| 442 | } | INTVAL INTVAL { |
| 443 | $2 = -$2; |
| 444 | if ($1 < 0 || $2 < 0) { |
| 445 | err() << "Invalid range: " << $1 << "-" << $2 << "!\n"; |
| 446 | exit(1); |
| 447 | } |
| 448 | $$ = new std::vector<unsigned>(); |
| 449 | if ($1 < $2) { |
| 450 | for (int i = $1; i <= $2; ++i) |
| 451 | $$->push_back(i); |
| 452 | } else { |
| 453 | for (int i = $1; i >= $2; --i) |
| 454 | $$->push_back(i); |
| 455 | } |
| 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"; |
| 461 | exit(1); |
| 462 | } |
| 463 | $$ = $1; |
| 464 | if ($3 < $5) { |
| 465 | for (int i = $3; i <= $5; ++i) |
| 466 | $$->push_back(i); |
| 467 | } else { |
| 468 | for (int i = $3; i >= $5; --i) |
| 469 | $$->push_back(i); |
| 470 | } |
| 471 | } | RBitList ',' INTVAL INTVAL { |
| 472 | $4 = -$4; |
| 473 | if ($3 < 0 || $4 < 0) { |
| 474 | err() << "Invalid range: " << $3 << "-" << $4 << "!\n"; |
| 475 | exit(1); |
| 476 | } |
| 477 | $$ = $1; |
| 478 | if ($3 < $4) { |
| 479 | for (int i = $3; i <= $4; ++i) |
| 480 | $$->push_back(i); |
| 481 | } else { |
| 482 | for (int i = $3; i >= $4; --i) |
| 483 | $$->push_back(i); |
| 484 | } |
| 485 | }; |
| 486 | |
| 487 | BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); }; |
| 488 | |
| 489 | OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; }; |
| 490 | |
| 491 | |
| 492 | |
| 493 | ValueList : /*empty*/ { |
| 494 | $$ = new std::vector<Init*>(); |
| 495 | } | ValueListNE { |
| 496 | $$ = $1; |
| 497 | }; |
| 498 | |
| 499 | ValueListNE : Value { |
| 500 | $$ = new std::vector<Init*>(); |
| 501 | $$->push_back($1); |
| 502 | } | ValueListNE ',' Value { |
| 503 | ($$ = $1)->push_back($3); |
| 504 | }; |
| 505 | |
| 506 | Declaration : OptPrefix Type ID OptValue { |
| 507 | std::string DecName = *$3; |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 508 | if (ParsingTemplateArgs) { |
| 509 | if (CurRec) { |
| 510 | DecName = CurRec->getName() + ":" + DecName; |
| 511 | } else { |
| 512 | assert(CurMultiClass); |
| 513 | } |
| 514 | if (CurMultiClass) |
| 515 | DecName = CurMultiClass->Rec.getName() + "::" + DecName; |
| 516 | } |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 517 | |
| 518 | addValue(RecordVal(DecName, $2, $1)); |
| 519 | setValue(DecName, 0, $4); |
| 520 | $$ = new std::string(DecName); |
| 521 | }; |
| 522 | |
| 523 | BodyItem : Declaration ';' { |
| 524 | delete $1; |
| 525 | } | LET ID OptBitList '=' Value ';' { |
| 526 | setValue(*$2, $3, $5); |
| 527 | delete $2; |
| 528 | delete $3; |
| 529 | }; |
| 530 | |
| 531 | BodyList : /*empty*/ | BodyList BodyItem; |
| 532 | Body : ';' | '{' BodyList '}'; |
| 533 | |
| 534 | SubClassRef : ClassID { |
| 535 | $$ = new SubClassRefTy($1, new std::vector<Init*>()); |
| 536 | } | ClassID '<' ValueListNE '>' { |
| 537 | $$ = new SubClassRefTy($1, $3); |
| 538 | }; |
| 539 | |
| 540 | ClassListNE : SubClassRef { |
| 541 | $$ = new std::vector<SubClassRefTy>(); |
| 542 | $$->push_back(*$1); |
| 543 | delete $1; |
| 544 | } |
| 545 | | ClassListNE ',' SubClassRef { |
| 546 | ($$=$1)->push_back(*$3); |
| 547 | delete $3; |
| 548 | }; |
| 549 | |
| 550 | ClassList : /*empty */ { |
| 551 | $$ = new std::vector<SubClassRefTy>(); |
| 552 | } |
| 553 | | ':' ClassListNE { |
| 554 | $$ = $2; |
| 555 | }; |
| 556 | |
| 557 | DeclListNE : Declaration { |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 558 | getActiveRec()->addTemplateArg(*$1); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 559 | delete $1; |
| 560 | } | DeclListNE ',' Declaration { |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 561 | getActiveRec()->addTemplateArg(*$3); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 562 | delete $3; |
| 563 | }; |
| 564 | |
| 565 | TemplateArgList : '<' DeclListNE '>' {}; |
| 566 | OptTemplateArgList : /*empty*/ | TemplateArgList; |
| 567 | |
| 568 | OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); }; |
| 569 | |
| 570 | ObjectName : OptID { |
| 571 | static unsigned AnonCounter = 0; |
| 572 | if ($1->empty()) |
| 573 | *$1 = "anonymous."+utostr(AnonCounter++); |
| 574 | $$ = $1; |
| 575 | }; |
| 576 | |
| 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"; |
| 585 | exit(1); |
| 586 | } |
| 587 | } else { |
| 588 | // If this is the first reference to this class, create and add it. |
| 589 | CurRec = new Record(*$1); |
| 590 | Records.addClass(CurRec); |
| 591 | } |
| 592 | delete $1; |
| 593 | }; |
| 594 | |
| 595 | DefName : ObjectName { |
| 596 | CurRec = new Record(*$1); |
| 597 | delete $1; |
| 598 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 599 | if (!CurMultiClass) { |
| 600 | // Top-level def definition. |
| 601 | |
| 602 | // Ensure redefinition doesn't happen. |
| 603 | if (Records.getDef(CurRec->getName())) { |
| 604 | err() << "def '" << CurRec->getName() << "' already defined!\n"; |
| 605 | exit(1); |
| 606 | } |
| 607 | Records.addDef(CurRec); |
| 608 | } else { |
| 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"; |
| 614 | exit(1); |
| 615 | } |
| 616 | CurMultiClass->DefPrototypes.push_back(CurRec); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 617 | } |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 618 | }; |
| 619 | |
| 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; |
| 625 | } |
| 626 | delete $1; // Delete the class list... |
| 627 | |
| 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); |
| 634 | } Body { |
| 635 | $$ = CurRec; |
| 636 | CurRec = 0; |
| 637 | }; |
| 638 | |
| 639 | ClassInst : CLASS ClassName { |
| 640 | ParsingTemplateArgs = true; |
| 641 | } OptTemplateArgList { |
| 642 | ParsingTemplateArgs = false; |
| 643 | } ObjectBody { |
| 644 | $$ = $6; |
| 645 | }; |
| 646 | |
| 647 | DefInst : DEF DefName ObjectBody { |
| 648 | $3->resolveReferences(); |
| 649 | |
| 650 | // If ObjectBody has template arguments, it's an error. |
| 651 | assert($3->getTemplateArgs().empty() && "How'd this get template args?"); |
| 652 | $$ = $3; |
| 653 | }; |
| 654 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 655 | // MultiClassDef - A def instance specified inside a multiclass. |
| 656 | MultiClassDef : DefInst { |
| 657 | $$ = $1; |
| 658 | // Copy the template arguments for the multiclass into the def. |
| 659 | const std::vector<std::string> &TArgs = CurMultiClass->Rec.getTemplateArgs(); |
| 660 | |
| 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?"); |
| 664 | $$->addValue(*RV); |
| 665 | } |
| 666 | }; |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 667 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 668 | // MultiClassBody - Sequence of def's that are instantiated when a multiclass is |
| 669 | // used. |
| 670 | MultiClassBody : MultiClassDef { |
| 671 | $$ = new std::vector<Record*>(); |
| 672 | $$->push_back($1); |
| 673 | } | MultiClassBody MultiClassDef { |
| 674 | $$->push_back($2); |
| 675 | }; |
| 676 | |
| 677 | MultiClassName : ID { |
| 678 | MultiClass *&MCE = MultiClasses[*$1]; |
| 679 | if (MCE) { |
| 680 | err() << "multiclass '" << *$1 << "' already defined!\n"; |
| 681 | exit(1); |
| 682 | } |
| 683 | MCE = CurMultiClass = new MultiClass(*$1); |
| 684 | delete $1; |
| 685 | }; |
| 686 | |
| 687 | // MultiClass - Multiple definitions. |
| 688 | MultiClassInst : MULTICLASS MultiClassName { |
| 689 | ParsingTemplateArgs = true; |
| 690 | } OptTemplateArgList { |
| 691 | ParsingTemplateArgs = false; |
| 692 | }'{' MultiClassBody '}' { |
| 693 | CurMultiClass = 0; |
| 694 | }; |
| 695 | |
| 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; |
| 704 | delete $5; |
| 705 | |
| 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"; |
| 710 | exit(1); |
| 711 | } |
| 712 | |
| 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]; |
| 716 | |
| 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()); |
| 720 | |
| 721 | addSubClass(DefProto, std::vector<Init*>()); |
| 722 | |
| 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? |
| 727 | // Set it now. |
| 728 | setValue(TArgs[i], 0, TemplateVals[i]); |
| 729 | |
| 730 | // Resolve it next. |
| 731 | CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); |
| 732 | |
| 733 | // Now remove it. |
| 734 | CurRec->removeValue(TArgs[i]); |
| 735 | |
| 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"; |
| 740 | exit(1); |
| 741 | } |
| 742 | } |
| 743 | |
| 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"; |
| 749 | exit(1); |
| 750 | } |
| 751 | Records.addDef(CurRec); |
| 752 | CurRec = 0; |
| 753 | } |
| 754 | |
| 755 | delete &TemplateVals; |
| 756 | delete $2; |
| 757 | }; |
| 758 | |
| 759 | Object : ClassInst {} | DefInst {}; |
| 760 | Object : MultiClassInst | DefMInst; |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 761 | |
| 762 | LETItem : ID OptBitList '=' Value { |
| 763 | LetStack.back().push_back(LetRecord(*$1, $2, $4)); |
| 764 | delete $1; delete $2; |
| 765 | }; |
| 766 | |
| 767 | LETList : LETItem | LETList ',' LETItem; |
| 768 | |
| 769 | // LETCommand - A 'LET' statement start... |
| 770 | LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN; |
| 771 | |
| 772 | // Support Set commands wrapping objects... both with and without braces. |
| 773 | Object : LETCommand '{' ObjectList '}' { |
| 774 | LetStack.pop_back(); |
| 775 | } |
| 776 | | LETCommand Object { |
| 777 | LetStack.pop_back(); |
| 778 | }; |
| 779 | |
| 780 | ObjectList : Object {} | ObjectList Object {}; |
| 781 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame^] | 782 | File : ObjectList; |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 783 | |
| 784 | %% |
| 785 | |
| 786 | int yyerror(const char *ErrorMsg) { |
| 787 | err() << "Error parsing: " << ErrorMsg << "\n"; |
| 788 | exit(1); |
| 789 | } |