LLVM API Documentation

Execution.cpp

Go to the documentation of this file.
00001 //===-- Execution.cpp - Implement code to simulate the program ------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file contains the actual instruction interpreter.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "interpreter"
00015 #include "Interpreter.h"
00016 #include "llvm/Constants.h"
00017 #include "llvm/DerivedTypes.h"
00018 #include "llvm/Instructions.h"
00019 #include "llvm/CodeGen/IntrinsicLowering.h"
00020 #include "llvm/Support/GetElementPtrTypeIterator.h"
00021 #include "llvm/ADT/Statistic.h"
00022 #include "llvm/Support/Debug.h"
00023 #include <cmath>  // For fmod
00024 using namespace llvm;
00025 
00026 namespace {
00027   Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
00028 
00029   Interpreter *TheEE = 0;
00030 }
00031 
00032 
00033 //===----------------------------------------------------------------------===//
00034 //                     Value Manipulation code
00035 //===----------------------------------------------------------------------===//
00036 
00037 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
00038                                    const Type *Ty);
00039 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
00040                                    const Type *Ty);
00041 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
00042                                    const Type *Ty);
00043 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
00044                                    const Type *Ty);
00045 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
00046                                    const Type *Ty);
00047 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
00048                                    const Type *Ty);
00049 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
00050                                    const Type *Ty);
00051 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
00052                                    const Type *Ty);
00053 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
00054                                    const Type *Ty);
00055 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
00056                                    const Type *Ty);
00057 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
00058                                    const Type *Ty);
00059 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
00060                                    const Type *Ty);
00061 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
00062                                    const Type *Ty);
00063 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
00064                                    const Type *Ty);
00065 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
00066                                    const Type *Ty);
00067 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
00068                                    const Type *Ty);
00069 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
00070                                       GenericValue Src3);
00071 
00072 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
00073                                                 ExecutionContext &SF) {
00074   switch (CE->getOpcode()) {
00075   case Instruction::Cast:
00076     return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
00077   case Instruction::GetElementPtr:
00078     return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
00079                                gep_type_end(CE), SF);
00080   case Instruction::Add:
00081     return executeAddInst(getOperandValue(CE->getOperand(0), SF),
00082                           getOperandValue(CE->getOperand(1), SF),
00083                           CE->getOperand(0)->getType());
00084   case Instruction::Sub:
00085     return executeSubInst(getOperandValue(CE->getOperand(0), SF),
00086                           getOperandValue(CE->getOperand(1), SF),
00087                           CE->getOperand(0)->getType());
00088   case Instruction::Mul:
00089     return executeMulInst(getOperandValue(CE->getOperand(0), SF),
00090                           getOperandValue(CE->getOperand(1), SF),
00091                           CE->getOperand(0)->getType());
00092   case Instruction::Div:
00093     return executeDivInst(getOperandValue(CE->getOperand(0), SF),
00094                           getOperandValue(CE->getOperand(1), SF),
00095                           CE->getOperand(0)->getType());
00096   case Instruction::Rem:
00097     return executeRemInst(getOperandValue(CE->getOperand(0), SF),
00098                           getOperandValue(CE->getOperand(1), SF),
00099                           CE->getOperand(0)->getType());
00100   case Instruction::And:
00101     return executeAndInst(getOperandValue(CE->getOperand(0), SF),
00102                           getOperandValue(CE->getOperand(1), SF),
00103                           CE->getOperand(0)->getType());
00104   case Instruction::Or:
00105     return executeOrInst(getOperandValue(CE->getOperand(0), SF),
00106                          getOperandValue(CE->getOperand(1), SF),
00107                          CE->getOperand(0)->getType());
00108   case Instruction::Xor:
00109     return executeXorInst(getOperandValue(CE->getOperand(0), SF),
00110                           getOperandValue(CE->getOperand(1), SF),
00111                           CE->getOperand(0)->getType());
00112   case Instruction::SetEQ:
00113     return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
00114                             getOperandValue(CE->getOperand(1), SF),
00115                             CE->getOperand(0)->getType());
00116   case Instruction::SetNE:
00117     return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
00118                             getOperandValue(CE->getOperand(1), SF),
00119                             CE->getOperand(0)->getType());
00120   case Instruction::SetLE:
00121     return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
00122                             getOperandValue(CE->getOperand(1), SF),
00123                             CE->getOperand(0)->getType());
00124   case Instruction::SetGE:
00125     return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
00126                             getOperandValue(CE->getOperand(1), SF),
00127                             CE->getOperand(0)->getType());
00128   case Instruction::SetLT:
00129     return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
00130                             getOperandValue(CE->getOperand(1), SF),
00131                             CE->getOperand(0)->getType());
00132   case Instruction::SetGT:
00133     return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
00134                             getOperandValue(CE->getOperand(1), SF),
00135                             CE->getOperand(0)->getType());
00136   case Instruction::Shl:
00137     return executeShlInst(getOperandValue(CE->getOperand(0), SF),
00138                           getOperandValue(CE->getOperand(1), SF),
00139                           CE->getOperand(0)->getType());
00140   case Instruction::Shr:
00141     return executeShrInst(getOperandValue(CE->getOperand(0), SF),
00142                           getOperandValue(CE->getOperand(1), SF),
00143                           CE->getOperand(0)->getType());
00144   case Instruction::Select:
00145     return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
00146                              getOperandValue(CE->getOperand(1), SF),
00147                              getOperandValue(CE->getOperand(2), SF));
00148   default:
00149     std::cerr << "Unhandled ConstantExpr: " << *CE << "\n";
00150     abort();
00151     return GenericValue();
00152   }
00153 }
00154 
00155 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
00156   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
00157     return getConstantExprValue(CE, SF);
00158   } else if (Constant *CPV = dyn_cast<Constant>(V)) {
00159     return getConstantValue(CPV);
00160   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
00161     return PTOGV(getPointerToGlobal(GV));
00162   } else {
00163     return SF.Values[V];
00164   }
00165 }
00166 
00167 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
00168   SF.Values[V] = Val;
00169 }
00170 
00171 void Interpreter::initializeExecutionEngine() {
00172   TheEE = this;
00173 }
00174 
00175 //===----------------------------------------------------------------------===//
00176 //                    Binary Instruction Implementations
00177 //===----------------------------------------------------------------------===//
00178 
00179 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
00180    case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
00181 
00182 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
00183                                    const Type *Ty) {
00184   GenericValue Dest;
00185   switch (Ty->getTypeID()) {
00186     IMPLEMENT_BINARY_OPERATOR(+, UByte);
00187     IMPLEMENT_BINARY_OPERATOR(+, SByte);
00188     IMPLEMENT_BINARY_OPERATOR(+, UShort);
00189     IMPLEMENT_BINARY_OPERATOR(+, Short);
00190     IMPLEMENT_BINARY_OPERATOR(+, UInt);
00191     IMPLEMENT_BINARY_OPERATOR(+, Int);
00192     IMPLEMENT_BINARY_OPERATOR(+, ULong);
00193     IMPLEMENT_BINARY_OPERATOR(+, Long);
00194     IMPLEMENT_BINARY_OPERATOR(+, Float);
00195     IMPLEMENT_BINARY_OPERATOR(+, Double);
00196   default:
00197     std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
00198     abort();
00199   }
00200   return Dest;
00201 }
00202 
00203 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
00204                                    const Type *Ty) {
00205   GenericValue Dest;
00206   switch (Ty->getTypeID()) {
00207     IMPLEMENT_BINARY_OPERATOR(-, UByte);
00208     IMPLEMENT_BINARY_OPERATOR(-, SByte);
00209     IMPLEMENT_BINARY_OPERATOR(-, UShort);
00210     IMPLEMENT_BINARY_OPERATOR(-, Short);
00211     IMPLEMENT_BINARY_OPERATOR(-, UInt);
00212     IMPLEMENT_BINARY_OPERATOR(-, Int);
00213     IMPLEMENT_BINARY_OPERATOR(-, ULong);
00214     IMPLEMENT_BINARY_OPERATOR(-, Long);
00215     IMPLEMENT_BINARY_OPERATOR(-, Float);
00216     IMPLEMENT_BINARY_OPERATOR(-, Double);
00217   default:
00218     std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
00219     abort();
00220   }
00221   return Dest;
00222 }
00223 
00224 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
00225                                    const Type *Ty) {
00226   GenericValue Dest;
00227   switch (Ty->getTypeID()) {
00228     IMPLEMENT_BINARY_OPERATOR(*, UByte);
00229     IMPLEMENT_BINARY_OPERATOR(*, SByte);
00230     IMPLEMENT_BINARY_OPERATOR(*, UShort);
00231     IMPLEMENT_BINARY_OPERATOR(*, Short);
00232     IMPLEMENT_BINARY_OPERATOR(*, UInt);
00233     IMPLEMENT_BINARY_OPERATOR(*, Int);
00234     IMPLEMENT_BINARY_OPERATOR(*, ULong);
00235     IMPLEMENT_BINARY_OPERATOR(*, Long);
00236     IMPLEMENT_BINARY_OPERATOR(*, Float);
00237     IMPLEMENT_BINARY_OPERATOR(*, Double);
00238   default:
00239     std::cout << "Unhandled type for Mul instruction: " << *Ty << "\n";
00240     abort();
00241   }
00242   return Dest;
00243 }
00244 
00245 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
00246                                    const Type *Ty) {
00247   GenericValue Dest;
00248   switch (Ty->getTypeID()) {
00249     IMPLEMENT_BINARY_OPERATOR(/, UByte);
00250     IMPLEMENT_BINARY_OPERATOR(/, SByte);
00251     IMPLEMENT_BINARY_OPERATOR(/, UShort);
00252     IMPLEMENT_BINARY_OPERATOR(/, Short);
00253     IMPLEMENT_BINARY_OPERATOR(/, UInt);
00254     IMPLEMENT_BINARY_OPERATOR(/, Int);
00255     IMPLEMENT_BINARY_OPERATOR(/, ULong);
00256     IMPLEMENT_BINARY_OPERATOR(/, Long);
00257     IMPLEMENT_BINARY_OPERATOR(/, Float);
00258     IMPLEMENT_BINARY_OPERATOR(/, Double);
00259   default:
00260     std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
00261     abort();
00262   }
00263   return Dest;
00264 }
00265 
00266 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
00267                                    const Type *Ty) {
00268   GenericValue Dest;
00269   switch (Ty->getTypeID()) {
00270     IMPLEMENT_BINARY_OPERATOR(%, UByte);
00271     IMPLEMENT_BINARY_OPERATOR(%, SByte);
00272     IMPLEMENT_BINARY_OPERATOR(%, UShort);
00273     IMPLEMENT_BINARY_OPERATOR(%, Short);
00274     IMPLEMENT_BINARY_OPERATOR(%, UInt);
00275     IMPLEMENT_BINARY_OPERATOR(%, Int);
00276     IMPLEMENT_BINARY_OPERATOR(%, ULong);
00277     IMPLEMENT_BINARY_OPERATOR(%, Long);
00278   case Type::FloatTyID:
00279     Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
00280     break;
00281   case Type::DoubleTyID:
00282     Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
00283     break;
00284   default:
00285     std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
00286     abort();
00287   }
00288   return Dest;
00289 }
00290 
00291 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
00292                                    const Type *Ty) {
00293   GenericValue Dest;
00294   switch (Ty->getTypeID()) {
00295     IMPLEMENT_BINARY_OPERATOR(&, Bool);
00296     IMPLEMENT_BINARY_OPERATOR(&, UByte);
00297     IMPLEMENT_BINARY_OPERATOR(&, SByte);
00298     IMPLEMENT_BINARY_OPERATOR(&, UShort);
00299     IMPLEMENT_BINARY_OPERATOR(&, Short);
00300     IMPLEMENT_BINARY_OPERATOR(&, UInt);
00301     IMPLEMENT_BINARY_OPERATOR(&, Int);
00302     IMPLEMENT_BINARY_OPERATOR(&, ULong);
00303     IMPLEMENT_BINARY_OPERATOR(&, Long);
00304   default:
00305     std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
00306     abort();
00307   }
00308   return Dest;
00309 }
00310 
00311 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
00312                                   const Type *Ty) {
00313   GenericValue Dest;
00314   switch (Ty->getTypeID()) {
00315     IMPLEMENT_BINARY_OPERATOR(|, Bool);
00316     IMPLEMENT_BINARY_OPERATOR(|, UByte);
00317     IMPLEMENT_BINARY_OPERATOR(|, SByte);
00318     IMPLEMENT_BINARY_OPERATOR(|, UShort);
00319     IMPLEMENT_BINARY_OPERATOR(|, Short);
00320     IMPLEMENT_BINARY_OPERATOR(|, UInt);
00321     IMPLEMENT_BINARY_OPERATOR(|, Int);
00322     IMPLEMENT_BINARY_OPERATOR(|, ULong);
00323     IMPLEMENT_BINARY_OPERATOR(|, Long);
00324   default:
00325     std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
00326     abort();
00327   }
00328   return Dest;
00329 }
00330 
00331 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
00332                                    const Type *Ty) {
00333   GenericValue Dest;
00334   switch (Ty->getTypeID()) {
00335     IMPLEMENT_BINARY_OPERATOR(^, Bool);
00336     IMPLEMENT_BINARY_OPERATOR(^, UByte);
00337     IMPLEMENT_BINARY_OPERATOR(^, SByte);
00338     IMPLEMENT_BINARY_OPERATOR(^, UShort);
00339     IMPLEMENT_BINARY_OPERATOR(^, Short);
00340     IMPLEMENT_BINARY_OPERATOR(^, UInt);
00341     IMPLEMENT_BINARY_OPERATOR(^, Int);
00342     IMPLEMENT_BINARY_OPERATOR(^, ULong);
00343     IMPLEMENT_BINARY_OPERATOR(^, Long);
00344   default:
00345     std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
00346     abort();
00347   }
00348   return Dest;
00349 }
00350 
00351 #define IMPLEMENT_SETCC(OP, TY) \
00352    case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
00353 
00354 // Handle pointers specially because they must be compared with only as much
00355 // width as the host has.  We _do not_ want to be comparing 64 bit values when
00356 // running on a 32-bit target, otherwise the upper 32 bits might mess up
00357 // comparisons if they contain garbage.
00358 #define IMPLEMENT_POINTERSETCC(OP) \
00359    case Type::PointerTyID: \
00360         Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
00361                        (void*)(intptr_t)Src2.PointerVal; break
00362 
00363 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
00364                                      const Type *Ty) {
00365   GenericValue Dest;
00366   switch (Ty->getTypeID()) {
00367     IMPLEMENT_SETCC(==, UByte);
00368     IMPLEMENT_SETCC(==, SByte);
00369     IMPLEMENT_SETCC(==, UShort);
00370     IMPLEMENT_SETCC(==, Short);
00371     IMPLEMENT_SETCC(==, UInt);
00372     IMPLEMENT_SETCC(==, Int);
00373     IMPLEMENT_SETCC(==, ULong);
00374     IMPLEMENT_SETCC(==, Long);
00375     IMPLEMENT_SETCC(==, Float);
00376     IMPLEMENT_SETCC(==, Double);
00377     IMPLEMENT_POINTERSETCC(==);
00378   default:
00379     std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
00380     abort();
00381   }
00382   return Dest;
00383 }
00384 
00385 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
00386                                      const Type *Ty) {
00387   GenericValue Dest;
00388   switch (Ty->getTypeID()) {
00389     IMPLEMENT_SETCC(!=, UByte);
00390     IMPLEMENT_SETCC(!=, SByte);
00391     IMPLEMENT_SETCC(!=, UShort);
00392     IMPLEMENT_SETCC(!=, Short);
00393     IMPLEMENT_SETCC(!=, UInt);
00394     IMPLEMENT_SETCC(!=, Int);
00395     IMPLEMENT_SETCC(!=, ULong);
00396     IMPLEMENT_SETCC(!=, Long);
00397     IMPLEMENT_SETCC(!=, Float);
00398     IMPLEMENT_SETCC(!=, Double);
00399     IMPLEMENT_POINTERSETCC(!=);
00400 
00401   default:
00402     std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
00403     abort();
00404   }
00405   return Dest;
00406 }
00407 
00408 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
00409                                      const Type *Ty) {
00410   GenericValue Dest;
00411   switch (Ty->getTypeID()) {
00412     IMPLEMENT_SETCC(<=, UByte);
00413     IMPLEMENT_SETCC(<=, SByte);
00414     IMPLEMENT_SETCC(<=, UShort);
00415     IMPLEMENT_SETCC(<=, Short);
00416     IMPLEMENT_SETCC(<=, UInt);
00417     IMPLEMENT_SETCC(<=, Int);
00418     IMPLEMENT_SETCC(<=, ULong);
00419     IMPLEMENT_SETCC(<=, Long);
00420     IMPLEMENT_SETCC(<=, Float);
00421     IMPLEMENT_SETCC(<=, Double);
00422     IMPLEMENT_POINTERSETCC(<=);
00423   default:
00424     std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
00425     abort();
00426   }
00427   return Dest;
00428 }
00429 
00430 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
00431                                      const Type *Ty) {
00432   GenericValue Dest;
00433   switch (Ty->getTypeID()) {
00434     IMPLEMENT_SETCC(>=, UByte);
00435     IMPLEMENT_SETCC(>=, SByte);
00436     IMPLEMENT_SETCC(>=, UShort);
00437     IMPLEMENT_SETCC(>=, Short);
00438     IMPLEMENT_SETCC(>=, UInt);
00439     IMPLEMENT_SETCC(>=, Int);
00440     IMPLEMENT_SETCC(>=, ULong);
00441     IMPLEMENT_SETCC(>=, Long);
00442     IMPLEMENT_SETCC(>=, Float);
00443     IMPLEMENT_SETCC(>=, Double);
00444     IMPLEMENT_POINTERSETCC(>=);
00445   default:
00446     std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
00447     abort();
00448   }
00449   return Dest;
00450 }
00451 
00452 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
00453                                      const Type *Ty) {
00454   GenericValue Dest;
00455   switch (Ty->getTypeID()) {
00456     IMPLEMENT_SETCC(<, UByte);
00457     IMPLEMENT_SETCC(<, SByte);
00458     IMPLEMENT_SETCC(<, UShort);
00459     IMPLEMENT_SETCC(<, Short);
00460     IMPLEMENT_SETCC(<, UInt);
00461     IMPLEMENT_SETCC(<, Int);
00462     IMPLEMENT_SETCC(<, ULong);
00463     IMPLEMENT_SETCC(<, Long);
00464     IMPLEMENT_SETCC(<, Float);
00465     IMPLEMENT_SETCC(<, Double);
00466     IMPLEMENT_POINTERSETCC(<);
00467   default:
00468     std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
00469     abort();
00470   }
00471   return Dest;
00472 }
00473 
00474 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
00475                                      const Type *Ty) {
00476   GenericValue Dest;
00477   switch (Ty->getTypeID()) {
00478     IMPLEMENT_SETCC(>, UByte);
00479     IMPLEMENT_SETCC(>, SByte);
00480     IMPLEMENT_SETCC(>, UShort);
00481     IMPLEMENT_SETCC(>, Short);
00482     IMPLEMENT_SETCC(>, UInt);
00483     IMPLEMENT_SETCC(>, Int);
00484     IMPLEMENT_SETCC(>, ULong);
00485     IMPLEMENT_SETCC(>, Long);
00486     IMPLEMENT_SETCC(>, Float);
00487     IMPLEMENT_SETCC(>, Double);
00488     IMPLEMENT_POINTERSETCC(>);
00489   default:
00490     std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
00491     abort();
00492   }
00493   return Dest;
00494 }
00495 
00496 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
00497   ExecutionContext &SF = ECStack.back();
00498   const Type *Ty    = I.getOperand(0)->getType();
00499   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
00500   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
00501   GenericValue R;   // Result
00502 
00503   switch (I.getOpcode()) {
00504   case Instruction::Add:   R = executeAddInst  (Src1, Src2, Ty); break;
00505   case Instruction::Sub:   R = executeSubInst  (Src1, Src2, Ty); break;
00506   case Instruction::Mul:   R = executeMulInst  (Src1, Src2, Ty); break;
00507   case Instruction::Div:   R = executeDivInst  (Src1, Src2, Ty); break;
00508   case Instruction::Rem:   R = executeRemInst  (Src1, Src2, Ty); break;
00509   case Instruction::And:   R = executeAndInst  (Src1, Src2, Ty); break;
00510   case Instruction::Or:    R = executeOrInst   (Src1, Src2, Ty); break;
00511   case Instruction::Xor:   R = executeXorInst  (Src1, Src2, Ty); break;
00512   case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
00513   case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
00514   case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
00515   case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
00516   case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
00517   case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
00518   default:
00519     std::cout << "Don't know how to handle this binary operator!\n-->" << I;
00520     abort();
00521   }
00522 
00523   SetValue(&I, R, SF);
00524 }
00525 
00526 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
00527                                       GenericValue Src3) {
00528   return Src1.BoolVal ? Src2 : Src3;
00529 }
00530 
00531 void Interpreter::visitSelectInst(SelectInst &I) {
00532   ExecutionContext &SF = ECStack.back();
00533   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
00534   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
00535   GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
00536   GenericValue R = executeSelectInst(Src1, Src2, Src3);
00537   SetValue(&I, R, SF);
00538 }
00539 
00540 
00541 //===----------------------------------------------------------------------===//
00542 //                     Terminator Instruction Implementations
00543 //===----------------------------------------------------------------------===//
00544 
00545 void Interpreter::exitCalled(GenericValue GV) {
00546   // runAtExitHandlers() assumes there are no stack frames, but
00547   // if exit() was called, then it had a stack frame. Blow away
00548   // the stack before interpreting atexit handlers.
00549   ECStack.clear ();
00550   runAtExitHandlers ();
00551   exit (GV.IntVal);
00552 }
00553 
00554 /// Pop the last stack frame off of ECStack and then copy the result
00555 /// back into the result variable if we are not returning void. The
00556 /// result variable may be the ExitValue, or the Value of the calling
00557 /// CallInst if there was a previous stack frame. This method may
00558 /// invalidate any ECStack iterators you have. This method also takes
00559 /// care of switching to the normal destination BB, if we are returning
00560 /// from an invoke.
00561 ///
00562 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
00563                                                   GenericValue Result) {
00564   // Pop the current stack frame.
00565   ECStack.pop_back();
00566 
00567   if (ECStack.empty()) {  // Finished main.  Put result into exit code...
00568     if (RetTy && RetTy->isIntegral()) {          // Nonvoid return type?
00569       ExitValue = Result;   // Capture the exit value of the program
00570     } else {
00571       memset(&ExitValue, 0, sizeof(ExitValue));
00572     }
00573   } else {
00574     // If we have a previous stack frame, and we have a previous call,
00575     // fill in the return value...
00576     ExecutionContext &CallingSF = ECStack.back();
00577     if (Instruction *I = CallingSF.Caller.getInstruction()) {
00578       if (CallingSF.Caller.getType() != Type::VoidTy)      // Save result...
00579         SetValue(I, Result, CallingSF);
00580       if (InvokeInst *II = dyn_cast<InvokeInst> (I))
00581         SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
00582       CallingSF.Caller = CallSite();          // We returned from the call...
00583     }
00584   }
00585 }
00586 
00587 void Interpreter::visitReturnInst(ReturnInst &I) {
00588   ExecutionContext &SF = ECStack.back();
00589   const Type *RetTy = Type::VoidTy;
00590   GenericValue Result;
00591 
00592   // Save away the return value... (if we are not 'ret void')
00593   if (I.getNumOperands()) {
00594     RetTy  = I.getReturnValue()->getType();
00595     Result = getOperandValue(I.getReturnValue(), SF);
00596   }
00597 
00598   popStackAndReturnValueToCaller(RetTy, Result);
00599 }
00600 
00601 void Interpreter::visitUnwindInst(UnwindInst &I) {
00602   // Unwind stack
00603   Instruction *Inst;
00604   do {
00605     ECStack.pop_back ();
00606     if (ECStack.empty ())
00607       abort ();
00608     Inst = ECStack.back ().Caller.getInstruction ();
00609   } while (!(Inst && isa<InvokeInst> (Inst)));
00610 
00611   // Return from invoke
00612   ExecutionContext &InvokingSF = ECStack.back ();
00613   InvokingSF.Caller = CallSite ();
00614 
00615   // Go to exceptional destination BB of invoke instruction
00616   SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
00617 }
00618 
00619 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
00620   std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
00621   abort();
00622 }
00623 
00624 void Interpreter::visitBranchInst(BranchInst &I) {
00625   ExecutionContext &SF = ECStack.back();
00626   BasicBlock *Dest;
00627 
00628   Dest = I.getSuccessor(0);          // Uncond branches have a fixed dest...
00629   if (!I.isUnconditional()) {
00630     Value *Cond = I.getCondition();
00631     if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
00632       Dest = I.getSuccessor(1);
00633   }
00634   SwitchToNewBasicBlock(Dest, SF);
00635 }
00636 
00637 void Interpreter::visitSwitchInst(SwitchInst &I) {
00638   ExecutionContext &SF = ECStack.back();
00639   GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
00640   const Type *ElTy = I.getOperand(0)->getType();
00641 
00642   // Check to see if any of the cases match...
00643   BasicBlock *Dest = 0;
00644   for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
00645     if (executeSetEQInst(CondVal,
00646                          getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
00647       Dest = cast<BasicBlock>(I.getOperand(i+1));
00648       break;
00649     }
00650 
00651   if (!Dest) Dest = I.getDefaultDest();   // No cases matched: use default
00652   SwitchToNewBasicBlock(Dest, SF);
00653 }
00654 
00655 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
00656 // This function handles the actual updating of block and instruction iterators
00657 // as well as execution of all of the PHI nodes in the destination block.
00658 //
00659 // This method does this because all of the PHI nodes must be executed
00660 // atomically, reading their inputs before any of the results are updated.  Not
00661 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
00662 // their inputs.  If the input PHI node is updated before it is read, incorrect
00663 // results can happen.  Thus we use a two phase approach.
00664 //
00665 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
00666   BasicBlock *PrevBB = SF.CurBB;      // Remember where we came from...
00667   SF.CurBB   = Dest;                  // Update CurBB to branch destination
00668   SF.CurInst = SF.CurBB->begin();     // Update new instruction ptr...
00669 
00670   if (!isa<PHINode>(SF.CurInst)) return;  // Nothing fancy to do
00671 
00672   // Loop over all of the PHI nodes in the current block, reading their inputs.
00673   std::vector<GenericValue> ResultValues;
00674 
00675   for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
00676     // Search for the value corresponding to this previous bb...
00677     int i = PN->getBasicBlockIndex(PrevBB);
00678     assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
00679     Value *IncomingValue = PN->getIncomingValue(i);
00680 
00681     // Save the incoming value for this PHI node...
00682     ResultValues.push_back(getOperandValue(IncomingValue, SF));
00683   }
00684 
00685   // Now loop over all of the PHI nodes setting their values...
00686   SF.CurInst = SF.CurBB->begin();
00687   for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
00688     PHINode *PN = cast<PHINode>(SF.CurInst);
00689     SetValue(PN, ResultValues[i], SF);
00690   }
00691 }
00692 
00693 //===----------------------------------------------------------------------===//
00694 //                     Memory Instruction Implementations
00695 //===----------------------------------------------------------------------===//
00696 
00697 void Interpreter::visitAllocationInst(AllocationInst &I) {
00698   ExecutionContext &SF = ECStack.back();
00699 
00700   const Type *Ty = I.getType()->getElementType();  // Type to be allocated
00701 
00702   // Get the number of elements being allocated by the array...
00703   unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
00704 
00705   // Allocate enough memory to hold the type...
00706   void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
00707 
00708   GenericValue Result = PTOGV(Memory);
00709   assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
00710   SetValue(&I, Result, SF);
00711 
00712   if (I.getOpcode() == Instruction::Alloca)
00713     ECStack.back().Allocas.add(Memory);
00714 }
00715 
00716 void Interpreter::visitFreeInst(FreeInst &I) {
00717   ExecutionContext &SF = ECStack.back();
00718   assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
00719   GenericValue Value = getOperandValue(I.getOperand(0), SF);
00720   // TODO: Check to make sure memory is allocated
00721   free(GVTOP(Value));   // Free memory
00722 }
00723 
00724 // getElementOffset - The workhorse for getelementptr.
00725 //
00726 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
00727                                               gep_type_iterator E,
00728                                               ExecutionContext &SF) {
00729   assert(isa<PointerType>(Ptr->getType()) &&
00730          "Cannot getElementOffset of a nonpointer type!");
00731 
00732   PointerTy Total = 0;
00733 
00734   for (; I != E; ++I) {
00735     if (const StructType *STy = dyn_cast<StructType>(*I)) {
00736       const StructLayout *SLO = TD.getStructLayout(STy);
00737 
00738       const ConstantUInt *CPU = cast<ConstantUInt>(I.getOperand());
00739       unsigned Index = unsigned(CPU->getValue());
00740 
00741       Total += (PointerTy)SLO->MemberOffsets[Index];
00742     } else {
00743       const SequentialType *ST = cast<SequentialType>(*I);
00744       // Get the index number for the array... which must be long type...
00745       GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
00746 
00747       uint64_t Idx;
00748       switch (I.getOperand()->getType()->getTypeID()) {
00749       default: assert(0 && "Illegal getelementptr index for sequential type!");
00750       case Type::SByteTyID:  Idx = IdxGV.SByteVal; break;
00751       case Type::ShortTyID:  Idx = IdxGV.ShortVal; break;
00752       case Type::IntTyID:    Idx = IdxGV.IntVal; break;
00753       case Type::LongTyID:   Idx = IdxGV.LongVal; break;
00754       case Type::UByteTyID:  Idx = IdxGV.UByteVal; break;
00755       case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
00756       case Type::UIntTyID:   Idx = IdxGV.UIntVal; break;
00757       case Type::ULongTyID:  Idx = IdxGV.ULongVal; break;
00758       }
00759       Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
00760     }
00761   }
00762 
00763   GenericValue Result;
00764   Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
00765   return Result;
00766 }
00767 
00768 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
00769   ExecutionContext &SF = ECStack.back();
00770   SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
00771                                    gep_type_begin(I), gep_type_end(I), SF), SF);
00772 }
00773 
00774 void Interpreter::visitLoadInst(LoadInst &I) {
00775   ExecutionContext &SF = ECStack.back();
00776   GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
00777   GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
00778   GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
00779   SetValue(&I, Result, SF);
00780 }
00781 
00782 void Interpreter::visitStoreInst(StoreInst &I) {
00783   ExecutionContext &SF = ECStack.back();
00784   GenericValue Val = getOperandValue(I.getOperand(0), SF);
00785   GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
00786   StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
00787                      I.getOperand(0)->getType());
00788 }
00789 
00790 //===----------------------------------------------------------------------===//
00791 //                 Miscellaneous Instruction Implementations
00792 //===----------------------------------------------------------------------===//
00793 
00794 void Interpreter::visitCallSite(CallSite CS) {
00795   ExecutionContext &SF = ECStack.back();
00796 
00797   // Check to see if this is an intrinsic function call...
00798   if (Function *F = CS.getCalledFunction())
00799    if (F->isExternal ())
00800     switch (F->getIntrinsicID()) {
00801     case Intrinsic::not_intrinsic:
00802       break;
00803     case Intrinsic::vastart: { // va_start
00804       GenericValue ArgIndex;
00805       ArgIndex.UIntPairVal.first = ECStack.size() - 1;
00806       ArgIndex.UIntPairVal.second = 0;
00807       SetValue(CS.getInstruction(), ArgIndex, SF);
00808       return;
00809     }
00810     case Intrinsic::vaend:    // va_end is a noop for the interpreter
00811       return;
00812     case Intrinsic::vacopy:   // va_copy: dest = src
00813       SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
00814       return;
00815     default:
00816       // If it is an unknown intrinsic function, use the intrinsic lowering
00817       // class to transform it into hopefully tasty LLVM code.
00818       //
00819       Instruction *Prev = CS.getInstruction()->getPrev();
00820       BasicBlock *Parent = CS.getInstruction()->getParent();
00821       IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
00822 
00823       // Restore the CurInst pointer to the first instruction newly inserted, if
00824       // any.
00825       if (!Prev) {
00826         SF.CurInst = Parent->begin();
00827       } else {
00828         SF.CurInst = Prev;
00829         ++SF.CurInst;
00830       }
00831       return;
00832     }
00833 
00834   SF.Caller = CS;
00835   std::vector<GenericValue> ArgVals;
00836   const unsigned NumArgs = SF.Caller.arg_size();
00837   ArgVals.reserve(NumArgs);
00838   for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
00839          e = SF.Caller.arg_end(); i != e; ++i) {
00840     Value *V = *i;
00841     ArgVals.push_back(getOperandValue(V, SF));
00842     // Promote all integral types whose size is < sizeof(int) into ints.  We do
00843     // this by zero or sign extending the value as appropriate according to the
00844     // source type.
00845     const Type *Ty = V->getType();
00846     if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
00847       if (Ty == Type::ShortTy)
00848         ArgVals.back().IntVal = ArgVals.back().ShortVal;
00849       else if (Ty == Type::UShortTy)
00850         ArgVals.back().UIntVal = ArgVals.back().UShortVal;
00851       else if (Ty == Type::SByteTy)
00852         ArgVals.back().IntVal = ArgVals.back().SByteVal;
00853       else if (Ty == Type::UByteTy)
00854         ArgVals.back().UIntVal = ArgVals.back().UByteVal;
00855       else if (Ty == Type::BoolTy)
00856         ArgVals.back().UIntVal = ArgVals.back().BoolVal;
00857       else
00858         assert(0 && "Unknown type!");
00859     }
00860   }
00861 
00862   // To handle indirect calls, we must get the pointer value from the argument
00863   // and treat it as a function pointer.
00864   GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
00865   callFunction((Function*)GVTOP(SRC), ArgVals);
00866 }
00867 
00868 #define IMPLEMENT_SHIFT(OP, TY) \
00869    case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
00870 
00871 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
00872                                    const Type *Ty) {
00873   GenericValue Dest;
00874   switch (Ty->getTypeID()) {
00875     IMPLEMENT_SHIFT(<<, UByte);
00876     IMPLEMENT_SHIFT(<<, SByte);
00877     IMPLEMENT_SHIFT(<<, UShort);
00878     IMPLEMENT_SHIFT(<<, Short);
00879     IMPLEMENT_SHIFT(<<, UInt);
00880     IMPLEMENT_SHIFT(<<, Int);
00881     IMPLEMENT_SHIFT(<<, ULong);
00882     IMPLEMENT_SHIFT(<<, Long);
00883   default:
00884     std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
00885   }
00886   return Dest;
00887 }
00888 
00889 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
00890                                    const Type *Ty) {
00891   GenericValue Dest;
00892   switch (Ty->getTypeID()) {
00893     IMPLEMENT_SHIFT(>>, UByte);
00894     IMPLEMENT_SHIFT(>>, SByte);
00895     IMPLEMENT_SHIFT(>>, UShort);
00896     IMPLEMENT_SHIFT(>>, Short);
00897     IMPLEMENT_SHIFT(>>, UInt);
00898     IMPLEMENT_SHIFT(>>, Int);
00899     IMPLEMENT_SHIFT(>>, ULong);
00900     IMPLEMENT_SHIFT(>>, Long);
00901   default:
00902     std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
00903     abort();
00904   }
00905   return Dest;
00906 }
00907 
00908 void Interpreter::visitShl(ShiftInst &I) {
00909   ExecutionContext &SF = ECStack.back();
00910   const Type *Ty    = I.getOperand(0)->getType();
00911   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
00912   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
00913   GenericValue Dest;
00914   Dest = executeShlInst (Src1, Src2, Ty);
00915   SetValue(&I, Dest, SF);
00916 }
00917 
00918 void Interpreter::visitShr(ShiftInst &I) {
00919   ExecutionContext &SF = ECStack.back();
00920   const Type *Ty    = I.getOperand(0)->getType();
00921   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
00922   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
00923   GenericValue Dest;
00924   Dest = executeShrInst (Src1, Src2, Ty);
00925   SetValue(&I, Dest, SF);
00926 }
00927 
00928 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
00929    case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
00930 
00931 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY)    \
00932   case Type::DESTTY##TyID:                      \
00933     switch (SrcTy->getTypeID()) {          \
00934       IMPLEMENT_CAST(DESTTY, DESTCTY, Bool);    \
00935       IMPLEMENT_CAST(DESTTY, DESTCTY, UByte);   \
00936       IMPLEMENT_CAST(DESTTY, DESTCTY, SByte);   \
00937       IMPLEMENT_CAST(DESTTY, DESTCTY, UShort);  \
00938       IMPLEMENT_CAST(DESTTY, DESTCTY, Short);   \
00939       IMPLEMENT_CAST(DESTTY, DESTCTY, UInt);    \
00940       IMPLEMENT_CAST(DESTTY, DESTCTY, Int);     \
00941       IMPLEMENT_CAST(DESTTY, DESTCTY, ULong);   \
00942       IMPLEMENT_CAST(DESTTY, DESTCTY, Long);    \
00943       IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
00944 
00945 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
00946       IMPLEMENT_CAST(DESTTY, DESTCTY, Float);   \
00947       IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
00948 
00949 #define IMPLEMENT_CAST_CASE_END()    \
00950     default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
00951       abort();                                  \
00952     }                                           \
00953     break
00954 
00955 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
00956    IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY);   \
00957    IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
00958    IMPLEMENT_CAST_CASE_END()
00959 
00960 GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
00961                                                ExecutionContext &SF) {
00962   const Type *SrcTy = SrcVal->getType();
00963   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
00964 
00965   switch (Ty->getTypeID()) {
00966     IMPLEMENT_CAST_CASE(UByte  , (unsigned char));
00967     IMPLEMENT_CAST_CASE(SByte  , (  signed char));
00968     IMPLEMENT_CAST_CASE(UShort , (unsigned short));
00969     IMPLEMENT_CAST_CASE(Short  , (  signed short));
00970     IMPLEMENT_CAST_CASE(UInt   , (unsigned int ));
00971     IMPLEMENT_CAST_CASE(Int    , (  signed int ));
00972     IMPLEMENT_CAST_CASE(ULong  , (uint64_t));
00973     IMPLEMENT_CAST_CASE(Long   , ( int64_t));
00974     IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
00975     IMPLEMENT_CAST_CASE(Float  , (float));
00976     IMPLEMENT_CAST_CASE(Double , (double));
00977     IMPLEMENT_CAST_CASE(Bool   , (bool));
00978   default:
00979     std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
00980     abort();
00981   }
00982 
00983   return Dest;
00984 }
00985 
00986 void Interpreter::visitCastInst(CastInst &I) {
00987   ExecutionContext &SF = ECStack.back();
00988   SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
00989 }
00990 
00991 #define IMPLEMENT_VAARG(TY) \
00992    case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
00993 
00994 void Interpreter::visitVAArgInst(VAArgInst &I) {
00995   ExecutionContext &SF = ECStack.back();
00996 
00997   // Get the incoming valist parameter.  LLI treats the valist as a
00998   // (ec-stack-depth var-arg-index) pair.
00999   GenericValue VAList = getOperandValue(I.getOperand(0), SF);
01000   GenericValue Dest;
01001   GenericValue Src = ECStack[VAList.UIntPairVal.first]
01002    .VarArgs[VAList.UIntPairVal.second];
01003   const Type *Ty = I.getType();
01004   switch (Ty->getTypeID()) {
01005     IMPLEMENT_VAARG(UByte);
01006     IMPLEMENT_VAARG(SByte);
01007     IMPLEMENT_VAARG(UShort);
01008     IMPLEMENT_VAARG(Short);
01009     IMPLEMENT_VAARG(UInt);
01010     IMPLEMENT_VAARG(Int);
01011     IMPLEMENT_VAARG(ULong);
01012     IMPLEMENT_VAARG(Long);
01013     IMPLEMENT_VAARG(Pointer);
01014     IMPLEMENT_VAARG(Float);
01015     IMPLEMENT_VAARG(Double);
01016     IMPLEMENT_VAARG(Bool);
01017   default:
01018     std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
01019     abort();
01020   }
01021 
01022   // Set the Value of this Instruction.
01023   SetValue(&I, Dest, SF);
01024 
01025   // Move the pointer to the next vararg.
01026   ++VAList.UIntPairVal.second;
01027 }
01028 
01029 //===----------------------------------------------------------------------===//
01030 //                        Dispatch and Execution Code
01031 //===----------------------------------------------------------------------===//
01032 
01033 //===----------------------------------------------------------------------===//
01034 // callFunction - Execute the specified function...
01035 //
01036 void Interpreter::callFunction(Function *F,
01037                                const std::vector<GenericValue> &ArgVals) {
01038   assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
01039           ECStack.back().Caller.arg_size() == ArgVals.size()) &&
01040          "Incorrect number of arguments passed into function call!");
01041   // Make a new stack frame... and fill it in.
01042   ECStack.push_back(ExecutionContext());
01043   ExecutionContext &StackFrame = ECStack.back();
01044   StackFrame.CurFunction = F;
01045 
01046   // Special handling for external functions.
01047   if (F->isExternal()) {
01048     GenericValue Result = callExternalFunction (F, ArgVals);
01049     // Simulate a 'ret' instruction of the appropriate type.
01050     popStackAndReturnValueToCaller (F->getReturnType (), Result);
01051     return;
01052   }
01053 
01054   // Get pointers to first LLVM BB & Instruction in function.
01055   StackFrame.CurBB     = F->begin();
01056   StackFrame.CurInst   = StackFrame.CurBB->begin();
01057 
01058   // Run through the function arguments and initialize their values...
01059   assert((ArgVals.size() == F->arg_size() ||
01060          (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
01061          "Invalid number of values passed to function invocation!");
01062 
01063   // Handle non-varargs arguments...
01064   unsigned i = 0;
01065   for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
01066     SetValue(AI, ArgVals[i], StackFrame);
01067 
01068   // Handle varargs arguments...
01069   StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
01070 }
01071 
01072 void Interpreter::run() {
01073   while (!ECStack.empty()) {
01074     // Interpret a single instruction & increment the "PC".
01075     ExecutionContext &SF = ECStack.back();  // Current stack frame
01076     Instruction &I = *SF.CurInst++;         // Increment before execute
01077 
01078     // Track the number of dynamic instructions executed.
01079     ++NumDynamicInsts;
01080 
01081     DEBUG(std::cerr << "About to interpret: " << I);
01082     visit(I);   // Dispatch to one of the visit* methods...
01083   }
01084 }