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 "Support/Statistic.h"
22 #include "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; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
684 SetValue(PN, ResultValues[i], SF);
687 //===----------------------------------------------------------------------===//
688 // Memory Instruction Implementations
689 //===----------------------------------------------------------------------===//
691 void Interpreter::visitAllocationInst(AllocationInst &I) {
692 ExecutionContext &SF = ECStack.back();
694 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
696 // Get the number of elements being allocated by the array...
697 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
699 // Allocate enough memory to hold the type...
700 void *Memory = malloc(NumElements * TD.getTypeSize(Ty));
702 GenericValue Result = PTOGV(Memory);
703 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
704 SetValue(&I, Result, SF);
706 if (I.getOpcode() == Instruction::Alloca)
707 ECStack.back().Allocas.add(Memory);
710 void Interpreter::visitFreeInst(FreeInst &I) {
711 ExecutionContext &SF = ECStack.back();
712 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
713 GenericValue Value = getOperandValue(I.getOperand(0), SF);
714 // TODO: Check to make sure memory is allocated
715 free(GVTOP(Value)); // Free memory
718 // getElementOffset - The workhorse for getelementptr.
720 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
722 ExecutionContext &SF) {
723 assert(isa<PointerType>(Ptr->getType()) &&
724 "Cannot getElementOffset of a nonpointer type!");
728 for (; I != E; ++I) {
729 if (const StructType *STy = dyn_cast<StructType>(*I)) {
730 const StructLayout *SLO = TD.getStructLayout(STy);
732 const ConstantUInt *CPU = cast<ConstantUInt>(I.getOperand());
733 unsigned Index = CPU->getValue();
735 Total += SLO->MemberOffsets[Index];
737 const SequentialType *ST = cast<SequentialType>(*I);
738 // Get the index number for the array... which must be long type...
739 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
742 switch (I.getOperand()->getType()->getTypeID()) {
743 default: assert(0 && "Illegal getelementptr index for sequential type!");
744 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
745 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
746 case Type::IntTyID: Idx = IdxGV.IntVal; break;
747 case Type::LongTyID: Idx = IdxGV.LongVal; break;
748 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
749 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
750 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
751 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
753 Total += TD.getTypeSize(ST->getElementType())*Idx;
758 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
762 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
763 ExecutionContext &SF = ECStack.back();
764 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
765 gep_type_begin(I), gep_type_end(I), SF), SF);
768 void Interpreter::visitLoadInst(LoadInst &I) {
769 ExecutionContext &SF = ECStack.back();
770 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
771 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
772 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
773 SetValue(&I, Result, SF);
776 void Interpreter::visitStoreInst(StoreInst &I) {
777 ExecutionContext &SF = ECStack.back();
778 GenericValue Val = getOperandValue(I.getOperand(0), SF);
779 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
780 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
781 I.getOperand(0)->getType());
784 //===----------------------------------------------------------------------===//
785 // Miscellaneous Instruction Implementations
786 //===----------------------------------------------------------------------===//
788 void Interpreter::visitCallSite(CallSite CS) {
789 ExecutionContext &SF = ECStack.back();
791 // Check to see if this is an intrinsic function call...
792 if (Function *F = CS.getCalledFunction())
793 if (F->isExternal ())
794 switch (F->getIntrinsicID()) {
795 case Intrinsic::not_intrinsic:
797 case Intrinsic::vastart: { // va_start
798 GenericValue ArgIndex;
799 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
800 ArgIndex.UIntPairVal.second = 0;
801 SetValue(CS.getInstruction(), ArgIndex, SF);
804 case Intrinsic::vaend: // va_end is a noop for the interpreter
806 case Intrinsic::vacopy: // va_copy: dest = src
807 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
810 // If it is an unknown intrinsic function, use the intrinsic lowering
811 // class to transform it into hopefully tasty LLVM code.
813 Instruction *Prev = CS.getInstruction()->getPrev();
814 BasicBlock *Parent = CS.getInstruction()->getParent();
815 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
817 // Restore the CurInst pointer to the first instruction newly inserted, if
820 SF.CurInst = Parent->begin();
829 std::vector<GenericValue> ArgVals;
830 const unsigned NumArgs = SF.Caller.arg_size();
831 ArgVals.reserve(NumArgs);
832 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
833 e = SF.Caller.arg_end(); i != e; ++i) {
835 ArgVals.push_back(getOperandValue(V, SF));
836 // Promote all integral types whose size is < sizeof(int) into ints. We do
837 // this by zero or sign extending the value as appropriate according to the
839 const Type *Ty = V->getType();
840 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
841 if (Ty == Type::ShortTy)
842 ArgVals.back().IntVal = ArgVals.back().ShortVal;
843 else if (Ty == Type::UShortTy)
844 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
845 else if (Ty == Type::SByteTy)
846 ArgVals.back().IntVal = ArgVals.back().SByteVal;
847 else if (Ty == Type::UByteTy)
848 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
849 else if (Ty == Type::BoolTy)
850 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
852 assert(0 && "Unknown type!");
856 // To handle indirect calls, we must get the pointer value from the argument
857 // and treat it as a function pointer.
858 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
859 callFunction((Function*)GVTOP(SRC), ArgVals);
862 #define IMPLEMENT_SHIFT(OP, TY) \
863 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
865 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
868 switch (Ty->getTypeID()) {
869 IMPLEMENT_SHIFT(<<, UByte);
870 IMPLEMENT_SHIFT(<<, SByte);
871 IMPLEMENT_SHIFT(<<, UShort);
872 IMPLEMENT_SHIFT(<<, Short);
873 IMPLEMENT_SHIFT(<<, UInt);
874 IMPLEMENT_SHIFT(<<, Int);
875 IMPLEMENT_SHIFT(<<, ULong);
876 IMPLEMENT_SHIFT(<<, Long);
878 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
883 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
886 switch (Ty->getTypeID()) {
887 IMPLEMENT_SHIFT(>>, UByte);
888 IMPLEMENT_SHIFT(>>, SByte);
889 IMPLEMENT_SHIFT(>>, UShort);
890 IMPLEMENT_SHIFT(>>, Short);
891 IMPLEMENT_SHIFT(>>, UInt);
892 IMPLEMENT_SHIFT(>>, Int);
893 IMPLEMENT_SHIFT(>>, ULong);
894 IMPLEMENT_SHIFT(>>, Long);
896 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
902 void Interpreter::visitShl(ShiftInst &I) {
903 ExecutionContext &SF = ECStack.back();
904 const Type *Ty = I.getOperand(0)->getType();
905 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
906 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
908 Dest = executeShlInst (Src1, Src2, Ty);
909 SetValue(&I, Dest, SF);
912 void Interpreter::visitShr(ShiftInst &I) {
913 ExecutionContext &SF = ECStack.back();
914 const Type *Ty = I.getOperand(0)->getType();
915 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
916 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
918 Dest = executeShrInst (Src1, Src2, Ty);
919 SetValue(&I, Dest, SF);
922 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
923 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
925 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
926 case Type::DESTTY##TyID: \
927 switch (SrcTy->getTypeID()) { \
928 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
929 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
930 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
931 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
932 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
933 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
934 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
935 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
936 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
937 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
939 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
940 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
941 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
943 #define IMPLEMENT_CAST_CASE_END() \
944 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
949 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
950 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
951 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
952 IMPLEMENT_CAST_CASE_END()
954 GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
955 ExecutionContext &SF) {
956 const Type *SrcTy = SrcVal->getType();
957 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
959 switch (Ty->getTypeID()) {
960 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
961 IMPLEMENT_CAST_CASE(SByte , ( signed char));
962 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
963 IMPLEMENT_CAST_CASE(Short , ( signed short));
964 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
965 IMPLEMENT_CAST_CASE(Int , ( signed int ));
966 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
967 IMPLEMENT_CAST_CASE(Long , ( int64_t));
968 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
969 IMPLEMENT_CAST_CASE(Float , (float));
970 IMPLEMENT_CAST_CASE(Double , (double));
971 IMPLEMENT_CAST_CASE(Bool , (bool));
973 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
980 void Interpreter::visitCastInst(CastInst &I) {
981 ExecutionContext &SF = ECStack.back();
982 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
985 void Interpreter::visitVANextInst(VANextInst &I) {
986 ExecutionContext &SF = ECStack.back();
988 // Get the incoming valist parameter. LLI treats the valist as a
989 // (ec-stack-depth var-arg-index) pair.
990 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
992 // Move the pointer to the next vararg.
993 ++VAList.UIntPairVal.second;
994 SetValue(&I, VAList, SF);
997 #define IMPLEMENT_VAARG(TY) \
998 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1000 void Interpreter::visitVAArgInst(VAArgInst &I) {
1001 ExecutionContext &SF = ECStack.back();
1003 // Get the incoming valist parameter. LLI treats the valist as a
1004 // (ec-stack-depth var-arg-index) pair.
1005 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1007 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1008 .VarArgs[VAList.UIntPairVal.second];
1009 const Type *Ty = I.getType();
1010 switch (Ty->getTypeID()) {
1011 IMPLEMENT_VAARG(UByte);
1012 IMPLEMENT_VAARG(SByte);
1013 IMPLEMENT_VAARG(UShort);
1014 IMPLEMENT_VAARG(Short);
1015 IMPLEMENT_VAARG(UInt);
1016 IMPLEMENT_VAARG(Int);
1017 IMPLEMENT_VAARG(ULong);
1018 IMPLEMENT_VAARG(Long);
1019 IMPLEMENT_VAARG(Pointer);
1020 IMPLEMENT_VAARG(Float);
1021 IMPLEMENT_VAARG(Double);
1022 IMPLEMENT_VAARG(Bool);
1024 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1028 // Set the Value of this Instruction.
1029 SetValue(&I, Dest, SF);
1032 //===----------------------------------------------------------------------===//
1033 // Dispatch and Execution Code
1034 //===----------------------------------------------------------------------===//
1036 //===----------------------------------------------------------------------===//
1037 // callFunction - Execute the specified function...
1039 void Interpreter::callFunction(Function *F,
1040 const std::vector<GenericValue> &ArgVals) {
1041 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1042 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1043 "Incorrect number of arguments passed into function call!");
1044 // Make a new stack frame... and fill it in.
1045 ECStack.push_back(ExecutionContext());
1046 ExecutionContext &StackFrame = ECStack.back();
1047 StackFrame.CurFunction = F;
1049 // Special handling for external functions.
1050 if (F->isExternal()) {
1051 GenericValue Result = callExternalFunction (F, ArgVals);
1052 // Simulate a 'ret' instruction of the appropriate type.
1053 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1057 // Get pointers to first LLVM BB & Instruction in function.
1058 StackFrame.CurBB = F->begin();
1059 StackFrame.CurInst = StackFrame.CurBB->begin();
1061 // Run through the function arguments and initialize their values...
1062 assert((ArgVals.size() == F->asize() ||
1063 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
1064 "Invalid number of values passed to function invocation!");
1066 // Handle non-varargs arguments...
1068 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
1069 SetValue(AI, ArgVals[i], StackFrame);
1071 // Handle varargs arguments...
1072 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1075 void Interpreter::run() {
1076 while (!ECStack.empty()) {
1077 // Interpret a single instruction & increment the "PC".
1078 ExecutionContext &SF = ECStack.back(); // Current stack frame
1079 Instruction &I = *SF.CurInst++; // Increment before execute
1081 // Track the number of dynamic instructions executed.
1084 DEBUG(std::cerr << "About to interpret: " << I);
1085 visit(I); // Dispatch to one of the visit* methods...