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