blob: 4a0c58906a5f389a9a257778647870084b1a2f8b [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmand1c881a2005-04-21 22:43:08 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00009//
Chris Lattner92101ac2001-08-23 17:05:04 +000010// This file contains the actual instruction interpreter.
11//
12//===----------------------------------------------------------------------===//
13
Brian Gaeke63438cc2003-12-11 00:22:59 +000014#define DEBUG_TYPE "interpreter"
Chris Lattner92101ac2001-08-23 17:05:04 +000015#include "Interpreter.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner73011782003-12-28 09:44:37 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/Instructions.h"
Chris Lattner30483732004-06-20 07:49:54 +000019#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattner4af6de82003-11-25 20:44:56 +000020#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencere1aa0662007-03-03 06:22:22 +000021#include "llvm/ADT/APInt.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/ADT/Statistic.h"
23#include "llvm/Support/Debug.h"
Reid Spencera54b7cb2007-01-12 07:05:14 +000024#include "llvm/Support/MathExtras.h"
Jeff Cohen97af7512006-12-02 02:22:01 +000025#include <cmath>
Chris Lattner4af6de82003-11-25 20:44:56 +000026using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000027
Chris Lattnercecf56b2006-12-19 22:56:53 +000028STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
29static Interpreter *TheEE = 0;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner2e42d3a2001-10-15 05:51:48 +000031//===----------------------------------------------------------------------===//
Reid Spencere1aa0662007-03-03 06:22:22 +000032// Various Helper Functions
Chris Lattner39bb5b42001-10-15 13:25:40 +000033//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000034
Reid Spencere1aa0662007-03-03 06:22:22 +000035static inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
Reid Spencera42c7fd2007-01-20 20:12:29 +000036 // Determine if the value is signed or not
37 bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
38 // If its signed, extend the sign bits
39 if (isSigned)
40 Val |= ~ITy->getBitMask();
41 return Val;
42}
43
Chris Lattner39bb5b42001-10-15 13:25:40 +000044static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000045 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +000046}
47
Chris Lattner2e42d3a2001-10-15 05:51:48 +000048void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +000049 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000050}
51
Chris Lattner2adcd832002-05-03 19:52:30 +000052//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000053// Binary Instruction Implementations
54//===----------------------------------------------------------------------===//
55
56#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
Reid Spencerf9536332007-03-06 03:09:31 +000057 case Type::TY##TyID: \
58 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
59 break
Chris Lattner92101ac2001-08-23 17:05:04 +000060
Reid Spencerf9536332007-03-06 03:09:31 +000061#define IMPLEMENT_INTEGER_BINOP1(OP, TY) \
Reid Spencera54b7cb2007-01-12 07:05:14 +000062 case Type::IntegerTyID: { \
Reid Spencerf9536332007-03-06 03:09:31 +000063 Dest.IntVal = Src1.IntVal OP Src2.IntVal; \
Reid Spencera54b7cb2007-01-12 07:05:14 +000064 break; \
65 }
66
Reid Spencera54b7cb2007-01-12 07:05:14 +000067
Reid Spencere1aa0662007-03-03 06:22:22 +000068static void executeAddInst(GenericValue &Dest, GenericValue Src1,
69 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000070 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +000071 IMPLEMENT_INTEGER_BINOP1(+, Ty);
Chris Lattner92101ac2001-08-23 17:05:04 +000072 IMPLEMENT_BINARY_OPERATOR(+, Float);
73 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000074 default:
Bill Wendlinge8156192006-12-07 01:30:32 +000075 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +000076 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +000077 }
Chris Lattner92101ac2001-08-23 17:05:04 +000078}
79
Reid Spencere1aa0662007-03-03 06:22:22 +000080static void executeSubInst(GenericValue &Dest, GenericValue Src1,
81 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000082 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +000083 IMPLEMENT_INTEGER_BINOP1(-, Ty);
Chris Lattner92101ac2001-08-23 17:05:04 +000084 IMPLEMENT_BINARY_OPERATOR(-, Float);
85 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000086 default:
Bill Wendlinge8156192006-12-07 01:30:32 +000087 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +000088 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +000089 }
Chris Lattner92101ac2001-08-23 17:05:04 +000090}
91
Reid Spencere1aa0662007-03-03 06:22:22 +000092static void executeMulInst(GenericValue &Dest, GenericValue Src1,
93 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000094 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +000095 IMPLEMENT_INTEGER_BINOP1(*, Ty);
Chris Lattnerc2593162001-10-27 08:28:11 +000096 IMPLEMENT_BINARY_OPERATOR(*, Float);
97 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +000098 default:
Bill Wendlinge8156192006-12-07 01:30:32 +000099 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000100 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000101 }
Chris Lattnerc2593162001-10-27 08:28:11 +0000102}
103
Reid Spencere1aa0662007-03-03 06:22:22 +0000104static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
105 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000106 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000107 IMPLEMENT_BINARY_OPERATOR(/, Float);
108 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000109 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000110 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000111 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000112 }
Chris Lattnerbb76f022001-10-30 20:27:31 +0000113}
114
Reid Spencere1aa0662007-03-03 06:22:22 +0000115static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
116 GenericValue Src2, const Type *Ty) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000117 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000118 case Type::FloatTyID:
119 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
120 break;
121 case Type::DoubleTyID:
122 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
123 break;
124 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000125 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000126 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000127 }
Chris Lattnerc2593162001-10-27 08:28:11 +0000128}
129
Reid Spencerf9536332007-03-06 03:09:31 +0000130#define IMPLEMENT_INTEGER_ICMP(OP, TY) \
131 case Type::IntegerTyID: \
132 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
133 break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000134
Chris Lattnerfd506f52003-04-23 19:55:35 +0000135// Handle pointers specially because they must be compared with only as much
136// width as the host has. We _do not_ want to be comparing 64 bit values when
137// running on a 32-bit target, otherwise the upper 32 bits might mess up
138// comparisons if they contain garbage.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000139#define IMPLEMENT_POINTER_ICMP(OP) \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000140 case Type::PointerTyID: \
Reid Spencerf9536332007-03-06 03:09:31 +0000141 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
142 (void*)(intptr_t)Src2.PointerVal); \
143 break;
Chris Lattnerfd506f52003-04-23 19:55:35 +0000144
Reid Spencere4d87aa2006-12-23 06:05:41 +0000145static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
146 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000147 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000148 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000149 IMPLEMENT_INTEGER_ICMP(eq,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000150 IMPLEMENT_POINTER_ICMP(==);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000151 default:
152 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
153 abort();
154 }
155 return Dest;
156}
157
158static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
159 const Type *Ty) {
160 GenericValue Dest;
161 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000162 IMPLEMENT_INTEGER_ICMP(ne,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000163 IMPLEMENT_POINTER_ICMP(!=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000164 default:
165 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
166 abort();
167 }
168 return Dest;
169}
170
171static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
172 const Type *Ty) {
173 GenericValue Dest;
174 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000175 IMPLEMENT_INTEGER_ICMP(ult,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000176 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000177 default:
178 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
179 abort();
180 }
181 return Dest;
182}
183
184static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
185 const Type *Ty) {
186 GenericValue Dest;
187 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000188 IMPLEMENT_INTEGER_ICMP(slt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000189 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000190 default:
191 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
192 abort();
193 }
194 return Dest;
195}
196
197static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
198 const Type *Ty) {
199 GenericValue Dest;
200 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000201 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000202 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000203 default:
204 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
205 abort();
206 }
207 return Dest;
208}
209
210static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
211 const Type *Ty) {
212 GenericValue Dest;
213 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000214 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000215 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000216 default:
217 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
218 abort();
219 }
220 return Dest;
221}
222
223static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
224 const Type *Ty) {
225 GenericValue Dest;
226 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000227 IMPLEMENT_INTEGER_ICMP(ule,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000228 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000229 default:
230 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
231 abort();
232 }
233 return Dest;
234}
235
236static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
237 const Type *Ty) {
238 GenericValue Dest;
239 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000240 IMPLEMENT_INTEGER_ICMP(sle,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000241 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000242 default:
243 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
244 abort();
245 }
246 return Dest;
247}
248
249static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
250 const Type *Ty) {
251 GenericValue Dest;
252 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000253 IMPLEMENT_INTEGER_ICMP(uge,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000254 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000255 default:
256 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
257 abort();
258 }
259 return Dest;
260}
261
262static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
263 const Type *Ty) {
264 GenericValue Dest;
265 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000266 IMPLEMENT_INTEGER_ICMP(sge,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000267 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000268 default:
269 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
270 abort();
271 }
272 return Dest;
273}
274
Reid Spencere49661b2006-12-31 05:51:36 +0000275void Interpreter::visitICmpInst(ICmpInst &I) {
276 ExecutionContext &SF = ECStack.back();
277 const Type *Ty = I.getOperand(0)->getType();
278 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
279 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
280 GenericValue R; // Result
281
282 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000283 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
284 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
Reid Spencere49661b2006-12-31 05:51:36 +0000285 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
286 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
287 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
288 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
289 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
290 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
291 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
292 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
293 default:
294 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
295 abort();
296 }
297
298 SetValue(&I, R, SF);
299}
300
301#define IMPLEMENT_FCMP(OP, TY) \
Reid Spencerf9536332007-03-06 03:09:31 +0000302 case Type::TY##TyID: \
303 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
304 break
Reid Spencere4d87aa2006-12-23 06:05:41 +0000305
Reid Spencera54b7cb2007-01-12 07:05:14 +0000306static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000307 const Type *Ty) {
308 GenericValue Dest;
309 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000310 IMPLEMENT_FCMP(==, Float);
311 IMPLEMENT_FCMP(==, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000312 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000313 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000314 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000315 }
316 return Dest;
317}
318
Reid Spencera54b7cb2007-01-12 07:05:14 +0000319static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000320 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000321 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000322 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000323 IMPLEMENT_FCMP(!=, Float);
324 IMPLEMENT_FCMP(!=, Double);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000325
Chris Lattner92101ac2001-08-23 17:05:04 +0000326 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000327 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000328 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000329 }
330 return Dest;
331}
332
Reid Spencera54b7cb2007-01-12 07:05:14 +0000333static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000334 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000335 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000336 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000337 IMPLEMENT_FCMP(<=, Float);
338 IMPLEMENT_FCMP(<=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000339 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000340 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000341 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000342 }
343 return Dest;
344}
345
Reid Spencera54b7cb2007-01-12 07:05:14 +0000346static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000347 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000348 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000349 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000350 IMPLEMENT_FCMP(>=, Float);
351 IMPLEMENT_FCMP(>=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000352 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000353 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000354 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000355 }
356 return Dest;
357}
358
Reid Spencera54b7cb2007-01-12 07:05:14 +0000359static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000360 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000361 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000362 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000363 IMPLEMENT_FCMP(<, Float);
364 IMPLEMENT_FCMP(<, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000365 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000366 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000367 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000368 }
369 return Dest;
370}
371
Reid Spencera54b7cb2007-01-12 07:05:14 +0000372static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000373 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000374 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000375 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000376 IMPLEMENT_FCMP(>, Float);
377 IMPLEMENT_FCMP(>, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000378 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000379 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000380 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000381 }
382 return Dest;
383}
384
Reid Spencera54b7cb2007-01-12 07:05:14 +0000385#define IMPLEMENT_UNORDERED(TY, X,Y) \
386 if (TY == Type::FloatTy) \
387 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
Reid Spencerf9536332007-03-06 03:09:31 +0000388 Dest.IntVal = APInt(1,true); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000389 return Dest; \
390 } \
391 else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
Reid Spencerf9536332007-03-06 03:09:31 +0000392 Dest.IntVal = APInt(1,true); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000393 return Dest; \
394 }
395
396
397static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
398 const Type *Ty) {
399 GenericValue Dest;
400 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
401 return executeFCMP_OEQ(Src1, Src2, Ty);
402}
403
404static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
405 const Type *Ty) {
406 GenericValue Dest;
407 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
408 return executeFCMP_ONE(Src1, Src2, Ty);
409}
410
411static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
412 const Type *Ty) {
413 GenericValue Dest;
414 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
415 return executeFCMP_OLE(Src1, Src2, Ty);
416}
417
418static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
419 const Type *Ty) {
420 GenericValue Dest;
421 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
422 return executeFCMP_OGE(Src1, Src2, Ty);
423}
424
425static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
426 const Type *Ty) {
427 GenericValue Dest;
428 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
429 return executeFCMP_OLT(Src1, Src2, Ty);
430}
431
432static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
433 const Type *Ty) {
434 GenericValue Dest;
435 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
436 return executeFCMP_OGT(Src1, Src2, Ty);
437}
438
439static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
440 const Type *Ty) {
441 GenericValue Dest;
442 if (Ty == Type::FloatTy)
Reid Spencerf9536332007-03-06 03:09:31 +0000443 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
444 Src2.FloatVal == Src2.FloatVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000445 else
Reid Spencerf9536332007-03-06 03:09:31 +0000446 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
447 Src2.DoubleVal == Src2.DoubleVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000448 return Dest;
449}
450
451static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
452 const Type *Ty) {
453 GenericValue Dest;
454 if (Ty == Type::FloatTy)
Reid Spencerf9536332007-03-06 03:09:31 +0000455 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
456 Src2.FloatVal != Src2.FloatVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000457 else
Reid Spencerf9536332007-03-06 03:09:31 +0000458 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
459 Src2.DoubleVal != Src2.DoubleVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000460 return Dest;
461}
462
Reid Spencere4d87aa2006-12-23 06:05:41 +0000463void Interpreter::visitFCmpInst(FCmpInst &I) {
464 ExecutionContext &SF = ECStack.back();
465 const Type *Ty = I.getOperand(0)->getType();
466 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
467 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
468 GenericValue R; // Result
469
470 switch (I.getPredicate()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000471 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
472 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000473 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
474 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
475 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
476 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
477 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
478 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
479 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
480 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
481 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
482 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
483 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
484 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
485 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
486 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000487 default:
488 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
489 abort();
490 }
491
492 SetValue(&I, R, SF);
493}
494
Reid Spencere4d87aa2006-12-23 06:05:41 +0000495static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
496 GenericValue Src2, const Type *Ty) {
497 GenericValue Result;
498 switch (predicate) {
499 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
500 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
501 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
502 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
503 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
504 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
505 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
506 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
507 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
508 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000509 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
510 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
511 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
512 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
513 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
514 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
515 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
516 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
517 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
518 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
519 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
520 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
521 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
522 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000523 case FCmpInst::FCMP_FALSE: {
524 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000525 Result.IntVal = APInt(1, false);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000526 return Result;
527 }
528 case FCmpInst::FCMP_TRUE: {
529 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000530 Result.IntVal = APInt(1, true);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000531 return Result;
532 }
533 default:
534 cerr << "Unhandled Cmp predicate\n";
535 abort();
536 }
537}
538
Chris Lattnerd7916e92003-05-10 21:22:39 +0000539void Interpreter::visitBinaryOperator(BinaryOperator &I) {
540 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000541 const Type *Ty = I.getOperand(0)->getType();
542 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
543 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000544 GenericValue R; // Result
545
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000546 switch (I.getOpcode()) {
Reid Spencere1aa0662007-03-03 06:22:22 +0000547 case Instruction::Add: executeAddInst (R, Src1, Src2, Ty); break;
548 case Instruction::Sub: executeSubInst (R, Src1, Src2, Ty); break;
549 case Instruction::Mul: executeMulInst (R, Src1, Src2, Ty); break;
Reid Spencere1aa0662007-03-03 06:22:22 +0000550 case Instruction::FDiv: executeFDivInst (R, Src1, Src2, Ty); break;
Reid Spencere1aa0662007-03-03 06:22:22 +0000551 case Instruction::FRem: executeFRemInst (R, Src1, Src2, Ty); break;
Reid Spencerf9536332007-03-06 03:09:31 +0000552 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
553 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
554 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
555 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
556 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
557 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
558 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000559 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000560 cerr << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner02868352003-04-22 21:22:33 +0000561 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000562 }
563
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000564 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000565}
566
Misha Brukmand1c881a2005-04-21 22:43:08 +0000567static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000568 GenericValue Src3) {
Reid Spencerf9536332007-03-06 03:09:31 +0000569 return Src1.IntVal == 0 ? Src3 : Src2;
Chris Lattner759d34f2004-04-20 16:43:21 +0000570}
571
572void Interpreter::visitSelectInst(SelectInst &I) {
573 ExecutionContext &SF = ECStack.back();
574 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
575 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
576 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
Reid Spencerf9536332007-03-06 03:09:31 +0000577 GenericValue R = executeSelectInst(Src1, Src2, Src3);
Chris Lattner759d34f2004-04-20 16:43:21 +0000578 SetValue(&I, R, SF);
579}
580
581
Chris Lattner92101ac2001-08-23 17:05:04 +0000582//===----------------------------------------------------------------------===//
583// Terminator Instruction Implementations
584//===----------------------------------------------------------------------===//
585
Chris Lattnere43db882001-10-27 04:15:57 +0000586void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000587 // runAtExitHandlers() assumes there are no stack frames, but
588 // if exit() was called, then it had a stack frame. Blow away
589 // the stack before interpreting atexit handlers.
590 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000591 runAtExitHandlers ();
Reid Spencerf9536332007-03-06 03:09:31 +0000592 exit (GV.IntVal.zextOrTrunc(32).getZExtValue());
Chris Lattnere43db882001-10-27 04:15:57 +0000593}
594
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000595/// Pop the last stack frame off of ECStack and then copy the result
596/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000597/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000598/// CallInst if there was a previous stack frame. This method may
599/// invalidate any ECStack iterators you have. This method also takes
600/// care of switching to the normal destination BB, if we are returning
601/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000602///
603void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
604 GenericValue Result) {
605 // Pop the current stack frame.
606 ECStack.pop_back();
607
Misha Brukmand1c881a2005-04-21 22:43:08 +0000608 if (ECStack.empty()) { // Finished main. Put result into exit code...
Chris Lattner42a75512007-01-15 02:27:26 +0000609 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000610 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000611 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000612 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000613 }
614 } else {
615 // If we have a previous stack frame, and we have a previous call,
616 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000617 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000618 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000619 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000620 SetValue(I, Result, CallingSF);
621 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
622 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000623 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000624 }
625 }
626}
627
Chris Lattnerd7916e92003-05-10 21:22:39 +0000628void Interpreter::visitReturnInst(ReturnInst &I) {
629 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000630 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000631 GenericValue Result;
632
633 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000634 if (I.getNumOperands()) {
635 RetTy = I.getReturnValue()->getType();
636 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000637 }
638
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000639 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000640}
641
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000642void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000643 // Unwind stack
644 Instruction *Inst;
645 do {
646 ECStack.pop_back ();
647 if (ECStack.empty ())
648 abort ();
649 Inst = ECStack.back ().Caller.getInstruction ();
650 } while (!(Inst && isa<InvokeInst> (Inst)));
651
652 // Return from invoke
653 ExecutionContext &InvokingSF = ECStack.back ();
654 InvokingSF.Caller = CallSite ();
655
656 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000657 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000658}
659
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000660void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000661 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000662 abort();
663}
664
Chris Lattnerd7916e92003-05-10 21:22:39 +0000665void Interpreter::visitBranchInst(BranchInst &I) {
666 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000667 BasicBlock *Dest;
668
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000669 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
670 if (!I.isUnconditional()) {
671 Value *Cond = I.getCondition();
Reid Spencerf9536332007-03-06 03:09:31 +0000672 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000673 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000674 }
Chris Lattner77113b62003-05-10 20:21:16 +0000675 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000676}
677
Chris Lattnerd7916e92003-05-10 21:22:39 +0000678void Interpreter::visitSwitchInst(SwitchInst &I) {
679 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000680 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
681 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000682
683 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000684 BasicBlock *Dest = 0;
685 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Reid Spencerf9536332007-03-06 03:09:31 +0000686 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
687 .IntVal != 0) {
Chris Lattner09e93922003-04-22 20:34:47 +0000688 Dest = cast<BasicBlock>(I.getOperand(i+1));
689 break;
690 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000691
Chris Lattner09e93922003-04-22 20:34:47 +0000692 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000693 SwitchToNewBasicBlock(Dest, SF);
694}
695
696// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
697// This function handles the actual updating of block and instruction iterators
698// as well as execution of all of the PHI nodes in the destination block.
699//
700// This method does this because all of the PHI nodes must be executed
701// atomically, reading their inputs before any of the results are updated. Not
702// doing this can cause problems if the PHI nodes depend on other PHI nodes for
703// their inputs. If the input PHI node is updated before it is read, incorrect
704// results can happen. Thus we use a two phase approach.
705//
706void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
707 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
708 SF.CurBB = Dest; // Update CurBB to branch destination
709 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
710
711 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
712
713 // Loop over all of the PHI nodes in the current block, reading their inputs.
714 std::vector<GenericValue> ResultValues;
715
716 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
717 // Search for the value corresponding to this previous bb...
718 int i = PN->getBasicBlockIndex(PrevBB);
719 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
720 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000721
Chris Lattner77113b62003-05-10 20:21:16 +0000722 // Save the incoming value for this PHI node...
723 ResultValues.push_back(getOperandValue(IncomingValue, SF));
724 }
725
726 // Now loop over all of the PHI nodes setting their values...
727 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000728 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
729 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000730 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000731 }
Chris Lattner09e93922003-04-22 20:34:47 +0000732}
733
Chris Lattner92101ac2001-08-23 17:05:04 +0000734//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000735// Memory Instruction Implementations
736//===----------------------------------------------------------------------===//
737
Chris Lattnerd7916e92003-05-10 21:22:39 +0000738void Interpreter::visitAllocationInst(AllocationInst &I) {
739 ExecutionContext &SF = ECStack.back();
740
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000741 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000742
Chris Lattnercc82cc12002-04-28 21:57:33 +0000743 // Get the number of elements being allocated by the array...
Reid Spencerf9536332007-03-06 03:09:31 +0000744 unsigned NumElements =
745 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
746
747 unsigned TypeSize = (size_t)TD.getTypeSize(Ty);
748
749 unsigned MemToAlloc = NumElements * TypeSize;
Chris Lattner86660982001-08-27 05:16:50 +0000750
751 // Allocate enough memory to hold the type...
Reid Spencerf9536332007-03-06 03:09:31 +0000752 void *Memory = malloc(MemToAlloc);
753
754 DOUT << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
755 << NumElements << " (Total: " << MemToAlloc << ") at "
756 << unsigned(Memory) << '\n';
Chris Lattner9bffa732002-02-19 18:50:09 +0000757
Chris Lattnerfe11a972002-12-23 23:59:41 +0000758 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000759 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000760 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000761
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000762 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000763 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000764}
765
Chris Lattnerd7916e92003-05-10 21:22:39 +0000766void Interpreter::visitFreeInst(FreeInst &I) {
767 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000768 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
769 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000770 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000771 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000772}
773
Chris Lattnera34c5682002-08-27 22:33:45 +0000774// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000775//
Chris Lattner4af6de82003-11-25 20:44:56 +0000776GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000777 gep_type_iterator E,
778 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000779 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000780 "Cannot getElementOffset of a nonpointer type!");
781
Reid Spencerf9536332007-03-06 03:09:31 +0000782 uint64_t Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000783
784 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000785 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000786 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000787
Reid Spencerb83eb642006-10-20 07:07:24 +0000788 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
789 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000790
Reid Spencerf9536332007-03-06 03:09:31 +0000791 Total += SLO->getElementOffset(Index);
Chris Lattner4af6de82003-11-25 20:44:56 +0000792 } else {
793 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000794 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000795 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
796
Reid Spencera54b7cb2007-01-12 07:05:14 +0000797 int64_t Idx;
798 unsigned BitWidth =
799 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
Reid Spencer23e28832007-01-18 01:25:42 +0000800 if (BitWidth == 32)
Reid Spencerf9536332007-03-06 03:09:31 +0000801 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
Reid Spencer23e28832007-01-18 01:25:42 +0000802 else if (BitWidth == 64)
Reid Spencerf9536332007-03-06 03:09:31 +0000803 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000804 else
Reid Spencer23e28832007-01-18 01:25:42 +0000805 assert(0 && "Invalid index type for getelementptr");
Reid Spencerf9536332007-03-06 03:09:31 +0000806 Total += TD.getTypeSize(ST->getElementType())*Idx;
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000807 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000808 }
809
Chris Lattnera34c5682002-08-27 22:33:45 +0000810 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000811 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
812 DOUT << "GEP Index " << Total << " bytes.\n";
Chris Lattnera34c5682002-08-27 22:33:45 +0000813 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000814}
815
Chris Lattnerd7916e92003-05-10 21:22:39 +0000816void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
817 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000818 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000819 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000820}
821
Chris Lattnerd7916e92003-05-10 21:22:39 +0000822void Interpreter::visitLoadInst(LoadInst &I) {
823 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000824 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000825 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Reid Spencere0929362007-03-03 08:38:04 +0000826 GenericValue Result;
Reid Spencere0929362007-03-03 08:38:04 +0000827 LoadValueFromMemory(Result, Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000828 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000829}
830
Chris Lattnerd7916e92003-05-10 21:22:39 +0000831void Interpreter::visitStoreInst(StoreInst &I) {
832 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000833 GenericValue Val = getOperandValue(I.getOperand(0), SF);
834 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000835 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000836 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000837}
838
Chris Lattner86660982001-08-27 05:16:50 +0000839//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000840// Miscellaneous Instruction Implementations
841//===----------------------------------------------------------------------===//
842
Brian Gaekefea483d2003-11-07 20:04:22 +0000843void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000844 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000845
846 // Check to see if this is an intrinsic function call...
847 if (Function *F = CS.getCalledFunction())
Reid Spencer5cbf9852007-01-30 20:08:39 +0000848 if (F->isDeclaration ())
Chris Lattner73011782003-12-28 09:44:37 +0000849 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000850 case Intrinsic::not_intrinsic:
851 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000852 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000853 GenericValue ArgIndex;
854 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
855 ArgIndex.UIntPairVal.second = 0;
856 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000857 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000858 }
Chris Lattner317201d2004-03-13 00:24:00 +0000859 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000860 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000861 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000862 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
863 return;
864 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000865 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000866 // class to transform it into hopefully tasty LLVM code.
867 //
868 Instruction *Prev = CS.getInstruction()->getPrev();
869 BasicBlock *Parent = CS.getInstruction()->getParent();
870 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
871
872 // Restore the CurInst pointer to the first instruction newly inserted, if
873 // any.
874 if (!Prev) {
875 SF.CurInst = Parent->begin();
876 } else {
877 SF.CurInst = Prev;
878 ++SF.CurInst;
879 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000880 return;
Chris Lattner73011782003-12-28 09:44:37 +0000881 }
882
Brian Gaekefea483d2003-11-07 20:04:22 +0000883 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000884 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000885 const unsigned NumArgs = SF.Caller.arg_size();
886 ArgVals.reserve(NumArgs);
887 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
888 e = SF.Caller.arg_end(); i != e; ++i) {
889 Value *V = *i;
890 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000891 // Promote all integral types whose size is < sizeof(int) into ints. We do
892 // this by zero or sign extending the value as appropriate according to the
893 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000894 const Type *Ty = V->getType();
Reid Spencerf9536332007-03-06 03:09:31 +0000895 if (Ty->isInteger())
896 if (ArgVals.back().IntVal.getBitWidth() < 32)
897 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
Chris Lattner93780132003-01-13 00:58:52 +0000898 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000899
Misha Brukmand1c881a2005-04-21 22:43:08 +0000900 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000901 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000902 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000903 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000904}
905
Reid Spencer832254e2007-02-02 02:16:23 +0000906void Interpreter::visitShl(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000907 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000908 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
909 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
910 GenericValue Dest;
Reid Spencerf9536332007-03-06 03:09:31 +0000911 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000912 SetValue(&I, Dest, SF);
913}
914
Reid Spencer832254e2007-02-02 02:16:23 +0000915void Interpreter::visitLShr(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000916 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000917 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
918 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
919 GenericValue Dest;
Reid Spencerf9536332007-03-06 03:09:31 +0000920 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
Reid Spencer3822ff52006-11-08 06:47:33 +0000921 SetValue(&I, Dest, SF);
922}
923
Reid Spencer832254e2007-02-02 02:16:23 +0000924void Interpreter::visitAShr(BinaryOperator &I) {
Reid Spencer3822ff52006-11-08 06:47:33 +0000925 ExecutionContext &SF = ECStack.back();
Reid Spencer3822ff52006-11-08 06:47:33 +0000926 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
927 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Reid Spencerf9536332007-03-06 03:09:31 +0000928 GenericValue Dest;
929 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000930 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000931}
932
Reid Spencera54b7cb2007-01-12 07:05:14 +0000933GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
934 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000935 const Type *SrcTy = SrcVal->getType();
936 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000937 const IntegerType *DITy = cast<IntegerType>(DstTy);
938 const IntegerType *SITy = cast<IntegerType>(SrcTy);
939 unsigned DBitWidth = DITy->getBitWidth();
940 unsigned SBitWidth = SITy->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000941 assert(SBitWidth > DBitWidth && "Invalid truncate");
Reid Spencerf9536332007-03-06 03:09:31 +0000942 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
Chris Lattnera34c5682002-08-27 22:33:45 +0000943 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000944}
Chris Lattner92101ac2001-08-23 17:05:04 +0000945
Reid Spencera54b7cb2007-01-12 07:05:14 +0000946GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
947 ExecutionContext &SF) {
948 const Type *SrcTy = SrcVal->getType();
949 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
950 const IntegerType *DITy = cast<IntegerType>(DstTy);
951 const IntegerType *SITy = cast<IntegerType>(SrcTy);
952 unsigned DBitWidth = DITy->getBitWidth();
953 unsigned SBitWidth = SITy->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000954 assert(SBitWidth < DBitWidth && "Invalid sign extend");
Reid Spencerf9536332007-03-06 03:09:31 +0000955 Dest.IntVal = Src.IntVal.sext(DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000956 return Dest;
957}
958
959GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
960 ExecutionContext &SF) {
961 const Type *SrcTy = SrcVal->getType();
962 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
963 const IntegerType *DITy = cast<IntegerType>(DstTy);
964 const IntegerType *SITy = cast<IntegerType>(SrcTy);
965 unsigned DBitWidth = DITy->getBitWidth();
966 unsigned SBitWidth = SITy->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000967 assert(SBitWidth < DBitWidth && "Invalid sign extend");
Reid Spencerf9536332007-03-06 03:09:31 +0000968 Dest.IntVal = Src.IntVal.zext(DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000969 return Dest;
970}
971
972GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
973 ExecutionContext &SF) {
974 const Type *SrcTy = SrcVal->getType();
975 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
976 assert(SrcTy == Type::DoubleTy && DstTy == Type::FloatTy &&
977 "Invalid FPTrunc instruction");
978 Dest.FloatVal = (float) Src.DoubleVal;
979 return Dest;
980}
981
982GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
983 ExecutionContext &SF) {
984 const Type *SrcTy = SrcVal->getType();
985 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
986 assert(SrcTy == Type::FloatTy && DstTy == Type::DoubleTy &&
987 "Invalid FPTrunc instruction");
988 Dest.DoubleVal = (double) Src.FloatVal;
989 return Dest;
990}
991
992GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
993 ExecutionContext &SF) {
994 const Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +0000995 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000996 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000997 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
Reid Spencere0929362007-03-03 08:38:04 +0000998
Reid Spencera54b7cb2007-01-12 07:05:14 +0000999 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001000 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001001 else
Reid Spencerf9536332007-03-06 03:09:31 +00001002 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001003 return Dest;
1004}
1005
1006GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1007 ExecutionContext &SF) {
1008 const Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +00001009 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001010 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001011 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001012
Reid Spencera54b7cb2007-01-12 07:05:14 +00001013 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001014 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001015 else
Reid Spencerf9536332007-03-06 03:09:31 +00001016 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001017 return Dest;
1018}
1019
1020GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1021 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001022 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001023 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001024
Reid Spencera54b7cb2007-01-12 07:05:14 +00001025 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001026 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001027 else
Reid Spencerf9536332007-03-06 03:09:31 +00001028 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001029 return Dest;
1030}
1031
1032GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1033 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001034 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencer24d6da52007-01-21 00:29:26 +00001035 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001036
Reid Spencera54b7cb2007-01-12 07:05:14 +00001037 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001038 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001039 else
Reid Spencerf9536332007-03-06 03:09:31 +00001040 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001041 return Dest;
Reid Spencerf9536332007-03-06 03:09:31 +00001042
Reid Spencera54b7cb2007-01-12 07:05:14 +00001043}
1044
1045GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1046 ExecutionContext &SF) {
1047 const Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +00001048 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001049 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001050 assert(isa<PointerType>(SrcTy) && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001051
Reid Spencerf9536332007-03-06 03:09:31 +00001052 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001053 return Dest;
1054}
1055
1056GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1057 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001058 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001059 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001060
Reid Spencer7553c342007-03-06 03:41:50 +00001061 uint32_t PtrSize = TD.getPointerSize();
1062 if (PtrSize != Src.IntVal.getBitWidth())
1063 Src.IntVal = Src.IntVal.trunc(PtrSize);
1064
1065 Dest.PointerVal = (PointerTy) Src.IntVal.getZExtValue();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001066 return Dest;
1067}
1068
1069GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1070 ExecutionContext &SF) {
1071
1072 const Type *SrcTy = SrcVal->getType();
1073 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1074 if (isa<PointerType>(DstTy)) {
1075 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1076 Dest.PointerVal = Src.PointerVal;
Chris Lattner42a75512007-01-15 02:27:26 +00001077 } else if (DstTy->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001078 if (SrcTy == Type::FloatTy) {
Reid Spencerf9536332007-03-06 03:09:31 +00001079 Dest.IntVal.floatToBits(Src.FloatVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001080 } else if (SrcTy == Type::DoubleTy) {
Reid Spencerf9536332007-03-06 03:09:31 +00001081 Dest.IntVal.doubleToBits(Src.DoubleVal);
Chris Lattner42a75512007-01-15 02:27:26 +00001082 } else if (SrcTy->isInteger()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001083 Dest.IntVal = Src.IntVal;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001084 } else
1085 assert(0 && "Invalid BitCast");
1086 } else if (DstTy == Type::FloatTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001087 if (SrcTy->isInteger())
Reid Spencerf9536332007-03-06 03:09:31 +00001088 Dest.FloatVal = Src.IntVal.bitsToFloat();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001089 else
1090 Dest.FloatVal = Src.FloatVal;
1091 } else if (DstTy == Type::DoubleTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001092 if (SrcTy->isInteger())
Reid Spencerf9536332007-03-06 03:09:31 +00001093 Dest.DoubleVal = Src.IntVal.bitsToDouble();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001094 else
1095 Dest.DoubleVal = Src.DoubleVal;
1096 } else
1097 assert(0 && "Invalid Bitcast");
1098
1099 return Dest;
1100}
1101
1102void Interpreter::visitTruncInst(TruncInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001103 ExecutionContext &SF = ECStack.back();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001104 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1105}
1106
1107void Interpreter::visitSExtInst(SExtInst &I) {
1108 ExecutionContext &SF = ECStack.back();
1109 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1110}
1111
1112void Interpreter::visitZExtInst(ZExtInst &I) {
1113 ExecutionContext &SF = ECStack.back();
1114 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1115}
1116
1117void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1118 ExecutionContext &SF = ECStack.back();
1119 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1120}
1121
1122void Interpreter::visitFPExtInst(FPExtInst &I) {
1123 ExecutionContext &SF = ECStack.back();
1124 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1125}
1126
1127void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1128 ExecutionContext &SF = ECStack.back();
1129 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1130}
1131
1132void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1133 ExecutionContext &SF = ECStack.back();
1134 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1135}
1136
1137void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1138 ExecutionContext &SF = ECStack.back();
1139 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1140}
1141
1142void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1143 ExecutionContext &SF = ECStack.back();
1144 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1145}
1146
1147void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1148 ExecutionContext &SF = ECStack.back();
1149 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1150}
1151
1152void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1153 ExecutionContext &SF = ECStack.back();
1154 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1155}
1156
1157void Interpreter::visitBitCastInst(BitCastInst &I) {
1158 ExecutionContext &SF = ECStack.back();
1159 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001160}
Chris Lattner92101ac2001-08-23 17:05:04 +00001161
Brian Gaekec1a2be12003-11-07 21:20:47 +00001162#define IMPLEMENT_VAARG(TY) \
1163 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1164
1165void Interpreter::visitVAArgInst(VAArgInst &I) {
1166 ExecutionContext &SF = ECStack.back();
1167
Brian Gaeke9d20b712004-02-25 23:01:48 +00001168 // Get the incoming valist parameter. LLI treats the valist as a
1169 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001170 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001171 GenericValue Dest;
1172 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Reid Spencerf9536332007-03-06 03:09:31 +00001173 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001174 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001175 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001176 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001177 IMPLEMENT_VAARG(Pointer);
1178 IMPLEMENT_VAARG(Float);
1179 IMPLEMENT_VAARG(Double);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001180 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001181 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Brian Gaekec1a2be12003-11-07 21:20:47 +00001182 abort();
1183 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001184
Brian Gaekec1a2be12003-11-07 21:20:47 +00001185 // Set the Value of this Instruction.
1186 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001187
1188 // Move the pointer to the next vararg.
1189 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001190}
1191
Reid Spencere1aa0662007-03-03 06:22:22 +00001192GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1193 ExecutionContext &SF) {
1194 switch (CE->getOpcode()) {
1195 case Instruction::Trunc:
1196 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1197 case Instruction::ZExt:
1198 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1199 case Instruction::SExt:
1200 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1201 case Instruction::FPTrunc:
1202 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1203 case Instruction::FPExt:
1204 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1205 case Instruction::UIToFP:
1206 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1207 case Instruction::SIToFP:
1208 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1209 case Instruction::FPToUI:
1210 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1211 case Instruction::FPToSI:
1212 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1213 case Instruction::PtrToInt:
1214 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1215 case Instruction::IntToPtr:
1216 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1217 case Instruction::BitCast:
1218 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1219 case Instruction::GetElementPtr:
1220 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1221 gep_type_end(CE), SF);
Reid Spencere0929362007-03-03 08:38:04 +00001222 case Instruction::FCmp:
1223 case Instruction::ICmp:
1224 return executeCmpInst(CE->getPredicate(),
1225 getOperandValue(CE->getOperand(0), SF),
1226 getOperandValue(CE->getOperand(1), SF),
1227 CE->getOperand(0)->getType());
1228 case Instruction::Select:
1229 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1230 getOperandValue(CE->getOperand(1), SF),
1231 getOperandValue(CE->getOperand(2), SF));
Reid Spencere1aa0662007-03-03 06:22:22 +00001232 default :
1233 break;
1234 }
Reid Spencere0929362007-03-03 08:38:04 +00001235
1236 // The cases below here require a GenericValue parameter for the result
1237 // so we initialize one, compute it and then return it.
Reid Spencerf9536332007-03-06 03:09:31 +00001238 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1239 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
Reid Spencere1aa0662007-03-03 06:22:22 +00001240 GenericValue Dest;
Reid Spencerf9536332007-03-06 03:09:31 +00001241 const Type * Ty = CE->getOperand(0)->getType();
Reid Spencere1aa0662007-03-03 06:22:22 +00001242 switch (CE->getOpcode()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001243 case Instruction::Add: executeAddInst (Dest, Op0, Op1, Ty); break;
1244 case Instruction::Sub: executeSubInst (Dest, Op0, Op1, Ty); break;
1245 case Instruction::Mul: executeMulInst (Dest, Op0, Op1, Ty); break;
1246 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1247 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1248 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1249 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1250 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1251 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
1252 case Instruction::And: Dest.IntVal = Op0.IntVal.And(Op1.IntVal); break;
1253 case Instruction::Or: Dest.IntVal = Op0.IntVal.Or(Op1.IntVal); break;
1254 case Instruction::Xor: Dest.IntVal = Op0.IntVal.Xor(Op1.IntVal); break;
1255 case Instruction::Shl:
1256 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1257 break;
1258 case Instruction::LShr:
1259 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1260 break;
1261 case Instruction::AShr:
1262 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1263 break;
Reid Spencere1aa0662007-03-03 06:22:22 +00001264 default:
1265 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
1266 abort();
1267 return GenericValue();
1268 }
Reid Spencere0929362007-03-03 08:38:04 +00001269 return Dest;
Reid Spencere1aa0662007-03-03 06:22:22 +00001270}
1271
1272GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1273 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1274 return getConstantExprValue(CE, SF);
1275 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1276 return getConstantValue(CPV);
1277 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1278 return PTOGV(getPointerToGlobal(GV));
1279 } else {
1280 return SF.Values[V];
1281 }
1282}
1283
Chris Lattner92101ac2001-08-23 17:05:04 +00001284//===----------------------------------------------------------------------===//
1285// Dispatch and Execution Code
1286//===----------------------------------------------------------------------===//
1287
Chris Lattner92101ac2001-08-23 17:05:04 +00001288//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001289// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001290//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001291void Interpreter::callFunction(Function *F,
1292 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001293 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1294 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1295 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001296 // Make a new stack frame... and fill it in.
1297 ECStack.push_back(ExecutionContext());
1298 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001299 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001300
1301 // Special handling for external functions.
Reid Spencer5cbf9852007-01-30 20:08:39 +00001302 if (F->isDeclaration()) {
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001303 GenericValue Result = callExternalFunction (F, ArgVals);
1304 // Simulate a 'ret' instruction of the appropriate type.
1305 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1306 return;
1307 }
1308
1309 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001310 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001311 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001312
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001313 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001314 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001315 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001316 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001317
1318 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001319 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001320 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001321 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001322
1323 // Handle varargs arguments...
1324 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001325}
1326
Chris Lattner92101ac2001-08-23 17:05:04 +00001327void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001328 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001329 // Interpret a single instruction & increment the "PC".
1330 ExecutionContext &SF = ECStack.back(); // Current stack frame
1331 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001332
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001333 // Track the number of dynamic instructions executed.
1334 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001335
Bill Wendling480f0932006-11-27 23:54:50 +00001336 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001337 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001338 }
1339}