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