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