blob: 2ddfc97c1b406ffc418e78bd6e1d17eae0b75c52 [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 Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/Statistic.h"
22#include "llvm/Support/Debug.h"
Reid Spencera54b7cb2007-01-12 07:05:14 +000023#include "llvm/Support/MathExtras.h"
Jeff Cohen97af7512006-12-02 02:22:01 +000024#include <cmath>
Chris Lattner4af6de82003-11-25 20:44:56 +000025using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000026
Chris Lattnercecf56b2006-12-19 22:56:53 +000027STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
28static Interpreter *TheEE = 0;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner73011782003-12-28 09:44:37 +000030
Chris Lattner2e42d3a2001-10-15 05:51:48 +000031//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000032// Value Manipulation code
33//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000034
Misha Brukmand1c881a2005-04-21 22:43:08 +000035static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
36 const Type *Ty);
37static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
38 const Type *Ty);
39static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
40 const Type *Ty);
Reid Spencer1628cec2006-10-26 06:15:43 +000041static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
42 const Type *Ty);
43static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
44 const Type *Ty);
45static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
46 const Type *Ty);
Reid Spencer0a783f72006-11-02 01:53:59 +000047static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
48 const Type *Ty);
49static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
50 const Type *Ty);
51static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
52 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000053static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
54 const Type *Ty);
55static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
56 const Type *Ty);
57static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
58 const Type *Ty);
Reid Spencere4d87aa2006-12-23 06:05:41 +000059static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
60 GenericValue Src2, const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000061static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
62 const Type *Ty);
Reid Spencer3822ff52006-11-08 06:47:33 +000063static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
64 const Type *Ty);
65static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
66 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000067static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +000068 GenericValue Src3);
69
Reid Spencera42c7fd2007-01-20 20:12:29 +000070inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
71 // Determine if the value is signed or not
72 bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
73 // If its signed, extend the sign bits
74 if (isSigned)
75 Val |= ~ITy->getBitMask();
76 return Val;
77}
78
Brian Gaeke63438cc2003-12-11 00:22:59 +000079GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
80 ExecutionContext &SF) {
81 switch (CE->getOpcode()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +000082 case Instruction::Trunc:
83 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000084 case Instruction::ZExt:
Reid Spencera54b7cb2007-01-12 07:05:14 +000085 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000086 case Instruction::SExt:
Reid Spencera54b7cb2007-01-12 07:05:14 +000087 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000088 case Instruction::FPTrunc:
Reid Spencera54b7cb2007-01-12 07:05:14 +000089 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000090 case Instruction::FPExt:
Reid Spencera54b7cb2007-01-12 07:05:14 +000091 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000092 case Instruction::UIToFP:
Reid Spencera54b7cb2007-01-12 07:05:14 +000093 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000094 case Instruction::SIToFP:
Reid Spencera54b7cb2007-01-12 07:05:14 +000095 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000096 case Instruction::FPToUI:
Reid Spencera54b7cb2007-01-12 07:05:14 +000097 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000098 case Instruction::FPToSI:
Reid Spencera54b7cb2007-01-12 07:05:14 +000099 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +0000100 case Instruction::PtrToInt:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000101 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +0000102 case Instruction::IntToPtr:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000103 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +0000104 case Instruction::BitCast:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000105 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
Brian Gaeke63438cc2003-12-11 00:22:59 +0000106 case Instruction::GetElementPtr:
107 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
108 gep_type_end(CE), SF);
109 case Instruction::Add:
110 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
111 getOperandValue(CE->getOperand(1), SF),
112 CE->getOperand(0)->getType());
113 case Instruction::Sub:
114 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
115 getOperandValue(CE->getOperand(1), SF),
116 CE->getOperand(0)->getType());
117 case Instruction::Mul:
118 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
119 getOperandValue(CE->getOperand(1), SF),
120 CE->getOperand(0)->getType());
Reid Spencer1628cec2006-10-26 06:15:43 +0000121 case Instruction::SDiv:
122 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
123 getOperandValue(CE->getOperand(1), SF),
124 CE->getOperand(0)->getType());
125 case Instruction::UDiv:
126 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
127 getOperandValue(CE->getOperand(1), SF),
128 CE->getOperand(0)->getType());
129 case Instruction::FDiv:
130 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
131 getOperandValue(CE->getOperand(1), SF),
132 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000133 case Instruction::URem:
134 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
Brian Gaeke63438cc2003-12-11 00:22:59 +0000135 getOperandValue(CE->getOperand(1), SF),
136 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000137 case Instruction::SRem:
138 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
139 getOperandValue(CE->getOperand(1), SF),
140 CE->getOperand(0)->getType());
141 case Instruction::FRem:
142 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
143 getOperandValue(CE->getOperand(1), SF),
144 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000145 case Instruction::And:
146 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
147 getOperandValue(CE->getOperand(1), SF),
148 CE->getOperand(0)->getType());
149 case Instruction::Or:
150 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
151 getOperandValue(CE->getOperand(1), SF),
152 CE->getOperand(0)->getType());
153 case Instruction::Xor:
154 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
155 getOperandValue(CE->getOperand(1), SF),
156 CE->getOperand(0)->getType());
Reid Spencere4d87aa2006-12-23 06:05:41 +0000157 case Instruction::FCmp:
158 case Instruction::ICmp:
159 return executeCmpInst(CE->getPredicate(),
160 getOperandValue(CE->getOperand(0), SF),
161 getOperandValue(CE->getOperand(1), SF),
162 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000163 case Instruction::Shl:
164 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
165 getOperandValue(CE->getOperand(1), SF),
166 CE->getOperand(0)->getType());
Reid Spencer3822ff52006-11-08 06:47:33 +0000167 case Instruction::LShr:
168 return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
169 getOperandValue(CE->getOperand(1), SF),
170 CE->getOperand(0)->getType());
171 case Instruction::AShr:
172 return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
173 getOperandValue(CE->getOperand(1), SF),
174 CE->getOperand(0)->getType());
Chris Lattner759d34f2004-04-20 16:43:21 +0000175 case Instruction::Select:
176 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
177 getOperandValue(CE->getOperand(1), SF),
178 getOperandValue(CE->getOperand(2), SF));
Brian Gaeke63438cc2003-12-11 00:22:59 +0000179 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000180 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Brian Gaeke63438cc2003-12-11 00:22:59 +0000181 abort();
182 return GenericValue();
183 }
184}
Chris Lattnera34c5682002-08-27 22:33:45 +0000185
Brian Gaeke29794cb2003-09-05 18:55:03 +0000186GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000187 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000188 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000189 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000190 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000191 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000192 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000193 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000194 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000195 }
196}
197
Chris Lattner39bb5b42001-10-15 13:25:40 +0000198static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000199 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000200}
201
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000202void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000203 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000204}
205
Chris Lattner2adcd832002-05-03 19:52:30 +0000206//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000207// Binary Instruction Implementations
208//===----------------------------------------------------------------------===//
209
210#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
211 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
212
Reid Spencera54b7cb2007-01-12 07:05:14 +0000213#define IMPLEMENT_INTEGER_BINOP(OP, TY) \
214 case Type::IntegerTyID: { \
215 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
216 if (BitWidth == 1) \
217 Dest.Int1Val = Src1.Int1Val OP Src2.Int1Val; \
218 else if (BitWidth <= 8) \
219 Dest.Int8Val = Src1.Int8Val OP Src2.Int8Val; \
220 else if (BitWidth <= 16) \
221 Dest.Int16Val = Src1.Int16Val OP Src2.Int16Val; \
222 else if (BitWidth <= 32) \
223 Dest.Int32Val = Src1.Int32Val OP Src2.Int32Val; \
224 else if (BitWidth <= 64) \
225 Dest.Int64Val = Src1.Int64Val OP Src2.Int64Val; \
226 else \
227 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
Reid Spencer65367b22007-01-18 02:12:51 +0000228 maskToBitWidth(Dest, BitWidth); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000229 break; \
230 }
231
232#define IMPLEMENT_SIGNED_BINOP(OP, TY) \
233 if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
234 unsigned BitWidth = ITy->getBitWidth(); \
235 if (BitWidth <= 8) \
236 Dest.Int8Val = ((int8_t)Src1.Int8Val) OP ((int8_t)Src2.Int8Val); \
237 else if (BitWidth <= 16) \
238 Dest.Int16Val = ((int16_t)Src1.Int16Val) OP ((int16_t)Src2.Int16Val); \
239 else if (BitWidth <= 32) \
240 Dest.Int32Val = ((int32_t)Src1.Int32Val) OP ((int32_t)Src2.Int32Val); \
241 else if (BitWidth <= 64) \
242 Dest.Int64Val = ((int64_t)Src1.Int64Val) OP ((int64_t)Src2.Int64Val); \
243 else { \
244 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
245 abort(); \
246 } \
Reid Spencer65367b22007-01-18 02:12:51 +0000247 maskToBitWidth(Dest, BitWidth); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000248 } else { \
249 cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
250 abort(); \
251 }
252
253#define IMPLEMENT_UNSIGNED_BINOP(OP, TY) \
254 if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
255 unsigned BitWidth = ITy->getBitWidth(); \
256 if (BitWidth <= 8) \
257 Dest.Int8Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
258 else if (BitWidth <= 16) \
259 Dest.Int16Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
260 else if (BitWidth <= 32) \
261 Dest.Int32Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
262 else if (BitWidth <= 64) \
263 Dest.Int64Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
264 else { \
265 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
266 abort(); \
267 } \
Reid Spencer65367b22007-01-18 02:12:51 +0000268 maskToBitWidth(Dest, BitWidth); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000269 } else { \
270 cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
271 abort(); \
272 }
273
Misha Brukmand1c881a2005-04-21 22:43:08 +0000274static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
275 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000276 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000277 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000278 IMPLEMENT_INTEGER_BINOP(+, Ty);
Chris Lattner92101ac2001-08-23 17:05:04 +0000279 IMPLEMENT_BINARY_OPERATOR(+, Float);
280 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000281 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000282 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000283 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000284 }
285 return Dest;
286}
287
Misha Brukmand1c881a2005-04-21 22:43:08 +0000288static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
289 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000290 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000291 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000292 IMPLEMENT_INTEGER_BINOP(-, Ty);
Chris Lattner92101ac2001-08-23 17:05:04 +0000293 IMPLEMENT_BINARY_OPERATOR(-, Float);
294 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000295 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000296 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000297 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000298 }
299 return Dest;
300}
301
Misha Brukmand1c881a2005-04-21 22:43:08 +0000302static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
303 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000304 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000305 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000306 IMPLEMENT_INTEGER_BINOP(*, Ty);
Chris Lattnerc2593162001-10-27 08:28:11 +0000307 IMPLEMENT_BINARY_OPERATOR(*, Float);
308 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000309 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000310 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000311 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000312 }
313 return Dest;
314}
315
Reid Spencer1628cec2006-10-26 06:15:43 +0000316static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
317 const Type *Ty) {
318 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000319 IMPLEMENT_UNSIGNED_BINOP(/,Ty)
Reid Spencer1628cec2006-10-26 06:15:43 +0000320 return Dest;
321}
322
323static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
324 const Type *Ty) {
325 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000326 IMPLEMENT_SIGNED_BINOP(/,Ty)
Reid Spencer1628cec2006-10-26 06:15:43 +0000327 return Dest;
328}
329
330static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000331 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000332 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000333 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000334 IMPLEMENT_BINARY_OPERATOR(/, Float);
335 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000336 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000337 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000338 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000339 }
340 return Dest;
341}
342
Reid Spencer0a783f72006-11-02 01:53:59 +0000343static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000344 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000345 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000346 IMPLEMENT_UNSIGNED_BINOP(%, Ty)
Reid Spencer0a783f72006-11-02 01:53:59 +0000347 return Dest;
348}
349
350static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
351 const Type *Ty) {
352 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000353 IMPLEMENT_SIGNED_BINOP(%, Ty)
Reid Spencer0a783f72006-11-02 01:53:59 +0000354 return Dest;
355}
356
357static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
358 const Type *Ty) {
359 GenericValue Dest;
360 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000361 case Type::FloatTyID:
362 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
363 break;
364 case Type::DoubleTyID:
365 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
366 break;
367 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000368 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000369 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000370 }
371 return Dest;
372}
373
Misha Brukmand1c881a2005-04-21 22:43:08 +0000374static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
375 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000376 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000377 IMPLEMENT_UNSIGNED_BINOP(&, Ty)
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000378 return Dest;
379}
380
Misha Brukmand1c881a2005-04-21 22:43:08 +0000381static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000382 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000383 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000384 IMPLEMENT_UNSIGNED_BINOP(|, Ty)
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000385 return Dest;
386}
387
Misha Brukmand1c881a2005-04-21 22:43:08 +0000388static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000389 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000390 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000391 IMPLEMENT_UNSIGNED_BINOP(^, Ty)
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000392 return Dest;
393}
394
Reid Spencera54b7cb2007-01-12 07:05:14 +0000395#define IMPLEMENT_SIGNED_ICMP(OP, TY) \
396 case Type::IntegerTyID: { \
Reid Spencera42c7fd2007-01-20 20:12:29 +0000397 const IntegerType* ITy = cast<IntegerType>(TY); \
398 unsigned BitWidth = ITy->getBitWidth(); \
399 int64_t LHS = 0, RHS = 0; \
400 if (BitWidth <= 8) { \
401 LHS = int64_t(doSignExtension(uint64_t(Src1.Int8Val), ITy)); \
402 RHS = int64_t(doSignExtension(uint64_t(Src2.Int8Val), ITy)); \
403 } else if (BitWidth <= 16) { \
404 LHS = int64_t(doSignExtension(uint64_t(Src1.Int16Val), ITy)); \
405 RHS = int64_t(doSignExtension(uint64_t(Src2.Int16Val), ITy)); \
406 } else if (BitWidth <= 32) { \
407 LHS = int64_t(doSignExtension(uint64_t(Src1.Int32Val), ITy)); \
408 RHS = int64_t(doSignExtension(uint64_t(Src2.Int32Val), ITy)); \
409 } else if (BitWidth <= 64) { \
410 LHS = int64_t(doSignExtension(uint64_t(Src1.Int64Val), ITy)); \
411 RHS = int64_t(doSignExtension(uint64_t(Src2.Int64Val), ITy)); \
412 } else { \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000413 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
414 abort(); \
415 } \
Reid Spencera42c7fd2007-01-20 20:12:29 +0000416 Dest.Int1Val = LHS OP RHS; \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000417 break; \
418 }
419
420#define IMPLEMENT_UNSIGNED_ICMP(OP, TY) \
421 case Type::IntegerTyID: { \
422 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
423 if (BitWidth == 1) \
424 Dest.Int1Val = ((uint8_t)Src1.Int1Val) OP ((uint8_t)Src2.Int1Val); \
425 else if (BitWidth <= 8) \
426 Dest.Int1Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
427 else if (BitWidth <= 16) \
428 Dest.Int1Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
429 else if (BitWidth <= 32) \
430 Dest.Int1Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
431 else if (BitWidth <= 64) \
432 Dest.Int1Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
433 else { \
434 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
435 abort(); \
436 } \
Reid Spencer65367b22007-01-18 02:12:51 +0000437 maskToBitWidth(Dest, BitWidth); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000438 break; \
439 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000440
Chris Lattnerfd506f52003-04-23 19:55:35 +0000441// Handle pointers specially because they must be compared with only as much
442// width as the host has. We _do not_ want to be comparing 64 bit values when
443// running on a 32-bit target, otherwise the upper 32 bits might mess up
444// comparisons if they contain garbage.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000445#define IMPLEMENT_POINTER_ICMP(OP) \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000446 case Type::PointerTyID: \
Reid Spencer4fe16d62007-01-11 18:21:29 +0000447 Dest.Int1Val = (void*)(intptr_t)Src1.PointerVal OP \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000448 (void*)(intptr_t)Src2.PointerVal; break
449
Reid Spencere4d87aa2006-12-23 06:05:41 +0000450static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
451 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000452 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000453 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000454 IMPLEMENT_UNSIGNED_ICMP(==, Ty);
455 IMPLEMENT_POINTER_ICMP(==);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000456 default:
457 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
458 abort();
459 }
460 return Dest;
461}
462
463static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
464 const Type *Ty) {
465 GenericValue Dest;
466 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000467 IMPLEMENT_UNSIGNED_ICMP(!=, Ty);
468 IMPLEMENT_POINTER_ICMP(!=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000469 default:
470 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
471 abort();
472 }
473 return Dest;
474}
475
476static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
477 const Type *Ty) {
478 GenericValue Dest;
479 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000480 IMPLEMENT_UNSIGNED_ICMP(<, Ty);
481 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000482 default:
483 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
484 abort();
485 }
486 return Dest;
487}
488
489static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
490 const Type *Ty) {
491 GenericValue Dest;
492 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000493 IMPLEMENT_SIGNED_ICMP(<, Ty);
494 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000495 default:
496 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
497 abort();
498 }
499 return Dest;
500}
501
502static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
503 const Type *Ty) {
504 GenericValue Dest;
505 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000506 IMPLEMENT_UNSIGNED_ICMP(>, Ty);
507 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000508 default:
509 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
510 abort();
511 }
512 return Dest;
513}
514
515static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
516 const Type *Ty) {
517 GenericValue Dest;
518 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000519 IMPLEMENT_SIGNED_ICMP(>, Ty);
520 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000521 default:
522 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
523 abort();
524 }
525 return Dest;
526}
527
528static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
529 const Type *Ty) {
530 GenericValue Dest;
531 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000532 IMPLEMENT_UNSIGNED_ICMP(<=, Ty);
533 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000534 default:
535 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
536 abort();
537 }
538 return Dest;
539}
540
541static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
542 const Type *Ty) {
543 GenericValue Dest;
544 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000545 IMPLEMENT_SIGNED_ICMP(<=, Ty);
546 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000547 default:
548 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
549 abort();
550 }
551 return Dest;
552}
553
554static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
555 const Type *Ty) {
556 GenericValue Dest;
557 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000558 IMPLEMENT_UNSIGNED_ICMP(>=,Ty);
559 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000560 default:
561 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
562 abort();
563 }
564 return Dest;
565}
566
567static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
568 const Type *Ty) {
569 GenericValue Dest;
570 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000571 IMPLEMENT_SIGNED_ICMP(>=, Ty);
572 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000573 default:
574 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
575 abort();
576 }
577 return Dest;
578}
579
Reid Spencere49661b2006-12-31 05:51:36 +0000580void Interpreter::visitICmpInst(ICmpInst &I) {
581 ExecutionContext &SF = ECStack.back();
582 const Type *Ty = I.getOperand(0)->getType();
583 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
584 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
585 GenericValue R; // Result
586
587 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000588 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
589 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
Reid Spencere49661b2006-12-31 05:51:36 +0000590 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
591 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
592 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
593 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
594 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
595 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
596 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
597 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
598 default:
599 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
600 abort();
601 }
602
603 SetValue(&I, R, SF);
604}
605
606#define IMPLEMENT_FCMP(OP, TY) \
Reid Spencer4fe16d62007-01-11 18:21:29 +0000607 case Type::TY##TyID: Dest.Int1Val = Src1.TY##Val OP Src2.TY##Val; break
Reid Spencere4d87aa2006-12-23 06:05:41 +0000608
Reid Spencera54b7cb2007-01-12 07:05:14 +0000609static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000610 const Type *Ty) {
611 GenericValue Dest;
612 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000613 IMPLEMENT_FCMP(==, Float);
614 IMPLEMENT_FCMP(==, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000615 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000616 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000617 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000618 }
619 return Dest;
620}
621
Reid Spencera54b7cb2007-01-12 07:05:14 +0000622static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000623 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000624 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000625 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000626 IMPLEMENT_FCMP(!=, Float);
627 IMPLEMENT_FCMP(!=, Double);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000628
Chris Lattner92101ac2001-08-23 17:05:04 +0000629 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000630 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000631 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000632 }
633 return Dest;
634}
635
Reid Spencera54b7cb2007-01-12 07:05:14 +0000636static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000637 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000638 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000639 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000640 IMPLEMENT_FCMP(<=, Float);
641 IMPLEMENT_FCMP(<=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000642 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000643 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000644 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000645 }
646 return Dest;
647}
648
Reid Spencera54b7cb2007-01-12 07:05:14 +0000649static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000650 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000651 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000652 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000653 IMPLEMENT_FCMP(>=, Float);
654 IMPLEMENT_FCMP(>=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000655 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000656 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000657 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000658 }
659 return Dest;
660}
661
Reid Spencera54b7cb2007-01-12 07:05:14 +0000662static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000663 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000664 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000665 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000666 IMPLEMENT_FCMP(<, Float);
667 IMPLEMENT_FCMP(<, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000668 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000669 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000670 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000671 }
672 return Dest;
673}
674
Reid Spencera54b7cb2007-01-12 07:05:14 +0000675static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000676 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000677 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000678 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000679 IMPLEMENT_FCMP(>, Float);
680 IMPLEMENT_FCMP(>, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000681 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000682 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000683 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000684 }
685 return Dest;
686}
687
Reid Spencera54b7cb2007-01-12 07:05:14 +0000688#define IMPLEMENT_UNORDERED(TY, X,Y) \
689 if (TY == Type::FloatTy) \
690 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
691 Dest.Int1Val = true; \
692 return Dest; \
693 } \
694 else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
695 Dest.Int1Val = true; \
696 return Dest; \
697 }
698
699
700static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
701 const Type *Ty) {
702 GenericValue Dest;
703 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
704 return executeFCMP_OEQ(Src1, Src2, Ty);
705}
706
707static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
708 const Type *Ty) {
709 GenericValue Dest;
710 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
711 return executeFCMP_ONE(Src1, Src2, Ty);
712}
713
714static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
715 const Type *Ty) {
716 GenericValue Dest;
717 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
718 return executeFCMP_OLE(Src1, Src2, Ty);
719}
720
721static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
722 const Type *Ty) {
723 GenericValue Dest;
724 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
725 return executeFCMP_OGE(Src1, Src2, Ty);
726}
727
728static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
729 const Type *Ty) {
730 GenericValue Dest;
731 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
732 return executeFCMP_OLT(Src1, Src2, Ty);
733}
734
735static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
736 const Type *Ty) {
737 GenericValue Dest;
738 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
739 return executeFCMP_OGT(Src1, Src2, Ty);
740}
741
742static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
743 const Type *Ty) {
744 GenericValue Dest;
745 if (Ty == Type::FloatTy)
746 Dest.Int1Val = (Src1.FloatVal == Src1.FloatVal &&
747 Src2.FloatVal == Src2.FloatVal);
748 else
749 Dest.Int1Val = (Src1.DoubleVal == Src1.DoubleVal &&
750 Src2.DoubleVal == Src2.DoubleVal);
751 return Dest;
752}
753
754static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
755 const Type *Ty) {
756 GenericValue Dest;
757 if (Ty == Type::FloatTy)
758 Dest.Int1Val = (Src1.FloatVal != Src1.FloatVal ||
759 Src2.FloatVal != Src2.FloatVal);
760 else
761 Dest.Int1Val = (Src1.DoubleVal != Src1.DoubleVal ||
762 Src2.DoubleVal != Src2.DoubleVal);
763 return Dest;
764}
765
Reid Spencere4d87aa2006-12-23 06:05:41 +0000766void Interpreter::visitFCmpInst(FCmpInst &I) {
767 ExecutionContext &SF = ECStack.back();
768 const Type *Ty = I.getOperand(0)->getType();
769 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
770 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
771 GenericValue R; // Result
772
773 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000774 case FCmpInst::FCMP_FALSE: R.Int1Val = false; break;
775 case FCmpInst::FCMP_TRUE: R.Int1Val = true; break;
776 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
777 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
778 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
779 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
780 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
781 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
782 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
783 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
784 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
785 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
786 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
787 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
788 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
789 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000790 default:
791 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
792 abort();
793 }
794
795 SetValue(&I, R, SF);
796}
797
Reid Spencere4d87aa2006-12-23 06:05:41 +0000798static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
799 GenericValue Src2, const Type *Ty) {
800 GenericValue Result;
801 switch (predicate) {
802 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
803 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
804 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
805 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
806 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
807 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
808 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
809 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
810 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
811 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000812 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
813 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
814 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
815 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
816 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
817 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
818 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
819 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
820 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
821 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
822 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
823 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
824 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
825 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000826 case FCmpInst::FCMP_FALSE: {
827 GenericValue Result;
Reid Spencer4fe16d62007-01-11 18:21:29 +0000828 Result.Int1Val = false;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000829 return Result;
830 }
831 case FCmpInst::FCMP_TRUE: {
832 GenericValue Result;
Reid Spencer4fe16d62007-01-11 18:21:29 +0000833 Result.Int1Val = true;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000834 return Result;
835 }
836 default:
837 cerr << "Unhandled Cmp predicate\n";
838 abort();
839 }
840}
841
Chris Lattnerd7916e92003-05-10 21:22:39 +0000842void Interpreter::visitBinaryOperator(BinaryOperator &I) {
843 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000844 const Type *Ty = I.getOperand(0)->getType();
845 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
846 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000847 GenericValue R; // Result
848
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000849 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000850 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
851 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
852 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000853 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
854 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
855 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
Reid Spencer0a783f72006-11-02 01:53:59 +0000856 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
857 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
858 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000859 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
860 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
861 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000862 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000863 cerr << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner02868352003-04-22 21:22:33 +0000864 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000865 }
866
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000867 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000868}
869
Misha Brukmand1c881a2005-04-21 22:43:08 +0000870static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000871 GenericValue Src3) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000872 return Src1.Int1Val ? Src2 : Src3;
Chris Lattner759d34f2004-04-20 16:43:21 +0000873}
874
875void Interpreter::visitSelectInst(SelectInst &I) {
876 ExecutionContext &SF = ECStack.back();
877 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
878 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
879 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
880 GenericValue R = executeSelectInst(Src1, Src2, Src3);
881 SetValue(&I, R, SF);
882}
883
884
Chris Lattner92101ac2001-08-23 17:05:04 +0000885//===----------------------------------------------------------------------===//
886// Terminator Instruction Implementations
887//===----------------------------------------------------------------------===//
888
Chris Lattnere43db882001-10-27 04:15:57 +0000889void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000890 // runAtExitHandlers() assumes there are no stack frames, but
891 // if exit() was called, then it had a stack frame. Blow away
892 // the stack before interpreting atexit handlers.
893 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000894 runAtExitHandlers ();
Reid Spencere49661b2006-12-31 05:51:36 +0000895 exit (GV.Int32Val);
Chris Lattnere43db882001-10-27 04:15:57 +0000896}
897
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000898/// Pop the last stack frame off of ECStack and then copy the result
899/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000900/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000901/// CallInst if there was a previous stack frame. This method may
902/// invalidate any ECStack iterators you have. This method also takes
903/// care of switching to the normal destination BB, if we are returning
904/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000905///
906void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
907 GenericValue Result) {
908 // Pop the current stack frame.
909 ECStack.pop_back();
910
Misha Brukmand1c881a2005-04-21 22:43:08 +0000911 if (ECStack.empty()) { // Finished main. Put result into exit code...
Chris Lattner42a75512007-01-15 02:27:26 +0000912 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000913 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000914 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000915 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000916 }
917 } else {
918 // If we have a previous stack frame, and we have a previous call,
919 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000920 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000921 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000922 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000923 SetValue(I, Result, CallingSF);
924 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
925 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000926 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000927 }
928 }
929}
930
Chris Lattnerd7916e92003-05-10 21:22:39 +0000931void Interpreter::visitReturnInst(ReturnInst &I) {
932 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000933 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000934 GenericValue Result;
935
936 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000937 if (I.getNumOperands()) {
938 RetTy = I.getReturnValue()->getType();
939 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000940 }
941
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000942 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000943}
944
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000945void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000946 // Unwind stack
947 Instruction *Inst;
948 do {
949 ECStack.pop_back ();
950 if (ECStack.empty ())
951 abort ();
952 Inst = ECStack.back ().Caller.getInstruction ();
953 } while (!(Inst && isa<InvokeInst> (Inst)));
954
955 // Return from invoke
956 ExecutionContext &InvokingSF = ECStack.back ();
957 InvokingSF.Caller = CallSite ();
958
959 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000960 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000961}
962
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000963void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000964 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000965 abort();
966}
967
Chris Lattnerd7916e92003-05-10 21:22:39 +0000968void Interpreter::visitBranchInst(BranchInst &I) {
969 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000970 BasicBlock *Dest;
971
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000972 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
973 if (!I.isUnconditional()) {
974 Value *Cond = I.getCondition();
Reid Spencer4fe16d62007-01-11 18:21:29 +0000975 if (getOperandValue(Cond, SF).Int1Val == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000976 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000977 }
Chris Lattner77113b62003-05-10 20:21:16 +0000978 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000979}
980
Chris Lattnerd7916e92003-05-10 21:22:39 +0000981void Interpreter::visitSwitchInst(SwitchInst &I) {
982 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000983 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
984 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000985
986 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000987 BasicBlock *Dest = 0;
988 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Reid Spencere4d87aa2006-12-23 06:05:41 +0000989 if (executeICMP_EQ(CondVal,
Reid Spencer4fe16d62007-01-11 18:21:29 +0000990 getOperandValue(I.getOperand(i), SF), ElTy).Int1Val) {
Chris Lattner09e93922003-04-22 20:34:47 +0000991 Dest = cast<BasicBlock>(I.getOperand(i+1));
992 break;
993 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000994
Chris Lattner09e93922003-04-22 20:34:47 +0000995 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000996 SwitchToNewBasicBlock(Dest, SF);
997}
998
999// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
1000// This function handles the actual updating of block and instruction iterators
1001// as well as execution of all of the PHI nodes in the destination block.
1002//
1003// This method does this because all of the PHI nodes must be executed
1004// atomically, reading their inputs before any of the results are updated. Not
1005// doing this can cause problems if the PHI nodes depend on other PHI nodes for
1006// their inputs. If the input PHI node is updated before it is read, incorrect
1007// results can happen. Thus we use a two phase approach.
1008//
1009void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
1010 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
1011 SF.CurBB = Dest; // Update CurBB to branch destination
1012 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
1013
1014 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
1015
1016 // Loop over all of the PHI nodes in the current block, reading their inputs.
1017 std::vector<GenericValue> ResultValues;
1018
1019 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
1020 // Search for the value corresponding to this previous bb...
1021 int i = PN->getBasicBlockIndex(PrevBB);
1022 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
1023 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +00001024
Chris Lattner77113b62003-05-10 20:21:16 +00001025 // Save the incoming value for this PHI node...
1026 ResultValues.push_back(getOperandValue(IncomingValue, SF));
1027 }
1028
1029 // Now loop over all of the PHI nodes setting their values...
1030 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +00001031 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
1032 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +00001033 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +00001034 }
Chris Lattner09e93922003-04-22 20:34:47 +00001035}
1036
Chris Lattner92101ac2001-08-23 17:05:04 +00001037//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +00001038// Memory Instruction Implementations
1039//===----------------------------------------------------------------------===//
1040
Chris Lattnerd7916e92003-05-10 21:22:39 +00001041void Interpreter::visitAllocationInst(AllocationInst &I) {
1042 ExecutionContext &SF = ECStack.back();
1043
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001044 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +00001045
Chris Lattnercc82cc12002-04-28 21:57:33 +00001046 // Get the number of elements being allocated by the array...
Reid Spencere49661b2006-12-31 05:51:36 +00001047 unsigned NumElements = getOperandValue(I.getOperand(0), SF).Int32Val;
Chris Lattner86660982001-08-27 05:16:50 +00001048
1049 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +00001050 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +00001051
Chris Lattnerfe11a972002-12-23 23:59:41 +00001052 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001053 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001054 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001055
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001056 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +00001057 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +00001058}
1059
Chris Lattnerd7916e92003-05-10 21:22:39 +00001060void Interpreter::visitFreeInst(FreeInst &I) {
1061 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001062 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
1063 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +00001064 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +00001065 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +00001066}
1067
Chris Lattnera34c5682002-08-27 22:33:45 +00001068// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +00001069//
Chris Lattner4af6de82003-11-25 20:44:56 +00001070GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001071 gep_type_iterator E,
1072 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001073 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +00001074 "Cannot getElementOffset of a nonpointer type!");
1075
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001076 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +00001077
1078 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +00001079 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +00001080 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +00001081
Reid Spencerb83eb642006-10-20 07:07:24 +00001082 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
1083 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +00001084
Chris Lattnerd5644962005-01-08 20:05:34 +00001085 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +00001086 } else {
1087 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +00001088 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +00001089 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
1090
Reid Spencera54b7cb2007-01-12 07:05:14 +00001091 int64_t Idx;
1092 unsigned BitWidth =
1093 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
Reid Spencer23e28832007-01-18 01:25:42 +00001094 if (BitWidth == 32)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001095 Idx = (int64_t)(int32_t)IdxGV.Int32Val;
Reid Spencer23e28832007-01-18 01:25:42 +00001096 else if (BitWidth == 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001097 Idx = (int64_t)IdxGV.Int64Val;
1098 else
Reid Spencer23e28832007-01-18 01:25:42 +00001099 assert(0 && "Invalid index type for getelementptr");
Chris Lattnerd5644962005-01-08 20:05:34 +00001100 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001101 }
Chris Lattner95c3af52001-10-29 19:32:19 +00001102 }
1103
Chris Lattnera34c5682002-08-27 22:33:45 +00001104 GenericValue Result;
1105 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
1106 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +00001107}
1108
Chris Lattnerd7916e92003-05-10 21:22:39 +00001109void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1110 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +00001111 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +00001112 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +00001113}
1114
Chris Lattnerd7916e92003-05-10 21:22:39 +00001115void Interpreter::visitLoadInst(LoadInst &I) {
1116 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001117 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +00001118 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +00001119 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001120 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001121}
1122
Chris Lattnerd7916e92003-05-10 21:22:39 +00001123void Interpreter::visitStoreInst(StoreInst &I) {
1124 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +00001125 GenericValue Val = getOperandValue(I.getOperand(0), SF);
1126 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +00001127 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +00001128 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +00001129}
1130
Chris Lattner86660982001-08-27 05:16:50 +00001131//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +00001132// Miscellaneous Instruction Implementations
1133//===----------------------------------------------------------------------===//
1134
Brian Gaekefea483d2003-11-07 20:04:22 +00001135void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001136 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +00001137
1138 // Check to see if this is an intrinsic function call...
1139 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +00001140 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +00001141 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +00001142 case Intrinsic::not_intrinsic:
1143 break;
Chris Lattner317201d2004-03-13 00:24:00 +00001144 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +00001145 GenericValue ArgIndex;
1146 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1147 ArgIndex.UIntPairVal.second = 0;
1148 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +00001149 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +00001150 }
Chris Lattner317201d2004-03-13 00:24:00 +00001151 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +00001152 return;
Chris Lattner317201d2004-03-13 00:24:00 +00001153 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +00001154 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1155 return;
1156 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +00001157 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +00001158 // class to transform it into hopefully tasty LLVM code.
1159 //
1160 Instruction *Prev = CS.getInstruction()->getPrev();
1161 BasicBlock *Parent = CS.getInstruction()->getParent();
1162 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1163
1164 // Restore the CurInst pointer to the first instruction newly inserted, if
1165 // any.
1166 if (!Prev) {
1167 SF.CurInst = Parent->begin();
1168 } else {
1169 SF.CurInst = Prev;
1170 ++SF.CurInst;
1171 }
Brian Gaekeb440dea2004-04-23 18:05:28 +00001172 return;
Chris Lattner73011782003-12-28 09:44:37 +00001173 }
1174
Brian Gaekefea483d2003-11-07 20:04:22 +00001175 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +00001176 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +00001177 const unsigned NumArgs = SF.Caller.arg_size();
1178 ArgVals.reserve(NumArgs);
1179 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1180 e = SF.Caller.arg_end(); i != e; ++i) {
1181 Value *V = *i;
1182 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +00001183 // Promote all integral types whose size is < sizeof(int) into ints. We do
1184 // this by zero or sign extending the value as appropriate according to the
1185 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +00001186 const Type *Ty = V->getType();
Chris Lattner42a75512007-01-15 02:27:26 +00001187 if (Ty->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001188 if (Ty->getPrimitiveSizeInBits() == 1)
Reid Spencer4fe16d62007-01-11 18:21:29 +00001189 ArgVals.back().Int32Val = ArgVals.back().Int1Val;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001190 else if (Ty->getPrimitiveSizeInBits() <= 8)
1191 ArgVals.back().Int32Val = ArgVals.back().Int8Val;
1192 else if (Ty->getPrimitiveSizeInBits() <= 16)
1193 ArgVals.back().Int32Val = ArgVals.back().Int16Val;
Chris Lattner93780132003-01-13 00:58:52 +00001194 }
1195 }
Chris Lattner365a76e2001-09-10 04:49:44 +00001196
Misha Brukmand1c881a2005-04-21 22:43:08 +00001197 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001198 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +00001199 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +00001200 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +00001201}
1202
Brian Gaeke63438cc2003-12-11 00:22:59 +00001203static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
1204 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +00001205 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001206 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1207 unsigned BitWidth = ITy->getBitWidth();
Reid Spencer65367b22007-01-18 02:12:51 +00001208 if (BitWidth <= 8)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001209 Dest.Int8Val = ((uint8_t)Src1.Int8Val) << ((uint32_t)Src2.Int8Val);
Reid Spencer65367b22007-01-18 02:12:51 +00001210 else if (BitWidth <= 16)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001211 Dest.Int16Val = ((uint16_t)Src1.Int16Val) << ((uint32_t)Src2.Int8Val);
Reid Spencer65367b22007-01-18 02:12:51 +00001212 else if (BitWidth <= 32)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001213 Dest.Int32Val = ((uint32_t)Src1.Int32Val) << ((uint32_t)Src2.Int8Val);
Reid Spencer65367b22007-01-18 02:12:51 +00001214 else if (BitWidth <= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001215 Dest.Int64Val = ((uint64_t)Src1.Int64Val) << ((uint32_t)Src2.Int8Val);
Reid Spencer65367b22007-01-18 02:12:51 +00001216 else {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001217 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1218 abort();
1219 }
Reid Spencer65367b22007-01-18 02:12:51 +00001220 maskToBitWidth(Dest, BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001221 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001222 cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
Reid Spencera54b7cb2007-01-12 07:05:14 +00001223 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001224 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001225 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001226}
1227
Reid Spencer3822ff52006-11-08 06:47:33 +00001228static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
1229 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +00001230 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001231 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1232 unsigned BitWidth = ITy->getBitWidth();
1233 if (BitWidth <= 8)
1234 Dest.Int8Val = ((uint8_t)Src1.Int8Val) >> ((uint32_t)Src2.Int8Val);
1235 else if (BitWidth <= 16)
1236 Dest.Int16Val = ((uint16_t)Src1.Int16Val) >> ((uint32_t)Src2.Int8Val);
1237 else if (BitWidth <= 32)
1238 Dest.Int32Val = ((uint32_t)Src1.Int32Val) >> ((uint32_t)Src2.Int8Val);
1239 else if (BitWidth <= 64)
1240 Dest.Int64Val = ((uint64_t)Src1.Int64Val) >> ((uint32_t)Src2.Int8Val);
1241 else {
1242 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1243 abort();
1244 }
Reid Spencer65367b22007-01-18 02:12:51 +00001245 maskToBitWidth(Dest, BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001246 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001247 cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
Reid Spencer3822ff52006-11-08 06:47:33 +00001248 abort();
1249 }
1250 return Dest;
1251}
1252
1253static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
1254 const Type *Ty) {
1255 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001256 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1257 unsigned BitWidth = ITy->getBitWidth();
1258 if (BitWidth <= 8)
1259 Dest.Int8Val = ((int8_t)Src1.Int8Val) >> ((int32_t)Src2.Int8Val);
1260 else if (BitWidth <= 16)
1261 Dest.Int16Val = ((int16_t)Src1.Int16Val) >> ((int32_t)Src2.Int8Val);
1262 else if (BitWidth <= 32)
1263 Dest.Int32Val = ((int32_t)Src1.Int32Val) >> ((int32_t)Src2.Int8Val);
1264 else if (BitWidth <= 64)
1265 Dest.Int64Val = ((int64_t)Src1.Int64Val) >> ((int32_t)Src2.Int8Val);
1266 else {
1267 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
1268 abort();
1269 }
Reid Spencer65367b22007-01-18 02:12:51 +00001270 maskToBitWidth(Dest, BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001271 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001272 cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +00001273 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001274 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001275 return Dest;
1276}
1277
1278void Interpreter::visitShl(ShiftInst &I) {
1279 ExecutionContext &SF = ECStack.back();
1280 const Type *Ty = I.getOperand(0)->getType();
1281 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1282 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1283 GenericValue Dest;
1284 Dest = executeShlInst (Src1, Src2, Ty);
1285 SetValue(&I, Dest, SF);
1286}
1287
Reid Spencer3822ff52006-11-08 06:47:33 +00001288void Interpreter::visitLShr(ShiftInst &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001289 ExecutionContext &SF = ECStack.back();
1290 const Type *Ty = I.getOperand(0)->getType();
1291 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1292 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1293 GenericValue Dest;
Reid Spencer3822ff52006-11-08 06:47:33 +00001294 Dest = executeLShrInst (Src1, Src2, Ty);
1295 SetValue(&I, Dest, SF);
1296}
1297
1298void Interpreter::visitAShr(ShiftInst &I) {
1299 ExecutionContext &SF = ECStack.back();
1300 const Type *Ty = I.getOperand(0)->getType();
1301 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1302 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1303 GenericValue Dest;
1304 Dest = executeAShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001305 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001306}
1307
Reid Spencer23e28832007-01-18 01:25:42 +00001308#define INTEGER_ASSIGN(DEST, BITWIDTH, VAL) \
1309 { \
1310 uint64_t Mask = (1ull << BITWIDTH) - 1; \
1311 if (BITWIDTH == 1) { \
1312 Dest.Int1Val = (bool) (VAL & Mask); \
1313 } else if (BITWIDTH <= 8) { \
1314 Dest.Int8Val = (uint8_t) (VAL & Mask); \
1315 } else if (BITWIDTH <= 16) { \
1316 Dest.Int16Val = (uint16_t) (VAL & Mask); \
1317 } else if (BITWIDTH <= 32) { \
1318 Dest.Int32Val = (uint32_t) (VAL & Mask); \
1319 } else \
1320 Dest.Int64Val = (uint64_t) (VAL & Mask); \
1321 }
Chris Lattner86660982001-08-27 05:16:50 +00001322
Reid Spencera54b7cb2007-01-12 07:05:14 +00001323GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
1324 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001325 const Type *SrcTy = SrcVal->getType();
1326 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001327 const IntegerType *DITy = cast<IntegerType>(DstTy);
1328 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1329 unsigned DBitWidth = DITy->getBitWidth();
1330 unsigned SBitWidth = SITy->getBitWidth();
1331 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1332 "Integer types > 64 bits not supported");
1333 assert(SBitWidth > DBitWidth && "Invalid truncate");
Chris Lattner86660982001-08-27 05:16:50 +00001334
Reid Spencera54b7cb2007-01-12 07:05:14 +00001335 // Mask the source value to its actual bit width. This ensures that any
1336 // high order bits are cleared.
1337 uint64_t Mask = (1ULL << DBitWidth) - 1;
1338 uint64_t MaskedVal = 0;
1339 if (SBitWidth <= 8)
1340 MaskedVal = Src.Int8Val & Mask;
1341 else if (SBitWidth <= 16)
1342 MaskedVal = Src.Int16Val & Mask;
1343 else if (SBitWidth <= 32)
1344 MaskedVal = Src.Int32Val & Mask;
1345 else
1346 MaskedVal = Src.Int64Val & Mask;
Chris Lattnera34c5682002-08-27 22:33:45 +00001347
Reid Spencera54b7cb2007-01-12 07:05:14 +00001348 INTEGER_ASSIGN(Dest, DBitWidth, MaskedVal);
Chris Lattnera34c5682002-08-27 22:33:45 +00001349 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001350}
Chris Lattner92101ac2001-08-23 17:05:04 +00001351
Reid Spencera54b7cb2007-01-12 07:05:14 +00001352GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
1353 ExecutionContext &SF) {
1354 const Type *SrcTy = SrcVal->getType();
1355 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1356 const IntegerType *DITy = cast<IntegerType>(DstTy);
1357 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1358 unsigned DBitWidth = DITy->getBitWidth();
1359 unsigned SBitWidth = SITy->getBitWidth();
1360 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1361 "Integer types > 64 bits not supported");
1362 assert(SBitWidth < DBitWidth && "Invalid sign extend");
Reid Spencerc00a4302007-01-20 08:32:52 +00001363
1364 // Normalize to a 64-bit value.
1365 uint64_t Normalized = 0;
1366 if (SBitWidth <= 8)
1367 Normalized = Src.Int8Val;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001368 else if (SBitWidth <= 16)
Reid Spencerc00a4302007-01-20 08:32:52 +00001369 Normalized = Src.Int16Val;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001370 else if (SBitWidth <= 32)
Reid Spencerc00a4302007-01-20 08:32:52 +00001371 Normalized = Src.Int32Val;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001372 else
Reid Spencerc00a4302007-01-20 08:32:52 +00001373 Normalized = Src.Int64Val;
1374
Reid Spencera42c7fd2007-01-20 20:12:29 +00001375 Normalized = doSignExtension(Normalized, SITy);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001376
1377 // Now that we have a sign extended value, assign it to the destination
Reid Spencerc00a4302007-01-20 08:32:52 +00001378 INTEGER_ASSIGN(Dest, DBitWidth, Normalized);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001379 return Dest;
1380}
1381
1382GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
1383 ExecutionContext &SF) {
1384 const Type *SrcTy = SrcVal->getType();
1385 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1386 const IntegerType *DITy = cast<IntegerType>(DstTy);
1387 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1388 unsigned DBitWidth = DITy->getBitWidth();
1389 unsigned SBitWidth = SITy->getBitWidth();
1390 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1391 "Integer types > 64 bits not supported");
1392 assert(SBitWidth < DBitWidth && "Invalid sign extend");
1393 uint64_t Extended = 0;
1394 if (SBitWidth == 1)
1395 // For sign extension from bool, we must extend the source bits.
1396 Extended = (uint64_t) (Src.Int1Val & 1);
1397 else if (SBitWidth <= 8)
1398 Extended = (uint64_t) (uint8_t)Src.Int8Val;
1399 else if (SBitWidth <= 16)
1400 Extended = (uint64_t) (uint16_t)Src.Int16Val;
1401 else if (SBitWidth <= 32)
1402 Extended = (uint64_t) (uint32_t)Src.Int32Val;
1403 else
1404 Extended = (uint64_t) Src.Int64Val;
1405
1406 // Now that we have a sign extended value, assign it to the destination
1407 INTEGER_ASSIGN(Dest, DBitWidth, Extended);
1408 return Dest;
1409}
1410
1411GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
1412 ExecutionContext &SF) {
1413 const Type *SrcTy = SrcVal->getType();
1414 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1415 assert(SrcTy == Type::DoubleTy && DstTy == Type::FloatTy &&
1416 "Invalid FPTrunc instruction");
1417 Dest.FloatVal = (float) Src.DoubleVal;
1418 return Dest;
1419}
1420
1421GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
1422 ExecutionContext &SF) {
1423 const Type *SrcTy = SrcVal->getType();
1424 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1425 assert(SrcTy == Type::FloatTy && DstTy == Type::DoubleTy &&
1426 "Invalid FPTrunc instruction");
1427 Dest.DoubleVal = (double) Src.FloatVal;
1428 return Dest;
1429}
1430
1431GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
1432 ExecutionContext &SF) {
1433 const Type *SrcTy = SrcVal->getType();
1434 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1435 const IntegerType *DITy = cast<IntegerType>(DstTy);
1436 unsigned DBitWidth = DITy->getBitWidth();
1437 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1438 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
1439 uint64_t Converted = 0;
1440 if (SrcTy->getTypeID() == Type::FloatTyID)
1441 Converted = (uint64_t) Src.FloatVal;
1442 else
1443 Converted = (uint64_t) Src.DoubleVal;
1444
1445 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1446 return Dest;
1447}
1448
1449GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1450 ExecutionContext &SF) {
1451 const Type *SrcTy = SrcVal->getType();
1452 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1453 const IntegerType *DITy = cast<IntegerType>(DstTy);
1454 unsigned DBitWidth = DITy->getBitWidth();
1455 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1456 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1457 int64_t Converted = 0;
1458 if (SrcTy->getTypeID() == Type::FloatTyID)
1459 Converted = (int64_t) Src.FloatVal;
1460 else
1461 Converted = (int64_t) Src.DoubleVal;
1462
1463 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1464 return Dest;
1465}
1466
1467GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1468 ExecutionContext &SF) {
1469 const Type *SrcTy = SrcVal->getType();
1470 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1471 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1472 unsigned SBitWidth = SITy->getBitWidth();
1473 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1474 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1475 uint64_t Converted = 0;
1476 if (SBitWidth == 1)
1477 Converted = (uint64_t) Src.Int1Val;
1478 else if (SBitWidth <= 8)
1479 Converted = (uint64_t) Src.Int8Val;
1480 else if (SBitWidth <= 16)
1481 Converted = (uint64_t) Src.Int16Val;
1482 else if (SBitWidth <= 32)
1483 Converted = (uint64_t) Src.Int32Val;
1484 else
1485 Converted = (uint64_t) Src.Int64Val;
1486
1487 if (DstTy->getTypeID() == Type::FloatTyID)
1488 Dest.FloatVal = (float) Converted;
1489 else
1490 Dest.DoubleVal = (double) Converted;
1491 return Dest;
1492}
1493
1494GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1495 ExecutionContext &SF) {
1496 const Type *SrcTy = SrcVal->getType();
1497 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1498 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1499 unsigned SBitWidth = SITy->getBitWidth();
1500 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
Reid Spencer24d6da52007-01-21 00:29:26 +00001501 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001502 int64_t Converted = 0;
1503 if (SBitWidth == 1)
1504 Converted = 0LL - Src.Int1Val;
1505 else if (SBitWidth <= 8)
1506 Converted = (int64_t) (int8_t)Src.Int8Val;
1507 else if (SBitWidth <= 16)
1508 Converted = (int64_t) (int16_t)Src.Int16Val;
1509 else if (SBitWidth <= 32)
1510 Converted = (int64_t) (int32_t)Src.Int32Val;
1511 else
1512 Converted = (int64_t) Src.Int64Val;
1513
1514 if (DstTy->getTypeID() == Type::FloatTyID)
1515 Dest.FloatVal = (float) Converted;
1516 else
1517 Dest.DoubleVal = (double) Converted;
1518 return Dest;
1519}
1520
1521GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1522 ExecutionContext &SF) {
1523 const Type *SrcTy = SrcVal->getType();
1524 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1525 const IntegerType *DITy = cast<IntegerType>(DstTy);
1526 unsigned DBitWidth = DITy->getBitWidth();
1527 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1528 assert(isa<PointerType>(SrcTy) && "Invalid PtrToInt instruction");
1529 INTEGER_ASSIGN(Dest, DBitWidth, (intptr_t) Src.PointerVal);
1530 return Dest;
1531}
1532
1533GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1534 ExecutionContext &SF) {
1535 const Type *SrcTy = SrcVal->getType();
1536 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1537 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1538 unsigned SBitWidth = SITy->getBitWidth();
1539 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1540 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1541 uint64_t Converted = 0;
1542 if (SBitWidth == 1)
1543 Converted = (uint64_t) Src.Int1Val;
1544 else if (SBitWidth <= 8)
1545 Converted = (uint64_t) Src.Int8Val;
1546 else if (SBitWidth <= 16)
1547 Converted = (uint64_t) Src.Int16Val;
1548 else if (SBitWidth <= 32)
1549 Converted = (uint64_t) Src.Int32Val;
1550 else
1551 Converted = (uint64_t) Src.Int64Val;
1552
1553 Dest.PointerVal = (PointerTy) Converted;
1554 return Dest;
1555}
1556
1557GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1558 ExecutionContext &SF) {
1559
1560 const Type *SrcTy = SrcVal->getType();
1561 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1562 if (isa<PointerType>(DstTy)) {
1563 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1564 Dest.PointerVal = Src.PointerVal;
Chris Lattner42a75512007-01-15 02:27:26 +00001565 } else if (DstTy->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001566 const IntegerType *DITy = cast<IntegerType>(DstTy);
1567 unsigned DBitWidth = DITy->getBitWidth();
1568 if (SrcTy == Type::FloatTy) {
1569 Dest.Int32Val = FloatToBits(Src.FloatVal);
1570 } else if (SrcTy == Type::DoubleTy) {
1571 Dest.Int64Val = DoubleToBits(Src.DoubleVal);
Chris Lattner42a75512007-01-15 02:27:26 +00001572 } else if (SrcTy->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001573 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1574 unsigned SBitWidth = SITy->getBitWidth();
1575 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1576 assert(SBitWidth == DBitWidth && "Invalid BitCast");
1577 if (SBitWidth == 1)
1578 Dest.Int1Val = Src.Int1Val;
1579 else if (SBitWidth <= 8)
1580 Dest.Int8Val = Src.Int8Val;
1581 else if (SBitWidth <= 16)
1582 Dest.Int16Val = Src.Int16Val;
1583 else if (SBitWidth <= 32)
1584 Dest.Int32Val = Src.Int32Val;
1585 else
1586 Dest.Int64Val = Src.Int64Val;
Reid Spencer65367b22007-01-18 02:12:51 +00001587 maskToBitWidth(Dest, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001588 } else
1589 assert(0 && "Invalid BitCast");
1590 } else if (DstTy == Type::FloatTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001591 if (SrcTy->isInteger())
Reid Spencera54b7cb2007-01-12 07:05:14 +00001592 Dest.FloatVal = BitsToFloat(Src.Int32Val);
1593 else
1594 Dest.FloatVal = Src.FloatVal;
1595 } else if (DstTy == Type::DoubleTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001596 if (SrcTy->isInteger())
Reid Spencera54b7cb2007-01-12 07:05:14 +00001597 Dest.DoubleVal = BitsToDouble(Src.Int64Val);
1598 else
1599 Dest.DoubleVal = Src.DoubleVal;
1600 } else
1601 assert(0 && "Invalid Bitcast");
1602
1603 return Dest;
1604}
1605
1606void Interpreter::visitTruncInst(TruncInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001607 ExecutionContext &SF = ECStack.back();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001608 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1609}
1610
1611void Interpreter::visitSExtInst(SExtInst &I) {
1612 ExecutionContext &SF = ECStack.back();
1613 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1614}
1615
1616void Interpreter::visitZExtInst(ZExtInst &I) {
1617 ExecutionContext &SF = ECStack.back();
1618 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1619}
1620
1621void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1622 ExecutionContext &SF = ECStack.back();
1623 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1624}
1625
1626void Interpreter::visitFPExtInst(FPExtInst &I) {
1627 ExecutionContext &SF = ECStack.back();
1628 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1629}
1630
1631void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1632 ExecutionContext &SF = ECStack.back();
1633 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1634}
1635
1636void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1637 ExecutionContext &SF = ECStack.back();
1638 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1639}
1640
1641void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1642 ExecutionContext &SF = ECStack.back();
1643 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1644}
1645
1646void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1647 ExecutionContext &SF = ECStack.back();
1648 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1649}
1650
1651void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1652 ExecutionContext &SF = ECStack.back();
1653 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1654}
1655
1656void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1657 ExecutionContext &SF = ECStack.back();
1658 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1659}
1660
1661void Interpreter::visitBitCastInst(BitCastInst &I) {
1662 ExecutionContext &SF = ECStack.back();
1663 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001664}
Chris Lattner92101ac2001-08-23 17:05:04 +00001665
Brian Gaekec1a2be12003-11-07 21:20:47 +00001666#define IMPLEMENT_VAARG(TY) \
1667 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1668
1669void Interpreter::visitVAArgInst(VAArgInst &I) {
1670 ExecutionContext &SF = ECStack.back();
1671
Brian Gaeke9d20b712004-02-25 23:01:48 +00001672 // Get the incoming valist parameter. LLI treats the valist as a
1673 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001674 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001675 GenericValue Dest;
1676 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001677 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001678 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001679 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001680 case Type::IntegerTyID: {
1681 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
1682 if (BitWidth == 1)
1683 Dest.Int1Val = Src.Int1Val;
1684 else if (BitWidth <= 8)
1685 Dest.Int8Val = Src.Int8Val;
1686 else if (BitWidth <= 16)
1687 Dest.Int16Val = Src.Int16Val;
1688 else if (BitWidth <= 32)
1689 Dest.Int32Val = Src.Int32Val;
1690 else if (BitWidth <= 64)
1691 Dest.Int64Val = Src.Int64Val;
1692 else
1693 assert("Integer types > 64 bits not supported");
Reid Spencer65367b22007-01-18 02:12:51 +00001694 maskToBitWidth(Dest, BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001695 }
Brian Gaekec1a2be12003-11-07 21:20:47 +00001696 IMPLEMENT_VAARG(Pointer);
1697 IMPLEMENT_VAARG(Float);
1698 IMPLEMENT_VAARG(Double);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001699 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001700 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Brian Gaekec1a2be12003-11-07 21:20:47 +00001701 abort();
1702 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001703
Brian Gaekec1a2be12003-11-07 21:20:47 +00001704 // Set the Value of this Instruction.
1705 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001706
1707 // Move the pointer to the next vararg.
1708 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001709}
1710
Chris Lattner92101ac2001-08-23 17:05:04 +00001711//===----------------------------------------------------------------------===//
1712// Dispatch and Execution Code
1713//===----------------------------------------------------------------------===//
1714
Chris Lattner92101ac2001-08-23 17:05:04 +00001715//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001716// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001717//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001718void Interpreter::callFunction(Function *F,
1719 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001720 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1721 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1722 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001723 // Make a new stack frame... and fill it in.
1724 ECStack.push_back(ExecutionContext());
1725 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001726 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001727
1728 // Special handling for external functions.
1729 if (F->isExternal()) {
1730 GenericValue Result = callExternalFunction (F, ArgVals);
1731 // Simulate a 'ret' instruction of the appropriate type.
1732 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1733 return;
1734 }
1735
1736 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001737 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001738 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001739
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001740 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001741 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001742 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001743 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001744
1745 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001746 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001747 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001748 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001749
1750 // Handle varargs arguments...
1751 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001752}
1753
Chris Lattner92101ac2001-08-23 17:05:04 +00001754void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001755 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001756 // Interpret a single instruction & increment the "PC".
1757 ExecutionContext &SF = ECStack.back(); // Current stack frame
1758 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001759
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001760 // Track the number of dynamic instructions executed.
1761 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001762
Bill Wendling480f0932006-11-27 23:54:50 +00001763 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001764 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001765 }
1766}