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
21 Interpreter *TheEE = 0;
24 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
27 //===----------------------------------------------------------------------===//
28 // Value Manipulation code
29 //===----------------------------------------------------------------------===//
31 // Operations used by constant expr implementations...
32 static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
33 ExecutionContext &SF);
34 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
37 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
38 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
39 switch (CE->getOpcode()) {
40 case Instruction::Cast:
41 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
42 case Instruction::GetElementPtr:
43 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
45 case Instruction::Add:
46 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
47 getOperandValue(CE->getOperand(1), SF),
50 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
52 return GenericValue();
54 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
55 return TheEE->getConstantValue(CPV);
56 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
57 return PTOGV(TheEE->getPointerToGlobal(GV));
63 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
67 //===----------------------------------------------------------------------===//
68 // Annotation Wrangling code
69 //===----------------------------------------------------------------------===//
71 void Interpreter::initializeExecutionEngine() {
75 //===----------------------------------------------------------------------===//
76 // Binary Instruction Implementations
77 //===----------------------------------------------------------------------===//
79 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
80 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
82 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
85 switch (Ty->getPrimitiveID()) {
86 IMPLEMENT_BINARY_OPERATOR(+, UByte);
87 IMPLEMENT_BINARY_OPERATOR(+, SByte);
88 IMPLEMENT_BINARY_OPERATOR(+, UShort);
89 IMPLEMENT_BINARY_OPERATOR(+, Short);
90 IMPLEMENT_BINARY_OPERATOR(+, UInt);
91 IMPLEMENT_BINARY_OPERATOR(+, Int);
92 IMPLEMENT_BINARY_OPERATOR(+, ULong);
93 IMPLEMENT_BINARY_OPERATOR(+, Long);
94 IMPLEMENT_BINARY_OPERATOR(+, Float);
95 IMPLEMENT_BINARY_OPERATOR(+, Double);
97 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
103 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
106 switch (Ty->getPrimitiveID()) {
107 IMPLEMENT_BINARY_OPERATOR(-, UByte);
108 IMPLEMENT_BINARY_OPERATOR(-, SByte);
109 IMPLEMENT_BINARY_OPERATOR(-, UShort);
110 IMPLEMENT_BINARY_OPERATOR(-, Short);
111 IMPLEMENT_BINARY_OPERATOR(-, UInt);
112 IMPLEMENT_BINARY_OPERATOR(-, Int);
113 IMPLEMENT_BINARY_OPERATOR(-, ULong);
114 IMPLEMENT_BINARY_OPERATOR(-, Long);
115 IMPLEMENT_BINARY_OPERATOR(-, Float);
116 IMPLEMENT_BINARY_OPERATOR(-, Double);
118 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
124 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
127 switch (Ty->getPrimitiveID()) {
128 IMPLEMENT_BINARY_OPERATOR(*, UByte);
129 IMPLEMENT_BINARY_OPERATOR(*, SByte);
130 IMPLEMENT_BINARY_OPERATOR(*, UShort);
131 IMPLEMENT_BINARY_OPERATOR(*, Short);
132 IMPLEMENT_BINARY_OPERATOR(*, UInt);
133 IMPLEMENT_BINARY_OPERATOR(*, Int);
134 IMPLEMENT_BINARY_OPERATOR(*, ULong);
135 IMPLEMENT_BINARY_OPERATOR(*, Long);
136 IMPLEMENT_BINARY_OPERATOR(*, Float);
137 IMPLEMENT_BINARY_OPERATOR(*, Double);
139 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
145 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
148 switch (Ty->getPrimitiveID()) {
149 IMPLEMENT_BINARY_OPERATOR(/, UByte);
150 IMPLEMENT_BINARY_OPERATOR(/, SByte);
151 IMPLEMENT_BINARY_OPERATOR(/, UShort);
152 IMPLEMENT_BINARY_OPERATOR(/, Short);
153 IMPLEMENT_BINARY_OPERATOR(/, UInt);
154 IMPLEMENT_BINARY_OPERATOR(/, Int);
155 IMPLEMENT_BINARY_OPERATOR(/, ULong);
156 IMPLEMENT_BINARY_OPERATOR(/, Long);
157 IMPLEMENT_BINARY_OPERATOR(/, Float);
158 IMPLEMENT_BINARY_OPERATOR(/, Double);
160 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
166 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
169 switch (Ty->getPrimitiveID()) {
170 IMPLEMENT_BINARY_OPERATOR(%, UByte);
171 IMPLEMENT_BINARY_OPERATOR(%, SByte);
172 IMPLEMENT_BINARY_OPERATOR(%, UShort);
173 IMPLEMENT_BINARY_OPERATOR(%, Short);
174 IMPLEMENT_BINARY_OPERATOR(%, UInt);
175 IMPLEMENT_BINARY_OPERATOR(%, Int);
176 IMPLEMENT_BINARY_OPERATOR(%, ULong);
177 IMPLEMENT_BINARY_OPERATOR(%, Long);
178 case Type::FloatTyID:
179 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
181 case Type::DoubleTyID:
182 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
185 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
191 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
194 switch (Ty->getPrimitiveID()) {
195 IMPLEMENT_BINARY_OPERATOR(&, Bool);
196 IMPLEMENT_BINARY_OPERATOR(&, UByte);
197 IMPLEMENT_BINARY_OPERATOR(&, SByte);
198 IMPLEMENT_BINARY_OPERATOR(&, UShort);
199 IMPLEMENT_BINARY_OPERATOR(&, Short);
200 IMPLEMENT_BINARY_OPERATOR(&, UInt);
201 IMPLEMENT_BINARY_OPERATOR(&, Int);
202 IMPLEMENT_BINARY_OPERATOR(&, ULong);
203 IMPLEMENT_BINARY_OPERATOR(&, Long);
205 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
211 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
214 switch (Ty->getPrimitiveID()) {
215 IMPLEMENT_BINARY_OPERATOR(|, Bool);
216 IMPLEMENT_BINARY_OPERATOR(|, UByte);
217 IMPLEMENT_BINARY_OPERATOR(|, SByte);
218 IMPLEMENT_BINARY_OPERATOR(|, UShort);
219 IMPLEMENT_BINARY_OPERATOR(|, Short);
220 IMPLEMENT_BINARY_OPERATOR(|, UInt);
221 IMPLEMENT_BINARY_OPERATOR(|, Int);
222 IMPLEMENT_BINARY_OPERATOR(|, ULong);
223 IMPLEMENT_BINARY_OPERATOR(|, Long);
225 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
231 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
234 switch (Ty->getPrimitiveID()) {
235 IMPLEMENT_BINARY_OPERATOR(^, Bool);
236 IMPLEMENT_BINARY_OPERATOR(^, UByte);
237 IMPLEMENT_BINARY_OPERATOR(^, SByte);
238 IMPLEMENT_BINARY_OPERATOR(^, UShort);
239 IMPLEMENT_BINARY_OPERATOR(^, Short);
240 IMPLEMENT_BINARY_OPERATOR(^, UInt);
241 IMPLEMENT_BINARY_OPERATOR(^, Int);
242 IMPLEMENT_BINARY_OPERATOR(^, ULong);
243 IMPLEMENT_BINARY_OPERATOR(^, Long);
245 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
251 #define IMPLEMENT_SETCC(OP, TY) \
252 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
254 // Handle pointers specially because they must be compared with only as much
255 // width as the host has. We _do not_ want to be comparing 64 bit values when
256 // running on a 32-bit target, otherwise the upper 32 bits might mess up
257 // comparisons if they contain garbage.
258 #define IMPLEMENT_POINTERSETCC(OP) \
259 case Type::PointerTyID: \
260 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
261 (void*)(intptr_t)Src2.PointerVal; break
263 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
266 switch (Ty->getPrimitiveID()) {
267 IMPLEMENT_SETCC(==, UByte);
268 IMPLEMENT_SETCC(==, SByte);
269 IMPLEMENT_SETCC(==, UShort);
270 IMPLEMENT_SETCC(==, Short);
271 IMPLEMENT_SETCC(==, UInt);
272 IMPLEMENT_SETCC(==, Int);
273 IMPLEMENT_SETCC(==, ULong);
274 IMPLEMENT_SETCC(==, Long);
275 IMPLEMENT_SETCC(==, Float);
276 IMPLEMENT_SETCC(==, Double);
277 IMPLEMENT_POINTERSETCC(==);
279 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
285 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
288 switch (Ty->getPrimitiveID()) {
289 IMPLEMENT_SETCC(!=, UByte);
290 IMPLEMENT_SETCC(!=, SByte);
291 IMPLEMENT_SETCC(!=, UShort);
292 IMPLEMENT_SETCC(!=, Short);
293 IMPLEMENT_SETCC(!=, UInt);
294 IMPLEMENT_SETCC(!=, Int);
295 IMPLEMENT_SETCC(!=, ULong);
296 IMPLEMENT_SETCC(!=, Long);
297 IMPLEMENT_SETCC(!=, Float);
298 IMPLEMENT_SETCC(!=, Double);
299 IMPLEMENT_POINTERSETCC(!=);
302 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
308 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
311 switch (Ty->getPrimitiveID()) {
312 IMPLEMENT_SETCC(<=, UByte);
313 IMPLEMENT_SETCC(<=, SByte);
314 IMPLEMENT_SETCC(<=, UShort);
315 IMPLEMENT_SETCC(<=, Short);
316 IMPLEMENT_SETCC(<=, UInt);
317 IMPLEMENT_SETCC(<=, Int);
318 IMPLEMENT_SETCC(<=, ULong);
319 IMPLEMENT_SETCC(<=, Long);
320 IMPLEMENT_SETCC(<=, Float);
321 IMPLEMENT_SETCC(<=, Double);
322 IMPLEMENT_POINTERSETCC(<=);
324 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
330 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
333 switch (Ty->getPrimitiveID()) {
334 IMPLEMENT_SETCC(>=, UByte);
335 IMPLEMENT_SETCC(>=, SByte);
336 IMPLEMENT_SETCC(>=, UShort);
337 IMPLEMENT_SETCC(>=, Short);
338 IMPLEMENT_SETCC(>=, UInt);
339 IMPLEMENT_SETCC(>=, Int);
340 IMPLEMENT_SETCC(>=, ULong);
341 IMPLEMENT_SETCC(>=, Long);
342 IMPLEMENT_SETCC(>=, Float);
343 IMPLEMENT_SETCC(>=, Double);
344 IMPLEMENT_POINTERSETCC(>=);
346 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
352 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
355 switch (Ty->getPrimitiveID()) {
356 IMPLEMENT_SETCC(<, UByte);
357 IMPLEMENT_SETCC(<, SByte);
358 IMPLEMENT_SETCC(<, UShort);
359 IMPLEMENT_SETCC(<, Short);
360 IMPLEMENT_SETCC(<, UInt);
361 IMPLEMENT_SETCC(<, Int);
362 IMPLEMENT_SETCC(<, ULong);
363 IMPLEMENT_SETCC(<, Long);
364 IMPLEMENT_SETCC(<, Float);
365 IMPLEMENT_SETCC(<, Double);
366 IMPLEMENT_POINTERSETCC(<);
368 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
374 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
377 switch (Ty->getPrimitiveID()) {
378 IMPLEMENT_SETCC(>, UByte);
379 IMPLEMENT_SETCC(>, SByte);
380 IMPLEMENT_SETCC(>, UShort);
381 IMPLEMENT_SETCC(>, Short);
382 IMPLEMENT_SETCC(>, UInt);
383 IMPLEMENT_SETCC(>, Int);
384 IMPLEMENT_SETCC(>, ULong);
385 IMPLEMENT_SETCC(>, Long);
386 IMPLEMENT_SETCC(>, Float);
387 IMPLEMENT_SETCC(>, Double);
388 IMPLEMENT_POINTERSETCC(>);
390 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
396 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
397 ExecutionContext &SF = ECStack.back();
398 const Type *Ty = I.getOperand(0)->getType();
399 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
400 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
401 GenericValue R; // Result
403 switch (I.getOpcode()) {
404 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
405 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
406 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
407 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
408 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
409 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
410 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
411 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
412 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
413 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
414 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
415 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
416 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
417 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
419 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
426 //===----------------------------------------------------------------------===//
427 // Terminator Instruction Implementations
428 //===----------------------------------------------------------------------===//
430 void Interpreter::exitCalled(GenericValue GV) {
431 ExitCode = GV.SByteVal;
435 /// Pop the last stack frame off of ECStack and then copy the result
436 /// back into the result variable if we are not returning void. The
437 /// result variable may be the ExitCode, or the Value of the calling
438 /// CallInst if there was a previous stack frame. This method may
439 /// invalidate any ECStack iterators you have. This method also takes
440 /// care of switching to the normal destination BB, if we are returning
443 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
444 GenericValue Result) {
445 // Pop the current stack frame.
448 if (ECStack.empty()) { // Finished main. Put result into exit code...
449 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
450 ExitCode = Result.IntVal; // Capture the exit code of the program
455 // If we have a previous stack frame, and we have a previous call,
456 // fill in the return value...
457 ExecutionContext &CallingSF = ECStack.back();
458 if (Instruction *I = CallingSF.Caller.getInstruction()) {
459 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
460 SetValue(I, Result, CallingSF);
461 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
462 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
463 CallingSF.Caller = CallSite(); // We returned from the call...
468 void Interpreter::visitReturnInst(ReturnInst &I) {
469 ExecutionContext &SF = ECStack.back();
470 const Type *RetTy = Type::VoidTy;
473 // Save away the return value... (if we are not 'ret void')
474 if (I.getNumOperands()) {
475 RetTy = I.getReturnValue()->getType();
476 Result = getOperandValue(I.getReturnValue(), SF);
479 popStackAndReturnValueToCaller(RetTy, Result);
482 void Interpreter::visitUnwindInst(UnwindInst &I) {
487 if (ECStack.empty ())
489 Inst = ECStack.back ().Caller.getInstruction ();
490 } while (!(Inst && isa<InvokeInst> (Inst)));
492 // Return from invoke
493 ExecutionContext &InvokingSF = ECStack.back ();
494 InvokingSF.Caller = CallSite ();
496 // Go to exceptional destination BB of invoke instruction
497 SwitchToNewBasicBlock (cast<InvokeInst> (Inst)->getExceptionalDest (),
501 void Interpreter::visitBranchInst(BranchInst &I) {
502 ExecutionContext &SF = ECStack.back();
505 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
506 if (!I.isUnconditional()) {
507 Value *Cond = I.getCondition();
508 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
509 Dest = I.getSuccessor(1);
511 SwitchToNewBasicBlock(Dest, SF);
514 void Interpreter::visitSwitchInst(SwitchInst &I) {
515 ExecutionContext &SF = ECStack.back();
516 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
517 const Type *ElTy = I.getOperand(0)->getType();
519 // Check to see if any of the cases match...
520 BasicBlock *Dest = 0;
521 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
522 if (executeSetEQInst(CondVal,
523 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
524 Dest = cast<BasicBlock>(I.getOperand(i+1));
528 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
529 SwitchToNewBasicBlock(Dest, SF);
532 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
533 // This function handles the actual updating of block and instruction iterators
534 // as well as execution of all of the PHI nodes in the destination block.
536 // This method does this because all of the PHI nodes must be executed
537 // atomically, reading their inputs before any of the results are updated. Not
538 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
539 // their inputs. If the input PHI node is updated before it is read, incorrect
540 // results can happen. Thus we use a two phase approach.
542 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
543 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
544 SF.CurBB = Dest; // Update CurBB to branch destination
545 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
547 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
549 // Loop over all of the PHI nodes in the current block, reading their inputs.
550 std::vector<GenericValue> ResultValues;
552 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
553 // Search for the value corresponding to this previous bb...
554 int i = PN->getBasicBlockIndex(PrevBB);
555 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
556 Value *IncomingValue = PN->getIncomingValue(i);
558 // Save the incoming value for this PHI node...
559 ResultValues.push_back(getOperandValue(IncomingValue, SF));
562 // Now loop over all of the PHI nodes setting their values...
563 SF.CurInst = SF.CurBB->begin();
564 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
566 SetValue(PN, ResultValues[i], SF);
569 //===----------------------------------------------------------------------===//
570 // Memory Instruction Implementations
571 //===----------------------------------------------------------------------===//
573 void Interpreter::visitAllocationInst(AllocationInst &I) {
574 ExecutionContext &SF = ECStack.back();
576 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
578 // Get the number of elements being allocated by the array...
579 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
581 // Allocate enough memory to hold the type...
582 void *Memory = malloc(NumElements * TD.getTypeSize(Ty));
584 GenericValue Result = PTOGV(Memory);
585 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
586 SetValue(&I, Result, SF);
588 if (I.getOpcode() == Instruction::Alloca)
589 ECStack.back().Allocas.add(Memory);
592 void Interpreter::visitFreeInst(FreeInst &I) {
593 ExecutionContext &SF = ECStack.back();
594 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
595 GenericValue Value = getOperandValue(I.getOperand(0), SF);
596 // TODO: Check to make sure memory is allocated
597 free(GVTOP(Value)); // Free memory
600 // getElementOffset - The workhorse for getelementptr.
602 GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
604 ExecutionContext &SF) {
605 assert(isa<PointerType>(Ptr->getType()) &&
606 "Cannot getElementOffset of a nonpointer type!");
609 const Type *Ty = Ptr->getType();
611 for (; I != E; ++I) {
612 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
613 const StructLayout *SLO = TD.getStructLayout(STy);
615 // Indices must be ubyte constants...
616 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
617 assert(CPU->getType() == Type::UByteTy);
618 unsigned Index = CPU->getValue();
620 Total += SLO->MemberOffsets[Index];
621 Ty = STy->getElementTypes()[Index];
622 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
623 // Get the index number for the array... which must be long type...
624 assert((*I)->getType() == Type::LongTy);
625 unsigned Idx = getOperandValue(*I, SF).LongVal;
626 Ty = ST->getElementType();
627 unsigned Size = TD.getTypeSize(Ty);
633 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
637 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
638 ExecutionContext &SF = ECStack.back();
639 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
640 I.idx_begin(), I.idx_end(), SF), SF);
643 void Interpreter::visitLoadInst(LoadInst &I) {
644 ExecutionContext &SF = ECStack.back();
645 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
646 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
647 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
648 SetValue(&I, Result, SF);
651 void Interpreter::visitStoreInst(StoreInst &I) {
652 ExecutionContext &SF = ECStack.back();
653 GenericValue Val = getOperandValue(I.getOperand(0), SF);
654 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
655 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
656 I.getOperand(0)->getType());
659 //===----------------------------------------------------------------------===//
660 // Miscellaneous Instruction Implementations
661 //===----------------------------------------------------------------------===//
663 void Interpreter::visitCallSite(CallSite CS) {
664 ExecutionContext &SF = ECStack.back();
666 std::vector<GenericValue> ArgVals;
667 const unsigned NumArgs = SF.Caller.arg_size();
668 ArgVals.reserve(NumArgs);
669 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
670 e = SF.Caller.arg_end(); i != e; ++i) {
672 ArgVals.push_back(getOperandValue(V, SF));
673 // Promote all integral types whose size is < sizeof(int) into ints. We do
674 // this by zero or sign extending the value as appropriate according to the
676 const Type *Ty = V->getType();
677 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
678 if (Ty == Type::ShortTy)
679 ArgVals.back().IntVal = ArgVals.back().ShortVal;
680 else if (Ty == Type::UShortTy)
681 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
682 else if (Ty == Type::SByteTy)
683 ArgVals.back().IntVal = ArgVals.back().SByteVal;
684 else if (Ty == Type::UByteTy)
685 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
686 else if (Ty == Type::BoolTy)
687 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
689 assert(0 && "Unknown type!");
693 // To handle indirect calls, we must get the pointer value from the argument
694 // and treat it as a function pointer.
695 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
696 callFunction((Function*)GVTOP(SRC), ArgVals);
699 #define IMPLEMENT_SHIFT(OP, TY) \
700 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
702 void Interpreter::visitShl(ShiftInst &I) {
703 ExecutionContext &SF = ECStack.back();
704 const Type *Ty = I.getOperand(0)->getType();
705 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
706 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
709 switch (Ty->getPrimitiveID()) {
710 IMPLEMENT_SHIFT(<<, UByte);
711 IMPLEMENT_SHIFT(<<, SByte);
712 IMPLEMENT_SHIFT(<<, UShort);
713 IMPLEMENT_SHIFT(<<, Short);
714 IMPLEMENT_SHIFT(<<, UInt);
715 IMPLEMENT_SHIFT(<<, Int);
716 IMPLEMENT_SHIFT(<<, ULong);
717 IMPLEMENT_SHIFT(<<, Long);
719 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
721 SetValue(&I, Dest, SF);
724 void Interpreter::visitShr(ShiftInst &I) {
725 ExecutionContext &SF = ECStack.back();
726 const Type *Ty = I.getOperand(0)->getType();
727 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
728 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
731 switch (Ty->getPrimitiveID()) {
732 IMPLEMENT_SHIFT(>>, UByte);
733 IMPLEMENT_SHIFT(>>, SByte);
734 IMPLEMENT_SHIFT(>>, UShort);
735 IMPLEMENT_SHIFT(>>, Short);
736 IMPLEMENT_SHIFT(>>, UInt);
737 IMPLEMENT_SHIFT(>>, Int);
738 IMPLEMENT_SHIFT(>>, ULong);
739 IMPLEMENT_SHIFT(>>, Long);
741 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
744 SetValue(&I, Dest, SF);
747 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
748 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
750 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
751 case Type::DESTTY##TyID: \
752 switch (SrcTy->getPrimitiveID()) { \
753 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
754 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
755 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
756 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
757 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
758 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
759 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
760 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
761 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
762 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
764 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
765 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
766 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
768 #define IMPLEMENT_CAST_CASE_END() \
769 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
774 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
775 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
776 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
777 IMPLEMENT_CAST_CASE_END()
779 GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
780 ExecutionContext &SF) {
781 const Type *SrcTy = SrcVal->getType();
782 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
784 switch (Ty->getPrimitiveID()) {
785 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
786 IMPLEMENT_CAST_CASE(SByte , ( signed char));
787 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
788 IMPLEMENT_CAST_CASE(Short , ( signed short));
789 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
790 IMPLEMENT_CAST_CASE(Int , ( signed int ));
791 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
792 IMPLEMENT_CAST_CASE(Long , ( int64_t));
793 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
794 IMPLEMENT_CAST_CASE(Float , (float));
795 IMPLEMENT_CAST_CASE(Double , (double));
796 IMPLEMENT_CAST_CASE(Bool , (bool));
798 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
805 void Interpreter::visitCastInst(CastInst &I) {
806 ExecutionContext &SF = ECStack.back();
807 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
810 void Interpreter::visitVANextInst(VANextInst &I) {
811 ExecutionContext &SF = ECStack.back();
813 // Get the incoming valist element. LLI treats the valist as an integer.
814 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
816 // Move to the next operand.
817 unsigned Argument = VAList.IntVal++;
818 assert(Argument < SF.VarArgs.size() &&
819 "Accessing past the last vararg argument!");
820 SetValue(&I, VAList, SF);
823 #define IMPLEMENT_VAARG(TY) \
824 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
826 void Interpreter::visitVAArgInst(VAArgInst &I) {
827 ExecutionContext &SF = ECStack.back();
829 // Get the incoming valist element. LLI treats the valist as an integer.
830 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
831 unsigned Argument = VAList.IntVal;
832 assert(Argument < SF.VarArgs.size() &&
833 "Accessing past the last vararg argument!");
834 GenericValue Dest, Src = SF.VarArgs[Argument];
835 const Type *Ty = I.getType();
836 switch (Ty->getPrimitiveID()) {
837 IMPLEMENT_VAARG(UByte);
838 IMPLEMENT_VAARG(SByte);
839 IMPLEMENT_VAARG(UShort);
840 IMPLEMENT_VAARG(Short);
841 IMPLEMENT_VAARG(UInt);
842 IMPLEMENT_VAARG(Int);
843 IMPLEMENT_VAARG(ULong);
844 IMPLEMENT_VAARG(Long);
845 IMPLEMENT_VAARG(Pointer);
846 IMPLEMENT_VAARG(Float);
847 IMPLEMENT_VAARG(Double);
848 IMPLEMENT_VAARG(Bool);
850 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
854 // Set the Value of this Instruction.
855 SetValue(&I, Dest, SF);
858 //===----------------------------------------------------------------------===//
859 // Dispatch and Execution Code
860 //===----------------------------------------------------------------------===//
862 //===----------------------------------------------------------------------===//
863 // callFunction - Execute the specified function...
865 void Interpreter::callFunction(Function *F,
866 const std::vector<GenericValue> &ArgVals) {
867 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
868 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
869 "Incorrect number of arguments passed into function call!");
870 // Make a new stack frame... and fill it in.
871 ECStack.push_back(ExecutionContext());
872 ExecutionContext &StackFrame = ECStack.back();
873 StackFrame.CurFunction = F;
875 // Special handling for external functions.
876 if (F->isExternal()) {
877 GenericValue Result = callExternalFunction (F, ArgVals);
878 // Simulate a 'ret' instruction of the appropriate type.
879 popStackAndReturnValueToCaller (F->getReturnType (), Result);
883 // Get pointers to first LLVM BB & Instruction in function.
884 StackFrame.CurBB = F->begin();
885 StackFrame.CurInst = StackFrame.CurBB->begin();
887 // Run through the function arguments and initialize their values...
888 assert((ArgVals.size() == F->asize() ||
889 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
890 "Invalid number of values passed to function invocation!");
892 // Handle non-varargs arguments...
894 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
895 SetValue(AI, ArgVals[i], StackFrame);
897 // Handle varargs arguments...
898 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
901 void Interpreter::run() {
902 while (!ECStack.empty()) {
903 // Interpret a single instruction & increment the "PC".
904 ExecutionContext &SF = ECStack.back(); // Current stack frame
905 Instruction &I = *SF.CurInst++; // Increment before execute
907 // Track the number of dynamic instructions executed.
910 visit(I); // Dispatch to one of the visit* methods...