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/CommandLine.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/MathExtras.h"
32 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
33 static Interpreter *TheEE = 0;
35 static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
36 cl::desc("make the interpreter print every volatile load and store"));
38 //===----------------------------------------------------------------------===//
39 // Various Helper Functions
40 //===----------------------------------------------------------------------===//
42 static inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
43 // Determine if the value is signed or not
44 bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
45 // If its signed, extend the sign bits
47 Val |= ~ITy->getBitMask();
51 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
55 void Interpreter::initializeExecutionEngine() {
59 //===----------------------------------------------------------------------===//
60 // Binary Instruction Implementations
61 //===----------------------------------------------------------------------===//
63 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
64 case Type::TY##TyID: \
65 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
68 #define IMPLEMENT_INTEGER_BINOP1(OP, TY) \
69 case Type::IntegerTyID: { \
70 Dest.IntVal = Src1.IntVal OP Src2.IntVal; \
75 static void executeAddInst(GenericValue &Dest, GenericValue Src1,
76 GenericValue Src2, const Type *Ty) {
77 switch (Ty->getTypeID()) {
78 IMPLEMENT_INTEGER_BINOP1(+, Ty);
79 IMPLEMENT_BINARY_OPERATOR(+, Float);
80 IMPLEMENT_BINARY_OPERATOR(+, Double);
82 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
87 static void executeSubInst(GenericValue &Dest, GenericValue Src1,
88 GenericValue Src2, const Type *Ty) {
89 switch (Ty->getTypeID()) {
90 IMPLEMENT_INTEGER_BINOP1(-, Ty);
91 IMPLEMENT_BINARY_OPERATOR(-, Float);
92 IMPLEMENT_BINARY_OPERATOR(-, Double);
94 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
99 static void executeMulInst(GenericValue &Dest, GenericValue Src1,
100 GenericValue Src2, const Type *Ty) {
101 switch (Ty->getTypeID()) {
102 IMPLEMENT_INTEGER_BINOP1(*, Ty);
103 IMPLEMENT_BINARY_OPERATOR(*, Float);
104 IMPLEMENT_BINARY_OPERATOR(*, Double);
106 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
111 static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
112 GenericValue Src2, const Type *Ty) {
113 switch (Ty->getTypeID()) {
114 IMPLEMENT_BINARY_OPERATOR(/, Float);
115 IMPLEMENT_BINARY_OPERATOR(/, Double);
117 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
122 static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
123 GenericValue Src2, const Type *Ty) {
124 switch (Ty->getTypeID()) {
125 case Type::FloatTyID:
126 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
128 case Type::DoubleTyID:
129 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
132 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
137 #define IMPLEMENT_INTEGER_ICMP(OP, TY) \
138 case Type::IntegerTyID: \
139 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
142 // Handle pointers specially because they must be compared with only as much
143 // width as the host has. We _do not_ want to be comparing 64 bit values when
144 // running on a 32-bit target, otherwise the upper 32 bits might mess up
145 // comparisons if they contain garbage.
146 #define IMPLEMENT_POINTER_ICMP(OP) \
147 case Type::PointerTyID: \
148 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
149 (void*)(intptr_t)Src2.PointerVal); \
152 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
155 switch (Ty->getTypeID()) {
156 IMPLEMENT_INTEGER_ICMP(eq,Ty);
157 IMPLEMENT_POINTER_ICMP(==);
159 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
165 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
168 switch (Ty->getTypeID()) {
169 IMPLEMENT_INTEGER_ICMP(ne,Ty);
170 IMPLEMENT_POINTER_ICMP(!=);
172 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
178 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
181 switch (Ty->getTypeID()) {
182 IMPLEMENT_INTEGER_ICMP(ult,Ty);
183 IMPLEMENT_POINTER_ICMP(<);
185 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
191 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
194 switch (Ty->getTypeID()) {
195 IMPLEMENT_INTEGER_ICMP(slt,Ty);
196 IMPLEMENT_POINTER_ICMP(<);
198 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
204 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
207 switch (Ty->getTypeID()) {
208 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
209 IMPLEMENT_POINTER_ICMP(>);
211 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
217 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
220 switch (Ty->getTypeID()) {
221 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
222 IMPLEMENT_POINTER_ICMP(>);
224 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
230 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
233 switch (Ty->getTypeID()) {
234 IMPLEMENT_INTEGER_ICMP(ule,Ty);
235 IMPLEMENT_POINTER_ICMP(<=);
237 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
243 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
246 switch (Ty->getTypeID()) {
247 IMPLEMENT_INTEGER_ICMP(sle,Ty);
248 IMPLEMENT_POINTER_ICMP(<=);
250 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
256 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
259 switch (Ty->getTypeID()) {
260 IMPLEMENT_INTEGER_ICMP(uge,Ty);
261 IMPLEMENT_POINTER_ICMP(>=);
263 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
269 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
272 switch (Ty->getTypeID()) {
273 IMPLEMENT_INTEGER_ICMP(sge,Ty);
274 IMPLEMENT_POINTER_ICMP(>=);
276 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
282 void Interpreter::visitICmpInst(ICmpInst &I) {
283 ExecutionContext &SF = ECStack.back();
284 const Type *Ty = I.getOperand(0)->getType();
285 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
286 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
287 GenericValue R; // Result
289 switch (I.getPredicate()) {
290 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
291 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
292 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
293 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
294 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
295 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
296 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
297 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
298 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
299 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
301 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
308 #define IMPLEMENT_FCMP(OP, TY) \
309 case Type::TY##TyID: \
310 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
313 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
316 switch (Ty->getTypeID()) {
317 IMPLEMENT_FCMP(==, Float);
318 IMPLEMENT_FCMP(==, Double);
320 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
326 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
329 switch (Ty->getTypeID()) {
330 IMPLEMENT_FCMP(!=, Float);
331 IMPLEMENT_FCMP(!=, Double);
334 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
340 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
343 switch (Ty->getTypeID()) {
344 IMPLEMENT_FCMP(<=, Float);
345 IMPLEMENT_FCMP(<=, Double);
347 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
353 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
356 switch (Ty->getTypeID()) {
357 IMPLEMENT_FCMP(>=, Float);
358 IMPLEMENT_FCMP(>=, Double);
360 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
366 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
369 switch (Ty->getTypeID()) {
370 IMPLEMENT_FCMP(<, Float);
371 IMPLEMENT_FCMP(<, Double);
373 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
379 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
382 switch (Ty->getTypeID()) {
383 IMPLEMENT_FCMP(>, Float);
384 IMPLEMENT_FCMP(>, Double);
386 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
392 #define IMPLEMENT_UNORDERED(TY, X,Y) \
393 if (TY == Type::FloatTy) { \
394 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
395 Dest.IntVal = APInt(1,true); \
398 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
399 Dest.IntVal = APInt(1,true); \
404 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
407 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
408 return executeFCMP_OEQ(Src1, Src2, Ty);
411 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
414 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
415 return executeFCMP_ONE(Src1, Src2, Ty);
418 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
421 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
422 return executeFCMP_OLE(Src1, Src2, Ty);
425 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
428 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
429 return executeFCMP_OGE(Src1, Src2, Ty);
432 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
435 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
436 return executeFCMP_OLT(Src1, Src2, Ty);
439 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
442 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
443 return executeFCMP_OGT(Src1, Src2, Ty);
446 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
449 if (Ty == Type::FloatTy)
450 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
451 Src2.FloatVal == Src2.FloatVal));
453 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
454 Src2.DoubleVal == Src2.DoubleVal));
458 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
461 if (Ty == Type::FloatTy)
462 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
463 Src2.FloatVal != Src2.FloatVal));
465 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
466 Src2.DoubleVal != Src2.DoubleVal));
470 void Interpreter::visitFCmpInst(FCmpInst &I) {
471 ExecutionContext &SF = ECStack.back();
472 const Type *Ty = I.getOperand(0)->getType();
473 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
474 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
475 GenericValue R; // Result
477 switch (I.getPredicate()) {
478 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
479 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
480 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
481 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
482 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
483 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
484 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
485 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
486 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
487 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
488 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
489 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
490 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
491 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
492 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
493 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
495 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
502 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
503 GenericValue Src2, const Type *Ty) {
506 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
507 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
508 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
509 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
510 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
511 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
512 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
513 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
514 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
515 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
516 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
517 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
518 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
519 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
520 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
521 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
522 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
523 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
524 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
525 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
526 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
527 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
528 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
529 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
530 case FCmpInst::FCMP_FALSE: {
532 Result.IntVal = APInt(1, false);
535 case FCmpInst::FCMP_TRUE: {
537 Result.IntVal = APInt(1, true);
541 cerr << "Unhandled Cmp predicate\n";
546 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
547 ExecutionContext &SF = ECStack.back();
548 const Type *Ty = I.getOperand(0)->getType();
549 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
550 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
551 GenericValue R; // Result
553 switch (I.getOpcode()) {
554 case Instruction::Add: executeAddInst (R, Src1, Src2, Ty); break;
555 case Instruction::Sub: executeSubInst (R, Src1, Src2, Ty); break;
556 case Instruction::Mul: executeMulInst (R, Src1, Src2, Ty); break;
557 case Instruction::FDiv: executeFDivInst (R, Src1, Src2, Ty); break;
558 case Instruction::FRem: executeFRemInst (R, Src1, Src2, Ty); break;
559 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
560 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
561 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
562 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
563 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
564 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
565 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
567 cerr << "Don't know how to handle this binary operator!\n-->" << I;
574 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
576 return Src1.IntVal == 0 ? Src3 : Src2;
579 void Interpreter::visitSelectInst(SelectInst &I) {
580 ExecutionContext &SF = ECStack.back();
581 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
582 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
583 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
584 GenericValue R = executeSelectInst(Src1, Src2, Src3);
589 //===----------------------------------------------------------------------===//
590 // Terminator Instruction Implementations
591 //===----------------------------------------------------------------------===//
593 void Interpreter::exitCalled(GenericValue GV) {
594 // runAtExitHandlers() assumes there are no stack frames, but
595 // if exit() was called, then it had a stack frame. Blow away
596 // the stack before interpreting atexit handlers.
598 runAtExitHandlers ();
599 exit (GV.IntVal.zextOrTrunc(32).getZExtValue());
602 /// Pop the last stack frame off of ECStack and then copy the result
603 /// back into the result variable if we are not returning void. The
604 /// result variable may be the ExitValue, or the Value of the calling
605 /// CallInst if there was a previous stack frame. This method may
606 /// invalidate any ECStack iterators you have. This method also takes
607 /// care of switching to the normal destination BB, if we are returning
610 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
611 GenericValue Result) {
612 // Pop the current stack frame.
615 if (ECStack.empty()) { // Finished main. Put result into exit code...
616 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
617 ExitValue = Result; // Capture the exit value of the program
619 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
622 // If we have a previous stack frame, and we have a previous call,
623 // fill in the return value...
624 ExecutionContext &CallingSF = ECStack.back();
625 if (Instruction *I = CallingSF.Caller.getInstruction()) {
626 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
627 SetValue(I, Result, CallingSF);
628 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
629 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
630 CallingSF.Caller = CallSite(); // We returned from the call...
635 void Interpreter::visitReturnInst(ReturnInst &I) {
636 ExecutionContext &SF = ECStack.back();
637 const Type *RetTy = Type::VoidTy;
640 // Save away the return value... (if we are not 'ret void')
641 if (I.getNumOperands()) {
642 RetTy = I.getReturnValue()->getType();
643 Result = getOperandValue(I.getReturnValue(), SF);
646 popStackAndReturnValueToCaller(RetTy, Result);
649 void Interpreter::visitUnwindInst(UnwindInst &I) {
654 if (ECStack.empty ())
656 Inst = ECStack.back ().Caller.getInstruction ();
657 } while (!(Inst && isa<InvokeInst> (Inst)));
659 // Return from invoke
660 ExecutionContext &InvokingSF = ECStack.back ();
661 InvokingSF.Caller = CallSite ();
663 // Go to exceptional destination BB of invoke instruction
664 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
667 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
668 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
672 void Interpreter::visitBranchInst(BranchInst &I) {
673 ExecutionContext &SF = ECStack.back();
676 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
677 if (!I.isUnconditional()) {
678 Value *Cond = I.getCondition();
679 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
680 Dest = I.getSuccessor(1);
682 SwitchToNewBasicBlock(Dest, SF);
685 void Interpreter::visitSwitchInst(SwitchInst &I) {
686 ExecutionContext &SF = ECStack.back();
687 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
688 const Type *ElTy = I.getOperand(0)->getType();
690 // Check to see if any of the cases match...
691 BasicBlock *Dest = 0;
692 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
693 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
695 Dest = cast<BasicBlock>(I.getOperand(i+1));
699 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
700 SwitchToNewBasicBlock(Dest, SF);
703 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
704 // This function handles the actual updating of block and instruction iterators
705 // as well as execution of all of the PHI nodes in the destination block.
707 // This method does this because all of the PHI nodes must be executed
708 // atomically, reading their inputs before any of the results are updated. Not
709 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
710 // their inputs. If the input PHI node is updated before it is read, incorrect
711 // results can happen. Thus we use a two phase approach.
713 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
714 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
715 SF.CurBB = Dest; // Update CurBB to branch destination
716 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
718 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
720 // Loop over all of the PHI nodes in the current block, reading their inputs.
721 std::vector<GenericValue> ResultValues;
723 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
724 // Search for the value corresponding to this previous bb...
725 int i = PN->getBasicBlockIndex(PrevBB);
726 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
727 Value *IncomingValue = PN->getIncomingValue(i);
729 // Save the incoming value for this PHI node...
730 ResultValues.push_back(getOperandValue(IncomingValue, SF));
733 // Now loop over all of the PHI nodes setting their values...
734 SF.CurInst = SF.CurBB->begin();
735 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
736 PHINode *PN = cast<PHINode>(SF.CurInst);
737 SetValue(PN, ResultValues[i], SF);
741 //===----------------------------------------------------------------------===//
742 // Memory Instruction Implementations
743 //===----------------------------------------------------------------------===//
745 void Interpreter::visitAllocationInst(AllocationInst &I) {
746 ExecutionContext &SF = ECStack.back();
748 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
750 // Get the number of elements being allocated by the array...
751 unsigned NumElements =
752 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
754 unsigned TypeSize = (size_t)TD.getABITypeSize(Ty);
756 // Avoid malloc-ing zero bytes, use max()...
757 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
759 // Allocate enough memory to hold the type...
760 void *Memory = malloc(MemToAlloc);
762 DOUT << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
763 << NumElements << " (Total: " << MemToAlloc << ") at "
764 << uintptr_t(Memory) << '\n';
766 GenericValue Result = PTOGV(Memory);
767 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
768 SetValue(&I, Result, SF);
770 if (I.getOpcode() == Instruction::Alloca)
771 ECStack.back().Allocas.add(Memory);
774 void Interpreter::visitFreeInst(FreeInst &I) {
775 ExecutionContext &SF = ECStack.back();
776 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
777 GenericValue Value = getOperandValue(I.getOperand(0), SF);
778 // TODO: Check to make sure memory is allocated
779 free(GVTOP(Value)); // Free memory
782 // getElementOffset - The workhorse for getelementptr.
784 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
786 ExecutionContext &SF) {
787 assert(isa<PointerType>(Ptr->getType()) &&
788 "Cannot getElementOffset of a nonpointer type!");
792 for (; I != E; ++I) {
793 if (const StructType *STy = dyn_cast<StructType>(*I)) {
794 const StructLayout *SLO = TD.getStructLayout(STy);
796 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
797 unsigned Index = unsigned(CPU->getZExtValue());
799 Total += SLO->getElementOffset(Index);
801 const SequentialType *ST = cast<SequentialType>(*I);
802 // Get the index number for the array... which must be long type...
803 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
807 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
809 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
811 assert(BitWidth == 64 && "Invalid index type for getelementptr");
812 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
814 Total += TD.getABITypeSize(ST->getElementType())*Idx;
819 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
820 DOUT << "GEP Index " << Total << " bytes.\n";
824 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
825 ExecutionContext &SF = ECStack.back();
826 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
827 gep_type_begin(I), gep_type_end(I), SF), SF);
830 void Interpreter::visitLoadInst(LoadInst &I) {
831 ExecutionContext &SF = ECStack.back();
832 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
833 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
835 LoadValueFromMemory(Result, Ptr, I.getType());
836 SetValue(&I, Result, SF);
837 if (I.isVolatile() && PrintVolatile)
838 cerr << "Volatile load " << I;
841 void Interpreter::visitStoreInst(StoreInst &I) {
842 ExecutionContext &SF = ECStack.back();
843 GenericValue Val = getOperandValue(I.getOperand(0), SF);
844 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
845 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
846 I.getOperand(0)->getType());
847 if (I.isVolatile() && PrintVolatile)
848 cerr << "Volatile store: " << I;
851 //===----------------------------------------------------------------------===//
852 // Miscellaneous Instruction Implementations
853 //===----------------------------------------------------------------------===//
855 void Interpreter::visitCallSite(CallSite CS) {
856 ExecutionContext &SF = ECStack.back();
858 // Check to see if this is an intrinsic function call...
859 Function *F = CS.getCalledFunction();
860 if (F && F->isDeclaration ())
861 switch (F->getIntrinsicID()) {
862 case Intrinsic::not_intrinsic:
864 case Intrinsic::vastart: { // va_start
865 GenericValue ArgIndex;
866 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
867 ArgIndex.UIntPairVal.second = 0;
868 SetValue(CS.getInstruction(), ArgIndex, SF);
871 case Intrinsic::vaend: // va_end is a noop for the interpreter
873 case Intrinsic::vacopy: // va_copy: dest = src
874 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
877 // If it is an unknown intrinsic function, use the intrinsic lowering
878 // class to transform it into hopefully tasty LLVM code.
880 BasicBlock::iterator me(CS.getInstruction());
881 BasicBlock *Parent = CS.getInstruction()->getParent();
882 bool atBegin(Parent->begin() == me);
885 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
887 // Restore the CurInst pointer to the first instruction newly inserted, if
890 SF.CurInst = Parent->begin();
900 std::vector<GenericValue> ArgVals;
901 const unsigned NumArgs = SF.Caller.arg_size();
902 ArgVals.reserve(NumArgs);
904 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
905 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
907 ArgVals.push_back(getOperandValue(V, SF));
908 // Promote all integral types whose size is < sizeof(i32) into i32.
909 // We do this by zero or sign extending the value as appropriate
910 // according to the parameter attributes
911 const Type *Ty = V->getType();
912 if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32)) {
913 if (CS.paramHasAttr(pNum, ParamAttr::ZExt))
914 ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
915 else if (CS.paramHasAttr(pNum, ParamAttr::SExt))
916 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
920 // To handle indirect calls, we must get the pointer value from the argument
921 // and treat it as a function pointer.
922 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
923 callFunction((Function*)GVTOP(SRC), ArgVals);
926 void Interpreter::visitShl(BinaryOperator &I) {
927 ExecutionContext &SF = ECStack.back();
928 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
929 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
931 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
932 SetValue(&I, Dest, SF);
935 void Interpreter::visitLShr(BinaryOperator &I) {
936 ExecutionContext &SF = ECStack.back();
937 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
938 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
940 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
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 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
950 SetValue(&I, Dest, SF);
953 GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
954 ExecutionContext &SF) {
955 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
956 const IntegerType *DITy = cast<IntegerType>(DstTy);
957 unsigned DBitWidth = DITy->getBitWidth();
958 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
962 GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
963 ExecutionContext &SF) {
964 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
965 const IntegerType *DITy = cast<IntegerType>(DstTy);
966 unsigned DBitWidth = DITy->getBitWidth();
967 Dest.IntVal = Src.IntVal.sext(DBitWidth);
971 GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
972 ExecutionContext &SF) {
973 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
974 const IntegerType *DITy = cast<IntegerType>(DstTy);
975 unsigned DBitWidth = DITy->getBitWidth();
976 Dest.IntVal = Src.IntVal.zext(DBitWidth);
980 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
981 ExecutionContext &SF) {
982 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
983 assert(SrcVal->getType() == Type::DoubleTy && DstTy == Type::FloatTy &&
984 "Invalid FPTrunc instruction");
985 Dest.FloatVal = (float) Src.DoubleVal;
989 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
990 ExecutionContext &SF) {
991 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
992 assert(SrcVal->getType() == Type::FloatTy && DstTy == Type::DoubleTy &&
993 "Invalid FPTrunc instruction");
994 Dest.DoubleVal = (double) Src.FloatVal;
998 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
999 ExecutionContext &SF) {
1000 const Type *SrcTy = SrcVal->getType();
1001 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1002 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1003 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
1005 if (SrcTy->getTypeID() == Type::FloatTyID)
1006 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1008 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1012 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1013 ExecutionContext &SF) {
1014 const Type *SrcTy = SrcVal->getType();
1015 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1016 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1017 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1019 if (SrcTy->getTypeID() == Type::FloatTyID)
1020 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1022 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1026 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1027 ExecutionContext &SF) {
1028 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1029 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1031 if (DstTy->getTypeID() == Type::FloatTyID)
1032 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
1034 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
1038 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1039 ExecutionContext &SF) {
1040 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1041 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
1043 if (DstTy->getTypeID() == Type::FloatTyID)
1044 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
1046 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
1051 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1052 ExecutionContext &SF) {
1053 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1054 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1055 assert(isa<PointerType>(SrcVal->getType()) && "Invalid PtrToInt instruction");
1057 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1061 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1062 ExecutionContext &SF) {
1063 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1064 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1066 uint32_t PtrSize = TD.getPointerSizeInBits();
1067 if (PtrSize != Src.IntVal.getBitWidth())
1068 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
1070 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
1074 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1075 ExecutionContext &SF) {
1077 const Type *SrcTy = SrcVal->getType();
1078 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1079 if (isa<PointerType>(DstTy)) {
1080 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1081 Dest.PointerVal = Src.PointerVal;
1082 } else if (DstTy->isInteger()) {
1083 if (SrcTy == Type::FloatTy) {
1084 Dest.IntVal.zext(sizeof(Src.FloatVal) * 8);
1085 Dest.IntVal.floatToBits(Src.FloatVal);
1086 } else if (SrcTy == Type::DoubleTy) {
1087 Dest.IntVal.zext(sizeof(Src.DoubleVal) * 8);
1088 Dest.IntVal.doubleToBits(Src.DoubleVal);
1089 } else if (SrcTy->isInteger()) {
1090 Dest.IntVal = Src.IntVal;
1092 assert(0 && "Invalid BitCast");
1093 } else if (DstTy == Type::FloatTy) {
1094 if (SrcTy->isInteger())
1095 Dest.FloatVal = Src.IntVal.bitsToFloat();
1097 Dest.FloatVal = Src.FloatVal;
1098 } else if (DstTy == Type::DoubleTy) {
1099 if (SrcTy->isInteger())
1100 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1102 Dest.DoubleVal = Src.DoubleVal;
1104 assert(0 && "Invalid Bitcast");
1109 void Interpreter::visitTruncInst(TruncInst &I) {
1110 ExecutionContext &SF = ECStack.back();
1111 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1114 void Interpreter::visitSExtInst(SExtInst &I) {
1115 ExecutionContext &SF = ECStack.back();
1116 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1119 void Interpreter::visitZExtInst(ZExtInst &I) {
1120 ExecutionContext &SF = ECStack.back();
1121 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1124 void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1125 ExecutionContext &SF = ECStack.back();
1126 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1129 void Interpreter::visitFPExtInst(FPExtInst &I) {
1130 ExecutionContext &SF = ECStack.back();
1131 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1134 void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1135 ExecutionContext &SF = ECStack.back();
1136 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1139 void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1140 ExecutionContext &SF = ECStack.back();
1141 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1144 void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1145 ExecutionContext &SF = ECStack.back();
1146 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1149 void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1150 ExecutionContext &SF = ECStack.back();
1151 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1154 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1155 ExecutionContext &SF = ECStack.back();
1156 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1159 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1160 ExecutionContext &SF = ECStack.back();
1161 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1164 void Interpreter::visitBitCastInst(BitCastInst &I) {
1165 ExecutionContext &SF = ECStack.back();
1166 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1169 #define IMPLEMENT_VAARG(TY) \
1170 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1172 void Interpreter::visitVAArgInst(VAArgInst &I) {
1173 ExecutionContext &SF = ECStack.back();
1175 // Get the incoming valist parameter. LLI treats the valist as a
1176 // (ec-stack-depth var-arg-index) pair.
1177 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1179 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1180 .VarArgs[VAList.UIntPairVal.second];
1181 const Type *Ty = I.getType();
1182 switch (Ty->getTypeID()) {
1183 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
1184 IMPLEMENT_VAARG(Pointer);
1185 IMPLEMENT_VAARG(Float);
1186 IMPLEMENT_VAARG(Double);
1188 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1192 // Set the Value of this Instruction.
1193 SetValue(&I, Dest, SF);
1195 // Move the pointer to the next vararg.
1196 ++VAList.UIntPairVal.second;
1199 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1200 ExecutionContext &SF) {
1201 switch (CE->getOpcode()) {
1202 case Instruction::Trunc:
1203 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1204 case Instruction::ZExt:
1205 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1206 case Instruction::SExt:
1207 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1208 case Instruction::FPTrunc:
1209 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1210 case Instruction::FPExt:
1211 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1212 case Instruction::UIToFP:
1213 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1214 case Instruction::SIToFP:
1215 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1216 case Instruction::FPToUI:
1217 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1218 case Instruction::FPToSI:
1219 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1220 case Instruction::PtrToInt:
1221 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1222 case Instruction::IntToPtr:
1223 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1224 case Instruction::BitCast:
1225 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1226 case Instruction::GetElementPtr:
1227 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1228 gep_type_end(CE), SF);
1229 case Instruction::FCmp:
1230 case Instruction::ICmp:
1231 return executeCmpInst(CE->getPredicate(),
1232 getOperandValue(CE->getOperand(0), SF),
1233 getOperandValue(CE->getOperand(1), SF),
1234 CE->getOperand(0)->getType());
1235 case Instruction::Select:
1236 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1237 getOperandValue(CE->getOperand(1), SF),
1238 getOperandValue(CE->getOperand(2), SF));
1243 // The cases below here require a GenericValue parameter for the result
1244 // so we initialize one, compute it and then return it.
1245 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1246 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
1248 const Type * Ty = CE->getOperand(0)->getType();
1249 switch (CE->getOpcode()) {
1250 case Instruction::Add: executeAddInst (Dest, Op0, Op1, Ty); break;
1251 case Instruction::Sub: executeSubInst (Dest, Op0, Op1, Ty); break;
1252 case Instruction::Mul: executeMulInst (Dest, Op0, Op1, Ty); break;
1253 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1254 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1255 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1256 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1257 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1258 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
1259 case Instruction::And: Dest.IntVal = Op0.IntVal.And(Op1.IntVal); break;
1260 case Instruction::Or: Dest.IntVal = Op0.IntVal.Or(Op1.IntVal); break;
1261 case Instruction::Xor: Dest.IntVal = Op0.IntVal.Xor(Op1.IntVal); break;
1262 case Instruction::Shl:
1263 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1265 case Instruction::LShr:
1266 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1268 case Instruction::AShr:
1269 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1272 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
1274 return GenericValue();
1279 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1280 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1281 return getConstantExprValue(CE, SF);
1282 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1283 return getConstantValue(CPV);
1284 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1285 return PTOGV(getPointerToGlobal(GV));
1287 return SF.Values[V];
1291 //===----------------------------------------------------------------------===//
1292 // Dispatch and Execution Code
1293 //===----------------------------------------------------------------------===//
1295 //===----------------------------------------------------------------------===//
1296 // callFunction - Execute the specified function...
1298 void Interpreter::callFunction(Function *F,
1299 const std::vector<GenericValue> &ArgVals) {
1300 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1301 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1302 "Incorrect number of arguments passed into function call!");
1303 // Make a new stack frame... and fill it in.
1304 ECStack.push_back(ExecutionContext());
1305 ExecutionContext &StackFrame = ECStack.back();
1306 StackFrame.CurFunction = F;
1308 // Special handling for external functions.
1309 if (F->isDeclaration()) {
1310 GenericValue Result = callExternalFunction (F, ArgVals);
1311 // Simulate a 'ret' instruction of the appropriate type.
1312 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1316 // Get pointers to first LLVM BB & Instruction in function.
1317 StackFrame.CurBB = F->begin();
1318 StackFrame.CurInst = StackFrame.CurBB->begin();
1320 // Run through the function arguments and initialize their values...
1321 assert((ArgVals.size() == F->arg_size() ||
1322 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1323 "Invalid number of values passed to function invocation!");
1325 // Handle non-varargs arguments...
1327 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1329 SetValue(AI, ArgVals[i], StackFrame);
1331 // Handle varargs arguments...
1332 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1336 void Interpreter::run() {
1337 while (!ECStack.empty()) {
1338 // Interpret a single instruction & increment the "PC".
1339 ExecutionContext &SF = ECStack.back(); // Current stack frame
1340 Instruction &I = *SF.CurInst++; // Increment before execute
1342 // Track the number of dynamic instructions executed.
1345 DOUT << "About to interpret: " << I;
1346 visit(I); // Dispatch to one of the visit* methods...
1348 // This is not safe, as visiting the instruction could lower it and free I.
1350 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1351 I.getType() != Type::VoidTy) {
1353 const GenericValue &Val = SF.Values[&I];
1354 switch (I.getType()->getTypeID()) {
1355 default: assert(0 && "Invalid GenericValue Type");
1356 case Type::VoidTyID: DOUT << "void"; break;
1357 case Type::FloatTyID: DOUT << "float " << Val.FloatVal; break;
1358 case Type::DoubleTyID: DOUT << "double " << Val.DoubleVal; break;
1359 case Type::PointerTyID: DOUT << "void* " << intptr_t(Val.PointerVal);
1361 case Type::IntegerTyID:
1362 DOUT << "i" << Val.IntVal.getBitWidth() << " "
1363 << Val.IntVal.toStringUnsigned(10)
1364 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";