1 //===- Record.cpp - Record implementation ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implement the tablegen record classes.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/System/DataTypes.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/ADT/StringExtras.h"
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!
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!
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());
61 Init *BitsRecTy::convertValue(BitInit *UI) {
62 if (Size != 1) return 0; // Can only convert single bit...
63 BitsInit *Ret = new BitsInit(1);
68 // convertValue from Int initializer to bits type: Split the integer up into the
69 // appropriate bits...
71 Init *BitsRecTy::convertValue(IntInit *II) {
72 int64_t Value = II->getValue();
73 // Make sure this bitfield is large enough to hold the integer value...
75 if (Value & ~((1LL << Size)-1))
78 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
82 BitsInit *Ret = new BitsInit(Size);
83 for (unsigned i = 0; i != Size; ++i)
84 Ret->setBit(i, new BitInit(Value & (1LL << i)));
89 Init *BitsRecTy::convertValue(BitsInit *BI) {
90 // If the number of bits is right, return it. Otherwise we need to expand or
92 if (BI->getNumBits() == Size) return BI;
96 Init *BitsRecTy::convertValue(TypedInit *VI) {
97 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
98 if (BRT->Size == Size) {
99 BitsInit *Ret = new BitsInit(Size);
100 for (unsigned i = 0; i != Size; ++i)
101 Ret->setBit(i, new VarBitInit(VI, i));
104 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
105 BitsInit *Ret = new BitsInit(1);
113 Init *IntRecTy::convertValue(BitInit *BI) {
114 return new IntInit(BI->getValue());
117 Init *IntRecTy::convertValue(BitsInit *BI) {
119 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
120 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
121 Result |= Bit->getValue() << i;
125 return new IntInit(Result);
128 Init *IntRecTy::convertValue(TypedInit *TI) {
129 if (TI->getType()->typeIsConvertibleTo(this))
130 return TI; // Accept variable if already of the right type!
134 Init *StringRecTy::convertValue(UnOpInit *BO) {
135 if (BO->getOpcode() == UnOpInit::CAST) {
136 Init *L = BO->getOperand()->convertInitializerTo(this);
137 if (L == 0) return 0;
138 if (L != BO->getOperand())
139 return new UnOpInit(UnOpInit::CAST, L, new StringRecTy);
143 return convertValue((TypedInit*)BO);
146 Init *StringRecTy::convertValue(BinOpInit *BO) {
147 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
148 Init *L = BO->getLHS()->convertInitializerTo(this);
149 Init *R = BO->getRHS()->convertInitializerTo(this);
150 if (L == 0 || R == 0) return 0;
151 if (L != BO->getLHS() || R != BO->getRHS())
152 return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy);
156 return convertValue((TypedInit*)BO);
160 Init *StringRecTy::convertValue(TypedInit *TI) {
161 if (dynamic_cast<StringRecTy*>(TI->getType()))
162 return TI; // Accept variable if already of the right type!
166 std::string ListRecTy::getAsString() const {
167 return "list<" + Ty->getAsString() + ">";
170 Init *ListRecTy::convertValue(ListInit *LI) {
171 std::vector<Init*> Elements;
173 // Verify that all of the elements of the list are subclasses of the
174 // appropriate class!
175 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
176 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
177 Elements.push_back(CI);
181 ListRecTy *LType = dynamic_cast<ListRecTy*>(LI->getType());
186 return new ListInit(Elements, new ListRecTy(Ty));
189 Init *ListRecTy::convertValue(TypedInit *TI) {
190 // Ensure that TI is compatible with our class.
191 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
192 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
197 Init *CodeRecTy::convertValue(TypedInit *TI) {
198 if (TI->getType()->typeIsConvertibleTo(this))
203 Init *DagRecTy::convertValue(TypedInit *TI) {
204 if (TI->getType()->typeIsConvertibleTo(this))
209 Init *DagRecTy::convertValue(UnOpInit *BO) {
210 if (BO->getOpcode() == UnOpInit::CAST) {
211 Init *L = BO->getOperand()->convertInitializerTo(this);
212 if (L == 0) return 0;
213 if (L != BO->getOperand())
214 return new UnOpInit(UnOpInit::CAST, L, new DagRecTy);
220 Init *DagRecTy::convertValue(BinOpInit *BO) {
221 if (BO->getOpcode() == BinOpInit::CONCAT) {
222 Init *L = BO->getLHS()->convertInitializerTo(this);
223 Init *R = BO->getRHS()->convertInitializerTo(this);
224 if (L == 0 || R == 0) return 0;
225 if (L != BO->getLHS() || R != BO->getRHS())
226 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
232 std::string RecordRecTy::getAsString() const {
233 return Rec->getName();
236 Init *RecordRecTy::convertValue(DefInit *DI) {
237 // Ensure that DI is a subclass of Rec.
238 if (!DI->getDef()->isSubClassOf(Rec))
243 Init *RecordRecTy::convertValue(TypedInit *TI) {
244 // Ensure that TI is compatible with Rec.
245 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
246 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
247 RRT->getRecord() == getRecord())
252 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
253 if (Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec))
256 const std::vector<Record*> &SC = Rec->getSuperClasses();
257 for (unsigned i = 0, e = SC.size(); i != e; ++i)
258 if (RHS->getRecord()->isSubClassOf(SC[i]))
265 /// resolveTypes - Find a common type that T1 and T2 convert to.
266 /// Return 0 if no such type exists.
268 RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
269 if (!T1->typeIsConvertibleTo(T2)) {
270 if (!T2->typeIsConvertibleTo(T1)) {
271 // If one is a Record type, check superclasses
272 RecordRecTy *RecTy1 = dynamic_cast<RecordRecTy*>(T1);
274 // See if T2 inherits from a type T1 also inherits from
275 const std::vector<Record *> &T1SuperClasses =
276 RecTy1->getRecord()->getSuperClasses();
277 for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(),
278 iend = T1SuperClasses.end();
281 RecordRecTy *SuperRecTy1 = new RecordRecTy(*i);
282 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
284 if (NewType1 != SuperRecTy1) {
291 RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2);
293 // See if T1 inherits from a type T2 also inherits from
294 const std::vector<Record *> &T2SuperClasses =
295 RecTy2->getRecord()->getSuperClasses();
296 for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(),
297 iend = T2SuperClasses.end();
300 RecordRecTy *SuperRecTy2 = new RecordRecTy(*i);
301 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
303 if (NewType2 != SuperRecTy2) {
318 //===----------------------------------------------------------------------===//
319 // Initializer implementations
320 //===----------------------------------------------------------------------===//
322 void Init::dump() const { return print(errs()); }
324 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
325 BitsInit *BI = new BitsInit(Bits.size());
326 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
327 if (Bits[i] >= getNumBits()) {
331 BI->setBit(i, getBit(Bits[i]));
336 std::string BitsInit::getAsString() const {
337 std::string Result = "{ ";
338 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
339 if (i) Result += ", ";
340 if (Init *Bit = getBit(e-i-1))
341 Result += Bit->getAsString();
345 return Result + " }";
348 // resolveReferences - If there are any field references that refer to fields
349 // that have been filled in, we can propagate the values now.
351 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
352 bool Changed = false;
353 BitsInit *New = new BitsInit(getNumBits());
355 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
357 Init *CurBit = getBit(i);
361 CurBit = CurBit->resolveReferences(R, RV);
362 Changed |= B != CurBit;
363 } while (B != CurBit);
364 New->setBit(i, CurBit);
373 std::string IntInit::getAsString() const {
374 return itostr(Value);
377 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
378 BitsInit *BI = new BitsInit(Bits.size());
380 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
385 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
390 Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
391 std::vector<Init*> Vals;
392 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
393 if (Elements[i] >= getSize())
395 Vals.push_back(getElement(Elements[i]));
397 return new ListInit(Vals, getType());
400 Record *ListInit::getElementAsRecord(unsigned i) const {
401 assert(i < Values.size() && "List element index out of range!");
402 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
403 if (DI == 0) throw "Expected record in list!";
407 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
408 std::vector<Init*> Resolved;
409 Resolved.reserve(getSize());
410 bool Changed = false;
412 for (unsigned i = 0, e = getSize(); i != e; ++i) {
414 Init *CurElt = getElement(i);
418 CurElt = CurElt->resolveReferences(R, RV);
419 Changed |= E != CurElt;
420 } while (E != CurElt);
421 Resolved.push_back(E);
425 return new ListInit(Resolved, getType());
429 Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
431 if (Elt >= getSize())
432 return 0; // Out of range reference.
433 Init *E = getElement(Elt);
434 // If the element is set to some value, or if we are resolving a reference
435 // to a specific variable and that variable is explicitly unset, then
436 // replace the VarListElementInit with it.
437 if (IRV || !dynamic_cast<UnsetInit*>(E))
442 std::string ListInit::getAsString() const {
443 std::string Result = "[";
444 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
445 if (i) Result += ", ";
446 Result += Values[i]->getAsString();
451 Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
453 Init *Folded = Fold(&R, 0);
455 if (Folded != this) {
456 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
458 return Typed->resolveBitReference(R, IRV, Bit);
465 Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
467 Init *Folded = Fold(&R, 0);
469 if (Folded != this) {
470 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
472 return Typed->resolveListElementReference(R, IRV, Elt);
479 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
480 switch (getOpcode()) {
481 default: assert(0 && "Unknown unop");
483 if (getType()->getAsString() == "string") {
484 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
489 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
491 return new StringInit(LHSd->getDef()->getName());
494 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
496 std::string Name = LHSs->getValue();
498 // From TGParser::ParseIDValue
500 if (const RecordVal *RV = CurRec->getValue(Name)) {
501 if (RV->getType() != getType())
502 throw "type mismatch in cast";
503 return new VarInit(Name, RV->getType());
506 std::string TemplateArgName = CurRec->getName()+":"+Name;
507 if (CurRec->isTemplateArg(TemplateArgName)) {
508 const RecordVal *RV = CurRec->getValue(TemplateArgName);
509 assert(RV && "Template arg doesn't exist??");
511 if (RV->getType() != getType())
512 throw "type mismatch in cast";
514 return new VarInit(TemplateArgName, RV->getType());
519 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
520 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
521 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
522 assert(RV && "Template arg doesn't exist??");
524 if (RV->getType() != getType())
525 throw "type mismatch in cast";
527 return new VarInit(MCName, RV->getType());
531 if (Record *D = Records.getDef(Name))
532 return new DefInit(D);
534 errs() << "Variable not defined: '" + Name + "'\n";
535 assert(0 && "Variable not found");
542 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
544 if (LHSl->getSize() == 0) {
545 assert(0 && "Empty list in car");
548 return LHSl->getElement(0);
553 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
555 if (LHSl->getSize() == 0) {
556 assert(0 && "Empty list in cdr");
559 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(),
566 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
568 if (LHSl->getSize() == 0) {
569 return new IntInit(1);
571 return new IntInit(0);
574 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
576 if (LHSs->getValue().empty()) {
577 return new IntInit(1);
579 return new IntInit(0);
589 Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
590 Init *lhs = LHS->resolveReferences(R, RV);
593 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
597 std::string UnOpInit::getAsString() const {
600 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
601 case CAR: Result = "!car"; break;
602 case CDR: Result = "!cdr"; break;
603 case LNULL: Result = "!null"; break;
605 return Result + "(" + LHS->getAsString() + ")";
608 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
609 switch (getOpcode()) {
610 default: assert(0 && "Unknown binop");
612 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
613 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
615 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
616 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
617 if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef())
618 throw "Concated Dag operators do not match!";
619 std::vector<Init*> Args;
620 std::vector<std::string> ArgNames;
621 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
622 Args.push_back(LHSs->getArg(i));
623 ArgNames.push_back(LHSs->getArgName(i));
625 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
626 Args.push_back(RHSs->getArg(i));
627 ArgNames.push_back(RHSs->getArgName(i));
629 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
634 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
635 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
637 return new StringInit(LHSs->getValue() + RHSs->getValue());
641 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
642 // to string objects.
644 dynamic_cast<IntInit*>(LHS->convertInitializerTo(new IntRecTy()));
646 dynamic_cast<IntInit*>(RHS->convertInitializerTo(new IntRecTy()));
649 return new IntInit(L->getValue() == R->getValue());
651 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
652 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
654 // Make sure we've resolved
656 return new IntInit(LHSs->getValue() == RHSs->getValue());
663 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
664 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
666 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
668 switch (getOpcode()) {
669 default: assert(0 && "Bad opcode!");
670 case SHL: Result = LHSv << RHSv; break;
671 case SRA: Result = LHSv >> RHSv; break;
672 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
674 return new IntInit(Result);
682 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
683 Init *lhs = LHS->resolveReferences(R, RV);
684 Init *rhs = RHS->resolveReferences(R, RV);
686 if (LHS != lhs || RHS != rhs)
687 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
691 std::string BinOpInit::getAsString() const {
694 case CONCAT: Result = "!con"; break;
695 case SHL: Result = "!shl"; break;
696 case SRA: Result = "!sra"; break;
697 case SRL: Result = "!srl"; break;
698 case EQ: Result = "!eq"; break;
699 case STRCONCAT: Result = "!strconcat"; break;
701 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
704 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
705 Record *CurRec, MultiClass *CurMultiClass);
707 static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
708 RecTy *Type, Record *CurRec,
709 MultiClass *CurMultiClass) {
710 std::vector<Init *> NewOperands;
712 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
714 // If this is a dag, recurse
715 if (TArg && TArg->getType()->getAsString() == "dag") {
716 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
717 CurRec, CurMultiClass);
725 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
726 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
729 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
730 Type, CurRec, CurMultiClass);
732 NewOperands.push_back(Result);
734 NewOperands.push_back(Arg);
736 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
737 NewOperands.push_back(Arg);
739 NewOperands.push_back(RHSo->getOperand(i));
743 // Now run the operator and use its result as the new leaf
744 OpInit *NewOp = RHSo->clone(NewOperands);
745 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
746 if (NewVal != NewOp) {
753 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
754 Record *CurRec, MultiClass *CurMultiClass) {
755 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
756 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
758 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
759 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
761 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
764 errs() << "!foreach requires an operator\n";
765 assert(0 && "No operator for !foreach");
768 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
771 errs() << "!foreach requires typed variable\n";
772 assert(0 && "No typed variable for !foreach");
775 if ((MHSd && DagType) || (MHSl && ListType)) {
777 Init *Val = MHSd->getOperator();
778 Init *Result = EvaluateOperation(RHSo, LHS, Val,
779 Type, CurRec, CurMultiClass);
784 std::vector<std::pair<Init *, std::string> > args;
785 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
788 Arg = MHSd->getArg(i);
789 ArgName = MHSd->getArgName(i);
792 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
793 CurRec, CurMultiClass);
798 // TODO: Process arg names
799 args.push_back(std::make_pair(Arg, ArgName));
802 return new DagInit(Val, "", args);
805 std::vector<Init *> NewOperands;
806 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
808 for (ListInit::iterator li = NewList.begin(),
809 liend = NewList.end();
814 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
815 // First, replace the foreach variable with the list item
816 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
817 NewOperands.push_back(Item);
819 NewOperands.push_back(RHSo->getOperand(i));
823 // Now run the operator and use its result as the new list item
824 OpInit *NewOp = RHSo->clone(NewOperands);
825 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
826 if (NewItem != NewOp) {
831 return new ListInit(NewList, MHSl->getType());
837 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
838 switch (getOpcode()) {
839 default: assert(0 && "Unknown binop");
841 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
842 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
843 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
845 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
846 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
847 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
849 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
850 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
851 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
853 if ((LHSd && MHSd && RHSd)
854 || (LHSv && MHSv && RHSv)
855 || (LHSs && MHSs && RHSs)) {
857 Record *Val = RHSd->getDef();
858 if (LHSd->getAsString() == RHSd->getAsString()) {
859 Val = MHSd->getDef();
861 return new DefInit(Val);
864 std::string Val = RHSv->getName();
865 if (LHSv->getAsString() == RHSv->getAsString()) {
866 Val = MHSv->getName();
868 return new VarInit(Val, getType());
871 std::string Val = RHSs->getValue();
873 std::string::size_type found;
874 std::string::size_type idx = 0;
876 found = Val.find(LHSs->getValue(), idx);
877 if (found != std::string::npos) {
878 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
880 idx = found + MHSs->getValue().size();
881 } while (found != std::string::npos);
883 return new StringInit(Val);
890 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
891 CurRec, CurMultiClass);
899 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
900 if (Init *I = LHS->convertInitializerTo(new IntRecTy()))
901 LHSi = dynamic_cast<IntInit*>(I);
903 if (LHSi->getValue()) {
916 Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
917 Init *lhs = LHS->resolveReferences(R, RV);
919 if (Opc == IF && lhs != LHS) {
920 IntInit *Value = dynamic_cast<IntInit*>(lhs);
921 if (Init *I = lhs->convertInitializerTo(new IntRecTy()))
922 Value = dynamic_cast<IntInit*>(I);
925 if (Value->getValue()) {
926 Init *mhs = MHS->resolveReferences(R, RV);
927 return (new TernOpInit(getOpcode(), lhs, mhs,
928 RHS, getType()))->Fold(&R, 0);
930 Init *rhs = RHS->resolveReferences(R, RV);
931 return (new TernOpInit(getOpcode(), lhs, MHS,
932 rhs, getType()))->Fold(&R, 0);
937 Init *mhs = MHS->resolveReferences(R, RV);
938 Init *rhs = RHS->resolveReferences(R, RV);
940 if (LHS != lhs || MHS != mhs || RHS != rhs)
941 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
945 std::string TernOpInit::getAsString() const {
948 case SUBST: Result = "!subst"; break;
949 case FOREACH: Result = "!foreach"; break;
950 case IF: Result = "!if"; break;
952 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
953 + RHS->getAsString() + ")";
956 RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
957 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
959 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
961 return Field->getType();
967 Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
968 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
969 if (T == 0) return 0; // Cannot subscript a non-bits variable...
970 unsigned NumBits = T->getNumBits();
972 BitsInit *BI = new BitsInit(Bits.size());
973 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
974 if (Bits[i] >= NumBits) {
978 BI->setBit(i, new VarBitInit(this, Bits[i]));
983 Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
984 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
985 if (T == 0) return 0; // Cannot subscript a non-list variable...
987 if (Elements.size() == 1)
988 return new VarListElementInit(this, Elements[0]);
990 std::vector<Init*> ListInits;
991 ListInits.reserve(Elements.size());
992 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
993 ListInits.push_back(new VarListElementInit(this, Elements[i]));
994 return new ListInit(ListInits, T);
998 Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1000 if (R.isTemplateArg(getName())) return 0;
1001 if (IRV && IRV->getName() != getName()) return 0;
1003 RecordVal *RV = R.getValue(getName());
1004 assert(RV && "Reference to a non-existent variable?");
1005 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1006 BitsInit *BI = (BitsInit*)RV->getValue();
1008 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1009 Init *B = BI->getBit(Bit);
1011 // If the bit is set to some value, or if we are resolving a reference to a
1012 // specific variable and that variable is explicitly unset, then replace the
1013 // VarBitInit with it.
1014 if (IRV || !dynamic_cast<UnsetInit*>(B))
1019 Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1021 if (R.isTemplateArg(getName())) return 0;
1022 if (IRV && IRV->getName() != getName()) return 0;
1024 RecordVal *RV = R.getValue(getName());
1025 assert(RV && "Reference to a non-existent variable?");
1026 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
1028 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
1029 assert(VI && "Invalid list element!");
1030 return new VarListElementInit(VI, Elt);
1033 if (Elt >= LI->getSize())
1034 return 0; // Out of range reference.
1035 Init *E = LI->getElement(Elt);
1036 // If the element is set to some value, or if we are resolving a reference
1037 // to a specific variable and that variable is explicitly unset, then
1038 // replace the VarListElementInit with it.
1039 if (IRV || !dynamic_cast<UnsetInit*>(E))
1045 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1046 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1047 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1048 return RV->getType();
1052 Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1053 const std::string &FieldName) const {
1054 if (dynamic_cast<RecordRecTy*>(getType()))
1055 if (const RecordVal *Val = R.getValue(VarName)) {
1056 if (RV != Val && (RV || dynamic_cast<UnsetInit*>(Val->getValue())))
1058 Init *TheInit = Val->getValue();
1059 assert(TheInit != this && "Infinite loop detected!");
1060 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
1068 /// resolveReferences - This method is used by classes that refer to other
1069 /// variables which may not be defined at the time the expression is formed.
1070 /// If a value is set for the variable later, this method will be called on
1071 /// users of the value to allow the value to propagate out.
1073 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
1074 if (RecordVal *Val = R.getValue(VarName))
1075 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
1076 return Val->getValue();
1080 std::string VarBitInit::getAsString() const {
1081 return TI->getAsString() + "{" + utostr(Bit) + "}";
1084 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1085 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
1090 std::string VarListElementInit::getAsString() const {
1091 return TI->getAsString() + "[" + utostr(Element) + "]";
1094 Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1095 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1101 Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1103 // FIXME: This should be implemented, to support references like:
1104 // bit B = AA[0]{1};
1108 Init *VarListElementInit::
1109 resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
1110 // FIXME: This should be implemented, to support references like:
1111 // int B = AA[0][1];
1115 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1116 if (const RecordVal *RV = Def->getValue(FieldName))
1117 return RV->getType();
1121 Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1122 const std::string &FieldName) const {
1123 return Def->getValue(FieldName)->getValue();
1127 std::string DefInit::getAsString() const {
1128 return Def->getName();
1131 Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1133 if (Init *BitsVal = Rec->getFieldInit(R, RV, FieldName))
1134 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1135 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1136 Init *B = BI->getBit(Bit);
1138 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
1139 return B; // Replace the VarBitInit with it.
1144 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1146 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
1147 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1148 if (Elt >= LI->getSize()) return 0;
1149 Init *E = LI->getElement(Elt);
1151 // If the element is set to some value, or if we are resolving a
1152 // reference to a specific variable and that variable is explicitly
1153 // unset, then replace the VarListElementInit with it.
1154 if (RV || !dynamic_cast<UnsetInit*>(E))
1160 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1161 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1163 Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName);
1165 Init *BVR = BitsVal->resolveReferences(R, RV);
1166 return BVR->isComplete() ? BVR : this;
1169 if (NewRec != Rec) {
1170 return new FieldInit(NewRec, FieldName);
1175 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1176 std::vector<Init*> NewArgs;
1177 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1178 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
1180 Init *Op = Val->resolveReferences(R, RV);
1182 if (Args != NewArgs || Op != Val)
1183 return new DagInit(Op, ValName, NewArgs, ArgNames);
1189 std::string DagInit::getAsString() const {
1190 std::string Result = "(" + Val->getAsString();
1191 if (!ValName.empty())
1192 Result += ":" + ValName;
1194 Result += " " + Args[0]->getAsString();
1195 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
1196 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
1197 Result += ", " + Args[i]->getAsString();
1198 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
1201 return Result + ")";
1205 //===----------------------------------------------------------------------===//
1206 // Other implementations
1207 //===----------------------------------------------------------------------===//
1209 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1210 : Name(N), Ty(T), Prefix(P) {
1211 Value = Ty->convertValue(new UnsetInit());
1212 assert(Value && "Cannot create unset value for current type!");
1215 void RecordVal::dump() const { errs() << *this; }
1217 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1218 if (getPrefix()) OS << "field ";
1219 OS << *getType() << " " << getName();
1222 OS << " = " << *getValue();
1224 if (PrintSem) OS << ";\n";
1227 unsigned Record::LastID = 0;
1229 void Record::setName(const std::string &Name) {
1230 if (Records.getDef(getName()) == this) {
1231 Records.removeDef(getName());
1233 Records.addDef(this);
1235 Records.removeClass(getName());
1237 Records.addClass(this);
1241 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1242 /// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1244 void Record::resolveReferencesTo(const RecordVal *RV) {
1245 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1246 if (Init *V = Values[i].getValue())
1247 Values[i].setValue(V->resolveReferences(*this, RV));
1251 void Record::dump() const { errs() << *this; }
1253 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
1256 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1257 if (!TArgs.empty()) {
1259 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1261 const RecordVal *RV = R.getValue(TArgs[i]);
1262 assert(RV && "Template argument record not found??");
1263 RV->print(OS, false);
1269 const std::vector<Record*> &SC = R.getSuperClasses();
1272 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1273 OS << " " << SC[i]->getName();
1277 const std::vector<RecordVal> &Vals = R.getValues();
1278 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1279 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1281 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1282 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1288 /// getValueInit - Return the initializer for a value with the specified name,
1289 /// or throw an exception if the field does not exist.
1291 Init *Record::getValueInit(StringRef FieldName) const {
1292 const RecordVal *R = getValue(FieldName);
1293 if (R == 0 || R->getValue() == 0)
1294 throw "Record `" + getName() + "' does not have a field named `" +
1295 FieldName.str() + "'!\n";
1296 return R->getValue();
1300 /// getValueAsString - This method looks up the specified field and returns its
1301 /// value as a string, throwing an exception if the field does not exist or if
1302 /// the value is not a string.
1304 std::string Record::getValueAsString(StringRef FieldName) const {
1305 const RecordVal *R = getValue(FieldName);
1306 if (R == 0 || R->getValue() == 0)
1307 throw "Record `" + getName() + "' does not have a field named `" +
1308 FieldName.str() + "'!\n";
1310 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1311 return SI->getValue();
1312 throw "Record `" + getName() + "', field `" + FieldName.str() +
1313 "' does not have a string initializer!";
1316 /// getValueAsBitsInit - This method looks up the specified field and returns
1317 /// its value as a BitsInit, throwing an exception if the field does not exist
1318 /// or if the value is not the right type.
1320 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
1321 const RecordVal *R = getValue(FieldName);
1322 if (R == 0 || R->getValue() == 0)
1323 throw "Record `" + getName() + "' does not have a field named `" +
1324 FieldName.str() + "'!\n";
1326 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1328 throw "Record `" + getName() + "', field `" + FieldName.str() +
1329 "' does not have a BitsInit initializer!";
1332 /// getValueAsListInit - This method looks up the specified field and returns
1333 /// its value as a ListInit, throwing an exception if the field does not exist
1334 /// or if the value is not the right type.
1336 ListInit *Record::getValueAsListInit(StringRef FieldName) const {
1337 const RecordVal *R = getValue(FieldName);
1338 if (R == 0 || R->getValue() == 0)
1339 throw "Record `" + getName() + "' does not have a field named `" +
1340 FieldName.str() + "'!\n";
1342 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1344 throw "Record `" + getName() + "', field `" + FieldName.str() +
1345 "' does not have a list initializer!";
1348 /// getValueAsListOfDefs - This method looks up the specified field and returns
1349 /// its value as a vector of records, throwing an exception if the field does
1350 /// not exist or if the value is not the right type.
1352 std::vector<Record*>
1353 Record::getValueAsListOfDefs(StringRef FieldName) const {
1354 ListInit *List = getValueAsListInit(FieldName);
1355 std::vector<Record*> Defs;
1356 for (unsigned i = 0; i < List->getSize(); i++) {
1357 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1358 Defs.push_back(DI->getDef());
1360 throw "Record `" + getName() + "', field `" + FieldName.str() +
1361 "' list is not entirely DefInit!";
1367 /// getValueAsInt - This method looks up the specified field and returns its
1368 /// value as an int64_t, throwing an exception if the field does not exist or if
1369 /// the value is not the right type.
1371 int64_t Record::getValueAsInt(StringRef FieldName) const {
1372 const RecordVal *R = getValue(FieldName);
1373 if (R == 0 || R->getValue() == 0)
1374 throw "Record `" + getName() + "' does not have a field named `" +
1375 FieldName.str() + "'!\n";
1377 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1378 return II->getValue();
1379 throw "Record `" + getName() + "', field `" + FieldName.str() +
1380 "' does not have an int initializer!";
1383 /// getValueAsListOfInts - This method looks up the specified field and returns
1384 /// its value as a vector of integers, throwing an exception if the field does
1385 /// not exist or if the value is not the right type.
1387 std::vector<int64_t>
1388 Record::getValueAsListOfInts(StringRef FieldName) const {
1389 ListInit *List = getValueAsListInit(FieldName);
1390 std::vector<int64_t> Ints;
1391 for (unsigned i = 0; i < List->getSize(); i++) {
1392 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1393 Ints.push_back(II->getValue());
1395 throw "Record `" + getName() + "', field `" + FieldName.str() +
1396 "' does not have a list of ints initializer!";
1402 /// getValueAsDef - This method looks up the specified field and returns its
1403 /// value as a Record, throwing an exception if the field does not exist or if
1404 /// the value is not the right type.
1406 Record *Record::getValueAsDef(StringRef FieldName) const {
1407 const RecordVal *R = getValue(FieldName);
1408 if (R == 0 || R->getValue() == 0)
1409 throw "Record `" + getName() + "' does not have a field named `" +
1410 FieldName.str() + "'!\n";
1412 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1413 return DI->getDef();
1414 throw "Record `" + getName() + "', field `" + FieldName.str() +
1415 "' does not have a def initializer!";
1418 /// getValueAsBit - This method looks up the specified field and returns its
1419 /// value as a bit, throwing an exception if the field does not exist or if
1420 /// the value is not the right type.
1422 bool Record::getValueAsBit(StringRef FieldName) const {
1423 const RecordVal *R = getValue(FieldName);
1424 if (R == 0 || R->getValue() == 0)
1425 throw "Record `" + getName() + "' does not have a field named `" +
1426 FieldName.str() + "'!\n";
1428 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1429 return BI->getValue();
1430 throw "Record `" + getName() + "', field `" + FieldName.str() +
1431 "' does not have a bit initializer!";
1434 /// getValueAsDag - This method looks up the specified field and returns its
1435 /// value as an Dag, throwing an exception if the field does not exist or if
1436 /// the value is not the right type.
1438 DagInit *Record::getValueAsDag(StringRef FieldName) const {
1439 const RecordVal *R = getValue(FieldName);
1440 if (R == 0 || R->getValue() == 0)
1441 throw "Record `" + getName() + "' does not have a field named `" +
1442 FieldName.str() + "'!\n";
1444 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1446 throw "Record `" + getName() + "', field `" + FieldName.str() +
1447 "' does not have a dag initializer!";
1450 std::string Record::getValueAsCode(StringRef FieldName) const {
1451 const RecordVal *R = getValue(FieldName);
1452 if (R == 0 || R->getValue() == 0)
1453 throw "Record `" + getName() + "' does not have a field named `" +
1454 FieldName.str() + "'!\n";
1456 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1457 return CI->getValue();
1458 throw "Record `" + getName() + "', field `" + FieldName.str() +
1459 "' does not have a code initializer!";
1463 void MultiClass::dump() const {
1464 errs() << "Record:\n";
1467 errs() << "Defs:\n";
1468 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1469 rend = DefPrototypes.end();
1477 void RecordKeeper::dump() const { errs() << *this; }
1479 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
1480 OS << "------------- Classes -----------------\n";
1481 const std::map<std::string, Record*> &Classes = RK.getClasses();
1482 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
1483 E = Classes.end(); I != E; ++I)
1484 OS << "class " << *I->second;
1486 OS << "------------- Defs -----------------\n";
1487 const std::map<std::string, Record*> &Defs = RK.getDefs();
1488 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
1489 E = Defs.end(); I != E; ++I)
1490 OS << "def " << *I->second;
1495 /// getAllDerivedDefinitions - This method returns all concrete definitions
1496 /// that derive from the specified class name. If a class with the specified
1497 /// name does not exist, an error is printed and true is returned.
1498 std::vector<Record*>
1499 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1500 Record *Class = Records.getClass(ClassName);
1502 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
1504 std::vector<Record*> Defs;
1505 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1506 E = getDefs().end(); I != E; ++I)
1507 if (I->second->isSubClassOf(Class))
1508 Defs.push_back(I->second);