blob: 45180c91f07d5b4e59f00f93071921683dc66a94 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the actual instruction interpreter.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "interpreter"
15#include "Interpreter.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Instructions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/CodeGen/IntrinsicLowering.h"
20#include "llvm/Support/GetElementPtrTypeIterator.h"
21#include "llvm/ADT/APInt.h"
22#include "llvm/ADT/Statistic.h"
Chris Lattner001ea9d2008-07-08 17:25:49 +000023#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Support/Debug.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000025#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/Support/MathExtras.h"
Gabor Greif7ee10f92007-10-11 19:40:35 +000027#include <algorithm>
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000028#include <cmath>
29#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030using namespace llvm;
31
32STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033
Chris Lattner001ea9d2008-07-08 17:25:49 +000034static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
35 cl::desc("make the interpreter print every volatile load and store"));
36
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037//===----------------------------------------------------------------------===//
38// Various Helper Functions
39//===----------------------------------------------------------------------===//
40
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
42 SF.Values[V] = Val;
43}
44
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045//===----------------------------------------------------------------------===//
46// Binary Instruction Implementations
47//===----------------------------------------------------------------------===//
48
49#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
50 case Type::TY##TyID: \
51 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
52 break
53
Dan Gohman7ce405e2009-06-04 22:49:04 +000054static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
55 GenericValue Src2, const Type *Ty) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 switch (Ty->getTypeID()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 IMPLEMENT_BINARY_OPERATOR(+, Float);
58 IMPLEMENT_BINARY_OPERATOR(+, Double);
59 default:
Dan Gohman7ce405e2009-06-04 22:49:04 +000060 cerr << "Unhandled type for FAdd instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +000061 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 }
63}
64
Dan Gohman7ce405e2009-06-04 22:49:04 +000065static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
66 GenericValue Src2, const Type *Ty) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 switch (Ty->getTypeID()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 IMPLEMENT_BINARY_OPERATOR(-, Float);
69 IMPLEMENT_BINARY_OPERATOR(-, Double);
70 default:
Dan Gohman7ce405e2009-06-04 22:49:04 +000071 cerr << "Unhandled type for FSub instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +000072 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 }
74}
75
Dan Gohman7ce405e2009-06-04 22:49:04 +000076static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
77 GenericValue Src2, const Type *Ty) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 switch (Ty->getTypeID()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 IMPLEMENT_BINARY_OPERATOR(*, Float);
80 IMPLEMENT_BINARY_OPERATOR(*, Double);
81 default:
Dan Gohman7ce405e2009-06-04 22:49:04 +000082 cerr << "Unhandled type for FMul instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +000083 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 }
85}
86
87static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
88 GenericValue Src2, const Type *Ty) {
89 switch (Ty->getTypeID()) {
90 IMPLEMENT_BINARY_OPERATOR(/, Float);
91 IMPLEMENT_BINARY_OPERATOR(/, Double);
92 default:
93 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +000094 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 }
96}
97
98static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
99 GenericValue Src2, const Type *Ty) {
100 switch (Ty->getTypeID()) {
101 case Type::FloatTyID:
102 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
103 break;
104 case Type::DoubleTyID:
105 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
106 break;
107 default:
108 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000109 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 }
111}
112
113#define IMPLEMENT_INTEGER_ICMP(OP, TY) \
114 case Type::IntegerTyID: \
115 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
116 break;
117
118// Handle pointers specially because they must be compared with only as much
119// width as the host has. We _do not_ want to be comparing 64 bit values when
120// running on a 32-bit target, otherwise the upper 32 bits might mess up
121// comparisons if they contain garbage.
122#define IMPLEMENT_POINTER_ICMP(OP) \
123 case Type::PointerTyID: \
124 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
125 (void*)(intptr_t)Src2.PointerVal); \
126 break;
127
128static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
129 const Type *Ty) {
130 GenericValue Dest;
131 switch (Ty->getTypeID()) {
132 IMPLEMENT_INTEGER_ICMP(eq,Ty);
133 IMPLEMENT_POINTER_ICMP(==);
134 default:
135 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000136 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 }
138 return Dest;
139}
140
141static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
142 const Type *Ty) {
143 GenericValue Dest;
144 switch (Ty->getTypeID()) {
145 IMPLEMENT_INTEGER_ICMP(ne,Ty);
146 IMPLEMENT_POINTER_ICMP(!=);
147 default:
148 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000149 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 }
151 return Dest;
152}
153
154static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
155 const Type *Ty) {
156 GenericValue Dest;
157 switch (Ty->getTypeID()) {
158 IMPLEMENT_INTEGER_ICMP(ult,Ty);
159 IMPLEMENT_POINTER_ICMP(<);
160 default:
161 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000162 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 }
164 return Dest;
165}
166
167static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
168 const Type *Ty) {
169 GenericValue Dest;
170 switch (Ty->getTypeID()) {
171 IMPLEMENT_INTEGER_ICMP(slt,Ty);
172 IMPLEMENT_POINTER_ICMP(<);
173 default:
174 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000175 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 }
177 return Dest;
178}
179
180static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
181 const Type *Ty) {
182 GenericValue Dest;
183 switch (Ty->getTypeID()) {
184 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
185 IMPLEMENT_POINTER_ICMP(>);
186 default:
187 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000188 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 }
190 return Dest;
191}
192
193static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
194 const Type *Ty) {
195 GenericValue Dest;
196 switch (Ty->getTypeID()) {
197 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
198 IMPLEMENT_POINTER_ICMP(>);
199 default:
200 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000201 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 }
203 return Dest;
204}
205
206static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
207 const Type *Ty) {
208 GenericValue Dest;
209 switch (Ty->getTypeID()) {
210 IMPLEMENT_INTEGER_ICMP(ule,Ty);
211 IMPLEMENT_POINTER_ICMP(<=);
212 default:
213 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000214 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 }
216 return Dest;
217}
218
219static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
220 const Type *Ty) {
221 GenericValue Dest;
222 switch (Ty->getTypeID()) {
223 IMPLEMENT_INTEGER_ICMP(sle,Ty);
224 IMPLEMENT_POINTER_ICMP(<=);
225 default:
226 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000227 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 }
229 return Dest;
230}
231
232static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
233 const Type *Ty) {
234 GenericValue Dest;
235 switch (Ty->getTypeID()) {
236 IMPLEMENT_INTEGER_ICMP(uge,Ty);
237 IMPLEMENT_POINTER_ICMP(>=);
238 default:
239 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000240 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 }
242 return Dest;
243}
244
245static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
246 const Type *Ty) {
247 GenericValue Dest;
248 switch (Ty->getTypeID()) {
249 IMPLEMENT_INTEGER_ICMP(sge,Ty);
250 IMPLEMENT_POINTER_ICMP(>=);
251 default:
252 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000253 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 }
255 return Dest;
256}
257
258void Interpreter::visitICmpInst(ICmpInst &I) {
259 ExecutionContext &SF = ECStack.back();
260 const Type *Ty = I.getOperand(0)->getType();
261 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
262 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
263 GenericValue R; // Result
264
265 switch (I.getPredicate()) {
266 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
267 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
268 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
269 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
270 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
271 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
272 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
273 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
274 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
275 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
276 default:
277 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
Edwin Törökbd448e32009-07-14 16:55:14 +0000278 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 }
280
281 SetValue(&I, R, SF);
282}
283
284#define IMPLEMENT_FCMP(OP, TY) \
285 case Type::TY##TyID: \
286 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
287 break
288
289static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
290 const Type *Ty) {
291 GenericValue Dest;
292 switch (Ty->getTypeID()) {
293 IMPLEMENT_FCMP(==, Float);
294 IMPLEMENT_FCMP(==, Double);
295 default:
296 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000297 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 }
299 return Dest;
300}
301
302static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
303 const Type *Ty) {
304 GenericValue Dest;
305 switch (Ty->getTypeID()) {
306 IMPLEMENT_FCMP(!=, Float);
307 IMPLEMENT_FCMP(!=, Double);
308
309 default:
310 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000311 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 }
313 return Dest;
314}
315
316static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
317 const Type *Ty) {
318 GenericValue Dest;
319 switch (Ty->getTypeID()) {
320 IMPLEMENT_FCMP(<=, Float);
321 IMPLEMENT_FCMP(<=, Double);
322 default:
323 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000324 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 }
326 return Dest;
327}
328
329static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
330 const Type *Ty) {
331 GenericValue Dest;
332 switch (Ty->getTypeID()) {
333 IMPLEMENT_FCMP(>=, Float);
334 IMPLEMENT_FCMP(>=, Double);
335 default:
336 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000337 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 }
339 return Dest;
340}
341
342static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
343 const Type *Ty) {
344 GenericValue Dest;
345 switch (Ty->getTypeID()) {
346 IMPLEMENT_FCMP(<, Float);
347 IMPLEMENT_FCMP(<, Double);
348 default:
349 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000350 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 }
352 return Dest;
353}
354
355static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
356 const Type *Ty) {
357 GenericValue Dest;
358 switch (Ty->getTypeID()) {
359 IMPLEMENT_FCMP(>, Float);
360 IMPLEMENT_FCMP(>, Double);
361 default:
362 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000363 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 }
365 return Dest;
366}
367
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000368#define IMPLEMENT_UNORDERED(TY, X,Y) \
Owen Anderson35b47072009-08-13 21:58:54 +0000369 if (TY == Type::getFloatTy(Ty->getContext())) { \
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000370 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
371 Dest.IntVal = APInt(1,true); \
372 return Dest; \
373 } \
374 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
375 Dest.IntVal = APInt(1,true); \
376 return Dest; \
377 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378
379
380static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
381 const Type *Ty) {
382 GenericValue Dest;
383 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
384 return executeFCMP_OEQ(Src1, Src2, Ty);
385}
386
387static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
388 const Type *Ty) {
389 GenericValue Dest;
390 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
391 return executeFCMP_ONE(Src1, Src2, Ty);
392}
393
394static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
395 const Type *Ty) {
396 GenericValue Dest;
397 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
398 return executeFCMP_OLE(Src1, Src2, Ty);
399}
400
401static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
402 const Type *Ty) {
403 GenericValue Dest;
404 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
405 return executeFCMP_OGE(Src1, Src2, Ty);
406}
407
408static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
409 const Type *Ty) {
410 GenericValue Dest;
411 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
412 return executeFCMP_OLT(Src1, Src2, Ty);
413}
414
415static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
416 const Type *Ty) {
417 GenericValue Dest;
418 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
419 return executeFCMP_OGT(Src1, Src2, Ty);
420}
421
422static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
423 const Type *Ty) {
424 GenericValue Dest;
Owen Anderson35b47072009-08-13 21:58:54 +0000425 if (Ty == Type::getFloatTy(Ty->getContext()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
427 Src2.FloatVal == Src2.FloatVal));
428 else
429 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
430 Src2.DoubleVal == Src2.DoubleVal));
431 return Dest;
432}
433
434static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
435 const Type *Ty) {
436 GenericValue Dest;
Owen Anderson35b47072009-08-13 21:58:54 +0000437 if (Ty == Type::getFloatTy(Ty->getContext()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
439 Src2.FloatVal != Src2.FloatVal));
440 else
441 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
442 Src2.DoubleVal != Src2.DoubleVal));
443 return Dest;
444}
445
446void Interpreter::visitFCmpInst(FCmpInst &I) {
447 ExecutionContext &SF = ECStack.back();
448 const Type *Ty = I.getOperand(0)->getType();
449 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
450 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
451 GenericValue R; // Result
452
453 switch (I.getPredicate()) {
454 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
455 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
456 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
457 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
458 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
459 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
460 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
461 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
462 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
463 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
464 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
465 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
466 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
467 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
468 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
469 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
470 default:
471 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
Edwin Törökbd448e32009-07-14 16:55:14 +0000472 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473 }
474
475 SetValue(&I, R, SF);
476}
477
478static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
479 GenericValue Src2, const Type *Ty) {
480 GenericValue Result;
481 switch (predicate) {
482 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
483 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
484 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
485 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
486 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
487 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
488 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
489 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
490 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
491 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
492 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
493 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
494 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
495 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
496 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
497 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
498 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
499 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
500 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
501 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
502 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
503 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
504 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
505 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
506 case FCmpInst::FCMP_FALSE: {
507 GenericValue Result;
508 Result.IntVal = APInt(1, false);
509 return Result;
510 }
511 case FCmpInst::FCMP_TRUE: {
512 GenericValue Result;
513 Result.IntVal = APInt(1, true);
514 return Result;
515 }
516 default:
517 cerr << "Unhandled Cmp predicate\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000518 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 }
520}
521
522void Interpreter::visitBinaryOperator(BinaryOperator &I) {
523 ExecutionContext &SF = ECStack.back();
524 const Type *Ty = I.getOperand(0)->getType();
525 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
526 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
527 GenericValue R; // Result
528
529 switch (I.getOpcode()) {
Dan Gohman7ce405e2009-06-04 22:49:04 +0000530 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
531 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
532 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
533 case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;
534 case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;
535 case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;
536 case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;
537 case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
539 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
540 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
541 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
542 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
543 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
544 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
545 default:
546 cerr << "Don't know how to handle this binary operator!\n-->" << I;
Edwin Törökbd448e32009-07-14 16:55:14 +0000547 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 }
549
550 SetValue(&I, R, SF);
551}
552
553static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
554 GenericValue Src3) {
555 return Src1.IntVal == 0 ? Src3 : Src2;
556}
557
558void Interpreter::visitSelectInst(SelectInst &I) {
559 ExecutionContext &SF = ECStack.back();
560 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
561 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
562 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
563 GenericValue R = executeSelectInst(Src1, Src2, Src3);
564 SetValue(&I, R, SF);
565}
566
567
568//===----------------------------------------------------------------------===//
569// Terminator Instruction Implementations
570//===----------------------------------------------------------------------===//
571
572void Interpreter::exitCalled(GenericValue GV) {
573 // runAtExitHandlers() assumes there are no stack frames, but
574 // if exit() was called, then it had a stack frame. Blow away
575 // the stack before interpreting atexit handlers.
576 ECStack.clear ();
577 runAtExitHandlers ();
578 exit (GV.IntVal.zextOrTrunc(32).getZExtValue());
579}
580
581/// Pop the last stack frame off of ECStack and then copy the result
582/// back into the result variable if we are not returning void. The
583/// result variable may be the ExitValue, or the Value of the calling
584/// CallInst if there was a previous stack frame. This method may
585/// invalidate any ECStack iterators you have. This method also takes
586/// care of switching to the normal destination BB, if we are returning
587/// from an invoke.
588///
589void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
590 GenericValue Result) {
591 // Pop the current stack frame.
592 ECStack.pop_back();
593
594 if (ECStack.empty()) { // Finished main. Put result into exit code...
595 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
596 ExitValue = Result; // Capture the exit value of the program
597 } else {
598 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
599 }
600 } else {
601 // If we have a previous stack frame, and we have a previous call,
602 // fill in the return value...
603 ExecutionContext &CallingSF = ECStack.back();
604 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Owen Anderson35b47072009-08-13 21:58:54 +0000605 // Save result...
606 if (CallingSF.Caller.getType() != Type::getVoidTy(RetTy->getContext()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 SetValue(I, Result, CallingSF);
608 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
609 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
610 CallingSF.Caller = CallSite(); // We returned from the call...
611 }
612 }
613}
614
615void Interpreter::visitReturnInst(ReturnInst &I) {
616 ExecutionContext &SF = ECStack.back();
Owen Anderson35b47072009-08-13 21:58:54 +0000617 const Type *RetTy = Type::getVoidTy(I.getContext());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 GenericValue Result;
619
620 // Save away the return value... (if we are not 'ret void')
621 if (I.getNumOperands()) {
622 RetTy = I.getReturnValue()->getType();
623 Result = getOperandValue(I.getReturnValue(), SF);
624 }
625
626 popStackAndReturnValueToCaller(RetTy, Result);
627}
628
629void Interpreter::visitUnwindInst(UnwindInst &I) {
630 // Unwind stack
631 Instruction *Inst;
632 do {
633 ECStack.pop_back ();
634 if (ECStack.empty ())
Edwin Törökced9ff82009-07-11 13:10:19 +0000635 llvm_report_error("Empty stack during unwind!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 Inst = ECStack.back ().Caller.getInstruction ();
637 } while (!(Inst && isa<InvokeInst> (Inst)));
638
639 // Return from invoke
640 ExecutionContext &InvokingSF = ECStack.back ();
641 InvokingSF.Caller = CallSite ();
642
643 // Go to exceptional destination BB of invoke instruction
644 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
645}
646
647void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Benjamin Kramer98c00ab2009-08-03 13:33:33 +0000648 llvm_report_error("Program executed an 'unreachable' instruction!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649}
650
651void Interpreter::visitBranchInst(BranchInst &I) {
652 ExecutionContext &SF = ECStack.back();
653 BasicBlock *Dest;
654
655 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
656 if (!I.isUnconditional()) {
657 Value *Cond = I.getCondition();
658 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
659 Dest = I.getSuccessor(1);
660 }
661 SwitchToNewBasicBlock(Dest, SF);
662}
663
664void Interpreter::visitSwitchInst(SwitchInst &I) {
665 ExecutionContext &SF = ECStack.back();
666 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
667 const Type *ElTy = I.getOperand(0)->getType();
668
669 // Check to see if any of the cases match...
670 BasicBlock *Dest = 0;
671 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
672 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
673 .IntVal != 0) {
674 Dest = cast<BasicBlock>(I.getOperand(i+1));
675 break;
676 }
677
678 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
679 SwitchToNewBasicBlock(Dest, SF);
680}
681
682// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
683// This function handles the actual updating of block and instruction iterators
684// as well as execution of all of the PHI nodes in the destination block.
685//
686// This method does this because all of the PHI nodes must be executed
687// atomically, reading their inputs before any of the results are updated. Not
688// doing this can cause problems if the PHI nodes depend on other PHI nodes for
689// their inputs. If the input PHI node is updated before it is read, incorrect
690// results can happen. Thus we use a two phase approach.
691//
692void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
693 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
694 SF.CurBB = Dest; // Update CurBB to branch destination
695 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
696
697 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
698
699 // Loop over all of the PHI nodes in the current block, reading their inputs.
700 std::vector<GenericValue> ResultValues;
701
702 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
703 // Search for the value corresponding to this previous bb...
704 int i = PN->getBasicBlockIndex(PrevBB);
705 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
706 Value *IncomingValue = PN->getIncomingValue(i);
707
708 // Save the incoming value for this PHI node...
709 ResultValues.push_back(getOperandValue(IncomingValue, SF));
710 }
711
712 // Now loop over all of the PHI nodes setting their values...
713 SF.CurInst = SF.CurBB->begin();
714 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
715 PHINode *PN = cast<PHINode>(SF.CurInst);
716 SetValue(PN, ResultValues[i], SF);
717 }
718}
719
720//===----------------------------------------------------------------------===//
721// Memory Instruction Implementations
722//===----------------------------------------------------------------------===//
723
724void Interpreter::visitAllocationInst(AllocationInst &I) {
725 ExecutionContext &SF = ECStack.back();
726
727 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
728
729 // Get the number of elements being allocated by the array...
730 unsigned NumElements =
731 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
732
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000733 unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734
Gabor Greif7ee10f92007-10-11 19:40:35 +0000735 // Avoid malloc-ing zero bytes, use max()...
736 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737
738 // Allocate enough memory to hold the type...
739 void *Memory = malloc(MemToAlloc);
740
741 DOUT << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
742 << NumElements << " (Total: " << MemToAlloc << ") at "
743 << uintptr_t(Memory) << '\n';
744
745 GenericValue Result = PTOGV(Memory);
746 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
747 SetValue(&I, Result, SF);
748
749 if (I.getOpcode() == Instruction::Alloca)
750 ECStack.back().Allocas.add(Memory);
751}
752
753void Interpreter::visitFreeInst(FreeInst &I) {
754 ExecutionContext &SF = ECStack.back();
755 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
756 GenericValue Value = getOperandValue(I.getOperand(0), SF);
757 // TODO: Check to make sure memory is allocated
758 free(GVTOP(Value)); // Free memory
759}
760
761// getElementOffset - The workhorse for getelementptr.
762//
763GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
764 gep_type_iterator E,
765 ExecutionContext &SF) {
766 assert(isa<PointerType>(Ptr->getType()) &&
767 "Cannot getElementOffset of a nonpointer type!");
768
769 uint64_t Total = 0;
770
771 for (; I != E; ++I) {
772 if (const StructType *STy = dyn_cast<StructType>(*I)) {
773 const StructLayout *SLO = TD.getStructLayout(STy);
774
775 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
776 unsigned Index = unsigned(CPU->getZExtValue());
777
778 Total += SLO->getElementOffset(Index);
779 } else {
780 const SequentialType *ST = cast<SequentialType>(*I);
781 // Get the index number for the array... which must be long type...
782 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
783
784 int64_t Idx;
785 unsigned BitWidth =
786 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
787 if (BitWidth == 32)
788 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
Chris Lattner3203f192008-04-06 21:50:58 +0000789 else {
790 assert(BitWidth == 64 && "Invalid index type for getelementptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
Chris Lattner3203f192008-04-06 21:50:58 +0000792 }
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000793 Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 }
795 }
796
797 GenericValue Result;
798 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
799 DOUT << "GEP Index " << Total << " bytes.\n";
800 return Result;
801}
802
803void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
804 ExecutionContext &SF = ECStack.back();
Owen Anderson0e17f632009-06-26 16:46:15 +0000805 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 gep_type_begin(I), gep_type_end(I), SF), SF);
807}
808
809void Interpreter::visitLoadInst(LoadInst &I) {
810 ExecutionContext &SF = ECStack.back();
811 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
812 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
813 GenericValue Result;
814 LoadValueFromMemory(Result, Ptr, I.getType());
815 SetValue(&I, Result, SF);
Chris Lattner001ea9d2008-07-08 17:25:49 +0000816 if (I.isVolatile() && PrintVolatile)
817 cerr << "Volatile load " << I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818}
819
820void Interpreter::visitStoreInst(StoreInst &I) {
821 ExecutionContext &SF = ECStack.back();
822 GenericValue Val = getOperandValue(I.getOperand(0), SF);
823 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
824 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
825 I.getOperand(0)->getType());
Chris Lattner001ea9d2008-07-08 17:25:49 +0000826 if (I.isVolatile() && PrintVolatile)
827 cerr << "Volatile store: " << I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000828}
829
830//===----------------------------------------------------------------------===//
831// Miscellaneous Instruction Implementations
832//===----------------------------------------------------------------------===//
833
834void Interpreter::visitCallSite(CallSite CS) {
835 ExecutionContext &SF = ECStack.back();
836
837 // Check to see if this is an intrinsic function call...
838 Function *F = CS.getCalledFunction();
839 if (F && F->isDeclaration ())
840 switch (F->getIntrinsicID()) {
841 case Intrinsic::not_intrinsic:
842 break;
843 case Intrinsic::vastart: { // va_start
844 GenericValue ArgIndex;
845 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
846 ArgIndex.UIntPairVal.second = 0;
847 SetValue(CS.getInstruction(), ArgIndex, SF);
848 return;
849 }
850 case Intrinsic::vaend: // va_end is a noop for the interpreter
851 return;
852 case Intrinsic::vacopy: // va_copy: dest = src
853 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
854 return;
855 default:
856 // If it is an unknown intrinsic function, use the intrinsic lowering
857 // class to transform it into hopefully tasty LLVM code.
858 //
859 BasicBlock::iterator me(CS.getInstruction());
860 BasicBlock *Parent = CS.getInstruction()->getParent();
861 bool atBegin(Parent->begin() == me);
862 if (!atBegin)
863 --me;
864 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
865
866 // Restore the CurInst pointer to the first instruction newly inserted, if
867 // any.
868 if (atBegin) {
869 SF.CurInst = Parent->begin();
870 } else {
871 SF.CurInst = me;
872 ++SF.CurInst;
873 }
874 return;
875 }
876
877
878 SF.Caller = CS;
879 std::vector<GenericValue> ArgVals;
880 const unsigned NumArgs = SF.Caller.arg_size();
881 ArgVals.reserve(NumArgs);
882 uint16_t pNum = 1;
883 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
884 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
885 Value *V = *i;
886 ArgVals.push_back(getOperandValue(V, SF));
Duncan Sands637ec552007-11-28 17:07:01 +0000887 // Promote all integral types whose size is < sizeof(i32) into i32.
888 // We do this by zero or sign extending the value as appropriate
889 // according to the parameter attributes
890 const Type *Ty = V->getType();
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000891 if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32)) {
Devang Pateld222f862008-09-25 21:00:45 +0000892 if (CS.paramHasAttr(pNum, Attribute::ZExt))
Duncan Sands637ec552007-11-28 17:07:01 +0000893 ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
Devang Pateld222f862008-09-25 21:00:45 +0000894 else if (CS.paramHasAttr(pNum, Attribute::SExt))
Duncan Sands637ec552007-11-28 17:07:01 +0000895 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000896 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000897 }
898
899 // To handle indirect calls, we must get the pointer value from the argument
900 // and treat it as a function pointer.
901 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
902 callFunction((Function*)GVTOP(SRC), ArgVals);
903}
904
905void Interpreter::visitShl(BinaryOperator &I) {
906 ExecutionContext &SF = ECStack.back();
907 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
908 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
909 GenericValue Dest;
Chris Lattner09484b42009-01-16 20:17:02 +0000910 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
911 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
912 else
913 Dest.IntVal = Src1.IntVal;
914
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915 SetValue(&I, Dest, SF);
916}
917
918void Interpreter::visitLShr(BinaryOperator &I) {
919 ExecutionContext &SF = ECStack.back();
920 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
921 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
922 GenericValue Dest;
Chris Lattner09484b42009-01-16 20:17:02 +0000923 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
924 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
925 else
926 Dest.IntVal = Src1.IntVal;
927
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000928 SetValue(&I, Dest, SF);
929}
930
931void Interpreter::visitAShr(BinaryOperator &I) {
932 ExecutionContext &SF = ECStack.back();
933 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
934 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner09484b42009-01-16 20:17:02 +0000935 GenericValue Dest;
936 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
937 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
938 else
939 Dest.IntVal = Src1.IntVal;
940
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000941 SetValue(&I, Dest, SF);
942}
943
944GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
945 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
947 const IntegerType *DITy = cast<IntegerType>(DstTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 unsigned DBitWidth = DITy->getBitWidth();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
950 return Dest;
951}
952
953GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
954 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000955 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
956 const IntegerType *DITy = cast<IntegerType>(DstTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000957 unsigned DBitWidth = DITy->getBitWidth();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000958 Dest.IntVal = Src.IntVal.sext(DBitWidth);
959 return Dest;
960}
961
962GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
963 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000964 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
965 const IntegerType *DITy = cast<IntegerType>(DstTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 unsigned DBitWidth = DITy->getBitWidth();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000967 Dest.IntVal = Src.IntVal.zext(DBitWidth);
968 return Dest;
969}
970
971GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
972 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000973 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Owen Anderson35b47072009-08-13 21:58:54 +0000974 assert(SrcVal->getType() == Type::getDoubleTy(SrcVal->getContext()) &&
975 DstTy == Type::getFloatTy(SrcVal->getContext()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +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) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Owen Anderson35b47072009-08-13 21:58:54 +0000984 assert(SrcVal->getType() == Type::getFloatTy(SrcVal->getContext()) &&
985 DstTy == Type::getDoubleTy(SrcVal->getContext()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 "Invalid FPTrunc instruction");
987 Dest.DoubleVal = (double) Src.FloatVal;
988 return Dest;
989}
990
991GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
992 ExecutionContext &SF) {
993 const Type *SrcTy = SrcVal->getType();
994 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
995 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
996 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
997
998 if (SrcTy->getTypeID() == Type::FloatTyID)
999 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1000 else
1001 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1002 return Dest;
1003}
1004
1005GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1006 ExecutionContext &SF) {
1007 const Type *SrcTy = SrcVal->getType();
1008 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1009 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1010 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1011
1012 if (SrcTy->getTypeID() == Type::FloatTyID)
1013 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1014 else
1015 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1016 return Dest;
1017}
1018
1019GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1020 ExecutionContext &SF) {
1021 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1022 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1023
1024 if (DstTy->getTypeID() == Type::FloatTyID)
1025 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
1026 else
1027 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
1028 return Dest;
1029}
1030
1031GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1032 ExecutionContext &SF) {
1033 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1034 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
1035
1036 if (DstTy->getTypeID() == Type::FloatTyID)
1037 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
1038 else
1039 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
1040 return Dest;
1041
1042}
1043
1044GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1045 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001046 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1047 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner3203f192008-04-06 21:50:58 +00001048 assert(isa<PointerType>(SrcVal->getType()) && "Invalid PtrToInt instruction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049
1050 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1051 return Dest;
1052}
1053
1054GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1055 ExecutionContext &SF) {
1056 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1057 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1058
1059 uint32_t PtrSize = TD.getPointerSizeInBits();
1060 if (PtrSize != Src.IntVal.getBitWidth())
1061 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
1062
1063 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
1064 return Dest;
1065}
1066
1067GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1068 ExecutionContext &SF) {
1069
1070 const Type *SrcTy = SrcVal->getType();
1071 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1072 if (isa<PointerType>(DstTy)) {
1073 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1074 Dest.PointerVal = Src.PointerVal;
1075 } else if (DstTy->isInteger()) {
Owen Anderson35b47072009-08-13 21:58:54 +00001076 if (SrcTy == Type::getFloatTy(SrcVal->getContext())) {
Dan Gohmand06cad62009-04-01 18:45:54 +00001077 Dest.IntVal.zext(sizeof(Src.FloatVal) * CHAR_BIT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078 Dest.IntVal.floatToBits(Src.FloatVal);
Owen Anderson35b47072009-08-13 21:58:54 +00001079 } else if (SrcTy == Type::getDoubleTy(SrcVal->getContext())) {
Dan Gohmand06cad62009-04-01 18:45:54 +00001080 Dest.IntVal.zext(sizeof(Src.DoubleVal) * CHAR_BIT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001081 Dest.IntVal.doubleToBits(Src.DoubleVal);
1082 } else if (SrcTy->isInteger()) {
1083 Dest.IntVal = Src.IntVal;
1084 } else
Edwin Törökbd448e32009-07-14 16:55:14 +00001085 llvm_unreachable("Invalid BitCast");
Owen Anderson35b47072009-08-13 21:58:54 +00001086 } else if (DstTy == Type::getFloatTy(SrcVal->getContext())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001087 if (SrcTy->isInteger())
1088 Dest.FloatVal = Src.IntVal.bitsToFloat();
1089 else
1090 Dest.FloatVal = Src.FloatVal;
Owen Anderson35b47072009-08-13 21:58:54 +00001091 } else if (DstTy == Type::getDoubleTy(SrcVal->getContext())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001092 if (SrcTy->isInteger())
1093 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1094 else
1095 Dest.DoubleVal = Src.DoubleVal;
1096 } else
Edwin Törökbd448e32009-07-14 16:55:14 +00001097 llvm_unreachable("Invalid Bitcast");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001098
1099 return Dest;
1100}
1101
1102void Interpreter::visitTruncInst(TruncInst &I) {
1103 ExecutionContext &SF = ECStack.back();
1104 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1105}
1106
1107void Interpreter::visitSExtInst(SExtInst &I) {
1108 ExecutionContext &SF = ECStack.back();
1109 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1110}
1111
1112void Interpreter::visitZExtInst(ZExtInst &I) {
1113 ExecutionContext &SF = ECStack.back();
1114 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1115}
1116
1117void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1118 ExecutionContext &SF = ECStack.back();
1119 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1120}
1121
1122void Interpreter::visitFPExtInst(FPExtInst &I) {
1123 ExecutionContext &SF = ECStack.back();
1124 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1125}
1126
1127void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1128 ExecutionContext &SF = ECStack.back();
1129 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1130}
1131
1132void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1133 ExecutionContext &SF = ECStack.back();
1134 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1135}
1136
1137void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1138 ExecutionContext &SF = ECStack.back();
1139 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1140}
1141
1142void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1143 ExecutionContext &SF = ECStack.back();
1144 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1145}
1146
1147void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1148 ExecutionContext &SF = ECStack.back();
1149 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1150}
1151
1152void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1153 ExecutionContext &SF = ECStack.back();
1154 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1155}
1156
1157void Interpreter::visitBitCastInst(BitCastInst &I) {
1158 ExecutionContext &SF = ECStack.back();
1159 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1160}
1161
1162#define IMPLEMENT_VAARG(TY) \
1163 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1164
1165void Interpreter::visitVAArgInst(VAArgInst &I) {
1166 ExecutionContext &SF = ECStack.back();
1167
1168 // Get the incoming valist parameter. LLI treats the valist as a
1169 // (ec-stack-depth var-arg-index) pair.
1170 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1171 GenericValue Dest;
1172 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1173 .VarArgs[VAList.UIntPairVal.second];
1174 const Type *Ty = I.getType();
1175 switch (Ty->getTypeID()) {
1176 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
1177 IMPLEMENT_VAARG(Pointer);
1178 IMPLEMENT_VAARG(Float);
1179 IMPLEMENT_VAARG(Double);
1180 default:
1181 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +00001182 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001183 }
1184
1185 // Set the Value of this Instruction.
1186 SetValue(&I, Dest, SF);
1187
1188 // Move the pointer to the next vararg.
1189 ++VAList.UIntPairVal.second;
1190}
1191
1192GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1193 ExecutionContext &SF) {
1194 switch (CE->getOpcode()) {
1195 case Instruction::Trunc:
1196 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1197 case Instruction::ZExt:
1198 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1199 case Instruction::SExt:
1200 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1201 case Instruction::FPTrunc:
1202 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1203 case Instruction::FPExt:
1204 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1205 case Instruction::UIToFP:
1206 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1207 case Instruction::SIToFP:
1208 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1209 case Instruction::FPToUI:
1210 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1211 case Instruction::FPToSI:
1212 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1213 case Instruction::PtrToInt:
1214 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1215 case Instruction::IntToPtr:
1216 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1217 case Instruction::BitCast:
1218 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1219 case Instruction::GetElementPtr:
1220 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1221 gep_type_end(CE), SF);
1222 case Instruction::FCmp:
1223 case Instruction::ICmp:
1224 return executeCmpInst(CE->getPredicate(),
1225 getOperandValue(CE->getOperand(0), SF),
1226 getOperandValue(CE->getOperand(1), SF),
1227 CE->getOperand(0)->getType());
1228 case Instruction::Select:
1229 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1230 getOperandValue(CE->getOperand(1), SF),
1231 getOperandValue(CE->getOperand(2), SF));
1232 default :
1233 break;
1234 }
1235
1236 // The cases below here require a GenericValue parameter for the result
1237 // so we initialize one, compute it and then return it.
1238 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1239 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
1240 GenericValue Dest;
1241 const Type * Ty = CE->getOperand(0)->getType();
1242 switch (CE->getOpcode()) {
Dan Gohman7ce405e2009-06-04 22:49:04 +00001243 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
1244 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
1245 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
1246 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
1247 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
1248 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001249 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1250 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1251 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1252 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1253 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1254 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
Dan Gohman7ce405e2009-06-04 22:49:04 +00001255 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
1256 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
1257 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001258 case Instruction::Shl:
1259 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1260 break;
1261 case Instruction::LShr:
1262 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1263 break;
1264 case Instruction::AShr:
1265 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1266 break;
1267 default:
1268 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +00001269 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001270 return GenericValue();
1271 }
1272 return Dest;
1273}
1274
1275GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1276 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1277 return getConstantExprValue(CE, SF);
1278 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1279 return getConstantValue(CPV);
1280 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1281 return PTOGV(getPointerToGlobal(GV));
1282 } else {
1283 return SF.Values[V];
1284 }
1285}
1286
1287//===----------------------------------------------------------------------===//
1288// Dispatch and Execution Code
1289//===----------------------------------------------------------------------===//
1290
1291//===----------------------------------------------------------------------===//
1292// callFunction - Execute the specified function...
1293//
1294void Interpreter::callFunction(Function *F,
1295 const std::vector<GenericValue> &ArgVals) {
1296 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1297 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1298 "Incorrect number of arguments passed into function call!");
1299 // Make a new stack frame... and fill it in.
1300 ECStack.push_back(ExecutionContext());
1301 ExecutionContext &StackFrame = ECStack.back();
1302 StackFrame.CurFunction = F;
1303
1304 // Special handling for external functions.
1305 if (F->isDeclaration()) {
1306 GenericValue Result = callExternalFunction (F, ArgVals);
1307 // Simulate a 'ret' instruction of the appropriate type.
1308 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1309 return;
1310 }
1311
1312 // Get pointers to first LLVM BB & Instruction in function.
1313 StackFrame.CurBB = F->begin();
1314 StackFrame.CurInst = StackFrame.CurBB->begin();
1315
1316 // Run through the function arguments and initialize their values...
1317 assert((ArgVals.size() == F->arg_size() ||
1318 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1319 "Invalid number of values passed to function invocation!");
1320
1321 // Handle non-varargs arguments...
1322 unsigned i = 0;
1323 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1324 AI != E; ++AI, ++i)
1325 SetValue(AI, ArgVals[i], StackFrame);
1326
1327 // Handle varargs arguments...
1328 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1329}
1330
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001331
1332void Interpreter::run() {
1333 while (!ECStack.empty()) {
1334 // Interpret a single instruction & increment the "PC".
1335 ExecutionContext &SF = ECStack.back(); // Current stack frame
1336 Instruction &I = *SF.CurInst++; // Increment before execute
1337
1338 // Track the number of dynamic instructions executed.
1339 ++NumDynamicInsts;
1340
1341 DOUT << "About to interpret: " << I;
1342 visit(I); // Dispatch to one of the visit* methods...
Chris Lattnerfc65a9f2007-09-21 18:30:39 +00001343#if 0
1344 // This is not safe, as visiting the instruction could lower it and free I.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001345#ifndef NDEBUG
1346 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1347 I.getType() != Type::VoidTy) {
1348 DOUT << " --> ";
Chris Lattnerfc65a9f2007-09-21 18:30:39 +00001349 const GenericValue &Val = SF.Values[&I];
1350 switch (I.getType()->getTypeID()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00001351 default: llvm_unreachable("Invalid GenericValue Type");
Chris Lattnerfc65a9f2007-09-21 18:30:39 +00001352 case Type::VoidTyID: DOUT << "void"; break;
1353 case Type::FloatTyID: DOUT << "float " << Val.FloatVal; break;
1354 case Type::DoubleTyID: DOUT << "double " << Val.DoubleVal; break;
1355 case Type::PointerTyID: DOUT << "void* " << intptr_t(Val.PointerVal);
1356 break;
1357 case Type::IntegerTyID:
1358 DOUT << "i" << Val.IntVal.getBitWidth() << " "
1359 << Val.IntVal.toStringUnsigned(10)
1360 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
1361 break;
1362 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001363 }
1364#endif
Chris Lattnerfc65a9f2007-09-21 18:30:39 +00001365#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366 }
1367}