Record.h revision 8dd6f0c8353f80de6526810899f271d539f6929c
1//===- llvm/TableGen/Record.h - Classes for Table Records -------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the main TableGen data structures, including the TableGen 11// types, values, and high-level data structures. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_TABLEGEN_RECORD_H 16#define LLVM_TABLEGEN_RECORD_H 17 18#include "llvm/ADT/ArrayRef.h" 19#include "llvm/ADT/FoldingSet.h" 20#include "llvm/Support/Allocator.h" 21#include "llvm/Support/SourceMgr.h" 22#include "llvm/Support/DataTypes.h" 23#include "llvm/Support/raw_ostream.h" 24#include <map> 25 26namespace llvm { 27class raw_ostream; 28 29// RecTy subclasses. 30class BitRecTy; 31class BitsRecTy; 32class IntRecTy; 33class StringRecTy; 34class ListRecTy; 35class DagRecTy; 36class RecordRecTy; 37 38// Init subclasses. 39class Init; 40class UnsetInit; 41class BitInit; 42class BitsInit; 43class IntInit; 44class StringInit; 45class ListInit; 46class UnOpInit; 47class BinOpInit; 48class TernOpInit; 49class DefInit; 50class DagInit; 51class TypedInit; 52class VarInit; 53class FieldInit; 54class VarBitInit; 55class VarListElementInit; 56 57// Other classes. 58class Record; 59class RecordVal; 60struct MultiClass; 61class RecordKeeper; 62 63//===----------------------------------------------------------------------===// 64// Type Classes 65//===----------------------------------------------------------------------===// 66 67class RecTy { 68 ListRecTy *ListTy; 69 virtual void anchor(); 70public: 71 RecTy() : ListTy(0) {} 72 virtual ~RecTy() {} 73 74 virtual std::string getAsString() const = 0; 75 void print(raw_ostream &OS) const { OS << getAsString(); } 76 void dump() const; 77 78 /// typeIsConvertibleTo - Return true if all values of 'this' type can be 79 /// converted to the specified type. 80 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0; 81 82 /// getListTy - Returns the type representing list<this>. 83 ListRecTy *getListTy(); 84 85public: // These methods should only be called from subclasses of Init 86 virtual Init *convertValue( UnsetInit *UI) { return 0; } 87 virtual Init *convertValue( BitInit *BI) { return 0; } 88 virtual Init *convertValue( BitsInit *BI) { return 0; } 89 virtual Init *convertValue( IntInit *II) { return 0; } 90 virtual Init *convertValue(StringInit *SI) { return 0; } 91 virtual Init *convertValue( ListInit *LI) { return 0; } 92 virtual Init *convertValue( UnOpInit *UI) { 93 return convertValue((TypedInit*)UI); 94 } 95 virtual Init *convertValue( BinOpInit *UI) { 96 return convertValue((TypedInit*)UI); 97 } 98 virtual Init *convertValue( TernOpInit *UI) { 99 return convertValue((TypedInit*)UI); 100 } 101 virtual Init *convertValue(VarBitInit *VB) { return 0; } 102 virtual Init *convertValue( DefInit *DI) { return 0; } 103 virtual Init *convertValue( DagInit *DI) { return 0; } 104 virtual Init *convertValue( TypedInit *TI) { return 0; } 105 virtual Init *convertValue( VarInit *VI) { 106 return convertValue((TypedInit*)VI); 107 } 108 virtual Init *convertValue( FieldInit *FI) { 109 return convertValue((TypedInit*)FI); 110 } 111 112public: // These methods should only be called by subclasses of RecTy. 113 // baseClassOf - These virtual methods should be overloaded to return true iff 114 // all values of type 'RHS' can be converted to the 'this' type. 115 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } 116 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } 117 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } 118 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } 119 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } 120 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } 121 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } 122}; 123 124inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) { 125 Ty.print(OS); 126 return OS; 127} 128 129 130/// BitRecTy - 'bit' - Represent a single bit 131/// 132class BitRecTy : public RecTy { 133 static BitRecTy Shared; 134 BitRecTy() {} 135public: 136 static BitRecTy *get() { return &Shared; } 137 138 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; } 139 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; } 140 virtual Init *convertValue( BitsInit *BI); 141 virtual Init *convertValue( IntInit *II); 142 virtual Init *convertValue(StringInit *SI) { return 0; } 143 virtual Init *convertValue( ListInit *LI) { return 0; } 144 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; } 145 virtual Init *convertValue( DefInit *DI) { return 0; } 146 virtual Init *convertValue( DagInit *DI) { return 0; } 147 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);} 148 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);} 149 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);} 150 virtual Init *convertValue( TypedInit *TI); 151 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} 152 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} 153 154 std::string getAsString() const { return "bit"; } 155 156 bool typeIsConvertibleTo(const RecTy *RHS) const { 157 return RHS->baseClassOf(this); 158 } 159 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; } 160 virtual bool baseClassOf(const BitsRecTy *RHS) const; 161 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; } 162 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } 163 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } 164 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } 165 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } 166 167}; 168 169 170// BitsRecTy - 'bits<n>' - Represent a fixed number of bits 171/// BitsRecTy - 'bits<n>' - Represent a fixed number of bits 172/// 173class BitsRecTy : public RecTy { 174 unsigned Size; 175 explicit BitsRecTy(unsigned Sz) : Size(Sz) {} 176public: 177 static BitsRecTy *get(unsigned Sz); 178 179 unsigned getNumBits() const { return Size; } 180 181 virtual Init *convertValue( UnsetInit *UI); 182 virtual Init *convertValue( BitInit *UI); 183 virtual Init *convertValue( BitsInit *BI); 184 virtual Init *convertValue( IntInit *II); 185 virtual Init *convertValue(StringInit *SI) { return 0; } 186 virtual Init *convertValue( ListInit *LI) { return 0; } 187 virtual Init *convertValue(VarBitInit *VB) { return 0; } 188 virtual Init *convertValue( DefInit *DI) { return 0; } 189 virtual Init *convertValue( DagInit *DI) { return 0; } 190 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);} 191 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);} 192 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);} 193 virtual Init *convertValue( TypedInit *TI); 194 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} 195 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} 196 197 std::string getAsString() const; 198 199 bool typeIsConvertibleTo(const RecTy *RHS) const { 200 return RHS->baseClassOf(this); 201 } 202 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; } 203 virtual bool baseClassOf(const BitsRecTy *RHS) const { 204 return RHS->Size == Size; 205 } 206 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; } 207 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } 208 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } 209 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } 210 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } 211 212}; 213 214 215/// IntRecTy - 'int' - Represent an integer value of no particular size 216/// 217class IntRecTy : public RecTy { 218 static IntRecTy Shared; 219 IntRecTy() {} 220public: 221 static IntRecTy *get() { return &Shared; } 222 223 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; } 224 virtual Init *convertValue( BitInit *BI); 225 virtual Init *convertValue( BitsInit *BI); 226 virtual Init *convertValue( IntInit *II) { return (Init*)II; } 227 virtual Init *convertValue(StringInit *SI) { return 0; } 228 virtual Init *convertValue( ListInit *LI) { return 0; } 229 virtual Init *convertValue(VarBitInit *VB) { return 0; } 230 virtual Init *convertValue( DefInit *DI) { return 0; } 231 virtual Init *convertValue( DagInit *DI) { return 0; } 232 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);} 233 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);} 234 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);} 235 virtual Init *convertValue( TypedInit *TI); 236 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} 237 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} 238 239 std::string getAsString() const { return "int"; } 240 241 bool typeIsConvertibleTo(const RecTy *RHS) const { 242 return RHS->baseClassOf(this); 243 } 244 245 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; } 246 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; } 247 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; } 248 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } 249 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } 250 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } 251 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } 252 253}; 254 255/// StringRecTy - 'string' - Represent an string value 256/// 257class StringRecTy : public RecTy { 258 static StringRecTy Shared; 259 StringRecTy() {} 260public: 261 static StringRecTy *get() { return &Shared; } 262 263 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; } 264 virtual Init *convertValue( BitInit *BI) { return 0; } 265 virtual Init *convertValue( BitsInit *BI) { return 0; } 266 virtual Init *convertValue( IntInit *II) { return 0; } 267 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; } 268 virtual Init *convertValue( ListInit *LI) { return 0; } 269 virtual Init *convertValue( UnOpInit *BO); 270 virtual Init *convertValue( BinOpInit *BO); 271 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);} 272 273 virtual Init *convertValue(VarBitInit *VB) { return 0; } 274 virtual Init *convertValue( DefInit *DI) { return 0; } 275 virtual Init *convertValue( DagInit *DI) { return 0; } 276 virtual Init *convertValue( TypedInit *TI); 277 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} 278 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} 279 280 std::string getAsString() const { return "string"; } 281 282 bool typeIsConvertibleTo(const RecTy *RHS) const { 283 return RHS->baseClassOf(this); 284 } 285 286 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } 287 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } 288 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } 289 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; } 290 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } 291 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } 292 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } 293}; 294 295// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of 296// the specified type. 297/// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must 298/// be of the specified type. 299/// 300class ListRecTy : public RecTy { 301 RecTy *Ty; 302 explicit ListRecTy(RecTy *T) : Ty(T) {} 303 friend ListRecTy *RecTy::getListTy(); 304public: 305 static ListRecTy *get(RecTy *T) { return T->getListTy(); } 306 RecTy *getElementType() const { return Ty; } 307 308 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; } 309 virtual Init *convertValue( BitInit *BI) { return 0; } 310 virtual Init *convertValue( BitsInit *BI) { return 0; } 311 virtual Init *convertValue( IntInit *II) { return 0; } 312 virtual Init *convertValue(StringInit *SI) { return 0; } 313 virtual Init *convertValue( ListInit *LI); 314 virtual Init *convertValue(VarBitInit *VB) { return 0; } 315 virtual Init *convertValue( DefInit *DI) { return 0; } 316 virtual Init *convertValue( DagInit *DI) { return 0; } 317 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);} 318 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);} 319 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);} 320 virtual Init *convertValue( TypedInit *TI); 321 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} 322 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} 323 324 std::string getAsString() const; 325 326 bool typeIsConvertibleTo(const RecTy *RHS) const { 327 return RHS->baseClassOf(this); 328 } 329 330 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } 331 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } 332 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } 333 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } 334 virtual bool baseClassOf(const ListRecTy *RHS) const { 335 return RHS->getElementType()->typeIsConvertibleTo(Ty); 336 } 337 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } 338 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } 339}; 340 341/// DagRecTy - 'dag' - Represent a dag fragment 342/// 343class DagRecTy : public RecTy { 344 static DagRecTy Shared; 345 DagRecTy() {} 346public: 347 static DagRecTy *get() { return &Shared; } 348 349 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; } 350 virtual Init *convertValue( BitInit *BI) { return 0; } 351 virtual Init *convertValue( BitsInit *BI) { return 0; } 352 virtual Init *convertValue( IntInit *II) { return 0; } 353 virtual Init *convertValue(StringInit *SI) { return 0; } 354 virtual Init *convertValue( ListInit *LI) { return 0; } 355 virtual Init *convertValue(VarBitInit *VB) { return 0; } 356 virtual Init *convertValue( DefInit *DI) { return 0; } 357 virtual Init *convertValue( UnOpInit *BO); 358 virtual Init *convertValue( BinOpInit *BO); 359 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);} 360 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; } 361 virtual Init *convertValue( TypedInit *TI); 362 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} 363 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} 364 365 std::string getAsString() const { return "dag"; } 366 367 bool typeIsConvertibleTo(const RecTy *RHS) const { 368 return RHS->baseClassOf(this); 369 } 370 371 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } 372 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } 373 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } 374 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } 375 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } 376 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; } 377 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } 378}; 379 380 381/// RecordRecTy - '[classname]' - Represent an instance of a class, such as: 382/// (R32 X = EAX). 383/// 384class RecordRecTy : public RecTy { 385 Record *Rec; 386 explicit RecordRecTy(Record *R) : Rec(R) {} 387 friend class Record; 388public: 389 static RecordRecTy *get(Record *R); 390 391 Record *getRecord() const { return Rec; } 392 393 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; } 394 virtual Init *convertValue( BitInit *BI) { return 0; } 395 virtual Init *convertValue( BitsInit *BI) { return 0; } 396 virtual Init *convertValue( IntInit *II) { return 0; } 397 virtual Init *convertValue(StringInit *SI) { return 0; } 398 virtual Init *convertValue( ListInit *LI) { return 0; } 399 virtual Init *convertValue(VarBitInit *VB) { return 0; } 400 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);} 401 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);} 402 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);} 403 virtual Init *convertValue( DefInit *DI); 404 virtual Init *convertValue( DagInit *DI) { return 0; } 405 virtual Init *convertValue( TypedInit *VI); 406 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);} 407 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);} 408 409 std::string getAsString() const; 410 411 bool typeIsConvertibleTo(const RecTy *RHS) const { 412 return RHS->baseClassOf(this); 413 } 414 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } 415 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } 416 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } 417 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } 418 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } 419 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } 420 virtual bool baseClassOf(const RecordRecTy *RHS) const; 421}; 422 423/// resolveTypes - Find a common type that T1 and T2 convert to. 424/// Return 0 if no such type exists. 425/// 426RecTy *resolveTypes(RecTy *T1, RecTy *T2); 427 428//===----------------------------------------------------------------------===// 429// Initializer Classes 430//===----------------------------------------------------------------------===// 431 432class Init { 433 Init(const Init &); // Do not define. 434 Init &operator=(const Init &); // Do not define. 435 virtual void anchor(); 436 437protected: 438 Init(void) {} 439 440public: 441 virtual ~Init() {} 442 443 /// isComplete - This virtual method should be overridden by values that may 444 /// not be completely specified yet. 445 virtual bool isComplete() const { return true; } 446 447 /// print - Print out this value. 448 void print(raw_ostream &OS) const { OS << getAsString(); } 449 450 /// getAsString - Convert this value to a string form. 451 virtual std::string getAsString() const = 0; 452 /// getAsUnquotedString - Convert this value to a string form, 453 /// without adding quote markers. This primaruly affects 454 /// StringInits where we will not surround the string value with 455 /// quotes. 456 virtual std::string getAsUnquotedString() const { return getAsString(); } 457 458 /// dump - Debugging method that may be called through a debugger, just 459 /// invokes print on stderr. 460 void dump() const; 461 462 /// convertInitializerTo - This virtual function is a simple call-back 463 /// function that should be overridden to call the appropriate 464 /// RecTy::convertValue method. 465 /// 466 virtual Init *convertInitializerTo(RecTy *Ty) const = 0; 467 468 /// convertInitializerBitRange - This method is used to implement the bitrange 469 /// selection operator. Given an initializer, it selects the specified bits 470 /// out, returning them as a new init of bits type. If it is not legal to use 471 /// the bit subscript operator on this initializer, return null. 472 /// 473 virtual Init * 474 convertInitializerBitRange(const std::vector<unsigned> &Bits) const { 475 return 0; 476 } 477 478 /// convertInitListSlice - This method is used to implement the list slice 479 /// selection operator. Given an initializer, it selects the specified list 480 /// elements, returning them as a new init of list type. If it is not legal 481 /// to take a slice of this, return null. 482 /// 483 virtual Init * 484 convertInitListSlice(const std::vector<unsigned> &Elements) const { 485 return 0; 486 } 487 488 /// getFieldType - This method is used to implement the FieldInit class. 489 /// Implementors of this method should return the type of the named field if 490 /// they are of record type. 491 /// 492 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; } 493 494 /// getFieldInit - This method complements getFieldType to return the 495 /// initializer for the specified field. If getFieldType returns non-null 496 /// this method should return non-null, otherwise it returns null. 497 /// 498 virtual Init *getFieldInit(Record &R, const RecordVal *RV, 499 const std::string &FieldName) const { 500 return 0; 501 } 502 503 /// resolveReferences - This method is used by classes that refer to other 504 /// variables which may not be defined at the time the expression is formed. 505 /// If a value is set for the variable later, this method will be called on 506 /// users of the value to allow the value to propagate out. 507 /// 508 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const { 509 return const_cast<Init *>(this); 510 } 511}; 512 513inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) { 514 I.print(OS); return OS; 515} 516 517/// TypedInit - This is the common super-class of types that have a specific, 518/// explicit, type. 519/// 520class TypedInit : public Init { 521 RecTy *Ty; 522 523 TypedInit(const TypedInit &Other); // Do not define. 524 TypedInit &operator=(const TypedInit &Other); // Do not define. 525 526protected: 527 explicit TypedInit(RecTy *T) : Ty(T) {} 528 529public: 530 RecTy *getType() const { return Ty; } 531 532 virtual Init * 533 convertInitializerBitRange(const std::vector<unsigned> &Bits) const; 534 virtual Init * 535 convertInitListSlice(const std::vector<unsigned> &Elements) const; 536 537 /// getFieldType - This method is used to implement the FieldInit class. 538 /// Implementors of this method should return the type of the named field if 539 /// they are of record type. 540 /// 541 virtual RecTy *getFieldType(const std::string &FieldName) const; 542 543 /// resolveBitReference - This method is used to implement 544 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we 545 /// simply return the resolved value, otherwise we return null. 546 /// 547 virtual Init *resolveBitReference(Record &R, const RecordVal *RV, 548 unsigned Bit) const = 0; 549 550 /// resolveListElementReference - This method is used to implement 551 /// VarListElementInit::resolveReferences. If the list element is resolvable 552 /// now, we return the resolved value, otherwise we return null. 553 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 554 unsigned Elt) const = 0; 555}; 556 557 558/// UnsetInit - ? - Represents an uninitialized value 559/// 560class UnsetInit : public Init { 561 UnsetInit() : Init() {} 562 UnsetInit(const UnsetInit &); // Do not define. 563 UnsetInit &operator=(const UnsetInit &Other); // Do not define. 564 virtual void anchor(); 565 566public: 567 static UnsetInit *get(); 568 569 virtual Init *convertInitializerTo(RecTy *Ty) const { 570 return Ty->convertValue(const_cast<UnsetInit *>(this)); 571 } 572 573 virtual bool isComplete() const { return false; } 574 virtual std::string getAsString() const { return "?"; } 575}; 576 577 578/// BitInit - true/false - Represent a concrete initializer for a bit. 579/// 580class BitInit : public Init { 581 bool Value; 582 583 explicit BitInit(bool V) : Value(V) {} 584 BitInit(const BitInit &Other); // Do not define. 585 BitInit &operator=(BitInit &Other); // Do not define. 586 virtual void anchor(); 587 588public: 589 static BitInit *get(bool V); 590 591 bool getValue() const { return Value; } 592 593 virtual Init *convertInitializerTo(RecTy *Ty) const { 594 return Ty->convertValue(const_cast<BitInit *>(this)); 595 } 596 597 virtual std::string getAsString() const { return Value ? "1" : "0"; } 598}; 599 600/// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value. 601/// It contains a vector of bits, whose size is determined by the type. 602/// 603class BitsInit : public Init, public FoldingSetNode { 604 std::vector<Init*> Bits; 605 606 BitsInit(ArrayRef<Init *> Range) : Bits(Range.begin(), Range.end()) {} 607 608 BitsInit(const BitsInit &Other); // Do not define. 609 BitsInit &operator=(const BitsInit &Other); // Do not define. 610 611public: 612 static BitsInit *get(ArrayRef<Init *> Range); 613 614 void Profile(FoldingSetNodeID &ID) const; 615 616 unsigned getNumBits() const { return Bits.size(); } 617 618 Init *getBit(unsigned Bit) const { 619 assert(Bit < Bits.size() && "Bit index out of range!"); 620 return Bits[Bit]; 621 } 622 623 virtual Init *convertInitializerTo(RecTy *Ty) const { 624 return Ty->convertValue(const_cast<BitsInit *>(this)); 625 } 626 virtual Init * 627 convertInitializerBitRange(const std::vector<unsigned> &Bits) const; 628 629 virtual bool isComplete() const { 630 for (unsigned i = 0; i != getNumBits(); ++i) 631 if (!getBit(i)->isComplete()) return false; 632 return true; 633 } 634 bool allInComplete() const { 635 for (unsigned i = 0; i != getNumBits(); ++i) 636 if (getBit(i)->isComplete()) return false; 637 return true; 638 } 639 virtual std::string getAsString() const; 640 641 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 642}; 643 644 645/// IntInit - 7 - Represent an initalization by a literal integer value. 646/// 647class IntInit : public TypedInit { 648 int64_t Value; 649 650 explicit IntInit(int64_t V) : TypedInit(IntRecTy::get()), Value(V) {} 651 652 IntInit(const IntInit &Other); // Do not define. 653 IntInit &operator=(const IntInit &Other); // Do note define. 654 655public: 656 static IntInit *get(int64_t V); 657 658 int64_t getValue() const { return Value; } 659 660 virtual Init *convertInitializerTo(RecTy *Ty) const { 661 return Ty->convertValue(const_cast<IntInit *>(this)); 662 } 663 virtual Init * 664 convertInitializerBitRange(const std::vector<unsigned> &Bits) const; 665 666 virtual std::string getAsString() const; 667 668 /// resolveBitReference - This method is used to implement 669 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we 670 /// simply return the resolved value, otherwise we return null. 671 /// 672 virtual Init *resolveBitReference(Record &R, const RecordVal *RV, 673 unsigned Bit) const { 674 assert(0 && "Illegal bit reference off int"); 675 return 0; 676 } 677 678 /// resolveListElementReference - This method is used to implement 679 /// VarListElementInit::resolveReferences. If the list element is resolvable 680 /// now, we return the resolved value, otherwise we return null. 681 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 682 unsigned Elt) const { 683 assert(0 && "Illegal element reference off int"); 684 return 0; 685 } 686}; 687 688 689/// StringInit - "foo" - Represent an initialization by a string value. 690/// 691class StringInit : public TypedInit { 692 std::string Value; 693 694 explicit StringInit(const std::string &V) 695 : TypedInit(StringRecTy::get()), Value(V) {} 696 697 StringInit(const StringInit &Other); // Do not define. 698 StringInit &operator=(const StringInit &Other); // Do not define. 699 virtual void anchor(); 700 701public: 702 static StringInit *get(StringRef); 703 704 const std::string &getValue() const { return Value; } 705 706 virtual Init *convertInitializerTo(RecTy *Ty) const { 707 return Ty->convertValue(const_cast<StringInit *>(this)); 708 } 709 710 virtual std::string getAsString() const { return "\"" + Value + "\""; } 711 virtual std::string getAsUnquotedString() const { return Value; } 712 713 /// resolveBitReference - This method is used to implement 714 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we 715 /// simply return the resolved value, otherwise we return null. 716 /// 717 virtual Init *resolveBitReference(Record &R, const RecordVal *RV, 718 unsigned Bit) const { 719 assert(0 && "Illegal bit reference off string"); 720 return 0; 721 } 722 723 /// resolveListElementReference - This method is used to implement 724 /// VarListElementInit::resolveReferences. If the list element is resolvable 725 /// now, we return the resolved value, otherwise we return null. 726 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 727 unsigned Elt) const { 728 assert(0 && "Illegal element reference off string"); 729 return 0; 730 } 731}; 732 733/// ListInit - [AL, AH, CL] - Represent a list of defs 734/// 735class ListInit : public TypedInit, public FoldingSetNode { 736 std::vector<Init*> Values; 737public: 738 typedef std::vector<Init*>::const_iterator const_iterator; 739 740private: 741 explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy) 742 : TypedInit(ListRecTy::get(EltTy)), Values(Range.begin(), Range.end()) {} 743 744 ListInit(const ListInit &Other); // Do not define. 745 ListInit &operator=(const ListInit &Other); // Do not define. 746 747public: 748 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy); 749 750 void Profile(FoldingSetNodeID &ID) const; 751 752 unsigned getSize() const { return Values.size(); } 753 Init *getElement(unsigned i) const { 754 assert(i < Values.size() && "List element index out of range!"); 755 return Values[i]; 756 } 757 758 Record *getElementAsRecord(unsigned i) const; 759 760 Init *convertInitListSlice(const std::vector<unsigned> &Elements) const; 761 762 virtual Init *convertInitializerTo(RecTy *Ty) const { 763 return Ty->convertValue(const_cast<ListInit *>(this)); 764 } 765 766 /// resolveReferences - This method is used by classes that refer to other 767 /// variables which may not be defined at the time they expression is formed. 768 /// If a value is set for the variable later, this method will be called on 769 /// users of the value to allow the value to propagate out. 770 /// 771 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 772 773 virtual std::string getAsString() const; 774 775 ArrayRef<Init*> getValues() const { return Values; } 776 777 inline const_iterator begin() const { return Values.begin(); } 778 inline const_iterator end () const { return Values.end(); } 779 780 inline size_t size () const { return Values.size(); } 781 inline bool empty() const { return Values.empty(); } 782 783 /// resolveBitReference - This method is used to implement 784 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we 785 /// simply return the resolved value, otherwise we return null. 786 /// 787 virtual Init *resolveBitReference(Record &R, const RecordVal *RV, 788 unsigned Bit) const { 789 assert(0 && "Illegal bit reference off list"); 790 return 0; 791 } 792 793 /// resolveListElementReference - This method is used to implement 794 /// VarListElementInit::resolveReferences. If the list element is resolvable 795 /// now, we return the resolved value, otherwise we return null. 796 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 797 unsigned Elt) const; 798}; 799 800 801/// OpInit - Base class for operators 802/// 803class OpInit : public TypedInit { 804 OpInit(const OpInit &Other); // Do not define. 805 OpInit &operator=(OpInit &Other); // Do not define. 806 807protected: 808 explicit OpInit(RecTy *Type) : TypedInit(Type) {} 809 810public: 811 // Clone - Clone this operator, replacing arguments with the new list 812 virtual OpInit *clone(std::vector<Init *> &Operands) const = 0; 813 814 virtual int getNumOperands() const = 0; 815 virtual Init *getOperand(int i) const = 0; 816 817 // Fold - If possible, fold this to a simpler init. Return this if not 818 // possible to fold. 819 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0; 820 821 virtual Init *convertInitializerTo(RecTy *Ty) const { 822 return Ty->convertValue(const_cast<OpInit *>(this)); 823 } 824 825 virtual Init *resolveBitReference(Record &R, const RecordVal *RV, 826 unsigned Bit) const; 827 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 828 unsigned Elt) const; 829}; 830 831 832/// UnOpInit - !op (X) - Transform an init. 833/// 834class UnOpInit : public OpInit { 835public: 836 enum UnaryOp { CAST, HEAD, TAIL, EMPTY }; 837private: 838 UnaryOp Opc; 839 Init *LHS; 840 841 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type) 842 : OpInit(Type), Opc(opc), LHS(lhs) {} 843 844 UnOpInit(const UnOpInit &Other); // Do not define. 845 UnOpInit &operator=(const UnOpInit &Other); // Do not define. 846 847public: 848 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type); 849 850 // Clone - Clone this operator, replacing arguments with the new list 851 virtual OpInit *clone(std::vector<Init *> &Operands) const { 852 assert(Operands.size() == 1 && 853 "Wrong number of operands for unary operation"); 854 return UnOpInit::get(getOpcode(), *Operands.begin(), getType()); 855 } 856 857 int getNumOperands() const { return 1; } 858 Init *getOperand(int i) const { 859 assert(i == 0 && "Invalid operand id for unary operator"); 860 return getOperand(); 861 } 862 863 UnaryOp getOpcode() const { return Opc; } 864 Init *getOperand() const { return LHS; } 865 866 // Fold - If possible, fold this to a simpler init. Return this if not 867 // possible to fold. 868 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const; 869 870 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 871 872 virtual std::string getAsString() const; 873}; 874 875/// BinOpInit - !op (X, Y) - Combine two inits. 876/// 877class BinOpInit : public OpInit { 878public: 879 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, EQ }; 880private: 881 BinaryOp Opc; 882 Init *LHS, *RHS; 883 884 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) : 885 OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {} 886 887 BinOpInit(const BinOpInit &Other); // Do not define. 888 BinOpInit &operator=(const BinOpInit &Other); // Do not define. 889 890public: 891 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs, 892 RecTy *Type); 893 894 // Clone - Clone this operator, replacing arguments with the new list 895 virtual OpInit *clone(std::vector<Init *> &Operands) const { 896 assert(Operands.size() == 2 && 897 "Wrong number of operands for binary operation"); 898 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType()); 899 } 900 901 int getNumOperands() const { return 2; } 902 Init *getOperand(int i) const { 903 assert((i == 0 || i == 1) && "Invalid operand id for binary operator"); 904 if (i == 0) { 905 return getLHS(); 906 } else { 907 return getRHS(); 908 } 909 } 910 911 BinaryOp getOpcode() const { return Opc; } 912 Init *getLHS() const { return LHS; } 913 Init *getRHS() const { return RHS; } 914 915 // Fold - If possible, fold this to a simpler init. Return this if not 916 // possible to fold. 917 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const; 918 919 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 920 921 virtual std::string getAsString() const; 922}; 923 924/// TernOpInit - !op (X, Y, Z) - Combine two inits. 925/// 926class TernOpInit : public OpInit { 927public: 928 enum TernaryOp { SUBST, FOREACH, IF }; 929private: 930 TernaryOp Opc; 931 Init *LHS, *MHS, *RHS; 932 933 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, 934 RecTy *Type) : 935 OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {} 936 937 TernOpInit(const TernOpInit &Other); // Do not define. 938 TernOpInit &operator=(const TernOpInit &Other); // Do not define. 939 940public: 941 static TernOpInit *get(TernaryOp opc, Init *lhs, 942 Init *mhs, Init *rhs, 943 RecTy *Type); 944 945 // Clone - Clone this operator, replacing arguments with the new list 946 virtual OpInit *clone(std::vector<Init *> &Operands) const { 947 assert(Operands.size() == 3 && 948 "Wrong number of operands for ternary operation"); 949 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2], 950 getType()); 951 } 952 953 int getNumOperands() const { return 3; } 954 Init *getOperand(int i) const { 955 assert((i == 0 || i == 1 || i == 2) && 956 "Invalid operand id for ternary operator"); 957 if (i == 0) { 958 return getLHS(); 959 } else if (i == 1) { 960 return getMHS(); 961 } else { 962 return getRHS(); 963 } 964 } 965 966 TernaryOp getOpcode() const { return Opc; } 967 Init *getLHS() const { return LHS; } 968 Init *getMHS() const { return MHS; } 969 Init *getRHS() const { return RHS; } 970 971 // Fold - If possible, fold this to a simpler init. Return this if not 972 // possible to fold. 973 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const; 974 975 virtual bool isComplete() const { return false; } 976 977 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 978 979 virtual std::string getAsString() const; 980}; 981 982 983/// VarInit - 'Opcode' - Represent a reference to an entire variable object. 984/// 985class VarInit : public TypedInit { 986 Init *VarName; 987 988 explicit VarInit(const std::string &VN, RecTy *T) 989 : TypedInit(T), VarName(StringInit::get(VN)) {} 990 explicit VarInit(Init *VN, RecTy *T) 991 : TypedInit(T), VarName(VN) {} 992 993 VarInit(const VarInit &Other); // Do not define. 994 VarInit &operator=(const VarInit &Other); // Do not define. 995 996public: 997 static VarInit *get(const std::string &VN, RecTy *T); 998 static VarInit *get(Init *VN, RecTy *T); 999 1000 virtual Init *convertInitializerTo(RecTy *Ty) const { 1001 return Ty->convertValue(const_cast<VarInit *>(this)); 1002 } 1003 1004 const std::string &getName() const; 1005 Init *getNameInit() const { return VarName; } 1006 std::string getNameInitAsString() const { 1007 return getNameInit()->getAsUnquotedString(); 1008 } 1009 1010 virtual Init *resolveBitReference(Record &R, const RecordVal *RV, 1011 unsigned Bit) const; 1012 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 1013 unsigned Elt) const; 1014 1015 virtual RecTy *getFieldType(const std::string &FieldName) const; 1016 virtual Init *getFieldInit(Record &R, const RecordVal *RV, 1017 const std::string &FieldName) const; 1018 1019 /// resolveReferences - This method is used by classes that refer to other 1020 /// variables which may not be defined at the time they expression is formed. 1021 /// If a value is set for the variable later, this method will be called on 1022 /// users of the value to allow the value to propagate out. 1023 /// 1024 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 1025 1026 virtual std::string getAsString() const { return getName(); } 1027}; 1028 1029 1030/// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field. 1031/// 1032class VarBitInit : public Init { 1033 TypedInit *TI; 1034 unsigned Bit; 1035 1036 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) { 1037 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) && 1038 ((BitsRecTy*)T->getType())->getNumBits() > B && 1039 "Illegal VarBitInit expression!"); 1040 } 1041 1042 VarBitInit(const VarBitInit &Other); // Do not define. 1043 VarBitInit &operator=(const VarBitInit &Other); // Do not define. 1044 1045public: 1046 static VarBitInit *get(TypedInit *T, unsigned B); 1047 1048 virtual Init *convertInitializerTo(RecTy *Ty) const { 1049 return Ty->convertValue(const_cast<VarBitInit *>(this)); 1050 } 1051 1052 TypedInit *getVariable() const { return TI; } 1053 unsigned getBitNum() const { return Bit; } 1054 1055 virtual std::string getAsString() const; 1056 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 1057}; 1058 1059/// VarListElementInit - List[4] - Represent access to one element of a var or 1060/// field. 1061class VarListElementInit : public TypedInit { 1062 TypedInit *TI; 1063 unsigned Element; 1064 1065 VarListElementInit(TypedInit *T, unsigned E) 1066 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()), 1067 TI(T), Element(E) { 1068 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) && 1069 "Illegal VarBitInit expression!"); 1070 } 1071 1072 VarListElementInit(const VarListElementInit &Other); // Do not define. 1073 VarListElementInit &operator=(const VarListElementInit &Other); // Do 1074 // not 1075 // define. 1076 1077public: 1078 static VarListElementInit *get(TypedInit *T, unsigned E); 1079 1080 virtual Init *convertInitializerTo(RecTy *Ty) const { 1081 return Ty->convertValue(const_cast<VarListElementInit *>(this)); 1082 } 1083 1084 TypedInit *getVariable() const { return TI; } 1085 unsigned getElementNum() const { return Element; } 1086 1087 virtual Init *resolveBitReference(Record &R, const RecordVal *RV, 1088 unsigned Bit) const; 1089 1090 /// resolveListElementReference - This method is used to implement 1091 /// VarListElementInit::resolveReferences. If the list element is resolvable 1092 /// now, we return the resolved value, otherwise we return null. 1093 virtual Init *resolveListElementReference(Record &R, 1094 const RecordVal *RV, 1095 unsigned Elt) const; 1096 1097 virtual std::string getAsString() const; 1098 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 1099}; 1100 1101/// DefInit - AL - Represent a reference to a 'def' in the description 1102/// 1103class DefInit : public TypedInit { 1104 Record *Def; 1105 1106 DefInit(Record *D, RecordRecTy *T) : TypedInit(T), Def(D) {} 1107 friend class Record; 1108 1109 DefInit(const DefInit &Other); // Do not define. 1110 DefInit &operator=(const DefInit &Other); // Do not define. 1111 1112public: 1113 static DefInit *get(Record*); 1114 1115 virtual Init *convertInitializerTo(RecTy *Ty) const { 1116 return Ty->convertValue(const_cast<DefInit *>(this)); 1117 } 1118 1119 Record *getDef() const { return Def; } 1120 1121 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits); 1122 1123 virtual RecTy *getFieldType(const std::string &FieldName) const; 1124 virtual Init *getFieldInit(Record &R, const RecordVal *RV, 1125 const std::string &FieldName) const; 1126 1127 virtual std::string getAsString() const; 1128 1129 /// resolveBitReference - This method is used to implement 1130 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we 1131 /// simply return the resolved value, otherwise we return null. 1132 /// 1133 virtual Init *resolveBitReference(Record &R, const RecordVal *RV, 1134 unsigned Bit) const { 1135 assert(0 && "Illegal bit reference off def"); 1136 return 0; 1137 } 1138 1139 /// resolveListElementReference - This method is used to implement 1140 /// VarListElementInit::resolveReferences. If the list element is resolvable 1141 /// now, we return the resolved value, otherwise we return null. 1142 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 1143 unsigned Elt) const { 1144 assert(0 && "Illegal element reference off def"); 1145 return 0; 1146 } 1147}; 1148 1149 1150/// FieldInit - X.Y - Represent a reference to a subfield of a variable 1151/// 1152class FieldInit : public TypedInit { 1153 Init *Rec; // Record we are referring to 1154 std::string FieldName; // Field we are accessing 1155 1156 FieldInit(Init *R, const std::string &FN) 1157 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) { 1158 assert(getType() && "FieldInit with non-record type!"); 1159 } 1160 1161 FieldInit(const FieldInit &Other); // Do not define. 1162 FieldInit &operator=(const FieldInit &Other); // Do not define. 1163 1164public: 1165 static FieldInit *get(Init *R, const std::string &FN); 1166 static FieldInit *get(Init *R, const Init *FN); 1167 1168 virtual Init *convertInitializerTo(RecTy *Ty) const { 1169 return Ty->convertValue(const_cast<FieldInit *>(this)); 1170 } 1171 1172 virtual Init *resolveBitReference(Record &R, const RecordVal *RV, 1173 unsigned Bit) const; 1174 virtual Init *resolveListElementReference(Record &R, 1175 const RecordVal *RV, 1176 unsigned Elt) const; 1177 1178 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 1179 1180 virtual std::string getAsString() const { 1181 return Rec->getAsString() + "." + FieldName; 1182 } 1183}; 1184 1185/// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required 1186/// to have at least one value then a (possibly empty) list of arguments. Each 1187/// argument can have a name associated with it. 1188/// 1189class DagInit : public TypedInit, public FoldingSetNode { 1190 Init *Val; 1191 std::string ValName; 1192 std::vector<Init*> Args; 1193 std::vector<std::string> ArgNames; 1194 1195 DagInit(Init *V, const std::string &VN, 1196 ArrayRef<Init *> ArgRange, 1197 ArrayRef<std::string> NameRange) 1198 : TypedInit(DagRecTy::get()), Val(V), ValName(VN), 1199 Args(ArgRange.begin(), ArgRange.end()), 1200 ArgNames(NameRange.begin(), NameRange.end()) {} 1201 1202 DagInit(const DagInit &Other); // Do not define. 1203 DagInit &operator=(const DagInit &Other); // Do not define. 1204 1205public: 1206 static DagInit *get(Init *V, const std::string &VN, 1207 ArrayRef<Init *> ArgRange, 1208 ArrayRef<std::string> NameRange); 1209 static DagInit *get(Init *V, const std::string &VN, 1210 const std::vector< 1211 std::pair<Init*, std::string> > &args); 1212 1213 void Profile(FoldingSetNodeID &ID) const; 1214 1215 virtual Init *convertInitializerTo(RecTy *Ty) const { 1216 return Ty->convertValue(const_cast<DagInit *>(this)); 1217 } 1218 1219 Init *getOperator() const { return Val; } 1220 1221 const std::string &getName() const { return ValName; } 1222 1223 unsigned getNumArgs() const { return Args.size(); } 1224 Init *getArg(unsigned Num) const { 1225 assert(Num < Args.size() && "Arg number out of range!"); 1226 return Args[Num]; 1227 } 1228 const std::string &getArgName(unsigned Num) const { 1229 assert(Num < ArgNames.size() && "Arg number out of range!"); 1230 return ArgNames[Num]; 1231 } 1232 1233 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const; 1234 1235 virtual std::string getAsString() const; 1236 1237 typedef std::vector<Init*>::const_iterator const_arg_iterator; 1238 typedef std::vector<std::string>::const_iterator const_name_iterator; 1239 1240 inline const_arg_iterator arg_begin() const { return Args.begin(); } 1241 inline const_arg_iterator arg_end () const { return Args.end(); } 1242 1243 inline size_t arg_size () const { return Args.size(); } 1244 inline bool arg_empty() const { return Args.empty(); } 1245 1246 inline const_name_iterator name_begin() const { return ArgNames.begin(); } 1247 inline const_name_iterator name_end () const { return ArgNames.end(); } 1248 1249 inline size_t name_size () const { return ArgNames.size(); } 1250 inline bool name_empty() const { return ArgNames.empty(); } 1251 1252 virtual Init *resolveBitReference(Record &R, const RecordVal *RV, 1253 unsigned Bit) const { 1254 assert(0 && "Illegal bit reference off dag"); 1255 return 0; 1256 } 1257 1258 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV, 1259 unsigned Elt) const { 1260 assert(0 && "Illegal element reference off dag"); 1261 return 0; 1262 } 1263}; 1264 1265//===----------------------------------------------------------------------===// 1266// High-Level Classes 1267//===----------------------------------------------------------------------===// 1268 1269class RecordVal { 1270 Init *Name; 1271 RecTy *Ty; 1272 unsigned Prefix; 1273 Init *Value; 1274public: 1275 RecordVal(Init *N, RecTy *T, unsigned P); 1276 RecordVal(const std::string &N, RecTy *T, unsigned P); 1277 1278 const std::string &getName() const; 1279 const Init *getNameInit() const { return Name; } 1280 std::string getNameInitAsString() const { 1281 return getNameInit()->getAsUnquotedString(); 1282 } 1283 1284 unsigned getPrefix() const { return Prefix; } 1285 RecTy *getType() const { return Ty; } 1286 Init *getValue() const { return Value; } 1287 1288 bool setValue(Init *V) { 1289 if (V) { 1290 Value = V->convertInitializerTo(Ty); 1291 return Value == 0; 1292 } 1293 Value = 0; 1294 return false; 1295 } 1296 1297 void dump() const; 1298 void print(raw_ostream &OS, bool PrintSem = true) const; 1299}; 1300 1301inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) { 1302 RV.print(OS << " "); 1303 return OS; 1304} 1305 1306class Record { 1307 static unsigned LastID; 1308 1309 // Unique record ID. 1310 unsigned ID; 1311 Init *Name; 1312 SMLoc Loc; 1313 std::vector<Init *> TemplateArgs; 1314 std::vector<RecordVal> Values; 1315 std::vector<Record*> SuperClasses; 1316 1317 // Tracks Record instances. Not owned by Record. 1318 RecordKeeper &TrackedRecords; 1319 1320 DefInit *TheInit; 1321 1322 void init(); 1323 void checkName(); 1324 1325public: 1326 1327 // Constructs a record. 1328 explicit Record(const std::string &N, SMLoc loc, RecordKeeper &records) : 1329 ID(LastID++), Name(StringInit::get(N)), Loc(loc), TrackedRecords(records), 1330 TheInit(0) { 1331 init(); 1332 } 1333 explicit Record(Init *N, SMLoc loc, RecordKeeper &records) : 1334 ID(LastID++), Name(N), Loc(loc), TrackedRecords(records), TheInit(0) { 1335 init(); 1336 } 1337 ~Record() {} 1338 1339 1340 static unsigned getNewUID() { return LastID++; } 1341 1342 1343 unsigned getID() const { return ID; } 1344 1345 const std::string &getName() const; 1346 Init *getNameInit() const { 1347 return Name; 1348 } 1349 const std::string getNameInitAsString() const { 1350 return getNameInit()->getAsUnquotedString(); 1351 } 1352 1353 void setName(Init *Name); // Also updates RecordKeeper. 1354 void setName(const std::string &Name); // Also updates RecordKeeper. 1355 1356 SMLoc getLoc() const { return Loc; } 1357 1358 /// get the corresponding DefInit. 1359 DefInit *getDefInit(); 1360 1361 const std::vector<Init *> &getTemplateArgs() const { 1362 return TemplateArgs; 1363 } 1364 const std::vector<RecordVal> &getValues() const { return Values; } 1365 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; } 1366 1367 bool isTemplateArg(Init *Name) const { 1368 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i) 1369 if (TemplateArgs[i] == Name) return true; 1370 return false; 1371 } 1372 bool isTemplateArg(StringRef Name) const { 1373 return isTemplateArg(StringInit::get(Name.str())); 1374 } 1375 1376 const RecordVal *getValue(const Init *Name) const { 1377 for (unsigned i = 0, e = Values.size(); i != e; ++i) 1378 if (Values[i].getNameInit() == Name) return &Values[i]; 1379 return 0; 1380 } 1381 const RecordVal *getValue(StringRef Name) const { 1382 return getValue(StringInit::get(Name)); 1383 } 1384 RecordVal *getValue(const Init *Name) { 1385 for (unsigned i = 0, e = Values.size(); i != e; ++i) 1386 if (Values[i].getNameInit() == Name) return &Values[i]; 1387 return 0; 1388 } 1389 RecordVal *getValue(StringRef Name) { 1390 return getValue(StringInit::get(Name)); 1391 } 1392 1393 void addTemplateArg(Init *Name) { 1394 assert(!isTemplateArg(Name) && "Template arg already defined!"); 1395 TemplateArgs.push_back(Name); 1396 } 1397 void addTemplateArg(StringRef Name) { 1398 addTemplateArg(StringInit::get(Name.str())); 1399 } 1400 1401 void addValue(const RecordVal &RV) { 1402 assert(getValue(RV.getNameInit()) == 0 && "Value already added!"); 1403 Values.push_back(RV); 1404 if (Values.size() > 1) 1405 // Keep NAME at the end of the list. It makes record dumps a 1406 // bit prettier and allows TableGen tests to be written more 1407 // naturally. Tests can use CHECK-NEXT to look for Record 1408 // fields they expect to see after a def. They can't do that if 1409 // NAME is the first Record field. 1410 std::swap(Values[Values.size() - 2], Values[Values.size() - 1]); 1411 } 1412 1413 void removeValue(Init *Name) { 1414 for (unsigned i = 0, e = Values.size(); i != e; ++i) 1415 if (Values[i].getNameInit() == Name) { 1416 Values.erase(Values.begin()+i); 1417 return; 1418 } 1419 assert(0 && "Cannot remove an entry that does not exist!"); 1420 } 1421 1422 void removeValue(StringRef Name) { 1423 removeValue(StringInit::get(Name.str())); 1424 } 1425 1426 bool isSubClassOf(const Record *R) const { 1427 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i) 1428 if (SuperClasses[i] == R) 1429 return true; 1430 return false; 1431 } 1432 1433 bool isSubClassOf(StringRef Name) const { 1434 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i) 1435 if (SuperClasses[i]->getNameInitAsString() == Name) 1436 return true; 1437 return false; 1438 } 1439 1440 void addSuperClass(Record *R) { 1441 assert(!isSubClassOf(R) && "Already subclassing record!"); 1442 SuperClasses.push_back(R); 1443 } 1444 1445 /// resolveReferences - If there are any field references that refer to fields 1446 /// that have been filled in, we can propagate the values now. 1447 /// 1448 void resolveReferences() { resolveReferencesTo(0); } 1449 1450 /// resolveReferencesTo - If anything in this record refers to RV, replace the 1451 /// reference to RV with the RHS of RV. If RV is null, we resolve all 1452 /// possible references. 1453 void resolveReferencesTo(const RecordVal *RV); 1454 1455 RecordKeeper &getRecords() const { 1456 return TrackedRecords; 1457 } 1458 1459 void dump() const; 1460 1461 //===--------------------------------------------------------------------===// 1462 // High-level methods useful to tablegen back-ends 1463 // 1464 1465 /// getValueInit - Return the initializer for a value with the specified name, 1466 /// or throw an exception if the field does not exist. 1467 /// 1468 Init *getValueInit(StringRef FieldName) const; 1469 1470 /// getValueAsString - This method looks up the specified field and returns 1471 /// its value as a string, throwing an exception if the field does not exist 1472 /// or if the value is not a string. 1473 /// 1474 std::string getValueAsString(StringRef FieldName) const; 1475 1476 /// getValueAsBitsInit - This method looks up the specified field and returns 1477 /// its value as a BitsInit, throwing an exception if the field does not exist 1478 /// or if the value is not the right type. 1479 /// 1480 BitsInit *getValueAsBitsInit(StringRef FieldName) const; 1481 1482 /// getValueAsListInit - This method looks up the specified field and returns 1483 /// its value as a ListInit, throwing an exception if the field does not exist 1484 /// or if the value is not the right type. 1485 /// 1486 ListInit *getValueAsListInit(StringRef FieldName) const; 1487 1488 /// getValueAsListOfDefs - This method looks up the specified field and 1489 /// returns its value as a vector of records, throwing an exception if the 1490 /// field does not exist or if the value is not the right type. 1491 /// 1492 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const; 1493 1494 /// getValueAsListOfInts - This method looks up the specified field and 1495 /// returns its value as a vector of integers, throwing an exception if the 1496 /// field does not exist or if the value is not the right type. 1497 /// 1498 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const; 1499 1500 /// getValueAsListOfStrings - This method looks up the specified field and 1501 /// returns its value as a vector of strings, throwing an exception if the 1502 /// field does not exist or if the value is not the right type. 1503 /// 1504 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const; 1505 1506 /// getValueAsDef - This method looks up the specified field and returns its 1507 /// value as a Record, throwing an exception if the field does not exist or if 1508 /// the value is not the right type. 1509 /// 1510 Record *getValueAsDef(StringRef FieldName) const; 1511 1512 /// getValueAsBit - This method looks up the specified field and returns its 1513 /// value as a bit, throwing an exception if the field does not exist or if 1514 /// the value is not the right type. 1515 /// 1516 bool getValueAsBit(StringRef FieldName) const; 1517 1518 /// getValueAsInt - This method looks up the specified field and returns its 1519 /// value as an int64_t, throwing an exception if the field does not exist or 1520 /// if the value is not the right type. 1521 /// 1522 int64_t getValueAsInt(StringRef FieldName) const; 1523 1524 /// getValueAsDag - This method looks up the specified field and returns its 1525 /// value as an Dag, throwing an exception if the field does not exist or if 1526 /// the value is not the right type. 1527 /// 1528 DagInit *getValueAsDag(StringRef FieldName) const; 1529}; 1530 1531raw_ostream &operator<<(raw_ostream &OS, const Record &R); 1532 1533struct MultiClass { 1534 Record Rec; // Placeholder for template args and Name. 1535 typedef std::vector<Record*> RecordVector; 1536 RecordVector DefPrototypes; 1537 1538 void dump() const; 1539 1540 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) : 1541 Rec(Name, Loc, Records) {} 1542}; 1543 1544class RecordKeeper { 1545 std::map<std::string, Record*> Classes, Defs; 1546public: 1547 ~RecordKeeper() { 1548 for (std::map<std::string, Record*>::iterator I = Classes.begin(), 1549 E = Classes.end(); I != E; ++I) 1550 delete I->second; 1551 for (std::map<std::string, Record*>::iterator I = Defs.begin(), 1552 E = Defs.end(); I != E; ++I) 1553 delete I->second; 1554 } 1555 1556 const std::map<std::string, Record*> &getClasses() const { return Classes; } 1557 const std::map<std::string, Record*> &getDefs() const { return Defs; } 1558 1559 Record *getClass(const std::string &Name) const { 1560 std::map<std::string, Record*>::const_iterator I = Classes.find(Name); 1561 return I == Classes.end() ? 0 : I->second; 1562 } 1563 Record *getDef(const std::string &Name) const { 1564 std::map<std::string, Record*>::const_iterator I = Defs.find(Name); 1565 return I == Defs.end() ? 0 : I->second; 1566 } 1567 void addClass(Record *R) { 1568 assert(getClass(R->getNameInitAsString()) == 0 && "Class already exists!"); 1569 Classes.insert(std::make_pair(R->getNameInitAsString(), R)); 1570 } 1571 void addDef(Record *R) { 1572 assert(getDef(R->getNameInitAsString()) == 0 && "Def already exists!"); 1573 Defs.insert(std::make_pair(R->getNameInitAsString(), R)); 1574 } 1575 1576 /// removeClass - Remove, but do not delete, the specified record. 1577 /// 1578 void removeClass(const std::string &Name) { 1579 assert(Classes.count(Name) && "Class does not exist!"); 1580 Classes.erase(Name); 1581 } 1582 /// removeDef - Remove, but do not delete, the specified record. 1583 /// 1584 void removeDef(const std::string &Name) { 1585 assert(Defs.count(Name) && "Def does not exist!"); 1586 Defs.erase(Name); 1587 } 1588 1589 //===--------------------------------------------------------------------===// 1590 // High-level helper methods, useful for tablegen backends... 1591 1592 /// getAllDerivedDefinitions - This method returns all concrete definitions 1593 /// that derive from the specified class name. If a class with the specified 1594 /// name does not exist, an exception is thrown. 1595 std::vector<Record*> 1596 getAllDerivedDefinitions(const std::string &ClassName) const; 1597 1598 void dump() const; 1599}; 1600 1601/// LessRecord - Sorting predicate to sort record pointers by name. 1602/// 1603struct LessRecord { 1604 bool operator()(const Record *Rec1, const Record *Rec2) const { 1605 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0; 1606 } 1607}; 1608 1609/// LessRecordFieldName - Sorting predicate to sort record pointers by their 1610/// name field. 1611/// 1612struct LessRecordFieldName { 1613 bool operator()(const Record *Rec1, const Record *Rec2) const { 1614 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name"); 1615 } 1616}; 1617 1618raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK); 1619 1620/// QualifyName - Return an Init with a qualifier prefix referring 1621/// to CurRec's name. 1622Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass, 1623 Init *Name, const std::string &Scoper); 1624 1625/// QualifyName - Return an Init with a qualifier prefix referring 1626/// to CurRec's name. 1627Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass, 1628 const std::string &Name, const std::string &Scoper); 1629 1630} // End llvm namespace 1631 1632#endif 1633