blob: 75583bb1d17f1e8c317052877a56f81b68c83def [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
Evan Chenga32dee22007-05-15 01:23:24 +0000187Init *DagRecTy::convertValue(BinOpInit *BO) {
188 if (BO->getOpcode() == BinOpInit::CONCAT) {
189 Init *L = BO->getLHS()->convertInitializerTo(this);
190 Init *R = BO->getRHS()->convertInitializerTo(this);
191 if (L == 0 || R == 0) return 0;
192 if (L != BO->getLHS() || R != BO->getRHS())
193 return new BinOpInit(BinOpInit::CONCAT, L, R);
194 return BO;
195 }
196 return 0;
197}
198
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000199
200void RecordRecTy::print(std::ostream &OS) const {
201 OS << Rec->getName();
202}
203
204Init *RecordRecTy::convertValue(DefInit *DI) {
205 // Ensure that DI is a subclass of Rec.
206 if (!DI->getDef()->isSubClassOf(Rec))
207 return 0;
208 return DI;
209}
210
211Init *RecordRecTy::convertValue(TypedInit *TI) {
212 // Ensure that TI is compatible with Rec.
213 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
214 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
215 RRT->getRecord() == getRecord())
216 return TI;
217 return 0;
218}
219
220bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
221 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
222}
223
224
225//===----------------------------------------------------------------------===//
226// Initializer implementations
227//===----------------------------------------------------------------------===//
228
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000229void Init::dump() const { return print(*cerr.stream()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000230
231Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
232 BitsInit *BI = new BitsInit(Bits.size());
233 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
234 if (Bits[i] >= getNumBits()) {
235 delete BI;
236 return 0;
237 }
238 BI->setBit(i, getBit(Bits[i]));
239 }
240 return BI;
241}
242
243void BitsInit::print(std::ostream &OS) const {
244 //if (!printInHex(OS)) return;
245 //if (!printAsVariable(OS)) return;
246 //if (!printAsUnset(OS)) return;
247
248 OS << "{ ";
249 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
250 if (i) OS << ", ";
251 if (Init *Bit = getBit(e-i-1))
252 Bit->print(OS);
253 else
254 OS << "*";
255 }
256 OS << " }";
257}
258
259bool BitsInit::printInHex(std::ostream &OS) const {
260 // First, attempt to convert the value into an integer value...
261 int Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000262 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000263 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
264 Result |= Bit->getValue() << i;
265 } else {
266 return true;
267 }
268
269 OS << "0x" << std::hex << Result << std::dec;
270 return false;
271}
272
273bool BitsInit::printAsVariable(std::ostream &OS) const {
274 // Get the variable that we may be set equal to...
275 assert(getNumBits() != 0);
276 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
277 if (FirstBit == 0) return true;
278 TypedInit *Var = FirstBit->getVariable();
279
280 // Check to make sure the types are compatible.
281 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
282 if (Ty == 0) return true;
283 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
284
285 // Check to make sure all bits are referring to the right bits in the variable
286 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
287 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
288 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
289 return true;
290 }
291
292 Var->print(OS);
293 return false;
294}
295
296bool BitsInit::printAsUnset(std::ostream &OS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000297 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000298 if (!dynamic_cast<UnsetInit*>(getBit(i)))
299 return true;
300 OS << "?";
301 return false;
302}
303
304// resolveReferences - If there are any field references that refer to fields
305// that have been filled in, we can propagate the values now.
306//
Chris Lattneref943742005-04-19 03:36:21 +0000307Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000308 bool Changed = false;
309 BitsInit *New = new BitsInit(getNumBits());
310
311 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
312 Init *B;
313 Init *CurBit = getBit(i);
314
315 do {
316 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000317 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000318 Changed |= B != CurBit;
319 } while (B != CurBit);
320 New->setBit(i, CurBit);
321 }
322
323 if (Changed)
324 return New;
325 delete New;
326 return this;
327}
328
329Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
330 BitsInit *BI = new BitsInit(Bits.size());
331
332 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
333 if (Bits[i] >= 32) {
334 delete BI;
335 return 0;
336 }
337 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
338 }
339 return BI;
340}
341
Chris Lattner8bf9e062004-07-26 23:21:34 +0000342Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
343 std::vector<Init*> Vals;
344 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
345 if (Elements[i] >= getSize())
346 return 0;
347 Vals.push_back(getElement(Elements[i]));
348 }
349 return new ListInit(Vals);
350}
351
Chris Lattnercbebe462007-02-27 22:08:27 +0000352Record *ListInit::getElementAsRecord(unsigned i) const {
353 assert(i < Values.size() && "List element index out of range!");
354 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
355 if (DI == 0) throw "Expected record in list!";
356 return DI->getDef();
357}
358
Chris Lattneref943742005-04-19 03:36:21 +0000359Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000360 std::vector<Init*> Resolved;
361 Resolved.reserve(getSize());
362 bool Changed = false;
363
364 for (unsigned i = 0, e = getSize(); i != e; ++i) {
365 Init *E;
366 Init *CurElt = getElement(i);
367
368 do {
369 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000370 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000371 Changed |= E != CurElt;
372 } while (E != CurElt);
373 Resolved.push_back(E);
374 }
375
376 if (Changed)
377 return new ListInit(Resolved);
378 return this;
379}
380
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000381void ListInit::print(std::ostream &OS) const {
382 OS << "[";
383 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
384 if (i) OS << ", ";
385 OS << *Values[i];
386 }
387 OS << "]";
388}
389
Chris Lattner51ffbf12006-03-31 21:53:49 +0000390Init *BinOpInit::Fold() {
391 switch (getOpcode()) {
392 default: assert(0 && "Unknown binop");
Evan Chenga32dee22007-05-15 01:23:24 +0000393 case CONCAT: {
394 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
395 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
396 if (LHSs && RHSs) {
397 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
398 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
Evan Cheng94b5a802007-07-19 01:14:50 +0000399 if (LOp->getDef() != ROp->getDef()) {
400 bool LIsOps =
401 LOp->getDef()->getName() == "outs" ||
402 LOp->getDef()->getName() != "ins" ||
403 LOp->getDef()->getName() != "defs";
404 bool RIsOps =
405 ROp->getDef()->getName() == "outs" ||
406 ROp->getDef()->getName() != "ins" ||
407 ROp->getDef()->getName() != "defs";
408 if (!LIsOps || !RIsOps)
409 throw "Concated Dag operators do not match!";
410 }
Evan Chenga32dee22007-05-15 01:23:24 +0000411 std::vector<Init*> Args;
412 std::vector<std::string> ArgNames;
413 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
414 Args.push_back(LHSs->getArg(i));
415 ArgNames.push_back(LHSs->getArgName(i));
416 }
417 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
418 Args.push_back(RHSs->getArg(i));
419 ArgNames.push_back(RHSs->getArgName(i));
420 }
421 return new DagInit(LHSs->getOperator(), Args, ArgNames);
422 }
423 break;
424 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000425 case STRCONCAT: {
426 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
427 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
428 if (LHSs && RHSs)
429 return new StringInit(LHSs->getValue() + RHSs->getValue());
430 break;
431 }
432 case SHL:
433 case SRA:
434 case SRL: {
435 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
436 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
437 if (LHSi && RHSi) {
438 int LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
439 int Result;
440 switch (getOpcode()) {
441 default: assert(0 && "Bad opcode!");
442 case SHL: Result = LHSv << RHSv; break;
443 case SRA: Result = LHSv >> RHSv; break;
444 case SRL: Result = (unsigned)LHSv >> (unsigned)RHSv; break;
445 }
446 return new IntInit(Result);
447 }
448 break;
449 }
450 }
451 return this;
452}
453
454Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
455 Init *lhs = LHS->resolveReferences(R, RV);
456 Init *rhs = RHS->resolveReferences(R, RV);
457
458 if (LHS != lhs || RHS != rhs)
459 return (new BinOpInit(getOpcode(), lhs, rhs))->Fold();
460 return Fold();
461}
462
463void BinOpInit::print(std::ostream &OS) const {
464 switch (Opc) {
Evan Chenga32dee22007-05-15 01:23:24 +0000465 case CONCAT: OS << "!con"; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000466 case SHL: OS << "!shl"; break;
467 case SRA: OS << "!sra"; break;
468 case SRL: OS << "!srl"; break;
469 case STRCONCAT: OS << "!strconcat"; break;
470 }
471 OS << "(";
472 LHS->print(OS);
473 OS << ", ";
474 RHS->print(OS);
475 OS << ")";
476}
477
Chris Lattner577fc3f2004-07-27 01:01:21 +0000478Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000479 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
480 if (T == 0) return 0; // Cannot subscript a non-bits variable...
481 unsigned NumBits = T->getNumBits();
482
483 BitsInit *BI = new BitsInit(Bits.size());
484 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
485 if (Bits[i] >= NumBits) {
486 delete BI;
487 return 0;
488 }
489 BI->setBit(i, new VarBitInit(this, Bits[i]));
490 }
491 return BI;
492}
493
Chris Lattner577fc3f2004-07-27 01:01:21 +0000494Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
495 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
496 if (T == 0) return 0; // Cannot subscript a non-list variable...
497
498 if (Elements.size() == 1)
499 return new VarListElementInit(this, Elements[0]);
500
501 std::vector<Init*> ListInits;
502 ListInits.reserve(Elements.size());
503 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
504 ListInits.push_back(new VarListElementInit(this, Elements[i]));
505 return new ListInit(ListInits);
506}
507
508
Chris Lattneref943742005-04-19 03:36:21 +0000509Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
510 unsigned Bit) {
511 if (R.isTemplateArg(getName())) return 0;
512 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000513
514 RecordVal *RV = R.getValue(getName());
515 assert(RV && "Reference to a non-existant variable?");
516 assert(dynamic_cast<BitsInit*>(RV->getValue()));
517 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +0000518
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000519 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
520 Init *B = BI->getBit(Bit);
521
522 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
523 return B; // Replace the VarBitInit with it.
Chris Lattner577fc3f2004-07-27 01:01:21 +0000524 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000525}
526
Chris Lattneref943742005-04-19 03:36:21 +0000527Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
528 unsigned Elt) {
529 if (R.isTemplateArg(getName())) return 0;
530 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000531
532 RecordVal *RV = R.getValue(getName());
533 assert(RV && "Reference to a non-existant variable?");
534 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
535 assert(LI && "Invalid list element!");
536
537 if (Elt >= LI->getSize())
538 return 0; // Out of range reference.
539 Init *E = LI->getElement(Elt);
540 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
541 return E; // Replace the VarListElementInit with it.
542 return 0;
543}
544
545
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000546RecTy *VarInit::getFieldType(const std::string &FieldName) const {
547 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
548 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
549 return RV->getType();
550 return 0;
551}
552
553Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +0000554 if (dynamic_cast<RecordRecTy*>(getType()))
Chris Lattnerd959ab92004-02-28 16:31:53 +0000555 if (const RecordVal *RV = R.getValue(VarName)) {
556 Init *TheInit = RV->getValue();
557 assert(TheInit != this && "Infinite loop detected!");
558 if (Init *I = TheInit->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000559 return I;
560 else
561 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +0000562 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000563 return 0;
564}
565
566/// resolveReferences - This method is used by classes that refer to other
567/// variables which may not be defined at the time they expression is formed.
568/// If a value is set for the variable later, this method will be called on
569/// users of the value to allow the value to propagate out.
570///
Chris Lattneref943742005-04-19 03:36:21 +0000571Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000572 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +0000573 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000574 return Val->getValue();
575 return this;
576}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000577
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000578
Chris Lattneref943742005-04-19 03:36:21 +0000579Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
580 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000581 return I;
582 return this;
583}
584
Chris Lattneref943742005-04-19 03:36:21 +0000585Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
586 if (Init *I = getVariable()->resolveListElementReference(R, RV,
587 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +0000588 return I;
589 return this;
590}
591
Chris Lattneref943742005-04-19 03:36:21 +0000592Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
593 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000594 // FIXME: This should be implemented, to support references like:
595 // bit B = AA[0]{1};
596 return 0;
597}
598
Chris Lattneref943742005-04-19 03:36:21 +0000599Init *VarListElementInit::
600resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000601 // FIXME: This should be implemented, to support references like:
602 // int B = AA[0][1];
603 return 0;
604}
605
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000606RecTy *DefInit::getFieldType(const std::string &FieldName) const {
607 if (const RecordVal *RV = Def->getValue(FieldName))
608 return RV->getType();
609 return 0;
610}
611
612Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
613 return Def->getValue(FieldName)->getValue();
614}
615
616
617void DefInit::print(std::ostream &OS) const {
618 OS << Def->getName();
619}
620
Chris Lattneref943742005-04-19 03:36:21 +0000621Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
622 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000623 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000624 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
625 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
626 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000627
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000628 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
629 return B; // Replace the VarBitInit with it.
630 }
Chris Lattner577fc3f2004-07-27 01:01:21 +0000631 return 0;
632}
633
Chris Lattneref943742005-04-19 03:36:21 +0000634Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
635 unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000636 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
637 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
638 if (Elt >= LI->getSize()) return 0;
639 Init *E = LI->getElement(Elt);
640
641 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
642 return E; // Replace the VarListElementInit with it.
643 }
644 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000645}
646
Chris Lattneref943742005-04-19 03:36:21 +0000647Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
648 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
649
650 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000651 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +0000652 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000653 return BVR->isComplete() ? BVR : this;
654 }
Chris Lattneref943742005-04-19 03:36:21 +0000655
656 if (NewRec != Rec) {
657 dump();
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000658 NewRec->dump(); cerr << "\n";
Chris Lattneref943742005-04-19 03:36:21 +0000659 return new FieldInit(NewRec, FieldName);
660 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000661 return this;
662}
663
Chris Lattner0d3ef402006-01-31 06:02:35 +0000664Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
665 std::vector<Init*> NewArgs;
666 for (unsigned i = 0, e = Args.size(); i != e; ++i)
667 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
668
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000669 Init *Op = Val->resolveReferences(R, RV);
670
671 if (Args != NewArgs || Op != Val)
672 return new DagInit(Op, NewArgs, ArgNames);
Chris Lattner0d3ef402006-01-31 06:02:35 +0000673
674 return this;
675}
676
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000677
678void DagInit::print(std::ostream &OS) const {
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000679 OS << "(" << *Val;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000680 if (Args.size()) {
681 OS << " " << *Args[0];
682 if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
683 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
684 OS << ", " << *Args[i];
685 if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
686 }
687 }
688 OS << ")";
689}
690
691
692//===----------------------------------------------------------------------===//
693// Other implementations
694//===----------------------------------------------------------------------===//
695
696RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
697 : Name(N), Ty(T), Prefix(P) {
698 Value = Ty->convertValue(new UnsetInit());
699 assert(Value && "Cannot create unset value for current type!");
700}
701
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000702void RecordVal::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000703
704void RecordVal::print(std::ostream &OS, bool PrintSem) const {
705 if (getPrefix()) OS << "field ";
706 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +0000707
708 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000709 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +0000710
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000711 if (PrintSem) OS << ";\n";
712}
713
Chris Lattnerac284252005-08-19 17:58:11 +0000714void Record::setName(const std::string &Name) {
715 if (Records.getDef(getName()) == this) {
716 Records.removeDef(getName());
717 this->Name = Name;
718 Records.addDef(this);
719 } else {
720 Records.removeClass(getName());
721 this->Name = Name;
722 Records.addClass(this);
723 }
724}
725
Chris Lattneref943742005-04-19 03:36:21 +0000726/// resolveReferencesTo - If anything in this record refers to RV, replace the
727/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
728/// references.
729void Record::resolveReferencesTo(const RecordVal *RV) {
730 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
731 if (Init *V = Values[i].getValue())
732 Values[i].setValue(V->resolveReferences(*this, RV));
733 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000734}
735
Chris Lattneref943742005-04-19 03:36:21 +0000736
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000737void Record::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000738
Chris Lattner68478662004-08-01 03:55:39 +0000739std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000740 OS << R.getName();
741
742 const std::vector<std::string> &TArgs = R.getTemplateArgs();
743 if (!TArgs.empty()) {
744 OS << "<";
745 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
746 if (i) OS << ", ";
747 const RecordVal *RV = R.getValue(TArgs[i]);
748 assert(RV && "Template argument record not found??");
749 RV->print(OS, false);
750 }
751 OS << ">";
752 }
753
754 OS << " {";
755 const std::vector<Record*> &SC = R.getSuperClasses();
756 if (!SC.empty()) {
757 OS << "\t//";
758 for (unsigned i = 0, e = SC.size(); i != e; ++i)
759 OS << " " << SC[i]->getName();
760 }
761 OS << "\n";
762
763 const std::vector<RecordVal> &Vals = R.getValues();
764 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
765 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
766 OS << Vals[i];
767 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
768 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
769 OS << Vals[i];
770
771 return OS << "}\n";
772}
773
774/// getValueInit - Return the initializer for a value with the specified name,
775/// or throw an exception if the field does not exist.
776///
777Init *Record::getValueInit(const std::string &FieldName) const {
778 const RecordVal *R = getValue(FieldName);
779 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000780 throw "Record `" + getName() + "' does not have a field named `" +
781 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000782 return R->getValue();
783}
784
785
786/// getValueAsString - This method looks up the specified field and returns its
787/// value as a string, throwing an exception if the field does not exist or if
788/// the value is not a string.
789///
790std::string Record::getValueAsString(const std::string &FieldName) const {
791 const RecordVal *R = getValue(FieldName);
792 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000793 throw "Record `" + getName() + "' does not have a field named `" +
794 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000795
796 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
797 return SI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000798 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000799 "' does not have a string initializer!";
800}
801
802/// getValueAsBitsInit - This method looks up the specified field and returns
803/// its value as a BitsInit, throwing an exception if the field does not exist
804/// or if the value is not the right type.
805///
806BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
807 const RecordVal *R = getValue(FieldName);
808 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000809 throw "Record `" + getName() + "' does not have a field named `" +
810 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000811
812 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
813 return BI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000814 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000815 "' does not have a BitsInit initializer!";
816}
817
818/// getValueAsListInit - This method looks up the specified field and returns
819/// its value as a ListInit, throwing an exception if the field does not exist
820/// or if the value is not the right type.
821///
822ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
823 const RecordVal *R = getValue(FieldName);
824 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000825 throw "Record `" + getName() + "' does not have a field named `" +
826 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000827
828 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
829 return LI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000830 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000831 "' does not have a list initializer!";
832}
833
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000834/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +0000835/// its value as a vector of records, throwing an exception if the field does
836/// not exist or if the value is not the right type.
837///
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000838std::vector<Record*>
839Record::getValueAsListOfDefs(const std::string &FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +0000840 ListInit *List = getValueAsListInit(FieldName);
841 std::vector<Record*> Defs;
842 for (unsigned i = 0; i < List->getSize(); i++) {
843 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
844 Defs.push_back(DI->getDef());
845 } else {
846 throw "Record `" + getName() + "', field `" + FieldName +
847 "' list is not entirely DefInit!";
848 }
849 }
850 return Defs;
851}
852
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000853/// getValueAsInt - This method looks up the specified field and returns its
854/// value as an int, throwing an exception if the field does not exist or if
855/// the value is not the right type.
856///
857int Record::getValueAsInt(const std::string &FieldName) const {
858 const RecordVal *R = getValue(FieldName);
859 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000860 throw "Record `" + getName() + "' does not have a field named `" +
861 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000862
863 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
864 return II->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000865 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +0000866 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000867}
868
869/// getValueAsDef - This method looks up the specified field and returns its
870/// value as a Record, throwing an exception if the field does not exist or if
871/// the value is not the right type.
872///
873Record *Record::getValueAsDef(const std::string &FieldName) const {
874 const RecordVal *R = getValue(FieldName);
875 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000876 throw "Record `" + getName() + "' does not have a field named `" +
877 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000878
879 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
880 return DI->getDef();
Misha Brukman41f9f292004-10-08 14:59:05 +0000881 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +0000882 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000883}
884
885/// getValueAsBit - This method looks up the specified field and returns its
886/// value as a bit, throwing an exception if the field does not exist or if
887/// the value is not the right type.
888///
889bool Record::getValueAsBit(const std::string &FieldName) const {
890 const RecordVal *R = getValue(FieldName);
891 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000892 throw "Record `" + getName() + "' does not have a field named `" +
893 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000894
895 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
896 return BI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000897 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000898 "' does not have a bit initializer!";
899}
900
901/// getValueAsDag - This method looks up the specified field and returns its
902/// value as an Dag, throwing an exception if the field does not exist or if
903/// the value is not the right type.
904///
905DagInit *Record::getValueAsDag(const std::string &FieldName) const {
906 const RecordVal *R = getValue(FieldName);
907 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000908 throw "Record `" + getName() + "' does not have a field named `" +
909 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000910
911 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
912 return DI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000913 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000914 "' does not have a dag initializer!";
915}
916
Chris Lattnerae939eb2005-09-13 21:44:28 +0000917std::string Record::getValueAsCode(const std::string &FieldName) const {
918 const RecordVal *R = getValue(FieldName);
919 if (R == 0 || R->getValue() == 0)
920 throw "Record `" + getName() + "' does not have a field named `" +
921 FieldName + "'!\n";
922
923 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
924 return CI->getValue();
925 throw "Record `" + getName() + "', field `" + FieldName +
926 "' does not have a code initializer!";
927}
928
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000929
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000930void RecordKeeper::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000931
Chris Lattner68478662004-08-01 03:55:39 +0000932std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000933 OS << "------------- Classes -----------------\n";
934 const std::map<std::string, Record*> &Classes = RK.getClasses();
935 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +0000936 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000937 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000938
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000939 OS << "------------- Defs -----------------\n";
940 const std::map<std::string, Record*> &Defs = RK.getDefs();
941 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +0000942 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000943 OS << "def " << *I->second;
944 return OS;
945}
946
947
948/// getAllDerivedDefinitions - This method returns all concrete definitions
949/// that derive from the specified class name. If a class with the specified
950/// name does not exist, an error is printed and true is returned.
951std::vector<Record*>
952RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
953 Record *Class = Records.getClass(ClassName);
954 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +0000955 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000956
957 std::vector<Record*> Defs;
958 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
959 E = getDefs().end(); I != E; ++I)
960 if (I->second->isSubClassOf(Class))
961 Defs.push_back(I->second);
962
963 return Defs;
964}
Brian Gaeke960707c2003-11-11 22:41:34 +0000965