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