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"
15 #include "Interpreter.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/CodeGen/IntrinsicLowering.h"
20 #include "llvm/Support/GetElementPtrTypeIterator.h"
21 #include "llvm/ADT/APInt.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/MathExtras.h"
28 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
29 static Interpreter *TheEE = 0;
31 //===----------------------------------------------------------------------===//
32 // Various Helper Functions
33 //===----------------------------------------------------------------------===//
35 static inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
36 // Determine if the value is signed or not
37 bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
38 // If its signed, extend the sign bits
40 Val |= ~ITy->getBitMask();
44 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
48 void Interpreter::initializeExecutionEngine() {
52 //===----------------------------------------------------------------------===//
53 // Binary Instruction Implementations
54 //===----------------------------------------------------------------------===//
56 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
57 case Type::TY##TyID: \
58 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
61 #define IMPLEMENT_INTEGER_BINOP1(OP, TY) \
62 case Type::IntegerTyID: { \
63 Dest.IntVal = Src1.IntVal OP Src2.IntVal; \
68 static void executeAddInst(GenericValue &Dest, GenericValue Src1,
69 GenericValue Src2, const Type *Ty) {
70 switch (Ty->getTypeID()) {
71 IMPLEMENT_INTEGER_BINOP1(+, Ty);
72 IMPLEMENT_BINARY_OPERATOR(+, Float);
73 IMPLEMENT_BINARY_OPERATOR(+, Double);
75 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
80 static void executeSubInst(GenericValue &Dest, GenericValue Src1,
81 GenericValue Src2, const Type *Ty) {
82 switch (Ty->getTypeID()) {
83 IMPLEMENT_INTEGER_BINOP1(-, Ty);
84 IMPLEMENT_BINARY_OPERATOR(-, Float);
85 IMPLEMENT_BINARY_OPERATOR(-, Double);
87 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
92 static void executeMulInst(GenericValue &Dest, GenericValue Src1,
93 GenericValue Src2, const Type *Ty) {
94 switch (Ty->getTypeID()) {
95 IMPLEMENT_INTEGER_BINOP1(*, Ty);
96 IMPLEMENT_BINARY_OPERATOR(*, Float);
97 IMPLEMENT_BINARY_OPERATOR(*, Double);
99 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
104 static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
105 GenericValue Src2, const Type *Ty) {
106 switch (Ty->getTypeID()) {
107 IMPLEMENT_BINARY_OPERATOR(/, Float);
108 IMPLEMENT_BINARY_OPERATOR(/, Double);
110 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
115 static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
116 GenericValue Src2, const Type *Ty) {
117 switch (Ty->getTypeID()) {
118 case Type::FloatTyID:
119 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
121 case Type::DoubleTyID:
122 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
125 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
130 #define IMPLEMENT_INTEGER_ICMP(OP, TY) \
131 case Type::IntegerTyID: \
132 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
135 // Handle pointers specially because they must be compared with only as much
136 // width as the host has. We _do not_ want to be comparing 64 bit values when
137 // running on a 32-bit target, otherwise the upper 32 bits might mess up
138 // comparisons if they contain garbage.
139 #define IMPLEMENT_POINTER_ICMP(OP) \
140 case Type::PointerTyID: \
141 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
142 (void*)(intptr_t)Src2.PointerVal); \
145 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
148 switch (Ty->getTypeID()) {
149 IMPLEMENT_INTEGER_ICMP(eq,Ty);
150 IMPLEMENT_POINTER_ICMP(==);
152 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
158 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
161 switch (Ty->getTypeID()) {
162 IMPLEMENT_INTEGER_ICMP(ne,Ty);
163 IMPLEMENT_POINTER_ICMP(!=);
165 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
171 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
174 switch (Ty->getTypeID()) {
175 IMPLEMENT_INTEGER_ICMP(ult,Ty);
176 IMPLEMENT_POINTER_ICMP(<);
178 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
184 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
187 switch (Ty->getTypeID()) {
188 IMPLEMENT_INTEGER_ICMP(slt,Ty);
189 IMPLEMENT_POINTER_ICMP(<);
191 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
197 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
200 switch (Ty->getTypeID()) {
201 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
202 IMPLEMENT_POINTER_ICMP(>);
204 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
210 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
213 switch (Ty->getTypeID()) {
214 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
215 IMPLEMENT_POINTER_ICMP(>);
217 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
223 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
226 switch (Ty->getTypeID()) {
227 IMPLEMENT_INTEGER_ICMP(ule,Ty);
228 IMPLEMENT_POINTER_ICMP(<=);
230 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
236 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
239 switch (Ty->getTypeID()) {
240 IMPLEMENT_INTEGER_ICMP(sle,Ty);
241 IMPLEMENT_POINTER_ICMP(<=);
243 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
249 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
252 switch (Ty->getTypeID()) {
253 IMPLEMENT_INTEGER_ICMP(uge,Ty);
254 IMPLEMENT_POINTER_ICMP(>=);
256 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
262 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
265 switch (Ty->getTypeID()) {
266 IMPLEMENT_INTEGER_ICMP(sge,Ty);
267 IMPLEMENT_POINTER_ICMP(>=);
269 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
275 void Interpreter::visitICmpInst(ICmpInst &I) {
276 ExecutionContext &SF = ECStack.back();
277 const Type *Ty = I.getOperand(0)->getType();
278 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
279 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
280 GenericValue R; // Result
282 switch (I.getPredicate()) {
283 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
284 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
285 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
286 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
287 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
288 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
289 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
290 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
291 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
292 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
294 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
301 #define IMPLEMENT_FCMP(OP, TY) \
302 case Type::TY##TyID: \
303 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
306 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
309 switch (Ty->getTypeID()) {
310 IMPLEMENT_FCMP(==, Float);
311 IMPLEMENT_FCMP(==, Double);
313 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
319 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
322 switch (Ty->getTypeID()) {
323 IMPLEMENT_FCMP(!=, Float);
324 IMPLEMENT_FCMP(!=, Double);
327 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
333 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
336 switch (Ty->getTypeID()) {
337 IMPLEMENT_FCMP(<=, Float);
338 IMPLEMENT_FCMP(<=, Double);
340 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
346 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
349 switch (Ty->getTypeID()) {
350 IMPLEMENT_FCMP(>=, Float);
351 IMPLEMENT_FCMP(>=, Double);
353 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
359 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
362 switch (Ty->getTypeID()) {
363 IMPLEMENT_FCMP(<, Float);
364 IMPLEMENT_FCMP(<, Double);
366 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
372 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
375 switch (Ty->getTypeID()) {
376 IMPLEMENT_FCMP(>, Float);
377 IMPLEMENT_FCMP(>, Double);
379 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
385 #define IMPLEMENT_UNORDERED(TY, X,Y) \
386 if (TY == Type::FloatTy) \
387 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
388 Dest.IntVal = APInt(1,true); \
391 else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
392 Dest.IntVal = APInt(1,true); \
397 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
400 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
401 return executeFCMP_OEQ(Src1, Src2, Ty);
404 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
407 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
408 return executeFCMP_ONE(Src1, Src2, Ty);
411 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
414 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
415 return executeFCMP_OLE(Src1, Src2, Ty);
418 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
421 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
422 return executeFCMP_OGE(Src1, Src2, Ty);
425 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
428 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
429 return executeFCMP_OLT(Src1, Src2, Ty);
432 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
435 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
436 return executeFCMP_OGT(Src1, Src2, Ty);
439 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
442 if (Ty == Type::FloatTy)
443 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
444 Src2.FloatVal == Src2.FloatVal));
446 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
447 Src2.DoubleVal == Src2.DoubleVal));
451 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
454 if (Ty == Type::FloatTy)
455 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
456 Src2.FloatVal != Src2.FloatVal));
458 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
459 Src2.DoubleVal != Src2.DoubleVal));
463 void Interpreter::visitFCmpInst(FCmpInst &I) {
464 ExecutionContext &SF = ECStack.back();
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 R; // Result
470 switch (I.getPredicate()) {
471 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
472 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
473 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
474 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
475 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
476 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
477 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
478 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
479 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
480 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
481 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
482 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
483 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
484 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
485 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
486 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
488 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
495 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
496 GenericValue Src2, const Type *Ty) {
499 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
500 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
501 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
502 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
503 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
504 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
505 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
506 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
507 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
508 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
509 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
510 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
511 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
512 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
513 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
514 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
515 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
516 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
517 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
518 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
519 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
520 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
521 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
522 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
523 case FCmpInst::FCMP_FALSE: {
525 Result.IntVal = APInt(1, false);
528 case FCmpInst::FCMP_TRUE: {
530 Result.IntVal = APInt(1, true);
534 cerr << "Unhandled Cmp predicate\n";
539 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
540 ExecutionContext &SF = ECStack.back();
541 const Type *Ty = I.getOperand(0)->getType();
542 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
543 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
544 GenericValue R; // Result
546 switch (I.getOpcode()) {
547 case Instruction::Add: executeAddInst (R, Src1, Src2, Ty); break;
548 case Instruction::Sub: executeSubInst (R, Src1, Src2, Ty); break;
549 case Instruction::Mul: executeMulInst (R, Src1, Src2, Ty); break;
550 case Instruction::FDiv: executeFDivInst (R, Src1, Src2, Ty); break;
551 case Instruction::FRem: executeFRemInst (R, Src1, Src2, Ty); break;
552 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
553 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
554 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
555 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
556 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
557 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
558 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
560 cerr << "Don't know how to handle this binary operator!\n-->" << I;
567 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
569 return Src1.IntVal == 0 ? Src3 : Src2;
572 void Interpreter::visitSelectInst(SelectInst &I) {
573 ExecutionContext &SF = ECStack.back();
574 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
575 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
576 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
577 GenericValue R = executeSelectInst(Src1, Src2, Src3);
582 //===----------------------------------------------------------------------===//
583 // Terminator Instruction Implementations
584 //===----------------------------------------------------------------------===//
586 void Interpreter::exitCalled(GenericValue GV) {
587 // runAtExitHandlers() assumes there are no stack frames, but
588 // if exit() was called, then it had a stack frame. Blow away
589 // the stack before interpreting atexit handlers.
591 runAtExitHandlers ();
592 exit (GV.IntVal.zextOrTrunc(32).getZExtValue());
595 /// Pop the last stack frame off of ECStack and then copy the result
596 /// back into the result variable if we are not returning void. The
597 /// result variable may be the ExitValue, or the Value of the calling
598 /// CallInst if there was a previous stack frame. This method may
599 /// invalidate any ECStack iterators you have. This method also takes
600 /// care of switching to the normal destination BB, if we are returning
603 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
604 GenericValue Result) {
605 // Pop the current stack frame.
608 if (ECStack.empty()) { // Finished main. Put result into exit code...
609 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
610 ExitValue = Result; // Capture the exit value of the program
612 memset(&ExitValue, 0, sizeof(ExitValue));
615 // If we have a previous stack frame, and we have a previous call,
616 // fill in the return value...
617 ExecutionContext &CallingSF = ECStack.back();
618 if (Instruction *I = CallingSF.Caller.getInstruction()) {
619 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
620 SetValue(I, Result, CallingSF);
621 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
622 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
623 CallingSF.Caller = CallSite(); // We returned from the call...
628 void Interpreter::visitReturnInst(ReturnInst &I) {
629 ExecutionContext &SF = ECStack.back();
630 const Type *RetTy = Type::VoidTy;
633 // Save away the return value... (if we are not 'ret void')
634 if (I.getNumOperands()) {
635 RetTy = I.getReturnValue()->getType();
636 Result = getOperandValue(I.getReturnValue(), SF);
639 popStackAndReturnValueToCaller(RetTy, Result);
642 void Interpreter::visitUnwindInst(UnwindInst &I) {
647 if (ECStack.empty ())
649 Inst = ECStack.back ().Caller.getInstruction ();
650 } while (!(Inst && isa<InvokeInst> (Inst)));
652 // Return from invoke
653 ExecutionContext &InvokingSF = ECStack.back ();
654 InvokingSF.Caller = CallSite ();
656 // Go to exceptional destination BB of invoke instruction
657 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
660 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
661 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
665 void Interpreter::visitBranchInst(BranchInst &I) {
666 ExecutionContext &SF = ECStack.back();
669 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
670 if (!I.isUnconditional()) {
671 Value *Cond = I.getCondition();
672 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
673 Dest = I.getSuccessor(1);
675 SwitchToNewBasicBlock(Dest, SF);
678 void Interpreter::visitSwitchInst(SwitchInst &I) {
679 ExecutionContext &SF = ECStack.back();
680 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
681 const Type *ElTy = I.getOperand(0)->getType();
683 // Check to see if any of the cases match...
684 BasicBlock *Dest = 0;
685 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
686 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
688 Dest = cast<BasicBlock>(I.getOperand(i+1));
692 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
693 SwitchToNewBasicBlock(Dest, SF);
696 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
697 // This function handles the actual updating of block and instruction iterators
698 // as well as execution of all of the PHI nodes in the destination block.
700 // This method does this because all of the PHI nodes must be executed
701 // atomically, reading their inputs before any of the results are updated. Not
702 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
703 // their inputs. If the input PHI node is updated before it is read, incorrect
704 // results can happen. Thus we use a two phase approach.
706 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
707 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
708 SF.CurBB = Dest; // Update CurBB to branch destination
709 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
711 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
713 // Loop over all of the PHI nodes in the current block, reading their inputs.
714 std::vector<GenericValue> ResultValues;
716 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
717 // Search for the value corresponding to this previous bb...
718 int i = PN->getBasicBlockIndex(PrevBB);
719 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
720 Value *IncomingValue = PN->getIncomingValue(i);
722 // Save the incoming value for this PHI node...
723 ResultValues.push_back(getOperandValue(IncomingValue, SF));
726 // Now loop over all of the PHI nodes setting their values...
727 SF.CurInst = SF.CurBB->begin();
728 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
729 PHINode *PN = cast<PHINode>(SF.CurInst);
730 SetValue(PN, ResultValues[i], SF);
734 //===----------------------------------------------------------------------===//
735 // Memory Instruction Implementations
736 //===----------------------------------------------------------------------===//
738 void Interpreter::visitAllocationInst(AllocationInst &I) {
739 ExecutionContext &SF = ECStack.back();
741 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
743 // Get the number of elements being allocated by the array...
744 unsigned NumElements =
745 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
747 unsigned TypeSize = (size_t)TD.getTypeSize(Ty);
749 unsigned MemToAlloc = NumElements * TypeSize;
751 // Allocate enough memory to hold the type...
752 void *Memory = malloc(MemToAlloc);
754 DOUT << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
755 << NumElements << " (Total: " << MemToAlloc << ") at "
756 << std::hex << Memory << '\n';
758 GenericValue Result = PTOGV(Memory);
759 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
760 SetValue(&I, Result, SF);
762 if (I.getOpcode() == Instruction::Alloca)
763 ECStack.back().Allocas.add(Memory);
766 void Interpreter::visitFreeInst(FreeInst &I) {
767 ExecutionContext &SF = ECStack.back();
768 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
769 GenericValue Value = getOperandValue(I.getOperand(0), SF);
770 // TODO: Check to make sure memory is allocated
771 free(GVTOP(Value)); // Free memory
774 // getElementOffset - The workhorse for getelementptr.
776 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
778 ExecutionContext &SF) {
779 assert(isa<PointerType>(Ptr->getType()) &&
780 "Cannot getElementOffset of a nonpointer type!");
784 for (; I != E; ++I) {
785 if (const StructType *STy = dyn_cast<StructType>(*I)) {
786 const StructLayout *SLO = TD.getStructLayout(STy);
788 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
789 unsigned Index = unsigned(CPU->getZExtValue());
791 Total += SLO->getElementOffset(Index);
793 const SequentialType *ST = cast<SequentialType>(*I);
794 // Get the index number for the array... which must be long type...
795 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
799 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
801 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
802 else if (BitWidth == 64)
803 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
805 assert(0 && "Invalid index type for getelementptr");
806 Total += TD.getTypeSize(ST->getElementType())*Idx;
811 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
812 DOUT << "GEP Index " << Total << " bytes.\n";
816 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
817 ExecutionContext &SF = ECStack.back();
818 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
819 gep_type_begin(I), gep_type_end(I), SF), SF);
822 void Interpreter::visitLoadInst(LoadInst &I) {
823 ExecutionContext &SF = ECStack.back();
824 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
825 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
827 LoadValueFromMemory(Result, Ptr, I.getType());
828 SetValue(&I, Result, SF);
831 void Interpreter::visitStoreInst(StoreInst &I) {
832 ExecutionContext &SF = ECStack.back();
833 GenericValue Val = getOperandValue(I.getOperand(0), SF);
834 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
835 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
836 I.getOperand(0)->getType());
839 //===----------------------------------------------------------------------===//
840 // Miscellaneous Instruction Implementations
841 //===----------------------------------------------------------------------===//
843 void Interpreter::visitCallSite(CallSite CS) {
844 ExecutionContext &SF = ECStack.back();
846 // Check to see if this is an intrinsic function call...
847 if (Function *F = CS.getCalledFunction())
848 if (F->isDeclaration ())
849 switch (F->getIntrinsicID()) {
850 case Intrinsic::not_intrinsic:
852 case Intrinsic::vastart: { // va_start
853 GenericValue ArgIndex;
854 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
855 ArgIndex.UIntPairVal.second = 0;
856 SetValue(CS.getInstruction(), ArgIndex, SF);
859 case Intrinsic::vaend: // va_end is a noop for the interpreter
861 case Intrinsic::vacopy: // va_copy: dest = src
862 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
865 // If it is an unknown intrinsic function, use the intrinsic lowering
866 // class to transform it into hopefully tasty LLVM code.
868 Instruction *Prev = CS.getInstruction()->getPrev();
869 BasicBlock *Parent = CS.getInstruction()->getParent();
870 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
872 // Restore the CurInst pointer to the first instruction newly inserted, if
875 SF.CurInst = Parent->begin();
884 std::vector<GenericValue> ArgVals;
885 const unsigned NumArgs = SF.Caller.arg_size();
886 ArgVals.reserve(NumArgs);
887 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
888 e = SF.Caller.arg_end(); i != e; ++i) {
890 ArgVals.push_back(getOperandValue(V, SF));
891 // Promote all integral types whose size is < sizeof(int) into ints. We do
892 // this by zero or sign extending the value as appropriate according to the
894 const Type *Ty = V->getType();
896 if (ArgVals.back().IntVal.getBitWidth() < 32)
897 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
900 // To handle indirect calls, we must get the pointer value from the argument
901 // and treat it as a function pointer.
902 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
903 callFunction((Function*)GVTOP(SRC), ArgVals);
906 void Interpreter::visitShl(BinaryOperator &I) {
907 ExecutionContext &SF = ECStack.back();
908 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
909 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
911 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
912 SetValue(&I, Dest, SF);
915 void Interpreter::visitLShr(BinaryOperator &I) {
916 ExecutionContext &SF = ECStack.back();
917 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
918 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
920 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
921 SetValue(&I, Dest, SF);
924 void Interpreter::visitAShr(BinaryOperator &I) {
925 ExecutionContext &SF = ECStack.back();
926 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
927 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
929 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
930 SetValue(&I, Dest, SF);
933 GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
934 ExecutionContext &SF) {
935 const Type *SrcTy = SrcVal->getType();
936 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
937 const IntegerType *DITy = cast<IntegerType>(DstTy);
938 const IntegerType *SITy = cast<IntegerType>(SrcTy);
939 unsigned DBitWidth = DITy->getBitWidth();
940 unsigned SBitWidth = SITy->getBitWidth();
941 assert(SBitWidth > DBitWidth && "Invalid truncate");
942 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
946 GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
947 ExecutionContext &SF) {
948 const Type *SrcTy = SrcVal->getType();
949 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
950 const IntegerType *DITy = cast<IntegerType>(DstTy);
951 const IntegerType *SITy = cast<IntegerType>(SrcTy);
952 unsigned DBitWidth = DITy->getBitWidth();
953 unsigned SBitWidth = SITy->getBitWidth();
954 assert(SBitWidth < DBitWidth && "Invalid sign extend");
955 Dest.IntVal = Src.IntVal.sext(DBitWidth);
959 GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
960 ExecutionContext &SF) {
961 const Type *SrcTy = SrcVal->getType();
962 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
963 const IntegerType *DITy = cast<IntegerType>(DstTy);
964 const IntegerType *SITy = cast<IntegerType>(SrcTy);
965 unsigned DBitWidth = DITy->getBitWidth();
966 unsigned SBitWidth = SITy->getBitWidth();
967 assert(SBitWidth < DBitWidth && "Invalid sign extend");
968 Dest.IntVal = Src.IntVal.zext(DBitWidth);
972 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
973 ExecutionContext &SF) {
974 const Type *SrcTy = SrcVal->getType();
975 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
976 assert(SrcTy == Type::DoubleTy && DstTy == Type::FloatTy &&
977 "Invalid FPTrunc instruction");
978 Dest.FloatVal = (float) Src.DoubleVal;
982 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
983 ExecutionContext &SF) {
984 const Type *SrcTy = SrcVal->getType();
985 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
986 assert(SrcTy == Type::FloatTy && DstTy == Type::DoubleTy &&
987 "Invalid FPTrunc instruction");
988 Dest.DoubleVal = (double) Src.FloatVal;
992 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
993 ExecutionContext &SF) {
994 const Type *SrcTy = SrcVal->getType();
995 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
996 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
997 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
999 if (SrcTy->getTypeID() == Type::FloatTyID)
1000 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1002 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1006 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1007 ExecutionContext &SF) {
1008 const Type *SrcTy = SrcVal->getType();
1009 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1010 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1011 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1013 if (SrcTy->getTypeID() == Type::FloatTyID)
1014 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1016 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1020 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1021 ExecutionContext &SF) {
1022 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1023 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1025 if (DstTy->getTypeID() == Type::FloatTyID)
1026 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
1028 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
1032 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1033 ExecutionContext &SF) {
1034 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1035 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
1037 if (DstTy->getTypeID() == Type::FloatTyID)
1038 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
1040 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
1045 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1046 ExecutionContext &SF) {
1047 const Type *SrcTy = SrcVal->getType();
1048 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1049 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1050 assert(isa<PointerType>(SrcTy) && "Invalid PtrToInt instruction");
1052 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1056 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1057 ExecutionContext &SF) {
1058 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1059 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1061 uint32_t PtrSize = TD.getPointerSizeInBits();
1062 if (PtrSize != Src.IntVal.getBitWidth())
1063 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
1065 Dest.PointerVal = (PointerTy) Src.IntVal.getZExtValue();
1069 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1070 ExecutionContext &SF) {
1072 const Type *SrcTy = SrcVal->getType();
1073 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1074 if (isa<PointerType>(DstTy)) {
1075 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1076 Dest.PointerVal = Src.PointerVal;
1077 } else if (DstTy->isInteger()) {
1078 if (SrcTy == Type::FloatTy) {
1079 Dest.IntVal.floatToBits(Src.FloatVal);
1080 } else if (SrcTy == Type::DoubleTy) {
1081 Dest.IntVal.doubleToBits(Src.DoubleVal);
1082 } else if (SrcTy->isInteger()) {
1083 Dest.IntVal = Src.IntVal;
1085 assert(0 && "Invalid BitCast");
1086 } else if (DstTy == Type::FloatTy) {
1087 if (SrcTy->isInteger())
1088 Dest.FloatVal = Src.IntVal.bitsToFloat();
1090 Dest.FloatVal = Src.FloatVal;
1091 } else if (DstTy == Type::DoubleTy) {
1092 if (SrcTy->isInteger())
1093 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1095 Dest.DoubleVal = Src.DoubleVal;
1097 assert(0 && "Invalid Bitcast");
1102 void Interpreter::visitTruncInst(TruncInst &I) {
1103 ExecutionContext &SF = ECStack.back();
1104 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1107 void Interpreter::visitSExtInst(SExtInst &I) {
1108 ExecutionContext &SF = ECStack.back();
1109 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1112 void Interpreter::visitZExtInst(ZExtInst &I) {
1113 ExecutionContext &SF = ECStack.back();
1114 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1117 void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1118 ExecutionContext &SF = ECStack.back();
1119 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1122 void Interpreter::visitFPExtInst(FPExtInst &I) {
1123 ExecutionContext &SF = ECStack.back();
1124 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1127 void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1128 ExecutionContext &SF = ECStack.back();
1129 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1132 void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1133 ExecutionContext &SF = ECStack.back();
1134 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1137 void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1138 ExecutionContext &SF = ECStack.back();
1139 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1142 void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1143 ExecutionContext &SF = ECStack.back();
1144 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1147 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1148 ExecutionContext &SF = ECStack.back();
1149 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1152 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1153 ExecutionContext &SF = ECStack.back();
1154 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1157 void Interpreter::visitBitCastInst(BitCastInst &I) {
1158 ExecutionContext &SF = ECStack.back();
1159 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1162 #define IMPLEMENT_VAARG(TY) \
1163 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1165 void Interpreter::visitVAArgInst(VAArgInst &I) {
1166 ExecutionContext &SF = ECStack.back();
1168 // Get the incoming valist parameter. LLI treats the valist as a
1169 // (ec-stack-depth var-arg-index) pair.
1170 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1172 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1173 .VarArgs[VAList.UIntPairVal.second];
1174 const Type *Ty = I.getType();
1175 switch (Ty->getTypeID()) {
1176 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
1177 IMPLEMENT_VAARG(Pointer);
1178 IMPLEMENT_VAARG(Float);
1179 IMPLEMENT_VAARG(Double);
1181 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1185 // Set the Value of this Instruction.
1186 SetValue(&I, Dest, SF);
1188 // Move the pointer to the next vararg.
1189 ++VAList.UIntPairVal.second;
1192 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1193 ExecutionContext &SF) {
1194 switch (CE->getOpcode()) {
1195 case Instruction::Trunc:
1196 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1197 case Instruction::ZExt:
1198 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1199 case Instruction::SExt:
1200 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1201 case Instruction::FPTrunc:
1202 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1203 case Instruction::FPExt:
1204 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1205 case Instruction::UIToFP:
1206 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1207 case Instruction::SIToFP:
1208 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1209 case Instruction::FPToUI:
1210 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1211 case Instruction::FPToSI:
1212 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1213 case Instruction::PtrToInt:
1214 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1215 case Instruction::IntToPtr:
1216 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1217 case Instruction::BitCast:
1218 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1219 case Instruction::GetElementPtr:
1220 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1221 gep_type_end(CE), SF);
1222 case Instruction::FCmp:
1223 case Instruction::ICmp:
1224 return executeCmpInst(CE->getPredicate(),
1225 getOperandValue(CE->getOperand(0), SF),
1226 getOperandValue(CE->getOperand(1), SF),
1227 CE->getOperand(0)->getType());
1228 case Instruction::Select:
1229 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1230 getOperandValue(CE->getOperand(1), SF),
1231 getOperandValue(CE->getOperand(2), SF));
1236 // The cases below here require a GenericValue parameter for the result
1237 // so we initialize one, compute it and then return it.
1238 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1239 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
1241 const Type * Ty = CE->getOperand(0)->getType();
1242 switch (CE->getOpcode()) {
1243 case Instruction::Add: executeAddInst (Dest, Op0, Op1, Ty); break;
1244 case Instruction::Sub: executeSubInst (Dest, Op0, Op1, Ty); break;
1245 case Instruction::Mul: executeMulInst (Dest, Op0, Op1, Ty); break;
1246 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1247 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1248 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1249 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1250 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1251 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
1252 case Instruction::And: Dest.IntVal = Op0.IntVal.And(Op1.IntVal); break;
1253 case Instruction::Or: Dest.IntVal = Op0.IntVal.Or(Op1.IntVal); break;
1254 case Instruction::Xor: Dest.IntVal = Op0.IntVal.Xor(Op1.IntVal); break;
1255 case Instruction::Shl:
1256 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1258 case Instruction::LShr:
1259 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1261 case Instruction::AShr:
1262 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1265 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
1267 return GenericValue();
1272 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1273 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1274 return getConstantExprValue(CE, SF);
1275 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1276 return getConstantValue(CPV);
1277 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1278 return PTOGV(getPointerToGlobal(GV));
1280 return SF.Values[V];
1284 //===----------------------------------------------------------------------===//
1285 // Dispatch and Execution Code
1286 //===----------------------------------------------------------------------===//
1288 //===----------------------------------------------------------------------===//
1289 // callFunction - Execute the specified function...
1291 void Interpreter::callFunction(Function *F,
1292 const std::vector<GenericValue> &ArgVals) {
1293 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1294 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1295 "Incorrect number of arguments passed into function call!");
1296 // Make a new stack frame... and fill it in.
1297 ECStack.push_back(ExecutionContext());
1298 ExecutionContext &StackFrame = ECStack.back();
1299 StackFrame.CurFunction = F;
1301 // Special handling for external functions.
1302 if (F->isDeclaration()) {
1303 GenericValue Result = callExternalFunction (F, ArgVals);
1304 // Simulate a 'ret' instruction of the appropriate type.
1305 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1309 // Get pointers to first LLVM BB & Instruction in function.
1310 StackFrame.CurBB = F->begin();
1311 StackFrame.CurInst = StackFrame.CurBB->begin();
1313 // Run through the function arguments and initialize their values...
1314 assert((ArgVals.size() == F->arg_size() ||
1315 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1316 "Invalid number of values passed to function invocation!");
1318 // Handle non-varargs arguments...
1320 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
1321 SetValue(AI, ArgVals[i], StackFrame);
1323 // Handle varargs arguments...
1324 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1327 void Interpreter::run() {
1328 while (!ECStack.empty()) {
1329 // Interpret a single instruction & increment the "PC".
1330 ExecutionContext &SF = ECStack.back(); // Current stack frame
1331 Instruction &I = *SF.CurInst++; // Increment before execute
1333 // Track the number of dynamic instructions executed.
1336 DOUT << "About to interpret: " << I;
1337 visit(I); // Dispatch to one of the visit* methods...