Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame^] | 1 | //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===// |
| 2 | // |
| 3 | // This file defines the main TableGen data structures, including the TableGen |
| 4 | // types, values, and high-level data structures. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 8 | #ifndef RECORD_H |
| 9 | #define RECORD_H |
| 10 | |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | #include <map> |
| 14 | #include <iostream> |
| 15 | #include <cassert> |
| 16 | |
| 17 | // RecTy subclasses... |
| 18 | class BitRecTy; |
| 19 | class BitsRecTy; |
| 20 | class IntRecTy; |
| 21 | class StringRecTy; |
| 22 | class ListRecTy; |
| 23 | class CodeRecTy; |
| 24 | class DagRecTy; |
| 25 | class RecordRecTy; |
| 26 | |
| 27 | // Init subclasses... |
| 28 | class Init; |
| 29 | class UnsetInit; |
| 30 | class BitInit; |
| 31 | class BitsInit; |
| 32 | class IntInit; |
| 33 | class StringInit; |
| 34 | class CodeInit; |
| 35 | class ListInit; |
| 36 | class DefInit; |
| 37 | class DagInit; |
| 38 | class TypedInit; |
| 39 | class VarInit; |
| 40 | class FieldInit; |
| 41 | class VarBitInit; |
| 42 | |
| 43 | // Other classes... |
| 44 | class Record; |
| 45 | |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | // Type Classes |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
| 50 | struct RecTy { |
| 51 | virtual ~RecTy() {} |
| 52 | |
| 53 | virtual void print(std::ostream &OS) const = 0; |
| 54 | void dump() const; |
| 55 | |
| 56 | /// typeIsConvertibleTo - Return true if all values of 'this' type can be |
| 57 | /// converted to the specified type. |
| 58 | virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0; |
| 59 | |
| 60 | public: // These methods should only be called from subclasses of Init |
| 61 | virtual Init *convertValue( UnsetInit *UI) { return 0; } |
| 62 | virtual Init *convertValue( BitInit *BI) { return 0; } |
| 63 | virtual Init *convertValue( BitsInit *BI) { return 0; } |
| 64 | virtual Init *convertValue( IntInit *II) { return 0; } |
| 65 | virtual Init *convertValue(StringInit *SI) { return 0; } |
| 66 | virtual Init *convertValue( ListInit *LI) { return 0; } |
| 67 | virtual Init *convertValue( CodeInit *CI) { return 0; } |
| 68 | virtual Init *convertValue(VarBitInit *VB) { return 0; } |
| 69 | virtual Init *convertValue( DefInit *DI) { return 0; } |
| 70 | virtual Init *convertValue( DagInit *DI) { return 0; } |
| 71 | virtual Init *convertValue( TypedInit *TI) { return 0; } |
| 72 | virtual Init *convertValue( VarInit *VI) { |
| 73 | return convertValue((TypedInit*)VI); |
| 74 | } |
| 75 | virtual Init *convertValue( FieldInit *FI) { |
| 76 | return convertValue((TypedInit*)FI); |
| 77 | } |
| 78 | |
| 79 | public: // These methods should only be called by subclasses of RecTy. |
| 80 | // baseClassOf - These virtual methods should be overloaded to return true iff |
| 81 | // all values of type 'RHS' can be converted to the 'this' type. |
| 82 | virtual bool baseClassOf(const BitRecTy *RHS) const { return false; } |
| 83 | virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; } |
| 84 | virtual bool baseClassOf(const IntRecTy *RHS) const { return false; } |
| 85 | virtual bool baseClassOf(const StringRecTy *RHS) const { return false; } |
| 86 | virtual bool baseClassOf(const ListRecTy *RHS) const { return false; } |
| 87 | virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; } |
| 88 | virtual bool baseClassOf(const DagRecTy *RHS) const { return false; } |
| 89 | virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; } |
| 90 | }; |
| 91 | |
| 92 | inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) { |
| 93 | Ty.print(OS); |
| 94 | return OS; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /// BitRecTy - 'bit' - Represent a single bit |
| 99 | /// |
| 100 | struct BitRecTy : public RecTy { |
| 101 | Init *convertValue(UnsetInit *UI) { return (Init*)UI; } |
| 102 | Init *convertValue(BitInit *BI) { return (Init*)BI; } |
| 103 | Init *convertValue(BitsInit *BI); |
| 104 | Init *convertValue(IntInit *II); |
| 105 | Init *convertValue(TypedInit *VI); |
| 106 | Init *convertValue(VarBitInit *VB) { return (Init*)VB; } |
| 107 | |
| 108 | void print(std::ostream &OS) const { OS << "bit"; } |
| 109 | |
| 110 | bool typeIsConvertibleTo(const RecTy *RHS) const { |
| 111 | return RHS->baseClassOf(this); |
| 112 | } |
| 113 | virtual bool baseClassOf(const BitRecTy *RHS) const { return true; } |
| 114 | virtual bool baseClassOf(const BitsRecTy *RHS) const; |
| 115 | virtual bool baseClassOf(const IntRecTy *RHS) const { return true; } |
| 116 | }; |
| 117 | |
| 118 | |
| 119 | /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits |
| 120 | /// |
| 121 | class BitsRecTy : public RecTy { |
| 122 | unsigned Size; |
| 123 | public: |
| 124 | BitsRecTy(unsigned Sz) : Size(Sz) {} |
| 125 | |
| 126 | unsigned getNumBits() const { return Size; } |
| 127 | |
| 128 | Init *convertValue(UnsetInit *UI); |
| 129 | Init *convertValue(BitInit *UI); |
| 130 | Init *convertValue(BitsInit *BI); |
| 131 | Init *convertValue(IntInit *II); |
| 132 | Init *convertValue(TypedInit *VI); |
| 133 | |
| 134 | void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; } |
| 135 | |
| 136 | bool typeIsConvertibleTo(const RecTy *RHS) const { |
| 137 | return RHS->baseClassOf(this); |
| 138 | } |
| 139 | virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; } |
| 140 | virtual bool baseClassOf(const IntRecTy *RHS) const { return true; } |
| 141 | virtual bool baseClassOf(const BitsRecTy *RHS) const { |
| 142 | return RHS->Size == Size; |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | /// IntRecTy - 'int' - Represent an integer value of no particular size |
| 148 | /// |
| 149 | struct IntRecTy : public RecTy { |
| 150 | Init *convertValue(UnsetInit *UI) { return (Init*)UI; } |
| 151 | Init *convertValue(IntInit *II) { return (Init*)II; } |
| 152 | Init *convertValue(BitInit *BI); |
| 153 | Init *convertValue(BitsInit *BI); |
| 154 | Init *convertValue(TypedInit *TI); |
| 155 | |
| 156 | void print(std::ostream &OS) const { OS << "int"; } |
| 157 | |
| 158 | bool typeIsConvertibleTo(const RecTy *RHS) const { |
| 159 | return RHS->baseClassOf(this); |
| 160 | } |
| 161 | |
| 162 | virtual bool baseClassOf(const BitRecTy *RHS) const { return true; } |
| 163 | virtual bool baseClassOf(const IntRecTy *RHS) const { return true; } |
| 164 | virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; } |
| 165 | }; |
| 166 | |
| 167 | /// StringRecTy - 'string' - Represent an string value |
| 168 | /// |
| 169 | struct StringRecTy : public RecTy { |
| 170 | Init *convertValue(UnsetInit *UI) { return (Init*)UI; } |
| 171 | Init *convertValue(StringInit *SI) { return (Init*)SI; } |
| 172 | Init *convertValue(TypedInit *TI); |
| 173 | void print(std::ostream &OS) const { OS << "string"; } |
| 174 | |
| 175 | bool typeIsConvertibleTo(const RecTy *RHS) const { |
| 176 | return RHS->baseClassOf(this); |
| 177 | } |
| 178 | |
| 179 | virtual bool baseClassOf(const StringRecTy *RHS) const { return true; } |
| 180 | }; |
| 181 | |
| 182 | /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of |
| 183 | /// the specified type. |
| 184 | /// |
| 185 | class ListRecTy : public RecTy { |
| 186 | RecTy *Ty; |
| 187 | public: |
| 188 | ListRecTy(RecTy *T) : Ty(T) {} |
| 189 | |
| 190 | RecTy *getElementType() const { return Ty; } |
| 191 | |
| 192 | Init *convertValue(UnsetInit *UI) { return (Init*)UI; } |
| 193 | Init *convertValue(ListInit *LI); |
| 194 | Init *convertValue(TypedInit *TI); |
| 195 | |
| 196 | void print(std::ostream &OS) const; |
| 197 | |
| 198 | bool typeIsConvertibleTo(const RecTy *RHS) const { |
| 199 | return RHS->baseClassOf(this); |
| 200 | } |
| 201 | |
| 202 | virtual bool baseClassOf(const ListRecTy *RHS) const { |
| 203 | return RHS->getElementType()->typeIsConvertibleTo(Ty); |
| 204 | } |
| 205 | }; |
| 206 | |
| 207 | /// CodeRecTy - 'code' - Represent an code fragment, function or method. |
| 208 | /// |
| 209 | struct CodeRecTy : public RecTy { |
| 210 | Init *convertValue(UnsetInit *UI) { return (Init*)UI; } |
| 211 | Init *convertValue( CodeInit *CI) { return (Init*)CI; } |
| 212 | Init *convertValue(TypedInit *TI); |
| 213 | |
| 214 | void print(std::ostream &OS) const { OS << "code"; } |
| 215 | |
| 216 | bool typeIsConvertibleTo(const RecTy *RHS) const { |
| 217 | return RHS->baseClassOf(this); |
| 218 | } |
| 219 | virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; } |
| 220 | }; |
| 221 | |
| 222 | /// DagRecTy - 'dag' - Represent a dag fragment |
| 223 | /// |
| 224 | struct DagRecTy : public RecTy { |
| 225 | Init *convertValue(UnsetInit *UI) { return (Init*)UI; } |
| 226 | Init *convertValue( DagInit *CI) { return (Init*)CI; } |
| 227 | Init *convertValue(TypedInit *TI); |
| 228 | |
| 229 | void print(std::ostream &OS) const { OS << "dag"; } |
| 230 | |
| 231 | bool typeIsConvertibleTo(const RecTy *RHS) const { |
| 232 | return RHS->baseClassOf(this); |
| 233 | } |
| 234 | virtual bool baseClassOf(const DagRecTy *RHS) const { return true; } |
| 235 | }; |
| 236 | |
| 237 | |
| 238 | /// RecordRecTy - '<classname>' - Represent an instance of a class, such as: |
| 239 | /// (R32 X = EAX). |
| 240 | /// |
| 241 | class RecordRecTy : public RecTy { |
| 242 | Record *Rec; |
| 243 | public: |
| 244 | RecordRecTy(Record *R) : Rec(R) {} |
| 245 | |
| 246 | Record *getRecord() const { return Rec; } |
| 247 | |
| 248 | Init *convertValue(UnsetInit *UI) { return (Init*)UI; } |
| 249 | Init *convertValue( DefInit *DI); |
| 250 | Init *convertValue(TypedInit *VI); |
| 251 | |
| 252 | void print(std::ostream &OS) const; |
| 253 | |
| 254 | bool typeIsConvertibleTo(const RecTy *RHS) const { |
| 255 | return RHS->baseClassOf(this); |
| 256 | } |
| 257 | virtual bool baseClassOf(const RecordRecTy *RHS) const; |
| 258 | }; |
| 259 | |
| 260 | |
| 261 | |
| 262 | //===----------------------------------------------------------------------===// |
| 263 | // Initializer Classes |
| 264 | //===----------------------------------------------------------------------===// |
| 265 | |
| 266 | struct Init { |
| 267 | virtual ~Init() {} |
| 268 | |
| 269 | /// isComplete - This virtual method should be overridden by values that may |
| 270 | /// not be completely specified yet. |
| 271 | virtual bool isComplete() const { return true; } |
| 272 | |
| 273 | /// print - Print out this value. |
| 274 | virtual void print(std::ostream &OS) const = 0; |
| 275 | |
| 276 | /// dump - Debugging method that may be called through a debugger, just |
| 277 | /// invokes print on cerr. |
| 278 | void dump() const; |
| 279 | |
| 280 | /// convertInitializerTo - This virtual function is a simple call-back |
| 281 | /// function that should be overridden to call the appropriate |
| 282 | /// RecTy::convertValue method. |
| 283 | /// |
| 284 | virtual Init *convertInitializerTo(RecTy *Ty) = 0; |
| 285 | |
| 286 | /// convertInitializerBitRange - This method is used to implement the bitrange |
| 287 | /// selection operator. Given an initializer, it selects the specified bits |
| 288 | /// out, returning them as a new init of bits type. If it is not legal to use |
| 289 | /// the bit subscript operator on this initializer, return null. |
| 290 | /// |
| 291 | virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) { |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | /// getFieldType - This method is used to implement the FieldInit class. |
| 296 | /// Implementors of this method should return the type of the named field if |
| 297 | /// they are of record type. |
| 298 | /// |
| 299 | virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; } |
| 300 | |
| 301 | /// getFieldInit - This method complements getFieldType to return the |
| 302 | /// initializer for the specified field. If getFieldType returns non-null |
| 303 | /// this method should return non-null, otherwise it returns null. |
| 304 | /// |
| 305 | virtual Init *getFieldInit(Record &R, const std::string &FieldName) const { |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | /// resolveReferences - This method is used by classes that refer to other |
| 310 | /// variables which may not be defined at the time they expression is formed. |
| 311 | /// If a value is set for the variable later, this method will be called on |
| 312 | /// users of the value to allow the value to propagate out. |
| 313 | /// |
| 314 | virtual Init *resolveReferences(Record &R) { return this; } |
| 315 | }; |
| 316 | |
| 317 | inline std::ostream &operator<<(std::ostream &OS, const Init &I) { |
| 318 | I.print(OS); return OS; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /// UnsetInit - ? - Represents an uninitialized value |
| 323 | /// |
| 324 | struct UnsetInit : public Init { |
| 325 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 326 | return Ty->convertValue(this); |
| 327 | } |
| 328 | |
| 329 | virtual bool isComplete() const { return false; } |
| 330 | virtual void print(std::ostream &OS) const { OS << "?"; } |
| 331 | }; |
| 332 | |
| 333 | |
| 334 | /// BitInit - true/false - Represent a concrete initializer for a bit. |
| 335 | /// |
| 336 | class BitInit : public Init { |
| 337 | bool Value; |
| 338 | public: |
| 339 | BitInit(bool V) : Value(V) {} |
| 340 | |
| 341 | bool getValue() const { return Value; } |
| 342 | |
| 343 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 344 | return Ty->convertValue(this); |
| 345 | } |
| 346 | |
| 347 | virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); } |
| 348 | }; |
| 349 | |
| 350 | /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value. |
| 351 | /// It contains a vector of bits, whose size is determined by the type. |
| 352 | /// |
| 353 | class BitsInit : public Init { |
| 354 | std::vector<Init*> Bits; |
| 355 | public: |
| 356 | BitsInit(unsigned Size) : Bits(Size) {} |
| 357 | |
| 358 | unsigned getNumBits() const { return Bits.size(); } |
| 359 | |
| 360 | Init *getBit(unsigned Bit) const { |
| 361 | assert(Bit < Bits.size() && "Bit index out of range!"); |
| 362 | return Bits[Bit]; |
| 363 | } |
| 364 | void setBit(unsigned Bit, Init *V) { |
| 365 | assert(Bit < Bits.size() && "Bit index out of range!"); |
| 366 | assert(Bits[Bit] == 0 && "Bit already set!"); |
| 367 | Bits[Bit] = V; |
| 368 | } |
| 369 | |
| 370 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 371 | return Ty->convertValue(this); |
| 372 | } |
| 373 | virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits); |
| 374 | |
| 375 | virtual bool isComplete() const { |
| 376 | for (unsigned i = 0; i != getNumBits(); ++i) |
| 377 | if (!getBit(i)->isComplete()) return false; |
| 378 | return true; |
| 379 | } |
| 380 | virtual void print(std::ostream &OS) const; |
| 381 | |
| 382 | virtual Init *resolveReferences(Record &R); |
| 383 | |
| 384 | // printXX - Print this bitstream with the specified format, returning true if |
| 385 | // it is not possible. |
| 386 | bool printInHex(std::ostream &OS) const; |
| 387 | bool printAsVariable(std::ostream &OS) const; |
| 388 | bool printAsUnset(std::ostream &OS) const; |
| 389 | }; |
| 390 | |
| 391 | |
| 392 | /// IntInit - 7 - Represent an initalization by a literal integer value. |
| 393 | /// |
| 394 | class IntInit : public Init { |
| 395 | int Value; |
| 396 | public: |
| 397 | IntInit(int V) : Value(V) {} |
| 398 | |
| 399 | int getValue() const { return Value; } |
| 400 | |
| 401 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 402 | return Ty->convertValue(this); |
| 403 | } |
| 404 | virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits); |
| 405 | |
| 406 | virtual void print(std::ostream &OS) const { OS << Value; } |
| 407 | }; |
| 408 | |
| 409 | |
| 410 | /// StringInit - "foo" - Represent an initialization by a string value. |
| 411 | /// |
| 412 | class StringInit : public Init { |
| 413 | std::string Value; |
| 414 | public: |
| 415 | StringInit(const std::string &V) : Value(V) {} |
| 416 | |
| 417 | const std::string &getValue() const { return Value; } |
| 418 | |
| 419 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 420 | return Ty->convertValue(this); |
| 421 | } |
| 422 | |
| 423 | virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; } |
| 424 | }; |
| 425 | |
| 426 | /// CodeInit - "[{...}]" - Represent a code fragment. |
| 427 | /// |
| 428 | class CodeInit : public Init { |
| 429 | std::string Value; |
| 430 | public: |
| 431 | CodeInit(const std::string &V) : Value(V) {} |
| 432 | |
| 433 | const std::string getValue() const { return Value; } |
| 434 | |
| 435 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 436 | return Ty->convertValue(this); |
| 437 | } |
| 438 | |
| 439 | virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; } |
| 440 | }; |
| 441 | |
| 442 | /// ListInit - [AL, AH, CL] - Represent a list of defs |
| 443 | /// |
| 444 | class ListInit : public Init { |
| 445 | std::vector<Init*> Values; |
| 446 | public: |
| 447 | ListInit(std::vector<Init*> &Vs) { |
| 448 | Values.swap(Vs); |
| 449 | } |
| 450 | |
| 451 | unsigned getSize() const { return Values.size(); } |
| 452 | Init *getElement(unsigned i) const { |
| 453 | assert(i < Values.size() && "List element index out of range!"); |
| 454 | return Values[i]; |
| 455 | } |
| 456 | |
| 457 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 458 | return Ty->convertValue(this); |
| 459 | } |
| 460 | |
| 461 | virtual void print(std::ostream &OS) const; |
| 462 | }; |
| 463 | |
| 464 | |
| 465 | /// TypedInit - This is the common super-class of types that have a specific, |
| 466 | /// explicit, type. |
| 467 | /// |
| 468 | class TypedInit : public Init { |
| 469 | RecTy *Ty; |
| 470 | public: |
| 471 | TypedInit(RecTy *T) : Ty(T) {} |
| 472 | |
| 473 | RecTy *getType() const { return Ty; } |
| 474 | |
| 475 | /// resolveBitReference - This method is used to implement |
| 476 | /// VarBitInit::resolveReferences. If the bit is able to be resolved, we |
| 477 | /// simply return the resolved value, otherwise we return this. |
| 478 | /// |
| 479 | virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0; |
| 480 | }; |
| 481 | |
| 482 | /// VarInit - 'Opcode' - Represent a reference to an entire variable object. |
| 483 | /// |
| 484 | class VarInit : public TypedInit { |
| 485 | std::string VarName; |
| 486 | public: |
| 487 | VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {} |
| 488 | |
| 489 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 490 | return Ty->convertValue(this); |
| 491 | } |
| 492 | |
| 493 | const std::string &getName() const { return VarName; } |
| 494 | |
| 495 | virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits); |
| 496 | |
| 497 | virtual Init *resolveBitReference(Record &R, unsigned Bit); |
| 498 | |
| 499 | virtual RecTy *getFieldType(const std::string &FieldName) const; |
| 500 | virtual Init *getFieldInit(Record &R, const std::string &FieldName) const; |
| 501 | |
| 502 | /// resolveReferences - This method is used by classes that refer to other |
| 503 | /// variables which may not be defined at the time they expression is formed. |
| 504 | /// If a value is set for the variable later, this method will be called on |
| 505 | /// users of the value to allow the value to propagate out. |
| 506 | /// |
| 507 | virtual Init *resolveReferences(Record &R); |
| 508 | |
| 509 | virtual void print(std::ostream &OS) const { OS << VarName; } |
| 510 | }; |
| 511 | |
| 512 | |
| 513 | /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field. |
| 514 | /// |
| 515 | class VarBitInit : public Init { |
| 516 | TypedInit *TI; |
| 517 | unsigned Bit; |
| 518 | public: |
| 519 | VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) { |
| 520 | assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) && |
| 521 | ((BitsRecTy*)T->getType())->getNumBits() > B && |
| 522 | "Illegal VarBitInit expression!"); |
| 523 | } |
| 524 | |
| 525 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 526 | return Ty->convertValue(this); |
| 527 | } |
| 528 | |
| 529 | TypedInit *getVariable() const { return TI; } |
| 530 | unsigned getBitNum() const { return Bit; } |
| 531 | |
| 532 | virtual void print(std::ostream &OS) const { |
| 533 | TI->print(OS); OS << "{" << Bit << "}"; |
| 534 | } |
| 535 | virtual Init *resolveReferences(Record &R); |
| 536 | }; |
| 537 | |
| 538 | |
| 539 | /// DefInit - AL - Represent a reference to a 'def' in the description |
| 540 | /// |
| 541 | class DefInit : public Init { |
| 542 | Record *Def; |
| 543 | public: |
| 544 | DefInit(Record *D) : Def(D) {} |
| 545 | |
| 546 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 547 | return Ty->convertValue(this); |
| 548 | } |
| 549 | |
| 550 | Record *getDef() const { return Def; } |
| 551 | |
| 552 | //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits); |
| 553 | |
| 554 | virtual RecTy *getFieldType(const std::string &FieldName) const; |
| 555 | virtual Init *getFieldInit(Record &R, const std::string &FieldName) const; |
| 556 | |
| 557 | virtual void print(std::ostream &OS) const; |
| 558 | }; |
| 559 | |
| 560 | |
| 561 | /// FieldInit - X.Y - Represent a reference to a subfield of a variable |
| 562 | /// |
| 563 | class FieldInit : public TypedInit { |
| 564 | Init *Rec; // Record we are referring to |
| 565 | std::string FieldName; // Field we are accessing |
| 566 | public: |
| 567 | FieldInit(Init *R, const std::string &FN) |
| 568 | : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) { |
| 569 | assert(getType() && "FieldInit with non-record type!"); |
| 570 | } |
| 571 | |
| 572 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 573 | return Ty->convertValue(this); |
| 574 | } |
| 575 | |
| 576 | virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits); |
| 577 | |
| 578 | virtual Init *resolveBitReference(Record &R, unsigned Bit); |
| 579 | |
| 580 | virtual Init *resolveReferences(Record &R); |
| 581 | |
| 582 | virtual void print(std::ostream &OS) const { |
| 583 | Rec->print(OS); OS << "." << FieldName; |
| 584 | } |
| 585 | }; |
| 586 | |
| 587 | /// DagInit - (def a, b) - Represent a DAG tree value. DAG inits are required |
| 588 | /// to have Records for their first value, after that, any legal Init is |
| 589 | /// possible. |
| 590 | /// |
| 591 | class DagInit : public Init { |
| 592 | Record *NodeTypeDef; |
| 593 | std::vector<Init*> Args; |
| 594 | std::vector<std::string> ArgNames; |
| 595 | public: |
| 596 | DagInit(Record *D, const std::vector<std::pair<Init*, std::string> > &args) |
| 597 | : NodeTypeDef(D) { |
| 598 | Args.reserve(args.size()); |
| 599 | ArgNames.reserve(args.size()); |
| 600 | for (unsigned i = 0, e = args.size(); i != e; ++i) { |
| 601 | Args.push_back(args[i].first); |
| 602 | ArgNames.push_back(args[i].second); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 607 | return Ty->convertValue(this); |
| 608 | } |
| 609 | |
| 610 | Record *getNodeType() const { return NodeTypeDef; } |
| 611 | |
| 612 | unsigned getNumArgs() const { return Args.size(); } |
| 613 | Init *getArg(unsigned Num) const { |
| 614 | assert(Num < Args.size() && "Arg number out of range!"); |
| 615 | return Args[Num]; |
| 616 | } |
| 617 | const std::string &getArgName(unsigned Num) const { |
| 618 | assert(Num < ArgNames.size() && "Arg number out of range!"); |
| 619 | return ArgNames[Num]; |
| 620 | } |
| 621 | |
| 622 | void setArg(unsigned Num, Init *I) { |
| 623 | assert(Num < Args.size() && "Arg number out of range!"); |
| 624 | Args[Num] = I; |
| 625 | } |
| 626 | |
| 627 | virtual void print(std::ostream &OS) const; |
| 628 | }; |
| 629 | |
| 630 | //===----------------------------------------------------------------------===// |
| 631 | // High-Level Classes |
| 632 | //===----------------------------------------------------------------------===// |
| 633 | |
| 634 | class RecordVal { |
| 635 | std::string Name; |
| 636 | RecTy *Ty; |
| 637 | unsigned Prefix; |
| 638 | Init *Value; |
| 639 | public: |
| 640 | RecordVal(const std::string &N, RecTy *T, unsigned P); |
| 641 | |
| 642 | const std::string &getName() const { return Name; } |
| 643 | |
| 644 | unsigned getPrefix() const { return Prefix; } |
| 645 | RecTy *getType() const { return Ty; } |
| 646 | Init *getValue() const { return Value; } |
| 647 | |
| 648 | bool setValue(Init *V) { |
| 649 | if (V) { |
| 650 | Value = V->convertInitializerTo(Ty); |
| 651 | return Value == 0; |
| 652 | } |
| 653 | Value = 0; |
| 654 | return false; |
| 655 | } |
| 656 | |
| 657 | void dump() const; |
| 658 | void print(std::ostream &OS, bool PrintSem = true) const; |
| 659 | }; |
| 660 | |
| 661 | inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) { |
| 662 | RV.print(OS << " "); |
| 663 | return OS; |
| 664 | } |
| 665 | |
| 666 | struct Record { |
| 667 | const std::string Name; |
| 668 | std::vector<std::string> TemplateArgs; |
| 669 | std::vector<RecordVal> Values; |
| 670 | std::vector<Record*> SuperClasses; |
| 671 | public: |
| 672 | |
| 673 | Record(const std::string &N) : Name(N) {} |
| 674 | ~Record() {} |
| 675 | |
| 676 | const std::string &getName() const { return Name; } |
| 677 | const std::vector<std::string> &getTemplateArgs() const { |
| 678 | return TemplateArgs; |
| 679 | } |
| 680 | const std::vector<RecordVal> &getValues() const { return Values; } |
| 681 | const std::vector<Record*> &getSuperClasses() const { return SuperClasses; } |
| 682 | |
| 683 | bool isTemplateArg(const std::string &Name) const { |
| 684 | for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i) |
| 685 | if (TemplateArgs[i] == Name) return true; |
| 686 | return false; |
| 687 | } |
| 688 | |
| 689 | const RecordVal *getValue(const std::string &Name) const { |
| 690 | for (unsigned i = 0, e = Values.size(); i != e; ++i) |
| 691 | if (Values[i].getName() == Name) return &Values[i]; |
| 692 | return 0; |
| 693 | } |
| 694 | RecordVal *getValue(const std::string &Name) { |
| 695 | for (unsigned i = 0, e = Values.size(); i != e; ++i) |
| 696 | if (Values[i].getName() == Name) return &Values[i]; |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | void addTemplateArg(const std::string &Name) { |
| 701 | assert(!isTemplateArg(Name) && "Template arg already defined!"); |
| 702 | TemplateArgs.push_back(Name); |
| 703 | } |
| 704 | |
| 705 | void addValue(const RecordVal &RV) { |
| 706 | assert(getValue(RV.getName()) == 0 && "Value already added!"); |
| 707 | Values.push_back(RV); |
| 708 | } |
| 709 | |
| 710 | void removeValue(const std::string &Name) { |
| 711 | assert(getValue(Name) && "Cannot remove an entry that does not exist!"); |
| 712 | for (unsigned i = 0, e = Values.size(); i != e; ++i) |
| 713 | if (Values[i].getName() == Name) { |
| 714 | Values.erase(Values.begin()+i); |
| 715 | return; |
| 716 | } |
| 717 | assert(0 && "Name does not exist in record!"); |
| 718 | } |
| 719 | |
| 720 | bool isSubClassOf(Record *R) const { |
| 721 | for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i) |
| 722 | if (SuperClasses[i] == R) |
| 723 | return true; |
| 724 | return false; |
| 725 | } |
| 726 | |
| 727 | bool isSubClassOf(const std::string &Name) const { |
| 728 | for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i) |
| 729 | if (SuperClasses[i]->getName() == Name) |
| 730 | return true; |
| 731 | return false; |
| 732 | } |
| 733 | |
| 734 | void addSuperClass(Record *R) { |
| 735 | assert(!isSubClassOf(R) && "Already subclassing record!"); |
| 736 | SuperClasses.push_back(R); |
| 737 | } |
| 738 | |
| 739 | // resolveReferences - If there are any field references that refer to fields |
| 740 | // that have been filled in, we can propagate the values now. |
| 741 | // |
| 742 | void resolveReferences(); |
| 743 | |
| 744 | void dump() const; |
| 745 | |
| 746 | //===--------------------------------------------------------------------===// |
| 747 | // High-level methods useful to tablegen back-ends |
| 748 | // |
| 749 | |
| 750 | /// getValueInit - Return the initializer for a value with the specified name, |
| 751 | /// or throw an exception if the field does not exist. |
| 752 | /// |
| 753 | Init *getValueInit(const std::string &FieldName) const; |
| 754 | |
| 755 | /// getValueAsString - This method looks up the specified field and returns |
| 756 | /// its value as a string, throwing an exception if the field does not exist |
| 757 | /// or if the value is not a string. |
| 758 | /// |
| 759 | std::string getValueAsString(const std::string &FieldName) const; |
| 760 | |
| 761 | /// getValueAsBitsInit - This method looks up the specified field and returns |
| 762 | /// its value as a BitsInit, throwing an exception if the field does not exist |
| 763 | /// or if the value is not the right type. |
| 764 | /// |
| 765 | BitsInit *getValueAsBitsInit(const std::string &FieldName) const; |
| 766 | |
| 767 | /// getValueAsListInit - This method looks up the specified field and returns |
| 768 | /// its value as a ListInit, throwing an exception if the field does not exist |
| 769 | /// or if the value is not the right type. |
| 770 | /// |
| 771 | ListInit *getValueAsListInit(const std::string &FieldName) const; |
| 772 | |
| 773 | /// getValueAsDef - This method looks up the specified field and returns its |
| 774 | /// value as a Record, throwing an exception if the field does not exist or if |
| 775 | /// the value is not the right type. |
| 776 | /// |
| 777 | Record *getValueAsDef(const std::string &FieldName) const; |
| 778 | |
| 779 | /// getValueAsBit - This method looks up the specified field and returns its |
| 780 | /// value as a bit, throwing an exception if the field does not exist or if |
| 781 | /// the value is not the right type. |
| 782 | /// |
| 783 | bool getValueAsBit(const std::string &FieldName) const; |
| 784 | |
| 785 | /// getValueAsInt - This method looks up the specified field and returns its |
| 786 | /// value as an int, throwing an exception if the field does not exist or if |
| 787 | /// the value is not the right type. |
| 788 | /// |
| 789 | int getValueAsInt(const std::string &FieldName) const; |
| 790 | |
| 791 | /// getValueAsDag - This method looks up the specified field and returns its |
| 792 | /// value as an Dag, throwing an exception if the field does not exist or if |
| 793 | /// the value is not the right type. |
| 794 | /// |
| 795 | DagInit *getValueAsDag(const std::string &FieldName) const; |
| 796 | }; |
| 797 | |
| 798 | std::ostream &operator<<(std::ostream &OS, const Record &R); |
| 799 | |
| 800 | class RecordKeeper { |
| 801 | std::map<std::string, Record*> Classes, Defs; |
| 802 | public: |
| 803 | ~RecordKeeper() { |
| 804 | for (std::map<std::string, Record*>::iterator I = Classes.begin(), |
| 805 | E = Classes.end(); I != E; ++I) |
| 806 | delete I->second; |
| 807 | for (std::map<std::string, Record*>::iterator I = Defs.begin(), |
| 808 | E = Defs.end(); I != E; ++I) |
| 809 | delete I->second; |
| 810 | } |
| 811 | |
| 812 | const std::map<std::string, Record*> &getClasses() const { return Classes; } |
| 813 | const std::map<std::string, Record*> &getDefs() const { return Defs; } |
| 814 | |
| 815 | Record *getClass(const std::string &Name) const { |
| 816 | std::map<std::string, Record*>::const_iterator I = Classes.find(Name); |
| 817 | return I == Classes.end() ? 0 : I->second; |
| 818 | } |
| 819 | Record *getDef(const std::string &Name) const { |
| 820 | std::map<std::string, Record*>::const_iterator I = Defs.find(Name); |
| 821 | return I == Defs.end() ? 0 : I->second; |
| 822 | } |
| 823 | void addClass(Record *R) { |
| 824 | assert(getClass(R->getName()) == 0 && "Class already exists!"); |
| 825 | Classes.insert(std::make_pair(R->getName(), R)); |
| 826 | } |
| 827 | void addDef(Record *R) { |
| 828 | assert(getDef(R->getName()) == 0 && "Def already exists!"); |
| 829 | Defs.insert(std::make_pair(R->getName(), R)); |
| 830 | } |
| 831 | |
| 832 | //===--------------------------------------------------------------------===// |
| 833 | // High-level helper methods, useful for tablegen backends... |
| 834 | |
| 835 | /// getAllDerivedDefinitions - This method returns all concrete definitions |
| 836 | /// that derive from the specified class name. If a class with the specified |
| 837 | /// name does not exist, an exception is thrown. |
| 838 | std::vector<Record*> |
| 839 | getAllDerivedDefinitions(const std::string &ClassName) const; |
| 840 | |
| 841 | |
| 842 | void dump() const; |
| 843 | }; |
| 844 | |
| 845 | std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK); |
| 846 | |
| 847 | extern RecordKeeper Records; |
| 848 | |
| 849 | #endif |