blob: 68f118d117937ac93ab191ca6d3df2eaeeee0689 [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmand1c881a2005-04-21 22:43:08 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00009//
Chris Lattner92101ac2001-08-23 17:05:04 +000010// This file contains the actual instruction interpreter.
11//
12//===----------------------------------------------------------------------===//
13
Brian Gaeke63438cc2003-12-11 00:22:59 +000014#define DEBUG_TYPE "interpreter"
Chris Lattner92101ac2001-08-23 17:05:04 +000015#include "Interpreter.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner73011782003-12-28 09:44:37 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/Instructions.h"
Chris Lattner30483732004-06-20 07:49:54 +000019#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattner4af6de82003-11-25 20:44:56 +000020#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencere1aa0662007-03-03 06:22:22 +000021#include "llvm/ADT/APInt.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/ADT/Statistic.h"
23#include "llvm/Support/Debug.h"
Reid Spencera54b7cb2007-01-12 07:05:14 +000024#include "llvm/Support/MathExtras.h"
Jeff Cohen97af7512006-12-02 02:22:01 +000025#include <cmath>
Chris Lattner4af6de82003-11-25 20:44:56 +000026using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000027
Chris Lattnercecf56b2006-12-19 22:56:53 +000028STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
29static Interpreter *TheEE = 0;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner2e42d3a2001-10-15 05:51:48 +000031//===----------------------------------------------------------------------===//
Reid Spencere1aa0662007-03-03 06:22:22 +000032// Various Helper Functions
Chris Lattner39bb5b42001-10-15 13:25:40 +000033//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000034
Reid Spencere1aa0662007-03-03 06:22:22 +000035inline void initializeAPInt(GenericValue &GV, const Type* Ty,
36 ExecutionContext &SF) {
37 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty))
38 GV.APIntVal = SF.getAPInt(ITy->getBitWidth());
39}
Chris Lattner759d34f2004-04-20 16:43:21 +000040
Reid Spencere1aa0662007-03-03 06:22:22 +000041static inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
Reid Spencera42c7fd2007-01-20 20:12:29 +000042 // Determine if the value is signed or not
43 bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
44 // If its signed, extend the sign bits
45 if (isSigned)
46 Val |= ~ITy->getBitMask();
47 return Val;
48}
49
Reid Spencere1aa0662007-03-03 06:22:22 +000050static inline void maskToBitWidth(GenericValue& GV, unsigned BitWidth) {
51 uint64_t BitMask = ~(uint64_t)(0ull) >> (64-BitWidth);
52 if (BitWidth <= 8)
53 GV.Int8Val &= BitMask;
54 else if (BitWidth <= 16)
55 GV.Int16Val &= BitMask;
56 else if (BitWidth <= 32)
57 GV.Int32Val &= BitMask;
58 else if (BitWidth <= 64)
59 GV.Int64Val &= BitMask;
60 else {
61 assert(GV.APIntVal && "Unallocated GV.APIntVal");
62 *(GV.APIntVal) &= APInt::getAllOnesValue(BitWidth);
Chris Lattner39bb5b42001-10-15 13:25:40 +000063 }
64}
65
Chris Lattner39bb5b42001-10-15 13:25:40 +000066static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000067 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +000068}
69
Chris Lattner2e42d3a2001-10-15 05:51:48 +000070void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +000071 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000072}
73
Chris Lattner2adcd832002-05-03 19:52:30 +000074//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000075// Binary Instruction Implementations
76//===----------------------------------------------------------------------===//
77
78#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
79 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
80
Reid Spencera54b7cb2007-01-12 07:05:14 +000081#define IMPLEMENT_INTEGER_BINOP(OP, TY) \
82 case Type::IntegerTyID: { \
83 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
Reid Spencere1aa0662007-03-03 06:22:22 +000084 if (BitWidth == 1) {\
Reid Spencera54b7cb2007-01-12 07:05:14 +000085 Dest.Int1Val = Src1.Int1Val OP Src2.Int1Val; \
Reid Spencere1aa0662007-03-03 06:22:22 +000086 maskToBitWidth(Dest, BitWidth); \
87 } else if (BitWidth <= 8) {\
Reid Spencera54b7cb2007-01-12 07:05:14 +000088 Dest.Int8Val = Src1.Int8Val OP Src2.Int8Val; \
Reid Spencere1aa0662007-03-03 06:22:22 +000089 maskToBitWidth(Dest, BitWidth); \
90 } else if (BitWidth <= 16) {\
Reid Spencera54b7cb2007-01-12 07:05:14 +000091 Dest.Int16Val = Src1.Int16Val OP Src2.Int16Val; \
Reid Spencere1aa0662007-03-03 06:22:22 +000092 maskToBitWidth(Dest, BitWidth); \
93 } else if (BitWidth <= 32) {\
Reid Spencera54b7cb2007-01-12 07:05:14 +000094 Dest.Int32Val = Src1.Int32Val OP Src2.Int32Val; \
Reid Spencere1aa0662007-03-03 06:22:22 +000095 maskToBitWidth(Dest, BitWidth); \
96 } else if (BitWidth <= 64) {\
Reid Spencera54b7cb2007-01-12 07:05:14 +000097 Dest.Int64Val = Src1.Int64Val OP Src2.Int64Val; \
Reid Spencere1aa0662007-03-03 06:22:22 +000098 maskToBitWidth(Dest, BitWidth); \
99 } else \
100 *(Dest.APIntVal) = *(Src1.APIntVal) OP *(Src2.APIntVal); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000101 break; \
102 }
103
Reid Spencere1aa0662007-03-03 06:22:22 +0000104#define IMPLEMENT_SIGNED_BINOP(OP, TY, APOP) \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000105 if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
106 unsigned BitWidth = ITy->getBitWidth(); \
Reid Spencere1aa0662007-03-03 06:22:22 +0000107 if (BitWidth <= 8) { \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000108 Dest.Int8Val = ((int8_t)Src1.Int8Val) OP ((int8_t)Src2.Int8Val); \
Reid Spencere1aa0662007-03-03 06:22:22 +0000109 maskToBitWidth(Dest, BitWidth); \
110 } else if (BitWidth <= 16) { \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000111 Dest.Int16Val = ((int16_t)Src1.Int16Val) OP ((int16_t)Src2.Int16Val); \
Reid Spencere1aa0662007-03-03 06:22:22 +0000112 maskToBitWidth(Dest, BitWidth); \
113 } else if (BitWidth <= 32) { \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000114 Dest.Int32Val = ((int32_t)Src1.Int32Val) OP ((int32_t)Src2.Int32Val); \
Reid Spencere1aa0662007-03-03 06:22:22 +0000115 maskToBitWidth(Dest, BitWidth); \
116 } else if (BitWidth <= 64) { \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000117 Dest.Int64Val = ((int64_t)Src1.Int64Val) OP ((int64_t)Src2.Int64Val); \
Reid Spencere1aa0662007-03-03 06:22:22 +0000118 maskToBitWidth(Dest, BitWidth); \
119 } else \
120 *(Dest.APIntVal) = Src1.APIntVal->APOP(*(Src2.APIntVal)); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000121 } else { \
122 cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
123 abort(); \
124 }
125
Reid Spencere1aa0662007-03-03 06:22:22 +0000126#define IMPLEMENT_UNSIGNED_BINOP(OP, TY, APOP) \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000127 if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
128 unsigned BitWidth = ITy->getBitWidth(); \
Reid Spencere1aa0662007-03-03 06:22:22 +0000129 if (BitWidth <= 8) {\
Reid Spencera54b7cb2007-01-12 07:05:14 +0000130 Dest.Int8Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
Reid Spencere1aa0662007-03-03 06:22:22 +0000131 maskToBitWidth(Dest, BitWidth); \
132 } else if (BitWidth <= 16) {\
Reid Spencera54b7cb2007-01-12 07:05:14 +0000133 Dest.Int16Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
Reid Spencere1aa0662007-03-03 06:22:22 +0000134 maskToBitWidth(Dest, BitWidth); \
135 } else if (BitWidth <= 32) {\
Reid Spencera54b7cb2007-01-12 07:05:14 +0000136 Dest.Int32Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
Reid Spencere1aa0662007-03-03 06:22:22 +0000137 maskToBitWidth(Dest, BitWidth); \
138 } else if (BitWidth <= 64) {\
Reid Spencera54b7cb2007-01-12 07:05:14 +0000139 Dest.Int64Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
Reid Spencere1aa0662007-03-03 06:22:22 +0000140 maskToBitWidth(Dest, BitWidth); \
141 } else \
142 *(Dest.APIntVal) = Src1.APIntVal->APOP(*(Src2.APIntVal)); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000143 } else { \
144 cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
145 abort(); \
146 }
147
Reid Spencere1aa0662007-03-03 06:22:22 +0000148static void executeAddInst(GenericValue &Dest, GenericValue Src1,
149 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000150 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000151 IMPLEMENT_INTEGER_BINOP(+, Ty);
Chris Lattner92101ac2001-08-23 17:05:04 +0000152 IMPLEMENT_BINARY_OPERATOR(+, Float);
153 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000154 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000155 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000156 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000157 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000158}
159
Reid Spencere1aa0662007-03-03 06:22:22 +0000160static void executeSubInst(GenericValue &Dest, GenericValue Src1,
161 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000162 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000163 IMPLEMENT_INTEGER_BINOP(-, Ty);
Chris Lattner92101ac2001-08-23 17:05:04 +0000164 IMPLEMENT_BINARY_OPERATOR(-, Float);
165 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000166 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000167 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000168 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000169 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000170}
171
Reid Spencere1aa0662007-03-03 06:22:22 +0000172static void executeMulInst(GenericValue &Dest, GenericValue Src1,
173 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000174 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000175 IMPLEMENT_INTEGER_BINOP(*, Ty);
Chris Lattnerc2593162001-10-27 08:28:11 +0000176 IMPLEMENT_BINARY_OPERATOR(*, Float);
177 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000178 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000179 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000180 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000181 }
Chris Lattnerc2593162001-10-27 08:28:11 +0000182}
183
Reid Spencere1aa0662007-03-03 06:22:22 +0000184static void executeUDivInst(GenericValue &Dest, GenericValue Src1,
185 GenericValue Src2, const Type *Ty) {
186 IMPLEMENT_UNSIGNED_BINOP(/,Ty,udiv)
Reid Spencer1628cec2006-10-26 06:15:43 +0000187}
188
Reid Spencere1aa0662007-03-03 06:22:22 +0000189static void executeSDivInst(GenericValue &Dest, GenericValue Src1,
190 GenericValue Src2, const Type *Ty) {
191 IMPLEMENT_SIGNED_BINOP(/,Ty,sdiv)
Reid Spencer1628cec2006-10-26 06:15:43 +0000192}
193
Reid Spencere1aa0662007-03-03 06:22:22 +0000194static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
195 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000196 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000197 IMPLEMENT_BINARY_OPERATOR(/, Float);
198 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000199 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000200 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000201 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000202 }
Chris Lattnerbb76f022001-10-30 20:27:31 +0000203}
204
Reid Spencere1aa0662007-03-03 06:22:22 +0000205static void executeURemInst(GenericValue &Dest, GenericValue Src1,
206 GenericValue Src2, const Type *Ty) {
207 IMPLEMENT_UNSIGNED_BINOP(%,Ty,urem)
Reid Spencer0a783f72006-11-02 01:53:59 +0000208}
209
Reid Spencere1aa0662007-03-03 06:22:22 +0000210static void executeSRemInst(GenericValue &Dest, GenericValue Src1,
211 GenericValue Src2, const Type *Ty) {
212 IMPLEMENT_SIGNED_BINOP(%,Ty,srem)
Reid Spencer0a783f72006-11-02 01:53:59 +0000213}
214
Reid Spencere1aa0662007-03-03 06:22:22 +0000215static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
216 GenericValue Src2, const Type *Ty) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000217 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000218 case Type::FloatTyID:
219 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
220 break;
221 case Type::DoubleTyID:
222 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
223 break;
224 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000225 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000226 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000227 }
Chris Lattnerc2593162001-10-27 08:28:11 +0000228}
229
Reid Spencere1aa0662007-03-03 06:22:22 +0000230static void executeAndInst(GenericValue &Dest, GenericValue Src1,
231 GenericValue Src2, const Type *Ty) {
232 IMPLEMENT_UNSIGNED_BINOP(&,Ty,And)
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000233}
234
Reid Spencere1aa0662007-03-03 06:22:22 +0000235static void executeOrInst(GenericValue &Dest, GenericValue Src1,
236 GenericValue Src2, const Type *Ty) {
237 IMPLEMENT_UNSIGNED_BINOP(|,Ty,Or)
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000238}
239
Reid Spencere1aa0662007-03-03 06:22:22 +0000240static void executeXorInst(GenericValue &Dest, GenericValue Src1,
241 GenericValue Src2, const Type *Ty) {
242 IMPLEMENT_UNSIGNED_BINOP(^,Ty,Xor)
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000243}
244
Reid Spencera54b7cb2007-01-12 07:05:14 +0000245#define IMPLEMENT_SIGNED_ICMP(OP, TY) \
246 case Type::IntegerTyID: { \
Reid Spencera42c7fd2007-01-20 20:12:29 +0000247 const IntegerType* ITy = cast<IntegerType>(TY); \
248 unsigned BitWidth = ITy->getBitWidth(); \
249 int64_t LHS = 0, RHS = 0; \
250 if (BitWidth <= 8) { \
251 LHS = int64_t(doSignExtension(uint64_t(Src1.Int8Val), ITy)); \
252 RHS = int64_t(doSignExtension(uint64_t(Src2.Int8Val), ITy)); \
253 } else if (BitWidth <= 16) { \
254 LHS = int64_t(doSignExtension(uint64_t(Src1.Int16Val), ITy)); \
255 RHS = int64_t(doSignExtension(uint64_t(Src2.Int16Val), ITy)); \
256 } else if (BitWidth <= 32) { \
257 LHS = int64_t(doSignExtension(uint64_t(Src1.Int32Val), ITy)); \
258 RHS = int64_t(doSignExtension(uint64_t(Src2.Int32Val), ITy)); \
259 } else if (BitWidth <= 64) { \
260 LHS = int64_t(doSignExtension(uint64_t(Src1.Int64Val), ITy)); \
261 RHS = int64_t(doSignExtension(uint64_t(Src2.Int64Val), ITy)); \
262 } else { \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000263 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
264 abort(); \
265 } \
Reid Spencera42c7fd2007-01-20 20:12:29 +0000266 Dest.Int1Val = LHS OP RHS; \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000267 break; \
268 }
269
270#define IMPLEMENT_UNSIGNED_ICMP(OP, TY) \
271 case Type::IntegerTyID: { \
272 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
273 if (BitWidth == 1) \
274 Dest.Int1Val = ((uint8_t)Src1.Int1Val) OP ((uint8_t)Src2.Int1Val); \
275 else if (BitWidth <= 8) \
276 Dest.Int1Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
277 else if (BitWidth <= 16) \
278 Dest.Int1Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
279 else if (BitWidth <= 32) \
280 Dest.Int1Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
281 else if (BitWidth <= 64) \
282 Dest.Int1Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
283 else { \
284 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
285 abort(); \
286 } \
Reid Spencer65367b22007-01-18 02:12:51 +0000287 maskToBitWidth(Dest, BitWidth); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000288 break; \
289 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000290
Chris Lattnerfd506f52003-04-23 19:55:35 +0000291// Handle pointers specially because they must be compared with only as much
292// width as the host has. We _do not_ want to be comparing 64 bit values when
293// running on a 32-bit target, otherwise the upper 32 bits might mess up
294// comparisons if they contain garbage.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000295#define IMPLEMENT_POINTER_ICMP(OP) \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000296 case Type::PointerTyID: \
Reid Spencer4fe16d62007-01-11 18:21:29 +0000297 Dest.Int1Val = (void*)(intptr_t)Src1.PointerVal OP \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000298 (void*)(intptr_t)Src2.PointerVal; break
299
Reid Spencere4d87aa2006-12-23 06:05:41 +0000300static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
301 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000302 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000303 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000304 IMPLEMENT_UNSIGNED_ICMP(==, Ty);
305 IMPLEMENT_POINTER_ICMP(==);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000306 default:
307 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
308 abort();
309 }
310 return Dest;
311}
312
313static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
314 const Type *Ty) {
315 GenericValue Dest;
316 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000317 IMPLEMENT_UNSIGNED_ICMP(!=, Ty);
318 IMPLEMENT_POINTER_ICMP(!=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000319 default:
320 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
321 abort();
322 }
323 return Dest;
324}
325
326static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
327 const Type *Ty) {
328 GenericValue Dest;
329 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000330 IMPLEMENT_UNSIGNED_ICMP(<, Ty);
331 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000332 default:
333 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
334 abort();
335 }
336 return Dest;
337}
338
339static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
340 const Type *Ty) {
341 GenericValue Dest;
342 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000343 IMPLEMENT_SIGNED_ICMP(<, Ty);
344 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000345 default:
346 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
347 abort();
348 }
349 return Dest;
350}
351
352static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
353 const Type *Ty) {
354 GenericValue Dest;
355 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000356 IMPLEMENT_UNSIGNED_ICMP(>, Ty);
357 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000358 default:
359 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
360 abort();
361 }
362 return Dest;
363}
364
365static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
366 const Type *Ty) {
367 GenericValue Dest;
368 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000369 IMPLEMENT_SIGNED_ICMP(>, Ty);
370 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000371 default:
372 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
373 abort();
374 }
375 return Dest;
376}
377
378static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
379 const Type *Ty) {
380 GenericValue Dest;
381 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000382 IMPLEMENT_UNSIGNED_ICMP(<=, Ty);
383 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000384 default:
385 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
386 abort();
387 }
388 return Dest;
389}
390
391static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
392 const Type *Ty) {
393 GenericValue Dest;
394 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000395 IMPLEMENT_SIGNED_ICMP(<=, Ty);
396 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000397 default:
398 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
399 abort();
400 }
401 return Dest;
402}
403
404static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
405 const Type *Ty) {
406 GenericValue Dest;
407 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000408 IMPLEMENT_UNSIGNED_ICMP(>=,Ty);
409 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000410 default:
411 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
412 abort();
413 }
414 return Dest;
415}
416
417static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
418 const Type *Ty) {
419 GenericValue Dest;
420 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000421 IMPLEMENT_SIGNED_ICMP(>=, Ty);
422 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000423 default:
424 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
425 abort();
426 }
427 return Dest;
428}
429
Reid Spencere49661b2006-12-31 05:51:36 +0000430void Interpreter::visitICmpInst(ICmpInst &I) {
431 ExecutionContext &SF = ECStack.back();
432 const Type *Ty = I.getOperand(0)->getType();
433 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
434 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
435 GenericValue R; // Result
436
437 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000438 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
439 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
Reid Spencere49661b2006-12-31 05:51:36 +0000440 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
441 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
442 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
443 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
444 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
445 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
446 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
447 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
448 default:
449 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
450 abort();
451 }
452
453 SetValue(&I, R, SF);
454}
455
456#define IMPLEMENT_FCMP(OP, TY) \
Reid Spencer4fe16d62007-01-11 18:21:29 +0000457 case Type::TY##TyID: Dest.Int1Val = Src1.TY##Val OP Src2.TY##Val; break
Reid Spencere4d87aa2006-12-23 06:05:41 +0000458
Reid Spencera54b7cb2007-01-12 07:05:14 +0000459static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000460 const Type *Ty) {
461 GenericValue Dest;
462 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000463 IMPLEMENT_FCMP(==, Float);
464 IMPLEMENT_FCMP(==, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000465 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000466 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000467 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000468 }
469 return Dest;
470}
471
Reid Spencera54b7cb2007-01-12 07:05:14 +0000472static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000473 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000474 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000475 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000476 IMPLEMENT_FCMP(!=, Float);
477 IMPLEMENT_FCMP(!=, Double);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000478
Chris Lattner92101ac2001-08-23 17:05:04 +0000479 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000480 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000481 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000482 }
483 return Dest;
484}
485
Reid Spencera54b7cb2007-01-12 07:05:14 +0000486static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000487 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000488 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000489 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000490 IMPLEMENT_FCMP(<=, Float);
491 IMPLEMENT_FCMP(<=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000492 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000493 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000494 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000495 }
496 return Dest;
497}
498
Reid Spencera54b7cb2007-01-12 07:05:14 +0000499static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000500 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000501 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000502 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000503 IMPLEMENT_FCMP(>=, Float);
504 IMPLEMENT_FCMP(>=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000505 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000506 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000507 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000508 }
509 return Dest;
510}
511
Reid Spencera54b7cb2007-01-12 07:05:14 +0000512static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000513 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000514 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000515 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000516 IMPLEMENT_FCMP(<, Float);
517 IMPLEMENT_FCMP(<, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000518 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000519 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000520 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000521 }
522 return Dest;
523}
524
Reid Spencera54b7cb2007-01-12 07:05:14 +0000525static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000526 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000527 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000528 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000529 IMPLEMENT_FCMP(>, Float);
530 IMPLEMENT_FCMP(>, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000531 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000532 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000533 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000534 }
535 return Dest;
536}
537
Reid Spencera54b7cb2007-01-12 07:05:14 +0000538#define IMPLEMENT_UNORDERED(TY, X,Y) \
539 if (TY == Type::FloatTy) \
540 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
541 Dest.Int1Val = true; \
542 return Dest; \
543 } \
544 else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
545 Dest.Int1Val = true; \
546 return Dest; \
547 }
548
549
550static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
551 const Type *Ty) {
552 GenericValue Dest;
553 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
554 return executeFCMP_OEQ(Src1, Src2, Ty);
555}
556
557static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
558 const Type *Ty) {
559 GenericValue Dest;
560 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
561 return executeFCMP_ONE(Src1, Src2, Ty);
562}
563
564static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
565 const Type *Ty) {
566 GenericValue Dest;
567 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
568 return executeFCMP_OLE(Src1, Src2, Ty);
569}
570
571static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
572 const Type *Ty) {
573 GenericValue Dest;
574 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
575 return executeFCMP_OGE(Src1, Src2, Ty);
576}
577
578static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
579 const Type *Ty) {
580 GenericValue Dest;
581 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
582 return executeFCMP_OLT(Src1, Src2, Ty);
583}
584
585static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
586 const Type *Ty) {
587 GenericValue Dest;
588 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
589 return executeFCMP_OGT(Src1, Src2, Ty);
590}
591
592static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
593 const Type *Ty) {
594 GenericValue Dest;
595 if (Ty == Type::FloatTy)
596 Dest.Int1Val = (Src1.FloatVal == Src1.FloatVal &&
597 Src2.FloatVal == Src2.FloatVal);
598 else
599 Dest.Int1Val = (Src1.DoubleVal == Src1.DoubleVal &&
600 Src2.DoubleVal == Src2.DoubleVal);
601 return Dest;
602}
603
604static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
605 const Type *Ty) {
606 GenericValue Dest;
607 if (Ty == Type::FloatTy)
608 Dest.Int1Val = (Src1.FloatVal != Src1.FloatVal ||
609 Src2.FloatVal != Src2.FloatVal);
610 else
611 Dest.Int1Val = (Src1.DoubleVal != Src1.DoubleVal ||
612 Src2.DoubleVal != Src2.DoubleVal);
613 return Dest;
614}
615
Reid Spencere4d87aa2006-12-23 06:05:41 +0000616void Interpreter::visitFCmpInst(FCmpInst &I) {
617 ExecutionContext &SF = ECStack.back();
618 const Type *Ty = I.getOperand(0)->getType();
619 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
620 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
621 GenericValue R; // Result
622
623 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000624 case FCmpInst::FCMP_FALSE: R.Int1Val = false; break;
625 case FCmpInst::FCMP_TRUE: R.Int1Val = true; break;
626 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
627 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
628 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
629 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
630 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
631 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
632 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
633 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
634 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
635 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
636 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
637 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
638 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
639 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000640 default:
641 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
642 abort();
643 }
644
645 SetValue(&I, R, SF);
646}
647
Reid Spencere4d87aa2006-12-23 06:05:41 +0000648static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
649 GenericValue Src2, const Type *Ty) {
650 GenericValue Result;
651 switch (predicate) {
652 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
653 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
654 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
655 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
656 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
657 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
658 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
659 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
660 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
661 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000662 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
663 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
664 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
665 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
666 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
667 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
668 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
669 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
670 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
671 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
672 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
673 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
674 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
675 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000676 case FCmpInst::FCMP_FALSE: {
677 GenericValue Result;
Reid Spencer4fe16d62007-01-11 18:21:29 +0000678 Result.Int1Val = false;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000679 return Result;
680 }
681 case FCmpInst::FCMP_TRUE: {
682 GenericValue Result;
Reid Spencer4fe16d62007-01-11 18:21:29 +0000683 Result.Int1Val = true;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000684 return Result;
685 }
686 default:
687 cerr << "Unhandled Cmp predicate\n";
688 abort();
689 }
690}
691
Chris Lattnerd7916e92003-05-10 21:22:39 +0000692void Interpreter::visitBinaryOperator(BinaryOperator &I) {
693 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000694 const Type *Ty = I.getOperand(0)->getType();
695 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
696 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000697 GenericValue R; // Result
Reid Spencere1aa0662007-03-03 06:22:22 +0000698 initializeAPInt(R, Ty, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000699
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000700 switch (I.getOpcode()) {
Reid Spencere1aa0662007-03-03 06:22:22 +0000701 case Instruction::Add: executeAddInst (R, Src1, Src2, Ty); break;
702 case Instruction::Sub: executeSubInst (R, Src1, Src2, Ty); break;
703 case Instruction::Mul: executeMulInst (R, Src1, Src2, Ty); break;
704 case Instruction::UDiv: executeUDivInst (R, Src1, Src2, Ty); break;
705 case Instruction::SDiv: executeSDivInst (R, Src1, Src2, Ty); break;
706 case Instruction::FDiv: executeFDivInst (R, Src1, Src2, Ty); break;
707 case Instruction::URem: executeURemInst (R, Src1, Src2, Ty); break;
708 case Instruction::SRem: executeSRemInst (R, Src1, Src2, Ty); break;
709 case Instruction::FRem: executeFRemInst (R, Src1, Src2, Ty); break;
710 case Instruction::And: executeAndInst (R, Src1, Src2, Ty); break;
711 case Instruction::Or: executeOrInst (R, Src1, Src2, Ty); break;
712 case Instruction::Xor: executeXorInst (R, Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000713 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000714 cerr << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner02868352003-04-22 21:22:33 +0000715 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000716 }
717
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000718 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000719}
720
Misha Brukmand1c881a2005-04-21 22:43:08 +0000721static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000722 GenericValue Src3) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000723 return Src1.Int1Val ? Src2 : Src3;
Chris Lattner759d34f2004-04-20 16:43:21 +0000724}
725
726void Interpreter::visitSelectInst(SelectInst &I) {
727 ExecutionContext &SF = ECStack.back();
728 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
729 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
730 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
Reid Spencere1aa0662007-03-03 06:22:22 +0000731 GenericValue R;
732 initializeAPInt(R, I.getOperand(1)->getType(), SF);
733 R = executeSelectInst(Src1, Src2, Src3);
Chris Lattner759d34f2004-04-20 16:43:21 +0000734 SetValue(&I, R, SF);
735}
736
737
Chris Lattner92101ac2001-08-23 17:05:04 +0000738//===----------------------------------------------------------------------===//
739// Terminator Instruction Implementations
740//===----------------------------------------------------------------------===//
741
Chris Lattnere43db882001-10-27 04:15:57 +0000742void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000743 // runAtExitHandlers() assumes there are no stack frames, but
744 // if exit() was called, then it had a stack frame. Blow away
745 // the stack before interpreting atexit handlers.
746 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000747 runAtExitHandlers ();
Reid Spencere49661b2006-12-31 05:51:36 +0000748 exit (GV.Int32Val);
Chris Lattnere43db882001-10-27 04:15:57 +0000749}
750
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000751/// Pop the last stack frame off of ECStack and then copy the result
752/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000753/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000754/// CallInst if there was a previous stack frame. This method may
755/// invalidate any ECStack iterators you have. This method also takes
756/// care of switching to the normal destination BB, if we are returning
757/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000758///
759void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
760 GenericValue Result) {
761 // Pop the current stack frame.
762 ECStack.pop_back();
763
Misha Brukmand1c881a2005-04-21 22:43:08 +0000764 if (ECStack.empty()) { // Finished main. Put result into exit code...
Chris Lattner42a75512007-01-15 02:27:26 +0000765 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000766 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000767 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000768 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000769 }
770 } else {
771 // If we have a previous stack frame, and we have a previous call,
772 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000773 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000774 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000775 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000776 SetValue(I, Result, CallingSF);
777 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
778 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000779 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000780 }
781 }
782}
783
Chris Lattnerd7916e92003-05-10 21:22:39 +0000784void Interpreter::visitReturnInst(ReturnInst &I) {
785 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000786 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000787 GenericValue Result;
788
789 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000790 if (I.getNumOperands()) {
791 RetTy = I.getReturnValue()->getType();
792 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000793 }
794
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000795 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000796}
797
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000798void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000799 // Unwind stack
800 Instruction *Inst;
801 do {
802 ECStack.pop_back ();
803 if (ECStack.empty ())
804 abort ();
805 Inst = ECStack.back ().Caller.getInstruction ();
806 } while (!(Inst && isa<InvokeInst> (Inst)));
807
808 // Return from invoke
809 ExecutionContext &InvokingSF = ECStack.back ();
810 InvokingSF.Caller = CallSite ();
811
812 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000813 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000814}
815
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000816void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000817 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000818 abort();
819}
820
Chris Lattnerd7916e92003-05-10 21:22:39 +0000821void Interpreter::visitBranchInst(BranchInst &I) {
822 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000823 BasicBlock *Dest;
824
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000825 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
826 if (!I.isUnconditional()) {
827 Value *Cond = I.getCondition();
Reid Spencer4fe16d62007-01-11 18:21:29 +0000828 if (getOperandValue(Cond, SF).Int1Val == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000829 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000830 }
Chris Lattner77113b62003-05-10 20:21:16 +0000831 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000832}
833
Chris Lattnerd7916e92003-05-10 21:22:39 +0000834void Interpreter::visitSwitchInst(SwitchInst &I) {
835 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000836 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
837 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000838
839 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000840 BasicBlock *Dest = 0;
841 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Reid Spencere4d87aa2006-12-23 06:05:41 +0000842 if (executeICMP_EQ(CondVal,
Reid Spencer4fe16d62007-01-11 18:21:29 +0000843 getOperandValue(I.getOperand(i), SF), ElTy).Int1Val) {
Chris Lattner09e93922003-04-22 20:34:47 +0000844 Dest = cast<BasicBlock>(I.getOperand(i+1));
845 break;
846 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000847
Chris Lattner09e93922003-04-22 20:34:47 +0000848 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000849 SwitchToNewBasicBlock(Dest, SF);
850}
851
852// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
853// This function handles the actual updating of block and instruction iterators
854// as well as execution of all of the PHI nodes in the destination block.
855//
856// This method does this because all of the PHI nodes must be executed
857// atomically, reading their inputs before any of the results are updated. Not
858// doing this can cause problems if the PHI nodes depend on other PHI nodes for
859// their inputs. If the input PHI node is updated before it is read, incorrect
860// results can happen. Thus we use a two phase approach.
861//
862void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
863 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
864 SF.CurBB = Dest; // Update CurBB to branch destination
865 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
866
867 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
868
869 // Loop over all of the PHI nodes in the current block, reading their inputs.
870 std::vector<GenericValue> ResultValues;
871
872 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
873 // Search for the value corresponding to this previous bb...
874 int i = PN->getBasicBlockIndex(PrevBB);
875 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
876 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000877
Chris Lattner77113b62003-05-10 20:21:16 +0000878 // Save the incoming value for this PHI node...
879 ResultValues.push_back(getOperandValue(IncomingValue, SF));
880 }
881
882 // Now loop over all of the PHI nodes setting their values...
883 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000884 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
885 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000886 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000887 }
Chris Lattner09e93922003-04-22 20:34:47 +0000888}
889
Chris Lattner92101ac2001-08-23 17:05:04 +0000890//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000891// Memory Instruction Implementations
892//===----------------------------------------------------------------------===//
893
Chris Lattnerd7916e92003-05-10 21:22:39 +0000894void Interpreter::visitAllocationInst(AllocationInst &I) {
895 ExecutionContext &SF = ECStack.back();
896
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000897 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000898
Chris Lattnercc82cc12002-04-28 21:57:33 +0000899 // Get the number of elements being allocated by the array...
Reid Spencere49661b2006-12-31 05:51:36 +0000900 unsigned NumElements = getOperandValue(I.getOperand(0), SF).Int32Val;
Chris Lattner86660982001-08-27 05:16:50 +0000901
902 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +0000903 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000904
Chris Lattnerfe11a972002-12-23 23:59:41 +0000905 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000906 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000907 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000908
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000909 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000910 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000911}
912
Chris Lattnerd7916e92003-05-10 21:22:39 +0000913void Interpreter::visitFreeInst(FreeInst &I) {
914 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000915 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
916 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000917 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000918 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000919}
920
Chris Lattnera34c5682002-08-27 22:33:45 +0000921// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000922//
Chris Lattner4af6de82003-11-25 20:44:56 +0000923GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000924 gep_type_iterator E,
925 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000926 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000927 "Cannot getElementOffset of a nonpointer type!");
928
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000929 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000930
931 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000932 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000933 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000934
Reid Spencerb83eb642006-10-20 07:07:24 +0000935 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
936 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000937
Chris Lattnerb1919e22007-02-10 19:55:17 +0000938 Total += (PointerTy)SLO->getElementOffset(Index);
Chris Lattner4af6de82003-11-25 20:44:56 +0000939 } else {
940 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000941 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000942 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
943
Reid Spencera54b7cb2007-01-12 07:05:14 +0000944 int64_t Idx;
945 unsigned BitWidth =
946 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
Reid Spencer23e28832007-01-18 01:25:42 +0000947 if (BitWidth == 32)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000948 Idx = (int64_t)(int32_t)IdxGV.Int32Val;
Reid Spencer23e28832007-01-18 01:25:42 +0000949 else if (BitWidth == 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000950 Idx = (int64_t)IdxGV.Int64Val;
951 else
Reid Spencer23e28832007-01-18 01:25:42 +0000952 assert(0 && "Invalid index type for getelementptr");
Chris Lattnerd5644962005-01-08 20:05:34 +0000953 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000954 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000955 }
956
Chris Lattnera34c5682002-08-27 22:33:45 +0000957 GenericValue Result;
958 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
959 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000960}
961
Chris Lattnerd7916e92003-05-10 21:22:39 +0000962void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
963 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000964 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000965 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000966}
967
Chris Lattnerd7916e92003-05-10 21:22:39 +0000968void Interpreter::visitLoadInst(LoadInst &I) {
969 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000970 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000971 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000972 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000973 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000974}
975
Chris Lattnerd7916e92003-05-10 21:22:39 +0000976void Interpreter::visitStoreInst(StoreInst &I) {
977 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000978 GenericValue Val = getOperandValue(I.getOperand(0), SF);
979 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000980 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000981 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000982}
983
Chris Lattner86660982001-08-27 05:16:50 +0000984//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000985// Miscellaneous Instruction Implementations
986//===----------------------------------------------------------------------===//
987
Brian Gaekefea483d2003-11-07 20:04:22 +0000988void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000989 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000990
991 // Check to see if this is an intrinsic function call...
992 if (Function *F = CS.getCalledFunction())
Reid Spencer5cbf9852007-01-30 20:08:39 +0000993 if (F->isDeclaration ())
Chris Lattner73011782003-12-28 09:44:37 +0000994 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000995 case Intrinsic::not_intrinsic:
996 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000997 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000998 GenericValue ArgIndex;
999 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1000 ArgIndex.UIntPairVal.second = 0;
1001 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +00001002 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +00001003 }
Chris Lattner317201d2004-03-13 00:24:00 +00001004 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +00001005 return;
Chris Lattner317201d2004-03-13 00:24:00 +00001006 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +00001007 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1008 return;
1009 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +00001010 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +00001011 // class to transform it into hopefully tasty LLVM code.
1012 //
1013 Instruction *Prev = CS.getInstruction()->getPrev();
1014 BasicBlock *Parent = CS.getInstruction()->getParent();
1015 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1016
1017 // Restore the CurInst pointer to the first instruction newly inserted, if
1018 // any.
1019 if (!Prev) {
1020 SF.CurInst = Parent->begin();
1021 } else {
1022 SF.CurInst = Prev;
1023 ++SF.CurInst;
1024 }
Brian Gaekeb440dea2004-04-23 18:05:28 +00001025 return;
Chris Lattner73011782003-12-28 09:44:37 +00001026 }
1027
Brian Gaekefea483d2003-11-07 20:04:22 +00001028 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +00001029 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +00001030 const unsigned NumArgs = SF.Caller.arg_size();
1031 ArgVals.reserve(NumArgs);
1032 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1033 e = SF.Caller.arg_end(); i != e; ++i) {
1034 Value *V = *i;
1035 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +00001036 // Promote all integral types whose size is < sizeof(int) into ints. We do
1037 // this by zero or sign extending the value as appropriate according to the
1038 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +00001039 const Type *Ty = V->getType();
Chris Lattner42a75512007-01-15 02:27:26 +00001040 if (Ty->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001041 if (Ty->getPrimitiveSizeInBits() == 1)
Reid Spencer4fe16d62007-01-11 18:21:29 +00001042 ArgVals.back().Int32Val = ArgVals.back().Int1Val;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001043 else if (Ty->getPrimitiveSizeInBits() <= 8)
1044 ArgVals.back().Int32Val = ArgVals.back().Int8Val;
1045 else if (Ty->getPrimitiveSizeInBits() <= 16)
1046 ArgVals.back().Int32Val = ArgVals.back().Int16Val;
Chris Lattner93780132003-01-13 00:58:52 +00001047 }
1048 }
Chris Lattner365a76e2001-09-10 04:49:44 +00001049
Misha Brukmand1c881a2005-04-21 22:43:08 +00001050 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001051 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +00001052 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +00001053 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +00001054}
1055
Brian Gaeke63438cc2003-12-11 00:22:59 +00001056static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
1057 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +00001058 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001059 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1060 unsigned BitWidth = ITy->getBitWidth();
Reid Spencer65367b22007-01-18 02:12:51 +00001061 if (BitWidth <= 8)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001062 Dest.Int8Val = ((uint8_t)Src1.Int8Val) << ((uint32_t)Src2.Int8Val);
Reid Spencer65367b22007-01-18 02:12:51 +00001063 else if (BitWidth <= 16)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001064 Dest.Int16Val = ((uint16_t)Src1.Int16Val) << ((uint32_t)Src2.Int8Val);
Reid Spencer65367b22007-01-18 02:12:51 +00001065 else if (BitWidth <= 32)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001066 Dest.Int32Val = ((uint32_t)Src1.Int32Val) << ((uint32_t)Src2.Int8Val);
Reid Spencer65367b22007-01-18 02:12:51 +00001067 else if (BitWidth <= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001068 Dest.Int64Val = ((uint64_t)Src1.Int64Val) << ((uint32_t)Src2.Int8Val);
Reid Spencer65367b22007-01-18 02:12:51 +00001069 else {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001070 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1071 abort();
1072 }
Reid Spencer65367b22007-01-18 02:12:51 +00001073 maskToBitWidth(Dest, BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001074 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001075 cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
Reid Spencera54b7cb2007-01-12 07:05:14 +00001076 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001077 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001078 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001079}
1080
Reid Spencer3822ff52006-11-08 06:47:33 +00001081static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
1082 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +00001083 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001084 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1085 unsigned BitWidth = ITy->getBitWidth();
1086 if (BitWidth <= 8)
1087 Dest.Int8Val = ((uint8_t)Src1.Int8Val) >> ((uint32_t)Src2.Int8Val);
1088 else if (BitWidth <= 16)
1089 Dest.Int16Val = ((uint16_t)Src1.Int16Val) >> ((uint32_t)Src2.Int8Val);
1090 else if (BitWidth <= 32)
1091 Dest.Int32Val = ((uint32_t)Src1.Int32Val) >> ((uint32_t)Src2.Int8Val);
1092 else if (BitWidth <= 64)
1093 Dest.Int64Val = ((uint64_t)Src1.Int64Val) >> ((uint32_t)Src2.Int8Val);
1094 else {
1095 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1096 abort();
1097 }
Reid Spencer65367b22007-01-18 02:12:51 +00001098 maskToBitWidth(Dest, BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001099 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001100 cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
Reid Spencer3822ff52006-11-08 06:47:33 +00001101 abort();
1102 }
1103 return Dest;
1104}
1105
1106static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
1107 const Type *Ty) {
1108 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001109 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1110 unsigned BitWidth = ITy->getBitWidth();
1111 if (BitWidth <= 8)
1112 Dest.Int8Val = ((int8_t)Src1.Int8Val) >> ((int32_t)Src2.Int8Val);
1113 else if (BitWidth <= 16)
1114 Dest.Int16Val = ((int16_t)Src1.Int16Val) >> ((int32_t)Src2.Int8Val);
1115 else if (BitWidth <= 32)
1116 Dest.Int32Val = ((int32_t)Src1.Int32Val) >> ((int32_t)Src2.Int8Val);
1117 else if (BitWidth <= 64)
1118 Dest.Int64Val = ((int64_t)Src1.Int64Val) >> ((int32_t)Src2.Int8Val);
1119 else {
1120 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
1121 abort();
1122 }
Reid Spencer65367b22007-01-18 02:12:51 +00001123 maskToBitWidth(Dest, BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001124 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001125 cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +00001126 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001127 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001128 return Dest;
1129}
1130
Reid Spencer832254e2007-02-02 02:16:23 +00001131void Interpreter::visitShl(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001132 ExecutionContext &SF = ECStack.back();
1133 const Type *Ty = I.getOperand(0)->getType();
1134 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1135 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1136 GenericValue Dest;
1137 Dest = executeShlInst (Src1, Src2, Ty);
1138 SetValue(&I, Dest, SF);
1139}
1140
Reid Spencer832254e2007-02-02 02:16:23 +00001141void Interpreter::visitLShr(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001142 ExecutionContext &SF = ECStack.back();
1143 const Type *Ty = I.getOperand(0)->getType();
1144 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1145 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1146 GenericValue Dest;
Reid Spencer3822ff52006-11-08 06:47:33 +00001147 Dest = executeLShrInst (Src1, Src2, Ty);
1148 SetValue(&I, Dest, SF);
1149}
1150
Reid Spencer832254e2007-02-02 02:16:23 +00001151void Interpreter::visitAShr(BinaryOperator &I) {
Reid Spencer3822ff52006-11-08 06:47:33 +00001152 ExecutionContext &SF = ECStack.back();
1153 const Type *Ty = I.getOperand(0)->getType();
1154 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1155 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1156 GenericValue Dest;
1157 Dest = executeAShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001158 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001159}
1160
Reid Spencer23e28832007-01-18 01:25:42 +00001161#define INTEGER_ASSIGN(DEST, BITWIDTH, VAL) \
1162 { \
Reid Spencer23cbb1c2007-02-08 00:29:31 +00001163 uint64_t Mask = ~(uint64_t)(0ull) >> (64-BITWIDTH); \
Reid Spencer23e28832007-01-18 01:25:42 +00001164 if (BITWIDTH == 1) { \
1165 Dest.Int1Val = (bool) (VAL & Mask); \
1166 } else if (BITWIDTH <= 8) { \
1167 Dest.Int8Val = (uint8_t) (VAL & Mask); \
1168 } else if (BITWIDTH <= 16) { \
1169 Dest.Int16Val = (uint16_t) (VAL & Mask); \
1170 } else if (BITWIDTH <= 32) { \
1171 Dest.Int32Val = (uint32_t) (VAL & Mask); \
1172 } else \
1173 Dest.Int64Val = (uint64_t) (VAL & Mask); \
1174 }
Chris Lattner86660982001-08-27 05:16:50 +00001175
Reid Spencera54b7cb2007-01-12 07:05:14 +00001176GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
1177 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001178 const Type *SrcTy = SrcVal->getType();
1179 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001180 const IntegerType *DITy = cast<IntegerType>(DstTy);
1181 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1182 unsigned DBitWidth = DITy->getBitWidth();
1183 unsigned SBitWidth = SITy->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001184 assert(SBitWidth > DBitWidth && "Invalid truncate");
Chris Lattner86660982001-08-27 05:16:50 +00001185
Reid Spencere1aa0662007-03-03 06:22:22 +00001186 if (DBitWidth > 64) {
1187 // Both values are APInt, just use the APInt trunc
1188 initializeAPInt(Dest, DstTy, SF);
1189 *(Dest.APIntVal) = Src.APIntVal->trunc(DBitWidth);
1190 return Dest;
1191 }
1192
1193 uint64_t MaskedVal = 0;
1194 uint64_t Mask = (1ULL << DBitWidth) - 1;
1195
Reid Spencera54b7cb2007-01-12 07:05:14 +00001196 // Mask the source value to its actual bit width. This ensures that any
1197 // high order bits are cleared.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001198 if (SBitWidth <= 8)
1199 MaskedVal = Src.Int8Val & Mask;
1200 else if (SBitWidth <= 16)
1201 MaskedVal = Src.Int16Val & Mask;
1202 else if (SBitWidth <= 32)
1203 MaskedVal = Src.Int32Val & Mask;
Reid Spencere1aa0662007-03-03 06:22:22 +00001204 else if (SBitWidth <= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001205 MaskedVal = Src.Int64Val & Mask;
Reid Spencere1aa0662007-03-03 06:22:22 +00001206 else
1207 MaskedVal = Src.APIntVal->trunc(DBitWidth).getZExtValue();
Chris Lattnera34c5682002-08-27 22:33:45 +00001208
Reid Spencera54b7cb2007-01-12 07:05:14 +00001209 INTEGER_ASSIGN(Dest, DBitWidth, MaskedVal);
Chris Lattnera34c5682002-08-27 22:33:45 +00001210 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001211}
Chris Lattner92101ac2001-08-23 17:05:04 +00001212
Reid Spencera54b7cb2007-01-12 07:05:14 +00001213GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
1214 ExecutionContext &SF) {
1215 const Type *SrcTy = SrcVal->getType();
1216 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1217 const IntegerType *DITy = cast<IntegerType>(DstTy);
1218 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1219 unsigned DBitWidth = DITy->getBitWidth();
1220 unsigned SBitWidth = SITy->getBitWidth();
1221 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1222 "Integer types > 64 bits not supported");
1223 assert(SBitWidth < DBitWidth && "Invalid sign extend");
Reid Spencerc00a4302007-01-20 08:32:52 +00001224
1225 // Normalize to a 64-bit value.
1226 uint64_t Normalized = 0;
1227 if (SBitWidth <= 8)
1228 Normalized = Src.Int8Val;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001229 else if (SBitWidth <= 16)
Reid Spencerc00a4302007-01-20 08:32:52 +00001230 Normalized = Src.Int16Val;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001231 else if (SBitWidth <= 32)
Reid Spencerc00a4302007-01-20 08:32:52 +00001232 Normalized = Src.Int32Val;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001233 else
Reid Spencerc00a4302007-01-20 08:32:52 +00001234 Normalized = Src.Int64Val;
1235
Reid Spencera42c7fd2007-01-20 20:12:29 +00001236 Normalized = doSignExtension(Normalized, SITy);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001237
1238 // Now that we have a sign extended value, assign it to the destination
Reid Spencerc00a4302007-01-20 08:32:52 +00001239 INTEGER_ASSIGN(Dest, DBitWidth, Normalized);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001240 return Dest;
1241}
1242
1243GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
1244 ExecutionContext &SF) {
1245 const Type *SrcTy = SrcVal->getType();
1246 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1247 const IntegerType *DITy = cast<IntegerType>(DstTy);
1248 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1249 unsigned DBitWidth = DITy->getBitWidth();
1250 unsigned SBitWidth = SITy->getBitWidth();
1251 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1252 "Integer types > 64 bits not supported");
1253 assert(SBitWidth < DBitWidth && "Invalid sign extend");
1254 uint64_t Extended = 0;
1255 if (SBitWidth == 1)
1256 // For sign extension from bool, we must extend the source bits.
1257 Extended = (uint64_t) (Src.Int1Val & 1);
1258 else if (SBitWidth <= 8)
1259 Extended = (uint64_t) (uint8_t)Src.Int8Val;
1260 else if (SBitWidth <= 16)
1261 Extended = (uint64_t) (uint16_t)Src.Int16Val;
1262 else if (SBitWidth <= 32)
1263 Extended = (uint64_t) (uint32_t)Src.Int32Val;
1264 else
1265 Extended = (uint64_t) Src.Int64Val;
1266
1267 // Now that we have a sign extended value, assign it to the destination
1268 INTEGER_ASSIGN(Dest, DBitWidth, Extended);
1269 return Dest;
1270}
1271
1272GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
1273 ExecutionContext &SF) {
1274 const Type *SrcTy = SrcVal->getType();
1275 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1276 assert(SrcTy == Type::DoubleTy && DstTy == Type::FloatTy &&
1277 "Invalid FPTrunc instruction");
1278 Dest.FloatVal = (float) Src.DoubleVal;
1279 return Dest;
1280}
1281
1282GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
1283 ExecutionContext &SF) {
1284 const Type *SrcTy = SrcVal->getType();
1285 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1286 assert(SrcTy == Type::FloatTy && DstTy == Type::DoubleTy &&
1287 "Invalid FPTrunc instruction");
1288 Dest.DoubleVal = (double) Src.FloatVal;
1289 return Dest;
1290}
1291
1292GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
1293 ExecutionContext &SF) {
1294 const Type *SrcTy = SrcVal->getType();
1295 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1296 const IntegerType *DITy = cast<IntegerType>(DstTy);
1297 unsigned DBitWidth = DITy->getBitWidth();
1298 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1299 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
1300 uint64_t Converted = 0;
1301 if (SrcTy->getTypeID() == Type::FloatTyID)
1302 Converted = (uint64_t) Src.FloatVal;
1303 else
1304 Converted = (uint64_t) Src.DoubleVal;
1305
1306 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1307 return Dest;
1308}
1309
1310GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1311 ExecutionContext &SF) {
1312 const Type *SrcTy = SrcVal->getType();
1313 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1314 const IntegerType *DITy = cast<IntegerType>(DstTy);
1315 unsigned DBitWidth = DITy->getBitWidth();
1316 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1317 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1318 int64_t Converted = 0;
1319 if (SrcTy->getTypeID() == Type::FloatTyID)
1320 Converted = (int64_t) Src.FloatVal;
1321 else
1322 Converted = (int64_t) Src.DoubleVal;
1323
1324 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1325 return Dest;
1326}
1327
1328GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1329 ExecutionContext &SF) {
1330 const Type *SrcTy = SrcVal->getType();
1331 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1332 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1333 unsigned SBitWidth = SITy->getBitWidth();
1334 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1335 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1336 uint64_t Converted = 0;
1337 if (SBitWidth == 1)
1338 Converted = (uint64_t) Src.Int1Val;
1339 else if (SBitWidth <= 8)
1340 Converted = (uint64_t) Src.Int8Val;
1341 else if (SBitWidth <= 16)
1342 Converted = (uint64_t) Src.Int16Val;
1343 else if (SBitWidth <= 32)
1344 Converted = (uint64_t) Src.Int32Val;
1345 else
1346 Converted = (uint64_t) Src.Int64Val;
1347
1348 if (DstTy->getTypeID() == Type::FloatTyID)
1349 Dest.FloatVal = (float) Converted;
1350 else
1351 Dest.DoubleVal = (double) Converted;
1352 return Dest;
1353}
1354
1355GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1356 ExecutionContext &SF) {
1357 const Type *SrcTy = SrcVal->getType();
1358 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1359 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1360 unsigned SBitWidth = SITy->getBitWidth();
1361 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
Reid Spencer24d6da52007-01-21 00:29:26 +00001362 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001363 int64_t Converted = 0;
1364 if (SBitWidth == 1)
1365 Converted = 0LL - Src.Int1Val;
1366 else if (SBitWidth <= 8)
1367 Converted = (int64_t) (int8_t)Src.Int8Val;
1368 else if (SBitWidth <= 16)
1369 Converted = (int64_t) (int16_t)Src.Int16Val;
1370 else if (SBitWidth <= 32)
1371 Converted = (int64_t) (int32_t)Src.Int32Val;
1372 else
1373 Converted = (int64_t) Src.Int64Val;
1374
1375 if (DstTy->getTypeID() == Type::FloatTyID)
1376 Dest.FloatVal = (float) Converted;
1377 else
1378 Dest.DoubleVal = (double) Converted;
1379 return Dest;
1380}
1381
1382GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1383 ExecutionContext &SF) {
1384 const Type *SrcTy = SrcVal->getType();
1385 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1386 const IntegerType *DITy = cast<IntegerType>(DstTy);
1387 unsigned DBitWidth = DITy->getBitWidth();
1388 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1389 assert(isa<PointerType>(SrcTy) && "Invalid PtrToInt instruction");
1390 INTEGER_ASSIGN(Dest, DBitWidth, (intptr_t) Src.PointerVal);
1391 return Dest;
1392}
1393
1394GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1395 ExecutionContext &SF) {
1396 const Type *SrcTy = SrcVal->getType();
1397 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1398 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1399 unsigned SBitWidth = SITy->getBitWidth();
1400 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1401 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1402 uint64_t Converted = 0;
1403 if (SBitWidth == 1)
1404 Converted = (uint64_t) Src.Int1Val;
1405 else if (SBitWidth <= 8)
1406 Converted = (uint64_t) Src.Int8Val;
1407 else if (SBitWidth <= 16)
1408 Converted = (uint64_t) Src.Int16Val;
1409 else if (SBitWidth <= 32)
1410 Converted = (uint64_t) Src.Int32Val;
1411 else
1412 Converted = (uint64_t) Src.Int64Val;
1413
1414 Dest.PointerVal = (PointerTy) Converted;
1415 return Dest;
1416}
1417
1418GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1419 ExecutionContext &SF) {
1420
1421 const Type *SrcTy = SrcVal->getType();
1422 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1423 if (isa<PointerType>(DstTy)) {
1424 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1425 Dest.PointerVal = Src.PointerVal;
Chris Lattner42a75512007-01-15 02:27:26 +00001426 } else if (DstTy->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001427 const IntegerType *DITy = cast<IntegerType>(DstTy);
1428 unsigned DBitWidth = DITy->getBitWidth();
1429 if (SrcTy == Type::FloatTy) {
1430 Dest.Int32Val = FloatToBits(Src.FloatVal);
1431 } else if (SrcTy == Type::DoubleTy) {
1432 Dest.Int64Val = DoubleToBits(Src.DoubleVal);
Chris Lattner42a75512007-01-15 02:27:26 +00001433 } else if (SrcTy->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001434 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1435 unsigned SBitWidth = SITy->getBitWidth();
1436 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1437 assert(SBitWidth == DBitWidth && "Invalid BitCast");
1438 if (SBitWidth == 1)
1439 Dest.Int1Val = Src.Int1Val;
1440 else if (SBitWidth <= 8)
1441 Dest.Int8Val = Src.Int8Val;
1442 else if (SBitWidth <= 16)
1443 Dest.Int16Val = Src.Int16Val;
1444 else if (SBitWidth <= 32)
1445 Dest.Int32Val = Src.Int32Val;
1446 else
1447 Dest.Int64Val = Src.Int64Val;
Reid Spencer65367b22007-01-18 02:12:51 +00001448 maskToBitWidth(Dest, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001449 } else
1450 assert(0 && "Invalid BitCast");
1451 } else if (DstTy == Type::FloatTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001452 if (SrcTy->isInteger())
Reid Spencera54b7cb2007-01-12 07:05:14 +00001453 Dest.FloatVal = BitsToFloat(Src.Int32Val);
1454 else
1455 Dest.FloatVal = Src.FloatVal;
1456 } else if (DstTy == Type::DoubleTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001457 if (SrcTy->isInteger())
Reid Spencera54b7cb2007-01-12 07:05:14 +00001458 Dest.DoubleVal = BitsToDouble(Src.Int64Val);
1459 else
1460 Dest.DoubleVal = Src.DoubleVal;
1461 } else
1462 assert(0 && "Invalid Bitcast");
1463
1464 return Dest;
1465}
1466
1467void Interpreter::visitTruncInst(TruncInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001468 ExecutionContext &SF = ECStack.back();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001469 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1470}
1471
1472void Interpreter::visitSExtInst(SExtInst &I) {
1473 ExecutionContext &SF = ECStack.back();
1474 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1475}
1476
1477void Interpreter::visitZExtInst(ZExtInst &I) {
1478 ExecutionContext &SF = ECStack.back();
1479 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1480}
1481
1482void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1483 ExecutionContext &SF = ECStack.back();
1484 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1485}
1486
1487void Interpreter::visitFPExtInst(FPExtInst &I) {
1488 ExecutionContext &SF = ECStack.back();
1489 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1490}
1491
1492void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1493 ExecutionContext &SF = ECStack.back();
1494 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1495}
1496
1497void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1498 ExecutionContext &SF = ECStack.back();
1499 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1500}
1501
1502void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1503 ExecutionContext &SF = ECStack.back();
1504 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1505}
1506
1507void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1508 ExecutionContext &SF = ECStack.back();
1509 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1510}
1511
1512void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1513 ExecutionContext &SF = ECStack.back();
1514 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1515}
1516
1517void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1518 ExecutionContext &SF = ECStack.back();
1519 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1520}
1521
1522void Interpreter::visitBitCastInst(BitCastInst &I) {
1523 ExecutionContext &SF = ECStack.back();
1524 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001525}
Chris Lattner92101ac2001-08-23 17:05:04 +00001526
Brian Gaekec1a2be12003-11-07 21:20:47 +00001527#define IMPLEMENT_VAARG(TY) \
1528 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1529
1530void Interpreter::visitVAArgInst(VAArgInst &I) {
1531 ExecutionContext &SF = ECStack.back();
1532
Brian Gaeke9d20b712004-02-25 23:01:48 +00001533 // Get the incoming valist parameter. LLI treats the valist as a
1534 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001535 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001536 GenericValue Dest;
1537 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001538 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001539 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001540 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001541 case Type::IntegerTyID: {
1542 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
1543 if (BitWidth == 1)
1544 Dest.Int1Val = Src.Int1Val;
1545 else if (BitWidth <= 8)
1546 Dest.Int8Val = Src.Int8Val;
1547 else if (BitWidth <= 16)
1548 Dest.Int16Val = Src.Int16Val;
1549 else if (BitWidth <= 32)
1550 Dest.Int32Val = Src.Int32Val;
1551 else if (BitWidth <= 64)
1552 Dest.Int64Val = Src.Int64Val;
1553 else
Chris Lattner64f150f2007-02-14 06:20:04 +00001554 assert(0 && "Integer types > 64 bits not supported");
Reid Spencer65367b22007-01-18 02:12:51 +00001555 maskToBitWidth(Dest, BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001556 }
Brian Gaekec1a2be12003-11-07 21:20:47 +00001557 IMPLEMENT_VAARG(Pointer);
1558 IMPLEMENT_VAARG(Float);
1559 IMPLEMENT_VAARG(Double);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001560 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001561 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Brian Gaekec1a2be12003-11-07 21:20:47 +00001562 abort();
1563 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001564
Brian Gaekec1a2be12003-11-07 21:20:47 +00001565 // Set the Value of this Instruction.
1566 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001567
1568 // Move the pointer to the next vararg.
1569 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001570}
1571
Reid Spencere1aa0662007-03-03 06:22:22 +00001572GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1573 ExecutionContext &SF) {
1574 switch (CE->getOpcode()) {
1575 case Instruction::Trunc:
1576 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1577 case Instruction::ZExt:
1578 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1579 case Instruction::SExt:
1580 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1581 case Instruction::FPTrunc:
1582 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1583 case Instruction::FPExt:
1584 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1585 case Instruction::UIToFP:
1586 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1587 case Instruction::SIToFP:
1588 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1589 case Instruction::FPToUI:
1590 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1591 case Instruction::FPToSI:
1592 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1593 case Instruction::PtrToInt:
1594 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1595 case Instruction::IntToPtr:
1596 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1597 case Instruction::BitCast:
1598 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1599 case Instruction::GetElementPtr:
1600 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1601 gep_type_end(CE), SF);
1602 default :
1603 break;
1604 }
1605 GenericValue Dest;
1606 initializeAPInt(Dest, CE->getType(), SF);
1607 switch (CE->getOpcode()) {
1608 case Instruction::Add:
1609 executeAddInst(Dest, getOperandValue(CE->getOperand(0), SF),
1610 getOperandValue(CE->getOperand(1), SF),
1611 CE->getOperand(0)->getType());
1612 case Instruction::Sub:
1613 executeSubInst(Dest, getOperandValue(CE->getOperand(0), SF),
1614 getOperandValue(CE->getOperand(1), SF),
1615 CE->getOperand(0)->getType());
1616 case Instruction::Mul:
1617 executeMulInst(Dest, getOperandValue(CE->getOperand(0), SF),
1618 getOperandValue(CE->getOperand(1), SF),
1619 CE->getOperand(0)->getType());
1620 case Instruction::SDiv:
1621 executeSDivInst(Dest, getOperandValue(CE->getOperand(0), SF),
1622 getOperandValue(CE->getOperand(1), SF),
1623 CE->getOperand(0)->getType());
1624 case Instruction::UDiv:
1625 executeUDivInst(Dest, getOperandValue(CE->getOperand(0), SF),
1626 getOperandValue(CE->getOperand(1), SF),
1627 CE->getOperand(0)->getType());
1628 case Instruction::FDiv:
1629 executeFDivInst(Dest, getOperandValue(CE->getOperand(0), SF),
1630 getOperandValue(CE->getOperand(1), SF),
1631 CE->getOperand(0)->getType());
1632 case Instruction::URem:
1633 executeURemInst(Dest, getOperandValue(CE->getOperand(0), SF),
1634 getOperandValue(CE->getOperand(1), SF),
1635 CE->getOperand(0)->getType());
1636 case Instruction::SRem:
1637 executeSRemInst(Dest, getOperandValue(CE->getOperand(0), SF),
1638 getOperandValue(CE->getOperand(1), SF),
1639 CE->getOperand(0)->getType());
1640 case Instruction::FRem:
1641 executeFRemInst(Dest, getOperandValue(CE->getOperand(0), SF),
1642 getOperandValue(CE->getOperand(1), SF),
1643 CE->getOperand(0)->getType());
1644 case Instruction::And:
1645 executeAndInst(Dest, getOperandValue(CE->getOperand(0), SF),
1646 getOperandValue(CE->getOperand(1), SF),
1647 CE->getOperand(0)->getType());
1648 case Instruction::Or:
1649 executeOrInst(Dest, getOperandValue(CE->getOperand(0), SF),
1650 getOperandValue(CE->getOperand(1), SF),
1651 CE->getOperand(0)->getType());
1652 case Instruction::Xor:
1653 executeXorInst(Dest, getOperandValue(CE->getOperand(0), SF),
1654 getOperandValue(CE->getOperand(1), SF),
1655 CE->getOperand(0)->getType());
1656 case Instruction::FCmp:
1657 case Instruction::ICmp:
1658 return executeCmpInst(CE->getPredicate(),
1659 getOperandValue(CE->getOperand(0), SF),
1660 getOperandValue(CE->getOperand(1), SF),
1661 CE->getOperand(0)->getType());
1662 case Instruction::Shl:
1663 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
1664 getOperandValue(CE->getOperand(1), SF),
1665 CE->getOperand(0)->getType());
1666 case Instruction::LShr:
1667 return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
1668 getOperandValue(CE->getOperand(1), SF),
1669 CE->getOperand(0)->getType());
1670 case Instruction::AShr:
1671 return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
1672 getOperandValue(CE->getOperand(1), SF),
1673 CE->getOperand(0)->getType());
1674 case Instruction::Select:
1675 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1676 getOperandValue(CE->getOperand(1), SF),
1677 getOperandValue(CE->getOperand(2), SF));
1678 default:
1679 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
1680 abort();
1681 return GenericValue();
1682 }
1683}
1684
1685GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1686 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1687 return getConstantExprValue(CE, SF);
1688 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1689 return getConstantValue(CPV);
1690 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1691 return PTOGV(getPointerToGlobal(GV));
1692 } else {
1693 return SF.Values[V];
1694 }
1695}
1696
Chris Lattner92101ac2001-08-23 17:05:04 +00001697//===----------------------------------------------------------------------===//
1698// Dispatch and Execution Code
1699//===----------------------------------------------------------------------===//
1700
Chris Lattner92101ac2001-08-23 17:05:04 +00001701//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001702// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001703//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001704void Interpreter::callFunction(Function *F,
1705 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001706 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1707 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1708 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001709 // Make a new stack frame... and fill it in.
1710 ECStack.push_back(ExecutionContext());
1711 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001712 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001713
1714 // Special handling for external functions.
Reid Spencer5cbf9852007-01-30 20:08:39 +00001715 if (F->isDeclaration()) {
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001716 GenericValue Result = callExternalFunction (F, ArgVals);
1717 // Simulate a 'ret' instruction of the appropriate type.
1718 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1719 return;
1720 }
1721
1722 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001723 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001724 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001725
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001726 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001727 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001728 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001729 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001730
1731 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001732 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001733 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001734 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001735
1736 // Handle varargs arguments...
1737 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001738}
1739
Chris Lattner92101ac2001-08-23 17:05:04 +00001740void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001741 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001742 // Interpret a single instruction & increment the "PC".
1743 ExecutionContext &SF = ECStack.back(); // Current stack frame
1744 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001745
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001746 // Track the number of dynamic instructions executed.
1747 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001748
Bill Wendling480f0932006-11-27 23:54:50 +00001749 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001750 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001751 }
1752}