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/IntrinsicLowering.h"
20 #include "llvm/Intrinsics.h"
21 #include "llvm/Support/GetElementPtrTypeIterator.h"
22 #include "Support/Statistic.h"
23 #include "Support/Debug.h"
24 #include <cmath> // For fmod
28 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
30 Interpreter *TheEE = 0;
34 //===----------------------------------------------------------------------===//
35 // Value Manipulation code
36 //===----------------------------------------------------------------------===//
38 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
40 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
42 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
44 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
46 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
48 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
50 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
52 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
54 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
56 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
58 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
60 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
62 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
64 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
66 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
68 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
71 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
72 ExecutionContext &SF) {
73 switch (CE->getOpcode()) {
74 case Instruction::Cast:
75 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
76 case Instruction::GetElementPtr:
77 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
78 gep_type_end(CE), SF);
79 case Instruction::Add:
80 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
81 getOperandValue(CE->getOperand(1), SF),
82 CE->getOperand(0)->getType());
83 case Instruction::Sub:
84 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
85 getOperandValue(CE->getOperand(1), SF),
86 CE->getOperand(0)->getType());
87 case Instruction::Mul:
88 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
89 getOperandValue(CE->getOperand(1), SF),
90 CE->getOperand(0)->getType());
91 case Instruction::Div:
92 return executeDivInst(getOperandValue(CE->getOperand(0), SF),
93 getOperandValue(CE->getOperand(1), SF),
94 CE->getOperand(0)->getType());
95 case Instruction::Rem:
96 return executeRemInst(getOperandValue(CE->getOperand(0), SF),
97 getOperandValue(CE->getOperand(1), SF),
98 CE->getOperand(0)->getType());
99 case Instruction::And:
100 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
101 getOperandValue(CE->getOperand(1), SF),
102 CE->getOperand(0)->getType());
103 case Instruction::Or:
104 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
105 getOperandValue(CE->getOperand(1), SF),
106 CE->getOperand(0)->getType());
107 case Instruction::Xor:
108 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
109 getOperandValue(CE->getOperand(1), SF),
110 CE->getOperand(0)->getType());
111 case Instruction::SetEQ:
112 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
113 getOperandValue(CE->getOperand(1), SF),
114 CE->getOperand(0)->getType());
115 case Instruction::SetNE:
116 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
117 getOperandValue(CE->getOperand(1), SF),
118 CE->getOperand(0)->getType());
119 case Instruction::SetLE:
120 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
121 getOperandValue(CE->getOperand(1), SF),
122 CE->getOperand(0)->getType());
123 case Instruction::SetGE:
124 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
125 getOperandValue(CE->getOperand(1), SF),
126 CE->getOperand(0)->getType());
127 case Instruction::SetLT:
128 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
129 getOperandValue(CE->getOperand(1), SF),
130 CE->getOperand(0)->getType());
131 case Instruction::SetGT:
132 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
133 getOperandValue(CE->getOperand(1), SF),
134 CE->getOperand(0)->getType());
135 case Instruction::Shl:
136 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
137 getOperandValue(CE->getOperand(1), SF),
138 CE->getOperand(0)->getType());
139 case Instruction::Shr:
140 return executeShrInst(getOperandValue(CE->getOperand(0), SF),
141 getOperandValue(CE->getOperand(1), SF),
142 CE->getOperand(0)->getType());
145 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
147 return GenericValue();
151 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
152 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
153 return getConstantExprValue(CE, SF);
154 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
155 return getConstantValue(CPV);
156 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
157 return PTOGV(getPointerToGlobal(GV));
163 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
167 void Interpreter::initializeExecutionEngine() {
171 //===----------------------------------------------------------------------===//
172 // Binary Instruction Implementations
173 //===----------------------------------------------------------------------===//
175 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
176 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
178 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
181 switch (Ty->getPrimitiveID()) {
182 IMPLEMENT_BINARY_OPERATOR(+, UByte);
183 IMPLEMENT_BINARY_OPERATOR(+, SByte);
184 IMPLEMENT_BINARY_OPERATOR(+, UShort);
185 IMPLEMENT_BINARY_OPERATOR(+, Short);
186 IMPLEMENT_BINARY_OPERATOR(+, UInt);
187 IMPLEMENT_BINARY_OPERATOR(+, Int);
188 IMPLEMENT_BINARY_OPERATOR(+, ULong);
189 IMPLEMENT_BINARY_OPERATOR(+, Long);
190 IMPLEMENT_BINARY_OPERATOR(+, Float);
191 IMPLEMENT_BINARY_OPERATOR(+, Double);
193 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
199 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
202 switch (Ty->getPrimitiveID()) {
203 IMPLEMENT_BINARY_OPERATOR(-, UByte);
204 IMPLEMENT_BINARY_OPERATOR(-, SByte);
205 IMPLEMENT_BINARY_OPERATOR(-, UShort);
206 IMPLEMENT_BINARY_OPERATOR(-, Short);
207 IMPLEMENT_BINARY_OPERATOR(-, UInt);
208 IMPLEMENT_BINARY_OPERATOR(-, Int);
209 IMPLEMENT_BINARY_OPERATOR(-, ULong);
210 IMPLEMENT_BINARY_OPERATOR(-, Long);
211 IMPLEMENT_BINARY_OPERATOR(-, Float);
212 IMPLEMENT_BINARY_OPERATOR(-, Double);
214 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
220 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
223 switch (Ty->getPrimitiveID()) {
224 IMPLEMENT_BINARY_OPERATOR(*, UByte);
225 IMPLEMENT_BINARY_OPERATOR(*, SByte);
226 IMPLEMENT_BINARY_OPERATOR(*, UShort);
227 IMPLEMENT_BINARY_OPERATOR(*, Short);
228 IMPLEMENT_BINARY_OPERATOR(*, UInt);
229 IMPLEMENT_BINARY_OPERATOR(*, Int);
230 IMPLEMENT_BINARY_OPERATOR(*, ULong);
231 IMPLEMENT_BINARY_OPERATOR(*, Long);
232 IMPLEMENT_BINARY_OPERATOR(*, Float);
233 IMPLEMENT_BINARY_OPERATOR(*, Double);
235 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
241 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
244 switch (Ty->getPrimitiveID()) {
245 IMPLEMENT_BINARY_OPERATOR(/, UByte);
246 IMPLEMENT_BINARY_OPERATOR(/, SByte);
247 IMPLEMENT_BINARY_OPERATOR(/, UShort);
248 IMPLEMENT_BINARY_OPERATOR(/, Short);
249 IMPLEMENT_BINARY_OPERATOR(/, UInt);
250 IMPLEMENT_BINARY_OPERATOR(/, Int);
251 IMPLEMENT_BINARY_OPERATOR(/, ULong);
252 IMPLEMENT_BINARY_OPERATOR(/, Long);
253 IMPLEMENT_BINARY_OPERATOR(/, Float);
254 IMPLEMENT_BINARY_OPERATOR(/, Double);
256 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
262 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
265 switch (Ty->getPrimitiveID()) {
266 IMPLEMENT_BINARY_OPERATOR(%, UByte);
267 IMPLEMENT_BINARY_OPERATOR(%, SByte);
268 IMPLEMENT_BINARY_OPERATOR(%, UShort);
269 IMPLEMENT_BINARY_OPERATOR(%, Short);
270 IMPLEMENT_BINARY_OPERATOR(%, UInt);
271 IMPLEMENT_BINARY_OPERATOR(%, Int);
272 IMPLEMENT_BINARY_OPERATOR(%, ULong);
273 IMPLEMENT_BINARY_OPERATOR(%, Long);
274 case Type::FloatTyID:
275 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
277 case Type::DoubleTyID:
278 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
281 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
287 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
290 switch (Ty->getPrimitiveID()) {
291 IMPLEMENT_BINARY_OPERATOR(&, Bool);
292 IMPLEMENT_BINARY_OPERATOR(&, UByte);
293 IMPLEMENT_BINARY_OPERATOR(&, SByte);
294 IMPLEMENT_BINARY_OPERATOR(&, UShort);
295 IMPLEMENT_BINARY_OPERATOR(&, Short);
296 IMPLEMENT_BINARY_OPERATOR(&, UInt);
297 IMPLEMENT_BINARY_OPERATOR(&, Int);
298 IMPLEMENT_BINARY_OPERATOR(&, ULong);
299 IMPLEMENT_BINARY_OPERATOR(&, Long);
301 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
307 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
310 switch (Ty->getPrimitiveID()) {
311 IMPLEMENT_BINARY_OPERATOR(|, Bool);
312 IMPLEMENT_BINARY_OPERATOR(|, UByte);
313 IMPLEMENT_BINARY_OPERATOR(|, SByte);
314 IMPLEMENT_BINARY_OPERATOR(|, UShort);
315 IMPLEMENT_BINARY_OPERATOR(|, Short);
316 IMPLEMENT_BINARY_OPERATOR(|, UInt);
317 IMPLEMENT_BINARY_OPERATOR(|, Int);
318 IMPLEMENT_BINARY_OPERATOR(|, ULong);
319 IMPLEMENT_BINARY_OPERATOR(|, Long);
321 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
327 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
330 switch (Ty->getPrimitiveID()) {
331 IMPLEMENT_BINARY_OPERATOR(^, Bool);
332 IMPLEMENT_BINARY_OPERATOR(^, UByte);
333 IMPLEMENT_BINARY_OPERATOR(^, SByte);
334 IMPLEMENT_BINARY_OPERATOR(^, UShort);
335 IMPLEMENT_BINARY_OPERATOR(^, Short);
336 IMPLEMENT_BINARY_OPERATOR(^, UInt);
337 IMPLEMENT_BINARY_OPERATOR(^, Int);
338 IMPLEMENT_BINARY_OPERATOR(^, ULong);
339 IMPLEMENT_BINARY_OPERATOR(^, Long);
341 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
347 #define IMPLEMENT_SETCC(OP, TY) \
348 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
350 // Handle pointers specially because they must be compared with only as much
351 // width as the host has. We _do not_ want to be comparing 64 bit values when
352 // running on a 32-bit target, otherwise the upper 32 bits might mess up
353 // comparisons if they contain garbage.
354 #define IMPLEMENT_POINTERSETCC(OP) \
355 case Type::PointerTyID: \
356 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
357 (void*)(intptr_t)Src2.PointerVal; break
359 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
362 switch (Ty->getPrimitiveID()) {
363 IMPLEMENT_SETCC(==, UByte);
364 IMPLEMENT_SETCC(==, SByte);
365 IMPLEMENT_SETCC(==, UShort);
366 IMPLEMENT_SETCC(==, Short);
367 IMPLEMENT_SETCC(==, UInt);
368 IMPLEMENT_SETCC(==, Int);
369 IMPLEMENT_SETCC(==, ULong);
370 IMPLEMENT_SETCC(==, Long);
371 IMPLEMENT_SETCC(==, Float);
372 IMPLEMENT_SETCC(==, Double);
373 IMPLEMENT_POINTERSETCC(==);
375 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
381 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
384 switch (Ty->getPrimitiveID()) {
385 IMPLEMENT_SETCC(!=, UByte);
386 IMPLEMENT_SETCC(!=, SByte);
387 IMPLEMENT_SETCC(!=, UShort);
388 IMPLEMENT_SETCC(!=, Short);
389 IMPLEMENT_SETCC(!=, UInt);
390 IMPLEMENT_SETCC(!=, Int);
391 IMPLEMENT_SETCC(!=, ULong);
392 IMPLEMENT_SETCC(!=, Long);
393 IMPLEMENT_SETCC(!=, Float);
394 IMPLEMENT_SETCC(!=, Double);
395 IMPLEMENT_POINTERSETCC(!=);
398 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
404 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
407 switch (Ty->getPrimitiveID()) {
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 SetLE instruction: " << Ty << "\n";
426 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
429 switch (Ty->getPrimitiveID()) {
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(>=);
442 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
448 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
451 switch (Ty->getPrimitiveID()) {
452 IMPLEMENT_SETCC(<, UByte);
453 IMPLEMENT_SETCC(<, SByte);
454 IMPLEMENT_SETCC(<, UShort);
455 IMPLEMENT_SETCC(<, Short);
456 IMPLEMENT_SETCC(<, UInt);
457 IMPLEMENT_SETCC(<, Int);
458 IMPLEMENT_SETCC(<, ULong);
459 IMPLEMENT_SETCC(<, Long);
460 IMPLEMENT_SETCC(<, Float);
461 IMPLEMENT_SETCC(<, Double);
462 IMPLEMENT_POINTERSETCC(<);
464 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
470 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
473 switch (Ty->getPrimitiveID()) {
474 IMPLEMENT_SETCC(>, UByte);
475 IMPLEMENT_SETCC(>, SByte);
476 IMPLEMENT_SETCC(>, UShort);
477 IMPLEMENT_SETCC(>, Short);
478 IMPLEMENT_SETCC(>, UInt);
479 IMPLEMENT_SETCC(>, Int);
480 IMPLEMENT_SETCC(>, ULong);
481 IMPLEMENT_SETCC(>, Long);
482 IMPLEMENT_SETCC(>, Float);
483 IMPLEMENT_SETCC(>, Double);
484 IMPLEMENT_POINTERSETCC(>);
486 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
492 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
493 ExecutionContext &SF = ECStack.back();
494 const Type *Ty = I.getOperand(0)->getType();
495 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
496 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
497 GenericValue R; // Result
499 switch (I.getOpcode()) {
500 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
501 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
502 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
503 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
504 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
505 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
506 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
507 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
508 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
509 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
510 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
511 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
512 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
513 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
515 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
522 //===----------------------------------------------------------------------===//
523 // Terminator Instruction Implementations
524 //===----------------------------------------------------------------------===//
526 void Interpreter::exitCalled(GenericValue GV) {
527 runAtExitHandlers ();
531 /// Pop the last stack frame off of ECStack and then copy the result
532 /// back into the result variable if we are not returning void. The
533 /// result variable may be the ExitCode, or the Value of the calling
534 /// CallInst if there was a previous stack frame. This method may
535 /// invalidate any ECStack iterators you have. This method also takes
536 /// care of switching to the normal destination BB, if we are returning
539 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
540 GenericValue Result) {
541 // Pop the current stack frame.
544 if (ECStack.empty()) { // Finished main. Put result into exit code...
545 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
546 ExitCode = Result.IntVal; // Capture the exit code of the program
551 // If we have a previous stack frame, and we have a previous call,
552 // fill in the return value...
553 ExecutionContext &CallingSF = ECStack.back();
554 if (Instruction *I = CallingSF.Caller.getInstruction()) {
555 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
556 SetValue(I, Result, CallingSF);
557 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
558 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
559 CallingSF.Caller = CallSite(); // We returned from the call...
564 void Interpreter::visitReturnInst(ReturnInst &I) {
565 ExecutionContext &SF = ECStack.back();
566 const Type *RetTy = Type::VoidTy;
569 // Save away the return value... (if we are not 'ret void')
570 if (I.getNumOperands()) {
571 RetTy = I.getReturnValue()->getType();
572 Result = getOperandValue(I.getReturnValue(), SF);
575 popStackAndReturnValueToCaller(RetTy, Result);
578 void Interpreter::visitUnwindInst(UnwindInst &I) {
583 if (ECStack.empty ())
585 Inst = ECStack.back ().Caller.getInstruction ();
586 } while (!(Inst && isa<InvokeInst> (Inst)));
588 // Return from invoke
589 ExecutionContext &InvokingSF = ECStack.back ();
590 InvokingSF.Caller = CallSite ();
592 // Go to exceptional destination BB of invoke instruction
593 SwitchToNewBasicBlock (cast<InvokeInst> (Inst)->getExceptionalDest (),
597 void Interpreter::visitBranchInst(BranchInst &I) {
598 ExecutionContext &SF = ECStack.back();
601 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
602 if (!I.isUnconditional()) {
603 Value *Cond = I.getCondition();
604 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
605 Dest = I.getSuccessor(1);
607 SwitchToNewBasicBlock(Dest, SF);
610 void Interpreter::visitSwitchInst(SwitchInst &I) {
611 ExecutionContext &SF = ECStack.back();
612 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
613 const Type *ElTy = I.getOperand(0)->getType();
615 // Check to see if any of the cases match...
616 BasicBlock *Dest = 0;
617 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
618 if (executeSetEQInst(CondVal,
619 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
620 Dest = cast<BasicBlock>(I.getOperand(i+1));
624 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
625 SwitchToNewBasicBlock(Dest, SF);
628 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
629 // This function handles the actual updating of block and instruction iterators
630 // as well as execution of all of the PHI nodes in the destination block.
632 // This method does this because all of the PHI nodes must be executed
633 // atomically, reading their inputs before any of the results are updated. Not
634 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
635 // their inputs. If the input PHI node is updated before it is read, incorrect
636 // results can happen. Thus we use a two phase approach.
638 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
639 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
640 SF.CurBB = Dest; // Update CurBB to branch destination
641 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
643 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
645 // Loop over all of the PHI nodes in the current block, reading their inputs.
646 std::vector<GenericValue> ResultValues;
648 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
649 // Search for the value corresponding to this previous bb...
650 int i = PN->getBasicBlockIndex(PrevBB);
651 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
652 Value *IncomingValue = PN->getIncomingValue(i);
654 // Save the incoming value for this PHI node...
655 ResultValues.push_back(getOperandValue(IncomingValue, SF));
658 // Now loop over all of the PHI nodes setting their values...
659 SF.CurInst = SF.CurBB->begin();
660 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
662 SetValue(PN, ResultValues[i], SF);
665 //===----------------------------------------------------------------------===//
666 // Memory Instruction Implementations
667 //===----------------------------------------------------------------------===//
669 void Interpreter::visitAllocationInst(AllocationInst &I) {
670 ExecutionContext &SF = ECStack.back();
672 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
674 // Get the number of elements being allocated by the array...
675 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
677 // Allocate enough memory to hold the type...
678 void *Memory = malloc(NumElements * TD.getTypeSize(Ty));
680 GenericValue Result = PTOGV(Memory);
681 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
682 SetValue(&I, Result, SF);
684 if (I.getOpcode() == Instruction::Alloca)
685 ECStack.back().Allocas.add(Memory);
688 void Interpreter::visitFreeInst(FreeInst &I) {
689 ExecutionContext &SF = ECStack.back();
690 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
691 GenericValue Value = getOperandValue(I.getOperand(0), SF);
692 // TODO: Check to make sure memory is allocated
693 free(GVTOP(Value)); // Free memory
696 // getElementOffset - The workhorse for getelementptr.
698 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
700 ExecutionContext &SF) {
701 assert(isa<PointerType>(Ptr->getType()) &&
702 "Cannot getElementOffset of a nonpointer type!");
706 for (; I != E; ++I) {
707 if (const StructType *STy = dyn_cast<StructType>(*I)) {
708 const StructLayout *SLO = TD.getStructLayout(STy);
710 const ConstantUInt *CPU = cast<ConstantUInt>(I.getOperand());
711 unsigned Index = CPU->getValue();
713 Total += SLO->MemberOffsets[Index];
715 const SequentialType *ST = cast<SequentialType>(*I);
716 // Get the index number for the array... which must be long type...
717 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
720 switch (I.getOperand()->getType()->getPrimitiveID()) {
721 default: assert(0 && "Illegal getelementptr index for sequential type!");
722 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
723 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
724 case Type::IntTyID: Idx = IdxGV.IntVal; break;
725 case Type::LongTyID: Idx = IdxGV.LongVal; break;
726 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
727 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
728 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
729 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
731 Total += TD.getTypeSize(ST->getElementType())*Idx;
736 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
740 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
741 ExecutionContext &SF = ECStack.back();
742 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
743 gep_type_begin(I), gep_type_end(I), SF), SF);
746 void Interpreter::visitLoadInst(LoadInst &I) {
747 ExecutionContext &SF = ECStack.back();
748 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
749 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
750 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
751 SetValue(&I, Result, SF);
754 void Interpreter::visitStoreInst(StoreInst &I) {
755 ExecutionContext &SF = ECStack.back();
756 GenericValue Val = getOperandValue(I.getOperand(0), SF);
757 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
758 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
759 I.getOperand(0)->getType());
762 //===----------------------------------------------------------------------===//
763 // Miscellaneous Instruction Implementations
764 //===----------------------------------------------------------------------===//
766 void Interpreter::visitCallSite(CallSite CS) {
767 ExecutionContext &SF = ECStack.back();
769 // Check to see if this is an intrinsic function call...
770 if (Function *F = CS.getCalledFunction())
771 switch (F->getIntrinsicID()) {
772 case Intrinsic::va_start: // va_start: implemented by getFirstVarArg()
773 SetValue(CS.getInstruction(), getFirstVarArg(), SF);
775 case Intrinsic::va_end: // va_end is a noop for the interpreter
777 case Intrinsic::va_copy: // va_copy: dest = src
778 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
781 // If it is an unknown intrinsic function, using the intrinsic lowering
782 // class to transform it into hopefully tasty LLVM code.
784 Instruction *Prev = CS.getInstruction()->getPrev();
785 BasicBlock *Parent = CS.getInstruction()->getParent();
786 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
788 // Restore the CurInst pointer to the first instruction newly inserted, if
791 SF.CurInst = Parent->begin();
799 std::vector<GenericValue> ArgVals;
800 const unsigned NumArgs = SF.Caller.arg_size();
801 ArgVals.reserve(NumArgs);
802 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
803 e = SF.Caller.arg_end(); i != e; ++i) {
805 ArgVals.push_back(getOperandValue(V, SF));
806 // Promote all integral types whose size is < sizeof(int) into ints. We do
807 // this by zero or sign extending the value as appropriate according to the
809 const Type *Ty = V->getType();
810 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
811 if (Ty == Type::ShortTy)
812 ArgVals.back().IntVal = ArgVals.back().ShortVal;
813 else if (Ty == Type::UShortTy)
814 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
815 else if (Ty == Type::SByteTy)
816 ArgVals.back().IntVal = ArgVals.back().SByteVal;
817 else if (Ty == Type::UByteTy)
818 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
819 else if (Ty == Type::BoolTy)
820 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
822 assert(0 && "Unknown type!");
826 // To handle indirect calls, we must get the pointer value from the argument
827 // and treat it as a function pointer.
828 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
829 callFunction((Function*)GVTOP(SRC), ArgVals);
832 #define IMPLEMENT_SHIFT(OP, TY) \
833 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
835 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
838 switch (Ty->getPrimitiveID()) {
839 IMPLEMENT_SHIFT(<<, UByte);
840 IMPLEMENT_SHIFT(<<, SByte);
841 IMPLEMENT_SHIFT(<<, UShort);
842 IMPLEMENT_SHIFT(<<, Short);
843 IMPLEMENT_SHIFT(<<, UInt);
844 IMPLEMENT_SHIFT(<<, Int);
845 IMPLEMENT_SHIFT(<<, ULong);
846 IMPLEMENT_SHIFT(<<, Long);
848 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
853 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
856 switch (Ty->getPrimitiveID()) {
857 IMPLEMENT_SHIFT(>>, UByte);
858 IMPLEMENT_SHIFT(>>, SByte);
859 IMPLEMENT_SHIFT(>>, UShort);
860 IMPLEMENT_SHIFT(>>, Short);
861 IMPLEMENT_SHIFT(>>, UInt);
862 IMPLEMENT_SHIFT(>>, Int);
863 IMPLEMENT_SHIFT(>>, ULong);
864 IMPLEMENT_SHIFT(>>, Long);
866 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
872 void Interpreter::visitShl(ShiftInst &I) {
873 ExecutionContext &SF = ECStack.back();
874 const Type *Ty = I.getOperand(0)->getType();
875 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
876 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
878 Dest = executeShlInst (Src1, Src2, Ty);
879 SetValue(&I, Dest, SF);
882 void Interpreter::visitShr(ShiftInst &I) {
883 ExecutionContext &SF = ECStack.back();
884 const Type *Ty = I.getOperand(0)->getType();
885 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
886 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
888 Dest = executeShrInst (Src1, Src2, Ty);
889 SetValue(&I, Dest, SF);
892 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
893 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
895 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
896 case Type::DESTTY##TyID: \
897 switch (SrcTy->getPrimitiveID()) { \
898 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
899 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
900 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
901 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
902 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
903 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
904 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
905 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
906 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
907 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
909 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
910 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
911 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
913 #define IMPLEMENT_CAST_CASE_END() \
914 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
919 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
920 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
921 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
922 IMPLEMENT_CAST_CASE_END()
924 GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
925 ExecutionContext &SF) {
926 const Type *SrcTy = SrcVal->getType();
927 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
929 switch (Ty->getPrimitiveID()) {
930 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
931 IMPLEMENT_CAST_CASE(SByte , ( signed char));
932 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
933 IMPLEMENT_CAST_CASE(Short , ( signed short));
934 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
935 IMPLEMENT_CAST_CASE(Int , ( signed int ));
936 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
937 IMPLEMENT_CAST_CASE(Long , ( int64_t));
938 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
939 IMPLEMENT_CAST_CASE(Float , (float));
940 IMPLEMENT_CAST_CASE(Double , (double));
941 IMPLEMENT_CAST_CASE(Bool , (bool));
943 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
950 void Interpreter::visitCastInst(CastInst &I) {
951 ExecutionContext &SF = ECStack.back();
952 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
955 void Interpreter::visitVANextInst(VANextInst &I) {
956 ExecutionContext &SF = ECStack.back();
958 // Get the incoming valist parameter. LLI treats the valist as a pointer
959 // to the next argument.
960 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
962 // Move the pointer to the next vararg.
963 GenericValue *ArgPtr = (GenericValue *) GVTOP (VAList);
965 VAList = PTOGV (ArgPtr);
966 SetValue(&I, VAList, SF);
969 #define IMPLEMENT_VAARG(TY) \
970 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
972 void Interpreter::visitVAArgInst(VAArgInst &I) {
973 ExecutionContext &SF = ECStack.back();
975 // Get the incoming valist parameter. LLI treats the valist as a pointer
976 // to the next argument.
977 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
978 assert (GVTOP (VAList) != 0 && "VAList was null in vaarg instruction");
979 GenericValue Dest, Src = *(GenericValue *) GVTOP (VAList);
980 const Type *Ty = I.getType();
981 switch (Ty->getPrimitiveID()) {
982 IMPLEMENT_VAARG(UByte);
983 IMPLEMENT_VAARG(SByte);
984 IMPLEMENT_VAARG(UShort);
985 IMPLEMENT_VAARG(Short);
986 IMPLEMENT_VAARG(UInt);
987 IMPLEMENT_VAARG(Int);
988 IMPLEMENT_VAARG(ULong);
989 IMPLEMENT_VAARG(Long);
990 IMPLEMENT_VAARG(Pointer);
991 IMPLEMENT_VAARG(Float);
992 IMPLEMENT_VAARG(Double);
993 IMPLEMENT_VAARG(Bool);
995 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
999 // Set the Value of this Instruction.
1000 SetValue(&I, Dest, SF);
1003 //===----------------------------------------------------------------------===//
1004 // Dispatch and Execution Code
1005 //===----------------------------------------------------------------------===//
1007 //===----------------------------------------------------------------------===//
1008 // callFunction - Execute the specified function...
1010 void Interpreter::callFunction(Function *F,
1011 const std::vector<GenericValue> &ArgVals) {
1012 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1013 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1014 "Incorrect number of arguments passed into function call!");
1015 // Make a new stack frame... and fill it in.
1016 ECStack.push_back(ExecutionContext());
1017 ExecutionContext &StackFrame = ECStack.back();
1018 StackFrame.CurFunction = F;
1020 // Special handling for external functions.
1021 if (F->isExternal()) {
1022 GenericValue Result = callExternalFunction (F, ArgVals);
1023 // Simulate a 'ret' instruction of the appropriate type.
1024 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1028 // Get pointers to first LLVM BB & Instruction in function.
1029 StackFrame.CurBB = F->begin();
1030 StackFrame.CurInst = StackFrame.CurBB->begin();
1032 // Run through the function arguments and initialize their values...
1033 assert((ArgVals.size() == F->asize() ||
1034 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
1035 "Invalid number of values passed to function invocation!");
1037 // Handle non-varargs arguments...
1039 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
1040 SetValue(AI, ArgVals[i], StackFrame);
1042 // Handle varargs arguments...
1043 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1046 void Interpreter::run() {
1047 while (!ECStack.empty()) {
1048 // Interpret a single instruction & increment the "PC".
1049 ExecutionContext &SF = ECStack.back(); // Current stack frame
1050 Instruction &I = *SF.CurInst++; // Increment before execute
1052 // Track the number of dynamic instructions executed.
1055 DEBUG(std::cerr << "About to interpret: " << I);
1056 visit(I); // Dispatch to one of the visit* methods...