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