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