1 //===-- Execution.cpp - Implement code to simulate the program ------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains the actual instruction interpreter. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #define DEBUG_TYPE "interpreter" 15 #include "Interpreter.h" 16 #include "llvm/Constants.h" 17 #include "llvm/DerivedTypes.h" 18 #include "llvm/Instructions.h" 19 #include "llvm/CodeGen/IntrinsicLowering.h" 20 #include "llvm/Support/GetElementPtrTypeIterator.h" 21 #include "llvm/ADT/APInt.h" 22 #include "llvm/ADT/Statistic.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Support/Debug.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include "llvm/Support/MathExtras.h" 27 #include <algorithm> 28 #include <cmath> 29 using namespace llvm; 30 31 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed"); 32 33 static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden, 34 cl::desc("make the interpreter print every volatile load and store")); 35 36 //===----------------------------------------------------------------------===// 37 // Various Helper Functions 38 //===----------------------------------------------------------------------===// 39 40 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) { 41 SF.Values[V] = Val; 42 } 43 44 //===----------------------------------------------------------------------===// 45 // Binary Instruction Implementations 46 //===----------------------------------------------------------------------===// 47 48 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \ 49 case Type::TY##TyID: \ 50 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \ 51 break 52 53 static void executeFAddInst(GenericValue &Dest, GenericValue Src1, 54 GenericValue Src2, Type *Ty) { 55 switch (Ty->getTypeID()) { 56 IMPLEMENT_BINARY_OPERATOR(+, Float); 57 IMPLEMENT_BINARY_OPERATOR(+, Double); 58 default: 59 dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n"; 60 llvm_unreachable(0); 61 } 62 } 63 64 static void executeFSubInst(GenericValue &Dest, GenericValue Src1, 65 GenericValue Src2, Type *Ty) { 66 switch (Ty->getTypeID()) { 67 IMPLEMENT_BINARY_OPERATOR(-, Float); 68 IMPLEMENT_BINARY_OPERATOR(-, Double); 69 default: 70 dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n"; 71 llvm_unreachable(0); 72 } 73 } 74 75 static void executeFMulInst(GenericValue &Dest, GenericValue Src1, 76 GenericValue Src2, Type *Ty) { 77 switch (Ty->getTypeID()) { 78 IMPLEMENT_BINARY_OPERATOR(*, Float); 79 IMPLEMENT_BINARY_OPERATOR(*, Double); 80 default: 81 dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n"; 82 llvm_unreachable(0); 83 } 84 } 85 86 static void executeFDivInst(GenericValue &Dest, GenericValue Src1, 87 GenericValue Src2, Type *Ty) { 88 switch (Ty->getTypeID()) { 89 IMPLEMENT_BINARY_OPERATOR(/, Float); 90 IMPLEMENT_BINARY_OPERATOR(/, Double); 91 default: 92 dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n"; 93 llvm_unreachable(0); 94 } 95 } 96 97 static void executeFRemInst(GenericValue &Dest, GenericValue Src1, 98 GenericValue Src2, Type *Ty) { 99 switch (Ty->getTypeID()) { 100 case Type::FloatTyID: 101 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal); 102 break; 103 case Type::DoubleTyID: 104 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal); 105 break; 106 default: 107 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n"; 108 llvm_unreachable(0); 109 } 110 } 111 112 #define IMPLEMENT_INTEGER_ICMP(OP, TY) \ 113 case Type::IntegerTyID: \ 114 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \ 115 break; 116 117 // Handle pointers specially because they must be compared with only as much 118 // width as the host has. We _do not_ want to be comparing 64 bit values when 119 // running on a 32-bit target, otherwise the upper 32 bits might mess up 120 // comparisons if they contain garbage. 121 #define IMPLEMENT_POINTER_ICMP(OP) \ 122 case Type::PointerTyID: \ 123 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \ 124 (void*)(intptr_t)Src2.PointerVal); \ 125 break; 126 127 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2, 128 Type *Ty) { 129 GenericValue Dest; 130 switch (Ty->getTypeID()) { 131 IMPLEMENT_INTEGER_ICMP(eq,Ty); 132 IMPLEMENT_POINTER_ICMP(==); 133 default: 134 dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n"; 135 llvm_unreachable(0); 136 } 137 return Dest; 138 } 139 140 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2, 141 Type *Ty) { 142 GenericValue Dest; 143 switch (Ty->getTypeID()) { 144 IMPLEMENT_INTEGER_ICMP(ne,Ty); 145 IMPLEMENT_POINTER_ICMP(!=); 146 default: 147 dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n"; 148 llvm_unreachable(0); 149 } 150 return Dest; 151 } 152 153 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2, 154 Type *Ty) { 155 GenericValue Dest; 156 switch (Ty->getTypeID()) { 157 IMPLEMENT_INTEGER_ICMP(ult,Ty); 158 IMPLEMENT_POINTER_ICMP(<); 159 default: 160 dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n"; 161 llvm_unreachable(0); 162 } 163 return Dest; 164 } 165 166 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2, 167 Type *Ty) { 168 GenericValue Dest; 169 switch (Ty->getTypeID()) { 170 IMPLEMENT_INTEGER_ICMP(slt,Ty); 171 IMPLEMENT_POINTER_ICMP(<); 172 default: 173 dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n"; 174 llvm_unreachable(0); 175 } 176 return Dest; 177 } 178 179 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2, 180 Type *Ty) { 181 GenericValue Dest; 182 switch (Ty->getTypeID()) { 183 IMPLEMENT_INTEGER_ICMP(ugt,Ty); 184 IMPLEMENT_POINTER_ICMP(>); 185 default: 186 dbgs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n"; 187 llvm_unreachable(0); 188 } 189 return Dest; 190 } 191 192 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2, 193 Type *Ty) { 194 GenericValue Dest; 195 switch (Ty->getTypeID()) { 196 IMPLEMENT_INTEGER_ICMP(sgt,Ty); 197 IMPLEMENT_POINTER_ICMP(>); 198 default: 199 dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n"; 200 llvm_unreachable(0); 201 } 202 return Dest; 203 } 204 205 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2, 206 Type *Ty) { 207 GenericValue Dest; 208 switch (Ty->getTypeID()) { 209 IMPLEMENT_INTEGER_ICMP(ule,Ty); 210 IMPLEMENT_POINTER_ICMP(<=); 211 default: 212 dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n"; 213 llvm_unreachable(0); 214 } 215 return Dest; 216 } 217 218 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2, 219 Type *Ty) { 220 GenericValue Dest; 221 switch (Ty->getTypeID()) { 222 IMPLEMENT_INTEGER_ICMP(sle,Ty); 223 IMPLEMENT_POINTER_ICMP(<=); 224 default: 225 dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n"; 226 llvm_unreachable(0); 227 } 228 return Dest; 229 } 230 231 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2, 232 Type *Ty) { 233 GenericValue Dest; 234 switch (Ty->getTypeID()) { 235 IMPLEMENT_INTEGER_ICMP(uge,Ty); 236 IMPLEMENT_POINTER_ICMP(>=); 237 default: 238 dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n"; 239 llvm_unreachable(0); 240 } 241 return Dest; 242 } 243 244 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2, 245 Type *Ty) { 246 GenericValue Dest; 247 switch (Ty->getTypeID()) { 248 IMPLEMENT_INTEGER_ICMP(sge,Ty); 249 IMPLEMENT_POINTER_ICMP(>=); 250 default: 251 dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n"; 252 llvm_unreachable(0); 253 } 254 return Dest; 255 } 256 257 void Interpreter::visitICmpInst(ICmpInst &I) { 258 ExecutionContext &SF = ECStack.back(); 259 Type *Ty = I.getOperand(0)->getType(); 260 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 261 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 262 GenericValue R; // Result 263 264 switch (I.getPredicate()) { 265 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break; 266 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break; 267 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break; 268 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break; 269 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break; 270 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break; 271 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break; 272 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break; 273 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break; 274 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break; 275 default: 276 dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I; 277 llvm_unreachable(0); 278 } 279 280 SetValue(&I, R, SF); 281 } 282 283 #define IMPLEMENT_FCMP(OP, TY) \ 284 case Type::TY##TyID: \ 285 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \ 286 break 287 288 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2, 289 Type *Ty) { 290 GenericValue Dest; 291 switch (Ty->getTypeID()) { 292 IMPLEMENT_FCMP(==, Float); 293 IMPLEMENT_FCMP(==, Double); 294 default: 295 dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n"; 296 llvm_unreachable(0); 297 } 298 return Dest; 299 } 300 301 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2, 302 Type *Ty) { 303 GenericValue Dest; 304 switch (Ty->getTypeID()) { 305 IMPLEMENT_FCMP(!=, Float); 306 IMPLEMENT_FCMP(!=, Double); 307 308 default: 309 dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n"; 310 llvm_unreachable(0); 311 } 312 return Dest; 313 } 314 315 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2, 316 Type *Ty) { 317 GenericValue Dest; 318 switch (Ty->getTypeID()) { 319 IMPLEMENT_FCMP(<=, Float); 320 IMPLEMENT_FCMP(<=, Double); 321 default: 322 dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n"; 323 llvm_unreachable(0); 324 } 325 return Dest; 326 } 327 328 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2, 329 Type *Ty) { 330 GenericValue Dest; 331 switch (Ty->getTypeID()) { 332 IMPLEMENT_FCMP(>=, Float); 333 IMPLEMENT_FCMP(>=, Double); 334 default: 335 dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n"; 336 llvm_unreachable(0); 337 } 338 return Dest; 339 } 340 341 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2, 342 Type *Ty) { 343 GenericValue Dest; 344 switch (Ty->getTypeID()) { 345 IMPLEMENT_FCMP(<, Float); 346 IMPLEMENT_FCMP(<, Double); 347 default: 348 dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n"; 349 llvm_unreachable(0); 350 } 351 return Dest; 352 } 353 354 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2, 355 Type *Ty) { 356 GenericValue Dest; 357 switch (Ty->getTypeID()) { 358 IMPLEMENT_FCMP(>, Float); 359 IMPLEMENT_FCMP(>, Double); 360 default: 361 dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n"; 362 llvm_unreachable(0); 363 } 364 return Dest; 365 } 366 367 #define IMPLEMENT_UNORDERED(TY, X,Y) \ 368 if (TY->isFloatTy()) { \ 369 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \ 370 Dest.IntVal = APInt(1,true); \ 371 return Dest; \ 372 } \ 373 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \ 374 Dest.IntVal = APInt(1,true); \ 375 return Dest; \ 376 } 377 378 379 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2, 380 Type *Ty) { 381 GenericValue Dest; 382 IMPLEMENT_UNORDERED(Ty, Src1, Src2) 383 return executeFCMP_OEQ(Src1, Src2, Ty); 384 } 385 386 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2, 387 Type *Ty) { 388 GenericValue Dest; 389 IMPLEMENT_UNORDERED(Ty, Src1, Src2) 390 return executeFCMP_ONE(Src1, Src2, Ty); 391 } 392 393 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2, 394 Type *Ty) { 395 GenericValue Dest; 396 IMPLEMENT_UNORDERED(Ty, Src1, Src2) 397 return executeFCMP_OLE(Src1, Src2, Ty); 398 } 399 400 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2, 401 Type *Ty) { 402 GenericValue Dest; 403 IMPLEMENT_UNORDERED(Ty, Src1, Src2) 404 return executeFCMP_OGE(Src1, Src2, Ty); 405 } 406 407 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2, 408 Type *Ty) { 409 GenericValue Dest; 410 IMPLEMENT_UNORDERED(Ty, Src1, Src2) 411 return executeFCMP_OLT(Src1, Src2, Ty); 412 } 413 414 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2, 415 Type *Ty) { 416 GenericValue Dest; 417 IMPLEMENT_UNORDERED(Ty, Src1, Src2) 418 return executeFCMP_OGT(Src1, Src2, Ty); 419 } 420 421 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2, 422 Type *Ty) { 423 GenericValue Dest; 424 if (Ty->isFloatTy()) 425 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal && 426 Src2.FloatVal == Src2.FloatVal)); 427 else 428 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal && 429 Src2.DoubleVal == Src2.DoubleVal)); 430 return Dest; 431 } 432 433 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2, 434 Type *Ty) { 435 GenericValue Dest; 436 if (Ty->isFloatTy()) 437 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal || 438 Src2.FloatVal != Src2.FloatVal)); 439 else 440 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal || 441 Src2.DoubleVal != Src2.DoubleVal)); 442 return Dest; 443 } 444 445 void Interpreter::visitFCmpInst(FCmpInst &I) { 446 ExecutionContext &SF = ECStack.back(); 447 Type *Ty = I.getOperand(0)->getType(); 448 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 449 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 450 GenericValue R; // Result 451 452 switch (I.getPredicate()) { 453 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break; 454 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break; 455 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break; 456 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break; 457 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break; 458 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break; 459 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break; 460 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break; 461 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break; 462 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break; 463 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break; 464 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break; 465 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break; 466 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break; 467 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break; 468 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break; 469 default: 470 dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I; 471 llvm_unreachable(0); 472 } 473 474 SetValue(&I, R, SF); 475 } 476 477 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1, 478 GenericValue Src2, Type *Ty) { 479 GenericValue Result; 480 switch (predicate) { 481 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty); 482 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty); 483 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty); 484 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty); 485 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty); 486 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty); 487 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty); 488 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty); 489 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty); 490 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty); 491 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty); 492 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty); 493 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty); 494 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty); 495 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty); 496 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty); 497 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty); 498 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty); 499 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty); 500 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty); 501 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty); 502 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty); 503 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty); 504 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty); 505 case FCmpInst::FCMP_FALSE: { 506 GenericValue Result; 507 Result.IntVal = APInt(1, false); 508 return Result; 509 } 510 case FCmpInst::FCMP_TRUE: { 511 GenericValue Result; 512 Result.IntVal = APInt(1, true); 513 return Result; 514 } 515 default: 516 dbgs() << "Unhandled Cmp predicate\n"; 517 llvm_unreachable(0); 518 } 519 } 520 521 void Interpreter::visitBinaryOperator(BinaryOperator &I) { 522 ExecutionContext &SF = ECStack.back(); 523 Type *Ty = I.getOperand(0)->getType(); 524 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 525 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 526 GenericValue R; // Result 527 528 switch (I.getOpcode()) { 529 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break; 530 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break; 531 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break; 532 case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break; 533 case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break; 534 case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break; 535 case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break; 536 case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break; 537 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break; 538 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break; 539 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break; 540 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break; 541 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break; 542 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break; 543 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break; 544 default: 545 dbgs() << "Don't know how to handle this binary operator!\n-->" << I; 546 llvm_unreachable(0); 547 } 548 549 SetValue(&I, R, SF); 550 } 551 552 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2, 553 GenericValue Src3) { 554 return Src1.IntVal == 0 ? Src3 : Src2; 555 } 556 557 void Interpreter::visitSelectInst(SelectInst &I) { 558 ExecutionContext &SF = ECStack.back(); 559 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 560 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 561 GenericValue Src3 = getOperandValue(I.getOperand(2), SF); 562 GenericValue R = executeSelectInst(Src1, Src2, Src3); 563 SetValue(&I, R, SF); 564 } 565 566 567 //===----------------------------------------------------------------------===// 568 // Terminator Instruction Implementations 569 //===----------------------------------------------------------------------===// 570 571 void Interpreter::exitCalled(GenericValue GV) { 572 // runAtExitHandlers() assumes there are no stack frames, but 573 // if exit() was called, then it had a stack frame. Blow away 574 // the stack before interpreting atexit handlers. 575 ECStack.clear(); 576 runAtExitHandlers(); 577 exit(GV.IntVal.zextOrTrunc(32).getZExtValue()); 578 } 579 580 /// Pop the last stack frame off of ECStack and then copy the result 581 /// back into the result variable if we are not returning void. The 582 /// result variable may be the ExitValue, or the Value of the calling 583 /// CallInst if there was a previous stack frame. This method may 584 /// invalidate any ECStack iterators you have. This method also takes 585 /// care of switching to the normal destination BB, if we are returning 586 /// from an invoke. 587 /// 588 void Interpreter::popStackAndReturnValueToCaller(Type *RetTy, 589 GenericValue Result) { 590 // Pop the current stack frame. 591 ECStack.pop_back(); 592 593 if (ECStack.empty()) { // Finished main. Put result into exit code... 594 if (RetTy && !RetTy->isVoidTy()) { // Nonvoid return type? 595 ExitValue = Result; // Capture the exit value of the program 596 } else { 597 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped)); 598 } 599 } else { 600 // If we have a previous stack frame, and we have a previous call, 601 // fill in the return value... 602 ExecutionContext &CallingSF = ECStack.back(); 603 if (Instruction *I = CallingSF.Caller.getInstruction()) { 604 // Save result... 605 if (!CallingSF.Caller.getType()->isVoidTy()) 606 SetValue(I, Result, CallingSF); 607 if (InvokeInst *II = dyn_cast<InvokeInst> (I)) 608 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF); 609 CallingSF.Caller = CallSite(); // We returned from the call... 610 } 611 } 612 } 613 614 void Interpreter::visitReturnInst(ReturnInst &I) { 615 ExecutionContext &SF = ECStack.back(); 616 Type *RetTy = Type::getVoidTy(I.getContext()); 617 GenericValue Result; 618 619 // Save away the return value... (if we are not 'ret void') 620 if (I.getNumOperands()) { 621 RetTy = I.getReturnValue()->getType(); 622 Result = getOperandValue(I.getReturnValue(), SF); 623 } 624 625 popStackAndReturnValueToCaller(RetTy, Result); 626 } 627 628 void Interpreter::visitUnreachableInst(UnreachableInst &I) { 629 report_fatal_error("Program executed an 'unreachable' instruction!"); 630 } 631 632 void Interpreter::visitBranchInst(BranchInst &I) { 633 ExecutionContext &SF = ECStack.back(); 634 BasicBlock *Dest; 635 636 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest... 637 if (!I.isUnconditional()) { 638 Value *Cond = I.getCondition(); 639 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond... 640 Dest = I.getSuccessor(1); 641 } 642 SwitchToNewBasicBlock(Dest, SF); 643 } 644 645 void Interpreter::visitSwitchInst(SwitchInst &I) { 646 ExecutionContext &SF = ECStack.back(); 647 Value* Cond = I.getCondition(); 648 Type *ElTy = Cond->getType(); 649 GenericValue CondVal = getOperandValue(Cond, SF); 650 651 // Check to see if any of the cases match... 652 BasicBlock *Dest = 0; 653 unsigned NumCases = I.getNumCases(); 654 // Skip the first item since that's the default case. 655 for (unsigned i = 0; i < NumCases; ++i) { 656 GenericValue CaseVal = getOperandValue(I.getCaseValue(i), SF); 657 if (executeICMP_EQ(CondVal, CaseVal, ElTy).IntVal != 0) { 658 Dest = cast<BasicBlock>(I.getCaseSuccessor(i)); 659 break; 660 } 661 } 662 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default 663 SwitchToNewBasicBlock(Dest, SF); 664 } 665 666 void Interpreter::visitIndirectBrInst(IndirectBrInst &I) { 667 ExecutionContext &SF = ECStack.back(); 668 void *Dest = GVTOP(getOperandValue(I.getAddress(), SF)); 669 SwitchToNewBasicBlock((BasicBlock*)Dest, SF); 670 } 671 672 673 // SwitchToNewBasicBlock - This method is used to jump to a new basic block. 674 // This function handles the actual updating of block and instruction iterators 675 // as well as execution of all of the PHI nodes in the destination block. 676 // 677 // This method does this because all of the PHI nodes must be executed 678 // atomically, reading their inputs before any of the results are updated. Not 679 // doing this can cause problems if the PHI nodes depend on other PHI nodes for 680 // their inputs. If the input PHI node is updated before it is read, incorrect 681 // results can happen. Thus we use a two phase approach. 682 // 683 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){ 684 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from... 685 SF.CurBB = Dest; // Update CurBB to branch destination 686 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr... 687 688 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do 689 690 // Loop over all of the PHI nodes in the current block, reading their inputs. 691 std::vector<GenericValue> ResultValues; 692 693 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) { 694 // Search for the value corresponding to this previous bb... 695 int i = PN->getBasicBlockIndex(PrevBB); 696 assert(i != -1 && "PHINode doesn't contain entry for predecessor??"); 697 Value *IncomingValue = PN->getIncomingValue(i); 698 699 // Save the incoming value for this PHI node... 700 ResultValues.push_back(getOperandValue(IncomingValue, SF)); 701 } 702 703 // Now loop over all of the PHI nodes setting their values... 704 SF.CurInst = SF.CurBB->begin(); 705 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) { 706 PHINode *PN = cast<PHINode>(SF.CurInst); 707 SetValue(PN, ResultValues[i], SF); 708 } 709 } 710 711 //===----------------------------------------------------------------------===// 712 // Memory Instruction Implementations 713 //===----------------------------------------------------------------------===// 714 715 void Interpreter::visitAllocaInst(AllocaInst &I) { 716 ExecutionContext &SF = ECStack.back(); 717 718 Type *Ty = I.getType()->getElementType(); // Type to be allocated 719 720 // Get the number of elements being allocated by the array... 721 unsigned NumElements = 722 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue(); 723 724 unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty); 725 726 // Avoid malloc-ing zero bytes, use max()... 727 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize); 728 729 // Allocate enough memory to hold the type... 730 void *Memory = malloc(MemToAlloc); 731 732 DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x " 733 << NumElements << " (Total: " << MemToAlloc << ") at " 734 << uintptr_t(Memory) << '\n'); 735 736 GenericValue Result = PTOGV(Memory); 737 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!"); 738 SetValue(&I, Result, SF); 739 740 if (I.getOpcode() == Instruction::Alloca) 741 ECStack.back().Allocas.add(Memory); 742 } 743 744 // getElementOffset - The workhorse for getelementptr. 745 // 746 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I, 747 gep_type_iterator E, 748 ExecutionContext &SF) { 749 assert(Ptr->getType()->isPointerTy() && 750 "Cannot getElementOffset of a nonpointer type!"); 751 752 uint64_t Total = 0; 753 754 for (; I != E; ++I) { 755 if (StructType *STy = dyn_cast<StructType>(*I)) { 756 const StructLayout *SLO = TD.getStructLayout(STy); 757 758 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand()); 759 unsigned Index = unsigned(CPU->getZExtValue()); 760 761 Total += SLO->getElementOffset(Index); 762 } else { 763 SequentialType *ST = cast<SequentialType>(*I); 764 // Get the index number for the array... which must be long type... 765 GenericValue IdxGV = getOperandValue(I.getOperand(), SF); 766 767 int64_t Idx; 768 unsigned BitWidth = 769 cast<IntegerType>(I.getOperand()->getType())->getBitWidth(); 770 if (BitWidth == 32) 771 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue(); 772 else { 773 assert(BitWidth == 64 && "Invalid index type for getelementptr"); 774 Idx = (int64_t)IdxGV.IntVal.getZExtValue(); 775 } 776 Total += TD.getTypeAllocSize(ST->getElementType())*Idx; 777 } 778 } 779 780 GenericValue Result; 781 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total; 782 DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n"); 783 return Result; 784 } 785 786 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) { 787 ExecutionContext &SF = ECStack.back(); 788 SetValue(&I, executeGEPOperation(I.getPointerOperand(), 789 gep_type_begin(I), gep_type_end(I), SF), SF); 790 } 791 792 void Interpreter::visitLoadInst(LoadInst &I) { 793 ExecutionContext &SF = ECStack.back(); 794 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); 795 GenericValue *Ptr = (GenericValue*)GVTOP(SRC); 796 GenericValue Result; 797 LoadValueFromMemory(Result, Ptr, I.getType()); 798 SetValue(&I, Result, SF); 799 if (I.isVolatile() && PrintVolatile) 800 dbgs() << "Volatile load " << I; 801 } 802 803 void Interpreter::visitStoreInst(StoreInst &I) { 804 ExecutionContext &SF = ECStack.back(); 805 GenericValue Val = getOperandValue(I.getOperand(0), SF); 806 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); 807 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC), 808 I.getOperand(0)->getType()); 809 if (I.isVolatile() && PrintVolatile) 810 dbgs() << "Volatile store: " << I; 811 } 812 813 //===----------------------------------------------------------------------===// 814 // Miscellaneous Instruction Implementations 815 //===----------------------------------------------------------------------===// 816 817 void Interpreter::visitCallSite(CallSite CS) { 818 ExecutionContext &SF = ECStack.back(); 819 820 // Check to see if this is an intrinsic function call... 821 Function *F = CS.getCalledFunction(); 822 if (F && F->isDeclaration()) 823 switch (F->getIntrinsicID()) { 824 case Intrinsic::not_intrinsic: 825 break; 826 case Intrinsic::vastart: { // va_start 827 GenericValue ArgIndex; 828 ArgIndex.UIntPairVal.first = ECStack.size() - 1; 829 ArgIndex.UIntPairVal.second = 0; 830 SetValue(CS.getInstruction(), ArgIndex, SF); 831 return; 832 } 833 case Intrinsic::vaend: // va_end is a noop for the interpreter 834 return; 835 case Intrinsic::vacopy: // va_copy: dest = src 836 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF); 837 return; 838 default: 839 // If it is an unknown intrinsic function, use the intrinsic lowering 840 // class to transform it into hopefully tasty LLVM code. 841 // 842 BasicBlock::iterator me(CS.getInstruction()); 843 BasicBlock *Parent = CS.getInstruction()->getParent(); 844 bool atBegin(Parent->begin() == me); 845 if (!atBegin) 846 --me; 847 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction())); 848 849 // Restore the CurInst pointer to the first instruction newly inserted, if 850 // any. 851 if (atBegin) { 852 SF.CurInst = Parent->begin(); 853 } else { 854 SF.CurInst = me; 855 ++SF.CurInst; 856 } 857 return; 858 } 859 860 861 SF.Caller = CS; 862 std::vector<GenericValue> ArgVals; 863 const unsigned NumArgs = SF.Caller.arg_size(); 864 ArgVals.reserve(NumArgs); 865 uint16_t pNum = 1; 866 for (CallSite::arg_iterator i = SF.Caller.arg_begin(), 867 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) { 868 Value *V = *i; 869 ArgVals.push_back(getOperandValue(V, SF)); 870 } 871 872 // To handle indirect calls, we must get the pointer value from the argument 873 // and treat it as a function pointer. 874 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF); 875 callFunction((Function*)GVTOP(SRC), ArgVals); 876 } 877 878 void Interpreter::visitShl(BinaryOperator &I) { 879 ExecutionContext &SF = ECStack.back(); 880 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 881 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 882 GenericValue Dest; 883 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth()) 884 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue()); 885 else 886 Dest.IntVal = Src1.IntVal; 887 888 SetValue(&I, Dest, SF); 889 } 890 891 void Interpreter::visitLShr(BinaryOperator &I) { 892 ExecutionContext &SF = ECStack.back(); 893 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 894 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 895 GenericValue Dest; 896 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth()) 897 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue()); 898 else 899 Dest.IntVal = Src1.IntVal; 900 901 SetValue(&I, Dest, SF); 902 } 903 904 void Interpreter::visitAShr(BinaryOperator &I) { 905 ExecutionContext &SF = ECStack.back(); 906 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 907 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 908 GenericValue Dest; 909 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth()) 910 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue()); 911 else 912 Dest.IntVal = Src1.IntVal; 913 914 SetValue(&I, Dest, SF); 915 } 916 917 GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy, 918 ExecutionContext &SF) { 919 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 920 IntegerType *DITy = cast<IntegerType>(DstTy); 921 unsigned DBitWidth = DITy->getBitWidth(); 922 Dest.IntVal = Src.IntVal.trunc(DBitWidth); 923 return Dest; 924 } 925 926 GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy, 927 ExecutionContext &SF) { 928 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 929 IntegerType *DITy = cast<IntegerType>(DstTy); 930 unsigned DBitWidth = DITy->getBitWidth(); 931 Dest.IntVal = Src.IntVal.sext(DBitWidth); 932 return Dest; 933 } 934 935 GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy, 936 ExecutionContext &SF) { 937 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 938 IntegerType *DITy = cast<IntegerType>(DstTy); 939 unsigned DBitWidth = DITy->getBitWidth(); 940 Dest.IntVal = Src.IntVal.zext(DBitWidth); 941 return Dest; 942 } 943 944 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy, 945 ExecutionContext &SF) { 946 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 947 assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() && 948 "Invalid FPTrunc instruction"); 949 Dest.FloatVal = (float) Src.DoubleVal; 950 return Dest; 951 } 952 953 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy, 954 ExecutionContext &SF) { 955 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 956 assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() && 957 "Invalid FPTrunc instruction"); 958 Dest.DoubleVal = (double) Src.FloatVal; 959 return Dest; 960 } 961 962 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy, 963 ExecutionContext &SF) { 964 Type *SrcTy = SrcVal->getType(); 965 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth(); 966 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 967 assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction"); 968 969 if (SrcTy->getTypeID() == Type::FloatTyID) 970 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth); 971 else 972 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth); 973 return Dest; 974 } 975 976 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy, 977 ExecutionContext &SF) { 978 Type *SrcTy = SrcVal->getType(); 979 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth(); 980 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 981 assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction"); 982 983 if (SrcTy->getTypeID() == Type::FloatTyID) 984 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth); 985 else 986 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth); 987 return Dest; 988 } 989 990 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy, 991 ExecutionContext &SF) { 992 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 993 assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction"); 994 995 if (DstTy->getTypeID() == Type::FloatTyID) 996 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal); 997 else 998 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal); 999 return Dest; 1000 } 1001 1002 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy, 1003 ExecutionContext &SF) { 1004 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 1005 assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction"); 1006 1007 if (DstTy->getTypeID() == Type::FloatTyID) 1008 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal); 1009 else 1010 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal); 1011 return Dest; 1012 1013 } 1014 1015 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy, 1016 ExecutionContext &SF) { 1017 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth(); 1018 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 1019 assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction"); 1020 1021 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal); 1022 return Dest; 1023 } 1024 1025 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy, 1026 ExecutionContext &SF) { 1027 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 1028 assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction"); 1029 1030 uint32_t PtrSize = TD.getPointerSizeInBits(); 1031 if (PtrSize != Src.IntVal.getBitWidth()) 1032 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize); 1033 1034 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue())); 1035 return Dest; 1036 } 1037 1038 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy, 1039 ExecutionContext &SF) { 1040 1041 Type *SrcTy = SrcVal->getType(); 1042 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 1043 if (DstTy->isPointerTy()) { 1044 assert(SrcTy->isPointerTy() && "Invalid BitCast"); 1045 Dest.PointerVal = Src.PointerVal; 1046 } else if (DstTy->isIntegerTy()) { 1047 if (SrcTy->isFloatTy()) { 1048 Dest.IntVal = APInt::floatToBits(Src.FloatVal); 1049 } else if (SrcTy->isDoubleTy()) { 1050 Dest.IntVal = APInt::doubleToBits(Src.DoubleVal); 1051 } else if (SrcTy->isIntegerTy()) { 1052 Dest.IntVal = Src.IntVal; 1053 } else 1054 llvm_unreachable("Invalid BitCast"); 1055 } else if (DstTy->isFloatTy()) { 1056 if (SrcTy->isIntegerTy()) 1057 Dest.FloatVal = Src.IntVal.bitsToFloat(); 1058 else 1059 Dest.FloatVal = Src.FloatVal; 1060 } else if (DstTy->isDoubleTy()) { 1061 if (SrcTy->isIntegerTy()) 1062 Dest.DoubleVal = Src.IntVal.bitsToDouble(); 1063 else 1064 Dest.DoubleVal = Src.DoubleVal; 1065 } else 1066 llvm_unreachable("Invalid Bitcast"); 1067 1068 return Dest; 1069 } 1070 1071 void Interpreter::visitTruncInst(TruncInst &I) { 1072 ExecutionContext &SF = ECStack.back(); 1073 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF); 1074 } 1075 1076 void Interpreter::visitSExtInst(SExtInst &I) { 1077 ExecutionContext &SF = ECStack.back(); 1078 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF); 1079 } 1080 1081 void Interpreter::visitZExtInst(ZExtInst &I) { 1082 ExecutionContext &SF = ECStack.back(); 1083 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF); 1084 } 1085 1086 void Interpreter::visitFPTruncInst(FPTruncInst &I) { 1087 ExecutionContext &SF = ECStack.back(); 1088 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF); 1089 } 1090 1091 void Interpreter::visitFPExtInst(FPExtInst &I) { 1092 ExecutionContext &SF = ECStack.back(); 1093 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF); 1094 } 1095 1096 void Interpreter::visitUIToFPInst(UIToFPInst &I) { 1097 ExecutionContext &SF = ECStack.back(); 1098 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF); 1099 } 1100 1101 void Interpreter::visitSIToFPInst(SIToFPInst &I) { 1102 ExecutionContext &SF = ECStack.back(); 1103 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF); 1104 } 1105 1106 void Interpreter::visitFPToUIInst(FPToUIInst &I) { 1107 ExecutionContext &SF = ECStack.back(); 1108 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF); 1109 } 1110 1111 void Interpreter::visitFPToSIInst(FPToSIInst &I) { 1112 ExecutionContext &SF = ECStack.back(); 1113 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF); 1114 } 1115 1116 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) { 1117 ExecutionContext &SF = ECStack.back(); 1118 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF); 1119 } 1120 1121 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) { 1122 ExecutionContext &SF = ECStack.back(); 1123 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF); 1124 } 1125 1126 void Interpreter::visitBitCastInst(BitCastInst &I) { 1127 ExecutionContext &SF = ECStack.back(); 1128 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF); 1129 } 1130 1131 #define IMPLEMENT_VAARG(TY) \ 1132 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break 1133 1134 void Interpreter::visitVAArgInst(VAArgInst &I) { 1135 ExecutionContext &SF = ECStack.back(); 1136 1137 // Get the incoming valist parameter. LLI treats the valist as a 1138 // (ec-stack-depth var-arg-index) pair. 1139 GenericValue VAList = getOperandValue(I.getOperand(0), SF); 1140 GenericValue Dest; 1141 GenericValue Src = ECStack[VAList.UIntPairVal.first] 1142 .VarArgs[VAList.UIntPairVal.second]; 1143 Type *Ty = I.getType(); 1144 switch (Ty->getTypeID()) { 1145 case Type::IntegerTyID: Dest.IntVal = Src.IntVal; 1146 IMPLEMENT_VAARG(Pointer); 1147 IMPLEMENT_VAARG(Float); 1148 IMPLEMENT_VAARG(Double); 1149 default: 1150 dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n"; 1151 llvm_unreachable(0); 1152 } 1153 1154 // Set the Value of this Instruction. 1155 SetValue(&I, Dest, SF); 1156 1157 // Move the pointer to the next vararg. 1158 ++VAList.UIntPairVal.second; 1159 } 1160 1161 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE, 1162 ExecutionContext &SF) { 1163 switch (CE->getOpcode()) { 1164 case Instruction::Trunc: 1165 return executeTruncInst(CE->getOperand(0), CE->getType(), SF); 1166 case Instruction::ZExt: 1167 return executeZExtInst(CE->getOperand(0), CE->getType(), SF); 1168 case Instruction::SExt: 1169 return executeSExtInst(CE->getOperand(0), CE->getType(), SF); 1170 case Instruction::FPTrunc: 1171 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF); 1172 case Instruction::FPExt: 1173 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF); 1174 case Instruction::UIToFP: 1175 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF); 1176 case Instruction::SIToFP: 1177 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF); 1178 case Instruction::FPToUI: 1179 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF); 1180 case Instruction::FPToSI: 1181 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF); 1182 case Instruction::PtrToInt: 1183 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF); 1184 case Instruction::IntToPtr: 1185 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF); 1186 case Instruction::BitCast: 1187 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF); 1188 case Instruction::GetElementPtr: 1189 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE), 1190 gep_type_end(CE), SF); 1191 case Instruction::FCmp: 1192 case Instruction::ICmp: 1193 return executeCmpInst(CE->getPredicate(), 1194 getOperandValue(CE->getOperand(0), SF), 1195 getOperandValue(CE->getOperand(1), SF), 1196 CE->getOperand(0)->getType()); 1197 case Instruction::Select: 1198 return executeSelectInst(getOperandValue(CE->getOperand(0), SF), 1199 getOperandValue(CE->getOperand(1), SF), 1200 getOperandValue(CE->getOperand(2), SF)); 1201 default : 1202 break; 1203 } 1204 1205 // The cases below here require a GenericValue parameter for the result 1206 // so we initialize one, compute it and then return it. 1207 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF); 1208 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF); 1209 GenericValue Dest; 1210 Type * Ty = CE->getOperand(0)->getType(); 1211 switch (CE->getOpcode()) { 1212 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break; 1213 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break; 1214 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break; 1215 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break; 1216 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break; 1217 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break; 1218 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break; 1219 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break; 1220 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break; 1221 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break; 1222 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break; 1223 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break; 1224 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break; 1225 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break; 1226 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break; 1227 case Instruction::Shl: 1228 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue()); 1229 break; 1230 case Instruction::LShr: 1231 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue()); 1232 break; 1233 case Instruction::AShr: 1234 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue()); 1235 break; 1236 default: 1237 dbgs() << "Unhandled ConstantExpr: " << *CE << "\n"; 1238 llvm_unreachable("Unhandled ConstantExpr"); 1239 } 1240 return Dest; 1241 } 1242 1243 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) { 1244 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 1245 return getConstantExprValue(CE, SF); 1246 } else if (Constant *CPV = dyn_cast<Constant>(V)) { 1247 return getConstantValue(CPV); 1248 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 1249 return PTOGV(getPointerToGlobal(GV)); 1250 } else { 1251 return SF.Values[V]; 1252 } 1253 } 1254 1255 //===----------------------------------------------------------------------===// 1256 // Dispatch and Execution Code 1257 //===----------------------------------------------------------------------===// 1258 1259 //===----------------------------------------------------------------------===// 1260 // callFunction - Execute the specified function... 1261 // 1262 void Interpreter::callFunction(Function *F, 1263 const std::vector<GenericValue> &ArgVals) { 1264 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 || 1265 ECStack.back().Caller.arg_size() == ArgVals.size()) && 1266 "Incorrect number of arguments passed into function call!"); 1267 // Make a new stack frame... and fill it in. 1268 ECStack.push_back(ExecutionContext()); 1269 ExecutionContext &StackFrame = ECStack.back(); 1270 StackFrame.CurFunction = F; 1271 1272 // Special handling for external functions. 1273 if (F->isDeclaration()) { 1274 GenericValue Result = callExternalFunction (F, ArgVals); 1275 // Simulate a 'ret' instruction of the appropriate type. 1276 popStackAndReturnValueToCaller (F->getReturnType (), Result); 1277 return; 1278 } 1279 1280 // Get pointers to first LLVM BB & Instruction in function. 1281 StackFrame.CurBB = F->begin(); 1282 StackFrame.CurInst = StackFrame.CurBB->begin(); 1283 1284 // Run through the function arguments and initialize their values... 1285 assert((ArgVals.size() == F->arg_size() || 1286 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&& 1287 "Invalid number of values passed to function invocation!"); 1288 1289 // Handle non-varargs arguments... 1290 unsigned i = 0; 1291 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); 1292 AI != E; ++AI, ++i) 1293 SetValue(AI, ArgVals[i], StackFrame); 1294 1295 // Handle varargs arguments... 1296 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end()); 1297 } 1298 1299 1300 void Interpreter::run() { 1301 while (!ECStack.empty()) { 1302 // Interpret a single instruction & increment the "PC". 1303 ExecutionContext &SF = ECStack.back(); // Current stack frame 1304 Instruction &I = *SF.CurInst++; // Increment before execute 1305 1306 // Track the number of dynamic instructions executed. 1307 ++NumDynamicInsts; 1308 1309 DEBUG(dbgs() << "About to interpret: " << I); 1310 visit(I); // Dispatch to one of the visit* methods... 1311 #if 0 1312 // This is not safe, as visiting the instruction could lower it and free I. 1313 DEBUG( 1314 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) && 1315 I.getType() != Type::VoidTy) { 1316 dbgs() << " --> "; 1317 const GenericValue &Val = SF.Values[&I]; 1318 switch (I.getType()->getTypeID()) { 1319 default: llvm_unreachable("Invalid GenericValue Type"); 1320 case Type::VoidTyID: dbgs() << "void"; break; 1321 case Type::FloatTyID: dbgs() << "float " << Val.FloatVal; break; 1322 case Type::DoubleTyID: dbgs() << "double " << Val.DoubleVal; break; 1323 case Type::PointerTyID: dbgs() << "void* " << intptr_t(Val.PointerVal); 1324 break; 1325 case Type::IntegerTyID: 1326 dbgs() << "i" << Val.IntVal.getBitWidth() << " " 1327 << Val.IntVal.toStringUnsigned(10) 1328 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n"; 1329 break; 1330 } 1331 }); 1332 #endif 1333 } 1334 } 1335