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