Do more to modernize MergeFunctions. Refactor in response to Chris' code review.
[llvm.git] / utils / TableGen / Record.cpp
blobd2cf379907f64465338f58998f9b9d3ffd787ec2
1 //===- Record.cpp - Record implementation ---------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implement the tablegen record classes.
12 //===----------------------------------------------------------------------===//
14 #include "Record.h"
15 #include "llvm/System/DataTypes.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/ADT/StringExtras.h"
19 using namespace llvm;
21 //===----------------------------------------------------------------------===//
22 // Type implementations
23 //===----------------------------------------------------------------------===//
25 void RecTy::dump() const { print(errs()); }
27 Init *BitRecTy::convertValue(BitsInit *BI) {
28 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
29 return BI->getBit(0);
32 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
33 return RHS->getNumBits() == 1;
36 Init *BitRecTy::convertValue(IntInit *II) {
37 int64_t Val = II->getValue();
38 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
40 return new BitInit(Val != 0);
43 Init *BitRecTy::convertValue(TypedInit *VI) {
44 if (dynamic_cast<BitRecTy*>(VI->getType()))
45 return VI; // Accept variable if it is already of bit type!
46 return 0;
49 std::string BitsRecTy::getAsString() const {
50 return "bits<" + utostr(Size) + ">";
53 Init *BitsRecTy::convertValue(UnsetInit *UI) {
54 BitsInit *Ret = new BitsInit(Size);
56 for (unsigned i = 0; i != Size; ++i)
57 Ret->setBit(i, new UnsetInit());
58 return Ret;
61 Init *BitsRecTy::convertValue(BitInit *UI) {
62 if (Size != 1) return 0; // Can only convert single bit...
63 BitsInit *Ret = new BitsInit(1);
64 Ret->setBit(0, UI);
65 return Ret;
68 // 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...
74 if (Value >= 0) {
75 if (Value & ~((1LL << Size)-1))
76 return 0;
77 } else {
78 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
79 return 0;
82 BitsInit *Ret = new BitsInit(Size);
83 for (unsigned i = 0; i != Size; ++i)
84 Ret->setBit(i, new BitInit(Value & (1LL << i)));
86 return Ret;
89 Init *BitsRecTy::convertValue(BitsInit *BI) {
90 // If the number of bits is right, return it. Otherwise we need to expand or
91 // truncate...
92 if (BI->getNumBits() == Size) return BI;
93 return 0;
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));
102 return Ret;
104 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
105 BitsInit *Ret = new BitsInit(1);
106 Ret->setBit(0, VI);
107 return Ret;
110 return 0;
113 Init *IntRecTy::convertValue(BitInit *BI) {
114 return new IntInit(BI->getValue());
117 Init *IntRecTy::convertValue(BitsInit *BI) {
118 int64_t Result = 0;
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;
122 } else {
123 return 0;
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!
131 return 0;
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);
140 return BO;
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);
153 return BO;
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);
162 return BO;
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!
173 return 0;
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);
188 else
189 return 0;
191 ListRecTy *LType = dynamic_cast<ListRecTy*>(LI->getType());
192 if (LType == 0) {
193 return 0;
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()))
203 return TI;
204 return 0;
207 Init *CodeRecTy::convertValue(TypedInit *TI) {
208 if (TI->getType()->typeIsConvertibleTo(this))
209 return TI;
210 return 0;
213 Init *DagRecTy::convertValue(TypedInit *TI) {
214 if (TI->getType()->typeIsConvertibleTo(this))
215 return TI;
216 return 0;
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);
225 return BO;
227 return 0;
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);
237 return BO;
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);
246 return BO;
249 return 0;
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))
259 return 0;
260 return DI;
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())
268 return TI;
269 return 0;
272 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
273 if (Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec))
274 return true;
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]))
279 return true;
281 return false;
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);
293 if (RecTy1) {
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();
299 i != iend;
300 ++i) {
301 RecordRecTy *SuperRecTy1 = new RecordRecTy(*i);
302 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
303 if (NewType1 != 0) {
304 if (NewType1 != SuperRecTy1) {
305 delete SuperRecTy1;
307 return NewType1;
311 RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2);
312 if (RecTy2) {
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();
318 i != iend;
319 ++i) {
320 RecordRecTy *SuperRecTy2 = new RecordRecTy(*i);
321 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
322 if (NewType2 != 0) {
323 if (NewType2 != SuperRecTy2) {
324 delete SuperRecTy2;
326 return NewType2;
330 return 0;
332 return T2;
334 return T1;
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()) {
348 delete BI;
349 return 0;
351 BI->setBit(i, getBit(Bits[i]));
353 return BI;
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();
362 else
363 Result += "*";
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) {
376 Init *B;
377 Init *CurBit = getBit(i);
379 do {
380 B = CurBit;
381 CurBit = CurBit->resolveReferences(R, RV);
382 Changed |= B != CurBit;
383 } while (B != CurBit);
384 New->setBit(i, CurBit);
387 if (Changed)
388 return New;
389 delete New;
390 return this;
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) {
401 if (Bits[i] >= 64) {
402 delete BI;
403 return 0;
405 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
407 return BI;
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())
414 return 0;
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!";
424 return DI->getDef();
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) {
433 Init *E;
434 Init *CurElt = getElement(i);
436 do {
437 E = CurElt;
438 CurElt = CurElt->resolveReferences(R, RV);
439 Changed |= E != CurElt;
440 } while (E != CurElt);
441 Resolved.push_back(E);
444 if (Changed)
445 return new ListInit(Resolved, getType());
446 return this;
449 Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
450 unsigned Elt) {
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))
458 return E;
459 return 0;
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();
468 return Result + "]";
471 Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
472 unsigned Bit) {
473 Init *Folded = Fold(&R, 0);
475 if (Folded != this) {
476 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
477 if (Typed) {
478 return Typed->resolveBitReference(R, IRV, Bit);
482 return 0;
485 Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
486 unsigned Elt) {
487 Init *Folded = Fold(&R, 0);
489 if (Folded != this) {
490 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
491 if (Typed) {
492 return Typed->resolveListElementReference(R, IRV, Elt);
496 return 0;
499 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
500 switch (getOpcode()) {
501 default: assert(0 && "Unknown unop");
502 case CAST: {
503 if (getType()->getAsString() == "string") {
504 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
505 if (LHSs) {
506 return LHSs;
509 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
510 if (LHSd) {
511 return new StringInit(LHSd->getDef()->getName());
513 } else {
514 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
515 if (LHSs) {
516 std::string Name = LHSs->getValue();
518 // From TGParser::ParseIDValue
519 if (CurRec) {
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());
540 if (CurMultiClass) {
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");
559 return 0;
562 break;
564 case CAR: {
565 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
566 if (LHSl) {
567 if (LHSl->getSize() == 0) {
568 assert(0 && "Empty list in car");
569 return 0;
571 return LHSl->getElement(0);
573 break;
575 case CDR: {
576 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
577 if (LHSl) {
578 if (LHSl->getSize() == 0) {
579 assert(0 && "Empty list in cdr");
580 return 0;
582 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(),
583 LHSl->getType());
584 return Result;
586 break;
588 case LNULL: {
589 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
590 if (LHSl) {
591 if (LHSl->getSize() == 0) {
592 return new IntInit(1);
593 } else {
594 return new IntInit(0);
597 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
598 if (LHSs) {
599 if (LHSs->getValue().empty()) {
600 return new IntInit(1);
601 } else {
602 return new IntInit(0);
606 break;
609 return this;
612 Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
613 Init *lhs = LHS->resolveReferences(R, RV);
615 if (LHS != lhs)
616 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
617 return Fold(&R, 0);
620 std::string UnOpInit::getAsString() const {
621 std::string Result;
622 switch (Opc) {
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");
634 case CAST: {
635 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
636 if (RecordType) {
637 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
638 if (Field) {
639 return Field->getType();
642 break;
645 return 0;
648 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
649 switch (getOpcode()) {
650 default: assert(0 && "Unknown binop");
651 case CONCAT: {
652 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
653 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
654 if (LHSs && RHSs) {
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);
671 break;
673 case STRCONCAT: {
674 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
675 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
676 if (LHSs && RHSs)
677 return new StringInit(LHSs->getValue() + RHSs->getValue());
678 break;
680 case NAMECONCAT: {
681 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
682 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
683 if (LHSs && RHSs) {
684 std::string Name(LHSs->getValue() + RHSs->getValue());
686 // From TGParser::ParseIDValue
687 if (CurRec) {
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());
708 if (CurMultiClass) {
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");
727 return 0;
729 break;
731 case EQ: {
732 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
733 // to string objects.
734 IntInit* L =
735 dynamic_cast<IntInit*>(LHS->convertInitializerTo(new IntRecTy()));
736 IntInit* R =
737 dynamic_cast<IntInit*>(RHS->convertInitializerTo(new IntRecTy()));
739 if (L && R)
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
746 if (LHSs && RHSs)
747 return new IntInit(LHSs->getValue() == RHSs->getValue());
749 break;
751 case SHL:
752 case SRA:
753 case SRL: {
754 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
755 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
756 if (LHSi && RHSi) {
757 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
758 int64_t Result;
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);
767 break;
770 return this;
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);
779 return Fold(&R, 0);
782 std::string BinOpInit::getAsString() const {
783 std::string Result;
784 switch (Opc) {
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;
791 case NAMECONCAT:
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);
811 if (Result != 0) {
812 return Result;
813 } else {
814 return 0;
818 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
819 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
821 if (RHSoo) {
822 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
823 Type, CurRec, CurMultiClass);
824 if (Result != 0) {
825 NewOperands.push_back(Result);
826 } else {
827 NewOperands.push_back(Arg);
829 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
830 NewOperands.push_back(Arg);
831 } else {
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) {
840 delete NewOp;
841 return NewVal;
843 return 0;
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);
856 if (!RHSo) {
857 errs() << "!foreach requires an operator\n";
858 assert(0 && "No operator for !foreach");
861 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
863 if (!LHSt) {
864 errs() << "!foreach requires typed variable\n";
865 assert(0 && "No typed variable for !foreach");
868 if ((MHSd && DagType) || (MHSl && ListType)) {
869 if (MHSd) {
870 Init *Val = MHSd->getOperator();
871 Init *Result = EvaluateOperation(RHSo, LHS, Val,
872 Type, CurRec, CurMultiClass);
873 if (Result != 0) {
874 Val = Result;
877 std::vector<std::pair<Init *, std::string> > args;
878 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
879 Init *Arg;
880 std::string ArgName;
881 Arg = MHSd->getArg(i);
882 ArgName = MHSd->getArgName(i);
884 // Process args
885 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
886 CurRec, CurMultiClass);
887 if (Result != 0) {
888 Arg = Result;
891 // TODO: Process arg names
892 args.push_back(std::make_pair(Arg, ArgName));
895 return new DagInit(Val, "", args);
897 if (MHSl) {
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();
903 li != liend;
904 ++li) {
905 Init *Item = *li;
906 NewOperands.clear();
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);
911 } else {
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) {
920 *li = NewItem;
921 delete NewOp;
924 return new ListInit(NewList, MHSl->getType());
927 return 0;
930 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
931 switch (getOpcode()) {
932 default: assert(0 && "Unknown binop");
933 case SUBST: {
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)) {
949 if (RHSd) {
950 Record *Val = RHSd->getDef();
951 if (LHSd->getAsString() == RHSd->getAsString()) {
952 Val = MHSd->getDef();
954 return new DefInit(Val);
956 if (RHSv) {
957 std::string Val = RHSv->getName();
958 if (LHSv->getAsString() == RHSv->getAsString()) {
959 Val = MHSv->getName();
961 return new VarInit(Val, getType());
963 if (RHSs) {
964 std::string Val = RHSs->getValue();
966 std::string::size_type found;
967 std::string::size_type idx = 0;
968 do {
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);
979 break;
982 case FOREACH: {
983 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
984 CurRec, CurMultiClass);
985 if (Result != 0) {
986 return Result;
988 break;
991 case IF: {
992 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
993 if (Init *I = LHS->convertInitializerTo(new IntRecTy()))
994 LHSi = dynamic_cast<IntInit*>(I);
995 if (LHSi) {
996 if (LHSi->getValue()) {
997 return MHS;
998 } else {
999 return RHS;
1002 break;
1006 return this;
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);
1016 if (Value != 0) {
1017 // Short-circuit
1018 if (Value->getValue()) {
1019 Init *mhs = MHS->resolveReferences(R, RV);
1020 return (new TernOpInit(getOpcode(), lhs, mhs,
1021 RHS, getType()))->Fold(&R, 0);
1022 } else {
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);
1035 return Fold(&R, 0);
1038 std::string TernOpInit::getAsString() const {
1039 std::string Result;
1040 switch (Opc) {
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) {
1057 delete BI;
1058 return 0;
1060 BI->setBit(i, new VarBitInit(this, Bits[i]));
1062 return BI;
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,
1081 unsigned Bit) {
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))
1097 return B;
1098 return 0;
1101 Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1102 unsigned Elt) {
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());
1109 if (!LI) {
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))
1122 return E;
1123 return 0;
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();
1131 return 0;
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())))
1139 return 0;
1140 Init *TheInit = Val->getValue();
1141 assert(TheInit != this && "Infinite loop detected!");
1142 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
1143 return I;
1144 else
1145 return 0;
1147 return 0;
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();
1159 return this;
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()))
1168 return I;
1169 return this;
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,
1178 getElementNum()))
1179 return I;
1180 return this;
1183 Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1184 unsigned Bit) {
1185 // FIXME: This should be implemented, to support references like:
1186 // bit B = AA[0]{1};
1187 return 0;
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];
1194 return 0;
1197 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1198 if (const RecordVal *RV = Def->getValue(FieldName))
1199 return RV->getType();
1200 return 0;
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,
1214 unsigned Bit) {
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.
1223 return 0;
1226 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1227 unsigned Elt) {
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))
1237 return E;
1239 return 0;
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);
1246 if (BitsVal) {
1247 Init *BVR = BitsVal->resolveReferences(R, RV);
1248 return BVR->isComplete() ? BVR : this;
1251 if (NewRec != Rec) {
1252 return new FieldInit(NewRec, FieldName);
1254 return this;
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);
1267 return this;
1271 std::string DagInit::getAsString() const {
1272 std::string Result = "(" + Val->getAsString();
1273 if (!ValName.empty())
1274 Result += ":" + ValName;
1275 if (Args.size()) {
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();
1303 if (getValue())
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());
1314 this->Name = Name;
1315 Records.addDef(this);
1316 } else {
1317 Records.removeClass(getName());
1318 this->Name = Name;
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
1325 /// references.
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) {
1336 OS << R.getName();
1338 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1339 if (!TArgs.empty()) {
1340 OS << "<";
1341 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1342 if (i) OS << ", ";
1343 const RecordVal *RV = R.getValue(TArgs[i]);
1344 assert(RV && "Template argument record not found??");
1345 RV->print(OS, false);
1347 OS << ">";
1350 OS << " {";
1351 const std::vector<Record*> &SC = R.getSuperClasses();
1352 if (!SC.empty()) {
1353 OS << "\t//";
1354 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1355 OS << " " << SC[i]->getName();
1357 OS << "\n";
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()))
1362 OS << Vals[i];
1363 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1364 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1365 OS << Vals[i];
1367 return OS << "}\n";
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()))
1409 return BI;
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()))
1425 return LI;
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());
1441 } else {
1442 throw "Record `" + getName() + "', field `" + FieldName.str() +
1443 "' list is not entirely DefInit!";
1446 return Defs;
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());
1476 } else {
1477 throw "Record `" + getName() + "', field `" + FieldName.str() +
1478 "' does not have a list of ints initializer!";
1481 return Ints;
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()))
1527 return DI;
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";
1547 Rec.dump();
1549 errs() << "Defs:\n";
1550 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1551 rend = DefPrototypes.end();
1552 r != rend;
1553 ++r) {
1554 (*r)->dump();
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;
1573 return OS;
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);
1583 if (!Class)
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);
1592 return Defs;