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