1f4a2713aSLionel Sambuc //===-- Execution.cpp - Implement code to simulate the program ------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc //  This file contains the actual instruction interpreter.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "Interpreter.h"
15f4a2713aSLionel Sambuc #include "llvm/ADT/APInt.h"
16f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h"
17f4a2713aSLionel Sambuc #include "llvm/CodeGen/IntrinsicLowering.h"
18f4a2713aSLionel Sambuc #include "llvm/IR/Constants.h"
19f4a2713aSLionel Sambuc #include "llvm/IR/DerivedTypes.h"
20*0a6a1f1dSLionel Sambuc #include "llvm/IR/GetElementPtrTypeIterator.h"
21f4a2713aSLionel Sambuc #include "llvm/IR/Instructions.h"
22f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h"
23f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
24f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
25f4a2713aSLionel Sambuc #include "llvm/Support/MathExtras.h"
26f4a2713aSLionel Sambuc #include <algorithm>
27f4a2713aSLionel Sambuc #include <cmath>
28f4a2713aSLionel Sambuc using namespace llvm;
29f4a2713aSLionel Sambuc 
30*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "interpreter"
31*0a6a1f1dSLionel Sambuc 
32f4a2713aSLionel Sambuc STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
33f4a2713aSLionel Sambuc 
34f4a2713aSLionel Sambuc static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
35f4a2713aSLionel Sambuc           cl::desc("make the interpreter print every volatile load and store"));
36f4a2713aSLionel Sambuc 
37f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
38f4a2713aSLionel Sambuc //                     Various Helper Functions
39f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
40f4a2713aSLionel Sambuc 
SetValue(Value * V,GenericValue Val,ExecutionContext & SF)41f4a2713aSLionel Sambuc static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
42f4a2713aSLionel Sambuc   SF.Values[V] = Val;
43f4a2713aSLionel Sambuc }
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
46f4a2713aSLionel Sambuc //                    Binary Instruction Implementations
47f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
50f4a2713aSLionel Sambuc    case Type::TY##TyID: \
51f4a2713aSLionel Sambuc      Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
52f4a2713aSLionel Sambuc      break
53f4a2713aSLionel Sambuc 
executeFAddInst(GenericValue & Dest,GenericValue Src1,GenericValue Src2,Type * Ty)54f4a2713aSLionel Sambuc static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
55f4a2713aSLionel Sambuc                             GenericValue Src2, Type *Ty) {
56f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
57f4a2713aSLionel Sambuc     IMPLEMENT_BINARY_OPERATOR(+, Float);
58f4a2713aSLionel Sambuc     IMPLEMENT_BINARY_OPERATOR(+, Double);
59f4a2713aSLionel Sambuc   default:
60f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n";
61*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
62f4a2713aSLionel Sambuc   }
63f4a2713aSLionel Sambuc }
64f4a2713aSLionel Sambuc 
executeFSubInst(GenericValue & Dest,GenericValue Src1,GenericValue Src2,Type * Ty)65f4a2713aSLionel Sambuc static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
66f4a2713aSLionel Sambuc                             GenericValue Src2, Type *Ty) {
67f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
68f4a2713aSLionel Sambuc     IMPLEMENT_BINARY_OPERATOR(-, Float);
69f4a2713aSLionel Sambuc     IMPLEMENT_BINARY_OPERATOR(-, Double);
70f4a2713aSLionel Sambuc   default:
71f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n";
72*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
73f4a2713aSLionel Sambuc   }
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc 
executeFMulInst(GenericValue & Dest,GenericValue Src1,GenericValue Src2,Type * Ty)76f4a2713aSLionel Sambuc static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
77f4a2713aSLionel Sambuc                             GenericValue Src2, Type *Ty) {
78f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
79f4a2713aSLionel Sambuc     IMPLEMENT_BINARY_OPERATOR(*, Float);
80f4a2713aSLionel Sambuc     IMPLEMENT_BINARY_OPERATOR(*, Double);
81f4a2713aSLionel Sambuc   default:
82f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n";
83*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
84f4a2713aSLionel Sambuc   }
85f4a2713aSLionel Sambuc }
86f4a2713aSLionel Sambuc 
executeFDivInst(GenericValue & Dest,GenericValue Src1,GenericValue Src2,Type * Ty)87f4a2713aSLionel Sambuc static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
88f4a2713aSLionel Sambuc                             GenericValue Src2, Type *Ty) {
89f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
90f4a2713aSLionel Sambuc     IMPLEMENT_BINARY_OPERATOR(/, Float);
91f4a2713aSLionel Sambuc     IMPLEMENT_BINARY_OPERATOR(/, Double);
92f4a2713aSLionel Sambuc   default:
93f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n";
94*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
95f4a2713aSLionel Sambuc   }
96f4a2713aSLionel Sambuc }
97f4a2713aSLionel Sambuc 
executeFRemInst(GenericValue & Dest,GenericValue Src1,GenericValue Src2,Type * Ty)98f4a2713aSLionel Sambuc static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
99f4a2713aSLionel Sambuc                             GenericValue Src2, Type *Ty) {
100f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
101f4a2713aSLionel Sambuc   case Type::FloatTyID:
102f4a2713aSLionel Sambuc     Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
103f4a2713aSLionel Sambuc     break;
104f4a2713aSLionel Sambuc   case Type::DoubleTyID:
105f4a2713aSLionel Sambuc     Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
106f4a2713aSLionel Sambuc     break;
107f4a2713aSLionel Sambuc   default:
108f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
109*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
110f4a2713aSLionel Sambuc   }
111f4a2713aSLionel Sambuc }
112f4a2713aSLionel Sambuc 
113f4a2713aSLionel Sambuc #define IMPLEMENT_INTEGER_ICMP(OP, TY) \
114f4a2713aSLionel Sambuc    case Type::IntegerTyID:  \
115f4a2713aSLionel Sambuc       Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
116f4a2713aSLionel Sambuc       break;
117f4a2713aSLionel Sambuc 
118f4a2713aSLionel Sambuc #define IMPLEMENT_VECTOR_INTEGER_ICMP(OP, TY)                        \
119f4a2713aSLionel Sambuc   case Type::VectorTyID: {                                           \
120f4a2713aSLionel Sambuc     assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());    \
121f4a2713aSLionel Sambuc     Dest.AggregateVal.resize( Src1.AggregateVal.size() );            \
122f4a2713aSLionel Sambuc     for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++)             \
123f4a2713aSLionel Sambuc       Dest.AggregateVal[_i].IntVal = APInt(1,                        \
124f4a2713aSLionel Sambuc       Src1.AggregateVal[_i].IntVal.OP(Src2.AggregateVal[_i].IntVal));\
125f4a2713aSLionel Sambuc   } break;
126f4a2713aSLionel Sambuc 
127f4a2713aSLionel Sambuc // Handle pointers specially because they must be compared with only as much
128f4a2713aSLionel Sambuc // width as the host has.  We _do not_ want to be comparing 64 bit values when
129f4a2713aSLionel Sambuc // running on a 32-bit target, otherwise the upper 32 bits might mess up
130f4a2713aSLionel Sambuc // comparisons if they contain garbage.
131f4a2713aSLionel Sambuc #define IMPLEMENT_POINTER_ICMP(OP) \
132f4a2713aSLionel Sambuc    case Type::PointerTyID: \
133f4a2713aSLionel Sambuc       Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
134f4a2713aSLionel Sambuc                             (void*)(intptr_t)Src2.PointerVal); \
135f4a2713aSLionel Sambuc       break;
136f4a2713aSLionel Sambuc 
executeICMP_EQ(GenericValue Src1,GenericValue Src2,Type * Ty)137f4a2713aSLionel Sambuc static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
138f4a2713aSLionel Sambuc                                    Type *Ty) {
139f4a2713aSLionel Sambuc   GenericValue Dest;
140f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
141f4a2713aSLionel Sambuc     IMPLEMENT_INTEGER_ICMP(eq,Ty);
142f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_INTEGER_ICMP(eq,Ty);
143f4a2713aSLionel Sambuc     IMPLEMENT_POINTER_ICMP(==);
144f4a2713aSLionel Sambuc   default:
145f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
146*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
147f4a2713aSLionel Sambuc   }
148f4a2713aSLionel Sambuc   return Dest;
149f4a2713aSLionel Sambuc }
150f4a2713aSLionel Sambuc 
executeICMP_NE(GenericValue Src1,GenericValue Src2,Type * Ty)151f4a2713aSLionel Sambuc static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
152f4a2713aSLionel Sambuc                                    Type *Ty) {
153f4a2713aSLionel Sambuc   GenericValue Dest;
154f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
155f4a2713aSLionel Sambuc     IMPLEMENT_INTEGER_ICMP(ne,Ty);
156f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_INTEGER_ICMP(ne,Ty);
157f4a2713aSLionel Sambuc     IMPLEMENT_POINTER_ICMP(!=);
158f4a2713aSLionel Sambuc   default:
159f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
160*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
161f4a2713aSLionel Sambuc   }
162f4a2713aSLionel Sambuc   return Dest;
163f4a2713aSLionel Sambuc }
164f4a2713aSLionel Sambuc 
executeICMP_ULT(GenericValue Src1,GenericValue Src2,Type * Ty)165f4a2713aSLionel Sambuc static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
166f4a2713aSLionel Sambuc                                     Type *Ty) {
167f4a2713aSLionel Sambuc   GenericValue Dest;
168f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
169f4a2713aSLionel Sambuc     IMPLEMENT_INTEGER_ICMP(ult,Ty);
170f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_INTEGER_ICMP(ult,Ty);
171f4a2713aSLionel Sambuc     IMPLEMENT_POINTER_ICMP(<);
172f4a2713aSLionel Sambuc   default:
173f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
174*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
175f4a2713aSLionel Sambuc   }
176f4a2713aSLionel Sambuc   return Dest;
177f4a2713aSLionel Sambuc }
178f4a2713aSLionel Sambuc 
executeICMP_SLT(GenericValue Src1,GenericValue Src2,Type * Ty)179f4a2713aSLionel Sambuc static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
180f4a2713aSLionel Sambuc                                     Type *Ty) {
181f4a2713aSLionel Sambuc   GenericValue Dest;
182f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
183f4a2713aSLionel Sambuc     IMPLEMENT_INTEGER_ICMP(slt,Ty);
184f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_INTEGER_ICMP(slt,Ty);
185f4a2713aSLionel Sambuc     IMPLEMENT_POINTER_ICMP(<);
186f4a2713aSLionel Sambuc   default:
187f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
188*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
189f4a2713aSLionel Sambuc   }
190f4a2713aSLionel Sambuc   return Dest;
191f4a2713aSLionel Sambuc }
192f4a2713aSLionel Sambuc 
executeICMP_UGT(GenericValue Src1,GenericValue Src2,Type * Ty)193f4a2713aSLionel Sambuc static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
194f4a2713aSLionel Sambuc                                     Type *Ty) {
195f4a2713aSLionel Sambuc   GenericValue Dest;
196f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
197f4a2713aSLionel Sambuc     IMPLEMENT_INTEGER_ICMP(ugt,Ty);
198f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_INTEGER_ICMP(ugt,Ty);
199f4a2713aSLionel Sambuc     IMPLEMENT_POINTER_ICMP(>);
200f4a2713aSLionel Sambuc   default:
201f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
202*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
203f4a2713aSLionel Sambuc   }
204f4a2713aSLionel Sambuc   return Dest;
205f4a2713aSLionel Sambuc }
206f4a2713aSLionel Sambuc 
executeICMP_SGT(GenericValue Src1,GenericValue Src2,Type * Ty)207f4a2713aSLionel Sambuc static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
208f4a2713aSLionel Sambuc                                     Type *Ty) {
209f4a2713aSLionel Sambuc   GenericValue Dest;
210f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
211f4a2713aSLionel Sambuc     IMPLEMENT_INTEGER_ICMP(sgt,Ty);
212f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_INTEGER_ICMP(sgt,Ty);
213f4a2713aSLionel Sambuc     IMPLEMENT_POINTER_ICMP(>);
214f4a2713aSLionel Sambuc   default:
215f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
216*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
217f4a2713aSLionel Sambuc   }
218f4a2713aSLionel Sambuc   return Dest;
219f4a2713aSLionel Sambuc }
220f4a2713aSLionel Sambuc 
executeICMP_ULE(GenericValue Src1,GenericValue Src2,Type * Ty)221f4a2713aSLionel Sambuc static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
222f4a2713aSLionel Sambuc                                     Type *Ty) {
223f4a2713aSLionel Sambuc   GenericValue Dest;
224f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
225f4a2713aSLionel Sambuc     IMPLEMENT_INTEGER_ICMP(ule,Ty);
226f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_INTEGER_ICMP(ule,Ty);
227f4a2713aSLionel Sambuc     IMPLEMENT_POINTER_ICMP(<=);
228f4a2713aSLionel Sambuc   default:
229f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
230*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
231f4a2713aSLionel Sambuc   }
232f4a2713aSLionel Sambuc   return Dest;
233f4a2713aSLionel Sambuc }
234f4a2713aSLionel Sambuc 
executeICMP_SLE(GenericValue Src1,GenericValue Src2,Type * Ty)235f4a2713aSLionel Sambuc static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
236f4a2713aSLionel Sambuc                                     Type *Ty) {
237f4a2713aSLionel Sambuc   GenericValue Dest;
238f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
239f4a2713aSLionel Sambuc     IMPLEMENT_INTEGER_ICMP(sle,Ty);
240f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_INTEGER_ICMP(sle,Ty);
241f4a2713aSLionel Sambuc     IMPLEMENT_POINTER_ICMP(<=);
242f4a2713aSLionel Sambuc   default:
243f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
244*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
245f4a2713aSLionel Sambuc   }
246f4a2713aSLionel Sambuc   return Dest;
247f4a2713aSLionel Sambuc }
248f4a2713aSLionel Sambuc 
executeICMP_UGE(GenericValue Src1,GenericValue Src2,Type * Ty)249f4a2713aSLionel Sambuc static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
250f4a2713aSLionel Sambuc                                     Type *Ty) {
251f4a2713aSLionel Sambuc   GenericValue Dest;
252f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
253f4a2713aSLionel Sambuc     IMPLEMENT_INTEGER_ICMP(uge,Ty);
254f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_INTEGER_ICMP(uge,Ty);
255f4a2713aSLionel Sambuc     IMPLEMENT_POINTER_ICMP(>=);
256f4a2713aSLionel Sambuc   default:
257f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
258*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
259f4a2713aSLionel Sambuc   }
260f4a2713aSLionel Sambuc   return Dest;
261f4a2713aSLionel Sambuc }
262f4a2713aSLionel Sambuc 
executeICMP_SGE(GenericValue Src1,GenericValue Src2,Type * Ty)263f4a2713aSLionel Sambuc static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
264f4a2713aSLionel Sambuc                                     Type *Ty) {
265f4a2713aSLionel Sambuc   GenericValue Dest;
266f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
267f4a2713aSLionel Sambuc     IMPLEMENT_INTEGER_ICMP(sge,Ty);
268f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_INTEGER_ICMP(sge,Ty);
269f4a2713aSLionel Sambuc     IMPLEMENT_POINTER_ICMP(>=);
270f4a2713aSLionel Sambuc   default:
271f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
272*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
273f4a2713aSLionel Sambuc   }
274f4a2713aSLionel Sambuc   return Dest;
275f4a2713aSLionel Sambuc }
276f4a2713aSLionel Sambuc 
visitICmpInst(ICmpInst & I)277f4a2713aSLionel Sambuc void Interpreter::visitICmpInst(ICmpInst &I) {
278f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
279f4a2713aSLionel Sambuc   Type *Ty    = I.getOperand(0)->getType();
280f4a2713aSLionel Sambuc   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
281f4a2713aSLionel Sambuc   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
282f4a2713aSLionel Sambuc   GenericValue R;   // Result
283f4a2713aSLionel Sambuc 
284f4a2713aSLionel Sambuc   switch (I.getPredicate()) {
285f4a2713aSLionel Sambuc   case ICmpInst::ICMP_EQ:  R = executeICMP_EQ(Src1,  Src2, Ty); break;
286f4a2713aSLionel Sambuc   case ICmpInst::ICMP_NE:  R = executeICMP_NE(Src1,  Src2, Ty); break;
287f4a2713aSLionel Sambuc   case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
288f4a2713aSLionel Sambuc   case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
289f4a2713aSLionel Sambuc   case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
290f4a2713aSLionel Sambuc   case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
291f4a2713aSLionel Sambuc   case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
292f4a2713aSLionel Sambuc   case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
293f4a2713aSLionel Sambuc   case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
294f4a2713aSLionel Sambuc   case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
295f4a2713aSLionel Sambuc   default:
296f4a2713aSLionel Sambuc     dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I;
297*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
298f4a2713aSLionel Sambuc   }
299f4a2713aSLionel Sambuc 
300f4a2713aSLionel Sambuc   SetValue(&I, R, SF);
301f4a2713aSLionel Sambuc }
302f4a2713aSLionel Sambuc 
303f4a2713aSLionel Sambuc #define IMPLEMENT_FCMP(OP, TY) \
304f4a2713aSLionel Sambuc    case Type::TY##TyID: \
305f4a2713aSLionel Sambuc      Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
306f4a2713aSLionel Sambuc      break
307f4a2713aSLionel Sambuc 
308f4a2713aSLionel Sambuc #define IMPLEMENT_VECTOR_FCMP_T(OP, TY)                             \
309f4a2713aSLionel Sambuc   assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());     \
310f4a2713aSLionel Sambuc   Dest.AggregateVal.resize( Src1.AggregateVal.size() );             \
311f4a2713aSLionel Sambuc   for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++)              \
312f4a2713aSLionel Sambuc     Dest.AggregateVal[_i].IntVal = APInt(1,                         \
313f4a2713aSLionel Sambuc     Src1.AggregateVal[_i].TY##Val OP Src2.AggregateVal[_i].TY##Val);\
314f4a2713aSLionel Sambuc   break;
315f4a2713aSLionel Sambuc 
316f4a2713aSLionel Sambuc #define IMPLEMENT_VECTOR_FCMP(OP)                                   \
317f4a2713aSLionel Sambuc   case Type::VectorTyID:                                            \
318f4a2713aSLionel Sambuc     if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) {   \
319f4a2713aSLionel Sambuc       IMPLEMENT_VECTOR_FCMP_T(OP, Float);                           \
320f4a2713aSLionel Sambuc     } else {                                                        \
321f4a2713aSLionel Sambuc         IMPLEMENT_VECTOR_FCMP_T(OP, Double);                        \
322f4a2713aSLionel Sambuc     }
323f4a2713aSLionel Sambuc 
executeFCMP_OEQ(GenericValue Src1,GenericValue Src2,Type * Ty)324f4a2713aSLionel Sambuc static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
325f4a2713aSLionel Sambuc                                    Type *Ty) {
326f4a2713aSLionel Sambuc   GenericValue Dest;
327f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
328f4a2713aSLionel Sambuc     IMPLEMENT_FCMP(==, Float);
329f4a2713aSLionel Sambuc     IMPLEMENT_FCMP(==, Double);
330f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_FCMP(==);
331f4a2713aSLionel Sambuc   default:
332f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
333*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
334f4a2713aSLionel Sambuc   }
335f4a2713aSLionel Sambuc   return Dest;
336f4a2713aSLionel Sambuc }
337f4a2713aSLionel Sambuc 
338f4a2713aSLionel Sambuc #define IMPLEMENT_SCALAR_NANS(TY, X,Y)                                      \
339f4a2713aSLionel Sambuc   if (TY->isFloatTy()) {                                                    \
340f4a2713aSLionel Sambuc     if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) {             \
341f4a2713aSLionel Sambuc       Dest.IntVal = APInt(1,false);                                         \
342f4a2713aSLionel Sambuc       return Dest;                                                          \
343f4a2713aSLionel Sambuc     }                                                                       \
344f4a2713aSLionel Sambuc   } else {                                                                  \
345f4a2713aSLionel Sambuc     if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) {         \
346f4a2713aSLionel Sambuc       Dest.IntVal = APInt(1,false);                                         \
347f4a2713aSLionel Sambuc       return Dest;                                                          \
348f4a2713aSLionel Sambuc     }                                                                       \
349f4a2713aSLionel Sambuc   }
350f4a2713aSLionel Sambuc 
351f4a2713aSLionel Sambuc #define MASK_VECTOR_NANS_T(X,Y, TZ, FLAG)                                   \
352f4a2713aSLionel Sambuc   assert(X.AggregateVal.size() == Y.AggregateVal.size());                   \
353f4a2713aSLionel Sambuc   Dest.AggregateVal.resize( X.AggregateVal.size() );                        \
354f4a2713aSLionel Sambuc   for( uint32_t _i=0;_i<X.AggregateVal.size();_i++) {                       \
355f4a2713aSLionel Sambuc     if (X.AggregateVal[_i].TZ##Val != X.AggregateVal[_i].TZ##Val ||         \
356f4a2713aSLionel Sambuc         Y.AggregateVal[_i].TZ##Val != Y.AggregateVal[_i].TZ##Val)           \
357f4a2713aSLionel Sambuc       Dest.AggregateVal[_i].IntVal = APInt(1,FLAG);                         \
358f4a2713aSLionel Sambuc     else  {                                                                 \
359f4a2713aSLionel Sambuc       Dest.AggregateVal[_i].IntVal = APInt(1,!FLAG);                        \
360f4a2713aSLionel Sambuc     }                                                                       \
361f4a2713aSLionel Sambuc   }
362f4a2713aSLionel Sambuc 
363f4a2713aSLionel Sambuc #define MASK_VECTOR_NANS(TY, X,Y, FLAG)                                     \
364f4a2713aSLionel Sambuc   if (TY->isVectorTy()) {                                                   \
365f4a2713aSLionel Sambuc     if (dyn_cast<VectorType>(TY)->getElementType()->isFloatTy()) {          \
366f4a2713aSLionel Sambuc       MASK_VECTOR_NANS_T(X, Y, Float, FLAG)                                 \
367f4a2713aSLionel Sambuc     } else {                                                                \
368f4a2713aSLionel Sambuc       MASK_VECTOR_NANS_T(X, Y, Double, FLAG)                                \
369f4a2713aSLionel Sambuc     }                                                                       \
370f4a2713aSLionel Sambuc   }                                                                         \
371f4a2713aSLionel Sambuc 
372f4a2713aSLionel Sambuc 
373f4a2713aSLionel Sambuc 
executeFCMP_ONE(GenericValue Src1,GenericValue Src2,Type * Ty)374f4a2713aSLionel Sambuc static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
375f4a2713aSLionel Sambuc                                     Type *Ty)
376f4a2713aSLionel Sambuc {
377f4a2713aSLionel Sambuc   GenericValue Dest;
378f4a2713aSLionel Sambuc   // if input is scalar value and Src1 or Src2 is NaN return false
379f4a2713aSLionel Sambuc   IMPLEMENT_SCALAR_NANS(Ty, Src1, Src2)
380f4a2713aSLionel Sambuc   // if vector input detect NaNs and fill mask
381f4a2713aSLionel Sambuc   MASK_VECTOR_NANS(Ty, Src1, Src2, false)
382f4a2713aSLionel Sambuc   GenericValue DestMask = Dest;
383f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
384f4a2713aSLionel Sambuc     IMPLEMENT_FCMP(!=, Float);
385f4a2713aSLionel Sambuc     IMPLEMENT_FCMP(!=, Double);
386f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_FCMP(!=);
387f4a2713aSLionel Sambuc     default:
388f4a2713aSLionel Sambuc       dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
389*0a6a1f1dSLionel Sambuc       llvm_unreachable(nullptr);
390f4a2713aSLionel Sambuc   }
391f4a2713aSLionel Sambuc   // in vector case mask out NaN elements
392f4a2713aSLionel Sambuc   if (Ty->isVectorTy())
393f4a2713aSLionel Sambuc     for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
394f4a2713aSLionel Sambuc       if (DestMask.AggregateVal[_i].IntVal == false)
395f4a2713aSLionel Sambuc         Dest.AggregateVal[_i].IntVal = APInt(1,false);
396f4a2713aSLionel Sambuc 
397f4a2713aSLionel Sambuc   return Dest;
398f4a2713aSLionel Sambuc }
399f4a2713aSLionel Sambuc 
executeFCMP_OLE(GenericValue Src1,GenericValue Src2,Type * Ty)400f4a2713aSLionel Sambuc static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
401f4a2713aSLionel Sambuc                                    Type *Ty) {
402f4a2713aSLionel Sambuc   GenericValue Dest;
403f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
404f4a2713aSLionel Sambuc     IMPLEMENT_FCMP(<=, Float);
405f4a2713aSLionel Sambuc     IMPLEMENT_FCMP(<=, Double);
406f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_FCMP(<=);
407f4a2713aSLionel Sambuc   default:
408f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
409*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
410f4a2713aSLionel Sambuc   }
411f4a2713aSLionel Sambuc   return Dest;
412f4a2713aSLionel Sambuc }
413f4a2713aSLionel Sambuc 
executeFCMP_OGE(GenericValue Src1,GenericValue Src2,Type * Ty)414f4a2713aSLionel Sambuc static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
415f4a2713aSLionel Sambuc                                    Type *Ty) {
416f4a2713aSLionel Sambuc   GenericValue Dest;
417f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
418f4a2713aSLionel Sambuc     IMPLEMENT_FCMP(>=, Float);
419f4a2713aSLionel Sambuc     IMPLEMENT_FCMP(>=, Double);
420f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_FCMP(>=);
421f4a2713aSLionel Sambuc   default:
422f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
423*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
424f4a2713aSLionel Sambuc   }
425f4a2713aSLionel Sambuc   return Dest;
426f4a2713aSLionel Sambuc }
427f4a2713aSLionel Sambuc 
executeFCMP_OLT(GenericValue Src1,GenericValue Src2,Type * Ty)428f4a2713aSLionel Sambuc static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
429f4a2713aSLionel Sambuc                                    Type *Ty) {
430f4a2713aSLionel Sambuc   GenericValue Dest;
431f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
432f4a2713aSLionel Sambuc     IMPLEMENT_FCMP(<, Float);
433f4a2713aSLionel Sambuc     IMPLEMENT_FCMP(<, Double);
434f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_FCMP(<);
435f4a2713aSLionel Sambuc   default:
436f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
437*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
438f4a2713aSLionel Sambuc   }
439f4a2713aSLionel Sambuc   return Dest;
440f4a2713aSLionel Sambuc }
441f4a2713aSLionel Sambuc 
executeFCMP_OGT(GenericValue Src1,GenericValue Src2,Type * Ty)442f4a2713aSLionel Sambuc static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
443f4a2713aSLionel Sambuc                                      Type *Ty) {
444f4a2713aSLionel Sambuc   GenericValue Dest;
445f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
446f4a2713aSLionel Sambuc     IMPLEMENT_FCMP(>, Float);
447f4a2713aSLionel Sambuc     IMPLEMENT_FCMP(>, Double);
448f4a2713aSLionel Sambuc     IMPLEMENT_VECTOR_FCMP(>);
449f4a2713aSLionel Sambuc   default:
450f4a2713aSLionel Sambuc     dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
451*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
452f4a2713aSLionel Sambuc   }
453f4a2713aSLionel Sambuc   return Dest;
454f4a2713aSLionel Sambuc }
455f4a2713aSLionel Sambuc 
456f4a2713aSLionel Sambuc #define IMPLEMENT_UNORDERED(TY, X,Y)                                     \
457f4a2713aSLionel Sambuc   if (TY->isFloatTy()) {                                                 \
458f4a2713aSLionel Sambuc     if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) {          \
459f4a2713aSLionel Sambuc       Dest.IntVal = APInt(1,true);                                       \
460f4a2713aSLionel Sambuc       return Dest;                                                       \
461f4a2713aSLionel Sambuc     }                                                                    \
462f4a2713aSLionel Sambuc   } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
463f4a2713aSLionel Sambuc     Dest.IntVal = APInt(1,true);                                         \
464f4a2713aSLionel Sambuc     return Dest;                                                         \
465f4a2713aSLionel Sambuc   }
466f4a2713aSLionel Sambuc 
467f4a2713aSLionel Sambuc #define IMPLEMENT_VECTOR_UNORDERED(TY, X,Y, _FUNC)                       \
468f4a2713aSLionel Sambuc   if (TY->isVectorTy()) {                                                \
469f4a2713aSLionel Sambuc     GenericValue DestMask = Dest;                                        \
470f4a2713aSLionel Sambuc     Dest = _FUNC(Src1, Src2, Ty);                                        \
471f4a2713aSLionel Sambuc       for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)               \
472f4a2713aSLionel Sambuc         if (DestMask.AggregateVal[_i].IntVal == true)                    \
473f4a2713aSLionel Sambuc           Dest.AggregateVal[_i].IntVal = APInt(1,true);                  \
474f4a2713aSLionel Sambuc       return Dest;                                                       \
475f4a2713aSLionel Sambuc   }
476f4a2713aSLionel Sambuc 
executeFCMP_UEQ(GenericValue Src1,GenericValue Src2,Type * Ty)477f4a2713aSLionel Sambuc static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
478f4a2713aSLionel Sambuc                                    Type *Ty) {
479f4a2713aSLionel Sambuc   GenericValue Dest;
480f4a2713aSLionel Sambuc   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
481f4a2713aSLionel Sambuc   MASK_VECTOR_NANS(Ty, Src1, Src2, true)
482f4a2713aSLionel Sambuc   IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OEQ)
483f4a2713aSLionel Sambuc   return executeFCMP_OEQ(Src1, Src2, Ty);
484f4a2713aSLionel Sambuc 
485f4a2713aSLionel Sambuc }
486f4a2713aSLionel Sambuc 
executeFCMP_UNE(GenericValue Src1,GenericValue Src2,Type * Ty)487f4a2713aSLionel Sambuc static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
488f4a2713aSLionel Sambuc                                    Type *Ty) {
489f4a2713aSLionel Sambuc   GenericValue Dest;
490f4a2713aSLionel Sambuc   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
491f4a2713aSLionel Sambuc   MASK_VECTOR_NANS(Ty, Src1, Src2, true)
492f4a2713aSLionel Sambuc   IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_ONE)
493f4a2713aSLionel Sambuc   return executeFCMP_ONE(Src1, Src2, Ty);
494f4a2713aSLionel Sambuc }
495f4a2713aSLionel Sambuc 
executeFCMP_ULE(GenericValue Src1,GenericValue Src2,Type * Ty)496f4a2713aSLionel Sambuc static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
497f4a2713aSLionel Sambuc                                    Type *Ty) {
498f4a2713aSLionel Sambuc   GenericValue Dest;
499f4a2713aSLionel Sambuc   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
500f4a2713aSLionel Sambuc   MASK_VECTOR_NANS(Ty, Src1, Src2, true)
501f4a2713aSLionel Sambuc   IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLE)
502f4a2713aSLionel Sambuc   return executeFCMP_OLE(Src1, Src2, Ty);
503f4a2713aSLionel Sambuc }
504f4a2713aSLionel Sambuc 
executeFCMP_UGE(GenericValue Src1,GenericValue Src2,Type * Ty)505f4a2713aSLionel Sambuc static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
506f4a2713aSLionel Sambuc                                    Type *Ty) {
507f4a2713aSLionel Sambuc   GenericValue Dest;
508f4a2713aSLionel Sambuc   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
509f4a2713aSLionel Sambuc   MASK_VECTOR_NANS(Ty, Src1, Src2, true)
510f4a2713aSLionel Sambuc   IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGE)
511f4a2713aSLionel Sambuc   return executeFCMP_OGE(Src1, Src2, Ty);
512f4a2713aSLionel Sambuc }
513f4a2713aSLionel Sambuc 
executeFCMP_ULT(GenericValue Src1,GenericValue Src2,Type * Ty)514f4a2713aSLionel Sambuc static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
515f4a2713aSLionel Sambuc                                    Type *Ty) {
516f4a2713aSLionel Sambuc   GenericValue Dest;
517f4a2713aSLionel Sambuc   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
518f4a2713aSLionel Sambuc   MASK_VECTOR_NANS(Ty, Src1, Src2, true)
519f4a2713aSLionel Sambuc   IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLT)
520f4a2713aSLionel Sambuc   return executeFCMP_OLT(Src1, Src2, Ty);
521f4a2713aSLionel Sambuc }
522f4a2713aSLionel Sambuc 
executeFCMP_UGT(GenericValue Src1,GenericValue Src2,Type * Ty)523f4a2713aSLionel Sambuc static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
524f4a2713aSLionel Sambuc                                      Type *Ty) {
525f4a2713aSLionel Sambuc   GenericValue Dest;
526f4a2713aSLionel Sambuc   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
527f4a2713aSLionel Sambuc   MASK_VECTOR_NANS(Ty, Src1, Src2, true)
528f4a2713aSLionel Sambuc   IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGT)
529f4a2713aSLionel Sambuc   return executeFCMP_OGT(Src1, Src2, Ty);
530f4a2713aSLionel Sambuc }
531f4a2713aSLionel Sambuc 
executeFCMP_ORD(GenericValue Src1,GenericValue Src2,Type * Ty)532f4a2713aSLionel Sambuc static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
533f4a2713aSLionel Sambuc                                      Type *Ty) {
534f4a2713aSLionel Sambuc   GenericValue Dest;
535f4a2713aSLionel Sambuc   if(Ty->isVectorTy()) {
536f4a2713aSLionel Sambuc     assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
537f4a2713aSLionel Sambuc     Dest.AggregateVal.resize( Src1.AggregateVal.size() );
538f4a2713aSLionel Sambuc     if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
539f4a2713aSLionel Sambuc       for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
540f4a2713aSLionel Sambuc         Dest.AggregateVal[_i].IntVal = APInt(1,
541f4a2713aSLionel Sambuc         ( (Src1.AggregateVal[_i].FloatVal ==
542f4a2713aSLionel Sambuc         Src1.AggregateVal[_i].FloatVal) &&
543f4a2713aSLionel Sambuc         (Src2.AggregateVal[_i].FloatVal ==
544f4a2713aSLionel Sambuc         Src2.AggregateVal[_i].FloatVal)));
545f4a2713aSLionel Sambuc     } else {
546f4a2713aSLionel Sambuc       for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
547f4a2713aSLionel Sambuc         Dest.AggregateVal[_i].IntVal = APInt(1,
548f4a2713aSLionel Sambuc         ( (Src1.AggregateVal[_i].DoubleVal ==
549f4a2713aSLionel Sambuc         Src1.AggregateVal[_i].DoubleVal) &&
550f4a2713aSLionel Sambuc         (Src2.AggregateVal[_i].DoubleVal ==
551f4a2713aSLionel Sambuc         Src2.AggregateVal[_i].DoubleVal)));
552f4a2713aSLionel Sambuc     }
553f4a2713aSLionel Sambuc   } else if (Ty->isFloatTy())
554f4a2713aSLionel Sambuc     Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
555f4a2713aSLionel Sambuc                            Src2.FloatVal == Src2.FloatVal));
556f4a2713aSLionel Sambuc   else {
557f4a2713aSLionel Sambuc     Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
558f4a2713aSLionel Sambuc                            Src2.DoubleVal == Src2.DoubleVal));
559f4a2713aSLionel Sambuc   }
560f4a2713aSLionel Sambuc   return Dest;
561f4a2713aSLionel Sambuc }
562f4a2713aSLionel Sambuc 
executeFCMP_UNO(GenericValue Src1,GenericValue Src2,Type * Ty)563f4a2713aSLionel Sambuc static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
564f4a2713aSLionel Sambuc                                      Type *Ty) {
565f4a2713aSLionel Sambuc   GenericValue Dest;
566f4a2713aSLionel Sambuc   if(Ty->isVectorTy()) {
567f4a2713aSLionel Sambuc     assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
568f4a2713aSLionel Sambuc     Dest.AggregateVal.resize( Src1.AggregateVal.size() );
569f4a2713aSLionel Sambuc     if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
570f4a2713aSLionel Sambuc       for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
571f4a2713aSLionel Sambuc         Dest.AggregateVal[_i].IntVal = APInt(1,
572f4a2713aSLionel Sambuc         ( (Src1.AggregateVal[_i].FloatVal !=
573f4a2713aSLionel Sambuc            Src1.AggregateVal[_i].FloatVal) ||
574f4a2713aSLionel Sambuc           (Src2.AggregateVal[_i].FloatVal !=
575f4a2713aSLionel Sambuc            Src2.AggregateVal[_i].FloatVal)));
576f4a2713aSLionel Sambuc       } else {
577f4a2713aSLionel Sambuc         for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
578f4a2713aSLionel Sambuc           Dest.AggregateVal[_i].IntVal = APInt(1,
579f4a2713aSLionel Sambuc           ( (Src1.AggregateVal[_i].DoubleVal !=
580f4a2713aSLionel Sambuc              Src1.AggregateVal[_i].DoubleVal) ||
581f4a2713aSLionel Sambuc             (Src2.AggregateVal[_i].DoubleVal !=
582f4a2713aSLionel Sambuc              Src2.AggregateVal[_i].DoubleVal)));
583f4a2713aSLionel Sambuc       }
584f4a2713aSLionel Sambuc   } else if (Ty->isFloatTy())
585f4a2713aSLionel Sambuc     Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
586f4a2713aSLionel Sambuc                            Src2.FloatVal != Src2.FloatVal));
587f4a2713aSLionel Sambuc   else {
588f4a2713aSLionel Sambuc     Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
589f4a2713aSLionel Sambuc                            Src2.DoubleVal != Src2.DoubleVal));
590f4a2713aSLionel Sambuc   }
591f4a2713aSLionel Sambuc   return Dest;
592f4a2713aSLionel Sambuc }
593f4a2713aSLionel Sambuc 
executeFCMP_BOOL(GenericValue Src1,GenericValue Src2,const Type * Ty,const bool val)594f4a2713aSLionel Sambuc static GenericValue executeFCMP_BOOL(GenericValue Src1, GenericValue Src2,
595f4a2713aSLionel Sambuc                                     const Type *Ty, const bool val) {
596f4a2713aSLionel Sambuc   GenericValue Dest;
597f4a2713aSLionel Sambuc     if(Ty->isVectorTy()) {
598f4a2713aSLionel Sambuc       assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
599f4a2713aSLionel Sambuc       Dest.AggregateVal.resize( Src1.AggregateVal.size() );
600f4a2713aSLionel Sambuc       for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
601f4a2713aSLionel Sambuc         Dest.AggregateVal[_i].IntVal = APInt(1,val);
602f4a2713aSLionel Sambuc     } else {
603f4a2713aSLionel Sambuc       Dest.IntVal = APInt(1, val);
604f4a2713aSLionel Sambuc     }
605f4a2713aSLionel Sambuc 
606f4a2713aSLionel Sambuc     return Dest;
607f4a2713aSLionel Sambuc }
608f4a2713aSLionel Sambuc 
visitFCmpInst(FCmpInst & I)609f4a2713aSLionel Sambuc void Interpreter::visitFCmpInst(FCmpInst &I) {
610f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
611f4a2713aSLionel Sambuc   Type *Ty    = I.getOperand(0)->getType();
612f4a2713aSLionel Sambuc   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
613f4a2713aSLionel Sambuc   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
614f4a2713aSLionel Sambuc   GenericValue R;   // Result
615f4a2713aSLionel Sambuc 
616f4a2713aSLionel Sambuc   switch (I.getPredicate()) {
617f4a2713aSLionel Sambuc   default:
618f4a2713aSLionel Sambuc     dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I;
619*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
620f4a2713aSLionel Sambuc   break;
621f4a2713aSLionel Sambuc   case FCmpInst::FCMP_FALSE: R = executeFCMP_BOOL(Src1, Src2, Ty, false);
622f4a2713aSLionel Sambuc   break;
623f4a2713aSLionel Sambuc   case FCmpInst::FCMP_TRUE:  R = executeFCMP_BOOL(Src1, Src2, Ty, true);
624f4a2713aSLionel Sambuc   break;
625f4a2713aSLionel Sambuc   case FCmpInst::FCMP_ORD:   R = executeFCMP_ORD(Src1, Src2, Ty); break;
626f4a2713aSLionel Sambuc   case FCmpInst::FCMP_UNO:   R = executeFCMP_UNO(Src1, Src2, Ty); break;
627f4a2713aSLionel Sambuc   case FCmpInst::FCMP_UEQ:   R = executeFCMP_UEQ(Src1, Src2, Ty); break;
628f4a2713aSLionel Sambuc   case FCmpInst::FCMP_OEQ:   R = executeFCMP_OEQ(Src1, Src2, Ty); break;
629f4a2713aSLionel Sambuc   case FCmpInst::FCMP_UNE:   R = executeFCMP_UNE(Src1, Src2, Ty); break;
630f4a2713aSLionel Sambuc   case FCmpInst::FCMP_ONE:   R = executeFCMP_ONE(Src1, Src2, Ty); break;
631f4a2713aSLionel Sambuc   case FCmpInst::FCMP_ULT:   R = executeFCMP_ULT(Src1, Src2, Ty); break;
632f4a2713aSLionel Sambuc   case FCmpInst::FCMP_OLT:   R = executeFCMP_OLT(Src1, Src2, Ty); break;
633f4a2713aSLionel Sambuc   case FCmpInst::FCMP_UGT:   R = executeFCMP_UGT(Src1, Src2, Ty); break;
634f4a2713aSLionel Sambuc   case FCmpInst::FCMP_OGT:   R = executeFCMP_OGT(Src1, Src2, Ty); break;
635f4a2713aSLionel Sambuc   case FCmpInst::FCMP_ULE:   R = executeFCMP_ULE(Src1, Src2, Ty); break;
636f4a2713aSLionel Sambuc   case FCmpInst::FCMP_OLE:   R = executeFCMP_OLE(Src1, Src2, Ty); break;
637f4a2713aSLionel Sambuc   case FCmpInst::FCMP_UGE:   R = executeFCMP_UGE(Src1, Src2, Ty); break;
638f4a2713aSLionel Sambuc   case FCmpInst::FCMP_OGE:   R = executeFCMP_OGE(Src1, Src2, Ty); break;
639f4a2713aSLionel Sambuc   }
640f4a2713aSLionel Sambuc 
641f4a2713aSLionel Sambuc   SetValue(&I, R, SF);
642f4a2713aSLionel Sambuc }
643f4a2713aSLionel Sambuc 
executeCmpInst(unsigned predicate,GenericValue Src1,GenericValue Src2,Type * Ty)644f4a2713aSLionel Sambuc static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
645f4a2713aSLionel Sambuc                                    GenericValue Src2, Type *Ty) {
646f4a2713aSLionel Sambuc   GenericValue Result;
647f4a2713aSLionel Sambuc   switch (predicate) {
648f4a2713aSLionel Sambuc   case ICmpInst::ICMP_EQ:    return executeICMP_EQ(Src1, Src2, Ty);
649f4a2713aSLionel Sambuc   case ICmpInst::ICMP_NE:    return executeICMP_NE(Src1, Src2, Ty);
650f4a2713aSLionel Sambuc   case ICmpInst::ICMP_UGT:   return executeICMP_UGT(Src1, Src2, Ty);
651f4a2713aSLionel Sambuc   case ICmpInst::ICMP_SGT:   return executeICMP_SGT(Src1, Src2, Ty);
652f4a2713aSLionel Sambuc   case ICmpInst::ICMP_ULT:   return executeICMP_ULT(Src1, Src2, Ty);
653f4a2713aSLionel Sambuc   case ICmpInst::ICMP_SLT:   return executeICMP_SLT(Src1, Src2, Ty);
654f4a2713aSLionel Sambuc   case ICmpInst::ICMP_UGE:   return executeICMP_UGE(Src1, Src2, Ty);
655f4a2713aSLionel Sambuc   case ICmpInst::ICMP_SGE:   return executeICMP_SGE(Src1, Src2, Ty);
656f4a2713aSLionel Sambuc   case ICmpInst::ICMP_ULE:   return executeICMP_ULE(Src1, Src2, Ty);
657f4a2713aSLionel Sambuc   case ICmpInst::ICMP_SLE:   return executeICMP_SLE(Src1, Src2, Ty);
658f4a2713aSLionel Sambuc   case FCmpInst::FCMP_ORD:   return executeFCMP_ORD(Src1, Src2, Ty);
659f4a2713aSLionel Sambuc   case FCmpInst::FCMP_UNO:   return executeFCMP_UNO(Src1, Src2, Ty);
660f4a2713aSLionel Sambuc   case FCmpInst::FCMP_OEQ:   return executeFCMP_OEQ(Src1, Src2, Ty);
661f4a2713aSLionel Sambuc   case FCmpInst::FCMP_UEQ:   return executeFCMP_UEQ(Src1, Src2, Ty);
662f4a2713aSLionel Sambuc   case FCmpInst::FCMP_ONE:   return executeFCMP_ONE(Src1, Src2, Ty);
663f4a2713aSLionel Sambuc   case FCmpInst::FCMP_UNE:   return executeFCMP_UNE(Src1, Src2, Ty);
664f4a2713aSLionel Sambuc   case FCmpInst::FCMP_OLT:   return executeFCMP_OLT(Src1, Src2, Ty);
665f4a2713aSLionel Sambuc   case FCmpInst::FCMP_ULT:   return executeFCMP_ULT(Src1, Src2, Ty);
666f4a2713aSLionel Sambuc   case FCmpInst::FCMP_OGT:   return executeFCMP_OGT(Src1, Src2, Ty);
667f4a2713aSLionel Sambuc   case FCmpInst::FCMP_UGT:   return executeFCMP_UGT(Src1, Src2, Ty);
668f4a2713aSLionel Sambuc   case FCmpInst::FCMP_OLE:   return executeFCMP_OLE(Src1, Src2, Ty);
669f4a2713aSLionel Sambuc   case FCmpInst::FCMP_ULE:   return executeFCMP_ULE(Src1, Src2, Ty);
670f4a2713aSLionel Sambuc   case FCmpInst::FCMP_OGE:   return executeFCMP_OGE(Src1, Src2, Ty);
671f4a2713aSLionel Sambuc   case FCmpInst::FCMP_UGE:   return executeFCMP_UGE(Src1, Src2, Ty);
672f4a2713aSLionel Sambuc   case FCmpInst::FCMP_FALSE: return executeFCMP_BOOL(Src1, Src2, Ty, false);
673f4a2713aSLionel Sambuc   case FCmpInst::FCMP_TRUE:  return executeFCMP_BOOL(Src1, Src2, Ty, true);
674f4a2713aSLionel Sambuc   default:
675f4a2713aSLionel Sambuc     dbgs() << "Unhandled Cmp predicate\n";
676*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
677f4a2713aSLionel Sambuc   }
678f4a2713aSLionel Sambuc }
679f4a2713aSLionel Sambuc 
visitBinaryOperator(BinaryOperator & I)680f4a2713aSLionel Sambuc void Interpreter::visitBinaryOperator(BinaryOperator &I) {
681f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
682f4a2713aSLionel Sambuc   Type *Ty    = I.getOperand(0)->getType();
683f4a2713aSLionel Sambuc   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
684f4a2713aSLionel Sambuc   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
685f4a2713aSLionel Sambuc   GenericValue R;   // Result
686f4a2713aSLionel Sambuc 
687f4a2713aSLionel Sambuc   // First process vector operation
688f4a2713aSLionel Sambuc   if (Ty->isVectorTy()) {
689f4a2713aSLionel Sambuc     assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
690f4a2713aSLionel Sambuc     R.AggregateVal.resize(Src1.AggregateVal.size());
691f4a2713aSLionel Sambuc 
692f4a2713aSLionel Sambuc     // Macros to execute binary operation 'OP' over integer vectors
693f4a2713aSLionel Sambuc #define INTEGER_VECTOR_OPERATION(OP)                               \
694f4a2713aSLionel Sambuc     for (unsigned i = 0; i < R.AggregateVal.size(); ++i)           \
695f4a2713aSLionel Sambuc       R.AggregateVal[i].IntVal =                                   \
696f4a2713aSLionel Sambuc       Src1.AggregateVal[i].IntVal OP Src2.AggregateVal[i].IntVal;
697f4a2713aSLionel Sambuc 
698f4a2713aSLionel Sambuc     // Additional macros to execute binary operations udiv/sdiv/urem/srem since
699f4a2713aSLionel Sambuc     // they have different notation.
700f4a2713aSLionel Sambuc #define INTEGER_VECTOR_FUNCTION(OP)                                \
701f4a2713aSLionel Sambuc     for (unsigned i = 0; i < R.AggregateVal.size(); ++i)           \
702f4a2713aSLionel Sambuc       R.AggregateVal[i].IntVal =                                   \
703f4a2713aSLionel Sambuc       Src1.AggregateVal[i].IntVal.OP(Src2.AggregateVal[i].IntVal);
704f4a2713aSLionel Sambuc 
705f4a2713aSLionel Sambuc     // Macros to execute binary operation 'OP' over floating point type TY
706f4a2713aSLionel Sambuc     // (float or double) vectors
707f4a2713aSLionel Sambuc #define FLOAT_VECTOR_FUNCTION(OP, TY)                               \
708f4a2713aSLionel Sambuc       for (unsigned i = 0; i < R.AggregateVal.size(); ++i)          \
709f4a2713aSLionel Sambuc         R.AggregateVal[i].TY =                                      \
710f4a2713aSLionel Sambuc         Src1.AggregateVal[i].TY OP Src2.AggregateVal[i].TY;
711f4a2713aSLionel Sambuc 
712f4a2713aSLionel Sambuc     // Macros to choose appropriate TY: float or double and run operation
713f4a2713aSLionel Sambuc     // execution
714f4a2713aSLionel Sambuc #define FLOAT_VECTOR_OP(OP) {                                         \
715f4a2713aSLionel Sambuc   if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy())        \
716f4a2713aSLionel Sambuc     FLOAT_VECTOR_FUNCTION(OP, FloatVal)                               \
717f4a2713aSLionel Sambuc   else {                                                              \
718f4a2713aSLionel Sambuc     if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy())     \
719f4a2713aSLionel Sambuc       FLOAT_VECTOR_FUNCTION(OP, DoubleVal)                            \
720f4a2713aSLionel Sambuc     else {                                                            \
721f4a2713aSLionel Sambuc       dbgs() << "Unhandled type for OP instruction: " << *Ty << "\n"; \
722f4a2713aSLionel Sambuc       llvm_unreachable(0);                                            \
723f4a2713aSLionel Sambuc     }                                                                 \
724f4a2713aSLionel Sambuc   }                                                                   \
725f4a2713aSLionel Sambuc }
726f4a2713aSLionel Sambuc 
727f4a2713aSLionel Sambuc     switch(I.getOpcode()){
728f4a2713aSLionel Sambuc     default:
729f4a2713aSLionel Sambuc       dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
730*0a6a1f1dSLionel Sambuc       llvm_unreachable(nullptr);
731f4a2713aSLionel Sambuc       break;
732f4a2713aSLionel Sambuc     case Instruction::Add:   INTEGER_VECTOR_OPERATION(+) break;
733f4a2713aSLionel Sambuc     case Instruction::Sub:   INTEGER_VECTOR_OPERATION(-) break;
734f4a2713aSLionel Sambuc     case Instruction::Mul:   INTEGER_VECTOR_OPERATION(*) break;
735f4a2713aSLionel Sambuc     case Instruction::UDiv:  INTEGER_VECTOR_FUNCTION(udiv) break;
736f4a2713aSLionel Sambuc     case Instruction::SDiv:  INTEGER_VECTOR_FUNCTION(sdiv) break;
737f4a2713aSLionel Sambuc     case Instruction::URem:  INTEGER_VECTOR_FUNCTION(urem) break;
738f4a2713aSLionel Sambuc     case Instruction::SRem:  INTEGER_VECTOR_FUNCTION(srem) break;
739f4a2713aSLionel Sambuc     case Instruction::And:   INTEGER_VECTOR_OPERATION(&) break;
740f4a2713aSLionel Sambuc     case Instruction::Or:    INTEGER_VECTOR_OPERATION(|) break;
741f4a2713aSLionel Sambuc     case Instruction::Xor:   INTEGER_VECTOR_OPERATION(^) break;
742f4a2713aSLionel Sambuc     case Instruction::FAdd:  FLOAT_VECTOR_OP(+) break;
743f4a2713aSLionel Sambuc     case Instruction::FSub:  FLOAT_VECTOR_OP(-) break;
744f4a2713aSLionel Sambuc     case Instruction::FMul:  FLOAT_VECTOR_OP(*) break;
745f4a2713aSLionel Sambuc     case Instruction::FDiv:  FLOAT_VECTOR_OP(/) break;
746f4a2713aSLionel Sambuc     case Instruction::FRem:
747f4a2713aSLionel Sambuc       if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy())
748f4a2713aSLionel Sambuc         for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
749f4a2713aSLionel Sambuc           R.AggregateVal[i].FloatVal =
750f4a2713aSLionel Sambuc           fmod(Src1.AggregateVal[i].FloatVal, Src2.AggregateVal[i].FloatVal);
751f4a2713aSLionel Sambuc       else {
752f4a2713aSLionel Sambuc         if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy())
753f4a2713aSLionel Sambuc           for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
754f4a2713aSLionel Sambuc             R.AggregateVal[i].DoubleVal =
755f4a2713aSLionel Sambuc             fmod(Src1.AggregateVal[i].DoubleVal, Src2.AggregateVal[i].DoubleVal);
756f4a2713aSLionel Sambuc         else {
757f4a2713aSLionel Sambuc           dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
758*0a6a1f1dSLionel Sambuc           llvm_unreachable(nullptr);
759f4a2713aSLionel Sambuc         }
760f4a2713aSLionel Sambuc       }
761f4a2713aSLionel Sambuc       break;
762f4a2713aSLionel Sambuc     }
763f4a2713aSLionel Sambuc   } else {
764f4a2713aSLionel Sambuc     switch (I.getOpcode()) {
765f4a2713aSLionel Sambuc     default:
766f4a2713aSLionel Sambuc       dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
767*0a6a1f1dSLionel Sambuc       llvm_unreachable(nullptr);
768f4a2713aSLionel Sambuc       break;
769f4a2713aSLionel Sambuc     case Instruction::Add:   R.IntVal = Src1.IntVal + Src2.IntVal; break;
770f4a2713aSLionel Sambuc     case Instruction::Sub:   R.IntVal = Src1.IntVal - Src2.IntVal; break;
771f4a2713aSLionel Sambuc     case Instruction::Mul:   R.IntVal = Src1.IntVal * Src2.IntVal; break;
772f4a2713aSLionel Sambuc     case Instruction::FAdd:  executeFAddInst(R, Src1, Src2, Ty); break;
773f4a2713aSLionel Sambuc     case Instruction::FSub:  executeFSubInst(R, Src1, Src2, Ty); break;
774f4a2713aSLionel Sambuc     case Instruction::FMul:  executeFMulInst(R, Src1, Src2, Ty); break;
775f4a2713aSLionel Sambuc     case Instruction::FDiv:  executeFDivInst(R, Src1, Src2, Ty); break;
776f4a2713aSLionel Sambuc     case Instruction::FRem:  executeFRemInst(R, Src1, Src2, Ty); break;
777f4a2713aSLionel Sambuc     case Instruction::UDiv:  R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
778f4a2713aSLionel Sambuc     case Instruction::SDiv:  R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
779f4a2713aSLionel Sambuc     case Instruction::URem:  R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
780f4a2713aSLionel Sambuc     case Instruction::SRem:  R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
781f4a2713aSLionel Sambuc     case Instruction::And:   R.IntVal = Src1.IntVal & Src2.IntVal; break;
782f4a2713aSLionel Sambuc     case Instruction::Or:    R.IntVal = Src1.IntVal | Src2.IntVal; break;
783f4a2713aSLionel Sambuc     case Instruction::Xor:   R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
784f4a2713aSLionel Sambuc     }
785f4a2713aSLionel Sambuc   }
786f4a2713aSLionel Sambuc   SetValue(&I, R, SF);
787f4a2713aSLionel Sambuc }
788f4a2713aSLionel Sambuc 
executeSelectInst(GenericValue Src1,GenericValue Src2,GenericValue Src3,const Type * Ty)789f4a2713aSLionel Sambuc static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
790f4a2713aSLionel Sambuc                                       GenericValue Src3, const Type *Ty) {
791f4a2713aSLionel Sambuc     GenericValue Dest;
792f4a2713aSLionel Sambuc     if(Ty->isVectorTy()) {
793f4a2713aSLionel Sambuc       assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
794f4a2713aSLionel Sambuc       assert(Src2.AggregateVal.size() == Src3.AggregateVal.size());
795f4a2713aSLionel Sambuc       Dest.AggregateVal.resize( Src1.AggregateVal.size() );
796f4a2713aSLionel Sambuc       for (size_t i = 0; i < Src1.AggregateVal.size(); ++i)
797f4a2713aSLionel Sambuc         Dest.AggregateVal[i] = (Src1.AggregateVal[i].IntVal == 0) ?
798f4a2713aSLionel Sambuc           Src3.AggregateVal[i] : Src2.AggregateVal[i];
799f4a2713aSLionel Sambuc     } else {
800f4a2713aSLionel Sambuc       Dest = (Src1.IntVal == 0) ? Src3 : Src2;
801f4a2713aSLionel Sambuc     }
802f4a2713aSLionel Sambuc     return Dest;
803f4a2713aSLionel Sambuc }
804f4a2713aSLionel Sambuc 
visitSelectInst(SelectInst & I)805f4a2713aSLionel Sambuc void Interpreter::visitSelectInst(SelectInst &I) {
806f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
807f4a2713aSLionel Sambuc   const Type * Ty = I.getOperand(0)->getType();
808f4a2713aSLionel Sambuc   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
809f4a2713aSLionel Sambuc   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
810f4a2713aSLionel Sambuc   GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
811f4a2713aSLionel Sambuc   GenericValue R = executeSelectInst(Src1, Src2, Src3, Ty);
812f4a2713aSLionel Sambuc   SetValue(&I, R, SF);
813f4a2713aSLionel Sambuc }
814f4a2713aSLionel Sambuc 
815f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
816f4a2713aSLionel Sambuc //                     Terminator Instruction Implementations
817f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
818f4a2713aSLionel Sambuc 
exitCalled(GenericValue GV)819f4a2713aSLionel Sambuc void Interpreter::exitCalled(GenericValue GV) {
820f4a2713aSLionel Sambuc   // runAtExitHandlers() assumes there are no stack frames, but
821f4a2713aSLionel Sambuc   // if exit() was called, then it had a stack frame. Blow away
822f4a2713aSLionel Sambuc   // the stack before interpreting atexit handlers.
823f4a2713aSLionel Sambuc   ECStack.clear();
824f4a2713aSLionel Sambuc   runAtExitHandlers();
825f4a2713aSLionel Sambuc   exit(GV.IntVal.zextOrTrunc(32).getZExtValue());
826f4a2713aSLionel Sambuc }
827f4a2713aSLionel Sambuc 
828f4a2713aSLionel Sambuc /// Pop the last stack frame off of ECStack and then copy the result
829f4a2713aSLionel Sambuc /// back into the result variable if we are not returning void. The
830f4a2713aSLionel Sambuc /// result variable may be the ExitValue, or the Value of the calling
831f4a2713aSLionel Sambuc /// CallInst if there was a previous stack frame. This method may
832f4a2713aSLionel Sambuc /// invalidate any ECStack iterators you have. This method also takes
833f4a2713aSLionel Sambuc /// care of switching to the normal destination BB, if we are returning
834f4a2713aSLionel Sambuc /// from an invoke.
835f4a2713aSLionel Sambuc ///
popStackAndReturnValueToCaller(Type * RetTy,GenericValue Result)836f4a2713aSLionel Sambuc void Interpreter::popStackAndReturnValueToCaller(Type *RetTy,
837f4a2713aSLionel Sambuc                                                  GenericValue Result) {
838f4a2713aSLionel Sambuc   // Pop the current stack frame.
839f4a2713aSLionel Sambuc   ECStack.pop_back();
840f4a2713aSLionel Sambuc 
841f4a2713aSLionel Sambuc   if (ECStack.empty()) {  // Finished main.  Put result into exit code...
842f4a2713aSLionel Sambuc     if (RetTy && !RetTy->isVoidTy()) {          // Nonvoid return type?
843f4a2713aSLionel Sambuc       ExitValue = Result;   // Capture the exit value of the program
844f4a2713aSLionel Sambuc     } else {
845f4a2713aSLionel Sambuc       memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
846f4a2713aSLionel Sambuc     }
847f4a2713aSLionel Sambuc   } else {
848f4a2713aSLionel Sambuc     // If we have a previous stack frame, and we have a previous call,
849f4a2713aSLionel Sambuc     // fill in the return value...
850f4a2713aSLionel Sambuc     ExecutionContext &CallingSF = ECStack.back();
851f4a2713aSLionel Sambuc     if (Instruction *I = CallingSF.Caller.getInstruction()) {
852f4a2713aSLionel Sambuc       // Save result...
853f4a2713aSLionel Sambuc       if (!CallingSF.Caller.getType()->isVoidTy())
854f4a2713aSLionel Sambuc         SetValue(I, Result, CallingSF);
855f4a2713aSLionel Sambuc       if (InvokeInst *II = dyn_cast<InvokeInst> (I))
856f4a2713aSLionel Sambuc         SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
857f4a2713aSLionel Sambuc       CallingSF.Caller = CallSite();          // We returned from the call...
858f4a2713aSLionel Sambuc     }
859f4a2713aSLionel Sambuc   }
860f4a2713aSLionel Sambuc }
861f4a2713aSLionel Sambuc 
visitReturnInst(ReturnInst & I)862f4a2713aSLionel Sambuc void Interpreter::visitReturnInst(ReturnInst &I) {
863f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
864f4a2713aSLionel Sambuc   Type *RetTy = Type::getVoidTy(I.getContext());
865f4a2713aSLionel Sambuc   GenericValue Result;
866f4a2713aSLionel Sambuc 
867f4a2713aSLionel Sambuc   // Save away the return value... (if we are not 'ret void')
868f4a2713aSLionel Sambuc   if (I.getNumOperands()) {
869f4a2713aSLionel Sambuc     RetTy  = I.getReturnValue()->getType();
870f4a2713aSLionel Sambuc     Result = getOperandValue(I.getReturnValue(), SF);
871f4a2713aSLionel Sambuc   }
872f4a2713aSLionel Sambuc 
873f4a2713aSLionel Sambuc   popStackAndReturnValueToCaller(RetTy, Result);
874f4a2713aSLionel Sambuc }
875f4a2713aSLionel Sambuc 
visitUnreachableInst(UnreachableInst & I)876f4a2713aSLionel Sambuc void Interpreter::visitUnreachableInst(UnreachableInst &I) {
877f4a2713aSLionel Sambuc   report_fatal_error("Program executed an 'unreachable' instruction!");
878f4a2713aSLionel Sambuc }
879f4a2713aSLionel Sambuc 
visitBranchInst(BranchInst & I)880f4a2713aSLionel Sambuc void Interpreter::visitBranchInst(BranchInst &I) {
881f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
882f4a2713aSLionel Sambuc   BasicBlock *Dest;
883f4a2713aSLionel Sambuc 
884f4a2713aSLionel Sambuc   Dest = I.getSuccessor(0);          // Uncond branches have a fixed dest...
885f4a2713aSLionel Sambuc   if (!I.isUnconditional()) {
886f4a2713aSLionel Sambuc     Value *Cond = I.getCondition();
887f4a2713aSLionel Sambuc     if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
888f4a2713aSLionel Sambuc       Dest = I.getSuccessor(1);
889f4a2713aSLionel Sambuc   }
890f4a2713aSLionel Sambuc   SwitchToNewBasicBlock(Dest, SF);
891f4a2713aSLionel Sambuc }
892f4a2713aSLionel Sambuc 
visitSwitchInst(SwitchInst & I)893f4a2713aSLionel Sambuc void Interpreter::visitSwitchInst(SwitchInst &I) {
894f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
895f4a2713aSLionel Sambuc   Value* Cond = I.getCondition();
896f4a2713aSLionel Sambuc   Type *ElTy = Cond->getType();
897f4a2713aSLionel Sambuc   GenericValue CondVal = getOperandValue(Cond, SF);
898f4a2713aSLionel Sambuc 
899f4a2713aSLionel Sambuc   // Check to see if any of the cases match...
900*0a6a1f1dSLionel Sambuc   BasicBlock *Dest = nullptr;
901f4a2713aSLionel Sambuc   for (SwitchInst::CaseIt i = I.case_begin(), e = I.case_end(); i != e; ++i) {
902f4a2713aSLionel Sambuc     GenericValue CaseVal = getOperandValue(i.getCaseValue(), SF);
903f4a2713aSLionel Sambuc     if (executeICMP_EQ(CondVal, CaseVal, ElTy).IntVal != 0) {
904f4a2713aSLionel Sambuc       Dest = cast<BasicBlock>(i.getCaseSuccessor());
905f4a2713aSLionel Sambuc       break;
906f4a2713aSLionel Sambuc     }
907f4a2713aSLionel Sambuc   }
908f4a2713aSLionel Sambuc   if (!Dest) Dest = I.getDefaultDest();   // No cases matched: use default
909f4a2713aSLionel Sambuc   SwitchToNewBasicBlock(Dest, SF);
910f4a2713aSLionel Sambuc }
911f4a2713aSLionel Sambuc 
visitIndirectBrInst(IndirectBrInst & I)912f4a2713aSLionel Sambuc void Interpreter::visitIndirectBrInst(IndirectBrInst &I) {
913f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
914f4a2713aSLionel Sambuc   void *Dest = GVTOP(getOperandValue(I.getAddress(), SF));
915f4a2713aSLionel Sambuc   SwitchToNewBasicBlock((BasicBlock*)Dest, SF);
916f4a2713aSLionel Sambuc }
917f4a2713aSLionel Sambuc 
918f4a2713aSLionel Sambuc 
919f4a2713aSLionel Sambuc // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
920f4a2713aSLionel Sambuc // This function handles the actual updating of block and instruction iterators
921f4a2713aSLionel Sambuc // as well as execution of all of the PHI nodes in the destination block.
922f4a2713aSLionel Sambuc //
923f4a2713aSLionel Sambuc // This method does this because all of the PHI nodes must be executed
924f4a2713aSLionel Sambuc // atomically, reading their inputs before any of the results are updated.  Not
925f4a2713aSLionel Sambuc // doing this can cause problems if the PHI nodes depend on other PHI nodes for
926f4a2713aSLionel Sambuc // their inputs.  If the input PHI node is updated before it is read, incorrect
927f4a2713aSLionel Sambuc // results can happen.  Thus we use a two phase approach.
928f4a2713aSLionel Sambuc //
SwitchToNewBasicBlock(BasicBlock * Dest,ExecutionContext & SF)929f4a2713aSLionel Sambuc void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
930f4a2713aSLionel Sambuc   BasicBlock *PrevBB = SF.CurBB;      // Remember where we came from...
931f4a2713aSLionel Sambuc   SF.CurBB   = Dest;                  // Update CurBB to branch destination
932f4a2713aSLionel Sambuc   SF.CurInst = SF.CurBB->begin();     // Update new instruction ptr...
933f4a2713aSLionel Sambuc 
934f4a2713aSLionel Sambuc   if (!isa<PHINode>(SF.CurInst)) return;  // Nothing fancy to do
935f4a2713aSLionel Sambuc 
936f4a2713aSLionel Sambuc   // Loop over all of the PHI nodes in the current block, reading their inputs.
937f4a2713aSLionel Sambuc   std::vector<GenericValue> ResultValues;
938f4a2713aSLionel Sambuc 
939f4a2713aSLionel Sambuc   for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
940f4a2713aSLionel Sambuc     // Search for the value corresponding to this previous bb...
941f4a2713aSLionel Sambuc     int i = PN->getBasicBlockIndex(PrevBB);
942f4a2713aSLionel Sambuc     assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
943f4a2713aSLionel Sambuc     Value *IncomingValue = PN->getIncomingValue(i);
944f4a2713aSLionel Sambuc 
945f4a2713aSLionel Sambuc     // Save the incoming value for this PHI node...
946f4a2713aSLionel Sambuc     ResultValues.push_back(getOperandValue(IncomingValue, SF));
947f4a2713aSLionel Sambuc   }
948f4a2713aSLionel Sambuc 
949f4a2713aSLionel Sambuc   // Now loop over all of the PHI nodes setting their values...
950f4a2713aSLionel Sambuc   SF.CurInst = SF.CurBB->begin();
951f4a2713aSLionel Sambuc   for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
952f4a2713aSLionel Sambuc     PHINode *PN = cast<PHINode>(SF.CurInst);
953f4a2713aSLionel Sambuc     SetValue(PN, ResultValues[i], SF);
954f4a2713aSLionel Sambuc   }
955f4a2713aSLionel Sambuc }
956f4a2713aSLionel Sambuc 
957f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
958f4a2713aSLionel Sambuc //                     Memory Instruction Implementations
959f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
960f4a2713aSLionel Sambuc 
visitAllocaInst(AllocaInst & I)961f4a2713aSLionel Sambuc void Interpreter::visitAllocaInst(AllocaInst &I) {
962f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
963f4a2713aSLionel Sambuc 
964f4a2713aSLionel Sambuc   Type *Ty = I.getType()->getElementType();  // Type to be allocated
965f4a2713aSLionel Sambuc 
966f4a2713aSLionel Sambuc   // Get the number of elements being allocated by the array...
967f4a2713aSLionel Sambuc   unsigned NumElements =
968f4a2713aSLionel Sambuc     getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
969f4a2713aSLionel Sambuc 
970f4a2713aSLionel Sambuc   unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
971f4a2713aSLionel Sambuc 
972f4a2713aSLionel Sambuc   // Avoid malloc-ing zero bytes, use max()...
973f4a2713aSLionel Sambuc   unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
974f4a2713aSLionel Sambuc 
975f4a2713aSLionel Sambuc   // Allocate enough memory to hold the type...
976f4a2713aSLionel Sambuc   void *Memory = malloc(MemToAlloc);
977f4a2713aSLionel Sambuc 
978f4a2713aSLionel Sambuc   DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
979f4a2713aSLionel Sambuc                << NumElements << " (Total: " << MemToAlloc << ") at "
980f4a2713aSLionel Sambuc                << uintptr_t(Memory) << '\n');
981f4a2713aSLionel Sambuc 
982f4a2713aSLionel Sambuc   GenericValue Result = PTOGV(Memory);
983*0a6a1f1dSLionel Sambuc   assert(Result.PointerVal && "Null pointer returned by malloc!");
984f4a2713aSLionel Sambuc   SetValue(&I, Result, SF);
985f4a2713aSLionel Sambuc 
986f4a2713aSLionel Sambuc   if (I.getOpcode() == Instruction::Alloca)
987f4a2713aSLionel Sambuc     ECStack.back().Allocas.add(Memory);
988f4a2713aSLionel Sambuc }
989f4a2713aSLionel Sambuc 
990f4a2713aSLionel Sambuc // getElementOffset - The workhorse for getelementptr.
991f4a2713aSLionel Sambuc //
executeGEPOperation(Value * Ptr,gep_type_iterator I,gep_type_iterator E,ExecutionContext & SF)992f4a2713aSLionel Sambuc GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
993f4a2713aSLionel Sambuc                                               gep_type_iterator E,
994f4a2713aSLionel Sambuc                                               ExecutionContext &SF) {
995f4a2713aSLionel Sambuc   assert(Ptr->getType()->isPointerTy() &&
996f4a2713aSLionel Sambuc          "Cannot getElementOffset of a nonpointer type!");
997f4a2713aSLionel Sambuc 
998f4a2713aSLionel Sambuc   uint64_t Total = 0;
999f4a2713aSLionel Sambuc 
1000f4a2713aSLionel Sambuc   for (; I != E; ++I) {
1001f4a2713aSLionel Sambuc     if (StructType *STy = dyn_cast<StructType>(*I)) {
1002f4a2713aSLionel Sambuc       const StructLayout *SLO = TD.getStructLayout(STy);
1003f4a2713aSLionel Sambuc 
1004f4a2713aSLionel Sambuc       const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
1005f4a2713aSLionel Sambuc       unsigned Index = unsigned(CPU->getZExtValue());
1006f4a2713aSLionel Sambuc 
1007f4a2713aSLionel Sambuc       Total += SLO->getElementOffset(Index);
1008f4a2713aSLionel Sambuc     } else {
1009f4a2713aSLionel Sambuc       SequentialType *ST = cast<SequentialType>(*I);
1010f4a2713aSLionel Sambuc       // Get the index number for the array... which must be long type...
1011f4a2713aSLionel Sambuc       GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
1012f4a2713aSLionel Sambuc 
1013f4a2713aSLionel Sambuc       int64_t Idx;
1014f4a2713aSLionel Sambuc       unsigned BitWidth =
1015f4a2713aSLionel Sambuc         cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
1016f4a2713aSLionel Sambuc       if (BitWidth == 32)
1017f4a2713aSLionel Sambuc         Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
1018f4a2713aSLionel Sambuc       else {
1019f4a2713aSLionel Sambuc         assert(BitWidth == 64 && "Invalid index type for getelementptr");
1020f4a2713aSLionel Sambuc         Idx = (int64_t)IdxGV.IntVal.getZExtValue();
1021f4a2713aSLionel Sambuc       }
1022f4a2713aSLionel Sambuc       Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
1023f4a2713aSLionel Sambuc     }
1024f4a2713aSLionel Sambuc   }
1025f4a2713aSLionel Sambuc 
1026f4a2713aSLionel Sambuc   GenericValue Result;
1027f4a2713aSLionel Sambuc   Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
1028f4a2713aSLionel Sambuc   DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n");
1029f4a2713aSLionel Sambuc   return Result;
1030f4a2713aSLionel Sambuc }
1031f4a2713aSLionel Sambuc 
visitGetElementPtrInst(GetElementPtrInst & I)1032f4a2713aSLionel Sambuc void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1033f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1034f4a2713aSLionel Sambuc   SetValue(&I, executeGEPOperation(I.getPointerOperand(),
1035f4a2713aSLionel Sambuc                                    gep_type_begin(I), gep_type_end(I), SF), SF);
1036f4a2713aSLionel Sambuc }
1037f4a2713aSLionel Sambuc 
visitLoadInst(LoadInst & I)1038f4a2713aSLionel Sambuc void Interpreter::visitLoadInst(LoadInst &I) {
1039f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1040f4a2713aSLionel Sambuc   GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
1041f4a2713aSLionel Sambuc   GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
1042f4a2713aSLionel Sambuc   GenericValue Result;
1043f4a2713aSLionel Sambuc   LoadValueFromMemory(Result, Ptr, I.getType());
1044f4a2713aSLionel Sambuc   SetValue(&I, Result, SF);
1045f4a2713aSLionel Sambuc   if (I.isVolatile() && PrintVolatile)
1046f4a2713aSLionel Sambuc     dbgs() << "Volatile load " << I;
1047f4a2713aSLionel Sambuc }
1048f4a2713aSLionel Sambuc 
visitStoreInst(StoreInst & I)1049f4a2713aSLionel Sambuc void Interpreter::visitStoreInst(StoreInst &I) {
1050f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1051f4a2713aSLionel Sambuc   GenericValue Val = getOperandValue(I.getOperand(0), SF);
1052f4a2713aSLionel Sambuc   GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
1053f4a2713aSLionel Sambuc   StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
1054f4a2713aSLionel Sambuc                      I.getOperand(0)->getType());
1055f4a2713aSLionel Sambuc   if (I.isVolatile() && PrintVolatile)
1056f4a2713aSLionel Sambuc     dbgs() << "Volatile store: " << I;
1057f4a2713aSLionel Sambuc }
1058f4a2713aSLionel Sambuc 
1059f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1060f4a2713aSLionel Sambuc //                 Miscellaneous Instruction Implementations
1061f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1062f4a2713aSLionel Sambuc 
visitCallSite(CallSite CS)1063f4a2713aSLionel Sambuc void Interpreter::visitCallSite(CallSite CS) {
1064f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1065f4a2713aSLionel Sambuc 
1066f4a2713aSLionel Sambuc   // Check to see if this is an intrinsic function call...
1067f4a2713aSLionel Sambuc   Function *F = CS.getCalledFunction();
1068f4a2713aSLionel Sambuc   if (F && F->isDeclaration())
1069f4a2713aSLionel Sambuc     switch (F->getIntrinsicID()) {
1070f4a2713aSLionel Sambuc     case Intrinsic::not_intrinsic:
1071f4a2713aSLionel Sambuc       break;
1072f4a2713aSLionel Sambuc     case Intrinsic::vastart: { // va_start
1073f4a2713aSLionel Sambuc       GenericValue ArgIndex;
1074f4a2713aSLionel Sambuc       ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1075f4a2713aSLionel Sambuc       ArgIndex.UIntPairVal.second = 0;
1076f4a2713aSLionel Sambuc       SetValue(CS.getInstruction(), ArgIndex, SF);
1077f4a2713aSLionel Sambuc       return;
1078f4a2713aSLionel Sambuc     }
1079f4a2713aSLionel Sambuc     case Intrinsic::vaend:    // va_end is a noop for the interpreter
1080f4a2713aSLionel Sambuc       return;
1081f4a2713aSLionel Sambuc     case Intrinsic::vacopy:   // va_copy: dest = src
1082f4a2713aSLionel Sambuc       SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1083f4a2713aSLionel Sambuc       return;
1084f4a2713aSLionel Sambuc     default:
1085f4a2713aSLionel Sambuc       // If it is an unknown intrinsic function, use the intrinsic lowering
1086f4a2713aSLionel Sambuc       // class to transform it into hopefully tasty LLVM code.
1087f4a2713aSLionel Sambuc       //
1088f4a2713aSLionel Sambuc       BasicBlock::iterator me(CS.getInstruction());
1089f4a2713aSLionel Sambuc       BasicBlock *Parent = CS.getInstruction()->getParent();
1090f4a2713aSLionel Sambuc       bool atBegin(Parent->begin() == me);
1091f4a2713aSLionel Sambuc       if (!atBegin)
1092f4a2713aSLionel Sambuc         --me;
1093f4a2713aSLionel Sambuc       IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1094f4a2713aSLionel Sambuc 
1095f4a2713aSLionel Sambuc       // Restore the CurInst pointer to the first instruction newly inserted, if
1096f4a2713aSLionel Sambuc       // any.
1097f4a2713aSLionel Sambuc       if (atBegin) {
1098f4a2713aSLionel Sambuc         SF.CurInst = Parent->begin();
1099f4a2713aSLionel Sambuc       } else {
1100f4a2713aSLionel Sambuc         SF.CurInst = me;
1101f4a2713aSLionel Sambuc         ++SF.CurInst;
1102f4a2713aSLionel Sambuc       }
1103f4a2713aSLionel Sambuc       return;
1104f4a2713aSLionel Sambuc     }
1105f4a2713aSLionel Sambuc 
1106f4a2713aSLionel Sambuc 
1107f4a2713aSLionel Sambuc   SF.Caller = CS;
1108f4a2713aSLionel Sambuc   std::vector<GenericValue> ArgVals;
1109f4a2713aSLionel Sambuc   const unsigned NumArgs = SF.Caller.arg_size();
1110f4a2713aSLionel Sambuc   ArgVals.reserve(NumArgs);
1111f4a2713aSLionel Sambuc   uint16_t pNum = 1;
1112f4a2713aSLionel Sambuc   for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1113f4a2713aSLionel Sambuc          e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
1114f4a2713aSLionel Sambuc     Value *V = *i;
1115f4a2713aSLionel Sambuc     ArgVals.push_back(getOperandValue(V, SF));
1116f4a2713aSLionel Sambuc   }
1117f4a2713aSLionel Sambuc 
1118f4a2713aSLionel Sambuc   // To handle indirect calls, we must get the pointer value from the argument
1119f4a2713aSLionel Sambuc   // and treat it as a function pointer.
1120f4a2713aSLionel Sambuc   GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
1121f4a2713aSLionel Sambuc   callFunction((Function*)GVTOP(SRC), ArgVals);
1122f4a2713aSLionel Sambuc }
1123f4a2713aSLionel Sambuc 
1124*0a6a1f1dSLionel Sambuc // auxiliary function for shift operations
getShiftAmount(uint64_t orgShiftAmount,llvm::APInt valueToShift)1125f4a2713aSLionel Sambuc static unsigned getShiftAmount(uint64_t orgShiftAmount,
1126f4a2713aSLionel Sambuc                                llvm::APInt valueToShift) {
1127f4a2713aSLionel Sambuc   unsigned valueWidth = valueToShift.getBitWidth();
1128f4a2713aSLionel Sambuc   if (orgShiftAmount < (uint64_t)valueWidth)
1129f4a2713aSLionel Sambuc     return orgShiftAmount;
1130f4a2713aSLionel Sambuc   // according to the llvm documentation, if orgShiftAmount > valueWidth,
1131f4a2713aSLionel Sambuc   // the result is undfeined. but we do shift by this rule:
1132f4a2713aSLionel Sambuc   return (NextPowerOf2(valueWidth-1) - 1) & orgShiftAmount;
1133f4a2713aSLionel Sambuc }
1134f4a2713aSLionel Sambuc 
1135f4a2713aSLionel Sambuc 
visitShl(BinaryOperator & I)1136f4a2713aSLionel Sambuc void Interpreter::visitShl(BinaryOperator &I) {
1137f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1138f4a2713aSLionel Sambuc   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1139f4a2713aSLionel Sambuc   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1140f4a2713aSLionel Sambuc   GenericValue Dest;
1141f4a2713aSLionel Sambuc   const Type *Ty = I.getType();
1142f4a2713aSLionel Sambuc 
1143f4a2713aSLionel Sambuc   if (Ty->isVectorTy()) {
1144f4a2713aSLionel Sambuc     uint32_t src1Size = uint32_t(Src1.AggregateVal.size());
1145f4a2713aSLionel Sambuc     assert(src1Size == Src2.AggregateVal.size());
1146f4a2713aSLionel Sambuc     for (unsigned i = 0; i < src1Size; i++) {
1147f4a2713aSLionel Sambuc       GenericValue Result;
1148f4a2713aSLionel Sambuc       uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1149f4a2713aSLionel Sambuc       llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1150f4a2713aSLionel Sambuc       Result.IntVal = valueToShift.shl(getShiftAmount(shiftAmount, valueToShift));
1151f4a2713aSLionel Sambuc       Dest.AggregateVal.push_back(Result);
1152f4a2713aSLionel Sambuc     }
1153f4a2713aSLionel Sambuc   } else {
1154f4a2713aSLionel Sambuc     // scalar
1155f4a2713aSLionel Sambuc     uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1156f4a2713aSLionel Sambuc     llvm::APInt valueToShift = Src1.IntVal;
1157f4a2713aSLionel Sambuc     Dest.IntVal = valueToShift.shl(getShiftAmount(shiftAmount, valueToShift));
1158f4a2713aSLionel Sambuc   }
1159f4a2713aSLionel Sambuc 
1160f4a2713aSLionel Sambuc   SetValue(&I, Dest, SF);
1161f4a2713aSLionel Sambuc }
1162f4a2713aSLionel Sambuc 
visitLShr(BinaryOperator & I)1163f4a2713aSLionel Sambuc void Interpreter::visitLShr(BinaryOperator &I) {
1164f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1165f4a2713aSLionel Sambuc   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1166f4a2713aSLionel Sambuc   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1167f4a2713aSLionel Sambuc   GenericValue Dest;
1168f4a2713aSLionel Sambuc   const Type *Ty = I.getType();
1169f4a2713aSLionel Sambuc 
1170f4a2713aSLionel Sambuc   if (Ty->isVectorTy()) {
1171f4a2713aSLionel Sambuc     uint32_t src1Size = uint32_t(Src1.AggregateVal.size());
1172f4a2713aSLionel Sambuc     assert(src1Size == Src2.AggregateVal.size());
1173f4a2713aSLionel Sambuc     for (unsigned i = 0; i < src1Size; i++) {
1174f4a2713aSLionel Sambuc       GenericValue Result;
1175f4a2713aSLionel Sambuc       uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1176f4a2713aSLionel Sambuc       llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1177f4a2713aSLionel Sambuc       Result.IntVal = valueToShift.lshr(getShiftAmount(shiftAmount, valueToShift));
1178f4a2713aSLionel Sambuc       Dest.AggregateVal.push_back(Result);
1179f4a2713aSLionel Sambuc     }
1180f4a2713aSLionel Sambuc   } else {
1181f4a2713aSLionel Sambuc     // scalar
1182f4a2713aSLionel Sambuc     uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1183f4a2713aSLionel Sambuc     llvm::APInt valueToShift = Src1.IntVal;
1184f4a2713aSLionel Sambuc     Dest.IntVal = valueToShift.lshr(getShiftAmount(shiftAmount, valueToShift));
1185f4a2713aSLionel Sambuc   }
1186f4a2713aSLionel Sambuc 
1187f4a2713aSLionel Sambuc   SetValue(&I, Dest, SF);
1188f4a2713aSLionel Sambuc }
1189f4a2713aSLionel Sambuc 
visitAShr(BinaryOperator & I)1190f4a2713aSLionel Sambuc void Interpreter::visitAShr(BinaryOperator &I) {
1191f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1192f4a2713aSLionel Sambuc   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1193f4a2713aSLionel Sambuc   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1194f4a2713aSLionel Sambuc   GenericValue Dest;
1195f4a2713aSLionel Sambuc   const Type *Ty = I.getType();
1196f4a2713aSLionel Sambuc 
1197f4a2713aSLionel Sambuc   if (Ty->isVectorTy()) {
1198f4a2713aSLionel Sambuc     size_t src1Size = Src1.AggregateVal.size();
1199f4a2713aSLionel Sambuc     assert(src1Size == Src2.AggregateVal.size());
1200f4a2713aSLionel Sambuc     for (unsigned i = 0; i < src1Size; i++) {
1201f4a2713aSLionel Sambuc       GenericValue Result;
1202f4a2713aSLionel Sambuc       uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1203f4a2713aSLionel Sambuc       llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1204f4a2713aSLionel Sambuc       Result.IntVal = valueToShift.ashr(getShiftAmount(shiftAmount, valueToShift));
1205f4a2713aSLionel Sambuc       Dest.AggregateVal.push_back(Result);
1206f4a2713aSLionel Sambuc     }
1207f4a2713aSLionel Sambuc   } else {
1208f4a2713aSLionel Sambuc     // scalar
1209f4a2713aSLionel Sambuc     uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1210f4a2713aSLionel Sambuc     llvm::APInt valueToShift = Src1.IntVal;
1211f4a2713aSLionel Sambuc     Dest.IntVal = valueToShift.ashr(getShiftAmount(shiftAmount, valueToShift));
1212f4a2713aSLionel Sambuc   }
1213f4a2713aSLionel Sambuc 
1214f4a2713aSLionel Sambuc   SetValue(&I, Dest, SF);
1215f4a2713aSLionel Sambuc }
1216f4a2713aSLionel Sambuc 
executeTruncInst(Value * SrcVal,Type * DstTy,ExecutionContext & SF)1217f4a2713aSLionel Sambuc GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy,
1218f4a2713aSLionel Sambuc                                            ExecutionContext &SF) {
1219f4a2713aSLionel Sambuc   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1220f4a2713aSLionel Sambuc   Type *SrcTy = SrcVal->getType();
1221f4a2713aSLionel Sambuc   if (SrcTy->isVectorTy()) {
1222f4a2713aSLionel Sambuc     Type *DstVecTy = DstTy->getScalarType();
1223f4a2713aSLionel Sambuc     unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1224f4a2713aSLionel Sambuc     unsigned NumElts = Src.AggregateVal.size();
1225f4a2713aSLionel Sambuc     // the sizes of src and dst vectors must be equal
1226f4a2713aSLionel Sambuc     Dest.AggregateVal.resize(NumElts);
1227f4a2713aSLionel Sambuc     for (unsigned i = 0; i < NumElts; i++)
1228f4a2713aSLionel Sambuc       Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.trunc(DBitWidth);
1229f4a2713aSLionel Sambuc   } else {
1230f4a2713aSLionel Sambuc     IntegerType *DITy = cast<IntegerType>(DstTy);
1231f4a2713aSLionel Sambuc     unsigned DBitWidth = DITy->getBitWidth();
1232f4a2713aSLionel Sambuc     Dest.IntVal = Src.IntVal.trunc(DBitWidth);
1233f4a2713aSLionel Sambuc   }
1234f4a2713aSLionel Sambuc   return Dest;
1235f4a2713aSLionel Sambuc }
1236f4a2713aSLionel Sambuc 
executeSExtInst(Value * SrcVal,Type * DstTy,ExecutionContext & SF)1237f4a2713aSLionel Sambuc GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy,
1238f4a2713aSLionel Sambuc                                           ExecutionContext &SF) {
1239f4a2713aSLionel Sambuc   const Type *SrcTy = SrcVal->getType();
1240f4a2713aSLionel Sambuc   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1241f4a2713aSLionel Sambuc   if (SrcTy->isVectorTy()) {
1242f4a2713aSLionel Sambuc     const Type *DstVecTy = DstTy->getScalarType();
1243f4a2713aSLionel Sambuc     unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1244f4a2713aSLionel Sambuc     unsigned size = Src.AggregateVal.size();
1245f4a2713aSLionel Sambuc     // the sizes of src and dst vectors must be equal.
1246f4a2713aSLionel Sambuc     Dest.AggregateVal.resize(size);
1247f4a2713aSLionel Sambuc     for (unsigned i = 0; i < size; i++)
1248f4a2713aSLionel Sambuc       Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.sext(DBitWidth);
1249f4a2713aSLionel Sambuc   } else {
1250f4a2713aSLionel Sambuc     const IntegerType *DITy = cast<IntegerType>(DstTy);
1251f4a2713aSLionel Sambuc     unsigned DBitWidth = DITy->getBitWidth();
1252f4a2713aSLionel Sambuc     Dest.IntVal = Src.IntVal.sext(DBitWidth);
1253f4a2713aSLionel Sambuc   }
1254f4a2713aSLionel Sambuc   return Dest;
1255f4a2713aSLionel Sambuc }
1256f4a2713aSLionel Sambuc 
executeZExtInst(Value * SrcVal,Type * DstTy,ExecutionContext & SF)1257f4a2713aSLionel Sambuc GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy,
1258f4a2713aSLionel Sambuc                                           ExecutionContext &SF) {
1259f4a2713aSLionel Sambuc   const Type *SrcTy = SrcVal->getType();
1260f4a2713aSLionel Sambuc   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1261f4a2713aSLionel Sambuc   if (SrcTy->isVectorTy()) {
1262f4a2713aSLionel Sambuc     const Type *DstVecTy = DstTy->getScalarType();
1263f4a2713aSLionel Sambuc     unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1264f4a2713aSLionel Sambuc 
1265f4a2713aSLionel Sambuc     unsigned size = Src.AggregateVal.size();
1266f4a2713aSLionel Sambuc     // the sizes of src and dst vectors must be equal.
1267f4a2713aSLionel Sambuc     Dest.AggregateVal.resize(size);
1268f4a2713aSLionel Sambuc     for (unsigned i = 0; i < size; i++)
1269f4a2713aSLionel Sambuc       Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.zext(DBitWidth);
1270f4a2713aSLionel Sambuc   } else {
1271f4a2713aSLionel Sambuc     const IntegerType *DITy = cast<IntegerType>(DstTy);
1272f4a2713aSLionel Sambuc     unsigned DBitWidth = DITy->getBitWidth();
1273f4a2713aSLionel Sambuc     Dest.IntVal = Src.IntVal.zext(DBitWidth);
1274f4a2713aSLionel Sambuc   }
1275f4a2713aSLionel Sambuc   return Dest;
1276f4a2713aSLionel Sambuc }
1277f4a2713aSLionel Sambuc 
executeFPTruncInst(Value * SrcVal,Type * DstTy,ExecutionContext & SF)1278f4a2713aSLionel Sambuc GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy,
1279f4a2713aSLionel Sambuc                                              ExecutionContext &SF) {
1280f4a2713aSLionel Sambuc   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1281f4a2713aSLionel Sambuc 
1282f4a2713aSLionel Sambuc   if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
1283f4a2713aSLionel Sambuc     assert(SrcVal->getType()->getScalarType()->isDoubleTy() &&
1284f4a2713aSLionel Sambuc            DstTy->getScalarType()->isFloatTy() &&
1285f4a2713aSLionel Sambuc            "Invalid FPTrunc instruction");
1286f4a2713aSLionel Sambuc 
1287f4a2713aSLionel Sambuc     unsigned size = Src.AggregateVal.size();
1288f4a2713aSLionel Sambuc     // the sizes of src and dst vectors must be equal.
1289f4a2713aSLionel Sambuc     Dest.AggregateVal.resize(size);
1290f4a2713aSLionel Sambuc     for (unsigned i = 0; i < size; i++)
1291f4a2713aSLionel Sambuc       Dest.AggregateVal[i].FloatVal = (float)Src.AggregateVal[i].DoubleVal;
1292f4a2713aSLionel Sambuc   } else {
1293f4a2713aSLionel Sambuc     assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
1294f4a2713aSLionel Sambuc            "Invalid FPTrunc instruction");
1295f4a2713aSLionel Sambuc     Dest.FloatVal = (float)Src.DoubleVal;
1296f4a2713aSLionel Sambuc   }
1297f4a2713aSLionel Sambuc 
1298f4a2713aSLionel Sambuc   return Dest;
1299f4a2713aSLionel Sambuc }
1300f4a2713aSLionel Sambuc 
executeFPExtInst(Value * SrcVal,Type * DstTy,ExecutionContext & SF)1301f4a2713aSLionel Sambuc GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy,
1302f4a2713aSLionel Sambuc                                            ExecutionContext &SF) {
1303f4a2713aSLionel Sambuc   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1304f4a2713aSLionel Sambuc 
1305f4a2713aSLionel Sambuc   if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
1306f4a2713aSLionel Sambuc     assert(SrcVal->getType()->getScalarType()->isFloatTy() &&
1307f4a2713aSLionel Sambuc            DstTy->getScalarType()->isDoubleTy() && "Invalid FPExt instruction");
1308f4a2713aSLionel Sambuc 
1309f4a2713aSLionel Sambuc     unsigned size = Src.AggregateVal.size();
1310f4a2713aSLionel Sambuc     // the sizes of src and dst vectors must be equal.
1311f4a2713aSLionel Sambuc     Dest.AggregateVal.resize(size);
1312f4a2713aSLionel Sambuc     for (unsigned i = 0; i < size; i++)
1313f4a2713aSLionel Sambuc       Dest.AggregateVal[i].DoubleVal = (double)Src.AggregateVal[i].FloatVal;
1314f4a2713aSLionel Sambuc   } else {
1315f4a2713aSLionel Sambuc     assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&
1316f4a2713aSLionel Sambuc            "Invalid FPExt instruction");
1317f4a2713aSLionel Sambuc     Dest.DoubleVal = (double)Src.FloatVal;
1318f4a2713aSLionel Sambuc   }
1319f4a2713aSLionel Sambuc 
1320f4a2713aSLionel Sambuc   return Dest;
1321f4a2713aSLionel Sambuc }
1322f4a2713aSLionel Sambuc 
executeFPToUIInst(Value * SrcVal,Type * DstTy,ExecutionContext & SF)1323f4a2713aSLionel Sambuc GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy,
1324f4a2713aSLionel Sambuc                                             ExecutionContext &SF) {
1325f4a2713aSLionel Sambuc   Type *SrcTy = SrcVal->getType();
1326f4a2713aSLionel Sambuc   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1327f4a2713aSLionel Sambuc 
1328f4a2713aSLionel Sambuc   if (SrcTy->getTypeID() == Type::VectorTyID) {
1329f4a2713aSLionel Sambuc     const Type *DstVecTy = DstTy->getScalarType();
1330f4a2713aSLionel Sambuc     const Type *SrcVecTy = SrcTy->getScalarType();
1331f4a2713aSLionel Sambuc     uint32_t DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1332f4a2713aSLionel Sambuc     unsigned size = Src.AggregateVal.size();
1333f4a2713aSLionel Sambuc     // the sizes of src and dst vectors must be equal.
1334f4a2713aSLionel Sambuc     Dest.AggregateVal.resize(size);
1335f4a2713aSLionel Sambuc 
1336f4a2713aSLionel Sambuc     if (SrcVecTy->getTypeID() == Type::FloatTyID) {
1337f4a2713aSLionel Sambuc       assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToUI instruction");
1338f4a2713aSLionel Sambuc       for (unsigned i = 0; i < size; i++)
1339f4a2713aSLionel Sambuc         Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(
1340f4a2713aSLionel Sambuc             Src.AggregateVal[i].FloatVal, DBitWidth);
1341f4a2713aSLionel Sambuc     } else {
1342f4a2713aSLionel Sambuc       for (unsigned i = 0; i < size; i++)
1343f4a2713aSLionel Sambuc         Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(
1344f4a2713aSLionel Sambuc             Src.AggregateVal[i].DoubleVal, DBitWidth);
1345f4a2713aSLionel Sambuc     }
1346f4a2713aSLionel Sambuc   } else {
1347f4a2713aSLionel Sambuc     // scalar
1348f4a2713aSLionel Sambuc     uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1349f4a2713aSLionel Sambuc     assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction");
1350f4a2713aSLionel Sambuc 
1351f4a2713aSLionel Sambuc     if (SrcTy->getTypeID() == Type::FloatTyID)
1352f4a2713aSLionel Sambuc       Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1353f4a2713aSLionel Sambuc     else {
1354f4a2713aSLionel Sambuc       Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1355f4a2713aSLionel Sambuc     }
1356f4a2713aSLionel Sambuc   }
1357f4a2713aSLionel Sambuc 
1358f4a2713aSLionel Sambuc   return Dest;
1359f4a2713aSLionel Sambuc }
1360f4a2713aSLionel Sambuc 
executeFPToSIInst(Value * SrcVal,Type * DstTy,ExecutionContext & SF)1361f4a2713aSLionel Sambuc GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy,
1362f4a2713aSLionel Sambuc                                             ExecutionContext &SF) {
1363f4a2713aSLionel Sambuc   Type *SrcTy = SrcVal->getType();
1364f4a2713aSLionel Sambuc   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1365f4a2713aSLionel Sambuc 
1366f4a2713aSLionel Sambuc   if (SrcTy->getTypeID() == Type::VectorTyID) {
1367f4a2713aSLionel Sambuc     const Type *DstVecTy = DstTy->getScalarType();
1368f4a2713aSLionel Sambuc     const Type *SrcVecTy = SrcTy->getScalarType();
1369f4a2713aSLionel Sambuc     uint32_t DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1370f4a2713aSLionel Sambuc     unsigned size = Src.AggregateVal.size();
1371f4a2713aSLionel Sambuc     // the sizes of src and dst vectors must be equal
1372f4a2713aSLionel Sambuc     Dest.AggregateVal.resize(size);
1373f4a2713aSLionel Sambuc 
1374f4a2713aSLionel Sambuc     if (SrcVecTy->getTypeID() == Type::FloatTyID) {
1375f4a2713aSLionel Sambuc       assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1376f4a2713aSLionel Sambuc       for (unsigned i = 0; i < size; i++)
1377f4a2713aSLionel Sambuc         Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(
1378f4a2713aSLionel Sambuc             Src.AggregateVal[i].FloatVal, DBitWidth);
1379f4a2713aSLionel Sambuc     } else {
1380f4a2713aSLionel Sambuc       for (unsigned i = 0; i < size; i++)
1381f4a2713aSLionel Sambuc         Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(
1382f4a2713aSLionel Sambuc             Src.AggregateVal[i].DoubleVal, DBitWidth);
1383f4a2713aSLionel Sambuc     }
1384f4a2713aSLionel Sambuc   } else {
1385f4a2713aSLionel Sambuc     // scalar
1386f4a2713aSLionel Sambuc     unsigned DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1387f4a2713aSLionel Sambuc     assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1388f4a2713aSLionel Sambuc 
1389f4a2713aSLionel Sambuc     if (SrcTy->getTypeID() == Type::FloatTyID)
1390f4a2713aSLionel Sambuc       Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1391f4a2713aSLionel Sambuc     else {
1392f4a2713aSLionel Sambuc       Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1393f4a2713aSLionel Sambuc     }
1394f4a2713aSLionel Sambuc   }
1395f4a2713aSLionel Sambuc   return Dest;
1396f4a2713aSLionel Sambuc }
1397f4a2713aSLionel Sambuc 
executeUIToFPInst(Value * SrcVal,Type * DstTy,ExecutionContext & SF)1398f4a2713aSLionel Sambuc GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy,
1399f4a2713aSLionel Sambuc                                             ExecutionContext &SF) {
1400f4a2713aSLionel Sambuc   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1401f4a2713aSLionel Sambuc 
1402f4a2713aSLionel Sambuc   if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
1403f4a2713aSLionel Sambuc     const Type *DstVecTy = DstTy->getScalarType();
1404f4a2713aSLionel Sambuc     unsigned size = Src.AggregateVal.size();
1405f4a2713aSLionel Sambuc     // the sizes of src and dst vectors must be equal
1406f4a2713aSLionel Sambuc     Dest.AggregateVal.resize(size);
1407f4a2713aSLionel Sambuc 
1408f4a2713aSLionel Sambuc     if (DstVecTy->getTypeID() == Type::FloatTyID) {
1409f4a2713aSLionel Sambuc       assert(DstVecTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1410f4a2713aSLionel Sambuc       for (unsigned i = 0; i < size; i++)
1411f4a2713aSLionel Sambuc         Dest.AggregateVal[i].FloatVal =
1412f4a2713aSLionel Sambuc             APIntOps::RoundAPIntToFloat(Src.AggregateVal[i].IntVal);
1413f4a2713aSLionel Sambuc     } else {
1414f4a2713aSLionel Sambuc       for (unsigned i = 0; i < size; i++)
1415f4a2713aSLionel Sambuc         Dest.AggregateVal[i].DoubleVal =
1416f4a2713aSLionel Sambuc             APIntOps::RoundAPIntToDouble(Src.AggregateVal[i].IntVal);
1417f4a2713aSLionel Sambuc     }
1418f4a2713aSLionel Sambuc   } else {
1419f4a2713aSLionel Sambuc     // scalar
1420f4a2713aSLionel Sambuc     assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1421f4a2713aSLionel Sambuc     if (DstTy->getTypeID() == Type::FloatTyID)
1422f4a2713aSLionel Sambuc       Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
1423f4a2713aSLionel Sambuc     else {
1424f4a2713aSLionel Sambuc       Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
1425f4a2713aSLionel Sambuc     }
1426f4a2713aSLionel Sambuc   }
1427f4a2713aSLionel Sambuc   return Dest;
1428f4a2713aSLionel Sambuc }
1429f4a2713aSLionel Sambuc 
executeSIToFPInst(Value * SrcVal,Type * DstTy,ExecutionContext & SF)1430f4a2713aSLionel Sambuc GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy,
1431f4a2713aSLionel Sambuc                                             ExecutionContext &SF) {
1432f4a2713aSLionel Sambuc   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1433f4a2713aSLionel Sambuc 
1434f4a2713aSLionel Sambuc   if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
1435f4a2713aSLionel Sambuc     const Type *DstVecTy = DstTy->getScalarType();
1436f4a2713aSLionel Sambuc     unsigned size = Src.AggregateVal.size();
1437f4a2713aSLionel Sambuc     // the sizes of src and dst vectors must be equal
1438f4a2713aSLionel Sambuc     Dest.AggregateVal.resize(size);
1439f4a2713aSLionel Sambuc 
1440f4a2713aSLionel Sambuc     if (DstVecTy->getTypeID() == Type::FloatTyID) {
1441f4a2713aSLionel Sambuc       assert(DstVecTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1442f4a2713aSLionel Sambuc       for (unsigned i = 0; i < size; i++)
1443f4a2713aSLionel Sambuc         Dest.AggregateVal[i].FloatVal =
1444f4a2713aSLionel Sambuc             APIntOps::RoundSignedAPIntToFloat(Src.AggregateVal[i].IntVal);
1445f4a2713aSLionel Sambuc     } else {
1446f4a2713aSLionel Sambuc       for (unsigned i = 0; i < size; i++)
1447f4a2713aSLionel Sambuc         Dest.AggregateVal[i].DoubleVal =
1448f4a2713aSLionel Sambuc             APIntOps::RoundSignedAPIntToDouble(Src.AggregateVal[i].IntVal);
1449f4a2713aSLionel Sambuc     }
1450f4a2713aSLionel Sambuc   } else {
1451f4a2713aSLionel Sambuc     // scalar
1452f4a2713aSLionel Sambuc     assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1453f4a2713aSLionel Sambuc 
1454f4a2713aSLionel Sambuc     if (DstTy->getTypeID() == Type::FloatTyID)
1455f4a2713aSLionel Sambuc       Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
1456f4a2713aSLionel Sambuc     else {
1457f4a2713aSLionel Sambuc       Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
1458f4a2713aSLionel Sambuc     }
1459f4a2713aSLionel Sambuc   }
1460f4a2713aSLionel Sambuc 
1461f4a2713aSLionel Sambuc   return Dest;
1462f4a2713aSLionel Sambuc }
1463f4a2713aSLionel Sambuc 
executePtrToIntInst(Value * SrcVal,Type * DstTy,ExecutionContext & SF)1464f4a2713aSLionel Sambuc GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy,
1465f4a2713aSLionel Sambuc                                               ExecutionContext &SF) {
1466f4a2713aSLionel Sambuc   uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1467f4a2713aSLionel Sambuc   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1468f4a2713aSLionel Sambuc   assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction");
1469f4a2713aSLionel Sambuc 
1470f4a2713aSLionel Sambuc   Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1471f4a2713aSLionel Sambuc   return Dest;
1472f4a2713aSLionel Sambuc }
1473f4a2713aSLionel Sambuc 
executeIntToPtrInst(Value * SrcVal,Type * DstTy,ExecutionContext & SF)1474f4a2713aSLionel Sambuc GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy,
1475f4a2713aSLionel Sambuc                                               ExecutionContext &SF) {
1476f4a2713aSLionel Sambuc   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1477f4a2713aSLionel Sambuc   assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction");
1478f4a2713aSLionel Sambuc 
1479f4a2713aSLionel Sambuc   uint32_t PtrSize = TD.getPointerSizeInBits();
1480f4a2713aSLionel Sambuc   if (PtrSize != Src.IntVal.getBitWidth())
1481f4a2713aSLionel Sambuc     Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
1482f4a2713aSLionel Sambuc 
1483f4a2713aSLionel Sambuc   Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
1484f4a2713aSLionel Sambuc   return Dest;
1485f4a2713aSLionel Sambuc }
1486f4a2713aSLionel Sambuc 
executeBitCastInst(Value * SrcVal,Type * DstTy,ExecutionContext & SF)1487f4a2713aSLionel Sambuc GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy,
1488f4a2713aSLionel Sambuc                                              ExecutionContext &SF) {
1489f4a2713aSLionel Sambuc 
1490f4a2713aSLionel Sambuc   // This instruction supports bitwise conversion of vectors to integers and
1491f4a2713aSLionel Sambuc   // to vectors of other types (as long as they have the same size)
1492f4a2713aSLionel Sambuc   Type *SrcTy = SrcVal->getType();
1493f4a2713aSLionel Sambuc   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1494f4a2713aSLionel Sambuc 
1495f4a2713aSLionel Sambuc   if ((SrcTy->getTypeID() == Type::VectorTyID) ||
1496f4a2713aSLionel Sambuc       (DstTy->getTypeID() == Type::VectorTyID)) {
1497f4a2713aSLionel Sambuc     // vector src bitcast to vector dst or vector src bitcast to scalar dst or
1498f4a2713aSLionel Sambuc     // scalar src bitcast to vector dst
1499f4a2713aSLionel Sambuc     bool isLittleEndian = TD.isLittleEndian();
1500f4a2713aSLionel Sambuc     GenericValue TempDst, TempSrc, SrcVec;
1501f4a2713aSLionel Sambuc     const Type *SrcElemTy;
1502f4a2713aSLionel Sambuc     const Type *DstElemTy;
1503f4a2713aSLionel Sambuc     unsigned SrcBitSize;
1504f4a2713aSLionel Sambuc     unsigned DstBitSize;
1505f4a2713aSLionel Sambuc     unsigned SrcNum;
1506f4a2713aSLionel Sambuc     unsigned DstNum;
1507f4a2713aSLionel Sambuc 
1508f4a2713aSLionel Sambuc     if (SrcTy->getTypeID() == Type::VectorTyID) {
1509f4a2713aSLionel Sambuc       SrcElemTy = SrcTy->getScalarType();
1510f4a2713aSLionel Sambuc       SrcBitSize = SrcTy->getScalarSizeInBits();
1511f4a2713aSLionel Sambuc       SrcNum = Src.AggregateVal.size();
1512f4a2713aSLionel Sambuc       SrcVec = Src;
1513f4a2713aSLionel Sambuc     } else {
1514f4a2713aSLionel Sambuc       // if src is scalar value, make it vector <1 x type>
1515f4a2713aSLionel Sambuc       SrcElemTy = SrcTy;
1516f4a2713aSLionel Sambuc       SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1517f4a2713aSLionel Sambuc       SrcNum = 1;
1518f4a2713aSLionel Sambuc       SrcVec.AggregateVal.push_back(Src);
1519f4a2713aSLionel Sambuc     }
1520f4a2713aSLionel Sambuc 
1521f4a2713aSLionel Sambuc     if (DstTy->getTypeID() == Type::VectorTyID) {
1522f4a2713aSLionel Sambuc       DstElemTy = DstTy->getScalarType();
1523f4a2713aSLionel Sambuc       DstBitSize = DstTy->getScalarSizeInBits();
1524f4a2713aSLionel Sambuc       DstNum = (SrcNum * SrcBitSize) / DstBitSize;
1525f4a2713aSLionel Sambuc     } else {
1526f4a2713aSLionel Sambuc       DstElemTy = DstTy;
1527f4a2713aSLionel Sambuc       DstBitSize = DstTy->getPrimitiveSizeInBits();
1528f4a2713aSLionel Sambuc       DstNum = 1;
1529f4a2713aSLionel Sambuc     }
1530f4a2713aSLionel Sambuc 
1531f4a2713aSLionel Sambuc     if (SrcNum * SrcBitSize != DstNum * DstBitSize)
1532f4a2713aSLionel Sambuc       llvm_unreachable("Invalid BitCast");
1533f4a2713aSLionel Sambuc 
1534f4a2713aSLionel Sambuc     // If src is floating point, cast to integer first.
1535f4a2713aSLionel Sambuc     TempSrc.AggregateVal.resize(SrcNum);
1536f4a2713aSLionel Sambuc     if (SrcElemTy->isFloatTy()) {
1537f4a2713aSLionel Sambuc       for (unsigned i = 0; i < SrcNum; i++)
1538f4a2713aSLionel Sambuc         TempSrc.AggregateVal[i].IntVal =
1539f4a2713aSLionel Sambuc             APInt::floatToBits(SrcVec.AggregateVal[i].FloatVal);
1540f4a2713aSLionel Sambuc 
1541f4a2713aSLionel Sambuc     } else if (SrcElemTy->isDoubleTy()) {
1542f4a2713aSLionel Sambuc       for (unsigned i = 0; i < SrcNum; i++)
1543f4a2713aSLionel Sambuc         TempSrc.AggregateVal[i].IntVal =
1544f4a2713aSLionel Sambuc             APInt::doubleToBits(SrcVec.AggregateVal[i].DoubleVal);
1545f4a2713aSLionel Sambuc     } else if (SrcElemTy->isIntegerTy()) {
1546f4a2713aSLionel Sambuc       for (unsigned i = 0; i < SrcNum; i++)
1547f4a2713aSLionel Sambuc         TempSrc.AggregateVal[i].IntVal = SrcVec.AggregateVal[i].IntVal;
1548f4a2713aSLionel Sambuc     } else {
1549f4a2713aSLionel Sambuc       // Pointers are not allowed as the element type of vector.
1550f4a2713aSLionel Sambuc       llvm_unreachable("Invalid Bitcast");
1551f4a2713aSLionel Sambuc     }
1552f4a2713aSLionel Sambuc 
1553f4a2713aSLionel Sambuc     // now TempSrc is integer type vector
1554f4a2713aSLionel Sambuc     if (DstNum < SrcNum) {
1555f4a2713aSLionel Sambuc       // Example: bitcast <4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>
1556f4a2713aSLionel Sambuc       unsigned Ratio = SrcNum / DstNum;
1557f4a2713aSLionel Sambuc       unsigned SrcElt = 0;
1558f4a2713aSLionel Sambuc       for (unsigned i = 0; i < DstNum; i++) {
1559f4a2713aSLionel Sambuc         GenericValue Elt;
1560f4a2713aSLionel Sambuc         Elt.IntVal = 0;
1561f4a2713aSLionel Sambuc         Elt.IntVal = Elt.IntVal.zext(DstBitSize);
1562f4a2713aSLionel Sambuc         unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize * (Ratio - 1);
1563f4a2713aSLionel Sambuc         for (unsigned j = 0; j < Ratio; j++) {
1564f4a2713aSLionel Sambuc           APInt Tmp;
1565f4a2713aSLionel Sambuc           Tmp = Tmp.zext(SrcBitSize);
1566f4a2713aSLionel Sambuc           Tmp = TempSrc.AggregateVal[SrcElt++].IntVal;
1567f4a2713aSLionel Sambuc           Tmp = Tmp.zext(DstBitSize);
1568f4a2713aSLionel Sambuc           Tmp = Tmp.shl(ShiftAmt);
1569f4a2713aSLionel Sambuc           ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
1570f4a2713aSLionel Sambuc           Elt.IntVal |= Tmp;
1571f4a2713aSLionel Sambuc         }
1572f4a2713aSLionel Sambuc         TempDst.AggregateVal.push_back(Elt);
1573f4a2713aSLionel Sambuc       }
1574f4a2713aSLionel Sambuc     } else {
1575f4a2713aSLionel Sambuc       // Example: bitcast <2 x i64> <i64 0, i64 1> to <4 x i32>
1576f4a2713aSLionel Sambuc       unsigned Ratio = DstNum / SrcNum;
1577f4a2713aSLionel Sambuc       for (unsigned i = 0; i < SrcNum; i++) {
1578f4a2713aSLionel Sambuc         unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize * (Ratio - 1);
1579f4a2713aSLionel Sambuc         for (unsigned j = 0; j < Ratio; j++) {
1580f4a2713aSLionel Sambuc           GenericValue Elt;
1581f4a2713aSLionel Sambuc           Elt.IntVal = Elt.IntVal.zext(SrcBitSize);
1582f4a2713aSLionel Sambuc           Elt.IntVal = TempSrc.AggregateVal[i].IntVal;
1583f4a2713aSLionel Sambuc           Elt.IntVal = Elt.IntVal.lshr(ShiftAmt);
1584f4a2713aSLionel Sambuc           // it could be DstBitSize == SrcBitSize, so check it
1585f4a2713aSLionel Sambuc           if (DstBitSize < SrcBitSize)
1586f4a2713aSLionel Sambuc             Elt.IntVal = Elt.IntVal.trunc(DstBitSize);
1587f4a2713aSLionel Sambuc           ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
1588f4a2713aSLionel Sambuc           TempDst.AggregateVal.push_back(Elt);
1589f4a2713aSLionel Sambuc         }
1590f4a2713aSLionel Sambuc       }
1591f4a2713aSLionel Sambuc     }
1592f4a2713aSLionel Sambuc 
1593f4a2713aSLionel Sambuc     // convert result from integer to specified type
1594f4a2713aSLionel Sambuc     if (DstTy->getTypeID() == Type::VectorTyID) {
1595f4a2713aSLionel Sambuc       if (DstElemTy->isDoubleTy()) {
1596f4a2713aSLionel Sambuc         Dest.AggregateVal.resize(DstNum);
1597f4a2713aSLionel Sambuc         for (unsigned i = 0; i < DstNum; i++)
1598f4a2713aSLionel Sambuc           Dest.AggregateVal[i].DoubleVal =
1599f4a2713aSLionel Sambuc               TempDst.AggregateVal[i].IntVal.bitsToDouble();
1600f4a2713aSLionel Sambuc       } else if (DstElemTy->isFloatTy()) {
1601f4a2713aSLionel Sambuc         Dest.AggregateVal.resize(DstNum);
1602f4a2713aSLionel Sambuc         for (unsigned i = 0; i < DstNum; i++)
1603f4a2713aSLionel Sambuc           Dest.AggregateVal[i].FloatVal =
1604f4a2713aSLionel Sambuc               TempDst.AggregateVal[i].IntVal.bitsToFloat();
1605f4a2713aSLionel Sambuc       } else {
1606f4a2713aSLionel Sambuc         Dest = TempDst;
1607f4a2713aSLionel Sambuc       }
1608f4a2713aSLionel Sambuc     } else {
1609f4a2713aSLionel Sambuc       if (DstElemTy->isDoubleTy())
1610f4a2713aSLionel Sambuc         Dest.DoubleVal = TempDst.AggregateVal[0].IntVal.bitsToDouble();
1611f4a2713aSLionel Sambuc       else if (DstElemTy->isFloatTy()) {
1612f4a2713aSLionel Sambuc         Dest.FloatVal = TempDst.AggregateVal[0].IntVal.bitsToFloat();
1613f4a2713aSLionel Sambuc       } else {
1614f4a2713aSLionel Sambuc         Dest.IntVal = TempDst.AggregateVal[0].IntVal;
1615f4a2713aSLionel Sambuc       }
1616f4a2713aSLionel Sambuc     }
1617f4a2713aSLionel Sambuc   } else { //  if ((SrcTy->getTypeID() == Type::VectorTyID) ||
1618f4a2713aSLionel Sambuc            //     (DstTy->getTypeID() == Type::VectorTyID))
1619f4a2713aSLionel Sambuc 
1620f4a2713aSLionel Sambuc     // scalar src bitcast to scalar dst
1621f4a2713aSLionel Sambuc     if (DstTy->isPointerTy()) {
1622f4a2713aSLionel Sambuc       assert(SrcTy->isPointerTy() && "Invalid BitCast");
1623f4a2713aSLionel Sambuc       Dest.PointerVal = Src.PointerVal;
1624f4a2713aSLionel Sambuc     } else if (DstTy->isIntegerTy()) {
1625f4a2713aSLionel Sambuc       if (SrcTy->isFloatTy())
1626f4a2713aSLionel Sambuc         Dest.IntVal = APInt::floatToBits(Src.FloatVal);
1627f4a2713aSLionel Sambuc       else if (SrcTy->isDoubleTy()) {
1628f4a2713aSLionel Sambuc         Dest.IntVal = APInt::doubleToBits(Src.DoubleVal);
1629f4a2713aSLionel Sambuc       } else if (SrcTy->isIntegerTy()) {
1630f4a2713aSLionel Sambuc         Dest.IntVal = Src.IntVal;
1631f4a2713aSLionel Sambuc       } else {
1632f4a2713aSLionel Sambuc         llvm_unreachable("Invalid BitCast");
1633f4a2713aSLionel Sambuc       }
1634f4a2713aSLionel Sambuc     } else if (DstTy->isFloatTy()) {
1635f4a2713aSLionel Sambuc       if (SrcTy->isIntegerTy())
1636f4a2713aSLionel Sambuc         Dest.FloatVal = Src.IntVal.bitsToFloat();
1637f4a2713aSLionel Sambuc       else {
1638f4a2713aSLionel Sambuc         Dest.FloatVal = Src.FloatVal;
1639f4a2713aSLionel Sambuc       }
1640f4a2713aSLionel Sambuc     } else if (DstTy->isDoubleTy()) {
1641f4a2713aSLionel Sambuc       if (SrcTy->isIntegerTy())
1642f4a2713aSLionel Sambuc         Dest.DoubleVal = Src.IntVal.bitsToDouble();
1643f4a2713aSLionel Sambuc       else {
1644f4a2713aSLionel Sambuc         Dest.DoubleVal = Src.DoubleVal;
1645f4a2713aSLionel Sambuc       }
1646f4a2713aSLionel Sambuc     } else {
1647f4a2713aSLionel Sambuc       llvm_unreachable("Invalid Bitcast");
1648f4a2713aSLionel Sambuc     }
1649f4a2713aSLionel Sambuc   }
1650f4a2713aSLionel Sambuc 
1651f4a2713aSLionel Sambuc   return Dest;
1652f4a2713aSLionel Sambuc }
1653f4a2713aSLionel Sambuc 
visitTruncInst(TruncInst & I)1654f4a2713aSLionel Sambuc void Interpreter::visitTruncInst(TruncInst &I) {
1655f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1656f4a2713aSLionel Sambuc   SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1657f4a2713aSLionel Sambuc }
1658f4a2713aSLionel Sambuc 
visitSExtInst(SExtInst & I)1659f4a2713aSLionel Sambuc void Interpreter::visitSExtInst(SExtInst &I) {
1660f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1661f4a2713aSLionel Sambuc   SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1662f4a2713aSLionel Sambuc }
1663f4a2713aSLionel Sambuc 
visitZExtInst(ZExtInst & I)1664f4a2713aSLionel Sambuc void Interpreter::visitZExtInst(ZExtInst &I) {
1665f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1666f4a2713aSLionel Sambuc   SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1667f4a2713aSLionel Sambuc }
1668f4a2713aSLionel Sambuc 
visitFPTruncInst(FPTruncInst & I)1669f4a2713aSLionel Sambuc void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1670f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1671f4a2713aSLionel Sambuc   SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1672f4a2713aSLionel Sambuc }
1673f4a2713aSLionel Sambuc 
visitFPExtInst(FPExtInst & I)1674f4a2713aSLionel Sambuc void Interpreter::visitFPExtInst(FPExtInst &I) {
1675f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1676f4a2713aSLionel Sambuc   SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1677f4a2713aSLionel Sambuc }
1678f4a2713aSLionel Sambuc 
visitUIToFPInst(UIToFPInst & I)1679f4a2713aSLionel Sambuc void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1680f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1681f4a2713aSLionel Sambuc   SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1682f4a2713aSLionel Sambuc }
1683f4a2713aSLionel Sambuc 
visitSIToFPInst(SIToFPInst & I)1684f4a2713aSLionel Sambuc void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1685f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1686f4a2713aSLionel Sambuc   SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1687f4a2713aSLionel Sambuc }
1688f4a2713aSLionel Sambuc 
visitFPToUIInst(FPToUIInst & I)1689f4a2713aSLionel Sambuc void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1690f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1691f4a2713aSLionel Sambuc   SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1692f4a2713aSLionel Sambuc }
1693f4a2713aSLionel Sambuc 
visitFPToSIInst(FPToSIInst & I)1694f4a2713aSLionel Sambuc void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1695f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1696f4a2713aSLionel Sambuc   SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1697f4a2713aSLionel Sambuc }
1698f4a2713aSLionel Sambuc 
visitPtrToIntInst(PtrToIntInst & I)1699f4a2713aSLionel Sambuc void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1700f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1701f4a2713aSLionel Sambuc   SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1702f4a2713aSLionel Sambuc }
1703f4a2713aSLionel Sambuc 
visitIntToPtrInst(IntToPtrInst & I)1704f4a2713aSLionel Sambuc void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1705f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1706f4a2713aSLionel Sambuc   SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1707f4a2713aSLionel Sambuc }
1708f4a2713aSLionel Sambuc 
visitBitCastInst(BitCastInst & I)1709f4a2713aSLionel Sambuc void Interpreter::visitBitCastInst(BitCastInst &I) {
1710f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1711f4a2713aSLionel Sambuc   SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1712f4a2713aSLionel Sambuc }
1713f4a2713aSLionel Sambuc 
1714f4a2713aSLionel Sambuc #define IMPLEMENT_VAARG(TY) \
1715f4a2713aSLionel Sambuc    case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1716f4a2713aSLionel Sambuc 
visitVAArgInst(VAArgInst & I)1717f4a2713aSLionel Sambuc void Interpreter::visitVAArgInst(VAArgInst &I) {
1718f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1719f4a2713aSLionel Sambuc 
1720f4a2713aSLionel Sambuc   // Get the incoming valist parameter.  LLI treats the valist as a
1721f4a2713aSLionel Sambuc   // (ec-stack-depth var-arg-index) pair.
1722f4a2713aSLionel Sambuc   GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1723f4a2713aSLionel Sambuc   GenericValue Dest;
1724f4a2713aSLionel Sambuc   GenericValue Src = ECStack[VAList.UIntPairVal.first]
1725f4a2713aSLionel Sambuc                       .VarArgs[VAList.UIntPairVal.second];
1726f4a2713aSLionel Sambuc   Type *Ty = I.getType();
1727f4a2713aSLionel Sambuc   switch (Ty->getTypeID()) {
1728f4a2713aSLionel Sambuc   case Type::IntegerTyID:
1729f4a2713aSLionel Sambuc     Dest.IntVal = Src.IntVal;
1730f4a2713aSLionel Sambuc     break;
1731f4a2713aSLionel Sambuc   IMPLEMENT_VAARG(Pointer);
1732f4a2713aSLionel Sambuc   IMPLEMENT_VAARG(Float);
1733f4a2713aSLionel Sambuc   IMPLEMENT_VAARG(Double);
1734f4a2713aSLionel Sambuc   default:
1735f4a2713aSLionel Sambuc     dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1736*0a6a1f1dSLionel Sambuc     llvm_unreachable(nullptr);
1737f4a2713aSLionel Sambuc   }
1738f4a2713aSLionel Sambuc 
1739f4a2713aSLionel Sambuc   // Set the Value of this Instruction.
1740f4a2713aSLionel Sambuc   SetValue(&I, Dest, SF);
1741f4a2713aSLionel Sambuc 
1742f4a2713aSLionel Sambuc   // Move the pointer to the next vararg.
1743f4a2713aSLionel Sambuc   ++VAList.UIntPairVal.second;
1744f4a2713aSLionel Sambuc }
1745f4a2713aSLionel Sambuc 
visitExtractElementInst(ExtractElementInst & I)1746f4a2713aSLionel Sambuc void Interpreter::visitExtractElementInst(ExtractElementInst &I) {
1747f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1748f4a2713aSLionel Sambuc   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1749f4a2713aSLionel Sambuc   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1750f4a2713aSLionel Sambuc   GenericValue Dest;
1751f4a2713aSLionel Sambuc 
1752f4a2713aSLionel Sambuc   Type *Ty = I.getType();
1753f4a2713aSLionel Sambuc   const unsigned indx = unsigned(Src2.IntVal.getZExtValue());
1754f4a2713aSLionel Sambuc 
1755f4a2713aSLionel Sambuc   if(Src1.AggregateVal.size() > indx) {
1756f4a2713aSLionel Sambuc     switch (Ty->getTypeID()) {
1757f4a2713aSLionel Sambuc     default:
1758f4a2713aSLionel Sambuc       dbgs() << "Unhandled destination type for extractelement instruction: "
1759f4a2713aSLionel Sambuc       << *Ty << "\n";
1760*0a6a1f1dSLionel Sambuc       llvm_unreachable(nullptr);
1761f4a2713aSLionel Sambuc       break;
1762f4a2713aSLionel Sambuc     case Type::IntegerTyID:
1763f4a2713aSLionel Sambuc       Dest.IntVal = Src1.AggregateVal[indx].IntVal;
1764f4a2713aSLionel Sambuc       break;
1765f4a2713aSLionel Sambuc     case Type::FloatTyID:
1766f4a2713aSLionel Sambuc       Dest.FloatVal = Src1.AggregateVal[indx].FloatVal;
1767f4a2713aSLionel Sambuc       break;
1768f4a2713aSLionel Sambuc     case Type::DoubleTyID:
1769f4a2713aSLionel Sambuc       Dest.DoubleVal = Src1.AggregateVal[indx].DoubleVal;
1770f4a2713aSLionel Sambuc       break;
1771f4a2713aSLionel Sambuc     }
1772f4a2713aSLionel Sambuc   } else {
1773f4a2713aSLionel Sambuc     dbgs() << "Invalid index in extractelement instruction\n";
1774f4a2713aSLionel Sambuc   }
1775f4a2713aSLionel Sambuc 
1776f4a2713aSLionel Sambuc   SetValue(&I, Dest, SF);
1777f4a2713aSLionel Sambuc }
1778f4a2713aSLionel Sambuc 
visitInsertElementInst(InsertElementInst & I)1779f4a2713aSLionel Sambuc void Interpreter::visitInsertElementInst(InsertElementInst &I) {
1780f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1781f4a2713aSLionel Sambuc   Type *Ty = I.getType();
1782f4a2713aSLionel Sambuc 
1783f4a2713aSLionel Sambuc   if(!(Ty->isVectorTy()) )
1784f4a2713aSLionel Sambuc     llvm_unreachable("Unhandled dest type for insertelement instruction");
1785f4a2713aSLionel Sambuc 
1786f4a2713aSLionel Sambuc   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1787f4a2713aSLionel Sambuc   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1788f4a2713aSLionel Sambuc   GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
1789f4a2713aSLionel Sambuc   GenericValue Dest;
1790f4a2713aSLionel Sambuc 
1791f4a2713aSLionel Sambuc   Type *TyContained = Ty->getContainedType(0);
1792f4a2713aSLionel Sambuc 
1793f4a2713aSLionel Sambuc   const unsigned indx = unsigned(Src3.IntVal.getZExtValue());
1794f4a2713aSLionel Sambuc   Dest.AggregateVal = Src1.AggregateVal;
1795f4a2713aSLionel Sambuc 
1796f4a2713aSLionel Sambuc   if(Src1.AggregateVal.size() <= indx)
1797f4a2713aSLionel Sambuc       llvm_unreachable("Invalid index in insertelement instruction");
1798f4a2713aSLionel Sambuc   switch (TyContained->getTypeID()) {
1799f4a2713aSLionel Sambuc     default:
1800f4a2713aSLionel Sambuc       llvm_unreachable("Unhandled dest type for insertelement instruction");
1801f4a2713aSLionel Sambuc     case Type::IntegerTyID:
1802f4a2713aSLionel Sambuc       Dest.AggregateVal[indx].IntVal = Src2.IntVal;
1803f4a2713aSLionel Sambuc       break;
1804f4a2713aSLionel Sambuc     case Type::FloatTyID:
1805f4a2713aSLionel Sambuc       Dest.AggregateVal[indx].FloatVal = Src2.FloatVal;
1806f4a2713aSLionel Sambuc       break;
1807f4a2713aSLionel Sambuc     case Type::DoubleTyID:
1808f4a2713aSLionel Sambuc       Dest.AggregateVal[indx].DoubleVal = Src2.DoubleVal;
1809f4a2713aSLionel Sambuc       break;
1810f4a2713aSLionel Sambuc   }
1811f4a2713aSLionel Sambuc   SetValue(&I, Dest, SF);
1812f4a2713aSLionel Sambuc }
1813f4a2713aSLionel Sambuc 
visitShuffleVectorInst(ShuffleVectorInst & I)1814f4a2713aSLionel Sambuc void Interpreter::visitShuffleVectorInst(ShuffleVectorInst &I){
1815f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1816f4a2713aSLionel Sambuc 
1817f4a2713aSLionel Sambuc   Type *Ty = I.getType();
1818f4a2713aSLionel Sambuc   if(!(Ty->isVectorTy()))
1819f4a2713aSLionel Sambuc     llvm_unreachable("Unhandled dest type for shufflevector instruction");
1820f4a2713aSLionel Sambuc 
1821f4a2713aSLionel Sambuc   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1822f4a2713aSLionel Sambuc   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1823f4a2713aSLionel Sambuc   GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
1824f4a2713aSLionel Sambuc   GenericValue Dest;
1825f4a2713aSLionel Sambuc 
1826f4a2713aSLionel Sambuc   // There is no need to check types of src1 and src2, because the compiled
1827f4a2713aSLionel Sambuc   // bytecode can't contain different types for src1 and src2 for a
1828f4a2713aSLionel Sambuc   // shufflevector instruction.
1829f4a2713aSLionel Sambuc 
1830f4a2713aSLionel Sambuc   Type *TyContained = Ty->getContainedType(0);
1831f4a2713aSLionel Sambuc   unsigned src1Size = (unsigned)Src1.AggregateVal.size();
1832f4a2713aSLionel Sambuc   unsigned src2Size = (unsigned)Src2.AggregateVal.size();
1833f4a2713aSLionel Sambuc   unsigned src3Size = (unsigned)Src3.AggregateVal.size();
1834f4a2713aSLionel Sambuc 
1835f4a2713aSLionel Sambuc   Dest.AggregateVal.resize(src3Size);
1836f4a2713aSLionel Sambuc 
1837f4a2713aSLionel Sambuc   switch (TyContained->getTypeID()) {
1838f4a2713aSLionel Sambuc     default:
1839f4a2713aSLionel Sambuc       llvm_unreachable("Unhandled dest type for insertelement instruction");
1840f4a2713aSLionel Sambuc       break;
1841f4a2713aSLionel Sambuc     case Type::IntegerTyID:
1842f4a2713aSLionel Sambuc       for( unsigned i=0; i<src3Size; i++) {
1843f4a2713aSLionel Sambuc         unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
1844f4a2713aSLionel Sambuc         if(j < src1Size)
1845f4a2713aSLionel Sambuc           Dest.AggregateVal[i].IntVal = Src1.AggregateVal[j].IntVal;
1846f4a2713aSLionel Sambuc         else if(j < src1Size + src2Size)
1847f4a2713aSLionel Sambuc           Dest.AggregateVal[i].IntVal = Src2.AggregateVal[j-src1Size].IntVal;
1848f4a2713aSLionel Sambuc         else
1849f4a2713aSLionel Sambuc           // The selector may not be greater than sum of lengths of first and
1850f4a2713aSLionel Sambuc           // second operands and llasm should not allow situation like
1851f4a2713aSLionel Sambuc           // %tmp = shufflevector <2 x i32> <i32 3, i32 4>, <2 x i32> undef,
1852f4a2713aSLionel Sambuc           //                      <2 x i32> < i32 0, i32 5 >,
1853f4a2713aSLionel Sambuc           // where i32 5 is invalid, but let it be additional check here:
1854f4a2713aSLionel Sambuc           llvm_unreachable("Invalid mask in shufflevector instruction");
1855f4a2713aSLionel Sambuc       }
1856f4a2713aSLionel Sambuc       break;
1857f4a2713aSLionel Sambuc     case Type::FloatTyID:
1858f4a2713aSLionel Sambuc       for( unsigned i=0; i<src3Size; i++) {
1859f4a2713aSLionel Sambuc         unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
1860f4a2713aSLionel Sambuc         if(j < src1Size)
1861f4a2713aSLionel Sambuc           Dest.AggregateVal[i].FloatVal = Src1.AggregateVal[j].FloatVal;
1862f4a2713aSLionel Sambuc         else if(j < src1Size + src2Size)
1863f4a2713aSLionel Sambuc           Dest.AggregateVal[i].FloatVal = Src2.AggregateVal[j-src1Size].FloatVal;
1864f4a2713aSLionel Sambuc         else
1865f4a2713aSLionel Sambuc           llvm_unreachable("Invalid mask in shufflevector instruction");
1866f4a2713aSLionel Sambuc         }
1867f4a2713aSLionel Sambuc       break;
1868f4a2713aSLionel Sambuc     case Type::DoubleTyID:
1869f4a2713aSLionel Sambuc       for( unsigned i=0; i<src3Size; i++) {
1870f4a2713aSLionel Sambuc         unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
1871f4a2713aSLionel Sambuc         if(j < src1Size)
1872f4a2713aSLionel Sambuc           Dest.AggregateVal[i].DoubleVal = Src1.AggregateVal[j].DoubleVal;
1873f4a2713aSLionel Sambuc         else if(j < src1Size + src2Size)
1874f4a2713aSLionel Sambuc           Dest.AggregateVal[i].DoubleVal =
1875f4a2713aSLionel Sambuc             Src2.AggregateVal[j-src1Size].DoubleVal;
1876f4a2713aSLionel Sambuc         else
1877f4a2713aSLionel Sambuc           llvm_unreachable("Invalid mask in shufflevector instruction");
1878f4a2713aSLionel Sambuc       }
1879f4a2713aSLionel Sambuc       break;
1880f4a2713aSLionel Sambuc   }
1881f4a2713aSLionel Sambuc   SetValue(&I, Dest, SF);
1882f4a2713aSLionel Sambuc }
1883f4a2713aSLionel Sambuc 
visitExtractValueInst(ExtractValueInst & I)1884f4a2713aSLionel Sambuc void Interpreter::visitExtractValueInst(ExtractValueInst &I) {
1885f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1886f4a2713aSLionel Sambuc   Value *Agg = I.getAggregateOperand();
1887f4a2713aSLionel Sambuc   GenericValue Dest;
1888f4a2713aSLionel Sambuc   GenericValue Src = getOperandValue(Agg, SF);
1889f4a2713aSLionel Sambuc 
1890f4a2713aSLionel Sambuc   ExtractValueInst::idx_iterator IdxBegin = I.idx_begin();
1891f4a2713aSLionel Sambuc   unsigned Num = I.getNumIndices();
1892f4a2713aSLionel Sambuc   GenericValue *pSrc = &Src;
1893f4a2713aSLionel Sambuc 
1894f4a2713aSLionel Sambuc   for (unsigned i = 0 ; i < Num; ++i) {
1895f4a2713aSLionel Sambuc     pSrc = &pSrc->AggregateVal[*IdxBegin];
1896f4a2713aSLionel Sambuc     ++IdxBegin;
1897f4a2713aSLionel Sambuc   }
1898f4a2713aSLionel Sambuc 
1899f4a2713aSLionel Sambuc   Type *IndexedType = ExtractValueInst::getIndexedType(Agg->getType(), I.getIndices());
1900f4a2713aSLionel Sambuc   switch (IndexedType->getTypeID()) {
1901f4a2713aSLionel Sambuc     default:
1902f4a2713aSLionel Sambuc       llvm_unreachable("Unhandled dest type for extractelement instruction");
1903f4a2713aSLionel Sambuc     break;
1904f4a2713aSLionel Sambuc     case Type::IntegerTyID:
1905f4a2713aSLionel Sambuc       Dest.IntVal = pSrc->IntVal;
1906f4a2713aSLionel Sambuc     break;
1907f4a2713aSLionel Sambuc     case Type::FloatTyID:
1908f4a2713aSLionel Sambuc       Dest.FloatVal = pSrc->FloatVal;
1909f4a2713aSLionel Sambuc     break;
1910f4a2713aSLionel Sambuc     case Type::DoubleTyID:
1911f4a2713aSLionel Sambuc       Dest.DoubleVal = pSrc->DoubleVal;
1912f4a2713aSLionel Sambuc     break;
1913f4a2713aSLionel Sambuc     case Type::ArrayTyID:
1914f4a2713aSLionel Sambuc     case Type::StructTyID:
1915f4a2713aSLionel Sambuc     case Type::VectorTyID:
1916f4a2713aSLionel Sambuc       Dest.AggregateVal = pSrc->AggregateVal;
1917f4a2713aSLionel Sambuc     break;
1918f4a2713aSLionel Sambuc     case Type::PointerTyID:
1919f4a2713aSLionel Sambuc       Dest.PointerVal = pSrc->PointerVal;
1920f4a2713aSLionel Sambuc     break;
1921f4a2713aSLionel Sambuc   }
1922f4a2713aSLionel Sambuc 
1923f4a2713aSLionel Sambuc   SetValue(&I, Dest, SF);
1924f4a2713aSLionel Sambuc }
1925f4a2713aSLionel Sambuc 
visitInsertValueInst(InsertValueInst & I)1926f4a2713aSLionel Sambuc void Interpreter::visitInsertValueInst(InsertValueInst &I) {
1927f4a2713aSLionel Sambuc 
1928f4a2713aSLionel Sambuc   ExecutionContext &SF = ECStack.back();
1929f4a2713aSLionel Sambuc   Value *Agg = I.getAggregateOperand();
1930f4a2713aSLionel Sambuc 
1931f4a2713aSLionel Sambuc   GenericValue Src1 = getOperandValue(Agg, SF);
1932f4a2713aSLionel Sambuc   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1933f4a2713aSLionel Sambuc   GenericValue Dest = Src1; // Dest is a slightly changed Src1
1934f4a2713aSLionel Sambuc 
1935f4a2713aSLionel Sambuc   ExtractValueInst::idx_iterator IdxBegin = I.idx_begin();
1936f4a2713aSLionel Sambuc   unsigned Num = I.getNumIndices();
1937f4a2713aSLionel Sambuc 
1938f4a2713aSLionel Sambuc   GenericValue *pDest = &Dest;
1939f4a2713aSLionel Sambuc   for (unsigned i = 0 ; i < Num; ++i) {
1940f4a2713aSLionel Sambuc     pDest = &pDest->AggregateVal[*IdxBegin];
1941f4a2713aSLionel Sambuc     ++IdxBegin;
1942f4a2713aSLionel Sambuc   }
1943f4a2713aSLionel Sambuc   // pDest points to the target value in the Dest now
1944f4a2713aSLionel Sambuc 
1945f4a2713aSLionel Sambuc   Type *IndexedType = ExtractValueInst::getIndexedType(Agg->getType(), I.getIndices());
1946f4a2713aSLionel Sambuc 
1947f4a2713aSLionel Sambuc   switch (IndexedType->getTypeID()) {
1948f4a2713aSLionel Sambuc     default:
1949f4a2713aSLionel Sambuc       llvm_unreachable("Unhandled dest type for insertelement instruction");
1950f4a2713aSLionel Sambuc     break;
1951f4a2713aSLionel Sambuc     case Type::IntegerTyID:
1952f4a2713aSLionel Sambuc       pDest->IntVal = Src2.IntVal;
1953f4a2713aSLionel Sambuc     break;
1954f4a2713aSLionel Sambuc     case Type::FloatTyID:
1955f4a2713aSLionel Sambuc       pDest->FloatVal = Src2.FloatVal;
1956f4a2713aSLionel Sambuc     break;
1957f4a2713aSLionel Sambuc     case Type::DoubleTyID:
1958f4a2713aSLionel Sambuc       pDest->DoubleVal = Src2.DoubleVal;
1959f4a2713aSLionel Sambuc     break;
1960f4a2713aSLionel Sambuc     case Type::ArrayTyID:
1961f4a2713aSLionel Sambuc     case Type::StructTyID:
1962f4a2713aSLionel Sambuc     case Type::VectorTyID:
1963f4a2713aSLionel Sambuc       pDest->AggregateVal = Src2.AggregateVal;
1964f4a2713aSLionel Sambuc     break;
1965f4a2713aSLionel Sambuc     case Type::PointerTyID:
1966f4a2713aSLionel Sambuc       pDest->PointerVal = Src2.PointerVal;
1967f4a2713aSLionel Sambuc     break;
1968f4a2713aSLionel Sambuc   }
1969f4a2713aSLionel Sambuc 
1970f4a2713aSLionel Sambuc   SetValue(&I, Dest, SF);
1971f4a2713aSLionel Sambuc }
1972f4a2713aSLionel Sambuc 
getConstantExprValue(ConstantExpr * CE,ExecutionContext & SF)1973f4a2713aSLionel Sambuc GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1974f4a2713aSLionel Sambuc                                                 ExecutionContext &SF) {
1975f4a2713aSLionel Sambuc   switch (CE->getOpcode()) {
1976f4a2713aSLionel Sambuc   case Instruction::Trunc:
1977f4a2713aSLionel Sambuc       return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1978f4a2713aSLionel Sambuc   case Instruction::ZExt:
1979f4a2713aSLionel Sambuc       return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1980f4a2713aSLionel Sambuc   case Instruction::SExt:
1981f4a2713aSLionel Sambuc       return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1982f4a2713aSLionel Sambuc   case Instruction::FPTrunc:
1983f4a2713aSLionel Sambuc       return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1984f4a2713aSLionel Sambuc   case Instruction::FPExt:
1985f4a2713aSLionel Sambuc       return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1986f4a2713aSLionel Sambuc   case Instruction::UIToFP:
1987f4a2713aSLionel Sambuc       return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1988f4a2713aSLionel Sambuc   case Instruction::SIToFP:
1989f4a2713aSLionel Sambuc       return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1990f4a2713aSLionel Sambuc   case Instruction::FPToUI:
1991f4a2713aSLionel Sambuc       return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1992f4a2713aSLionel Sambuc   case Instruction::FPToSI:
1993f4a2713aSLionel Sambuc       return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1994f4a2713aSLionel Sambuc   case Instruction::PtrToInt:
1995f4a2713aSLionel Sambuc       return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1996f4a2713aSLionel Sambuc   case Instruction::IntToPtr:
1997f4a2713aSLionel Sambuc       return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1998f4a2713aSLionel Sambuc   case Instruction::BitCast:
1999f4a2713aSLionel Sambuc       return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
2000f4a2713aSLionel Sambuc   case Instruction::GetElementPtr:
2001f4a2713aSLionel Sambuc     return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
2002f4a2713aSLionel Sambuc                                gep_type_end(CE), SF);
2003f4a2713aSLionel Sambuc   case Instruction::FCmp:
2004f4a2713aSLionel Sambuc   case Instruction::ICmp:
2005f4a2713aSLionel Sambuc     return executeCmpInst(CE->getPredicate(),
2006f4a2713aSLionel Sambuc                           getOperandValue(CE->getOperand(0), SF),
2007f4a2713aSLionel Sambuc                           getOperandValue(CE->getOperand(1), SF),
2008f4a2713aSLionel Sambuc                           CE->getOperand(0)->getType());
2009f4a2713aSLionel Sambuc   case Instruction::Select:
2010f4a2713aSLionel Sambuc     return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
2011f4a2713aSLionel Sambuc                              getOperandValue(CE->getOperand(1), SF),
2012f4a2713aSLionel Sambuc                              getOperandValue(CE->getOperand(2), SF),
2013f4a2713aSLionel Sambuc                              CE->getOperand(0)->getType());
2014f4a2713aSLionel Sambuc   default :
2015f4a2713aSLionel Sambuc     break;
2016f4a2713aSLionel Sambuc   }
2017f4a2713aSLionel Sambuc 
2018f4a2713aSLionel Sambuc   // The cases below here require a GenericValue parameter for the result
2019f4a2713aSLionel Sambuc   // so we initialize one, compute it and then return it.
2020f4a2713aSLionel Sambuc   GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
2021f4a2713aSLionel Sambuc   GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
2022f4a2713aSLionel Sambuc   GenericValue Dest;
2023f4a2713aSLionel Sambuc   Type * Ty = CE->getOperand(0)->getType();
2024f4a2713aSLionel Sambuc   switch (CE->getOpcode()) {
2025f4a2713aSLionel Sambuc   case Instruction::Add:  Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
2026f4a2713aSLionel Sambuc   case Instruction::Sub:  Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
2027f4a2713aSLionel Sambuc   case Instruction::Mul:  Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
2028f4a2713aSLionel Sambuc   case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
2029f4a2713aSLionel Sambuc   case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
2030f4a2713aSLionel Sambuc   case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
2031f4a2713aSLionel Sambuc   case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
2032f4a2713aSLionel Sambuc   case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
2033f4a2713aSLionel Sambuc   case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
2034f4a2713aSLionel Sambuc   case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
2035f4a2713aSLionel Sambuc   case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
2036f4a2713aSLionel Sambuc   case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
2037f4a2713aSLionel Sambuc   case Instruction::And:  Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
2038f4a2713aSLionel Sambuc   case Instruction::Or:   Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
2039f4a2713aSLionel Sambuc   case Instruction::Xor:  Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
2040f4a2713aSLionel Sambuc   case Instruction::Shl:
2041f4a2713aSLionel Sambuc     Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
2042f4a2713aSLionel Sambuc     break;
2043f4a2713aSLionel Sambuc   case Instruction::LShr:
2044f4a2713aSLionel Sambuc     Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
2045f4a2713aSLionel Sambuc     break;
2046f4a2713aSLionel Sambuc   case Instruction::AShr:
2047f4a2713aSLionel Sambuc     Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
2048f4a2713aSLionel Sambuc     break;
2049f4a2713aSLionel Sambuc   default:
2050f4a2713aSLionel Sambuc     dbgs() << "Unhandled ConstantExpr: " << *CE << "\n";
2051f4a2713aSLionel Sambuc     llvm_unreachable("Unhandled ConstantExpr");
2052f4a2713aSLionel Sambuc   }
2053f4a2713aSLionel Sambuc   return Dest;
2054f4a2713aSLionel Sambuc }
2055f4a2713aSLionel Sambuc 
getOperandValue(Value * V,ExecutionContext & SF)2056f4a2713aSLionel Sambuc GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
2057f4a2713aSLionel Sambuc   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
2058f4a2713aSLionel Sambuc     return getConstantExprValue(CE, SF);
2059f4a2713aSLionel Sambuc   } else if (Constant *CPV = dyn_cast<Constant>(V)) {
2060f4a2713aSLionel Sambuc     return getConstantValue(CPV);
2061f4a2713aSLionel Sambuc   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2062f4a2713aSLionel Sambuc     return PTOGV(getPointerToGlobal(GV));
2063f4a2713aSLionel Sambuc   } else {
2064f4a2713aSLionel Sambuc     return SF.Values[V];
2065f4a2713aSLionel Sambuc   }
2066f4a2713aSLionel Sambuc }
2067f4a2713aSLionel Sambuc 
2068f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2069f4a2713aSLionel Sambuc //                        Dispatch and Execution Code
2070f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2071f4a2713aSLionel Sambuc 
2072f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2073f4a2713aSLionel Sambuc // callFunction - Execute the specified function...
2074f4a2713aSLionel Sambuc //
callFunction(Function * F,const std::vector<GenericValue> & ArgVals)2075f4a2713aSLionel Sambuc void Interpreter::callFunction(Function *F,
2076f4a2713aSLionel Sambuc                                const std::vector<GenericValue> &ArgVals) {
2077*0a6a1f1dSLionel Sambuc   assert((ECStack.empty() || !ECStack.back().Caller.getInstruction() ||
2078f4a2713aSLionel Sambuc           ECStack.back().Caller.arg_size() == ArgVals.size()) &&
2079f4a2713aSLionel Sambuc          "Incorrect number of arguments passed into function call!");
2080f4a2713aSLionel Sambuc   // Make a new stack frame... and fill it in.
2081f4a2713aSLionel Sambuc   ECStack.push_back(ExecutionContext());
2082f4a2713aSLionel Sambuc   ExecutionContext &StackFrame = ECStack.back();
2083f4a2713aSLionel Sambuc   StackFrame.CurFunction = F;
2084f4a2713aSLionel Sambuc 
2085f4a2713aSLionel Sambuc   // Special handling for external functions.
2086f4a2713aSLionel Sambuc   if (F->isDeclaration()) {
2087f4a2713aSLionel Sambuc     GenericValue Result = callExternalFunction (F, ArgVals);
2088f4a2713aSLionel Sambuc     // Simulate a 'ret' instruction of the appropriate type.
2089f4a2713aSLionel Sambuc     popStackAndReturnValueToCaller (F->getReturnType (), Result);
2090f4a2713aSLionel Sambuc     return;
2091f4a2713aSLionel Sambuc   }
2092f4a2713aSLionel Sambuc 
2093f4a2713aSLionel Sambuc   // Get pointers to first LLVM BB & Instruction in function.
2094f4a2713aSLionel Sambuc   StackFrame.CurBB     = F->begin();
2095f4a2713aSLionel Sambuc   StackFrame.CurInst   = StackFrame.CurBB->begin();
2096f4a2713aSLionel Sambuc 
2097f4a2713aSLionel Sambuc   // Run through the function arguments and initialize their values...
2098f4a2713aSLionel Sambuc   assert((ArgVals.size() == F->arg_size() ||
2099f4a2713aSLionel Sambuc          (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
2100f4a2713aSLionel Sambuc          "Invalid number of values passed to function invocation!");
2101f4a2713aSLionel Sambuc 
2102f4a2713aSLionel Sambuc   // Handle non-varargs arguments...
2103f4a2713aSLionel Sambuc   unsigned i = 0;
2104f4a2713aSLionel Sambuc   for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
2105f4a2713aSLionel Sambuc        AI != E; ++AI, ++i)
2106f4a2713aSLionel Sambuc     SetValue(AI, ArgVals[i], StackFrame);
2107f4a2713aSLionel Sambuc 
2108f4a2713aSLionel Sambuc   // Handle varargs arguments...
2109f4a2713aSLionel Sambuc   StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
2110f4a2713aSLionel Sambuc }
2111f4a2713aSLionel Sambuc 
2112f4a2713aSLionel Sambuc 
run()2113f4a2713aSLionel Sambuc void Interpreter::run() {
2114f4a2713aSLionel Sambuc   while (!ECStack.empty()) {
2115f4a2713aSLionel Sambuc     // Interpret a single instruction & increment the "PC".
2116f4a2713aSLionel Sambuc     ExecutionContext &SF = ECStack.back();  // Current stack frame
2117f4a2713aSLionel Sambuc     Instruction &I = *SF.CurInst++;         // Increment before execute
2118f4a2713aSLionel Sambuc 
2119f4a2713aSLionel Sambuc     // Track the number of dynamic instructions executed.
2120f4a2713aSLionel Sambuc     ++NumDynamicInsts;
2121f4a2713aSLionel Sambuc 
2122f4a2713aSLionel Sambuc     DEBUG(dbgs() << "About to interpret: " << I);
2123f4a2713aSLionel Sambuc     visit(I);   // Dispatch to one of the visit* methods...
2124f4a2713aSLionel Sambuc #if 0
2125f4a2713aSLionel Sambuc     // This is not safe, as visiting the instruction could lower it and free I.
2126f4a2713aSLionel Sambuc DEBUG(
2127f4a2713aSLionel Sambuc     if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
2128f4a2713aSLionel Sambuc         I.getType() != Type::VoidTy) {
2129f4a2713aSLionel Sambuc       dbgs() << "  --> ";
2130f4a2713aSLionel Sambuc       const GenericValue &Val = SF.Values[&I];
2131f4a2713aSLionel Sambuc       switch (I.getType()->getTypeID()) {
2132f4a2713aSLionel Sambuc       default: llvm_unreachable("Invalid GenericValue Type");
2133f4a2713aSLionel Sambuc       case Type::VoidTyID:    dbgs() << "void"; break;
2134f4a2713aSLionel Sambuc       case Type::FloatTyID:   dbgs() << "float " << Val.FloatVal; break;
2135f4a2713aSLionel Sambuc       case Type::DoubleTyID:  dbgs() << "double " << Val.DoubleVal; break;
2136f4a2713aSLionel Sambuc       case Type::PointerTyID: dbgs() << "void* " << intptr_t(Val.PointerVal);
2137f4a2713aSLionel Sambuc         break;
2138f4a2713aSLionel Sambuc       case Type::IntegerTyID:
2139f4a2713aSLionel Sambuc         dbgs() << "i" << Val.IntVal.getBitWidth() << " "
2140f4a2713aSLionel Sambuc                << Val.IntVal.toStringUnsigned(10)
2141f4a2713aSLionel Sambuc                << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
2142f4a2713aSLionel Sambuc         break;
2143f4a2713aSLionel Sambuc       }
2144f4a2713aSLionel Sambuc     });
2145f4a2713aSLionel Sambuc #endif
2146f4a2713aSLionel Sambuc   }
2147f4a2713aSLionel Sambuc }
2148