1 //===-- Execution.cpp - Implement code to simulate the program ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/MathExtras.h"
31 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
32 static Interpreter *TheEE = 0;
34 static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
35 cl::desc("make the interpreter print every volatile load and store"));
37 //===----------------------------------------------------------------------===//
38 // Various Helper Functions
39 //===----------------------------------------------------------------------===//
41 static inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
42 // Determine if the value is signed or not
43 bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
44 // If its signed, extend the sign bits
46 Val |= ~ITy->getBitMask();
50 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
54 void Interpreter::initializeExecutionEngine() {
58 //===----------------------------------------------------------------------===//
59 // Binary Instruction Implementations
60 //===----------------------------------------------------------------------===//
62 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
63 case Type::TY##TyID: \
64 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
67 static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
68 GenericValue Src2, const Type *Ty) {
69 switch (Ty->getTypeID()) {
70 IMPLEMENT_BINARY_OPERATOR(+, Float);
71 IMPLEMENT_BINARY_OPERATOR(+, Double);
73 cerr << "Unhandled type for FAdd instruction: " << *Ty << "\n";
78 static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
79 GenericValue Src2, const Type *Ty) {
80 switch (Ty->getTypeID()) {
81 IMPLEMENT_BINARY_OPERATOR(-, Float);
82 IMPLEMENT_BINARY_OPERATOR(-, Double);
84 cerr << "Unhandled type for FSub instruction: " << *Ty << "\n";
89 static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
90 GenericValue Src2, const Type *Ty) {
91 switch (Ty->getTypeID()) {
92 IMPLEMENT_BINARY_OPERATOR(*, Float);
93 IMPLEMENT_BINARY_OPERATOR(*, Double);
95 cerr << "Unhandled type for FMul instruction: " << *Ty << "\n";
100 static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
101 GenericValue Src2, const Type *Ty) {
102 switch (Ty->getTypeID()) {
103 IMPLEMENT_BINARY_OPERATOR(/, Float);
104 IMPLEMENT_BINARY_OPERATOR(/, Double);
106 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
111 static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
112 GenericValue Src2, const Type *Ty) {
113 switch (Ty->getTypeID()) {
114 case Type::FloatTyID:
115 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
117 case Type::DoubleTyID:
118 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
121 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
126 #define IMPLEMENT_INTEGER_ICMP(OP, TY) \
127 case Type::IntegerTyID: \
128 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
131 // Handle pointers specially because they must be compared with only as much
132 // width as the host has. We _do not_ want to be comparing 64 bit values when
133 // running on a 32-bit target, otherwise the upper 32 bits might mess up
134 // comparisons if they contain garbage.
135 #define IMPLEMENT_POINTER_ICMP(OP) \
136 case Type::PointerTyID: \
137 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
138 (void*)(intptr_t)Src2.PointerVal); \
141 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
144 switch (Ty->getTypeID()) {
145 IMPLEMENT_INTEGER_ICMP(eq,Ty);
146 IMPLEMENT_POINTER_ICMP(==);
148 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
154 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
157 switch (Ty->getTypeID()) {
158 IMPLEMENT_INTEGER_ICMP(ne,Ty);
159 IMPLEMENT_POINTER_ICMP(!=);
161 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
167 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
170 switch (Ty->getTypeID()) {
171 IMPLEMENT_INTEGER_ICMP(ult,Ty);
172 IMPLEMENT_POINTER_ICMP(<);
174 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
180 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
183 switch (Ty->getTypeID()) {
184 IMPLEMENT_INTEGER_ICMP(slt,Ty);
185 IMPLEMENT_POINTER_ICMP(<);
187 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
193 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
196 switch (Ty->getTypeID()) {
197 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
198 IMPLEMENT_POINTER_ICMP(>);
200 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
206 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
209 switch (Ty->getTypeID()) {
210 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
211 IMPLEMENT_POINTER_ICMP(>);
213 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
219 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
222 switch (Ty->getTypeID()) {
223 IMPLEMENT_INTEGER_ICMP(ule,Ty);
224 IMPLEMENT_POINTER_ICMP(<=);
226 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
232 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
235 switch (Ty->getTypeID()) {
236 IMPLEMENT_INTEGER_ICMP(sle,Ty);
237 IMPLEMENT_POINTER_ICMP(<=);
239 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
245 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
248 switch (Ty->getTypeID()) {
249 IMPLEMENT_INTEGER_ICMP(uge,Ty);
250 IMPLEMENT_POINTER_ICMP(>=);
252 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
258 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
261 switch (Ty->getTypeID()) {
262 IMPLEMENT_INTEGER_ICMP(sge,Ty);
263 IMPLEMENT_POINTER_ICMP(>=);
265 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
271 void Interpreter::visitICmpInst(ICmpInst &I) {
272 ExecutionContext &SF = ECStack.back();
273 const Type *Ty = I.getOperand(0)->getType();
274 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
275 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
276 GenericValue R; // Result
278 switch (I.getPredicate()) {
279 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
280 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
281 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
282 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
283 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
284 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
285 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
286 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
287 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
288 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
290 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
297 #define IMPLEMENT_FCMP(OP, TY) \
298 case Type::TY##TyID: \
299 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
302 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
305 switch (Ty->getTypeID()) {
306 IMPLEMENT_FCMP(==, Float);
307 IMPLEMENT_FCMP(==, Double);
309 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
315 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
318 switch (Ty->getTypeID()) {
319 IMPLEMENT_FCMP(!=, Float);
320 IMPLEMENT_FCMP(!=, Double);
323 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
329 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
332 switch (Ty->getTypeID()) {
333 IMPLEMENT_FCMP(<=, Float);
334 IMPLEMENT_FCMP(<=, Double);
336 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
342 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
345 switch (Ty->getTypeID()) {
346 IMPLEMENT_FCMP(>=, Float);
347 IMPLEMENT_FCMP(>=, Double);
349 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
355 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
358 switch (Ty->getTypeID()) {
359 IMPLEMENT_FCMP(<, Float);
360 IMPLEMENT_FCMP(<, Double);
362 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
368 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
371 switch (Ty->getTypeID()) {
372 IMPLEMENT_FCMP(>, Float);
373 IMPLEMENT_FCMP(>, Double);
375 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
381 #define IMPLEMENT_UNORDERED(TY, X,Y) \
382 if (TY == Type::FloatTy) { \
383 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
384 Dest.IntVal = APInt(1,true); \
387 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
388 Dest.IntVal = APInt(1,true); \
393 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
396 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
397 return executeFCMP_OEQ(Src1, Src2, Ty);
400 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
403 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
404 return executeFCMP_ONE(Src1, Src2, Ty);
407 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
410 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
411 return executeFCMP_OLE(Src1, Src2, Ty);
414 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
417 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
418 return executeFCMP_OGE(Src1, Src2, Ty);
421 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
424 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
425 return executeFCMP_OLT(Src1, Src2, Ty);
428 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
431 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
432 return executeFCMP_OGT(Src1, Src2, Ty);
435 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
438 if (Ty == Type::FloatTy)
439 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
440 Src2.FloatVal == Src2.FloatVal));
442 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
443 Src2.DoubleVal == Src2.DoubleVal));
447 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
450 if (Ty == Type::FloatTy)
451 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
452 Src2.FloatVal != Src2.FloatVal));
454 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
455 Src2.DoubleVal != Src2.DoubleVal));
459 void Interpreter::visitFCmpInst(FCmpInst &I) {
460 ExecutionContext &SF = ECStack.back();
461 const Type *Ty = I.getOperand(0)->getType();
462 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
463 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
464 GenericValue R; // Result
466 switch (I.getPredicate()) {
467 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
468 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
469 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
470 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
471 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
472 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
473 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
474 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
475 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
476 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
477 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
478 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
479 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
480 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
481 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
482 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
484 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
491 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
492 GenericValue Src2, const Type *Ty) {
495 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
496 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
497 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
498 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
499 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
500 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
501 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
502 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
503 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
504 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
505 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
506 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
507 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
508 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
509 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
510 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
511 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
512 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
513 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
514 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
515 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
516 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
517 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
518 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
519 case FCmpInst::FCMP_FALSE: {
521 Result.IntVal = APInt(1, false);
524 case FCmpInst::FCMP_TRUE: {
526 Result.IntVal = APInt(1, true);
530 cerr << "Unhandled Cmp predicate\n";
535 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
536 ExecutionContext &SF = ECStack.back();
537 const Type *Ty = I.getOperand(0)->getType();
538 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
539 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
540 GenericValue R; // Result
542 switch (I.getOpcode()) {
543 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
544 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
545 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
546 case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;
547 case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;
548 case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;
549 case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;
550 case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;
551 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
552 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
553 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
554 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
555 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
556 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
557 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
559 cerr << "Don't know how to handle this binary operator!\n-->" << I;
566 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
568 return Src1.IntVal == 0 ? Src3 : Src2;
571 void Interpreter::visitSelectInst(SelectInst &I) {
572 ExecutionContext &SF = ECStack.back();
573 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
574 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
575 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
576 GenericValue R = executeSelectInst(Src1, Src2, Src3);
581 //===----------------------------------------------------------------------===//
582 // Terminator Instruction Implementations
583 //===----------------------------------------------------------------------===//
585 void Interpreter::exitCalled(GenericValue GV) {
586 // runAtExitHandlers() assumes there are no stack frames, but
587 // if exit() was called, then it had a stack frame. Blow away
588 // the stack before interpreting atexit handlers.
590 runAtExitHandlers ();
591 exit (GV.IntVal.zextOrTrunc(32).getZExtValue());
594 /// Pop the last stack frame off of ECStack and then copy the result
595 /// back into the result variable if we are not returning void. The
596 /// result variable may be the ExitValue, or the Value of the calling
597 /// CallInst if there was a previous stack frame. This method may
598 /// invalidate any ECStack iterators you have. This method also takes
599 /// care of switching to the normal destination BB, if we are returning
602 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
603 GenericValue Result) {
604 // Pop the current stack frame.
607 if (ECStack.empty()) { // Finished main. Put result into exit code...
608 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
609 ExitValue = Result; // Capture the exit value of the program
611 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
614 // If we have a previous stack frame, and we have a previous call,
615 // fill in the return value...
616 ExecutionContext &CallingSF = ECStack.back();
617 if (Instruction *I = CallingSF.Caller.getInstruction()) {
618 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
619 SetValue(I, Result, CallingSF);
620 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
621 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
622 CallingSF.Caller = CallSite(); // We returned from the call...
627 void Interpreter::visitReturnInst(ReturnInst &I) {
628 ExecutionContext &SF = ECStack.back();
629 const Type *RetTy = Type::VoidTy;
632 // Save away the return value... (if we are not 'ret void')
633 if (I.getNumOperands()) {
634 RetTy = I.getReturnValue()->getType();
635 Result = getOperandValue(I.getReturnValue(), SF);
638 popStackAndReturnValueToCaller(RetTy, Result);
641 void Interpreter::visitUnwindInst(UnwindInst &I) {
646 if (ECStack.empty ())
648 Inst = ECStack.back ().Caller.getInstruction ();
649 } while (!(Inst && isa<InvokeInst> (Inst)));
651 // Return from invoke
652 ExecutionContext &InvokingSF = ECStack.back ();
653 InvokingSF.Caller = CallSite ();
655 // Go to exceptional destination BB of invoke instruction
656 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
659 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
660 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
664 void Interpreter::visitBranchInst(BranchInst &I) {
665 ExecutionContext &SF = ECStack.back();
668 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
669 if (!I.isUnconditional()) {
670 Value *Cond = I.getCondition();
671 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
672 Dest = I.getSuccessor(1);
674 SwitchToNewBasicBlock(Dest, SF);
677 void Interpreter::visitSwitchInst(SwitchInst &I) {
678 ExecutionContext &SF = ECStack.back();
679 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
680 const Type *ElTy = I.getOperand(0)->getType();
682 // Check to see if any of the cases match...
683 BasicBlock *Dest = 0;
684 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
685 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
687 Dest = cast<BasicBlock>(I.getOperand(i+1));
691 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
692 SwitchToNewBasicBlock(Dest, SF);
695 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
696 // This function handles the actual updating of block and instruction iterators
697 // as well as execution of all of the PHI nodes in the destination block.
699 // This method does this because all of the PHI nodes must be executed
700 // atomically, reading their inputs before any of the results are updated. Not
701 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
702 // their inputs. If the input PHI node is updated before it is read, incorrect
703 // results can happen. Thus we use a two phase approach.
705 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
706 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
707 SF.CurBB = Dest; // Update CurBB to branch destination
708 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
710 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
712 // Loop over all of the PHI nodes in the current block, reading their inputs.
713 std::vector<GenericValue> ResultValues;
715 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
716 // Search for the value corresponding to this previous bb...
717 int i = PN->getBasicBlockIndex(PrevBB);
718 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
719 Value *IncomingValue = PN->getIncomingValue(i);
721 // Save the incoming value for this PHI node...
722 ResultValues.push_back(getOperandValue(IncomingValue, SF));
725 // Now loop over all of the PHI nodes setting their values...
726 SF.CurInst = SF.CurBB->begin();
727 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
728 PHINode *PN = cast<PHINode>(SF.CurInst);
729 SetValue(PN, ResultValues[i], SF);
733 //===----------------------------------------------------------------------===//
734 // Memory Instruction Implementations
735 //===----------------------------------------------------------------------===//
737 void Interpreter::visitAllocationInst(AllocationInst &I) {
738 ExecutionContext &SF = ECStack.back();
740 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
742 // Get the number of elements being allocated by the array...
743 unsigned NumElements =
744 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
746 unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
748 // Avoid malloc-ing zero bytes, use max()...
749 unsigned MemToAlloc = std::max(1U, 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 << uintptr_t(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();
803 assert(BitWidth == 64 && "Invalid index type for getelementptr");
804 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
806 Total += TD.getTypeAllocSize(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);
829 if (I.isVolatile() && PrintVolatile)
830 cerr << "Volatile load " << I;
833 void Interpreter::visitStoreInst(StoreInst &I) {
834 ExecutionContext &SF = ECStack.back();
835 GenericValue Val = getOperandValue(I.getOperand(0), SF);
836 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
837 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
838 I.getOperand(0)->getType());
839 if (I.isVolatile() && PrintVolatile)
840 cerr << "Volatile store: " << I;
843 //===----------------------------------------------------------------------===//
844 // Miscellaneous Instruction Implementations
845 //===----------------------------------------------------------------------===//
847 void Interpreter::visitCallSite(CallSite CS) {
848 ExecutionContext &SF = ECStack.back();
850 // Check to see if this is an intrinsic function call...
851 Function *F = CS.getCalledFunction();
852 if (F && F->isDeclaration ())
853 switch (F->getIntrinsicID()) {
854 case Intrinsic::not_intrinsic:
856 case Intrinsic::vastart: { // va_start
857 GenericValue ArgIndex;
858 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
859 ArgIndex.UIntPairVal.second = 0;
860 SetValue(CS.getInstruction(), ArgIndex, SF);
863 case Intrinsic::vaend: // va_end is a noop for the interpreter
865 case Intrinsic::vacopy: // va_copy: dest = src
866 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
869 // If it is an unknown intrinsic function, use the intrinsic lowering
870 // class to transform it into hopefully tasty LLVM code.
872 BasicBlock::iterator me(CS.getInstruction());
873 BasicBlock *Parent = CS.getInstruction()->getParent();
874 bool atBegin(Parent->begin() == me);
877 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
879 // Restore the CurInst pointer to the first instruction newly inserted, if
882 SF.CurInst = Parent->begin();
892 std::vector<GenericValue> ArgVals;
893 const unsigned NumArgs = SF.Caller.arg_size();
894 ArgVals.reserve(NumArgs);
896 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
897 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
899 ArgVals.push_back(getOperandValue(V, SF));
900 // Promote all integral types whose size is < sizeof(i32) into i32.
901 // We do this by zero or sign extending the value as appropriate
902 // according to the parameter attributes
903 const Type *Ty = V->getType();
904 if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32)) {
905 if (CS.paramHasAttr(pNum, Attribute::ZExt))
906 ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
907 else if (CS.paramHasAttr(pNum, Attribute::SExt))
908 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
912 // To handle indirect calls, we must get the pointer value from the argument
913 // and treat it as a function pointer.
914 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
915 callFunction((Function*)GVTOP(SRC), ArgVals);
918 void Interpreter::visitShl(BinaryOperator &I) {
919 ExecutionContext &SF = ECStack.back();
920 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
921 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
923 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
924 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
926 Dest.IntVal = Src1.IntVal;
928 SetValue(&I, Dest, SF);
931 void Interpreter::visitLShr(BinaryOperator &I) {
932 ExecutionContext &SF = ECStack.back();
933 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
934 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
936 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
937 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
939 Dest.IntVal = Src1.IntVal;
941 SetValue(&I, Dest, SF);
944 void Interpreter::visitAShr(BinaryOperator &I) {
945 ExecutionContext &SF = ECStack.back();
946 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
947 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
949 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
950 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
952 Dest.IntVal = Src1.IntVal;
954 SetValue(&I, Dest, SF);
957 GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
958 ExecutionContext &SF) {
959 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
960 const IntegerType *DITy = cast<IntegerType>(DstTy);
961 unsigned DBitWidth = DITy->getBitWidth();
962 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
966 GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
967 ExecutionContext &SF) {
968 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
969 const IntegerType *DITy = cast<IntegerType>(DstTy);
970 unsigned DBitWidth = DITy->getBitWidth();
971 Dest.IntVal = Src.IntVal.sext(DBitWidth);
975 GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
976 ExecutionContext &SF) {
977 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
978 const IntegerType *DITy = cast<IntegerType>(DstTy);
979 unsigned DBitWidth = DITy->getBitWidth();
980 Dest.IntVal = Src.IntVal.zext(DBitWidth);
984 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
985 ExecutionContext &SF) {
986 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
987 assert(SrcVal->getType() == Type::DoubleTy && DstTy == Type::FloatTy &&
988 "Invalid FPTrunc instruction");
989 Dest.FloatVal = (float) Src.DoubleVal;
993 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
994 ExecutionContext &SF) {
995 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
996 assert(SrcVal->getType() == Type::FloatTy && DstTy == Type::DoubleTy &&
997 "Invalid FPTrunc instruction");
998 Dest.DoubleVal = (double) Src.FloatVal;
1002 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
1003 ExecutionContext &SF) {
1004 const Type *SrcTy = SrcVal->getType();
1005 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1006 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1007 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
1009 if (SrcTy->getTypeID() == Type::FloatTyID)
1010 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1012 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1016 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1017 ExecutionContext &SF) {
1018 const Type *SrcTy = SrcVal->getType();
1019 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1020 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1021 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1023 if (SrcTy->getTypeID() == Type::FloatTyID)
1024 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1026 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1030 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1031 ExecutionContext &SF) {
1032 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1033 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1035 if (DstTy->getTypeID() == Type::FloatTyID)
1036 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
1038 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
1042 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1043 ExecutionContext &SF) {
1044 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1045 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
1047 if (DstTy->getTypeID() == Type::FloatTyID)
1048 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
1050 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
1055 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1056 ExecutionContext &SF) {
1057 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1058 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1059 assert(isa<PointerType>(SrcVal->getType()) && "Invalid PtrToInt instruction");
1061 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1065 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1066 ExecutionContext &SF) {
1067 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1068 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1070 uint32_t PtrSize = TD.getPointerSizeInBits();
1071 if (PtrSize != Src.IntVal.getBitWidth())
1072 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
1074 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
1078 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1079 ExecutionContext &SF) {
1081 const Type *SrcTy = SrcVal->getType();
1082 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1083 if (isa<PointerType>(DstTy)) {
1084 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1085 Dest.PointerVal = Src.PointerVal;
1086 } else if (DstTy->isInteger()) {
1087 if (SrcTy == Type::FloatTy) {
1088 Dest.IntVal.zext(sizeof(Src.FloatVal) * CHAR_BIT);
1089 Dest.IntVal.floatToBits(Src.FloatVal);
1090 } else if (SrcTy == Type::DoubleTy) {
1091 Dest.IntVal.zext(sizeof(Src.DoubleVal) * CHAR_BIT);
1092 Dest.IntVal.doubleToBits(Src.DoubleVal);
1093 } else if (SrcTy->isInteger()) {
1094 Dest.IntVal = Src.IntVal;
1096 assert(0 && "Invalid BitCast");
1097 } else if (DstTy == Type::FloatTy) {
1098 if (SrcTy->isInteger())
1099 Dest.FloatVal = Src.IntVal.bitsToFloat();
1101 Dest.FloatVal = Src.FloatVal;
1102 } else if (DstTy == Type::DoubleTy) {
1103 if (SrcTy->isInteger())
1104 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1106 Dest.DoubleVal = Src.DoubleVal;
1108 assert(0 && "Invalid Bitcast");
1113 void Interpreter::visitTruncInst(TruncInst &I) {
1114 ExecutionContext &SF = ECStack.back();
1115 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1118 void Interpreter::visitSExtInst(SExtInst &I) {
1119 ExecutionContext &SF = ECStack.back();
1120 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1123 void Interpreter::visitZExtInst(ZExtInst &I) {
1124 ExecutionContext &SF = ECStack.back();
1125 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1128 void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1129 ExecutionContext &SF = ECStack.back();
1130 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1133 void Interpreter::visitFPExtInst(FPExtInst &I) {
1134 ExecutionContext &SF = ECStack.back();
1135 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1138 void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1139 ExecutionContext &SF = ECStack.back();
1140 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1143 void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1144 ExecutionContext &SF = ECStack.back();
1145 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1148 void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1149 ExecutionContext &SF = ECStack.back();
1150 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1153 void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1154 ExecutionContext &SF = ECStack.back();
1155 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1158 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1159 ExecutionContext &SF = ECStack.back();
1160 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1163 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1164 ExecutionContext &SF = ECStack.back();
1165 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1168 void Interpreter::visitBitCastInst(BitCastInst &I) {
1169 ExecutionContext &SF = ECStack.back();
1170 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1173 #define IMPLEMENT_VAARG(TY) \
1174 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1176 void Interpreter::visitVAArgInst(VAArgInst &I) {
1177 ExecutionContext &SF = ECStack.back();
1179 // Get the incoming valist parameter. LLI treats the valist as a
1180 // (ec-stack-depth var-arg-index) pair.
1181 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1183 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1184 .VarArgs[VAList.UIntPairVal.second];
1185 const Type *Ty = I.getType();
1186 switch (Ty->getTypeID()) {
1187 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
1188 IMPLEMENT_VAARG(Pointer);
1189 IMPLEMENT_VAARG(Float);
1190 IMPLEMENT_VAARG(Double);
1192 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1196 // Set the Value of this Instruction.
1197 SetValue(&I, Dest, SF);
1199 // Move the pointer to the next vararg.
1200 ++VAList.UIntPairVal.second;
1203 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1204 ExecutionContext &SF) {
1205 switch (CE->getOpcode()) {
1206 case Instruction::Trunc:
1207 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1208 case Instruction::ZExt:
1209 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1210 case Instruction::SExt:
1211 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1212 case Instruction::FPTrunc:
1213 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1214 case Instruction::FPExt:
1215 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1216 case Instruction::UIToFP:
1217 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1218 case Instruction::SIToFP:
1219 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1220 case Instruction::FPToUI:
1221 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1222 case Instruction::FPToSI:
1223 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1224 case Instruction::PtrToInt:
1225 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1226 case Instruction::IntToPtr:
1227 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1228 case Instruction::BitCast:
1229 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1230 case Instruction::GetElementPtr:
1231 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1232 gep_type_end(CE), SF);
1233 case Instruction::FCmp:
1234 case Instruction::ICmp:
1235 return executeCmpInst(CE->getPredicate(),
1236 getOperandValue(CE->getOperand(0), SF),
1237 getOperandValue(CE->getOperand(1), SF),
1238 CE->getOperand(0)->getType());
1239 case Instruction::Select:
1240 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1241 getOperandValue(CE->getOperand(1), SF),
1242 getOperandValue(CE->getOperand(2), SF));
1247 // The cases below here require a GenericValue parameter for the result
1248 // so we initialize one, compute it and then return it.
1249 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1250 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
1252 const Type * Ty = CE->getOperand(0)->getType();
1253 switch (CE->getOpcode()) {
1254 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
1255 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
1256 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
1257 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
1258 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
1259 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
1260 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1261 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1262 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1263 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1264 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1265 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
1266 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
1267 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
1268 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
1269 case Instruction::Shl:
1270 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1272 case Instruction::LShr:
1273 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1275 case Instruction::AShr:
1276 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1279 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
1281 return GenericValue();
1286 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1287 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1288 return getConstantExprValue(CE, SF);
1289 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1290 return getConstantValue(CPV);
1291 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1292 return PTOGV(getPointerToGlobal(GV));
1294 return SF.Values[V];
1298 //===----------------------------------------------------------------------===//
1299 // Dispatch and Execution Code
1300 //===----------------------------------------------------------------------===//
1302 //===----------------------------------------------------------------------===//
1303 // callFunction - Execute the specified function...
1305 void Interpreter::callFunction(Function *F,
1306 const std::vector<GenericValue> &ArgVals) {
1307 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1308 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1309 "Incorrect number of arguments passed into function call!");
1310 // Make a new stack frame... and fill it in.
1311 ECStack.push_back(ExecutionContext());
1312 ExecutionContext &StackFrame = ECStack.back();
1313 StackFrame.CurFunction = F;
1315 // Special handling for external functions.
1316 if (F->isDeclaration()) {
1317 GenericValue Result = callExternalFunction (F, ArgVals);
1318 // Simulate a 'ret' instruction of the appropriate type.
1319 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1323 // Get pointers to first LLVM BB & Instruction in function.
1324 StackFrame.CurBB = F->begin();
1325 StackFrame.CurInst = StackFrame.CurBB->begin();
1327 // Run through the function arguments and initialize their values...
1328 assert((ArgVals.size() == F->arg_size() ||
1329 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1330 "Invalid number of values passed to function invocation!");
1332 // Handle non-varargs arguments...
1334 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1336 SetValue(AI, ArgVals[i], StackFrame);
1338 // Handle varargs arguments...
1339 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1343 void Interpreter::run() {
1344 while (!ECStack.empty()) {
1345 // Interpret a single instruction & increment the "PC".
1346 ExecutionContext &SF = ECStack.back(); // Current stack frame
1347 Instruction &I = *SF.CurInst++; // Increment before execute
1349 // Track the number of dynamic instructions executed.
1352 DOUT << "About to interpret: " << I;
1353 visit(I); // Dispatch to one of the visit* methods...
1355 // This is not safe, as visiting the instruction could lower it and free I.
1357 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1358 I.getType() != Type::VoidTy) {
1360 const GenericValue &Val = SF.Values[&I];
1361 switch (I.getType()->getTypeID()) {
1362 default: assert(0 && "Invalid GenericValue Type");
1363 case Type::VoidTyID: DOUT << "void"; break;
1364 case Type::FloatTyID: DOUT << "float " << Val.FloatVal; break;
1365 case Type::DoubleTyID: DOUT << "double " << Val.DoubleVal; break;
1366 case Type::PointerTyID: DOUT << "void* " << intptr_t(Val.PointerVal);
1368 case Type::IntegerTyID:
1369 DOUT << "i" << Val.IntVal.getBitWidth() << " "
1370 << Val.IntVal.toStringUnsigned(10)
1371 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";