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 | |
| 570 | virtual void print(std::ostream &OS) const { OS << Value; } |
| 571 | }; |
| 572 | |
| 573 | |
| 574 | /// StringInit - "foo" - Represent an initialization by a string value. |
| 575 | /// |
| 576 | class StringInit : public Init { |
| 577 | std::string Value; |
| 578 | public: |
| 579 | StringInit(const std::string &V) : Value(V) {} |
| 580 | |
| 581 | const std::string &getValue() const { return Value; } |
| 582 | |
| 583 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 584 | return Ty->convertValue(this); |
| 585 | } |
| 586 | |
| 587 | virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; } |
| 588 | }; |
| 589 | |
| 590 | /// CodeInit - "[{...}]" - Represent a code fragment. |
| 591 | /// |
| 592 | class CodeInit : public Init { |
| 593 | std::string Value; |
| 594 | public: |
| 595 | CodeInit(const std::string &V) : Value(V) {} |
| 596 | |
| 597 | const std::string getValue() const { return Value; } |
| 598 | |
| 599 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 600 | return Ty->convertValue(this); |
| 601 | } |
| 602 | |
| 603 | virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; } |
| 604 | }; |
| 605 | |
| 606 | /// ListInit - [AL, AH, CL] - Represent a list of defs |
| 607 | /// |
| 608 | class ListInit : public Init { |
| 609 | std::vector<Init*> Values; |
| 610 | public: |
| 611 | ListInit(std::vector<Init*> &Vs) { |
| 612 | Values.swap(Vs); |
| 613 | } |
| 614 | |
| 615 | unsigned getSize() const { return Values.size(); } |
| 616 | Init *getElement(unsigned i) const { |
| 617 | assert(i < Values.size() && "List element index out of range!"); |
| 618 | return Values[i]; |
| 619 | } |
| 620 | |
Chris Lattner | 8bf9e06 | 2004-07-26 23:21:34 +0000 | [diff] [blame] | 621 | Init *convertInitListSlice(const std::vector<unsigned> &Elements); |
| 622 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 623 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 624 | return Ty->convertValue(this); |
| 625 | } |
| 626 | |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 627 | /// resolveReferences - This method is used by classes that refer to other |
| 628 | /// variables which may not be defined at the time they expression is formed. |
| 629 | /// If a value is set for the variable later, this method will be called on |
| 630 | /// users of the value to allow the value to propagate out. |
| 631 | /// |
| 632 | virtual Init *resolveReferences(Record &R); |
| 633 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 634 | virtual void print(std::ostream &OS) const; |
| 635 | }; |
| 636 | |
| 637 | |
| 638 | /// TypedInit - This is the common super-class of types that have a specific, |
| 639 | /// explicit, type. |
| 640 | /// |
| 641 | class TypedInit : public Init { |
| 642 | RecTy *Ty; |
| 643 | public: |
| 644 | TypedInit(RecTy *T) : Ty(T) {} |
| 645 | |
| 646 | RecTy *getType() const { return Ty; } |
| 647 | |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 648 | virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits); |
| 649 | virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements); |
| 650 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 651 | /// resolveBitReference - This method is used to implement |
| 652 | /// VarBitInit::resolveReferences. If the bit is able to be resolved, we |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 653 | /// simply return the resolved value, otherwise we return null. |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 654 | /// |
| 655 | virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0; |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 656 | |
| 657 | /// resolveListElementReference - This method is used to implement |
| 658 | /// VarListElementInit::resolveReferences. If the list element is resolvable |
| 659 | /// now, we return the resolved value, otherwise we return null. |
| 660 | virtual Init *resolveListElementReference(Record &R, unsigned Elt) = 0; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 661 | }; |
| 662 | |
| 663 | /// VarInit - 'Opcode' - Represent a reference to an entire variable object. |
| 664 | /// |
| 665 | class VarInit : public TypedInit { |
| 666 | std::string VarName; |
| 667 | public: |
| 668 | VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {} |
| 669 | |
| 670 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 671 | return Ty->convertValue(this); |
| 672 | } |
| 673 | |
| 674 | const std::string &getName() const { return VarName; } |
| 675 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 676 | virtual Init *resolveBitReference(Record &R, unsigned Bit); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 677 | virtual Init *resolveListElementReference(Record &R, unsigned Elt); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 678 | |
| 679 | virtual RecTy *getFieldType(const std::string &FieldName) const; |
| 680 | virtual Init *getFieldInit(Record &R, const std::string &FieldName) const; |
| 681 | |
| 682 | /// resolveReferences - This method is used by classes that refer to other |
| 683 | /// variables which may not be defined at the time they expression is formed. |
| 684 | /// If a value is set for the variable later, this method will be called on |
| 685 | /// users of the value to allow the value to propagate out. |
| 686 | /// |
| 687 | virtual Init *resolveReferences(Record &R); |
| 688 | |
| 689 | virtual void print(std::ostream &OS) const { OS << VarName; } |
| 690 | }; |
| 691 | |
| 692 | |
| 693 | /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field. |
| 694 | /// |
| 695 | class VarBitInit : public Init { |
| 696 | TypedInit *TI; |
| 697 | unsigned Bit; |
| 698 | public: |
| 699 | VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) { |
| 700 | assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) && |
| 701 | ((BitsRecTy*)T->getType())->getNumBits() > B && |
| 702 | "Illegal VarBitInit expression!"); |
| 703 | } |
| 704 | |
| 705 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 706 | return Ty->convertValue(this); |
| 707 | } |
| 708 | |
| 709 | TypedInit *getVariable() const { return TI; } |
| 710 | unsigned getBitNum() const { return Bit; } |
| 711 | |
| 712 | virtual void print(std::ostream &OS) const { |
| 713 | TI->print(OS); OS << "{" << Bit << "}"; |
| 714 | } |
| 715 | virtual Init *resolveReferences(Record &R); |
| 716 | }; |
| 717 | |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 718 | /// VarListElementInit - List[4] - Represent access to one element of a var or |
| 719 | /// field. |
| 720 | class VarListElementInit : public TypedInit { |
| 721 | TypedInit *TI; |
| 722 | unsigned Element; |
| 723 | public: |
| 724 | VarListElementInit(TypedInit *T, unsigned E) |
| 725 | : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()), |
| 726 | TI(T), Element(E) { |
| 727 | assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) && |
| 728 | "Illegal VarBitInit expression!"); |
| 729 | } |
| 730 | |
| 731 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 732 | return Ty->convertValue(this); |
| 733 | } |
| 734 | |
| 735 | TypedInit *getVariable() const { return TI; } |
| 736 | unsigned getElementNum() const { return Element; } |
| 737 | |
| 738 | virtual Init *resolveBitReference(Record &R, unsigned Bit); |
| 739 | |
| 740 | /// resolveListElementReference - This method is used to implement |
| 741 | /// VarListElementInit::resolveReferences. If the list element is resolvable |
| 742 | /// now, we return the resolved value, otherwise we return null. |
| 743 | virtual Init *resolveListElementReference(Record &R, unsigned Elt); |
| 744 | |
| 745 | virtual void print(std::ostream &OS) const { |
| 746 | TI->print(OS); OS << "[" << Element << "]"; |
| 747 | } |
| 748 | virtual Init *resolveReferences(Record &R); |
| 749 | }; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 750 | |
| 751 | /// DefInit - AL - Represent a reference to a 'def' in the description |
| 752 | /// |
| 753 | class DefInit : public Init { |
| 754 | Record *Def; |
| 755 | public: |
| 756 | DefInit(Record *D) : Def(D) {} |
| 757 | |
| 758 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 759 | return Ty->convertValue(this); |
| 760 | } |
| 761 | |
| 762 | Record *getDef() const { return Def; } |
| 763 | |
| 764 | //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits); |
| 765 | |
| 766 | virtual RecTy *getFieldType(const std::string &FieldName) const; |
| 767 | virtual Init *getFieldInit(Record &R, const std::string &FieldName) const; |
| 768 | |
| 769 | virtual void print(std::ostream &OS) const; |
| 770 | }; |
| 771 | |
| 772 | |
| 773 | /// FieldInit - X.Y - Represent a reference to a subfield of a variable |
| 774 | /// |
| 775 | class FieldInit : public TypedInit { |
| 776 | Init *Rec; // Record we are referring to |
| 777 | std::string FieldName; // Field we are accessing |
| 778 | public: |
| 779 | FieldInit(Init *R, const std::string &FN) |
| 780 | : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) { |
| 781 | assert(getType() && "FieldInit with non-record type!"); |
| 782 | } |
| 783 | |
| 784 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 785 | return Ty->convertValue(this); |
| 786 | } |
| 787 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 788 | virtual Init *resolveBitReference(Record &R, unsigned Bit); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 789 | virtual Init *resolveListElementReference(Record &R, unsigned Elt); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 790 | |
| 791 | virtual Init *resolveReferences(Record &R); |
| 792 | |
| 793 | virtual void print(std::ostream &OS) const { |
| 794 | Rec->print(OS); OS << "." << FieldName; |
| 795 | } |
| 796 | }; |
| 797 | |
| 798 | /// DagInit - (def a, b) - Represent a DAG tree value. DAG inits are required |
| 799 | /// to have Records for their first value, after that, any legal Init is |
| 800 | /// possible. |
| 801 | /// |
| 802 | class DagInit : public Init { |
| 803 | Record *NodeTypeDef; |
| 804 | std::vector<Init*> Args; |
| 805 | std::vector<std::string> ArgNames; |
| 806 | public: |
| 807 | DagInit(Record *D, const std::vector<std::pair<Init*, std::string> > &args) |
| 808 | : NodeTypeDef(D) { |
| 809 | Args.reserve(args.size()); |
| 810 | ArgNames.reserve(args.size()); |
| 811 | for (unsigned i = 0, e = args.size(); i != e; ++i) { |
| 812 | Args.push_back(args[i].first); |
| 813 | ArgNames.push_back(args[i].second); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | virtual Init *convertInitializerTo(RecTy *Ty) { |
| 818 | return Ty->convertValue(this); |
| 819 | } |
| 820 | |
| 821 | Record *getNodeType() const { return NodeTypeDef; } |
| 822 | |
| 823 | unsigned getNumArgs() const { return Args.size(); } |
| 824 | Init *getArg(unsigned Num) const { |
| 825 | assert(Num < Args.size() && "Arg number out of range!"); |
| 826 | return Args[Num]; |
| 827 | } |
| 828 | const std::string &getArgName(unsigned Num) const { |
| 829 | assert(Num < ArgNames.size() && "Arg number out of range!"); |
| 830 | return ArgNames[Num]; |
| 831 | } |
| 832 | |
| 833 | void setArg(unsigned Num, Init *I) { |
| 834 | assert(Num < Args.size() && "Arg number out of range!"); |
| 835 | Args[Num] = I; |
| 836 | } |
| 837 | |
| 838 | virtual void print(std::ostream &OS) const; |
| 839 | }; |
| 840 | |
| 841 | //===----------------------------------------------------------------------===// |
| 842 | // High-Level Classes |
| 843 | //===----------------------------------------------------------------------===// |
| 844 | |
| 845 | class RecordVal { |
| 846 | std::string Name; |
| 847 | RecTy *Ty; |
| 848 | unsigned Prefix; |
| 849 | Init *Value; |
| 850 | public: |
| 851 | RecordVal(const std::string &N, RecTy *T, unsigned P); |
| 852 | |
| 853 | const std::string &getName() const { return Name; } |
| 854 | |
| 855 | unsigned getPrefix() const { return Prefix; } |
| 856 | RecTy *getType() const { return Ty; } |
| 857 | Init *getValue() const { return Value; } |
| 858 | |
| 859 | bool setValue(Init *V) { |
| 860 | if (V) { |
| 861 | Value = V->convertInitializerTo(Ty); |
| 862 | return Value == 0; |
| 863 | } |
| 864 | Value = 0; |
| 865 | return false; |
| 866 | } |
| 867 | |
| 868 | void dump() const; |
| 869 | void print(std::ostream &OS, bool PrintSem = true) const; |
| 870 | }; |
| 871 | |
| 872 | inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) { |
| 873 | RV.print(OS << " "); |
| 874 | return OS; |
| 875 | } |
| 876 | |
Chris Lattner | 87a1061 | 2004-10-23 04:58:50 +0000 | [diff] [blame] | 877 | class Record { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 878 | const std::string Name; |
| 879 | std::vector<std::string> TemplateArgs; |
| 880 | std::vector<RecordVal> Values; |
| 881 | std::vector<Record*> SuperClasses; |
| 882 | public: |
| 883 | |
| 884 | Record(const std::string &N) : Name(N) {} |
| 885 | ~Record() {} |
| 886 | |
| 887 | const std::string &getName() const { return Name; } |
| 888 | const std::vector<std::string> &getTemplateArgs() const { |
| 889 | return TemplateArgs; |
| 890 | } |
| 891 | const std::vector<RecordVal> &getValues() const { return Values; } |
| 892 | const std::vector<Record*> &getSuperClasses() const { return SuperClasses; } |
| 893 | |
| 894 | bool isTemplateArg(const std::string &Name) const { |
| 895 | for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i) |
| 896 | if (TemplateArgs[i] == Name) return true; |
| 897 | return false; |
| 898 | } |
| 899 | |
| 900 | const RecordVal *getValue(const std::string &Name) const { |
| 901 | for (unsigned i = 0, e = Values.size(); i != e; ++i) |
| 902 | if (Values[i].getName() == Name) return &Values[i]; |
| 903 | return 0; |
| 904 | } |
| 905 | RecordVal *getValue(const std::string &Name) { |
| 906 | for (unsigned i = 0, e = Values.size(); i != e; ++i) |
| 907 | if (Values[i].getName() == Name) return &Values[i]; |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | void addTemplateArg(const std::string &Name) { |
| 912 | assert(!isTemplateArg(Name) && "Template arg already defined!"); |
| 913 | TemplateArgs.push_back(Name); |
| 914 | } |
| 915 | |
| 916 | void addValue(const RecordVal &RV) { |
| 917 | assert(getValue(RV.getName()) == 0 && "Value already added!"); |
| 918 | Values.push_back(RV); |
| 919 | } |
| 920 | |
| 921 | void removeValue(const std::string &Name) { |
| 922 | assert(getValue(Name) && "Cannot remove an entry that does not exist!"); |
| 923 | for (unsigned i = 0, e = Values.size(); i != e; ++i) |
| 924 | if (Values[i].getName() == Name) { |
| 925 | Values.erase(Values.begin()+i); |
| 926 | return; |
| 927 | } |
| 928 | assert(0 && "Name does not exist in record!"); |
| 929 | } |
| 930 | |
| 931 | bool isSubClassOf(Record *R) const { |
| 932 | for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i) |
| 933 | if (SuperClasses[i] == R) |
| 934 | return true; |
| 935 | return false; |
| 936 | } |
| 937 | |
| 938 | bool isSubClassOf(const std::string &Name) const { |
| 939 | for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i) |
| 940 | if (SuperClasses[i]->getName() == Name) |
| 941 | return true; |
| 942 | return false; |
| 943 | } |
| 944 | |
| 945 | void addSuperClass(Record *R) { |
| 946 | assert(!isSubClassOf(R) && "Already subclassing record!"); |
| 947 | SuperClasses.push_back(R); |
| 948 | } |
| 949 | |
| 950 | // resolveReferences - If there are any field references that refer to fields |
| 951 | // that have been filled in, we can propagate the values now. |
| 952 | // |
| 953 | void resolveReferences(); |
| 954 | |
| 955 | void dump() const; |
| 956 | |
| 957 | //===--------------------------------------------------------------------===// |
| 958 | // High-level methods useful to tablegen back-ends |
| 959 | // |
| 960 | |
| 961 | /// getValueInit - Return the initializer for a value with the specified name, |
| 962 | /// or throw an exception if the field does not exist. |
| 963 | /// |
| 964 | Init *getValueInit(const std::string &FieldName) const; |
| 965 | |
| 966 | /// getValueAsString - This method looks up the specified field and returns |
| 967 | /// its value as a string, throwing an exception if the field does not exist |
| 968 | /// or if the value is not a string. |
| 969 | /// |
| 970 | std::string getValueAsString(const std::string &FieldName) const; |
| 971 | |
| 972 | /// getValueAsBitsInit - This method looks up the specified field and returns |
| 973 | /// its value as a BitsInit, throwing an exception if the field does not exist |
| 974 | /// or if the value is not the right type. |
| 975 | /// |
| 976 | BitsInit *getValueAsBitsInit(const std::string &FieldName) const; |
| 977 | |
| 978 | /// getValueAsListInit - This method looks up the specified field and returns |
| 979 | /// its value as a ListInit, throwing an exception if the field does not exist |
| 980 | /// or if the value is not the right type. |
| 981 | /// |
| 982 | ListInit *getValueAsListInit(const std::string &FieldName) const; |
| 983 | |
| 984 | /// getValueAsDef - This method looks up the specified field and returns its |
| 985 | /// value as a Record, throwing an exception if the field does not exist or if |
| 986 | /// the value is not the right type. |
| 987 | /// |
| 988 | Record *getValueAsDef(const std::string &FieldName) const; |
| 989 | |
| 990 | /// getValueAsBit - This method looks up the specified field and returns its |
| 991 | /// value as a bit, throwing an exception if the field does not exist or if |
| 992 | /// the value is not the right type. |
| 993 | /// |
| 994 | bool getValueAsBit(const std::string &FieldName) const; |
| 995 | |
| 996 | /// getValueAsInt - This method looks up the specified field and returns its |
| 997 | /// value as an int, throwing an exception if the field does not exist or if |
| 998 | /// the value is not the right type. |
| 999 | /// |
| 1000 | int getValueAsInt(const std::string &FieldName) const; |
| 1001 | |
| 1002 | /// getValueAsDag - This method looks up the specified field and returns its |
| 1003 | /// value as an Dag, throwing an exception if the field does not exist or if |
| 1004 | /// the value is not the right type. |
| 1005 | /// |
| 1006 | DagInit *getValueAsDag(const std::string &FieldName) const; |
| 1007 | }; |
| 1008 | |
| 1009 | std::ostream &operator<<(std::ostream &OS, const Record &R); |
| 1010 | |
| 1011 | class RecordKeeper { |
| 1012 | std::map<std::string, Record*> Classes, Defs; |
| 1013 | public: |
| 1014 | ~RecordKeeper() { |
| 1015 | for (std::map<std::string, Record*>::iterator I = Classes.begin(), |
| 1016 | E = Classes.end(); I != E; ++I) |
| 1017 | delete I->second; |
| 1018 | for (std::map<std::string, Record*>::iterator I = Defs.begin(), |
| 1019 | E = Defs.end(); I != E; ++I) |
| 1020 | delete I->second; |
| 1021 | } |
| 1022 | |
| 1023 | const std::map<std::string, Record*> &getClasses() const { return Classes; } |
| 1024 | const std::map<std::string, Record*> &getDefs() const { return Defs; } |
| 1025 | |
| 1026 | Record *getClass(const std::string &Name) const { |
| 1027 | std::map<std::string, Record*>::const_iterator I = Classes.find(Name); |
| 1028 | return I == Classes.end() ? 0 : I->second; |
| 1029 | } |
| 1030 | Record *getDef(const std::string &Name) const { |
| 1031 | std::map<std::string, Record*>::const_iterator I = Defs.find(Name); |
| 1032 | return I == Defs.end() ? 0 : I->second; |
| 1033 | } |
| 1034 | void addClass(Record *R) { |
| 1035 | assert(getClass(R->getName()) == 0 && "Class already exists!"); |
| 1036 | Classes.insert(std::make_pair(R->getName(), R)); |
| 1037 | } |
| 1038 | void addDef(Record *R) { |
| 1039 | assert(getDef(R->getName()) == 0 && "Def already exists!"); |
| 1040 | Defs.insert(std::make_pair(R->getName(), R)); |
| 1041 | } |
| 1042 | |
| 1043 | //===--------------------------------------------------------------------===// |
| 1044 | // High-level helper methods, useful for tablegen backends... |
| 1045 | |
| 1046 | /// getAllDerivedDefinitions - This method returns all concrete definitions |
| 1047 | /// that derive from the specified class name. If a class with the specified |
| 1048 | /// name does not exist, an exception is thrown. |
| 1049 | std::vector<Record*> |
| 1050 | getAllDerivedDefinitions(const std::string &ClassName) const; |
| 1051 | |
| 1052 | |
| 1053 | void dump() const; |
| 1054 | }; |
| 1055 | |
| 1056 | std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK); |
| 1057 | |
| 1058 | extern RecordKeeper Records; |
| 1059 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 1060 | } // End llvm namespace |
| 1061 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1062 | #endif |