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