blob: d05e8d010c187e47bb045573a169b87dc22df24f [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"
24#include "llvm/Support/Debug.h"
Reid Spencera54b7cb2007-01-12 07:05:14 +000025#include "llvm/Support/MathExtras.h"
Gabor Greif724441e2007-10-11 19:40:35 +000026#include <algorithm>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000027#include <cmath>
28#include <cstring>
Chris Lattner4af6de82003-11-25 20:44:56 +000029using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000030
Chris Lattnercecf56b2006-12-19 22:56:53 +000031STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
32static Interpreter *TheEE = 0;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner2e42d3a2001-10-15 05:51:48 +000034//===----------------------------------------------------------------------===//
Reid Spencere1aa0662007-03-03 06:22:22 +000035// Various Helper Functions
Chris Lattner39bb5b42001-10-15 13:25:40 +000036//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000037
Reid Spencere1aa0662007-03-03 06:22:22 +000038static inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
Reid Spencera42c7fd2007-01-20 20:12:29 +000039 // Determine if the value is signed or not
40 bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
41 // If its signed, extend the sign bits
42 if (isSigned)
43 Val |= ~ITy->getBitMask();
44 return Val;
45}
46
Chris Lattner39bb5b42001-10-15 13:25:40 +000047static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000048 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +000049}
50
Chris Lattner2e42d3a2001-10-15 05:51:48 +000051void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +000052 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000053}
54
Chris Lattner2adcd832002-05-03 19:52:30 +000055//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000056// Binary Instruction Implementations
57//===----------------------------------------------------------------------===//
58
59#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
Reid Spencerf9536332007-03-06 03:09:31 +000060 case Type::TY##TyID: \
61 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
62 break
Chris Lattner92101ac2001-08-23 17:05:04 +000063
Reid Spencerf9536332007-03-06 03:09:31 +000064#define IMPLEMENT_INTEGER_BINOP1(OP, TY) \
Reid Spencera54b7cb2007-01-12 07:05:14 +000065 case Type::IntegerTyID: { \
Reid Spencerf9536332007-03-06 03:09:31 +000066 Dest.IntVal = Src1.IntVal OP Src2.IntVal; \
Reid Spencera54b7cb2007-01-12 07:05:14 +000067 break; \
68 }
69
Reid Spencera54b7cb2007-01-12 07:05:14 +000070
Reid Spencere1aa0662007-03-03 06:22:22 +000071static void executeAddInst(GenericValue &Dest, GenericValue Src1,
72 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000073 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +000074 IMPLEMENT_INTEGER_BINOP1(+, Ty);
Chris Lattner92101ac2001-08-23 17:05:04 +000075 IMPLEMENT_BINARY_OPERATOR(+, Float);
76 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000077 default:
Bill Wendlinge8156192006-12-07 01:30:32 +000078 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +000079 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +000080 }
Chris Lattner92101ac2001-08-23 17:05:04 +000081}
82
Reid Spencere1aa0662007-03-03 06:22:22 +000083static void executeSubInst(GenericValue &Dest, GenericValue Src1,
84 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000085 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +000086 IMPLEMENT_INTEGER_BINOP1(-, Ty);
Chris Lattner92101ac2001-08-23 17:05:04 +000087 IMPLEMENT_BINARY_OPERATOR(-, Float);
88 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000089 default:
Bill Wendlinge8156192006-12-07 01:30:32 +000090 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +000091 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +000092 }
Chris Lattner92101ac2001-08-23 17:05:04 +000093}
94
Reid Spencere1aa0662007-03-03 06:22:22 +000095static void executeMulInst(GenericValue &Dest, GenericValue Src1,
96 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000097 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +000098 IMPLEMENT_INTEGER_BINOP1(*, Ty);
Chris Lattnerc2593162001-10-27 08:28:11 +000099 IMPLEMENT_BINARY_OPERATOR(*, Float);
100 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000101 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000102 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000103 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000104 }
Chris Lattnerc2593162001-10-27 08:28:11 +0000105}
106
Reid Spencere1aa0662007-03-03 06:22:22 +0000107static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
108 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000109 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000110 IMPLEMENT_BINARY_OPERATOR(/, Float);
111 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000112 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000113 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000114 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000115 }
Chris Lattnerbb76f022001-10-30 20:27:31 +0000116}
117
Reid Spencere1aa0662007-03-03 06:22:22 +0000118static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
119 GenericValue Src2, const Type *Ty) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000120 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000121 case Type::FloatTyID:
122 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
123 break;
124 case Type::DoubleTyID:
125 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
126 break;
127 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000128 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000129 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000130 }
Chris Lattnerc2593162001-10-27 08:28:11 +0000131}
132
Reid Spencerf9536332007-03-06 03:09:31 +0000133#define IMPLEMENT_INTEGER_ICMP(OP, TY) \
134 case Type::IntegerTyID: \
135 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
136 break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000137
Chris Lattnerfd506f52003-04-23 19:55:35 +0000138// Handle pointers specially because they must be compared with only as much
139// width as the host has. We _do not_ want to be comparing 64 bit values when
140// running on a 32-bit target, otherwise the upper 32 bits might mess up
141// comparisons if they contain garbage.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000142#define IMPLEMENT_POINTER_ICMP(OP) \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000143 case Type::PointerTyID: \
Reid Spencerf9536332007-03-06 03:09:31 +0000144 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
145 (void*)(intptr_t)Src2.PointerVal); \
146 break;
Chris Lattnerfd506f52003-04-23 19:55:35 +0000147
Reid Spencere4d87aa2006-12-23 06:05:41 +0000148static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
149 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000150 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000151 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000152 IMPLEMENT_INTEGER_ICMP(eq,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000153 IMPLEMENT_POINTER_ICMP(==);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000154 default:
155 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
156 abort();
157 }
158 return Dest;
159}
160
161static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
162 const Type *Ty) {
163 GenericValue Dest;
164 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000165 IMPLEMENT_INTEGER_ICMP(ne,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000166 IMPLEMENT_POINTER_ICMP(!=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000167 default:
168 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
169 abort();
170 }
171 return Dest;
172}
173
174static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
175 const Type *Ty) {
176 GenericValue Dest;
177 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000178 IMPLEMENT_INTEGER_ICMP(ult,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000179 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000180 default:
181 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
182 abort();
183 }
184 return Dest;
185}
186
187static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
188 const Type *Ty) {
189 GenericValue Dest;
190 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000191 IMPLEMENT_INTEGER_ICMP(slt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000192 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000193 default:
194 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
195 abort();
196 }
197 return Dest;
198}
199
200static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
201 const Type *Ty) {
202 GenericValue Dest;
203 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000204 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000205 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000206 default:
207 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
208 abort();
209 }
210 return Dest;
211}
212
213static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
214 const Type *Ty) {
215 GenericValue Dest;
216 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000217 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000218 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000219 default:
220 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
221 abort();
222 }
223 return Dest;
224}
225
226static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
227 const Type *Ty) {
228 GenericValue Dest;
229 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000230 IMPLEMENT_INTEGER_ICMP(ule,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000231 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000232 default:
233 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
234 abort();
235 }
236 return Dest;
237}
238
239static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
240 const Type *Ty) {
241 GenericValue Dest;
242 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000243 IMPLEMENT_INTEGER_ICMP(sle,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000244 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000245 default:
246 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
247 abort();
248 }
249 return Dest;
250}
251
252static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
253 const Type *Ty) {
254 GenericValue Dest;
255 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000256 IMPLEMENT_INTEGER_ICMP(uge,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000257 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000258 default:
259 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
260 abort();
261 }
262 return Dest;
263}
264
265static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
266 const Type *Ty) {
267 GenericValue Dest;
268 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000269 IMPLEMENT_INTEGER_ICMP(sge,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000270 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000271 default:
272 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
273 abort();
274 }
275 return Dest;
276}
277
Reid Spencere49661b2006-12-31 05:51:36 +0000278void Interpreter::visitICmpInst(ICmpInst &I) {
279 ExecutionContext &SF = ECStack.back();
280 const Type *Ty = I.getOperand(0)->getType();
281 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
282 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
283 GenericValue R; // Result
284
285 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000286 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
287 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
Reid Spencere49661b2006-12-31 05:51:36 +0000288 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
289 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
290 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
291 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
292 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
293 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
294 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
295 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
296 default:
297 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
298 abort();
299 }
300
301 SetValue(&I, R, SF);
302}
303
304#define IMPLEMENT_FCMP(OP, TY) \
Reid Spencerf9536332007-03-06 03:09:31 +0000305 case Type::TY##TyID: \
306 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
307 break
Reid Spencere4d87aa2006-12-23 06:05:41 +0000308
Reid Spencera54b7cb2007-01-12 07:05:14 +0000309static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000310 const Type *Ty) {
311 GenericValue Dest;
312 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000313 IMPLEMENT_FCMP(==, Float);
314 IMPLEMENT_FCMP(==, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000315 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000316 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000317 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000318 }
319 return Dest;
320}
321
Reid Spencera54b7cb2007-01-12 07:05:14 +0000322static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000323 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000324 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000325 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000326 IMPLEMENT_FCMP(!=, Float);
327 IMPLEMENT_FCMP(!=, Double);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000328
Chris Lattner92101ac2001-08-23 17:05:04 +0000329 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000330 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000331 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000332 }
333 return Dest;
334}
335
Reid Spencera54b7cb2007-01-12 07:05:14 +0000336static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000337 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000338 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000339 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000340 IMPLEMENT_FCMP(<=, Float);
341 IMPLEMENT_FCMP(<=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000342 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000343 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000344 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000345 }
346 return Dest;
347}
348
Reid Spencera54b7cb2007-01-12 07:05:14 +0000349static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000350 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000351 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000352 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000353 IMPLEMENT_FCMP(>=, Float);
354 IMPLEMENT_FCMP(>=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000355 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000356 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000357 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000358 }
359 return Dest;
360}
361
Reid Spencera54b7cb2007-01-12 07:05:14 +0000362static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000363 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000364 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000365 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000366 IMPLEMENT_FCMP(<, Float);
367 IMPLEMENT_FCMP(<, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000368 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000369 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000370 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000371 }
372 return Dest;
373}
374
Reid Spencera54b7cb2007-01-12 07:05:14 +0000375static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000376 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000377 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000378 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000379 IMPLEMENT_FCMP(>, Float);
380 IMPLEMENT_FCMP(>, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000381 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000382 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000383 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000384 }
385 return Dest;
386}
387
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000388#define IMPLEMENT_UNORDERED(TY, X,Y) \
389 if (TY == Type::FloatTy) { \
390 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
391 Dest.IntVal = APInt(1,true); \
392 return Dest; \
393 } \
394 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
395 Dest.IntVal = APInt(1,true); \
396 return Dest; \
397 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000398
399
400static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
401 const Type *Ty) {
402 GenericValue Dest;
403 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
404 return executeFCMP_OEQ(Src1, Src2, Ty);
405}
406
407static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
408 const Type *Ty) {
409 GenericValue Dest;
410 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
411 return executeFCMP_ONE(Src1, Src2, Ty);
412}
413
414static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
415 const Type *Ty) {
416 GenericValue Dest;
417 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
418 return executeFCMP_OLE(Src1, Src2, Ty);
419}
420
421static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
422 const Type *Ty) {
423 GenericValue Dest;
424 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
425 return executeFCMP_OGE(Src1, Src2, Ty);
426}
427
428static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
429 const Type *Ty) {
430 GenericValue Dest;
431 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
432 return executeFCMP_OLT(Src1, Src2, Ty);
433}
434
435static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
436 const Type *Ty) {
437 GenericValue Dest;
438 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
439 return executeFCMP_OGT(Src1, Src2, Ty);
440}
441
442static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
443 const Type *Ty) {
444 GenericValue Dest;
445 if (Ty == Type::FloatTy)
Reid Spencerf9536332007-03-06 03:09:31 +0000446 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
447 Src2.FloatVal == Src2.FloatVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000448 else
Reid Spencerf9536332007-03-06 03:09:31 +0000449 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
450 Src2.DoubleVal == Src2.DoubleVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000451 return Dest;
452}
453
454static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
455 const Type *Ty) {
456 GenericValue Dest;
457 if (Ty == Type::FloatTy)
Reid Spencerf9536332007-03-06 03:09:31 +0000458 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
459 Src2.FloatVal != Src2.FloatVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000460 else
Reid Spencerf9536332007-03-06 03:09:31 +0000461 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
462 Src2.DoubleVal != Src2.DoubleVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000463 return Dest;
464}
465
Reid Spencere4d87aa2006-12-23 06:05:41 +0000466void Interpreter::visitFCmpInst(FCmpInst &I) {
467 ExecutionContext &SF = ECStack.back();
468 const Type *Ty = I.getOperand(0)->getType();
469 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
470 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
471 GenericValue R; // Result
472
473 switch (I.getPredicate()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000474 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
475 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000476 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
477 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
478 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
479 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
480 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
481 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
482 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
483 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
484 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
485 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
486 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
487 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
488 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
489 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000490 default:
491 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
492 abort();
493 }
494
495 SetValue(&I, R, SF);
496}
497
Reid Spencere4d87aa2006-12-23 06:05:41 +0000498static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
499 GenericValue Src2, const Type *Ty) {
500 GenericValue Result;
501 switch (predicate) {
502 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
503 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
504 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
505 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
506 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
507 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
508 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
509 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
510 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
511 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000512 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
513 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
514 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
515 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
516 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
517 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
518 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
519 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
520 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
521 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
522 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
523 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
524 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
525 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000526 case FCmpInst::FCMP_FALSE: {
527 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000528 Result.IntVal = APInt(1, false);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000529 return Result;
530 }
531 case FCmpInst::FCMP_TRUE: {
532 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000533 Result.IntVal = APInt(1, true);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000534 return Result;
535 }
536 default:
537 cerr << "Unhandled Cmp predicate\n";
538 abort();
539 }
540}
541
Chris Lattnerd7916e92003-05-10 21:22:39 +0000542void Interpreter::visitBinaryOperator(BinaryOperator &I) {
543 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000544 const Type *Ty = I.getOperand(0)->getType();
545 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
546 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000547 GenericValue R; // Result
548
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000549 switch (I.getOpcode()) {
Reid Spencere1aa0662007-03-03 06:22:22 +0000550 case Instruction::Add: executeAddInst (R, Src1, Src2, Ty); break;
551 case Instruction::Sub: executeSubInst (R, Src1, Src2, Ty); break;
552 case Instruction::Mul: executeMulInst (R, Src1, Src2, Ty); break;
Reid Spencere1aa0662007-03-03 06:22:22 +0000553 case Instruction::FDiv: executeFDivInst (R, Src1, Src2, Ty); break;
Reid Spencere1aa0662007-03-03 06:22:22 +0000554 case Instruction::FRem: executeFRemInst (R, Src1, Src2, Ty); break;
Reid Spencerf9536332007-03-06 03:09:31 +0000555 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
556 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
557 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
558 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
559 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
560 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
561 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000562 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000563 cerr << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner02868352003-04-22 21:22:33 +0000564 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000565 }
566
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000567 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000568}
569
Misha Brukmand1c881a2005-04-21 22:43:08 +0000570static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000571 GenericValue Src3) {
Reid Spencerf9536332007-03-06 03:09:31 +0000572 return Src1.IntVal == 0 ? Src3 : Src2;
Chris Lattner759d34f2004-04-20 16:43:21 +0000573}
574
575void Interpreter::visitSelectInst(SelectInst &I) {
576 ExecutionContext &SF = ECStack.back();
577 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
578 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
579 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
Reid Spencerf9536332007-03-06 03:09:31 +0000580 GenericValue R = executeSelectInst(Src1, Src2, Src3);
Chris Lattner759d34f2004-04-20 16:43:21 +0000581 SetValue(&I, R, SF);
582}
583
584
Chris Lattner92101ac2001-08-23 17:05:04 +0000585//===----------------------------------------------------------------------===//
586// Terminator Instruction Implementations
587//===----------------------------------------------------------------------===//
588
Chris Lattnere43db882001-10-27 04:15:57 +0000589void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000590 // runAtExitHandlers() assumes there are no stack frames, but
591 // if exit() was called, then it had a stack frame. Blow away
592 // the stack before interpreting atexit handlers.
593 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000594 runAtExitHandlers ();
Reid Spencerf9536332007-03-06 03:09:31 +0000595 exit (GV.IntVal.zextOrTrunc(32).getZExtValue());
Chris Lattnere43db882001-10-27 04:15:57 +0000596}
597
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000598/// Pop the last stack frame off of ECStack and then copy the result
599/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000600/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000601/// CallInst if there was a previous stack frame. This method may
602/// invalidate any ECStack iterators you have. This method also takes
603/// care of switching to the normal destination BB, if we are returning
604/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000605///
606void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
607 GenericValue Result) {
608 // Pop the current stack frame.
609 ECStack.pop_back();
610
Misha Brukmand1c881a2005-04-21 22:43:08 +0000611 if (ECStack.empty()) { // Finished main. Put result into exit code...
Chris Lattner42a75512007-01-15 02:27:26 +0000612 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000613 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000614 } else {
Reid Spencere7707872007-06-01 22:23:29 +0000615 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000616 }
617 } else {
618 // If we have a previous stack frame, and we have a previous call,
619 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000620 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000621 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000622 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000623 SetValue(I, Result, CallingSF);
624 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
625 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000626 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000627 }
628 }
629}
630
Chris Lattnerd7916e92003-05-10 21:22:39 +0000631void Interpreter::visitReturnInst(ReturnInst &I) {
632 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000633 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000634 GenericValue Result;
635
636 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000637 if (I.getNumOperands()) {
638 RetTy = I.getReturnValue()->getType();
639 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000640 }
641
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000642 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000643}
644
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000645void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000646 // Unwind stack
647 Instruction *Inst;
648 do {
649 ECStack.pop_back ();
650 if (ECStack.empty ())
651 abort ();
652 Inst = ECStack.back ().Caller.getInstruction ();
653 } while (!(Inst && isa<InvokeInst> (Inst)));
654
655 // Return from invoke
656 ExecutionContext &InvokingSF = ECStack.back ();
657 InvokingSF.Caller = CallSite ();
658
659 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000660 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000661}
662
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000663void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000664 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000665 abort();
666}
667
Chris Lattnerd7916e92003-05-10 21:22:39 +0000668void Interpreter::visitBranchInst(BranchInst &I) {
669 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000670 BasicBlock *Dest;
671
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000672 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
673 if (!I.isUnconditional()) {
674 Value *Cond = I.getCondition();
Reid Spencerf9536332007-03-06 03:09:31 +0000675 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000676 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000677 }
Chris Lattner77113b62003-05-10 20:21:16 +0000678 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000679}
680
Chris Lattnerd7916e92003-05-10 21:22:39 +0000681void Interpreter::visitSwitchInst(SwitchInst &I) {
682 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000683 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
684 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000685
686 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000687 BasicBlock *Dest = 0;
688 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Reid Spencerf9536332007-03-06 03:09:31 +0000689 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
690 .IntVal != 0) {
Chris Lattner09e93922003-04-22 20:34:47 +0000691 Dest = cast<BasicBlock>(I.getOperand(i+1));
692 break;
693 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000694
Chris Lattner09e93922003-04-22 20:34:47 +0000695 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000696 SwitchToNewBasicBlock(Dest, SF);
697}
698
699// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
700// This function handles the actual updating of block and instruction iterators
701// as well as execution of all of the PHI nodes in the destination block.
702//
703// This method does this because all of the PHI nodes must be executed
704// atomically, reading their inputs before any of the results are updated. Not
705// doing this can cause problems if the PHI nodes depend on other PHI nodes for
706// their inputs. If the input PHI node is updated before it is read, incorrect
707// results can happen. Thus we use a two phase approach.
708//
709void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
710 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
711 SF.CurBB = Dest; // Update CurBB to branch destination
712 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
713
714 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
715
716 // Loop over all of the PHI nodes in the current block, reading their inputs.
717 std::vector<GenericValue> ResultValues;
718
719 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
720 // Search for the value corresponding to this previous bb...
721 int i = PN->getBasicBlockIndex(PrevBB);
722 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
723 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000724
Chris Lattner77113b62003-05-10 20:21:16 +0000725 // Save the incoming value for this PHI node...
726 ResultValues.push_back(getOperandValue(IncomingValue, SF));
727 }
728
729 // Now loop over all of the PHI nodes setting their values...
730 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000731 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
732 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000733 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000734 }
Chris Lattner09e93922003-04-22 20:34:47 +0000735}
736
Chris Lattner92101ac2001-08-23 17:05:04 +0000737//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000738// Memory Instruction Implementations
739//===----------------------------------------------------------------------===//
740
Chris Lattnerd7916e92003-05-10 21:22:39 +0000741void Interpreter::visitAllocationInst(AllocationInst &I) {
742 ExecutionContext &SF = ECStack.back();
743
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000744 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000745
Chris Lattnercc82cc12002-04-28 21:57:33 +0000746 // Get the number of elements being allocated by the array...
Reid Spencerf9536332007-03-06 03:09:31 +0000747 unsigned NumElements =
748 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
749
Duncan Sands514ab342007-11-01 20:53:16 +0000750 unsigned TypeSize = (size_t)TD.getABITypeSize(Ty);
Reid Spencerf9536332007-03-06 03:09:31 +0000751
Gabor Greif724441e2007-10-11 19:40:35 +0000752 // Avoid malloc-ing zero bytes, use max()...
753 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
Chris Lattner86660982001-08-27 05:16:50 +0000754
755 // Allocate enough memory to hold the type...
Reid Spencerf9536332007-03-06 03:09:31 +0000756 void *Memory = malloc(MemToAlloc);
757
758 DOUT << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
759 << NumElements << " (Total: " << MemToAlloc << ") at "
Bill Wendling47992ea2007-03-08 23:37:24 +0000760 << uintptr_t(Memory) << '\n';
Chris Lattner9bffa732002-02-19 18:50:09 +0000761
Chris Lattnerfe11a972002-12-23 23:59:41 +0000762 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000763 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000764 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000765
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000766 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000767 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000768}
769
Chris Lattnerd7916e92003-05-10 21:22:39 +0000770void Interpreter::visitFreeInst(FreeInst &I) {
771 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000772 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
773 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000774 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000775 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000776}
777
Chris Lattnera34c5682002-08-27 22:33:45 +0000778// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000779//
Chris Lattner4af6de82003-11-25 20:44:56 +0000780GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000781 gep_type_iterator E,
782 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000783 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000784 "Cannot getElementOffset of a nonpointer type!");
785
Reid Spencerf9536332007-03-06 03:09:31 +0000786 uint64_t Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000787
788 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000789 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000790 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000791
Reid Spencerb83eb642006-10-20 07:07:24 +0000792 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
793 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000794
Reid Spencerf9536332007-03-06 03:09:31 +0000795 Total += SLO->getElementOffset(Index);
Chris Lattner4af6de82003-11-25 20:44:56 +0000796 } else {
797 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000798 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000799 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
800
Reid Spencera54b7cb2007-01-12 07:05:14 +0000801 int64_t Idx;
802 unsigned BitWidth =
803 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
Reid Spencer23e28832007-01-18 01:25:42 +0000804 if (BitWidth == 32)
Reid Spencerf9536332007-03-06 03:09:31 +0000805 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
Chris Lattneraee7f212008-04-06 21:50:58 +0000806 else {
807 assert(BitWidth == 64 && "Invalid index type for getelementptr");
Reid Spencerf9536332007-03-06 03:09:31 +0000808 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
Chris Lattneraee7f212008-04-06 21:50:58 +0000809 }
Duncan Sands514ab342007-11-01 20:53:16 +0000810 Total += TD.getABITypeSize(ST->getElementType())*Idx;
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000811 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000812 }
813
Chris Lattnera34c5682002-08-27 22:33:45 +0000814 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000815 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
816 DOUT << "GEP Index " << Total << " bytes.\n";
Chris Lattnera34c5682002-08-27 22:33:45 +0000817 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000818}
819
Chris Lattnerd7916e92003-05-10 21:22:39 +0000820void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
821 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000822 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000823 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000824}
825
Chris Lattnerd7916e92003-05-10 21:22:39 +0000826void Interpreter::visitLoadInst(LoadInst &I) {
827 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000828 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000829 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Reid Spencere0929362007-03-03 08:38:04 +0000830 GenericValue Result;
Reid Spencere0929362007-03-03 08:38:04 +0000831 LoadValueFromMemory(Result, Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000832 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000833}
834
Chris Lattnerd7916e92003-05-10 21:22:39 +0000835void Interpreter::visitStoreInst(StoreInst &I) {
836 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000837 GenericValue Val = getOperandValue(I.getOperand(0), SF);
838 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000839 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000840 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000841}
842
Chris Lattner86660982001-08-27 05:16:50 +0000843//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000844// Miscellaneous Instruction Implementations
845//===----------------------------------------------------------------------===//
846
Brian Gaekefea483d2003-11-07 20:04:22 +0000847void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000848 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000849
850 // Check to see if this is an intrinsic function call...
Reid Spencer4ccf4622007-04-16 21:50:40 +0000851 Function *F = CS.getCalledFunction();
852 if (F && F->isDeclaration ())
Chris Lattner73011782003-12-28 09:44:37 +0000853 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000854 case Intrinsic::not_intrinsic:
855 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000856 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000857 GenericValue ArgIndex;
858 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
859 ArgIndex.UIntPairVal.second = 0;
860 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000861 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000862 }
Chris Lattner317201d2004-03-13 00:24:00 +0000863 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000864 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000865 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000866 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
867 return;
868 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000869 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000870 // class to transform it into hopefully tasty LLVM code.
871 //
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000872 BasicBlock::iterator me(CS.getInstruction());
Chris Lattner73011782003-12-28 09:44:37 +0000873 BasicBlock *Parent = CS.getInstruction()->getParent();
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000874 bool atBegin(Parent->begin() == me);
875 if (!atBegin)
876 --me;
Chris Lattner73011782003-12-28 09:44:37 +0000877 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
878
879 // Restore the CurInst pointer to the first instruction newly inserted, if
880 // any.
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000881 if (atBegin) {
Chris Lattner73011782003-12-28 09:44:37 +0000882 SF.CurInst = Parent->begin();
883 } else {
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000884 SF.CurInst = me;
Chris Lattner73011782003-12-28 09:44:37 +0000885 ++SF.CurInst;
886 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000887 return;
Chris Lattner73011782003-12-28 09:44:37 +0000888 }
889
Reid Spencer4ccf4622007-04-16 21:50:40 +0000890
Brian Gaekefea483d2003-11-07 20:04:22 +0000891 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000892 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000893 const unsigned NumArgs = SF.Caller.arg_size();
894 ArgVals.reserve(NumArgs);
Reid Spencer4ccf4622007-04-16 21:50:40 +0000895 uint16_t pNum = 1;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000896 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
Reid Spencer4ccf4622007-04-16 21:50:40 +0000897 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
Brian Gaekeae2495a2003-11-07 19:59:08 +0000898 Value *V = *i;
899 ArgVals.push_back(getOperandValue(V, SF));
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000900 // 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();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000904 if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32)) {
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000905 if (CS.paramHasAttr(pNum, ParamAttr::ZExt))
906 ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
907 else if (CS.paramHasAttr(pNum, ParamAttr::SExt))
908 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000909 }
Chris Lattner93780132003-01-13 00:58:52 +0000910 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000911
Misha Brukmand1c881a2005-04-21 22:43:08 +0000912 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000913 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000914 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000915 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000916}
917
Reid Spencer832254e2007-02-02 02:16:23 +0000918void Interpreter::visitShl(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000919 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000920 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
921 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
922 GenericValue Dest;
Reid Spencerf9536332007-03-06 03:09:31 +0000923 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000924 SetValue(&I, Dest, SF);
925}
926
Reid Spencer832254e2007-02-02 02:16:23 +0000927void Interpreter::visitLShr(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000928 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000929 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
930 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
931 GenericValue Dest;
Reid Spencerf9536332007-03-06 03:09:31 +0000932 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
Reid Spencer3822ff52006-11-08 06:47:33 +0000933 SetValue(&I, Dest, SF);
934}
935
Reid Spencer832254e2007-02-02 02:16:23 +0000936void Interpreter::visitAShr(BinaryOperator &I) {
Reid Spencer3822ff52006-11-08 06:47:33 +0000937 ExecutionContext &SF = ECStack.back();
Reid Spencer3822ff52006-11-08 06:47:33 +0000938 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
939 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Reid Spencerf9536332007-03-06 03:09:31 +0000940 GenericValue Dest;
941 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000942 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000943}
944
Reid Spencera54b7cb2007-01-12 07:05:14 +0000945GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
946 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000947 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000948 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000949 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +0000950 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
Chris Lattnera34c5682002-08-27 22:33:45 +0000951 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000952}
Chris Lattner92101ac2001-08-23 17:05:04 +0000953
Reid Spencera54b7cb2007-01-12 07:05:14 +0000954GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
955 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000956 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
957 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000958 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +0000959 Dest.IntVal = Src.IntVal.sext(DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000960 return Dest;
961}
962
963GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
964 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000965 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
966 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000967 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +0000968 Dest.IntVal = Src.IntVal.zext(DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000969 return Dest;
970}
971
972GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
973 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000974 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattneraee7f212008-04-06 21:50:58 +0000975 assert(SrcVal->getType() == Type::DoubleTy && DstTy == Type::FloatTy &&
Reid Spencera54b7cb2007-01-12 07:05:14 +0000976 "Invalid FPTrunc instruction");
977 Dest.FloatVal = (float) Src.DoubleVal;
978 return Dest;
979}
980
981GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
982 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000983 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattneraee7f212008-04-06 21:50:58 +0000984 assert(SrcVal->getType() == Type::FloatTy && DstTy == Type::DoubleTy &&
Reid Spencera54b7cb2007-01-12 07:05:14 +0000985 "Invalid FPTrunc instruction");
986 Dest.DoubleVal = (double) Src.FloatVal;
987 return Dest;
988}
989
990GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
991 ExecutionContext &SF) {
992 const Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +0000993 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000994 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000995 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
Reid Spencere0929362007-03-03 08:38:04 +0000996
Reid Spencera54b7cb2007-01-12 07:05:14 +0000997 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +0000998 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000999 else
Reid Spencerf9536332007-03-06 03:09:31 +00001000 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001001 return Dest;
1002}
1003
1004GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1005 ExecutionContext &SF) {
1006 const Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +00001007 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001008 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001009 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001010
Reid Spencera54b7cb2007-01-12 07:05:14 +00001011 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001012 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001013 else
Reid Spencerf9536332007-03-06 03:09:31 +00001014 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001015 return Dest;
1016}
1017
1018GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1019 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001020 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001021 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001022
Reid Spencera54b7cb2007-01-12 07:05:14 +00001023 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001024 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001025 else
Reid Spencerf9536332007-03-06 03:09:31 +00001026 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001027 return Dest;
1028}
1029
1030GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1031 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001032 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencer24d6da52007-01-21 00:29:26 +00001033 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001034
Reid Spencera54b7cb2007-01-12 07:05:14 +00001035 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001036 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001037 else
Reid Spencerf9536332007-03-06 03:09:31 +00001038 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001039 return Dest;
Reid Spencerf9536332007-03-06 03:09:31 +00001040
Reid Spencera54b7cb2007-01-12 07:05:14 +00001041}
1042
1043GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1044 ExecutionContext &SF) {
Reid Spencerf9536332007-03-06 03:09:31 +00001045 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001046 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattneraee7f212008-04-06 21:50:58 +00001047 assert(isa<PointerType>(SrcVal->getType()) && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001048
Reid Spencerf9536332007-03-06 03:09:31 +00001049 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001050 return Dest;
1051}
1052
1053GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1054 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001055 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001056 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001057
Reid Spencer57631052007-03-06 03:46:41 +00001058 uint32_t PtrSize = TD.getPointerSizeInBits();
Reid Spencer7553c342007-03-06 03:41:50 +00001059 if (PtrSize != Src.IntVal.getBitWidth())
Reid Spencer57631052007-03-06 03:46:41 +00001060 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
Reid Spencer7553c342007-03-06 03:41:50 +00001061
Reid Spencer6bc63332007-04-26 18:19:35 +00001062 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
Reid Spencera54b7cb2007-01-12 07:05:14 +00001063 return Dest;
1064}
1065
1066GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1067 ExecutionContext &SF) {
1068
1069 const Type *SrcTy = SrcVal->getType();
1070 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1071 if (isa<PointerType>(DstTy)) {
1072 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1073 Dest.PointerVal = Src.PointerVal;
Chris Lattner42a75512007-01-15 02:27:26 +00001074 } else if (DstTy->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001075 if (SrcTy == Type::FloatTy) {
Reid Spencere4b43942007-05-04 03:37:38 +00001076 Dest.IntVal.zext(sizeof(Src.FloatVal) * 8);
Reid Spencerf9536332007-03-06 03:09:31 +00001077 Dest.IntVal.floatToBits(Src.FloatVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001078 } else if (SrcTy == Type::DoubleTy) {
Reid Spencere4b43942007-05-04 03:37:38 +00001079 Dest.IntVal.zext(sizeof(Src.DoubleVal) * 8);
Reid Spencerf9536332007-03-06 03:09:31 +00001080 Dest.IntVal.doubleToBits(Src.DoubleVal);
Chris Lattner42a75512007-01-15 02:27:26 +00001081 } else if (SrcTy->isInteger()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001082 Dest.IntVal = Src.IntVal;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001083 } else
1084 assert(0 && "Invalid BitCast");
1085 } else if (DstTy == Type::FloatTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001086 if (SrcTy->isInteger())
Reid Spencerf9536332007-03-06 03:09:31 +00001087 Dest.FloatVal = Src.IntVal.bitsToFloat();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001088 else
1089 Dest.FloatVal = Src.FloatVal;
1090 } else if (DstTy == Type::DoubleTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001091 if (SrcTy->isInteger())
Reid Spencerf9536332007-03-06 03:09:31 +00001092 Dest.DoubleVal = Src.IntVal.bitsToDouble();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001093 else
1094 Dest.DoubleVal = Src.DoubleVal;
1095 } else
1096 assert(0 && "Invalid Bitcast");
1097
1098 return Dest;
1099}
1100
1101void Interpreter::visitTruncInst(TruncInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001102 ExecutionContext &SF = ECStack.back();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001103 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1104}
1105
1106void Interpreter::visitSExtInst(SExtInst &I) {
1107 ExecutionContext &SF = ECStack.back();
1108 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1109}
1110
1111void Interpreter::visitZExtInst(ZExtInst &I) {
1112 ExecutionContext &SF = ECStack.back();
1113 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1114}
1115
1116void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1117 ExecutionContext &SF = ECStack.back();
1118 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1119}
1120
1121void Interpreter::visitFPExtInst(FPExtInst &I) {
1122 ExecutionContext &SF = ECStack.back();
1123 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1124}
1125
1126void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1127 ExecutionContext &SF = ECStack.back();
1128 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1129}
1130
1131void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1132 ExecutionContext &SF = ECStack.back();
1133 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1134}
1135
1136void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1137 ExecutionContext &SF = ECStack.back();
1138 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1139}
1140
1141void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1142 ExecutionContext &SF = ECStack.back();
1143 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1144}
1145
1146void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1147 ExecutionContext &SF = ECStack.back();
1148 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1149}
1150
1151void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1152 ExecutionContext &SF = ECStack.back();
1153 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1154}
1155
1156void Interpreter::visitBitCastInst(BitCastInst &I) {
1157 ExecutionContext &SF = ECStack.back();
1158 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001159}
Chris Lattner92101ac2001-08-23 17:05:04 +00001160
Brian Gaekec1a2be12003-11-07 21:20:47 +00001161#define IMPLEMENT_VAARG(TY) \
1162 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1163
1164void Interpreter::visitVAArgInst(VAArgInst &I) {
1165 ExecutionContext &SF = ECStack.back();
1166
Brian Gaeke9d20b712004-02-25 23:01:48 +00001167 // Get the incoming valist parameter. LLI treats the valist as a
1168 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001169 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001170 GenericValue Dest;
1171 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Reid Spencerf9536332007-03-06 03:09:31 +00001172 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001173 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001174 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001175 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001176 IMPLEMENT_VAARG(Pointer);
1177 IMPLEMENT_VAARG(Float);
1178 IMPLEMENT_VAARG(Double);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001179 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001180 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Brian Gaekec1a2be12003-11-07 21:20:47 +00001181 abort();
1182 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001183
Brian Gaekec1a2be12003-11-07 21:20:47 +00001184 // Set the Value of this Instruction.
1185 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001186
1187 // Move the pointer to the next vararg.
1188 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001189}
1190
Reid Spencere1aa0662007-03-03 06:22:22 +00001191GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1192 ExecutionContext &SF) {
1193 switch (CE->getOpcode()) {
1194 case Instruction::Trunc:
1195 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1196 case Instruction::ZExt:
1197 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1198 case Instruction::SExt:
1199 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1200 case Instruction::FPTrunc:
1201 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1202 case Instruction::FPExt:
1203 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1204 case Instruction::UIToFP:
1205 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1206 case Instruction::SIToFP:
1207 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1208 case Instruction::FPToUI:
1209 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1210 case Instruction::FPToSI:
1211 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1212 case Instruction::PtrToInt:
1213 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1214 case Instruction::IntToPtr:
1215 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1216 case Instruction::BitCast:
1217 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1218 case Instruction::GetElementPtr:
1219 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1220 gep_type_end(CE), SF);
Reid Spencere0929362007-03-03 08:38:04 +00001221 case Instruction::FCmp:
1222 case Instruction::ICmp:
1223 return executeCmpInst(CE->getPredicate(),
1224 getOperandValue(CE->getOperand(0), SF),
1225 getOperandValue(CE->getOperand(1), SF),
1226 CE->getOperand(0)->getType());
1227 case Instruction::Select:
1228 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1229 getOperandValue(CE->getOperand(1), SF),
1230 getOperandValue(CE->getOperand(2), SF));
Reid Spencere1aa0662007-03-03 06:22:22 +00001231 default :
1232 break;
1233 }
Reid Spencere0929362007-03-03 08:38:04 +00001234
1235 // The cases below here require a GenericValue parameter for the result
1236 // so we initialize one, compute it and then return it.
Reid Spencerf9536332007-03-06 03:09:31 +00001237 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1238 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
Reid Spencere1aa0662007-03-03 06:22:22 +00001239 GenericValue Dest;
Reid Spencerf9536332007-03-06 03:09:31 +00001240 const Type * Ty = CE->getOperand(0)->getType();
Reid Spencere1aa0662007-03-03 06:22:22 +00001241 switch (CE->getOpcode()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001242 case Instruction::Add: executeAddInst (Dest, Op0, Op1, Ty); break;
1243 case Instruction::Sub: executeSubInst (Dest, Op0, Op1, Ty); break;
1244 case Instruction::Mul: executeMulInst (Dest, Op0, Op1, Ty); break;
1245 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1246 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1247 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1248 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1249 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1250 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
1251 case Instruction::And: Dest.IntVal = Op0.IntVal.And(Op1.IntVal); break;
1252 case Instruction::Or: Dest.IntVal = Op0.IntVal.Or(Op1.IntVal); break;
1253 case Instruction::Xor: Dest.IntVal = Op0.IntVal.Xor(Op1.IntVal); break;
1254 case Instruction::Shl:
1255 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1256 break;
1257 case Instruction::LShr:
1258 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1259 break;
1260 case Instruction::AShr:
1261 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1262 break;
Reid Spencere1aa0662007-03-03 06:22:22 +00001263 default:
1264 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
1265 abort();
1266 return GenericValue();
1267 }
Reid Spencere0929362007-03-03 08:38:04 +00001268 return Dest;
Reid Spencere1aa0662007-03-03 06:22:22 +00001269}
1270
1271GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1272 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1273 return getConstantExprValue(CE, SF);
1274 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1275 return getConstantValue(CPV);
1276 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1277 return PTOGV(getPointerToGlobal(GV));
1278 } else {
1279 return SF.Values[V];
1280 }
1281}
1282
Chris Lattner92101ac2001-08-23 17:05:04 +00001283//===----------------------------------------------------------------------===//
1284// Dispatch and Execution Code
1285//===----------------------------------------------------------------------===//
1286
Chris Lattner92101ac2001-08-23 17:05:04 +00001287//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001288// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001289//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001290void Interpreter::callFunction(Function *F,
1291 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001292 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1293 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1294 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001295 // Make a new stack frame... and fill it in.
1296 ECStack.push_back(ExecutionContext());
1297 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001298 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001299
1300 // Special handling for external functions.
Reid Spencer5cbf9852007-01-30 20:08:39 +00001301 if (F->isDeclaration()) {
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001302 GenericValue Result = callExternalFunction (F, ArgVals);
1303 // Simulate a 'ret' instruction of the appropriate type.
1304 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1305 return;
1306 }
1307
1308 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001309 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001310 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001311
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001312 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001313 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001314 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001315 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001316
1317 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001318 unsigned i = 0;
Reid Spencer4ccf4622007-04-16 21:50:40 +00001319 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1320 AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001321 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001322
1323 // Handle varargs arguments...
1324 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001325}
1326
Reid Spencer951418b2007-05-16 02:05:13 +00001327
Chris Lattner92101ac2001-08-23 17:05:04 +00001328void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001329 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001330 // Interpret a single instruction & increment the "PC".
1331 ExecutionContext &SF = ECStack.back(); // Current stack frame
1332 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001333
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001334 // Track the number of dynamic instructions executed.
1335 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001336
Bill Wendling480f0932006-11-27 23:54:50 +00001337 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001338 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner27725bf2007-09-21 18:30:39 +00001339#if 0
1340 // This is not safe, as visiting the instruction could lower it and free I.
Reid Spencer951418b2007-05-16 02:05:13 +00001341#ifndef NDEBUG
1342 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1343 I.getType() != Type::VoidTy) {
1344 DOUT << " --> ";
Chris Lattner27725bf2007-09-21 18:30:39 +00001345 const GenericValue &Val = SF.Values[&I];
1346 switch (I.getType()->getTypeID()) {
1347 default: assert(0 && "Invalid GenericValue Type");
1348 case Type::VoidTyID: DOUT << "void"; break;
1349 case Type::FloatTyID: DOUT << "float " << Val.FloatVal; break;
1350 case Type::DoubleTyID: DOUT << "double " << Val.DoubleVal; break;
1351 case Type::PointerTyID: DOUT << "void* " << intptr_t(Val.PointerVal);
1352 break;
1353 case Type::IntegerTyID:
1354 DOUT << "i" << Val.IntVal.getBitWidth() << " "
1355 << Val.IntVal.toStringUnsigned(10)
1356 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
1357 break;
1358 }
Reid Spencer951418b2007-05-16 02:05:13 +00001359 }
1360#endif
Chris Lattner27725bf2007-09-21 18:30:39 +00001361#endif
Chris Lattner92101ac2001-08-23 17:05:04 +00001362 }
1363}