blob: 6542111709c894019cf02269e0c782d123ef39e7 [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 Lattnerfe11a972002-12-23 23:59:41 +00009#include "llvm/GlobalVariable.h"
10#include "llvm/Function.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000011#include "llvm/iPHINode.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000012#include "llvm/iOther.h"
13#include "llvm/iTerminators.h"
Chris Lattner86660982001-08-27 05:16:50 +000014#include "llvm/iMemory.h"
Chris Lattnere2cbbce2002-04-29 18:56:45 +000015#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000017#include "llvm/Assembly/Writer.h"
Chris Lattnerf23eb852001-12-14 16:49:29 +000018#include "Support/CommandLine.h"
Chris Lattnerbbdabce2002-12-08 05:51:08 +000019#include "Support/Statistic.h"
Chris Lattnerbb76f022001-10-30 20:27:31 +000020#include <math.h> // For fmod
Chris Lattner5af0c482001-11-07 04:23:00 +000021#include <signal.h>
22#include <setjmp.h>
Chris Lattner2e42d3a2001-10-15 05:51:48 +000023
Chris Lattnerfe11a972002-12-23 23:59:41 +000024Interpreter *TheEE = 0;
25
Chris Lattnerbbdabce2002-12-08 05:51:08 +000026namespace {
27 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
Chris Lattner138b0cd2002-12-08 06:01:34 +000028
29 cl::opt<bool>
Chris Lattnerfe11a972002-12-23 23:59:41 +000030 QuietMode("quiet", cl::desc("Do not emit any non-program output"),
31 cl::init(true));
Chris Lattner138b0cd2002-12-08 06:01:34 +000032
33 cl::alias
34 QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode));
35
36 cl::opt<bool>
37 ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks"));
38
39 cl::opt<bool>
40 AbortOnExceptions("abort-on-exception",
41 cl::desc("Halt execution on a machine exception"));
Chris Lattnerbbdabce2002-12-08 05:51:08 +000042}
43
Chris Lattner2e42d3a2001-10-15 05:51:48 +000044// Create a TargetData structure to handle memory addressing and size/alignment
45// computations
46//
Chris Lattnerea38c0e2001-11-07 19:46:27 +000047CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000048
Chris Lattnere2409062001-11-12 16:19:45 +000049#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner5ff62e92002-07-22 02:10:13 +000050static cl::opt<bool>
51ProfileStructureFields("profilestructfields",
52 cl::desc("Profile Structure Field Accesses"));
Chris Lattnere2409062001-11-12 16:19:45 +000053#include <map>
Chris Lattner02868352003-04-22 21:22:33 +000054static std::map<const StructType *, std::vector<unsigned> > FieldAccessCounts;
Chris Lattnere2409062001-11-12 16:19:45 +000055#endif
56
Chris Lattner5af0c482001-11-07 04:23:00 +000057sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000058static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000059
60extern "C" {
61static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000062 if (InInstruction)
63 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000064}
65}
66
67static void initializeSignalHandlers() {
68 struct sigaction Action;
69 Action.sa_handler = SigHandler;
70 Action.sa_flags = SA_SIGINFO;
71 sigemptyset(&Action.sa_mask);
72 sigaction(SIGSEGV, &Action, 0);
73 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000074 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000075 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000076}
77
Chris Lattner2e42d3a2001-10-15 05:51:48 +000078
79//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000080// Value Manipulation code
81//===----------------------------------------------------------------------===//
82
83static unsigned getOperandSlot(Value *V) {
84 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
85 assert(SN && "Operand does not have a slot number annotation!");
86 return SN->SlotNum;
87}
88
Chris Lattnera34c5682002-08-27 22:33:45 +000089// Operations used by constant expr implementations...
90static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
91 ExecutionContext &SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000092static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000093 const Type *Ty);
Chris Lattnera34c5682002-08-27 22:33:45 +000094
Chris Lattnerfddc7552002-10-15 20:34:05 +000095
Chris Lattner39bb5b42001-10-15 13:25:40 +000096static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +000097 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
98 switch (CE->getOpcode()) {
99 case Instruction::Cast:
100 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
101 case Instruction::GetElementPtr:
Chris Lattnerfe11a972002-12-23 23:59:41 +0000102 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
103 CE->op_end(), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000104 case Instruction::Add:
105 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
106 getOperandValue(CE->getOperand(1), SF),
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000107 CE->getType());
Chris Lattnera34c5682002-08-27 22:33:45 +0000108 default:
Chris Lattner02868352003-04-22 21:22:33 +0000109 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
Chris Lattnera34c5682002-08-27 22:33:45 +0000110 abort();
Chris Lattner04e2ad72003-04-21 22:43:32 +0000111 return GenericValue();
Chris Lattnera34c5682002-08-27 22:33:45 +0000112 }
113 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000114 return TheEE->getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000115 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000116 return PTOGV(TheEE->getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000117 } else {
118 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000119 unsigned OpSlot = getOperandSlot(V);
120 assert(TyP < SF.Values.size() &&
121 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000122 return SF.Values[TyP][getOperandSlot(V)];
123 }
124}
125
126static void printOperandInfo(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000127 if (isa<Constant>(V)) {
Chris Lattner02868352003-04-22 21:22:33 +0000128 std::cout << "Constant Pool Value\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000129 } else if (isa<GlobalValue>(V)) {
Chris Lattner02868352003-04-22 21:22:33 +0000130 std::cout << "Global Value\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000131 } else {
132 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
133 unsigned Slot = getOperandSlot(V);
Chris Lattner02868352003-04-22 21:22:33 +0000134 std::cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
135 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
136 << " Contents=0x";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000137
138 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
139 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
140 unsigned char Cur = Buf[i];
Chris Lattner02868352003-04-22 21:22:33 +0000141 std::cout << ( Cur >= 160?char((Cur>>4)+'A'-10):char((Cur>>4) + '0'))
142 << ((Cur&15) >= 10?char((Cur&15)+'A'-10):char((Cur&15) + '0'));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000143 }
Chris Lattner02868352003-04-22 21:22:33 +0000144 std::cout << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000145 }
146}
147
148
149
150static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
151 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
152
Chris Lattner02868352003-04-22 21:22:33 +0000153 //std::cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)]<< "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000154 SF.Values[TyP][getOperandSlot(V)] = Val;
155}
156
157
158//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000159// Annotation Wrangling code
160//===----------------------------------------------------------------------===//
161
162void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000163 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000164 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
165 &MethodInfo::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000166 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000167}
168
Chris Lattner2adcd832002-05-03 19:52:30 +0000169//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000170// Binary Instruction Implementations
171//===----------------------------------------------------------------------===//
172
173#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
174 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
175
176static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000177 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000178 GenericValue Dest;
179 switch (Ty->getPrimitiveID()) {
180 IMPLEMENT_BINARY_OPERATOR(+, UByte);
181 IMPLEMENT_BINARY_OPERATOR(+, SByte);
182 IMPLEMENT_BINARY_OPERATOR(+, UShort);
183 IMPLEMENT_BINARY_OPERATOR(+, Short);
184 IMPLEMENT_BINARY_OPERATOR(+, UInt);
185 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000186 IMPLEMENT_BINARY_OPERATOR(+, ULong);
187 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000188 IMPLEMENT_BINARY_OPERATOR(+, Float);
189 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000190 default:
Chris Lattner02868352003-04-22 21:22:33 +0000191 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
192 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000193 }
194 return Dest;
195}
196
197static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000198 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000199 GenericValue Dest;
200 switch (Ty->getPrimitiveID()) {
201 IMPLEMENT_BINARY_OPERATOR(-, UByte);
202 IMPLEMENT_BINARY_OPERATOR(-, SByte);
203 IMPLEMENT_BINARY_OPERATOR(-, UShort);
204 IMPLEMENT_BINARY_OPERATOR(-, Short);
205 IMPLEMENT_BINARY_OPERATOR(-, UInt);
206 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000207 IMPLEMENT_BINARY_OPERATOR(-, ULong);
208 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000209 IMPLEMENT_BINARY_OPERATOR(-, Float);
210 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000211 default:
Chris Lattner02868352003-04-22 21:22:33 +0000212 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
213 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000214 }
215 return Dest;
216}
217
Chris Lattnerc2593162001-10-27 08:28:11 +0000218static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000219 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000220 GenericValue Dest;
221 switch (Ty->getPrimitiveID()) {
222 IMPLEMENT_BINARY_OPERATOR(*, UByte);
223 IMPLEMENT_BINARY_OPERATOR(*, SByte);
224 IMPLEMENT_BINARY_OPERATOR(*, UShort);
225 IMPLEMENT_BINARY_OPERATOR(*, Short);
226 IMPLEMENT_BINARY_OPERATOR(*, UInt);
227 IMPLEMENT_BINARY_OPERATOR(*, Int);
228 IMPLEMENT_BINARY_OPERATOR(*, ULong);
229 IMPLEMENT_BINARY_OPERATOR(*, Long);
230 IMPLEMENT_BINARY_OPERATOR(*, Float);
231 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000232 default:
Chris Lattner02868352003-04-22 21:22:33 +0000233 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
234 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000235 }
236 return Dest;
237}
238
239static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000240 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000241 GenericValue Dest;
242 switch (Ty->getPrimitiveID()) {
243 IMPLEMENT_BINARY_OPERATOR(/, UByte);
244 IMPLEMENT_BINARY_OPERATOR(/, SByte);
245 IMPLEMENT_BINARY_OPERATOR(/, UShort);
246 IMPLEMENT_BINARY_OPERATOR(/, Short);
247 IMPLEMENT_BINARY_OPERATOR(/, UInt);
248 IMPLEMENT_BINARY_OPERATOR(/, Int);
249 IMPLEMENT_BINARY_OPERATOR(/, ULong);
250 IMPLEMENT_BINARY_OPERATOR(/, Long);
251 IMPLEMENT_BINARY_OPERATOR(/, Float);
252 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000253 default:
Chris Lattner02868352003-04-22 21:22:33 +0000254 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
255 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000256 }
257 return Dest;
258}
259
260static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000261 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000262 GenericValue Dest;
263 switch (Ty->getPrimitiveID()) {
264 IMPLEMENT_BINARY_OPERATOR(%, UByte);
265 IMPLEMENT_BINARY_OPERATOR(%, SByte);
266 IMPLEMENT_BINARY_OPERATOR(%, UShort);
267 IMPLEMENT_BINARY_OPERATOR(%, Short);
268 IMPLEMENT_BINARY_OPERATOR(%, UInt);
269 IMPLEMENT_BINARY_OPERATOR(%, Int);
270 IMPLEMENT_BINARY_OPERATOR(%, ULong);
271 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000272 case Type::FloatTyID:
273 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
274 break;
275 case Type::DoubleTyID:
276 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
277 break;
278 default:
Chris Lattner02868352003-04-22 21:22:33 +0000279 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
280 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000281 }
282 return Dest;
283}
284
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000285static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000286 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000287 GenericValue Dest;
288 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000289 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000290 IMPLEMENT_BINARY_OPERATOR(&, UByte);
291 IMPLEMENT_BINARY_OPERATOR(&, SByte);
292 IMPLEMENT_BINARY_OPERATOR(&, UShort);
293 IMPLEMENT_BINARY_OPERATOR(&, Short);
294 IMPLEMENT_BINARY_OPERATOR(&, UInt);
295 IMPLEMENT_BINARY_OPERATOR(&, Int);
296 IMPLEMENT_BINARY_OPERATOR(&, ULong);
297 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000298 default:
Chris Lattner02868352003-04-22 21:22:33 +0000299 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
300 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000301 }
302 return Dest;
303}
304
305
306static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000307 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000308 GenericValue Dest;
309 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000310 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000311 IMPLEMENT_BINARY_OPERATOR(|, UByte);
312 IMPLEMENT_BINARY_OPERATOR(|, SByte);
313 IMPLEMENT_BINARY_OPERATOR(|, UShort);
314 IMPLEMENT_BINARY_OPERATOR(|, Short);
315 IMPLEMENT_BINARY_OPERATOR(|, UInt);
316 IMPLEMENT_BINARY_OPERATOR(|, Int);
317 IMPLEMENT_BINARY_OPERATOR(|, ULong);
318 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000319 default:
Chris Lattner02868352003-04-22 21:22:33 +0000320 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
321 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000322 }
323 return Dest;
324}
325
326
327static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000328 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000329 GenericValue Dest;
330 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000331 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000332 IMPLEMENT_BINARY_OPERATOR(^, UByte);
333 IMPLEMENT_BINARY_OPERATOR(^, SByte);
334 IMPLEMENT_BINARY_OPERATOR(^, UShort);
335 IMPLEMENT_BINARY_OPERATOR(^, Short);
336 IMPLEMENT_BINARY_OPERATOR(^, UInt);
337 IMPLEMENT_BINARY_OPERATOR(^, Int);
338 IMPLEMENT_BINARY_OPERATOR(^, ULong);
339 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000340 default:
Chris Lattner02868352003-04-22 21:22:33 +0000341 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
342 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000343 }
344 return Dest;
345}
346
347
Chris Lattner92101ac2001-08-23 17:05:04 +0000348#define IMPLEMENT_SETCC(OP, TY) \
349 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
350
Chris Lattner92101ac2001-08-23 17:05:04 +0000351static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000352 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000353 GenericValue Dest;
354 switch (Ty->getPrimitiveID()) {
355 IMPLEMENT_SETCC(==, UByte);
356 IMPLEMENT_SETCC(==, SByte);
357 IMPLEMENT_SETCC(==, UShort);
358 IMPLEMENT_SETCC(==, Short);
359 IMPLEMENT_SETCC(==, UInt);
360 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000361 IMPLEMENT_SETCC(==, ULong);
362 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000363 IMPLEMENT_SETCC(==, Float);
364 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000365 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000366 default:
Chris Lattner02868352003-04-22 21:22:33 +0000367 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
368 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000369 }
370 return Dest;
371}
372
373static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000374 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000375 GenericValue Dest;
376 switch (Ty->getPrimitiveID()) {
377 IMPLEMENT_SETCC(!=, UByte);
378 IMPLEMENT_SETCC(!=, SByte);
379 IMPLEMENT_SETCC(!=, UShort);
380 IMPLEMENT_SETCC(!=, Short);
381 IMPLEMENT_SETCC(!=, UInt);
382 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000383 IMPLEMENT_SETCC(!=, ULong);
384 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000385 IMPLEMENT_SETCC(!=, Float);
386 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000387 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000388
Chris Lattner92101ac2001-08-23 17:05:04 +0000389 default:
Chris Lattner02868352003-04-22 21:22:33 +0000390 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
391 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000392 }
393 return Dest;
394}
395
396static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000397 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000398 GenericValue Dest;
399 switch (Ty->getPrimitiveID()) {
400 IMPLEMENT_SETCC(<=, UByte);
401 IMPLEMENT_SETCC(<=, SByte);
402 IMPLEMENT_SETCC(<=, UShort);
403 IMPLEMENT_SETCC(<=, Short);
404 IMPLEMENT_SETCC(<=, UInt);
405 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000406 IMPLEMENT_SETCC(<=, ULong);
407 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000408 IMPLEMENT_SETCC(<=, Float);
409 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000410 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000411 default:
Chris Lattner02868352003-04-22 21:22:33 +0000412 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
413 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000414 }
415 return Dest;
416}
417
418static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000419 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000420 GenericValue Dest;
421 switch (Ty->getPrimitiveID()) {
422 IMPLEMENT_SETCC(>=, UByte);
423 IMPLEMENT_SETCC(>=, SByte);
424 IMPLEMENT_SETCC(>=, UShort);
425 IMPLEMENT_SETCC(>=, Short);
426 IMPLEMENT_SETCC(>=, UInt);
427 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000428 IMPLEMENT_SETCC(>=, ULong);
429 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000430 IMPLEMENT_SETCC(>=, Float);
431 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000432 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000433 default:
Chris Lattner02868352003-04-22 21:22:33 +0000434 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
435 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000436 }
437 return Dest;
438}
439
440static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000441 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000442 GenericValue Dest;
443 switch (Ty->getPrimitiveID()) {
444 IMPLEMENT_SETCC(<, UByte);
445 IMPLEMENT_SETCC(<, SByte);
446 IMPLEMENT_SETCC(<, UShort);
447 IMPLEMENT_SETCC(<, Short);
448 IMPLEMENT_SETCC(<, UInt);
449 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000450 IMPLEMENT_SETCC(<, ULong);
451 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000452 IMPLEMENT_SETCC(<, Float);
453 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000454 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000455 default:
Chris Lattner02868352003-04-22 21:22:33 +0000456 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
457 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000458 }
459 return Dest;
460}
461
462static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000463 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000464 GenericValue Dest;
465 switch (Ty->getPrimitiveID()) {
466 IMPLEMENT_SETCC(>, UByte);
467 IMPLEMENT_SETCC(>, SByte);
468 IMPLEMENT_SETCC(>, UShort);
469 IMPLEMENT_SETCC(>, Short);
470 IMPLEMENT_SETCC(>, UInt);
471 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000472 IMPLEMENT_SETCC(>, ULong);
473 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000474 IMPLEMENT_SETCC(>, Float);
475 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000476 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000477 default:
Chris Lattner02868352003-04-22 21:22:33 +0000478 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
479 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000480 }
481 return Dest;
482}
483
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000484static void executeBinaryInst(BinaryOperator &I, ExecutionContext &SF) {
485 const Type *Ty = I.getOperand(0)->getType();
486 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
487 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000488 GenericValue R; // Result
489
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000490 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000491 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
492 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
493 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
494 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
495 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
496 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
497 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
498 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
499 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
500 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
501 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
502 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
503 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
504 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000505 default:
Chris Lattner02868352003-04-22 21:22:33 +0000506 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
507 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000508 }
509
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000510 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000511}
512
Chris Lattner92101ac2001-08-23 17:05:04 +0000513//===----------------------------------------------------------------------===//
514// Terminator Instruction Implementations
515//===----------------------------------------------------------------------===//
516
Chris Lattnera95c6992001-11-12 16:28:48 +0000517static void PerformExitStuff() {
518#ifdef PROFILE_STRUCTURE_FIELDS
519 // Print out structure field accounting information...
520 if (!FieldAccessCounts.empty()) {
Chris Lattner84efe092001-11-12 20:13:14 +0000521 CW << "Profile Field Access Counts:\n";
Chris Lattner02868352003-04-22 21:22:33 +0000522 std::map<const StructType *, std::vector<unsigned> >::iterator
Chris Lattnera95c6992001-11-12 16:28:48 +0000523 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
524 for (; I != E; ++I) {
Chris Lattner02868352003-04-22 21:22:33 +0000525 std::vector<unsigned> &OfC = I->second;
Chris Lattnera95c6992001-11-12 16:28:48 +0000526 CW << " '" << (Value*)I->first << "'\t- Sum=";
527
528 unsigned Sum = 0;
529 for (unsigned i = 0; i < OfC.size(); ++i)
530 Sum += OfC[i];
531 CW << Sum << " - ";
532
533 for (unsigned i = 0; i < OfC.size(); ++i) {
534 if (i) CW << ", ";
535 CW << OfC[i];
536 }
Chris Lattner697954c2002-01-20 22:54:45 +0000537 CW << "\n";
Chris Lattnera95c6992001-11-12 16:28:48 +0000538 }
Chris Lattner697954c2002-01-20 22:54:45 +0000539 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000540
541 CW << "Profile Field Access Percentages:\n";
Chris Lattner02868352003-04-22 21:22:33 +0000542 std::cout.precision(3);
Chris Lattner84efe092001-11-12 20:13:14 +0000543 for (I = FieldAccessCounts.begin(); I != E; ++I) {
Chris Lattner02868352003-04-22 21:22:33 +0000544 std::vector<unsigned> &OfC = I->second;
Chris Lattner84efe092001-11-12 20:13:14 +0000545 unsigned Sum = 0;
546 for (unsigned i = 0; i < OfC.size(); ++i)
547 Sum += OfC[i];
548
549 CW << " '" << (Value*)I->first << "'\t- ";
550 for (unsigned i = 0; i < OfC.size(); ++i) {
551 if (i) CW << ", ";
552 CW << double(OfC[i])/Sum;
553 }
Chris Lattner697954c2002-01-20 22:54:45 +0000554 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000555 }
Chris Lattner697954c2002-01-20 22:54:45 +0000556 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000557
Chris Lattnera95c6992001-11-12 16:28:48 +0000558 FieldAccessCounts.clear();
559 }
560#endif
561}
562
Chris Lattnere43db882001-10-27 04:15:57 +0000563void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000564 if (!QuietMode) {
Chris Lattner02868352003-04-22 21:22:33 +0000565 std::cout << "Program returned ";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000566 print(Type::IntTy, GV);
Chris Lattner02868352003-04-22 21:22:33 +0000567 std::cout << " via 'void exit(int)'\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000568 }
Chris Lattnere43db882001-10-27 04:15:57 +0000569
570 ExitCode = GV.SByteVal;
571 ECStack.clear();
Chris Lattnera95c6992001-11-12 16:28:48 +0000572 PerformExitStuff();
Chris Lattnere43db882001-10-27 04:15:57 +0000573}
574
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000575void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000576 const Type *RetTy = 0;
577 GenericValue Result;
578
579 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000580 if (I.getNumOperands()) {
581 RetTy = I.getReturnValue()->getType();
582 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000583 }
584
585 // Save previously executing meth
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000586 const Function *M = ECStack.back().CurMethod;
Chris Lattner92101ac2001-08-23 17:05:04 +0000587
588 // Pop the current stack frame... this invalidates SF
589 ECStack.pop_back();
590
591 if (ECStack.empty()) { // Finished main. Put result into exit code...
592 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000593 if (!QuietMode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000594 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattnerf23eb852001-12-14 16:49:29 +0000595 << "\" returned ";
596 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000597 std::cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000598 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000599
600 if (RetTy->isIntegral())
Chris Lattnerf4dca802002-05-02 19:28:45 +0000601 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000602 } else {
603 ExitCode = 0;
604 }
Chris Lattnere2409062001-11-12 16:19:45 +0000605
Chris Lattnera95c6992001-11-12 16:28:48 +0000606 PerformExitStuff();
Chris Lattner92101ac2001-08-23 17:05:04 +0000607 return;
608 }
609
610 // If we have a previous stack frame, and we have a previous call, fill in
611 // the return value...
612 //
613 ExecutionContext &NewSF = ECStack.back();
614 if (NewSF.Caller) {
615 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
616 SetValue(NewSF.Caller, Result, NewSF);
617
618 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000619 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000620 // This must be a function that is executing because of a user 'call'
621 // instruction.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000622 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000623 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000624 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000625 std::cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000626 }
627}
628
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000629void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000630 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
631 BasicBlock *Dest;
632
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000633 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
634 if (!I.isUnconditional()) {
635 Value *Cond = I.getCondition();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000636 GenericValue CondVal = getOperandValue(Cond, SF);
637 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000638 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000639 }
640 SF.CurBB = Dest; // Update CurBB to branch destination
641 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
642}
643
Chris Lattner09e93922003-04-22 20:34:47 +0000644static void executeSwitch(SwitchInst &I, ExecutionContext &SF) {
645 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
646 const Type *ElTy = I.getOperand(0)->getType();
647 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
648 BasicBlock *Dest = 0;
649
650 // Check to see if any of the cases match...
651 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2) {
652 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000653 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000654 Dest = cast<BasicBlock>(I.getOperand(i+1));
655 break;
656 }
657 }
658
659 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
660 SF.CurBB = Dest; // Update CurBB to branch destination
661 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
662}
663
664
Chris Lattner92101ac2001-08-23 17:05:04 +0000665//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000666// Memory Instruction Implementations
667//===----------------------------------------------------------------------===//
668
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000669void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) {
670 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000671
Chris Lattnercc82cc12002-04-28 21:57:33 +0000672 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000673 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000674
675 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000676 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000677 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
678
Chris Lattnerfe11a972002-12-23 23:59:41 +0000679 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000680 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000681 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000682
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000683 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000684 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000685}
686
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000687static void executeFreeInst(FreeInst &I, ExecutionContext &SF) {
688 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
689 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000690 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000691 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000692}
693
Chris Lattner95c3af52001-10-29 19:32:19 +0000694
Chris Lattnera34c5682002-08-27 22:33:45 +0000695// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000696//
Chris Lattnerfe11a972002-12-23 23:59:41 +0000697GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
698 User::op_iterator E,
699 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000700 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000701 "Cannot getElementOffset of a nonpointer type!");
702
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000703 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000704 const Type *Ty = Ptr->getType();
705
706 for (; I != E; ++I) {
Chris Lattner782b9392001-11-26 18:18:18 +0000707 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
708 const StructLayout *SLO = TD.getStructLayout(STy);
709
710 // Indicies must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000711 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000712 assert(CPU->getType() == Type::UByteTy);
713 unsigned Index = CPU->getValue();
714
Chris Lattnere2409062001-11-12 16:19:45 +0000715#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner782b9392001-11-26 18:18:18 +0000716 if (ProfileStructureFields) {
717 // Do accounting for this field...
Chris Lattner02868352003-04-22 21:22:33 +0000718 std::vector<unsigned> &OfC = FieldAccessCounts[STy];
Chris Lattner782b9392001-11-26 18:18:18 +0000719 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
720 OfC[Index]++;
721 }
Chris Lattnere2409062001-11-12 16:19:45 +0000722#endif
Chris Lattner782b9392001-11-26 18:18:18 +0000723
724 Total += SLO->MemberOffsets[Index];
725 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000726 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000727
Chris Lattner006a4a52003-02-25 21:14:59 +0000728 // Get the index number for the array... which must be long type...
Chris Lattner0374b8d2002-09-11 01:21:35 +0000729 assert((*I)->getType() == Type::LongTy);
Chris Lattnere8b3e9b2002-09-13 23:30:42 +0000730 unsigned Idx = getOperandValue(*I, SF).LongVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000731 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000732 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattner02868352003-04-22 21:22:33 +0000733 std::cerr << "Out of range memory access to element #" << Idx
734 << " of a " << AT->getNumElements() << " element array."
735 << " Subscript #" << *I << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000736 // Get outta here!!!
Chris Lattner74030252002-02-12 15:47:23 +0000737 siglongjmp(SignalRecoverBuffer, SIGTRAP);
Chris Lattnerf23eb852001-12-14 16:49:29 +0000738 }
Chris Lattner782b9392001-11-26 18:18:18 +0000739
Chris Lattnerf23eb852001-12-14 16:49:29 +0000740 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000741 unsigned Size = TD.getTypeSize(Ty);
742 Total += Size*Idx;
743 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000744 }
745
Chris Lattnera34c5682002-08-27 22:33:45 +0000746 GenericValue Result;
747 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
748 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000749}
750
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000751static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000752 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattnera34c5682002-08-27 22:33:45 +0000753 I.idx_begin(), I.idx_end(), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000754}
755
Chris Lattnerfe11a972002-12-23 23:59:41 +0000756void Interpreter::executeLoadInst(LoadInst &I, ExecutionContext &SF) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000757 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000758 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner86660982001-08-27 05:16:50 +0000759 GenericValue Result;
760
Chris Lattnerfddc7552002-10-15 20:34:05 +0000761 if (TD.isLittleEndian()) {
762 switch (I.getType()->getPrimitiveID()) {
763 case Type::BoolTyID:
764 case Type::UByteTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000765 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
Chris Lattnerfddc7552002-10-15 20:34:05 +0000766 case Type::UShortTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000767 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
768 ((unsigned)Ptr->Untyped[1] << 8);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000769 break;
770 case Type::FloatTyID:
771 case Type::UIntTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000772 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
773 ((unsigned)Ptr->Untyped[1] << 8) |
774 ((unsigned)Ptr->Untyped[2] << 16) |
775 ((unsigned)Ptr->Untyped[3] << 24);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000776 break;
777 case Type::DoubleTyID:
778 case Type::ULongTyID:
779 case Type::LongTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000780 case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
781 ((uint64_t)Ptr->Untyped[1] << 8) |
782 ((uint64_t)Ptr->Untyped[2] << 16) |
783 ((uint64_t)Ptr->Untyped[3] << 24) |
784 ((uint64_t)Ptr->Untyped[4] << 32) |
785 ((uint64_t)Ptr->Untyped[5] << 40) |
786 ((uint64_t)Ptr->Untyped[6] << 48) |
787 ((uint64_t)Ptr->Untyped[7] << 56);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000788 break;
789 default:
Chris Lattner02868352003-04-22 21:22:33 +0000790 std::cout << "Cannot load value of type " << *I.getType() << "!\n";
791 abort();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000792 }
793 } else {
794 switch (I.getType()->getPrimitiveID()) {
795 case Type::BoolTyID:
796 case Type::UByteTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000797 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
Chris Lattnerfddc7552002-10-15 20:34:05 +0000798 case Type::UShortTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000799 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
800 ((unsigned)Ptr->Untyped[0] << 8);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000801 break;
802 case Type::FloatTyID:
803 case Type::UIntTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000804 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
805 ((unsigned)Ptr->Untyped[2] << 8) |
806 ((unsigned)Ptr->Untyped[1] << 16) |
807 ((unsigned)Ptr->Untyped[0] << 24);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000808 break;
809 case Type::DoubleTyID:
810 case Type::ULongTyID:
811 case Type::LongTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000812 case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
813 ((uint64_t)Ptr->Untyped[6] << 8) |
814 ((uint64_t)Ptr->Untyped[5] << 16) |
815 ((uint64_t)Ptr->Untyped[4] << 24) |
816 ((uint64_t)Ptr->Untyped[3] << 32) |
817 ((uint64_t)Ptr->Untyped[2] << 40) |
818 ((uint64_t)Ptr->Untyped[1] << 48) |
819 ((uint64_t)Ptr->Untyped[0] << 56);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000820 break;
821 default:
Chris Lattner02868352003-04-22 21:22:33 +0000822 std::cout << "Cannot load value of type " << *I.getType() << "!\n";
823 abort();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000824 }
Chris Lattner86660982001-08-27 05:16:50 +0000825 }
826
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000827 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000828}
829
Chris Lattnerfe11a972002-12-23 23:59:41 +0000830void Interpreter::executeStoreInst(StoreInst &I, ExecutionContext &SF) {
Chris Lattnerfddc7552002-10-15 20:34:05 +0000831 GenericValue Val = getOperandValue(I.getOperand(0), SF);
832 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000833 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000834 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000835}
836
Chris Lattner86660982001-08-27 05:16:50 +0000837
Chris Lattnerab2dea52002-11-07 19:29:31 +0000838
Chris Lattner86660982001-08-27 05:16:50 +0000839//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000840// Miscellaneous Instruction Implementations
841//===----------------------------------------------------------------------===//
842
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000843void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) {
844 ECStack.back().Caller = &I;
Chris Lattner02868352003-04-22 21:22:33 +0000845 std::vector<GenericValue> ArgVals;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000846 ArgVals.reserve(I.getNumOperands()-1);
Chris Lattner93780132003-01-13 00:58:52 +0000847 for (unsigned i = 1; i < I.getNumOperands(); ++i) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000848 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
Chris Lattner93780132003-01-13 00:58:52 +0000849 // Promote all integral types whose size is < sizeof(int) into ints. We do
850 // this by zero or sign extending the value as appropriate according to the
851 // source type.
852 if (I.getOperand(i)->getType()->isIntegral() &&
853 I.getOperand(i)->getType()->getPrimitiveSize() < 4) {
854 const Type *Ty = I.getOperand(i)->getType();
855 if (Ty == Type::ShortTy)
856 ArgVals.back().IntVal = ArgVals.back().ShortVal;
857 else if (Ty == Type::UShortTy)
858 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
859 else if (Ty == Type::SByteTy)
860 ArgVals.back().IntVal = ArgVals.back().SByteVal;
861 else if (Ty == Type::UByteTy)
862 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
863 else if (Ty == Type::BoolTy)
864 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
865 else
866 assert(0 && "Unknown type!");
867 }
868 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000869
Chris Lattner070cf5e2001-11-07 20:12:30 +0000870 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000871 // and treat it as a function pointer.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000872 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
Chris Lattner070cf5e2001-11-07 20:12:30 +0000873
Chris Lattnerfe11a972002-12-23 23:59:41 +0000874 callMethod((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000875}
876
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000877static void executePHINode(PHINode &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000878 BasicBlock *PrevBB = SF.PrevBB;
879 Value *IncomingValue = 0;
880
881 // Search for the value corresponding to this previous bb...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000882 for (unsigned i = I.getNumIncomingValues(); i > 0;) {
883 if (I.getIncomingBlock(--i) == PrevBB) {
884 IncomingValue = I.getIncomingValue(i);
Chris Lattner92101ac2001-08-23 17:05:04 +0000885 break;
886 }
887 }
888 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
889
890 // Found the value, set as the result...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000891 SetValue(&I, getOperandValue(IncomingValue, SF), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000892}
893
Chris Lattner86660982001-08-27 05:16:50 +0000894#define IMPLEMENT_SHIFT(OP, TY) \
895 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
896
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000897static void executeShlInst(ShiftInst &I, ExecutionContext &SF) {
898 const Type *Ty = I.getOperand(0)->getType();
899 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
900 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000901 GenericValue Dest;
902
903 switch (Ty->getPrimitiveID()) {
904 IMPLEMENT_SHIFT(<<, UByte);
905 IMPLEMENT_SHIFT(<<, SByte);
906 IMPLEMENT_SHIFT(<<, UShort);
907 IMPLEMENT_SHIFT(<<, Short);
908 IMPLEMENT_SHIFT(<<, UInt);
909 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000910 IMPLEMENT_SHIFT(<<, ULong);
911 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000912 default:
Chris Lattner02868352003-04-22 21:22:33 +0000913 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000914 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000915 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000916}
917
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000918static void executeShrInst(ShiftInst &I, ExecutionContext &SF) {
919 const Type *Ty = I.getOperand(0)->getType();
920 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
921 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000922 GenericValue Dest;
923
924 switch (Ty->getPrimitiveID()) {
925 IMPLEMENT_SHIFT(>>, UByte);
926 IMPLEMENT_SHIFT(>>, SByte);
927 IMPLEMENT_SHIFT(>>, UShort);
928 IMPLEMENT_SHIFT(>>, Short);
929 IMPLEMENT_SHIFT(>>, UInt);
930 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000931 IMPLEMENT_SHIFT(>>, ULong);
932 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000933 default:
Chris Lattner02868352003-04-22 21:22:33 +0000934 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
935 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000936 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000937 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000938}
939
940#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000941 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000942
943#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
944 case Type::DESTTY##TyID: \
945 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000946 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000947 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
948 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
949 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
950 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
951 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000952 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
953 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000954 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
955 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000956
957#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
958 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
959 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
960
961#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner02868352003-04-22 21:22:33 +0000962 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
963 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000964 } \
965 break
966
967#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
968 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
969 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000970 IMPLEMENT_CAST_CASE_END()
971
Chris Lattnera34c5682002-08-27 22:33:45 +0000972static GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
973 ExecutionContext &SF) {
974 const Type *SrcTy = SrcVal->getType();
975 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000976
977 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000978 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
979 IMPLEMENT_CAST_CASE(SByte , ( signed char));
980 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000981 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000982 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
983 IMPLEMENT_CAST_CASE(Int , ( signed int ));
984 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
985 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000986 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000987 IMPLEMENT_CAST_CASE(Float , (float));
988 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +0000989 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +0000990 default:
Chris Lattner02868352003-04-22 21:22:33 +0000991 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +0000992 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000993 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000994
995 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000996}
Chris Lattner92101ac2001-08-23 17:05:04 +0000997
998
Chris Lattnera34c5682002-08-27 22:33:45 +0000999static void executeCastInst(CastInst &I, ExecutionContext &SF) {
1000 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1001}
Chris Lattner92101ac2001-08-23 17:05:04 +00001002
1003
1004//===----------------------------------------------------------------------===//
1005// Dispatch and Execution Code
1006//===----------------------------------------------------------------------===//
1007
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001008MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001009 // Assign slot numbers to the function arguments...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001010 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
1011 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001012
1013 // Iterate over all of the instructions...
1014 unsigned InstNum = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001015 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
1016 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
1017 // For each instruction... Add Annote
1018 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001019}
1020
1021unsigned MethodInfo::getValueSlot(const Value *V) {
1022 unsigned Plane = V->getType()->getUniqueID();
1023 if (Plane >= NumPlaneElements.size())
1024 NumPlaneElements.resize(Plane+1, 0);
1025 return NumPlaneElements[Plane]++;
1026}
1027
1028
Chris Lattner92101ac2001-08-23 17:05:04 +00001029//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001030// callMethod - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001031//
Chris Lattner02868352003-04-22 21:22:33 +00001032void Interpreter::callMethod(Function *M,
1033 const std::vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +00001034 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1035 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1036 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +00001037 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001038 GenericValue Result = callExternalMethod(M, ArgVals);
1039 const Type *RetTy = M->getReturnType();
1040
1041 // Copy the result back into the result variable if we are not returning
1042 // void.
1043 if (RetTy != Type::VoidTy) {
1044 if (!ECStack.empty() && ECStack.back().Caller) {
1045 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +00001046 SetValue(SF.Caller, Result, SF);
1047
1048 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +00001049 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001050 // print it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001051 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +00001052 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001053 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +00001054 std::cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001055
1056 if (RetTy->isIntegral())
Chris Lattner0c4e8862002-09-03 01:08:28 +00001057 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattnerbb76f022001-10-30 20:27:31 +00001058 }
1059 }
1060
Chris Lattner92101ac2001-08-23 17:05:04 +00001061 return;
1062 }
1063
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001064 // Process the function, assigning instruction numbers to the instructions in
1065 // the function. Also calculate the number of values for each type slot
1066 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +00001067 //
1068 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001069 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001070
Chris Lattner92101ac2001-08-23 17:05:04 +00001071 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1072 StackFrame.CurMethod = M;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001073 StackFrame.CurBB = M->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001074 StackFrame.CurInst = StackFrame.CurBB->begin();
1075 StackFrame.MethInfo = MethInfo;
1076
1077 // Initialize the values to nothing...
1078 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001079 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001080 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1081
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001082 // Taint the initial values of stuff
1083 memset(&StackFrame.Values[i][0], 42,
1084 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1085 }
1086
Chris Lattner92101ac2001-08-23 17:05:04 +00001087 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1088
Chris Lattner92101ac2001-08-23 17:05:04 +00001089
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001090 // Run through the function arguments and initialize their values...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001091 assert(ArgVals.size() == M->asize() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001092 "Invalid number of values passed to function invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001093 unsigned i = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001094 for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i)
1095 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001096}
1097
1098// executeInstruction - Interpret a single instruction, increment the "PC", and
1099// return true if the next instruction is a breakpoint...
1100//
1101bool Interpreter::executeInstruction() {
1102 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1103
1104 ExecutionContext &SF = ECStack.back(); // Current stack frame
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001105 Instruction &I = *SF.CurInst++; // Increment before execute
Chris Lattner92101ac2001-08-23 17:05:04 +00001106
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001107 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001108 CW << "Run:" << I;
1109
Chris Lattnerbbdabce2002-12-08 05:51:08 +00001110 // Track the number of dynamic instructions executed.
1111 ++NumDynamicInsts;
1112
Chris Lattner5af0c482001-11-07 04:23:00 +00001113 // Set a sigsetjmp buffer so that we can recover if an error happens during
1114 // instruction execution...
1115 //
1116 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1117 --SF.CurInst; // Back up to erroring instruction
Chris Lattner74030252002-02-12 15:47:23 +00001118 if (SigNo != SIGINT) {
Chris Lattner02868352003-04-22 21:22:33 +00001119 std::cout << "EXCEPTION OCCURRED [" << strsignal(SigNo) << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001120 printStackTrace();
Chris Lattner74030252002-02-12 15:47:23 +00001121 // If -abort-on-exception was specified, terminate LLI instead of trying
1122 // to debug it.
1123 //
1124 if (AbortOnExceptions) exit(1);
Chris Lattner782b9392001-11-26 18:18:18 +00001125 } else if (SigNo == SIGINT) {
Chris Lattner02868352003-04-22 21:22:33 +00001126 std::cout << "CTRL-C Detected, execution halted.\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001127 }
1128 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001129 return true;
1130 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001131
Chris Lattner461f02f2001-11-07 05:31:27 +00001132 InInstruction = true;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001133 if (I.isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001134 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001135 } else {
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001136 switch (I.getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +00001137 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001138 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1139 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner09e93922003-04-22 20:34:47 +00001140 case Instruction::Switch: executeSwitch (cast<SwitchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001141 // Memory Instructions
1142 case Instruction::Alloca:
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001143 case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break;
Chris Lattnerbb76f022001-10-30 20:27:31 +00001144 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1145 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1146 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001147 case Instruction::GetElementPtr:
1148 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001149
1150 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001151 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1152 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1153 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1154 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1155 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001156 default:
Chris Lattner02868352003-04-22 21:22:33 +00001157 std::cout << "Don't know how to execute this instruction!\n-->" << I;
1158 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +00001159 }
1160 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001161 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001162
1163 // Reset the current frame location to the top of stack
1164 CurFrame = ECStack.size()-1;
1165
1166 if (CurFrame == -1) return false; // No breakpoint if no code
1167
1168 // Return true if there is a breakpoint annotation on the instruction...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001169 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
Chris Lattner92101ac2001-08-23 17:05:04 +00001170}
1171
1172void Interpreter::stepInstruction() { // Do the 'step' command
1173 if (ECStack.empty()) {
Chris Lattner02868352003-04-22 21:22:33 +00001174 std::cout << "Error: no program running, cannot step!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001175 return;
1176 }
1177
1178 // Run an instruction...
1179 executeInstruction();
1180
1181 // Print the next instruction to execute...
1182 printCurrentInstruction();
1183}
1184
1185// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001186void Interpreter::nextInstruction() { // Do the 'next' command
1187 if (ECStack.empty()) {
Chris Lattner02868352003-04-22 21:22:33 +00001188 std::cout << "Error: no program running, cannot 'next'!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001189 return;
1190 }
1191
1192 // If this is a call instruction, step over the call instruction...
1193 // TODO: ICALL, CALL WITH, ...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001194 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001195 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001196 // Step into the function...
1197 if (executeInstruction()) {
1198 // Hit a breakpoint, print current instruction, then return to user...
Chris Lattner02868352003-04-22 21:22:33 +00001199 std::cout << "Breakpoint hit!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001200 printCurrentInstruction();
1201 return;
1202 }
1203
Chris Lattnera74a6b52001-10-29 14:08:33 +00001204 // If we we able to step into the function, finish it now. We might not be
1205 // able the step into a function, if it's external for example.
1206 if (ECStack.size() != StackSize)
1207 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001208 else
1209 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001210
Chris Lattner92101ac2001-08-23 17:05:04 +00001211 } else {
1212 // Normal instruction, just step...
1213 stepInstruction();
1214 }
1215}
1216
1217void Interpreter::run() {
1218 if (ECStack.empty()) {
Chris Lattner02868352003-04-22 21:22:33 +00001219 std::cout << "Error: no program running, cannot run!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001220 return;
1221 }
1222
1223 bool HitBreakpoint = false;
1224 while (!ECStack.empty() && !HitBreakpoint) {
1225 // Run an instruction...
1226 HitBreakpoint = executeInstruction();
1227 }
1228
Chris Lattner02868352003-04-22 21:22:33 +00001229 if (HitBreakpoint)
1230 std::cout << "Breakpoint hit!\n";
1231
Chris Lattner92101ac2001-08-23 17:05:04 +00001232 // Print the next instruction to execute...
1233 printCurrentInstruction();
1234}
1235
1236void Interpreter::finish() {
1237 if (ECStack.empty()) {
Chris Lattner02868352003-04-22 21:22:33 +00001238 std::cout << "Error: no program running, cannot run!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001239 return;
1240 }
1241
1242 unsigned StackSize = ECStack.size();
1243 bool HitBreakpoint = false;
1244 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1245 // Run an instruction...
1246 HitBreakpoint = executeInstruction();
1247 }
1248
Chris Lattner02868352003-04-22 21:22:33 +00001249 if (HitBreakpoint)
1250 std::cout << "Breakpoint hit!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001251
1252 // Print the next instruction to execute...
1253 printCurrentInstruction();
1254}
1255
1256
1257
1258// printCurrentInstruction - Print out the instruction that the virtual PC is
1259// at, or fail silently if no program is running.
1260//
1261void Interpreter::printCurrentInstruction() {
1262 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001263 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
Chris Lattner02868352003-04-22 21:22:33 +00001264 WriteAsOperand(std::cout, ECStack.back().CurBB) << ":\n";
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001265
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001266 Instruction &I = *ECStack.back().CurInst;
1267 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001268 assert(IN && "Instruction has no numbering annotation!");
Chris Lattner02868352003-04-22 21:22:33 +00001269 std::cout << "#" << IN->InstNum << I;
Chris Lattner92101ac2001-08-23 17:05:04 +00001270 }
1271}
1272
1273void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001274 switch (Ty->getPrimitiveID()) {
Chris Lattner02868352003-04-22 21:22:33 +00001275 case Type::BoolTyID: std::cout << (V.BoolVal?"true":"false"); break;
Chris Lattner65629d52002-08-13 20:45:11 +00001276 case Type::SByteTyID:
Chris Lattner02868352003-04-22 21:22:33 +00001277 std::cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
Chris Lattner65629d52002-08-13 20:45:11 +00001278 case Type::UByteTyID:
Chris Lattner02868352003-04-22 21:22:33 +00001279 std::cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
1280 case Type::ShortTyID: std::cout << V.ShortVal; break;
1281 case Type::UShortTyID: std::cout << V.UShortVal; break;
1282 case Type::IntTyID: std::cout << V.IntVal; break;
1283 case Type::UIntTyID: std::cout << V.UIntVal; break;
1284 case Type::LongTyID: std::cout << (long)V.LongVal; break;
1285 case Type::ULongTyID: std::cout << (unsigned long)V.ULongVal; break;
1286 case Type::FloatTyID: std::cout << V.FloatVal; break;
1287 case Type::DoubleTyID: std::cout << V.DoubleVal; break;
1288 case Type::PointerTyID:std::cout << (void*)GVTOP(V); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001289 default:
Chris Lattner02868352003-04-22 21:22:33 +00001290 std::cout << "- Don't know how to print value of this type!";
Chris Lattner92101ac2001-08-23 17:05:04 +00001291 break;
1292 }
1293}
1294
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001295void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001296 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001297 printValue(Ty, V);
1298}
1299
Chris Lattner697954c2002-01-20 22:54:45 +00001300void Interpreter::print(const std::string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001301 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1302 if (!PickedVal) return;
1303
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001304 if (const Function *F = dyn_cast<const Function>(PickedVal)) {
1305 CW << F; // Print the function
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001306 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001307 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001308 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1309 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001310 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001311 print(PickedVal->getType(),
1312 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner02868352003-04-22 21:22:33 +00001313 std::cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001314 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001315}
1316
Chris Lattner697954c2002-01-20 22:54:45 +00001317void Interpreter::infoValue(const std::string &Name) {
Chris Lattner86660982001-08-27 05:16:50 +00001318 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1319 if (!PickedVal) return;
1320
Chris Lattner02868352003-04-22 21:22:33 +00001321 std::cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001322 print(PickedVal->getType(),
1323 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner02868352003-04-22 21:22:33 +00001324 std::cout << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001325 printOperandInfo(PickedVal, ECStack[CurFrame]);
1326}
1327
Chris Lattner461f02f2001-11-07 05:31:27 +00001328// printStackFrame - Print information about the specified stack frame, or -1
1329// for the default one.
1330//
Chris Lattner601d7152002-07-25 17:37:05 +00001331void Interpreter::printStackFrame(int FrameNo) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001332 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001333 Function *F = ECStack[FrameNo].CurMethod;
1334 const Type *RetTy = F->getReturnType();
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001335
1336 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001337 << (Value*)RetTy << " \"" << F->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001338
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001339 unsigned i = 0;
1340 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
Chris Lattner02868352003-04-22 21:22:33 +00001341 if (i != 0) std::cout << ", ";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001342 CW << *I << "=";
Chris Lattner461f02f2001-11-07 05:31:27 +00001343
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001344 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001345 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001346
Chris Lattner02868352003-04-22 21:22:33 +00001347 std::cout << ")\n";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001348
1349 if (FrameNo != int(ECStack.size()-1)) {
1350 BasicBlock::iterator I = ECStack[FrameNo].CurInst;
1351 CW << --I;
1352 } else {
1353 CW << *ECStack[FrameNo].CurInst;
1354 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001355}
Chris Lattner461f02f2001-11-07 05:31:27 +00001356