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"; \
222 #define IMPLEMENT_SIGNED_BINOP(OP, TY) \
223 if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
224 unsigned BitWidth = ITy->getBitWidth(); \
226 Dest.Int8Val = ((int8_t)Src1.Int8Val) OP ((int8_t)Src2.Int8Val); \
227 else if (BitWidth <= 16) \
228 Dest.Int16Val = ((int16_t)Src1.Int16Val) OP ((int16_t)Src2.Int16Val); \
229 else if (BitWidth <= 32) \
230 Dest.Int32Val = ((int32_t)Src1.Int32Val) OP ((int32_t)Src2.Int32Val); \
231 else if (BitWidth <= 64) \
232 Dest.Int64Val = ((int64_t)Src1.Int64Val) OP ((int64_t)Src2.Int64Val); \
234 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
238 cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
242 #define IMPLEMENT_UNSIGNED_BINOP(OP, TY) \
243 if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
244 unsigned BitWidth = ITy->getBitWidth(); \
246 Dest.Int8Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
247 else if (BitWidth <= 16) \
248 Dest.Int16Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
249 else if (BitWidth <= 32) \
250 Dest.Int32Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
251 else if (BitWidth <= 64) \
252 Dest.Int64Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
254 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
258 cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
262 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
265 switch (Ty->getTypeID()) {
266 IMPLEMENT_INTEGER_BINOP(+, Ty);
267 IMPLEMENT_BINARY_OPERATOR(+, Float);
268 IMPLEMENT_BINARY_OPERATOR(+, Double);
270 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
276 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
279 switch (Ty->getTypeID()) {
280 IMPLEMENT_INTEGER_BINOP(-, Ty);
281 IMPLEMENT_BINARY_OPERATOR(-, Float);
282 IMPLEMENT_BINARY_OPERATOR(-, Double);
284 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
290 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
293 switch (Ty->getTypeID()) {
294 IMPLEMENT_INTEGER_BINOP(*, Ty);
295 IMPLEMENT_BINARY_OPERATOR(*, Float);
296 IMPLEMENT_BINARY_OPERATOR(*, Double);
298 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
304 static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
307 IMPLEMENT_UNSIGNED_BINOP(/,Ty)
311 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
314 IMPLEMENT_SIGNED_BINOP(/,Ty)
318 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
321 switch (Ty->getTypeID()) {
322 IMPLEMENT_BINARY_OPERATOR(/, Float);
323 IMPLEMENT_BINARY_OPERATOR(/, Double);
325 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
331 static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
334 IMPLEMENT_UNSIGNED_BINOP(%, Ty)
338 static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
341 IMPLEMENT_SIGNED_BINOP(%, Ty)
345 static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
348 switch (Ty->getTypeID()) {
349 case Type::FloatTyID:
350 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
352 case Type::DoubleTyID:
353 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
356 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
362 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
365 IMPLEMENT_UNSIGNED_BINOP(&, Ty)
369 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
372 IMPLEMENT_UNSIGNED_BINOP(|, Ty)
376 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
379 IMPLEMENT_UNSIGNED_BINOP(^, Ty)
383 #define IMPLEMENT_SIGNED_ICMP(OP, TY) \
384 case Type::IntegerTyID: { \
385 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
387 Dest.Int1Val = ((int8_t)Src1.Int1Val) OP ((int8_t)Src2.Int1Val); \
388 else if (BitWidth <= 8) \
389 Dest.Int1Val = ((int8_t)Src1.Int8Val) OP ((int8_t)Src2.Int8Val); \
390 else if (BitWidth <= 16) \
391 Dest.Int1Val = ((int16_t)Src1.Int16Val) OP ((int16_t)Src2.Int16Val); \
392 else if (BitWidth <= 32) \
393 Dest.Int1Val = ((int32_t)Src1.Int32Val) OP ((int32_t)Src2.Int32Val); \
394 else if (BitWidth <= 64) \
395 Dest.Int1Val = ((int64_t)Src1.Int64Val) OP ((int64_t)Src2.Int64Val); \
397 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
403 #define IMPLEMENT_UNSIGNED_ICMP(OP, TY) \
404 case Type::IntegerTyID: { \
405 unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
407 Dest.Int1Val = ((uint8_t)Src1.Int1Val) OP ((uint8_t)Src2.Int1Val); \
408 else if (BitWidth <= 8) \
409 Dest.Int1Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \
410 else if (BitWidth <= 16) \
411 Dest.Int1Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
412 else if (BitWidth <= 32) \
413 Dest.Int1Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
414 else if (BitWidth <= 64) \
415 Dest.Int1Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
417 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
423 // Handle pointers specially because they must be compared with only as much
424 // width as the host has. We _do not_ want to be comparing 64 bit values when
425 // running on a 32-bit target, otherwise the upper 32 bits might mess up
426 // comparisons if they contain garbage.
427 #define IMPLEMENT_POINTER_ICMP(OP) \
428 case Type::PointerTyID: \
429 Dest.Int1Val = (void*)(intptr_t)Src1.PointerVal OP \
430 (void*)(intptr_t)Src2.PointerVal; break
432 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
435 switch (Ty->getTypeID()) {
436 IMPLEMENT_UNSIGNED_ICMP(==, Ty);
437 IMPLEMENT_POINTER_ICMP(==);
439 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
445 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
448 switch (Ty->getTypeID()) {
449 IMPLEMENT_UNSIGNED_ICMP(!=, Ty);
450 IMPLEMENT_POINTER_ICMP(!=);
452 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
458 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
461 switch (Ty->getTypeID()) {
462 IMPLEMENT_UNSIGNED_ICMP(<, Ty);
463 IMPLEMENT_POINTER_ICMP(<);
465 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
471 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
474 switch (Ty->getTypeID()) {
475 IMPLEMENT_SIGNED_ICMP(<, Ty);
476 IMPLEMENT_POINTER_ICMP(<);
478 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
484 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
487 switch (Ty->getTypeID()) {
488 IMPLEMENT_UNSIGNED_ICMP(>, Ty);
489 IMPLEMENT_POINTER_ICMP(>);
491 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
497 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
500 switch (Ty->getTypeID()) {
501 IMPLEMENT_SIGNED_ICMP(>, Ty);
502 IMPLEMENT_POINTER_ICMP(>);
504 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
510 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
513 switch (Ty->getTypeID()) {
514 IMPLEMENT_UNSIGNED_ICMP(<=, Ty);
515 IMPLEMENT_POINTER_ICMP(<=);
517 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
523 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
526 switch (Ty->getTypeID()) {
527 IMPLEMENT_SIGNED_ICMP(<=, Ty);
528 IMPLEMENT_POINTER_ICMP(<=);
530 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
536 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
539 switch (Ty->getTypeID()) {
540 IMPLEMENT_UNSIGNED_ICMP(>=,Ty);
541 IMPLEMENT_POINTER_ICMP(>=);
543 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
549 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
552 switch (Ty->getTypeID()) {
553 IMPLEMENT_SIGNED_ICMP(>=, Ty);
554 IMPLEMENT_POINTER_ICMP(>=);
556 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
562 void Interpreter::visitICmpInst(ICmpInst &I) {
563 ExecutionContext &SF = ECStack.back();
564 const Type *Ty = I.getOperand(0)->getType();
565 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
566 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
567 GenericValue R; // Result
569 switch (I.getPredicate()) {
570 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
571 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
572 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
573 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
574 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
575 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
576 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
577 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
578 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
579 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
581 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
588 #define IMPLEMENT_FCMP(OP, TY) \
589 case Type::TY##TyID: Dest.Int1Val = Src1.TY##Val OP Src2.TY##Val; break
591 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
594 switch (Ty->getTypeID()) {
595 IMPLEMENT_FCMP(==, Float);
596 IMPLEMENT_FCMP(==, Double);
598 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
604 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
607 switch (Ty->getTypeID()) {
608 IMPLEMENT_FCMP(!=, Float);
609 IMPLEMENT_FCMP(!=, Double);
612 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
618 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
621 switch (Ty->getTypeID()) {
622 IMPLEMENT_FCMP(<=, Float);
623 IMPLEMENT_FCMP(<=, Double);
625 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
631 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
634 switch (Ty->getTypeID()) {
635 IMPLEMENT_FCMP(>=, Float);
636 IMPLEMENT_FCMP(>=, Double);
638 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
644 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
647 switch (Ty->getTypeID()) {
648 IMPLEMENT_FCMP(<, Float);
649 IMPLEMENT_FCMP(<, Double);
651 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
657 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
660 switch (Ty->getTypeID()) {
661 IMPLEMENT_FCMP(>, Float);
662 IMPLEMENT_FCMP(>, Double);
664 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
670 #define IMPLEMENT_UNORDERED(TY, X,Y) \
671 if (TY == Type::FloatTy) \
672 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
673 Dest.Int1Val = true; \
676 else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
677 Dest.Int1Val = true; \
682 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
685 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
686 return executeFCMP_OEQ(Src1, Src2, Ty);
689 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
692 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
693 return executeFCMP_ONE(Src1, Src2, Ty);
696 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
699 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
700 return executeFCMP_OLE(Src1, Src2, Ty);
703 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
706 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
707 return executeFCMP_OGE(Src1, Src2, Ty);
710 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
713 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
714 return executeFCMP_OLT(Src1, Src2, Ty);
717 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
720 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
721 return executeFCMP_OGT(Src1, Src2, Ty);
724 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
727 if (Ty == Type::FloatTy)
728 Dest.Int1Val = (Src1.FloatVal == Src1.FloatVal &&
729 Src2.FloatVal == Src2.FloatVal);
731 Dest.Int1Val = (Src1.DoubleVal == Src1.DoubleVal &&
732 Src2.DoubleVal == Src2.DoubleVal);
736 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
739 if (Ty == Type::FloatTy)
740 Dest.Int1Val = (Src1.FloatVal != Src1.FloatVal ||
741 Src2.FloatVal != Src2.FloatVal);
743 Dest.Int1Val = (Src1.DoubleVal != Src1.DoubleVal ||
744 Src2.DoubleVal != Src2.DoubleVal);
748 void Interpreter::visitFCmpInst(FCmpInst &I) {
749 ExecutionContext &SF = ECStack.back();
750 const Type *Ty = I.getOperand(0)->getType();
751 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
752 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
753 GenericValue R; // Result
755 switch (I.getPredicate()) {
756 case FCmpInst::FCMP_FALSE: R.Int1Val = false; break;
757 case FCmpInst::FCMP_TRUE: R.Int1Val = true; break;
758 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
759 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
760 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
761 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
762 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
763 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
764 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
765 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
766 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
767 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
768 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
769 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
770 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
771 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
773 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
780 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
781 GenericValue Src2, const Type *Ty) {
784 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
785 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
786 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
787 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
788 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
789 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
790 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
791 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
792 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
793 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
794 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
795 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
796 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
797 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
798 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
799 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
800 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
801 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
802 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
803 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
804 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
805 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
806 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
807 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
808 case FCmpInst::FCMP_FALSE: {
810 Result.Int1Val = false;
813 case FCmpInst::FCMP_TRUE: {
815 Result.Int1Val = true;
819 cerr << "Unhandled Cmp predicate\n";
824 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
825 ExecutionContext &SF = ECStack.back();
826 const Type *Ty = I.getOperand(0)->getType();
827 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
828 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
829 GenericValue R; // Result
831 switch (I.getOpcode()) {
832 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
833 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
834 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
835 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
836 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
837 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
838 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
839 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
840 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
841 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
842 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
843 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
845 cerr << "Don't know how to handle this binary operator!\n-->" << I;
852 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
854 return Src1.Int1Val ? Src2 : Src3;
857 void Interpreter::visitSelectInst(SelectInst &I) {
858 ExecutionContext &SF = ECStack.back();
859 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
860 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
861 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
862 GenericValue R = executeSelectInst(Src1, Src2, Src3);
867 //===----------------------------------------------------------------------===//
868 // Terminator Instruction Implementations
869 //===----------------------------------------------------------------------===//
871 void Interpreter::exitCalled(GenericValue GV) {
872 // runAtExitHandlers() assumes there are no stack frames, but
873 // if exit() was called, then it had a stack frame. Blow away
874 // the stack before interpreting atexit handlers.
876 runAtExitHandlers ();
880 /// Pop the last stack frame off of ECStack and then copy the result
881 /// back into the result variable if we are not returning void. The
882 /// result variable may be the ExitValue, or the Value of the calling
883 /// CallInst if there was a previous stack frame. This method may
884 /// invalidate any ECStack iterators you have. This method also takes
885 /// care of switching to the normal destination BB, if we are returning
888 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
889 GenericValue Result) {
890 // Pop the current stack frame.
893 if (ECStack.empty()) { // Finished main. Put result into exit code...
894 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
895 ExitValue = Result; // Capture the exit value of the program
897 memset(&ExitValue, 0, sizeof(ExitValue));
900 // If we have a previous stack frame, and we have a previous call,
901 // fill in the return value...
902 ExecutionContext &CallingSF = ECStack.back();
903 if (Instruction *I = CallingSF.Caller.getInstruction()) {
904 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
905 SetValue(I, Result, CallingSF);
906 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
907 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
908 CallingSF.Caller = CallSite(); // We returned from the call...
913 void Interpreter::visitReturnInst(ReturnInst &I) {
914 ExecutionContext &SF = ECStack.back();
915 const Type *RetTy = Type::VoidTy;
918 // Save away the return value... (if we are not 'ret void')
919 if (I.getNumOperands()) {
920 RetTy = I.getReturnValue()->getType();
921 Result = getOperandValue(I.getReturnValue(), SF);
924 popStackAndReturnValueToCaller(RetTy, Result);
927 void Interpreter::visitUnwindInst(UnwindInst &I) {
932 if (ECStack.empty ())
934 Inst = ECStack.back ().Caller.getInstruction ();
935 } while (!(Inst && isa<InvokeInst> (Inst)));
937 // Return from invoke
938 ExecutionContext &InvokingSF = ECStack.back ();
939 InvokingSF.Caller = CallSite ();
941 // Go to exceptional destination BB of invoke instruction
942 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
945 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
946 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
950 void Interpreter::visitBranchInst(BranchInst &I) {
951 ExecutionContext &SF = ECStack.back();
954 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
955 if (!I.isUnconditional()) {
956 Value *Cond = I.getCondition();
957 if (getOperandValue(Cond, SF).Int1Val == 0) // If false cond...
958 Dest = I.getSuccessor(1);
960 SwitchToNewBasicBlock(Dest, SF);
963 void Interpreter::visitSwitchInst(SwitchInst &I) {
964 ExecutionContext &SF = ECStack.back();
965 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
966 const Type *ElTy = I.getOperand(0)->getType();
968 // Check to see if any of the cases match...
969 BasicBlock *Dest = 0;
970 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
971 if (executeICMP_EQ(CondVal,
972 getOperandValue(I.getOperand(i), SF), ElTy).Int1Val) {
973 Dest = cast<BasicBlock>(I.getOperand(i+1));
977 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
978 SwitchToNewBasicBlock(Dest, SF);
981 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
982 // This function handles the actual updating of block and instruction iterators
983 // as well as execution of all of the PHI nodes in the destination block.
985 // This method does this because all of the PHI nodes must be executed
986 // atomically, reading their inputs before any of the results are updated. Not
987 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
988 // their inputs. If the input PHI node is updated before it is read, incorrect
989 // results can happen. Thus we use a two phase approach.
991 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
992 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
993 SF.CurBB = Dest; // Update CurBB to branch destination
994 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
996 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
998 // Loop over all of the PHI nodes in the current block, reading their inputs.
999 std::vector<GenericValue> ResultValues;
1001 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
1002 // Search for the value corresponding to this previous bb...
1003 int i = PN->getBasicBlockIndex(PrevBB);
1004 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
1005 Value *IncomingValue = PN->getIncomingValue(i);
1007 // Save the incoming value for this PHI node...
1008 ResultValues.push_back(getOperandValue(IncomingValue, SF));
1011 // Now loop over all of the PHI nodes setting their values...
1012 SF.CurInst = SF.CurBB->begin();
1013 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
1014 PHINode *PN = cast<PHINode>(SF.CurInst);
1015 SetValue(PN, ResultValues[i], SF);
1019 //===----------------------------------------------------------------------===//
1020 // Memory Instruction Implementations
1021 //===----------------------------------------------------------------------===//
1023 void Interpreter::visitAllocationInst(AllocationInst &I) {
1024 ExecutionContext &SF = ECStack.back();
1026 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
1028 // Get the number of elements being allocated by the array...
1029 unsigned NumElements = getOperandValue(I.getOperand(0), SF).Int32Val;
1031 // Allocate enough memory to hold the type...
1032 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
1034 GenericValue Result = PTOGV(Memory);
1035 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
1036 SetValue(&I, Result, SF);
1038 if (I.getOpcode() == Instruction::Alloca)
1039 ECStack.back().Allocas.add(Memory);
1042 void Interpreter::visitFreeInst(FreeInst &I) {
1043 ExecutionContext &SF = ECStack.back();
1044 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
1045 GenericValue Value = getOperandValue(I.getOperand(0), SF);
1046 // TODO: Check to make sure memory is allocated
1047 free(GVTOP(Value)); // Free memory
1050 // getElementOffset - The workhorse for getelementptr.
1052 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
1053 gep_type_iterator E,
1054 ExecutionContext &SF) {
1055 assert(isa<PointerType>(Ptr->getType()) &&
1056 "Cannot getElementOffset of a nonpointer type!");
1058 PointerTy Total = 0;
1060 for (; I != E; ++I) {
1061 if (const StructType *STy = dyn_cast<StructType>(*I)) {
1062 const StructLayout *SLO = TD.getStructLayout(STy);
1064 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
1065 unsigned Index = unsigned(CPU->getZExtValue());
1067 Total += (PointerTy)SLO->MemberOffsets[Index];
1069 const SequentialType *ST = cast<SequentialType>(*I);
1070 // Get the index number for the array... which must be long type...
1071 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
1075 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
1077 Idx = (int64_t)(int32_t)IdxGV.Int32Val;
1078 else if (BitWidth == 64)
1079 Idx = (int64_t)IdxGV.Int64Val;
1081 assert(0 && "Invalid index type for getelementptr");
1082 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
1086 GenericValue Result;
1087 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
1091 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1092 ExecutionContext &SF = ECStack.back();
1093 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
1094 gep_type_begin(I), gep_type_end(I), SF), SF);
1097 void Interpreter::visitLoadInst(LoadInst &I) {
1098 ExecutionContext &SF = ECStack.back();
1099 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
1100 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
1101 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
1102 SetValue(&I, Result, SF);
1105 void Interpreter::visitStoreInst(StoreInst &I) {
1106 ExecutionContext &SF = ECStack.back();
1107 GenericValue Val = getOperandValue(I.getOperand(0), SF);
1108 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
1109 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
1110 I.getOperand(0)->getType());
1113 //===----------------------------------------------------------------------===//
1114 // Miscellaneous Instruction Implementations
1115 //===----------------------------------------------------------------------===//
1117 void Interpreter::visitCallSite(CallSite CS) {
1118 ExecutionContext &SF = ECStack.back();
1120 // Check to see if this is an intrinsic function call...
1121 if (Function *F = CS.getCalledFunction())
1122 if (F->isExternal ())
1123 switch (F->getIntrinsicID()) {
1124 case Intrinsic::not_intrinsic:
1126 case Intrinsic::vastart: { // va_start
1127 GenericValue ArgIndex;
1128 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1129 ArgIndex.UIntPairVal.second = 0;
1130 SetValue(CS.getInstruction(), ArgIndex, SF);
1133 case Intrinsic::vaend: // va_end is a noop for the interpreter
1135 case Intrinsic::vacopy: // va_copy: dest = src
1136 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1139 // If it is an unknown intrinsic function, use the intrinsic lowering
1140 // class to transform it into hopefully tasty LLVM code.
1142 Instruction *Prev = CS.getInstruction()->getPrev();
1143 BasicBlock *Parent = CS.getInstruction()->getParent();
1144 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1146 // Restore the CurInst pointer to the first instruction newly inserted, if
1149 SF.CurInst = Parent->begin();
1158 std::vector<GenericValue> ArgVals;
1159 const unsigned NumArgs = SF.Caller.arg_size();
1160 ArgVals.reserve(NumArgs);
1161 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1162 e = SF.Caller.arg_end(); i != e; ++i) {
1164 ArgVals.push_back(getOperandValue(V, SF));
1165 // Promote all integral types whose size is < sizeof(int) into ints. We do
1166 // this by zero or sign extending the value as appropriate according to the
1168 const Type *Ty = V->getType();
1169 if (Ty->isInteger()) {
1170 if (Ty->getPrimitiveSizeInBits() == 1)
1171 ArgVals.back().Int32Val = ArgVals.back().Int1Val;
1172 else if (Ty->getPrimitiveSizeInBits() <= 8)
1173 ArgVals.back().Int32Val = ArgVals.back().Int8Val;
1174 else if (Ty->getPrimitiveSizeInBits() <= 16)
1175 ArgVals.back().Int32Val = ArgVals.back().Int16Val;
1179 // To handle indirect calls, we must get the pointer value from the argument
1180 // and treat it as a function pointer.
1181 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
1182 callFunction((Function*)GVTOP(SRC), ArgVals);
1185 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
1188 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1189 unsigned BitWidth = ITy->getBitWidth();
1191 Dest.Int8Val = ((uint8_t)Src1.Int8Val) << ((uint32_t)Src2.Int8Val);
1192 else if (BitWidth <= 16)
1193 Dest.Int16Val = ((uint16_t)Src1.Int16Val) << ((uint32_t)Src2.Int8Val);
1194 else if (BitWidth <= 32)
1195 Dest.Int32Val = ((uint32_t)Src1.Int32Val) << ((uint32_t)Src2.Int8Val);
1196 else if (BitWidth <= 64)
1197 Dest.Int64Val = ((uint64_t)Src1.Int64Val) << ((uint32_t)Src2.Int8Val);
1199 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1203 cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
1209 static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
1212 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1213 unsigned BitWidth = ITy->getBitWidth();
1215 Dest.Int8Val = ((uint8_t)Src1.Int8Val) >> ((uint32_t)Src2.Int8Val);
1216 else if (BitWidth <= 16)
1217 Dest.Int16Val = ((uint16_t)Src1.Int16Val) >> ((uint32_t)Src2.Int8Val);
1218 else if (BitWidth <= 32)
1219 Dest.Int32Val = ((uint32_t)Src1.Int32Val) >> ((uint32_t)Src2.Int8Val);
1220 else if (BitWidth <= 64)
1221 Dest.Int64Val = ((uint64_t)Src1.Int64Val) >> ((uint32_t)Src2.Int8Val);
1223 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1227 cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
1233 static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
1236 if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1237 unsigned BitWidth = ITy->getBitWidth();
1239 Dest.Int8Val = ((int8_t)Src1.Int8Val) >> ((int32_t)Src2.Int8Val);
1240 else if (BitWidth <= 16)
1241 Dest.Int16Val = ((int16_t)Src1.Int16Val) >> ((int32_t)Src2.Int8Val);
1242 else if (BitWidth <= 32)
1243 Dest.Int32Val = ((int32_t)Src1.Int32Val) >> ((int32_t)Src2.Int8Val);
1244 else if (BitWidth <= 64)
1245 Dest.Int64Val = ((int64_t)Src1.Int64Val) >> ((int32_t)Src2.Int8Val);
1247 cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
1251 cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
1257 void Interpreter::visitShl(ShiftInst &I) {
1258 ExecutionContext &SF = ECStack.back();
1259 const Type *Ty = I.getOperand(0)->getType();
1260 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1261 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1263 Dest = executeShlInst (Src1, Src2, Ty);
1264 SetValue(&I, Dest, SF);
1267 void Interpreter::visitLShr(ShiftInst &I) {
1268 ExecutionContext &SF = ECStack.back();
1269 const Type *Ty = I.getOperand(0)->getType();
1270 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1271 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1273 Dest = executeLShrInst (Src1, Src2, Ty);
1274 SetValue(&I, Dest, SF);
1277 void Interpreter::visitAShr(ShiftInst &I) {
1278 ExecutionContext &SF = ECStack.back();
1279 const Type *Ty = I.getOperand(0)->getType();
1280 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1281 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1283 Dest = executeAShrInst (Src1, Src2, Ty);
1284 SetValue(&I, Dest, SF);
1287 #define INTEGER_ASSIGN(DEST, BITWIDTH, VAL) \
1289 uint64_t Mask = (1ull << BITWIDTH) - 1; \
1290 if (BITWIDTH == 1) { \
1291 Dest.Int1Val = (bool) (VAL & Mask); \
1292 } else if (BITWIDTH <= 8) { \
1293 Dest.Int8Val = (uint8_t) (VAL & Mask); \
1294 } else if (BITWIDTH <= 16) { \
1295 Dest.Int16Val = (uint16_t) (VAL & Mask); \
1296 } else if (BITWIDTH <= 32) { \
1297 Dest.Int32Val = (uint32_t) (VAL & Mask); \
1299 Dest.Int64Val = (uint64_t) (VAL & Mask); \
1302 GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
1303 ExecutionContext &SF) {
1304 const Type *SrcTy = SrcVal->getType();
1305 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1306 const IntegerType *DITy = cast<IntegerType>(DstTy);
1307 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1308 unsigned DBitWidth = DITy->getBitWidth();
1309 unsigned SBitWidth = SITy->getBitWidth();
1310 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1311 "Integer types > 64 bits not supported");
1312 assert(SBitWidth > DBitWidth && "Invalid truncate");
1314 // Mask the source value to its actual bit width. This ensures that any
1315 // high order bits are cleared.
1316 uint64_t Mask = (1ULL << DBitWidth) - 1;
1317 uint64_t MaskedVal = 0;
1319 MaskedVal = Src.Int8Val & Mask;
1320 else if (SBitWidth <= 16)
1321 MaskedVal = Src.Int16Val & Mask;
1322 else if (SBitWidth <= 32)
1323 MaskedVal = Src.Int32Val & Mask;
1325 MaskedVal = Src.Int64Val & Mask;
1327 INTEGER_ASSIGN(Dest, DBitWidth, MaskedVal);
1331 GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
1332 ExecutionContext &SF) {
1333 const Type *SrcTy = SrcVal->getType();
1334 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1335 const IntegerType *DITy = cast<IntegerType>(DstTy);
1336 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1337 unsigned DBitWidth = DITy->getBitWidth();
1338 unsigned SBitWidth = SITy->getBitWidth();
1339 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1340 "Integer types > 64 bits not supported");
1341 assert(SBitWidth < DBitWidth && "Invalid sign extend");
1342 int64_t Extended = 0;
1344 // For sign extension from bool, we must extend the source bits.
1345 Extended = 0 - (Src.Int1Val & 1);
1346 else if (SBitWidth <= 8)
1347 Extended = (int64_t) (int8_t)Src.Int8Val;
1348 else if (SBitWidth <= 16)
1349 Extended = (int64_t) (int16_t)Src.Int16Val;
1350 else if (SBitWidth <= 32)
1351 Extended = (int64_t) (int32_t)Src.Int32Val;
1353 Extended = (int64_t) Src.Int64Val;
1355 // Now that we have a sign extended value, assign it to the destination
1356 INTEGER_ASSIGN(Dest, DBitWidth, Extended);
1360 GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
1361 ExecutionContext &SF) {
1362 const Type *SrcTy = SrcVal->getType();
1363 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1364 const IntegerType *DITy = cast<IntegerType>(DstTy);
1365 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1366 unsigned DBitWidth = DITy->getBitWidth();
1367 unsigned SBitWidth = SITy->getBitWidth();
1368 assert(SBitWidth <= 64 && DBitWidth <= 64 &&
1369 "Integer types > 64 bits not supported");
1370 assert(SBitWidth < DBitWidth && "Invalid sign extend");
1371 uint64_t Extended = 0;
1373 // For sign extension from bool, we must extend the source bits.
1374 Extended = (uint64_t) (Src.Int1Val & 1);
1375 else if (SBitWidth <= 8)
1376 Extended = (uint64_t) (uint8_t)Src.Int8Val;
1377 else if (SBitWidth <= 16)
1378 Extended = (uint64_t) (uint16_t)Src.Int16Val;
1379 else if (SBitWidth <= 32)
1380 Extended = (uint64_t) (uint32_t)Src.Int32Val;
1382 Extended = (uint64_t) Src.Int64Val;
1384 // Now that we have a sign extended value, assign it to the destination
1385 INTEGER_ASSIGN(Dest, DBitWidth, Extended);
1389 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
1390 ExecutionContext &SF) {
1391 const Type *SrcTy = SrcVal->getType();
1392 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1393 assert(SrcTy == Type::DoubleTy && DstTy == Type::FloatTy &&
1394 "Invalid FPTrunc instruction");
1395 Dest.FloatVal = (float) Src.DoubleVal;
1399 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
1400 ExecutionContext &SF) {
1401 const Type *SrcTy = SrcVal->getType();
1402 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1403 assert(SrcTy == Type::FloatTy && DstTy == Type::DoubleTy &&
1404 "Invalid FPTrunc instruction");
1405 Dest.DoubleVal = (double) Src.FloatVal;
1409 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
1410 ExecutionContext &SF) {
1411 const Type *SrcTy = SrcVal->getType();
1412 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1413 const IntegerType *DITy = cast<IntegerType>(DstTy);
1414 unsigned DBitWidth = DITy->getBitWidth();
1415 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1416 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
1417 uint64_t Converted = 0;
1418 if (SrcTy->getTypeID() == Type::FloatTyID)
1419 Converted = (uint64_t) Src.FloatVal;
1421 Converted = (uint64_t) Src.DoubleVal;
1423 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1427 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1428 ExecutionContext &SF) {
1429 const Type *SrcTy = SrcVal->getType();
1430 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1431 const IntegerType *DITy = cast<IntegerType>(DstTy);
1432 unsigned DBitWidth = DITy->getBitWidth();
1433 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1434 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1435 int64_t Converted = 0;
1436 if (SrcTy->getTypeID() == Type::FloatTyID)
1437 Converted = (int64_t) Src.FloatVal;
1439 Converted = (int64_t) Src.DoubleVal;
1441 INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1445 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1446 ExecutionContext &SF) {
1447 const Type *SrcTy = SrcVal->getType();
1448 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1449 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1450 unsigned SBitWidth = SITy->getBitWidth();
1451 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1452 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1453 uint64_t Converted = 0;
1455 Converted = (uint64_t) Src.Int1Val;
1456 else if (SBitWidth <= 8)
1457 Converted = (uint64_t) Src.Int8Val;
1458 else if (SBitWidth <= 16)
1459 Converted = (uint64_t) Src.Int16Val;
1460 else if (SBitWidth <= 32)
1461 Converted = (uint64_t) Src.Int32Val;
1463 Converted = (uint64_t) Src.Int64Val;
1465 if (DstTy->getTypeID() == Type::FloatTyID)
1466 Dest.FloatVal = (float) Converted;
1468 Dest.DoubleVal = (double) Converted;
1472 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1473 ExecutionContext &SF) {
1474 const Type *SrcTy = SrcVal->getType();
1475 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1476 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1477 unsigned SBitWidth = SITy->getBitWidth();
1478 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1479 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1480 int64_t Converted = 0;
1482 Converted = 0LL - Src.Int1Val;
1483 else if (SBitWidth <= 8)
1484 Converted = (int64_t) (int8_t)Src.Int8Val;
1485 else if (SBitWidth <= 16)
1486 Converted = (int64_t) (int16_t)Src.Int16Val;
1487 else if (SBitWidth <= 32)
1488 Converted = (int64_t) (int32_t)Src.Int32Val;
1490 Converted = (int64_t) Src.Int64Val;
1492 if (DstTy->getTypeID() == Type::FloatTyID)
1493 Dest.FloatVal = (float) Converted;
1495 Dest.DoubleVal = (double) Converted;
1499 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1500 ExecutionContext &SF) {
1501 const Type *SrcTy = SrcVal->getType();
1502 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1503 const IntegerType *DITy = cast<IntegerType>(DstTy);
1504 unsigned DBitWidth = DITy->getBitWidth();
1505 assert(DBitWidth <= 64 && "Integer types > 64 bits not supported");
1506 assert(isa<PointerType>(SrcTy) && "Invalid PtrToInt instruction");
1507 INTEGER_ASSIGN(Dest, DBitWidth, (intptr_t) Src.PointerVal);
1511 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1512 ExecutionContext &SF) {
1513 const Type *SrcTy = SrcVal->getType();
1514 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1515 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1516 unsigned SBitWidth = SITy->getBitWidth();
1517 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1518 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1519 uint64_t Converted = 0;
1521 Converted = (uint64_t) Src.Int1Val;
1522 else if (SBitWidth <= 8)
1523 Converted = (uint64_t) Src.Int8Val;
1524 else if (SBitWidth <= 16)
1525 Converted = (uint64_t) Src.Int16Val;
1526 else if (SBitWidth <= 32)
1527 Converted = (uint64_t) Src.Int32Val;
1529 Converted = (uint64_t) Src.Int64Val;
1531 Dest.PointerVal = (PointerTy) Converted;
1535 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1536 ExecutionContext &SF) {
1538 const Type *SrcTy = SrcVal->getType();
1539 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1540 if (isa<PointerType>(DstTy)) {
1541 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1542 Dest.PointerVal = Src.PointerVal;
1543 } else if (DstTy->isInteger()) {
1544 const IntegerType *DITy = cast<IntegerType>(DstTy);
1545 unsigned DBitWidth = DITy->getBitWidth();
1546 if (SrcTy == Type::FloatTy) {
1547 Dest.Int32Val = FloatToBits(Src.FloatVal);
1548 } else if (SrcTy == Type::DoubleTy) {
1549 Dest.Int64Val = DoubleToBits(Src.DoubleVal);
1550 } else if (SrcTy->isInteger()) {
1551 const IntegerType *SITy = cast<IntegerType>(SrcTy);
1552 unsigned SBitWidth = SITy->getBitWidth();
1553 assert(SBitWidth <= 64 && "Integer types > 64 bits not supported");
1554 assert(SBitWidth == DBitWidth && "Invalid BitCast");
1556 Dest.Int1Val = Src.Int1Val;
1557 else if (SBitWidth <= 8)
1558 Dest.Int8Val = Src.Int8Val;
1559 else if (SBitWidth <= 16)
1560 Dest.Int16Val = Src.Int16Val;
1561 else if (SBitWidth <= 32)
1562 Dest.Int32Val = Src.Int32Val;
1564 Dest.Int64Val = Src.Int64Val;
1566 assert(0 && "Invalid BitCast");
1567 } else if (DstTy == Type::FloatTy) {
1568 if (SrcTy->isInteger())
1569 Dest.FloatVal = BitsToFloat(Src.Int32Val);
1571 Dest.FloatVal = Src.FloatVal;
1572 } else if (DstTy == Type::DoubleTy) {
1573 if (SrcTy->isInteger())
1574 Dest.DoubleVal = BitsToDouble(Src.Int64Val);
1576 Dest.DoubleVal = Src.DoubleVal;
1578 assert(0 && "Invalid Bitcast");
1583 void Interpreter::visitTruncInst(TruncInst &I) {
1584 ExecutionContext &SF = ECStack.back();
1585 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1588 void Interpreter::visitSExtInst(SExtInst &I) {
1589 ExecutionContext &SF = ECStack.back();
1590 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1593 void Interpreter::visitZExtInst(ZExtInst &I) {
1594 ExecutionContext &SF = ECStack.back();
1595 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1598 void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1599 ExecutionContext &SF = ECStack.back();
1600 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1603 void Interpreter::visitFPExtInst(FPExtInst &I) {
1604 ExecutionContext &SF = ECStack.back();
1605 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1608 void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1609 ExecutionContext &SF = ECStack.back();
1610 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1613 void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1614 ExecutionContext &SF = ECStack.back();
1615 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1618 void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1619 ExecutionContext &SF = ECStack.back();
1620 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1623 void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1624 ExecutionContext &SF = ECStack.back();
1625 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1628 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1629 ExecutionContext &SF = ECStack.back();
1630 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1633 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1634 ExecutionContext &SF = ECStack.back();
1635 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1638 void Interpreter::visitBitCastInst(BitCastInst &I) {
1639 ExecutionContext &SF = ECStack.back();
1640 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1643 #define IMPLEMENT_VAARG(TY) \
1644 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1646 void Interpreter::visitVAArgInst(VAArgInst &I) {
1647 ExecutionContext &SF = ECStack.back();
1649 // Get the incoming valist parameter. LLI treats the valist as a
1650 // (ec-stack-depth var-arg-index) pair.
1651 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1653 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1654 .VarArgs[VAList.UIntPairVal.second];
1655 const Type *Ty = I.getType();
1656 switch (Ty->getTypeID()) {
1657 case Type::IntegerTyID: {
1658 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
1660 Dest.Int1Val = Src.Int1Val;
1661 else if (BitWidth <= 8)
1662 Dest.Int8Val = Src.Int8Val;
1663 else if (BitWidth <= 16)
1664 Dest.Int16Val = Src.Int16Val;
1665 else if (BitWidth <= 32)
1666 Dest.Int32Val = Src.Int32Val;
1667 else if (BitWidth <= 64)
1668 Dest.Int64Val = Src.Int64Val;
1670 assert("Integer types > 64 bits not supported");
1672 IMPLEMENT_VAARG(Pointer);
1673 IMPLEMENT_VAARG(Float);
1674 IMPLEMENT_VAARG(Double);
1676 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1680 // Set the Value of this Instruction.
1681 SetValue(&I, Dest, SF);
1683 // Move the pointer to the next vararg.
1684 ++VAList.UIntPairVal.second;
1687 //===----------------------------------------------------------------------===//
1688 // Dispatch and Execution Code
1689 //===----------------------------------------------------------------------===//
1691 //===----------------------------------------------------------------------===//
1692 // callFunction - Execute the specified function...
1694 void Interpreter::callFunction(Function *F,
1695 const std::vector<GenericValue> &ArgVals) {
1696 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1697 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1698 "Incorrect number of arguments passed into function call!");
1699 // Make a new stack frame... and fill it in.
1700 ECStack.push_back(ExecutionContext());
1701 ExecutionContext &StackFrame = ECStack.back();
1702 StackFrame.CurFunction = F;
1704 // Special handling for external functions.
1705 if (F->isExternal()) {
1706 GenericValue Result = callExternalFunction (F, ArgVals);
1707 // Simulate a 'ret' instruction of the appropriate type.
1708 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1712 // Get pointers to first LLVM BB & Instruction in function.
1713 StackFrame.CurBB = F->begin();
1714 StackFrame.CurInst = StackFrame.CurBB->begin();
1716 // Run through the function arguments and initialize their values...
1717 assert((ArgVals.size() == F->arg_size() ||
1718 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1719 "Invalid number of values passed to function invocation!");
1721 // Handle non-varargs arguments...
1723 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
1724 SetValue(AI, ArgVals[i], StackFrame);
1726 // Handle varargs arguments...
1727 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1730 void Interpreter::run() {
1731 while (!ECStack.empty()) {
1732 // Interpret a single instruction & increment the "PC".
1733 ExecutionContext &SF = ECStack.back(); // Current stack frame
1734 Instruction &I = *SF.CurInst++; // Increment before execute
1736 // Track the number of dynamic instructions executed.
1739 DOUT << "About to interpret: " << I;
1740 visit(I); // Dispatch to one of the visit* methods...