Support using DebugLoc's in a DenseMap.
[llvm/stm8.git] / utils / TableGen / Record.cpp
blobabbbafed09d87574ff0351d03fa7fb9a966289b5
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.
12 //===----------------------------------------------------------------------===//
14 #include "Record.h"
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/ADT/StringExtras.h"
19 using namespace llvm;
21 //===----------------------------------------------------------------------===//
22 // Type implementations
23 //===----------------------------------------------------------------------===//
25 void RecTy::dump() const { print(errs()); }
27 Init *BitRecTy::convertValue(BitsInit *BI) {
28 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
29 return BI->getBit(0);
32 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
33 return RHS->getNumBits() == 1;
36 Init *BitRecTy::convertValue(IntInit *II) {
37 int64_t Val = II->getValue();
38 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
40 return new BitInit(Val != 0);
43 Init *BitRecTy::convertValue(TypedInit *VI) {
44 if (dynamic_cast<BitRecTy*>(VI->getType()))
45 return VI; // Accept variable if it is already of bit type!
46 return 0;
49 std::string BitsRecTy::getAsString() const {
50 return "bits<" + utostr(Size) + ">";
53 Init *BitsRecTy::convertValue(UnsetInit *UI) {
54 BitsInit *Ret = new BitsInit(Size);
56 for (unsigned i = 0; i != Size; ++i)
57 Ret->setBit(i, new UnsetInit());
58 return Ret;
61 Init *BitsRecTy::convertValue(BitInit *UI) {
62 if (Size != 1) return 0; // Can only convert single bit.
63 BitsInit *Ret = new BitsInit(1);
64 Ret->setBit(0, UI);
65 return Ret;
68 /// canFitInBitfield - Return true if the number of bits is large enough to hold
69 /// the integer value.
70 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
71 if (Value >= 0) {
72 if (Value & ~((1LL << NumBits) - 1))
73 return false;
74 } else if ((Value >> NumBits) != -1 || (Value & (1LL << (NumBits-1))) == 0) {
75 return false;
78 return true;
81 /// convertValue from Int initializer to bits type: Split the integer up into the
82 /// appropriate bits.
83 ///
84 Init *BitsRecTy::convertValue(IntInit *II) {
85 int64_t Value = II->getValue();
86 // Make sure this bitfield is large enough to hold the integer value.
87 if (!canFitInBitfield(Value, Size))
88 return 0;
90 BitsInit *Ret = new BitsInit(Size);
91 for (unsigned i = 0; i != Size; ++i)
92 Ret->setBit(i, new BitInit(Value & (1LL << i)));
94 return Ret;
97 Init *BitsRecTy::convertValue(BitsInit *BI) {
98 // If the number of bits is right, return it. Otherwise we need to expand or
99 // truncate.
100 if (BI->getNumBits() == Size) return BI;
101 return 0;
104 Init *BitsRecTy::convertValue(TypedInit *VI) {
105 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
106 if (BRT->Size == Size) {
107 BitsInit *Ret = new BitsInit(Size);
108 for (unsigned i = 0; i != Size; ++i)
109 Ret->setBit(i, new VarBitInit(VI, i));
110 return Ret;
113 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
114 BitsInit *Ret = new BitsInit(1);
115 Ret->setBit(0, VI);
116 return Ret;
119 if (TernOpInit *Tern = dynamic_cast<TernOpInit*>(VI)) {
120 if (Tern->getOpcode() == TernOpInit::IF) {
121 Init *LHS = Tern->getLHS();
122 Init *MHS = Tern->getMHS();
123 Init *RHS = Tern->getRHS();
125 IntInit *MHSi = dynamic_cast<IntInit*>(MHS);
126 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
128 if (MHSi && RHSi) {
129 int64_t MHSVal = MHSi->getValue();
130 int64_t RHSVal = RHSi->getValue();
132 if (canFitInBitfield(MHSVal, Size) && canFitInBitfield(RHSVal, Size)) {
133 BitsInit *Ret = new BitsInit(Size);
135 for (unsigned i = 0; i != Size; ++i)
136 Ret->setBit(i, new TernOpInit(TernOpInit::IF, LHS,
137 new IntInit((MHSVal & (1LL << i)) ? 1 : 0),
138 new IntInit((RHSVal & (1LL << i)) ? 1 : 0),
139 VI->getType()));
141 return Ret;
143 } else {
144 BitsInit *MHSbs = dynamic_cast<BitsInit*>(MHS);
145 BitsInit *RHSbs = dynamic_cast<BitsInit*>(RHS);
147 if (MHSbs && RHSbs) {
148 BitsInit *Ret = new BitsInit(Size);
150 for (unsigned i = 0; i != Size; ++i)
151 Ret->setBit(i, new TernOpInit(TernOpInit::IF, LHS,
152 MHSbs->getBit(i),
153 RHSbs->getBit(i),
154 VI->getType()));
156 return Ret;
162 return 0;
165 Init *IntRecTy::convertValue(BitInit *BI) {
166 return new IntInit(BI->getValue());
169 Init *IntRecTy::convertValue(BitsInit *BI) {
170 int64_t Result = 0;
171 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
172 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
173 Result |= Bit->getValue() << i;
174 } else {
175 return 0;
177 return new IntInit(Result);
180 Init *IntRecTy::convertValue(TypedInit *TI) {
181 if (TI->getType()->typeIsConvertibleTo(this))
182 return TI; // Accept variable if already of the right type!
183 return 0;
186 Init *StringRecTy::convertValue(UnOpInit *BO) {
187 if (BO->getOpcode() == UnOpInit::CAST) {
188 Init *L = BO->getOperand()->convertInitializerTo(this);
189 if (L == 0) return 0;
190 if (L != BO->getOperand())
191 return new UnOpInit(UnOpInit::CAST, L, new StringRecTy);
192 return BO;
195 return convertValue((TypedInit*)BO);
198 Init *StringRecTy::convertValue(BinOpInit *BO) {
199 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
200 Init *L = BO->getLHS()->convertInitializerTo(this);
201 Init *R = BO->getRHS()->convertInitializerTo(this);
202 if (L == 0 || R == 0) return 0;
203 if (L != BO->getLHS() || R != BO->getRHS())
204 return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy);
205 return BO;
208 return convertValue((TypedInit*)BO);
212 Init *StringRecTy::convertValue(TypedInit *TI) {
213 if (dynamic_cast<StringRecTy*>(TI->getType()))
214 return TI; // Accept variable if already of the right type!
215 return 0;
218 std::string ListRecTy::getAsString() const {
219 return "list<" + Ty->getAsString() + ">";
222 Init *ListRecTy::convertValue(ListInit *LI) {
223 std::vector<Init*> Elements;
225 // Verify that all of the elements of the list are subclasses of the
226 // appropriate class!
227 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
228 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
229 Elements.push_back(CI);
230 else
231 return 0;
233 ListRecTy *LType = dynamic_cast<ListRecTy*>(LI->getType());
234 if (LType == 0) {
235 return 0;
238 return new ListInit(Elements, new ListRecTy(Ty));
241 Init *ListRecTy::convertValue(TypedInit *TI) {
242 // Ensure that TI is compatible with our class.
243 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
244 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
245 return TI;
246 return 0;
249 Init *CodeRecTy::convertValue(TypedInit *TI) {
250 if (TI->getType()->typeIsConvertibleTo(this))
251 return TI;
252 return 0;
255 Init *DagRecTy::convertValue(TypedInit *TI) {
256 if (TI->getType()->typeIsConvertibleTo(this))
257 return TI;
258 return 0;
261 Init *DagRecTy::convertValue(UnOpInit *BO) {
262 if (BO->getOpcode() == UnOpInit::CAST) {
263 Init *L = BO->getOperand()->convertInitializerTo(this);
264 if (L == 0) return 0;
265 if (L != BO->getOperand())
266 return new UnOpInit(UnOpInit::CAST, L, new DagRecTy);
267 return BO;
269 return 0;
272 Init *DagRecTy::convertValue(BinOpInit *BO) {
273 if (BO->getOpcode() == BinOpInit::CONCAT) {
274 Init *L = BO->getLHS()->convertInitializerTo(this);
275 Init *R = BO->getRHS()->convertInitializerTo(this);
276 if (L == 0 || R == 0) return 0;
277 if (L != BO->getLHS() || R != BO->getRHS())
278 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
279 return BO;
281 return 0;
284 std::string RecordRecTy::getAsString() const {
285 return Rec->getName();
288 Init *RecordRecTy::convertValue(DefInit *DI) {
289 // Ensure that DI is a subclass of Rec.
290 if (!DI->getDef()->isSubClassOf(Rec))
291 return 0;
292 return DI;
295 Init *RecordRecTy::convertValue(TypedInit *TI) {
296 // Ensure that TI is compatible with Rec.
297 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
298 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
299 RRT->getRecord() == getRecord())
300 return TI;
301 return 0;
304 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
305 if (Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec))
306 return true;
308 const std::vector<Record*> &SC = Rec->getSuperClasses();
309 for (unsigned i = 0, e = SC.size(); i != e; ++i)
310 if (RHS->getRecord()->isSubClassOf(SC[i]))
311 return true;
313 return false;
317 /// resolveTypes - Find a common type that T1 and T2 convert to.
318 /// Return 0 if no such type exists.
320 RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
321 if (!T1->typeIsConvertibleTo(T2)) {
322 if (!T2->typeIsConvertibleTo(T1)) {
323 // If one is a Record type, check superclasses
324 RecordRecTy *RecTy1 = dynamic_cast<RecordRecTy*>(T1);
325 if (RecTy1) {
326 // See if T2 inherits from a type T1 also inherits from
327 const std::vector<Record *> &T1SuperClasses =
328 RecTy1->getRecord()->getSuperClasses();
329 for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(),
330 iend = T1SuperClasses.end();
331 i != iend;
332 ++i) {
333 RecordRecTy *SuperRecTy1 = new RecordRecTy(*i);
334 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
335 if (NewType1 != 0) {
336 if (NewType1 != SuperRecTy1) {
337 delete SuperRecTy1;
339 return NewType1;
343 RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2);
344 if (RecTy2) {
345 // See if T1 inherits from a type T2 also inherits from
346 const std::vector<Record *> &T2SuperClasses =
347 RecTy2->getRecord()->getSuperClasses();
348 for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(),
349 iend = T2SuperClasses.end();
350 i != iend;
351 ++i) {
352 RecordRecTy *SuperRecTy2 = new RecordRecTy(*i);
353 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
354 if (NewType2 != 0) {
355 if (NewType2 != SuperRecTy2) {
356 delete SuperRecTy2;
358 return NewType2;
362 return 0;
364 return T2;
366 return T1;
370 //===----------------------------------------------------------------------===//
371 // Initializer implementations
372 //===----------------------------------------------------------------------===//
374 void Init::dump() const { return print(errs()); }
376 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
377 BitsInit *BI = new BitsInit(Bits.size());
378 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
379 if (Bits[i] >= getNumBits()) {
380 delete BI;
381 return 0;
383 BI->setBit(i, getBit(Bits[i]));
385 return BI;
388 std::string BitsInit::getAsString() const {
389 std::string Result = "{ ";
390 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
391 if (i) Result += ", ";
392 if (Init *Bit = getBit(e-i-1))
393 Result += Bit->getAsString();
394 else
395 Result += "*";
397 return Result + " }";
400 // resolveReferences - If there are any field references that refer to fields
401 // that have been filled in, we can propagate the values now.
403 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
404 bool Changed = false;
405 BitsInit *New = new BitsInit(getNumBits());
407 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
408 Init *B;
409 Init *CurBit = getBit(i);
411 do {
412 B = CurBit;
413 CurBit = CurBit->resolveReferences(R, RV);
414 Changed |= B != CurBit;
415 } while (B != CurBit);
416 New->setBit(i, CurBit);
419 if (Changed)
420 return New;
421 delete New;
422 return this;
425 std::string IntInit::getAsString() const {
426 return itostr(Value);
429 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
430 BitsInit *BI = new BitsInit(Bits.size());
432 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
433 if (Bits[i] >= 64) {
434 delete BI;
435 return 0;
437 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
439 return BI;
442 Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
443 std::vector<Init*> Vals;
444 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
445 if (Elements[i] >= getSize())
446 return 0;
447 Vals.push_back(getElement(Elements[i]));
449 return new ListInit(Vals, getType());
452 Record *ListInit::getElementAsRecord(unsigned i) const {
453 assert(i < Values.size() && "List element index out of range!");
454 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
455 if (DI == 0) throw "Expected record in list!";
456 return DI->getDef();
459 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
460 std::vector<Init*> Resolved;
461 Resolved.reserve(getSize());
462 bool Changed = false;
464 for (unsigned i = 0, e = getSize(); i != e; ++i) {
465 Init *E;
466 Init *CurElt = getElement(i);
468 do {
469 E = CurElt;
470 CurElt = CurElt->resolveReferences(R, RV);
471 Changed |= E != CurElt;
472 } while (E != CurElt);
473 Resolved.push_back(E);
476 if (Changed)
477 return new ListInit(Resolved, getType());
478 return this;
481 Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
482 unsigned Elt) {
483 if (Elt >= getSize())
484 return 0; // Out of range reference.
485 Init *E = getElement(Elt);
486 // If the element is set to some value, or if we are resolving a reference
487 // to a specific variable and that variable is explicitly unset, then
488 // replace the VarListElementInit with it.
489 if (IRV || !dynamic_cast<UnsetInit*>(E))
490 return E;
491 return 0;
494 std::string ListInit::getAsString() const {
495 std::string Result = "[";
496 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
497 if (i) Result += ", ";
498 Result += Values[i]->getAsString();
500 return Result + "]";
503 Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
504 unsigned Bit) {
505 Init *Folded = Fold(&R, 0);
507 if (Folded != this) {
508 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
509 if (Typed) {
510 return Typed->resolveBitReference(R, IRV, Bit);
514 return 0;
517 Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
518 unsigned Elt) {
519 Init *Folded = Fold(&R, 0);
521 if (Folded != this) {
522 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
523 if (Typed) {
524 return Typed->resolveListElementReference(R, IRV, Elt);
528 return 0;
531 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
532 switch (getOpcode()) {
533 default: assert(0 && "Unknown unop");
534 case CAST: {
535 if (getType()->getAsString() == "string") {
536 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
537 if (LHSs) {
538 return LHSs;
541 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
542 if (LHSd) {
543 return new StringInit(LHSd->getDef()->getName());
545 } else {
546 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
547 if (LHSs) {
548 std::string Name = LHSs->getValue();
550 // From TGParser::ParseIDValue
551 if (CurRec) {
552 if (const RecordVal *RV = CurRec->getValue(Name)) {
553 if (RV->getType() != getType())
554 throw "type mismatch in cast";
555 return new VarInit(Name, RV->getType());
558 std::string TemplateArgName = CurRec->getName()+":"+Name;
559 if (CurRec->isTemplateArg(TemplateArgName)) {
560 const RecordVal *RV = CurRec->getValue(TemplateArgName);
561 assert(RV && "Template arg doesn't exist??");
563 if (RV->getType() != getType())
564 throw "type mismatch in cast";
566 return new VarInit(TemplateArgName, RV->getType());
570 if (CurMultiClass) {
571 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
572 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
573 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
574 assert(RV && "Template arg doesn't exist??");
576 if (RV->getType() != getType())
577 throw "type mismatch in cast";
579 return new VarInit(MCName, RV->getType());
583 if (Record *D = (CurRec->getRecords()).getDef(Name))
584 return new DefInit(D);
586 errs() << "Variable not defined: '" + Name + "'\n";
587 assert(0 && "Variable not found");
588 return 0;
591 break;
593 case HEAD: {
594 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
595 if (LHSl) {
596 if (LHSl->getSize() == 0) {
597 assert(0 && "Empty list in car");
598 return 0;
600 return LHSl->getElement(0);
602 break;
604 case TAIL: {
605 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
606 if (LHSl) {
607 if (LHSl->getSize() == 0) {
608 assert(0 && "Empty list in cdr");
609 return 0;
611 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(),
612 LHSl->getType());
613 return Result;
615 break;
617 case EMPTY: {
618 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
619 if (LHSl) {
620 if (LHSl->getSize() == 0) {
621 return new IntInit(1);
622 } else {
623 return new IntInit(0);
626 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
627 if (LHSs) {
628 if (LHSs->getValue().empty()) {
629 return new IntInit(1);
630 } else {
631 return new IntInit(0);
635 break;
638 return this;
641 Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
642 Init *lhs = LHS->resolveReferences(R, RV);
644 if (LHS != lhs)
645 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
646 return Fold(&R, 0);
649 std::string UnOpInit::getAsString() const {
650 std::string Result;
651 switch (Opc) {
652 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
653 case HEAD: Result = "!head"; break;
654 case TAIL: Result = "!tail"; break;
655 case EMPTY: Result = "!empty"; break;
657 return Result + "(" + LHS->getAsString() + ")";
660 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
661 switch (getOpcode()) {
662 default: assert(0 && "Unknown binop");
663 case CONCAT: {
664 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
665 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
666 if (LHSs && RHSs) {
667 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
668 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
669 if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef())
670 throw "Concated Dag operators do not match!";
671 std::vector<Init*> Args;
672 std::vector<std::string> ArgNames;
673 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
674 Args.push_back(LHSs->getArg(i));
675 ArgNames.push_back(LHSs->getArgName(i));
677 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
678 Args.push_back(RHSs->getArg(i));
679 ArgNames.push_back(RHSs->getArgName(i));
681 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
683 break;
685 case STRCONCAT: {
686 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
687 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
688 if (LHSs && RHSs)
689 return new StringInit(LHSs->getValue() + RHSs->getValue());
690 break;
692 case EQ: {
693 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
694 // to string objects.
695 IntInit* L =
696 dynamic_cast<IntInit*>(LHS->convertInitializerTo(new IntRecTy()));
697 IntInit* R =
698 dynamic_cast<IntInit*>(RHS->convertInitializerTo(new IntRecTy()));
700 if (L && R)
701 return new IntInit(L->getValue() == R->getValue());
703 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
704 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
706 // Make sure we've resolved
707 if (LHSs && RHSs)
708 return new IntInit(LHSs->getValue() == RHSs->getValue());
710 break;
712 case SHL:
713 case SRA:
714 case SRL: {
715 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
716 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
717 if (LHSi && RHSi) {
718 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
719 int64_t Result;
720 switch (getOpcode()) {
721 default: assert(0 && "Bad opcode!");
722 case SHL: Result = LHSv << RHSv; break;
723 case SRA: Result = LHSv >> RHSv; break;
724 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
726 return new IntInit(Result);
728 break;
731 return this;
734 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
735 Init *lhs = LHS->resolveReferences(R, RV);
736 Init *rhs = RHS->resolveReferences(R, RV);
738 if (LHS != lhs || RHS != rhs)
739 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
740 return Fold(&R, 0);
743 std::string BinOpInit::getAsString() const {
744 std::string Result;
745 switch (Opc) {
746 case CONCAT: Result = "!con"; break;
747 case SHL: Result = "!shl"; break;
748 case SRA: Result = "!sra"; break;
749 case SRL: Result = "!srl"; break;
750 case EQ: Result = "!eq"; break;
751 case STRCONCAT: Result = "!strconcat"; break;
753 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
756 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
757 Record *CurRec, MultiClass *CurMultiClass);
759 static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
760 RecTy *Type, Record *CurRec,
761 MultiClass *CurMultiClass) {
762 std::vector<Init *> NewOperands;
764 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
766 // If this is a dag, recurse
767 if (TArg && TArg->getType()->getAsString() == "dag") {
768 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
769 CurRec, CurMultiClass);
770 if (Result != 0) {
771 return Result;
772 } else {
773 return 0;
777 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
778 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
780 if (RHSoo) {
781 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
782 Type, CurRec, CurMultiClass);
783 if (Result != 0) {
784 NewOperands.push_back(Result);
785 } else {
786 NewOperands.push_back(Arg);
788 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
789 NewOperands.push_back(Arg);
790 } else {
791 NewOperands.push_back(RHSo->getOperand(i));
795 // Now run the operator and use its result as the new leaf
796 OpInit *NewOp = RHSo->clone(NewOperands);
797 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
798 if (NewVal != NewOp) {
799 delete NewOp;
800 return NewVal;
802 return 0;
805 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
806 Record *CurRec, MultiClass *CurMultiClass) {
807 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
808 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
810 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
811 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
813 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
815 if (!RHSo) {
816 errs() << "!foreach requires an operator\n";
817 assert(0 && "No operator for !foreach");
820 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
822 if (!LHSt) {
823 errs() << "!foreach requires typed variable\n";
824 assert(0 && "No typed variable for !foreach");
827 if ((MHSd && DagType) || (MHSl && ListType)) {
828 if (MHSd) {
829 Init *Val = MHSd->getOperator();
830 Init *Result = EvaluateOperation(RHSo, LHS, Val,
831 Type, CurRec, CurMultiClass);
832 if (Result != 0) {
833 Val = Result;
836 std::vector<std::pair<Init *, std::string> > args;
837 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
838 Init *Arg;
839 std::string ArgName;
840 Arg = MHSd->getArg(i);
841 ArgName = MHSd->getArgName(i);
843 // Process args
844 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
845 CurRec, CurMultiClass);
846 if (Result != 0) {
847 Arg = Result;
850 // TODO: Process arg names
851 args.push_back(std::make_pair(Arg, ArgName));
854 return new DagInit(Val, "", args);
856 if (MHSl) {
857 std::vector<Init *> NewOperands;
858 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
860 for (ListInit::iterator li = NewList.begin(),
861 liend = NewList.end();
862 li != liend;
863 ++li) {
864 Init *Item = *li;
865 NewOperands.clear();
866 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
867 // First, replace the foreach variable with the list item
868 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
869 NewOperands.push_back(Item);
870 } else {
871 NewOperands.push_back(RHSo->getOperand(i));
875 // Now run the operator and use its result as the new list item
876 OpInit *NewOp = RHSo->clone(NewOperands);
877 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
878 if (NewItem != NewOp) {
879 *li = NewItem;
880 delete NewOp;
883 return new ListInit(NewList, MHSl->getType());
886 return 0;
889 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
890 switch (getOpcode()) {
891 default: assert(0 && "Unknown binop");
892 case SUBST: {
893 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
894 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
895 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
897 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
898 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
899 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
901 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
902 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
903 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
905 if ((LHSd && MHSd && RHSd)
906 || (LHSv && MHSv && RHSv)
907 || (LHSs && MHSs && RHSs)) {
908 if (RHSd) {
909 Record *Val = RHSd->getDef();
910 if (LHSd->getAsString() == RHSd->getAsString()) {
911 Val = MHSd->getDef();
913 return new DefInit(Val);
915 if (RHSv) {
916 std::string Val = RHSv->getName();
917 if (LHSv->getAsString() == RHSv->getAsString()) {
918 Val = MHSv->getName();
920 return new VarInit(Val, getType());
922 if (RHSs) {
923 std::string Val = RHSs->getValue();
925 std::string::size_type found;
926 std::string::size_type idx = 0;
927 do {
928 found = Val.find(LHSs->getValue(), idx);
929 if (found != std::string::npos) {
930 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
932 idx = found + MHSs->getValue().size();
933 } while (found != std::string::npos);
935 return new StringInit(Val);
938 break;
941 case FOREACH: {
942 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
943 CurRec, CurMultiClass);
944 if (Result != 0) {
945 return Result;
947 break;
950 case IF: {
951 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
952 if (Init *I = LHS->convertInitializerTo(new IntRecTy()))
953 LHSi = dynamic_cast<IntInit*>(I);
954 if (LHSi) {
955 if (LHSi->getValue()) {
956 return MHS;
957 } else {
958 return RHS;
961 break;
965 return this;
968 Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
969 Init *lhs = LHS->resolveReferences(R, RV);
971 if (Opc == IF && lhs != LHS) {
972 IntInit *Value = dynamic_cast<IntInit*>(lhs);
973 if (Init *I = lhs->convertInitializerTo(new IntRecTy()))
974 Value = dynamic_cast<IntInit*>(I);
975 if (Value != 0) {
976 // Short-circuit
977 if (Value->getValue()) {
978 Init *mhs = MHS->resolveReferences(R, RV);
979 return (new TernOpInit(getOpcode(), lhs, mhs,
980 RHS, getType()))->Fold(&R, 0);
981 } else {
982 Init *rhs = RHS->resolveReferences(R, RV);
983 return (new TernOpInit(getOpcode(), lhs, MHS,
984 rhs, getType()))->Fold(&R, 0);
989 Init *mhs = MHS->resolveReferences(R, RV);
990 Init *rhs = RHS->resolveReferences(R, RV);
992 if (LHS != lhs || MHS != mhs || RHS != rhs)
993 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
994 return Fold(&R, 0);
997 std::string TernOpInit::getAsString() const {
998 std::string Result;
999 switch (Opc) {
1000 case SUBST: Result = "!subst"; break;
1001 case FOREACH: Result = "!foreach"; break;
1002 case IF: Result = "!if"; break;
1004 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
1005 + RHS->getAsString() + ")";
1008 RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
1009 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
1010 if (RecordType) {
1011 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
1012 if (Field) {
1013 return Field->getType();
1016 return 0;
1019 Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
1020 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
1021 if (T == 0) return 0; // Cannot subscript a non-bits variable.
1022 unsigned NumBits = T->getNumBits();
1024 BitsInit *BI = new BitsInit(Bits.size());
1025 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1026 if (Bits[i] >= NumBits) {
1027 delete BI;
1028 return 0;
1030 BI->setBit(i, new VarBitInit(this, Bits[i]));
1032 return BI;
1035 Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
1036 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
1037 if (T == 0) return 0; // Cannot subscript a non-list variable.
1039 if (Elements.size() == 1)
1040 return new VarListElementInit(this, Elements[0]);
1042 std::vector<Init*> ListInits;
1043 ListInits.reserve(Elements.size());
1044 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1045 ListInits.push_back(new VarListElementInit(this, Elements[i]));
1046 return new ListInit(ListInits, T);
1050 Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1051 unsigned Bit) {
1052 if (R.isTemplateArg(getName())) return 0;
1053 if (IRV && IRV->getName() != getName()) return 0;
1055 RecordVal *RV = R.getValue(getName());
1056 assert(RV && "Reference to a non-existent variable?");
1057 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1058 BitsInit *BI = (BitsInit*)RV->getValue();
1060 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1061 Init *B = BI->getBit(Bit);
1063 // If the bit is set to some value, or if we are resolving a reference to a
1064 // specific variable and that variable is explicitly unset, then replace the
1065 // VarBitInit with it.
1066 if (IRV || !dynamic_cast<UnsetInit*>(B))
1067 return B;
1068 return 0;
1071 Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1072 unsigned Elt) {
1073 if (R.isTemplateArg(getName())) return 0;
1074 if (IRV && IRV->getName() != getName()) return 0;
1076 RecordVal *RV = R.getValue(getName());
1077 assert(RV && "Reference to a non-existent variable?");
1078 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
1079 if (!LI) {
1080 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
1081 assert(VI && "Invalid list element!");
1082 return new VarListElementInit(VI, Elt);
1085 if (Elt >= LI->getSize())
1086 return 0; // Out of range reference.
1087 Init *E = LI->getElement(Elt);
1088 // If the element is set to some value, or if we are resolving a reference
1089 // to a specific variable and that variable is explicitly unset, then
1090 // replace the VarListElementInit with it.
1091 if (IRV || !dynamic_cast<UnsetInit*>(E))
1092 return E;
1093 return 0;
1097 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1098 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1099 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1100 return RV->getType();
1101 return 0;
1104 Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1105 const std::string &FieldName) const {
1106 if (dynamic_cast<RecordRecTy*>(getType()))
1107 if (const RecordVal *Val = R.getValue(VarName)) {
1108 if (RV != Val && (RV || dynamic_cast<UnsetInit*>(Val->getValue())))
1109 return 0;
1110 Init *TheInit = Val->getValue();
1111 assert(TheInit != this && "Infinite loop detected!");
1112 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
1113 return I;
1114 else
1115 return 0;
1117 return 0;
1120 /// resolveReferences - This method is used by classes that refer to other
1121 /// variables which may not be defined at the time the expression is formed.
1122 /// If a value is set for the variable later, this method will be called on
1123 /// users of the value to allow the value to propagate out.
1125 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
1126 if (RecordVal *Val = R.getValue(VarName))
1127 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
1128 return Val->getValue();
1129 return this;
1132 std::string VarBitInit::getAsString() const {
1133 return TI->getAsString() + "{" + utostr(Bit) + "}";
1136 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1137 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
1138 return I;
1139 return this;
1142 std::string VarListElementInit::getAsString() const {
1143 return TI->getAsString() + "[" + utostr(Element) + "]";
1146 Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1147 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1148 getElementNum()))
1149 return I;
1150 return this;
1153 Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1154 unsigned Bit) {
1155 // FIXME: This should be implemented, to support references like:
1156 // bit B = AA[0]{1};
1157 return 0;
1160 Init *VarListElementInit::
1161 resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
1162 // FIXME: This should be implemented, to support references like:
1163 // int B = AA[0][1];
1164 return 0;
1167 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1168 if (const RecordVal *RV = Def->getValue(FieldName))
1169 return RV->getType();
1170 return 0;
1173 Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1174 const std::string &FieldName) const {
1175 return Def->getValue(FieldName)->getValue();
1179 std::string DefInit::getAsString() const {
1180 return Def->getName();
1183 Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1184 unsigned Bit) {
1185 if (Init *BitsVal = Rec->getFieldInit(R, RV, FieldName))
1186 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1187 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1188 Init *B = BI->getBit(Bit);
1190 if (dynamic_cast<BitInit*>(B)) // If the bit is set.
1191 return B; // Replace the VarBitInit with it.
1193 return 0;
1196 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1197 unsigned Elt) {
1198 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
1199 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1200 if (Elt >= LI->getSize()) return 0;
1201 Init *E = LI->getElement(Elt);
1203 // If the element is set to some value, or if we are resolving a
1204 // reference to a specific variable and that variable is explicitly
1205 // unset, then replace the VarListElementInit with it.
1206 if (RV || !dynamic_cast<UnsetInit*>(E))
1207 return E;
1209 return 0;
1212 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1213 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1215 Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName);
1216 if (BitsVal) {
1217 Init *BVR = BitsVal->resolveReferences(R, RV);
1218 return BVR->isComplete() ? BVR : this;
1221 if (NewRec != Rec) {
1222 return new FieldInit(NewRec, FieldName);
1224 return this;
1227 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1228 std::vector<Init*> NewArgs;
1229 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1230 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
1232 Init *Op = Val->resolveReferences(R, RV);
1234 if (Args != NewArgs || Op != Val)
1235 return new DagInit(Op, ValName, NewArgs, ArgNames);
1237 return this;
1241 std::string DagInit::getAsString() const {
1242 std::string Result = "(" + Val->getAsString();
1243 if (!ValName.empty())
1244 Result += ":" + ValName;
1245 if (Args.size()) {
1246 Result += " " + Args[0]->getAsString();
1247 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
1248 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
1249 Result += ", " + Args[i]->getAsString();
1250 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
1253 return Result + ")";
1257 //===----------------------------------------------------------------------===//
1258 // Other implementations
1259 //===----------------------------------------------------------------------===//
1261 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1262 : Name(N), Ty(T), Prefix(P) {
1263 Value = Ty->convertValue(new UnsetInit());
1264 assert(Value && "Cannot create unset value for current type!");
1267 void RecordVal::dump() const { errs() << *this; }
1269 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1270 if (getPrefix()) OS << "field ";
1271 OS << *getType() << " " << getName();
1273 if (getValue())
1274 OS << " = " << *getValue();
1276 if (PrintSem) OS << ";\n";
1279 unsigned Record::LastID = 0;
1281 void Record::setName(const std::string &Name) {
1282 if (TrackedRecords.getDef(getName()) == this) {
1283 TrackedRecords.removeDef(getName());
1284 this->Name = Name;
1285 TrackedRecords.addDef(this);
1286 } else {
1287 TrackedRecords.removeClass(getName());
1288 this->Name = Name;
1289 TrackedRecords.addClass(this);
1293 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1294 /// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1295 /// references.
1296 void Record::resolveReferencesTo(const RecordVal *RV) {
1297 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1298 if (Init *V = Values[i].getValue())
1299 Values[i].setValue(V->resolveReferences(*this, RV));
1303 void Record::dump() const { errs() << *this; }
1305 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
1306 OS << R.getName();
1308 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1309 if (!TArgs.empty()) {
1310 OS << "<";
1311 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1312 if (i) OS << ", ";
1313 const RecordVal *RV = R.getValue(TArgs[i]);
1314 assert(RV && "Template argument record not found??");
1315 RV->print(OS, false);
1317 OS << ">";
1320 OS << " {";
1321 const std::vector<Record*> &SC = R.getSuperClasses();
1322 if (!SC.empty()) {
1323 OS << "\t//";
1324 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1325 OS << " " << SC[i]->getName();
1327 OS << "\n";
1329 const std::vector<RecordVal> &Vals = R.getValues();
1330 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1331 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1332 OS << Vals[i];
1333 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1334 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1335 OS << Vals[i];
1337 return OS << "}\n";
1340 /// getValueInit - Return the initializer for a value with the specified name,
1341 /// or throw an exception if the field does not exist.
1343 Init *Record::getValueInit(StringRef FieldName) const {
1344 const RecordVal *R = getValue(FieldName);
1345 if (R == 0 || R->getValue() == 0)
1346 throw "Record `" + getName() + "' does not have a field named `" +
1347 FieldName.str() + "'!\n";
1348 return R->getValue();
1352 /// getValueAsString - This method looks up the specified field and returns its
1353 /// value as a string, throwing an exception if the field does not exist or if
1354 /// the value is not a string.
1356 std::string Record::getValueAsString(StringRef FieldName) const {
1357 const RecordVal *R = getValue(FieldName);
1358 if (R == 0 || R->getValue() == 0)
1359 throw "Record `" + getName() + "' does not have a field named `" +
1360 FieldName.str() + "'!\n";
1362 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1363 return SI->getValue();
1364 throw "Record `" + getName() + "', field `" + FieldName.str() +
1365 "' does not have a string initializer!";
1368 /// getValueAsBitsInit - This method looks up the specified field and returns
1369 /// its value as a BitsInit, throwing an exception if the field does not exist
1370 /// or if the value is not the right type.
1372 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
1373 const RecordVal *R = getValue(FieldName);
1374 if (R == 0 || R->getValue() == 0)
1375 throw "Record `" + getName() + "' does not have a field named `" +
1376 FieldName.str() + "'!\n";
1378 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1379 return BI;
1380 throw "Record `" + getName() + "', field `" + FieldName.str() +
1381 "' does not have a BitsInit initializer!";
1384 /// getValueAsListInit - This method looks up the specified field and returns
1385 /// its value as a ListInit, throwing an exception if the field does not exist
1386 /// or if the value is not the right type.
1388 ListInit *Record::getValueAsListInit(StringRef FieldName) const {
1389 const RecordVal *R = getValue(FieldName);
1390 if (R == 0 || R->getValue() == 0)
1391 throw "Record `" + getName() + "' does not have a field named `" +
1392 FieldName.str() + "'!\n";
1394 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1395 return LI;
1396 throw "Record `" + getName() + "', field `" + FieldName.str() +
1397 "' does not have a list initializer!";
1400 /// getValueAsListOfDefs - This method looks up the specified field and returns
1401 /// its value as a vector of records, throwing an exception if the field does
1402 /// not exist or if the value is not the right type.
1404 std::vector<Record*>
1405 Record::getValueAsListOfDefs(StringRef FieldName) const {
1406 ListInit *List = getValueAsListInit(FieldName);
1407 std::vector<Record*> Defs;
1408 for (unsigned i = 0; i < List->getSize(); i++) {
1409 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1410 Defs.push_back(DI->getDef());
1411 } else {
1412 throw "Record `" + getName() + "', field `" + FieldName.str() +
1413 "' list is not entirely DefInit!";
1416 return Defs;
1419 /// getValueAsInt - This method looks up the specified field and returns its
1420 /// value as an int64_t, throwing an exception if the field does not exist or if
1421 /// the value is not the right type.
1423 int64_t Record::getValueAsInt(StringRef FieldName) const {
1424 const RecordVal *R = getValue(FieldName);
1425 if (R == 0 || R->getValue() == 0)
1426 throw "Record `" + getName() + "' does not have a field named `" +
1427 FieldName.str() + "'!\n";
1429 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1430 return II->getValue();
1431 throw "Record `" + getName() + "', field `" + FieldName.str() +
1432 "' does not have an int initializer!";
1435 /// getValueAsListOfInts - This method looks up the specified field and returns
1436 /// its value as a vector of integers, throwing an exception if the field does
1437 /// not exist or if the value is not the right type.
1439 std::vector<int64_t>
1440 Record::getValueAsListOfInts(StringRef FieldName) const {
1441 ListInit *List = getValueAsListInit(FieldName);
1442 std::vector<int64_t> Ints;
1443 for (unsigned i = 0; i < List->getSize(); i++) {
1444 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1445 Ints.push_back(II->getValue());
1446 } else {
1447 throw "Record `" + getName() + "', field `" + FieldName.str() +
1448 "' does not have a list of ints initializer!";
1451 return Ints;
1454 /// getValueAsDef - This method looks up the specified field and returns its
1455 /// value as a Record, throwing an exception if the field does not exist or if
1456 /// the value is not the right type.
1458 Record *Record::getValueAsDef(StringRef FieldName) const {
1459 const RecordVal *R = getValue(FieldName);
1460 if (R == 0 || R->getValue() == 0)
1461 throw "Record `" + getName() + "' does not have a field named `" +
1462 FieldName.str() + "'!\n";
1464 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1465 return DI->getDef();
1466 throw "Record `" + getName() + "', field `" + FieldName.str() +
1467 "' does not have a def initializer!";
1470 /// getValueAsBit - This method looks up the specified field and returns its
1471 /// value as a bit, throwing an exception if the field does not exist or if
1472 /// the value is not the right type.
1474 bool Record::getValueAsBit(StringRef FieldName) const {
1475 const RecordVal *R = getValue(FieldName);
1476 if (R == 0 || R->getValue() == 0)
1477 throw "Record `" + getName() + "' does not have a field named `" +
1478 FieldName.str() + "'!\n";
1480 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1481 return BI->getValue();
1482 throw "Record `" + getName() + "', field `" + FieldName.str() +
1483 "' does not have a bit initializer!";
1486 /// getValueAsDag - This method looks up the specified field and returns its
1487 /// value as an Dag, throwing an exception if the field does not exist or if
1488 /// the value is not the right type.
1490 DagInit *Record::getValueAsDag(StringRef FieldName) const {
1491 const RecordVal *R = getValue(FieldName);
1492 if (R == 0 || R->getValue() == 0)
1493 throw "Record `" + getName() + "' does not have a field named `" +
1494 FieldName.str() + "'!\n";
1496 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1497 return DI;
1498 throw "Record `" + getName() + "', field `" + FieldName.str() +
1499 "' does not have a dag initializer!";
1502 std::string Record::getValueAsCode(StringRef FieldName) const {
1503 const RecordVal *R = getValue(FieldName);
1504 if (R == 0 || R->getValue() == 0)
1505 throw "Record `" + getName() + "' does not have a field named `" +
1506 FieldName.str() + "'!\n";
1508 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1509 return CI->getValue();
1510 throw "Record `" + getName() + "', field `" + FieldName.str() +
1511 "' does not have a code initializer!";
1515 void MultiClass::dump() const {
1516 errs() << "Record:\n";
1517 Rec.dump();
1519 errs() << "Defs:\n";
1520 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1521 rend = DefPrototypes.end();
1522 r != rend;
1523 ++r) {
1524 (*r)->dump();
1529 void RecordKeeper::dump() const { errs() << *this; }
1531 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
1532 OS << "------------- Classes -----------------\n";
1533 const std::map<std::string, Record*> &Classes = RK.getClasses();
1534 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
1535 E = Classes.end(); I != E; ++I)
1536 OS << "class " << *I->second;
1538 OS << "------------- Defs -----------------\n";
1539 const std::map<std::string, Record*> &Defs = RK.getDefs();
1540 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
1541 E = Defs.end(); I != E; ++I)
1542 OS << "def " << *I->second;
1543 return OS;
1547 /// getAllDerivedDefinitions - This method returns all concrete definitions
1548 /// that derive from the specified class name. If a class with the specified
1549 /// name does not exist, an error is printed and true is returned.
1550 std::vector<Record*>
1551 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1552 Record *Class = getClass(ClassName);
1553 if (!Class)
1554 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
1556 std::vector<Record*> Defs;
1557 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1558 E = getDefs().end(); I != E; ++I)
1559 if (I->second->isSubClassOf(Class))
1560 Defs.push_back(I->second);
1562 return Defs;