blob: b24421999559bfeefdb565308115f9aa5d442239 [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>
19
Chris Lattner68478662004-08-01 03:55:39 +000020using namespace llvm;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000021
22//===----------------------------------------------------------------------===//
23// Type implementations
24//===----------------------------------------------------------------------===//
25
Bill Wendling9bfb1e12006-12-07 22:21:48 +000026void RecTy::dump() const { print(*cerr.stream()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000027
28Init *BitRecTy::convertValue(BitsInit *BI) {
29 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
30 return BI->getBit(0);
31}
32
33bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
34 return RHS->getNumBits() == 1;
35}
36
37Init *BitRecTy::convertValue(IntInit *II) {
Dan Gohmanca0546f2008-10-17 01:33:43 +000038 int64_t Val = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000039 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
Misha Brukman650ba8e2005-04-22 00:00:37 +000040
41 return new BitInit(Val != 0);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000042}
43
44Init *BitRecTy::convertValue(TypedInit *VI) {
45 if (dynamic_cast<BitRecTy*>(VI->getType()))
46 return VI; // Accept variable if it is already of bit type!
47 return 0;
48}
49
Chris Lattner8b9ecda2007-11-20 22:25:16 +000050std::string BitsRecTy::getAsString() const {
51 return "bits<" + utostr(Size) + ">";
52}
53
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000054Init *BitsRecTy::convertValue(UnsetInit *UI) {
55 BitsInit *Ret = new BitsInit(Size);
56
57 for (unsigned i = 0; i != Size; ++i)
58 Ret->setBit(i, new UnsetInit());
59 return Ret;
60}
61
62Init *BitsRecTy::convertValue(BitInit *UI) {
63 if (Size != 1) return 0; // Can only convert single bit...
64 BitsInit *Ret = new BitsInit(1);
65 Ret->setBit(0, UI);
66 return Ret;
67}
68
69// convertValue from Int initializer to bits type: Split the integer up into the
70// appropriate bits...
71//
72Init *BitsRecTy::convertValue(IntInit *II) {
Misha Brukman6752fb52004-06-21 18:01:47 +000073 int64_t Value = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000074 // Make sure this bitfield is large enough to hold the integer value...
75 if (Value >= 0) {
Misha Brukman6752fb52004-06-21 18:01:47 +000076 if (Value & ~((1LL << Size)-1))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000077 return 0;
78 } else {
Jeff Cohen0add83e2006-02-18 03:20:33 +000079 if ((Value >> Size) != -1 || ((Value & (1LL << (Size-1))) == 0))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000080 return 0;
81 }
82
83 BitsInit *Ret = new BitsInit(Size);
84 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen0add83e2006-02-18 03:20:33 +000085 Ret->setBit(i, new BitInit(Value & (1LL << i)));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000086
87 return Ret;
88}
89
90Init *BitsRecTy::convertValue(BitsInit *BI) {
91 // If the number of bits is right, return it. Otherwise we need to expand or
92 // truncate...
93 if (BI->getNumBits() == Size) return BI;
94 return 0;
95}
96
97Init *BitsRecTy::convertValue(TypedInit *VI) {
98 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
99 if (BRT->Size == Size) {
100 BitsInit *Ret = new BitsInit(Size);
101 for (unsigned i = 0; i != Size; ++i)
Jeff Cohen88e7b722005-04-22 04:13:13 +0000102 Ret->setBit(i, new VarBitInit(VI, i));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000103 return Ret;
104 }
105 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
106 BitsInit *Ret = new BitsInit(1);
107 Ret->setBit(0, VI);
108 return Ret;
109 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000110
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000111 return 0;
112}
113
114Init *IntRecTy::convertValue(BitInit *BI) {
115 return new IntInit(BI->getValue());
116}
117
118Init *IntRecTy::convertValue(BitsInit *BI) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000119 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000120 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000121 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
122 Result |= Bit->getValue() << i;
123 } else {
124 return 0;
125 }
126 return new IntInit(Result);
127}
128
129Init *IntRecTy::convertValue(TypedInit *TI) {
130 if (TI->getType()->typeIsConvertibleTo(this))
131 return TI; // Accept variable if already of the right type!
132 return 0;
133}
134
Chris Lattner51ffbf12006-03-31 21:53:49 +0000135Init *StringRecTy::convertValue(BinOpInit *BO) {
136 if (BO->getOpcode() == BinOpInit::STRCONCAT) {
137 Init *L = BO->getLHS()->convertInitializerTo(this);
138 Init *R = BO->getRHS()->convertInitializerTo(this);
139 if (L == 0 || R == 0) return 0;
140 if (L != BO->getLHS() || R != BO->getRHS())
141 return new BinOpInit(BinOpInit::STRCONCAT, L, R);
142 return BO;
143 }
144 return 0;
145}
146
147
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000148Init *StringRecTy::convertValue(TypedInit *TI) {
149 if (dynamic_cast<StringRecTy*>(TI->getType()))
150 return TI; // Accept variable if already of the right type!
151 return 0;
152}
153
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000154std::string ListRecTy::getAsString() const {
155 return "list<" + Ty->getAsString() + ">";
156}
157
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000158Init *ListRecTy::convertValue(ListInit *LI) {
159 std::vector<Init*> Elements;
160
161 // Verify that all of the elements of the list are subclasses of the
162 // appropriate class!
163 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
164 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
165 Elements.push_back(CI);
166 else
167 return 0;
168
169 return new ListInit(Elements);
170}
171
172Init *ListRecTy::convertValue(TypedInit *TI) {
173 // Ensure that TI is compatible with our class.
174 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
175 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
176 return TI;
177 return 0;
178}
179
180Init *CodeRecTy::convertValue(TypedInit *TI) {
181 if (TI->getType()->typeIsConvertibleTo(this))
182 return TI;
183 return 0;
184}
185
186Init *DagRecTy::convertValue(TypedInit *TI) {
187 if (TI->getType()->typeIsConvertibleTo(this))
188 return TI;
189 return 0;
190}
191
Evan Chenga32dee22007-05-15 01:23:24 +0000192Init *DagRecTy::convertValue(BinOpInit *BO) {
193 if (BO->getOpcode() == BinOpInit::CONCAT) {
194 Init *L = BO->getLHS()->convertInitializerTo(this);
195 Init *R = BO->getRHS()->convertInitializerTo(this);
196 if (L == 0 || R == 0) return 0;
197 if (L != BO->getLHS() || R != BO->getRHS())
198 return new BinOpInit(BinOpInit::CONCAT, L, R);
199 return BO;
200 }
201 return 0;
202}
203
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000204std::string RecordRecTy::getAsString() const {
205 return Rec->getName();
206}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000207
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000208Init *RecordRecTy::convertValue(DefInit *DI) {
209 // Ensure that DI is a subclass of Rec.
210 if (!DI->getDef()->isSubClassOf(Rec))
211 return 0;
212 return DI;
213}
214
215Init *RecordRecTy::convertValue(TypedInit *TI) {
216 // Ensure that TI is compatible with Rec.
217 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
218 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
219 RRT->getRecord() == getRecord())
220 return TI;
221 return 0;
222}
223
224bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
225 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
226}
227
228
229//===----------------------------------------------------------------------===//
230// Initializer implementations
231//===----------------------------------------------------------------------===//
232
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000233void Init::dump() const { return print(*cerr.stream()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000234
235Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
236 BitsInit *BI = new BitsInit(Bits.size());
237 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
238 if (Bits[i] >= getNumBits()) {
239 delete BI;
240 return 0;
241 }
242 BI->setBit(i, getBit(Bits[i]));
243 }
244 return BI;
245}
246
Chris Lattner695506c2007-11-22 21:05:25 +0000247std::string BitsInit::getAsString() const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000248 //if (!printInHex(OS)) return;
249 //if (!printAsVariable(OS)) return;
250 //if (!printAsUnset(OS)) return;
251
Chris Lattner695506c2007-11-22 21:05:25 +0000252 std::string Result = "{ ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000253 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000254 if (i) Result += ", ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000255 if (Init *Bit = getBit(e-i-1))
Chris Lattner695506c2007-11-22 21:05:25 +0000256 Result += Bit->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000257 else
Chris Lattner695506c2007-11-22 21:05:25 +0000258 Result += "*";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000259 }
Chris Lattner695506c2007-11-22 21:05:25 +0000260 return Result + " }";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000261}
262
263bool BitsInit::printInHex(std::ostream &OS) const {
264 // First, attempt to convert the value into an integer value...
Dan Gohmanca0546f2008-10-17 01:33:43 +0000265 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000266 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000267 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
268 Result |= Bit->getValue() << i;
269 } else {
270 return true;
271 }
272
273 OS << "0x" << std::hex << Result << std::dec;
274 return false;
275}
276
277bool BitsInit::printAsVariable(std::ostream &OS) const {
278 // Get the variable that we may be set equal to...
279 assert(getNumBits() != 0);
280 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
281 if (FirstBit == 0) return true;
282 TypedInit *Var = FirstBit->getVariable();
283
284 // Check to make sure the types are compatible.
285 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
286 if (Ty == 0) return true;
287 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
288
289 // Check to make sure all bits are referring to the right bits in the variable
290 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
291 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
292 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
293 return true;
294 }
295
296 Var->print(OS);
297 return false;
298}
299
300bool BitsInit::printAsUnset(std::ostream &OS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000301 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000302 if (!dynamic_cast<UnsetInit*>(getBit(i)))
303 return true;
304 OS << "?";
305 return false;
306}
307
308// resolveReferences - If there are any field references that refer to fields
309// that have been filled in, we can propagate the values now.
310//
Chris Lattneref943742005-04-19 03:36:21 +0000311Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000312 bool Changed = false;
313 BitsInit *New = new BitsInit(getNumBits());
314
315 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
316 Init *B;
317 Init *CurBit = getBit(i);
318
319 do {
320 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000321 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000322 Changed |= B != CurBit;
323 } while (B != CurBit);
324 New->setBit(i, CurBit);
325 }
326
327 if (Changed)
328 return New;
329 delete New;
330 return this;
331}
332
Chris Lattner695506c2007-11-22 21:05:25 +0000333std::string IntInit::getAsString() const {
334 return itostr(Value);
335}
336
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000337Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
338 BitsInit *BI = new BitsInit(Bits.size());
339
340 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000341 if (Bits[i] >= 64) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000342 delete BI;
343 return 0;
344 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000345 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000346 }
347 return BI;
348}
349
Chris Lattner8bf9e062004-07-26 23:21:34 +0000350Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
351 std::vector<Init*> Vals;
352 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
353 if (Elements[i] >= getSize())
354 return 0;
355 Vals.push_back(getElement(Elements[i]));
356 }
357 return new ListInit(Vals);
358}
359
Chris Lattnercbebe462007-02-27 22:08:27 +0000360Record *ListInit::getElementAsRecord(unsigned i) const {
361 assert(i < Values.size() && "List element index out of range!");
362 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
363 if (DI == 0) throw "Expected record in list!";
364 return DI->getDef();
365}
366
Chris Lattneref943742005-04-19 03:36:21 +0000367Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000368 std::vector<Init*> Resolved;
369 Resolved.reserve(getSize());
370 bool Changed = false;
371
372 for (unsigned i = 0, e = getSize(); i != e; ++i) {
373 Init *E;
374 Init *CurElt = getElement(i);
375
376 do {
377 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000378 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000379 Changed |= E != CurElt;
380 } while (E != CurElt);
381 Resolved.push_back(E);
382 }
383
384 if (Changed)
385 return new ListInit(Resolved);
386 return this;
387}
388
Chris Lattner695506c2007-11-22 21:05:25 +0000389std::string ListInit::getAsString() const {
390 std::string Result = "[";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000391 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000392 if (i) Result += ", ";
393 Result += Values[i]->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000394 }
Chris Lattner695506c2007-11-22 21:05:25 +0000395 return Result + "]";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000396}
397
David Greenea9c6c5d2009-04-22 20:18:10 +0000398Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000399 switch (getOpcode()) {
400 default: assert(0 && "Unknown binop");
Evan Chenga32dee22007-05-15 01:23:24 +0000401 case CONCAT: {
402 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
403 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
404 if (LHSs && RHSs) {
405 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
406 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
Evan Cheng94b5a802007-07-19 01:14:50 +0000407 if (LOp->getDef() != ROp->getDef()) {
408 bool LIsOps =
409 LOp->getDef()->getName() == "outs" ||
410 LOp->getDef()->getName() != "ins" ||
411 LOp->getDef()->getName() != "defs";
412 bool RIsOps =
413 ROp->getDef()->getName() == "outs" ||
414 ROp->getDef()->getName() != "ins" ||
415 ROp->getDef()->getName() != "defs";
416 if (!LIsOps || !RIsOps)
417 throw "Concated Dag operators do not match!";
418 }
Evan Chenga32dee22007-05-15 01:23:24 +0000419 std::vector<Init*> Args;
420 std::vector<std::string> ArgNames;
421 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
422 Args.push_back(LHSs->getArg(i));
423 ArgNames.push_back(LHSs->getArgName(i));
424 }
425 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
426 Args.push_back(RHSs->getArg(i));
427 ArgNames.push_back(RHSs->getArgName(i));
428 }
Nate Begemandbe3f772009-03-19 05:21:56 +0000429 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
Evan Chenga32dee22007-05-15 01:23:24 +0000430 }
431 break;
432 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000433 case STRCONCAT: {
434 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
435 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
436 if (LHSs && RHSs)
437 return new StringInit(LHSs->getValue() + RHSs->getValue());
438 break;
439 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000440 case NAMECONCAT: {
441 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
442 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
443 if (LHSs && RHSs) {
444 std::string Name(LHSs->getValue() + RHSs->getValue());
445
446 // From TGParser::ParseIDValue
447 if (CurRec) {
448 if (const RecordVal *RV = CurRec->getValue(Name))
449 return new VarInit(Name, RV->getType());
450
451 std::string TemplateArgName = CurRec->getName()+":"+Name;
452 if (CurRec->isTemplateArg(TemplateArgName)) {
453 const RecordVal *RV = CurRec->getValue(TemplateArgName);
454 assert(RV && "Template arg doesn't exist??");
455 return new VarInit(TemplateArgName, RV->getType());
456 }
457 }
458
459 if (CurMultiClass) {
460 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
461 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
462 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
463 assert(RV && "Template arg doesn't exist??");
464 return new VarInit(MCName, RV->getType());
465 }
466 }
467
468 if (Record *D = Records.getDef(Name))
469 return new DefInit(D);
470
471 cerr << "Variable not defined: '" + Name + "'\n";
472 assert(0 && "Variable not found");
473 return 0;
474 }
475 break;
476 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000477 case SHL:
478 case SRA:
479 case SRL: {
480 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
481 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
482 if (LHSi && RHSi) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000483 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
484 int64_t Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000485 switch (getOpcode()) {
486 default: assert(0 && "Bad opcode!");
487 case SHL: Result = LHSv << RHSv; break;
488 case SRA: Result = LHSv >> RHSv; break;
Dan Gohmanca0546f2008-10-17 01:33:43 +0000489 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000490 }
491 return new IntInit(Result);
492 }
493 break;
494 }
495 }
496 return this;
497}
498
499Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
500 Init *lhs = LHS->resolveReferences(R, RV);
501 Init *rhs = RHS->resolveReferences(R, RV);
502
503 if (LHS != lhs || RHS != rhs)
David Greenea9c6c5d2009-04-22 20:18:10 +0000504 return (new BinOpInit(getOpcode(), lhs, rhs))->Fold(&R, 0);
505 return Fold(&R, 0);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000506}
507
Chris Lattner695506c2007-11-22 21:05:25 +0000508std::string BinOpInit::getAsString() const {
509 std::string Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000510 switch (Opc) {
Chris Lattner695506c2007-11-22 21:05:25 +0000511 case CONCAT: Result = "!con"; break;
512 case SHL: Result = "!shl"; break;
513 case SRA: Result = "!sra"; break;
514 case SRL: Result = "!srl"; break;
515 case STRCONCAT: Result = "!strconcat"; break;
David Greenea9c6c5d2009-04-22 20:18:10 +0000516 case NAMECONCAT: Result = "!nameconcat"; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000517 }
Chris Lattner695506c2007-11-22 21:05:25 +0000518 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
Chris Lattner51ffbf12006-03-31 21:53:49 +0000519}
520
Chris Lattner577fc3f2004-07-27 01:01:21 +0000521Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000522 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
523 if (T == 0) return 0; // Cannot subscript a non-bits variable...
524 unsigned NumBits = T->getNumBits();
525
526 BitsInit *BI = new BitsInit(Bits.size());
527 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
528 if (Bits[i] >= NumBits) {
529 delete BI;
530 return 0;
531 }
532 BI->setBit(i, new VarBitInit(this, Bits[i]));
533 }
534 return BI;
535}
536
Chris Lattner577fc3f2004-07-27 01:01:21 +0000537Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
538 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
539 if (T == 0) return 0; // Cannot subscript a non-list variable...
540
541 if (Elements.size() == 1)
542 return new VarListElementInit(this, Elements[0]);
543
544 std::vector<Init*> ListInits;
545 ListInits.reserve(Elements.size());
546 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
547 ListInits.push_back(new VarListElementInit(this, Elements[i]));
548 return new ListInit(ListInits);
549}
550
551
Chris Lattneref943742005-04-19 03:36:21 +0000552Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
553 unsigned Bit) {
554 if (R.isTemplateArg(getName())) return 0;
555 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000556
557 RecordVal *RV = R.getValue(getName());
558 assert(RV && "Reference to a non-existant variable?");
559 assert(dynamic_cast<BitsInit*>(RV->getValue()));
560 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +0000561
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000562 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
563 Init *B = BI->getBit(Bit);
564
565 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
566 return B; // Replace the VarBitInit with it.
Chris Lattner577fc3f2004-07-27 01:01:21 +0000567 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000568}
569
Chris Lattneref943742005-04-19 03:36:21 +0000570Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
571 unsigned Elt) {
572 if (R.isTemplateArg(getName())) return 0;
573 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000574
575 RecordVal *RV = R.getValue(getName());
576 assert(RV && "Reference to a non-existant variable?");
577 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
578 assert(LI && "Invalid list element!");
579
580 if (Elt >= LI->getSize())
581 return 0; // Out of range reference.
582 Init *E = LI->getElement(Elt);
583 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
584 return E; // Replace the VarListElementInit with it.
585 return 0;
586}
587
588
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000589RecTy *VarInit::getFieldType(const std::string &FieldName) const {
590 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
591 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
592 return RV->getType();
593 return 0;
594}
595
596Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +0000597 if (dynamic_cast<RecordRecTy*>(getType()))
Chris Lattnerd959ab92004-02-28 16:31:53 +0000598 if (const RecordVal *RV = R.getValue(VarName)) {
599 Init *TheInit = RV->getValue();
600 assert(TheInit != this && "Infinite loop detected!");
601 if (Init *I = TheInit->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000602 return I;
603 else
604 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +0000605 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000606 return 0;
607}
608
609/// resolveReferences - This method is used by classes that refer to other
610/// variables which may not be defined at the time they expression is formed.
611/// If a value is set for the variable later, this method will be called on
612/// users of the value to allow the value to propagate out.
613///
Chris Lattneref943742005-04-19 03:36:21 +0000614Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000615 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +0000616 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000617 return Val->getValue();
618 return this;
619}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000620
Chris Lattner695506c2007-11-22 21:05:25 +0000621std::string VarBitInit::getAsString() const {
622 return TI->getAsString() + "{" + utostr(Bit) + "}";
623}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000624
Chris Lattneref943742005-04-19 03:36:21 +0000625Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
626 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000627 return I;
628 return this;
629}
630
Chris Lattner695506c2007-11-22 21:05:25 +0000631std::string VarListElementInit::getAsString() const {
632 return TI->getAsString() + "[" + utostr(Element) + "]";
633}
634
Chris Lattneref943742005-04-19 03:36:21 +0000635Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
636 if (Init *I = getVariable()->resolveListElementReference(R, RV,
637 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +0000638 return I;
639 return this;
640}
641
Chris Lattneref943742005-04-19 03:36:21 +0000642Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
643 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000644 // FIXME: This should be implemented, to support references like:
645 // bit B = AA[0]{1};
646 return 0;
647}
648
Chris Lattneref943742005-04-19 03:36:21 +0000649Init *VarListElementInit::
650resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000651 // FIXME: This should be implemented, to support references like:
652 // int B = AA[0][1];
653 return 0;
654}
655
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000656RecTy *DefInit::getFieldType(const std::string &FieldName) const {
657 if (const RecordVal *RV = Def->getValue(FieldName))
658 return RV->getType();
659 return 0;
660}
661
662Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
663 return Def->getValue(FieldName)->getValue();
664}
665
666
Chris Lattner695506c2007-11-22 21:05:25 +0000667std::string DefInit::getAsString() const {
668 return Def->getName();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000669}
670
Chris Lattneref943742005-04-19 03:36:21 +0000671Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
672 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000673 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000674 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
675 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
676 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000677
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000678 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
679 return B; // Replace the VarBitInit with it.
680 }
Chris Lattner577fc3f2004-07-27 01:01:21 +0000681 return 0;
682}
683
Chris Lattneref943742005-04-19 03:36:21 +0000684Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
685 unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000686 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
687 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
688 if (Elt >= LI->getSize()) return 0;
689 Init *E = LI->getElement(Elt);
690
691 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
692 return E; // Replace the VarListElementInit with it.
693 }
694 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000695}
696
Chris Lattneref943742005-04-19 03:36:21 +0000697Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
698 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
699
700 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000701 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +0000702 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000703 return BVR->isComplete() ? BVR : this;
704 }
Chris Lattneref943742005-04-19 03:36:21 +0000705
706 if (NewRec != Rec) {
Chris Lattneref943742005-04-19 03:36:21 +0000707 return new FieldInit(NewRec, FieldName);
708 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000709 return this;
710}
711
Chris Lattner0d3ef402006-01-31 06:02:35 +0000712Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
713 std::vector<Init*> NewArgs;
714 for (unsigned i = 0, e = Args.size(); i != e; ++i)
715 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
716
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000717 Init *Op = Val->resolveReferences(R, RV);
718
719 if (Args != NewArgs || Op != Val)
Nate Begemandbe3f772009-03-19 05:21:56 +0000720 return new DagInit(Op, "", NewArgs, ArgNames);
Chris Lattner0d3ef402006-01-31 06:02:35 +0000721
722 return this;
723}
724
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000725
Chris Lattner695506c2007-11-22 21:05:25 +0000726std::string DagInit::getAsString() const {
727 std::string Result = "(" + Val->getAsString();
Nate Begemandbe3f772009-03-19 05:21:56 +0000728 if (!ValName.empty())
729 Result += ":" + ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000730 if (Args.size()) {
Chris Lattner695506c2007-11-22 21:05:25 +0000731 Result += " " + Args[0]->getAsString();
732 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000733 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000734 Result += ", " + Args[i]->getAsString();
735 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000736 }
737 }
Chris Lattner695506c2007-11-22 21:05:25 +0000738 return Result + ")";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000739}
740
741
742//===----------------------------------------------------------------------===//
743// Other implementations
744//===----------------------------------------------------------------------===//
745
746RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
747 : Name(N), Ty(T), Prefix(P) {
748 Value = Ty->convertValue(new UnsetInit());
749 assert(Value && "Cannot create unset value for current type!");
750}
751
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000752void RecordVal::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000753
754void RecordVal::print(std::ostream &OS, bool PrintSem) const {
755 if (getPrefix()) OS << "field ";
756 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +0000757
758 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000759 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +0000760
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000761 if (PrintSem) OS << ";\n";
762}
763
Chris Lattnerac284252005-08-19 17:58:11 +0000764void Record::setName(const std::string &Name) {
765 if (Records.getDef(getName()) == this) {
766 Records.removeDef(getName());
767 this->Name = Name;
768 Records.addDef(this);
769 } else {
770 Records.removeClass(getName());
771 this->Name = Name;
772 Records.addClass(this);
773 }
774}
775
Chris Lattneref943742005-04-19 03:36:21 +0000776/// resolveReferencesTo - If anything in this record refers to RV, replace the
777/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
778/// references.
779void Record::resolveReferencesTo(const RecordVal *RV) {
780 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
781 if (Init *V = Values[i].getValue())
782 Values[i].setValue(V->resolveReferences(*this, RV));
783 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000784}
785
Chris Lattneref943742005-04-19 03:36:21 +0000786
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000787void Record::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000788
Chris Lattner68478662004-08-01 03:55:39 +0000789std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000790 OS << R.getName();
791
792 const std::vector<std::string> &TArgs = R.getTemplateArgs();
793 if (!TArgs.empty()) {
794 OS << "<";
795 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
796 if (i) OS << ", ";
797 const RecordVal *RV = R.getValue(TArgs[i]);
798 assert(RV && "Template argument record not found??");
799 RV->print(OS, false);
800 }
801 OS << ">";
802 }
803
804 OS << " {";
805 const std::vector<Record*> &SC = R.getSuperClasses();
806 if (!SC.empty()) {
807 OS << "\t//";
808 for (unsigned i = 0, e = SC.size(); i != e; ++i)
809 OS << " " << SC[i]->getName();
810 }
811 OS << "\n";
812
813 const std::vector<RecordVal> &Vals = R.getValues();
814 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
815 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
816 OS << Vals[i];
817 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
818 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
819 OS << Vals[i];
820
821 return OS << "}\n";
822}
823
824/// getValueInit - Return the initializer for a value with the specified name,
825/// or throw an exception if the field does not exist.
826///
827Init *Record::getValueInit(const std::string &FieldName) const {
828 const RecordVal *R = getValue(FieldName);
829 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000830 throw "Record `" + getName() + "' does not have a field named `" +
831 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000832 return R->getValue();
833}
834
835
836/// getValueAsString - This method looks up the specified field and returns its
837/// value as a string, throwing an exception if the field does not exist or if
838/// the value is not a string.
839///
840std::string Record::getValueAsString(const std::string &FieldName) const {
841 const RecordVal *R = getValue(FieldName);
842 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000843 throw "Record `" + getName() + "' does not have a field named `" +
844 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000845
846 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
847 return SI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000848 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000849 "' does not have a string initializer!";
850}
851
852/// getValueAsBitsInit - This method looks up the specified field and returns
853/// its value as a BitsInit, throwing an exception if the field does not exist
854/// or if the value is not the right type.
855///
856BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
857 const RecordVal *R = getValue(FieldName);
858 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000859 throw "Record `" + getName() + "' does not have a field named `" +
860 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000861
862 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
863 return BI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000864 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000865 "' does not have a BitsInit initializer!";
866}
867
868/// getValueAsListInit - This method looks up the specified field and returns
869/// its value as a ListInit, throwing an exception if the field does not exist
870/// or if the value is not the right type.
871///
872ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
873 const RecordVal *R = getValue(FieldName);
874 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000875 throw "Record `" + getName() + "' does not have a field named `" +
876 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000877
878 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
879 return LI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000880 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000881 "' does not have a list initializer!";
882}
883
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000884/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +0000885/// its value as a vector of records, throwing an exception if the field does
886/// not exist or if the value is not the right type.
887///
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000888std::vector<Record*>
889Record::getValueAsListOfDefs(const std::string &FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +0000890 ListInit *List = getValueAsListInit(FieldName);
891 std::vector<Record*> Defs;
892 for (unsigned i = 0; i < List->getSize(); i++) {
893 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
894 Defs.push_back(DI->getDef());
895 } else {
896 throw "Record `" + getName() + "', field `" + FieldName +
897 "' list is not entirely DefInit!";
898 }
899 }
900 return Defs;
901}
902
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000903/// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +0000904/// value as an int64_t, throwing an exception if the field does not exist or if
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000905/// the value is not the right type.
906///
Dan Gohmanca0546f2008-10-17 01:33:43 +0000907int64_t Record::getValueAsInt(const std::string &FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000908 const RecordVal *R = getValue(FieldName);
909 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000910 throw "Record `" + getName() + "' does not have a field named `" +
911 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000912
913 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
914 return II->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000915 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +0000916 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000917}
918
Anton Korobeynikova468a112007-11-11 11:19:37 +0000919/// getValueAsListOfInts - This method looks up the specified field and returns
920/// its value as a vector of integers, throwing an exception if the field does
921/// not exist or if the value is not the right type.
922///
Dan Gohmanca0546f2008-10-17 01:33:43 +0000923std::vector<int64_t>
Anton Korobeynikova468a112007-11-11 11:19:37 +0000924Record::getValueAsListOfInts(const std::string &FieldName) const {
925 ListInit *List = getValueAsListInit(FieldName);
Dan Gohmanca0546f2008-10-17 01:33:43 +0000926 std::vector<int64_t> Ints;
Anton Korobeynikova468a112007-11-11 11:19:37 +0000927 for (unsigned i = 0; i < List->getSize(); i++) {
928 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
929 Ints.push_back(II->getValue());
930 } else {
931 throw "Record `" + getName() + "', field `" + FieldName +
932 "' does not have a list of ints initializer!";
933 }
934 }
935 return Ints;
936}
937
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000938/// getValueAsDef - This method looks up the specified field and returns its
939/// value as a Record, throwing an exception if the field does not exist or if
940/// the value is not the right type.
941///
942Record *Record::getValueAsDef(const std::string &FieldName) const {
943 const RecordVal *R = getValue(FieldName);
944 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000945 throw "Record `" + getName() + "' does not have a field named `" +
946 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000947
948 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
949 return DI->getDef();
Misha Brukman41f9f292004-10-08 14:59:05 +0000950 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +0000951 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000952}
953
954/// getValueAsBit - This method looks up the specified field and returns its
955/// value as a bit, throwing an exception if the field does not exist or if
956/// the value is not the right type.
957///
958bool Record::getValueAsBit(const std::string &FieldName) const {
959 const RecordVal *R = getValue(FieldName);
960 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000961 throw "Record `" + getName() + "' does not have a field named `" +
962 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000963
964 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
965 return BI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000966 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000967 "' does not have a bit initializer!";
968}
969
970/// getValueAsDag - This method looks up the specified field and returns its
971/// value as an Dag, throwing an exception if the field does not exist or if
972/// the value is not the right type.
973///
974DagInit *Record::getValueAsDag(const std::string &FieldName) const {
975 const RecordVal *R = getValue(FieldName);
976 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000977 throw "Record `" + getName() + "' does not have a field named `" +
978 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000979
980 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
981 return DI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000982 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000983 "' does not have a dag initializer!";
984}
985
Chris Lattnerae939eb2005-09-13 21:44:28 +0000986std::string Record::getValueAsCode(const std::string &FieldName) const {
987 const RecordVal *R = getValue(FieldName);
988 if (R == 0 || R->getValue() == 0)
989 throw "Record `" + getName() + "' does not have a field named `" +
990 FieldName + "'!\n";
991
992 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
993 return CI->getValue();
994 throw "Record `" + getName() + "', field `" + FieldName +
995 "' does not have a code initializer!";
996}
997
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000998
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000999void RecordKeeper::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001000
Chris Lattner68478662004-08-01 03:55:39 +00001001std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001002 OS << "------------- Classes -----------------\n";
1003 const std::map<std::string, Record*> &Classes = RK.getClasses();
1004 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001005 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001006 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001007
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001008 OS << "------------- Defs -----------------\n";
1009 const std::map<std::string, Record*> &Defs = RK.getDefs();
1010 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001011 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001012 OS << "def " << *I->second;
1013 return OS;
1014}
1015
1016
1017/// getAllDerivedDefinitions - This method returns all concrete definitions
1018/// that derive from the specified class name. If a class with the specified
1019/// name does not exist, an error is printed and true is returned.
1020std::vector<Record*>
1021RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1022 Record *Class = Records.getClass(ClassName);
1023 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +00001024 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001025
1026 std::vector<Record*> Defs;
1027 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1028 E = getDefs().end(); I != E; ++I)
1029 if (I->second->isSubClassOf(Class))
1030 Defs.push_back(I->second);
1031
1032 return Defs;
1033}
Brian Gaeke960707c2003-11-11 22:41:34 +00001034