1 //===-- Execution.cpp - Implement code to simulate the program ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/Statistic.h"
22 #include "llvm/Support/Debug.h"
23 #include <cmath> // For fmod
27 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
29 Interpreter *TheEE = 0;
33 //===----------------------------------------------------------------------===//
34 // Value Manipulation code
35 //===----------------------------------------------------------------------===//
37 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
39 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
41 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
43 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
45 static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
47 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
49 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
51 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
53 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
55 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
57 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
59 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
61 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
63 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
65 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
67 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
69 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
71 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
73 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
76 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
77 ExecutionContext &SF) {
78 switch (CE->getOpcode()) {
79 case Instruction::Cast:
80 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
81 case Instruction::GetElementPtr:
82 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
83 gep_type_end(CE), SF);
84 case Instruction::Add:
85 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
86 getOperandValue(CE->getOperand(1), SF),
87 CE->getOperand(0)->getType());
88 case Instruction::Sub:
89 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
90 getOperandValue(CE->getOperand(1), SF),
91 CE->getOperand(0)->getType());
92 case Instruction::Mul:
93 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
94 getOperandValue(CE->getOperand(1), SF),
95 CE->getOperand(0)->getType());
96 case Instruction::SDiv:
97 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
98 getOperandValue(CE->getOperand(1), SF),
99 CE->getOperand(0)->getType());
100 case Instruction::UDiv:
101 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
102 getOperandValue(CE->getOperand(1), SF),
103 CE->getOperand(0)->getType());
104 case Instruction::FDiv:
105 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
106 getOperandValue(CE->getOperand(1), SF),
107 CE->getOperand(0)->getType());
108 case Instruction::Rem:
109 return executeRemInst(getOperandValue(CE->getOperand(0), SF),
110 getOperandValue(CE->getOperand(1), SF),
111 CE->getOperand(0)->getType());
112 case Instruction::And:
113 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
114 getOperandValue(CE->getOperand(1), SF),
115 CE->getOperand(0)->getType());
116 case Instruction::Or:
117 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
118 getOperandValue(CE->getOperand(1), SF),
119 CE->getOperand(0)->getType());
120 case Instruction::Xor:
121 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
122 getOperandValue(CE->getOperand(1), SF),
123 CE->getOperand(0)->getType());
124 case Instruction::SetEQ:
125 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
126 getOperandValue(CE->getOperand(1), SF),
127 CE->getOperand(0)->getType());
128 case Instruction::SetNE:
129 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
130 getOperandValue(CE->getOperand(1), SF),
131 CE->getOperand(0)->getType());
132 case Instruction::SetLE:
133 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
134 getOperandValue(CE->getOperand(1), SF),
135 CE->getOperand(0)->getType());
136 case Instruction::SetGE:
137 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
138 getOperandValue(CE->getOperand(1), SF),
139 CE->getOperand(0)->getType());
140 case Instruction::SetLT:
141 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
142 getOperandValue(CE->getOperand(1), SF),
143 CE->getOperand(0)->getType());
144 case Instruction::SetGT:
145 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
146 getOperandValue(CE->getOperand(1), SF),
147 CE->getOperand(0)->getType());
148 case Instruction::Shl:
149 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
150 getOperandValue(CE->getOperand(1), SF),
151 CE->getOperand(0)->getType());
152 case Instruction::Shr:
153 return executeShrInst(getOperandValue(CE->getOperand(0), SF),
154 getOperandValue(CE->getOperand(1), SF),
155 CE->getOperand(0)->getType());
156 case Instruction::Select:
157 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
158 getOperandValue(CE->getOperand(1), SF),
159 getOperandValue(CE->getOperand(2), SF));
161 std::cerr << "Unhandled ConstantExpr: " << *CE << "\n";
163 return GenericValue();
167 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
168 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
169 return getConstantExprValue(CE, SF);
170 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
171 return getConstantValue(CPV);
172 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
173 return PTOGV(getPointerToGlobal(GV));
179 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
183 void Interpreter::initializeExecutionEngine() {
187 //===----------------------------------------------------------------------===//
188 // Binary Instruction Implementations
189 //===----------------------------------------------------------------------===//
191 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
192 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
194 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
197 switch (Ty->getTypeID()) {
198 IMPLEMENT_BINARY_OPERATOR(+, UByte);
199 IMPLEMENT_BINARY_OPERATOR(+, SByte);
200 IMPLEMENT_BINARY_OPERATOR(+, UShort);
201 IMPLEMENT_BINARY_OPERATOR(+, Short);
202 IMPLEMENT_BINARY_OPERATOR(+, UInt);
203 IMPLEMENT_BINARY_OPERATOR(+, Int);
204 IMPLEMENT_BINARY_OPERATOR(+, ULong);
205 IMPLEMENT_BINARY_OPERATOR(+, Long);
206 IMPLEMENT_BINARY_OPERATOR(+, Float);
207 IMPLEMENT_BINARY_OPERATOR(+, Double);
209 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
215 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
218 switch (Ty->getTypeID()) {
219 IMPLEMENT_BINARY_OPERATOR(-, UByte);
220 IMPLEMENT_BINARY_OPERATOR(-, SByte);
221 IMPLEMENT_BINARY_OPERATOR(-, UShort);
222 IMPLEMENT_BINARY_OPERATOR(-, Short);
223 IMPLEMENT_BINARY_OPERATOR(-, UInt);
224 IMPLEMENT_BINARY_OPERATOR(-, Int);
225 IMPLEMENT_BINARY_OPERATOR(-, ULong);
226 IMPLEMENT_BINARY_OPERATOR(-, Long);
227 IMPLEMENT_BINARY_OPERATOR(-, Float);
228 IMPLEMENT_BINARY_OPERATOR(-, Double);
230 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
236 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
239 switch (Ty->getTypeID()) {
240 IMPLEMENT_BINARY_OPERATOR(*, UByte);
241 IMPLEMENT_BINARY_OPERATOR(*, SByte);
242 IMPLEMENT_BINARY_OPERATOR(*, UShort);
243 IMPLEMENT_BINARY_OPERATOR(*, Short);
244 IMPLEMENT_BINARY_OPERATOR(*, UInt);
245 IMPLEMENT_BINARY_OPERATOR(*, Int);
246 IMPLEMENT_BINARY_OPERATOR(*, ULong);
247 IMPLEMENT_BINARY_OPERATOR(*, Long);
248 IMPLEMENT_BINARY_OPERATOR(*, Float);
249 IMPLEMENT_BINARY_OPERATOR(*, Double);
251 std::cout << "Unhandled type for Mul instruction: " << *Ty << "\n";
257 #define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
258 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
260 static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
264 Ty = Ty->getUnsignedVersion();
265 switch (Ty->getTypeID()) {
266 IMPLEMENT_SIGNLESS_BINOP(/, UByte, SByte);
267 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
268 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
269 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
271 std::cout << "Unhandled type for UDiv instruction: " << *Ty << "\n";
277 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
280 if (Ty->isUnsigned())
281 Ty = Ty->getSignedVersion();
282 switch (Ty->getTypeID()) {
283 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
284 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
285 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
286 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
288 std::cout << "Unhandled type for SDiv instruction: " << *Ty << "\n";
294 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
297 switch (Ty->getTypeID()) {
298 IMPLEMENT_BINARY_OPERATOR(/, Float);
299 IMPLEMENT_BINARY_OPERATOR(/, Double);
301 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
307 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
310 switch (Ty->getTypeID()) {
311 IMPLEMENT_BINARY_OPERATOR(%, UByte);
312 IMPLEMENT_BINARY_OPERATOR(%, SByte);
313 IMPLEMENT_BINARY_OPERATOR(%, UShort);
314 IMPLEMENT_BINARY_OPERATOR(%, Short);
315 IMPLEMENT_BINARY_OPERATOR(%, UInt);
316 IMPLEMENT_BINARY_OPERATOR(%, Int);
317 IMPLEMENT_BINARY_OPERATOR(%, ULong);
318 IMPLEMENT_BINARY_OPERATOR(%, Long);
319 case Type::FloatTyID:
320 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
322 case Type::DoubleTyID:
323 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
326 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
332 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
335 switch (Ty->getTypeID()) {
336 IMPLEMENT_BINARY_OPERATOR(&, Bool);
337 IMPLEMENT_BINARY_OPERATOR(&, UByte);
338 IMPLEMENT_BINARY_OPERATOR(&, SByte);
339 IMPLEMENT_BINARY_OPERATOR(&, UShort);
340 IMPLEMENT_BINARY_OPERATOR(&, Short);
341 IMPLEMENT_BINARY_OPERATOR(&, UInt);
342 IMPLEMENT_BINARY_OPERATOR(&, Int);
343 IMPLEMENT_BINARY_OPERATOR(&, ULong);
344 IMPLEMENT_BINARY_OPERATOR(&, Long);
346 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
352 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
355 switch (Ty->getTypeID()) {
356 IMPLEMENT_BINARY_OPERATOR(|, Bool);
357 IMPLEMENT_BINARY_OPERATOR(|, UByte);
358 IMPLEMENT_BINARY_OPERATOR(|, SByte);
359 IMPLEMENT_BINARY_OPERATOR(|, UShort);
360 IMPLEMENT_BINARY_OPERATOR(|, Short);
361 IMPLEMENT_BINARY_OPERATOR(|, UInt);
362 IMPLEMENT_BINARY_OPERATOR(|, Int);
363 IMPLEMENT_BINARY_OPERATOR(|, ULong);
364 IMPLEMENT_BINARY_OPERATOR(|, Long);
366 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
372 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
375 switch (Ty->getTypeID()) {
376 IMPLEMENT_BINARY_OPERATOR(^, Bool);
377 IMPLEMENT_BINARY_OPERATOR(^, UByte);
378 IMPLEMENT_BINARY_OPERATOR(^, SByte);
379 IMPLEMENT_BINARY_OPERATOR(^, UShort);
380 IMPLEMENT_BINARY_OPERATOR(^, Short);
381 IMPLEMENT_BINARY_OPERATOR(^, UInt);
382 IMPLEMENT_BINARY_OPERATOR(^, Int);
383 IMPLEMENT_BINARY_OPERATOR(^, ULong);
384 IMPLEMENT_BINARY_OPERATOR(^, Long);
386 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
392 #define IMPLEMENT_SETCC(OP, TY) \
393 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
395 // Handle pointers specially because they must be compared with only as much
396 // width as the host has. We _do not_ want to be comparing 64 bit values when
397 // running on a 32-bit target, otherwise the upper 32 bits might mess up
398 // comparisons if they contain garbage.
399 #define IMPLEMENT_POINTERSETCC(OP) \
400 case Type::PointerTyID: \
401 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
402 (void*)(intptr_t)Src2.PointerVal; break
404 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
407 switch (Ty->getTypeID()) {
408 IMPLEMENT_SETCC(==, UByte);
409 IMPLEMENT_SETCC(==, SByte);
410 IMPLEMENT_SETCC(==, UShort);
411 IMPLEMENT_SETCC(==, Short);
412 IMPLEMENT_SETCC(==, UInt);
413 IMPLEMENT_SETCC(==, Int);
414 IMPLEMENT_SETCC(==, ULong);
415 IMPLEMENT_SETCC(==, Long);
416 IMPLEMENT_SETCC(==, Float);
417 IMPLEMENT_SETCC(==, Double);
418 IMPLEMENT_POINTERSETCC(==);
420 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
426 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
429 switch (Ty->getTypeID()) {
430 IMPLEMENT_SETCC(!=, UByte);
431 IMPLEMENT_SETCC(!=, SByte);
432 IMPLEMENT_SETCC(!=, UShort);
433 IMPLEMENT_SETCC(!=, Short);
434 IMPLEMENT_SETCC(!=, UInt);
435 IMPLEMENT_SETCC(!=, Int);
436 IMPLEMENT_SETCC(!=, ULong);
437 IMPLEMENT_SETCC(!=, Long);
438 IMPLEMENT_SETCC(!=, Float);
439 IMPLEMENT_SETCC(!=, Double);
440 IMPLEMENT_POINTERSETCC(!=);
443 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
449 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
452 switch (Ty->getTypeID()) {
453 IMPLEMENT_SETCC(<=, UByte);
454 IMPLEMENT_SETCC(<=, SByte);
455 IMPLEMENT_SETCC(<=, UShort);
456 IMPLEMENT_SETCC(<=, Short);
457 IMPLEMENT_SETCC(<=, UInt);
458 IMPLEMENT_SETCC(<=, Int);
459 IMPLEMENT_SETCC(<=, ULong);
460 IMPLEMENT_SETCC(<=, Long);
461 IMPLEMENT_SETCC(<=, Float);
462 IMPLEMENT_SETCC(<=, Double);
463 IMPLEMENT_POINTERSETCC(<=);
465 std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
471 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
474 switch (Ty->getTypeID()) {
475 IMPLEMENT_SETCC(>=, UByte);
476 IMPLEMENT_SETCC(>=, SByte);
477 IMPLEMENT_SETCC(>=, UShort);
478 IMPLEMENT_SETCC(>=, Short);
479 IMPLEMENT_SETCC(>=, UInt);
480 IMPLEMENT_SETCC(>=, Int);
481 IMPLEMENT_SETCC(>=, ULong);
482 IMPLEMENT_SETCC(>=, Long);
483 IMPLEMENT_SETCC(>=, Float);
484 IMPLEMENT_SETCC(>=, Double);
485 IMPLEMENT_POINTERSETCC(>=);
487 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
493 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
496 switch (Ty->getTypeID()) {
497 IMPLEMENT_SETCC(<, UByte);
498 IMPLEMENT_SETCC(<, SByte);
499 IMPLEMENT_SETCC(<, UShort);
500 IMPLEMENT_SETCC(<, Short);
501 IMPLEMENT_SETCC(<, UInt);
502 IMPLEMENT_SETCC(<, Int);
503 IMPLEMENT_SETCC(<, ULong);
504 IMPLEMENT_SETCC(<, Long);
505 IMPLEMENT_SETCC(<, Float);
506 IMPLEMENT_SETCC(<, Double);
507 IMPLEMENT_POINTERSETCC(<);
509 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
515 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
518 switch (Ty->getTypeID()) {
519 IMPLEMENT_SETCC(>, UByte);
520 IMPLEMENT_SETCC(>, SByte);
521 IMPLEMENT_SETCC(>, UShort);
522 IMPLEMENT_SETCC(>, Short);
523 IMPLEMENT_SETCC(>, UInt);
524 IMPLEMENT_SETCC(>, Int);
525 IMPLEMENT_SETCC(>, ULong);
526 IMPLEMENT_SETCC(>, Long);
527 IMPLEMENT_SETCC(>, Float);
528 IMPLEMENT_SETCC(>, Double);
529 IMPLEMENT_POINTERSETCC(>);
531 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
537 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
538 ExecutionContext &SF = ECStack.back();
539 const Type *Ty = I.getOperand(0)->getType();
540 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
541 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
542 GenericValue R; // Result
544 switch (I.getOpcode()) {
545 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
546 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
547 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
548 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
549 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
550 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
551 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
552 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
553 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
554 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
555 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
556 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
557 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
558 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
559 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
560 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
562 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
569 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
571 return Src1.BoolVal ? Src2 : Src3;
574 void Interpreter::visitSelectInst(SelectInst &I) {
575 ExecutionContext &SF = ECStack.back();
576 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
577 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
578 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
579 GenericValue R = executeSelectInst(Src1, Src2, Src3);
584 //===----------------------------------------------------------------------===//
585 // Terminator Instruction Implementations
586 //===----------------------------------------------------------------------===//
588 void Interpreter::exitCalled(GenericValue GV) {
589 // runAtExitHandlers() assumes there are no stack frames, but
590 // if exit() was called, then it had a stack frame. Blow away
591 // the stack before interpreting atexit handlers.
593 runAtExitHandlers ();
597 /// Pop the last stack frame off of ECStack and then copy the result
598 /// back into the result variable if we are not returning void. The
599 /// result variable may be the ExitValue, or the Value of the calling
600 /// CallInst if there was a previous stack frame. This method may
601 /// invalidate any ECStack iterators you have. This method also takes
602 /// care of switching to the normal destination BB, if we are returning
605 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
606 GenericValue Result) {
607 // Pop the current stack frame.
610 if (ECStack.empty()) { // Finished main. Put result into exit code...
611 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
612 ExitValue = Result; // Capture the exit value of the program
614 memset(&ExitValue, 0, sizeof(ExitValue));
617 // If we have a previous stack frame, and we have a previous call,
618 // fill in the return value...
619 ExecutionContext &CallingSF = ECStack.back();
620 if (Instruction *I = CallingSF.Caller.getInstruction()) {
621 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
622 SetValue(I, Result, CallingSF);
623 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
624 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
625 CallingSF.Caller = CallSite(); // We returned from the call...
630 void Interpreter::visitReturnInst(ReturnInst &I) {
631 ExecutionContext &SF = ECStack.back();
632 const Type *RetTy = Type::VoidTy;
635 // Save away the return value... (if we are not 'ret void')
636 if (I.getNumOperands()) {
637 RetTy = I.getReturnValue()->getType();
638 Result = getOperandValue(I.getReturnValue(), SF);
641 popStackAndReturnValueToCaller(RetTy, Result);
644 void Interpreter::visitUnwindInst(UnwindInst &I) {
649 if (ECStack.empty ())
651 Inst = ECStack.back ().Caller.getInstruction ();
652 } while (!(Inst && isa<InvokeInst> (Inst)));
654 // Return from invoke
655 ExecutionContext &InvokingSF = ECStack.back ();
656 InvokingSF.Caller = CallSite ();
658 // Go to exceptional destination BB of invoke instruction
659 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
662 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
663 std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
667 void Interpreter::visitBranchInst(BranchInst &I) {
668 ExecutionContext &SF = ECStack.back();
671 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
672 if (!I.isUnconditional()) {
673 Value *Cond = I.getCondition();
674 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
675 Dest = I.getSuccessor(1);
677 SwitchToNewBasicBlock(Dest, SF);
680 void Interpreter::visitSwitchInst(SwitchInst &I) {
681 ExecutionContext &SF = ECStack.back();
682 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
683 const Type *ElTy = I.getOperand(0)->getType();
685 // Check to see if any of the cases match...
686 BasicBlock *Dest = 0;
687 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
688 if (executeSetEQInst(CondVal,
689 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
690 Dest = cast<BasicBlock>(I.getOperand(i+1));
694 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
695 SwitchToNewBasicBlock(Dest, SF);
698 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
699 // This function handles the actual updating of block and instruction iterators
700 // as well as execution of all of the PHI nodes in the destination block.
702 // This method does this because all of the PHI nodes must be executed
703 // atomically, reading their inputs before any of the results are updated. Not
704 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
705 // their inputs. If the input PHI node is updated before it is read, incorrect
706 // results can happen. Thus we use a two phase approach.
708 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
709 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
710 SF.CurBB = Dest; // Update CurBB to branch destination
711 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
713 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
715 // Loop over all of the PHI nodes in the current block, reading their inputs.
716 std::vector<GenericValue> ResultValues;
718 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
719 // Search for the value corresponding to this previous bb...
720 int i = PN->getBasicBlockIndex(PrevBB);
721 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
722 Value *IncomingValue = PN->getIncomingValue(i);
724 // Save the incoming value for this PHI node...
725 ResultValues.push_back(getOperandValue(IncomingValue, SF));
728 // Now loop over all of the PHI nodes setting their values...
729 SF.CurInst = SF.CurBB->begin();
730 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
731 PHINode *PN = cast<PHINode>(SF.CurInst);
732 SetValue(PN, ResultValues[i], SF);
736 //===----------------------------------------------------------------------===//
737 // Memory Instruction Implementations
738 //===----------------------------------------------------------------------===//
740 void Interpreter::visitAllocationInst(AllocationInst &I) {
741 ExecutionContext &SF = ECStack.back();
743 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
745 // Get the number of elements being allocated by the array...
746 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
748 // Allocate enough memory to hold the type...
749 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
751 GenericValue Result = PTOGV(Memory);
752 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
753 SetValue(&I, Result, SF);
755 if (I.getOpcode() == Instruction::Alloca)
756 ECStack.back().Allocas.add(Memory);
759 void Interpreter::visitFreeInst(FreeInst &I) {
760 ExecutionContext &SF = ECStack.back();
761 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
762 GenericValue Value = getOperandValue(I.getOperand(0), SF);
763 // TODO: Check to make sure memory is allocated
764 free(GVTOP(Value)); // Free memory
767 // getElementOffset - The workhorse for getelementptr.
769 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
771 ExecutionContext &SF) {
772 assert(isa<PointerType>(Ptr->getType()) &&
773 "Cannot getElementOffset of a nonpointer type!");
777 for (; I != E; ++I) {
778 if (const StructType *STy = dyn_cast<StructType>(*I)) {
779 const StructLayout *SLO = TD.getStructLayout(STy);
781 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
782 unsigned Index = unsigned(CPU->getZExtValue());
784 Total += (PointerTy)SLO->MemberOffsets[Index];
786 const SequentialType *ST = cast<SequentialType>(*I);
787 // Get the index number for the array... which must be long type...
788 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
791 switch (I.getOperand()->getType()->getTypeID()) {
792 default: assert(0 && "Illegal getelementptr index for sequential type!");
793 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
794 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
795 case Type::IntTyID: Idx = IdxGV.IntVal; break;
796 case Type::LongTyID: Idx = IdxGV.LongVal; break;
797 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
798 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
799 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
800 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
802 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
807 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
811 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
812 ExecutionContext &SF = ECStack.back();
813 SetValue(&I, TheEE->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);
821 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
822 SetValue(&I, Result, SF);
825 void Interpreter::visitStoreInst(StoreInst &I) {
826 ExecutionContext &SF = ECStack.back();
827 GenericValue Val = getOperandValue(I.getOperand(0), SF);
828 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
829 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
830 I.getOperand(0)->getType());
833 //===----------------------------------------------------------------------===//
834 // Miscellaneous Instruction Implementations
835 //===----------------------------------------------------------------------===//
837 void Interpreter::visitCallSite(CallSite CS) {
838 ExecutionContext &SF = ECStack.back();
840 // Check to see if this is an intrinsic function call...
841 if (Function *F = CS.getCalledFunction())
842 if (F->isExternal ())
843 switch (F->getIntrinsicID()) {
844 case Intrinsic::not_intrinsic:
846 case Intrinsic::vastart: { // va_start
847 GenericValue ArgIndex;
848 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
849 ArgIndex.UIntPairVal.second = 0;
850 SetValue(CS.getInstruction(), ArgIndex, SF);
853 case Intrinsic::vaend: // va_end is a noop for the interpreter
855 case Intrinsic::vacopy: // va_copy: dest = src
856 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
859 // If it is an unknown intrinsic function, use the intrinsic lowering
860 // class to transform it into hopefully tasty LLVM code.
862 Instruction *Prev = CS.getInstruction()->getPrev();
863 BasicBlock *Parent = CS.getInstruction()->getParent();
864 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
866 // Restore the CurInst pointer to the first instruction newly inserted, if
869 SF.CurInst = Parent->begin();
878 std::vector<GenericValue> ArgVals;
879 const unsigned NumArgs = SF.Caller.arg_size();
880 ArgVals.reserve(NumArgs);
881 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
882 e = SF.Caller.arg_end(); i != e; ++i) {
884 ArgVals.push_back(getOperandValue(V, SF));
885 // Promote all integral types whose size is < sizeof(int) into ints. We do
886 // this by zero or sign extending the value as appropriate according to the
888 const Type *Ty = V->getType();
889 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
890 if (Ty == Type::ShortTy)
891 ArgVals.back().IntVal = ArgVals.back().ShortVal;
892 else if (Ty == Type::UShortTy)
893 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
894 else if (Ty == Type::SByteTy)
895 ArgVals.back().IntVal = ArgVals.back().SByteVal;
896 else if (Ty == Type::UByteTy)
897 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
898 else if (Ty == Type::BoolTy)
899 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
901 assert(0 && "Unknown type!");
905 // To handle indirect calls, we must get the pointer value from the argument
906 // and treat it as a function pointer.
907 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
908 callFunction((Function*)GVTOP(SRC), ArgVals);
911 #define IMPLEMENT_SHIFT(OP, TY) \
912 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
914 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
917 switch (Ty->getTypeID()) {
918 IMPLEMENT_SHIFT(<<, UByte);
919 IMPLEMENT_SHIFT(<<, SByte);
920 IMPLEMENT_SHIFT(<<, UShort);
921 IMPLEMENT_SHIFT(<<, Short);
922 IMPLEMENT_SHIFT(<<, UInt);
923 IMPLEMENT_SHIFT(<<, Int);
924 IMPLEMENT_SHIFT(<<, ULong);
925 IMPLEMENT_SHIFT(<<, Long);
927 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
932 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
935 switch (Ty->getTypeID()) {
936 IMPLEMENT_SHIFT(>>, UByte);
937 IMPLEMENT_SHIFT(>>, SByte);
938 IMPLEMENT_SHIFT(>>, UShort);
939 IMPLEMENT_SHIFT(>>, Short);
940 IMPLEMENT_SHIFT(>>, UInt);
941 IMPLEMENT_SHIFT(>>, Int);
942 IMPLEMENT_SHIFT(>>, ULong);
943 IMPLEMENT_SHIFT(>>, Long);
945 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
951 void Interpreter::visitShl(ShiftInst &I) {
952 ExecutionContext &SF = ECStack.back();
953 const Type *Ty = I.getOperand(0)->getType();
954 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
955 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
957 Dest = executeShlInst (Src1, Src2, Ty);
958 SetValue(&I, Dest, SF);
961 void Interpreter::visitShr(ShiftInst &I) {
962 ExecutionContext &SF = ECStack.back();
963 const Type *Ty = I.getOperand(0)->getType();
964 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
965 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
967 Dest = executeShrInst (Src1, Src2, Ty);
968 SetValue(&I, Dest, SF);
971 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
972 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
974 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
975 case Type::DESTTY##TyID: \
976 switch (SrcTy->getTypeID()) { \
977 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
978 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
979 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
980 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
981 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
982 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
983 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
984 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
985 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
986 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
988 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
989 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
990 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
992 #define IMPLEMENT_CAST_CASE_END() \
993 default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
998 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
999 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1000 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
1001 IMPLEMENT_CAST_CASE_END()
1003 GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
1004 ExecutionContext &SF) {
1005 const Type *SrcTy = SrcVal->getType();
1006 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1008 switch (Ty->getTypeID()) {
1009 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1010 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1011 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1012 IMPLEMENT_CAST_CASE(Short , ( signed short));
1013 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1014 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1015 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1016 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1017 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1018 IMPLEMENT_CAST_CASE(Float , (float));
1019 IMPLEMENT_CAST_CASE(Double , (double));
1020 IMPLEMENT_CAST_CASE(Bool , (bool));
1022 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
1029 void Interpreter::visitCastInst(CastInst &I) {
1030 ExecutionContext &SF = ECStack.back();
1031 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1034 #define IMPLEMENT_VAARG(TY) \
1035 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1037 void Interpreter::visitVAArgInst(VAArgInst &I) {
1038 ExecutionContext &SF = ECStack.back();
1040 // Get the incoming valist parameter. LLI treats the valist as a
1041 // (ec-stack-depth var-arg-index) pair.
1042 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1044 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1045 .VarArgs[VAList.UIntPairVal.second];
1046 const Type *Ty = I.getType();
1047 switch (Ty->getTypeID()) {
1048 IMPLEMENT_VAARG(UByte);
1049 IMPLEMENT_VAARG(SByte);
1050 IMPLEMENT_VAARG(UShort);
1051 IMPLEMENT_VAARG(Short);
1052 IMPLEMENT_VAARG(UInt);
1053 IMPLEMENT_VAARG(Int);
1054 IMPLEMENT_VAARG(ULong);
1055 IMPLEMENT_VAARG(Long);
1056 IMPLEMENT_VAARG(Pointer);
1057 IMPLEMENT_VAARG(Float);
1058 IMPLEMENT_VAARG(Double);
1059 IMPLEMENT_VAARG(Bool);
1061 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1065 // Set the Value of this Instruction.
1066 SetValue(&I, Dest, SF);
1068 // Move the pointer to the next vararg.
1069 ++VAList.UIntPairVal.second;
1072 //===----------------------------------------------------------------------===//
1073 // Dispatch and Execution Code
1074 //===----------------------------------------------------------------------===//
1076 //===----------------------------------------------------------------------===//
1077 // callFunction - Execute the specified function...
1079 void Interpreter::callFunction(Function *F,
1080 const std::vector<GenericValue> &ArgVals) {
1081 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1082 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1083 "Incorrect number of arguments passed into function call!");
1084 // Make a new stack frame... and fill it in.
1085 ECStack.push_back(ExecutionContext());
1086 ExecutionContext &StackFrame = ECStack.back();
1087 StackFrame.CurFunction = F;
1089 // Special handling for external functions.
1090 if (F->isExternal()) {
1091 GenericValue Result = callExternalFunction (F, ArgVals);
1092 // Simulate a 'ret' instruction of the appropriate type.
1093 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1097 // Get pointers to first LLVM BB & Instruction in function.
1098 StackFrame.CurBB = F->begin();
1099 StackFrame.CurInst = StackFrame.CurBB->begin();
1101 // Run through the function arguments and initialize their values...
1102 assert((ArgVals.size() == F->arg_size() ||
1103 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1104 "Invalid number of values passed to function invocation!");
1106 // Handle non-varargs arguments...
1108 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
1109 SetValue(AI, ArgVals[i], StackFrame);
1111 // Handle varargs arguments...
1112 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1115 void Interpreter::run() {
1116 while (!ECStack.empty()) {
1117 // Interpret a single instruction & increment the "PC".
1118 ExecutionContext &SF = ECStack.back(); // Current stack frame
1119 Instruction &I = *SF.CurInst++; // Increment before execute
1121 // Track the number of dynamic instructions executed.
1124 DEBUG(std::cerr << "About to interpret: " << I);
1125 visit(I); // Dispatch to one of the visit* methods...