LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Record.cpp
Go to the documentation of this file.
1 //===- Record.cpp - Record implementation ---------------------------------===//
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 // Implement the tablegen record classes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/TableGen/Record.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/FoldingSet.h"
17 #include "llvm/ADT/Hashing.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/Support/DataTypes.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/TableGen/Error.h"
26 
27 using namespace llvm;
28 
29 //===----------------------------------------------------------------------===//
30 // std::string wrapper for DenseMap purposes
31 //===----------------------------------------------------------------------===//
32 
33 namespace llvm {
34 
35 /// TableGenStringKey - This is a wrapper for std::string suitable for
36 /// using as a key to a DenseMap. Because there isn't a particularly
37 /// good way to indicate tombstone or empty keys for strings, we want
38 /// to wrap std::string to indicate that this is a "special" string
39 /// not expected to take on certain values (those of the tombstone and
40 /// empty keys). This makes things a little safer as it clarifies
41 /// that DenseMap is really not appropriate for general strings.
42 
44 public:
45  TableGenStringKey(const std::string &str) : data(str) {}
46  TableGenStringKey(const char *str) : data(str) {}
47 
48  const std::string &str() const { return data; }
49 
51  using llvm::hash_value;
52  return hash_value(Value.str());
53  }
54 private:
55  std::string data;
56 };
57 
58 /// Specialize DenseMapInfo for TableGenStringKey.
59 template<> struct DenseMapInfo<TableGenStringKey> {
60  static inline TableGenStringKey getEmptyKey() {
61  TableGenStringKey Empty("<<<EMPTY KEY>>>");
62  return Empty;
63  }
65  TableGenStringKey Tombstone("<<<TOMBSTONE KEY>>>");
66  return Tombstone;
67  }
68  static unsigned getHashValue(const TableGenStringKey& Val) {
69  using llvm::hash_value;
70  return hash_value(Val);
71  }
72  static bool isEqual(const TableGenStringKey& LHS,
73  const TableGenStringKey& RHS) {
74  return LHS.str() == RHS.str();
75  }
76 };
77 
78 } // namespace llvm
79 
80 //===----------------------------------------------------------------------===//
81 // Type implementations
82 //===----------------------------------------------------------------------===//
83 
84 BitRecTy BitRecTy::Shared;
85 IntRecTy IntRecTy::Shared;
86 StringRecTy StringRecTy::Shared;
87 DagRecTy DagRecTy::Shared;
88 
89 void RecTy::anchor() { }
90 void RecTy::dump() const { print(errs()); }
91 
93  if (!ListTy)
94  ListTy = new ListRecTy(this);
95  return ListTy;
96 }
97 
98 bool RecTy::baseClassOf(const RecTy *RHS) const{
99  assert (RHS && "NULL pointer");
100  return Kind == RHS->getRecTyKind();
101 }
102 
104  if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
105  return BI->getBit(0);
106 }
107 
109  int64_t Val = II->getValue();
110  if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
111 
112  return BitInit::get(Val != 0);
113 }
114 
116  RecTy *Ty = VI->getType();
117  if (isa<BitRecTy>(Ty) || isa<BitsRecTy>(Ty) || isa<IntRecTy>(Ty))
118  return VI; // Accept variable if it is already of bit type!
119  return 0;
120 }
121 
122 bool BitRecTy::baseClassOf(const RecTy *RHS) const{
124  return true;
125  if(const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
126  return BitsTy->getNumBits() == 1;
127  return false;
128 }
129 
130 BitsRecTy *BitsRecTy::get(unsigned Sz) {
131  static std::vector<BitsRecTy*> Shared;
132  if (Sz >= Shared.size())
133  Shared.resize(Sz + 1);
134  BitsRecTy *&Ty = Shared[Sz];
135  if (!Ty)
136  Ty = new BitsRecTy(Sz);
137  return Ty;
138 }
139 
140 std::string BitsRecTy::getAsString() const {
141  return "bits<" + utostr(Size) + ">";
142 }
143 
145  SmallVector<Init *, 16> NewBits(Size);
146 
147  for (unsigned i = 0; i != Size; ++i)
148  NewBits[i] = UnsetInit::get();
149 
150  return BitsInit::get(NewBits);
151 }
152 
154  if (Size != 1) return 0; // Can only convert single bit.
155  return BitsInit::get(UI);
156 }
157 
158 /// canFitInBitfield - Return true if the number of bits is large enough to hold
159 /// the integer value.
160 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
161  // For example, with NumBits == 4, we permit Values from [-7 .. 15].
162  return (NumBits >= sizeof(Value) * 8) ||
163  (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
164 }
165 
166 /// convertValue from Int initializer to bits type: Split the integer up into the
167 /// appropriate bits.
168 ///
170  int64_t Value = II->getValue();
171  // Make sure this bitfield is large enough to hold the integer value.
172  if (!canFitInBitfield(Value, Size))
173  return 0;
174 
175  SmallVector<Init *, 16> NewBits(Size);
176 
177  for (unsigned i = 0; i != Size; ++i)
178  NewBits[i] = BitInit::get(Value & (1LL << i));
179 
180  return BitsInit::get(NewBits);
181 }
182 
184  // If the number of bits is right, return it. Otherwise we need to expand or
185  // truncate.
186  if (BI->getNumBits() == Size) return BI;
187  return 0;
188 }
189 
191  if (Size == 1 && isa<BitRecTy>(VI->getType()))
192  return BitsInit::get(VI);
193 
194  if (VI->getType()->typeIsConvertibleTo(this)) {
195  SmallVector<Init *, 16> NewBits(Size);
196 
197  for (unsigned i = 0; i != Size; ++i)
198  NewBits[i] = VarBitInit::get(VI, i);
199  return BitsInit::get(NewBits);
200  }
201 
202  return 0;
203 }
204 
205 bool BitsRecTy::baseClassOf(const RecTy *RHS) const{
206  if (RecTy::baseClassOf(RHS)) //argument and the receiver are the same type
207  return cast<BitsRecTy>(RHS)->Size == Size;
208  RecTyKind kind = RHS->getRecTyKind();
209  return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
210 }
211 
213  return IntInit::get(BI->getValue());
214 }
215 
217  int64_t Result = 0;
218  for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
219  if (BitInit *Bit = dyn_cast<BitInit>(BI->getBit(i))) {
220  Result |= Bit->getValue() << i;
221  } else {
222  return 0;
223  }
224  return IntInit::get(Result);
225 }
226 
228  if (TI->getType()->typeIsConvertibleTo(this))
229  return TI; // Accept variable if already of the right type!
230  return 0;
231 }
232 
233 bool IntRecTy::baseClassOf(const RecTy *RHS) const{
234  RecTyKind kind = RHS->getRecTyKind();
235  return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
236 }
237 
239  if (BO->getOpcode() == UnOpInit::CAST) {
240  Init *L = BO->getOperand()->convertInitializerTo(this);
241  if (L == 0) return 0;
242  if (L != BO->getOperand())
243  return UnOpInit::get(UnOpInit::CAST, L, new StringRecTy);
244  return BO;
245  }
246 
247  return convertValue((TypedInit*)BO);
248 }
249 
251  if (BO->getOpcode() == BinOpInit::STRCONCAT) {
252  Init *L = BO->getLHS()->convertInitializerTo(this);
253  Init *R = BO->getRHS()->convertInitializerTo(this);
254  if (L == 0 || R == 0) return 0;
255  if (L != BO->getLHS() || R != BO->getRHS())
257  return BO;
258  }
259 
260  return convertValue((TypedInit*)BO);
261 }
262 
263 
265  if (isa<StringRecTy>(TI->getType()))
266  return TI; // Accept variable if already of the right type!
267  return 0;
268 }
269 
270 std::string ListRecTy::getAsString() const {
271  return "list<" + Ty->getAsString() + ">";
272 }
273 
275  std::vector<Init*> Elements;
276 
277  // Verify that all of the elements of the list are subclasses of the
278  // appropriate class!
279  for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
280  if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
281  Elements.push_back(CI);
282  else
283  return 0;
284 
285  if (!isa<ListRecTy>(LI->getType()))
286  return 0;
287 
288  return ListInit::get(Elements, this);
289 }
290 
292  // Ensure that TI is compatible with our class.
293  if (ListRecTy *LRT = dyn_cast<ListRecTy>(TI->getType()))
294  if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
295  return TI;
296  return 0;
297 }
298 
299 bool ListRecTy::baseClassOf(const RecTy *RHS) const{
300  if(const ListRecTy* ListTy = dyn_cast<ListRecTy>(RHS))
301  return ListTy->getElementType()->typeIsConvertibleTo(Ty);
302  return false;
303 }
304 
306  if (TI->getType()->typeIsConvertibleTo(this))
307  return TI;
308  return 0;
309 }
310 
312  if (BO->getOpcode() == UnOpInit::CAST) {
313  Init *L = BO->getOperand()->convertInitializerTo(this);
314  if (L == 0) return 0;
315  if (L != BO->getOperand())
316  return UnOpInit::get(UnOpInit::CAST, L, new DagRecTy);
317  return BO;
318  }
319  return 0;
320 }
321 
323  if (BO->getOpcode() == BinOpInit::CONCAT) {
324  Init *L = BO->getLHS()->convertInitializerTo(this);
325  Init *R = BO->getRHS()->convertInitializerTo(this);
326  if (L == 0 || R == 0) return 0;
327  if (L != BO->getLHS() || R != BO->getRHS())
328  return BinOpInit::get(BinOpInit::CONCAT, L, R, new DagRecTy);
329  return BO;
330  }
331  return 0;
332 }
333 
335  return dyn_cast<RecordRecTy>(R->getDefInit()->getType());
336 }
337 
338 std::string RecordRecTy::getAsString() const {
339  return Rec->getName();
340 }
341 
343  // Ensure that DI is a subclass of Rec.
344  if (!DI->getDef()->isSubClassOf(Rec))
345  return 0;
346  return DI;
347 }
348 
350  // Ensure that TI is compatible with Rec.
351  if (RecordRecTy *RRT = dyn_cast<RecordRecTy>(TI->getType()))
352  if (RRT->getRecord()->isSubClassOf(getRecord()) ||
353  RRT->getRecord() == getRecord())
354  return TI;
355  return 0;
356 }
357 
358 bool RecordRecTy::baseClassOf(const RecTy *RHS) const{
359  const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
360  if (!RTy)
361  return false;
362 
363  if (Rec == RTy->getRecord() || RTy->getRecord()->isSubClassOf(Rec))
364  return true;
365 
366  const std::vector<Record*> &SC = Rec->getSuperClasses();
367  for (unsigned i = 0, e = SC.size(); i != e; ++i)
368  if (RTy->getRecord()->isSubClassOf(SC[i]))
369  return true;
370 
371  return false;
372 }
373 
374 /// resolveTypes - Find a common type that T1 and T2 convert to.
375 /// Return 0 if no such type exists.
376 ///
378  if (T1->typeIsConvertibleTo(T2))
379  return T2;
380  if (T2->typeIsConvertibleTo(T1))
381  return T1;
382 
383  // If one is a Record type, check superclasses
384  if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
385  // See if T2 inherits from a type T1 also inherits from
386  const std::vector<Record *> &T1SuperClasses =
387  RecTy1->getRecord()->getSuperClasses();
388  for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(),
389  iend = T1SuperClasses.end();
390  i != iend;
391  ++i) {
392  RecordRecTy *SuperRecTy1 = RecordRecTy::get(*i);
393  RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
394  if (NewType1 != 0) {
395  if (NewType1 != SuperRecTy1) {
396  delete SuperRecTy1;
397  }
398  return NewType1;
399  }
400  }
401  }
402  if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2)) {
403  // See if T1 inherits from a type T2 also inherits from
404  const std::vector<Record *> &T2SuperClasses =
405  RecTy2->getRecord()->getSuperClasses();
406  for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(),
407  iend = T2SuperClasses.end();
408  i != iend;
409  ++i) {
410  RecordRecTy *SuperRecTy2 = RecordRecTy::get(*i);
411  RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
412  if (NewType2 != 0) {
413  if (NewType2 != SuperRecTy2) {
414  delete SuperRecTy2;
415  }
416  return NewType2;
417  }
418  }
419  }
420  return 0;
421 }
422 
423 
424 //===----------------------------------------------------------------------===//
425 // Initializer implementations
426 //===----------------------------------------------------------------------===//
427 
428 void Init::anchor() { }
429 void Init::dump() const { return print(errs()); }
430 
431 void UnsetInit::anchor() { }
432 
434  static UnsetInit TheInit;
435  return &TheInit;
436 }
437 
438 void BitInit::anchor() { }
439 
441  static BitInit True(true);
442  static BitInit False(false);
443 
444  return V ? &True : &False;
445 }
446 
447 static void
449  ID.AddInteger(Range.size());
450 
451  for (ArrayRef<Init *>::iterator i = Range.begin(),
452  iend = Range.end();
453  i != iend;
454  ++i)
455  ID.AddPointer(*i);
456 }
457 
459  typedef FoldingSet<BitsInit> Pool;
460  static Pool ThePool;
461 
463  ProfileBitsInit(ID, Range);
464 
465  void *IP = 0;
466  if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
467  return I;
468 
469  BitsInit *I = new BitsInit(Range);
470  ThePool.InsertNode(I, IP);
471 
472  return I;
473 }
474 
476  ProfileBitsInit(ID, Bits);
477 }
478 
479 Init *
480 BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
481  SmallVector<Init *, 16> NewBits(Bits.size());
482 
483  for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
484  if (Bits[i] >= getNumBits())
485  return 0;
486  NewBits[i] = getBit(Bits[i]);
487  }
488  return BitsInit::get(NewBits);
489 }
490 
491 std::string BitsInit::getAsString() const {
492  std::string Result = "{ ";
493  for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
494  if (i) Result += ", ";
495  if (Init *Bit = getBit(e-i-1))
496  Result += Bit->getAsString();
497  else
498  Result += "*";
499  }
500  return Result + " }";
501 }
502 
503 // Fix bit initializer to preserve the behavior that bit reference from a unset
504 // bits initializer will resolve into VarBitInit to keep the field name and bit
505 // number used in targets with fixed insn length.
506 static Init *fixBitInit(const RecordVal *RV, Init *Before, Init *After) {
507  if (RV || After != UnsetInit::get())
508  return After;
509  return Before;
510 }
511 
512 // resolveReferences - If there are any field references that refer to fields
513 // that have been filled in, we can propagate the values now.
514 //
516  bool Changed = false;
518 
519  Init *CachedInit = 0;
520  Init *CachedBitVar = 0;
521  bool CachedBitVarChanged = false;
522 
523  for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
524  Init *CurBit = Bits[i];
525  Init *CurBitVar = CurBit->getBitVar();
526 
527  NewBits[i] = CurBit;
528 
529  if (CurBitVar == CachedBitVar) {
530  if (CachedBitVarChanged) {
531  Init *Bit = CachedInit->getBit(CurBit->getBitNum());
532  NewBits[i] = fixBitInit(RV, CurBit, Bit);
533  }
534  continue;
535  }
536  CachedBitVar = CurBitVar;
537  CachedBitVarChanged = false;
538 
539  Init *B;
540  do {
541  B = CurBitVar;
542  CurBitVar = CurBitVar->resolveReferences(R, RV);
543  CachedBitVarChanged |= B != CurBitVar;
544  Changed |= B != CurBitVar;
545  } while (B != CurBitVar);
546  CachedInit = CurBitVar;
547 
548  if (CachedBitVarChanged) {
549  Init *Bit = CurBitVar->getBit(CurBit->getBitNum());
550  NewBits[i] = fixBitInit(RV, CurBit, Bit);
551  }
552  }
553 
554  if (Changed)
555  return BitsInit::get(NewBits);
556 
557  return const_cast<BitsInit *>(this);
558 }
559 
560 namespace {
561  template<typename T>
562  class Pool : public T {
563  public:
564  ~Pool();
565  };
566  template<typename T>
567  Pool<T>::~Pool() {
568  for (typename T::iterator I = this->begin(), E = this->end(); I != E; ++I) {
569  typename T::value_type &Item = *I;
570  delete Item.second;
571  }
572  }
573 }
574 
575 IntInit *IntInit::get(int64_t V) {
576  static Pool<DenseMap<int64_t, IntInit *> > ThePool;
577 
578  IntInit *&I = ThePool[V];
579  if (!I) I = new IntInit(V);
580  return I;
581 }
582 
583 std::string IntInit::getAsString() const {
584  return itostr(Value);
585 }
586 
587 Init *
588 IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
589  SmallVector<Init *, 16> NewBits(Bits.size());
590 
591  for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
592  if (Bits[i] >= 64)
593  return 0;
594 
595  NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i]));
596  }
597  return BitsInit::get(NewBits);
598 }
599 
600 void StringInit::anchor() { }
601 
603  static Pool<StringMap<StringInit *> > ThePool;
604 
605  StringInit *&I = ThePool[V];
606  if (!I) I = new StringInit(V);
607  return I;
608 }
609 
611  ArrayRef<Init *> Range,
612  RecTy *EltTy) {
613  ID.AddInteger(Range.size());
614  ID.AddPointer(EltTy);
615 
616  for (ArrayRef<Init *>::iterator i = Range.begin(),
617  iend = Range.end();
618  i != iend;
619  ++i)
620  ID.AddPointer(*i);
621 }
622 
624  typedef FoldingSet<ListInit> Pool;
625  static Pool ThePool;
626 
627  // Just use the FoldingSetNodeID to compute a hash. Use a DenseMap
628  // for actual storage.
630  ProfileListInit(ID, Range, EltTy);
631 
632  void *IP = 0;
633  if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
634  return I;
635 
636  ListInit *I = new ListInit(Range, EltTy);
637  ThePool.InsertNode(I, IP);
638  return I;
639 }
640 
642  ListRecTy *ListType = dyn_cast<ListRecTy>(getType());
643  assert(ListType && "Bad type for ListInit!");
644  RecTy *EltTy = ListType->getElementType();
645 
646  ProfileListInit(ID, Values, EltTy);
647 }
648 
649 Init *
650 ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) const {
651  std::vector<Init*> Vals;
652  for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
653  if (Elements[i] >= getSize())
654  return 0;
655  Vals.push_back(getElement(Elements[i]));
656  }
657  return ListInit::get(Vals, getType());
658 }
659 
661  assert(i < Values.size() && "List element index out of range!");
662  DefInit *DI = dyn_cast<DefInit>(Values[i]);
663  if (DI == 0)
664  PrintFatalError("Expected record in list!");
665  return DI->getDef();
666 }
667 
669  std::vector<Init*> Resolved;
670  Resolved.reserve(getSize());
671  bool Changed = false;
672 
673  for (unsigned i = 0, e = getSize(); i != e; ++i) {
674  Init *E;
675  Init *CurElt = getElement(i);
676 
677  do {
678  E = CurElt;
679  CurElt = CurElt->resolveReferences(R, RV);
680  Changed |= E != CurElt;
681  } while (E != CurElt);
682  Resolved.push_back(E);
683  }
684 
685  if (Changed)
686  return ListInit::get(Resolved, getType());
687  return const_cast<ListInit *>(this);
688 }
689 
691  unsigned Elt) const {
692  if (Elt >= getSize())
693  return 0; // Out of range reference.
694  Init *E = getElement(Elt);
695  // If the element is set to some value, or if we are resolving a reference
696  // to a specific variable and that variable is explicitly unset, then
697  // replace the VarListElementInit with it.
698  if (IRV || !isa<UnsetInit>(E))
699  return E;
700  return 0;
701 }
702 
703 std::string ListInit::getAsString() const {
704  std::string Result = "[";
705  for (unsigned i = 0, e = Values.size(); i != e; ++i) {
706  if (i) Result += ", ";
707  Result += Values[i]->getAsString();
708  }
709  return Result + "]";
710 }
711 
713  unsigned Elt) const {
714  Init *Resolved = resolveReferences(R, IRV);
715  OpInit *OResolved = dyn_cast<OpInit>(Resolved);
716  if (OResolved) {
717  Resolved = OResolved->Fold(&R, 0);
718  }
719 
720  if (Resolved != this) {
721  TypedInit *Typed = dyn_cast<TypedInit>(Resolved);
722  assert(Typed && "Expected typed init for list reference");
723  if (Typed) {
724  Init *New = Typed->resolveListElementReference(R, IRV, Elt);
725  if (New)
726  return New;
727  return VarListElementInit::get(Typed, Elt);
728  }
729  }
730 
731  return 0;
732 }
733 
734 Init *OpInit::getBit(unsigned Bit) const {
735  if (getType() == BitRecTy::get())
736  return const_cast<OpInit*>(this);
737  return VarBitInit::get(const_cast<OpInit*>(this), Bit);
738 }
739 
741  typedef std::pair<std::pair<unsigned, Init *>, RecTy *> Key;
742  static Pool<DenseMap<Key, UnOpInit *> > ThePool;
743 
744  Key TheKey(std::make_pair(std::make_pair(opc, lhs), Type));
745 
746  UnOpInit *&I = ThePool[TheKey];
747  if (!I) I = new UnOpInit(opc, lhs, Type);
748  return I;
749 }
750 
751 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
752  switch (getOpcode()) {
753  case CAST: {
754  if (getType()->getAsString() == "string") {
755  if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
756  return LHSs;
757 
758  if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
759  return StringInit::get(LHSd->getDef()->getName());
760 
761  if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
762  return StringInit::get(LHSi->getAsString());
763  } else {
764  if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) {
765  std::string Name = LHSs->getValue();
766 
767  // From TGParser::ParseIDValue
768  if (CurRec) {
769  if (const RecordVal *RV = CurRec->getValue(Name)) {
770  if (RV->getType() != getType())
771  PrintFatalError("type mismatch in cast");
772  return VarInit::get(Name, RV->getType());
773  }
774 
775  Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name,
776  ":");
777 
778  if (CurRec->isTemplateArg(TemplateArgName)) {
779  const RecordVal *RV = CurRec->getValue(TemplateArgName);
780  assert(RV && "Template arg doesn't exist??");
781 
782  if (RV->getType() != getType())
783  PrintFatalError("type mismatch in cast");
784 
785  return VarInit::get(TemplateArgName, RV->getType());
786  }
787  }
788 
789  if (CurMultiClass) {
790  Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
791 
792  if (CurMultiClass->Rec.isTemplateArg(MCName)) {
793  const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
794  assert(RV && "Template arg doesn't exist??");
795 
796  if (RV->getType() != getType())
797  PrintFatalError("type mismatch in cast");
798 
799  return VarInit::get(MCName, RV->getType());
800  }
801  }
802 
803  if (Record *D = (CurRec->getRecords()).getDef(Name))
804  return DefInit::get(D);
805 
806  PrintFatalError(CurRec->getLoc(),
807  "Undefined reference:'" + Name + "'\n");
808  }
809  }
810  break;
811  }
812  case HEAD: {
813  if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
814  if (LHSl->getSize() == 0) {
815  assert(0 && "Empty list in car");
816  return 0;
817  }
818  return LHSl->getElement(0);
819  }
820  break;
821  }
822  case TAIL: {
823  if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
824  if (LHSl->getSize() == 0) {
825  assert(0 && "Empty list in cdr");
826  return 0;
827  }
828  // Note the +1. We can't just pass the result of getValues()
829  // directly.
830  ArrayRef<Init *>::iterator begin = LHSl->getValues().begin()+1;
831  ArrayRef<Init *>::iterator end = LHSl->getValues().end();
832  ListInit *Result =
833  ListInit::get(ArrayRef<Init *>(begin, end - begin),
834  LHSl->getType());
835  return Result;
836  }
837  break;
838  }
839  case EMPTY: {
840  if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
841  if (LHSl->getSize() == 0) {
842  return IntInit::get(1);
843  } else {
844  return IntInit::get(0);
845  }
846  }
847  if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) {
848  if (LHSs->getValue().empty()) {
849  return IntInit::get(1);
850  } else {
851  return IntInit::get(0);
852  }
853  }
854 
855  break;
856  }
857  }
858  return const_cast<UnOpInit *>(this);
859 }
860 
862  Init *lhs = LHS->resolveReferences(R, RV);
863 
864  if (LHS != lhs)
865  return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, 0);
866  return Fold(&R, 0);
867 }
868 
869 std::string UnOpInit::getAsString() const {
870  std::string Result;
871  switch (Opc) {
872  case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
873  case HEAD: Result = "!head"; break;
874  case TAIL: Result = "!tail"; break;
875  case EMPTY: Result = "!empty"; break;
876  }
877  return Result + "(" + LHS->getAsString() + ")";
878 }
879 
881  Init *rhs, RecTy *Type) {
882  typedef std::pair<
883  std::pair<std::pair<unsigned, Init *>, Init *>,
884  RecTy *
885  > Key;
886 
887  static Pool<DenseMap<Key, BinOpInit *> > ThePool;
888 
889  Key TheKey(std::make_pair(std::make_pair(std::make_pair(opc, lhs), rhs),
890  Type));
891 
892  BinOpInit *&I = ThePool[TheKey];
893  if (!I) I = new BinOpInit(opc, lhs, rhs, Type);
894  return I;
895 }
896 
897 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
898  switch (getOpcode()) {
899  case CONCAT: {
900  DagInit *LHSs = dyn_cast<DagInit>(LHS);
901  DagInit *RHSs = dyn_cast<DagInit>(RHS);
902  if (LHSs && RHSs) {
903  DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
904  DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
905  if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef())
906  PrintFatalError("Concated Dag operators do not match!");
907  std::vector<Init*> Args;
908  std::vector<std::string> ArgNames;
909  for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
910  Args.push_back(LHSs->getArg(i));
911  ArgNames.push_back(LHSs->getArgName(i));
912  }
913  for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
914  Args.push_back(RHSs->getArg(i));
915  ArgNames.push_back(RHSs->getArgName(i));
916  }
917  return DagInit::get(LHSs->getOperator(), "", Args, ArgNames);
918  }
919  break;
920  }
921  case STRCONCAT: {
922  StringInit *LHSs = dyn_cast<StringInit>(LHS);
923  StringInit *RHSs = dyn_cast<StringInit>(RHS);
924  if (LHSs && RHSs)
925  return StringInit::get(LHSs->getValue() + RHSs->getValue());
926  break;
927  }
928  case EQ: {
929  // try to fold eq comparison for 'bit' and 'int', otherwise fallback
930  // to string objects.
931  IntInit *L =
932  dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
933  IntInit *R =
934  dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
935 
936  if (L && R)
937  return IntInit::get(L->getValue() == R->getValue());
938 
939  StringInit *LHSs = dyn_cast<StringInit>(LHS);
940  StringInit *RHSs = dyn_cast<StringInit>(RHS);
941 
942  // Make sure we've resolved
943  if (LHSs && RHSs)
944  return IntInit::get(LHSs->getValue() == RHSs->getValue());
945 
946  break;
947  }
948  case ADD:
949  case SHL:
950  case SRA:
951  case SRL: {
952  IntInit *LHSi = dyn_cast<IntInit>(LHS);
953  IntInit *RHSi = dyn_cast<IntInit>(RHS);
954  if (LHSi && RHSi) {
955  int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
956  int64_t Result;
957  switch (getOpcode()) {
958  default: llvm_unreachable("Bad opcode!");
959  case ADD: Result = LHSv + RHSv; break;
960  case SHL: Result = LHSv << RHSv; break;
961  case SRA: Result = LHSv >> RHSv; break;
962  case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
963  }
964  return IntInit::get(Result);
965  }
966  break;
967  }
968  }
969  return const_cast<BinOpInit *>(this);
970 }
971 
973  Init *lhs = LHS->resolveReferences(R, RV);
974  Init *rhs = RHS->resolveReferences(R, RV);
975 
976  if (LHS != lhs || RHS != rhs)
977  return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
978  return Fold(&R, 0);
979 }
980 
981 std::string BinOpInit::getAsString() const {
982  std::string Result;
983  switch (Opc) {
984  case CONCAT: Result = "!con"; break;
985  case ADD: Result = "!add"; break;
986  case SHL: Result = "!shl"; break;
987  case SRA: Result = "!sra"; break;
988  case SRL: Result = "!srl"; break;
989  case EQ: Result = "!eq"; break;
990  case STRCONCAT: Result = "!strconcat"; break;
991  }
992  return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
993 }
994 
996  Init *mhs, Init *rhs,
997  RecTy *Type) {
998  typedef std::pair<
999  std::pair<
1000  std::pair<std::pair<unsigned, RecTy *>, Init *>,
1001  Init *
1002  >,
1003  Init *
1004  > Key;
1005 
1006  typedef DenseMap<Key, TernOpInit *> Pool;
1007  static Pool ThePool;
1008 
1009  Key TheKey(std::make_pair(std::make_pair(std::make_pair(std::make_pair(opc,
1010  Type),
1011  lhs),
1012  mhs),
1013  rhs));
1014 
1015  TernOpInit *&I = ThePool[TheKey];
1016  if (!I) I = new TernOpInit(opc, lhs, mhs, rhs, Type);
1017  return I;
1018 }
1019 
1020 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
1021  Record *CurRec, MultiClass *CurMultiClass);
1022 
1023 static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
1024  RecTy *Type, Record *CurRec,
1025  MultiClass *CurMultiClass) {
1026  std::vector<Init *> NewOperands;
1027 
1028  TypedInit *TArg = dyn_cast<TypedInit>(Arg);
1029 
1030  // If this is a dag, recurse
1031  if (TArg && TArg->getType()->getAsString() == "dag") {
1032  Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
1033  CurRec, CurMultiClass);
1034  if (Result != 0) {
1035  return Result;
1036  } else {
1037  return 0;
1038  }
1039  }
1040 
1041  for (int i = 0; i < RHSo->getNumOperands(); ++i) {
1042  OpInit *RHSoo = dyn_cast<OpInit>(RHSo->getOperand(i));
1043 
1044  if (RHSoo) {
1045  Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
1046  Type, CurRec, CurMultiClass);
1047  if (Result != 0) {
1048  NewOperands.push_back(Result);
1049  } else {
1050  NewOperands.push_back(Arg);
1051  }
1052  } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
1053  NewOperands.push_back(Arg);
1054  } else {
1055  NewOperands.push_back(RHSo->getOperand(i));
1056  }
1057  }
1058 
1059  // Now run the operator and use its result as the new leaf
1060  const OpInit *NewOp = RHSo->clone(NewOperands);
1061  Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
1062  if (NewVal != NewOp)
1063  return NewVal;
1064 
1065  return 0;
1066 }
1067 
1068 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
1069  Record *CurRec, MultiClass *CurMultiClass) {
1070  DagInit *MHSd = dyn_cast<DagInit>(MHS);
1071  ListInit *MHSl = dyn_cast<ListInit>(MHS);
1072 
1073  OpInit *RHSo = dyn_cast<OpInit>(RHS);
1074 
1075  if (!RHSo) {
1076  PrintFatalError(CurRec->getLoc(), "!foreach requires an operator\n");
1077  }
1078 
1079  TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
1080 
1081  if (!LHSt)
1082  PrintFatalError(CurRec->getLoc(), "!foreach requires typed variable\n");
1083 
1084  if ((MHSd && isa<DagRecTy>(Type)) || (MHSl && isa<ListRecTy>(Type))) {
1085  if (MHSd) {
1086  Init *Val = MHSd->getOperator();
1087  Init *Result = EvaluateOperation(RHSo, LHS, Val,
1088  Type, CurRec, CurMultiClass);
1089  if (Result != 0) {
1090  Val = Result;
1091  }
1092 
1093  std::vector<std::pair<Init *, std::string> > args;
1094  for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
1095  Init *Arg;
1096  std::string ArgName;
1097  Arg = MHSd->getArg(i);
1098  ArgName = MHSd->getArgName(i);
1099 
1100  // Process args
1101  Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
1102  CurRec, CurMultiClass);
1103  if (Result != 0) {
1104  Arg = Result;
1105  }
1106 
1107  // TODO: Process arg names
1108  args.push_back(std::make_pair(Arg, ArgName));
1109  }
1110 
1111  return DagInit::get(Val, "", args);
1112  }
1113  if (MHSl) {
1114  std::vector<Init *> NewOperands;
1115  std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
1116 
1117  for (std::vector<Init *>::iterator li = NewList.begin(),
1118  liend = NewList.end();
1119  li != liend;
1120  ++li) {
1121  Init *Item = *li;
1122  NewOperands.clear();
1123  for(int i = 0; i < RHSo->getNumOperands(); ++i) {
1124  // First, replace the foreach variable with the list item
1125  if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
1126  NewOperands.push_back(Item);
1127  } else {
1128  NewOperands.push_back(RHSo->getOperand(i));
1129  }
1130  }
1131 
1132  // Now run the operator and use its result as the new list item
1133  const OpInit *NewOp = RHSo->clone(NewOperands);
1134  Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
1135  if (NewItem != NewOp)
1136  *li = NewItem;
1137  }
1138  return ListInit::get(NewList, MHSl->getType());
1139  }
1140  }
1141  return 0;
1142 }
1143 
1144 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
1145  switch (getOpcode()) {
1146  case SUBST: {
1147  DefInit *LHSd = dyn_cast<DefInit>(LHS);
1148  VarInit *LHSv = dyn_cast<VarInit>(LHS);
1149  StringInit *LHSs = dyn_cast<StringInit>(LHS);
1150 
1151  DefInit *MHSd = dyn_cast<DefInit>(MHS);
1152  VarInit *MHSv = dyn_cast<VarInit>(MHS);
1153  StringInit *MHSs = dyn_cast<StringInit>(MHS);
1154 
1155  DefInit *RHSd = dyn_cast<DefInit>(RHS);
1156  VarInit *RHSv = dyn_cast<VarInit>(RHS);
1157  StringInit *RHSs = dyn_cast<StringInit>(RHS);
1158 
1159  if ((LHSd && MHSd && RHSd)
1160  || (LHSv && MHSv && RHSv)
1161  || (LHSs && MHSs && RHSs)) {
1162  if (RHSd) {
1163  Record *Val = RHSd->getDef();
1164  if (LHSd->getAsString() == RHSd->getAsString()) {
1165  Val = MHSd->getDef();
1166  }
1167  return DefInit::get(Val);
1168  }
1169  if (RHSv) {
1170  std::string Val = RHSv->getName();
1171  if (LHSv->getAsString() == RHSv->getAsString()) {
1172  Val = MHSv->getName();
1173  }
1174  return VarInit::get(Val, getType());
1175  }
1176  if (RHSs) {
1177  std::string Val = RHSs->getValue();
1178 
1179  std::string::size_type found;
1180  std::string::size_type idx = 0;
1181  do {
1182  found = Val.find(LHSs->getValue(), idx);
1183  if (found != std::string::npos) {
1184  Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
1185  }
1186  idx = found + MHSs->getValue().size();
1187  } while (found != std::string::npos);
1188 
1189  return StringInit::get(Val);
1190  }
1191  }
1192  break;
1193  }
1194 
1195  case FOREACH: {
1196  Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
1197  CurRec, CurMultiClass);
1198  if (Result != 0) {
1199  return Result;
1200  }
1201  break;
1202  }
1203 
1204  case IF: {
1205  IntInit *LHSi = dyn_cast<IntInit>(LHS);
1206  if (Init *I = LHS->convertInitializerTo(IntRecTy::get()))
1207  LHSi = dyn_cast<IntInit>(I);
1208  if (LHSi) {
1209  if (LHSi->getValue()) {
1210  return MHS;
1211  } else {
1212  return RHS;
1213  }
1214  }
1215  break;
1216  }
1217  }
1218 
1219  return const_cast<TernOpInit *>(this);
1220 }
1221 
1223  const RecordVal *RV) const {
1224  Init *lhs = LHS->resolveReferences(R, RV);
1225 
1226  if (Opc == IF && lhs != LHS) {
1227  IntInit *Value = dyn_cast<IntInit>(lhs);
1228  if (Init *I = lhs->convertInitializerTo(IntRecTy::get()))
1229  Value = dyn_cast<IntInit>(I);
1230  if (Value != 0) {
1231  // Short-circuit
1232  if (Value->getValue()) {
1233  Init *mhs = MHS->resolveReferences(R, RV);
1234  return (TernOpInit::get(getOpcode(), lhs, mhs,
1235  RHS, getType()))->Fold(&R, 0);
1236  } else {
1237  Init *rhs = RHS->resolveReferences(R, RV);
1238  return (TernOpInit::get(getOpcode(), lhs, MHS,
1239  rhs, getType()))->Fold(&R, 0);
1240  }
1241  }
1242  }
1243 
1244  Init *mhs = MHS->resolveReferences(R, RV);
1245  Init *rhs = RHS->resolveReferences(R, RV);
1246 
1247  if (LHS != lhs || MHS != mhs || RHS != rhs)
1248  return (TernOpInit::get(getOpcode(), lhs, mhs, rhs,
1249  getType()))->Fold(&R, 0);
1250  return Fold(&R, 0);
1251 }
1252 
1253 std::string TernOpInit::getAsString() const {
1254  std::string Result;
1255  switch (Opc) {
1256  case SUBST: Result = "!subst"; break;
1257  case FOREACH: Result = "!foreach"; break;
1258  case IF: Result = "!if"; break;
1259  }
1260  return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
1261  + RHS->getAsString() + ")";
1262 }
1263 
1264 RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
1265  if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType()))
1266  if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName))
1267  return Field->getType();
1268  return 0;
1269 }
1270 
1271 Init *
1272 TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
1274  if (T == 0) return 0; // Cannot subscript a non-bits variable.
1275  unsigned NumBits = T->getNumBits();
1276 
1277  SmallVector<Init *, 16> NewBits(Bits.size());
1278  for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1279  if (Bits[i] >= NumBits)
1280  return 0;
1281 
1282  NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), Bits[i]);
1283  }
1284  return BitsInit::get(NewBits);
1285 }
1286 
1287 Init *
1288 TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) const {
1290  if (T == 0) return 0; // Cannot subscript a non-list variable.
1291 
1292  if (Elements.size() == 1)
1293  return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
1294 
1295  std::vector<Init*> ListInits;
1296  ListInits.reserve(Elements.size());
1297  for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1298  ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
1299  Elements[i]));
1300  return ListInit::get(ListInits, T);
1301 }
1302 
1303 
1304 VarInit *VarInit::get(const std::string &VN, RecTy *T) {
1305  Init *Value = StringInit::get(VN);
1306  return VarInit::get(Value, T);
1307 }
1308 
1310  typedef std::pair<RecTy *, Init *> Key;
1311  static Pool<DenseMap<Key, VarInit *> > ThePool;
1312 
1313  Key TheKey(std::make_pair(T, VN));
1314 
1315  VarInit *&I = ThePool[TheKey];
1316  if (!I) I = new VarInit(VN, T);
1317  return I;
1318 }
1319 
1320 const std::string &VarInit::getName() const {
1321  StringInit *NameString = dyn_cast<StringInit>(getNameInit());
1322  assert(NameString && "VarInit name is not a string!");
1323  return NameString->getValue();
1324 }
1325 
1326 Init *VarInit::getBit(unsigned Bit) const {
1327  if (getType() == BitRecTy::get())
1328  return const_cast<VarInit*>(this);
1329  return VarBitInit::get(const_cast<VarInit*>(this), Bit);
1330 }
1331 
1333  const RecordVal *IRV,
1334  unsigned Elt) const {
1335  if (R.isTemplateArg(getNameInit())) return 0;
1336  if (IRV && IRV->getNameInit() != getNameInit()) return 0;
1337 
1338  RecordVal *RV = R.getValue(getNameInit());
1339  assert(RV && "Reference to a non-existent variable?");
1340  ListInit *LI = dyn_cast<ListInit>(RV->getValue());
1341  if (!LI) {
1342  TypedInit *VI = dyn_cast<TypedInit>(RV->getValue());
1343  assert(VI && "Invalid list element!");
1344  return VarListElementInit::get(VI, Elt);
1345  }
1346 
1347  if (Elt >= LI->getSize())
1348  return 0; // Out of range reference.
1349  Init *E = LI->getElement(Elt);
1350  // If the element is set to some value, or if we are resolving a reference
1351  // to a specific variable and that variable is explicitly unset, then
1352  // replace the VarListElementInit with it.
1353  if (IRV || !isa<UnsetInit>(E))
1354  return E;
1355  return 0;
1356 }
1357 
1358 
1359 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1360  if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType()))
1361  if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1362  return RV->getType();
1363  return 0;
1364 }
1365 
1367  const std::string &FieldName) const {
1368  if (isa<RecordRecTy>(getType()))
1369  if (const RecordVal *Val = R.getValue(VarName)) {
1370  if (RV != Val && (RV || isa<UnsetInit>(Val->getValue())))
1371  return 0;
1372  Init *TheInit = Val->getValue();
1373  assert(TheInit != this && "Infinite loop detected!");
1374  if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
1375  return I;
1376  else
1377  return 0;
1378  }
1379  return 0;
1380 }
1381 
1382 /// resolveReferences - This method is used by classes that refer to other
1383 /// variables which may not be defined at the time the expression is formed.
1384 /// If a value is set for the variable later, this method will be called on
1385 /// users of the value to allow the value to propagate out.
1386 ///
1388  if (RecordVal *Val = R.getValue(VarName))
1389  if (RV == Val || (RV == 0 && !isa<UnsetInit>(Val->getValue())))
1390  return Val->getValue();
1391  return const_cast<VarInit *>(this);
1392 }
1393 
1395  typedef std::pair<TypedInit *, unsigned> Key;
1396  typedef DenseMap<Key, VarBitInit *> Pool;
1397 
1398  static Pool ThePool;
1399 
1400  Key TheKey(std::make_pair(T, B));
1401 
1402  VarBitInit *&I = ThePool[TheKey];
1403  if (!I) I = new VarBitInit(T, B);
1404  return I;
1405 }
1406 
1407 std::string VarBitInit::getAsString() const {
1408  return TI->getAsString() + "{" + utostr(Bit) + "}";
1409 }
1410 
1412  Init *I = TI->resolveReferences(R, RV);
1413  if (TI != I)
1414  return I->getBit(getBitNum());
1415 
1416  return const_cast<VarBitInit*>(this);
1417 }
1418 
1420  unsigned E) {
1421  typedef std::pair<TypedInit *, unsigned> Key;
1423 
1424  static Pool ThePool;
1425 
1426  Key TheKey(std::make_pair(T, E));
1427 
1428  VarListElementInit *&I = ThePool[TheKey];
1429  if (!I) I = new VarListElementInit(T, E);
1430  return I;
1431 }
1432 
1433 std::string VarListElementInit::getAsString() const {
1434  return TI->getAsString() + "[" + utostr(Element) + "]";
1435 }
1436 
1437 Init *
1440  getElementNum()))
1441  return I;
1442  return const_cast<VarListElementInit *>(this);
1443 }
1444 
1446  if (getType() == BitRecTy::get())
1447  return const_cast<VarListElementInit*>(this);
1448  return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
1449 }
1450 
1452  const RecordVal *RV,
1453  unsigned Elt) const {
1454  Init *Result = TI->resolveListElementReference(R, RV, Element);
1455 
1456  if (Result) {
1457  if (TypedInit *TInit = dyn_cast<TypedInit>(Result)) {
1458  Init *Result2 = TInit->resolveListElementReference(R, RV, Elt);
1459  if (Result2) return Result2;
1460  return new VarListElementInit(TInit, Elt);
1461  }
1462  return Result;
1463  }
1464 
1465  return 0;
1466 }
1467 
1469  return R->getDefInit();
1470 }
1471 
1472 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1473  if (const RecordVal *RV = Def->getValue(FieldName))
1474  return RV->getType();
1475  return 0;
1476 }
1477 
1479  const std::string &FieldName) const {
1480  return Def->getValue(FieldName)->getValue();
1481 }
1482 
1483 
1484 std::string DefInit::getAsString() const {
1485  return Def->getName();
1486 }
1487 
1488 FieldInit *FieldInit::get(Init *R, const std::string &FN) {
1489  typedef std::pair<Init *, TableGenStringKey> Key;
1490  typedef DenseMap<Key, FieldInit *> Pool;
1491  static Pool ThePool;
1492 
1493  Key TheKey(std::make_pair(R, FN));
1494 
1495  FieldInit *&I = ThePool[TheKey];
1496  if (!I) I = new FieldInit(R, FN);
1497  return I;
1498 }
1499 
1500 Init *FieldInit::getBit(unsigned Bit) const {
1501  if (getType() == BitRecTy::get())
1502  return const_cast<FieldInit*>(this);
1503  return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
1504 }
1505 
1507  unsigned Elt) const {
1508  if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
1509  if (ListInit *LI = dyn_cast<ListInit>(ListVal)) {
1510  if (Elt >= LI->getSize()) return 0;
1511  Init *E = LI->getElement(Elt);
1512 
1513  // If the element is set to some value, or if we are resolving a
1514  // reference to a specific variable and that variable is explicitly
1515  // unset, then replace the VarListElementInit with it.
1516  if (RV || !isa<UnsetInit>(E))
1517  return E;
1518  }
1519  return 0;
1520 }
1521 
1523  Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1524 
1525  Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName);
1526  if (BitsVal) {
1527  Init *BVR = BitsVal->resolveReferences(R, RV);
1528  return BVR->isComplete() ? BVR : const_cast<FieldInit *>(this);
1529  }
1530 
1531  if (NewRec != Rec) {
1532  return FieldInit::get(NewRec, FieldName);
1533  }
1534  return const_cast<FieldInit *>(this);
1535 }
1536 
1537 static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, const std::string &VN,
1538  ArrayRef<Init *> ArgRange,
1539  ArrayRef<std::string> NameRange) {
1540  ID.AddPointer(V);
1541  ID.AddString(VN);
1542 
1543  ArrayRef<Init *>::iterator Arg = ArgRange.begin();
1545  while (Arg != ArgRange.end()) {
1546  assert(Name != NameRange.end() && "Arg name underflow!");
1547  ID.AddPointer(*Arg++);
1548  ID.AddString(*Name++);
1549  }
1550  assert(Name == NameRange.end() && "Arg name overflow!");
1551 }
1552 
1553 DagInit *
1554 DagInit::get(Init *V, const std::string &VN,
1555  ArrayRef<Init *> ArgRange,
1556  ArrayRef<std::string> NameRange) {
1557  typedef FoldingSet<DagInit> Pool;
1558  static Pool ThePool;
1559 
1561  ProfileDagInit(ID, V, VN, ArgRange, NameRange);
1562 
1563  void *IP = 0;
1564  if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
1565  return I;
1566 
1567  DagInit *I = new DagInit(V, VN, ArgRange, NameRange);
1568  ThePool.InsertNode(I, IP);
1569 
1570  return I;
1571 }
1572 
1573 DagInit *
1574 DagInit::get(Init *V, const std::string &VN,
1575  const std::vector<std::pair<Init*, std::string> > &args) {
1576  typedef std::pair<Init*, std::string> PairType;
1577 
1578  std::vector<Init *> Args;
1579  std::vector<std::string> Names;
1580 
1581  for (std::vector<PairType>::const_iterator i = args.begin(),
1582  iend = args.end();
1583  i != iend;
1584  ++i) {
1585  Args.push_back(i->first);
1586  Names.push_back(i->second);
1587  }
1588 
1589  return DagInit::get(V, VN, Args, Names);
1590 }
1591 
1593  ProfileDagInit(ID, Val, ValName, Args, ArgNames);
1594 }
1595 
1597  std::vector<Init*> NewArgs;
1598  for (unsigned i = 0, e = Args.size(); i != e; ++i)
1599  NewArgs.push_back(Args[i]->resolveReferences(R, RV));
1600 
1601  Init *Op = Val->resolveReferences(R, RV);
1602 
1603  if (Args != NewArgs || Op != Val)
1604  return DagInit::get(Op, ValName, NewArgs, ArgNames);
1605 
1606  return const_cast<DagInit *>(this);
1607 }
1608 
1609 
1610 std::string DagInit::getAsString() const {
1611  std::string Result = "(" + Val->getAsString();
1612  if (!ValName.empty())
1613  Result += ":" + ValName;
1614  if (Args.size()) {
1615  Result += " " + Args[0]->getAsString();
1616  if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
1617  for (unsigned i = 1, e = Args.size(); i != e; ++i) {
1618  Result += ", " + Args[i]->getAsString();
1619  if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
1620  }
1621  }
1622  return Result + ")";
1623 }
1624 
1625 
1626 //===----------------------------------------------------------------------===//
1627 // Other implementations
1628 //===----------------------------------------------------------------------===//
1629 
1631  : Name(N), Ty(T), Prefix(P) {
1633  assert(Value && "Cannot create unset value for current type!");
1634 }
1635 
1636 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1637  : Name(StringInit::get(N)), Ty(T), Prefix(P) {
1639  assert(Value && "Cannot create unset value for current type!");
1640 }
1641 
1642 const std::string &RecordVal::getName() const {
1643  StringInit *NameString = dyn_cast<StringInit>(Name);
1644  assert(NameString && "RecordVal name is not a string!");
1645  return NameString->getValue();
1646 }
1647 
1648 void RecordVal::dump() const { errs() << *this; }
1649 
1650 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1651  if (getPrefix()) OS << "field ";
1652  OS << *getType() << " " << getNameInitAsString();
1653 
1654  if (getValue())
1655  OS << " = " << *getValue();
1656 
1657  if (PrintSem) OS << ";\n";
1658 }
1659 
1660 unsigned Record::LastID = 0;
1661 
1662 void Record::init() {
1663  checkName();
1664 
1665  // Every record potentially has a def at the top. This value is
1666  // replaced with the top-level def name at instantiation time.
1667  RecordVal DN("NAME", StringRecTy::get(), 0);
1668  addValue(DN);
1669 }
1670 
1671 void Record::checkName() {
1672  // Ensure the record name has string type.
1673  const TypedInit *TypedName = dyn_cast<const TypedInit>(Name);
1674  assert(TypedName && "Record name is not typed!");
1675  RecTy *Type = TypedName->getType();
1676  if (!isa<StringRecTy>(Type))
1677  PrintFatalError(getLoc(), "Record name is not a string!");
1678 }
1679 
1681  if (!TheInit)
1682  TheInit = new DefInit(this, new RecordRecTy(this));
1683  return TheInit;
1684 }
1685 
1686 const std::string &Record::getName() const {
1687  const StringInit *NameString = dyn_cast<StringInit>(Name);
1688  assert(NameString && "Record name is not a string!");
1689  return NameString->getValue();
1690 }
1691 
1692 void Record::setName(Init *NewName) {
1693  if (TrackedRecords.getDef(Name->getAsUnquotedString()) == this) {
1694  TrackedRecords.removeDef(Name->getAsUnquotedString());
1695  TrackedRecords.addDef(this);
1696  } else if (TrackedRecords.getClass(Name->getAsUnquotedString()) == this) {
1697  TrackedRecords.removeClass(Name->getAsUnquotedString());
1698  TrackedRecords.addClass(this);
1699  } // Otherwise this isn't yet registered.
1700  Name = NewName;
1701  checkName();
1702  // DO NOT resolve record values to the name at this point because
1703  // there might be default values for arguments of this def. Those
1704  // arguments might not have been resolved yet so we don't want to
1705  // prematurely assume values for those arguments were not passed to
1706  // this def.
1707  //
1708  // Nonetheless, it may be that some of this Record's values
1709  // reference the record name. Indeed, the reason for having the
1710  // record name be an Init is to provide this flexibility. The extra
1711  // resolve steps after completely instantiating defs takes care of
1712  // this. See TGParser::ParseDef and TGParser::ParseDefm.
1713 }
1714 
1715 void Record::setName(const std::string &Name) {
1716  setName(StringInit::get(Name));
1717 }
1718 
1719 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1720 /// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1721 /// references.
1723  for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1724  if (RV == &Values[i]) // Skip resolve the same field as the given one
1725  continue;
1726  if (Init *V = Values[i].getValue())
1727  if (Values[i].setValue(V->resolveReferences(*this, RV)))
1728  PrintFatalError(getLoc(), "Invalid value is found when setting '"
1729  + Values[i].getNameInitAsString()
1730  + "' after resolving references"
1731  + (RV ? " against '" + RV->getNameInitAsString()
1732  + "' of ("
1733  + RV->getValue()->getAsUnquotedString() + ")"
1734  : "")
1735  + "\n");
1736  }
1737  Init *OldName = getNameInit();
1738  Init *NewName = Name->resolveReferences(*this, RV);
1739  if (NewName != OldName) {
1740  // Re-register with RecordKeeper.
1741  setName(NewName);
1742  }
1743 }
1744 
1745 void Record::dump() const { errs() << *this; }
1746 
1748  OS << R.getNameInitAsString();
1749 
1750  const std::vector<Init *> &TArgs = R.getTemplateArgs();
1751  if (!TArgs.empty()) {
1752  OS << "<";
1753  for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1754  if (i) OS << ", ";
1755  const RecordVal *RV = R.getValue(TArgs[i]);
1756  assert(RV && "Template argument record not found??");
1757  RV->print(OS, false);
1758  }
1759  OS << ">";
1760  }
1761 
1762  OS << " {";
1763  const std::vector<Record*> &SC = R.getSuperClasses();
1764  if (!SC.empty()) {
1765  OS << "\t//";
1766  for (unsigned i = 0, e = SC.size(); i != e; ++i)
1767  OS << " " << SC[i]->getNameInitAsString();
1768  }
1769  OS << "\n";
1770 
1771  const std::vector<RecordVal> &Vals = R.getValues();
1772  for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1773  if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1774  OS << Vals[i];
1775  for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1776  if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1777  OS << Vals[i];
1778 
1779  return OS << "}\n";
1780 }
1781 
1782 /// getValueInit - Return the initializer for a value with the specified name,
1783 /// or abort if the field does not exist.
1784 ///
1786  const RecordVal *R = getValue(FieldName);
1787  if (R == 0 || R->getValue() == 0)
1788  PrintFatalError(getLoc(), "Record `" + getName() +
1789  "' does not have a field named `" + FieldName.str() + "'!\n");
1790  return R->getValue();
1791 }
1792 
1793 
1794 /// getValueAsString - This method looks up the specified field and returns its
1795 /// value as a string, aborts if the field does not exist or if
1796 /// the value is not a string.
1797 ///
1798 std::string Record::getValueAsString(StringRef FieldName) const {
1799  const RecordVal *R = getValue(FieldName);
1800  if (R == 0 || R->getValue() == 0)
1801  PrintFatalError(getLoc(), "Record `" + getName() +
1802  "' does not have a field named `" + FieldName.str() + "'!\n");
1803 
1804  if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
1805  return SI->getValue();
1806  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1807  FieldName.str() + "' does not have a string initializer!");
1808 }
1809 
1810 /// getValueAsBitsInit - This method looks up the specified field and returns
1811 /// its value as a BitsInit, aborts if the field does not exist or if
1812 /// the value is not the right type.
1813 ///
1815  const RecordVal *R = getValue(FieldName);
1816  if (R == 0 || R->getValue() == 0)
1817  PrintFatalError(getLoc(), "Record `" + getName() +
1818  "' does not have a field named `" + FieldName.str() + "'!\n");
1819 
1820  if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
1821  return BI;
1822  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1823  FieldName.str() + "' does not have a BitsInit initializer!");
1824 }
1825 
1826 /// getValueAsListInit - This method looks up the specified field and returns
1827 /// its value as a ListInit, aborting if the field does not exist or if
1828 /// the value is not the right type.
1829 ///
1831  const RecordVal *R = getValue(FieldName);
1832  if (R == 0 || R->getValue() == 0)
1833  PrintFatalError(getLoc(), "Record `" + getName() +
1834  "' does not have a field named `" + FieldName.str() + "'!\n");
1835 
1836  if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
1837  return LI;
1838  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1839  FieldName.str() + "' does not have a list initializer!");
1840 }
1841 
1842 /// getValueAsListOfDefs - This method looks up the specified field and returns
1843 /// its value as a vector of records, aborting if the field does not exist
1844 /// or if the value is not the right type.
1845 ///
1846 std::vector<Record*>
1848  ListInit *List = getValueAsListInit(FieldName);
1849  std::vector<Record*> Defs;
1850  for (unsigned i = 0; i < List->getSize(); i++) {
1851  if (DefInit *DI = dyn_cast<DefInit>(List->getElement(i))) {
1852  Defs.push_back(DI->getDef());
1853  } else {
1854  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1855  FieldName.str() + "' list is not entirely DefInit!");
1856  }
1857  }
1858  return Defs;
1859 }
1860 
1861 /// getValueAsInt - This method looks up the specified field and returns its
1862 /// value as an int64_t, aborting if the field does not exist or if the value
1863 /// is not the right type.
1864 ///
1865 int64_t Record::getValueAsInt(StringRef FieldName) const {
1866  const RecordVal *R = getValue(FieldName);
1867  if (R == 0 || R->getValue() == 0)
1868  PrintFatalError(getLoc(), "Record `" + getName() +
1869  "' does not have a field named `" + FieldName.str() + "'!\n");
1870 
1871  if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
1872  return II->getValue();
1873  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1874  FieldName.str() + "' does not have an int initializer!");
1875 }
1876 
1877 /// getValueAsListOfInts - This method looks up the specified field and returns
1878 /// its value as a vector of integers, aborting if the field does not exist or
1879 /// if the value is not the right type.
1880 ///
1881 std::vector<int64_t>
1883  ListInit *List = getValueAsListInit(FieldName);
1884  std::vector<int64_t> Ints;
1885  for (unsigned i = 0; i < List->getSize(); i++) {
1886  if (IntInit *II = dyn_cast<IntInit>(List->getElement(i))) {
1887  Ints.push_back(II->getValue());
1888  } else {
1889  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1890  FieldName.str() + "' does not have a list of ints initializer!");
1891  }
1892  }
1893  return Ints;
1894 }
1895 
1896 /// getValueAsListOfStrings - This method looks up the specified field and
1897 /// returns its value as a vector of strings, aborting if the field does not
1898 /// exist or if the value is not the right type.
1899 ///
1900 std::vector<std::string>
1902  ListInit *List = getValueAsListInit(FieldName);
1903  std::vector<std::string> Strings;
1904  for (unsigned i = 0; i < List->getSize(); i++) {
1905  if (StringInit *II = dyn_cast<StringInit>(List->getElement(i))) {
1906  Strings.push_back(II->getValue());
1907  } else {
1908  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1909  FieldName.str() + "' does not have a list of strings initializer!");
1910  }
1911  }
1912  return Strings;
1913 }
1914 
1915 /// getValueAsDef - This method looks up the specified field and returns its
1916 /// value as a Record, aborting if the field does not exist or if the value
1917 /// is not the right type.
1918 ///
1920  const RecordVal *R = getValue(FieldName);
1921  if (R == 0 || R->getValue() == 0)
1922  PrintFatalError(getLoc(), "Record `" + getName() +
1923  "' does not have a field named `" + FieldName.str() + "'!\n");
1924 
1925  if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
1926  return DI->getDef();
1927  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1928  FieldName.str() + "' does not have a def initializer!");
1929 }
1930 
1931 /// getValueAsBit - This method looks up the specified field and returns its
1932 /// value as a bit, aborting if the field does not exist or if the value is
1933 /// not the right type.
1934 ///
1935 bool Record::getValueAsBit(StringRef FieldName) const {
1936  const RecordVal *R = getValue(FieldName);
1937  if (R == 0 || R->getValue() == 0)
1938  PrintFatalError(getLoc(), "Record `" + getName() +
1939  "' does not have a field named `" + FieldName.str() + "'!\n");
1940 
1941  if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1942  return BI->getValue();
1943  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1944  FieldName.str() + "' does not have a bit initializer!");
1945 }
1946 
1947 bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
1948  const RecordVal *R = getValue(FieldName);
1949  if (R == 0 || R->getValue() == 0)
1950  PrintFatalError(getLoc(), "Record `" + getName() +
1951  "' does not have a field named `" + FieldName.str() + "'!\n");
1952 
1953  if (R->getValue() == UnsetInit::get()) {
1954  Unset = true;
1955  return false;
1956  }
1957  Unset = false;
1958  if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
1959  return BI->getValue();
1960  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1961  FieldName.str() + "' does not have a bit initializer!");
1962 }
1963 
1964 /// getValueAsDag - This method looks up the specified field and returns its
1965 /// value as an Dag, aborting if the field does not exist or if the value is
1966 /// not the right type.
1967 ///
1969  const RecordVal *R = getValue(FieldName);
1970  if (R == 0 || R->getValue() == 0)
1971  PrintFatalError(getLoc(), "Record `" + getName() +
1972  "' does not have a field named `" + FieldName.str() + "'!\n");
1973 
1974  if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
1975  return DI;
1976  PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
1977  FieldName.str() + "' does not have a dag initializer!");
1978 }
1979 
1980 
1981 void MultiClass::dump() const {
1982  errs() << "Record:\n";
1983  Rec.dump();
1984 
1985  errs() << "Defs:\n";
1986  for (RecordVector::const_iterator r = DefPrototypes.begin(),
1987  rend = DefPrototypes.end();
1988  r != rend;
1989  ++r) {
1990  (*r)->dump();
1991  }
1992 }
1993 
1994 
1995 void RecordKeeper::dump() const { errs() << *this; }
1996 
1998  OS << "------------- Classes -----------------\n";
1999  const std::map<std::string, Record*> &Classes = RK.getClasses();
2000  for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
2001  E = Classes.end(); I != E; ++I)
2002  OS << "class " << *I->second;
2003 
2004  OS << "------------- Defs -----------------\n";
2005  const std::map<std::string, Record*> &Defs = RK.getDefs();
2006  for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
2007  E = Defs.end(); I != E; ++I)
2008  OS << "def " << *I->second;
2009  return OS;
2010 }
2011 
2012 
2013 /// getAllDerivedDefinitions - This method returns all concrete definitions
2014 /// that derive from the specified class name. If a class with the specified
2015 /// name does not exist, an error is printed and true is returned.
2016 std::vector<Record*>
2017 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
2018  Record *Class = getClass(ClassName);
2019  if (!Class)
2020  PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
2021 
2022  std::vector<Record*> Defs;
2023  for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
2024  E = getDefs().end(); I != E; ++I)
2025  if (I->second->isSubClassOf(Class))
2026  Defs.push_back(I->second);
2027 
2028  return Defs;
2029 }
2030 
2031 /// QualifyName - Return an Init with a qualifier prefix referring
2032 /// to CurRec's name.
2033 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
2034  Init *Name, const std::string &Scoper) {
2035  RecTy *Type = dyn_cast<TypedInit>(Name)->getType();
2036 
2037  BinOpInit *NewName =
2040  CurRec.getNameInit(),
2041  StringInit::get(Scoper),
2042  Type)->Fold(&CurRec, CurMultiClass),
2043  Name,
2044  Type);
2045 
2046  if (CurMultiClass && Scoper != "::") {
2047  NewName =
2050  CurMultiClass->Rec.getNameInit(),
2051  StringInit::get("::"),
2052  Type)->Fold(&CurRec, CurMultiClass),
2053  NewName->Fold(&CurRec, CurMultiClass),
2054  Type);
2055  }
2056 
2057  return NewName->Fold(&CurRec, CurMultiClass);
2058 }
2059 
2060 /// QualifyName - Return an Init with a qualifier prefix referring
2061 /// to CurRec's name.
2062 Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
2063  const std::string &Name,
2064  const std::string &Scoper) {
2065  return QualifyName(CurRec, CurMultiClass, StringInit::get(Name), Scoper);
2066 }
static BinOpInit * get(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type)
Definition: Record.cpp:880
virtual Init * resolveReferences(Record &R, const RecordVal *RV) const
Definition: Record.cpp:972
void AddPointer(const void *Ptr)
Definition: FoldingSet.cpp:52
void print(raw_ostream &OS) const
Definition: Record.h:94
COFF::RelocationTypeX86 Type
Definition: COFFYAML.cpp:227
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:181
RecTyKind
Subclass discriminator (for dyn_cast<> et al.)
Definition: Record.h:72
raw_ostream & errs()
virtual bool isComplete() const
Definition: Record.h:469
const std::string & getValue() const
Definition: Record.h:756
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:475
unsigned getNumBits() const
Definition: Record.h:668
virtual std::string getAsString() const
getAsString - Convert this value to a string form.
Definition: Record.cpp:1484
unsigned getNumArgs() const
Definition: Record.h:1296
virtual Init * getOperand(int i) const
Definition: Record.h:909
bool getValueAsBit(StringRef FieldName) const
Definition: Record.cpp:1935
const std::string & getArgName(unsigned Num) const
Definition: Record.h:1301
virtual Init * getBit(unsigned Bit) const
Definition: Record.cpp:1500
friend hash_code hash_value(const TableGenStringKey &Value)
Definition: Record.cpp:50
virtual Init * getBit(unsigned Bit) const
Definition: Record.h:690
Record * getElementAsRecord(unsigned i) const
Definition: Record.cpp:660
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:266
virtual RecTy * getFieldType(const std::string &FieldName) const
Definition: Record.cpp:1359
virtual Init * resolveReferences(Record &R, const RecordVal *RV) const
Definition: Record.cpp:1411
std::string getNameInitAsString() const
Definition: Record.h:1350
iterator end() const
Definition: ArrayRef.h:98
static UnOpInit * get(UnaryOp opc, Init *lhs, RecTy *Type)
Definition: Record.cpp:740
Init * getValueInit(StringRef FieldName) const
Definition: Record.cpp:1785
const std::string & str() const
Definition: Record.cpp:48
const std::vector< Init * > & getTemplateArgs() const
Definition: Record.h:1447
virtual bool baseClassOf(const RecTy *) const
Definition: Record.cpp:205
RecTy * getType() const
Definition: Record.h:570
virtual Init * getFieldInit(Record &R, const RecordVal *RV, const std::string &FieldName) const
Definition: Record.h:522
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:181
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:641
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:173
virtual Init * Fold(Record *CurRec, MultiClass *CurMultiClass) const
Definition: Record.cpp:1144
BinaryOp getOpcode() const
Definition: Record.h:965
Record * getValueAsDef(StringRef FieldName) const
Definition: Record.cpp:1919
Init * getElement(unsigned i) const
Definition: Record.h:802
virtual Init * Fold(Record *CurRec, MultiClass *CurMultiClass) const
Definition: Record.cpp:897
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Definition: Function.cpp:657
virtual Init * resolveReferences(Record &R, const RecordVal *RV) const
Definition: Record.cpp:1596
static TableGenStringKey getEmptyKey()
Definition: Record.cpp:60
virtual Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const
Definition: Record.cpp:712
virtual RecTy * getFieldType(const std::string &FieldName) const
Definition: Record.cpp:1472
std::string getValueAsString(StringRef FieldName) const
Definition: Record.cpp:1798
Init * getOperator() const
Definition: Record.h:1292
virtual std::string getAsUnquotedString() const
Definition: Record.h:480
static BitRecTy * get()
Definition: Record.h:151
LoopInfoBase< BlockT, LoopT > * LI
Definition: LoopInfoImpl.h:411
void print(raw_ostream &OS) const
print - Print out this value.
Definition: Record.h:472
const std::string & getName() const
Definition: Record.cpp:1686
static IntInit * get(int64_t V)
Definition: Record.cpp:575
virtual Init * convertValue(UnsetInit *UI)
Definition: Record.h:305
virtual Init * resolveReferences(Record &R, const RecordVal *RV) const
Definition: Record.cpp:1438
virtual int getNumOperands() const =0
virtual std::string getAsString() const
getAsString - Convert this value to a string form.
Definition: Record.cpp:491
Init * getValue() const
Definition: Record.h:1356
static BitsRecTy * get(unsigned Sz)
Definition: Record.cpp:130
virtual Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const =0
static void ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef< Init * > Range)
Definition: Record.cpp:448
#define llvm_unreachable(msg)
static StringRecTy * get()
Definition: Record.h:264
virtual std::string getAsString() const
getAsString - Convert this value to a string form.
Definition: Record.cpp:869
Init * getLHS() const
Definition: Record.h:966
void AddInteger(signed I)
Definition: FoldingSet.cpp:60
virtual std::string getAsString() const
getAsString - Convert this value to a string form.
Definition: Record.cpp:1253
Record * getClass(const std::string &Name) const
Definition: Record.h:1663
static Init * ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, Record *CurRec, MultiClass *CurMultiClass)
Definition: Record.cpp:1068
virtual bool baseClassOf(const RecTy *) const
Definition: Record.cpp:358
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
int64_t getValue() const
Definition: Record.h:714
void print(raw_ostream &OS, bool PrintSem=true) const
Definition: Record.cpp:1650
const std::map< std::string, Record * > & getClasses() const
Definition: Record.h:1660
BitsInit * getValueAsBitsInit(StringRef FieldName) const
Definition: Record.cpp:1814
RecordVal(Init *N, RecTy *T, unsigned P)
Definition: Record.cpp:1630
virtual Init * convertValue(UnsetInit *UI)
Definition: Record.cpp:144
RecTy * getType() const
Definition: Record.h:1355
ListInit * getValueAsListInit(StringRef FieldName) const
Definition: Record.cpp:1830
virtual OpInit * clone(std::vector< Init * > &Operands) const =0
const std::string getNameInitAsString() const
Definition: Record.h:1435
virtual std::string getAsString() const
getAsString - Convert this value to a string form.
Definition: Record.cpp:981
virtual Init * resolveReferences(Record &R, const RecordVal *RV) const
Definition: Record.cpp:515
Init * getArg(unsigned Num) const
Definition: Record.h:1297
void Profile(FoldingSetNodeID &ID) const
Definition: Record.cpp:1592
static std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:88
hash_code hash_value(const APFloat &Arg)
Definition: APFloat.cpp:2814
Init * getRHS() const
Definition: Record.h:967
virtual std::string getAsString() const
Definition: Record.cpp:338
virtual std::string getAsString() const
getAsString - Convert this value to a string form.
Definition: Record.cpp:583
void dump() const
Definition: Record.cpp:1981
virtual unsigned getBitNum() const
Definition: Record.h:546
const std::string & getName() const
Definition: Record.cpp:1320
virtual Init * getBit(unsigned Bit) const =0
DagInit * getValueAsDag(StringRef FieldName) const
Definition: Record.cpp:1968
void dump() const
Definition: Record.cpp:90
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:109
std::vector< std::string > getValueAsListOfStrings(StringRef FieldName) const
Definition: Record.cpp:1901
bool getValue() const
Definition: Record.h:634
virtual std::string getAsString() const
Definition: Record.cpp:140
virtual bool baseClassOf(const RecTy *) const
Definition: Record.cpp:122
virtual Init * convertInitListSlice(const std::vector< unsigned > &Elements) const
Definition: Record.cpp:1288
static BitsInit * get(ArrayRef< Init * > Range)
Definition: Record.cpp:458
void resolveReferencesTo(const RecordVal *RV)
Definition: Record.cpp:1722
iterator begin() const
Definition: StringRef.h:97
virtual Init * convertInitializerTo(RecTy *Ty) const =0
static unsigned getHashValue(const TableGenStringKey &Val)
Definition: Record.cpp:68
virtual unsigned getBitNum() const
Definition: Record.h:1118
virtual RecTy * getFieldType(const std::string &FieldName) const
Definition: Record.cpp:1264
Record * getDef(const std::string &Name) const
Definition: Record.h:1667
unsigned getSize() const
Definition: Record.h:801
#define P(N)
virtual Init * resolveReferences(Record &R, const RecordVal *RV) const
Definition: Record.cpp:1222
static VarInit * get(const std::string &VN, RecTy *T)
Definition: Record.cpp:1304
std::vector< Record * > getAllDerivedDefinitions(const std::string &ClassName) const
Definition: Record.cpp:2017
virtual Init * convertValue(UnsetInit *UI)
Definition: Record.h:105
virtual bool baseClassOf(const RecTy *) const
Definition: Record.cpp:299
virtual Init * getBit(unsigned Bit) const
Definition: Record.cpp:1326
RecordVector DefPrototypes
Definition: Record.h:1639
TypedInit * getVariable() const
Definition: Record.h:1156
virtual Init * getBit(unsigned Bit) const
Definition: Record.cpp:1445
virtual Init * convertInitializerBitRange(const std::vector< unsigned > &Bits) const
Definition: Record.cpp:588
const RecordVal * getValue(const Init *Name) const
Definition: Record.h:1463
virtual Init * convertValue(UnsetInit *UI)
Definition: Record.h:153
iterator begin() const
Definition: ArrayRef.h:97
virtual bool typeIsConvertibleTo(const RecTy *RHS) const =0
std::vector< int64_t > getValueAsListOfInts(StringRef FieldName) const
Definition: Record.cpp:1882
static TableGenStringKey getTombstoneKey()
Definition: Record.cpp:64
RecTyKind getRecTyKind() const
Definition: Record.h:88
static BitInit * get(bool V)
Definition: Record.cpp:440
TableGenStringKey(const std::string &str)
Definition: Record.cpp:45
static FieldInit * get(Init *R, const std::string &FN)
Definition: Record.cpp:1488
static bool canFitInBitfield(int64_t Value, unsigned NumBits)
Definition: Record.cpp:160
const std::vector< Record * > & getSuperClasses() const
Definition: Record.h:1451
Record * getDef() const
Definition: Record.h:1193
DefInit * getDefInit()
get the corresponding DefInit.
Definition: Record.cpp:1680
static std::string itostr(int64_t X)
Definition: StringExtras.h:104
virtual std::string getAsString() const
getAsString - Convert this value to a string form.
Definition: Record.cpp:1610
virtual std::string getAsString() const
getAsString - Convert this value to a string form.
Definition: Record.cpp:703
std::vector< Record * > getValueAsListOfDefs(StringRef FieldName) const
Definition: Record.cpp:1847
virtual Init * resolveReferences(Record &R, const RecordVal *RV) const
Definition: Record.h:532
Init * QualifyName(Record &CurRec, MultiClass *CurMultiClass, Init *Name, const std::string &Scoper)
Definition: Record.cpp:2033
const Init * getNameInit() const
Definition: Record.h:1349
int64_t getValueAsInt(StringRef FieldName) const
Definition: Record.cpp:1865
virtual Init * convertInitializerBitRange(const std::vector< unsigned > &Bits) const
Definition: Record.cpp:480
virtual Init * Fold(Record *CurRec, MultiClass *CurMultiClass) const =0
virtual std::string getAsString() const
getAsString - Convert this value to a string form.
Definition: Record.cpp:1433
virtual std::string getAsString() const
getAsString - Convert this value to a string form.
Definition: Record.cpp:1407
virtual Init * Fold(Record *CurRec, MultiClass *CurMultiClass) const
Definition: Record.cpp:751
static Init * EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg, RecTy *Type, Record *CurRec, MultiClass *CurMultiClass)
Definition: Record.cpp:1023
void addDef(Record *R)
Definition: Record.h:1676
ArrayRef< SMLoc > getLoc() const
Definition: Record.h:1442
static bool isEqual(const TableGenStringKey &LHS, const TableGenStringKey &RHS)
Definition: Record.cpp:72
virtual Init * getFieldInit(Record &R, const RecordVal *RV, const std::string &FieldName) const
Definition: Record.cpp:1478
static VarBitInit * get(TypedInit *T, unsigned B)
Definition: Record.cpp:1394
static StringInit * get(StringRef)
Definition: Record.cpp:602
static RecordRecTy * get(Record *R)
Definition: Record.cpp:334
void dump() const
Definition: Record.cpp:1648
Init * getNameInit() const
Definition: Record.h:1432
static DefInit * get(Record *)
Definition: Record.cpp:1468
void dump() const
Definition: Record.cpp:1745
const std::vector< RecordVal > & getValues() const
Definition: Record.h:1450
virtual Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const
Definition: Record.cpp:1451
TableGenStringKey(const char *str)
Definition: Record.cpp:46
unsigned getElementNum() const
Definition: Record.h:1157
virtual Init * convertValue(UnsetInit *UI)
Definition: Record.h:342
static void ProfileListInit(FoldingSetNodeID &ID, ArrayRef< Init * > Range, RecTy *EltTy)
Definition: Record.cpp:610
bool isTemplateArg(Init *Name) const
Definition: Record.h:1454
virtual Init * convertInitListSlice(const std::vector< unsigned > &Elements) const
Definition: Record.cpp:650
virtual Init * resolveReferences(Record &R, const RecordVal *RV) const
Definition: Record.cpp:861
An opaque object representing a hash code.
Definition: Hashing.h:79
static Init * fixBitInit(const RecordVal *RV, Init *Before, Init *After)
Definition: Record.cpp:506
LLVM_ATTRIBUTE_NORETURN void PrintFatalError(const std::string &Msg)
RecTy * getElementType() const
Definition: Record.h:303
RecordKeeper & getRecords() const
Definition: Record.h:1543
void dump() const
Definition: Record.cpp:1995
static DagInit * get(Init *V, const std::string &VN, ArrayRef< Init * > ArgRange, ArrayRef< std::string > NameRange)
Definition: Record.cpp:1554
static MachineInstr * getDef(unsigned Reg, const MachineRegisterInfo *MRI)
unsigned getNumBits() const
Definition: Record.h:190
virtual Init * getBitVar() const
Definition: Record.h:542
void removeDef(const std::string &Name)
Definition: Record.h:1690
virtual Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const
Definition: Record.cpp:1506
virtual Init * convertValue(UnsetInit *UI)
Definition: Record.h:382
std::string getName(ID id, ArrayRef< Type * > Tys=None)
Definition: Function.cpp:400
bool isSubClassOf(const Record *R) const
Definition: Record.h:1513
static UnsetInit * get()
Definition: Record.cpp:433
virtual Init * getFieldInit(Record &R, const RecordVal *RV, const std::string &FieldName) const
Definition: Record.cpp:1366
void addValue(const RecordVal &RV)
Definition: Record.h:1488
static TernOpInit * get(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type)
Definition: Record.cpp:995
virtual Init * resolveReferences(Record &R, const RecordVal *RV) const
Definition: Record.cpp:1387
static VarListElementInit * get(TypedInit *T, unsigned E)
Definition: Record.cpp:1419
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
virtual Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const
Definition: Record.cpp:690
RecTy * resolveTypes(RecTy *T1, RecTy *T2)
Definition: Record.cpp:377
virtual Init * convertValue(UnsetInit *UI)
Definition: Record.h:229
static ListInit * get(ArrayRef< Init * > Range, RecTy *EltTy)
Definition: Record.cpp:623
const std::map< std::string, Record * > & getDefs() const
Definition: Record.h:1661
void removeClass(const std::string &Name)
Definition: Record.h:1684
void AddString(StringRef String)
Definition: FoldingSet.cpp:87
UnaryOp getOpcode() const
Definition: Record.h:914
virtual Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const
Definition: Record.cpp:1332
reverse_iterator rend(StringRef path)
Get reverse end iterator over path.
Definition: Path.h:101
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:1688
Record * getRecord() const
Definition: Record.h:380
bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const
Definition: Record.cpp:1947
virtual Init * resolveReferences(Record &R, const RecordVal *RV) const
Definition: Record.cpp:668
const std::string & getName() const
Definition: Record.cpp:1642
virtual Init * getBit(unsigned Bit) const
Definition: Record.cpp:734
Init * getNameInit() const
Definition: Record.h:1065
LLVM Value Representation.
Definition: Value.h:66
virtual Init * resolveReferences(Record &R, const RecordVal *RV) const
Definition: Record.cpp:1522
virtual std::string getAsString() const =0
getAsString - Convert this value to a string form.
static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, const std::string &VN, ArrayRef< Init * > ArgRange, ArrayRef< std::string > NameRange)
Definition: Record.cpp:1537
virtual Init * getOperand(int i) const =0
void addClass(Record *R)
Definition: Record.h:1671
iterator end() const
Definition: StringRef.h:99
TernaryOp getOpcode() const
Definition: Record.h:1023
unsigned getPrefix() const
Definition: Record.h:1354
void dump() const
Definition: Record.cpp:429
void setName(Init *Name)
Definition: Record.cpp:1692
virtual Init * convertValue(UnsetInit *UI)
Definition: Record.h:266
virtual bool baseClassOf(const RecTy *) const
Definition: Record.cpp:233
virtual std::string getAsString() const =0
ListRecTy * getListTy()
getListTy - Returns the type representing list<this>.
Definition: Record.cpp:92
virtual bool baseClassOf(const RecTy *) const
Definition: Record.cpp:98
#define T1
virtual Init * convertInitializerBitRange(const std::vector< unsigned > &Bits) const
Definition: Record.cpp:1272
static IntRecTy * get()
Definition: Record.h:227
virtual std::string getAsString() const
Definition: Record.cpp:270