blob: 29837ffcfec74a1aa9f295f1c4123cce545f922b [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"
19#include "llvm/ParameterAttributes.h"
20#include "llvm/CodeGen/IntrinsicLowering.h"
21#include "llvm/Support/GetElementPtrTypeIterator.h"
22#include "llvm/ADT/APInt.h"
23#include "llvm/ADT/Statistic.h"
Chris Lattner001ea9d2008-07-08 17:25:49 +000024#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Support/Debug.h"
26#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");
33static Interpreter *TheEE = 0;
34
Chris Lattner001ea9d2008-07-08 17:25:49 +000035static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
36 cl::desc("make the interpreter print every volatile load and store"));
37
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038//===----------------------------------------------------------------------===//
39// Various Helper Functions
40//===----------------------------------------------------------------------===//
41
42static inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
43 // Determine if the value is signed or not
44 bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
45 // If its signed, extend the sign bits
46 if (isSigned)
47 Val |= ~ITy->getBitMask();
48 return Val;
49}
50
51static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
52 SF.Values[V] = Val;
53}
54
55void Interpreter::initializeExecutionEngine() {
56 TheEE = this;
57}
58
59//===----------------------------------------------------------------------===//
60// Binary Instruction Implementations
61//===----------------------------------------------------------------------===//
62
63#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
64 case Type::TY##TyID: \
65 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
66 break
67
68#define IMPLEMENT_INTEGER_BINOP1(OP, TY) \
69 case Type::IntegerTyID: { \
70 Dest.IntVal = Src1.IntVal OP Src2.IntVal; \
71 break; \
72 }
73
74
75static void executeAddInst(GenericValue &Dest, GenericValue Src1,
76 GenericValue Src2, const Type *Ty) {
77 switch (Ty->getTypeID()) {
78 IMPLEMENT_INTEGER_BINOP1(+, Ty);
79 IMPLEMENT_BINARY_OPERATOR(+, Float);
80 IMPLEMENT_BINARY_OPERATOR(+, Double);
81 default:
82 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
83 abort();
84 }
85}
86
87static void executeSubInst(GenericValue &Dest, GenericValue Src1,
88 GenericValue Src2, const Type *Ty) {
89 switch (Ty->getTypeID()) {
90 IMPLEMENT_INTEGER_BINOP1(-, Ty);
91 IMPLEMENT_BINARY_OPERATOR(-, Float);
92 IMPLEMENT_BINARY_OPERATOR(-, Double);
93 default:
94 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
95 abort();
96 }
97}
98
99static void executeMulInst(GenericValue &Dest, GenericValue Src1,
100 GenericValue Src2, const Type *Ty) {
101 switch (Ty->getTypeID()) {
102 IMPLEMENT_INTEGER_BINOP1(*, Ty);
103 IMPLEMENT_BINARY_OPERATOR(*, Float);
104 IMPLEMENT_BINARY_OPERATOR(*, Double);
105 default:
106 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
107 abort();
108 }
109}
110
111static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
112 GenericValue Src2, const Type *Ty) {
113 switch (Ty->getTypeID()) {
114 IMPLEMENT_BINARY_OPERATOR(/, Float);
115 IMPLEMENT_BINARY_OPERATOR(/, Double);
116 default:
117 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
118 abort();
119 }
120}
121
122static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
123 GenericValue Src2, const Type *Ty) {
124 switch (Ty->getTypeID()) {
125 case Type::FloatTyID:
126 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
127 break;
128 case Type::DoubleTyID:
129 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
130 break;
131 default:
132 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
133 abort();
134 }
135}
136
137#define IMPLEMENT_INTEGER_ICMP(OP, TY) \
138 case Type::IntegerTyID: \
139 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
140 break;
141
142// Handle pointers specially because they must be compared with only as much
143// width as the host has. We _do not_ want to be comparing 64 bit values when
144// running on a 32-bit target, otherwise the upper 32 bits might mess up
145// comparisons if they contain garbage.
146#define IMPLEMENT_POINTER_ICMP(OP) \
147 case Type::PointerTyID: \
148 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
149 (void*)(intptr_t)Src2.PointerVal); \
150 break;
151
152static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
153 const Type *Ty) {
154 GenericValue Dest;
155 switch (Ty->getTypeID()) {
156 IMPLEMENT_INTEGER_ICMP(eq,Ty);
157 IMPLEMENT_POINTER_ICMP(==);
158 default:
159 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
160 abort();
161 }
162 return Dest;
163}
164
165static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
166 const Type *Ty) {
167 GenericValue Dest;
168 switch (Ty->getTypeID()) {
169 IMPLEMENT_INTEGER_ICMP(ne,Ty);
170 IMPLEMENT_POINTER_ICMP(!=);
171 default:
172 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
173 abort();
174 }
175 return Dest;
176}
177
178static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
179 const Type *Ty) {
180 GenericValue Dest;
181 switch (Ty->getTypeID()) {
182 IMPLEMENT_INTEGER_ICMP(ult,Ty);
183 IMPLEMENT_POINTER_ICMP(<);
184 default:
185 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
186 abort();
187 }
188 return Dest;
189}
190
191static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
192 const Type *Ty) {
193 GenericValue Dest;
194 switch (Ty->getTypeID()) {
195 IMPLEMENT_INTEGER_ICMP(slt,Ty);
196 IMPLEMENT_POINTER_ICMP(<);
197 default:
198 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
199 abort();
200 }
201 return Dest;
202}
203
204static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
205 const Type *Ty) {
206 GenericValue Dest;
207 switch (Ty->getTypeID()) {
208 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
209 IMPLEMENT_POINTER_ICMP(>);
210 default:
211 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
212 abort();
213 }
214 return Dest;
215}
216
217static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
218 const Type *Ty) {
219 GenericValue Dest;
220 switch (Ty->getTypeID()) {
221 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
222 IMPLEMENT_POINTER_ICMP(>);
223 default:
224 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
225 abort();
226 }
227 return Dest;
228}
229
230static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
231 const Type *Ty) {
232 GenericValue Dest;
233 switch (Ty->getTypeID()) {
234 IMPLEMENT_INTEGER_ICMP(ule,Ty);
235 IMPLEMENT_POINTER_ICMP(<=);
236 default:
237 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
238 abort();
239 }
240 return Dest;
241}
242
243static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
244 const Type *Ty) {
245 GenericValue Dest;
246 switch (Ty->getTypeID()) {
247 IMPLEMENT_INTEGER_ICMP(sle,Ty);
248 IMPLEMENT_POINTER_ICMP(<=);
249 default:
250 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
251 abort();
252 }
253 return Dest;
254}
255
256static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
257 const Type *Ty) {
258 GenericValue Dest;
259 switch (Ty->getTypeID()) {
260 IMPLEMENT_INTEGER_ICMP(uge,Ty);
261 IMPLEMENT_POINTER_ICMP(>=);
262 default:
263 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
264 abort();
265 }
266 return Dest;
267}
268
269static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
270 const Type *Ty) {
271 GenericValue Dest;
272 switch (Ty->getTypeID()) {
273 IMPLEMENT_INTEGER_ICMP(sge,Ty);
274 IMPLEMENT_POINTER_ICMP(>=);
275 default:
276 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
277 abort();
278 }
279 return Dest;
280}
281
282void Interpreter::visitICmpInst(ICmpInst &I) {
283 ExecutionContext &SF = ECStack.back();
284 const Type *Ty = I.getOperand(0)->getType();
285 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
286 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
287 GenericValue R; // Result
288
289 switch (I.getPredicate()) {
290 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
291 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
292 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
293 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
294 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
295 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
296 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
297 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
298 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
299 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
300 default:
301 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
302 abort();
303 }
304
305 SetValue(&I, R, SF);
306}
307
308#define IMPLEMENT_FCMP(OP, TY) \
309 case Type::TY##TyID: \
310 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
311 break
312
313static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
314 const Type *Ty) {
315 GenericValue Dest;
316 switch (Ty->getTypeID()) {
317 IMPLEMENT_FCMP(==, Float);
318 IMPLEMENT_FCMP(==, Double);
319 default:
320 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
321 abort();
322 }
323 return Dest;
324}
325
326static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
327 const Type *Ty) {
328 GenericValue Dest;
329 switch (Ty->getTypeID()) {
330 IMPLEMENT_FCMP(!=, Float);
331 IMPLEMENT_FCMP(!=, Double);
332
333 default:
334 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
335 abort();
336 }
337 return Dest;
338}
339
340static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
341 const Type *Ty) {
342 GenericValue Dest;
343 switch (Ty->getTypeID()) {
344 IMPLEMENT_FCMP(<=, Float);
345 IMPLEMENT_FCMP(<=, Double);
346 default:
347 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
348 abort();
349 }
350 return Dest;
351}
352
353static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
354 const Type *Ty) {
355 GenericValue Dest;
356 switch (Ty->getTypeID()) {
357 IMPLEMENT_FCMP(>=, Float);
358 IMPLEMENT_FCMP(>=, Double);
359 default:
360 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
361 abort();
362 }
363 return Dest;
364}
365
366static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
367 const Type *Ty) {
368 GenericValue Dest;
369 switch (Ty->getTypeID()) {
370 IMPLEMENT_FCMP(<, Float);
371 IMPLEMENT_FCMP(<, Double);
372 default:
373 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
374 abort();
375 }
376 return Dest;
377}
378
379static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
380 const Type *Ty) {
381 GenericValue Dest;
382 switch (Ty->getTypeID()) {
383 IMPLEMENT_FCMP(>, Float);
384 IMPLEMENT_FCMP(>, Double);
385 default:
386 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
387 abort();
388 }
389 return Dest;
390}
391
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000392#define IMPLEMENT_UNORDERED(TY, X,Y) \
393 if (TY == Type::FloatTy) { \
394 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
395 Dest.IntVal = APInt(1,true); \
396 return Dest; \
397 } \
398 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
399 Dest.IntVal = APInt(1,true); \
400 return Dest; \
401 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402
403
404static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
405 const Type *Ty) {
406 GenericValue Dest;
407 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
408 return executeFCMP_OEQ(Src1, Src2, Ty);
409}
410
411static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
412 const Type *Ty) {
413 GenericValue Dest;
414 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
415 return executeFCMP_ONE(Src1, Src2, Ty);
416}
417
418static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
419 const Type *Ty) {
420 GenericValue Dest;
421 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
422 return executeFCMP_OLE(Src1, Src2, Ty);
423}
424
425static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
426 const Type *Ty) {
427 GenericValue Dest;
428 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
429 return executeFCMP_OGE(Src1, Src2, Ty);
430}
431
432static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
433 const Type *Ty) {
434 GenericValue Dest;
435 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
436 return executeFCMP_OLT(Src1, Src2, Ty);
437}
438
439static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
440 const Type *Ty) {
441 GenericValue Dest;
442 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
443 return executeFCMP_OGT(Src1, Src2, Ty);
444}
445
446static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
447 const Type *Ty) {
448 GenericValue Dest;
449 if (Ty == Type::FloatTy)
450 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
451 Src2.FloatVal == Src2.FloatVal));
452 else
453 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
454 Src2.DoubleVal == Src2.DoubleVal));
455 return Dest;
456}
457
458static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
459 const Type *Ty) {
460 GenericValue Dest;
461 if (Ty == Type::FloatTy)
462 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
463 Src2.FloatVal != Src2.FloatVal));
464 else
465 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
466 Src2.DoubleVal != Src2.DoubleVal));
467 return Dest;
468}
469
470void Interpreter::visitFCmpInst(FCmpInst &I) {
471 ExecutionContext &SF = ECStack.back();
472 const Type *Ty = I.getOperand(0)->getType();
473 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
474 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
475 GenericValue R; // Result
476
477 switch (I.getPredicate()) {
478 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
479 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
480 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
481 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
482 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
483 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
484 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
485 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
486 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
487 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
488 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
489 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
490 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
491 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
492 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
493 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
494 default:
495 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
496 abort();
497 }
498
499 SetValue(&I, R, SF);
500}
501
502static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
503 GenericValue Src2, const Type *Ty) {
504 GenericValue Result;
505 switch (predicate) {
506 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
507 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
508 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
509 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
510 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
511 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
512 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
513 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
514 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
515 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
516 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
517 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
518 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
519 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
520 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
521 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
522 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
523 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
524 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
525 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
526 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
527 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
528 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
529 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
530 case FCmpInst::FCMP_FALSE: {
531 GenericValue Result;
532 Result.IntVal = APInt(1, false);
533 return Result;
534 }
535 case FCmpInst::FCMP_TRUE: {
536 GenericValue Result;
537 Result.IntVal = APInt(1, true);
538 return Result;
539 }
540 default:
541 cerr << "Unhandled Cmp predicate\n";
542 abort();
543 }
544}
545
546void Interpreter::visitBinaryOperator(BinaryOperator &I) {
547 ExecutionContext &SF = ECStack.back();
548 const Type *Ty = I.getOperand(0)->getType();
549 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
550 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
551 GenericValue R; // Result
552
553 switch (I.getOpcode()) {
554 case Instruction::Add: executeAddInst (R, Src1, Src2, Ty); break;
555 case Instruction::Sub: executeSubInst (R, Src1, Src2, Ty); break;
556 case Instruction::Mul: executeMulInst (R, Src1, Src2, Ty); break;
557 case Instruction::FDiv: executeFDivInst (R, Src1, Src2, Ty); break;
558 case Instruction::FRem: executeFRemInst (R, Src1, Src2, Ty); break;
559 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
560 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
561 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
562 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
563 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
564 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
565 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
566 default:
567 cerr << "Don't know how to handle this binary operator!\n-->" << I;
568 abort();
569 }
570
571 SetValue(&I, R, SF);
572}
573
574static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
575 GenericValue Src3) {
576 return Src1.IntVal == 0 ? Src3 : Src2;
577}
578
579void Interpreter::visitSelectInst(SelectInst &I) {
580 ExecutionContext &SF = ECStack.back();
581 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
582 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
583 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
584 GenericValue R = executeSelectInst(Src1, Src2, Src3);
585 SetValue(&I, R, SF);
586}
587
588
589//===----------------------------------------------------------------------===//
590// Terminator Instruction Implementations
591//===----------------------------------------------------------------------===//
592
593void Interpreter::exitCalled(GenericValue GV) {
594 // runAtExitHandlers() assumes there are no stack frames, but
595 // if exit() was called, then it had a stack frame. Blow away
596 // the stack before interpreting atexit handlers.
597 ECStack.clear ();
598 runAtExitHandlers ();
599 exit (GV.IntVal.zextOrTrunc(32).getZExtValue());
600}
601
602/// Pop the last stack frame off of ECStack and then copy the result
603/// back into the result variable if we are not returning void. The
604/// result variable may be the ExitValue, or the Value of the calling
605/// CallInst if there was a previous stack frame. This method may
606/// invalidate any ECStack iterators you have. This method also takes
607/// care of switching to the normal destination BB, if we are returning
608/// from an invoke.
609///
610void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
611 GenericValue Result) {
612 // Pop the current stack frame.
613 ECStack.pop_back();
614
615 if (ECStack.empty()) { // Finished main. Put result into exit code...
616 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
617 ExitValue = Result; // Capture the exit value of the program
618 } else {
619 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
620 }
621 } else {
622 // If we have a previous stack frame, and we have a previous call,
623 // fill in the return value...
624 ExecutionContext &CallingSF = ECStack.back();
625 if (Instruction *I = CallingSF.Caller.getInstruction()) {
626 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
627 SetValue(I, Result, CallingSF);
628 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
629 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
630 CallingSF.Caller = CallSite(); // We returned from the call...
631 }
632 }
633}
634
635void Interpreter::visitReturnInst(ReturnInst &I) {
636 ExecutionContext &SF = ECStack.back();
637 const Type *RetTy = Type::VoidTy;
638 GenericValue Result;
639
640 // Save away the return value... (if we are not 'ret void')
641 if (I.getNumOperands()) {
642 RetTy = I.getReturnValue()->getType();
643 Result = getOperandValue(I.getReturnValue(), SF);
644 }
645
646 popStackAndReturnValueToCaller(RetTy, Result);
647}
648
649void Interpreter::visitUnwindInst(UnwindInst &I) {
650 // Unwind stack
651 Instruction *Inst;
652 do {
653 ECStack.pop_back ();
654 if (ECStack.empty ())
655 abort ();
656 Inst = ECStack.back ().Caller.getInstruction ();
657 } while (!(Inst && isa<InvokeInst> (Inst)));
658
659 // Return from invoke
660 ExecutionContext &InvokingSF = ECStack.back ();
661 InvokingSF.Caller = CallSite ();
662
663 // Go to exceptional destination BB of invoke instruction
664 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
665}
666
667void Interpreter::visitUnreachableInst(UnreachableInst &I) {
668 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
669 abort();
670}
671
672void Interpreter::visitBranchInst(BranchInst &I) {
673 ExecutionContext &SF = ECStack.back();
674 BasicBlock *Dest;
675
676 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
677 if (!I.isUnconditional()) {
678 Value *Cond = I.getCondition();
679 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
680 Dest = I.getSuccessor(1);
681 }
682 SwitchToNewBasicBlock(Dest, SF);
683}
684
685void Interpreter::visitSwitchInst(SwitchInst &I) {
686 ExecutionContext &SF = ECStack.back();
687 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
688 const Type *ElTy = I.getOperand(0)->getType();
689
690 // Check to see if any of the cases match...
691 BasicBlock *Dest = 0;
692 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
693 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
694 .IntVal != 0) {
695 Dest = cast<BasicBlock>(I.getOperand(i+1));
696 break;
697 }
698
699 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
700 SwitchToNewBasicBlock(Dest, SF);
701}
702
703// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
704// This function handles the actual updating of block and instruction iterators
705// as well as execution of all of the PHI nodes in the destination block.
706//
707// This method does this because all of the PHI nodes must be executed
708// atomically, reading their inputs before any of the results are updated. Not
709// doing this can cause problems if the PHI nodes depend on other PHI nodes for
710// their inputs. If the input PHI node is updated before it is read, incorrect
711// results can happen. Thus we use a two phase approach.
712//
713void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
714 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
715 SF.CurBB = Dest; // Update CurBB to branch destination
716 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
717
718 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
719
720 // Loop over all of the PHI nodes in the current block, reading their inputs.
721 std::vector<GenericValue> ResultValues;
722
723 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
724 // Search for the value corresponding to this previous bb...
725 int i = PN->getBasicBlockIndex(PrevBB);
726 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
727 Value *IncomingValue = PN->getIncomingValue(i);
728
729 // Save the incoming value for this PHI node...
730 ResultValues.push_back(getOperandValue(IncomingValue, SF));
731 }
732
733 // Now loop over all of the PHI nodes setting their values...
734 SF.CurInst = SF.CurBB->begin();
735 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
736 PHINode *PN = cast<PHINode>(SF.CurInst);
737 SetValue(PN, ResultValues[i], SF);
738 }
739}
740
741//===----------------------------------------------------------------------===//
742// Memory Instruction Implementations
743//===----------------------------------------------------------------------===//
744
745void Interpreter::visitAllocationInst(AllocationInst &I) {
746 ExecutionContext &SF = ECStack.back();
747
748 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
749
750 // Get the number of elements being allocated by the array...
751 unsigned NumElements =
752 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
753
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000754 unsigned TypeSize = (size_t)TD.getABITypeSize(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755
Gabor Greif7ee10f92007-10-11 19:40:35 +0000756 // Avoid malloc-ing zero bytes, use max()...
757 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758
759 // Allocate enough memory to hold the type...
760 void *Memory = malloc(MemToAlloc);
761
762 DOUT << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
763 << NumElements << " (Total: " << MemToAlloc << ") at "
764 << uintptr_t(Memory) << '\n';
765
766 GenericValue Result = PTOGV(Memory);
767 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
768 SetValue(&I, Result, SF);
769
770 if (I.getOpcode() == Instruction::Alloca)
771 ECStack.back().Allocas.add(Memory);
772}
773
774void Interpreter::visitFreeInst(FreeInst &I) {
775 ExecutionContext &SF = ECStack.back();
776 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
777 GenericValue Value = getOperandValue(I.getOperand(0), SF);
778 // TODO: Check to make sure memory is allocated
779 free(GVTOP(Value)); // Free memory
780}
781
782// getElementOffset - The workhorse for getelementptr.
783//
784GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
785 gep_type_iterator E,
786 ExecutionContext &SF) {
787 assert(isa<PointerType>(Ptr->getType()) &&
788 "Cannot getElementOffset of a nonpointer type!");
789
790 uint64_t Total = 0;
791
792 for (; I != E; ++I) {
793 if (const StructType *STy = dyn_cast<StructType>(*I)) {
794 const StructLayout *SLO = TD.getStructLayout(STy);
795
796 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
797 unsigned Index = unsigned(CPU->getZExtValue());
798
799 Total += SLO->getElementOffset(Index);
800 } else {
801 const SequentialType *ST = cast<SequentialType>(*I);
802 // Get the index number for the array... which must be long type...
803 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
804
805 int64_t Idx;
806 unsigned BitWidth =
807 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
808 if (BitWidth == 32)
809 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
Chris Lattner3203f192008-04-06 21:50:58 +0000810 else {
811 assert(BitWidth == 64 && "Invalid index type for getelementptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
Chris Lattner3203f192008-04-06 21:50:58 +0000813 }
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000814 Total += TD.getABITypeSize(ST->getElementType())*Idx;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 }
816 }
817
818 GenericValue Result;
819 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
820 DOUT << "GEP Index " << Total << " bytes.\n";
821 return Result;
822}
823
824void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
825 ExecutionContext &SF = ECStack.back();
826 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
827 gep_type_begin(I), gep_type_end(I), SF), SF);
828}
829
830void Interpreter::visitLoadInst(LoadInst &I) {
831 ExecutionContext &SF = ECStack.back();
832 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
833 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
834 GenericValue Result;
835 LoadValueFromMemory(Result, Ptr, I.getType());
836 SetValue(&I, Result, SF);
Chris Lattner001ea9d2008-07-08 17:25:49 +0000837 if (I.isVolatile() && PrintVolatile)
838 cerr << "Volatile load " << I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839}
840
841void Interpreter::visitStoreInst(StoreInst &I) {
842 ExecutionContext &SF = ECStack.back();
843 GenericValue Val = getOperandValue(I.getOperand(0), SF);
844 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
845 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
846 I.getOperand(0)->getType());
Chris Lattner001ea9d2008-07-08 17:25:49 +0000847 if (I.isVolatile() && PrintVolatile)
848 cerr << "Volatile store: " << I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849}
850
851//===----------------------------------------------------------------------===//
852// Miscellaneous Instruction Implementations
853//===----------------------------------------------------------------------===//
854
855void Interpreter::visitCallSite(CallSite CS) {
856 ExecutionContext &SF = ECStack.back();
857
858 // Check to see if this is an intrinsic function call...
859 Function *F = CS.getCalledFunction();
860 if (F && F->isDeclaration ())
861 switch (F->getIntrinsicID()) {
862 case Intrinsic::not_intrinsic:
863 break;
864 case Intrinsic::vastart: { // va_start
865 GenericValue ArgIndex;
866 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
867 ArgIndex.UIntPairVal.second = 0;
868 SetValue(CS.getInstruction(), ArgIndex, SF);
869 return;
870 }
871 case Intrinsic::vaend: // va_end is a noop for the interpreter
872 return;
873 case Intrinsic::vacopy: // va_copy: dest = src
874 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
875 return;
876 default:
877 // If it is an unknown intrinsic function, use the intrinsic lowering
878 // class to transform it into hopefully tasty LLVM code.
879 //
880 BasicBlock::iterator me(CS.getInstruction());
881 BasicBlock *Parent = CS.getInstruction()->getParent();
882 bool atBegin(Parent->begin() == me);
883 if (!atBegin)
884 --me;
885 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
886
887 // Restore the CurInst pointer to the first instruction newly inserted, if
888 // any.
889 if (atBegin) {
890 SF.CurInst = Parent->begin();
891 } else {
892 SF.CurInst = me;
893 ++SF.CurInst;
894 }
895 return;
896 }
897
898
899 SF.Caller = CS;
900 std::vector<GenericValue> ArgVals;
901 const unsigned NumArgs = SF.Caller.arg_size();
902 ArgVals.reserve(NumArgs);
903 uint16_t pNum = 1;
904 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
905 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
906 Value *V = *i;
907 ArgVals.push_back(getOperandValue(V, SF));
Duncan Sands637ec552007-11-28 17:07:01 +0000908 // Promote all integral types whose size is < sizeof(i32) into i32.
909 // We do this by zero or sign extending the value as appropriate
910 // according to the parameter attributes
911 const Type *Ty = V->getType();
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000912 if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32)) {
Duncan Sands637ec552007-11-28 17:07:01 +0000913 if (CS.paramHasAttr(pNum, ParamAttr::ZExt))
914 ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
915 else if (CS.paramHasAttr(pNum, ParamAttr::SExt))
916 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000917 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918 }
919
920 // To handle indirect calls, we must get the pointer value from the argument
921 // and treat it as a function pointer.
922 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
923 callFunction((Function*)GVTOP(SRC), ArgVals);
924}
925
926void Interpreter::visitShl(BinaryOperator &I) {
927 ExecutionContext &SF = ECStack.back();
928 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
929 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
930 GenericValue Dest;
931 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
932 SetValue(&I, Dest, SF);
933}
934
935void Interpreter::visitLShr(BinaryOperator &I) {
936 ExecutionContext &SF = ECStack.back();
937 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
938 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
939 GenericValue Dest;
940 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
941 SetValue(&I, Dest, SF);
942}
943
944void Interpreter::visitAShr(BinaryOperator &I) {
945 ExecutionContext &SF = ECStack.back();
946 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
947 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
948 GenericValue Dest;
949 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
950 SetValue(&I, Dest, SF);
951}
952
953GenericValue Interpreter::executeTruncInst(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.trunc(DBitWidth);
959 return Dest;
960}
961
962GenericValue Interpreter::executeSExtInst(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.sext(DBitWidth);
968 return Dest;
969}
970
971GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
972 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000973 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
974 const IntegerType *DITy = cast<IntegerType>(DstTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000975 unsigned DBitWidth = DITy->getBitWidth();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976 Dest.IntVal = Src.IntVal.zext(DBitWidth);
977 return Dest;
978}
979
980GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
981 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner3203f192008-04-06 21:50:58 +0000983 assert(SrcVal->getType() == Type::DoubleTy && DstTy == Type::FloatTy &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 "Invalid FPTrunc instruction");
985 Dest.FloatVal = (float) Src.DoubleVal;
986 return Dest;
987}
988
989GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
990 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner3203f192008-04-06 21:50:58 +0000992 assert(SrcVal->getType() == Type::FloatTy && DstTy == Type::DoubleTy &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993 "Invalid FPTrunc instruction");
994 Dest.DoubleVal = (double) Src.FloatVal;
995 return Dest;
996}
997
998GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
999 ExecutionContext &SF) {
1000 const Type *SrcTy = SrcVal->getType();
1001 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1002 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1003 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
1004
1005 if (SrcTy->getTypeID() == Type::FloatTyID)
1006 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1007 else
1008 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1009 return Dest;
1010}
1011
1012GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1013 ExecutionContext &SF) {
1014 const Type *SrcTy = SrcVal->getType();
1015 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1016 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1017 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1018
1019 if (SrcTy->getTypeID() == Type::FloatTyID)
1020 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1021 else
1022 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1023 return Dest;
1024}
1025
1026GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1027 ExecutionContext &SF) {
1028 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1029 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1030
1031 if (DstTy->getTypeID() == Type::FloatTyID)
1032 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
1033 else
1034 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
1035 return Dest;
1036}
1037
1038GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1039 ExecutionContext &SF) {
1040 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1041 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
1042
1043 if (DstTy->getTypeID() == Type::FloatTyID)
1044 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
1045 else
1046 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
1047 return Dest;
1048
1049}
1050
1051GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1052 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001053 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1054 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner3203f192008-04-06 21:50:58 +00001055 assert(isa<PointerType>(SrcVal->getType()) && "Invalid PtrToInt instruction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001056
1057 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1058 return Dest;
1059}
1060
1061GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1062 ExecutionContext &SF) {
1063 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1064 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1065
1066 uint32_t PtrSize = TD.getPointerSizeInBits();
1067 if (PtrSize != Src.IntVal.getBitWidth())
1068 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
1069
1070 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
1071 return Dest;
1072}
1073
1074GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1075 ExecutionContext &SF) {
1076
1077 const Type *SrcTy = SrcVal->getType();
1078 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1079 if (isa<PointerType>(DstTy)) {
1080 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1081 Dest.PointerVal = Src.PointerVal;
1082 } else if (DstTy->isInteger()) {
1083 if (SrcTy == Type::FloatTy) {
1084 Dest.IntVal.zext(sizeof(Src.FloatVal) * 8);
1085 Dest.IntVal.floatToBits(Src.FloatVal);
1086 } else if (SrcTy == Type::DoubleTy) {
1087 Dest.IntVal.zext(sizeof(Src.DoubleVal) * 8);
1088 Dest.IntVal.doubleToBits(Src.DoubleVal);
1089 } else if (SrcTy->isInteger()) {
1090 Dest.IntVal = Src.IntVal;
1091 } else
1092 assert(0 && "Invalid BitCast");
1093 } else if (DstTy == Type::FloatTy) {
1094 if (SrcTy->isInteger())
1095 Dest.FloatVal = Src.IntVal.bitsToFloat();
1096 else
1097 Dest.FloatVal = Src.FloatVal;
1098 } else if (DstTy == Type::DoubleTy) {
1099 if (SrcTy->isInteger())
1100 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1101 else
1102 Dest.DoubleVal = Src.DoubleVal;
1103 } else
1104 assert(0 && "Invalid Bitcast");
1105
1106 return Dest;
1107}
1108
1109void Interpreter::visitTruncInst(TruncInst &I) {
1110 ExecutionContext &SF = ECStack.back();
1111 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1112}
1113
1114void Interpreter::visitSExtInst(SExtInst &I) {
1115 ExecutionContext &SF = ECStack.back();
1116 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1117}
1118
1119void Interpreter::visitZExtInst(ZExtInst &I) {
1120 ExecutionContext &SF = ECStack.back();
1121 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1122}
1123
1124void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1125 ExecutionContext &SF = ECStack.back();
1126 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1127}
1128
1129void Interpreter::visitFPExtInst(FPExtInst &I) {
1130 ExecutionContext &SF = ECStack.back();
1131 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1132}
1133
1134void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1135 ExecutionContext &SF = ECStack.back();
1136 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1137}
1138
1139void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1140 ExecutionContext &SF = ECStack.back();
1141 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1142}
1143
1144void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1145 ExecutionContext &SF = ECStack.back();
1146 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1147}
1148
1149void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1150 ExecutionContext &SF = ECStack.back();
1151 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1152}
1153
1154void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1155 ExecutionContext &SF = ECStack.back();
1156 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1157}
1158
1159void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1160 ExecutionContext &SF = ECStack.back();
1161 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1162}
1163
1164void Interpreter::visitBitCastInst(BitCastInst &I) {
1165 ExecutionContext &SF = ECStack.back();
1166 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1167}
1168
1169#define IMPLEMENT_VAARG(TY) \
1170 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1171
1172void Interpreter::visitVAArgInst(VAArgInst &I) {
1173 ExecutionContext &SF = ECStack.back();
1174
1175 // Get the incoming valist parameter. LLI treats the valist as a
1176 // (ec-stack-depth var-arg-index) pair.
1177 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1178 GenericValue Dest;
1179 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1180 .VarArgs[VAList.UIntPairVal.second];
1181 const Type *Ty = I.getType();
1182 switch (Ty->getTypeID()) {
1183 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
1184 IMPLEMENT_VAARG(Pointer);
1185 IMPLEMENT_VAARG(Float);
1186 IMPLEMENT_VAARG(Double);
1187 default:
1188 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1189 abort();
1190 }
1191
1192 // Set the Value of this Instruction.
1193 SetValue(&I, Dest, SF);
1194
1195 // Move the pointer to the next vararg.
1196 ++VAList.UIntPairVal.second;
1197}
1198
1199GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1200 ExecutionContext &SF) {
1201 switch (CE->getOpcode()) {
1202 case Instruction::Trunc:
1203 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1204 case Instruction::ZExt:
1205 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1206 case Instruction::SExt:
1207 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1208 case Instruction::FPTrunc:
1209 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1210 case Instruction::FPExt:
1211 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1212 case Instruction::UIToFP:
1213 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1214 case Instruction::SIToFP:
1215 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1216 case Instruction::FPToUI:
1217 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1218 case Instruction::FPToSI:
1219 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1220 case Instruction::PtrToInt:
1221 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1222 case Instruction::IntToPtr:
1223 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1224 case Instruction::BitCast:
1225 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1226 case Instruction::GetElementPtr:
1227 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1228 gep_type_end(CE), SF);
1229 case Instruction::FCmp:
1230 case Instruction::ICmp:
1231 return executeCmpInst(CE->getPredicate(),
1232 getOperandValue(CE->getOperand(0), SF),
1233 getOperandValue(CE->getOperand(1), SF),
1234 CE->getOperand(0)->getType());
1235 case Instruction::Select:
1236 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1237 getOperandValue(CE->getOperand(1), SF),
1238 getOperandValue(CE->getOperand(2), SF));
1239 default :
1240 break;
1241 }
1242
1243 // The cases below here require a GenericValue parameter for the result
1244 // so we initialize one, compute it and then return it.
1245 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1246 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
1247 GenericValue Dest;
1248 const Type * Ty = CE->getOperand(0)->getType();
1249 switch (CE->getOpcode()) {
1250 case Instruction::Add: executeAddInst (Dest, Op0, Op1, Ty); break;
1251 case Instruction::Sub: executeSubInst (Dest, Op0, Op1, Ty); break;
1252 case Instruction::Mul: executeMulInst (Dest, Op0, Op1, Ty); break;
1253 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1254 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1255 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1256 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1257 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1258 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
1259 case Instruction::And: Dest.IntVal = Op0.IntVal.And(Op1.IntVal); break;
1260 case Instruction::Or: Dest.IntVal = Op0.IntVal.Or(Op1.IntVal); break;
1261 case Instruction::Xor: Dest.IntVal = Op0.IntVal.Xor(Op1.IntVal); break;
1262 case Instruction::Shl:
1263 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1264 break;
1265 case Instruction::LShr:
1266 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1267 break;
1268 case Instruction::AShr:
1269 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1270 break;
1271 default:
1272 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
1273 abort();
1274 return GenericValue();
1275 }
1276 return Dest;
1277}
1278
1279GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1280 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1281 return getConstantExprValue(CE, SF);
1282 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1283 return getConstantValue(CPV);
1284 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1285 return PTOGV(getPointerToGlobal(GV));
1286 } else {
1287 return SF.Values[V];
1288 }
1289}
1290
1291//===----------------------------------------------------------------------===//
1292// Dispatch and Execution Code
1293//===----------------------------------------------------------------------===//
1294
1295//===----------------------------------------------------------------------===//
1296// callFunction - Execute the specified function...
1297//
1298void Interpreter::callFunction(Function *F,
1299 const std::vector<GenericValue> &ArgVals) {
1300 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1301 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1302 "Incorrect number of arguments passed into function call!");
1303 // Make a new stack frame... and fill it in.
1304 ECStack.push_back(ExecutionContext());
1305 ExecutionContext &StackFrame = ECStack.back();
1306 StackFrame.CurFunction = F;
1307
1308 // Special handling for external functions.
1309 if (F->isDeclaration()) {
1310 GenericValue Result = callExternalFunction (F, ArgVals);
1311 // Simulate a 'ret' instruction of the appropriate type.
1312 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1313 return;
1314 }
1315
1316 // Get pointers to first LLVM BB & Instruction in function.
1317 StackFrame.CurBB = F->begin();
1318 StackFrame.CurInst = StackFrame.CurBB->begin();
1319
1320 // Run through the function arguments and initialize their values...
1321 assert((ArgVals.size() == F->arg_size() ||
1322 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1323 "Invalid number of values passed to function invocation!");
1324
1325 // Handle non-varargs arguments...
1326 unsigned i = 0;
1327 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1328 AI != E; ++AI, ++i)
1329 SetValue(AI, ArgVals[i], StackFrame);
1330
1331 // Handle varargs arguments...
1332 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1333}
1334
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001335
1336void Interpreter::run() {
1337 while (!ECStack.empty()) {
1338 // Interpret a single instruction & increment the "PC".
1339 ExecutionContext &SF = ECStack.back(); // Current stack frame
1340 Instruction &I = *SF.CurInst++; // Increment before execute
1341
1342 // Track the number of dynamic instructions executed.
1343 ++NumDynamicInsts;
1344
1345 DOUT << "About to interpret: " << I;
1346 visit(I); // Dispatch to one of the visit* methods...
Chris Lattnerfc65a9f2007-09-21 18:30:39 +00001347#if 0
1348 // This is not safe, as visiting the instruction could lower it and free I.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001349#ifndef NDEBUG
1350 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1351 I.getType() != Type::VoidTy) {
1352 DOUT << " --> ";
Chris Lattnerfc65a9f2007-09-21 18:30:39 +00001353 const GenericValue &Val = SF.Values[&I];
1354 switch (I.getType()->getTypeID()) {
1355 default: assert(0 && "Invalid GenericValue Type");
1356 case Type::VoidTyID: DOUT << "void"; break;
1357 case Type::FloatTyID: DOUT << "float " << Val.FloatVal; break;
1358 case Type::DoubleTyID: DOUT << "double " << Val.DoubleVal; break;
1359 case Type::PointerTyID: DOUT << "void* " << intptr_t(Val.PointerVal);
1360 break;
1361 case Type::IntegerTyID:
1362 DOUT << "i" << Val.IntVal.getBitWidth() << " "
1363 << Val.IntVal.toStringUnsigned(10)
1364 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
1365 break;
1366 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001367 }
1368#endif
Chris Lattnerfc65a9f2007-09-21 18:30:39 +00001369#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 }
1371}