blob: c5f0565749979bdf4637e1ac9b527c4a397d621d [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//
10//
11//===----------------------------------------------------------------------===//
12
13#include "Record.h"
Misha Brukman46cee7d2004-09-30 18:27:39 +000014#include "llvm/Support/DataTypes.h"
Chris Lattner68478662004-08-01 03:55:39 +000015using namespace llvm;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000016
17//===----------------------------------------------------------------------===//
18// Type implementations
19//===----------------------------------------------------------------------===//
20
21void RecTy::dump() const { print(std::cerr); }
22
23Init *BitRecTy::convertValue(BitsInit *BI) {
24 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
25 return BI->getBit(0);
26}
27
28bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
29 return RHS->getNumBits() == 1;
30}
31
32Init *BitRecTy::convertValue(IntInit *II) {
33 int Val = II->getValue();
34 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
Misha Brukman650ba8e2005-04-22 00:00:37 +000035
36 return new BitInit(Val != 0);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000037}
38
39Init *BitRecTy::convertValue(TypedInit *VI) {
40 if (dynamic_cast<BitRecTy*>(VI->getType()))
41 return VI; // Accept variable if it is already of bit type!
42 return 0;
43}
44
45Init *BitsRecTy::convertValue(UnsetInit *UI) {
46 BitsInit *Ret = new BitsInit(Size);
47
48 for (unsigned i = 0; i != Size; ++i)
49 Ret->setBit(i, new UnsetInit());
50 return Ret;
51}
52
53Init *BitsRecTy::convertValue(BitInit *UI) {
54 if (Size != 1) return 0; // Can only convert single bit...
55 BitsInit *Ret = new BitsInit(1);
56 Ret->setBit(0, UI);
57 return Ret;
58}
59
60// convertValue from Int initializer to bits type: Split the integer up into the
61// appropriate bits...
62//
63Init *BitsRecTy::convertValue(IntInit *II) {
Misha Brukman6752fb52004-06-21 18:01:47 +000064 int64_t Value = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000065 // Make sure this bitfield is large enough to hold the integer value...
66 if (Value >= 0) {
Misha Brukman6752fb52004-06-21 18:01:47 +000067 if (Value & ~((1LL << Size)-1))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000068 return 0;
69 } else {
Chris Lattner429aaa52004-11-05 04:50:59 +000070 if ((Value >> Size) != -1 || ((Value & (1 << (Size-1))) == 0))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000071 return 0;
72 }
73
74 BitsInit *Ret = new BitsInit(Size);
75 for (unsigned i = 0; i != Size; ++i)
76 Ret->setBit(i, new BitInit(Value & (1 << i)));
77
78 return Ret;
79}
80
81Init *BitsRecTy::convertValue(BitsInit *BI) {
82 // If the number of bits is right, return it. Otherwise we need to expand or
83 // truncate...
84 if (BI->getNumBits() == Size) return BI;
85 return 0;
86}
87
88Init *BitsRecTy::convertValue(TypedInit *VI) {
89 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
90 if (BRT->Size == Size) {
91 BitsInit *Ret = new BitsInit(Size);
92 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen88e7b722005-04-22 04:13:13 +000093 Ret->setBit(i, new VarBitInit(VI, i));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000094 return Ret;
95 }
96 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
97 BitsInit *Ret = new BitsInit(1);
98 Ret->setBit(0, VI);
99 return Ret;
100 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000101
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000102 return 0;
103}
104
105Init *IntRecTy::convertValue(BitInit *BI) {
106 return new IntInit(BI->getValue());
107}
108
109Init *IntRecTy::convertValue(BitsInit *BI) {
110 int Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000111 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000112 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
113 Result |= Bit->getValue() << i;
114 } else {
115 return 0;
116 }
117 return new IntInit(Result);
118}
119
120Init *IntRecTy::convertValue(TypedInit *TI) {
121 if (TI->getType()->typeIsConvertibleTo(this))
122 return TI; // Accept variable if already of the right type!
123 return 0;
124}
125
126Init *StringRecTy::convertValue(TypedInit *TI) {
127 if (dynamic_cast<StringRecTy*>(TI->getType()))
128 return TI; // Accept variable if already of the right type!
129 return 0;
130}
131
132void ListRecTy::print(std::ostream &OS) const {
133 OS << "list<" << *Ty << ">";
134}
135
136Init *ListRecTy::convertValue(ListInit *LI) {
137 std::vector<Init*> Elements;
138
139 // Verify that all of the elements of the list are subclasses of the
140 // appropriate class!
141 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
142 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
143 Elements.push_back(CI);
144 else
145 return 0;
146
147 return new ListInit(Elements);
148}
149
150Init *ListRecTy::convertValue(TypedInit *TI) {
151 // Ensure that TI is compatible with our class.
152 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
153 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
154 return TI;
155 return 0;
156}
157
158Init *CodeRecTy::convertValue(TypedInit *TI) {
159 if (TI->getType()->typeIsConvertibleTo(this))
160 return TI;
161 return 0;
162}
163
164Init *DagRecTy::convertValue(TypedInit *TI) {
165 if (TI->getType()->typeIsConvertibleTo(this))
166 return TI;
167 return 0;
168}
169
170
171void RecordRecTy::print(std::ostream &OS) const {
172 OS << Rec->getName();
173}
174
175Init *RecordRecTy::convertValue(DefInit *DI) {
176 // Ensure that DI is a subclass of Rec.
177 if (!DI->getDef()->isSubClassOf(Rec))
178 return 0;
179 return DI;
180}
181
182Init *RecordRecTy::convertValue(TypedInit *TI) {
183 // Ensure that TI is compatible with Rec.
184 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
185 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
186 RRT->getRecord() == getRecord())
187 return TI;
188 return 0;
189}
190
191bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
192 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
193}
194
195
196//===----------------------------------------------------------------------===//
197// Initializer implementations
198//===----------------------------------------------------------------------===//
199
200void Init::dump() const { return print(std::cerr); }
201
202Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
203 BitsInit *BI = new BitsInit(Bits.size());
204 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
205 if (Bits[i] >= getNumBits()) {
206 delete BI;
207 return 0;
208 }
209 BI->setBit(i, getBit(Bits[i]));
210 }
211 return BI;
212}
213
214void BitsInit::print(std::ostream &OS) const {
215 //if (!printInHex(OS)) return;
216 //if (!printAsVariable(OS)) return;
217 //if (!printAsUnset(OS)) return;
218
219 OS << "{ ";
220 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
221 if (i) OS << ", ";
222 if (Init *Bit = getBit(e-i-1))
223 Bit->print(OS);
224 else
225 OS << "*";
226 }
227 OS << " }";
228}
229
230bool BitsInit::printInHex(std::ostream &OS) const {
231 // First, attempt to convert the value into an integer value...
232 int Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000233 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000234 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
235 Result |= Bit->getValue() << i;
236 } else {
237 return true;
238 }
239
240 OS << "0x" << std::hex << Result << std::dec;
241 return false;
242}
243
244bool BitsInit::printAsVariable(std::ostream &OS) const {
245 // Get the variable that we may be set equal to...
246 assert(getNumBits() != 0);
247 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
248 if (FirstBit == 0) return true;
249 TypedInit *Var = FirstBit->getVariable();
250
251 // Check to make sure the types are compatible.
252 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
253 if (Ty == 0) return true;
254 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
255
256 // Check to make sure all bits are referring to the right bits in the variable
257 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
258 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
259 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
260 return true;
261 }
262
263 Var->print(OS);
264 return false;
265}
266
267bool BitsInit::printAsUnset(std::ostream &OS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000268 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000269 if (!dynamic_cast<UnsetInit*>(getBit(i)))
270 return true;
271 OS << "?";
272 return false;
273}
274
275// resolveReferences - If there are any field references that refer to fields
276// that have been filled in, we can propagate the values now.
277//
Chris Lattneref943742005-04-19 03:36:21 +0000278Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000279 bool Changed = false;
280 BitsInit *New = new BitsInit(getNumBits());
281
282 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
283 Init *B;
284 Init *CurBit = getBit(i);
285
286 do {
287 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000288 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000289 Changed |= B != CurBit;
290 } while (B != CurBit);
291 New->setBit(i, CurBit);
292 }
293
294 if (Changed)
295 return New;
296 delete New;
297 return this;
298}
299
Chris Lattner3ff0e112005-04-19 01:17:35 +0000300Init *IntInit::getBinaryOp(BinaryOp Op, Init *RHS) {
301 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
302 if (RHSi == 0) return 0;
303
304 int NewValue;
305 switch (Op) {
Chris Lattnerb1748302005-04-19 15:32:30 +0000306 default: assert(0 && "Unknown binop");
Chris Lattner3ff0e112005-04-19 01:17:35 +0000307 case SHL: NewValue = Value << RHSi->getValue(); break;
308 case SRA: NewValue = Value >> RHSi->getValue(); break;
309 case SRL: NewValue = (unsigned)Value >> (unsigned)RHSi->getValue(); break;
310 }
311 return new IntInit(NewValue);
312}
313
314
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000315Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
316 BitsInit *BI = new BitsInit(Bits.size());
317
318 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
319 if (Bits[i] >= 32) {
320 delete BI;
321 return 0;
322 }
323 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
324 }
325 return BI;
326}
327
Chris Lattner8bf9e062004-07-26 23:21:34 +0000328Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
329 std::vector<Init*> Vals;
330 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
331 if (Elements[i] >= getSize())
332 return 0;
333 Vals.push_back(getElement(Elements[i]));
334 }
335 return new ListInit(Vals);
336}
337
Chris Lattneref943742005-04-19 03:36:21 +0000338Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000339 std::vector<Init*> Resolved;
340 Resolved.reserve(getSize());
341 bool Changed = false;
342
343 for (unsigned i = 0, e = getSize(); i != e; ++i) {
344 Init *E;
345 Init *CurElt = getElement(i);
346
347 do {
348 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000349 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000350 Changed |= E != CurElt;
351 } while (E != CurElt);
352 Resolved.push_back(E);
353 }
354
355 if (Changed)
356 return new ListInit(Resolved);
357 return this;
358}
359
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000360void ListInit::print(std::ostream &OS) const {
361 OS << "[";
362 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
363 if (i) OS << ", ";
364 OS << *Values[i];
365 }
366 OS << "]";
367}
368
Chris Lattner577fc3f2004-07-27 01:01:21 +0000369Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000370 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
371 if (T == 0) return 0; // Cannot subscript a non-bits variable...
372 unsigned NumBits = T->getNumBits();
373
374 BitsInit *BI = new BitsInit(Bits.size());
375 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
376 if (Bits[i] >= NumBits) {
377 delete BI;
378 return 0;
379 }
380 BI->setBit(i, new VarBitInit(this, Bits[i]));
381 }
382 return BI;
383}
384
Chris Lattner577fc3f2004-07-27 01:01:21 +0000385Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
386 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
387 if (T == 0) return 0; // Cannot subscript a non-list variable...
388
389 if (Elements.size() == 1)
390 return new VarListElementInit(this, Elements[0]);
391
392 std::vector<Init*> ListInits;
393 ListInits.reserve(Elements.size());
394 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
395 ListInits.push_back(new VarListElementInit(this, Elements[i]));
396 return new ListInit(ListInits);
397}
398
399
Chris Lattneref943742005-04-19 03:36:21 +0000400Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
401 unsigned Bit) {
402 if (R.isTemplateArg(getName())) return 0;
403 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000404
405 RecordVal *RV = R.getValue(getName());
406 assert(RV && "Reference to a non-existant variable?");
407 assert(dynamic_cast<BitsInit*>(RV->getValue()));
408 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +0000409
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000410 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
411 Init *B = BI->getBit(Bit);
412
413 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
414 return B; // Replace the VarBitInit with it.
Chris Lattner577fc3f2004-07-27 01:01:21 +0000415 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000416}
417
Chris Lattneref943742005-04-19 03:36:21 +0000418Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
419 unsigned Elt) {
420 if (R.isTemplateArg(getName())) return 0;
421 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000422
423 RecordVal *RV = R.getValue(getName());
424 assert(RV && "Reference to a non-existant variable?");
425 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
426 assert(LI && "Invalid list element!");
427
428 if (Elt >= LI->getSize())
429 return 0; // Out of range reference.
430 Init *E = LI->getElement(Elt);
431 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
432 return E; // Replace the VarListElementInit with it.
433 return 0;
434}
435
436
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000437RecTy *VarInit::getFieldType(const std::string &FieldName) const {
438 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
439 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
440 return RV->getType();
441 return 0;
442}
443
444Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
445 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
Chris Lattnerd959ab92004-02-28 16:31:53 +0000446 if (const RecordVal *RV = R.getValue(VarName)) {
447 Init *TheInit = RV->getValue();
448 assert(TheInit != this && "Infinite loop detected!");
449 if (Init *I = TheInit->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000450 return I;
451 else
452 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +0000453 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000454 return 0;
455}
456
457/// resolveReferences - This method is used by classes that refer to other
458/// variables which may not be defined at the time they expression is formed.
459/// If a value is set for the variable later, this method will be called on
460/// users of the value to allow the value to propagate out.
461///
Chris Lattneref943742005-04-19 03:36:21 +0000462Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000463 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +0000464 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000465 return Val->getValue();
466 return this;
467}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000468
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000469
Chris Lattneref943742005-04-19 03:36:21 +0000470Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
471 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000472 return I;
473 return this;
474}
475
Chris Lattneref943742005-04-19 03:36:21 +0000476Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
477 if (Init *I = getVariable()->resolveListElementReference(R, RV,
478 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +0000479 return I;
480 return this;
481}
482
Chris Lattneref943742005-04-19 03:36:21 +0000483Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
484 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000485 // FIXME: This should be implemented, to support references like:
486 // bit B = AA[0]{1};
487 return 0;
488}
489
Chris Lattneref943742005-04-19 03:36:21 +0000490Init *VarListElementInit::
491resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000492 // FIXME: This should be implemented, to support references like:
493 // int B = AA[0][1];
494 return 0;
495}
496
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000497RecTy *DefInit::getFieldType(const std::string &FieldName) const {
498 if (const RecordVal *RV = Def->getValue(FieldName))
499 return RV->getType();
500 return 0;
501}
502
503Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
504 return Def->getValue(FieldName)->getValue();
505}
506
507
508void DefInit::print(std::ostream &OS) const {
509 OS << Def->getName();
510}
511
Chris Lattneref943742005-04-19 03:36:21 +0000512Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
513 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000514 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000515 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
516 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
517 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000518
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000519 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
520 return B; // Replace the VarBitInit with it.
521 }
Chris Lattner577fc3f2004-07-27 01:01:21 +0000522 return 0;
523}
524
Chris Lattneref943742005-04-19 03:36:21 +0000525Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
526 unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000527 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
528 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
529 if (Elt >= LI->getSize()) return 0;
530 Init *E = LI->getElement(Elt);
531
532 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
533 return E; // Replace the VarListElementInit with it.
534 }
535 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000536}
537
Chris Lattneref943742005-04-19 03:36:21 +0000538Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
539 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
540
541 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000542 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +0000543 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000544 return BVR->isComplete() ? BVR : this;
545 }
Chris Lattneref943742005-04-19 03:36:21 +0000546
547 if (NewRec != Rec) {
548 dump();
549 NewRec->dump(); std::cerr << "\n";
550 return new FieldInit(NewRec, FieldName);
551 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000552 return this;
553}
554
555
556void DagInit::print(std::ostream &OS) const {
557 OS << "(" << NodeTypeDef->getName();
558 if (Args.size()) {
559 OS << " " << *Args[0];
560 if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
561 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
562 OS << ", " << *Args[i];
563 if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
564 }
565 }
566 OS << ")";
567}
568
569
570//===----------------------------------------------------------------------===//
571// Other implementations
572//===----------------------------------------------------------------------===//
573
574RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
575 : Name(N), Ty(T), Prefix(P) {
576 Value = Ty->convertValue(new UnsetInit());
577 assert(Value && "Cannot create unset value for current type!");
578}
579
580void RecordVal::dump() const { std::cerr << *this; }
581
582void RecordVal::print(std::ostream &OS, bool PrintSem) const {
583 if (getPrefix()) OS << "field ";
584 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +0000585
586 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000587 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +0000588
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000589 if (PrintSem) OS << ";\n";
590}
591
Chris Lattnerac284252005-08-19 17:58:11 +0000592void Record::setName(const std::string &Name) {
593 if (Records.getDef(getName()) == this) {
594 Records.removeDef(getName());
595 this->Name = Name;
596 Records.addDef(this);
597 } else {
598 Records.removeClass(getName());
599 this->Name = Name;
600 Records.addClass(this);
601 }
602}
603
Chris Lattneref943742005-04-19 03:36:21 +0000604/// resolveReferencesTo - If anything in this record refers to RV, replace the
605/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
606/// references.
607void Record::resolveReferencesTo(const RecordVal *RV) {
608 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
609 if (Init *V = Values[i].getValue())
610 Values[i].setValue(V->resolveReferences(*this, RV));
611 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000612}
613
Chris Lattneref943742005-04-19 03:36:21 +0000614
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000615void Record::dump() const { std::cerr << *this; }
616
Chris Lattner68478662004-08-01 03:55:39 +0000617std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000618 OS << R.getName();
619
620 const std::vector<std::string> &TArgs = R.getTemplateArgs();
621 if (!TArgs.empty()) {
622 OS << "<";
623 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
624 if (i) OS << ", ";
625 const RecordVal *RV = R.getValue(TArgs[i]);
626 assert(RV && "Template argument record not found??");
627 RV->print(OS, false);
628 }
629 OS << ">";
630 }
631
632 OS << " {";
633 const std::vector<Record*> &SC = R.getSuperClasses();
634 if (!SC.empty()) {
635 OS << "\t//";
636 for (unsigned i = 0, e = SC.size(); i != e; ++i)
637 OS << " " << SC[i]->getName();
638 }
639 OS << "\n";
640
641 const std::vector<RecordVal> &Vals = R.getValues();
642 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
643 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
644 OS << Vals[i];
645 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
646 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
647 OS << Vals[i];
648
649 return OS << "}\n";
650}
651
652/// getValueInit - Return the initializer for a value with the specified name,
653/// or throw an exception if the field does not exist.
654///
655Init *Record::getValueInit(const std::string &FieldName) const {
656 const RecordVal *R = getValue(FieldName);
657 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000658 throw "Record `" + getName() + "' does not have a field named `" +
659 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000660 return R->getValue();
661}
662
663
664/// getValueAsString - This method looks up the specified field and returns its
665/// value as a string, throwing an exception if the field does not exist or if
666/// the value is not a string.
667///
668std::string Record::getValueAsString(const std::string &FieldName) const {
669 const RecordVal *R = getValue(FieldName);
670 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000671 throw "Record `" + getName() + "' does not have a field named `" +
672 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000673
674 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
675 return SI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000676 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000677 "' does not have a string initializer!";
678}
679
680/// getValueAsBitsInit - This method looks up the specified field and returns
681/// its value as a BitsInit, throwing an exception if the field does not exist
682/// or if the value is not the right type.
683///
684BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
685 const RecordVal *R = getValue(FieldName);
686 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000687 throw "Record `" + getName() + "' does not have a field named `" +
688 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000689
690 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
691 return BI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000692 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000693 "' does not have a BitsInit initializer!";
694}
695
696/// getValueAsListInit - This method looks up the specified field and returns
697/// its value as a ListInit, throwing an exception if the field does not exist
698/// or if the value is not the right type.
699///
700ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
701 const RecordVal *R = getValue(FieldName);
702 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000703 throw "Record `" + getName() + "' does not have a field named `" +
704 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000705
706 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
707 return LI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000708 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000709 "' does not have a list initializer!";
710}
711
712/// getValueAsInt - This method looks up the specified field and returns its
713/// value as an int, throwing an exception if the field does not exist or if
714/// the value is not the right type.
715///
716int Record::getValueAsInt(const std::string &FieldName) const {
717 const RecordVal *R = getValue(FieldName);
718 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000719 throw "Record `" + getName() + "' does not have a field named `" +
720 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000721
722 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
723 return II->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000724 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000725 "' does not have a list initializer!";
726}
727
728/// getValueAsDef - This method looks up the specified field and returns its
729/// value as a Record, throwing an exception if the field does not exist or if
730/// the value is not the right type.
731///
732Record *Record::getValueAsDef(const std::string &FieldName) const {
733 const RecordVal *R = getValue(FieldName);
734 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000735 throw "Record `" + getName() + "' does not have a field named `" +
736 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000737
738 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
739 return DI->getDef();
Misha Brukman41f9f292004-10-08 14:59:05 +0000740 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000741 "' does not have a list initializer!";
742}
743
744/// getValueAsBit - This method looks up the specified field and returns its
745/// value as a bit, throwing an exception if the field does not exist or if
746/// the value is not the right type.
747///
748bool Record::getValueAsBit(const std::string &FieldName) const {
749 const RecordVal *R = getValue(FieldName);
750 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000751 throw "Record `" + getName() + "' does not have a field named `" +
752 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000753
754 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
755 return BI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000756 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000757 "' does not have a bit initializer!";
758}
759
760/// getValueAsDag - This method looks up the specified field and returns its
761/// value as an Dag, throwing an exception if the field does not exist or if
762/// the value is not the right type.
763///
764DagInit *Record::getValueAsDag(const std::string &FieldName) const {
765 const RecordVal *R = getValue(FieldName);
766 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000767 throw "Record `" + getName() + "' does not have a field named `" +
768 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000769
770 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
771 return DI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000772 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000773 "' does not have a dag initializer!";
774}
775
Chris Lattnerae939eb2005-09-13 21:44:28 +0000776std::string Record::getValueAsCode(const std::string &FieldName) const {
777 const RecordVal *R = getValue(FieldName);
778 if (R == 0 || R->getValue() == 0)
779 throw "Record `" + getName() + "' does not have a field named `" +
780 FieldName + "'!\n";
781
782 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
783 return CI->getValue();
784 throw "Record `" + getName() + "', field `" + FieldName +
785 "' does not have a code initializer!";
786}
787
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000788
789void RecordKeeper::dump() const { std::cerr << *this; }
790
Chris Lattner68478662004-08-01 03:55:39 +0000791std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000792 OS << "------------- Classes -----------------\n";
793 const std::map<std::string, Record*> &Classes = RK.getClasses();
794 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +0000795 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000796 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000797
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000798 OS << "------------- Defs -----------------\n";
799 const std::map<std::string, Record*> &Defs = RK.getDefs();
800 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +0000801 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000802 OS << "def " << *I->second;
803 return OS;
804}
805
806
807/// getAllDerivedDefinitions - This method returns all concrete definitions
808/// that derive from the specified class name. If a class with the specified
809/// name does not exist, an error is printed and true is returned.
810std::vector<Record*>
811RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
812 Record *Class = Records.getClass(ClassName);
813 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +0000814 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000815
816 std::vector<Record*> Defs;
817 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
818 E = getDefs().end(); I != E; ++I)
819 if (I->second->isSubClassOf(Class))
820 Defs.push_back(I->second);
821
822 return Defs;
823}
Brian Gaeke960707c2003-11-11 22:41:34 +0000824