Proper name 16 bit libcalls
[llvm/msp430.git] / utils / TableGen / Record.cpp
blob2e64c838ab7104e0218d1681e5de5ae752162730
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/Support/DataTypes.h"
16 #include "llvm/Support/Streams.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include <ios>
20 using namespace llvm;
22 //===----------------------------------------------------------------------===//
23 // Type implementations
24 //===----------------------------------------------------------------------===//
26 void RecTy::dump() const { print(*cerr.stream()); }
28 Init *BitRecTy::convertValue(BitsInit *BI) {
29 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
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 // convertValue from Int initializer to bits type: Split the integer up into the
70 // appropriate bits...
72 Init *BitsRecTy::convertValue(IntInit *II) {
73 int64_t Value = II->getValue();
74 // Make sure this bitfield is large enough to hold the integer value...
75 if (Value >= 0) {
76 if (Value & ~((1LL << Size)-1))
77 return 0;
78 } else {
79 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
80 return 0;
83 BitsInit *Ret = new BitsInit(Size);
84 for (unsigned i = 0; i != Size; ++i)
85 Ret->setBit(i, new BitInit(Value & (1LL << i)));
87 return Ret;
90 Init *BitsRecTy::convertValue(BitsInit *BI) {
91 // If the number of bits is right, return it. Otherwise we need to expand or
92 // truncate...
93 if (BI->getNumBits() == Size) return BI;
94 return 0;
97 Init *BitsRecTy::convertValue(TypedInit *VI) {
98 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
99 if (BRT->Size == Size) {
100 BitsInit *Ret = new BitsInit(Size);
101 for (unsigned i = 0; i != Size; ++i)
102 Ret->setBit(i, new VarBitInit(VI, i));
103 return Ret;
105 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
106 BitsInit *Ret = new BitsInit(1);
107 Ret->setBit(0, VI);
108 return Ret;
111 return 0;
114 Init *IntRecTy::convertValue(BitInit *BI) {
115 return new IntInit(BI->getValue());
118 Init *IntRecTy::convertValue(BitsInit *BI) {
119 int64_t Result = 0;
120 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
121 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
122 Result |= Bit->getValue() << i;
123 } else {
124 return 0;
126 return new IntInit(Result);
129 Init *IntRecTy::convertValue(TypedInit *TI) {
130 if (TI->getType()->typeIsConvertibleTo(this))
131 return TI; // Accept variable if already of the right type!
132 return 0;
135 Init *StringRecTy::convertValue(BinOpInit *BO) {
136 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
137 Init *L = BO->getLHS()->convertInitializerTo(this);
138 Init *R = BO->getRHS()->convertInitializerTo(this);
139 if (L == 0 || R == 0) return 0;
140 if (L != BO->getLHS() || R != BO->getRHS())
141 return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy);
142 return BO;
144 if (BO->getOpcode() == BinOpInit::NAMECONCAT) {
145 if (BO->getType()->getAsString() == getAsString()) {
146 Init *L = BO->getLHS()->convertInitializerTo(this);
147 Init *R = BO->getRHS()->convertInitializerTo(this);
148 if (L == 0 || R == 0) return 0;
149 if (L != BO->getLHS() || R != BO->getRHS())
150 return new BinOpInit(BinOpInit::NAMECONCAT, L, R, new StringRecTy);
151 return BO;
155 return convertValue((TypedInit*)BO);
159 Init *StringRecTy::convertValue(TypedInit *TI) {
160 if (dynamic_cast<StringRecTy*>(TI->getType()))
161 return TI; // Accept variable if already of the right type!
162 return 0;
165 std::string ListRecTy::getAsString() const {
166 return "list<" + Ty->getAsString() + ">";
169 Init *ListRecTy::convertValue(ListInit *LI) {
170 std::vector<Init*> Elements;
172 // Verify that all of the elements of the list are subclasses of the
173 // appropriate class!
174 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
175 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
176 Elements.push_back(CI);
177 else
178 return 0;
180 return new ListInit(Elements);
183 Init *ListRecTy::convertValue(TypedInit *TI) {
184 // Ensure that TI is compatible with our class.
185 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
186 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
187 return TI;
188 return 0;
191 Init *CodeRecTy::convertValue(TypedInit *TI) {
192 if (TI->getType()->typeIsConvertibleTo(this))
193 return TI;
194 return 0;
197 Init *DagRecTy::convertValue(TypedInit *TI) {
198 if (TI->getType()->typeIsConvertibleTo(this))
199 return TI;
200 return 0;
203 Init *DagRecTy::convertValue(BinOpInit *BO) {
204 if (BO->getOpcode() == BinOpInit::CONCAT) {
205 Init *L = BO->getLHS()->convertInitializerTo(this);
206 Init *R = BO->getRHS()->convertInitializerTo(this);
207 if (L == 0 || R == 0) return 0;
208 if (L != BO->getLHS() || R != BO->getRHS())
209 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
210 return BO;
212 if (BO->getOpcode() == BinOpInit::NAMECONCAT) {
213 if (BO->getType()->getAsString() == getAsString()) {
214 Init *L = BO->getLHS()->convertInitializerTo(this);
215 Init *R = BO->getRHS()->convertInitializerTo(this);
216 if (L == 0 || R == 0) return 0;
217 if (L != BO->getLHS() || R != BO->getRHS())
218 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
219 return BO;
222 return 0;
225 std::string RecordRecTy::getAsString() const {
226 return Rec->getName();
229 Init *RecordRecTy::convertValue(DefInit *DI) {
230 // Ensure that DI is a subclass of Rec.
231 if (!DI->getDef()->isSubClassOf(Rec))
232 return 0;
233 return DI;
236 Init *RecordRecTy::convertValue(TypedInit *TI) {
237 // Ensure that TI is compatible with Rec.
238 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
239 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
240 RRT->getRecord() == getRecord())
241 return TI;
242 return 0;
245 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
246 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
250 //===----------------------------------------------------------------------===//
251 // Initializer implementations
252 //===----------------------------------------------------------------------===//
254 void Init::dump() const { return print(*cerr.stream()); }
256 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
257 BitsInit *BI = new BitsInit(Bits.size());
258 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
259 if (Bits[i] >= getNumBits()) {
260 delete BI;
261 return 0;
263 BI->setBit(i, getBit(Bits[i]));
265 return BI;
268 std::string BitsInit::getAsString() const {
269 //if (!printInHex(OS)) return;
270 //if (!printAsVariable(OS)) return;
271 //if (!printAsUnset(OS)) return;
273 std::string Result = "{ ";
274 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
275 if (i) Result += ", ";
276 if (Init *Bit = getBit(e-i-1))
277 Result += Bit->getAsString();
278 else
279 Result += "*";
281 return Result + " }";
284 bool BitsInit::printInHex(std::ostream &OS) const {
285 // First, attempt to convert the value into an integer value...
286 int64_t Result = 0;
287 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
288 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
289 Result |= Bit->getValue() << i;
290 } else {
291 return true;
294 OS << "0x" << std::hex << Result << std::dec;
295 return false;
298 bool BitsInit::printAsVariable(std::ostream &OS) const {
299 // Get the variable that we may be set equal to...
300 assert(getNumBits() != 0);
301 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
302 if (FirstBit == 0) return true;
303 TypedInit *Var = FirstBit->getVariable();
305 // Check to make sure the types are compatible.
306 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
307 if (Ty == 0) return true;
308 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
310 // Check to make sure all bits are referring to the right bits in the variable
311 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
312 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
313 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
314 return true;
317 Var->print(OS);
318 return false;
321 bool BitsInit::printAsUnset(std::ostream &OS) const {
322 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
323 if (!dynamic_cast<UnsetInit*>(getBit(i)))
324 return true;
325 OS << "?";
326 return false;
329 // resolveReferences - If there are any field references that refer to fields
330 // that have been filled in, we can propagate the values now.
332 Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
333 bool Changed = false;
334 BitsInit *New = new BitsInit(getNumBits());
336 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
337 Init *B;
338 Init *CurBit = getBit(i);
340 do {
341 B = CurBit;
342 CurBit = CurBit->resolveReferences(R, RV);
343 Changed |= B != CurBit;
344 } while (B != CurBit);
345 New->setBit(i, CurBit);
348 if (Changed)
349 return New;
350 delete New;
351 return this;
354 std::string IntInit::getAsString() const {
355 return itostr(Value);
358 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
359 BitsInit *BI = new BitsInit(Bits.size());
361 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
362 if (Bits[i] >= 64) {
363 delete BI;
364 return 0;
366 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
368 return BI;
371 Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
372 std::vector<Init*> Vals;
373 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
374 if (Elements[i] >= getSize())
375 return 0;
376 Vals.push_back(getElement(Elements[i]));
378 return new ListInit(Vals);
381 Record *ListInit::getElementAsRecord(unsigned i) const {
382 assert(i < Values.size() && "List element index out of range!");
383 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
384 if (DI == 0) throw "Expected record in list!";
385 return DI->getDef();
388 Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
389 std::vector<Init*> Resolved;
390 Resolved.reserve(getSize());
391 bool Changed = false;
393 for (unsigned i = 0, e = getSize(); i != e; ++i) {
394 Init *E;
395 Init *CurElt = getElement(i);
397 do {
398 E = CurElt;
399 CurElt = CurElt->resolveReferences(R, RV);
400 Changed |= E != CurElt;
401 } while (E != CurElt);
402 Resolved.push_back(E);
405 if (Changed)
406 return new ListInit(Resolved);
407 return this;
410 std::string ListInit::getAsString() const {
411 std::string Result = "[";
412 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
413 if (i) Result += ", ";
414 Result += Values[i]->getAsString();
416 return Result + "]";
419 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
420 switch (getOpcode()) {
421 default: assert(0 && "Unknown binop");
422 case CONCAT: {
423 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
424 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
425 if (LHSs && RHSs) {
426 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
427 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
428 if (LOp->getDef() != ROp->getDef()) {
429 bool LIsOps =
430 LOp->getDef()->getName() == "outs" ||
431 LOp->getDef()->getName() != "ins" ||
432 LOp->getDef()->getName() != "defs";
433 bool RIsOps =
434 ROp->getDef()->getName() == "outs" ||
435 ROp->getDef()->getName() != "ins" ||
436 ROp->getDef()->getName() != "defs";
437 if (!LIsOps || !RIsOps)
438 throw "Concated Dag operators do not match!";
440 std::vector<Init*> Args;
441 std::vector<std::string> ArgNames;
442 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
443 Args.push_back(LHSs->getArg(i));
444 ArgNames.push_back(LHSs->getArgName(i));
446 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
447 Args.push_back(RHSs->getArg(i));
448 ArgNames.push_back(RHSs->getArgName(i));
450 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
452 break;
454 case STRCONCAT: {
455 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
456 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
457 if (LHSs && RHSs)
458 return new StringInit(LHSs->getValue() + RHSs->getValue());
459 break;
461 case NAMECONCAT: {
462 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
463 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
464 if (LHSs && RHSs) {
465 std::string Name(LHSs->getValue() + RHSs->getValue());
467 // From TGParser::ParseIDValue
468 if (CurRec) {
469 if (const RecordVal *RV = CurRec->getValue(Name)) {
470 if (RV->getType() != getType()) {
471 throw "type mismatch in nameconcat";
473 return new VarInit(Name, RV->getType());
476 std::string TemplateArgName = CurRec->getName()+":"+Name;
477 if (CurRec->isTemplateArg(TemplateArgName)) {
478 const RecordVal *RV = CurRec->getValue(TemplateArgName);
479 assert(RV && "Template arg doesn't exist??");
481 if (RV->getType() != getType()) {
482 throw "type mismatch in nameconcat";
485 return new VarInit(TemplateArgName, RV->getType());
489 if (CurMultiClass) {
490 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
491 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
492 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
493 assert(RV && "Template arg doesn't exist??");
495 if (RV->getType() != getType()) {
496 throw "type mismatch in nameconcat";
499 return new VarInit(MCName, RV->getType());
503 if (Record *D = Records.getDef(Name))
504 return new DefInit(D);
506 cerr << "Variable not defined: '" + Name + "'\n";
507 assert(0 && "Variable not found");
508 return 0;
510 break;
512 case SHL:
513 case SRA:
514 case SRL: {
515 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
516 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
517 if (LHSi && RHSi) {
518 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
519 int64_t Result;
520 switch (getOpcode()) {
521 default: assert(0 && "Bad opcode!");
522 case SHL: Result = LHSv << RHSv; break;
523 case SRA: Result = LHSv >> RHSv; break;
524 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
526 return new IntInit(Result);
528 break;
531 return this;
534 Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
535 Init *lhs = LHS->resolveReferences(R, RV);
536 Init *rhs = RHS->resolveReferences(R, RV);
538 if (LHS != lhs || RHS != rhs)
539 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
540 return Fold(&R, 0);
543 std::string BinOpInit::getAsString() const {
544 std::string Result;
545 switch (Opc) {
546 case CONCAT: Result = "!con"; break;
547 case SHL: Result = "!shl"; break;
548 case SRA: Result = "!sra"; break;
549 case SRL: Result = "!srl"; break;
550 case STRCONCAT: Result = "!strconcat"; break;
551 case NAMECONCAT:
552 Result = "!nameconcat<" + getType()->getAsString() + ">"; break;
554 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
557 Init *BinOpInit::resolveBitReference(Record &R, const RecordVal *IRV,
558 unsigned Bit) {
559 Init *Folded = Fold(&R, 0);
561 if (Folded != this) {
562 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
563 if (Typed) {
564 return Typed->resolveBitReference(R, IRV, Bit);
568 return 0;
571 Init *BinOpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
572 unsigned Elt) {
573 Init *Folded = Fold(&R, 0);
575 if (Folded != this) {
576 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
577 if (Typed) {
578 return Typed->resolveListElementReference(R, IRV, Elt);
582 return 0;
585 Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
586 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
587 if (T == 0) return 0; // Cannot subscript a non-bits variable...
588 unsigned NumBits = T->getNumBits();
590 BitsInit *BI = new BitsInit(Bits.size());
591 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
592 if (Bits[i] >= NumBits) {
593 delete BI;
594 return 0;
596 BI->setBit(i, new VarBitInit(this, Bits[i]));
598 return BI;
601 Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
602 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
603 if (T == 0) return 0; // Cannot subscript a non-list variable...
605 if (Elements.size() == 1)
606 return new VarListElementInit(this, Elements[0]);
608 std::vector<Init*> ListInits;
609 ListInits.reserve(Elements.size());
610 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
611 ListInits.push_back(new VarListElementInit(this, Elements[i]));
612 return new ListInit(ListInits);
616 Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
617 unsigned Bit) {
618 if (R.isTemplateArg(getName())) return 0;
619 if (IRV && IRV->getName() != getName()) return 0;
621 RecordVal *RV = R.getValue(getName());
622 assert(RV && "Reference to a non-existant variable?");
623 assert(dynamic_cast<BitsInit*>(RV->getValue()));
624 BitsInit *BI = (BitsInit*)RV->getValue();
626 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
627 Init *B = BI->getBit(Bit);
629 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
630 return B; // Replace the VarBitInit with it.
631 return 0;
634 Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
635 unsigned Elt) {
636 if (R.isTemplateArg(getName())) return 0;
637 if (IRV && IRV->getName() != getName()) return 0;
639 RecordVal *RV = R.getValue(getName());
640 assert(RV && "Reference to a non-existant variable?");
641 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
642 assert(LI && "Invalid list element!");
644 if (Elt >= LI->getSize())
645 return 0; // Out of range reference.
646 Init *E = LI->getElement(Elt);
647 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
648 return E; // Replace the VarListElementInit with it.
649 return 0;
653 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
654 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
655 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
656 return RV->getType();
657 return 0;
660 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
661 if (dynamic_cast<RecordRecTy*>(getType()))
662 if (const RecordVal *RV = R.getValue(VarName)) {
663 Init *TheInit = RV->getValue();
664 assert(TheInit != this && "Infinite loop detected!");
665 if (Init *I = TheInit->getFieldInit(R, FieldName))
666 return I;
667 else
668 return 0;
670 return 0;
673 /// resolveReferences - This method is used by classes that refer to other
674 /// variables which may not be defined at the time they expression is formed.
675 /// If a value is set for the variable later, this method will be called on
676 /// users of the value to allow the value to propagate out.
678 Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
679 if (RecordVal *Val = R.getValue(VarName))
680 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
681 return Val->getValue();
682 return this;
685 std::string VarBitInit::getAsString() const {
686 return TI->getAsString() + "{" + utostr(Bit) + "}";
689 Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
690 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
691 return I;
692 return this;
695 std::string VarListElementInit::getAsString() const {
696 return TI->getAsString() + "[" + utostr(Element) + "]";
699 Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
700 if (Init *I = getVariable()->resolveListElementReference(R, RV,
701 getElementNum()))
702 return I;
703 return this;
706 Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
707 unsigned Bit) {
708 // FIXME: This should be implemented, to support references like:
709 // bit B = AA[0]{1};
710 return 0;
713 Init *VarListElementInit::
714 resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
715 // FIXME: This should be implemented, to support references like:
716 // int B = AA[0][1];
717 return 0;
720 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
721 if (const RecordVal *RV = Def->getValue(FieldName))
722 return RV->getType();
723 return 0;
726 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
727 return Def->getValue(FieldName)->getValue();
731 std::string DefInit::getAsString() const {
732 return Def->getName();
735 Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
736 unsigned Bit) {
737 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
738 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
739 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
740 Init *B = BI->getBit(Bit);
742 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
743 return B; // Replace the VarBitInit with it.
745 return 0;
748 Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
749 unsigned Elt) {
750 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
751 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
752 if (Elt >= LI->getSize()) return 0;
753 Init *E = LI->getElement(Elt);
755 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
756 return E; // Replace the VarListElementInit with it.
758 return 0;
761 Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
762 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
764 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
765 if (BitsVal) {
766 Init *BVR = BitsVal->resolveReferences(R, RV);
767 return BVR->isComplete() ? BVR : this;
770 if (NewRec != Rec) {
771 return new FieldInit(NewRec, FieldName);
773 return this;
776 Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
777 std::vector<Init*> NewArgs;
778 for (unsigned i = 0, e = Args.size(); i != e; ++i)
779 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
781 Init *Op = Val->resolveReferences(R, RV);
783 if (Args != NewArgs || Op != Val)
784 return new DagInit(Op, "", NewArgs, ArgNames);
786 return this;
790 std::string DagInit::getAsString() const {
791 std::string Result = "(" + Val->getAsString();
792 if (!ValName.empty())
793 Result += ":" + ValName;
794 if (Args.size()) {
795 Result += " " + Args[0]->getAsString();
796 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
797 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
798 Result += ", " + Args[i]->getAsString();
799 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
802 return Result + ")";
806 //===----------------------------------------------------------------------===//
807 // Other implementations
808 //===----------------------------------------------------------------------===//
810 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
811 : Name(N), Ty(T), Prefix(P) {
812 Value = Ty->convertValue(new UnsetInit());
813 assert(Value && "Cannot create unset value for current type!");
816 void RecordVal::dump() const { cerr << *this; }
818 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
819 if (getPrefix()) OS << "field ";
820 OS << *getType() << " " << getName();
822 if (getValue())
823 OS << " = " << *getValue();
825 if (PrintSem) OS << ";\n";
828 void Record::setName(const std::string &Name) {
829 if (Records.getDef(getName()) == this) {
830 Records.removeDef(getName());
831 this->Name = Name;
832 Records.addDef(this);
833 } else {
834 Records.removeClass(getName());
835 this->Name = Name;
836 Records.addClass(this);
840 /// resolveReferencesTo - If anything in this record refers to RV, replace the
841 /// reference to RV with the RHS of RV. If RV is null, we resolve all possible
842 /// references.
843 void Record::resolveReferencesTo(const RecordVal *RV) {
844 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
845 if (Init *V = Values[i].getValue())
846 Values[i].setValue(V->resolveReferences(*this, RV));
851 void Record::dump() const { cerr << *this; }
853 std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
854 OS << R.getName();
856 const std::vector<std::string> &TArgs = R.getTemplateArgs();
857 if (!TArgs.empty()) {
858 OS << "<";
859 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
860 if (i) OS << ", ";
861 const RecordVal *RV = R.getValue(TArgs[i]);
862 assert(RV && "Template argument record not found??");
863 RV->print(OS, false);
865 OS << ">";
868 OS << " {";
869 const std::vector<Record*> &SC = R.getSuperClasses();
870 if (!SC.empty()) {
871 OS << "\t//";
872 for (unsigned i = 0, e = SC.size(); i != e; ++i)
873 OS << " " << SC[i]->getName();
875 OS << "\n";
877 const std::vector<RecordVal> &Vals = R.getValues();
878 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
879 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
880 OS << Vals[i];
881 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
882 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
883 OS << Vals[i];
885 return OS << "}\n";
888 /// getValueInit - Return the initializer for a value with the specified name,
889 /// or throw an exception if the field does not exist.
891 Init *Record::getValueInit(const std::string &FieldName) const {
892 const RecordVal *R = getValue(FieldName);
893 if (R == 0 || R->getValue() == 0)
894 throw "Record `" + getName() + "' does not have a field named `" +
895 FieldName + "'!\n";
896 return R->getValue();
900 /// getValueAsString - This method looks up the specified field and returns its
901 /// value as a string, throwing an exception if the field does not exist or if
902 /// the value is not a string.
904 std::string Record::getValueAsString(const std::string &FieldName) const {
905 const RecordVal *R = getValue(FieldName);
906 if (R == 0 || R->getValue() == 0)
907 throw "Record `" + getName() + "' does not have a field named `" +
908 FieldName + "'!\n";
910 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
911 return SI->getValue();
912 throw "Record `" + getName() + "', field `" + FieldName +
913 "' does not have a string initializer!";
916 /// getValueAsBitsInit - This method looks up the specified field and returns
917 /// its value as a BitsInit, throwing an exception if the field does not exist
918 /// or if the value is not the right type.
920 BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
921 const RecordVal *R = getValue(FieldName);
922 if (R == 0 || R->getValue() == 0)
923 throw "Record `" + getName() + "' does not have a field named `" +
924 FieldName + "'!\n";
926 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
927 return BI;
928 throw "Record `" + getName() + "', field `" + FieldName +
929 "' does not have a BitsInit initializer!";
932 /// getValueAsListInit - This method looks up the specified field and returns
933 /// its value as a ListInit, throwing an exception if the field does not exist
934 /// or if the value is not the right type.
936 ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
937 const RecordVal *R = getValue(FieldName);
938 if (R == 0 || R->getValue() == 0)
939 throw "Record `" + getName() + "' does not have a field named `" +
940 FieldName + "'!\n";
942 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
943 return LI;
944 throw "Record `" + getName() + "', field `" + FieldName +
945 "' does not have a list initializer!";
948 /// getValueAsListOfDefs - This method looks up the specified field and returns
949 /// its value as a vector of records, throwing an exception if the field does
950 /// not exist or if the value is not the right type.
952 std::vector<Record*>
953 Record::getValueAsListOfDefs(const std::string &FieldName) const {
954 ListInit *List = getValueAsListInit(FieldName);
955 std::vector<Record*> Defs;
956 for (unsigned i = 0; i < List->getSize(); i++) {
957 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
958 Defs.push_back(DI->getDef());
959 } else {
960 throw "Record `" + getName() + "', field `" + FieldName +
961 "' list is not entirely DefInit!";
964 return Defs;
967 /// getValueAsInt - This method looks up the specified field and returns its
968 /// value as an int64_t, throwing an exception if the field does not exist or if
969 /// the value is not the right type.
971 int64_t Record::getValueAsInt(const std::string &FieldName) const {
972 const RecordVal *R = getValue(FieldName);
973 if (R == 0 || R->getValue() == 0)
974 throw "Record `" + getName() + "' does not have a field named `" +
975 FieldName + "'!\n";
977 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
978 return II->getValue();
979 throw "Record `" + getName() + "', field `" + FieldName +
980 "' does not have an int initializer!";
983 /// getValueAsListOfInts - This method looks up the specified field and returns
984 /// its value as a vector of integers, throwing an exception if the field does
985 /// not exist or if the value is not the right type.
987 std::vector<int64_t>
988 Record::getValueAsListOfInts(const std::string &FieldName) const {
989 ListInit *List = getValueAsListInit(FieldName);
990 std::vector<int64_t> Ints;
991 for (unsigned i = 0; i < List->getSize(); i++) {
992 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
993 Ints.push_back(II->getValue());
994 } else {
995 throw "Record `" + getName() + "', field `" + FieldName +
996 "' does not have a list of ints initializer!";
999 return Ints;
1002 /// getValueAsDef - This method looks up the specified field and returns its
1003 /// value as a Record, throwing an exception if the field does not exist or if
1004 /// the value is not the right type.
1006 Record *Record::getValueAsDef(const std::string &FieldName) const {
1007 const RecordVal *R = getValue(FieldName);
1008 if (R == 0 || R->getValue() == 0)
1009 throw "Record `" + getName() + "' does not have a field named `" +
1010 FieldName + "'!\n";
1012 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1013 return DI->getDef();
1014 throw "Record `" + getName() + "', field `" + FieldName +
1015 "' does not have a def initializer!";
1018 /// getValueAsBit - This method looks up the specified field and returns its
1019 /// value as a bit, throwing an exception if the field does not exist or if
1020 /// the value is not the right type.
1022 bool Record::getValueAsBit(const std::string &FieldName) const {
1023 const RecordVal *R = getValue(FieldName);
1024 if (R == 0 || R->getValue() == 0)
1025 throw "Record `" + getName() + "' does not have a field named `" +
1026 FieldName + "'!\n";
1028 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1029 return BI->getValue();
1030 throw "Record `" + getName() + "', field `" + FieldName +
1031 "' does not have a bit initializer!";
1034 /// getValueAsDag - This method looks up the specified field and returns its
1035 /// value as an Dag, throwing an exception if the field does not exist or if
1036 /// the value is not the right type.
1038 DagInit *Record::getValueAsDag(const std::string &FieldName) const {
1039 const RecordVal *R = getValue(FieldName);
1040 if (R == 0 || R->getValue() == 0)
1041 throw "Record `" + getName() + "' does not have a field named `" +
1042 FieldName + "'!\n";
1044 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1045 return DI;
1046 throw "Record `" + getName() + "', field `" + FieldName +
1047 "' does not have a dag initializer!";
1050 std::string Record::getValueAsCode(const std::string &FieldName) const {
1051 const RecordVal *R = getValue(FieldName);
1052 if (R == 0 || R->getValue() == 0)
1053 throw "Record `" + getName() + "' does not have a field named `" +
1054 FieldName + "'!\n";
1056 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1057 return CI->getValue();
1058 throw "Record `" + getName() + "', field `" + FieldName +
1059 "' does not have a code initializer!";
1063 void MultiClass::dump() const {
1064 cerr << "Record:\n";
1065 Rec.dump();
1067 cerr << "Defs:\n";
1068 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1069 rend = DefPrototypes.end();
1070 r != rend;
1071 ++r) {
1072 (*r)->dump();
1077 void RecordKeeper::dump() const { cerr << *this; }
1079 std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
1080 OS << "------------- Classes -----------------\n";
1081 const std::map<std::string, Record*> &Classes = RK.getClasses();
1082 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
1083 E = Classes.end(); I != E; ++I)
1084 OS << "class " << *I->second;
1086 OS << "------------- Defs -----------------\n";
1087 const std::map<std::string, Record*> &Defs = RK.getDefs();
1088 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
1089 E = Defs.end(); I != E; ++I)
1090 OS << "def " << *I->second;
1091 return OS;
1095 /// getAllDerivedDefinitions - This method returns all concrete definitions
1096 /// that derive from the specified class name. If a class with the specified
1097 /// name does not exist, an error is printed and true is returned.
1098 std::vector<Record*>
1099 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1100 Record *Class = Records.getClass(ClassName);
1101 if (!Class)
1102 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
1104 std::vector<Record*> Defs;
1105 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1106 E = getDefs().end(); I != E; ++I)
1107 if (I->second->isSubClassOf(Class))
1108 Defs.push_back(I->second);
1110 return Defs;