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/ParameterAttributes.h"
20 #include "llvm/CodeGen/IntrinsicLowering.h"
21 #include "llvm/Support/GetElementPtrTypeIterator.h"
22 #include "llvm/ADT/APInt.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/MathExtras.h"
30 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
31 static Interpreter *TheEE = 0;
33 //===----------------------------------------------------------------------===//
34 // Various Helper Functions
35 //===----------------------------------------------------------------------===//
37 static inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
38 // Determine if the value is signed or not
39 bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
40 // If its signed, extend the sign bits
42 Val |= ~ITy->getBitMask();
46 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
50 void Interpreter::initializeExecutionEngine() {
54 //===----------------------------------------------------------------------===//
55 // Binary Instruction Implementations
56 //===----------------------------------------------------------------------===//
58 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
59 case Type::TY##TyID: \
60 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
63 #define IMPLEMENT_INTEGER_BINOP1(OP, TY) \
64 case Type::IntegerTyID: { \
65 Dest.IntVal = Src1.IntVal OP Src2.IntVal; \
70 static void executeAddInst(GenericValue &Dest, GenericValue Src1,
71 GenericValue Src2, const Type *Ty) {
72 switch (Ty->getTypeID()) {
73 IMPLEMENT_INTEGER_BINOP1(+, Ty);
74 IMPLEMENT_BINARY_OPERATOR(+, Float);
75 IMPLEMENT_BINARY_OPERATOR(+, Double);
77 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
82 static void executeSubInst(GenericValue &Dest, GenericValue Src1,
83 GenericValue Src2, const Type *Ty) {
84 switch (Ty->getTypeID()) {
85 IMPLEMENT_INTEGER_BINOP1(-, Ty);
86 IMPLEMENT_BINARY_OPERATOR(-, Float);
87 IMPLEMENT_BINARY_OPERATOR(-, Double);
89 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
94 static void executeMulInst(GenericValue &Dest, GenericValue Src1,
95 GenericValue Src2, const Type *Ty) {
96 switch (Ty->getTypeID()) {
97 IMPLEMENT_INTEGER_BINOP1(*, Ty);
98 IMPLEMENT_BINARY_OPERATOR(*, Float);
99 IMPLEMENT_BINARY_OPERATOR(*, Double);
101 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
106 static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
107 GenericValue Src2, const Type *Ty) {
108 switch (Ty->getTypeID()) {
109 IMPLEMENT_BINARY_OPERATOR(/, Float);
110 IMPLEMENT_BINARY_OPERATOR(/, Double);
112 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
117 static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
118 GenericValue Src2, const Type *Ty) {
119 switch (Ty->getTypeID()) {
120 case Type::FloatTyID:
121 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
123 case Type::DoubleTyID:
124 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
127 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
132 #define IMPLEMENT_INTEGER_ICMP(OP, TY) \
133 case Type::IntegerTyID: \
134 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
137 // Handle pointers specially because they must be compared with only as much
138 // width as the host has. We _do not_ want to be comparing 64 bit values when
139 // running on a 32-bit target, otherwise the upper 32 bits might mess up
140 // comparisons if they contain garbage.
141 #define IMPLEMENT_POINTER_ICMP(OP) \
142 case Type::PointerTyID: \
143 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
144 (void*)(intptr_t)Src2.PointerVal); \
147 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
150 switch (Ty->getTypeID()) {
151 IMPLEMENT_INTEGER_ICMP(eq,Ty);
152 IMPLEMENT_POINTER_ICMP(==);
154 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
160 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
163 switch (Ty->getTypeID()) {
164 IMPLEMENT_INTEGER_ICMP(ne,Ty);
165 IMPLEMENT_POINTER_ICMP(!=);
167 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
173 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
176 switch (Ty->getTypeID()) {
177 IMPLEMENT_INTEGER_ICMP(ult,Ty);
178 IMPLEMENT_POINTER_ICMP(<);
180 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
186 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
189 switch (Ty->getTypeID()) {
190 IMPLEMENT_INTEGER_ICMP(slt,Ty);
191 IMPLEMENT_POINTER_ICMP(<);
193 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
199 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
202 switch (Ty->getTypeID()) {
203 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
204 IMPLEMENT_POINTER_ICMP(>);
206 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
212 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
215 switch (Ty->getTypeID()) {
216 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
217 IMPLEMENT_POINTER_ICMP(>);
219 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
225 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
228 switch (Ty->getTypeID()) {
229 IMPLEMENT_INTEGER_ICMP(ule,Ty);
230 IMPLEMENT_POINTER_ICMP(<=);
232 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
238 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
241 switch (Ty->getTypeID()) {
242 IMPLEMENT_INTEGER_ICMP(sle,Ty);
243 IMPLEMENT_POINTER_ICMP(<=);
245 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
251 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
254 switch (Ty->getTypeID()) {
255 IMPLEMENT_INTEGER_ICMP(uge,Ty);
256 IMPLEMENT_POINTER_ICMP(>=);
258 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
264 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
267 switch (Ty->getTypeID()) {
268 IMPLEMENT_INTEGER_ICMP(sge,Ty);
269 IMPLEMENT_POINTER_ICMP(>=);
271 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
277 void Interpreter::visitICmpInst(ICmpInst &I) {
278 ExecutionContext &SF = ECStack.back();
279 const Type *Ty = I.getOperand(0)->getType();
280 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
281 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
282 GenericValue R; // Result
284 switch (I.getPredicate()) {
285 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
286 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
287 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
288 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
289 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
290 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
291 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
292 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
293 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
294 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
296 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
303 #define IMPLEMENT_FCMP(OP, TY) \
304 case Type::TY##TyID: \
305 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
308 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
311 switch (Ty->getTypeID()) {
312 IMPLEMENT_FCMP(==, Float);
313 IMPLEMENT_FCMP(==, Double);
315 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
321 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
324 switch (Ty->getTypeID()) {
325 IMPLEMENT_FCMP(!=, Float);
326 IMPLEMENT_FCMP(!=, Double);
329 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
335 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
338 switch (Ty->getTypeID()) {
339 IMPLEMENT_FCMP(<=, Float);
340 IMPLEMENT_FCMP(<=, Double);
342 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
348 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
351 switch (Ty->getTypeID()) {
352 IMPLEMENT_FCMP(>=, Float);
353 IMPLEMENT_FCMP(>=, Double);
355 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
361 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
364 switch (Ty->getTypeID()) {
365 IMPLEMENT_FCMP(<, Float);
366 IMPLEMENT_FCMP(<, Double);
368 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
374 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
377 switch (Ty->getTypeID()) {
378 IMPLEMENT_FCMP(>, Float);
379 IMPLEMENT_FCMP(>, Double);
381 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
387 #define IMPLEMENT_UNORDERED(TY, X,Y) \
388 if (TY == Type::FloatTy) \
389 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
390 Dest.IntVal = APInt(1,true); \
393 else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
394 Dest.IntVal = APInt(1,true); \
399 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
402 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
403 return executeFCMP_OEQ(Src1, Src2, Ty);
406 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
409 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
410 return executeFCMP_ONE(Src1, Src2, Ty);
413 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
416 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
417 return executeFCMP_OLE(Src1, Src2, Ty);
420 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
423 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
424 return executeFCMP_OGE(Src1, Src2, Ty);
427 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
430 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
431 return executeFCMP_OLT(Src1, Src2, Ty);
434 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
437 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
438 return executeFCMP_OGT(Src1, Src2, Ty);
441 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
444 if (Ty == Type::FloatTy)
445 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
446 Src2.FloatVal == Src2.FloatVal));
448 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
449 Src2.DoubleVal == Src2.DoubleVal));
453 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
456 if (Ty == Type::FloatTy)
457 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
458 Src2.FloatVal != Src2.FloatVal));
460 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
461 Src2.DoubleVal != Src2.DoubleVal));
465 void Interpreter::visitFCmpInst(FCmpInst &I) {
466 ExecutionContext &SF = ECStack.back();
467 const Type *Ty = I.getOperand(0)->getType();
468 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
469 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
470 GenericValue R; // Result
472 switch (I.getPredicate()) {
473 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
474 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
475 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
476 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
477 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
478 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
479 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
480 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
481 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
482 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
483 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
484 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
485 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
486 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
487 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
488 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
490 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
497 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
498 GenericValue Src2, const Type *Ty) {
501 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
502 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
503 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
504 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
505 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
506 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
507 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
508 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
509 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
510 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
511 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
512 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
513 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
514 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
515 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
516 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
517 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
518 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
519 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
520 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
521 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
522 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
523 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
524 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
525 case FCmpInst::FCMP_FALSE: {
527 Result.IntVal = APInt(1, false);
530 case FCmpInst::FCMP_TRUE: {
532 Result.IntVal = APInt(1, true);
536 cerr << "Unhandled Cmp predicate\n";
541 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
542 ExecutionContext &SF = ECStack.back();
543 const Type *Ty = I.getOperand(0)->getType();
544 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
545 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
546 GenericValue R; // Result
548 switch (I.getOpcode()) {
549 case Instruction::Add: executeAddInst (R, Src1, Src2, Ty); break;
550 case Instruction::Sub: executeSubInst (R, Src1, Src2, Ty); break;
551 case Instruction::Mul: executeMulInst (R, Src1, Src2, Ty); break;
552 case Instruction::FDiv: executeFDivInst (R, Src1, Src2, Ty); break;
553 case Instruction::FRem: executeFRemInst (R, Src1, Src2, Ty); break;
554 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
555 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
556 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
557 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
558 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
559 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
560 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
562 cerr << "Don't know how to handle this binary operator!\n-->" << I;
569 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
571 return Src1.IntVal == 0 ? Src3 : Src2;
574 void Interpreter::visitSelectInst(SelectInst &I) {
575 ExecutionContext &SF = ECStack.back();
576 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
577 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
578 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
579 GenericValue R = executeSelectInst(Src1, Src2, Src3);
584 //===----------------------------------------------------------------------===//
585 // Terminator Instruction Implementations
586 //===----------------------------------------------------------------------===//
588 void Interpreter::exitCalled(GenericValue GV) {
589 // runAtExitHandlers() assumes there are no stack frames, but
590 // if exit() was called, then it had a stack frame. Blow away
591 // the stack before interpreting atexit handlers.
593 runAtExitHandlers ();
594 exit (GV.IntVal.zextOrTrunc(32).getZExtValue());
597 /// Pop the last stack frame off of ECStack and then copy the result
598 /// back into the result variable if we are not returning void. The
599 /// result variable may be the ExitValue, or the Value of the calling
600 /// CallInst if there was a previous stack frame. This method may
601 /// invalidate any ECStack iterators you have. This method also takes
602 /// care of switching to the normal destination BB, if we are returning
605 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
606 GenericValue Result) {
607 // Pop the current stack frame.
610 if (ECStack.empty()) { // Finished main. Put result into exit code...
611 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
612 ExitValue = Result; // Capture the exit value of the program
614 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
617 // If we have a previous stack frame, and we have a previous call,
618 // fill in the return value...
619 ExecutionContext &CallingSF = ECStack.back();
620 if (Instruction *I = CallingSF.Caller.getInstruction()) {
621 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
622 SetValue(I, Result, CallingSF);
623 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
624 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
625 CallingSF.Caller = CallSite(); // We returned from the call...
630 void Interpreter::visitReturnInst(ReturnInst &I) {
631 ExecutionContext &SF = ECStack.back();
632 const Type *RetTy = Type::VoidTy;
635 // Save away the return value... (if we are not 'ret void')
636 if (I.getNumOperands()) {
637 RetTy = I.getReturnValue()->getType();
638 Result = getOperandValue(I.getReturnValue(), SF);
641 popStackAndReturnValueToCaller(RetTy, Result);
644 void Interpreter::visitUnwindInst(UnwindInst &I) {
649 if (ECStack.empty ())
651 Inst = ECStack.back ().Caller.getInstruction ();
652 } while (!(Inst && isa<InvokeInst> (Inst)));
654 // Return from invoke
655 ExecutionContext &InvokingSF = ECStack.back ();
656 InvokingSF.Caller = CallSite ();
658 // Go to exceptional destination BB of invoke instruction
659 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
662 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
663 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
667 void Interpreter::visitBranchInst(BranchInst &I) {
668 ExecutionContext &SF = ECStack.back();
671 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
672 if (!I.isUnconditional()) {
673 Value *Cond = I.getCondition();
674 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
675 Dest = I.getSuccessor(1);
677 SwitchToNewBasicBlock(Dest, SF);
680 void Interpreter::visitSwitchInst(SwitchInst &I) {
681 ExecutionContext &SF = ECStack.back();
682 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
683 const Type *ElTy = I.getOperand(0)->getType();
685 // Check to see if any of the cases match...
686 BasicBlock *Dest = 0;
687 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
688 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
690 Dest = cast<BasicBlock>(I.getOperand(i+1));
694 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
695 SwitchToNewBasicBlock(Dest, SF);
698 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
699 // This function handles the actual updating of block and instruction iterators
700 // as well as execution of all of the PHI nodes in the destination block.
702 // This method does this because all of the PHI nodes must be executed
703 // atomically, reading their inputs before any of the results are updated. Not
704 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
705 // their inputs. If the input PHI node is updated before it is read, incorrect
706 // results can happen. Thus we use a two phase approach.
708 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
709 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
710 SF.CurBB = Dest; // Update CurBB to branch destination
711 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
713 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
715 // Loop over all of the PHI nodes in the current block, reading their inputs.
716 std::vector<GenericValue> ResultValues;
718 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
719 // Search for the value corresponding to this previous bb...
720 int i = PN->getBasicBlockIndex(PrevBB);
721 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
722 Value *IncomingValue = PN->getIncomingValue(i);
724 // Save the incoming value for this PHI node...
725 ResultValues.push_back(getOperandValue(IncomingValue, SF));
728 // Now loop over all of the PHI nodes setting their values...
729 SF.CurInst = SF.CurBB->begin();
730 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
731 PHINode *PN = cast<PHINode>(SF.CurInst);
732 SetValue(PN, ResultValues[i], SF);
736 //===----------------------------------------------------------------------===//
737 // Memory Instruction Implementations
738 //===----------------------------------------------------------------------===//
740 void Interpreter::visitAllocationInst(AllocationInst &I) {
741 ExecutionContext &SF = ECStack.back();
743 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
745 // Get the number of elements being allocated by the array...
746 unsigned NumElements =
747 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
749 unsigned TypeSize = (size_t)TD.getABITypeSize(Ty);
751 // Avoid malloc-ing zero bytes, use max()...
752 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
754 // Allocate enough memory to hold the type...
755 void *Memory = malloc(MemToAlloc);
757 DOUT << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
758 << NumElements << " (Total: " << MemToAlloc << ") at "
759 << uintptr_t(Memory) << '\n';
761 GenericValue Result = PTOGV(Memory);
762 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
763 SetValue(&I, Result, SF);
765 if (I.getOpcode() == Instruction::Alloca)
766 ECStack.back().Allocas.add(Memory);
769 void Interpreter::visitFreeInst(FreeInst &I) {
770 ExecutionContext &SF = ECStack.back();
771 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
772 GenericValue Value = getOperandValue(I.getOperand(0), SF);
773 // TODO: Check to make sure memory is allocated
774 free(GVTOP(Value)); // Free memory
777 // getElementOffset - The workhorse for getelementptr.
779 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
781 ExecutionContext &SF) {
782 assert(isa<PointerType>(Ptr->getType()) &&
783 "Cannot getElementOffset of a nonpointer type!");
787 for (; I != E; ++I) {
788 if (const StructType *STy = dyn_cast<StructType>(*I)) {
789 const StructLayout *SLO = TD.getStructLayout(STy);
791 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
792 unsigned Index = unsigned(CPU->getZExtValue());
794 Total += SLO->getElementOffset(Index);
796 const SequentialType *ST = cast<SequentialType>(*I);
797 // Get the index number for the array... which must be long type...
798 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
802 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
804 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
805 else if (BitWidth == 64)
806 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
808 assert(0 && "Invalid index type for getelementptr");
809 Total += TD.getABITypeSize(ST->getElementType())*Idx;
814 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
815 DOUT << "GEP Index " << Total << " bytes.\n";
819 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
820 ExecutionContext &SF = ECStack.back();
821 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
822 gep_type_begin(I), gep_type_end(I), SF), SF);
825 void Interpreter::visitLoadInst(LoadInst &I) {
826 ExecutionContext &SF = ECStack.back();
827 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
828 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
830 LoadValueFromMemory(Result, Ptr, I.getType());
831 SetValue(&I, Result, SF);
834 void Interpreter::visitStoreInst(StoreInst &I) {
835 ExecutionContext &SF = ECStack.back();
836 GenericValue Val = getOperandValue(I.getOperand(0), SF);
837 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
838 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
839 I.getOperand(0)->getType());
842 //===----------------------------------------------------------------------===//
843 // Miscellaneous Instruction Implementations
844 //===----------------------------------------------------------------------===//
846 void Interpreter::visitCallSite(CallSite CS) {
847 ExecutionContext &SF = ECStack.back();
849 // Check to see if this is an intrinsic function call...
850 Function *F = CS.getCalledFunction();
851 if (F && F->isDeclaration ())
852 switch (F->getIntrinsicID()) {
853 case Intrinsic::not_intrinsic:
855 case Intrinsic::vastart: { // va_start
856 GenericValue ArgIndex;
857 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
858 ArgIndex.UIntPairVal.second = 0;
859 SetValue(CS.getInstruction(), ArgIndex, SF);
862 case Intrinsic::vaend: // va_end is a noop for the interpreter
864 case Intrinsic::vacopy: // va_copy: dest = src
865 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
868 // If it is an unknown intrinsic function, use the intrinsic lowering
869 // class to transform it into hopefully tasty LLVM code.
871 BasicBlock::iterator me(CS.getInstruction());
872 BasicBlock *Parent = CS.getInstruction()->getParent();
873 bool atBegin(Parent->begin() == me);
876 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
878 // Restore the CurInst pointer to the first instruction newly inserted, if
881 SF.CurInst = Parent->begin();
891 std::vector<GenericValue> ArgVals;
892 const unsigned NumArgs = SF.Caller.arg_size();
893 ArgVals.reserve(NumArgs);
895 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
896 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
898 ArgVals.push_back(getOperandValue(V, SF));
899 // Promote all integral types whose size is < sizeof(i32) into i32.
900 // We do this by zero or sign extending the value as appropriate
901 // according to the parameter attributes
902 const Type *Ty = V->getType();
903 if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32))
904 if (CS.paramHasAttr(pNum, ParamAttr::ZExt))
905 ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
906 else if (CS.paramHasAttr(pNum, ParamAttr::SExt))
907 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
910 // To handle indirect calls, we must get the pointer value from the argument
911 // and treat it as a function pointer.
912 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
913 callFunction((Function*)GVTOP(SRC), ArgVals);
916 void Interpreter::visitShl(BinaryOperator &I) {
917 ExecutionContext &SF = ECStack.back();
918 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
919 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
921 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
922 SetValue(&I, Dest, SF);
925 void Interpreter::visitLShr(BinaryOperator &I) {
926 ExecutionContext &SF = ECStack.back();
927 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
928 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
930 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
931 SetValue(&I, Dest, SF);
934 void Interpreter::visitAShr(BinaryOperator &I) {
935 ExecutionContext &SF = ECStack.back();
936 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
937 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
939 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
940 SetValue(&I, Dest, SF);
943 GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
944 ExecutionContext &SF) {
945 const Type *SrcTy = SrcVal->getType();
946 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
947 const IntegerType *DITy = cast<IntegerType>(DstTy);
948 const IntegerType *SITy = cast<IntegerType>(SrcTy);
949 unsigned DBitWidth = DITy->getBitWidth();
950 unsigned SBitWidth = SITy->getBitWidth();
951 assert(SBitWidth > DBitWidth && "Invalid truncate");
952 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
956 GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
957 ExecutionContext &SF) {
958 const Type *SrcTy = SrcVal->getType();
959 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
960 const IntegerType *DITy = cast<IntegerType>(DstTy);
961 const IntegerType *SITy = cast<IntegerType>(SrcTy);
962 unsigned DBitWidth = DITy->getBitWidth();
963 unsigned SBitWidth = SITy->getBitWidth();
964 assert(SBitWidth < DBitWidth && "Invalid sign extend");
965 Dest.IntVal = Src.IntVal.sext(DBitWidth);
969 GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
970 ExecutionContext &SF) {
971 const Type *SrcTy = SrcVal->getType();
972 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
973 const IntegerType *DITy = cast<IntegerType>(DstTy);
974 const IntegerType *SITy = cast<IntegerType>(SrcTy);
975 unsigned DBitWidth = DITy->getBitWidth();
976 unsigned SBitWidth = SITy->getBitWidth();
977 assert(SBitWidth < DBitWidth && "Invalid sign extend");
978 Dest.IntVal = Src.IntVal.zext(DBitWidth);
982 GenericValue Interpreter::executeFPTruncInst(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::DoubleTy && DstTy == Type::FloatTy &&
987 "Invalid FPTrunc instruction");
988 Dest.FloatVal = (float) Src.DoubleVal;
992 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
993 ExecutionContext &SF) {
994 const Type *SrcTy = SrcVal->getType();
995 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
996 assert(SrcTy == 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 const Type *SrcTy = SrcVal->getType();
1058 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1059 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1060 assert(isa<PointerType>(SrcTy) && "Invalid PtrToInt instruction");
1062 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1066 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1067 ExecutionContext &SF) {
1068 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1069 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1071 uint32_t PtrSize = TD.getPointerSizeInBits();
1072 if (PtrSize != Src.IntVal.getBitWidth())
1073 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
1075 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
1079 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1080 ExecutionContext &SF) {
1082 const Type *SrcTy = SrcVal->getType();
1083 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1084 if (isa<PointerType>(DstTy)) {
1085 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1086 Dest.PointerVal = Src.PointerVal;
1087 } else if (DstTy->isInteger()) {
1088 if (SrcTy == Type::FloatTy) {
1089 Dest.IntVal.zext(sizeof(Src.FloatVal) * 8);
1090 Dest.IntVal.floatToBits(Src.FloatVal);
1091 } else if (SrcTy == Type::DoubleTy) {
1092 Dest.IntVal.zext(sizeof(Src.DoubleVal) * 8);
1093 Dest.IntVal.doubleToBits(Src.DoubleVal);
1094 } else if (SrcTy->isInteger()) {
1095 Dest.IntVal = Src.IntVal;
1097 assert(0 && "Invalid BitCast");
1098 } else if (DstTy == Type::FloatTy) {
1099 if (SrcTy->isInteger())
1100 Dest.FloatVal = Src.IntVal.bitsToFloat();
1102 Dest.FloatVal = Src.FloatVal;
1103 } else if (DstTy == Type::DoubleTy) {
1104 if (SrcTy->isInteger())
1105 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1107 Dest.DoubleVal = Src.DoubleVal;
1109 assert(0 && "Invalid Bitcast");
1114 void Interpreter::visitTruncInst(TruncInst &I) {
1115 ExecutionContext &SF = ECStack.back();
1116 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1119 void Interpreter::visitSExtInst(SExtInst &I) {
1120 ExecutionContext &SF = ECStack.back();
1121 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1124 void Interpreter::visitZExtInst(ZExtInst &I) {
1125 ExecutionContext &SF = ECStack.back();
1126 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1129 void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1130 ExecutionContext &SF = ECStack.back();
1131 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1134 void Interpreter::visitFPExtInst(FPExtInst &I) {
1135 ExecutionContext &SF = ECStack.back();
1136 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1139 void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1140 ExecutionContext &SF = ECStack.back();
1141 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1144 void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1145 ExecutionContext &SF = ECStack.back();
1146 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1149 void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1150 ExecutionContext &SF = ECStack.back();
1151 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1154 void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1155 ExecutionContext &SF = ECStack.back();
1156 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1159 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1160 ExecutionContext &SF = ECStack.back();
1161 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1164 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1165 ExecutionContext &SF = ECStack.back();
1166 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1169 void Interpreter::visitBitCastInst(BitCastInst &I) {
1170 ExecutionContext &SF = ECStack.back();
1171 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1174 #define IMPLEMENT_VAARG(TY) \
1175 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1177 void Interpreter::visitVAArgInst(VAArgInst &I) {
1178 ExecutionContext &SF = ECStack.back();
1180 // Get the incoming valist parameter. LLI treats the valist as a
1181 // (ec-stack-depth var-arg-index) pair.
1182 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1184 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1185 .VarArgs[VAList.UIntPairVal.second];
1186 const Type *Ty = I.getType();
1187 switch (Ty->getTypeID()) {
1188 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
1189 IMPLEMENT_VAARG(Pointer);
1190 IMPLEMENT_VAARG(Float);
1191 IMPLEMENT_VAARG(Double);
1193 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1197 // Set the Value of this Instruction.
1198 SetValue(&I, Dest, SF);
1200 // Move the pointer to the next vararg.
1201 ++VAList.UIntPairVal.second;
1204 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1205 ExecutionContext &SF) {
1206 switch (CE->getOpcode()) {
1207 case Instruction::Trunc:
1208 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1209 case Instruction::ZExt:
1210 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1211 case Instruction::SExt:
1212 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1213 case Instruction::FPTrunc:
1214 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1215 case Instruction::FPExt:
1216 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1217 case Instruction::UIToFP:
1218 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1219 case Instruction::SIToFP:
1220 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1221 case Instruction::FPToUI:
1222 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1223 case Instruction::FPToSI:
1224 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1225 case Instruction::PtrToInt:
1226 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1227 case Instruction::IntToPtr:
1228 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1229 case Instruction::BitCast:
1230 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1231 case Instruction::GetElementPtr:
1232 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1233 gep_type_end(CE), SF);
1234 case Instruction::FCmp:
1235 case Instruction::ICmp:
1236 return executeCmpInst(CE->getPredicate(),
1237 getOperandValue(CE->getOperand(0), SF),
1238 getOperandValue(CE->getOperand(1), SF),
1239 CE->getOperand(0)->getType());
1240 case Instruction::Select:
1241 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1242 getOperandValue(CE->getOperand(1), SF),
1243 getOperandValue(CE->getOperand(2), SF));
1248 // The cases below here require a GenericValue parameter for the result
1249 // so we initialize one, compute it and then return it.
1250 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1251 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
1253 const Type * Ty = CE->getOperand(0)->getType();
1254 switch (CE->getOpcode()) {
1255 case Instruction::Add: executeAddInst (Dest, Op0, Op1, Ty); break;
1256 case Instruction::Sub: executeSubInst (Dest, Op0, Op1, Ty); break;
1257 case Instruction::Mul: executeMulInst (Dest, Op0, Op1, Ty); break;
1258 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1259 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1260 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1261 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1262 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1263 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
1264 case Instruction::And: Dest.IntVal = Op0.IntVal.And(Op1.IntVal); break;
1265 case Instruction::Or: Dest.IntVal = Op0.IntVal.Or(Op1.IntVal); break;
1266 case Instruction::Xor: Dest.IntVal = Op0.IntVal.Xor(Op1.IntVal); break;
1267 case Instruction::Shl:
1268 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1270 case Instruction::LShr:
1271 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1273 case Instruction::AShr:
1274 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1277 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
1279 return GenericValue();
1284 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1285 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1286 return getConstantExprValue(CE, SF);
1287 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1288 return getConstantValue(CPV);
1289 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1290 return PTOGV(getPointerToGlobal(GV));
1292 return SF.Values[V];
1296 //===----------------------------------------------------------------------===//
1297 // Dispatch and Execution Code
1298 //===----------------------------------------------------------------------===//
1300 //===----------------------------------------------------------------------===//
1301 // callFunction - Execute the specified function...
1303 void Interpreter::callFunction(Function *F,
1304 const std::vector<GenericValue> &ArgVals) {
1305 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1306 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1307 "Incorrect number of arguments passed into function call!");
1308 // Make a new stack frame... and fill it in.
1309 ECStack.push_back(ExecutionContext());
1310 ExecutionContext &StackFrame = ECStack.back();
1311 StackFrame.CurFunction = F;
1313 // Special handling for external functions.
1314 if (F->isDeclaration()) {
1315 GenericValue Result = callExternalFunction (F, ArgVals);
1316 // Simulate a 'ret' instruction of the appropriate type.
1317 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1321 // Get pointers to first LLVM BB & Instruction in function.
1322 StackFrame.CurBB = F->begin();
1323 StackFrame.CurInst = StackFrame.CurBB->begin();
1325 // Run through the function arguments and initialize their values...
1326 assert((ArgVals.size() == F->arg_size() ||
1327 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1328 "Invalid number of values passed to function invocation!");
1330 // Handle non-varargs arguments...
1332 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1334 SetValue(AI, ArgVals[i], StackFrame);
1336 // Handle varargs arguments...
1337 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1341 void Interpreter::run() {
1342 while (!ECStack.empty()) {
1343 // Interpret a single instruction & increment the "PC".
1344 ExecutionContext &SF = ECStack.back(); // Current stack frame
1345 Instruction &I = *SF.CurInst++; // Increment before execute
1347 // Track the number of dynamic instructions executed.
1350 DOUT << "About to interpret: " << I;
1351 visit(I); // Dispatch to one of the visit* methods...
1353 // This is not safe, as visiting the instruction could lower it and free I.
1355 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1356 I.getType() != Type::VoidTy) {
1358 const GenericValue &Val = SF.Values[&I];
1359 switch (I.getType()->getTypeID()) {
1360 default: assert(0 && "Invalid GenericValue Type");
1361 case Type::VoidTyID: DOUT << "void"; break;
1362 case Type::FloatTyID: DOUT << "float " << Val.FloatVal; break;
1363 case Type::DoubleTyID: DOUT << "double " << Val.DoubleVal; break;
1364 case Type::PointerTyID: DOUT << "void* " << intptr_t(Val.PointerVal);
1366 case Type::IntegerTyID:
1367 DOUT << "i" << Val.IntVal.getBitWidth() << " "
1368 << Val.IntVal.toStringUnsigned(10)
1369 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";