blob: 16720b347d29bf9c323e4fa672ce10f3db5ddf6c [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 Spencere0929362007-03-03 08:38:04 +0000245#define IMPLEMENT_SIGNED_ICMP(OP, TY, APOP) \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000246 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)); \
Reid Spencere0929362007-03-03 08:38:04 +0000253 Dest.Int1Val = LHS OP RHS; \
Reid Spencera42c7fd2007-01-20 20:12:29 +0000254 } else if (BitWidth <= 16) { \
255 LHS = int64_t(doSignExtension(uint64_t(Src1.Int16Val), ITy)); \
256 RHS = int64_t(doSignExtension(uint64_t(Src2.Int16Val), ITy)); \
Reid Spencere0929362007-03-03 08:38:04 +0000257 Dest.Int1Val = LHS OP RHS; \
Reid Spencera42c7fd2007-01-20 20:12:29 +0000258 } else if (BitWidth <= 32) { \
259 LHS = int64_t(doSignExtension(uint64_t(Src1.Int32Val), ITy)); \
260 RHS = int64_t(doSignExtension(uint64_t(Src2.Int32Val), ITy)); \
Reid Spencere0929362007-03-03 08:38:04 +0000261 Dest.Int1Val = LHS OP RHS; \
Reid Spencera42c7fd2007-01-20 20:12:29 +0000262 } else if (BitWidth <= 64) { \
263 LHS = int64_t(doSignExtension(uint64_t(Src1.Int64Val), ITy)); \
264 RHS = int64_t(doSignExtension(uint64_t(Src2.Int64Val), ITy)); \
Reid Spencere0929362007-03-03 08:38:04 +0000265 Dest.Int1Val = LHS OP RHS; \
Reid Spencera42c7fd2007-01-20 20:12:29 +0000266 } else { \
Reid Spencere0929362007-03-03 08:38:04 +0000267 Dest.Int1Val = Src1.APIntVal->APOP(*(Src2.APIntVal)); \
268 } \
269 break; \
270 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000271
Reid Spencere0929362007-03-03 08:38:04 +0000272#define IMPLEMENT_UNSIGNED_ICMP(OP, TY, APOP) \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000273 case Type::IntegerTyID: { \
274 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
Reid Spencere0929362007-03-03 08:38:04 +0000275 if (BitWidth == 1) { \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000276 Dest.Int1Val = ((uint8_t)Src1.Int1Val) OP ((uint8_t)Src2.Int1Val); \
Reid Spencere0929362007-03-03 08:38:04 +0000277 maskToBitWidth(Dest, BitWidth); \
278 } else if (BitWidth <= 8) { \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000279 Dest.Int1Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
Reid Spencere0929362007-03-03 08:38:04 +0000280 maskToBitWidth(Dest, BitWidth); \
281 } else if (BitWidth <= 16) { \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000282 Dest.Int1Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
Reid Spencere0929362007-03-03 08:38:04 +0000283 maskToBitWidth(Dest, BitWidth); \
284 } else if (BitWidth <= 32) { \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000285 Dest.Int1Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
Reid Spencere0929362007-03-03 08:38:04 +0000286 maskToBitWidth(Dest, BitWidth); \
287 } else if (BitWidth <= 64) { \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000288 Dest.Int1Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
Reid Spencere0929362007-03-03 08:38:04 +0000289 maskToBitWidth(Dest, BitWidth); \
290 } else { \
291 Dest.Int1Val = Src1.APIntVal->APOP(*(Src2.APIntVal)); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000292 } \
293 break; \
294 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000295
Chris Lattnerfd506f52003-04-23 19:55:35 +0000296// Handle pointers specially because they must be compared with only as much
297// width as the host has. We _do not_ want to be comparing 64 bit values when
298// running on a 32-bit target, otherwise the upper 32 bits might mess up
299// comparisons if they contain garbage.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000300#define IMPLEMENT_POINTER_ICMP(OP) \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000301 case Type::PointerTyID: \
Reid Spencer4fe16d62007-01-11 18:21:29 +0000302 Dest.Int1Val = (void*)(intptr_t)Src1.PointerVal OP \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000303 (void*)(intptr_t)Src2.PointerVal; break
304
Reid Spencere4d87aa2006-12-23 06:05:41 +0000305static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
306 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000307 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000308 switch (Ty->getTypeID()) {
Reid Spencere0929362007-03-03 08:38:04 +0000309 IMPLEMENT_UNSIGNED_ICMP(==, Ty, eq);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000310 IMPLEMENT_POINTER_ICMP(==);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000311 default:
312 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
313 abort();
314 }
315 return Dest;
316}
317
318static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
319 const Type *Ty) {
320 GenericValue Dest;
321 switch (Ty->getTypeID()) {
Reid Spencere0929362007-03-03 08:38:04 +0000322 IMPLEMENT_UNSIGNED_ICMP(!=, Ty, ne);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000323 IMPLEMENT_POINTER_ICMP(!=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000324 default:
325 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
326 abort();
327 }
328 return Dest;
329}
330
331static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
332 const Type *Ty) {
333 GenericValue Dest;
334 switch (Ty->getTypeID()) {
Reid Spencere0929362007-03-03 08:38:04 +0000335 IMPLEMENT_UNSIGNED_ICMP(<, Ty, ult);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000336 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000337 default:
338 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
339 abort();
340 }
341 return Dest;
342}
343
344static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
345 const Type *Ty) {
346 GenericValue Dest;
347 switch (Ty->getTypeID()) {
Reid Spencere0929362007-03-03 08:38:04 +0000348 IMPLEMENT_SIGNED_ICMP(<, Ty, slt);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000349 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000350 default:
351 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
352 abort();
353 }
354 return Dest;
355}
356
357static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
358 const Type *Ty) {
359 GenericValue Dest;
360 switch (Ty->getTypeID()) {
Reid Spencere0929362007-03-03 08:38:04 +0000361 IMPLEMENT_UNSIGNED_ICMP(>, Ty, ugt);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000362 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000363 default:
364 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
365 abort();
366 }
367 return Dest;
368}
369
370static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
371 const Type *Ty) {
372 GenericValue Dest;
373 switch (Ty->getTypeID()) {
Reid Spencere0929362007-03-03 08:38:04 +0000374 IMPLEMENT_SIGNED_ICMP(>, Ty, sgt);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000375 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000376 default:
377 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
378 abort();
379 }
380 return Dest;
381}
382
383static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
384 const Type *Ty) {
385 GenericValue Dest;
386 switch (Ty->getTypeID()) {
Reid Spencere0929362007-03-03 08:38:04 +0000387 IMPLEMENT_UNSIGNED_ICMP(<=, Ty, ule);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000388 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000389 default:
390 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
391 abort();
392 }
393 return Dest;
394}
395
396static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
397 const Type *Ty) {
398 GenericValue Dest;
399 switch (Ty->getTypeID()) {
Reid Spencere0929362007-03-03 08:38:04 +0000400 IMPLEMENT_SIGNED_ICMP(<=, Ty, sle);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000401 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000402 default:
403 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
404 abort();
405 }
406 return Dest;
407}
408
409static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
410 const Type *Ty) {
411 GenericValue Dest;
412 switch (Ty->getTypeID()) {
Reid Spencere0929362007-03-03 08:38:04 +0000413 IMPLEMENT_UNSIGNED_ICMP(>=, Ty, uge);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000414 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000415 default:
416 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
417 abort();
418 }
419 return Dest;
420}
421
422static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
423 const Type *Ty) {
424 GenericValue Dest;
425 switch (Ty->getTypeID()) {
Reid Spencere0929362007-03-03 08:38:04 +0000426 IMPLEMENT_SIGNED_ICMP(>=, Ty, sge);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000427 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000428 default:
429 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
430 abort();
431 }
432 return Dest;
433}
434
Reid Spencere49661b2006-12-31 05:51:36 +0000435void Interpreter::visitICmpInst(ICmpInst &I) {
436 ExecutionContext &SF = ECStack.back();
437 const Type *Ty = I.getOperand(0)->getType();
438 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
439 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
440 GenericValue R; // Result
441
442 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000443 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
444 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
Reid Spencere49661b2006-12-31 05:51:36 +0000445 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
446 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
447 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
448 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
449 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
450 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
451 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
452 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
453 default:
454 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
455 abort();
456 }
457
458 SetValue(&I, R, SF);
459}
460
461#define IMPLEMENT_FCMP(OP, TY) \
Reid Spencer4fe16d62007-01-11 18:21:29 +0000462 case Type::TY##TyID: Dest.Int1Val = Src1.TY##Val OP Src2.TY##Val; break
Reid Spencere4d87aa2006-12-23 06:05:41 +0000463
Reid Spencera54b7cb2007-01-12 07:05:14 +0000464static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000465 const Type *Ty) {
466 GenericValue Dest;
467 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000468 IMPLEMENT_FCMP(==, Float);
469 IMPLEMENT_FCMP(==, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000470 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000471 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000472 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000473 }
474 return Dest;
475}
476
Reid Spencera54b7cb2007-01-12 07:05:14 +0000477static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000478 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000479 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000480 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000481 IMPLEMENT_FCMP(!=, Float);
482 IMPLEMENT_FCMP(!=, Double);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000483
Chris Lattner92101ac2001-08-23 17:05:04 +0000484 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000485 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000486 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000487 }
488 return Dest;
489}
490
Reid Spencera54b7cb2007-01-12 07:05:14 +0000491static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000492 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000493 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000494 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000495 IMPLEMENT_FCMP(<=, Float);
496 IMPLEMENT_FCMP(<=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000497 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000498 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000499 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000500 }
501 return Dest;
502}
503
Reid Spencera54b7cb2007-01-12 07:05:14 +0000504static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000505 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000506 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000507 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000508 IMPLEMENT_FCMP(>=, Float);
509 IMPLEMENT_FCMP(>=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000510 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000511 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000512 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000513 }
514 return Dest;
515}
516
Reid Spencera54b7cb2007-01-12 07:05:14 +0000517static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000518 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000519 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000520 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000521 IMPLEMENT_FCMP(<, Float);
522 IMPLEMENT_FCMP(<, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000523 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000524 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000525 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000526 }
527 return Dest;
528}
529
Reid Spencera54b7cb2007-01-12 07:05:14 +0000530static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000531 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000532 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000533 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000534 IMPLEMENT_FCMP(>, Float);
535 IMPLEMENT_FCMP(>, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000536 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000537 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000538 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000539 }
540 return Dest;
541}
542
Reid Spencera54b7cb2007-01-12 07:05:14 +0000543#define IMPLEMENT_UNORDERED(TY, X,Y) \
544 if (TY == Type::FloatTy) \
545 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
546 Dest.Int1Val = true; \
547 return Dest; \
548 } \
549 else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
550 Dest.Int1Val = true; \
551 return Dest; \
552 }
553
554
555static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
556 const Type *Ty) {
557 GenericValue Dest;
558 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
559 return executeFCMP_OEQ(Src1, Src2, Ty);
560}
561
562static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
563 const Type *Ty) {
564 GenericValue Dest;
565 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
566 return executeFCMP_ONE(Src1, Src2, Ty);
567}
568
569static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
570 const Type *Ty) {
571 GenericValue Dest;
572 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
573 return executeFCMP_OLE(Src1, Src2, Ty);
574}
575
576static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
577 const Type *Ty) {
578 GenericValue Dest;
579 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
580 return executeFCMP_OGE(Src1, Src2, Ty);
581}
582
583static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
584 const Type *Ty) {
585 GenericValue Dest;
586 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
587 return executeFCMP_OLT(Src1, Src2, Ty);
588}
589
590static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
591 const Type *Ty) {
592 GenericValue Dest;
593 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
594 return executeFCMP_OGT(Src1, Src2, Ty);
595}
596
597static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
598 const Type *Ty) {
599 GenericValue Dest;
600 if (Ty == Type::FloatTy)
601 Dest.Int1Val = (Src1.FloatVal == Src1.FloatVal &&
602 Src2.FloatVal == Src2.FloatVal);
603 else
604 Dest.Int1Val = (Src1.DoubleVal == Src1.DoubleVal &&
605 Src2.DoubleVal == Src2.DoubleVal);
606 return Dest;
607}
608
609static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
610 const Type *Ty) {
611 GenericValue Dest;
612 if (Ty == Type::FloatTy)
613 Dest.Int1Val = (Src1.FloatVal != Src1.FloatVal ||
614 Src2.FloatVal != Src2.FloatVal);
615 else
616 Dest.Int1Val = (Src1.DoubleVal != Src1.DoubleVal ||
617 Src2.DoubleVal != Src2.DoubleVal);
618 return Dest;
619}
620
Reid Spencere4d87aa2006-12-23 06:05:41 +0000621void Interpreter::visitFCmpInst(FCmpInst &I) {
622 ExecutionContext &SF = ECStack.back();
623 const Type *Ty = I.getOperand(0)->getType();
624 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
625 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
626 GenericValue R; // Result
627
628 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000629 case FCmpInst::FCMP_FALSE: R.Int1Val = false; break;
630 case FCmpInst::FCMP_TRUE: R.Int1Val = true; break;
631 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
632 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
633 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
634 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
635 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
636 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
637 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
638 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
639 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
640 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
641 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
642 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
643 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
644 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000645 default:
646 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
647 abort();
648 }
649
650 SetValue(&I, R, SF);
651}
652
Reid Spencere4d87aa2006-12-23 06:05:41 +0000653static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
654 GenericValue Src2, const Type *Ty) {
655 GenericValue Result;
656 switch (predicate) {
657 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
658 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
659 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
660 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
661 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
662 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
663 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
664 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
665 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
666 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000667 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
668 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
669 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
670 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
671 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
672 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
673 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
674 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
675 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
676 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
677 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
678 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
679 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
680 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000681 case FCmpInst::FCMP_FALSE: {
682 GenericValue Result;
Reid Spencer4fe16d62007-01-11 18:21:29 +0000683 Result.Int1Val = false;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000684 return Result;
685 }
686 case FCmpInst::FCMP_TRUE: {
687 GenericValue Result;
Reid Spencer4fe16d62007-01-11 18:21:29 +0000688 Result.Int1Val = true;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000689 return Result;
690 }
691 default:
692 cerr << "Unhandled Cmp predicate\n";
693 abort();
694 }
695}
696
Chris Lattnerd7916e92003-05-10 21:22:39 +0000697void Interpreter::visitBinaryOperator(BinaryOperator &I) {
698 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000699 const Type *Ty = I.getOperand(0)->getType();
700 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
701 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000702 GenericValue R; // Result
Reid Spencere1aa0662007-03-03 06:22:22 +0000703 initializeAPInt(R, Ty, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000704
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000705 switch (I.getOpcode()) {
Reid Spencere1aa0662007-03-03 06:22:22 +0000706 case Instruction::Add: executeAddInst (R, Src1, Src2, Ty); break;
707 case Instruction::Sub: executeSubInst (R, Src1, Src2, Ty); break;
708 case Instruction::Mul: executeMulInst (R, Src1, Src2, Ty); break;
709 case Instruction::UDiv: executeUDivInst (R, Src1, Src2, Ty); break;
710 case Instruction::SDiv: executeSDivInst (R, Src1, Src2, Ty); break;
711 case Instruction::FDiv: executeFDivInst (R, Src1, Src2, Ty); break;
712 case Instruction::URem: executeURemInst (R, Src1, Src2, Ty); break;
713 case Instruction::SRem: executeSRemInst (R, Src1, Src2, Ty); break;
714 case Instruction::FRem: executeFRemInst (R, Src1, Src2, Ty); break;
715 case Instruction::And: executeAndInst (R, Src1, Src2, Ty); break;
716 case Instruction::Or: executeOrInst (R, Src1, Src2, Ty); break;
717 case Instruction::Xor: executeXorInst (R, Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000718 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000719 cerr << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner02868352003-04-22 21:22:33 +0000720 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000721 }
722
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000723 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000724}
725
Misha Brukmand1c881a2005-04-21 22:43:08 +0000726static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000727 GenericValue Src3) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000728 return Src1.Int1Val ? Src2 : Src3;
Chris Lattner759d34f2004-04-20 16:43:21 +0000729}
730
731void Interpreter::visitSelectInst(SelectInst &I) {
732 ExecutionContext &SF = ECStack.back();
733 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
734 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
735 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
Reid Spencere1aa0662007-03-03 06:22:22 +0000736 GenericValue R;
737 initializeAPInt(R, I.getOperand(1)->getType(), SF);
738 R = executeSelectInst(Src1, Src2, Src3);
Chris Lattner759d34f2004-04-20 16:43:21 +0000739 SetValue(&I, R, SF);
740}
741
742
Chris Lattner92101ac2001-08-23 17:05:04 +0000743//===----------------------------------------------------------------------===//
744// Terminator Instruction Implementations
745//===----------------------------------------------------------------------===//
746
Chris Lattnere43db882001-10-27 04:15:57 +0000747void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000748 // runAtExitHandlers() assumes there are no stack frames, but
749 // if exit() was called, then it had a stack frame. Blow away
750 // the stack before interpreting atexit handlers.
751 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000752 runAtExitHandlers ();
Reid Spencere49661b2006-12-31 05:51:36 +0000753 exit (GV.Int32Val);
Chris Lattnere43db882001-10-27 04:15:57 +0000754}
755
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000756/// Pop the last stack frame off of ECStack and then copy the result
757/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000758/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000759/// CallInst if there was a previous stack frame. This method may
760/// invalidate any ECStack iterators you have. This method also takes
761/// care of switching to the normal destination BB, if we are returning
762/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000763///
764void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
765 GenericValue Result) {
766 // Pop the current stack frame.
767 ECStack.pop_back();
768
Misha Brukmand1c881a2005-04-21 22:43:08 +0000769 if (ECStack.empty()) { // Finished main. Put result into exit code...
Chris Lattner42a75512007-01-15 02:27:26 +0000770 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000771 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000772 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000773 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000774 }
775 } else {
776 // If we have a previous stack frame, and we have a previous call,
777 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000778 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000779 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000780 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000781 SetValue(I, Result, CallingSF);
782 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
783 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000784 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000785 }
786 }
787}
788
Chris Lattnerd7916e92003-05-10 21:22:39 +0000789void Interpreter::visitReturnInst(ReturnInst &I) {
790 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000791 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000792 GenericValue Result;
793
794 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000795 if (I.getNumOperands()) {
796 RetTy = I.getReturnValue()->getType();
797 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000798 }
799
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000800 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000801}
802
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000803void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000804 // Unwind stack
805 Instruction *Inst;
806 do {
807 ECStack.pop_back ();
808 if (ECStack.empty ())
809 abort ();
810 Inst = ECStack.back ().Caller.getInstruction ();
811 } while (!(Inst && isa<InvokeInst> (Inst)));
812
813 // Return from invoke
814 ExecutionContext &InvokingSF = ECStack.back ();
815 InvokingSF.Caller = CallSite ();
816
817 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000818 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000819}
820
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000821void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000822 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000823 abort();
824}
825
Chris Lattnerd7916e92003-05-10 21:22:39 +0000826void Interpreter::visitBranchInst(BranchInst &I) {
827 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000828 BasicBlock *Dest;
829
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000830 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
831 if (!I.isUnconditional()) {
832 Value *Cond = I.getCondition();
Reid Spencer4fe16d62007-01-11 18:21:29 +0000833 if (getOperandValue(Cond, SF).Int1Val == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000834 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000835 }
Chris Lattner77113b62003-05-10 20:21:16 +0000836 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000837}
838
Chris Lattnerd7916e92003-05-10 21:22:39 +0000839void Interpreter::visitSwitchInst(SwitchInst &I) {
840 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000841 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
842 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000843
844 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000845 BasicBlock *Dest = 0;
846 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Reid Spencere4d87aa2006-12-23 06:05:41 +0000847 if (executeICMP_EQ(CondVal,
Reid Spencer4fe16d62007-01-11 18:21:29 +0000848 getOperandValue(I.getOperand(i), SF), ElTy).Int1Val) {
Chris Lattner09e93922003-04-22 20:34:47 +0000849 Dest = cast<BasicBlock>(I.getOperand(i+1));
850 break;
851 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000852
Chris Lattner09e93922003-04-22 20:34:47 +0000853 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000854 SwitchToNewBasicBlock(Dest, SF);
855}
856
857// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
858// This function handles the actual updating of block and instruction iterators
859// as well as execution of all of the PHI nodes in the destination block.
860//
861// This method does this because all of the PHI nodes must be executed
862// atomically, reading their inputs before any of the results are updated. Not
863// doing this can cause problems if the PHI nodes depend on other PHI nodes for
864// their inputs. If the input PHI node is updated before it is read, incorrect
865// results can happen. Thus we use a two phase approach.
866//
867void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
868 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
869 SF.CurBB = Dest; // Update CurBB to branch destination
870 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
871
872 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
873
874 // Loop over all of the PHI nodes in the current block, reading their inputs.
875 std::vector<GenericValue> ResultValues;
876
877 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
878 // Search for the value corresponding to this previous bb...
879 int i = PN->getBasicBlockIndex(PrevBB);
880 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
881 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000882
Chris Lattner77113b62003-05-10 20:21:16 +0000883 // Save the incoming value for this PHI node...
884 ResultValues.push_back(getOperandValue(IncomingValue, SF));
885 }
886
887 // Now loop over all of the PHI nodes setting their values...
888 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000889 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
890 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000891 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000892 }
Chris Lattner09e93922003-04-22 20:34:47 +0000893}
894
Chris Lattner92101ac2001-08-23 17:05:04 +0000895//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000896// Memory Instruction Implementations
897//===----------------------------------------------------------------------===//
898
Chris Lattnerd7916e92003-05-10 21:22:39 +0000899void Interpreter::visitAllocationInst(AllocationInst &I) {
900 ExecutionContext &SF = ECStack.back();
901
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000902 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000903
Chris Lattnercc82cc12002-04-28 21:57:33 +0000904 // Get the number of elements being allocated by the array...
Reid Spencere49661b2006-12-31 05:51:36 +0000905 unsigned NumElements = getOperandValue(I.getOperand(0), SF).Int32Val;
Chris Lattner86660982001-08-27 05:16:50 +0000906
907 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +0000908 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000909
Chris Lattnerfe11a972002-12-23 23:59:41 +0000910 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000911 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000912 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000913
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000914 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000915 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000916}
917
Chris Lattnerd7916e92003-05-10 21:22:39 +0000918void Interpreter::visitFreeInst(FreeInst &I) {
919 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000920 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
921 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000922 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000923 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000924}
925
Chris Lattnera34c5682002-08-27 22:33:45 +0000926// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000927//
Chris Lattner4af6de82003-11-25 20:44:56 +0000928GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000929 gep_type_iterator E,
930 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000931 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000932 "Cannot getElementOffset of a nonpointer type!");
933
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000934 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000935
936 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000937 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000938 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000939
Reid Spencerb83eb642006-10-20 07:07:24 +0000940 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
941 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000942
Chris Lattnerb1919e22007-02-10 19:55:17 +0000943 Total += (PointerTy)SLO->getElementOffset(Index);
Chris Lattner4af6de82003-11-25 20:44:56 +0000944 } else {
945 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000946 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000947 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
948
Reid Spencera54b7cb2007-01-12 07:05:14 +0000949 int64_t Idx;
950 unsigned BitWidth =
951 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
Reid Spencer23e28832007-01-18 01:25:42 +0000952 if (BitWidth == 32)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000953 Idx = (int64_t)(int32_t)IdxGV.Int32Val;
Reid Spencer23e28832007-01-18 01:25:42 +0000954 else if (BitWidth == 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000955 Idx = (int64_t)IdxGV.Int64Val;
956 else
Reid Spencer23e28832007-01-18 01:25:42 +0000957 assert(0 && "Invalid index type for getelementptr");
Chris Lattnerd5644962005-01-08 20:05:34 +0000958 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000959 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000960 }
961
Chris Lattnera34c5682002-08-27 22:33:45 +0000962 GenericValue Result;
963 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
964 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000965}
966
Chris Lattnerd7916e92003-05-10 21:22:39 +0000967void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
968 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000969 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000970 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000971}
972
Chris Lattnerd7916e92003-05-10 21:22:39 +0000973void Interpreter::visitLoadInst(LoadInst &I) {
974 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000975 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000976 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Reid Spencere0929362007-03-03 08:38:04 +0000977 GenericValue Result;
978 initializeAPInt(Result, I.getType(), SF);
979 LoadValueFromMemory(Result, Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000980 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000981}
982
Chris Lattnerd7916e92003-05-10 21:22:39 +0000983void Interpreter::visitStoreInst(StoreInst &I) {
984 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000985 GenericValue Val = getOperandValue(I.getOperand(0), SF);
986 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000987 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000988 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000989}
990
Chris Lattner86660982001-08-27 05:16:50 +0000991//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000992// Miscellaneous Instruction Implementations
993//===----------------------------------------------------------------------===//
994
Brian Gaekefea483d2003-11-07 20:04:22 +0000995void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000996 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000997
998 // Check to see if this is an intrinsic function call...
999 if (Function *F = CS.getCalledFunction())
Reid Spencer5cbf9852007-01-30 20:08:39 +00001000 if (F->isDeclaration ())
Chris Lattner73011782003-12-28 09:44:37 +00001001 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +00001002 case Intrinsic::not_intrinsic:
1003 break;
Chris Lattner317201d2004-03-13 00:24:00 +00001004 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +00001005 GenericValue ArgIndex;
1006 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1007 ArgIndex.UIntPairVal.second = 0;
1008 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +00001009 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +00001010 }
Chris Lattner317201d2004-03-13 00:24:00 +00001011 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +00001012 return;
Chris Lattner317201d2004-03-13 00:24:00 +00001013 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +00001014 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1015 return;
1016 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +00001017 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +00001018 // class to transform it into hopefully tasty LLVM code.
1019 //
1020 Instruction *Prev = CS.getInstruction()->getPrev();
1021 BasicBlock *Parent = CS.getInstruction()->getParent();
1022 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1023
1024 // Restore the CurInst pointer to the first instruction newly inserted, if
1025 // any.
1026 if (!Prev) {
1027 SF.CurInst = Parent->begin();
1028 } else {
1029 SF.CurInst = Prev;
1030 ++SF.CurInst;
1031 }
Brian Gaekeb440dea2004-04-23 18:05:28 +00001032 return;
Chris Lattner73011782003-12-28 09:44:37 +00001033 }
1034
Brian Gaekefea483d2003-11-07 20:04:22 +00001035 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +00001036 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +00001037 const unsigned NumArgs = SF.Caller.arg_size();
1038 ArgVals.reserve(NumArgs);
1039 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1040 e = SF.Caller.arg_end(); i != e; ++i) {
1041 Value *V = *i;
1042 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +00001043 // Promote all integral types whose size is < sizeof(int) into ints. We do
1044 // this by zero or sign extending the value as appropriate according to the
1045 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +00001046 const Type *Ty = V->getType();
Chris Lattner42a75512007-01-15 02:27:26 +00001047 if (Ty->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001048 if (Ty->getPrimitiveSizeInBits() == 1)
Reid Spencer4fe16d62007-01-11 18:21:29 +00001049 ArgVals.back().Int32Val = ArgVals.back().Int1Val;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001050 else if (Ty->getPrimitiveSizeInBits() <= 8)
1051 ArgVals.back().Int32Val = ArgVals.back().Int8Val;
1052 else if (Ty->getPrimitiveSizeInBits() <= 16)
1053 ArgVals.back().Int32Val = ArgVals.back().Int16Val;
Chris Lattner93780132003-01-13 00:58:52 +00001054 }
1055 }
Chris Lattner365a76e2001-09-10 04:49:44 +00001056
Misha Brukmand1c881a2005-04-21 22:43:08 +00001057 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001058 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +00001059 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +00001060 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +00001061}
1062
Reid Spencere0929362007-03-03 08:38:04 +00001063static void executeShlInst(GenericValue &Dest, GenericValue Src1,
1064 GenericValue Src2, const Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001065 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1066 unsigned BitWidth = ITy->getBitWidth();
Reid Spencere0929362007-03-03 08:38:04 +00001067 if (BitWidth <= 8) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001068 Dest.Int8Val = ((uint8_t)Src1.Int8Val) << ((uint32_t)Src2.Int8Val);
Reid Spencere0929362007-03-03 08:38:04 +00001069 maskToBitWidth(Dest, BitWidth);
1070 } else if (BitWidth <= 16) {
1071 Dest.Int16Val = ((uint16_t)Src1.Int16Val) << ((uint32_t)Src2.Int16Val);
1072 maskToBitWidth(Dest, BitWidth);
1073 } else if (BitWidth <= 32) {
1074 Dest.Int32Val = ((uint32_t)Src1.Int32Val) << ((uint32_t)Src2.Int32Val);
1075 maskToBitWidth(Dest, BitWidth);
1076 } else if (BitWidth <= 64) {
1077 Dest.Int64Val = ((uint64_t)Src1.Int64Val) << ((uint32_t)Src2.Int64Val);
1078 maskToBitWidth(Dest, BitWidth);
1079 } else {
1080 *(Dest.APIntVal) = Src1.APIntVal->shl(Src2.APIntVal->getZExtValue());
Reid Spencera54b7cb2007-01-12 07:05:14 +00001081 }
1082 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001083 cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
Reid Spencera54b7cb2007-01-12 07:05:14 +00001084 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001085 }
Chris Lattner86660982001-08-27 05:16:50 +00001086}
1087
Reid Spencere0929362007-03-03 08:38:04 +00001088static void executeLShrInst(GenericValue &Dest, GenericValue Src1,
1089 GenericValue Src2, const Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001090 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1091 unsigned BitWidth = ITy->getBitWidth();
Reid Spencere0929362007-03-03 08:38:04 +00001092 if (BitWidth <= 8) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001093 Dest.Int8Val = ((uint8_t)Src1.Int8Val) >> ((uint32_t)Src2.Int8Val);
Reid Spencere0929362007-03-03 08:38:04 +00001094 maskToBitWidth(Dest, BitWidth);
1095 } else if (BitWidth <= 16) {
1096 Dest.Int16Val = ((uint16_t)Src1.Int16Val) >> ((uint32_t)Src2.Int16Val);
1097 maskToBitWidth(Dest, BitWidth);
1098 } else if (BitWidth <= 32) {
1099 Dest.Int32Val = ((uint32_t)Src1.Int32Val) >> ((uint32_t)Src2.Int32Val);
1100 maskToBitWidth(Dest, BitWidth);
1101 } else if (BitWidth <= 64) {
1102 Dest.Int64Val = ((uint64_t)Src1.Int64Val) >> ((uint32_t)Src2.Int64Val);
1103 maskToBitWidth(Dest, BitWidth);
1104 } else {
1105 *(Dest.APIntVal) = Src1.APIntVal->lshr(Src2.APIntVal->getZExtValue());
Reid Spencera54b7cb2007-01-12 07:05:14 +00001106 }
1107 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001108 cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
Reid Spencer3822ff52006-11-08 06:47:33 +00001109 abort();
1110 }
Reid Spencer3822ff52006-11-08 06:47:33 +00001111}
1112
Reid Spencere0929362007-03-03 08:38:04 +00001113static void executeAShrInst(GenericValue &Dest, GenericValue Src1,
1114 GenericValue Src2, const Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001115 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1116 unsigned BitWidth = ITy->getBitWidth();
Reid Spencere0929362007-03-03 08:38:04 +00001117 if (BitWidth <= 8) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001118 Dest.Int8Val = ((int8_t)Src1.Int8Val) >> ((int32_t)Src2.Int8Val);
Reid Spencere0929362007-03-03 08:38:04 +00001119 maskToBitWidth(Dest, BitWidth);
1120 } else if (BitWidth <= 16) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001121 Dest.Int16Val = ((int16_t)Src1.Int16Val) >> ((int32_t)Src2.Int8Val);
Reid Spencere0929362007-03-03 08:38:04 +00001122 maskToBitWidth(Dest, BitWidth);
1123 } else if (BitWidth <= 32) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001124 Dest.Int32Val = ((int32_t)Src1.Int32Val) >> ((int32_t)Src2.Int8Val);
Reid Spencere0929362007-03-03 08:38:04 +00001125 maskToBitWidth(Dest, BitWidth);
1126 } else if (BitWidth <= 64) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001127 Dest.Int64Val = ((int64_t)Src1.Int64Val) >> ((int32_t)Src2.Int8Val);
Reid Spencere0929362007-03-03 08:38:04 +00001128 maskToBitWidth(Dest, BitWidth);
1129 } else {
1130 *(Dest.APIntVal) = Src1.APIntVal->ashr(Src2.APIntVal->getZExtValue());
Reid Spencera54b7cb2007-01-12 07:05:14 +00001131 }
1132 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001133 cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +00001134 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001135 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001136}
1137
Reid Spencer832254e2007-02-02 02:16:23 +00001138void Interpreter::visitShl(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001139 ExecutionContext &SF = ECStack.back();
1140 const Type *Ty = I.getOperand(0)->getType();
1141 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1142 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1143 GenericValue Dest;
Reid Spencere0929362007-03-03 08:38:04 +00001144 initializeAPInt(Dest, Ty, SF);
1145 executeShlInst (Dest, Src1, Src2, Ty);
Brian Gaeke63438cc2003-12-11 00:22:59 +00001146 SetValue(&I, Dest, SF);
1147}
1148
Reid Spencer832254e2007-02-02 02:16:23 +00001149void Interpreter::visitLShr(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001150 ExecutionContext &SF = ECStack.back();
1151 const Type *Ty = I.getOperand(0)->getType();
1152 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1153 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1154 GenericValue Dest;
Reid Spencere0929362007-03-03 08:38:04 +00001155 initializeAPInt(Dest, Ty, SF);
1156 executeLShrInst (Dest, Src1, Src2, Ty);
Reid Spencer3822ff52006-11-08 06:47:33 +00001157 SetValue(&I, Dest, SF);
1158}
1159
Reid Spencer832254e2007-02-02 02:16:23 +00001160void Interpreter::visitAShr(BinaryOperator &I) {
Reid Spencer3822ff52006-11-08 06:47:33 +00001161 ExecutionContext &SF = ECStack.back();
1162 const Type *Ty = I.getOperand(0)->getType();
1163 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1164 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1165 GenericValue Dest;
Reid Spencere0929362007-03-03 08:38:04 +00001166 initializeAPInt(Dest, Ty, SF);
1167 executeAShrInst (Dest, Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001168 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001169}
1170
Reid Spencer23e28832007-01-18 01:25:42 +00001171#define INTEGER_ASSIGN(DEST, BITWIDTH, VAL) \
1172 { \
Reid Spencer23cbb1c2007-02-08 00:29:31 +00001173 uint64_t Mask = ~(uint64_t)(0ull) >> (64-BITWIDTH); \
Reid Spencer23e28832007-01-18 01:25:42 +00001174 if (BITWIDTH == 1) { \
1175 Dest.Int1Val = (bool) (VAL & Mask); \
1176 } else if (BITWIDTH <= 8) { \
1177 Dest.Int8Val = (uint8_t) (VAL & Mask); \
1178 } else if (BITWIDTH <= 16) { \
1179 Dest.Int16Val = (uint16_t) (VAL & Mask); \
1180 } else if (BITWIDTH <= 32) { \
1181 Dest.Int32Val = (uint32_t) (VAL & Mask); \
1182 } else \
1183 Dest.Int64Val = (uint64_t) (VAL & Mask); \
1184 }
Chris Lattner86660982001-08-27 05:16:50 +00001185
Reid Spencera54b7cb2007-01-12 07:05:14 +00001186GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
1187 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001188 const Type *SrcTy = SrcVal->getType();
1189 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001190 const IntegerType *DITy = cast<IntegerType>(DstTy);
1191 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1192 unsigned DBitWidth = DITy->getBitWidth();
1193 unsigned SBitWidth = SITy->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001194 assert(SBitWidth > DBitWidth && "Invalid truncate");
Chris Lattner86660982001-08-27 05:16:50 +00001195
Reid Spencere1aa0662007-03-03 06:22:22 +00001196 if (DBitWidth > 64) {
1197 // Both values are APInt, just use the APInt trunc
1198 initializeAPInt(Dest, DstTy, SF);
1199 *(Dest.APIntVal) = Src.APIntVal->trunc(DBitWidth);
1200 return Dest;
1201 }
1202
1203 uint64_t MaskedVal = 0;
1204 uint64_t Mask = (1ULL << DBitWidth) - 1;
1205
Reid Spencera54b7cb2007-01-12 07:05:14 +00001206 // Mask the source value to its actual bit width. This ensures that any
1207 // high order bits are cleared.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001208 if (SBitWidth <= 8)
1209 MaskedVal = Src.Int8Val & Mask;
1210 else if (SBitWidth <= 16)
1211 MaskedVal = Src.Int16Val & Mask;
1212 else if (SBitWidth <= 32)
1213 MaskedVal = Src.Int32Val & Mask;
Reid Spencere1aa0662007-03-03 06:22:22 +00001214 else if (SBitWidth <= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001215 MaskedVal = Src.Int64Val & Mask;
Reid Spencere1aa0662007-03-03 06:22:22 +00001216 else
1217 MaskedVal = Src.APIntVal->trunc(DBitWidth).getZExtValue();
Chris Lattnera34c5682002-08-27 22:33:45 +00001218
Reid Spencera54b7cb2007-01-12 07:05:14 +00001219 INTEGER_ASSIGN(Dest, DBitWidth, MaskedVal);
Chris Lattnera34c5682002-08-27 22:33:45 +00001220 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001221}
Chris Lattner92101ac2001-08-23 17:05:04 +00001222
Reid Spencera54b7cb2007-01-12 07:05:14 +00001223GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
1224 ExecutionContext &SF) {
1225 const Type *SrcTy = SrcVal->getType();
1226 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1227 const IntegerType *DITy = cast<IntegerType>(DstTy);
1228 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1229 unsigned DBitWidth = DITy->getBitWidth();
1230 unsigned SBitWidth = SITy->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001231 assert(SBitWidth < DBitWidth && "Invalid sign extend");
Reid Spencerc00a4302007-01-20 08:32:52 +00001232
Reid Spencere0929362007-03-03 08:38:04 +00001233 if (SBitWidth > 64) {
1234 // Both values are APInt, just use the APInt::sext method;
1235 initializeAPInt(Dest, DstTy, SF);
1236 *(Dest.APIntVal) = Src.APIntVal->sext(DBitWidth);
1237 return Dest;
1238 }
1239
Reid Spencerc00a4302007-01-20 08:32:52 +00001240 // Normalize to a 64-bit value.
1241 uint64_t Normalized = 0;
1242 if (SBitWidth <= 8)
1243 Normalized = Src.Int8Val;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001244 else if (SBitWidth <= 16)
Reid Spencerc00a4302007-01-20 08:32:52 +00001245 Normalized = Src.Int16Val;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001246 else if (SBitWidth <= 32)
Reid Spencerc00a4302007-01-20 08:32:52 +00001247 Normalized = Src.Int32Val;
Reid Spencere0929362007-03-03 08:38:04 +00001248 else
Reid Spencerc00a4302007-01-20 08:32:52 +00001249 Normalized = Src.Int64Val;
1250
Reid Spencere0929362007-03-03 08:38:04 +00001251 if (DBitWidth > 64) {
1252 // Destination is an APint, construct it and return
1253 initializeAPInt(Dest, DstTy, SF);
1254 *(Dest.APIntVal) = APInt(SBitWidth, Normalized).sext(DBitWidth);
1255 return Dest;
1256 }
1257
Reid Spencera42c7fd2007-01-20 20:12:29 +00001258 Normalized = doSignExtension(Normalized, SITy);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001259
1260 // Now that we have a sign extended value, assign it to the destination
Reid Spencerc00a4302007-01-20 08:32:52 +00001261 INTEGER_ASSIGN(Dest, DBitWidth, Normalized);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001262 return Dest;
1263}
1264
1265GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
1266 ExecutionContext &SF) {
1267 const Type *SrcTy = SrcVal->getType();
1268 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1269 const IntegerType *DITy = cast<IntegerType>(DstTy);
1270 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1271 unsigned DBitWidth = DITy->getBitWidth();
1272 unsigned SBitWidth = SITy->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001273 assert(SBitWidth < DBitWidth && "Invalid sign extend");
Reid Spencere0929362007-03-03 08:38:04 +00001274
1275 if (SBitWidth > 64) {
1276 // Both values are APInt, just use the APInt::sext method;
1277 initializeAPInt(Dest, DstTy, SF);
1278 *(Dest.APIntVal) = Src.APIntVal->zext(DBitWidth);
1279 return Dest;
1280 }
1281
Reid Spencera54b7cb2007-01-12 07:05:14 +00001282 uint64_t Extended = 0;
1283 if (SBitWidth == 1)
1284 // For sign extension from bool, we must extend the source bits.
1285 Extended = (uint64_t) (Src.Int1Val & 1);
1286 else if (SBitWidth <= 8)
1287 Extended = (uint64_t) (uint8_t)Src.Int8Val;
1288 else if (SBitWidth <= 16)
1289 Extended = (uint64_t) (uint16_t)Src.Int16Val;
1290 else if (SBitWidth <= 32)
1291 Extended = (uint64_t) (uint32_t)Src.Int32Val;
1292 else
1293 Extended = (uint64_t) Src.Int64Val;
1294
Reid Spencere0929362007-03-03 08:38:04 +00001295 if (DBitWidth > 64) {
1296 // Destination is an APint, construct it and return
1297 initializeAPInt(Dest, DstTy, SF);
1298 *(Dest.APIntVal) = APInt(SBitWidth, Extended).zext(DBitWidth);
1299 return Dest;
1300 }
1301
Reid Spencera54b7cb2007-01-12 07:05:14 +00001302 // Now that we have a sign extended value, assign it to the destination
1303 INTEGER_ASSIGN(Dest, DBitWidth, Extended);
1304 return Dest;
1305}
1306
1307GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
1308 ExecutionContext &SF) {
1309 const Type *SrcTy = SrcVal->getType();
1310 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1311 assert(SrcTy == Type::DoubleTy && DstTy == Type::FloatTy &&
1312 "Invalid FPTrunc instruction");
1313 Dest.FloatVal = (float) Src.DoubleVal;
1314 return Dest;
1315}
1316
1317GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
1318 ExecutionContext &SF) {
1319 const Type *SrcTy = SrcVal->getType();
1320 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1321 assert(SrcTy == Type::FloatTy && DstTy == Type::DoubleTy &&
1322 "Invalid FPTrunc instruction");
1323 Dest.DoubleVal = (double) Src.FloatVal;
1324 return Dest;
1325}
1326
1327GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
1328 ExecutionContext &SF) {
1329 const Type *SrcTy = SrcVal->getType();
1330 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1331 const IntegerType *DITy = cast<IntegerType>(DstTy);
1332 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001333 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001334
1335 if (DBitWidth > 64) {
1336 initializeAPInt(Dest, DITy, SF);
1337 if (SrcTy->getTypeID() == Type::FloatTyID)
1338 *(Dest.APIntVal) = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1339 else
1340 *(Dest.APIntVal) = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1341 return Dest;
1342 }
1343
Reid Spencera54b7cb2007-01-12 07:05:14 +00001344 uint64_t Converted = 0;
1345 if (SrcTy->getTypeID() == Type::FloatTyID)
1346 Converted = (uint64_t) Src.FloatVal;
1347 else
1348 Converted = (uint64_t) Src.DoubleVal;
1349
1350 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1351 return Dest;
1352}
1353
1354GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1355 ExecutionContext &SF) {
1356 const Type *SrcTy = SrcVal->getType();
1357 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1358 const IntegerType *DITy = cast<IntegerType>(DstTy);
1359 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001360 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001361
1362 if (DBitWidth > 64) {
1363 initializeAPInt(Dest, DITy, SF);
1364 if (SrcTy->getTypeID() == Type::FloatTyID)
1365 *(Dest.APIntVal) = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1366 else
1367 *(Dest.APIntVal) = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1368 return Dest;
1369 }
1370
Reid Spencera54b7cb2007-01-12 07:05:14 +00001371 int64_t Converted = 0;
1372 if (SrcTy->getTypeID() == Type::FloatTyID)
1373 Converted = (int64_t) Src.FloatVal;
1374 else
1375 Converted = (int64_t) Src.DoubleVal;
1376
1377 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1378 return Dest;
1379}
1380
1381GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1382 ExecutionContext &SF) {
1383 const Type *SrcTy = SrcVal->getType();
1384 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1385 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1386 unsigned SBitWidth = SITy->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001387 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001388
1389 if (SBitWidth > 64) {
1390 if (DstTy->getTypeID() == Type::FloatTyID)
1391 Dest.FloatVal = APIntOps::RoundAPIntToFloat(*(Src.APIntVal));
1392 else
1393 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(*(Src.APIntVal));
1394 return Dest;
1395 }
1396
Reid Spencera54b7cb2007-01-12 07:05:14 +00001397 uint64_t Converted = 0;
1398 if (SBitWidth == 1)
1399 Converted = (uint64_t) Src.Int1Val;
1400 else if (SBitWidth <= 8)
1401 Converted = (uint64_t) Src.Int8Val;
1402 else if (SBitWidth <= 16)
1403 Converted = (uint64_t) Src.Int16Val;
1404 else if (SBitWidth <= 32)
1405 Converted = (uint64_t) Src.Int32Val;
1406 else
1407 Converted = (uint64_t) Src.Int64Val;
1408
1409 if (DstTy->getTypeID() == Type::FloatTyID)
1410 Dest.FloatVal = (float) Converted;
1411 else
1412 Dest.DoubleVal = (double) Converted;
1413 return Dest;
1414}
1415
1416GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1417 ExecutionContext &SF) {
1418 const Type *SrcTy = SrcVal->getType();
1419 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1420 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1421 unsigned SBitWidth = SITy->getBitWidth();
Reid Spencer24d6da52007-01-21 00:29:26 +00001422 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001423
1424 if (SBitWidth > 64) {
1425 if (DstTy->getTypeID() == Type::FloatTyID)
1426 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(*(Src.APIntVal));
1427 else
1428 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(*(Src.APIntVal));
1429 return Dest;
1430 }
1431
Reid Spencera54b7cb2007-01-12 07:05:14 +00001432 int64_t Converted = 0;
1433 if (SBitWidth == 1)
1434 Converted = 0LL - Src.Int1Val;
1435 else if (SBitWidth <= 8)
1436 Converted = (int64_t) (int8_t)Src.Int8Val;
1437 else if (SBitWidth <= 16)
1438 Converted = (int64_t) (int16_t)Src.Int16Val;
1439 else if (SBitWidth <= 32)
1440 Converted = (int64_t) (int32_t)Src.Int32Val;
1441 else
1442 Converted = (int64_t) Src.Int64Val;
1443
1444 if (DstTy->getTypeID() == Type::FloatTyID)
1445 Dest.FloatVal = (float) Converted;
1446 else
1447 Dest.DoubleVal = (double) Converted;
1448 return Dest;
1449}
1450
1451GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1452 ExecutionContext &SF) {
1453 const Type *SrcTy = SrcVal->getType();
1454 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1455 const IntegerType *DITy = cast<IntegerType>(DstTy);
1456 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001457 assert(isa<PointerType>(SrcTy) && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001458
1459 if (DBitWidth > 64) {
1460 initializeAPInt(Dest, DstTy, SF);
1461 *(Dest.APIntVal) = (intptr_t) Src.PointerVal;
1462 }
1463
Reid Spencera54b7cb2007-01-12 07:05:14 +00001464 INTEGER_ASSIGN(Dest, DBitWidth, (intptr_t) Src.PointerVal);
1465 return Dest;
1466}
1467
1468GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1469 ExecutionContext &SF) {
1470 const Type *SrcTy = SrcVal->getType();
1471 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1472 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1473 unsigned SBitWidth = SITy->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001474 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001475
Reid Spencera54b7cb2007-01-12 07:05:14 +00001476 uint64_t Converted = 0;
1477 if (SBitWidth == 1)
1478 Converted = (uint64_t) Src.Int1Val;
1479 else if (SBitWidth <= 8)
1480 Converted = (uint64_t) Src.Int8Val;
1481 else if (SBitWidth <= 16)
1482 Converted = (uint64_t) Src.Int16Val;
1483 else if (SBitWidth <= 32)
1484 Converted = (uint64_t) Src.Int32Val;
Reid Spencere0929362007-03-03 08:38:04 +00001485 else if (SBitWidth <= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001486 Converted = (uint64_t) Src.Int64Val;
Reid Spencere0929362007-03-03 08:38:04 +00001487 else
1488 Converted = (uint64_t) Src.APIntVal->trunc(64).getZExtValue();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001489
1490 Dest.PointerVal = (PointerTy) Converted;
1491 return Dest;
1492}
1493
1494GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1495 ExecutionContext &SF) {
1496
1497 const Type *SrcTy = SrcVal->getType();
1498 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1499 if (isa<PointerType>(DstTy)) {
1500 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1501 Dest.PointerVal = Src.PointerVal;
Chris Lattner42a75512007-01-15 02:27:26 +00001502 } else if (DstTy->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001503 const IntegerType *DITy = cast<IntegerType>(DstTy);
1504 unsigned DBitWidth = DITy->getBitWidth();
1505 if (SrcTy == Type::FloatTy) {
1506 Dest.Int32Val = FloatToBits(Src.FloatVal);
1507 } else if (SrcTy == Type::DoubleTy) {
1508 Dest.Int64Val = DoubleToBits(Src.DoubleVal);
Chris Lattner42a75512007-01-15 02:27:26 +00001509 } else if (SrcTy->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001510 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1511 unsigned SBitWidth = SITy->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001512 assert(SBitWidth == DBitWidth && "Invalid BitCast");
Reid Spencere0929362007-03-03 08:38:04 +00001513 if (SBitWidth == 1) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001514 Dest.Int1Val = Src.Int1Val;
Reid Spencere0929362007-03-03 08:38:04 +00001515 maskToBitWidth(Dest, DBitWidth);
1516 } else if (SBitWidth <= 8) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001517 Dest.Int8Val = Src.Int8Val;
Reid Spencere0929362007-03-03 08:38:04 +00001518 maskToBitWidth(Dest, DBitWidth);
1519 } else if (SBitWidth <= 16) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001520 Dest.Int16Val = Src.Int16Val;
Reid Spencere0929362007-03-03 08:38:04 +00001521 maskToBitWidth(Dest, DBitWidth);
1522 } else if (SBitWidth <= 32) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001523 Dest.Int32Val = Src.Int32Val;
Reid Spencere0929362007-03-03 08:38:04 +00001524 maskToBitWidth(Dest, DBitWidth);
1525 } else if (SBitWidth <= 64) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001526 Dest.Int64Val = Src.Int64Val;
Reid Spencere0929362007-03-03 08:38:04 +00001527 maskToBitWidth(Dest, DBitWidth);
1528 } else {
1529 *(Dest.APIntVal) = *(Src.APIntVal);
1530 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00001531 } else
1532 assert(0 && "Invalid BitCast");
1533 } else if (DstTy == Type::FloatTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001534 if (SrcTy->isInteger())
Reid Spencera54b7cb2007-01-12 07:05:14 +00001535 Dest.FloatVal = BitsToFloat(Src.Int32Val);
1536 else
1537 Dest.FloatVal = Src.FloatVal;
1538 } else if (DstTy == Type::DoubleTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001539 if (SrcTy->isInteger())
Reid Spencera54b7cb2007-01-12 07:05:14 +00001540 Dest.DoubleVal = BitsToDouble(Src.Int64Val);
1541 else
1542 Dest.DoubleVal = Src.DoubleVal;
1543 } else
1544 assert(0 && "Invalid Bitcast");
1545
1546 return Dest;
1547}
1548
1549void Interpreter::visitTruncInst(TruncInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001550 ExecutionContext &SF = ECStack.back();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001551 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1552}
1553
1554void Interpreter::visitSExtInst(SExtInst &I) {
1555 ExecutionContext &SF = ECStack.back();
1556 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1557}
1558
1559void Interpreter::visitZExtInst(ZExtInst &I) {
1560 ExecutionContext &SF = ECStack.back();
1561 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1562}
1563
1564void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1565 ExecutionContext &SF = ECStack.back();
1566 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1567}
1568
1569void Interpreter::visitFPExtInst(FPExtInst &I) {
1570 ExecutionContext &SF = ECStack.back();
1571 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1572}
1573
1574void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1575 ExecutionContext &SF = ECStack.back();
1576 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1577}
1578
1579void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1580 ExecutionContext &SF = ECStack.back();
1581 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1582}
1583
1584void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1585 ExecutionContext &SF = ECStack.back();
1586 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1587}
1588
1589void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1590 ExecutionContext &SF = ECStack.back();
1591 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1592}
1593
1594void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1595 ExecutionContext &SF = ECStack.back();
1596 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1597}
1598
1599void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1600 ExecutionContext &SF = ECStack.back();
1601 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1602}
1603
1604void Interpreter::visitBitCastInst(BitCastInst &I) {
1605 ExecutionContext &SF = ECStack.back();
1606 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001607}
Chris Lattner92101ac2001-08-23 17:05:04 +00001608
Brian Gaekec1a2be12003-11-07 21:20:47 +00001609#define IMPLEMENT_VAARG(TY) \
1610 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1611
1612void Interpreter::visitVAArgInst(VAArgInst &I) {
1613 ExecutionContext &SF = ECStack.back();
1614
Brian Gaeke9d20b712004-02-25 23:01:48 +00001615 // Get the incoming valist parameter. LLI treats the valist as a
1616 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001617 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001618 GenericValue Dest;
1619 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001620 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001621 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001622 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001623 case Type::IntegerTyID: {
1624 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Reid Spencere0929362007-03-03 08:38:04 +00001625 if (BitWidth == 1) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001626 Dest.Int1Val = Src.Int1Val;
Reid Spencere0929362007-03-03 08:38:04 +00001627 maskToBitWidth(Dest, BitWidth);
1628 } else if (BitWidth <= 8) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001629 Dest.Int8Val = Src.Int8Val;
Reid Spencere0929362007-03-03 08:38:04 +00001630 maskToBitWidth(Dest, BitWidth);
1631 } else if (BitWidth <= 16) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001632 Dest.Int16Val = Src.Int16Val;
Reid Spencere0929362007-03-03 08:38:04 +00001633 maskToBitWidth(Dest, BitWidth);
1634 } else if (BitWidth <= 32) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001635 Dest.Int32Val = Src.Int32Val;
Reid Spencere0929362007-03-03 08:38:04 +00001636 maskToBitWidth(Dest, BitWidth);
1637 } else if (BitWidth <= 64) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001638 Dest.Int64Val = Src.Int64Val;
Reid Spencere0929362007-03-03 08:38:04 +00001639 maskToBitWidth(Dest, BitWidth);
1640 } else {
1641 *(Dest.APIntVal) = *(Src.APIntVal);
1642 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00001643 }
Brian Gaekec1a2be12003-11-07 21:20:47 +00001644 IMPLEMENT_VAARG(Pointer);
1645 IMPLEMENT_VAARG(Float);
1646 IMPLEMENT_VAARG(Double);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001647 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001648 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Brian Gaekec1a2be12003-11-07 21:20:47 +00001649 abort();
1650 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001651
Brian Gaekec1a2be12003-11-07 21:20:47 +00001652 // Set the Value of this Instruction.
1653 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001654
1655 // Move the pointer to the next vararg.
1656 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001657}
1658
Reid Spencere1aa0662007-03-03 06:22:22 +00001659GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1660 ExecutionContext &SF) {
1661 switch (CE->getOpcode()) {
1662 case Instruction::Trunc:
1663 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1664 case Instruction::ZExt:
1665 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1666 case Instruction::SExt:
1667 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1668 case Instruction::FPTrunc:
1669 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1670 case Instruction::FPExt:
1671 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1672 case Instruction::UIToFP:
1673 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1674 case Instruction::SIToFP:
1675 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1676 case Instruction::FPToUI:
1677 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1678 case Instruction::FPToSI:
1679 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1680 case Instruction::PtrToInt:
1681 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1682 case Instruction::IntToPtr:
1683 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1684 case Instruction::BitCast:
1685 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1686 case Instruction::GetElementPtr:
1687 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1688 gep_type_end(CE), SF);
Reid Spencere0929362007-03-03 08:38:04 +00001689 case Instruction::FCmp:
1690 case Instruction::ICmp:
1691 return executeCmpInst(CE->getPredicate(),
1692 getOperandValue(CE->getOperand(0), SF),
1693 getOperandValue(CE->getOperand(1), SF),
1694 CE->getOperand(0)->getType());
1695 case Instruction::Select:
1696 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1697 getOperandValue(CE->getOperand(1), SF),
1698 getOperandValue(CE->getOperand(2), SF));
Reid Spencere1aa0662007-03-03 06:22:22 +00001699 default :
1700 break;
1701 }
Reid Spencere0929362007-03-03 08:38:04 +00001702
1703 // The cases below here require a GenericValue parameter for the result
1704 // so we initialize one, compute it and then return it.
Reid Spencere1aa0662007-03-03 06:22:22 +00001705 GenericValue Dest;
1706 initializeAPInt(Dest, CE->getType(), SF);
1707 switch (CE->getOpcode()) {
1708 case Instruction::Add:
1709 executeAddInst(Dest, getOperandValue(CE->getOperand(0), SF),
1710 getOperandValue(CE->getOperand(1), SF),
1711 CE->getOperand(0)->getType());
1712 case Instruction::Sub:
1713 executeSubInst(Dest, getOperandValue(CE->getOperand(0), SF),
1714 getOperandValue(CE->getOperand(1), SF),
1715 CE->getOperand(0)->getType());
1716 case Instruction::Mul:
1717 executeMulInst(Dest, getOperandValue(CE->getOperand(0), SF),
1718 getOperandValue(CE->getOperand(1), SF),
1719 CE->getOperand(0)->getType());
1720 case Instruction::SDiv:
1721 executeSDivInst(Dest, getOperandValue(CE->getOperand(0), SF),
1722 getOperandValue(CE->getOperand(1), SF),
1723 CE->getOperand(0)->getType());
1724 case Instruction::UDiv:
1725 executeUDivInst(Dest, getOperandValue(CE->getOperand(0), SF),
1726 getOperandValue(CE->getOperand(1), SF),
1727 CE->getOperand(0)->getType());
1728 case Instruction::FDiv:
1729 executeFDivInst(Dest, getOperandValue(CE->getOperand(0), SF),
1730 getOperandValue(CE->getOperand(1), SF),
1731 CE->getOperand(0)->getType());
1732 case Instruction::URem:
1733 executeURemInst(Dest, getOperandValue(CE->getOperand(0), SF),
1734 getOperandValue(CE->getOperand(1), SF),
1735 CE->getOperand(0)->getType());
1736 case Instruction::SRem:
1737 executeSRemInst(Dest, getOperandValue(CE->getOperand(0), SF),
1738 getOperandValue(CE->getOperand(1), SF),
1739 CE->getOperand(0)->getType());
1740 case Instruction::FRem:
1741 executeFRemInst(Dest, getOperandValue(CE->getOperand(0), SF),
1742 getOperandValue(CE->getOperand(1), SF),
1743 CE->getOperand(0)->getType());
1744 case Instruction::And:
1745 executeAndInst(Dest, getOperandValue(CE->getOperand(0), SF),
1746 getOperandValue(CE->getOperand(1), SF),
1747 CE->getOperand(0)->getType());
1748 case Instruction::Or:
1749 executeOrInst(Dest, getOperandValue(CE->getOperand(0), SF),
1750 getOperandValue(CE->getOperand(1), SF),
1751 CE->getOperand(0)->getType());
1752 case Instruction::Xor:
1753 executeXorInst(Dest, getOperandValue(CE->getOperand(0), SF),
1754 getOperandValue(CE->getOperand(1), SF),
1755 CE->getOperand(0)->getType());
Reid Spencere1aa0662007-03-03 06:22:22 +00001756 case Instruction::Shl:
Reid Spencere0929362007-03-03 08:38:04 +00001757 executeShlInst(Dest, getOperandValue(CE->getOperand(0), SF),
1758 getOperandValue(CE->getOperand(1), SF),
1759 CE->getOperand(0)->getType());
1760 case Instruction::LShr:
1761 executeLShrInst(Dest, getOperandValue(CE->getOperand(0), SF),
Reid Spencere1aa0662007-03-03 06:22:22 +00001762 getOperandValue(CE->getOperand(1), SF),
1763 CE->getOperand(0)->getType());
Reid Spencere1aa0662007-03-03 06:22:22 +00001764 case Instruction::AShr:
Reid Spencere0929362007-03-03 08:38:04 +00001765 executeAShrInst(Dest, getOperandValue(CE->getOperand(0), SF),
1766 getOperandValue(CE->getOperand(1), SF),
1767 CE->getOperand(0)->getType());
Reid Spencere1aa0662007-03-03 06:22:22 +00001768 default:
1769 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
1770 abort();
1771 return GenericValue();
1772 }
Reid Spencere0929362007-03-03 08:38:04 +00001773 return Dest;
Reid Spencere1aa0662007-03-03 06:22:22 +00001774}
1775
1776GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1777 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1778 return getConstantExprValue(CE, SF);
1779 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1780 return getConstantValue(CPV);
1781 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1782 return PTOGV(getPointerToGlobal(GV));
1783 } else {
1784 return SF.Values[V];
1785 }
1786}
1787
Chris Lattner92101ac2001-08-23 17:05:04 +00001788//===----------------------------------------------------------------------===//
1789// Dispatch and Execution Code
1790//===----------------------------------------------------------------------===//
1791
Chris Lattner92101ac2001-08-23 17:05:04 +00001792//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001793// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001794//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001795void Interpreter::callFunction(Function *F,
1796 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001797 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1798 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1799 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001800 // Make a new stack frame... and fill it in.
1801 ECStack.push_back(ExecutionContext());
1802 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001803 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001804
1805 // Special handling for external functions.
Reid Spencer5cbf9852007-01-30 20:08:39 +00001806 if (F->isDeclaration()) {
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001807 GenericValue Result = callExternalFunction (F, ArgVals);
1808 // Simulate a 'ret' instruction of the appropriate type.
1809 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1810 return;
1811 }
1812
1813 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001814 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001815 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001816
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001817 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001818 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001819 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001820 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001821
1822 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001823 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001824 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001825 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001826
1827 // Handle varargs arguments...
1828 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001829}
1830
Chris Lattner92101ac2001-08-23 17:05:04 +00001831void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001832 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001833 // Interpret a single instruction & increment the "PC".
1834 ExecutionContext &SF = ECStack.back(); // Current stack frame
1835 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001836
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001837 // Track the number of dynamic instructions executed.
1838 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001839
Bill Wendling480f0932006-11-27 23:54:50 +00001840 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001841 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001842 }
1843}