blob: 77f82e08fc708b564934c805e0db387e79f11112 [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);
48 GET_CONST_VAL(Float , ConstPoolFP);
49 GET_CONST_VAL(Double , ConstPoolFP);
50 case Type::PointerTyID:
51 if (isa<ConstPoolPointerNull>(CPV)) {
52 Result.PointerVal = 0;
53 } else if (ConstPoolPointerRef *CPR =dyn_cast<ConstPoolPointerRef>(CPV)) {
54 assert(0 && "Not implemented!");
55 } else {
56 assert(0 && "Unknown constant pointer type!");
57 }
58 break;
59 default:
60 cout << "ERROR: Constant unimp for type: " << CPV->getType() << endl;
61 }
62 return Result;
63 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
64 GlobalAddress *Address =
65 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
66 GenericValue Result;
67 Result.PointerVal = (GenericValue*)Address->Ptr;
68 return Result;
69 } else {
70 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
71 return SF.Values[TyP][getOperandSlot(V)];
72 }
73}
74
75static void printOperandInfo(Value *V, ExecutionContext &SF) {
76 if (isa<ConstPoolVal>(V)) {
77 cout << "Constant Pool Value\n";
78 } else if (isa<GlobalValue>(V)) {
79 cout << "Global Value\n";
80 } else {
81 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
82 unsigned Slot = getOperandSlot(V);
83 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
84 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF << endl;
85 }
86}
87
88
89
90static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
91 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
92
93 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << endl;
94 SF.Values[TyP][getOperandSlot(V)] = Val;
95}
96
97
98//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +000099// Annotation Wrangling code
100//===----------------------------------------------------------------------===//
101
102void Interpreter::initializeExecutionEngine() {
103 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
104 &MethodInfo::Create);
105 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
106 &GlobalAddress::Create);
107}
108
109// InitializeMemory - Recursive function to apply a ConstPool value into the
110// specified memory location...
111//
112static void InitializeMemory(ConstPoolVal *Init, char *Addr) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000113#define INITIALIZE_MEMORY(TYID, CLASS, TY) \
114 case Type::TYID##TyID: { \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000115 TY Tmp = cast<CLASS>(Init)->getValue(); \
Chris Lattner39bb5b42001-10-15 13:25:40 +0000116 memcpy(Addr, &Tmp, sizeof(TY)); \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000117 } return
118
119 switch (Init->getType()->getPrimitiveID()) {
120 INITIALIZE_MEMORY(Bool , ConstPoolBool, bool);
121 INITIALIZE_MEMORY(UByte , ConstPoolUInt, unsigned char);
122 INITIALIZE_MEMORY(SByte , ConstPoolSInt, signed char);
123 INITIALIZE_MEMORY(UShort , ConstPoolUInt, unsigned short);
124 INITIALIZE_MEMORY(Short , ConstPoolSInt, signed short);
125 INITIALIZE_MEMORY(UInt , ConstPoolUInt, unsigned int);
126 INITIALIZE_MEMORY(Int , ConstPoolSInt, signed int);
127 INITIALIZE_MEMORY(ULong , ConstPoolUInt, uint64_t);
128 INITIALIZE_MEMORY(Long , ConstPoolSInt, int64_t);
129 INITIALIZE_MEMORY(Float , ConstPoolFP , float);
130 INITIALIZE_MEMORY(Double , ConstPoolFP , double);
131#undef INITIALIZE_MEMORY
Chris Lattner39bb5b42001-10-15 13:25:40 +0000132
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000133 case Type::ArrayTyID: {
134 ConstPoolArray *CPA = cast<ConstPoolArray>(Init);
135 const vector<Use> &Val = CPA->getValues();
136 unsigned ElementSize =
137 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
138 for (unsigned i = 0; i < Val.size(); ++i)
139 InitializeMemory(cast<ConstPoolVal>(Val[i].get()), Addr+i*ElementSize);
140 return;
141 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000142
143 case Type::StructTyID: {
144 ConstPoolStruct *CPS = cast<ConstPoolStruct>(Init);
145 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
146 const vector<Use> &Val = CPS->getValues();
147 for (unsigned i = 0; i < Val.size(); ++i)
148 InitializeMemory(cast<ConstPoolVal>(Val[i].get()),
149 Addr+SL->MemberOffsets[i]);
150 return;
151 }
152
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000153 case Type::PointerTyID:
Chris Lattner39bb5b42001-10-15 13:25:40 +0000154 if (isa<ConstPoolPointerNull>(Init)) {
155 *(void**)Addr = 0;
156 } else if (ConstPoolPointerRef *CPR = dyn_cast<ConstPoolPointerRef>(Init)) {
157 GlobalAddress *Address =
158 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
159 *(void**)Addr = (GenericValue*)Address->Ptr;
160 } else {
161 assert(0 && "Unknown Constant pointer type!");
162 }
163 return;
164
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000165 default:
166 cout << "Bad Type: " << Init->getType()->getDescription() << endl;
167 assert(0 && "Unknown constant type to initialize memory with!");
168 }
169}
170
171Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
172 assert(AID == GlobalAddressAID);
173
174 // This annotation will only be created on GlobalValue objects...
175 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
176
177 if (isa<Method>(GVal)) {
178 // The GlobalAddress object for a method is just a pointer to method itself.
179 // Don't delete it when the annotation is gone though!
180 return new GlobalAddress(GVal, false);
181 }
182
183 // Handle the case of a global variable...
184 assert(isa<GlobalVariable>(GVal) &&
185 "Global value found that isn't a method or global variable!");
186 GlobalVariable *GV = cast<GlobalVariable>(GVal);
187
188 // First off, we must allocate space for the global variable to point at...
189 const Type *Ty = GV->getType()->getValueType(); // Type to be allocated
190 unsigned NumElements = 1;
191
192 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
193 assert(GV->hasInitializer() && "Const val must have an initializer!");
194 // Allocating a unsized array type?
195 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
196
197 // Get the number of elements being allocated by the array...
198 NumElements =cast<ConstPoolArray>(GV->getInitializer())->getValues().size();
199 }
200
201 // Allocate enough memory to hold the type...
202 void *Addr = malloc(NumElements * TD.getTypeSize(Ty));
203 assert(Addr != 0 && "Null pointer returned by malloc!");
204
205 // Initialize the memory if there is an initializer...
206 if (GV->hasInitializer())
207 InitializeMemory(GV->getInitializer(), (char*)Addr);
208
209 return new GlobalAddress(Addr, true); // Simply invoke the ctor
210}
211
Chris Lattner92101ac2001-08-23 17:05:04 +0000212
213//===----------------------------------------------------------------------===//
214// Binary Instruction Implementations
215//===----------------------------------------------------------------------===//
216
217#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
218 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
Chris Lattner86660982001-08-27 05:16:50 +0000219#define IMPLEMENT_BINARY_PTR_OPERATOR(OP) \
220 case Type::PointerTyID: Dest.PointerVal = \
221 (GenericValue*)((unsigned long)Src1.PointerVal OP (unsigned long)Src2.PointerVal); break
Chris Lattner92101ac2001-08-23 17:05:04 +0000222
223static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
224 const Type *Ty, ExecutionContext &SF) {
225 GenericValue Dest;
226 switch (Ty->getPrimitiveID()) {
227 IMPLEMENT_BINARY_OPERATOR(+, UByte);
228 IMPLEMENT_BINARY_OPERATOR(+, SByte);
229 IMPLEMENT_BINARY_OPERATOR(+, UShort);
230 IMPLEMENT_BINARY_OPERATOR(+, Short);
231 IMPLEMENT_BINARY_OPERATOR(+, UInt);
232 IMPLEMENT_BINARY_OPERATOR(+, Int);
233 IMPLEMENT_BINARY_OPERATOR(+, Float);
234 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000235 IMPLEMENT_BINARY_PTR_OPERATOR(+);
Chris Lattner92101ac2001-08-23 17:05:04 +0000236 case Type::ULongTyID:
237 case Type::LongTyID:
238 default:
239 cout << "Unhandled type for Add instruction: " << Ty << endl;
240 }
241 return Dest;
242}
243
244static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
245 const Type *Ty, ExecutionContext &SF) {
246 GenericValue Dest;
247 switch (Ty->getPrimitiveID()) {
248 IMPLEMENT_BINARY_OPERATOR(-, UByte);
249 IMPLEMENT_BINARY_OPERATOR(-, SByte);
250 IMPLEMENT_BINARY_OPERATOR(-, UShort);
251 IMPLEMENT_BINARY_OPERATOR(-, Short);
252 IMPLEMENT_BINARY_OPERATOR(-, UInt);
253 IMPLEMENT_BINARY_OPERATOR(-, Int);
254 IMPLEMENT_BINARY_OPERATOR(-, Float);
255 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000256 IMPLEMENT_BINARY_PTR_OPERATOR(-);
Chris Lattner92101ac2001-08-23 17:05:04 +0000257 case Type::ULongTyID:
258 case Type::LongTyID:
259 default:
260 cout << "Unhandled type for Sub instruction: " << Ty << endl;
261 }
262 return Dest;
263}
264
265#define IMPLEMENT_SETCC(OP, TY) \
266 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
267
Chris Lattner92101ac2001-08-23 17:05:04 +0000268static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
269 const Type *Ty, ExecutionContext &SF) {
270 GenericValue Dest;
271 switch (Ty->getPrimitiveID()) {
272 IMPLEMENT_SETCC(==, UByte);
273 IMPLEMENT_SETCC(==, SByte);
274 IMPLEMENT_SETCC(==, UShort);
275 IMPLEMENT_SETCC(==, Short);
276 IMPLEMENT_SETCC(==, UInt);
277 IMPLEMENT_SETCC(==, Int);
278 IMPLEMENT_SETCC(==, Float);
279 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000280 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000281 case Type::ULongTyID:
282 case Type::LongTyID:
283 default:
284 cout << "Unhandled type for SetEQ instruction: " << Ty << endl;
285 }
286 return Dest;
287}
288
289static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
290 const Type *Ty, ExecutionContext &SF) {
291 GenericValue Dest;
292 switch (Ty->getPrimitiveID()) {
293 IMPLEMENT_SETCC(!=, UByte);
294 IMPLEMENT_SETCC(!=, SByte);
295 IMPLEMENT_SETCC(!=, UShort);
296 IMPLEMENT_SETCC(!=, Short);
297 IMPLEMENT_SETCC(!=, UInt);
298 IMPLEMENT_SETCC(!=, Int);
299 IMPLEMENT_SETCC(!=, Float);
300 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000301 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000302 case Type::ULongTyID:
303 case Type::LongTyID:
304 default:
305 cout << "Unhandled type for SetNE instruction: " << Ty << endl;
306 }
307 return Dest;
308}
309
310static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
311 const Type *Ty, ExecutionContext &SF) {
312 GenericValue Dest;
313 switch (Ty->getPrimitiveID()) {
314 IMPLEMENT_SETCC(<=, UByte);
315 IMPLEMENT_SETCC(<=, SByte);
316 IMPLEMENT_SETCC(<=, UShort);
317 IMPLEMENT_SETCC(<=, Short);
318 IMPLEMENT_SETCC(<=, UInt);
319 IMPLEMENT_SETCC(<=, Int);
320 IMPLEMENT_SETCC(<=, Float);
321 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000322 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000323 case Type::ULongTyID:
324 case Type::LongTyID:
325 default:
326 cout << "Unhandled type for SetLE instruction: " << Ty << endl;
327 }
328 return Dest;
329}
330
331static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
332 const Type *Ty, ExecutionContext &SF) {
333 GenericValue Dest;
334 switch (Ty->getPrimitiveID()) {
335 IMPLEMENT_SETCC(>=, UByte);
336 IMPLEMENT_SETCC(>=, SByte);
337 IMPLEMENT_SETCC(>=, UShort);
338 IMPLEMENT_SETCC(>=, Short);
339 IMPLEMENT_SETCC(>=, UInt);
340 IMPLEMENT_SETCC(>=, Int);
341 IMPLEMENT_SETCC(>=, Float);
342 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000343 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000344 case Type::ULongTyID:
345 case Type::LongTyID:
346 default:
347 cout << "Unhandled type for SetGE instruction: " << Ty << endl;
348 }
349 return Dest;
350}
351
352static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
353 const Type *Ty, ExecutionContext &SF) {
354 GenericValue Dest;
355 switch (Ty->getPrimitiveID()) {
356 IMPLEMENT_SETCC(<, UByte);
357 IMPLEMENT_SETCC(<, SByte);
358 IMPLEMENT_SETCC(<, UShort);
359 IMPLEMENT_SETCC(<, Short);
360 IMPLEMENT_SETCC(<, UInt);
361 IMPLEMENT_SETCC(<, Int);
362 IMPLEMENT_SETCC(<, Float);
363 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000364 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000365 case Type::ULongTyID:
366 case Type::LongTyID:
367 default:
368 cout << "Unhandled type for SetLT instruction: " << Ty << endl;
369 }
370 return Dest;
371}
372
373static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
374 const Type *Ty, ExecutionContext &SF) {
375 GenericValue Dest;
376 switch (Ty->getPrimitiveID()) {
377 IMPLEMENT_SETCC(>, UByte);
378 IMPLEMENT_SETCC(>, SByte);
379 IMPLEMENT_SETCC(>, UShort);
380 IMPLEMENT_SETCC(>, Short);
381 IMPLEMENT_SETCC(>, UInt);
382 IMPLEMENT_SETCC(>, Int);
383 IMPLEMENT_SETCC(>, Float);
384 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000385 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000386 case Type::ULongTyID:
387 case Type::LongTyID:
388 default:
389 cout << "Unhandled type for SetGT instruction: " << Ty << endl;
390 }
391 return Dest;
392}
393
394static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
395 const Type *Ty = I->getOperand(0)->getType();
396 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
397 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
398 GenericValue R; // Result
399
400 switch (I->getOpcode()) {
401 case Instruction::Add: R = executeAddInst(Src1, Src2, Ty, SF); break;
402 case Instruction::Sub: R = executeSubInst(Src1, Src2, Ty, SF); break;
403 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
404 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
405 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
406 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
407 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
408 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
409 default:
410 cout << "Don't know how to handle this binary operator!\n-->" << I;
411 }
412
413 SetValue(I, R, SF);
414}
415
Chris Lattner92101ac2001-08-23 17:05:04 +0000416//===----------------------------------------------------------------------===//
417// Terminator Instruction Implementations
418//===----------------------------------------------------------------------===//
419
420void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
421 const Type *RetTy = 0;
422 GenericValue Result;
423
424 // Save away the return value... (if we are not 'ret void')
425 if (I->getNumOperands()) {
426 RetTy = I->getReturnValue()->getType();
427 Result = getOperandValue(I->getReturnValue(), SF);
428 }
429
430 // Save previously executing meth
431 const Method *M = ECStack.back().CurMethod;
432
433 // Pop the current stack frame... this invalidates SF
434 ECStack.pop_back();
435
436 if (ECStack.empty()) { // Finished main. Put result into exit code...
437 if (RetTy) { // Nonvoid return type?
438 cout << "Method " << M->getType() << " \"" << M->getName()
439 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000440 print(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000441 cout << endl;
442
443 if (RetTy->isIntegral())
444 ExitCode = Result.SByteVal; // Capture the exit code of the program
445 } else {
446 ExitCode = 0;
447 }
448 return;
449 }
450
451 // If we have a previous stack frame, and we have a previous call, fill in
452 // the return value...
453 //
454 ExecutionContext &NewSF = ECStack.back();
455 if (NewSF.Caller) {
456 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
457 SetValue(NewSF.Caller, Result, NewSF);
458
459 NewSF.Caller = 0; // We returned from the call...
Chris Lattner365a76e2001-09-10 04:49:44 +0000460 } else {
461 // This must be a function that is executing because of a user 'call'
462 // instruction.
463 cout << "Method " << M->getType() << " \"" << M->getName()
464 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000465 print(RetTy, Result);
Chris Lattner365a76e2001-09-10 04:49:44 +0000466 cout << endl;
Chris Lattner92101ac2001-08-23 17:05:04 +0000467 }
468}
469
470void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
471 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
472 BasicBlock *Dest;
473
474 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
475 if (!I->isUnconditional()) {
476 if (getOperandValue(I->getCondition(), SF).BoolVal == 0) // If false cond...
477 Dest = I->getSuccessor(1);
478 }
479 SF.CurBB = Dest; // Update CurBB to branch destination
480 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
481}
482
483//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000484// Memory Instruction Implementations
485//===----------------------------------------------------------------------===//
486
Chris Lattner86660982001-08-27 05:16:50 +0000487void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
488 const Type *Ty = I->getType()->getValueType(); // Type to be allocated
489 unsigned NumElements = 1;
490
491 if (I->getNumOperands()) { // Allocating a unsized array type?
Chris Lattnerb00c5822001-10-02 03:41:24 +0000492 assert(isa<ArrayType>(Ty) && cast<const ArrayType>(Ty)->isUnsized() &&
Chris Lattner86660982001-08-27 05:16:50 +0000493 "Allocation inst with size operand for !unsized array type???");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000494 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
Chris Lattner86660982001-08-27 05:16:50 +0000495
496 // Get the number of elements being allocated by the array...
497 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
498 NumElements = NumEl.UIntVal;
499 }
500
501 // Allocate enough memory to hold the type...
502 GenericValue Result;
503 Result.PointerVal = (GenericValue*)malloc(NumElements * TD.getTypeSize(Ty));
504 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
505 SetValue(I, Result, SF);
506
507 if (I->getOpcode() == Instruction::Alloca) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000508 // TODO: FIXME: alloca should keep track of memory to free it later...
Chris Lattner86660982001-08-27 05:16:50 +0000509 }
510}
511
512static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
513 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
514 GenericValue Value = getOperandValue(I->getOperand(0), SF);
515 // TODO: Check to make sure memory is allocated
516 free(Value.PointerVal); // Free memory
517}
518
519static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
520 assert(I->getNumOperands() == 1 && "NI!");
521 GenericValue *Ptr = getOperandValue(I->getPtrOperand(), SF).PointerVal;
522 GenericValue Result;
523
524 switch (I->getType()->getPrimitiveID()) {
525 case Type::BoolTyID:
526 case Type::UByteTyID:
527 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
528 case Type::UShortTyID:
529 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
530 case Type::UIntTyID:
531 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
532 //case Type::ULongTyID:
533 //case Type::LongTyID: Result.LongVal = Ptr->LongVal; break;
534 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
535 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
536 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
537 default:
538 cout << "Cannot load value of type " << I->getType() << "!\n";
539 }
540
541 SetValue(I, Result, SF);
542}
543
544static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
545 GenericValue *Ptr = getOperandValue(I->getPtrOperand(), SF).PointerVal;
546 GenericValue Val = getOperandValue(I->getOperand(0), SF);
547 assert(I->getNumOperands() == 2 && "NI!");
548
549 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
550 case Type::BoolTyID:
551 case Type::UByteTyID:
552 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
553 case Type::UShortTyID:
554 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
555 case Type::UIntTyID:
556 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
557 //case Type::ULongTyID:
558 //case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
559 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
560 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
561 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
562 default:
563 cout << "Cannot store value of type " << I->getType() << "!\n";
564 }
565}
566
567
568//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000569// Miscellaneous Instruction Implementations
570//===----------------------------------------------------------------------===//
571
572void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
573 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000574 vector<GenericValue> ArgVals;
575 ArgVals.reserve(I->getNumOperands()-1);
576 for (unsigned i = 1; i < I->getNumOperands(); ++i)
577 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
578
579 callMethod(I->getCalledMethod(), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000580}
581
582static void executePHINode(PHINode *I, ExecutionContext &SF) {
583 BasicBlock *PrevBB = SF.PrevBB;
584 Value *IncomingValue = 0;
585
586 // Search for the value corresponding to this previous bb...
587 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
588 if (I->getIncomingBlock(--i) == PrevBB) {
589 IncomingValue = I->getIncomingValue(i);
590 break;
591 }
592 }
593 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
594
595 // Found the value, set as the result...
596 SetValue(I, getOperandValue(IncomingValue, SF), SF);
597}
598
Chris Lattner86660982001-08-27 05:16:50 +0000599#define IMPLEMENT_SHIFT(OP, TY) \
600 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
601
602static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
603 const Type *Ty = I->getOperand(0)->getType();
604 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
605 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
606 GenericValue Dest;
607
608 switch (Ty->getPrimitiveID()) {
609 IMPLEMENT_SHIFT(<<, UByte);
610 IMPLEMENT_SHIFT(<<, SByte);
611 IMPLEMENT_SHIFT(<<, UShort);
612 IMPLEMENT_SHIFT(<<, Short);
613 IMPLEMENT_SHIFT(<<, UInt);
614 IMPLEMENT_SHIFT(<<, Int);
615 case Type::ULongTyID:
616 case Type::LongTyID:
617 default:
618 cout << "Unhandled type for Shl instruction: " << Ty << endl;
619 }
620 SetValue(I, Dest, SF);
621}
622
623static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
624 const Type *Ty = I->getOperand(0)->getType();
625 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
626 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
627 GenericValue Dest;
628
629 switch (Ty->getPrimitiveID()) {
630 IMPLEMENT_SHIFT(>>, UByte);
631 IMPLEMENT_SHIFT(>>, SByte);
632 IMPLEMENT_SHIFT(>>, UShort);
633 IMPLEMENT_SHIFT(>>, Short);
634 IMPLEMENT_SHIFT(>>, UInt);
635 IMPLEMENT_SHIFT(>>, Int);
636 case Type::ULongTyID:
637 case Type::LongTyID:
638 default:
639 cout << "Unhandled type for Shr instruction: " << Ty << endl;
640 }
641 SetValue(I, Dest, SF);
642}
643
644#define IMPLEMENT_CAST(DTY, DCTY, STY) \
645 case Type::STY##TyID: Dest.DTY##Val = (DCTY)Src.STY##Val; break;
646
647#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
648 case Type::DESTTY##TyID: \
649 switch (SrcTy->getPrimitiveID()) { \
650 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
651 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
652 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
653 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
654 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
655 IMPLEMENT_CAST(DESTTY, DESTCTY, Int);
656
657#define IMPLEMENT_CAST_CASE_PTR_IMP(DESTTY, DESTCTY) \
658 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer)
659
660#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
661 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
662 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
663
664#define IMPLEMENT_CAST_CASE_END() \
665 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << endl; \
666 break; \
667 } \
668 break
669
670#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
671 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
672 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
673 IMPLEMENT_CAST_CASE_PTR_IMP(DESTTY, DESTCTY); \
674 IMPLEMENT_CAST_CASE_END()
675
676#define IMPLEMENT_CAST_CASE_FP(DESTTY, DESTCTY) \
677 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
678 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
679 IMPLEMENT_CAST_CASE_END()
680
681#define IMPLEMENT_CAST_CASE_PTR(DESTTY, DESTCTY) \
682 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
683 IMPLEMENT_CAST_CASE_PTR_IMP(DESTTY, DESTCTY); \
684 IMPLEMENT_CAST_CASE_END()
685
686static void executeCastInst(CastInst *I, ExecutionContext &SF) {
687 const Type *Ty = I->getType();
688 const Type *SrcTy = I->getOperand(0)->getType();
689 GenericValue Src = getOperandValue(I->getOperand(0), SF);
690 GenericValue Dest;
691
692 switch (Ty->getPrimitiveID()) {
693 IMPLEMENT_CAST_CASE(UByte , unsigned char);
694 IMPLEMENT_CAST_CASE(SByte , signed char);
695 IMPLEMENT_CAST_CASE(UShort, unsigned short);
696 IMPLEMENT_CAST_CASE(Short , signed char);
697 IMPLEMENT_CAST_CASE(UInt , unsigned int );
698 IMPLEMENT_CAST_CASE(Int , signed int );
699 IMPLEMENT_CAST_CASE_FP(Float , float);
700 IMPLEMENT_CAST_CASE_FP(Double, double);
701 IMPLEMENT_CAST_CASE_PTR(Pointer, GenericValue *);
702 case Type::ULongTyID:
703 case Type::LongTyID:
704 default:
705 cout << "Unhandled dest type for cast instruction: " << Ty << endl;
706 }
707 SetValue(I, Dest, SF);
708}
Chris Lattner92101ac2001-08-23 17:05:04 +0000709
710
711
712
713//===----------------------------------------------------------------------===//
714// Dispatch and Execution Code
715//===----------------------------------------------------------------------===//
716
717MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
718 // Assign slot numbers to the method arguments...
719 const Method::ArgumentListType &ArgList = M->getArgumentList();
720 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
721 AE = ArgList.end(); AI != AE; ++AI) {
722 MethodArgument *MA = *AI;
723 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
724 }
725
726 // Iterate over all of the instructions...
727 unsigned InstNum = 0;
728 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
729 MI != ME; ++MI) {
730 Instruction *I = *MI; // For each instruction...
731 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
732 }
733}
734
735unsigned MethodInfo::getValueSlot(const Value *V) {
736 unsigned Plane = V->getType()->getUniqueID();
737 if (Plane >= NumPlaneElements.size())
738 NumPlaneElements.resize(Plane+1, 0);
739 return NumPlaneElements[Plane]++;
740}
741
742
Chris Lattner92101ac2001-08-23 17:05:04 +0000743//===----------------------------------------------------------------------===//
744// callMethod - Execute the specified method...
745//
Chris Lattner365a76e2001-09-10 04:49:44 +0000746void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
747 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
748 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
749 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +0000750 if (M->isExternal()) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000751 callExternalMethod(M, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000752 return;
753 }
754
755 // Process the method, assigning instruction numbers to the instructions in
756 // the method. Also calculate the number of values for each type slot active.
757 //
758 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +0000759 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +0000760
Chris Lattner92101ac2001-08-23 17:05:04 +0000761 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
762 StackFrame.CurMethod = M;
763 StackFrame.CurBB = M->front();
764 StackFrame.CurInst = StackFrame.CurBB->begin();
765 StackFrame.MethInfo = MethInfo;
766
767 // Initialize the values to nothing...
768 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
769 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i)
770 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
771
772 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
773
Chris Lattner92101ac2001-08-23 17:05:04 +0000774
Chris Lattner365a76e2001-09-10 04:49:44 +0000775 // Run through the method arguments and initialize their values...
776 unsigned i = 0;
777 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
778 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
779 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +0000780 }
781}
782
783// executeInstruction - Interpret a single instruction, increment the "PC", and
784// return true if the next instruction is a breakpoint...
785//
786bool Interpreter::executeInstruction() {
787 assert(!ECStack.empty() && "No program running, cannot execute inst!");
788
789 ExecutionContext &SF = ECStack.back(); // Current stack frame
790 Instruction *I = *SF.CurInst++; // Increment before execute
791
792 if (I->isBinaryOp()) {
793 executeBinaryInst((BinaryOperator*)I, SF);
794 } else {
795 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +0000796 // Terminators
Chris Lattner92101ac2001-08-23 17:05:04 +0000797 case Instruction::Ret: executeRetInst ((ReturnInst*)I, SF); break;
798 case Instruction::Br: executeBrInst ((BranchInst*)I, SF); break;
Chris Lattner86660982001-08-27 05:16:50 +0000799 // Memory Instructions
800 case Instruction::Alloca:
801 case Instruction::Malloc: executeAllocInst ((AllocationInst*)I, SF); break;
Chris Lattnerb00c5822001-10-02 03:41:24 +0000802 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
803 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
804 case Instruction::Store: executeStoreInst (cast<StoreInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +0000805
806 // Miscellaneous Instructions
Chris Lattnerb00c5822001-10-02 03:41:24 +0000807 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
808 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
809 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
810 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
811 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000812 default:
813 cout << "Don't know how to execute this instruction!\n-->" << I;
814 }
815 }
816
817 // Reset the current frame location to the top of stack
818 CurFrame = ECStack.size()-1;
819
820 if (CurFrame == -1) return false; // No breakpoint if no code
821
822 // Return true if there is a breakpoint annotation on the instruction...
823 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
824}
825
826void Interpreter::stepInstruction() { // Do the 'step' command
827 if (ECStack.empty()) {
828 cout << "Error: no program running, cannot step!\n";
829 return;
830 }
831
832 // Run an instruction...
833 executeInstruction();
834
835 // Print the next instruction to execute...
836 printCurrentInstruction();
837}
838
839// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +0000840void Interpreter::nextInstruction() { // Do the 'next' command
841 if (ECStack.empty()) {
842 cout << "Error: no program running, cannot 'next'!\n";
843 return;
844 }
845
846 // If this is a call instruction, step over the call instruction...
847 // TODO: ICALL, CALL WITH, ...
848 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
849 // Step into the function...
850 if (executeInstruction()) {
851 // Hit a breakpoint, print current instruction, then return to user...
852 cout << "Breakpoint hit!\n";
853 printCurrentInstruction();
854 return;
855 }
856
857 // Finish executing the function...
858 finish();
859 } else {
860 // Normal instruction, just step...
861 stepInstruction();
862 }
863}
864
865void Interpreter::run() {
866 if (ECStack.empty()) {
867 cout << "Error: no program running, cannot run!\n";
868 return;
869 }
870
871 bool HitBreakpoint = false;
872 while (!ECStack.empty() && !HitBreakpoint) {
873 // Run an instruction...
874 HitBreakpoint = executeInstruction();
875 }
876
877 if (HitBreakpoint) {
878 cout << "Breakpoint hit!\n";
879 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000880 // Print the next instruction to execute...
881 printCurrentInstruction();
882}
883
884void Interpreter::finish() {
885 if (ECStack.empty()) {
886 cout << "Error: no program running, cannot run!\n";
887 return;
888 }
889
890 unsigned StackSize = ECStack.size();
891 bool HitBreakpoint = false;
892 while (ECStack.size() >= StackSize && !HitBreakpoint) {
893 // Run an instruction...
894 HitBreakpoint = executeInstruction();
895 }
896
897 if (HitBreakpoint) {
898 cout << "Breakpoint hit!\n";
899 }
900
901 // Print the next instruction to execute...
902 printCurrentInstruction();
903}
904
905
906
907// printCurrentInstruction - Print out the instruction that the virtual PC is
908// at, or fail silently if no program is running.
909//
910void Interpreter::printCurrentInstruction() {
911 if (!ECStack.empty()) {
912 Instruction *I = *ECStack.back().CurInst;
913 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
914 assert(IN && "Instruction has no numbering annotation!");
915 cout << "#" << IN->InstNum << I;
916 }
917}
918
919void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000920 switch (Ty->getPrimitiveID()) {
921 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
922 case Type::SByteTyID: cout << V.SByteVal; break;
923 case Type::UByteTyID: cout << V.UByteVal; break;
924 case Type::ShortTyID: cout << V.ShortVal; break;
925 case Type::UShortTyID: cout << V.UShortVal; break;
926 case Type::IntTyID: cout << V.IntVal; break;
927 case Type::UIntTyID: cout << V.UIntVal; break;
928 case Type::FloatTyID: cout << V.FloatVal; break;
929 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000930 case Type::PointerTyID:cout << V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000931 default:
932 cout << "- Don't know how to print value of this type!";
933 break;
934 }
935}
936
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000937void Interpreter::print(const Type *Ty, GenericValue V) {
938 cout << Ty << " ";
939 printValue(Ty, V);
940}
941
942void Interpreter::print(const string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000943 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
944 if (!PickedVal) return;
945
Chris Lattner9636a912001-10-01 16:18:37 +0000946 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000947 cout << M; // Print the method
948 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000949 print(PickedVal->getType(),
950 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner92101ac2001-08-23 17:05:04 +0000951 cout << endl;
952 }
953
954}
955
Chris Lattner86660982001-08-27 05:16:50 +0000956void Interpreter::infoValue(const string &Name) {
957 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
958 if (!PickedVal) return;
959
960 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000961 print(PickedVal->getType(),
962 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner86660982001-08-27 05:16:50 +0000963 cout << endl;
964 printOperandInfo(PickedVal, ECStack[CurFrame]);
965}
966
Chris Lattner92101ac2001-08-23 17:05:04 +0000967void Interpreter::list() {
968 if (ECStack.empty())
969 cout << "Error: No program executing!\n";
970 else
971 cout << ECStack[CurFrame].CurMethod; // Just print the method out...
972}
973
974void Interpreter::printStackTrace() {
975 if (ECStack.empty()) cout << "No program executing!\n";
976
977 for (unsigned i = 0; i < ECStack.size(); ++i) {
978 cout << (((int)i == CurFrame) ? '>' : '-');
979 cout << "#" << i << ". " << ECStack[i].CurMethod->getType() << " \""
980 << ECStack[i].CurMethod->getName() << "\"(";
981 // TODO: Print Args
982 cout << ")" << endl;
983 cout << *ECStack[i].CurInst;
984 }
985}