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