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