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