blob: 47cab7b5db8b36e40d46f3b7f09332e4d438dfad [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//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Chris Lattner8b9ecda2007-11-20 22:25:16 +000017#include "llvm/ADT/StringExtras.h"
Duraid Madina14492af2005-12-26 05:08:55 +000018#include <ios>
David Greene07eba052009-06-08 17:00:34 +000019#include <sys/types.h>
20#include <regex.h>
Duraid Madina14492af2005-12-26 05:08:55 +000021
Chris Lattner68478662004-08-01 03:55:39 +000022using namespace llvm;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000023
24//===----------------------------------------------------------------------===//
25// Type implementations
26//===----------------------------------------------------------------------===//
27
Bill Wendling9bfb1e12006-12-07 22:21:48 +000028void RecTy::dump() const { print(*cerr.stream()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000029
30Init *BitRecTy::convertValue(BitsInit *BI) {
31 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
32 return BI->getBit(0);
33}
34
35bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
36 return RHS->getNumBits() == 1;
37}
38
39Init *BitRecTy::convertValue(IntInit *II) {
Dan Gohmanca0546f2008-10-17 01:33:43 +000040 int64_t Val = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000041 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
Misha Brukman650ba8e2005-04-22 00:00:37 +000042
43 return new BitInit(Val != 0);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000044}
45
46Init *BitRecTy::convertValue(TypedInit *VI) {
47 if (dynamic_cast<BitRecTy*>(VI->getType()))
48 return VI; // Accept variable if it is already of bit type!
49 return 0;
50}
51
Chris Lattner8b9ecda2007-11-20 22:25:16 +000052std::string BitsRecTy::getAsString() const {
53 return "bits<" + utostr(Size) + ">";
54}
55
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000056Init *BitsRecTy::convertValue(UnsetInit *UI) {
57 BitsInit *Ret = new BitsInit(Size);
58
59 for (unsigned i = 0; i != Size; ++i)
60 Ret->setBit(i, new UnsetInit());
61 return Ret;
62}
63
64Init *BitsRecTy::convertValue(BitInit *UI) {
65 if (Size != 1) return 0; // Can only convert single bit...
66 BitsInit *Ret = new BitsInit(1);
67 Ret->setBit(0, UI);
68 return Ret;
69}
70
71// convertValue from Int initializer to bits type: Split the integer up into the
72// appropriate bits...
73//
74Init *BitsRecTy::convertValue(IntInit *II) {
Misha Brukman6752fb52004-06-21 18:01:47 +000075 int64_t Value = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000076 // Make sure this bitfield is large enough to hold the integer value...
77 if (Value >= 0) {
Misha Brukman6752fb52004-06-21 18:01:47 +000078 if (Value & ~((1LL << Size)-1))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000079 return 0;
80 } else {
Jeff Cohen0add83e2006-02-18 03:20:33 +000081 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000082 return 0;
83 }
84
85 BitsInit *Ret = new BitsInit(Size);
86 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen0add83e2006-02-18 03:20:33 +000087 Ret->setBit(i, new BitInit(Value & (1LL << i)));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000088
89 return Ret;
90}
91
92Init *BitsRecTy::convertValue(BitsInit *BI) {
93 // If the number of bits is right, return it. Otherwise we need to expand or
94 // truncate...
95 if (BI->getNumBits() == Size) return BI;
96 return 0;
97}
98
99Init *BitsRecTy::convertValue(TypedInit *VI) {
100 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
101 if (BRT->Size == Size) {
102 BitsInit *Ret = new BitsInit(Size);
103 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen88e7b722005-04-22 04:13:13 +0000104 Ret->setBit(i, new VarBitInit(VI, i));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000105 return Ret;
106 }
107 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
108 BitsInit *Ret = new BitsInit(1);
109 Ret->setBit(0, VI);
110 return Ret;
111 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000112
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000113 return 0;
114}
115
116Init *IntRecTy::convertValue(BitInit *BI) {
117 return new IntInit(BI->getValue());
118}
119
120Init *IntRecTy::convertValue(BitsInit *BI) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000121 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000122 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000123 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
124 Result |= Bit->getValue() << i;
125 } else {
126 return 0;
127 }
128 return new IntInit(Result);
129}
130
131Init *IntRecTy::convertValue(TypedInit *TI) {
132 if (TI->getType()->typeIsConvertibleTo(this))
133 return TI; // Accept variable if already of the right type!
134 return 0;
135}
136
David Greenee8f3b272009-05-14 21:22:49 +0000137Init *StringRecTy::convertValue(UnOpInit *BO) {
138 if (BO->getOpcode() == UnOpInit::CAST) {
139 Init *L = BO->getOperand()->convertInitializerTo(this);
140 if (L == 0) return 0;
141 if (L != BO->getOperand())
142 return new UnOpInit(UnOpInit::CAST, L, new StringRecTy);
143 return BO;
144 }
David Greene5d0c0512009-05-14 20:54:48 +0000145
David Greenee8f3b272009-05-14 21:22:49 +0000146 return convertValue((TypedInit*)BO);
147}
David Greene5d0c0512009-05-14 20:54:48 +0000148
Chris Lattner51ffbf12006-03-31 21:53:49 +0000149Init *StringRecTy::convertValue(BinOpInit *BO) {
150 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
151 Init *L = BO->getLHS()->convertInitializerTo(this);
152 Init *R = BO->getRHS()->convertInitializerTo(this);
153 if (L == 0 || R == 0) return 0;
154 if (L != BO->getLHS() || R != BO->getRHS())
David Greene196ac3c2009-04-23 21:25:15 +0000155 return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000156 return BO;
157 }
David Greene196ac3c2009-04-23 21:25:15 +0000158 if (BO->getOpcode() == BinOpInit::NAMECONCAT) {
159 if (BO->getType()->getAsString() == getAsString()) {
160 Init *L = BO->getLHS()->convertInitializerTo(this);
161 Init *R = BO->getRHS()->convertInitializerTo(this);
162 if (L == 0 || R == 0) return 0;
163 if (L != BO->getLHS() || R != BO->getRHS())
164 return new BinOpInit(BinOpInit::NAMECONCAT, L, R, new StringRecTy);
165 return BO;
166 }
167 }
168
169 return convertValue((TypedInit*)BO);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000170}
171
172
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000173Init *StringRecTy::convertValue(TypedInit *TI) {
174 if (dynamic_cast<StringRecTy*>(TI->getType()))
175 return TI; // Accept variable if already of the right type!
176 return 0;
177}
178
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000179std::string ListRecTy::getAsString() const {
180 return "list<" + Ty->getAsString() + ">";
181}
182
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000183Init *ListRecTy::convertValue(ListInit *LI) {
184 std::vector<Init*> Elements;
185
186 // Verify that all of the elements of the list are subclasses of the
187 // appropriate class!
188 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
189 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
190 Elements.push_back(CI);
191 else
192 return 0;
193
194 return new ListInit(Elements);
195}
196
197Init *ListRecTy::convertValue(TypedInit *TI) {
198 // Ensure that TI is compatible with our class.
199 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
200 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
201 return TI;
202 return 0;
203}
204
205Init *CodeRecTy::convertValue(TypedInit *TI) {
206 if (TI->getType()->typeIsConvertibleTo(this))
207 return TI;
208 return 0;
209}
210
211Init *DagRecTy::convertValue(TypedInit *TI) {
212 if (TI->getType()->typeIsConvertibleTo(this))
213 return TI;
214 return 0;
215}
216
David Greenee8f3b272009-05-14 21:22:49 +0000217Init *DagRecTy::convertValue(UnOpInit *BO) {
218 if (BO->getOpcode() == UnOpInit::CAST) {
219 Init *L = BO->getOperand()->convertInitializerTo(this);
220 if (L == 0) return 0;
221 if (L != BO->getOperand())
222 return new UnOpInit(UnOpInit::CAST, L, new DagRecTy);
223 return BO;
224 }
225 return 0;
226}
David Greene5d0c0512009-05-14 20:54:48 +0000227
Evan Chenga32dee22007-05-15 01:23:24 +0000228Init *DagRecTy::convertValue(BinOpInit *BO) {
229 if (BO->getOpcode() == BinOpInit::CONCAT) {
230 Init *L = BO->getLHS()->convertInitializerTo(this);
231 Init *R = BO->getRHS()->convertInitializerTo(this);
232 if (L == 0 || R == 0) return 0;
233 if (L != BO->getLHS() || R != BO->getRHS())
David Greene196ac3c2009-04-23 21:25:15 +0000234 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
Evan Chenga32dee22007-05-15 01:23:24 +0000235 return BO;
236 }
David Greene196ac3c2009-04-23 21:25:15 +0000237 if (BO->getOpcode() == BinOpInit::NAMECONCAT) {
238 if (BO->getType()->getAsString() == getAsString()) {
239 Init *L = BO->getLHS()->convertInitializerTo(this);
240 Init *R = BO->getRHS()->convertInitializerTo(this);
241 if (L == 0 || R == 0) return 0;
242 if (L != BO->getLHS() || R != BO->getRHS())
243 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
244 return BO;
245 }
246 }
Evan Chenga32dee22007-05-15 01:23:24 +0000247 return 0;
248}
249
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000250std::string RecordRecTy::getAsString() const {
251 return Rec->getName();
252}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000253
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000254Init *RecordRecTy::convertValue(DefInit *DI) {
255 // Ensure that DI is a subclass of Rec.
256 if (!DI->getDef()->isSubClassOf(Rec))
257 return 0;
258 return DI;
259}
260
261Init *RecordRecTy::convertValue(TypedInit *TI) {
262 // Ensure that TI is compatible with Rec.
263 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
264 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
265 RRT->getRecord() == getRecord())
266 return TI;
267 return 0;
268}
269
270bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
271 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
272}
273
274
275//===----------------------------------------------------------------------===//
276// Initializer implementations
277//===----------------------------------------------------------------------===//
278
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000279void Init::dump() const { return print(*cerr.stream()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000280
281Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
282 BitsInit *BI = new BitsInit(Bits.size());
283 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
284 if (Bits[i] >= getNumBits()) {
285 delete BI;
286 return 0;
287 }
288 BI->setBit(i, getBit(Bits[i]));
289 }
290 return BI;
291}
292
Chris Lattner695506c2007-11-22 21:05:25 +0000293std::string BitsInit::getAsString() const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000294 //if (!printInHex(OS)) return;
295 //if (!printAsVariable(OS)) return;
296 //if (!printAsUnset(OS)) return;
297
Chris Lattner695506c2007-11-22 21:05:25 +0000298 std::string Result = "{ ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000299 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000300 if (i) Result += ", ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000301 if (Init *Bit = getBit(e-i-1))
Chris Lattner695506c2007-11-22 21:05:25 +0000302 Result += Bit->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000303 else
Chris Lattner695506c2007-11-22 21:05:25 +0000304 Result += "*";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000305 }
Chris Lattner695506c2007-11-22 21:05:25 +0000306 return Result + " }";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000307}
308
309bool BitsInit::printInHex(std::ostream &OS) const {
310 // First, attempt to convert the value into an integer value...
Dan Gohmanca0546f2008-10-17 01:33:43 +0000311 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000312 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000313 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
314 Result |= Bit->getValue() << i;
315 } else {
316 return true;
317 }
318
319 OS << "0x" << std::hex << Result << std::dec;
320 return false;
321}
322
323bool BitsInit::printAsVariable(std::ostream &OS) const {
324 // Get the variable that we may be set equal to...
325 assert(getNumBits() != 0);
326 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
327 if (FirstBit == 0) return true;
328 TypedInit *Var = FirstBit->getVariable();
329
330 // Check to make sure the types are compatible.
331 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
332 if (Ty == 0) return true;
333 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
334
335 // Check to make sure all bits are referring to the right bits in the variable
336 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
337 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
338 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
339 return true;
340 }
341
342 Var->print(OS);
343 return false;
344}
345
346bool BitsInit::printAsUnset(std::ostream &OS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000347 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000348 if (!dynamic_cast<UnsetInit*>(getBit(i)))
349 return true;
350 OS << "?";
351 return false;
352}
353
354// resolveReferences - If there are any field references that refer to fields
355// that have been filled in, we can propagate the values now.
356//
Chris Lattneref943742005-04-19 03:36:21 +0000357Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000358 bool Changed = false;
359 BitsInit *New = new BitsInit(getNumBits());
360
361 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
362 Init *B;
363 Init *CurBit = getBit(i);
364
365 do {
366 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000367 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000368 Changed |= B != CurBit;
369 } while (B != CurBit);
370 New->setBit(i, CurBit);
371 }
372
373 if (Changed)
374 return New;
375 delete New;
376 return this;
377}
378
Chris Lattner695506c2007-11-22 21:05:25 +0000379std::string IntInit::getAsString() const {
380 return itostr(Value);
381}
382
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000383Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
384 BitsInit *BI = new BitsInit(Bits.size());
385
386 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000387 if (Bits[i] >= 64) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000388 delete BI;
389 return 0;
390 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000391 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000392 }
393 return BI;
394}
395
Chris Lattner8bf9e062004-07-26 23:21:34 +0000396Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
397 std::vector<Init*> Vals;
398 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
399 if (Elements[i] >= getSize())
400 return 0;
401 Vals.push_back(getElement(Elements[i]));
402 }
403 return new ListInit(Vals);
404}
405
Chris Lattnercbebe462007-02-27 22:08:27 +0000406Record *ListInit::getElementAsRecord(unsigned i) const {
407 assert(i < Values.size() && "List element index out of range!");
408 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
409 if (DI == 0) throw "Expected record in list!";
410 return DI->getDef();
411}
412
Chris Lattneref943742005-04-19 03:36:21 +0000413Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000414 std::vector<Init*> Resolved;
415 Resolved.reserve(getSize());
416 bool Changed = false;
417
418 for (unsigned i = 0, e = getSize(); i != e; ++i) {
419 Init *E;
420 Init *CurElt = getElement(i);
421
422 do {
423 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000424 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000425 Changed |= E != CurElt;
426 } while (E != CurElt);
427 Resolved.push_back(E);
428 }
429
430 if (Changed)
431 return new ListInit(Resolved);
432 return this;
433}
434
Chris Lattner695506c2007-11-22 21:05:25 +0000435std::string ListInit::getAsString() const {
436 std::string Result = "[";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000437 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000438 if (i) Result += ", ";
439 Result += Values[i]->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000440 }
Chris Lattner695506c2007-11-22 21:05:25 +0000441 return Result + "]";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000442}
443
David Greene5d0c0512009-05-14 20:54:48 +0000444Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV,
445 unsigned Bit) {
446 Init *Folded = Fold(&R, 0);
447
448 if (Folded != this) {
449 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
450 if (Typed) {
451 return Typed->resolveBitReference(R, IRV, Bit);
452 }
453 }
454
455 return 0;
456}
457
458Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
459 unsigned Elt) {
460 Init *Folded = Fold(&R, 0);
461
462 if (Folded != this) {
463 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
464 if (Typed) {
465 return Typed->resolveListElementReference(R, IRV, Elt);
466 }
467 }
468
469 return 0;
470}
471
David Greenee8f3b272009-05-14 21:22:49 +0000472Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
473 switch (getOpcode()) {
474 default: assert(0 && "Unknown unop");
475 case CAST: {
476 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
477 if (LHSs) {
478 std::string Name = LHSs->getValue();
David Greene5d0c0512009-05-14 20:54:48 +0000479
David Greenee8f3b272009-05-14 21:22:49 +0000480 // From TGParser::ParseIDValue
481 if (CurRec) {
482 if (const RecordVal *RV = CurRec->getValue(Name)) {
483 if (RV->getType() != getType()) {
484 throw "type mismatch in nameconcat";
485 }
486 return new VarInit(Name, RV->getType());
487 }
David Greene5d0c0512009-05-14 20:54:48 +0000488
David Greenee8f3b272009-05-14 21:22:49 +0000489 std::string TemplateArgName = CurRec->getName()+":"+Name;
490 if (CurRec->isTemplateArg(TemplateArgName)) {
491 const RecordVal *RV = CurRec->getValue(TemplateArgName);
492 assert(RV && "Template arg doesn't exist??");
David Greene5d0c0512009-05-14 20:54:48 +0000493
David Greenee8f3b272009-05-14 21:22:49 +0000494 if (RV->getType() != getType()) {
495 throw "type mismatch in nameconcat";
496 }
David Greene5d0c0512009-05-14 20:54:48 +0000497
David Greenee8f3b272009-05-14 21:22:49 +0000498 return new VarInit(TemplateArgName, RV->getType());
499 }
500 }
David Greene5d0c0512009-05-14 20:54:48 +0000501
David Greenee8f3b272009-05-14 21:22:49 +0000502 if (CurMultiClass) {
503 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
504 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
505 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
506 assert(RV && "Template arg doesn't exist??");
David Greene5d0c0512009-05-14 20:54:48 +0000507
David Greenee8f3b272009-05-14 21:22:49 +0000508 if (RV->getType() != getType()) {
509 throw "type mismatch in nameconcat";
510 }
David Greene5d0c0512009-05-14 20:54:48 +0000511
David Greenee8f3b272009-05-14 21:22:49 +0000512 return new VarInit(MCName, RV->getType());
513 }
514 }
David Greene5d0c0512009-05-14 20:54:48 +0000515
David Greenee8f3b272009-05-14 21:22:49 +0000516 if (Record *D = Records.getDef(Name))
517 return new DefInit(D);
David Greene5d0c0512009-05-14 20:54:48 +0000518
David Greenee8f3b272009-05-14 21:22:49 +0000519 cerr << "Variable not defined: '" + Name + "'\n";
520 assert(0 && "Variable not found");
521 return 0;
522 }
523 break;
524 }
David Greened571b3c2009-05-14 22:38:31 +0000525 case CAR: {
526 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
527 if (LHSl) {
528 if (LHSl->getSize() == 0) {
529 assert(0 && "Empty list in car");
530 return 0;
531 }
532 return LHSl->getElement(0);
533 }
534 break;
535 }
536 case CDR: {
537 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
538 if (LHSl) {
539 if (LHSl->getSize() == 0) {
540 assert(0 && "Empty list in cdr");
541 return 0;
542 }
543 ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end());
544 return Result;
545 }
546 break;
547 }
548 case LNULL: {
549 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
550 if (LHSl) {
551 if (LHSl->getSize() == 0) {
552 return new IntInit(1);
553 }
554 else {
555 return new IntInit(0);
556 }
557 }
558 break;
559 }
David Greenee8f3b272009-05-14 21:22:49 +0000560 }
561 return this;
562}
David Greene5d0c0512009-05-14 20:54:48 +0000563
David Greenee8f3b272009-05-14 21:22:49 +0000564Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) {
565 Init *lhs = LHS->resolveReferences(R, RV);
David Greene5d0c0512009-05-14 20:54:48 +0000566
David Greenee8f3b272009-05-14 21:22:49 +0000567 if (LHS != lhs)
568 return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0);
569 return Fold(&R, 0);
570}
David Greene5d0c0512009-05-14 20:54:48 +0000571
David Greenee8f3b272009-05-14 21:22:49 +0000572std::string UnOpInit::getAsString() const {
573 std::string Result;
574 switch (Opc) {
575 case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
David Greened571b3c2009-05-14 22:38:31 +0000576 case CAR: Result = "!car"; break;
577 case CDR: Result = "!cdr"; break;
578 case LNULL: Result = "!null"; break;
David Greenee8f3b272009-05-14 21:22:49 +0000579 }
580 return Result + "(" + LHS->getAsString() + ")";
581}
David Greene5d0c0512009-05-14 20:54:48 +0000582
David Greenea9c6c5d2009-04-22 20:18:10 +0000583Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000584 switch (getOpcode()) {
585 default: assert(0 && "Unknown binop");
Evan Chenga32dee22007-05-15 01:23:24 +0000586 case CONCAT: {
587 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
588 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
589 if (LHSs && RHSs) {
590 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
591 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
Evan Cheng94b5a802007-07-19 01:14:50 +0000592 if (LOp->getDef() != ROp->getDef()) {
593 bool LIsOps =
594 LOp->getDef()->getName() == "outs" ||
595 LOp->getDef()->getName() != "ins" ||
596 LOp->getDef()->getName() != "defs";
597 bool RIsOps =
598 ROp->getDef()->getName() == "outs" ||
599 ROp->getDef()->getName() != "ins" ||
600 ROp->getDef()->getName() != "defs";
601 if (!LIsOps || !RIsOps)
602 throw "Concated Dag operators do not match!";
603 }
Evan Chenga32dee22007-05-15 01:23:24 +0000604 std::vector<Init*> Args;
605 std::vector<std::string> ArgNames;
606 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
607 Args.push_back(LHSs->getArg(i));
608 ArgNames.push_back(LHSs->getArgName(i));
609 }
610 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
611 Args.push_back(RHSs->getArg(i));
612 ArgNames.push_back(RHSs->getArgName(i));
613 }
Nate Begemandbe3f772009-03-19 05:21:56 +0000614 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
Evan Chenga32dee22007-05-15 01:23:24 +0000615 }
616 break;
617 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000618 case STRCONCAT: {
619 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
620 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
621 if (LHSs && RHSs)
622 return new StringInit(LHSs->getValue() + RHSs->getValue());
623 break;
624 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000625 case NAMECONCAT: {
626 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
627 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
628 if (LHSs && RHSs) {
629 std::string Name(LHSs->getValue() + RHSs->getValue());
630
631 // From TGParser::ParseIDValue
632 if (CurRec) {
David Greene196ac3c2009-04-23 21:25:15 +0000633 if (const RecordVal *RV = CurRec->getValue(Name)) {
634 if (RV->getType() != getType()) {
635 throw "type mismatch in nameconcat";
636 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000637 return new VarInit(Name, RV->getType());
David Greene196ac3c2009-04-23 21:25:15 +0000638 }
639
David Greenea9c6c5d2009-04-22 20:18:10 +0000640 std::string TemplateArgName = CurRec->getName()+":"+Name;
641 if (CurRec->isTemplateArg(TemplateArgName)) {
642 const RecordVal *RV = CurRec->getValue(TemplateArgName);
643 assert(RV && "Template arg doesn't exist??");
David Greene196ac3c2009-04-23 21:25:15 +0000644
645 if (RV->getType() != getType()) {
646 throw "type mismatch in nameconcat";
647 }
648
David Greenea9c6c5d2009-04-22 20:18:10 +0000649 return new VarInit(TemplateArgName, RV->getType());
650 }
651 }
652
653 if (CurMultiClass) {
654 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
655 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
656 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
657 assert(RV && "Template arg doesn't exist??");
David Greene196ac3c2009-04-23 21:25:15 +0000658
659 if (RV->getType() != getType()) {
660 throw "type mismatch in nameconcat";
661 }
662
David Greenea9c6c5d2009-04-22 20:18:10 +0000663 return new VarInit(MCName, RV->getType());
664 }
665 }
666
667 if (Record *D = Records.getDef(Name))
668 return new DefInit(D);
669
670 cerr << "Variable not defined: '" + Name + "'\n";
671 assert(0 && "Variable not found");
672 return 0;
673 }
674 break;
675 }
David Greene07eba052009-06-08 17:00:34 +0000676 case REGMATCH: {
677 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
678 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
679 if (LHSs && RHSs) {
680 regex_t compiled;
681 int err = regcomp (&compiled, LHSs->getValue().c_str(), REG_EXTENDED);
682 if (err != 0) {
683 size_t length = regerror (err, &compiled, NULL, 0);
684 char *buffer = new char[length];
685 (void) regerror (err, &compiled, buffer, length);
686 std::string errmsg = buffer;
687 delete[] buffer;
688 regfree(&compiled);
689 throw errmsg;
690 }
691 int result = regexec(&compiled, RHSs->getValue().c_str(), 0, NULL, 0);
692 if (result == REG_ESPACE) {
693 size_t length = regerror (err, &compiled, NULL, 0);
694 char *buffer = new char[length];
695 (void) regerror (err, &compiled, buffer, length);
696 std::string errmsg = buffer;
697 delete[] buffer;
698 regfree(&compiled);
699 throw errmsg;
700 }
701 regfree(&compiled);
702 return new IntInit(result == 0);
703 }
704 break;
705 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000706 case SHL:
707 case SRA:
708 case SRL: {
709 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
710 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
711 if (LHSi && RHSi) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000712 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
713 int64_t Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000714 switch (getOpcode()) {
715 default: assert(0 && "Bad opcode!");
716 case SHL: Result = LHSv << RHSv; break;
717 case SRA: Result = LHSv >> RHSv; break;
Dan Gohmanca0546f2008-10-17 01:33:43 +0000718 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000719 }
720 return new IntInit(Result);
721 }
722 break;
723 }
724 }
725 return this;
726}
727
728Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
729 Init *lhs = LHS->resolveReferences(R, RV);
730 Init *rhs = RHS->resolveReferences(R, RV);
731
732 if (LHS != lhs || RHS != rhs)
David Greene196ac3c2009-04-23 21:25:15 +0000733 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
David Greenea9c6c5d2009-04-22 20:18:10 +0000734 return Fold(&R, 0);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000735}
736
Chris Lattner695506c2007-11-22 21:05:25 +0000737std::string BinOpInit::getAsString() const {
738 std::string Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000739 switch (Opc) {
Chris Lattner695506c2007-11-22 21:05:25 +0000740 case CONCAT: Result = "!con"; break;
741 case SHL: Result = "!shl"; break;
742 case SRA: Result = "!sra"; break;
743 case SRL: Result = "!srl"; break;
744 case STRCONCAT: Result = "!strconcat"; break;
David Greene07eba052009-06-08 17:00:34 +0000745 case REGMATCH: Result = "!regmatch"; break;
David Greene196ac3c2009-04-23 21:25:15 +0000746 case NAMECONCAT:
747 Result = "!nameconcat<" + getType()->getAsString() + ">"; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000748 }
Chris Lattner695506c2007-11-22 21:05:25 +0000749 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
Chris Lattner51ffbf12006-03-31 21:53:49 +0000750}
751
David Greenee917fff2009-05-14 22:23:47 +0000752static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
753 Record *CurRec, MultiClass *CurMultiClass);
754
755static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
756 RecTy *Type, Record *CurRec,
757 MultiClass *CurMultiClass) {
758 std::vector<Init *> NewOperands;
759
760 TypedInit *TArg = dynamic_cast<TypedInit*>(Arg);
761
762 // If this is a dag, recurse
763 if (TArg && TArg->getType()->getAsString() == "dag") {
764 Init *Result = ForeachHelper(LHS, Arg, RHSo, Type,
765 CurRec, CurMultiClass);
766 if (Result != 0) {
767 return Result;
768 }
769 else {
770 return 0;
771 }
772 }
773
774 for (int i = 0; i < RHSo->getNumOperands(); ++i) {
775 OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i));
776
777 if (RHSoo) {
778 Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
779 Type, CurRec, CurMultiClass);
780 if (Result != 0) {
781 NewOperands.push_back(Result);
782 }
783 else {
784 NewOperands.push_back(Arg);
785 }
786 }
787 else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
788 NewOperands.push_back(Arg);
789 }
790 else {
791 NewOperands.push_back(RHSo->getOperand(i));
792 }
793 }
794
795 // Now run the operator and use its result as the new leaf
796 OpInit *NewOp = RHSo->clone(NewOperands);
797 Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
798 if (NewVal != NewOp) {
799 delete NewOp;
800 return NewVal;
801 }
802 return 0;
803}
804
805static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
806 Record *CurRec, MultiClass *CurMultiClass) {
807 DagInit *MHSd = dynamic_cast<DagInit*>(MHS);
808 ListInit *MHSl = dynamic_cast<ListInit*>(MHS);
809
810 DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type);
811 ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type);
812
813 OpInit *RHSo = dynamic_cast<OpInit*>(RHS);
814
815 if (!RHSo) {
816 cerr << "!foreach requires an operator\n";
817 assert(0 && "No operator for !foreach");
818 }
819
820 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
821
822 if (!LHSt) {
823 cerr << "!foreach requires typed variable\n";
824 assert(0 && "No typed variable for !foreach");
825 }
826
Nick Lewycky94298222009-05-15 03:07:14 +0000827 if ((MHSd && DagType) || (MHSl && ListType)) {
David Greenee917fff2009-05-14 22:23:47 +0000828 if (MHSd) {
829 Init *Val = MHSd->getOperator();
830 Init *Result = EvaluateOperation(RHSo, LHS, Val,
831 Type, CurRec, CurMultiClass);
832 if (Result != 0) {
833 Val = Result;
834 }
835
836 std::vector<std::pair<Init *, std::string> > args;
837 for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
838 Init *Arg;
839 std::string ArgName;
840 Arg = MHSd->getArg(i);
841 ArgName = MHSd->getArgName(i);
842
843 // Process args
844 Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
845 CurRec, CurMultiClass);
846 if (Result != 0) {
847 Arg = Result;
848 }
849
850 // TODO: Process arg names
851 args.push_back(std::make_pair(Arg, ArgName));
852 }
853
854 return new DagInit(Val, "", args);
855 }
856 if (MHSl) {
857 std::vector<Init *> NewOperands;
858 std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
859
860 for (ListInit::iterator li = NewList.begin(),
861 liend = NewList.end();
862 li != liend;
863 ++li) {
864 Init *Item = *li;
865 NewOperands.clear();
866 for(int i = 0; i < RHSo->getNumOperands(); ++i) {
867 // First, replace the foreach variable with the list item
868 if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
869 NewOperands.push_back(Item);
870 }
871 else {
872 NewOperands.push_back(RHSo->getOperand(i));
873 }
874 }
875
876 // Now run the operator and use its result as the new list item
877 OpInit *NewOp = RHSo->clone(NewOperands);
878 Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
879 if (NewItem != NewOp) {
880 *li = NewItem;
881 delete NewOp;
882 }
883 }
884 return new ListInit(NewList);
885 }
886 }
887 return 0;
888}
889
David Greene98ed3c72009-05-14 21:54:42 +0000890Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
891 switch (getOpcode()) {
892 default: assert(0 && "Unknown binop");
893 case SUBST: {
894 DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
895 VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
896 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene196ac3c2009-04-23 21:25:15 +0000897
David Greene98ed3c72009-05-14 21:54:42 +0000898 DefInit *MHSd = dynamic_cast<DefInit*>(MHS);
899 VarInit *MHSv = dynamic_cast<VarInit*>(MHS);
900 StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
David Greene5d0c0512009-05-14 20:54:48 +0000901
David Greene98ed3c72009-05-14 21:54:42 +0000902 DefInit *RHSd = dynamic_cast<DefInit*>(RHS);
903 VarInit *RHSv = dynamic_cast<VarInit*>(RHS);
904 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
David Greene5d0c0512009-05-14 20:54:48 +0000905
David Greene98ed3c72009-05-14 21:54:42 +0000906 if ((LHSd && MHSd && RHSd)
907 || (LHSv && MHSv && RHSv)
908 || (LHSs && MHSs && RHSs)) {
909 if (RHSd) {
910 Record *Val = RHSd->getDef();
911 if (LHSd->getAsString() == RHSd->getAsString()) {
912 Val = MHSd->getDef();
913 }
914 return new DefInit(Val);
915 }
916 if (RHSv) {
917 std::string Val = RHSv->getName();
918 if (LHSv->getAsString() == RHSv->getAsString()) {
919 Val = MHSv->getName();
920 }
921 return new VarInit(Val, getType());
922 }
923 if (RHSs) {
924 std::string Val = RHSs->getValue();
David Greene5d0c0512009-05-14 20:54:48 +0000925
David Greene98ed3c72009-05-14 21:54:42 +0000926 std::string::size_type found;
927 do {
928 found = Val.find(LHSs->getValue());
929 if (found != std::string::npos) {
930 Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
931 }
932 } while (found != std::string::npos);
David Greene5d0c0512009-05-14 20:54:48 +0000933
David Greene98ed3c72009-05-14 21:54:42 +0000934 return new StringInit(Val);
935 }
936 }
937 break;
938 }
David Greene5d0c0512009-05-14 20:54:48 +0000939
David Greene98ed3c72009-05-14 21:54:42 +0000940 case FOREACH: {
David Greenee917fff2009-05-14 22:23:47 +0000941 Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
942 CurRec, CurMultiClass);
943 if (Result != 0) {
944 return Result;
David Greene98ed3c72009-05-14 21:54:42 +0000945 }
946 break;
947 }
David Greene3587eed2009-05-14 23:26:46 +0000948
949 case IF: {
950 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
951 if (LHSi) {
952 if (LHSi->getValue()) {
953 return MHS;
954 }
955 else {
956 return RHS;
957 }
958 }
959 break;
960 }
David Greene98ed3c72009-05-14 21:54:42 +0000961 }
David Greene5d0c0512009-05-14 20:54:48 +0000962
David Greene98ed3c72009-05-14 21:54:42 +0000963 return this;
964}
David Greene5d0c0512009-05-14 20:54:48 +0000965
David Greene98ed3c72009-05-14 21:54:42 +0000966Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) {
967 Init *lhs = LHS->resolveReferences(R, RV);
968 Init *mhs = MHS->resolveReferences(R, RV);
969 Init *rhs = RHS->resolveReferences(R, RV);
David Greene196ac3c2009-04-23 21:25:15 +0000970
David Greene98ed3c72009-05-14 21:54:42 +0000971 if (LHS != lhs || MHS != mhs || RHS != rhs)
972 return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0);
973 return Fold(&R, 0);
974}
David Greene196ac3c2009-04-23 21:25:15 +0000975
David Greene98ed3c72009-05-14 21:54:42 +0000976std::string TernOpInit::getAsString() const {
977 std::string Result;
978 switch (Opc) {
979 case SUBST: Result = "!subst"; break;
980 case FOREACH: Result = "!foreach"; break;
David Greene3587eed2009-05-14 23:26:46 +0000981 case IF: Result = "!if"; break;
David Greene98ed3c72009-05-14 21:54:42 +0000982 }
983 return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
984 + RHS->getAsString() + ")";
985}
David Greene196ac3c2009-04-23 21:25:15 +0000986
Chris Lattner577fc3f2004-07-27 01:01:21 +0000987Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000988 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
989 if (T == 0) return 0; // Cannot subscript a non-bits variable...
990 unsigned NumBits = T->getNumBits();
991
992 BitsInit *BI = new BitsInit(Bits.size());
993 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
994 if (Bits[i] >= NumBits) {
995 delete BI;
996 return 0;
997 }
998 BI->setBit(i, new VarBitInit(this, Bits[i]));
999 }
1000 return BI;
1001}
1002
Chris Lattner577fc3f2004-07-27 01:01:21 +00001003Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
1004 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
1005 if (T == 0) return 0; // Cannot subscript a non-list variable...
1006
1007 if (Elements.size() == 1)
1008 return new VarListElementInit(this, Elements[0]);
1009
1010 std::vector<Init*> ListInits;
1011 ListInits.reserve(Elements.size());
1012 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
1013 ListInits.push_back(new VarListElementInit(this, Elements[i]));
1014 return new ListInit(ListInits);
1015}
1016
1017
Chris Lattneref943742005-04-19 03:36:21 +00001018Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
1019 unsigned Bit) {
1020 if (R.isTemplateArg(getName())) return 0;
1021 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001022
1023 RecordVal *RV = R.getValue(getName());
1024 assert(RV && "Reference to a non-existant variable?");
1025 assert(dynamic_cast<BitsInit*>(RV->getValue()));
1026 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +00001027
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001028 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1029 Init *B = BI->getBit(Bit);
1030
1031 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
1032 return B; // Replace the VarBitInit with it.
Chris Lattner577fc3f2004-07-27 01:01:21 +00001033 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001034}
1035
Chris Lattneref943742005-04-19 03:36:21 +00001036Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
1037 unsigned Elt) {
1038 if (R.isTemplateArg(getName())) return 0;
1039 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +00001040
1041 RecordVal *RV = R.getValue(getName());
1042 assert(RV && "Reference to a non-existant variable?");
1043 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
David Greene9d3febe2009-05-14 20:38:52 +00001044 if (!LI) {
1045 VarInit *VI = dynamic_cast<VarInit*>(RV->getValue());
1046 assert(VI && "Invalid list element!");
1047 return new VarListElementInit(VI, Elt);
1048 }
1049
Chris Lattner577fc3f2004-07-27 01:01:21 +00001050 if (Elt >= LI->getSize())
1051 return 0; // Out of range reference.
1052 Init *E = LI->getElement(Elt);
1053 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
1054 return E; // Replace the VarListElementInit with it.
1055 return 0;
1056}
1057
1058
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001059RecTy *VarInit::getFieldType(const std::string &FieldName) const {
1060 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
1061 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
1062 return RV->getType();
1063 return 0;
1064}
1065
1066Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +00001067 if (dynamic_cast<RecordRecTy*>(getType()))
Chris Lattnerd959ab92004-02-28 16:31:53 +00001068 if (const RecordVal *RV = R.getValue(VarName)) {
1069 Init *TheInit = RV->getValue();
1070 assert(TheInit != this && "Infinite loop detected!");
1071 if (Init *I = TheInit->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001072 return I;
1073 else
1074 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +00001075 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001076 return 0;
1077}
1078
1079/// resolveReferences - This method is used by classes that refer to other
1080/// variables which may not be defined at the time they expression is formed.
1081/// If a value is set for the variable later, this method will be called on
1082/// users of the value to allow the value to propagate out.
1083///
Chris Lattneref943742005-04-19 03:36:21 +00001084Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001085 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +00001086 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001087 return Val->getValue();
1088 return this;
1089}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001090
Chris Lattner695506c2007-11-22 21:05:25 +00001091std::string VarBitInit::getAsString() const {
1092 return TI->getAsString() + "{" + utostr(Bit) + "}";
1093}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001094
Chris Lattneref943742005-04-19 03:36:21 +00001095Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
1096 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001097 return I;
1098 return this;
1099}
1100
Chris Lattner695506c2007-11-22 21:05:25 +00001101std::string VarListElementInit::getAsString() const {
1102 return TI->getAsString() + "[" + utostr(Element) + "]";
1103}
1104
Chris Lattneref943742005-04-19 03:36:21 +00001105Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
1106 if (Init *I = getVariable()->resolveListElementReference(R, RV,
1107 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +00001108 return I;
1109 return this;
1110}
1111
Chris Lattneref943742005-04-19 03:36:21 +00001112Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
1113 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001114 // FIXME: This should be implemented, to support references like:
1115 // bit B = AA[0]{1};
1116 return 0;
1117}
1118
Chris Lattneref943742005-04-19 03:36:21 +00001119Init *VarListElementInit::
1120resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001121 // FIXME: This should be implemented, to support references like:
1122 // int B = AA[0][1];
1123 return 0;
1124}
1125
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001126RecTy *DefInit::getFieldType(const std::string &FieldName) const {
1127 if (const RecordVal *RV = Def->getValue(FieldName))
1128 return RV->getType();
1129 return 0;
1130}
1131
1132Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
1133 return Def->getValue(FieldName)->getValue();
1134}
1135
1136
Chris Lattner695506c2007-11-22 21:05:25 +00001137std::string DefInit::getAsString() const {
1138 return Def->getName();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001139}
1140
Chris Lattneref943742005-04-19 03:36:21 +00001141Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
1142 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001143 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001144 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
1145 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
1146 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +00001147
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001148 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
1149 return B; // Replace the VarBitInit with it.
1150 }
Chris Lattner577fc3f2004-07-27 01:01:21 +00001151 return 0;
1152}
1153
Chris Lattneref943742005-04-19 03:36:21 +00001154Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
1155 unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +00001156 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
1157 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
1158 if (Elt >= LI->getSize()) return 0;
1159 Init *E = LI->getElement(Elt);
1160
1161 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
1162 return E; // Replace the VarListElementInit with it.
1163 }
1164 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001165}
1166
Chris Lattneref943742005-04-19 03:36:21 +00001167Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
1168 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
1169
1170 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001171 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +00001172 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001173 return BVR->isComplete() ? BVR : this;
1174 }
Chris Lattneref943742005-04-19 03:36:21 +00001175
1176 if (NewRec != Rec) {
Chris Lattneref943742005-04-19 03:36:21 +00001177 return new FieldInit(NewRec, FieldName);
1178 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001179 return this;
1180}
1181
Chris Lattner0d3ef402006-01-31 06:02:35 +00001182Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
1183 std::vector<Init*> NewArgs;
1184 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1185 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
1186
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001187 Init *Op = Val->resolveReferences(R, RV);
1188
1189 if (Args != NewArgs || Op != Val)
Nate Begemandbe3f772009-03-19 05:21:56 +00001190 return new DagInit(Op, "", NewArgs, ArgNames);
Chris Lattner0d3ef402006-01-31 06:02:35 +00001191
1192 return this;
1193}
1194
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001195
Chris Lattner695506c2007-11-22 21:05:25 +00001196std::string DagInit::getAsString() const {
1197 std::string Result = "(" + Val->getAsString();
Nate Begemandbe3f772009-03-19 05:21:56 +00001198 if (!ValName.empty())
1199 Result += ":" + ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001200 if (Args.size()) {
Chris Lattner695506c2007-11-22 21:05:25 +00001201 Result += " " + Args[0]->getAsString();
1202 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001203 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +00001204 Result += ", " + Args[i]->getAsString();
1205 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001206 }
1207 }
Chris Lattner695506c2007-11-22 21:05:25 +00001208 return Result + ")";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001209}
1210
1211
1212//===----------------------------------------------------------------------===//
1213// Other implementations
1214//===----------------------------------------------------------------------===//
1215
1216RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
1217 : Name(N), Ty(T), Prefix(P) {
1218 Value = Ty->convertValue(new UnsetInit());
1219 assert(Value && "Cannot create unset value for current type!");
1220}
1221
Bill Wendling9bfb1e12006-12-07 22:21:48 +00001222void RecordVal::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001223
1224void RecordVal::print(std::ostream &OS, bool PrintSem) const {
1225 if (getPrefix()) OS << "field ";
1226 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +00001227
1228 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001229 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +00001230
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001231 if (PrintSem) OS << ";\n";
1232}
1233
Chris Lattnerac284252005-08-19 17:58:11 +00001234void Record::setName(const std::string &Name) {
1235 if (Records.getDef(getName()) == this) {
1236 Records.removeDef(getName());
1237 this->Name = Name;
1238 Records.addDef(this);
1239 } else {
1240 Records.removeClass(getName());
1241 this->Name = Name;
1242 Records.addClass(this);
1243 }
1244}
1245
Chris Lattneref943742005-04-19 03:36:21 +00001246/// resolveReferencesTo - If anything in this record refers to RV, replace the
1247/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
1248/// references.
1249void Record::resolveReferencesTo(const RecordVal *RV) {
1250 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1251 if (Init *V = Values[i].getValue())
1252 Values[i].setValue(V->resolveReferences(*this, RV));
1253 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001254}
1255
Chris Lattneref943742005-04-19 03:36:21 +00001256
Bill Wendling9bfb1e12006-12-07 22:21:48 +00001257void Record::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001258
Chris Lattner68478662004-08-01 03:55:39 +00001259std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001260 OS << R.getName();
1261
1262 const std::vector<std::string> &TArgs = R.getTemplateArgs();
1263 if (!TArgs.empty()) {
1264 OS << "<";
1265 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1266 if (i) OS << ", ";
1267 const RecordVal *RV = R.getValue(TArgs[i]);
1268 assert(RV && "Template argument record not found??");
1269 RV->print(OS, false);
1270 }
1271 OS << ">";
1272 }
1273
1274 OS << " {";
1275 const std::vector<Record*> &SC = R.getSuperClasses();
1276 if (!SC.empty()) {
1277 OS << "\t//";
1278 for (unsigned i = 0, e = SC.size(); i != e; ++i)
1279 OS << " " << SC[i]->getName();
1280 }
1281 OS << "\n";
1282
1283 const std::vector<RecordVal> &Vals = R.getValues();
1284 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1285 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1286 OS << Vals[i];
1287 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
1288 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
1289 OS << Vals[i];
1290
1291 return OS << "}\n";
1292}
1293
1294/// getValueInit - Return the initializer for a value with the specified name,
1295/// or throw an exception if the field does not exist.
1296///
1297Init *Record::getValueInit(const std::string &FieldName) const {
1298 const RecordVal *R = getValue(FieldName);
1299 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001300 throw "Record `" + getName() + "' does not have a field named `" +
1301 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001302 return R->getValue();
1303}
1304
1305
1306/// getValueAsString - This method looks up the specified field and returns its
1307/// value as a string, throwing an exception if the field does not exist or if
1308/// the value is not a string.
1309///
1310std::string Record::getValueAsString(const std::string &FieldName) const {
1311 const RecordVal *R = getValue(FieldName);
1312 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001313 throw "Record `" + getName() + "' does not have a field named `" +
1314 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001315
1316 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
1317 return SI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +00001318 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001319 "' does not have a string initializer!";
1320}
1321
1322/// getValueAsBitsInit - This method looks up the specified field and returns
1323/// its value as a BitsInit, throwing an exception if the field does not exist
1324/// or if the value is not the right type.
1325///
1326BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
1327 const RecordVal *R = getValue(FieldName);
1328 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001329 throw "Record `" + getName() + "' does not have a field named `" +
1330 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001331
1332 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
1333 return BI;
Misha Brukman41f9f292004-10-08 14:59:05 +00001334 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001335 "' does not have a BitsInit initializer!";
1336}
1337
1338/// getValueAsListInit - This method looks up the specified field and returns
1339/// its value as a ListInit, throwing an exception if the field does not exist
1340/// or if the value is not the right type.
1341///
1342ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
1343 const RecordVal *R = getValue(FieldName);
1344 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001345 throw "Record `" + getName() + "' does not have a field named `" +
1346 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001347
1348 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
1349 return LI;
Misha Brukman41f9f292004-10-08 14:59:05 +00001350 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001351 "' does not have a list initializer!";
1352}
1353
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001354/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +00001355/// its value as a vector of records, throwing an exception if the field does
1356/// not exist or if the value is not the right type.
1357///
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001358std::vector<Record*>
1359Record::getValueAsListOfDefs(const std::string &FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +00001360 ListInit *List = getValueAsListInit(FieldName);
1361 std::vector<Record*> Defs;
1362 for (unsigned i = 0; i < List->getSize(); i++) {
1363 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
1364 Defs.push_back(DI->getDef());
1365 } else {
1366 throw "Record `" + getName() + "', field `" + FieldName +
1367 "' list is not entirely DefInit!";
1368 }
1369 }
1370 return Defs;
1371}
1372
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001373/// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001374/// value as an int64_t, throwing an exception if the field does not exist or if
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001375/// the value is not the right type.
1376///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001377int64_t Record::getValueAsInt(const std::string &FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001378 const RecordVal *R = getValue(FieldName);
1379 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001380 throw "Record `" + getName() + "' does not have a field named `" +
1381 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001382
1383 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
1384 return II->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +00001385 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +00001386 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001387}
1388
Anton Korobeynikova468a112007-11-11 11:19:37 +00001389/// getValueAsListOfInts - This method looks up the specified field and returns
1390/// its value as a vector of integers, throwing an exception if the field does
1391/// not exist or if the value is not the right type.
1392///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001393std::vector<int64_t>
Anton Korobeynikova468a112007-11-11 11:19:37 +00001394Record::getValueAsListOfInts(const std::string &FieldName) const {
1395 ListInit *List = getValueAsListInit(FieldName);
Dan Gohmanca0546f2008-10-17 01:33:43 +00001396 std::vector<int64_t> Ints;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001397 for (unsigned i = 0; i < List->getSize(); i++) {
1398 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
1399 Ints.push_back(II->getValue());
1400 } else {
1401 throw "Record `" + getName() + "', field `" + FieldName +
1402 "' does not have a list of ints initializer!";
1403 }
1404 }
1405 return Ints;
1406}
1407
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001408/// getValueAsDef - This method looks up the specified field and returns its
1409/// value as a Record, throwing an exception if the field does not exist or if
1410/// the value is not the right type.
1411///
1412Record *Record::getValueAsDef(const std::string &FieldName) const {
1413 const RecordVal *R = getValue(FieldName);
1414 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001415 throw "Record `" + getName() + "' does not have a field named `" +
1416 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001417
1418 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1419 return DI->getDef();
Misha Brukman41f9f292004-10-08 14:59:05 +00001420 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +00001421 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001422}
1423
1424/// getValueAsBit - This method looks up the specified field and returns its
1425/// value as a bit, throwing an exception if the field does not exist or if
1426/// the value is not the right type.
1427///
1428bool Record::getValueAsBit(const std::string &FieldName) const {
1429 const RecordVal *R = getValue(FieldName);
1430 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001431 throw "Record `" + getName() + "' does not have a field named `" +
1432 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001433
1434 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1435 return BI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +00001436 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001437 "' does not have a bit initializer!";
1438}
1439
1440/// getValueAsDag - This method looks up the specified field and returns its
1441/// value as an Dag, throwing an exception if the field does not exist or if
1442/// the value is not the right type.
1443///
1444DagInit *Record::getValueAsDag(const std::string &FieldName) const {
1445 const RecordVal *R = getValue(FieldName);
1446 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001447 throw "Record `" + getName() + "' does not have a field named `" +
1448 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001449
1450 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1451 return DI;
Misha Brukman41f9f292004-10-08 14:59:05 +00001452 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001453 "' does not have a dag initializer!";
1454}
1455
Chris Lattnerae939eb2005-09-13 21:44:28 +00001456std::string Record::getValueAsCode(const std::string &FieldName) const {
1457 const RecordVal *R = getValue(FieldName);
1458 if (R == 0 || R->getValue() == 0)
1459 throw "Record `" + getName() + "' does not have a field named `" +
1460 FieldName + "'!\n";
1461
1462 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1463 return CI->getValue();
1464 throw "Record `" + getName() + "', field `" + FieldName +
1465 "' does not have a code initializer!";
1466}
1467
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001468
David Greene7049e792009-04-24 16:55:41 +00001469void MultiClass::dump() const {
1470 cerr << "Record:\n";
1471 Rec.dump();
1472
1473 cerr << "Defs:\n";
1474 for (RecordVector::const_iterator r = DefPrototypes.begin(),
1475 rend = DefPrototypes.end();
1476 r != rend;
1477 ++r) {
1478 (*r)->dump();
1479 }
1480}
1481
1482
Bill Wendling9bfb1e12006-12-07 22:21:48 +00001483void RecordKeeper::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001484
Chris Lattner68478662004-08-01 03:55:39 +00001485std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001486 OS << "------------- Classes -----------------\n";
1487 const std::map<std::string, Record*> &Classes = RK.getClasses();
1488 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001489 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001490 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001491
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001492 OS << "------------- Defs -----------------\n";
1493 const std::map<std::string, Record*> &Defs = RK.getDefs();
1494 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001495 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001496 OS << "def " << *I->second;
1497 return OS;
1498}
1499
1500
1501/// getAllDerivedDefinitions - This method returns all concrete definitions
1502/// that derive from the specified class name. If a class with the specified
1503/// name does not exist, an error is printed and true is returned.
1504std::vector<Record*>
1505RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1506 Record *Class = Records.getClass(ClassName);
1507 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +00001508 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001509
1510 std::vector<Record*> Defs;
1511 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1512 E = getDefs().end(); I != E; ++I)
1513 if (I->second->isSubClassOf(Class))
1514 Defs.push_back(I->second);
1515
1516 return Defs;
1517}
Brian Gaeke960707c2003-11-11 22:41:34 +00001518