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 executeUDivInst(GenericValue Src1, GenericValue Src2,
45 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
47 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
49 static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
51 static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
53 static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
55 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
57 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
59 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
61 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
63 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
65 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
67 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
69 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
71 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
73 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
75 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
77 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
80 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
81 ExecutionContext &SF) {
82 switch (CE->getOpcode()) {
83 case Instruction::Cast:
84 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
85 case Instruction::GetElementPtr:
86 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
87 gep_type_end(CE), SF);
88 case Instruction::Add:
89 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
90 getOperandValue(CE->getOperand(1), SF),
91 CE->getOperand(0)->getType());
92 case Instruction::Sub:
93 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
94 getOperandValue(CE->getOperand(1), SF),
95 CE->getOperand(0)->getType());
96 case Instruction::Mul:
97 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
98 getOperandValue(CE->getOperand(1), SF),
99 CE->getOperand(0)->getType());
100 case Instruction::SDiv:
101 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
102 getOperandValue(CE->getOperand(1), SF),
103 CE->getOperand(0)->getType());
104 case Instruction::UDiv:
105 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
106 getOperandValue(CE->getOperand(1), SF),
107 CE->getOperand(0)->getType());
108 case Instruction::FDiv:
109 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
110 getOperandValue(CE->getOperand(1), SF),
111 CE->getOperand(0)->getType());
112 case Instruction::URem:
113 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
114 getOperandValue(CE->getOperand(1), SF),
115 CE->getOperand(0)->getType());
116 case Instruction::SRem:
117 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
118 getOperandValue(CE->getOperand(1), SF),
119 CE->getOperand(0)->getType());
120 case Instruction::FRem:
121 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
122 getOperandValue(CE->getOperand(1), SF),
123 CE->getOperand(0)->getType());
124 case Instruction::And:
125 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
126 getOperandValue(CE->getOperand(1), SF),
127 CE->getOperand(0)->getType());
128 case Instruction::Or:
129 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
130 getOperandValue(CE->getOperand(1), SF),
131 CE->getOperand(0)->getType());
132 case Instruction::Xor:
133 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
134 getOperandValue(CE->getOperand(1), SF),
135 CE->getOperand(0)->getType());
136 case Instruction::SetEQ:
137 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
138 getOperandValue(CE->getOperand(1), SF),
139 CE->getOperand(0)->getType());
140 case Instruction::SetNE:
141 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
142 getOperandValue(CE->getOperand(1), SF),
143 CE->getOperand(0)->getType());
144 case Instruction::SetLE:
145 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
146 getOperandValue(CE->getOperand(1), SF),
147 CE->getOperand(0)->getType());
148 case Instruction::SetGE:
149 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
150 getOperandValue(CE->getOperand(1), SF),
151 CE->getOperand(0)->getType());
152 case Instruction::SetLT:
153 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
154 getOperandValue(CE->getOperand(1), SF),
155 CE->getOperand(0)->getType());
156 case Instruction::SetGT:
157 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
158 getOperandValue(CE->getOperand(1), SF),
159 CE->getOperand(0)->getType());
160 case Instruction::Shl:
161 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
162 getOperandValue(CE->getOperand(1), SF),
163 CE->getOperand(0)->getType());
164 case Instruction::Shr:
165 return executeShrInst(getOperandValue(CE->getOperand(0), SF),
166 getOperandValue(CE->getOperand(1), SF),
167 CE->getOperand(0)->getType());
168 case Instruction::Select:
169 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
170 getOperandValue(CE->getOperand(1), SF),
171 getOperandValue(CE->getOperand(2), SF));
173 std::cerr << "Unhandled ConstantExpr: " << *CE << "\n";
175 return GenericValue();
179 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
180 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
181 return getConstantExprValue(CE, SF);
182 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
183 return getConstantValue(CPV);
184 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
185 return PTOGV(getPointerToGlobal(GV));
191 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
195 void Interpreter::initializeExecutionEngine() {
199 //===----------------------------------------------------------------------===//
200 // Binary Instruction Implementations
201 //===----------------------------------------------------------------------===//
203 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
204 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
206 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
209 switch (Ty->getTypeID()) {
210 IMPLEMENT_BINARY_OPERATOR(+, UByte);
211 IMPLEMENT_BINARY_OPERATOR(+, SByte);
212 IMPLEMENT_BINARY_OPERATOR(+, UShort);
213 IMPLEMENT_BINARY_OPERATOR(+, Short);
214 IMPLEMENT_BINARY_OPERATOR(+, UInt);
215 IMPLEMENT_BINARY_OPERATOR(+, Int);
216 IMPLEMENT_BINARY_OPERATOR(+, ULong);
217 IMPLEMENT_BINARY_OPERATOR(+, Long);
218 IMPLEMENT_BINARY_OPERATOR(+, Float);
219 IMPLEMENT_BINARY_OPERATOR(+, Double);
221 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
227 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
230 switch (Ty->getTypeID()) {
231 IMPLEMENT_BINARY_OPERATOR(-, UByte);
232 IMPLEMENT_BINARY_OPERATOR(-, SByte);
233 IMPLEMENT_BINARY_OPERATOR(-, UShort);
234 IMPLEMENT_BINARY_OPERATOR(-, Short);
235 IMPLEMENT_BINARY_OPERATOR(-, UInt);
236 IMPLEMENT_BINARY_OPERATOR(-, Int);
237 IMPLEMENT_BINARY_OPERATOR(-, ULong);
238 IMPLEMENT_BINARY_OPERATOR(-, Long);
239 IMPLEMENT_BINARY_OPERATOR(-, Float);
240 IMPLEMENT_BINARY_OPERATOR(-, Double);
242 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
248 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
251 switch (Ty->getTypeID()) {
252 IMPLEMENT_BINARY_OPERATOR(*, UByte);
253 IMPLEMENT_BINARY_OPERATOR(*, SByte);
254 IMPLEMENT_BINARY_OPERATOR(*, UShort);
255 IMPLEMENT_BINARY_OPERATOR(*, Short);
256 IMPLEMENT_BINARY_OPERATOR(*, UInt);
257 IMPLEMENT_BINARY_OPERATOR(*, Int);
258 IMPLEMENT_BINARY_OPERATOR(*, ULong);
259 IMPLEMENT_BINARY_OPERATOR(*, Long);
260 IMPLEMENT_BINARY_OPERATOR(*, Float);
261 IMPLEMENT_BINARY_OPERATOR(*, Double);
263 std::cout << "Unhandled type for Mul instruction: " << *Ty << "\n";
269 #define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
270 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
272 static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
275 switch (Ty->getTypeID()) {
276 IMPLEMENT_SIGNLESS_BINOP(/, UByte, SByte);
277 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
278 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
279 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
281 std::cout << "Unhandled type for UDiv instruction: " << *Ty << "\n";
287 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
290 switch (Ty->getTypeID()) {
291 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
292 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
293 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
294 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
296 std::cout << "Unhandled type for SDiv instruction: " << *Ty << "\n";
302 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
305 switch (Ty->getTypeID()) {
306 IMPLEMENT_BINARY_OPERATOR(/, Float);
307 IMPLEMENT_BINARY_OPERATOR(/, Double);
309 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
315 static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
318 switch (Ty->getTypeID()) {
319 IMPLEMENT_SIGNLESS_BINOP(%, UByte, SByte);
320 IMPLEMENT_SIGNLESS_BINOP(%, UShort, Short);
321 IMPLEMENT_SIGNLESS_BINOP(%, UInt, Int);
322 IMPLEMENT_SIGNLESS_BINOP(%, ULong, Long);
324 std::cout << "Unhandled type for URem instruction: " << *Ty << "\n";
330 static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
333 switch (Ty->getTypeID()) {
334 IMPLEMENT_SIGNLESS_BINOP(%, SByte, UByte);
335 IMPLEMENT_SIGNLESS_BINOP(%, Short, UShort);
336 IMPLEMENT_SIGNLESS_BINOP(%, Int, UInt);
337 IMPLEMENT_SIGNLESS_BINOP(%, Long, ULong);
339 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
345 static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
348 switch (Ty->getTypeID()) {
349 case Type::FloatTyID:
350 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
352 case Type::DoubleTyID:
353 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
356 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
362 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
365 switch (Ty->getTypeID()) {
366 IMPLEMENT_BINARY_OPERATOR(&, Bool);
367 IMPLEMENT_BINARY_OPERATOR(&, UByte);
368 IMPLEMENT_BINARY_OPERATOR(&, SByte);
369 IMPLEMENT_BINARY_OPERATOR(&, UShort);
370 IMPLEMENT_BINARY_OPERATOR(&, Short);
371 IMPLEMENT_BINARY_OPERATOR(&, UInt);
372 IMPLEMENT_BINARY_OPERATOR(&, Int);
373 IMPLEMENT_BINARY_OPERATOR(&, ULong);
374 IMPLEMENT_BINARY_OPERATOR(&, Long);
376 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
382 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
385 switch (Ty->getTypeID()) {
386 IMPLEMENT_BINARY_OPERATOR(|, Bool);
387 IMPLEMENT_BINARY_OPERATOR(|, UByte);
388 IMPLEMENT_BINARY_OPERATOR(|, SByte);
389 IMPLEMENT_BINARY_OPERATOR(|, UShort);
390 IMPLEMENT_BINARY_OPERATOR(|, Short);
391 IMPLEMENT_BINARY_OPERATOR(|, UInt);
392 IMPLEMENT_BINARY_OPERATOR(|, Int);
393 IMPLEMENT_BINARY_OPERATOR(|, ULong);
394 IMPLEMENT_BINARY_OPERATOR(|, Long);
396 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
402 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
405 switch (Ty->getTypeID()) {
406 IMPLEMENT_BINARY_OPERATOR(^, Bool);
407 IMPLEMENT_BINARY_OPERATOR(^, UByte);
408 IMPLEMENT_BINARY_OPERATOR(^, SByte);
409 IMPLEMENT_BINARY_OPERATOR(^, UShort);
410 IMPLEMENT_BINARY_OPERATOR(^, Short);
411 IMPLEMENT_BINARY_OPERATOR(^, UInt);
412 IMPLEMENT_BINARY_OPERATOR(^, Int);
413 IMPLEMENT_BINARY_OPERATOR(^, ULong);
414 IMPLEMENT_BINARY_OPERATOR(^, Long);
416 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
422 #define IMPLEMENT_SETCC(OP, TY) \
423 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
425 // Handle pointers specially because they must be compared with only as much
426 // width as the host has. We _do not_ want to be comparing 64 bit values when
427 // running on a 32-bit target, otherwise the upper 32 bits might mess up
428 // comparisons if they contain garbage.
429 #define IMPLEMENT_POINTERSETCC(OP) \
430 case Type::PointerTyID: \
431 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
432 (void*)(intptr_t)Src2.PointerVal; break
434 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
437 switch (Ty->getTypeID()) {
438 IMPLEMENT_SETCC(==, UByte);
439 IMPLEMENT_SETCC(==, SByte);
440 IMPLEMENT_SETCC(==, UShort);
441 IMPLEMENT_SETCC(==, Short);
442 IMPLEMENT_SETCC(==, UInt);
443 IMPLEMENT_SETCC(==, Int);
444 IMPLEMENT_SETCC(==, ULong);
445 IMPLEMENT_SETCC(==, Long);
446 IMPLEMENT_SETCC(==, Float);
447 IMPLEMENT_SETCC(==, Double);
448 IMPLEMENT_POINTERSETCC(==);
450 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
456 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
459 switch (Ty->getTypeID()) {
460 IMPLEMENT_SETCC(!=, UByte);
461 IMPLEMENT_SETCC(!=, SByte);
462 IMPLEMENT_SETCC(!=, UShort);
463 IMPLEMENT_SETCC(!=, Short);
464 IMPLEMENT_SETCC(!=, UInt);
465 IMPLEMENT_SETCC(!=, Int);
466 IMPLEMENT_SETCC(!=, ULong);
467 IMPLEMENT_SETCC(!=, Long);
468 IMPLEMENT_SETCC(!=, Float);
469 IMPLEMENT_SETCC(!=, Double);
470 IMPLEMENT_POINTERSETCC(!=);
473 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
479 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
482 switch (Ty->getTypeID()) {
483 IMPLEMENT_SETCC(<=, UByte);
484 IMPLEMENT_SETCC(<=, SByte);
485 IMPLEMENT_SETCC(<=, UShort);
486 IMPLEMENT_SETCC(<=, Short);
487 IMPLEMENT_SETCC(<=, UInt);
488 IMPLEMENT_SETCC(<=, Int);
489 IMPLEMENT_SETCC(<=, ULong);
490 IMPLEMENT_SETCC(<=, Long);
491 IMPLEMENT_SETCC(<=, Float);
492 IMPLEMENT_SETCC(<=, Double);
493 IMPLEMENT_POINTERSETCC(<=);
495 std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
501 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
504 switch (Ty->getTypeID()) {
505 IMPLEMENT_SETCC(>=, UByte);
506 IMPLEMENT_SETCC(>=, SByte);
507 IMPLEMENT_SETCC(>=, UShort);
508 IMPLEMENT_SETCC(>=, Short);
509 IMPLEMENT_SETCC(>=, UInt);
510 IMPLEMENT_SETCC(>=, Int);
511 IMPLEMENT_SETCC(>=, ULong);
512 IMPLEMENT_SETCC(>=, Long);
513 IMPLEMENT_SETCC(>=, Float);
514 IMPLEMENT_SETCC(>=, Double);
515 IMPLEMENT_POINTERSETCC(>=);
517 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
523 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
526 switch (Ty->getTypeID()) {
527 IMPLEMENT_SETCC(<, UByte);
528 IMPLEMENT_SETCC(<, SByte);
529 IMPLEMENT_SETCC(<, UShort);
530 IMPLEMENT_SETCC(<, Short);
531 IMPLEMENT_SETCC(<, UInt);
532 IMPLEMENT_SETCC(<, Int);
533 IMPLEMENT_SETCC(<, ULong);
534 IMPLEMENT_SETCC(<, Long);
535 IMPLEMENT_SETCC(<, Float);
536 IMPLEMENT_SETCC(<, Double);
537 IMPLEMENT_POINTERSETCC(<);
539 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
545 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
548 switch (Ty->getTypeID()) {
549 IMPLEMENT_SETCC(>, UByte);
550 IMPLEMENT_SETCC(>, SByte);
551 IMPLEMENT_SETCC(>, UShort);
552 IMPLEMENT_SETCC(>, Short);
553 IMPLEMENT_SETCC(>, UInt);
554 IMPLEMENT_SETCC(>, Int);
555 IMPLEMENT_SETCC(>, ULong);
556 IMPLEMENT_SETCC(>, Long);
557 IMPLEMENT_SETCC(>, Float);
558 IMPLEMENT_SETCC(>, Double);
559 IMPLEMENT_POINTERSETCC(>);
561 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
567 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
568 ExecutionContext &SF = ECStack.back();
569 const Type *Ty = I.getOperand(0)->getType();
570 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
571 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
572 GenericValue R; // Result
574 switch (I.getOpcode()) {
575 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
576 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
577 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
578 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
579 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
580 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
581 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
582 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
583 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
584 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
585 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
586 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
587 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
588 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
589 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
590 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
591 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
592 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
594 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
601 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
603 return Src1.BoolVal ? Src2 : Src3;
606 void Interpreter::visitSelectInst(SelectInst &I) {
607 ExecutionContext &SF = ECStack.back();
608 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
609 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
610 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
611 GenericValue R = executeSelectInst(Src1, Src2, Src3);
616 //===----------------------------------------------------------------------===//
617 // Terminator Instruction Implementations
618 //===----------------------------------------------------------------------===//
620 void Interpreter::exitCalled(GenericValue GV) {
621 // runAtExitHandlers() assumes there are no stack frames, but
622 // if exit() was called, then it had a stack frame. Blow away
623 // the stack before interpreting atexit handlers.
625 runAtExitHandlers ();
629 /// Pop the last stack frame off of ECStack and then copy the result
630 /// back into the result variable if we are not returning void. The
631 /// result variable may be the ExitValue, or the Value of the calling
632 /// CallInst if there was a previous stack frame. This method may
633 /// invalidate any ECStack iterators you have. This method also takes
634 /// care of switching to the normal destination BB, if we are returning
637 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
638 GenericValue Result) {
639 // Pop the current stack frame.
642 if (ECStack.empty()) { // Finished main. Put result into exit code...
643 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
644 ExitValue = Result; // Capture the exit value of the program
646 memset(&ExitValue, 0, sizeof(ExitValue));
649 // If we have a previous stack frame, and we have a previous call,
650 // fill in the return value...
651 ExecutionContext &CallingSF = ECStack.back();
652 if (Instruction *I = CallingSF.Caller.getInstruction()) {
653 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
654 SetValue(I, Result, CallingSF);
655 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
656 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
657 CallingSF.Caller = CallSite(); // We returned from the call...
662 void Interpreter::visitReturnInst(ReturnInst &I) {
663 ExecutionContext &SF = ECStack.back();
664 const Type *RetTy = Type::VoidTy;
667 // Save away the return value... (if we are not 'ret void')
668 if (I.getNumOperands()) {
669 RetTy = I.getReturnValue()->getType();
670 Result = getOperandValue(I.getReturnValue(), SF);
673 popStackAndReturnValueToCaller(RetTy, Result);
676 void Interpreter::visitUnwindInst(UnwindInst &I) {
681 if (ECStack.empty ())
683 Inst = ECStack.back ().Caller.getInstruction ();
684 } while (!(Inst && isa<InvokeInst> (Inst)));
686 // Return from invoke
687 ExecutionContext &InvokingSF = ECStack.back ();
688 InvokingSF.Caller = CallSite ();
690 // Go to exceptional destination BB of invoke instruction
691 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
694 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
695 std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
699 void Interpreter::visitBranchInst(BranchInst &I) {
700 ExecutionContext &SF = ECStack.back();
703 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
704 if (!I.isUnconditional()) {
705 Value *Cond = I.getCondition();
706 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
707 Dest = I.getSuccessor(1);
709 SwitchToNewBasicBlock(Dest, SF);
712 void Interpreter::visitSwitchInst(SwitchInst &I) {
713 ExecutionContext &SF = ECStack.back();
714 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
715 const Type *ElTy = I.getOperand(0)->getType();
717 // Check to see if any of the cases match...
718 BasicBlock *Dest = 0;
719 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
720 if (executeSetEQInst(CondVal,
721 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
722 Dest = cast<BasicBlock>(I.getOperand(i+1));
726 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
727 SwitchToNewBasicBlock(Dest, SF);
730 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
731 // This function handles the actual updating of block and instruction iterators
732 // as well as execution of all of the PHI nodes in the destination block.
734 // This method does this because all of the PHI nodes must be executed
735 // atomically, reading their inputs before any of the results are updated. Not
736 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
737 // their inputs. If the input PHI node is updated before it is read, incorrect
738 // results can happen. Thus we use a two phase approach.
740 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
741 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
742 SF.CurBB = Dest; // Update CurBB to branch destination
743 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
745 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
747 // Loop over all of the PHI nodes in the current block, reading their inputs.
748 std::vector<GenericValue> ResultValues;
750 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
751 // Search for the value corresponding to this previous bb...
752 int i = PN->getBasicBlockIndex(PrevBB);
753 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
754 Value *IncomingValue = PN->getIncomingValue(i);
756 // Save the incoming value for this PHI node...
757 ResultValues.push_back(getOperandValue(IncomingValue, SF));
760 // Now loop over all of the PHI nodes setting their values...
761 SF.CurInst = SF.CurBB->begin();
762 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
763 PHINode *PN = cast<PHINode>(SF.CurInst);
764 SetValue(PN, ResultValues[i], SF);
768 //===----------------------------------------------------------------------===//
769 // Memory Instruction Implementations
770 //===----------------------------------------------------------------------===//
772 void Interpreter::visitAllocationInst(AllocationInst &I) {
773 ExecutionContext &SF = ECStack.back();
775 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
777 // Get the number of elements being allocated by the array...
778 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
780 // Allocate enough memory to hold the type...
781 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
783 GenericValue Result = PTOGV(Memory);
784 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
785 SetValue(&I, Result, SF);
787 if (I.getOpcode() == Instruction::Alloca)
788 ECStack.back().Allocas.add(Memory);
791 void Interpreter::visitFreeInst(FreeInst &I) {
792 ExecutionContext &SF = ECStack.back();
793 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
794 GenericValue Value = getOperandValue(I.getOperand(0), SF);
795 // TODO: Check to make sure memory is allocated
796 free(GVTOP(Value)); // Free memory
799 // getElementOffset - The workhorse for getelementptr.
801 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
803 ExecutionContext &SF) {
804 assert(isa<PointerType>(Ptr->getType()) &&
805 "Cannot getElementOffset of a nonpointer type!");
809 for (; I != E; ++I) {
810 if (const StructType *STy = dyn_cast<StructType>(*I)) {
811 const StructLayout *SLO = TD.getStructLayout(STy);
813 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
814 unsigned Index = unsigned(CPU->getZExtValue());
816 Total += (PointerTy)SLO->MemberOffsets[Index];
818 const SequentialType *ST = cast<SequentialType>(*I);
819 // Get the index number for the array... which must be long type...
820 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
823 switch (I.getOperand()->getType()->getTypeID()) {
824 default: assert(0 && "Illegal getelementptr index for sequential type!");
825 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
826 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
827 case Type::IntTyID: Idx = IdxGV.IntVal; break;
828 case Type::LongTyID: Idx = IdxGV.LongVal; break;
829 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
830 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
831 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
832 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
834 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
839 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
843 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
844 ExecutionContext &SF = ECStack.back();
845 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
846 gep_type_begin(I), gep_type_end(I), SF), SF);
849 void Interpreter::visitLoadInst(LoadInst &I) {
850 ExecutionContext &SF = ECStack.back();
851 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
852 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
853 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
854 SetValue(&I, Result, SF);
857 void Interpreter::visitStoreInst(StoreInst &I) {
858 ExecutionContext &SF = ECStack.back();
859 GenericValue Val = getOperandValue(I.getOperand(0), SF);
860 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
861 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
862 I.getOperand(0)->getType());
865 //===----------------------------------------------------------------------===//
866 // Miscellaneous Instruction Implementations
867 //===----------------------------------------------------------------------===//
869 void Interpreter::visitCallSite(CallSite CS) {
870 ExecutionContext &SF = ECStack.back();
872 // Check to see if this is an intrinsic function call...
873 if (Function *F = CS.getCalledFunction())
874 if (F->isExternal ())
875 switch (F->getIntrinsicID()) {
876 case Intrinsic::not_intrinsic:
878 case Intrinsic::vastart: { // va_start
879 GenericValue ArgIndex;
880 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
881 ArgIndex.UIntPairVal.second = 0;
882 SetValue(CS.getInstruction(), ArgIndex, SF);
885 case Intrinsic::vaend: // va_end is a noop for the interpreter
887 case Intrinsic::vacopy: // va_copy: dest = src
888 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
891 // If it is an unknown intrinsic function, use the intrinsic lowering
892 // class to transform it into hopefully tasty LLVM code.
894 Instruction *Prev = CS.getInstruction()->getPrev();
895 BasicBlock *Parent = CS.getInstruction()->getParent();
896 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
898 // Restore the CurInst pointer to the first instruction newly inserted, if
901 SF.CurInst = Parent->begin();
910 std::vector<GenericValue> ArgVals;
911 const unsigned NumArgs = SF.Caller.arg_size();
912 ArgVals.reserve(NumArgs);
913 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
914 e = SF.Caller.arg_end(); i != e; ++i) {
916 ArgVals.push_back(getOperandValue(V, SF));
917 // Promote all integral types whose size is < sizeof(int) into ints. We do
918 // this by zero or sign extending the value as appropriate according to the
920 const Type *Ty = V->getType();
921 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
922 if (Ty == Type::ShortTy)
923 ArgVals.back().IntVal = ArgVals.back().ShortVal;
924 else if (Ty == Type::UShortTy)
925 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
926 else if (Ty == Type::SByteTy)
927 ArgVals.back().IntVal = ArgVals.back().SByteVal;
928 else if (Ty == Type::UByteTy)
929 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
930 else if (Ty == Type::BoolTy)
931 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
933 assert(0 && "Unknown type!");
937 // To handle indirect calls, we must get the pointer value from the argument
938 // and treat it as a function pointer.
939 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
940 callFunction((Function*)GVTOP(SRC), ArgVals);
943 #define IMPLEMENT_SHIFT(OP, TY) \
944 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
946 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
949 switch (Ty->getTypeID()) {
950 IMPLEMENT_SHIFT(<<, UByte);
951 IMPLEMENT_SHIFT(<<, SByte);
952 IMPLEMENT_SHIFT(<<, UShort);
953 IMPLEMENT_SHIFT(<<, Short);
954 IMPLEMENT_SHIFT(<<, UInt);
955 IMPLEMENT_SHIFT(<<, Int);
956 IMPLEMENT_SHIFT(<<, ULong);
957 IMPLEMENT_SHIFT(<<, Long);
959 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
964 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
967 switch (Ty->getTypeID()) {
968 IMPLEMENT_SHIFT(>>, UByte);
969 IMPLEMENT_SHIFT(>>, SByte);
970 IMPLEMENT_SHIFT(>>, UShort);
971 IMPLEMENT_SHIFT(>>, Short);
972 IMPLEMENT_SHIFT(>>, UInt);
973 IMPLEMENT_SHIFT(>>, Int);
974 IMPLEMENT_SHIFT(>>, ULong);
975 IMPLEMENT_SHIFT(>>, Long);
977 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
983 void Interpreter::visitShl(ShiftInst &I) {
984 ExecutionContext &SF = ECStack.back();
985 const Type *Ty = I.getOperand(0)->getType();
986 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
987 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
989 Dest = executeShlInst (Src1, Src2, Ty);
990 SetValue(&I, Dest, SF);
993 void Interpreter::visitShr(ShiftInst &I) {
994 ExecutionContext &SF = ECStack.back();
995 const Type *Ty = I.getOperand(0)->getType();
996 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
997 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
999 Dest = executeShrInst (Src1, Src2, Ty);
1000 SetValue(&I, Dest, SF);
1003 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
1004 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
1006 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
1007 case Type::DESTTY##TyID: \
1008 switch (SrcTy->getTypeID()) { \
1009 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
1010 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
1011 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
1012 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1013 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1014 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
1015 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1016 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
1017 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
1018 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
1020 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
1021 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
1022 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
1024 #define IMPLEMENT_CAST_CASE_END() \
1025 default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
1030 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
1031 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1032 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
1033 IMPLEMENT_CAST_CASE_END()
1035 GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
1036 ExecutionContext &SF) {
1037 const Type *SrcTy = SrcVal->getType();
1038 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1040 switch (Ty->getTypeID()) {
1041 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1042 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1043 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1044 IMPLEMENT_CAST_CASE(Short , ( signed short));
1045 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1046 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1047 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1048 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1049 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1050 IMPLEMENT_CAST_CASE(Float , (float));
1051 IMPLEMENT_CAST_CASE(Double , (double));
1052 IMPLEMENT_CAST_CASE(Bool , (bool));
1054 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
1061 void Interpreter::visitCastInst(CastInst &I) {
1062 ExecutionContext &SF = ECStack.back();
1063 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1066 #define IMPLEMENT_VAARG(TY) \
1067 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1069 void Interpreter::visitVAArgInst(VAArgInst &I) {
1070 ExecutionContext &SF = ECStack.back();
1072 // Get the incoming valist parameter. LLI treats the valist as a
1073 // (ec-stack-depth var-arg-index) pair.
1074 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1076 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1077 .VarArgs[VAList.UIntPairVal.second];
1078 const Type *Ty = I.getType();
1079 switch (Ty->getTypeID()) {
1080 IMPLEMENT_VAARG(UByte);
1081 IMPLEMENT_VAARG(SByte);
1082 IMPLEMENT_VAARG(UShort);
1083 IMPLEMENT_VAARG(Short);
1084 IMPLEMENT_VAARG(UInt);
1085 IMPLEMENT_VAARG(Int);
1086 IMPLEMENT_VAARG(ULong);
1087 IMPLEMENT_VAARG(Long);
1088 IMPLEMENT_VAARG(Pointer);
1089 IMPLEMENT_VAARG(Float);
1090 IMPLEMENT_VAARG(Double);
1091 IMPLEMENT_VAARG(Bool);
1093 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1097 // Set the Value of this Instruction.
1098 SetValue(&I, Dest, SF);
1100 // Move the pointer to the next vararg.
1101 ++VAList.UIntPairVal.second;
1104 //===----------------------------------------------------------------------===//
1105 // Dispatch and Execution Code
1106 //===----------------------------------------------------------------------===//
1108 //===----------------------------------------------------------------------===//
1109 // callFunction - Execute the specified function...
1111 void Interpreter::callFunction(Function *F,
1112 const std::vector<GenericValue> &ArgVals) {
1113 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1114 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1115 "Incorrect number of arguments passed into function call!");
1116 // Make a new stack frame... and fill it in.
1117 ECStack.push_back(ExecutionContext());
1118 ExecutionContext &StackFrame = ECStack.back();
1119 StackFrame.CurFunction = F;
1121 // Special handling for external functions.
1122 if (F->isExternal()) {
1123 GenericValue Result = callExternalFunction (F, ArgVals);
1124 // Simulate a 'ret' instruction of the appropriate type.
1125 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1129 // Get pointers to first LLVM BB & Instruction in function.
1130 StackFrame.CurBB = F->begin();
1131 StackFrame.CurInst = StackFrame.CurBB->begin();
1133 // Run through the function arguments and initialize their values...
1134 assert((ArgVals.size() == F->arg_size() ||
1135 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1136 "Invalid number of values passed to function invocation!");
1138 // Handle non-varargs arguments...
1140 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
1141 SetValue(AI, ArgVals[i], StackFrame);
1143 // Handle varargs arguments...
1144 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1147 void Interpreter::run() {
1148 while (!ECStack.empty()) {
1149 // Interpret a single instruction & increment the "PC".
1150 ExecutionContext &SF = ECStack.back(); // Current stack frame
1151 Instruction &I = *SF.CurInst++; // Increment before execute
1153 // Track the number of dynamic instructions executed.
1156 DEBUG(std::cerr << "About to interpret: " << I);
1157 visit(I); // Dispatch to one of the visit* methods...