LLVM API Documentation
00001 //===-- Execution.cpp - Implement code to simulate the program ------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the actual instruction interpreter. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "interpreter" 00015 #include "Interpreter.h" 00016 #include "llvm/ADT/APInt.h" 00017 #include "llvm/ADT/Statistic.h" 00018 #include "llvm/CodeGen/IntrinsicLowering.h" 00019 #include "llvm/IR/Constants.h" 00020 #include "llvm/IR/DerivedTypes.h" 00021 #include "llvm/IR/Instructions.h" 00022 #include "llvm/Support/CommandLine.h" 00023 #include "llvm/Support/Debug.h" 00024 #include "llvm/Support/ErrorHandling.h" 00025 #include "llvm/Support/GetElementPtrTypeIterator.h" 00026 #include "llvm/Support/MathExtras.h" 00027 #include <algorithm> 00028 #include <cmath> 00029 using namespace llvm; 00030 00031 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed"); 00032 00033 static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden, 00034 cl::desc("make the interpreter print every volatile load and store")); 00035 00036 //===----------------------------------------------------------------------===// 00037 // Various Helper Functions 00038 //===----------------------------------------------------------------------===// 00039 00040 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) { 00041 SF.Values[V] = Val; 00042 } 00043 00044 //===----------------------------------------------------------------------===// 00045 // Binary Instruction Implementations 00046 //===----------------------------------------------------------------------===// 00047 00048 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \ 00049 case Type::TY##TyID: \ 00050 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \ 00051 break 00052 00053 static void executeFAddInst(GenericValue &Dest, GenericValue Src1, 00054 GenericValue Src2, Type *Ty) { 00055 switch (Ty->getTypeID()) { 00056 IMPLEMENT_BINARY_OPERATOR(+, Float); 00057 IMPLEMENT_BINARY_OPERATOR(+, Double); 00058 default: 00059 dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n"; 00060 llvm_unreachable(0); 00061 } 00062 } 00063 00064 static void executeFSubInst(GenericValue &Dest, GenericValue Src1, 00065 GenericValue Src2, Type *Ty) { 00066 switch (Ty->getTypeID()) { 00067 IMPLEMENT_BINARY_OPERATOR(-, Float); 00068 IMPLEMENT_BINARY_OPERATOR(-, Double); 00069 default: 00070 dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n"; 00071 llvm_unreachable(0); 00072 } 00073 } 00074 00075 static void executeFMulInst(GenericValue &Dest, GenericValue Src1, 00076 GenericValue Src2, Type *Ty) { 00077 switch (Ty->getTypeID()) { 00078 IMPLEMENT_BINARY_OPERATOR(*, Float); 00079 IMPLEMENT_BINARY_OPERATOR(*, Double); 00080 default: 00081 dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n"; 00082 llvm_unreachable(0); 00083 } 00084 } 00085 00086 static void executeFDivInst(GenericValue &Dest, GenericValue Src1, 00087 GenericValue Src2, Type *Ty) { 00088 switch (Ty->getTypeID()) { 00089 IMPLEMENT_BINARY_OPERATOR(/, Float); 00090 IMPLEMENT_BINARY_OPERATOR(/, Double); 00091 default: 00092 dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n"; 00093 llvm_unreachable(0); 00094 } 00095 } 00096 00097 static void executeFRemInst(GenericValue &Dest, GenericValue Src1, 00098 GenericValue Src2, Type *Ty) { 00099 switch (Ty->getTypeID()) { 00100 case Type::FloatTyID: 00101 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal); 00102 break; 00103 case Type::DoubleTyID: 00104 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal); 00105 break; 00106 default: 00107 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n"; 00108 llvm_unreachable(0); 00109 } 00110 } 00111 00112 #define IMPLEMENT_INTEGER_ICMP(OP, TY) \ 00113 case Type::IntegerTyID: \ 00114 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \ 00115 break; 00116 00117 #define IMPLEMENT_VECTOR_INTEGER_ICMP(OP, TY) \ 00118 case Type::VectorTyID: { \ 00119 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \ 00120 Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \ 00121 for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \ 00122 Dest.AggregateVal[_i].IntVal = APInt(1, \ 00123 Src1.AggregateVal[_i].IntVal.OP(Src2.AggregateVal[_i].IntVal));\ 00124 } break; 00125 00126 // Handle pointers specially because they must be compared with only as much 00127 // width as the host has. We _do not_ want to be comparing 64 bit values when 00128 // running on a 32-bit target, otherwise the upper 32 bits might mess up 00129 // comparisons if they contain garbage. 00130 #define IMPLEMENT_POINTER_ICMP(OP) \ 00131 case Type::PointerTyID: \ 00132 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \ 00133 (void*)(intptr_t)Src2.PointerVal); \ 00134 break; 00135 00136 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2, 00137 Type *Ty) { 00138 GenericValue Dest; 00139 switch (Ty->getTypeID()) { 00140 IMPLEMENT_INTEGER_ICMP(eq,Ty); 00141 IMPLEMENT_VECTOR_INTEGER_ICMP(eq,Ty); 00142 IMPLEMENT_POINTER_ICMP(==); 00143 default: 00144 dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n"; 00145 llvm_unreachable(0); 00146 } 00147 return Dest; 00148 } 00149 00150 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2, 00151 Type *Ty) { 00152 GenericValue Dest; 00153 switch (Ty->getTypeID()) { 00154 IMPLEMENT_INTEGER_ICMP(ne,Ty); 00155 IMPLEMENT_VECTOR_INTEGER_ICMP(ne,Ty); 00156 IMPLEMENT_POINTER_ICMP(!=); 00157 default: 00158 dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n"; 00159 llvm_unreachable(0); 00160 } 00161 return Dest; 00162 } 00163 00164 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2, 00165 Type *Ty) { 00166 GenericValue Dest; 00167 switch (Ty->getTypeID()) { 00168 IMPLEMENT_INTEGER_ICMP(ult,Ty); 00169 IMPLEMENT_VECTOR_INTEGER_ICMP(ult,Ty); 00170 IMPLEMENT_POINTER_ICMP(<); 00171 default: 00172 dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n"; 00173 llvm_unreachable(0); 00174 } 00175 return Dest; 00176 } 00177 00178 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2, 00179 Type *Ty) { 00180 GenericValue Dest; 00181 switch (Ty->getTypeID()) { 00182 IMPLEMENT_INTEGER_ICMP(slt,Ty); 00183 IMPLEMENT_VECTOR_INTEGER_ICMP(slt,Ty); 00184 IMPLEMENT_POINTER_ICMP(<); 00185 default: 00186 dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n"; 00187 llvm_unreachable(0); 00188 } 00189 return Dest; 00190 } 00191 00192 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2, 00193 Type *Ty) { 00194 GenericValue Dest; 00195 switch (Ty->getTypeID()) { 00196 IMPLEMENT_INTEGER_ICMP(ugt,Ty); 00197 IMPLEMENT_VECTOR_INTEGER_ICMP(ugt,Ty); 00198 IMPLEMENT_POINTER_ICMP(>); 00199 default: 00200 dbgs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n"; 00201 llvm_unreachable(0); 00202 } 00203 return Dest; 00204 } 00205 00206 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2, 00207 Type *Ty) { 00208 GenericValue Dest; 00209 switch (Ty->getTypeID()) { 00210 IMPLEMENT_INTEGER_ICMP(sgt,Ty); 00211 IMPLEMENT_VECTOR_INTEGER_ICMP(sgt,Ty); 00212 IMPLEMENT_POINTER_ICMP(>); 00213 default: 00214 dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n"; 00215 llvm_unreachable(0); 00216 } 00217 return Dest; 00218 } 00219 00220 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2, 00221 Type *Ty) { 00222 GenericValue Dest; 00223 switch (Ty->getTypeID()) { 00224 IMPLEMENT_INTEGER_ICMP(ule,Ty); 00225 IMPLEMENT_VECTOR_INTEGER_ICMP(ule,Ty); 00226 IMPLEMENT_POINTER_ICMP(<=); 00227 default: 00228 dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n"; 00229 llvm_unreachable(0); 00230 } 00231 return Dest; 00232 } 00233 00234 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2, 00235 Type *Ty) { 00236 GenericValue Dest; 00237 switch (Ty->getTypeID()) { 00238 IMPLEMENT_INTEGER_ICMP(sle,Ty); 00239 IMPLEMENT_VECTOR_INTEGER_ICMP(sle,Ty); 00240 IMPLEMENT_POINTER_ICMP(<=); 00241 default: 00242 dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n"; 00243 llvm_unreachable(0); 00244 } 00245 return Dest; 00246 } 00247 00248 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2, 00249 Type *Ty) { 00250 GenericValue Dest; 00251 switch (Ty->getTypeID()) { 00252 IMPLEMENT_INTEGER_ICMP(uge,Ty); 00253 IMPLEMENT_VECTOR_INTEGER_ICMP(uge,Ty); 00254 IMPLEMENT_POINTER_ICMP(>=); 00255 default: 00256 dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n"; 00257 llvm_unreachable(0); 00258 } 00259 return Dest; 00260 } 00261 00262 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2, 00263 Type *Ty) { 00264 GenericValue Dest; 00265 switch (Ty->getTypeID()) { 00266 IMPLEMENT_INTEGER_ICMP(sge,Ty); 00267 IMPLEMENT_VECTOR_INTEGER_ICMP(sge,Ty); 00268 IMPLEMENT_POINTER_ICMP(>=); 00269 default: 00270 dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n"; 00271 llvm_unreachable(0); 00272 } 00273 return Dest; 00274 } 00275 00276 void Interpreter::visitICmpInst(ICmpInst &I) { 00277 ExecutionContext &SF = ECStack.back(); 00278 Type *Ty = I.getOperand(0)->getType(); 00279 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 00280 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 00281 GenericValue R; // Result 00282 00283 switch (I.getPredicate()) { 00284 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break; 00285 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break; 00286 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break; 00287 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break; 00288 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break; 00289 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break; 00290 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break; 00291 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break; 00292 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break; 00293 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break; 00294 default: 00295 dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I; 00296 llvm_unreachable(0); 00297 } 00298 00299 SetValue(&I, R, SF); 00300 } 00301 00302 #define IMPLEMENT_FCMP(OP, TY) \ 00303 case Type::TY##TyID: \ 00304 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \ 00305 break 00306 00307 #define IMPLEMENT_VECTOR_FCMP_T(OP, TY) \ 00308 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \ 00309 Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \ 00310 for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \ 00311 Dest.AggregateVal[_i].IntVal = APInt(1, \ 00312 Src1.AggregateVal[_i].TY##Val OP Src2.AggregateVal[_i].TY##Val);\ 00313 break; 00314 00315 #define IMPLEMENT_VECTOR_FCMP(OP) \ 00316 case Type::VectorTyID: \ 00317 if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) { \ 00318 IMPLEMENT_VECTOR_FCMP_T(OP, Float); \ 00319 } else { \ 00320 IMPLEMENT_VECTOR_FCMP_T(OP, Double); \ 00321 } 00322 00323 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2, 00324 Type *Ty) { 00325 GenericValue Dest; 00326 switch (Ty->getTypeID()) { 00327 IMPLEMENT_FCMP(==, Float); 00328 IMPLEMENT_FCMP(==, Double); 00329 IMPLEMENT_VECTOR_FCMP(==); 00330 default: 00331 dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n"; 00332 llvm_unreachable(0); 00333 } 00334 return Dest; 00335 } 00336 00337 #define IMPLEMENT_SCALAR_NANS(TY, X,Y) \ 00338 if (TY->isFloatTy()) { \ 00339 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \ 00340 Dest.IntVal = APInt(1,false); \ 00341 return Dest; \ 00342 } \ 00343 } else { \ 00344 if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \ 00345 Dest.IntVal = APInt(1,false); \ 00346 return Dest; \ 00347 } \ 00348 } 00349 00350 #define MASK_VECTOR_NANS_T(X,Y, TZ, FLAG) \ 00351 assert(X.AggregateVal.size() == Y.AggregateVal.size()); \ 00352 Dest.AggregateVal.resize( X.AggregateVal.size() ); \ 00353 for( uint32_t _i=0;_i<X.AggregateVal.size();_i++) { \ 00354 if (X.AggregateVal[_i].TZ##Val != X.AggregateVal[_i].TZ##Val || \ 00355 Y.AggregateVal[_i].TZ##Val != Y.AggregateVal[_i].TZ##Val) \ 00356 Dest.AggregateVal[_i].IntVal = APInt(1,FLAG); \ 00357 else { \ 00358 Dest.AggregateVal[_i].IntVal = APInt(1,!FLAG); \ 00359 } \ 00360 } 00361 00362 #define MASK_VECTOR_NANS(TY, X,Y, FLAG) \ 00363 if (TY->isVectorTy()) { \ 00364 if (dyn_cast<VectorType>(TY)->getElementType()->isFloatTy()) { \ 00365 MASK_VECTOR_NANS_T(X, Y, Float, FLAG) \ 00366 } else { \ 00367 MASK_VECTOR_NANS_T(X, Y, Double, FLAG) \ 00368 } \ 00369 } \ 00370 00371 00372 00373 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2, 00374 Type *Ty) 00375 { 00376 GenericValue Dest; 00377 // if input is scalar value and Src1 or Src2 is NaN return false 00378 IMPLEMENT_SCALAR_NANS(Ty, Src1, Src2) 00379 // if vector input detect NaNs and fill mask 00380 MASK_VECTOR_NANS(Ty, Src1, Src2, false) 00381 GenericValue DestMask = Dest; 00382 switch (Ty->getTypeID()) { 00383 IMPLEMENT_FCMP(!=, Float); 00384 IMPLEMENT_FCMP(!=, Double); 00385 IMPLEMENT_VECTOR_FCMP(!=); 00386 default: 00387 dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n"; 00388 llvm_unreachable(0); 00389 } 00390 // in vector case mask out NaN elements 00391 if (Ty->isVectorTy()) 00392 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++) 00393 if (DestMask.AggregateVal[_i].IntVal == false) 00394 Dest.AggregateVal[_i].IntVal = APInt(1,false); 00395 00396 return Dest; 00397 } 00398 00399 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2, 00400 Type *Ty) { 00401 GenericValue Dest; 00402 switch (Ty->getTypeID()) { 00403 IMPLEMENT_FCMP(<=, Float); 00404 IMPLEMENT_FCMP(<=, Double); 00405 IMPLEMENT_VECTOR_FCMP(<=); 00406 default: 00407 dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n"; 00408 llvm_unreachable(0); 00409 } 00410 return Dest; 00411 } 00412 00413 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2, 00414 Type *Ty) { 00415 GenericValue Dest; 00416 switch (Ty->getTypeID()) { 00417 IMPLEMENT_FCMP(>=, Float); 00418 IMPLEMENT_FCMP(>=, Double); 00419 IMPLEMENT_VECTOR_FCMP(>=); 00420 default: 00421 dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n"; 00422 llvm_unreachable(0); 00423 } 00424 return Dest; 00425 } 00426 00427 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2, 00428 Type *Ty) { 00429 GenericValue Dest; 00430 switch (Ty->getTypeID()) { 00431 IMPLEMENT_FCMP(<, Float); 00432 IMPLEMENT_FCMP(<, Double); 00433 IMPLEMENT_VECTOR_FCMP(<); 00434 default: 00435 dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n"; 00436 llvm_unreachable(0); 00437 } 00438 return Dest; 00439 } 00440 00441 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2, 00442 Type *Ty) { 00443 GenericValue Dest; 00444 switch (Ty->getTypeID()) { 00445 IMPLEMENT_FCMP(>, Float); 00446 IMPLEMENT_FCMP(>, Double); 00447 IMPLEMENT_VECTOR_FCMP(>); 00448 default: 00449 dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n"; 00450 llvm_unreachable(0); 00451 } 00452 return Dest; 00453 } 00454 00455 #define IMPLEMENT_UNORDERED(TY, X,Y) \ 00456 if (TY->isFloatTy()) { \ 00457 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \ 00458 Dest.IntVal = APInt(1,true); \ 00459 return Dest; \ 00460 } \ 00461 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \ 00462 Dest.IntVal = APInt(1,true); \ 00463 return Dest; \ 00464 } 00465 00466 #define IMPLEMENT_VECTOR_UNORDERED(TY, X,Y, _FUNC) \ 00467 if (TY->isVectorTy()) { \ 00468 GenericValue DestMask = Dest; \ 00469 Dest = _FUNC(Src1, Src2, Ty); \ 00470 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++) \ 00471 if (DestMask.AggregateVal[_i].IntVal == true) \ 00472 Dest.AggregateVal[_i].IntVal = APInt(1,true); \ 00473 return Dest; \ 00474 } 00475 00476 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2, 00477 Type *Ty) { 00478 GenericValue Dest; 00479 IMPLEMENT_UNORDERED(Ty, Src1, Src2) 00480 MASK_VECTOR_NANS(Ty, Src1, Src2, true) 00481 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OEQ) 00482 return executeFCMP_OEQ(Src1, Src2, Ty); 00483 00484 } 00485 00486 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2, 00487 Type *Ty) { 00488 GenericValue Dest; 00489 IMPLEMENT_UNORDERED(Ty, Src1, Src2) 00490 MASK_VECTOR_NANS(Ty, Src1, Src2, true) 00491 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_ONE) 00492 return executeFCMP_ONE(Src1, Src2, Ty); 00493 } 00494 00495 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2, 00496 Type *Ty) { 00497 GenericValue Dest; 00498 IMPLEMENT_UNORDERED(Ty, Src1, Src2) 00499 MASK_VECTOR_NANS(Ty, Src1, Src2, true) 00500 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLE) 00501 return executeFCMP_OLE(Src1, Src2, Ty); 00502 } 00503 00504 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2, 00505 Type *Ty) { 00506 GenericValue Dest; 00507 IMPLEMENT_UNORDERED(Ty, Src1, Src2) 00508 MASK_VECTOR_NANS(Ty, Src1, Src2, true) 00509 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGE) 00510 return executeFCMP_OGE(Src1, Src2, Ty); 00511 } 00512 00513 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2, 00514 Type *Ty) { 00515 GenericValue Dest; 00516 IMPLEMENT_UNORDERED(Ty, Src1, Src2) 00517 MASK_VECTOR_NANS(Ty, Src1, Src2, true) 00518 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLT) 00519 return executeFCMP_OLT(Src1, Src2, Ty); 00520 } 00521 00522 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2, 00523 Type *Ty) { 00524 GenericValue Dest; 00525 IMPLEMENT_UNORDERED(Ty, Src1, Src2) 00526 MASK_VECTOR_NANS(Ty, Src1, Src2, true) 00527 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGT) 00528 return executeFCMP_OGT(Src1, Src2, Ty); 00529 } 00530 00531 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2, 00532 Type *Ty) { 00533 GenericValue Dest; 00534 if(Ty->isVectorTy()) { 00535 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); 00536 Dest.AggregateVal.resize( Src1.AggregateVal.size() ); 00537 if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) { 00538 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++) 00539 Dest.AggregateVal[_i].IntVal = APInt(1, 00540 ( (Src1.AggregateVal[_i].FloatVal == 00541 Src1.AggregateVal[_i].FloatVal) && 00542 (Src2.AggregateVal[_i].FloatVal == 00543 Src2.AggregateVal[_i].FloatVal))); 00544 } else { 00545 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++) 00546 Dest.AggregateVal[_i].IntVal = APInt(1, 00547 ( (Src1.AggregateVal[_i].DoubleVal == 00548 Src1.AggregateVal[_i].DoubleVal) && 00549 (Src2.AggregateVal[_i].DoubleVal == 00550 Src2.AggregateVal[_i].DoubleVal))); 00551 } 00552 } else if (Ty->isFloatTy()) 00553 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal && 00554 Src2.FloatVal == Src2.FloatVal)); 00555 else { 00556 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal && 00557 Src2.DoubleVal == Src2.DoubleVal)); 00558 } 00559 return Dest; 00560 } 00561 00562 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2, 00563 Type *Ty) { 00564 GenericValue Dest; 00565 if(Ty->isVectorTy()) { 00566 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); 00567 Dest.AggregateVal.resize( Src1.AggregateVal.size() ); 00568 if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) { 00569 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++) 00570 Dest.AggregateVal[_i].IntVal = APInt(1, 00571 ( (Src1.AggregateVal[_i].FloatVal != 00572 Src1.AggregateVal[_i].FloatVal) || 00573 (Src2.AggregateVal[_i].FloatVal != 00574 Src2.AggregateVal[_i].FloatVal))); 00575 } else { 00576 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++) 00577 Dest.AggregateVal[_i].IntVal = APInt(1, 00578 ( (Src1.AggregateVal[_i].DoubleVal != 00579 Src1.AggregateVal[_i].DoubleVal) || 00580 (Src2.AggregateVal[_i].DoubleVal != 00581 Src2.AggregateVal[_i].DoubleVal))); 00582 } 00583 } else if (Ty->isFloatTy()) 00584 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal || 00585 Src2.FloatVal != Src2.FloatVal)); 00586 else { 00587 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal || 00588 Src2.DoubleVal != Src2.DoubleVal)); 00589 } 00590 return Dest; 00591 } 00592 00593 static GenericValue executeFCMP_BOOL(GenericValue Src1, GenericValue Src2, 00594 const Type *Ty, const bool val) { 00595 GenericValue Dest; 00596 if(Ty->isVectorTy()) { 00597 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); 00598 Dest.AggregateVal.resize( Src1.AggregateVal.size() ); 00599 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++) 00600 Dest.AggregateVal[_i].IntVal = APInt(1,val); 00601 } else { 00602 Dest.IntVal = APInt(1, val); 00603 } 00604 00605 return Dest; 00606 } 00607 00608 void Interpreter::visitFCmpInst(FCmpInst &I) { 00609 ExecutionContext &SF = ECStack.back(); 00610 Type *Ty = I.getOperand(0)->getType(); 00611 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 00612 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 00613 GenericValue R; // Result 00614 00615 switch (I.getPredicate()) { 00616 default: 00617 dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I; 00618 llvm_unreachable(0); 00619 break; 00620 case FCmpInst::FCMP_FALSE: R = executeFCMP_BOOL(Src1, Src2, Ty, false); 00621 break; 00622 case FCmpInst::FCMP_TRUE: R = executeFCMP_BOOL(Src1, Src2, Ty, true); 00623 break; 00624 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break; 00625 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break; 00626 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break; 00627 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break; 00628 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break; 00629 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break; 00630 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break; 00631 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break; 00632 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break; 00633 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break; 00634 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break; 00635 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break; 00636 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break; 00637 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break; 00638 } 00639 00640 SetValue(&I, R, SF); 00641 } 00642 00643 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1, 00644 GenericValue Src2, Type *Ty) { 00645 GenericValue Result; 00646 switch (predicate) { 00647 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty); 00648 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty); 00649 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty); 00650 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty); 00651 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty); 00652 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty); 00653 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty); 00654 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty); 00655 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty); 00656 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty); 00657 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty); 00658 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty); 00659 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty); 00660 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty); 00661 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty); 00662 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty); 00663 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty); 00664 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty); 00665 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty); 00666 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty); 00667 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty); 00668 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty); 00669 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty); 00670 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty); 00671 case FCmpInst::FCMP_FALSE: return executeFCMP_BOOL(Src1, Src2, Ty, false); 00672 case FCmpInst::FCMP_TRUE: return executeFCMP_BOOL(Src1, Src2, Ty, true); 00673 default: 00674 dbgs() << "Unhandled Cmp predicate\n"; 00675 llvm_unreachable(0); 00676 } 00677 } 00678 00679 void Interpreter::visitBinaryOperator(BinaryOperator &I) { 00680 ExecutionContext &SF = ECStack.back(); 00681 Type *Ty = I.getOperand(0)->getType(); 00682 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 00683 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 00684 GenericValue R; // Result 00685 00686 // First process vector operation 00687 if (Ty->isVectorTy()) { 00688 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); 00689 R.AggregateVal.resize(Src1.AggregateVal.size()); 00690 00691 // Macros to execute binary operation 'OP' over integer vectors 00692 #define INTEGER_VECTOR_OPERATION(OP) \ 00693 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \ 00694 R.AggregateVal[i].IntVal = \ 00695 Src1.AggregateVal[i].IntVal OP Src2.AggregateVal[i].IntVal; 00696 00697 // Additional macros to execute binary operations udiv/sdiv/urem/srem since 00698 // they have different notation. 00699 #define INTEGER_VECTOR_FUNCTION(OP) \ 00700 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \ 00701 R.AggregateVal[i].IntVal = \ 00702 Src1.AggregateVal[i].IntVal.OP(Src2.AggregateVal[i].IntVal); 00703 00704 // Macros to execute binary operation 'OP' over floating point type TY 00705 // (float or double) vectors 00706 #define FLOAT_VECTOR_FUNCTION(OP, TY) \ 00707 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \ 00708 R.AggregateVal[i].TY = \ 00709 Src1.AggregateVal[i].TY OP Src2.AggregateVal[i].TY; 00710 00711 // Macros to choose appropriate TY: float or double and run operation 00712 // execution 00713 #define FLOAT_VECTOR_OP(OP) { \ 00714 if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) \ 00715 FLOAT_VECTOR_FUNCTION(OP, FloatVal) \ 00716 else { \ 00717 if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy()) \ 00718 FLOAT_VECTOR_FUNCTION(OP, DoubleVal) \ 00719 else { \ 00720 dbgs() << "Unhandled type for OP instruction: " << *Ty << "\n"; \ 00721 llvm_unreachable(0); \ 00722 } \ 00723 } \ 00724 } 00725 00726 switch(I.getOpcode()){ 00727 default: 00728 dbgs() << "Don't know how to handle this binary operator!\n-->" << I; 00729 llvm_unreachable(0); 00730 break; 00731 case Instruction::Add: INTEGER_VECTOR_OPERATION(+) break; 00732 case Instruction::Sub: INTEGER_VECTOR_OPERATION(-) break; 00733 case Instruction::Mul: INTEGER_VECTOR_OPERATION(*) break; 00734 case Instruction::UDiv: INTEGER_VECTOR_FUNCTION(udiv) break; 00735 case Instruction::SDiv: INTEGER_VECTOR_FUNCTION(sdiv) break; 00736 case Instruction::URem: INTEGER_VECTOR_FUNCTION(urem) break; 00737 case Instruction::SRem: INTEGER_VECTOR_FUNCTION(srem) break; 00738 case Instruction::And: INTEGER_VECTOR_OPERATION(&) break; 00739 case Instruction::Or: INTEGER_VECTOR_OPERATION(|) break; 00740 case Instruction::Xor: INTEGER_VECTOR_OPERATION(^) break; 00741 case Instruction::FAdd: FLOAT_VECTOR_OP(+) break; 00742 case Instruction::FSub: FLOAT_VECTOR_OP(-) break; 00743 case Instruction::FMul: FLOAT_VECTOR_OP(*) break; 00744 case Instruction::FDiv: FLOAT_VECTOR_OP(/) break; 00745 case Instruction::FRem: 00746 if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) 00747 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) 00748 R.AggregateVal[i].FloatVal = 00749 fmod(Src1.AggregateVal[i].FloatVal, Src2.AggregateVal[i].FloatVal); 00750 else { 00751 if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy()) 00752 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) 00753 R.AggregateVal[i].DoubleVal = 00754 fmod(Src1.AggregateVal[i].DoubleVal, Src2.AggregateVal[i].DoubleVal); 00755 else { 00756 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n"; 00757 llvm_unreachable(0); 00758 } 00759 } 00760 break; 00761 } 00762 } else { 00763 switch (I.getOpcode()) { 00764 default: 00765 dbgs() << "Don't know how to handle this binary operator!\n-->" << I; 00766 llvm_unreachable(0); 00767 break; 00768 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break; 00769 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break; 00770 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break; 00771 case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break; 00772 case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break; 00773 case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break; 00774 case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break; 00775 case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break; 00776 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break; 00777 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break; 00778 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break; 00779 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break; 00780 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break; 00781 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break; 00782 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break; 00783 } 00784 } 00785 SetValue(&I, R, SF); 00786 } 00787 00788 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2, 00789 GenericValue Src3) { 00790 return Src1.IntVal == 0 ? Src3 : Src2; 00791 } 00792 00793 void Interpreter::visitSelectInst(SelectInst &I) { 00794 ExecutionContext &SF = ECStack.back(); 00795 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 00796 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 00797 GenericValue Src3 = getOperandValue(I.getOperand(2), SF); 00798 GenericValue R = executeSelectInst(Src1, Src2, Src3); 00799 SetValue(&I, R, SF); 00800 } 00801 00802 00803 //===----------------------------------------------------------------------===// 00804 // Terminator Instruction Implementations 00805 //===----------------------------------------------------------------------===// 00806 00807 void Interpreter::exitCalled(GenericValue GV) { 00808 // runAtExitHandlers() assumes there are no stack frames, but 00809 // if exit() was called, then it had a stack frame. Blow away 00810 // the stack before interpreting atexit handlers. 00811 ECStack.clear(); 00812 runAtExitHandlers(); 00813 exit(GV.IntVal.zextOrTrunc(32).getZExtValue()); 00814 } 00815 00816 /// Pop the last stack frame off of ECStack and then copy the result 00817 /// back into the result variable if we are not returning void. The 00818 /// result variable may be the ExitValue, or the Value of the calling 00819 /// CallInst if there was a previous stack frame. This method may 00820 /// invalidate any ECStack iterators you have. This method also takes 00821 /// care of switching to the normal destination BB, if we are returning 00822 /// from an invoke. 00823 /// 00824 void Interpreter::popStackAndReturnValueToCaller(Type *RetTy, 00825 GenericValue Result) { 00826 // Pop the current stack frame. 00827 ECStack.pop_back(); 00828 00829 if (ECStack.empty()) { // Finished main. Put result into exit code... 00830 if (RetTy && !RetTy->isVoidTy()) { // Nonvoid return type? 00831 ExitValue = Result; // Capture the exit value of the program 00832 } else { 00833 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped)); 00834 } 00835 } else { 00836 // If we have a previous stack frame, and we have a previous call, 00837 // fill in the return value... 00838 ExecutionContext &CallingSF = ECStack.back(); 00839 if (Instruction *I = CallingSF.Caller.getInstruction()) { 00840 // Save result... 00841 if (!CallingSF.Caller.getType()->isVoidTy()) 00842 SetValue(I, Result, CallingSF); 00843 if (InvokeInst *II = dyn_cast<InvokeInst> (I)) 00844 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF); 00845 CallingSF.Caller = CallSite(); // We returned from the call... 00846 } 00847 } 00848 } 00849 00850 void Interpreter::visitReturnInst(ReturnInst &I) { 00851 ExecutionContext &SF = ECStack.back(); 00852 Type *RetTy = Type::getVoidTy(I.getContext()); 00853 GenericValue Result; 00854 00855 // Save away the return value... (if we are not 'ret void') 00856 if (I.getNumOperands()) { 00857 RetTy = I.getReturnValue()->getType(); 00858 Result = getOperandValue(I.getReturnValue(), SF); 00859 } 00860 00861 popStackAndReturnValueToCaller(RetTy, Result); 00862 } 00863 00864 void Interpreter::visitUnreachableInst(UnreachableInst &I) { 00865 report_fatal_error("Program executed an 'unreachable' instruction!"); 00866 } 00867 00868 void Interpreter::visitBranchInst(BranchInst &I) { 00869 ExecutionContext &SF = ECStack.back(); 00870 BasicBlock *Dest; 00871 00872 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest... 00873 if (!I.isUnconditional()) { 00874 Value *Cond = I.getCondition(); 00875 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond... 00876 Dest = I.getSuccessor(1); 00877 } 00878 SwitchToNewBasicBlock(Dest, SF); 00879 } 00880 00881 void Interpreter::visitSwitchInst(SwitchInst &I) { 00882 ExecutionContext &SF = ECStack.back(); 00883 Value* Cond = I.getCondition(); 00884 Type *ElTy = Cond->getType(); 00885 GenericValue CondVal = getOperandValue(Cond, SF); 00886 00887 // Check to see if any of the cases match... 00888 BasicBlock *Dest = 0; 00889 for (SwitchInst::CaseIt i = I.case_begin(), e = I.case_end(); i != e; ++i) { 00890 IntegersSubset& Case = i.getCaseValueEx(); 00891 if (Case.isSingleNumber()) { 00892 // FIXME: Currently work with ConstantInt based numbers. 00893 const ConstantInt *CI = Case.getSingleNumber(0).toConstantInt(); 00894 GenericValue Val = getOperandValue(const_cast<ConstantInt*>(CI), SF); 00895 if (executeICMP_EQ(Val, CondVal, ElTy).IntVal != 0) { 00896 Dest = cast<BasicBlock>(i.getCaseSuccessor()); 00897 break; 00898 } 00899 } 00900 if (Case.isSingleNumbersOnly()) { 00901 for (unsigned n = 0, en = Case.getNumItems(); n != en; ++n) { 00902 // FIXME: Currently work with ConstantInt based numbers. 00903 const ConstantInt *CI = Case.getSingleNumber(n).toConstantInt(); 00904 GenericValue Val = getOperandValue(const_cast<ConstantInt*>(CI), SF); 00905 if (executeICMP_EQ(Val, CondVal, ElTy).IntVal != 0) { 00906 Dest = cast<BasicBlock>(i.getCaseSuccessor()); 00907 break; 00908 } 00909 } 00910 } else 00911 for (unsigned n = 0, en = Case.getNumItems(); n != en; ++n) { 00912 IntegersSubset::Range r = Case.getItem(n); 00913 // FIXME: Currently work with ConstantInt based numbers. 00914 const ConstantInt *LowCI = r.getLow().toConstantInt(); 00915 const ConstantInt *HighCI = r.getHigh().toConstantInt(); 00916 GenericValue Low = getOperandValue(const_cast<ConstantInt*>(LowCI), SF); 00917 GenericValue High = getOperandValue(const_cast<ConstantInt*>(HighCI), SF); 00918 if (executeICMP_ULE(Low, CondVal, ElTy).IntVal != 0 && 00919 executeICMP_ULE(CondVal, High, ElTy).IntVal != 0) { 00920 Dest = cast<BasicBlock>(i.getCaseSuccessor()); 00921 break; 00922 } 00923 } 00924 } 00925 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default 00926 SwitchToNewBasicBlock(Dest, SF); 00927 } 00928 00929 void Interpreter::visitIndirectBrInst(IndirectBrInst &I) { 00930 ExecutionContext &SF = ECStack.back(); 00931 void *Dest = GVTOP(getOperandValue(I.getAddress(), SF)); 00932 SwitchToNewBasicBlock((BasicBlock*)Dest, SF); 00933 } 00934 00935 00936 // SwitchToNewBasicBlock - This method is used to jump to a new basic block. 00937 // This function handles the actual updating of block and instruction iterators 00938 // as well as execution of all of the PHI nodes in the destination block. 00939 // 00940 // This method does this because all of the PHI nodes must be executed 00941 // atomically, reading their inputs before any of the results are updated. Not 00942 // doing this can cause problems if the PHI nodes depend on other PHI nodes for 00943 // their inputs. If the input PHI node is updated before it is read, incorrect 00944 // results can happen. Thus we use a two phase approach. 00945 // 00946 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){ 00947 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from... 00948 SF.CurBB = Dest; // Update CurBB to branch destination 00949 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr... 00950 00951 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do 00952 00953 // Loop over all of the PHI nodes in the current block, reading their inputs. 00954 std::vector<GenericValue> ResultValues; 00955 00956 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) { 00957 // Search for the value corresponding to this previous bb... 00958 int i = PN->getBasicBlockIndex(PrevBB); 00959 assert(i != -1 && "PHINode doesn't contain entry for predecessor??"); 00960 Value *IncomingValue = PN->getIncomingValue(i); 00961 00962 // Save the incoming value for this PHI node... 00963 ResultValues.push_back(getOperandValue(IncomingValue, SF)); 00964 } 00965 00966 // Now loop over all of the PHI nodes setting their values... 00967 SF.CurInst = SF.CurBB->begin(); 00968 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) { 00969 PHINode *PN = cast<PHINode>(SF.CurInst); 00970 SetValue(PN, ResultValues[i], SF); 00971 } 00972 } 00973 00974 //===----------------------------------------------------------------------===// 00975 // Memory Instruction Implementations 00976 //===----------------------------------------------------------------------===// 00977 00978 void Interpreter::visitAllocaInst(AllocaInst &I) { 00979 ExecutionContext &SF = ECStack.back(); 00980 00981 Type *Ty = I.getType()->getElementType(); // Type to be allocated 00982 00983 // Get the number of elements being allocated by the array... 00984 unsigned NumElements = 00985 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue(); 00986 00987 unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty); 00988 00989 // Avoid malloc-ing zero bytes, use max()... 00990 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize); 00991 00992 // Allocate enough memory to hold the type... 00993 void *Memory = malloc(MemToAlloc); 00994 00995 DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x " 00996 << NumElements << " (Total: " << MemToAlloc << ") at " 00997 << uintptr_t(Memory) << '\n'); 00998 00999 GenericValue Result = PTOGV(Memory); 01000 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!"); 01001 SetValue(&I, Result, SF); 01002 01003 if (I.getOpcode() == Instruction::Alloca) 01004 ECStack.back().Allocas.add(Memory); 01005 } 01006 01007 // getElementOffset - The workhorse for getelementptr. 01008 // 01009 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I, 01010 gep_type_iterator E, 01011 ExecutionContext &SF) { 01012 assert(Ptr->getType()->isPointerTy() && 01013 "Cannot getElementOffset of a nonpointer type!"); 01014 01015 uint64_t Total = 0; 01016 01017 for (; I != E; ++I) { 01018 if (StructType *STy = dyn_cast<StructType>(*I)) { 01019 const StructLayout *SLO = TD.getStructLayout(STy); 01020 01021 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand()); 01022 unsigned Index = unsigned(CPU->getZExtValue()); 01023 01024 Total += SLO->getElementOffset(Index); 01025 } else { 01026 SequentialType *ST = cast<SequentialType>(*I); 01027 // Get the index number for the array... which must be long type... 01028 GenericValue IdxGV = getOperandValue(I.getOperand(), SF); 01029 01030 int64_t Idx; 01031 unsigned BitWidth = 01032 cast<IntegerType>(I.getOperand()->getType())->getBitWidth(); 01033 if (BitWidth == 32) 01034 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue(); 01035 else { 01036 assert(BitWidth == 64 && "Invalid index type for getelementptr"); 01037 Idx = (int64_t)IdxGV.IntVal.getZExtValue(); 01038 } 01039 Total += TD.getTypeAllocSize(ST->getElementType())*Idx; 01040 } 01041 } 01042 01043 GenericValue Result; 01044 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total; 01045 DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n"); 01046 return Result; 01047 } 01048 01049 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) { 01050 ExecutionContext &SF = ECStack.back(); 01051 SetValue(&I, executeGEPOperation(I.getPointerOperand(), 01052 gep_type_begin(I), gep_type_end(I), SF), SF); 01053 } 01054 01055 void Interpreter::visitLoadInst(LoadInst &I) { 01056 ExecutionContext &SF = ECStack.back(); 01057 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); 01058 GenericValue *Ptr = (GenericValue*)GVTOP(SRC); 01059 GenericValue Result; 01060 LoadValueFromMemory(Result, Ptr, I.getType()); 01061 SetValue(&I, Result, SF); 01062 if (I.isVolatile() && PrintVolatile) 01063 dbgs() << "Volatile load " << I; 01064 } 01065 01066 void Interpreter::visitStoreInst(StoreInst &I) { 01067 ExecutionContext &SF = ECStack.back(); 01068 GenericValue Val = getOperandValue(I.getOperand(0), SF); 01069 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); 01070 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC), 01071 I.getOperand(0)->getType()); 01072 if (I.isVolatile() && PrintVolatile) 01073 dbgs() << "Volatile store: " << I; 01074 } 01075 01076 //===----------------------------------------------------------------------===// 01077 // Miscellaneous Instruction Implementations 01078 //===----------------------------------------------------------------------===// 01079 01080 void Interpreter::visitCallSite(CallSite CS) { 01081 ExecutionContext &SF = ECStack.back(); 01082 01083 // Check to see if this is an intrinsic function call... 01084 Function *F = CS.getCalledFunction(); 01085 if (F && F->isDeclaration()) 01086 switch (F->getIntrinsicID()) { 01087 case Intrinsic::not_intrinsic: 01088 break; 01089 case Intrinsic::vastart: { // va_start 01090 GenericValue ArgIndex; 01091 ArgIndex.UIntPairVal.first = ECStack.size() - 1; 01092 ArgIndex.UIntPairVal.second = 0; 01093 SetValue(CS.getInstruction(), ArgIndex, SF); 01094 return; 01095 } 01096 case Intrinsic::vaend: // va_end is a noop for the interpreter 01097 return; 01098 case Intrinsic::vacopy: // va_copy: dest = src 01099 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF); 01100 return; 01101 default: 01102 // If it is an unknown intrinsic function, use the intrinsic lowering 01103 // class to transform it into hopefully tasty LLVM code. 01104 // 01105 BasicBlock::iterator me(CS.getInstruction()); 01106 BasicBlock *Parent = CS.getInstruction()->getParent(); 01107 bool atBegin(Parent->begin() == me); 01108 if (!atBegin) 01109 --me; 01110 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction())); 01111 01112 // Restore the CurInst pointer to the first instruction newly inserted, if 01113 // any. 01114 if (atBegin) { 01115 SF.CurInst = Parent->begin(); 01116 } else { 01117 SF.CurInst = me; 01118 ++SF.CurInst; 01119 } 01120 return; 01121 } 01122 01123 01124 SF.Caller = CS; 01125 std::vector<GenericValue> ArgVals; 01126 const unsigned NumArgs = SF.Caller.arg_size(); 01127 ArgVals.reserve(NumArgs); 01128 uint16_t pNum = 1; 01129 for (CallSite::arg_iterator i = SF.Caller.arg_begin(), 01130 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) { 01131 Value *V = *i; 01132 ArgVals.push_back(getOperandValue(V, SF)); 01133 } 01134 01135 // To handle indirect calls, we must get the pointer value from the argument 01136 // and treat it as a function pointer. 01137 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF); 01138 callFunction((Function*)GVTOP(SRC), ArgVals); 01139 } 01140 01141 void Interpreter::visitShl(BinaryOperator &I) { 01142 ExecutionContext &SF = ECStack.back(); 01143 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 01144 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 01145 GenericValue Dest; 01146 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth()) 01147 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue()); 01148 else 01149 Dest.IntVal = Src1.IntVal; 01150 01151 SetValue(&I, Dest, SF); 01152 } 01153 01154 void Interpreter::visitLShr(BinaryOperator &I) { 01155 ExecutionContext &SF = ECStack.back(); 01156 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 01157 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 01158 GenericValue Dest; 01159 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth()) 01160 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue()); 01161 else 01162 Dest.IntVal = Src1.IntVal; 01163 01164 SetValue(&I, Dest, SF); 01165 } 01166 01167 void Interpreter::visitAShr(BinaryOperator &I) { 01168 ExecutionContext &SF = ECStack.back(); 01169 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 01170 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 01171 GenericValue Dest; 01172 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth()) 01173 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue()); 01174 else 01175 Dest.IntVal = Src1.IntVal; 01176 01177 SetValue(&I, Dest, SF); 01178 } 01179 01180 GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy, 01181 ExecutionContext &SF) { 01182 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 01183 IntegerType *DITy = cast<IntegerType>(DstTy); 01184 unsigned DBitWidth = DITy->getBitWidth(); 01185 Dest.IntVal = Src.IntVal.trunc(DBitWidth); 01186 return Dest; 01187 } 01188 01189 GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy, 01190 ExecutionContext &SF) { 01191 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 01192 IntegerType *DITy = cast<IntegerType>(DstTy); 01193 unsigned DBitWidth = DITy->getBitWidth(); 01194 Dest.IntVal = Src.IntVal.sext(DBitWidth); 01195 return Dest; 01196 } 01197 01198 GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy, 01199 ExecutionContext &SF) { 01200 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 01201 IntegerType *DITy = cast<IntegerType>(DstTy); 01202 unsigned DBitWidth = DITy->getBitWidth(); 01203 Dest.IntVal = Src.IntVal.zext(DBitWidth); 01204 return Dest; 01205 } 01206 01207 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy, 01208 ExecutionContext &SF) { 01209 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 01210 assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() && 01211 "Invalid FPTrunc instruction"); 01212 Dest.FloatVal = (float) Src.DoubleVal; 01213 return Dest; 01214 } 01215 01216 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy, 01217 ExecutionContext &SF) { 01218 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 01219 assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() && 01220 "Invalid FPTrunc instruction"); 01221 Dest.DoubleVal = (double) Src.FloatVal; 01222 return Dest; 01223 } 01224 01225 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy, 01226 ExecutionContext &SF) { 01227 Type *SrcTy = SrcVal->getType(); 01228 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth(); 01229 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 01230 assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction"); 01231 01232 if (SrcTy->getTypeID() == Type::FloatTyID) 01233 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth); 01234 else 01235 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth); 01236 return Dest; 01237 } 01238 01239 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy, 01240 ExecutionContext &SF) { 01241 Type *SrcTy = SrcVal->getType(); 01242 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth(); 01243 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 01244 assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction"); 01245 01246 if (SrcTy->getTypeID() == Type::FloatTyID) 01247 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth); 01248 else 01249 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth); 01250 return Dest; 01251 } 01252 01253 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy, 01254 ExecutionContext &SF) { 01255 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 01256 assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction"); 01257 01258 if (DstTy->getTypeID() == Type::FloatTyID) 01259 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal); 01260 else 01261 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal); 01262 return Dest; 01263 } 01264 01265 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy, 01266 ExecutionContext &SF) { 01267 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 01268 assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction"); 01269 01270 if (DstTy->getTypeID() == Type::FloatTyID) 01271 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal); 01272 else 01273 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal); 01274 return Dest; 01275 01276 } 01277 01278 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy, 01279 ExecutionContext &SF) { 01280 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth(); 01281 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 01282 assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction"); 01283 01284 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal); 01285 return Dest; 01286 } 01287 01288 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy, 01289 ExecutionContext &SF) { 01290 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 01291 assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction"); 01292 01293 uint32_t PtrSize = TD.getPointerSizeInBits(); 01294 if (PtrSize != Src.IntVal.getBitWidth()) 01295 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize); 01296 01297 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue())); 01298 return Dest; 01299 } 01300 01301 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy, 01302 ExecutionContext &SF) { 01303 01304 Type *SrcTy = SrcVal->getType(); 01305 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 01306 if (DstTy->isPointerTy()) { 01307 assert(SrcTy->isPointerTy() && "Invalid BitCast"); 01308 Dest.PointerVal = Src.PointerVal; 01309 } else if (DstTy->isIntegerTy()) { 01310 if (SrcTy->isFloatTy()) { 01311 Dest.IntVal = APInt::floatToBits(Src.FloatVal); 01312 } else if (SrcTy->isDoubleTy()) { 01313 Dest.IntVal = APInt::doubleToBits(Src.DoubleVal); 01314 } else if (SrcTy->isIntegerTy()) { 01315 Dest.IntVal = Src.IntVal; 01316 } else 01317 llvm_unreachable("Invalid BitCast"); 01318 } else if (DstTy->isFloatTy()) { 01319 if (SrcTy->isIntegerTy()) 01320 Dest.FloatVal = Src.IntVal.bitsToFloat(); 01321 else 01322 Dest.FloatVal = Src.FloatVal; 01323 } else if (DstTy->isDoubleTy()) { 01324 if (SrcTy->isIntegerTy()) 01325 Dest.DoubleVal = Src.IntVal.bitsToDouble(); 01326 else 01327 Dest.DoubleVal = Src.DoubleVal; 01328 } else 01329 llvm_unreachable("Invalid Bitcast"); 01330 01331 return Dest; 01332 } 01333 01334 void Interpreter::visitTruncInst(TruncInst &I) { 01335 ExecutionContext &SF = ECStack.back(); 01336 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF); 01337 } 01338 01339 void Interpreter::visitSExtInst(SExtInst &I) { 01340 ExecutionContext &SF = ECStack.back(); 01341 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF); 01342 } 01343 01344 void Interpreter::visitZExtInst(ZExtInst &I) { 01345 ExecutionContext &SF = ECStack.back(); 01346 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF); 01347 } 01348 01349 void Interpreter::visitFPTruncInst(FPTruncInst &I) { 01350 ExecutionContext &SF = ECStack.back(); 01351 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF); 01352 } 01353 01354 void Interpreter::visitFPExtInst(FPExtInst &I) { 01355 ExecutionContext &SF = ECStack.back(); 01356 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF); 01357 } 01358 01359 void Interpreter::visitUIToFPInst(UIToFPInst &I) { 01360 ExecutionContext &SF = ECStack.back(); 01361 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF); 01362 } 01363 01364 void Interpreter::visitSIToFPInst(SIToFPInst &I) { 01365 ExecutionContext &SF = ECStack.back(); 01366 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF); 01367 } 01368 01369 void Interpreter::visitFPToUIInst(FPToUIInst &I) { 01370 ExecutionContext &SF = ECStack.back(); 01371 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF); 01372 } 01373 01374 void Interpreter::visitFPToSIInst(FPToSIInst &I) { 01375 ExecutionContext &SF = ECStack.back(); 01376 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF); 01377 } 01378 01379 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) { 01380 ExecutionContext &SF = ECStack.back(); 01381 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF); 01382 } 01383 01384 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) { 01385 ExecutionContext &SF = ECStack.back(); 01386 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF); 01387 } 01388 01389 void Interpreter::visitBitCastInst(BitCastInst &I) { 01390 ExecutionContext &SF = ECStack.back(); 01391 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF); 01392 } 01393 01394 #define IMPLEMENT_VAARG(TY) \ 01395 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break 01396 01397 void Interpreter::visitVAArgInst(VAArgInst &I) { 01398 ExecutionContext &SF = ECStack.back(); 01399 01400 // Get the incoming valist parameter. LLI treats the valist as a 01401 // (ec-stack-depth var-arg-index) pair. 01402 GenericValue VAList = getOperandValue(I.getOperand(0), SF); 01403 GenericValue Dest; 01404 GenericValue Src = ECStack[VAList.UIntPairVal.first] 01405 .VarArgs[VAList.UIntPairVal.second]; 01406 Type *Ty = I.getType(); 01407 switch (Ty->getTypeID()) { 01408 case Type::IntegerTyID: 01409 Dest.IntVal = Src.IntVal; 01410 break; 01411 IMPLEMENT_VAARG(Pointer); 01412 IMPLEMENT_VAARG(Float); 01413 IMPLEMENT_VAARG(Double); 01414 default: 01415 dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n"; 01416 llvm_unreachable(0); 01417 } 01418 01419 // Set the Value of this Instruction. 01420 SetValue(&I, Dest, SF); 01421 01422 // Move the pointer to the next vararg. 01423 ++VAList.UIntPairVal.second; 01424 } 01425 01426 void Interpreter::visitExtractElementInst(ExtractElementInst &I) { 01427 ExecutionContext &SF = ECStack.back(); 01428 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 01429 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 01430 GenericValue Dest; 01431 01432 Type *Ty = I.getType(); 01433 const unsigned indx = unsigned(Src2.IntVal.getZExtValue()); 01434 01435 if(Src1.AggregateVal.size() > indx) { 01436 switch (Ty->getTypeID()) { 01437 default: 01438 dbgs() << "Unhandled destination type for extractelement instruction: " 01439 << *Ty << "\n"; 01440 llvm_unreachable(0); 01441 break; 01442 case Type::IntegerTyID: 01443 Dest.IntVal = Src1.AggregateVal[indx].IntVal; 01444 break; 01445 case Type::FloatTyID: 01446 Dest.FloatVal = Src1.AggregateVal[indx].FloatVal; 01447 break; 01448 case Type::DoubleTyID: 01449 Dest.DoubleVal = Src1.AggregateVal[indx].DoubleVal; 01450 break; 01451 } 01452 } else { 01453 dbgs() << "Invalid index in extractelement instruction\n"; 01454 } 01455 01456 SetValue(&I, Dest, SF); 01457 } 01458 01459 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE, 01460 ExecutionContext &SF) { 01461 switch (CE->getOpcode()) { 01462 case Instruction::Trunc: 01463 return executeTruncInst(CE->getOperand(0), CE->getType(), SF); 01464 case Instruction::ZExt: 01465 return executeZExtInst(CE->getOperand(0), CE->getType(), SF); 01466 case Instruction::SExt: 01467 return executeSExtInst(CE->getOperand(0), CE->getType(), SF); 01468 case Instruction::FPTrunc: 01469 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF); 01470 case Instruction::FPExt: 01471 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF); 01472 case Instruction::UIToFP: 01473 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF); 01474 case Instruction::SIToFP: 01475 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF); 01476 case Instruction::FPToUI: 01477 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF); 01478 case Instruction::FPToSI: 01479 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF); 01480 case Instruction::PtrToInt: 01481 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF); 01482 case Instruction::IntToPtr: 01483 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF); 01484 case Instruction::BitCast: 01485 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF); 01486 case Instruction::GetElementPtr: 01487 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE), 01488 gep_type_end(CE), SF); 01489 case Instruction::FCmp: 01490 case Instruction::ICmp: 01491 return executeCmpInst(CE->getPredicate(), 01492 getOperandValue(CE->getOperand(0), SF), 01493 getOperandValue(CE->getOperand(1), SF), 01494 CE->getOperand(0)->getType()); 01495 case Instruction::Select: 01496 return executeSelectInst(getOperandValue(CE->getOperand(0), SF), 01497 getOperandValue(CE->getOperand(1), SF), 01498 getOperandValue(CE->getOperand(2), SF)); 01499 default : 01500 break; 01501 } 01502 01503 // The cases below here require a GenericValue parameter for the result 01504 // so we initialize one, compute it and then return it. 01505 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF); 01506 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF); 01507 GenericValue Dest; 01508 Type * Ty = CE->getOperand(0)->getType(); 01509 switch (CE->getOpcode()) { 01510 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break; 01511 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break; 01512 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break; 01513 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break; 01514 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break; 01515 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break; 01516 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break; 01517 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break; 01518 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break; 01519 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break; 01520 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break; 01521 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break; 01522 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break; 01523 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break; 01524 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break; 01525 case Instruction::Shl: 01526 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue()); 01527 break; 01528 case Instruction::LShr: 01529 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue()); 01530 break; 01531 case Instruction::AShr: 01532 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue()); 01533 break; 01534 default: 01535 dbgs() << "Unhandled ConstantExpr: " << *CE << "\n"; 01536 llvm_unreachable("Unhandled ConstantExpr"); 01537 } 01538 return Dest; 01539 } 01540 01541 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) { 01542 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 01543 return getConstantExprValue(CE, SF); 01544 } else if (Constant *CPV = dyn_cast<Constant>(V)) { 01545 return getConstantValue(CPV); 01546 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 01547 return PTOGV(getPointerToGlobal(GV)); 01548 } else { 01549 return SF.Values[V]; 01550 } 01551 } 01552 01553 //===----------------------------------------------------------------------===// 01554 // Dispatch and Execution Code 01555 //===----------------------------------------------------------------------===// 01556 01557 //===----------------------------------------------------------------------===// 01558 // callFunction - Execute the specified function... 01559 // 01560 void Interpreter::callFunction(Function *F, 01561 const std::vector<GenericValue> &ArgVals) { 01562 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 || 01563 ECStack.back().Caller.arg_size() == ArgVals.size()) && 01564 "Incorrect number of arguments passed into function call!"); 01565 // Make a new stack frame... and fill it in. 01566 ECStack.push_back(ExecutionContext()); 01567 ExecutionContext &StackFrame = ECStack.back(); 01568 StackFrame.CurFunction = F; 01569 01570 // Special handling for external functions. 01571 if (F->isDeclaration()) { 01572 GenericValue Result = callExternalFunction (F, ArgVals); 01573 // Simulate a 'ret' instruction of the appropriate type. 01574 popStackAndReturnValueToCaller (F->getReturnType (), Result); 01575 return; 01576 } 01577 01578 // Get pointers to first LLVM BB & Instruction in function. 01579 StackFrame.CurBB = F->begin(); 01580 StackFrame.CurInst = StackFrame.CurBB->begin(); 01581 01582 // Run through the function arguments and initialize their values... 01583 assert((ArgVals.size() == F->arg_size() || 01584 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&& 01585 "Invalid number of values passed to function invocation!"); 01586 01587 // Handle non-varargs arguments... 01588 unsigned i = 0; 01589 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); 01590 AI != E; ++AI, ++i) 01591 SetValue(AI, ArgVals[i], StackFrame); 01592 01593 // Handle varargs arguments... 01594 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end()); 01595 } 01596 01597 01598 void Interpreter::run() { 01599 while (!ECStack.empty()) { 01600 // Interpret a single instruction & increment the "PC". 01601 ExecutionContext &SF = ECStack.back(); // Current stack frame 01602 Instruction &I = *SF.CurInst++; // Increment before execute 01603 01604 // Track the number of dynamic instructions executed. 01605 ++NumDynamicInsts; 01606 01607 DEBUG(dbgs() << "About to interpret: " << I); 01608 visit(I); // Dispatch to one of the visit* methods... 01609 #if 0 01610 // This is not safe, as visiting the instruction could lower it and free I. 01611 DEBUG( 01612 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) && 01613 I.getType() != Type::VoidTy) { 01614 dbgs() << " --> "; 01615 const GenericValue &Val = SF.Values[&I]; 01616 switch (I.getType()->getTypeID()) { 01617 default: llvm_unreachable("Invalid GenericValue Type"); 01618 case Type::VoidTyID: dbgs() << "void"; break; 01619 case Type::FloatTyID: dbgs() << "float " << Val.FloatVal; break; 01620 case Type::DoubleTyID: dbgs() << "double " << Val.DoubleVal; break; 01621 case Type::PointerTyID: dbgs() << "void* " << intptr_t(Val.PointerVal); 01622 break; 01623 case Type::IntegerTyID: 01624 dbgs() << "i" << Val.IntVal.getBitWidth() << " " 01625 << Val.IntVal.toStringUnsigned(10) 01626 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n"; 01627 break; 01628 } 01629 }); 01630 #endif 01631 } 01632 }