blob: f11cf816b22bb4dbbe5e902513b8475fd79ffd4b [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"
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"
24#include "llvm/Support/Debug.h"
Reid Spencera54b7cb2007-01-12 07:05:14 +000025#include "llvm/Support/MathExtras.h"
Jeff Cohen97af7512006-12-02 02:22:01 +000026#include <cmath>
Gabor Greif724441e2007-10-11 19:40:35 +000027#include <algorithm>
Chris Lattner4af6de82003-11-25 20:44:56 +000028using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000029
Chris Lattnercecf56b2006-12-19 22:56:53 +000030STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
31static Interpreter *TheEE = 0;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner2e42d3a2001-10-15 05:51:48 +000033//===----------------------------------------------------------------------===//
Reid Spencere1aa0662007-03-03 06:22:22 +000034// Various Helper Functions
Chris Lattner39bb5b42001-10-15 13:25:40 +000035//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000036
Reid Spencere1aa0662007-03-03 06:22:22 +000037static inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
Reid Spencera42c7fd2007-01-20 20:12:29 +000038 // Determine if the value is signed or not
39 bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
40 // If its signed, extend the sign bits
41 if (isSigned)
42 Val |= ~ITy->getBitMask();
43 return Val;
44}
45
Chris Lattner39bb5b42001-10-15 13:25:40 +000046static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000047 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +000048}
49
Chris Lattner2e42d3a2001-10-15 05:51:48 +000050void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +000051 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000052}
53
Chris Lattner2adcd832002-05-03 19:52:30 +000054//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000055// Binary Instruction Implementations
56//===----------------------------------------------------------------------===//
57
58#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
Reid Spencerf9536332007-03-06 03:09:31 +000059 case Type::TY##TyID: \
60 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
61 break
Chris Lattner92101ac2001-08-23 17:05:04 +000062
Reid Spencerf9536332007-03-06 03:09:31 +000063#define IMPLEMENT_INTEGER_BINOP1(OP, TY) \
Reid Spencera54b7cb2007-01-12 07:05:14 +000064 case Type::IntegerTyID: { \
Reid Spencerf9536332007-03-06 03:09:31 +000065 Dest.IntVal = Src1.IntVal OP Src2.IntVal; \
Reid Spencera54b7cb2007-01-12 07:05:14 +000066 break; \
67 }
68
Reid Spencera54b7cb2007-01-12 07:05:14 +000069
Reid Spencere1aa0662007-03-03 06:22:22 +000070static void executeAddInst(GenericValue &Dest, GenericValue Src1,
71 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000072 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +000073 IMPLEMENT_INTEGER_BINOP1(+, Ty);
Chris Lattner92101ac2001-08-23 17:05:04 +000074 IMPLEMENT_BINARY_OPERATOR(+, Float);
75 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000076 default:
Bill Wendlinge8156192006-12-07 01:30:32 +000077 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +000078 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +000079 }
Chris Lattner92101ac2001-08-23 17:05:04 +000080}
81
Reid Spencere1aa0662007-03-03 06:22:22 +000082static void executeSubInst(GenericValue &Dest, GenericValue Src1,
83 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000084 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +000085 IMPLEMENT_INTEGER_BINOP1(-, Ty);
Chris Lattner92101ac2001-08-23 17:05:04 +000086 IMPLEMENT_BINARY_OPERATOR(-, Float);
87 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000088 default:
Bill Wendlinge8156192006-12-07 01:30:32 +000089 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +000090 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +000091 }
Chris Lattner92101ac2001-08-23 17:05:04 +000092}
93
Reid Spencere1aa0662007-03-03 06:22:22 +000094static void executeMulInst(GenericValue &Dest, GenericValue Src1,
95 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000096 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +000097 IMPLEMENT_INTEGER_BINOP1(*, Ty);
Chris Lattnerc2593162001-10-27 08:28:11 +000098 IMPLEMENT_BINARY_OPERATOR(*, Float);
99 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000100 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000101 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000102 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000103 }
Chris Lattnerc2593162001-10-27 08:28:11 +0000104}
105
Reid Spencere1aa0662007-03-03 06:22:22 +0000106static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
107 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000108 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000109 IMPLEMENT_BINARY_OPERATOR(/, Float);
110 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000111 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000112 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000113 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000114 }
Chris Lattnerbb76f022001-10-30 20:27:31 +0000115}
116
Reid Spencere1aa0662007-03-03 06:22:22 +0000117static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
118 GenericValue Src2, const Type *Ty) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000119 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000120 case Type::FloatTyID:
121 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
122 break;
123 case Type::DoubleTyID:
124 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
125 break;
126 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000127 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000128 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000129 }
Chris Lattnerc2593162001-10-27 08:28:11 +0000130}
131
Reid Spencerf9536332007-03-06 03:09:31 +0000132#define IMPLEMENT_INTEGER_ICMP(OP, TY) \
133 case Type::IntegerTyID: \
134 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
135 break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000136
Chris Lattnerfd506f52003-04-23 19:55:35 +0000137// Handle pointers specially because they must be compared with only as much
138// width as the host has. We _do not_ want to be comparing 64 bit values when
139// running on a 32-bit target, otherwise the upper 32 bits might mess up
140// comparisons if they contain garbage.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000141#define IMPLEMENT_POINTER_ICMP(OP) \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000142 case Type::PointerTyID: \
Reid Spencerf9536332007-03-06 03:09:31 +0000143 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
144 (void*)(intptr_t)Src2.PointerVal); \
145 break;
Chris Lattnerfd506f52003-04-23 19:55:35 +0000146
Reid Spencere4d87aa2006-12-23 06:05:41 +0000147static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
148 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000149 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000150 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000151 IMPLEMENT_INTEGER_ICMP(eq,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000152 IMPLEMENT_POINTER_ICMP(==);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000153 default:
154 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
155 abort();
156 }
157 return Dest;
158}
159
160static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
161 const Type *Ty) {
162 GenericValue Dest;
163 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000164 IMPLEMENT_INTEGER_ICMP(ne,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000165 IMPLEMENT_POINTER_ICMP(!=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000166 default:
167 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
168 abort();
169 }
170 return Dest;
171}
172
173static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
174 const Type *Ty) {
175 GenericValue Dest;
176 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000177 IMPLEMENT_INTEGER_ICMP(ult,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000178 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000179 default:
180 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
181 abort();
182 }
183 return Dest;
184}
185
186static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
187 const Type *Ty) {
188 GenericValue Dest;
189 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000190 IMPLEMENT_INTEGER_ICMP(slt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000191 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000192 default:
193 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
194 abort();
195 }
196 return Dest;
197}
198
199static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
200 const Type *Ty) {
201 GenericValue Dest;
202 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000203 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000204 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000205 default:
206 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
207 abort();
208 }
209 return Dest;
210}
211
212static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
213 const Type *Ty) {
214 GenericValue Dest;
215 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000216 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000217 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000218 default:
219 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
220 abort();
221 }
222 return Dest;
223}
224
225static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
226 const Type *Ty) {
227 GenericValue Dest;
228 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000229 IMPLEMENT_INTEGER_ICMP(ule,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000230 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000231 default:
232 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
233 abort();
234 }
235 return Dest;
236}
237
238static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
239 const Type *Ty) {
240 GenericValue Dest;
241 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000242 IMPLEMENT_INTEGER_ICMP(sle,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000243 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000244 default:
245 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
246 abort();
247 }
248 return Dest;
249}
250
251static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
252 const Type *Ty) {
253 GenericValue Dest;
254 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000255 IMPLEMENT_INTEGER_ICMP(uge,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000256 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000257 default:
258 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
259 abort();
260 }
261 return Dest;
262}
263
264static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
265 const Type *Ty) {
266 GenericValue Dest;
267 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000268 IMPLEMENT_INTEGER_ICMP(sge,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000269 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000270 default:
271 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
272 abort();
273 }
274 return Dest;
275}
276
Reid Spencere49661b2006-12-31 05:51:36 +0000277void Interpreter::visitICmpInst(ICmpInst &I) {
278 ExecutionContext &SF = ECStack.back();
279 const Type *Ty = I.getOperand(0)->getType();
280 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
281 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
282 GenericValue R; // Result
283
284 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000285 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
286 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
Reid Spencere49661b2006-12-31 05:51:36 +0000287 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
288 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
289 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
290 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
291 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
292 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
293 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
294 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
295 default:
296 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
297 abort();
298 }
299
300 SetValue(&I, R, SF);
301}
302
303#define IMPLEMENT_FCMP(OP, TY) \
Reid Spencerf9536332007-03-06 03:09:31 +0000304 case Type::TY##TyID: \
305 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
306 break
Reid Spencere4d87aa2006-12-23 06:05:41 +0000307
Reid Spencera54b7cb2007-01-12 07:05:14 +0000308static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000309 const Type *Ty) {
310 GenericValue Dest;
311 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000312 IMPLEMENT_FCMP(==, Float);
313 IMPLEMENT_FCMP(==, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000314 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000315 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000316 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000317 }
318 return Dest;
319}
320
Reid Spencera54b7cb2007-01-12 07:05:14 +0000321static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000322 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000323 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000324 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000325 IMPLEMENT_FCMP(!=, Float);
326 IMPLEMENT_FCMP(!=, Double);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000327
Chris Lattner92101ac2001-08-23 17:05:04 +0000328 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000329 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000330 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000331 }
332 return Dest;
333}
334
Reid Spencera54b7cb2007-01-12 07:05:14 +0000335static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000336 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000337 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000338 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000339 IMPLEMENT_FCMP(<=, Float);
340 IMPLEMENT_FCMP(<=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000341 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000342 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000343 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000344 }
345 return Dest;
346}
347
Reid Spencera54b7cb2007-01-12 07:05:14 +0000348static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000349 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000350 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000351 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000352 IMPLEMENT_FCMP(>=, Float);
353 IMPLEMENT_FCMP(>=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000354 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000355 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000356 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000357 }
358 return Dest;
359}
360
Reid Spencera54b7cb2007-01-12 07:05:14 +0000361static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000362 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000363 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000364 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000365 IMPLEMENT_FCMP(<, Float);
366 IMPLEMENT_FCMP(<, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000367 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000368 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000369 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000370 }
371 return Dest;
372}
373
Reid Spencera54b7cb2007-01-12 07:05:14 +0000374static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000375 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000376 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000377 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000378 IMPLEMENT_FCMP(>, Float);
379 IMPLEMENT_FCMP(>, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000380 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000381 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000382 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000383 }
384 return Dest;
385}
386
Reid Spencera54b7cb2007-01-12 07:05:14 +0000387#define IMPLEMENT_UNORDERED(TY, X,Y) \
388 if (TY == Type::FloatTy) \
389 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
Reid Spencerf9536332007-03-06 03:09:31 +0000390 Dest.IntVal = APInt(1,true); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000391 return Dest; \
392 } \
393 else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
Reid Spencerf9536332007-03-06 03:09:31 +0000394 Dest.IntVal = APInt(1,true); \
Reid Spencera54b7cb2007-01-12 07:05:14 +0000395 return Dest; \
396 }
397
398
399static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
400 const Type *Ty) {
401 GenericValue Dest;
402 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
403 return executeFCMP_OEQ(Src1, Src2, Ty);
404}
405
406static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
407 const Type *Ty) {
408 GenericValue Dest;
409 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
410 return executeFCMP_ONE(Src1, Src2, Ty);
411}
412
413static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
414 const Type *Ty) {
415 GenericValue Dest;
416 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
417 return executeFCMP_OLE(Src1, Src2, Ty);
418}
419
420static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
421 const Type *Ty) {
422 GenericValue Dest;
423 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
424 return executeFCMP_OGE(Src1, Src2, Ty);
425}
426
427static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
428 const Type *Ty) {
429 GenericValue Dest;
430 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
431 return executeFCMP_OLT(Src1, Src2, Ty);
432}
433
434static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
435 const Type *Ty) {
436 GenericValue Dest;
437 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
438 return executeFCMP_OGT(Src1, Src2, Ty);
439}
440
441static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
442 const Type *Ty) {
443 GenericValue Dest;
444 if (Ty == Type::FloatTy)
Reid Spencerf9536332007-03-06 03:09:31 +0000445 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
446 Src2.FloatVal == Src2.FloatVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000447 else
Reid Spencerf9536332007-03-06 03:09:31 +0000448 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
449 Src2.DoubleVal == Src2.DoubleVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000450 return Dest;
451}
452
453static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
454 const Type *Ty) {
455 GenericValue Dest;
456 if (Ty == Type::FloatTy)
Reid Spencerf9536332007-03-06 03:09:31 +0000457 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
458 Src2.FloatVal != Src2.FloatVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000459 else
Reid Spencerf9536332007-03-06 03:09:31 +0000460 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
461 Src2.DoubleVal != Src2.DoubleVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000462 return Dest;
463}
464
Reid Spencere4d87aa2006-12-23 06:05:41 +0000465void Interpreter::visitFCmpInst(FCmpInst &I) {
466 ExecutionContext &SF = ECStack.back();
467 const Type *Ty = I.getOperand(0)->getType();
468 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
469 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
470 GenericValue R; // Result
471
472 switch (I.getPredicate()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000473 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
474 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000475 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
476 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
477 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
478 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
479 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
480 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
481 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
482 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
483 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
484 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
485 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
486 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
487 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
488 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000489 default:
490 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
491 abort();
492 }
493
494 SetValue(&I, R, SF);
495}
496
Reid Spencere4d87aa2006-12-23 06:05:41 +0000497static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
498 GenericValue Src2, const Type *Ty) {
499 GenericValue Result;
500 switch (predicate) {
501 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
502 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
503 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
504 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
505 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
506 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
507 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
508 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
509 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
510 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000511 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
512 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
513 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
514 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
515 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
516 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
517 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
518 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
519 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
520 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
521 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
522 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
523 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
524 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000525 case FCmpInst::FCMP_FALSE: {
526 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000527 Result.IntVal = APInt(1, false);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000528 return Result;
529 }
530 case FCmpInst::FCMP_TRUE: {
531 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000532 Result.IntVal = APInt(1, true);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000533 return Result;
534 }
535 default:
536 cerr << "Unhandled Cmp predicate\n";
537 abort();
538 }
539}
540
Chris Lattnerd7916e92003-05-10 21:22:39 +0000541void Interpreter::visitBinaryOperator(BinaryOperator &I) {
542 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000543 const Type *Ty = I.getOperand(0)->getType();
544 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
545 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000546 GenericValue R; // Result
547
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000548 switch (I.getOpcode()) {
Reid Spencere1aa0662007-03-03 06:22:22 +0000549 case Instruction::Add: executeAddInst (R, Src1, Src2, Ty); break;
550 case Instruction::Sub: executeSubInst (R, Src1, Src2, Ty); break;
551 case Instruction::Mul: executeMulInst (R, Src1, Src2, Ty); break;
Reid Spencere1aa0662007-03-03 06:22:22 +0000552 case Instruction::FDiv: executeFDivInst (R, Src1, Src2, Ty); break;
Reid Spencere1aa0662007-03-03 06:22:22 +0000553 case Instruction::FRem: executeFRemInst (R, Src1, Src2, Ty); break;
Reid Spencerf9536332007-03-06 03:09:31 +0000554 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
555 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
556 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
557 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
558 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
559 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
560 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000561 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000562 cerr << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner02868352003-04-22 21:22:33 +0000563 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000564 }
565
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000566 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000567}
568
Misha Brukmand1c881a2005-04-21 22:43:08 +0000569static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000570 GenericValue Src3) {
Reid Spencerf9536332007-03-06 03:09:31 +0000571 return Src1.IntVal == 0 ? Src3 : Src2;
Chris Lattner759d34f2004-04-20 16:43:21 +0000572}
573
574void Interpreter::visitSelectInst(SelectInst &I) {
575 ExecutionContext &SF = ECStack.back();
576 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
577 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
578 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
Reid Spencerf9536332007-03-06 03:09:31 +0000579 GenericValue R = executeSelectInst(Src1, Src2, Src3);
Chris Lattner759d34f2004-04-20 16:43:21 +0000580 SetValue(&I, R, SF);
581}
582
583
Chris Lattner92101ac2001-08-23 17:05:04 +0000584//===----------------------------------------------------------------------===//
585// Terminator Instruction Implementations
586//===----------------------------------------------------------------------===//
587
Chris Lattnere43db882001-10-27 04:15:57 +0000588void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000589 // runAtExitHandlers() assumes there are no stack frames, but
590 // if exit() was called, then it had a stack frame. Blow away
591 // the stack before interpreting atexit handlers.
592 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000593 runAtExitHandlers ();
Reid Spencerf9536332007-03-06 03:09:31 +0000594 exit (GV.IntVal.zextOrTrunc(32).getZExtValue());
Chris Lattnere43db882001-10-27 04:15:57 +0000595}
596
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000597/// Pop the last stack frame off of ECStack and then copy the result
598/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000599/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000600/// CallInst if there was a previous stack frame. This method may
601/// invalidate any ECStack iterators you have. This method also takes
602/// care of switching to the normal destination BB, if we are returning
603/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000604///
605void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
606 GenericValue Result) {
607 // Pop the current stack frame.
608 ECStack.pop_back();
609
Misha Brukmand1c881a2005-04-21 22:43:08 +0000610 if (ECStack.empty()) { // Finished main. Put result into exit code...
Chris Lattner42a75512007-01-15 02:27:26 +0000611 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000612 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000613 } else {
Reid Spencere7707872007-06-01 22:23:29 +0000614 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000615 }
616 } else {
617 // If we have a previous stack frame, and we have a previous call,
618 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000619 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000620 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000621 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000622 SetValue(I, Result, CallingSF);
623 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
624 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000625 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000626 }
627 }
628}
629
Chris Lattnerd7916e92003-05-10 21:22:39 +0000630void Interpreter::visitReturnInst(ReturnInst &I) {
631 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000632 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000633 GenericValue Result;
634
635 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000636 if (I.getNumOperands()) {
637 RetTy = I.getReturnValue()->getType();
638 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000639 }
640
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000641 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000642}
643
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000644void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000645 // Unwind stack
646 Instruction *Inst;
647 do {
648 ECStack.pop_back ();
649 if (ECStack.empty ())
650 abort ();
651 Inst = ECStack.back ().Caller.getInstruction ();
652 } while (!(Inst && isa<InvokeInst> (Inst)));
653
654 // Return from invoke
655 ExecutionContext &InvokingSF = ECStack.back ();
656 InvokingSF.Caller = CallSite ();
657
658 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000659 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000660}
661
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000662void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000663 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000664 abort();
665}
666
Chris Lattnerd7916e92003-05-10 21:22:39 +0000667void Interpreter::visitBranchInst(BranchInst &I) {
668 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000669 BasicBlock *Dest;
670
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000671 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
672 if (!I.isUnconditional()) {
673 Value *Cond = I.getCondition();
Reid Spencerf9536332007-03-06 03:09:31 +0000674 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000675 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000676 }
Chris Lattner77113b62003-05-10 20:21:16 +0000677 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000678}
679
Chris Lattnerd7916e92003-05-10 21:22:39 +0000680void Interpreter::visitSwitchInst(SwitchInst &I) {
681 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000682 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
683 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000684
685 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000686 BasicBlock *Dest = 0;
687 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Reid Spencerf9536332007-03-06 03:09:31 +0000688 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
689 .IntVal != 0) {
Chris Lattner09e93922003-04-22 20:34:47 +0000690 Dest = cast<BasicBlock>(I.getOperand(i+1));
691 break;
692 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000693
Chris Lattner09e93922003-04-22 20:34:47 +0000694 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000695 SwitchToNewBasicBlock(Dest, SF);
696}
697
698// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
699// This function handles the actual updating of block and instruction iterators
700// as well as execution of all of the PHI nodes in the destination block.
701//
702// This method does this because all of the PHI nodes must be executed
703// atomically, reading their inputs before any of the results are updated. Not
704// doing this can cause problems if the PHI nodes depend on other PHI nodes for
705// their inputs. If the input PHI node is updated before it is read, incorrect
706// results can happen. Thus we use a two phase approach.
707//
708void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
709 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
710 SF.CurBB = Dest; // Update CurBB to branch destination
711 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
712
713 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
714
715 // Loop over all of the PHI nodes in the current block, reading their inputs.
716 std::vector<GenericValue> ResultValues;
717
718 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
719 // Search for the value corresponding to this previous bb...
720 int i = PN->getBasicBlockIndex(PrevBB);
721 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
722 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000723
Chris Lattner77113b62003-05-10 20:21:16 +0000724 // Save the incoming value for this PHI node...
725 ResultValues.push_back(getOperandValue(IncomingValue, SF));
726 }
727
728 // Now loop over all of the PHI nodes setting their values...
729 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000730 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
731 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000732 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000733 }
Chris Lattner09e93922003-04-22 20:34:47 +0000734}
735
Chris Lattner92101ac2001-08-23 17:05:04 +0000736//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000737// Memory Instruction Implementations
738//===----------------------------------------------------------------------===//
739
Chris Lattnerd7916e92003-05-10 21:22:39 +0000740void Interpreter::visitAllocationInst(AllocationInst &I) {
741 ExecutionContext &SF = ECStack.back();
742
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000743 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000744
Chris Lattnercc82cc12002-04-28 21:57:33 +0000745 // Get the number of elements being allocated by the array...
Reid Spencerf9536332007-03-06 03:09:31 +0000746 unsigned NumElements =
747 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
748
749 unsigned TypeSize = (size_t)TD.getTypeSize(Ty);
750
Gabor Greif724441e2007-10-11 19:40:35 +0000751 // Avoid malloc-ing zero bytes, use max()...
752 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
Chris Lattner86660982001-08-27 05:16:50 +0000753
754 // Allocate enough memory to hold the type...
Reid Spencerf9536332007-03-06 03:09:31 +0000755 void *Memory = malloc(MemToAlloc);
756
757 DOUT << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
758 << NumElements << " (Total: " << MemToAlloc << ") at "
Bill Wendling47992ea2007-03-08 23:37:24 +0000759 << uintptr_t(Memory) << '\n';
Chris Lattner9bffa732002-02-19 18:50:09 +0000760
Chris Lattnerfe11a972002-12-23 23:59:41 +0000761 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000762 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000763 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000764
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000765 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000766 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000767}
768
Chris Lattnerd7916e92003-05-10 21:22:39 +0000769void Interpreter::visitFreeInst(FreeInst &I) {
770 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000771 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
772 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000773 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000774 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000775}
776
Chris Lattnera34c5682002-08-27 22:33:45 +0000777// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000778//
Chris Lattner4af6de82003-11-25 20:44:56 +0000779GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000780 gep_type_iterator E,
781 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000782 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000783 "Cannot getElementOffset of a nonpointer type!");
784
Reid Spencerf9536332007-03-06 03:09:31 +0000785 uint64_t Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000786
787 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000788 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000789 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000790
Reid Spencerb83eb642006-10-20 07:07:24 +0000791 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
792 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000793
Reid Spencerf9536332007-03-06 03:09:31 +0000794 Total += SLO->getElementOffset(Index);
Chris Lattner4af6de82003-11-25 20:44:56 +0000795 } else {
796 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000797 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000798 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
799
Reid Spencera54b7cb2007-01-12 07:05:14 +0000800 int64_t Idx;
801 unsigned BitWidth =
802 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
Reid Spencer23e28832007-01-18 01:25:42 +0000803 if (BitWidth == 32)
Reid Spencerf9536332007-03-06 03:09:31 +0000804 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
Reid Spencer23e28832007-01-18 01:25:42 +0000805 else if (BitWidth == 64)
Reid Spencerf9536332007-03-06 03:09:31 +0000806 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000807 else
Reid Spencer23e28832007-01-18 01:25:42 +0000808 assert(0 && "Invalid index type for getelementptr");
Reid Spencerf9536332007-03-06 03:09:31 +0000809 Total += TD.getTypeSize(ST->getElementType())*Idx;
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000810 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000811 }
812
Chris Lattnera34c5682002-08-27 22:33:45 +0000813 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000814 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
815 DOUT << "GEP Index " << Total << " bytes.\n";
Chris Lattnera34c5682002-08-27 22:33:45 +0000816 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000817}
818
Chris Lattnerd7916e92003-05-10 21:22:39 +0000819void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
820 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000821 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000822 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000823}
824
Chris Lattnerd7916e92003-05-10 21:22:39 +0000825void Interpreter::visitLoadInst(LoadInst &I) {
826 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000827 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000828 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Reid Spencere0929362007-03-03 08:38:04 +0000829 GenericValue Result;
Reid Spencere0929362007-03-03 08:38:04 +0000830 LoadValueFromMemory(Result, Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000831 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000832}
833
Chris Lattnerd7916e92003-05-10 21:22:39 +0000834void Interpreter::visitStoreInst(StoreInst &I) {
835 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000836 GenericValue Val = getOperandValue(I.getOperand(0), SF);
837 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000838 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000839 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000840}
841
Chris Lattner86660982001-08-27 05:16:50 +0000842//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000843// Miscellaneous Instruction Implementations
844//===----------------------------------------------------------------------===//
845
Brian Gaekefea483d2003-11-07 20:04:22 +0000846void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000847 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000848
849 // Check to see if this is an intrinsic function call...
Reid Spencer4ccf4622007-04-16 21:50:40 +0000850 Function *F = CS.getCalledFunction();
851 if (F && F->isDeclaration ())
Chris Lattner73011782003-12-28 09:44:37 +0000852 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000853 case Intrinsic::not_intrinsic:
854 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000855 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000856 GenericValue ArgIndex;
857 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
858 ArgIndex.UIntPairVal.second = 0;
859 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000860 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000861 }
Chris Lattner317201d2004-03-13 00:24:00 +0000862 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000863 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000864 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000865 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
866 return;
867 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000868 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000869 // class to transform it into hopefully tasty LLVM code.
870 //
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000871 BasicBlock::iterator me(CS.getInstruction());
Chris Lattner73011782003-12-28 09:44:37 +0000872 BasicBlock *Parent = CS.getInstruction()->getParent();
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000873 bool atBegin(Parent->begin() == me);
874 if (!atBegin)
875 --me;
Chris Lattner73011782003-12-28 09:44:37 +0000876 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
877
878 // Restore the CurInst pointer to the first instruction newly inserted, if
879 // any.
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000880 if (atBegin) {
Chris Lattner73011782003-12-28 09:44:37 +0000881 SF.CurInst = Parent->begin();
882 } else {
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000883 SF.CurInst = me;
Chris Lattner73011782003-12-28 09:44:37 +0000884 ++SF.CurInst;
885 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000886 return;
Chris Lattner73011782003-12-28 09:44:37 +0000887 }
888
Reid Spencer4ccf4622007-04-16 21:50:40 +0000889
Brian Gaekefea483d2003-11-07 20:04:22 +0000890 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000891 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000892 const unsigned NumArgs = SF.Caller.arg_size();
893 ArgVals.reserve(NumArgs);
Reid Spencer4ccf4622007-04-16 21:50:40 +0000894 uint16_t pNum = 1;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000895 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
Reid Spencer4ccf4622007-04-16 21:50:40 +0000896 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
Brian Gaekeae2495a2003-11-07 19:59:08 +0000897 Value *V = *i;
898 ArgVals.push_back(getOperandValue(V, SF));
Reid Spencer4ccf4622007-04-16 21:50:40 +0000899 if (F) {
900 // Promote all integral types whose size is < sizeof(i32) into i32.
901 // We do this by zero or sign extending the value as appropriate
902 // according to the parameter attributes
903 const Type *Ty = V->getType();
904 if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32))
905 if (const ParamAttrsList *PA = F->getParamAttrs())
906 if (PA->paramHasAttr(pNum, ParamAttr::ZExt))
907 ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
908 else if (PA->paramHasAttr(pNum, ParamAttr::SExt))
909 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
910 }
Chris Lattner93780132003-01-13 00:58:52 +0000911 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000912
Misha Brukmand1c881a2005-04-21 22:43:08 +0000913 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000914 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000915 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000916 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000917}
918
Reid Spencer832254e2007-02-02 02:16:23 +0000919void Interpreter::visitShl(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000920 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000921 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
922 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
923 GenericValue Dest;
Reid Spencerf9536332007-03-06 03:09:31 +0000924 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000925 SetValue(&I, Dest, SF);
926}
927
Reid Spencer832254e2007-02-02 02:16:23 +0000928void Interpreter::visitLShr(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000929 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000930 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
931 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
932 GenericValue Dest;
Reid Spencerf9536332007-03-06 03:09:31 +0000933 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
Reid Spencer3822ff52006-11-08 06:47:33 +0000934 SetValue(&I, Dest, SF);
935}
936
Reid Spencer832254e2007-02-02 02:16:23 +0000937void Interpreter::visitAShr(BinaryOperator &I) {
Reid Spencer3822ff52006-11-08 06:47:33 +0000938 ExecutionContext &SF = ECStack.back();
Reid Spencer3822ff52006-11-08 06:47:33 +0000939 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
940 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Reid Spencerf9536332007-03-06 03:09:31 +0000941 GenericValue Dest;
942 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000943 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000944}
945
Reid Spencera54b7cb2007-01-12 07:05:14 +0000946GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
947 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000948 const Type *SrcTy = SrcVal->getType();
949 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000950 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 truncate");
Reid Spencerf9536332007-03-06 03:09:31 +0000955 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
Chris Lattnera34c5682002-08-27 22:33:45 +0000956 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000957}
Chris Lattner92101ac2001-08-23 17:05:04 +0000958
Reid Spencera54b7cb2007-01-12 07:05:14 +0000959GenericValue Interpreter::executeSExtInst(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.sext(DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000969 return Dest;
970}
971
972GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
973 ExecutionContext &SF) {
974 const Type *SrcTy = SrcVal->getType();
975 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
976 const IntegerType *DITy = cast<IntegerType>(DstTy);
977 const IntegerType *SITy = cast<IntegerType>(SrcTy);
978 unsigned DBitWidth = DITy->getBitWidth();
979 unsigned SBitWidth = SITy->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000980 assert(SBitWidth < DBitWidth && "Invalid sign extend");
Reid Spencerf9536332007-03-06 03:09:31 +0000981 Dest.IntVal = Src.IntVal.zext(DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000982 return Dest;
983}
984
985GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
986 ExecutionContext &SF) {
987 const Type *SrcTy = SrcVal->getType();
988 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
989 assert(SrcTy == Type::DoubleTy && DstTy == Type::FloatTy &&
990 "Invalid FPTrunc instruction");
991 Dest.FloatVal = (float) Src.DoubleVal;
992 return Dest;
993}
994
995GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
996 ExecutionContext &SF) {
997 const Type *SrcTy = SrcVal->getType();
998 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
999 assert(SrcTy == Type::FloatTy && DstTy == Type::DoubleTy &&
1000 "Invalid FPTrunc instruction");
1001 Dest.DoubleVal = (double) Src.FloatVal;
1002 return Dest;
1003}
1004
1005GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
1006 ExecutionContext &SF) {
1007 const Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +00001008 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001009 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001010 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001011
Reid Spencera54b7cb2007-01-12 07:05:14 +00001012 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001013 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001014 else
Reid Spencerf9536332007-03-06 03:09:31 +00001015 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001016 return Dest;
1017}
1018
1019GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1020 ExecutionContext &SF) {
1021 const Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +00001022 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001023 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001024 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001025
Reid Spencera54b7cb2007-01-12 07:05:14 +00001026 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001027 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001028 else
Reid Spencerf9536332007-03-06 03:09:31 +00001029 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001030 return Dest;
1031}
1032
1033GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1034 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001035 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001036 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001037
Reid Spencera54b7cb2007-01-12 07:05:14 +00001038 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001039 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001040 else
Reid Spencerf9536332007-03-06 03:09:31 +00001041 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001042 return Dest;
1043}
1044
1045GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1046 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001047 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencer24d6da52007-01-21 00:29:26 +00001048 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001049
Reid Spencera54b7cb2007-01-12 07:05:14 +00001050 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001051 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001052 else
Reid Spencerf9536332007-03-06 03:09:31 +00001053 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001054 return Dest;
Reid Spencerf9536332007-03-06 03:09:31 +00001055
Reid Spencera54b7cb2007-01-12 07:05:14 +00001056}
1057
1058GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1059 ExecutionContext &SF) {
1060 const Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +00001061 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001062 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001063 assert(isa<PointerType>(SrcTy) && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001064
Reid Spencerf9536332007-03-06 03:09:31 +00001065 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001066 return Dest;
1067}
1068
1069GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1070 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001071 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001072 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001073
Reid Spencer57631052007-03-06 03:46:41 +00001074 uint32_t PtrSize = TD.getPointerSizeInBits();
Reid Spencer7553c342007-03-06 03:41:50 +00001075 if (PtrSize != Src.IntVal.getBitWidth())
Reid Spencer57631052007-03-06 03:46:41 +00001076 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
Reid Spencer7553c342007-03-06 03:41:50 +00001077
Reid Spencer6bc63332007-04-26 18:19:35 +00001078 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
Reid Spencera54b7cb2007-01-12 07:05:14 +00001079 return Dest;
1080}
1081
1082GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1083 ExecutionContext &SF) {
1084
1085 const Type *SrcTy = SrcVal->getType();
1086 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1087 if (isa<PointerType>(DstTy)) {
1088 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1089 Dest.PointerVal = Src.PointerVal;
Chris Lattner42a75512007-01-15 02:27:26 +00001090 } else if (DstTy->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001091 if (SrcTy == Type::FloatTy) {
Reid Spencere4b43942007-05-04 03:37:38 +00001092 Dest.IntVal.zext(sizeof(Src.FloatVal) * 8);
Reid Spencerf9536332007-03-06 03:09:31 +00001093 Dest.IntVal.floatToBits(Src.FloatVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001094 } else if (SrcTy == Type::DoubleTy) {
Reid Spencere4b43942007-05-04 03:37:38 +00001095 Dest.IntVal.zext(sizeof(Src.DoubleVal) * 8);
Reid Spencerf9536332007-03-06 03:09:31 +00001096 Dest.IntVal.doubleToBits(Src.DoubleVal);
Chris Lattner42a75512007-01-15 02:27:26 +00001097 } else if (SrcTy->isInteger()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001098 Dest.IntVal = Src.IntVal;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001099 } else
1100 assert(0 && "Invalid BitCast");
1101 } else if (DstTy == Type::FloatTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001102 if (SrcTy->isInteger())
Reid Spencerf9536332007-03-06 03:09:31 +00001103 Dest.FloatVal = Src.IntVal.bitsToFloat();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001104 else
1105 Dest.FloatVal = Src.FloatVal;
1106 } else if (DstTy == Type::DoubleTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001107 if (SrcTy->isInteger())
Reid Spencerf9536332007-03-06 03:09:31 +00001108 Dest.DoubleVal = Src.IntVal.bitsToDouble();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001109 else
1110 Dest.DoubleVal = Src.DoubleVal;
1111 } else
1112 assert(0 && "Invalid Bitcast");
1113
1114 return Dest;
1115}
1116
1117void Interpreter::visitTruncInst(TruncInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001118 ExecutionContext &SF = ECStack.back();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001119 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1120}
1121
1122void Interpreter::visitSExtInst(SExtInst &I) {
1123 ExecutionContext &SF = ECStack.back();
1124 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1125}
1126
1127void Interpreter::visitZExtInst(ZExtInst &I) {
1128 ExecutionContext &SF = ECStack.back();
1129 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1130}
1131
1132void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1133 ExecutionContext &SF = ECStack.back();
1134 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1135}
1136
1137void Interpreter::visitFPExtInst(FPExtInst &I) {
1138 ExecutionContext &SF = ECStack.back();
1139 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1140}
1141
1142void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1143 ExecutionContext &SF = ECStack.back();
1144 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1145}
1146
1147void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1148 ExecutionContext &SF = ECStack.back();
1149 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1150}
1151
1152void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1153 ExecutionContext &SF = ECStack.back();
1154 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1155}
1156
1157void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1158 ExecutionContext &SF = ECStack.back();
1159 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1160}
1161
1162void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1163 ExecutionContext &SF = ECStack.back();
1164 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1165}
1166
1167void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1168 ExecutionContext &SF = ECStack.back();
1169 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1170}
1171
1172void Interpreter::visitBitCastInst(BitCastInst &I) {
1173 ExecutionContext &SF = ECStack.back();
1174 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001175}
Chris Lattner92101ac2001-08-23 17:05:04 +00001176
Brian Gaekec1a2be12003-11-07 21:20:47 +00001177#define IMPLEMENT_VAARG(TY) \
1178 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1179
1180void Interpreter::visitVAArgInst(VAArgInst &I) {
1181 ExecutionContext &SF = ECStack.back();
1182
Brian Gaeke9d20b712004-02-25 23:01:48 +00001183 // Get the incoming valist parameter. LLI treats the valist as a
1184 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001185 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001186 GenericValue Dest;
1187 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Reid Spencerf9536332007-03-06 03:09:31 +00001188 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001189 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001190 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001191 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001192 IMPLEMENT_VAARG(Pointer);
1193 IMPLEMENT_VAARG(Float);
1194 IMPLEMENT_VAARG(Double);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001195 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001196 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Brian Gaekec1a2be12003-11-07 21:20:47 +00001197 abort();
1198 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001199
Brian Gaekec1a2be12003-11-07 21:20:47 +00001200 // Set the Value of this Instruction.
1201 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001202
1203 // Move the pointer to the next vararg.
1204 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001205}
1206
Reid Spencere1aa0662007-03-03 06:22:22 +00001207GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1208 ExecutionContext &SF) {
1209 switch (CE->getOpcode()) {
1210 case Instruction::Trunc:
1211 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1212 case Instruction::ZExt:
1213 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1214 case Instruction::SExt:
1215 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1216 case Instruction::FPTrunc:
1217 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1218 case Instruction::FPExt:
1219 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1220 case Instruction::UIToFP:
1221 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1222 case Instruction::SIToFP:
1223 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1224 case Instruction::FPToUI:
1225 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1226 case Instruction::FPToSI:
1227 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1228 case Instruction::PtrToInt:
1229 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1230 case Instruction::IntToPtr:
1231 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1232 case Instruction::BitCast:
1233 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1234 case Instruction::GetElementPtr:
1235 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1236 gep_type_end(CE), SF);
Reid Spencere0929362007-03-03 08:38:04 +00001237 case Instruction::FCmp:
1238 case Instruction::ICmp:
1239 return executeCmpInst(CE->getPredicate(),
1240 getOperandValue(CE->getOperand(0), SF),
1241 getOperandValue(CE->getOperand(1), SF),
1242 CE->getOperand(0)->getType());
1243 case Instruction::Select:
1244 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1245 getOperandValue(CE->getOperand(1), SF),
1246 getOperandValue(CE->getOperand(2), SF));
Reid Spencere1aa0662007-03-03 06:22:22 +00001247 default :
1248 break;
1249 }
Reid Spencere0929362007-03-03 08:38:04 +00001250
1251 // The cases below here require a GenericValue parameter for the result
1252 // so we initialize one, compute it and then return it.
Reid Spencerf9536332007-03-06 03:09:31 +00001253 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1254 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
Reid Spencere1aa0662007-03-03 06:22:22 +00001255 GenericValue Dest;
Reid Spencerf9536332007-03-06 03:09:31 +00001256 const Type * Ty = CE->getOperand(0)->getType();
Reid Spencere1aa0662007-03-03 06:22:22 +00001257 switch (CE->getOpcode()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001258 case Instruction::Add: executeAddInst (Dest, Op0, Op1, Ty); break;
1259 case Instruction::Sub: executeSubInst (Dest, Op0, Op1, Ty); break;
1260 case Instruction::Mul: executeMulInst (Dest, Op0, Op1, Ty); break;
1261 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1262 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1263 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1264 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1265 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1266 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
1267 case Instruction::And: Dest.IntVal = Op0.IntVal.And(Op1.IntVal); break;
1268 case Instruction::Or: Dest.IntVal = Op0.IntVal.Or(Op1.IntVal); break;
1269 case Instruction::Xor: Dest.IntVal = Op0.IntVal.Xor(Op1.IntVal); break;
1270 case Instruction::Shl:
1271 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1272 break;
1273 case Instruction::LShr:
1274 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1275 break;
1276 case Instruction::AShr:
1277 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1278 break;
Reid Spencere1aa0662007-03-03 06:22:22 +00001279 default:
1280 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
1281 abort();
1282 return GenericValue();
1283 }
Reid Spencere0929362007-03-03 08:38:04 +00001284 return Dest;
Reid Spencere1aa0662007-03-03 06:22:22 +00001285}
1286
1287GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1288 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1289 return getConstantExprValue(CE, SF);
1290 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1291 return getConstantValue(CPV);
1292 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1293 return PTOGV(getPointerToGlobal(GV));
1294 } else {
1295 return SF.Values[V];
1296 }
1297}
1298
Chris Lattner92101ac2001-08-23 17:05:04 +00001299//===----------------------------------------------------------------------===//
1300// Dispatch and Execution Code
1301//===----------------------------------------------------------------------===//
1302
Chris Lattner92101ac2001-08-23 17:05:04 +00001303//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001304// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001305//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001306void Interpreter::callFunction(Function *F,
1307 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001308 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1309 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1310 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001311 // Make a new stack frame... and fill it in.
1312 ECStack.push_back(ExecutionContext());
1313 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001314 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001315
1316 // Special handling for external functions.
Reid Spencer5cbf9852007-01-30 20:08:39 +00001317 if (F->isDeclaration()) {
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001318 GenericValue Result = callExternalFunction (F, ArgVals);
1319 // Simulate a 'ret' instruction of the appropriate type.
1320 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1321 return;
1322 }
1323
1324 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001325 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001326 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001327
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001328 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001329 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001330 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001331 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001332
1333 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001334 unsigned i = 0;
Reid Spencer4ccf4622007-04-16 21:50:40 +00001335 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1336 AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001337 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001338
1339 // Handle varargs arguments...
1340 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001341}
1342
Reid Spencer951418b2007-05-16 02:05:13 +00001343
Chris Lattner92101ac2001-08-23 17:05:04 +00001344void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001345 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001346 // Interpret a single instruction & increment the "PC".
1347 ExecutionContext &SF = ECStack.back(); // Current stack frame
1348 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001349
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001350 // Track the number of dynamic instructions executed.
1351 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001352
Bill Wendling480f0932006-11-27 23:54:50 +00001353 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001354 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner27725bf2007-09-21 18:30:39 +00001355#if 0
1356 // This is not safe, as visiting the instruction could lower it and free I.
Reid Spencer951418b2007-05-16 02:05:13 +00001357#ifndef NDEBUG
1358 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1359 I.getType() != Type::VoidTy) {
1360 DOUT << " --> ";
Chris Lattner27725bf2007-09-21 18:30:39 +00001361 const GenericValue &Val = SF.Values[&I];
1362 switch (I.getType()->getTypeID()) {
1363 default: assert(0 && "Invalid GenericValue Type");
1364 case Type::VoidTyID: DOUT << "void"; break;
1365 case Type::FloatTyID: DOUT << "float " << Val.FloatVal; break;
1366 case Type::DoubleTyID: DOUT << "double " << Val.DoubleVal; break;
1367 case Type::PointerTyID: DOUT << "void* " << intptr_t(Val.PointerVal);
1368 break;
1369 case Type::IntegerTyID:
1370 DOUT << "i" << Val.IntVal.getBitWidth() << " "
1371 << Val.IntVal.toStringUnsigned(10)
1372 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
1373 break;
1374 }
Reid Spencer951418b2007-05-16 02:05:13 +00001375 }
1376#endif
Chris Lattner27725bf2007-09-21 18:30:39 +00001377#endif
Chris Lattner92101ac2001-08-23 17:05:04 +00001378 }
1379}