make tblgen produce a function that returns the name for a physreg.
[llvm/avr.git] / utils / TableGen / Record.h
blob9415109dbb3b3095d632870fddaff2474d3e3013
1 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the main TableGen data structures, including the TableGen
11 // types, values, and high-level data structures.
13 //===----------------------------------------------------------------------===//
15 #ifndef RECORD_H
16 #define RECORD_H
18 #include "llvm/Support/SourceMgr.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <map>
23 namespace llvm {
24 class raw_ostream;
26 // RecTy subclasses.
27 class BitRecTy;
28 class BitsRecTy;
29 class IntRecTy;
30 class StringRecTy;
31 class ListRecTy;
32 class CodeRecTy;
33 class DagRecTy;
34 class RecordRecTy;
36 // Init subclasses.
37 struct Init;
38 class UnsetInit;
39 class BitInit;
40 class BitsInit;
41 class IntInit;
42 class StringInit;
43 class CodeInit;
44 class ListInit;
45 class UnOpInit;
46 class BinOpInit;
47 class TernOpInit;
48 class DefInit;
49 class DagInit;
50 class TypedInit;
51 class VarInit;
52 class FieldInit;
53 class VarBitInit;
54 class VarListElementInit;
56 // Other classes.
57 class Record;
58 class RecordVal;
59 struct MultiClass;
61 //===----------------------------------------------------------------------===//
62 // Type Classes
63 //===----------------------------------------------------------------------===//
65 struct RecTy {
66 virtual ~RecTy() {}
68 virtual std::string getAsString() const = 0;
69 void print(raw_ostream &OS) const { OS << getAsString(); }
70 void dump() const;
72 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
73 /// converted to the specified type.
74 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
76 public: // These methods should only be called from subclasses of Init
77 virtual Init *convertValue( UnsetInit *UI) { return 0; }
78 virtual Init *convertValue( BitInit *BI) { return 0; }
79 virtual Init *convertValue( BitsInit *BI) { return 0; }
80 virtual Init *convertValue( IntInit *II) { return 0; }
81 virtual Init *convertValue(StringInit *SI) { return 0; }
82 virtual Init *convertValue( ListInit *LI) { return 0; }
83 virtual Init *convertValue( UnOpInit *UI) {
84 return convertValue((TypedInit*)UI);
86 virtual Init *convertValue( BinOpInit *UI) {
87 return convertValue((TypedInit*)UI);
89 virtual Init *convertValue( TernOpInit *UI) {
90 return convertValue((TypedInit*)UI);
92 virtual Init *convertValue( CodeInit *CI) { return 0; }
93 virtual Init *convertValue(VarBitInit *VB) { return 0; }
94 virtual Init *convertValue( DefInit *DI) { return 0; }
95 virtual Init *convertValue( DagInit *DI) { return 0; }
96 virtual Init *convertValue( TypedInit *TI) { return 0; }
97 virtual Init *convertValue( VarInit *VI) {
98 return convertValue((TypedInit*)VI);
100 virtual Init *convertValue( FieldInit *FI) {
101 return convertValue((TypedInit*)FI);
104 public: // These methods should only be called by subclasses of RecTy.
105 // baseClassOf - These virtual methods should be overloaded to return true iff
106 // all values of type 'RHS' can be converted to the 'this' type.
107 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
108 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
109 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
110 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
111 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
112 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
113 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
114 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
117 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
118 Ty.print(OS);
119 return OS;
123 /// BitRecTy - 'bit' - Represent a single bit
125 class BitRecTy : public RecTy {
126 public:
127 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
128 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
129 virtual Init *convertValue( BitsInit *BI);
130 virtual Init *convertValue( IntInit *II);
131 virtual Init *convertValue(StringInit *SI) { return 0; }
132 virtual Init *convertValue( ListInit *LI) { return 0; }
133 virtual Init *convertValue( CodeInit *CI) { return 0; }
134 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
135 virtual Init *convertValue( DefInit *DI) { return 0; }
136 virtual Init *convertValue( DagInit *DI) { return 0; }
137 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
138 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
139 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
140 virtual Init *convertValue( TypedInit *TI);
141 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
142 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
144 std::string getAsString() const { return "bit"; }
146 bool typeIsConvertibleTo(const RecTy *RHS) const {
147 return RHS->baseClassOf(this);
149 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
150 virtual bool baseClassOf(const BitsRecTy *RHS) const;
151 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
152 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
153 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
154 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
155 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
156 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
161 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
162 /// BitsRecTy - 'bits&lt;n&gt;' - Represent a fixed number of bits
164 class BitsRecTy : public RecTy {
165 unsigned Size;
166 public:
167 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
169 unsigned getNumBits() const { return Size; }
171 virtual Init *convertValue( UnsetInit *UI);
172 virtual Init *convertValue( BitInit *UI);
173 virtual Init *convertValue( BitsInit *BI);
174 virtual Init *convertValue( IntInit *II);
175 virtual Init *convertValue(StringInit *SI) { return 0; }
176 virtual Init *convertValue( ListInit *LI) { return 0; }
177 virtual Init *convertValue( CodeInit *CI) { return 0; }
178 virtual Init *convertValue(VarBitInit *VB) { return 0; }
179 virtual Init *convertValue( DefInit *DI) { return 0; }
180 virtual Init *convertValue( DagInit *DI) { return 0; }
181 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
182 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
183 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
184 virtual Init *convertValue( TypedInit *TI);
185 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
186 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
188 std::string getAsString() const;
190 bool typeIsConvertibleTo(const RecTy *RHS) const {
191 return RHS->baseClassOf(this);
193 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
194 virtual bool baseClassOf(const BitsRecTy *RHS) const {
195 return RHS->Size == Size;
197 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
198 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
199 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
200 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
201 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
202 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
207 /// IntRecTy - 'int' - Represent an integer value of no particular size
209 class IntRecTy : public RecTy {
210 public:
211 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
212 virtual Init *convertValue( BitInit *BI);
213 virtual Init *convertValue( BitsInit *BI);
214 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
215 virtual Init *convertValue(StringInit *SI) { return 0; }
216 virtual Init *convertValue( ListInit *LI) { return 0; }
217 virtual Init *convertValue( CodeInit *CI) { return 0; }
218 virtual Init *convertValue(VarBitInit *VB) { return 0; }
219 virtual Init *convertValue( DefInit *DI) { return 0; }
220 virtual Init *convertValue( DagInit *DI) { return 0; }
221 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
222 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
223 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
224 virtual Init *convertValue( TypedInit *TI);
225 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
226 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
228 std::string getAsString() const { return "int"; }
230 bool typeIsConvertibleTo(const RecTy *RHS) const {
231 return RHS->baseClassOf(this);
234 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
235 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
236 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
237 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
238 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
239 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
240 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
241 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
245 /// StringRecTy - 'string' - Represent an string value
247 class StringRecTy : public RecTy {
248 public:
249 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
250 virtual Init *convertValue( BitInit *BI) { return 0; }
251 virtual Init *convertValue( BitsInit *BI) { return 0; }
252 virtual Init *convertValue( IntInit *II) { return 0; }
253 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
254 virtual Init *convertValue( ListInit *LI) { return 0; }
255 virtual Init *convertValue( UnOpInit *BO);
256 virtual Init *convertValue( BinOpInit *BO);
257 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
259 virtual Init *convertValue( CodeInit *CI) { return 0; }
260 virtual Init *convertValue(VarBitInit *VB) { return 0; }
261 virtual Init *convertValue( DefInit *DI) { return 0; }
262 virtual Init *convertValue( DagInit *DI) { return 0; }
263 virtual Init *convertValue( TypedInit *TI);
264 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
265 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
267 std::string getAsString() const { return "string"; }
269 bool typeIsConvertibleTo(const RecTy *RHS) const {
270 return RHS->baseClassOf(this);
273 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
274 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
275 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
276 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
277 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
278 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
279 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
280 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
283 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
284 // the specified type.
285 /// ListRecTy - 'list&lt;Ty&gt;' - Represent a list of values, all of which must
286 /// be of the specified type.
288 class ListRecTy : public RecTy {
289 RecTy *Ty;
290 public:
291 explicit ListRecTy(RecTy *T) : Ty(T) {}
293 RecTy *getElementType() const { return Ty; }
295 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
296 virtual Init *convertValue( BitInit *BI) { return 0; }
297 virtual Init *convertValue( BitsInit *BI) { return 0; }
298 virtual Init *convertValue( IntInit *II) { return 0; }
299 virtual Init *convertValue(StringInit *SI) { return 0; }
300 virtual Init *convertValue( ListInit *LI);
301 virtual Init *convertValue( CodeInit *CI) { return 0; }
302 virtual Init *convertValue(VarBitInit *VB) { return 0; }
303 virtual Init *convertValue( DefInit *DI) { return 0; }
304 virtual Init *convertValue( DagInit *DI) { return 0; }
305 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
306 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
307 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
308 virtual Init *convertValue( TypedInit *TI);
309 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
310 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
312 std::string getAsString() const;
314 bool typeIsConvertibleTo(const RecTy *RHS) const {
315 return RHS->baseClassOf(this);
318 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
319 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
320 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
321 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
322 virtual bool baseClassOf(const ListRecTy *RHS) const {
323 return RHS->getElementType()->typeIsConvertibleTo(Ty);
325 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
326 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
327 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
330 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
332 class CodeRecTy : public RecTy {
333 public:
334 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
335 virtual Init *convertValue( BitInit *BI) { return 0; }
336 virtual Init *convertValue( BitsInit *BI) { return 0; }
337 virtual Init *convertValue( IntInit *II) { return 0; }
338 virtual Init *convertValue(StringInit *SI) { return 0; }
339 virtual Init *convertValue( ListInit *LI) { return 0; }
340 virtual Init *convertValue( CodeInit *CI) { return (Init*)CI; }
341 virtual Init *convertValue(VarBitInit *VB) { return 0; }
342 virtual Init *convertValue( DefInit *DI) { return 0; }
343 virtual Init *convertValue( DagInit *DI) { return 0; }
344 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
345 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
346 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
347 virtual Init *convertValue( TypedInit *TI);
348 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
349 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
351 std::string getAsString() const { return "code"; }
353 bool typeIsConvertibleTo(const RecTy *RHS) const {
354 return RHS->baseClassOf(this);
356 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
357 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
358 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
359 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
360 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
361 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
362 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
363 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
366 /// DagRecTy - 'dag' - Represent a dag fragment
368 class DagRecTy : public RecTy {
369 public:
370 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
371 virtual Init *convertValue( BitInit *BI) { return 0; }
372 virtual Init *convertValue( BitsInit *BI) { return 0; }
373 virtual Init *convertValue( IntInit *II) { return 0; }
374 virtual Init *convertValue(StringInit *SI) { return 0; }
375 virtual Init *convertValue( ListInit *LI) { return 0; }
376 virtual Init *convertValue( CodeInit *CI) { return 0; }
377 virtual Init *convertValue(VarBitInit *VB) { return 0; }
378 virtual Init *convertValue( DefInit *DI) { return 0; }
379 virtual Init *convertValue( UnOpInit *BO);
380 virtual Init *convertValue( BinOpInit *BO);
381 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
382 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
383 virtual Init *convertValue( TypedInit *TI);
384 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
385 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
387 std::string getAsString() const { return "dag"; }
389 bool typeIsConvertibleTo(const RecTy *RHS) const {
390 return RHS->baseClassOf(this);
393 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
394 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
395 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
396 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
397 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
398 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
399 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
400 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
404 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
405 /// (R32 X = EAX).
407 class RecordRecTy : public RecTy {
408 Record *Rec;
409 public:
410 explicit RecordRecTy(Record *R) : Rec(R) {}
412 Record *getRecord() const { return Rec; }
414 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
415 virtual Init *convertValue( BitInit *BI) { return 0; }
416 virtual Init *convertValue( BitsInit *BI) { return 0; }
417 virtual Init *convertValue( IntInit *II) { return 0; }
418 virtual Init *convertValue(StringInit *SI) { return 0; }
419 virtual Init *convertValue( ListInit *LI) { return 0; }
420 virtual Init *convertValue( CodeInit *CI) { return 0; }
421 virtual Init *convertValue(VarBitInit *VB) { return 0; }
422 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
423 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
424 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
425 virtual Init *convertValue( DefInit *DI);
426 virtual Init *convertValue( DagInit *DI) { return 0; }
427 virtual Init *convertValue( TypedInit *VI);
428 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
429 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
431 std::string getAsString() const;
433 bool typeIsConvertibleTo(const RecTy *RHS) const {
434 return RHS->baseClassOf(this);
436 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
437 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
438 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
439 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
440 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
441 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
442 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
443 virtual bool baseClassOf(const RecordRecTy *RHS) const;
446 /// resolveTypes - Find a common type that T1 and T2 convert to.
447 /// Return 0 if no such type exists.
449 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
451 //===----------------------------------------------------------------------===//
452 // Initializer Classes
453 //===----------------------------------------------------------------------===//
455 struct Init {
456 virtual ~Init() {}
458 /// isComplete - This virtual method should be overridden by values that may
459 /// not be completely specified yet.
460 virtual bool isComplete() const { return true; }
462 /// print - Print out this value.
463 void print(raw_ostream &OS) const { OS << getAsString(); }
465 /// getAsString - Convert this value to a string form.
466 virtual std::string getAsString() const = 0;
468 /// dump - Debugging method that may be called through a debugger, just
469 /// invokes print on stderr.
470 void dump() const;
472 /// convertInitializerTo - This virtual function is a simple call-back
473 /// function that should be overridden to call the appropriate
474 /// RecTy::convertValue method.
476 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
478 /// convertInitializerBitRange - This method is used to implement the bitrange
479 /// selection operator. Given an initializer, it selects the specified bits
480 /// out, returning them as a new init of bits type. If it is not legal to use
481 /// the bit subscript operator on this initializer, return null.
483 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
484 return 0;
487 /// convertInitListSlice - This method is used to implement the list slice
488 /// selection operator. Given an initializer, it selects the specified list
489 /// elements, returning them as a new init of list type. If it is not legal
490 /// to take a slice of this, return null.
492 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
493 return 0;
496 /// getFieldType - This method is used to implement the FieldInit class.
497 /// Implementors of this method should return the type of the named field if
498 /// they are of record type.
500 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
502 /// getFieldInit - This method complements getFieldType to return the
503 /// initializer for the specified field. If getFieldType returns non-null
504 /// this method should return non-null, otherwise it returns null.
506 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
507 return 0;
510 /// resolveReferences - This method is used by classes that refer to other
511 /// variables which may not be defined at the time they expression is formed.
512 /// If a value is set for the variable later, this method will be called on
513 /// users of the value to allow the value to propagate out.
515 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
516 return this;
520 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
521 I.print(OS); return OS;
524 /// TypedInit - This is the common super-class of types that have a specific,
525 /// explicit, type.
527 class TypedInit : public Init {
528 RecTy *Ty;
529 public:
530 explicit TypedInit(RecTy *T) : Ty(T) {}
532 RecTy *getType() const { return Ty; }
534 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
535 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
537 /// resolveBitReference - This method is used to implement
538 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
539 /// simply return the resolved value, otherwise we return null.
541 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
542 unsigned Bit) = 0;
544 /// resolveListElementReference - This method is used to implement
545 /// VarListElementInit::resolveReferences. If the list element is resolvable
546 /// now, we return the resolved value, otherwise we return null.
547 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
548 unsigned Elt) = 0;
552 /// UnsetInit - ? - Represents an uninitialized value
554 class UnsetInit : public Init {
555 public:
556 virtual Init *convertInitializerTo(RecTy *Ty) {
557 return Ty->convertValue(this);
560 virtual bool isComplete() const { return false; }
561 virtual std::string getAsString() const { return "?"; }
565 /// BitInit - true/false - Represent a concrete initializer for a bit.
567 class BitInit : public Init {
568 bool Value;
569 public:
570 explicit BitInit(bool V) : Value(V) {}
572 bool getValue() const { return Value; }
574 virtual Init *convertInitializerTo(RecTy *Ty) {
575 return Ty->convertValue(this);
578 virtual std::string getAsString() const { return Value ? "1" : "0"; }
581 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
582 /// It contains a vector of bits, whose size is determined by the type.
584 class BitsInit : public Init {
585 std::vector<Init*> Bits;
586 public:
587 explicit BitsInit(unsigned Size) : Bits(Size) {}
589 unsigned getNumBits() const { return Bits.size(); }
591 Init *getBit(unsigned Bit) const {
592 assert(Bit < Bits.size() && "Bit index out of range!");
593 return Bits[Bit];
595 void setBit(unsigned Bit, Init *V) {
596 assert(Bit < Bits.size() && "Bit index out of range!");
597 assert(Bits[Bit] == 0 && "Bit already set!");
598 Bits[Bit] = V;
601 virtual Init *convertInitializerTo(RecTy *Ty) {
602 return Ty->convertValue(this);
604 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
606 virtual bool isComplete() const {
607 for (unsigned i = 0; i != getNumBits(); ++i)
608 if (!getBit(i)->isComplete()) return false;
609 return true;
611 virtual std::string getAsString() const;
613 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
615 // printXX - Print this bitstream with the specified format, returning true if
616 // it is not possible.
617 bool printInHex(raw_ostream &OS) const;
618 bool printAsVariable(raw_ostream &OS) const;
619 bool printAsUnset(raw_ostream &OS) const;
623 /// IntInit - 7 - Represent an initalization by a literal integer value.
625 class IntInit : public TypedInit {
626 int64_t Value;
627 public:
628 explicit IntInit(int64_t V) : TypedInit(new IntRecTy), Value(V) {}
630 int64_t getValue() const { return Value; }
632 virtual Init *convertInitializerTo(RecTy *Ty) {
633 return Ty->convertValue(this);
635 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
637 virtual std::string getAsString() const;
639 /// resolveBitReference - This method is used to implement
640 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
641 /// simply return the resolved value, otherwise we return null.
643 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
644 unsigned Bit) {
645 assert(0 && "Illegal bit reference off int");
646 return 0;
649 /// resolveListElementReference - This method is used to implement
650 /// VarListElementInit::resolveReferences. If the list element is resolvable
651 /// now, we return the resolved value, otherwise we return null.
652 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
653 unsigned Elt) {
654 assert(0 && "Illegal element reference off int");
655 return 0;
660 /// StringInit - "foo" - Represent an initialization by a string value.
662 class StringInit : public TypedInit {
663 std::string Value;
664 public:
665 explicit StringInit(const std::string &V)
666 : TypedInit(new StringRecTy), Value(V) {}
668 const std::string &getValue() const { return Value; }
670 virtual Init *convertInitializerTo(RecTy *Ty) {
671 return Ty->convertValue(this);
674 virtual std::string getAsString() const { return "\"" + Value + "\""; }
676 /// resolveBitReference - This method is used to implement
677 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
678 /// simply return the resolved value, otherwise we return null.
680 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
681 unsigned Bit) {
682 assert(0 && "Illegal bit reference off string");
683 return 0;
686 /// resolveListElementReference - This method is used to implement
687 /// VarListElementInit::resolveReferences. If the list element is resolvable
688 /// now, we return the resolved value, otherwise we return null.
689 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
690 unsigned Elt) {
691 assert(0 && "Illegal element reference off string");
692 return 0;
696 /// CodeInit - "[{...}]" - Represent a code fragment.
698 class CodeInit : public Init {
699 std::string Value;
700 public:
701 explicit CodeInit(const std::string &V) : Value(V) {}
703 const std::string getValue() const { return Value; }
705 virtual Init *convertInitializerTo(RecTy *Ty) {
706 return Ty->convertValue(this);
709 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
712 /// ListInit - [AL, AH, CL] - Represent a list of defs
714 class ListInit : public TypedInit {
715 std::vector<Init*> Values;
716 public:
717 typedef std::vector<Init*>::iterator iterator;
718 typedef std::vector<Init*>::const_iterator const_iterator;
720 explicit ListInit(std::vector<Init*> &Vs, RecTy *EltTy)
721 : TypedInit(new ListRecTy(EltTy)) {
722 Values.swap(Vs);
724 explicit ListInit(iterator Start, iterator End, RecTy *EltTy)
725 : TypedInit(new ListRecTy(EltTy)), Values(Start, End) {}
727 unsigned getSize() const { return Values.size(); }
728 Init *getElement(unsigned i) const {
729 assert(i < Values.size() && "List element index out of range!");
730 return Values[i];
733 Record *getElementAsRecord(unsigned i) const;
735 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
737 virtual Init *convertInitializerTo(RecTy *Ty) {
738 return Ty->convertValue(this);
741 /// resolveReferences - This method is used by classes that refer to other
742 /// variables which may not be defined at the time they expression is formed.
743 /// If a value is set for the variable later, this method will be called on
744 /// users of the value to allow the value to propagate out.
746 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
748 virtual std::string getAsString() const;
750 inline iterator begin() { return Values.begin(); }
751 inline const_iterator begin() const { return Values.begin(); }
752 inline iterator end () { return Values.end(); }
753 inline const_iterator end () const { return Values.end(); }
755 inline size_t size () const { return Values.size(); }
756 inline bool empty() const { return Values.empty(); }
758 /// resolveBitReference - This method is used to implement
759 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
760 /// simply return the resolved value, otherwise we return null.
762 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
763 unsigned Bit) {
764 assert(0 && "Illegal bit reference off list");
765 return 0;
768 /// resolveListElementReference - This method is used to implement
769 /// VarListElementInit::resolveReferences. If the list element is resolvable
770 /// now, we return the resolved value, otherwise we return null.
771 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
772 unsigned Elt);
776 /// OpInit - Base class for operators
778 class OpInit : public TypedInit {
779 public:
780 OpInit(RecTy *Type) : TypedInit(Type) {}
782 // Clone - Clone this operator, replacing arguments with the new list
783 virtual OpInit *clone(std::vector<Init *> &Operands) = 0;
785 virtual int getNumOperands() const = 0;
786 virtual Init *getOperand(int i) = 0;
788 // Fold - If possible, fold this to a simpler init. Return this if not
789 // possible to fold.
790 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) = 0;
792 virtual Init *convertInitializerTo(RecTy *Ty) {
793 return Ty->convertValue(this);
796 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
797 unsigned Bit);
798 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
799 unsigned Elt);
803 /// UnOpInit - !op (X) - Transform an init.
805 class UnOpInit : public OpInit {
806 public:
807 enum UnaryOp { CAST, CAR, CDR, LNULL };
808 private:
809 UnaryOp Opc;
810 Init *LHS;
811 public:
812 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type) :
813 OpInit(Type), Opc(opc), LHS(lhs) {
816 // Clone - Clone this operator, replacing arguments with the new list
817 virtual OpInit *clone(std::vector<Init *> &Operands) {
818 assert(Operands.size() == 1 &&
819 "Wrong number of operands for unary operation");
820 return new UnOpInit(getOpcode(), *Operands.begin(), getType());
823 int getNumOperands() const { return 1; }
824 Init *getOperand(int i) {
825 assert(i == 0 && "Invalid operand id for unary operator");
826 return getOperand();
829 UnaryOp getOpcode() const { return Opc; }
830 Init *getOperand() const { return LHS; }
832 // Fold - If possible, fold this to a simpler init. Return this if not
833 // possible to fold.
834 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
836 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
838 /// getFieldType - This method is used to implement the FieldInit class.
839 /// Implementors of this method should return the type of the named field if
840 /// they are of record type.
842 virtual RecTy *getFieldType(const std::string &FieldName) const;
844 virtual std::string getAsString() const;
847 /// BinOpInit - !op (X, Y) - Combine two inits.
849 class BinOpInit : public OpInit {
850 public:
851 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT };
852 private:
853 BinaryOp Opc;
854 Init *LHS, *RHS;
855 public:
856 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
857 OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {
860 // Clone - Clone this operator, replacing arguments with the new list
861 virtual OpInit *clone(std::vector<Init *> &Operands) {
862 assert(Operands.size() == 2 &&
863 "Wrong number of operands for binary operation");
864 return new BinOpInit(getOpcode(), Operands[0], Operands[1], getType());
867 int getNumOperands() const { return 2; }
868 Init *getOperand(int i) {
869 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
870 if (i == 0) {
871 return getLHS();
873 else {
874 return getRHS();
878 BinaryOp getOpcode() const { return Opc; }
879 Init *getLHS() const { return LHS; }
880 Init *getRHS() const { return RHS; }
882 // Fold - If possible, fold this to a simpler init. Return this if not
883 // possible to fold.
884 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
886 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
888 virtual std::string getAsString() const;
891 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
893 class TernOpInit : public OpInit {
894 public:
895 enum TernaryOp { SUBST, FOREACH, IF };
896 private:
897 TernaryOp Opc;
898 Init *LHS, *MHS, *RHS;
899 public:
900 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type) :
901 OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {
904 // Clone - Clone this operator, replacing arguments with the new list
905 virtual OpInit *clone(std::vector<Init *> &Operands) {
906 assert(Operands.size() == 3 &&
907 "Wrong number of operands for ternary operation");
908 return new TernOpInit(getOpcode(), Operands[0], Operands[1], Operands[2],
909 getType());
912 int getNumOperands() const { return 3; }
913 Init *getOperand(int i) {
914 assert((i == 0 || i == 1 || i == 2) &&
915 "Invalid operand id for ternary operator");
916 if (i == 0) {
917 return getLHS();
919 else if (i == 1) {
920 return getMHS();
922 else {
923 return getRHS();
927 TernaryOp getOpcode() const { return Opc; }
928 Init *getLHS() const { return LHS; }
929 Init *getMHS() const { return MHS; }
930 Init *getRHS() const { return RHS; }
932 // Fold - If possible, fold this to a simpler init. Return this if not
933 // possible to fold.
934 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
936 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
938 virtual std::string getAsString() const;
942 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
944 class VarInit : public TypedInit {
945 std::string VarName;
946 public:
947 explicit VarInit(const std::string &VN, RecTy *T)
948 : TypedInit(T), VarName(VN) {}
950 virtual Init *convertInitializerTo(RecTy *Ty) {
951 return Ty->convertValue(this);
954 const std::string &getName() const { return VarName; }
956 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
957 unsigned Bit);
958 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
959 unsigned Elt);
961 virtual RecTy *getFieldType(const std::string &FieldName) const;
962 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
964 /// resolveReferences - This method is used by classes that refer to other
965 /// variables which may not be defined at the time they expression is formed.
966 /// If a value is set for the variable later, this method will be called on
967 /// users of the value to allow the value to propagate out.
969 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
971 virtual std::string getAsString() const { return VarName; }
975 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
977 class VarBitInit : public Init {
978 TypedInit *TI;
979 unsigned Bit;
980 public:
981 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
982 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
983 ((BitsRecTy*)T->getType())->getNumBits() > B &&
984 "Illegal VarBitInit expression!");
987 virtual Init *convertInitializerTo(RecTy *Ty) {
988 return Ty->convertValue(this);
991 TypedInit *getVariable() const { return TI; }
992 unsigned getBitNum() const { return Bit; }
994 virtual std::string getAsString() const;
995 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
998 /// VarListElementInit - List[4] - Represent access to one element of a var or
999 /// field.
1000 class VarListElementInit : public TypedInit {
1001 TypedInit *TI;
1002 unsigned Element;
1003 public:
1004 VarListElementInit(TypedInit *T, unsigned E)
1005 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
1006 TI(T), Element(E) {
1007 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
1008 "Illegal VarBitInit expression!");
1011 virtual Init *convertInitializerTo(RecTy *Ty) {
1012 return Ty->convertValue(this);
1015 TypedInit *getVariable() const { return TI; }
1016 unsigned getElementNum() const { return Element; }
1018 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1019 unsigned Bit);
1021 /// resolveListElementReference - This method is used to implement
1022 /// VarListElementInit::resolveReferences. If the list element is resolvable
1023 /// now, we return the resolved value, otherwise we return null.
1024 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1025 unsigned Elt);
1027 virtual std::string getAsString() const;
1028 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
1031 /// DefInit - AL - Represent a reference to a 'def' in the description
1033 class DefInit : public TypedInit {
1034 Record *Def;
1035 public:
1036 explicit DefInit(Record *D) : TypedInit(new RecordRecTy(D)), Def(D) {}
1038 virtual Init *convertInitializerTo(RecTy *Ty) {
1039 return Ty->convertValue(this);
1042 Record *getDef() const { return Def; }
1044 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1046 virtual RecTy *getFieldType(const std::string &FieldName) const;
1047 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
1049 virtual std::string getAsString() const;
1051 /// resolveBitReference - This method is used to implement
1052 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
1053 /// simply return the resolved value, otherwise we return null.
1055 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1056 unsigned Bit) {
1057 assert(0 && "Illegal bit reference off def");
1058 return 0;
1061 /// resolveListElementReference - This method is used to implement
1062 /// VarListElementInit::resolveReferences. If the list element is resolvable
1063 /// now, we return the resolved value, otherwise we return null.
1064 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1065 unsigned Elt) {
1066 assert(0 && "Illegal element reference off def");
1067 return 0;
1072 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1074 class FieldInit : public TypedInit {
1075 Init *Rec; // Record we are referring to
1076 std::string FieldName; // Field we are accessing
1077 public:
1078 FieldInit(Init *R, const std::string &FN)
1079 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
1080 assert(getType() && "FieldInit with non-record type!");
1083 virtual Init *convertInitializerTo(RecTy *Ty) {
1084 return Ty->convertValue(this);
1087 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1088 unsigned Bit);
1089 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1090 unsigned Elt);
1092 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
1094 virtual std::string getAsString() const {
1095 return Rec->getAsString() + "." + FieldName;
1099 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1100 /// to have at least one value then a (possibly empty) list of arguments. Each
1101 /// argument can have a name associated with it.
1103 class DagInit : public TypedInit {
1104 Init *Val;
1105 std::string ValName;
1106 std::vector<Init*> Args;
1107 std::vector<std::string> ArgNames;
1108 public:
1109 DagInit(Init *V, std::string VN,
1110 const std::vector<std::pair<Init*, std::string> > &args)
1111 : TypedInit(new DagRecTy), Val(V), ValName(VN) {
1112 Args.reserve(args.size());
1113 ArgNames.reserve(args.size());
1114 for (unsigned i = 0, e = args.size(); i != e; ++i) {
1115 Args.push_back(args[i].first);
1116 ArgNames.push_back(args[i].second);
1119 DagInit(Init *V, std::string VN, const std::vector<Init*> &args,
1120 const std::vector<std::string> &argNames)
1121 : TypedInit(new DagRecTy), Val(V), ValName(VN), Args(args), ArgNames(argNames) {
1124 virtual Init *convertInitializerTo(RecTy *Ty) {
1125 return Ty->convertValue(this);
1128 Init *getOperator() const { return Val; }
1130 const std::string &getName() const { return ValName; }
1132 unsigned getNumArgs() const { return Args.size(); }
1133 Init *getArg(unsigned Num) const {
1134 assert(Num < Args.size() && "Arg number out of range!");
1135 return Args[Num];
1137 const std::string &getArgName(unsigned Num) const {
1138 assert(Num < ArgNames.size() && "Arg number out of range!");
1139 return ArgNames[Num];
1142 void setArg(unsigned Num, Init *I) {
1143 assert(Num < Args.size() && "Arg number out of range!");
1144 Args[Num] = I;
1147 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
1149 virtual std::string getAsString() const;
1151 typedef std::vector<Init*>::iterator arg_iterator;
1152 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1153 typedef std::vector<std::string>::iterator name_iterator;
1154 typedef std::vector<std::string>::const_iterator const_name_iterator;
1156 inline arg_iterator arg_begin() { return Args.begin(); }
1157 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1158 inline arg_iterator arg_end () { return Args.end(); }
1159 inline const_arg_iterator arg_end () const { return Args.end(); }
1161 inline size_t arg_size () const { return Args.size(); }
1162 inline bool arg_empty() const { return Args.empty(); }
1164 inline name_iterator name_begin() { return ArgNames.begin(); }
1165 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1166 inline name_iterator name_end () { return ArgNames.end(); }
1167 inline const_name_iterator name_end () const { return ArgNames.end(); }
1169 inline size_t name_size () const { return ArgNames.size(); }
1170 inline bool name_empty() const { return ArgNames.empty(); }
1172 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1173 unsigned Bit) {
1174 assert(0 && "Illegal bit reference off dag");
1175 return 0;
1178 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1179 unsigned Elt) {
1180 assert(0 && "Illegal element reference off dag");
1181 return 0;
1186 //===----------------------------------------------------------------------===//
1187 // High-Level Classes
1188 //===----------------------------------------------------------------------===//
1190 class RecordVal {
1191 std::string Name;
1192 RecTy *Ty;
1193 unsigned Prefix;
1194 Init *Value;
1195 public:
1196 RecordVal(const std::string &N, RecTy *T, unsigned P);
1198 const std::string &getName() const { return Name; }
1200 unsigned getPrefix() const { return Prefix; }
1201 RecTy *getType() const { return Ty; }
1202 Init *getValue() const { return Value; }
1204 bool setValue(Init *V) {
1205 if (V) {
1206 Value = V->convertInitializerTo(Ty);
1207 return Value == 0;
1209 Value = 0;
1210 return false;
1213 void dump() const;
1214 void print(raw_ostream &OS, bool PrintSem = true) const;
1217 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1218 RV.print(OS << " ");
1219 return OS;
1222 class Record {
1223 static unsigned LastID;
1225 // Unique record ID.
1226 unsigned ID;
1227 std::string Name;
1228 SMLoc Loc;
1229 std::vector<std::string> TemplateArgs;
1230 std::vector<RecordVal> Values;
1231 std::vector<Record*> SuperClasses;
1232 public:
1234 explicit Record(const std::string &N, SMLoc loc) :
1235 ID(LastID++), Name(N), Loc(loc) {}
1236 ~Record() {}
1238 unsigned getID() const { return ID; }
1240 const std::string &getName() const { return Name; }
1241 void setName(const std::string &Name); // Also updates RecordKeeper.
1243 SMLoc getLoc() const { return Loc; }
1245 const std::vector<std::string> &getTemplateArgs() const {
1246 return TemplateArgs;
1248 const std::vector<RecordVal> &getValues() const { return Values; }
1249 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1251 bool isTemplateArg(const std::string &Name) const {
1252 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1253 if (TemplateArgs[i] == Name) return true;
1254 return false;
1257 const RecordVal *getValue(const std::string &Name) const {
1258 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1259 if (Values[i].getName() == Name) return &Values[i];
1260 return 0;
1262 RecordVal *getValue(const std::string &Name) {
1263 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1264 if (Values[i].getName() == Name) return &Values[i];
1265 return 0;
1268 void addTemplateArg(const std::string &Name) {
1269 assert(!isTemplateArg(Name) && "Template arg already defined!");
1270 TemplateArgs.push_back(Name);
1273 void addValue(const RecordVal &RV) {
1274 assert(getValue(RV.getName()) == 0 && "Value already added!");
1275 Values.push_back(RV);
1278 void removeValue(const std::string &Name) {
1279 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
1280 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1281 if (Values[i].getName() == Name) {
1282 Values.erase(Values.begin()+i);
1283 return;
1285 assert(0 && "Name does not exist in record!");
1288 bool isSubClassOf(const Record *R) const {
1289 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1290 if (SuperClasses[i] == R)
1291 return true;
1292 return false;
1295 bool isSubClassOf(const std::string &Name) const {
1296 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1297 if (SuperClasses[i]->getName() == Name)
1298 return true;
1299 return false;
1302 void addSuperClass(Record *R) {
1303 assert(!isSubClassOf(R) && "Already subclassing record!");
1304 SuperClasses.push_back(R);
1307 /// resolveReferences - If there are any field references that refer to fields
1308 /// that have been filled in, we can propagate the values now.
1310 void resolveReferences() { resolveReferencesTo(0); }
1312 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1313 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1314 /// possible references.
1315 void resolveReferencesTo(const RecordVal *RV);
1317 void dump() const;
1319 //===--------------------------------------------------------------------===//
1320 // High-level methods useful to tablegen back-ends
1323 /// getValueInit - Return the initializer for a value with the specified name,
1324 /// or throw an exception if the field does not exist.
1326 Init *getValueInit(const std::string &FieldName) const;
1328 /// getValueAsString - This method looks up the specified field and returns
1329 /// its value as a string, throwing an exception if the field does not exist
1330 /// or if the value is not a string.
1332 std::string getValueAsString(const std::string &FieldName) const;
1334 /// getValueAsBitsInit - This method looks up the specified field and returns
1335 /// its value as a BitsInit, throwing an exception if the field does not exist
1336 /// or if the value is not the right type.
1338 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
1340 /// getValueAsListInit - This method looks up the specified field and returns
1341 /// its value as a ListInit, throwing an exception if the field does not exist
1342 /// or if the value is not the right type.
1344 ListInit *getValueAsListInit(const std::string &FieldName) const;
1346 /// getValueAsListOfDefs - This method looks up the specified field and
1347 /// returns its value as a vector of records, throwing an exception if the
1348 /// field does not exist or if the value is not the right type.
1350 std::vector<Record*> getValueAsListOfDefs(const std::string &FieldName) const;
1352 /// getValueAsListOfInts - This method looks up the specified field and returns
1353 /// its value as a vector of integers, throwing an exception if the field does
1354 /// not exist or if the value is not the right type.
1356 std::vector<int64_t> getValueAsListOfInts(const std::string &FieldName) const;
1358 /// getValueAsDef - This method looks up the specified field and returns its
1359 /// value as a Record, throwing an exception if the field does not exist or if
1360 /// the value is not the right type.
1362 Record *getValueAsDef(const std::string &FieldName) const;
1364 /// getValueAsBit - This method looks up the specified field and returns its
1365 /// value as a bit, throwing an exception if the field does not exist or if
1366 /// the value is not the right type.
1368 bool getValueAsBit(const std::string &FieldName) const;
1370 /// getValueAsInt - This method looks up the specified field and returns its
1371 /// value as an int64_t, throwing an exception if the field does not exist or
1372 /// if the value is not the right type.
1374 int64_t getValueAsInt(const std::string &FieldName) const;
1376 /// getValueAsDag - This method looks up the specified field and returns its
1377 /// value as an Dag, throwing an exception if the field does not exist or if
1378 /// the value is not the right type.
1380 DagInit *getValueAsDag(const std::string &FieldName) const;
1382 /// getValueAsCode - This method looks up the specified field and returns
1383 /// its value as the string data in a CodeInit, throwing an exception if the
1384 /// field does not exist or if the value is not a code object.
1386 std::string getValueAsCode(const std::string &FieldName) const;
1389 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1391 struct MultiClass {
1392 Record Rec; // Placeholder for template args and Name.
1393 typedef std::vector<Record*> RecordVector;
1394 RecordVector DefPrototypes;
1396 void dump() const;
1398 MultiClass(const std::string &Name, SMLoc Loc) : Rec(Name, Loc) {}
1401 class RecordKeeper {
1402 std::map<std::string, Record*> Classes, Defs;
1403 public:
1404 ~RecordKeeper() {
1405 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1406 E = Classes.end(); I != E; ++I)
1407 delete I->second;
1408 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1409 E = Defs.end(); I != E; ++I)
1410 delete I->second;
1413 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1414 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1416 Record *getClass(const std::string &Name) const {
1417 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1418 return I == Classes.end() ? 0 : I->second;
1420 Record *getDef(const std::string &Name) const {
1421 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1422 return I == Defs.end() ? 0 : I->second;
1424 void addClass(Record *R) {
1425 assert(getClass(R->getName()) == 0 && "Class already exists!");
1426 Classes.insert(std::make_pair(R->getName(), R));
1428 void addDef(Record *R) {
1429 assert(getDef(R->getName()) == 0 && "Def already exists!");
1430 Defs.insert(std::make_pair(R->getName(), R));
1433 /// removeClass - Remove, but do not delete, the specified record.
1435 void removeClass(const std::string &Name) {
1436 assert(Classes.count(Name) && "Class does not exist!");
1437 Classes.erase(Name);
1439 /// removeDef - Remove, but do not delete, the specified record.
1441 void removeDef(const std::string &Name) {
1442 assert(Defs.count(Name) && "Def does not exist!");
1443 Defs.erase(Name);
1446 //===--------------------------------------------------------------------===//
1447 // High-level helper methods, useful for tablegen backends...
1449 /// getAllDerivedDefinitions - This method returns all concrete definitions
1450 /// that derive from the specified class name. If a class with the specified
1451 /// name does not exist, an exception is thrown.
1452 std::vector<Record*>
1453 getAllDerivedDefinitions(const std::string &ClassName) const;
1456 void dump() const;
1459 /// LessRecord - Sorting predicate to sort record pointers by name.
1461 struct LessRecord {
1462 bool operator()(const Record *Rec1, const Record *Rec2) const {
1463 return Rec1->getName() < Rec2->getName();
1467 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1468 /// name field.
1470 struct LessRecordFieldName {
1471 bool operator()(const Record *Rec1, const Record *Rec2) const {
1472 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1477 class TGError {
1478 SMLoc Loc;
1479 std::string Message;
1480 public:
1481 TGError(SMLoc loc, const std::string &message) : Loc(loc), Message(message) {}
1483 SMLoc getLoc() const { return Loc; }
1484 const std::string &getMessage() const { return Message; }
1488 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1490 extern RecordKeeper Records;
1492 void PrintError(SMLoc ErrorLoc, const std::string &Msg);
1495 } // End llvm namespace
1497 #endif