blob: 29837ffcfec74a1aa9f295f1c4123cce545f922b [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Reid Spencer4ccf4622007-04-16 21:50:40 +000019#include "llvm/ParameterAttributes.h"
Chris Lattner30483732004-06-20 07:49:54 +000020#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattner4af6de82003-11-25 20:44:56 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencere1aa0662007-03-03 06:22:22 +000022#include "llvm/ADT/APInt.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/ADT/Statistic.h"
Chris Lattner663ceeb2008-07-08 17:25:49 +000024#include "llvm/Support/CommandLine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/Debug.h"
Reid Spencera54b7cb2007-01-12 07:05:14 +000026#include "llvm/Support/MathExtras.h"
Gabor Greif724441e2007-10-11 19:40:35 +000027#include <algorithm>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000028#include <cmath>
29#include <cstring>
Chris Lattner4af6de82003-11-25 20:44:56 +000030using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000031
Chris Lattnercecf56b2006-12-19 22:56:53 +000032STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
33static Interpreter *TheEE = 0;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner663ceeb2008-07-08 17:25:49 +000035static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
36 cl::desc("make the interpreter print every volatile load and store"));
37
Chris Lattner2e42d3a2001-10-15 05:51:48 +000038//===----------------------------------------------------------------------===//
Reid Spencere1aa0662007-03-03 06:22:22 +000039// Various Helper Functions
Chris Lattner39bb5b42001-10-15 13:25:40 +000040//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000041
Reid Spencere1aa0662007-03-03 06:22:22 +000042static inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
Reid Spencera42c7fd2007-01-20 20:12:29 +000043 // Determine if the value is signed or not
44 bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
45 // If its signed, extend the sign bits
46 if (isSigned)
47 Val |= ~ITy->getBitMask();
48 return Val;
49}
50
Chris Lattner39bb5b42001-10-15 13:25:40 +000051static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000052 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +000053}
54
Chris Lattner2e42d3a2001-10-15 05:51:48 +000055void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +000056 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000057}
58
Chris Lattner2adcd832002-05-03 19:52:30 +000059//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000060// Binary Instruction Implementations
61//===----------------------------------------------------------------------===//
62
63#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
Reid Spencerf9536332007-03-06 03:09:31 +000064 case Type::TY##TyID: \
65 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
66 break
Chris Lattner92101ac2001-08-23 17:05:04 +000067
Reid Spencerf9536332007-03-06 03:09:31 +000068#define IMPLEMENT_INTEGER_BINOP1(OP, TY) \
Reid Spencera54b7cb2007-01-12 07:05:14 +000069 case Type::IntegerTyID: { \
Reid Spencerf9536332007-03-06 03:09:31 +000070 Dest.IntVal = Src1.IntVal OP Src2.IntVal; \
Reid Spencera54b7cb2007-01-12 07:05:14 +000071 break; \
72 }
73
Reid Spencera54b7cb2007-01-12 07:05:14 +000074
Reid Spencere1aa0662007-03-03 06:22:22 +000075static void executeAddInst(GenericValue &Dest, GenericValue Src1,
76 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000077 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +000078 IMPLEMENT_INTEGER_BINOP1(+, Ty);
Chris Lattner92101ac2001-08-23 17:05:04 +000079 IMPLEMENT_BINARY_OPERATOR(+, Float);
80 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000081 default:
Bill Wendlinge8156192006-12-07 01:30:32 +000082 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +000083 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +000084 }
Chris Lattner92101ac2001-08-23 17:05:04 +000085}
86
Reid Spencere1aa0662007-03-03 06:22:22 +000087static void executeSubInst(GenericValue &Dest, GenericValue Src1,
88 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000089 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +000090 IMPLEMENT_INTEGER_BINOP1(-, Ty);
Chris Lattner92101ac2001-08-23 17:05:04 +000091 IMPLEMENT_BINARY_OPERATOR(-, Float);
92 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000093 default:
Bill Wendlinge8156192006-12-07 01:30:32 +000094 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +000095 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +000096 }
Chris Lattner92101ac2001-08-23 17:05:04 +000097}
98
Reid Spencere1aa0662007-03-03 06:22:22 +000099static void executeMulInst(GenericValue &Dest, GenericValue Src1,
100 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000101 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000102 IMPLEMENT_INTEGER_BINOP1(*, Ty);
Chris Lattnerc2593162001-10-27 08:28:11 +0000103 IMPLEMENT_BINARY_OPERATOR(*, Float);
104 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000105 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000106 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000107 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000108 }
Chris Lattnerc2593162001-10-27 08:28:11 +0000109}
110
Reid Spencere1aa0662007-03-03 06:22:22 +0000111static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
112 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000113 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000114 IMPLEMENT_BINARY_OPERATOR(/, Float);
115 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000116 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000117 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000118 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000119 }
Chris Lattnerbb76f022001-10-30 20:27:31 +0000120}
121
Reid Spencere1aa0662007-03-03 06:22:22 +0000122static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
123 GenericValue Src2, const Type *Ty) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000124 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000125 case Type::FloatTyID:
126 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
127 break;
128 case Type::DoubleTyID:
129 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
130 break;
131 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000132 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000133 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000134 }
Chris Lattnerc2593162001-10-27 08:28:11 +0000135}
136
Reid Spencerf9536332007-03-06 03:09:31 +0000137#define IMPLEMENT_INTEGER_ICMP(OP, TY) \
138 case Type::IntegerTyID: \
139 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
140 break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000141
Chris Lattnerfd506f52003-04-23 19:55:35 +0000142// Handle pointers specially because they must be compared with only as much
143// width as the host has. We _do not_ want to be comparing 64 bit values when
144// running on a 32-bit target, otherwise the upper 32 bits might mess up
145// comparisons if they contain garbage.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000146#define IMPLEMENT_POINTER_ICMP(OP) \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000147 case Type::PointerTyID: \
Reid Spencerf9536332007-03-06 03:09:31 +0000148 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
149 (void*)(intptr_t)Src2.PointerVal); \
150 break;
Chris Lattnerfd506f52003-04-23 19:55:35 +0000151
Reid Spencere4d87aa2006-12-23 06:05:41 +0000152static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
153 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000154 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000155 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000156 IMPLEMENT_INTEGER_ICMP(eq,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000157 IMPLEMENT_POINTER_ICMP(==);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000158 default:
159 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
160 abort();
161 }
162 return Dest;
163}
164
165static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
166 const Type *Ty) {
167 GenericValue Dest;
168 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000169 IMPLEMENT_INTEGER_ICMP(ne,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000170 IMPLEMENT_POINTER_ICMP(!=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000171 default:
172 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
173 abort();
174 }
175 return Dest;
176}
177
178static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
179 const Type *Ty) {
180 GenericValue Dest;
181 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000182 IMPLEMENT_INTEGER_ICMP(ult,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000183 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000184 default:
185 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
186 abort();
187 }
188 return Dest;
189}
190
191static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
192 const Type *Ty) {
193 GenericValue Dest;
194 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000195 IMPLEMENT_INTEGER_ICMP(slt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000196 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000197 default:
198 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
199 abort();
200 }
201 return Dest;
202}
203
204static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
205 const Type *Ty) {
206 GenericValue Dest;
207 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000208 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000209 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000210 default:
211 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
212 abort();
213 }
214 return Dest;
215}
216
217static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
218 const Type *Ty) {
219 GenericValue Dest;
220 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000221 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000222 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000223 default:
224 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
225 abort();
226 }
227 return Dest;
228}
229
230static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
231 const Type *Ty) {
232 GenericValue Dest;
233 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000234 IMPLEMENT_INTEGER_ICMP(ule,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000235 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000236 default:
237 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
238 abort();
239 }
240 return Dest;
241}
242
243static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
244 const Type *Ty) {
245 GenericValue Dest;
246 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000247 IMPLEMENT_INTEGER_ICMP(sle,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000248 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000249 default:
250 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
251 abort();
252 }
253 return Dest;
254}
255
256static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
257 const Type *Ty) {
258 GenericValue Dest;
259 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000260 IMPLEMENT_INTEGER_ICMP(uge,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000261 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000262 default:
263 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
264 abort();
265 }
266 return Dest;
267}
268
269static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
270 const Type *Ty) {
271 GenericValue Dest;
272 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000273 IMPLEMENT_INTEGER_ICMP(sge,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000274 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000275 default:
276 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
277 abort();
278 }
279 return Dest;
280}
281
Reid Spencere49661b2006-12-31 05:51:36 +0000282void Interpreter::visitICmpInst(ICmpInst &I) {
283 ExecutionContext &SF = ECStack.back();
284 const Type *Ty = I.getOperand(0)->getType();
285 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
286 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
287 GenericValue R; // Result
288
289 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000290 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
291 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
Reid Spencere49661b2006-12-31 05:51:36 +0000292 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
293 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
294 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
295 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
296 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
297 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
298 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
299 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
300 default:
301 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
302 abort();
303 }
304
305 SetValue(&I, R, SF);
306}
307
308#define IMPLEMENT_FCMP(OP, TY) \
Reid Spencerf9536332007-03-06 03:09:31 +0000309 case Type::TY##TyID: \
310 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
311 break
Reid Spencere4d87aa2006-12-23 06:05:41 +0000312
Reid Spencera54b7cb2007-01-12 07:05:14 +0000313static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000314 const Type *Ty) {
315 GenericValue Dest;
316 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000317 IMPLEMENT_FCMP(==, Float);
318 IMPLEMENT_FCMP(==, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000319 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000320 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000321 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000322 }
323 return Dest;
324}
325
Reid Spencera54b7cb2007-01-12 07:05:14 +0000326static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000327 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000328 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000329 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000330 IMPLEMENT_FCMP(!=, Float);
331 IMPLEMENT_FCMP(!=, Double);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000332
Chris Lattner92101ac2001-08-23 17:05:04 +0000333 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000334 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000335 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000336 }
337 return Dest;
338}
339
Reid Spencera54b7cb2007-01-12 07:05:14 +0000340static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000341 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000342 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000343 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000344 IMPLEMENT_FCMP(<=, Float);
345 IMPLEMENT_FCMP(<=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000346 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000347 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000348 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000349 }
350 return Dest;
351}
352
Reid Spencera54b7cb2007-01-12 07:05:14 +0000353static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000354 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000355 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000356 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000357 IMPLEMENT_FCMP(>=, Float);
358 IMPLEMENT_FCMP(>=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000359 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000360 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000361 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000362 }
363 return Dest;
364}
365
Reid Spencera54b7cb2007-01-12 07:05:14 +0000366static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000367 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000368 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000369 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000370 IMPLEMENT_FCMP(<, Float);
371 IMPLEMENT_FCMP(<, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000372 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000373 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000374 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000375 }
376 return Dest;
377}
378
Reid Spencera54b7cb2007-01-12 07:05:14 +0000379static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000380 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000381 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000382 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000383 IMPLEMENT_FCMP(>, Float);
384 IMPLEMENT_FCMP(>, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000385 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000386 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000387 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000388 }
389 return Dest;
390}
391
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000392#define IMPLEMENT_UNORDERED(TY, X,Y) \
393 if (TY == Type::FloatTy) { \
394 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
395 Dest.IntVal = APInt(1,true); \
396 return Dest; \
397 } \
398 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
399 Dest.IntVal = APInt(1,true); \
400 return Dest; \
401 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000402
403
404static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
405 const Type *Ty) {
406 GenericValue Dest;
407 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
408 return executeFCMP_OEQ(Src1, Src2, Ty);
409}
410
411static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
412 const Type *Ty) {
413 GenericValue Dest;
414 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
415 return executeFCMP_ONE(Src1, Src2, Ty);
416}
417
418static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
419 const Type *Ty) {
420 GenericValue Dest;
421 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
422 return executeFCMP_OLE(Src1, Src2, Ty);
423}
424
425static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
426 const Type *Ty) {
427 GenericValue Dest;
428 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
429 return executeFCMP_OGE(Src1, Src2, Ty);
430}
431
432static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
433 const Type *Ty) {
434 GenericValue Dest;
435 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
436 return executeFCMP_OLT(Src1, Src2, Ty);
437}
438
439static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
440 const Type *Ty) {
441 GenericValue Dest;
442 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
443 return executeFCMP_OGT(Src1, Src2, Ty);
444}
445
446static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
447 const Type *Ty) {
448 GenericValue Dest;
449 if (Ty == Type::FloatTy)
Reid Spencerf9536332007-03-06 03:09:31 +0000450 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
451 Src2.FloatVal == Src2.FloatVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000452 else
Reid Spencerf9536332007-03-06 03:09:31 +0000453 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
454 Src2.DoubleVal == Src2.DoubleVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000455 return Dest;
456}
457
458static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
459 const Type *Ty) {
460 GenericValue Dest;
461 if (Ty == Type::FloatTy)
Reid Spencerf9536332007-03-06 03:09:31 +0000462 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
463 Src2.FloatVal != Src2.FloatVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000464 else
Reid Spencerf9536332007-03-06 03:09:31 +0000465 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
466 Src2.DoubleVal != Src2.DoubleVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000467 return Dest;
468}
469
Reid Spencere4d87aa2006-12-23 06:05:41 +0000470void Interpreter::visitFCmpInst(FCmpInst &I) {
471 ExecutionContext &SF = ECStack.back();
472 const Type *Ty = I.getOperand(0)->getType();
473 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
474 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
475 GenericValue R; // Result
476
477 switch (I.getPredicate()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000478 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
479 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000480 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
481 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
482 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
483 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
484 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
485 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
486 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
487 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
488 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
489 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
490 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
491 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
492 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
493 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000494 default:
495 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
496 abort();
497 }
498
499 SetValue(&I, R, SF);
500}
501
Reid Spencere4d87aa2006-12-23 06:05:41 +0000502static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
503 GenericValue Src2, const Type *Ty) {
504 GenericValue Result;
505 switch (predicate) {
506 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
507 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
508 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
509 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
510 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
511 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
512 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
513 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
514 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
515 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000516 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
517 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
518 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
519 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
520 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
521 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
522 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
523 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
524 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
525 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
526 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
527 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
528 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
529 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000530 case FCmpInst::FCMP_FALSE: {
531 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000532 Result.IntVal = APInt(1, false);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000533 return Result;
534 }
535 case FCmpInst::FCMP_TRUE: {
536 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000537 Result.IntVal = APInt(1, true);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000538 return Result;
539 }
540 default:
541 cerr << "Unhandled Cmp predicate\n";
542 abort();
543 }
544}
545
Chris Lattnerd7916e92003-05-10 21:22:39 +0000546void Interpreter::visitBinaryOperator(BinaryOperator &I) {
547 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000548 const Type *Ty = I.getOperand(0)->getType();
549 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
550 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000551 GenericValue R; // Result
552
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000553 switch (I.getOpcode()) {
Reid Spencere1aa0662007-03-03 06:22:22 +0000554 case Instruction::Add: executeAddInst (R, Src1, Src2, Ty); break;
555 case Instruction::Sub: executeSubInst (R, Src1, Src2, Ty); break;
556 case Instruction::Mul: executeMulInst (R, Src1, Src2, Ty); break;
Reid Spencere1aa0662007-03-03 06:22:22 +0000557 case Instruction::FDiv: executeFDivInst (R, Src1, Src2, Ty); break;
Reid Spencere1aa0662007-03-03 06:22:22 +0000558 case Instruction::FRem: executeFRemInst (R, Src1, Src2, Ty); break;
Reid Spencerf9536332007-03-06 03:09:31 +0000559 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
560 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
561 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
562 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
563 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
564 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
565 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000566 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000567 cerr << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner02868352003-04-22 21:22:33 +0000568 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000569 }
570
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000571 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000572}
573
Misha Brukmand1c881a2005-04-21 22:43:08 +0000574static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000575 GenericValue Src3) {
Reid Spencerf9536332007-03-06 03:09:31 +0000576 return Src1.IntVal == 0 ? Src3 : Src2;
Chris Lattner759d34f2004-04-20 16:43:21 +0000577}
578
579void Interpreter::visitSelectInst(SelectInst &I) {
580 ExecutionContext &SF = ECStack.back();
581 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
582 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
583 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
Reid Spencerf9536332007-03-06 03:09:31 +0000584 GenericValue R = executeSelectInst(Src1, Src2, Src3);
Chris Lattner759d34f2004-04-20 16:43:21 +0000585 SetValue(&I, R, SF);
586}
587
588
Chris Lattner92101ac2001-08-23 17:05:04 +0000589//===----------------------------------------------------------------------===//
590// Terminator Instruction Implementations
591//===----------------------------------------------------------------------===//
592
Chris Lattnere43db882001-10-27 04:15:57 +0000593void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000594 // runAtExitHandlers() assumes there are no stack frames, but
595 // if exit() was called, then it had a stack frame. Blow away
596 // the stack before interpreting atexit handlers.
597 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000598 runAtExitHandlers ();
Reid Spencerf9536332007-03-06 03:09:31 +0000599 exit (GV.IntVal.zextOrTrunc(32).getZExtValue());
Chris Lattnere43db882001-10-27 04:15:57 +0000600}
601
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000602/// Pop the last stack frame off of ECStack and then copy the result
603/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000604/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000605/// CallInst if there was a previous stack frame. This method may
606/// invalidate any ECStack iterators you have. This method also takes
607/// care of switching to the normal destination BB, if we are returning
608/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000609///
610void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
611 GenericValue Result) {
612 // Pop the current stack frame.
613 ECStack.pop_back();
614
Misha Brukmand1c881a2005-04-21 22:43:08 +0000615 if (ECStack.empty()) { // Finished main. Put result into exit code...
Chris Lattner42a75512007-01-15 02:27:26 +0000616 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000617 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000618 } else {
Reid Spencere7707872007-06-01 22:23:29 +0000619 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000620 }
621 } else {
622 // If we have a previous stack frame, and we have a previous call,
623 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000624 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000625 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000626 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000627 SetValue(I, Result, CallingSF);
628 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
629 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000630 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000631 }
632 }
633}
634
Chris Lattnerd7916e92003-05-10 21:22:39 +0000635void Interpreter::visitReturnInst(ReturnInst &I) {
636 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000637 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000638 GenericValue Result;
639
640 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000641 if (I.getNumOperands()) {
642 RetTy = I.getReturnValue()->getType();
643 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000644 }
645
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000646 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000647}
648
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000649void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000650 // Unwind stack
651 Instruction *Inst;
652 do {
653 ECStack.pop_back ();
654 if (ECStack.empty ())
655 abort ();
656 Inst = ECStack.back ().Caller.getInstruction ();
657 } while (!(Inst && isa<InvokeInst> (Inst)));
658
659 // Return from invoke
660 ExecutionContext &InvokingSF = ECStack.back ();
661 InvokingSF.Caller = CallSite ();
662
663 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000664 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000665}
666
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000667void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000668 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000669 abort();
670}
671
Chris Lattnerd7916e92003-05-10 21:22:39 +0000672void Interpreter::visitBranchInst(BranchInst &I) {
673 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000674 BasicBlock *Dest;
675
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000676 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
677 if (!I.isUnconditional()) {
678 Value *Cond = I.getCondition();
Reid Spencerf9536332007-03-06 03:09:31 +0000679 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000680 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000681 }
Chris Lattner77113b62003-05-10 20:21:16 +0000682 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000683}
684
Chris Lattnerd7916e92003-05-10 21:22:39 +0000685void Interpreter::visitSwitchInst(SwitchInst &I) {
686 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000687 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
688 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000689
690 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000691 BasicBlock *Dest = 0;
692 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Reid Spencerf9536332007-03-06 03:09:31 +0000693 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
694 .IntVal != 0) {
Chris Lattner09e93922003-04-22 20:34:47 +0000695 Dest = cast<BasicBlock>(I.getOperand(i+1));
696 break;
697 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000698
Chris Lattner09e93922003-04-22 20:34:47 +0000699 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000700 SwitchToNewBasicBlock(Dest, SF);
701}
702
703// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
704// This function handles the actual updating of block and instruction iterators
705// as well as execution of all of the PHI nodes in the destination block.
706//
707// This method does this because all of the PHI nodes must be executed
708// atomically, reading their inputs before any of the results are updated. Not
709// doing this can cause problems if the PHI nodes depend on other PHI nodes for
710// their inputs. If the input PHI node is updated before it is read, incorrect
711// results can happen. Thus we use a two phase approach.
712//
713void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
714 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
715 SF.CurBB = Dest; // Update CurBB to branch destination
716 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
717
718 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
719
720 // Loop over all of the PHI nodes in the current block, reading their inputs.
721 std::vector<GenericValue> ResultValues;
722
723 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
724 // Search for the value corresponding to this previous bb...
725 int i = PN->getBasicBlockIndex(PrevBB);
726 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
727 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000728
Chris Lattner77113b62003-05-10 20:21:16 +0000729 // Save the incoming value for this PHI node...
730 ResultValues.push_back(getOperandValue(IncomingValue, SF));
731 }
732
733 // Now loop over all of the PHI nodes setting their values...
734 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000735 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
736 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000737 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000738 }
Chris Lattner09e93922003-04-22 20:34:47 +0000739}
740
Chris Lattner92101ac2001-08-23 17:05:04 +0000741//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000742// Memory Instruction Implementations
743//===----------------------------------------------------------------------===//
744
Chris Lattnerd7916e92003-05-10 21:22:39 +0000745void Interpreter::visitAllocationInst(AllocationInst &I) {
746 ExecutionContext &SF = ECStack.back();
747
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000748 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000749
Chris Lattnercc82cc12002-04-28 21:57:33 +0000750 // Get the number of elements being allocated by the array...
Reid Spencerf9536332007-03-06 03:09:31 +0000751 unsigned NumElements =
752 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
753
Duncan Sands514ab342007-11-01 20:53:16 +0000754 unsigned TypeSize = (size_t)TD.getABITypeSize(Ty);
Reid Spencerf9536332007-03-06 03:09:31 +0000755
Gabor Greif724441e2007-10-11 19:40:35 +0000756 // Avoid malloc-ing zero bytes, use max()...
757 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
Chris Lattner86660982001-08-27 05:16:50 +0000758
759 // Allocate enough memory to hold the type...
Reid Spencerf9536332007-03-06 03:09:31 +0000760 void *Memory = malloc(MemToAlloc);
761
762 DOUT << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
763 << NumElements << " (Total: " << MemToAlloc << ") at "
Bill Wendling47992ea2007-03-08 23:37:24 +0000764 << uintptr_t(Memory) << '\n';
Chris Lattner9bffa732002-02-19 18:50:09 +0000765
Chris Lattnerfe11a972002-12-23 23:59:41 +0000766 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000767 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000768 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000769
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000770 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000771 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000772}
773
Chris Lattnerd7916e92003-05-10 21:22:39 +0000774void Interpreter::visitFreeInst(FreeInst &I) {
775 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000776 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
777 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000778 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000779 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000780}
781
Chris Lattnera34c5682002-08-27 22:33:45 +0000782// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000783//
Chris Lattner4af6de82003-11-25 20:44:56 +0000784GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000785 gep_type_iterator E,
786 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000787 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000788 "Cannot getElementOffset of a nonpointer type!");
789
Reid Spencerf9536332007-03-06 03:09:31 +0000790 uint64_t Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000791
792 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000793 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000794 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000795
Reid Spencerb83eb642006-10-20 07:07:24 +0000796 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
797 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000798
Reid Spencerf9536332007-03-06 03:09:31 +0000799 Total += SLO->getElementOffset(Index);
Chris Lattner4af6de82003-11-25 20:44:56 +0000800 } else {
801 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000802 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000803 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
804
Reid Spencera54b7cb2007-01-12 07:05:14 +0000805 int64_t Idx;
806 unsigned BitWidth =
807 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
Reid Spencer23e28832007-01-18 01:25:42 +0000808 if (BitWidth == 32)
Reid Spencerf9536332007-03-06 03:09:31 +0000809 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
Chris Lattneraee7f212008-04-06 21:50:58 +0000810 else {
811 assert(BitWidth == 64 && "Invalid index type for getelementptr");
Reid Spencerf9536332007-03-06 03:09:31 +0000812 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
Chris Lattneraee7f212008-04-06 21:50:58 +0000813 }
Duncan Sands514ab342007-11-01 20:53:16 +0000814 Total += TD.getABITypeSize(ST->getElementType())*Idx;
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000815 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000816 }
817
Chris Lattnera34c5682002-08-27 22:33:45 +0000818 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000819 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
820 DOUT << "GEP Index " << Total << " bytes.\n";
Chris Lattnera34c5682002-08-27 22:33:45 +0000821 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000822}
823
Chris Lattnerd7916e92003-05-10 21:22:39 +0000824void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
825 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000826 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000827 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000828}
829
Chris Lattnerd7916e92003-05-10 21:22:39 +0000830void Interpreter::visitLoadInst(LoadInst &I) {
831 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000832 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000833 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Reid Spencere0929362007-03-03 08:38:04 +0000834 GenericValue Result;
Reid Spencere0929362007-03-03 08:38:04 +0000835 LoadValueFromMemory(Result, Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000836 SetValue(&I, Result, SF);
Chris Lattner663ceeb2008-07-08 17:25:49 +0000837 if (I.isVolatile() && PrintVolatile)
838 cerr << "Volatile load " << I;
Chris Lattner86660982001-08-27 05:16:50 +0000839}
840
Chris Lattnerd7916e92003-05-10 21:22:39 +0000841void Interpreter::visitStoreInst(StoreInst &I) {
842 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000843 GenericValue Val = getOperandValue(I.getOperand(0), SF);
844 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000845 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000846 I.getOperand(0)->getType());
Chris Lattner663ceeb2008-07-08 17:25:49 +0000847 if (I.isVolatile() && PrintVolatile)
848 cerr << "Volatile store: " << I;
Chris Lattnerfddc7552002-10-15 20:34:05 +0000849}
850
Chris Lattner86660982001-08-27 05:16:50 +0000851//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000852// Miscellaneous Instruction Implementations
853//===----------------------------------------------------------------------===//
854
Brian Gaekefea483d2003-11-07 20:04:22 +0000855void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000856 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000857
858 // Check to see if this is an intrinsic function call...
Reid Spencer4ccf4622007-04-16 21:50:40 +0000859 Function *F = CS.getCalledFunction();
860 if (F && F->isDeclaration ())
Chris Lattner73011782003-12-28 09:44:37 +0000861 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000862 case Intrinsic::not_intrinsic:
863 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000864 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000865 GenericValue ArgIndex;
866 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
867 ArgIndex.UIntPairVal.second = 0;
868 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000869 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000870 }
Chris Lattner317201d2004-03-13 00:24:00 +0000871 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000872 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000873 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000874 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
875 return;
876 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000877 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000878 // class to transform it into hopefully tasty LLVM code.
879 //
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000880 BasicBlock::iterator me(CS.getInstruction());
Chris Lattner73011782003-12-28 09:44:37 +0000881 BasicBlock *Parent = CS.getInstruction()->getParent();
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000882 bool atBegin(Parent->begin() == me);
883 if (!atBegin)
884 --me;
Chris Lattner73011782003-12-28 09:44:37 +0000885 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
886
887 // Restore the CurInst pointer to the first instruction newly inserted, if
888 // any.
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000889 if (atBegin) {
Chris Lattner73011782003-12-28 09:44:37 +0000890 SF.CurInst = Parent->begin();
891 } else {
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000892 SF.CurInst = me;
Chris Lattner73011782003-12-28 09:44:37 +0000893 ++SF.CurInst;
894 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000895 return;
Chris Lattner73011782003-12-28 09:44:37 +0000896 }
897
Reid Spencer4ccf4622007-04-16 21:50:40 +0000898
Brian Gaekefea483d2003-11-07 20:04:22 +0000899 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000900 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000901 const unsigned NumArgs = SF.Caller.arg_size();
902 ArgVals.reserve(NumArgs);
Reid Spencer4ccf4622007-04-16 21:50:40 +0000903 uint16_t pNum = 1;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000904 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
Reid Spencer4ccf4622007-04-16 21:50:40 +0000905 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
Brian Gaekeae2495a2003-11-07 19:59:08 +0000906 Value *V = *i;
907 ArgVals.push_back(getOperandValue(V, SF));
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000908 // Promote all integral types whose size is < sizeof(i32) into i32.
909 // We do this by zero or sign extending the value as appropriate
910 // according to the parameter attributes
911 const Type *Ty = V->getType();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000912 if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32)) {
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000913 if (CS.paramHasAttr(pNum, ParamAttr::ZExt))
914 ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
915 else if (CS.paramHasAttr(pNum, ParamAttr::SExt))
916 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000917 }
Chris Lattner93780132003-01-13 00:58:52 +0000918 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000919
Misha Brukmand1c881a2005-04-21 22:43:08 +0000920 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000921 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000922 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000923 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000924}
925
Reid Spencer832254e2007-02-02 02:16:23 +0000926void Interpreter::visitShl(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000927 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000928 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
929 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
930 GenericValue Dest;
Reid Spencerf9536332007-03-06 03:09:31 +0000931 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000932 SetValue(&I, Dest, SF);
933}
934
Reid Spencer832254e2007-02-02 02:16:23 +0000935void Interpreter::visitLShr(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000936 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000937 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
938 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
939 GenericValue Dest;
Reid Spencerf9536332007-03-06 03:09:31 +0000940 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
Reid Spencer3822ff52006-11-08 06:47:33 +0000941 SetValue(&I, Dest, SF);
942}
943
Reid Spencer832254e2007-02-02 02:16:23 +0000944void Interpreter::visitAShr(BinaryOperator &I) {
Reid Spencer3822ff52006-11-08 06:47:33 +0000945 ExecutionContext &SF = ECStack.back();
Reid Spencer3822ff52006-11-08 06:47:33 +0000946 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
947 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Reid Spencerf9536332007-03-06 03:09:31 +0000948 GenericValue Dest;
949 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000950 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000951}
952
Reid Spencera54b7cb2007-01-12 07:05:14 +0000953GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
954 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000955 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000956 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000957 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +0000958 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
Chris Lattnera34c5682002-08-27 22:33:45 +0000959 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000960}
Chris Lattner92101ac2001-08-23 17:05:04 +0000961
Reid Spencera54b7cb2007-01-12 07:05:14 +0000962GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
963 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000964 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
965 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000966 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +0000967 Dest.IntVal = Src.IntVal.sext(DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000968 return Dest;
969}
970
971GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
972 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000973 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
974 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000975 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +0000976 Dest.IntVal = Src.IntVal.zext(DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000977 return Dest;
978}
979
980GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
981 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000982 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattneraee7f212008-04-06 21:50:58 +0000983 assert(SrcVal->getType() == Type::DoubleTy && DstTy == Type::FloatTy &&
Reid Spencera54b7cb2007-01-12 07:05:14 +0000984 "Invalid FPTrunc instruction");
985 Dest.FloatVal = (float) Src.DoubleVal;
986 return Dest;
987}
988
989GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
990 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000991 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattneraee7f212008-04-06 21:50:58 +0000992 assert(SrcVal->getType() == Type::FloatTy && DstTy == Type::DoubleTy &&
Reid Spencera54b7cb2007-01-12 07:05:14 +0000993 "Invalid FPTrunc instruction");
994 Dest.DoubleVal = (double) Src.FloatVal;
995 return Dest;
996}
997
998GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
999 ExecutionContext &SF) {
1000 const Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +00001001 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001002 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001003 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001004
Reid Spencera54b7cb2007-01-12 07:05:14 +00001005 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001006 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001007 else
Reid Spencerf9536332007-03-06 03:09:31 +00001008 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001009 return Dest;
1010}
1011
1012GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1013 ExecutionContext &SF) {
1014 const Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +00001015 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001016 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001017 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001018
Reid Spencera54b7cb2007-01-12 07:05:14 +00001019 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001020 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001021 else
Reid Spencerf9536332007-03-06 03:09:31 +00001022 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001023 return Dest;
1024}
1025
1026GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1027 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001028 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001029 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001030
Reid Spencera54b7cb2007-01-12 07:05:14 +00001031 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001032 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001033 else
Reid Spencerf9536332007-03-06 03:09:31 +00001034 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001035 return Dest;
1036}
1037
1038GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1039 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001040 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencer24d6da52007-01-21 00:29:26 +00001041 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001042
Reid Spencera54b7cb2007-01-12 07:05:14 +00001043 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001044 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001045 else
Reid Spencerf9536332007-03-06 03:09:31 +00001046 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001047 return Dest;
Reid Spencerf9536332007-03-06 03:09:31 +00001048
Reid Spencera54b7cb2007-01-12 07:05:14 +00001049}
1050
1051GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1052 ExecutionContext &SF) {
Reid Spencerf9536332007-03-06 03:09:31 +00001053 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001054 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattneraee7f212008-04-06 21:50:58 +00001055 assert(isa<PointerType>(SrcVal->getType()) && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001056
Reid Spencerf9536332007-03-06 03:09:31 +00001057 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001058 return Dest;
1059}
1060
1061GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1062 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001063 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001064 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001065
Reid Spencer57631052007-03-06 03:46:41 +00001066 uint32_t PtrSize = TD.getPointerSizeInBits();
Reid Spencer7553c342007-03-06 03:41:50 +00001067 if (PtrSize != Src.IntVal.getBitWidth())
Reid Spencer57631052007-03-06 03:46:41 +00001068 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
Reid Spencer7553c342007-03-06 03:41:50 +00001069
Reid Spencer6bc63332007-04-26 18:19:35 +00001070 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
Reid Spencera54b7cb2007-01-12 07:05:14 +00001071 return Dest;
1072}
1073
1074GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1075 ExecutionContext &SF) {
1076
1077 const Type *SrcTy = SrcVal->getType();
1078 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1079 if (isa<PointerType>(DstTy)) {
1080 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1081 Dest.PointerVal = Src.PointerVal;
Chris Lattner42a75512007-01-15 02:27:26 +00001082 } else if (DstTy->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001083 if (SrcTy == Type::FloatTy) {
Reid Spencere4b43942007-05-04 03:37:38 +00001084 Dest.IntVal.zext(sizeof(Src.FloatVal) * 8);
Reid Spencerf9536332007-03-06 03:09:31 +00001085 Dest.IntVal.floatToBits(Src.FloatVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001086 } else if (SrcTy == Type::DoubleTy) {
Reid Spencere4b43942007-05-04 03:37:38 +00001087 Dest.IntVal.zext(sizeof(Src.DoubleVal) * 8);
Reid Spencerf9536332007-03-06 03:09:31 +00001088 Dest.IntVal.doubleToBits(Src.DoubleVal);
Chris Lattner42a75512007-01-15 02:27:26 +00001089 } else if (SrcTy->isInteger()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001090 Dest.IntVal = Src.IntVal;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001091 } else
1092 assert(0 && "Invalid BitCast");
1093 } else if (DstTy == Type::FloatTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001094 if (SrcTy->isInteger())
Reid Spencerf9536332007-03-06 03:09:31 +00001095 Dest.FloatVal = Src.IntVal.bitsToFloat();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001096 else
1097 Dest.FloatVal = Src.FloatVal;
1098 } else if (DstTy == Type::DoubleTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001099 if (SrcTy->isInteger())
Reid Spencerf9536332007-03-06 03:09:31 +00001100 Dest.DoubleVal = Src.IntVal.bitsToDouble();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001101 else
1102 Dest.DoubleVal = Src.DoubleVal;
1103 } else
1104 assert(0 && "Invalid Bitcast");
1105
1106 return Dest;
1107}
1108
1109void Interpreter::visitTruncInst(TruncInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001110 ExecutionContext &SF = ECStack.back();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001111 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1112}
1113
1114void Interpreter::visitSExtInst(SExtInst &I) {
1115 ExecutionContext &SF = ECStack.back();
1116 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1117}
1118
1119void Interpreter::visitZExtInst(ZExtInst &I) {
1120 ExecutionContext &SF = ECStack.back();
1121 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1122}
1123
1124void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1125 ExecutionContext &SF = ECStack.back();
1126 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1127}
1128
1129void Interpreter::visitFPExtInst(FPExtInst &I) {
1130 ExecutionContext &SF = ECStack.back();
1131 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1132}
1133
1134void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1135 ExecutionContext &SF = ECStack.back();
1136 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1137}
1138
1139void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1140 ExecutionContext &SF = ECStack.back();
1141 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1142}
1143
1144void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1145 ExecutionContext &SF = ECStack.back();
1146 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1147}
1148
1149void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1150 ExecutionContext &SF = ECStack.back();
1151 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1152}
1153
1154void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1155 ExecutionContext &SF = ECStack.back();
1156 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1157}
1158
1159void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1160 ExecutionContext &SF = ECStack.back();
1161 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1162}
1163
1164void Interpreter::visitBitCastInst(BitCastInst &I) {
1165 ExecutionContext &SF = ECStack.back();
1166 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001167}
Chris Lattner92101ac2001-08-23 17:05:04 +00001168
Brian Gaekec1a2be12003-11-07 21:20:47 +00001169#define IMPLEMENT_VAARG(TY) \
1170 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1171
1172void Interpreter::visitVAArgInst(VAArgInst &I) {
1173 ExecutionContext &SF = ECStack.back();
1174
Brian Gaeke9d20b712004-02-25 23:01:48 +00001175 // Get the incoming valist parameter. LLI treats the valist as a
1176 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001177 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001178 GenericValue Dest;
1179 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Reid Spencerf9536332007-03-06 03:09:31 +00001180 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001181 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001182 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001183 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001184 IMPLEMENT_VAARG(Pointer);
1185 IMPLEMENT_VAARG(Float);
1186 IMPLEMENT_VAARG(Double);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001187 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001188 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Brian Gaekec1a2be12003-11-07 21:20:47 +00001189 abort();
1190 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001191
Brian Gaekec1a2be12003-11-07 21:20:47 +00001192 // Set the Value of this Instruction.
1193 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001194
1195 // Move the pointer to the next vararg.
1196 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001197}
1198
Reid Spencere1aa0662007-03-03 06:22:22 +00001199GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1200 ExecutionContext &SF) {
1201 switch (CE->getOpcode()) {
1202 case Instruction::Trunc:
1203 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1204 case Instruction::ZExt:
1205 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1206 case Instruction::SExt:
1207 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1208 case Instruction::FPTrunc:
1209 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1210 case Instruction::FPExt:
1211 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1212 case Instruction::UIToFP:
1213 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1214 case Instruction::SIToFP:
1215 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1216 case Instruction::FPToUI:
1217 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1218 case Instruction::FPToSI:
1219 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1220 case Instruction::PtrToInt:
1221 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1222 case Instruction::IntToPtr:
1223 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1224 case Instruction::BitCast:
1225 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1226 case Instruction::GetElementPtr:
1227 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1228 gep_type_end(CE), SF);
Reid Spencere0929362007-03-03 08:38:04 +00001229 case Instruction::FCmp:
1230 case Instruction::ICmp:
1231 return executeCmpInst(CE->getPredicate(),
1232 getOperandValue(CE->getOperand(0), SF),
1233 getOperandValue(CE->getOperand(1), SF),
1234 CE->getOperand(0)->getType());
1235 case Instruction::Select:
1236 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1237 getOperandValue(CE->getOperand(1), SF),
1238 getOperandValue(CE->getOperand(2), SF));
Reid Spencere1aa0662007-03-03 06:22:22 +00001239 default :
1240 break;
1241 }
Reid Spencere0929362007-03-03 08:38:04 +00001242
1243 // The cases below here require a GenericValue parameter for the result
1244 // so we initialize one, compute it and then return it.
Reid Spencerf9536332007-03-06 03:09:31 +00001245 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1246 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
Reid Spencere1aa0662007-03-03 06:22:22 +00001247 GenericValue Dest;
Reid Spencerf9536332007-03-06 03:09:31 +00001248 const Type * Ty = CE->getOperand(0)->getType();
Reid Spencere1aa0662007-03-03 06:22:22 +00001249 switch (CE->getOpcode()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001250 case Instruction::Add: executeAddInst (Dest, Op0, Op1, Ty); break;
1251 case Instruction::Sub: executeSubInst (Dest, Op0, Op1, Ty); break;
1252 case Instruction::Mul: executeMulInst (Dest, Op0, Op1, Ty); break;
1253 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1254 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1255 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1256 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1257 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1258 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
1259 case Instruction::And: Dest.IntVal = Op0.IntVal.And(Op1.IntVal); break;
1260 case Instruction::Or: Dest.IntVal = Op0.IntVal.Or(Op1.IntVal); break;
1261 case Instruction::Xor: Dest.IntVal = Op0.IntVal.Xor(Op1.IntVal); break;
1262 case Instruction::Shl:
1263 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1264 break;
1265 case Instruction::LShr:
1266 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1267 break;
1268 case Instruction::AShr:
1269 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1270 break;
Reid Spencere1aa0662007-03-03 06:22:22 +00001271 default:
1272 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
1273 abort();
1274 return GenericValue();
1275 }
Reid Spencere0929362007-03-03 08:38:04 +00001276 return Dest;
Reid Spencere1aa0662007-03-03 06:22:22 +00001277}
1278
1279GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1280 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1281 return getConstantExprValue(CE, SF);
1282 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1283 return getConstantValue(CPV);
1284 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1285 return PTOGV(getPointerToGlobal(GV));
1286 } else {
1287 return SF.Values[V];
1288 }
1289}
1290
Chris Lattner92101ac2001-08-23 17:05:04 +00001291//===----------------------------------------------------------------------===//
1292// Dispatch and Execution Code
1293//===----------------------------------------------------------------------===//
1294
Chris Lattner92101ac2001-08-23 17:05:04 +00001295//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001296// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001297//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001298void Interpreter::callFunction(Function *F,
1299 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001300 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1301 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1302 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001303 // Make a new stack frame... and fill it in.
1304 ECStack.push_back(ExecutionContext());
1305 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001306 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001307
1308 // Special handling for external functions.
Reid Spencer5cbf9852007-01-30 20:08:39 +00001309 if (F->isDeclaration()) {
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001310 GenericValue Result = callExternalFunction (F, ArgVals);
1311 // Simulate a 'ret' instruction of the appropriate type.
1312 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1313 return;
1314 }
1315
1316 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001317 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001318 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001319
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001320 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001321 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001322 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001323 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001324
1325 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001326 unsigned i = 0;
Reid Spencer4ccf4622007-04-16 21:50:40 +00001327 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1328 AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001329 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001330
1331 // Handle varargs arguments...
1332 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001333}
1334
Reid Spencer951418b2007-05-16 02:05:13 +00001335
Chris Lattner92101ac2001-08-23 17:05:04 +00001336void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001337 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001338 // Interpret a single instruction & increment the "PC".
1339 ExecutionContext &SF = ECStack.back(); // Current stack frame
1340 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001341
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001342 // Track the number of dynamic instructions executed.
1343 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001344
Bill Wendling480f0932006-11-27 23:54:50 +00001345 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001346 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner27725bf2007-09-21 18:30:39 +00001347#if 0
1348 // This is not safe, as visiting the instruction could lower it and free I.
Reid Spencer951418b2007-05-16 02:05:13 +00001349#ifndef NDEBUG
1350 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1351 I.getType() != Type::VoidTy) {
1352 DOUT << " --> ";
Chris Lattner27725bf2007-09-21 18:30:39 +00001353 const GenericValue &Val = SF.Values[&I];
1354 switch (I.getType()->getTypeID()) {
1355 default: assert(0 && "Invalid GenericValue Type");
1356 case Type::VoidTyID: DOUT << "void"; break;
1357 case Type::FloatTyID: DOUT << "float " << Val.FloatVal; break;
1358 case Type::DoubleTyID: DOUT << "double " << Val.DoubleVal; break;
1359 case Type::PointerTyID: DOUT << "void* " << intptr_t(Val.PointerVal);
1360 break;
1361 case Type::IntegerTyID:
1362 DOUT << "i" << Val.IntVal.getBitWidth() << " "
1363 << Val.IntVal.toStringUnsigned(10)
1364 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
1365 break;
1366 }
Reid Spencer951418b2007-05-16 02:05:13 +00001367 }
1368#endif
Chris Lattner27725bf2007-09-21 18:30:39 +00001369#endif
Chris Lattner92101ac2001-08-23 17:05:04 +00001370 }
1371}