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"
26 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
28 Interpreter *TheEE = 0;
32 //===----------------------------------------------------------------------===//
33 // Value Manipulation code
34 //===----------------------------------------------------------------------===//
36 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
38 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
40 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
42 static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
44 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
46 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
48 static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
50 static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
52 static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
54 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
56 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
58 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
60 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
62 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
64 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
66 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
68 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
70 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
72 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
74 static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
76 static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
78 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
81 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
82 ExecutionContext &SF) {
83 switch (CE->getOpcode()) {
84 case Instruction::Trunc:
85 case Instruction::ZExt:
86 case Instruction::SExt:
87 case Instruction::FPTrunc:
88 case Instruction::FPExt:
89 case Instruction::UIToFP:
90 case Instruction::SIToFP:
91 case Instruction::FPToUI:
92 case Instruction::FPToSI:
93 case Instruction::PtrToInt:
94 case Instruction::IntToPtr:
95 case Instruction::BitCast:
96 return executeCastOperation(Instruction::CastOps(CE->getOpcode()),
97 CE->getOperand(0), CE->getType(), SF);
98 case Instruction::GetElementPtr:
99 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
100 gep_type_end(CE), SF);
101 case Instruction::Add:
102 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
103 getOperandValue(CE->getOperand(1), SF),
104 CE->getOperand(0)->getType());
105 case Instruction::Sub:
106 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
107 getOperandValue(CE->getOperand(1), SF),
108 CE->getOperand(0)->getType());
109 case Instruction::Mul:
110 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
111 getOperandValue(CE->getOperand(1), SF),
112 CE->getOperand(0)->getType());
113 case Instruction::SDiv:
114 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
115 getOperandValue(CE->getOperand(1), SF),
116 CE->getOperand(0)->getType());
117 case Instruction::UDiv:
118 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
119 getOperandValue(CE->getOperand(1), SF),
120 CE->getOperand(0)->getType());
121 case Instruction::FDiv:
122 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
123 getOperandValue(CE->getOperand(1), SF),
124 CE->getOperand(0)->getType());
125 case Instruction::URem:
126 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
127 getOperandValue(CE->getOperand(1), SF),
128 CE->getOperand(0)->getType());
129 case Instruction::SRem:
130 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
131 getOperandValue(CE->getOperand(1), SF),
132 CE->getOperand(0)->getType());
133 case Instruction::FRem:
134 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
135 getOperandValue(CE->getOperand(1), SF),
136 CE->getOperand(0)->getType());
137 case Instruction::And:
138 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
139 getOperandValue(CE->getOperand(1), SF),
140 CE->getOperand(0)->getType());
141 case Instruction::Or:
142 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
143 getOperandValue(CE->getOperand(1), SF),
144 CE->getOperand(0)->getType());
145 case Instruction::Xor:
146 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
147 getOperandValue(CE->getOperand(1), SF),
148 CE->getOperand(0)->getType());
149 case Instruction::SetEQ:
150 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
151 getOperandValue(CE->getOperand(1), SF),
152 CE->getOperand(0)->getType());
153 case Instruction::SetNE:
154 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
155 getOperandValue(CE->getOperand(1), SF),
156 CE->getOperand(0)->getType());
157 case Instruction::SetLE:
158 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
159 getOperandValue(CE->getOperand(1), SF),
160 CE->getOperand(0)->getType());
161 case Instruction::SetGE:
162 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
163 getOperandValue(CE->getOperand(1), SF),
164 CE->getOperand(0)->getType());
165 case Instruction::SetLT:
166 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
167 getOperandValue(CE->getOperand(1), SF),
168 CE->getOperand(0)->getType());
169 case Instruction::SetGT:
170 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
171 getOperandValue(CE->getOperand(1), SF),
172 CE->getOperand(0)->getType());
173 case Instruction::Shl:
174 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
175 getOperandValue(CE->getOperand(1), SF),
176 CE->getOperand(0)->getType());
177 case Instruction::LShr:
178 return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
179 getOperandValue(CE->getOperand(1), SF),
180 CE->getOperand(0)->getType());
181 case Instruction::AShr:
182 return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
183 getOperandValue(CE->getOperand(1), SF),
184 CE->getOperand(0)->getType());
185 case Instruction::Select:
186 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
187 getOperandValue(CE->getOperand(1), SF),
188 getOperandValue(CE->getOperand(2), SF));
190 llvm_cerr << "Unhandled ConstantExpr: " << *CE << "\n";
192 return GenericValue();
196 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
197 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
198 return getConstantExprValue(CE, SF);
199 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
200 return getConstantValue(CPV);
201 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
202 return PTOGV(getPointerToGlobal(GV));
208 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
212 void Interpreter::initializeExecutionEngine() {
216 //===----------------------------------------------------------------------===//
217 // Binary Instruction Implementations
218 //===----------------------------------------------------------------------===//
220 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
221 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
223 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
226 switch (Ty->getTypeID()) {
227 IMPLEMENT_BINARY_OPERATOR(+, UByte);
228 IMPLEMENT_BINARY_OPERATOR(+, SByte);
229 IMPLEMENT_BINARY_OPERATOR(+, UShort);
230 IMPLEMENT_BINARY_OPERATOR(+, Short);
231 IMPLEMENT_BINARY_OPERATOR(+, UInt);
232 IMPLEMENT_BINARY_OPERATOR(+, Int);
233 IMPLEMENT_BINARY_OPERATOR(+, ULong);
234 IMPLEMENT_BINARY_OPERATOR(+, Long);
235 IMPLEMENT_BINARY_OPERATOR(+, Float);
236 IMPLEMENT_BINARY_OPERATOR(+, Double);
238 llvm_cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
244 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
247 switch (Ty->getTypeID()) {
248 IMPLEMENT_BINARY_OPERATOR(-, UByte);
249 IMPLEMENT_BINARY_OPERATOR(-, SByte);
250 IMPLEMENT_BINARY_OPERATOR(-, UShort);
251 IMPLEMENT_BINARY_OPERATOR(-, Short);
252 IMPLEMENT_BINARY_OPERATOR(-, UInt);
253 IMPLEMENT_BINARY_OPERATOR(-, Int);
254 IMPLEMENT_BINARY_OPERATOR(-, ULong);
255 IMPLEMENT_BINARY_OPERATOR(-, Long);
256 IMPLEMENT_BINARY_OPERATOR(-, Float);
257 IMPLEMENT_BINARY_OPERATOR(-, Double);
259 llvm_cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
265 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
268 switch (Ty->getTypeID()) {
269 IMPLEMENT_BINARY_OPERATOR(*, UByte);
270 IMPLEMENT_BINARY_OPERATOR(*, SByte);
271 IMPLEMENT_BINARY_OPERATOR(*, UShort);
272 IMPLEMENT_BINARY_OPERATOR(*, Short);
273 IMPLEMENT_BINARY_OPERATOR(*, UInt);
274 IMPLEMENT_BINARY_OPERATOR(*, Int);
275 IMPLEMENT_BINARY_OPERATOR(*, ULong);
276 IMPLEMENT_BINARY_OPERATOR(*, Long);
277 IMPLEMENT_BINARY_OPERATOR(*, Float);
278 IMPLEMENT_BINARY_OPERATOR(*, Double);
280 llvm_cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
286 #define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
287 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
289 static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
292 switch (Ty->getTypeID()) {
293 IMPLEMENT_SIGNLESS_BINOP(/, UByte, SByte);
294 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
295 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
296 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
298 llvm_cerr << "Unhandled type for UDiv instruction: " << *Ty << "\n";
304 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
307 switch (Ty->getTypeID()) {
308 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
309 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
310 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
311 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
313 llvm_cerr << "Unhandled type for SDiv instruction: " << *Ty << "\n";
319 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
322 switch (Ty->getTypeID()) {
323 IMPLEMENT_BINARY_OPERATOR(/, Float);
324 IMPLEMENT_BINARY_OPERATOR(/, Double);
326 llvm_cerr << "Unhandled type for Div instruction: " << *Ty << "\n";
332 static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
335 switch (Ty->getTypeID()) {
336 IMPLEMENT_SIGNLESS_BINOP(%, UByte, SByte);
337 IMPLEMENT_SIGNLESS_BINOP(%, UShort, Short);
338 IMPLEMENT_SIGNLESS_BINOP(%, UInt, Int);
339 IMPLEMENT_SIGNLESS_BINOP(%, ULong, Long);
341 llvm_cerr << "Unhandled type for URem instruction: " << *Ty << "\n";
347 static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
350 switch (Ty->getTypeID()) {
351 IMPLEMENT_SIGNLESS_BINOP(%, SByte, UByte);
352 IMPLEMENT_SIGNLESS_BINOP(%, Short, UShort);
353 IMPLEMENT_SIGNLESS_BINOP(%, Int, UInt);
354 IMPLEMENT_SIGNLESS_BINOP(%, Long, ULong);
356 llvm_cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
362 static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
365 switch (Ty->getTypeID()) {
366 case Type::FloatTyID:
367 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
369 case Type::DoubleTyID:
370 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
373 llvm_cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
379 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
382 switch (Ty->getTypeID()) {
383 IMPLEMENT_BINARY_OPERATOR(&, Bool);
384 IMPLEMENT_BINARY_OPERATOR(&, UByte);
385 IMPLEMENT_BINARY_OPERATOR(&, SByte);
386 IMPLEMENT_BINARY_OPERATOR(&, UShort);
387 IMPLEMENT_BINARY_OPERATOR(&, Short);
388 IMPLEMENT_BINARY_OPERATOR(&, UInt);
389 IMPLEMENT_BINARY_OPERATOR(&, Int);
390 IMPLEMENT_BINARY_OPERATOR(&, ULong);
391 IMPLEMENT_BINARY_OPERATOR(&, Long);
393 llvm_cerr << "Unhandled type for And instruction: " << *Ty << "\n";
399 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
402 switch (Ty->getTypeID()) {
403 IMPLEMENT_BINARY_OPERATOR(|, Bool);
404 IMPLEMENT_BINARY_OPERATOR(|, UByte);
405 IMPLEMENT_BINARY_OPERATOR(|, SByte);
406 IMPLEMENT_BINARY_OPERATOR(|, UShort);
407 IMPLEMENT_BINARY_OPERATOR(|, Short);
408 IMPLEMENT_BINARY_OPERATOR(|, UInt);
409 IMPLEMENT_BINARY_OPERATOR(|, Int);
410 IMPLEMENT_BINARY_OPERATOR(|, ULong);
411 IMPLEMENT_BINARY_OPERATOR(|, Long);
413 llvm_cerr << "Unhandled type for Or instruction: " << *Ty << "\n";
419 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
422 switch (Ty->getTypeID()) {
423 IMPLEMENT_BINARY_OPERATOR(^, Bool);
424 IMPLEMENT_BINARY_OPERATOR(^, UByte);
425 IMPLEMENT_BINARY_OPERATOR(^, SByte);
426 IMPLEMENT_BINARY_OPERATOR(^, UShort);
427 IMPLEMENT_BINARY_OPERATOR(^, Short);
428 IMPLEMENT_BINARY_OPERATOR(^, UInt);
429 IMPLEMENT_BINARY_OPERATOR(^, Int);
430 IMPLEMENT_BINARY_OPERATOR(^, ULong);
431 IMPLEMENT_BINARY_OPERATOR(^, Long);
433 llvm_cerr << "Unhandled type for Xor instruction: " << *Ty << "\n";
439 #define IMPLEMENT_SETCC(OP, TY) \
440 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
442 // Handle pointers specially because they must be compared with only as much
443 // width as the host has. We _do not_ want to be comparing 64 bit values when
444 // running on a 32-bit target, otherwise the upper 32 bits might mess up
445 // comparisons if they contain garbage.
446 #define IMPLEMENT_POINTERSETCC(OP) \
447 case Type::PointerTyID: \
448 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
449 (void*)(intptr_t)Src2.PointerVal; break
451 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
454 switch (Ty->getTypeID()) {
455 IMPLEMENT_SETCC(==, UByte);
456 IMPLEMENT_SETCC(==, SByte);
457 IMPLEMENT_SETCC(==, UShort);
458 IMPLEMENT_SETCC(==, Short);
459 IMPLEMENT_SETCC(==, UInt);
460 IMPLEMENT_SETCC(==, Int);
461 IMPLEMENT_SETCC(==, ULong);
462 IMPLEMENT_SETCC(==, Long);
463 IMPLEMENT_SETCC(==, Float);
464 IMPLEMENT_SETCC(==, Double);
465 IMPLEMENT_POINTERSETCC(==);
467 llvm_cerr << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
473 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
476 switch (Ty->getTypeID()) {
477 IMPLEMENT_SETCC(!=, UByte);
478 IMPLEMENT_SETCC(!=, SByte);
479 IMPLEMENT_SETCC(!=, UShort);
480 IMPLEMENT_SETCC(!=, Short);
481 IMPLEMENT_SETCC(!=, UInt);
482 IMPLEMENT_SETCC(!=, Int);
483 IMPLEMENT_SETCC(!=, ULong);
484 IMPLEMENT_SETCC(!=, Long);
485 IMPLEMENT_SETCC(!=, Float);
486 IMPLEMENT_SETCC(!=, Double);
487 IMPLEMENT_POINTERSETCC(!=);
490 llvm_cerr << "Unhandled type for SetNE instruction: " << *Ty << "\n";
496 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
499 switch (Ty->getTypeID()) {
500 IMPLEMENT_SETCC(<=, UByte);
501 IMPLEMENT_SETCC(<=, SByte);
502 IMPLEMENT_SETCC(<=, UShort);
503 IMPLEMENT_SETCC(<=, Short);
504 IMPLEMENT_SETCC(<=, UInt);
505 IMPLEMENT_SETCC(<=, Int);
506 IMPLEMENT_SETCC(<=, ULong);
507 IMPLEMENT_SETCC(<=, Long);
508 IMPLEMENT_SETCC(<=, Float);
509 IMPLEMENT_SETCC(<=, Double);
510 IMPLEMENT_POINTERSETCC(<=);
512 llvm_cerr << "Unhandled type for SetLE instruction: " << *Ty << "\n";
518 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
521 switch (Ty->getTypeID()) {
522 IMPLEMENT_SETCC(>=, UByte);
523 IMPLEMENT_SETCC(>=, SByte);
524 IMPLEMENT_SETCC(>=, UShort);
525 IMPLEMENT_SETCC(>=, Short);
526 IMPLEMENT_SETCC(>=, UInt);
527 IMPLEMENT_SETCC(>=, Int);
528 IMPLEMENT_SETCC(>=, ULong);
529 IMPLEMENT_SETCC(>=, Long);
530 IMPLEMENT_SETCC(>=, Float);
531 IMPLEMENT_SETCC(>=, Double);
532 IMPLEMENT_POINTERSETCC(>=);
534 llvm_cerr << "Unhandled type for SetGE instruction: " << *Ty << "\n";
540 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
543 switch (Ty->getTypeID()) {
544 IMPLEMENT_SETCC(<, UByte);
545 IMPLEMENT_SETCC(<, SByte);
546 IMPLEMENT_SETCC(<, UShort);
547 IMPLEMENT_SETCC(<, Short);
548 IMPLEMENT_SETCC(<, UInt);
549 IMPLEMENT_SETCC(<, Int);
550 IMPLEMENT_SETCC(<, ULong);
551 IMPLEMENT_SETCC(<, Long);
552 IMPLEMENT_SETCC(<, Float);
553 IMPLEMENT_SETCC(<, Double);
554 IMPLEMENT_POINTERSETCC(<);
556 llvm_cerr << "Unhandled type for SetLT instruction: " << *Ty << "\n";
562 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
565 switch (Ty->getTypeID()) {
566 IMPLEMENT_SETCC(>, UByte);
567 IMPLEMENT_SETCC(>, SByte);
568 IMPLEMENT_SETCC(>, UShort);
569 IMPLEMENT_SETCC(>, Short);
570 IMPLEMENT_SETCC(>, UInt);
571 IMPLEMENT_SETCC(>, Int);
572 IMPLEMENT_SETCC(>, ULong);
573 IMPLEMENT_SETCC(>, Long);
574 IMPLEMENT_SETCC(>, Float);
575 IMPLEMENT_SETCC(>, Double);
576 IMPLEMENT_POINTERSETCC(>);
578 llvm_cerr << "Unhandled type for SetGT instruction: " << *Ty << "\n";
584 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
585 ExecutionContext &SF = ECStack.back();
586 const Type *Ty = I.getOperand(0)->getType();
587 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
588 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
589 GenericValue R; // Result
591 switch (I.getOpcode()) {
592 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
593 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
594 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
595 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
596 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
597 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
598 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
599 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
600 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
601 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
602 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
603 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
604 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
605 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
606 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
607 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
608 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
609 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
611 llvm_cerr << "Don't know how to handle this binary operator!\n-->" << I;
618 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
620 return Src1.BoolVal ? Src2 : Src3;
623 void Interpreter::visitSelectInst(SelectInst &I) {
624 ExecutionContext &SF = ECStack.back();
625 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
626 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
627 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
628 GenericValue R = executeSelectInst(Src1, Src2, Src3);
633 //===----------------------------------------------------------------------===//
634 // Terminator Instruction Implementations
635 //===----------------------------------------------------------------------===//
637 void Interpreter::exitCalled(GenericValue GV) {
638 // runAtExitHandlers() assumes there are no stack frames, but
639 // if exit() was called, then it had a stack frame. Blow away
640 // the stack before interpreting atexit handlers.
642 runAtExitHandlers ();
646 /// Pop the last stack frame off of ECStack and then copy the result
647 /// back into the result variable if we are not returning void. The
648 /// result variable may be the ExitValue, or the Value of the calling
649 /// CallInst if there was a previous stack frame. This method may
650 /// invalidate any ECStack iterators you have. This method also takes
651 /// care of switching to the normal destination BB, if we are returning
654 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
655 GenericValue Result) {
656 // Pop the current stack frame.
659 if (ECStack.empty()) { // Finished main. Put result into exit code...
660 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
661 ExitValue = Result; // Capture the exit value of the program
663 memset(&ExitValue, 0, sizeof(ExitValue));
666 // If we have a previous stack frame, and we have a previous call,
667 // fill in the return value...
668 ExecutionContext &CallingSF = ECStack.back();
669 if (Instruction *I = CallingSF.Caller.getInstruction()) {
670 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
671 SetValue(I, Result, CallingSF);
672 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
673 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
674 CallingSF.Caller = CallSite(); // We returned from the call...
679 void Interpreter::visitReturnInst(ReturnInst &I) {
680 ExecutionContext &SF = ECStack.back();
681 const Type *RetTy = Type::VoidTy;
684 // Save away the return value... (if we are not 'ret void')
685 if (I.getNumOperands()) {
686 RetTy = I.getReturnValue()->getType();
687 Result = getOperandValue(I.getReturnValue(), SF);
690 popStackAndReturnValueToCaller(RetTy, Result);
693 void Interpreter::visitUnwindInst(UnwindInst &I) {
698 if (ECStack.empty ())
700 Inst = ECStack.back ().Caller.getInstruction ();
701 } while (!(Inst && isa<InvokeInst> (Inst)));
703 // Return from invoke
704 ExecutionContext &InvokingSF = ECStack.back ();
705 InvokingSF.Caller = CallSite ();
707 // Go to exceptional destination BB of invoke instruction
708 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
711 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
712 llvm_cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
716 void Interpreter::visitBranchInst(BranchInst &I) {
717 ExecutionContext &SF = ECStack.back();
720 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
721 if (!I.isUnconditional()) {
722 Value *Cond = I.getCondition();
723 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
724 Dest = I.getSuccessor(1);
726 SwitchToNewBasicBlock(Dest, SF);
729 void Interpreter::visitSwitchInst(SwitchInst &I) {
730 ExecutionContext &SF = ECStack.back();
731 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
732 const Type *ElTy = I.getOperand(0)->getType();
734 // Check to see if any of the cases match...
735 BasicBlock *Dest = 0;
736 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
737 if (executeSetEQInst(CondVal,
738 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
739 Dest = cast<BasicBlock>(I.getOperand(i+1));
743 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
744 SwitchToNewBasicBlock(Dest, SF);
747 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
748 // This function handles the actual updating of block and instruction iterators
749 // as well as execution of all of the PHI nodes in the destination block.
751 // This method does this because all of the PHI nodes must be executed
752 // atomically, reading their inputs before any of the results are updated. Not
753 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
754 // their inputs. If the input PHI node is updated before it is read, incorrect
755 // results can happen. Thus we use a two phase approach.
757 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
758 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
759 SF.CurBB = Dest; // Update CurBB to branch destination
760 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
762 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
764 // Loop over all of the PHI nodes in the current block, reading their inputs.
765 std::vector<GenericValue> ResultValues;
767 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
768 // Search for the value corresponding to this previous bb...
769 int i = PN->getBasicBlockIndex(PrevBB);
770 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
771 Value *IncomingValue = PN->getIncomingValue(i);
773 // Save the incoming value for this PHI node...
774 ResultValues.push_back(getOperandValue(IncomingValue, SF));
777 // Now loop over all of the PHI nodes setting their values...
778 SF.CurInst = SF.CurBB->begin();
779 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
780 PHINode *PN = cast<PHINode>(SF.CurInst);
781 SetValue(PN, ResultValues[i], SF);
785 //===----------------------------------------------------------------------===//
786 // Memory Instruction Implementations
787 //===----------------------------------------------------------------------===//
789 void Interpreter::visitAllocationInst(AllocationInst &I) {
790 ExecutionContext &SF = ECStack.back();
792 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
794 // Get the number of elements being allocated by the array...
795 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
797 // Allocate enough memory to hold the type...
798 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
800 GenericValue Result = PTOGV(Memory);
801 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
802 SetValue(&I, Result, SF);
804 if (I.getOpcode() == Instruction::Alloca)
805 ECStack.back().Allocas.add(Memory);
808 void Interpreter::visitFreeInst(FreeInst &I) {
809 ExecutionContext &SF = ECStack.back();
810 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
811 GenericValue Value = getOperandValue(I.getOperand(0), SF);
812 // TODO: Check to make sure memory is allocated
813 free(GVTOP(Value)); // Free memory
816 // getElementOffset - The workhorse for getelementptr.
818 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
820 ExecutionContext &SF) {
821 assert(isa<PointerType>(Ptr->getType()) &&
822 "Cannot getElementOffset of a nonpointer type!");
826 for (; I != E; ++I) {
827 if (const StructType *STy = dyn_cast<StructType>(*I)) {
828 const StructLayout *SLO = TD.getStructLayout(STy);
830 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
831 unsigned Index = unsigned(CPU->getZExtValue());
833 Total += (PointerTy)SLO->MemberOffsets[Index];
835 const SequentialType *ST = cast<SequentialType>(*I);
836 // Get the index number for the array... which must be long type...
837 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
840 switch (I.getOperand()->getType()->getTypeID()) {
841 default: assert(0 && "Illegal getelementptr index for sequential type!");
842 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
843 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
844 case Type::IntTyID: Idx = IdxGV.IntVal; break;
845 case Type::LongTyID: Idx = IdxGV.LongVal; break;
846 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
847 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
848 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
849 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
851 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
856 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
860 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
861 ExecutionContext &SF = ECStack.back();
862 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
863 gep_type_begin(I), gep_type_end(I), SF), SF);
866 void Interpreter::visitLoadInst(LoadInst &I) {
867 ExecutionContext &SF = ECStack.back();
868 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
869 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
870 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
871 SetValue(&I, Result, SF);
874 void Interpreter::visitStoreInst(StoreInst &I) {
875 ExecutionContext &SF = ECStack.back();
876 GenericValue Val = getOperandValue(I.getOperand(0), SF);
877 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
878 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
879 I.getOperand(0)->getType());
882 //===----------------------------------------------------------------------===//
883 // Miscellaneous Instruction Implementations
884 //===----------------------------------------------------------------------===//
886 void Interpreter::visitCallSite(CallSite CS) {
887 ExecutionContext &SF = ECStack.back();
889 // Check to see if this is an intrinsic function call...
890 if (Function *F = CS.getCalledFunction())
891 if (F->isExternal ())
892 switch (F->getIntrinsicID()) {
893 case Intrinsic::not_intrinsic:
895 case Intrinsic::vastart: { // va_start
896 GenericValue ArgIndex;
897 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
898 ArgIndex.UIntPairVal.second = 0;
899 SetValue(CS.getInstruction(), ArgIndex, SF);
902 case Intrinsic::vaend: // va_end is a noop for the interpreter
904 case Intrinsic::vacopy: // va_copy: dest = src
905 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
908 // If it is an unknown intrinsic function, use the intrinsic lowering
909 // class to transform it into hopefully tasty LLVM code.
911 Instruction *Prev = CS.getInstruction()->getPrev();
912 BasicBlock *Parent = CS.getInstruction()->getParent();
913 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
915 // Restore the CurInst pointer to the first instruction newly inserted, if
918 SF.CurInst = Parent->begin();
927 std::vector<GenericValue> ArgVals;
928 const unsigned NumArgs = SF.Caller.arg_size();
929 ArgVals.reserve(NumArgs);
930 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
931 e = SF.Caller.arg_end(); i != e; ++i) {
933 ArgVals.push_back(getOperandValue(V, SF));
934 // Promote all integral types whose size is < sizeof(int) into ints. We do
935 // this by zero or sign extending the value as appropriate according to the
937 const Type *Ty = V->getType();
938 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
939 if (Ty == Type::ShortTy)
940 ArgVals.back().IntVal = ArgVals.back().ShortVal;
941 else if (Ty == Type::UShortTy)
942 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
943 else if (Ty == Type::SByteTy)
944 ArgVals.back().IntVal = ArgVals.back().SByteVal;
945 else if (Ty == Type::UByteTy)
946 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
947 else if (Ty == Type::BoolTy)
948 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
950 assert(0 && "Unknown type!");
954 // To handle indirect calls, we must get the pointer value from the argument
955 // and treat it as a function pointer.
956 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
957 callFunction((Function*)GVTOP(SRC), ArgVals);
960 #define IMPLEMENT_SHIFT(OP, TY) \
961 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
963 #define IMPLEMENT_SIGNLESS_SHIFT(OP, TY1, TY2) \
964 case Type::TY2##TyID: \
965 IMPLEMENT_SHIFT(OP, TY1)
967 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
970 switch (Ty->getTypeID()) {
971 IMPLEMENT_SHIFT(<<, UByte);
972 IMPLEMENT_SHIFT(<<, SByte);
973 IMPLEMENT_SHIFT(<<, UShort);
974 IMPLEMENT_SHIFT(<<, Short);
975 IMPLEMENT_SHIFT(<<, UInt);
976 IMPLEMENT_SHIFT(<<, Int);
977 IMPLEMENT_SHIFT(<<, ULong);
978 IMPLEMENT_SHIFT(<<, Long);
980 llvm_cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
985 static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
988 switch (Ty->getTypeID()) {
989 IMPLEMENT_SIGNLESS_SHIFT(>>, UByte, SByte);
990 IMPLEMENT_SIGNLESS_SHIFT(>>, UShort, Short);
991 IMPLEMENT_SIGNLESS_SHIFT(>>, UInt, Int);
992 IMPLEMENT_SIGNLESS_SHIFT(>>, ULong, Long);
994 llvm_cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
1000 static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
1003 switch (Ty->getTypeID()) {
1004 IMPLEMENT_SIGNLESS_SHIFT(>>, SByte, UByte);
1005 IMPLEMENT_SIGNLESS_SHIFT(>>, Short, UShort);
1006 IMPLEMENT_SIGNLESS_SHIFT(>>, Int, UInt);
1007 IMPLEMENT_SIGNLESS_SHIFT(>>, Long, ULong);
1009 llvm_cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
1015 void Interpreter::visitShl(ShiftInst &I) {
1016 ExecutionContext &SF = ECStack.back();
1017 const Type *Ty = I.getOperand(0)->getType();
1018 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1019 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1021 Dest = executeShlInst (Src1, Src2, Ty);
1022 SetValue(&I, Dest, SF);
1025 void Interpreter::visitLShr(ShiftInst &I) {
1026 ExecutionContext &SF = ECStack.back();
1027 const Type *Ty = I.getOperand(0)->getType();
1028 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1029 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1031 Dest = executeLShrInst (Src1, Src2, Ty);
1032 SetValue(&I, Dest, SF);
1035 void Interpreter::visitAShr(ShiftInst &I) {
1036 ExecutionContext &SF = ECStack.back();
1037 const Type *Ty = I.getOperand(0)->getType();
1038 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1039 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1041 Dest = executeAShrInst (Src1, Src2, Ty);
1042 SetValue(&I, Dest, SF);
1045 #define IMPLEMENT_CAST_START \
1046 switch (DstTy->getTypeID()) {
1048 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
1049 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
1051 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
1052 case Type::DESTTY##TyID: \
1053 switch (SrcTy->getTypeID()) { \
1054 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
1055 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
1056 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
1057 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1058 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1059 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
1060 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1061 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
1062 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
1063 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer); \
1064 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
1065 IMPLEMENT_CAST(DESTTY, DESTCTY, Double) \
1067 llvm_cerr << "Unhandled cast: " \
1068 << *SrcTy << " to " << *DstTy << "\n"; \
1073 #define IMPLEMENT_CAST_END \
1074 default: llvm_cerr \
1075 << "Unhandled dest type for cast instruction: " \
1076 << *DstTy << "\n"; \
1080 GenericValue Interpreter::executeCastOperation(Instruction::CastOps opcode,
1081 Value *SrcVal, const Type *DstTy,
1082 ExecutionContext &SF) {
1083 const Type *SrcTy = SrcVal->getType();
1084 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1086 if (opcode == Instruction::Trunc && DstTy->getTypeID() == Type::BoolTyID) {
1087 // For truncations to bool, we must clear the high order bits of the source
1088 switch (SrcTy->getTypeID()) {
1089 case Type::BoolTyID: Src.BoolVal &= 1; break;
1090 case Type::SByteTyID: Src.SByteVal &= 1; break;
1091 case Type::UByteTyID: Src.UByteVal &= 1; break;
1092 case Type::ShortTyID: Src.ShortVal &= 1; break;
1093 case Type::UShortTyID: Src.UShortVal &= 1; break;
1094 case Type::IntTyID: Src.IntVal &= 1; break;
1095 case Type::UIntTyID: Src.UIntVal &= 1; break;
1096 case Type::LongTyID: Src.LongVal &= 1; break;
1097 case Type::ULongTyID: Src.ULongVal &= 1; break;
1099 assert(0 && "Can't trunc a non-integer!");
1102 } else if (opcode == Instruction::SExt &&
1103 SrcTy->getTypeID() == Type::BoolTyID) {
1104 // For sign extension from bool, we must extend the source bits.
1105 SrcTy = Type::LongTy;
1106 Src.LongVal = 0 - Src.BoolVal;
1110 case Instruction::Trunc: // src integer, dest integral (can't be long)
1111 IMPLEMENT_CAST_START
1112 IMPLEMENT_CAST_CASE(Bool , (bool));
1113 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1114 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1115 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1116 IMPLEMENT_CAST_CASE(Short , ( signed short));
1117 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1118 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1121 case Instruction::ZExt: // src integral (can't be long), dest integer
1122 IMPLEMENT_CAST_START
1123 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1124 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1125 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1126 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1127 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1128 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int ));
1129 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1130 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1133 case Instruction::SExt: // src integral (can't be long), dest integer
1134 IMPLEMENT_CAST_START
1135 IMPLEMENT_CAST_CASE(UByte , (unsigned char)(signed char));
1136 IMPLEMENT_CAST_CASE(SByte , (signed char));
1137 IMPLEMENT_CAST_CASE(UShort , (unsigned short)(signed short));
1138 IMPLEMENT_CAST_CASE(Short , (signed short));
1139 IMPLEMENT_CAST_CASE(UInt , (unsigned int )(signed int));
1140 IMPLEMENT_CAST_CASE(Int , (signed int));
1141 IMPLEMENT_CAST_CASE(ULong , (uint64_t)(int64_t));
1142 IMPLEMENT_CAST_CASE(Long , (int64_t));
1145 case Instruction::FPTrunc: // src double, dest float
1146 IMPLEMENT_CAST_START
1147 IMPLEMENT_CAST_CASE(Float , (float));
1150 case Instruction::FPExt: // src float, dest double
1151 IMPLEMENT_CAST_START
1152 IMPLEMENT_CAST_CASE(Double , (double));
1155 case Instruction::UIToFP: // src integral, dest floating
1156 IMPLEMENT_CAST_START
1157 IMPLEMENT_CAST_CASE(Float , (float)(uint64_t));
1158 IMPLEMENT_CAST_CASE(Double , (double)(uint64_t));
1161 case Instruction::SIToFP: // src integeral, dest floating
1162 IMPLEMENT_CAST_START
1163 IMPLEMENT_CAST_CASE(Float , (float)(int64_t));
1164 IMPLEMENT_CAST_CASE(Double , (double)(int64_t));
1167 case Instruction::FPToUI: // src floating, dest integral
1168 IMPLEMENT_CAST_START
1169 IMPLEMENT_CAST_CASE(Bool , (bool));
1170 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1171 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1172 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1173 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1174 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1175 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int ));
1176 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1177 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1180 case Instruction::FPToSI: // src floating, dest integral
1181 IMPLEMENT_CAST_START
1182 IMPLEMENT_CAST_CASE(Bool , (bool));
1183 IMPLEMENT_CAST_CASE(UByte , (unsigned char)(signed char));
1184 IMPLEMENT_CAST_CASE(SByte , (signed char));
1185 IMPLEMENT_CAST_CASE(UShort , (unsigned short)(signed short));
1186 IMPLEMENT_CAST_CASE(Short , (signed short));
1187 IMPLEMENT_CAST_CASE(UInt , (unsigned int )(signed int));
1188 IMPLEMENT_CAST_CASE(Int , (signed int));
1189 IMPLEMENT_CAST_CASE(ULong , (uint64_t)(int64_t));
1190 IMPLEMENT_CAST_CASE(Long , (int64_t));
1193 case Instruction::PtrToInt: // src pointer, dest integral
1194 IMPLEMENT_CAST_START
1195 IMPLEMENT_CAST_CASE(Bool , (bool));
1196 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1197 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1198 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1199 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1200 IMPLEMENT_CAST_CASE(UInt , (unsigned int));
1201 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int));
1202 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1203 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1206 case Instruction::IntToPtr: // src integral, dest pointer
1207 IMPLEMENT_CAST_START
1208 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1211 case Instruction::BitCast: // src any, dest any (same size)
1212 IMPLEMENT_CAST_START
1213 IMPLEMENT_CAST_CASE(Bool , (bool));
1214 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1215 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1216 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1217 IMPLEMENT_CAST_CASE(Short , ( signed short));
1218 IMPLEMENT_CAST_CASE(UInt , (unsigned int));
1219 IMPLEMENT_CAST_CASE(Int , ( signed int));
1220 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1221 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1222 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1223 IMPLEMENT_CAST_CASE(Float , (float));
1224 IMPLEMENT_CAST_CASE(Double , (double));
1229 << "Invalid cast opcode for cast instruction: " << opcode << "\n";
1235 void Interpreter::visitCastInst(CastInst &I) {
1236 ExecutionContext &SF = ECStack.back();
1237 SetValue(&I, executeCastOperation(I.getOpcode(), I.getOperand(0),
1238 I.getType(), SF), SF);
1241 #define IMPLEMENT_VAARG(TY) \
1242 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1244 void Interpreter::visitVAArgInst(VAArgInst &I) {
1245 ExecutionContext &SF = ECStack.back();
1247 // Get the incoming valist parameter. LLI treats the valist as a
1248 // (ec-stack-depth var-arg-index) pair.
1249 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1251 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1252 .VarArgs[VAList.UIntPairVal.second];
1253 const Type *Ty = I.getType();
1254 switch (Ty->getTypeID()) {
1255 IMPLEMENT_VAARG(UByte);
1256 IMPLEMENT_VAARG(SByte);
1257 IMPLEMENT_VAARG(UShort);
1258 IMPLEMENT_VAARG(Short);
1259 IMPLEMENT_VAARG(UInt);
1260 IMPLEMENT_VAARG(Int);
1261 IMPLEMENT_VAARG(ULong);
1262 IMPLEMENT_VAARG(Long);
1263 IMPLEMENT_VAARG(Pointer);
1264 IMPLEMENT_VAARG(Float);
1265 IMPLEMENT_VAARG(Double);
1266 IMPLEMENT_VAARG(Bool);
1268 llvm_cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1272 // Set the Value of this Instruction.
1273 SetValue(&I, Dest, SF);
1275 // Move the pointer to the next vararg.
1276 ++VAList.UIntPairVal.second;
1279 //===----------------------------------------------------------------------===//
1280 // Dispatch and Execution Code
1281 //===----------------------------------------------------------------------===//
1283 //===----------------------------------------------------------------------===//
1284 // callFunction - Execute the specified function...
1286 void Interpreter::callFunction(Function *F,
1287 const std::vector<GenericValue> &ArgVals) {
1288 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1289 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1290 "Incorrect number of arguments passed into function call!");
1291 // Make a new stack frame... and fill it in.
1292 ECStack.push_back(ExecutionContext());
1293 ExecutionContext &StackFrame = ECStack.back();
1294 StackFrame.CurFunction = F;
1296 // Special handling for external functions.
1297 if (F->isExternal()) {
1298 GenericValue Result = callExternalFunction (F, ArgVals);
1299 // Simulate a 'ret' instruction of the appropriate type.
1300 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1304 // Get pointers to first LLVM BB & Instruction in function.
1305 StackFrame.CurBB = F->begin();
1306 StackFrame.CurInst = StackFrame.CurBB->begin();
1308 // Run through the function arguments and initialize their values...
1309 assert((ArgVals.size() == F->arg_size() ||
1310 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1311 "Invalid number of values passed to function invocation!");
1313 // Handle non-varargs arguments...
1315 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
1316 SetValue(AI, ArgVals[i], StackFrame);
1318 // Handle varargs arguments...
1319 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1322 void Interpreter::run() {
1323 while (!ECStack.empty()) {
1324 // Interpret a single instruction & increment the "PC".
1325 ExecutionContext &SF = ECStack.back(); // Current stack frame
1326 Instruction &I = *SF.CurInst++; // Increment before execute
1328 // Track the number of dynamic instructions executed.
1331 DOUT << "About to interpret: " << I;
1332 visit(I); // Dispatch to one of the visit* methods...