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 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
71 ExecutionContext &SF) {
72 switch (CE->getOpcode()) {
73 case Instruction::Trunc:
74 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
75 case Instruction::ZExt:
76 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
77 case Instruction::SExt:
78 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
79 case Instruction::FPTrunc:
80 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
81 case Instruction::FPExt:
82 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
83 case Instruction::UIToFP:
84 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
85 case Instruction::SIToFP:
86 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
87 case Instruction::FPToUI:
88 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
89 case Instruction::FPToSI:
90 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
91 case Instruction::PtrToInt:
92 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
93 case Instruction::IntToPtr:
94 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
95 case Instruction::BitCast:
96 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
97 case Instruction::GetElementPtr:
98 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
99 gep_type_end(CE), SF);
100 case Instruction::Add:
101 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
102 getOperandValue(CE->getOperand(1), SF),
103 CE->getOperand(0)->getType());
104 case Instruction::Sub:
105 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
106 getOperandValue(CE->getOperand(1), SF),
107 CE->getOperand(0)->getType());
108 case Instruction::Mul:
109 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
110 getOperandValue(CE->getOperand(1), SF),
111 CE->getOperand(0)->getType());
112 case Instruction::SDiv:
113 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
114 getOperandValue(CE->getOperand(1), SF),
115 CE->getOperand(0)->getType());
116 case Instruction::UDiv:
117 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
118 getOperandValue(CE->getOperand(1), SF),
119 CE->getOperand(0)->getType());
120 case Instruction::FDiv:
121 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
122 getOperandValue(CE->getOperand(1), SF),
123 CE->getOperand(0)->getType());
124 case Instruction::URem:
125 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
126 getOperandValue(CE->getOperand(1), SF),
127 CE->getOperand(0)->getType());
128 case Instruction::SRem:
129 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
130 getOperandValue(CE->getOperand(1), SF),
131 CE->getOperand(0)->getType());
132 case Instruction::FRem:
133 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
134 getOperandValue(CE->getOperand(1), SF),
135 CE->getOperand(0)->getType());
136 case Instruction::And:
137 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
138 getOperandValue(CE->getOperand(1), SF),
139 CE->getOperand(0)->getType());
140 case Instruction::Or:
141 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
142 getOperandValue(CE->getOperand(1), SF),
143 CE->getOperand(0)->getType());
144 case Instruction::Xor:
145 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
146 getOperandValue(CE->getOperand(1), SF),
147 CE->getOperand(0)->getType());
148 case Instruction::FCmp:
149 case Instruction::ICmp:
150 return executeCmpInst(CE->getPredicate(),
151 getOperandValue(CE->getOperand(0), SF),
152 getOperandValue(CE->getOperand(1), SF),
153 CE->getOperand(0)->getType());
154 case Instruction::Shl:
155 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
156 getOperandValue(CE->getOperand(1), SF),
157 CE->getOperand(0)->getType());
158 case Instruction::LShr:
159 return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
160 getOperandValue(CE->getOperand(1), SF),
161 CE->getOperand(0)->getType());
162 case Instruction::AShr:
163 return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
164 getOperandValue(CE->getOperand(1), SF),
165 CE->getOperand(0)->getType());
166 case Instruction::Select:
167 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
168 getOperandValue(CE->getOperand(1), SF),
169 getOperandValue(CE->getOperand(2), SF));
171 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
173 return GenericValue();
177 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
178 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
179 return getConstantExprValue(CE, SF);
180 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
181 return getConstantValue(CPV);
182 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
183 return PTOGV(getPointerToGlobal(GV));
189 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
193 void Interpreter::initializeExecutionEngine() {
197 //===----------------------------------------------------------------------===//
198 // Binary Instruction Implementations
199 //===----------------------------------------------------------------------===//
201 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
202 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
204 #define IMPLEMENT_INTEGER_BINOP(OP, TY) \
205 case Type::IntegerTyID: { \
206 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
208 Dest.Int1Val = Src1.Int1Val OP Src2.Int1Val; \
209 else if (BitWidth <= 8) \
210 Dest.Int8Val = Src1.Int8Val OP Src2.Int8Val; \
211 else if (BitWidth <= 16) \
212 Dest.Int16Val = Src1.Int16Val OP Src2.Int16Val; \
213 else if (BitWidth <= 32) \
214 Dest.Int32Val = Src1.Int32Val OP Src2.Int32Val; \
215 else if (BitWidth <= 64) \
216 Dest.Int64Val = Src1.Int64Val OP Src2.Int64Val; \
218 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
219 maskToBitWidth(Dest, BitWidth); \
223 #define IMPLEMENT_SIGNED_BINOP(OP, TY) \
224 if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
225 unsigned BitWidth = ITy->getBitWidth(); \
227 Dest.Int8Val = ((int8_t)Src1.Int8Val) OP ((int8_t)Src2.Int8Val); \
228 else if (BitWidth <= 16) \
229 Dest.Int16Val = ((int16_t)Src1.Int16Val) OP ((int16_t)Src2.Int16Val); \
230 else if (BitWidth <= 32) \
231 Dest.Int32Val = ((int32_t)Src1.Int32Val) OP ((int32_t)Src2.Int32Val); \
232 else if (BitWidth <= 64) \
233 Dest.Int64Val = ((int64_t)Src1.Int64Val) OP ((int64_t)Src2.Int64Val); \
235 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
238 maskToBitWidth(Dest, BitWidth); \
240 cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
244 #define IMPLEMENT_UNSIGNED_BINOP(OP, TY) \
245 if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
246 unsigned BitWidth = ITy->getBitWidth(); \
248 Dest.Int8Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
249 else if (BitWidth <= 16) \
250 Dest.Int16Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
251 else if (BitWidth <= 32) \
252 Dest.Int32Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
253 else if (BitWidth <= 64) \
254 Dest.Int64Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
256 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
259 maskToBitWidth(Dest, BitWidth); \
261 cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
265 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
268 switch (Ty->getTypeID()) {
269 IMPLEMENT_INTEGER_BINOP(+, Ty);
270 IMPLEMENT_BINARY_OPERATOR(+, Float);
271 IMPLEMENT_BINARY_OPERATOR(+, Double);
273 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
279 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
282 switch (Ty->getTypeID()) {
283 IMPLEMENT_INTEGER_BINOP(-, Ty);
284 IMPLEMENT_BINARY_OPERATOR(-, Float);
285 IMPLEMENT_BINARY_OPERATOR(-, Double);
287 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
293 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
296 switch (Ty->getTypeID()) {
297 IMPLEMENT_INTEGER_BINOP(*, Ty);
298 IMPLEMENT_BINARY_OPERATOR(*, Float);
299 IMPLEMENT_BINARY_OPERATOR(*, Double);
301 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
307 static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
310 IMPLEMENT_UNSIGNED_BINOP(/,Ty)
314 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
317 IMPLEMENT_SIGNED_BINOP(/,Ty)
321 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
324 switch (Ty->getTypeID()) {
325 IMPLEMENT_BINARY_OPERATOR(/, Float);
326 IMPLEMENT_BINARY_OPERATOR(/, Double);
328 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
334 static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
337 IMPLEMENT_UNSIGNED_BINOP(%, Ty)
341 static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
344 IMPLEMENT_SIGNED_BINOP(%, Ty)
348 static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
351 switch (Ty->getTypeID()) {
352 case Type::FloatTyID:
353 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
355 case Type::DoubleTyID:
356 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
359 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
365 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
368 IMPLEMENT_UNSIGNED_BINOP(&, Ty)
372 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
375 IMPLEMENT_UNSIGNED_BINOP(|, Ty)
379 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
382 IMPLEMENT_UNSIGNED_BINOP(^, Ty)
386 #define IMPLEMENT_SIGNED_ICMP(OP, TY) \
387 case Type::IntegerTyID: { \
388 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
390 Dest.Int1Val = ((int8_t)Src1.Int1Val) OP ((int8_t)Src2.Int1Val); \
391 else if (BitWidth <= 8) \
392 Dest.Int1Val = ((int8_t)Src1.Int8Val) OP ((int8_t)Src2.Int8Val); \
393 else if (BitWidth <= 16) \
394 Dest.Int1Val = ((int16_t)Src1.Int16Val) OP ((int16_t)Src2.Int16Val); \
395 else if (BitWidth <= 32) \
396 Dest.Int1Val = ((int32_t)Src1.Int32Val) OP ((int32_t)Src2.Int32Val); \
397 else if (BitWidth <= 64) \
398 Dest.Int1Val = ((int64_t)Src1.Int64Val) OP ((int64_t)Src2.Int64Val); \
400 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
403 maskToBitWidth(Dest, BitWidth); \
407 #define IMPLEMENT_UNSIGNED_ICMP(OP, TY) \
408 case Type::IntegerTyID: { \
409 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
411 Dest.Int1Val = ((uint8_t)Src1.Int1Val) OP ((uint8_t)Src2.Int1Val); \
412 else if (BitWidth <= 8) \
413 Dest.Int1Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
414 else if (BitWidth <= 16) \
415 Dest.Int1Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
416 else if (BitWidth <= 32) \
417 Dest.Int1Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
418 else if (BitWidth <= 64) \
419 Dest.Int1Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
421 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
424 maskToBitWidth(Dest, BitWidth); \
428 // Handle pointers specially because they must be compared with only as much
429 // width as the host has. We _do not_ want to be comparing 64 bit values when
430 // running on a 32-bit target, otherwise the upper 32 bits might mess up
431 // comparisons if they contain garbage.
432 #define IMPLEMENT_POINTER_ICMP(OP) \
433 case Type::PointerTyID: \
434 Dest.Int1Val = (void*)(intptr_t)Src1.PointerVal OP \
435 (void*)(intptr_t)Src2.PointerVal; break
437 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
440 switch (Ty->getTypeID()) {
441 IMPLEMENT_UNSIGNED_ICMP(==, Ty);
442 IMPLEMENT_POINTER_ICMP(==);
444 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
450 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
453 switch (Ty->getTypeID()) {
454 IMPLEMENT_UNSIGNED_ICMP(!=, Ty);
455 IMPLEMENT_POINTER_ICMP(!=);
457 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
463 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
466 switch (Ty->getTypeID()) {
467 IMPLEMENT_UNSIGNED_ICMP(<, Ty);
468 IMPLEMENT_POINTER_ICMP(<);
470 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
476 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
479 switch (Ty->getTypeID()) {
480 IMPLEMENT_SIGNED_ICMP(<, Ty);
481 IMPLEMENT_POINTER_ICMP(<);
483 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
489 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
492 switch (Ty->getTypeID()) {
493 IMPLEMENT_UNSIGNED_ICMP(>, Ty);
494 IMPLEMENT_POINTER_ICMP(>);
496 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
502 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
505 switch (Ty->getTypeID()) {
506 IMPLEMENT_SIGNED_ICMP(>, Ty);
507 IMPLEMENT_POINTER_ICMP(>);
509 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
515 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
518 switch (Ty->getTypeID()) {
519 IMPLEMENT_UNSIGNED_ICMP(<=, Ty);
520 IMPLEMENT_POINTER_ICMP(<=);
522 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
528 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
531 switch (Ty->getTypeID()) {
532 IMPLEMENT_SIGNED_ICMP(<=, Ty);
533 IMPLEMENT_POINTER_ICMP(<=);
535 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
541 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
544 switch (Ty->getTypeID()) {
545 IMPLEMENT_UNSIGNED_ICMP(>=,Ty);
546 IMPLEMENT_POINTER_ICMP(>=);
548 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
554 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
557 switch (Ty->getTypeID()) {
558 IMPLEMENT_SIGNED_ICMP(>=, Ty);
559 IMPLEMENT_POINTER_ICMP(>=);
561 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
567 void Interpreter::visitICmpInst(ICmpInst &I) {
568 ExecutionContext &SF = ECStack.back();
569 const Type *Ty = I.getOperand(0)->getType();
570 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
571 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
572 GenericValue R; // Result
574 switch (I.getPredicate()) {
575 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
576 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
577 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
578 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
579 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
580 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
581 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
582 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
583 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
584 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
586 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
593 #define IMPLEMENT_FCMP(OP, TY) \
594 case Type::TY##TyID: Dest.Int1Val = Src1.TY##Val OP Src2.TY##Val; break
596 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
599 switch (Ty->getTypeID()) {
600 IMPLEMENT_FCMP(==, Float);
601 IMPLEMENT_FCMP(==, Double);
603 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
609 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
612 switch (Ty->getTypeID()) {
613 IMPLEMENT_FCMP(!=, Float);
614 IMPLEMENT_FCMP(!=, Double);
617 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
623 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
626 switch (Ty->getTypeID()) {
627 IMPLEMENT_FCMP(<=, Float);
628 IMPLEMENT_FCMP(<=, Double);
630 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
636 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
639 switch (Ty->getTypeID()) {
640 IMPLEMENT_FCMP(>=, Float);
641 IMPLEMENT_FCMP(>=, Double);
643 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
649 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
652 switch (Ty->getTypeID()) {
653 IMPLEMENT_FCMP(<, Float);
654 IMPLEMENT_FCMP(<, Double);
656 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
662 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
665 switch (Ty->getTypeID()) {
666 IMPLEMENT_FCMP(>, Float);
667 IMPLEMENT_FCMP(>, Double);
669 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
675 #define IMPLEMENT_UNORDERED(TY, X,Y) \
676 if (TY == Type::FloatTy) \
677 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
678 Dest.Int1Val = true; \
681 else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
682 Dest.Int1Val = true; \
687 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
690 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
691 return executeFCMP_OEQ(Src1, Src2, Ty);
694 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
697 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
698 return executeFCMP_ONE(Src1, Src2, Ty);
701 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
704 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
705 return executeFCMP_OLE(Src1, Src2, Ty);
708 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
711 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
712 return executeFCMP_OGE(Src1, Src2, Ty);
715 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
718 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
719 return executeFCMP_OLT(Src1, Src2, Ty);
722 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
725 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
726 return executeFCMP_OGT(Src1, Src2, Ty);
729 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
732 if (Ty == Type::FloatTy)
733 Dest.Int1Val = (Src1.FloatVal == Src1.FloatVal &&
734 Src2.FloatVal == Src2.FloatVal);
736 Dest.Int1Val = (Src1.DoubleVal == Src1.DoubleVal &&
737 Src2.DoubleVal == Src2.DoubleVal);
741 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
744 if (Ty == Type::FloatTy)
745 Dest.Int1Val = (Src1.FloatVal != Src1.FloatVal ||
746 Src2.FloatVal != Src2.FloatVal);
748 Dest.Int1Val = (Src1.DoubleVal != Src1.DoubleVal ||
749 Src2.DoubleVal != Src2.DoubleVal);
753 void Interpreter::visitFCmpInst(FCmpInst &I) {
754 ExecutionContext &SF = ECStack.back();
755 const Type *Ty = I.getOperand(0)->getType();
756 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
757 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
758 GenericValue R; // Result
760 switch (I.getPredicate()) {
761 case FCmpInst::FCMP_FALSE: R.Int1Val = false; break;
762 case FCmpInst::FCMP_TRUE: R.Int1Val = true; break;
763 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
764 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
765 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
766 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
767 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
768 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
769 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
770 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
771 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
772 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
773 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
774 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
775 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
776 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
778 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
785 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
786 GenericValue Src2, const Type *Ty) {
789 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
790 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
791 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
792 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
793 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
794 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
795 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
796 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
797 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
798 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
799 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
800 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
801 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
802 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
803 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
804 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
805 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
806 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
807 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
808 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
809 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
810 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
811 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
812 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
813 case FCmpInst::FCMP_FALSE: {
815 Result.Int1Val = false;
818 case FCmpInst::FCMP_TRUE: {
820 Result.Int1Val = true;
824 cerr << "Unhandled Cmp predicate\n";
829 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
830 ExecutionContext &SF = ECStack.back();
831 const Type *Ty = I.getOperand(0)->getType();
832 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
833 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
834 GenericValue R; // Result
836 switch (I.getOpcode()) {
837 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
838 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
839 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
840 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
841 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
842 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
843 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
844 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
845 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
846 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
847 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
848 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
850 cerr << "Don't know how to handle this binary operator!\n-->" << I;
857 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
859 return Src1.Int1Val ? Src2 : Src3;
862 void Interpreter::visitSelectInst(SelectInst &I) {
863 ExecutionContext &SF = ECStack.back();
864 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
865 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
866 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
867 GenericValue R = executeSelectInst(Src1, Src2, Src3);
872 //===----------------------------------------------------------------------===//
873 // Terminator Instruction Implementations
874 //===----------------------------------------------------------------------===//
876 void Interpreter::exitCalled(GenericValue GV) {
877 // runAtExitHandlers() assumes there are no stack frames, but
878 // if exit() was called, then it had a stack frame. Blow away
879 // the stack before interpreting atexit handlers.
881 runAtExitHandlers ();
885 /// Pop the last stack frame off of ECStack and then copy the result
886 /// back into the result variable if we are not returning void. The
887 /// result variable may be the ExitValue, or the Value of the calling
888 /// CallInst if there was a previous stack frame. This method may
889 /// invalidate any ECStack iterators you have. This method also takes
890 /// care of switching to the normal destination BB, if we are returning
893 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
894 GenericValue Result) {
895 // Pop the current stack frame.
898 if (ECStack.empty()) { // Finished main. Put result into exit code...
899 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
900 ExitValue = Result; // Capture the exit value of the program
902 memset(&ExitValue, 0, sizeof(ExitValue));
905 // If we have a previous stack frame, and we have a previous call,
906 // fill in the return value...
907 ExecutionContext &CallingSF = ECStack.back();
908 if (Instruction *I = CallingSF.Caller.getInstruction()) {
909 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
910 SetValue(I, Result, CallingSF);
911 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
912 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
913 CallingSF.Caller = CallSite(); // We returned from the call...
918 void Interpreter::visitReturnInst(ReturnInst &I) {
919 ExecutionContext &SF = ECStack.back();
920 const Type *RetTy = Type::VoidTy;
923 // Save away the return value... (if we are not 'ret void')
924 if (I.getNumOperands()) {
925 RetTy = I.getReturnValue()->getType();
926 Result = getOperandValue(I.getReturnValue(), SF);
929 popStackAndReturnValueToCaller(RetTy, Result);
932 void Interpreter::visitUnwindInst(UnwindInst &I) {
937 if (ECStack.empty ())
939 Inst = ECStack.back ().Caller.getInstruction ();
940 } while (!(Inst && isa<InvokeInst> (Inst)));
942 // Return from invoke
943 ExecutionContext &InvokingSF = ECStack.back ();
944 InvokingSF.Caller = CallSite ();
946 // Go to exceptional destination BB of invoke instruction
947 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
950 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
951 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
955 void Interpreter::visitBranchInst(BranchInst &I) {
956 ExecutionContext &SF = ECStack.back();
959 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
960 if (!I.isUnconditional()) {
961 Value *Cond = I.getCondition();
962 if (getOperandValue(Cond, SF).Int1Val == 0) // If false cond...
963 Dest = I.getSuccessor(1);
965 SwitchToNewBasicBlock(Dest, SF);
968 void Interpreter::visitSwitchInst(SwitchInst &I) {
969 ExecutionContext &SF = ECStack.back();
970 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
971 const Type *ElTy = I.getOperand(0)->getType();
973 // Check to see if any of the cases match...
974 BasicBlock *Dest = 0;
975 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
976 if (executeICMP_EQ(CondVal,
977 getOperandValue(I.getOperand(i), SF), ElTy).Int1Val) {
978 Dest = cast<BasicBlock>(I.getOperand(i+1));
982 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
983 SwitchToNewBasicBlock(Dest, SF);
986 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
987 // This function handles the actual updating of block and instruction iterators
988 // as well as execution of all of the PHI nodes in the destination block.
990 // This method does this because all of the PHI nodes must be executed
991 // atomically, reading their inputs before any of the results are updated. Not
992 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
993 // their inputs. If the input PHI node is updated before it is read, incorrect
994 // results can happen. Thus we use a two phase approach.
996 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
997 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
998 SF.CurBB = Dest; // Update CurBB to branch destination
999 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
1001 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
1003 // Loop over all of the PHI nodes in the current block, reading their inputs.
1004 std::vector<GenericValue> ResultValues;
1006 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
1007 // Search for the value corresponding to this previous bb...
1008 int i = PN->getBasicBlockIndex(PrevBB);
1009 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
1010 Value *IncomingValue = PN->getIncomingValue(i);
1012 // Save the incoming value for this PHI node...
1013 ResultValues.push_back(getOperandValue(IncomingValue, SF));
1016 // Now loop over all of the PHI nodes setting their values...
1017 SF.CurInst = SF.CurBB->begin();
1018 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
1019 PHINode *PN = cast<PHINode>(SF.CurInst);
1020 SetValue(PN, ResultValues[i], SF);
1024 //===----------------------------------------------------------------------===//
1025 // Memory Instruction Implementations
1026 //===----------------------------------------------------------------------===//
1028 void Interpreter::visitAllocationInst(AllocationInst &I) {
1029 ExecutionContext &SF = ECStack.back();
1031 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
1033 // Get the number of elements being allocated by the array...
1034 unsigned NumElements = getOperandValue(I.getOperand(0), SF).Int32Val;
1036 // Allocate enough memory to hold the type...
1037 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
1039 GenericValue Result = PTOGV(Memory);
1040 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
1041 SetValue(&I, Result, SF);
1043 if (I.getOpcode() == Instruction::Alloca)
1044 ECStack.back().Allocas.add(Memory);
1047 void Interpreter::visitFreeInst(FreeInst &I) {
1048 ExecutionContext &SF = ECStack.back();
1049 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
1050 GenericValue Value = getOperandValue(I.getOperand(0), SF);
1051 // TODO: Check to make sure memory is allocated
1052 free(GVTOP(Value)); // Free memory
1055 // getElementOffset - The workhorse for getelementptr.
1057 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
1058 gep_type_iterator E,
1059 ExecutionContext &SF) {
1060 assert(isa<PointerType>(Ptr->getType()) &&
1061 "Cannot getElementOffset of a nonpointer type!");
1063 PointerTy Total = 0;
1065 for (; I != E; ++I) {
1066 if (const StructType *STy = dyn_cast<StructType>(*I)) {
1067 const StructLayout *SLO = TD.getStructLayout(STy);
1069 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
1070 unsigned Index = unsigned(CPU->getZExtValue());
1072 Total += (PointerTy)SLO->MemberOffsets[Index];
1074 const SequentialType *ST = cast<SequentialType>(*I);
1075 // Get the index number for the array... which must be long type...
1076 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
1080 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
1082 Idx = (int64_t)(int32_t)IdxGV.Int32Val;
1083 else if (BitWidth == 64)
1084 Idx = (int64_t)IdxGV.Int64Val;
1086 assert(0 && "Invalid index type for getelementptr");
1087 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
1091 GenericValue Result;
1092 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
1096 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1097 ExecutionContext &SF = ECStack.back();
1098 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
1099 gep_type_begin(I), gep_type_end(I), SF), SF);
1102 void Interpreter::visitLoadInst(LoadInst &I) {
1103 ExecutionContext &SF = ECStack.back();
1104 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
1105 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
1106 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
1107 SetValue(&I, Result, SF);
1110 void Interpreter::visitStoreInst(StoreInst &I) {
1111 ExecutionContext &SF = ECStack.back();
1112 GenericValue Val = getOperandValue(I.getOperand(0), SF);
1113 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
1114 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
1115 I.getOperand(0)->getType());
1118 //===----------------------------------------------------------------------===//
1119 // Miscellaneous Instruction Implementations
1120 //===----------------------------------------------------------------------===//
1122 void Interpreter::visitCallSite(CallSite CS) {
1123 ExecutionContext &SF = ECStack.back();
1125 // Check to see if this is an intrinsic function call...
1126 if (Function *F = CS.getCalledFunction())
1127 if (F->isExternal ())
1128 switch (F->getIntrinsicID()) {
1129 case Intrinsic::not_intrinsic:
1131 case Intrinsic::vastart: { // va_start
1132 GenericValue ArgIndex;
1133 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1134 ArgIndex.UIntPairVal.second = 0;
1135 SetValue(CS.getInstruction(), ArgIndex, SF);
1138 case Intrinsic::vaend: // va_end is a noop for the interpreter
1140 case Intrinsic::vacopy: // va_copy: dest = src
1141 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1144 // If it is an unknown intrinsic function, use the intrinsic lowering
1145 // class to transform it into hopefully tasty LLVM code.
1147 Instruction *Prev = CS.getInstruction()->getPrev();
1148 BasicBlock *Parent = CS.getInstruction()->getParent();
1149 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1151 // Restore the CurInst pointer to the first instruction newly inserted, if
1154 SF.CurInst = Parent->begin();
1163 std::vector<GenericValue> ArgVals;
1164 const unsigned NumArgs = SF.Caller.arg_size();
1165 ArgVals.reserve(NumArgs);
1166 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1167 e = SF.Caller.arg_end(); i != e; ++i) {
1169 ArgVals.push_back(getOperandValue(V, SF));
1170 // Promote all integral types whose size is < sizeof(int) into ints. We do
1171 // this by zero or sign extending the value as appropriate according to the
1173 const Type *Ty = V->getType();
1174 if (Ty->isInteger()) {
1175 if (Ty->getPrimitiveSizeInBits() == 1)
1176 ArgVals.back().Int32Val = ArgVals.back().Int1Val;
1177 else if (Ty->getPrimitiveSizeInBits() <= 8)
1178 ArgVals.back().Int32Val = ArgVals.back().Int8Val;
1179 else if (Ty->getPrimitiveSizeInBits() <= 16)
1180 ArgVals.back().Int32Val = ArgVals.back().Int16Val;
1184 // To handle indirect calls, we must get the pointer value from the argument
1185 // and treat it as a function pointer.
1186 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
1187 callFunction((Function*)GVTOP(SRC), ArgVals);
1190 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
1193 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1194 unsigned BitWidth = ITy->getBitWidth();
1196 Dest.Int8Val = ((uint8_t)Src1.Int8Val) << ((uint32_t)Src2.Int8Val);
1197 else if (BitWidth <= 16)
1198 Dest.Int16Val = ((uint16_t)Src1.Int16Val) << ((uint32_t)Src2.Int8Val);
1199 else if (BitWidth <= 32)
1200 Dest.Int32Val = ((uint32_t)Src1.Int32Val) << ((uint32_t)Src2.Int8Val);
1201 else if (BitWidth <= 64)
1202 Dest.Int64Val = ((uint64_t)Src1.Int64Val) << ((uint32_t)Src2.Int8Val);
1204 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1207 maskToBitWidth(Dest, BitWidth);
1209 cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
1215 static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
1218 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1219 unsigned BitWidth = ITy->getBitWidth();
1221 Dest.Int8Val = ((uint8_t)Src1.Int8Val) >> ((uint32_t)Src2.Int8Val);
1222 else if (BitWidth <= 16)
1223 Dest.Int16Val = ((uint16_t)Src1.Int16Val) >> ((uint32_t)Src2.Int8Val);
1224 else if (BitWidth <= 32)
1225 Dest.Int32Val = ((uint32_t)Src1.Int32Val) >> ((uint32_t)Src2.Int8Val);
1226 else if (BitWidth <= 64)
1227 Dest.Int64Val = ((uint64_t)Src1.Int64Val) >> ((uint32_t)Src2.Int8Val);
1229 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1232 maskToBitWidth(Dest, BitWidth);
1234 cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
1240 static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
1243 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1244 unsigned BitWidth = ITy->getBitWidth();
1246 Dest.Int8Val = ((int8_t)Src1.Int8Val) >> ((int32_t)Src2.Int8Val);
1247 else if (BitWidth <= 16)
1248 Dest.Int16Val = ((int16_t)Src1.Int16Val) >> ((int32_t)Src2.Int8Val);
1249 else if (BitWidth <= 32)
1250 Dest.Int32Val = ((int32_t)Src1.Int32Val) >> ((int32_t)Src2.Int8Val);
1251 else if (BitWidth <= 64)
1252 Dest.Int64Val = ((int64_t)Src1.Int64Val) >> ((int32_t)Src2.Int8Val);
1254 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
1257 maskToBitWidth(Dest, BitWidth);
1259 cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
1265 void Interpreter::visitShl(ShiftInst &I) {
1266 ExecutionContext &SF = ECStack.back();
1267 const Type *Ty = I.getOperand(0)->getType();
1268 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1269 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1271 Dest = executeShlInst (Src1, Src2, Ty);
1272 SetValue(&I, Dest, SF);
1275 void Interpreter::visitLShr(ShiftInst &I) {
1276 ExecutionContext &SF = ECStack.back();
1277 const Type *Ty = I.getOperand(0)->getType();
1278 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1279 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1281 Dest = executeLShrInst (Src1, Src2, Ty);
1282 SetValue(&I, Dest, SF);
1285 void Interpreter::visitAShr(ShiftInst &I) {
1286 ExecutionContext &SF = ECStack.back();
1287 const Type *Ty = I.getOperand(0)->getType();
1288 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1289 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1291 Dest = executeAShrInst (Src1, Src2, Ty);
1292 SetValue(&I, Dest, SF);
1295 #define INTEGER_ASSIGN(DEST, BITWIDTH, VAL) \
1297 uint64_t Mask = (1ull << BITWIDTH) - 1; \
1298 if (BITWIDTH == 1) { \
1299 Dest.Int1Val = (bool) (VAL & Mask); \
1300 } else if (BITWIDTH <= 8) { \
1301 Dest.Int8Val = (uint8_t) (VAL & Mask); \
1302 } else if (BITWIDTH <= 16) { \
1303 Dest.Int16Val = (uint16_t) (VAL & Mask); \
1304 } else if (BITWIDTH <= 32) { \
1305 Dest.Int32Val = (uint32_t) (VAL & Mask); \
1307 Dest.Int64Val = (uint64_t) (VAL & Mask); \
1310 GenericValue Interpreter::executeTruncInst(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 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1316 unsigned DBitWidth = DITy->getBitWidth();
1317 unsigned SBitWidth = SITy->getBitWidth();
1318 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1319 "Integer types > 64 bits not supported");
1320 assert(SBitWidth > DBitWidth && "Invalid truncate");
1322 // Mask the source value to its actual bit width. This ensures that any
1323 // high order bits are cleared.
1324 uint64_t Mask = (1ULL << DBitWidth) - 1;
1325 uint64_t MaskedVal = 0;
1327 MaskedVal = Src.Int8Val & Mask;
1328 else if (SBitWidth <= 16)
1329 MaskedVal = Src.Int16Val & Mask;
1330 else if (SBitWidth <= 32)
1331 MaskedVal = Src.Int32Val & Mask;
1333 MaskedVal = Src.Int64Val & Mask;
1335 INTEGER_ASSIGN(Dest, DBitWidth, MaskedVal);
1339 GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
1340 ExecutionContext &SF) {
1341 const Type *SrcTy = SrcVal->getType();
1342 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1343 const IntegerType *DITy = cast<IntegerType>(DstTy);
1344 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1345 unsigned DBitWidth = DITy->getBitWidth();
1346 unsigned SBitWidth = SITy->getBitWidth();
1347 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1348 "Integer types > 64 bits not supported");
1349 assert(SBitWidth < DBitWidth && "Invalid sign extend");
1350 int64_t Extended = 0;
1352 // For sign extension from bool, we must extend the source bits.
1353 Extended = 0 - (Src.Int1Val & 1);
1354 else if (SBitWidth <= 8)
1355 Extended = (int64_t) (int8_t)Src.Int8Val;
1356 else if (SBitWidth <= 16)
1357 Extended = (int64_t) (int16_t)Src.Int16Val;
1358 else if (SBitWidth <= 32)
1359 Extended = (int64_t) (int32_t)Src.Int32Val;
1361 Extended = (int64_t) Src.Int64Val;
1363 // Now that we have a sign extended value, assign it to the destination
1364 INTEGER_ASSIGN(Dest, DBitWidth, Extended);
1368 GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
1369 ExecutionContext &SF) {
1370 const Type *SrcTy = SrcVal->getType();
1371 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1372 const IntegerType *DITy = cast<IntegerType>(DstTy);
1373 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1374 unsigned DBitWidth = DITy->getBitWidth();
1375 unsigned SBitWidth = SITy->getBitWidth();
1376 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1377 "Integer types > 64 bits not supported");
1378 assert(SBitWidth < DBitWidth && "Invalid sign extend");
1379 uint64_t Extended = 0;
1381 // For sign extension from bool, we must extend the source bits.
1382 Extended = (uint64_t) (Src.Int1Val & 1);
1383 else if (SBitWidth <= 8)
1384 Extended = (uint64_t) (uint8_t)Src.Int8Val;
1385 else if (SBitWidth <= 16)
1386 Extended = (uint64_t) (uint16_t)Src.Int16Val;
1387 else if (SBitWidth <= 32)
1388 Extended = (uint64_t) (uint32_t)Src.Int32Val;
1390 Extended = (uint64_t) Src.Int64Val;
1392 // Now that we have a sign extended value, assign it to the destination
1393 INTEGER_ASSIGN(Dest, DBitWidth, Extended);
1397 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
1398 ExecutionContext &SF) {
1399 const Type *SrcTy = SrcVal->getType();
1400 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1401 assert(SrcTy == Type::DoubleTy && DstTy == Type::FloatTy &&
1402 "Invalid FPTrunc instruction");
1403 Dest.FloatVal = (float) Src.DoubleVal;
1407 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
1408 ExecutionContext &SF) {
1409 const Type *SrcTy = SrcVal->getType();
1410 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1411 assert(SrcTy == Type::FloatTy && DstTy == Type::DoubleTy &&
1412 "Invalid FPTrunc instruction");
1413 Dest.DoubleVal = (double) Src.FloatVal;
1417 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
1418 ExecutionContext &SF) {
1419 const Type *SrcTy = SrcVal->getType();
1420 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1421 const IntegerType *DITy = cast<IntegerType>(DstTy);
1422 unsigned DBitWidth = DITy->getBitWidth();
1423 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1424 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
1425 uint64_t Converted = 0;
1426 if (SrcTy->getTypeID() == Type::FloatTyID)
1427 Converted = (uint64_t) Src.FloatVal;
1429 Converted = (uint64_t) Src.DoubleVal;
1431 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1435 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1436 ExecutionContext &SF) {
1437 const Type *SrcTy = SrcVal->getType();
1438 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1439 const IntegerType *DITy = cast<IntegerType>(DstTy);
1440 unsigned DBitWidth = DITy->getBitWidth();
1441 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1442 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1443 int64_t Converted = 0;
1444 if (SrcTy->getTypeID() == Type::FloatTyID)
1445 Converted = (int64_t) Src.FloatVal;
1447 Converted = (int64_t) Src.DoubleVal;
1449 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1453 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1454 ExecutionContext &SF) {
1455 const Type *SrcTy = SrcVal->getType();
1456 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1457 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1458 unsigned SBitWidth = SITy->getBitWidth();
1459 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1460 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1461 uint64_t Converted = 0;
1463 Converted = (uint64_t) Src.Int1Val;
1464 else if (SBitWidth <= 8)
1465 Converted = (uint64_t) Src.Int8Val;
1466 else if (SBitWidth <= 16)
1467 Converted = (uint64_t) Src.Int16Val;
1468 else if (SBitWidth <= 32)
1469 Converted = (uint64_t) Src.Int32Val;
1471 Converted = (uint64_t) Src.Int64Val;
1473 if (DstTy->getTypeID() == Type::FloatTyID)
1474 Dest.FloatVal = (float) Converted;
1476 Dest.DoubleVal = (double) Converted;
1480 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1481 ExecutionContext &SF) {
1482 const Type *SrcTy = SrcVal->getType();
1483 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1484 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1485 unsigned SBitWidth = SITy->getBitWidth();
1486 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1487 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1488 int64_t Converted = 0;
1490 Converted = 0LL - Src.Int1Val;
1491 else if (SBitWidth <= 8)
1492 Converted = (int64_t) (int8_t)Src.Int8Val;
1493 else if (SBitWidth <= 16)
1494 Converted = (int64_t) (int16_t)Src.Int16Val;
1495 else if (SBitWidth <= 32)
1496 Converted = (int64_t) (int32_t)Src.Int32Val;
1498 Converted = (int64_t) Src.Int64Val;
1500 if (DstTy->getTypeID() == Type::FloatTyID)
1501 Dest.FloatVal = (float) Converted;
1503 Dest.DoubleVal = (double) Converted;
1507 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1508 ExecutionContext &SF) {
1509 const Type *SrcTy = SrcVal->getType();
1510 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1511 const IntegerType *DITy = cast<IntegerType>(DstTy);
1512 unsigned DBitWidth = DITy->getBitWidth();
1513 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1514 assert(isa<PointerType>(SrcTy) && "Invalid PtrToInt instruction");
1515 INTEGER_ASSIGN(Dest, DBitWidth, (intptr_t) Src.PointerVal);
1519 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1520 ExecutionContext &SF) {
1521 const Type *SrcTy = SrcVal->getType();
1522 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1523 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1524 unsigned SBitWidth = SITy->getBitWidth();
1525 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1526 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1527 uint64_t Converted = 0;
1529 Converted = (uint64_t) Src.Int1Val;
1530 else if (SBitWidth <= 8)
1531 Converted = (uint64_t) Src.Int8Val;
1532 else if (SBitWidth <= 16)
1533 Converted = (uint64_t) Src.Int16Val;
1534 else if (SBitWidth <= 32)
1535 Converted = (uint64_t) Src.Int32Val;
1537 Converted = (uint64_t) Src.Int64Val;
1539 Dest.PointerVal = (PointerTy) Converted;
1543 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1544 ExecutionContext &SF) {
1546 const Type *SrcTy = SrcVal->getType();
1547 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1548 if (isa<PointerType>(DstTy)) {
1549 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1550 Dest.PointerVal = Src.PointerVal;
1551 } else if (DstTy->isInteger()) {
1552 const IntegerType *DITy = cast<IntegerType>(DstTy);
1553 unsigned DBitWidth = DITy->getBitWidth();
1554 if (SrcTy == Type::FloatTy) {
1555 Dest.Int32Val = FloatToBits(Src.FloatVal);
1556 } else if (SrcTy == Type::DoubleTy) {
1557 Dest.Int64Val = DoubleToBits(Src.DoubleVal);
1558 } else if (SrcTy->isInteger()) {
1559 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1560 unsigned SBitWidth = SITy->getBitWidth();
1561 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1562 assert(SBitWidth == DBitWidth && "Invalid BitCast");
1564 Dest.Int1Val = Src.Int1Val;
1565 else if (SBitWidth <= 8)
1566 Dest.Int8Val = Src.Int8Val;
1567 else if (SBitWidth <= 16)
1568 Dest.Int16Val = Src.Int16Val;
1569 else if (SBitWidth <= 32)
1570 Dest.Int32Val = Src.Int32Val;
1572 Dest.Int64Val = Src.Int64Val;
1573 maskToBitWidth(Dest, DBitWidth);
1575 assert(0 && "Invalid BitCast");
1576 } else if (DstTy == Type::FloatTy) {
1577 if (SrcTy->isInteger())
1578 Dest.FloatVal = BitsToFloat(Src.Int32Val);
1580 Dest.FloatVal = Src.FloatVal;
1581 } else if (DstTy == Type::DoubleTy) {
1582 if (SrcTy->isInteger())
1583 Dest.DoubleVal = BitsToDouble(Src.Int64Val);
1585 Dest.DoubleVal = Src.DoubleVal;
1587 assert(0 && "Invalid Bitcast");
1592 void Interpreter::visitTruncInst(TruncInst &I) {
1593 ExecutionContext &SF = ECStack.back();
1594 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1597 void Interpreter::visitSExtInst(SExtInst &I) {
1598 ExecutionContext &SF = ECStack.back();
1599 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1602 void Interpreter::visitZExtInst(ZExtInst &I) {
1603 ExecutionContext &SF = ECStack.back();
1604 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1607 void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1608 ExecutionContext &SF = ECStack.back();
1609 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1612 void Interpreter::visitFPExtInst(FPExtInst &I) {
1613 ExecutionContext &SF = ECStack.back();
1614 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1617 void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1618 ExecutionContext &SF = ECStack.back();
1619 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1622 void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1623 ExecutionContext &SF = ECStack.back();
1624 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1627 void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1628 ExecutionContext &SF = ECStack.back();
1629 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1632 void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1633 ExecutionContext &SF = ECStack.back();
1634 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1637 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1638 ExecutionContext &SF = ECStack.back();
1639 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1642 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1643 ExecutionContext &SF = ECStack.back();
1644 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1647 void Interpreter::visitBitCastInst(BitCastInst &I) {
1648 ExecutionContext &SF = ECStack.back();
1649 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1652 #define IMPLEMENT_VAARG(TY) \
1653 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1655 void Interpreter::visitVAArgInst(VAArgInst &I) {
1656 ExecutionContext &SF = ECStack.back();
1658 // Get the incoming valist parameter. LLI treats the valist as a
1659 // (ec-stack-depth var-arg-index) pair.
1660 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1662 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1663 .VarArgs[VAList.UIntPairVal.second];
1664 const Type *Ty = I.getType();
1665 switch (Ty->getTypeID()) {
1666 case Type::IntegerTyID: {
1667 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
1669 Dest.Int1Val = Src.Int1Val;
1670 else if (BitWidth <= 8)
1671 Dest.Int8Val = Src.Int8Val;
1672 else if (BitWidth <= 16)
1673 Dest.Int16Val = Src.Int16Val;
1674 else if (BitWidth <= 32)
1675 Dest.Int32Val = Src.Int32Val;
1676 else if (BitWidth <= 64)
1677 Dest.Int64Val = Src.Int64Val;
1679 assert("Integer types > 64 bits not supported");
1680 maskToBitWidth(Dest, BitWidth);
1682 IMPLEMENT_VAARG(Pointer);
1683 IMPLEMENT_VAARG(Float);
1684 IMPLEMENT_VAARG(Double);
1686 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1690 // Set the Value of this Instruction.
1691 SetValue(&I, Dest, SF);
1693 // Move the pointer to the next vararg.
1694 ++VAList.UIntPairVal.second;
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->isExternal()) {
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...