blob: 94e0cb472f3627bc2613052bbc214d517b7b77ed [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"
Bill Wendling9bfb1e12006-12-07 22:21:48 +000016#include "llvm/Support/Streams.h"
Duraid Madina14492af2005-12-26 05:08:55 +000017#include <ios>
18
Chris Lattner68478662004-08-01 03:55:39 +000019using namespace llvm;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000020
21//===----------------------------------------------------------------------===//
22// Type implementations
23//===----------------------------------------------------------------------===//
24
Bill Wendling9bfb1e12006-12-07 22:21:48 +000025void RecTy::dump() const { print(*cerr.stream()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000026
27Init *BitRecTy::convertValue(BitsInit *BI) {
28 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
29 return BI->getBit(0);
30}
31
32bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
33 return RHS->getNumBits() == 1;
34}
35
36Init *BitRecTy::convertValue(IntInit *II) {
37 int Val = II->getValue();
38 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
Misha Brukman650ba8e2005-04-22 00:00:37 +000039
40 return new BitInit(Val != 0);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000041}
42
43Init *BitRecTy::convertValue(TypedInit *VI) {
44 if (dynamic_cast<BitRecTy*>(VI->getType()))
45 return VI; // Accept variable if it is already of bit type!
46 return 0;
47}
48
49Init *BitsRecTy::convertValue(UnsetInit *UI) {
50 BitsInit *Ret = new BitsInit(Size);
51
52 for (unsigned i = 0; i != Size; ++i)
53 Ret->setBit(i, new UnsetInit());
54 return Ret;
55}
56
57Init *BitsRecTy::convertValue(BitInit *UI) {
58 if (Size != 1) return 0; // Can only convert single bit...
59 BitsInit *Ret = new BitsInit(1);
60 Ret->setBit(0, UI);
61 return Ret;
62}
63
64// convertValue from Int initializer to bits type: Split the integer up into the
65// appropriate bits...
66//
67Init *BitsRecTy::convertValue(IntInit *II) {
Misha Brukman6752fb52004-06-21 18:01:47 +000068 int64_t Value = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000069 // Make sure this bitfield is large enough to hold the integer value...
70 if (Value >= 0) {
Misha Brukman6752fb52004-06-21 18:01:47 +000071 if (Value & ~((1LL << Size)-1))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000072 return 0;
73 } else {
Jeff Cohen0add83e2006-02-18 03:20:33 +000074 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000075 return 0;
76 }
77
78 BitsInit *Ret = new BitsInit(Size);
79 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen0add83e2006-02-18 03:20:33 +000080 Ret->setBit(i, new BitInit(Value & (1LL << i)));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000081
82 return Ret;
83}
84
85Init *BitsRecTy::convertValue(BitsInit *BI) {
86 // If the number of bits is right, return it. Otherwise we need to expand or
87 // truncate...
88 if (BI->getNumBits() == Size) return BI;
89 return 0;
90}
91
92Init *BitsRecTy::convertValue(TypedInit *VI) {
93 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
94 if (BRT->Size == Size) {
95 BitsInit *Ret = new BitsInit(Size);
96 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen88e7b722005-04-22 04:13:13 +000097 Ret->setBit(i, new VarBitInit(VI, i));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000098 return Ret;
99 }
100 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
101 BitsInit *Ret = new BitsInit(1);
102 Ret->setBit(0, VI);
103 return Ret;
104 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000105
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000106 return 0;
107}
108
109Init *IntRecTy::convertValue(BitInit *BI) {
110 return new IntInit(BI->getValue());
111}
112
113Init *IntRecTy::convertValue(BitsInit *BI) {
114 int Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000115 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000116 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
117 Result |= Bit->getValue() << i;
118 } else {
119 return 0;
120 }
121 return new IntInit(Result);
122}
123
124Init *IntRecTy::convertValue(TypedInit *TI) {
125 if (TI->getType()->typeIsConvertibleTo(this))
126 return TI; // Accept variable if already of the right type!
127 return 0;
128}
129
Chris Lattner51ffbf12006-03-31 21:53:49 +0000130Init *StringRecTy::convertValue(BinOpInit *BO) {
131 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
132 Init *L = BO->getLHS()->convertInitializerTo(this);
133 Init *R = BO->getRHS()->convertInitializerTo(this);
134 if (L == 0 || R == 0) return 0;
135 if (L != BO->getLHS() || R != BO->getRHS())
136 return new BinOpInit(BinOpInit::STRCONCAT, L, R);
137 return BO;
138 }
139 return 0;
140}
141
142
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000143Init *StringRecTy::convertValue(TypedInit *TI) {
144 if (dynamic_cast<StringRecTy*>(TI->getType()))
145 return TI; // Accept variable if already of the right type!
146 return 0;
147}
148
149void ListRecTy::print(std::ostream &OS) const {
150 OS << "list<" << *Ty << ">";
151}
152
153Init *ListRecTy::convertValue(ListInit *LI) {
154 std::vector<Init*> Elements;
155
156 // Verify that all of the elements of the list are subclasses of the
157 // appropriate class!
158 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
159 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
160 Elements.push_back(CI);
161 else
162 return 0;
163
164 return new ListInit(Elements);
165}
166
167Init *ListRecTy::convertValue(TypedInit *TI) {
168 // Ensure that TI is compatible with our class.
169 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
170 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
171 return TI;
172 return 0;
173}
174
175Init *CodeRecTy::convertValue(TypedInit *TI) {
176 if (TI->getType()->typeIsConvertibleTo(this))
177 return TI;
178 return 0;
179}
180
181Init *DagRecTy::convertValue(TypedInit *TI) {
182 if (TI->getType()->typeIsConvertibleTo(this))
183 return TI;
184 return 0;
185}
186
187
188void RecordRecTy::print(std::ostream &OS) const {
189 OS << Rec->getName();
190}
191
192Init *RecordRecTy::convertValue(DefInit *DI) {
193 // Ensure that DI is a subclass of Rec.
194 if (!DI->getDef()->isSubClassOf(Rec))
195 return 0;
196 return DI;
197}
198
199Init *RecordRecTy::convertValue(TypedInit *TI) {
200 // Ensure that TI is compatible with Rec.
201 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
202 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
203 RRT->getRecord() == getRecord())
204 return TI;
205 return 0;
206}
207
208bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
209 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
210}
211
212
213//===----------------------------------------------------------------------===//
214// Initializer implementations
215//===----------------------------------------------------------------------===//
216
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000217void Init::dump() const { return print(*cerr.stream()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000218
219Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
220 BitsInit *BI = new BitsInit(Bits.size());
221 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
222 if (Bits[i] >= getNumBits()) {
223 delete BI;
224 return 0;
225 }
226 BI->setBit(i, getBit(Bits[i]));
227 }
228 return BI;
229}
230
231void BitsInit::print(std::ostream &OS) const {
232 //if (!printInHex(OS)) return;
233 //if (!printAsVariable(OS)) return;
234 //if (!printAsUnset(OS)) return;
235
236 OS << "{ ";
237 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
238 if (i) OS << ", ";
239 if (Init *Bit = getBit(e-i-1))
240 Bit->print(OS);
241 else
242 OS << "*";
243 }
244 OS << " }";
245}
246
247bool BitsInit::printInHex(std::ostream &OS) const {
248 // First, attempt to convert the value into an integer value...
249 int Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000250 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000251 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
252 Result |= Bit->getValue() << i;
253 } else {
254 return true;
255 }
256
257 OS << "0x" << std::hex << Result << std::dec;
258 return false;
259}
260
261bool BitsInit::printAsVariable(std::ostream &OS) const {
262 // Get the variable that we may be set equal to...
263 assert(getNumBits() != 0);
264 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
265 if (FirstBit == 0) return true;
266 TypedInit *Var = FirstBit->getVariable();
267
268 // Check to make sure the types are compatible.
269 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
270 if (Ty == 0) return true;
271 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
272
273 // Check to make sure all bits are referring to the right bits in the variable
274 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
275 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
276 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
277 return true;
278 }
279
280 Var->print(OS);
281 return false;
282}
283
284bool BitsInit::printAsUnset(std::ostream &OS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000285 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000286 if (!dynamic_cast<UnsetInit*>(getBit(i)))
287 return true;
288 OS << "?";
289 return false;
290}
291
292// resolveReferences - If there are any field references that refer to fields
293// that have been filled in, we can propagate the values now.
294//
Chris Lattneref943742005-04-19 03:36:21 +0000295Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000296 bool Changed = false;
297 BitsInit *New = new BitsInit(getNumBits());
298
299 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
300 Init *B;
301 Init *CurBit = getBit(i);
302
303 do {
304 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000305 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000306 Changed |= B != CurBit;
307 } while (B != CurBit);
308 New->setBit(i, CurBit);
309 }
310
311 if (Changed)
312 return New;
313 delete New;
314 return this;
315}
316
317Init *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 Lattner51ffbf12006-03-31 21:53:49 +0000371Init *BinOpInit::Fold() {
372 switch (getOpcode()) {
373 default: assert(0 && "Unknown binop");
374 case STRCONCAT: {
375 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
376 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
377 if (LHSs && RHSs)
378 return new StringInit(LHSs->getValue() + RHSs->getValue());
379 break;
380 }
381 case SHL:
382 case SRA:
383 case SRL: {
384 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
385 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
386 if (LHSi && RHSi) {
387 int LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
388 int Result;
389 switch (getOpcode()) {
390 default: assert(0 && "Bad opcode!");
391 case SHL: Result = LHSv << RHSv; break;
392 case SRA: Result = LHSv >> RHSv; break;
393 case SRL: Result = (unsigned)LHSv >> (unsigned)RHSv; break;
394 }
395 return new IntInit(Result);
396 }
397 break;
398 }
399 }
400 return this;
401}
402
403Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
404 Init *lhs = LHS->resolveReferences(R, RV);
405 Init *rhs = RHS->resolveReferences(R, RV);
406
407 if (LHS != lhs || RHS != rhs)
408 return (new BinOpInit(getOpcode(), lhs, rhs))->Fold();
409 return Fold();
410}
411
412void BinOpInit::print(std::ostream &OS) const {
413 switch (Opc) {
414 case SHL: OS << "!shl"; break;
415 case SRA: OS << "!sra"; break;
416 case SRL: OS << "!srl"; break;
417 case STRCONCAT: OS << "!strconcat"; break;
418 }
419 OS << "(";
420 LHS->print(OS);
421 OS << ", ";
422 RHS->print(OS);
423 OS << ")";
424}
425
Chris Lattner577fc3f2004-07-27 01:01:21 +0000426Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000427 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
428 if (T == 0) return 0; // Cannot subscript a non-bits variable...
429 unsigned NumBits = T->getNumBits();
430
431 BitsInit *BI = new BitsInit(Bits.size());
432 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
433 if (Bits[i] >= NumBits) {
434 delete BI;
435 return 0;
436 }
437 BI->setBit(i, new VarBitInit(this, Bits[i]));
438 }
439 return BI;
440}
441
Chris Lattner577fc3f2004-07-27 01:01:21 +0000442Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
443 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
444 if (T == 0) return 0; // Cannot subscript a non-list variable...
445
446 if (Elements.size() == 1)
447 return new VarListElementInit(this, Elements[0]);
448
449 std::vector<Init*> ListInits;
450 ListInits.reserve(Elements.size());
451 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
452 ListInits.push_back(new VarListElementInit(this, Elements[i]));
453 return new ListInit(ListInits);
454}
455
456
Chris Lattneref943742005-04-19 03:36:21 +0000457Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
458 unsigned Bit) {
459 if (R.isTemplateArg(getName())) return 0;
460 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000461
462 RecordVal *RV = R.getValue(getName());
463 assert(RV && "Reference to a non-existant variable?");
464 assert(dynamic_cast<BitsInit*>(RV->getValue()));
465 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +0000466
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000467 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
468 Init *B = BI->getBit(Bit);
469
470 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
471 return B; // Replace the VarBitInit with it.
Chris Lattner577fc3f2004-07-27 01:01:21 +0000472 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000473}
474
Chris Lattneref943742005-04-19 03:36:21 +0000475Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
476 unsigned Elt) {
477 if (R.isTemplateArg(getName())) return 0;
478 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000479
480 RecordVal *RV = R.getValue(getName());
481 assert(RV && "Reference to a non-existant variable?");
482 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
483 assert(LI && "Invalid list element!");
484
485 if (Elt >= LI->getSize())
486 return 0; // Out of range reference.
487 Init *E = LI->getElement(Elt);
488 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
489 return E; // Replace the VarListElementInit with it.
490 return 0;
491}
492
493
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000494RecTy *VarInit::getFieldType(const std::string &FieldName) const {
495 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
496 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
497 return RV->getType();
498 return 0;
499}
500
501Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +0000502 if (dynamic_cast<RecordRecTy*>(getType()))
Chris Lattnerd959ab92004-02-28 16:31:53 +0000503 if (const RecordVal *RV = R.getValue(VarName)) {
504 Init *TheInit = RV->getValue();
505 assert(TheInit != this && "Infinite loop detected!");
506 if (Init *I = TheInit->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000507 return I;
508 else
509 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +0000510 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000511 return 0;
512}
513
514/// resolveReferences - This method is used by classes that refer to other
515/// variables which may not be defined at the time they expression is formed.
516/// If a value is set for the variable later, this method will be called on
517/// users of the value to allow the value to propagate out.
518///
Chris Lattneref943742005-04-19 03:36:21 +0000519Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000520 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +0000521 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000522 return Val->getValue();
523 return this;
524}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000525
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000526
Chris Lattneref943742005-04-19 03:36:21 +0000527Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
528 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000529 return I;
530 return this;
531}
532
Chris Lattneref943742005-04-19 03:36:21 +0000533Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
534 if (Init *I = getVariable()->resolveListElementReference(R, RV,
535 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +0000536 return I;
537 return this;
538}
539
Chris Lattneref943742005-04-19 03:36:21 +0000540Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
541 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000542 // FIXME: This should be implemented, to support references like:
543 // bit B = AA[0]{1};
544 return 0;
545}
546
Chris Lattneref943742005-04-19 03:36:21 +0000547Init *VarListElementInit::
548resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000549 // FIXME: This should be implemented, to support references like:
550 // int B = AA[0][1];
551 return 0;
552}
553
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000554RecTy *DefInit::getFieldType(const std::string &FieldName) const {
555 if (const RecordVal *RV = Def->getValue(FieldName))
556 return RV->getType();
557 return 0;
558}
559
560Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
561 return Def->getValue(FieldName)->getValue();
562}
563
564
565void DefInit::print(std::ostream &OS) const {
566 OS << Def->getName();
567}
568
Chris Lattneref943742005-04-19 03:36:21 +0000569Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
570 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000571 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000572 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
573 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
574 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000575
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000576 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
577 return B; // Replace the VarBitInit with it.
578 }
Chris Lattner577fc3f2004-07-27 01:01:21 +0000579 return 0;
580}
581
Chris Lattneref943742005-04-19 03:36:21 +0000582Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
583 unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000584 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
585 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
586 if (Elt >= LI->getSize()) return 0;
587 Init *E = LI->getElement(Elt);
588
589 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
590 return E; // Replace the VarListElementInit with it.
591 }
592 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000593}
594
Chris Lattneref943742005-04-19 03:36:21 +0000595Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
596 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
597
598 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000599 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +0000600 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000601 return BVR->isComplete() ? BVR : this;
602 }
Chris Lattneref943742005-04-19 03:36:21 +0000603
604 if (NewRec != Rec) {
605 dump();
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000606 NewRec->dump(); cerr << "\n";
Chris Lattneref943742005-04-19 03:36:21 +0000607 return new FieldInit(NewRec, FieldName);
608 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000609 return this;
610}
611
Chris Lattner0d3ef402006-01-31 06:02:35 +0000612Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
613 std::vector<Init*> NewArgs;
614 for (unsigned i = 0, e = Args.size(); i != e; ++i)
615 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
616
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000617 Init *Op = Val->resolveReferences(R, RV);
618
619 if (Args != NewArgs || Op != Val)
620 return new DagInit(Op, NewArgs, ArgNames);
Chris Lattner0d3ef402006-01-31 06:02:35 +0000621
622 return this;
623}
624
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000625
626void DagInit::print(std::ostream &OS) const {
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000627 OS << "(" << *Val;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000628 if (Args.size()) {
629 OS << " " << *Args[0];
630 if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
631 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
632 OS << ", " << *Args[i];
633 if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
634 }
635 }
636 OS << ")";
637}
638
639
640//===----------------------------------------------------------------------===//
641// Other implementations
642//===----------------------------------------------------------------------===//
643
644RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
645 : Name(N), Ty(T), Prefix(P) {
646 Value = Ty->convertValue(new UnsetInit());
647 assert(Value && "Cannot create unset value for current type!");
648}
649
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000650void RecordVal::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000651
652void RecordVal::print(std::ostream &OS, bool PrintSem) const {
653 if (getPrefix()) OS << "field ";
654 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +0000655
656 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000657 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +0000658
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000659 if (PrintSem) OS << ";\n";
660}
661
Chris Lattnerac284252005-08-19 17:58:11 +0000662void Record::setName(const std::string &Name) {
663 if (Records.getDef(getName()) == this) {
664 Records.removeDef(getName());
665 this->Name = Name;
666 Records.addDef(this);
667 } else {
668 Records.removeClass(getName());
669 this->Name = Name;
670 Records.addClass(this);
671 }
672}
673
Chris Lattneref943742005-04-19 03:36:21 +0000674/// resolveReferencesTo - If anything in this record refers to RV, replace the
675/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
676/// references.
677void Record::resolveReferencesTo(const RecordVal *RV) {
678 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
679 if (Init *V = Values[i].getValue())
680 Values[i].setValue(V->resolveReferences(*this, RV));
681 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000682}
683
Chris Lattneref943742005-04-19 03:36:21 +0000684
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000685void Record::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000686
Chris Lattner68478662004-08-01 03:55:39 +0000687std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000688 OS << R.getName();
689
690 const std::vector<std::string> &TArgs = R.getTemplateArgs();
691 if (!TArgs.empty()) {
692 OS << "<";
693 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
694 if (i) OS << ", ";
695 const RecordVal *RV = R.getValue(TArgs[i]);
696 assert(RV && "Template argument record not found??");
697 RV->print(OS, false);
698 }
699 OS << ">";
700 }
701
702 OS << " {";
703 const std::vector<Record*> &SC = R.getSuperClasses();
704 if (!SC.empty()) {
705 OS << "\t//";
706 for (unsigned i = 0, e = SC.size(); i != e; ++i)
707 OS << " " << SC[i]->getName();
708 }
709 OS << "\n";
710
711 const std::vector<RecordVal> &Vals = R.getValues();
712 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
713 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
714 OS << Vals[i];
715 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
716 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
717 OS << Vals[i];
718
719 return OS << "}\n";
720}
721
722/// getValueInit - Return the initializer for a value with the specified name,
723/// or throw an exception if the field does not exist.
724///
725Init *Record::getValueInit(const std::string &FieldName) const {
726 const RecordVal *R = getValue(FieldName);
727 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000728 throw "Record `" + getName() + "' does not have a field named `" +
729 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000730 return R->getValue();
731}
732
733
734/// getValueAsString - This method looks up the specified field and returns its
735/// value as a string, throwing an exception if the field does not exist or if
736/// the value is not a string.
737///
738std::string Record::getValueAsString(const std::string &FieldName) const {
739 const RecordVal *R = getValue(FieldName);
740 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000741 throw "Record `" + getName() + "' does not have a field named `" +
742 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000743
744 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
745 return SI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000746 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000747 "' does not have a string initializer!";
748}
749
750/// getValueAsBitsInit - This method looks up the specified field and returns
751/// its value as a BitsInit, throwing an exception if the field does not exist
752/// or if the value is not the right type.
753///
754BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
755 const RecordVal *R = getValue(FieldName);
756 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000757 throw "Record `" + getName() + "' does not have a field named `" +
758 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000759
760 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
761 return BI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000762 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000763 "' does not have a BitsInit initializer!";
764}
765
766/// getValueAsListInit - This method looks up the specified field and returns
767/// its value as a ListInit, throwing an exception if the field does not exist
768/// or if the value is not the right type.
769///
770ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
771 const RecordVal *R = getValue(FieldName);
772 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000773 throw "Record `" + getName() + "' does not have a field named `" +
774 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000775
776 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
777 return LI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000778 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000779 "' does not have a list initializer!";
780}
781
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000782/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +0000783/// its value as a vector of records, throwing an exception if the field does
784/// not exist or if the value is not the right type.
785///
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000786std::vector<Record*>
787Record::getValueAsListOfDefs(const std::string &FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +0000788 ListInit *List = getValueAsListInit(FieldName);
789 std::vector<Record*> Defs;
790 for (unsigned i = 0; i < List->getSize(); i++) {
791 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
792 Defs.push_back(DI->getDef());
793 } else {
794 throw "Record `" + getName() + "', field `" + FieldName +
795 "' list is not entirely DefInit!";
796 }
797 }
798 return Defs;
799}
800
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000801/// getValueAsInt - This method looks up the specified field and returns its
802/// value as an int, throwing an exception if the field does not exist or if
803/// the value is not the right type.
804///
805int Record::getValueAsInt(const std::string &FieldName) const {
806 const RecordVal *R = getValue(FieldName);
807 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000808 throw "Record `" + getName() + "' does not have a field named `" +
809 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000810
811 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
812 return II->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000813 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +0000814 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000815}
816
817/// getValueAsDef - This method looks up the specified field and returns its
818/// value as a Record, throwing an exception if the field does not exist or if
819/// the value is not the right type.
820///
821Record *Record::getValueAsDef(const std::string &FieldName) const {
822 const RecordVal *R = getValue(FieldName);
823 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000824 throw "Record `" + getName() + "' does not have a field named `" +
825 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000826
827 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
828 return DI->getDef();
Misha Brukman41f9f292004-10-08 14:59:05 +0000829 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +0000830 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000831}
832
833/// getValueAsBit - This method looks up the specified field and returns its
834/// value as a bit, throwing an exception if the field does not exist or if
835/// the value is not the right type.
836///
837bool Record::getValueAsBit(const std::string &FieldName) const {
838 const RecordVal *R = getValue(FieldName);
839 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000840 throw "Record `" + getName() + "' does not have a field named `" +
841 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000842
843 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
844 return BI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000845 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000846 "' does not have a bit initializer!";
847}
848
849/// getValueAsDag - This method looks up the specified field and returns its
850/// value as an Dag, throwing an exception if the field does not exist or if
851/// the value is not the right type.
852///
853DagInit *Record::getValueAsDag(const std::string &FieldName) const {
854 const RecordVal *R = getValue(FieldName);
855 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000856 throw "Record `" + getName() + "' does not have a field named `" +
857 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000858
859 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
860 return DI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000861 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000862 "' does not have a dag initializer!";
863}
864
Chris Lattnerae939eb2005-09-13 21:44:28 +0000865std::string Record::getValueAsCode(const std::string &FieldName) const {
866 const RecordVal *R = getValue(FieldName);
867 if (R == 0 || R->getValue() == 0)
868 throw "Record `" + getName() + "' does not have a field named `" +
869 FieldName + "'!\n";
870
871 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
872 return CI->getValue();
873 throw "Record `" + getName() + "', field `" + FieldName +
874 "' does not have a code initializer!";
875}
876
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000877
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000878void RecordKeeper::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000879
Chris Lattner68478662004-08-01 03:55:39 +0000880std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000881 OS << "------------- Classes -----------------\n";
882 const std::map<std::string, Record*> &Classes = RK.getClasses();
883 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +0000884 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000885 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000886
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000887 OS << "------------- Defs -----------------\n";
888 const std::map<std::string, Record*> &Defs = RK.getDefs();
889 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +0000890 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000891 OS << "def " << *I->second;
892 return OS;
893}
894
895
896/// getAllDerivedDefinitions - This method returns all concrete definitions
897/// that derive from the specified class name. If a class with the specified
898/// name does not exist, an error is printed and true is returned.
899std::vector<Record*>
900RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
901 Record *Class = Records.getClass(ClassName);
902 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +0000903 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000904
905 std::vector<Record*> Defs;
906 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
907 E = getDefs().end(); I != E; ++I)
908 if (I->second->isSubClassOf(Class))
909 Defs.push_back(I->second);
910
911 return Defs;
912}
Brian Gaeke960707c2003-11-11 22:41:34 +0000913