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 #include "Interpreter.h"
15 #include "llvm/Instructions.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Constants.h"
18 #include "Support/Statistic.h"
19 #include <cmath> // For fmod
24 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
27 Interpreter *TheEE = 0;
29 //===----------------------------------------------------------------------===//
30 // Value Manipulation code
31 //===----------------------------------------------------------------------===//
33 // Operations used by constant expr implementations...
34 static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
35 ExecutionContext &SF);
36 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
39 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
40 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
41 switch (CE->getOpcode()) {
42 case Instruction::Cast:
43 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
44 case Instruction::GetElementPtr:
45 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
47 case Instruction::Add:
48 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
49 getOperandValue(CE->getOperand(1), SF),
52 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
54 return GenericValue();
56 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
57 return TheEE->getConstantValue(CPV);
58 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
59 return PTOGV(TheEE->getPointerToGlobal(GV));
65 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
69 //===----------------------------------------------------------------------===//
70 // Annotation Wrangling code
71 //===----------------------------------------------------------------------===//
73 void Interpreter::initializeExecutionEngine() {
77 //===----------------------------------------------------------------------===//
78 // Binary Instruction Implementations
79 //===----------------------------------------------------------------------===//
81 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
82 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
84 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
87 switch (Ty->getPrimitiveID()) {
88 IMPLEMENT_BINARY_OPERATOR(+, UByte);
89 IMPLEMENT_BINARY_OPERATOR(+, SByte);
90 IMPLEMENT_BINARY_OPERATOR(+, UShort);
91 IMPLEMENT_BINARY_OPERATOR(+, Short);
92 IMPLEMENT_BINARY_OPERATOR(+, UInt);
93 IMPLEMENT_BINARY_OPERATOR(+, Int);
94 IMPLEMENT_BINARY_OPERATOR(+, ULong);
95 IMPLEMENT_BINARY_OPERATOR(+, Long);
96 IMPLEMENT_BINARY_OPERATOR(+, Float);
97 IMPLEMENT_BINARY_OPERATOR(+, Double);
99 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
105 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
108 switch (Ty->getPrimitiveID()) {
109 IMPLEMENT_BINARY_OPERATOR(-, UByte);
110 IMPLEMENT_BINARY_OPERATOR(-, SByte);
111 IMPLEMENT_BINARY_OPERATOR(-, UShort);
112 IMPLEMENT_BINARY_OPERATOR(-, Short);
113 IMPLEMENT_BINARY_OPERATOR(-, UInt);
114 IMPLEMENT_BINARY_OPERATOR(-, Int);
115 IMPLEMENT_BINARY_OPERATOR(-, ULong);
116 IMPLEMENT_BINARY_OPERATOR(-, Long);
117 IMPLEMENT_BINARY_OPERATOR(-, Float);
118 IMPLEMENT_BINARY_OPERATOR(-, Double);
120 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
126 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
129 switch (Ty->getPrimitiveID()) {
130 IMPLEMENT_BINARY_OPERATOR(*, UByte);
131 IMPLEMENT_BINARY_OPERATOR(*, SByte);
132 IMPLEMENT_BINARY_OPERATOR(*, UShort);
133 IMPLEMENT_BINARY_OPERATOR(*, Short);
134 IMPLEMENT_BINARY_OPERATOR(*, UInt);
135 IMPLEMENT_BINARY_OPERATOR(*, Int);
136 IMPLEMENT_BINARY_OPERATOR(*, ULong);
137 IMPLEMENT_BINARY_OPERATOR(*, Long);
138 IMPLEMENT_BINARY_OPERATOR(*, Float);
139 IMPLEMENT_BINARY_OPERATOR(*, Double);
141 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
147 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
150 switch (Ty->getPrimitiveID()) {
151 IMPLEMENT_BINARY_OPERATOR(/, UByte);
152 IMPLEMENT_BINARY_OPERATOR(/, SByte);
153 IMPLEMENT_BINARY_OPERATOR(/, UShort);
154 IMPLEMENT_BINARY_OPERATOR(/, Short);
155 IMPLEMENT_BINARY_OPERATOR(/, UInt);
156 IMPLEMENT_BINARY_OPERATOR(/, Int);
157 IMPLEMENT_BINARY_OPERATOR(/, ULong);
158 IMPLEMENT_BINARY_OPERATOR(/, Long);
159 IMPLEMENT_BINARY_OPERATOR(/, Float);
160 IMPLEMENT_BINARY_OPERATOR(/, Double);
162 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
168 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
171 switch (Ty->getPrimitiveID()) {
172 IMPLEMENT_BINARY_OPERATOR(%, UByte);
173 IMPLEMENT_BINARY_OPERATOR(%, SByte);
174 IMPLEMENT_BINARY_OPERATOR(%, UShort);
175 IMPLEMENT_BINARY_OPERATOR(%, Short);
176 IMPLEMENT_BINARY_OPERATOR(%, UInt);
177 IMPLEMENT_BINARY_OPERATOR(%, Int);
178 IMPLEMENT_BINARY_OPERATOR(%, ULong);
179 IMPLEMENT_BINARY_OPERATOR(%, Long);
180 case Type::FloatTyID:
181 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
183 case Type::DoubleTyID:
184 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
187 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
193 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
196 switch (Ty->getPrimitiveID()) {
197 IMPLEMENT_BINARY_OPERATOR(&, Bool);
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);
207 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
213 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
216 switch (Ty->getPrimitiveID()) {
217 IMPLEMENT_BINARY_OPERATOR(|, Bool);
218 IMPLEMENT_BINARY_OPERATOR(|, UByte);
219 IMPLEMENT_BINARY_OPERATOR(|, SByte);
220 IMPLEMENT_BINARY_OPERATOR(|, UShort);
221 IMPLEMENT_BINARY_OPERATOR(|, Short);
222 IMPLEMENT_BINARY_OPERATOR(|, UInt);
223 IMPLEMENT_BINARY_OPERATOR(|, Int);
224 IMPLEMENT_BINARY_OPERATOR(|, ULong);
225 IMPLEMENT_BINARY_OPERATOR(|, Long);
227 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
233 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
236 switch (Ty->getPrimitiveID()) {
237 IMPLEMENT_BINARY_OPERATOR(^, Bool);
238 IMPLEMENT_BINARY_OPERATOR(^, UByte);
239 IMPLEMENT_BINARY_OPERATOR(^, SByte);
240 IMPLEMENT_BINARY_OPERATOR(^, UShort);
241 IMPLEMENT_BINARY_OPERATOR(^, Short);
242 IMPLEMENT_BINARY_OPERATOR(^, UInt);
243 IMPLEMENT_BINARY_OPERATOR(^, Int);
244 IMPLEMENT_BINARY_OPERATOR(^, ULong);
245 IMPLEMENT_BINARY_OPERATOR(^, Long);
247 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
253 #define IMPLEMENT_SETCC(OP, TY) \
254 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
256 // Handle pointers specially because they must be compared with only as much
257 // width as the host has. We _do not_ want to be comparing 64 bit values when
258 // running on a 32-bit target, otherwise the upper 32 bits might mess up
259 // comparisons if they contain garbage.
260 #define IMPLEMENT_POINTERSETCC(OP) \
261 case Type::PointerTyID: \
262 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
263 (void*)(intptr_t)Src2.PointerVal; break
265 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
268 switch (Ty->getPrimitiveID()) {
269 IMPLEMENT_SETCC(==, UByte);
270 IMPLEMENT_SETCC(==, SByte);
271 IMPLEMENT_SETCC(==, UShort);
272 IMPLEMENT_SETCC(==, Short);
273 IMPLEMENT_SETCC(==, UInt);
274 IMPLEMENT_SETCC(==, Int);
275 IMPLEMENT_SETCC(==, ULong);
276 IMPLEMENT_SETCC(==, Long);
277 IMPLEMENT_SETCC(==, Float);
278 IMPLEMENT_SETCC(==, Double);
279 IMPLEMENT_POINTERSETCC(==);
281 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
287 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
290 switch (Ty->getPrimitiveID()) {
291 IMPLEMENT_SETCC(!=, UByte);
292 IMPLEMENT_SETCC(!=, SByte);
293 IMPLEMENT_SETCC(!=, UShort);
294 IMPLEMENT_SETCC(!=, Short);
295 IMPLEMENT_SETCC(!=, UInt);
296 IMPLEMENT_SETCC(!=, Int);
297 IMPLEMENT_SETCC(!=, ULong);
298 IMPLEMENT_SETCC(!=, Long);
299 IMPLEMENT_SETCC(!=, Float);
300 IMPLEMENT_SETCC(!=, Double);
301 IMPLEMENT_POINTERSETCC(!=);
304 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
310 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
313 switch (Ty->getPrimitiveID()) {
314 IMPLEMENT_SETCC(<=, UByte);
315 IMPLEMENT_SETCC(<=, SByte);
316 IMPLEMENT_SETCC(<=, UShort);
317 IMPLEMENT_SETCC(<=, Short);
318 IMPLEMENT_SETCC(<=, UInt);
319 IMPLEMENT_SETCC(<=, Int);
320 IMPLEMENT_SETCC(<=, ULong);
321 IMPLEMENT_SETCC(<=, Long);
322 IMPLEMENT_SETCC(<=, Float);
323 IMPLEMENT_SETCC(<=, Double);
324 IMPLEMENT_POINTERSETCC(<=);
326 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
332 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
335 switch (Ty->getPrimitiveID()) {
336 IMPLEMENT_SETCC(>=, UByte);
337 IMPLEMENT_SETCC(>=, SByte);
338 IMPLEMENT_SETCC(>=, UShort);
339 IMPLEMENT_SETCC(>=, Short);
340 IMPLEMENT_SETCC(>=, UInt);
341 IMPLEMENT_SETCC(>=, Int);
342 IMPLEMENT_SETCC(>=, ULong);
343 IMPLEMENT_SETCC(>=, Long);
344 IMPLEMENT_SETCC(>=, Float);
345 IMPLEMENT_SETCC(>=, Double);
346 IMPLEMENT_POINTERSETCC(>=);
348 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
354 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
357 switch (Ty->getPrimitiveID()) {
358 IMPLEMENT_SETCC(<, UByte);
359 IMPLEMENT_SETCC(<, SByte);
360 IMPLEMENT_SETCC(<, UShort);
361 IMPLEMENT_SETCC(<, Short);
362 IMPLEMENT_SETCC(<, UInt);
363 IMPLEMENT_SETCC(<, Int);
364 IMPLEMENT_SETCC(<, ULong);
365 IMPLEMENT_SETCC(<, Long);
366 IMPLEMENT_SETCC(<, Float);
367 IMPLEMENT_SETCC(<, Double);
368 IMPLEMENT_POINTERSETCC(<);
370 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
376 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
379 switch (Ty->getPrimitiveID()) {
380 IMPLEMENT_SETCC(>, UByte);
381 IMPLEMENT_SETCC(>, SByte);
382 IMPLEMENT_SETCC(>, UShort);
383 IMPLEMENT_SETCC(>, Short);
384 IMPLEMENT_SETCC(>, UInt);
385 IMPLEMENT_SETCC(>, Int);
386 IMPLEMENT_SETCC(>, ULong);
387 IMPLEMENT_SETCC(>, Long);
388 IMPLEMENT_SETCC(>, Float);
389 IMPLEMENT_SETCC(>, Double);
390 IMPLEMENT_POINTERSETCC(>);
392 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
398 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
399 ExecutionContext &SF = ECStack.back();
400 const Type *Ty = I.getOperand(0)->getType();
401 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
402 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
403 GenericValue R; // Result
405 switch (I.getOpcode()) {
406 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
407 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
408 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
409 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
410 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
411 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
412 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
413 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
414 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
415 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
416 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
417 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
418 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
419 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
421 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
428 //===----------------------------------------------------------------------===//
429 // Terminator Instruction Implementations
430 //===----------------------------------------------------------------------===//
432 void Interpreter::exitCalled(GenericValue GV) {
433 ExitCode = GV.SByteVal;
437 /// Pop the last stack frame off of ECStack and then copy the result
438 /// back into the result variable if we are not returning void. The
439 /// result variable may be the ExitCode, or the Value of the calling
440 /// CallInst if there was a previous stack frame. This method may
441 /// invalidate any ECStack iterators you have. This method also takes
442 /// care of switching to the normal destination BB, if we are returning
445 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
446 GenericValue Result) {
447 // Pop the current stack frame.
450 if (ECStack.empty()) { // Finished main. Put result into exit code...
451 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
452 ExitCode = Result.IntVal; // Capture the exit code of the program
457 // If we have a previous stack frame, and we have a previous call,
458 // fill in the return value...
459 ExecutionContext &CallingSF = ECStack.back();
460 if (Instruction *I = CallingSF.Caller.getInstruction()) {
461 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
462 SetValue(I, Result, CallingSF);
463 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
464 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
465 CallingSF.Caller = CallSite(); // We returned from the call...
470 void Interpreter::visitReturnInst(ReturnInst &I) {
471 ExecutionContext &SF = ECStack.back();
472 const Type *RetTy = Type::VoidTy;
475 // Save away the return value... (if we are not 'ret void')
476 if (I.getNumOperands()) {
477 RetTy = I.getReturnValue()->getType();
478 Result = getOperandValue(I.getReturnValue(), SF);
481 popStackAndReturnValueToCaller(RetTy, Result);
484 void Interpreter::visitUnwindInst(UnwindInst &I) {
489 if (ECStack.empty ())
491 Inst = ECStack.back ().Caller.getInstruction ();
492 } while (!(Inst && isa<InvokeInst> (Inst)));
494 // Return from invoke
495 ExecutionContext &InvokingSF = ECStack.back ();
496 InvokingSF.Caller = CallSite ();
498 // Go to exceptional destination BB of invoke instruction
499 SwitchToNewBasicBlock (cast<InvokeInst> (Inst)->getExceptionalDest (),
503 void Interpreter::visitBranchInst(BranchInst &I) {
504 ExecutionContext &SF = ECStack.back();
507 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
508 if (!I.isUnconditional()) {
509 Value *Cond = I.getCondition();
510 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
511 Dest = I.getSuccessor(1);
513 SwitchToNewBasicBlock(Dest, SF);
516 void Interpreter::visitSwitchInst(SwitchInst &I) {
517 ExecutionContext &SF = ECStack.back();
518 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
519 const Type *ElTy = I.getOperand(0)->getType();
521 // Check to see if any of the cases match...
522 BasicBlock *Dest = 0;
523 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
524 if (executeSetEQInst(CondVal,
525 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
526 Dest = cast<BasicBlock>(I.getOperand(i+1));
530 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
531 SwitchToNewBasicBlock(Dest, SF);
534 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
535 // This function handles the actual updating of block and instruction iterators
536 // as well as execution of all of the PHI nodes in the destination block.
538 // This method does this because all of the PHI nodes must be executed
539 // atomically, reading their inputs before any of the results are updated. Not
540 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
541 // their inputs. If the input PHI node is updated before it is read, incorrect
542 // results can happen. Thus we use a two phase approach.
544 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
545 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
546 SF.CurBB = Dest; // Update CurBB to branch destination
547 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
549 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
551 // Loop over all of the PHI nodes in the current block, reading their inputs.
552 std::vector<GenericValue> ResultValues;
554 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
555 // Search for the value corresponding to this previous bb...
556 int i = PN->getBasicBlockIndex(PrevBB);
557 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
558 Value *IncomingValue = PN->getIncomingValue(i);
560 // Save the incoming value for this PHI node...
561 ResultValues.push_back(getOperandValue(IncomingValue, SF));
564 // Now loop over all of the PHI nodes setting their values...
565 SF.CurInst = SF.CurBB->begin();
566 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
568 SetValue(PN, ResultValues[i], SF);
571 //===----------------------------------------------------------------------===//
572 // Memory Instruction Implementations
573 //===----------------------------------------------------------------------===//
575 void Interpreter::visitAllocationInst(AllocationInst &I) {
576 ExecutionContext &SF = ECStack.back();
578 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
580 // Get the number of elements being allocated by the array...
581 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
583 // Allocate enough memory to hold the type...
584 void *Memory = malloc(NumElements * TD.getTypeSize(Ty));
586 GenericValue Result = PTOGV(Memory);
587 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
588 SetValue(&I, Result, SF);
590 if (I.getOpcode() == Instruction::Alloca)
591 ECStack.back().Allocas.add(Memory);
594 void Interpreter::visitFreeInst(FreeInst &I) {
595 ExecutionContext &SF = ECStack.back();
596 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
597 GenericValue Value = getOperandValue(I.getOperand(0), SF);
598 // TODO: Check to make sure memory is allocated
599 free(GVTOP(Value)); // Free memory
602 // getElementOffset - The workhorse for getelementptr.
604 GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
606 ExecutionContext &SF) {
607 assert(isa<PointerType>(Ptr->getType()) &&
608 "Cannot getElementOffset of a nonpointer type!");
611 const Type *Ty = Ptr->getType();
613 for (; I != E; ++I) {
614 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
615 const StructLayout *SLO = TD.getStructLayout(STy);
617 // Indices must be ubyte constants...
618 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
619 assert(CPU->getType() == Type::UByteTy);
620 unsigned Index = CPU->getValue();
622 Total += SLO->MemberOffsets[Index];
623 Ty = STy->getElementTypes()[Index];
624 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
625 // Get the index number for the array... which must be long type...
626 assert((*I)->getType() == Type::LongTy);
627 unsigned Idx = getOperandValue(*I, SF).LongVal;
628 Ty = ST->getElementType();
629 unsigned Size = TD.getTypeSize(Ty);
635 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
639 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
640 ExecutionContext &SF = ECStack.back();
641 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
642 I.idx_begin(), I.idx_end(), SF), SF);
645 void Interpreter::visitLoadInst(LoadInst &I) {
646 ExecutionContext &SF = ECStack.back();
647 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
648 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
649 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
650 SetValue(&I, Result, SF);
653 void Interpreter::visitStoreInst(StoreInst &I) {
654 ExecutionContext &SF = ECStack.back();
655 GenericValue Val = getOperandValue(I.getOperand(0), SF);
656 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
657 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
658 I.getOperand(0)->getType());
661 //===----------------------------------------------------------------------===//
662 // Miscellaneous Instruction Implementations
663 //===----------------------------------------------------------------------===//
665 void Interpreter::visitCallSite(CallSite CS) {
666 ExecutionContext &SF = ECStack.back();
668 std::vector<GenericValue> ArgVals;
669 const unsigned NumArgs = SF.Caller.arg_size();
670 ArgVals.reserve(NumArgs);
671 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
672 e = SF.Caller.arg_end(); i != e; ++i) {
674 ArgVals.push_back(getOperandValue(V, SF));
675 // Promote all integral types whose size is < sizeof(int) into ints. We do
676 // this by zero or sign extending the value as appropriate according to the
678 const Type *Ty = V->getType();
679 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
680 if (Ty == Type::ShortTy)
681 ArgVals.back().IntVal = ArgVals.back().ShortVal;
682 else if (Ty == Type::UShortTy)
683 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
684 else if (Ty == Type::SByteTy)
685 ArgVals.back().IntVal = ArgVals.back().SByteVal;
686 else if (Ty == Type::UByteTy)
687 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
688 else if (Ty == Type::BoolTy)
689 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
691 assert(0 && "Unknown type!");
695 // To handle indirect calls, we must get the pointer value from the argument
696 // and treat it as a function pointer.
697 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
698 callFunction((Function*)GVTOP(SRC), ArgVals);
701 #define IMPLEMENT_SHIFT(OP, TY) \
702 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
704 void Interpreter::visitShl(ShiftInst &I) {
705 ExecutionContext &SF = ECStack.back();
706 const Type *Ty = I.getOperand(0)->getType();
707 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
708 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
711 switch (Ty->getPrimitiveID()) {
712 IMPLEMENT_SHIFT(<<, UByte);
713 IMPLEMENT_SHIFT(<<, SByte);
714 IMPLEMENT_SHIFT(<<, UShort);
715 IMPLEMENT_SHIFT(<<, Short);
716 IMPLEMENT_SHIFT(<<, UInt);
717 IMPLEMENT_SHIFT(<<, Int);
718 IMPLEMENT_SHIFT(<<, ULong);
719 IMPLEMENT_SHIFT(<<, Long);
721 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
723 SetValue(&I, Dest, SF);
726 void Interpreter::visitShr(ShiftInst &I) {
727 ExecutionContext &SF = ECStack.back();
728 const Type *Ty = I.getOperand(0)->getType();
729 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
730 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
733 switch (Ty->getPrimitiveID()) {
734 IMPLEMENT_SHIFT(>>, UByte);
735 IMPLEMENT_SHIFT(>>, SByte);
736 IMPLEMENT_SHIFT(>>, UShort);
737 IMPLEMENT_SHIFT(>>, Short);
738 IMPLEMENT_SHIFT(>>, UInt);
739 IMPLEMENT_SHIFT(>>, Int);
740 IMPLEMENT_SHIFT(>>, ULong);
741 IMPLEMENT_SHIFT(>>, Long);
743 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
746 SetValue(&I, Dest, SF);
749 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
750 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
752 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
753 case Type::DESTTY##TyID: \
754 switch (SrcTy->getPrimitiveID()) { \
755 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
756 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
757 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
758 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
759 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
760 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
761 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
762 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
763 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
764 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
766 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
767 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
768 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
770 #define IMPLEMENT_CAST_CASE_END() \
771 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
776 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
777 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
778 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
779 IMPLEMENT_CAST_CASE_END()
781 GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
782 ExecutionContext &SF) {
783 const Type *SrcTy = SrcVal->getType();
784 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
786 switch (Ty->getPrimitiveID()) {
787 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
788 IMPLEMENT_CAST_CASE(SByte , ( signed char));
789 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
790 IMPLEMENT_CAST_CASE(Short , ( signed short));
791 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
792 IMPLEMENT_CAST_CASE(Int , ( signed int ));
793 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
794 IMPLEMENT_CAST_CASE(Long , ( int64_t));
795 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
796 IMPLEMENT_CAST_CASE(Float , (float));
797 IMPLEMENT_CAST_CASE(Double , (double));
798 IMPLEMENT_CAST_CASE(Bool , (bool));
800 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
807 void Interpreter::visitCastInst(CastInst &I) {
808 ExecutionContext &SF = ECStack.back();
809 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
812 void Interpreter::visitVANextInst(VANextInst &I) {
813 ExecutionContext &SF = ECStack.back();
815 // Get the incoming valist element. LLI treats the valist as an integer.
816 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
818 // Move to the next operand.
819 unsigned Argument = VAList.IntVal++;
820 assert(Argument < SF.VarArgs.size() &&
821 "Accessing past the last vararg argument!");
822 SetValue(&I, VAList, SF);
825 #define IMPLEMENT_VAARG(TY) \
826 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
828 void Interpreter::visitVAArgInst(VAArgInst &I) {
829 ExecutionContext &SF = ECStack.back();
831 // Get the incoming valist element. LLI treats the valist as an integer.
832 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
833 unsigned Argument = VAList.IntVal;
834 assert(Argument < SF.VarArgs.size() &&
835 "Accessing past the last vararg argument!");
836 GenericValue Dest, Src = SF.VarArgs[Argument];
837 const Type *Ty = I.getType();
838 switch (Ty->getPrimitiveID()) {
839 IMPLEMENT_VAARG(UByte);
840 IMPLEMENT_VAARG(SByte);
841 IMPLEMENT_VAARG(UShort);
842 IMPLEMENT_VAARG(Short);
843 IMPLEMENT_VAARG(UInt);
844 IMPLEMENT_VAARG(Int);
845 IMPLEMENT_VAARG(ULong);
846 IMPLEMENT_VAARG(Long);
847 IMPLEMENT_VAARG(Pointer);
848 IMPLEMENT_VAARG(Float);
849 IMPLEMENT_VAARG(Double);
850 IMPLEMENT_VAARG(Bool);
852 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
856 // Set the Value of this Instruction.
857 SetValue(&I, Dest, SF);
860 //===----------------------------------------------------------------------===//
861 // Dispatch and Execution Code
862 //===----------------------------------------------------------------------===//
864 //===----------------------------------------------------------------------===//
865 // callFunction - Execute the specified function...
867 void Interpreter::callFunction(Function *F,
868 const std::vector<GenericValue> &ArgVals) {
869 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
870 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
871 "Incorrect number of arguments passed into function call!");
872 // Make a new stack frame... and fill it in.
873 ECStack.push_back(ExecutionContext());
874 ExecutionContext &StackFrame = ECStack.back();
875 StackFrame.CurFunction = F;
877 // Special handling for external functions.
878 if (F->isExternal()) {
879 GenericValue Result = callExternalFunction (F, ArgVals);
880 // Simulate a 'ret' instruction of the appropriate type.
881 popStackAndReturnValueToCaller (F->getReturnType (), Result);
885 // Get pointers to first LLVM BB & Instruction in function.
886 StackFrame.CurBB = F->begin();
887 StackFrame.CurInst = StackFrame.CurBB->begin();
889 // Run through the function arguments and initialize their values...
890 assert((ArgVals.size() == F->asize() ||
891 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
892 "Invalid number of values passed to function invocation!");
894 // Handle non-varargs arguments...
896 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
897 SetValue(AI, ArgVals[i], StackFrame);
899 // Handle varargs arguments...
900 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
903 void Interpreter::run() {
904 while (!ECStack.empty()) {
905 // Interpret a single instruction & increment the "PC".
906 ExecutionContext &SF = ECStack.back(); // Current stack frame
907 Instruction &I = *SF.CurInst++; // Increment before execute
909 // Track the number of dynamic instructions executed.
912 visit(I); // Dispatch to one of the visit* methods...
916 } // End llvm namespace