llvm.org GIT mirror llvm / 7c78888
Move TableGen's parser and entry point into a library This is the first step towards splitting LLVM and Clang's tblgen executables. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@140951 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 7 years ago
87 changed file(s) with 7014 addition(s) and 6886 deletion(s). Raw diff Collapse all Expand all
194194
195195 # Put this before tblgen. Else we have a circular dependence.
196196 add_subdirectory(lib/Support)
197 add_subdirectory(lib/TableGen)
197198
198199 set(LLVM_TABLEGEN "tblgen" CACHE
199200 STRING "Native TableGen executable. Saves building one when cross-compiling.")
99 LEVEL := .
1010
1111 # Top-Level LLVM Build Stages:
12 # 1. Build lib/Support, which is used by utils (tblgen).
12 # 1. Build lib/Support and lib/TableGen, which are used by utils (tblgen).
1313 # 2. Build utils, which is used by VMCore.
1414 # 3. Build VMCore, which builds the Intrinsics.inc file used by libs.
1515 # 4. Build libs, which are needed by llvm-config.
2626 ifneq ($(findstring llvmCore, $(RC_ProjectName)),llvmCore) # Normal build (not "Apple-style").
2727
2828 ifeq ($(BUILD_DIRS_ONLY),1)
29 DIRS := lib/Support utils
29 DIRS := lib/Support lib/TableGen utils
3030 OPTIONAL_DIRS :=
3131 else
32 DIRS := lib/Support utils lib/VMCore lib tools/llvm-shlib \
32 DIRS := lib/Support lib/TableGen utils lib/VMCore lib tools/llvm-shlib \
3333 tools/llvm-config tools runtime docs unittests
3434 OPTIONAL_DIRS := projects bindings
3535 endif
0 //===- llvm/TableGen/Error.h - tblgen error handling helpers ----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains error handling helper routines to pretty-print diagnostic
10 // messages from tblgen.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TABLEGEN_ERROR_H
15 #define LLVM_TABLEGEN_ERROR_H
16
17 #include "llvm/Support/SourceMgr.h"
18
19 namespace llvm {
20
21 class TGError {
22 SMLoc Loc;
23 std::string Message;
24 public:
25 TGError(SMLoc loc, const std::string &message) : Loc(loc), Message(message) {}
26
27 SMLoc getLoc() const { return Loc; }
28 const std::string &getMessage() const { return Message; }
29 };
30
31 void PrintError(SMLoc ErrorLoc, const Twine &Msg);
32 void PrintError(const char *Loc, const Twine &Msg);
33 void PrintError(const Twine &Msg);
34 void PrintError(const TGError &Error);
35
36
37 extern SourceMgr SrcMgr;
38
39
40 } // end namespace "llvm"
41
42 #endif
0 //===- llvm/TableGen/Main.h - tblgen entry point ----------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the common entry point for tblgen tools.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_TABLEGEN_MAIN_H
14 #define LLVM_TABLEGEN_MAIN_H
15
16 namespace llvm {
17
18 class TableGenAction;
19
20 /// Run the table generator, performing the specified Action on parsed records.
21 int TableGenMain(char *argv0, TableGenAction &Action);
22
23 }
24
25 #endif
0 //===- llvm/TableGen/Record.h - Classes for Table Records -------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the main TableGen data structures, including the TableGen
10 // types, values, and high-level data structures.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TABLEGEN_RECORD_H
15 #define LLVM_TABLEGEN_RECORD_H
16
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include
24
25 namespace llvm {
26 class raw_ostream;
27
28 // RecTy subclasses.
29 class BitRecTy;
30 class BitsRecTy;
31 class IntRecTy;
32 class StringRecTy;
33 class ListRecTy;
34 class CodeRecTy;
35 class DagRecTy;
36 class RecordRecTy;
37
38 // Init subclasses.
39 class Init;
40 class UnsetInit;
41 class BitInit;
42 class BitsInit;
43 class IntInit;
44 class StringInit;
45 class CodeInit;
46 class ListInit;
47 class UnOpInit;
48 class BinOpInit;
49 class TernOpInit;
50 class DefInit;
51 class DagInit;
52 class TypedInit;
53 class VarInit;
54 class FieldInit;
55 class VarBitInit;
56 class VarListElementInit;
57
58 // Other classes.
59 class Record;
60 class RecordVal;
61 struct MultiClass;
62 class RecordKeeper;
63
64 //===----------------------------------------------------------------------===//
65 // Type Classes
66 //===----------------------------------------------------------------------===//
67
68 class RecTy {
69 ListRecTy *ListTy;
70 public:
71 RecTy() : ListTy(0) {}
72 virtual ~RecTy() {}
73
74 virtual std::string getAsString() const = 0;
75 void print(raw_ostream &OS) const { OS << getAsString(); }
76 void dump() const;
77
78 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
79 /// converted to the specified type.
80 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
81
82 /// getListTy - Returns the type representing list.
83 ListRecTy *getListTy();
84
85 public: // These methods should only be called from subclasses of Init
86 virtual Init *convertValue( UnsetInit *UI) { return 0; }
87 virtual Init *convertValue( BitInit *BI) { return 0; }
88 virtual Init *convertValue( BitsInit *BI) { return 0; }
89 virtual Init *convertValue( IntInit *II) { return 0; }
90 virtual Init *convertValue(StringInit *SI) { return 0; }
91 virtual Init *convertValue( ListInit *LI) { return 0; }
92 virtual Init *convertValue( UnOpInit *UI) {
93 return convertValue((TypedInit*)UI);
94 }
95 virtual Init *convertValue( BinOpInit *UI) {
96 return convertValue((TypedInit*)UI);
97 }
98 virtual Init *convertValue( TernOpInit *UI) {
99 return convertValue((TypedInit*)UI);
100 }
101 virtual Init *convertValue( CodeInit *CI) { return 0; }
102 virtual Init *convertValue(VarBitInit *VB) { return 0; }
103 virtual Init *convertValue( DefInit *DI) { return 0; }
104 virtual Init *convertValue( DagInit *DI) { return 0; }
105 virtual Init *convertValue( TypedInit *TI) { return 0; }
106 virtual Init *convertValue( VarInit *VI) {
107 return convertValue((TypedInit*)VI);
108 }
109 virtual Init *convertValue( FieldInit *FI) {
110 return convertValue((TypedInit*)FI);
111 }
112
113 public: // These methods should only be called by subclasses of RecTy.
114 // baseClassOf - These virtual methods should be overloaded to return true iff
115 // all values of type 'RHS' can be converted to the 'this' type.
116 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
117 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
118 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
119 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
120 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
121 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
122 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
123 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
124 };
125
126 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
127 Ty.print(OS);
128 return OS;
129 }
130
131
132 /// BitRecTy - 'bit' - Represent a single bit
133 ///
134 class BitRecTy : public RecTy {
135 static BitRecTy Shared;
136 BitRecTy() {}
137 public:
138 static BitRecTy *get() { return &Shared; }
139
140 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
141 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
142 virtual Init *convertValue( BitsInit *BI);
143 virtual Init *convertValue( IntInit *II);
144 virtual Init *convertValue(StringInit *SI) { return 0; }
145 virtual Init *convertValue( ListInit *LI) { return 0; }
146 virtual Init *convertValue( CodeInit *CI) { return 0; }
147 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
148 virtual Init *convertValue( DefInit *DI) { return 0; }
149 virtual Init *convertValue( DagInit *DI) { return 0; }
150 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
151 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
152 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
153 virtual Init *convertValue( TypedInit *TI);
154 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
155 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
156
157 std::string getAsString() const { return "bit"; }
158
159 bool typeIsConvertibleTo(const RecTy *RHS) const {
160 return RHS->baseClassOf(this);
161 }
162 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
163 virtual bool baseClassOf(const BitsRecTy *RHS) const;
164 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
165 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
166 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
167 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
168 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
169 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
170
171 };
172
173
174 // BitsRecTy - 'bits' - Represent a fixed number of bits
175 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
176 ///
177 class BitsRecTy : public RecTy {
178 unsigned Size;
179 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
180 public:
181 static BitsRecTy *get(unsigned Sz);
182
183 unsigned getNumBits() const { return Size; }
184
185 virtual Init *convertValue( UnsetInit *UI);
186 virtual Init *convertValue( BitInit *UI);
187 virtual Init *convertValue( BitsInit *BI);
188 virtual Init *convertValue( IntInit *II);
189 virtual Init *convertValue(StringInit *SI) { return 0; }
190 virtual Init *convertValue( ListInit *LI) { return 0; }
191 virtual Init *convertValue( CodeInit *CI) { return 0; }
192 virtual Init *convertValue(VarBitInit *VB) { return 0; }
193 virtual Init *convertValue( DefInit *DI) { return 0; }
194 virtual Init *convertValue( DagInit *DI) { return 0; }
195 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
196 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
197 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
198 virtual Init *convertValue( TypedInit *TI);
199 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
200 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
201
202 std::string getAsString() const;
203
204 bool typeIsConvertibleTo(const RecTy *RHS) const {
205 return RHS->baseClassOf(this);
206 }
207 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
208 virtual bool baseClassOf(const BitsRecTy *RHS) const {
209 return RHS->Size == Size;
210 }
211 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
212 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
213 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
214 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
215 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
216 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
217
218 };
219
220
221 /// IntRecTy - 'int' - Represent an integer value of no particular size
222 ///
223 class IntRecTy : public RecTy {
224 static IntRecTy Shared;
225 IntRecTy() {}
226 public:
227 static IntRecTy *get() { return &Shared; }
228
229 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
230 virtual Init *convertValue( BitInit *BI);
231 virtual Init *convertValue( BitsInit *BI);
232 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
233 virtual Init *convertValue(StringInit *SI) { return 0; }
234 virtual Init *convertValue( ListInit *LI) { return 0; }
235 virtual Init *convertValue( CodeInit *CI) { return 0; }
236 virtual Init *convertValue(VarBitInit *VB) { return 0; }
237 virtual Init *convertValue( DefInit *DI) { return 0; }
238 virtual Init *convertValue( DagInit *DI) { return 0; }
239 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
240 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
241 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
242 virtual Init *convertValue( TypedInit *TI);
243 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
244 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
245
246 std::string getAsString() const { return "int"; }
247
248 bool typeIsConvertibleTo(const RecTy *RHS) const {
249 return RHS->baseClassOf(this);
250 }
251
252 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
253 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
254 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
255 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
256 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
257 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
258 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
259 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
260
261 };
262
263 /// StringRecTy - 'string' - Represent an string value
264 ///
265 class StringRecTy : public RecTy {
266 static StringRecTy Shared;
267 StringRecTy() {}
268 public:
269 static StringRecTy *get() { return &Shared; }
270
271 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
272 virtual Init *convertValue( BitInit *BI) { return 0; }
273 virtual Init *convertValue( BitsInit *BI) { return 0; }
274 virtual Init *convertValue( IntInit *II) { return 0; }
275 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
276 virtual Init *convertValue( ListInit *LI) { return 0; }
277 virtual Init *convertValue( UnOpInit *BO);
278 virtual Init *convertValue( BinOpInit *BO);
279 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
280
281 virtual Init *convertValue( CodeInit *CI) { return 0; }
282 virtual Init *convertValue(VarBitInit *VB) { return 0; }
283 virtual Init *convertValue( DefInit *DI) { return 0; }
284 virtual Init *convertValue( DagInit *DI) { return 0; }
285 virtual Init *convertValue( TypedInit *TI);
286 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
287 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
288
289 std::string getAsString() const { return "string"; }
290
291 bool typeIsConvertibleTo(const RecTy *RHS) const {
292 return RHS->baseClassOf(this);
293 }
294
295 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
296 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
297 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
298 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
299 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
300 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
301 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
302 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
303 };
304
305 // ListRecTy - 'list' - Represent a list of values, all of which must be of
306 // the specified type.
307 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must
308 /// be of the specified type.
309 ///
310 class ListRecTy : public RecTy {
311 RecTy *Ty;
312 explicit ListRecTy(RecTy *T) : Ty(T) {}
313 friend ListRecTy *RecTy::getListTy();
314 public:
315 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
316 RecTy *getElementType() const { return Ty; }
317
318 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
319 virtual Init *convertValue( BitInit *BI) { return 0; }
320 virtual Init *convertValue( BitsInit *BI) { return 0; }
321 virtual Init *convertValue( IntInit *II) { return 0; }
322 virtual Init *convertValue(StringInit *SI) { return 0; }
323 virtual Init *convertValue( ListInit *LI);
324 virtual Init *convertValue( CodeInit *CI) { return 0; }
325 virtual Init *convertValue(VarBitInit *VB) { return 0; }
326 virtual Init *convertValue( DefInit *DI) { return 0; }
327 virtual Init *convertValue( DagInit *DI) { return 0; }
328 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
329 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
330 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
331 virtual Init *convertValue( TypedInit *TI);
332 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
333 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
334
335 std::string getAsString() const;
336
337 bool typeIsConvertibleTo(const RecTy *RHS) const {
338 return RHS->baseClassOf(this);
339 }
340
341 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
342 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
343 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
344 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
345 virtual bool baseClassOf(const ListRecTy *RHS) const {
346 return RHS->getElementType()->typeIsConvertibleTo(Ty);
347 }
348 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
349 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
350 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
351 };
352
353 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
354 ///
355 class CodeRecTy : public RecTy {
356 static CodeRecTy Shared;
357 CodeRecTy() {}
358 public:
359 static CodeRecTy *get() { return &Shared; }
360
361 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
362 virtual Init *convertValue( BitInit *BI) { return 0; }
363 virtual Init *convertValue( BitsInit *BI) { return 0; }
364 virtual Init *convertValue( IntInit *II) { return 0; }
365 virtual Init *convertValue(StringInit *SI) { return 0; }
366 virtual Init *convertValue( ListInit *LI) { return 0; }
367 virtual Init *convertValue( CodeInit *CI) { return (Init*)CI; }
368 virtual Init *convertValue(VarBitInit *VB) { return 0; }
369 virtual Init *convertValue( DefInit *DI) { return 0; }
370 virtual Init *convertValue( DagInit *DI) { return 0; }
371 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
372 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
373 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
374 virtual Init *convertValue( TypedInit *TI);
375 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
376 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
377
378 std::string getAsString() const { return "code"; }
379
380 bool typeIsConvertibleTo(const RecTy *RHS) const {
381 return RHS->baseClassOf(this);
382 }
383 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
384 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
385 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
386 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
387 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
388 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
389 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
390 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
391 };
392
393 /// DagRecTy - 'dag' - Represent a dag fragment
394 ///
395 class DagRecTy : public RecTy {
396 static DagRecTy Shared;
397 DagRecTy() {}
398 public:
399 static DagRecTy *get() { return &Shared; }
400
401 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
402 virtual Init *convertValue( BitInit *BI) { return 0; }
403 virtual Init *convertValue( BitsInit *BI) { return 0; }
404 virtual Init *convertValue( IntInit *II) { return 0; }
405 virtual Init *convertValue(StringInit *SI) { return 0; }
406 virtual Init *convertValue( ListInit *LI) { return 0; }
407 virtual Init *convertValue( CodeInit *CI) { return 0; }
408 virtual Init *convertValue(VarBitInit *VB) { return 0; }
409 virtual Init *convertValue( DefInit *DI) { return 0; }
410 virtual Init *convertValue( UnOpInit *BO);
411 virtual Init *convertValue( BinOpInit *BO);
412 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
413 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
414 virtual Init *convertValue( TypedInit *TI);
415 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
416 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
417
418 std::string getAsString() const { return "dag"; }
419
420 bool typeIsConvertibleTo(const RecTy *RHS) const {
421 return RHS->baseClassOf(this);
422 }
423
424 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
425 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
426 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
427 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
428 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
429 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
430 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
431 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
432 };
433
434
435 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
436 /// (R32 X = EAX).
437 ///
438 class RecordRecTy : public RecTy {
439 Record *Rec;
440 explicit RecordRecTy(Record *R) : Rec(R) {}
441 friend class Record;
442 public:
443 static RecordRecTy *get(Record *R);
444
445 Record *getRecord() const { return Rec; }
446
447 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
448 virtual Init *convertValue( BitInit *BI) { return 0; }
449 virtual Init *convertValue( BitsInit *BI) { return 0; }
450 virtual Init *convertValue( IntInit *II) { return 0; }
451 virtual Init *convertValue(StringInit *SI) { return 0; }
452 virtual Init *convertValue( ListInit *LI) { return 0; }
453 virtual Init *convertValue( CodeInit *CI) { return 0; }
454 virtual Init *convertValue(VarBitInit *VB) { return 0; }
455 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
456 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
457 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
458 virtual Init *convertValue( DefInit *DI);
459 virtual Init *convertValue( DagInit *DI) { return 0; }
460 virtual Init *convertValue( TypedInit *VI);
461 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
462 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
463
464 std::string getAsString() const;
465
466 bool typeIsConvertibleTo(const RecTy *RHS) const {
467 return RHS->baseClassOf(this);
468 }
469 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
470 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
471 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
472 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
473 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
474 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
475 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
476 virtual bool baseClassOf(const RecordRecTy *RHS) const;
477 };
478
479 /// resolveTypes - Find a common type that T1 and T2 convert to.
480 /// Return 0 if no such type exists.
481 ///
482 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
483
484 //===----------------------------------------------------------------------===//
485 // Initializer Classes
486 //===----------------------------------------------------------------------===//
487
488 class Init {
489 Init(const Init &); // Do not define.
490 Init &operator=(const Init &); // Do not define.
491
492 protected:
493 Init(void) {}
494
495 public:
496 virtual ~Init() {}
497
498 /// isComplete - This virtual method should be overridden by values that may
499 /// not be completely specified yet.
500 virtual bool isComplete() const { return true; }
501
502 /// print - Print out this value.
503 void print(raw_ostream &OS) const { OS << getAsString(); }
504
505 /// getAsString - Convert this value to a string form.
506 virtual std::string getAsString() const = 0;
507 /// getAsUnquotedString - Convert this value to a string form,
508 /// without adding quote markers. This primaruly affects
509 /// StringInits where we will not surround the string value with
510 /// quotes.
511 virtual std::string getAsUnquotedString() const { return getAsString(); }
512
513 /// dump - Debugging method that may be called through a debugger, just
514 /// invokes print on stderr.
515 void dump() const;
516
517 /// convertInitializerTo - This virtual function is a simple call-back
518 /// function that should be overridden to call the appropriate
519 /// RecTy::convertValue method.
520 ///
521 virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
522
523 /// convertInitializerBitRange - This method is used to implement the bitrange
524 /// selection operator. Given an initializer, it selects the specified bits
525 /// out, returning them as a new init of bits type. If it is not legal to use
526 /// the bit subscript operator on this initializer, return null.
527 ///
528 virtual Init *
529 convertInitializerBitRange(const std::vector &Bits) const {
530 return 0;
531 }
532
533 /// convertInitListSlice - This method is used to implement the list slice
534 /// selection operator. Given an initializer, it selects the specified list
535 /// elements, returning them as a new init of list type. If it is not legal
536 /// to take a slice of this, return null.
537 ///
538 virtual Init *
539 convertInitListSlice(const std::vector &Elements) const {
540 return 0;
541 }
542
543 /// getFieldType - This method is used to implement the FieldInit class.
544 /// Implementors of this method should return the type of the named field if
545 /// they are of record type.
546 ///
547 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
548
549 /// getFieldInit - This method complements getFieldType to return the
550 /// initializer for the specified field. If getFieldType returns non-null
551 /// this method should return non-null, otherwise it returns null.
552 ///
553 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
554 const std::string &FieldName) const {
555 return 0;
556 }
557
558 /// resolveReferences - This method is used by classes that refer to other
559 /// variables which may not be defined at the time the expression is formed.
560 /// If a value is set for the variable later, this method will be called on
561 /// users of the value to allow the value to propagate out.
562 ///
563 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
564 return const_cast(this);
565 }
566 };
567
568 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
569 I.print(OS); return OS;
570 }
571
572 /// TypedInit - This is the common super-class of types that have a specific,
573 /// explicit, type.
574 ///
575 class TypedInit : public Init {
576 RecTy *Ty;
577
578 TypedInit(const TypedInit &Other); // Do not define.
579 TypedInit &operator=(const TypedInit &Other); // Do not define.
580
581 protected:
582 explicit TypedInit(RecTy *T) : Ty(T) {}
583
584 public:
585 RecTy *getType() const { return Ty; }
586
587 virtual Init *
588 convertInitializerBitRange(const std::vector &Bits) const;
589 virtual Init *
590 convertInitListSlice(const std::vector &Elements) const;
591
592 /// getFieldType - This method is used to implement the FieldInit class.
593 /// Implementors of this method should return the type of the named field if
594 /// they are of record type.
595 ///
596 virtual RecTy *getFieldType(const std::string &FieldName) const;
597
598 /// resolveBitReference - This method is used to implement
599 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
600 /// simply return the resolved value, otherwise we return null.
601 ///
602 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
603 unsigned Bit) const = 0;
604
605 /// resolveListElementReference - This method is used to implement
606 /// VarListElementInit::resolveReferences. If the list element is resolvable
607 /// now, we return the resolved value, otherwise we return null.
608 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
609 unsigned Elt) const = 0;
610 };
611
612
613 /// UnsetInit - ? - Represents an uninitialized value
614 ///
615 class UnsetInit : public Init {
616 UnsetInit() : Init() {}
617 UnsetInit(const UnsetInit &); // Do not define.
618 UnsetInit &operator=(const UnsetInit &Other); // Do not define.
619
620 public:
621 static UnsetInit *get();
622
623 virtual Init *convertInitializerTo(RecTy *Ty) const {
624 return Ty->convertValue(const_cast(this));
625 }
626
627 virtual bool isComplete() const { return false; }
628 virtual std::string getAsString() const { return "?"; }
629 };
630
631
632 /// BitInit - true/false - Represent a concrete initializer for a bit.
633 ///
634 class BitInit : public Init {
635 bool Value;
636
637 explicit BitInit(bool V) : Value(V) {}
638 BitInit(const BitInit &Other); // Do not define.
639 BitInit &operator=(BitInit &Other); // Do not define.
640
641 public:
642 static BitInit *get(bool V);
643
644 bool getValue() const { return Value; }
645
646 virtual Init *convertInitializerTo(RecTy *Ty) const {
647 return Ty->convertValue(const_cast(this));
648 }
649
650 virtual std::string getAsString() const { return Value ? "1" : "0"; }
651 };
652
653 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
654 /// It contains a vector of bits, whose size is determined by the type.
655 ///
656 class BitsInit : public Init, public FoldingSetNode {
657 std::vector Bits;
658
659 BitsInit(ArrayRef Range) : Bits(Range.begin(), Range.end()) {}
660
661 BitsInit(const BitsInit &Other); // Do not define.
662 BitsInit &operator=(const BitsInit &Other); // Do not define.
663
664 public:
665 static BitsInit *get(ArrayRef Range);
666
667 void Profile(FoldingSetNodeID &ID) const;
668
669 unsigned getNumBits() const { return Bits.size(); }
670
671 Init *getBit(unsigned Bit) const {
672 assert(Bit < Bits.size() && "Bit index out of range!");
673 return Bits[Bit];
674 }
675
676 virtual Init *convertInitializerTo(RecTy *Ty) const {
677 return Ty->convertValue(const_cast(this));
678 }
679 virtual Init *
680 convertInitializerBitRange(const std::vector &Bits) const;
681
682 virtual bool isComplete() const {
683 for (unsigned i = 0; i != getNumBits(); ++i)
684 if (!getBit(i)->isComplete()) return false;
685 return true;
686 }
687 bool allInComplete() const {
688 for (unsigned i = 0; i != getNumBits(); ++i)
689 if (getBit(i)->isComplete()) return false;
690 return true;
691 }
692 virtual std::string getAsString() const;
693
694 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
695 };
696
697
698 /// IntInit - 7 - Represent an initalization by a literal integer value.
699 ///
700 class IntInit : public TypedInit {
701 int64_t Value;
702
703 explicit IntInit(int64_t V) : TypedInit(IntRecTy::get()), Value(V) {}
704
705 IntInit(const IntInit &Other); // Do not define.
706 IntInit &operator=(const IntInit &Other); // Do note define.
707
708 public:
709 static IntInit *get(int64_t V);
710
711 int64_t getValue() const { return Value; }
712
713 virtual Init *convertInitializerTo(RecTy *Ty) const {
714 return Ty->convertValue(const_cast(this));
715 }
716 virtual Init *
717 convertInitializerBitRange(const std::vector &Bits) const;
718
719 virtual std::string getAsString() const;
720
721 /// resolveBitReference - This method is used to implement
722 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
723 /// simply return the resolved value, otherwise we return null.
724 ///
725 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
726 unsigned Bit) const {
727 assert(0 && "Illegal bit reference off int");
728 return 0;
729 }
730
731 /// resolveListElementReference - This method is used to implement
732 /// VarListElementInit::resolveReferences. If the list element is resolvable
733 /// now, we return the resolved value, otherwise we return null.
734 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
735 unsigned Elt) const {
736 assert(0 && "Illegal element reference off int");
737 return 0;
738 }
739 };
740
741
742 /// StringInit - "foo" - Represent an initialization by a string value.
743 ///
744 class StringInit : public TypedInit {
745 std::string Value;
746
747 explicit StringInit(const std::string &V)
748 : TypedInit(StringRecTy::get()), Value(V) {}
749
750 StringInit(const StringInit &Other); // Do not define.
751 StringInit &operator=(const StringInit &Other); // Do not define.
752
753 public:
754 static StringInit *get(const std::string &V);
755
756 const std::string &getValue() const { return Value; }
757
758 virtual Init *convertInitializerTo(RecTy *Ty) const {
759 return Ty->convertValue(const_cast(this));
760 }
761
762 virtual std::string getAsString() const { return "\"" + Value + "\""; }
763 virtual std::string getAsUnquotedString() const { return Value; }
764
765 /// resolveBitReference - This method is used to implement
766 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
767 /// simply return the resolved value, otherwise we return null.
768 ///
769 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
770 unsigned Bit) const {
771 assert(0 && "Illegal bit reference off string");
772 return 0;
773 }
774
775 /// resolveListElementReference - This method is used to implement
776 /// VarListElementInit::resolveReferences. If the list element is resolvable
777 /// now, we return the resolved value, otherwise we return null.
778 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
779 unsigned Elt) const {
780 assert(0 && "Illegal element reference off string");
781 return 0;
782 }
783 };
784
785 /// CodeInit - "[{...}]" - Represent a code fragment.
786 ///
787 class CodeInit : public Init {
788 std::string Value;
789
790 explicit CodeInit(const std::string &V) : Value(V) {}
791
792 CodeInit(const CodeInit &Other); // Do not define.
793 CodeInit &operator=(const CodeInit &Other); // Do not define.
794
795 public:
796 static CodeInit *get(const std::string &V);
797
798 const std::string &getValue() const { return Value; }
799
800 virtual Init *convertInitializerTo(RecTy *Ty) const {
801 return Ty->convertValue(const_cast(this));
802 }
803
804 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
805 };
806
807 /// ListInit - [AL, AH, CL] - Represent a list of defs
808 ///
809 class ListInit : public TypedInit, public FoldingSetNode {
810 std::vector Values;
811 public:
812 typedef std::vector::const_iterator const_iterator;
813
814 private:
815 explicit ListInit(ArrayRef Range, RecTy *EltTy)
816 : TypedInit(ListRecTy::get(EltTy)), Values(Range.begin(), Range.end()) {}
817
818 ListInit(const ListInit &Other); // Do not define.
819 ListInit &operator=(const ListInit &Other); // Do not define.
820
821 public:
822 static ListInit *get(ArrayRef Range, RecTy *EltTy);
823
824 void Profile(FoldingSetNodeID &ID) const;
825
826 unsigned getSize() const { return Values.size(); }
827 Init *getElement(unsigned i) const {
828 assert(i < Values.size() && "List element index out of range!");
829 return Values[i];
830 }
831
832 Record *getElementAsRecord(unsigned i) const;
833
834 Init *convertInitListSlice(const std::vector &Elements) const;
835
836 virtual Init *convertInitializerTo(RecTy *Ty) const {
837 return Ty->convertValue(const_cast(this));
838 }
839
840 /// resolveReferences - This method is used by classes that refer to other
841 /// variables which may not be defined at the time they expression is formed.
842 /// If a value is set for the variable later, this method will be called on
843 /// users of the value to allow the value to propagate out.
844 ///
845 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
846
847 virtual std::string getAsString() const;
848
849 ArrayRef getValues() const { return Values; }
850
851 inline const_iterator begin() const { return Values.begin(); }
852 inline const_iterator end () const { return Values.end(); }
853
854 inline size_t size () const { return Values.size(); }
855 inline bool empty() const { return Values.empty(); }
856
857 /// resolveBitReference - This method is used to implement
858 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
859 /// simply return the resolved value, otherwise we return null.
860 ///
861 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
862 unsigned Bit) const {
863 assert(0 && "Illegal bit reference off list");
864 return 0;
865 }
866
867 /// resolveListElementReference - This method is used to implement
868 /// VarListElementInit::resolveReferences. If the list element is resolvable
869 /// now, we return the resolved value, otherwise we return null.
870 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
871 unsigned Elt) const;
872 };
873
874
875 /// OpInit - Base class for operators
876 ///
877 class OpInit : public TypedInit {
878 OpInit(const OpInit &Other); // Do not define.
879 OpInit &operator=(OpInit &Other); // Do not define.
880
881 protected:
882 explicit OpInit(RecTy *Type) : TypedInit(Type) {}
883
884 public:
885 // Clone - Clone this operator, replacing arguments with the new list
886 virtual OpInit *clone(std::vector &Operands) const = 0;
887
888 virtual int getNumOperands() const = 0;
889 virtual Init *getOperand(int i) const = 0;
890
891 // Fold - If possible, fold this to a simpler init. Return this if not
892 // possible to fold.
893 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
894
895 virtual Init *convertInitializerTo(RecTy *Ty) const {
896 return Ty->convertValue(const_cast(this));
897 }
898
899 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
900 unsigned Bit) const;
901 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
902 unsigned Elt) const;
903 };
904
905
906 /// UnOpInit - !op (X) - Transform an init.
907 ///
908 class UnOpInit : public OpInit {
909 public:
910 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
911 private:
912 UnaryOp Opc;
913 Init *LHS;
914
915 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
916 : OpInit(Type), Opc(opc), LHS(lhs) {}
917
918 UnOpInit(const UnOpInit &Other); // Do not define.
919 UnOpInit &operator=(const UnOpInit &Other); // Do not define.
920
921 public:
922 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
923
924 // Clone - Clone this operator, replacing arguments with the new list
925 virtual OpInit *clone(std::vector &Operands) const {
926 assert(Operands.size() == 1 &&
927 "Wrong number of operands for unary operation");
928 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
929 }
930
931 int getNumOperands() const { return 1; }
932 Init *getOperand(int i) const {
933 assert(i == 0 && "Invalid operand id for unary operator");
934 return getOperand();
935 }
936
937 UnaryOp getOpcode() const { return Opc; }
938 Init *getOperand() const { return LHS; }
939
940 // Fold - If possible, fold this to a simpler init. Return this if not
941 // possible to fold.
942 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
943
944 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
945
946 virtual std::string getAsString() const;
947 };
948
949 /// BinOpInit - !op (X, Y) - Combine two inits.
950 ///
951 class BinOpInit : public OpInit {
952 public:
953 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
954 private:
955 BinaryOp Opc;
956 Init *LHS, *RHS;
957
958 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
959 OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {}
960
961 BinOpInit(const BinOpInit &Other); // Do not define.
962 BinOpInit &operator=(const BinOpInit &Other); // Do not define.
963
964 public:
965 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
966 RecTy *Type);
967
968 // Clone - Clone this operator, replacing arguments with the new list
969 virtual OpInit *clone(std::vector &Operands) const {
970 assert(Operands.size() == 2 &&
971 "Wrong number of operands for binary operation");
972 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
973 }
974
975 int getNumOperands() const { return 2; }
976 Init *getOperand(int i) const {
977 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
978 if (i == 0) {
979 return getLHS();
980 } else {
981 return getRHS();
982 }
983 }
984
985 BinaryOp getOpcode() const { return Opc; }
986 Init *getLHS() const { return LHS; }
987 Init *getRHS() const { return RHS; }
988
989 // Fold - If possible, fold this to a simpler init. Return this if not
990 // possible to fold.
991 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
992
993 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
994
995 virtual std::string getAsString() const;
996 };
997
998 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
999 ///
1000 class TernOpInit : public OpInit {
1001 public:
1002 enum TernaryOp { SUBST, FOREACH, IF };
1003 private:
1004 TernaryOp Opc;
1005 Init *LHS, *MHS, *RHS;
1006
1007 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
1008 RecTy *Type) :
1009 OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
1010
1011 TernOpInit(const TernOpInit &Other); // Do not define.
1012 TernOpInit &operator=(const TernOpInit &Other); // Do not define.
1013
1014 public:
1015 static TernOpInit *get(TernaryOp opc, Init *lhs,
1016 Init *mhs, Init *rhs,
1017 RecTy *Type);
1018
1019 // Clone - Clone this operator, replacing arguments with the new list
1020 virtual OpInit *clone(std::vector &Operands) const {
1021 assert(Operands.size() == 3 &&
1022 "Wrong number of operands for ternary operation");
1023 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
1024 getType());
1025 }
1026
1027 int getNumOperands() const { return 3; }
1028 Init *getOperand(int i) const {
1029 assert((i == 0 || i == 1 || i == 2) &&
1030 "Invalid operand id for ternary operator");
1031 if (i == 0) {
1032 return getLHS();
1033 } else if (i == 1) {
1034 return getMHS();
1035 } else {
1036 return getRHS();
1037 }
1038 }
1039
1040 TernaryOp getOpcode() const { return Opc; }
1041 Init *getLHS() const { return LHS; }
1042 Init *getMHS() const { return MHS; }
1043 Init *getRHS() const { return RHS; }
1044
1045 // Fold - If possible, fold this to a simpler init. Return this if not
1046 // possible to fold.
1047 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
1048
1049 virtual bool isComplete() const { return false; }
1050
1051 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1052
1053 virtual std::string getAsString() const;
1054 };
1055
1056
1057 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1058 ///
1059 class VarInit : public TypedInit {
1060 std::string VarName;
1061
1062 explicit VarInit(const std::string &VN, RecTy *T)
1063 : TypedInit(T), VarName(VN) {}
1064
1065 VarInit(const VarInit &Other); // Do not define.
1066 VarInit &operator=(const VarInit &Other); // Do not define.
1067
1068 public:
1069 static VarInit *get(const std::string &VN, RecTy *T);
1070 static VarInit *get(Init *VN, RecTy *T);
1071
1072 virtual Init *convertInitializerTo(RecTy *Ty) const {
1073 return Ty->convertValue(const_cast(this));
1074 }
1075
1076 const std::string &getName() const { return VarName; }
1077
1078 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1079 unsigned Bit) const;
1080 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1081 unsigned Elt) const;
1082
1083 virtual RecTy *getFieldType(const std::string &FieldName) const;
1084 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1085 const std::string &FieldName) const;
1086
1087 /// resolveReferences - This method is used by classes that refer to other
1088 /// variables which may not be defined at the time they expression is formed.
1089 /// If a value is set for the variable later, this method will be called on
1090 /// users of the value to allow the value to propagate out.
1091 ///
1092 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1093
1094 virtual std::string getAsString() const { return VarName; }
1095 };
1096
1097
1098 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1099 ///
1100 class VarBitInit : public Init {
1101 TypedInit *TI;
1102 unsigned Bit;
1103
1104 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
1105 assert(T->getType() && dynamic_cast(T->getType()) &&
1106 ((BitsRecTy*)T->getType())->getNumBits() > B &&
1107 "Illegal VarBitInit expression!");
1108 }
1109
1110 VarBitInit(const VarBitInit &Other); // Do not define.
1111 VarBitInit &operator=(const VarBitInit &Other); // Do not define.
1112
1113 public:
1114 static VarBitInit *get(TypedInit *T, unsigned B);
1115
1116 virtual Init *convertInitializerTo(RecTy *Ty) const {
1117 return Ty->convertValue(const_cast(this));
1118 }
1119
1120 TypedInit *getVariable() const { return TI; }
1121 unsigned getBitNum() const { return Bit; }
1122
1123 virtual std::string getAsString() const;
1124 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1125 };
1126
1127 /// VarListElementInit - List[4] - Represent access to one element of a var or
1128 /// field.
1129 class VarListElementInit : public TypedInit {
1130 TypedInit *TI;
1131 unsigned Element;
1132
1133 VarListElementInit(TypedInit *T, unsigned E)
1134 : TypedInit(dynamic_cast(T->getType())->getElementType()),
1135 TI(T), Element(E) {
1136 assert(T->getType() && dynamic_cast(T->getType()) &&
1137 "Illegal VarBitInit expression!");
1138 }
1139
1140 VarListElementInit(const VarListElementInit &Other); // Do not define.
1141 VarListElementInit &operator=(const VarListElementInit &Other); // Do
1142 // not
1143 // define.
1144
1145 public:
1146 static VarListElementInit *get(TypedInit *T, unsigned E);
1147
1148 virtual Init *convertInitializerTo(RecTy *Ty) const {
1149 return Ty->convertValue(const_cast(this));
1150 }
1151
1152 TypedInit *getVariable() const { return TI; }
1153 unsigned getElementNum() const { return Element; }
1154
1155 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1156 unsigned Bit) const;
1157
1158 /// resolveListElementReference - This method is used to implement
1159 /// VarListElementInit::resolveReferences. If the list element is resolvable
1160 /// now, we return the resolved value, otherwise we return null.
1161 virtual Init *resolveListElementReference(Record &R,
1162 const RecordVal *RV,
1163 unsigned Elt) const;
1164
1165 virtual std::string getAsString() const;
1166 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1167 };
1168
1169 /// DefInit - AL - Represent a reference to a 'def' in the description
1170 ///
1171 class DefInit : public TypedInit {
1172 Record *Def;
1173
1174 DefInit(Record *D, RecordRecTy *T) : TypedInit(T), Def(D) {}
1175 friend class Record;
1176
1177 DefInit(const DefInit &Other); // Do not define.
1178 DefInit &operator=(const DefInit &Other); // Do not define.
1179
1180 public:
1181 static DefInit *get(Record*);
1182
1183 virtual Init *convertInitializerTo(RecTy *Ty) const {
1184 return Ty->convertValue(const_cast(this));
1185 }
1186
1187 Record *getDef() const { return Def; }
1188
1189 //virtual Init *convertInitializerBitRange(const std::vector &Bits);
1190
1191 virtual RecTy *getFieldType(const std::string &FieldName) const;
1192 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1193 const std::string &FieldName) const;
1194
1195 virtual std::string getAsString() const;
1196
1197 /// resolveBitReference - This method is used to implement
1198 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
1199 /// simply return the resolved value, otherwise we return null.
1200 ///
1201 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1202 unsigned Bit) const {
1203 assert(0 && "Illegal bit reference off def");
1204 return 0;
1205 }
1206
1207 /// resolveListElementReference - This method is used to implement
1208 /// VarListElementInit::resolveReferences. If the list element is resolvable
1209 /// now, we return the resolved value, otherwise we return null.
1210 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1211 unsigned Elt) const {
1212 assert(0 && "Illegal element reference off def");
1213 return 0;
1214 }
1215 };
1216
1217
1218 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1219 ///
1220 class FieldInit : public TypedInit {
1221 Init *Rec; // Record we are referring to
1222 std::string FieldName; // Field we are accessing
1223
1224 FieldInit(Init *R, const std::string &FN)
1225 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
1226 assert(getType() && "FieldInit with non-record type!");
1227 }
1228
1229 FieldInit(const FieldInit &Other); // Do not define.
1230 FieldInit &operator=(const FieldInit &Other); // Do not define.
1231
1232 public:
1233 static FieldInit *get(Init *R, const std::string &FN);
1234 static FieldInit *get(Init *R, const Init *FN);
1235
1236 virtual Init *convertInitializerTo(RecTy *Ty) const {
1237 return Ty->convertValue(const_cast(this));
1238 }
1239
1240 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1241 unsigned Bit) const;
1242 virtual Init *resolveListElementReference(Record &R,
1243 const RecordVal *RV,
1244 unsigned Elt) const;
1245
1246 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1247
1248 virtual std::string getAsString() const {
1249 return Rec->getAsString() + "." + FieldName;
1250 }
1251 };
1252
1253 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1254 /// to have at least one value then a (possibly empty) list of arguments. Each
1255 /// argument can have a name associated with it.
1256 ///
1257 class DagInit : public TypedInit, public FoldingSetNode {
1258 Init *Val;
1259 std::string ValName;
1260 std::vector Args;
1261 std::vector ArgNames;
1262
1263 DagInit(Init *V, const std::string &VN,
1264 ArrayRef ArgRange,
1265 ArrayRef NameRange)
1266 : TypedInit(DagRecTy::get()), Val(V), ValName(VN),
1267 Args(ArgRange.begin(), ArgRange.end()),
1268 ArgNames(NameRange.begin(), NameRange.end()) {}
1269
1270 DagInit(const DagInit &Other); // Do not define.
1271 DagInit &operator=(const DagInit &Other); // Do not define.
1272
1273 public:
1274 static DagInit *get(Init *V, const std::string &VN,
1275 ArrayRef ArgRange,
1276 ArrayRef NameRange);
1277 static DagInit *get(Init *V, const std::string &VN,
1278 const std::vector<
1279 std::pair > &args);
1280
1281 void Profile(FoldingSetNodeID &ID) const;
1282
1283 virtual Init *convertInitializerTo(RecTy *Ty) const {
1284 return Ty->convertValue(const_cast(this));
1285 }
1286
1287 Init *getOperator() const { return Val; }
1288
1289 const std::string &getName() const { return ValName; }
1290
1291 unsigned getNumArgs() const { return Args.size(); }
1292 Init *getArg(unsigned Num) const {
1293 assert(Num < Args.size() && "Arg number out of range!");
1294 return Args[Num];
1295 }
1296 const std::string &getArgName(unsigned Num) const {
1297 assert(Num < ArgNames.size() && "Arg number out of range!");
1298 return ArgNames[Num];
1299 }
1300
1301 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const;
1302
1303 virtual std::string getAsString() const;
1304
1305 typedef std::vector::const_iterator const_arg_iterator;
1306 typedef std::vector::const_iterator const_name_iterator;
1307
1308 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1309 inline const_arg_iterator arg_end () const { return Args.end(); }
1310
1311 inline size_t arg_size () const { return Args.size(); }
1312 inline bool arg_empty() const { return Args.empty(); }
1313
1314 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1315 inline const_name_iterator name_end () const { return ArgNames.end(); }
1316
1317 inline size_t name_size () const { return ArgNames.size(); }
1318 inline bool name_empty() const { return ArgNames.empty(); }
1319
1320 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1321 unsigned Bit) const {
1322 assert(0 && "Illegal bit reference off dag");
1323 return 0;
1324 }
1325
1326 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1327 unsigned Elt) const {
1328 assert(0 && "Illegal element reference off dag");
1329 return 0;
1330 }
1331 };
1332
1333 //===----------------------------------------------------------------------===//
1334 // High-Level Classes
1335 //===----------------------------------------------------------------------===//
1336
1337 class RecordVal {
1338 Init *Name;
1339 RecTy *Ty;
1340 unsigned Prefix;
1341 Init *Value;
1342 public:
1343 RecordVal(Init *N, RecTy *T, unsigned P);
1344 RecordVal(const std::string &N, RecTy *T, unsigned P);
1345
1346 const std::string &getName() const;
1347
1348 unsigned getPrefix() const { return Prefix; }
1349 RecTy *getType() const { return Ty; }
1350 Init *getValue() const { return Value; }
1351
1352 bool setValue(Init *V) {
1353 if (V) {
1354 Value = V->convertInitializerTo(Ty);
1355 return Value == 0;
1356 }
1357 Value = 0;
1358 return false;
1359 }
1360
1361 void dump() const;
1362 void print(raw_ostream &OS, bool PrintSem = true) const;
1363 };
1364
1365 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1366 RV.print(OS << " ");
1367 return OS;
1368 }
1369
1370 class Record {
1371 static unsigned LastID;
1372
1373 // Unique record ID.
1374 unsigned ID;
1375 Init *Name;
1376 SMLoc Loc;
1377 std::vector TemplateArgs;
1378 std::vector Values;
1379 std::vector SuperClasses;
1380
1381 // Tracks Record instances. Not owned by Record.
1382 RecordKeeper &TrackedRecords;
1383
1384 DefInit *TheInit;
1385
1386 void checkName();
1387
1388 public:
1389
1390 // Constructs a record.
1391 explicit Record(const std::string &N, SMLoc loc, RecordKeeper &records) :
1392 ID(LastID++), Name(StringInit::get(N)), Loc(loc), TrackedRecords(records), TheInit(0) {}
1393 ~Record() {}
1394
1395
1396 static unsigned getNewUID() { return LastID++; }
1397
1398
1399 unsigned getID() const { return ID; }
1400
1401 const std::string &getName() const;
1402 void setName(Init *Name); // Also updates RecordKeeper.
1403 void setName(const std::string &Name); // Also updates RecordKeeper.
1404
1405 SMLoc getLoc() const { return Loc; }
1406
1407 /// get the corresponding DefInit.
1408 DefInit *getDefInit();
1409
1410 const std::vector &getTemplateArgs() const {
1411 return TemplateArgs;
1412 }
1413 const std::vector &getValues() const { return Values; }
1414 const std::vector &getSuperClasses() const { return SuperClasses; }
1415
1416 bool isTemplateArg(StringRef Name) const {
1417 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1418 if (TemplateArgs[i] == Name) return true;
1419 return false;
1420 }
1421
1422 const RecordVal *getValue(StringRef Name) const {
1423 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1424 if (Values[i].getName() == Name) return &Values[i];
1425 return 0;
1426 }
1427 RecordVal *getValue(StringRef Name) {
1428 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1429 if (Values[i].getName() == Name) return &Values[i];
1430 return 0;
1431 }
1432
1433 void addTemplateArg(StringRef Name) {
1434 assert(!isTemplateArg(Name) && "Template arg already defined!");
1435 TemplateArgs.push_back(Name);
1436 }
1437
1438 void addValue(const RecordVal &RV) {
1439 assert(getValue(RV.getName()) == 0 && "Value already added!");
1440 Values.push_back(RV);
1441 }
1442
1443 void removeValue(StringRef Name) {
1444 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1445 if (Values[i].getName() == Name) {
1446 Values.erase(Values.begin()+i);
1447 return;
1448 }
1449 assert(0 && "Cannot remove an entry that does not exist!");
1450 }
1451
1452 bool isSubClassOf(const Record *R) const {
1453 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1454 if (SuperClasses[i] == R)
1455 return true;
1456 return false;
1457 }
1458
1459 bool isSubClassOf(StringRef Name) const {
1460 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1461 if (SuperClasses[i]->getName() == Name)
1462 return true;
1463 return false;
1464 }
1465
1466 void addSuperClass(Record *R) {
1467 assert(!isSubClassOf(R) && "Already subclassing record!");
1468 SuperClasses.push_back(R);
1469 }
1470
1471 /// resolveReferences - If there are any field references that refer to fields
1472 /// that have been filled in, we can propagate the values now.
1473 ///
1474 void resolveReferences() { resolveReferencesTo(0); }
1475
1476 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1477 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1478 /// possible references.
1479 void resolveReferencesTo(const RecordVal *RV);
1480
1481 RecordKeeper &getRecords() const {
1482 return TrackedRecords;
1483 }
1484
1485 void dump() const;
1486
1487 //===--------------------------------------------------------------------===//
1488 // High-level methods useful to tablegen back-ends
1489 //
1490
1491 /// getValueInit - Return the initializer for a value with the specified name,
1492 /// or throw an exception if the field does not exist.
1493 ///
1494 Init *getValueInit(StringRef FieldName) const;
1495
1496 /// getValueAsString - This method looks up the specified field and returns
1497 /// its value as a string, throwing an exception if the field does not exist
1498 /// or if the value is not a string.
1499 ///
1500 std::string getValueAsString(StringRef FieldName) const;
1501
1502 /// getValueAsBitsInit - This method looks up the specified field and returns
1503 /// its value as a BitsInit, throwing an exception if the field does not exist
1504 /// or if the value is not the right type.
1505 ///
1506 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1507
1508 /// getValueAsListInit - This method looks up the specified field and returns
1509 /// its value as a ListInit, throwing an exception if the field does not exist
1510 /// or if the value is not the right type.
1511 ///
1512 ListInit *getValueAsListInit(StringRef FieldName) const;
1513
1514 /// getValueAsListOfDefs - This method looks up the specified field and
1515 /// returns its value as a vector of records, throwing an exception if the
1516 /// field does not exist or if the value is not the right type.
1517 ///
1518 std::vector getValueAsListOfDefs(StringRef FieldName) const;
1519
1520 /// getValueAsListOfInts - This method looks up the specified field and
1521 /// returns its value as a vector of integers, throwing an exception if the
1522 /// field does not exist or if the value is not the right type.
1523 ///
1524 std::vector getValueAsListOfInts(StringRef FieldName) const;
1525
1526 /// getValueAsListOfStrings - This method looks up the specified field and
1527 /// returns its value as a vector of strings, throwing an exception if the
1528 /// field does not exist or if the value is not the right type.
1529 ///
1530 std::vector getValueAsListOfStrings(StringRef FieldName) const;
1531
1532 /// getValueAsDef - This method looks up the specified field and returns its
1533 /// value as a Record, throwing an exception if the field does not exist or if
1534 /// the value is not the right type.
1535 ///
1536 Record *getValueAsDef(StringRef FieldName) const;
1537
1538 /// getValueAsBit - This method looks up the specified field and returns its
1539 /// value as a bit, throwing an exception if the field does not exist or if
1540 /// the value is not the right type.
1541 ///
1542 bool getValueAsBit(StringRef FieldName) const;
1543
1544 /// getValueAsInt - This method looks up the specified field and returns its
1545 /// value as an int64_t, throwing an exception if the field does not exist or
1546 /// if the value is not the right type.
1547 ///
1548 int64_t getValueAsInt(StringRef FieldName) const;
1549
1550 /// getValueAsDag - This method looks up the specified field and returns its
1551 /// value as an Dag, throwing an exception if the field does not exist or if
1552 /// the value is not the right type.
1553 ///
1554 DagInit *getValueAsDag(StringRef FieldName) const;
1555
1556 /// getValueAsCode - This method looks up the specified field and returns
1557 /// its value as the string data in a CodeInit, throwing an exception if the
1558 /// field does not exist or if the value is not a code object.
1559 ///
1560 std::string getValueAsCode(StringRef FieldName) const;
1561 };
1562
1563 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1564
1565 struct MultiClass {
1566 Record Rec; // Placeholder for template args and Name.
1567 typedef std::vector RecordVector;
1568 RecordVector DefPrototypes;
1569
1570 void dump() const;
1571
1572 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1573 Rec(Name, Loc, Records) {}
1574 };
1575
1576 class RecordKeeper {
1577 std::map Classes, Defs;
1578 public:
1579 ~RecordKeeper() {
1580 for (std::map::iterator I = Classes.begin(),
1581 E = Classes.end(); I != E; ++I)
1582 delete I->second;
1583 for (std::map::iterator I = Defs.begin(),
1584 E = Defs.end(); I != E; ++I)
1585 delete I->second;
1586 }
1587
1588 const std::map &getClasses() const { return Classes; }
1589 const std::map &getDefs() const { return Defs; }
1590
1591 Record *getClass(const std::string &Name) const {
1592 std::map::const_iterator I = Classes.find(Name);
1593 return I == Classes.end() ? 0 : I->second;
1594 }
1595 Record *getDef(const std::string &Name) const {
1596 std::map::const_iterator I = Defs.find(Name);
1597 return I == Defs.end() ? 0 : I->second;
1598 }
1599 void addClass(Record *R) {
1600 assert(getClass(R->getName()) == 0 && "Class already exists!");
1601 Classes.insert(std::make_pair(R->getName(), R));
1602 }
1603 void addDef(Record *R) {
1604 assert(getDef(R->getName()) == 0 && "Def already exists!");
1605 Defs.insert(std::make_pair(R->getName(), R));
1606 }
1607
1608 /// removeClass - Remove, but do not delete, the specified record.
1609 ///
1610 void removeClass(const std::string &Name) {
1611 assert(Classes.count(Name) && "Class does not exist!");
1612 Classes.erase(Name);
1613 }
1614 /// removeDef - Remove, but do not delete, the specified record.
1615 ///
1616 void removeDef(const std::string &Name) {
1617 assert(Defs.count(Name) && "Def does not exist!");
1618 Defs.erase(Name);
1619 }
1620
1621 //===--------------------------------------------------------------------===//
1622 // High-level helper methods, useful for tablegen backends...
1623
1624 /// getAllDerivedDefinitions - This method returns all concrete definitions
1625 /// that derive from the specified class name. If a class with the specified
1626 /// name does not exist, an exception is thrown.
1627 std::vector
1628 getAllDerivedDefinitions(const std::string &ClassName) const;
1629
1630 void dump() const;
1631 };
1632
1633 /// LessRecord - Sorting predicate to sort record pointers by name.
1634 ///
1635 struct LessRecord {
1636 bool operator()(const Record *Rec1, const Record *Rec2) const {
1637 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1638 }
1639 };
1640
1641 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1642 /// name field.
1643 ///
1644 struct LessRecordFieldName {
1645 bool operator()(const Record *Rec1, const Record *Rec2) const {
1646 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1647 }
1648 };
1649
1650 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1651
1652 } // End llvm namespace
1653
1654 #endif
0 //===- llvm/TableGen/TableGenAction.h - defines TableGenAction --*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the TableGenAction base class to be derived from by
10 // tblgen tools.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TABLEGEN_TABLEGENACTION_H
15 #define LLVM_TABLEGEN_TABLEGENACTION_H
16
17 namespace llvm {
18
19 class raw_ostream;
20 class RecordKeeper;
21
22 class TableGenAction {
23 public:
24 virtual ~TableGenAction() {}
25
26 /// Perform the action using Records, and write output to OS.
27 /// @returns true on error, false otherwise
28 virtual bool operator()(raw_ostream &OS, RecordKeeper &Records) = 0;
29 };
30
31 }
32
33 #endif
0 //===- llvm/TableGen/TableGenBackend.h - Backend base class -----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The TableGenBackend class is provided as a common interface for all TableGen
10 // backends. It provides useful services and an standardized interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TABLEGEN_TABLEGENBACKEND_H
15 #define LLVM_TABLEGEN_TABLEGENBACKEND_H
16
17 #include "llvm/Support/raw_ostream.h"
18 #include
19
20 namespace llvm {
21
22 class Record;
23 class RecordKeeper;
24
25 struct TableGenBackend {
26 virtual ~TableGenBackend() {}
27
28 // run - All TableGen backends should implement the run method, which should
29 // be the main entry point.
30 virtual void run(raw_ostream &OS) = 0;
31
32
33 public: // Useful helper routines...
34 /// EmitSourceFileHeader - Output a LLVM style file header to the specified
35 /// ostream.
36 void EmitSourceFileHeader(const std::string &Desc, raw_ostream &OS) const;
37
38 };
39
40 } // End llvm namespace
41
42 #endif
None # `Support' library is added on the top-level CMakeLists.txt
0 # `Support' and `TableGen' libraries are added on the top-level CMakeLists.txt
11
22 add_subdirectory(VMCore)
33 add_subdirectory(CodeGen)
0 ## FIXME: This only requires RTTI because tblgen uses it. Fix that.
1 set(LLVM_REQUIRES_RTTI 1)
2 set(LLVM_REQUIRES_EH 1)
3
4 add_llvm_library(LLVMTableGen
5 Error.cpp
6 Main.cpp
7 Record.cpp
8 TableGenBackend.cpp
9 TGLexer.cpp
10 TGParser.cpp
11 )
12
13 add_llvm_library_dependencies(LLVMTableGen
14 LLVMSupport
15 )
0 //===- Error.cpp - tblgen error handling helper routines --------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains error handling helper routines to pretty-print diagnostic
10 // messages from tblgen.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/TableGen/Error.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/raw_ostream.h"
17
18 namespace llvm {
19
20 SourceMgr SrcMgr;
21
22 void PrintError(SMLoc ErrorLoc, const Twine &Msg) {
23 SrcMgr.PrintMessage(ErrorLoc, Msg, "error");
24 }
25
26 void PrintError(const char *Loc, const Twine &Msg) {
27 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg, "error");
28 }
29
30 void PrintError(const Twine &Msg) {
31 errs() << "error:" << Msg << "\n";
32 }
33
34 void PrintError(const TGError &Error) {
35 PrintError(Error.getLoc(), Error.getMessage());
36 }
37
38 } // end namespace llvm
0 //===- Main.cpp - Top-Level TableGen implementation -----------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // TableGen is a tool which can be used to build up a description of something,
10 // then invoke one or more "tablegen backends" to emit information about the
11 // description in some predefined format. In practice, this is used by the LLVM
12 // code generators to automate generation of a code generator through a
13 // high-level description of the target.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "TGParser.h"
18 #include "llvm/ADT/OwningPtr.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/ToolOutputFile.h"
22 #include "llvm/Support/system_error.h"
23 #include "llvm/TableGen/Error.h"
24 #include "llvm/TableGen/Record.h"
25 #include "llvm/TableGen/TableGenAction.h"
26 #include
27 #include
28 using namespace llvm;
29
30 namespace {
31 cl::opt
32 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
33 cl::init("-"));
34
35 cl::opt
36 DependFilename("d", cl::desc("Dependency filename"), cl::value_desc("filename"),
37 cl::init(""));
38
39 cl::opt
40 InputFilename(cl::Positional, cl::desc(""), cl::init("-"));
41
42 cl::list
43 IncludeDirs("I", cl::desc("Directory of include files"),
44 cl::value_desc("directory"), cl::Prefix);
45 }
46
47 namespace llvm {
48
49 int TableGenMain(char *argv0, TableGenAction &Action) {
50 RecordKeeper Records;
51
52 try {
53 // Parse the input file.
54 OwningPtr File;
55 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) {
56 errs() << "Could not open input file '" << InputFilename << "': "
57 << ec.message() <<"\n";
58 return 1;
59 }
60 MemoryBuffer *F = File.take();
61
62 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
63 SrcMgr.AddNewSourceBuffer(F, SMLoc());
64
65 // Record the location of the include directory so that the lexer can find
66 // it later.
67 SrcMgr.setIncludeDirs(IncludeDirs);
68
69 TGParser Parser(SrcMgr, Records);
70
71 if (Parser.ParseFile())
72 return 1;
73
74 std::string Error;
75 tool_output_file Out(OutputFilename.c_str(), Error);
76 if (!Error.empty()) {
77 errs() << argv0 << ": error opening " << OutputFilename
78 << ":" << Error << "\n";
79 return 1;
80 }
81 if (!DependFilename.empty()) {
82 if (OutputFilename == "-") {
83 errs() << argv0 << ": the option -d must be used together with -o\n";
84 return 1;
85 }
86 tool_output_file DepOut(DependFilename.c_str(), Error);
87 if (!Error.empty()) {
88 errs() << argv0 << ": error opening " << DependFilename
89 << ":" << Error << "\n";
90 return 1;
91 }
92 DepOut.os() << OutputFilename << ":";
93 const std::vector &Dependencies = Parser.getDependencies();
94 for (std::vector::const_iterator I = Dependencies.begin(),
95 E = Dependencies.end();
96 I != E; ++I) {
97 DepOut.os() << " " << (*I);
98 }
99 DepOut.os() << "\n";
100 DepOut.keep();
101 }
102
103 if (Action(Out.os(), Records))
104 return 1;
105
106 // Declare success.
107 Out.keep();
108 return 0;
109
110 } catch (const TGError &Error) {
111 PrintError(Error);
112 } catch (const std::string &Error) {
113 PrintError(Error);
114 } catch (const char *Error) {
115 PrintError(Error);
116 } catch (...) {
117 errs() << argv0 << ": Unknown unexpected exception occurred.\n";
118 }
119
120 return 1;
121 }
122
123 }
0 ##===- lib/TableGen/Makefile -------------------------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8
9 LEVEL = ../..
10 LIBRARYNAME = LLVMTableGen
11 BUILD_ARCHIVE = 1
12
13 ## FIXME: This only requires RTTI because tblgen uses it. Fix that.
14 REQUIRES_RTTI = 1
15 REQUIRES_EH = 1
16
17 include $(LEVEL)/Makefile.common
0 //===- Record.cpp - Record implementation ---------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implement the tablegen record classes.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/TableGen/Record.h"
14 #include "llvm/TableGen/Error.h"
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/StringMap.h"
24
25 using namespace llvm;
26
27 //===----------------------------------------------------------------------===//
28 // std::string wrapper for DenseMap purposes
29 //===----------------------------------------------------------------------===//
30
31 /// TableGenStringKey - This is a wrapper for std::string suitable for
32 /// using as a key to a DenseMap. Because there isn't a particularly
33 /// good way to indicate tombstone or empty keys for strings, we want
34 /// to wrap std::string to indicate that this is a "special" string
35 /// not expected to take on certain values (those of the tombstone and
36 /// empty keys). This makes things a little safer as it clarifies
37 /// that DenseMap is really not appropriate for general strings.
38
39 class TableGenStringKey {
40 public:
41 TableGenStringKey(const std::string &str) : data(str) {}
42 TableGenStringKey(const char *str) : data(str) {}
43
44 const std::string &str() const { return data; }
45
46 private:
47 std::string data;
48 };
49
50 /// Specialize DenseMapInfo for TableGenStringKey.
51 namespace llvm {
52
53 template<> struct DenseMapInfo {
54 static inline TableGenStringKey getEmptyKey() {
55 TableGenStringKey Empty("<<>>");
56 return Empty;
57 }
58 static inline TableGenStringKey getTombstoneKey() {
59 TableGenStringKey Tombstone("<<>>");
60 return Tombstone;
61 }
62 static unsigned getHashValue(const TableGenStringKey& Val) {
63 return HashString(Val.str());
64 }
65 static bool isEqual(const TableGenStringKey& LHS,
66 const TableGenStringKey& RHS) {
67 return LHS.str() == RHS.str();
68 }
69 };
70
71 }
72
73 //===----------------------------------------------------------------------===//
74 // Type implementations
75 //===----------------------------------------------------------------------===//
76
77 BitRecTy BitRecTy::Shared;
78 IntRecTy IntRecTy::Shared;
79 StringRecTy StringRecTy::Shared;
80 CodeRecTy CodeRecTy::Shared;
81 DagRecTy DagRecTy::Shared;
82
83 void RecTy::dump() const { print(errs()); }
84
85 ListRecTy *RecTy::getListTy() {
86 if (!ListTy)
87 ListTy = new ListRecTy(this);
88 return ListTy;
89 }
90
91 Init *BitRecTy::convertValue(BitsInit *BI) {
92 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
93 return BI->getBit(0);
94 }
95
96 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
97 return RHS->getNumBits() == 1;
98 }
99
100 Init *BitRecTy::convertValue(IntInit *II) {
101 int64_t Val = II->getValue();
102 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
103
104 return BitInit::get(Val != 0);
105 }
106
107 Init *BitRecTy::convertValue(TypedInit *VI) {
108 if (dynamic_cast(VI->getType()))
109 return VI; // Accept variable if it is already of bit type!
110 return 0;
111 }
112
113 BitsRecTy *BitsRecTy::get(unsigned Sz) {
114 static std::vector Shared;
115 if (Sz >= Shared.size())
116 Shared.resize(Sz + 1);
117 BitsRecTy *&Ty = Shared[Sz];
118 if (!Ty)
119 Ty = new BitsRecTy(Sz);
120 return Ty;
121 }
122
123 std::string BitsRecTy::getAsString() const {
124 return "bits<" + utostr(Size) + ">";
125 }
126
127 Init *BitsRecTy::convertValue(UnsetInit *UI) {
128 SmallVector NewBits(Size);
129
130 for (unsigned i = 0; i != Size; ++i)
131 NewBits[i] = UnsetInit::get();
132
133 return BitsInit::get(NewBits);
134 }
135
136 Init *BitsRecTy::convertValue(BitInit *UI) {
137 if (Size != 1) return 0; // Can only convert single bit.
138 return BitsInit::get(UI);
139 }
140
141 /// canFitInBitfield - Return true if the number of bits is large enough to hold
142 /// the integer value.
143 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
144 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
145 return (NumBits >= sizeof(Value) * 8) ||
146 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
147 }
148
149 /// convertValue from Int initializer to bits type: Split the integer up into the
150 /// appropriate bits.
151 ///
152 Init *BitsRecTy::convertValue(IntInit *II) {
153 int64_t Value = II->getValue();
154 // Make sure this bitfield is large enough to hold the integer value.
155 if (!canFitInBitfield(Value, Size))
156 return 0;
157
158 SmallVector NewBits(Size);
159
160 for (unsigned i = 0; i != Size; ++i)
161 NewBits[i] = BitInit::get(Value & (1LL << i));
162
163 return BitsInit::get(NewBits);
164 }
165
166 Init *BitsRecTy::convertValue(BitsInit *BI) {
167 // If the number of bits is right, return it. Otherwise we need to expand or
168 // truncate.
169 if (BI->getNumBits() == Size) return BI;
170 return 0;
171 }
172
173 Init *BitsRecTy::convertValue(TypedInit *VI) {
174 if (BitsRecTy *BRT = dynamic_cast(VI->getType()))
175 if (BRT->Size == Size) {
176 SmallVector NewBits(Size);
177
178 for (unsigned i = 0; i != Size; ++i)
179 NewBits[i] = VarBitInit::get(VI, i);
180 return BitsInit::get(NewBits);
181 }
182
183 if (Size == 1 && dynamic_cast(VI->getType()))
184 return BitsInit::get(VI);
185
186 if (TernOpInit *Tern = dynamic_cast(VI)) {
187 if (Tern->getOpcode() == TernOpInit::IF) {
188 Init *LHS = Tern->getLHS();
189 Init *MHS = Tern->getMHS();
190 Init *RHS = Tern->getRHS();
191
192 IntInit *MHSi = dynamic_cast(MHS);
193 IntInit *RHSi = dynamic_cast(RHS);
194
195 if (MHSi && RHSi) {
196 int64_t MHSVal = MHSi->getValue();
197 int64_t RHSVal = RHSi->getValue();
198
199 if (canFitInBitfield(MHSVal, Size) && canFitInBitfield(RHSVal, Size)) {
200 SmallVector NewBits(Size);
201
202 for (unsigned i = 0; i != Size; ++i)
203 NewBits[i] =
204 TernOpInit::get(TernOpInit::IF, LHS,
205 IntInit::get((MHSVal & (1LL << i)) ? 1 : 0),
206 IntInit::get((RHSVal & (1LL << i)) ? 1 : 0),
207 VI->getType());
208
209 return BitsInit::get(NewBits);
210 }
211 } else {
212 BitsInit *MHSbs = dynamic_cast(MHS);
213 BitsInit *RHSbs = dynamic_cast(RHS);
214
215 if (MHSbs && RHSbs) {
216 SmallVector NewBits(Size);
217
218 for (unsigned i = 0; i != Size; ++i)
219 NewBits[i] = TernOpInit::get(TernOpInit::IF, LHS,
220 MHSbs->getBit(i),
221 RHSbs->getBit(i),
222 VI->getType());
223
224 return BitsInit::get(NewBits);
225 }
226 }
227 }
228 }
229
230 return 0;
231 }
232
233 Init *IntRecTy::convertValue(BitInit *BI) {
234 return IntInit::get(BI->getValue());
235 }
236
237 Init *IntRecTy::convertValue(BitsInit *BI) {
238 int64_t Result = 0;
239 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
240 if (BitInit *Bit = dynamic_cast(BI->getBit(i))) {
241 Result |= Bit->getValue() << i;
242 } else {
243 return 0;
244 }
245 return IntInit::get(Result);
246 }
247
248 Init *IntRecTy::convertValue(TypedInit *TI) {
249 if (TI->getType()->typeIsConvertibleTo(this))
250 return TI; // Accept variable if already of the right type!
251 return 0;
252 }
253
254 Init *StringRecTy::convertValue(UnOpInit *BO) {
255 if (BO->getOpcode() == UnOpInit::CAST) {
256 Init *L = BO->getOperand()->convertInitializerTo(this);
257 if (L == 0) return 0;
258 if (L != BO->getOperand())
259 return UnOpInit::get(UnOpInit::CAST, L, new StringRecTy);
260 return BO;
261 }
262
263 return convertValue((TypedInit*)BO);
264 }
265
266 Init *StringRecTy::convertValue(BinOpInit *BO) {
267 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
268 Init *L = BO->getLHS()->convertInitializerTo(this);
269 Init *R = BO->getRHS()->convertInitializerTo(this);
270 if (L == 0 || R == 0) return 0;
271 if (L != BO->getLHS() || R != BO->getRHS())
272 return BinOpInit::get(BinOpInit::STRCONCAT, L, R, new StringRecTy);
273 return BO;
274 }
275
276 return convertValue((TypedInit*)BO);
277 }
278
279
280 Init *StringRecTy::convertValue(TypedInit *TI) {
281 if (dynamic_cast(TI->getType()))
282 return TI; // Accept variable if already of the right type!
283 return 0;
284 }
285
286 std::string ListRecTy::getAsString() const {
287 return "list<" + Ty->getAsString() + ">";
288 }
289
290 Init *ListRecTy::convertValue(ListInit *LI) {
291 std::vector Elements;
292
293 // Verify that all of the elements of the list are subclasses of the
294 // appropriate class!
295 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
296 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
297 Elements.push_back(CI);
298 else
299 return 0;
300
301 ListRecTy *LType = dynamic_cast(LI->getType());
302 if (LType == 0) {
303 return 0;
304 }
305
306 return ListInit::get(Elements, this);
307 }
308
309 Init *ListRecTy::convertValue(TypedInit *TI) {
310 // Ensure that TI is compatible with our class.
311 if (ListRecTy *LRT = dynamic_cast(TI->getType()))
312 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
313 return TI;
314 return 0;
315 }
316
317 Init *CodeRecTy::convertValue(TypedInit *TI) {
318 if (TI->getType()->typeIsConvertibleTo(this))
319 return TI;
320 return 0;
321 }
322
323 Init *DagRecTy::convertValue(TypedInit *TI) {
324 if (TI->getType()->typeIsConvertibleTo(this))
325 return TI;
326 return 0;
327 }
328
329 Init *DagRecTy::convertValue(UnOpInit *BO) {
330 if (BO->getOpcode() == UnOpInit::CAST) {
331 Init *L = BO->getOperand()->convertInitializerTo(this);
332 if (L == 0) return 0;
333 if (L != BO->getOperand())
334 return UnOpInit::get(UnOpInit::CAST, L, new DagRecTy);
335 return BO;
336 }
337 return 0;
338 }
339
340 Init *DagRecTy::convertValue(BinOpInit *BO) {
341 if (BO->getOpcode() == BinOpInit::CONCAT) {
342 Init *L = BO->getLHS()->convertInitializerTo(this);
343 Init *R = BO->getRHS()->convertInitializerTo(this);
344 if (L == 0 || R == 0) return 0;
345 if (L != BO->getLHS() || R != BO->getRHS())
346 return BinOpInit::get(BinOpInit::CONCAT, L, R, new DagRecTy);
347 return BO;
348 }
349 return 0;
350 }
351
352 RecordRecTy *RecordRecTy::get(Record *R) {
353 return &dynamic_cast(*R->getDefInit()->getType());
354 }
355
356 std::string RecordRecTy::getAsString() const {
357 return Rec->getName();
358 }
359
360 Init *RecordRecTy::convertValue(DefInit *DI) {
361 // Ensure that DI is a subclass of Rec.
362 if (!DI->getDef()->isSubClassOf(Rec))
363 return 0;
364 return DI;
365 }
366
367 Init *RecordRecTy::convertValue(TypedInit *TI) {
368 // Ensure that TI is compatible with Rec.
369 if (RecordRecTy *RRT = dynamic_cast(TI->getType()))
370 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
371 RRT->getRecord() == getRecord())
372 return TI;
373 return 0;
374 }
375
376 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
377 if (Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec))
378 return true;
379
380 const std::vector &SC = Rec->getSuperClasses();
381 for (unsigned i = 0, e = SC.size(); i != e; ++i)
382 if (RHS->getRecord()->isSubClassOf(SC[i]))
383 return true;
384
385 return false;
386 }
387
388
389 /// resolveTypes - Find a common type that T1 and T2 convert to.
390 /// Return 0 if no such type exists.
391 ///
392 RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
393 if (!T1->typeIsConvertibleTo(T2)) {
394 if (!T2->typeIsConvertibleTo(T1)) {
395 // If one is a Record type, check superclasses
396 RecordRecTy *RecTy1 = dynamic_cast(T1);
397 if (RecTy1) {
398 // See if T2 inherits from a type T1 also inherits from
399 const std::vector &T1SuperClasses =
400 RecTy1->getRecord()->getSuperClasses();
401 for(std::vector::const_iterator i = T1SuperClasses.begin(),
402 iend = T1SuperClasses.end();
403 i != iend;
404 ++i) {
405 RecordRecTy *SuperRecTy1 = RecordRecTy::get(*i);
406 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
407 if (NewType1 != 0) {
408 if (NewType1 != SuperRecTy1) {
409 delete SuperRecTy1;
410 }
411 return NewType1;
412 }
413 }
414 }
415 RecordRecTy *RecTy2 = dynamic_cast(T2);
416 if (RecTy2) {
417 // See if T1 inherits from a type T2 also inherits from
418 const std::vector &T2SuperClasses =
419 RecTy2->getRecord()->getSuperClasses();
420 for (std::vector::const_iterator i = T2SuperClasses.begin(),
421 iend = T2SuperClasses.end();
422 i != iend;
423 ++i) {
424 RecordRecTy *SuperRecTy2 = RecordRecTy::get(*i);
425 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
426 if (NewType2 != 0) {
427 if (NewType2 != SuperRecTy2) {
428 delete SuperRecTy2;
429 }
430 return NewType2;
431 }
432 }
433 }
434 return 0;
435 }
436 return T2;
437 }
438 return T1;
439 }
440
441
442 //===----------------------------------------------------------------------===//
443 // Initializer implementations
444 //===----------------------------------------------------------------------===//
445
446 void Init::dump() const { return print(errs()); }
447
448 UnsetInit *UnsetInit::get() {
449 static UnsetInit TheInit;
450 return &TheInit;
451 }
452
453 BitInit *BitInit::get(bool V) {
454 static BitInit True(true);
455 static BitInit False(false);
456
457 return V ? &True : &False;
458 }
459
460 static void
461 ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef Range) {
462 ID.AddInteger(Range.size());
463
464 for (ArrayRef::iterator i = Range.begin(),
465 iend = Range.end();
466 i != iend;
467 ++i)
468 ID.AddPointer(*i);
469 }
470
471 BitsInit *BitsInit::get(ArrayRef Range) {
472 typedef FoldingSet Pool;
473 static Pool ThePool;
474
475 FoldingSetNodeID ID;
476 ProfileBitsInit(ID, Range);
477
478 void *IP = 0;
479 if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
480 return I;
481
482 BitsInit *I = new BitsInit(Range);
483 ThePool.InsertNode(I, IP);
484
485 return I;
486 }
487
488 void BitsInit::Profile(FoldingSetNodeID &ID) const {
489 ProfileBitsInit(ID, Bits);
490 }
491
492 Init *
493 BitsInit::convertInitializerBitRange(const std::vector &Bits) const {
494 SmallVector NewBits(Bits.size());
495
496 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
497 if (Bits[i] >= getNumBits())
498 return 0;
499 NewBits[i] = getBit(Bits[i]);
500 }
501 return BitsInit::get(NewBits);
502 }
503
504 std::string BitsInit::getAsString() const {
505 std::string Result = "{ ";
506 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
507 if (i) Result += ", ";
508 if (Init *Bit = getBit(e-i-1))
509 Result += Bit->getAsString();
510 else
511 Result += "*";
512 }
513 return Result + " }";
514 }
515
516 // resolveReferences - If there are any field references that refer to fields
517 // that have been filled in, we can propagate the values now.
518 //
519 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const {
520 bool Changed = false;
521 SmallVector NewBits(getNumBits());
522
523 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
524 Init *B;
525 Init *CurBit = getBit(i);
526
527 do {
528 B = CurBit;
529 CurBit = CurBit->resolveReferences(R, RV);
530 Changed |= B != CurBit;
531 } while (B != CurBit);
532 NewBits[i] = CurBit;
533 }
534
535 if (Changed)
536 return BitsInit::get(NewBits);
537
538 return const_cast(this);
539 }
540
541 IntInit *IntInit::get(int64_t V) {
542 typedef DenseMap Pool;
543 static Pool ThePool;
544
545 IntInit *&I = ThePool[V];
546 if (!I) I = new IntInit(V);
547 return I;
548 }
549
550 std::string IntInit::getAsString() const {
551 return itostr(Value);
552 }
553
554 Init *
555 IntInit::convertInitializerBitRange(const std::vector &Bits) const {
556 SmallVector NewBits(Bits.size());
557
558 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
559 if (Bits[i] >= 64)
560 return 0;
561
562 NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i]));
563 }
564 return BitsInit::get(NewBits);
565 }
566
567 StringInit *StringInit::get(const std::string &V) {
568 typedef StringMap Pool;
569 static Pool ThePool;
570
571 StringInit *&I = ThePool[V];
572 if (!I) I = new StringInit(V);
573 return I;
574 }
575
576 CodeInit *CodeInit::get(const std::string &V) {
577 typedef StringMap Pool;
578 static Pool ThePool;
579
580 CodeInit *&I = ThePool[V];
581 if (!I) I = new CodeInit(V);
582 return I;
583 }
584
585 static void ProfileListInit(FoldingSetNodeID &ID,
586 ArrayRef Range,
587 RecTy *EltTy) {
588 ID.AddInteger(Range.size());
589 ID.AddPointer(EltTy);
590
591 for (ArrayRef::iterator i = Range.begin(),
592 iend = Range.end();
593 i != iend;
594 ++i)
595 ID.AddPointer(*i);
596 }
597
598 ListInit *ListInit::get(ArrayRef Range, RecTy *EltTy) {
599 typedef FoldingSet Pool;
600 static Pool ThePool;
601
602 // Just use the FoldingSetNodeID to compute a hash. Use a DenseMap
603 // for actual storage.
604 FoldingSetNodeID ID;
605 ProfileListInit(ID, Range, EltTy);
606
607 void *IP = 0;
608 if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
609 return I;
610
611 ListInit *I = new ListInit(Range, EltTy);
612 ThePool.InsertNode(I, IP);
613 return I;
614 }
615
616 void ListInit::Profile(FoldingSetNodeID &ID) const {
617 ListRecTy *ListType = dynamic_cast(getType());
618 assert(ListType && "Bad type for ListInit!");
619 RecTy *EltTy = ListType->getElementType();
620
621 ProfileListInit(ID, Values, EltTy);
622 }
623
624 Init *
625 ListInit::convertInitListSlice(const std::vector &Elements) const {
626 std::vector Vals;
627 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
628 if (Elements[i] >= getSize())
629 return 0;
630 Vals.push_back(getElement(Elements[i]));
631 }
632 return ListInit::get(Vals, getType());
633 }
634
635 Record *ListInit::getElementAsRecord(unsigned i) const {
636 assert(i < Values.size() && "List element index out of range!");
637 DefInit *DI = dynamic_cast(Values[i]);
638 if (DI == 0) throw "Expected record in list!";
639 return DI->getDef();
640 }
641
642 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const {
643 std::vector Resolved;
644 Resolved.reserve(getSize());
645 bool Changed = false;
646
647 for (unsigned i = 0, e = getSize(); i != e; ++i) {
648 Init *E;
649 Init *CurElt = getElement(i);
650
651 do {
652 E = CurElt;
653 CurElt = CurElt->resolveReferences(R, RV);
654 Changed |= E != CurElt;
655 } while (E != CurElt);
656 Resolved.push_back(E);
657 }
658
659 if (Changed)
660 return ListInit::get(Resolved, getType());
661 return const_cast(this);
662 }
663
664 Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
665 unsigned Elt) const {
666 if (Elt >= getSize())
667 return 0; // Out of range reference.
668 Init *E = getElement(Elt);
669 // If the element is set to some value, or if we are resolving a reference
670 // to a specific variable and that variable is explicitly unset, then
671 // replace the VarListElementInit with it.
672 if (IRV || !dynamic_cast(E))
673 return E;
674 return 0;
675 }
676
677 std::string ListInit::getAsString() const {
678 std::string Result = "[";
679 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
680 if (i) Result += ", ";
681 Result += Values[i]->getAsString();
682 }
683 return Result + "]";
684 }
685
686 Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
687 unsigned Bit) const {
688 Init *Folded = Fold(&R, 0);
689
690 if (Folded != this) {
691 TypedInit *Typed = dynamic_cast(Folded);
692 if (Typed) {
693 return Typed->resolveBitReference(R, IRV, Bit);
694 }
695 }
696
697 return 0;
698 }
699
700 Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
701 unsigned Elt) const {
702 Init *Folded = Fold(&R, 0);
703
704 if (Folded != this) {
705 TypedInit *Typed = dynamic_cast(Folded);
706 if (Typed) {
707 return Typed->resolveListElementReference(R, IRV, Elt);
708 }
709 }
710
711 return 0;
712 }
713
714 UnOpInit *UnOpInit::get(UnaryOp opc, Init *lhs, RecTy *Type) {
715 typedef std::pair, RecTy *> Key;
716
717 typedef DenseMap Pool;
718 static Pool ThePool;
719
720 Key TheKey(std::make_pair(std::make_pair(opc, lhs), Type));
721
722 UnOpInit *&I = ThePool[TheKey];
723 if (!I) I = new UnOpInit(opc, lhs, Type);
724 return I;
725 }
726
727 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
728 switch (getOpcode()) {
729 default: assert(0 && "Unknown unop");
730 case CAST: {
731 if (getType()->getAsString() == "string") {
732 StringInit *LHSs = dynamic_cast(LHS);
733 if (LHSs) {
734 return LHSs;
735 }
736
737 DefInit *LHSd = dynamic_cast(LHS);
738 if (LHSd) {
739 return StringInit::get(LHSd->getDef()->getName());
740 }
741 } else {
742 StringInit *LHSs = dynamic_cast(LHS);
743 if (LHSs) {
744 std::string Name = LHSs->getValue();
745
746 // From TGParser::ParseIDValue
747 if (CurRec) {
748 if (const RecordVal *RV = CurRec->getValue(Name)) {
749 if (RV->getType() != getType())
750 throw "type mismatch in cast";
751 return VarInit::get(Name, RV->getType());
752 }
753
754 std::string TemplateArgName = CurRec->getName()+":"+Name;
755 if (CurRec->isTemplateArg(TemplateArgName)) {
756 const RecordVal *RV = CurRec->getValue(TemplateArgName);
757 assert(RV && "Template arg doesn't exist??");
758
759 if (RV->getType() != getType())
760 throw "type mismatch in cast";
761
762 return VarInit::get(TemplateArgName, RV->getType());
763 }
764 }
765
766 if (CurMultiClass) {
767 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
768 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
769 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
770 assert(RV && "Template arg doesn't exist??");
771
772 if (RV->getType() != getType())
773 throw "type mismatch in cast";
774
775 return VarInit::get(MCName, RV->getType());
776 }
777 }
778
779 if (Record *D = (CurRec->getRecords()).getDef(Name))
780 return DefInit::get(D);
781
782 throw TGError(CurRec->getLoc(), "Undefined reference:'" + Name + "'\n");
783 }
784 }
785 break;
786 }
787 case HEAD: {
788 ListInit *LHSl = dynamic_cast(LHS);
789 if (LHSl) {
790 if (LHSl->getSize() == 0) {
791 assert(0 && "Empty list in car");
792 return 0;
793 }
794 return LHSl->getElement(0);
795 }
796 break;
797 }
798 case TAIL: {
799 ListInit *LHSl = dynamic_cast(LHS);
800 if (LHSl) {
801 if (LHSl->getSize() == 0) {
802 assert(0 && "Empty list in cdr");
803 return 0;
804 }
805 // Note the +1. We can't just pass the result of getValues()
806 // directly.
807 ArrayRef::iterator begin = LHSl->getValues().begin()+1;
808 ArrayRef::iterator end = LHSl->getValues().end();
809 ListInit *Result =
810 ListInit::get(ArrayRef(begin, end - begin),
811 LHSl->getType());
812 return Result;
813 }
814 break;
815 }
816 case EMPTY: {
817 ListInit *LHSl = dynamic_cast(LHS);
818 if (LHSl) {
819 if (LHSl->getSize() == 0) {
820 return IntInit::get(1);
821 } else {
822 return IntInit::get(0);
823 }
824 }
825 StringInit *LHSs = dynamic_cast(LHS);
826 if (LHSs) {
827 if (LHSs->getValue().empty()) {
828 return IntInit::get(1);
829 } else {
830 return IntInit::get(0);
831 }
832 }
833
834 break;
835 }
836 }
837 return const_cast(this);
838 }
839
840 Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
841 Init *lhs = LHS->resolveReferences(R, RV);
842
843 if (LHS != lhs)
844 return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, 0);
845 return Fold(&R, 0);
846 }
847
848 std::string UnOpInit::getAsString() const {
849 std::string Result;
850 switch (Opc) {
851 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
852 case HEAD: Result = "!head"; break;
853 case TAIL: Result = "!tail"; break;
854 case EMPTY: Result = "!empty"; break;
855 }
856 return Result + "(" + LHS->getAsString() + ")";
857 }
858
859 BinOpInit *BinOpInit::get(BinaryOp opc, Init *lhs,
860 Init *rhs, RecTy *Type) {
861 typedef std::pair<
862 std::pair, Init *>,
863 RecTy *
864 > Key;
865
866 typedef DenseMap Pool;
867 static Pool ThePool;
868
869 Key TheKey(std::make_pair(std::make_pair(std::make_pair(opc, lhs), rhs),
870 Type));
871
872 BinOpInit *&I = ThePool[TheKey];
873 if (!I) I = new BinOpInit(opc, lhs, rhs, Type);
874 return I;
875 }
876
877 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
878 switch (getOpcode()) {
879 default: assert(0 && "Unknown binop");
880 case CONCAT: {
881 DagInit *LHSs = dynamic_cast(LHS);
882 DagInit *RHSs = dynamic_cast(RHS);
883 if (LHSs && RHSs) {
884 DefInit *LOp = dynamic_cast(LHSs->getOperator());
885 DefInit *ROp = dynamic_cast(RHSs->getOperator());
886 if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef())
887 throw "Concated Dag operators do not match!";
888 std::vector Args;
889 std::vector ArgNames;
890 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
891 Args.push_back(LHSs->getArg(i));
892 ArgNames.push_back(LHSs->getArgName(i));
893 }
894 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
895 Args.push_back(RHSs->getArg(i));
896 ArgNames.push_back(RHSs->getArgName(i));
897 }
898 return DagInit::get(LHSs->getOperator(), "", Args, ArgNames);
899 }
900 break;
901 }
902 case STRCONCAT: {
903 StringInit *LHSs = dynamic_cast(LHS);
904 StringInit *RHSs = dynamic_cast(RHS);
905 if (LHSs && RHSs)
906 return StringInit::get(LHSs->getValue() + RHSs->getValue());
907 break;
908 }
909 case EQ: {
910 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
911 // to string objects.
912 IntInit* L =
913 dynamic_cast(LHS->convertInitializerTo(IntRecTy::get()));
914 IntInit* R =
915 dynamic_cast(RHS->convertInitializerTo(IntRecTy::get()));
916
917 if (L && R)
918 return IntInit::get(L->getValue() == R->getValue());
919
920 StringInit *LHSs = dynamic_cast(LHS);
921 StringInit *RHSs = dynamic_cast(RHS);
922
923 // Make sure we've resolved
924 if (LHSs && RHSs)
925 return IntInit::get(LHSs->getValue() == RHSs->getValue());
926
927 break;
928 }
929 case SHL:
930 case SRA:
931 case SRL: {
932 IntInit *LHSi = dynamic_cast(LHS);
933 IntInit *RHSi = dynamic_cast(RHS);
934 if (LHSi && RHSi) {
935 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
936 int64_t Result;
937 switch (getOpcode()) {
938 default: assert(0 && "Bad opcode!");
939 case SHL: Result = LHSv << RHSv; break;
940 case SRA: Result = LHSv >> RHSv; break;
941 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
942 }
943 return IntInit::get(Result);
944 }
945 break;
946 }
947 }
948 return const_cast(this);
949 }
950
951 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
952 Init *lhs = LHS->resolveReferences(R, RV);
953 Init *rhs = RHS->resolveReferences(R, RV);
954
955 if (LHS != lhs || RHS != rhs)
956 return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
957 return Fold(&R, 0);
958 }
959
960 std::string BinOpInit::getAsString() const {
961 std::string Result;
962 switch (Opc) {
963 case CONCAT: Result = "!con"; break;
964 case SHL: Result = "!shl"; break;
965 case SRA: Result = "!sra"; break;
966 case SRL: Result = "!srl"; break;
967 case EQ: Result = "!eq"; break;
968 case STRCONCAT: Result = "!strconcat"; break;
969 }
970 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
971 }
972
973 TernOpInit *TernOpInit::get(TernaryOp opc, Init *lhs,
974 Init *mhs, Init *rhs,
975 RecTy *Type) {
976 typedef std::pair<
977 std::pair<
978 std::pair, Init *>,
979 Init *
980 >,
981 Init *
982 > Key;
983
984 typedef DenseMap Pool;
985 static Pool ThePool;
986
987 Key TheKey(std::make_pair(std::make_pair(std::make_pair(std::make_pair(opc,
988 Type),
989 lhs),
990 mhs),
991 rhs));
992
993 TernOpInit *&I = ThePool[TheKey];
994 if (!I) I = new TernOpInit(opc, lhs, mhs, rhs, Type);
995 return I;
996 }
997
998 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
999 Record *CurRec, MultiClass *CurMultiClass);
1000
1001 static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
1002 RecTy *Type, Record *CurRec,
1003 MultiClass *CurMultiClass) {
1004 std::vector NewOperands;
1005
1006 TypedInit *TArg = dynamic_cast(Arg);
1007
1008 // If this is a dag, recurse
1009 if (TArg && TArg->getType()->getAsString() == "dag") {
1010 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
1011 CurRec, CurMultiClass);
1012 if (Result != 0) {
1013 return Result;
1014 } else {
1015 return 0;
1016 }
1017 }
1018
1019 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
1020 OpInit *RHSoo = dynamic_cast(RHSo->getOperand(i));
1021
1022 if (RHSoo) {
1023 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
1024 Type, CurRec, CurMultiClass);
1025 if (Result != 0) {
1026 NewOperands.push_back(Result);
1027 } else {
1028 NewOperands.push_back(Arg);
1029 }
1030 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
1031 NewOperands.push_back(Arg);
1032 } else {
1033 NewOperands.push_back(RHSo->getOperand(i));
1034 }
1035 }
1036
1037 // Now run the operator and use its result as the new leaf
1038 const OpInit *NewOp = RHSo->clone(NewOperands);
1039 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
1040 if (NewVal != NewOp)
1041 return NewVal;
1042
1043 return 0;
1044 }
1045
1046 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
1047 Record *CurRec, MultiClass *CurMultiClass) {
1048 DagInit *MHSd = dynamic_cast(MHS);
1049 ListInit *MHSl = dynamic_cast(MHS);
1050
1051 DagRecTy *DagType = dynamic_cast(Type);
1052 ListRecTy *ListType = dynamic_cast(Type);
1053
1054 OpInit *RHSo = dynamic_cast(RHS);
1055
1056 if (!RHSo) {
1057 throw TGError(CurRec->getLoc(), "!foreach requires an operator\n");
1058 }
1059
1060 TypedInit *LHSt = dynamic_cast(LHS);
1061
1062 if (!LHSt) {
1063 throw TGError(CurRec->getLoc(), "!foreach requires typed variable\n");
1064 }
1065
1066 if ((MHSd && DagType) || (MHSl && ListType)) {
1067 if (MHSd) {
1068 Init *Val = MHSd->getOperator();
1069 Init *Result = EvaluateOperation(RHSo, LHS, Val,
1070 Type, CurRec, CurMultiClass);
1071 if (Result != 0) {
1072 Val = Result;
1073 }
1074
1075 std::vector > args;
1076 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
1077 Init *Arg;
1078 std::string ArgName;
1079 Arg = MHSd->getArg(i);
1080 ArgName = MHSd->getArgName(i);
1081
1082 // Process args
1083 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
1084 CurRec, CurMultiClass);
1085 if (Result != 0) {
1086 Arg = Result;
1087 }
1088
1089 // TODO: Process arg names
1090 args.push_back(std::make_pair(Arg, ArgName));
1091 }
1092
1093 return DagInit::get(Val, "", args);
1094 }
1095 if (MHSl) {
1096 std::vector NewOperands;
1097 std::vector NewList(MHSl->begin(), MHSl->end());
1098
1099 for (std::vector::iterator li = NewList.begin(),
1100 liend = NewList.end();
1101 li != liend;
1102 ++li) {
1103 Init *Item = *li;
1104 NewOperands.clear();
1105 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
1106 // First, replace the foreach variable with the list item
1107 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
1108 NewOperands.push_back(Item);
1109 } else {
1110 NewOperands.push_back(RHSo->getOperand(i));
1111 }
1112 }
1113
1114 // Now run the operator and use its result as the new list item
1115 const OpInit *NewOp = RHSo->clone(NewOperands);
1116 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
1117 if (NewItem != NewOp)
1118 *li = NewItem;
1119 }
1120 return ListInit::get(NewList, MHSl->getType());
1121 }
1122 }
1123 return 0;
1124 }
1125
1126 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
1127 switch (getOpcode()) {
1128 default: assert(0 && "Unknown binop");
1129 case SUBST: {
1130 DefInit *LHSd = dynamic_cast(LHS);
1131 VarInit *LHSv = dynamic_cast(LHS);
1132 StringInit *LHSs = dynamic_cast(LHS);
1133
1134 DefInit *MHSd = dynamic_cast(MHS);
1135 VarInit *MHSv = dynamic_cast(MHS);
1136 StringInit *MHSs = dynamic_cast(MHS);
1137
1138 DefInit *RHSd = dynamic_cast(RHS);
1139 VarInit *RHSv = dynamic_cast(RHS);
1140 StringInit *RHSs = dynamic_cast(RHS);
1141
1142 if ((LHSd && MHSd && RHSd)
1143 || (LHSv && MHSv && RHSv)
1144 || (LHSs && MHSs && RHSs)) {
1145 if (RHSd) {
1146 Record *Val = RHSd->getDef();
1147 if (LHSd->getAsString() == RHSd->getAsString()) {
1148 Val = MHSd->getDef();
1149 }
1150 return DefInit::get(Val);
1151 }
1152 if (RHSv) {
1153 std::string Val = RHSv->getName();
1154 if (LHSv->getAsString() == RHSv->getAsString()) {
1155 Val = MHSv->getName();
1156 }
1157 return VarInit::get(Val, getType());
1158 }
1159 if (RHSs) {
1160 std::string Val = RHSs->getValue();
1161
1162 std::string::size_type found;
1163 std::string::size_type idx = 0;
1164 do {
1165 found = Val.find(LHSs->getValue(), idx);
1166 if (found != std::string::npos) {
1167 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1168 }
1169 idx = found + MHSs->getValue().size();
1170 } while (found != std::string::npos);
1171
1172 return StringInit::get(Val);
1173 }
1174 }
1175 break;
1176 }
1177
1178 case FOREACH: {
1179 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
1180 CurRec, CurMultiClass);
1181 if (Result != 0) {
1182 return Result;
1183 }
1184 break;
1185 }
1186
1187 case IF: {
1188 IntInit *LHSi = dynamic_cast(LHS);
1189 if (Init *I = LHS->convertInitializerTo(IntRecTy::get()))
1190 LHSi = dynamic_cast(I);
1191 if (LHSi) {
1192 if (LHSi->getValue()) {
1193 return MHS;
1194 } else {
1195 return RHS;
1196 }
1197 }
1198 break;
1199 }
1200 }
1201
1202 return const_cast(this);
1203 }
1204
1205 Init *TernOpInit::resolveReferences(Record &R,
1206 const RecordVal *RV) const {
1207 Init *lhs = LHS->resolveReferences(R, RV);
1208
1209 if (Opc == IF && lhs != LHS) {
1210 IntInit *Value = dynamic_cast(lhs);
1211 if (Init *I = lhs->convertInitializerTo(IntRecTy::get()))
1212 Value = dynamic_cast(I);
1213 if (Value != 0) {
1214 // Short-circuit
1215 if (Value->getValue()) {
1216 Init *mhs = MHS->resolveReferences(R, RV);
1217 return (TernOpInit::get(getOpcode(), lhs, mhs,
1218 RHS, getType()))->Fold(&R, 0);
1219 } else {
1220 Init *rhs = RHS->resolveReferences(R, RV);
1221 return (TernOpInit::get(getOpcode(), lhs, MHS,
1222 rhs, getType()))->Fold(&R, 0);
1223 }
1224 }
1225 }
1226
1227 Init *mhs = MHS->resolveReferences(R, RV);
1228 Init *rhs = RHS->resolveReferences(R, RV);
1229
1230 if (LHS != lhs || MHS != mhs || RHS != rhs)
1231 return (TernOpInit::get(getOpcode(), lhs, mhs, rhs,
1232 getType()))->Fold(&R, 0);
1233 return Fold(&R, 0);
1234 }
1235
1236 std::string TernOpInit::getAsString() const {
1237 std::string Result;
1238 switch (Opc) {
1239 case SUBST: Result = "!subst"; break;
1240 case FOREACH: Result = "!foreach"; break;
1241 case IF: Result = "!if"; break;
1242 }
1243 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
1244 + RHS->getAsString() + ")";
1245 }
1246
1247 RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
1248 RecordRecTy *RecordType = dynamic_cast(getType());
1249 if (RecordType) {
1250 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
1251 if (Field) {
1252 return Field->getType();
1253 }
1254 }
1255 return 0;
1256 }
1257
1258 Init *
1259 TypedInit::convertInitializerBitRange(const std::vector &Bits) const {
1260 BitsRecTy *T = dynamic_cast(getType());
1261 if (T == 0) return 0; // Cannot subscript a non-bits variable.
1262 unsigned NumBits = T->getNumBits();
1263
1264 SmallVector NewBits(Bits.size());
1265 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1266 if (Bits[i] >= NumBits)
1267 return 0;
1268
1269 NewBits[i] = VarBitInit::get(const_cast(this), Bits[i]);
1270 }
1271 return BitsInit::get(NewBits);
1272 }
1273
1274 Init *
1275 TypedInit::convertInitListSlice(const std::vector &Elements) const {
1276 ListRecTy *T = dynamic_cast(getType());
1277 if (T == 0) return 0; // Cannot subscript a non-list variable.
1278
1279 if (Elements.size() == 1)
1280 return VarListElementInit::get(const_cast(this), Elements[0]);
1281
1282 std::vector ListInits;
1283 ListInits.reserve(Elements.size());
1284 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1285 ListInits.push_back(VarListElementInit::get(const_cast(this),
1286 Elements[i]));
1287 return ListInit::get(ListInits, T);
1288 }
1289
1290
1291 VarInit *VarInit::get(const std::string &VN, RecTy *T) {
1292 typedef std::pair Key;
1293 typedef DenseMap Pool;
1294 static Pool ThePool;
1295
1296 Key TheKey(std::make_pair(T, VN));
1297
1298 VarInit *&I = ThePool[TheKey];
1299 if (!I) I = new VarInit(VN, T);
1300 return I;
1301 }
1302
1303 Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1304 unsigned Bit) const {
1305 if (R.isTemplateArg(getName())) return 0;
1306 if (IRV && IRV->getName() != getName()) return 0;
1307
1308 RecordVal *RV = R.getValue(getName());
1309 assert(RV && "Reference to a non-existent variable?");
1310 assert(dynamic_cast(RV->getValue()));
1311 BitsInit *BI = (BitsInit*)RV->getValue();
1312
1313 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1314 Init *B = BI->getBit(Bit);
1315
1316 // If the bit is set to some value, or if we are resolving a reference to a
1317 // specific variable and that variable is explicitly unset, then replace the
1318 // VarBitInit with it.
1319 if (IRV || !dynamic_cast(B))
1320 return B;
1321 return 0;
1322 }
1323
1324 Init *VarInit::resolveListElementReference(Record &R,
1325 const RecordVal *IRV,
1326 unsigned Elt) const {
1327 if (R.isTemplateArg(getName())) return 0;
1328 if (IRV && IRV->getName() != getName()) return 0;
1329
1330 RecordVal *RV = R.getValue(getName());
1331 assert(RV && "Reference to a non-existent variable?");
1332 ListInit *LI = dynamic_cast(RV->getValue());
1333 if (!LI) {
1334 VarInit *VI = dynamic_cast(RV->getValue());
1335 assert(VI && "Invalid list element!");
1336 return VarListElementInit::get(VI, Elt);
1337 }
1338
1339 if (Elt >= LI->getSize())
1340 return 0; // Out of range reference.
1341 Init *E = LI->getElement(Elt);
1342 // If the element is set to some value, or if we are resolving a reference
1343 // to a specific variable and that variable is explicitly unset, then
1344 // replace the VarListElementInit with it.
1345 if (IRV || !dynamic_cast(E))
1346 return E;
1347 return 0;
1348 }
1349
1350
1351 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1352 if (RecordRecTy *RTy = dynamic_cast(getType()))
1353 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1354 return RV->getType();
1355 return 0;
1356 }
1357
1358 Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1359 const std::string &FieldName) const {
1360 if (dynamic_cast(getType()))
1361 if (const RecordVal *Val = R.getValue(VarName)) {
1362 if (RV != Val && (RV || dynamic_cast(Val->getValue())))
1363 return 0;
1364 Init *TheInit = Val->getValue();
1365 assert(TheInit != this && "Infinite loop detected!");
1366 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
1367 return I;
1368 else
1369 return 0;
1370 }
1371 return 0;
1372 }
1373
1374 /// resolveReferences - This method is used by classes that refer to other
1375 /// variables which may not be defined at the time the expression is formed.
1376 /// If a value is set for the variable later, this method will be called on
1377 /// users of the value to allow the value to propagate out.
1378 ///
1379 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const {
1380 if (RecordVal *Val = R.getValue(VarName))
1381 if (RV == Val || (RV == 0 && !dynamic_cast(Val->getValue())))
1382 return Val->getValue();
1383 return const_cast(this);
1384 }
1385
1386 VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
1387 typedef std::pair Key;
1388 typedef DenseMap Pool;
1389
1390 static Pool ThePool;
1391
1392 Key TheKey(std::make_pair(T, B));
1393
1394 VarBitInit *&I = ThePool[TheKey];
1395 if (!I) I = new VarBitInit(T, B);
1396 return I;
1397 }
1398
1399 std::string VarBitInit::getAsString() const {
1400 return TI->getAsString() + "{" + utostr(Bit) + "}";
1401 }
1402
1403 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const {
1404 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
1405 return I;
1406 return const_cast(this);
1407 }
1408
1409 VarListElementInit *VarListElementInit::get(TypedInit *T,
1410 unsigned E) {
1411 typedef std::pair Key;
1412 typedef DenseMap Pool;
1413
1414 static Pool ThePool;
1415
1416 Key TheKey(std::make_pair(T, E));
1417
1418 VarListElementInit *&I = ThePool[TheKey];
1419 if (!I) I = new VarListElementInit(T, E);
1420 return I;
1421 }
1422
1423 std::string VarListElementInit::getAsString() const {
1424 return TI->getAsString() + "[" + utostr(Element) + "]";
1425 }
1426
1427 Init *
1428 VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) const {
1429 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1430 getElementNum()))
1431 return I;
1432 return const_cast(this);
1433 }
1434
1435 Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1436 unsigned Bit) const {
1437 // FIXME: This should be implemented, to support references like:
1438 // bit B = AA[0]{1};
1439 return 0;
1440 }
1441
1442 Init *VarListElementInit:: resolveListElementReference(Record &R,
1443 const RecordVal *RV,
1444 unsigned Elt) const {
1445 Init *Result = TI->resolveListElementReference(R, RV, Element);
1446
1447 if (Result) {
1448 TypedInit *TInit = dynamic_cast(Result);
1449 if (TInit) {
1450 return TInit->resolveListElementReference(R, RV, Elt);
1451 }
1452 return Result;
1453 }
1454