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