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/Support/DataTypes.h"
16 #include "llvm/Support/Streams.h"
17 #include "llvm/ADT/StringExtras.h"
22 //===----------------------------------------------------------------------===//
23 // Type implementations
24 //===----------------------------------------------------------------------===//
26 void RecTy::dump() const { print(*cerr.stream()); }
28 Init *BitRecTy::convertValue(BitsInit *BI) {
29 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
33 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
34 return RHS->getNumBits() == 1;
37 Init *BitRecTy::convertValue(IntInit *II) {
38 int64_t Val = II->getValue();
39 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
41 return new BitInit(Val != 0);
44 Init *BitRecTy::convertValue(TypedInit *VI) {
45 if (dynamic_cast<BitRecTy*>(VI->getType()))
46 return VI; // Accept variable if it is already of bit type!
50 std::string BitsRecTy::getAsString() const {
51 return "bits<" + utostr(Size) + ">";
54 Init *BitsRecTy::convertValue(UnsetInit *UI) {
55 BitsInit *Ret = new BitsInit(Size);
57 for (unsigned i = 0; i != Size; ++i)
58 Ret->setBit(i, new UnsetInit());
62 Init *BitsRecTy::convertValue(BitInit *UI) {
63 if (Size != 1) return 0; // Can only convert single bit...
64 BitsInit *Ret = new BitsInit(1);
69 // convertValue from Int initializer to bits type: Split the integer up into the
70 // appropriate bits...
72 Init *BitsRecTy::convertValue(IntInit *II) {
73 int64_t Value = II->getValue();
74 // Make sure this bitfield is large enough to hold the integer value...
76 if (Value & ~((1LL << Size)-1))
79 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
83 BitsInit *Ret = new BitsInit(Size);
84 for (unsigned i = 0; i != Size; ++i)
85 Ret->setBit(i, new BitInit(Value & (1LL << i)));
90 Init *BitsRecTy::convertValue(BitsInit *BI) {
91 // If the number of bits is right, return it. Otherwise we need to expand or
93 if (BI->getNumBits() == Size) return BI;
97 Init *BitsRecTy::convertValue(TypedInit *VI) {
98 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
99 if (BRT->Size == Size) {
100 BitsInit *Ret = new BitsInit(Size);
101 for (unsigned i = 0; i != Size; ++i)
102 Ret->setBit(i, new VarBitInit(VI, i));
105 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
106 BitsInit *Ret = new BitsInit(1);
114 Init *IntRecTy::convertValue(BitInit *BI) {
115 return new IntInit(BI->getValue());
118 Init *IntRecTy::convertValue(BitsInit *BI) {
120 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
121 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
122 Result |= Bit->getValue() << i;
126 return new IntInit(Result);
129 Init *IntRecTy::convertValue(TypedInit *TI) {
130 if (TI->getType()->typeIsConvertibleTo(this))
131 return TI; // Accept variable if already of the right type!
135 Init *StringRecTy::convertValue(UnOpInit *BO) {
136 if (BO->getOpcode() == UnOpInit::CAST) {
137 Init *L = BO->getOperand()->convertInitializerTo(this);
138 if (L == 0) return 0;
139 if (L != BO->getOperand())
140 return new UnOpInit(UnOpInit::CAST, L, new StringRecTy);
144 return convertValue((TypedInit*)BO);
147 Init *StringRecTy::convertValue(BinOpInit *BO) {
148 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
149 Init *L = BO->getLHS()->convertInitializerTo(this);
150 Init *R = BO->getRHS()->convertInitializerTo(this);
151 if (L == 0 || R == 0) return 0;
152 if (L != BO->getLHS() || R != BO->getRHS())
153 return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy);
156 if (BO->getOpcode() == BinOpInit::NAMECONCAT) {
157 if (BO->getType()->getAsString() == getAsString()) {
158 Init *L = BO->getLHS()->convertInitializerTo(this);
159 Init *R = BO->getRHS()->convertInitializerTo(this);
160 if (L == 0 || R == 0) return 0;
161 if (L != BO->getLHS() || R != BO->getRHS())
162 return new BinOpInit(BinOpInit::NAMECONCAT, L, R, new StringRecTy);
167 return convertValue((TypedInit*)BO);
171 Init *StringRecTy::convertValue(TypedInit *TI) {
172 if (dynamic_cast<StringRecTy*>(TI->getType()))
173 return TI; // Accept variable if already of the right type!
177 std::string ListRecTy::getAsString() const {
178 return "list<" + Ty->getAsString() + ">";
181 Init *ListRecTy::convertValue(ListInit *LI) {
182 std::vector<Init*> Elements;
184 // Verify that all of the elements of the list are subclasses of the
185 // appropriate class!
186 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
187 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
188 Elements.push_back(CI);
192 return new ListInit(Elements);
195 Init *ListRecTy::convertValue(TypedInit *TI) {
196 // Ensure that TI is compatible with our class.
197 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
198 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
203 Init *CodeRecTy::convertValue(TypedInit *TI) {
204 if (TI->getType()->typeIsConvertibleTo(this))
209 Init *DagRecTy::convertValue(TypedInit *TI) {
210 if (TI->getType()->typeIsConvertibleTo(this))
215 Init *DagRecTy::convertValue(UnOpInit *BO) {
216 if (BO->getOpcode() == UnOpInit::CAST) {
217 Init *L = BO->getOperand()->convertInitializerTo(this);
218 if (L == 0) return 0;
219 if (L != BO->getOperand())
220 return new UnOpInit(UnOpInit::CAST, L, new DagRecTy);
226 Init *DagRecTy::convertValue(BinOpInit *BO) {
227 if (BO->getOpcode() == BinOpInit::CONCAT) {
228 Init *L = BO->getLHS()->convertInitializerTo(this);
229 Init *R = BO->getRHS()->convertInitializerTo(this);
230 if (L == 0 || R == 0) return 0;
231 if (L != BO->getLHS() || R != BO->getRHS())
232 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
235 if (BO->getOpcode() == BinOpInit::NAMECONCAT) {
236 if (BO->getType()->getAsString() == getAsString()) {
237 Init *L = BO->getLHS()->convertInitializerTo(this);
238 Init *R = BO->getRHS()->convertInitializerTo(this);
239 if (L == 0 || R == 0) return 0;
240 if (L != BO->getLHS() || R != BO->getRHS())
241 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
248 std::string RecordRecTy::getAsString() const {
249 return Rec->getName();
252 Init *RecordRecTy::convertValue(DefInit *DI) {
253 // Ensure that DI is a subclass of Rec.
254 if (!DI->getDef()->isSubClassOf(Rec))
259 Init *RecordRecTy::convertValue(TypedInit *TI) {
260 // Ensure that TI is compatible with Rec.
261 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
262 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
263 RRT->getRecord() == getRecord())
268 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
269 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
273 //===----------------------------------------------------------------------===//
274 // Initializer implementations
275 //===----------------------------------------------------------------------===//
277 void Init::dump() const { return print(*cerr.stream()); }
279 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
280 BitsInit *BI = new BitsInit(Bits.size());
281 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
282 if (Bits[i] >= getNumBits()) {
286 BI->setBit(i, getBit(Bits[i]));
291 std::string BitsInit::getAsString() const {
292 //if (!printInHex(OS)) return;
293 //if (!printAsVariable(OS)) return;
294 //if (!printAsUnset(OS)) return;
296 std::string Result = "{ ";
297 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
298 if (i) Result += ", ";
299 if (Init *Bit = getBit(e-i-1))
300 Result += Bit->getAsString();
304 return Result + " }";
307 bool BitsInit::printInHex(std::ostream &OS) const {
308 // First, attempt to convert the value into an integer value...
310 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
311 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
312 Result |= Bit->getValue() << i;
317 OS << "0x" << std::hex << Result << std::dec;
321 bool BitsInit::printAsVariable(std::ostream &OS) const {
322 // Get the variable that we may be set equal to...
323 assert(getNumBits() != 0);
324 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
325 if (FirstBit == 0) return true;
326 TypedInit *Var = FirstBit->getVariable();
328 // Check to make sure the types are compatible.
329 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
330 if (Ty == 0) return true;
331 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
333 // Check to make sure all bits are referring to the right bits in the variable
334 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
335 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
336 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
344 bool BitsInit::printAsUnset(std::ostream &OS) const {
345 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
346 if (!dynamic_cast<UnsetInit*>(getBit(i)))
352 // resolveReferences - If there are any field references that refer to fields
353 // that have been filled in, we can propagate the values now.
355 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
356 bool Changed = false;
357 BitsInit *New = new BitsInit(getNumBits());
359 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
361 Init *CurBit = getBit(i);
365 CurBit = CurBit->resolveReferences(R, RV);
366 Changed |= B != CurBit;
367 } while (B != CurBit);
368 New->setBit(i, CurBit);
377 std::string IntInit::getAsString() const {
378 return itostr(Value);
381 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
382 BitsInit *BI = new BitsInit(Bits.size());
384 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
389 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
394 Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
395 std::vector<Init*> Vals;
396 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
397 if (Elements[i] >= getSize())
399 Vals.push_back(getElement(Elements[i]));
401 return new ListInit(Vals);
404 Record *ListInit::getElementAsRecord(unsigned i) const {
405 assert(i < Values.size() && "List element index out of range!");
406 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
407 if (DI == 0) throw "Expected record in list!";
411 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
412 std::vector<Init*> Resolved;
413 Resolved.reserve(getSize());
414 bool Changed = false;
416 for (unsigned i = 0, e = getSize(); i != e; ++i) {
418 Init *CurElt = getElement(i);
422 CurElt = CurElt->resolveReferences(R, RV);
423 Changed |= E != CurElt;
424 } while (E != CurElt);
425 Resolved.push_back(E);
429 return new ListInit(Resolved);
433 std::string ListInit::getAsString() const {
434 std::string Result = "[";
435 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
436 if (i) Result += ", ";
437 Result += Values[i]->getAsString();
442 Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
444 Init *Folded = Fold(&R, 0);
446 if (Folded != this) {
447 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
449 return Typed->resolveBitReference(R, IRV, Bit);
456 Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
458 Init *Folded = Fold(&R, 0);
460 if (Folded != this) {
461 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
463 return Typed->resolveListElementReference(R, IRV, Elt);
470 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
471 switch (getOpcode()) {
472 default: assert(0 && "Unknown unop");
474 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
476 std::string Name = LHSs->getValue();
478 // From TGParser::ParseIDValue
480 if (const RecordVal *RV = CurRec->getValue(Name)) {
481 if (RV->getType() != getType()) {
482 throw "type mismatch in nameconcat";
484 return new VarInit(Name, RV->getType());
487 std::string TemplateArgName = CurRec->getName()+":"+Name;
488 if (CurRec->isTemplateArg(TemplateArgName)) {
489 const RecordVal *RV = CurRec->getValue(TemplateArgName);
490 assert(RV && "Template arg doesn't exist??");
492 if (RV->getType() != getType()) {
493 throw "type mismatch in nameconcat";
496 return new VarInit(TemplateArgName, RV->getType());
501 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
502 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
503 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
504 assert(RV && "Template arg doesn't exist??");
506 if (RV->getType() != getType()) {
507 throw "type mismatch in nameconcat";
510 return new VarInit(MCName, RV->getType());
514 if (Record *D = Records.getDef(Name))
515 return new DefInit(D);
517 cerr << "Variable not defined: '" + Name + "'\n";
518 assert(0 && "Variable not found");
527 Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
528 Init *lhs = LHS->resolveReferences(R, RV);
531 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
535 std::string UnOpInit::getAsString() const {
538 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
540 return Result + "(" + LHS->getAsString() + ")";
543 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
544 switch (getOpcode()) {
545 default: assert(0 && "Unknown binop");
547 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
548 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
550 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
551 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
552 if (LOp->getDef() != ROp->getDef()) {
554 LOp->getDef()->getName() == "outs" ||
555 LOp->getDef()->getName() != "ins" ||
556 LOp->getDef()->getName() != "defs";
558 ROp->getDef()->getName() == "outs" ||
559 ROp->getDef()->getName() != "ins" ||
560 ROp->getDef()->getName() != "defs";
561 if (!LIsOps || !RIsOps)
562 throw "Concated Dag operators do not match!";
564 std::vector<Init*> Args;
565 std::vector<std::string> ArgNames;
566 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
567 Args.push_back(LHSs->getArg(i));
568 ArgNames.push_back(LHSs->getArgName(i));
570 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
571 Args.push_back(RHSs->getArg(i));
572 ArgNames.push_back(RHSs->getArgName(i));
574 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
579 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
580 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
582 return new StringInit(LHSs->getValue() + RHSs->getValue());
586 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
587 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
589 std::string Name(LHSs->getValue() + RHSs->getValue());
591 // From TGParser::ParseIDValue
593 if (const RecordVal *RV = CurRec->getValue(Name)) {
594 if (RV->getType() != getType()) {
595 throw "type mismatch in nameconcat";
597 return new VarInit(Name, RV->getType());
600 std::string TemplateArgName = CurRec->getName()+":"+Name;
601 if (CurRec->isTemplateArg(TemplateArgName)) {
602 const RecordVal *RV = CurRec->getValue(TemplateArgName);
603 assert(RV && "Template arg doesn't exist??");
605 if (RV->getType() != getType()) {
606 throw "type mismatch in nameconcat";
609 return new VarInit(TemplateArgName, RV->getType());
614 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
615 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
616 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
617 assert(RV && "Template arg doesn't exist??");
619 if (RV->getType() != getType()) {
620 throw "type mismatch in nameconcat";
623 return new VarInit(MCName, RV->getType());
627 if (Record *D = Records.getDef(Name))
628 return new DefInit(D);
630 cerr << "Variable not defined: '" + Name + "'\n";
631 assert(0 && "Variable not found");
639 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
640 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
642 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
644 switch (getOpcode()) {
645 default: assert(0 && "Bad opcode!");
646 case SHL: Result = LHSv << RHSv; break;
647 case SRA: Result = LHSv >> RHSv; break;
648 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
650 return new IntInit(Result);
658 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
659 Init *lhs = LHS->resolveReferences(R, RV);
660 Init *rhs = RHS->resolveReferences(R, RV);
662 if (LHS != lhs || RHS != rhs)
663 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
667 std::string BinOpInit::getAsString() const {
670 case CONCAT: Result = "!con"; break;
671 case SHL: Result = "!shl"; break;
672 case SRA: Result = "!sra"; break;
673 case SRL: Result = "!srl"; break;
674 case STRCONCAT: Result = "!strconcat"; break;
676 Result = "!nameconcat<" + getType()->getAsString() + ">"; break;
678 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
681 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
682 switch (getOpcode()) {
683 default: assert(0 && "Unknown binop");
685 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
686 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
687 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
689 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
690 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
691 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
693 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
694 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
695 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
697 if ((LHSd && MHSd && RHSd)
698 || (LHSv && MHSv && RHSv)
699 || (LHSs && MHSs && RHSs)) {
701 Record *Val = RHSd->getDef();
702 if (LHSd->getAsString() == RHSd->getAsString()) {
703 Val = MHSd->getDef();
705 return new DefInit(Val);
708 std::string Val = RHSv->getName();
709 if (LHSv->getAsString() == RHSv->getAsString()) {
710 Val = MHSv->getName();
712 return new VarInit(Val, getType());
715 std::string Val = RHSs->getValue();
717 std::string::size_type found;
719 found = Val.find(LHSs->getValue());
720 if (found != std::string::npos) {
721 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
723 } while (found != std::string::npos);
725 return new StringInit(Val);
732 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
733 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
735 DagRecTy *DagType = dynamic_cast<DagRecTy*>(getType());
736 ListRecTy *ListType = dynamic_cast<ListRecTy*>(getType());
738 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
741 cerr << "!foreach requires an operator\n";
742 assert(0 && "No operator for !foreach");
745 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
748 cerr << "!foreach requires typed variable\n";
749 assert(0 && "No typed variable for !foreach");
752 if (MHSd && DagType || MHSl && ListType) {
753 std::vector<Init *> NewOperands;
755 Init *Val = MHSd->getOperator();
756 TypedInit *TVal = dynamic_cast<TypedInit*>(Val);
758 if (TVal && TVal->getType()->typeIsConvertibleTo(LHSt->getType())) {
760 // First, replace the foreach variable with the DAG leaf
761 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
762 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
763 NewOperands.push_back(Val);
766 NewOperands.push_back(RHSo->getOperand(i));
770 // Now run the operator and use its result as the new leaf
771 OpInit *NewOp = RHSo->clone(NewOperands);
772 Val = NewOp->Fold(CurRec, CurMultiClass);
778 std::vector<std::pair<Init *, std::string> > args;
779 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
782 Arg = MHSd->getArg(i);
783 ArgName = MHSd->getArgName(i);
785 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
787 if (TArg && TArg->getType()->typeIsConvertibleTo(LHSt->getType())) {
790 // First, replace the foreach variable with the DAG leaf
791 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
792 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
793 NewOperands.push_back(Arg);
796 NewOperands.push_back(RHSo->getOperand(i));
800 // Now run the operator and use its result as the new leaf
801 OpInit *NewOp = RHSo->clone(NewOperands);
802 Arg = NewOp->Fold(CurRec, CurMultiClass);
808 if (LHSt->getType()->getAsString() == "string") {
811 // First, replace the foreach variable with the DAG leaf
812 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
813 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
814 NewOperands.push_back(new StringInit(ArgName));
817 NewOperands.push_back(RHSo->getOperand(i));
821 // Now run the operator and use its result as the new leaf
822 OpInit *NewOp = RHSo->clone(NewOperands);
823 Init *ArgNameInit = NewOp->Fold(CurRec, CurMultiClass);
824 StringInit *SArgNameInit = dynamic_cast<StringInit*>(ArgNameInit);
826 ArgName = SArgNameInit->getValue();
828 if (ArgNameInit != NewOp) {
834 args.push_back(std::make_pair(Arg, ArgName));
837 return new DagInit(Val, "", args);
840 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
842 for (ListInit::iterator li = NewList.begin(),
843 liend = NewList.end();
847 TypedInit *TItem = dynamic_cast<TypedInit*>(Item);
848 if (TItem && TItem->getType()->typeIsConvertibleTo(LHSt->getType())) {
849 // First, replace the foreach variable with the list item
850 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
851 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
852 NewOperands.push_back(Item);
855 NewOperands.push_back(RHSo->getOperand(i));
859 // Now run the operator and use its result as the new list item
860 OpInit *NewOp = RHSo->clone(NewOperands);
861 *li = NewOp->Fold(CurRec, CurMultiClass);
868 return new ListInit(NewList);
878 Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
879 Init *lhs = LHS->resolveReferences(R, RV);
880 Init *mhs = MHS->resolveReferences(R, RV);
881 Init *rhs = RHS->resolveReferences(R, RV);
883 if (LHS != lhs || MHS != mhs || RHS != rhs)
884 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
888 std::string TernOpInit::getAsString() const {
891 case SUBST: Result = "!subst"; break;
892 case FOREACH: Result = "!foreach"; break;
894 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
895 + RHS->getAsString() + ")";
898 Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
899 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
900 if (T == 0) return 0; // Cannot subscript a non-bits variable...
901 unsigned NumBits = T->getNumBits();
903 BitsInit *BI = new BitsInit(Bits.size());
904 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
905 if (Bits[i] >= NumBits) {
909 BI->setBit(i, new VarBitInit(this, Bits[i]));
914 Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
915 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
916 if (T == 0) return 0; // Cannot subscript a non-list variable...
918 if (Elements.size() == 1)
919 return new VarListElementInit(this, Elements[0]);
921 std::vector<Init*> ListInits;
922 ListInits.reserve(Elements.size());
923 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
924 ListInits.push_back(new VarListElementInit(this, Elements[i]));
925 return new ListInit(ListInits);
929 Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
931 if (R.isTemplateArg(getName())) return 0;
932 if (IRV && IRV->getName() != getName()) return 0;
934 RecordVal *RV = R.getValue(getName());
935 assert(RV && "Reference to a non-existant variable?");
936 assert(dynamic_cast<BitsInit*>(RV->getValue()));
937 BitsInit *BI = (BitsInit*)RV->getValue();
939 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
940 Init *B = BI->getBit(Bit);
942 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
943 return B; // Replace the VarBitInit with it.
947 Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
949 if (R.isTemplateArg(getName())) return 0;
950 if (IRV && IRV->getName() != getName()) return 0;
952 RecordVal *RV = R.getValue(getName());
953 assert(RV && "Reference to a non-existant variable?");
954 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
956 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
957 assert(VI && "Invalid list element!");
958 return new VarListElementInit(VI, Elt);
961 if (Elt >= LI->getSize())
962 return 0; // Out of range reference.
963 Init *E = LI->getElement(Elt);
964 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
965 return E; // Replace the VarListElementInit with it.
970 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
971 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
972 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
973 return RV->getType();
977 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
978 if (dynamic_cast<RecordRecTy*>(getType()))
979 if (const RecordVal *RV = R.getValue(VarName)) {
980 Init *TheInit = RV->getValue();
981 assert(TheInit != this && "Infinite loop detected!");
982 if (Init *I = TheInit->getFieldInit(R, FieldName))
990 /// resolveReferences - This method is used by classes that refer to other
991 /// variables which may not be defined at the time they expression is formed.
992 /// If a value is set for the variable later, this method will be called on
993 /// users of the value to allow the value to propagate out.
995 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
996 if (RecordVal *Val = R.getValue(VarName))
997 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
998 return Val->getValue();
1002 std::string VarBitInit::getAsString() const {
1003 return TI->getAsString() + "{" + utostr(Bit) + "}";
1006 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1007 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
1012 std::string VarListElementInit::getAsString() const {
1013 return TI->getAsString() + "[" + utostr(Element) + "]";
1016 Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1017 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1023 Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1025 // FIXME: This should be implemented, to support references like:
1026 // bit B = AA[0]{1};
1030 Init *VarListElementInit::
1031 resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
1032 // FIXME: This should be implemented, to support references like:
1033 // int B = AA[0][1];
1037 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1038 if (const RecordVal *RV = Def->getValue(FieldName))
1039 return RV->getType();
1043 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
1044 return Def->getValue(FieldName)->getValue();
1048 std::string DefInit::getAsString() const {
1049 return Def->getName();
1052 Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1054 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
1055 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1056 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1057 Init *B = BI->getBit(Bit);
1059 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
1060 return B; // Replace the VarBitInit with it.
1065 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1067 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
1068 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1069 if (Elt >= LI->getSize()) return 0;
1070 Init *E = LI->getElement(Elt);
1072 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
1073 return E; // Replace the VarListElementInit with it.
1078 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1079 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1081 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
1083 Init *BVR = BitsVal->resolveReferences(R, RV);
1084 return BVR->isComplete() ? BVR : this;
1087 if (NewRec != Rec) {
1088 return new FieldInit(NewRec, FieldName);
1093 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1094 std::vector<Init*> NewArgs;
1095 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1096 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
1098 Init *Op = Val->resolveReferences(R, RV);
1100 if (Args != NewArgs || Op != Val)
1101 return new DagInit(Op, "", NewArgs, ArgNames);
1107 std::string DagInit::getAsString() const {
1108 std::string Result = "(" + Val->getAsString();
1109 if (!ValName.empty())
1110 Result += ":" + ValName;
1112 Result += " " + Args[0]->getAsString();
1113 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
1114 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
1115 Result += ", " + Args[i]->getAsString();
1116 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
1119 return Result + ")";
1123 //===----------------------------------------------------------------------===//
1124 // Other implementations
1125 //===----------------------------------------------------------------------===//
1127 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1128 : Name(N), Ty(T), Prefix(P) {
1129 Value = Ty->convertValue(new UnsetInit());
1130 assert(Value && "Cannot create unset value for current type!");
1133 void RecordVal::dump() const { cerr << *this; }
1135 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
1136 if (getPrefix()) OS << "field ";
1137 OS << *getType() << " " << getName();
1140 OS << " = " << *getValue();
1142 if (PrintSem) OS << ";\n";
1145 void Record::setName(const std::string &Name) {
1146 if (Records.getDef(getName()) == this) {
1147 Records.removeDef(getName());
1149 Records.addDef(this);
1151 Records.removeClass(getName());
1153 Records.addClass(this);
1157 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1158 /// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1160 void Record::resolveReferencesTo(const RecordVal *RV) {
1161 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1162 if (Init *V = Values[i].getValue())
1163 Values[i].setValue(V->resolveReferences(*this, RV));
1168 void Record::dump() const { cerr << *this; }
1170 std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
1173 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1174 if (!TArgs.empty()) {
1176 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1178 const RecordVal *RV = R.getValue(TArgs[i]);
1179 assert(RV && "Template argument record not found??");
1180 RV->print(OS, false);
1186 const std::vector<Record*> &SC = R.getSuperClasses();
1189 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1190 OS << " " << SC[i]->getName();
1194 const std::vector<RecordVal> &Vals = R.getValues();
1195 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1196 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1198 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1199 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1205 /// getValueInit - Return the initializer for a value with the specified name,
1206 /// or throw an exception if the field does not exist.
1208 Init *Record::getValueInit(const std::string &FieldName) const {
1209 const RecordVal *R = getValue(FieldName);
1210 if (R == 0 || R->getValue() == 0)
1211 throw "Record `" + getName() + "' does not have a field named `" +
1213 return R->getValue();
1217 /// getValueAsString - This method looks up the specified field and returns its
1218 /// value as a string, throwing an exception if the field does not exist or if
1219 /// the value is not a string.
1221 std::string Record::getValueAsString(const std::string &FieldName) const {
1222 const RecordVal *R = getValue(FieldName);
1223 if (R == 0 || R->getValue() == 0)
1224 throw "Record `" + getName() + "' does not have a field named `" +
1227 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1228 return SI->getValue();
1229 throw "Record `" + getName() + "', field `" + FieldName +
1230 "' does not have a string initializer!";
1233 /// getValueAsBitsInit - This method looks up the specified field and returns
1234 /// its value as a BitsInit, throwing an exception if the field does not exist
1235 /// or if the value is not the right type.
1237 BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
1238 const RecordVal *R = getValue(FieldName);
1239 if (R == 0 || R->getValue() == 0)
1240 throw "Record `" + getName() + "' does not have a field named `" +
1243 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1245 throw "Record `" + getName() + "', field `" + FieldName +
1246 "' does not have a BitsInit initializer!";
1249 /// getValueAsListInit - This method looks up the specified field and returns
1250 /// its value as a ListInit, throwing an exception if the field does not exist
1251 /// or if the value is not the right type.
1253 ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
1254 const RecordVal *R = getValue(FieldName);
1255 if (R == 0 || R->getValue() == 0)
1256 throw "Record `" + getName() + "' does not have a field named `" +
1259 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1261 throw "Record `" + getName() + "', field `" + FieldName +
1262 "' does not have a list initializer!";
1265 /// getValueAsListOfDefs - This method looks up the specified field and returns
1266 /// its value as a vector of records, throwing an exception if the field does
1267 /// not exist or if the value is not the right type.
1269 std::vector<Record*>
1270 Record::getValueAsListOfDefs(const std::string &FieldName) const {
1271 ListInit *List = getValueAsListInit(FieldName);
1272 std::vector<Record*> Defs;
1273 for (unsigned i = 0; i < List->getSize(); i++) {
1274 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1275 Defs.push_back(DI->getDef());
1277 throw "Record `" + getName() + "', field `" + FieldName +
1278 "' list is not entirely DefInit!";
1284 /// getValueAsInt - This method looks up the specified field and returns its
1285 /// value as an int64_t, throwing an exception if the field does not exist or if
1286 /// the value is not the right type.
1288 int64_t Record::getValueAsInt(const std::string &FieldName) const {
1289 const RecordVal *R = getValue(FieldName);
1290 if (R == 0 || R->getValue() == 0)
1291 throw "Record `" + getName() + "' does not have a field named `" +
1294 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1295 return II->getValue();
1296 throw "Record `" + getName() + "', field `" + FieldName +
1297 "' does not have an int initializer!";
1300 /// getValueAsListOfInts - This method looks up the specified field and returns
1301 /// its value as a vector of integers, throwing an exception if the field does
1302 /// not exist or if the value is not the right type.
1304 std::vector<int64_t>
1305 Record::getValueAsListOfInts(const std::string &FieldName) const {
1306 ListInit *List = getValueAsListInit(FieldName);
1307 std::vector<int64_t> Ints;
1308 for (unsigned i = 0; i < List->getSize(); i++) {
1309 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1310 Ints.push_back(II->getValue());
1312 throw "Record `" + getName() + "', field `" + FieldName +
1313 "' does not have a list of ints initializer!";
1319 /// getValueAsDef - This method looks up the specified field and returns its
1320 /// value as a Record, throwing an exception if the field does not exist or if
1321 /// the value is not the right type.
1323 Record *Record::getValueAsDef(const std::string &FieldName) const {
1324 const RecordVal *R = getValue(FieldName);
1325 if (R == 0 || R->getValue() == 0)
1326 throw "Record `" + getName() + "' does not have a field named `" +
1329 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1330 return DI->getDef();
1331 throw "Record `" + getName() + "', field `" + FieldName +
1332 "' does not have a def initializer!";
1335 /// getValueAsBit - This method looks up the specified field and returns its
1336 /// value as a bit, throwing an exception if the field does not exist or if
1337 /// the value is not the right type.
1339 bool Record::getValueAsBit(const std::string &FieldName) const {
1340 const RecordVal *R = getValue(FieldName);
1341 if (R == 0 || R->getValue() == 0)
1342 throw "Record `" + getName() + "' does not have a field named `" +
1345 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1346 return BI->getValue();
1347 throw "Record `" + getName() + "', field `" + FieldName +
1348 "' does not have a bit initializer!";
1351 /// getValueAsDag - This method looks up the specified field and returns its
1352 /// value as an Dag, throwing an exception if the field does not exist or if
1353 /// the value is not the right type.
1355 DagInit *Record::getValueAsDag(const std::string &FieldName) const {
1356 const RecordVal *R = getValue(FieldName);
1357 if (R == 0 || R->getValue() == 0)
1358 throw "Record `" + getName() + "' does not have a field named `" +
1361 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1363 throw "Record `" + getName() + "', field `" + FieldName +
1364 "' does not have a dag initializer!";
1367 std::string Record::getValueAsCode(const std::string &FieldName) const {
1368 const RecordVal *R = getValue(FieldName);
1369 if (R == 0 || R->getValue() == 0)
1370 throw "Record `" + getName() + "' does not have a field named `" +
1373 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1374 return CI->getValue();
1375 throw "Record `" + getName() + "', field `" + FieldName +
1376 "' does not have a code initializer!";
1380 void MultiClass::dump() const {
1381 cerr << "Record:\n";
1385 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1386 rend = DefPrototypes.end();
1394 void RecordKeeper::dump() const { cerr << *this; }
1396 std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
1397 OS << "------------- Classes -----------------\n";
1398 const std::map<std::string, Record*> &Classes = RK.getClasses();
1399 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
1400 E = Classes.end(); I != E; ++I)
1401 OS << "class " << *I->second;
1403 OS << "------------- Defs -----------------\n";
1404 const std::map<std::string, Record*> &Defs = RK.getDefs();
1405 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
1406 E = Defs.end(); I != E; ++I)
1407 OS << "def " << *I->second;
1412 /// getAllDerivedDefinitions - This method returns all concrete definitions
1413 /// that derive from the specified class name. If a class with the specified
1414 /// name does not exist, an error is printed and true is returned.
1415 std::vector<Record*>
1416 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1417 Record *Class = Records.getClass(ClassName);
1419 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
1421 std::vector<Record*> Defs;
1422 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1423 E = getDefs().end(); I != E; ++I)
1424 if (I->second->isSubClassOf(Class))
1425 Defs.push_back(I->second);