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
);
155 if (BO
->getOpcode() == BinOpInit::NAMECONCAT
) {
156 if (BO
->getType()->getAsString() == getAsString()) {
157 Init
*L
= BO
->getLHS()->convertInitializerTo(this);
158 Init
*R
= BO
->getRHS()->convertInitializerTo(this);
159 if (L
== 0 || R
== 0) return 0;
160 if (L
!= BO
->getLHS() || R
!= BO
->getRHS())
161 return new BinOpInit(BinOpInit::NAMECONCAT
, L
, R
, new StringRecTy
);
166 return convertValue((TypedInit
*)BO
);
170 Init
*StringRecTy::convertValue(TypedInit
*TI
) {
171 if (dynamic_cast<StringRecTy
*>(TI
->getType()))
172 return TI
; // Accept variable if already of the right type!
176 std::string
ListRecTy::getAsString() const {
177 return "list<" + Ty
->getAsString() + ">";
180 Init
*ListRecTy::convertValue(ListInit
*LI
) {
181 std::vector
<Init
*> Elements
;
183 // Verify that all of the elements of the list are subclasses of the
184 // appropriate class!
185 for (unsigned i
= 0, e
= LI
->getSize(); i
!= e
; ++i
)
186 if (Init
*CI
= LI
->getElement(i
)->convertInitializerTo(Ty
))
187 Elements
.push_back(CI
);
191 ListRecTy
*LType
= dynamic_cast<ListRecTy
*>(LI
->getType());
196 return new ListInit(Elements
, new ListRecTy(Ty
));
199 Init
*ListRecTy::convertValue(TypedInit
*TI
) {
200 // Ensure that TI is compatible with our class.
201 if (ListRecTy
*LRT
= dynamic_cast<ListRecTy
*>(TI
->getType()))
202 if (LRT
->getElementType()->typeIsConvertibleTo(getElementType()))
207 Init
*CodeRecTy::convertValue(TypedInit
*TI
) {
208 if (TI
->getType()->typeIsConvertibleTo(this))
213 Init
*DagRecTy::convertValue(TypedInit
*TI
) {
214 if (TI
->getType()->typeIsConvertibleTo(this))
219 Init
*DagRecTy::convertValue(UnOpInit
*BO
) {
220 if (BO
->getOpcode() == UnOpInit::CAST
) {
221 Init
*L
= BO
->getOperand()->convertInitializerTo(this);
222 if (L
== 0) return 0;
223 if (L
!= BO
->getOperand())
224 return new UnOpInit(UnOpInit::CAST
, L
, new DagRecTy
);
230 Init
*DagRecTy::convertValue(BinOpInit
*BO
) {
231 if (BO
->getOpcode() == BinOpInit::CONCAT
) {
232 Init
*L
= BO
->getLHS()->convertInitializerTo(this);
233 Init
*R
= BO
->getRHS()->convertInitializerTo(this);
234 if (L
== 0 || R
== 0) return 0;
235 if (L
!= BO
->getLHS() || R
!= BO
->getRHS())
236 return new BinOpInit(BinOpInit::CONCAT
, L
, R
, new DagRecTy
);
239 if (BO
->getOpcode() == BinOpInit::NAMECONCAT
) {
240 if (BO
->getType()->getAsString() == getAsString()) {
241 Init
*L
= BO
->getLHS()->convertInitializerTo(this);
242 Init
*R
= BO
->getRHS()->convertInitializerTo(this);
243 if (L
== 0 || R
== 0) return 0;
244 if (L
!= BO
->getLHS() || R
!= BO
->getRHS())
245 return new BinOpInit(BinOpInit::CONCAT
, L
, R
, new DagRecTy
);
252 std::string
RecordRecTy::getAsString() const {
253 return Rec
->getName();
256 Init
*RecordRecTy::convertValue(DefInit
*DI
) {
257 // Ensure that DI is a subclass of Rec.
258 if (!DI
->getDef()->isSubClassOf(Rec
))
263 Init
*RecordRecTy::convertValue(TypedInit
*TI
) {
264 // Ensure that TI is compatible with Rec.
265 if (RecordRecTy
*RRT
= dynamic_cast<RecordRecTy
*>(TI
->getType()))
266 if (RRT
->getRecord()->isSubClassOf(getRecord()) ||
267 RRT
->getRecord() == getRecord())
272 bool RecordRecTy::baseClassOf(const RecordRecTy
*RHS
) const {
273 return Rec
== RHS
->getRecord() || RHS
->getRecord()->isSubClassOf(Rec
);
277 /// resolveTypes - Find a common type that T1 and T2 convert to.
278 /// Return 0 if no such type exists.
280 RecTy
*llvm::resolveTypes(RecTy
*T1
, RecTy
*T2
) {
281 if (!T1
->typeIsConvertibleTo(T2
)) {
282 if (!T2
->typeIsConvertibleTo(T1
)) {
283 // If one is a Record type, check superclasses
284 RecordRecTy
*RecTy1
= dynamic_cast<RecordRecTy
*>(T1
);
286 // See if T2 inherits from a type T1 also inherits from
287 const std::vector
<Record
*> &T1SuperClasses
=
288 RecTy1
->getRecord()->getSuperClasses();
289 for(std::vector
<Record
*>::const_iterator i
= T1SuperClasses
.begin(),
290 iend
= T1SuperClasses
.end();
293 RecordRecTy
*SuperRecTy1
= new RecordRecTy(*i
);
294 RecTy
*NewType1
= resolveTypes(SuperRecTy1
, T2
);
296 if (NewType1
!= SuperRecTy1
) {
303 RecordRecTy
*RecTy2
= dynamic_cast<RecordRecTy
*>(T2
);
305 // See if T1 inherits from a type T2 also inherits from
306 const std::vector
<Record
*> &T2SuperClasses
=
307 RecTy2
->getRecord()->getSuperClasses();
308 for (std::vector
<Record
*>::const_iterator i
= T2SuperClasses
.begin(),
309 iend
= T2SuperClasses
.end();
312 RecordRecTy
*SuperRecTy2
= new RecordRecTy(*i
);
313 RecTy
*NewType2
= resolveTypes(T1
, SuperRecTy2
);
315 if (NewType2
!= SuperRecTy2
) {
330 //===----------------------------------------------------------------------===//
331 // Initializer implementations
332 //===----------------------------------------------------------------------===//
334 void Init::dump() const { return print(errs()); }
336 Init
*BitsInit::convertInitializerBitRange(const std::vector
<unsigned> &Bits
) {
337 BitsInit
*BI
= new BitsInit(Bits
.size());
338 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
) {
339 if (Bits
[i
] >= getNumBits()) {
343 BI
->setBit(i
, getBit(Bits
[i
]));
348 std::string
BitsInit::getAsString() const {
349 std::string Result
= "{ ";
350 for (unsigned i
= 0, e
= getNumBits(); i
!= e
; ++i
) {
351 if (i
) Result
+= ", ";
352 if (Init
*Bit
= getBit(e
-i
-1))
353 Result
+= Bit
->getAsString();
357 return Result
+ " }";
360 // resolveReferences - If there are any field references that refer to fields
361 // that have been filled in, we can propagate the values now.
363 Init
*BitsInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
364 bool Changed
= false;
365 BitsInit
*New
= new BitsInit(getNumBits());
367 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
) {
369 Init
*CurBit
= getBit(i
);
373 CurBit
= CurBit
->resolveReferences(R
, RV
);
374 Changed
|= B
!= CurBit
;
375 } while (B
!= CurBit
);
376 New
->setBit(i
, CurBit
);
385 std::string
IntInit::getAsString() const {
386 return itostr(Value
);
389 Init
*IntInit::convertInitializerBitRange(const std::vector
<unsigned> &Bits
) {
390 BitsInit
*BI
= new BitsInit(Bits
.size());
392 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
) {
397 BI
->setBit(i
, new BitInit(Value
& (INT64_C(1) << Bits
[i
])));
402 Init
*ListInit::convertInitListSlice(const std::vector
<unsigned> &Elements
) {
403 std::vector
<Init
*> Vals
;
404 for (unsigned i
= 0, e
= Elements
.size(); i
!= e
; ++i
) {
405 if (Elements
[i
] >= getSize())
407 Vals
.push_back(getElement(Elements
[i
]));
409 return new ListInit(Vals
, getType());
412 Record
*ListInit::getElementAsRecord(unsigned i
) const {
413 assert(i
< Values
.size() && "List element index out of range!");
414 DefInit
*DI
= dynamic_cast<DefInit
*>(Values
[i
]);
415 if (DI
== 0) throw "Expected record in list!";
419 Init
*ListInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
420 std::vector
<Init
*> Resolved
;
421 Resolved
.reserve(getSize());
422 bool Changed
= false;
424 for (unsigned i
= 0, e
= getSize(); i
!= e
; ++i
) {
426 Init
*CurElt
= getElement(i
);
430 CurElt
= CurElt
->resolveReferences(R
, RV
);
431 Changed
|= E
!= CurElt
;
432 } while (E
!= CurElt
);
433 Resolved
.push_back(E
);
437 return new ListInit(Resolved
, getType());
441 Init
*ListInit::resolveListElementReference(Record
&R
, const RecordVal
*IRV
,
443 if (Elt
>= getSize())
444 return 0; // Out of range reference.
445 Init
*E
= getElement(Elt
);
446 // If the element is set to some value, or if we are resolving a reference
447 // to a specific variable and that variable is explicitly unset, then
448 // replace the VarListElementInit with it.
449 if (IRV
|| !dynamic_cast<UnsetInit
*>(E
))
454 std::string
ListInit::getAsString() const {
455 std::string Result
= "[";
456 for (unsigned i
= 0, e
= Values
.size(); i
!= e
; ++i
) {
457 if (i
) Result
+= ", ";
458 Result
+= Values
[i
]->getAsString();
463 Init
*OpInit::resolveBitReference(Record
&R
, const RecordVal
*IRV
,
465 Init
*Folded
= Fold(&R
, 0);
467 if (Folded
!= this) {
468 TypedInit
*Typed
= dynamic_cast<TypedInit
*>(Folded
);
470 return Typed
->resolveBitReference(R
, IRV
, Bit
);
477 Init
*OpInit::resolveListElementReference(Record
&R
, const RecordVal
*IRV
,
479 Init
*Folded
= Fold(&R
, 0);
481 if (Folded
!= this) {
482 TypedInit
*Typed
= dynamic_cast<TypedInit
*>(Folded
);
484 return Typed
->resolveListElementReference(R
, IRV
, Elt
);
491 Init
*UnOpInit::Fold(Record
*CurRec
, MultiClass
*CurMultiClass
) {
492 switch (getOpcode()) {
493 default: assert(0 && "Unknown unop");
495 if (getType()->getAsString() == "string") {
496 StringInit
*LHSs
= dynamic_cast<StringInit
*>(LHS
);
501 DefInit
*LHSd
= dynamic_cast<DefInit
*>(LHS
);
503 return new StringInit(LHSd
->getDef()->getName());
506 StringInit
*LHSs
= dynamic_cast<StringInit
*>(LHS
);
508 std::string Name
= LHSs
->getValue();
510 // From TGParser::ParseIDValue
512 if (const RecordVal
*RV
= CurRec
->getValue(Name
)) {
513 if (RV
->getType() != getType()) {
514 throw "type mismatch in nameconcat";
516 return new VarInit(Name
, RV
->getType());
519 std::string TemplateArgName
= CurRec
->getName()+":"+Name
;
520 if (CurRec
->isTemplateArg(TemplateArgName
)) {
521 const RecordVal
*RV
= CurRec
->getValue(TemplateArgName
);
522 assert(RV
&& "Template arg doesn't exist??");
524 if (RV
->getType() != getType()) {
525 throw "type mismatch in nameconcat";
528 return new VarInit(TemplateArgName
, RV
->getType());
533 std::string MCName
= CurMultiClass
->Rec
.getName()+"::"+Name
;
534 if (CurMultiClass
->Rec
.isTemplateArg(MCName
)) {
535 const RecordVal
*RV
= CurMultiClass
->Rec
.getValue(MCName
);
536 assert(RV
&& "Template arg doesn't exist??");
538 if (RV
->getType() != getType()) {
539 throw "type mismatch in nameconcat";
542 return new VarInit(MCName
, RV
->getType());
546 if (Record
*D
= Records
.getDef(Name
))
547 return new DefInit(D
);
549 errs() << "Variable not defined: '" + Name
+ "'\n";
550 assert(0 && "Variable not found");
557 ListInit
*LHSl
= dynamic_cast<ListInit
*>(LHS
);
559 if (LHSl
->getSize() == 0) {
560 assert(0 && "Empty list in car");
563 return LHSl
->getElement(0);
568 ListInit
*LHSl
= dynamic_cast<ListInit
*>(LHS
);
570 if (LHSl
->getSize() == 0) {
571 assert(0 && "Empty list in cdr");
574 ListInit
*Result
= new ListInit(LHSl
->begin()+1, LHSl
->end(),
581 ListInit
*LHSl
= dynamic_cast<ListInit
*>(LHS
);
583 if (LHSl
->getSize() == 0) {
584 return new IntInit(1);
586 return new IntInit(0);
589 StringInit
*LHSs
= dynamic_cast<StringInit
*>(LHS
);
591 if (LHSs
->getValue().empty()) {
592 return new IntInit(1);
594 return new IntInit(0);
604 Init
*UnOpInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
605 Init
*lhs
= LHS
->resolveReferences(R
, RV
);
608 return (new UnOpInit(getOpcode(), lhs
, getType()))->Fold(&R
, 0);
612 std::string
UnOpInit::getAsString() const {
615 case CAST
: Result
= "!cast<" + getType()->getAsString() + ">"; break;
616 case CAR
: Result
= "!car"; break;
617 case CDR
: Result
= "!cdr"; break;
618 case LNULL
: Result
= "!null"; break;
620 return Result
+ "(" + LHS
->getAsString() + ")";
623 RecTy
*UnOpInit::getFieldType(const std::string
&FieldName
) const {
624 switch (getOpcode()) {
625 default: assert(0 && "Unknown unop");
627 RecordRecTy
*RecordType
= dynamic_cast<RecordRecTy
*>(getType());
629 RecordVal
*Field
= RecordType
->getRecord()->getValue(FieldName
);
631 return Field
->getType();
640 Init
*BinOpInit::Fold(Record
*CurRec
, MultiClass
*CurMultiClass
) {
641 switch (getOpcode()) {
642 default: assert(0 && "Unknown binop");
644 DagInit
*LHSs
= dynamic_cast<DagInit
*>(LHS
);
645 DagInit
*RHSs
= dynamic_cast<DagInit
*>(RHS
);
647 DefInit
*LOp
= dynamic_cast<DefInit
*>(LHSs
->getOperator());
648 DefInit
*ROp
= dynamic_cast<DefInit
*>(RHSs
->getOperator());
649 if (LOp
== 0 || ROp
== 0 || LOp
->getDef() != ROp
->getDef())
650 throw "Concated Dag operators do not match!";
651 std::vector
<Init
*> Args
;
652 std::vector
<std::string
> ArgNames
;
653 for (unsigned i
= 0, e
= LHSs
->getNumArgs(); i
!= e
; ++i
) {
654 Args
.push_back(LHSs
->getArg(i
));
655 ArgNames
.push_back(LHSs
->getArgName(i
));
657 for (unsigned i
= 0, e
= RHSs
->getNumArgs(); i
!= e
; ++i
) {
658 Args
.push_back(RHSs
->getArg(i
));
659 ArgNames
.push_back(RHSs
->getArgName(i
));
661 return new DagInit(LHSs
->getOperator(), "", Args
, ArgNames
);
666 StringInit
*LHSs
= dynamic_cast<StringInit
*>(LHS
);
667 StringInit
*RHSs
= dynamic_cast<StringInit
*>(RHS
);
669 return new StringInit(LHSs
->getValue() + RHSs
->getValue());
673 StringInit
*LHSs
= dynamic_cast<StringInit
*>(LHS
);
674 StringInit
*RHSs
= dynamic_cast<StringInit
*>(RHS
);
676 std::string
Name(LHSs
->getValue() + RHSs
->getValue());
678 // From TGParser::ParseIDValue
680 if (const RecordVal
*RV
= CurRec
->getValue(Name
)) {
681 if (RV
->getType() != getType()) {
682 throw "type mismatch in nameconcat";
684 return new VarInit(Name
, RV
->getType());
687 std::string TemplateArgName
= CurRec
->getName()+":"+Name
;
688 if (CurRec
->isTemplateArg(TemplateArgName
)) {
689 const RecordVal
*RV
= CurRec
->getValue(TemplateArgName
);
690 assert(RV
&& "Template arg doesn't exist??");
692 if (RV
->getType() != getType()) {
693 throw "type mismatch in nameconcat";
696 return new VarInit(TemplateArgName
, RV
->getType());
701 std::string MCName
= CurMultiClass
->Rec
.getName()+"::"+Name
;
702 if (CurMultiClass
->Rec
.isTemplateArg(MCName
)) {
703 const RecordVal
*RV
= CurMultiClass
->Rec
.getValue(MCName
);
704 assert(RV
&& "Template arg doesn't exist??");
706 if (RV
->getType() != getType()) {
707 throw "type mismatch in nameconcat";
710 return new VarInit(MCName
, RV
->getType());
714 if (Record
*D
= Records
.getDef(Name
))
715 return new DefInit(D
);
717 errs() << "Variable not defined in !nameconcat: '" + Name
+ "'\n";
718 assert(0 && "Variable not found in !nameconcat");
724 // Make sure we've resolved
725 StringInit
*LHSs
= dynamic_cast<StringInit
*>(LHS
);
726 StringInit
*RHSs
= dynamic_cast<StringInit
*>(RHS
);
728 return new IntInit(LHSs
->getValue() == RHSs
->getValue());
735 IntInit
*LHSi
= dynamic_cast<IntInit
*>(LHS
);
736 IntInit
*RHSi
= dynamic_cast<IntInit
*>(RHS
);
738 int64_t LHSv
= LHSi
->getValue(), RHSv
= RHSi
->getValue();
740 switch (getOpcode()) {
741 default: assert(0 && "Bad opcode!");
742 case SHL
: Result
= LHSv
<< RHSv
; break;
743 case SRA
: Result
= LHSv
>> RHSv
; break;
744 case SRL
: Result
= (uint64_t)LHSv
>> (uint64_t)RHSv
; break;
746 return new IntInit(Result
);
754 Init
*BinOpInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
755 Init
*lhs
= LHS
->resolveReferences(R
, RV
);
756 Init
*rhs
= RHS
->resolveReferences(R
, RV
);
758 if (LHS
!= lhs
|| RHS
!= rhs
)
759 return (new BinOpInit(getOpcode(), lhs
, rhs
, getType()))->Fold(&R
, 0);
763 std::string
BinOpInit::getAsString() const {
766 case CONCAT
: Result
= "!con"; break;
767 case SHL
: Result
= "!shl"; break;
768 case SRA
: Result
= "!sra"; break;
769 case SRL
: Result
= "!srl"; break;
770 case EQ
: Result
= "!eq"; break;
771 case STRCONCAT
: Result
= "!strconcat"; break;
773 Result
= "!nameconcat<" + getType()->getAsString() + ">"; break;
775 return Result
+ "(" + LHS
->getAsString() + ", " + RHS
->getAsString() + ")";
778 static Init
*ForeachHelper(Init
*LHS
, Init
*MHS
, Init
*RHS
, RecTy
*Type
,
779 Record
*CurRec
, MultiClass
*CurMultiClass
);
781 static Init
*EvaluateOperation(OpInit
*RHSo
, Init
*LHS
, Init
*Arg
,
782 RecTy
*Type
, Record
*CurRec
,
783 MultiClass
*CurMultiClass
) {
784 std::vector
<Init
*> NewOperands
;
786 TypedInit
*TArg
= dynamic_cast<TypedInit
*>(Arg
);
788 // If this is a dag, recurse
789 if (TArg
&& TArg
->getType()->getAsString() == "dag") {
790 Init
*Result
= ForeachHelper(LHS
, Arg
, RHSo
, Type
,
791 CurRec
, CurMultiClass
);
799 for (int i
= 0; i
< RHSo
->getNumOperands(); ++i
) {
800 OpInit
*RHSoo
= dynamic_cast<OpInit
*>(RHSo
->getOperand(i
));
803 Init
*Result
= EvaluateOperation(RHSoo
, LHS
, Arg
,
804 Type
, CurRec
, CurMultiClass
);
806 NewOperands
.push_back(Result
);
808 NewOperands
.push_back(Arg
);
810 } else if (LHS
->getAsString() == RHSo
->getOperand(i
)->getAsString()) {
811 NewOperands
.push_back(Arg
);
813 NewOperands
.push_back(RHSo
->getOperand(i
));
817 // Now run the operator and use its result as the new leaf
818 OpInit
*NewOp
= RHSo
->clone(NewOperands
);
819 Init
*NewVal
= NewOp
->Fold(CurRec
, CurMultiClass
);
820 if (NewVal
!= NewOp
) {
827 static Init
*ForeachHelper(Init
*LHS
, Init
*MHS
, Init
*RHS
, RecTy
*Type
,
828 Record
*CurRec
, MultiClass
*CurMultiClass
) {
829 DagInit
*MHSd
= dynamic_cast<DagInit
*>(MHS
);
830 ListInit
*MHSl
= dynamic_cast<ListInit
*>(MHS
);
832 DagRecTy
*DagType
= dynamic_cast<DagRecTy
*>(Type
);
833 ListRecTy
*ListType
= dynamic_cast<ListRecTy
*>(Type
);
835 OpInit
*RHSo
= dynamic_cast<OpInit
*>(RHS
);
838 errs() << "!foreach requires an operator\n";
839 assert(0 && "No operator for !foreach");
842 TypedInit
*LHSt
= dynamic_cast<TypedInit
*>(LHS
);
845 errs() << "!foreach requires typed variable\n";
846 assert(0 && "No typed variable for !foreach");
849 if ((MHSd
&& DagType
) || (MHSl
&& ListType
)) {
851 Init
*Val
= MHSd
->getOperator();
852 Init
*Result
= EvaluateOperation(RHSo
, LHS
, Val
,
853 Type
, CurRec
, CurMultiClass
);
858 std::vector
<std::pair
<Init
*, std::string
> > args
;
859 for (unsigned int i
= 0; i
< MHSd
->getNumArgs(); ++i
) {
862 Arg
= MHSd
->getArg(i
);
863 ArgName
= MHSd
->getArgName(i
);
866 Init
*Result
= EvaluateOperation(RHSo
, LHS
, Arg
, Type
,
867 CurRec
, CurMultiClass
);
872 // TODO: Process arg names
873 args
.push_back(std::make_pair(Arg
, ArgName
));
876 return new DagInit(Val
, "", args
);
879 std::vector
<Init
*> NewOperands
;
880 std::vector
<Init
*> NewList(MHSl
->begin(), MHSl
->end());
882 for (ListInit::iterator li
= NewList
.begin(),
883 liend
= NewList
.end();
888 for(int i
= 0; i
< RHSo
->getNumOperands(); ++i
) {
889 // First, replace the foreach variable with the list item
890 if (LHS
->getAsString() == RHSo
->getOperand(i
)->getAsString()) {
891 NewOperands
.push_back(Item
);
893 NewOperands
.push_back(RHSo
->getOperand(i
));
897 // Now run the operator and use its result as the new list item
898 OpInit
*NewOp
= RHSo
->clone(NewOperands
);
899 Init
*NewItem
= NewOp
->Fold(CurRec
, CurMultiClass
);
900 if (NewItem
!= NewOp
) {
905 return new ListInit(NewList
, MHSl
->getType());
911 Init
*TernOpInit::Fold(Record
*CurRec
, MultiClass
*CurMultiClass
) {
912 switch (getOpcode()) {
913 default: assert(0 && "Unknown binop");
915 DefInit
*LHSd
= dynamic_cast<DefInit
*>(LHS
);
916 VarInit
*LHSv
= dynamic_cast<VarInit
*>(LHS
);
917 StringInit
*LHSs
= dynamic_cast<StringInit
*>(LHS
);
919 DefInit
*MHSd
= dynamic_cast<DefInit
*>(MHS
);
920 VarInit
*MHSv
= dynamic_cast<VarInit
*>(MHS
);
921 StringInit
*MHSs
= dynamic_cast<StringInit
*>(MHS
);
923 DefInit
*RHSd
= dynamic_cast<DefInit
*>(RHS
);
924 VarInit
*RHSv
= dynamic_cast<VarInit
*>(RHS
);
925 StringInit
*RHSs
= dynamic_cast<StringInit
*>(RHS
);
927 if ((LHSd
&& MHSd
&& RHSd
)
928 || (LHSv
&& MHSv
&& RHSv
)
929 || (LHSs
&& MHSs
&& RHSs
)) {
931 Record
*Val
= RHSd
->getDef();
932 if (LHSd
->getAsString() == RHSd
->getAsString()) {
933 Val
= MHSd
->getDef();
935 return new DefInit(Val
);
938 std::string Val
= RHSv
->getName();
939 if (LHSv
->getAsString() == RHSv
->getAsString()) {
940 Val
= MHSv
->getName();
942 return new VarInit(Val
, getType());
945 std::string Val
= RHSs
->getValue();
947 std::string::size_type found
;
948 std::string::size_type idx
= 0;
950 found
= Val
.find(LHSs
->getValue(), idx
);
951 if (found
!= std::string::npos
) {
952 Val
.replace(found
, LHSs
->getValue().size(), MHSs
->getValue());
954 idx
= found
+ MHSs
->getValue().size();
955 } while (found
!= std::string::npos
);
957 return new StringInit(Val
);
964 Init
*Result
= ForeachHelper(LHS
, MHS
, RHS
, getType(),
965 CurRec
, CurMultiClass
);
973 IntInit
*LHSi
= dynamic_cast<IntInit
*>(LHS
);
975 if (LHSi
->getValue()) {
988 Init
*TernOpInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
989 Init
*lhs
= LHS
->resolveReferences(R
, RV
);
991 if (Opc
== IF
&& lhs
!= LHS
) {
992 IntInit
*Value
= dynamic_cast<IntInit
*>(lhs
);
995 if (Value
->getValue()) {
996 Init
*mhs
= MHS
->resolveReferences(R
, RV
);
997 return (new TernOpInit(getOpcode(), lhs
, mhs
,
998 RHS
, getType()))->Fold(&R
, 0);
1000 Init
*rhs
= RHS
->resolveReferences(R
, RV
);
1001 return (new TernOpInit(getOpcode(), lhs
, MHS
,
1002 rhs
, getType()))->Fold(&R
, 0);
1007 Init
*mhs
= MHS
->resolveReferences(R
, RV
);
1008 Init
*rhs
= RHS
->resolveReferences(R
, RV
);
1010 if (LHS
!= lhs
|| MHS
!= mhs
|| RHS
!= rhs
)
1011 return (new TernOpInit(getOpcode(), lhs
, mhs
, rhs
, getType()))->Fold(&R
, 0);
1015 std::string
TernOpInit::getAsString() const {
1018 case SUBST
: Result
= "!subst"; break;
1019 case FOREACH
: Result
= "!foreach"; break;
1020 case IF
: Result
= "!if"; break;
1022 return Result
+ "(" + LHS
->getAsString() + ", " + MHS
->getAsString() + ", "
1023 + RHS
->getAsString() + ")";
1026 Init
*TypedInit::convertInitializerBitRange(const std::vector
<unsigned> &Bits
) {
1027 BitsRecTy
*T
= dynamic_cast<BitsRecTy
*>(getType());
1028 if (T
== 0) return 0; // Cannot subscript a non-bits variable...
1029 unsigned NumBits
= T
->getNumBits();
1031 BitsInit
*BI
= new BitsInit(Bits
.size());
1032 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
) {
1033 if (Bits
[i
] >= NumBits
) {
1037 BI
->setBit(i
, new VarBitInit(this, Bits
[i
]));
1042 Init
*TypedInit::convertInitListSlice(const std::vector
<unsigned> &Elements
) {
1043 ListRecTy
*T
= dynamic_cast<ListRecTy
*>(getType());
1044 if (T
== 0) return 0; // Cannot subscript a non-list variable...
1046 if (Elements
.size() == 1)
1047 return new VarListElementInit(this, Elements
[0]);
1049 std::vector
<Init
*> ListInits
;
1050 ListInits
.reserve(Elements
.size());
1051 for (unsigned i
= 0, e
= Elements
.size(); i
!= e
; ++i
)
1052 ListInits
.push_back(new VarListElementInit(this, Elements
[i
]));
1053 return new ListInit(ListInits
, T
);
1057 Init
*VarInit::resolveBitReference(Record
&R
, const RecordVal
*IRV
,
1059 if (R
.isTemplateArg(getName())) return 0;
1060 if (IRV
&& IRV
->getName() != getName()) return 0;
1062 RecordVal
*RV
= R
.getValue(getName());
1063 assert(RV
&& "Reference to a non-existent variable?");
1064 assert(dynamic_cast<BitsInit
*>(RV
->getValue()));
1065 BitsInit
*BI
= (BitsInit
*)RV
->getValue();
1067 assert(Bit
< BI
->getNumBits() && "Bit reference out of range!");
1068 Init
*B
= BI
->getBit(Bit
);
1070 // If the bit is set to some value, or if we are resolving a reference to a
1071 // specific variable and that variable is explicitly unset, then replace the
1072 // VarBitInit with it.
1073 if (IRV
|| !dynamic_cast<UnsetInit
*>(B
))
1078 Init
*VarInit::resolveListElementReference(Record
&R
, const RecordVal
*IRV
,
1080 if (R
.isTemplateArg(getName())) return 0;
1081 if (IRV
&& IRV
->getName() != getName()) return 0;
1083 RecordVal
*RV
= R
.getValue(getName());
1084 assert(RV
&& "Reference to a non-existent variable?");
1085 ListInit
*LI
= dynamic_cast<ListInit
*>(RV
->getValue());
1087 VarInit
*VI
= dynamic_cast<VarInit
*>(RV
->getValue());
1088 assert(VI
&& "Invalid list element!");
1089 return new VarListElementInit(VI
, Elt
);
1092 if (Elt
>= LI
->getSize())
1093 return 0; // Out of range reference.
1094 Init
*E
= LI
->getElement(Elt
);
1095 // If the element is set to some value, or if we are resolving a reference
1096 // to a specific variable and that variable is explicitly unset, then
1097 // replace the VarListElementInit with it.
1098 if (IRV
|| !dynamic_cast<UnsetInit
*>(E
))
1104 RecTy
*VarInit::getFieldType(const std::string
&FieldName
) const {
1105 if (RecordRecTy
*RTy
= dynamic_cast<RecordRecTy
*>(getType()))
1106 if (const RecordVal
*RV
= RTy
->getRecord()->getValue(FieldName
))
1107 return RV
->getType();
1111 Init
*VarInit::getFieldInit(Record
&R
, const RecordVal
*RV
,
1112 const std::string
&FieldName
) const {
1113 if (dynamic_cast<RecordRecTy
*>(getType()))
1114 if (const RecordVal
*Val
= R
.getValue(VarName
)) {
1115 if (RV
!= Val
&& (RV
|| dynamic_cast<UnsetInit
*>(Val
->getValue())))
1117 Init
*TheInit
= Val
->getValue();
1118 assert(TheInit
!= this && "Infinite loop detected!");
1119 if (Init
*I
= TheInit
->getFieldInit(R
, RV
, FieldName
))
1127 /// resolveReferences - This method is used by classes that refer to other
1128 /// variables which may not be defined at the time the expression is formed.
1129 /// If a value is set for the variable later, this method will be called on
1130 /// users of the value to allow the value to propagate out.
1132 Init
*VarInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
1133 if (RecordVal
*Val
= R
.getValue(VarName
))
1134 if (RV
== Val
|| (RV
== 0 && !dynamic_cast<UnsetInit
*>(Val
->getValue())))
1135 return Val
->getValue();
1139 std::string
VarBitInit::getAsString() const {
1140 return TI
->getAsString() + "{" + utostr(Bit
) + "}";
1143 Init
*VarBitInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
1144 if (Init
*I
= getVariable()->resolveBitReference(R
, RV
, getBitNum()))
1149 std::string
VarListElementInit::getAsString() const {
1150 return TI
->getAsString() + "[" + utostr(Element
) + "]";
1153 Init
*VarListElementInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
1154 if (Init
*I
= getVariable()->resolveListElementReference(R
, RV
,
1160 Init
*VarListElementInit::resolveBitReference(Record
&R
, const RecordVal
*RV
,
1162 // FIXME: This should be implemented, to support references like:
1163 // bit B = AA[0]{1};
1167 Init
*VarListElementInit::
1168 resolveListElementReference(Record
&R
, const RecordVal
*RV
, unsigned Elt
) {
1169 // FIXME: This should be implemented, to support references like:
1170 // int B = AA[0][1];
1174 RecTy
*DefInit::getFieldType(const std::string
&FieldName
) const {
1175 if (const RecordVal
*RV
= Def
->getValue(FieldName
))
1176 return RV
->getType();
1180 Init
*DefInit::getFieldInit(Record
&R
, const RecordVal
*RV
,
1181 const std::string
&FieldName
) const {
1182 return Def
->getValue(FieldName
)->getValue();
1186 std::string
DefInit::getAsString() const {
1187 return Def
->getName();
1190 Init
*FieldInit::resolveBitReference(Record
&R
, const RecordVal
*RV
,
1192 if (Init
*BitsVal
= Rec
->getFieldInit(R
, RV
, FieldName
))
1193 if (BitsInit
*BI
= dynamic_cast<BitsInit
*>(BitsVal
)) {
1194 assert(Bit
< BI
->getNumBits() && "Bit reference out of range!");
1195 Init
*B
= BI
->getBit(Bit
);
1197 if (dynamic_cast<BitInit
*>(B
)) // If the bit is set...
1198 return B
; // Replace the VarBitInit with it.
1203 Init
*FieldInit::resolveListElementReference(Record
&R
, const RecordVal
*RV
,
1205 if (Init
*ListVal
= Rec
->getFieldInit(R
, RV
, FieldName
))
1206 if (ListInit
*LI
= dynamic_cast<ListInit
*>(ListVal
)) {
1207 if (Elt
>= LI
->getSize()) return 0;
1208 Init
*E
= LI
->getElement(Elt
);
1210 // If the element is set to some value, or if we are resolving a
1211 // reference to a specific variable and that variable is explicitly
1212 // unset, then replace the VarListElementInit with it.
1213 if (RV
|| !dynamic_cast<UnsetInit
*>(E
))
1219 Init
*FieldInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
1220 Init
*NewRec
= RV
? Rec
->resolveReferences(R
, RV
) : Rec
;
1222 Init
*BitsVal
= NewRec
->getFieldInit(R
, RV
, FieldName
);
1224 Init
*BVR
= BitsVal
->resolveReferences(R
, RV
);
1225 return BVR
->isComplete() ? BVR
: this;
1228 if (NewRec
!= Rec
) {
1229 return new FieldInit(NewRec
, FieldName
);
1234 Init
*DagInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
1235 std::vector
<Init
*> NewArgs
;
1236 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; ++i
)
1237 NewArgs
.push_back(Args
[i
]->resolveReferences(R
, RV
));
1239 Init
*Op
= Val
->resolveReferences(R
, RV
);
1241 if (Args
!= NewArgs
|| Op
!= Val
)
1242 return new DagInit(Op
, "", NewArgs
, ArgNames
);
1248 std::string
DagInit::getAsString() const {
1249 std::string Result
= "(" + Val
->getAsString();
1250 if (!ValName
.empty())
1251 Result
+= ":" + ValName
;
1253 Result
+= " " + Args
[0]->getAsString();
1254 if (!ArgNames
[0].empty()) Result
+= ":$" + ArgNames
[0];
1255 for (unsigned i
= 1, e
= Args
.size(); i
!= e
; ++i
) {
1256 Result
+= ", " + Args
[i
]->getAsString();
1257 if (!ArgNames
[i
].empty()) Result
+= ":$" + ArgNames
[i
];
1260 return Result
+ ")";
1264 //===----------------------------------------------------------------------===//
1265 // Other implementations
1266 //===----------------------------------------------------------------------===//
1268 RecordVal::RecordVal(const std::string
&N
, RecTy
*T
, unsigned P
)
1269 : Name(N
), Ty(T
), Prefix(P
) {
1270 Value
= Ty
->convertValue(new UnsetInit());
1271 assert(Value
&& "Cannot create unset value for current type!");
1274 void RecordVal::dump() const { errs() << *this; }
1276 void RecordVal::print(raw_ostream
&OS
, bool PrintSem
) const {
1277 if (getPrefix()) OS
<< "field ";
1278 OS
<< *getType() << " " << getName();
1281 OS
<< " = " << *getValue();
1283 if (PrintSem
) OS
<< ";\n";
1286 unsigned Record::LastID
= 0;
1288 void Record::setName(const std::string
&Name
) {
1289 if (Records
.getDef(getName()) == this) {
1290 Records
.removeDef(getName());
1292 Records
.addDef(this);
1294 Records
.removeClass(getName());
1296 Records
.addClass(this);
1300 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1301 /// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1303 void Record::resolveReferencesTo(const RecordVal
*RV
) {
1304 for (unsigned i
= 0, e
= Values
.size(); i
!= e
; ++i
) {
1305 if (Init
*V
= Values
[i
].getValue())
1306 Values
[i
].setValue(V
->resolveReferences(*this, RV
));
1310 void Record::dump() const { errs() << *this; }
1312 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const Record
&R
) {
1315 const std::vector
<std::string
> &TArgs
= R
.getTemplateArgs();
1316 if (!TArgs
.empty()) {
1318 for (unsigned i
= 0, e
= TArgs
.size(); i
!= e
; ++i
) {
1320 const RecordVal
*RV
= R
.getValue(TArgs
[i
]);
1321 assert(RV
&& "Template argument record not found??");
1322 RV
->print(OS
, false);
1328 const std::vector
<Record
*> &SC
= R
.getSuperClasses();
1331 for (unsigned i
= 0, e
= SC
.size(); i
!= e
; ++i
)
1332 OS
<< " " << SC
[i
]->getName();
1336 const std::vector
<RecordVal
> &Vals
= R
.getValues();
1337 for (unsigned i
= 0, e
= Vals
.size(); i
!= e
; ++i
)
1338 if (Vals
[i
].getPrefix() && !R
.isTemplateArg(Vals
[i
].getName()))
1340 for (unsigned i
= 0, e
= Vals
.size(); i
!= e
; ++i
)
1341 if (!Vals
[i
].getPrefix() && !R
.isTemplateArg(Vals
[i
].getName()))
1347 /// getValueInit - Return the initializer for a value with the specified name,
1348 /// or throw an exception if the field does not exist.
1350 Init
*Record::getValueInit(StringRef FieldName
) const {
1351 const RecordVal
*R
= getValue(FieldName
);
1352 if (R
== 0 || R
->getValue() == 0)
1353 throw "Record `" + getName() + "' does not have a field named `" +
1354 FieldName
.str() + "'!\n";
1355 return R
->getValue();
1359 /// getValueAsString - This method looks up the specified field and returns its
1360 /// value as a string, throwing an exception if the field does not exist or if
1361 /// the value is not a string.
1363 std::string
Record::getValueAsString(StringRef FieldName
) const {
1364 const RecordVal
*R
= getValue(FieldName
);
1365 if (R
== 0 || R
->getValue() == 0)
1366 throw "Record `" + getName() + "' does not have a field named `" +
1367 FieldName
.str() + "'!\n";
1369 if (const StringInit
*SI
= dynamic_cast<const StringInit
*>(R
->getValue()))
1370 return SI
->getValue();
1371 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1372 "' does not have a string initializer!";
1375 /// getValueAsBitsInit - This method looks up the specified field and returns
1376 /// its value as a BitsInit, throwing an exception if the field does not exist
1377 /// or if the value is not the right type.
1379 BitsInit
*Record::getValueAsBitsInit(StringRef FieldName
) const {
1380 const RecordVal
*R
= getValue(FieldName
);
1381 if (R
== 0 || R
->getValue() == 0)
1382 throw "Record `" + getName() + "' does not have a field named `" +
1383 FieldName
.str() + "'!\n";
1385 if (BitsInit
*BI
= dynamic_cast<BitsInit
*>(R
->getValue()))
1387 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1388 "' does not have a BitsInit initializer!";
1391 /// getValueAsListInit - This method looks up the specified field and returns
1392 /// its value as a ListInit, throwing an exception if the field does not exist
1393 /// or if the value is not the right type.
1395 ListInit
*Record::getValueAsListInit(StringRef FieldName
) const {
1396 const RecordVal
*R
= getValue(FieldName
);
1397 if (R
== 0 || R
->getValue() == 0)
1398 throw "Record `" + getName() + "' does not have a field named `" +
1399 FieldName
.str() + "'!\n";
1401 if (ListInit
*LI
= dynamic_cast<ListInit
*>(R
->getValue()))
1403 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1404 "' does not have a list initializer!";
1407 /// getValueAsListOfDefs - This method looks up the specified field and returns
1408 /// its value as a vector of records, throwing an exception if the field does
1409 /// not exist or if the value is not the right type.
1411 std::vector
<Record
*>
1412 Record::getValueAsListOfDefs(StringRef FieldName
) const {
1413 ListInit
*List
= getValueAsListInit(FieldName
);
1414 std::vector
<Record
*> Defs
;
1415 for (unsigned i
= 0; i
< List
->getSize(); i
++) {
1416 if (DefInit
*DI
= dynamic_cast<DefInit
*>(List
->getElement(i
))) {
1417 Defs
.push_back(DI
->getDef());
1419 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1420 "' list is not entirely DefInit!";
1426 /// getValueAsInt - This method looks up the specified field and returns its
1427 /// value as an int64_t, throwing an exception if the field does not exist or if
1428 /// the value is not the right type.
1430 int64_t Record::getValueAsInt(StringRef FieldName
) const {
1431 const RecordVal
*R
= getValue(FieldName
);
1432 if (R
== 0 || R
->getValue() == 0)
1433 throw "Record `" + getName() + "' does not have a field named `" +
1434 FieldName
.str() + "'!\n";
1436 if (IntInit
*II
= dynamic_cast<IntInit
*>(R
->getValue()))
1437 return II
->getValue();
1438 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1439 "' does not have an int initializer!";
1442 /// getValueAsListOfInts - This method looks up the specified field and returns
1443 /// its value as a vector of integers, throwing an exception if the field does
1444 /// not exist or if the value is not the right type.
1446 std::vector
<int64_t>
1447 Record::getValueAsListOfInts(StringRef FieldName
) const {
1448 ListInit
*List
= getValueAsListInit(FieldName
);
1449 std::vector
<int64_t> Ints
;
1450 for (unsigned i
= 0; i
< List
->getSize(); i
++) {
1451 if (IntInit
*II
= dynamic_cast<IntInit
*>(List
->getElement(i
))) {
1452 Ints
.push_back(II
->getValue());
1454 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1455 "' does not have a list of ints initializer!";
1461 /// getValueAsDef - This method looks up the specified field and returns its
1462 /// value as a Record, throwing an exception if the field does not exist or if
1463 /// the value is not the right type.
1465 Record
*Record::getValueAsDef(StringRef FieldName
) const {
1466 const RecordVal
*R
= getValue(FieldName
);
1467 if (R
== 0 || R
->getValue() == 0)
1468 throw "Record `" + getName() + "' does not have a field named `" +
1469 FieldName
.str() + "'!\n";
1471 if (DefInit
*DI
= dynamic_cast<DefInit
*>(R
->getValue()))
1472 return DI
->getDef();
1473 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1474 "' does not have a def initializer!";
1477 /// getValueAsBit - This method looks up the specified field and returns its
1478 /// value as a bit, throwing an exception if the field does not exist or if
1479 /// the value is not the right type.
1481 bool Record::getValueAsBit(StringRef FieldName
) const {
1482 const RecordVal
*R
= getValue(FieldName
);
1483 if (R
== 0 || R
->getValue() == 0)
1484 throw "Record `" + getName() + "' does not have a field named `" +
1485 FieldName
.str() + "'!\n";
1487 if (BitInit
*BI
= dynamic_cast<BitInit
*>(R
->getValue()))
1488 return BI
->getValue();
1489 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1490 "' does not have a bit initializer!";
1493 /// getValueAsDag - This method looks up the specified field and returns its
1494 /// value as an Dag, throwing an exception if the field does not exist or if
1495 /// the value is not the right type.
1497 DagInit
*Record::getValueAsDag(StringRef FieldName
) const {
1498 const RecordVal
*R
= getValue(FieldName
);
1499 if (R
== 0 || R
->getValue() == 0)
1500 throw "Record `" + getName() + "' does not have a field named `" +
1501 FieldName
.str() + "'!\n";
1503 if (DagInit
*DI
= dynamic_cast<DagInit
*>(R
->getValue()))
1505 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1506 "' does not have a dag initializer!";
1509 std::string
Record::getValueAsCode(StringRef FieldName
) const {
1510 const RecordVal
*R
= getValue(FieldName
);
1511 if (R
== 0 || R
->getValue() == 0)
1512 throw "Record `" + getName() + "' does not have a field named `" +
1513 FieldName
.str() + "'!\n";
1515 if (const CodeInit
*CI
= dynamic_cast<const CodeInit
*>(R
->getValue()))
1516 return CI
->getValue();
1517 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1518 "' does not have a code initializer!";
1522 void MultiClass::dump() const {
1523 errs() << "Record:\n";
1526 errs() << "Defs:\n";
1527 for (RecordVector::const_iterator r
= DefPrototypes
.begin(),
1528 rend
= DefPrototypes
.end();
1536 void RecordKeeper::dump() const { errs() << *this; }
1538 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const RecordKeeper
&RK
) {
1539 OS
<< "------------- Classes -----------------\n";
1540 const std::map
<std::string
, Record
*> &Classes
= RK
.getClasses();
1541 for (std::map
<std::string
, Record
*>::const_iterator I
= Classes
.begin(),
1542 E
= Classes
.end(); I
!= E
; ++I
)
1543 OS
<< "class " << *I
->second
;
1545 OS
<< "------------- Defs -----------------\n";
1546 const std::map
<std::string
, Record
*> &Defs
= RK
.getDefs();
1547 for (std::map
<std::string
, Record
*>::const_iterator I
= Defs
.begin(),
1548 E
= Defs
.end(); I
!= E
; ++I
)
1549 OS
<< "def " << *I
->second
;
1554 /// getAllDerivedDefinitions - This method returns all concrete definitions
1555 /// that derive from the specified class name. If a class with the specified
1556 /// name does not exist, an error is printed and true is returned.
1557 std::vector
<Record
*>
1558 RecordKeeper::getAllDerivedDefinitions(const std::string
&ClassName
) const {
1559 Record
*Class
= Records
.getClass(ClassName
);
1561 throw "ERROR: Couldn't find the `" + ClassName
+ "' class!\n";
1563 std::vector
<Record
*> Defs
;
1564 for (std::map
<std::string
, Record
*>::const_iterator I
= getDefs().begin(),
1565 E
= getDefs().end(); I
!= E
; ++I
)
1566 if (I
->second
->isSubClassOf(Class
))
1567 Defs
.push_back(I
->second
);