blob: 9ce645de34ff2d3c53228d2bd2f8e36a2117af2c [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())
David Greene196ac3c2009-04-23 21:25:15 +0000141 return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000142 return BO;
143 }
David Greene196ac3c2009-04-23 21:25:15 +0000144 if (BO->getOpcode() == BinOpInit::NAMECONCAT) {
145 if (BO->getType()->getAsString() == getAsString()) {
146 Init *L = BO->getLHS()->convertInitializerTo(this);
147 Init *R = BO->getRHS()->convertInitializerTo(this);
148 if (L == 0 || R == 0) return 0;
149 if (L != BO->getLHS() || R != BO->getRHS())
150 return new BinOpInit(BinOpInit::NAMECONCAT, L, R, new StringRecTy);
151 return BO;
152 }
153 }
154
155 return convertValue((TypedInit*)BO);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000156}
157
158
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000159Init *StringRecTy::convertValue(TypedInit *TI) {
160 if (dynamic_cast<StringRecTy*>(TI->getType()))
161 return TI; // Accept variable if already of the right type!
162 return 0;
163}
164
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000165std::string ListRecTy::getAsString() const {
166 return "list<" + Ty->getAsString() + ">";
167}
168
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000169Init *ListRecTy::convertValue(ListInit *LI) {
170 std::vector<Init*> Elements;
171
172 // Verify that all of the elements of the list are subclasses of the
173 // appropriate class!
174 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
175 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
176 Elements.push_back(CI);
177 else
178 return 0;
179
180 return new ListInit(Elements);
181}
182
183Init *ListRecTy::convertValue(TypedInit *TI) {
184 // Ensure that TI is compatible with our class.
185 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
186 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
187 return TI;
188 return 0;
189}
190
191Init *CodeRecTy::convertValue(TypedInit *TI) {
192 if (TI->getType()->typeIsConvertibleTo(this))
193 return TI;
194 return 0;
195}
196
197Init *DagRecTy::convertValue(TypedInit *TI) {
198 if (TI->getType()->typeIsConvertibleTo(this))
199 return TI;
200 return 0;
201}
202
Evan Chenga32dee22007-05-15 01:23:24 +0000203Init *DagRecTy::convertValue(BinOpInit *BO) {
204 if (BO->getOpcode() == BinOpInit::CONCAT) {
205 Init *L = BO->getLHS()->convertInitializerTo(this);
206 Init *R = BO->getRHS()->convertInitializerTo(this);
207 if (L == 0 || R == 0) return 0;
208 if (L != BO->getLHS() || R != BO->getRHS())
David Greene196ac3c2009-04-23 21:25:15 +0000209 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
Evan Chenga32dee22007-05-15 01:23:24 +0000210 return BO;
211 }
David Greene196ac3c2009-04-23 21:25:15 +0000212 if (BO->getOpcode() == BinOpInit::NAMECONCAT) {
213 if (BO->getType()->getAsString() == getAsString()) {
214 Init *L = BO->getLHS()->convertInitializerTo(this);
215 Init *R = BO->getRHS()->convertInitializerTo(this);
216 if (L == 0 || R == 0) return 0;
217 if (L != BO->getLHS() || R != BO->getRHS())
218 return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy);
219 return BO;
220 }
221 }
Evan Chenga32dee22007-05-15 01:23:24 +0000222 return 0;
223}
224
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000225std::string RecordRecTy::getAsString() const {
226 return Rec->getName();
227}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000228
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000229Init *RecordRecTy::convertValue(DefInit *DI) {
230 // Ensure that DI is a subclass of Rec.
231 if (!DI->getDef()->isSubClassOf(Rec))
232 return 0;
233 return DI;
234}
235
236Init *RecordRecTy::convertValue(TypedInit *TI) {
237 // Ensure that TI is compatible with Rec.
238 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
239 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
240 RRT->getRecord() == getRecord())
241 return TI;
242 return 0;
243}
244
245bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
246 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
247}
248
249
250//===----------------------------------------------------------------------===//
251// Initializer implementations
252//===----------------------------------------------------------------------===//
253
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000254void Init::dump() const { return print(*cerr.stream()); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000255
256Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
257 BitsInit *BI = new BitsInit(Bits.size());
258 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
259 if (Bits[i] >= getNumBits()) {
260 delete BI;
261 return 0;
262 }
263 BI->setBit(i, getBit(Bits[i]));
264 }
265 return BI;
266}
267
Chris Lattner695506c2007-11-22 21:05:25 +0000268std::string BitsInit::getAsString() const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000269 //if (!printInHex(OS)) return;
270 //if (!printAsVariable(OS)) return;
271 //if (!printAsUnset(OS)) return;
272
Chris Lattner695506c2007-11-22 21:05:25 +0000273 std::string Result = "{ ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000274 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000275 if (i) Result += ", ";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000276 if (Init *Bit = getBit(e-i-1))
Chris Lattner695506c2007-11-22 21:05:25 +0000277 Result += Bit->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000278 else
Chris Lattner695506c2007-11-22 21:05:25 +0000279 Result += "*";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000280 }
Chris Lattner695506c2007-11-22 21:05:25 +0000281 return Result + " }";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000282}
283
284bool BitsInit::printInHex(std::ostream &OS) const {
285 // First, attempt to convert the value into an integer value...
Dan Gohmanca0546f2008-10-17 01:33:43 +0000286 int64_t Result = 0;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000287 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000288 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
289 Result |= Bit->getValue() << i;
290 } else {
291 return true;
292 }
293
294 OS << "0x" << std::hex << Result << std::dec;
295 return false;
296}
297
298bool BitsInit::printAsVariable(std::ostream &OS) const {
299 // Get the variable that we may be set equal to...
300 assert(getNumBits() != 0);
301 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
302 if (FirstBit == 0) return true;
303 TypedInit *Var = FirstBit->getVariable();
304
305 // Check to make sure the types are compatible.
306 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
307 if (Ty == 0) return true;
308 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
309
310 // Check to make sure all bits are referring to the right bits in the variable
311 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
312 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
313 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
314 return true;
315 }
316
317 Var->print(OS);
318 return false;
319}
320
321bool BitsInit::printAsUnset(std::ostream &OS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000322 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000323 if (!dynamic_cast<UnsetInit*>(getBit(i)))
324 return true;
325 OS << "?";
326 return false;
327}
328
329// resolveReferences - If there are any field references that refer to fields
330// that have been filled in, we can propagate the values now.
331//
Chris Lattneref943742005-04-19 03:36:21 +0000332Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000333 bool Changed = false;
334 BitsInit *New = new BitsInit(getNumBits());
335
336 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
337 Init *B;
338 Init *CurBit = getBit(i);
339
340 do {
341 B = CurBit;
Chris Lattneref943742005-04-19 03:36:21 +0000342 CurBit = CurBit->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000343 Changed |= B != CurBit;
344 } while (B != CurBit);
345 New->setBit(i, CurBit);
346 }
347
348 if (Changed)
349 return New;
350 delete New;
351 return this;
352}
353
Chris Lattner695506c2007-11-22 21:05:25 +0000354std::string IntInit::getAsString() const {
355 return itostr(Value);
356}
357
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000358Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
359 BitsInit *BI = new BitsInit(Bits.size());
360
361 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000362 if (Bits[i] >= 64) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000363 delete BI;
364 return 0;
365 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000366 BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i])));
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000367 }
368 return BI;
369}
370
Chris Lattner8bf9e062004-07-26 23:21:34 +0000371Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
372 std::vector<Init*> Vals;
373 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
374 if (Elements[i] >= getSize())
375 return 0;
376 Vals.push_back(getElement(Elements[i]));
377 }
378 return new ListInit(Vals);
379}
380
Chris Lattnercbebe462007-02-27 22:08:27 +0000381Record *ListInit::getElementAsRecord(unsigned i) const {
382 assert(i < Values.size() && "List element index out of range!");
383 DefInit *DI = dynamic_cast<DefInit*>(Values[i]);
384 if (DI == 0) throw "Expected record in list!";
385 return DI->getDef();
386}
387
Chris Lattneref943742005-04-19 03:36:21 +0000388Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000389 std::vector<Init*> Resolved;
390 Resolved.reserve(getSize());
391 bool Changed = false;
392
393 for (unsigned i = 0, e = getSize(); i != e; ++i) {
394 Init *E;
395 Init *CurElt = getElement(i);
396
397 do {
398 E = CurElt;
Chris Lattneref943742005-04-19 03:36:21 +0000399 CurElt = CurElt->resolveReferences(R, RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000400 Changed |= E != CurElt;
401 } while (E != CurElt);
402 Resolved.push_back(E);
403 }
404
405 if (Changed)
406 return new ListInit(Resolved);
407 return this;
408}
409
Chris Lattner695506c2007-11-22 21:05:25 +0000410std::string ListInit::getAsString() const {
411 std::string Result = "[";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000412 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000413 if (i) Result += ", ";
414 Result += Values[i]->getAsString();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000415 }
Chris Lattner695506c2007-11-22 21:05:25 +0000416 return Result + "]";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000417}
418
David Greenea9c6c5d2009-04-22 20:18:10 +0000419Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
Chris Lattner51ffbf12006-03-31 21:53:49 +0000420 switch (getOpcode()) {
421 default: assert(0 && "Unknown binop");
Evan Chenga32dee22007-05-15 01:23:24 +0000422 case CONCAT: {
423 DagInit *LHSs = dynamic_cast<DagInit*>(LHS);
424 DagInit *RHSs = dynamic_cast<DagInit*>(RHS);
425 if (LHSs && RHSs) {
426 DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator());
427 DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator());
Evan Cheng94b5a802007-07-19 01:14:50 +0000428 if (LOp->getDef() != ROp->getDef()) {
429 bool LIsOps =
430 LOp->getDef()->getName() == "outs" ||
431 LOp->getDef()->getName() != "ins" ||
432 LOp->getDef()->getName() != "defs";
433 bool RIsOps =
434 ROp->getDef()->getName() == "outs" ||
435 ROp->getDef()->getName() != "ins" ||
436 ROp->getDef()->getName() != "defs";
437 if (!LIsOps || !RIsOps)
438 throw "Concated Dag operators do not match!";
439 }
Evan Chenga32dee22007-05-15 01:23:24 +0000440 std::vector<Init*> Args;
441 std::vector<std::string> ArgNames;
442 for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
443 Args.push_back(LHSs->getArg(i));
444 ArgNames.push_back(LHSs->getArgName(i));
445 }
446 for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
447 Args.push_back(RHSs->getArg(i));
448 ArgNames.push_back(RHSs->getArgName(i));
449 }
Nate Begemandbe3f772009-03-19 05:21:56 +0000450 return new DagInit(LHSs->getOperator(), "", Args, ArgNames);
Evan Chenga32dee22007-05-15 01:23:24 +0000451 }
452 break;
453 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000454 case STRCONCAT: {
455 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
456 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
457 if (LHSs && RHSs)
458 return new StringInit(LHSs->getValue() + RHSs->getValue());
459 break;
460 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000461 case NAMECONCAT: {
462 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
463 StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
464 if (LHSs && RHSs) {
465 std::string Name(LHSs->getValue() + RHSs->getValue());
466
467 // From TGParser::ParseIDValue
468 if (CurRec) {
David Greene196ac3c2009-04-23 21:25:15 +0000469 if (const RecordVal *RV = CurRec->getValue(Name)) {
470 if (RV->getType() != getType()) {
471 throw "type mismatch in nameconcat";
472 }
David Greenea9c6c5d2009-04-22 20:18:10 +0000473 return new VarInit(Name, RV->getType());
David Greene196ac3c2009-04-23 21:25:15 +0000474 }
475
David Greenea9c6c5d2009-04-22 20:18:10 +0000476 std::string TemplateArgName = CurRec->getName()+":"+Name;
477 if (CurRec->isTemplateArg(TemplateArgName)) {
478 const RecordVal *RV = CurRec->getValue(TemplateArgName);
479 assert(RV && "Template arg doesn't exist??");
David Greene196ac3c2009-04-23 21:25:15 +0000480
481 if (RV->getType() != getType()) {
482 throw "type mismatch in nameconcat";
483 }
484
David Greenea9c6c5d2009-04-22 20:18:10 +0000485 return new VarInit(TemplateArgName, RV->getType());
486 }
487 }
488
489 if (CurMultiClass) {
490 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
491 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
492 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
493 assert(RV && "Template arg doesn't exist??");
David Greene196ac3c2009-04-23 21:25:15 +0000494
495 if (RV->getType() != getType()) {
496 throw "type mismatch in nameconcat";
497 }
498
David Greenea9c6c5d2009-04-22 20:18:10 +0000499 return new VarInit(MCName, RV->getType());
500 }
501 }
502
503 if (Record *D = Records.getDef(Name))
504 return new DefInit(D);
505
506 cerr << "Variable not defined: '" + Name + "'\n";
507 assert(0 && "Variable not found");
508 return 0;
509 }
510 break;
511 }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000512 case SHL:
513 case SRA:
514 case SRL: {
515 IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
516 IntInit *RHSi = dynamic_cast<IntInit*>(RHS);
517 if (LHSi && RHSi) {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000518 int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
519 int64_t Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000520 switch (getOpcode()) {
521 default: assert(0 && "Bad opcode!");
522 case SHL: Result = LHSv << RHSv; break;
523 case SRA: Result = LHSv >> RHSv; break;
Dan Gohmanca0546f2008-10-17 01:33:43 +0000524 case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000525 }
526 return new IntInit(Result);
527 }
528 break;
529 }
530 }
531 return this;
532}
533
534Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) {
535 Init *lhs = LHS->resolveReferences(R, RV);
536 Init *rhs = RHS->resolveReferences(R, RV);
537
538 if (LHS != lhs || RHS != rhs)
David Greene196ac3c2009-04-23 21:25:15 +0000539 return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0);
David Greenea9c6c5d2009-04-22 20:18:10 +0000540 return Fold(&R, 0);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000541}
542
Chris Lattner695506c2007-11-22 21:05:25 +0000543std::string BinOpInit::getAsString() const {
544 std::string Result;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000545 switch (Opc) {
Chris Lattner695506c2007-11-22 21:05:25 +0000546 case CONCAT: Result = "!con"; break;
547 case SHL: Result = "!shl"; break;
548 case SRA: Result = "!sra"; break;
549 case SRL: Result = "!srl"; break;
550 case STRCONCAT: Result = "!strconcat"; break;
David Greene196ac3c2009-04-23 21:25:15 +0000551 case NAMECONCAT:
552 Result = "!nameconcat<" + getType()->getAsString() + ">"; break;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000553 }
Chris Lattner695506c2007-11-22 21:05:25 +0000554 return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
Chris Lattner51ffbf12006-03-31 21:53:49 +0000555}
556
David Greene196ac3c2009-04-23 21:25:15 +0000557Init *BinOpInit::resolveBitReference(Record &R, const RecordVal *IRV,
558 unsigned Bit) {
559 Init *Folded = Fold(&R, 0);
560
561 if (Folded != this) {
562 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
563 if (Typed) {
564 return Typed->resolveBitReference(R, IRV, Bit);
565 }
566 }
567
568 return 0;
569}
570
571Init *BinOpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
572 unsigned Elt) {
573 Init *Folded = Fold(&R, 0);
574
575 if (Folded != this) {
576 TypedInit *Typed = dynamic_cast<TypedInit *>(Folded);
577 if (Typed) {
578 return Typed->resolveListElementReference(R, IRV, Elt);
579 }
580 }
581
582 return 0;
583}
584
Chris Lattner577fc3f2004-07-27 01:01:21 +0000585Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000586 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
587 if (T == 0) return 0; // Cannot subscript a non-bits variable...
588 unsigned NumBits = T->getNumBits();
589
590 BitsInit *BI = new BitsInit(Bits.size());
591 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
592 if (Bits[i] >= NumBits) {
593 delete BI;
594 return 0;
595 }
596 BI->setBit(i, new VarBitInit(this, Bits[i]));
597 }
598 return BI;
599}
600
Chris Lattner577fc3f2004-07-27 01:01:21 +0000601Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
602 ListRecTy *T = dynamic_cast<ListRecTy*>(getType());
603 if (T == 0) return 0; // Cannot subscript a non-list variable...
604
605 if (Elements.size() == 1)
606 return new VarListElementInit(this, Elements[0]);
607
608 std::vector<Init*> ListInits;
609 ListInits.reserve(Elements.size());
610 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
611 ListInits.push_back(new VarListElementInit(this, Elements[i]));
612 return new ListInit(ListInits);
613}
614
615
Chris Lattneref943742005-04-19 03:36:21 +0000616Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV,
617 unsigned Bit) {
618 if (R.isTemplateArg(getName())) return 0;
619 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000620
621 RecordVal *RV = R.getValue(getName());
622 assert(RV && "Reference to a non-existant variable?");
623 assert(dynamic_cast<BitsInit*>(RV->getValue()));
624 BitsInit *BI = (BitsInit*)RV->getValue();
Misha Brukman650ba8e2005-04-22 00:00:37 +0000625
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000626 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
627 Init *B = BI->getBit(Bit);
628
629 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
630 return B; // Replace the VarBitInit with it.
Chris Lattner577fc3f2004-07-27 01:01:21 +0000631 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000632}
633
Chris Lattneref943742005-04-19 03:36:21 +0000634Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV,
635 unsigned Elt) {
636 if (R.isTemplateArg(getName())) return 0;
637 if (IRV && IRV->getName() != getName()) return 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000638
639 RecordVal *RV = R.getValue(getName());
640 assert(RV && "Reference to a non-existant variable?");
641 ListInit *LI = dynamic_cast<ListInit*>(RV->getValue());
642 assert(LI && "Invalid list element!");
643
644 if (Elt >= LI->getSize())
645 return 0; // Out of range reference.
646 Init *E = LI->getElement(Elt);
647 if (!dynamic_cast<UnsetInit*>(E)) // If the element is set
648 return E; // Replace the VarListElementInit with it.
649 return 0;
650}
651
652
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000653RecTy *VarInit::getFieldType(const std::string &FieldName) const {
654 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
655 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
656 return RV->getType();
657 return 0;
658}
659
660Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
Reid Spencer2a826862006-11-02 20:46:16 +0000661 if (dynamic_cast<RecordRecTy*>(getType()))
Chris Lattnerd959ab92004-02-28 16:31:53 +0000662 if (const RecordVal *RV = R.getValue(VarName)) {
663 Init *TheInit = RV->getValue();
664 assert(TheInit != this && "Infinite loop detected!");
665 if (Init *I = TheInit->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000666 return I;
667 else
668 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +0000669 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000670 return 0;
671}
672
673/// resolveReferences - This method is used by classes that refer to other
674/// variables which may not be defined at the time they expression is formed.
675/// If a value is set for the variable later, this method will be called on
676/// users of the value to allow the value to propagate out.
677///
Chris Lattneref943742005-04-19 03:36:21 +0000678Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000679 if (RecordVal *Val = R.getValue(VarName))
Chris Lattneref943742005-04-19 03:36:21 +0000680 if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue())))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000681 return Val->getValue();
682 return this;
683}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000684
Chris Lattner695506c2007-11-22 21:05:25 +0000685std::string VarBitInit::getAsString() const {
686 return TI->getAsString() + "{" + utostr(Bit) + "}";
687}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000688
Chris Lattneref943742005-04-19 03:36:21 +0000689Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) {
690 if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum()))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000691 return I;
692 return this;
693}
694
Chris Lattner695506c2007-11-22 21:05:25 +0000695std::string VarListElementInit::getAsString() const {
696 return TI->getAsString() + "[" + utostr(Element) + "]";
697}
698
Chris Lattneref943742005-04-19 03:36:21 +0000699Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) {
700 if (Init *I = getVariable()->resolveListElementReference(R, RV,
701 getElementNum()))
Chris Lattner577fc3f2004-07-27 01:01:21 +0000702 return I;
703 return this;
704}
705
Chris Lattneref943742005-04-19 03:36:21 +0000706Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV,
707 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000708 // FIXME: This should be implemented, to support references like:
709 // bit B = AA[0]{1};
710 return 0;
711}
712
Chris Lattneref943742005-04-19 03:36:21 +0000713Init *VarListElementInit::
714resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000715 // FIXME: This should be implemented, to support references like:
716 // int B = AA[0][1];
717 return 0;
718}
719
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000720RecTy *DefInit::getFieldType(const std::string &FieldName) const {
721 if (const RecordVal *RV = Def->getValue(FieldName))
722 return RV->getType();
723 return 0;
724}
725
726Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
727 return Def->getValue(FieldName)->getValue();
728}
729
730
Chris Lattner695506c2007-11-22 21:05:25 +0000731std::string DefInit::getAsString() const {
732 return Def->getName();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000733}
734
Chris Lattneref943742005-04-19 03:36:21 +0000735Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV,
736 unsigned Bit) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000737 if (Init *BitsVal = Rec->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000738 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
739 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
740 Init *B = BI->getBit(Bit);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000741
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000742 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
743 return B; // Replace the VarBitInit with it.
744 }
Chris Lattner577fc3f2004-07-27 01:01:21 +0000745 return 0;
746}
747
Chris Lattneref943742005-04-19 03:36:21 +0000748Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
749 unsigned Elt) {
Chris Lattner577fc3f2004-07-27 01:01:21 +0000750 if (Init *ListVal = Rec->getFieldInit(R, FieldName))
751 if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) {
752 if (Elt >= LI->getSize()) return 0;
753 Init *E = LI->getElement(Elt);
754
755 if (!dynamic_cast<UnsetInit*>(E)) // If the bit is set...
756 return E; // Replace the VarListElementInit with it.
757 }
758 return 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000759}
760
Chris Lattneref943742005-04-19 03:36:21 +0000761Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) {
762 Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
763
764 Init *BitsVal = NewRec->getFieldInit(R, FieldName);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000765 if (BitsVal) {
Chris Lattneref943742005-04-19 03:36:21 +0000766 Init *BVR = BitsVal->resolveReferences(R, RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000767 return BVR->isComplete() ? BVR : this;
768 }
Chris Lattneref943742005-04-19 03:36:21 +0000769
770 if (NewRec != Rec) {
Chris Lattneref943742005-04-19 03:36:21 +0000771 return new FieldInit(NewRec, FieldName);
772 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000773 return this;
774}
775
Chris Lattner0d3ef402006-01-31 06:02:35 +0000776Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) {
777 std::vector<Init*> NewArgs;
778 for (unsigned i = 0, e = Args.size(); i != e; ++i)
779 NewArgs.push_back(Args[i]->resolveReferences(R, RV));
780
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000781 Init *Op = Val->resolveReferences(R, RV);
782
783 if (Args != NewArgs || Op != Val)
Nate Begemandbe3f772009-03-19 05:21:56 +0000784 return new DagInit(Op, "", NewArgs, ArgNames);
Chris Lattner0d3ef402006-01-31 06:02:35 +0000785
786 return this;
787}
788
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000789
Chris Lattner695506c2007-11-22 21:05:25 +0000790std::string DagInit::getAsString() const {
791 std::string Result = "(" + Val->getAsString();
Nate Begemandbe3f772009-03-19 05:21:56 +0000792 if (!ValName.empty())
793 Result += ":" + ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000794 if (Args.size()) {
Chris Lattner695506c2007-11-22 21:05:25 +0000795 Result += " " + Args[0]->getAsString();
796 if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000797 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattner695506c2007-11-22 21:05:25 +0000798 Result += ", " + Args[i]->getAsString();
799 if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000800 }
801 }
Chris Lattner695506c2007-11-22 21:05:25 +0000802 return Result + ")";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000803}
804
805
806//===----------------------------------------------------------------------===//
807// Other implementations
808//===----------------------------------------------------------------------===//
809
810RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
811 : Name(N), Ty(T), Prefix(P) {
812 Value = Ty->convertValue(new UnsetInit());
813 assert(Value && "Cannot create unset value for current type!");
814}
815
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000816void RecordVal::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000817
818void RecordVal::print(std::ostream &OS, bool PrintSem) const {
819 if (getPrefix()) OS << "field ";
820 OS << *getType() << " " << getName();
Chris Lattneref943742005-04-19 03:36:21 +0000821
822 if (getValue())
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000823 OS << " = " << *getValue();
Chris Lattneref943742005-04-19 03:36:21 +0000824
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000825 if (PrintSem) OS << ";\n";
826}
827
Chris Lattnerac284252005-08-19 17:58:11 +0000828void Record::setName(const std::string &Name) {
829 if (Records.getDef(getName()) == this) {
830 Records.removeDef(getName());
831 this->Name = Name;
832 Records.addDef(this);
833 } else {
834 Records.removeClass(getName());
835 this->Name = Name;
836 Records.addClass(this);
837 }
838}
839
Chris Lattneref943742005-04-19 03:36:21 +0000840/// resolveReferencesTo - If anything in this record refers to RV, replace the
841/// reference to RV with the RHS of RV. If RV is null, we resolve all possible
842/// references.
843void Record::resolveReferencesTo(const RecordVal *RV) {
844 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
845 if (Init *V = Values[i].getValue())
846 Values[i].setValue(V->resolveReferences(*this, RV));
847 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000848}
849
Chris Lattneref943742005-04-19 03:36:21 +0000850
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000851void Record::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000852
Chris Lattner68478662004-08-01 03:55:39 +0000853std::ostream &llvm::operator<<(std::ostream &OS, const Record &R) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000854 OS << R.getName();
855
856 const std::vector<std::string> &TArgs = R.getTemplateArgs();
857 if (!TArgs.empty()) {
858 OS << "<";
859 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
860 if (i) OS << ", ";
861 const RecordVal *RV = R.getValue(TArgs[i]);
862 assert(RV && "Template argument record not found??");
863 RV->print(OS, false);
864 }
865 OS << ">";
866 }
867
868 OS << " {";
869 const std::vector<Record*> &SC = R.getSuperClasses();
870 if (!SC.empty()) {
871 OS << "\t//";
872 for (unsigned i = 0, e = SC.size(); i != e; ++i)
873 OS << " " << SC[i]->getName();
874 }
875 OS << "\n";
876
877 const std::vector<RecordVal> &Vals = R.getValues();
878 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
879 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
880 OS << Vals[i];
881 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
882 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
883 OS << Vals[i];
884
885 return OS << "}\n";
886}
887
888/// getValueInit - Return the initializer for a value with the specified name,
889/// or throw an exception if the field does not exist.
890///
891Init *Record::getValueInit(const std::string &FieldName) const {
892 const RecordVal *R = getValue(FieldName);
893 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000894 throw "Record `" + getName() + "' does not have a field named `" +
895 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000896 return R->getValue();
897}
898
899
900/// getValueAsString - This method looks up the specified field and returns its
901/// value as a string, throwing an exception if the field does not exist or if
902/// the value is not a string.
903///
904std::string Record::getValueAsString(const std::string &FieldName) const {
905 const RecordVal *R = getValue(FieldName);
906 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000907 throw "Record `" + getName() + "' does not have a field named `" +
908 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000909
910 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
911 return SI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000912 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000913 "' does not have a string initializer!";
914}
915
916/// getValueAsBitsInit - This method looks up the specified field and returns
917/// its value as a BitsInit, throwing an exception if the field does not exist
918/// or if the value is not the right type.
919///
920BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
921 const RecordVal *R = getValue(FieldName);
922 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000923 throw "Record `" + getName() + "' does not have a field named `" +
924 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000925
926 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
927 return BI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000928 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000929 "' does not have a BitsInit initializer!";
930}
931
932/// getValueAsListInit - This method looks up the specified field and returns
933/// its value as a ListInit, throwing an exception if the field does not exist
934/// or if the value is not the right type.
935///
936ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
937 const RecordVal *R = getValue(FieldName);
938 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000939 throw "Record `" + getName() + "' does not have a field named `" +
940 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000941
942 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
943 return LI;
Misha Brukman41f9f292004-10-08 14:59:05 +0000944 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000945 "' does not have a list initializer!";
946}
947
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000948/// getValueAsListOfDefs - This method looks up the specified field and returns
Jim Laskeyb04feb62005-10-28 21:46:31 +0000949/// its value as a vector of records, throwing an exception if the field does
950/// not exist or if the value is not the right type.
951///
Chris Lattner7ad0bed2005-10-28 22:49:02 +0000952std::vector<Record*>
953Record::getValueAsListOfDefs(const std::string &FieldName) const {
Jim Laskeyb04feb62005-10-28 21:46:31 +0000954 ListInit *List = getValueAsListInit(FieldName);
955 std::vector<Record*> Defs;
956 for (unsigned i = 0; i < List->getSize(); i++) {
957 if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) {
958 Defs.push_back(DI->getDef());
959 } else {
960 throw "Record `" + getName() + "', field `" + FieldName +
961 "' list is not entirely DefInit!";
962 }
963 }
964 return Defs;
965}
966
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000967/// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +0000968/// value as an int64_t, throwing an exception if the field does not exist or if
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000969/// the value is not the right type.
970///
Dan Gohmanca0546f2008-10-17 01:33:43 +0000971int64_t Record::getValueAsInt(const std::string &FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000972 const RecordVal *R = getValue(FieldName);
973 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +0000974 throw "Record `" + getName() + "' does not have a field named `" +
975 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000976
977 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
978 return II->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +0000979 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +0000980 "' does not have an int initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000981}
982
Anton Korobeynikova468a112007-11-11 11:19:37 +0000983/// getValueAsListOfInts - This method looks up the specified field and returns
984/// its value as a vector of integers, throwing an exception if the field does
985/// not exist or if the value is not the right type.
986///
Dan Gohmanca0546f2008-10-17 01:33:43 +0000987std::vector<int64_t>
Anton Korobeynikova468a112007-11-11 11:19:37 +0000988Record::getValueAsListOfInts(const std::string &FieldName) const {
989 ListInit *List = getValueAsListInit(FieldName);
Dan Gohmanca0546f2008-10-17 01:33:43 +0000990 std::vector<int64_t> Ints;
Anton Korobeynikova468a112007-11-11 11:19:37 +0000991 for (unsigned i = 0; i < List->getSize(); i++) {
992 if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) {
993 Ints.push_back(II->getValue());
994 } else {
995 throw "Record `" + getName() + "', field `" + FieldName +
996 "' does not have a list of ints initializer!";
997 }
998 }
999 return Ints;
1000}
1001
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001002/// getValueAsDef - This method looks up the specified field and returns its
1003/// value as a Record, throwing an exception if the field does not exist or if
1004/// the value is not the right type.
1005///
1006Record *Record::getValueAsDef(const std::string &FieldName) const {
1007 const RecordVal *R = getValue(FieldName);
1008 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001009 throw "Record `" + getName() + "' does not have a field named `" +
1010 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001011
1012 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
1013 return DI->getDef();
Misha Brukman41f9f292004-10-08 14:59:05 +00001014 throw "Record `" + getName() + "', field `" + FieldName +
Nate Begemanf621b332005-11-30 18:37:14 +00001015 "' does not have a def initializer!";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001016}
1017
1018/// getValueAsBit - This method looks up the specified field and returns its
1019/// value as a bit, throwing an exception if the field does not exist or if
1020/// the value is not the right type.
1021///
1022bool Record::getValueAsBit(const std::string &FieldName) const {
1023 const RecordVal *R = getValue(FieldName);
1024 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001025 throw "Record `" + getName() + "' does not have a field named `" +
1026 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001027
1028 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
1029 return BI->getValue();
Misha Brukman41f9f292004-10-08 14:59:05 +00001030 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001031 "' does not have a bit initializer!";
1032}
1033
1034/// getValueAsDag - This method looks up the specified field and returns its
1035/// value as an Dag, throwing an exception if the field does not exist or if
1036/// the value is not the right type.
1037///
1038DagInit *Record::getValueAsDag(const std::string &FieldName) const {
1039 const RecordVal *R = getValue(FieldName);
1040 if (R == 0 || R->getValue() == 0)
Misha Brukman41f9f292004-10-08 14:59:05 +00001041 throw "Record `" + getName() + "' does not have a field named `" +
1042 FieldName + "'!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001043
1044 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
1045 return DI;
Misha Brukman41f9f292004-10-08 14:59:05 +00001046 throw "Record `" + getName() + "', field `" + FieldName +
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001047 "' does not have a dag initializer!";
1048}
1049
Chris Lattnerae939eb2005-09-13 21:44:28 +00001050std::string Record::getValueAsCode(const std::string &FieldName) const {
1051 const RecordVal *R = getValue(FieldName);
1052 if (R == 0 || R->getValue() == 0)
1053 throw "Record `" + getName() + "' does not have a field named `" +
1054 FieldName + "'!\n";
1055
1056 if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
1057 return CI->getValue();
1058 throw "Record `" + getName() + "', field `" + FieldName +
1059 "' does not have a code initializer!";
1060}
1061
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001062
Bill Wendling9bfb1e12006-12-07 22:21:48 +00001063void RecordKeeper::dump() const { cerr << *this; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001064
Chris Lattner68478662004-08-01 03:55:39 +00001065std::ostream &llvm::operator<<(std::ostream &OS, const RecordKeeper &RK) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001066 OS << "------------- Classes -----------------\n";
1067 const std::map<std::string, Record*> &Classes = RK.getClasses();
1068 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001069 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001070 OS << "class " << *I->second;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001071
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001072 OS << "------------- Defs -----------------\n";
1073 const std::map<std::string, Record*> &Defs = RK.getDefs();
1074 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001075 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001076 OS << "def " << *I->second;
1077 return OS;
1078}
1079
1080
1081/// getAllDerivedDefinitions - This method returns all concrete definitions
1082/// that derive from the specified class name. If a class with the specified
1083/// name does not exist, an error is printed and true is returned.
1084std::vector<Record*>
1085RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
1086 Record *Class = Records.getClass(ClassName);
1087 if (!Class)
Misha Brukman41f9f292004-10-08 14:59:05 +00001088 throw "ERROR: Couldn't find the `" + ClassName + "' class!\n";
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001089
1090 std::vector<Record*> Defs;
1091 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
1092 E = getDefs().end(); I != E; ++I)
1093 if (I->second->isSubClassOf(Class))
1094 Defs.push_back(I->second);
1095
1096 return Defs;
1097}
Brian Gaeke960707c2003-11-11 22:41:34 +00001098