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(nullptr) {}
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 nullptr; }
106  virtual Init *convertValue(   BitInit *BI) { return nullptr; }
107  virtual Init *convertValue(  BitsInit *BI) { return nullptr; }
108  virtual Init *convertValue(   IntInit *II) { return nullptr; }
109  virtual Init *convertValue(StringInit *SI) { return nullptr; }
110  virtual Init *convertValue(  ListInit *LI) { return nullptr; }
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 nullptr; }
121  virtual Init *convertValue(   DefInit *DI) { return nullptr; }
122  virtual Init *convertValue(   DagInit *DI) { return nullptr; }
123  virtual Init *convertValue( TypedInit *TI) { return nullptr; }
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/// BitRecTy - 'bit' - Represent a single bit
141///
142class BitRecTy : public RecTy {
143  static BitRecTy Shared;
144  BitRecTy() : RecTy(BitRecTyKind) {}
145
146public:
147  static bool classof(const RecTy *RT) {
148    return RT->getRecTyKind() == BitRecTyKind;
149  }
150
151  static BitRecTy *get() { return &Shared; }
152
153  Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
154  Init *convertValue(   BitInit *BI) override { return (Init*)BI; }
155  Init *convertValue(  BitsInit *BI) override;
156  Init *convertValue(   IntInit *II) override;
157  Init *convertValue(StringInit *SI) override { return nullptr; }
158  Init *convertValue(  ListInit *LI) override { return nullptr; }
159  Init *convertValue(VarBitInit *VB) override { return (Init*)VB; }
160  Init *convertValue(   DefInit *DI) override { return nullptr; }
161  Init *convertValue(   DagInit *DI) override { return nullptr; }
162  Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
163  Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
164  Init *convertValue( TernOpInit *UI) override {return RecTy::convertValue(UI);}
165  Init *convertValue( TypedInit *TI) override;
166  Init *convertValue(   VarInit *VI) override { return RecTy::convertValue(VI);}
167  Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
168
169  std::string getAsString() const override { return "bit"; }
170
171  bool typeIsConvertibleTo(const RecTy *RHS) const override {
172    return RHS->baseClassOf(this);
173  }
174  bool baseClassOf(const RecTy*) const override;
175};
176
177/// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
178///
179class BitsRecTy : public RecTy {
180  unsigned Size;
181  explicit BitsRecTy(unsigned Sz) : RecTy(BitsRecTyKind), Size(Sz) {}
182
183public:
184  static bool classof(const RecTy *RT) {
185    return RT->getRecTyKind() == BitsRecTyKind;
186  }
187
188  static BitsRecTy *get(unsigned Sz);
189
190  unsigned getNumBits() const { return Size; }
191
192  Init *convertValue( UnsetInit *UI) override;
193  Init *convertValue(   BitInit *UI) override;
194  Init *convertValue(  BitsInit *BI) override;
195  Init *convertValue(   IntInit *II) override;
196  Init *convertValue(StringInit *SI) override { return nullptr; }
197  Init *convertValue(  ListInit *LI) override { return nullptr; }
198  Init *convertValue(VarBitInit *VB) override { return nullptr; }
199  Init *convertValue(   DefInit *DI) override { return nullptr; }
200  Init *convertValue(   DagInit *DI) override { return nullptr; }
201  Init *convertValue(  UnOpInit *UI) override { return RecTy::convertValue(UI);}
202  Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
203  Init *convertValue(TernOpInit *UI) override { return RecTy::convertValue(UI);}
204  Init *convertValue( TypedInit *TI) override;
205  Init *convertValue(   VarInit *VI) override { return RecTy::convertValue(VI);}
206  Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
207
208  std::string getAsString() const override;
209
210  bool typeIsConvertibleTo(const RecTy *RHS) const override {
211    return RHS->baseClassOf(this);
212  }
213  bool baseClassOf(const RecTy*) const override;
214};
215
216/// IntRecTy - 'int' - Represent an integer value of no particular size
217///
218class IntRecTy : public RecTy {
219  static IntRecTy Shared;
220  IntRecTy() : RecTy(IntRecTyKind) {}
221
222public:
223  static bool classof(const RecTy *RT) {
224    return RT->getRecTyKind() == IntRecTyKind;
225  }
226
227  static IntRecTy *get() { return &Shared; }
228
229  Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
230  Init *convertValue(   BitInit *BI) override;
231  Init *convertValue(  BitsInit *BI) override;
232  Init *convertValue(   IntInit *II) override { return (Init*)II; }
233  Init *convertValue(StringInit *SI) override { return nullptr; }
234  Init *convertValue(  ListInit *LI) override { return nullptr; }
235  Init *convertValue(VarBitInit *VB) override { return nullptr; }
236  Init *convertValue(   DefInit *DI) override { return nullptr; }
237  Init *convertValue(   DagInit *DI) override { return nullptr; }
238  Init *convertValue( UnOpInit *UI)  override { return RecTy::convertValue(UI);}
239  Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
240  Init *convertValue( TernOpInit *UI) override {return RecTy::convertValue(UI);}
241  Init *convertValue( TypedInit *TI) override;
242  Init *convertValue(   VarInit *VI) override { return RecTy::convertValue(VI);}
243  Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
244
245  std::string getAsString() const override { return "int"; }
246
247  bool typeIsConvertibleTo(const RecTy *RHS) const override {
248    return RHS->baseClassOf(this);
249  }
250
251  bool baseClassOf(const RecTy*) const override;
252};
253
254/// StringRecTy - 'string' - Represent an string value
255///
256class StringRecTy : public RecTy {
257  static StringRecTy Shared;
258  StringRecTy() : RecTy(StringRecTyKind) {}
259
260public:
261  static bool classof(const RecTy *RT) {
262    return RT->getRecTyKind() == StringRecTyKind;
263  }
264
265  static StringRecTy *get() { return &Shared; }
266
267  Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
268  Init *convertValue(   BitInit *BI) override { return nullptr; }
269  Init *convertValue(  BitsInit *BI) override { return nullptr; }
270  Init *convertValue(   IntInit *II) override { return nullptr; }
271  Init *convertValue(StringInit *SI) override { return (Init*)SI; }
272  Init *convertValue(  ListInit *LI) override { return nullptr; }
273  Init *convertValue( UnOpInit *BO) override;
274  Init *convertValue( BinOpInit *BO) override;
275  Init *convertValue( TernOpInit *BO) override {return RecTy::convertValue(BO);}
276
277  Init *convertValue(VarBitInit *VB) override { return nullptr; }
278  Init *convertValue(   DefInit *DI) override { return nullptr; }
279  Init *convertValue(   DagInit *DI) override { return nullptr; }
280  Init *convertValue( TypedInit *TI) override;
281  Init *convertValue(   VarInit *VI) override { return RecTy::convertValue(VI);}
282  Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
283
284  std::string getAsString() const override { return "string"; }
285
286  bool typeIsConvertibleTo(const RecTy *RHS) const override {
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///
294class ListRecTy : public RecTy {
295  RecTy *Ty;
296  explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), Ty(T) {}
297  friend ListRecTy *RecTy::getListTy();
298
299public:
300  static bool classof(const RecTy *RT) {
301    return RT->getRecTyKind() == ListRecTyKind;
302  }
303
304  static ListRecTy *get(RecTy *T) { return T->getListTy(); }
305  RecTy *getElementType() const { return Ty; }
306
307  Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
308  Init *convertValue(   BitInit *BI) override { return nullptr; }
309  Init *convertValue(  BitsInit *BI) override { return nullptr; }
310  Init *convertValue(   IntInit *II) override { return nullptr; }
311  Init *convertValue(StringInit *SI) override { return nullptr; }
312  Init *convertValue(  ListInit *LI) override;
313  Init *convertValue(VarBitInit *VB) override { return nullptr; }
314  Init *convertValue(   DefInit *DI) override { return nullptr; }
315  Init *convertValue(   DagInit *DI) override { return nullptr; }
316  Init *convertValue(  UnOpInit *UI) override { return RecTy::convertValue(UI);}
317  Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
318  Init *convertValue(TernOpInit *UI) override { return RecTy::convertValue(UI);}
319  Init *convertValue( TypedInit *TI) override;
320  Init *convertValue(   VarInit *VI) override { return RecTy::convertValue(VI);}
321  Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
322
323  std::string getAsString() const override;
324
325  bool typeIsConvertibleTo(const RecTy *RHS) const override {
326    return RHS->baseClassOf(this);
327  }
328
329  bool baseClassOf(const RecTy*) const override;
330};
331
332/// DagRecTy - 'dag' - Represent a dag fragment
333///
334class DagRecTy : public RecTy {
335  static DagRecTy Shared;
336  DagRecTy() : RecTy(DagRecTyKind) {}
337
338public:
339  static bool classof(const RecTy *RT) {
340    return RT->getRecTyKind() == DagRecTyKind;
341  }
342
343  static DagRecTy *get() { return &Shared; }
344
345  Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
346  Init *convertValue(   BitInit *BI) override { return nullptr; }
347  Init *convertValue(  BitsInit *BI) override { return nullptr; }
348  Init *convertValue(   IntInit *II) override { return nullptr; }
349  Init *convertValue(StringInit *SI) override { return nullptr; }
350  Init *convertValue(  ListInit *LI) override { return nullptr; }
351  Init *convertValue(VarBitInit *VB) override { return nullptr; }
352  Init *convertValue(   DefInit *DI) override { return nullptr; }
353  Init *convertValue( UnOpInit *BO) override;
354  Init *convertValue( BinOpInit *BO) override;
355  Init *convertValue( TernOpInit *BO) override {return RecTy::convertValue(BO);}
356  Init *convertValue(   DagInit *CI) override { return (Init*)CI; }
357  Init *convertValue( TypedInit *TI) override;
358  Init *convertValue(   VarInit *VI) override { return RecTy::convertValue(VI);}
359  Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
360
361  std::string getAsString() const override { return "dag"; }
362
363  bool typeIsConvertibleTo(const RecTy *RHS) const override {
364    return RHS->baseClassOf(this);
365  }
366};
367
368/// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
369/// (R32 X = EAX).
370///
371class RecordRecTy : public RecTy {
372  Record *Rec;
373  explicit RecordRecTy(Record *R) : RecTy(RecordRecTyKind), Rec(R) {}
374  friend class Record;
375
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  Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
386  Init *convertValue(   BitInit *BI) override { return nullptr; }
387  Init *convertValue(  BitsInit *BI) override { return nullptr; }
388  Init *convertValue(   IntInit *II) override { return nullptr; }
389  Init *convertValue(StringInit *SI) override { return nullptr; }
390  Init *convertValue(  ListInit *LI) override { return nullptr; }
391  Init *convertValue(VarBitInit *VB) override { return nullptr; }
392  Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
393  Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
394  Init *convertValue( TernOpInit *UI) override {return RecTy::convertValue(UI);}
395  Init *convertValue(   DefInit *DI) override;
396  Init *convertValue(   DagInit *DI) override { return nullptr; }
397  Init *convertValue( TypedInit *VI) override;
398  Init *convertValue(   VarInit *VI) override { return RecTy::convertValue(VI);}
399  Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
400
401  std::string getAsString() const override;
402
403  bool typeIsConvertibleTo(const RecTy *RHS) const override {
404    return RHS->baseClassOf(this);
405  }
406  bool baseClassOf(const RecTy*) const override;
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 nullptr;
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 nullptr;
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 {
520    return nullptr;
521  }
522
523  /// getFieldInit - This method complements getFieldType to return the
524  /// initializer for the specified field.  If getFieldType returns non-null
525  /// this method should return non-null, otherwise it returns null.
526  ///
527  virtual Init *getFieldInit(Record &R, const RecordVal *RV,
528                             const std::string &FieldName) const {
529    return nullptr;
530  }
531
532  /// resolveReferences - This method is used by classes that refer to other
533  /// variables which may not be defined at the time the expression is formed.
534  /// If a value is set for the variable later, this method will be called on
535  /// users of the value to allow the value to propagate out.
536  ///
537  virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
538    return const_cast<Init *>(this);
539  }
540
541  /// getBit - This method is used to return the initializer for the specified
542  /// bit.
543  virtual Init *getBit(unsigned Bit) const = 0;
544
545  /// getBitVar - This method is used to retrieve the initializer for bit
546  /// reference. For non-VarBitInit, it simply returns itself.
547  virtual Init *getBitVar() const { return const_cast<Init*>(this); }
548
549  /// getBitNum - This method is used to retrieve the bit number of a bit
550  /// reference. For non-VarBitInit, it simply returns 0.
551  virtual unsigned getBitNum() const { return 0; }
552};
553
554inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
555  I.print(OS); return OS;
556}
557
558/// TypedInit - This is the common super-class of types that have a specific,
559/// explicit, type.
560///
561class TypedInit : public Init {
562  RecTy *Ty;
563
564  TypedInit(const TypedInit &Other) LLVM_DELETED_FUNCTION;
565  TypedInit &operator=(const TypedInit &Other) LLVM_DELETED_FUNCTION;
566
567protected:
568  explicit TypedInit(InitKind K, RecTy *T) : Init(K), Ty(T) {}
569
570public:
571  static bool classof(const Init *I) {
572    return I->getKind() >= IK_FirstTypedInit &&
573           I->getKind() <= IK_LastTypedInit;
574  }
575  RecTy *getType() const { return Ty; }
576
577  Init *
578  convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
579  Init *
580  convertInitListSlice(const std::vector<unsigned> &Elements) const override;
581
582  /// getFieldType - This method is used to implement the FieldInit class.
583  /// Implementors of this method should return the type of the named field if
584  /// they are of record type.
585  ///
586  RecTy *getFieldType(const std::string &FieldName) const override;
587
588  /// resolveListElementReference - This method is used to implement
589  /// VarListElementInit::resolveReferences.  If the list element is resolvable
590  /// now, we return the resolved value, otherwise we return null.
591  virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
592                                            unsigned Elt) const = 0;
593};
594
595/// UnsetInit - ? - Represents an uninitialized value
596///
597class UnsetInit : public Init {
598  UnsetInit() : Init(IK_UnsetInit) {}
599  UnsetInit(const UnsetInit &) LLVM_DELETED_FUNCTION;
600  UnsetInit &operator=(const UnsetInit &Other) LLVM_DELETED_FUNCTION;
601  void anchor() override;
602
603public:
604  static bool classof(const Init *I) {
605    return I->getKind() == IK_UnsetInit;
606  }
607  static UnsetInit *get();
608
609  Init *convertInitializerTo(RecTy *Ty) const override {
610    return Ty->convertValue(const_cast<UnsetInit *>(this));
611  }
612
613  Init *getBit(unsigned Bit) const override {
614    return const_cast<UnsetInit*>(this);
615  }
616
617  bool isComplete() const override { return false; }
618  std::string getAsString() const override { return "?"; }
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  void anchor() override;
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  Init *convertInitializerTo(RecTy *Ty) const override {
640    return Ty->convertValue(const_cast<BitInit *>(this));
641  }
642
643  Init *getBit(unsigned Bit) const override {
644    assert(Bit < 1 && "Bit index out of range!");
645    return const_cast<BitInit*>(this);
646  }
647
648  std::string getAsString() const override { 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  Init *convertInitializerTo(RecTy *Ty) const override {
674    return Ty->convertValue(const_cast<BitsInit *>(this));
675  }
676  Init *
677  convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
678
679  bool isComplete() const override {
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  std::string getAsString() const override;
690
691  Init *resolveReferences(Record &R, const RecordVal *RV) const override;
692
693  Init *getBit(unsigned Bit) const override {
694    assert(Bit < Bits.size() && "Bit index out of range!");
695    return Bits[Bit];
696  }
697};
698
699/// IntInit - 7 - Represent an initialization by a literal integer value.
700///
701class IntInit : public TypedInit {
702  int64_t Value;
703
704  explicit IntInit(int64_t V)
705    : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
706
707  IntInit(const IntInit &Other) LLVM_DELETED_FUNCTION;
708  IntInit &operator=(const IntInit &Other) LLVM_DELETED_FUNCTION;
709
710public:
711  static bool classof(const Init *I) {
712    return I->getKind() == IK_IntInit;
713  }
714  static IntInit *get(int64_t V);
715
716  int64_t getValue() const { return Value; }
717
718  Init *convertInitializerTo(RecTy *Ty) const override {
719    return Ty->convertValue(const_cast<IntInit *>(this));
720  }
721  Init *
722  convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
723
724  std::string getAsString() const override;
725
726  /// resolveListElementReference - This method is used to implement
727  /// VarListElementInit::resolveReferences.  If the list element is resolvable
728  /// now, we return the resolved value, otherwise we return null.
729  Init *resolveListElementReference(Record &R, const RecordVal *RV,
730                                    unsigned Elt) const override {
731    llvm_unreachable("Illegal element reference off int");
732  }
733
734  Init *getBit(unsigned Bit) const override {
735    return BitInit::get((Value & (1ULL << Bit)) != 0);
736  }
737};
738
739/// StringInit - "foo" - Represent an initialization by a string value.
740///
741class StringInit : public TypedInit {
742  std::string Value;
743
744  explicit StringInit(const std::string &V)
745    : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
746
747  StringInit(const StringInit &Other) LLVM_DELETED_FUNCTION;
748  StringInit &operator=(const StringInit &Other) LLVM_DELETED_FUNCTION;
749  void anchor() override;
750
751public:
752  static bool classof(const Init *I) {
753    return I->getKind() == IK_StringInit;
754  }
755  static StringInit *get(StringRef);
756
757  const std::string &getValue() const { return Value; }
758
759  Init *convertInitializerTo(RecTy *Ty) const override {
760    return Ty->convertValue(const_cast<StringInit *>(this));
761  }
762
763  std::string getAsString() const override { return "\"" + Value + "\""; }
764  std::string getAsUnquotedString() const override { return Value; }
765
766  /// resolveListElementReference - This method is used to implement
767  /// VarListElementInit::resolveReferences.  If the list element is resolvable
768  /// now, we return the resolved value, otherwise we return null.
769  Init *resolveListElementReference(Record &R, const RecordVal *RV,
770                                    unsigned Elt) const override {
771    llvm_unreachable("Illegal element reference off string");
772  }
773
774  Init *getBit(unsigned Bit) const override {
775    llvm_unreachable("Illegal bit reference off string");
776  }
777};
778
779/// ListInit - [AL, AH, CL] - Represent a list of defs
780///
781class ListInit : public TypedInit, public FoldingSetNode {
782  std::vector<Init*> Values;
783
784public:
785  typedef std::vector<Init*>::const_iterator const_iterator;
786
787private:
788  explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
789    : TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
790      Values(Range.begin(), Range.end()) {}
791
792  ListInit(const ListInit &Other) LLVM_DELETED_FUNCTION;
793  ListInit &operator=(const ListInit &Other) LLVM_DELETED_FUNCTION;
794
795public:
796  static bool classof(const Init *I) {
797    return I->getKind() == IK_ListInit;
798  }
799  static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
800
801  void Profile(FoldingSetNodeID &ID) const;
802
803  unsigned getSize() const { return Values.size(); }
804  Init *getElement(unsigned i) const {
805    assert(i < Values.size() && "List element index out of range!");
806    return Values[i];
807  }
808
809  Record *getElementAsRecord(unsigned i) const;
810
811  Init *
812    convertInitListSlice(const std::vector<unsigned> &Elements) const override;
813
814  Init *convertInitializerTo(RecTy *Ty) const override {
815    return Ty->convertValue(const_cast<ListInit *>(this));
816  }
817
818  /// resolveReferences - This method is used by classes that refer to other
819  /// variables which may not be defined at the time they expression is formed.
820  /// If a value is set for the variable later, this method will be called on
821  /// users of the value to allow the value to propagate out.
822  ///
823  Init *resolveReferences(Record &R, const RecordVal *RV) const override;
824
825  std::string getAsString() const override;
826
827  ArrayRef<Init*> getValues() const { return Values; }
828
829  inline const_iterator begin() const { return Values.begin(); }
830  inline const_iterator end  () const { return Values.end();   }
831
832  inline size_t         size () const { return Values.size();  }
833  inline bool           empty() const { return Values.empty(); }
834
835  /// resolveListElementReference - This method is used to implement
836  /// VarListElementInit::resolveReferences.  If the list element is resolvable
837  /// now, we return the resolved value, otherwise we return null.
838  Init *resolveListElementReference(Record &R, const RecordVal *RV,
839                                    unsigned Elt) const override;
840
841  Init *getBit(unsigned Bit) const override {
842    llvm_unreachable("Illegal bit reference off list");
843  }
844};
845
846/// OpInit - Base class for operators
847///
848class OpInit : public TypedInit {
849  OpInit(const OpInit &Other) LLVM_DELETED_FUNCTION;
850  OpInit &operator=(OpInit &Other) LLVM_DELETED_FUNCTION;
851
852protected:
853  explicit OpInit(InitKind K, RecTy *Type) : TypedInit(K, Type) {}
854
855public:
856  static bool classof(const Init *I) {
857    return I->getKind() >= IK_FirstOpInit &&
858           I->getKind() <= IK_LastOpInit;
859  }
860  // Clone - Clone this operator, replacing arguments with the new list
861  virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
862
863  virtual int getNumOperands() const = 0;
864  virtual Init *getOperand(int i) const = 0;
865
866  // Fold - If possible, fold this to a simpler init.  Return this if not
867  // possible to fold.
868  virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
869
870  Init *convertInitializerTo(RecTy *Ty) const override {
871    return Ty->convertValue(const_cast<OpInit *>(this));
872  }
873
874  Init *resolveListElementReference(Record &R, const RecordVal *RV,
875                                    unsigned Elt) const override;
876
877  Init *getBit(unsigned Bit) const override;
878};
879
880/// UnOpInit - !op (X) - Transform an init.
881///
882class UnOpInit : public OpInit {
883public:
884  enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
885
886private:
887  UnaryOp Opc;
888  Init *LHS;
889
890  UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
891    : OpInit(IK_UnOpInit, Type), Opc(opc), LHS(lhs) {}
892
893  UnOpInit(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
894  UnOpInit &operator=(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
895
896public:
897  static bool classof(const Init *I) {
898    return I->getKind() == IK_UnOpInit;
899  }
900  static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
901
902  // Clone - Clone this operator, replacing arguments with the new list
903  OpInit *clone(std::vector<Init *> &Operands) const override {
904    assert(Operands.size() == 1 &&
905           "Wrong number of operands for unary operation");
906    return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
907  }
908
909  int getNumOperands() const override { return 1; }
910  Init *getOperand(int i) const override {
911    assert(i == 0 && "Invalid operand id for unary operator");
912    return getOperand();
913  }
914
915  UnaryOp getOpcode() const { return Opc; }
916  Init *getOperand() const { return LHS; }
917
918  // Fold - If possible, fold this to a simpler init.  Return this if not
919  // possible to fold.
920  Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
921
922  Init *resolveReferences(Record &R, const RecordVal *RV) const override;
923
924  std::string getAsString() const override;
925};
926
927/// BinOpInit - !op (X, Y) - Combine two inits.
928///
929class BinOpInit : public OpInit {
930public:
931  enum BinaryOp { ADD, SHL, SRA, SRL, LISTCONCAT, STRCONCAT, CONCAT, EQ };
932
933private:
934  BinaryOp Opc;
935  Init *LHS, *RHS;
936
937  BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
938      OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
939
940  BinOpInit(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
941  BinOpInit &operator=(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
942
943public:
944  static bool classof(const Init *I) {
945    return I->getKind() == IK_BinOpInit;
946  }
947  static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
948                        RecTy *Type);
949
950  // Clone - Clone this operator, replacing arguments with the new list
951  OpInit *clone(std::vector<Init *> &Operands) const override {
952    assert(Operands.size() == 2 &&
953           "Wrong number of operands for binary operation");
954    return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
955  }
956
957  int getNumOperands() const override { return 2; }
958  Init *getOperand(int i) const override {
959    assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
960    if (i == 0) {
961      return getLHS();
962    } else {
963      return getRHS();
964    }
965  }
966
967  BinaryOp getOpcode() const { return Opc; }
968  Init *getLHS() const { return LHS; }
969  Init *getRHS() const { return RHS; }
970
971  // Fold - If possible, fold this to a simpler init.  Return this if not
972  // possible to fold.
973  Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
974
975  Init *resolveReferences(Record &R, const RecordVal *RV) const override;
976
977  std::string getAsString() const override;
978};
979
980/// TernOpInit - !op (X, Y, Z) - Combine two inits.
981///
982class TernOpInit : public OpInit {
983public:
984  enum TernaryOp { SUBST, FOREACH, IF };
985
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  OpInit *clone(std::vector<Init *> &Operands) const override {
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  int getNumOperands() const override { return 3; }
1014  Init *getOperand(int i) const override {
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  Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
1034
1035  bool isComplete() const override { return false; }
1036
1037  Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1038
1039  std::string getAsString() const override;
1040};
1041
1042/// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1043///
1044class VarInit : public TypedInit {
1045  Init *VarName;
1046
1047  explicit VarInit(const std::string &VN, RecTy *T)
1048      : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
1049  explicit VarInit(Init *VN, RecTy *T)
1050      : TypedInit(IK_VarInit, T), VarName(VN) {}
1051
1052  VarInit(const VarInit &Other) LLVM_DELETED_FUNCTION;
1053  VarInit &operator=(const VarInit &Other) LLVM_DELETED_FUNCTION;
1054
1055public:
1056  static bool classof(const Init *I) {
1057    return I->getKind() == IK_VarInit;
1058  }
1059  static VarInit *get(const std::string &VN, RecTy *T);
1060  static VarInit *get(Init *VN, RecTy *T);
1061
1062  Init *convertInitializerTo(RecTy *Ty) const override {
1063    return Ty->convertValue(const_cast<VarInit *>(this));
1064  }
1065
1066  const std::string &getName() const;
1067  Init *getNameInit() const { return VarName; }
1068  std::string getNameInitAsString() const {
1069    return getNameInit()->getAsUnquotedString();
1070  }
1071
1072  Init *resolveListElementReference(Record &R, const RecordVal *RV,
1073                                    unsigned Elt) const override;
1074
1075  RecTy *getFieldType(const std::string &FieldName) const override;
1076  Init *getFieldInit(Record &R, const RecordVal *RV,
1077                     const std::string &FieldName) const override;
1078
1079  /// resolveReferences - This method is used by classes that refer to other
1080  /// variables which may not be defined at the time they expression is formed.
1081  /// If a value is set for the variable later, this method will be called on
1082  /// users of the value to allow the value to propagate out.
1083  ///
1084  Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1085
1086  Init *getBit(unsigned Bit) const override;
1087
1088  std::string getAsString() const override { return getName(); }
1089};
1090
1091/// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1092///
1093class VarBitInit : public Init {
1094  TypedInit *TI;
1095  unsigned Bit;
1096
1097  VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
1098    assert(T->getType() &&
1099           (isa<IntRecTy>(T->getType()) ||
1100            (isa<BitsRecTy>(T->getType()) &&
1101             cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
1102           "Illegal VarBitInit expression!");
1103  }
1104
1105  VarBitInit(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1106  VarBitInit &operator=(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1107
1108public:
1109  static bool classof(const Init *I) {
1110    return I->getKind() == IK_VarBitInit;
1111  }
1112  static VarBitInit *get(TypedInit *T, unsigned B);
1113
1114  Init *convertInitializerTo(RecTy *Ty) const override {
1115    return Ty->convertValue(const_cast<VarBitInit *>(this));
1116  }
1117
1118  Init *getBitVar() const override { return TI; }
1119  unsigned getBitNum() const override { return Bit; }
1120
1121  std::string getAsString() const override;
1122  Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1123
1124  Init *getBit(unsigned B) const override {
1125    assert(B < 1 && "Bit index out of range!");
1126    return const_cast<VarBitInit*>(this);
1127  }
1128};
1129
1130/// VarListElementInit - List[4] - Represent access to one element of a var or
1131/// field.
1132class VarListElementInit : public TypedInit {
1133  TypedInit *TI;
1134  unsigned Element;
1135
1136  VarListElementInit(TypedInit *T, unsigned E)
1137      : TypedInit(IK_VarListElementInit,
1138                  cast<ListRecTy>(T->getType())->getElementType()),
1139        TI(T), Element(E) {
1140    assert(T->getType() && isa<ListRecTy>(T->getType()) &&
1141           "Illegal VarBitInit expression!");
1142  }
1143
1144  VarListElementInit(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1145  void operator=(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1146
1147public:
1148  static bool classof(const Init *I) {
1149    return I->getKind() == IK_VarListElementInit;
1150  }
1151  static VarListElementInit *get(TypedInit *T, unsigned E);
1152
1153  Init *convertInitializerTo(RecTy *Ty) const override {
1154    return Ty->convertValue(const_cast<VarListElementInit *>(this));
1155  }
1156
1157  TypedInit *getVariable() const { return TI; }
1158  unsigned getElementNum() const { return Element; }
1159
1160  /// resolveListElementReference - This method is used to implement
1161  /// VarListElementInit::resolveReferences.  If the list element is resolvable
1162  /// now, we return the resolved value, otherwise we return null.
1163  Init *resolveListElementReference(Record &R, const RecordVal *RV,
1164                                    unsigned Elt) const override;
1165
1166  std::string getAsString() const override;
1167  Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1168
1169  Init *getBit(unsigned Bit) const override;
1170};
1171
1172/// DefInit - AL - Represent a reference to a 'def' in the description
1173///
1174class DefInit : public TypedInit {
1175  Record *Def;
1176
1177  DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
1178  friend class Record;
1179
1180  DefInit(const DefInit &Other) LLVM_DELETED_FUNCTION;
1181  DefInit &operator=(const DefInit &Other) LLVM_DELETED_FUNCTION;
1182
1183public:
1184  static bool classof(const Init *I) {
1185    return I->getKind() == IK_DefInit;
1186  }
1187  static DefInit *get(Record*);
1188
1189  Init *convertInitializerTo(RecTy *Ty) const override {
1190    return Ty->convertValue(const_cast<DefInit *>(this));
1191  }
1192
1193  Record *getDef() const { return Def; }
1194
1195  //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1196
1197  RecTy *getFieldType(const std::string &FieldName) const override;
1198  Init *getFieldInit(Record &R, const RecordVal *RV,
1199                     const std::string &FieldName) const override;
1200
1201  std::string getAsString() const override;
1202
1203  Init *getBit(unsigned Bit) const override {
1204    llvm_unreachable("Illegal bit reference off def");
1205  }
1206
1207  /// resolveListElementReference - This method is used to implement
1208  /// VarListElementInit::resolveReferences.  If the list element is resolvable
1209  /// now, we return the resolved value, otherwise we return null.
1210  Init *resolveListElementReference(Record &R, const RecordVal *RV,
1211                                    unsigned Elt) const override {
1212    llvm_unreachable("Illegal element reference off def");
1213  }
1214};
1215
1216/// FieldInit - X.Y - Represent a reference to a subfield of a variable
1217///
1218class FieldInit : public TypedInit {
1219  Init *Rec;                // Record we are referring to
1220  std::string FieldName;    // Field we are accessing
1221
1222  FieldInit(Init *R, const std::string &FN)
1223      : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1224    assert(getType() && "FieldInit with non-record type!");
1225  }
1226
1227  FieldInit(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1228  FieldInit &operator=(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1229
1230public:
1231  static bool classof(const Init *I) {
1232    return I->getKind() == IK_FieldInit;
1233  }
1234  static FieldInit *get(Init *R, const std::string &FN);
1235  static FieldInit *get(Init *R, const Init *FN);
1236
1237  Init *convertInitializerTo(RecTy *Ty) const override {
1238    return Ty->convertValue(const_cast<FieldInit *>(this));
1239  }
1240
1241  Init *getBit(unsigned Bit) const override;
1242
1243  Init *resolveListElementReference(Record &R, const RecordVal *RV,
1244                                    unsigned Elt) const override;
1245
1246  Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1247
1248  std::string getAsString() const override {
1249    return Rec->getAsString() + "." + FieldName;
1250  }
1251};
1252
1253/// DagInit - (v a, b) - Represent a DAG tree value.  DAG inits are required
1254/// to have at least one value then a (possibly empty) list of arguments.  Each
1255/// argument can have a name associated with it.
1256///
1257class DagInit : public TypedInit, public FoldingSetNode {
1258  Init *Val;
1259  std::string ValName;
1260  std::vector<Init*> Args;
1261  std::vector<std::string> ArgNames;
1262
1263  DagInit(Init *V, const std::string &VN,
1264          ArrayRef<Init *> ArgRange,
1265          ArrayRef<std::string> NameRange)
1266      : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
1267          Args(ArgRange.begin(), ArgRange.end()),
1268          ArgNames(NameRange.begin(), NameRange.end()) {}
1269
1270  DagInit(const DagInit &Other) LLVM_DELETED_FUNCTION;
1271  DagInit &operator=(const DagInit &Other) LLVM_DELETED_FUNCTION;
1272
1273public:
1274  static bool classof(const Init *I) {
1275    return I->getKind() == IK_DagInit;
1276  }
1277  static DagInit *get(Init *V, const std::string &VN,
1278                      ArrayRef<Init *> ArgRange,
1279                      ArrayRef<std::string> NameRange);
1280  static DagInit *get(Init *V, const std::string &VN,
1281                      const std::vector<
1282                        std::pair<Init*, std::string> > &args);
1283
1284  void Profile(FoldingSetNodeID &ID) const;
1285
1286  Init *convertInitializerTo(RecTy *Ty) const override {
1287    return Ty->convertValue(const_cast<DagInit *>(this));
1288  }
1289
1290  Init *getOperator() const { return Val; }
1291
1292  const std::string &getName() const { return ValName; }
1293
1294  unsigned getNumArgs() const { return Args.size(); }
1295  Init *getArg(unsigned Num) const {
1296    assert(Num < Args.size() && "Arg number out of range!");
1297    return Args[Num];
1298  }
1299  const std::string &getArgName(unsigned Num) const {
1300    assert(Num < ArgNames.size() && "Arg number out of range!");
1301    return ArgNames[Num];
1302  }
1303
1304  Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1305
1306  std::string getAsString() const override;
1307
1308  typedef std::vector<Init*>::const_iterator       const_arg_iterator;
1309  typedef std::vector<std::string>::const_iterator const_name_iterator;
1310
1311  inline const_arg_iterator  arg_begin() const { return Args.begin(); }
1312  inline const_arg_iterator  arg_end  () const { return Args.end();   }
1313
1314  inline size_t              arg_size () const { return Args.size();  }
1315  inline bool                arg_empty() const { return Args.empty(); }
1316
1317  inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1318  inline const_name_iterator name_end  () const { return ArgNames.end();   }
1319
1320  inline size_t              name_size () const { return ArgNames.size();  }
1321  inline bool                name_empty() const { return ArgNames.empty(); }
1322
1323  Init *getBit(unsigned Bit) const override {
1324    llvm_unreachable("Illegal bit reference off dag");
1325  }
1326
1327  Init *resolveListElementReference(Record &R, const RecordVal *RV,
1328                                    unsigned Elt) const override {
1329    llvm_unreachable("Illegal element reference off dag");
1330  }
1331};
1332
1333//===----------------------------------------------------------------------===//
1334//  High-Level Classes
1335//===----------------------------------------------------------------------===//
1336
1337class RecordVal {
1338  Init *Name;
1339  RecTy *Ty;
1340  unsigned Prefix;
1341  Init *Value;
1342
1343public:
1344  RecordVal(Init *N, RecTy *T, unsigned P);
1345  RecordVal(const std::string &N, RecTy *T, unsigned P);
1346
1347  const std::string &getName() const;
1348  const Init *getNameInit() const { return Name; }
1349  std::string getNameInitAsString() const {
1350    return getNameInit()->getAsUnquotedString();
1351  }
1352
1353  unsigned getPrefix() const { return Prefix; }
1354  RecTy *getType() const { return Ty; }
1355  Init *getValue() const { return Value; }
1356
1357  bool setValue(Init *V) {
1358    if (V) {
1359      Value = V->convertInitializerTo(Ty);
1360      return Value == nullptr;
1361    }
1362    Value = nullptr;
1363    return false;
1364  }
1365
1366  void dump() const;
1367  void print(raw_ostream &OS, bool PrintSem = true) const;
1368};
1369
1370inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1371  RV.print(OS << "  ");
1372  return OS;
1373}
1374
1375class Record {
1376  static unsigned LastID;
1377
1378  // Unique record ID.
1379  unsigned ID;
1380  Init *Name;
1381  // Location where record was instantiated, followed by the location of
1382  // multiclass prototypes used.
1383  SmallVector<SMLoc, 4> Locs;
1384  std::vector<Init *> TemplateArgs;
1385  std::vector<RecordVal> Values;
1386  std::vector<Record *> SuperClasses;
1387  std::vector<SMRange> SuperClassRanges;
1388
1389  // Tracks Record instances. Not owned by Record.
1390  RecordKeeper &TrackedRecords;
1391
1392  DefInit *TheInit;
1393  bool IsAnonymous;
1394
1395  void init();
1396  void checkName();
1397
1398public:
1399  // Constructs a record.
1400  explicit Record(const std::string &N, ArrayRef<SMLoc> locs,
1401                  RecordKeeper &records, bool Anonymous = false) :
1402    ID(LastID++), Name(StringInit::get(N)), Locs(locs.begin(), locs.end()),
1403    TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous) {
1404    init();
1405  }
1406  explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
1407                  bool Anonymous = false) :
1408    ID(LastID++), Name(N), Locs(locs.begin(), locs.end()),
1409    TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous) {
1410    init();
1411  }
1412
1413  // When copy-constructing a Record, we must still guarantee a globally unique
1414  // ID number.  All other fields can be copied normally.
1415  Record(const Record &O) :
1416    ID(LastID++), Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
1417    Values(O.Values), SuperClasses(O.SuperClasses),
1418    SuperClassRanges(O.SuperClassRanges), TrackedRecords(O.TrackedRecords),
1419    TheInit(O.TheInit), IsAnonymous(O.IsAnonymous) { }
1420
1421  ~Record() {}
1422
1423  static unsigned getNewUID() { return LastID++; }
1424
1425  unsigned getID() const { return ID; }
1426
1427  const std::string &getName() const;
1428  Init *getNameInit() const {
1429    return Name;
1430  }
1431  const std::string getNameInitAsString() const {
1432    return getNameInit()->getAsUnquotedString();
1433  }
1434
1435  void setName(Init *Name);               // Also updates RecordKeeper.
1436  void setName(const std::string &Name);  // Also updates RecordKeeper.
1437
1438  ArrayRef<SMLoc> getLoc() const { return Locs; }
1439
1440  /// get the corresponding DefInit.
1441  DefInit *getDefInit();
1442
1443  const std::vector<Init *> &getTemplateArgs() const {
1444    return TemplateArgs;
1445  }
1446  const std::vector<RecordVal> &getValues() const { return Values; }
1447  const std::vector<Record*>   &getSuperClasses() const { return SuperClasses; }
1448  ArrayRef<SMRange> getSuperClassRanges() const { return SuperClassRanges; }
1449
1450  bool isTemplateArg(Init *Name) const {
1451    for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1452      if (TemplateArgs[i] == Name) return true;
1453    return false;
1454  }
1455  bool isTemplateArg(StringRef Name) const {
1456    return isTemplateArg(StringInit::get(Name.str()));
1457  }
1458
1459  const RecordVal *getValue(const Init *Name) const {
1460    for (unsigned i = 0, e = Values.size(); i != e; ++i)
1461      if (Values[i].getNameInit() == Name) return &Values[i];
1462    return nullptr;
1463  }
1464  const RecordVal *getValue(StringRef Name) const {
1465    return getValue(StringInit::get(Name));
1466  }
1467  RecordVal *getValue(const Init *Name) {
1468    for (unsigned i = 0, e = Values.size(); i != e; ++i)
1469      if (Values[i].getNameInit() == Name) return &Values[i];
1470    return nullptr;
1471  }
1472  RecordVal *getValue(StringRef Name) {
1473    return getValue(StringInit::get(Name));
1474  }
1475
1476  void addTemplateArg(Init *Name) {
1477    assert(!isTemplateArg(Name) && "Template arg already defined!");
1478    TemplateArgs.push_back(Name);
1479  }
1480  void addTemplateArg(StringRef Name) {
1481    addTemplateArg(StringInit::get(Name.str()));
1482  }
1483
1484  void addValue(const RecordVal &RV) {
1485    assert(getValue(RV.getNameInit()) == nullptr && "Value already added!");
1486    Values.push_back(RV);
1487    if (Values.size() > 1)
1488      // Keep NAME at the end of the list.  It makes record dumps a
1489      // bit prettier and allows TableGen tests to be written more
1490      // naturally.  Tests can use CHECK-NEXT to look for Record
1491      // fields they expect to see after a def.  They can't do that if
1492      // NAME is the first Record field.
1493      std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
1494  }
1495
1496  void removeValue(Init *Name) {
1497    for (unsigned i = 0, e = Values.size(); i != e; ++i)
1498      if (Values[i].getNameInit() == Name) {
1499        Values.erase(Values.begin()+i);
1500        return;
1501      }
1502    llvm_unreachable("Cannot remove an entry that does not exist!");
1503  }
1504
1505  void removeValue(StringRef Name) {
1506    removeValue(StringInit::get(Name.str()));
1507  }
1508
1509  bool isSubClassOf(const Record *R) const {
1510    for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1511      if (SuperClasses[i] == R)
1512        return true;
1513    return false;
1514  }
1515
1516  bool isSubClassOf(StringRef Name) const {
1517    for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1518      if (SuperClasses[i]->getNameInitAsString() == Name)
1519        return true;
1520    return false;
1521  }
1522
1523  void addSuperClass(Record *R, SMRange Range) {
1524    assert(!isSubClassOf(R) && "Already subclassing record!");
1525    SuperClasses.push_back(R);
1526    SuperClassRanges.push_back(Range);
1527  }
1528
1529  /// resolveReferences - If there are any field references that refer to fields
1530  /// that have been filled in, we can propagate the values now.
1531  ///
1532  void resolveReferences() { resolveReferencesTo(nullptr); }
1533
1534  /// resolveReferencesTo - If anything in this record refers to RV, replace the
1535  /// reference to RV with the RHS of RV.  If RV is null, we resolve all
1536  /// possible references.
1537  void resolveReferencesTo(const RecordVal *RV);
1538
1539  RecordKeeper &getRecords() const {
1540    return TrackedRecords;
1541  }
1542
1543  bool isAnonymous() const {
1544    return IsAnonymous;
1545  }
1546
1547  void dump() const;
1548
1549  //===--------------------------------------------------------------------===//
1550  // High-level methods useful to tablegen back-ends
1551  //
1552
1553  /// getValueInit - Return the initializer for a value with the specified name,
1554  /// or throw an exception if the field does not exist.
1555  ///
1556  Init *getValueInit(StringRef FieldName) const;
1557
1558  /// Return true if the named field is unset.
1559  bool isValueUnset(StringRef FieldName) const {
1560    return getValueInit(FieldName) == UnsetInit::get();
1561  }
1562
1563  /// getValueAsString - This method looks up the specified field and returns
1564  /// its value as a string, throwing an exception if the field does not exist
1565  /// or if the value is not a string.
1566  ///
1567  std::string getValueAsString(StringRef FieldName) const;
1568
1569  /// getValueAsBitsInit - This method looks up the specified field and returns
1570  /// its value as a BitsInit, throwing an exception if the field does not exist
1571  /// or if the value is not the right type.
1572  ///
1573  BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1574
1575  /// getValueAsListInit - This method looks up the specified field and returns
1576  /// its value as a ListInit, throwing an exception if the field does not exist
1577  /// or if the value is not the right type.
1578  ///
1579  ListInit *getValueAsListInit(StringRef FieldName) const;
1580
1581  /// getValueAsListOfDefs - This method looks up the specified field and
1582  /// returns its value as a vector of records, throwing an exception if the
1583  /// field does not exist or if the value is not the right type.
1584  ///
1585  std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1586
1587  /// getValueAsListOfInts - This method looks up the specified field and
1588  /// returns its value as a vector of integers, throwing an exception if the
1589  /// field does not exist or if the value is not the right type.
1590  ///
1591  std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1592
1593  /// getValueAsListOfStrings - This method looks up the specified field and
1594  /// returns its value as a vector of strings, throwing an exception if the
1595  /// field does not exist or if the value is not the right type.
1596  ///
1597  std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1598
1599  /// getValueAsDef - This method looks up the specified field and returns its
1600  /// value as a Record, throwing an exception if the field does not exist or if
1601  /// the value is not the right type.
1602  ///
1603  Record *getValueAsDef(StringRef FieldName) const;
1604
1605  /// getValueAsBit - This method looks up the specified field and returns its
1606  /// value as a bit, throwing an exception if the field does not exist or if
1607  /// the value is not the right type.
1608  ///
1609  bool getValueAsBit(StringRef FieldName) const;
1610
1611  /// getValueAsBitOrUnset - This method looks up the specified field and
1612  /// returns its value as a bit. If the field is unset, sets Unset to true and
1613  /// returns false.
1614  ///
1615  bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
1616
1617  /// getValueAsInt - This method looks up the specified field and returns its
1618  /// value as an int64_t, throwing an exception if the field does not exist or
1619  /// if the value is not the right type.
1620  ///
1621  int64_t getValueAsInt(StringRef FieldName) const;
1622
1623  /// getValueAsDag - This method looks up the specified field and returns its
1624  /// value as an Dag, throwing an exception if the field does not exist or if
1625  /// the value is not the right type.
1626  ///
1627  DagInit *getValueAsDag(StringRef FieldName) const;
1628};
1629
1630raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1631
1632struct MultiClass {
1633  Record Rec;  // Placeholder for template args and Name.
1634  typedef std::vector<Record*> RecordVector;
1635  RecordVector DefPrototypes;
1636
1637  void dump() const;
1638
1639  MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1640    Rec(Name, Loc, Records) {}
1641};
1642
1643class RecordKeeper {
1644  std::map<std::string, Record*> Classes, Defs;
1645
1646public:
1647  ~RecordKeeper() {
1648    for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1649           E = Classes.end(); I != E; ++I)
1650      delete I->second;
1651    for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1652           E = Defs.end(); I != E; ++I)
1653      delete I->second;
1654  }
1655
1656  const std::map<std::string, Record*> &getClasses() const { return Classes; }
1657  const std::map<std::string, Record*> &getDefs() const { return Defs; }
1658
1659  Record *getClass(const std::string &Name) const {
1660    std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1661    return I == Classes.end() ? nullptr : I->second;
1662  }
1663  Record *getDef(const std::string &Name) const {
1664    std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1665    return I == Defs.end() ? nullptr : I->second;
1666  }
1667  void addClass(Record *R) {
1668    bool Ins = Classes.insert(std::make_pair(R->getName(), R)).second;
1669    (void)Ins;
1670    assert(Ins && "Class already exists");
1671  }
1672  void addDef(Record *R) {
1673    bool Ins = Defs.insert(std::make_pair(R->getName(), R)).second;
1674    (void)Ins;
1675    assert(Ins && "Record already exists");
1676  }
1677
1678  /// removeClass - Remove, but do not delete, the specified record.
1679  ///
1680  void removeClass(const std::string &Name) {
1681    assert(Classes.count(Name) && "Class does not exist!");
1682    Classes.erase(Name);
1683  }
1684  /// removeDef - Remove, but do not delete, the specified record.
1685  ///
1686  void removeDef(const std::string &Name) {
1687    assert(Defs.count(Name) && "Def does not exist!");
1688    Defs.erase(Name);
1689  }
1690
1691  //===--------------------------------------------------------------------===//
1692  // High-level helper methods, useful for tablegen backends...
1693
1694  /// getAllDerivedDefinitions - This method returns all concrete definitions
1695  /// that derive from the specified class name.  If a class with the specified
1696  /// name does not exist, an exception is thrown.
1697  std::vector<Record*>
1698  getAllDerivedDefinitions(const std::string &ClassName) const;
1699
1700  void dump() const;
1701};
1702
1703/// LessRecord - Sorting predicate to sort record pointers by name.
1704///
1705struct LessRecord {
1706  bool operator()(const Record *Rec1, const Record *Rec2) const {
1707    return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1708  }
1709};
1710
1711/// LessRecordByID - Sorting predicate to sort record pointers by their
1712/// unique ID. If you just need a deterministic order, use this, since it
1713/// just compares two `unsigned`; the other sorting predicates require
1714/// string manipulation.
1715struct LessRecordByID {
1716  bool operator()(const Record *LHS, const Record *RHS) const {
1717    return LHS->getID() < RHS->getID();
1718  }
1719};
1720
1721/// LessRecordFieldName - Sorting predicate to sort record pointers by their
1722/// name field.
1723///
1724struct LessRecordFieldName {
1725  bool operator()(const Record *Rec1, const Record *Rec2) const {
1726    return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1727  }
1728};
1729
1730struct LessRecordRegister {
1731  static size_t min(size_t a, size_t b) { return a < b ? a : b; }
1732  static bool ascii_isdigit(char x) { return x >= '0' && x <= '9'; }
1733
1734  struct RecordParts {
1735    SmallVector<std::pair< bool, StringRef>, 4> Parts;
1736
1737    RecordParts(StringRef Rec) {
1738      if (Rec.empty())
1739        return;
1740
1741      size_t Len = 0;
1742      const char *Start = Rec.data();
1743      const char *Curr = Start;
1744      bool isDigitPart = ascii_isdigit(Curr[0]);
1745      for (size_t I = 0, E = Rec.size(); I != E; ++I, ++Len) {
1746        bool isDigit = ascii_isdigit(Curr[I]);
1747        if (isDigit != isDigitPart) {
1748          Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1749          Len = 0;
1750          Start = &Curr[I];
1751          isDigitPart = ascii_isdigit(Curr[I]);
1752        }
1753      }
1754      // Push the last part.
1755      Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1756    }
1757
1758    size_t size() { return Parts.size(); }
1759
1760    std::pair<bool, StringRef> getPart(size_t i) {
1761      assert (i < Parts.size() && "Invalid idx!");
1762      return Parts[i];
1763    }
1764  };
1765
1766  bool operator()(const Record *Rec1, const Record *Rec2) const {
1767    RecordParts LHSParts(StringRef(Rec1->getName()));
1768    RecordParts RHSParts(StringRef(Rec2->getName()));
1769
1770    size_t LHSNumParts = LHSParts.size();
1771    size_t RHSNumParts = RHSParts.size();
1772    assert (LHSNumParts && RHSNumParts && "Expected at least one part!");
1773
1774    if (LHSNumParts != RHSNumParts)
1775      return LHSNumParts < RHSNumParts;
1776
1777    // We expect the registers to be of the form [_a-zA-z]+([0-9]*[_a-zA-Z]*)*.
1778    for (size_t I = 0, E = LHSNumParts; I < E; I+=2) {
1779      std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1780      std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1781      // Expect even part to always be alpha.
1782      assert (LHSPart.first == false && RHSPart.first == false &&
1783              "Expected both parts to be alpha.");
1784      if (int Res = LHSPart.second.compare(RHSPart.second))
1785        return Res < 0;
1786    }
1787    for (size_t I = 1, E = LHSNumParts; I < E; I+=2) {
1788      std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1789      std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1790      // Expect odd part to always be numeric.
1791      assert (LHSPart.first == true && RHSPart.first == true &&
1792              "Expected both parts to be numeric.");
1793      if (LHSPart.second.size() != RHSPart.second.size())
1794        return LHSPart.second.size() < RHSPart.second.size();
1795
1796      unsigned LHSVal, RHSVal;
1797
1798      bool LHSFailed = LHSPart.second.getAsInteger(10, LHSVal); (void)LHSFailed;
1799      assert(!LHSFailed && "Unable to convert LHS to integer.");
1800      bool RHSFailed = RHSPart.second.getAsInteger(10, RHSVal); (void)RHSFailed;
1801      assert(!RHSFailed && "Unable to convert RHS to integer.");
1802
1803      if (LHSVal != RHSVal)
1804        return LHSVal < RHSVal;
1805    }
1806    return LHSNumParts < RHSNumParts;
1807  }
1808};
1809
1810raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1811
1812/// QualifyName - Return an Init with a qualifier prefix referring
1813/// to CurRec's name.
1814Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1815                  Init *Name, const std::string &Scoper);
1816
1817/// QualifyName - Return an Init with a qualifier prefix referring
1818/// to CurRec's name.
1819Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1820                  const std::string &Name, const std::string &Scoper);
1821
1822} // End llvm namespace
1823
1824#endif
1825