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 executeURemInst(GenericValue Src1, GenericValue Src2,
53 static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
55 static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
57 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
59 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
61 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
63 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
65 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
67 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
69 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
71 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
73 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
75 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
77 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
79 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
82 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
83 ExecutionContext &SF) {
84 switch (CE->getOpcode()) {
85 case Instruction::Cast:
86 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
87 case Instruction::GetElementPtr:
88 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
89 gep_type_end(CE), SF);
90 case Instruction::Add:
91 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
92 getOperandValue(CE->getOperand(1), SF),
93 CE->getOperand(0)->getType());
94 case Instruction::Sub:
95 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
96 getOperandValue(CE->getOperand(1), SF),
97 CE->getOperand(0)->getType());
98 case Instruction::Mul:
99 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
100 getOperandValue(CE->getOperand(1), SF),
101 CE->getOperand(0)->getType());
102 case Instruction::SDiv:
103 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
104 getOperandValue(CE->getOperand(1), SF),
105 CE->getOperand(0)->getType());
106 case Instruction::UDiv:
107 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
108 getOperandValue(CE->getOperand(1), SF),
109 CE->getOperand(0)->getType());
110 case Instruction::FDiv:
111 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
112 getOperandValue(CE->getOperand(1), SF),
113 CE->getOperand(0)->getType());
114 case Instruction::URem:
115 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
116 getOperandValue(CE->getOperand(1), SF),
117 CE->getOperand(0)->getType());
118 case Instruction::SRem:
119 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
120 getOperandValue(CE->getOperand(1), SF),
121 CE->getOperand(0)->getType());
122 case Instruction::FRem:
123 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
124 getOperandValue(CE->getOperand(1), SF),
125 CE->getOperand(0)->getType());
126 case Instruction::And:
127 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
128 getOperandValue(CE->getOperand(1), SF),
129 CE->getOperand(0)->getType());
130 case Instruction::Or:
131 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
132 getOperandValue(CE->getOperand(1), SF),
133 CE->getOperand(0)->getType());
134 case Instruction::Xor:
135 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
136 getOperandValue(CE->getOperand(1), SF),
137 CE->getOperand(0)->getType());
138 case Instruction::SetEQ:
139 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
140 getOperandValue(CE->getOperand(1), SF),
141 CE->getOperand(0)->getType());
142 case Instruction::SetNE:
143 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
144 getOperandValue(CE->getOperand(1), SF),
145 CE->getOperand(0)->getType());
146 case Instruction::SetLE:
147 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
148 getOperandValue(CE->getOperand(1), SF),
149 CE->getOperand(0)->getType());
150 case Instruction::SetGE:
151 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
152 getOperandValue(CE->getOperand(1), SF),
153 CE->getOperand(0)->getType());
154 case Instruction::SetLT:
155 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
156 getOperandValue(CE->getOperand(1), SF),
157 CE->getOperand(0)->getType());
158 case Instruction::SetGT:
159 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
160 getOperandValue(CE->getOperand(1), SF),
161 CE->getOperand(0)->getType());
162 case Instruction::Shl:
163 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
164 getOperandValue(CE->getOperand(1), SF),
165 CE->getOperand(0)->getType());
166 case Instruction::Shr:
167 return executeShrInst(getOperandValue(CE->getOperand(0), SF),
168 getOperandValue(CE->getOperand(1), SF),
169 CE->getOperand(0)->getType());
170 case Instruction::Select:
171 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
172 getOperandValue(CE->getOperand(1), SF),
173 getOperandValue(CE->getOperand(2), SF));
175 std::cerr << "Unhandled ConstantExpr: " << *CE << "\n";
177 return GenericValue();
181 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
182 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
183 return getConstantExprValue(CE, SF);
184 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
185 return getConstantValue(CPV);
186 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
187 return PTOGV(getPointerToGlobal(GV));
193 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
197 void Interpreter::initializeExecutionEngine() {
201 //===----------------------------------------------------------------------===//
202 // Binary Instruction Implementations
203 //===----------------------------------------------------------------------===//
205 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
206 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
208 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
211 switch (Ty->getTypeID()) {
212 IMPLEMENT_BINARY_OPERATOR(+, UByte);
213 IMPLEMENT_BINARY_OPERATOR(+, SByte);
214 IMPLEMENT_BINARY_OPERATOR(+, UShort);
215 IMPLEMENT_BINARY_OPERATOR(+, Short);
216 IMPLEMENT_BINARY_OPERATOR(+, UInt);
217 IMPLEMENT_BINARY_OPERATOR(+, Int);
218 IMPLEMENT_BINARY_OPERATOR(+, ULong);
219 IMPLEMENT_BINARY_OPERATOR(+, Long);
220 IMPLEMENT_BINARY_OPERATOR(+, Float);
221 IMPLEMENT_BINARY_OPERATOR(+, Double);
223 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
229 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
232 switch (Ty->getTypeID()) {
233 IMPLEMENT_BINARY_OPERATOR(-, UByte);
234 IMPLEMENT_BINARY_OPERATOR(-, SByte);
235 IMPLEMENT_BINARY_OPERATOR(-, UShort);
236 IMPLEMENT_BINARY_OPERATOR(-, Short);
237 IMPLEMENT_BINARY_OPERATOR(-, UInt);
238 IMPLEMENT_BINARY_OPERATOR(-, Int);
239 IMPLEMENT_BINARY_OPERATOR(-, ULong);
240 IMPLEMENT_BINARY_OPERATOR(-, Long);
241 IMPLEMENT_BINARY_OPERATOR(-, Float);
242 IMPLEMENT_BINARY_OPERATOR(-, Double);
244 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
250 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
253 switch (Ty->getTypeID()) {
254 IMPLEMENT_BINARY_OPERATOR(*, UByte);
255 IMPLEMENT_BINARY_OPERATOR(*, SByte);
256 IMPLEMENT_BINARY_OPERATOR(*, UShort);
257 IMPLEMENT_BINARY_OPERATOR(*, Short);
258 IMPLEMENT_BINARY_OPERATOR(*, UInt);
259 IMPLEMENT_BINARY_OPERATOR(*, Int);
260 IMPLEMENT_BINARY_OPERATOR(*, ULong);
261 IMPLEMENT_BINARY_OPERATOR(*, Long);
262 IMPLEMENT_BINARY_OPERATOR(*, Float);
263 IMPLEMENT_BINARY_OPERATOR(*, Double);
265 std::cout << "Unhandled type for Mul instruction: " << *Ty << "\n";
271 #define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
272 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
274 static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
277 switch (Ty->getTypeID()) {
278 IMPLEMENT_SIGNLESS_BINOP(/, UByte, SByte);
279 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
280 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
281 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
283 std::cout << "Unhandled type for UDiv instruction: " << *Ty << "\n";
289 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
292 switch (Ty->getTypeID()) {
293 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
294 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
295 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
296 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
298 std::cout << "Unhandled type for SDiv instruction: " << *Ty << "\n";
304 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
307 switch (Ty->getTypeID()) {
308 IMPLEMENT_BINARY_OPERATOR(/, Float);
309 IMPLEMENT_BINARY_OPERATOR(/, Double);
311 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
317 static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
320 switch (Ty->getTypeID()) {
321 IMPLEMENT_SIGNLESS_BINOP(%, UByte, SByte);
322 IMPLEMENT_SIGNLESS_BINOP(%, UShort, Short);
323 IMPLEMENT_SIGNLESS_BINOP(%, UInt, Int);
324 IMPLEMENT_SIGNLESS_BINOP(%, ULong, Long);
326 std::cout << "Unhandled type for URem instruction: " << *Ty << "\n";
332 static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
335 switch (Ty->getTypeID()) {
336 IMPLEMENT_SIGNLESS_BINOP(%, SByte, UByte);
337 IMPLEMENT_SIGNLESS_BINOP(%, Short, UShort);
338 IMPLEMENT_SIGNLESS_BINOP(%, Int, UInt);
339 IMPLEMENT_SIGNLESS_BINOP(%, Long, ULong);
341 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
347 static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
350 switch (Ty->getTypeID()) {
351 case Type::FloatTyID:
352 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
354 case Type::DoubleTyID:
355 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
358 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
364 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
367 switch (Ty->getTypeID()) {
368 IMPLEMENT_BINARY_OPERATOR(&, Bool);
369 IMPLEMENT_BINARY_OPERATOR(&, UByte);
370 IMPLEMENT_BINARY_OPERATOR(&, SByte);
371 IMPLEMENT_BINARY_OPERATOR(&, UShort);
372 IMPLEMENT_BINARY_OPERATOR(&, Short);
373 IMPLEMENT_BINARY_OPERATOR(&, UInt);
374 IMPLEMENT_BINARY_OPERATOR(&, Int);
375 IMPLEMENT_BINARY_OPERATOR(&, ULong);
376 IMPLEMENT_BINARY_OPERATOR(&, Long);
378 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
384 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
387 switch (Ty->getTypeID()) {
388 IMPLEMENT_BINARY_OPERATOR(|, Bool);
389 IMPLEMENT_BINARY_OPERATOR(|, UByte);
390 IMPLEMENT_BINARY_OPERATOR(|, SByte);
391 IMPLEMENT_BINARY_OPERATOR(|, UShort);
392 IMPLEMENT_BINARY_OPERATOR(|, Short);
393 IMPLEMENT_BINARY_OPERATOR(|, UInt);
394 IMPLEMENT_BINARY_OPERATOR(|, Int);
395 IMPLEMENT_BINARY_OPERATOR(|, ULong);
396 IMPLEMENT_BINARY_OPERATOR(|, Long);
398 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
404 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
407 switch (Ty->getTypeID()) {
408 IMPLEMENT_BINARY_OPERATOR(^, Bool);
409 IMPLEMENT_BINARY_OPERATOR(^, UByte);
410 IMPLEMENT_BINARY_OPERATOR(^, SByte);
411 IMPLEMENT_BINARY_OPERATOR(^, UShort);
412 IMPLEMENT_BINARY_OPERATOR(^, Short);
413 IMPLEMENT_BINARY_OPERATOR(^, UInt);
414 IMPLEMENT_BINARY_OPERATOR(^, Int);
415 IMPLEMENT_BINARY_OPERATOR(^, ULong);
416 IMPLEMENT_BINARY_OPERATOR(^, Long);
418 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
424 #define IMPLEMENT_SETCC(OP, TY) \
425 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
427 // Handle pointers specially because they must be compared with only as much
428 // width as the host has. We _do not_ want to be comparing 64 bit values when
429 // running on a 32-bit target, otherwise the upper 32 bits might mess up
430 // comparisons if they contain garbage.
431 #define IMPLEMENT_POINTERSETCC(OP) \
432 case Type::PointerTyID: \
433 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
434 (void*)(intptr_t)Src2.PointerVal; break
436 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
439 switch (Ty->getTypeID()) {
440 IMPLEMENT_SETCC(==, UByte);
441 IMPLEMENT_SETCC(==, SByte);
442 IMPLEMENT_SETCC(==, UShort);
443 IMPLEMENT_SETCC(==, Short);
444 IMPLEMENT_SETCC(==, UInt);
445 IMPLEMENT_SETCC(==, Int);
446 IMPLEMENT_SETCC(==, ULong);
447 IMPLEMENT_SETCC(==, Long);
448 IMPLEMENT_SETCC(==, Float);
449 IMPLEMENT_SETCC(==, Double);
450 IMPLEMENT_POINTERSETCC(==);
452 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
458 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
461 switch (Ty->getTypeID()) {
462 IMPLEMENT_SETCC(!=, UByte);
463 IMPLEMENT_SETCC(!=, SByte);
464 IMPLEMENT_SETCC(!=, UShort);
465 IMPLEMENT_SETCC(!=, Short);
466 IMPLEMENT_SETCC(!=, UInt);
467 IMPLEMENT_SETCC(!=, Int);
468 IMPLEMENT_SETCC(!=, ULong);
469 IMPLEMENT_SETCC(!=, Long);
470 IMPLEMENT_SETCC(!=, Float);
471 IMPLEMENT_SETCC(!=, Double);
472 IMPLEMENT_POINTERSETCC(!=);
475 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
481 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
484 switch (Ty->getTypeID()) {
485 IMPLEMENT_SETCC(<=, UByte);
486 IMPLEMENT_SETCC(<=, SByte);
487 IMPLEMENT_SETCC(<=, UShort);
488 IMPLEMENT_SETCC(<=, Short);
489 IMPLEMENT_SETCC(<=, UInt);
490 IMPLEMENT_SETCC(<=, Int);
491 IMPLEMENT_SETCC(<=, ULong);
492 IMPLEMENT_SETCC(<=, Long);
493 IMPLEMENT_SETCC(<=, Float);
494 IMPLEMENT_SETCC(<=, Double);
495 IMPLEMENT_POINTERSETCC(<=);
497 std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
503 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
506 switch (Ty->getTypeID()) {
507 IMPLEMENT_SETCC(>=, UByte);
508 IMPLEMENT_SETCC(>=, SByte);
509 IMPLEMENT_SETCC(>=, UShort);
510 IMPLEMENT_SETCC(>=, Short);
511 IMPLEMENT_SETCC(>=, UInt);
512 IMPLEMENT_SETCC(>=, Int);
513 IMPLEMENT_SETCC(>=, ULong);
514 IMPLEMENT_SETCC(>=, Long);
515 IMPLEMENT_SETCC(>=, Float);
516 IMPLEMENT_SETCC(>=, Double);
517 IMPLEMENT_POINTERSETCC(>=);
519 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
525 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
528 switch (Ty->getTypeID()) {
529 IMPLEMENT_SETCC(<, UByte);
530 IMPLEMENT_SETCC(<, SByte);
531 IMPLEMENT_SETCC(<, UShort);
532 IMPLEMENT_SETCC(<, Short);
533 IMPLEMENT_SETCC(<, UInt);
534 IMPLEMENT_SETCC(<, Int);
535 IMPLEMENT_SETCC(<, ULong);
536 IMPLEMENT_SETCC(<, Long);
537 IMPLEMENT_SETCC(<, Float);
538 IMPLEMENT_SETCC(<, Double);
539 IMPLEMENT_POINTERSETCC(<);
541 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
547 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
550 switch (Ty->getTypeID()) {
551 IMPLEMENT_SETCC(>, UByte);
552 IMPLEMENT_SETCC(>, SByte);
553 IMPLEMENT_SETCC(>, UShort);
554 IMPLEMENT_SETCC(>, Short);
555 IMPLEMENT_SETCC(>, UInt);
556 IMPLEMENT_SETCC(>, Int);
557 IMPLEMENT_SETCC(>, ULong);
558 IMPLEMENT_SETCC(>, Long);
559 IMPLEMENT_SETCC(>, Float);
560 IMPLEMENT_SETCC(>, Double);
561 IMPLEMENT_POINTERSETCC(>);
563 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
569 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
570 ExecutionContext &SF = ECStack.back();
571 const Type *Ty = I.getOperand(0)->getType();
572 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
573 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
574 GenericValue R; // Result
576 switch (I.getOpcode()) {
577 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
578 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
579 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
580 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
581 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
582 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
583 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
584 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
585 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
586 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
587 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
588 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
589 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
590 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
591 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
592 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
593 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
594 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
596 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
603 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
605 return Src1.BoolVal ? Src2 : Src3;
608 void Interpreter::visitSelectInst(SelectInst &I) {
609 ExecutionContext &SF = ECStack.back();
610 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
611 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
612 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
613 GenericValue R = executeSelectInst(Src1, Src2, Src3);
618 //===----------------------------------------------------------------------===//
619 // Terminator Instruction Implementations
620 //===----------------------------------------------------------------------===//
622 void Interpreter::exitCalled(GenericValue GV) {
623 // runAtExitHandlers() assumes there are no stack frames, but
624 // if exit() was called, then it had a stack frame. Blow away
625 // the stack before interpreting atexit handlers.
627 runAtExitHandlers ();
631 /// Pop the last stack frame off of ECStack and then copy the result
632 /// back into the result variable if we are not returning void. The
633 /// result variable may be the ExitValue, or the Value of the calling
634 /// CallInst if there was a previous stack frame. This method may
635 /// invalidate any ECStack iterators you have. This method also takes
636 /// care of switching to the normal destination BB, if we are returning
639 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
640 GenericValue Result) {
641 // Pop the current stack frame.
644 if (ECStack.empty()) { // Finished main. Put result into exit code...
645 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
646 ExitValue = Result; // Capture the exit value of the program
648 memset(&ExitValue, 0, sizeof(ExitValue));
651 // If we have a previous stack frame, and we have a previous call,
652 // fill in the return value...
653 ExecutionContext &CallingSF = ECStack.back();
654 if (Instruction *I = CallingSF.Caller.getInstruction()) {
655 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
656 SetValue(I, Result, CallingSF);
657 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
658 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
659 CallingSF.Caller = CallSite(); // We returned from the call...
664 void Interpreter::visitReturnInst(ReturnInst &I) {
665 ExecutionContext &SF = ECStack.back();
666 const Type *RetTy = Type::VoidTy;
669 // Save away the return value... (if we are not 'ret void')
670 if (I.getNumOperands()) {
671 RetTy = I.getReturnValue()->getType();
672 Result = getOperandValue(I.getReturnValue(), SF);
675 popStackAndReturnValueToCaller(RetTy, Result);
678 void Interpreter::visitUnwindInst(UnwindInst &I) {
683 if (ECStack.empty ())
685 Inst = ECStack.back ().Caller.getInstruction ();
686 } while (!(Inst && isa<InvokeInst> (Inst)));
688 // Return from invoke
689 ExecutionContext &InvokingSF = ECStack.back ();
690 InvokingSF.Caller = CallSite ();
692 // Go to exceptional destination BB of invoke instruction
693 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
696 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
697 std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
701 void Interpreter::visitBranchInst(BranchInst &I) {
702 ExecutionContext &SF = ECStack.back();
705 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
706 if (!I.isUnconditional()) {
707 Value *Cond = I.getCondition();
708 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
709 Dest = I.getSuccessor(1);
711 SwitchToNewBasicBlock(Dest, SF);
714 void Interpreter::visitSwitchInst(SwitchInst &I) {
715 ExecutionContext &SF = ECStack.back();
716 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
717 const Type *ElTy = I.getOperand(0)->getType();
719 // Check to see if any of the cases match...
720 BasicBlock *Dest = 0;
721 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
722 if (executeSetEQInst(CondVal,
723 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
724 Dest = cast<BasicBlock>(I.getOperand(i+1));
728 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
729 SwitchToNewBasicBlock(Dest, SF);
732 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
733 // This function handles the actual updating of block and instruction iterators
734 // as well as execution of all of the PHI nodes in the destination block.
736 // This method does this because all of the PHI nodes must be executed
737 // atomically, reading their inputs before any of the results are updated. Not
738 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
739 // their inputs. If the input PHI node is updated before it is read, incorrect
740 // results can happen. Thus we use a two phase approach.
742 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
743 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
744 SF.CurBB = Dest; // Update CurBB to branch destination
745 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
747 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
749 // Loop over all of the PHI nodes in the current block, reading their inputs.
750 std::vector<GenericValue> ResultValues;
752 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
753 // Search for the value corresponding to this previous bb...
754 int i = PN->getBasicBlockIndex(PrevBB);
755 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
756 Value *IncomingValue = PN->getIncomingValue(i);
758 // Save the incoming value for this PHI node...
759 ResultValues.push_back(getOperandValue(IncomingValue, SF));
762 // Now loop over all of the PHI nodes setting their values...
763 SF.CurInst = SF.CurBB->begin();
764 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
765 PHINode *PN = cast<PHINode>(SF.CurInst);
766 SetValue(PN, ResultValues[i], SF);
770 //===----------------------------------------------------------------------===//
771 // Memory Instruction Implementations
772 //===----------------------------------------------------------------------===//
774 void Interpreter::visitAllocationInst(AllocationInst &I) {
775 ExecutionContext &SF = ECStack.back();
777 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
779 // Get the number of elements being allocated by the array...
780 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
782 // Allocate enough memory to hold the type...
783 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
785 GenericValue Result = PTOGV(Memory);
786 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
787 SetValue(&I, Result, SF);
789 if (I.getOpcode() == Instruction::Alloca)
790 ECStack.back().Allocas.add(Memory);
793 void Interpreter::visitFreeInst(FreeInst &I) {
794 ExecutionContext &SF = ECStack.back();
795 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
796 GenericValue Value = getOperandValue(I.getOperand(0), SF);
797 // TODO: Check to make sure memory is allocated
798 free(GVTOP(Value)); // Free memory
801 // getElementOffset - The workhorse for getelementptr.
803 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
805 ExecutionContext &SF) {
806 assert(isa<PointerType>(Ptr->getType()) &&
807 "Cannot getElementOffset of a nonpointer type!");
811 for (; I != E; ++I) {
812 if (const StructType *STy = dyn_cast<StructType>(*I)) {
813 const StructLayout *SLO = TD.getStructLayout(STy);
815 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
816 unsigned Index = unsigned(CPU->getZExtValue());
818 Total += (PointerTy)SLO->MemberOffsets[Index];
820 const SequentialType *ST = cast<SequentialType>(*I);
821 // Get the index number for the array... which must be long type...
822 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
825 switch (I.getOperand()->getType()->getTypeID()) {
826 default: assert(0 && "Illegal getelementptr index for sequential type!");
827 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
828 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
829 case Type::IntTyID: Idx = IdxGV.IntVal; break;
830 case Type::LongTyID: Idx = IdxGV.LongVal; break;
831 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
832 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
833 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
834 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
836 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
841 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
845 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
846 ExecutionContext &SF = ECStack.back();
847 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
848 gep_type_begin(I), gep_type_end(I), SF), SF);
851 void Interpreter::visitLoadInst(LoadInst &I) {
852 ExecutionContext &SF = ECStack.back();
853 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
854 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
855 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
856 SetValue(&I, Result, SF);
859 void Interpreter::visitStoreInst(StoreInst &I) {
860 ExecutionContext &SF = ECStack.back();
861 GenericValue Val = getOperandValue(I.getOperand(0), SF);
862 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
863 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
864 I.getOperand(0)->getType());
867 //===----------------------------------------------------------------------===//
868 // Miscellaneous Instruction Implementations
869 //===----------------------------------------------------------------------===//
871 void Interpreter::visitCallSite(CallSite CS) {
872 ExecutionContext &SF = ECStack.back();
874 // Check to see if this is an intrinsic function call...
875 if (Function *F = CS.getCalledFunction())
876 if (F->isExternal ())
877 switch (F->getIntrinsicID()) {
878 case Intrinsic::not_intrinsic:
880 case Intrinsic::vastart: { // va_start
881 GenericValue ArgIndex;
882 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
883 ArgIndex.UIntPairVal.second = 0;
884 SetValue(CS.getInstruction(), ArgIndex, SF);
887 case Intrinsic::vaend: // va_end is a noop for the interpreter
889 case Intrinsic::vacopy: // va_copy: dest = src
890 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
893 // If it is an unknown intrinsic function, use the intrinsic lowering
894 // class to transform it into hopefully tasty LLVM code.
896 Instruction *Prev = CS.getInstruction()->getPrev();
897 BasicBlock *Parent = CS.getInstruction()->getParent();
898 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
900 // Restore the CurInst pointer to the first instruction newly inserted, if
903 SF.CurInst = Parent->begin();
912 std::vector<GenericValue> ArgVals;
913 const unsigned NumArgs = SF.Caller.arg_size();
914 ArgVals.reserve(NumArgs);
915 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
916 e = SF.Caller.arg_end(); i != e; ++i) {
918 ArgVals.push_back(getOperandValue(V, SF));
919 // Promote all integral types whose size is < sizeof(int) into ints. We do
920 // this by zero or sign extending the value as appropriate according to the
922 const Type *Ty = V->getType();
923 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
924 if (Ty == Type::ShortTy)
925 ArgVals.back().IntVal = ArgVals.back().ShortVal;
926 else if (Ty == Type::UShortTy)
927 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
928 else if (Ty == Type::SByteTy)
929 ArgVals.back().IntVal = ArgVals.back().SByteVal;
930 else if (Ty == Type::UByteTy)
931 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
932 else if (Ty == Type::BoolTy)
933 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
935 assert(0 && "Unknown type!");
939 // To handle indirect calls, we must get the pointer value from the argument
940 // and treat it as a function pointer.
941 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
942 callFunction((Function*)GVTOP(SRC), ArgVals);
945 #define IMPLEMENT_SHIFT(OP, TY) \
946 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
948 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
951 switch (Ty->getTypeID()) {
952 IMPLEMENT_SHIFT(<<, UByte);
953 IMPLEMENT_SHIFT(<<, SByte);
954 IMPLEMENT_SHIFT(<<, UShort);
955 IMPLEMENT_SHIFT(<<, Short);
956 IMPLEMENT_SHIFT(<<, UInt);
957 IMPLEMENT_SHIFT(<<, Int);
958 IMPLEMENT_SHIFT(<<, ULong);
959 IMPLEMENT_SHIFT(<<, Long);
961 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
966 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
969 switch (Ty->getTypeID()) {
970 IMPLEMENT_SHIFT(>>, UByte);
971 IMPLEMENT_SHIFT(>>, SByte);
972 IMPLEMENT_SHIFT(>>, UShort);
973 IMPLEMENT_SHIFT(>>, Short);
974 IMPLEMENT_SHIFT(>>, UInt);
975 IMPLEMENT_SHIFT(>>, Int);
976 IMPLEMENT_SHIFT(>>, ULong);
977 IMPLEMENT_SHIFT(>>, Long);
979 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
985 void Interpreter::visitShl(ShiftInst &I) {
986 ExecutionContext &SF = ECStack.back();
987 const Type *Ty = I.getOperand(0)->getType();
988 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
989 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
991 Dest = executeShlInst (Src1, Src2, Ty);
992 SetValue(&I, Dest, SF);
995 void Interpreter::visitShr(ShiftInst &I) {
996 ExecutionContext &SF = ECStack.back();
997 const Type *Ty = I.getOperand(0)->getType();
998 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
999 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1001 Dest = executeShrInst (Src1, Src2, Ty);
1002 SetValue(&I, Dest, SF);
1005 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
1006 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
1008 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
1009 case Type::DESTTY##TyID: \
1010 switch (SrcTy->getTypeID()) { \
1011 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
1012 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
1013 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
1014 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1015 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1016 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
1017 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1018 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
1019 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
1020 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
1022 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
1023 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
1024 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
1026 #define IMPLEMENT_CAST_CASE_END() \
1027 default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
1032 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
1033 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1034 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
1035 IMPLEMENT_CAST_CASE_END()
1037 GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
1038 ExecutionContext &SF) {
1039 const Type *SrcTy = SrcVal->getType();
1040 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1042 switch (Ty->getTypeID()) {
1043 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1044 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1045 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1046 IMPLEMENT_CAST_CASE(Short , ( signed short));
1047 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1048 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1049 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1050 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1051 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1052 IMPLEMENT_CAST_CASE(Float , (float));
1053 IMPLEMENT_CAST_CASE(Double , (double));
1054 IMPLEMENT_CAST_CASE(Bool , (bool));
1056 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
1063 void Interpreter::visitCastInst(CastInst &I) {
1064 ExecutionContext &SF = ECStack.back();
1065 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1068 #define IMPLEMENT_VAARG(TY) \
1069 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1071 void Interpreter::visitVAArgInst(VAArgInst &I) {
1072 ExecutionContext &SF = ECStack.back();
1074 // Get the incoming valist parameter. LLI treats the valist as a
1075 // (ec-stack-depth var-arg-index) pair.
1076 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1078 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1079 .VarArgs[VAList.UIntPairVal.second];
1080 const Type *Ty = I.getType();
1081 switch (Ty->getTypeID()) {
1082 IMPLEMENT_VAARG(UByte);
1083 IMPLEMENT_VAARG(SByte);
1084 IMPLEMENT_VAARG(UShort);
1085 IMPLEMENT_VAARG(Short);
1086 IMPLEMENT_VAARG(UInt);
1087 IMPLEMENT_VAARG(Int);
1088 IMPLEMENT_VAARG(ULong);
1089 IMPLEMENT_VAARG(Long);
1090 IMPLEMENT_VAARG(Pointer);
1091 IMPLEMENT_VAARG(Float);
1092 IMPLEMENT_VAARG(Double);
1093 IMPLEMENT_VAARG(Bool);
1095 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1099 // Set the Value of this Instruction.
1100 SetValue(&I, Dest, SF);
1102 // Move the pointer to the next vararg.
1103 ++VAList.UIntPairVal.second;
1106 //===----------------------------------------------------------------------===//
1107 // Dispatch and Execution Code
1108 //===----------------------------------------------------------------------===//
1110 //===----------------------------------------------------------------------===//
1111 // callFunction - Execute the specified function...
1113 void Interpreter::callFunction(Function *F,
1114 const std::vector<GenericValue> &ArgVals) {
1115 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1116 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1117 "Incorrect number of arguments passed into function call!");
1118 // Make a new stack frame... and fill it in.
1119 ECStack.push_back(ExecutionContext());
1120 ExecutionContext &StackFrame = ECStack.back();
1121 StackFrame.CurFunction = F;
1123 // Special handling for external functions.
1124 if (F->isExternal()) {
1125 GenericValue Result = callExternalFunction (F, ArgVals);
1126 // Simulate a 'ret' instruction of the appropriate type.
1127 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1131 // Get pointers to first LLVM BB & Instruction in function.
1132 StackFrame.CurBB = F->begin();
1133 StackFrame.CurInst = StackFrame.CurBB->begin();
1135 // Run through the function arguments and initialize their values...
1136 assert((ArgVals.size() == F->arg_size() ||
1137 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1138 "Invalid number of values passed to function invocation!");
1140 // Handle non-varargs arguments...
1142 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
1143 SetValue(AI, ArgVals[i], StackFrame);
1145 // Handle varargs arguments...
1146 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1149 void Interpreter::run() {
1150 while (!ECStack.empty()) {
1151 // Interpret a single instruction & increment the "PC".
1152 ExecutionContext &SF = ECStack.back(); // Current stack frame
1153 Instruction &I = *SF.CurInst++; // Increment before execute
1155 // Track the number of dynamic instructions executed.
1158 DEBUG(std::cerr << "About to interpret: " << I);
1159 visit(I); // Dispatch to one of the visit* methods...