1 //===-- Execution.cpp - Implement code to simulate the program ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/Debug.h"
24 #include "llvm/Support/MathExtras.h"
28 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
29 static Interpreter *TheEE = 0;
31 //===----------------------------------------------------------------------===//
32 // Various Helper Functions
33 //===----------------------------------------------------------------------===//
35 inline void initializeAPInt(GenericValue &GV, const Type* Ty,
36 ExecutionContext &SF) {
37 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty))
38 GV.APIntVal = SF.getAPInt(ITy->getBitWidth());
41 static inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
42 // Determine if the value is signed or not
43 bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
44 // If its signed, extend the sign bits
46 Val |= ~ITy->getBitMask();
50 static inline void maskToBitWidth(GenericValue& GV, unsigned BitWidth) {
51 uint64_t BitMask = ~(uint64_t)(0ull) >> (64-BitWidth);
53 GV.Int8Val &= BitMask;
54 else if (BitWidth <= 16)
55 GV.Int16Val &= BitMask;
56 else if (BitWidth <= 32)
57 GV.Int32Val &= BitMask;
58 else if (BitWidth <= 64)
59 GV.Int64Val &= BitMask;
61 assert(GV.APIntVal && "Unallocated GV.APIntVal");
62 *(GV.APIntVal) &= APInt::getAllOnesValue(BitWidth);
66 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
70 void Interpreter::initializeExecutionEngine() {
74 //===----------------------------------------------------------------------===//
75 // Binary Instruction Implementations
76 //===----------------------------------------------------------------------===//
78 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
79 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
81 #define IMPLEMENT_INTEGER_BINOP(OP, TY) \
82 case Type::IntegerTyID: { \
83 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
85 Dest.Int1Val = Src1.Int1Val OP Src2.Int1Val; \
86 maskToBitWidth(Dest, BitWidth); \
87 } else if (BitWidth <= 8) {\
88 Dest.Int8Val = Src1.Int8Val OP Src2.Int8Val; \
89 maskToBitWidth(Dest, BitWidth); \
90 } else if (BitWidth <= 16) {\
91 Dest.Int16Val = Src1.Int16Val OP Src2.Int16Val; \
92 maskToBitWidth(Dest, BitWidth); \
93 } else if (BitWidth <= 32) {\
94 Dest.Int32Val = Src1.Int32Val OP Src2.Int32Val; \
95 maskToBitWidth(Dest, BitWidth); \
96 } else if (BitWidth <= 64) {\
97 Dest.Int64Val = Src1.Int64Val OP Src2.Int64Val; \
98 maskToBitWidth(Dest, BitWidth); \
100 *(Dest.APIntVal) = *(Src1.APIntVal) OP *(Src2.APIntVal); \
104 #define IMPLEMENT_SIGNED_BINOP(OP, TY, APOP) \
105 if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
106 unsigned BitWidth = ITy->getBitWidth(); \
107 if (BitWidth <= 8) { \
108 Dest.Int8Val = ((int8_t)Src1.Int8Val) OP ((int8_t)Src2.Int8Val); \
109 maskToBitWidth(Dest, BitWidth); \
110 } else if (BitWidth <= 16) { \
111 Dest.Int16Val = ((int16_t)Src1.Int16Val) OP ((int16_t)Src2.Int16Val); \
112 maskToBitWidth(Dest, BitWidth); \
113 } else if (BitWidth <= 32) { \
114 Dest.Int32Val = ((int32_t)Src1.Int32Val) OP ((int32_t)Src2.Int32Val); \
115 maskToBitWidth(Dest, BitWidth); \
116 } else if (BitWidth <= 64) { \
117 Dest.Int64Val = ((int64_t)Src1.Int64Val) OP ((int64_t)Src2.Int64Val); \
118 maskToBitWidth(Dest, BitWidth); \
120 *(Dest.APIntVal) = Src1.APIntVal->APOP(*(Src2.APIntVal)); \
122 cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
126 #define IMPLEMENT_UNSIGNED_BINOP(OP, TY, APOP) \
127 if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
128 unsigned BitWidth = ITy->getBitWidth(); \
129 if (BitWidth <= 8) {\
130 Dest.Int8Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
131 maskToBitWidth(Dest, BitWidth); \
132 } else if (BitWidth <= 16) {\
133 Dest.Int16Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
134 maskToBitWidth(Dest, BitWidth); \
135 } else if (BitWidth <= 32) {\
136 Dest.Int32Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
137 maskToBitWidth(Dest, BitWidth); \
138 } else if (BitWidth <= 64) {\
139 Dest.Int64Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
140 maskToBitWidth(Dest, BitWidth); \
142 *(Dest.APIntVal) = Src1.APIntVal->APOP(*(Src2.APIntVal)); \
144 cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
148 static void executeAddInst(GenericValue &Dest, GenericValue Src1,
149 GenericValue Src2, const Type *Ty) {
150 switch (Ty->getTypeID()) {
151 IMPLEMENT_INTEGER_BINOP(+, Ty);
152 IMPLEMENT_BINARY_OPERATOR(+, Float);
153 IMPLEMENT_BINARY_OPERATOR(+, Double);
155 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
160 static void executeSubInst(GenericValue &Dest, GenericValue Src1,
161 GenericValue Src2, const Type *Ty) {
162 switch (Ty->getTypeID()) {
163 IMPLEMENT_INTEGER_BINOP(-, Ty);
164 IMPLEMENT_BINARY_OPERATOR(-, Float);
165 IMPLEMENT_BINARY_OPERATOR(-, Double);
167 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
172 static void executeMulInst(GenericValue &Dest, GenericValue Src1,
173 GenericValue Src2, const Type *Ty) {
174 switch (Ty->getTypeID()) {
175 IMPLEMENT_INTEGER_BINOP(*, Ty);
176 IMPLEMENT_BINARY_OPERATOR(*, Float);
177 IMPLEMENT_BINARY_OPERATOR(*, Double);
179 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
184 static void executeUDivInst(GenericValue &Dest, GenericValue Src1,
185 GenericValue Src2, const Type *Ty) {
186 IMPLEMENT_UNSIGNED_BINOP(/,Ty,udiv)
189 static void executeSDivInst(GenericValue &Dest, GenericValue Src1,
190 GenericValue Src2, const Type *Ty) {
191 IMPLEMENT_SIGNED_BINOP(/,Ty,sdiv)
194 static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
195 GenericValue Src2, const Type *Ty) {
196 switch (Ty->getTypeID()) {
197 IMPLEMENT_BINARY_OPERATOR(/, Float);
198 IMPLEMENT_BINARY_OPERATOR(/, Double);
200 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
205 static void executeURemInst(GenericValue &Dest, GenericValue Src1,
206 GenericValue Src2, const Type *Ty) {
207 IMPLEMENT_UNSIGNED_BINOP(%,Ty,urem)
210 static void executeSRemInst(GenericValue &Dest, GenericValue Src1,
211 GenericValue Src2, const Type *Ty) {
212 IMPLEMENT_SIGNED_BINOP(%,Ty,srem)
215 static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
216 GenericValue Src2, const Type *Ty) {
217 switch (Ty->getTypeID()) {
218 case Type::FloatTyID:
219 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
221 case Type::DoubleTyID:
222 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
225 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
230 static void executeAndInst(GenericValue &Dest, GenericValue Src1,
231 GenericValue Src2, const Type *Ty) {
232 IMPLEMENT_UNSIGNED_BINOP(&,Ty,And)
235 static void executeOrInst(GenericValue &Dest, GenericValue Src1,
236 GenericValue Src2, const Type *Ty) {
237 IMPLEMENT_UNSIGNED_BINOP(|,Ty,Or)
240 static void executeXorInst(GenericValue &Dest, GenericValue Src1,
241 GenericValue Src2, const Type *Ty) {
242 IMPLEMENT_UNSIGNED_BINOP(^,Ty,Xor)
245 #define IMPLEMENT_SIGNED_ICMP(OP, TY) \
246 case Type::IntegerTyID: { \
247 const IntegerType* ITy = cast<IntegerType>(TY); \
248 unsigned BitWidth = ITy->getBitWidth(); \
249 int64_t LHS = 0, RHS = 0; \
250 if (BitWidth <= 8) { \
251 LHS = int64_t(doSignExtension(uint64_t(Src1.Int8Val), ITy)); \
252 RHS = int64_t(doSignExtension(uint64_t(Src2.Int8Val), ITy)); \
253 } else if (BitWidth <= 16) { \
254 LHS = int64_t(doSignExtension(uint64_t(Src1.Int16Val), ITy)); \
255 RHS = int64_t(doSignExtension(uint64_t(Src2.Int16Val), ITy)); \
256 } else if (BitWidth <= 32) { \
257 LHS = int64_t(doSignExtension(uint64_t(Src1.Int32Val), ITy)); \
258 RHS = int64_t(doSignExtension(uint64_t(Src2.Int32Val), ITy)); \
259 } else if (BitWidth <= 64) { \
260 LHS = int64_t(doSignExtension(uint64_t(Src1.Int64Val), ITy)); \
261 RHS = int64_t(doSignExtension(uint64_t(Src2.Int64Val), ITy)); \
263 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
266 Dest.Int1Val = LHS OP RHS; \
270 #define IMPLEMENT_UNSIGNED_ICMP(OP, TY) \
271 case Type::IntegerTyID: { \
272 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
274 Dest.Int1Val = ((uint8_t)Src1.Int1Val) OP ((uint8_t)Src2.Int1Val); \
275 else if (BitWidth <= 8) \
276 Dest.Int1Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
277 else if (BitWidth <= 16) \
278 Dest.Int1Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
279 else if (BitWidth <= 32) \
280 Dest.Int1Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
281 else if (BitWidth <= 64) \
282 Dest.Int1Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
284 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
287 maskToBitWidth(Dest, BitWidth); \
291 // Handle pointers specially because they must be compared with only as much
292 // width as the host has. We _do not_ want to be comparing 64 bit values when
293 // running on a 32-bit target, otherwise the upper 32 bits might mess up
294 // comparisons if they contain garbage.
295 #define IMPLEMENT_POINTER_ICMP(OP) \
296 case Type::PointerTyID: \
297 Dest.Int1Val = (void*)(intptr_t)Src1.PointerVal OP \
298 (void*)(intptr_t)Src2.PointerVal; break
300 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
303 switch (Ty->getTypeID()) {
304 IMPLEMENT_UNSIGNED_ICMP(==, Ty);
305 IMPLEMENT_POINTER_ICMP(==);
307 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
313 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
316 switch (Ty->getTypeID()) {
317 IMPLEMENT_UNSIGNED_ICMP(!=, Ty);
318 IMPLEMENT_POINTER_ICMP(!=);
320 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
326 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
329 switch (Ty->getTypeID()) {
330 IMPLEMENT_UNSIGNED_ICMP(<, Ty);
331 IMPLEMENT_POINTER_ICMP(<);
333 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
339 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
342 switch (Ty->getTypeID()) {
343 IMPLEMENT_SIGNED_ICMP(<, Ty);
344 IMPLEMENT_POINTER_ICMP(<);
346 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
352 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
355 switch (Ty->getTypeID()) {
356 IMPLEMENT_UNSIGNED_ICMP(>, Ty);
357 IMPLEMENT_POINTER_ICMP(>);
359 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
365 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
368 switch (Ty->getTypeID()) {
369 IMPLEMENT_SIGNED_ICMP(>, Ty);
370 IMPLEMENT_POINTER_ICMP(>);
372 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
378 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
381 switch (Ty->getTypeID()) {
382 IMPLEMENT_UNSIGNED_ICMP(<=, Ty);
383 IMPLEMENT_POINTER_ICMP(<=);
385 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
391 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
394 switch (Ty->getTypeID()) {
395 IMPLEMENT_SIGNED_ICMP(<=, Ty);
396 IMPLEMENT_POINTER_ICMP(<=);
398 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
404 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
407 switch (Ty->getTypeID()) {
408 IMPLEMENT_UNSIGNED_ICMP(>=,Ty);
409 IMPLEMENT_POINTER_ICMP(>=);
411 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
417 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
420 switch (Ty->getTypeID()) {
421 IMPLEMENT_SIGNED_ICMP(>=, Ty);
422 IMPLEMENT_POINTER_ICMP(>=);
424 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
430 void Interpreter::visitICmpInst(ICmpInst &I) {
431 ExecutionContext &SF = ECStack.back();
432 const Type *Ty = I.getOperand(0)->getType();
433 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
434 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
435 GenericValue R; // Result
437 switch (I.getPredicate()) {
438 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
439 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
440 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
441 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
442 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
443 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
444 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
445 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
446 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
447 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
449 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
456 #define IMPLEMENT_FCMP(OP, TY) \
457 case Type::TY##TyID: Dest.Int1Val = Src1.TY##Val OP Src2.TY##Val; break
459 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
462 switch (Ty->getTypeID()) {
463 IMPLEMENT_FCMP(==, Float);
464 IMPLEMENT_FCMP(==, Double);
466 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
472 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
475 switch (Ty->getTypeID()) {
476 IMPLEMENT_FCMP(!=, Float);
477 IMPLEMENT_FCMP(!=, Double);
480 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
486 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
489 switch (Ty->getTypeID()) {
490 IMPLEMENT_FCMP(<=, Float);
491 IMPLEMENT_FCMP(<=, Double);
493 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
499 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
502 switch (Ty->getTypeID()) {
503 IMPLEMENT_FCMP(>=, Float);
504 IMPLEMENT_FCMP(>=, Double);
506 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
512 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
515 switch (Ty->getTypeID()) {
516 IMPLEMENT_FCMP(<, Float);
517 IMPLEMENT_FCMP(<, Double);
519 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
525 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
528 switch (Ty->getTypeID()) {
529 IMPLEMENT_FCMP(>, Float);
530 IMPLEMENT_FCMP(>, Double);
532 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
538 #define IMPLEMENT_UNORDERED(TY, X,Y) \
539 if (TY == Type::FloatTy) \
540 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
541 Dest.Int1Val = true; \
544 else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
545 Dest.Int1Val = true; \
550 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
553 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
554 return executeFCMP_OEQ(Src1, Src2, Ty);
557 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
560 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
561 return executeFCMP_ONE(Src1, Src2, Ty);
564 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
567 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
568 return executeFCMP_OLE(Src1, Src2, Ty);
571 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
574 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
575 return executeFCMP_OGE(Src1, Src2, Ty);
578 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
581 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
582 return executeFCMP_OLT(Src1, Src2, Ty);
585 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
588 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
589 return executeFCMP_OGT(Src1, Src2, Ty);
592 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
595 if (Ty == Type::FloatTy)
596 Dest.Int1Val = (Src1.FloatVal == Src1.FloatVal &&
597 Src2.FloatVal == Src2.FloatVal);
599 Dest.Int1Val = (Src1.DoubleVal == Src1.DoubleVal &&
600 Src2.DoubleVal == Src2.DoubleVal);
604 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
607 if (Ty == Type::FloatTy)
608 Dest.Int1Val = (Src1.FloatVal != Src1.FloatVal ||
609 Src2.FloatVal != Src2.FloatVal);
611 Dest.Int1Val = (Src1.DoubleVal != Src1.DoubleVal ||
612 Src2.DoubleVal != Src2.DoubleVal);
616 void Interpreter::visitFCmpInst(FCmpInst &I) {
617 ExecutionContext &SF = ECStack.back();
618 const Type *Ty = I.getOperand(0)->getType();
619 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
620 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
621 GenericValue R; // Result
623 switch (I.getPredicate()) {
624 case FCmpInst::FCMP_FALSE: R.Int1Val = false; break;
625 case FCmpInst::FCMP_TRUE: R.Int1Val = true; break;
626 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
627 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
628 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
629 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
630 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
631 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
632 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
633 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
634 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
635 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
636 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
637 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
638 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
639 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
641 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
648 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
649 GenericValue Src2, const Type *Ty) {
652 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
653 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
654 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
655 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
656 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
657 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
658 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
659 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
660 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
661 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
662 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
663 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
664 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
665 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
666 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
667 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
668 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
669 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
670 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
671 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
672 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
673 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
674 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
675 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
676 case FCmpInst::FCMP_FALSE: {
678 Result.Int1Val = false;
681 case FCmpInst::FCMP_TRUE: {
683 Result.Int1Val = true;
687 cerr << "Unhandled Cmp predicate\n";
692 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
693 ExecutionContext &SF = ECStack.back();
694 const Type *Ty = I.getOperand(0)->getType();
695 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
696 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
697 GenericValue R; // Result
698 initializeAPInt(R, Ty, SF);
700 switch (I.getOpcode()) {
701 case Instruction::Add: executeAddInst (R, Src1, Src2, Ty); break;
702 case Instruction::Sub: executeSubInst (R, Src1, Src2, Ty); break;
703 case Instruction::Mul: executeMulInst (R, Src1, Src2, Ty); break;
704 case Instruction::UDiv: executeUDivInst (R, Src1, Src2, Ty); break;
705 case Instruction::SDiv: executeSDivInst (R, Src1, Src2, Ty); break;
706 case Instruction::FDiv: executeFDivInst (R, Src1, Src2, Ty); break;
707 case Instruction::URem: executeURemInst (R, Src1, Src2, Ty); break;
708 case Instruction::SRem: executeSRemInst (R, Src1, Src2, Ty); break;
709 case Instruction::FRem: executeFRemInst (R, Src1, Src2, Ty); break;
710 case Instruction::And: executeAndInst (R, Src1, Src2, Ty); break;
711 case Instruction::Or: executeOrInst (R, Src1, Src2, Ty); break;
712 case Instruction::Xor: executeXorInst (R, Src1, Src2, Ty); break;
714 cerr << "Don't know how to handle this binary operator!\n-->" << I;
721 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
723 return Src1.Int1Val ? Src2 : Src3;
726 void Interpreter::visitSelectInst(SelectInst &I) {
727 ExecutionContext &SF = ECStack.back();
728 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
729 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
730 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
732 initializeAPInt(R, I.getOperand(1)->getType(), SF);
733 R = executeSelectInst(Src1, Src2, Src3);
738 //===----------------------------------------------------------------------===//
739 // Terminator Instruction Implementations
740 //===----------------------------------------------------------------------===//
742 void Interpreter::exitCalled(GenericValue GV) {
743 // runAtExitHandlers() assumes there are no stack frames, but
744 // if exit() was called, then it had a stack frame. Blow away
745 // the stack before interpreting atexit handlers.
747 runAtExitHandlers ();
751 /// Pop the last stack frame off of ECStack and then copy the result
752 /// back into the result variable if we are not returning void. The
753 /// result variable may be the ExitValue, or the Value of the calling
754 /// CallInst if there was a previous stack frame. This method may
755 /// invalidate any ECStack iterators you have. This method also takes
756 /// care of switching to the normal destination BB, if we are returning
759 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
760 GenericValue Result) {
761 // Pop the current stack frame.
764 if (ECStack.empty()) { // Finished main. Put result into exit code...
765 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
766 ExitValue = Result; // Capture the exit value of the program
768 memset(&ExitValue, 0, sizeof(ExitValue));
771 // If we have a previous stack frame, and we have a previous call,
772 // fill in the return value...
773 ExecutionContext &CallingSF = ECStack.back();
774 if (Instruction *I = CallingSF.Caller.getInstruction()) {
775 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
776 SetValue(I, Result, CallingSF);
777 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
778 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
779 CallingSF.Caller = CallSite(); // We returned from the call...
784 void Interpreter::visitReturnInst(ReturnInst &I) {
785 ExecutionContext &SF = ECStack.back();
786 const Type *RetTy = Type::VoidTy;
789 // Save away the return value... (if we are not 'ret void')
790 if (I.getNumOperands()) {
791 RetTy = I.getReturnValue()->getType();
792 Result = getOperandValue(I.getReturnValue(), SF);
795 popStackAndReturnValueToCaller(RetTy, Result);
798 void Interpreter::visitUnwindInst(UnwindInst &I) {
803 if (ECStack.empty ())
805 Inst = ECStack.back ().Caller.getInstruction ();
806 } while (!(Inst && isa<InvokeInst> (Inst)));
808 // Return from invoke
809 ExecutionContext &InvokingSF = ECStack.back ();
810 InvokingSF.Caller = CallSite ();
812 // Go to exceptional destination BB of invoke instruction
813 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
816 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
817 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
821 void Interpreter::visitBranchInst(BranchInst &I) {
822 ExecutionContext &SF = ECStack.back();
825 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
826 if (!I.isUnconditional()) {
827 Value *Cond = I.getCondition();
828 if (getOperandValue(Cond, SF).Int1Val == 0) // If false cond...
829 Dest = I.getSuccessor(1);
831 SwitchToNewBasicBlock(Dest, SF);
834 void Interpreter::visitSwitchInst(SwitchInst &I) {
835 ExecutionContext &SF = ECStack.back();
836 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
837 const Type *ElTy = I.getOperand(0)->getType();
839 // Check to see if any of the cases match...
840 BasicBlock *Dest = 0;
841 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
842 if (executeICMP_EQ(CondVal,
843 getOperandValue(I.getOperand(i), SF), ElTy).Int1Val) {
844 Dest = cast<BasicBlock>(I.getOperand(i+1));
848 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
849 SwitchToNewBasicBlock(Dest, SF);
852 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
853 // This function handles the actual updating of block and instruction iterators
854 // as well as execution of all of the PHI nodes in the destination block.
856 // This method does this because all of the PHI nodes must be executed
857 // atomically, reading their inputs before any of the results are updated. Not
858 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
859 // their inputs. If the input PHI node is updated before it is read, incorrect
860 // results can happen. Thus we use a two phase approach.
862 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
863 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
864 SF.CurBB = Dest; // Update CurBB to branch destination
865 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
867 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
869 // Loop over all of the PHI nodes in the current block, reading their inputs.
870 std::vector<GenericValue> ResultValues;
872 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
873 // Search for the value corresponding to this previous bb...
874 int i = PN->getBasicBlockIndex(PrevBB);
875 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
876 Value *IncomingValue = PN->getIncomingValue(i);
878 // Save the incoming value for this PHI node...
879 ResultValues.push_back(getOperandValue(IncomingValue, SF));
882 // Now loop over all of the PHI nodes setting their values...
883 SF.CurInst = SF.CurBB->begin();
884 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
885 PHINode *PN = cast<PHINode>(SF.CurInst);
886 SetValue(PN, ResultValues[i], SF);
890 //===----------------------------------------------------------------------===//
891 // Memory Instruction Implementations
892 //===----------------------------------------------------------------------===//
894 void Interpreter::visitAllocationInst(AllocationInst &I) {
895 ExecutionContext &SF = ECStack.back();
897 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
899 // Get the number of elements being allocated by the array...
900 unsigned NumElements = getOperandValue(I.getOperand(0), SF).Int32Val;
902 // Allocate enough memory to hold the type...
903 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
905 GenericValue Result = PTOGV(Memory);
906 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
907 SetValue(&I, Result, SF);
909 if (I.getOpcode() == Instruction::Alloca)
910 ECStack.back().Allocas.add(Memory);
913 void Interpreter::visitFreeInst(FreeInst &I) {
914 ExecutionContext &SF = ECStack.back();
915 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
916 GenericValue Value = getOperandValue(I.getOperand(0), SF);
917 // TODO: Check to make sure memory is allocated
918 free(GVTOP(Value)); // Free memory
921 // getElementOffset - The workhorse for getelementptr.
923 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
925 ExecutionContext &SF) {
926 assert(isa<PointerType>(Ptr->getType()) &&
927 "Cannot getElementOffset of a nonpointer type!");
931 for (; I != E; ++I) {
932 if (const StructType *STy = dyn_cast<StructType>(*I)) {
933 const StructLayout *SLO = TD.getStructLayout(STy);
935 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
936 unsigned Index = unsigned(CPU->getZExtValue());
938 Total += (PointerTy)SLO->getElementOffset(Index);
940 const SequentialType *ST = cast<SequentialType>(*I);
941 // Get the index number for the array... which must be long type...
942 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
946 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
948 Idx = (int64_t)(int32_t)IdxGV.Int32Val;
949 else if (BitWidth == 64)
950 Idx = (int64_t)IdxGV.Int64Val;
952 assert(0 && "Invalid index type for getelementptr");
953 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
958 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
962 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
963 ExecutionContext &SF = ECStack.back();
964 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
965 gep_type_begin(I), gep_type_end(I), SF), SF);
968 void Interpreter::visitLoadInst(LoadInst &I) {
969 ExecutionContext &SF = ECStack.back();
970 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
971 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
972 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
973 SetValue(&I, Result, SF);
976 void Interpreter::visitStoreInst(StoreInst &I) {
977 ExecutionContext &SF = ECStack.back();
978 GenericValue Val = getOperandValue(I.getOperand(0), SF);
979 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
980 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
981 I.getOperand(0)->getType());
984 //===----------------------------------------------------------------------===//
985 // Miscellaneous Instruction Implementations
986 //===----------------------------------------------------------------------===//
988 void Interpreter::visitCallSite(CallSite CS) {
989 ExecutionContext &SF = ECStack.back();
991 // Check to see if this is an intrinsic function call...
992 if (Function *F = CS.getCalledFunction())
993 if (F->isDeclaration ())
994 switch (F->getIntrinsicID()) {
995 case Intrinsic::not_intrinsic:
997 case Intrinsic::vastart: { // va_start
998 GenericValue ArgIndex;
999 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1000 ArgIndex.UIntPairVal.second = 0;
1001 SetValue(CS.getInstruction(), ArgIndex, SF);
1004 case Intrinsic::vaend: // va_end is a noop for the interpreter
1006 case Intrinsic::vacopy: // va_copy: dest = src
1007 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1010 // If it is an unknown intrinsic function, use the intrinsic lowering
1011 // class to transform it into hopefully tasty LLVM code.
1013 Instruction *Prev = CS.getInstruction()->getPrev();
1014 BasicBlock *Parent = CS.getInstruction()->getParent();
1015 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1017 // Restore the CurInst pointer to the first instruction newly inserted, if
1020 SF.CurInst = Parent->begin();
1029 std::vector<GenericValue> ArgVals;
1030 const unsigned NumArgs = SF.Caller.arg_size();
1031 ArgVals.reserve(NumArgs);
1032 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1033 e = SF.Caller.arg_end(); i != e; ++i) {
1035 ArgVals.push_back(getOperandValue(V, SF));
1036 // Promote all integral types whose size is < sizeof(int) into ints. We do
1037 // this by zero or sign extending the value as appropriate according to the
1039 const Type *Ty = V->getType();
1040 if (Ty->isInteger()) {
1041 if (Ty->getPrimitiveSizeInBits() == 1)
1042 ArgVals.back().Int32Val = ArgVals.back().Int1Val;
1043 else if (Ty->getPrimitiveSizeInBits() <= 8)
1044 ArgVals.back().Int32Val = ArgVals.back().Int8Val;
1045 else if (Ty->getPrimitiveSizeInBits() <= 16)
1046 ArgVals.back().Int32Val = ArgVals.back().Int16Val;
1050 // To handle indirect calls, we must get the pointer value from the argument
1051 // and treat it as a function pointer.
1052 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
1053 callFunction((Function*)GVTOP(SRC), ArgVals);
1056 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
1059 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1060 unsigned BitWidth = ITy->getBitWidth();
1062 Dest.Int8Val = ((uint8_t)Src1.Int8Val) << ((uint32_t)Src2.Int8Val);
1063 else if (BitWidth <= 16)
1064 Dest.Int16Val = ((uint16_t)Src1.Int16Val) << ((uint32_t)Src2.Int8Val);
1065 else if (BitWidth <= 32)
1066 Dest.Int32Val = ((uint32_t)Src1.Int32Val) << ((uint32_t)Src2.Int8Val);
1067 else if (BitWidth <= 64)
1068 Dest.Int64Val = ((uint64_t)Src1.Int64Val) << ((uint32_t)Src2.Int8Val);
1070 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1073 maskToBitWidth(Dest, BitWidth);
1075 cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
1081 static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
1084 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1085 unsigned BitWidth = ITy->getBitWidth();
1087 Dest.Int8Val = ((uint8_t)Src1.Int8Val) >> ((uint32_t)Src2.Int8Val);
1088 else if (BitWidth <= 16)
1089 Dest.Int16Val = ((uint16_t)Src1.Int16Val) >> ((uint32_t)Src2.Int8Val);
1090 else if (BitWidth <= 32)
1091 Dest.Int32Val = ((uint32_t)Src1.Int32Val) >> ((uint32_t)Src2.Int8Val);
1092 else if (BitWidth <= 64)
1093 Dest.Int64Val = ((uint64_t)Src1.Int64Val) >> ((uint32_t)Src2.Int8Val);
1095 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1098 maskToBitWidth(Dest, BitWidth);
1100 cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
1106 static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
1109 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1110 unsigned BitWidth = ITy->getBitWidth();
1112 Dest.Int8Val = ((int8_t)Src1.Int8Val) >> ((int32_t)Src2.Int8Val);
1113 else if (BitWidth <= 16)
1114 Dest.Int16Val = ((int16_t)Src1.Int16Val) >> ((int32_t)Src2.Int8Val);
1115 else if (BitWidth <= 32)
1116 Dest.Int32Val = ((int32_t)Src1.Int32Val) >> ((int32_t)Src2.Int8Val);
1117 else if (BitWidth <= 64)
1118 Dest.Int64Val = ((int64_t)Src1.Int64Val) >> ((int32_t)Src2.Int8Val);
1120 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
1123 maskToBitWidth(Dest, BitWidth);
1125 cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
1131 void Interpreter::visitShl(BinaryOperator &I) {
1132 ExecutionContext &SF = ECStack.back();
1133 const Type *Ty = I.getOperand(0)->getType();
1134 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1135 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1137 Dest = executeShlInst (Src1, Src2, Ty);
1138 SetValue(&I, Dest, SF);
1141 void Interpreter::visitLShr(BinaryOperator &I) {
1142 ExecutionContext &SF = ECStack.back();
1143 const Type *Ty = I.getOperand(0)->getType();
1144 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1145 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1147 Dest = executeLShrInst (Src1, Src2, Ty);
1148 SetValue(&I, Dest, SF);
1151 void Interpreter::visitAShr(BinaryOperator &I) {
1152 ExecutionContext &SF = ECStack.back();
1153 const Type *Ty = I.getOperand(0)->getType();
1154 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1155 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1157 Dest = executeAShrInst (Src1, Src2, Ty);
1158 SetValue(&I, Dest, SF);
1161 #define INTEGER_ASSIGN(DEST, BITWIDTH, VAL) \
1163 uint64_t Mask = ~(uint64_t)(0ull) >> (64-BITWIDTH); \
1164 if (BITWIDTH == 1) { \
1165 Dest.Int1Val = (bool) (VAL & Mask); \
1166 } else if (BITWIDTH <= 8) { \
1167 Dest.Int8Val = (uint8_t) (VAL & Mask); \
1168 } else if (BITWIDTH <= 16) { \
1169 Dest.Int16Val = (uint16_t) (VAL & Mask); \
1170 } else if (BITWIDTH <= 32) { \
1171 Dest.Int32Val = (uint32_t) (VAL & Mask); \
1173 Dest.Int64Val = (uint64_t) (VAL & Mask); \
1176 GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
1177 ExecutionContext &SF) {
1178 const Type *SrcTy = SrcVal->getType();
1179 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1180 const IntegerType *DITy = cast<IntegerType>(DstTy);
1181 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1182 unsigned DBitWidth = DITy->getBitWidth();
1183 unsigned SBitWidth = SITy->getBitWidth();
1184 assert(SBitWidth > DBitWidth && "Invalid truncate");
1186 if (DBitWidth > 64) {
1187 // Both values are APInt, just use the APInt trunc
1188 initializeAPInt(Dest, DstTy, SF);
1189 *(Dest.APIntVal) = Src.APIntVal->trunc(DBitWidth);
1193 uint64_t MaskedVal = 0;
1194 uint64_t Mask = (1ULL << DBitWidth) - 1;
1196 // Mask the source value to its actual bit width. This ensures that any
1197 // high order bits are cleared.
1199 MaskedVal = Src.Int8Val & Mask;
1200 else if (SBitWidth <= 16)
1201 MaskedVal = Src.Int16Val & Mask;
1202 else if (SBitWidth <= 32)
1203 MaskedVal = Src.Int32Val & Mask;
1204 else if (SBitWidth <= 64)
1205 MaskedVal = Src.Int64Val & Mask;
1207 MaskedVal = Src.APIntVal->trunc(DBitWidth).getZExtValue();
1209 INTEGER_ASSIGN(Dest, DBitWidth, MaskedVal);
1213 GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
1214 ExecutionContext &SF) {
1215 const Type *SrcTy = SrcVal->getType();
1216 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1217 const IntegerType *DITy = cast<IntegerType>(DstTy);
1218 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1219 unsigned DBitWidth = DITy->getBitWidth();
1220 unsigned SBitWidth = SITy->getBitWidth();
1221 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1222 "Integer types > 64 bits not supported");
1223 assert(SBitWidth < DBitWidth && "Invalid sign extend");
1225 // Normalize to a 64-bit value.
1226 uint64_t Normalized = 0;
1228 Normalized = Src.Int8Val;
1229 else if (SBitWidth <= 16)
1230 Normalized = Src.Int16Val;
1231 else if (SBitWidth <= 32)
1232 Normalized = Src.Int32Val;
1234 Normalized = Src.Int64Val;
1236 Normalized = doSignExtension(Normalized, SITy);
1238 // Now that we have a sign extended value, assign it to the destination
1239 INTEGER_ASSIGN(Dest, DBitWidth, Normalized);
1243 GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
1244 ExecutionContext &SF) {
1245 const Type *SrcTy = SrcVal->getType();
1246 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1247 const IntegerType *DITy = cast<IntegerType>(DstTy);
1248 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1249 unsigned DBitWidth = DITy->getBitWidth();
1250 unsigned SBitWidth = SITy->getBitWidth();
1251 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1252 "Integer types > 64 bits not supported");
1253 assert(SBitWidth < DBitWidth && "Invalid sign extend");
1254 uint64_t Extended = 0;
1256 // For sign extension from bool, we must extend the source bits.
1257 Extended = (uint64_t) (Src.Int1Val & 1);
1258 else if (SBitWidth <= 8)
1259 Extended = (uint64_t) (uint8_t)Src.Int8Val;
1260 else if (SBitWidth <= 16)
1261 Extended = (uint64_t) (uint16_t)Src.Int16Val;
1262 else if (SBitWidth <= 32)
1263 Extended = (uint64_t) (uint32_t)Src.Int32Val;
1265 Extended = (uint64_t) Src.Int64Val;
1267 // Now that we have a sign extended value, assign it to the destination
1268 INTEGER_ASSIGN(Dest, DBitWidth, Extended);
1272 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
1273 ExecutionContext &SF) {
1274 const Type *SrcTy = SrcVal->getType();
1275 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1276 assert(SrcTy == Type::DoubleTy && DstTy == Type::FloatTy &&
1277 "Invalid FPTrunc instruction");
1278 Dest.FloatVal = (float) Src.DoubleVal;
1282 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
1283 ExecutionContext &SF) {
1284 const Type *SrcTy = SrcVal->getType();
1285 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1286 assert(SrcTy == Type::FloatTy && DstTy == Type::DoubleTy &&
1287 "Invalid FPTrunc instruction");
1288 Dest.DoubleVal = (double) Src.FloatVal;
1292 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
1293 ExecutionContext &SF) {
1294 const Type *SrcTy = SrcVal->getType();
1295 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1296 const IntegerType *DITy = cast<IntegerType>(DstTy);
1297 unsigned DBitWidth = DITy->getBitWidth();
1298 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1299 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
1300 uint64_t Converted = 0;
1301 if (SrcTy->getTypeID() == Type::FloatTyID)
1302 Converted = (uint64_t) Src.FloatVal;
1304 Converted = (uint64_t) Src.DoubleVal;
1306 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1310 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1311 ExecutionContext &SF) {
1312 const Type *SrcTy = SrcVal->getType();
1313 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1314 const IntegerType *DITy = cast<IntegerType>(DstTy);
1315 unsigned DBitWidth = DITy->getBitWidth();
1316 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1317 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1318 int64_t Converted = 0;
1319 if (SrcTy->getTypeID() == Type::FloatTyID)
1320 Converted = (int64_t) Src.FloatVal;
1322 Converted = (int64_t) Src.DoubleVal;
1324 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1328 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1329 ExecutionContext &SF) {
1330 const Type *SrcTy = SrcVal->getType();
1331 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1332 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1333 unsigned SBitWidth = SITy->getBitWidth();
1334 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1335 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1336 uint64_t Converted = 0;
1338 Converted = (uint64_t) Src.Int1Val;
1339 else if (SBitWidth <= 8)
1340 Converted = (uint64_t) Src.Int8Val;
1341 else if (SBitWidth <= 16)
1342 Converted = (uint64_t) Src.Int16Val;
1343 else if (SBitWidth <= 32)
1344 Converted = (uint64_t) Src.Int32Val;
1346 Converted = (uint64_t) Src.Int64Val;
1348 if (DstTy->getTypeID() == Type::FloatTyID)
1349 Dest.FloatVal = (float) Converted;
1351 Dest.DoubleVal = (double) Converted;
1355 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1356 ExecutionContext &SF) {
1357 const Type *SrcTy = SrcVal->getType();
1358 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1359 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1360 unsigned SBitWidth = SITy->getBitWidth();
1361 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1362 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
1363 int64_t Converted = 0;
1365 Converted = 0LL - Src.Int1Val;
1366 else if (SBitWidth <= 8)
1367 Converted = (int64_t) (int8_t)Src.Int8Val;
1368 else if (SBitWidth <= 16)
1369 Converted = (int64_t) (int16_t)Src.Int16Val;
1370 else if (SBitWidth <= 32)
1371 Converted = (int64_t) (int32_t)Src.Int32Val;
1373 Converted = (int64_t) Src.Int64Val;
1375 if (DstTy->getTypeID() == Type::FloatTyID)
1376 Dest.FloatVal = (float) Converted;
1378 Dest.DoubleVal = (double) Converted;
1382 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1383 ExecutionContext &SF) {
1384 const Type *SrcTy = SrcVal->getType();
1385 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1386 const IntegerType *DITy = cast<IntegerType>(DstTy);
1387 unsigned DBitWidth = DITy->getBitWidth();
1388 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1389 assert(isa<PointerType>(SrcTy) && "Invalid PtrToInt instruction");
1390 INTEGER_ASSIGN(Dest, DBitWidth, (intptr_t) Src.PointerVal);
1394 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1395 ExecutionContext &SF) {
1396 const Type *SrcTy = SrcVal->getType();
1397 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1398 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1399 unsigned SBitWidth = SITy->getBitWidth();
1400 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1401 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1402 uint64_t Converted = 0;
1404 Converted = (uint64_t) Src.Int1Val;
1405 else if (SBitWidth <= 8)
1406 Converted = (uint64_t) Src.Int8Val;
1407 else if (SBitWidth <= 16)
1408 Converted = (uint64_t) Src.Int16Val;
1409 else if (SBitWidth <= 32)
1410 Converted = (uint64_t) Src.Int32Val;
1412 Converted = (uint64_t) Src.Int64Val;
1414 Dest.PointerVal = (PointerTy) Converted;
1418 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1419 ExecutionContext &SF) {
1421 const Type *SrcTy = SrcVal->getType();
1422 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1423 if (isa<PointerType>(DstTy)) {
1424 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1425 Dest.PointerVal = Src.PointerVal;
1426 } else if (DstTy->isInteger()) {
1427 const IntegerType *DITy = cast<IntegerType>(DstTy);
1428 unsigned DBitWidth = DITy->getBitWidth();
1429 if (SrcTy == Type::FloatTy) {
1430 Dest.Int32Val = FloatToBits(Src.FloatVal);
1431 } else if (SrcTy == Type::DoubleTy) {
1432 Dest.Int64Val = DoubleToBits(Src.DoubleVal);
1433 } else if (SrcTy->isInteger()) {
1434 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1435 unsigned SBitWidth = SITy->getBitWidth();
1436 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1437 assert(SBitWidth == DBitWidth && "Invalid BitCast");
1439 Dest.Int1Val = Src.Int1Val;
1440 else if (SBitWidth <= 8)
1441 Dest.Int8Val = Src.Int8Val;
1442 else if (SBitWidth <= 16)
1443 Dest.Int16Val = Src.Int16Val;
1444 else if (SBitWidth <= 32)
1445 Dest.Int32Val = Src.Int32Val;
1447 Dest.Int64Val = Src.Int64Val;
1448 maskToBitWidth(Dest, DBitWidth);
1450 assert(0 && "Invalid BitCast");
1451 } else if (DstTy == Type::FloatTy) {
1452 if (SrcTy->isInteger())
1453 Dest.FloatVal = BitsToFloat(Src.Int32Val);
1455 Dest.FloatVal = Src.FloatVal;
1456 } else if (DstTy == Type::DoubleTy) {
1457 if (SrcTy->isInteger())
1458 Dest.DoubleVal = BitsToDouble(Src.Int64Val);
1460 Dest.DoubleVal = Src.DoubleVal;
1462 assert(0 && "Invalid Bitcast");
1467 void Interpreter::visitTruncInst(TruncInst &I) {
1468 ExecutionContext &SF = ECStack.back();
1469 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1472 void Interpreter::visitSExtInst(SExtInst &I) {
1473 ExecutionContext &SF = ECStack.back();
1474 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1477 void Interpreter::visitZExtInst(ZExtInst &I) {
1478 ExecutionContext &SF = ECStack.back();
1479 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1482 void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1483 ExecutionContext &SF = ECStack.back();
1484 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1487 void Interpreter::visitFPExtInst(FPExtInst &I) {
1488 ExecutionContext &SF = ECStack.back();
1489 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1492 void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1493 ExecutionContext &SF = ECStack.back();
1494 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1497 void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1498 ExecutionContext &SF = ECStack.back();
1499 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1502 void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1503 ExecutionContext &SF = ECStack.back();
1504 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1507 void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1508 ExecutionContext &SF = ECStack.back();
1509 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1512 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1513 ExecutionContext &SF = ECStack.back();
1514 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1517 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1518 ExecutionContext &SF = ECStack.back();
1519 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1522 void Interpreter::visitBitCastInst(BitCastInst &I) {
1523 ExecutionContext &SF = ECStack.back();
1524 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1527 #define IMPLEMENT_VAARG(TY) \
1528 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1530 void Interpreter::visitVAArgInst(VAArgInst &I) {
1531 ExecutionContext &SF = ECStack.back();
1533 // Get the incoming valist parameter. LLI treats the valist as a
1534 // (ec-stack-depth var-arg-index) pair.
1535 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1537 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1538 .VarArgs[VAList.UIntPairVal.second];
1539 const Type *Ty = I.getType();
1540 switch (Ty->getTypeID()) {
1541 case Type::IntegerTyID: {
1542 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
1544 Dest.Int1Val = Src.Int1Val;
1545 else if (BitWidth <= 8)
1546 Dest.Int8Val = Src.Int8Val;
1547 else if (BitWidth <= 16)
1548 Dest.Int16Val = Src.Int16Val;
1549 else if (BitWidth <= 32)
1550 Dest.Int32Val = Src.Int32Val;
1551 else if (BitWidth <= 64)
1552 Dest.Int64Val = Src.Int64Val;
1554 assert(0 && "Integer types > 64 bits not supported");
1555 maskToBitWidth(Dest, BitWidth);
1557 IMPLEMENT_VAARG(Pointer);
1558 IMPLEMENT_VAARG(Float);
1559 IMPLEMENT_VAARG(Double);
1561 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1565 // Set the Value of this Instruction.
1566 SetValue(&I, Dest, SF);
1568 // Move the pointer to the next vararg.
1569 ++VAList.UIntPairVal.second;
1572 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1573 ExecutionContext &SF) {
1574 switch (CE->getOpcode()) {
1575 case Instruction::Trunc:
1576 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1577 case Instruction::ZExt:
1578 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1579 case Instruction::SExt:
1580 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1581 case Instruction::FPTrunc:
1582 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1583 case Instruction::FPExt:
1584 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1585 case Instruction::UIToFP:
1586 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1587 case Instruction::SIToFP:
1588 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1589 case Instruction::FPToUI:
1590 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1591 case Instruction::FPToSI:
1592 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1593 case Instruction::PtrToInt:
1594 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1595 case Instruction::IntToPtr:
1596 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1597 case Instruction::BitCast:
1598 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1599 case Instruction::GetElementPtr:
1600 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1601 gep_type_end(CE), SF);
1606 initializeAPInt(Dest, CE->getType(), SF);
1607 switch (CE->getOpcode()) {
1608 case Instruction::Add:
1609 executeAddInst(Dest, getOperandValue(CE->getOperand(0), SF),
1610 getOperandValue(CE->getOperand(1), SF),
1611 CE->getOperand(0)->getType());
1612 case Instruction::Sub:
1613 executeSubInst(Dest, getOperandValue(CE->getOperand(0), SF),
1614 getOperandValue(CE->getOperand(1), SF),
1615 CE->getOperand(0)->getType());
1616 case Instruction::Mul:
1617 executeMulInst(Dest, getOperandValue(CE->getOperand(0), SF),
1618 getOperandValue(CE->getOperand(1), SF),
1619 CE->getOperand(0)->getType());
1620 case Instruction::SDiv:
1621 executeSDivInst(Dest, getOperandValue(CE->getOperand(0), SF),
1622 getOperandValue(CE->getOperand(1), SF),
1623 CE->getOperand(0)->getType());
1624 case Instruction::UDiv:
1625 executeUDivInst(Dest, getOperandValue(CE->getOperand(0), SF),
1626 getOperandValue(CE->getOperand(1), SF),
1627 CE->getOperand(0)->getType());
1628 case Instruction::FDiv:
1629 executeFDivInst(Dest, getOperandValue(CE->getOperand(0), SF),
1630 getOperandValue(CE->getOperand(1), SF),
1631 CE->getOperand(0)->getType());
1632 case Instruction::URem:
1633 executeURemInst(Dest, getOperandValue(CE->getOperand(0), SF),
1634 getOperandValue(CE->getOperand(1), SF),
1635 CE->getOperand(0)->getType());
1636 case Instruction::SRem:
1637 executeSRemInst(Dest, getOperandValue(CE->getOperand(0), SF),
1638 getOperandValue(CE->getOperand(1), SF),
1639 CE->getOperand(0)->getType());
1640 case Instruction::FRem:
1641 executeFRemInst(Dest, getOperandValue(CE->getOperand(0), SF),
1642 getOperandValue(CE->getOperand(1), SF),
1643 CE->getOperand(0)->getType());
1644 case Instruction::And:
1645 executeAndInst(Dest, getOperandValue(CE->getOperand(0), SF),
1646 getOperandValue(CE->getOperand(1), SF),
1647 CE->getOperand(0)->getType());
1648 case Instruction::Or:
1649 executeOrInst(Dest, getOperandValue(CE->getOperand(0), SF),
1650 getOperandValue(CE->getOperand(1), SF),
1651 CE->getOperand(0)->getType());
1652 case Instruction::Xor:
1653 executeXorInst(Dest, getOperandValue(CE->getOperand(0), SF),
1654 getOperandValue(CE->getOperand(1), SF),
1655 CE->getOperand(0)->getType());
1656 case Instruction::FCmp:
1657 case Instruction::ICmp:
1658 return executeCmpInst(CE->getPredicate(),
1659 getOperandValue(CE->getOperand(0), SF),
1660 getOperandValue(CE->getOperand(1), SF),
1661 CE->getOperand(0)->getType());
1662 case Instruction::Shl:
1663 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
1664 getOperandValue(CE->getOperand(1), SF),
1665 CE->getOperand(0)->getType());
1666 case Instruction::LShr:
1667 return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
1668 getOperandValue(CE->getOperand(1), SF),
1669 CE->getOperand(0)->getType());
1670 case Instruction::AShr:
1671 return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
1672 getOperandValue(CE->getOperand(1), SF),
1673 CE->getOperand(0)->getType());
1674 case Instruction::Select:
1675 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1676 getOperandValue(CE->getOperand(1), SF),
1677 getOperandValue(CE->getOperand(2), SF));
1679 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
1681 return GenericValue();
1685 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1686 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1687 return getConstantExprValue(CE, SF);
1688 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1689 return getConstantValue(CPV);
1690 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1691 return PTOGV(getPointerToGlobal(GV));
1693 return SF.Values[V];
1697 //===----------------------------------------------------------------------===//
1698 // Dispatch and Execution Code
1699 //===----------------------------------------------------------------------===//
1701 //===----------------------------------------------------------------------===//
1702 // callFunction - Execute the specified function...
1704 void Interpreter::callFunction(Function *F,
1705 const std::vector<GenericValue> &ArgVals) {
1706 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1707 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1708 "Incorrect number of arguments passed into function call!");
1709 // Make a new stack frame... and fill it in.
1710 ECStack.push_back(ExecutionContext());
1711 ExecutionContext &StackFrame = ECStack.back();
1712 StackFrame.CurFunction = F;
1714 // Special handling for external functions.
1715 if (F->isDeclaration()) {
1716 GenericValue Result = callExternalFunction (F, ArgVals);
1717 // Simulate a 'ret' instruction of the appropriate type.
1718 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1722 // Get pointers to first LLVM BB & Instruction in function.
1723 StackFrame.CurBB = F->begin();
1724 StackFrame.CurInst = StackFrame.CurBB->begin();
1726 // Run through the function arguments and initialize their values...
1727 assert((ArgVals.size() == F->arg_size() ||
1728 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1729 "Invalid number of values passed to function invocation!");
1731 // Handle non-varargs arguments...
1733 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
1734 SetValue(AI, ArgVals[i], StackFrame);
1736 // Handle varargs arguments...
1737 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1740 void Interpreter::run() {
1741 while (!ECStack.empty()) {
1742 // Interpret a single instruction & increment the "PC".
1743 ExecutionContext &SF = ECStack.back(); // Current stack frame
1744 Instruction &I = *SF.CurInst++; // Increment before execute
1746 // Track the number of dynamic instructions executed.
1749 DOUT << "About to interpret: " << I;
1750 visit(I); // Dispatch to one of the visit* methods...