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