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");
33 static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
34 cl::desc("make the interpreter print every volatile load and store"));
36 //===----------------------------------------------------------------------===//
37 // Various Helper Functions
38 //===----------------------------------------------------------------------===//
40 static inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
41 // Determine if the value is signed or not
42 bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
43 // If its signed, extend the sign bits
45 Val |= ~ITy->getBitMask();
49 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
53 //===----------------------------------------------------------------------===//
54 // Binary Instruction Implementations
55 //===----------------------------------------------------------------------===//
57 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
58 case Type::TY##TyID: \
59 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
62 static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
63 GenericValue Src2, const Type *Ty) {
64 switch (Ty->getTypeID()) {
65 IMPLEMENT_BINARY_OPERATOR(+, Float);
66 IMPLEMENT_BINARY_OPERATOR(+, Double);
68 cerr << "Unhandled type for FAdd instruction: " << *Ty << "\n";
73 static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
74 GenericValue Src2, const Type *Ty) {
75 switch (Ty->getTypeID()) {
76 IMPLEMENT_BINARY_OPERATOR(-, Float);
77 IMPLEMENT_BINARY_OPERATOR(-, Double);
79 cerr << "Unhandled type for FSub instruction: " << *Ty << "\n";
84 static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
85 GenericValue Src2, const Type *Ty) {
86 switch (Ty->getTypeID()) {
87 IMPLEMENT_BINARY_OPERATOR(*, Float);
88 IMPLEMENT_BINARY_OPERATOR(*, Double);
90 cerr << "Unhandled type for FMul instruction: " << *Ty << "\n";
95 static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
96 GenericValue Src2, const Type *Ty) {
97 switch (Ty->getTypeID()) {
98 IMPLEMENT_BINARY_OPERATOR(/, Float);
99 IMPLEMENT_BINARY_OPERATOR(/, Double);
101 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
106 static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
107 GenericValue Src2, const Type *Ty) {
108 switch (Ty->getTypeID()) {
109 case Type::FloatTyID:
110 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
112 case Type::DoubleTyID:
113 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
116 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
121 #define IMPLEMENT_INTEGER_ICMP(OP, TY) \
122 case Type::IntegerTyID: \
123 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
126 // Handle pointers specially because they must be compared with only as much
127 // width as the host has. We _do not_ want to be comparing 64 bit values when
128 // running on a 32-bit target, otherwise the upper 32 bits might mess up
129 // comparisons if they contain garbage.
130 #define IMPLEMENT_POINTER_ICMP(OP) \
131 case Type::PointerTyID: \
132 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
133 (void*)(intptr_t)Src2.PointerVal); \
136 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
139 switch (Ty->getTypeID()) {
140 IMPLEMENT_INTEGER_ICMP(eq,Ty);
141 IMPLEMENT_POINTER_ICMP(==);
143 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
149 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
152 switch (Ty->getTypeID()) {
153 IMPLEMENT_INTEGER_ICMP(ne,Ty);
154 IMPLEMENT_POINTER_ICMP(!=);
156 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
162 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
165 switch (Ty->getTypeID()) {
166 IMPLEMENT_INTEGER_ICMP(ult,Ty);
167 IMPLEMENT_POINTER_ICMP(<);
169 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
175 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
178 switch (Ty->getTypeID()) {
179 IMPLEMENT_INTEGER_ICMP(slt,Ty);
180 IMPLEMENT_POINTER_ICMP(<);
182 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
188 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
191 switch (Ty->getTypeID()) {
192 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
193 IMPLEMENT_POINTER_ICMP(>);
195 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
201 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
204 switch (Ty->getTypeID()) {
205 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
206 IMPLEMENT_POINTER_ICMP(>);
208 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
214 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
217 switch (Ty->getTypeID()) {
218 IMPLEMENT_INTEGER_ICMP(ule,Ty);
219 IMPLEMENT_POINTER_ICMP(<=);
221 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
227 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
230 switch (Ty->getTypeID()) {
231 IMPLEMENT_INTEGER_ICMP(sle,Ty);
232 IMPLEMENT_POINTER_ICMP(<=);
234 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
240 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
243 switch (Ty->getTypeID()) {
244 IMPLEMENT_INTEGER_ICMP(uge,Ty);
245 IMPLEMENT_POINTER_ICMP(>=);
247 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
253 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
256 switch (Ty->getTypeID()) {
257 IMPLEMENT_INTEGER_ICMP(sge,Ty);
258 IMPLEMENT_POINTER_ICMP(>=);
260 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
266 void Interpreter::visitICmpInst(ICmpInst &I) {
267 ExecutionContext &SF = ECStack.back();
268 const Type *Ty = I.getOperand(0)->getType();
269 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
270 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
271 GenericValue R; // Result
273 switch (I.getPredicate()) {
274 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
275 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
276 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
277 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
278 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
279 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
280 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
281 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
282 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
283 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
285 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
292 #define IMPLEMENT_FCMP(OP, TY) \
293 case Type::TY##TyID: \
294 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
297 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
300 switch (Ty->getTypeID()) {
301 IMPLEMENT_FCMP(==, Float);
302 IMPLEMENT_FCMP(==, Double);
304 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
310 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
313 switch (Ty->getTypeID()) {
314 IMPLEMENT_FCMP(!=, Float);
315 IMPLEMENT_FCMP(!=, Double);
318 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
324 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
327 switch (Ty->getTypeID()) {
328 IMPLEMENT_FCMP(<=, Float);
329 IMPLEMENT_FCMP(<=, Double);
331 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
337 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
340 switch (Ty->getTypeID()) {
341 IMPLEMENT_FCMP(>=, Float);
342 IMPLEMENT_FCMP(>=, Double);
344 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
350 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
353 switch (Ty->getTypeID()) {
354 IMPLEMENT_FCMP(<, Float);
355 IMPLEMENT_FCMP(<, Double);
357 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
363 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
366 switch (Ty->getTypeID()) {
367 IMPLEMENT_FCMP(>, Float);
368 IMPLEMENT_FCMP(>, Double);
370 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
376 #define IMPLEMENT_UNORDERED(TY, X,Y) \
377 if (TY == Type::FloatTy) { \
378 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
379 Dest.IntVal = APInt(1,true); \
382 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
383 Dest.IntVal = APInt(1,true); \
388 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
391 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
392 return executeFCMP_OEQ(Src1, Src2, Ty);
395 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
398 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
399 return executeFCMP_ONE(Src1, Src2, Ty);
402 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
405 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
406 return executeFCMP_OLE(Src1, Src2, Ty);
409 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
412 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
413 return executeFCMP_OGE(Src1, Src2, Ty);
416 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
419 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
420 return executeFCMP_OLT(Src1, Src2, Ty);
423 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
426 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
427 return executeFCMP_OGT(Src1, Src2, Ty);
430 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
433 if (Ty == Type::FloatTy)
434 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
435 Src2.FloatVal == Src2.FloatVal));
437 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
438 Src2.DoubleVal == Src2.DoubleVal));
442 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
445 if (Ty == Type::FloatTy)
446 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
447 Src2.FloatVal != Src2.FloatVal));
449 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
450 Src2.DoubleVal != Src2.DoubleVal));
454 void Interpreter::visitFCmpInst(FCmpInst &I) {
455 ExecutionContext &SF = ECStack.back();
456 const Type *Ty = I.getOperand(0)->getType();
457 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
458 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
459 GenericValue R; // Result
461 switch (I.getPredicate()) {
462 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
463 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
464 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
465 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
466 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
467 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
468 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
469 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
470 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
471 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
472 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
473 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
474 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
475 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
476 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
477 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
479 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
486 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
487 GenericValue Src2, const Type *Ty) {
490 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
491 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
492 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
493 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
494 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
495 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
496 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
497 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
498 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
499 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
500 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
501 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
502 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
503 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
504 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
505 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
506 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
507 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
508 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
509 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
510 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
511 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
512 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
513 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
514 case FCmpInst::FCMP_FALSE: {
516 Result.IntVal = APInt(1, false);
519 case FCmpInst::FCMP_TRUE: {
521 Result.IntVal = APInt(1, true);
525 cerr << "Unhandled Cmp predicate\n";
530 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
531 ExecutionContext &SF = ECStack.back();
532 const Type *Ty = I.getOperand(0)->getType();
533 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
534 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
535 GenericValue R; // Result
537 switch (I.getOpcode()) {
538 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
539 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
540 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
541 case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;
542 case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;
543 case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;
544 case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;
545 case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;
546 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
547 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
548 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
549 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
550 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
551 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
552 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
554 cerr << "Don't know how to handle this binary operator!\n-->" << I;
561 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
563 return Src1.IntVal == 0 ? Src3 : Src2;
566 void Interpreter::visitSelectInst(SelectInst &I) {
567 ExecutionContext &SF = ECStack.back();
568 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
569 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
570 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
571 GenericValue R = executeSelectInst(Src1, Src2, Src3);
576 //===----------------------------------------------------------------------===//
577 // Terminator Instruction Implementations
578 //===----------------------------------------------------------------------===//
580 void Interpreter::exitCalled(GenericValue GV) {
581 // runAtExitHandlers() assumes there are no stack frames, but
582 // if exit() was called, then it had a stack frame. Blow away
583 // the stack before interpreting atexit handlers.
585 runAtExitHandlers ();
586 exit (GV.IntVal.zextOrTrunc(32).getZExtValue());
589 /// Pop the last stack frame off of ECStack and then copy the result
590 /// back into the result variable if we are not returning void. The
591 /// result variable may be the ExitValue, or the Value of the calling
592 /// CallInst if there was a previous stack frame. This method may
593 /// invalidate any ECStack iterators you have. This method also takes
594 /// care of switching to the normal destination BB, if we are returning
597 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
598 GenericValue Result) {
599 // Pop the current stack frame.
602 if (ECStack.empty()) { // Finished main. Put result into exit code...
603 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
604 ExitValue = Result; // Capture the exit value of the program
606 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
609 // If we have a previous stack frame, and we have a previous call,
610 // fill in the return value...
611 ExecutionContext &CallingSF = ECStack.back();
612 if (Instruction *I = CallingSF.Caller.getInstruction()) {
613 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
614 SetValue(I, Result, CallingSF);
615 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
616 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
617 CallingSF.Caller = CallSite(); // We returned from the call...
622 void Interpreter::visitReturnInst(ReturnInst &I) {
623 ExecutionContext &SF = ECStack.back();
624 const Type *RetTy = Type::VoidTy;
627 // Save away the return value... (if we are not 'ret void')
628 if (I.getNumOperands()) {
629 RetTy = I.getReturnValue()->getType();
630 Result = getOperandValue(I.getReturnValue(), SF);
633 popStackAndReturnValueToCaller(RetTy, Result);
636 void Interpreter::visitUnwindInst(UnwindInst &I) {
641 if (ECStack.empty ())
643 Inst = ECStack.back ().Caller.getInstruction ();
644 } while (!(Inst && isa<InvokeInst> (Inst)));
646 // Return from invoke
647 ExecutionContext &InvokingSF = ECStack.back ();
648 InvokingSF.Caller = CallSite ();
650 // Go to exceptional destination BB of invoke instruction
651 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
654 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
655 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
659 void Interpreter::visitBranchInst(BranchInst &I) {
660 ExecutionContext &SF = ECStack.back();
663 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
664 if (!I.isUnconditional()) {
665 Value *Cond = I.getCondition();
666 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
667 Dest = I.getSuccessor(1);
669 SwitchToNewBasicBlock(Dest, SF);
672 void Interpreter::visitSwitchInst(SwitchInst &I) {
673 ExecutionContext &SF = ECStack.back();
674 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
675 const Type *ElTy = I.getOperand(0)->getType();
677 // Check to see if any of the cases match...
678 BasicBlock *Dest = 0;
679 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
680 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
682 Dest = cast<BasicBlock>(I.getOperand(i+1));
686 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
687 SwitchToNewBasicBlock(Dest, SF);
690 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
691 // This function handles the actual updating of block and instruction iterators
692 // as well as execution of all of the PHI nodes in the destination block.
694 // This method does this because all of the PHI nodes must be executed
695 // atomically, reading their inputs before any of the results are updated. Not
696 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
697 // their inputs. If the input PHI node is updated before it is read, incorrect
698 // results can happen. Thus we use a two phase approach.
700 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
701 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
702 SF.CurBB = Dest; // Update CurBB to branch destination
703 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
705 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
707 // Loop over all of the PHI nodes in the current block, reading their inputs.
708 std::vector<GenericValue> ResultValues;
710 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
711 // Search for the value corresponding to this previous bb...
712 int i = PN->getBasicBlockIndex(PrevBB);
713 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
714 Value *IncomingValue = PN->getIncomingValue(i);
716 // Save the incoming value for this PHI node...
717 ResultValues.push_back(getOperandValue(IncomingValue, SF));
720 // Now loop over all of the PHI nodes setting their values...
721 SF.CurInst = SF.CurBB->begin();
722 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
723 PHINode *PN = cast<PHINode>(SF.CurInst);
724 SetValue(PN, ResultValues[i], SF);
728 //===----------------------------------------------------------------------===//
729 // Memory Instruction Implementations
730 //===----------------------------------------------------------------------===//
732 void Interpreter::visitAllocationInst(AllocationInst &I) {
733 ExecutionContext &SF = ECStack.back();
735 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
737 // Get the number of elements being allocated by the array...
738 unsigned NumElements =
739 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
741 unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
743 // Avoid malloc-ing zero bytes, use max()...
744 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
746 // Allocate enough memory to hold the type...
747 void *Memory = malloc(MemToAlloc);
749 DOUT << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
750 << NumElements << " (Total: " << MemToAlloc << ") at "
751 << uintptr_t(Memory) << '\n';
753 GenericValue Result = PTOGV(Memory);
754 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
755 SetValue(&I, Result, SF);
757 if (I.getOpcode() == Instruction::Alloca)
758 ECStack.back().Allocas.add(Memory);
761 void Interpreter::visitFreeInst(FreeInst &I) {
762 ExecutionContext &SF = ECStack.back();
763 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
764 GenericValue Value = getOperandValue(I.getOperand(0), SF);
765 // TODO: Check to make sure memory is allocated
766 free(GVTOP(Value)); // Free memory
769 // getElementOffset - The workhorse for getelementptr.
771 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
773 ExecutionContext &SF) {
774 assert(isa<PointerType>(Ptr->getType()) &&
775 "Cannot getElementOffset of a nonpointer type!");
779 for (; I != E; ++I) {
780 if (const StructType *STy = dyn_cast<StructType>(*I)) {
781 const StructLayout *SLO = TD.getStructLayout(STy);
783 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
784 unsigned Index = unsigned(CPU->getZExtValue());
786 Total += SLO->getElementOffset(Index);
788 const SequentialType *ST = cast<SequentialType>(*I);
789 // Get the index number for the array... which must be long type...
790 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
794 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
796 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
798 assert(BitWidth == 64 && "Invalid index type for getelementptr");
799 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
801 Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
806 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
807 DOUT << "GEP Index " << Total << " bytes.\n";
811 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
812 ExecutionContext &SF = ECStack.back();
813 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
814 gep_type_begin(I), gep_type_end(I), SF), SF);
817 void Interpreter::visitLoadInst(LoadInst &I) {
818 ExecutionContext &SF = ECStack.back();
819 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
820 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
822 LoadValueFromMemory(Result, Ptr, I.getType());
823 SetValue(&I, Result, SF);
824 if (I.isVolatile() && PrintVolatile)
825 cerr << "Volatile load " << I;
828 void Interpreter::visitStoreInst(StoreInst &I) {
829 ExecutionContext &SF = ECStack.back();
830 GenericValue Val = getOperandValue(I.getOperand(0), SF);
831 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
832 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
833 I.getOperand(0)->getType());
834 if (I.isVolatile() && PrintVolatile)
835 cerr << "Volatile store: " << I;
838 //===----------------------------------------------------------------------===//
839 // Miscellaneous Instruction Implementations
840 //===----------------------------------------------------------------------===//
842 void Interpreter::visitCallSite(CallSite CS) {
843 ExecutionContext &SF = ECStack.back();
845 // Check to see if this is an intrinsic function call...
846 Function *F = CS.getCalledFunction();
847 if (F && F->isDeclaration ())
848 switch (F->getIntrinsicID()) {
849 case Intrinsic::not_intrinsic:
851 case Intrinsic::vastart: { // va_start
852 GenericValue ArgIndex;
853 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
854 ArgIndex.UIntPairVal.second = 0;
855 SetValue(CS.getInstruction(), ArgIndex, SF);
858 case Intrinsic::vaend: // va_end is a noop for the interpreter
860 case Intrinsic::vacopy: // va_copy: dest = src
861 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
864 // If it is an unknown intrinsic function, use the intrinsic lowering
865 // class to transform it into hopefully tasty LLVM code.
867 BasicBlock::iterator me(CS.getInstruction());
868 BasicBlock *Parent = CS.getInstruction()->getParent();
869 bool atBegin(Parent->begin() == me);
872 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
874 // Restore the CurInst pointer to the first instruction newly inserted, if
877 SF.CurInst = Parent->begin();
887 std::vector<GenericValue> ArgVals;
888 const unsigned NumArgs = SF.Caller.arg_size();
889 ArgVals.reserve(NumArgs);
891 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
892 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
894 ArgVals.push_back(getOperandValue(V, SF));
895 // Promote all integral types whose size is < sizeof(i32) into i32.
896 // We do this by zero or sign extending the value as appropriate
897 // according to the parameter attributes
898 const Type *Ty = V->getType();
899 if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32)) {
900 if (CS.paramHasAttr(pNum, Attribute::ZExt))
901 ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
902 else if (CS.paramHasAttr(pNum, Attribute::SExt))
903 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
907 // To handle indirect calls, we must get the pointer value from the argument
908 // and treat it as a function pointer.
909 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
910 callFunction((Function*)GVTOP(SRC), ArgVals);
913 void Interpreter::visitShl(BinaryOperator &I) {
914 ExecutionContext &SF = ECStack.back();
915 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
916 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
918 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
919 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
921 Dest.IntVal = Src1.IntVal;
923 SetValue(&I, Dest, SF);
926 void Interpreter::visitLShr(BinaryOperator &I) {
927 ExecutionContext &SF = ECStack.back();
928 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
929 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
931 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
932 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
934 Dest.IntVal = Src1.IntVal;
936 SetValue(&I, Dest, SF);
939 void Interpreter::visitAShr(BinaryOperator &I) {
940 ExecutionContext &SF = ECStack.back();
941 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
942 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
944 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
945 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
947 Dest.IntVal = Src1.IntVal;
949 SetValue(&I, Dest, SF);
952 GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
953 ExecutionContext &SF) {
954 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
955 const IntegerType *DITy = cast<IntegerType>(DstTy);
956 unsigned DBitWidth = DITy->getBitWidth();
957 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
961 GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
962 ExecutionContext &SF) {
963 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
964 const IntegerType *DITy = cast<IntegerType>(DstTy);
965 unsigned DBitWidth = DITy->getBitWidth();
966 Dest.IntVal = Src.IntVal.sext(DBitWidth);
970 GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
971 ExecutionContext &SF) {
972 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
973 const IntegerType *DITy = cast<IntegerType>(DstTy);
974 unsigned DBitWidth = DITy->getBitWidth();
975 Dest.IntVal = Src.IntVal.zext(DBitWidth);
979 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
980 ExecutionContext &SF) {
981 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
982 assert(SrcVal->getType() == Type::DoubleTy && DstTy == Type::FloatTy &&
983 "Invalid FPTrunc instruction");
984 Dest.FloatVal = (float) Src.DoubleVal;
988 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
989 ExecutionContext &SF) {
990 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
991 assert(SrcVal->getType() == Type::FloatTy && DstTy == Type::DoubleTy &&
992 "Invalid FPTrunc instruction");
993 Dest.DoubleVal = (double) Src.FloatVal;
997 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
998 ExecutionContext &SF) {
999 const Type *SrcTy = SrcVal->getType();
1000 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1001 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1002 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
1004 if (SrcTy->getTypeID() == Type::FloatTyID)
1005 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1007 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1011 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1012 ExecutionContext &SF) {
1013 const Type *SrcTy = SrcVal->getType();
1014 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1015 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1016 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1018 if (SrcTy->getTypeID() == Type::FloatTyID)
1019 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1021 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1025 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1026 ExecutionContext &SF) {
1027 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1028 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1030 if (DstTy->getTypeID() == Type::FloatTyID)
1031 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
1033 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
1037 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1038 ExecutionContext &SF) {
1039 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1040 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
1042 if (DstTy->getTypeID() == Type::FloatTyID)
1043 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
1045 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
1050 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1051 ExecutionContext &SF) {
1052 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1053 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1054 assert(isa<PointerType>(SrcVal->getType()) && "Invalid PtrToInt instruction");
1056 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1060 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1061 ExecutionContext &SF) {
1062 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1063 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1065 uint32_t PtrSize = TD.getPointerSizeInBits();
1066 if (PtrSize != Src.IntVal.getBitWidth())
1067 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
1069 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
1073 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1074 ExecutionContext &SF) {
1076 const Type *SrcTy = SrcVal->getType();
1077 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1078 if (isa<PointerType>(DstTy)) {
1079 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1080 Dest.PointerVal = Src.PointerVal;
1081 } else if (DstTy->isInteger()) {
1082 if (SrcTy == Type::FloatTy) {
1083 Dest.IntVal.zext(sizeof(Src.FloatVal) * CHAR_BIT);
1084 Dest.IntVal.floatToBits(Src.FloatVal);
1085 } else if (SrcTy == Type::DoubleTy) {
1086 Dest.IntVal.zext(sizeof(Src.DoubleVal) * CHAR_BIT);
1087 Dest.IntVal.doubleToBits(Src.DoubleVal);
1088 } else if (SrcTy->isInteger()) {
1089 Dest.IntVal = Src.IntVal;
1091 assert(0 && "Invalid BitCast");
1092 } else if (DstTy == Type::FloatTy) {
1093 if (SrcTy->isInteger())
1094 Dest.FloatVal = Src.IntVal.bitsToFloat();
1096 Dest.FloatVal = Src.FloatVal;
1097 } else if (DstTy == Type::DoubleTy) {
1098 if (SrcTy->isInteger())
1099 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1101 Dest.DoubleVal = Src.DoubleVal;
1103 assert(0 && "Invalid Bitcast");
1108 void Interpreter::visitTruncInst(TruncInst &I) {
1109 ExecutionContext &SF = ECStack.back();
1110 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1113 void Interpreter::visitSExtInst(SExtInst &I) {
1114 ExecutionContext &SF = ECStack.back();
1115 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1118 void Interpreter::visitZExtInst(ZExtInst &I) {
1119 ExecutionContext &SF = ECStack.back();
1120 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1123 void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1124 ExecutionContext &SF = ECStack.back();
1125 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1128 void Interpreter::visitFPExtInst(FPExtInst &I) {
1129 ExecutionContext &SF = ECStack.back();
1130 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1133 void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1134 ExecutionContext &SF = ECStack.back();
1135 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1138 void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1139 ExecutionContext &SF = ECStack.back();
1140 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1143 void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1144 ExecutionContext &SF = ECStack.back();
1145 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1148 void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1149 ExecutionContext &SF = ECStack.back();
1150 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1153 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1154 ExecutionContext &SF = ECStack.back();
1155 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1158 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1159 ExecutionContext &SF = ECStack.back();
1160 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1163 void Interpreter::visitBitCastInst(BitCastInst &I) {
1164 ExecutionContext &SF = ECStack.back();
1165 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1168 #define IMPLEMENT_VAARG(TY) \
1169 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1171 void Interpreter::visitVAArgInst(VAArgInst &I) {
1172 ExecutionContext &SF = ECStack.back();
1174 // Get the incoming valist parameter. LLI treats the valist as a
1175 // (ec-stack-depth var-arg-index) pair.
1176 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1178 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1179 .VarArgs[VAList.UIntPairVal.second];
1180 const Type *Ty = I.getType();
1181 switch (Ty->getTypeID()) {
1182 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
1183 IMPLEMENT_VAARG(Pointer);
1184 IMPLEMENT_VAARG(Float);
1185 IMPLEMENT_VAARG(Double);
1187 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1191 // Set the Value of this Instruction.
1192 SetValue(&I, Dest, SF);
1194 // Move the pointer to the next vararg.
1195 ++VAList.UIntPairVal.second;
1198 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1199 ExecutionContext &SF) {
1200 switch (CE->getOpcode()) {
1201 case Instruction::Trunc:
1202 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1203 case Instruction::ZExt:
1204 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1205 case Instruction::SExt:
1206 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1207 case Instruction::FPTrunc:
1208 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1209 case Instruction::FPExt:
1210 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1211 case Instruction::UIToFP:
1212 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1213 case Instruction::SIToFP:
1214 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1215 case Instruction::FPToUI:
1216 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1217 case Instruction::FPToSI:
1218 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1219 case Instruction::PtrToInt:
1220 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1221 case Instruction::IntToPtr:
1222 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1223 case Instruction::BitCast:
1224 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1225 case Instruction::GetElementPtr:
1226 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1227 gep_type_end(CE), SF);
1228 case Instruction::FCmp:
1229 case Instruction::ICmp:
1230 return executeCmpInst(CE->getPredicate(),
1231 getOperandValue(CE->getOperand(0), SF),
1232 getOperandValue(CE->getOperand(1), SF),
1233 CE->getOperand(0)->getType());
1234 case Instruction::Select:
1235 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1236 getOperandValue(CE->getOperand(1), SF),
1237 getOperandValue(CE->getOperand(2), SF));
1242 // The cases below here require a GenericValue parameter for the result
1243 // so we initialize one, compute it and then return it.
1244 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1245 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
1247 const Type * Ty = CE->getOperand(0)->getType();
1248 switch (CE->getOpcode()) {
1249 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
1250 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
1251 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
1252 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
1253 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
1254 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
1255 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1256 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1257 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1258 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1259 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1260 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
1261 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
1262 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
1263 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
1264 case Instruction::Shl:
1265 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1267 case Instruction::LShr:
1268 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1270 case Instruction::AShr:
1271 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1274 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
1276 return GenericValue();
1281 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1282 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1283 return getConstantExprValue(CE, SF);
1284 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1285 return getConstantValue(CPV);
1286 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1287 return PTOGV(getPointerToGlobal(GV));
1289 return SF.Values[V];
1293 //===----------------------------------------------------------------------===//
1294 // Dispatch and Execution Code
1295 //===----------------------------------------------------------------------===//
1297 //===----------------------------------------------------------------------===//
1298 // callFunction - Execute the specified function...
1300 void Interpreter::callFunction(Function *F,
1301 const std::vector<GenericValue> &ArgVals) {
1302 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1303 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1304 "Incorrect number of arguments passed into function call!");
1305 // Make a new stack frame... and fill it in.
1306 ECStack.push_back(ExecutionContext());
1307 ExecutionContext &StackFrame = ECStack.back();
1308 StackFrame.CurFunction = F;
1310 // Special handling for external functions.
1311 if (F->isDeclaration()) {
1312 GenericValue Result = callExternalFunction (F, ArgVals);
1313 // Simulate a 'ret' instruction of the appropriate type.
1314 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1318 // Get pointers to first LLVM BB & Instruction in function.
1319 StackFrame.CurBB = F->begin();
1320 StackFrame.CurInst = StackFrame.CurBB->begin();
1322 // Run through the function arguments and initialize their values...
1323 assert((ArgVals.size() == F->arg_size() ||
1324 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1325 "Invalid number of values passed to function invocation!");
1327 // Handle non-varargs arguments...
1329 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1331 SetValue(AI, ArgVals[i], StackFrame);
1333 // Handle varargs arguments...
1334 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1338 void Interpreter::run() {
1339 while (!ECStack.empty()) {
1340 // Interpret a single instruction & increment the "PC".
1341 ExecutionContext &SF = ECStack.back(); // Current stack frame
1342 Instruction &I = *SF.CurInst++; // Increment before execute
1344 // Track the number of dynamic instructions executed.
1347 DOUT << "About to interpret: " << I;
1348 visit(I); // Dispatch to one of the visit* methods...
1350 // This is not safe, as visiting the instruction could lower it and free I.
1352 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1353 I.getType() != Type::VoidTy) {
1355 const GenericValue &Val = SF.Values[&I];
1356 switch (I.getType()->getTypeID()) {
1357 default: assert(0 && "Invalid GenericValue Type");
1358 case Type::VoidTyID: DOUT << "void"; break;
1359 case Type::FloatTyID: DOUT << "float " << Val.FloatVal; break;
1360 case Type::DoubleTyID: DOUT << "double " << Val.DoubleVal; break;
1361 case Type::PointerTyID: DOUT << "void* " << intptr_t(Val.PointerVal);
1363 case Type::IntegerTyID:
1364 DOUT << "i" << Val.IntVal.getBitWidth() << " "
1365 << Val.IntVal.toStringUnsigned(10)
1366 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";