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/Statistic.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/MathExtras.h"
27 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
28 static Interpreter *TheEE = 0;
31 //===----------------------------------------------------------------------===//
32 // Value Manipulation code
33 //===----------------------------------------------------------------------===//
35 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
37 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
39 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
41 static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
43 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
45 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
47 static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
49 static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
51 static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
53 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
55 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
57 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
59 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
60 GenericValue Src2, const Type *Ty);
61 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
63 static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
65 static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
67 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
70 inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
71 // Determine if the value is signed or not
72 bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
73 // If its signed, extend the sign bits
75 Val |= ~ITy->getBitMask();
79 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
80 ExecutionContext &SF) {
81 switch (CE->getOpcode()) {
82 case Instruction::Trunc:
83 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
84 case Instruction::ZExt:
85 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
86 case Instruction::SExt:
87 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
88 case Instruction::FPTrunc:
89 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
90 case Instruction::FPExt:
91 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
92 case Instruction::UIToFP:
93 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
94 case Instruction::SIToFP:
95 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
96 case Instruction::FPToUI:
97 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
98 case Instruction::FPToSI:
99 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
100 case Instruction::PtrToInt:
101 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
102 case Instruction::IntToPtr:
103 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
104 case Instruction::BitCast:
105 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
106 case Instruction::GetElementPtr:
107 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
108 gep_type_end(CE), SF);
109 case Instruction::Add:
110 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
111 getOperandValue(CE->getOperand(1), SF),
112 CE->getOperand(0)->getType());
113 case Instruction::Sub:
114 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
115 getOperandValue(CE->getOperand(1), SF),
116 CE->getOperand(0)->getType());
117 case Instruction::Mul:
118 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
119 getOperandValue(CE->getOperand(1), SF),
120 CE->getOperand(0)->getType());
121 case Instruction::SDiv:
122 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
123 getOperandValue(CE->getOperand(1), SF),
124 CE->getOperand(0)->getType());
125 case Instruction::UDiv:
126 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
127 getOperandValue(CE->getOperand(1), SF),
128 CE->getOperand(0)->getType());
129 case Instruction::FDiv:
130 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
131 getOperandValue(CE->getOperand(1), SF),
132 CE->getOperand(0)->getType());
133 case Instruction::URem:
134 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
135 getOperandValue(CE->getOperand(1), SF),
136 CE->getOperand(0)->getType());
137 case Instruction::SRem:
138 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
139 getOperandValue(CE->getOperand(1), SF),
140 CE->getOperand(0)->getType());
141 case Instruction::FRem:
142 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
143 getOperandValue(CE->getOperand(1), SF),
144 CE->getOperand(0)->getType());
145 case Instruction::And:
146 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
147 getOperandValue(CE->getOperand(1), SF),
148 CE->getOperand(0)->getType());
149 case Instruction::Or:
150 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
151 getOperandValue(CE->getOperand(1), SF),
152 CE->getOperand(0)->getType());
153 case Instruction::Xor:
154 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
155 getOperandValue(CE->getOperand(1), SF),
156 CE->getOperand(0)->getType());
157 case Instruction::FCmp:
158 case Instruction::ICmp:
159 return executeCmpInst(CE->getPredicate(),
160 getOperandValue(CE->getOperand(0), SF),
161 getOperandValue(CE->getOperand(1), SF),
162 CE->getOperand(0)->getType());
163 case Instruction::Shl:
164 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
165 getOperandValue(CE->getOperand(1), SF),
166 CE->getOperand(0)->getType());
167 case Instruction::LShr:
168 return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
169 getOperandValue(CE->getOperand(1), SF),
170 CE->getOperand(0)->getType());
171 case Instruction::AShr:
172 return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
173 getOperandValue(CE->getOperand(1), SF),
174 CE->getOperand(0)->getType());
175 case Instruction::Select:
176 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
177 getOperandValue(CE->getOperand(1), SF),
178 getOperandValue(CE->getOperand(2), SF));
180 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
182 return GenericValue();
186 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
187 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
188 return getConstantExprValue(CE, SF);
189 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
190 return getConstantValue(CPV);
191 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
192 return PTOGV(getPointerToGlobal(GV));
198 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
202 void Interpreter::initializeExecutionEngine() {
206 //===----------------------------------------------------------------------===//
207 // Binary Instruction Implementations
208 //===----------------------------------------------------------------------===//
210 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
211 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
213 #define IMPLEMENT_INTEGER_BINOP(OP, TY) \
214 case Type::IntegerTyID: { \
215 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
217 Dest.Int1Val = Src1.Int1Val OP Src2.Int1Val; \
218 else if (BitWidth <= 8) \
219 Dest.Int8Val = Src1.Int8Val OP Src2.Int8Val; \
220 else if (BitWidth <= 16) \
221 Dest.Int16Val = Src1.Int16Val OP Src2.Int16Val; \
222 else if (BitWidth <= 32) \
223 Dest.Int32Val = Src1.Int32Val OP Src2.Int32Val; \
224 else if (BitWidth <= 64) \
225 Dest.Int64Val = Src1.Int64Val OP Src2.Int64Val; \
227 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
228 maskToBitWidth(Dest, BitWidth); \
232 #define IMPLEMENT_SIGNED_BINOP(OP, TY) \
233 if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
234 unsigned BitWidth = ITy->getBitWidth(); \
236 Dest.Int8Val = ((int8_t)Src1.Int8Val) OP ((int8_t)Src2.Int8Val); \
237 else if (BitWidth <= 16) \
238 Dest.Int16Val = ((int16_t)Src1.Int16Val) OP ((int16_t)Src2.Int16Val); \
239 else if (BitWidth <= 32) \
240 Dest.Int32Val = ((int32_t)Src1.Int32Val) OP ((int32_t)Src2.Int32Val); \
241 else if (BitWidth <= 64) \
242 Dest.Int64Val = ((int64_t)Src1.Int64Val) OP ((int64_t)Src2.Int64Val); \
244 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
247 maskToBitWidth(Dest, BitWidth); \
249 cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
253 #define IMPLEMENT_UNSIGNED_BINOP(OP, TY) \
254 if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
255 unsigned BitWidth = ITy->getBitWidth(); \
257 Dest.Int8Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
258 else if (BitWidth <= 16) \
259 Dest.Int16Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
260 else if (BitWidth <= 32) \
261 Dest.Int32Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
262 else if (BitWidth <= 64) \
263 Dest.Int64Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
265 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
268 maskToBitWidth(Dest, BitWidth); \
270 cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
274 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
277 switch (Ty->getTypeID()) {
278 IMPLEMENT_INTEGER_BINOP(+, Ty);
279 IMPLEMENT_BINARY_OPERATOR(+, Float);
280 IMPLEMENT_BINARY_OPERATOR(+, Double);
282 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
288 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
291 switch (Ty->getTypeID()) {
292 IMPLEMENT_INTEGER_BINOP(-, Ty);
293 IMPLEMENT_BINARY_OPERATOR(-, Float);
294 IMPLEMENT_BINARY_OPERATOR(-, Double);
296 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
302 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
305 switch (Ty->getTypeID()) {
306 IMPLEMENT_INTEGER_BINOP(*, Ty);
307 IMPLEMENT_BINARY_OPERATOR(*, Float);
308 IMPLEMENT_BINARY_OPERATOR(*, Double);
310 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
316 static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
319 IMPLEMENT_UNSIGNED_BINOP(/,Ty)
323 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
326 IMPLEMENT_SIGNED_BINOP(/,Ty)
330 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
333 switch (Ty->getTypeID()) {
334 IMPLEMENT_BINARY_OPERATOR(/, Float);
335 IMPLEMENT_BINARY_OPERATOR(/, Double);
337 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
343 static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
346 IMPLEMENT_UNSIGNED_BINOP(%, Ty)
350 static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
353 IMPLEMENT_SIGNED_BINOP(%, Ty)
357 static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
360 switch (Ty->getTypeID()) {
361 case Type::FloatTyID:
362 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
364 case Type::DoubleTyID:
365 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
368 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
374 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
377 IMPLEMENT_UNSIGNED_BINOP(&, Ty)
381 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
384 IMPLEMENT_UNSIGNED_BINOP(|, Ty)
388 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
391 IMPLEMENT_UNSIGNED_BINOP(^, Ty)
395 #define IMPLEMENT_SIGNED_ICMP(OP, TY) \
396 case Type::IntegerTyID: { \
397 const IntegerType* ITy = cast<IntegerType>(TY); \
398 unsigned BitWidth = ITy->getBitWidth(); \
399 int64_t LHS = 0, RHS = 0; \
400 if (BitWidth <= 8) { \
401 LHS = int64_t(doSignExtension(uint64_t(Src1.Int8Val), ITy)); \
402 RHS = int64_t(doSignExtension(uint64_t(Src2.Int8Val), ITy)); \
403 } else if (BitWidth <= 16) { \
404 LHS = int64_t(doSignExtension(uint64_t(Src1.Int16Val), ITy)); \
405 RHS = int64_t(doSignExtension(uint64_t(Src2.Int16Val), ITy)); \
406 } else if (BitWidth <= 32) { \
407 LHS = int64_t(doSignExtension(uint64_t(Src1.Int32Val), ITy)); \
408 RHS = int64_t(doSignExtension(uint64_t(Src2.Int32Val), ITy)); \
409 } else if (BitWidth <= 64) { \
410 LHS = int64_t(doSignExtension(uint64_t(Src1.Int64Val), ITy)); \
411 RHS = int64_t(doSignExtension(uint64_t(Src2.Int64Val), ITy)); \
413 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
416 Dest.Int1Val = LHS OP RHS; \
420 #define IMPLEMENT_UNSIGNED_ICMP(OP, TY) \
421 case Type::IntegerTyID: { \
422 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
424 Dest.Int1Val = ((uint8_t)Src1.Int1Val) OP ((uint8_t)Src2.Int1Val); \
425 else if (BitWidth <= 8) \
426 Dest.Int1Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
427 else if (BitWidth <= 16) \
428 Dest.Int1Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
429 else if (BitWidth <= 32) \
430 Dest.Int1Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
431 else if (BitWidth <= 64) \
432 Dest.Int1Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
434 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
437 maskToBitWidth(Dest, BitWidth); \
441 // Handle pointers specially because they must be compared with only as much
442 // width as the host has. We _do not_ want to be comparing 64 bit values when
443 // running on a 32-bit target, otherwise the upper 32 bits might mess up
444 // comparisons if they contain garbage.
445 #define IMPLEMENT_POINTER_ICMP(OP) \
446 case Type::PointerTyID: \
447 Dest.Int1Val = (void*)(intptr_t)Src1.PointerVal OP \
448 (void*)(intptr_t)Src2.PointerVal; break
450 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
453 switch (Ty->getTypeID()) {
454 IMPLEMENT_UNSIGNED_ICMP(==, Ty);
455 IMPLEMENT_POINTER_ICMP(==);
457 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
463 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
466 switch (Ty->getTypeID()) {
467 IMPLEMENT_UNSIGNED_ICMP(!=, Ty);
468 IMPLEMENT_POINTER_ICMP(!=);
470 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
476 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
479 switch (Ty->getTypeID()) {
480 IMPLEMENT_UNSIGNED_ICMP(<, Ty);
481 IMPLEMENT_POINTER_ICMP(<);
483 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
489 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
492 switch (Ty->getTypeID()) {
493 IMPLEMENT_SIGNED_ICMP(<, Ty);
494 IMPLEMENT_POINTER_ICMP(<);
496 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
502 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
505 switch (Ty->getTypeID()) {
506 IMPLEMENT_UNSIGNED_ICMP(>, Ty);
507 IMPLEMENT_POINTER_ICMP(>);
509 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
515 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
518 switch (Ty->getTypeID()) {
519 IMPLEMENT_SIGNED_ICMP(>, Ty);
520 IMPLEMENT_POINTER_ICMP(>);
522 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
528 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
531 switch (Ty->getTypeID()) {
532 IMPLEMENT_UNSIGNED_ICMP(<=, Ty);
533 IMPLEMENT_POINTER_ICMP(<=);
535 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
541 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
544 switch (Ty->getTypeID()) {
545 IMPLEMENT_SIGNED_ICMP(<=, Ty);
546 IMPLEMENT_POINTER_ICMP(<=);
548 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
554 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
557 switch (Ty->getTypeID()) {
558 IMPLEMENT_UNSIGNED_ICMP(>=,Ty);
559 IMPLEMENT_POINTER_ICMP(>=);
561 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
567 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
570 switch (Ty->getTypeID()) {
571 IMPLEMENT_SIGNED_ICMP(>=, Ty);
572 IMPLEMENT_POINTER_ICMP(>=);
574 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
580 void Interpreter::visitICmpInst(ICmpInst &I) {
581 ExecutionContext &SF = ECStack.back();
582 const Type *Ty = I.getOperand(0)->getType();
583 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
584 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
585 GenericValue R; // Result
587 switch (I.getPredicate()) {
588 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
589 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
590 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
591 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
592 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
593 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
594 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
595 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
596 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
597 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
599 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
606 #define IMPLEMENT_FCMP(OP, TY) \
607 case Type::TY##TyID: Dest.Int1Val = Src1.TY##Val OP Src2.TY##Val; break
609 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
612 switch (Ty->getTypeID()) {
613 IMPLEMENT_FCMP(==, Float);
614 IMPLEMENT_FCMP(==, Double);
616 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
622 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
625 switch (Ty->getTypeID()) {
626 IMPLEMENT_FCMP(!=, Float);
627 IMPLEMENT_FCMP(!=, Double);
630 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
636 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
639 switch (Ty->getTypeID()) {
640 IMPLEMENT_FCMP(<=, Float);
641 IMPLEMENT_FCMP(<=, Double);
643 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
649 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
652 switch (Ty->getTypeID()) {
653 IMPLEMENT_FCMP(>=, Float);
654 IMPLEMENT_FCMP(>=, Double);
656 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
662 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
665 switch (Ty->getTypeID()) {
666 IMPLEMENT_FCMP(<, Float);
667 IMPLEMENT_FCMP(<, Double);
669 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
675 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
678 switch (Ty->getTypeID()) {
679 IMPLEMENT_FCMP(>, Float);
680 IMPLEMENT_FCMP(>, Double);
682 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
688 #define IMPLEMENT_UNORDERED(TY, X,Y) \
689 if (TY == Type::FloatTy) \
690 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
691 Dest.Int1Val = true; \
694 else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
695 Dest.Int1Val = true; \
700 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
703 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
704 return executeFCMP_OEQ(Src1, Src2, Ty);
707 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
710 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
711 return executeFCMP_ONE(Src1, Src2, Ty);
714 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
717 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
718 return executeFCMP_OLE(Src1, Src2, Ty);
721 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
724 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
725 return executeFCMP_OGE(Src1, Src2, Ty);
728 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
731 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
732 return executeFCMP_OLT(Src1, Src2, Ty);
735 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
738 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
739 return executeFCMP_OGT(Src1, Src2, Ty);
742 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
745 if (Ty == Type::FloatTy)
746 Dest.Int1Val = (Src1.FloatVal == Src1.FloatVal &&
747 Src2.FloatVal == Src2.FloatVal);
749 Dest.Int1Val = (Src1.DoubleVal == Src1.DoubleVal &&
750 Src2.DoubleVal == Src2.DoubleVal);
754 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
757 if (Ty == Type::FloatTy)
758 Dest.Int1Val = (Src1.FloatVal != Src1.FloatVal ||
759 Src2.FloatVal != Src2.FloatVal);
761 Dest.Int1Val = (Src1.DoubleVal != Src1.DoubleVal ||
762 Src2.DoubleVal != Src2.DoubleVal);
766 void Interpreter::visitFCmpInst(FCmpInst &I) {
767 ExecutionContext &SF = ECStack.back();
768 const Type *Ty = I.getOperand(0)->getType();
769 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
770 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
771 GenericValue R; // Result
773 switch (I.getPredicate()) {
774 case FCmpInst::FCMP_FALSE: R.Int1Val = false; break;
775 case FCmpInst::FCMP_TRUE: R.Int1Val = true; break;
776 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
777 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
778 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
779 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
780 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
781 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
782 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
783 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
784 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
785 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
786 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
787 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
788 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
789 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
791 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
798 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
799 GenericValue Src2, const Type *Ty) {
802 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
803 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
804 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
805 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
806 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
807 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
808 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
809 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
810 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
811 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
812 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
813 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
814 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
815 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
816 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
817 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
818 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
819 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
820 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
821 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
822 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
823 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
824 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
825 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
826 case FCmpInst::FCMP_FALSE: {
828 Result.Int1Val = false;
831 case FCmpInst::FCMP_TRUE: {
833 Result.Int1Val = true;
837 cerr << "Unhandled Cmp predicate\n";
842 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
843 ExecutionContext &SF = ECStack.back();
844 const Type *Ty = I.getOperand(0)->getType();
845 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
846 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
847 GenericValue R; // Result
849 switch (I.getOpcode()) {
850 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
851 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
852 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
853 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
854 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
855 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
856 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
857 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
858 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
859 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
860 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
861 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
863 cerr << "Don't know how to handle this binary operator!\n-->" << I;
870 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
872 return Src1.Int1Val ? Src2 : Src3;
875 void Interpreter::visitSelectInst(SelectInst &I) {
876 ExecutionContext &SF = ECStack.back();
877 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
878 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
879 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
880 GenericValue R = executeSelectInst(Src1, Src2, Src3);
885 //===----------------------------------------------------------------------===//
886 // Terminator Instruction Implementations
887 //===----------------------------------------------------------------------===//
889 void Interpreter::exitCalled(GenericValue GV) {
890 // runAtExitHandlers() assumes there are no stack frames, but
891 // if exit() was called, then it had a stack frame. Blow away
892 // the stack before interpreting atexit handlers.
894 runAtExitHandlers ();
898 /// Pop the last stack frame off of ECStack and then copy the result
899 /// back into the result variable if we are not returning void. The
900 /// result variable may be the ExitValue, or the Value of the calling
901 /// CallInst if there was a previous stack frame. This method may
902 /// invalidate any ECStack iterators you have. This method also takes
903 /// care of switching to the normal destination BB, if we are returning
906 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
907 GenericValue Result) {
908 // Pop the current stack frame.
911 if (ECStack.empty()) { // Finished main. Put result into exit code...
912 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
913 ExitValue = Result; // Capture the exit value of the program
915 memset(&ExitValue, 0, sizeof(ExitValue));
918 // If we have a previous stack frame, and we have a previous call,
919 // fill in the return value...
920 ExecutionContext &CallingSF = ECStack.back();
921 if (Instruction *I = CallingSF.Caller.getInstruction()) {
922 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
923 SetValue(I, Result, CallingSF);
924 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
925 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
926 CallingSF.Caller = CallSite(); // We returned from the call...
931 void Interpreter::visitReturnInst(ReturnInst &I) {
932 ExecutionContext &SF = ECStack.back();
933 const Type *RetTy = Type::VoidTy;
936 // Save away the return value... (if we are not 'ret void')
937 if (I.getNumOperands()) {
938 RetTy = I.getReturnValue()->getType();
939 Result = getOperandValue(I.getReturnValue(), SF);
942 popStackAndReturnValueToCaller(RetTy, Result);
945 void Interpreter::visitUnwindInst(UnwindInst &I) {
950 if (ECStack.empty ())
952 Inst = ECStack.back ().Caller.getInstruction ();
953 } while (!(Inst && isa<InvokeInst> (Inst)));
955 // Return from invoke
956 ExecutionContext &InvokingSF = ECStack.back ();
957 InvokingSF.Caller = CallSite ();
959 // Go to exceptional destination BB of invoke instruction
960 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
963 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
964 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
968 void Interpreter::visitBranchInst(BranchInst &I) {
969 ExecutionContext &SF = ECStack.back();
972 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
973 if (!I.isUnconditional()) {
974 Value *Cond = I.getCondition();
975 if (getOperandValue(Cond, SF).Int1Val == 0) // If false cond...
976 Dest = I.getSuccessor(1);
978 SwitchToNewBasicBlock(Dest, SF);
981 void Interpreter::visitSwitchInst(SwitchInst &I) {
982 ExecutionContext &SF = ECStack.back();
983 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
984 const Type *ElTy = I.getOperand(0)->getType();
986 // Check to see if any of the cases match...
987 BasicBlock *Dest = 0;
988 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
989 if (executeICMP_EQ(CondVal,
990 getOperandValue(I.getOperand(i), SF), ElTy).Int1Val) {
991 Dest = cast<BasicBlock>(I.getOperand(i+1));
995 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
996 SwitchToNewBasicBlock(Dest, SF);
999 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
1000 // This function handles the actual updating of block and instruction iterators
1001 // as well as execution of all of the PHI nodes in the destination block.
1003 // This method does this because all of the PHI nodes must be executed
1004 // atomically, reading their inputs before any of the results are updated. Not
1005 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
1006 // their inputs. If the input PHI node is updated before it is read, incorrect
1007 // results can happen. Thus we use a two phase approach.
1009 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
1010 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
1011 SF.CurBB = Dest; // Update CurBB to branch destination
1012 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
1014 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
1016 // Loop over all of the PHI nodes in the current block, reading their inputs.
1017 std::vector<GenericValue> ResultValues;
1019 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
1020 // Search for the value corresponding to this previous bb...
1021 int i = PN->getBasicBlockIndex(PrevBB);
1022 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
1023 Value *IncomingValue = PN->getIncomingValue(i);
1025 // Save the incoming value for this PHI node...
1026 ResultValues.push_back(getOperandValue(IncomingValue, SF));
1029 // Now loop over all of the PHI nodes setting their values...
1030 SF.CurInst = SF.CurBB->begin();
1031 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
1032 PHINode *PN = cast<PHINode>(SF.CurInst);
1033 SetValue(PN, ResultValues[i], SF);
1037 //===----------------------------------------------------------------------===//
1038 // Memory Instruction Implementations
1039 //===----------------------------------------------------------------------===//
1041 void Interpreter::visitAllocationInst(AllocationInst &I) {
1042 ExecutionContext &SF = ECStack.back();
1044 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
1046 // Get the number of elements being allocated by the array...
1047 unsigned NumElements = getOperandValue(I.getOperand(0), SF).Int32Val;
1049 // Allocate enough memory to hold the type...
1050 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
1052 GenericValue Result = PTOGV(Memory);
1053 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
1054 SetValue(&I, Result, SF);
1056 if (I.getOpcode() == Instruction::Alloca)
1057 ECStack.back().Allocas.add(Memory);
1060 void Interpreter::visitFreeInst(FreeInst &I) {
1061 ExecutionContext &SF = ECStack.back();
1062 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
1063 GenericValue Value = getOperandValue(I.getOperand(0), SF);
1064 // TODO: Check to make sure memory is allocated
1065 free(GVTOP(Value)); // Free memory
1068 // getElementOffset - The workhorse for getelementptr.
1070 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
1071 gep_type_iterator E,
1072 ExecutionContext &SF) {
1073 assert(isa<PointerType>(Ptr->getType()) &&
1074 "Cannot getElementOffset of a nonpointer type!");
1076 PointerTy Total = 0;
1078 for (; I != E; ++I) {
1079 if (const StructType *STy = dyn_cast<StructType>(*I)) {
1080 const StructLayout *SLO = TD.getStructLayout(STy);
1082 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
1083 unsigned Index = unsigned(CPU->getZExtValue());
1085 Total += (PointerTy)SLO->MemberOffsets[Index];
1087 const SequentialType *ST = cast<SequentialType>(*I);
1088 // Get the index number for the array... which must be long type...
1089 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
1093 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
1095 Idx = (int64_t)(int32_t)IdxGV.Int32Val;
1096 else if (BitWidth == 64)
1097 Idx = (int64_t)IdxGV.Int64Val;
1099 assert(0 && "Invalid index type for getelementptr");
1100 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
1104 GenericValue Result;
1105 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
1109 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1110 ExecutionContext &SF = ECStack.back();
1111 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
1112 gep_type_begin(I), gep_type_end(I), SF), SF);
1115 void Interpreter::visitLoadInst(LoadInst &I) {
1116 ExecutionContext &SF = ECStack.back();
1117 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
1118 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
1119 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
1120 SetValue(&I, Result, SF);
1123 void Interpreter::visitStoreInst(StoreInst &I) {
1124 ExecutionContext &SF = ECStack.back();
1125 GenericValue Val = getOperandValue(I.getOperand(0), SF);
1126 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
1127 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
1128 I.getOperand(0)->getType());
1131 //===----------------------------------------------------------------------===//
1132 // Miscellaneous Instruction Implementations
1133 //===----------------------------------------------------------------------===//
1135 void Interpreter::visitCallSite(CallSite CS) {
1136 ExecutionContext &SF = ECStack.back();
1138 // Check to see if this is an intrinsic function call...
1139 if (Function *F = CS.getCalledFunction())
1140 if (F->isDeclaration ())
1141 switch (F->getIntrinsicID()) {
1142 case Intrinsic::not_intrinsic:
1144 case Intrinsic::vastart: { // va_start
1145 GenericValue ArgIndex;
1146 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1147 ArgIndex.UIntPairVal.second = 0;
1148 SetValue(CS.getInstruction(), ArgIndex, SF);
1151 case Intrinsic::vaend: // va_end is a noop for the interpreter
1153 case Intrinsic::vacopy: // va_copy: dest = src
1154 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1157 // If it is an unknown intrinsic function, use the intrinsic lowering
1158 // class to transform it into hopefully tasty LLVM code.
1160 Instruction *Prev = CS.getInstruction()->getPrev();
1161 BasicBlock *Parent = CS.getInstruction()->getParent();
1162 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1164 // Restore the CurInst pointer to the first instruction newly inserted, if
1167 SF.CurInst = Parent->begin();
1176 std::vector<GenericValue> ArgVals;
1177 const unsigned NumArgs = SF.Caller.arg_size();
1178 ArgVals.reserve(NumArgs);
1179 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1180 e = SF.Caller.arg_end(); i != e; ++i) {
1182 ArgVals.push_back(getOperandValue(V, SF));
1183 // Promote all integral types whose size is < sizeof(int) into ints. We do
1184 // this by zero or sign extending the value as appropriate according to the
1186 const Type *Ty = V->getType();
1187 if (Ty->isInteger()) {
1188 if (Ty->getPrimitiveSizeInBits() == 1)
1189 ArgVals.back().Int32Val = ArgVals.back().Int1Val;
1190 else if (Ty->getPrimitiveSizeInBits() <= 8)
1191 ArgVals.back().Int32Val = ArgVals.back().Int8Val;
1192 else if (Ty->getPrimitiveSizeInBits() <= 16)
1193 ArgVals.back().Int32Val = ArgVals.back().Int16Val;
1197 // To handle indirect calls, we must get the pointer value from the argument
1198 // and treat it as a function pointer.
1199 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
1200 callFunction((Function*)GVTOP(SRC), ArgVals);
1203 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
1206 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1207 unsigned BitWidth = ITy->getBitWidth();
1209 Dest.Int8Val = ((uint8_t)Src1.Int8Val) << ((uint32_t)Src2.Int8Val);
1210 else if (BitWidth <= 16)
1211 Dest.Int16Val = ((uint16_t)Src1.Int16Val) << ((uint32_t)Src2.Int8Val);
1212 else if (BitWidth <= 32)
1213 Dest.Int32Val = ((uint32_t)Src1.Int32Val) << ((uint32_t)Src2.Int8Val);
1214 else if (BitWidth <= 64)
1215 Dest.Int64Val = ((uint64_t)Src1.Int64Val) << ((uint32_t)Src2.Int8Val);
1217 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1220 maskToBitWidth(Dest, BitWidth);
1222 cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
1228 static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
1231 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1232 unsigned BitWidth = ITy->getBitWidth();
1234 Dest.Int8Val = ((uint8_t)Src1.Int8Val) >> ((uint32_t)Src2.Int8Val);
1235 else if (BitWidth <= 16)
1236 Dest.Int16Val = ((uint16_t)Src1.Int16Val) >> ((uint32_t)Src2.Int8Val);
1237 else if (BitWidth <= 32)
1238 Dest.Int32Val = ((uint32_t)Src1.Int32Val) >> ((uint32_t)Src2.Int8Val);
1239 else if (BitWidth <= 64)
1240 Dest.Int64Val = ((uint64_t)Src1.Int64Val) >> ((uint32_t)Src2.Int8Val);
1242 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1245 maskToBitWidth(Dest, BitWidth);
1247 cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
1253 static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
1256 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1257 unsigned BitWidth = ITy->getBitWidth();
1259 Dest.Int8Val = ((int8_t)Src1.Int8Val) >> ((int32_t)Src2.Int8Val);
1260 else if (BitWidth <= 16)
1261 Dest.Int16Val = ((int16_t)Src1.Int16Val) >> ((int32_t)Src2.Int8Val);
1262 else if (BitWidth <= 32)
1263 Dest.Int32Val = ((int32_t)Src1.Int32Val) >> ((int32_t)Src2.Int8Val);
1264 else if (BitWidth <= 64)
1265 Dest.Int64Val = ((int64_t)Src1.Int64Val) >> ((int32_t)Src2.Int8Val);
1267 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
1270 maskToBitWidth(Dest, BitWidth);
1272 cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
1278 void Interpreter::visitShl(BinaryOperator &I) {
1279 ExecutionContext &SF = ECStack.back();
1280 const Type *Ty = I.getOperand(0)->getType();
1281 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1282 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1284 Dest = executeShlInst (Src1, Src2, Ty);
1285 SetValue(&I, Dest, SF);
1288 void Interpreter::visitLShr(BinaryOperator &I) {
1289 ExecutionContext &SF = ECStack.back();
1290 const Type *Ty = I.getOperand(0)->getType();
1291 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1292 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1294 Dest = executeLShrInst (Src1, Src2, Ty);
1295 SetValue(&I, Dest, SF);
1298 void Interpreter::visitAShr(BinaryOperator &I) {
1299 ExecutionContext &SF = ECStack.back();
1300 const Type *Ty = I.getOperand(0)->getType();
1301 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1302 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1304 Dest = executeAShrInst (Src1, Src2, Ty);
1305 SetValue(&I, Dest, SF);
1308 #define INTEGER_ASSIGN(DEST, BITWIDTH, VAL) \
1310 uint64_t Mask = ~(uint64_t)(0ull) >> (64-BITWIDTH); \
1311 if (BITWIDTH == 1) { \
1312 Dest.Int1Val = (bool) (VAL & Mask); \
1313 } else if (BITWIDTH <= 8) { \
1314 Dest.Int8Val = (uint8_t) (VAL & Mask); \
1315 } else if (BITWIDTH <= 16) { \
1316 Dest.Int16Val = (uint16_t) (VAL & Mask); \
1317 } else if (BITWIDTH <= 32) { \
1318 Dest.Int32Val = (uint32_t) (VAL & Mask); \
1320 Dest.Int64Val = (uint64_t) (VAL & Mask); \
1323 GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
1324 ExecutionContext &SF) {
1325 const Type *SrcTy = SrcVal->getType();
1326 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1327 const IntegerType *DITy = cast<IntegerType>(DstTy);
1328 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1329 unsigned DBitWidth = DITy->getBitWidth();
1330 unsigned SBitWidth = SITy->getBitWidth();
1331 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1332 "Integer types > 64 bits not supported");
1333 assert(SBitWidth > DBitWidth && "Invalid truncate");
1335 // Mask the source value to its actual bit width. This ensures that any
1336 // high order bits are cleared.
1337 uint64_t Mask = (1ULL << DBitWidth) - 1;
1338 uint64_t MaskedVal = 0;
1340 MaskedVal = Src.Int8Val & Mask;
1341 else if (SBitWidth <= 16)
1342 MaskedVal = Src.Int16Val & Mask;
1343 else if (SBitWidth <= 32)
1344 MaskedVal = Src.Int32Val & Mask;
1346 MaskedVal = Src.Int64Val & Mask;
1348 INTEGER_ASSIGN(Dest, DBitWidth, MaskedVal);
1352 GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
1353 ExecutionContext &SF) {
1354 const Type *SrcTy = SrcVal->getType();
1355 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1356 const IntegerType *DITy = cast<IntegerType>(DstTy);
1357 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1358 unsigned DBitWidth = DITy->getBitWidth();
1359 unsigned SBitWidth = SITy->getBitWidth();
1360 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1361 "Integer types > 64 bits not supported");
1362 assert(SBitWidth < DBitWidth && "Invalid sign extend");
1364 // Normalize to a 64-bit value.
1365 uint64_t Normalized = 0;
1367 Normalized = Src.Int8Val;
1368 else if (SBitWidth <= 16)
1369 Normalized = Src.Int16Val;
1370 else if (SBitWidth <= 32)
1371 Normalized = Src.Int32Val;
1373 Normalized = Src.Int64Val;
1375 Normalized = doSignExtension(Normalized, SITy);
1377 // Now that we have a sign extended value, assign it to the destination
1378 INTEGER_ASSIGN(Dest, DBitWidth, Normalized);
1382 GenericValue Interpreter::executeZExtInst(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 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1388 unsigned DBitWidth = DITy->getBitWidth();
1389 unsigned SBitWidth = SITy->getBitWidth();
1390 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1391 "Integer types > 64 bits not supported");
1392 assert(SBitWidth < DBitWidth && "Invalid sign extend");
1393 uint64_t Extended = 0;
1395 // For sign extension from bool, we must extend the source bits.
1396 Extended = (uint64_t) (Src.Int1Val & 1);
1397 else if (SBitWidth <= 8)
1398 Extended = (uint64_t) (uint8_t)Src.Int8Val;
1399 else if (SBitWidth <= 16)
1400 Extended = (uint64_t) (uint16_t)Src.Int16Val;
1401 else if (SBitWidth <= 32)
1402 Extended = (uint64_t) (uint32_t)Src.Int32Val;
1404 Extended = (uint64_t) Src.Int64Val;
1406 // Now that we have a sign extended value, assign it to the destination
1407 INTEGER_ASSIGN(Dest, DBitWidth, Extended);
1411 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
1412 ExecutionContext &SF) {
1413 const Type *SrcTy = SrcVal->getType();
1414 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1415 assert(SrcTy == Type::DoubleTy && DstTy == Type::FloatTy &&
1416 "Invalid FPTrunc instruction");
1417 Dest.FloatVal = (float) Src.DoubleVal;
1421 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
1422 ExecutionContext &SF) {
1423 const Type *SrcTy = SrcVal->getType();
1424 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1425 assert(SrcTy == Type::FloatTy && DstTy == Type::DoubleTy &&
1426 "Invalid FPTrunc instruction");
1427 Dest.DoubleVal = (double) Src.FloatVal;
1431 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
1432 ExecutionContext &SF) {
1433 const Type *SrcTy = SrcVal->getType();
1434 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1435 const IntegerType *DITy = cast<IntegerType>(DstTy);
1436 unsigned DBitWidth = DITy->getBitWidth();
1437 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1438 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
1439 uint64_t Converted = 0;
1440 if (SrcTy->getTypeID() == Type::FloatTyID)
1441 Converted = (uint64_t) Src.FloatVal;
1443 Converted = (uint64_t) Src.DoubleVal;
1445 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1449 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1450 ExecutionContext &SF) {
1451 const Type *SrcTy = SrcVal->getType();
1452 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1453 const IntegerType *DITy = cast<IntegerType>(DstTy);
1454 unsigned DBitWidth = DITy->getBitWidth();
1455 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1456 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1457 int64_t Converted = 0;
1458 if (SrcTy->getTypeID() == Type::FloatTyID)
1459 Converted = (int64_t) Src.FloatVal;
1461 Converted = (int64_t) Src.DoubleVal;
1463 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1467 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1468 ExecutionContext &SF) {
1469 const Type *SrcTy = SrcVal->getType();
1470 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1471 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1472 unsigned SBitWidth = SITy->getBitWidth();
1473 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1474 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1475 uint64_t Converted = 0;
1477 Converted = (uint64_t) Src.Int1Val;
1478 else if (SBitWidth <= 8)
1479 Converted = (uint64_t) Src.Int8Val;
1480 else if (SBitWidth <= 16)
1481 Converted = (uint64_t) Src.Int16Val;
1482 else if (SBitWidth <= 32)
1483 Converted = (uint64_t) Src.Int32Val;
1485 Converted = (uint64_t) Src.Int64Val;
1487 if (DstTy->getTypeID() == Type::FloatTyID)
1488 Dest.FloatVal = (float) Converted;
1490 Dest.DoubleVal = (double) Converted;
1494 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1495 ExecutionContext &SF) {
1496 const Type *SrcTy = SrcVal->getType();
1497 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1498 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1499 unsigned SBitWidth = SITy->getBitWidth();
1500 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1501 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
1502 int64_t Converted = 0;
1504 Converted = 0LL - Src.Int1Val;
1505 else if (SBitWidth <= 8)
1506 Converted = (int64_t) (int8_t)Src.Int8Val;
1507 else if (SBitWidth <= 16)
1508 Converted = (int64_t) (int16_t)Src.Int16Val;
1509 else if (SBitWidth <= 32)
1510 Converted = (int64_t) (int32_t)Src.Int32Val;
1512 Converted = (int64_t) Src.Int64Val;
1514 if (DstTy->getTypeID() == Type::FloatTyID)
1515 Dest.FloatVal = (float) Converted;
1517 Dest.DoubleVal = (double) Converted;
1521 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1522 ExecutionContext &SF) {
1523 const Type *SrcTy = SrcVal->getType();
1524 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1525 const IntegerType *DITy = cast<IntegerType>(DstTy);
1526 unsigned DBitWidth = DITy->getBitWidth();
1527 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1528 assert(isa<PointerType>(SrcTy) && "Invalid PtrToInt instruction");
1529 INTEGER_ASSIGN(Dest, DBitWidth, (intptr_t) Src.PointerVal);
1533 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1534 ExecutionContext &SF) {
1535 const Type *SrcTy = SrcVal->getType();
1536 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1537 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1538 unsigned SBitWidth = SITy->getBitWidth();
1539 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1540 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1541 uint64_t Converted = 0;
1543 Converted = (uint64_t) Src.Int1Val;
1544 else if (SBitWidth <= 8)
1545 Converted = (uint64_t) Src.Int8Val;
1546 else if (SBitWidth <= 16)
1547 Converted = (uint64_t) Src.Int16Val;
1548 else if (SBitWidth <= 32)
1549 Converted = (uint64_t) Src.Int32Val;
1551 Converted = (uint64_t) Src.Int64Val;
1553 Dest.PointerVal = (PointerTy) Converted;
1557 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1558 ExecutionContext &SF) {
1560 const Type *SrcTy = SrcVal->getType();
1561 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1562 if (isa<PointerType>(DstTy)) {
1563 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1564 Dest.PointerVal = Src.PointerVal;
1565 } else if (DstTy->isInteger()) {
1566 const IntegerType *DITy = cast<IntegerType>(DstTy);
1567 unsigned DBitWidth = DITy->getBitWidth();
1568 if (SrcTy == Type::FloatTy) {
1569 Dest.Int32Val = FloatToBits(Src.FloatVal);
1570 } else if (SrcTy == Type::DoubleTy) {
1571 Dest.Int64Val = DoubleToBits(Src.DoubleVal);
1572 } else if (SrcTy->isInteger()) {
1573 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1574 unsigned SBitWidth = SITy->getBitWidth();
1575 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1576 assert(SBitWidth == DBitWidth && "Invalid BitCast");
1578 Dest.Int1Val = Src.Int1Val;
1579 else if (SBitWidth <= 8)
1580 Dest.Int8Val = Src.Int8Val;
1581 else if (SBitWidth <= 16)
1582 Dest.Int16Val = Src.Int16Val;
1583 else if (SBitWidth <= 32)
1584 Dest.Int32Val = Src.Int32Val;
1586 Dest.Int64Val = Src.Int64Val;
1587 maskToBitWidth(Dest, DBitWidth);
1589 assert(0 && "Invalid BitCast");
1590 } else if (DstTy == Type::FloatTy) {
1591 if (SrcTy->isInteger())
1592 Dest.FloatVal = BitsToFloat(Src.Int32Val);
1594 Dest.FloatVal = Src.FloatVal;
1595 } else if (DstTy == Type::DoubleTy) {
1596 if (SrcTy->isInteger())
1597 Dest.DoubleVal = BitsToDouble(Src.Int64Val);
1599 Dest.DoubleVal = Src.DoubleVal;
1601 assert(0 && "Invalid Bitcast");
1606 void Interpreter::visitTruncInst(TruncInst &I) {
1607 ExecutionContext &SF = ECStack.back();
1608 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1611 void Interpreter::visitSExtInst(SExtInst &I) {
1612 ExecutionContext &SF = ECStack.back();
1613 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1616 void Interpreter::visitZExtInst(ZExtInst &I) {
1617 ExecutionContext &SF = ECStack.back();
1618 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1621 void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1622 ExecutionContext &SF = ECStack.back();
1623 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1626 void Interpreter::visitFPExtInst(FPExtInst &I) {
1627 ExecutionContext &SF = ECStack.back();
1628 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1631 void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1632 ExecutionContext &SF = ECStack.back();
1633 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1636 void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1637 ExecutionContext &SF = ECStack.back();
1638 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1641 void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1642 ExecutionContext &SF = ECStack.back();
1643 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1646 void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1647 ExecutionContext &SF = ECStack.back();
1648 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1651 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1652 ExecutionContext &SF = ECStack.back();
1653 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1656 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1657 ExecutionContext &SF = ECStack.back();
1658 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1661 void Interpreter::visitBitCastInst(BitCastInst &I) {
1662 ExecutionContext &SF = ECStack.back();
1663 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1666 #define IMPLEMENT_VAARG(TY) \
1667 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1669 void Interpreter::visitVAArgInst(VAArgInst &I) {
1670 ExecutionContext &SF = ECStack.back();
1672 // Get the incoming valist parameter. LLI treats the valist as a
1673 // (ec-stack-depth var-arg-index) pair.
1674 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1676 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1677 .VarArgs[VAList.UIntPairVal.second];
1678 const Type *Ty = I.getType();
1679 switch (Ty->getTypeID()) {
1680 case Type::IntegerTyID: {
1681 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
1683 Dest.Int1Val = Src.Int1Val;
1684 else if (BitWidth <= 8)
1685 Dest.Int8Val = Src.Int8Val;
1686 else if (BitWidth <= 16)
1687 Dest.Int16Val = Src.Int16Val;
1688 else if (BitWidth <= 32)
1689 Dest.Int32Val = Src.Int32Val;
1690 else if (BitWidth <= 64)
1691 Dest.Int64Val = Src.Int64Val;
1693 assert("Integer types > 64 bits not supported");
1694 maskToBitWidth(Dest, BitWidth);
1696 IMPLEMENT_VAARG(Pointer);
1697 IMPLEMENT_VAARG(Float);
1698 IMPLEMENT_VAARG(Double);
1700 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1704 // Set the Value of this Instruction.
1705 SetValue(&I, Dest, SF);
1707 // Move the pointer to the next vararg.
1708 ++VAList.UIntPairVal.second;
1711 //===----------------------------------------------------------------------===//
1712 // Dispatch and Execution Code
1713 //===----------------------------------------------------------------------===//
1715 //===----------------------------------------------------------------------===//
1716 // callFunction - Execute the specified function...
1718 void Interpreter::callFunction(Function *F,
1719 const std::vector<GenericValue> &ArgVals) {
1720 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1721 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1722 "Incorrect number of arguments passed into function call!");
1723 // Make a new stack frame... and fill it in.
1724 ECStack.push_back(ExecutionContext());
1725 ExecutionContext &StackFrame = ECStack.back();
1726 StackFrame.CurFunction = F;
1728 // Special handling for external functions.
1729 if (F->isDeclaration()) {
1730 GenericValue Result = callExternalFunction (F, ArgVals);
1731 // Simulate a 'ret' instruction of the appropriate type.
1732 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1736 // Get pointers to first LLVM BB & Instruction in function.
1737 StackFrame.CurBB = F->begin();
1738 StackFrame.CurInst = StackFrame.CurBB->begin();
1740 // Run through the function arguments and initialize their values...
1741 assert((ArgVals.size() == F->arg_size() ||
1742 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1743 "Invalid number of values passed to function invocation!");
1745 // Handle non-varargs arguments...
1747 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
1748 SetValue(AI, ArgVals[i], StackFrame);
1750 // Handle varargs arguments...
1751 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1754 void Interpreter::run() {
1755 while (!ECStack.empty()) {
1756 // Interpret a single instruction & increment the "PC".
1757 ExecutionContext &SF = ECStack.back(); // Current stack frame
1758 Instruction &I = *SF.CurInst++; // Increment before execute
1760 // Track the number of dynamic instructions executed.
1763 DOUT << "About to interpret: " << I;
1764 visit(I); // Dispatch to one of the visit* methods...