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); \
343 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
344 Dest.IntVal = APInt(1,false); \
348 #define MASK_VECTOR_NANS_T(X,Y, TZ, FLAG) \
349 assert(X.AggregateVal.size() == Y.AggregateVal.size()); \
350 Dest.AggregateVal.resize( X.AggregateVal.size() ); \
351 for( uint32_t _i=0;_i<X.AggregateVal.size();_i++) { \
352 if (X.AggregateVal[_i].TZ##Val != X.AggregateVal[_i].TZ##Val || \
353 Y.AggregateVal[_i].TZ##Val != Y.AggregateVal[_i].TZ##Val) \
354 Dest.AggregateVal[_i].IntVal = APInt(1,FLAG); \
356 Dest.AggregateVal[_i].IntVal = APInt(1,!FLAG); \
360 #define MASK_VECTOR_NANS(TY, X,Y, FLAG) \
361 if (TY->isVectorTy()) \
362 if (dyn_cast<VectorType>(TY)->getElementType()->isFloatTy()) { \
363 MASK_VECTOR_NANS_T(X, Y, Float, FLAG) \
365 MASK_VECTOR_NANS_T(X, Y, Double, FLAG) \
370 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
374 // if input is scalar value and Src1 or Src2 is NaN return false
375 IMPLEMENT_SCALAR_NANS(Ty, Src1, Src2)
376 // if vector input detect NaNs and fill mask
377 MASK_VECTOR_NANS(Ty, Src1, Src2, false)
378 GenericValue DestMask = Dest;
379 switch (Ty->getTypeID()) {
380 IMPLEMENT_FCMP(!=, Float);
381 IMPLEMENT_FCMP(!=, Double);
382 IMPLEMENT_VECTOR_FCMP(!=);
384 dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
387 // in vector case mask out NaN elements
388 if (Ty->isVectorTy())
389 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
390 if (DestMask.AggregateVal[_i].IntVal == false)
391 Dest.AggregateVal[_i].IntVal = APInt(1,false);
396 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
399 switch (Ty->getTypeID()) {
400 IMPLEMENT_FCMP(<=, Float);
401 IMPLEMENT_FCMP(<=, Double);
402 IMPLEMENT_VECTOR_FCMP(<=);
404 dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
410 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
413 switch (Ty->getTypeID()) {
414 IMPLEMENT_FCMP(>=, Float);
415 IMPLEMENT_FCMP(>=, Double);
416 IMPLEMENT_VECTOR_FCMP(>=);
418 dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
424 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
427 switch (Ty->getTypeID()) {
428 IMPLEMENT_FCMP(<, Float);
429 IMPLEMENT_FCMP(<, Double);
430 IMPLEMENT_VECTOR_FCMP(<);
432 dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
438 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
441 switch (Ty->getTypeID()) {
442 IMPLEMENT_FCMP(>, Float);
443 IMPLEMENT_FCMP(>, Double);
444 IMPLEMENT_VECTOR_FCMP(>);
446 dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
452 #define IMPLEMENT_UNORDERED(TY, X,Y) \
453 if (TY->isFloatTy()) { \
454 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
455 Dest.IntVal = APInt(1,true); \
458 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
459 Dest.IntVal = APInt(1,true); \
463 #define IMPLEMENT_VECTOR_UNORDERED(TY, X,Y, _FUNC) \
464 if (TY->isVectorTy()) { \
465 GenericValue DestMask = Dest; \
466 Dest = _FUNC(Src1, Src2, Ty); \
467 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++) \
468 if (DestMask.AggregateVal[_i].IntVal == true) \
469 Dest.AggregateVal[_i].IntVal = APInt(1,true); \
473 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
476 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
477 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
478 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OEQ)
479 return executeFCMP_OEQ(Src1, Src2, Ty);
483 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
486 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
487 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
488 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_ONE)
489 return executeFCMP_ONE(Src1, Src2, Ty);
492 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
495 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
496 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
497 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLE)
498 return executeFCMP_OLE(Src1, Src2, Ty);
501 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
504 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
505 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
506 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGE)
507 return executeFCMP_OGE(Src1, Src2, Ty);
510 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
513 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
514 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
515 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLT)
516 return executeFCMP_OLT(Src1, Src2, Ty);
519 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
522 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
523 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
524 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGT)
525 return executeFCMP_OGT(Src1, Src2, Ty);
528 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
531 if(Ty->isVectorTy()) {
532 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
533 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
534 if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
535 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
536 Dest.AggregateVal[_i].IntVal = APInt(1,
537 ( (Src1.AggregateVal[_i].FloatVal ==
538 Src1.AggregateVal[_i].FloatVal) &&
539 (Src2.AggregateVal[_i].FloatVal ==
540 Src2.AggregateVal[_i].FloatVal)));
542 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
543 Dest.AggregateVal[_i].IntVal = APInt(1,
544 ( (Src1.AggregateVal[_i].DoubleVal ==
545 Src1.AggregateVal[_i].DoubleVal) &&
546 (Src2.AggregateVal[_i].DoubleVal ==
547 Src2.AggregateVal[_i].DoubleVal)));
549 } else if (Ty->isFloatTy())
550 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
551 Src2.FloatVal == Src2.FloatVal));
553 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
554 Src2.DoubleVal == Src2.DoubleVal));
559 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
562 if(Ty->isVectorTy()) {
563 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
564 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
565 if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
566 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
567 Dest.AggregateVal[_i].IntVal = APInt(1,
568 ( (Src1.AggregateVal[_i].FloatVal !=
569 Src1.AggregateVal[_i].FloatVal) ||
570 (Src2.AggregateVal[_i].FloatVal !=
571 Src2.AggregateVal[_i].FloatVal)));
573 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
574 Dest.AggregateVal[_i].IntVal = APInt(1,
575 ( (Src1.AggregateVal[_i].DoubleVal !=
576 Src1.AggregateVal[_i].DoubleVal) ||
577 (Src2.AggregateVal[_i].DoubleVal !=
578 Src2.AggregateVal[_i].DoubleVal)));
580 } else if (Ty->isFloatTy())
581 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
582 Src2.FloatVal != Src2.FloatVal));
584 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
585 Src2.DoubleVal != Src2.DoubleVal));
590 static GenericValue executeFCMP_BOOL(GenericValue Src1, GenericValue Src2,
591 const Type *Ty, const bool val) {
593 if(Ty->isVectorTy()) {
594 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
595 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
596 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
597 Dest.AggregateVal[_i].IntVal = APInt(1,val);
599 Dest.IntVal = APInt(1, val);
605 void Interpreter::visitFCmpInst(FCmpInst &I) {
606 ExecutionContext &SF = ECStack.back();
607 Type *Ty = I.getOperand(0)->getType();
608 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
609 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
610 GenericValue R; // Result
612 switch (I.getPredicate()) {
614 dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I;
617 case FCmpInst::FCMP_FALSE: R = executeFCMP_BOOL(Src1, Src2, Ty, false);
619 case FCmpInst::FCMP_TRUE: R = executeFCMP_BOOL(Src1, Src2, Ty, true);
621 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
622 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
623 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
624 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
625 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
626 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
627 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
628 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
629 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
630 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
631 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
632 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
633 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
634 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
640 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
641 GenericValue Src2, Type *Ty) {
644 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
645 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
646 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
647 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
648 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
649 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
650 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
651 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
652 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
653 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
654 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
655 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
656 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
657 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
658 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
659 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
660 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
661 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
662 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
663 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
664 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
665 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
666 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
667 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
668 case FCmpInst::FCMP_FALSE: return executeFCMP_BOOL(Src1, Src2, Ty, false);
669 case FCmpInst::FCMP_TRUE: return executeFCMP_BOOL(Src1, Src2, Ty, true);
671 dbgs() << "Unhandled Cmp predicate\n";
676 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
677 ExecutionContext &SF = ECStack.back();
678 Type *Ty = I.getOperand(0)->getType();
679 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
680 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
681 GenericValue R; // Result
683 // First process vector operation
684 if (Ty->isVectorTy()) {
685 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
686 R.AggregateVal.resize(Src1.AggregateVal.size());
688 // Macros to execute binary operation 'OP' over integer vectors
689 #define INTEGER_VECTOR_OPERATION(OP) \
690 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
691 R.AggregateVal[i].IntVal = \
692 Src1.AggregateVal[i].IntVal OP Src2.AggregateVal[i].IntVal;
694 // Additional macros to execute binary operations udiv/sdiv/urem/srem since
695 // they have different notation.
696 #define INTEGER_VECTOR_FUNCTION(OP) \
697 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
698 R.AggregateVal[i].IntVal = \
699 Src1.AggregateVal[i].IntVal.OP(Src2.AggregateVal[i].IntVal);
701 // Macros to execute binary operation 'OP' over floating point type TY
702 // (float or double) vectors
703 #define FLOAT_VECTOR_FUNCTION(OP, TY) \
704 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
705 R.AggregateVal[i].TY = \
706 Src1.AggregateVal[i].TY OP Src2.AggregateVal[i].TY;
708 // Macros to choose appropriate TY: float or double and run operation
710 #define FLOAT_VECTOR_OP(OP) { \
711 if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) \
712 FLOAT_VECTOR_FUNCTION(OP, FloatVal) \
714 if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy()) \
715 FLOAT_VECTOR_FUNCTION(OP, DoubleVal) \
717 dbgs() << "Unhandled type for OP instruction: " << *Ty << "\n"; \
718 llvm_unreachable(0); \
723 switch(I.getOpcode()){
725 dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
728 case Instruction::Add: INTEGER_VECTOR_OPERATION(+) break;
729 case Instruction::Sub: INTEGER_VECTOR_OPERATION(-) break;
730 case Instruction::Mul: INTEGER_VECTOR_OPERATION(*) break;
731 case Instruction::UDiv: INTEGER_VECTOR_FUNCTION(udiv) break;
732 case Instruction::SDiv: INTEGER_VECTOR_FUNCTION(sdiv) break;
733 case Instruction::URem: INTEGER_VECTOR_FUNCTION(urem) break;
734 case Instruction::SRem: INTEGER_VECTOR_FUNCTION(srem) break;
735 case Instruction::And: INTEGER_VECTOR_OPERATION(&) break;
736 case Instruction::Or: INTEGER_VECTOR_OPERATION(|) break;
737 case Instruction::Xor: INTEGER_VECTOR_OPERATION(^) break;
738 case Instruction::FAdd: FLOAT_VECTOR_OP(+) break;
739 case Instruction::FSub: FLOAT_VECTOR_OP(-) break;
740 case Instruction::FMul: FLOAT_VECTOR_OP(*) break;
741 case Instruction::FDiv: FLOAT_VECTOR_OP(/) break;
742 case Instruction::FRem:
743 if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy())
744 for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
745 R.AggregateVal[i].FloatVal =
746 fmod(Src1.AggregateVal[i].FloatVal, Src2.AggregateVal[i].FloatVal);
748 if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy())
749 for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
750 R.AggregateVal[i].DoubleVal =
751 fmod(Src1.AggregateVal[i].DoubleVal, Src2.AggregateVal[i].DoubleVal);
753 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
760 switch (I.getOpcode()) {
762 dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
765 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
766 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
767 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
768 case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;
769 case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;
770 case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;
771 case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;
772 case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;
773 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
774 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
775 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
776 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
777 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
778 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
779 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
785 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
787 return Src1.IntVal == 0 ? Src3 : Src2;
790 void Interpreter::visitSelectInst(SelectInst &I) {
791 ExecutionContext &SF = ECStack.back();
792 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
793 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
794 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
795 GenericValue R = executeSelectInst(Src1, Src2, Src3);
800 //===----------------------------------------------------------------------===//
801 // Terminator Instruction Implementations
802 //===----------------------------------------------------------------------===//
804 void Interpreter::exitCalled(GenericValue GV) {
805 // runAtExitHandlers() assumes there are no stack frames, but
806 // if exit() was called, then it had a stack frame. Blow away
807 // the stack before interpreting atexit handlers.
810 exit(GV.IntVal.zextOrTrunc(32).getZExtValue());
813 /// Pop the last stack frame off of ECStack and then copy the result
814 /// back into the result variable if we are not returning void. The
815 /// result variable may be the ExitValue, or the Value of the calling
816 /// CallInst if there was a previous stack frame. This method may
817 /// invalidate any ECStack iterators you have. This method also takes
818 /// care of switching to the normal destination BB, if we are returning
821 void Interpreter::popStackAndReturnValueToCaller(Type *RetTy,
822 GenericValue Result) {
823 // Pop the current stack frame.
826 if (ECStack.empty()) { // Finished main. Put result into exit code...
827 if (RetTy && !RetTy->isVoidTy()) { // Nonvoid return type?
828 ExitValue = Result; // Capture the exit value of the program
830 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
833 // If we have a previous stack frame, and we have a previous call,
834 // fill in the return value...
835 ExecutionContext &CallingSF = ECStack.back();
836 if (Instruction *I = CallingSF.Caller.getInstruction()) {
838 if (!CallingSF.Caller.getType()->isVoidTy())
839 SetValue(I, Result, CallingSF);
840 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
841 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
842 CallingSF.Caller = CallSite(); // We returned from the call...
847 void Interpreter::visitReturnInst(ReturnInst &I) {
848 ExecutionContext &SF = ECStack.back();
849 Type *RetTy = Type::getVoidTy(I.getContext());
852 // Save away the return value... (if we are not 'ret void')
853 if (I.getNumOperands()) {
854 RetTy = I.getReturnValue()->getType();
855 Result = getOperandValue(I.getReturnValue(), SF);
858 popStackAndReturnValueToCaller(RetTy, Result);
861 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
862 report_fatal_error("Program executed an 'unreachable' instruction!");
865 void Interpreter::visitBranchInst(BranchInst &I) {
866 ExecutionContext &SF = ECStack.back();
869 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
870 if (!I.isUnconditional()) {
871 Value *Cond = I.getCondition();
872 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
873 Dest = I.getSuccessor(1);
875 SwitchToNewBasicBlock(Dest, SF);
878 void Interpreter::visitSwitchInst(SwitchInst &I) {
879 ExecutionContext &SF = ECStack.back();
880 Value* Cond = I.getCondition();
881 Type *ElTy = Cond->getType();
882 GenericValue CondVal = getOperandValue(Cond, SF);
884 // Check to see if any of the cases match...
885 BasicBlock *Dest = 0;
886 for (SwitchInst::CaseIt i = I.case_begin(), e = I.case_end(); i != e; ++i) {
887 IntegersSubset& Case = i.getCaseValueEx();
888 if (Case.isSingleNumber()) {
889 // FIXME: Currently work with ConstantInt based numbers.
890 const ConstantInt *CI = Case.getSingleNumber(0).toConstantInt();
891 GenericValue Val = getOperandValue(const_cast<ConstantInt*>(CI), SF);
892 if (executeICMP_EQ(Val, CondVal, ElTy).IntVal != 0) {
893 Dest = cast<BasicBlock>(i.getCaseSuccessor());
897 if (Case.isSingleNumbersOnly()) {
898 for (unsigned n = 0, en = Case.getNumItems(); n != en; ++n) {
899 // FIXME: Currently work with ConstantInt based numbers.
900 const ConstantInt *CI = Case.getSingleNumber(n).toConstantInt();
901 GenericValue Val = getOperandValue(const_cast<ConstantInt*>(CI), SF);
902 if (executeICMP_EQ(Val, CondVal, ElTy).IntVal != 0) {
903 Dest = cast<BasicBlock>(i.getCaseSuccessor());
908 for (unsigned n = 0, en = Case.getNumItems(); n != en; ++n) {
909 IntegersSubset::Range r = Case.getItem(n);
910 // FIXME: Currently work with ConstantInt based numbers.
911 const ConstantInt *LowCI = r.getLow().toConstantInt();
912 const ConstantInt *HighCI = r.getHigh().toConstantInt();
913 GenericValue Low = getOperandValue(const_cast<ConstantInt*>(LowCI), SF);
914 GenericValue High = getOperandValue(const_cast<ConstantInt*>(HighCI), SF);
915 if (executeICMP_ULE(Low, CondVal, ElTy).IntVal != 0 &&
916 executeICMP_ULE(CondVal, High, ElTy).IntVal != 0) {
917 Dest = cast<BasicBlock>(i.getCaseSuccessor());
922 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
923 SwitchToNewBasicBlock(Dest, SF);
926 void Interpreter::visitIndirectBrInst(IndirectBrInst &I) {
927 ExecutionContext &SF = ECStack.back();
928 void *Dest = GVTOP(getOperandValue(I.getAddress(), SF));
929 SwitchToNewBasicBlock((BasicBlock*)Dest, SF);
933 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
934 // This function handles the actual updating of block and instruction iterators
935 // as well as execution of all of the PHI nodes in the destination block.
937 // This method does this because all of the PHI nodes must be executed
938 // atomically, reading their inputs before any of the results are updated. Not
939 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
940 // their inputs. If the input PHI node is updated before it is read, incorrect
941 // results can happen. Thus we use a two phase approach.
943 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
944 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
945 SF.CurBB = Dest; // Update CurBB to branch destination
946 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
948 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
950 // Loop over all of the PHI nodes in the current block, reading their inputs.
951 std::vector<GenericValue> ResultValues;
953 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
954 // Search for the value corresponding to this previous bb...
955 int i = PN->getBasicBlockIndex(PrevBB);
956 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
957 Value *IncomingValue = PN->getIncomingValue(i);
959 // Save the incoming value for this PHI node...
960 ResultValues.push_back(getOperandValue(IncomingValue, SF));
963 // Now loop over all of the PHI nodes setting their values...
964 SF.CurInst = SF.CurBB->begin();
965 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
966 PHINode *PN = cast<PHINode>(SF.CurInst);
967 SetValue(PN, ResultValues[i], SF);
971 //===----------------------------------------------------------------------===//
972 // Memory Instruction Implementations
973 //===----------------------------------------------------------------------===//
975 void Interpreter::visitAllocaInst(AllocaInst &I) {
976 ExecutionContext &SF = ECStack.back();
978 Type *Ty = I.getType()->getElementType(); // Type to be allocated
980 // Get the number of elements being allocated by the array...
981 unsigned NumElements =
982 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
984 unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
986 // Avoid malloc-ing zero bytes, use max()...
987 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
989 // Allocate enough memory to hold the type...
990 void *Memory = malloc(MemToAlloc);
992 DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
993 << NumElements << " (Total: " << MemToAlloc << ") at "
994 << uintptr_t(Memory) << '\n');
996 GenericValue Result = PTOGV(Memory);
997 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
998 SetValue(&I, Result, SF);
1000 if (I.getOpcode() == Instruction::Alloca)
1001 ECStack.back().Allocas.add(Memory);
1004 // getElementOffset - The workhorse for getelementptr.
1006 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
1007 gep_type_iterator E,
1008 ExecutionContext &SF) {
1009 assert(Ptr->getType()->isPointerTy() &&
1010 "Cannot getElementOffset of a nonpointer type!");
1014 for (; I != E; ++I) {
1015 if (StructType *STy = dyn_cast<StructType>(*I)) {
1016 const StructLayout *SLO = TD.getStructLayout(STy);
1018 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
1019 unsigned Index = unsigned(CPU->getZExtValue());
1021 Total += SLO->getElementOffset(Index);
1023 SequentialType *ST = cast<SequentialType>(*I);
1024 // Get the index number for the array... which must be long type...
1025 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
1029 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
1031 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
1033 assert(BitWidth == 64 && "Invalid index type for getelementptr");
1034 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
1036 Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
1040 GenericValue Result;
1041 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
1042 DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n");
1046 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1047 ExecutionContext &SF = ECStack.back();
1048 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
1049 gep_type_begin(I), gep_type_end(I), SF), SF);
1052 void Interpreter::visitLoadInst(LoadInst &I) {
1053 ExecutionContext &SF = ECStack.back();
1054 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
1055 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
1056 GenericValue Result;
1057 LoadValueFromMemory(Result, Ptr, I.getType());
1058 SetValue(&I, Result, SF);
1059 if (I.isVolatile() && PrintVolatile)
1060 dbgs() << "Volatile load " << I;
1063 void Interpreter::visitStoreInst(StoreInst &I) {
1064 ExecutionContext &SF = ECStack.back();
1065 GenericValue Val = getOperandValue(I.getOperand(0), SF);
1066 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
1067 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
1068 I.getOperand(0)->getType());
1069 if (I.isVolatile() && PrintVolatile)
1070 dbgs() << "Volatile store: " << I;
1073 //===----------------------------------------------------------------------===//
1074 // Miscellaneous Instruction Implementations
1075 //===----------------------------------------------------------------------===//
1077 void Interpreter::visitCallSite(CallSite CS) {
1078 ExecutionContext &SF = ECStack.back();
1080 // Check to see if this is an intrinsic function call...
1081 Function *F = CS.getCalledFunction();
1082 if (F && F->isDeclaration())
1083 switch (F->getIntrinsicID()) {
1084 case Intrinsic::not_intrinsic:
1086 case Intrinsic::vastart: { // va_start
1087 GenericValue ArgIndex;
1088 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1089 ArgIndex.UIntPairVal.second = 0;
1090 SetValue(CS.getInstruction(), ArgIndex, SF);
1093 case Intrinsic::vaend: // va_end is a noop for the interpreter
1095 case Intrinsic::vacopy: // va_copy: dest = src
1096 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1099 // If it is an unknown intrinsic function, use the intrinsic lowering
1100 // class to transform it into hopefully tasty LLVM code.
1102 BasicBlock::iterator me(CS.getInstruction());
1103 BasicBlock *Parent = CS.getInstruction()->getParent();
1104 bool atBegin(Parent->begin() == me);
1107 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1109 // Restore the CurInst pointer to the first instruction newly inserted, if
1112 SF.CurInst = Parent->begin();
1122 std::vector<GenericValue> ArgVals;
1123 const unsigned NumArgs = SF.Caller.arg_size();
1124 ArgVals.reserve(NumArgs);
1126 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1127 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
1129 ArgVals.push_back(getOperandValue(V, SF));
1132 // To handle indirect calls, we must get the pointer value from the argument
1133 // and treat it as a function pointer.
1134 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
1135 callFunction((Function*)GVTOP(SRC), ArgVals);
1138 void Interpreter::visitShl(BinaryOperator &I) {
1139 ExecutionContext &SF = ECStack.back();
1140 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1141 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1143 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
1144 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
1146 Dest.IntVal = Src1.IntVal;
1148 SetValue(&I, Dest, SF);
1151 void Interpreter::visitLShr(BinaryOperator &I) {
1152 ExecutionContext &SF = ECStack.back();
1153 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1154 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1156 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
1157 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
1159 Dest.IntVal = Src1.IntVal;
1161 SetValue(&I, Dest, SF);
1164 void Interpreter::visitAShr(BinaryOperator &I) {
1165 ExecutionContext &SF = ECStack.back();
1166 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1167 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1169 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
1170 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
1172 Dest.IntVal = Src1.IntVal;
1174 SetValue(&I, Dest, SF);
1177 GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy,
1178 ExecutionContext &SF) {
1179 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1180 IntegerType *DITy = cast<IntegerType>(DstTy);
1181 unsigned DBitWidth = DITy->getBitWidth();
1182 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
1186 GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy,
1187 ExecutionContext &SF) {
1188 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1189 IntegerType *DITy = cast<IntegerType>(DstTy);
1190 unsigned DBitWidth = DITy->getBitWidth();
1191 Dest.IntVal = Src.IntVal.sext(DBitWidth);
1195 GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy,
1196 ExecutionContext &SF) {
1197 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1198 IntegerType *DITy = cast<IntegerType>(DstTy);
1199 unsigned DBitWidth = DITy->getBitWidth();
1200 Dest.IntVal = Src.IntVal.zext(DBitWidth);
1204 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy,
1205 ExecutionContext &SF) {
1206 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1207 assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
1208 "Invalid FPTrunc instruction");
1209 Dest.FloatVal = (float) Src.DoubleVal;
1213 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy,
1214 ExecutionContext &SF) {
1215 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1216 assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&
1217 "Invalid FPTrunc instruction");
1218 Dest.DoubleVal = (double) Src.FloatVal;
1222 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy,
1223 ExecutionContext &SF) {
1224 Type *SrcTy = SrcVal->getType();
1225 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1226 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1227 assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction");
1229 if (SrcTy->getTypeID() == Type::FloatTyID)
1230 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1232 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1236 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy,
1237 ExecutionContext &SF) {
1238 Type *SrcTy = SrcVal->getType();
1239 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1240 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1241 assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1243 if (SrcTy->getTypeID() == Type::FloatTyID)
1244 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1246 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1250 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy,
1251 ExecutionContext &SF) {
1252 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1253 assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1255 if (DstTy->getTypeID() == Type::FloatTyID)
1256 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
1258 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
1262 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy,
1263 ExecutionContext &SF) {
1264 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1265 assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1267 if (DstTy->getTypeID() == Type::FloatTyID)
1268 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
1270 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
1275 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy,
1276 ExecutionContext &SF) {
1277 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1278 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1279 assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction");
1281 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1285 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy,
1286 ExecutionContext &SF) {
1287 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1288 assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction");
1290 uint32_t PtrSize = TD.getPointerSizeInBits();
1291 if (PtrSize != Src.IntVal.getBitWidth())
1292 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
1294 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
1298 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy,
1299 ExecutionContext &SF) {
1301 Type *SrcTy = SrcVal->getType();
1302 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1303 if (DstTy->isPointerTy()) {
1304 assert(SrcTy->isPointerTy() && "Invalid BitCast");
1305 Dest.PointerVal = Src.PointerVal;
1306 } else if (DstTy->isIntegerTy()) {
1307 if (SrcTy->isFloatTy()) {
1308 Dest.IntVal = APInt::floatToBits(Src.FloatVal);
1309 } else if (SrcTy->isDoubleTy()) {
1310 Dest.IntVal = APInt::doubleToBits(Src.DoubleVal);
1311 } else if (SrcTy->isIntegerTy()) {
1312 Dest.IntVal = Src.IntVal;
1314 llvm_unreachable("Invalid BitCast");
1315 } else if (DstTy->isFloatTy()) {
1316 if (SrcTy->isIntegerTy())
1317 Dest.FloatVal = Src.IntVal.bitsToFloat();
1319 Dest.FloatVal = Src.FloatVal;
1320 } else if (DstTy->isDoubleTy()) {
1321 if (SrcTy->isIntegerTy())
1322 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1324 Dest.DoubleVal = Src.DoubleVal;
1326 llvm_unreachable("Invalid Bitcast");
1331 void Interpreter::visitTruncInst(TruncInst &I) {
1332 ExecutionContext &SF = ECStack.back();
1333 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1336 void Interpreter::visitSExtInst(SExtInst &I) {
1337 ExecutionContext &SF = ECStack.back();
1338 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1341 void Interpreter::visitZExtInst(ZExtInst &I) {
1342 ExecutionContext &SF = ECStack.back();
1343 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1346 void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1347 ExecutionContext &SF = ECStack.back();
1348 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1351 void Interpreter::visitFPExtInst(FPExtInst &I) {
1352 ExecutionContext &SF = ECStack.back();
1353 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1356 void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1357 ExecutionContext &SF = ECStack.back();
1358 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1361 void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1362 ExecutionContext &SF = ECStack.back();
1363 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1366 void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1367 ExecutionContext &SF = ECStack.back();
1368 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1371 void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1372 ExecutionContext &SF = ECStack.back();
1373 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1376 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1377 ExecutionContext &SF = ECStack.back();
1378 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1381 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1382 ExecutionContext &SF = ECStack.back();
1383 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1386 void Interpreter::visitBitCastInst(BitCastInst &I) {
1387 ExecutionContext &SF = ECStack.back();
1388 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1391 #define IMPLEMENT_VAARG(TY) \
1392 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1394 void Interpreter::visitVAArgInst(VAArgInst &I) {
1395 ExecutionContext &SF = ECStack.back();
1397 // Get the incoming valist parameter. LLI treats the valist as a
1398 // (ec-stack-depth var-arg-index) pair.
1399 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1401 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1402 .VarArgs[VAList.UIntPairVal.second];
1403 Type *Ty = I.getType();
1404 switch (Ty->getTypeID()) {
1405 case Type::IntegerTyID:
1406 Dest.IntVal = Src.IntVal;
1408 IMPLEMENT_VAARG(Pointer);
1409 IMPLEMENT_VAARG(Float);
1410 IMPLEMENT_VAARG(Double);
1412 dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1413 llvm_unreachable(0);
1416 // Set the Value of this Instruction.
1417 SetValue(&I, Dest, SF);
1419 // Move the pointer to the next vararg.
1420 ++VAList.UIntPairVal.second;
1423 void Interpreter::visitExtractElementInst(ExtractElementInst &I) {
1424 ExecutionContext &SF = ECStack.back();
1425 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1426 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1429 Type *Ty = I.getType();
1430 const unsigned indx = unsigned(Src2.IntVal.getZExtValue());
1432 if(Src1.AggregateVal.size() > indx) {
1433 switch (Ty->getTypeID()) {
1435 dbgs() << "Unhandled destination type for extractelement instruction: "
1437 llvm_unreachable(0);
1439 case Type::IntegerTyID:
1440 Dest.IntVal = Src1.AggregateVal[indx].IntVal;
1442 case Type::FloatTyID:
1443 Dest.FloatVal = Src1.AggregateVal[indx].FloatVal;
1445 case Type::DoubleTyID:
1446 Dest.DoubleVal = Src1.AggregateVal[indx].DoubleVal;
1450 dbgs() << "Invalid index in extractelement instruction\n";
1453 SetValue(&I, Dest, SF);
1456 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1457 ExecutionContext &SF) {
1458 switch (CE->getOpcode()) {
1459 case Instruction::Trunc:
1460 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1461 case Instruction::ZExt:
1462 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1463 case Instruction::SExt:
1464 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1465 case Instruction::FPTrunc:
1466 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1467 case Instruction::FPExt:
1468 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1469 case Instruction::UIToFP:
1470 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1471 case Instruction::SIToFP:
1472 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1473 case Instruction::FPToUI:
1474 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1475 case Instruction::FPToSI:
1476 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1477 case Instruction::PtrToInt:
1478 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1479 case Instruction::IntToPtr:
1480 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1481 case Instruction::BitCast:
1482 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1483 case Instruction::GetElementPtr:
1484 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1485 gep_type_end(CE), SF);
1486 case Instruction::FCmp:
1487 case Instruction::ICmp:
1488 return executeCmpInst(CE->getPredicate(),
1489 getOperandValue(CE->getOperand(0), SF),
1490 getOperandValue(CE->getOperand(1), SF),
1491 CE->getOperand(0)->getType());
1492 case Instruction::Select:
1493 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1494 getOperandValue(CE->getOperand(1), SF),
1495 getOperandValue(CE->getOperand(2), SF));
1500 // The cases below here require a GenericValue parameter for the result
1501 // so we initialize one, compute it and then return it.
1502 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1503 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
1505 Type * Ty = CE->getOperand(0)->getType();
1506 switch (CE->getOpcode()) {
1507 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
1508 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
1509 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
1510 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
1511 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
1512 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
1513 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1514 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1515 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1516 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1517 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1518 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
1519 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
1520 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
1521 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
1522 case Instruction::Shl:
1523 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1525 case Instruction::LShr:
1526 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1528 case Instruction::AShr:
1529 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1532 dbgs() << "Unhandled ConstantExpr: " << *CE << "\n";
1533 llvm_unreachable("Unhandled ConstantExpr");
1538 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1539 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1540 return getConstantExprValue(CE, SF);
1541 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1542 return getConstantValue(CPV);
1543 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1544 return PTOGV(getPointerToGlobal(GV));
1546 return SF.Values[V];
1550 //===----------------------------------------------------------------------===//
1551 // Dispatch and Execution Code
1552 //===----------------------------------------------------------------------===//
1554 //===----------------------------------------------------------------------===//
1555 // callFunction - Execute the specified function...
1557 void Interpreter::callFunction(Function *F,
1558 const std::vector<GenericValue> &ArgVals) {
1559 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1560 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1561 "Incorrect number of arguments passed into function call!");
1562 // Make a new stack frame... and fill it in.
1563 ECStack.push_back(ExecutionContext());
1564 ExecutionContext &StackFrame = ECStack.back();
1565 StackFrame.CurFunction = F;
1567 // Special handling for external functions.
1568 if (F->isDeclaration()) {
1569 GenericValue Result = callExternalFunction (F, ArgVals);
1570 // Simulate a 'ret' instruction of the appropriate type.
1571 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1575 // Get pointers to first LLVM BB & Instruction in function.
1576 StackFrame.CurBB = F->begin();
1577 StackFrame.CurInst = StackFrame.CurBB->begin();
1579 // Run through the function arguments and initialize their values...
1580 assert((ArgVals.size() == F->arg_size() ||
1581 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1582 "Invalid number of values passed to function invocation!");
1584 // Handle non-varargs arguments...
1586 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1588 SetValue(AI, ArgVals[i], StackFrame);
1590 // Handle varargs arguments...
1591 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1595 void Interpreter::run() {
1596 while (!ECStack.empty()) {
1597 // Interpret a single instruction & increment the "PC".
1598 ExecutionContext &SF = ECStack.back(); // Current stack frame
1599 Instruction &I = *SF.CurInst++; // Increment before execute
1601 // Track the number of dynamic instructions executed.
1604 DEBUG(dbgs() << "About to interpret: " << I);
1605 visit(I); // Dispatch to one of the visit* methods...
1607 // This is not safe, as visiting the instruction could lower it and free I.
1609 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1610 I.getType() != Type::VoidTy) {
1612 const GenericValue &Val = SF.Values[&I];
1613 switch (I.getType()->getTypeID()) {
1614 default: llvm_unreachable("Invalid GenericValue Type");
1615 case Type::VoidTyID: dbgs() << "void"; break;
1616 case Type::FloatTyID: dbgs() << "float " << Val.FloatVal; break;
1617 case Type::DoubleTyID: dbgs() << "double " << Val.DoubleVal; break;
1618 case Type::PointerTyID: dbgs() << "void* " << intptr_t(Val.PointerVal);
1620 case Type::IntegerTyID:
1621 dbgs() << "i" << Val.IntVal.getBitWidth() << " "
1622 << Val.IntVal.toStringUnsigned(10)
1623 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";