Proper name 16 bit libcalls
[llvm/msp430.git] / utils / TableGen / Record.h
blobf76de7d4ef34ef9adf657191c46d3ef89e8474f6
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 "TGSourceMgr.h"
19 #include "llvm/Support/DataTypes.h"
20 #include <map>
21 #include <ostream>
23 namespace llvm {
25 // RecTy subclasses.
26 class BitRecTy;
27 class BitsRecTy;
28 class IntRecTy;
29 class StringRecTy;
30 class ListRecTy;
31 class CodeRecTy;
32 class DagRecTy;
33 class RecordRecTy;
35 // Init subclasses.
36 struct Init;
37 class UnsetInit;
38 class BitInit;
39 class BitsInit;
40 class IntInit;
41 class StringInit;
42 class CodeInit;
43 class ListInit;
44 class BinOpInit;
45 class DefInit;
46 class DagInit;
47 class TypedInit;
48 class VarInit;
49 class FieldInit;
50 class VarBitInit;
51 class VarListElementInit;
53 // Other classes.
54 class Record;
55 class RecordVal;
56 struct MultiClass;
58 //===----------------------------------------------------------------------===//
59 // Type Classes
60 //===----------------------------------------------------------------------===//
62 struct RecTy {
63 virtual ~RecTy() {}
65 virtual std::string getAsString() const = 0;
66 void print(std::ostream &OS) const { OS << getAsString(); }
67 void dump() const;
69 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
70 /// converted to the specified type.
71 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
73 public: // These methods should only be called from subclasses of Init
74 virtual Init *convertValue( UnsetInit *UI) { return 0; }
75 virtual Init *convertValue( BitInit *BI) { return 0; }
76 virtual Init *convertValue( BitsInit *BI) { return 0; }
77 virtual Init *convertValue( IntInit *II) { return 0; }
78 virtual Init *convertValue(StringInit *SI) { return 0; }
79 virtual Init *convertValue( ListInit *LI) { return 0; }
80 virtual Init *convertValue( BinOpInit *UI) {
81 return convertValue((TypedInit*)UI);
83 virtual Init *convertValue( CodeInit *CI) { return 0; }
84 virtual Init *convertValue(VarBitInit *VB) { return 0; }
85 virtual Init *convertValue( DefInit *DI) { return 0; }
86 virtual Init *convertValue( DagInit *DI) { return 0; }
87 virtual Init *convertValue( TypedInit *TI) { return 0; }
88 virtual Init *convertValue( VarInit *VI) {
89 return convertValue((TypedInit*)VI);
91 virtual Init *convertValue( FieldInit *FI) {
92 return convertValue((TypedInit*)FI);
95 public: // These methods should only be called by subclasses of RecTy.
96 // baseClassOf - These virtual methods should be overloaded to return true iff
97 // all values of type 'RHS' can be converted to the 'this' type.
98 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
99 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
100 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
101 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
102 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
103 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
104 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
105 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
108 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
109 Ty.print(OS);
110 return OS;
114 /// BitRecTy - 'bit' - Represent a single bit
116 class BitRecTy : public RecTy {
117 public:
118 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
119 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
120 virtual Init *convertValue( BitsInit *BI);
121 virtual Init *convertValue( IntInit *II);
122 virtual Init *convertValue(StringInit *SI) { return 0; }
123 virtual Init *convertValue( ListInit *LI) { return 0; }
124 virtual Init *convertValue( CodeInit *CI) { return 0; }
125 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
126 virtual Init *convertValue( DefInit *DI) { return 0; }
127 virtual Init *convertValue( DagInit *DI) { return 0; }
128 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
129 virtual Init *convertValue( TypedInit *TI);
130 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
131 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
133 std::string getAsString() const { return "bit"; }
135 bool typeIsConvertibleTo(const RecTy *RHS) const {
136 return RHS->baseClassOf(this);
138 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
139 virtual bool baseClassOf(const BitsRecTy *RHS) const;
140 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
141 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
142 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
143 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
144 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
145 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
150 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
151 /// BitsRecTy - 'bits&lt;n&gt;' - Represent a fixed number of bits
153 class BitsRecTy : public RecTy {
154 unsigned Size;
155 public:
156 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
158 unsigned getNumBits() const { return Size; }
160 virtual Init *convertValue( UnsetInit *UI);
161 virtual Init *convertValue( BitInit *UI);
162 virtual Init *convertValue( BitsInit *BI);
163 virtual Init *convertValue( IntInit *II);
164 virtual Init *convertValue(StringInit *SI) { return 0; }
165 virtual Init *convertValue( ListInit *LI) { return 0; }
166 virtual Init *convertValue( CodeInit *CI) { return 0; }
167 virtual Init *convertValue(VarBitInit *VB) { return 0; }
168 virtual Init *convertValue( DefInit *DI) { return 0; }
169 virtual Init *convertValue( DagInit *DI) { return 0; }
170 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
171 virtual Init *convertValue( TypedInit *TI);
172 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
173 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
175 std::string getAsString() const;
177 bool typeIsConvertibleTo(const RecTy *RHS) const {
178 return RHS->baseClassOf(this);
180 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
181 virtual bool baseClassOf(const BitsRecTy *RHS) const {
182 return RHS->Size == Size;
184 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
185 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
186 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
187 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
188 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
189 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
194 /// IntRecTy - 'int' - Represent an integer value of no particular size
196 class IntRecTy : public RecTy {
197 public:
198 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
199 virtual Init *convertValue( BitInit *BI);
200 virtual Init *convertValue( BitsInit *BI);
201 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
202 virtual Init *convertValue(StringInit *SI) { return 0; }
203 virtual Init *convertValue( ListInit *LI) { return 0; }
204 virtual Init *convertValue( CodeInit *CI) { return 0; }
205 virtual Init *convertValue(VarBitInit *VB) { return 0; }
206 virtual Init *convertValue( DefInit *DI) { return 0; }
207 virtual Init *convertValue( DagInit *DI) { return 0; }
208 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
209 virtual Init *convertValue( TypedInit *TI);
210 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
211 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
213 std::string getAsString() const { return "int"; }
215 bool typeIsConvertibleTo(const RecTy *RHS) const {
216 return RHS->baseClassOf(this);
219 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
220 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
221 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
222 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
223 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
224 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
225 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
226 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
230 /// StringRecTy - 'string' - Represent an string value
232 class StringRecTy : public RecTy {
233 public:
234 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
235 virtual Init *convertValue( BitInit *BI) { return 0; }
236 virtual Init *convertValue( BitsInit *BI) { return 0; }
237 virtual Init *convertValue( IntInit *II) { return 0; }
238 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
239 virtual Init *convertValue( ListInit *LI) { return 0; }
240 virtual Init *convertValue( BinOpInit *BO);
241 virtual Init *convertValue( CodeInit *CI) { return 0; }
242 virtual Init *convertValue(VarBitInit *VB) { return 0; }
243 virtual Init *convertValue( DefInit *DI) { return 0; }
244 virtual Init *convertValue( DagInit *DI) { return 0; }
245 virtual Init *convertValue( TypedInit *TI);
246 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
247 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
249 std::string getAsString() const { return "string"; }
251 bool typeIsConvertibleTo(const RecTy *RHS) const {
252 return RHS->baseClassOf(this);
255 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
256 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
257 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
258 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
259 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
260 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
261 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
262 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
265 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
266 // the specified type.
267 /// ListRecTy - 'list&lt;Ty&gt;' - Represent a list of values, all of which must
268 /// be of the specified type.
270 class ListRecTy : public RecTy {
271 RecTy *Ty;
272 public:
273 explicit ListRecTy(RecTy *T) : Ty(T) {}
275 RecTy *getElementType() const { return Ty; }
277 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
278 virtual Init *convertValue( BitInit *BI) { return 0; }
279 virtual Init *convertValue( BitsInit *BI) { return 0; }
280 virtual Init *convertValue( IntInit *II) { return 0; }
281 virtual Init *convertValue(StringInit *SI) { return 0; }
282 virtual Init *convertValue( ListInit *LI);
283 virtual Init *convertValue( CodeInit *CI) { return 0; }
284 virtual Init *convertValue(VarBitInit *VB) { return 0; }
285 virtual Init *convertValue( DefInit *DI) { return 0; }
286 virtual Init *convertValue( DagInit *DI) { return 0; }
287 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
288 virtual Init *convertValue( TypedInit *TI);
289 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
290 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
292 std::string getAsString() const;
294 bool typeIsConvertibleTo(const RecTy *RHS) const {
295 return RHS->baseClassOf(this);
298 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
299 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
300 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
301 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
302 virtual bool baseClassOf(const ListRecTy *RHS) const {
303 return RHS->getElementType()->typeIsConvertibleTo(Ty);
305 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
306 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
307 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
310 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
312 class CodeRecTy : public RecTy {
313 public:
314 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
315 virtual Init *convertValue( BitInit *BI) { return 0; }
316 virtual Init *convertValue( BitsInit *BI) { return 0; }
317 virtual Init *convertValue( IntInit *II) { return 0; }
318 virtual Init *convertValue(StringInit *SI) { return 0; }
319 virtual Init *convertValue( ListInit *LI) { return 0; }
320 virtual Init *convertValue( CodeInit *CI) { return (Init*)CI; }
321 virtual Init *convertValue(VarBitInit *VB) { return 0; }
322 virtual Init *convertValue( DefInit *DI) { return 0; }
323 virtual Init *convertValue( DagInit *DI) { return 0; }
324 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
325 virtual Init *convertValue( TypedInit *TI);
326 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
327 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
329 std::string getAsString() const { return "code"; }
331 bool typeIsConvertibleTo(const RecTy *RHS) const {
332 return RHS->baseClassOf(this);
334 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
335 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
336 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
337 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
338 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
339 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
340 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
341 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
344 /// DagRecTy - 'dag' - Represent a dag fragment
346 class DagRecTy : public RecTy {
347 public:
348 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
349 virtual Init *convertValue( BitInit *BI) { return 0; }
350 virtual Init *convertValue( BitsInit *BI) { return 0; }
351 virtual Init *convertValue( IntInit *II) { return 0; }
352 virtual Init *convertValue(StringInit *SI) { return 0; }
353 virtual Init *convertValue( ListInit *LI) { return 0; }
354 virtual Init *convertValue( CodeInit *CI) { return 0; }
355 virtual Init *convertValue(VarBitInit *VB) { return 0; }
356 virtual Init *convertValue( DefInit *DI) { return 0; }
357 virtual Init *convertValue( BinOpInit *BO);
358 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
359 virtual Init *convertValue( TypedInit *TI);
360 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
361 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
363 std::string getAsString() const { return "dag"; }
365 bool typeIsConvertibleTo(const RecTy *RHS) const {
366 return RHS->baseClassOf(this);
369 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
370 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
371 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
372 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
373 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
374 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
375 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
376 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
380 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
381 /// (R32 X = EAX).
383 class RecordRecTy : public RecTy {
384 Record *Rec;
385 public:
386 explicit RecordRecTy(Record *R) : Rec(R) {}
388 Record *getRecord() const { return Rec; }
390 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
391 virtual Init *convertValue( BitInit *BI) { return 0; }
392 virtual Init *convertValue( BitsInit *BI) { return 0; }
393 virtual Init *convertValue( IntInit *II) { return 0; }
394 virtual Init *convertValue(StringInit *SI) { return 0; }
395 virtual Init *convertValue( ListInit *LI) { return 0; }
396 virtual Init *convertValue( CodeInit *CI) { return 0; }
397 virtual Init *convertValue(VarBitInit *VB) { return 0; }
398 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
399 virtual Init *convertValue( DefInit *DI);
400 virtual Init *convertValue( DagInit *DI) { return 0; }
401 virtual Init *convertValue( TypedInit *VI);
402 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
403 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
405 std::string getAsString() const;
407 bool typeIsConvertibleTo(const RecTy *RHS) const {
408 return RHS->baseClassOf(this);
410 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
411 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
412 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
413 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
414 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
415 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
416 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
417 virtual bool baseClassOf(const RecordRecTy *RHS) const;
422 //===----------------------------------------------------------------------===//
423 // Initializer Classes
424 //===----------------------------------------------------------------------===//
426 struct Init {
427 virtual ~Init() {}
429 /// isComplete - This virtual method should be overridden by values that may
430 /// not be completely specified yet.
431 virtual bool isComplete() const { return true; }
433 /// print - Print out this value.
434 void print(std::ostream &OS) const { OS << getAsString(); }
436 /// getAsString - Convert this value to a string form.
437 virtual std::string getAsString() const = 0;
439 /// dump - Debugging method that may be called through a debugger, just
440 /// invokes print on cerr.
441 void dump() const;
443 /// convertInitializerTo - This virtual function is a simple call-back
444 /// function that should be overridden to call the appropriate
445 /// RecTy::convertValue method.
447 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
449 /// convertInitializerBitRange - This method is used to implement the bitrange
450 /// selection operator. Given an initializer, it selects the specified bits
451 /// out, returning them as a new init of bits type. If it is not legal to use
452 /// the bit subscript operator on this initializer, return null.
454 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
455 return 0;
458 /// convertInitListSlice - This method is used to implement the list slice
459 /// selection operator. Given an initializer, it selects the specified list
460 /// elements, returning them as a new init of list type. If it is not legal
461 /// to take a slice of this, return null.
463 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
464 return 0;
467 /// getFieldType - This method is used to implement the FieldInit class.
468 /// Implementors of this method should return the type of the named field if
469 /// they are of record type.
471 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
473 /// getFieldInit - This method complements getFieldType to return the
474 /// initializer for the specified field. If getFieldType returns non-null
475 /// this method should return non-null, otherwise it returns null.
477 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
478 return 0;
481 /// resolveReferences - This method is used by classes that refer to other
482 /// variables which may not be defined at the time they expression is formed.
483 /// If a value is set for the variable later, this method will be called on
484 /// users of the value to allow the value to propagate out.
486 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
487 return this;
491 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
492 I.print(OS); return OS;
496 /// UnsetInit - ? - Represents an uninitialized value
498 class UnsetInit : public Init {
499 public:
500 virtual Init *convertInitializerTo(RecTy *Ty) {
501 return Ty->convertValue(this);
504 virtual bool isComplete() const { return false; }
505 virtual std::string getAsString() const { return "?"; }
509 /// BitInit - true/false - Represent a concrete initializer for a bit.
511 class BitInit : public Init {
512 bool Value;
513 public:
514 explicit BitInit(bool V) : Value(V) {}
516 bool getValue() const { return Value; }
518 virtual Init *convertInitializerTo(RecTy *Ty) {
519 return Ty->convertValue(this);
522 virtual std::string getAsString() const { return Value ? "1" : "0"; }
525 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
526 /// It contains a vector of bits, whose size is determined by the type.
528 class BitsInit : public Init {
529 std::vector<Init*> Bits;
530 public:
531 explicit BitsInit(unsigned Size) : Bits(Size) {}
533 unsigned getNumBits() const { return Bits.size(); }
535 Init *getBit(unsigned Bit) const {
536 assert(Bit < Bits.size() && "Bit index out of range!");
537 return Bits[Bit];
539 void setBit(unsigned Bit, Init *V) {
540 assert(Bit < Bits.size() && "Bit index out of range!");
541 assert(Bits[Bit] == 0 && "Bit already set!");
542 Bits[Bit] = V;
545 virtual Init *convertInitializerTo(RecTy *Ty) {
546 return Ty->convertValue(this);
548 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
550 virtual bool isComplete() const {
551 for (unsigned i = 0; i != getNumBits(); ++i)
552 if (!getBit(i)->isComplete()) return false;
553 return true;
555 virtual std::string getAsString() const;
557 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
559 // printXX - Print this bitstream with the specified format, returning true if
560 // it is not possible.
561 bool printInHex(std::ostream &OS) const;
562 bool printAsVariable(std::ostream &OS) const;
563 bool printAsUnset(std::ostream &OS) const;
567 /// IntInit - 7 - Represent an initalization by a literal integer value.
569 class IntInit : public Init {
570 int64_t Value;
571 public:
572 explicit IntInit(int64_t V) : Value(V) {}
574 int64_t getValue() const { return Value; }
576 virtual Init *convertInitializerTo(RecTy *Ty) {
577 return Ty->convertValue(this);
579 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
581 virtual std::string getAsString() const;
585 /// StringInit - "foo" - Represent an initialization by a string value.
587 class StringInit : public Init {
588 std::string Value;
589 public:
590 explicit StringInit(const std::string &V) : Value(V) {}
592 const std::string &getValue() const { return Value; }
594 virtual Init *convertInitializerTo(RecTy *Ty) {
595 return Ty->convertValue(this);
598 virtual std::string getAsString() const { return "\"" + Value + "\""; }
601 /// CodeInit - "[{...}]" - Represent a code fragment.
603 class CodeInit : public Init {
604 std::string Value;
605 public:
606 explicit CodeInit(const std::string &V) : Value(V) {}
608 const std::string getValue() const { return Value; }
610 virtual Init *convertInitializerTo(RecTy *Ty) {
611 return Ty->convertValue(this);
614 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
617 /// ListInit - [AL, AH, CL] - Represent a list of defs
619 class ListInit : public Init {
620 std::vector<Init*> Values;
621 public:
622 explicit ListInit(std::vector<Init*> &Vs) {
623 Values.swap(Vs);
626 unsigned getSize() const { return Values.size(); }
627 Init *getElement(unsigned i) const {
628 assert(i < Values.size() && "List element index out of range!");
629 return Values[i];
632 Record *getElementAsRecord(unsigned i) const;
634 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
636 virtual Init *convertInitializerTo(RecTy *Ty) {
637 return Ty->convertValue(this);
640 /// resolveReferences - This method is used by classes that refer to other
641 /// variables which may not be defined at the time they expression is formed.
642 /// If a value is set for the variable later, this method will be called on
643 /// users of the value to allow the value to propagate out.
645 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
647 virtual std::string getAsString() const;
649 typedef std::vector<Init*>::iterator iterator;
650 typedef std::vector<Init*>::const_iterator const_iterator;
652 inline iterator begin() { return Values.begin(); }
653 inline const_iterator begin() const { return Values.begin(); }
654 inline iterator end () { return Values.end(); }
655 inline const_iterator end () const { return Values.end(); }
657 inline size_t size () const { return Values.size(); }
658 inline bool empty() const { return Values.empty(); }
662 /// TypedInit - This is the common super-class of types that have a specific,
663 /// explicit, type.
665 class TypedInit : public Init {
666 RecTy *Ty;
667 public:
668 explicit TypedInit(RecTy *T) : Ty(T) {}
670 RecTy *getType() const { return Ty; }
672 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
673 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
675 /// resolveBitReference - This method is used to implement
676 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
677 /// simply return the resolved value, otherwise we return null.
679 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
680 unsigned Bit) = 0;
682 /// resolveListElementReference - This method is used to implement
683 /// VarListElementInit::resolveReferences. If the list element is resolvable
684 /// now, we return the resolved value, otherwise we return null.
685 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
686 unsigned Elt) = 0;
690 /// BinOpInit - !op (X, Y) - Combine two inits.
692 class BinOpInit : public TypedInit {
693 public:
694 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT };
695 private:
696 BinaryOp Opc;
697 Init *LHS, *RHS;
698 public:
699 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
700 TypedInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {
703 BinaryOp getOpcode() const { return Opc; }
704 Init *getLHS() const { return LHS; }
705 Init *getRHS() const { return RHS; }
707 // Fold - If possible, fold this to a simpler init. Return this if not
708 // possible to fold.
709 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
711 virtual Init *convertInitializerTo(RecTy *Ty) {
712 return Ty->convertValue(this);
715 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
716 unsigned Bit);
717 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
718 unsigned Elt);
720 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
722 virtual std::string getAsString() const;
726 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
728 class VarInit : public TypedInit {
729 std::string VarName;
730 public:
731 explicit VarInit(const std::string &VN, RecTy *T)
732 : TypedInit(T), VarName(VN) {}
734 virtual Init *convertInitializerTo(RecTy *Ty) {
735 return Ty->convertValue(this);
738 const std::string &getName() const { return VarName; }
740 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
741 unsigned Bit);
742 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
743 unsigned Elt);
745 virtual RecTy *getFieldType(const std::string &FieldName) const;
746 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
748 /// resolveReferences - This method is used by classes that refer to other
749 /// variables which may not be defined at the time they expression is formed.
750 /// If a value is set for the variable later, this method will be called on
751 /// users of the value to allow the value to propagate out.
753 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
755 virtual std::string getAsString() const { return VarName; }
759 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
761 class VarBitInit : public Init {
762 TypedInit *TI;
763 unsigned Bit;
764 public:
765 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
766 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
767 ((BitsRecTy*)T->getType())->getNumBits() > B &&
768 "Illegal VarBitInit expression!");
771 virtual Init *convertInitializerTo(RecTy *Ty) {
772 return Ty->convertValue(this);
775 TypedInit *getVariable() const { return TI; }
776 unsigned getBitNum() const { return Bit; }
778 virtual std::string getAsString() const;
779 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
782 /// VarListElementInit - List[4] - Represent access to one element of a var or
783 /// field.
784 class VarListElementInit : public TypedInit {
785 TypedInit *TI;
786 unsigned Element;
787 public:
788 VarListElementInit(TypedInit *T, unsigned E)
789 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
790 TI(T), Element(E) {
791 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
792 "Illegal VarBitInit expression!");
795 virtual Init *convertInitializerTo(RecTy *Ty) {
796 return Ty->convertValue(this);
799 TypedInit *getVariable() const { return TI; }
800 unsigned getElementNum() const { return Element; }
802 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
803 unsigned Bit);
805 /// resolveListElementReference - This method is used to implement
806 /// VarListElementInit::resolveReferences. If the list element is resolvable
807 /// now, we return the resolved value, otherwise we return null.
808 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
809 unsigned Elt);
811 virtual std::string getAsString() const;
812 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
815 /// DefInit - AL - Represent a reference to a 'def' in the description
817 class DefInit : public Init {
818 Record *Def;
819 public:
820 explicit DefInit(Record *D) : Def(D) {}
822 virtual Init *convertInitializerTo(RecTy *Ty) {
823 return Ty->convertValue(this);
826 Record *getDef() const { return Def; }
828 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
830 virtual RecTy *getFieldType(const std::string &FieldName) const;
831 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
833 virtual std::string getAsString() const;
837 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
839 class FieldInit : public TypedInit {
840 Init *Rec; // Record we are referring to
841 std::string FieldName; // Field we are accessing
842 public:
843 FieldInit(Init *R, const std::string &FN)
844 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
845 assert(getType() && "FieldInit with non-record type!");
848 virtual Init *convertInitializerTo(RecTy *Ty) {
849 return Ty->convertValue(this);
852 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
853 unsigned Bit);
854 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
855 unsigned Elt);
857 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
859 virtual std::string getAsString() const {
860 return Rec->getAsString() + "." + FieldName;
864 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
865 /// to have at least one value then a (possibly empty) list of arguments. Each
866 /// argument can have a name associated with it.
868 class DagInit : public Init {
869 Init *Val;
870 std::string ValName;
871 std::vector<Init*> Args;
872 std::vector<std::string> ArgNames;
873 public:
874 DagInit(Init *V, std::string VN,
875 const std::vector<std::pair<Init*, std::string> > &args)
876 : Val(V), ValName(VN) {
877 Args.reserve(args.size());
878 ArgNames.reserve(args.size());
879 for (unsigned i = 0, e = args.size(); i != e; ++i) {
880 Args.push_back(args[i].first);
881 ArgNames.push_back(args[i].second);
884 DagInit(Init *V, std::string VN, const std::vector<Init*> &args,
885 const std::vector<std::string> &argNames)
886 : Val(V), ValName(VN), Args(args), ArgNames(argNames) {
889 virtual Init *convertInitializerTo(RecTy *Ty) {
890 return Ty->convertValue(this);
893 Init *getOperator() const { return Val; }
895 const std::string &getName() const { return ValName; }
897 unsigned getNumArgs() const { return Args.size(); }
898 Init *getArg(unsigned Num) const {
899 assert(Num < Args.size() && "Arg number out of range!");
900 return Args[Num];
902 const std::string &getArgName(unsigned Num) const {
903 assert(Num < ArgNames.size() && "Arg number out of range!");
904 return ArgNames[Num];
907 void setArg(unsigned Num, Init *I) {
908 assert(Num < Args.size() && "Arg number out of range!");
909 Args[Num] = I;
912 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
914 virtual std::string getAsString() const;
916 typedef std::vector<Init*>::iterator arg_iterator;
917 typedef std::vector<Init*>::const_iterator const_arg_iterator;
918 typedef std::vector<std::string>::iterator name_iterator;
919 typedef std::vector<std::string>::const_iterator const_name_iterator;
921 inline arg_iterator arg_begin() { return Args.begin(); }
922 inline const_arg_iterator arg_begin() const { return Args.begin(); }
923 inline arg_iterator arg_end () { return Args.end(); }
924 inline const_arg_iterator arg_end () const { return Args.end(); }
926 inline size_t arg_size () const { return Args.size(); }
927 inline bool arg_empty() const { return Args.empty(); }
929 inline name_iterator name_begin() { return ArgNames.begin(); }
930 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
931 inline name_iterator name_end () { return ArgNames.end(); }
932 inline const_name_iterator name_end () const { return ArgNames.end(); }
934 inline size_t name_size () const { return ArgNames.size(); }
935 inline bool name_empty() const { return ArgNames.empty(); }
939 //===----------------------------------------------------------------------===//
940 // High-Level Classes
941 //===----------------------------------------------------------------------===//
943 class RecordVal {
944 std::string Name;
945 RecTy *Ty;
946 unsigned Prefix;
947 Init *Value;
948 public:
949 RecordVal(const std::string &N, RecTy *T, unsigned P);
951 const std::string &getName() const { return Name; }
953 unsigned getPrefix() const { return Prefix; }
954 RecTy *getType() const { return Ty; }
955 Init *getValue() const { return Value; }
957 bool setValue(Init *V) {
958 if (V) {
959 Value = V->convertInitializerTo(Ty);
960 return Value == 0;
962 Value = 0;
963 return false;
966 void dump() const;
967 void print(std::ostream &OS, bool PrintSem = true) const;
970 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
971 RV.print(OS << " ");
972 return OS;
975 class Record {
976 std::string Name;
977 TGLoc Loc;
978 std::vector<std::string> TemplateArgs;
979 std::vector<RecordVal> Values;
980 std::vector<Record*> SuperClasses;
981 public:
983 explicit Record(const std::string &N, TGLoc loc) : Name(N), Loc(loc) {}
984 ~Record() {}
986 const std::string &getName() const { return Name; }
987 void setName(const std::string &Name); // Also updates RecordKeeper.
989 TGLoc getLoc() const { return Loc; }
991 const std::vector<std::string> &getTemplateArgs() const {
992 return TemplateArgs;
994 const std::vector<RecordVal> &getValues() const { return Values; }
995 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
997 bool isTemplateArg(const std::string &Name) const {
998 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
999 if (TemplateArgs[i] == Name) return true;
1000 return false;
1003 const RecordVal *getValue(const std::string &Name) const {
1004 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1005 if (Values[i].getName() == Name) return &Values[i];
1006 return 0;
1008 RecordVal *getValue(const std::string &Name) {
1009 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1010 if (Values[i].getName() == Name) return &Values[i];
1011 return 0;
1014 void addTemplateArg(const std::string &Name) {
1015 assert(!isTemplateArg(Name) && "Template arg already defined!");
1016 TemplateArgs.push_back(Name);
1019 void addValue(const RecordVal &RV) {
1020 assert(getValue(RV.getName()) == 0 && "Value already added!");
1021 Values.push_back(RV);
1024 void removeValue(const std::string &Name) {
1025 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
1026 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1027 if (Values[i].getName() == Name) {
1028 Values.erase(Values.begin()+i);
1029 return;
1031 assert(0 && "Name does not exist in record!");
1034 bool isSubClassOf(const Record *R) const {
1035 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1036 if (SuperClasses[i] == R)
1037 return true;
1038 return false;
1041 bool isSubClassOf(const std::string &Name) const {
1042 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1043 if (SuperClasses[i]->getName() == Name)
1044 return true;
1045 return false;
1048 void addSuperClass(Record *R) {
1049 assert(!isSubClassOf(R) && "Already subclassing record!");
1050 SuperClasses.push_back(R);
1053 /// resolveReferences - If there are any field references that refer to fields
1054 /// that have been filled in, we can propagate the values now.
1056 void resolveReferences() { resolveReferencesTo(0); }
1058 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1059 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1060 /// possible references.
1061 void resolveReferencesTo(const RecordVal *RV);
1063 void dump() const;
1065 //===--------------------------------------------------------------------===//
1066 // High-level methods useful to tablegen back-ends
1069 /// getValueInit - Return the initializer for a value with the specified name,
1070 /// or throw an exception if the field does not exist.
1072 Init *getValueInit(const std::string &FieldName) const;
1074 /// getValueAsString - This method looks up the specified field and returns
1075 /// its value as a string, throwing an exception if the field does not exist
1076 /// or if the value is not a string.
1078 std::string getValueAsString(const std::string &FieldName) const;
1080 /// getValueAsBitsInit - This method looks up the specified field and returns
1081 /// its value as a BitsInit, throwing an exception if the field does not exist
1082 /// or if the value is not the right type.
1084 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
1086 /// getValueAsListInit - This method looks up the specified field and returns
1087 /// its value as a ListInit, throwing an exception if the field does not exist
1088 /// or if the value is not the right type.
1090 ListInit *getValueAsListInit(const std::string &FieldName) const;
1092 /// getValueAsListOfDefs - This method looks up the specified field and
1093 /// returns its value as a vector of records, throwing an exception if the
1094 /// field does not exist or if the value is not the right type.
1096 std::vector<Record*> getValueAsListOfDefs(const std::string &FieldName) const;
1098 /// getValueAsListOfInts - This method looks up the specified field and returns
1099 /// its value as a vector of integers, throwing an exception if the field does
1100 /// not exist or if the value is not the right type.
1102 std::vector<int64_t> getValueAsListOfInts(const std::string &FieldName) const;
1104 /// getValueAsDef - This method looks up the specified field and returns its
1105 /// value as a Record, throwing an exception if the field does not exist or if
1106 /// the value is not the right type.
1108 Record *getValueAsDef(const std::string &FieldName) const;
1110 /// getValueAsBit - This method looks up the specified field and returns its
1111 /// value as a bit, throwing an exception if the field does not exist or if
1112 /// the value is not the right type.
1114 bool getValueAsBit(const std::string &FieldName) const;
1116 /// getValueAsInt - This method looks up the specified field and returns its
1117 /// value as an int64_t, throwing an exception if the field does not exist or
1118 /// if the value is not the right type.
1120 int64_t getValueAsInt(const std::string &FieldName) const;
1122 /// getValueAsDag - This method looks up the specified field and returns its
1123 /// value as an Dag, throwing an exception if the field does not exist or if
1124 /// the value is not the right type.
1126 DagInit *getValueAsDag(const std::string &FieldName) const;
1128 /// getValueAsCode - This method looks up the specified field and returns
1129 /// its value as the string data in a CodeInit, throwing an exception if the
1130 /// field does not exist or if the value is not a code object.
1132 std::string getValueAsCode(const std::string &FieldName) const;
1135 std::ostream &operator<<(std::ostream &OS, const Record &R);
1137 struct MultiClass {
1138 Record Rec; // Placeholder for template args and Name.
1139 typedef std::vector<Record*> RecordVector;
1140 RecordVector DefPrototypes;
1142 void dump() const;
1144 MultiClass(const std::string &Name, TGLoc Loc) : Rec(Name, Loc) {}
1147 class RecordKeeper {
1148 std::map<std::string, Record*> Classes, Defs;
1149 public:
1150 ~RecordKeeper() {
1151 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1152 E = Classes.end(); I != E; ++I)
1153 delete I->second;
1154 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1155 E = Defs.end(); I != E; ++I)
1156 delete I->second;
1159 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1160 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1162 Record *getClass(const std::string &Name) const {
1163 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1164 return I == Classes.end() ? 0 : I->second;
1166 Record *getDef(const std::string &Name) const {
1167 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1168 return I == Defs.end() ? 0 : I->second;
1170 void addClass(Record *R) {
1171 assert(getClass(R->getName()) == 0 && "Class already exists!");
1172 Classes.insert(std::make_pair(R->getName(), R));
1174 void addDef(Record *R) {
1175 assert(getDef(R->getName()) == 0 && "Def already exists!");
1176 Defs.insert(std::make_pair(R->getName(), R));
1179 /// removeClass - Remove, but do not delete, the specified record.
1181 void removeClass(const std::string &Name) {
1182 assert(Classes.count(Name) && "Class does not exist!");
1183 Classes.erase(Name);
1185 /// removeDef - Remove, but do not delete, the specified record.
1187 void removeDef(const std::string &Name) {
1188 assert(Defs.count(Name) && "Def does not exist!");
1189 Defs.erase(Name);
1192 //===--------------------------------------------------------------------===//
1193 // High-level helper methods, useful for tablegen backends...
1195 /// getAllDerivedDefinitions - This method returns all concrete definitions
1196 /// that derive from the specified class name. If a class with the specified
1197 /// name does not exist, an exception is thrown.
1198 std::vector<Record*>
1199 getAllDerivedDefinitions(const std::string &ClassName) const;
1202 void dump() const;
1205 /// LessRecord - Sorting predicate to sort record pointers by name.
1207 struct LessRecord {
1208 bool operator()(const Record *Rec1, const Record *Rec2) const {
1209 return Rec1->getName() < Rec2->getName();
1213 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1214 /// name field.
1216 struct LessRecordFieldName {
1217 bool operator()(const Record *Rec1, const Record *Rec2) const {
1218 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1223 class TGError {
1224 TGLoc Loc;
1225 std::string Message;
1226 public:
1227 TGError(TGLoc loc, const std::string &message) : Loc(loc), Message(message) {}
1229 TGLoc getLoc() const { return Loc; }
1230 const std::string &getMessage() const { return Message; }
1234 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
1236 extern RecordKeeper Records;
1238 void PrintError(TGLoc ErrorLoc, const std::string &Msg);
1241 } // End llvm namespace
1243 #endif