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