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