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