blob: 51014813b2966e7acba61e41b903d539e47002ec [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"
Duraid Madina14492af2005-12-26 05:08:55 +000015#include <ios>
16
Chris Lattner68478662004-08-01 03:55:39 +000017using namespace llvm;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000018
19//===----------------------------------------------------------------------===//
20// Type implementations
21//===----------------------------------------------------------------------===//
22
23void RecTy::dump() const { print(std::cerr); }
24
25Init *BitRecTy::convertValue(BitsInit *BI) {
26 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
27 return BI->getBit(0);
28}
29
30bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
31 return RHS->getNumBits() == 1;
32}
33
34Init *BitRecTy::convertValue(IntInit *II) {
35 int Val = II->getValue();
36 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
Misha Brukman650ba8e2005-04-22 00:00:37 +000037
38 return new BitInit(Val != 0);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000039}
40
41Init *BitRecTy::convertValue(TypedInit *VI) {
42 if (dynamic_cast<BitRecTy*>(VI->getType()))
43 return VI; // Accept variable if it is already of bit type!
44 return 0;
45}
46
47Init *BitsRecTy::convertValue(UnsetInit *UI) {
48 BitsInit *Ret = new BitsInit(Size);
49
50 for (unsigned i = 0; i != Size; ++i)
51 Ret->setBit(i, new UnsetInit());
52 return Ret;
53}
54
55Init *BitsRecTy::convertValue(BitInit *UI) {
56 if (Size != 1) return 0; // Can only convert single bit...
57 BitsInit *Ret = new BitsInit(1);
58 Ret->setBit(0, UI);
59 return Ret;
60}
61
62// convertValue from Int initializer to bits type: Split the integer up into the
63// appropriate bits...
64//
65Init *BitsRecTy::convertValue(IntInit *II) {
Misha Brukman6752fb52004-06-21 18:01:47 +000066 int64_t Value = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000067 // Make sure this bitfield is large enough to hold the integer value...
68 if (Value >= 0) {
Misha Brukman6752fb52004-06-21 18:01:47 +000069 if (Value & ~((1LL << Size)-1))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000070 return 0;
71 } else {
Chris Lattner429aaa52004-11-05 04:50:59 +000072 if ((Value >> Size) != -1 || ((Value & (1 << (Size-1))) == 0))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000073 return 0;
74 }
75
76 BitsInit *Ret = new BitsInit(Size);
77 for (unsigned i = 0; i != Size; ++i)
78 Ret->setBit(i, new BitInit(Value & (1 << i)));
79
80 return Ret;
81}
82
83Init *BitsRecTy::convertValue(BitsInit *BI) {
84 // If the number of bits is right, return it. Otherwise we need to expand or
85 // truncate...
86 if (BI->getNumBits() == Size) return BI;
87 return 0;
88}
89
90Init *BitsRecTy::convertValue(TypedInit *VI) {
91 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
92 if (BRT->Size == Size) {
93 BitsInit *Ret = new BitsInit(Size);
94 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen88e7b722005-04-22 04:13:13 +000095 Ret->setBit(i, new VarBitInit(VI, i));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000096 return Ret;
97 }
98 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
99 BitsInit *Ret = new BitsInit(1);
100 Ret->setBit(0, VI);
101 return Ret;
102 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000103
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000104 return 0;
105}
106
107Init *IntRecTy::convertValue(BitInit *BI) {
108 return new IntInit(BI->getValue());
109}
110
111Init *IntRecTy::convertValue(BitsInit *BI) {
112 int Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000113 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000114 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
115 Result |= Bit->getValue() << i;
116 } else {
117 return 0;
118 }
119 return new IntInit(Result);
120}
121
122Init *IntRecTy::convertValue(TypedInit *TI) {
123 if (TI->getType()->typeIsConvertibleTo(this))
124 return TI; // Accept variable if already of the right type!
125 return 0;
126}
127
128Init *StringRecTy::convertValue(TypedInit *TI) {
129 if (dynamic_cast<StringRecTy*>(TI->getType()))
130 return TI; // Accept variable if already of the right type!
131 return 0;
132}
133
134void ListRecTy::print(std::ostream &OS) const {
135 OS << "list<" << *Ty << ">";
136}
137
138Init *ListRecTy::convertValue(ListInit *LI) {
139 std::vector<Init*> Elements;
140
141 // Verify that all of the elements of the list are subclasses of the
142 // appropriate class!
143 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
144 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
145 Elements.push_back(CI);
146 else
147 return 0;
148
149 return new ListInit(Elements);
150}
151
152Init *ListRecTy::convertValue(TypedInit *TI) {
153 // Ensure that TI is compatible with our class.
154 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
155 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
156 return TI;
157 return 0;
158}
159
160Init *CodeRecTy::convertValue(TypedInit *TI) {
161 if (TI->getType()->typeIsConvertibleTo(this))
162 return TI;
163 return 0;
164}
165
166Init *DagRecTy::convertValue(TypedInit *TI) {
167 if (TI->getType()->typeIsConvertibleTo(this))
168 return TI;
169 return 0;
170}
171
172
173void RecordRecTy::print(std::ostream &OS) const {
174 OS << Rec->getName();
175}
176
177Init *RecordRecTy::convertValue(DefInit *DI) {
178 // Ensure that DI is a subclass of Rec.
179 if (!DI->getDef()->isSubClassOf(Rec))
180 return 0;
181 return DI;
182}
183
184Init *RecordRecTy::convertValue(TypedInit *TI) {
185 // Ensure that TI is compatible with Rec.
186 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
187 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
188 RRT->getRecord() == getRecord())
189 return TI;
190 return 0;
191}
192
193bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
194 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
195}
196
197
198//===----------------------------------------------------------------------===//
199// Initializer implementations
200//===----------------------------------------------------------------------===//
201
202void Init::dump() const { return print(std::cerr); }
203
204Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
205 BitsInit *BI = new BitsInit(Bits.size());
206 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
207 if (Bits[i] >= getNumBits()) {
208 delete BI;
209 return 0;
210 }
211 BI->setBit(i, getBit(Bits[i]));
212 }
213 return BI;
214}
215
216void BitsInit::print(std::ostream &OS) const {
217 //if (!printInHex(OS)) return;
218 //if (!printAsVariable(OS)) return;
219 //if (!printAsUnset(OS)) return;
220
221 OS << "{ ";
222 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
223 if (i) OS << ", ";
224 if (Init *Bit = getBit(e-i-1))
225 Bit->print(OS);
226 else
227 OS << "*";
228 }
229 OS << " }";
230}
231
232bool BitsInit::printInHex(std::ostream &OS) const {
233 // First, attempt to convert the value into an integer value...
234 int Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000235 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000236 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
237 Result |= Bit->getValue() << i;
238 } else {
239 return true;
240 }
241
242 OS << "0x" << std::hex << Result << std::dec;
243 return false;
244}
245
246bool BitsInit::printAsVariable(std::ostream &OS) const {
247 // Get the variable that we may be set equal to...
248 assert(getNumBits() != 0);
249 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
250 if (FirstBit == 0) return true;
251 TypedInit *Var = FirstBit->getVariable();
252
253 // Check to make sure the types are compatible.
254 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
255 if (Ty == 0) return true;
256 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
257
258 // Check to make sure all bits are referring to the right bits in the variable
259 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
260 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
261 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
262 return true;
263 }
264
265 Var->print(OS);
266 return false;
267}
268
269bool BitsInit::printAsUnset(std::ostream &OS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000270 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000271 if (!dynamic_cast<UnsetInit*>(getBit(i)))
272 return true;
273 OS << "?";
274 return false;
275}
276
277// resolveReferences - If there are any field references that refer to fields
278// that have been filled in, we can propagate the values now.
279//
Chris Lattneref943742005-04-19 03:36:21 +0000280Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000281 bool Changed = false;
282 BitsInit *New = new BitsInit(getNumBits());
283
284 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
285 Init *B;
286 Init *CurBit = getBit(i);
287
288 do {
289 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000290 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000291 Changed |= B != CurBit;
292 } while (B != CurBit);
293 New->setBit(i, CurBit);
294 }
295
296 if (Changed)
297 return New;
298 delete New;
299 return this;
300}
301
Chris Lattner3ff0e112005-04-19 01:17:35 +0000302Init *IntInit::getBinaryOp(BinaryOp Op, Init *RHS) {
303 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
304 if (RHSi == 0) return 0;
305
306 int NewValue;
307 switch (Op) {
Chris Lattnerb1748302005-04-19 15:32:30 +0000308 default: assert(0 && "Unknown binop");
Chris Lattner3ff0e112005-04-19 01:17:35 +0000309 case SHL: NewValue = Value << RHSi->getValue(); break;
310 case SRA: NewValue = Value >> RHSi->getValue(); break;
311 case SRL: NewValue = (unsigned)Value >> (unsigned)RHSi->getValue(); break;
312 }
313 return new IntInit(NewValue);
314}
315
316
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000317Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
318 BitsInit *BI = new BitsInit(Bits.size());
319
320 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
321 if (Bits[i] >= 32) {
322 delete BI;
323 return 0;
324 }
325 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
326 }
327 return BI;
328}
329
Chris Lattner8bf9e062004-07-26 23:21:34 +0000330Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
331 std::vector<Init*> Vals;
332 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
333 if (Elements[i] >= getSize())
334 return 0;
335 Vals.push_back(getElement(Elements[i]));
336 }
337 return new ListInit(Vals);
338}
339
Chris Lattneref943742005-04-19 03:36:21 +0000340Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000341 std::vector<Init*> Resolved;
342 Resolved.reserve(getSize());
343 bool Changed = false;
344
345 for (unsigned i = 0, e = getSize(); i != e; ++i) {
346 Init *E;
347 Init *CurElt = getElement(i);
348
349 do {
350 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000351 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000352 Changed |= E != CurElt;
353 } while (E != CurElt);
354 Resolved.push_back(E);
355 }
356
357 if (Changed)
358 return new ListInit(Resolved);
359 return this;
360}
361
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000362void ListInit::print(std::ostream &OS) const {
363 OS << "[";
364 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
365 if (i) OS << ", ";
366 OS << *Values[i];
367 }
368 OS << "]";
369}
370
Chris Lattner577fc3f2004-07-27 01:01:21 +0000371Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000372 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
373 if (T == 0) return 0; // Cannot subscript a non-bits variable...
374 unsigned NumBits = T->getNumBits();
375
376 BitsInit *BI = new BitsInit(Bits.size());
377 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
378 if (Bits[i] >= NumBits) {
379 delete BI;
380 return 0;
381 }
382 BI->setBit(i, new VarBitInit(this, Bits[i]));
383 }
384 return BI;
385}
386
Chris Lattner577fc3f2004-07-27 01:01:21 +0000387Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
388 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
389 if (T == 0) return 0; // Cannot subscript a non-list variable...
390
391 if (Elements.size() == 1)
392 return new VarListElementInit(this, Elements[0]);
393
394 std::vector<Init*> ListInits;
395 ListInits.reserve(Elements.size());
396 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
397 ListInits.push_back(new VarListElementInit(this, Elements[i]));
398 return new ListInit(ListInits);
399}
400
401
Chris Lattneref943742005-04-19 03:36:21 +0000402Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
403 unsigned Bit) {
404 if (R.isTemplateArg(getName())) return 0;
405 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000406
407 RecordVal *RV = R.getValue(getName());
408 assert(RV && "Reference to a non-existant variable?");
409 assert(dynamic_cast<BitsInit*>(RV->getValue()));
410 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +0000411
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000412 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
413 Init *B = BI->getBit(Bit);
414
415 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
416 return B; // Replace the VarBitInit with it.
Chris Lattner577fc3f2004-07-27 01:01:21 +0000417 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000418}
419
Chris Lattneref943742005-04-19 03:36:21 +0000420Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
421 unsigned Elt) {
422 if (R.isTemplateArg(getName())) return 0;
423 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000424
425 RecordVal *RV = R.getValue(getName());
426 assert(RV && "Reference to a non-existant variable?");
427 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
428 assert(LI && "Invalid list element!");
429
430 if (Elt >= LI->getSize())
431 return 0; // Out of range reference.
432 Init *E = LI->getElement(Elt);
433 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
434 return E; // Replace the VarListElementInit with it.
435 return 0;
436}
437
438
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000439RecTy *VarInit::getFieldType(const std::string &FieldName) const {
440 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
441 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
442 return RV->getType();
443 return 0;
444}
445
446Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
447 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
Chris Lattnerd959ab92004-02-28 16:31:53 +0000448 if (const RecordVal *RV = R.getValue(VarName)) {
449 Init *TheInit = RV->getValue();
450 assert(TheInit != this && "Infinite loop detected!");
451 if (Init *I = TheInit->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000452 return I;
453 else
454 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +0000455 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000456 return 0;
457}
458
459/// resolveReferences - This method is used by classes that refer to other
460/// variables which may not be defined at the time they expression is formed.
461/// If a value is set for the variable later, this method will be called on
462/// users of the value to allow the value to propagate out.
463///
Chris Lattneref943742005-04-19 03:36:21 +0000464Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000465 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +0000466 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000467 return Val->getValue();
468 return this;
469}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000470
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000471
Chris Lattneref943742005-04-19 03:36:21 +0000472Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
473 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000474 return I;
475 return this;
476}
477
Chris Lattneref943742005-04-19 03:36:21 +0000478Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
479 if (Init *I = getVariable()->resolveListElementReference(R, RV,
480 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +0000481 return I;
482 return this;
483}
484
Chris Lattneref943742005-04-19 03:36:21 +0000485Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
486 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000487 // FIXME: This should be implemented, to support references like:
488 // bit B = AA[0]{1};
489 return 0;
490}
491
Chris Lattneref943742005-04-19 03:36:21 +0000492Init *VarListElementInit::
493resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000494 // FIXME: This should be implemented, to support references like:
495 // int B = AA[0][1];
496 return 0;
497}
498
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000499RecTy *DefInit::getFieldType(const std::string &FieldName) const {
500 if (const RecordVal *RV = Def->getValue(FieldName))
501 return RV->getType();
502 return 0;
503}
504
505Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
506 return Def->getValue(FieldName)->getValue();
507}
508
509
510void DefInit::print(std::ostream &OS) const {
511 OS << Def->getName();
512}
513
Chris Lattneref943742005-04-19 03:36:21 +0000514Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
515 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000516 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000517 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
518 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
519 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000520
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000521 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
522 return B; // Replace the VarBitInit with it.
523 }
Chris Lattner577fc3f2004-07-27 01:01:21 +0000524 return 0;
525}
526
Chris Lattneref943742005-04-19 03:36:21 +0000527Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
528 unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000529 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
530 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
531 if (Elt >= LI->getSize()) return 0;
532 Init *E = LI->getElement(Elt);
533
534 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
535 return E; // Replace the VarListElementInit with it.
536 }
537 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000538}
539
Chris Lattneref943742005-04-19 03:36:21 +0000540Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
541 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
542
543 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000544 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +0000545 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000546 return BVR->isComplete() ? BVR : this;
547 }
Chris Lattneref943742005-04-19 03:36:21 +0000548
549 if (NewRec != Rec) {
550 dump();
551 NewRec->dump(); std::cerr << "\n";
552 return new FieldInit(NewRec, FieldName);
553 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000554 return this;
555}
556
Chris Lattner0d3ef402006-01-31 06:02:35 +0000557Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
558 std::vector<Init*> NewArgs;
559 for (unsigned i = 0, e = Args.size(); i != e; ++i)
560 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
561
562 if (Args != NewArgs)
563 return new DagInit(NodeTypeDef, NewArgs, ArgNames);
564
565 return this;
566}
567
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000568
569void DagInit::print(std::ostream &OS) const {
570 OS << "(" << NodeTypeDef->getName();
571 if (Args.size()) {
572 OS << " " << *Args[0];
573 if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
574 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
575 OS << ", " << *Args[i];
576 if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
577 }
578 }
579 OS << ")";
580}
581
582
583//===----------------------------------------------------------------------===//
584// Other implementations
585//===----------------------------------------------------------------------===//
586
587RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
588 : Name(N), Ty(T), Prefix(P) {
589 Value = Ty->convertValue(new UnsetInit());
590 assert(Value && "Cannot create unset value for current type!");
591}
592
593void RecordVal::dump() const { std::cerr << *this; }
594
595void RecordVal::print(std::ostream &OS, bool PrintSem) const {
596 if (getPrefix()) OS << "field ";
597 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +0000598
599 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000600 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +0000601
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000602 if (PrintSem) OS << ";\n";
603}
604
Chris Lattnerac284252005-08-19 17:58:11 +0000605void Record::setName(const std::string &Name) {
606 if (Records.getDef(getName()) == this) {
607 Records.removeDef(getName());
608 this->Name = Name;
609 Records.addDef(this);
610 } else {
611 Records.removeClass(getName());
612 this->Name = Name;
613 Records.addClass(this);
614 }
615}
616
Chris Lattneref943742005-04-19 03:36:21 +0000617/// resolveReferencesTo - If anything in this record refers to RV, replace the
618/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
619/// references.
620void Record::resolveReferencesTo(const RecordVal *RV) {
621 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
622 if (Init *V = Values[i].getValue())
623 Values[i].setValue(V->resolveReferences(*this, RV));
624 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000625}
626
Chris Lattneref943742005-04-19 03:36:21 +0000627
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000628void Record::dump() const { std::cerr << *this; }
629
Chris Lattner68478662004-08-01 03:55:39 +0000630std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000631 OS << R.getName();
632
633 const std::vector<std::string> &TArgs = R.getTemplateArgs();
634 if (!TArgs.empty()) {
635 OS << "<";
636 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
637 if (i) OS << ", ";
638 const RecordVal *RV = R.getValue(TArgs[i]);
639 assert(RV && "Template argument record not found??");
640 RV->print(OS, false);
641 }
642 OS << ">";
643 }
644
645 OS << " {";
646 const std::vector<Record*> &SC = R.getSuperClasses();
647 if (!SC.empty()) {
648 OS << "\t//";
649 for (unsigned i = 0, e = SC.size(); i != e; ++i)
650 OS << " " << SC[i]->getName();
651 }
652 OS << "\n";
653
654 const std::vector<RecordVal> &Vals = R.getValues();
655 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
656 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
657 OS << Vals[i];
658 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
659 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
660 OS << Vals[i];
661
662 return OS << "}\n";
663}
664
665/// getValueInit - Return the initializer for a value with the specified name,
666/// or throw an exception if the field does not exist.
667///
668Init *Record::getValueInit(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 return R->getValue();
674}
675
676
677/// getValueAsString - This method looks up the specified field and returns its
678/// value as a string, throwing an exception if the field does not exist or if
679/// the value is not a string.
680///
681std::string Record::getValueAsString(const std::string &FieldName) const {
682 const RecordVal *R = getValue(FieldName);
683 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000684 throw "Record `" + getName() + "' does not have a field named `" +
685 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000686
687 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
688 return SI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000689 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000690 "' does not have a string initializer!";
691}
692
693/// getValueAsBitsInit - This method looks up the specified field and returns
694/// its value as a BitsInit, throwing an exception if the field does not exist
695/// or if the value is not the right type.
696///
697BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
698 const RecordVal *R = getValue(FieldName);
699 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000700 throw "Record `" + getName() + "' does not have a field named `" +
701 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000702
703 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
704 return BI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000705 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000706 "' does not have a BitsInit initializer!";
707}
708
709/// getValueAsListInit - This method looks up the specified field and returns
710/// its value as a ListInit, throwing an exception if the field does not exist
711/// or if the value is not the right type.
712///
713ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
714 const RecordVal *R = getValue(FieldName);
715 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000716 throw "Record `" + getName() + "' does not have a field named `" +
717 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000718
719 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
720 return LI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000721 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000722 "' does not have a list initializer!";
723}
724
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000725/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +0000726/// its value as a vector of records, throwing an exception if the field does
727/// not exist or if the value is not the right type.
728///
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000729std::vector<Record*>
730Record::getValueAsListOfDefs(const std::string &FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +0000731 ListInit *List = getValueAsListInit(FieldName);
732 std::vector<Record*> Defs;
733 for (unsigned i = 0; i < List->getSize(); i++) {
734 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
735 Defs.push_back(DI->getDef());
736 } else {
737 throw "Record `" + getName() + "', field `" + FieldName +
738 "' list is not entirely DefInit!";
739 }
740 }
741 return Defs;
742}
743
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000744/// getValueAsInt - This method looks up the specified field and returns its
745/// value as an int, throwing an exception if the field does not exist or if
746/// the value is not the right type.
747///
748int Record::getValueAsInt(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 (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
755 return II->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000756 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +0000757 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000758}
759
760/// getValueAsDef - This method looks up the specified field and returns its
761/// value as a Record, throwing an exception if the field does not exist or if
762/// the value is not the right type.
763///
764Record *Record::getValueAsDef(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 (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
771 return DI->getDef();
Misha Brukman41f9f292004-10-08 14:59:05 +0000772 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +0000773 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000774}
775
776/// getValueAsBit - This method looks up the specified field and returns its
777/// value as a bit, throwing an exception if the field does not exist or if
778/// the value is not the right type.
779///
780bool Record::getValueAsBit(const std::string &FieldName) const {
781 const RecordVal *R = getValue(FieldName);
782 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000783 throw "Record `" + getName() + "' does not have a field named `" +
784 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000785
786 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
787 return BI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000788 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000789 "' does not have a bit initializer!";
790}
791
792/// getValueAsDag - This method looks up the specified field and returns its
793/// value as an Dag, throwing an exception if the field does not exist or if
794/// the value is not the right type.
795///
796DagInit *Record::getValueAsDag(const std::string &FieldName) const {
797 const RecordVal *R = getValue(FieldName);
798 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000799 throw "Record `" + getName() + "' does not have a field named `" +
800 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000801
802 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
803 return DI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000804 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000805 "' does not have a dag initializer!";
806}
807
Chris Lattnerae939eb2005-09-13 21:44:28 +0000808std::string Record::getValueAsCode(const std::string &FieldName) const {
809 const RecordVal *R = getValue(FieldName);
810 if (R == 0 || R->getValue() == 0)
811 throw "Record `" + getName() + "' does not have a field named `" +
812 FieldName + "'!\n";
813
814 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
815 return CI->getValue();
816 throw "Record `" + getName() + "', field `" + FieldName +
817 "' does not have a code initializer!";
818}
819
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000820
821void RecordKeeper::dump() const { std::cerr << *this; }
822
Chris Lattner68478662004-08-01 03:55:39 +0000823std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000824 OS << "------------- Classes -----------------\n";
825 const std::map<std::string, Record*> &Classes = RK.getClasses();
826 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +0000827 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000828 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000829
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000830 OS << "------------- Defs -----------------\n";
831 const std::map<std::string, Record*> &Defs = RK.getDefs();
832 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +0000833 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000834 OS << "def " << *I->second;
835 return OS;
836}
837
838
839/// getAllDerivedDefinitions - This method returns all concrete definitions
840/// that derive from the specified class name. If a class with the specified
841/// name does not exist, an error is printed and true is returned.
842std::vector<Record*>
843RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
844 Record *Class = Records.getClass(ClassName);
845 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +0000846 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000847
848 std::vector<Record*> Defs;
849 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
850 E = getDefs().end(); I != E; ++I)
851 if (I->second->isSubClassOf(Class))
852 Defs.push_back(I->second);
853
854 return Defs;
855}
Brian Gaeke960707c2003-11-11 22:41:34 +0000856