struct Init -> class Init.
[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/ADT/FoldingSet.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <map>
24
25 namespace llvm {
26 class raw_ostream;
27
28 // RecTy subclasses.
29 class BitRecTy;
30 class BitsRecTy;
31 class IntRecTy;
32 class StringRecTy;
33 class ListRecTy;
34 class CodeRecTy;
35 class DagRecTy;
36 class RecordRecTy;
37
38 // Init subclasses.
39 class Init;
40 class UnsetInit;
41 class BitInit;
42 class BitsInit;
43 class IntInit;
44 class StringInit;
45 class CodeInit;
46 class ListInit;
47 class UnOpInit;
48 class BinOpInit;
49 class TernOpInit;
50 class DefInit;
51 class DagInit;
52 class TypedInit;
53 class VarInit;
54 class FieldInit;
55 class VarBitInit;
56 class VarListElementInit;
57
58 // Other classes.
59 class Record;
60 class RecordVal;
61 struct MultiClass;
62 class RecordKeeper;
63
64 //===----------------------------------------------------------------------===//
65 //  Type Classes
66 //===----------------------------------------------------------------------===//
67
68 struct RecTy {
69   virtual ~RecTy() {}
70
71   virtual std::string getAsString() const = 0;
72   void print(raw_ostream &OS) const { OS << getAsString(); }
73   void dump() const;
74
75   /// typeIsConvertibleTo - Return true if all values of 'this' type can be
76   /// converted to the specified type.
77   virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
78
79 public:   // These methods should only be called from subclasses of Init
80   virtual const Init *convertValue(const  UnsetInit *UI) { return 0; }
81   virtual const Init *convertValue(const    BitInit *BI) { return 0; }
82   virtual const Init *convertValue(const   BitsInit *BI) { return 0; }
83   virtual const Init *convertValue(const    IntInit *II) { return 0; }
84   virtual const Init *convertValue(const StringInit *SI) { return 0; }
85   virtual const Init *convertValue(const   ListInit *LI) { return 0; }
86   virtual const Init *convertValue(const   UnOpInit *UI) {
87     return convertValue((const TypedInit*)UI);
88   }
89   virtual const Init *convertValue(const  BinOpInit *UI) {
90     return convertValue((const TypedInit*)UI);
91   }
92   virtual const Init *convertValue(const TernOpInit *UI) {
93     return convertValue((const TypedInit*)UI);
94   }
95   virtual const Init *convertValue(const   CodeInit *CI) { return 0; }
96   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
97   virtual const Init *convertValue(const    DefInit *DI) { return 0; }
98   virtual const Init *convertValue(const    DagInit *DI) { return 0; }
99   virtual const Init *convertValue(const  TypedInit *TI) { return 0; }
100   virtual const Init *convertValue(const    VarInit *VI) {
101     return convertValue((const TypedInit*)VI);
102   }
103   virtual const Init *convertValue(const  FieldInit *FI) {
104     return convertValue((const TypedInit*)FI);
105   }
106
107 public:   // These methods should only be called by subclasses of RecTy.
108   // baseClassOf - These virtual methods should be overloaded to return true iff
109   // all values of type 'RHS' can be converted to the 'this' type.
110   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
111   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
112   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
113   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
114   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
115   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
116   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
117   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
118 };
119
120 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
121   Ty.print(OS);
122   return OS;
123 }
124
125
126 /// BitRecTy - 'bit' - Represent a single bit
127 ///
128 class BitRecTy : public RecTy {
129 public:
130   virtual const Init *convertValue(const  UnsetInit *UI) {
131     return (const Init*)UI;
132   }
133   virtual const Init *convertValue(const    BitInit *BI) {
134     return (const Init*)BI;
135   }
136   virtual const Init *convertValue(const   BitsInit *BI);
137   virtual const Init *convertValue(const    IntInit *II);
138   virtual const Init *convertValue(const StringInit *SI) { return 0; }
139   virtual const Init *convertValue(const   ListInit *LI) { return 0; }
140   virtual const Init *convertValue(const   CodeInit *CI) { return 0; }
141   virtual const Init *convertValue(const VarBitInit *VB) {
142     return (const Init*)VB;
143   }
144   virtual const Init *convertValue(const    DefInit *DI) { return 0; }
145   virtual const Init *convertValue(const    DagInit *DI) { return 0; }
146   virtual const Init *convertValue(const   UnOpInit *UI) {
147     return RecTy::convertValue(UI);
148   }
149   virtual const Init *convertValue(const  BinOpInit *UI) {
150     return RecTy::convertValue(UI);
151   }
152   virtual const Init *convertValue(const TernOpInit *UI) {
153     return RecTy::convertValue(UI);
154   }
155   virtual const Init *convertValue(const  TypedInit *TI);
156   virtual const Init *convertValue(const    VarInit *VI) {
157     return RecTy::convertValue(VI);
158   }
159   virtual const Init *convertValue(const  FieldInit *FI) {
160     return RecTy::convertValue(FI);
161   }
162
163   std::string getAsString() const { return "bit"; }
164
165   bool typeIsConvertibleTo(const RecTy *RHS) const {
166     return RHS->baseClassOf(this);
167   }
168   virtual bool baseClassOf(const BitRecTy    *RHS) const { return true; }
169   virtual bool baseClassOf(const BitsRecTy   *RHS) const;
170   virtual bool baseClassOf(const IntRecTy    *RHS) const { return true; }
171   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
172   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
173   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
174   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
175   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
176
177 };
178
179
180 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
181 /// BitsRecTy - 'bits&lt;n&gt;' - Represent a fixed number of bits
182 ///
183 class BitsRecTy : public RecTy {
184   unsigned Size;
185 public:
186   explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
187
188   unsigned getNumBits() const { return Size; }
189
190   virtual const Init *convertValue(const  UnsetInit *UI);
191   virtual const Init *convertValue(const    BitInit *UI);
192   virtual const Init *convertValue(const   BitsInit *BI);
193   virtual const Init *convertValue(const    IntInit *II);
194   virtual const Init *convertValue(const StringInit *SI) { return 0; }
195   virtual const Init *convertValue(const   ListInit *LI) { return 0; }
196   virtual const Init *convertValue(const   CodeInit *CI) { return 0; }
197   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
198   virtual const Init *convertValue(const    DefInit *DI) { return 0; }
199   virtual const Init *convertValue(const    DagInit *DI) { return 0; }
200   virtual const Init *convertValue(const   UnOpInit *UI) {
201     return RecTy::convertValue(UI);
202   }
203   virtual const Init *convertValue(const  BinOpInit *UI) {
204     return RecTy::convertValue(UI);
205   }
206   virtual const Init *convertValue(const TernOpInit *UI) {
207     return RecTy::convertValue(UI);
208   }
209   virtual const Init *convertValue(const  TypedInit *TI);
210   virtual const Init *convertValue(const    VarInit *VI) {
211     return RecTy::convertValue(VI);
212   }
213   virtual const Init *convertValue(const  FieldInit *FI) {
214     return RecTy::convertValue(FI);
215   }
216
217   std::string getAsString() const;
218
219   bool typeIsConvertibleTo(const RecTy *RHS) const {
220     return RHS->baseClassOf(this);
221   }
222   virtual bool baseClassOf(const BitRecTy    *RHS) const { return Size == 1; }
223   virtual bool baseClassOf(const BitsRecTy   *RHS) const {
224     return RHS->Size == Size;
225   }
226   virtual bool baseClassOf(const IntRecTy    *RHS) const { return true; }
227   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
228   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
229   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
230   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
231   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
232
233 };
234
235
236 /// IntRecTy - 'int' - Represent an integer value of no particular size
237 ///
238 class IntRecTy : public RecTy {
239 public:
240   virtual const Init *convertValue(const  UnsetInit *UI) {
241     return (const Init*)UI;
242   }
243   virtual const Init *convertValue(const    BitInit *BI);
244   virtual const Init *convertValue(const   BitsInit *BI);
245   virtual const Init *convertValue(const    IntInit *II) {
246     return (const Init*)II;
247   }
248   virtual const Init *convertValue(const StringInit *SI) { return 0; }
249   virtual const Init *convertValue(const   ListInit *LI) { return 0; }
250   virtual const Init *convertValue(const   CodeInit *CI) { return 0; }
251   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
252   virtual const Init *convertValue(const    DefInit *DI) { return 0; }
253   virtual const Init *convertValue(const    DagInit *DI) { return 0; }
254   virtual const Init *convertValue(const  UnOpInit *UI) {
255     return RecTy::convertValue(UI);
256   }
257   virtual const Init *convertValue(const  BinOpInit *UI) {
258     return RecTy::convertValue(UI);
259   }
260   virtual const Init *convertValue(const  TernOpInit *UI) {
261     return RecTy::convertValue(UI);
262   }
263   virtual const Init *convertValue(const  TypedInit *TI);
264   virtual const Init *convertValue(const    VarInit *VI) {
265     return RecTy::convertValue(VI);
266   }
267   virtual const Init *convertValue(const  FieldInit *FI) {
268     return RecTy::convertValue(FI);
269   }
270
271   std::string getAsString() const { return "int"; }
272
273   bool typeIsConvertibleTo(const RecTy *RHS) const {
274     return RHS->baseClassOf(this);
275   }
276
277   virtual bool baseClassOf(const BitRecTy    *RHS) const { return true; }
278   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return true; }
279   virtual bool baseClassOf(const IntRecTy    *RHS) const { return true; }
280   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
281   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
282   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
283   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
284   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
285
286 };
287
288 /// StringRecTy - 'string' - Represent an string value
289 ///
290 class StringRecTy : public RecTy {
291 public:
292   virtual const Init *convertValue(const  UnsetInit *UI) {
293     return (const Init*)UI;
294   }
295   virtual const Init *convertValue(const    BitInit *BI) { return 0; }
296   virtual const Init *convertValue(const   BitsInit *BI) { return 0; }
297   virtual const Init *convertValue(const    IntInit *II) { return 0; }
298   virtual const Init *convertValue(const StringInit *SI) {
299     return (const Init*)SI;
300   }
301   virtual const Init *convertValue(const   ListInit *LI) { return 0; }
302   virtual const Init *convertValue(const   UnOpInit *BO);
303   virtual const Init *convertValue(const  BinOpInit *BO);
304   virtual const Init *convertValue(const TernOpInit *BO) {
305     return RecTy::convertValue(BO);
306   }
307
308   virtual const Init *convertValue(const   CodeInit *CI) { return 0; }
309   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
310   virtual const Init *convertValue(const    DefInit *DI) { return 0; }
311   virtual const Init *convertValue(const    DagInit *DI) { return 0; }
312   virtual const Init *convertValue(const  TypedInit *TI);
313   virtual const Init *convertValue(const    VarInit *VI) {
314     return RecTy::convertValue(VI);
315   }
316   virtual const Init *convertValue(const  FieldInit *FI) {
317     return RecTy::convertValue(FI);
318   }
319
320   std::string getAsString() const { return "string"; }
321
322   bool typeIsConvertibleTo(const RecTy *RHS) const {
323     return RHS->baseClassOf(this);
324   }
325
326   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
327   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
328   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
329   virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
330   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
331   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
332   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
333   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
334 };
335
336 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
337 // the specified type.
338 /// ListRecTy - 'list&lt;Ty&gt;' - Represent a list of values, all of which must
339 /// be of the specified type.
340 ///
341 class ListRecTy : public RecTy {
342   RecTy *Ty;
343 public:
344   explicit ListRecTy(RecTy *T) : Ty(T) {}
345
346   RecTy *getElementType() const { return Ty; }
347
348   virtual const Init *convertValue(const  UnsetInit *UI) {
349     return (const Init*)UI;
350   }
351   virtual const Init *convertValue(const    BitInit *BI) { return 0; }
352   virtual const Init *convertValue(const   BitsInit *BI) { return 0; }
353   virtual const Init *convertValue(const    IntInit *II) { return 0; }
354   virtual const Init *convertValue(const StringInit *SI) { return 0; }
355   virtual const Init *convertValue(const   ListInit *LI);
356   virtual const Init *convertValue(const   CodeInit *CI) { return 0; }
357   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
358   virtual const Init *convertValue(const    DefInit *DI) { return 0; }
359   virtual const Init *convertValue(const    DagInit *DI) { return 0; }
360   virtual const Init *convertValue(const  UnOpInit *UI) {
361     return RecTy::convertValue(UI);
362   }
363   virtual const Init *convertValue(const  BinOpInit *UI) {
364     return RecTy::convertValue(UI);
365   }
366   virtual const Init *convertValue(const  TernOpInit *UI) {
367     return RecTy::convertValue(UI);
368   }
369   virtual const Init *convertValue(const  TypedInit *TI);
370   virtual const Init *convertValue(const    VarInit *VI) {
371     return RecTy::convertValue(VI);
372   }
373   virtual const Init *convertValue(const  FieldInit *FI) {
374     return RecTy::convertValue(FI);
375   }
376
377   std::string getAsString() const;
378
379   bool typeIsConvertibleTo(const RecTy *RHS) const {
380     return RHS->baseClassOf(this);
381   }
382
383   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
384   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
385   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
386   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
387   virtual bool baseClassOf(const ListRecTy   *RHS) const {
388     return RHS->getElementType()->typeIsConvertibleTo(Ty);
389   }
390   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
391   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
392   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
393 };
394
395 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
396 ///
397 class CodeRecTy : public RecTy {
398 public:
399   virtual const Init *convertValue(const  UnsetInit *UI) {
400     return (const Init*)UI;
401   }
402   virtual const Init *convertValue(const    BitInit *BI) { return 0; }
403   virtual const Init *convertValue(const   BitsInit *BI) { return 0; }
404   virtual const Init *convertValue(const    IntInit *II) { return 0; }
405   virtual const Init *convertValue(const StringInit *SI) { return 0; }
406   virtual const Init *convertValue(const   ListInit *LI) { return 0; }
407   virtual const Init *convertValue(const   CodeInit *CI) {
408     return (const Init*)CI;
409   }
410   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
411   virtual const Init *convertValue(const    DefInit *DI) { return 0; }
412   virtual const Init *convertValue(const    DagInit *DI) { return 0; }
413   virtual const Init *convertValue(const  UnOpInit *UI) {
414     return RecTy::convertValue(UI);
415   }
416   virtual const Init *convertValue(const  BinOpInit *UI) {
417     return RecTy::convertValue(UI);
418   }
419   virtual const Init *convertValue(const  TernOpInit *UI) {
420     return RecTy::convertValue(UI);
421   }
422   virtual const Init *convertValue(const  TypedInit *TI);
423   virtual const Init *convertValue(const    VarInit *VI) {
424     return RecTy::convertValue(VI);
425   }
426   virtual const Init *convertValue(const  FieldInit *FI) {
427     return RecTy::convertValue(FI);
428   }
429
430   std::string getAsString() const { return "code"; }
431
432   bool typeIsConvertibleTo(const RecTy *RHS) const {
433     return RHS->baseClassOf(this);
434   }
435   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
436   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
437   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
438   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
439   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
440   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return true; }
441   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
442   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
443 };
444
445 /// DagRecTy - 'dag' - Represent a dag fragment
446 ///
447 class DagRecTy : public RecTy {
448 public:
449   virtual const Init *convertValue(const  UnsetInit *UI) {
450     return (const Init*)UI;
451   }
452   virtual const Init *convertValue(const    BitInit *BI) { return 0; }
453   virtual const Init *convertValue(const   BitsInit *BI) { return 0; }
454   virtual const Init *convertValue(const    IntInit *II) { return 0; }
455   virtual const Init *convertValue(const StringInit *SI) { return 0; }
456   virtual const Init *convertValue(const   ListInit *LI) { return 0; }
457   virtual const Init *convertValue(const   CodeInit *CI) { return 0; }
458   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
459   virtual const Init *convertValue(const    DefInit *DI) { return 0; }
460   virtual const Init *convertValue(const  UnOpInit *BO);
461   virtual const Init *convertValue(const  BinOpInit *BO);
462   virtual const Init *convertValue(const  TernOpInit *BO) {
463     return RecTy::convertValue(BO);
464   }
465   virtual const Init *convertValue(const    DagInit *CI) {
466     return (const Init*)CI;
467   }
468   virtual const Init *convertValue(const  TypedInit *TI);
469   virtual const Init *convertValue(const    VarInit *VI) {
470     return RecTy::convertValue(VI);
471   }
472   virtual const Init *convertValue(const  FieldInit *FI) {
473     return RecTy::convertValue(FI);
474   }
475
476   std::string getAsString() const { return "dag"; }
477
478   bool typeIsConvertibleTo(const RecTy *RHS) const {
479     return RHS->baseClassOf(this);
480   }
481
482   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
483   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
484   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
485   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
486   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
487   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
488   virtual bool baseClassOf(const DagRecTy    *RHS) const { return true; }
489   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
490 };
491
492
493 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
494 /// (R32 X = EAX).
495 ///
496 class RecordRecTy : public RecTy {
497   Record *Rec;
498 public:
499   explicit RecordRecTy(Record *R) : Rec(R) {}
500
501   Record *getRecord() const { return Rec; }
502
503   virtual const Init *convertValue(const  UnsetInit *UI) {
504     return (const Init*)UI;
505   }
506   virtual const Init *convertValue(const    BitInit *BI) { return 0; }
507   virtual const Init *convertValue(const   BitsInit *BI) { return 0; }
508   virtual const Init *convertValue(const    IntInit *II) { return 0; }
509   virtual const Init *convertValue(const StringInit *SI) { return 0; }
510   virtual const Init *convertValue(const   ListInit *LI) { return 0; }
511   virtual const Init *convertValue(const   CodeInit *CI) { return 0; }
512   virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
513   virtual const Init *convertValue(const  UnOpInit *UI) {
514     return RecTy::convertValue(UI);
515   }
516   virtual const Init *convertValue(const  BinOpInit *UI) {
517     return RecTy::convertValue(UI);
518   }
519   virtual const Init *convertValue(const  TernOpInit *UI) {
520     return RecTy::convertValue(UI);
521   }
522   virtual const Init *convertValue(const    DefInit *DI);
523   virtual const Init *convertValue(const    DagInit *DI) { return 0; }
524   virtual const Init *convertValue(const  TypedInit *VI);
525   virtual const Init *convertValue(const    VarInit *VI) {
526     return RecTy::convertValue(VI);
527   }
528   virtual const Init *convertValue(const  FieldInit *FI) {
529     return RecTy::convertValue(FI);
530   }
531
532   std::string getAsString() const;
533
534   bool typeIsConvertibleTo(const RecTy *RHS) const {
535     return RHS->baseClassOf(this);
536   }
537   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
538   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
539   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
540   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
541   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
542   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
543   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
544   virtual bool baseClassOf(const RecordRecTy *RHS) const;
545 };
546
547 /// resolveTypes - Find a common type that T1 and T2 convert to.
548 /// Return 0 if no such type exists.
549 ///
550 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
551
552 //===----------------------------------------------------------------------===//
553 //  Initializer Classes
554 //===----------------------------------------------------------------------===//
555
556 class Init : public FastFoldingSetNode {
557   Init(const Init &);  // Do not define.
558   Init &operator=(const Init &);  // Do not define.
559
560 protected:
561   Init(const FoldingSetNodeID &ID) : FastFoldingSetNode(ID) {}
562
563   static FoldingSet<Init> UniqueInits;
564   static BumpPtrAllocator InitAllocator;
565
566   enum Type {
567     initUnset,
568     initBit,
569     initBits,
570     initInt,
571     initString,
572     initCode,
573     initList,
574     initUnOp,
575     initBinOp,
576     initTernOp,
577     initQuadOp,
578     initVar,
579     initVarBit,
580     initVarListElement,
581     initDef,
582     initField,
583     initDag
584   };
585
586 public:
587   virtual ~Init() {}
588
589   static void ReleaseMemory() {
590     InitAllocator.Reset();
591   }
592
593   /// isComplete - This virtual method should be overridden by values that may
594   /// not be completely specified yet.
595   virtual bool isComplete() const { return true; }
596
597   /// print - Print out this value.
598   void print(raw_ostream &OS) const { OS << getAsString(); }
599
600   /// getAsString - Convert this value to a string form.
601   virtual std::string getAsString() const = 0;
602
603   /// dump - Debugging method that may be called through a debugger, just
604   /// invokes print on stderr.
605   void dump() const;
606
607   /// convertInitializerTo - This virtual function is a simple call-back
608   /// function that should be overridden to call the appropriate
609   /// RecTy::convertValue method.
610   ///
611   virtual const Init *convertInitializerTo(RecTy *Ty) const = 0;
612
613   /// convertInitializerBitRange - This method is used to implement the bitrange
614   /// selection operator.  Given an initializer, it selects the specified bits
615   /// out, returning them as a new init of bits type.  If it is not legal to use
616   /// the bit subscript operator on this initializer, return null.
617   ///
618   virtual const Init *
619   convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
620     return 0;
621   }
622
623   /// convertInitListSlice - This method is used to implement the list slice
624   /// selection operator.  Given an initializer, it selects the specified list
625   /// elements, returning them as a new init of list type.  If it is not legal
626   /// to take a slice of this, return null.
627   ///
628   virtual const Init *
629   convertInitListSlice(const std::vector<unsigned> &Elements) const {
630     return 0;
631   }
632
633   /// getFieldType - This method is used to implement the FieldInit class.
634   /// Implementors of this method should return the type of the named field if
635   /// they are of record type.
636   ///
637   virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
638
639   /// getFieldInit - This method complements getFieldType to return the
640   /// initializer for the specified field.  If getFieldType returns non-null
641   /// this method should return non-null, otherwise it returns null.
642   ///
643   virtual const Init *getFieldInit(Record &R, const RecordVal *RV,
644                                    const std::string &FieldName) const {
645     return 0;
646   }
647
648   /// resolveReferences - This method is used by classes that refer to other
649   /// variables which may not be defined at the time the expression is formed.
650   /// If a value is set for the variable later, this method will be called on
651   /// users of the value to allow the value to propagate out.
652   ///
653   virtual const Init *resolveReferences(Record &R,
654                                         const RecordVal *RV) const {
655     return this;
656   }
657 };
658
659 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
660   I.print(OS); return OS;
661 }
662
663 /// TypedInit - This is the common super-class of types that have a specific,
664 /// explicit, type.
665 ///
666 class TypedInit : public Init {
667   RecTy *Ty;
668
669   TypedInit(const TypedInit &Other);  // Do not define.
670   TypedInit &operator=(const TypedInit &Other);  // Do not define.
671
672 protected:
673   explicit TypedInit(const FoldingSetNodeID &ID, RecTy *T) : Init(ID), Ty(T) {}
674
675 public:
676   RecTy *getType() const { return Ty; }
677
678   virtual const Init *
679   convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
680   virtual const Init *
681   convertInitListSlice(const std::vector<unsigned> &Elements) const;
682
683   /// getFieldType - This method is used to implement the FieldInit class.
684   /// Implementors of this method should return the type of the named field if
685   /// they are of record type.
686   ///
687   virtual RecTy *getFieldType(const std::string &FieldName) const;
688
689   /// resolveBitReference - This method is used to implement
690   /// VarBitInit::resolveReferences.  If the bit is able to be resolved, we
691   /// simply return the resolved value, otherwise we return null.
692   ///
693   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
694                                           unsigned Bit) const = 0;
695
696   /// resolveListElementReference - This method is used to implement
697   /// VarListElementInit::resolveReferences.  If the list element is resolvable
698   /// now, we return the resolved value, otherwise we return null.
699   virtual const Init *resolveListElementReference(Record &R,
700                                                   const RecordVal *RV,
701                                                   unsigned Elt) const = 0;
702 };
703
704
705 /// UnsetInit - ? - Represents an uninitialized value
706 ///
707 class UnsetInit : public Init {
708   UnsetInit(const FoldingSetNodeID &ID) : Init(ID) {}
709   UnsetInit(const UnsetInit &);  // Do not define.
710   UnsetInit &operator=(const UnsetInit &Other);  // Do not define.
711
712 public:
713   static const UnsetInit *Create();
714
715   virtual const Init *convertInitializerTo(RecTy *Ty) const {
716     return Ty->convertValue(this);
717   }
718
719   virtual bool isComplete() const { return false; }
720   virtual std::string getAsString() const { return "?"; }
721 };
722
723
724 /// BitInit - true/false - Represent a concrete initializer for a bit.
725 ///
726 class BitInit : public Init {
727   bool Value;
728
729   explicit BitInit(const FoldingSetNodeID &ID, bool V) : Init(ID), Value(V) {}
730   BitInit(const BitInit &Other);  // Do not define.
731   BitInit &operator=(BitInit &Other);  // Do not define.
732
733 public:
734   static const BitInit *Create(bool V);
735
736   bool getValue() const { return Value; }
737
738   virtual const Init *convertInitializerTo(RecTy *Ty) const {
739     return Ty->convertValue(this);
740   }
741
742   virtual std::string getAsString() const { return Value ? "1" : "0"; }
743 };
744
745 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
746 /// It contains a vector of bits, whose size is determined by the type.
747 ///
748 class BitsInit : public Init {
749   std::vector<const Init*> Bits;
750
751   BitsInit(const FoldingSetNodeID &ID, unsigned Size)
752       : Init(ID), Bits(Size) {}
753
754   template<typename InputIterator>
755   BitsInit(const FoldingSetNodeID &ID, InputIterator start, InputIterator end) 
756       : Init(ID), Bits(start, end) {}
757
758   BitsInit(const BitsInit &Other);  // Do not define.
759   BitsInit &operator=(const BitsInit &Other);  // Do not define.
760
761 public:
762   template<typename InputIterator>
763   static const BitsInit *Create(InputIterator Start, InputIterator End) {
764     FoldingSetNodeID ID;
765     ID.AddInteger(initBits);
766     ID.AddInteger(std::distance(Start, End));
767
768     InputIterator S = Start;
769     while (S != End)
770       ID.AddPointer(*S++);
771
772     void *IP = 0;
773     if (const Init *I = UniqueInits.FindNodeOrInsertPos(ID, IP))
774       return static_cast<const BitsInit *>(I);
775
776     BitsInit *I = InitAllocator.Allocate<BitsInit>();
777     new (I) BitsInit(ID, Start, End);
778     UniqueInits.InsertNode(I, IP);
779     return I;
780   }
781
782   unsigned getNumBits() const { return Bits.size(); }
783
784   const Init *getBit(unsigned Bit) const {
785     assert(Bit < Bits.size() && "Bit index out of range!");
786     return Bits[Bit];
787   }
788   void setBit(unsigned Bit, const Init *V) {
789     assert(Bit < Bits.size() && "Bit index out of range!");
790     assert(Bits[Bit] == 0 && "Bit already set!");
791     Bits[Bit] = V;
792   }
793
794   virtual const Init *convertInitializerTo(RecTy *Ty) const {
795     return Ty->convertValue(this);
796   }
797   virtual const Init *
798   convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
799
800   virtual bool isComplete() const {
801     for (unsigned i = 0; i != getNumBits(); ++i)
802       if (!getBit(i)->isComplete()) return false;
803     return true;
804   }
805   bool allInComplete() const {
806     for (unsigned i = 0; i != getNumBits(); ++i)
807       if (getBit(i)->isComplete()) return false;
808     return true;
809   }
810   virtual std::string getAsString() const;
811
812   virtual const Init *resolveReferences(Record &R,
813                                         const RecordVal *RV) const;
814 };
815
816
817 /// IntInit - 7 - Represent an initalization by a literal integer value.
818 ///
819 class IntInit : public TypedInit {
820   int64_t Value;
821
822   explicit IntInit(const FoldingSetNodeID &ID, int64_t V)
823       : TypedInit(ID, new IntRecTy), Value(V) {}
824
825   IntInit(const IntInit &Other);  // Do not define.
826   IntInit &operator=(const IntInit &Other);  // Do note define.
827
828 public:
829   static const IntInit *Create(int64_t V);
830
831   int64_t getValue() const { return Value; }
832
833   virtual const Init *convertInitializerTo(RecTy *Ty) const {
834     return Ty->convertValue(this);
835   }
836   virtual const Init *
837   convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
838
839   virtual std::string getAsString() const;
840
841   /// resolveBitReference - This method is used to implement
842   /// VarBitInit::resolveReferences.  If the bit is able to be resolved, we
843   /// simply return the resolved value, otherwise we return null.
844   ///
845   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
846                                           unsigned Bit) const {
847     assert(0 && "Illegal bit reference off int");
848     return 0;
849   }
850
851   /// resolveListElementReference - This method is used to implement
852   /// VarListElementInit::resolveReferences.  If the list element is resolvable
853   /// now, we return the resolved value, otherwise we return null.
854   virtual const Init *resolveListElementReference(Record &R,
855                                                   const RecordVal *RV,
856                                                   unsigned Elt) const {
857     assert(0 && "Illegal element reference off int");
858     return 0;
859   }
860 };
861
862
863 /// StringInit - "foo" - Represent an initialization by a string value.
864 ///
865 class StringInit : public TypedInit {
866   std::string Value;
867
868   explicit StringInit(const FoldingSetNodeID &ID, const std::string &V)
869       : TypedInit(ID, new StringRecTy), Value(V) {}
870
871   StringInit(const StringInit &Other);  // Do not define.
872   StringInit &operator=(const StringInit &Other);  // Do not define.
873
874 public:
875   static const StringInit *Create(const std::string &V);
876
877   const std::string &getValue() const { return Value; }
878
879   virtual const Init *convertInitializerTo(RecTy *Ty) const {
880     return Ty->convertValue(this);
881   }
882
883   virtual std::string getAsString() const { return "\"" + Value + "\""; }
884
885   /// resolveBitReference - This method is used to implement
886   /// VarBitInit::resolveReferences.  If the bit is able to be resolved, we
887   /// simply return the resolved value, otherwise we return null.
888   ///
889   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
890                                           unsigned Bit) const {
891     assert(0 && "Illegal bit reference off string");
892     return 0;
893   }
894
895   /// resolveListElementReference - This method is used to implement
896   /// VarListElementInit::resolveReferences.  If the list element is resolvable
897   /// now, we return the resolved value, otherwise we return null.
898   virtual const Init *resolveListElementReference(Record &R,
899                                                   const RecordVal *RV,
900                                                   unsigned Elt) const {
901     assert(0 && "Illegal element reference off string");
902     return 0;
903   }
904 };
905
906 /// CodeInit - "[{...}]" - Represent a code fragment.
907 ///
908 class CodeInit : public Init {
909   std::string Value;
910
911   explicit CodeInit(const FoldingSetNodeID &ID, const std::string &V)
912       : Init(ID), Value(V) {}
913
914   CodeInit(const CodeInit &Other);  // Do not define.
915   CodeInit &operator=(const CodeInit &Other);  // Do not define.
916
917 public:
918   static const CodeInit *Create(const std::string &V);
919
920   const std::string &getValue() const { return Value; }
921
922   virtual const Init *convertInitializerTo(RecTy *Ty) const {
923     return Ty->convertValue(this);
924   }
925
926   virtual std::string getAsString() const { return "[{" + Value + "}]"; }
927 };
928
929 /// ListInit - [AL, AH, CL] - Represent a list of defs
930 ///
931 class ListInit : public TypedInit {
932   std::vector<const Init*> Values;
933
934 public:
935   typedef std::vector<const Init*>::const_iterator const_iterator;
936
937 private:
938   ListInit(const FoldingSetNodeID &ID, std::vector<const Init*> &Vs,
939            RecTy *EltTy)
940       : TypedInit(ID, new ListRecTy(EltTy)) {
941     Values.swap(Vs);
942   }
943
944   template<typename InputIterator>
945   ListInit(const FoldingSetNodeID &ID, InputIterator Start, InputIterator End,
946            RecTy *EltTy)
947       : TypedInit(ID, new ListRecTy(EltTy)), Values(Start, End) {}
948
949   ListInit(const ListInit &Other);  // Do not define.
950   ListInit &operator=(const ListInit &Other);  // Do not define.
951
952 public:
953   static const ListInit *Create(std::vector<const Init*> &Vs, RecTy *EltTy);
954
955   template<typename InputIterator>
956   static const ListInit *Create(InputIterator Start, InputIterator End,
957                                 RecTy *EltTy) {
958     FoldingSetNodeID ID;
959     ID.AddInteger(initList);
960     ID.AddString(EltTy->getAsString());
961
962     InputIterator S = Start;
963     while (S != End)
964       ID.AddPointer(*S++); 
965
966     void *IP = 0;
967     if (const Init *I = UniqueInits.FindNodeOrInsertPos(ID, IP))
968       return static_cast<const ListInit *>(I);
969
970     ListInit *I = InitAllocator.Allocate<ListInit>();
971     new (I) ListInit(ID, Start, End, EltTy);
972     UniqueInits.InsertNode(I, IP);
973     return I;
974   }
975
976   unsigned getSize() const { return Values.size(); }
977   const Init *getElement(unsigned i) const {
978     assert(i < Values.size() && "List element index out of range!");
979     return Values[i];
980   }
981
982   Record *getElementAsRecord(unsigned i) const;
983
984   const Init *convertInitListSlice(const std::vector<unsigned> &Elements) const;
985
986   virtual const Init *convertInitializerTo(RecTy *Ty) const {
987     return Ty->convertValue(this);
988   }
989
990   /// resolveReferences - This method is used by classes that refer to other
991   /// variables which may not be defined at the time they expression is formed.
992   /// If a value is set for the variable later, this method will be called on
993   /// users of the value to allow the value to propagate out.
994   ///
995   virtual const Init *resolveReferences(Record &R,
996                                         const RecordVal *RV) const;
997
998   virtual std::string getAsString() const;
999
1000   inline const_iterator begin() const { return Values.begin(); }
1001   inline const_iterator end  () const { return Values.end();   }
1002
1003   inline size_t         size () const { return Values.size();  }
1004   inline bool           empty() const { return Values.empty(); }
1005
1006   /// resolveBitReference - This method is used to implement
1007   /// VarBitInit::resolveReferences.  If the bit is able to be resolved, we
1008   /// simply return the resolved value, otherwise we return null.
1009   ///
1010   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1011                                           unsigned Bit) const {
1012     assert(0 && "Illegal bit reference off list");
1013     return 0;
1014   }
1015
1016   /// resolveListElementReference - This method is used to implement
1017   /// VarListElementInit::resolveReferences.  If the list element is resolvable
1018   /// now, we return the resolved value, otherwise we return null.
1019   virtual const Init *resolveListElementReference(Record &R,
1020                                                   const RecordVal *RV,
1021                                                   unsigned Elt) const;
1022 };
1023
1024
1025 /// OpInit - Base class for operators
1026 ///
1027 class OpInit : public TypedInit {
1028   OpInit(const OpInit &Other);  // Do not define.
1029   OpInit &operator=(OpInit &Other);  // Do not define.
1030
1031 protected:
1032   explicit OpInit(const FoldingSetNodeID &ID, RecTy *Type)
1033       : TypedInit(ID, Type) {}
1034
1035 public:
1036   // Clone - Clone this operator, replacing arguments with the new list
1037   virtual const OpInit *clone(std::vector<const Init *> &Operands) const = 0;
1038
1039   virtual int getNumOperands() const = 0;
1040   virtual const Init *getOperand(int i) const = 0;
1041
1042   // Fold - If possible, fold this to a simpler init.  Return this if not
1043   // possible to fold.
1044   virtual const Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
1045
1046   virtual const Init *convertInitializerTo(RecTy *Ty) const {
1047     return Ty->convertValue(this);
1048   }
1049
1050   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1051                                           unsigned Bit) const;
1052   virtual const Init *resolveListElementReference(Record &R,
1053                                                   const RecordVal *RV,
1054                                                   unsigned Elt) const;
1055 };
1056
1057
1058 /// UnOpInit - !op (X) - Transform an init.
1059 ///
1060 class UnOpInit : public OpInit {
1061 public:
1062   enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
1063 private:
1064   UnaryOp Opc;
1065   const Init *LHS;
1066
1067   UnOpInit(const FoldingSetNodeID &ID, UnaryOp opc, const Init *lhs,
1068            RecTy *Type)
1069       : OpInit(ID, Type), Opc(opc), LHS(lhs) {}
1070
1071   UnOpInit(const UnOpInit &Other);  // Do not define.
1072   UnOpInit &operator=(const UnOpInit &Other);  // Do not define.
1073
1074 public:
1075   static const UnOpInit *Create(UnaryOp opc, const Init *lhs, RecTy *Type);
1076
1077   // Clone - Clone this operator, replacing arguments with the new list
1078   virtual const OpInit *clone(std::vector<const Init *> &Operands) const {
1079     assert(Operands.size() == 1 &&
1080            "Wrong number of operands for unary operation");
1081     return UnOpInit::Create(getOpcode(), *Operands.begin(), getType());
1082   }
1083
1084   int getNumOperands() const { return 1; }
1085   const Init *getOperand(int i) const {
1086     assert(i == 0 && "Invalid operand id for unary operator");
1087     return getOperand();
1088   }
1089
1090   UnaryOp getOpcode() const { return Opc; }
1091   const Init *getOperand() const { return LHS; }
1092
1093   // Fold - If possible, fold this to a simpler init.  Return this if not
1094   // possible to fold.
1095   const Init *Fold(Record *CurRec, MultiClass *CurMultiClass)const ;
1096
1097   virtual const Init *resolveReferences(Record &R,
1098                                         const RecordVal *RV) const;
1099
1100   virtual std::string getAsString() const;
1101 };
1102
1103 /// BinOpInit - !op (X, Y) - Combine two inits.
1104 ///
1105 class BinOpInit : public OpInit {
1106 public:
1107   enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
1108 private:
1109   BinaryOp Opc;
1110   const Init *LHS, *RHS;
1111
1112   BinOpInit(const FoldingSetNodeID &ID, BinaryOp opc, const Init *lhs,
1113             const Init *rhs, RecTy *Type) :
1114       OpInit(ID, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
1115
1116   BinOpInit(const BinOpInit &Other);  // Do not define.
1117   BinOpInit &operator=(const BinOpInit &Other);  // Do not define.
1118
1119 public:
1120   static const BinOpInit *Create(BinaryOp opc, const Init *lhs, const Init *rhs,
1121                                  RecTy *Type);
1122
1123   // Clone - Clone this operator, replacing arguments with the new list
1124   virtual const OpInit *clone(std::vector<const Init *> &Operands) const {
1125     assert(Operands.size() == 2 &&
1126            "Wrong number of operands for binary operation");
1127     return BinOpInit::Create(getOpcode(), Operands[0], Operands[1], getType());
1128   }
1129
1130   int getNumOperands() const { return 2; }
1131   const Init *getOperand(int i) const {
1132     assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
1133     if (i == 0) {
1134       return getLHS();
1135     } else {
1136       return getRHS();
1137     }
1138   }
1139
1140   BinaryOp getOpcode() const { return Opc; }
1141   const Init *getLHS() const { return LHS; }
1142   const Init *getRHS() const { return RHS; }
1143
1144   // Fold - If possible, fold this to a simpler init.  Return this if not
1145   // possible to fold.
1146   const Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
1147
1148   virtual const Init *resolveReferences(Record &R,
1149                                         const RecordVal *RV) const;
1150
1151   virtual std::string getAsString() const;
1152 };
1153
1154 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
1155 ///
1156 class TernOpInit : public OpInit {
1157 public:
1158   enum TernaryOp { SUBST, FOREACH, IF };
1159 private:
1160   TernaryOp Opc;
1161   const Init *LHS, *MHS, *RHS;
1162
1163   TernOpInit(const FoldingSetNodeID &ID, TernaryOp opc, const Init *lhs,
1164              const Init *mhs, const Init *rhs, RecTy *Type) :
1165       OpInit(ID, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
1166
1167   TernOpInit(const TernOpInit &Other);  // Do not define.
1168   TernOpInit &operator=(const TernOpInit &Other);  // Do not define.
1169
1170 public:
1171   static const TernOpInit *Create(TernaryOp opc, const Init *lhs,
1172                                   const Init *mhs, const Init *rhs,
1173                                   RecTy *Type);
1174
1175   // Clone - Clone this operator, replacing arguments with the new list
1176   virtual const OpInit *clone(std::vector<const Init *> &Operands) const {
1177     assert(Operands.size() == 3 &&
1178            "Wrong number of operands for ternary operation");
1179     return TernOpInit::Create(getOpcode(), Operands[0], Operands[1],
1180                               Operands[2], getType());
1181   }
1182
1183   int getNumOperands() const { return 3; }
1184   const Init *getOperand(int i) const {
1185     assert((i == 0 || i == 1 || i == 2) &&
1186            "Invalid operand id for ternary operator");
1187     if (i == 0) {
1188       return getLHS();
1189     } else if (i == 1) {
1190       return getMHS();
1191     } else {
1192       return getRHS();
1193     }
1194   }
1195
1196   TernaryOp getOpcode() const { return Opc; }
1197   const Init *getLHS() const { return LHS; }
1198   const Init *getMHS() const { return MHS; }
1199   const Init *getRHS() const { return RHS; }
1200
1201   // Fold - If possible, fold this to a simpler init.  Return this if not
1202   // possible to fold.
1203   const Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
1204
1205   virtual bool isComplete() const { return false; }
1206
1207   virtual const Init *resolveReferences(Record &R,
1208                                         const RecordVal *RV) const;
1209
1210   virtual std::string getAsString() const;
1211 };
1212
1213
1214 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1215 ///
1216 class VarInit : public TypedInit {
1217   std::string VarName;
1218
1219   explicit VarInit(const FoldingSetNodeID &ID, const std::string &VN, RecTy *T)
1220       : TypedInit(ID, T), VarName(VN) {}
1221
1222   VarInit(const VarInit &Other);  // Do not define.
1223   VarInit &operator=(const VarInit &Other);  // Do not define.
1224
1225 public:
1226   static const VarInit *Create(const std::string &VN, RecTy *T);
1227   static const VarInit *Create(const Init *VN, RecTy *T);
1228
1229   virtual const Init *convertInitializerTo(RecTy *Ty) const {
1230     return Ty->convertValue(this);
1231   }
1232
1233   const std::string &getName() const { return VarName; }
1234
1235   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1236                                           unsigned Bit) const;
1237   virtual const Init *resolveListElementReference(Record &R,
1238                                                   const RecordVal *RV,
1239                                                   unsigned Elt) const;
1240
1241   virtual RecTy *getFieldType(const std::string &FieldName) const;
1242   virtual const Init *getFieldInit(Record &R, const RecordVal *RV,
1243                              const std::string &FieldName) const;
1244
1245   /// resolveReferences - This method is used by classes that refer to other
1246   /// variables which may not be defined at the time they expression is formed.
1247   /// If a value is set for the variable later, this method will be called on
1248   /// users of the value to allow the value to propagate out.
1249   ///
1250   virtual const Init *resolveReferences(Record &R,
1251                                         const RecordVal *RV) const;
1252
1253   virtual std::string getAsString() const { return VarName; }
1254 };
1255
1256
1257 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1258 ///
1259 class VarBitInit : public Init {
1260   const TypedInit *TI;
1261   unsigned Bit;
1262
1263   VarBitInit(const FoldingSetNodeID &ID, const TypedInit *T, unsigned B)
1264       : Init(ID), TI(T), Bit(B) {
1265     assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
1266            ((BitsRecTy*)T->getType())->getNumBits() > B &&
1267            "Illegal VarBitInit expression!");
1268   }
1269
1270   VarBitInit(const VarBitInit &Other);  // Do not define.
1271   VarBitInit &operator=(const VarBitInit &Other);  // Do not define.
1272
1273 public:
1274   static const VarBitInit *Create(const TypedInit *T, unsigned B);
1275
1276   virtual const Init *convertInitializerTo(RecTy *Ty) const {
1277     return Ty->convertValue(this);
1278   }
1279
1280   const TypedInit *getVariable() const { return TI; }
1281   unsigned getBitNum() const { return Bit; }
1282
1283   virtual std::string getAsString() const;
1284   virtual const Init *resolveReferences(Record &R,
1285                                         const RecordVal *RV) const;
1286 };
1287
1288 /// VarListElementInit - List[4] - Represent access to one element of a var or
1289 /// field.
1290 class VarListElementInit : public TypedInit {
1291   const TypedInit *TI;
1292   unsigned Element;
1293
1294   VarListElementInit(const FoldingSetNodeID &ID, const TypedInit *T, unsigned E)
1295       : TypedInit(ID, dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
1296                 TI(T), Element(E) {
1297     assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
1298            "Illegal VarBitInit expression!");
1299   }
1300
1301   VarListElementInit(const VarListElementInit &Other);  // Do not define.
1302   VarListElementInit &operator=(const VarListElementInit &Other);  // Do
1303                                                                    // not
1304                                                                    // define.
1305
1306 public:
1307   static const VarListElementInit *Create(const TypedInit *T, unsigned E);
1308
1309   virtual const Init *convertInitializerTo(RecTy *Ty) const {
1310     return Ty->convertValue(this);
1311   }
1312
1313   const TypedInit *getVariable() const { return TI; }
1314   unsigned getElementNum() const { return Element; }
1315
1316   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1317                                           unsigned Bit) const;
1318
1319   /// resolveListElementReference - This method is used to implement
1320   /// VarListElementInit::resolveReferences.  If the list element is resolvable
1321   /// now, we return the resolved value, otherwise we return null.
1322   virtual const Init *resolveListElementReference(Record &R, const RecordVal *RV,
1323                                             unsigned Elt) const;
1324
1325   virtual std::string getAsString() const;
1326   virtual const Init *resolveReferences(Record &R,
1327                                         const RecordVal *RV) const;
1328 };
1329
1330 /// DefInit - AL - Represent a reference to a 'def' in the description
1331 ///
1332 class DefInit : public TypedInit {
1333   Record *Def;
1334
1335   explicit DefInit(const FoldingSetNodeID &ID, Record *D)
1336       : TypedInit(ID, new RecordRecTy(D)), Def(D) {}
1337
1338   DefInit(const DefInit &Other);  // Do not define.
1339   DefInit &operator=(const DefInit &Other);  // Do not define.
1340
1341 public:
1342   static const DefInit *Create(Record *D);
1343
1344   virtual const Init *convertInitializerTo(RecTy *Ty) const {
1345     return Ty->convertValue(this);
1346   }
1347
1348   Record *getDef() const { return Def; }
1349
1350   //virtual const Init *
1351   //convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
1352
1353   virtual RecTy *getFieldType(const std::string &FieldName) const;
1354   virtual const Init *getFieldInit(Record &R, const RecordVal *RV,
1355                              const std::string &FieldName) const;
1356
1357   virtual std::string getAsString() const;
1358
1359   /// resolveBitReference - This method is used to implement
1360   /// VarBitInit::resolveReferences.  If the bit is able to be resolved, we
1361   /// simply return the resolved value, otherwise we return null.
1362   ///
1363   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1364                                           unsigned Bit) const {
1365     assert(0 && "Illegal bit reference off def");
1366     return 0;
1367   }
1368
1369   /// resolveListElementReference - This method is used to implement
1370   /// VarListElementInit::resolveReferences.  If the list element is resolvable
1371   /// now, we return the resolved value, otherwise we return null.
1372   virtual const Init *resolveListElementReference(Record &R,
1373                                                   const RecordVal *RV,
1374                                                   unsigned Elt) const {
1375     assert(0 && "Illegal element reference off def");
1376     return 0;
1377   }
1378 };
1379
1380
1381 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1382 ///
1383 class FieldInit : public TypedInit {
1384   const Init *Rec;                // Record we are referring to
1385   std::string FieldName;    // Field we are accessing
1386
1387   FieldInit(const FoldingSetNodeID &ID, const Init *R, const std::string &FN)
1388       : TypedInit(ID, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1389     assert(getType() && "FieldInit with non-record type!");
1390   }
1391
1392   FieldInit(const FieldInit &Other);  // Do not define.
1393   FieldInit &operator=(const FieldInit &Other);  // Do not define.
1394
1395 public:
1396   static const FieldInit *Create(const Init *R, const std::string &FN);
1397   static const FieldInit *Create(const Init *R, const Init *FN);
1398
1399   virtual const Init *convertInitializerTo(RecTy *Ty) const {
1400     return Ty->convertValue(this);
1401   }
1402
1403   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1404                                           unsigned Bit) const;
1405   virtual const Init *resolveListElementReference(Record &R,
1406                                                   const RecordVal *RV,
1407                                                   unsigned Elt) const;
1408
1409   virtual const Init *resolveReferences(Record &R,
1410                                         const RecordVal *RV) const;
1411
1412   virtual std::string getAsString() const {
1413     return Rec->getAsString() + "." + FieldName;
1414   }
1415 };
1416
1417 /// DagInit - (v a, b) - Represent a DAG tree value.  DAG inits are required
1418 /// to have at least one value then a (possibly empty) list of arguments.  Each
1419 /// argument can have a name associated with it.
1420 ///
1421 class DagInit : public TypedInit {
1422   const Init *Val;
1423   std::string ValName;
1424   std::vector<const Init*> Args;
1425   std::vector<std::string> ArgNames;
1426
1427   DagInit(const FoldingSetNodeID &ID, const Init *V, const std::string &VN,
1428           const std::vector<std::pair<const Init*, std::string> > &args)
1429       : TypedInit(ID, new DagRecTy), Val(V), ValName(VN) {
1430     Args.reserve(args.size());
1431     ArgNames.reserve(args.size());
1432     for (unsigned i = 0, e = args.size(); i != e; ++i) {
1433       Args.push_back(args[i].first);
1434       ArgNames.push_back(args[i].second);
1435     }
1436   }
1437   DagInit(const FoldingSetNodeID &ID, const Init *V, const std::string &VN,
1438           const std::vector<const Init*> &args,
1439           const std::vector<std::string> &argNames)
1440       : TypedInit(ID, new DagRecTy), Val(V), ValName(VN), Args(args),
1441       ArgNames(argNames) { }
1442
1443   DagInit(const DagInit &Other);  // Do not define.
1444   DagInit &operator=(const DagInit &Other);  // Do not define.
1445
1446 public:
1447   static const DagInit *Create(const Init *V, const std::string &VN,
1448                                const std::vector<
1449                                  std::pair<const Init*, std::string> > &args);
1450   static const DagInit *Create(const Init *V, const std::string &VN,
1451                                const std::vector<const Init*> &args,
1452                                const std::vector<std::string> &argNames);
1453
1454   virtual const Init *convertInitializerTo(RecTy *Ty) const {
1455     return Ty->convertValue(this);
1456   }
1457
1458   const Init *getOperator() const { return Val; }
1459
1460   const std::string &getName() const { return ValName; }
1461
1462   unsigned getNumArgs() const { return Args.size(); }
1463   const Init *getArg(unsigned Num) const {
1464     assert(Num < Args.size() && "Arg number out of range!");
1465     return Args[Num];
1466   }
1467   const std::string &getArgName(unsigned Num) const {
1468     assert(Num < ArgNames.size() && "Arg number out of range!");
1469     return ArgNames[Num];
1470   }
1471
1472   void setArg(unsigned Num, const Init *I) {
1473     assert(Num < Args.size() && "Arg number out of range!");
1474     Args[Num] = I;
1475   }
1476
1477   virtual const Init *resolveReferences(Record &R,
1478                                         const RecordVal *RV) const;
1479
1480   virtual std::string getAsString() const;
1481
1482   typedef std::vector<const Init*>::iterator             arg_iterator;
1483   typedef std::vector<const Init*>::const_iterator       const_arg_iterator;
1484   typedef std::vector<std::string>::iterator       name_iterator;
1485   typedef std::vector<std::string>::const_iterator const_name_iterator;
1486
1487   inline arg_iterator        arg_begin()       { return Args.begin(); }
1488   inline const_arg_iterator  arg_begin() const { return Args.begin(); }
1489   inline arg_iterator        arg_end  ()       { return Args.end();   }
1490   inline const_arg_iterator  arg_end  () const { return Args.end();   }
1491
1492   inline size_t              arg_size () const { return Args.size();  }
1493   inline bool                arg_empty() const { return Args.empty(); }
1494
1495   inline name_iterator       name_begin()       { return ArgNames.begin(); }
1496   inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1497   inline name_iterator       name_end  ()       { return ArgNames.end();   }
1498   inline const_name_iterator name_end  () const { return ArgNames.end();   }
1499
1500   inline size_t              name_size () const { return ArgNames.size();  }
1501   inline bool                name_empty() const { return ArgNames.empty(); }
1502
1503   virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1504                                           unsigned Bit) const {
1505     assert(0 && "Illegal bit reference off dag");
1506     return 0;
1507   }
1508
1509   virtual const Init *resolveListElementReference(Record &R,
1510                                                   const RecordVal *RV,
1511                                                   unsigned Elt) const {
1512     assert(0 && "Illegal element reference off dag");
1513     return 0;
1514   }
1515 };
1516
1517 //===----------------------------------------------------------------------===//
1518 //  High-Level Classes
1519 //===----------------------------------------------------------------------===//
1520
1521 class RecordVal {
1522   std::string Name;
1523   RecTy *Ty;
1524   unsigned Prefix;
1525   const Init *Value;
1526 public:
1527   RecordVal(const std::string &N, RecTy *T, unsigned P);
1528
1529   const std::string &getName() const { return Name; }
1530
1531   unsigned getPrefix() const { return Prefix; }
1532   RecTy *getType() const { return Ty; }
1533   const Init *getValue() const { return Value; }
1534
1535   bool setValue(const Init *V) {
1536     if (V) {
1537       Value = V->convertInitializerTo(Ty);
1538       return Value == 0;
1539     }
1540     Value = 0;
1541     return false;
1542   }
1543
1544   void dump() const;
1545   void print(raw_ostream &OS, bool PrintSem = true) const;
1546 };
1547
1548 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1549   RV.print(OS << "  ");
1550   return OS;
1551 }
1552
1553 class Record {
1554   static unsigned LastID;
1555
1556   // Unique record ID.
1557   unsigned ID;
1558   std::string Name;
1559   SMLoc Loc;
1560   std::vector<std::string> TemplateArgs;
1561   std::vector<RecordVal> Values;
1562   std::vector<Record*> SuperClasses;
1563
1564   // Tracks Record instances. Not owned by Record.
1565   RecordKeeper &TrackedRecords;
1566
1567 public:
1568
1569   // Constructs a record.
1570   explicit Record(const std::string &N, SMLoc loc, RecordKeeper &records) :
1571     ID(LastID++), Name(N), Loc(loc), TrackedRecords(records) {}
1572   ~Record() {}
1573
1574
1575   static unsigned getNewUID() { return LastID++; }
1576
1577
1578   unsigned getID() const { return ID; }
1579
1580   const std::string &getName() const { return Name; }
1581   void setName(const std::string &Name);  // Also updates RecordKeeper.
1582
1583   SMLoc getLoc() const { return Loc; }
1584
1585   const std::vector<std::string> &getTemplateArgs() const {
1586     return TemplateArgs;
1587   }
1588   const std::vector<RecordVal> &getValues() const { return Values; }
1589   const std::vector<Record*>   &getSuperClasses() const { return SuperClasses; }
1590
1591   bool isTemplateArg(StringRef Name) const {
1592     for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1593       if (TemplateArgs[i] == Name) return true;
1594     return false;
1595   }
1596
1597   const RecordVal *getValue(StringRef Name) const {
1598     for (unsigned i = 0, e = Values.size(); i != e; ++i)
1599       if (Values[i].getName() == Name) return &Values[i];
1600     return 0;
1601   }
1602   RecordVal *getValue(StringRef Name) {
1603     for (unsigned i = 0, e = Values.size(); i != e; ++i)
1604       if (Values[i].getName() == Name) return &Values[i];
1605     return 0;
1606   }
1607
1608   void addTemplateArg(StringRef Name) {
1609     assert(!isTemplateArg(Name) && "Template arg already defined!");
1610     TemplateArgs.push_back(Name);
1611   }
1612
1613   void addValue(const RecordVal &RV) {
1614     assert(getValue(RV.getName()) == 0 && "Value already added!");
1615     Values.push_back(RV);
1616   }
1617
1618   void removeValue(StringRef Name) {
1619     for (unsigned i = 0, e = Values.size(); i != e; ++i)
1620       if (Values[i].getName() == Name) {
1621         Values.erase(Values.begin()+i);
1622         return;
1623       }
1624     assert(0 && "Cannot remove an entry that does not exist!");
1625   }
1626
1627   bool isSubClassOf(const Record *R) const {
1628     for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1629       if (SuperClasses[i] == R)
1630         return true;
1631     return false;
1632   }
1633
1634   bool isSubClassOf(StringRef Name) const {
1635     for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1636       if (SuperClasses[i]->getName() == Name)
1637         return true;
1638     return false;
1639   }
1640
1641   void addSuperClass(Record *R) {
1642     assert(!isSubClassOf(R) && "Already subclassing record!");
1643     SuperClasses.push_back(R);
1644   }
1645
1646   /// resolveReferences - If there are any field references that refer to fields
1647   /// that have been filled in, we can propagate the values now.
1648   ///
1649   void resolveReferences() { resolveReferencesTo(0); }
1650
1651   /// resolveReferencesTo - If anything in this record refers to RV, replace the
1652   /// reference to RV with the RHS of RV.  If RV is null, we resolve all
1653   /// possible references.
1654   void resolveReferencesTo(const RecordVal *RV);
1655
1656   RecordKeeper &getRecords() const {
1657     return TrackedRecords;
1658   }
1659
1660   void dump() const;
1661
1662   //===--------------------------------------------------------------------===//
1663   // High-level methods useful to tablegen back-ends
1664   //
1665
1666   /// getValueInit - Return the initializer for a value with the specified name,
1667   /// or throw an exception if the field does not exist.
1668   ///
1669   const Init *getValueInit(StringRef FieldName) const;
1670
1671   /// getValueAsString - This method looks up the specified field and returns
1672   /// its value as a string, throwing an exception if the field does not exist
1673   /// or if the value is not a string.
1674   ///
1675   std::string getValueAsString(StringRef FieldName) const;
1676
1677   /// getValueAsBitsInit - This method looks up the specified field and returns
1678   /// its value as a BitsInit, throwing an exception if the field does not exist
1679   /// or if the value is not the right type.
1680   ///
1681   const BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1682
1683   /// getValueAsListInit - This method looks up the specified field and returns
1684   /// its value as a ListInit, throwing an exception if the field does not exist
1685   /// or if the value is not the right type.
1686   ///
1687   const ListInit *getValueAsListInit(StringRef FieldName) const;
1688
1689   /// getValueAsListOfDefs - This method looks up the specified field and
1690   /// returns its value as a vector of records, throwing an exception if the
1691   /// field does not exist or if the value is not the right type.
1692   ///
1693   std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1694
1695   /// getValueAsListOfInts - This method looks up the specified field and
1696   /// returns its value as a vector of integers, throwing an exception if the
1697   /// field does not exist or if the value is not the right type.
1698   ///
1699   std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1700
1701   /// getValueAsListOfStrings - This method looks up the specified field and
1702   /// returns its value as a vector of strings, throwing an exception if the
1703   /// field does not exist or if the value is not the right type.
1704   ///
1705   std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1706
1707   /// getValueAsDef - This method looks up the specified field and returns its
1708   /// value as a Record, throwing an exception if the field does not exist or if
1709   /// the value is not the right type.
1710   ///
1711   Record *getValueAsDef(StringRef FieldName) const;
1712
1713   /// getValueAsBit - This method looks up the specified field and returns its
1714   /// value as a bit, throwing an exception if the field does not exist or if
1715   /// the value is not the right type.
1716   ///
1717   bool getValueAsBit(StringRef FieldName) const;
1718
1719   /// getValueAsInt - This method looks up the specified field and returns its
1720   /// value as an int64_t, throwing an exception if the field does not exist or
1721   /// if the value is not the right type.
1722   ///
1723   int64_t getValueAsInt(StringRef FieldName) const;
1724
1725   /// getValueAsDag - This method looks up the specified field and returns its
1726   /// value as an Dag, throwing an exception if the field does not exist or if
1727   /// the value is not the right type.
1728   ///
1729   const DagInit *getValueAsDag(StringRef FieldName) const;
1730
1731   /// getValueAsCode - This method looks up the specified field and returns
1732   /// its value as the string data in a CodeInit, throwing an exception if the
1733   /// field does not exist or if the value is not a code object.
1734   ///
1735   std::string getValueAsCode(StringRef FieldName) const;
1736 };
1737
1738 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1739
1740 struct MultiClass {
1741   Record Rec;  // Placeholder for template args and Name.
1742   typedef std::vector<Record*> RecordVector;
1743   RecordVector DefPrototypes;
1744
1745   void dump() const;
1746
1747   MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) : 
1748     Rec(Name, Loc, Records) {}
1749 };
1750
1751 class RecordKeeper {
1752   std::map<std::string, Record*> Classes, Defs;
1753 public:
1754   ~RecordKeeper() {
1755     for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1756            E = Classes.end(); I != E; ++I)
1757       delete I->second;
1758     for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1759            E = Defs.end(); I != E; ++I)
1760       delete I->second;
1761   }
1762
1763   const std::map<std::string, Record*> &getClasses() const { return Classes; }
1764   const std::map<std::string, Record*> &getDefs() const { return Defs; }
1765
1766   Record *getClass(const std::string &Name) const {
1767     std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1768     return I == Classes.end() ? 0 : I->second;
1769   }
1770   Record *getDef(const std::string &Name) const {
1771     std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1772     return I == Defs.end() ? 0 : I->second;
1773   }
1774   void addClass(Record *R) {
1775     assert(getClass(R->getName()) == 0 && "Class already exists!");
1776     Classes.insert(std::make_pair(R->getName(), R));
1777   }
1778   void addDef(Record *R) {
1779     assert(getDef(R->getName()) == 0 && "Def already exists!");
1780     Defs.insert(std::make_pair(R->getName(), R));
1781   }
1782
1783   /// removeClass - Remove, but do not delete, the specified record.
1784   ///
1785   void removeClass(const std::string &Name) {
1786     assert(Classes.count(Name) && "Class does not exist!");
1787     Classes.erase(Name);
1788   }
1789   /// removeDef - Remove, but do not delete, the specified record.
1790   ///
1791   void removeDef(const std::string &Name) {
1792     assert(Defs.count(Name) && "Def does not exist!");
1793     Defs.erase(Name);
1794   }
1795
1796   //===--------------------------------------------------------------------===//
1797   // High-level helper methods, useful for tablegen backends...
1798
1799   /// getAllDerivedDefinitions - This method returns all concrete definitions
1800   /// that derive from the specified class name.  If a class with the specified
1801   /// name does not exist, an exception is thrown.
1802   std::vector<Record*>
1803   getAllDerivedDefinitions(const std::string &ClassName) const;
1804
1805   void dump() const;
1806 };
1807
1808 /// LessRecord - Sorting predicate to sort record pointers by name.
1809 ///
1810 struct LessRecord {
1811   bool operator()(const Record *Rec1, const Record *Rec2) const {
1812     return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1813   }
1814 };
1815
1816 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1817 /// name field.
1818 ///
1819 struct LessRecordFieldName {
1820   bool operator()(const Record *Rec1, const Record *Rec2) const {
1821     return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1822   }
1823 };
1824
1825 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1826
1827 } // End llvm namespace
1828
1829 #endif