1 //===-- Execution.cpp - Implement code to simulate the program ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the actual instruction interpreter.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "interpreter"
15 #include "Interpreter.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/CodeGen/IntrinsicLowering.h"
20 #include "llvm/Support/GetElementPtrTypeIterator.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Support/Debug.h"
23 #include <cmath> // For fmod
27 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
29 Interpreter *TheEE = 0;
33 //===----------------------------------------------------------------------===//
34 // Value Manipulation code
35 //===----------------------------------------------------------------------===//
37 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
39 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
41 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
43 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
45 static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
47 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
49 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
51 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
53 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
55 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
57 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
59 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
61 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
63 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
65 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
67 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
69 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
71 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
73 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
76 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
77 ExecutionContext &SF) {
78 switch (CE->getOpcode()) {
79 case Instruction::Cast:
80 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
81 case Instruction::GetElementPtr:
82 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
83 gep_type_end(CE), SF);
84 case Instruction::Add:
85 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
86 getOperandValue(CE->getOperand(1), SF),
87 CE->getOperand(0)->getType());
88 case Instruction::Sub:
89 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
90 getOperandValue(CE->getOperand(1), SF),
91 CE->getOperand(0)->getType());
92 case Instruction::Mul:
93 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
94 getOperandValue(CE->getOperand(1), SF),
95 CE->getOperand(0)->getType());
96 case Instruction::SDiv:
97 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
98 getOperandValue(CE->getOperand(1), SF),
99 CE->getOperand(0)->getType());
100 case Instruction::UDiv:
101 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
102 getOperandValue(CE->getOperand(1), SF),
103 CE->getOperand(0)->getType());
104 case Instruction::FDiv:
105 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
106 getOperandValue(CE->getOperand(1), SF),
107 CE->getOperand(0)->getType());
108 case Instruction::Rem:
109 return executeRemInst(getOperandValue(CE->getOperand(0), SF),
110 getOperandValue(CE->getOperand(1), SF),
111 CE->getOperand(0)->getType());
112 case Instruction::And:
113 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
114 getOperandValue(CE->getOperand(1), SF),
115 CE->getOperand(0)->getType());
116 case Instruction::Or:
117 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
118 getOperandValue(CE->getOperand(1), SF),
119 CE->getOperand(0)->getType());
120 case Instruction::Xor:
121 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
122 getOperandValue(CE->getOperand(1), SF),
123 CE->getOperand(0)->getType());
124 case Instruction::SetEQ:
125 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
126 getOperandValue(CE->getOperand(1), SF),
127 CE->getOperand(0)->getType());
128 case Instruction::SetNE:
129 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
130 getOperandValue(CE->getOperand(1), SF),
131 CE->getOperand(0)->getType());
132 case Instruction::SetLE:
133 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
134 getOperandValue(CE->getOperand(1), SF),
135 CE->getOperand(0)->getType());
136 case Instruction::SetGE:
137 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
138 getOperandValue(CE->getOperand(1), SF),
139 CE->getOperand(0)->getType());
140 case Instruction::SetLT:
141 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
142 getOperandValue(CE->getOperand(1), SF),
143 CE->getOperand(0)->getType());
144 case Instruction::SetGT:
145 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
146 getOperandValue(CE->getOperand(1), SF),
147 CE->getOperand(0)->getType());
148 case Instruction::Shl:
149 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
150 getOperandValue(CE->getOperand(1), SF),
151 CE->getOperand(0)->getType());
152 case Instruction::Shr:
153 return executeShrInst(getOperandValue(CE->getOperand(0), SF),
154 getOperandValue(CE->getOperand(1), SF),
155 CE->getOperand(0)->getType());
156 case Instruction::Select:
157 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
158 getOperandValue(CE->getOperand(1), SF),
159 getOperandValue(CE->getOperand(2), SF));
161 std::cerr << "Unhandled ConstantExpr: " << *CE << "\n";
163 return GenericValue();
167 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
168 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
169 return getConstantExprValue(CE, SF);
170 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
171 return getConstantValue(CPV);
172 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
173 return PTOGV(getPointerToGlobal(GV));
179 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
183 void Interpreter::initializeExecutionEngine() {
187 //===----------------------------------------------------------------------===//
188 // Binary Instruction Implementations
189 //===----------------------------------------------------------------------===//
191 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
192 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
194 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
197 switch (Ty->getTypeID()) {
198 IMPLEMENT_BINARY_OPERATOR(+, UByte);
199 IMPLEMENT_BINARY_OPERATOR(+, SByte);
200 IMPLEMENT_BINARY_OPERATOR(+, UShort);
201 IMPLEMENT_BINARY_OPERATOR(+, Short);
202 IMPLEMENT_BINARY_OPERATOR(+, UInt);
203 IMPLEMENT_BINARY_OPERATOR(+, Int);
204 IMPLEMENT_BINARY_OPERATOR(+, ULong);
205 IMPLEMENT_BINARY_OPERATOR(+, Long);
206 IMPLEMENT_BINARY_OPERATOR(+, Float);
207 IMPLEMENT_BINARY_OPERATOR(+, Double);
209 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
215 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
218 switch (Ty->getTypeID()) {
219 IMPLEMENT_BINARY_OPERATOR(-, UByte);
220 IMPLEMENT_BINARY_OPERATOR(-, SByte);
221 IMPLEMENT_BINARY_OPERATOR(-, UShort);
222 IMPLEMENT_BINARY_OPERATOR(-, Short);
223 IMPLEMENT_BINARY_OPERATOR(-, UInt);
224 IMPLEMENT_BINARY_OPERATOR(-, Int);
225 IMPLEMENT_BINARY_OPERATOR(-, ULong);
226 IMPLEMENT_BINARY_OPERATOR(-, Long);
227 IMPLEMENT_BINARY_OPERATOR(-, Float);
228 IMPLEMENT_BINARY_OPERATOR(-, Double);
230 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
236 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
239 switch (Ty->getTypeID()) {
240 IMPLEMENT_BINARY_OPERATOR(*, UByte);
241 IMPLEMENT_BINARY_OPERATOR(*, SByte);
242 IMPLEMENT_BINARY_OPERATOR(*, UShort);
243 IMPLEMENT_BINARY_OPERATOR(*, Short);
244 IMPLEMENT_BINARY_OPERATOR(*, UInt);
245 IMPLEMENT_BINARY_OPERATOR(*, Int);
246 IMPLEMENT_BINARY_OPERATOR(*, ULong);
247 IMPLEMENT_BINARY_OPERATOR(*, Long);
248 IMPLEMENT_BINARY_OPERATOR(*, Float);
249 IMPLEMENT_BINARY_OPERATOR(*, Double);
251 std::cout << "Unhandled type for Mul instruction: " << *Ty << "\n";
257 #define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
258 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
260 static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
263 switch (Ty->getTypeID()) {
264 IMPLEMENT_SIGNLESS_BINOP(/, UByte, SByte);
265 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
266 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
267 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
269 std::cout << "Unhandled type for UDiv instruction: " << *Ty << "\n";
275 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
278 switch (Ty->getTypeID()) {
279 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
280 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
281 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
282 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
284 std::cout << "Unhandled type for SDiv instruction: " << *Ty << "\n";
290 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
293 switch (Ty->getTypeID()) {
294 IMPLEMENT_BINARY_OPERATOR(/, Float);
295 IMPLEMENT_BINARY_OPERATOR(/, Double);
297 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
303 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
306 switch (Ty->getTypeID()) {
307 IMPLEMENT_BINARY_OPERATOR(%, UByte);
308 IMPLEMENT_BINARY_OPERATOR(%, SByte);
309 IMPLEMENT_BINARY_OPERATOR(%, UShort);
310 IMPLEMENT_BINARY_OPERATOR(%, Short);
311 IMPLEMENT_BINARY_OPERATOR(%, UInt);
312 IMPLEMENT_BINARY_OPERATOR(%, Int);
313 IMPLEMENT_BINARY_OPERATOR(%, ULong);
314 IMPLEMENT_BINARY_OPERATOR(%, Long);
315 case Type::FloatTyID:
316 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
318 case Type::DoubleTyID:
319 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
322 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
328 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
331 switch (Ty->getTypeID()) {
332 IMPLEMENT_BINARY_OPERATOR(&, Bool);
333 IMPLEMENT_BINARY_OPERATOR(&, UByte);
334 IMPLEMENT_BINARY_OPERATOR(&, SByte);
335 IMPLEMENT_BINARY_OPERATOR(&, UShort);
336 IMPLEMENT_BINARY_OPERATOR(&, Short);
337 IMPLEMENT_BINARY_OPERATOR(&, UInt);
338 IMPLEMENT_BINARY_OPERATOR(&, Int);
339 IMPLEMENT_BINARY_OPERATOR(&, ULong);
340 IMPLEMENT_BINARY_OPERATOR(&, Long);
342 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
348 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
351 switch (Ty->getTypeID()) {
352 IMPLEMENT_BINARY_OPERATOR(|, Bool);
353 IMPLEMENT_BINARY_OPERATOR(|, UByte);
354 IMPLEMENT_BINARY_OPERATOR(|, SByte);
355 IMPLEMENT_BINARY_OPERATOR(|, UShort);
356 IMPLEMENT_BINARY_OPERATOR(|, Short);
357 IMPLEMENT_BINARY_OPERATOR(|, UInt);
358 IMPLEMENT_BINARY_OPERATOR(|, Int);
359 IMPLEMENT_BINARY_OPERATOR(|, ULong);
360 IMPLEMENT_BINARY_OPERATOR(|, Long);
362 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
368 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
371 switch (Ty->getTypeID()) {
372 IMPLEMENT_BINARY_OPERATOR(^, Bool);
373 IMPLEMENT_BINARY_OPERATOR(^, UByte);
374 IMPLEMENT_BINARY_OPERATOR(^, SByte);
375 IMPLEMENT_BINARY_OPERATOR(^, UShort);
376 IMPLEMENT_BINARY_OPERATOR(^, Short);
377 IMPLEMENT_BINARY_OPERATOR(^, UInt);
378 IMPLEMENT_BINARY_OPERATOR(^, Int);
379 IMPLEMENT_BINARY_OPERATOR(^, ULong);
380 IMPLEMENT_BINARY_OPERATOR(^, Long);
382 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
388 #define IMPLEMENT_SETCC(OP, TY) \
389 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
391 // Handle pointers specially because they must be compared with only as much
392 // width as the host has. We _do not_ want to be comparing 64 bit values when
393 // running on a 32-bit target, otherwise the upper 32 bits might mess up
394 // comparisons if they contain garbage.
395 #define IMPLEMENT_POINTERSETCC(OP) \
396 case Type::PointerTyID: \
397 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
398 (void*)(intptr_t)Src2.PointerVal; break
400 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
403 switch (Ty->getTypeID()) {
404 IMPLEMENT_SETCC(==, UByte);
405 IMPLEMENT_SETCC(==, SByte);
406 IMPLEMENT_SETCC(==, UShort);
407 IMPLEMENT_SETCC(==, Short);
408 IMPLEMENT_SETCC(==, UInt);
409 IMPLEMENT_SETCC(==, Int);
410 IMPLEMENT_SETCC(==, ULong);
411 IMPLEMENT_SETCC(==, Long);
412 IMPLEMENT_SETCC(==, Float);
413 IMPLEMENT_SETCC(==, Double);
414 IMPLEMENT_POINTERSETCC(==);
416 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
422 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
425 switch (Ty->getTypeID()) {
426 IMPLEMENT_SETCC(!=, UByte);
427 IMPLEMENT_SETCC(!=, SByte);
428 IMPLEMENT_SETCC(!=, UShort);
429 IMPLEMENT_SETCC(!=, Short);
430 IMPLEMENT_SETCC(!=, UInt);
431 IMPLEMENT_SETCC(!=, Int);
432 IMPLEMENT_SETCC(!=, ULong);
433 IMPLEMENT_SETCC(!=, Long);
434 IMPLEMENT_SETCC(!=, Float);
435 IMPLEMENT_SETCC(!=, Double);
436 IMPLEMENT_POINTERSETCC(!=);
439 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
445 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
448 switch (Ty->getTypeID()) {
449 IMPLEMENT_SETCC(<=, UByte);
450 IMPLEMENT_SETCC(<=, SByte);
451 IMPLEMENT_SETCC(<=, UShort);
452 IMPLEMENT_SETCC(<=, Short);
453 IMPLEMENT_SETCC(<=, UInt);
454 IMPLEMENT_SETCC(<=, Int);
455 IMPLEMENT_SETCC(<=, ULong);
456 IMPLEMENT_SETCC(<=, Long);
457 IMPLEMENT_SETCC(<=, Float);
458 IMPLEMENT_SETCC(<=, Double);
459 IMPLEMENT_POINTERSETCC(<=);
461 std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
467 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
470 switch (Ty->getTypeID()) {
471 IMPLEMENT_SETCC(>=, UByte);
472 IMPLEMENT_SETCC(>=, SByte);
473 IMPLEMENT_SETCC(>=, UShort);
474 IMPLEMENT_SETCC(>=, Short);
475 IMPLEMENT_SETCC(>=, UInt);
476 IMPLEMENT_SETCC(>=, Int);
477 IMPLEMENT_SETCC(>=, ULong);
478 IMPLEMENT_SETCC(>=, Long);
479 IMPLEMENT_SETCC(>=, Float);
480 IMPLEMENT_SETCC(>=, Double);
481 IMPLEMENT_POINTERSETCC(>=);
483 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
489 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
492 switch (Ty->getTypeID()) {
493 IMPLEMENT_SETCC(<, UByte);
494 IMPLEMENT_SETCC(<, SByte);
495 IMPLEMENT_SETCC(<, UShort);
496 IMPLEMENT_SETCC(<, Short);
497 IMPLEMENT_SETCC(<, UInt);
498 IMPLEMENT_SETCC(<, Int);
499 IMPLEMENT_SETCC(<, ULong);
500 IMPLEMENT_SETCC(<, Long);
501 IMPLEMENT_SETCC(<, Float);
502 IMPLEMENT_SETCC(<, Double);
503 IMPLEMENT_POINTERSETCC(<);
505 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
511 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
514 switch (Ty->getTypeID()) {
515 IMPLEMENT_SETCC(>, UByte);
516 IMPLEMENT_SETCC(>, SByte);
517 IMPLEMENT_SETCC(>, UShort);
518 IMPLEMENT_SETCC(>, Short);
519 IMPLEMENT_SETCC(>, UInt);
520 IMPLEMENT_SETCC(>, Int);
521 IMPLEMENT_SETCC(>, ULong);
522 IMPLEMENT_SETCC(>, Long);
523 IMPLEMENT_SETCC(>, Float);
524 IMPLEMENT_SETCC(>, Double);
525 IMPLEMENT_POINTERSETCC(>);
527 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
533 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
534 ExecutionContext &SF = ECStack.back();
535 const Type *Ty = I.getOperand(0)->getType();
536 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
537 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
538 GenericValue R; // Result
540 switch (I.getOpcode()) {
541 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
542 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
543 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
544 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
545 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
546 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
547 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
548 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
549 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
550 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
551 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
552 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
553 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
554 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
555 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
556 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
558 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
565 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
567 return Src1.BoolVal ? Src2 : Src3;
570 void Interpreter::visitSelectInst(SelectInst &I) {
571 ExecutionContext &SF = ECStack.back();
572 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
573 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
574 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
575 GenericValue R = executeSelectInst(Src1, Src2, Src3);
580 //===----------------------------------------------------------------------===//
581 // Terminator Instruction Implementations
582 //===----------------------------------------------------------------------===//
584 void Interpreter::exitCalled(GenericValue GV) {
585 // runAtExitHandlers() assumes there are no stack frames, but
586 // if exit() was called, then it had a stack frame. Blow away
587 // the stack before interpreting atexit handlers.
589 runAtExitHandlers ();
593 /// Pop the last stack frame off of ECStack and then copy the result
594 /// back into the result variable if we are not returning void. The
595 /// result variable may be the ExitValue, or the Value of the calling
596 /// CallInst if there was a previous stack frame. This method may
597 /// invalidate any ECStack iterators you have. This method also takes
598 /// care of switching to the normal destination BB, if we are returning
601 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
602 GenericValue Result) {
603 // Pop the current stack frame.
606 if (ECStack.empty()) { // Finished main. Put result into exit code...
607 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
608 ExitValue = Result; // Capture the exit value of the program
610 memset(&ExitValue, 0, sizeof(ExitValue));
613 // If we have a previous stack frame, and we have a previous call,
614 // fill in the return value...
615 ExecutionContext &CallingSF = ECStack.back();
616 if (Instruction *I = CallingSF.Caller.getInstruction()) {
617 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
618 SetValue(I, Result, CallingSF);
619 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
620 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
621 CallingSF.Caller = CallSite(); // We returned from the call...
626 void Interpreter::visitReturnInst(ReturnInst &I) {
627 ExecutionContext &SF = ECStack.back();
628 const Type *RetTy = Type::VoidTy;
631 // Save away the return value... (if we are not 'ret void')
632 if (I.getNumOperands()) {
633 RetTy = I.getReturnValue()->getType();
634 Result = getOperandValue(I.getReturnValue(), SF);
637 popStackAndReturnValueToCaller(RetTy, Result);
640 void Interpreter::visitUnwindInst(UnwindInst &I) {
645 if (ECStack.empty ())
647 Inst = ECStack.back ().Caller.getInstruction ();
648 } while (!(Inst && isa<InvokeInst> (Inst)));
650 // Return from invoke
651 ExecutionContext &InvokingSF = ECStack.back ();
652 InvokingSF.Caller = CallSite ();
654 // Go to exceptional destination BB of invoke instruction
655 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
658 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
659 std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
663 void Interpreter::visitBranchInst(BranchInst &I) {
664 ExecutionContext &SF = ECStack.back();
667 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
668 if (!I.isUnconditional()) {
669 Value *Cond = I.getCondition();
670 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
671 Dest = I.getSuccessor(1);
673 SwitchToNewBasicBlock(Dest, SF);
676 void Interpreter::visitSwitchInst(SwitchInst &I) {
677 ExecutionContext &SF = ECStack.back();
678 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
679 const Type *ElTy = I.getOperand(0)->getType();
681 // Check to see if any of the cases match...
682 BasicBlock *Dest = 0;
683 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
684 if (executeSetEQInst(CondVal,
685 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
686 Dest = cast<BasicBlock>(I.getOperand(i+1));
690 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
691 SwitchToNewBasicBlock(Dest, SF);
694 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
695 // This function handles the actual updating of block and instruction iterators
696 // as well as execution of all of the PHI nodes in the destination block.
698 // This method does this because all of the PHI nodes must be executed
699 // atomically, reading their inputs before any of the results are updated. Not
700 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
701 // their inputs. If the input PHI node is updated before it is read, incorrect
702 // results can happen. Thus we use a two phase approach.
704 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
705 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
706 SF.CurBB = Dest; // Update CurBB to branch destination
707 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
709 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
711 // Loop over all of the PHI nodes in the current block, reading their inputs.
712 std::vector<GenericValue> ResultValues;
714 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
715 // Search for the value corresponding to this previous bb...
716 int i = PN->getBasicBlockIndex(PrevBB);
717 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
718 Value *IncomingValue = PN->getIncomingValue(i);
720 // Save the incoming value for this PHI node...
721 ResultValues.push_back(getOperandValue(IncomingValue, SF));
724 // Now loop over all of the PHI nodes setting their values...
725 SF.CurInst = SF.CurBB->begin();
726 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
727 PHINode *PN = cast<PHINode>(SF.CurInst);
728 SetValue(PN, ResultValues[i], SF);
732 //===----------------------------------------------------------------------===//
733 // Memory Instruction Implementations
734 //===----------------------------------------------------------------------===//
736 void Interpreter::visitAllocationInst(AllocationInst &I) {
737 ExecutionContext &SF = ECStack.back();
739 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
741 // Get the number of elements being allocated by the array...
742 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
744 // Allocate enough memory to hold the type...
745 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
747 GenericValue Result = PTOGV(Memory);
748 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
749 SetValue(&I, Result, SF);
751 if (I.getOpcode() == Instruction::Alloca)
752 ECStack.back().Allocas.add(Memory);
755 void Interpreter::visitFreeInst(FreeInst &I) {
756 ExecutionContext &SF = ECStack.back();
757 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
758 GenericValue Value = getOperandValue(I.getOperand(0), SF);
759 // TODO: Check to make sure memory is allocated
760 free(GVTOP(Value)); // Free memory
763 // getElementOffset - The workhorse for getelementptr.
765 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
767 ExecutionContext &SF) {
768 assert(isa<PointerType>(Ptr->getType()) &&
769 "Cannot getElementOffset of a nonpointer type!");
773 for (; I != E; ++I) {
774 if (const StructType *STy = dyn_cast<StructType>(*I)) {
775 const StructLayout *SLO = TD.getStructLayout(STy);
777 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
778 unsigned Index = unsigned(CPU->getZExtValue());
780 Total += (PointerTy)SLO->MemberOffsets[Index];
782 const SequentialType *ST = cast<SequentialType>(*I);
783 // Get the index number for the array... which must be long type...
784 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
787 switch (I.getOperand()->getType()->getTypeID()) {
788 default: assert(0 && "Illegal getelementptr index for sequential type!");
789 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
790 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
791 case Type::IntTyID: Idx = IdxGV.IntVal; break;
792 case Type::LongTyID: Idx = IdxGV.LongVal; break;
793 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
794 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
795 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
796 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
798 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
803 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
807 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
808 ExecutionContext &SF = ECStack.back();
809 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
810 gep_type_begin(I), gep_type_end(I), SF), SF);
813 void Interpreter::visitLoadInst(LoadInst &I) {
814 ExecutionContext &SF = ECStack.back();
815 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
816 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
817 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
818 SetValue(&I, Result, SF);
821 void Interpreter::visitStoreInst(StoreInst &I) {
822 ExecutionContext &SF = ECStack.back();
823 GenericValue Val = getOperandValue(I.getOperand(0), SF);
824 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
825 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
826 I.getOperand(0)->getType());
829 //===----------------------------------------------------------------------===//
830 // Miscellaneous Instruction Implementations
831 //===----------------------------------------------------------------------===//
833 void Interpreter::visitCallSite(CallSite CS) {
834 ExecutionContext &SF = ECStack.back();
836 // Check to see if this is an intrinsic function call...
837 if (Function *F = CS.getCalledFunction())
838 if (F->isExternal ())
839 switch (F->getIntrinsicID()) {
840 case Intrinsic::not_intrinsic:
842 case Intrinsic::vastart: { // va_start
843 GenericValue ArgIndex;
844 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
845 ArgIndex.UIntPairVal.second = 0;
846 SetValue(CS.getInstruction(), ArgIndex, SF);
849 case Intrinsic::vaend: // va_end is a noop for the interpreter
851 case Intrinsic::vacopy: // va_copy: dest = src
852 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
855 // If it is an unknown intrinsic function, use the intrinsic lowering
856 // class to transform it into hopefully tasty LLVM code.
858 Instruction *Prev = CS.getInstruction()->getPrev();
859 BasicBlock *Parent = CS.getInstruction()->getParent();
860 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
862 // Restore the CurInst pointer to the first instruction newly inserted, if
865 SF.CurInst = Parent->begin();
874 std::vector<GenericValue> ArgVals;
875 const unsigned NumArgs = SF.Caller.arg_size();
876 ArgVals.reserve(NumArgs);
877 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
878 e = SF.Caller.arg_end(); i != e; ++i) {
880 ArgVals.push_back(getOperandValue(V, SF));
881 // Promote all integral types whose size is < sizeof(int) into ints. We do
882 // this by zero or sign extending the value as appropriate according to the
884 const Type *Ty = V->getType();
885 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
886 if (Ty == Type::ShortTy)
887 ArgVals.back().IntVal = ArgVals.back().ShortVal;
888 else if (Ty == Type::UShortTy)
889 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
890 else if (Ty == Type::SByteTy)
891 ArgVals.back().IntVal = ArgVals.back().SByteVal;
892 else if (Ty == Type::UByteTy)
893 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
894 else if (Ty == Type::BoolTy)
895 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
897 assert(0 && "Unknown type!");
901 // To handle indirect calls, we must get the pointer value from the argument
902 // and treat it as a function pointer.
903 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
904 callFunction((Function*)GVTOP(SRC), ArgVals);
907 #define IMPLEMENT_SHIFT(OP, TY) \
908 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
910 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
913 switch (Ty->getTypeID()) {
914 IMPLEMENT_SHIFT(<<, UByte);
915 IMPLEMENT_SHIFT(<<, SByte);
916 IMPLEMENT_SHIFT(<<, UShort);
917 IMPLEMENT_SHIFT(<<, Short);
918 IMPLEMENT_SHIFT(<<, UInt);
919 IMPLEMENT_SHIFT(<<, Int);
920 IMPLEMENT_SHIFT(<<, ULong);
921 IMPLEMENT_SHIFT(<<, Long);
923 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
928 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
931 switch (Ty->getTypeID()) {
932 IMPLEMENT_SHIFT(>>, UByte);
933 IMPLEMENT_SHIFT(>>, SByte);
934 IMPLEMENT_SHIFT(>>, UShort);
935 IMPLEMENT_SHIFT(>>, Short);
936 IMPLEMENT_SHIFT(>>, UInt);
937 IMPLEMENT_SHIFT(>>, Int);
938 IMPLEMENT_SHIFT(>>, ULong);
939 IMPLEMENT_SHIFT(>>, Long);
941 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
947 void Interpreter::visitShl(ShiftInst &I) {
948 ExecutionContext &SF = ECStack.back();
949 const Type *Ty = I.getOperand(0)->getType();
950 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
951 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
953 Dest = executeShlInst (Src1, Src2, Ty);
954 SetValue(&I, Dest, SF);
957 void Interpreter::visitShr(ShiftInst &I) {
958 ExecutionContext &SF = ECStack.back();
959 const Type *Ty = I.getOperand(0)->getType();
960 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
961 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
963 Dest = executeShrInst (Src1, Src2, Ty);
964 SetValue(&I, Dest, SF);
967 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
968 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
970 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
971 case Type::DESTTY##TyID: \
972 switch (SrcTy->getTypeID()) { \
973 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
974 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
975 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
976 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
977 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
978 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
979 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
980 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
981 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
982 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
984 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
985 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
986 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
988 #define IMPLEMENT_CAST_CASE_END() \
989 default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
994 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
995 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
996 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
997 IMPLEMENT_CAST_CASE_END()
999 GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
1000 ExecutionContext &SF) {
1001 const Type *SrcTy = SrcVal->getType();
1002 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1004 switch (Ty->getTypeID()) {
1005 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1006 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1007 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1008 IMPLEMENT_CAST_CASE(Short , ( signed short));
1009 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1010 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1011 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1012 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1013 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1014 IMPLEMENT_CAST_CASE(Float , (float));
1015 IMPLEMENT_CAST_CASE(Double , (double));
1016 IMPLEMENT_CAST_CASE(Bool , (bool));
1018 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
1025 void Interpreter::visitCastInst(CastInst &I) {
1026 ExecutionContext &SF = ECStack.back();
1027 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1030 #define IMPLEMENT_VAARG(TY) \
1031 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1033 void Interpreter::visitVAArgInst(VAArgInst &I) {
1034 ExecutionContext &SF = ECStack.back();
1036 // Get the incoming valist parameter. LLI treats the valist as a
1037 // (ec-stack-depth var-arg-index) pair.
1038 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1040 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1041 .VarArgs[VAList.UIntPairVal.second];
1042 const Type *Ty = I.getType();
1043 switch (Ty->getTypeID()) {
1044 IMPLEMENT_VAARG(UByte);
1045 IMPLEMENT_VAARG(SByte);
1046 IMPLEMENT_VAARG(UShort);
1047 IMPLEMENT_VAARG(Short);
1048 IMPLEMENT_VAARG(UInt);
1049 IMPLEMENT_VAARG(Int);
1050 IMPLEMENT_VAARG(ULong);
1051 IMPLEMENT_VAARG(Long);
1052 IMPLEMENT_VAARG(Pointer);
1053 IMPLEMENT_VAARG(Float);
1054 IMPLEMENT_VAARG(Double);
1055 IMPLEMENT_VAARG(Bool);
1057 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1061 // Set the Value of this Instruction.
1062 SetValue(&I, Dest, SF);
1064 // Move the pointer to the next vararg.
1065 ++VAList.UIntPairVal.second;
1068 //===----------------------------------------------------------------------===//
1069 // Dispatch and Execution Code
1070 //===----------------------------------------------------------------------===//
1072 //===----------------------------------------------------------------------===//
1073 // callFunction - Execute the specified function...
1075 void Interpreter::callFunction(Function *F,
1076 const std::vector<GenericValue> &ArgVals) {
1077 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1078 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1079 "Incorrect number of arguments passed into function call!");
1080 // Make a new stack frame... and fill it in.
1081 ECStack.push_back(ExecutionContext());
1082 ExecutionContext &StackFrame = ECStack.back();
1083 StackFrame.CurFunction = F;
1085 // Special handling for external functions.
1086 if (F->isExternal()) {
1087 GenericValue Result = callExternalFunction (F, ArgVals);
1088 // Simulate a 'ret' instruction of the appropriate type.
1089 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1093 // Get pointers to first LLVM BB & Instruction in function.
1094 StackFrame.CurBB = F->begin();
1095 StackFrame.CurInst = StackFrame.CurBB->begin();
1097 // Run through the function arguments and initialize their values...
1098 assert((ArgVals.size() == F->arg_size() ||
1099 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1100 "Invalid number of values passed to function invocation!");
1102 // Handle non-varargs arguments...
1104 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
1105 SetValue(AI, ArgVals[i], StackFrame);
1107 // Handle varargs arguments...
1108 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1111 void Interpreter::run() {
1112 while (!ECStack.empty()) {
1113 // Interpret a single instruction & increment the "PC".
1114 ExecutionContext &SF = ECStack.back(); // Current stack frame
1115 Instruction &I = *SF.CurInst++; // Increment before execute
1117 // Track the number of dynamic instructions executed.
1120 DEBUG(std::cerr << "About to interpret: " << I);
1121 visit(I); // Dispatch to one of the visit* methods...