blob: 61624f3dc99ad33aa88827a58f42942a8ead586b [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
Brian Gaeke63438cc2003-12-11 00:22:59 +000070GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
71 ExecutionContext &SF) {
72 switch (CE->getOpcode()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +000073 case Instruction::Trunc:
74 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000075 case Instruction::ZExt:
Reid Spencera54b7cb2007-01-12 07:05:14 +000076 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000077 case Instruction::SExt:
Reid Spencera54b7cb2007-01-12 07:05:14 +000078 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000079 case Instruction::FPTrunc:
Reid Spencera54b7cb2007-01-12 07:05:14 +000080 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000081 case Instruction::FPExt:
Reid Spencera54b7cb2007-01-12 07:05:14 +000082 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000083 case Instruction::UIToFP:
Reid Spencera54b7cb2007-01-12 07:05:14 +000084 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000085 case Instruction::SIToFP:
Reid Spencera54b7cb2007-01-12 07:05:14 +000086 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000087 case Instruction::FPToUI:
Reid Spencera54b7cb2007-01-12 07:05:14 +000088 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000089 case Instruction::FPToSI:
Reid Spencera54b7cb2007-01-12 07:05:14 +000090 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000091 case Instruction::PtrToInt:
Reid Spencera54b7cb2007-01-12 07:05:14 +000092 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000093 case Instruction::IntToPtr:
Reid Spencera54b7cb2007-01-12 07:05:14 +000094 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
Reid Spencer3da59db2006-11-27 01:05:10 +000095 case Instruction::BitCast:
Reid Spencera54b7cb2007-01-12 07:05:14 +000096 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
Brian Gaeke63438cc2003-12-11 00:22:59 +000097 case Instruction::GetElementPtr:
98 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
99 gep_type_end(CE), SF);
100 case Instruction::Add:
101 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
102 getOperandValue(CE->getOperand(1), SF),
103 CE->getOperand(0)->getType());
104 case Instruction::Sub:
105 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
106 getOperandValue(CE->getOperand(1), SF),
107 CE->getOperand(0)->getType());
108 case Instruction::Mul:
109 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
110 getOperandValue(CE->getOperand(1), SF),
111 CE->getOperand(0)->getType());
Reid Spencer1628cec2006-10-26 06:15:43 +0000112 case Instruction::SDiv:
113 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
114 getOperandValue(CE->getOperand(1), SF),
115 CE->getOperand(0)->getType());
116 case Instruction::UDiv:
117 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
118 getOperandValue(CE->getOperand(1), SF),
119 CE->getOperand(0)->getType());
120 case Instruction::FDiv:
121 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
122 getOperandValue(CE->getOperand(1), SF),
123 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000124 case Instruction::URem:
125 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
Brian Gaeke63438cc2003-12-11 00:22:59 +0000126 getOperandValue(CE->getOperand(1), SF),
127 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000128 case Instruction::SRem:
129 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
130 getOperandValue(CE->getOperand(1), SF),
131 CE->getOperand(0)->getType());
132 case Instruction::FRem:
133 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
134 getOperandValue(CE->getOperand(1), SF),
135 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000136 case Instruction::And:
137 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
138 getOperandValue(CE->getOperand(1), SF),
139 CE->getOperand(0)->getType());
140 case Instruction::Or:
141 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
142 getOperandValue(CE->getOperand(1), SF),
143 CE->getOperand(0)->getType());
144 case Instruction::Xor:
145 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
146 getOperandValue(CE->getOperand(1), SF),
147 CE->getOperand(0)->getType());
Reid Spencere4d87aa2006-12-23 06:05:41 +0000148 case Instruction::FCmp:
149 case Instruction::ICmp:
150 return executeCmpInst(CE->getPredicate(),
151 getOperandValue(CE->getOperand(0), SF),
152 getOperandValue(CE->getOperand(1), SF),
153 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000154 case Instruction::Shl:
155 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
156 getOperandValue(CE->getOperand(1), SF),
157 CE->getOperand(0)->getType());
Reid Spencer3822ff52006-11-08 06:47:33 +0000158 case Instruction::LShr:
159 return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
160 getOperandValue(CE->getOperand(1), SF),
161 CE->getOperand(0)->getType());
162 case Instruction::AShr:
163 return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
164 getOperandValue(CE->getOperand(1), SF),
165 CE->getOperand(0)->getType());
Chris Lattner759d34f2004-04-20 16:43:21 +0000166 case Instruction::Select:
167 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
168 getOperandValue(CE->getOperand(1), SF),
169 getOperandValue(CE->getOperand(2), SF));
Brian Gaeke63438cc2003-12-11 00:22:59 +0000170 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000171 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Brian Gaeke63438cc2003-12-11 00:22:59 +0000172 abort();
173 return GenericValue();
174 }
175}
Chris Lattnera34c5682002-08-27 22:33:45 +0000176
Brian Gaeke29794cb2003-09-05 18:55:03 +0000177GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000178 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000179 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000180 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000181 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000182 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000183 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000184 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000185 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000186 }
187}
188
Chris Lattner39bb5b42001-10-15 13:25:40 +0000189static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000190 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000191}
192
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000193void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000194 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000195}
196
Chris Lattner2adcd832002-05-03 19:52:30 +0000197//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000198// Binary Instruction Implementations
199//===----------------------------------------------------------------------===//
200
201#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
202 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
203
Reid Spencera54b7cb2007-01-12 07:05:14 +0000204#define IMPLEMENT_INTEGER_BINOP(OP, TY) \
205 case Type::IntegerTyID: { \
206 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
207 if (BitWidth == 1) \
208 Dest.Int1Val = Src1.Int1Val OP Src2.Int1Val; \
209 else if (BitWidth <= 8) \
210 Dest.Int8Val = Src1.Int8Val OP Src2.Int8Val; \
211 else if (BitWidth <= 16) \
212 Dest.Int16Val = Src1.Int16Val OP Src2.Int16Val; \
213 else if (BitWidth <= 32) \
214 Dest.Int32Val = Src1.Int32Val OP Src2.Int32Val; \
215 else if (BitWidth <= 64) \
216 Dest.Int64Val = Src1.Int64Val OP Src2.Int64Val; \
217 else \
218 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
Reid Spencer65367b22007-01-18 02:12:51 +0000219 maskToBitWidth(Dest, BitWidth); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000220 break; \
221 }
222
223#define IMPLEMENT_SIGNED_BINOP(OP, TY) \
224 if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
225 unsigned BitWidth = ITy->getBitWidth(); \
226 if (BitWidth <= 8) \
227 Dest.Int8Val = ((int8_t)Src1.Int8Val) OP ((int8_t)Src2.Int8Val); \
228 else if (BitWidth <= 16) \
229 Dest.Int16Val = ((int16_t)Src1.Int16Val) OP ((int16_t)Src2.Int16Val); \
230 else if (BitWidth <= 32) \
231 Dest.Int32Val = ((int32_t)Src1.Int32Val) OP ((int32_t)Src2.Int32Val); \
232 else if (BitWidth <= 64) \
233 Dest.Int64Val = ((int64_t)Src1.Int64Val) OP ((int64_t)Src2.Int64Val); \
234 else { \
235 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
236 abort(); \
237 } \
Reid Spencer65367b22007-01-18 02:12:51 +0000238 maskToBitWidth(Dest, BitWidth); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000239 } else { \
240 cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
241 abort(); \
242 }
243
244#define IMPLEMENT_UNSIGNED_BINOP(OP, TY) \
245 if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
246 unsigned BitWidth = ITy->getBitWidth(); \
247 if (BitWidth <= 8) \
248 Dest.Int8Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
249 else if (BitWidth <= 16) \
250 Dest.Int16Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
251 else if (BitWidth <= 32) \
252 Dest.Int32Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
253 else if (BitWidth <= 64) \
254 Dest.Int64Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
255 else { \
256 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
257 abort(); \
258 } \
Reid Spencer65367b22007-01-18 02:12:51 +0000259 maskToBitWidth(Dest, BitWidth); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000260 } else { \
261 cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
262 abort(); \
263 }
264
Misha Brukmand1c881a2005-04-21 22:43:08 +0000265static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
266 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000267 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000268 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000269 IMPLEMENT_INTEGER_BINOP(+, Ty);
Chris Lattner92101ac2001-08-23 17:05:04 +0000270 IMPLEMENT_BINARY_OPERATOR(+, Float);
271 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000272 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000273 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000274 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000275 }
276 return Dest;
277}
278
Misha Brukmand1c881a2005-04-21 22:43:08 +0000279static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
280 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000281 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000282 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000283 IMPLEMENT_INTEGER_BINOP(-, Ty);
Chris Lattner92101ac2001-08-23 17:05:04 +0000284 IMPLEMENT_BINARY_OPERATOR(-, Float);
285 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000286 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000287 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000288 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000289 }
290 return Dest;
291}
292
Misha Brukmand1c881a2005-04-21 22:43:08 +0000293static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
294 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000295 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000296 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000297 IMPLEMENT_INTEGER_BINOP(*, Ty);
Chris Lattnerc2593162001-10-27 08:28:11 +0000298 IMPLEMENT_BINARY_OPERATOR(*, Float);
299 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000300 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000301 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000302 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000303 }
304 return Dest;
305}
306
Reid Spencer1628cec2006-10-26 06:15:43 +0000307static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
308 const Type *Ty) {
309 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000310 IMPLEMENT_UNSIGNED_BINOP(/,Ty)
Reid Spencer1628cec2006-10-26 06:15:43 +0000311 return Dest;
312}
313
314static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
315 const Type *Ty) {
316 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000317 IMPLEMENT_SIGNED_BINOP(/,Ty)
Reid Spencer1628cec2006-10-26 06:15:43 +0000318 return Dest;
319}
320
321static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000322 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000323 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000324 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000325 IMPLEMENT_BINARY_OPERATOR(/, Float);
326 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000327 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000328 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000329 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000330 }
331 return Dest;
332}
333
Reid Spencer0a783f72006-11-02 01:53:59 +0000334static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000335 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000336 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000337 IMPLEMENT_UNSIGNED_BINOP(%, Ty)
Reid Spencer0a783f72006-11-02 01:53:59 +0000338 return Dest;
339}
340
341static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
342 const Type *Ty) {
343 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000344 IMPLEMENT_SIGNED_BINOP(%, Ty)
Reid Spencer0a783f72006-11-02 01:53:59 +0000345 return Dest;
346}
347
348static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
349 const Type *Ty) {
350 GenericValue Dest;
351 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000352 case Type::FloatTyID:
353 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
354 break;
355 case Type::DoubleTyID:
356 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
357 break;
358 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000359 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000360 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000361 }
362 return Dest;
363}
364
Misha Brukmand1c881a2005-04-21 22:43:08 +0000365static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
366 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000367 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000368 IMPLEMENT_UNSIGNED_BINOP(&, Ty)
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000369 return Dest;
370}
371
Misha Brukmand1c881a2005-04-21 22:43:08 +0000372static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000373 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000374 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000375 IMPLEMENT_UNSIGNED_BINOP(|, Ty)
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000376 return Dest;
377}
378
Misha Brukmand1c881a2005-04-21 22:43:08 +0000379static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000380 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000381 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000382 IMPLEMENT_UNSIGNED_BINOP(^, Ty)
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000383 return Dest;
384}
385
Reid Spencera54b7cb2007-01-12 07:05:14 +0000386#define IMPLEMENT_SIGNED_ICMP(OP, TY) \
387 case Type::IntegerTyID: { \
388 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
389 if (BitWidth == 1) \
390 Dest.Int1Val = ((int8_t)Src1.Int1Val) OP ((int8_t)Src2.Int1Val); \
391 else if (BitWidth <= 8) \
392 Dest.Int1Val = ((int8_t)Src1.Int8Val) OP ((int8_t)Src2.Int8Val); \
393 else if (BitWidth <= 16) \
394 Dest.Int1Val = ((int16_t)Src1.Int16Val) OP ((int16_t)Src2.Int16Val); \
395 else if (BitWidth <= 32) \
396 Dest.Int1Val = ((int32_t)Src1.Int32Val) OP ((int32_t)Src2.Int32Val); \
397 else if (BitWidth <= 64) \
398 Dest.Int1Val = ((int64_t)Src1.Int64Val) OP ((int64_t)Src2.Int64Val); \
399 else { \
400 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
401 abort(); \
402 } \
Reid Spencer65367b22007-01-18 02:12:51 +0000403 maskToBitWidth(Dest, BitWidth); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000404 break; \
405 }
406
407#define IMPLEMENT_UNSIGNED_ICMP(OP, TY) \
408 case Type::IntegerTyID: { \
409 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
410 if (BitWidth == 1) \
411 Dest.Int1Val = ((uint8_t)Src1.Int1Val) OP ((uint8_t)Src2.Int1Val); \
412 else if (BitWidth <= 8) \
413 Dest.Int1Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
414 else if (BitWidth <= 16) \
415 Dest.Int1Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
416 else if (BitWidth <= 32) \
417 Dest.Int1Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
418 else if (BitWidth <= 64) \
419 Dest.Int1Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
420 else { \
421 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
422 abort(); \
423 } \
Reid Spencer65367b22007-01-18 02:12:51 +0000424 maskToBitWidth(Dest, BitWidth); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000425 break; \
426 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000427
Chris Lattnerfd506f52003-04-23 19:55:35 +0000428// Handle pointers specially because they must be compared with only as much
429// width as the host has. We _do not_ want to be comparing 64 bit values when
430// running on a 32-bit target, otherwise the upper 32 bits might mess up
431// comparisons if they contain garbage.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000432#define IMPLEMENT_POINTER_ICMP(OP) \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000433 case Type::PointerTyID: \
Reid Spencer4fe16d62007-01-11 18:21:29 +0000434 Dest.Int1Val = (void*)(intptr_t)Src1.PointerVal OP \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000435 (void*)(intptr_t)Src2.PointerVal; break
436
Reid Spencere4d87aa2006-12-23 06:05:41 +0000437static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
438 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000439 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000440 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000441 IMPLEMENT_UNSIGNED_ICMP(==, Ty);
442 IMPLEMENT_POINTER_ICMP(==);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000443 default:
444 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
445 abort();
446 }
447 return Dest;
448}
449
450static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
451 const Type *Ty) {
452 GenericValue Dest;
453 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_NE predicate: " << *Ty << "\n";
458 abort();
459 }
460 return Dest;
461}
462
463static GenericValue executeICMP_ULT(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_ULT predicate: " << *Ty << "\n";
471 abort();
472 }
473 return Dest;
474}
475
476static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
477 const Type *Ty) {
478 GenericValue Dest;
479 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000480 IMPLEMENT_SIGNED_ICMP(<, Ty);
481 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000482 default:
483 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
484 abort();
485 }
486 return Dest;
487}
488
489static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
490 const Type *Ty) {
491 GenericValue Dest;
492 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000493 IMPLEMENT_UNSIGNED_ICMP(>, Ty);
494 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000495 default:
496 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
497 abort();
498 }
499 return Dest;
500}
501
502static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
503 const Type *Ty) {
504 GenericValue Dest;
505 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000506 IMPLEMENT_SIGNED_ICMP(>, Ty);
507 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000508 default:
509 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
510 abort();
511 }
512 return Dest;
513}
514
515static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
516 const Type *Ty) {
517 GenericValue Dest;
518 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000519 IMPLEMENT_UNSIGNED_ICMP(<=, Ty);
520 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000521 default:
522 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
523 abort();
524 }
525 return Dest;
526}
527
528static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
529 const Type *Ty) {
530 GenericValue Dest;
531 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000532 IMPLEMENT_SIGNED_ICMP(<=, Ty);
533 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000534 default:
535 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
536 abort();
537 }
538 return Dest;
539}
540
541static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
542 const Type *Ty) {
543 GenericValue Dest;
544 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000545 IMPLEMENT_UNSIGNED_ICMP(>=,Ty);
546 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000547 default:
548 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
549 abort();
550 }
551 return Dest;
552}
553
554static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
555 const Type *Ty) {
556 GenericValue Dest;
557 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000558 IMPLEMENT_SIGNED_ICMP(>=, Ty);
559 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000560 default:
561 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
562 abort();
563 }
564 return Dest;
565}
566
Reid Spencere49661b2006-12-31 05:51:36 +0000567void Interpreter::visitICmpInst(ICmpInst &I) {
568 ExecutionContext &SF = ECStack.back();
569 const Type *Ty = I.getOperand(0)->getType();
570 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
571 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
572 GenericValue R; // Result
573
574 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000575 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
576 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
Reid Spencere49661b2006-12-31 05:51:36 +0000577 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
578 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
579 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
580 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
581 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
582 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
583 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
584 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
585 default:
586 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
587 abort();
588 }
589
590 SetValue(&I, R, SF);
591}
592
593#define IMPLEMENT_FCMP(OP, TY) \
Reid Spencer4fe16d62007-01-11 18:21:29 +0000594 case Type::TY##TyID: Dest.Int1Val = Src1.TY##Val OP Src2.TY##Val; break
Reid Spencere4d87aa2006-12-23 06:05:41 +0000595
Reid Spencera54b7cb2007-01-12 07:05:14 +0000596static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000597 const Type *Ty) {
598 GenericValue Dest;
599 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000600 IMPLEMENT_FCMP(==, Float);
601 IMPLEMENT_FCMP(==, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000602 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000603 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000604 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000605 }
606 return Dest;
607}
608
Reid Spencera54b7cb2007-01-12 07:05:14 +0000609static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000610 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000611 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000612 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000613 IMPLEMENT_FCMP(!=, Float);
614 IMPLEMENT_FCMP(!=, Double);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000615
Chris Lattner92101ac2001-08-23 17:05:04 +0000616 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000617 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000618 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000619 }
620 return Dest;
621}
622
Reid Spencera54b7cb2007-01-12 07:05:14 +0000623static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000624 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000625 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000626 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000627 IMPLEMENT_FCMP(<=, Float);
628 IMPLEMENT_FCMP(<=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000629 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000630 cerr << "Unhandled type for FCmp LE 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_OGE(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 GE 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_OLT(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 LT 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_OGT(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +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 GT 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 +0000675#define IMPLEMENT_UNORDERED(TY, X,Y) \
676 if (TY == Type::FloatTy) \
677 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
678 Dest.Int1Val = true; \
679 return Dest; \
680 } \
681 else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
682 Dest.Int1Val = true; \
683 return Dest; \
684 }
685
686
687static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
688 const Type *Ty) {
689 GenericValue Dest;
690 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
691 return executeFCMP_OEQ(Src1, Src2, Ty);
692}
693
694static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
695 const Type *Ty) {
696 GenericValue Dest;
697 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
698 return executeFCMP_ONE(Src1, Src2, Ty);
699}
700
701static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
702 const Type *Ty) {
703 GenericValue Dest;
704 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
705 return executeFCMP_OLE(Src1, Src2, Ty);
706}
707
708static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
709 const Type *Ty) {
710 GenericValue Dest;
711 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
712 return executeFCMP_OGE(Src1, Src2, Ty);
713}
714
715static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
716 const Type *Ty) {
717 GenericValue Dest;
718 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
719 return executeFCMP_OLT(Src1, Src2, Ty);
720}
721
722static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
723 const Type *Ty) {
724 GenericValue Dest;
725 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
726 return executeFCMP_OGT(Src1, Src2, Ty);
727}
728
729static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
730 const Type *Ty) {
731 GenericValue Dest;
732 if (Ty == Type::FloatTy)
733 Dest.Int1Val = (Src1.FloatVal == Src1.FloatVal &&
734 Src2.FloatVal == Src2.FloatVal);
735 else
736 Dest.Int1Val = (Src1.DoubleVal == Src1.DoubleVal &&
737 Src2.DoubleVal == Src2.DoubleVal);
738 return Dest;
739}
740
741static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
742 const Type *Ty) {
743 GenericValue Dest;
744 if (Ty == Type::FloatTy)
745 Dest.Int1Val = (Src1.FloatVal != Src1.FloatVal ||
746 Src2.FloatVal != Src2.FloatVal);
747 else
748 Dest.Int1Val = (Src1.DoubleVal != Src1.DoubleVal ||
749 Src2.DoubleVal != Src2.DoubleVal);
750 return Dest;
751}
752
Reid Spencere4d87aa2006-12-23 06:05:41 +0000753void Interpreter::visitFCmpInst(FCmpInst &I) {
754 ExecutionContext &SF = ECStack.back();
755 const Type *Ty = I.getOperand(0)->getType();
756 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
757 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
758 GenericValue R; // Result
759
760 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000761 case FCmpInst::FCMP_FALSE: R.Int1Val = false; break;
762 case FCmpInst::FCMP_TRUE: R.Int1Val = true; break;
763 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
764 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
765 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
766 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
767 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
768 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
769 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
770 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
771 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
772 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
773 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
774 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
775 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
776 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000777 default:
778 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
779 abort();
780 }
781
782 SetValue(&I, R, SF);
783}
784
Reid Spencere4d87aa2006-12-23 06:05:41 +0000785static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
786 GenericValue Src2, const Type *Ty) {
787 GenericValue Result;
788 switch (predicate) {
789 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
790 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
791 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
792 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
793 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
794 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
795 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
796 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
797 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
798 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000799 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
800 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
801 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
802 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
803 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
804 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
805 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
806 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
807 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
808 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
809 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
810 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
811 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
812 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000813 case FCmpInst::FCMP_FALSE: {
814 GenericValue Result;
Reid Spencer4fe16d62007-01-11 18:21:29 +0000815 Result.Int1Val = false;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000816 return Result;
817 }
818 case FCmpInst::FCMP_TRUE: {
819 GenericValue Result;
Reid Spencer4fe16d62007-01-11 18:21:29 +0000820 Result.Int1Val = true;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000821 return Result;
822 }
823 default:
824 cerr << "Unhandled Cmp predicate\n";
825 abort();
826 }
827}
828
Chris Lattnerd7916e92003-05-10 21:22:39 +0000829void Interpreter::visitBinaryOperator(BinaryOperator &I) {
830 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000831 const Type *Ty = I.getOperand(0)->getType();
832 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
833 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000834 GenericValue R; // Result
835
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000836 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000837 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
838 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
839 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000840 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
841 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
842 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
Reid Spencer0a783f72006-11-02 01:53:59 +0000843 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
844 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
845 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000846 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
847 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
848 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000849 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000850 cerr << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner02868352003-04-22 21:22:33 +0000851 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000852 }
853
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000854 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000855}
856
Misha Brukmand1c881a2005-04-21 22:43:08 +0000857static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000858 GenericValue Src3) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000859 return Src1.Int1Val ? Src2 : Src3;
Chris Lattner759d34f2004-04-20 16:43:21 +0000860}
861
862void Interpreter::visitSelectInst(SelectInst &I) {
863 ExecutionContext &SF = ECStack.back();
864 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
865 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
866 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
867 GenericValue R = executeSelectInst(Src1, Src2, Src3);
868 SetValue(&I, R, SF);
869}
870
871
Chris Lattner92101ac2001-08-23 17:05:04 +0000872//===----------------------------------------------------------------------===//
873// Terminator Instruction Implementations
874//===----------------------------------------------------------------------===//
875
Chris Lattnere43db882001-10-27 04:15:57 +0000876void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000877 // runAtExitHandlers() assumes there are no stack frames, but
878 // if exit() was called, then it had a stack frame. Blow away
879 // the stack before interpreting atexit handlers.
880 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000881 runAtExitHandlers ();
Reid Spencere49661b2006-12-31 05:51:36 +0000882 exit (GV.Int32Val);
Chris Lattnere43db882001-10-27 04:15:57 +0000883}
884
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000885/// Pop the last stack frame off of ECStack and then copy the result
886/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000887/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000888/// CallInst if there was a previous stack frame. This method may
889/// invalidate any ECStack iterators you have. This method also takes
890/// care of switching to the normal destination BB, if we are returning
891/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000892///
893void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
894 GenericValue Result) {
895 // Pop the current stack frame.
896 ECStack.pop_back();
897
Misha Brukmand1c881a2005-04-21 22:43:08 +0000898 if (ECStack.empty()) { // Finished main. Put result into exit code...
Chris Lattner42a75512007-01-15 02:27:26 +0000899 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000900 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000901 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000902 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000903 }
904 } else {
905 // If we have a previous stack frame, and we have a previous call,
906 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000907 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000908 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000909 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000910 SetValue(I, Result, CallingSF);
911 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
912 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000913 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000914 }
915 }
916}
917
Chris Lattnerd7916e92003-05-10 21:22:39 +0000918void Interpreter::visitReturnInst(ReturnInst &I) {
919 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000920 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000921 GenericValue Result;
922
923 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000924 if (I.getNumOperands()) {
925 RetTy = I.getReturnValue()->getType();
926 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000927 }
928
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000929 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000930}
931
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000932void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000933 // Unwind stack
934 Instruction *Inst;
935 do {
936 ECStack.pop_back ();
937 if (ECStack.empty ())
938 abort ();
939 Inst = ECStack.back ().Caller.getInstruction ();
940 } while (!(Inst && isa<InvokeInst> (Inst)));
941
942 // Return from invoke
943 ExecutionContext &InvokingSF = ECStack.back ();
944 InvokingSF.Caller = CallSite ();
945
946 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000947 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000948}
949
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000950void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000951 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000952 abort();
953}
954
Chris Lattnerd7916e92003-05-10 21:22:39 +0000955void Interpreter::visitBranchInst(BranchInst &I) {
956 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000957 BasicBlock *Dest;
958
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000959 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
960 if (!I.isUnconditional()) {
961 Value *Cond = I.getCondition();
Reid Spencer4fe16d62007-01-11 18:21:29 +0000962 if (getOperandValue(Cond, SF).Int1Val == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000963 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000964 }
Chris Lattner77113b62003-05-10 20:21:16 +0000965 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000966}
967
Chris Lattnerd7916e92003-05-10 21:22:39 +0000968void Interpreter::visitSwitchInst(SwitchInst &I) {
969 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000970 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
971 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000972
973 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000974 BasicBlock *Dest = 0;
975 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Reid Spencere4d87aa2006-12-23 06:05:41 +0000976 if (executeICMP_EQ(CondVal,
Reid Spencer4fe16d62007-01-11 18:21:29 +0000977 getOperandValue(I.getOperand(i), SF), ElTy).Int1Val) {
Chris Lattner09e93922003-04-22 20:34:47 +0000978 Dest = cast<BasicBlock>(I.getOperand(i+1));
979 break;
980 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000981
Chris Lattner09e93922003-04-22 20:34:47 +0000982 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000983 SwitchToNewBasicBlock(Dest, SF);
984}
985
986// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
987// This function handles the actual updating of block and instruction iterators
988// as well as execution of all of the PHI nodes in the destination block.
989//
990// This method does this because all of the PHI nodes must be executed
991// atomically, reading their inputs before any of the results are updated. Not
992// doing this can cause problems if the PHI nodes depend on other PHI nodes for
993// their inputs. If the input PHI node is updated before it is read, incorrect
994// results can happen. Thus we use a two phase approach.
995//
996void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
997 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
998 SF.CurBB = Dest; // Update CurBB to branch destination
999 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
1000
1001 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
1002
1003 // Loop over all of the PHI nodes in the current block, reading their inputs.
1004 std::vector<GenericValue> ResultValues;
1005
1006 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
1007 // Search for the value corresponding to this previous bb...
1008 int i = PN->getBasicBlockIndex(PrevBB);
1009 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
1010 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +00001011
Chris Lattner77113b62003-05-10 20:21:16 +00001012 // Save the incoming value for this PHI node...
1013 ResultValues.push_back(getOperandValue(IncomingValue, SF));
1014 }
1015
1016 // Now loop over all of the PHI nodes setting their values...
1017 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +00001018 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
1019 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +00001020 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +00001021 }
Chris Lattner09e93922003-04-22 20:34:47 +00001022}
1023
Chris Lattner92101ac2001-08-23 17:05:04 +00001024//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +00001025// Memory Instruction Implementations
1026//===----------------------------------------------------------------------===//
1027
Chris Lattnerd7916e92003-05-10 21:22:39 +00001028void Interpreter::visitAllocationInst(AllocationInst &I) {
1029 ExecutionContext &SF = ECStack.back();
1030
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001031 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +00001032
Chris Lattnercc82cc12002-04-28 21:57:33 +00001033 // Get the number of elements being allocated by the array...
Reid Spencere49661b2006-12-31 05:51:36 +00001034 unsigned NumElements = getOperandValue(I.getOperand(0), SF).Int32Val;
Chris Lattner86660982001-08-27 05:16:50 +00001035
1036 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +00001037 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +00001038
Chris Lattnerfe11a972002-12-23 23:59:41 +00001039 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001040 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001041 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001042
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001043 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +00001044 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +00001045}
1046
Chris Lattnerd7916e92003-05-10 21:22:39 +00001047void Interpreter::visitFreeInst(FreeInst &I) {
1048 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001049 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
1050 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +00001051 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +00001052 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +00001053}
1054
Chris Lattnera34c5682002-08-27 22:33:45 +00001055// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +00001056//
Chris Lattner4af6de82003-11-25 20:44:56 +00001057GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001058 gep_type_iterator E,
1059 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001060 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +00001061 "Cannot getElementOffset of a nonpointer type!");
1062
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001063 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +00001064
1065 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +00001066 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +00001067 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +00001068
Reid Spencerb83eb642006-10-20 07:07:24 +00001069 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
1070 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +00001071
Chris Lattnerd5644962005-01-08 20:05:34 +00001072 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +00001073 } else {
1074 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +00001075 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +00001076 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
1077
Reid Spencera54b7cb2007-01-12 07:05:14 +00001078 int64_t Idx;
1079 unsigned BitWidth =
1080 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
Reid Spencer23e28832007-01-18 01:25:42 +00001081 if (BitWidth == 32)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001082 Idx = (int64_t)(int32_t)IdxGV.Int32Val;
Reid Spencer23e28832007-01-18 01:25:42 +00001083 else if (BitWidth == 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001084 Idx = (int64_t)IdxGV.Int64Val;
1085 else
Reid Spencer23e28832007-01-18 01:25:42 +00001086 assert(0 && "Invalid index type for getelementptr");
Chris Lattnerd5644962005-01-08 20:05:34 +00001087 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001088 }
Chris Lattner95c3af52001-10-29 19:32:19 +00001089 }
1090
Chris Lattnera34c5682002-08-27 22:33:45 +00001091 GenericValue Result;
1092 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
1093 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +00001094}
1095
Chris Lattnerd7916e92003-05-10 21:22:39 +00001096void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1097 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +00001098 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +00001099 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +00001100}
1101
Chris Lattnerd7916e92003-05-10 21:22:39 +00001102void Interpreter::visitLoadInst(LoadInst &I) {
1103 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001104 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +00001105 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +00001106 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001107 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001108}
1109
Chris Lattnerd7916e92003-05-10 21:22:39 +00001110void Interpreter::visitStoreInst(StoreInst &I) {
1111 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +00001112 GenericValue Val = getOperandValue(I.getOperand(0), SF);
1113 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +00001114 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +00001115 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +00001116}
1117
Chris Lattner86660982001-08-27 05:16:50 +00001118//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +00001119// Miscellaneous Instruction Implementations
1120//===----------------------------------------------------------------------===//
1121
Brian Gaekefea483d2003-11-07 20:04:22 +00001122void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001123 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +00001124
1125 // Check to see if this is an intrinsic function call...
1126 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +00001127 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +00001128 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +00001129 case Intrinsic::not_intrinsic:
1130 break;
Chris Lattner317201d2004-03-13 00:24:00 +00001131 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +00001132 GenericValue ArgIndex;
1133 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1134 ArgIndex.UIntPairVal.second = 0;
1135 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +00001136 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +00001137 }
Chris Lattner317201d2004-03-13 00:24:00 +00001138 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +00001139 return;
Chris Lattner317201d2004-03-13 00:24:00 +00001140 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +00001141 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1142 return;
1143 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +00001144 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +00001145 // class to transform it into hopefully tasty LLVM code.
1146 //
1147 Instruction *Prev = CS.getInstruction()->getPrev();
1148 BasicBlock *Parent = CS.getInstruction()->getParent();
1149 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1150
1151 // Restore the CurInst pointer to the first instruction newly inserted, if
1152 // any.
1153 if (!Prev) {
1154 SF.CurInst = Parent->begin();
1155 } else {
1156 SF.CurInst = Prev;
1157 ++SF.CurInst;
1158 }
Brian Gaekeb440dea2004-04-23 18:05:28 +00001159 return;
Chris Lattner73011782003-12-28 09:44:37 +00001160 }
1161
Brian Gaekefea483d2003-11-07 20:04:22 +00001162 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +00001163 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +00001164 const unsigned NumArgs = SF.Caller.arg_size();
1165 ArgVals.reserve(NumArgs);
1166 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1167 e = SF.Caller.arg_end(); i != e; ++i) {
1168 Value *V = *i;
1169 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +00001170 // Promote all integral types whose size is < sizeof(int) into ints. We do
1171 // this by zero or sign extending the value as appropriate according to the
1172 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +00001173 const Type *Ty = V->getType();
Chris Lattner42a75512007-01-15 02:27:26 +00001174 if (Ty->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001175 if (Ty->getPrimitiveSizeInBits() == 1)
Reid Spencer4fe16d62007-01-11 18:21:29 +00001176 ArgVals.back().Int32Val = ArgVals.back().Int1Val;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001177 else if (Ty->getPrimitiveSizeInBits() <= 8)
1178 ArgVals.back().Int32Val = ArgVals.back().Int8Val;
1179 else if (Ty->getPrimitiveSizeInBits() <= 16)
1180 ArgVals.back().Int32Val = ArgVals.back().Int16Val;
Chris Lattner93780132003-01-13 00:58:52 +00001181 }
1182 }
Chris Lattner365a76e2001-09-10 04:49:44 +00001183
Misha Brukmand1c881a2005-04-21 22:43:08 +00001184 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001185 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +00001186 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +00001187 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +00001188}
1189
Brian Gaeke63438cc2003-12-11 00:22:59 +00001190static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
1191 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +00001192 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001193 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1194 unsigned BitWidth = ITy->getBitWidth();
Reid Spencer65367b22007-01-18 02:12:51 +00001195 if (BitWidth <= 8)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001196 Dest.Int8Val = ((uint8_t)Src1.Int8Val) << ((uint32_t)Src2.Int8Val);
Reid Spencer65367b22007-01-18 02:12:51 +00001197 else if (BitWidth <= 16)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001198 Dest.Int16Val = ((uint16_t)Src1.Int16Val) << ((uint32_t)Src2.Int8Val);
Reid Spencer65367b22007-01-18 02:12:51 +00001199 else if (BitWidth <= 32)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001200 Dest.Int32Val = ((uint32_t)Src1.Int32Val) << ((uint32_t)Src2.Int8Val);
Reid Spencer65367b22007-01-18 02:12:51 +00001201 else if (BitWidth <= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001202 Dest.Int64Val = ((uint64_t)Src1.Int64Val) << ((uint32_t)Src2.Int8Val);
Reid Spencer65367b22007-01-18 02:12:51 +00001203 else {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001204 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1205 abort();
1206 }
Reid Spencer65367b22007-01-18 02:12:51 +00001207 maskToBitWidth(Dest, BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001208 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001209 cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
Reid Spencera54b7cb2007-01-12 07:05:14 +00001210 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001211 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001212 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001213}
1214
Reid Spencer3822ff52006-11-08 06:47:33 +00001215static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
1216 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +00001217 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001218 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1219 unsigned BitWidth = ITy->getBitWidth();
1220 if (BitWidth <= 8)
1221 Dest.Int8Val = ((uint8_t)Src1.Int8Val) >> ((uint32_t)Src2.Int8Val);
1222 else if (BitWidth <= 16)
1223 Dest.Int16Val = ((uint16_t)Src1.Int16Val) >> ((uint32_t)Src2.Int8Val);
1224 else if (BitWidth <= 32)
1225 Dest.Int32Val = ((uint32_t)Src1.Int32Val) >> ((uint32_t)Src2.Int8Val);
1226 else if (BitWidth <= 64)
1227 Dest.Int64Val = ((uint64_t)Src1.Int64Val) >> ((uint32_t)Src2.Int8Val);
1228 else {
1229 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1230 abort();
1231 }
Reid Spencer65367b22007-01-18 02:12:51 +00001232 maskToBitWidth(Dest, BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001233 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001234 cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
Reid Spencer3822ff52006-11-08 06:47:33 +00001235 abort();
1236 }
1237 return Dest;
1238}
1239
1240static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
1241 const Type *Ty) {
1242 GenericValue Dest;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001243 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1244 unsigned BitWidth = ITy->getBitWidth();
1245 if (BitWidth <= 8)
1246 Dest.Int8Val = ((int8_t)Src1.Int8Val) >> ((int32_t)Src2.Int8Val);
1247 else if (BitWidth <= 16)
1248 Dest.Int16Val = ((int16_t)Src1.Int16Val) >> ((int32_t)Src2.Int8Val);
1249 else if (BitWidth <= 32)
1250 Dest.Int32Val = ((int32_t)Src1.Int32Val) >> ((int32_t)Src2.Int8Val);
1251 else if (BitWidth <= 64)
1252 Dest.Int64Val = ((int64_t)Src1.Int64Val) >> ((int32_t)Src2.Int8Val);
1253 else {
1254 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
1255 abort();
1256 }
Reid Spencer65367b22007-01-18 02:12:51 +00001257 maskToBitWidth(Dest, BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001258 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001259 cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +00001260 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001261 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001262 return Dest;
1263}
1264
1265void Interpreter::visitShl(ShiftInst &I) {
1266 ExecutionContext &SF = ECStack.back();
1267 const Type *Ty = I.getOperand(0)->getType();
1268 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1269 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1270 GenericValue Dest;
1271 Dest = executeShlInst (Src1, Src2, Ty);
1272 SetValue(&I, Dest, SF);
1273}
1274
Reid Spencer3822ff52006-11-08 06:47:33 +00001275void Interpreter::visitLShr(ShiftInst &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001276 ExecutionContext &SF = ECStack.back();
1277 const Type *Ty = I.getOperand(0)->getType();
1278 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1279 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1280 GenericValue Dest;
Reid Spencer3822ff52006-11-08 06:47:33 +00001281 Dest = executeLShrInst (Src1, Src2, Ty);
1282 SetValue(&I, Dest, SF);
1283}
1284
1285void Interpreter::visitAShr(ShiftInst &I) {
1286 ExecutionContext &SF = ECStack.back();
1287 const Type *Ty = I.getOperand(0)->getType();
1288 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1289 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1290 GenericValue Dest;
1291 Dest = executeAShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001292 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001293}
1294
Reid Spencer23e28832007-01-18 01:25:42 +00001295#define INTEGER_ASSIGN(DEST, BITWIDTH, VAL) \
1296 { \
1297 uint64_t Mask = (1ull << BITWIDTH) - 1; \
1298 if (BITWIDTH == 1) { \
1299 Dest.Int1Val = (bool) (VAL & Mask); \
1300 } else if (BITWIDTH <= 8) { \
1301 Dest.Int8Val = (uint8_t) (VAL & Mask); \
1302 } else if (BITWIDTH <= 16) { \
1303 Dest.Int16Val = (uint16_t) (VAL & Mask); \
1304 } else if (BITWIDTH <= 32) { \
1305 Dest.Int32Val = (uint32_t) (VAL & Mask); \
1306 } else \
1307 Dest.Int64Val = (uint64_t) (VAL & Mask); \
1308 }
Chris Lattner86660982001-08-27 05:16:50 +00001309
Reid Spencera54b7cb2007-01-12 07:05:14 +00001310GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
1311 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001312 const Type *SrcTy = SrcVal->getType();
1313 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001314 const IntegerType *DITy = cast<IntegerType>(DstTy);
1315 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1316 unsigned DBitWidth = DITy->getBitWidth();
1317 unsigned SBitWidth = SITy->getBitWidth();
1318 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1319 "Integer types > 64 bits not supported");
1320 assert(SBitWidth > DBitWidth && "Invalid truncate");
Chris Lattner86660982001-08-27 05:16:50 +00001321
Reid Spencera54b7cb2007-01-12 07:05:14 +00001322 // Mask the source value to its actual bit width. This ensures that any
1323 // high order bits are cleared.
1324 uint64_t Mask = (1ULL << DBitWidth) - 1;
1325 uint64_t MaskedVal = 0;
1326 if (SBitWidth <= 8)
1327 MaskedVal = Src.Int8Val & Mask;
1328 else if (SBitWidth <= 16)
1329 MaskedVal = Src.Int16Val & Mask;
1330 else if (SBitWidth <= 32)
1331 MaskedVal = Src.Int32Val & Mask;
1332 else
1333 MaskedVal = Src.Int64Val & Mask;
Chris Lattnera34c5682002-08-27 22:33:45 +00001334
Reid Spencera54b7cb2007-01-12 07:05:14 +00001335 INTEGER_ASSIGN(Dest, DBitWidth, MaskedVal);
Chris Lattnera34c5682002-08-27 22:33:45 +00001336 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001337}
Chris Lattner92101ac2001-08-23 17:05:04 +00001338
Reid Spencera54b7cb2007-01-12 07:05:14 +00001339GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
1340 ExecutionContext &SF) {
1341 const Type *SrcTy = SrcVal->getType();
1342 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1343 const IntegerType *DITy = cast<IntegerType>(DstTy);
1344 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1345 unsigned DBitWidth = DITy->getBitWidth();
1346 unsigned SBitWidth = SITy->getBitWidth();
1347 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1348 "Integer types > 64 bits not supported");
1349 assert(SBitWidth < DBitWidth && "Invalid sign extend");
1350 int64_t Extended = 0;
1351 if (SBitWidth == 1)
1352 // For sign extension from bool, we must extend the source bits.
1353 Extended = 0 - (Src.Int1Val & 1);
1354 else if (SBitWidth <= 8)
1355 Extended = (int64_t) (int8_t)Src.Int8Val;
1356 else if (SBitWidth <= 16)
1357 Extended = (int64_t) (int16_t)Src.Int16Val;
1358 else if (SBitWidth <= 32)
1359 Extended = (int64_t) (int32_t)Src.Int32Val;
1360 else
1361 Extended = (int64_t) Src.Int64Val;
1362
1363 // Now that we have a sign extended value, assign it to the destination
1364 INTEGER_ASSIGN(Dest, DBitWidth, Extended);
1365 return Dest;
1366}
1367
1368GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
1369 ExecutionContext &SF) {
1370 const Type *SrcTy = SrcVal->getType();
1371 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1372 const IntegerType *DITy = cast<IntegerType>(DstTy);
1373 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1374 unsigned DBitWidth = DITy->getBitWidth();
1375 unsigned SBitWidth = SITy->getBitWidth();
1376 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1377 "Integer types > 64 bits not supported");
1378 assert(SBitWidth < DBitWidth && "Invalid sign extend");
1379 uint64_t Extended = 0;
1380 if (SBitWidth == 1)
1381 // For sign extension from bool, we must extend the source bits.
1382 Extended = (uint64_t) (Src.Int1Val & 1);
1383 else if (SBitWidth <= 8)
1384 Extended = (uint64_t) (uint8_t)Src.Int8Val;
1385 else if (SBitWidth <= 16)
1386 Extended = (uint64_t) (uint16_t)Src.Int16Val;
1387 else if (SBitWidth <= 32)
1388 Extended = (uint64_t) (uint32_t)Src.Int32Val;
1389 else
1390 Extended = (uint64_t) Src.Int64Val;
1391
1392 // Now that we have a sign extended value, assign it to the destination
1393 INTEGER_ASSIGN(Dest, DBitWidth, Extended);
1394 return Dest;
1395}
1396
1397GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
1398 ExecutionContext &SF) {
1399 const Type *SrcTy = SrcVal->getType();
1400 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1401 assert(SrcTy == Type::DoubleTy && DstTy == Type::FloatTy &&
1402 "Invalid FPTrunc instruction");
1403 Dest.FloatVal = (float) Src.DoubleVal;
1404 return Dest;
1405}
1406
1407GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
1408 ExecutionContext &SF) {
1409 const Type *SrcTy = SrcVal->getType();
1410 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1411 assert(SrcTy == Type::FloatTy && DstTy == Type::DoubleTy &&
1412 "Invalid FPTrunc instruction");
1413 Dest.DoubleVal = (double) Src.FloatVal;
1414 return Dest;
1415}
1416
1417GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
1418 ExecutionContext &SF) {
1419 const Type *SrcTy = SrcVal->getType();
1420 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1421 const IntegerType *DITy = cast<IntegerType>(DstTy);
1422 unsigned DBitWidth = DITy->getBitWidth();
1423 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1424 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
1425 uint64_t Converted = 0;
1426 if (SrcTy->getTypeID() == Type::FloatTyID)
1427 Converted = (uint64_t) Src.FloatVal;
1428 else
1429 Converted = (uint64_t) Src.DoubleVal;
1430
1431 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1432 return Dest;
1433}
1434
1435GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1436 ExecutionContext &SF) {
1437 const Type *SrcTy = SrcVal->getType();
1438 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1439 const IntegerType *DITy = cast<IntegerType>(DstTy);
1440 unsigned DBitWidth = DITy->getBitWidth();
1441 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1442 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1443 int64_t Converted = 0;
1444 if (SrcTy->getTypeID() == Type::FloatTyID)
1445 Converted = (int64_t) Src.FloatVal;
1446 else
1447 Converted = (int64_t) Src.DoubleVal;
1448
1449 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1450 return Dest;
1451}
1452
1453GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1454 ExecutionContext &SF) {
1455 const Type *SrcTy = SrcVal->getType();
1456 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1457 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1458 unsigned SBitWidth = SITy->getBitWidth();
1459 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1460 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1461 uint64_t Converted = 0;
1462 if (SBitWidth == 1)
1463 Converted = (uint64_t) Src.Int1Val;
1464 else if (SBitWidth <= 8)
1465 Converted = (uint64_t) Src.Int8Val;
1466 else if (SBitWidth <= 16)
1467 Converted = (uint64_t) Src.Int16Val;
1468 else if (SBitWidth <= 32)
1469 Converted = (uint64_t) Src.Int32Val;
1470 else
1471 Converted = (uint64_t) Src.Int64Val;
1472
1473 if (DstTy->getTypeID() == Type::FloatTyID)
1474 Dest.FloatVal = (float) Converted;
1475 else
1476 Dest.DoubleVal = (double) Converted;
1477 return Dest;
1478}
1479
1480GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1481 ExecutionContext &SF) {
1482 const Type *SrcTy = SrcVal->getType();
1483 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1484 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1485 unsigned SBitWidth = SITy->getBitWidth();
1486 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1487 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1488 int64_t Converted = 0;
1489 if (SBitWidth == 1)
1490 Converted = 0LL - Src.Int1Val;
1491 else if (SBitWidth <= 8)
1492 Converted = (int64_t) (int8_t)Src.Int8Val;
1493 else if (SBitWidth <= 16)
1494 Converted = (int64_t) (int16_t)Src.Int16Val;
1495 else if (SBitWidth <= 32)
1496 Converted = (int64_t) (int32_t)Src.Int32Val;
1497 else
1498 Converted = (int64_t) Src.Int64Val;
1499
1500 if (DstTy->getTypeID() == Type::FloatTyID)
1501 Dest.FloatVal = (float) Converted;
1502 else
1503 Dest.DoubleVal = (double) Converted;
1504 return Dest;
1505}
1506
1507GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1508 ExecutionContext &SF) {
1509 const Type *SrcTy = SrcVal->getType();
1510 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1511 const IntegerType *DITy = cast<IntegerType>(DstTy);
1512 unsigned DBitWidth = DITy->getBitWidth();
1513 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1514 assert(isa<PointerType>(SrcTy) && "Invalid PtrToInt instruction");
1515 INTEGER_ASSIGN(Dest, DBitWidth, (intptr_t) Src.PointerVal);
1516 return Dest;
1517}
1518
1519GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1520 ExecutionContext &SF) {
1521 const Type *SrcTy = SrcVal->getType();
1522 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1523 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1524 unsigned SBitWidth = SITy->getBitWidth();
1525 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1526 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1527 uint64_t Converted = 0;
1528 if (SBitWidth == 1)
1529 Converted = (uint64_t) Src.Int1Val;
1530 else if (SBitWidth <= 8)
1531 Converted = (uint64_t) Src.Int8Val;
1532 else if (SBitWidth <= 16)
1533 Converted = (uint64_t) Src.Int16Val;
1534 else if (SBitWidth <= 32)
1535 Converted = (uint64_t) Src.Int32Val;
1536 else
1537 Converted = (uint64_t) Src.Int64Val;
1538
1539 Dest.PointerVal = (PointerTy) Converted;
1540 return Dest;
1541}
1542
1543GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1544 ExecutionContext &SF) {
1545
1546 const Type *SrcTy = SrcVal->getType();
1547 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1548 if (isa<PointerType>(DstTy)) {
1549 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1550 Dest.PointerVal = Src.PointerVal;
Chris Lattner42a75512007-01-15 02:27:26 +00001551 } else if (DstTy->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001552 const IntegerType *DITy = cast<IntegerType>(DstTy);
1553 unsigned DBitWidth = DITy->getBitWidth();
1554 if (SrcTy == Type::FloatTy) {
1555 Dest.Int32Val = FloatToBits(Src.FloatVal);
1556 } else if (SrcTy == Type::DoubleTy) {
1557 Dest.Int64Val = DoubleToBits(Src.DoubleVal);
Chris Lattner42a75512007-01-15 02:27:26 +00001558 } else if (SrcTy->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001559 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1560 unsigned SBitWidth = SITy->getBitWidth();
1561 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1562 assert(SBitWidth == DBitWidth && "Invalid BitCast");
1563 if (SBitWidth == 1)
1564 Dest.Int1Val = Src.Int1Val;
1565 else if (SBitWidth <= 8)
1566 Dest.Int8Val = Src.Int8Val;
1567 else if (SBitWidth <= 16)
1568 Dest.Int16Val = Src.Int16Val;
1569 else if (SBitWidth <= 32)
1570 Dest.Int32Val = Src.Int32Val;
1571 else
1572 Dest.Int64Val = Src.Int64Val;
Reid Spencer65367b22007-01-18 02:12:51 +00001573 maskToBitWidth(Dest, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001574 } else
1575 assert(0 && "Invalid BitCast");
1576 } else if (DstTy == Type::FloatTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001577 if (SrcTy->isInteger())
Reid Spencera54b7cb2007-01-12 07:05:14 +00001578 Dest.FloatVal = BitsToFloat(Src.Int32Val);
1579 else
1580 Dest.FloatVal = Src.FloatVal;
1581 } else if (DstTy == Type::DoubleTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001582 if (SrcTy->isInteger())
Reid Spencera54b7cb2007-01-12 07:05:14 +00001583 Dest.DoubleVal = BitsToDouble(Src.Int64Val);
1584 else
1585 Dest.DoubleVal = Src.DoubleVal;
1586 } else
1587 assert(0 && "Invalid Bitcast");
1588
1589 return Dest;
1590}
1591
1592void Interpreter::visitTruncInst(TruncInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001593 ExecutionContext &SF = ECStack.back();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001594 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1595}
1596
1597void Interpreter::visitSExtInst(SExtInst &I) {
1598 ExecutionContext &SF = ECStack.back();
1599 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1600}
1601
1602void Interpreter::visitZExtInst(ZExtInst &I) {
1603 ExecutionContext &SF = ECStack.back();
1604 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1605}
1606
1607void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1608 ExecutionContext &SF = ECStack.back();
1609 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1610}
1611
1612void Interpreter::visitFPExtInst(FPExtInst &I) {
1613 ExecutionContext &SF = ECStack.back();
1614 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1615}
1616
1617void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1618 ExecutionContext &SF = ECStack.back();
1619 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1620}
1621
1622void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1623 ExecutionContext &SF = ECStack.back();
1624 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1625}
1626
1627void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1628 ExecutionContext &SF = ECStack.back();
1629 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1630}
1631
1632void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1633 ExecutionContext &SF = ECStack.back();
1634 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1635}
1636
1637void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1638 ExecutionContext &SF = ECStack.back();
1639 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1640}
1641
1642void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1643 ExecutionContext &SF = ECStack.back();
1644 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1645}
1646
1647void Interpreter::visitBitCastInst(BitCastInst &I) {
1648 ExecutionContext &SF = ECStack.back();
1649 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001650}
Chris Lattner92101ac2001-08-23 17:05:04 +00001651
Brian Gaekec1a2be12003-11-07 21:20:47 +00001652#define IMPLEMENT_VAARG(TY) \
1653 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1654
1655void Interpreter::visitVAArgInst(VAArgInst &I) {
1656 ExecutionContext &SF = ECStack.back();
1657
Brian Gaeke9d20b712004-02-25 23:01:48 +00001658 // Get the incoming valist parameter. LLI treats the valist as a
1659 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001660 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001661 GenericValue Dest;
1662 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001663 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001664 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001665 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001666 case Type::IntegerTyID: {
1667 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
1668 if (BitWidth == 1)
1669 Dest.Int1Val = Src.Int1Val;
1670 else if (BitWidth <= 8)
1671 Dest.Int8Val = Src.Int8Val;
1672 else if (BitWidth <= 16)
1673 Dest.Int16Val = Src.Int16Val;
1674 else if (BitWidth <= 32)
1675 Dest.Int32Val = Src.Int32Val;
1676 else if (BitWidth <= 64)
1677 Dest.Int64Val = Src.Int64Val;
1678 else
1679 assert("Integer types > 64 bits not supported");
Reid Spencer65367b22007-01-18 02:12:51 +00001680 maskToBitWidth(Dest, BitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001681 }
Brian Gaekec1a2be12003-11-07 21:20:47 +00001682 IMPLEMENT_VAARG(Pointer);
1683 IMPLEMENT_VAARG(Float);
1684 IMPLEMENT_VAARG(Double);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001685 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001686 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Brian Gaekec1a2be12003-11-07 21:20:47 +00001687 abort();
1688 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001689
Brian Gaekec1a2be12003-11-07 21:20:47 +00001690 // Set the Value of this Instruction.
1691 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001692
1693 // Move the pointer to the next vararg.
1694 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001695}
1696
Chris Lattner92101ac2001-08-23 17:05:04 +00001697//===----------------------------------------------------------------------===//
1698// Dispatch and Execution Code
1699//===----------------------------------------------------------------------===//
1700
Chris Lattner92101ac2001-08-23 17:05:04 +00001701//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001702// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001703//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001704void Interpreter::callFunction(Function *F,
1705 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001706 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1707 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1708 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001709 // Make a new stack frame... and fill it in.
1710 ECStack.push_back(ExecutionContext());
1711 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001712 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001713
1714 // Special handling for external functions.
1715 if (F->isExternal()) {
1716 GenericValue Result = callExternalFunction (F, ArgVals);
1717 // Simulate a 'ret' instruction of the appropriate type.
1718 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1719 return;
1720 }
1721
1722 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001723 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001724 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001725
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001726 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001727 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001728 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001729 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001730
1731 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001732 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001733 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001734 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001735
1736 // Handle varargs arguments...
1737 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001738}
1739
Chris Lattner92101ac2001-08-23 17:05:04 +00001740void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001741 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001742 // Interpret a single instruction & increment the "PC".
1743 ExecutionContext &SF = ECStack.back(); // Current stack frame
1744 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001745
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001746 // Track the number of dynamic instructions executed.
1747 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001748
Bill Wendling480f0932006-11-27 23:54:50 +00001749 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001750 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001751 }
1752}