1 //===-- Execution.cpp - Implement code to simulate the program ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/ADT/APInt.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/CodeGen/IntrinsicLowering.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/GetElementPtrTypeIterator.h"
26 #include "llvm/Support/MathExtras.h"
31 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
33 static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
34 cl::desc("make the interpreter print every volatile load and store"));
36 //===----------------------------------------------------------------------===//
37 // Various Helper Functions
38 //===----------------------------------------------------------------------===//
40 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
44 //===----------------------------------------------------------------------===//
45 // Binary Instruction Implementations
46 //===----------------------------------------------------------------------===//
48 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
49 case Type::TY##TyID: \
50 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
53 static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
54 GenericValue Src2, Type *Ty) {
55 switch (Ty->getTypeID()) {
56 IMPLEMENT_BINARY_OPERATOR(+, Float);
57 IMPLEMENT_BINARY_OPERATOR(+, Double);
59 dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n";
64 static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
65 GenericValue Src2, Type *Ty) {
66 switch (Ty->getTypeID()) {
67 IMPLEMENT_BINARY_OPERATOR(-, Float);
68 IMPLEMENT_BINARY_OPERATOR(-, Double);
70 dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n";
75 static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
76 GenericValue Src2, Type *Ty) {
77 switch (Ty->getTypeID()) {
78 IMPLEMENT_BINARY_OPERATOR(*, Float);
79 IMPLEMENT_BINARY_OPERATOR(*, Double);
81 dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n";
86 static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
87 GenericValue Src2, Type *Ty) {
88 switch (Ty->getTypeID()) {
89 IMPLEMENT_BINARY_OPERATOR(/, Float);
90 IMPLEMENT_BINARY_OPERATOR(/, Double);
92 dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n";
97 static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
98 GenericValue Src2, Type *Ty) {
99 switch (Ty->getTypeID()) {
100 case Type::FloatTyID:
101 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
103 case Type::DoubleTyID:
104 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
107 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
112 #define IMPLEMENT_INTEGER_ICMP(OP, TY) \
113 case Type::IntegerTyID: \
114 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
117 #define IMPLEMENT_VECTOR_INTEGER_ICMP(OP, TY) \
118 case Type::VectorTyID: { \
119 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \
120 Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \
121 for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \
122 Dest.AggregateVal[_i].IntVal = APInt(1, \
123 Src1.AggregateVal[_i].IntVal.OP(Src2.AggregateVal[_i].IntVal));\
126 // Handle pointers specially because they must be compared with only as much
127 // width as the host has. We _do not_ want to be comparing 64 bit values when
128 // running on a 32-bit target, otherwise the upper 32 bits might mess up
129 // comparisons if they contain garbage.
130 #define IMPLEMENT_POINTER_ICMP(OP) \
131 case Type::PointerTyID: \
132 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
133 (void*)(intptr_t)Src2.PointerVal); \
136 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
139 switch (Ty->getTypeID()) {
140 IMPLEMENT_INTEGER_ICMP(eq,Ty);
141 IMPLEMENT_VECTOR_INTEGER_ICMP(eq,Ty);
142 IMPLEMENT_POINTER_ICMP(==);
144 dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
150 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
153 switch (Ty->getTypeID()) {
154 IMPLEMENT_INTEGER_ICMP(ne,Ty);
155 IMPLEMENT_VECTOR_INTEGER_ICMP(ne,Ty);
156 IMPLEMENT_POINTER_ICMP(!=);
158 dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
164 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
167 switch (Ty->getTypeID()) {
168 IMPLEMENT_INTEGER_ICMP(ult,Ty);
169 IMPLEMENT_VECTOR_INTEGER_ICMP(ult,Ty);
170 IMPLEMENT_POINTER_ICMP(<);
172 dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
178 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
181 switch (Ty->getTypeID()) {
182 IMPLEMENT_INTEGER_ICMP(slt,Ty);
183 IMPLEMENT_VECTOR_INTEGER_ICMP(slt,Ty);
184 IMPLEMENT_POINTER_ICMP(<);
186 dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
192 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
195 switch (Ty->getTypeID()) {
196 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
197 IMPLEMENT_VECTOR_INTEGER_ICMP(ugt,Ty);
198 IMPLEMENT_POINTER_ICMP(>);
200 dbgs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
206 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
209 switch (Ty->getTypeID()) {
210 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
211 IMPLEMENT_VECTOR_INTEGER_ICMP(sgt,Ty);
212 IMPLEMENT_POINTER_ICMP(>);
214 dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
220 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
223 switch (Ty->getTypeID()) {
224 IMPLEMENT_INTEGER_ICMP(ule,Ty);
225 IMPLEMENT_VECTOR_INTEGER_ICMP(ule,Ty);
226 IMPLEMENT_POINTER_ICMP(<=);
228 dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
234 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
237 switch (Ty->getTypeID()) {
238 IMPLEMENT_INTEGER_ICMP(sle,Ty);
239 IMPLEMENT_VECTOR_INTEGER_ICMP(sle,Ty);
240 IMPLEMENT_POINTER_ICMP(<=);
242 dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
248 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
251 switch (Ty->getTypeID()) {
252 IMPLEMENT_INTEGER_ICMP(uge,Ty);
253 IMPLEMENT_VECTOR_INTEGER_ICMP(uge,Ty);
254 IMPLEMENT_POINTER_ICMP(>=);
256 dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
262 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
265 switch (Ty->getTypeID()) {
266 IMPLEMENT_INTEGER_ICMP(sge,Ty);
267 IMPLEMENT_VECTOR_INTEGER_ICMP(sge,Ty);
268 IMPLEMENT_POINTER_ICMP(>=);
270 dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
276 void Interpreter::visitICmpInst(ICmpInst &I) {
277 ExecutionContext &SF = ECStack.back();
278 Type *Ty = I.getOperand(0)->getType();
279 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
280 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
281 GenericValue R; // Result
283 switch (I.getPredicate()) {
284 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
285 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
286 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
287 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
288 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
289 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
290 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
291 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
292 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
293 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
295 dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I;
302 #define IMPLEMENT_FCMP(OP, TY) \
303 case Type::TY##TyID: \
304 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
307 #define IMPLEMENT_VECTOR_FCMP_T(OP, TY) \
308 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \
309 Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \
310 for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \
311 Dest.AggregateVal[_i].IntVal = APInt(1, \
312 Src1.AggregateVal[_i].TY##Val OP Src2.AggregateVal[_i].TY##Val);\
315 #define IMPLEMENT_VECTOR_FCMP(OP) \
316 case Type::VectorTyID: \
317 if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) { \
318 IMPLEMENT_VECTOR_FCMP_T(OP, Float); \
320 IMPLEMENT_VECTOR_FCMP_T(OP, Double); \
323 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
326 switch (Ty->getTypeID()) {
327 IMPLEMENT_FCMP(==, Float);
328 IMPLEMENT_FCMP(==, Double);
329 IMPLEMENT_VECTOR_FCMP(==);
331 dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
337 #define IMPLEMENT_SCALAR_NANS(TY, X,Y) \
338 if (TY->isFloatTy()) { \
339 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
340 Dest.IntVal = APInt(1,false); \
344 if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
345 Dest.IntVal = APInt(1,false); \
350 #define MASK_VECTOR_NANS_T(X,Y, TZ, FLAG) \
351 assert(X.AggregateVal.size() == Y.AggregateVal.size()); \
352 Dest.AggregateVal.resize( X.AggregateVal.size() ); \
353 for( uint32_t _i=0;_i<X.AggregateVal.size();_i++) { \
354 if (X.AggregateVal[_i].TZ##Val != X.AggregateVal[_i].TZ##Val || \
355 Y.AggregateVal[_i].TZ##Val != Y.AggregateVal[_i].TZ##Val) \
356 Dest.AggregateVal[_i].IntVal = APInt(1,FLAG); \
358 Dest.AggregateVal[_i].IntVal = APInt(1,!FLAG); \
362 #define MASK_VECTOR_NANS(TY, X,Y, FLAG) \
363 if (TY->isVectorTy()) { \
364 if (dyn_cast<VectorType>(TY)->getElementType()->isFloatTy()) { \
365 MASK_VECTOR_NANS_T(X, Y, Float, FLAG) \
367 MASK_VECTOR_NANS_T(X, Y, Double, FLAG) \
373 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
377 // if input is scalar value and Src1 or Src2 is NaN return false
378 IMPLEMENT_SCALAR_NANS(Ty, Src1, Src2)
379 // if vector input detect NaNs and fill mask
380 MASK_VECTOR_NANS(Ty, Src1, Src2, false)
381 GenericValue DestMask = Dest;
382 switch (Ty->getTypeID()) {
383 IMPLEMENT_FCMP(!=, Float);
384 IMPLEMENT_FCMP(!=, Double);
385 IMPLEMENT_VECTOR_FCMP(!=);
387 dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
390 // in vector case mask out NaN elements
391 if (Ty->isVectorTy())
392 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
393 if (DestMask.AggregateVal[_i].IntVal == false)
394 Dest.AggregateVal[_i].IntVal = APInt(1,false);
399 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
402 switch (Ty->getTypeID()) {
403 IMPLEMENT_FCMP(<=, Float);
404 IMPLEMENT_FCMP(<=, Double);
405 IMPLEMENT_VECTOR_FCMP(<=);
407 dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
413 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
416 switch (Ty->getTypeID()) {
417 IMPLEMENT_FCMP(>=, Float);
418 IMPLEMENT_FCMP(>=, Double);
419 IMPLEMENT_VECTOR_FCMP(>=);
421 dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
427 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
430 switch (Ty->getTypeID()) {
431 IMPLEMENT_FCMP(<, Float);
432 IMPLEMENT_FCMP(<, Double);
433 IMPLEMENT_VECTOR_FCMP(<);
435 dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
441 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
444 switch (Ty->getTypeID()) {
445 IMPLEMENT_FCMP(>, Float);
446 IMPLEMENT_FCMP(>, Double);
447 IMPLEMENT_VECTOR_FCMP(>);
449 dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
455 #define IMPLEMENT_UNORDERED(TY, X,Y) \
456 if (TY->isFloatTy()) { \
457 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
458 Dest.IntVal = APInt(1,true); \
461 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
462 Dest.IntVal = APInt(1,true); \
466 #define IMPLEMENT_VECTOR_UNORDERED(TY, X,Y, _FUNC) \
467 if (TY->isVectorTy()) { \
468 GenericValue DestMask = Dest; \
469 Dest = _FUNC(Src1, Src2, Ty); \
470 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++) \
471 if (DestMask.AggregateVal[_i].IntVal == true) \
472 Dest.AggregateVal[_i].IntVal = APInt(1,true); \
476 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
479 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
480 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
481 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OEQ)
482 return executeFCMP_OEQ(Src1, Src2, Ty);
486 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
489 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
490 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
491 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_ONE)
492 return executeFCMP_ONE(Src1, Src2, Ty);
495 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
498 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
499 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
500 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLE)
501 return executeFCMP_OLE(Src1, Src2, Ty);
504 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
507 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
508 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
509 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGE)
510 return executeFCMP_OGE(Src1, Src2, Ty);
513 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
516 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
517 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
518 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLT)
519 return executeFCMP_OLT(Src1, Src2, Ty);
522 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
525 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
526 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
527 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGT)
528 return executeFCMP_OGT(Src1, Src2, Ty);
531 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
534 if(Ty->isVectorTy()) {
535 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
536 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
537 if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
538 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
539 Dest.AggregateVal[_i].IntVal = APInt(1,
540 ( (Src1.AggregateVal[_i].FloatVal ==
541 Src1.AggregateVal[_i].FloatVal) &&
542 (Src2.AggregateVal[_i].FloatVal ==
543 Src2.AggregateVal[_i].FloatVal)));
545 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
546 Dest.AggregateVal[_i].IntVal = APInt(1,
547 ( (Src1.AggregateVal[_i].DoubleVal ==
548 Src1.AggregateVal[_i].DoubleVal) &&
549 (Src2.AggregateVal[_i].DoubleVal ==
550 Src2.AggregateVal[_i].DoubleVal)));
552 } else if (Ty->isFloatTy())
553 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
554 Src2.FloatVal == Src2.FloatVal));
556 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
557 Src2.DoubleVal == Src2.DoubleVal));
562 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
565 if(Ty->isVectorTy()) {
566 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
567 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
568 if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
569 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
570 Dest.AggregateVal[_i].IntVal = APInt(1,
571 ( (Src1.AggregateVal[_i].FloatVal !=
572 Src1.AggregateVal[_i].FloatVal) ||
573 (Src2.AggregateVal[_i].FloatVal !=
574 Src2.AggregateVal[_i].FloatVal)));
576 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
577 Dest.AggregateVal[_i].IntVal = APInt(1,
578 ( (Src1.AggregateVal[_i].DoubleVal !=
579 Src1.AggregateVal[_i].DoubleVal) ||
580 (Src2.AggregateVal[_i].DoubleVal !=
581 Src2.AggregateVal[_i].DoubleVal)));
583 } else if (Ty->isFloatTy())
584 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
585 Src2.FloatVal != Src2.FloatVal));
587 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
588 Src2.DoubleVal != Src2.DoubleVal));
593 static GenericValue executeFCMP_BOOL(GenericValue Src1, GenericValue Src2,
594 const Type *Ty, const bool val) {
596 if(Ty->isVectorTy()) {
597 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
598 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
599 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
600 Dest.AggregateVal[_i].IntVal = APInt(1,val);
602 Dest.IntVal = APInt(1, val);
608 void Interpreter::visitFCmpInst(FCmpInst &I) {
609 ExecutionContext &SF = ECStack.back();
610 Type *Ty = I.getOperand(0)->getType();
611 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
612 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
613 GenericValue R; // Result
615 switch (I.getPredicate()) {
617 dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I;
620 case FCmpInst::FCMP_FALSE: R = executeFCMP_BOOL(Src1, Src2, Ty, false);
622 case FCmpInst::FCMP_TRUE: R = executeFCMP_BOOL(Src1, Src2, Ty, true);
624 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
625 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
626 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
627 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
628 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
629 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
630 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
631 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
632 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
633 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
634 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
635 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
636 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
637 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
643 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
644 GenericValue Src2, Type *Ty) {
647 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
648 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
649 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
650 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
651 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
652 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
653 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
654 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
655 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
656 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
657 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
658 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
659 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
660 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
661 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
662 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
663 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
664 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
665 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
666 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
667 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
668 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
669 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
670 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
671 case FCmpInst::FCMP_FALSE: return executeFCMP_BOOL(Src1, Src2, Ty, false);
672 case FCmpInst::FCMP_TRUE: return executeFCMP_BOOL(Src1, Src2, Ty, true);
674 dbgs() << "Unhandled Cmp predicate\n";
679 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
680 ExecutionContext &SF = ECStack.back();
681 Type *Ty = I.getOperand(0)->getType();
682 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
683 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
684 GenericValue R; // Result
686 // First process vector operation
687 if (Ty->isVectorTy()) {
688 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
689 R.AggregateVal.resize(Src1.AggregateVal.size());
691 // Macros to execute binary operation 'OP' over integer vectors
692 #define INTEGER_VECTOR_OPERATION(OP) \
693 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
694 R.AggregateVal[i].IntVal = \
695 Src1.AggregateVal[i].IntVal OP Src2.AggregateVal[i].IntVal;
697 // Additional macros to execute binary operations udiv/sdiv/urem/srem since
698 // they have different notation.
699 #define INTEGER_VECTOR_FUNCTION(OP) \
700 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
701 R.AggregateVal[i].IntVal = \
702 Src1.AggregateVal[i].IntVal.OP(Src2.AggregateVal[i].IntVal);
704 // Macros to execute binary operation 'OP' over floating point type TY
705 // (float or double) vectors
706 #define FLOAT_VECTOR_FUNCTION(OP, TY) \
707 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
708 R.AggregateVal[i].TY = \
709 Src1.AggregateVal[i].TY OP Src2.AggregateVal[i].TY;
711 // Macros to choose appropriate TY: float or double and run operation
713 #define FLOAT_VECTOR_OP(OP) { \
714 if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) \
715 FLOAT_VECTOR_FUNCTION(OP, FloatVal) \
717 if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy()) \
718 FLOAT_VECTOR_FUNCTION(OP, DoubleVal) \
720 dbgs() << "Unhandled type for OP instruction: " << *Ty << "\n"; \
721 llvm_unreachable(0); \
726 switch(I.getOpcode()){
728 dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
731 case Instruction::Add: INTEGER_VECTOR_OPERATION(+) break;
732 case Instruction::Sub: INTEGER_VECTOR_OPERATION(-) break;
733 case Instruction::Mul: INTEGER_VECTOR_OPERATION(*) break;
734 case Instruction::UDiv: INTEGER_VECTOR_FUNCTION(udiv) break;
735 case Instruction::SDiv: INTEGER_VECTOR_FUNCTION(sdiv) break;
736 case Instruction::URem: INTEGER_VECTOR_FUNCTION(urem) break;
737 case Instruction::SRem: INTEGER_VECTOR_FUNCTION(srem) break;
738 case Instruction::And: INTEGER_VECTOR_OPERATION(&) break;
739 case Instruction::Or: INTEGER_VECTOR_OPERATION(|) break;
740 case Instruction::Xor: INTEGER_VECTOR_OPERATION(^) break;
741 case Instruction::FAdd: FLOAT_VECTOR_OP(+) break;
742 case Instruction::FSub: FLOAT_VECTOR_OP(-) break;
743 case Instruction::FMul: FLOAT_VECTOR_OP(*) break;
744 case Instruction::FDiv: FLOAT_VECTOR_OP(/) break;
745 case Instruction::FRem:
746 if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy())
747 for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
748 R.AggregateVal[i].FloatVal =
749 fmod(Src1.AggregateVal[i].FloatVal, Src2.AggregateVal[i].FloatVal);
751 if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy())
752 for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
753 R.AggregateVal[i].DoubleVal =
754 fmod(Src1.AggregateVal[i].DoubleVal, Src2.AggregateVal[i].DoubleVal);
756 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
763 switch (I.getOpcode()) {
765 dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
768 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
769 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
770 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
771 case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;
772 case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;
773 case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;
774 case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;
775 case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;
776 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
777 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
778 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
779 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
780 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
781 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
782 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
788 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
790 return Src1.IntVal == 0 ? Src3 : Src2;
793 void Interpreter::visitSelectInst(SelectInst &I) {
794 ExecutionContext &SF = ECStack.back();
795 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
796 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
797 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
798 GenericValue R = executeSelectInst(Src1, Src2, Src3);
803 //===----------------------------------------------------------------------===//
804 // Terminator Instruction Implementations
805 //===----------------------------------------------------------------------===//
807 void Interpreter::exitCalled(GenericValue GV) {
808 // runAtExitHandlers() assumes there are no stack frames, but
809 // if exit() was called, then it had a stack frame. Blow away
810 // the stack before interpreting atexit handlers.
813 exit(GV.IntVal.zextOrTrunc(32).getZExtValue());
816 /// Pop the last stack frame off of ECStack and then copy the result
817 /// back into the result variable if we are not returning void. The
818 /// result variable may be the ExitValue, or the Value of the calling
819 /// CallInst if there was a previous stack frame. This method may
820 /// invalidate any ECStack iterators you have. This method also takes
821 /// care of switching to the normal destination BB, if we are returning
824 void Interpreter::popStackAndReturnValueToCaller(Type *RetTy,
825 GenericValue Result) {
826 // Pop the current stack frame.
829 if (ECStack.empty()) { // Finished main. Put result into exit code...
830 if (RetTy && !RetTy->isVoidTy()) { // Nonvoid return type?
831 ExitValue = Result; // Capture the exit value of the program
833 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
836 // If we have a previous stack frame, and we have a previous call,
837 // fill in the return value...
838 ExecutionContext &CallingSF = ECStack.back();
839 if (Instruction *I = CallingSF.Caller.getInstruction()) {
841 if (!CallingSF.Caller.getType()->isVoidTy())
842 SetValue(I, Result, CallingSF);
843 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
844 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
845 CallingSF.Caller = CallSite(); // We returned from the call...
850 void Interpreter::visitReturnInst(ReturnInst &I) {
851 ExecutionContext &SF = ECStack.back();
852 Type *RetTy = Type::getVoidTy(I.getContext());
855 // Save away the return value... (if we are not 'ret void')
856 if (I.getNumOperands()) {
857 RetTy = I.getReturnValue()->getType();
858 Result = getOperandValue(I.getReturnValue(), SF);
861 popStackAndReturnValueToCaller(RetTy, Result);
864 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
865 report_fatal_error("Program executed an 'unreachable' instruction!");
868 void Interpreter::visitBranchInst(BranchInst &I) {
869 ExecutionContext &SF = ECStack.back();
872 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
873 if (!I.isUnconditional()) {
874 Value *Cond = I.getCondition();
875 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
876 Dest = I.getSuccessor(1);
878 SwitchToNewBasicBlock(Dest, SF);
881 void Interpreter::visitSwitchInst(SwitchInst &I) {
882 ExecutionContext &SF = ECStack.back();
883 Value* Cond = I.getCondition();
884 Type *ElTy = Cond->getType();
885 GenericValue CondVal = getOperandValue(Cond, SF);
887 // Check to see if any of the cases match...
888 BasicBlock *Dest = 0;
889 for (SwitchInst::CaseIt i = I.case_begin(), e = I.case_end(); i != e; ++i) {
890 IntegersSubset& Case = i.getCaseValueEx();
891 if (Case.isSingleNumber()) {
892 // FIXME: Currently work with ConstantInt based numbers.
893 const ConstantInt *CI = Case.getSingleNumber(0).toConstantInt();
894 GenericValue Val = getOperandValue(const_cast<ConstantInt*>(CI), SF);
895 if (executeICMP_EQ(Val, CondVal, ElTy).IntVal != 0) {
896 Dest = cast<BasicBlock>(i.getCaseSuccessor());
900 if (Case.isSingleNumbersOnly()) {
901 for (unsigned n = 0, en = Case.getNumItems(); n != en; ++n) {
902 // FIXME: Currently work with ConstantInt based numbers.
903 const ConstantInt *CI = Case.getSingleNumber(n).toConstantInt();
904 GenericValue Val = getOperandValue(const_cast<ConstantInt*>(CI), SF);
905 if (executeICMP_EQ(Val, CondVal, ElTy).IntVal != 0) {
906 Dest = cast<BasicBlock>(i.getCaseSuccessor());
911 for (unsigned n = 0, en = Case.getNumItems(); n != en; ++n) {
912 IntegersSubset::Range r = Case.getItem(n);
913 // FIXME: Currently work with ConstantInt based numbers.
914 const ConstantInt *LowCI = r.getLow().toConstantInt();
915 const ConstantInt *HighCI = r.getHigh().toConstantInt();
916 GenericValue Low = getOperandValue(const_cast<ConstantInt*>(LowCI), SF);
917 GenericValue High = getOperandValue(const_cast<ConstantInt*>(HighCI), SF);
918 if (executeICMP_ULE(Low, CondVal, ElTy).IntVal != 0 &&
919 executeICMP_ULE(CondVal, High, ElTy).IntVal != 0) {
920 Dest = cast<BasicBlock>(i.getCaseSuccessor());
925 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
926 SwitchToNewBasicBlock(Dest, SF);
929 void Interpreter::visitIndirectBrInst(IndirectBrInst &I) {
930 ExecutionContext &SF = ECStack.back();
931 void *Dest = GVTOP(getOperandValue(I.getAddress(), SF));
932 SwitchToNewBasicBlock((BasicBlock*)Dest, SF);
936 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
937 // This function handles the actual updating of block and instruction iterators
938 // as well as execution of all of the PHI nodes in the destination block.
940 // This method does this because all of the PHI nodes must be executed
941 // atomically, reading their inputs before any of the results are updated. Not
942 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
943 // their inputs. If the input PHI node is updated before it is read, incorrect
944 // results can happen. Thus we use a two phase approach.
946 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
947 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
948 SF.CurBB = Dest; // Update CurBB to branch destination
949 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
951 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
953 // Loop over all of the PHI nodes in the current block, reading their inputs.
954 std::vector<GenericValue> ResultValues;
956 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
957 // Search for the value corresponding to this previous bb...
958 int i = PN->getBasicBlockIndex(PrevBB);
959 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
960 Value *IncomingValue = PN->getIncomingValue(i);
962 // Save the incoming value for this PHI node...
963 ResultValues.push_back(getOperandValue(IncomingValue, SF));
966 // Now loop over all of the PHI nodes setting their values...
967 SF.CurInst = SF.CurBB->begin();
968 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
969 PHINode *PN = cast<PHINode>(SF.CurInst);
970 SetValue(PN, ResultValues[i], SF);
974 //===----------------------------------------------------------------------===//
975 // Memory Instruction Implementations
976 //===----------------------------------------------------------------------===//
978 void Interpreter::visitAllocaInst(AllocaInst &I) {
979 ExecutionContext &SF = ECStack.back();
981 Type *Ty = I.getType()->getElementType(); // Type to be allocated
983 // Get the number of elements being allocated by the array...
984 unsigned NumElements =
985 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
987 unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
989 // Avoid malloc-ing zero bytes, use max()...
990 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
992 // Allocate enough memory to hold the type...
993 void *Memory = malloc(MemToAlloc);
995 DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
996 << NumElements << " (Total: " << MemToAlloc << ") at "
997 << uintptr_t(Memory) << '\n');
999 GenericValue Result = PTOGV(Memory);
1000 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
1001 SetValue(&I, Result, SF);
1003 if (I.getOpcode() == Instruction::Alloca)
1004 ECStack.back().Allocas.add(Memory);
1007 // getElementOffset - The workhorse for getelementptr.
1009 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
1010 gep_type_iterator E,
1011 ExecutionContext &SF) {
1012 assert(Ptr->getType()->isPointerTy() &&
1013 "Cannot getElementOffset of a nonpointer type!");
1017 for (; I != E; ++I) {
1018 if (StructType *STy = dyn_cast<StructType>(*I)) {
1019 const StructLayout *SLO = TD.getStructLayout(STy);
1021 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
1022 unsigned Index = unsigned(CPU->getZExtValue());
1024 Total += SLO->getElementOffset(Index);
1026 SequentialType *ST = cast<SequentialType>(*I);
1027 // Get the index number for the array... which must be long type...
1028 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
1032 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
1034 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
1036 assert(BitWidth == 64 && "Invalid index type for getelementptr");
1037 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
1039 Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
1043 GenericValue Result;
1044 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
1045 DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n");
1049 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1050 ExecutionContext &SF = ECStack.back();
1051 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
1052 gep_type_begin(I), gep_type_end(I), SF), SF);
1055 void Interpreter::visitLoadInst(LoadInst &I) {
1056 ExecutionContext &SF = ECStack.back();
1057 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
1058 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
1059 GenericValue Result;
1060 LoadValueFromMemory(Result, Ptr, I.getType());
1061 SetValue(&I, Result, SF);
1062 if (I.isVolatile() && PrintVolatile)
1063 dbgs() << "Volatile load " << I;
1066 void Interpreter::visitStoreInst(StoreInst &I) {
1067 ExecutionContext &SF = ECStack.back();
1068 GenericValue Val = getOperandValue(I.getOperand(0), SF);
1069 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
1070 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
1071 I.getOperand(0)->getType());
1072 if (I.isVolatile() && PrintVolatile)
1073 dbgs() << "Volatile store: " << I;
1076 //===----------------------------------------------------------------------===//
1077 // Miscellaneous Instruction Implementations
1078 //===----------------------------------------------------------------------===//
1080 void Interpreter::visitCallSite(CallSite CS) {
1081 ExecutionContext &SF = ECStack.back();
1083 // Check to see if this is an intrinsic function call...
1084 Function *F = CS.getCalledFunction();
1085 if (F && F->isDeclaration())
1086 switch (F->getIntrinsicID()) {
1087 case Intrinsic::not_intrinsic:
1089 case Intrinsic::vastart: { // va_start
1090 GenericValue ArgIndex;
1091 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1092 ArgIndex.UIntPairVal.second = 0;
1093 SetValue(CS.getInstruction(), ArgIndex, SF);
1096 case Intrinsic::vaend: // va_end is a noop for the interpreter
1098 case Intrinsic::vacopy: // va_copy: dest = src
1099 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1102 // If it is an unknown intrinsic function, use the intrinsic lowering
1103 // class to transform it into hopefully tasty LLVM code.
1105 BasicBlock::iterator me(CS.getInstruction());
1106 BasicBlock *Parent = CS.getInstruction()->getParent();
1107 bool atBegin(Parent->begin() == me);
1110 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1112 // Restore the CurInst pointer to the first instruction newly inserted, if
1115 SF.CurInst = Parent->begin();
1125 std::vector<GenericValue> ArgVals;
1126 const unsigned NumArgs = SF.Caller.arg_size();
1127 ArgVals.reserve(NumArgs);
1129 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1130 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
1132 ArgVals.push_back(getOperandValue(V, SF));
1135 // To handle indirect calls, we must get the pointer value from the argument
1136 // and treat it as a function pointer.
1137 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
1138 callFunction((Function*)GVTOP(SRC), ArgVals);
1141 void Interpreter::visitShl(BinaryOperator &I) {
1142 ExecutionContext &SF = ECStack.back();
1143 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1144 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1146 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
1147 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
1149 Dest.IntVal = Src1.IntVal;
1151 SetValue(&I, Dest, SF);
1154 void Interpreter::visitLShr(BinaryOperator &I) {
1155 ExecutionContext &SF = ECStack.back();
1156 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1157 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1159 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
1160 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
1162 Dest.IntVal = Src1.IntVal;
1164 SetValue(&I, Dest, SF);
1167 void Interpreter::visitAShr(BinaryOperator &I) {
1168 ExecutionContext &SF = ECStack.back();
1169 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1170 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1172 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
1173 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
1175 Dest.IntVal = Src1.IntVal;
1177 SetValue(&I, Dest, SF);
1180 GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy,
1181 ExecutionContext &SF) {
1182 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1183 IntegerType *DITy = cast<IntegerType>(DstTy);
1184 unsigned DBitWidth = DITy->getBitWidth();
1185 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
1189 GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy,
1190 ExecutionContext &SF) {
1191 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1192 IntegerType *DITy = cast<IntegerType>(DstTy);
1193 unsigned DBitWidth = DITy->getBitWidth();
1194 Dest.IntVal = Src.IntVal.sext(DBitWidth);
1198 GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy,
1199 ExecutionContext &SF) {
1200 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1201 IntegerType *DITy = cast<IntegerType>(DstTy);
1202 unsigned DBitWidth = DITy->getBitWidth();
1203 Dest.IntVal = Src.IntVal.zext(DBitWidth);
1207 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy,
1208 ExecutionContext &SF) {
1209 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1210 assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
1211 "Invalid FPTrunc instruction");
1212 Dest.FloatVal = (float) Src.DoubleVal;
1216 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy,
1217 ExecutionContext &SF) {
1218 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1219 assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&
1220 "Invalid FPTrunc instruction");
1221 Dest.DoubleVal = (double) Src.FloatVal;
1225 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy,
1226 ExecutionContext &SF) {
1227 Type *SrcTy = SrcVal->getType();
1228 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1229 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1230 assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction");
1232 if (SrcTy->getTypeID() == Type::FloatTyID)
1233 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1235 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1239 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy,
1240 ExecutionContext &SF) {
1241 Type *SrcTy = SrcVal->getType();
1242 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1243 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1244 assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1246 if (SrcTy->getTypeID() == Type::FloatTyID)
1247 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1249 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1253 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy,
1254 ExecutionContext &SF) {
1255 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1256 assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1258 if (DstTy->getTypeID() == Type::FloatTyID)
1259 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
1261 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
1265 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy,
1266 ExecutionContext &SF) {
1267 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1268 assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1270 if (DstTy->getTypeID() == Type::FloatTyID)
1271 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
1273 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
1278 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy,
1279 ExecutionContext &SF) {
1280 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1281 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1282 assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction");
1284 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1288 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy,
1289 ExecutionContext &SF) {
1290 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1291 assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction");
1293 uint32_t PtrSize = TD.getPointerSizeInBits();
1294 if (PtrSize != Src.IntVal.getBitWidth())
1295 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
1297 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
1301 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy,
1302 ExecutionContext &SF) {
1304 Type *SrcTy = SrcVal->getType();
1305 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1306 if (DstTy->isPointerTy()) {
1307 assert(SrcTy->isPointerTy() && "Invalid BitCast");
1308 Dest.PointerVal = Src.PointerVal;
1309 } else if (DstTy->isIntegerTy()) {
1310 if (SrcTy->isFloatTy()) {
1311 Dest.IntVal = APInt::floatToBits(Src.FloatVal);
1312 } else if (SrcTy->isDoubleTy()) {
1313 Dest.IntVal = APInt::doubleToBits(Src.DoubleVal);
1314 } else if (SrcTy->isIntegerTy()) {
1315 Dest.IntVal = Src.IntVal;
1317 llvm_unreachable("Invalid BitCast");
1318 } else if (DstTy->isFloatTy()) {
1319 if (SrcTy->isIntegerTy())
1320 Dest.FloatVal = Src.IntVal.bitsToFloat();
1322 Dest.FloatVal = Src.FloatVal;
1323 } else if (DstTy->isDoubleTy()) {
1324 if (SrcTy->isIntegerTy())
1325 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1327 Dest.DoubleVal = Src.DoubleVal;
1329 llvm_unreachable("Invalid Bitcast");
1334 void Interpreter::visitTruncInst(TruncInst &I) {
1335 ExecutionContext &SF = ECStack.back();
1336 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1339 void Interpreter::visitSExtInst(SExtInst &I) {
1340 ExecutionContext &SF = ECStack.back();
1341 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1344 void Interpreter::visitZExtInst(ZExtInst &I) {
1345 ExecutionContext &SF = ECStack.back();
1346 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1349 void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1350 ExecutionContext &SF = ECStack.back();
1351 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1354 void Interpreter::visitFPExtInst(FPExtInst &I) {
1355 ExecutionContext &SF = ECStack.back();
1356 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1359 void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1360 ExecutionContext &SF = ECStack.back();
1361 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1364 void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1365 ExecutionContext &SF = ECStack.back();
1366 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1369 void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1370 ExecutionContext &SF = ECStack.back();
1371 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1374 void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1375 ExecutionContext &SF = ECStack.back();
1376 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1379 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1380 ExecutionContext &SF = ECStack.back();
1381 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1384 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1385 ExecutionContext &SF = ECStack.back();
1386 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1389 void Interpreter::visitBitCastInst(BitCastInst &I) {
1390 ExecutionContext &SF = ECStack.back();
1391 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1394 #define IMPLEMENT_VAARG(TY) \
1395 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1397 void Interpreter::visitVAArgInst(VAArgInst &I) {
1398 ExecutionContext &SF = ECStack.back();
1400 // Get the incoming valist parameter. LLI treats the valist as a
1401 // (ec-stack-depth var-arg-index) pair.
1402 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1404 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1405 .VarArgs[VAList.UIntPairVal.second];
1406 Type *Ty = I.getType();
1407 switch (Ty->getTypeID()) {
1408 case Type::IntegerTyID:
1409 Dest.IntVal = Src.IntVal;
1411 IMPLEMENT_VAARG(Pointer);
1412 IMPLEMENT_VAARG(Float);
1413 IMPLEMENT_VAARG(Double);
1415 dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1416 llvm_unreachable(0);
1419 // Set the Value of this Instruction.
1420 SetValue(&I, Dest, SF);
1422 // Move the pointer to the next vararg.
1423 ++VAList.UIntPairVal.second;
1426 void Interpreter::visitExtractElementInst(ExtractElementInst &I) {
1427 ExecutionContext &SF = ECStack.back();
1428 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1429 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1432 Type *Ty = I.getType();
1433 const unsigned indx = unsigned(Src2.IntVal.getZExtValue());
1435 if(Src1.AggregateVal.size() > indx) {
1436 switch (Ty->getTypeID()) {
1438 dbgs() << "Unhandled destination type for extractelement instruction: "
1440 llvm_unreachable(0);
1442 case Type::IntegerTyID:
1443 Dest.IntVal = Src1.AggregateVal[indx].IntVal;
1445 case Type::FloatTyID:
1446 Dest.FloatVal = Src1.AggregateVal[indx].FloatVal;
1448 case Type::DoubleTyID:
1449 Dest.DoubleVal = Src1.AggregateVal[indx].DoubleVal;
1453 dbgs() << "Invalid index in extractelement instruction\n";
1456 SetValue(&I, Dest, SF);
1459 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1460 ExecutionContext &SF) {
1461 switch (CE->getOpcode()) {
1462 case Instruction::Trunc:
1463 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1464 case Instruction::ZExt:
1465 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1466 case Instruction::SExt:
1467 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1468 case Instruction::FPTrunc:
1469 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1470 case Instruction::FPExt:
1471 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1472 case Instruction::UIToFP:
1473 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1474 case Instruction::SIToFP:
1475 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1476 case Instruction::FPToUI:
1477 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1478 case Instruction::FPToSI:
1479 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1480 case Instruction::PtrToInt:
1481 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1482 case Instruction::IntToPtr:
1483 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1484 case Instruction::BitCast:
1485 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1486 case Instruction::GetElementPtr:
1487 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1488 gep_type_end(CE), SF);
1489 case Instruction::FCmp:
1490 case Instruction::ICmp:
1491 return executeCmpInst(CE->getPredicate(),
1492 getOperandValue(CE->getOperand(0), SF),
1493 getOperandValue(CE->getOperand(1), SF),
1494 CE->getOperand(0)->getType());
1495 case Instruction::Select:
1496 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1497 getOperandValue(CE->getOperand(1), SF),
1498 getOperandValue(CE->getOperand(2), SF));
1503 // The cases below here require a GenericValue parameter for the result
1504 // so we initialize one, compute it and then return it.
1505 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1506 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
1508 Type * Ty = CE->getOperand(0)->getType();
1509 switch (CE->getOpcode()) {
1510 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
1511 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
1512 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
1513 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
1514 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
1515 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
1516 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1517 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1518 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1519 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1520 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1521 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
1522 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
1523 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
1524 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
1525 case Instruction::Shl:
1526 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1528 case Instruction::LShr:
1529 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1531 case Instruction::AShr:
1532 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1535 dbgs() << "Unhandled ConstantExpr: " << *CE << "\n";
1536 llvm_unreachable("Unhandled ConstantExpr");
1541 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1542 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1543 return getConstantExprValue(CE, SF);
1544 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1545 return getConstantValue(CPV);
1546 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1547 return PTOGV(getPointerToGlobal(GV));
1549 return SF.Values[V];
1553 //===----------------------------------------------------------------------===//
1554 // Dispatch and Execution Code
1555 //===----------------------------------------------------------------------===//
1557 //===----------------------------------------------------------------------===//
1558 // callFunction - Execute the specified function...
1560 void Interpreter::callFunction(Function *F,
1561 const std::vector<GenericValue> &ArgVals) {
1562 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1563 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1564 "Incorrect number of arguments passed into function call!");
1565 // Make a new stack frame... and fill it in.
1566 ECStack.push_back(ExecutionContext());
1567 ExecutionContext &StackFrame = ECStack.back();
1568 StackFrame.CurFunction = F;
1570 // Special handling for external functions.
1571 if (F->isDeclaration()) {
1572 GenericValue Result = callExternalFunction (F, ArgVals);
1573 // Simulate a 'ret' instruction of the appropriate type.
1574 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1578 // Get pointers to first LLVM BB & Instruction in function.
1579 StackFrame.CurBB = F->begin();
1580 StackFrame.CurInst = StackFrame.CurBB->begin();
1582 // Run through the function arguments and initialize their values...
1583 assert((ArgVals.size() == F->arg_size() ||
1584 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1585 "Invalid number of values passed to function invocation!");
1587 // Handle non-varargs arguments...
1589 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1591 SetValue(AI, ArgVals[i], StackFrame);
1593 // Handle varargs arguments...
1594 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1598 void Interpreter::run() {
1599 while (!ECStack.empty()) {
1600 // Interpret a single instruction & increment the "PC".
1601 ExecutionContext &SF = ECStack.back(); // Current stack frame
1602 Instruction &I = *SF.CurInst++; // Increment before execute
1604 // Track the number of dynamic instructions executed.
1607 DEBUG(dbgs() << "About to interpret: " << I);
1608 visit(I); // Dispatch to one of the visit* methods...
1610 // This is not safe, as visiting the instruction could lower it and free I.
1612 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1613 I.getType() != Type::VoidTy) {
1615 const GenericValue &Val = SF.Values[&I];
1616 switch (I.getType()->getTypeID()) {
1617 default: llvm_unreachable("Invalid GenericValue Type");
1618 case Type::VoidTyID: dbgs() << "void"; break;
1619 case Type::FloatTyID: dbgs() << "float " << Val.FloatVal; break;
1620 case Type::DoubleTyID: dbgs() << "double " << Val.DoubleVal; break;
1621 case Type::PointerTyID: dbgs() << "void* " << intptr_t(Val.PointerVal);
1623 case Type::IntegerTyID:
1624 dbgs() << "i" << Val.IntVal.getBitWidth() << " "
1625 << Val.IntVal.toStringUnsigned(10)
1626 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";