Silence -Wunused-variable in release builds.
[llvm/stm8.git] / utils / TableGen / Record.cpp
blob730eca1b3ca5d94e087e3b6c8e4271587ef65788
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 "Error.h"
16 #include "llvm/Support/DataTypes.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/ADT/StringExtras.h"
20 using namespace llvm;
22 //===----------------------------------------------------------------------===//
23 // Type implementations
24 //===----------------------------------------------------------------------===//
26 void RecTy::dump() const { print(errs()); }
28 Init *BitRecTy::convertValue(BitsInit *BI) {
29 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
30 return BI->getBit(0);
33 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
34 return RHS->getNumBits() == 1;
37 Init *BitRecTy::convertValue(IntInit *II) {
38 int64_t Val = II->getValue();
39 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
41 return new BitInit(Val != 0);
44 Init *BitRecTy::convertValue(TypedInit *VI) {
45 if (dynamic_cast<BitRecTy*>(VI->getType()))
46 return VI; // Accept variable if it is already of bit type!
47 return 0;
50 std::string BitsRecTy::getAsString() const {
51 return "bits<" + utostr(Size) + ">";
54 Init *BitsRecTy::convertValue(UnsetInit *UI) {
55 BitsInit *Ret = new BitsInit(Size);
57 for (unsigned i = 0; i != Size; ++i)
58 Ret->setBit(i, new UnsetInit());
59 return Ret;
62 Init *BitsRecTy::convertValue(BitInit *UI) {
63 if (Size != 1) return 0; // Can only convert single bit.
64 BitsInit *Ret = new BitsInit(1);
65 Ret->setBit(0, UI);
66 return Ret;
69 /// canFitInBitfield - Return true if the number of bits is large enough to hold
70 /// the integer value.
71 static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
72 // For example, with NumBits == 4, we permit Values from [-7 .. 15].
73 return (NumBits >= sizeof(Value) * 8) ||
74 (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
77 /// convertValue from Int initializer to bits type: Split the integer up into the
78 /// appropriate bits.
79 ///
80 Init *BitsRecTy::convertValue(IntInit *II) {
81 int64_t Value = II->getValue();
82 // Make sure this bitfield is large enough to hold the integer value.
83 if (!canFitInBitfield(Value, Size))
84 return 0;
86 BitsInit *Ret = new BitsInit(Size);
87 for (unsigned i = 0; i != Size; ++i)
88 Ret->setBit(i, new BitInit(Value & (1LL << i)));
90 return Ret;
93 Init *BitsRecTy::convertValue(BitsInit *BI) {
94 // If the number of bits is right, return it. Otherwise we need to expand or
95 // truncate.
96 if (BI->getNumBits() == Size) return BI;
97 return 0;
100 Init *BitsRecTy::convertValue(TypedInit *VI) {
101 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
102 if (BRT->Size == Size) {
103 BitsInit *Ret = new BitsInit(Size);
104 for (unsigned i = 0; i != Size; ++i)
105 Ret->setBit(i, new VarBitInit(VI, i));
106 return Ret;
109 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
110 BitsInit *Ret = new BitsInit(1);
111 Ret->setBit(0, VI);
112 return Ret;
115 if (TernOpInit *Tern = dynamic_cast<TernOpInit*>(VI)) {
116 if (Tern->getOpcode() == TernOpInit::IF) {
117 Init *LHS = Tern->getLHS();
118 Init *MHS = Tern->getMHS();
119 Init *RHS = Tern->getRHS();
121 IntInit *MHSi = dynamic_cast<IntInit*>(MHS);
122 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
124 if (MHSi && RHSi) {
125 int64_t MHSVal = MHSi->getValue();
126 int64_t RHSVal = RHSi->getValue();
128 if (canFitInBitfield(MHSVal, Size) && canFitInBitfield(RHSVal, Size)) {
129 BitsInit *Ret = new BitsInit(Size);
131 for (unsigned i = 0; i != Size; ++i)
132 Ret->setBit(i, new TernOpInit(TernOpInit::IF, LHS,
133 new IntInit((MHSVal & (1LL << i)) ? 1 : 0),
134 new IntInit((RHSVal & (1LL << i)) ? 1 : 0),
135 VI->getType()));
137 return Ret;
139 } else {
140 BitsInit *MHSbs = dynamic_cast<BitsInit*>(MHS);
141 BitsInit *RHSbs = dynamic_cast<BitsInit*>(RHS);
143 if (MHSbs && RHSbs) {
144 BitsInit *Ret = new BitsInit(Size);
146 for (unsigned i = 0; i != Size; ++i)
147 Ret->setBit(i, new TernOpInit(TernOpInit::IF, LHS,
148 MHSbs->getBit(i),
149 RHSbs->getBit(i),
150 VI->getType()));
152 return Ret;
158 return 0;
161 Init *IntRecTy::convertValue(BitInit *BI) {
162 return new IntInit(BI->getValue());
165 Init *IntRecTy::convertValue(BitsInit *BI) {
166 int64_t Result = 0;
167 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
168 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
169 Result |= Bit->getValue() << i;
170 } else {
171 return 0;
173 return new IntInit(Result);
176 Init *IntRecTy::convertValue(TypedInit *TI) {
177 if (TI->getType()->typeIsConvertibleTo(this))
178 return TI; // Accept variable if already of the right type!
179 return 0;
182 Init *StringRecTy::convertValue(UnOpInit *BO) {
183 if (BO->getOpcode() == UnOpInit::CAST) {
184 Init *L = BO->getOperand()->convertInitializerTo(this);
185 if (L == 0) return 0;
186 if (L != BO->getOperand())
187 return new UnOpInit(UnOpInit::CAST, L, new StringRecTy);
188 return BO;
191 return convertValue((TypedInit*)BO);
194 Init *StringRecTy::convertValue(BinOpInit *BO) {
195 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
196 Init *L = BO->getLHS()->convertInitializerTo(this);
197 Init *R = BO->getRHS()->convertInitializerTo(this);
198 if (L == 0 || R == 0) return 0;
199 if (L != BO->getLHS() || R != BO->getRHS())
200 return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy);
201 return BO;
204 return convertValue((TypedInit*)BO);
208 Init *StringRecTy::convertValue(TypedInit *TI) {
209 if (dynamic_cast<StringRecTy*>(TI->getType()))
210 return TI; // Accept variable if already of the right type!
211 return 0;
214 std::string ListRecTy::getAsString() const {
215 return "list<" + Ty->getAsString() + ">";
218 Init *ListRecTy::convertValue(ListInit *LI) {
219 std::vector<Init*> Elements;
221 // Verify that all of the elements of the list are subclasses of the
222 // appropriate class!
223 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
224 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
225 Elements.push_back(CI);
226 else
227 return 0;
229 ListRecTy *LType = dynamic_cast<ListRecTy*>(LI->getType());
230 if (LType == 0) {
231 return 0;
234 return new ListInit(Elements, new ListRecTy(Ty));
237 Init *ListRecTy::convertValue(TypedInit *TI) {
238 // Ensure that TI is compatible with our class.
239 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
240 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
241 return TI;
242 return 0;
245 Init *CodeRecTy::convertValue(TypedInit *TI) {
246 if (TI->getType()->typeIsConvertibleTo(this))
247 return TI;
248 return 0;
251 Init *DagRecTy::convertValue(TypedInit *TI) {
252 if (TI->getType()->typeIsConvertibleTo(this))
253 return TI;
254 return 0;
257 Init *DagRecTy::convertValue(UnOpInit *BO) {
258 if (BO->getOpcode() == UnOpInit::CAST) {
259 Init *L = BO->getOperand()->convertInitializerTo(this);
260 if (L == 0) return 0;
261 if (L != BO->getOperand())
262 return new UnOpInit(UnOpInit::CAST, L, new DagRecTy);
263 return BO;
265 return 0;
268 Init *DagRecTy::convertValue(BinOpInit *BO) {
269 if (BO->getOpcode() == BinOpInit::CONCAT) {
270 Init *L = BO->getLHS()->convertInitializerTo(this);
271 Init *R = BO->getRHS()->convertInitializerTo(this);
272 if (L == 0 || R == 0) return 0;
273 if (L != BO->getLHS() || R != BO->getRHS())
274 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
275 return BO;
277 return 0;
280 std::string RecordRecTy::getAsString() const {
281 return Rec->getName();
284 Init *RecordRecTy::convertValue(DefInit *DI) {
285 // Ensure that DI is a subclass of Rec.
286 if (!DI->getDef()->isSubClassOf(Rec))
287 return 0;
288 return DI;
291 Init *RecordRecTy::convertValue(TypedInit *TI) {
292 // Ensure that TI is compatible with Rec.
293 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
294 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
295 RRT->getRecord() == getRecord())
296 return TI;
297 return 0;
300 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
301 if (Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec))
302 return true;
304 const std::vector<Record*> &SC = Rec->getSuperClasses();
305 for (unsigned i = 0, e = SC.size(); i != e; ++i)
306 if (RHS->getRecord()->isSubClassOf(SC[i]))
307 return true;
309 return false;
313 /// resolveTypes - Find a common type that T1 and T2 convert to.
314 /// Return 0 if no such type exists.
316 RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
317 if (!T1->typeIsConvertibleTo(T2)) {
318 if (!T2->typeIsConvertibleTo(T1)) {
319 // If one is a Record type, check superclasses
320 RecordRecTy *RecTy1 = dynamic_cast<RecordRecTy*>(T1);
321 if (RecTy1) {
322 // See if T2 inherits from a type T1 also inherits from
323 const std::vector<Record *> &T1SuperClasses =
324 RecTy1->getRecord()->getSuperClasses();
325 for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(),
326 iend = T1SuperClasses.end();
327 i != iend;
328 ++i) {
329 RecordRecTy *SuperRecTy1 = new RecordRecTy(*i);
330 RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
331 if (NewType1 != 0) {
332 if (NewType1 != SuperRecTy1) {
333 delete SuperRecTy1;
335 return NewType1;
339 RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2);
340 if (RecTy2) {
341 // See if T1 inherits from a type T2 also inherits from
342 const std::vector<Record *> &T2SuperClasses =
343 RecTy2->getRecord()->getSuperClasses();
344 for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(),
345 iend = T2SuperClasses.end();
346 i != iend;
347 ++i) {
348 RecordRecTy *SuperRecTy2 = new RecordRecTy(*i);
349 RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
350 if (NewType2 != 0) {
351 if (NewType2 != SuperRecTy2) {
352 delete SuperRecTy2;
354 return NewType2;
358 return 0;
360 return T2;
362 return T1;
366 //===----------------------------------------------------------------------===//
367 // Initializer implementations
368 //===----------------------------------------------------------------------===//
370 void Init::dump() const { return print(errs()); }
372 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
373 BitsInit *BI = new BitsInit(Bits.size());
374 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
375 if (Bits[i] >= getNumBits()) {
376 delete BI;
377 return 0;
379 BI->setBit(i, getBit(Bits[i]));
381 return BI;
384 std::string BitsInit::getAsString() const {
385 std::string Result = "{ ";
386 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
387 if (i) Result += ", ";
388 if (Init *Bit = getBit(e-i-1))
389 Result += Bit->getAsString();
390 else
391 Result += "*";
393 return Result + " }";
396 // resolveReferences - If there are any field references that refer to fields
397 // that have been filled in, we can propagate the values now.
399 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
400 bool Changed = false;
401 BitsInit *New = new BitsInit(getNumBits());
403 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
404 Init *B;
405 Init *CurBit = getBit(i);
407 do {
408 B = CurBit;
409 CurBit = CurBit->resolveReferences(R, RV);
410 Changed |= B != CurBit;
411 } while (B != CurBit);
412 New->setBit(i, CurBit);
415 if (Changed)
416 return New;
417 delete New;
418 return this;
421 std::string IntInit::getAsString() const {
422 return itostr(Value);
425 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
426 BitsInit *BI = new BitsInit(Bits.size());
428 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
429 if (Bits[i] >= 64) {
430 delete BI;
431 return 0;
433 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
435 return BI;
438 Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
439 std::vector<Init*> Vals;
440 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
441 if (Elements[i] >= getSize())
442 return 0;
443 Vals.push_back(getElement(Elements[i]));
445 return new ListInit(Vals, getType());
448 Record *ListInit::getElementAsRecord(unsigned i) const {
449 assert(i < Values.size() && "List element index out of range!");
450 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
451 if (DI == 0) throw "Expected record in list!";
452 return DI->getDef();
455 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
456 std::vector<Init*> Resolved;
457 Resolved.reserve(getSize());
458 bool Changed = false;
460 for (unsigned i = 0, e = getSize(); i != e; ++i) {
461 Init *E;
462 Init *CurElt = getElement(i);
464 do {
465 E = CurElt;
466 CurElt = CurElt->resolveReferences(R, RV);
467 Changed |= E != CurElt;
468 } while (E != CurElt);
469 Resolved.push_back(E);
472 if (Changed)
473 return new ListInit(Resolved, getType());
474 return this;
477 Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
478 unsigned Elt) {
479 if (Elt >= getSize())
480 return 0; // Out of range reference.
481 Init *E = getElement(Elt);
482 // If the element is set to some value, or if we are resolving a reference
483 // to a specific variable and that variable is explicitly unset, then
484 // replace the VarListElementInit with it.
485 if (IRV || !dynamic_cast<UnsetInit*>(E))
486 return E;
487 return 0;
490 std::string ListInit::getAsString() const {
491 std::string Result = "[";
492 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
493 if (i) Result += ", ";
494 Result += Values[i]->getAsString();
496 return Result + "]";
499 Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
500 unsigned Bit) {
501 Init *Folded = Fold(&R, 0);
503 if (Folded != this) {
504 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
505 if (Typed) {
506 return Typed->resolveBitReference(R, IRV, Bit);
510 return 0;
513 Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
514 unsigned Elt) {
515 Init *Folded = Fold(&R, 0);
517 if (Folded != this) {
518 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
519 if (Typed) {
520 return Typed->resolveListElementReference(R, IRV, Elt);
524 return 0;
527 Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
528 switch (getOpcode()) {
529 default: assert(0 && "Unknown unop");
530 case CAST: {
531 if (getType()->getAsString() == "string") {
532 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
533 if (LHSs) {
534 return LHSs;
537 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
538 if (LHSd) {
539 return new StringInit(LHSd->getDef()->getName());
541 } else {
542 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
543 if (LHSs) {
544 std::string Name = LHSs->getValue();
546 // From TGParser::ParseIDValue
547 if (CurRec) {
548 if (const RecordVal *RV = CurRec->getValue(Name)) {
549 if (RV->getType() != getType())
550 throw "type mismatch in cast";
551 return new VarInit(Name, RV->getType());
554 std::string TemplateArgName = CurRec->getName()+":"+Name;
555 if (CurRec->isTemplateArg(TemplateArgName)) {
556 const RecordVal *RV = CurRec->getValue(TemplateArgName);
557 assert(RV && "Template arg doesn't exist??");
559 if (RV->getType() != getType())
560 throw "type mismatch in cast";
562 return new VarInit(TemplateArgName, RV->getType());
566 if (CurMultiClass) {
567 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
568 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
569 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
570 assert(RV && "Template arg doesn't exist??");
572 if (RV->getType() != getType())
573 throw "type mismatch in cast";
575 return new VarInit(MCName, RV->getType());
579 if (Record *D = (CurRec->getRecords()).getDef(Name))
580 return new DefInit(D);
582 throw TGError(CurRec->getLoc(), "Undefined reference:'" + Name + "'\n");
585 break;
587 case HEAD: {
588 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
589 if (LHSl) {
590 if (LHSl->getSize() == 0) {
591 assert(0 && "Empty list in car");
592 return 0;
594 return LHSl->getElement(0);
596 break;
598 case TAIL: {
599 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
600 if (LHSl) {
601 if (LHSl->getSize() == 0) {
602 assert(0 && "Empty list in cdr");
603 return 0;
605 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(),
606 LHSl->getType());
607 return Result;
609 break;
611 case EMPTY: {
612 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
613 if (LHSl) {
614 if (LHSl->getSize() == 0) {
615 return new IntInit(1);
616 } else {
617 return new IntInit(0);
620 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
621 if (LHSs) {
622 if (LHSs->getValue().empty()) {
623 return new IntInit(1);
624 } else {
625 return new IntInit(0);
629 break;
632 return this;
635 Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
636 Init *lhs = LHS->resolveReferences(R, RV);
638 if (LHS != lhs)
639 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
640 return Fold(&R, 0);
643 std::string UnOpInit::getAsString() const {
644 std::string Result;
645 switch (Opc) {
646 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
647 case HEAD: Result = "!head"; break;
648 case TAIL: Result = "!tail"; break;
649 case EMPTY: Result = "!empty"; break;
651 return Result + "(" + LHS->getAsString() + ")";
654 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
655 switch (getOpcode()) {
656 default: assert(0 && "Unknown binop");
657 case CONCAT: {
658 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
659 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
660 if (LHSs && RHSs) {
661 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
662 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
663 if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef())
664 throw "Concated Dag operators do not match!";
665 std::vector<Init*> Args;
666 std::vector<std::string> ArgNames;
667 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
668 Args.push_back(LHSs->getArg(i));
669 ArgNames.push_back(LHSs->getArgName(i));
671 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
672 Args.push_back(RHSs->getArg(i));
673 ArgNames.push_back(RHSs->getArgName(i));
675 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
677 break;
679 case STRCONCAT: {
680 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
681 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
682 if (LHSs && RHSs)
683 return new StringInit(LHSs->getValue() + RHSs->getValue());
684 break;
686 case EQ: {
687 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
688 // to string objects.
689 IntInit* L =
690 dynamic_cast<IntInit*>(LHS->convertInitializerTo(new IntRecTy()));
691 IntInit* R =
692 dynamic_cast<IntInit*>(RHS->convertInitializerTo(new IntRecTy()));
694 if (L && R)
695 return new IntInit(L->getValue() == R->getValue());
697 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
698 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
700 // Make sure we've resolved
701 if (LHSs && RHSs)
702 return new IntInit(LHSs->getValue() == RHSs->getValue());
704 break;
706 case SHL:
707 case SRA:
708 case SRL: {
709 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
710 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
711 if (LHSi && RHSi) {
712 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
713 int64_t Result;
714 switch (getOpcode()) {
715 default: assert(0 && "Bad opcode!");
716 case SHL: Result = LHSv << RHSv; break;
717 case SRA: Result = LHSv >> RHSv; break;
718 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
720 return new IntInit(Result);
722 break;
725 return this;
728 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
729 Init *lhs = LHS->resolveReferences(R, RV);
730 Init *rhs = RHS->resolveReferences(R, RV);
732 if (LHS != lhs || RHS != rhs)
733 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
734 return Fold(&R, 0);
737 std::string BinOpInit::getAsString() const {
738 std::string Result;
739 switch (Opc) {
740 case CONCAT: Result = "!con"; break;
741 case SHL: Result = "!shl"; break;
742 case SRA: Result = "!sra"; break;
743 case SRL: Result = "!srl"; break;
744 case EQ: Result = "!eq"; break;
745 case STRCONCAT: Result = "!strconcat"; break;
747 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
750 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
751 Record *CurRec, MultiClass *CurMultiClass);
753 static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
754 RecTy *Type, Record *CurRec,
755 MultiClass *CurMultiClass) {
756 std::vector<Init *> NewOperands;
758 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
760 // If this is a dag, recurse
761 if (TArg && TArg->getType()->getAsString() == "dag") {
762 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
763 CurRec, CurMultiClass);
764 if (Result != 0) {
765 return Result;
766 } else {
767 return 0;
771 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
772 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
774 if (RHSoo) {
775 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
776 Type, CurRec, CurMultiClass);
777 if (Result != 0) {
778 NewOperands.push_back(Result);
779 } else {
780 NewOperands.push_back(Arg);
782 } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
783 NewOperands.push_back(Arg);
784 } else {
785 NewOperands.push_back(RHSo->getOperand(i));
789 // Now run the operator and use its result as the new leaf
790 OpInit *NewOp = RHSo->clone(NewOperands);
791 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
792 if (NewVal != NewOp) {
793 delete NewOp;
794 return NewVal;
796 return 0;
799 static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
800 Record *CurRec, MultiClass *CurMultiClass) {
801 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
802 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
804 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
805 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
807 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
809 if (!RHSo) {
810 throw TGError(CurRec->getLoc(), "!foreach requires an operator\n");
813 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
815 if (!LHSt) {
816 throw TGError(CurRec->getLoc(), "!foreach requires typed variable\n");
819 if ((MHSd && DagType) || (MHSl && ListType)) {
820 if (MHSd) {
821 Init *Val = MHSd->getOperator();
822 Init *Result = EvaluateOperation(RHSo, LHS, Val,
823 Type, CurRec, CurMultiClass);
824 if (Result != 0) {
825 Val = Result;
828 std::vector<std::pair<Init *, std::string> > args;
829 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
830 Init *Arg;
831 std::string ArgName;
832 Arg = MHSd->getArg(i);
833 ArgName = MHSd->getArgName(i);
835 // Process args
836 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
837 CurRec, CurMultiClass);
838 if (Result != 0) {
839 Arg = Result;
842 // TODO: Process arg names
843 args.push_back(std::make_pair(Arg, ArgName));
846 return new DagInit(Val, "", args);
848 if (MHSl) {
849 std::vector<Init *> NewOperands;
850 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
852 for (ListInit::iterator li = NewList.begin(),
853 liend = NewList.end();
854 li != liend;
855 ++li) {
856 Init *Item = *li;
857 NewOperands.clear();
858 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
859 // First, replace the foreach variable with the list item
860 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
861 NewOperands.push_back(Item);
862 } else {
863 NewOperands.push_back(RHSo->getOperand(i));
867 // Now run the operator and use its result as the new list item
868 OpInit *NewOp = RHSo->clone(NewOperands);
869 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
870 if (NewItem != NewOp) {
871 *li = NewItem;
872 delete NewOp;
875 return new ListInit(NewList, MHSl->getType());
878 return 0;
881 Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
882 switch (getOpcode()) {
883 default: assert(0 && "Unknown binop");
884 case SUBST: {
885 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
886 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
887 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
889 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
890 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
891 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
893 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
894 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
895 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
897 if ((LHSd && MHSd && RHSd)
898 || (LHSv && MHSv && RHSv)
899 || (LHSs && MHSs && RHSs)) {
900 if (RHSd) {
901 Record *Val = RHSd->getDef();
902 if (LHSd->getAsString() == RHSd->getAsString()) {
903 Val = MHSd->getDef();
905 return new DefInit(Val);
907 if (RHSv) {
908 std::string Val = RHSv->getName();
909 if (LHSv->getAsString() == RHSv->getAsString()) {
910 Val = MHSv->getName();
912 return new VarInit(Val, getType());
914 if (RHSs) {
915 std::string Val = RHSs->getValue();
917 std::string::size_type found;
918 std::string::size_type idx = 0;
919 do {
920 found = Val.find(LHSs->getValue(), idx);
921 if (found != std::string::npos) {
922 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
924 idx = found + MHSs->getValue().size();
925 } while (found != std::string::npos);
927 return new StringInit(Val);
930 break;
933 case FOREACH: {
934 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
935 CurRec, CurMultiClass);
936 if (Result != 0) {
937 return Result;
939 break;
942 case IF: {
943 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
944 if (Init *I = LHS->convertInitializerTo(new IntRecTy()))
945 LHSi = dynamic_cast<IntInit*>(I);
946 if (LHSi) {
947 if (LHSi->getValue()) {
948 return MHS;
949 } else {
950 return RHS;
953 break;
957 return this;
960 Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
961 Init *lhs = LHS->resolveReferences(R, RV);
963 if (Opc == IF && lhs != LHS) {
964 IntInit *Value = dynamic_cast<IntInit*>(lhs);
965 if (Init *I = lhs->convertInitializerTo(new IntRecTy()))
966 Value = dynamic_cast<IntInit*>(I);
967 if (Value != 0) {
968 // Short-circuit
969 if (Value->getValue()) {
970 Init *mhs = MHS->resolveReferences(R, RV);
971 return (new TernOpInit(getOpcode(), lhs, mhs,
972 RHS, getType()))->Fold(&R, 0);
973 } else {
974 Init *rhs = RHS->resolveReferences(R, RV);
975 return (new TernOpInit(getOpcode(), lhs, MHS,
976 rhs, getType()))->Fold(&R, 0);
981 Init *mhs = MHS->resolveReferences(R, RV);
982 Init *rhs = RHS->resolveReferences(R, RV);
984 if (LHS != lhs || MHS != mhs || RHS != rhs)
985 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
986 return Fold(&R, 0);
989 std::string TernOpInit::getAsString() const {
990 std::string Result;
991 switch (Opc) {
992 case SUBST: Result = "!subst"; break;
993 case FOREACH: Result = "!foreach"; break;
994 case IF: Result = "!if"; break;
996 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
997 + RHS->getAsString() + ")";
1000 RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
1001 RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
1002 if (RecordType) {
1003 RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
1004 if (Field) {
1005 return Field->getType();
1008 return 0;
1011 Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
1012 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
1013 if (T == 0) return 0; // Cannot subscript a non-bits variable.
1014 unsigned NumBits = T->getNumBits();
1016 BitsInit *BI = new BitsInit(Bits.size());
1017 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
1018 if (Bits[i] >= NumBits) {
1019 delete BI;
1020 return 0;
1022 BI->setBit(i, new VarBitInit(this, Bits[i]));
1024 return BI;
1027 Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
1028 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
1029 if (T == 0) return 0; // Cannot subscript a non-list variable.
1031 if (Elements.size() == 1)
1032 return new VarListElementInit(this, Elements[0]);
1034 std::vector<Init*> ListInits;
1035 ListInits.reserve(Elements.size());
1036 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1037 ListInits.push_back(new VarListElementInit(this, Elements[i]));
1038 return new ListInit(ListInits, T);
1042 Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1043 unsigned Bit) {
1044 if (R.isTemplateArg(getName())) return 0;
1045 if (IRV && IRV->getName() != getName()) return 0;
1047 RecordVal *RV = R.getValue(getName());
1048 assert(RV && "Reference to a non-existent variable?");
1049 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1050 BitsInit *BI = (BitsInit*)RV->getValue();
1052 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1053 Init *B = BI->getBit(Bit);
1055 // If the bit is set to some value, or if we are resolving a reference to a
1056 // specific variable and that variable is explicitly unset, then replace the
1057 // VarBitInit with it.
1058 if (IRV || !dynamic_cast<UnsetInit*>(B))
1059 return B;
1060 return 0;
1063 Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1064 unsigned Elt) {
1065 if (R.isTemplateArg(getName())) return 0;
1066 if (IRV && IRV->getName() != getName()) return 0;
1068 RecordVal *RV = R.getValue(getName());
1069 assert(RV && "Reference to a non-existent variable?");
1070 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
1071 if (!LI) {
1072 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
1073 assert(VI && "Invalid list element!");
1074 return new VarListElementInit(VI, Elt);
1077 if (Elt >= LI->getSize())
1078 return 0; // Out of range reference.
1079 Init *E = LI->getElement(Elt);
1080 // If the element is set to some value, or if we are resolving a reference
1081 // to a specific variable and that variable is explicitly unset, then
1082 // replace the VarListElementInit with it.
1083 if (IRV || !dynamic_cast<UnsetInit*>(E))
1084 return E;
1085 return 0;
1089 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1090 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1091 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1092 return RV->getType();
1093 return 0;
1096 Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
1097 const std::string &FieldName) const {
1098 if (dynamic_cast<RecordRecTy*>(getType()))
1099 if (const RecordVal *Val = R.getValue(VarName)) {
1100 if (RV != Val && (RV || dynamic_cast<UnsetInit*>(Val->getValue())))
1101 return 0;
1102 Init *TheInit = Val->getValue();
1103 assert(TheInit != this && "Infinite loop detected!");
1104 if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
1105 return I;
1106 else
1107 return 0;
1109 return 0;
1112 /// resolveReferences - This method is used by classes that refer to other
1113 /// variables which may not be defined at the time the expression is formed.
1114 /// If a value is set for the variable later, this method will be called on
1115 /// users of the value to allow the value to propagate out.
1117 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
1118 if (RecordVal *Val = R.getValue(VarName))
1119 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
1120 return Val->getValue();
1121 return this;
1124 std::string VarBitInit::getAsString() const {
1125 return TI->getAsString() + "{" + utostr(Bit) + "}";
1128 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1129 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
1130 return I;
1131 return this;
1134 std::string VarListElementInit::getAsString() const {
1135 return TI->getAsString() + "[" + utostr(Element) + "]";
1138 Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1139 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1140 getElementNum()))
1141 return I;
1142 return this;
1145 Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1146 unsigned Bit) {
1147 // FIXME: This should be implemented, to support references like:
1148 // bit B = AA[0]{1};
1149 return 0;
1152 Init *VarListElementInit::
1153 resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
1154 // FIXME: This should be implemented, to support references like:
1155 // int B = AA[0][1];
1156 return 0;
1159 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1160 if (const RecordVal *RV = Def->getValue(FieldName))
1161 return RV->getType();
1162 return 0;
1165 Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
1166 const std::string &FieldName) const {
1167 return Def->getValue(FieldName)->getValue();
1171 std::string DefInit::getAsString() const {
1172 return Def->getName();
1175 Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1176 unsigned Bit) {
1177 if (Init *BitsVal = Rec->getFieldInit(R, RV, FieldName))
1178 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1179 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1180 Init *B = BI->getBit(Bit);
1182 if (dynamic_cast<BitInit*>(B)) // If the bit is set.
1183 return B; // Replace the VarBitInit with it.
1185 return 0;
1188 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1189 unsigned Elt) {
1190 if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
1191 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1192 if (Elt >= LI->getSize()) return 0;
1193 Init *E = LI->getElement(Elt);
1195 // If the element is set to some value, or if we are resolving a
1196 // reference to a specific variable and that variable is explicitly
1197 // unset, then replace the VarListElementInit with it.
1198 if (RV || !dynamic_cast<UnsetInit*>(E))
1199 return E;
1201 return 0;
1204 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1205 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1207 Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName);
1208 if (BitsVal) {
1209 Init *BVR = BitsVal->resolveReferences(R, RV);
1210 return BVR->isComplete() ? BVR : this;
1213 if (NewRec != Rec) {
1214 return new FieldInit(NewRec, FieldName);
1216 return this;
1219 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1220 std::vector<Init*> NewArgs;
1221 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1222 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
1224 Init *Op = Val->resolveReferences(R, RV);
1226 if (Args != NewArgs || Op != Val)
1227 return new DagInit(Op, ValName, NewArgs, ArgNames);
1229 return this;
1233 std::string DagInit::getAsString() const {
1234 std::string Result = "(" + Val->getAsString();
1235 if (!ValName.empty())
1236 Result += ":" + ValName;
1237 if (Args.size()) {
1238 Result += " " + Args[0]->getAsString();
1239 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
1240 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
1241 Result += ", " + Args[i]->getAsString();
1242 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
1245 return Result + ")";
1249 //===----------------------------------------------------------------------===//
1250 // Other implementations
1251 //===----------------------------------------------------------------------===//
1253 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1254 : Name(N), Ty(T), Prefix(P) {
1255 Value = Ty->convertValue(new UnsetInit());
1256 assert(Value && "Cannot create unset value for current type!");
1259 void RecordVal::dump() const { errs() << *this; }
1261 void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
1262 if (getPrefix()) OS << "field ";
1263 OS << *getType() << " " << getName();
1265 if (getValue())
1266 OS << " = " << *getValue();
1268 if (PrintSem) OS << ";\n";
1271 unsigned Record::LastID = 0;
1273 void Record::setName(const std::string &Name) {
1274 if (TrackedRecords.getDef(getName()) == this) {
1275 TrackedRecords.removeDef(getName());
1276 this->Name = Name;
1277 TrackedRecords.addDef(this);
1278 } else {
1279 TrackedRecords.removeClass(getName());
1280 this->Name = Name;
1281 TrackedRecords.addClass(this);
1285 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1286 /// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1287 /// references.
1288 void Record::resolveReferencesTo(const RecordVal *RV) {
1289 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1290 if (Init *V = Values[i].getValue())
1291 Values[i].setValue(V->resolveReferences(*this, RV));
1295 void Record::dump() const { errs() << *this; }
1297 raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
1298 OS << R.getName();
1300 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1301 if (!TArgs.empty()) {
1302 OS << "<";
1303 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1304 if (i) OS << ", ";
1305 const RecordVal *RV = R.getValue(TArgs[i]);
1306 assert(RV && "Template argument record not found??");
1307 RV->print(OS, false);
1309 OS << ">";
1312 OS << " {";
1313 const std::vector<Record*> &SC = R.getSuperClasses();
1314 if (!SC.empty()) {
1315 OS << "\t//";
1316 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1317 OS << " " << SC[i]->getName();
1319 OS << "\n";
1321 const std::vector<RecordVal> &Vals = R.getValues();
1322 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1323 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1324 OS << Vals[i];
1325 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1326 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1327 OS << Vals[i];
1329 return OS << "}\n";
1332 /// getValueInit - Return the initializer for a value with the specified name,
1333 /// or throw an exception if the field does not exist.
1335 Init *Record::getValueInit(StringRef FieldName) const {
1336 const RecordVal *R = getValue(FieldName);
1337 if (R == 0 || R->getValue() == 0)
1338 throw "Record `" + getName() + "' does not have a field named `" +
1339 FieldName.str() + "'!\n";
1340 return R->getValue();
1344 /// getValueAsString - This method looks up the specified field and returns its
1345 /// value as a string, throwing an exception if the field does not exist or if
1346 /// the value is not a string.
1348 std::string Record::getValueAsString(StringRef FieldName) const {
1349 const RecordVal *R = getValue(FieldName);
1350 if (R == 0 || R->getValue() == 0)
1351 throw "Record `" + getName() + "' does not have a field named `" +
1352 FieldName.str() + "'!\n";
1354 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1355 return SI->getValue();
1356 throw "Record `" + getName() + "', field `" + FieldName.str() +
1357 "' does not have a string initializer!";
1360 /// getValueAsBitsInit - This method looks up the specified field and returns
1361 /// its value as a BitsInit, throwing an exception if the field does not exist
1362 /// or if the value is not the right type.
1364 BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
1365 const RecordVal *R = getValue(FieldName);
1366 if (R == 0 || R->getValue() == 0)
1367 throw "Record `" + getName() + "' does not have a field named `" +
1368 FieldName.str() + "'!\n";
1370 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1371 return BI;
1372 throw "Record `" + getName() + "', field `" + FieldName.str() +
1373 "' does not have a BitsInit initializer!";
1376 /// getValueAsListInit - This method looks up the specified field and returns
1377 /// its value as a ListInit, throwing an exception if the field does not exist
1378 /// or if the value is not the right type.
1380 ListInit *Record::getValueAsListInit(StringRef FieldName) const {
1381 const RecordVal *R = getValue(FieldName);
1382 if (R == 0 || R->getValue() == 0)
1383 throw "Record `" + getName() + "' does not have a field named `" +
1384 FieldName.str() + "'!\n";
1386 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1387 return LI;
1388 throw "Record `" + getName() + "', field `" + FieldName.str() +
1389 "' does not have a list initializer!";
1392 /// getValueAsListOfDefs - This method looks up the specified field and returns
1393 /// its value as a vector of records, throwing an exception if the field does
1394 /// not exist or if the value is not the right type.
1396 std::vector<Record*>
1397 Record::getValueAsListOfDefs(StringRef FieldName) const {
1398 ListInit *List = getValueAsListInit(FieldName);
1399 std::vector<Record*> Defs;
1400 for (unsigned i = 0; i < List->getSize(); i++) {
1401 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1402 Defs.push_back(DI->getDef());
1403 } else {
1404 throw "Record `" + getName() + "', field `" + FieldName.str() +
1405 "' list is not entirely DefInit!";
1408 return Defs;
1411 /// getValueAsInt - This method looks up the specified field and returns its
1412 /// value as an int64_t, throwing an exception if the field does not exist or if
1413 /// the value is not the right type.
1415 int64_t Record::getValueAsInt(StringRef FieldName) const {
1416 const RecordVal *R = getValue(FieldName);
1417 if (R == 0 || R->getValue() == 0)
1418 throw "Record `" + getName() + "' does not have a field named `" +
1419 FieldName.str() + "'!\n";
1421 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1422 return II->getValue();
1423 throw "Record `" + getName() + "', field `" + FieldName.str() +
1424 "' does not have an int initializer!";
1427 /// getValueAsListOfInts - This method looks up the specified field and returns
1428 /// its value as a vector of integers, throwing an exception if the field does
1429 /// not exist or if the value is not the right type.
1431 std::vector<int64_t>
1432 Record::getValueAsListOfInts(StringRef FieldName) const {
1433 ListInit *List = getValueAsListInit(FieldName);
1434 std::vector<int64_t> Ints;
1435 for (unsigned i = 0; i < List->getSize(); i++) {
1436 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1437 Ints.push_back(II->getValue());
1438 } else {
1439 throw "Record `" + getName() + "', field `" + FieldName.str() +
1440 "' does not have a list of ints initializer!";
1443 return Ints;
1446 /// getValueAsListOfStrings - This method looks up the specified field and
1447 /// returns its value as a vector of strings, throwing an exception if the
1448 /// field does not exist or if the value is not the right type.
1450 std::vector<std::string>
1451 Record::getValueAsListOfStrings(StringRef FieldName) const {
1452 ListInit *List = getValueAsListInit(FieldName);
1453 std::vector<std::string> Strings;
1454 for (unsigned i = 0; i < List->getSize(); i++) {
1455 if (StringInit *II = dynamic_cast<StringInit*>(List->getElement(i))) {
1456 Strings.push_back(II->getValue());
1457 } else {
1458 throw "Record `" + getName() + "', field `" + FieldName.str() +
1459 "' does not have a list of strings initializer!";
1462 return Strings;
1465 /// getValueAsDef - This method looks up the specified field and returns its
1466 /// value as a Record, throwing an exception if the field does not exist or if
1467 /// the value is not the right type.
1469 Record *Record::getValueAsDef(StringRef FieldName) const {
1470 const RecordVal *R = getValue(FieldName);
1471 if (R == 0 || R->getValue() == 0)
1472 throw "Record `" + getName() + "' does not have a field named `" +
1473 FieldName.str() + "'!\n";
1475 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1476 return DI->getDef();
1477 throw "Record `" + getName() + "', field `" + FieldName.str() +
1478 "' does not have a def initializer!";
1481 /// getValueAsBit - This method looks up the specified field and returns its
1482 /// value as a bit, throwing an exception if the field does not exist or if
1483 /// the value is not the right type.
1485 bool Record::getValueAsBit(StringRef FieldName) const {
1486 const RecordVal *R = getValue(FieldName);
1487 if (R == 0 || R->getValue() == 0)
1488 throw "Record `" + getName() + "' does not have a field named `" +
1489 FieldName.str() + "'!\n";
1491 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1492 return BI->getValue();
1493 throw "Record `" + getName() + "', field `" + FieldName.str() +
1494 "' does not have a bit initializer!";
1497 /// getValueAsDag - This method looks up the specified field and returns its
1498 /// value as an Dag, throwing an exception if the field does not exist or if
1499 /// the value is not the right type.
1501 DagInit *Record::getValueAsDag(StringRef FieldName) const {
1502 const RecordVal *R = getValue(FieldName);
1503 if (R == 0 || R->getValue() == 0)
1504 throw "Record `" + getName() + "' does not have a field named `" +
1505 FieldName.str() + "'!\n";
1507 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1508 return DI;
1509 throw "Record `" + getName() + "', field `" + FieldName.str() +
1510 "' does not have a dag initializer!";
1513 std::string Record::getValueAsCode(StringRef FieldName) const {
1514 const RecordVal *R = getValue(FieldName);
1515 if (R == 0 || R->getValue() == 0)
1516 throw "Record `" + getName() + "' does not have a field named `" +
1517 FieldName.str() + "'!\n";
1519 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1520 return CI->getValue();
1521 throw "Record `" + getName() + "', field `" + FieldName.str() +
1522 "' does not have a code initializer!";
1526 void MultiClass::dump() const {
1527 errs() << "Record:\n";
1528 Rec.dump();
1530 errs() << "Defs:\n";
1531 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1532 rend = DefPrototypes.end();
1533 r != rend;
1534 ++r) {
1535 (*r)->dump();
1540 void RecordKeeper::dump() const { errs() << *this; }
1542 raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
1543 OS << "------------- Classes -----------------\n";
1544 const std::map<std::string, Record*> &Classes = RK.getClasses();
1545 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
1546 E = Classes.end(); I != E; ++I)
1547 OS << "class " << *I->second;
1549 OS << "------------- Defs -----------------\n";
1550 const std::map<std::string, Record*> &Defs = RK.getDefs();
1551 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
1552 E = Defs.end(); I != E; ++I)
1553 OS << "def " << *I->second;
1554 return OS;
1558 /// getAllDerivedDefinitions - This method returns all concrete definitions
1559 /// that derive from the specified class name. If a class with the specified
1560 /// name does not exist, an error is printed and true is returned.
1561 std::vector<Record*>
1562 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1563 Record *Class = getClass(ClassName);
1564 if (!Class)
1565 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
1567 std::vector<Record*> Defs;
1568 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1569 E = getDefs().end(); I != E; ++I)
1570 if (I->second->isSubClassOf(Class))
1571 Defs.push_back(I->second);
1573 return Defs;