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/ErrorHandling.h"
26 #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 void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
44 //===----------------------------------------------------------------------===//
45 // Binary Instruction Implementations
46 //===----------------------------------------------------------------------===//
48 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
49 case Type::TY##TyID: \
50 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
53 static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
54 GenericValue Src2, Type *Ty) {
55 switch (Ty->getTypeID()) {
56 IMPLEMENT_BINARY_OPERATOR(+, Float);
57 IMPLEMENT_BINARY_OPERATOR(+, Double);
59 dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n";
64 static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
65 GenericValue Src2, Type *Ty) {
66 switch (Ty->getTypeID()) {
67 IMPLEMENT_BINARY_OPERATOR(-, Float);
68 IMPLEMENT_BINARY_OPERATOR(-, Double);
70 dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n";
75 static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
76 GenericValue Src2, Type *Ty) {
77 switch (Ty->getTypeID()) {
78 IMPLEMENT_BINARY_OPERATOR(*, Float);
79 IMPLEMENT_BINARY_OPERATOR(*, Double);
81 dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n";
86 static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
87 GenericValue Src2, Type *Ty) {
88 switch (Ty->getTypeID()) {
89 IMPLEMENT_BINARY_OPERATOR(/, Float);
90 IMPLEMENT_BINARY_OPERATOR(/, Double);
92 dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n";
97 static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
98 GenericValue Src2, Type *Ty) {
99 switch (Ty->getTypeID()) {
100 case Type::FloatTyID:
101 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
103 case Type::DoubleTyID:
104 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
107 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
112 #define IMPLEMENT_INTEGER_ICMP(OP, TY) \
113 case Type::IntegerTyID: \
114 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
117 // Handle pointers specially because they must be compared with only as much
118 // width as the host has. We _do not_ want to be comparing 64 bit values when
119 // running on a 32-bit target, otherwise the upper 32 bits might mess up
120 // comparisons if they contain garbage.
121 #define IMPLEMENT_POINTER_ICMP(OP) \
122 case Type::PointerTyID: \
123 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
124 (void*)(intptr_t)Src2.PointerVal); \
127 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
130 switch (Ty->getTypeID()) {
131 IMPLEMENT_INTEGER_ICMP(eq,Ty);
132 IMPLEMENT_POINTER_ICMP(==);
134 dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
140 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
143 switch (Ty->getTypeID()) {
144 IMPLEMENT_INTEGER_ICMP(ne,Ty);
145 IMPLEMENT_POINTER_ICMP(!=);
147 dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
153 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
156 switch (Ty->getTypeID()) {
157 IMPLEMENT_INTEGER_ICMP(ult,Ty);
158 IMPLEMENT_POINTER_ICMP(<);
160 dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
166 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
169 switch (Ty->getTypeID()) {
170 IMPLEMENT_INTEGER_ICMP(slt,Ty);
171 IMPLEMENT_POINTER_ICMP(<);
173 dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
179 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
182 switch (Ty->getTypeID()) {
183 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
184 IMPLEMENT_POINTER_ICMP(>);
186 dbgs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
192 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
195 switch (Ty->getTypeID()) {
196 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
197 IMPLEMENT_POINTER_ICMP(>);
199 dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
205 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
208 switch (Ty->getTypeID()) {
209 IMPLEMENT_INTEGER_ICMP(ule,Ty);
210 IMPLEMENT_POINTER_ICMP(<=);
212 dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
218 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
221 switch (Ty->getTypeID()) {
222 IMPLEMENT_INTEGER_ICMP(sle,Ty);
223 IMPLEMENT_POINTER_ICMP(<=);
225 dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
231 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
234 switch (Ty->getTypeID()) {
235 IMPLEMENT_INTEGER_ICMP(uge,Ty);
236 IMPLEMENT_POINTER_ICMP(>=);
238 dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
244 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
247 switch (Ty->getTypeID()) {
248 IMPLEMENT_INTEGER_ICMP(sge,Ty);
249 IMPLEMENT_POINTER_ICMP(>=);
251 dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
257 void Interpreter::visitICmpInst(ICmpInst &I) {
258 ExecutionContext &SF = ECStack.back();
259 Type *Ty = I.getOperand(0)->getType();
260 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
261 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
262 GenericValue R; // Result
264 switch (I.getPredicate()) {
265 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
266 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
267 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
268 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
269 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
270 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
271 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
272 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
273 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
274 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
276 dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I;
283 #define IMPLEMENT_FCMP(OP, TY) \
284 case Type::TY##TyID: \
285 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
288 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
291 switch (Ty->getTypeID()) {
292 IMPLEMENT_FCMP(==, Float);
293 IMPLEMENT_FCMP(==, Double);
295 dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
301 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
304 switch (Ty->getTypeID()) {
305 IMPLEMENT_FCMP(!=, Float);
306 IMPLEMENT_FCMP(!=, Double);
309 dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
315 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
318 switch (Ty->getTypeID()) {
319 IMPLEMENT_FCMP(<=, Float);
320 IMPLEMENT_FCMP(<=, Double);
322 dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
328 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
331 switch (Ty->getTypeID()) {
332 IMPLEMENT_FCMP(>=, Float);
333 IMPLEMENT_FCMP(>=, Double);
335 dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
341 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
344 switch (Ty->getTypeID()) {
345 IMPLEMENT_FCMP(<, Float);
346 IMPLEMENT_FCMP(<, Double);
348 dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
354 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
357 switch (Ty->getTypeID()) {
358 IMPLEMENT_FCMP(>, Float);
359 IMPLEMENT_FCMP(>, Double);
361 dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
367 #define IMPLEMENT_UNORDERED(TY, X,Y) \
368 if (TY->isFloatTy()) { \
369 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
370 Dest.IntVal = APInt(1,true); \
373 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
374 Dest.IntVal = APInt(1,true); \
379 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
382 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
383 return executeFCMP_OEQ(Src1, Src2, Ty);
386 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
389 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
390 return executeFCMP_ONE(Src1, Src2, Ty);
393 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
396 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
397 return executeFCMP_OLE(Src1, Src2, Ty);
400 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
403 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
404 return executeFCMP_OGE(Src1, Src2, Ty);
407 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
410 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
411 return executeFCMP_OLT(Src1, Src2, Ty);
414 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
417 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
418 return executeFCMP_OGT(Src1, Src2, Ty);
421 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
425 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
426 Src2.FloatVal == Src2.FloatVal));
428 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
429 Src2.DoubleVal == Src2.DoubleVal));
433 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
437 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
438 Src2.FloatVal != Src2.FloatVal));
440 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
441 Src2.DoubleVal != Src2.DoubleVal));
445 void Interpreter::visitFCmpInst(FCmpInst &I) {
446 ExecutionContext &SF = ECStack.back();
447 Type *Ty = I.getOperand(0)->getType();
448 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
449 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
450 GenericValue R; // Result
452 switch (I.getPredicate()) {
453 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
454 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
455 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
456 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
457 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
458 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
459 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
460 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
461 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
462 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
463 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
464 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
465 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
466 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
467 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
468 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
470 dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I;
477 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
478 GenericValue Src2, Type *Ty) {
481 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
482 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
483 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
484 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
485 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
486 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
487 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
488 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
489 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
490 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
491 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
492 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
493 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
494 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
495 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
496 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
497 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
498 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
499 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
500 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
501 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
502 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
503 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
504 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
505 case FCmpInst::FCMP_FALSE: {
507 Result.IntVal = APInt(1, false);
510 case FCmpInst::FCMP_TRUE: {
512 Result.IntVal = APInt(1, true);
516 dbgs() << "Unhandled Cmp predicate\n";
521 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
522 ExecutionContext &SF = ECStack.back();
523 Type *Ty = I.getOperand(0)->getType();
524 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
525 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
526 GenericValue R; // Result
528 switch (I.getOpcode()) {
529 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
530 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
531 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
532 case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;
533 case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;
534 case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;
535 case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;
536 case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;
537 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
538 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
539 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
540 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
541 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
542 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
543 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
545 dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
552 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
554 return Src1.IntVal == 0 ? Src3 : Src2;
557 void Interpreter::visitSelectInst(SelectInst &I) {
558 ExecutionContext &SF = ECStack.back();
559 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
560 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
561 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
562 GenericValue R = executeSelectInst(Src1, Src2, Src3);
567 //===----------------------------------------------------------------------===//
568 // Terminator Instruction Implementations
569 //===----------------------------------------------------------------------===//
571 void Interpreter::exitCalled(GenericValue GV) {
572 // runAtExitHandlers() assumes there are no stack frames, but
573 // if exit() was called, then it had a stack frame. Blow away
574 // the stack before interpreting atexit handlers.
577 exit(GV.IntVal.zextOrTrunc(32).getZExtValue());
580 /// Pop the last stack frame off of ECStack and then copy the result
581 /// back into the result variable if we are not returning void. The
582 /// result variable may be the ExitValue, or the Value of the calling
583 /// CallInst if there was a previous stack frame. This method may
584 /// invalidate any ECStack iterators you have. This method also takes
585 /// care of switching to the normal destination BB, if we are returning
588 void Interpreter::popStackAndReturnValueToCaller(Type *RetTy,
589 GenericValue Result) {
590 // Pop the current stack frame.
593 if (ECStack.empty()) { // Finished main. Put result into exit code...
594 if (RetTy && !RetTy->isVoidTy()) { // Nonvoid return type?
595 ExitValue = Result; // Capture the exit value of the program
597 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
600 // If we have a previous stack frame, and we have a previous call,
601 // fill in the return value...
602 ExecutionContext &CallingSF = ECStack.back();
603 if (Instruction *I = CallingSF.Caller.getInstruction()) {
605 if (!CallingSF.Caller.getType()->isVoidTy())
606 SetValue(I, Result, CallingSF);
607 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
608 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
609 CallingSF.Caller = CallSite(); // We returned from the call...
614 void Interpreter::visitReturnInst(ReturnInst &I) {
615 ExecutionContext &SF = ECStack.back();
616 Type *RetTy = Type::getVoidTy(I.getContext());
619 // Save away the return value... (if we are not 'ret void')
620 if (I.getNumOperands()) {
621 RetTy = I.getReturnValue()->getType();
622 Result = getOperandValue(I.getReturnValue(), SF);
625 popStackAndReturnValueToCaller(RetTy, Result);
628 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
629 report_fatal_error("Program executed an 'unreachable' instruction!");
632 void Interpreter::visitBranchInst(BranchInst &I) {
633 ExecutionContext &SF = ECStack.back();
636 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
637 if (!I.isUnconditional()) {
638 Value *Cond = I.getCondition();
639 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
640 Dest = I.getSuccessor(1);
642 SwitchToNewBasicBlock(Dest, SF);
645 void Interpreter::visitSwitchInst(SwitchInst &I) {
646 ExecutionContext &SF = ECStack.back();
647 Value* Cond = I.getCondition();
648 Type *ElTy = Cond->getType();
649 GenericValue CondVal = getOperandValue(Cond, SF);
651 // Check to see if any of the cases match...
652 BasicBlock *Dest = 0;
653 for (SwitchInst::CaseIt i = I.case_begin(), e = I.case_end(); i != e; ++i) {
654 IntegersSubset& Case = i.getCaseValueEx();
655 if (Case.isSingleNumber()) {
656 // FIXME: Currently work with ConstantInt based numbers.
657 const ConstantInt *CI = Case.getSingleNumber(0).toConstantInt();
658 GenericValue Val = getOperandValue(const_cast<ConstantInt*>(CI), SF);
659 if (executeICMP_EQ(Val, CondVal, ElTy).IntVal != 0) {
660 Dest = cast<BasicBlock>(i.getCaseSuccessor());
664 if (Case.isSingleNumbersOnly()) {
665 for (unsigned n = 0, en = Case.getNumItems(); n != en; ++n) {
666 // FIXME: Currently work with ConstantInt based numbers.
667 const ConstantInt *CI = Case.getSingleNumber(n).toConstantInt();
668 GenericValue Val = getOperandValue(const_cast<ConstantInt*>(CI), SF);
669 if (executeICMP_EQ(Val, CondVal, ElTy).IntVal != 0) {
670 Dest = cast<BasicBlock>(i.getCaseSuccessor());
675 for (unsigned n = 0, en = Case.getNumItems(); n != en; ++n) {
676 IntegersSubset::Range r = Case.getItem(n);
677 // FIXME: Currently work with ConstantInt based numbers.
678 const ConstantInt *LowCI = r.getLow().toConstantInt();
679 const ConstantInt *HighCI = r.getHigh().toConstantInt();
680 GenericValue Low = getOperandValue(const_cast<ConstantInt*>(LowCI), SF);
681 GenericValue High = getOperandValue(const_cast<ConstantInt*>(HighCI), SF);
682 if (executeICMP_ULE(Low, CondVal, ElTy).IntVal != 0 &&
683 executeICMP_ULE(CondVal, High, ElTy).IntVal != 0) {
684 Dest = cast<BasicBlock>(i.getCaseSuccessor());
689 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
690 SwitchToNewBasicBlock(Dest, SF);
693 void Interpreter::visitIndirectBrInst(IndirectBrInst &I) {
694 ExecutionContext &SF = ECStack.back();
695 void *Dest = GVTOP(getOperandValue(I.getAddress(), SF));
696 SwitchToNewBasicBlock((BasicBlock*)Dest, SF);
700 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
701 // This function handles the actual updating of block and instruction iterators
702 // as well as execution of all of the PHI nodes in the destination block.
704 // This method does this because all of the PHI nodes must be executed
705 // atomically, reading their inputs before any of the results are updated. Not
706 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
707 // their inputs. If the input PHI node is updated before it is read, incorrect
708 // results can happen. Thus we use a two phase approach.
710 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
711 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
712 SF.CurBB = Dest; // Update CurBB to branch destination
713 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
715 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
717 // Loop over all of the PHI nodes in the current block, reading their inputs.
718 std::vector<GenericValue> ResultValues;
720 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
721 // Search for the value corresponding to this previous bb...
722 int i = PN->getBasicBlockIndex(PrevBB);
723 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
724 Value *IncomingValue = PN->getIncomingValue(i);
726 // Save the incoming value for this PHI node...
727 ResultValues.push_back(getOperandValue(IncomingValue, SF));
730 // Now loop over all of the PHI nodes setting their values...
731 SF.CurInst = SF.CurBB->begin();
732 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
733 PHINode *PN = cast<PHINode>(SF.CurInst);
734 SetValue(PN, ResultValues[i], SF);
738 //===----------------------------------------------------------------------===//
739 // Memory Instruction Implementations
740 //===----------------------------------------------------------------------===//
742 void Interpreter::visitAllocaInst(AllocaInst &I) {
743 ExecutionContext &SF = ECStack.back();
745 Type *Ty = I.getType()->getElementType(); // Type to be allocated
747 // Get the number of elements being allocated by the array...
748 unsigned NumElements =
749 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
751 unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
753 // Avoid malloc-ing zero bytes, use max()...
754 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
756 // Allocate enough memory to hold the type...
757 void *Memory = malloc(MemToAlloc);
759 DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
760 << NumElements << " (Total: " << MemToAlloc << ") at "
761 << uintptr_t(Memory) << '\n');
763 GenericValue Result = PTOGV(Memory);
764 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
765 SetValue(&I, Result, SF);
767 if (I.getOpcode() == Instruction::Alloca)
768 ECStack.back().Allocas.add(Memory);
771 // getElementOffset - The workhorse for getelementptr.
773 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
775 ExecutionContext &SF) {
776 assert(Ptr->getType()->isPointerTy() &&
777 "Cannot getElementOffset of a nonpointer type!");
781 for (; I != E; ++I) {
782 if (StructType *STy = dyn_cast<StructType>(*I)) {
783 const StructLayout *SLO = TD.getStructLayout(STy);
785 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
786 unsigned Index = unsigned(CPU->getZExtValue());
788 Total += SLO->getElementOffset(Index);
790 SequentialType *ST = cast<SequentialType>(*I);
791 // Get the index number for the array... which must be long type...
792 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
796 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
798 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
800 assert(BitWidth == 64 && "Invalid index type for getelementptr");
801 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
803 Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
808 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
809 DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n");
813 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
814 ExecutionContext &SF = ECStack.back();
815 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
816 gep_type_begin(I), gep_type_end(I), SF), SF);
819 void Interpreter::visitLoadInst(LoadInst &I) {
820 ExecutionContext &SF = ECStack.back();
821 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
822 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
824 LoadValueFromMemory(Result, Ptr, I.getType());
825 SetValue(&I, Result, SF);
826 if (I.isVolatile() && PrintVolatile)
827 dbgs() << "Volatile load " << I;
830 void Interpreter::visitStoreInst(StoreInst &I) {
831 ExecutionContext &SF = ECStack.back();
832 GenericValue Val = getOperandValue(I.getOperand(0), SF);
833 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
834 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
835 I.getOperand(0)->getType());
836 if (I.isVolatile() && PrintVolatile)
837 dbgs() << "Volatile store: " << I;
840 //===----------------------------------------------------------------------===//
841 // Miscellaneous Instruction Implementations
842 //===----------------------------------------------------------------------===//
844 void Interpreter::visitCallSite(CallSite CS) {
845 ExecutionContext &SF = ECStack.back();
847 // Check to see if this is an intrinsic function call...
848 Function *F = CS.getCalledFunction();
849 if (F && F->isDeclaration())
850 switch (F->getIntrinsicID()) {
851 case Intrinsic::not_intrinsic:
853 case Intrinsic::vastart: { // va_start
854 GenericValue ArgIndex;
855 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
856 ArgIndex.UIntPairVal.second = 0;
857 SetValue(CS.getInstruction(), ArgIndex, SF);
860 case Intrinsic::vaend: // va_end is a noop for the interpreter
862 case Intrinsic::vacopy: // va_copy: dest = src
863 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
866 // If it is an unknown intrinsic function, use the intrinsic lowering
867 // class to transform it into hopefully tasty LLVM code.
869 BasicBlock::iterator me(CS.getInstruction());
870 BasicBlock *Parent = CS.getInstruction()->getParent();
871 bool atBegin(Parent->begin() == me);
874 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
876 // Restore the CurInst pointer to the first instruction newly inserted, if
879 SF.CurInst = Parent->begin();
889 std::vector<GenericValue> ArgVals;
890 const unsigned NumArgs = SF.Caller.arg_size();
891 ArgVals.reserve(NumArgs);
893 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
894 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
896 ArgVals.push_back(getOperandValue(V, SF));
899 // To handle indirect calls, we must get the pointer value from the argument
900 // and treat it as a function pointer.
901 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
902 callFunction((Function*)GVTOP(SRC), ArgVals);
905 void Interpreter::visitShl(BinaryOperator &I) {
906 ExecutionContext &SF = ECStack.back();
907 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
908 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
910 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
911 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
913 Dest.IntVal = Src1.IntVal;
915 SetValue(&I, Dest, SF);
918 void Interpreter::visitLShr(BinaryOperator &I) {
919 ExecutionContext &SF = ECStack.back();
920 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
921 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
923 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
924 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
926 Dest.IntVal = Src1.IntVal;
928 SetValue(&I, Dest, SF);
931 void Interpreter::visitAShr(BinaryOperator &I) {
932 ExecutionContext &SF = ECStack.back();
933 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
934 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
936 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
937 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
939 Dest.IntVal = Src1.IntVal;
941 SetValue(&I, Dest, SF);
944 GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy,
945 ExecutionContext &SF) {
946 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
947 IntegerType *DITy = cast<IntegerType>(DstTy);
948 unsigned DBitWidth = DITy->getBitWidth();
949 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
953 GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy,
954 ExecutionContext &SF) {
955 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
956 IntegerType *DITy = cast<IntegerType>(DstTy);
957 unsigned DBitWidth = DITy->getBitWidth();
958 Dest.IntVal = Src.IntVal.sext(DBitWidth);
962 GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy,
963 ExecutionContext &SF) {
964 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
965 IntegerType *DITy = cast<IntegerType>(DstTy);
966 unsigned DBitWidth = DITy->getBitWidth();
967 Dest.IntVal = Src.IntVal.zext(DBitWidth);
971 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy,
972 ExecutionContext &SF) {
973 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
974 assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
975 "Invalid FPTrunc instruction");
976 Dest.FloatVal = (float) Src.DoubleVal;
980 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy,
981 ExecutionContext &SF) {
982 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
983 assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&
984 "Invalid FPTrunc instruction");
985 Dest.DoubleVal = (double) Src.FloatVal;
989 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy,
990 ExecutionContext &SF) {
991 Type *SrcTy = SrcVal->getType();
992 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
993 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
994 assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction");
996 if (SrcTy->getTypeID() == Type::FloatTyID)
997 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
999 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1003 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy,
1004 ExecutionContext &SF) {
1005 Type *SrcTy = SrcVal->getType();
1006 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1007 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1008 assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1010 if (SrcTy->getTypeID() == Type::FloatTyID)
1011 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1013 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1017 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy,
1018 ExecutionContext &SF) {
1019 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1020 assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1022 if (DstTy->getTypeID() == Type::FloatTyID)
1023 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
1025 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
1029 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy,
1030 ExecutionContext &SF) {
1031 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1032 assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1034 if (DstTy->getTypeID() == Type::FloatTyID)
1035 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
1037 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
1042 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy,
1043 ExecutionContext &SF) {
1044 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1045 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1046 assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction");
1048 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1052 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy,
1053 ExecutionContext &SF) {
1054 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1055 assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction");
1057 uint32_t PtrSize = TD.getPointerSizeInBits();
1058 if (PtrSize != Src.IntVal.getBitWidth())
1059 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
1061 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
1065 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy,
1066 ExecutionContext &SF) {
1068 Type *SrcTy = SrcVal->getType();
1069 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1070 if (DstTy->isPointerTy()) {
1071 assert(SrcTy->isPointerTy() && "Invalid BitCast");
1072 Dest.PointerVal = Src.PointerVal;
1073 } else if (DstTy->isIntegerTy()) {
1074 if (SrcTy->isFloatTy()) {
1075 Dest.IntVal = APInt::floatToBits(Src.FloatVal);
1076 } else if (SrcTy->isDoubleTy()) {
1077 Dest.IntVal = APInt::doubleToBits(Src.DoubleVal);
1078 } else if (SrcTy->isIntegerTy()) {
1079 Dest.IntVal = Src.IntVal;
1081 llvm_unreachable("Invalid BitCast");
1082 } else if (DstTy->isFloatTy()) {
1083 if (SrcTy->isIntegerTy())
1084 Dest.FloatVal = Src.IntVal.bitsToFloat();
1086 Dest.FloatVal = Src.FloatVal;
1087 } else if (DstTy->isDoubleTy()) {
1088 if (SrcTy->isIntegerTy())
1089 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1091 Dest.DoubleVal = Src.DoubleVal;
1093 llvm_unreachable("Invalid Bitcast");
1098 void Interpreter::visitTruncInst(TruncInst &I) {
1099 ExecutionContext &SF = ECStack.back();
1100 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1103 void Interpreter::visitSExtInst(SExtInst &I) {
1104 ExecutionContext &SF = ECStack.back();
1105 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1108 void Interpreter::visitZExtInst(ZExtInst &I) {
1109 ExecutionContext &SF = ECStack.back();
1110 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1113 void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1114 ExecutionContext &SF = ECStack.back();
1115 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1118 void Interpreter::visitFPExtInst(FPExtInst &I) {
1119 ExecutionContext &SF = ECStack.back();
1120 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1123 void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1124 ExecutionContext &SF = ECStack.back();
1125 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1128 void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1129 ExecutionContext &SF = ECStack.back();
1130 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1133 void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1134 ExecutionContext &SF = ECStack.back();
1135 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1138 void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1139 ExecutionContext &SF = ECStack.back();
1140 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1143 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1144 ExecutionContext &SF = ECStack.back();
1145 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1148 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1149 ExecutionContext &SF = ECStack.back();
1150 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1153 void Interpreter::visitBitCastInst(BitCastInst &I) {
1154 ExecutionContext &SF = ECStack.back();
1155 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1158 #define IMPLEMENT_VAARG(TY) \
1159 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1161 void Interpreter::visitVAArgInst(VAArgInst &I) {
1162 ExecutionContext &SF = ECStack.back();
1164 // Get the incoming valist parameter. LLI treats the valist as a
1165 // (ec-stack-depth var-arg-index) pair.
1166 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1168 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1169 .VarArgs[VAList.UIntPairVal.second];
1170 Type *Ty = I.getType();
1171 switch (Ty->getTypeID()) {
1172 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
1173 IMPLEMENT_VAARG(Pointer);
1174 IMPLEMENT_VAARG(Float);
1175 IMPLEMENT_VAARG(Double);
1177 dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1178 llvm_unreachable(0);
1181 // Set the Value of this Instruction.
1182 SetValue(&I, Dest, SF);
1184 // Move the pointer to the next vararg.
1185 ++VAList.UIntPairVal.second;
1188 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1189 ExecutionContext &SF) {
1190 switch (CE->getOpcode()) {
1191 case Instruction::Trunc:
1192 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1193 case Instruction::ZExt:
1194 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1195 case Instruction::SExt:
1196 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1197 case Instruction::FPTrunc:
1198 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1199 case Instruction::FPExt:
1200 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1201 case Instruction::UIToFP:
1202 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1203 case Instruction::SIToFP:
1204 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1205 case Instruction::FPToUI:
1206 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1207 case Instruction::FPToSI:
1208 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1209 case Instruction::PtrToInt:
1210 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1211 case Instruction::IntToPtr:
1212 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1213 case Instruction::BitCast:
1214 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1215 case Instruction::GetElementPtr:
1216 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1217 gep_type_end(CE), SF);
1218 case Instruction::FCmp:
1219 case Instruction::ICmp:
1220 return executeCmpInst(CE->getPredicate(),
1221 getOperandValue(CE->getOperand(0), SF),
1222 getOperandValue(CE->getOperand(1), SF),
1223 CE->getOperand(0)->getType());
1224 case Instruction::Select:
1225 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1226 getOperandValue(CE->getOperand(1), SF),
1227 getOperandValue(CE->getOperand(2), SF));
1232 // The cases below here require a GenericValue parameter for the result
1233 // so we initialize one, compute it and then return it.
1234 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1235 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
1237 Type * Ty = CE->getOperand(0)->getType();
1238 switch (CE->getOpcode()) {
1239 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
1240 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
1241 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
1242 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
1243 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
1244 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
1245 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1246 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1247 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1248 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1249 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1250 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
1251 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
1252 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
1253 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
1254 case Instruction::Shl:
1255 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1257 case Instruction::LShr:
1258 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1260 case Instruction::AShr:
1261 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1264 dbgs() << "Unhandled ConstantExpr: " << *CE << "\n";
1265 llvm_unreachable("Unhandled ConstantExpr");
1270 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1271 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1272 return getConstantExprValue(CE, SF);
1273 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1274 return getConstantValue(CPV);
1275 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1276 return PTOGV(getPointerToGlobal(GV));
1278 return SF.Values[V];
1282 //===----------------------------------------------------------------------===//
1283 // Dispatch and Execution Code
1284 //===----------------------------------------------------------------------===//
1286 //===----------------------------------------------------------------------===//
1287 // callFunction - Execute the specified function...
1289 void Interpreter::callFunction(Function *F,
1290 const std::vector<GenericValue> &ArgVals) {
1291 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1292 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1293 "Incorrect number of arguments passed into function call!");
1294 // Make a new stack frame... and fill it in.
1295 ECStack.push_back(ExecutionContext());
1296 ExecutionContext &StackFrame = ECStack.back();
1297 StackFrame.CurFunction = F;
1299 // Special handling for external functions.
1300 if (F->isDeclaration()) {
1301 GenericValue Result = callExternalFunction (F, ArgVals);
1302 // Simulate a 'ret' instruction of the appropriate type.
1303 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1307 // Get pointers to first LLVM BB & Instruction in function.
1308 StackFrame.CurBB = F->begin();
1309 StackFrame.CurInst = StackFrame.CurBB->begin();
1311 // Run through the function arguments and initialize their values...
1312 assert((ArgVals.size() == F->arg_size() ||
1313 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1314 "Invalid number of values passed to function invocation!");
1316 // Handle non-varargs arguments...
1318 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1320 SetValue(AI, ArgVals[i], StackFrame);
1322 // Handle varargs arguments...
1323 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1327 void Interpreter::run() {
1328 while (!ECStack.empty()) {
1329 // Interpret a single instruction & increment the "PC".
1330 ExecutionContext &SF = ECStack.back(); // Current stack frame
1331 Instruction &I = *SF.CurInst++; // Increment before execute
1333 // Track the number of dynamic instructions executed.
1336 DEBUG(dbgs() << "About to interpret: " << I);
1337 visit(I); // Dispatch to one of the visit* methods...
1339 // This is not safe, as visiting the instruction could lower it and free I.
1341 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1342 I.getType() != Type::VoidTy) {
1344 const GenericValue &Val = SF.Values[&I];
1345 switch (I.getType()->getTypeID()) {
1346 default: llvm_unreachable("Invalid GenericValue Type");
1347 case Type::VoidTyID: dbgs() << "void"; break;
1348 case Type::FloatTyID: dbgs() << "float " << Val.FloatVal; break;
1349 case Type::DoubleTyID: dbgs() << "double " << Val.DoubleVal; break;
1350 case Type::PointerTyID: dbgs() << "void* " << intptr_t(Val.PointerVal);
1352 case Type::IntegerTyID:
1353 dbgs() << "i" << Val.IntVal.getBitWidth() << " "
1354 << Val.IntVal.toStringUnsigned(10)
1355 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";