blob: 315b9e8fb4a1623193b0dd7b63ad3f1705de1fda [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
2//
3// This file contains the actual instruction interpreter.
4//
5//===----------------------------------------------------------------------===//
6
7#include "Interpreter.h"
8#include "ExecutionAnnotations.h"
9#include "llvm/iOther.h"
10#include "llvm/iTerminators.h"
Chris Lattner86660982001-08-27 05:16:50 +000011#include "llvm/iMemory.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000012#include "llvm/Type.h"
13#include "llvm/ConstPoolVals.h"
14#include "llvm/Assembly/Writer.h"
Chris Lattner86660982001-08-27 05:16:50 +000015#include "llvm/Support/DataTypes.h"
Chris Lattner41c2e5c2001-09-28 22:56:43 +000016#include "llvm/Target/TargetData.h"
Chris Lattner2e42d3a2001-10-15 05:51:48 +000017#include "llvm/GlobalVariable.h"
18
19// Create a TargetData structure to handle memory addressing and size/alignment
20// computations
21//
22static TargetData TD("lli Interpreter");
23
24//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000025// Value Manipulation code
26//===----------------------------------------------------------------------===//
27
28static unsigned getOperandSlot(Value *V) {
29 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
30 assert(SN && "Operand does not have a slot number annotation!");
31 return SN->SlotNum;
32}
33
34#define GET_CONST_VAL(TY, CLASS) \
35 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break
36
37static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
38 if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(V)) {
39 GenericValue Result;
40 switch (CPV->getType()->getPrimitiveID()) {
41 GET_CONST_VAL(Bool , ConstPoolBool);
42 GET_CONST_VAL(UByte , ConstPoolUInt);
43 GET_CONST_VAL(SByte , ConstPoolSInt);
44 GET_CONST_VAL(UShort , ConstPoolUInt);
45 GET_CONST_VAL(Short , ConstPoolSInt);
46 GET_CONST_VAL(UInt , ConstPoolUInt);
47 GET_CONST_VAL(Int , ConstPoolSInt);
Chris Lattner7b851ab2001-10-15 19:18:26 +000048 GET_CONST_VAL(ULong , ConstPoolUInt);
49 GET_CONST_VAL(Long , ConstPoolSInt);
Chris Lattner39bb5b42001-10-15 13:25:40 +000050 GET_CONST_VAL(Float , ConstPoolFP);
51 GET_CONST_VAL(Double , ConstPoolFP);
52 case Type::PointerTyID:
53 if (isa<ConstPoolPointerNull>(CPV)) {
54 Result.PointerVal = 0;
55 } else if (ConstPoolPointerRef *CPR =dyn_cast<ConstPoolPointerRef>(CPV)) {
56 assert(0 && "Not implemented!");
57 } else {
58 assert(0 && "Unknown constant pointer type!");
59 }
60 break;
61 default:
62 cout << "ERROR: Constant unimp for type: " << CPV->getType() << endl;
63 }
64 return Result;
65 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
66 GlobalAddress *Address =
67 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
68 GenericValue Result;
69 Result.PointerVal = (GenericValue*)Address->Ptr;
70 return Result;
71 } else {
72 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
73 return SF.Values[TyP][getOperandSlot(V)];
74 }
75}
76
77static void printOperandInfo(Value *V, ExecutionContext &SF) {
78 if (isa<ConstPoolVal>(V)) {
79 cout << "Constant Pool Value\n";
80 } else if (isa<GlobalValue>(V)) {
81 cout << "Global Value\n";
82 } else {
83 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
84 unsigned Slot = getOperandSlot(V);
85 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
86 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF << endl;
87 }
88}
89
90
91
92static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
93 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
94
95 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << endl;
96 SF.Values[TyP][getOperandSlot(V)] = Val;
97}
98
99
100//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000101// Annotation Wrangling code
102//===----------------------------------------------------------------------===//
103
104void Interpreter::initializeExecutionEngine() {
105 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
106 &MethodInfo::Create);
107 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
108 &GlobalAddress::Create);
109}
110
111// InitializeMemory - Recursive function to apply a ConstPool value into the
112// specified memory location...
113//
114static void InitializeMemory(ConstPoolVal *Init, char *Addr) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000115#define INITIALIZE_MEMORY(TYID, CLASS, TY) \
116 case Type::TYID##TyID: { \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000117 TY Tmp = cast<CLASS>(Init)->getValue(); \
Chris Lattner39bb5b42001-10-15 13:25:40 +0000118 memcpy(Addr, &Tmp, sizeof(TY)); \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000119 } return
120
121 switch (Init->getType()->getPrimitiveID()) {
122 INITIALIZE_MEMORY(Bool , ConstPoolBool, bool);
123 INITIALIZE_MEMORY(UByte , ConstPoolUInt, unsigned char);
124 INITIALIZE_MEMORY(SByte , ConstPoolSInt, signed char);
125 INITIALIZE_MEMORY(UShort , ConstPoolUInt, unsigned short);
126 INITIALIZE_MEMORY(Short , ConstPoolSInt, signed short);
127 INITIALIZE_MEMORY(UInt , ConstPoolUInt, unsigned int);
128 INITIALIZE_MEMORY(Int , ConstPoolSInt, signed int);
129 INITIALIZE_MEMORY(ULong , ConstPoolUInt, uint64_t);
130 INITIALIZE_MEMORY(Long , ConstPoolSInt, int64_t);
131 INITIALIZE_MEMORY(Float , ConstPoolFP , float);
132 INITIALIZE_MEMORY(Double , ConstPoolFP , double);
133#undef INITIALIZE_MEMORY
Chris Lattner39bb5b42001-10-15 13:25:40 +0000134
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000135 case Type::ArrayTyID: {
136 ConstPoolArray *CPA = cast<ConstPoolArray>(Init);
137 const vector<Use> &Val = CPA->getValues();
138 unsigned ElementSize =
139 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
140 for (unsigned i = 0; i < Val.size(); ++i)
141 InitializeMemory(cast<ConstPoolVal>(Val[i].get()), Addr+i*ElementSize);
142 return;
143 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000144
145 case Type::StructTyID: {
146 ConstPoolStruct *CPS = cast<ConstPoolStruct>(Init);
147 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
148 const vector<Use> &Val = CPS->getValues();
149 for (unsigned i = 0; i < Val.size(); ++i)
150 InitializeMemory(cast<ConstPoolVal>(Val[i].get()),
151 Addr+SL->MemberOffsets[i]);
152 return;
153 }
154
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000155 case Type::PointerTyID:
Chris Lattner39bb5b42001-10-15 13:25:40 +0000156 if (isa<ConstPoolPointerNull>(Init)) {
157 *(void**)Addr = 0;
158 } else if (ConstPoolPointerRef *CPR = dyn_cast<ConstPoolPointerRef>(Init)) {
159 GlobalAddress *Address =
160 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
161 *(void**)Addr = (GenericValue*)Address->Ptr;
162 } else {
163 assert(0 && "Unknown Constant pointer type!");
164 }
165 return;
166
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000167 default:
168 cout << "Bad Type: " << Init->getType()->getDescription() << endl;
169 assert(0 && "Unknown constant type to initialize memory with!");
170 }
171}
172
173Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
174 assert(AID == GlobalAddressAID);
175
176 // This annotation will only be created on GlobalValue objects...
177 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
178
179 if (isa<Method>(GVal)) {
180 // The GlobalAddress object for a method is just a pointer to method itself.
181 // Don't delete it when the annotation is gone though!
182 return new GlobalAddress(GVal, false);
183 }
184
185 // Handle the case of a global variable...
186 assert(isa<GlobalVariable>(GVal) &&
187 "Global value found that isn't a method or global variable!");
188 GlobalVariable *GV = cast<GlobalVariable>(GVal);
189
190 // First off, we must allocate space for the global variable to point at...
191 const Type *Ty = GV->getType()->getValueType(); // Type to be allocated
192 unsigned NumElements = 1;
193
194 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
195 assert(GV->hasInitializer() && "Const val must have an initializer!");
196 // Allocating a unsized array type?
197 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
198
199 // Get the number of elements being allocated by the array...
200 NumElements =cast<ConstPoolArray>(GV->getInitializer())->getValues().size();
201 }
202
203 // Allocate enough memory to hold the type...
204 void *Addr = malloc(NumElements * TD.getTypeSize(Ty));
205 assert(Addr != 0 && "Null pointer returned by malloc!");
206
207 // Initialize the memory if there is an initializer...
208 if (GV->hasInitializer())
209 InitializeMemory(GV->getInitializer(), (char*)Addr);
210
211 return new GlobalAddress(Addr, true); // Simply invoke the ctor
212}
213
Chris Lattner92101ac2001-08-23 17:05:04 +0000214
215//===----------------------------------------------------------------------===//
216// Binary Instruction Implementations
217//===----------------------------------------------------------------------===//
218
219#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
220 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
Chris Lattner86660982001-08-27 05:16:50 +0000221#define IMPLEMENT_BINARY_PTR_OPERATOR(OP) \
222 case Type::PointerTyID: Dest.PointerVal = \
223 (GenericValue*)((unsigned long)Src1.PointerVal OP (unsigned long)Src2.PointerVal); break
Chris Lattner92101ac2001-08-23 17:05:04 +0000224
225static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
226 const Type *Ty, ExecutionContext &SF) {
227 GenericValue Dest;
228 switch (Ty->getPrimitiveID()) {
229 IMPLEMENT_BINARY_OPERATOR(+, UByte);
230 IMPLEMENT_BINARY_OPERATOR(+, SByte);
231 IMPLEMENT_BINARY_OPERATOR(+, UShort);
232 IMPLEMENT_BINARY_OPERATOR(+, Short);
233 IMPLEMENT_BINARY_OPERATOR(+, UInt);
234 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000235 IMPLEMENT_BINARY_OPERATOR(+, ULong);
236 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000237 IMPLEMENT_BINARY_OPERATOR(+, Float);
238 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000239 IMPLEMENT_BINARY_PTR_OPERATOR(+);
Chris Lattner92101ac2001-08-23 17:05:04 +0000240 default:
241 cout << "Unhandled type for Add instruction: " << Ty << endl;
242 }
243 return Dest;
244}
245
246static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
247 const Type *Ty, ExecutionContext &SF) {
248 GenericValue Dest;
249 switch (Ty->getPrimitiveID()) {
250 IMPLEMENT_BINARY_OPERATOR(-, UByte);
251 IMPLEMENT_BINARY_OPERATOR(-, SByte);
252 IMPLEMENT_BINARY_OPERATOR(-, UShort);
253 IMPLEMENT_BINARY_OPERATOR(-, Short);
254 IMPLEMENT_BINARY_OPERATOR(-, UInt);
255 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000256 IMPLEMENT_BINARY_OPERATOR(-, ULong);
257 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000258 IMPLEMENT_BINARY_OPERATOR(-, Float);
259 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000260 IMPLEMENT_BINARY_PTR_OPERATOR(-);
Chris Lattner92101ac2001-08-23 17:05:04 +0000261 default:
262 cout << "Unhandled type for Sub instruction: " << Ty << endl;
263 }
264 return Dest;
265}
266
267#define IMPLEMENT_SETCC(OP, TY) \
268 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
269
Chris Lattner92101ac2001-08-23 17:05:04 +0000270static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
271 const Type *Ty, ExecutionContext &SF) {
272 GenericValue Dest;
273 switch (Ty->getPrimitiveID()) {
274 IMPLEMENT_SETCC(==, UByte);
275 IMPLEMENT_SETCC(==, SByte);
276 IMPLEMENT_SETCC(==, UShort);
277 IMPLEMENT_SETCC(==, Short);
278 IMPLEMENT_SETCC(==, UInt);
279 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000280 IMPLEMENT_SETCC(==, ULong);
281 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000282 IMPLEMENT_SETCC(==, Float);
283 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000284 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000285 default:
286 cout << "Unhandled type for SetEQ instruction: " << Ty << endl;
287 }
288 return Dest;
289}
290
291static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
292 const Type *Ty, ExecutionContext &SF) {
293 GenericValue Dest;
294 switch (Ty->getPrimitiveID()) {
295 IMPLEMENT_SETCC(!=, UByte);
296 IMPLEMENT_SETCC(!=, SByte);
297 IMPLEMENT_SETCC(!=, UShort);
298 IMPLEMENT_SETCC(!=, Short);
299 IMPLEMENT_SETCC(!=, UInt);
300 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000301 IMPLEMENT_SETCC(!=, ULong);
302 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000303 IMPLEMENT_SETCC(!=, Float);
304 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000305 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000306 default:
307 cout << "Unhandled type for SetNE instruction: " << Ty << endl;
308 }
309 return Dest;
310}
311
312static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
313 const Type *Ty, ExecutionContext &SF) {
314 GenericValue Dest;
315 switch (Ty->getPrimitiveID()) {
316 IMPLEMENT_SETCC(<=, UByte);
317 IMPLEMENT_SETCC(<=, SByte);
318 IMPLEMENT_SETCC(<=, UShort);
319 IMPLEMENT_SETCC(<=, Short);
320 IMPLEMENT_SETCC(<=, UInt);
321 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000322 IMPLEMENT_SETCC(<=, ULong);
323 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000324 IMPLEMENT_SETCC(<=, Float);
325 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000326 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000327 default:
328 cout << "Unhandled type for SetLE instruction: " << Ty << endl;
329 }
330 return Dest;
331}
332
333static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
334 const Type *Ty, ExecutionContext &SF) {
335 GenericValue Dest;
336 switch (Ty->getPrimitiveID()) {
337 IMPLEMENT_SETCC(>=, UByte);
338 IMPLEMENT_SETCC(>=, SByte);
339 IMPLEMENT_SETCC(>=, UShort);
340 IMPLEMENT_SETCC(>=, Short);
341 IMPLEMENT_SETCC(>=, UInt);
342 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000343 IMPLEMENT_SETCC(>=, ULong);
344 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000345 IMPLEMENT_SETCC(>=, Float);
346 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000347 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000348 default:
349 cout << "Unhandled type for SetGE instruction: " << Ty << endl;
350 }
351 return Dest;
352}
353
354static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
355 const Type *Ty, ExecutionContext &SF) {
356 GenericValue Dest;
357 switch (Ty->getPrimitiveID()) {
358 IMPLEMENT_SETCC(<, UByte);
359 IMPLEMENT_SETCC(<, SByte);
360 IMPLEMENT_SETCC(<, UShort);
361 IMPLEMENT_SETCC(<, Short);
362 IMPLEMENT_SETCC(<, UInt);
363 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000364 IMPLEMENT_SETCC(<, ULong);
365 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000366 IMPLEMENT_SETCC(<, Float);
367 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000368 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000369 default:
370 cout << "Unhandled type for SetLT instruction: " << Ty << endl;
371 }
372 return Dest;
373}
374
375static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
376 const Type *Ty, ExecutionContext &SF) {
377 GenericValue Dest;
378 switch (Ty->getPrimitiveID()) {
379 IMPLEMENT_SETCC(>, UByte);
380 IMPLEMENT_SETCC(>, SByte);
381 IMPLEMENT_SETCC(>, UShort);
382 IMPLEMENT_SETCC(>, Short);
383 IMPLEMENT_SETCC(>, UInt);
384 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000385 IMPLEMENT_SETCC(>, ULong);
386 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000387 IMPLEMENT_SETCC(>, Float);
388 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000389 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000390 default:
391 cout << "Unhandled type for SetGT instruction: " << Ty << endl;
392 }
393 return Dest;
394}
395
396static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
397 const Type *Ty = I->getOperand(0)->getType();
398 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
399 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
400 GenericValue R; // Result
401
402 switch (I->getOpcode()) {
403 case Instruction::Add: R = executeAddInst(Src1, Src2, Ty, SF); break;
404 case Instruction::Sub: R = executeSubInst(Src1, Src2, Ty, SF); break;
405 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
406 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
407 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
408 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
409 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
410 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
411 default:
412 cout << "Don't know how to handle this binary operator!\n-->" << I;
413 }
414
415 SetValue(I, R, SF);
416}
417
Chris Lattner92101ac2001-08-23 17:05:04 +0000418//===----------------------------------------------------------------------===//
419// Terminator Instruction Implementations
420//===----------------------------------------------------------------------===//
421
422void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
423 const Type *RetTy = 0;
424 GenericValue Result;
425
426 // Save away the return value... (if we are not 'ret void')
427 if (I->getNumOperands()) {
428 RetTy = I->getReturnValue()->getType();
429 Result = getOperandValue(I->getReturnValue(), SF);
430 }
431
432 // Save previously executing meth
433 const Method *M = ECStack.back().CurMethod;
434
435 // Pop the current stack frame... this invalidates SF
436 ECStack.pop_back();
437
438 if (ECStack.empty()) { // Finished main. Put result into exit code...
439 if (RetTy) { // Nonvoid return type?
440 cout << "Method " << M->getType() << " \"" << M->getName()
441 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000442 print(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000443 cout << endl;
444
445 if (RetTy->isIntegral())
446 ExitCode = Result.SByteVal; // Capture the exit code of the program
447 } else {
448 ExitCode = 0;
449 }
450 return;
451 }
452
453 // If we have a previous stack frame, and we have a previous call, fill in
454 // the return value...
455 //
456 ExecutionContext &NewSF = ECStack.back();
457 if (NewSF.Caller) {
458 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
459 SetValue(NewSF.Caller, Result, NewSF);
460
461 NewSF.Caller = 0; // We returned from the call...
Chris Lattner365a76e2001-09-10 04:49:44 +0000462 } else {
463 // This must be a function that is executing because of a user 'call'
464 // instruction.
465 cout << "Method " << M->getType() << " \"" << M->getName()
466 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000467 print(RetTy, Result);
Chris Lattner365a76e2001-09-10 04:49:44 +0000468 cout << endl;
Chris Lattner92101ac2001-08-23 17:05:04 +0000469 }
470}
471
472void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
473 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
474 BasicBlock *Dest;
475
476 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
477 if (!I->isUnconditional()) {
478 if (getOperandValue(I->getCondition(), SF).BoolVal == 0) // If false cond...
479 Dest = I->getSuccessor(1);
480 }
481 SF.CurBB = Dest; // Update CurBB to branch destination
482 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
483}
484
485//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000486// Memory Instruction Implementations
487//===----------------------------------------------------------------------===//
488
Chris Lattner86660982001-08-27 05:16:50 +0000489void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
490 const Type *Ty = I->getType()->getValueType(); // Type to be allocated
491 unsigned NumElements = 1;
492
493 if (I->getNumOperands()) { // Allocating a unsized array type?
Chris Lattnerb00c5822001-10-02 03:41:24 +0000494 assert(isa<ArrayType>(Ty) && cast<const ArrayType>(Ty)->isUnsized() &&
Chris Lattner86660982001-08-27 05:16:50 +0000495 "Allocation inst with size operand for !unsized array type???");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000496 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
Chris Lattner86660982001-08-27 05:16:50 +0000497
498 // Get the number of elements being allocated by the array...
499 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
500 NumElements = NumEl.UIntVal;
501 }
502
503 // Allocate enough memory to hold the type...
504 GenericValue Result;
505 Result.PointerVal = (GenericValue*)malloc(NumElements * TD.getTypeSize(Ty));
506 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
507 SetValue(I, Result, SF);
508
509 if (I->getOpcode() == Instruction::Alloca) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000510 // TODO: FIXME: alloca should keep track of memory to free it later...
Chris Lattner86660982001-08-27 05:16:50 +0000511 }
512}
513
514static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
515 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
516 GenericValue Value = getOperandValue(I->getOperand(0), SF);
517 // TODO: Check to make sure memory is allocated
518 free(Value.PointerVal); // Free memory
519}
520
521static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
522 assert(I->getNumOperands() == 1 && "NI!");
523 GenericValue *Ptr = getOperandValue(I->getPtrOperand(), SF).PointerVal;
524 GenericValue Result;
525
526 switch (I->getType()->getPrimitiveID()) {
527 case Type::BoolTyID:
528 case Type::UByteTyID:
529 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
530 case Type::UShortTyID:
531 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
532 case Type::UIntTyID:
533 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000534 case Type::ULongTyID:
535 case Type::LongTyID: Result.LongVal = Ptr->LongVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000536 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
537 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
538 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
539 default:
540 cout << "Cannot load value of type " << I->getType() << "!\n";
541 }
542
543 SetValue(I, Result, SF);
544}
545
546static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
547 GenericValue *Ptr = getOperandValue(I->getPtrOperand(), SF).PointerVal;
548 GenericValue Val = getOperandValue(I->getOperand(0), SF);
549 assert(I->getNumOperands() == 2 && "NI!");
550
551 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
552 case Type::BoolTyID:
553 case Type::UByteTyID:
554 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
555 case Type::UShortTyID:
556 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
557 case Type::UIntTyID:
558 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000559 case Type::ULongTyID:
560 case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000561 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
562 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
563 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
564 default:
565 cout << "Cannot store value of type " << I->getType() << "!\n";
566 }
567}
568
569
570//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000571// Miscellaneous Instruction Implementations
572//===----------------------------------------------------------------------===//
573
574void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
575 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000576 vector<GenericValue> ArgVals;
577 ArgVals.reserve(I->getNumOperands()-1);
578 for (unsigned i = 1; i < I->getNumOperands(); ++i)
579 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
580
581 callMethod(I->getCalledMethod(), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000582}
583
584static void executePHINode(PHINode *I, ExecutionContext &SF) {
585 BasicBlock *PrevBB = SF.PrevBB;
586 Value *IncomingValue = 0;
587
588 // Search for the value corresponding to this previous bb...
589 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
590 if (I->getIncomingBlock(--i) == PrevBB) {
591 IncomingValue = I->getIncomingValue(i);
592 break;
593 }
594 }
595 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
596
597 // Found the value, set as the result...
598 SetValue(I, getOperandValue(IncomingValue, SF), SF);
599}
600
Chris Lattner86660982001-08-27 05:16:50 +0000601#define IMPLEMENT_SHIFT(OP, TY) \
602 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
603
604static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
605 const Type *Ty = I->getOperand(0)->getType();
606 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
607 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
608 GenericValue Dest;
609
610 switch (Ty->getPrimitiveID()) {
611 IMPLEMENT_SHIFT(<<, UByte);
612 IMPLEMENT_SHIFT(<<, SByte);
613 IMPLEMENT_SHIFT(<<, UShort);
614 IMPLEMENT_SHIFT(<<, Short);
615 IMPLEMENT_SHIFT(<<, UInt);
616 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000617 IMPLEMENT_SHIFT(<<, ULong);
618 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000619 default:
620 cout << "Unhandled type for Shl instruction: " << Ty << endl;
621 }
622 SetValue(I, Dest, SF);
623}
624
625static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
626 const Type *Ty = I->getOperand(0)->getType();
627 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
628 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
629 GenericValue Dest;
630
631 switch (Ty->getPrimitiveID()) {
632 IMPLEMENT_SHIFT(>>, UByte);
633 IMPLEMENT_SHIFT(>>, SByte);
634 IMPLEMENT_SHIFT(>>, UShort);
635 IMPLEMENT_SHIFT(>>, Short);
636 IMPLEMENT_SHIFT(>>, UInt);
637 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000638 IMPLEMENT_SHIFT(>>, ULong);
639 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000640 default:
641 cout << "Unhandled type for Shr instruction: " << Ty << endl;
642 }
643 SetValue(I, Dest, SF);
644}
645
646#define IMPLEMENT_CAST(DTY, DCTY, STY) \
647 case Type::STY##TyID: Dest.DTY##Val = (DCTY)Src.STY##Val; break;
648
649#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
650 case Type::DESTTY##TyID: \
651 switch (SrcTy->getPrimitiveID()) { \
652 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
653 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
654 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
655 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
656 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000657 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
658 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
659 IMPLEMENT_CAST(DESTTY, DESTCTY, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000660
661#define IMPLEMENT_CAST_CASE_PTR_IMP(DESTTY, DESTCTY) \
662 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer)
663
664#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
665 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
666 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
667
668#define IMPLEMENT_CAST_CASE_END() \
669 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << endl; \
670 break; \
671 } \
672 break
673
674#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
675 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
676 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
677 IMPLEMENT_CAST_CASE_PTR_IMP(DESTTY, DESTCTY); \
678 IMPLEMENT_CAST_CASE_END()
679
680#define IMPLEMENT_CAST_CASE_FP(DESTTY, DESTCTY) \
681 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
682 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
683 IMPLEMENT_CAST_CASE_END()
684
685#define IMPLEMENT_CAST_CASE_PTR(DESTTY, DESTCTY) \
686 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
687 IMPLEMENT_CAST_CASE_PTR_IMP(DESTTY, DESTCTY); \
688 IMPLEMENT_CAST_CASE_END()
689
690static void executeCastInst(CastInst *I, ExecutionContext &SF) {
691 const Type *Ty = I->getType();
692 const Type *SrcTy = I->getOperand(0)->getType();
693 GenericValue Src = getOperandValue(I->getOperand(0), SF);
694 GenericValue Dest;
695
696 switch (Ty->getPrimitiveID()) {
697 IMPLEMENT_CAST_CASE(UByte , unsigned char);
698 IMPLEMENT_CAST_CASE(SByte , signed char);
699 IMPLEMENT_CAST_CASE(UShort, unsigned short);
700 IMPLEMENT_CAST_CASE(Short , signed char);
701 IMPLEMENT_CAST_CASE(UInt , unsigned int );
702 IMPLEMENT_CAST_CASE(Int , signed int );
Chris Lattner7b851ab2001-10-15 19:18:26 +0000703 IMPLEMENT_CAST_CASE(ULong , uint64_t );
704 IMPLEMENT_CAST_CASE(Long , int64_t );
Chris Lattner86660982001-08-27 05:16:50 +0000705 IMPLEMENT_CAST_CASE_FP(Float , float);
706 IMPLEMENT_CAST_CASE_FP(Double, double);
707 IMPLEMENT_CAST_CASE_PTR(Pointer, GenericValue *);
Chris Lattner86660982001-08-27 05:16:50 +0000708 default:
709 cout << "Unhandled dest type for cast instruction: " << Ty << endl;
710 }
711 SetValue(I, Dest, SF);
712}
Chris Lattner92101ac2001-08-23 17:05:04 +0000713
714
715
716
717//===----------------------------------------------------------------------===//
718// Dispatch and Execution Code
719//===----------------------------------------------------------------------===//
720
721MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
722 // Assign slot numbers to the method arguments...
723 const Method::ArgumentListType &ArgList = M->getArgumentList();
724 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
725 AE = ArgList.end(); AI != AE; ++AI) {
726 MethodArgument *MA = *AI;
727 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
728 }
729
730 // Iterate over all of the instructions...
731 unsigned InstNum = 0;
732 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
733 MI != ME; ++MI) {
734 Instruction *I = *MI; // For each instruction...
735 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
736 }
737}
738
739unsigned MethodInfo::getValueSlot(const Value *V) {
740 unsigned Plane = V->getType()->getUniqueID();
741 if (Plane >= NumPlaneElements.size())
742 NumPlaneElements.resize(Plane+1, 0);
743 return NumPlaneElements[Plane]++;
744}
745
746
Chris Lattner92101ac2001-08-23 17:05:04 +0000747//===----------------------------------------------------------------------===//
748// callMethod - Execute the specified method...
749//
Chris Lattner365a76e2001-09-10 04:49:44 +0000750void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
751 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
752 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
753 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +0000754 if (M->isExternal()) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000755 callExternalMethod(M, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000756 return;
757 }
758
759 // Process the method, assigning instruction numbers to the instructions in
760 // the method. Also calculate the number of values for each type slot active.
761 //
762 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +0000763 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +0000764
Chris Lattner92101ac2001-08-23 17:05:04 +0000765 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
766 StackFrame.CurMethod = M;
767 StackFrame.CurBB = M->front();
768 StackFrame.CurInst = StackFrame.CurBB->begin();
769 StackFrame.MethInfo = MethInfo;
770
771 // Initialize the values to nothing...
772 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
773 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i)
774 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
775
776 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
777
Chris Lattner92101ac2001-08-23 17:05:04 +0000778
Chris Lattner365a76e2001-09-10 04:49:44 +0000779 // Run through the method arguments and initialize their values...
780 unsigned i = 0;
781 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
782 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
783 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +0000784 }
785}
786
787// executeInstruction - Interpret a single instruction, increment the "PC", and
788// return true if the next instruction is a breakpoint...
789//
790bool Interpreter::executeInstruction() {
791 assert(!ECStack.empty() && "No program running, cannot execute inst!");
792
793 ExecutionContext &SF = ECStack.back(); // Current stack frame
794 Instruction *I = *SF.CurInst++; // Increment before execute
795
796 if (I->isBinaryOp()) {
797 executeBinaryInst((BinaryOperator*)I, SF);
798 } else {
799 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +0000800 // Terminators
Chris Lattner92101ac2001-08-23 17:05:04 +0000801 case Instruction::Ret: executeRetInst ((ReturnInst*)I, SF); break;
802 case Instruction::Br: executeBrInst ((BranchInst*)I, SF); break;
Chris Lattner86660982001-08-27 05:16:50 +0000803 // Memory Instructions
804 case Instruction::Alloca:
805 case Instruction::Malloc: executeAllocInst ((AllocationInst*)I, SF); break;
Chris Lattnerb00c5822001-10-02 03:41:24 +0000806 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
807 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
808 case Instruction::Store: executeStoreInst (cast<StoreInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +0000809
810 // Miscellaneous Instructions
Chris Lattnerb00c5822001-10-02 03:41:24 +0000811 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
812 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
813 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
814 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
815 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000816 default:
817 cout << "Don't know how to execute this instruction!\n-->" << I;
818 }
819 }
820
821 // Reset the current frame location to the top of stack
822 CurFrame = ECStack.size()-1;
823
824 if (CurFrame == -1) return false; // No breakpoint if no code
825
826 // Return true if there is a breakpoint annotation on the instruction...
827 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
828}
829
830void Interpreter::stepInstruction() { // Do the 'step' command
831 if (ECStack.empty()) {
832 cout << "Error: no program running, cannot step!\n";
833 return;
834 }
835
836 // Run an instruction...
837 executeInstruction();
838
839 // Print the next instruction to execute...
840 printCurrentInstruction();
841}
842
843// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +0000844void Interpreter::nextInstruction() { // Do the 'next' command
845 if (ECStack.empty()) {
846 cout << "Error: no program running, cannot 'next'!\n";
847 return;
848 }
849
850 // If this is a call instruction, step over the call instruction...
851 // TODO: ICALL, CALL WITH, ...
852 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
853 // Step into the function...
854 if (executeInstruction()) {
855 // Hit a breakpoint, print current instruction, then return to user...
856 cout << "Breakpoint hit!\n";
857 printCurrentInstruction();
858 return;
859 }
860
861 // Finish executing the function...
862 finish();
863 } else {
864 // Normal instruction, just step...
865 stepInstruction();
866 }
867}
868
869void Interpreter::run() {
870 if (ECStack.empty()) {
871 cout << "Error: no program running, cannot run!\n";
872 return;
873 }
874
875 bool HitBreakpoint = false;
876 while (!ECStack.empty() && !HitBreakpoint) {
877 // Run an instruction...
878 HitBreakpoint = executeInstruction();
879 }
880
881 if (HitBreakpoint) {
882 cout << "Breakpoint hit!\n";
883 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000884 // Print the next instruction to execute...
885 printCurrentInstruction();
886}
887
888void Interpreter::finish() {
889 if (ECStack.empty()) {
890 cout << "Error: no program running, cannot run!\n";
891 return;
892 }
893
894 unsigned StackSize = ECStack.size();
895 bool HitBreakpoint = false;
896 while (ECStack.size() >= StackSize && !HitBreakpoint) {
897 // Run an instruction...
898 HitBreakpoint = executeInstruction();
899 }
900
901 if (HitBreakpoint) {
902 cout << "Breakpoint hit!\n";
903 }
904
905 // Print the next instruction to execute...
906 printCurrentInstruction();
907}
908
909
910
911// printCurrentInstruction - Print out the instruction that the virtual PC is
912// at, or fail silently if no program is running.
913//
914void Interpreter::printCurrentInstruction() {
915 if (!ECStack.empty()) {
916 Instruction *I = *ECStack.back().CurInst;
917 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
918 assert(IN && "Instruction has no numbering annotation!");
919 cout << "#" << IN->InstNum << I;
920 }
921}
922
923void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000924 switch (Ty->getPrimitiveID()) {
925 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
926 case Type::SByteTyID: cout << V.SByteVal; break;
927 case Type::UByteTyID: cout << V.UByteVal; break;
928 case Type::ShortTyID: cout << V.ShortVal; break;
929 case Type::UShortTyID: cout << V.UShortVal; break;
930 case Type::IntTyID: cout << V.IntVal; break;
931 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000932 case Type::LongTyID: cout << V.LongVal; break;
933 case Type::ULongTyID: cout << V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000934 case Type::FloatTyID: cout << V.FloatVal; break;
935 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000936 case Type::PointerTyID:cout << V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000937 default:
938 cout << "- Don't know how to print value of this type!";
939 break;
940 }
941}
942
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000943void Interpreter::print(const Type *Ty, GenericValue V) {
944 cout << Ty << " ";
945 printValue(Ty, V);
946}
947
948void Interpreter::print(const string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000949 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
950 if (!PickedVal) return;
951
Chris Lattner9636a912001-10-01 16:18:37 +0000952 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000953 cout << M; // Print the method
954 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000955 print(PickedVal->getType(),
956 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner92101ac2001-08-23 17:05:04 +0000957 cout << endl;
958 }
959
960}
961
Chris Lattner86660982001-08-27 05:16:50 +0000962void Interpreter::infoValue(const string &Name) {
963 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
964 if (!PickedVal) return;
965
966 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000967 print(PickedVal->getType(),
968 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner86660982001-08-27 05:16:50 +0000969 cout << endl;
970 printOperandInfo(PickedVal, ECStack[CurFrame]);
971}
972
Chris Lattner92101ac2001-08-23 17:05:04 +0000973void Interpreter::list() {
974 if (ECStack.empty())
975 cout << "Error: No program executing!\n";
976 else
977 cout << ECStack[CurFrame].CurMethod; // Just print the method out...
978}
979
980void Interpreter::printStackTrace() {
981 if (ECStack.empty()) cout << "No program executing!\n";
982
983 for (unsigned i = 0; i < ECStack.size(); ++i) {
984 cout << (((int)i == CurFrame) ? '>' : '-');
985 cout << "#" << i << ". " << ECStack[i].CurMethod->getType() << " \""
986 << ECStack[i].CurMethod->getName() << "\"(";
987 // TODO: Print Args
988 cout << ")" << endl;
989 cout << *ECStack[i].CurInst;
990 }
991}