blob: 79c404aae06884f598806f7318d0b42424ba788e [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- 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 Lattnerd5bc41a2003-04-25 04:21:19 +00009#include "llvm/Module.h"
10#include "llvm/Instructions.h"
Chris Lattnere2cbbce2002-04-29 18:56:45 +000011#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000012#include "llvm/Constants.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000013#include "llvm/Assembly/Writer.h"
Chris Lattnerf23eb852001-12-14 16:49:29 +000014#include "Support/CommandLine.h"
Chris Lattnerbbdabce2002-12-08 05:51:08 +000015#include "Support/Statistic.h"
Chris Lattnerbb76f022001-10-30 20:27:31 +000016#include <math.h> // For fmod
Chris Lattner5af0c482001-11-07 04:23:00 +000017#include <signal.h>
18#include <setjmp.h>
Chris Lattner2e42d3a2001-10-15 05:51:48 +000019
Chris Lattnerfe11a972002-12-23 23:59:41 +000020Interpreter *TheEE = 0;
21
Chris Lattnerbbdabce2002-12-08 05:51:08 +000022namespace {
23 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
Chris Lattner138b0cd2002-12-08 06:01:34 +000024
25 cl::opt<bool>
Chris Lattnerfe11a972002-12-23 23:59:41 +000026 QuietMode("quiet", cl::desc("Do not emit any non-program output"),
27 cl::init(true));
Chris Lattner138b0cd2002-12-08 06:01:34 +000028
29 cl::alias
30 QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode));
31
32 cl::opt<bool>
33 ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks"));
Chris Lattnerbbdabce2002-12-08 05:51:08 +000034}
35
Chris Lattner2e42d3a2001-10-15 05:51:48 +000036// Create a TargetData structure to handle memory addressing and size/alignment
37// computations
38//
Chris Lattnerea38c0e2001-11-07 19:46:27 +000039CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000040
Chris Lattner5af0c482001-11-07 04:23:00 +000041sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000042static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000043
44extern "C" {
45static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000046 if (InInstruction)
47 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000048}
49}
50
51static void initializeSignalHandlers() {
52 struct sigaction Action;
53 Action.sa_handler = SigHandler;
54 Action.sa_flags = SA_SIGINFO;
55 sigemptyset(&Action.sa_mask);
56 sigaction(SIGSEGV, &Action, 0);
57 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000058 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000059 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000060}
61
Chris Lattner2e42d3a2001-10-15 05:51:48 +000062
63//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000064// Value Manipulation code
65//===----------------------------------------------------------------------===//
66
67static unsigned getOperandSlot(Value *V) {
68 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
69 assert(SN && "Operand does not have a slot number annotation!");
70 return SN->SlotNum;
71}
72
Chris Lattnera34c5682002-08-27 22:33:45 +000073// Operations used by constant expr implementations...
74static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
75 ExecutionContext &SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000076static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000077 const Type *Ty);
Chris Lattnera34c5682002-08-27 22:33:45 +000078
Chris Lattnerfddc7552002-10-15 20:34:05 +000079
Chris Lattner39bb5b42001-10-15 13:25:40 +000080static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +000081 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
82 switch (CE->getOpcode()) {
83 case Instruction::Cast:
84 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
85 case Instruction::GetElementPtr:
Chris Lattnerfe11a972002-12-23 23:59:41 +000086 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
87 CE->op_end(), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000088 case Instruction::Add:
89 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
90 getOperandValue(CE->getOperand(1), SF),
Chris Lattnerb945e4d2003-04-22 20:37:39 +000091 CE->getType());
Chris Lattnera34c5682002-08-27 22:33:45 +000092 default:
Chris Lattner02868352003-04-22 21:22:33 +000093 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
Chris Lattnera34c5682002-08-27 22:33:45 +000094 abort();
Chris Lattner04e2ad72003-04-21 22:43:32 +000095 return GenericValue();
Chris Lattnera34c5682002-08-27 22:33:45 +000096 }
97 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000098 return TheEE->getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +000099 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000100 return PTOGV(TheEE->getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000101 } else {
102 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000103 unsigned OpSlot = getOperandSlot(V);
104 assert(TyP < SF.Values.size() &&
105 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000106 return SF.Values[TyP][getOperandSlot(V)];
107 }
108}
109
110static void printOperandInfo(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000111 if (isa<Constant>(V)) {
Chris Lattner02868352003-04-22 21:22:33 +0000112 std::cout << "Constant Pool Value\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000113 } else if (isa<GlobalValue>(V)) {
Chris Lattner02868352003-04-22 21:22:33 +0000114 std::cout << "Global Value\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000115 } else {
116 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
117 unsigned Slot = getOperandSlot(V);
Chris Lattner02868352003-04-22 21:22:33 +0000118 std::cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
119 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
120 << " Contents=0x";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000121
122 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
123 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
124 unsigned char Cur = Buf[i];
Chris Lattner02868352003-04-22 21:22:33 +0000125 std::cout << ( Cur >= 160?char((Cur>>4)+'A'-10):char((Cur>>4) + '0'))
126 << ((Cur&15) >= 10?char((Cur&15)+'A'-10):char((Cur&15) + '0'));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000127 }
Chris Lattner02868352003-04-22 21:22:33 +0000128 std::cout << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000129 }
130}
131
132
133
134static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
135 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
136
Chris Lattner02868352003-04-22 21:22:33 +0000137 //std::cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)]<< "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000138 SF.Values[TyP][getOperandSlot(V)] = Val;
139}
140
141
142//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000143// Annotation Wrangling code
144//===----------------------------------------------------------------------===//
145
146void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000147 TheEE = this;
Chris Lattnerda82ed52003-05-08 16:18:31 +0000148 AnnotationManager::registerAnnotationFactory(FunctionInfoAID,
149 &FunctionInfo::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000150 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000151}
152
Chris Lattner2adcd832002-05-03 19:52:30 +0000153//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000154// Binary Instruction Implementations
155//===----------------------------------------------------------------------===//
156
157#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
158 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
159
160static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000161 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000162 GenericValue Dest;
163 switch (Ty->getPrimitiveID()) {
164 IMPLEMENT_BINARY_OPERATOR(+, UByte);
165 IMPLEMENT_BINARY_OPERATOR(+, SByte);
166 IMPLEMENT_BINARY_OPERATOR(+, UShort);
167 IMPLEMENT_BINARY_OPERATOR(+, Short);
168 IMPLEMENT_BINARY_OPERATOR(+, UInt);
169 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000170 IMPLEMENT_BINARY_OPERATOR(+, ULong);
171 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000172 IMPLEMENT_BINARY_OPERATOR(+, Float);
173 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000174 default:
Chris Lattner02868352003-04-22 21:22:33 +0000175 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
176 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000177 }
178 return Dest;
179}
180
181static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000182 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000183 GenericValue Dest;
184 switch (Ty->getPrimitiveID()) {
185 IMPLEMENT_BINARY_OPERATOR(-, UByte);
186 IMPLEMENT_BINARY_OPERATOR(-, SByte);
187 IMPLEMENT_BINARY_OPERATOR(-, UShort);
188 IMPLEMENT_BINARY_OPERATOR(-, Short);
189 IMPLEMENT_BINARY_OPERATOR(-, UInt);
190 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000191 IMPLEMENT_BINARY_OPERATOR(-, ULong);
192 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000193 IMPLEMENT_BINARY_OPERATOR(-, Float);
194 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000195 default:
Chris Lattner02868352003-04-22 21:22:33 +0000196 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
197 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000198 }
199 return Dest;
200}
201
Chris Lattnerc2593162001-10-27 08:28:11 +0000202static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000203 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000204 GenericValue Dest;
205 switch (Ty->getPrimitiveID()) {
206 IMPLEMENT_BINARY_OPERATOR(*, UByte);
207 IMPLEMENT_BINARY_OPERATOR(*, SByte);
208 IMPLEMENT_BINARY_OPERATOR(*, UShort);
209 IMPLEMENT_BINARY_OPERATOR(*, Short);
210 IMPLEMENT_BINARY_OPERATOR(*, UInt);
211 IMPLEMENT_BINARY_OPERATOR(*, Int);
212 IMPLEMENT_BINARY_OPERATOR(*, ULong);
213 IMPLEMENT_BINARY_OPERATOR(*, Long);
214 IMPLEMENT_BINARY_OPERATOR(*, Float);
215 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000216 default:
Chris Lattner02868352003-04-22 21:22:33 +0000217 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
218 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000219 }
220 return Dest;
221}
222
223static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000224 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000225 GenericValue Dest;
226 switch (Ty->getPrimitiveID()) {
227 IMPLEMENT_BINARY_OPERATOR(/, UByte);
228 IMPLEMENT_BINARY_OPERATOR(/, SByte);
229 IMPLEMENT_BINARY_OPERATOR(/, UShort);
230 IMPLEMENT_BINARY_OPERATOR(/, Short);
231 IMPLEMENT_BINARY_OPERATOR(/, UInt);
232 IMPLEMENT_BINARY_OPERATOR(/, Int);
233 IMPLEMENT_BINARY_OPERATOR(/, ULong);
234 IMPLEMENT_BINARY_OPERATOR(/, Long);
235 IMPLEMENT_BINARY_OPERATOR(/, Float);
236 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000237 default:
Chris Lattner02868352003-04-22 21:22:33 +0000238 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
239 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000240 }
241 return Dest;
242}
243
244static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000245 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000246 GenericValue Dest;
247 switch (Ty->getPrimitiveID()) {
248 IMPLEMENT_BINARY_OPERATOR(%, UByte);
249 IMPLEMENT_BINARY_OPERATOR(%, SByte);
250 IMPLEMENT_BINARY_OPERATOR(%, UShort);
251 IMPLEMENT_BINARY_OPERATOR(%, Short);
252 IMPLEMENT_BINARY_OPERATOR(%, UInt);
253 IMPLEMENT_BINARY_OPERATOR(%, Int);
254 IMPLEMENT_BINARY_OPERATOR(%, ULong);
255 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000256 case Type::FloatTyID:
257 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
258 break;
259 case Type::DoubleTyID:
260 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
261 break;
262 default:
Chris Lattner02868352003-04-22 21:22:33 +0000263 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
264 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000265 }
266 return Dest;
267}
268
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000269static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000270 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000271 GenericValue Dest;
272 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000273 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000274 IMPLEMENT_BINARY_OPERATOR(&, UByte);
275 IMPLEMENT_BINARY_OPERATOR(&, SByte);
276 IMPLEMENT_BINARY_OPERATOR(&, UShort);
277 IMPLEMENT_BINARY_OPERATOR(&, Short);
278 IMPLEMENT_BINARY_OPERATOR(&, UInt);
279 IMPLEMENT_BINARY_OPERATOR(&, Int);
280 IMPLEMENT_BINARY_OPERATOR(&, ULong);
281 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000282 default:
Chris Lattner02868352003-04-22 21:22:33 +0000283 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
284 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000285 }
286 return Dest;
287}
288
289
290static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000291 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000292 GenericValue Dest;
293 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000294 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000295 IMPLEMENT_BINARY_OPERATOR(|, UByte);
296 IMPLEMENT_BINARY_OPERATOR(|, SByte);
297 IMPLEMENT_BINARY_OPERATOR(|, UShort);
298 IMPLEMENT_BINARY_OPERATOR(|, Short);
299 IMPLEMENT_BINARY_OPERATOR(|, UInt);
300 IMPLEMENT_BINARY_OPERATOR(|, Int);
301 IMPLEMENT_BINARY_OPERATOR(|, ULong);
302 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000303 default:
Chris Lattner02868352003-04-22 21:22:33 +0000304 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
305 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000306 }
307 return Dest;
308}
309
310
311static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000312 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000313 GenericValue Dest;
314 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000315 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000316 IMPLEMENT_BINARY_OPERATOR(^, UByte);
317 IMPLEMENT_BINARY_OPERATOR(^, SByte);
318 IMPLEMENT_BINARY_OPERATOR(^, UShort);
319 IMPLEMENT_BINARY_OPERATOR(^, Short);
320 IMPLEMENT_BINARY_OPERATOR(^, UInt);
321 IMPLEMENT_BINARY_OPERATOR(^, Int);
322 IMPLEMENT_BINARY_OPERATOR(^, ULong);
323 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000324 default:
Chris Lattner02868352003-04-22 21:22:33 +0000325 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
326 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000327 }
328 return Dest;
329}
330
331
Chris Lattner92101ac2001-08-23 17:05:04 +0000332#define IMPLEMENT_SETCC(OP, TY) \
333 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
334
Chris Lattnerfd506f52003-04-23 19:55:35 +0000335// Handle pointers specially because they must be compared with only as much
336// width as the host has. We _do not_ want to be comparing 64 bit values when
337// running on a 32-bit target, otherwise the upper 32 bits might mess up
338// comparisons if they contain garbage.
339#define IMPLEMENT_POINTERSETCC(OP) \
340 case Type::PointerTyID: \
341 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
342 (void*)(intptr_t)Src2.PointerVal; break
343
Chris Lattner92101ac2001-08-23 17:05:04 +0000344static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000345 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000346 GenericValue Dest;
347 switch (Ty->getPrimitiveID()) {
348 IMPLEMENT_SETCC(==, UByte);
349 IMPLEMENT_SETCC(==, SByte);
350 IMPLEMENT_SETCC(==, UShort);
351 IMPLEMENT_SETCC(==, Short);
352 IMPLEMENT_SETCC(==, UInt);
353 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000354 IMPLEMENT_SETCC(==, ULong);
355 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000356 IMPLEMENT_SETCC(==, Float);
357 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000358 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000359 default:
Chris Lattner02868352003-04-22 21:22:33 +0000360 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
361 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000362 }
363 return Dest;
364}
365
366static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000367 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000368 GenericValue Dest;
369 switch (Ty->getPrimitiveID()) {
370 IMPLEMENT_SETCC(!=, UByte);
371 IMPLEMENT_SETCC(!=, SByte);
372 IMPLEMENT_SETCC(!=, UShort);
373 IMPLEMENT_SETCC(!=, Short);
374 IMPLEMENT_SETCC(!=, UInt);
375 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000376 IMPLEMENT_SETCC(!=, ULong);
377 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000378 IMPLEMENT_SETCC(!=, Float);
379 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000380 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000381
Chris Lattner92101ac2001-08-23 17:05:04 +0000382 default:
Chris Lattner02868352003-04-22 21:22:33 +0000383 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
384 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000385 }
386 return Dest;
387}
388
389static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000390 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000391 GenericValue Dest;
392 switch (Ty->getPrimitiveID()) {
393 IMPLEMENT_SETCC(<=, UByte);
394 IMPLEMENT_SETCC(<=, SByte);
395 IMPLEMENT_SETCC(<=, UShort);
396 IMPLEMENT_SETCC(<=, Short);
397 IMPLEMENT_SETCC(<=, UInt);
398 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000399 IMPLEMENT_SETCC(<=, ULong);
400 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000401 IMPLEMENT_SETCC(<=, Float);
402 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000403 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000404 default:
Chris Lattner02868352003-04-22 21:22:33 +0000405 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
406 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000407 }
408 return Dest;
409}
410
411static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000412 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000413 GenericValue Dest;
414 switch (Ty->getPrimitiveID()) {
415 IMPLEMENT_SETCC(>=, UByte);
416 IMPLEMENT_SETCC(>=, SByte);
417 IMPLEMENT_SETCC(>=, UShort);
418 IMPLEMENT_SETCC(>=, Short);
419 IMPLEMENT_SETCC(>=, UInt);
420 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000421 IMPLEMENT_SETCC(>=, ULong);
422 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000423 IMPLEMENT_SETCC(>=, Float);
424 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000425 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000426 default:
Chris Lattner02868352003-04-22 21:22:33 +0000427 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
428 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000429 }
430 return Dest;
431}
432
433static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000434 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000435 GenericValue Dest;
436 switch (Ty->getPrimitiveID()) {
437 IMPLEMENT_SETCC(<, UByte);
438 IMPLEMENT_SETCC(<, SByte);
439 IMPLEMENT_SETCC(<, UShort);
440 IMPLEMENT_SETCC(<, Short);
441 IMPLEMENT_SETCC(<, UInt);
442 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000443 IMPLEMENT_SETCC(<, ULong);
444 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000445 IMPLEMENT_SETCC(<, Float);
446 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000447 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000448 default:
Chris Lattner02868352003-04-22 21:22:33 +0000449 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
450 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000451 }
452 return Dest;
453}
454
455static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000456 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000457 GenericValue Dest;
458 switch (Ty->getPrimitiveID()) {
459 IMPLEMENT_SETCC(>, UByte);
460 IMPLEMENT_SETCC(>, SByte);
461 IMPLEMENT_SETCC(>, UShort);
462 IMPLEMENT_SETCC(>, Short);
463 IMPLEMENT_SETCC(>, UInt);
464 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000465 IMPLEMENT_SETCC(>, ULong);
466 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000467 IMPLEMENT_SETCC(>, Float);
468 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000469 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000470 default:
Chris Lattner02868352003-04-22 21:22:33 +0000471 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
472 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000473 }
474 return Dest;
475}
476
Chris Lattnerd7916e92003-05-10 21:22:39 +0000477void Interpreter::visitBinaryOperator(BinaryOperator &I) {
478 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000479 const Type *Ty = I.getOperand(0)->getType();
480 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
481 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000482 GenericValue R; // Result
483
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000484 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000485 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
486 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
487 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
488 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
489 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
490 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
491 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
492 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
493 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
494 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
495 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
496 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
497 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
498 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000499 default:
Chris Lattner02868352003-04-22 21:22:33 +0000500 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
501 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000502 }
503
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000504 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000505}
506
Chris Lattner92101ac2001-08-23 17:05:04 +0000507//===----------------------------------------------------------------------===//
508// Terminator Instruction Implementations
509//===----------------------------------------------------------------------===//
510
Chris Lattnere43db882001-10-27 04:15:57 +0000511void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000512 if (!QuietMode) {
Chris Lattner02868352003-04-22 21:22:33 +0000513 std::cout << "Program returned ";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000514 print(Type::IntTy, GV);
Chris Lattner02868352003-04-22 21:22:33 +0000515 std::cout << " via 'void exit(int)'\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000516 }
Chris Lattnere43db882001-10-27 04:15:57 +0000517
518 ExitCode = GV.SByteVal;
519 ECStack.clear();
520}
521
Chris Lattnerd7916e92003-05-10 21:22:39 +0000522void Interpreter::visitReturnInst(ReturnInst &I) {
523 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000524 const Type *RetTy = 0;
525 GenericValue Result;
526
527 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000528 if (I.getNumOperands()) {
529 RetTy = I.getReturnValue()->getType();
530 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000531 }
532
533 // Save previously executing meth
Chris Lattnerda82ed52003-05-08 16:18:31 +0000534 const Function *M = ECStack.back().CurFunction;
Chris Lattner92101ac2001-08-23 17:05:04 +0000535
536 // Pop the current stack frame... this invalidates SF
537 ECStack.pop_back();
538
539 if (ECStack.empty()) { // Finished main. Put result into exit code...
540 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000541 if (!QuietMode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000542 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattnerf23eb852001-12-14 16:49:29 +0000543 << "\" returned ";
544 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000545 std::cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000546 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000547
548 if (RetTy->isIntegral())
Chris Lattnerf4dca802002-05-02 19:28:45 +0000549 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000550 } else {
551 ExitCode = 0;
552 }
553 return;
554 }
555
556 // If we have a previous stack frame, and we have a previous call, fill in
557 // the return value...
558 //
559 ExecutionContext &NewSF = ECStack.back();
560 if (NewSF.Caller) {
561 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
562 SetValue(NewSF.Caller, Result, NewSF);
563
564 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000565 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000566 // This must be a function that is executing because of a user 'call'
567 // instruction.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000568 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000569 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000570 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000571 std::cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000572 }
573}
574
Chris Lattnerd7916e92003-05-10 21:22:39 +0000575void Interpreter::visitBranchInst(BranchInst &I) {
576 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000577 BasicBlock *Dest;
578
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000579 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
580 if (!I.isUnconditional()) {
581 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000582 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000583 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000584 }
Chris Lattner77113b62003-05-10 20:21:16 +0000585 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000586}
587
Chris Lattnerd7916e92003-05-10 21:22:39 +0000588void Interpreter::visitSwitchInst(SwitchInst &I) {
589 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000590 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
591 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000592
593 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000594 BasicBlock *Dest = 0;
595 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000596 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000597 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000598 Dest = cast<BasicBlock>(I.getOperand(i+1));
599 break;
600 }
Chris Lattner09e93922003-04-22 20:34:47 +0000601
602 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000603 SwitchToNewBasicBlock(Dest, SF);
604}
605
606// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
607// This function handles the actual updating of block and instruction iterators
608// as well as execution of all of the PHI nodes in the destination block.
609//
610// This method does this because all of the PHI nodes must be executed
611// atomically, reading their inputs before any of the results are updated. Not
612// doing this can cause problems if the PHI nodes depend on other PHI nodes for
613// their inputs. If the input PHI node is updated before it is read, incorrect
614// results can happen. Thus we use a two phase approach.
615//
616void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
617 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
618 SF.CurBB = Dest; // Update CurBB to branch destination
619 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
620
621 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
622
623 // Loop over all of the PHI nodes in the current block, reading their inputs.
624 std::vector<GenericValue> ResultValues;
625
626 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000627 if (Trace) CW << "Run:" << PN;
628
Chris Lattner77113b62003-05-10 20:21:16 +0000629 // Search for the value corresponding to this previous bb...
630 int i = PN->getBasicBlockIndex(PrevBB);
631 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
632 Value *IncomingValue = PN->getIncomingValue(i);
633
634 // Save the incoming value for this PHI node...
635 ResultValues.push_back(getOperandValue(IncomingValue, SF));
636 }
637
638 // Now loop over all of the PHI nodes setting their values...
639 SF.CurInst = SF.CurBB->begin();
640 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
641 ++SF.CurInst, ++i)
642 SetValue(PN, ResultValues[i], SF);
Chris Lattner09e93922003-04-22 20:34:47 +0000643}
644
645
Chris Lattner92101ac2001-08-23 17:05:04 +0000646//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000647// Memory Instruction Implementations
648//===----------------------------------------------------------------------===//
649
Chris Lattnerd7916e92003-05-10 21:22:39 +0000650void Interpreter::visitAllocationInst(AllocationInst &I) {
651 ExecutionContext &SF = ECStack.back();
652
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000653 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000654
Chris Lattnercc82cc12002-04-28 21:57:33 +0000655 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000656 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000657
658 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000659 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000660 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
661
Chris Lattnerfe11a972002-12-23 23:59:41 +0000662 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000663 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000664 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000665
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000666 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000667 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000668}
669
Chris Lattnerd7916e92003-05-10 21:22:39 +0000670void Interpreter::visitFreeInst(FreeInst &I) {
671 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000672 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
673 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000674 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000675 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000676}
677
Chris Lattner95c3af52001-10-29 19:32:19 +0000678
Chris Lattnera34c5682002-08-27 22:33:45 +0000679// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000680//
Chris Lattnerfe11a972002-12-23 23:59:41 +0000681GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
682 User::op_iterator E,
683 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000684 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000685 "Cannot getElementOffset of a nonpointer type!");
686
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000687 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000688 const Type *Ty = Ptr->getType();
689
690 for (; I != E; ++I) {
Chris Lattner782b9392001-11-26 18:18:18 +0000691 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
692 const StructLayout *SLO = TD.getStructLayout(STy);
693
694 // Indicies must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000695 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000696 assert(CPU->getType() == Type::UByteTy);
697 unsigned Index = CPU->getValue();
698
Chris Lattner782b9392001-11-26 18:18:18 +0000699 Total += SLO->MemberOffsets[Index];
700 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000701 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000702
Chris Lattner006a4a52003-02-25 21:14:59 +0000703 // Get the index number for the array... which must be long type...
Chris Lattner0374b8d2002-09-11 01:21:35 +0000704 assert((*I)->getType() == Type::LongTy);
Chris Lattnere8b3e9b2002-09-13 23:30:42 +0000705 unsigned Idx = getOperandValue(*I, SF).LongVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000706 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000707 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattner02868352003-04-22 21:22:33 +0000708 std::cerr << "Out of range memory access to element #" << Idx
709 << " of a " << AT->getNumElements() << " element array."
710 << " Subscript #" << *I << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000711 // Get outta here!!!
Chris Lattner74030252002-02-12 15:47:23 +0000712 siglongjmp(SignalRecoverBuffer, SIGTRAP);
Chris Lattnerf23eb852001-12-14 16:49:29 +0000713 }
Chris Lattner782b9392001-11-26 18:18:18 +0000714
Chris Lattnerf23eb852001-12-14 16:49:29 +0000715 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000716 unsigned Size = TD.getTypeSize(Ty);
717 Total += Size*Idx;
718 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000719 }
720
Chris Lattnera34c5682002-08-27 22:33:45 +0000721 GenericValue Result;
722 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
723 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000724}
725
Chris Lattnerd7916e92003-05-10 21:22:39 +0000726void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
727 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000728 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattnera34c5682002-08-27 22:33:45 +0000729 I.idx_begin(), I.idx_end(), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000730}
731
Chris Lattnerd7916e92003-05-10 21:22:39 +0000732void Interpreter::visitLoadInst(LoadInst &I) {
733 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000734 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000735 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000736 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000737 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000738}
739
Chris Lattnerd7916e92003-05-10 21:22:39 +0000740void Interpreter::visitStoreInst(StoreInst &I) {
741 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000742 GenericValue Val = getOperandValue(I.getOperand(0), SF);
743 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000744 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000745 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000746}
747
Chris Lattner86660982001-08-27 05:16:50 +0000748
Chris Lattnerab2dea52002-11-07 19:29:31 +0000749
Chris Lattner86660982001-08-27 05:16:50 +0000750//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000751// Miscellaneous Instruction Implementations
752//===----------------------------------------------------------------------===//
753
Chris Lattnerd7916e92003-05-10 21:22:39 +0000754void Interpreter::visitCallInst(CallInst &I) {
755 ExecutionContext &SF = ECStack.back();
756 SF.Caller = &I;
Chris Lattner02868352003-04-22 21:22:33 +0000757 std::vector<GenericValue> ArgVals;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000758 ArgVals.reserve(I.getNumOperands()-1);
Chris Lattner93780132003-01-13 00:58:52 +0000759 for (unsigned i = 1; i < I.getNumOperands(); ++i) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000760 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
Chris Lattner93780132003-01-13 00:58:52 +0000761 // Promote all integral types whose size is < sizeof(int) into ints. We do
762 // this by zero or sign extending the value as appropriate according to the
763 // source type.
764 if (I.getOperand(i)->getType()->isIntegral() &&
765 I.getOperand(i)->getType()->getPrimitiveSize() < 4) {
766 const Type *Ty = I.getOperand(i)->getType();
767 if (Ty == Type::ShortTy)
768 ArgVals.back().IntVal = ArgVals.back().ShortVal;
769 else if (Ty == Type::UShortTy)
770 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
771 else if (Ty == Type::SByteTy)
772 ArgVals.back().IntVal = ArgVals.back().SByteVal;
773 else if (Ty == Type::UByteTy)
774 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
775 else if (Ty == Type::BoolTy)
776 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
777 else
778 assert(0 && "Unknown type!");
779 }
780 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000781
Chris Lattner070cf5e2001-11-07 20:12:30 +0000782 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000783 // and treat it as a function pointer.
Chris Lattnerd7916e92003-05-10 21:22:39 +0000784 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000785 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000786}
787
Chris Lattner86660982001-08-27 05:16:50 +0000788#define IMPLEMENT_SHIFT(OP, TY) \
789 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
790
Chris Lattnerd7916e92003-05-10 21:22:39 +0000791void Interpreter::visitShl(ShiftInst &I) {
792 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000793 const Type *Ty = I.getOperand(0)->getType();
794 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
795 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000796 GenericValue Dest;
797
798 switch (Ty->getPrimitiveID()) {
799 IMPLEMENT_SHIFT(<<, UByte);
800 IMPLEMENT_SHIFT(<<, SByte);
801 IMPLEMENT_SHIFT(<<, UShort);
802 IMPLEMENT_SHIFT(<<, Short);
803 IMPLEMENT_SHIFT(<<, UInt);
804 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000805 IMPLEMENT_SHIFT(<<, ULong);
806 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000807 default:
Chris Lattner02868352003-04-22 21:22:33 +0000808 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000809 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000810 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000811}
812
Chris Lattnerd7916e92003-05-10 21:22:39 +0000813void Interpreter::visitShr(ShiftInst &I) {
814 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000815 const Type *Ty = I.getOperand(0)->getType();
816 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
817 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000818 GenericValue Dest;
819
820 switch (Ty->getPrimitiveID()) {
821 IMPLEMENT_SHIFT(>>, UByte);
822 IMPLEMENT_SHIFT(>>, SByte);
823 IMPLEMENT_SHIFT(>>, UShort);
824 IMPLEMENT_SHIFT(>>, Short);
825 IMPLEMENT_SHIFT(>>, UInt);
826 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000827 IMPLEMENT_SHIFT(>>, ULong);
828 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000829 default:
Chris Lattner02868352003-04-22 21:22:33 +0000830 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
831 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000832 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000833 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000834}
835
836#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000837 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000838
839#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
840 case Type::DESTTY##TyID: \
841 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000842 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000843 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
844 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
845 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
846 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
847 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000848 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
849 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000850 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
851 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000852
853#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
854 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
855 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
856
857#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner02868352003-04-22 21:22:33 +0000858 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
859 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000860 } \
861 break
862
863#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
864 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
865 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000866 IMPLEMENT_CAST_CASE_END()
867
Chris Lattnera34c5682002-08-27 22:33:45 +0000868static GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
869 ExecutionContext &SF) {
870 const Type *SrcTy = SrcVal->getType();
871 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000872
873 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000874 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
875 IMPLEMENT_CAST_CASE(SByte , ( signed char));
876 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000877 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000878 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
879 IMPLEMENT_CAST_CASE(Int , ( signed int ));
880 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
881 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000882 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000883 IMPLEMENT_CAST_CASE(Float , (float));
884 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +0000885 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +0000886 default:
Chris Lattner02868352003-04-22 21:22:33 +0000887 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +0000888 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000889 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000890
891 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000892}
Chris Lattner92101ac2001-08-23 17:05:04 +0000893
894
Chris Lattnerd7916e92003-05-10 21:22:39 +0000895void Interpreter::visitCastInst(CastInst &I) {
896 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +0000897 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
898}
Chris Lattner92101ac2001-08-23 17:05:04 +0000899
Chris Lattnerd7916e92003-05-10 21:22:39 +0000900void Interpreter::visitVarArgInst(VarArgInst &I) {
901 ExecutionContext &SF = ECStack.back();
902
Chris Lattner374344c2003-05-08 16:52:43 +0000903 // Get the pointer to the valist element. LLI treats the valist in memory as
904 // an integer.
905 GenericValue VAListPtr = getOperandValue(I.getOperand(0), SF);
906
907 // Load the pointer
908 GenericValue VAList =
909 TheEE->LoadValueFromMemory((GenericValue *)GVTOP(VAListPtr), Type::UIntTy);
910
911 unsigned Argument = VAList.IntVal++;
912
913 // Update the valist to point to the next argument...
914 TheEE->StoreValueToMemory(VAList, (GenericValue *)GVTOP(VAListPtr),
915 Type::UIntTy);
916
917 // Set the value...
918 assert(Argument < SF.VarArgs.size() &&
919 "Accessing past the last vararg argument!");
920 SetValue(&I, SF.VarArgs[Argument], SF);
921}
Chris Lattner92101ac2001-08-23 17:05:04 +0000922
923//===----------------------------------------------------------------------===//
924// Dispatch and Execution Code
925//===----------------------------------------------------------------------===//
926
Chris Lattnerda82ed52003-05-08 16:18:31 +0000927FunctionInfo::FunctionInfo(Function *F) : Annotation(FunctionInfoAID) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000928 // Assign slot numbers to the function arguments...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000929 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
930 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
Chris Lattner92101ac2001-08-23 17:05:04 +0000931
932 // Iterate over all of the instructions...
933 unsigned InstNum = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000934 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
935 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
936 // For each instruction... Add Annote
937 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
Chris Lattner92101ac2001-08-23 17:05:04 +0000938}
939
Chris Lattnerda82ed52003-05-08 16:18:31 +0000940unsigned FunctionInfo::getValueSlot(const Value *V) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000941 unsigned Plane = V->getType()->getUniqueID();
942 if (Plane >= NumPlaneElements.size())
943 NumPlaneElements.resize(Plane+1, 0);
944 return NumPlaneElements[Plane]++;
945}
946
947
Chris Lattner92101ac2001-08-23 17:05:04 +0000948//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000949// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +0000950//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000951void Interpreter::callFunction(Function *F,
952 const std::vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000953 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
954 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
955 "Incorrect number of arguments passed into function call!");
Chris Lattnercdf51782003-05-08 16:06:52 +0000956 if (F->isExternal()) {
Chris Lattnerda82ed52003-05-08 16:18:31 +0000957 GenericValue Result = callExternalFunction(F, ArgVals);
Chris Lattnercdf51782003-05-08 16:06:52 +0000958 const Type *RetTy = F->getReturnType();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000959
960 // Copy the result back into the result variable if we are not returning
961 // void.
962 if (RetTy != Type::VoidTy) {
963 if (!ECStack.empty() && ECStack.back().Caller) {
964 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000965 SetValue(SF.Caller, Result, SF);
966
967 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000968 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000969 // print it.
Chris Lattnercdf51782003-05-08 16:06:52 +0000970 CW << "Function " << F->getType() << " \"" << F->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000971 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000972 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000973 std::cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000974
975 if (RetTy->isIntegral())
Chris Lattner0c4e8862002-09-03 01:08:28 +0000976 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattnerbb76f022001-10-30 20:27:31 +0000977 }
978 }
979
Chris Lattner92101ac2001-08-23 17:05:04 +0000980 return;
981 }
982
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000983 // Process the function, assigning instruction numbers to the instructions in
984 // the function. Also calculate the number of values for each type slot
985 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +0000986 //
Chris Lattnerda82ed52003-05-08 16:18:31 +0000987 FunctionInfo *FuncInfo =
988 (FunctionInfo*)F->getOrCreateAnnotation(FunctionInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +0000989 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +0000990
Chris Lattner92101ac2001-08-23 17:05:04 +0000991 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
Chris Lattnerda82ed52003-05-08 16:18:31 +0000992 StackFrame.CurFunction = F;
Chris Lattnercdf51782003-05-08 16:06:52 +0000993 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +0000994 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000995 StackFrame.FuncInfo = FuncInfo;
Chris Lattner92101ac2001-08-23 17:05:04 +0000996
997 // Initialize the values to nothing...
Chris Lattnerda82ed52003-05-08 16:18:31 +0000998 StackFrame.Values.resize(FuncInfo->NumPlaneElements.size());
999 for (unsigned i = 0; i < FuncInfo->NumPlaneElements.size(); ++i) {
1000 StackFrame.Values[i].resize(FuncInfo->NumPlaneElements[i]);
Chris Lattner92101ac2001-08-23 17:05:04 +00001001
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001002 // Taint the initial values of stuff
1003 memset(&StackFrame.Values[i][0], 42,
Chris Lattnerda82ed52003-05-08 16:18:31 +00001004 FuncInfo->NumPlaneElements[i]*sizeof(GenericValue));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001005 }
1006
Chris Lattner92101ac2001-08-23 17:05:04 +00001007
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001008 // Run through the function arguments and initialize their values...
Chris Lattnercdf51782003-05-08 16:06:52 +00001009 assert((ArgVals.size() == F->asize() ||
1010 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001011 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001012
1013 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001014 unsigned i = 0;
Chris Lattnercdf51782003-05-08 16:06:52 +00001015 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001016 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001017
1018 // Handle varargs arguments...
1019 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001020}
1021
1022// executeInstruction - Interpret a single instruction, increment the "PC", and
1023// return true if the next instruction is a breakpoint...
1024//
1025bool Interpreter::executeInstruction() {
1026 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1027
1028 ExecutionContext &SF = ECStack.back(); // Current stack frame
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001029 Instruction &I = *SF.CurInst++; // Increment before execute
Chris Lattner92101ac2001-08-23 17:05:04 +00001030
Chris Lattnerd7916e92003-05-10 21:22:39 +00001031 if (Trace) CW << "Run:" << I;
Chris Lattner5af0c482001-11-07 04:23:00 +00001032
Chris Lattnerbbdabce2002-12-08 05:51:08 +00001033 // Track the number of dynamic instructions executed.
1034 ++NumDynamicInsts;
1035
Chris Lattner5af0c482001-11-07 04:23:00 +00001036 // Set a sigsetjmp buffer so that we can recover if an error happens during
1037 // instruction execution...
1038 //
1039 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1040 --SF.CurInst; // Back up to erroring instruction
Brian Gaekef58815e2003-09-04 22:21:24 +00001041 std::cout << "EXCEPTION OCCURRED [" << strsignal(SigNo) << "]\n";
1042 exit(1);
Chris Lattner461f02f2001-11-07 05:31:27 +00001043 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001044 return true;
1045 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001046
Chris Lattner461f02f2001-11-07 05:31:27 +00001047 InInstruction = true;
Chris Lattnerd7916e92003-05-10 21:22:39 +00001048 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner461f02f2001-11-07 05:31:27 +00001049 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001050
1051 // Reset the current frame location to the top of stack
1052 CurFrame = ECStack.size()-1;
1053
1054 if (CurFrame == -1) return false; // No breakpoint if no code
1055
1056 // Return true if there is a breakpoint annotation on the instruction...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001057 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
Chris Lattner92101ac2001-08-23 17:05:04 +00001058}
1059
1060void Interpreter::stepInstruction() { // Do the 'step' command
1061 if (ECStack.empty()) {
Chris Lattner02868352003-04-22 21:22:33 +00001062 std::cout << "Error: no program running, cannot step!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001063 return;
1064 }
1065
1066 // Run an instruction...
1067 executeInstruction();
1068
1069 // Print the next instruction to execute...
1070 printCurrentInstruction();
1071}
1072
1073// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001074void Interpreter::nextInstruction() { // Do the 'next' command
1075 if (ECStack.empty()) {
Chris Lattner02868352003-04-22 21:22:33 +00001076 std::cout << "Error: no program running, cannot 'next'!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001077 return;
1078 }
1079
1080 // If this is a call instruction, step over the call instruction...
1081 // TODO: ICALL, CALL WITH, ...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001082 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001083 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001084 // Step into the function...
1085 if (executeInstruction()) {
1086 // Hit a breakpoint, print current instruction, then return to user...
Chris Lattner02868352003-04-22 21:22:33 +00001087 std::cout << "Breakpoint hit!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001088 printCurrentInstruction();
1089 return;
1090 }
1091
Chris Lattnera74a6b52001-10-29 14:08:33 +00001092 // If we we able to step into the function, finish it now. We might not be
1093 // able the step into a function, if it's external for example.
1094 if (ECStack.size() != StackSize)
1095 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001096 else
1097 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001098
Chris Lattner92101ac2001-08-23 17:05:04 +00001099 } else {
1100 // Normal instruction, just step...
1101 stepInstruction();
1102 }
1103}
1104
1105void Interpreter::run() {
1106 if (ECStack.empty()) {
Chris Lattner02868352003-04-22 21:22:33 +00001107 std::cout << "Error: no program running, cannot run!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001108 return;
1109 }
1110
1111 bool HitBreakpoint = false;
1112 while (!ECStack.empty() && !HitBreakpoint) {
1113 // Run an instruction...
1114 HitBreakpoint = executeInstruction();
1115 }
1116
Chris Lattner02868352003-04-22 21:22:33 +00001117 if (HitBreakpoint)
1118 std::cout << "Breakpoint hit!\n";
1119
Chris Lattner92101ac2001-08-23 17:05:04 +00001120 // Print the next instruction to execute...
1121 printCurrentInstruction();
1122}
1123
1124void Interpreter::finish() {
1125 if (ECStack.empty()) {
Chris Lattner02868352003-04-22 21:22:33 +00001126 std::cout << "Error: no program running, cannot run!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001127 return;
1128 }
1129
1130 unsigned StackSize = ECStack.size();
1131 bool HitBreakpoint = false;
1132 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1133 // Run an instruction...
1134 HitBreakpoint = executeInstruction();
1135 }
1136
Chris Lattner02868352003-04-22 21:22:33 +00001137 if (HitBreakpoint)
1138 std::cout << "Breakpoint hit!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001139
1140 // Print the next instruction to execute...
1141 printCurrentInstruction();
1142}
1143
1144
1145
1146// printCurrentInstruction - Print out the instruction that the virtual PC is
1147// at, or fail silently if no program is running.
1148//
1149void Interpreter::printCurrentInstruction() {
1150 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001151 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
Chris Lattner02868352003-04-22 21:22:33 +00001152 WriteAsOperand(std::cout, ECStack.back().CurBB) << ":\n";
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001153
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001154 Instruction &I = *ECStack.back().CurInst;
1155 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001156 assert(IN && "Instruction has no numbering annotation!");
Chris Lattner02868352003-04-22 21:22:33 +00001157 std::cout << "#" << IN->InstNum << I;
Chris Lattner92101ac2001-08-23 17:05:04 +00001158 }
1159}
1160
1161void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001162 switch (Ty->getPrimitiveID()) {
Chris Lattner02868352003-04-22 21:22:33 +00001163 case Type::BoolTyID: std::cout << (V.BoolVal?"true":"false"); break;
Chris Lattner65629d52002-08-13 20:45:11 +00001164 case Type::SByteTyID:
Chris Lattner02868352003-04-22 21:22:33 +00001165 std::cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
Chris Lattner65629d52002-08-13 20:45:11 +00001166 case Type::UByteTyID:
Chris Lattner02868352003-04-22 21:22:33 +00001167 std::cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
1168 case Type::ShortTyID: std::cout << V.ShortVal; break;
1169 case Type::UShortTyID: std::cout << V.UShortVal; break;
1170 case Type::IntTyID: std::cout << V.IntVal; break;
1171 case Type::UIntTyID: std::cout << V.UIntVal; break;
1172 case Type::LongTyID: std::cout << (long)V.LongVal; break;
1173 case Type::ULongTyID: std::cout << (unsigned long)V.ULongVal; break;
1174 case Type::FloatTyID: std::cout << V.FloatVal; break;
1175 case Type::DoubleTyID: std::cout << V.DoubleVal; break;
1176 case Type::PointerTyID:std::cout << (void*)GVTOP(V); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001177 default:
Chris Lattner02868352003-04-22 21:22:33 +00001178 std::cout << "- Don't know how to print value of this type!";
Chris Lattner92101ac2001-08-23 17:05:04 +00001179 break;
1180 }
1181}
1182
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001183void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001184 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001185 printValue(Ty, V);
1186}
1187
Chris Lattner697954c2002-01-20 22:54:45 +00001188void Interpreter::print(const std::string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001189 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1190 if (!PickedVal) return;
1191
Chris Lattner832688a2003-07-23 15:30:32 +00001192 if (const Function *F = dyn_cast<Function>(PickedVal)) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001193 CW << F; // Print the function
Chris Lattner832688a2003-07-23 15:30:32 +00001194 } else if (const Type *Ty = dyn_cast<Type>(PickedVal)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001195 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
Chris Lattner832688a2003-07-23 15:30:32 +00001196 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(PickedVal)) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001197 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001198 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001199 print(PickedVal->getType(),
1200 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner02868352003-04-22 21:22:33 +00001201 std::cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001202 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001203}
1204
Chris Lattner697954c2002-01-20 22:54:45 +00001205void Interpreter::infoValue(const std::string &Name) {
Chris Lattner86660982001-08-27 05:16:50 +00001206 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1207 if (!PickedVal) return;
1208
Chris Lattner02868352003-04-22 21:22:33 +00001209 std::cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001210 print(PickedVal->getType(),
1211 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner02868352003-04-22 21:22:33 +00001212 std::cout << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001213 printOperandInfo(PickedVal, ECStack[CurFrame]);
1214}
1215
Chris Lattner461f02f2001-11-07 05:31:27 +00001216// printStackFrame - Print information about the specified stack frame, or -1
1217// for the default one.
1218//
Chris Lattner601d7152002-07-25 17:37:05 +00001219void Interpreter::printStackFrame(int FrameNo) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001220 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattnerda82ed52003-05-08 16:18:31 +00001221 Function *F = ECStack[FrameNo].CurFunction;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001222 const Type *RetTy = F->getReturnType();
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001223
1224 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001225 << (Value*)RetTy << " \"" << F->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001226
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001227 unsigned i = 0;
1228 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
Chris Lattner02868352003-04-22 21:22:33 +00001229 if (i != 0) std::cout << ", ";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001230 CW << *I << "=";
Chris Lattner461f02f2001-11-07 05:31:27 +00001231
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001232 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001233 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001234
Chris Lattner02868352003-04-22 21:22:33 +00001235 std::cout << ")\n";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001236
1237 if (FrameNo != int(ECStack.size()-1)) {
1238 BasicBlock::iterator I = ECStack[FrameNo].CurInst;
1239 CW << --I;
1240 } else {
1241 CW << *ECStack[FrameNo].CurInst;
1242 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001243}
Chris Lattner461f02f2001-11-07 05:31:27 +00001244