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 executeDivInst(GenericValue Src1, GenericValue Src2,
47 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
49 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
51 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
53 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
55 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
57 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
59 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
61 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
63 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
65 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
67 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
69 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
72 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
73 ExecutionContext &SF) {
74 switch (CE->getOpcode()) {
75 case Instruction::Cast:
76 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
77 case Instruction::GetElementPtr:
78 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
79 gep_type_end(CE), SF);
80 case Instruction::Add:
81 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
82 getOperandValue(CE->getOperand(1), SF),
83 CE->getOperand(0)->getType());
84 case Instruction::Sub:
85 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
86 getOperandValue(CE->getOperand(1), SF),
87 CE->getOperand(0)->getType());
88 case Instruction::Mul:
89 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
90 getOperandValue(CE->getOperand(1), SF),
91 CE->getOperand(0)->getType());
92 case Instruction::Div:
93 return executeDivInst(getOperandValue(CE->getOperand(0), SF),
94 getOperandValue(CE->getOperand(1), SF),
95 CE->getOperand(0)->getType());
96 case Instruction::Rem:
97 return executeRemInst(getOperandValue(CE->getOperand(0), SF),
98 getOperandValue(CE->getOperand(1), SF),
99 CE->getOperand(0)->getType());
100 case Instruction::And:
101 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
102 getOperandValue(CE->getOperand(1), SF),
103 CE->getOperand(0)->getType());
104 case Instruction::Or:
105 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
106 getOperandValue(CE->getOperand(1), SF),
107 CE->getOperand(0)->getType());
108 case Instruction::Xor:
109 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
110 getOperandValue(CE->getOperand(1), SF),
111 CE->getOperand(0)->getType());
112 case Instruction::SetEQ:
113 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
114 getOperandValue(CE->getOperand(1), SF),
115 CE->getOperand(0)->getType());
116 case Instruction::SetNE:
117 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
118 getOperandValue(CE->getOperand(1), SF),
119 CE->getOperand(0)->getType());
120 case Instruction::SetLE:
121 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
122 getOperandValue(CE->getOperand(1), SF),
123 CE->getOperand(0)->getType());
124 case Instruction::SetGE:
125 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
126 getOperandValue(CE->getOperand(1), SF),
127 CE->getOperand(0)->getType());
128 case Instruction::SetLT:
129 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
130 getOperandValue(CE->getOperand(1), SF),
131 CE->getOperand(0)->getType());
132 case Instruction::SetGT:
133 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
134 getOperandValue(CE->getOperand(1), SF),
135 CE->getOperand(0)->getType());
136 case Instruction::Shl:
137 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
138 getOperandValue(CE->getOperand(1), SF),
139 CE->getOperand(0)->getType());
140 case Instruction::Shr:
141 return executeShrInst(getOperandValue(CE->getOperand(0), SF),
142 getOperandValue(CE->getOperand(1), SF),
143 CE->getOperand(0)->getType());
144 case Instruction::Select:
145 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
146 getOperandValue(CE->getOperand(1), SF),
147 getOperandValue(CE->getOperand(2), SF));
149 std::cerr << "Unhandled ConstantExpr: " << *CE << "\n";
151 return GenericValue();
155 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
156 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
157 return getConstantExprValue(CE, SF);
158 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
159 return getConstantValue(CPV);
160 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
161 return PTOGV(getPointerToGlobal(GV));
167 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
171 void Interpreter::initializeExecutionEngine() {
175 //===----------------------------------------------------------------------===//
176 // Binary Instruction Implementations
177 //===----------------------------------------------------------------------===//
179 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
180 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
182 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
185 switch (Ty->getTypeID()) {
186 IMPLEMENT_BINARY_OPERATOR(+, UByte);
187 IMPLEMENT_BINARY_OPERATOR(+, SByte);
188 IMPLEMENT_BINARY_OPERATOR(+, UShort);
189 IMPLEMENT_BINARY_OPERATOR(+, Short);
190 IMPLEMENT_BINARY_OPERATOR(+, UInt);
191 IMPLEMENT_BINARY_OPERATOR(+, Int);
192 IMPLEMENT_BINARY_OPERATOR(+, ULong);
193 IMPLEMENT_BINARY_OPERATOR(+, Long);
194 IMPLEMENT_BINARY_OPERATOR(+, Float);
195 IMPLEMENT_BINARY_OPERATOR(+, Double);
197 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
203 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
206 switch (Ty->getTypeID()) {
207 IMPLEMENT_BINARY_OPERATOR(-, UByte);
208 IMPLEMENT_BINARY_OPERATOR(-, SByte);
209 IMPLEMENT_BINARY_OPERATOR(-, UShort);
210 IMPLEMENT_BINARY_OPERATOR(-, Short);
211 IMPLEMENT_BINARY_OPERATOR(-, UInt);
212 IMPLEMENT_BINARY_OPERATOR(-, Int);
213 IMPLEMENT_BINARY_OPERATOR(-, ULong);
214 IMPLEMENT_BINARY_OPERATOR(-, Long);
215 IMPLEMENT_BINARY_OPERATOR(-, Float);
216 IMPLEMENT_BINARY_OPERATOR(-, Double);
218 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
224 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
227 switch (Ty->getTypeID()) {
228 IMPLEMENT_BINARY_OPERATOR(*, UByte);
229 IMPLEMENT_BINARY_OPERATOR(*, SByte);
230 IMPLEMENT_BINARY_OPERATOR(*, UShort);
231 IMPLEMENT_BINARY_OPERATOR(*, Short);
232 IMPLEMENT_BINARY_OPERATOR(*, UInt);
233 IMPLEMENT_BINARY_OPERATOR(*, Int);
234 IMPLEMENT_BINARY_OPERATOR(*, ULong);
235 IMPLEMENT_BINARY_OPERATOR(*, Long);
236 IMPLEMENT_BINARY_OPERATOR(*, Float);
237 IMPLEMENT_BINARY_OPERATOR(*, Double);
239 std::cout << "Unhandled type for Mul instruction: " << *Ty << "\n";
245 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
248 switch (Ty->getTypeID()) {
249 IMPLEMENT_BINARY_OPERATOR(/, UByte);
250 IMPLEMENT_BINARY_OPERATOR(/, SByte);
251 IMPLEMENT_BINARY_OPERATOR(/, UShort);
252 IMPLEMENT_BINARY_OPERATOR(/, Short);
253 IMPLEMENT_BINARY_OPERATOR(/, UInt);
254 IMPLEMENT_BINARY_OPERATOR(/, Int);
255 IMPLEMENT_BINARY_OPERATOR(/, ULong);
256 IMPLEMENT_BINARY_OPERATOR(/, Long);
257 IMPLEMENT_BINARY_OPERATOR(/, Float);
258 IMPLEMENT_BINARY_OPERATOR(/, Double);
260 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
266 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
269 switch (Ty->getTypeID()) {
270 IMPLEMENT_BINARY_OPERATOR(%, UByte);
271 IMPLEMENT_BINARY_OPERATOR(%, SByte);
272 IMPLEMENT_BINARY_OPERATOR(%, UShort);
273 IMPLEMENT_BINARY_OPERATOR(%, Short);
274 IMPLEMENT_BINARY_OPERATOR(%, UInt);
275 IMPLEMENT_BINARY_OPERATOR(%, Int);
276 IMPLEMENT_BINARY_OPERATOR(%, ULong);
277 IMPLEMENT_BINARY_OPERATOR(%, Long);
278 case Type::FloatTyID:
279 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
281 case Type::DoubleTyID:
282 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
285 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
291 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
294 switch (Ty->getTypeID()) {
295 IMPLEMENT_BINARY_OPERATOR(&, Bool);
296 IMPLEMENT_BINARY_OPERATOR(&, UByte);
297 IMPLEMENT_BINARY_OPERATOR(&, SByte);
298 IMPLEMENT_BINARY_OPERATOR(&, UShort);
299 IMPLEMENT_BINARY_OPERATOR(&, Short);
300 IMPLEMENT_BINARY_OPERATOR(&, UInt);
301 IMPLEMENT_BINARY_OPERATOR(&, Int);
302 IMPLEMENT_BINARY_OPERATOR(&, ULong);
303 IMPLEMENT_BINARY_OPERATOR(&, Long);
305 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
311 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
314 switch (Ty->getTypeID()) {
315 IMPLEMENT_BINARY_OPERATOR(|, Bool);
316 IMPLEMENT_BINARY_OPERATOR(|, UByte);
317 IMPLEMENT_BINARY_OPERATOR(|, SByte);
318 IMPLEMENT_BINARY_OPERATOR(|, UShort);
319 IMPLEMENT_BINARY_OPERATOR(|, Short);
320 IMPLEMENT_BINARY_OPERATOR(|, UInt);
321 IMPLEMENT_BINARY_OPERATOR(|, Int);
322 IMPLEMENT_BINARY_OPERATOR(|, ULong);
323 IMPLEMENT_BINARY_OPERATOR(|, Long);
325 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
331 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
334 switch (Ty->getTypeID()) {
335 IMPLEMENT_BINARY_OPERATOR(^, Bool);
336 IMPLEMENT_BINARY_OPERATOR(^, UByte);
337 IMPLEMENT_BINARY_OPERATOR(^, SByte);
338 IMPLEMENT_BINARY_OPERATOR(^, UShort);
339 IMPLEMENT_BINARY_OPERATOR(^, Short);
340 IMPLEMENT_BINARY_OPERATOR(^, UInt);
341 IMPLEMENT_BINARY_OPERATOR(^, Int);
342 IMPLEMENT_BINARY_OPERATOR(^, ULong);
343 IMPLEMENT_BINARY_OPERATOR(^, Long);
345 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
351 #define IMPLEMENT_SETCC(OP, TY) \
352 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
354 // Handle pointers specially because they must be compared with only as much
355 // width as the host has. We _do not_ want to be comparing 64 bit values when
356 // running on a 32-bit target, otherwise the upper 32 bits might mess up
357 // comparisons if they contain garbage.
358 #define IMPLEMENT_POINTERSETCC(OP) \
359 case Type::PointerTyID: \
360 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
361 (void*)(intptr_t)Src2.PointerVal; break
363 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
366 switch (Ty->getTypeID()) {
367 IMPLEMENT_SETCC(==, UByte);
368 IMPLEMENT_SETCC(==, SByte);
369 IMPLEMENT_SETCC(==, UShort);
370 IMPLEMENT_SETCC(==, Short);
371 IMPLEMENT_SETCC(==, UInt);
372 IMPLEMENT_SETCC(==, Int);
373 IMPLEMENT_SETCC(==, ULong);
374 IMPLEMENT_SETCC(==, Long);
375 IMPLEMENT_SETCC(==, Float);
376 IMPLEMENT_SETCC(==, Double);
377 IMPLEMENT_POINTERSETCC(==);
379 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
385 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
388 switch (Ty->getTypeID()) {
389 IMPLEMENT_SETCC(!=, UByte);
390 IMPLEMENT_SETCC(!=, SByte);
391 IMPLEMENT_SETCC(!=, UShort);
392 IMPLEMENT_SETCC(!=, Short);
393 IMPLEMENT_SETCC(!=, UInt);
394 IMPLEMENT_SETCC(!=, Int);
395 IMPLEMENT_SETCC(!=, ULong);
396 IMPLEMENT_SETCC(!=, Long);
397 IMPLEMENT_SETCC(!=, Float);
398 IMPLEMENT_SETCC(!=, Double);
399 IMPLEMENT_POINTERSETCC(!=);
402 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
408 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
411 switch (Ty->getTypeID()) {
412 IMPLEMENT_SETCC(<=, UByte);
413 IMPLEMENT_SETCC(<=, SByte);
414 IMPLEMENT_SETCC(<=, UShort);
415 IMPLEMENT_SETCC(<=, Short);
416 IMPLEMENT_SETCC(<=, UInt);
417 IMPLEMENT_SETCC(<=, Int);
418 IMPLEMENT_SETCC(<=, ULong);
419 IMPLEMENT_SETCC(<=, Long);
420 IMPLEMENT_SETCC(<=, Float);
421 IMPLEMENT_SETCC(<=, Double);
422 IMPLEMENT_POINTERSETCC(<=);
424 std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
430 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
433 switch (Ty->getTypeID()) {
434 IMPLEMENT_SETCC(>=, UByte);
435 IMPLEMENT_SETCC(>=, SByte);
436 IMPLEMENT_SETCC(>=, UShort);
437 IMPLEMENT_SETCC(>=, Short);
438 IMPLEMENT_SETCC(>=, UInt);
439 IMPLEMENT_SETCC(>=, Int);
440 IMPLEMENT_SETCC(>=, ULong);
441 IMPLEMENT_SETCC(>=, Long);
442 IMPLEMENT_SETCC(>=, Float);
443 IMPLEMENT_SETCC(>=, Double);
444 IMPLEMENT_POINTERSETCC(>=);
446 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
452 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
455 switch (Ty->getTypeID()) {
456 IMPLEMENT_SETCC(<, UByte);
457 IMPLEMENT_SETCC(<, SByte);
458 IMPLEMENT_SETCC(<, UShort);
459 IMPLEMENT_SETCC(<, Short);
460 IMPLEMENT_SETCC(<, UInt);
461 IMPLEMENT_SETCC(<, Int);
462 IMPLEMENT_SETCC(<, ULong);
463 IMPLEMENT_SETCC(<, Long);
464 IMPLEMENT_SETCC(<, Float);
465 IMPLEMENT_SETCC(<, Double);
466 IMPLEMENT_POINTERSETCC(<);
468 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
474 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
477 switch (Ty->getTypeID()) {
478 IMPLEMENT_SETCC(>, UByte);
479 IMPLEMENT_SETCC(>, SByte);
480 IMPLEMENT_SETCC(>, UShort);
481 IMPLEMENT_SETCC(>, Short);
482 IMPLEMENT_SETCC(>, UInt);
483 IMPLEMENT_SETCC(>, Int);
484 IMPLEMENT_SETCC(>, ULong);
485 IMPLEMENT_SETCC(>, Long);
486 IMPLEMENT_SETCC(>, Float);
487 IMPLEMENT_SETCC(>, Double);
488 IMPLEMENT_POINTERSETCC(>);
490 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
496 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
497 ExecutionContext &SF = ECStack.back();
498 const Type *Ty = I.getOperand(0)->getType();
499 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
500 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
501 GenericValue R; // Result
503 switch (I.getOpcode()) {
504 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
505 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
506 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
507 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
508 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
509 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
510 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
511 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
512 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
513 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
514 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
515 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
516 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
517 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
519 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
526 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
528 return Src1.BoolVal ? Src2 : Src3;
531 void Interpreter::visitSelectInst(SelectInst &I) {
532 ExecutionContext &SF = ECStack.back();
533 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
534 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
535 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
536 GenericValue R = executeSelectInst(Src1, Src2, Src3);
541 //===----------------------------------------------------------------------===//
542 // Terminator Instruction Implementations
543 //===----------------------------------------------------------------------===//
545 void Interpreter::exitCalled(GenericValue GV) {
546 // runAtExitHandlers() assumes there are no stack frames, but
547 // if exit() was called, then it had a stack frame. Blow away
548 // the stack before interpreting atexit handlers.
550 runAtExitHandlers ();
554 /// Pop the last stack frame off of ECStack and then copy the result
555 /// back into the result variable if we are not returning void. The
556 /// result variable may be the ExitCode, or the Value of the calling
557 /// CallInst if there was a previous stack frame. This method may
558 /// invalidate any ECStack iterators you have. This method also takes
559 /// care of switching to the normal destination BB, if we are returning
562 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
563 GenericValue Result) {
564 // Pop the current stack frame.
567 if (ECStack.empty()) { // Finished main. Put result into exit code...
568 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
569 ExitCode = Result.IntVal; // Capture the exit code of the program
574 // If we have a previous stack frame, and we have a previous call,
575 // fill in the return value...
576 ExecutionContext &CallingSF = ECStack.back();
577 if (Instruction *I = CallingSF.Caller.getInstruction()) {
578 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
579 SetValue(I, Result, CallingSF);
580 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
581 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
582 CallingSF.Caller = CallSite(); // We returned from the call...
587 void Interpreter::visitReturnInst(ReturnInst &I) {
588 ExecutionContext &SF = ECStack.back();
589 const Type *RetTy = Type::VoidTy;
592 // Save away the return value... (if we are not 'ret void')
593 if (I.getNumOperands()) {
594 RetTy = I.getReturnValue()->getType();
595 Result = getOperandValue(I.getReturnValue(), SF);
598 popStackAndReturnValueToCaller(RetTy, Result);
601 void Interpreter::visitUnwindInst(UnwindInst &I) {
606 if (ECStack.empty ())
608 Inst = ECStack.back ().Caller.getInstruction ();
609 } while (!(Inst && isa<InvokeInst> (Inst)));
611 // Return from invoke
612 ExecutionContext &InvokingSF = ECStack.back ();
613 InvokingSF.Caller = CallSite ();
615 // Go to exceptional destination BB of invoke instruction
616 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
619 void Interpreter::visitBranchInst(BranchInst &I) {
620 ExecutionContext &SF = ECStack.back();
623 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
624 if (!I.isUnconditional()) {
625 Value *Cond = I.getCondition();
626 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
627 Dest = I.getSuccessor(1);
629 SwitchToNewBasicBlock(Dest, SF);
632 void Interpreter::visitSwitchInst(SwitchInst &I) {
633 ExecutionContext &SF = ECStack.back();
634 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
635 const Type *ElTy = I.getOperand(0)->getType();
637 // Check to see if any of the cases match...
638 BasicBlock *Dest = 0;
639 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
640 if (executeSetEQInst(CondVal,
641 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
642 Dest = cast<BasicBlock>(I.getOperand(i+1));
646 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
647 SwitchToNewBasicBlock(Dest, SF);
650 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
651 // This function handles the actual updating of block and instruction iterators
652 // as well as execution of all of the PHI nodes in the destination block.
654 // This method does this because all of the PHI nodes must be executed
655 // atomically, reading their inputs before any of the results are updated. Not
656 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
657 // their inputs. If the input PHI node is updated before it is read, incorrect
658 // results can happen. Thus we use a two phase approach.
660 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
661 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
662 SF.CurBB = Dest; // Update CurBB to branch destination
663 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
665 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
667 // Loop over all of the PHI nodes in the current block, reading their inputs.
668 std::vector<GenericValue> ResultValues;
670 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
671 // Search for the value corresponding to this previous bb...
672 int i = PN->getBasicBlockIndex(PrevBB);
673 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
674 Value *IncomingValue = PN->getIncomingValue(i);
676 // Save the incoming value for this PHI node...
677 ResultValues.push_back(getOperandValue(IncomingValue, SF));
680 // Now loop over all of the PHI nodes setting their values...
681 SF.CurInst = SF.CurBB->begin();
682 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
683 PHINode *PN = cast<PHINode>(SF.CurInst);
684 SetValue(PN, ResultValues[i], SF);
688 //===----------------------------------------------------------------------===//
689 // Memory Instruction Implementations
690 //===----------------------------------------------------------------------===//
692 void Interpreter::visitAllocationInst(AllocationInst &I) {
693 ExecutionContext &SF = ECStack.back();
695 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
697 // Get the number of elements being allocated by the array...
698 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
700 // Allocate enough memory to hold the type...
701 void *Memory = malloc(NumElements * TD.getTypeSize(Ty));
703 GenericValue Result = PTOGV(Memory);
704 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
705 SetValue(&I, Result, SF);
707 if (I.getOpcode() == Instruction::Alloca)
708 ECStack.back().Allocas.add(Memory);
711 void Interpreter::visitFreeInst(FreeInst &I) {
712 ExecutionContext &SF = ECStack.back();
713 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
714 GenericValue Value = getOperandValue(I.getOperand(0), SF);
715 // TODO: Check to make sure memory is allocated
716 free(GVTOP(Value)); // Free memory
719 // getElementOffset - The workhorse for getelementptr.
721 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
723 ExecutionContext &SF) {
724 assert(isa<PointerType>(Ptr->getType()) &&
725 "Cannot getElementOffset of a nonpointer type!");
729 for (; I != E; ++I) {
730 if (const StructType *STy = dyn_cast<StructType>(*I)) {
731 const StructLayout *SLO = TD.getStructLayout(STy);
733 const ConstantUInt *CPU = cast<ConstantUInt>(I.getOperand());
734 unsigned Index = CPU->getValue();
736 Total += SLO->MemberOffsets[Index];
738 const SequentialType *ST = cast<SequentialType>(*I);
739 // Get the index number for the array... which must be long type...
740 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
743 switch (I.getOperand()->getType()->getTypeID()) {
744 default: assert(0 && "Illegal getelementptr index for sequential type!");
745 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
746 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
747 case Type::IntTyID: Idx = IdxGV.IntVal; break;
748 case Type::LongTyID: Idx = IdxGV.LongVal; break;
749 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
750 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
751 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
752 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
754 Total += TD.getTypeSize(ST->getElementType())*Idx;
759 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
763 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
764 ExecutionContext &SF = ECStack.back();
765 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
766 gep_type_begin(I), gep_type_end(I), SF), SF);
769 void Interpreter::visitLoadInst(LoadInst &I) {
770 ExecutionContext &SF = ECStack.back();
771 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
772 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
773 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
774 SetValue(&I, Result, SF);
777 void Interpreter::visitStoreInst(StoreInst &I) {
778 ExecutionContext &SF = ECStack.back();
779 GenericValue Val = getOperandValue(I.getOperand(0), SF);
780 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
781 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
782 I.getOperand(0)->getType());
785 //===----------------------------------------------------------------------===//
786 // Miscellaneous Instruction Implementations
787 //===----------------------------------------------------------------------===//
789 void Interpreter::visitCallSite(CallSite CS) {
790 ExecutionContext &SF = ECStack.back();
792 // Check to see if this is an intrinsic function call...
793 if (Function *F = CS.getCalledFunction())
794 if (F->isExternal ())
795 switch (F->getIntrinsicID()) {
796 case Intrinsic::not_intrinsic:
798 case Intrinsic::vastart: { // va_start
799 GenericValue ArgIndex;
800 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
801 ArgIndex.UIntPairVal.second = 0;
802 SetValue(CS.getInstruction(), ArgIndex, SF);
805 case Intrinsic::vaend: // va_end is a noop for the interpreter
807 case Intrinsic::vacopy: // va_copy: dest = src
808 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
811 // If it is an unknown intrinsic function, use the intrinsic lowering
812 // class to transform it into hopefully tasty LLVM code.
814 Instruction *Prev = CS.getInstruction()->getPrev();
815 BasicBlock *Parent = CS.getInstruction()->getParent();
816 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
818 // Restore the CurInst pointer to the first instruction newly inserted, if
821 SF.CurInst = Parent->begin();
830 std::vector<GenericValue> ArgVals;
831 const unsigned NumArgs = SF.Caller.arg_size();
832 ArgVals.reserve(NumArgs);
833 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
834 e = SF.Caller.arg_end(); i != e; ++i) {
836 ArgVals.push_back(getOperandValue(V, SF));
837 // Promote all integral types whose size is < sizeof(int) into ints. We do
838 // this by zero or sign extending the value as appropriate according to the
840 const Type *Ty = V->getType();
841 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
842 if (Ty == Type::ShortTy)
843 ArgVals.back().IntVal = ArgVals.back().ShortVal;
844 else if (Ty == Type::UShortTy)
845 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
846 else if (Ty == Type::SByteTy)
847 ArgVals.back().IntVal = ArgVals.back().SByteVal;
848 else if (Ty == Type::UByteTy)
849 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
850 else if (Ty == Type::BoolTy)
851 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
853 assert(0 && "Unknown type!");
857 // To handle indirect calls, we must get the pointer value from the argument
858 // and treat it as a function pointer.
859 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
860 callFunction((Function*)GVTOP(SRC), ArgVals);
863 #define IMPLEMENT_SHIFT(OP, TY) \
864 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
866 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
869 switch (Ty->getTypeID()) {
870 IMPLEMENT_SHIFT(<<, UByte);
871 IMPLEMENT_SHIFT(<<, SByte);
872 IMPLEMENT_SHIFT(<<, UShort);
873 IMPLEMENT_SHIFT(<<, Short);
874 IMPLEMENT_SHIFT(<<, UInt);
875 IMPLEMENT_SHIFT(<<, Int);
876 IMPLEMENT_SHIFT(<<, ULong);
877 IMPLEMENT_SHIFT(<<, Long);
879 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
884 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
887 switch (Ty->getTypeID()) {
888 IMPLEMENT_SHIFT(>>, UByte);
889 IMPLEMENT_SHIFT(>>, SByte);
890 IMPLEMENT_SHIFT(>>, UShort);
891 IMPLEMENT_SHIFT(>>, Short);
892 IMPLEMENT_SHIFT(>>, UInt);
893 IMPLEMENT_SHIFT(>>, Int);
894 IMPLEMENT_SHIFT(>>, ULong);
895 IMPLEMENT_SHIFT(>>, Long);
897 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
903 void Interpreter::visitShl(ShiftInst &I) {
904 ExecutionContext &SF = ECStack.back();
905 const Type *Ty = I.getOperand(0)->getType();
906 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
907 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
909 Dest = executeShlInst (Src1, Src2, Ty);
910 SetValue(&I, Dest, SF);
913 void Interpreter::visitShr(ShiftInst &I) {
914 ExecutionContext &SF = ECStack.back();
915 const Type *Ty = I.getOperand(0)->getType();
916 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
917 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
919 Dest = executeShrInst (Src1, Src2, Ty);
920 SetValue(&I, Dest, SF);
923 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
924 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
926 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
927 case Type::DESTTY##TyID: \
928 switch (SrcTy->getTypeID()) { \
929 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
930 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
931 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
932 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
933 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
934 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
935 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
936 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
937 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
938 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
940 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
941 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
942 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
944 #define IMPLEMENT_CAST_CASE_END() \
945 default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
950 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
951 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
952 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
953 IMPLEMENT_CAST_CASE_END()
955 GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
956 ExecutionContext &SF) {
957 const Type *SrcTy = SrcVal->getType();
958 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
960 switch (Ty->getTypeID()) {
961 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
962 IMPLEMENT_CAST_CASE(SByte , ( signed char));
963 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
964 IMPLEMENT_CAST_CASE(Short , ( signed short));
965 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
966 IMPLEMENT_CAST_CASE(Int , ( signed int ));
967 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
968 IMPLEMENT_CAST_CASE(Long , ( int64_t));
969 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
970 IMPLEMENT_CAST_CASE(Float , (float));
971 IMPLEMENT_CAST_CASE(Double , (double));
972 IMPLEMENT_CAST_CASE(Bool , (bool));
974 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
981 void Interpreter::visitCastInst(CastInst &I) {
982 ExecutionContext &SF = ECStack.back();
983 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
986 void Interpreter::visitVANextInst(VANextInst &I) {
987 ExecutionContext &SF = ECStack.back();
989 // Get the incoming valist parameter. LLI treats the valist as a
990 // (ec-stack-depth var-arg-index) pair.
991 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
993 // Move the pointer to the next vararg.
994 ++VAList.UIntPairVal.second;
995 SetValue(&I, VAList, SF);
998 #define IMPLEMENT_VAARG(TY) \
999 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1001 void Interpreter::visitVAArgInst(VAArgInst &I) {
1002 ExecutionContext &SF = ECStack.back();
1004 // Get the incoming valist parameter. LLI treats the valist as a
1005 // (ec-stack-depth var-arg-index) pair.
1006 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1008 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1009 .VarArgs[VAList.UIntPairVal.second];
1010 const Type *Ty = I.getType();
1011 switch (Ty->getTypeID()) {
1012 IMPLEMENT_VAARG(UByte);
1013 IMPLEMENT_VAARG(SByte);
1014 IMPLEMENT_VAARG(UShort);
1015 IMPLEMENT_VAARG(Short);
1016 IMPLEMENT_VAARG(UInt);
1017 IMPLEMENT_VAARG(Int);
1018 IMPLEMENT_VAARG(ULong);
1019 IMPLEMENT_VAARG(Long);
1020 IMPLEMENT_VAARG(Pointer);
1021 IMPLEMENT_VAARG(Float);
1022 IMPLEMENT_VAARG(Double);
1023 IMPLEMENT_VAARG(Bool);
1025 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1029 // Set the Value of this Instruction.
1030 SetValue(&I, Dest, SF);
1033 //===----------------------------------------------------------------------===//
1034 // Dispatch and Execution Code
1035 //===----------------------------------------------------------------------===//
1037 //===----------------------------------------------------------------------===//
1038 // callFunction - Execute the specified function...
1040 void Interpreter::callFunction(Function *F,
1041 const std::vector<GenericValue> &ArgVals) {
1042 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1043 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1044 "Incorrect number of arguments passed into function call!");
1045 // Make a new stack frame... and fill it in.
1046 ECStack.push_back(ExecutionContext());
1047 ExecutionContext &StackFrame = ECStack.back();
1048 StackFrame.CurFunction = F;
1050 // Special handling for external functions.
1051 if (F->isExternal()) {
1052 GenericValue Result = callExternalFunction (F, ArgVals);
1053 // Simulate a 'ret' instruction of the appropriate type.
1054 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1058 // Get pointers to first LLVM BB & Instruction in function.
1059 StackFrame.CurBB = F->begin();
1060 StackFrame.CurInst = StackFrame.CurBB->begin();
1062 // Run through the function arguments and initialize their values...
1063 assert((ArgVals.size() == F->asize() ||
1064 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
1065 "Invalid number of values passed to function invocation!");
1067 // Handle non-varargs arguments...
1069 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
1070 SetValue(AI, ArgVals[i], StackFrame);
1072 // Handle varargs arguments...
1073 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1076 void Interpreter::run() {
1077 while (!ECStack.empty()) {
1078 // Interpret a single instruction & increment the "PC".
1079 ExecutionContext &SF = ECStack.back(); // Current stack frame
1080 Instruction &I = *SF.CurInst++; // Increment before execute
1082 // Track the number of dynamic instructions executed.
1085 DEBUG(std::cerr << "About to interpret: " << I);
1086 visit(I); // Dispatch to one of the visit* methods...