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