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