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
executeDivInst(GenericValue Src1
, GenericValue Src2
,
47 static GenericValue
executeAndInst(GenericValue Src1
, GenericValue Src2
,
49 static GenericValue
executeOrInst(GenericValue Src1
, GenericValue Src2
,
51 static GenericValue
executeXorInst(GenericValue Src1
, GenericValue Src2
,
53 static GenericValue
executeSetEQInst(GenericValue Src1
, GenericValue Src2
,
55 static GenericValue
executeSetNEInst(GenericValue Src1
, GenericValue Src2
,
57 static GenericValue
executeSetLTInst(GenericValue Src1
, GenericValue Src2
,
59 static GenericValue
executeSetGTInst(GenericValue Src1
, GenericValue Src2
,
61 static GenericValue
executeSetLEInst(GenericValue Src1
, GenericValue Src2
,
63 static GenericValue
executeSetGEInst(GenericValue Src1
, GenericValue Src2
,
65 static GenericValue
executeShlInst(GenericValue Src1
, GenericValue Src2
,
67 static GenericValue
executeShrInst(GenericValue Src1
, GenericValue Src2
,
69 static GenericValue
executeSelectInst(GenericValue Src1
, GenericValue Src2
,
72 GenericValue
Interpreter::getConstantExprValue (ConstantExpr
*CE
,
73 ExecutionContext
&SF
) {
74 switch (CE
->getOpcode()) {
75 case Instruction::Cast
:
76 return executeCastOperation(CE
->getOperand(0), CE
->getType(), SF
);
77 case Instruction::GetElementPtr
:
78 return executeGEPOperation(CE
->getOperand(0), gep_type_begin(CE
),
79 gep_type_end(CE
), SF
);
80 case Instruction::Add
:
81 return executeAddInst(getOperandValue(CE
->getOperand(0), SF
),
82 getOperandValue(CE
->getOperand(1), SF
),
83 CE
->getOperand(0)->getType());
84 case Instruction::Sub
:
85 return executeSubInst(getOperandValue(CE
->getOperand(0), SF
),
86 getOperandValue(CE
->getOperand(1), SF
),
87 CE
->getOperand(0)->getType());
88 case Instruction::Mul
:
89 return executeMulInst(getOperandValue(CE
->getOperand(0), SF
),
90 getOperandValue(CE
->getOperand(1), SF
),
91 CE
->getOperand(0)->getType());
92 case Instruction::Div
:
93 return executeDivInst(getOperandValue(CE
->getOperand(0), SF
),
94 getOperandValue(CE
->getOperand(1), SF
),
95 CE
->getOperand(0)->getType());
96 case Instruction::Rem
:
97 return executeRemInst(getOperandValue(CE
->getOperand(0), SF
),
98 getOperandValue(CE
->getOperand(1), SF
),
99 CE
->getOperand(0)->getType());
100 case Instruction::And
:
101 return executeAndInst(getOperandValue(CE
->getOperand(0), SF
),
102 getOperandValue(CE
->getOperand(1), SF
),
103 CE
->getOperand(0)->getType());
104 case Instruction::Or
:
105 return executeOrInst(getOperandValue(CE
->getOperand(0), SF
),
106 getOperandValue(CE
->getOperand(1), SF
),
107 CE
->getOperand(0)->getType());
108 case Instruction::Xor
:
109 return executeXorInst(getOperandValue(CE
->getOperand(0), SF
),
110 getOperandValue(CE
->getOperand(1), SF
),
111 CE
->getOperand(0)->getType());
112 case Instruction::SetEQ
:
113 return executeSetEQInst(getOperandValue(CE
->getOperand(0), SF
),
114 getOperandValue(CE
->getOperand(1), SF
),
115 CE
->getOperand(0)->getType());
116 case Instruction::SetNE
:
117 return executeSetNEInst(getOperandValue(CE
->getOperand(0), SF
),
118 getOperandValue(CE
->getOperand(1), SF
),
119 CE
->getOperand(0)->getType());
120 case Instruction::SetLE
:
121 return executeSetLEInst(getOperandValue(CE
->getOperand(0), SF
),
122 getOperandValue(CE
->getOperand(1), SF
),
123 CE
->getOperand(0)->getType());
124 case Instruction::SetGE
:
125 return executeSetGEInst(getOperandValue(CE
->getOperand(0), SF
),
126 getOperandValue(CE
->getOperand(1), SF
),
127 CE
->getOperand(0)->getType());
128 case Instruction::SetLT
:
129 return executeSetLTInst(getOperandValue(CE
->getOperand(0), SF
),
130 getOperandValue(CE
->getOperand(1), SF
),
131 CE
->getOperand(0)->getType());
132 case Instruction::SetGT
:
133 return executeSetGTInst(getOperandValue(CE
->getOperand(0), SF
),
134 getOperandValue(CE
->getOperand(1), SF
),
135 CE
->getOperand(0)->getType());
136 case Instruction::Shl
:
137 return executeShlInst(getOperandValue(CE
->getOperand(0), SF
),
138 getOperandValue(CE
->getOperand(1), SF
),
139 CE
->getOperand(0)->getType());
140 case Instruction::Shr
:
141 return executeShrInst(getOperandValue(CE
->getOperand(0), SF
),
142 getOperandValue(CE
->getOperand(1), SF
),
143 CE
->getOperand(0)->getType());
144 case Instruction::Select
:
145 return executeSelectInst(getOperandValue(CE
->getOperand(0), SF
),
146 getOperandValue(CE
->getOperand(1), SF
),
147 getOperandValue(CE
->getOperand(2), SF
));
149 std::cerr
<< "Unhandled ConstantExpr: " << *CE
<< "\n";
151 return GenericValue();
155 GenericValue
Interpreter::getOperandValue(Value
*V
, ExecutionContext
&SF
) {
156 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(V
)) {
157 return getConstantExprValue(CE
, SF
);
158 } else if (Constant
*CPV
= dyn_cast
<Constant
>(V
)) {
159 return getConstantValue(CPV
);
160 } else if (GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
)) {
161 return PTOGV(getPointerToGlobal(GV
));
167 static void SetValue(Value
*V
, GenericValue Val
, ExecutionContext
&SF
) {
171 void Interpreter::initializeExecutionEngine() {
175 //===----------------------------------------------------------------------===//
176 // Binary Instruction Implementations
177 //===----------------------------------------------------------------------===//
179 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
180 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
182 static GenericValue
executeAddInst(GenericValue Src1
, GenericValue Src2
,
185 switch (Ty
->getTypeID()) {
186 IMPLEMENT_BINARY_OPERATOR(+, UByte
);
187 IMPLEMENT_BINARY_OPERATOR(+, SByte
);
188 IMPLEMENT_BINARY_OPERATOR(+, UShort
);
189 IMPLEMENT_BINARY_OPERATOR(+, Short
);
190 IMPLEMENT_BINARY_OPERATOR(+, UInt
);
191 IMPLEMENT_BINARY_OPERATOR(+, Int
);
192 IMPLEMENT_BINARY_OPERATOR(+, ULong
);
193 IMPLEMENT_BINARY_OPERATOR(+, Long
);
194 IMPLEMENT_BINARY_OPERATOR(+, Float
);
195 IMPLEMENT_BINARY_OPERATOR(+, Double
);
197 std::cout
<< "Unhandled type for Add instruction: " << *Ty
<< "\n";
203 static GenericValue
executeSubInst(GenericValue Src1
, GenericValue Src2
,
206 switch (Ty
->getTypeID()) {
207 IMPLEMENT_BINARY_OPERATOR(-, UByte
);
208 IMPLEMENT_BINARY_OPERATOR(-, SByte
);
209 IMPLEMENT_BINARY_OPERATOR(-, UShort
);
210 IMPLEMENT_BINARY_OPERATOR(-, Short
);
211 IMPLEMENT_BINARY_OPERATOR(-, UInt
);
212 IMPLEMENT_BINARY_OPERATOR(-, Int
);
213 IMPLEMENT_BINARY_OPERATOR(-, ULong
);
214 IMPLEMENT_BINARY_OPERATOR(-, Long
);
215 IMPLEMENT_BINARY_OPERATOR(-, Float
);
216 IMPLEMENT_BINARY_OPERATOR(-, Double
);
218 std::cout
<< "Unhandled type for Sub instruction: " << *Ty
<< "\n";
224 static GenericValue
executeMulInst(GenericValue Src1
, GenericValue Src2
,
227 switch (Ty
->getTypeID()) {
228 IMPLEMENT_BINARY_OPERATOR(*, UByte
);
229 IMPLEMENT_BINARY_OPERATOR(*, SByte
);
230 IMPLEMENT_BINARY_OPERATOR(*, UShort
);
231 IMPLEMENT_BINARY_OPERATOR(*, Short
);
232 IMPLEMENT_BINARY_OPERATOR(*, UInt
);
233 IMPLEMENT_BINARY_OPERATOR(*, Int
);
234 IMPLEMENT_BINARY_OPERATOR(*, ULong
);
235 IMPLEMENT_BINARY_OPERATOR(*, Long
);
236 IMPLEMENT_BINARY_OPERATOR(*, Float
);
237 IMPLEMENT_BINARY_OPERATOR(*, Double
);
239 std::cout
<< "Unhandled type for Mul instruction: " << *Ty
<< "\n";
245 static GenericValue
executeDivInst(GenericValue Src1
, GenericValue Src2
,
248 switch (Ty
->getTypeID()) {
249 IMPLEMENT_BINARY_OPERATOR(/, UByte
);
250 IMPLEMENT_BINARY_OPERATOR(/, SByte
);
251 IMPLEMENT_BINARY_OPERATOR(/, UShort
);
252 IMPLEMENT_BINARY_OPERATOR(/, Short
);
253 IMPLEMENT_BINARY_OPERATOR(/, UInt
);
254 IMPLEMENT_BINARY_OPERATOR(/, Int
);
255 IMPLEMENT_BINARY_OPERATOR(/, ULong
);
256 IMPLEMENT_BINARY_OPERATOR(/, Long
);
257 IMPLEMENT_BINARY_OPERATOR(/, Float
);
258 IMPLEMENT_BINARY_OPERATOR(/, Double
);
260 std::cout
<< "Unhandled type for Div instruction: " << *Ty
<< "\n";
266 static GenericValue
executeRemInst(GenericValue Src1
, GenericValue Src2
,
269 switch (Ty
->getTypeID()) {
270 IMPLEMENT_BINARY_OPERATOR(%, UByte
);
271 IMPLEMENT_BINARY_OPERATOR(%, SByte
);
272 IMPLEMENT_BINARY_OPERATOR(%, UShort
);
273 IMPLEMENT_BINARY_OPERATOR(%, Short
);
274 IMPLEMENT_BINARY_OPERATOR(%, UInt
);
275 IMPLEMENT_BINARY_OPERATOR(%, Int
);
276 IMPLEMENT_BINARY_OPERATOR(%, ULong
);
277 IMPLEMENT_BINARY_OPERATOR(%, Long
);
278 case Type::FloatTyID
:
279 Dest
.FloatVal
= fmod(Src1
.FloatVal
, Src2
.FloatVal
);
281 case Type::DoubleTyID
:
282 Dest
.DoubleVal
= fmod(Src1
.DoubleVal
, Src2
.DoubleVal
);
285 std::cout
<< "Unhandled type for Rem instruction: " << *Ty
<< "\n";
291 static GenericValue
executeAndInst(GenericValue Src1
, GenericValue Src2
,
294 switch (Ty
->getTypeID()) {
295 IMPLEMENT_BINARY_OPERATOR(&, Bool
);
296 IMPLEMENT_BINARY_OPERATOR(&, UByte
);
297 IMPLEMENT_BINARY_OPERATOR(&, SByte
);
298 IMPLEMENT_BINARY_OPERATOR(&, UShort
);
299 IMPLEMENT_BINARY_OPERATOR(&, Short
);
300 IMPLEMENT_BINARY_OPERATOR(&, UInt
);
301 IMPLEMENT_BINARY_OPERATOR(&, Int
);
302 IMPLEMENT_BINARY_OPERATOR(&, ULong
);
303 IMPLEMENT_BINARY_OPERATOR(&, Long
);
305 std::cout
<< "Unhandled type for And instruction: " << *Ty
<< "\n";
311 static GenericValue
executeOrInst(GenericValue Src1
, GenericValue Src2
,
314 switch (Ty
->getTypeID()) {
315 IMPLEMENT_BINARY_OPERATOR(|, Bool
);
316 IMPLEMENT_BINARY_OPERATOR(|, UByte
);
317 IMPLEMENT_BINARY_OPERATOR(|, SByte
);
318 IMPLEMENT_BINARY_OPERATOR(|, UShort
);
319 IMPLEMENT_BINARY_OPERATOR(|, Short
);
320 IMPLEMENT_BINARY_OPERATOR(|, UInt
);
321 IMPLEMENT_BINARY_OPERATOR(|, Int
);
322 IMPLEMENT_BINARY_OPERATOR(|, ULong
);
323 IMPLEMENT_BINARY_OPERATOR(|, Long
);
325 std::cout
<< "Unhandled type for Or instruction: " << *Ty
<< "\n";
331 static GenericValue
executeXorInst(GenericValue Src1
, GenericValue Src2
,
334 switch (Ty
->getTypeID()) {
335 IMPLEMENT_BINARY_OPERATOR(^, Bool
);
336 IMPLEMENT_BINARY_OPERATOR(^, UByte
);
337 IMPLEMENT_BINARY_OPERATOR(^, SByte
);
338 IMPLEMENT_BINARY_OPERATOR(^, UShort
);
339 IMPLEMENT_BINARY_OPERATOR(^, Short
);
340 IMPLEMENT_BINARY_OPERATOR(^, UInt
);
341 IMPLEMENT_BINARY_OPERATOR(^, Int
);
342 IMPLEMENT_BINARY_OPERATOR(^, ULong
);
343 IMPLEMENT_BINARY_OPERATOR(^, Long
);
345 std::cout
<< "Unhandled type for Xor instruction: " << *Ty
<< "\n";
351 #define IMPLEMENT_SETCC(OP, TY) \
352 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
354 // Handle pointers specially because they must be compared with only as much
355 // width as the host has. We _do not_ want to be comparing 64 bit values when
356 // running on a 32-bit target, otherwise the upper 32 bits might mess up
357 // comparisons if they contain garbage.
358 #define IMPLEMENT_POINTERSETCC(OP) \
359 case Type::PointerTyID: \
360 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
361 (void*)(intptr_t)Src2.PointerVal; break
363 static GenericValue
executeSetEQInst(GenericValue Src1
, GenericValue Src2
,
366 switch (Ty
->getTypeID()) {
367 IMPLEMENT_SETCC(==, UByte
);
368 IMPLEMENT_SETCC(==, SByte
);
369 IMPLEMENT_SETCC(==, UShort
);
370 IMPLEMENT_SETCC(==, Short
);
371 IMPLEMENT_SETCC(==, UInt
);
372 IMPLEMENT_SETCC(==, Int
);
373 IMPLEMENT_SETCC(==, ULong
);
374 IMPLEMENT_SETCC(==, Long
);
375 IMPLEMENT_SETCC(==, Float
);
376 IMPLEMENT_SETCC(==, Double
);
377 IMPLEMENT_POINTERSETCC(==);
379 std::cout
<< "Unhandled type for SetEQ instruction: " << *Ty
<< "\n";
385 static GenericValue
executeSetNEInst(GenericValue Src1
, GenericValue Src2
,
388 switch (Ty
->getTypeID()) {
389 IMPLEMENT_SETCC(!=, UByte
);
390 IMPLEMENT_SETCC(!=, SByte
);
391 IMPLEMENT_SETCC(!=, UShort
);
392 IMPLEMENT_SETCC(!=, Short
);
393 IMPLEMENT_SETCC(!=, UInt
);
394 IMPLEMENT_SETCC(!=, Int
);
395 IMPLEMENT_SETCC(!=, ULong
);
396 IMPLEMENT_SETCC(!=, Long
);
397 IMPLEMENT_SETCC(!=, Float
);
398 IMPLEMENT_SETCC(!=, Double
);
399 IMPLEMENT_POINTERSETCC(!=);
402 std::cout
<< "Unhandled type for SetNE instruction: " << *Ty
<< "\n";
408 static GenericValue
executeSetLEInst(GenericValue Src1
, GenericValue Src2
,
411 switch (Ty
->getTypeID()) {
412 IMPLEMENT_SETCC(<=, UByte
);
413 IMPLEMENT_SETCC(<=, SByte
);
414 IMPLEMENT_SETCC(<=, UShort
);
415 IMPLEMENT_SETCC(<=, Short
);
416 IMPLEMENT_SETCC(<=, UInt
);
417 IMPLEMENT_SETCC(<=, Int
);
418 IMPLEMENT_SETCC(<=, ULong
);
419 IMPLEMENT_SETCC(<=, Long
);
420 IMPLEMENT_SETCC(<=, Float
);
421 IMPLEMENT_SETCC(<=, Double
);
422 IMPLEMENT_POINTERSETCC(<=);
424 std::cout
<< "Unhandled type for SetLE instruction: " << *Ty
<< "\n";
430 static GenericValue
executeSetGEInst(GenericValue Src1
, GenericValue Src2
,
433 switch (Ty
->getTypeID()) {
434 IMPLEMENT_SETCC(>=, UByte
);
435 IMPLEMENT_SETCC(>=, SByte
);
436 IMPLEMENT_SETCC(>=, UShort
);
437 IMPLEMENT_SETCC(>=, Short
);
438 IMPLEMENT_SETCC(>=, UInt
);
439 IMPLEMENT_SETCC(>=, Int
);
440 IMPLEMENT_SETCC(>=, ULong
);
441 IMPLEMENT_SETCC(>=, Long
);
442 IMPLEMENT_SETCC(>=, Float
);
443 IMPLEMENT_SETCC(>=, Double
);
444 IMPLEMENT_POINTERSETCC(>=);
446 std::cout
<< "Unhandled type for SetGE instruction: " << *Ty
<< "\n";
452 static GenericValue
executeSetLTInst(GenericValue Src1
, GenericValue Src2
,
455 switch (Ty
->getTypeID()) {
456 IMPLEMENT_SETCC(<, UByte
);
457 IMPLEMENT_SETCC(<, SByte
);
458 IMPLEMENT_SETCC(<, UShort
);
459 IMPLEMENT_SETCC(<, Short
);
460 IMPLEMENT_SETCC(<, UInt
);
461 IMPLEMENT_SETCC(<, Int
);
462 IMPLEMENT_SETCC(<, ULong
);
463 IMPLEMENT_SETCC(<, Long
);
464 IMPLEMENT_SETCC(<, Float
);
465 IMPLEMENT_SETCC(<, Double
);
466 IMPLEMENT_POINTERSETCC(<);
468 std::cout
<< "Unhandled type for SetLT instruction: " << *Ty
<< "\n";
474 static GenericValue
executeSetGTInst(GenericValue Src1
, GenericValue Src2
,
477 switch (Ty
->getTypeID()) {
478 IMPLEMENT_SETCC(>, UByte
);
479 IMPLEMENT_SETCC(>, SByte
);
480 IMPLEMENT_SETCC(>, UShort
);
481 IMPLEMENT_SETCC(>, Short
);
482 IMPLEMENT_SETCC(>, UInt
);
483 IMPLEMENT_SETCC(>, Int
);
484 IMPLEMENT_SETCC(>, ULong
);
485 IMPLEMENT_SETCC(>, Long
);
486 IMPLEMENT_SETCC(>, Float
);
487 IMPLEMENT_SETCC(>, Double
);
488 IMPLEMENT_POINTERSETCC(>);
490 std::cout
<< "Unhandled type for SetGT instruction: " << *Ty
<< "\n";
496 void Interpreter::visitBinaryOperator(BinaryOperator
&I
) {
497 ExecutionContext
&SF
= ECStack
.back();
498 const Type
*Ty
= I
.getOperand(0)->getType();
499 GenericValue Src1
= getOperandValue(I
.getOperand(0), SF
);
500 GenericValue Src2
= getOperandValue(I
.getOperand(1), SF
);
501 GenericValue R
; // Result
503 switch (I
.getOpcode()) {
504 case Instruction::Add
: R
= executeAddInst (Src1
, Src2
, Ty
); break;
505 case Instruction::Sub
: R
= executeSubInst (Src1
, Src2
, Ty
); break;
506 case Instruction::Mul
: R
= executeMulInst (Src1
, Src2
, Ty
); break;
507 case Instruction::Div
: R
= executeDivInst (Src1
, Src2
, Ty
); break;
508 case Instruction::Rem
: R
= executeRemInst (Src1
, Src2
, Ty
); break;
509 case Instruction::And
: R
= executeAndInst (Src1
, Src2
, Ty
); break;
510 case Instruction::Or
: R
= executeOrInst (Src1
, Src2
, Ty
); break;
511 case Instruction::Xor
: R
= executeXorInst (Src1
, Src2
, Ty
); break;
512 case Instruction::SetEQ
: R
= executeSetEQInst(Src1
, Src2
, Ty
); break;
513 case Instruction::SetNE
: R
= executeSetNEInst(Src1
, Src2
, Ty
); break;
514 case Instruction::SetLE
: R
= executeSetLEInst(Src1
, Src2
, Ty
); break;
515 case Instruction::SetGE
: R
= executeSetGEInst(Src1
, Src2
, Ty
); break;
516 case Instruction::SetLT
: R
= executeSetLTInst(Src1
, Src2
, Ty
); break;
517 case Instruction::SetGT
: R
= executeSetGTInst(Src1
, Src2
, Ty
); break;
519 std::cout
<< "Don't know how to handle this binary operator!\n-->" << I
;
526 static GenericValue
executeSelectInst(GenericValue Src1
, GenericValue Src2
,
528 return Src1
.BoolVal
? Src2
: Src3
;
531 void Interpreter::visitSelectInst(SelectInst
&I
) {
532 ExecutionContext
&SF
= ECStack
.back();
533 GenericValue Src1
= getOperandValue(I
.getOperand(0), SF
);
534 GenericValue Src2
= getOperandValue(I
.getOperand(1), SF
);
535 GenericValue Src3
= getOperandValue(I
.getOperand(2), SF
);
536 GenericValue R
= executeSelectInst(Src1
, Src2
, Src3
);
541 //===----------------------------------------------------------------------===//
542 // Terminator Instruction Implementations
543 //===----------------------------------------------------------------------===//
545 void Interpreter::exitCalled(GenericValue GV
) {
546 // runAtExitHandlers() assumes there are no stack frames, but
547 // if exit() was called, then it had a stack frame. Blow away
548 // the stack before interpreting atexit handlers.
550 runAtExitHandlers ();
554 /// Pop the last stack frame off of ECStack and then copy the result
555 /// back into the result variable if we are not returning void. The
556 /// result variable may be the ExitValue, or the Value of the calling
557 /// CallInst if there was a previous stack frame. This method may
558 /// invalidate any ECStack iterators you have. This method also takes
559 /// care of switching to the normal destination BB, if we are returning
562 void Interpreter::popStackAndReturnValueToCaller (const Type
*RetTy
,
563 GenericValue Result
) {
564 // Pop the current stack frame.
567 if (ECStack
.empty()) { // Finished main. Put result into exit code...
568 if (RetTy
&& RetTy
->isIntegral()) { // Nonvoid return type?
569 ExitValue
= Result
; // Capture the exit value of the program
571 memset(&ExitValue
, 0, sizeof(ExitValue
));
574 // If we have a previous stack frame, and we have a previous call,
575 // fill in the return value...
576 ExecutionContext
&CallingSF
= ECStack
.back();
577 if (Instruction
*I
= CallingSF
.Caller
.getInstruction()) {
578 if (CallingSF
.Caller
.getType() != Type::VoidTy
) // Save result...
579 SetValue(I
, Result
, CallingSF
);
580 if (InvokeInst
*II
= dyn_cast
<InvokeInst
> (I
))
581 SwitchToNewBasicBlock (II
->getNormalDest (), CallingSF
);
582 CallingSF
.Caller
= CallSite(); // We returned from the call...
587 void Interpreter::visitReturnInst(ReturnInst
&I
) {
588 ExecutionContext
&SF
= ECStack
.back();
589 const Type
*RetTy
= Type::VoidTy
;
592 // Save away the return value... (if we are not 'ret void')
593 if (I
.getNumOperands()) {
594 RetTy
= I
.getReturnValue()->getType();
595 Result
= getOperandValue(I
.getReturnValue(), SF
);
598 popStackAndReturnValueToCaller(RetTy
, Result
);
601 void Interpreter::visitUnwindInst(UnwindInst
&I
) {
606 if (ECStack
.empty ())
608 Inst
= ECStack
.back ().Caller
.getInstruction ();
609 } while (!(Inst
&& isa
<InvokeInst
> (Inst
)));
611 // Return from invoke
612 ExecutionContext
&InvokingSF
= ECStack
.back ();
613 InvokingSF
.Caller
= CallSite ();
615 // Go to exceptional destination BB of invoke instruction
616 SwitchToNewBasicBlock(cast
<InvokeInst
>(Inst
)->getUnwindDest(), InvokingSF
);
619 void Interpreter::visitUnreachableInst(UnreachableInst
&I
) {
620 std::cerr
<< "ERROR: Program executed an 'unreachable' instruction!\n";
624 void Interpreter::visitBranchInst(BranchInst
&I
) {
625 ExecutionContext
&SF
= ECStack
.back();
628 Dest
= I
.getSuccessor(0); // Uncond branches have a fixed dest...
629 if (!I
.isUnconditional()) {
630 Value
*Cond
= I
.getCondition();
631 if (getOperandValue(Cond
, SF
).BoolVal
== 0) // If false cond...
632 Dest
= I
.getSuccessor(1);
634 SwitchToNewBasicBlock(Dest
, SF
);
637 void Interpreter::visitSwitchInst(SwitchInst
&I
) {
638 ExecutionContext
&SF
= ECStack
.back();
639 GenericValue CondVal
= getOperandValue(I
.getOperand(0), SF
);
640 const Type
*ElTy
= I
.getOperand(0)->getType();
642 // Check to see if any of the cases match...
643 BasicBlock
*Dest
= 0;
644 for (unsigned i
= 2, e
= I
.getNumOperands(); i
!= e
; i
+= 2)
645 if (executeSetEQInst(CondVal
,
646 getOperandValue(I
.getOperand(i
), SF
), ElTy
).BoolVal
) {
647 Dest
= cast
<BasicBlock
>(I
.getOperand(i
+1));
651 if (!Dest
) Dest
= I
.getDefaultDest(); // No cases matched: use default
652 SwitchToNewBasicBlock(Dest
, SF
);
655 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
656 // This function handles the actual updating of block and instruction iterators
657 // as well as execution of all of the PHI nodes in the destination block.
659 // This method does this because all of the PHI nodes must be executed
660 // atomically, reading their inputs before any of the results are updated. Not
661 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
662 // their inputs. If the input PHI node is updated before it is read, incorrect
663 // results can happen. Thus we use a two phase approach.
665 void Interpreter::SwitchToNewBasicBlock(BasicBlock
*Dest
, ExecutionContext
&SF
){
666 BasicBlock
*PrevBB
= SF
.CurBB
; // Remember where we came from...
667 SF
.CurBB
= Dest
; // Update CurBB to branch destination
668 SF
.CurInst
= SF
.CurBB
->begin(); // Update new instruction ptr...
670 if (!isa
<PHINode
>(SF
.CurInst
)) return; // Nothing fancy to do
672 // Loop over all of the PHI nodes in the current block, reading their inputs.
673 std::vector
<GenericValue
> ResultValues
;
675 for (; PHINode
*PN
= dyn_cast
<PHINode
>(SF
.CurInst
); ++SF
.CurInst
) {
676 // Search for the value corresponding to this previous bb...
677 int i
= PN
->getBasicBlockIndex(PrevBB
);
678 assert(i
!= -1 && "PHINode doesn't contain entry for predecessor??");
679 Value
*IncomingValue
= PN
->getIncomingValue(i
);
681 // Save the incoming value for this PHI node...
682 ResultValues
.push_back(getOperandValue(IncomingValue
, SF
));
685 // Now loop over all of the PHI nodes setting their values...
686 SF
.CurInst
= SF
.CurBB
->begin();
687 for (unsigned i
= 0; isa
<PHINode
>(SF
.CurInst
); ++SF
.CurInst
, ++i
) {
688 PHINode
*PN
= cast
<PHINode
>(SF
.CurInst
);
689 SetValue(PN
, ResultValues
[i
], SF
);
693 //===----------------------------------------------------------------------===//
694 // Memory Instruction Implementations
695 //===----------------------------------------------------------------------===//
697 void Interpreter::visitAllocationInst(AllocationInst
&I
) {
698 ExecutionContext
&SF
= ECStack
.back();
700 const Type
*Ty
= I
.getType()->getElementType(); // Type to be allocated
702 // Get the number of elements being allocated by the array...
703 unsigned NumElements
= getOperandValue(I
.getOperand(0), SF
).UIntVal
;
705 // Allocate enough memory to hold the type...
706 void *Memory
= malloc(NumElements
* (size_t)TD
.getTypeSize(Ty
));
708 GenericValue Result
= PTOGV(Memory
);
709 assert(Result
.PointerVal
!= 0 && "Null pointer returned by malloc!");
710 SetValue(&I
, Result
, SF
);
712 if (I
.getOpcode() == Instruction::Alloca
)
713 ECStack
.back().Allocas
.add(Memory
);
716 void Interpreter::visitFreeInst(FreeInst
&I
) {
717 ExecutionContext
&SF
= ECStack
.back();
718 assert(isa
<PointerType
>(I
.getOperand(0)->getType()) && "Freeing nonptr?");
719 GenericValue Value
= getOperandValue(I
.getOperand(0), SF
);
720 // TODO: Check to make sure memory is allocated
721 free(GVTOP(Value
)); // Free memory
724 // getElementOffset - The workhorse for getelementptr.
726 GenericValue
Interpreter::executeGEPOperation(Value
*Ptr
, gep_type_iterator I
,
728 ExecutionContext
&SF
) {
729 assert(isa
<PointerType
>(Ptr
->getType()) &&
730 "Cannot getElementOffset of a nonpointer type!");
734 for (; I
!= E
; ++I
) {
735 if (const StructType
*STy
= dyn_cast
<StructType
>(*I
)) {
736 const StructLayout
*SLO
= TD
.getStructLayout(STy
);
738 const ConstantUInt
*CPU
= cast
<ConstantUInt
>(I
.getOperand());
739 unsigned Index
= unsigned(CPU
->getValue());
741 Total
+= (PointerTy
)SLO
->MemberOffsets
[Index
];
743 const SequentialType
*ST
= cast
<SequentialType
>(*I
);
744 // Get the index number for the array... which must be long type...
745 GenericValue IdxGV
= getOperandValue(I
.getOperand(), SF
);
748 switch (I
.getOperand()->getType()->getTypeID()) {
749 default: assert(0 && "Illegal getelementptr index for sequential type!");
750 case Type::SByteTyID
: Idx
= IdxGV
.SByteVal
; break;
751 case Type::ShortTyID
: Idx
= IdxGV
.ShortVal
; break;
752 case Type::IntTyID
: Idx
= IdxGV
.IntVal
; break;
753 case Type::LongTyID
: Idx
= IdxGV
.LongVal
; break;
754 case Type::UByteTyID
: Idx
= IdxGV
.UByteVal
; break;
755 case Type::UShortTyID
: Idx
= IdxGV
.UShortVal
; break;
756 case Type::UIntTyID
: Idx
= IdxGV
.UIntVal
; break;
757 case Type::ULongTyID
: Idx
= IdxGV
.ULongVal
; break;
759 Total
+= PointerTy(TD
.getTypeSize(ST
->getElementType())*Idx
);
764 Result
.PointerVal
= getOperandValue(Ptr
, SF
).PointerVal
+ Total
;
768 void Interpreter::visitGetElementPtrInst(GetElementPtrInst
&I
) {
769 ExecutionContext
&SF
= ECStack
.back();
770 SetValue(&I
, TheEE
->executeGEPOperation(I
.getPointerOperand(),
771 gep_type_begin(I
), gep_type_end(I
), SF
), SF
);
774 void Interpreter::visitLoadInst(LoadInst
&I
) {
775 ExecutionContext
&SF
= ECStack
.back();
776 GenericValue SRC
= getOperandValue(I
.getPointerOperand(), SF
);
777 GenericValue
*Ptr
= (GenericValue
*)GVTOP(SRC
);
778 GenericValue Result
= LoadValueFromMemory(Ptr
, I
.getType());
779 SetValue(&I
, Result
, SF
);
782 void Interpreter::visitStoreInst(StoreInst
&I
) {
783 ExecutionContext
&SF
= ECStack
.back();
784 GenericValue Val
= getOperandValue(I
.getOperand(0), SF
);
785 GenericValue SRC
= getOperandValue(I
.getPointerOperand(), SF
);
786 StoreValueToMemory(Val
, (GenericValue
*)GVTOP(SRC
),
787 I
.getOperand(0)->getType());
790 //===----------------------------------------------------------------------===//
791 // Miscellaneous Instruction Implementations
792 //===----------------------------------------------------------------------===//
794 void Interpreter::visitCallSite(CallSite CS
) {
795 ExecutionContext
&SF
= ECStack
.back();
797 // Check to see if this is an intrinsic function call...
798 if (Function
*F
= CS
.getCalledFunction())
799 if (F
->isExternal ())
800 switch (F
->getIntrinsicID()) {
801 case Intrinsic::not_intrinsic
:
803 case Intrinsic::vastart
: { // va_start
804 GenericValue ArgIndex
;
805 ArgIndex
.UIntPairVal
.first
= ECStack
.size() - 1;
806 ArgIndex
.UIntPairVal
.second
= 0;
807 SetValue(CS
.getInstruction(), ArgIndex
, SF
);
810 case Intrinsic::vaend
: // va_end is a noop for the interpreter
812 case Intrinsic::vacopy
: // va_copy: dest = src
813 SetValue(CS
.getInstruction(), getOperandValue(*CS
.arg_begin(), SF
), SF
);
816 // If it is an unknown intrinsic function, use the intrinsic lowering
817 // class to transform it into hopefully tasty LLVM code.
819 Instruction
*Prev
= CS
.getInstruction()->getPrev();
820 BasicBlock
*Parent
= CS
.getInstruction()->getParent();
821 IL
->LowerIntrinsicCall(cast
<CallInst
>(CS
.getInstruction()));
823 // Restore the CurInst pointer to the first instruction newly inserted, if
826 SF
.CurInst
= Parent
->begin();
835 std::vector
<GenericValue
> ArgVals
;
836 const unsigned NumArgs
= SF
.Caller
.arg_size();
837 ArgVals
.reserve(NumArgs
);
838 for (CallSite::arg_iterator i
= SF
.Caller
.arg_begin(),
839 e
= SF
.Caller
.arg_end(); i
!= e
; ++i
) {
841 ArgVals
.push_back(getOperandValue(V
, SF
));
842 // Promote all integral types whose size is < sizeof(int) into ints. We do
843 // this by zero or sign extending the value as appropriate according to the
845 const Type
*Ty
= V
->getType();
846 if (Ty
->isIntegral() && Ty
->getPrimitiveSize() < 4) {
847 if (Ty
== Type::ShortTy
)
848 ArgVals
.back().IntVal
= ArgVals
.back().ShortVal
;
849 else if (Ty
== Type::UShortTy
)
850 ArgVals
.back().UIntVal
= ArgVals
.back().UShortVal
;
851 else if (Ty
== Type::SByteTy
)
852 ArgVals
.back().IntVal
= ArgVals
.back().SByteVal
;
853 else if (Ty
== Type::UByteTy
)
854 ArgVals
.back().UIntVal
= ArgVals
.back().UByteVal
;
855 else if (Ty
== Type::BoolTy
)
856 ArgVals
.back().UIntVal
= ArgVals
.back().BoolVal
;
858 assert(0 && "Unknown type!");
862 // To handle indirect calls, we must get the pointer value from the argument
863 // and treat it as a function pointer.
864 GenericValue SRC
= getOperandValue(SF
.Caller
.getCalledValue(), SF
);
865 callFunction((Function
*)GVTOP(SRC
), ArgVals
);
868 #define IMPLEMENT_SHIFT(OP, TY) \
869 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
871 static GenericValue
executeShlInst(GenericValue Src1
, GenericValue Src2
,
874 switch (Ty
->getTypeID()) {
875 IMPLEMENT_SHIFT(<<, UByte
);
876 IMPLEMENT_SHIFT(<<, SByte
);
877 IMPLEMENT_SHIFT(<<, UShort
);
878 IMPLEMENT_SHIFT(<<, Short
);
879 IMPLEMENT_SHIFT(<<, UInt
);
880 IMPLEMENT_SHIFT(<<, Int
);
881 IMPLEMENT_SHIFT(<<, ULong
);
882 IMPLEMENT_SHIFT(<<, Long
);
884 std::cout
<< "Unhandled type for Shl instruction: " << *Ty
<< "\n";
889 static GenericValue
executeShrInst(GenericValue Src1
, GenericValue Src2
,
892 switch (Ty
->getTypeID()) {
893 IMPLEMENT_SHIFT(>>, UByte
);
894 IMPLEMENT_SHIFT(>>, SByte
);
895 IMPLEMENT_SHIFT(>>, UShort
);
896 IMPLEMENT_SHIFT(>>, Short
);
897 IMPLEMENT_SHIFT(>>, UInt
);
898 IMPLEMENT_SHIFT(>>, Int
);
899 IMPLEMENT_SHIFT(>>, ULong
);
900 IMPLEMENT_SHIFT(>>, Long
);
902 std::cout
<< "Unhandled type for Shr instruction: " << *Ty
<< "\n";
908 void Interpreter::visitShl(ShiftInst
&I
) {
909 ExecutionContext
&SF
= ECStack
.back();
910 const Type
*Ty
= I
.getOperand(0)->getType();
911 GenericValue Src1
= getOperandValue(I
.getOperand(0), SF
);
912 GenericValue Src2
= getOperandValue(I
.getOperand(1), SF
);
914 Dest
= executeShlInst (Src1
, Src2
, Ty
);
915 SetValue(&I
, Dest
, SF
);
918 void Interpreter::visitShr(ShiftInst
&I
) {
919 ExecutionContext
&SF
= ECStack
.back();
920 const Type
*Ty
= I
.getOperand(0)->getType();
921 GenericValue Src1
= getOperandValue(I
.getOperand(0), SF
);
922 GenericValue Src2
= getOperandValue(I
.getOperand(1), SF
);
924 Dest
= executeShrInst (Src1
, Src2
, Ty
);
925 SetValue(&I
, Dest
, SF
);
928 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
929 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
931 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
932 case Type::DESTTY##TyID: \
933 switch (SrcTy->getTypeID()) { \
934 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
935 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
936 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
937 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
938 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
939 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
940 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
941 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
942 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
943 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
945 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
946 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
947 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
949 #define IMPLEMENT_CAST_CASE_END() \
950 default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
955 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
956 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
957 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
958 IMPLEMENT_CAST_CASE_END()
960 GenericValue
Interpreter::executeCastOperation(Value
*SrcVal
, const Type
*Ty
,
961 ExecutionContext
&SF
) {
962 const Type
*SrcTy
= SrcVal
->getType();
963 GenericValue Dest
, Src
= getOperandValue(SrcVal
, SF
);
965 switch (Ty
->getTypeID()) {
966 IMPLEMENT_CAST_CASE(UByte
, (unsigned char));
967 IMPLEMENT_CAST_CASE(SByte
, ( signed char));
968 IMPLEMENT_CAST_CASE(UShort
, (unsigned short));
969 IMPLEMENT_CAST_CASE(Short
, ( signed short));
970 IMPLEMENT_CAST_CASE(UInt
, (unsigned int ));
971 IMPLEMENT_CAST_CASE(Int
, ( signed int ));
972 IMPLEMENT_CAST_CASE(ULong
, (uint64_t));
973 IMPLEMENT_CAST_CASE(Long
, ( int64_t));
974 IMPLEMENT_CAST_CASE(Pointer
, (PointerTy
));
975 IMPLEMENT_CAST_CASE(Float
, (float));
976 IMPLEMENT_CAST_CASE(Double
, (double));
977 IMPLEMENT_CAST_CASE(Bool
, (bool));
979 std::cout
<< "Unhandled dest type for cast instruction: " << *Ty
<< "\n";
986 void Interpreter::visitCastInst(CastInst
&I
) {
987 ExecutionContext
&SF
= ECStack
.back();
988 SetValue(&I
, executeCastOperation(I
.getOperand(0), I
.getType(), SF
), SF
);
991 #define IMPLEMENT_VAARG(TY) \
992 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
994 void Interpreter::visitVAArgInst(VAArgInst
&I
) {
995 ExecutionContext
&SF
= ECStack
.back();
997 // Get the incoming valist parameter. LLI treats the valist as a
998 // (ec-stack-depth var-arg-index) pair.
999 GenericValue VAList
= getOperandValue(I
.getOperand(0), SF
);
1001 GenericValue Src
= ECStack
[VAList
.UIntPairVal
.first
]
1002 .VarArgs
[VAList
.UIntPairVal
.second
];
1003 const Type
*Ty
= I
.getType();
1004 switch (Ty
->getTypeID()) {
1005 IMPLEMENT_VAARG(UByte
);
1006 IMPLEMENT_VAARG(SByte
);
1007 IMPLEMENT_VAARG(UShort
);
1008 IMPLEMENT_VAARG(Short
);
1009 IMPLEMENT_VAARG(UInt
);
1010 IMPLEMENT_VAARG(Int
);
1011 IMPLEMENT_VAARG(ULong
);
1012 IMPLEMENT_VAARG(Long
);
1013 IMPLEMENT_VAARG(Pointer
);
1014 IMPLEMENT_VAARG(Float
);
1015 IMPLEMENT_VAARG(Double
);
1016 IMPLEMENT_VAARG(Bool
);
1018 std::cout
<< "Unhandled dest type for vaarg instruction: " << *Ty
<< "\n";
1022 // Set the Value of this Instruction.
1023 SetValue(&I
, Dest
, SF
);
1025 // Move the pointer to the next vararg.
1026 ++VAList
.UIntPairVal
.second
;
1029 //===----------------------------------------------------------------------===//
1030 // Dispatch and Execution Code
1031 //===----------------------------------------------------------------------===//
1033 //===----------------------------------------------------------------------===//
1034 // callFunction - Execute the specified function...
1036 void Interpreter::callFunction(Function
*F
,
1037 const std::vector
<GenericValue
> &ArgVals
) {
1038 assert((ECStack
.empty() || ECStack
.back().Caller
.getInstruction() == 0 ||
1039 ECStack
.back().Caller
.arg_size() == ArgVals
.size()) &&
1040 "Incorrect number of arguments passed into function call!");
1041 // Make a new stack frame... and fill it in.
1042 ECStack
.push_back(ExecutionContext());
1043 ExecutionContext
&StackFrame
= ECStack
.back();
1044 StackFrame
.CurFunction
= F
;
1046 // Special handling for external functions.
1047 if (F
->isExternal()) {
1048 GenericValue Result
= callExternalFunction (F
, ArgVals
);
1049 // Simulate a 'ret' instruction of the appropriate type.
1050 popStackAndReturnValueToCaller (F
->getReturnType (), Result
);
1054 // Get pointers to first LLVM BB & Instruction in function.
1055 StackFrame
.CurBB
= F
->begin();
1056 StackFrame
.CurInst
= StackFrame
.CurBB
->begin();
1058 // Run through the function arguments and initialize their values...
1059 assert((ArgVals
.size() == F
->arg_size() ||
1060 (ArgVals
.size() > F
->arg_size() && F
->getFunctionType()->isVarArg()))&&
1061 "Invalid number of values passed to function invocation!");
1063 // Handle non-varargs arguments...
1065 for (Function::arg_iterator AI
= F
->arg_begin(), E
= F
->arg_end(); AI
!= E
; ++AI
, ++i
)
1066 SetValue(AI
, ArgVals
[i
], StackFrame
);
1068 // Handle varargs arguments...
1069 StackFrame
.VarArgs
.assign(ArgVals
.begin()+i
, ArgVals
.end());
1072 void Interpreter::run() {
1073 while (!ECStack
.empty()) {
1074 // Interpret a single instruction & increment the "PC".
1075 ExecutionContext
&SF
= ECStack
.back(); // Current stack frame
1076 Instruction
&I
= *SF
.CurInst
++; // Increment before execute
1078 // Track the number of dynamic instructions executed.
1081 DEBUG(std::cerr
<< "About to interpret: " << I
);
1082 visit(I
); // Dispatch to one of the visit* methods...