blob: a7ded5b705b6f1dd840540a3f5f74d39def042ec [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)) {
Chris Lattnerc2593162001-10-27 08:28:11 +000054 Result.ULongVal = 0;
Chris Lattner39bb5b42001-10-15 13:25:40 +000055 } 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;
Chris Lattnerc2593162001-10-27 08:28:11 +000069 Result.ULongVal = (uint64_t)(GenericValue*)Address->Ptr;
Chris Lattner39bb5b42001-10-15 13:25:40 +000070 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
221
222static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
223 const Type *Ty, ExecutionContext &SF) {
224 GenericValue Dest;
225 switch (Ty->getPrimitiveID()) {
226 IMPLEMENT_BINARY_OPERATOR(+, UByte);
227 IMPLEMENT_BINARY_OPERATOR(+, SByte);
228 IMPLEMENT_BINARY_OPERATOR(+, UShort);
229 IMPLEMENT_BINARY_OPERATOR(+, Short);
230 IMPLEMENT_BINARY_OPERATOR(+, UInt);
231 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000232 IMPLEMENT_BINARY_OPERATOR(+, ULong);
233 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000234 IMPLEMENT_BINARY_OPERATOR(+, Float);
235 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000236 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000237 default:
238 cout << "Unhandled type for Add instruction: " << Ty << endl;
239 }
240 return Dest;
241}
242
243static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
244 const Type *Ty, ExecutionContext &SF) {
245 GenericValue Dest;
246 switch (Ty->getPrimitiveID()) {
247 IMPLEMENT_BINARY_OPERATOR(-, UByte);
248 IMPLEMENT_BINARY_OPERATOR(-, SByte);
249 IMPLEMENT_BINARY_OPERATOR(-, UShort);
250 IMPLEMENT_BINARY_OPERATOR(-, Short);
251 IMPLEMENT_BINARY_OPERATOR(-, UInt);
252 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000253 IMPLEMENT_BINARY_OPERATOR(-, ULong);
254 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000255 IMPLEMENT_BINARY_OPERATOR(-, Float);
256 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000257 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000258 default:
259 cout << "Unhandled type for Sub instruction: " << Ty << endl;
260 }
261 return Dest;
262}
263
Chris Lattnerc2593162001-10-27 08:28:11 +0000264static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
265 const Type *Ty, ExecutionContext &SF) {
266 GenericValue Dest;
267 switch (Ty->getPrimitiveID()) {
268 IMPLEMENT_BINARY_OPERATOR(*, UByte);
269 IMPLEMENT_BINARY_OPERATOR(*, SByte);
270 IMPLEMENT_BINARY_OPERATOR(*, UShort);
271 IMPLEMENT_BINARY_OPERATOR(*, Short);
272 IMPLEMENT_BINARY_OPERATOR(*, UInt);
273 IMPLEMENT_BINARY_OPERATOR(*, Int);
274 IMPLEMENT_BINARY_OPERATOR(*, ULong);
275 IMPLEMENT_BINARY_OPERATOR(*, Long);
276 IMPLEMENT_BINARY_OPERATOR(*, Float);
277 IMPLEMENT_BINARY_OPERATOR(*, Double);
278 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
279 default:
280 cout << "Unhandled type for Mul instruction: " << Ty << endl;
281 }
282 return Dest;
283}
284
285static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
286 const Type *Ty, ExecutionContext &SF) {
287 GenericValue Dest;
288 switch (Ty->getPrimitiveID()) {
289 IMPLEMENT_BINARY_OPERATOR(/, UByte);
290 IMPLEMENT_BINARY_OPERATOR(/, SByte);
291 IMPLEMENT_BINARY_OPERATOR(/, UShort);
292 IMPLEMENT_BINARY_OPERATOR(/, Short);
293 IMPLEMENT_BINARY_OPERATOR(/, UInt);
294 IMPLEMENT_BINARY_OPERATOR(/, Int);
295 IMPLEMENT_BINARY_OPERATOR(/, ULong);
296 IMPLEMENT_BINARY_OPERATOR(/, Long);
297 IMPLEMENT_BINARY_OPERATOR(/, Float);
298 IMPLEMENT_BINARY_OPERATOR(/, Double);
299 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
300 default:
301 cout << "Unhandled type for Mul instruction: " << Ty << endl;
302 }
303 return Dest;
304}
305
Chris Lattner92101ac2001-08-23 17:05:04 +0000306#define IMPLEMENT_SETCC(OP, TY) \
307 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
308
Chris Lattner92101ac2001-08-23 17:05:04 +0000309static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
310 const Type *Ty, ExecutionContext &SF) {
311 GenericValue Dest;
312 switch (Ty->getPrimitiveID()) {
313 IMPLEMENT_SETCC(==, UByte);
314 IMPLEMENT_SETCC(==, SByte);
315 IMPLEMENT_SETCC(==, UShort);
316 IMPLEMENT_SETCC(==, Short);
317 IMPLEMENT_SETCC(==, UInt);
318 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000319 IMPLEMENT_SETCC(==, ULong);
320 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000321 IMPLEMENT_SETCC(==, Float);
322 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000323 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000324 default:
325 cout << "Unhandled type for SetEQ instruction: " << Ty << endl;
326 }
327 return Dest;
328}
329
330static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
331 const Type *Ty, ExecutionContext &SF) {
332 GenericValue Dest;
333 switch (Ty->getPrimitiveID()) {
334 IMPLEMENT_SETCC(!=, UByte);
335 IMPLEMENT_SETCC(!=, SByte);
336 IMPLEMENT_SETCC(!=, UShort);
337 IMPLEMENT_SETCC(!=, Short);
338 IMPLEMENT_SETCC(!=, UInt);
339 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000340 IMPLEMENT_SETCC(!=, ULong);
341 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000342 IMPLEMENT_SETCC(!=, Float);
343 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000344 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000345 default:
346 cout << "Unhandled type for SetNE instruction: " << Ty << endl;
347 }
348 return Dest;
349}
350
351static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
352 const Type *Ty, ExecutionContext &SF) {
353 GenericValue Dest;
354 switch (Ty->getPrimitiveID()) {
355 IMPLEMENT_SETCC(<=, UByte);
356 IMPLEMENT_SETCC(<=, SByte);
357 IMPLEMENT_SETCC(<=, UShort);
358 IMPLEMENT_SETCC(<=, Short);
359 IMPLEMENT_SETCC(<=, UInt);
360 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000361 IMPLEMENT_SETCC(<=, ULong);
362 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000363 IMPLEMENT_SETCC(<=, Float);
364 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000365 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000366 default:
367 cout << "Unhandled type for SetLE instruction: " << Ty << endl;
368 }
369 return Dest;
370}
371
372static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
373 const Type *Ty, ExecutionContext &SF) {
374 GenericValue Dest;
375 switch (Ty->getPrimitiveID()) {
376 IMPLEMENT_SETCC(>=, UByte);
377 IMPLEMENT_SETCC(>=, SByte);
378 IMPLEMENT_SETCC(>=, UShort);
379 IMPLEMENT_SETCC(>=, Short);
380 IMPLEMENT_SETCC(>=, UInt);
381 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000382 IMPLEMENT_SETCC(>=, ULong);
383 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000384 IMPLEMENT_SETCC(>=, Float);
385 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000386 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000387 default:
388 cout << "Unhandled type for SetGE instruction: " << Ty << endl;
389 }
390 return Dest;
391}
392
393static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
394 const Type *Ty, ExecutionContext &SF) {
395 GenericValue Dest;
396 switch (Ty->getPrimitiveID()) {
397 IMPLEMENT_SETCC(<, UByte);
398 IMPLEMENT_SETCC(<, SByte);
399 IMPLEMENT_SETCC(<, UShort);
400 IMPLEMENT_SETCC(<, Short);
401 IMPLEMENT_SETCC(<, UInt);
402 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000403 IMPLEMENT_SETCC(<, ULong);
404 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000405 IMPLEMENT_SETCC(<, Float);
406 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000407 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000408 default:
409 cout << "Unhandled type for SetLT instruction: " << Ty << endl;
410 }
411 return Dest;
412}
413
414static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
415 const Type *Ty, ExecutionContext &SF) {
416 GenericValue Dest;
417 switch (Ty->getPrimitiveID()) {
418 IMPLEMENT_SETCC(>, UByte);
419 IMPLEMENT_SETCC(>, SByte);
420 IMPLEMENT_SETCC(>, UShort);
421 IMPLEMENT_SETCC(>, Short);
422 IMPLEMENT_SETCC(>, UInt);
423 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000424 IMPLEMENT_SETCC(>, ULong);
425 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000426 IMPLEMENT_SETCC(>, Float);
427 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000428 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000429 default:
430 cout << "Unhandled type for SetGT instruction: " << Ty << endl;
431 }
432 return Dest;
433}
434
435static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
436 const Type *Ty = I->getOperand(0)->getType();
437 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
438 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
439 GenericValue R; // Result
440
441 switch (I->getOpcode()) {
442 case Instruction::Add: R = executeAddInst(Src1, Src2, Ty, SF); break;
443 case Instruction::Sub: R = executeSubInst(Src1, Src2, Ty, SF); break;
Chris Lattnerc2593162001-10-27 08:28:11 +0000444 case Instruction::Mul: R = executeMulInst(Src1, Src2, Ty, SF); break;
445 case Instruction::Div: R = executeDivInst(Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000446 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
447 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
448 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
449 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
450 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
451 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
452 default:
453 cout << "Don't know how to handle this binary operator!\n-->" << I;
454 }
455
456 SetValue(I, R, SF);
457}
458
Chris Lattner92101ac2001-08-23 17:05:04 +0000459//===----------------------------------------------------------------------===//
460// Terminator Instruction Implementations
461//===----------------------------------------------------------------------===//
462
Chris Lattnere43db882001-10-27 04:15:57 +0000463void Interpreter::exitCalled(GenericValue GV) {
464 cout << "Program returned ";
465 print(Type::IntTy, GV);
466 cout << " via 'void exit(int)'\n";
467
468 ExitCode = GV.SByteVal;
469 ECStack.clear();
470}
471
Chris Lattner92101ac2001-08-23 17:05:04 +0000472void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
473 const Type *RetTy = 0;
474 GenericValue Result;
475
476 // Save away the return value... (if we are not 'ret void')
477 if (I->getNumOperands()) {
478 RetTy = I->getReturnValue()->getType();
479 Result = getOperandValue(I->getReturnValue(), SF);
480 }
481
482 // Save previously executing meth
483 const Method *M = ECStack.back().CurMethod;
484
485 // Pop the current stack frame... this invalidates SF
486 ECStack.pop_back();
487
488 if (ECStack.empty()) { // Finished main. Put result into exit code...
489 if (RetTy) { // Nonvoid return type?
490 cout << "Method " << M->getType() << " \"" << M->getName()
491 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000492 print(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000493 cout << endl;
494
495 if (RetTy->isIntegral())
496 ExitCode = Result.SByteVal; // Capture the exit code of the program
497 } else {
498 ExitCode = 0;
499 }
500 return;
501 }
502
503 // If we have a previous stack frame, and we have a previous call, fill in
504 // the return value...
505 //
506 ExecutionContext &NewSF = ECStack.back();
507 if (NewSF.Caller) {
508 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
509 SetValue(NewSF.Caller, Result, NewSF);
510
511 NewSF.Caller = 0; // We returned from the call...
Chris Lattner365a76e2001-09-10 04:49:44 +0000512 } else {
513 // This must be a function that is executing because of a user 'call'
514 // instruction.
515 cout << "Method " << M->getType() << " \"" << M->getName()
516 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000517 print(RetTy, Result);
Chris Lattner365a76e2001-09-10 04:49:44 +0000518 cout << endl;
Chris Lattner92101ac2001-08-23 17:05:04 +0000519 }
520}
521
522void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
523 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
524 BasicBlock *Dest;
525
526 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
527 if (!I->isUnconditional()) {
528 if (getOperandValue(I->getCondition(), SF).BoolVal == 0) // If false cond...
529 Dest = I->getSuccessor(1);
530 }
531 SF.CurBB = Dest; // Update CurBB to branch destination
532 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
533}
534
535//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000536// Memory Instruction Implementations
537//===----------------------------------------------------------------------===//
538
Chris Lattner86660982001-08-27 05:16:50 +0000539void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
540 const Type *Ty = I->getType()->getValueType(); // Type to be allocated
541 unsigned NumElements = 1;
542
543 if (I->getNumOperands()) { // Allocating a unsized array type?
Chris Lattnerb00c5822001-10-02 03:41:24 +0000544 assert(isa<ArrayType>(Ty) && cast<const ArrayType>(Ty)->isUnsized() &&
Chris Lattner86660982001-08-27 05:16:50 +0000545 "Allocation inst with size operand for !unsized array type???");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000546 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
Chris Lattner86660982001-08-27 05:16:50 +0000547
548 // Get the number of elements being allocated by the array...
549 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
550 NumElements = NumEl.UIntVal;
551 }
552
553 // Allocate enough memory to hold the type...
554 GenericValue Result;
Chris Lattnerc2593162001-10-27 08:28:11 +0000555 Result.ULongVal = (uint64_t)malloc(NumElements * TD.getTypeSize(Ty));
556 assert(Result.ULongVal != 0 && "Null pointer returned by malloc!");
Chris Lattner86660982001-08-27 05:16:50 +0000557 SetValue(I, Result, SF);
558
559 if (I->getOpcode() == Instruction::Alloca) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000560 // TODO: FIXME: alloca should keep track of memory to free it later...
Chris Lattner86660982001-08-27 05:16:50 +0000561 }
562}
563
564static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
565 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
566 GenericValue Value = getOperandValue(I->getOperand(0), SF);
567 // TODO: Check to make sure memory is allocated
Chris Lattnerc2593162001-10-27 08:28:11 +0000568 free((void*)Value.ULongVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000569}
570
571static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
572 assert(I->getNumOperands() == 1 && "NI!");
Chris Lattnerc2593162001-10-27 08:28:11 +0000573 GenericValue *Ptr =
574 (GenericValue*)getOperandValue(I->getPtrOperand(), SF).ULongVal;
Chris Lattner86660982001-08-27 05:16:50 +0000575 GenericValue Result;
576
577 switch (I->getType()->getPrimitiveID()) {
578 case Type::BoolTyID:
579 case Type::UByteTyID:
580 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
581 case Type::UShortTyID:
582 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
583 case Type::UIntTyID:
584 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000585 case Type::ULongTyID:
Chris Lattnerc2593162001-10-27 08:28:11 +0000586 case Type::LongTyID:
587 case Type::PointerTyID: Result.ULongVal = Ptr->ULongVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000588 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
589 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000590 default:
591 cout << "Cannot load value of type " << I->getType() << "!\n";
592 }
593
594 SetValue(I, Result, SF);
595}
596
597static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000598 GenericValue *Ptr =
599 (GenericValue *)getOperandValue(I->getPtrOperand(), SF).ULongVal;
Chris Lattner86660982001-08-27 05:16:50 +0000600 GenericValue Val = getOperandValue(I->getOperand(0), SF);
601 assert(I->getNumOperands() == 2 && "NI!");
602
603 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
604 case Type::BoolTyID:
605 case Type::UByteTyID:
606 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
607 case Type::UShortTyID:
608 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
609 case Type::UIntTyID:
610 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000611 case Type::ULongTyID:
Chris Lattnerc2593162001-10-27 08:28:11 +0000612 case Type::LongTyID:
613 case Type::PointerTyID: Ptr->LongVal = Val.LongVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000614 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
615 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000616 default:
617 cout << "Cannot store value of type " << I->getType() << "!\n";
618 }
619}
620
621
622//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000623// Miscellaneous Instruction Implementations
624//===----------------------------------------------------------------------===//
625
626void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
627 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000628 vector<GenericValue> ArgVals;
629 ArgVals.reserve(I->getNumOperands()-1);
630 for (unsigned i = 1; i < I->getNumOperands(); ++i)
631 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
632
633 callMethod(I->getCalledMethod(), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000634}
635
636static void executePHINode(PHINode *I, ExecutionContext &SF) {
637 BasicBlock *PrevBB = SF.PrevBB;
638 Value *IncomingValue = 0;
639
640 // Search for the value corresponding to this previous bb...
641 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
642 if (I->getIncomingBlock(--i) == PrevBB) {
643 IncomingValue = I->getIncomingValue(i);
644 break;
645 }
646 }
647 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
648
649 // Found the value, set as the result...
650 SetValue(I, getOperandValue(IncomingValue, SF), SF);
651}
652
Chris Lattner86660982001-08-27 05:16:50 +0000653#define IMPLEMENT_SHIFT(OP, TY) \
654 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
655
656static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
657 const Type *Ty = I->getOperand(0)->getType();
658 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
659 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
660 GenericValue Dest;
661
662 switch (Ty->getPrimitiveID()) {
663 IMPLEMENT_SHIFT(<<, UByte);
664 IMPLEMENT_SHIFT(<<, SByte);
665 IMPLEMENT_SHIFT(<<, UShort);
666 IMPLEMENT_SHIFT(<<, Short);
667 IMPLEMENT_SHIFT(<<, UInt);
668 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000669 IMPLEMENT_SHIFT(<<, ULong);
670 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000671 default:
672 cout << "Unhandled type for Shl instruction: " << Ty << endl;
673 }
674 SetValue(I, Dest, SF);
675}
676
677static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
678 const Type *Ty = I->getOperand(0)->getType();
679 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
680 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
681 GenericValue Dest;
682
683 switch (Ty->getPrimitiveID()) {
684 IMPLEMENT_SHIFT(>>, UByte);
685 IMPLEMENT_SHIFT(>>, SByte);
686 IMPLEMENT_SHIFT(>>, UShort);
687 IMPLEMENT_SHIFT(>>, Short);
688 IMPLEMENT_SHIFT(>>, UInt);
689 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000690 IMPLEMENT_SHIFT(>>, ULong);
691 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000692 default:
693 cout << "Unhandled type for Shr instruction: " << Ty << endl;
694 }
695 SetValue(I, Dest, SF);
696}
697
698#define IMPLEMENT_CAST(DTY, DCTY, STY) \
699 case Type::STY##TyID: Dest.DTY##Val = (DCTY)Src.STY##Val; break;
700
701#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
702 case Type::DESTTY##TyID: \
703 switch (SrcTy->getPrimitiveID()) { \
704 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
705 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
706 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
707 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
708 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000709 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
710 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000711 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
712 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000713
714#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
715 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
716 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
717
718#define IMPLEMENT_CAST_CASE_END() \
719 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << endl; \
720 break; \
721 } \
722 break
723
724#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
725 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
726 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000727 IMPLEMENT_CAST_CASE_END()
728
729static void executeCastInst(CastInst *I, ExecutionContext &SF) {
730 const Type *Ty = I->getType();
731 const Type *SrcTy = I->getOperand(0)->getType();
732 GenericValue Src = getOperandValue(I->getOperand(0), SF);
733 GenericValue Dest;
734
735 switch (Ty->getPrimitiveID()) {
736 IMPLEMENT_CAST_CASE(UByte , unsigned char);
737 IMPLEMENT_CAST_CASE(SByte , signed char);
738 IMPLEMENT_CAST_CASE(UShort, unsigned short);
739 IMPLEMENT_CAST_CASE(Short , signed char);
740 IMPLEMENT_CAST_CASE(UInt , unsigned int );
741 IMPLEMENT_CAST_CASE(Int , signed int );
Chris Lattner7b851ab2001-10-15 19:18:26 +0000742 IMPLEMENT_CAST_CASE(ULong , uint64_t );
743 IMPLEMENT_CAST_CASE(Long , int64_t );
Chris Lattnerc2593162001-10-27 08:28:11 +0000744 IMPLEMENT_CAST_CASE(Pointer, uint64_t);
745 IMPLEMENT_CAST_CASE(Float , float);
746 IMPLEMENT_CAST_CASE(Double, double);
Chris Lattner86660982001-08-27 05:16:50 +0000747 default:
748 cout << "Unhandled dest type for cast instruction: " << Ty << endl;
749 }
750 SetValue(I, Dest, SF);
751}
Chris Lattner92101ac2001-08-23 17:05:04 +0000752
753
754
755
756//===----------------------------------------------------------------------===//
757// Dispatch and Execution Code
758//===----------------------------------------------------------------------===//
759
760MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
761 // Assign slot numbers to the method arguments...
762 const Method::ArgumentListType &ArgList = M->getArgumentList();
763 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
764 AE = ArgList.end(); AI != AE; ++AI) {
765 MethodArgument *MA = *AI;
766 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
767 }
768
769 // Iterate over all of the instructions...
770 unsigned InstNum = 0;
771 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
772 MI != ME; ++MI) {
773 Instruction *I = *MI; // For each instruction...
774 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
775 }
776}
777
778unsigned MethodInfo::getValueSlot(const Value *V) {
779 unsigned Plane = V->getType()->getUniqueID();
780 if (Plane >= NumPlaneElements.size())
781 NumPlaneElements.resize(Plane+1, 0);
782 return NumPlaneElements[Plane]++;
783}
784
785
Chris Lattner92101ac2001-08-23 17:05:04 +0000786//===----------------------------------------------------------------------===//
787// callMethod - Execute the specified method...
788//
Chris Lattner365a76e2001-09-10 04:49:44 +0000789void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
790 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
791 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
792 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +0000793 if (M->isExternal()) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000794 callExternalMethod(M, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000795 return;
796 }
797
798 // Process the method, assigning instruction numbers to the instructions in
799 // the method. Also calculate the number of values for each type slot active.
800 //
801 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +0000802 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +0000803
Chris Lattner92101ac2001-08-23 17:05:04 +0000804 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
805 StackFrame.CurMethod = M;
806 StackFrame.CurBB = M->front();
807 StackFrame.CurInst = StackFrame.CurBB->begin();
808 StackFrame.MethInfo = MethInfo;
809
810 // Initialize the values to nothing...
811 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
812 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i)
813 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
814
815 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
816
Chris Lattner92101ac2001-08-23 17:05:04 +0000817
Chris Lattner365a76e2001-09-10 04:49:44 +0000818 // Run through the method arguments and initialize their values...
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000819 assert(ArgVals.size() == M->getArgumentList().size() &&
820 "Invalid number of values passed to method invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +0000821 unsigned i = 0;
822 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
823 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
824 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +0000825 }
826}
827
828// executeInstruction - Interpret a single instruction, increment the "PC", and
829// return true if the next instruction is a breakpoint...
830//
831bool Interpreter::executeInstruction() {
832 assert(!ECStack.empty() && "No program running, cannot execute inst!");
833
834 ExecutionContext &SF = ECStack.back(); // Current stack frame
835 Instruction *I = *SF.CurInst++; // Increment before execute
836
837 if (I->isBinaryOp()) {
838 executeBinaryInst((BinaryOperator*)I, SF);
839 } else {
840 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +0000841 // Terminators
Chris Lattner92101ac2001-08-23 17:05:04 +0000842 case Instruction::Ret: executeRetInst ((ReturnInst*)I, SF); break;
843 case Instruction::Br: executeBrInst ((BranchInst*)I, SF); break;
Chris Lattner86660982001-08-27 05:16:50 +0000844 // Memory Instructions
845 case Instruction::Alloca:
846 case Instruction::Malloc: executeAllocInst ((AllocationInst*)I, SF); break;
Chris Lattnerb00c5822001-10-02 03:41:24 +0000847 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
848 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
849 case Instruction::Store: executeStoreInst (cast<StoreInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +0000850
851 // Miscellaneous Instructions
Chris Lattnerb00c5822001-10-02 03:41:24 +0000852 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
853 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
854 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
855 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
856 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000857 default:
858 cout << "Don't know how to execute this instruction!\n-->" << I;
859 }
860 }
861
862 // Reset the current frame location to the top of stack
863 CurFrame = ECStack.size()-1;
864
865 if (CurFrame == -1) return false; // No breakpoint if no code
866
867 // Return true if there is a breakpoint annotation on the instruction...
868 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
869}
870
871void Interpreter::stepInstruction() { // Do the 'step' command
872 if (ECStack.empty()) {
873 cout << "Error: no program running, cannot step!\n";
874 return;
875 }
876
877 // Run an instruction...
878 executeInstruction();
879
880 // Print the next instruction to execute...
881 printCurrentInstruction();
882}
883
884// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +0000885void Interpreter::nextInstruction() { // Do the 'next' command
886 if (ECStack.empty()) {
887 cout << "Error: no program running, cannot 'next'!\n";
888 return;
889 }
890
891 // If this is a call instruction, step over the call instruction...
892 // TODO: ICALL, CALL WITH, ...
893 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
894 // Step into the function...
895 if (executeInstruction()) {
896 // Hit a breakpoint, print current instruction, then return to user...
897 cout << "Breakpoint hit!\n";
898 printCurrentInstruction();
899 return;
900 }
901
902 // Finish executing the function...
903 finish();
904 } else {
905 // Normal instruction, just step...
906 stepInstruction();
907 }
908}
909
910void Interpreter::run() {
911 if (ECStack.empty()) {
912 cout << "Error: no program running, cannot run!\n";
913 return;
914 }
915
916 bool HitBreakpoint = false;
917 while (!ECStack.empty() && !HitBreakpoint) {
918 // Run an instruction...
919 HitBreakpoint = executeInstruction();
920 }
921
922 if (HitBreakpoint) {
923 cout << "Breakpoint hit!\n";
924 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000925 // Print the next instruction to execute...
926 printCurrentInstruction();
927}
928
929void Interpreter::finish() {
930 if (ECStack.empty()) {
931 cout << "Error: no program running, cannot run!\n";
932 return;
933 }
934
935 unsigned StackSize = ECStack.size();
936 bool HitBreakpoint = false;
937 while (ECStack.size() >= StackSize && !HitBreakpoint) {
938 // Run an instruction...
939 HitBreakpoint = executeInstruction();
940 }
941
942 if (HitBreakpoint) {
943 cout << "Breakpoint hit!\n";
944 }
945
946 // Print the next instruction to execute...
947 printCurrentInstruction();
948}
949
950
951
952// printCurrentInstruction - Print out the instruction that the virtual PC is
953// at, or fail silently if no program is running.
954//
955void Interpreter::printCurrentInstruction() {
956 if (!ECStack.empty()) {
957 Instruction *I = *ECStack.back().CurInst;
958 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
959 assert(IN && "Instruction has no numbering annotation!");
960 cout << "#" << IN->InstNum << I;
961 }
962}
963
964void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000965 switch (Ty->getPrimitiveID()) {
966 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
967 case Type::SByteTyID: cout << V.SByteVal; break;
968 case Type::UByteTyID: cout << V.UByteVal; break;
969 case Type::ShortTyID: cout << V.ShortVal; break;
970 case Type::UShortTyID: cout << V.UShortVal; break;
971 case Type::IntTyID: cout << V.IntVal; break;
972 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000973 case Type::LongTyID: cout << V.LongVal; break;
974 case Type::ULongTyID: cout << V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000975 case Type::FloatTyID: cout << V.FloatVal; break;
976 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerc2593162001-10-27 08:28:11 +0000977 case Type::PointerTyID:cout << (void*)V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000978 default:
979 cout << "- Don't know how to print value of this type!";
980 break;
981 }
982}
983
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000984void Interpreter::print(const Type *Ty, GenericValue V) {
985 cout << Ty << " ";
986 printValue(Ty, V);
987}
988
989void Interpreter::print(const string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000990 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
991 if (!PickedVal) return;
992
Chris Lattner9636a912001-10-01 16:18:37 +0000993 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000994 cout << M; // Print the method
995 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000996 print(PickedVal->getType(),
997 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner92101ac2001-08-23 17:05:04 +0000998 cout << endl;
999 }
1000
1001}
1002
Chris Lattner86660982001-08-27 05:16:50 +00001003void Interpreter::infoValue(const string &Name) {
1004 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1005 if (!PickedVal) return;
1006
1007 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001008 print(PickedVal->getType(),
1009 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner86660982001-08-27 05:16:50 +00001010 cout << endl;
1011 printOperandInfo(PickedVal, ECStack[CurFrame]);
1012}
1013
Chris Lattner92101ac2001-08-23 17:05:04 +00001014void Interpreter::list() {
1015 if (ECStack.empty())
1016 cout << "Error: No program executing!\n";
1017 else
1018 cout << ECStack[CurFrame].CurMethod; // Just print the method out...
1019}
1020
1021void Interpreter::printStackTrace() {
1022 if (ECStack.empty()) cout << "No program executing!\n";
1023
1024 for (unsigned i = 0; i < ECStack.size(); ++i) {
1025 cout << (((int)i == CurFrame) ? '>' : '-');
1026 cout << "#" << i << ". " << ECStack[i].CurMethod->getType() << " \""
1027 << ECStack[i].CurMethod->getName() << "\"(";
1028 // TODO: Print Args
1029 cout << ")" << endl;
1030 cout << *ECStack[i].CurInst;
1031 }
1032}