Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1 | //===-- Execution.cpp - Implement code to simulate the program ------------===// |
| 2 | // |
| 3 | // This file contains the actual instruction interpreter. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include "Interpreter.h" |
| 8 | #include "ExecutionAnnotations.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 9 | #include "llvm/iPHINode.h" |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 10 | #include "llvm/iOther.h" |
| 11 | #include "llvm/iTerminators.h" |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 12 | #include "llvm/iMemory.h" |
Chris Lattner | e2cbbce | 2002-04-29 18:56:45 +0000 | [diff] [blame] | 13 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 15 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 41c2e5c | 2001-09-28 22:56:43 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetData.h" |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 17 | #include "Support/CommandLine.h" |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 18 | #include <math.h> // For fmod |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 19 | #include <signal.h> |
| 20 | #include <setjmp.h> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 21 | using std::vector; |
| 22 | using std::cout; |
| 23 | using std::cerr; |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 25 | static cl::opt<bool> |
| 26 | QuietMode("quiet", cl::desc("Do not emit any non-program output")); |
| 27 | |
| 28 | static cl::alias |
| 29 | QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode)); |
| 30 | |
| 31 | static cl::opt<bool> |
| 32 | ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks")); |
| 33 | |
| 34 | static cl::opt<bool> |
| 35 | AbortOnExceptions("abort-on-exception", |
| 36 | cl::desc("Halt execution on a machine exception")); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 38 | // Create a TargetData structure to handle memory addressing and size/alignment |
| 39 | // computations |
| 40 | // |
| 41 | static TargetData TD("lli Interpreter"); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 42 | CachedWriter CW; // Object to accelerate printing of LLVM |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 43 | |
| 44 | |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 45 | #ifdef PROFILE_STRUCTURE_FIELDS |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 46 | static cl::opt<bool> |
| 47 | ProfileStructureFields("profilestructfields", |
| 48 | cl::desc("Profile Structure Field Accesses")); |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 49 | #include <map> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 50 | static std::map<const StructType *, vector<unsigned> > FieldAccessCounts; |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 53 | sigjmp_buf SignalRecoverBuffer; |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 54 | static bool InInstruction = false; |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 55 | |
| 56 | extern "C" { |
| 57 | static void SigHandler(int Signal) { |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 58 | if (InInstruction) |
| 59 | siglongjmp(SignalRecoverBuffer, Signal); |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | static void initializeSignalHandlers() { |
| 64 | struct sigaction Action; |
| 65 | Action.sa_handler = SigHandler; |
| 66 | Action.sa_flags = SA_SIGINFO; |
| 67 | sigemptyset(&Action.sa_mask); |
| 68 | sigaction(SIGSEGV, &Action, 0); |
| 69 | sigaction(SIGBUS, &Action, 0); |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 70 | sigaction(SIGINT, &Action, 0); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 71 | sigaction(SIGFPE, &Action, 0); |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 74 | |
| 75 | //===----------------------------------------------------------------------===// |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 76 | // Value Manipulation code |
| 77 | //===----------------------------------------------------------------------===// |
| 78 | |
| 79 | static unsigned getOperandSlot(Value *V) { |
| 80 | SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID); |
| 81 | assert(SN && "Operand does not have a slot number annotation!"); |
| 82 | return SN->SlotNum; |
| 83 | } |
| 84 | |
| 85 | #define GET_CONST_VAL(TY, CLASS) \ |
| 86 | case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break |
| 87 | |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 88 | // Operations used by constant expr implementations... |
| 89 | static GenericValue executeCastOperation(Value *Src, const Type *DestTy, |
| 90 | ExecutionContext &SF); |
| 91 | static GenericValue executeGEPOperation(Value *Src, User::op_iterator IdxBegin, |
| 92 | User::op_iterator IdxEnd, |
| 93 | ExecutionContext &SF); |
| 94 | static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2, |
| 95 | const Type *Ty, ExecutionContext &SF); |
| 96 | |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 97 | static GenericValue getOperandValue(Value *V, ExecutionContext &SF) { |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 98 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 99 | switch (CE->getOpcode()) { |
| 100 | case Instruction::Cast: |
| 101 | return executeCastOperation(CE->getOperand(0), CE->getType(), SF); |
| 102 | case Instruction::GetElementPtr: |
| 103 | return executeGEPOperation(CE->getOperand(0), CE->op_begin()+1, |
| 104 | CE->op_end(), SF); |
| 105 | case Instruction::Add: |
| 106 | return executeAddInst(getOperandValue(CE->getOperand(0), SF), |
| 107 | getOperandValue(CE->getOperand(1), SF), |
| 108 | CE->getType(), SF); |
| 109 | default: |
| 110 | cerr << "Unhandled ConstantExpr: " << CE << "\n"; |
| 111 | abort(); |
| 112 | { GenericValue V; return V; } |
| 113 | } |
| 114 | } else if (Constant *CPV = dyn_cast<Constant>(V)) { |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 115 | GenericValue Result; |
| 116 | switch (CPV->getType()->getPrimitiveID()) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 117 | GET_CONST_VAL(Bool , ConstantBool); |
| 118 | GET_CONST_VAL(UByte , ConstantUInt); |
| 119 | GET_CONST_VAL(SByte , ConstantSInt); |
| 120 | GET_CONST_VAL(UShort , ConstantUInt); |
| 121 | GET_CONST_VAL(Short , ConstantSInt); |
| 122 | GET_CONST_VAL(UInt , ConstantUInt); |
| 123 | GET_CONST_VAL(Int , ConstantSInt); |
| 124 | GET_CONST_VAL(ULong , ConstantUInt); |
| 125 | GET_CONST_VAL(Long , ConstantSInt); |
| 126 | GET_CONST_VAL(Float , ConstantFP); |
| 127 | GET_CONST_VAL(Double , ConstantFP); |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 128 | case Type::PointerTyID: |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 129 | if (isa<ConstantPointerNull>(CPV)) { |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 130 | Result.PointerVal = 0; |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 131 | } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) { |
| 132 | return getOperandValue(CPR->getValue(), SF); |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 133 | } else { |
| 134 | assert(0 && "Unknown constant pointer type!"); |
| 135 | } |
| 136 | break; |
| 137 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 138 | cout << "ERROR: Constant unimp for type: " << CPV->getType() << "\n"; |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 139 | } |
| 140 | return Result; |
| 141 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 142 | GlobalAddress *Address = |
| 143 | (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID); |
| 144 | GenericValue Result; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 145 | Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr; |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 146 | return Result; |
| 147 | } else { |
| 148 | unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 149 | unsigned OpSlot = getOperandSlot(V); |
| 150 | assert(TyP < SF.Values.size() && |
| 151 | OpSlot < SF.Values[TyP].size() && "Value out of range!"); |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 152 | return SF.Values[TyP][getOperandSlot(V)]; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static void printOperandInfo(Value *V, ExecutionContext &SF) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 157 | if (isa<Constant>(V)) { |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 158 | cout << "Constant Pool Value\n"; |
| 159 | } else if (isa<GlobalValue>(V)) { |
| 160 | cout << "Global Value\n"; |
| 161 | } else { |
| 162 | unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value |
| 163 | unsigned Slot = getOperandSlot(V); |
| 164 | cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 165 | << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF |
| 166 | << " Contents=0x"; |
| 167 | |
| 168 | const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot]; |
| 169 | for (unsigned i = 0; i < sizeof(GenericValue); ++i) { |
| 170 | unsigned char Cur = Buf[i]; |
| 171 | cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0')) |
| 172 | << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0')); |
| 173 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 174 | cout << "\n"; |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) { |
| 181 | unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value |
| 182 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 183 | //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << "\n"; |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 184 | SF.Values[TyP][getOperandSlot(V)] = Val; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 189 | // Annotation Wrangling code |
| 190 | //===----------------------------------------------------------------------===// |
| 191 | |
| 192 | void Interpreter::initializeExecutionEngine() { |
| 193 | AnnotationManager::registerAnnotationFactory(MethodInfoAID, |
| 194 | &MethodInfo::Create); |
| 195 | AnnotationManager::registerAnnotationFactory(GlobalAddressAID, |
| 196 | &GlobalAddress::Create); |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 197 | initializeSignalHandlers(); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 200 | // InitializeMemory - Recursive function to apply a Constant value into the |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 201 | // specified memory location... |
| 202 | // |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 203 | static void InitializeMemory(const Constant *Init, char *Addr) { |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 204 | #define INITIALIZE_MEMORY(TYID, CLASS, TY) \ |
| 205 | case Type::TYID##TyID: { \ |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 206 | TY Tmp = cast<CLASS>(Init)->getValue(); \ |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 207 | memcpy(Addr, &Tmp, sizeof(TY)); \ |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 208 | } return |
| 209 | |
| 210 | switch (Init->getType()->getPrimitiveID()) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 211 | INITIALIZE_MEMORY(Bool , ConstantBool, bool); |
| 212 | INITIALIZE_MEMORY(UByte , ConstantUInt, unsigned char); |
| 213 | INITIALIZE_MEMORY(SByte , ConstantSInt, signed char); |
| 214 | INITIALIZE_MEMORY(UShort , ConstantUInt, unsigned short); |
| 215 | INITIALIZE_MEMORY(Short , ConstantSInt, signed short); |
| 216 | INITIALIZE_MEMORY(UInt , ConstantUInt, unsigned int); |
| 217 | INITIALIZE_MEMORY(Int , ConstantSInt, signed int); |
| 218 | INITIALIZE_MEMORY(ULong , ConstantUInt, uint64_t); |
| 219 | INITIALIZE_MEMORY(Long , ConstantSInt, int64_t); |
| 220 | INITIALIZE_MEMORY(Float , ConstantFP , float); |
| 221 | INITIALIZE_MEMORY(Double , ConstantFP , double); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 222 | #undef INITIALIZE_MEMORY |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 223 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 224 | case Type::ArrayTyID: { |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 225 | const ConstantArray *CPA = cast<ConstantArray>(Init); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 226 | const vector<Use> &Val = CPA->getValues(); |
| 227 | unsigned ElementSize = |
| 228 | TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType()); |
| 229 | for (unsigned i = 0; i < Val.size(); ++i) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 230 | InitializeMemory(cast<Constant>(Val[i].get()), Addr+i*ElementSize); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 231 | return; |
| 232 | } |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 233 | |
| 234 | case Type::StructTyID: { |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 235 | const ConstantStruct *CPS = cast<ConstantStruct>(Init); |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 236 | const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType())); |
| 237 | const vector<Use> &Val = CPS->getValues(); |
| 238 | for (unsigned i = 0; i < Val.size(); ++i) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 239 | InitializeMemory(cast<Constant>(Val[i].get()), |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 240 | Addr+SL->MemberOffsets[i]); |
| 241 | return; |
| 242 | } |
| 243 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 244 | case Type::PointerTyID: |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 245 | if (isa<ConstantPointerNull>(Init)) { |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 246 | *(void**)Addr = 0; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 247 | } else if (const ConstantPointerRef *CPR = |
| 248 | dyn_cast<ConstantPointerRef>(Init)) { |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 249 | GlobalAddress *Address = |
| 250 | (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID); |
| 251 | *(void**)Addr = (GenericValue*)Address->Ptr; |
| 252 | } else { |
| 253 | assert(0 && "Unknown Constant pointer type!"); |
| 254 | } |
| 255 | return; |
| 256 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 257 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 258 | CW << "Bad Type: " << Init->getType() << "\n"; |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 259 | assert(0 && "Unknown constant type to initialize memory with!"); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){ |
| 264 | assert(AID == GlobalAddressAID); |
| 265 | |
| 266 | // This annotation will only be created on GlobalValue objects... |
| 267 | GlobalValue *GVal = cast<GlobalValue>((Value*)O); |
| 268 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 269 | if (isa<Function>(GVal)) { |
| 270 | // The GlobalAddress object for a function is just a pointer to function |
| 271 | // itself. Don't delete it when the annotation is gone though! |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 272 | return new GlobalAddress(GVal, false); |
| 273 | } |
| 274 | |
| 275 | // Handle the case of a global variable... |
| 276 | assert(isa<GlobalVariable>(GVal) && |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 277 | "Global value found that isn't a function or global variable!"); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 278 | GlobalVariable *GV = cast<GlobalVariable>(GVal); |
| 279 | |
| 280 | // First off, we must allocate space for the global variable to point at... |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 281 | const Type *Ty = GV->getType()->getElementType(); // Type to be allocated |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 282 | |
| 283 | // Allocate enough memory to hold the type... |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 284 | void *Addr = calloc(1, TD.getTypeSize(Ty)); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 285 | assert(Addr != 0 && "Null pointer returned by malloc!"); |
| 286 | |
| 287 | // Initialize the memory if there is an initializer... |
| 288 | if (GV->hasInitializer()) |
| 289 | InitializeMemory(GV->getInitializer(), (char*)Addr); |
| 290 | |
| 291 | return new GlobalAddress(Addr, true); // Simply invoke the ctor |
| 292 | } |
| 293 | |
Chris Lattner | 2adcd83 | 2002-05-03 19:52:30 +0000 | [diff] [blame] | 294 | //===----------------------------------------------------------------------===// |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 295 | // Binary Instruction Implementations |
| 296 | //===----------------------------------------------------------------------===// |
| 297 | |
| 298 | #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \ |
| 299 | case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break |
| 300 | |
| 301 | static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2, |
| 302 | const Type *Ty, ExecutionContext &SF) { |
| 303 | GenericValue Dest; |
| 304 | switch (Ty->getPrimitiveID()) { |
| 305 | IMPLEMENT_BINARY_OPERATOR(+, UByte); |
| 306 | IMPLEMENT_BINARY_OPERATOR(+, SByte); |
| 307 | IMPLEMENT_BINARY_OPERATOR(+, UShort); |
| 308 | IMPLEMENT_BINARY_OPERATOR(+, Short); |
| 309 | IMPLEMENT_BINARY_OPERATOR(+, UInt); |
| 310 | IMPLEMENT_BINARY_OPERATOR(+, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 311 | IMPLEMENT_BINARY_OPERATOR(+, ULong); |
| 312 | IMPLEMENT_BINARY_OPERATOR(+, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 313 | IMPLEMENT_BINARY_OPERATOR(+, Float); |
| 314 | IMPLEMENT_BINARY_OPERATOR(+, Double); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 315 | IMPLEMENT_BINARY_OPERATOR(+, Pointer); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 316 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 317 | cout << "Unhandled type for Add instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 318 | } |
| 319 | return Dest; |
| 320 | } |
| 321 | |
| 322 | static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2, |
| 323 | const Type *Ty, ExecutionContext &SF) { |
| 324 | GenericValue Dest; |
| 325 | switch (Ty->getPrimitiveID()) { |
| 326 | IMPLEMENT_BINARY_OPERATOR(-, UByte); |
| 327 | IMPLEMENT_BINARY_OPERATOR(-, SByte); |
| 328 | IMPLEMENT_BINARY_OPERATOR(-, UShort); |
| 329 | IMPLEMENT_BINARY_OPERATOR(-, Short); |
| 330 | IMPLEMENT_BINARY_OPERATOR(-, UInt); |
| 331 | IMPLEMENT_BINARY_OPERATOR(-, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 332 | IMPLEMENT_BINARY_OPERATOR(-, ULong); |
| 333 | IMPLEMENT_BINARY_OPERATOR(-, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 334 | IMPLEMENT_BINARY_OPERATOR(-, Float); |
| 335 | IMPLEMENT_BINARY_OPERATOR(-, Double); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 336 | IMPLEMENT_BINARY_OPERATOR(-, Pointer); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 337 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 338 | cout << "Unhandled type for Sub instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 339 | } |
| 340 | return Dest; |
| 341 | } |
| 342 | |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 343 | static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2, |
| 344 | const Type *Ty, ExecutionContext &SF) { |
| 345 | GenericValue Dest; |
| 346 | switch (Ty->getPrimitiveID()) { |
| 347 | IMPLEMENT_BINARY_OPERATOR(*, UByte); |
| 348 | IMPLEMENT_BINARY_OPERATOR(*, SByte); |
| 349 | IMPLEMENT_BINARY_OPERATOR(*, UShort); |
| 350 | IMPLEMENT_BINARY_OPERATOR(*, Short); |
| 351 | IMPLEMENT_BINARY_OPERATOR(*, UInt); |
| 352 | IMPLEMENT_BINARY_OPERATOR(*, Int); |
| 353 | IMPLEMENT_BINARY_OPERATOR(*, ULong); |
| 354 | IMPLEMENT_BINARY_OPERATOR(*, Long); |
| 355 | IMPLEMENT_BINARY_OPERATOR(*, Float); |
| 356 | IMPLEMENT_BINARY_OPERATOR(*, Double); |
| 357 | IMPLEMENT_BINARY_OPERATOR(*, Pointer); |
| 358 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 359 | cout << "Unhandled type for Mul instruction: " << Ty << "\n"; |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 360 | } |
| 361 | return Dest; |
| 362 | } |
| 363 | |
| 364 | static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2, |
| 365 | const Type *Ty, ExecutionContext &SF) { |
| 366 | GenericValue Dest; |
| 367 | switch (Ty->getPrimitiveID()) { |
| 368 | IMPLEMENT_BINARY_OPERATOR(/, UByte); |
| 369 | IMPLEMENT_BINARY_OPERATOR(/, SByte); |
| 370 | IMPLEMENT_BINARY_OPERATOR(/, UShort); |
| 371 | IMPLEMENT_BINARY_OPERATOR(/, Short); |
| 372 | IMPLEMENT_BINARY_OPERATOR(/, UInt); |
| 373 | IMPLEMENT_BINARY_OPERATOR(/, Int); |
| 374 | IMPLEMENT_BINARY_OPERATOR(/, ULong); |
| 375 | IMPLEMENT_BINARY_OPERATOR(/, Long); |
| 376 | IMPLEMENT_BINARY_OPERATOR(/, Float); |
| 377 | IMPLEMENT_BINARY_OPERATOR(/, Double); |
| 378 | IMPLEMENT_BINARY_OPERATOR(/, Pointer); |
| 379 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 380 | cout << "Unhandled type for Div instruction: " << Ty << "\n"; |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 381 | } |
| 382 | return Dest; |
| 383 | } |
| 384 | |
| 385 | static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2, |
| 386 | const Type *Ty, ExecutionContext &SF) { |
| 387 | GenericValue Dest; |
| 388 | switch (Ty->getPrimitiveID()) { |
| 389 | IMPLEMENT_BINARY_OPERATOR(%, UByte); |
| 390 | IMPLEMENT_BINARY_OPERATOR(%, SByte); |
| 391 | IMPLEMENT_BINARY_OPERATOR(%, UShort); |
| 392 | IMPLEMENT_BINARY_OPERATOR(%, Short); |
| 393 | IMPLEMENT_BINARY_OPERATOR(%, UInt); |
| 394 | IMPLEMENT_BINARY_OPERATOR(%, Int); |
| 395 | IMPLEMENT_BINARY_OPERATOR(%, ULong); |
| 396 | IMPLEMENT_BINARY_OPERATOR(%, Long); |
| 397 | IMPLEMENT_BINARY_OPERATOR(%, Pointer); |
| 398 | case Type::FloatTyID: |
| 399 | Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal); |
| 400 | break; |
| 401 | case Type::DoubleTyID: |
| 402 | Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal); |
| 403 | break; |
| 404 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 405 | cout << "Unhandled type for Rem instruction: " << Ty << "\n"; |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 406 | } |
| 407 | return Dest; |
| 408 | } |
| 409 | |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 410 | static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 411 | const Type *Ty, ExecutionContext &SF) { |
| 412 | GenericValue Dest; |
| 413 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 414 | IMPLEMENT_BINARY_OPERATOR(&, UByte); |
| 415 | IMPLEMENT_BINARY_OPERATOR(&, SByte); |
| 416 | IMPLEMENT_BINARY_OPERATOR(&, UShort); |
| 417 | IMPLEMENT_BINARY_OPERATOR(&, Short); |
| 418 | IMPLEMENT_BINARY_OPERATOR(&, UInt); |
| 419 | IMPLEMENT_BINARY_OPERATOR(&, Int); |
| 420 | IMPLEMENT_BINARY_OPERATOR(&, ULong); |
| 421 | IMPLEMENT_BINARY_OPERATOR(&, Long); |
| 422 | IMPLEMENT_BINARY_OPERATOR(&, Pointer); |
| 423 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 424 | cout << "Unhandled type for And instruction: " << Ty << "\n"; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 425 | } |
| 426 | return Dest; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2, |
| 431 | const Type *Ty, ExecutionContext &SF) { |
| 432 | GenericValue Dest; |
| 433 | switch (Ty->getPrimitiveID()) { |
| 434 | IMPLEMENT_BINARY_OPERATOR(|, UByte); |
| 435 | IMPLEMENT_BINARY_OPERATOR(|, SByte); |
| 436 | IMPLEMENT_BINARY_OPERATOR(|, UShort); |
| 437 | IMPLEMENT_BINARY_OPERATOR(|, Short); |
| 438 | IMPLEMENT_BINARY_OPERATOR(|, UInt); |
| 439 | IMPLEMENT_BINARY_OPERATOR(|, Int); |
| 440 | IMPLEMENT_BINARY_OPERATOR(|, ULong); |
| 441 | IMPLEMENT_BINARY_OPERATOR(|, Long); |
| 442 | IMPLEMENT_BINARY_OPERATOR(|, Pointer); |
| 443 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 444 | cout << "Unhandled type for Or instruction: " << Ty << "\n"; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 445 | } |
| 446 | return Dest; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2, |
| 451 | const Type *Ty, ExecutionContext &SF) { |
| 452 | GenericValue Dest; |
| 453 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 454 | IMPLEMENT_BINARY_OPERATOR(^, UByte); |
| 455 | IMPLEMENT_BINARY_OPERATOR(^, SByte); |
| 456 | IMPLEMENT_BINARY_OPERATOR(^, UShort); |
| 457 | IMPLEMENT_BINARY_OPERATOR(^, Short); |
| 458 | IMPLEMENT_BINARY_OPERATOR(^, UInt); |
| 459 | IMPLEMENT_BINARY_OPERATOR(^, Int); |
| 460 | IMPLEMENT_BINARY_OPERATOR(^, ULong); |
| 461 | IMPLEMENT_BINARY_OPERATOR(^, Long); |
| 462 | IMPLEMENT_BINARY_OPERATOR(^, Pointer); |
| 463 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 464 | cout << "Unhandled type for Xor instruction: " << Ty << "\n"; |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 465 | } |
| 466 | return Dest; |
| 467 | } |
| 468 | |
| 469 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 470 | #define IMPLEMENT_SETCC(OP, TY) \ |
| 471 | case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break |
| 472 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 473 | static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2, |
| 474 | const Type *Ty, ExecutionContext &SF) { |
| 475 | GenericValue Dest; |
| 476 | switch (Ty->getPrimitiveID()) { |
| 477 | IMPLEMENT_SETCC(==, UByte); |
| 478 | IMPLEMENT_SETCC(==, SByte); |
| 479 | IMPLEMENT_SETCC(==, UShort); |
| 480 | IMPLEMENT_SETCC(==, Short); |
| 481 | IMPLEMENT_SETCC(==, UInt); |
| 482 | IMPLEMENT_SETCC(==, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 483 | IMPLEMENT_SETCC(==, ULong); |
| 484 | IMPLEMENT_SETCC(==, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 485 | IMPLEMENT_SETCC(==, Float); |
| 486 | IMPLEMENT_SETCC(==, Double); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 487 | IMPLEMENT_SETCC(==, Pointer); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 488 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 489 | cout << "Unhandled type for SetEQ instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 490 | } |
| 491 | return Dest; |
| 492 | } |
| 493 | |
| 494 | static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2, |
| 495 | const Type *Ty, ExecutionContext &SF) { |
| 496 | GenericValue Dest; |
| 497 | switch (Ty->getPrimitiveID()) { |
| 498 | IMPLEMENT_SETCC(!=, UByte); |
| 499 | IMPLEMENT_SETCC(!=, SByte); |
| 500 | IMPLEMENT_SETCC(!=, UShort); |
| 501 | IMPLEMENT_SETCC(!=, Short); |
| 502 | IMPLEMENT_SETCC(!=, UInt); |
| 503 | IMPLEMENT_SETCC(!=, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 504 | IMPLEMENT_SETCC(!=, ULong); |
| 505 | IMPLEMENT_SETCC(!=, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 506 | IMPLEMENT_SETCC(!=, Float); |
| 507 | IMPLEMENT_SETCC(!=, Double); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 508 | IMPLEMENT_SETCC(!=, Pointer); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 509 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 510 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 511 | cout << "Unhandled type for SetNE instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 512 | } |
| 513 | return Dest; |
| 514 | } |
| 515 | |
| 516 | static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2, |
| 517 | const Type *Ty, ExecutionContext &SF) { |
| 518 | GenericValue Dest; |
| 519 | switch (Ty->getPrimitiveID()) { |
| 520 | IMPLEMENT_SETCC(<=, UByte); |
| 521 | IMPLEMENT_SETCC(<=, SByte); |
| 522 | IMPLEMENT_SETCC(<=, UShort); |
| 523 | IMPLEMENT_SETCC(<=, Short); |
| 524 | IMPLEMENT_SETCC(<=, UInt); |
| 525 | IMPLEMENT_SETCC(<=, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 526 | IMPLEMENT_SETCC(<=, ULong); |
| 527 | IMPLEMENT_SETCC(<=, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 528 | IMPLEMENT_SETCC(<=, Float); |
| 529 | IMPLEMENT_SETCC(<=, Double); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 530 | IMPLEMENT_SETCC(<=, Pointer); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 531 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 532 | cout << "Unhandled type for SetLE instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 533 | } |
| 534 | return Dest; |
| 535 | } |
| 536 | |
| 537 | static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2, |
| 538 | const Type *Ty, ExecutionContext &SF) { |
| 539 | GenericValue Dest; |
| 540 | switch (Ty->getPrimitiveID()) { |
| 541 | IMPLEMENT_SETCC(>=, UByte); |
| 542 | IMPLEMENT_SETCC(>=, SByte); |
| 543 | IMPLEMENT_SETCC(>=, UShort); |
| 544 | IMPLEMENT_SETCC(>=, Short); |
| 545 | IMPLEMENT_SETCC(>=, UInt); |
| 546 | IMPLEMENT_SETCC(>=, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 547 | IMPLEMENT_SETCC(>=, ULong); |
| 548 | IMPLEMENT_SETCC(>=, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 549 | IMPLEMENT_SETCC(>=, Float); |
| 550 | IMPLEMENT_SETCC(>=, Double); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 551 | IMPLEMENT_SETCC(>=, Pointer); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 552 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 553 | cout << "Unhandled type for SetGE instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 554 | } |
| 555 | return Dest; |
| 556 | } |
| 557 | |
| 558 | static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2, |
| 559 | const Type *Ty, ExecutionContext &SF) { |
| 560 | GenericValue Dest; |
| 561 | switch (Ty->getPrimitiveID()) { |
| 562 | IMPLEMENT_SETCC(<, UByte); |
| 563 | IMPLEMENT_SETCC(<, SByte); |
| 564 | IMPLEMENT_SETCC(<, UShort); |
| 565 | IMPLEMENT_SETCC(<, Short); |
| 566 | IMPLEMENT_SETCC(<, UInt); |
| 567 | IMPLEMENT_SETCC(<, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 568 | IMPLEMENT_SETCC(<, ULong); |
| 569 | IMPLEMENT_SETCC(<, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 570 | IMPLEMENT_SETCC(<, Float); |
| 571 | IMPLEMENT_SETCC(<, Double); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 572 | IMPLEMENT_SETCC(<, Pointer); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 573 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 574 | cout << "Unhandled type for SetLT instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 575 | } |
| 576 | return Dest; |
| 577 | } |
| 578 | |
| 579 | static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2, |
| 580 | const Type *Ty, ExecutionContext &SF) { |
| 581 | GenericValue Dest; |
| 582 | switch (Ty->getPrimitiveID()) { |
| 583 | IMPLEMENT_SETCC(>, UByte); |
| 584 | IMPLEMENT_SETCC(>, SByte); |
| 585 | IMPLEMENT_SETCC(>, UShort); |
| 586 | IMPLEMENT_SETCC(>, Short); |
| 587 | IMPLEMENT_SETCC(>, UInt); |
| 588 | IMPLEMENT_SETCC(>, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 589 | IMPLEMENT_SETCC(>, ULong); |
| 590 | IMPLEMENT_SETCC(>, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 591 | IMPLEMENT_SETCC(>, Float); |
| 592 | IMPLEMENT_SETCC(>, Double); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 593 | IMPLEMENT_SETCC(>, Pointer); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 594 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 595 | cout << "Unhandled type for SetGT instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 596 | } |
| 597 | return Dest; |
| 598 | } |
| 599 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 600 | static void executeBinaryInst(BinaryOperator &I, ExecutionContext &SF) { |
| 601 | const Type *Ty = I.getOperand(0)->getType(); |
| 602 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 603 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 604 | GenericValue R; // Result |
| 605 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 606 | switch (I.getOpcode()) { |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 607 | case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break; |
| 608 | case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break; |
| 609 | case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break; |
| 610 | case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break; |
| 611 | case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 612 | case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break; |
| 613 | case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break; |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 614 | case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 615 | case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break; |
| 616 | case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break; |
| 617 | case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break; |
| 618 | case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break; |
| 619 | case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break; |
| 620 | case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break; |
| 621 | default: |
| 622 | cout << "Don't know how to handle this binary operator!\n-->" << I; |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 623 | R = Src1; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 626 | SetValue(&I, R, SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 629 | //===----------------------------------------------------------------------===// |
| 630 | // Terminator Instruction Implementations |
| 631 | //===----------------------------------------------------------------------===// |
| 632 | |
Chris Lattner | a95c699 | 2001-11-12 16:28:48 +0000 | [diff] [blame] | 633 | static void PerformExitStuff() { |
| 634 | #ifdef PROFILE_STRUCTURE_FIELDS |
| 635 | // Print out structure field accounting information... |
| 636 | if (!FieldAccessCounts.empty()) { |
Chris Lattner | 84efe09 | 2001-11-12 20:13:14 +0000 | [diff] [blame] | 637 | CW << "Profile Field Access Counts:\n"; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 638 | std::map<const StructType *, vector<unsigned> >::iterator |
Chris Lattner | a95c699 | 2001-11-12 16:28:48 +0000 | [diff] [blame] | 639 | I = FieldAccessCounts.begin(), E = FieldAccessCounts.end(); |
| 640 | for (; I != E; ++I) { |
| 641 | vector<unsigned> &OfC = I->second; |
| 642 | CW << " '" << (Value*)I->first << "'\t- Sum="; |
| 643 | |
| 644 | unsigned Sum = 0; |
| 645 | for (unsigned i = 0; i < OfC.size(); ++i) |
| 646 | Sum += OfC[i]; |
| 647 | CW << Sum << " - "; |
| 648 | |
| 649 | for (unsigned i = 0; i < OfC.size(); ++i) { |
| 650 | if (i) CW << ", "; |
| 651 | CW << OfC[i]; |
| 652 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 653 | CW << "\n"; |
Chris Lattner | a95c699 | 2001-11-12 16:28:48 +0000 | [diff] [blame] | 654 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 655 | CW << "\n"; |
Chris Lattner | 84efe09 | 2001-11-12 20:13:14 +0000 | [diff] [blame] | 656 | |
| 657 | CW << "Profile Field Access Percentages:\n"; |
| 658 | cout.precision(3); |
| 659 | for (I = FieldAccessCounts.begin(); I != E; ++I) { |
| 660 | vector<unsigned> &OfC = I->second; |
| 661 | unsigned Sum = 0; |
| 662 | for (unsigned i = 0; i < OfC.size(); ++i) |
| 663 | Sum += OfC[i]; |
| 664 | |
| 665 | CW << " '" << (Value*)I->first << "'\t- "; |
| 666 | for (unsigned i = 0; i < OfC.size(); ++i) { |
| 667 | if (i) CW << ", "; |
| 668 | CW << double(OfC[i])/Sum; |
| 669 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 670 | CW << "\n"; |
Chris Lattner | 84efe09 | 2001-11-12 20:13:14 +0000 | [diff] [blame] | 671 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 672 | CW << "\n"; |
Chris Lattner | 84efe09 | 2001-11-12 20:13:14 +0000 | [diff] [blame] | 673 | |
Chris Lattner | a95c699 | 2001-11-12 16:28:48 +0000 | [diff] [blame] | 674 | FieldAccessCounts.clear(); |
| 675 | } |
| 676 | #endif |
| 677 | } |
| 678 | |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 679 | void Interpreter::exitCalled(GenericValue GV) { |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 680 | if (!QuietMode) { |
| 681 | cout << "Program returned "; |
| 682 | print(Type::IntTy, GV); |
| 683 | cout << " via 'void exit(int)'\n"; |
| 684 | } |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 685 | |
| 686 | ExitCode = GV.SByteVal; |
| 687 | ECStack.clear(); |
Chris Lattner | a95c699 | 2001-11-12 16:28:48 +0000 | [diff] [blame] | 688 | PerformExitStuff(); |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 691 | void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 692 | const Type *RetTy = 0; |
| 693 | GenericValue Result; |
| 694 | |
| 695 | // Save away the return value... (if we are not 'ret void') |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 696 | if (I.getNumOperands()) { |
| 697 | RetTy = I.getReturnValue()->getType(); |
| 698 | Result = getOperandValue(I.getReturnValue(), SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | // Save previously executing meth |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 702 | const Function *M = ECStack.back().CurMethod; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 703 | |
| 704 | // Pop the current stack frame... this invalidates SF |
| 705 | ECStack.pop_back(); |
| 706 | |
| 707 | if (ECStack.empty()) { // Finished main. Put result into exit code... |
| 708 | if (RetTy) { // Nonvoid return type? |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 709 | if (!QuietMode) { |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 710 | CW << "Function " << M->getType() << " \"" << M->getName() |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 711 | << "\" returned "; |
| 712 | print(RetTy, Result); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 713 | cout << "\n"; |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 714 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 715 | |
| 716 | if (RetTy->isIntegral()) |
Chris Lattner | f4dca80 | 2002-05-02 19:28:45 +0000 | [diff] [blame] | 717 | ExitCode = Result.IntVal; // Capture the exit code of the program |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 718 | } else { |
| 719 | ExitCode = 0; |
| 720 | } |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 721 | |
Chris Lattner | a95c699 | 2001-11-12 16:28:48 +0000 | [diff] [blame] | 722 | PerformExitStuff(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 723 | return; |
| 724 | } |
| 725 | |
| 726 | // If we have a previous stack frame, and we have a previous call, fill in |
| 727 | // the return value... |
| 728 | // |
| 729 | ExecutionContext &NewSF = ECStack.back(); |
| 730 | if (NewSF.Caller) { |
| 731 | if (NewSF.Caller->getType() != Type::VoidTy) // Save result... |
| 732 | SetValue(NewSF.Caller, Result, NewSF); |
| 733 | |
| 734 | NewSF.Caller = 0; // We returned from the call... |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 735 | } else if (!QuietMode) { |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 736 | // This must be a function that is executing because of a user 'call' |
| 737 | // instruction. |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 738 | CW << "Function " << M->getType() << " \"" << M->getName() |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 739 | << "\" returned "; |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 740 | print(RetTy, Result); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 741 | cout << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 745 | void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 746 | SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work... |
| 747 | BasicBlock *Dest; |
| 748 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 749 | Dest = I.getSuccessor(0); // Uncond branches have a fixed dest... |
| 750 | if (!I.isUnconditional()) { |
| 751 | Value *Cond = I.getCondition(); |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 752 | GenericValue CondVal = getOperandValue(Cond, SF); |
| 753 | if (CondVal.BoolVal == 0) // If false cond... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 754 | Dest = I.getSuccessor(1); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 755 | } |
| 756 | SF.CurBB = Dest; // Update CurBB to branch destination |
| 757 | SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr... |
| 758 | } |
| 759 | |
| 760 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 761 | // Memory Instruction Implementations |
| 762 | //===----------------------------------------------------------------------===// |
| 763 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 764 | void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) { |
| 765 | const Type *Ty = I.getType()->getElementType(); // Type to be allocated |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 766 | |
Chris Lattner | cc82cc1 | 2002-04-28 21:57:33 +0000 | [diff] [blame] | 767 | // Get the number of elements being allocated by the array... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 768 | unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 769 | |
| 770 | // Allocate enough memory to hold the type... |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 771 | // FIXME: Don't use CALLOC, use a tainted malloc. |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 772 | void *Memory = calloc(NumElements, TD.getTypeSize(Ty)); |
| 773 | |
| 774 | GenericValue Result; |
| 775 | Result.PointerVal = (PointerTy)Memory; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 776 | assert(Result.PointerVal != 0 && "Null pointer returned by malloc!"); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 777 | SetValue(&I, Result, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 778 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 779 | if (I.getOpcode() == Instruction::Alloca) |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 780 | ECStack.back().Allocas.add(Memory); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 783 | static void executeFreeInst(FreeInst &I, ExecutionContext &SF) { |
| 784 | assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?"); |
| 785 | GenericValue Value = getOperandValue(I.getOperand(0), SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 786 | // TODO: Check to make sure memory is allocated |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 787 | free((void*)Value.PointerVal); // Free memory |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 790 | |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 791 | // getElementOffset - The workhorse for getelementptr. |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 792 | // |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 793 | static GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I, |
| 794 | User::op_iterator E, |
| 795 | ExecutionContext &SF) { |
| 796 | assert(isa<PointerType>(Ptr->getType()) && |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 797 | "Cannot getElementOffset of a nonpointer type!"); |
| 798 | |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 799 | PointerTy Total = 0; |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 800 | const Type *Ty = Ptr->getType(); |
| 801 | |
| 802 | for (; I != E; ++I) { |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 803 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 804 | const StructLayout *SLO = TD.getStructLayout(STy); |
| 805 | |
| 806 | // Indicies must be ubyte constants... |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 807 | const ConstantUInt *CPU = cast<ConstantUInt>(*I); |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 808 | assert(CPU->getType() == Type::UByteTy); |
| 809 | unsigned Index = CPU->getValue(); |
| 810 | |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 811 | #ifdef PROFILE_STRUCTURE_FIELDS |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 812 | if (ProfileStructureFields) { |
| 813 | // Do accounting for this field... |
| 814 | vector<unsigned> &OfC = FieldAccessCounts[STy]; |
| 815 | if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size()); |
| 816 | OfC[Index]++; |
| 817 | } |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 818 | #endif |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 819 | |
| 820 | Total += SLO->MemberOffsets[Index]; |
| 821 | Ty = STy->getElementTypes()[Index]; |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 822 | } else if (const SequentialType *ST = cast<SequentialType>(Ty)) { |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 823 | |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 824 | // Get the index number for the array... which must be uint type... |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 825 | assert((*I)->getType() == Type::UIntTy); |
| 826 | unsigned Idx = getOperandValue(*I, SF).UIntVal; |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 827 | if (const ArrayType *AT = dyn_cast<ArrayType>(ST)) |
Chris Lattner | c0fbd57 | 2002-02-11 20:19:16 +0000 | [diff] [blame] | 828 | if (Idx >= AT->getNumElements() && ArrayChecksEnabled) { |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 829 | cerr << "Out of range memory access to element #" << Idx |
| 830 | << " of a " << AT->getNumElements() << " element array." |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 831 | << " Subscript #" << *I << "\n"; |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 832 | // Get outta here!!! |
Chris Lattner | 7403025 | 2002-02-12 15:47:23 +0000 | [diff] [blame] | 833 | siglongjmp(SignalRecoverBuffer, SIGTRAP); |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 834 | } |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 835 | |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 836 | Ty = ST->getElementType(); |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 837 | unsigned Size = TD.getTypeSize(Ty); |
| 838 | Total += Size*Idx; |
| 839 | } |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 842 | GenericValue Result; |
| 843 | Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total; |
| 844 | return Result; |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 847 | static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) { |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 848 | SetValue(&I, executeGEPOperation(I.getPointerOperand(), |
| 849 | I.idx_begin(), I.idx_end(), SF), SF); |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 852 | static void executeLoadInst(LoadInst &I, ExecutionContext &SF) { |
| 853 | GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); |
Chris Lattner | 24ea74e | 2002-08-22 22:49:05 +0000 | [diff] [blame] | 854 | GenericValue *Ptr = (GenericValue*)SRC.PointerVal; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 855 | GenericValue Result; |
| 856 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 857 | switch (I.getType()->getPrimitiveID()) { |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 858 | case Type::BoolTyID: |
| 859 | case Type::UByteTyID: |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 860 | case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 861 | case Type::UShortTyID: |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 862 | case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 863 | case Type::UIntTyID: |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 864 | case Type::IntTyID: Result.IntVal = Ptr->IntVal; break; |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 865 | case Type::ULongTyID: |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 866 | case Type::LongTyID: Result.ULongVal = Ptr->ULongVal; break; |
| 867 | case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break; |
| 868 | case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break; |
| 869 | case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 870 | default: |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 871 | cout << "Cannot load value of type " << I.getType() << "!\n"; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 874 | SetValue(&I, Result, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 877 | static void executeStoreInst(StoreInst &I, ExecutionContext &SF) { |
| 878 | GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); |
Chris Lattner | 24ea74e | 2002-08-22 22:49:05 +0000 | [diff] [blame] | 879 | GenericValue *Ptr = (GenericValue *)SRC.PointerVal; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 880 | GenericValue Val = getOperandValue(I.getOperand(0), SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 881 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 882 | switch (I.getOperand(0)->getType()->getPrimitiveID()) { |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 883 | case Type::BoolTyID: |
| 884 | case Type::UByteTyID: |
| 885 | case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break; |
| 886 | case Type::UShortTyID: |
| 887 | case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break; |
| 888 | case Type::UIntTyID: |
| 889 | case Type::IntTyID: Ptr->IntVal = Val.IntVal; break; |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 890 | case Type::ULongTyID: |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 891 | case Type::LongTyID: Ptr->LongVal = Val.LongVal; break; |
| 892 | case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 893 | case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break; |
| 894 | case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 895 | default: |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 896 | cout << "Cannot store value of type " << I.getType() << "!\n"; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
| 900 | |
| 901 | //===----------------------------------------------------------------------===// |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 902 | // Miscellaneous Instruction Implementations |
| 903 | //===----------------------------------------------------------------------===// |
| 904 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 905 | void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) { |
| 906 | ECStack.back().Caller = &I; |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 907 | vector<GenericValue> ArgVals; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 908 | ArgVals.reserve(I.getNumOperands()-1); |
| 909 | for (unsigned i = 1; i < I.getNumOperands(); ++i) |
| 910 | ArgVals.push_back(getOperandValue(I.getOperand(i), SF)); |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 911 | |
Chris Lattner | 070cf5e | 2001-11-07 20:12:30 +0000 | [diff] [blame] | 912 | // To handle indirect calls, we must get the pointer value from the argument |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 913 | // and treat it as a function pointer. |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 914 | GenericValue SRC = getOperandValue(I.getCalledValue(), SF); |
Chris Lattner | 070cf5e | 2001-11-07 20:12:30 +0000 | [diff] [blame] | 915 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 916 | callMethod((Function*)SRC.PointerVal, ArgVals); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 919 | static void executePHINode(PHINode &I, ExecutionContext &SF) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 920 | BasicBlock *PrevBB = SF.PrevBB; |
| 921 | Value *IncomingValue = 0; |
| 922 | |
| 923 | // Search for the value corresponding to this previous bb... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 924 | for (unsigned i = I.getNumIncomingValues(); i > 0;) { |
| 925 | if (I.getIncomingBlock(--i) == PrevBB) { |
| 926 | IncomingValue = I.getIncomingValue(i); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 927 | break; |
| 928 | } |
| 929 | } |
| 930 | assert(IncomingValue && "No PHI node predecessor for current PrevBB!"); |
| 931 | |
| 932 | // Found the value, set as the result... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 933 | SetValue(&I, getOperandValue(IncomingValue, SF), SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 936 | #define IMPLEMENT_SHIFT(OP, TY) \ |
| 937 | case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break |
| 938 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 939 | static void executeShlInst(ShiftInst &I, ExecutionContext &SF) { |
| 940 | const Type *Ty = I.getOperand(0)->getType(); |
| 941 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 942 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 943 | GenericValue Dest; |
| 944 | |
| 945 | switch (Ty->getPrimitiveID()) { |
| 946 | IMPLEMENT_SHIFT(<<, UByte); |
| 947 | IMPLEMENT_SHIFT(<<, SByte); |
| 948 | IMPLEMENT_SHIFT(<<, UShort); |
| 949 | IMPLEMENT_SHIFT(<<, Short); |
| 950 | IMPLEMENT_SHIFT(<<, UInt); |
| 951 | IMPLEMENT_SHIFT(<<, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 952 | IMPLEMENT_SHIFT(<<, ULong); |
| 953 | IMPLEMENT_SHIFT(<<, Long); |
Chris Lattner | 743cd3e | 2002-07-09 18:42:36 +0000 | [diff] [blame] | 954 | IMPLEMENT_SHIFT(<<, Pointer); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 955 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 956 | cout << "Unhandled type for Shl instruction: " << Ty << "\n"; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 957 | } |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 958 | SetValue(&I, Dest, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 961 | static void executeShrInst(ShiftInst &I, ExecutionContext &SF) { |
| 962 | const Type *Ty = I.getOperand(0)->getType(); |
| 963 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 964 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 965 | GenericValue Dest; |
| 966 | |
| 967 | switch (Ty->getPrimitiveID()) { |
| 968 | IMPLEMENT_SHIFT(>>, UByte); |
| 969 | IMPLEMENT_SHIFT(>>, SByte); |
| 970 | IMPLEMENT_SHIFT(>>, UShort); |
| 971 | IMPLEMENT_SHIFT(>>, Short); |
| 972 | IMPLEMENT_SHIFT(>>, UInt); |
| 973 | IMPLEMENT_SHIFT(>>, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 974 | IMPLEMENT_SHIFT(>>, ULong); |
| 975 | IMPLEMENT_SHIFT(>>, Long); |
Chris Lattner | 743cd3e | 2002-07-09 18:42:36 +0000 | [diff] [blame] | 976 | IMPLEMENT_SHIFT(>>, Pointer); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 977 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 978 | cout << "Unhandled type for Shr instruction: " << Ty << "\n"; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 979 | } |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 980 | SetValue(&I, Dest, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | #define IMPLEMENT_CAST(DTY, DCTY, STY) \ |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 984 | case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 985 | |
| 986 | #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \ |
| 987 | case Type::DESTTY##TyID: \ |
| 988 | switch (SrcTy->getPrimitiveID()) { \ |
Chris Lattner | f4dca80 | 2002-05-02 19:28:45 +0000 | [diff] [blame] | 989 | IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \ |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 990 | IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \ |
| 991 | IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \ |
| 992 | IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \ |
| 993 | IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \ |
| 994 | IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \ |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 995 | IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \ |
| 996 | IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \ |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 997 | IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \ |
| 998 | IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 999 | |
| 1000 | #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \ |
| 1001 | IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \ |
| 1002 | IMPLEMENT_CAST(DESTTY, DESTCTY, Double) |
| 1003 | |
| 1004 | #define IMPLEMENT_CAST_CASE_END() \ |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1005 | default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \ |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1006 | break; \ |
| 1007 | } \ |
| 1008 | break |
| 1009 | |
| 1010 | #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \ |
| 1011 | IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \ |
| 1012 | IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \ |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1013 | IMPLEMENT_CAST_CASE_END() |
| 1014 | |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1015 | static GenericValue executeCastOperation(Value *SrcVal, const Type *Ty, |
| 1016 | ExecutionContext &SF) { |
| 1017 | const Type *SrcTy = SrcVal->getType(); |
| 1018 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1019 | |
| 1020 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1021 | IMPLEMENT_CAST_CASE(UByte , (unsigned char)); |
| 1022 | IMPLEMENT_CAST_CASE(SByte , ( signed char)); |
| 1023 | IMPLEMENT_CAST_CASE(UShort , (unsigned short)); |
Chris Lattner | 1bbd361 | 2002-08-02 22:06:04 +0000 | [diff] [blame] | 1024 | IMPLEMENT_CAST_CASE(Short , ( signed short)); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1025 | IMPLEMENT_CAST_CASE(UInt , (unsigned int )); |
| 1026 | IMPLEMENT_CAST_CASE(Int , ( signed int )); |
| 1027 | IMPLEMENT_CAST_CASE(ULong , (uint64_t)); |
| 1028 | IMPLEMENT_CAST_CASE(Long , ( int64_t)); |
| 1029 | IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t)); |
| 1030 | IMPLEMENT_CAST_CASE(Float , (float)); |
| 1031 | IMPLEMENT_CAST_CASE(Double , (double)); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1032 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1033 | cout << "Unhandled dest type for cast instruction: " << Ty << "\n"; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1034 | } |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1035 | |
| 1036 | return Dest; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1037 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1038 | |
| 1039 | |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1040 | static void executeCastInst(CastInst &I, ExecutionContext &SF) { |
| 1041 | SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF); |
| 1042 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1043 | |
| 1044 | |
| 1045 | //===----------------------------------------------------------------------===// |
| 1046 | // Dispatch and Execution Code |
| 1047 | //===----------------------------------------------------------------------===// |
| 1048 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1049 | MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) { |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1050 | // Assign slot numbers to the function arguments... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1051 | for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI) |
| 1052 | AI->addAnnotation(new SlotNumber(getValueSlot(AI))); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1053 | |
| 1054 | // Iterate over all of the instructions... |
| 1055 | unsigned InstNum = 0; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1056 | for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB) |
| 1057 | for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II) |
| 1058 | // For each instruction... Add Annote |
| 1059 | II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II))); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | unsigned MethodInfo::getValueSlot(const Value *V) { |
| 1063 | unsigned Plane = V->getType()->getUniqueID(); |
| 1064 | if (Plane >= NumPlaneElements.size()) |
| 1065 | NumPlaneElements.resize(Plane+1, 0); |
| 1066 | return NumPlaneElements[Plane]++; |
| 1067 | } |
| 1068 | |
| 1069 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1070 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1071 | // callMethod - Execute the specified function... |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1072 | // |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1073 | void Interpreter::callMethod(Function *M, const vector<GenericValue> &ArgVals) { |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 1074 | assert((ECStack.empty() || ECStack.back().Caller == 0 || |
| 1075 | ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) && |
| 1076 | "Incorrect number of arguments passed into function call!"); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1077 | if (M->isExternal()) { |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1078 | GenericValue Result = callExternalMethod(M, ArgVals); |
| 1079 | const Type *RetTy = M->getReturnType(); |
| 1080 | |
| 1081 | // Copy the result back into the result variable if we are not returning |
| 1082 | // void. |
| 1083 | if (RetTy != Type::VoidTy) { |
| 1084 | if (!ECStack.empty() && ECStack.back().Caller) { |
| 1085 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1086 | SetValue(SF.Caller, Result, SF); |
| 1087 | |
| 1088 | SF.Caller = 0; // We returned from the call... |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 1089 | } else if (!QuietMode) { |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1090 | // print it. |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1091 | CW << "Function " << M->getType() << " \"" << M->getName() |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 1092 | << "\" returned "; |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1093 | print(RetTy, Result); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1094 | cout << "\n"; |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1095 | |
| 1096 | if (RetTy->isIntegral()) |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame^] | 1097 | ExitCode = Result.IntVal; // Capture the exit code of the program |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1101 | return; |
| 1102 | } |
| 1103 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1104 | // Process the function, assigning instruction numbers to the instructions in |
| 1105 | // the function. Also calculate the number of values for each type slot |
| 1106 | // active. |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1107 | // |
| 1108 | MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1109 | ECStack.push_back(ExecutionContext()); // Make a new stack frame... |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1110 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1111 | ExecutionContext &StackFrame = ECStack.back(); // Fill it in... |
| 1112 | StackFrame.CurMethod = M; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1113 | StackFrame.CurBB = M->begin(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1114 | StackFrame.CurInst = StackFrame.CurBB->begin(); |
| 1115 | StackFrame.MethInfo = MethInfo; |
| 1116 | |
| 1117 | // Initialize the values to nothing... |
| 1118 | StackFrame.Values.resize(MethInfo->NumPlaneElements.size()); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1119 | for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1120 | StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]); |
| 1121 | |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1122 | // Taint the initial values of stuff |
| 1123 | memset(&StackFrame.Values[i][0], 42, |
| 1124 | MethInfo->NumPlaneElements[i]*sizeof(GenericValue)); |
| 1125 | } |
| 1126 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1127 | StackFrame.PrevBB = 0; // No previous BB for PHI nodes... |
| 1128 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1129 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1130 | // Run through the function arguments and initialize their values... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1131 | assert(ArgVals.size() == M->asize() && |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1132 | "Invalid number of values passed to function invocation!"); |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 1133 | unsigned i = 0; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1134 | for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i) |
| 1135 | SetValue(AI, ArgVals[i], StackFrame); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | // executeInstruction - Interpret a single instruction, increment the "PC", and |
| 1139 | // return true if the next instruction is a breakpoint... |
| 1140 | // |
| 1141 | bool Interpreter::executeInstruction() { |
| 1142 | assert(!ECStack.empty() && "No program running, cannot execute inst!"); |
| 1143 | |
| 1144 | ExecutionContext &SF = ECStack.back(); // Current stack frame |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1145 | Instruction &I = *SF.CurInst++; // Increment before execute |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1146 | |
Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 1147 | if (Trace) |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 1148 | CW << "Run:" << I; |
| 1149 | |
| 1150 | // Set a sigsetjmp buffer so that we can recover if an error happens during |
| 1151 | // instruction execution... |
| 1152 | // |
| 1153 | if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) { |
| 1154 | --SF.CurInst; // Back up to erroring instruction |
Chris Lattner | 7403025 | 2002-02-12 15:47:23 +0000 | [diff] [blame] | 1155 | if (SigNo != SIGINT) { |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1156 | cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n"; |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1157 | printStackTrace(); |
Chris Lattner | 7403025 | 2002-02-12 15:47:23 +0000 | [diff] [blame] | 1158 | // If -abort-on-exception was specified, terminate LLI instead of trying |
| 1159 | // to debug it. |
| 1160 | // |
| 1161 | if (AbortOnExceptions) exit(1); |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 1162 | } else if (SigNo == SIGINT) { |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1163 | cout << "CTRL-C Detected, execution halted.\n"; |
| 1164 | } |
| 1165 | InInstruction = false; |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 1166 | return true; |
| 1167 | } |
Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 1168 | |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1169 | InInstruction = true; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1170 | if (I.isBinaryOp()) { |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1171 | executeBinaryInst(cast<BinaryOperator>(I), SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1172 | } else { |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1173 | switch (I.getOpcode()) { |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1174 | // Terminators |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1175 | case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break; |
| 1176 | case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1177 | // Memory Instructions |
| 1178 | case Instruction::Alloca: |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1179 | case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break; |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1180 | case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break; |
| 1181 | case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break; |
| 1182 | case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break; |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 1183 | case Instruction::GetElementPtr: |
| 1184 | executeGEPInst(cast<GetElementPtrInst>(I), SF); break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1185 | |
| 1186 | // Miscellaneous Instructions |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1187 | case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break; |
| 1188 | case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break; |
| 1189 | case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break; |
| 1190 | case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break; |
| 1191 | case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1192 | default: |
| 1193 | cout << "Don't know how to execute this instruction!\n-->" << I; |
| 1194 | } |
| 1195 | } |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1196 | InInstruction = false; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1197 | |
| 1198 | // Reset the current frame location to the top of stack |
| 1199 | CurFrame = ECStack.size()-1; |
| 1200 | |
| 1201 | if (CurFrame == -1) return false; // No breakpoint if no code |
| 1202 | |
| 1203 | // Return true if there is a breakpoint annotation on the instruction... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1204 | return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | void Interpreter::stepInstruction() { // Do the 'step' command |
| 1208 | if (ECStack.empty()) { |
| 1209 | cout << "Error: no program running, cannot step!\n"; |
| 1210 | return; |
| 1211 | } |
| 1212 | |
| 1213 | // Run an instruction... |
| 1214 | executeInstruction(); |
| 1215 | |
| 1216 | // Print the next instruction to execute... |
| 1217 | printCurrentInstruction(); |
| 1218 | } |
| 1219 | |
| 1220 | // --- UI Stuff... |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1221 | void Interpreter::nextInstruction() { // Do the 'next' command |
| 1222 | if (ECStack.empty()) { |
| 1223 | cout << "Error: no program running, cannot 'next'!\n"; |
| 1224 | return; |
| 1225 | } |
| 1226 | |
| 1227 | // If this is a call instruction, step over the call instruction... |
| 1228 | // TODO: ICALL, CALL WITH, ... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1229 | if (ECStack.back().CurInst->getOpcode() == Instruction::Call) { |
Chris Lattner | a74a6b5 | 2001-10-29 14:08:33 +0000 | [diff] [blame] | 1230 | unsigned StackSize = ECStack.size(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1231 | // Step into the function... |
| 1232 | if (executeInstruction()) { |
| 1233 | // Hit a breakpoint, print current instruction, then return to user... |
| 1234 | cout << "Breakpoint hit!\n"; |
| 1235 | printCurrentInstruction(); |
| 1236 | return; |
| 1237 | } |
| 1238 | |
Chris Lattner | a74a6b5 | 2001-10-29 14:08:33 +0000 | [diff] [blame] | 1239 | // If we we able to step into the function, finish it now. We might not be |
| 1240 | // able the step into a function, if it's external for example. |
| 1241 | if (ECStack.size() != StackSize) |
| 1242 | finish(); // Finish executing the function... |
Chris Lattner | 069aa25 | 2001-10-29 16:05:19 +0000 | [diff] [blame] | 1243 | else |
| 1244 | printCurrentInstruction(); |
Chris Lattner | a74a6b5 | 2001-10-29 14:08:33 +0000 | [diff] [blame] | 1245 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1246 | } else { |
| 1247 | // Normal instruction, just step... |
| 1248 | stepInstruction(); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | void Interpreter::run() { |
| 1253 | if (ECStack.empty()) { |
| 1254 | cout << "Error: no program running, cannot run!\n"; |
| 1255 | return; |
| 1256 | } |
| 1257 | |
| 1258 | bool HitBreakpoint = false; |
| 1259 | while (!ECStack.empty() && !HitBreakpoint) { |
| 1260 | // Run an instruction... |
| 1261 | HitBreakpoint = executeInstruction(); |
| 1262 | } |
| 1263 | |
| 1264 | if (HitBreakpoint) { |
| 1265 | cout << "Breakpoint hit!\n"; |
| 1266 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1267 | // Print the next instruction to execute... |
| 1268 | printCurrentInstruction(); |
| 1269 | } |
| 1270 | |
| 1271 | void Interpreter::finish() { |
| 1272 | if (ECStack.empty()) { |
| 1273 | cout << "Error: no program running, cannot run!\n"; |
| 1274 | return; |
| 1275 | } |
| 1276 | |
| 1277 | unsigned StackSize = ECStack.size(); |
| 1278 | bool HitBreakpoint = false; |
| 1279 | while (ECStack.size() >= StackSize && !HitBreakpoint) { |
| 1280 | // Run an instruction... |
| 1281 | HitBreakpoint = executeInstruction(); |
| 1282 | } |
| 1283 | |
| 1284 | if (HitBreakpoint) { |
| 1285 | cout << "Breakpoint hit!\n"; |
| 1286 | } |
| 1287 | |
| 1288 | // Print the next instruction to execute... |
| 1289 | printCurrentInstruction(); |
| 1290 | } |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | // printCurrentInstruction - Print out the instruction that the virtual PC is |
| 1295 | // at, or fail silently if no program is running. |
| 1296 | // |
| 1297 | void Interpreter::printCurrentInstruction() { |
| 1298 | if (!ECStack.empty()) { |
Chris Lattner | f5b2ec1 | 2001-10-29 20:44:34 +0000 | [diff] [blame] | 1299 | if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label |
| 1300 | WriteAsOperand(cout, ECStack.back().CurBB) << ":\n"; |
| 1301 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1302 | Instruction &I = *ECStack.back().CurInst; |
| 1303 | InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1304 | assert(IN && "Instruction has no numbering annotation!"); |
| 1305 | cout << "#" << IN->InstNum << I; |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | void Interpreter::printValue(const Type *Ty, GenericValue V) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1310 | switch (Ty->getPrimitiveID()) { |
| 1311 | case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break; |
Chris Lattner | 65629d5 | 2002-08-13 20:45:11 +0000 | [diff] [blame] | 1312 | case Type::SByteTyID: |
| 1313 | cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break; |
| 1314 | case Type::UByteTyID: |
| 1315 | cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1316 | case Type::ShortTyID: cout << V.ShortVal; break; |
| 1317 | case Type::UShortTyID: cout << V.UShortVal; break; |
| 1318 | case Type::IntTyID: cout << V.IntVal; break; |
| 1319 | case Type::UIntTyID: cout << V.UIntVal; break; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1320 | case Type::LongTyID: cout << (long)V.LongVal; break; |
| 1321 | case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1322 | case Type::FloatTyID: cout << V.FloatVal; break; |
| 1323 | case Type::DoubleTyID: cout << V.DoubleVal; break; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1324 | case Type::PointerTyID:cout << (void*)V.PointerVal; break; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1325 | default: |
| 1326 | cout << "- Don't know how to print value of this type!"; |
| 1327 | break; |
| 1328 | } |
| 1329 | } |
| 1330 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 1331 | void Interpreter::print(const Type *Ty, GenericValue V) { |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 1332 | CW << Ty << " "; |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 1333 | printValue(Ty, V); |
| 1334 | } |
| 1335 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1336 | void Interpreter::print(const std::string &Name) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1337 | Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name)); |
| 1338 | if (!PickedVal) return; |
| 1339 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1340 | if (const Function *F = dyn_cast<const Function>(PickedVal)) { |
| 1341 | CW << F; // Print the function |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1342 | } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1343 | CW << "type %" << Name << " = " << Ty->getDescription() << "\n"; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1344 | } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) { |
| 1345 | CW << BB; // Print the basic block |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1346 | } else { // Otherwise there should be an annotation for the slot# |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 1347 | print(PickedVal->getType(), |
| 1348 | getOperandValue(PickedVal, ECStack[CurFrame])); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1349 | cout << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1350 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1353 | void Interpreter::infoValue(const std::string &Name) { |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1354 | Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name)); |
| 1355 | if (!PickedVal) return; |
| 1356 | |
| 1357 | cout << "Value: "; |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 1358 | print(PickedVal->getType(), |
| 1359 | getOperandValue(PickedVal, ECStack[CurFrame])); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1360 | cout << "\n"; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1361 | printOperandInfo(PickedVal, ECStack[CurFrame]); |
| 1362 | } |
| 1363 | |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1364 | // printStackFrame - Print information about the specified stack frame, or -1 |
| 1365 | // for the default one. |
| 1366 | // |
Chris Lattner | 601d715 | 2002-07-25 17:37:05 +0000 | [diff] [blame] | 1367 | void Interpreter::printStackFrame(int FrameNo) { |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1368 | if (FrameNo == -1) FrameNo = CurFrame; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1369 | Function *F = ECStack[FrameNo].CurMethod; |
| 1370 | const Type *RetTy = F->getReturnType(); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1371 | |
| 1372 | CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". " |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1373 | << (Value*)RetTy << " \"" << F->getName() << "\"("; |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1374 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1375 | unsigned i = 0; |
| 1376 | for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) { |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1377 | if (i != 0) cout << ", "; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1378 | CW << *I << "="; |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1379 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1380 | printValue(I->getType(), getOperandValue(I, ECStack[FrameNo])); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1381 | } |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1382 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1383 | cout << ")\n"; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1384 | |
| 1385 | if (FrameNo != int(ECStack.size()-1)) { |
| 1386 | BasicBlock::iterator I = ECStack[FrameNo].CurInst; |
| 1387 | CW << --I; |
| 1388 | } else { |
| 1389 | CW << *ECStack[FrameNo].CurInst; |
| 1390 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1391 | } |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1392 | |