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