blob: c1ef4ed82f40587dda9e8e2aa682b5e5f33b53d1 [file] [log] [blame]
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001//===- Record.cpp - Record implementation ---------------------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswelld3032032003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswelld3032032003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00009//
Chris Lattner51ffbf12006-03-31 21:53:49 +000010// Implement the tablegen record classes.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "Record.h"
Misha Brukman46cee7d2004-09-30 18:27:39 +000015#include "llvm/Support/DataTypes.h"
Duraid Madina14492af2005-12-26 05:08:55 +000016#include <ios>
17
Chris Lattner68478662004-08-01 03:55:39 +000018using namespace llvm;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000019
20//===----------------------------------------------------------------------===//
21// Type implementations
22//===----------------------------------------------------------------------===//
23
24void RecTy::dump() const { print(std::cerr); }
25
26Init *BitRecTy::convertValue(BitsInit *BI) {
27 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
28 return BI->getBit(0);
29}
30
31bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
32 return RHS->getNumBits() == 1;
33}
34
35Init *BitRecTy::convertValue(IntInit *II) {
36 int Val = II->getValue();
37 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
Misha Brukman650ba8e2005-04-22 00:00:37 +000038
39 return new BitInit(Val != 0);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000040}
41
42Init *BitRecTy::convertValue(TypedInit *VI) {
43 if (dynamic_cast<BitRecTy*>(VI->getType()))
44 return VI; // Accept variable if it is already of bit type!
45 return 0;
46}
47
48Init *BitsRecTy::convertValue(UnsetInit *UI) {
49 BitsInit *Ret = new BitsInit(Size);
50
51 for (unsigned i = 0; i != Size; ++i)
52 Ret->setBit(i, new UnsetInit());
53 return Ret;
54}
55
56Init *BitsRecTy::convertValue(BitInit *UI) {
57 if (Size != 1) return 0; // Can only convert single bit...
58 BitsInit *Ret = new BitsInit(1);
59 Ret->setBit(0, UI);
60 return Ret;
61}
62
63// convertValue from Int initializer to bits type: Split the integer up into the
64// appropriate bits...
65//
66Init *BitsRecTy::convertValue(IntInit *II) {
Misha Brukman6752fb52004-06-21 18:01:47 +000067 int64_t Value = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000068 // Make sure this bitfield is large enough to hold the integer value...
69 if (Value >= 0) {
Misha Brukman6752fb52004-06-21 18:01:47 +000070 if (Value & ~((1LL << Size)-1))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000071 return 0;
72 } else {
Jeff Cohen0add83e2006-02-18 03:20:33 +000073 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000074 return 0;
75 }
76
77 BitsInit *Ret = new BitsInit(Size);
78 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen0add83e2006-02-18 03:20:33 +000079 Ret->setBit(i, new BitInit(Value & (1LL << i)));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000080
81 return Ret;
82}
83
84Init *BitsRecTy::convertValue(BitsInit *BI) {
85 // If the number of bits is right, return it. Otherwise we need to expand or
86 // truncate...
87 if (BI->getNumBits() == Size) return BI;
88 return 0;
89}
90
91Init *BitsRecTy::convertValue(TypedInit *VI) {
92 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
93 if (BRT->Size == Size) {
94 BitsInit *Ret = new BitsInit(Size);
95 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen88e7b722005-04-22 04:13:13 +000096 Ret->setBit(i, new VarBitInit(VI, i));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000097 return Ret;
98 }
99 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
100 BitsInit *Ret = new BitsInit(1);
101 Ret->setBit(0, VI);
102 return Ret;
103 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000104
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000105 return 0;
106}
107
108Init *IntRecTy::convertValue(BitInit *BI) {
109 return new IntInit(BI->getValue());
110}
111
112Init *IntRecTy::convertValue(BitsInit *BI) {
113 int Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000114 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000115 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
116 Result |= Bit->getValue() << i;
117 } else {
118 return 0;
119 }
120 return new IntInit(Result);
121}
122
123Init *IntRecTy::convertValue(TypedInit *TI) {
124 if (TI->getType()->typeIsConvertibleTo(this))
125 return TI; // Accept variable if already of the right type!
126 return 0;
127}
128
Chris Lattner51ffbf12006-03-31 21:53:49 +0000129Init *StringRecTy::convertValue(BinOpInit *BO) {
130 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
131 Init *L = BO->getLHS()->convertInitializerTo(this);
132 Init *R = BO->getRHS()->convertInitializerTo(this);
133 if (L == 0 || R == 0) return 0;
134 if (L != BO->getLHS() || R != BO->getRHS())
135 return new BinOpInit(BinOpInit::STRCONCAT, L, R);
136 return BO;
137 }
138 return 0;
139}
140
141
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000142Init *StringRecTy::convertValue(TypedInit *TI) {
143 if (dynamic_cast<StringRecTy*>(TI->getType()))
144 return TI; // Accept variable if already of the right type!
145 return 0;
146}
147
148void ListRecTy::print(std::ostream &OS) const {
149 OS << "list<" << *Ty << ">";
150}
151
152Init *ListRecTy::convertValue(ListInit *LI) {
153 std::vector<Init*> Elements;
154
155 // Verify that all of the elements of the list are subclasses of the
156 // appropriate class!
157 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
158 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
159 Elements.push_back(CI);
160 else
161 return 0;
162
163 return new ListInit(Elements);
164}
165
166Init *ListRecTy::convertValue(TypedInit *TI) {
167 // Ensure that TI is compatible with our class.
168 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
169 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
170 return TI;
171 return 0;
172}
173
174Init *CodeRecTy::convertValue(TypedInit *TI) {
175 if (TI->getType()->typeIsConvertibleTo(this))
176 return TI;
177 return 0;
178}
179
180Init *DagRecTy::convertValue(TypedInit *TI) {
181 if (TI->getType()->typeIsConvertibleTo(this))
182 return TI;
183 return 0;
184}
185
186
187void RecordRecTy::print(std::ostream &OS) const {
188 OS << Rec->getName();
189}
190
191Init *RecordRecTy::convertValue(DefInit *DI) {
192 // Ensure that DI is a subclass of Rec.
193 if (!DI->getDef()->isSubClassOf(Rec))
194 return 0;
195 return DI;
196}
197
198Init *RecordRecTy::convertValue(TypedInit *TI) {
199 // Ensure that TI is compatible with Rec.
200 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
201 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
202 RRT->getRecord() == getRecord())
203 return TI;
204 return 0;
205}
206
207bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
208 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
209}
210
211
212//===----------------------------------------------------------------------===//
213// Initializer implementations
214//===----------------------------------------------------------------------===//
215
216void Init::dump() const { return print(std::cerr); }
217
218Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
219 BitsInit *BI = new BitsInit(Bits.size());
220 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
221 if (Bits[i] >= getNumBits()) {
222 delete BI;
223 return 0;
224 }
225 BI->setBit(i, getBit(Bits[i]));
226 }
227 return BI;
228}
229
230void BitsInit::print(std::ostream &OS) const {
231 //if (!printInHex(OS)) return;
232 //if (!printAsVariable(OS)) return;
233 //if (!printAsUnset(OS)) return;
234
235 OS << "{ ";
236 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
237 if (i) OS << ", ";
238 if (Init *Bit = getBit(e-i-1))
239 Bit->print(OS);
240 else
241 OS << "*";
242 }
243 OS << " }";
244}
245
246bool BitsInit::printInHex(std::ostream &OS) const {
247 // First, attempt to convert the value into an integer value...
248 int Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000249 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000250 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
251 Result |= Bit->getValue() << i;
252 } else {
253 return true;
254 }
255
256 OS << "0x" << std::hex << Result << std::dec;
257 return false;
258}
259
260bool BitsInit::printAsVariable(std::ostream &OS) const {
261 // Get the variable that we may be set equal to...
262 assert(getNumBits() != 0);
263 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
264 if (FirstBit == 0) return true;
265 TypedInit *Var = FirstBit->getVariable();
266
267 // Check to make sure the types are compatible.
268 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
269 if (Ty == 0) return true;
270 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
271
272 // Check to make sure all bits are referring to the right bits in the variable
273 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
274 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
275 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
276 return true;
277 }
278
279 Var->print(OS);
280 return false;
281}
282
283bool BitsInit::printAsUnset(std::ostream &OS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000284 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000285 if (!dynamic_cast<UnsetInit*>(getBit(i)))
286 return true;
287 OS << "?";
288 return false;
289}
290
291// resolveReferences - If there are any field references that refer to fields
292// that have been filled in, we can propagate the values now.
293//
Chris Lattneref943742005-04-19 03:36:21 +0000294Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000295 bool Changed = false;
296 BitsInit *New = new BitsInit(getNumBits());
297
298 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
299 Init *B;
300 Init *CurBit = getBit(i);
301
302 do {
303 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000304 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000305 Changed |= B != CurBit;
306 } while (B != CurBit);
307 New->setBit(i, CurBit);
308 }
309
310 if (Changed)
311 return New;
312 delete New;
313 return this;
314}
315
316Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
317 BitsInit *BI = new BitsInit(Bits.size());
318
319 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
320 if (Bits[i] >= 32) {
321 delete BI;
322 return 0;
323 }
324 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
325 }
326 return BI;
327}
328
Chris Lattner8bf9e062004-07-26 23:21:34 +0000329Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
330 std::vector<Init*> Vals;
331 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
332 if (Elements[i] >= getSize())
333 return 0;
334 Vals.push_back(getElement(Elements[i]));
335 }
336 return new ListInit(Vals);
337}
338
Chris Lattneref943742005-04-19 03:36:21 +0000339Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000340 std::vector<Init*> Resolved;
341 Resolved.reserve(getSize());
342 bool Changed = false;
343
344 for (unsigned i = 0, e = getSize(); i != e; ++i) {
345 Init *E;
346 Init *CurElt = getElement(i);
347
348 do {
349 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000350 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000351 Changed |= E != CurElt;
352 } while (E != CurElt);
353 Resolved.push_back(E);
354 }
355
356 if (Changed)
357 return new ListInit(Resolved);
358 return this;
359}
360
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000361void ListInit::print(std::ostream &OS) const {
362 OS << "[";
363 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
364 if (i) OS << ", ";
365 OS << *Values[i];
366 }
367 OS << "]";
368}
369
Chris Lattner51ffbf12006-03-31 21:53:49 +0000370Init *BinOpInit::Fold() {
371 switch (getOpcode()) {
372 default: assert(0 && "Unknown binop");
373 case STRCONCAT: {
374 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
375 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
376 if (LHSs && RHSs)
377 return new StringInit(LHSs->getValue() + RHSs->getValue());
378 break;
379 }
380 case SHL:
381 case SRA:
382 case SRL: {
383 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
384 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
385 if (LHSi && RHSi) {
386 int LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
387 int Result;
388 switch (getOpcode()) {
389 default: assert(0 && "Bad opcode!");
390 case SHL: Result = LHSv << RHSv; break;
391 case SRA: Result = LHSv >> RHSv; break;
392 case SRL: Result = (unsigned)LHSv >> (unsigned)RHSv; break;
393 }
394 return new IntInit(Result);
395 }
396 break;
397 }
398 }
399 return this;
400}
401
402Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
403 Init *lhs = LHS->resolveReferences(R, RV);
404 Init *rhs = RHS->resolveReferences(R, RV);
405
406 if (LHS != lhs || RHS != rhs)
407 return (new BinOpInit(getOpcode(), lhs, rhs))->Fold();
408 return Fold();
409}
410
411void BinOpInit::print(std::ostream &OS) const {
412 switch (Opc) {
413 case SHL: OS << "!shl"; break;
414 case SRA: OS << "!sra"; break;
415 case SRL: OS << "!srl"; break;
416 case STRCONCAT: OS << "!strconcat"; break;
417 }
418 OS << "(";
419 LHS->print(OS);
420 OS << ", ";
421 RHS->print(OS);
422 OS << ")";
423}
424
Chris Lattner577fc3f2004-07-27 01:01:21 +0000425Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000426 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
427 if (T == 0) return 0; // Cannot subscript a non-bits variable...
428 unsigned NumBits = T->getNumBits();
429
430 BitsInit *BI = new BitsInit(Bits.size());
431 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
432 if (Bits[i] >= NumBits) {
433 delete BI;
434 return 0;
435 }
436 BI->setBit(i, new VarBitInit(this, Bits[i]));
437 }
438 return BI;
439}
440
Chris Lattner577fc3f2004-07-27 01:01:21 +0000441Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
442 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
443 if (T == 0) return 0; // Cannot subscript a non-list variable...
444
445 if (Elements.size() == 1)
446 return new VarListElementInit(this, Elements[0]);
447
448 std::vector<Init*> ListInits;
449 ListInits.reserve(Elements.size());
450 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
451 ListInits.push_back(new VarListElementInit(this, Elements[i]));
452 return new ListInit(ListInits);
453}
454
455
Chris Lattneref943742005-04-19 03:36:21 +0000456Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
457 unsigned Bit) {
458 if (R.isTemplateArg(getName())) return 0;
459 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000460
461 RecordVal *RV = R.getValue(getName());
462 assert(RV && "Reference to a non-existant variable?");
463 assert(dynamic_cast<BitsInit*>(RV->getValue()));
464 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +0000465
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000466 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
467 Init *B = BI->getBit(Bit);
468
469 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
470 return B; // Replace the VarBitInit with it.
Chris Lattner577fc3f2004-07-27 01:01:21 +0000471 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000472}
473
Chris Lattneref943742005-04-19 03:36:21 +0000474Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
475 unsigned Elt) {
476 if (R.isTemplateArg(getName())) return 0;
477 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000478
479 RecordVal *RV = R.getValue(getName());
480 assert(RV && "Reference to a non-existant variable?");
481 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
482 assert(LI && "Invalid list element!");
483
484 if (Elt >= LI->getSize())
485 return 0; // Out of range reference.
486 Init *E = LI->getElement(Elt);
487 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
488 return E; // Replace the VarListElementInit with it.
489 return 0;
490}
491
492
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000493RecTy *VarInit::getFieldType(const std::string &FieldName) const {
494 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
495 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
496 return RV->getType();
497 return 0;
498}
499
500Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +0000501 if (dynamic_cast<RecordRecTy*>(getType()))
Chris Lattnerd959ab92004-02-28 16:31:53 +0000502 if (const RecordVal *RV = R.getValue(VarName)) {
503 Init *TheInit = RV->getValue();
504 assert(TheInit != this && "Infinite loop detected!");
505 if (Init *I = TheInit->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000506 return I;
507 else
508 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +0000509 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000510 return 0;
511}
512
513/// resolveReferences - This method is used by classes that refer to other
514/// variables which may not be defined at the time they expression is formed.
515/// If a value is set for the variable later, this method will be called on
516/// users of the value to allow the value to propagate out.
517///
Chris Lattneref943742005-04-19 03:36:21 +0000518Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000519 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +0000520 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000521 return Val->getValue();
522 return this;
523}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000524
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000525
Chris Lattneref943742005-04-19 03:36:21 +0000526Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
527 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000528 return I;
529 return this;
530}
531
Chris Lattneref943742005-04-19 03:36:21 +0000532Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
533 if (Init *I = getVariable()->resolveListElementReference(R, RV,
534 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +0000535 return I;
536 return this;
537}
538
Chris Lattneref943742005-04-19 03:36:21 +0000539Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
540 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000541 // FIXME: This should be implemented, to support references like:
542 // bit B = AA[0]{1};
543 return 0;
544}
545
Chris Lattneref943742005-04-19 03:36:21 +0000546Init *VarListElementInit::
547resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000548 // FIXME: This should be implemented, to support references like:
549 // int B = AA[0][1];
550 return 0;
551}
552
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000553RecTy *DefInit::getFieldType(const std::string &FieldName) const {
554 if (const RecordVal *RV = Def->getValue(FieldName))
555 return RV->getType();
556 return 0;
557}
558
559Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
560 return Def->getValue(FieldName)->getValue();
561}
562
563
564void DefInit::print(std::ostream &OS) const {
565 OS << Def->getName();
566}
567
Chris Lattneref943742005-04-19 03:36:21 +0000568Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
569 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000570 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000571 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
572 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
573 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000574
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000575 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
576 return B; // Replace the VarBitInit with it.
577 }
Chris Lattner577fc3f2004-07-27 01:01:21 +0000578 return 0;
579}
580
Chris Lattneref943742005-04-19 03:36:21 +0000581Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
582 unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000583 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
584 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
585 if (Elt >= LI->getSize()) return 0;
586 Init *E = LI->getElement(Elt);
587
588 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
589 return E; // Replace the VarListElementInit with it.
590 }
591 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000592}
593
Chris Lattneref943742005-04-19 03:36:21 +0000594Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
595 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
596
597 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000598 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +0000599 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000600 return BVR->isComplete() ? BVR : this;
601 }
Chris Lattneref943742005-04-19 03:36:21 +0000602
603 if (NewRec != Rec) {
604 dump();
605 NewRec->dump(); std::cerr << "\n";
606 return new FieldInit(NewRec, FieldName);
607 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000608 return this;
609}
610
Chris Lattner0d3ef402006-01-31 06:02:35 +0000611Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
612 std::vector<Init*> NewArgs;
613 for (unsigned i = 0, e = Args.size(); i != e; ++i)
614 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
615
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000616 Init *Op = Val->resolveReferences(R, RV);
617
618 if (Args != NewArgs || Op != Val)
619 return new DagInit(Op, NewArgs, ArgNames);
Chris Lattner0d3ef402006-01-31 06:02:35 +0000620
621 return this;
622}
623
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000624
625void DagInit::print(std::ostream &OS) const {
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000626 OS << "(" << *Val;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000627 if (Args.size()) {
628 OS << " " << *Args[0];
629 if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
630 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
631 OS << ", " << *Args[i];
632 if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
633 }
634 }
635 OS << ")";
636}
637
638
639//===----------------------------------------------------------------------===//
640// Other implementations
641//===----------------------------------------------------------------------===//
642
643RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
644 : Name(N), Ty(T), Prefix(P) {
645 Value = Ty->convertValue(new UnsetInit());
646 assert(Value && "Cannot create unset value for current type!");
647}
648
649void RecordVal::dump() const { std::cerr << *this; }
650
651void RecordVal::print(std::ostream &OS, bool PrintSem) const {
652 if (getPrefix()) OS << "field ";
653 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +0000654
655 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000656 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +0000657
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000658 if (PrintSem) OS << ";\n";
659}
660
Chris Lattnerac284252005-08-19 17:58:11 +0000661void Record::setName(const std::string &Name) {
662 if (Records.getDef(getName()) == this) {
663 Records.removeDef(getName());
664 this->Name = Name;
665 Records.addDef(this);
666 } else {
667 Records.removeClass(getName());
668 this->Name = Name;
669 Records.addClass(this);
670 }
671}
672
Chris Lattneref943742005-04-19 03:36:21 +0000673/// resolveReferencesTo - If anything in this record refers to RV, replace the
674/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
675/// references.
676void Record::resolveReferencesTo(const RecordVal *RV) {
677 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
678 if (Init *V = Values[i].getValue())
679 Values[i].setValue(V->resolveReferences(*this, RV));
680 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000681}
682
Chris Lattneref943742005-04-19 03:36:21 +0000683
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000684void Record::dump() const { std::cerr << *this; }
685
Chris Lattner68478662004-08-01 03:55:39 +0000686std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000687 OS << R.getName();
688
689 const std::vector<std::string> &TArgs = R.getTemplateArgs();
690 if (!TArgs.empty()) {
691 OS << "<";
692 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
693 if (i) OS << ", ";
694 const RecordVal *RV = R.getValue(TArgs[i]);
695 assert(RV && "Template argument record not found??");
696 RV->print(OS, false);
697 }
698 OS << ">";
699 }
700
701 OS << " {";
702 const std::vector<Record*> &SC = R.getSuperClasses();
703 if (!SC.empty()) {
704 OS << "\t//";
705 for (unsigned i = 0, e = SC.size(); i != e; ++i)
706 OS << " " << SC[i]->getName();
707 }
708 OS << "\n";
709
710 const std::vector<RecordVal> &Vals = R.getValues();
711 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
712 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
713 OS << Vals[i];
714 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
715 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
716 OS << Vals[i];
717
718 return OS << "}\n";
719}
720
721/// getValueInit - Return the initializer for a value with the specified name,
722/// or throw an exception if the field does not exist.
723///
724Init *Record::getValueInit(const std::string &FieldName) const {
725 const RecordVal *R = getValue(FieldName);
726 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000727 throw "Record `" + getName() + "' does not have a field named `" +
728 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000729 return R->getValue();
730}
731
732
733/// getValueAsString - This method looks up the specified field and returns its
734/// value as a string, throwing an exception if the field does not exist or if
735/// the value is not a string.
736///
737std::string Record::getValueAsString(const std::string &FieldName) const {
738 const RecordVal *R = getValue(FieldName);
739 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000740 throw "Record `" + getName() + "' does not have a field named `" +
741 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000742
743 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
744 return SI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000745 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000746 "' does not have a string initializer!";
747}
748
749/// getValueAsBitsInit - This method looks up the specified field and returns
750/// its value as a BitsInit, throwing an exception if the field does not exist
751/// or if the value is not the right type.
752///
753BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
754 const RecordVal *R = getValue(FieldName);
755 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000756 throw "Record `" + getName() + "' does not have a field named `" +
757 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000758
759 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
760 return BI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000761 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000762 "' does not have a BitsInit initializer!";
763}
764
765/// getValueAsListInit - This method looks up the specified field and returns
766/// its value as a ListInit, throwing an exception if the field does not exist
767/// or if the value is not the right type.
768///
769ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
770 const RecordVal *R = getValue(FieldName);
771 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000772 throw "Record `" + getName() + "' does not have a field named `" +
773 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000774
775 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
776 return LI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000777 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000778 "' does not have a list initializer!";
779}
780
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000781/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +0000782/// its value as a vector of records, throwing an exception if the field does
783/// not exist or if the value is not the right type.
784///
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000785std::vector<Record*>
786Record::getValueAsListOfDefs(const std::string &FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +0000787 ListInit *List = getValueAsListInit(FieldName);
788 std::vector<Record*> Defs;
789 for (unsigned i = 0; i < List->getSize(); i++) {
790 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
791 Defs.push_back(DI->getDef());
792 } else {
793 throw "Record `" + getName() + "', field `" + FieldName +
794 "' list is not entirely DefInit!";
795 }
796 }
797 return Defs;
798}
799
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000800/// getValueAsInt - This method looks up the specified field and returns its
801/// value as an int, throwing an exception if the field does not exist or if
802/// the value is not the right type.
803///
804int Record::getValueAsInt(const std::string &FieldName) const {
805 const RecordVal *R = getValue(FieldName);
806 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000807 throw "Record `" + getName() + "' does not have a field named `" +
808 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000809
810 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
811 return II->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000812 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +0000813 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000814}
815
816/// getValueAsDef - This method looks up the specified field and returns its
817/// value as a Record, throwing an exception if the field does not exist or if
818/// the value is not the right type.
819///
820Record *Record::getValueAsDef(const std::string &FieldName) const {
821 const RecordVal *R = getValue(FieldName);
822 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000823 throw "Record `" + getName() + "' does not have a field named `" +
824 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000825
826 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
827 return DI->getDef();
Misha Brukman41f9f292004-10-08 14:59:05 +0000828 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +0000829 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000830}
831
832/// getValueAsBit - This method looks up the specified field and returns its
833/// value as a bit, throwing an exception if the field does not exist or if
834/// the value is not the right type.
835///
836bool Record::getValueAsBit(const std::string &FieldName) const {
837 const RecordVal *R = getValue(FieldName);
838 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000839 throw "Record `" + getName() + "' does not have a field named `" +
840 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000841
842 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
843 return BI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000844 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000845 "' does not have a bit initializer!";
846}
847
848/// getValueAsDag - This method looks up the specified field and returns its
849/// value as an Dag, throwing an exception if the field does not exist or if
850/// the value is not the right type.
851///
852DagInit *Record::getValueAsDag(const std::string &FieldName) const {
853 const RecordVal *R = getValue(FieldName);
854 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000855 throw "Record `" + getName() + "' does not have a field named `" +
856 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000857
858 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
859 return DI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000860 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000861 "' does not have a dag initializer!";
862}
863
Chris Lattnerae939eb2005-09-13 21:44:28 +0000864std::string Record::getValueAsCode(const std::string &FieldName) const {
865 const RecordVal *R = getValue(FieldName);
866 if (R == 0 || R->getValue() == 0)
867 throw "Record `" + getName() + "' does not have a field named `" +
868 FieldName + "'!\n";
869
870 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
871 return CI->getValue();
872 throw "Record `" + getName() + "', field `" + FieldName +
873 "' does not have a code initializer!";
874}
875
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000876
877void RecordKeeper::dump() const { std::cerr << *this; }
878
Chris Lattner68478662004-08-01 03:55:39 +0000879std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000880 OS << "------------- Classes -----------------\n";
881 const std::map<std::string, Record*> &Classes = RK.getClasses();
882 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +0000883 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000884 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000885
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000886 OS << "------------- Defs -----------------\n";
887 const std::map<std::string, Record*> &Defs = RK.getDefs();
888 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +0000889 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000890 OS << "def " << *I->second;
891 return OS;
892}
893
894
895/// getAllDerivedDefinitions - This method returns all concrete definitions
896/// that derive from the specified class name. If a class with the specified
897/// name does not exist, an error is printed and true is returned.
898std::vector<Record*>
899RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
900 Record *Class = Records.getClass(ClassName);
901 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +0000902 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000903
904 std::vector<Record*> Defs;
905 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
906 E = getDefs().end(); I != E; ++I)
907 if (I->second->isSubClassOf(Class))
908 Defs.push_back(I->second);
909
910 return Defs;
911}
Brian Gaeke960707c2003-11-11 22:41:34 +0000912