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 | |
Chris Lattner | dc52f17 | 2006-10-07 07:15:19 +0000 | [diff] [blame] | 93 | Record *TheRec = getActiveRec(); |
| 94 | RecordVal *RV = TheRec->getValue(ValName); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 95 | if (RV == 0) { |
| 96 | err() << "Value '" << ValName << "' unknown!\n"; |
| 97 | exit(1); |
| 98 | } |
| 99 | |
| 100 | // Do not allow assignments like 'X = X'. This will just cause infinite loops |
| 101 | // in the resolution machinery. |
| 102 | if (!BitList) |
| 103 | if (VarInit *VI = dynamic_cast<VarInit*>(V)) |
| 104 | if (VI->getName() == ValName) |
| 105 | return; |
| 106 | |
| 107 | // If we are assigning to a subset of the bits in the value... then we must be |
| 108 | // assigning to a field of BitsRecTy, which must have a BitsInit |
| 109 | // initializer... |
| 110 | // |
| 111 | if (BitList) { |
| 112 | BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue()); |
| 113 | if (CurVal == 0) { |
| 114 | err() << "Value '" << ValName << "' is not a bits type!\n"; |
| 115 | exit(1); |
| 116 | } |
| 117 | |
| 118 | // Convert the incoming value to a bits type of the appropriate size... |
| 119 | Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size())); |
| 120 | if (BI == 0) { |
| 121 | V->convertInitializerTo(new BitsRecTy(BitList->size())); |
| 122 | err() << "Initializer '" << *V << "' not compatible with bit range!\n"; |
| 123 | exit(1); |
| 124 | } |
| 125 | |
| 126 | // We should have a BitsInit type now... |
| 127 | assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0); |
| 128 | BitsInit *BInit = (BitsInit*)BI; |
| 129 | |
| 130 | BitsInit *NewVal = new BitsInit(CurVal->getNumBits()); |
| 131 | |
| 132 | // Loop over bits, assigning values as appropriate... |
| 133 | for (unsigned i = 0, e = BitList->size(); i != e; ++i) { |
| 134 | unsigned Bit = (*BitList)[i]; |
| 135 | if (NewVal->getBit(Bit)) { |
| 136 | err() << "Cannot set bit #" << Bit << " of value '" << ValName |
| 137 | << "' more than once!\n"; |
| 138 | exit(1); |
| 139 | } |
| 140 | NewVal->setBit(Bit, BInit->getBit(i)); |
| 141 | } |
| 142 | |
| 143 | for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i) |
| 144 | if (NewVal->getBit(i) == 0) |
| 145 | NewVal->setBit(i, CurVal->getBit(i)); |
| 146 | |
| 147 | V = NewVal; |
| 148 | } |
| 149 | |
| 150 | if (RV->setValue(V)) { |
| 151 | err() << "Value '" << ValName << "' of type '" << *RV->getType() |
| 152 | << "' is incompatible with initializer '" << *V << "'!\n"; |
| 153 | exit(1); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // addSubClass - Add SC as a subclass to CurRec, resolving TemplateArgs as SC's |
| 158 | // template arguments. |
| 159 | static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) { |
| 160 | // Add all of the values in the subclass into the current class... |
| 161 | const std::vector<RecordVal> &Vals = SC->getValues(); |
| 162 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) |
| 163 | addValue(Vals[i]); |
| 164 | |
| 165 | const std::vector<std::string> &TArgs = SC->getTemplateArgs(); |
| 166 | |
| 167 | // Ensure that an appropriate number of template arguments are specified... |
| 168 | if (TArgs.size() < TemplateArgs.size()) { |
| 169 | err() << "ERROR: More template args specified than expected!\n"; |
| 170 | exit(1); |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // Loop over all of the template arguments, setting them to the specified |
| 174 | // value or leaving them as the default if necessary. |
| 175 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
| 176 | if (i < TemplateArgs.size()) { // A value is specified for this temp-arg? |
| 177 | // Set it now. |
| 178 | setValue(TArgs[i], 0, TemplateArgs[i]); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 180 | // Resolve it next. |
| 181 | CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); |
| 182 | |
| 183 | |
| 184 | // Now remove it. |
| 185 | CurRec->removeValue(TArgs[i]); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 187 | } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { |
| 188 | err() << "ERROR: Value not specified for template argument #" |
| 189 | << i << " (" << TArgs[i] << ") of subclass '" << SC->getName() |
| 190 | << "'!\n"; |
| 191 | exit(1); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | // Since everything went well, we can now set the "superclass" list for the |
| 196 | // current record. |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 197 | const std::vector<Record*> &SCs = SC->getSuperClasses(); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 198 | for (unsigned i = 0, e = SCs.size(); i != e; ++i) |
| 199 | addSuperClass(SCs[i]); |
| 200 | addSuperClass(SC); |
| 201 | } |
| 202 | |
| 203 | } // End llvm namespace |
| 204 | |
| 205 | using namespace llvm; |
| 206 | |
| 207 | %} |
| 208 | |
| 209 | %union { |
| 210 | std::string* StrVal; |
| 211 | int IntVal; |
| 212 | llvm::RecTy* Ty; |
| 213 | llvm::Init* Initializer; |
| 214 | std::vector<llvm::Init*>* FieldList; |
| 215 | std::vector<unsigned>* BitList; |
| 216 | llvm::Record* Rec; |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 217 | std::vector<llvm::Record*>* RecList; |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 218 | SubClassRefTy* SubClassRef; |
| 219 | std::vector<SubClassRefTy>* SubClassList; |
| 220 | std::vector<std::pair<llvm::Init*, std::string> >* DagValueList; |
| 221 | }; |
| 222 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 223 | %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] | 224 | %token SHLTOK SRATOK SRLTOK STRCONCATTOK |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 225 | %token <IntVal> INTVAL |
| 226 | %token <StrVal> ID VARNAME STRVAL CODEFRAGMENT |
| 227 | |
| 228 | %type <Ty> Type |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 229 | %type <Rec> ClassInst DefInst MultiClassDef ObjectBody ClassID |
| 230 | %type <RecList> MultiClassBody |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 231 | |
| 232 | %type <SubClassRef> SubClassRef |
| 233 | %type <SubClassList> ClassList ClassListNE |
| 234 | %type <IntVal> OptPrefix |
Chris Lattner | 8177969 | 2006-03-30 22:51:12 +0000 | [diff] [blame] | 235 | %type <Initializer> Value OptValue IDValue |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 236 | %type <DagValueList> DagArgList DagArgListNE |
| 237 | %type <FieldList> ValueList ValueListNE |
| 238 | %type <BitList> BitList OptBitList RBitList |
| 239 | %type <StrVal> Declaration OptID OptVarName ObjectName |
| 240 | |
| 241 | %start File |
| 242 | |
| 243 | %% |
| 244 | |
| 245 | ClassID : ID { |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 246 | if (CurDefmPrefix) { |
| 247 | // If CurDefmPrefix is set, we're parsing a defm, which means that this is |
| 248 | // actually the name of a multiclass. |
| 249 | MultiClass *MC = MultiClasses[*$1]; |
| 250 | if (MC == 0) { |
| 251 | err() << "Couldn't find class '" << *$1 << "'!\n"; |
| 252 | exit(1); |
| 253 | } |
| 254 | $$ = &MC->Rec; |
| 255 | } else { |
| 256 | $$ = Records.getClass(*$1); |
| 257 | } |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 258 | if ($$ == 0) { |
| 259 | err() << "Couldn't find class '" << *$1 << "'!\n"; |
| 260 | exit(1); |
| 261 | } |
| 262 | delete $1; |
| 263 | }; |
| 264 | |
| 265 | |
| 266 | // TableGen types... |
| 267 | Type : STRING { // string type |
| 268 | $$ = new StringRecTy(); |
| 269 | } | BIT { // bit type |
| 270 | $$ = new BitRecTy(); |
| 271 | } | BITS '<' INTVAL '>' { // bits<x> type |
| 272 | $$ = new BitsRecTy($3); |
| 273 | } | INT { // int type |
| 274 | $$ = new IntRecTy(); |
| 275 | } | LIST '<' Type '>' { // list<x> type |
| 276 | $$ = new ListRecTy($3); |
| 277 | } | CODE { // code type |
| 278 | $$ = new CodeRecTy(); |
| 279 | } | DAG { // dag type |
| 280 | $$ = new DagRecTy(); |
| 281 | } | ClassID { // Record Type |
| 282 | $$ = new RecordRecTy($1); |
| 283 | }; |
| 284 | |
| 285 | OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; }; |
| 286 | |
| 287 | OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; }; |
| 288 | |
Chris Lattner | 8177969 | 2006-03-30 22:51:12 +0000 | [diff] [blame] | 289 | IDValue : ID { |
| 290 | if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) { |
| 291 | $$ = new VarInit(*$1, RV->getType()); |
| 292 | } else if (CurRec && CurRec->isTemplateArg(CurRec->getName()+":"+*$1)) { |
| 293 | const RecordVal *RV = CurRec->getValue(CurRec->getName()+":"+*$1); |
| 294 | assert(RV && "Template arg doesn't exist??"); |
| 295 | $$ = new VarInit(CurRec->getName()+":"+*$1, RV->getType()); |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 296 | } else if (CurMultiClass && |
| 297 | CurMultiClass->Rec.isTemplateArg(CurMultiClass->Rec.getName()+"::"+*$1)) { |
| 298 | std::string Name = CurMultiClass->Rec.getName()+"::"+*$1; |
| 299 | const RecordVal *RV = CurMultiClass->Rec.getValue(Name); |
| 300 | assert(RV && "Template arg doesn't exist??"); |
| 301 | $$ = new VarInit(Name, RV->getType()); |
Chris Lattner | 8177969 | 2006-03-30 22:51:12 +0000 | [diff] [blame] | 302 | } else if (Record *D = Records.getDef(*$1)) { |
| 303 | $$ = new DefInit(D); |
| 304 | } else { |
| 305 | err() << "Variable not defined: '" << *$1 << "'!\n"; |
| 306 | exit(1); |
| 307 | } |
| 308 | |
| 309 | delete $1; |
| 310 | }; |
| 311 | |
| 312 | Value : IDValue { |
| 313 | $$ = $1; |
| 314 | } | INTVAL { |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 315 | $$ = new IntInit($1); |
| 316 | } | STRVAL { |
| 317 | $$ = new StringInit(*$1); |
| 318 | delete $1; |
| 319 | } | CODEFRAGMENT { |
| 320 | $$ = new CodeInit(*$1); |
| 321 | delete $1; |
| 322 | } | '?' { |
| 323 | $$ = new UnsetInit(); |
| 324 | } | '{' ValueList '}' { |
| 325 | BitsInit *Init = new BitsInit($2->size()); |
| 326 | for (unsigned i = 0, e = $2->size(); i != e; ++i) { |
| 327 | struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy()); |
| 328 | if (Bit == 0) { |
| 329 | err() << "Element #" << i << " (" << *(*$2)[i] |
| 330 | << ") is not convertable to a bit!\n"; |
| 331 | exit(1); |
| 332 | } |
| 333 | Init->setBit($2->size()-i-1, Bit); |
| 334 | } |
| 335 | $$ = Init; |
| 336 | delete $2; |
| 337 | } | ID '<' ValueListNE '>' { |
| 338 | // This is a CLASS<initvalslist> expression. This is supposed to synthesize |
| 339 | // a new anonymous definition, deriving from CLASS<initvalslist> with no |
| 340 | // body. |
| 341 | Record *Class = Records.getClass(*$1); |
| 342 | if (!Class) { |
| 343 | err() << "Expected a class, got '" << *$1 << "'!\n"; |
| 344 | exit(1); |
| 345 | } |
| 346 | delete $1; |
| 347 | |
| 348 | static unsigned AnonCounter = 0; |
| 349 | Record *OldRec = CurRec; // Save CurRec. |
| 350 | |
| 351 | // Create the new record, set it as CurRec temporarily. |
| 352 | CurRec = new Record("anonymous.val."+utostr(AnonCounter++)); |
| 353 | addSubClass(Class, *$3); // Add info about the subclass to CurRec. |
| 354 | delete $3; // Free up the template args. |
| 355 | |
| 356 | CurRec->resolveReferences(); |
| 357 | |
| 358 | Records.addDef(CurRec); |
| 359 | |
| 360 | // The result of the expression is a reference to the new record. |
| 361 | $$ = new DefInit(CurRec); |
| 362 | |
| 363 | // Restore the old CurRec |
| 364 | CurRec = OldRec; |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 365 | } | Value '{' BitList '}' { |
| 366 | $$ = $1->convertInitializerBitRange(*$3); |
| 367 | if ($$ == 0) { |
| 368 | err() << "Invalid bit range for value '" << *$1 << "'!\n"; |
| 369 | exit(1); |
| 370 | } |
| 371 | delete $3; |
| 372 | } | '[' ValueList ']' { |
| 373 | $$ = new ListInit(*$2); |
| 374 | delete $2; |
| 375 | } | Value '.' ID { |
| 376 | if (!$1->getFieldType(*$3)) { |
| 377 | err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n"; |
| 378 | exit(1); |
| 379 | } |
| 380 | $$ = new FieldInit($1, *$3); |
| 381 | delete $3; |
Chris Lattner | 8177969 | 2006-03-30 22:51:12 +0000 | [diff] [blame] | 382 | } | '(' IDValue DagArgList ')' { |
| 383 | $$ = new DagInit($2, *$3); |
| 384 | delete $3; |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 385 | } | Value '[' BitList ']' { |
| 386 | std::reverse($3->begin(), $3->end()); |
| 387 | $$ = $1->convertInitListSlice(*$3); |
| 388 | if ($$ == 0) { |
| 389 | err() << "Invalid list slice for value '" << *$1 << "'!\n"; |
| 390 | exit(1); |
| 391 | } |
| 392 | delete $3; |
| 393 | } | SHLTOK '(' Value ',' Value ')' { |
Chris Lattner | b831691 | 2006-03-31 21:54:11 +0000 | [diff] [blame] | 394 | $$ = (new BinOpInit(BinOpInit::SHL, $3, $5))->Fold(); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 395 | } | SRATOK '(' Value ',' Value ')' { |
Chris Lattner | b831691 | 2006-03-31 21:54:11 +0000 | [diff] [blame] | 396 | $$ = (new BinOpInit(BinOpInit::SRA, $3, $5))->Fold(); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 397 | } | SRLTOK '(' Value ',' Value ')' { |
Chris Lattner | b831691 | 2006-03-31 21:54:11 +0000 | [diff] [blame] | 398 | $$ = (new BinOpInit(BinOpInit::SRL, $3, $5))->Fold(); |
| 399 | } | STRCONCATTOK '(' Value ',' Value ')' { |
| 400 | $$ = (new BinOpInit(BinOpInit::STRCONCAT, $3, $5))->Fold(); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 401 | }; |
| 402 | |
| 403 | OptVarName : /* empty */ { |
| 404 | $$ = new std::string(); |
| 405 | } |
| 406 | | ':' VARNAME { |
| 407 | $$ = $2; |
| 408 | }; |
| 409 | |
| 410 | DagArgListNE : Value OptVarName { |
| 411 | $$ = new std::vector<std::pair<Init*, std::string> >(); |
| 412 | $$->push_back(std::make_pair($1, *$2)); |
| 413 | delete $2; |
| 414 | } |
| 415 | | DagArgListNE ',' Value OptVarName { |
| 416 | $1->push_back(std::make_pair($3, *$4)); |
| 417 | delete $4; |
| 418 | $$ = $1; |
| 419 | }; |
| 420 | |
| 421 | DagArgList : /*empty*/ { |
| 422 | $$ = new std::vector<std::pair<Init*, std::string> >(); |
| 423 | } |
| 424 | | DagArgListNE { $$ = $1; }; |
| 425 | |
| 426 | |
| 427 | RBitList : INTVAL { |
| 428 | $$ = new std::vector<unsigned>(); |
| 429 | $$->push_back($1); |
| 430 | } | INTVAL '-' INTVAL { |
| 431 | if ($1 < 0 || $3 < 0) { |
| 432 | err() << "Invalid range: " << $1 << "-" << $3 << "!\n"; |
| 433 | exit(1); |
| 434 | } |
| 435 | $$ = new std::vector<unsigned>(); |
| 436 | if ($1 < $3) { |
| 437 | for (int i = $1; i <= $3; ++i) |
| 438 | $$->push_back(i); |
| 439 | } else { |
| 440 | for (int i = $1; i >= $3; --i) |
| 441 | $$->push_back(i); |
| 442 | } |
| 443 | } | INTVAL INTVAL { |
| 444 | $2 = -$2; |
| 445 | if ($1 < 0 || $2 < 0) { |
| 446 | err() << "Invalid range: " << $1 << "-" << $2 << "!\n"; |
| 447 | exit(1); |
| 448 | } |
| 449 | $$ = new std::vector<unsigned>(); |
| 450 | if ($1 < $2) { |
| 451 | for (int i = $1; i <= $2; ++i) |
| 452 | $$->push_back(i); |
| 453 | } else { |
| 454 | for (int i = $1; i >= $2; --i) |
| 455 | $$->push_back(i); |
| 456 | } |
| 457 | } | RBitList ',' INTVAL { |
| 458 | ($$=$1)->push_back($3); |
| 459 | } | RBitList ',' INTVAL '-' INTVAL { |
| 460 | if ($3 < 0 || $5 < 0) { |
| 461 | err() << "Invalid range: " << $3 << "-" << $5 << "!\n"; |
| 462 | exit(1); |
| 463 | } |
| 464 | $$ = $1; |
| 465 | if ($3 < $5) { |
| 466 | for (int i = $3; i <= $5; ++i) |
| 467 | $$->push_back(i); |
| 468 | } else { |
| 469 | for (int i = $3; i >= $5; --i) |
| 470 | $$->push_back(i); |
| 471 | } |
| 472 | } | RBitList ',' INTVAL INTVAL { |
| 473 | $4 = -$4; |
| 474 | if ($3 < 0 || $4 < 0) { |
| 475 | err() << "Invalid range: " << $3 << "-" << $4 << "!\n"; |
| 476 | exit(1); |
| 477 | } |
| 478 | $$ = $1; |
| 479 | if ($3 < $4) { |
| 480 | for (int i = $3; i <= $4; ++i) |
| 481 | $$->push_back(i); |
| 482 | } else { |
| 483 | for (int i = $3; i >= $4; --i) |
| 484 | $$->push_back(i); |
| 485 | } |
| 486 | }; |
| 487 | |
| 488 | BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); }; |
| 489 | |
| 490 | OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; }; |
| 491 | |
| 492 | |
| 493 | |
| 494 | ValueList : /*empty*/ { |
| 495 | $$ = new std::vector<Init*>(); |
| 496 | } | ValueListNE { |
| 497 | $$ = $1; |
| 498 | }; |
| 499 | |
| 500 | ValueListNE : Value { |
| 501 | $$ = new std::vector<Init*>(); |
| 502 | $$->push_back($1); |
| 503 | } | ValueListNE ',' Value { |
| 504 | ($$ = $1)->push_back($3); |
| 505 | }; |
| 506 | |
| 507 | Declaration : OptPrefix Type ID OptValue { |
| 508 | std::string DecName = *$3; |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 509 | if (ParsingTemplateArgs) { |
| 510 | if (CurRec) { |
| 511 | DecName = CurRec->getName() + ":" + DecName; |
| 512 | } else { |
| 513 | assert(CurMultiClass); |
| 514 | } |
| 515 | if (CurMultiClass) |
| 516 | DecName = CurMultiClass->Rec.getName() + "::" + DecName; |
| 517 | } |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 518 | |
| 519 | addValue(RecordVal(DecName, $2, $1)); |
| 520 | setValue(DecName, 0, $4); |
| 521 | $$ = new std::string(DecName); |
| 522 | }; |
| 523 | |
| 524 | BodyItem : Declaration ';' { |
| 525 | delete $1; |
| 526 | } | LET ID OptBitList '=' Value ';' { |
| 527 | setValue(*$2, $3, $5); |
| 528 | delete $2; |
| 529 | delete $3; |
| 530 | }; |
| 531 | |
| 532 | BodyList : /*empty*/ | BodyList BodyItem; |
| 533 | Body : ';' | '{' BodyList '}'; |
| 534 | |
| 535 | SubClassRef : ClassID { |
| 536 | $$ = new SubClassRefTy($1, new std::vector<Init*>()); |
| 537 | } | ClassID '<' ValueListNE '>' { |
| 538 | $$ = new SubClassRefTy($1, $3); |
| 539 | }; |
| 540 | |
| 541 | ClassListNE : SubClassRef { |
| 542 | $$ = new std::vector<SubClassRefTy>(); |
| 543 | $$->push_back(*$1); |
| 544 | delete $1; |
| 545 | } |
| 546 | | ClassListNE ',' SubClassRef { |
| 547 | ($$=$1)->push_back(*$3); |
| 548 | delete $3; |
| 549 | }; |
| 550 | |
| 551 | ClassList : /*empty */ { |
| 552 | $$ = new std::vector<SubClassRefTy>(); |
| 553 | } |
| 554 | | ':' ClassListNE { |
| 555 | $$ = $2; |
| 556 | }; |
| 557 | |
| 558 | DeclListNE : Declaration { |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 559 | getActiveRec()->addTemplateArg(*$1); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 560 | delete $1; |
| 561 | } | DeclListNE ',' Declaration { |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 562 | getActiveRec()->addTemplateArg(*$3); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 563 | delete $3; |
| 564 | }; |
| 565 | |
| 566 | TemplateArgList : '<' DeclListNE '>' {}; |
| 567 | OptTemplateArgList : /*empty*/ | TemplateArgList; |
| 568 | |
| 569 | OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); }; |
| 570 | |
| 571 | ObjectName : OptID { |
| 572 | static unsigned AnonCounter = 0; |
| 573 | if ($1->empty()) |
| 574 | *$1 = "anonymous."+utostr(AnonCounter++); |
| 575 | $$ = $1; |
| 576 | }; |
| 577 | |
| 578 | ClassName : ObjectName { |
| 579 | // If a class of this name already exists, it must be a forward ref. |
| 580 | if ((CurRec = Records.getClass(*$1))) { |
| 581 | // If the body was previously defined, this is an error. |
| 582 | if (!CurRec->getValues().empty() || |
| 583 | !CurRec->getSuperClasses().empty() || |
| 584 | !CurRec->getTemplateArgs().empty()) { |
| 585 | err() << "Class '" << CurRec->getName() << "' already defined!\n"; |
| 586 | exit(1); |
| 587 | } |
| 588 | } else { |
| 589 | // If this is the first reference to this class, create and add it. |
| 590 | CurRec = new Record(*$1); |
| 591 | Records.addClass(CurRec); |
| 592 | } |
| 593 | delete $1; |
| 594 | }; |
| 595 | |
| 596 | DefName : ObjectName { |
| 597 | CurRec = new Record(*$1); |
| 598 | delete $1; |
| 599 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 600 | if (!CurMultiClass) { |
| 601 | // Top-level def definition. |
| 602 | |
| 603 | // Ensure redefinition doesn't happen. |
| 604 | if (Records.getDef(CurRec->getName())) { |
| 605 | err() << "def '" << CurRec->getName() << "' already defined!\n"; |
| 606 | exit(1); |
| 607 | } |
| 608 | Records.addDef(CurRec); |
| 609 | } else { |
| 610 | // Otherwise, a def inside a multiclass, add it to the multiclass. |
| 611 | for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i) |
| 612 | if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) { |
| 613 | err() << "def '" << CurRec->getName() |
| 614 | << "' already defined in this multiclass!\n"; |
| 615 | exit(1); |
| 616 | } |
| 617 | CurMultiClass->DefPrototypes.push_back(CurRec); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 618 | } |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 619 | }; |
| 620 | |
| 621 | ObjectBody : ClassList { |
| 622 | for (unsigned i = 0, e = $1->size(); i != e; ++i) { |
| 623 | addSubClass((*$1)[i].first, *(*$1)[i].second); |
| 624 | // Delete the template arg values for the class |
| 625 | delete (*$1)[i].second; |
| 626 | } |
Chris Lattner | bcc3f0a | 2006-10-11 18:13:09 +0000 | [diff] [blame] | 627 | delete $1; // Delete the class list. |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 628 | |
Chris Lattner | bcc3f0a | 2006-10-11 18:13:09 +0000 | [diff] [blame] | 629 | // Process any variables on the let stack. |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 630 | for (unsigned i = 0, e = LetStack.size(); i != e; ++i) |
| 631 | for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) |
| 632 | setValue(LetStack[i][j].Name, |
| 633 | LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0, |
| 634 | LetStack[i][j].Value); |
| 635 | } Body { |
| 636 | $$ = CurRec; |
| 637 | CurRec = 0; |
| 638 | }; |
| 639 | |
| 640 | ClassInst : CLASS ClassName { |
| 641 | ParsingTemplateArgs = true; |
| 642 | } OptTemplateArgList { |
| 643 | ParsingTemplateArgs = false; |
| 644 | } ObjectBody { |
| 645 | $$ = $6; |
| 646 | }; |
| 647 | |
| 648 | DefInst : DEF DefName ObjectBody { |
Chris Lattner | 7717d09 | 2006-09-01 21:59:03 +0000 | [diff] [blame] | 649 | if (CurMultiClass == 0) // Def's in multiclasses aren't really defs. |
| 650 | $3->resolveReferences(); |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 651 | |
| 652 | // If ObjectBody has template arguments, it's an error. |
| 653 | assert($3->getTemplateArgs().empty() && "How'd this get template args?"); |
| 654 | $$ = $3; |
| 655 | }; |
| 656 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 657 | // MultiClassDef - A def instance specified inside a multiclass. |
| 658 | MultiClassDef : DefInst { |
| 659 | $$ = $1; |
| 660 | // Copy the template arguments for the multiclass into the def. |
| 661 | const std::vector<std::string> &TArgs = CurMultiClass->Rec.getTemplateArgs(); |
| 662 | |
| 663 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
| 664 | const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]); |
| 665 | assert(RV && "Template arg doesn't exist?"); |
| 666 | $$->addValue(*RV); |
| 667 | } |
| 668 | }; |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 669 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 670 | // MultiClassBody - Sequence of def's that are instantiated when a multiclass is |
| 671 | // used. |
| 672 | MultiClassBody : MultiClassDef { |
| 673 | $$ = new std::vector<Record*>(); |
| 674 | $$->push_back($1); |
| 675 | } | MultiClassBody MultiClassDef { |
| 676 | $$->push_back($2); |
| 677 | }; |
| 678 | |
| 679 | MultiClassName : ID { |
| 680 | MultiClass *&MCE = MultiClasses[*$1]; |
| 681 | if (MCE) { |
| 682 | err() << "multiclass '" << *$1 << "' already defined!\n"; |
| 683 | exit(1); |
| 684 | } |
| 685 | MCE = CurMultiClass = new MultiClass(*$1); |
| 686 | delete $1; |
| 687 | }; |
| 688 | |
| 689 | // MultiClass - Multiple definitions. |
| 690 | MultiClassInst : MULTICLASS MultiClassName { |
| 691 | ParsingTemplateArgs = true; |
| 692 | } OptTemplateArgList { |
| 693 | ParsingTemplateArgs = false; |
| 694 | }'{' MultiClassBody '}' { |
| 695 | CurMultiClass = 0; |
| 696 | }; |
| 697 | |
| 698 | // DefMInst - Instantiate a multiclass. |
| 699 | DefMInst : DEFM ID { CurDefmPrefix = $2; } ':' SubClassRef ';' { |
| 700 | // To instantiate a multiclass, we need to first get the multiclass, then |
| 701 | // instantiate each def contained in the multiclass with the SubClassRef |
| 702 | // template parameters. |
| 703 | MultiClass *MC = MultiClasses[$5->first->getName()]; |
| 704 | assert(MC && "Didn't lookup multiclass correctly?"); |
| 705 | std::vector<Init*> &TemplateVals = *$5->second; |
| 706 | delete $5; |
| 707 | |
| 708 | // Verify that the correct number of template arguments were specified. |
| 709 | const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs(); |
| 710 | if (TArgs.size() < TemplateVals.size()) { |
| 711 | err() << "ERROR: More template args specified than multiclass expects!\n"; |
| 712 | exit(1); |
| 713 | } |
| 714 | |
| 715 | // Loop over all the def's in the multiclass, instantiating each one. |
| 716 | for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) { |
| 717 | Record *DefProto = MC->DefPrototypes[i]; |
| 718 | |
| 719 | // Add the suffix to the defm name to get the new name. |
| 720 | assert(CurRec == 0 && "A def is current?"); |
| 721 | CurRec = new Record(*$2 + DefProto->getName()); |
| 722 | |
| 723 | addSubClass(DefProto, std::vector<Init*>()); |
| 724 | |
| 725 | // Loop over all of the template arguments, setting them to the specified |
| 726 | // value or leaving them as the default if necessary. |
| 727 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
| 728 | if (i < TemplateVals.size()) { // A value is specified for this temp-arg? |
| 729 | // Set it now. |
| 730 | setValue(TArgs[i], 0, TemplateVals[i]); |
| 731 | |
| 732 | // Resolve it next. |
| 733 | CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); |
| 734 | |
| 735 | // Now remove it. |
| 736 | CurRec->removeValue(TArgs[i]); |
| 737 | |
| 738 | } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { |
| 739 | err() << "ERROR: Value not specified for template argument #" |
| 740 | << i << " (" << TArgs[i] << ") of multiclassclass '" |
| 741 | << MC->Rec.getName() << "'!\n"; |
| 742 | exit(1); |
| 743 | } |
| 744 | } |
| 745 | |
Chris Lattner | bcc3f0a | 2006-10-11 18:13:09 +0000 | [diff] [blame] | 746 | // If the mdef is inside a 'let' expression, add to each def. |
| 747 | for (unsigned i = 0, e = LetStack.size(); i != e; ++i) |
| 748 | for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) |
| 749 | setValue(LetStack[i][j].Name, |
| 750 | LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0, |
| 751 | LetStack[i][j].Value); |
| 752 | |
| 753 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 754 | // Ensure redefinition doesn't happen. |
| 755 | if (Records.getDef(CurRec->getName())) { |
| 756 | err() << "def '" << CurRec->getName() << "' already defined, " |
| 757 | << "instantiating defm '" << *$2 << "' with subdef '" |
| 758 | << DefProto->getName() << "'!\n"; |
| 759 | exit(1); |
| 760 | } |
| 761 | Records.addDef(CurRec); |
Chris Lattner | 7717d09 | 2006-09-01 21:59:03 +0000 | [diff] [blame] | 762 | |
| 763 | CurRec->resolveReferences(); |
| 764 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 765 | CurRec = 0; |
| 766 | } |
| 767 | |
| 768 | delete &TemplateVals; |
| 769 | delete $2; |
Chris Lattner | cce56af | 2006-09-01 22:07:27 +0000 | [diff] [blame] | 770 | CurDefmPrefix = 0; |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 771 | }; |
| 772 | |
| 773 | Object : ClassInst {} | DefInst {}; |
| 774 | Object : MultiClassInst | DefMInst; |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 775 | |
| 776 | LETItem : ID OptBitList '=' Value { |
| 777 | LetStack.back().push_back(LetRecord(*$1, $2, $4)); |
| 778 | delete $1; delete $2; |
| 779 | }; |
| 780 | |
| 781 | LETList : LETItem | LETList ',' LETItem; |
| 782 | |
| 783 | // LETCommand - A 'LET' statement start... |
| 784 | LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN; |
| 785 | |
| 786 | // Support Set commands wrapping objects... both with and without braces. |
| 787 | Object : LETCommand '{' ObjectList '}' { |
| 788 | LetStack.pop_back(); |
| 789 | } |
| 790 | | LETCommand Object { |
| 791 | LetStack.pop_back(); |
| 792 | }; |
| 793 | |
| 794 | ObjectList : Object {} | ObjectList Object {}; |
| 795 | |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 796 | File : ObjectList; |
Chris Lattner | bdd3c16 | 2006-02-15 07:24:01 +0000 | [diff] [blame] | 797 | |
| 798 | %% |
| 799 | |
| 800 | int yyerror(const char *ErrorMsg) { |
| 801 | err() << "Error parsing: " << ErrorMsg << "\n"; |
| 802 | exit(1); |
| 803 | } |