blob: 34a80fedd4152ad8ffd67f570592f8cd23e3af5d [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 Lattner92101ac2001-08-23 17:05:04 +000017
18static unsigned getOperandSlot(Value *V) {
19 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
20 assert(SN && "Operand does not have a slot number annotation!");
21 return SN->SlotNum;
22}
23
24#define GET_CONST_VAL(TY, CLASS) \
25 case Type::TY##TyID: Result.TY##Val = ((CLASS*)CPV)->getValue(); break
26
27static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattner1d87bcf2001-10-01 20:11:19 +000028 if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(V)) {
Chris Lattner92101ac2001-08-23 17:05:04 +000029 GenericValue Result;
30 switch (CPV->getType()->getPrimitiveID()) {
31 GET_CONST_VAL(Bool , ConstPoolBool);
32 GET_CONST_VAL(UByte , ConstPoolUInt);
33 GET_CONST_VAL(SByte , ConstPoolSInt);
34 GET_CONST_VAL(UShort , ConstPoolUInt);
35 GET_CONST_VAL(Short , ConstPoolSInt);
36 GET_CONST_VAL(UInt , ConstPoolUInt);
37 GET_CONST_VAL(Int , ConstPoolSInt);
38 GET_CONST_VAL(Float , ConstPoolFP);
39 GET_CONST_VAL(Double , ConstPoolFP);
40 default:
41 cout << "ERROR: Constant unimp for type: " << CPV->getType() << endl;
42 }
43 return Result;
44 } else {
45 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
46 return SF.Values[TyP][getOperandSlot(V)];
47 }
48}
49
Chris Lattner86660982001-08-27 05:16:50 +000050static void printOperandInfo(Value *V, ExecutionContext &SF) {
Chris Lattner1d87bcf2001-10-01 20:11:19 +000051 if (!isa<ConstPoolVal>(V)) {
Chris Lattner86660982001-08-27 05:16:50 +000052 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
53 unsigned Slot = getOperandSlot(V);
54 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
55 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF << endl;
56 }
57}
58
59
60
Chris Lattner92101ac2001-08-23 17:05:04 +000061static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
62 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattner86660982001-08-27 05:16:50 +000063
64 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << endl;
Chris Lattner92101ac2001-08-23 17:05:04 +000065 SF.Values[TyP][getOperandSlot(V)] = Val;
66}
67
68
69
70//===----------------------------------------------------------------------===//
71// Binary Instruction Implementations
72//===----------------------------------------------------------------------===//
73
74#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
75 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
Chris Lattner86660982001-08-27 05:16:50 +000076#define IMPLEMENT_BINARY_PTR_OPERATOR(OP) \
77 case Type::PointerTyID: Dest.PointerVal = \
78 (GenericValue*)((unsigned long)Src1.PointerVal OP (unsigned long)Src2.PointerVal); break
Chris Lattner92101ac2001-08-23 17:05:04 +000079
80static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
81 const Type *Ty, ExecutionContext &SF) {
82 GenericValue Dest;
83 switch (Ty->getPrimitiveID()) {
84 IMPLEMENT_BINARY_OPERATOR(+, UByte);
85 IMPLEMENT_BINARY_OPERATOR(+, SByte);
86 IMPLEMENT_BINARY_OPERATOR(+, UShort);
87 IMPLEMENT_BINARY_OPERATOR(+, Short);
88 IMPLEMENT_BINARY_OPERATOR(+, UInt);
89 IMPLEMENT_BINARY_OPERATOR(+, Int);
90 IMPLEMENT_BINARY_OPERATOR(+, Float);
91 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner86660982001-08-27 05:16:50 +000092 IMPLEMENT_BINARY_PTR_OPERATOR(+);
Chris Lattner92101ac2001-08-23 17:05:04 +000093 case Type::ULongTyID:
94 case Type::LongTyID:
95 default:
96 cout << "Unhandled type for Add instruction: " << Ty << endl;
97 }
98 return Dest;
99}
100
101static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
102 const Type *Ty, ExecutionContext &SF) {
103 GenericValue Dest;
104 switch (Ty->getPrimitiveID()) {
105 IMPLEMENT_BINARY_OPERATOR(-, UByte);
106 IMPLEMENT_BINARY_OPERATOR(-, SByte);
107 IMPLEMENT_BINARY_OPERATOR(-, UShort);
108 IMPLEMENT_BINARY_OPERATOR(-, Short);
109 IMPLEMENT_BINARY_OPERATOR(-, UInt);
110 IMPLEMENT_BINARY_OPERATOR(-, Int);
111 IMPLEMENT_BINARY_OPERATOR(-, Float);
112 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000113 IMPLEMENT_BINARY_PTR_OPERATOR(-);
Chris Lattner92101ac2001-08-23 17:05:04 +0000114 case Type::ULongTyID:
115 case Type::LongTyID:
116 default:
117 cout << "Unhandled type for Sub instruction: " << Ty << endl;
118 }
119 return Dest;
120}
121
122#define IMPLEMENT_SETCC(OP, TY) \
123 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
124
Chris Lattner92101ac2001-08-23 17:05:04 +0000125static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
126 const Type *Ty, ExecutionContext &SF) {
127 GenericValue Dest;
128 switch (Ty->getPrimitiveID()) {
129 IMPLEMENT_SETCC(==, UByte);
130 IMPLEMENT_SETCC(==, SByte);
131 IMPLEMENT_SETCC(==, UShort);
132 IMPLEMENT_SETCC(==, Short);
133 IMPLEMENT_SETCC(==, UInt);
134 IMPLEMENT_SETCC(==, Int);
135 IMPLEMENT_SETCC(==, Float);
136 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000137 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000138 case Type::ULongTyID:
139 case Type::LongTyID:
140 default:
141 cout << "Unhandled type for SetEQ instruction: " << Ty << endl;
142 }
143 return Dest;
144}
145
146static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
147 const Type *Ty, ExecutionContext &SF) {
148 GenericValue Dest;
149 switch (Ty->getPrimitiveID()) {
150 IMPLEMENT_SETCC(!=, UByte);
151 IMPLEMENT_SETCC(!=, SByte);
152 IMPLEMENT_SETCC(!=, UShort);
153 IMPLEMENT_SETCC(!=, Short);
154 IMPLEMENT_SETCC(!=, UInt);
155 IMPLEMENT_SETCC(!=, Int);
156 IMPLEMENT_SETCC(!=, Float);
157 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000158 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000159 case Type::ULongTyID:
160 case Type::LongTyID:
161 default:
162 cout << "Unhandled type for SetNE instruction: " << Ty << endl;
163 }
164 return Dest;
165}
166
167static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
168 const Type *Ty, ExecutionContext &SF) {
169 GenericValue Dest;
170 switch (Ty->getPrimitiveID()) {
171 IMPLEMENT_SETCC(<=, UByte);
172 IMPLEMENT_SETCC(<=, SByte);
173 IMPLEMENT_SETCC(<=, UShort);
174 IMPLEMENT_SETCC(<=, Short);
175 IMPLEMENT_SETCC(<=, UInt);
176 IMPLEMENT_SETCC(<=, Int);
177 IMPLEMENT_SETCC(<=, Float);
178 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000179 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000180 case Type::ULongTyID:
181 case Type::LongTyID:
182 default:
183 cout << "Unhandled type for SetLE instruction: " << Ty << endl;
184 }
185 return Dest;
186}
187
188static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
189 const Type *Ty, ExecutionContext &SF) {
190 GenericValue Dest;
191 switch (Ty->getPrimitiveID()) {
192 IMPLEMENT_SETCC(>=, UByte);
193 IMPLEMENT_SETCC(>=, SByte);
194 IMPLEMENT_SETCC(>=, UShort);
195 IMPLEMENT_SETCC(>=, Short);
196 IMPLEMENT_SETCC(>=, UInt);
197 IMPLEMENT_SETCC(>=, Int);
198 IMPLEMENT_SETCC(>=, Float);
199 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000200 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000201 case Type::ULongTyID:
202 case Type::LongTyID:
203 default:
204 cout << "Unhandled type for SetGE instruction: " << Ty << endl;
205 }
206 return Dest;
207}
208
209static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
210 const Type *Ty, ExecutionContext &SF) {
211 GenericValue Dest;
212 switch (Ty->getPrimitiveID()) {
213 IMPLEMENT_SETCC(<, UByte);
214 IMPLEMENT_SETCC(<, SByte);
215 IMPLEMENT_SETCC(<, UShort);
216 IMPLEMENT_SETCC(<, Short);
217 IMPLEMENT_SETCC(<, UInt);
218 IMPLEMENT_SETCC(<, Int);
219 IMPLEMENT_SETCC(<, Float);
220 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000221 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000222 case Type::ULongTyID:
223 case Type::LongTyID:
224 default:
225 cout << "Unhandled type for SetLT instruction: " << Ty << endl;
226 }
227 return Dest;
228}
229
230static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
231 const Type *Ty, ExecutionContext &SF) {
232 GenericValue Dest;
233 switch (Ty->getPrimitiveID()) {
234 IMPLEMENT_SETCC(>, UByte);
235 IMPLEMENT_SETCC(>, SByte);
236 IMPLEMENT_SETCC(>, UShort);
237 IMPLEMENT_SETCC(>, Short);
238 IMPLEMENT_SETCC(>, UInt);
239 IMPLEMENT_SETCC(>, Int);
240 IMPLEMENT_SETCC(>, Float);
241 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000242 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000243 case Type::ULongTyID:
244 case Type::LongTyID:
245 default:
246 cout << "Unhandled type for SetGT instruction: " << Ty << endl;
247 }
248 return Dest;
249}
250
251static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
252 const Type *Ty = I->getOperand(0)->getType();
253 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
254 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
255 GenericValue R; // Result
256
257 switch (I->getOpcode()) {
258 case Instruction::Add: R = executeAddInst(Src1, Src2, Ty, SF); break;
259 case Instruction::Sub: R = executeSubInst(Src1, Src2, Ty, SF); break;
260 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
261 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
262 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
263 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
264 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
265 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
266 default:
267 cout << "Don't know how to handle this binary operator!\n-->" << I;
268 }
269
270 SetValue(I, R, SF);
271}
272
Chris Lattner92101ac2001-08-23 17:05:04 +0000273//===----------------------------------------------------------------------===//
274// Terminator Instruction Implementations
275//===----------------------------------------------------------------------===//
276
277void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
278 const Type *RetTy = 0;
279 GenericValue Result;
280
281 // Save away the return value... (if we are not 'ret void')
282 if (I->getNumOperands()) {
283 RetTy = I->getReturnValue()->getType();
284 Result = getOperandValue(I->getReturnValue(), SF);
285 }
286
287 // Save previously executing meth
288 const Method *M = ECStack.back().CurMethod;
289
290 // Pop the current stack frame... this invalidates SF
291 ECStack.pop_back();
292
293 if (ECStack.empty()) { // Finished main. Put result into exit code...
294 if (RetTy) { // Nonvoid return type?
295 cout << "Method " << M->getType() << " \"" << M->getName()
296 << "\" returned ";
297 printValue(RetTy, Result);
298 cout << endl;
299
300 if (RetTy->isIntegral())
301 ExitCode = Result.SByteVal; // Capture the exit code of the program
302 } else {
303 ExitCode = 0;
304 }
305 return;
306 }
307
308 // If we have a previous stack frame, and we have a previous call, fill in
309 // the return value...
310 //
311 ExecutionContext &NewSF = ECStack.back();
312 if (NewSF.Caller) {
313 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
314 SetValue(NewSF.Caller, Result, NewSF);
315
316 NewSF.Caller = 0; // We returned from the call...
Chris Lattner365a76e2001-09-10 04:49:44 +0000317 } else {
318 // This must be a function that is executing because of a user 'call'
319 // instruction.
320 cout << "Method " << M->getType() << " \"" << M->getName()
321 << "\" returned ";
322 printValue(RetTy, Result);
323 cout << endl;
Chris Lattner92101ac2001-08-23 17:05:04 +0000324 }
325}
326
327void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
328 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
329 BasicBlock *Dest;
330
331 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
332 if (!I->isUnconditional()) {
333 if (getOperandValue(I->getCondition(), SF).BoolVal == 0) // If false cond...
334 Dest = I->getSuccessor(1);
335 }
336 SF.CurBB = Dest; // Update CurBB to branch destination
337 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
338}
339
340//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000341// Memory Instruction Implementations
342//===----------------------------------------------------------------------===//
343
344// Create a TargetData structure to handle memory addressing and size/alignment
345// computations
346//
347static TargetData TD("lli Interpreter");
348
349void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
350 const Type *Ty = I->getType()->getValueType(); // Type to be allocated
351 unsigned NumElements = 1;
352
353 if (I->getNumOperands()) { // Allocating a unsized array type?
Chris Lattnerb00c5822001-10-02 03:41:24 +0000354 assert(isa<ArrayType>(Ty) && cast<const ArrayType>(Ty)->isUnsized() &&
Chris Lattner86660982001-08-27 05:16:50 +0000355 "Allocation inst with size operand for !unsized array type???");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000356 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
Chris Lattner86660982001-08-27 05:16:50 +0000357
358 // Get the number of elements being allocated by the array...
359 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
360 NumElements = NumEl.UIntVal;
361 }
362
363 // Allocate enough memory to hold the type...
364 GenericValue Result;
365 Result.PointerVal = (GenericValue*)malloc(NumElements * TD.getTypeSize(Ty));
366 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
367 SetValue(I, Result, SF);
368
369 if (I->getOpcode() == Instruction::Alloca) {
370 // Keep track to free it later...
371 }
372}
373
374static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
375 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
376 GenericValue Value = getOperandValue(I->getOperand(0), SF);
377 // TODO: Check to make sure memory is allocated
378 free(Value.PointerVal); // Free memory
379}
380
381static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
382 assert(I->getNumOperands() == 1 && "NI!");
383 GenericValue *Ptr = getOperandValue(I->getPtrOperand(), SF).PointerVal;
384 GenericValue Result;
385
386 switch (I->getType()->getPrimitiveID()) {
387 case Type::BoolTyID:
388 case Type::UByteTyID:
389 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
390 case Type::UShortTyID:
391 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
392 case Type::UIntTyID:
393 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
394 //case Type::ULongTyID:
395 //case Type::LongTyID: Result.LongVal = Ptr->LongVal; break;
396 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
397 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
398 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
399 default:
400 cout << "Cannot load value of type " << I->getType() << "!\n";
401 }
402
403 SetValue(I, Result, SF);
404}
405
406static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
407 GenericValue *Ptr = getOperandValue(I->getPtrOperand(), SF).PointerVal;
408 GenericValue Val = getOperandValue(I->getOperand(0), SF);
409 assert(I->getNumOperands() == 2 && "NI!");
410
411 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
412 case Type::BoolTyID:
413 case Type::UByteTyID:
414 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
415 case Type::UShortTyID:
416 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
417 case Type::UIntTyID:
418 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
419 //case Type::ULongTyID:
420 //case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
421 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
422 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
423 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
424 default:
425 cout << "Cannot store value of type " << I->getType() << "!\n";
426 }
427}
428
429
430//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000431// Miscellaneous Instruction Implementations
432//===----------------------------------------------------------------------===//
433
434void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
435 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000436 vector<GenericValue> ArgVals;
437 ArgVals.reserve(I->getNumOperands()-1);
438 for (unsigned i = 1; i < I->getNumOperands(); ++i)
439 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
440
441 callMethod(I->getCalledMethod(), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000442}
443
444static void executePHINode(PHINode *I, ExecutionContext &SF) {
445 BasicBlock *PrevBB = SF.PrevBB;
446 Value *IncomingValue = 0;
447
448 // Search for the value corresponding to this previous bb...
449 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
450 if (I->getIncomingBlock(--i) == PrevBB) {
451 IncomingValue = I->getIncomingValue(i);
452 break;
453 }
454 }
455 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
456
457 // Found the value, set as the result...
458 SetValue(I, getOperandValue(IncomingValue, SF), SF);
459}
460
Chris Lattner86660982001-08-27 05:16:50 +0000461#define IMPLEMENT_SHIFT(OP, TY) \
462 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
463
464static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
465 const Type *Ty = I->getOperand(0)->getType();
466 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
467 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
468 GenericValue Dest;
469
470 switch (Ty->getPrimitiveID()) {
471 IMPLEMENT_SHIFT(<<, UByte);
472 IMPLEMENT_SHIFT(<<, SByte);
473 IMPLEMENT_SHIFT(<<, UShort);
474 IMPLEMENT_SHIFT(<<, Short);
475 IMPLEMENT_SHIFT(<<, UInt);
476 IMPLEMENT_SHIFT(<<, Int);
477 case Type::ULongTyID:
478 case Type::LongTyID:
479 default:
480 cout << "Unhandled type for Shl instruction: " << Ty << endl;
481 }
482 SetValue(I, Dest, SF);
483}
484
485static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
486 const Type *Ty = I->getOperand(0)->getType();
487 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
488 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
489 GenericValue Dest;
490
491 switch (Ty->getPrimitiveID()) {
492 IMPLEMENT_SHIFT(>>, UByte);
493 IMPLEMENT_SHIFT(>>, SByte);
494 IMPLEMENT_SHIFT(>>, UShort);
495 IMPLEMENT_SHIFT(>>, Short);
496 IMPLEMENT_SHIFT(>>, UInt);
497 IMPLEMENT_SHIFT(>>, Int);
498 case Type::ULongTyID:
499 case Type::LongTyID:
500 default:
501 cout << "Unhandled type for Shr instruction: " << Ty << endl;
502 }
503 SetValue(I, Dest, SF);
504}
505
506#define IMPLEMENT_CAST(DTY, DCTY, STY) \
507 case Type::STY##TyID: Dest.DTY##Val = (DCTY)Src.STY##Val; break;
508
509#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
510 case Type::DESTTY##TyID: \
511 switch (SrcTy->getPrimitiveID()) { \
512 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
513 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
514 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
515 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
516 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
517 IMPLEMENT_CAST(DESTTY, DESTCTY, Int);
518
519#define IMPLEMENT_CAST_CASE_PTR_IMP(DESTTY, DESTCTY) \
520 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer)
521
522#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
523 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
524 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
525
526#define IMPLEMENT_CAST_CASE_END() \
527 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << endl; \
528 break; \
529 } \
530 break
531
532#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
533 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
534 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
535 IMPLEMENT_CAST_CASE_PTR_IMP(DESTTY, DESTCTY); \
536 IMPLEMENT_CAST_CASE_END()
537
538#define IMPLEMENT_CAST_CASE_FP(DESTTY, DESTCTY) \
539 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
540 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
541 IMPLEMENT_CAST_CASE_END()
542
543#define IMPLEMENT_CAST_CASE_PTR(DESTTY, DESTCTY) \
544 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
545 IMPLEMENT_CAST_CASE_PTR_IMP(DESTTY, DESTCTY); \
546 IMPLEMENT_CAST_CASE_END()
547
548static void executeCastInst(CastInst *I, ExecutionContext &SF) {
549 const Type *Ty = I->getType();
550 const Type *SrcTy = I->getOperand(0)->getType();
551 GenericValue Src = getOperandValue(I->getOperand(0), SF);
552 GenericValue Dest;
553
554 switch (Ty->getPrimitiveID()) {
555 IMPLEMENT_CAST_CASE(UByte , unsigned char);
556 IMPLEMENT_CAST_CASE(SByte , signed char);
557 IMPLEMENT_CAST_CASE(UShort, unsigned short);
558 IMPLEMENT_CAST_CASE(Short , signed char);
559 IMPLEMENT_CAST_CASE(UInt , unsigned int );
560 IMPLEMENT_CAST_CASE(Int , signed int );
561 IMPLEMENT_CAST_CASE_FP(Float , float);
562 IMPLEMENT_CAST_CASE_FP(Double, double);
563 IMPLEMENT_CAST_CASE_PTR(Pointer, GenericValue *);
564 case Type::ULongTyID:
565 case Type::LongTyID:
566 default:
567 cout << "Unhandled dest type for cast instruction: " << Ty << endl;
568 }
569 SetValue(I, Dest, SF);
570}
Chris Lattner92101ac2001-08-23 17:05:04 +0000571
572
573
574
575//===----------------------------------------------------------------------===//
576// Dispatch and Execution Code
577//===----------------------------------------------------------------------===//
578
579MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
580 // Assign slot numbers to the method arguments...
581 const Method::ArgumentListType &ArgList = M->getArgumentList();
582 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
583 AE = ArgList.end(); AI != AE; ++AI) {
584 MethodArgument *MA = *AI;
585 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
586 }
587
588 // Iterate over all of the instructions...
589 unsigned InstNum = 0;
590 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
591 MI != ME; ++MI) {
592 Instruction *I = *MI; // For each instruction...
593 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
594 }
595}
596
597unsigned MethodInfo::getValueSlot(const Value *V) {
598 unsigned Plane = V->getType()->getUniqueID();
599 if (Plane >= NumPlaneElements.size())
600 NumPlaneElements.resize(Plane+1, 0);
601 return NumPlaneElements[Plane]++;
602}
603
604
605void Interpreter::initializeExecutionEngine() {
606 AnnotationManager::registerAnnotationFactory(MethodInfoAID, CreateMethodInfo);
607}
608
Chris Lattner92101ac2001-08-23 17:05:04 +0000609//===----------------------------------------------------------------------===//
610// callMethod - Execute the specified method...
611//
Chris Lattner365a76e2001-09-10 04:49:44 +0000612void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
613 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
614 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
615 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +0000616 if (M->isExternal()) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000617 callExternalMethod(M, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000618 return;
619 }
620
621 // Process the method, assigning instruction numbers to the instructions in
622 // the method. Also calculate the number of values for each type slot active.
623 //
624 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +0000625 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +0000626
Chris Lattner92101ac2001-08-23 17:05:04 +0000627 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
628 StackFrame.CurMethod = M;
629 StackFrame.CurBB = M->front();
630 StackFrame.CurInst = StackFrame.CurBB->begin();
631 StackFrame.MethInfo = MethInfo;
632
633 // Initialize the values to nothing...
634 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
635 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i)
636 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
637
638 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
639
Chris Lattner92101ac2001-08-23 17:05:04 +0000640
Chris Lattner365a76e2001-09-10 04:49:44 +0000641 // Run through the method arguments and initialize their values...
642 unsigned i = 0;
643 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
644 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
645 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +0000646 }
647}
648
649// executeInstruction - Interpret a single instruction, increment the "PC", and
650// return true if the next instruction is a breakpoint...
651//
652bool Interpreter::executeInstruction() {
653 assert(!ECStack.empty() && "No program running, cannot execute inst!");
654
655 ExecutionContext &SF = ECStack.back(); // Current stack frame
656 Instruction *I = *SF.CurInst++; // Increment before execute
657
658 if (I->isBinaryOp()) {
659 executeBinaryInst((BinaryOperator*)I, SF);
660 } else {
661 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +0000662 // Terminators
Chris Lattner92101ac2001-08-23 17:05:04 +0000663 case Instruction::Ret: executeRetInst ((ReturnInst*)I, SF); break;
664 case Instruction::Br: executeBrInst ((BranchInst*)I, SF); break;
Chris Lattner86660982001-08-27 05:16:50 +0000665 // Memory Instructions
666 case Instruction::Alloca:
667 case Instruction::Malloc: executeAllocInst ((AllocationInst*)I, SF); break;
Chris Lattnerb00c5822001-10-02 03:41:24 +0000668 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
669 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
670 case Instruction::Store: executeStoreInst (cast<StoreInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +0000671
672 // Miscellaneous Instructions
Chris Lattnerb00c5822001-10-02 03:41:24 +0000673 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
674 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
675 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
676 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
677 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000678 default:
679 cout << "Don't know how to execute this instruction!\n-->" << I;
680 }
681 }
682
683 // Reset the current frame location to the top of stack
684 CurFrame = ECStack.size()-1;
685
686 if (CurFrame == -1) return false; // No breakpoint if no code
687
688 // Return true if there is a breakpoint annotation on the instruction...
689 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
690}
691
692void Interpreter::stepInstruction() { // Do the 'step' command
693 if (ECStack.empty()) {
694 cout << "Error: no program running, cannot step!\n";
695 return;
696 }
697
698 // Run an instruction...
699 executeInstruction();
700
701 // Print the next instruction to execute...
702 printCurrentInstruction();
703}
704
705// --- UI Stuff...
706
707
708
709void Interpreter::nextInstruction() { // Do the 'next' command
710 if (ECStack.empty()) {
711 cout << "Error: no program running, cannot 'next'!\n";
712 return;
713 }
714
715 // If this is a call instruction, step over the call instruction...
716 // TODO: ICALL, CALL WITH, ...
717 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
718 // Step into the function...
719 if (executeInstruction()) {
720 // Hit a breakpoint, print current instruction, then return to user...
721 cout << "Breakpoint hit!\n";
722 printCurrentInstruction();
723 return;
724 }
725
726 // Finish executing the function...
727 finish();
728 } else {
729 // Normal instruction, just step...
730 stepInstruction();
731 }
732}
733
734void Interpreter::run() {
735 if (ECStack.empty()) {
736 cout << "Error: no program running, cannot run!\n";
737 return;
738 }
739
740 bool HitBreakpoint = false;
741 while (!ECStack.empty() && !HitBreakpoint) {
742 // Run an instruction...
743 HitBreakpoint = executeInstruction();
744 }
745
746 if (HitBreakpoint) {
747 cout << "Breakpoint hit!\n";
748 }
749
750 // Print the next instruction to execute...
751 printCurrentInstruction();
752}
753
754void Interpreter::finish() {
755 if (ECStack.empty()) {
756 cout << "Error: no program running, cannot run!\n";
757 return;
758 }
759
760 unsigned StackSize = ECStack.size();
761 bool HitBreakpoint = false;
762 while (ECStack.size() >= StackSize && !HitBreakpoint) {
763 // Run an instruction...
764 HitBreakpoint = executeInstruction();
765 }
766
767 if (HitBreakpoint) {
768 cout << "Breakpoint hit!\n";
769 }
770
771 // Print the next instruction to execute...
772 printCurrentInstruction();
773}
774
775
776
777// printCurrentInstruction - Print out the instruction that the virtual PC is
778// at, or fail silently if no program is running.
779//
780void Interpreter::printCurrentInstruction() {
781 if (!ECStack.empty()) {
782 Instruction *I = *ECStack.back().CurInst;
783 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
784 assert(IN && "Instruction has no numbering annotation!");
785 cout << "#" << IN->InstNum << I;
786 }
787}
788
789void Interpreter::printValue(const Type *Ty, GenericValue V) {
790 cout << Ty << " ";
791
792 switch (Ty->getPrimitiveID()) {
793 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
794 case Type::SByteTyID: cout << V.SByteVal; break;
795 case Type::UByteTyID: cout << V.UByteVal; break;
796 case Type::ShortTyID: cout << V.ShortVal; break;
797 case Type::UShortTyID: cout << V.UShortVal; break;
798 case Type::IntTyID: cout << V.IntVal; break;
799 case Type::UIntTyID: cout << V.UIntVal; break;
800 case Type::FloatTyID: cout << V.FloatVal; break;
801 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000802 case Type::PointerTyID:cout << V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000803 default:
804 cout << "- Don't know how to print value of this type!";
805 break;
806 }
807}
808
809void Interpreter::printValue(const string &Name) {
810 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
811 if (!PickedVal) return;
812
Chris Lattner9636a912001-10-01 16:18:37 +0000813 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000814 cout << M; // Print the method
815 } else { // Otherwise there should be an annotation for the slot#
816 printValue(PickedVal->getType(),
817 getOperandValue(PickedVal, ECStack[CurFrame]));
818 cout << endl;
819 }
820
821}
822
Chris Lattner86660982001-08-27 05:16:50 +0000823void Interpreter::infoValue(const string &Name) {
824 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
825 if (!PickedVal) return;
826
827 cout << "Value: ";
828 printValue(PickedVal->getType(),
829 getOperandValue(PickedVal, ECStack[CurFrame]));
830 cout << endl;
831 printOperandInfo(PickedVal, ECStack[CurFrame]);
832}
833
Chris Lattner92101ac2001-08-23 17:05:04 +0000834void Interpreter::list() {
835 if (ECStack.empty())
836 cout << "Error: No program executing!\n";
837 else
838 cout << ECStack[CurFrame].CurMethod; // Just print the method out...
839}
840
841void Interpreter::printStackTrace() {
842 if (ECStack.empty()) cout << "No program executing!\n";
843
844 for (unsigned i = 0; i < ECStack.size(); ++i) {
845 cout << (((int)i == CurFrame) ? '>' : '-');
846 cout << "#" << i << ". " << ECStack[i].CurMethod->getType() << " \""
847 << ECStack[i].CurMethod->getName() << "\"(";
848 // TODO: Print Args
849 cout << ")" << endl;
850 cout << *ECStack[i].CurInst;
851 }
852}