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 if (Rec
== RHS
->getRecord() || RHS
->getRecord()->isSubClassOf(Rec
))
276 const std::vector
<Record
*> &SC
= Rec
->getSuperClasses();
277 for (unsigned i
= 0, e
= SC
.size(); i
!= e
; ++i
)
278 if (RHS
->getRecord()->isSubClassOf(SC
[i
]))
285 /// resolveTypes - Find a common type that T1 and T2 convert to.
286 /// Return 0 if no such type exists.
288 RecTy
*llvm::resolveTypes(RecTy
*T1
, RecTy
*T2
) {
289 if (!T1
->typeIsConvertibleTo(T2
)) {
290 if (!T2
->typeIsConvertibleTo(T1
)) {
291 // If one is a Record type, check superclasses
292 RecordRecTy
*RecTy1
= dynamic_cast<RecordRecTy
*>(T1
);
294 // See if T2 inherits from a type T1 also inherits from
295 const std::vector
<Record
*> &T1SuperClasses
=
296 RecTy1
->getRecord()->getSuperClasses();
297 for(std::vector
<Record
*>::const_iterator i
= T1SuperClasses
.begin(),
298 iend
= T1SuperClasses
.end();
301 RecordRecTy
*SuperRecTy1
= new RecordRecTy(*i
);
302 RecTy
*NewType1
= resolveTypes(SuperRecTy1
, T2
);
304 if (NewType1
!= SuperRecTy1
) {
311 RecordRecTy
*RecTy2
= dynamic_cast<RecordRecTy
*>(T2
);
313 // See if T1 inherits from a type T2 also inherits from
314 const std::vector
<Record
*> &T2SuperClasses
=
315 RecTy2
->getRecord()->getSuperClasses();
316 for (std::vector
<Record
*>::const_iterator i
= T2SuperClasses
.begin(),
317 iend
= T2SuperClasses
.end();
320 RecordRecTy
*SuperRecTy2
= new RecordRecTy(*i
);
321 RecTy
*NewType2
= resolveTypes(T1
, SuperRecTy2
);
323 if (NewType2
!= SuperRecTy2
) {
338 //===----------------------------------------------------------------------===//
339 // Initializer implementations
340 //===----------------------------------------------------------------------===//
342 void Init::dump() const { return print(errs()); }
344 Init
*BitsInit::convertInitializerBitRange(const std::vector
<unsigned> &Bits
) {
345 BitsInit
*BI
= new BitsInit(Bits
.size());
346 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
) {
347 if (Bits
[i
] >= getNumBits()) {
351 BI
->setBit(i
, getBit(Bits
[i
]));
356 std::string
BitsInit::getAsString() const {
357 std::string Result
= "{ ";
358 for (unsigned i
= 0, e
= getNumBits(); i
!= e
; ++i
) {
359 if (i
) Result
+= ", ";
360 if (Init
*Bit
= getBit(e
-i
-1))
361 Result
+= Bit
->getAsString();
365 return Result
+ " }";
368 // resolveReferences - If there are any field references that refer to fields
369 // that have been filled in, we can propagate the values now.
371 Init
*BitsInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
372 bool Changed
= false;
373 BitsInit
*New
= new BitsInit(getNumBits());
375 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
) {
377 Init
*CurBit
= getBit(i
);
381 CurBit
= CurBit
->resolveReferences(R
, RV
);
382 Changed
|= B
!= CurBit
;
383 } while (B
!= CurBit
);
384 New
->setBit(i
, CurBit
);
393 std::string
IntInit::getAsString() const {
394 return itostr(Value
);
397 Init
*IntInit::convertInitializerBitRange(const std::vector
<unsigned> &Bits
) {
398 BitsInit
*BI
= new BitsInit(Bits
.size());
400 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
) {
405 BI
->setBit(i
, new BitInit(Value
& (INT64_C(1) << Bits
[i
])));
410 Init
*ListInit::convertInitListSlice(const std::vector
<unsigned> &Elements
) {
411 std::vector
<Init
*> Vals
;
412 for (unsigned i
= 0, e
= Elements
.size(); i
!= e
; ++i
) {
413 if (Elements
[i
] >= getSize())
415 Vals
.push_back(getElement(Elements
[i
]));
417 return new ListInit(Vals
, getType());
420 Record
*ListInit::getElementAsRecord(unsigned i
) const {
421 assert(i
< Values
.size() && "List element index out of range!");
422 DefInit
*DI
= dynamic_cast<DefInit
*>(Values
[i
]);
423 if (DI
== 0) throw "Expected record in list!";
427 Init
*ListInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
428 std::vector
<Init
*> Resolved
;
429 Resolved
.reserve(getSize());
430 bool Changed
= false;
432 for (unsigned i
= 0, e
= getSize(); i
!= e
; ++i
) {
434 Init
*CurElt
= getElement(i
);
438 CurElt
= CurElt
->resolveReferences(R
, RV
);
439 Changed
|= E
!= CurElt
;
440 } while (E
!= CurElt
);
441 Resolved
.push_back(E
);
445 return new ListInit(Resolved
, getType());
449 Init
*ListInit::resolveListElementReference(Record
&R
, const RecordVal
*IRV
,
451 if (Elt
>= getSize())
452 return 0; // Out of range reference.
453 Init
*E
= getElement(Elt
);
454 // If the element is set to some value, or if we are resolving a reference
455 // to a specific variable and that variable is explicitly unset, then
456 // replace the VarListElementInit with it.
457 if (IRV
|| !dynamic_cast<UnsetInit
*>(E
))
462 std::string
ListInit::getAsString() const {
463 std::string Result
= "[";
464 for (unsigned i
= 0, e
= Values
.size(); i
!= e
; ++i
) {
465 if (i
) Result
+= ", ";
466 Result
+= Values
[i
]->getAsString();
471 Init
*OpInit::resolveBitReference(Record
&R
, const RecordVal
*IRV
,
473 Init
*Folded
= Fold(&R
, 0);
475 if (Folded
!= this) {
476 TypedInit
*Typed
= dynamic_cast<TypedInit
*>(Folded
);
478 return Typed
->resolveBitReference(R
, IRV
, Bit
);
485 Init
*OpInit::resolveListElementReference(Record
&R
, const RecordVal
*IRV
,
487 Init
*Folded
= Fold(&R
, 0);
489 if (Folded
!= this) {
490 TypedInit
*Typed
= dynamic_cast<TypedInit
*>(Folded
);
492 return Typed
->resolveListElementReference(R
, IRV
, Elt
);
499 Init
*UnOpInit::Fold(Record
*CurRec
, MultiClass
*CurMultiClass
) {
500 switch (getOpcode()) {
501 default: assert(0 && "Unknown unop");
503 if (getType()->getAsString() == "string") {
504 StringInit
*LHSs
= dynamic_cast<StringInit
*>(LHS
);
509 DefInit
*LHSd
= dynamic_cast<DefInit
*>(LHS
);
511 return new StringInit(LHSd
->getDef()->getName());
514 StringInit
*LHSs
= dynamic_cast<StringInit
*>(LHS
);
516 std::string Name
= LHSs
->getValue();
518 // From TGParser::ParseIDValue
520 if (const RecordVal
*RV
= CurRec
->getValue(Name
)) {
521 if (RV
->getType() != getType()) {
522 throw "type mismatch in nameconcat";
524 return new VarInit(Name
, RV
->getType());
527 std::string TemplateArgName
= CurRec
->getName()+":"+Name
;
528 if (CurRec
->isTemplateArg(TemplateArgName
)) {
529 const RecordVal
*RV
= CurRec
->getValue(TemplateArgName
);
530 assert(RV
&& "Template arg doesn't exist??");
532 if (RV
->getType() != getType()) {
533 throw "type mismatch in nameconcat";
536 return new VarInit(TemplateArgName
, RV
->getType());
541 std::string MCName
= CurMultiClass
->Rec
.getName()+"::"+Name
;
542 if (CurMultiClass
->Rec
.isTemplateArg(MCName
)) {
543 const RecordVal
*RV
= CurMultiClass
->Rec
.getValue(MCName
);
544 assert(RV
&& "Template arg doesn't exist??");
546 if (RV
->getType() != getType()) {
547 throw "type mismatch in nameconcat";
550 return new VarInit(MCName
, RV
->getType());
554 if (Record
*D
= Records
.getDef(Name
))
555 return new DefInit(D
);
557 errs() << "Variable not defined: '" + Name
+ "'\n";
558 assert(0 && "Variable not found");
565 ListInit
*LHSl
= dynamic_cast<ListInit
*>(LHS
);
567 if (LHSl
->getSize() == 0) {
568 assert(0 && "Empty list in car");
571 return LHSl
->getElement(0);
576 ListInit
*LHSl
= dynamic_cast<ListInit
*>(LHS
);
578 if (LHSl
->getSize() == 0) {
579 assert(0 && "Empty list in cdr");
582 ListInit
*Result
= new ListInit(LHSl
->begin()+1, LHSl
->end(),
589 ListInit
*LHSl
= dynamic_cast<ListInit
*>(LHS
);
591 if (LHSl
->getSize() == 0) {
592 return new IntInit(1);
594 return new IntInit(0);
597 StringInit
*LHSs
= dynamic_cast<StringInit
*>(LHS
);
599 if (LHSs
->getValue().empty()) {
600 return new IntInit(1);
602 return new IntInit(0);
612 Init
*UnOpInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
613 Init
*lhs
= LHS
->resolveReferences(R
, RV
);
616 return (new UnOpInit(getOpcode(), lhs
, getType()))->Fold(&R
, 0);
620 std::string
UnOpInit::getAsString() const {
623 case CAST
: Result
= "!cast<" + getType()->getAsString() + ">"; break;
624 case CAR
: Result
= "!car"; break;
625 case CDR
: Result
= "!cdr"; break;
626 case LNULL
: Result
= "!null"; break;
628 return Result
+ "(" + LHS
->getAsString() + ")";
631 RecTy
*UnOpInit::getFieldType(const std::string
&FieldName
) const {
632 switch (getOpcode()) {
633 default: assert(0 && "Unknown unop");
635 RecordRecTy
*RecordType
= dynamic_cast<RecordRecTy
*>(getType());
637 RecordVal
*Field
= RecordType
->getRecord()->getValue(FieldName
);
639 return Field
->getType();
648 Init
*BinOpInit::Fold(Record
*CurRec
, MultiClass
*CurMultiClass
) {
649 switch (getOpcode()) {
650 default: assert(0 && "Unknown binop");
652 DagInit
*LHSs
= dynamic_cast<DagInit
*>(LHS
);
653 DagInit
*RHSs
= dynamic_cast<DagInit
*>(RHS
);
655 DefInit
*LOp
= dynamic_cast<DefInit
*>(LHSs
->getOperator());
656 DefInit
*ROp
= dynamic_cast<DefInit
*>(RHSs
->getOperator());
657 if (LOp
== 0 || ROp
== 0 || LOp
->getDef() != ROp
->getDef())
658 throw "Concated Dag operators do not match!";
659 std::vector
<Init
*> Args
;
660 std::vector
<std::string
> ArgNames
;
661 for (unsigned i
= 0, e
= LHSs
->getNumArgs(); i
!= e
; ++i
) {
662 Args
.push_back(LHSs
->getArg(i
));
663 ArgNames
.push_back(LHSs
->getArgName(i
));
665 for (unsigned i
= 0, e
= RHSs
->getNumArgs(); i
!= e
; ++i
) {
666 Args
.push_back(RHSs
->getArg(i
));
667 ArgNames
.push_back(RHSs
->getArgName(i
));
669 return new DagInit(LHSs
->getOperator(), "", Args
, ArgNames
);
674 StringInit
*LHSs
= dynamic_cast<StringInit
*>(LHS
);
675 StringInit
*RHSs
= dynamic_cast<StringInit
*>(RHS
);
677 return new StringInit(LHSs
->getValue() + RHSs
->getValue());
681 StringInit
*LHSs
= dynamic_cast<StringInit
*>(LHS
);
682 StringInit
*RHSs
= dynamic_cast<StringInit
*>(RHS
);
684 std::string
Name(LHSs
->getValue() + RHSs
->getValue());
686 // From TGParser::ParseIDValue
688 if (const RecordVal
*RV
= CurRec
->getValue(Name
)) {
689 if (RV
->getType() != getType()) {
690 throw "type mismatch in nameconcat";
692 return new VarInit(Name
, RV
->getType());
695 std::string TemplateArgName
= CurRec
->getName()+":"+Name
;
696 if (CurRec
->isTemplateArg(TemplateArgName
)) {
697 const RecordVal
*RV
= CurRec
->getValue(TemplateArgName
);
698 assert(RV
&& "Template arg doesn't exist??");
700 if (RV
->getType() != getType()) {
701 throw "type mismatch in nameconcat";
704 return new VarInit(TemplateArgName
, RV
->getType());
709 std::string MCName
= CurMultiClass
->Rec
.getName()+"::"+Name
;
710 if (CurMultiClass
->Rec
.isTemplateArg(MCName
)) {
711 const RecordVal
*RV
= CurMultiClass
->Rec
.getValue(MCName
);
712 assert(RV
&& "Template arg doesn't exist??");
714 if (RV
->getType() != getType()) {
715 throw "type mismatch in nameconcat";
718 return new VarInit(MCName
, RV
->getType());
722 if (Record
*D
= Records
.getDef(Name
))
723 return new DefInit(D
);
725 errs() << "Variable not defined in !nameconcat: '" + Name
+ "'\n";
726 assert(0 && "Variable not found in !nameconcat");
732 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
733 // to string objects.
735 dynamic_cast<IntInit
*>(LHS
->convertInitializerTo(new IntRecTy()));
737 dynamic_cast<IntInit
*>(RHS
->convertInitializerTo(new IntRecTy()));
740 return new IntInit(L
->getValue() == R
->getValue());
742 StringInit
*LHSs
= dynamic_cast<StringInit
*>(LHS
);
743 StringInit
*RHSs
= dynamic_cast<StringInit
*>(RHS
);
745 // Make sure we've resolved
747 return new IntInit(LHSs
->getValue() == RHSs
->getValue());
754 IntInit
*LHSi
= dynamic_cast<IntInit
*>(LHS
);
755 IntInit
*RHSi
= dynamic_cast<IntInit
*>(RHS
);
757 int64_t LHSv
= LHSi
->getValue(), RHSv
= RHSi
->getValue();
759 switch (getOpcode()) {
760 default: assert(0 && "Bad opcode!");
761 case SHL
: Result
= LHSv
<< RHSv
; break;
762 case SRA
: Result
= LHSv
>> RHSv
; break;
763 case SRL
: Result
= (uint64_t)LHSv
>> (uint64_t)RHSv
; break;
765 return new IntInit(Result
);
773 Init
*BinOpInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
774 Init
*lhs
= LHS
->resolveReferences(R
, RV
);
775 Init
*rhs
= RHS
->resolveReferences(R
, RV
);
777 if (LHS
!= lhs
|| RHS
!= rhs
)
778 return (new BinOpInit(getOpcode(), lhs
, rhs
, getType()))->Fold(&R
, 0);
782 std::string
BinOpInit::getAsString() const {
785 case CONCAT
: Result
= "!con"; break;
786 case SHL
: Result
= "!shl"; break;
787 case SRA
: Result
= "!sra"; break;
788 case SRL
: Result
= "!srl"; break;
789 case EQ
: Result
= "!eq"; break;
790 case STRCONCAT
: Result
= "!strconcat"; break;
792 Result
= "!nameconcat<" + getType()->getAsString() + ">"; break;
794 return Result
+ "(" + LHS
->getAsString() + ", " + RHS
->getAsString() + ")";
797 static Init
*ForeachHelper(Init
*LHS
, Init
*MHS
, Init
*RHS
, RecTy
*Type
,
798 Record
*CurRec
, MultiClass
*CurMultiClass
);
800 static Init
*EvaluateOperation(OpInit
*RHSo
, Init
*LHS
, Init
*Arg
,
801 RecTy
*Type
, Record
*CurRec
,
802 MultiClass
*CurMultiClass
) {
803 std::vector
<Init
*> NewOperands
;
805 TypedInit
*TArg
= dynamic_cast<TypedInit
*>(Arg
);
807 // If this is a dag, recurse
808 if (TArg
&& TArg
->getType()->getAsString() == "dag") {
809 Init
*Result
= ForeachHelper(LHS
, Arg
, RHSo
, Type
,
810 CurRec
, CurMultiClass
);
818 for (int i
= 0; i
< RHSo
->getNumOperands(); ++i
) {
819 OpInit
*RHSoo
= dynamic_cast<OpInit
*>(RHSo
->getOperand(i
));
822 Init
*Result
= EvaluateOperation(RHSoo
, LHS
, Arg
,
823 Type
, CurRec
, CurMultiClass
);
825 NewOperands
.push_back(Result
);
827 NewOperands
.push_back(Arg
);
829 } else if (LHS
->getAsString() == RHSo
->getOperand(i
)->getAsString()) {
830 NewOperands
.push_back(Arg
);
832 NewOperands
.push_back(RHSo
->getOperand(i
));
836 // Now run the operator and use its result as the new leaf
837 OpInit
*NewOp
= RHSo
->clone(NewOperands
);
838 Init
*NewVal
= NewOp
->Fold(CurRec
, CurMultiClass
);
839 if (NewVal
!= NewOp
) {
846 static Init
*ForeachHelper(Init
*LHS
, Init
*MHS
, Init
*RHS
, RecTy
*Type
,
847 Record
*CurRec
, MultiClass
*CurMultiClass
) {
848 DagInit
*MHSd
= dynamic_cast<DagInit
*>(MHS
);
849 ListInit
*MHSl
= dynamic_cast<ListInit
*>(MHS
);
851 DagRecTy
*DagType
= dynamic_cast<DagRecTy
*>(Type
);
852 ListRecTy
*ListType
= dynamic_cast<ListRecTy
*>(Type
);
854 OpInit
*RHSo
= dynamic_cast<OpInit
*>(RHS
);
857 errs() << "!foreach requires an operator\n";
858 assert(0 && "No operator for !foreach");
861 TypedInit
*LHSt
= dynamic_cast<TypedInit
*>(LHS
);
864 errs() << "!foreach requires typed variable\n";
865 assert(0 && "No typed variable for !foreach");
868 if ((MHSd
&& DagType
) || (MHSl
&& ListType
)) {
870 Init
*Val
= MHSd
->getOperator();
871 Init
*Result
= EvaluateOperation(RHSo
, LHS
, Val
,
872 Type
, CurRec
, CurMultiClass
);
877 std::vector
<std::pair
<Init
*, std::string
> > args
;
878 for (unsigned int i
= 0; i
< MHSd
->getNumArgs(); ++i
) {
881 Arg
= MHSd
->getArg(i
);
882 ArgName
= MHSd
->getArgName(i
);
885 Init
*Result
= EvaluateOperation(RHSo
, LHS
, Arg
, Type
,
886 CurRec
, CurMultiClass
);
891 // TODO: Process arg names
892 args
.push_back(std::make_pair(Arg
, ArgName
));
895 return new DagInit(Val
, "", args
);
898 std::vector
<Init
*> NewOperands
;
899 std::vector
<Init
*> NewList(MHSl
->begin(), MHSl
->end());
901 for (ListInit::iterator li
= NewList
.begin(),
902 liend
= NewList
.end();
907 for(int i
= 0; i
< RHSo
->getNumOperands(); ++i
) {
908 // First, replace the foreach variable with the list item
909 if (LHS
->getAsString() == RHSo
->getOperand(i
)->getAsString()) {
910 NewOperands
.push_back(Item
);
912 NewOperands
.push_back(RHSo
->getOperand(i
));
916 // Now run the operator and use its result as the new list item
917 OpInit
*NewOp
= RHSo
->clone(NewOperands
);
918 Init
*NewItem
= NewOp
->Fold(CurRec
, CurMultiClass
);
919 if (NewItem
!= NewOp
) {
924 return new ListInit(NewList
, MHSl
->getType());
930 Init
*TernOpInit::Fold(Record
*CurRec
, MultiClass
*CurMultiClass
) {
931 switch (getOpcode()) {
932 default: assert(0 && "Unknown binop");
934 DefInit
*LHSd
= dynamic_cast<DefInit
*>(LHS
);
935 VarInit
*LHSv
= dynamic_cast<VarInit
*>(LHS
);
936 StringInit
*LHSs
= dynamic_cast<StringInit
*>(LHS
);
938 DefInit
*MHSd
= dynamic_cast<DefInit
*>(MHS
);
939 VarInit
*MHSv
= dynamic_cast<VarInit
*>(MHS
);
940 StringInit
*MHSs
= dynamic_cast<StringInit
*>(MHS
);
942 DefInit
*RHSd
= dynamic_cast<DefInit
*>(RHS
);
943 VarInit
*RHSv
= dynamic_cast<VarInit
*>(RHS
);
944 StringInit
*RHSs
= dynamic_cast<StringInit
*>(RHS
);
946 if ((LHSd
&& MHSd
&& RHSd
)
947 || (LHSv
&& MHSv
&& RHSv
)
948 || (LHSs
&& MHSs
&& RHSs
)) {
950 Record
*Val
= RHSd
->getDef();
951 if (LHSd
->getAsString() == RHSd
->getAsString()) {
952 Val
= MHSd
->getDef();
954 return new DefInit(Val
);
957 std::string Val
= RHSv
->getName();
958 if (LHSv
->getAsString() == RHSv
->getAsString()) {
959 Val
= MHSv
->getName();
961 return new VarInit(Val
, getType());
964 std::string Val
= RHSs
->getValue();
966 std::string::size_type found
;
967 std::string::size_type idx
= 0;
969 found
= Val
.find(LHSs
->getValue(), idx
);
970 if (found
!= std::string::npos
) {
971 Val
.replace(found
, LHSs
->getValue().size(), MHSs
->getValue());
973 idx
= found
+ MHSs
->getValue().size();
974 } while (found
!= std::string::npos
);
976 return new StringInit(Val
);
983 Init
*Result
= ForeachHelper(LHS
, MHS
, RHS
, getType(),
984 CurRec
, CurMultiClass
);
992 IntInit
*LHSi
= dynamic_cast<IntInit
*>(LHS
);
993 if (Init
*I
= LHS
->convertInitializerTo(new IntRecTy()))
994 LHSi
= dynamic_cast<IntInit
*>(I
);
996 if (LHSi
->getValue()) {
1009 Init
*TernOpInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
1010 Init
*lhs
= LHS
->resolveReferences(R
, RV
);
1012 if (Opc
== IF
&& lhs
!= LHS
) {
1013 IntInit
*Value
= dynamic_cast<IntInit
*>(lhs
);
1014 if (Init
*I
= lhs
->convertInitializerTo(new IntRecTy()))
1015 Value
= dynamic_cast<IntInit
*>(I
);
1018 if (Value
->getValue()) {
1019 Init
*mhs
= MHS
->resolveReferences(R
, RV
);
1020 return (new TernOpInit(getOpcode(), lhs
, mhs
,
1021 RHS
, getType()))->Fold(&R
, 0);
1023 Init
*rhs
= RHS
->resolveReferences(R
, RV
);
1024 return (new TernOpInit(getOpcode(), lhs
, MHS
,
1025 rhs
, getType()))->Fold(&R
, 0);
1030 Init
*mhs
= MHS
->resolveReferences(R
, RV
);
1031 Init
*rhs
= RHS
->resolveReferences(R
, RV
);
1033 if (LHS
!= lhs
|| MHS
!= mhs
|| RHS
!= rhs
)
1034 return (new TernOpInit(getOpcode(), lhs
, mhs
, rhs
, getType()))->Fold(&R
, 0);
1038 std::string
TernOpInit::getAsString() const {
1041 case SUBST
: Result
= "!subst"; break;
1042 case FOREACH
: Result
= "!foreach"; break;
1043 case IF
: Result
= "!if"; break;
1045 return Result
+ "(" + LHS
->getAsString() + ", " + MHS
->getAsString() + ", "
1046 + RHS
->getAsString() + ")";
1049 Init
*TypedInit::convertInitializerBitRange(const std::vector
<unsigned> &Bits
) {
1050 BitsRecTy
*T
= dynamic_cast<BitsRecTy
*>(getType());
1051 if (T
== 0) return 0; // Cannot subscript a non-bits variable...
1052 unsigned NumBits
= T
->getNumBits();
1054 BitsInit
*BI
= new BitsInit(Bits
.size());
1055 for (unsigned i
= 0, e
= Bits
.size(); i
!= e
; ++i
) {
1056 if (Bits
[i
] >= NumBits
) {
1060 BI
->setBit(i
, new VarBitInit(this, Bits
[i
]));
1065 Init
*TypedInit::convertInitListSlice(const std::vector
<unsigned> &Elements
) {
1066 ListRecTy
*T
= dynamic_cast<ListRecTy
*>(getType());
1067 if (T
== 0) return 0; // Cannot subscript a non-list variable...
1069 if (Elements
.size() == 1)
1070 return new VarListElementInit(this, Elements
[0]);
1072 std::vector
<Init
*> ListInits
;
1073 ListInits
.reserve(Elements
.size());
1074 for (unsigned i
= 0, e
= Elements
.size(); i
!= e
; ++i
)
1075 ListInits
.push_back(new VarListElementInit(this, Elements
[i
]));
1076 return new ListInit(ListInits
, T
);
1080 Init
*VarInit::resolveBitReference(Record
&R
, const RecordVal
*IRV
,
1082 if (R
.isTemplateArg(getName())) return 0;
1083 if (IRV
&& IRV
->getName() != getName()) return 0;
1085 RecordVal
*RV
= R
.getValue(getName());
1086 assert(RV
&& "Reference to a non-existent variable?");
1087 assert(dynamic_cast<BitsInit
*>(RV
->getValue()));
1088 BitsInit
*BI
= (BitsInit
*)RV
->getValue();
1090 assert(Bit
< BI
->getNumBits() && "Bit reference out of range!");
1091 Init
*B
= BI
->getBit(Bit
);
1093 // If the bit is set to some value, or if we are resolving a reference to a
1094 // specific variable and that variable is explicitly unset, then replace the
1095 // VarBitInit with it.
1096 if (IRV
|| !dynamic_cast<UnsetInit
*>(B
))
1101 Init
*VarInit::resolveListElementReference(Record
&R
, const RecordVal
*IRV
,
1103 if (R
.isTemplateArg(getName())) return 0;
1104 if (IRV
&& IRV
->getName() != getName()) return 0;
1106 RecordVal
*RV
= R
.getValue(getName());
1107 assert(RV
&& "Reference to a non-existent variable?");
1108 ListInit
*LI
= dynamic_cast<ListInit
*>(RV
->getValue());
1110 VarInit
*VI
= dynamic_cast<VarInit
*>(RV
->getValue());
1111 assert(VI
&& "Invalid list element!");
1112 return new VarListElementInit(VI
, Elt
);
1115 if (Elt
>= LI
->getSize())
1116 return 0; // Out of range reference.
1117 Init
*E
= LI
->getElement(Elt
);
1118 // If the element is set to some value, or if we are resolving a reference
1119 // to a specific variable and that variable is explicitly unset, then
1120 // replace the VarListElementInit with it.
1121 if (IRV
|| !dynamic_cast<UnsetInit
*>(E
))
1127 RecTy
*VarInit::getFieldType(const std::string
&FieldName
) const {
1128 if (RecordRecTy
*RTy
= dynamic_cast<RecordRecTy
*>(getType()))
1129 if (const RecordVal
*RV
= RTy
->getRecord()->getValue(FieldName
))
1130 return RV
->getType();
1134 Init
*VarInit::getFieldInit(Record
&R
, const RecordVal
*RV
,
1135 const std::string
&FieldName
) const {
1136 if (dynamic_cast<RecordRecTy
*>(getType()))
1137 if (const RecordVal
*Val
= R
.getValue(VarName
)) {
1138 if (RV
!= Val
&& (RV
|| dynamic_cast<UnsetInit
*>(Val
->getValue())))
1140 Init
*TheInit
= Val
->getValue();
1141 assert(TheInit
!= this && "Infinite loop detected!");
1142 if (Init
*I
= TheInit
->getFieldInit(R
, RV
, FieldName
))
1150 /// resolveReferences - This method is used by classes that refer to other
1151 /// variables which may not be defined at the time the expression is formed.
1152 /// If a value is set for the variable later, this method will be called on
1153 /// users of the value to allow the value to propagate out.
1155 Init
*VarInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
1156 if (RecordVal
*Val
= R
.getValue(VarName
))
1157 if (RV
== Val
|| (RV
== 0 && !dynamic_cast<UnsetInit
*>(Val
->getValue())))
1158 return Val
->getValue();
1162 std::string
VarBitInit::getAsString() const {
1163 return TI
->getAsString() + "{" + utostr(Bit
) + "}";
1166 Init
*VarBitInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
1167 if (Init
*I
= getVariable()->resolveBitReference(R
, RV
, getBitNum()))
1172 std::string
VarListElementInit::getAsString() const {
1173 return TI
->getAsString() + "[" + utostr(Element
) + "]";
1176 Init
*VarListElementInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
1177 if (Init
*I
= getVariable()->resolveListElementReference(R
, RV
,
1183 Init
*VarListElementInit::resolveBitReference(Record
&R
, const RecordVal
*RV
,
1185 // FIXME: This should be implemented, to support references like:
1186 // bit B = AA[0]{1};
1190 Init
*VarListElementInit::
1191 resolveListElementReference(Record
&R
, const RecordVal
*RV
, unsigned Elt
) {
1192 // FIXME: This should be implemented, to support references like:
1193 // int B = AA[0][1];
1197 RecTy
*DefInit::getFieldType(const std::string
&FieldName
) const {
1198 if (const RecordVal
*RV
= Def
->getValue(FieldName
))
1199 return RV
->getType();
1203 Init
*DefInit::getFieldInit(Record
&R
, const RecordVal
*RV
,
1204 const std::string
&FieldName
) const {
1205 return Def
->getValue(FieldName
)->getValue();
1209 std::string
DefInit::getAsString() const {
1210 return Def
->getName();
1213 Init
*FieldInit::resolveBitReference(Record
&R
, const RecordVal
*RV
,
1215 if (Init
*BitsVal
= Rec
->getFieldInit(R
, RV
, FieldName
))
1216 if (BitsInit
*BI
= dynamic_cast<BitsInit
*>(BitsVal
)) {
1217 assert(Bit
< BI
->getNumBits() && "Bit reference out of range!");
1218 Init
*B
= BI
->getBit(Bit
);
1220 if (dynamic_cast<BitInit
*>(B
)) // If the bit is set...
1221 return B
; // Replace the VarBitInit with it.
1226 Init
*FieldInit::resolveListElementReference(Record
&R
, const RecordVal
*RV
,
1228 if (Init
*ListVal
= Rec
->getFieldInit(R
, RV
, FieldName
))
1229 if (ListInit
*LI
= dynamic_cast<ListInit
*>(ListVal
)) {
1230 if (Elt
>= LI
->getSize()) return 0;
1231 Init
*E
= LI
->getElement(Elt
);
1233 // If the element is set to some value, or if we are resolving a
1234 // reference to a specific variable and that variable is explicitly
1235 // unset, then replace the VarListElementInit with it.
1236 if (RV
|| !dynamic_cast<UnsetInit
*>(E
))
1242 Init
*FieldInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
1243 Init
*NewRec
= RV
? Rec
->resolveReferences(R
, RV
) : Rec
;
1245 Init
*BitsVal
= NewRec
->getFieldInit(R
, RV
, FieldName
);
1247 Init
*BVR
= BitsVal
->resolveReferences(R
, RV
);
1248 return BVR
->isComplete() ? BVR
: this;
1251 if (NewRec
!= Rec
) {
1252 return new FieldInit(NewRec
, FieldName
);
1257 Init
*DagInit::resolveReferences(Record
&R
, const RecordVal
*RV
) {
1258 std::vector
<Init
*> NewArgs
;
1259 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; ++i
)
1260 NewArgs
.push_back(Args
[i
]->resolveReferences(R
, RV
));
1262 Init
*Op
= Val
->resolveReferences(R
, RV
);
1264 if (Args
!= NewArgs
|| Op
!= Val
)
1265 return new DagInit(Op
, ValName
, NewArgs
, ArgNames
);
1271 std::string
DagInit::getAsString() const {
1272 std::string Result
= "(" + Val
->getAsString();
1273 if (!ValName
.empty())
1274 Result
+= ":" + ValName
;
1276 Result
+= " " + Args
[0]->getAsString();
1277 if (!ArgNames
[0].empty()) Result
+= ":$" + ArgNames
[0];
1278 for (unsigned i
= 1, e
= Args
.size(); i
!= e
; ++i
) {
1279 Result
+= ", " + Args
[i
]->getAsString();
1280 if (!ArgNames
[i
].empty()) Result
+= ":$" + ArgNames
[i
];
1283 return Result
+ ")";
1287 //===----------------------------------------------------------------------===//
1288 // Other implementations
1289 //===----------------------------------------------------------------------===//
1291 RecordVal::RecordVal(const std::string
&N
, RecTy
*T
, unsigned P
)
1292 : Name(N
), Ty(T
), Prefix(P
) {
1293 Value
= Ty
->convertValue(new UnsetInit());
1294 assert(Value
&& "Cannot create unset value for current type!");
1297 void RecordVal::dump() const { errs() << *this; }
1299 void RecordVal::print(raw_ostream
&OS
, bool PrintSem
) const {
1300 if (getPrefix()) OS
<< "field ";
1301 OS
<< *getType() << " " << getName();
1304 OS
<< " = " << *getValue();
1306 if (PrintSem
) OS
<< ";\n";
1309 unsigned Record::LastID
= 0;
1311 void Record::setName(const std::string
&Name
) {
1312 if (Records
.getDef(getName()) == this) {
1313 Records
.removeDef(getName());
1315 Records
.addDef(this);
1317 Records
.removeClass(getName());
1319 Records
.addClass(this);
1323 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1324 /// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1326 void Record::resolveReferencesTo(const RecordVal
*RV
) {
1327 for (unsigned i
= 0, e
= Values
.size(); i
!= e
; ++i
) {
1328 if (Init
*V
= Values
[i
].getValue())
1329 Values
[i
].setValue(V
->resolveReferences(*this, RV
));
1333 void Record::dump() const { errs() << *this; }
1335 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const Record
&R
) {
1338 const std::vector
<std::string
> &TArgs
= R
.getTemplateArgs();
1339 if (!TArgs
.empty()) {
1341 for (unsigned i
= 0, e
= TArgs
.size(); i
!= e
; ++i
) {
1343 const RecordVal
*RV
= R
.getValue(TArgs
[i
]);
1344 assert(RV
&& "Template argument record not found??");
1345 RV
->print(OS
, false);
1351 const std::vector
<Record
*> &SC
= R
.getSuperClasses();
1354 for (unsigned i
= 0, e
= SC
.size(); i
!= e
; ++i
)
1355 OS
<< " " << SC
[i
]->getName();
1359 const std::vector
<RecordVal
> &Vals
= R
.getValues();
1360 for (unsigned i
= 0, e
= Vals
.size(); i
!= e
; ++i
)
1361 if (Vals
[i
].getPrefix() && !R
.isTemplateArg(Vals
[i
].getName()))
1363 for (unsigned i
= 0, e
= Vals
.size(); i
!= e
; ++i
)
1364 if (!Vals
[i
].getPrefix() && !R
.isTemplateArg(Vals
[i
].getName()))
1370 /// getValueInit - Return the initializer for a value with the specified name,
1371 /// or throw an exception if the field does not exist.
1373 Init
*Record::getValueInit(StringRef FieldName
) const {
1374 const RecordVal
*R
= getValue(FieldName
);
1375 if (R
== 0 || R
->getValue() == 0)
1376 throw "Record `" + getName() + "' does not have a field named `" +
1377 FieldName
.str() + "'!\n";
1378 return R
->getValue();
1382 /// getValueAsString - This method looks up the specified field and returns its
1383 /// value as a string, throwing an exception if the field does not exist or if
1384 /// the value is not a string.
1386 std::string
Record::getValueAsString(StringRef FieldName
) const {
1387 const RecordVal
*R
= getValue(FieldName
);
1388 if (R
== 0 || R
->getValue() == 0)
1389 throw "Record `" + getName() + "' does not have a field named `" +
1390 FieldName
.str() + "'!\n";
1392 if (const StringInit
*SI
= dynamic_cast<const StringInit
*>(R
->getValue()))
1393 return SI
->getValue();
1394 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1395 "' does not have a string initializer!";
1398 /// getValueAsBitsInit - This method looks up the specified field and returns
1399 /// its value as a BitsInit, throwing an exception if the field does not exist
1400 /// or if the value is not the right type.
1402 BitsInit
*Record::getValueAsBitsInit(StringRef FieldName
) const {
1403 const RecordVal
*R
= getValue(FieldName
);
1404 if (R
== 0 || R
->getValue() == 0)
1405 throw "Record `" + getName() + "' does not have a field named `" +
1406 FieldName
.str() + "'!\n";
1408 if (BitsInit
*BI
= dynamic_cast<BitsInit
*>(R
->getValue()))
1410 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1411 "' does not have a BitsInit initializer!";
1414 /// getValueAsListInit - This method looks up the specified field and returns
1415 /// its value as a ListInit, throwing an exception if the field does not exist
1416 /// or if the value is not the right type.
1418 ListInit
*Record::getValueAsListInit(StringRef FieldName
) const {
1419 const RecordVal
*R
= getValue(FieldName
);
1420 if (R
== 0 || R
->getValue() == 0)
1421 throw "Record `" + getName() + "' does not have a field named `" +
1422 FieldName
.str() + "'!\n";
1424 if (ListInit
*LI
= dynamic_cast<ListInit
*>(R
->getValue()))
1426 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1427 "' does not have a list initializer!";
1430 /// getValueAsListOfDefs - This method looks up the specified field and returns
1431 /// its value as a vector of records, throwing an exception if the field does
1432 /// not exist or if the value is not the right type.
1434 std::vector
<Record
*>
1435 Record::getValueAsListOfDefs(StringRef FieldName
) const {
1436 ListInit
*List
= getValueAsListInit(FieldName
);
1437 std::vector
<Record
*> Defs
;
1438 for (unsigned i
= 0; i
< List
->getSize(); i
++) {
1439 if (DefInit
*DI
= dynamic_cast<DefInit
*>(List
->getElement(i
))) {
1440 Defs
.push_back(DI
->getDef());
1442 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1443 "' list is not entirely DefInit!";
1449 /// getValueAsInt - This method looks up the specified field and returns its
1450 /// value as an int64_t, throwing an exception if the field does not exist or if
1451 /// the value is not the right type.
1453 int64_t Record::getValueAsInt(StringRef FieldName
) const {
1454 const RecordVal
*R
= getValue(FieldName
);
1455 if (R
== 0 || R
->getValue() == 0)
1456 throw "Record `" + getName() + "' does not have a field named `" +
1457 FieldName
.str() + "'!\n";
1459 if (IntInit
*II
= dynamic_cast<IntInit
*>(R
->getValue()))
1460 return II
->getValue();
1461 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1462 "' does not have an int initializer!";
1465 /// getValueAsListOfInts - This method looks up the specified field and returns
1466 /// its value as a vector of integers, throwing an exception if the field does
1467 /// not exist or if the value is not the right type.
1469 std::vector
<int64_t>
1470 Record::getValueAsListOfInts(StringRef FieldName
) const {
1471 ListInit
*List
= getValueAsListInit(FieldName
);
1472 std::vector
<int64_t> Ints
;
1473 for (unsigned i
= 0; i
< List
->getSize(); i
++) {
1474 if (IntInit
*II
= dynamic_cast<IntInit
*>(List
->getElement(i
))) {
1475 Ints
.push_back(II
->getValue());
1477 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1478 "' does not have a list of ints initializer!";
1484 /// getValueAsDef - This method looks up the specified field and returns its
1485 /// value as a Record, throwing an exception if the field does not exist or if
1486 /// the value is not the right type.
1488 Record
*Record::getValueAsDef(StringRef FieldName
) const {
1489 const RecordVal
*R
= getValue(FieldName
);
1490 if (R
== 0 || R
->getValue() == 0)
1491 throw "Record `" + getName() + "' does not have a field named `" +
1492 FieldName
.str() + "'!\n";
1494 if (DefInit
*DI
= dynamic_cast<DefInit
*>(R
->getValue()))
1495 return DI
->getDef();
1496 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1497 "' does not have a def initializer!";
1500 /// getValueAsBit - This method looks up the specified field and returns its
1501 /// value as a bit, throwing an exception if the field does not exist or if
1502 /// the value is not the right type.
1504 bool Record::getValueAsBit(StringRef FieldName
) const {
1505 const RecordVal
*R
= getValue(FieldName
);
1506 if (R
== 0 || R
->getValue() == 0)
1507 throw "Record `" + getName() + "' does not have a field named `" +
1508 FieldName
.str() + "'!\n";
1510 if (BitInit
*BI
= dynamic_cast<BitInit
*>(R
->getValue()))
1511 return BI
->getValue();
1512 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1513 "' does not have a bit initializer!";
1516 /// getValueAsDag - This method looks up the specified field and returns its
1517 /// value as an Dag, throwing an exception if the field does not exist or if
1518 /// the value is not the right type.
1520 DagInit
*Record::getValueAsDag(StringRef FieldName
) const {
1521 const RecordVal
*R
= getValue(FieldName
);
1522 if (R
== 0 || R
->getValue() == 0)
1523 throw "Record `" + getName() + "' does not have a field named `" +
1524 FieldName
.str() + "'!\n";
1526 if (DagInit
*DI
= dynamic_cast<DagInit
*>(R
->getValue()))
1528 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1529 "' does not have a dag initializer!";
1532 std::string
Record::getValueAsCode(StringRef FieldName
) const {
1533 const RecordVal
*R
= getValue(FieldName
);
1534 if (R
== 0 || R
->getValue() == 0)
1535 throw "Record `" + getName() + "' does not have a field named `" +
1536 FieldName
.str() + "'!\n";
1538 if (const CodeInit
*CI
= dynamic_cast<const CodeInit
*>(R
->getValue()))
1539 return CI
->getValue();
1540 throw "Record `" + getName() + "', field `" + FieldName
.str() +
1541 "' does not have a code initializer!";
1545 void MultiClass::dump() const {
1546 errs() << "Record:\n";
1549 errs() << "Defs:\n";
1550 for (RecordVector::const_iterator r
= DefPrototypes
.begin(),
1551 rend
= DefPrototypes
.end();
1559 void RecordKeeper::dump() const { errs() << *this; }
1561 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const RecordKeeper
&RK
) {
1562 OS
<< "------------- Classes -----------------\n";
1563 const std::map
<std::string
, Record
*> &Classes
= RK
.getClasses();
1564 for (std::map
<std::string
, Record
*>::const_iterator I
= Classes
.begin(),
1565 E
= Classes
.end(); I
!= E
; ++I
)
1566 OS
<< "class " << *I
->second
;
1568 OS
<< "------------- Defs -----------------\n";
1569 const std::map
<std::string
, Record
*> &Defs
= RK
.getDefs();
1570 for (std::map
<std::string
, Record
*>::const_iterator I
= Defs
.begin(),
1571 E
= Defs
.end(); I
!= E
; ++I
)
1572 OS
<< "def " << *I
->second
;
1577 /// getAllDerivedDefinitions - This method returns all concrete definitions
1578 /// that derive from the specified class name. If a class with the specified
1579 /// name does not exist, an error is printed and true is returned.
1580 std::vector
<Record
*>
1581 RecordKeeper::getAllDerivedDefinitions(const std::string
&ClassName
) const {
1582 Record
*Class
= Records
.getClass(ClassName
);
1584 throw "ERROR: Couldn't find the `" + ClassName
+ "' class!\n";
1586 std::vector
<Record
*> Defs
;
1587 for (std::map
<std::string
, Record
*>::const_iterator I
= getDefs().begin(),
1588 E
= getDefs().end(); I
!= E
; ++I
)
1589 if (I
->second
->isSubClassOf(Class
))
1590 Defs
.push_back(I
->second
);