1 //===-- Execution.cpp - Implement code to simulate the program ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the actual instruction interpreter.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "interpreter"
16 #include "Interpreter.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Constants.h"
20 #include "llvm/Support/GetElementPtrTypeIterator.h"
21 #include "Support/Statistic.h"
22 #include "Support/Debug.h"
23 #include <cmath> // For fmod
27 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
31 Interpreter *TheEE = 0;
34 //===----------------------------------------------------------------------===//
35 // Value Manipulation code
36 //===----------------------------------------------------------------------===//
37 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
39 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
41 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
43 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
45 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
47 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
49 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
51 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
53 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
55 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
57 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
59 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
61 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
63 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
65 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
67 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
70 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
71 ExecutionContext &SF) {
72 switch (CE->getOpcode()) {
73 case Instruction::Cast:
74 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
75 case Instruction::GetElementPtr:
76 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
77 gep_type_end(CE), SF);
78 case Instruction::Add:
79 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
80 getOperandValue(CE->getOperand(1), SF),
81 CE->getOperand(0)->getType());
82 case Instruction::Sub:
83 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
84 getOperandValue(CE->getOperand(1), SF),
85 CE->getOperand(0)->getType());
86 case Instruction::Mul:
87 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
88 getOperandValue(CE->getOperand(1), SF),
89 CE->getOperand(0)->getType());
90 case Instruction::Div:
91 return executeDivInst(getOperandValue(CE->getOperand(0), SF),
92 getOperandValue(CE->getOperand(1), SF),
93 CE->getOperand(0)->getType());
94 case Instruction::Rem:
95 return executeRemInst(getOperandValue(CE->getOperand(0), SF),
96 getOperandValue(CE->getOperand(1), SF),
97 CE->getOperand(0)->getType());
98 case Instruction::And:
99 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
100 getOperandValue(CE->getOperand(1), SF),
101 CE->getOperand(0)->getType());
102 case Instruction::Or:
103 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
104 getOperandValue(CE->getOperand(1), SF),
105 CE->getOperand(0)->getType());
106 case Instruction::Xor:
107 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
108 getOperandValue(CE->getOperand(1), SF),
109 CE->getOperand(0)->getType());
110 case Instruction::SetEQ:
111 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
112 getOperandValue(CE->getOperand(1), SF),
113 CE->getOperand(0)->getType());
114 case Instruction::SetNE:
115 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
116 getOperandValue(CE->getOperand(1), SF),
117 CE->getOperand(0)->getType());
118 case Instruction::SetLE:
119 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
120 getOperandValue(CE->getOperand(1), SF),
121 CE->getOperand(0)->getType());
122 case Instruction::SetGE:
123 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
124 getOperandValue(CE->getOperand(1), SF),
125 CE->getOperand(0)->getType());
126 case Instruction::SetLT:
127 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
128 getOperandValue(CE->getOperand(1), SF),
129 CE->getOperand(0)->getType());
130 case Instruction::SetGT:
131 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
132 getOperandValue(CE->getOperand(1), SF),
133 CE->getOperand(0)->getType());
134 case Instruction::Shl:
135 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
136 getOperandValue(CE->getOperand(1), SF),
137 CE->getOperand(0)->getType());
138 case Instruction::Shr:
139 return executeShrInst(getOperandValue(CE->getOperand(0), SF),
140 getOperandValue(CE->getOperand(1), SF),
141 CE->getOperand(0)->getType());
144 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
146 return GenericValue();
150 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
151 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
152 return getConstantExprValue(CE, SF);
153 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
154 return getConstantValue(CPV);
155 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
156 return PTOGV(getPointerToGlobal(GV));
162 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
166 void Interpreter::initializeExecutionEngine() {
170 //===----------------------------------------------------------------------===//
171 // Binary Instruction Implementations
172 //===----------------------------------------------------------------------===//
174 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
175 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
177 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
180 switch (Ty->getPrimitiveID()) {
181 IMPLEMENT_BINARY_OPERATOR(+, UByte);
182 IMPLEMENT_BINARY_OPERATOR(+, SByte);
183 IMPLEMENT_BINARY_OPERATOR(+, UShort);
184 IMPLEMENT_BINARY_OPERATOR(+, Short);
185 IMPLEMENT_BINARY_OPERATOR(+, UInt);
186 IMPLEMENT_BINARY_OPERATOR(+, Int);
187 IMPLEMENT_BINARY_OPERATOR(+, ULong);
188 IMPLEMENT_BINARY_OPERATOR(+, Long);
189 IMPLEMENT_BINARY_OPERATOR(+, Float);
190 IMPLEMENT_BINARY_OPERATOR(+, Double);
192 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
198 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
201 switch (Ty->getPrimitiveID()) {
202 IMPLEMENT_BINARY_OPERATOR(-, UByte);
203 IMPLEMENT_BINARY_OPERATOR(-, SByte);
204 IMPLEMENT_BINARY_OPERATOR(-, UShort);
205 IMPLEMENT_BINARY_OPERATOR(-, Short);
206 IMPLEMENT_BINARY_OPERATOR(-, UInt);
207 IMPLEMENT_BINARY_OPERATOR(-, Int);
208 IMPLEMENT_BINARY_OPERATOR(-, ULong);
209 IMPLEMENT_BINARY_OPERATOR(-, Long);
210 IMPLEMENT_BINARY_OPERATOR(-, Float);
211 IMPLEMENT_BINARY_OPERATOR(-, Double);
213 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
219 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
222 switch (Ty->getPrimitiveID()) {
223 IMPLEMENT_BINARY_OPERATOR(*, UByte);
224 IMPLEMENT_BINARY_OPERATOR(*, SByte);
225 IMPLEMENT_BINARY_OPERATOR(*, UShort);
226 IMPLEMENT_BINARY_OPERATOR(*, Short);
227 IMPLEMENT_BINARY_OPERATOR(*, UInt);
228 IMPLEMENT_BINARY_OPERATOR(*, Int);
229 IMPLEMENT_BINARY_OPERATOR(*, ULong);
230 IMPLEMENT_BINARY_OPERATOR(*, Long);
231 IMPLEMENT_BINARY_OPERATOR(*, Float);
232 IMPLEMENT_BINARY_OPERATOR(*, Double);
234 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
240 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
243 switch (Ty->getPrimitiveID()) {
244 IMPLEMENT_BINARY_OPERATOR(/, UByte);
245 IMPLEMENT_BINARY_OPERATOR(/, SByte);
246 IMPLEMENT_BINARY_OPERATOR(/, UShort);
247 IMPLEMENT_BINARY_OPERATOR(/, Short);
248 IMPLEMENT_BINARY_OPERATOR(/, UInt);
249 IMPLEMENT_BINARY_OPERATOR(/, Int);
250 IMPLEMENT_BINARY_OPERATOR(/, ULong);
251 IMPLEMENT_BINARY_OPERATOR(/, Long);
252 IMPLEMENT_BINARY_OPERATOR(/, Float);
253 IMPLEMENT_BINARY_OPERATOR(/, Double);
255 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
261 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
264 switch (Ty->getPrimitiveID()) {
265 IMPLEMENT_BINARY_OPERATOR(%, UByte);
266 IMPLEMENT_BINARY_OPERATOR(%, SByte);
267 IMPLEMENT_BINARY_OPERATOR(%, UShort);
268 IMPLEMENT_BINARY_OPERATOR(%, Short);
269 IMPLEMENT_BINARY_OPERATOR(%, UInt);
270 IMPLEMENT_BINARY_OPERATOR(%, Int);
271 IMPLEMENT_BINARY_OPERATOR(%, ULong);
272 IMPLEMENT_BINARY_OPERATOR(%, Long);
273 case Type::FloatTyID:
274 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
276 case Type::DoubleTyID:
277 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
280 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
286 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
289 switch (Ty->getPrimitiveID()) {
290 IMPLEMENT_BINARY_OPERATOR(&, Bool);
291 IMPLEMENT_BINARY_OPERATOR(&, UByte);
292 IMPLEMENT_BINARY_OPERATOR(&, SByte);
293 IMPLEMENT_BINARY_OPERATOR(&, UShort);
294 IMPLEMENT_BINARY_OPERATOR(&, Short);
295 IMPLEMENT_BINARY_OPERATOR(&, UInt);
296 IMPLEMENT_BINARY_OPERATOR(&, Int);
297 IMPLEMENT_BINARY_OPERATOR(&, ULong);
298 IMPLEMENT_BINARY_OPERATOR(&, Long);
300 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
306 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
309 switch (Ty->getPrimitiveID()) {
310 IMPLEMENT_BINARY_OPERATOR(|, Bool);
311 IMPLEMENT_BINARY_OPERATOR(|, UByte);
312 IMPLEMENT_BINARY_OPERATOR(|, SByte);
313 IMPLEMENT_BINARY_OPERATOR(|, UShort);
314 IMPLEMENT_BINARY_OPERATOR(|, Short);
315 IMPLEMENT_BINARY_OPERATOR(|, UInt);
316 IMPLEMENT_BINARY_OPERATOR(|, Int);
317 IMPLEMENT_BINARY_OPERATOR(|, ULong);
318 IMPLEMENT_BINARY_OPERATOR(|, Long);
320 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
326 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
329 switch (Ty->getPrimitiveID()) {
330 IMPLEMENT_BINARY_OPERATOR(^, Bool);
331 IMPLEMENT_BINARY_OPERATOR(^, UByte);
332 IMPLEMENT_BINARY_OPERATOR(^, SByte);
333 IMPLEMENT_BINARY_OPERATOR(^, UShort);
334 IMPLEMENT_BINARY_OPERATOR(^, Short);
335 IMPLEMENT_BINARY_OPERATOR(^, UInt);
336 IMPLEMENT_BINARY_OPERATOR(^, Int);
337 IMPLEMENT_BINARY_OPERATOR(^, ULong);
338 IMPLEMENT_BINARY_OPERATOR(^, Long);
340 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
346 #define IMPLEMENT_SETCC(OP, TY) \
347 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
349 // Handle pointers specially because they must be compared with only as much
350 // width as the host has. We _do not_ want to be comparing 64 bit values when
351 // running on a 32-bit target, otherwise the upper 32 bits might mess up
352 // comparisons if they contain garbage.
353 #define IMPLEMENT_POINTERSETCC(OP) \
354 case Type::PointerTyID: \
355 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
356 (void*)(intptr_t)Src2.PointerVal; break
358 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
361 switch (Ty->getPrimitiveID()) {
362 IMPLEMENT_SETCC(==, UByte);
363 IMPLEMENT_SETCC(==, SByte);
364 IMPLEMENT_SETCC(==, UShort);
365 IMPLEMENT_SETCC(==, Short);
366 IMPLEMENT_SETCC(==, UInt);
367 IMPLEMENT_SETCC(==, Int);
368 IMPLEMENT_SETCC(==, ULong);
369 IMPLEMENT_SETCC(==, Long);
370 IMPLEMENT_SETCC(==, Float);
371 IMPLEMENT_SETCC(==, Double);
372 IMPLEMENT_POINTERSETCC(==);
374 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
380 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
383 switch (Ty->getPrimitiveID()) {
384 IMPLEMENT_SETCC(!=, UByte);
385 IMPLEMENT_SETCC(!=, SByte);
386 IMPLEMENT_SETCC(!=, UShort);
387 IMPLEMENT_SETCC(!=, Short);
388 IMPLEMENT_SETCC(!=, UInt);
389 IMPLEMENT_SETCC(!=, Int);
390 IMPLEMENT_SETCC(!=, ULong);
391 IMPLEMENT_SETCC(!=, Long);
392 IMPLEMENT_SETCC(!=, Float);
393 IMPLEMENT_SETCC(!=, Double);
394 IMPLEMENT_POINTERSETCC(!=);
397 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
403 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
406 switch (Ty->getPrimitiveID()) {
407 IMPLEMENT_SETCC(<=, UByte);
408 IMPLEMENT_SETCC(<=, SByte);
409 IMPLEMENT_SETCC(<=, UShort);
410 IMPLEMENT_SETCC(<=, Short);
411 IMPLEMENT_SETCC(<=, UInt);
412 IMPLEMENT_SETCC(<=, Int);
413 IMPLEMENT_SETCC(<=, ULong);
414 IMPLEMENT_SETCC(<=, Long);
415 IMPLEMENT_SETCC(<=, Float);
416 IMPLEMENT_SETCC(<=, Double);
417 IMPLEMENT_POINTERSETCC(<=);
419 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
425 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
428 switch (Ty->getPrimitiveID()) {
429 IMPLEMENT_SETCC(>=, UByte);
430 IMPLEMENT_SETCC(>=, SByte);
431 IMPLEMENT_SETCC(>=, UShort);
432 IMPLEMENT_SETCC(>=, Short);
433 IMPLEMENT_SETCC(>=, UInt);
434 IMPLEMENT_SETCC(>=, Int);
435 IMPLEMENT_SETCC(>=, ULong);
436 IMPLEMENT_SETCC(>=, Long);
437 IMPLEMENT_SETCC(>=, Float);
438 IMPLEMENT_SETCC(>=, Double);
439 IMPLEMENT_POINTERSETCC(>=);
441 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
447 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
450 switch (Ty->getPrimitiveID()) {
451 IMPLEMENT_SETCC(<, UByte);
452 IMPLEMENT_SETCC(<, SByte);
453 IMPLEMENT_SETCC(<, UShort);
454 IMPLEMENT_SETCC(<, Short);
455 IMPLEMENT_SETCC(<, UInt);
456 IMPLEMENT_SETCC(<, Int);
457 IMPLEMENT_SETCC(<, ULong);
458 IMPLEMENT_SETCC(<, Long);
459 IMPLEMENT_SETCC(<, Float);
460 IMPLEMENT_SETCC(<, Double);
461 IMPLEMENT_POINTERSETCC(<);
463 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
469 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
472 switch (Ty->getPrimitiveID()) {
473 IMPLEMENT_SETCC(>, UByte);
474 IMPLEMENT_SETCC(>, SByte);
475 IMPLEMENT_SETCC(>, UShort);
476 IMPLEMENT_SETCC(>, Short);
477 IMPLEMENT_SETCC(>, UInt);
478 IMPLEMENT_SETCC(>, Int);
479 IMPLEMENT_SETCC(>, ULong);
480 IMPLEMENT_SETCC(>, Long);
481 IMPLEMENT_SETCC(>, Float);
482 IMPLEMENT_SETCC(>, Double);
483 IMPLEMENT_POINTERSETCC(>);
485 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
491 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
492 ExecutionContext &SF = ECStack.back();
493 const Type *Ty = I.getOperand(0)->getType();
494 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
495 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
496 GenericValue R; // Result
498 switch (I.getOpcode()) {
499 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
500 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
501 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
502 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
503 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
504 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
505 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
506 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
507 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
508 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
509 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
510 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
511 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
512 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
514 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
521 //===----------------------------------------------------------------------===//
522 // Terminator Instruction Implementations
523 //===----------------------------------------------------------------------===//
525 void Interpreter::exitCalled(GenericValue GV) {
526 runAtExitHandlers ();
530 /// Pop the last stack frame off of ECStack and then copy the result
531 /// back into the result variable if we are not returning void. The
532 /// result variable may be the ExitCode, or the Value of the calling
533 /// CallInst if there was a previous stack frame. This method may
534 /// invalidate any ECStack iterators you have. This method also takes
535 /// care of switching to the normal destination BB, if we are returning
538 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
539 GenericValue Result) {
540 // Pop the current stack frame.
543 if (ECStack.empty()) { // Finished main. Put result into exit code...
544 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
545 ExitCode = Result.IntVal; // Capture the exit code of the program
550 // If we have a previous stack frame, and we have a previous call,
551 // fill in the return value...
552 ExecutionContext &CallingSF = ECStack.back();
553 if (Instruction *I = CallingSF.Caller.getInstruction()) {
554 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
555 SetValue(I, Result, CallingSF);
556 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
557 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
558 CallingSF.Caller = CallSite(); // We returned from the call...
563 void Interpreter::visitReturnInst(ReturnInst &I) {
564 ExecutionContext &SF = ECStack.back();
565 const Type *RetTy = Type::VoidTy;
568 // Save away the return value... (if we are not 'ret void')
569 if (I.getNumOperands()) {
570 RetTy = I.getReturnValue()->getType();
571 Result = getOperandValue(I.getReturnValue(), SF);
574 popStackAndReturnValueToCaller(RetTy, Result);
577 void Interpreter::visitUnwindInst(UnwindInst &I) {
582 if (ECStack.empty ())
584 Inst = ECStack.back ().Caller.getInstruction ();
585 } while (!(Inst && isa<InvokeInst> (Inst)));
587 // Return from invoke
588 ExecutionContext &InvokingSF = ECStack.back ();
589 InvokingSF.Caller = CallSite ();
591 // Go to exceptional destination BB of invoke instruction
592 SwitchToNewBasicBlock (cast<InvokeInst> (Inst)->getExceptionalDest (),
596 void Interpreter::visitBranchInst(BranchInst &I) {
597 ExecutionContext &SF = ECStack.back();
600 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
601 if (!I.isUnconditional()) {
602 Value *Cond = I.getCondition();
603 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
604 Dest = I.getSuccessor(1);
606 SwitchToNewBasicBlock(Dest, SF);
609 void Interpreter::visitSwitchInst(SwitchInst &I) {
610 ExecutionContext &SF = ECStack.back();
611 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
612 const Type *ElTy = I.getOperand(0)->getType();
614 // Check to see if any of the cases match...
615 BasicBlock *Dest = 0;
616 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
617 if (executeSetEQInst(CondVal,
618 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
619 Dest = cast<BasicBlock>(I.getOperand(i+1));
623 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
624 SwitchToNewBasicBlock(Dest, SF);
627 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
628 // This function handles the actual updating of block and instruction iterators
629 // as well as execution of all of the PHI nodes in the destination block.
631 // This method does this because all of the PHI nodes must be executed
632 // atomically, reading their inputs before any of the results are updated. Not
633 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
634 // their inputs. If the input PHI node is updated before it is read, incorrect
635 // results can happen. Thus we use a two phase approach.
637 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
638 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
639 SF.CurBB = Dest; // Update CurBB to branch destination
640 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
642 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
644 // Loop over all of the PHI nodes in the current block, reading their inputs.
645 std::vector<GenericValue> ResultValues;
647 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
648 // Search for the value corresponding to this previous bb...
649 int i = PN->getBasicBlockIndex(PrevBB);
650 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
651 Value *IncomingValue = PN->getIncomingValue(i);
653 // Save the incoming value for this PHI node...
654 ResultValues.push_back(getOperandValue(IncomingValue, SF));
657 // Now loop over all of the PHI nodes setting their values...
658 SF.CurInst = SF.CurBB->begin();
659 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
661 SetValue(PN, ResultValues[i], SF);
664 //===----------------------------------------------------------------------===//
665 // Memory Instruction Implementations
666 //===----------------------------------------------------------------------===//
668 void Interpreter::visitAllocationInst(AllocationInst &I) {
669 ExecutionContext &SF = ECStack.back();
671 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
673 // Get the number of elements being allocated by the array...
674 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
676 // Allocate enough memory to hold the type...
677 void *Memory = malloc(NumElements * TD.getTypeSize(Ty));
679 GenericValue Result = PTOGV(Memory);
680 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
681 SetValue(&I, Result, SF);
683 if (I.getOpcode() == Instruction::Alloca)
684 ECStack.back().Allocas.add(Memory);
687 void Interpreter::visitFreeInst(FreeInst &I) {
688 ExecutionContext &SF = ECStack.back();
689 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
690 GenericValue Value = getOperandValue(I.getOperand(0), SF);
691 // TODO: Check to make sure memory is allocated
692 free(GVTOP(Value)); // Free memory
695 // getElementOffset - The workhorse for getelementptr.
697 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
699 ExecutionContext &SF) {
700 assert(isa<PointerType>(Ptr->getType()) &&
701 "Cannot getElementOffset of a nonpointer type!");
705 for (; I != E; ++I) {
706 if (const StructType *STy = dyn_cast<StructType>(*I)) {
707 const StructLayout *SLO = TD.getStructLayout(STy);
709 // Indices must be ubyte constants...
710 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
711 unsigned Index = CPU->getValue();
713 Total += SLO->MemberOffsets[Index];
715 const SequentialType *ST = cast<SequentialType>(*I);
716 // Get the index number for the array... which must be long type...
717 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
720 switch (I.getOperand()->getType()->getPrimitiveID()) {
721 default: assert(0 && "Illegal getelementptr index for sequential type!");
722 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
723 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
724 case Type::IntTyID: Idx = IdxGV.IntVal; break;
725 case Type::LongTyID: Idx = IdxGV.LongVal; break;
726 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
727 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
728 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
729 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
731 Total += TD.getTypeSize(ST->getElementType())*Idx;
736 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
740 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
741 ExecutionContext &SF = ECStack.back();
742 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
743 gep_type_begin(I), gep_type_end(I), SF), SF);
746 void Interpreter::visitLoadInst(LoadInst &I) {
747 ExecutionContext &SF = ECStack.back();
748 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
749 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
750 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
751 SetValue(&I, Result, SF);
754 void Interpreter::visitStoreInst(StoreInst &I) {
755 ExecutionContext &SF = ECStack.back();
756 GenericValue Val = getOperandValue(I.getOperand(0), SF);
757 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
758 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
759 I.getOperand(0)->getType());
762 //===----------------------------------------------------------------------===//
763 // Miscellaneous Instruction Implementations
764 //===----------------------------------------------------------------------===//
766 void Interpreter::visitCallSite(CallSite CS) {
767 ExecutionContext &SF = ECStack.back();
769 std::vector<GenericValue> ArgVals;
770 const unsigned NumArgs = SF.Caller.arg_size();
771 ArgVals.reserve(NumArgs);
772 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
773 e = SF.Caller.arg_end(); i != e; ++i) {
775 ArgVals.push_back(getOperandValue(V, SF));
776 // Promote all integral types whose size is < sizeof(int) into ints. We do
777 // this by zero or sign extending the value as appropriate according to the
779 const Type *Ty = V->getType();
780 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
781 if (Ty == Type::ShortTy)
782 ArgVals.back().IntVal = ArgVals.back().ShortVal;
783 else if (Ty == Type::UShortTy)
784 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
785 else if (Ty == Type::SByteTy)
786 ArgVals.back().IntVal = ArgVals.back().SByteVal;
787 else if (Ty == Type::UByteTy)
788 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
789 else if (Ty == Type::BoolTy)
790 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
792 assert(0 && "Unknown type!");
796 // To handle indirect calls, we must get the pointer value from the argument
797 // and treat it as a function pointer.
798 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
799 callFunction((Function*)GVTOP(SRC), ArgVals);
802 #define IMPLEMENT_SHIFT(OP, TY) \
803 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
805 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
808 switch (Ty->getPrimitiveID()) {
809 IMPLEMENT_SHIFT(<<, UByte);
810 IMPLEMENT_SHIFT(<<, SByte);
811 IMPLEMENT_SHIFT(<<, UShort);
812 IMPLEMENT_SHIFT(<<, Short);
813 IMPLEMENT_SHIFT(<<, UInt);
814 IMPLEMENT_SHIFT(<<, Int);
815 IMPLEMENT_SHIFT(<<, ULong);
816 IMPLEMENT_SHIFT(<<, Long);
818 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
823 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
826 switch (Ty->getPrimitiveID()) {
827 IMPLEMENT_SHIFT(>>, UByte);
828 IMPLEMENT_SHIFT(>>, SByte);
829 IMPLEMENT_SHIFT(>>, UShort);
830 IMPLEMENT_SHIFT(>>, Short);
831 IMPLEMENT_SHIFT(>>, UInt);
832 IMPLEMENT_SHIFT(>>, Int);
833 IMPLEMENT_SHIFT(>>, ULong);
834 IMPLEMENT_SHIFT(>>, Long);
836 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
842 void Interpreter::visitShl(ShiftInst &I) {
843 ExecutionContext &SF = ECStack.back();
844 const Type *Ty = I.getOperand(0)->getType();
845 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
846 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
848 Dest = executeShlInst (Src1, Src2, Ty);
849 SetValue(&I, Dest, SF);
852 void Interpreter::visitShr(ShiftInst &I) {
853 ExecutionContext &SF = ECStack.back();
854 const Type *Ty = I.getOperand(0)->getType();
855 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
856 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
858 Dest = executeShrInst (Src1, Src2, Ty);
859 SetValue(&I, Dest, SF);
862 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
863 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
865 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
866 case Type::DESTTY##TyID: \
867 switch (SrcTy->getPrimitiveID()) { \
868 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
869 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
870 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
871 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
872 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
873 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
874 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
875 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
876 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
877 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
879 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
880 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
881 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
883 #define IMPLEMENT_CAST_CASE_END() \
884 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
889 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
890 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
891 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
892 IMPLEMENT_CAST_CASE_END()
894 GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
895 ExecutionContext &SF) {
896 const Type *SrcTy = SrcVal->getType();
897 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
899 switch (Ty->getPrimitiveID()) {
900 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
901 IMPLEMENT_CAST_CASE(SByte , ( signed char));
902 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
903 IMPLEMENT_CAST_CASE(Short , ( signed short));
904 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
905 IMPLEMENT_CAST_CASE(Int , ( signed int ));
906 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
907 IMPLEMENT_CAST_CASE(Long , ( int64_t));
908 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
909 IMPLEMENT_CAST_CASE(Float , (float));
910 IMPLEMENT_CAST_CASE(Double , (double));
911 IMPLEMENT_CAST_CASE(Bool , (bool));
913 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
920 void Interpreter::visitCastInst(CastInst &I) {
921 ExecutionContext &SF = ECStack.back();
922 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
925 void Interpreter::visitVANextInst(VANextInst &I) {
926 ExecutionContext &SF = ECStack.back();
928 // Get the incoming valist parameter. LLI treats the valist as a pointer
929 // to the next argument.
930 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
932 // Move the pointer to the next vararg.
933 GenericValue *ArgPtr = (GenericValue *) GVTOP (VAList);
935 VAList = PTOGV (ArgPtr);
936 SetValue(&I, VAList, SF);
939 #define IMPLEMENT_VAARG(TY) \
940 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
942 void Interpreter::visitVAArgInst(VAArgInst &I) {
943 ExecutionContext &SF = ECStack.back();
945 // Get the incoming valist parameter. LLI treats the valist as a pointer
946 // to the next argument.
947 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
948 assert (GVTOP (VAList) != 0 && "VAList was null in vaarg instruction");
949 GenericValue Dest, Src = *(GenericValue *) GVTOP (VAList);
950 const Type *Ty = I.getType();
951 switch (Ty->getPrimitiveID()) {
952 IMPLEMENT_VAARG(UByte);
953 IMPLEMENT_VAARG(SByte);
954 IMPLEMENT_VAARG(UShort);
955 IMPLEMENT_VAARG(Short);
956 IMPLEMENT_VAARG(UInt);
957 IMPLEMENT_VAARG(Int);
958 IMPLEMENT_VAARG(ULong);
959 IMPLEMENT_VAARG(Long);
960 IMPLEMENT_VAARG(Pointer);
961 IMPLEMENT_VAARG(Float);
962 IMPLEMENT_VAARG(Double);
963 IMPLEMENT_VAARG(Bool);
965 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
969 // Set the Value of this Instruction.
970 SetValue(&I, Dest, SF);
973 //===----------------------------------------------------------------------===//
974 // Dispatch and Execution Code
975 //===----------------------------------------------------------------------===//
977 //===----------------------------------------------------------------------===//
978 // callFunction - Execute the specified function...
980 void Interpreter::callFunction(Function *F,
981 const std::vector<GenericValue> &ArgVals) {
982 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
983 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
984 "Incorrect number of arguments passed into function call!");
985 // Make a new stack frame... and fill it in.
986 ECStack.push_back(ExecutionContext());
987 ExecutionContext &StackFrame = ECStack.back();
988 StackFrame.CurFunction = F;
990 // Special handling for external functions.
991 if (F->isExternal()) {
992 GenericValue Result = callExternalFunction (F, ArgVals);
993 // Simulate a 'ret' instruction of the appropriate type.
994 popStackAndReturnValueToCaller (F->getReturnType (), Result);
998 // Get pointers to first LLVM BB & Instruction in function.
999 StackFrame.CurBB = F->begin();
1000 StackFrame.CurInst = StackFrame.CurBB->begin();
1002 // Run through the function arguments and initialize their values...
1003 assert((ArgVals.size() == F->asize() ||
1004 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
1005 "Invalid number of values passed to function invocation!");
1007 // Handle non-varargs arguments...
1009 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
1010 SetValue(AI, ArgVals[i], StackFrame);
1012 // Handle varargs arguments...
1013 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1016 void Interpreter::run() {
1017 while (!ECStack.empty()) {
1018 // Interpret a single instruction & increment the "PC".
1019 ExecutionContext &SF = ECStack.back(); // Current stack frame
1020 Instruction &I = *SF.CurInst++; // Increment before execute
1022 // Track the number of dynamic instructions executed.
1025 DEBUG(std::cerr << "About to interpret: " << I);
1026 visit(I); // Dispatch to one of the visit* methods...