blob: f03514dba2bb129c0d245a31cde12fd33e93db0f [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"));
34
35 cl::opt<bool>
36 AbortOnExceptions("abort-on-exception",
37 cl::desc("Halt execution on a machine exception"));
Chris Lattnerbbdabce2002-12-08 05:51:08 +000038}
39
Chris Lattner2e42d3a2001-10-15 05:51:48 +000040// Create a TargetData structure to handle memory addressing and size/alignment
41// computations
42//
Chris Lattnerea38c0e2001-11-07 19:46:27 +000043CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000044
Chris Lattnere2409062001-11-12 16:19:45 +000045#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner5ff62e92002-07-22 02:10:13 +000046static cl::opt<bool>
47ProfileStructureFields("profilestructfields",
48 cl::desc("Profile Structure Field Accesses"));
Chris Lattnere2409062001-11-12 16:19:45 +000049#include <map>
Chris Lattner02868352003-04-22 21:22:33 +000050static std::map<const StructType *, std::vector<unsigned> > FieldAccessCounts;
Chris Lattnere2409062001-11-12 16:19:45 +000051#endif
52
Chris Lattner5af0c482001-11-07 04:23:00 +000053sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000054static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000055
56extern "C" {
57static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000058 if (InInstruction)
59 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000060}
61}
62
63static void initializeSignalHandlers() {
64 struct sigaction Action;
65 Action.sa_handler = SigHandler;
66 Action.sa_flags = SA_SIGINFO;
67 sigemptyset(&Action.sa_mask);
68 sigaction(SIGSEGV, &Action, 0);
69 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000070 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000071 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000072}
73
Chris Lattner2e42d3a2001-10-15 05:51:48 +000074
75//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000076// Value Manipulation code
77//===----------------------------------------------------------------------===//
78
79static unsigned getOperandSlot(Value *V) {
80 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
81 assert(SN && "Operand does not have a slot number annotation!");
82 return SN->SlotNum;
83}
84
Chris Lattnera34c5682002-08-27 22:33:45 +000085// Operations used by constant expr implementations...
86static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
87 ExecutionContext &SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000088static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000089 const Type *Ty);
Chris Lattnera34c5682002-08-27 22:33:45 +000090
Chris Lattnerfddc7552002-10-15 20:34:05 +000091
Chris Lattner39bb5b42001-10-15 13:25:40 +000092static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +000093 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
94 switch (CE->getOpcode()) {
95 case Instruction::Cast:
96 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
97 case Instruction::GetElementPtr:
Chris Lattnerfe11a972002-12-23 23:59:41 +000098 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
99 CE->op_end(), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000100 case Instruction::Add:
101 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
102 getOperandValue(CE->getOperand(1), SF),
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000103 CE->getType());
Chris Lattnera34c5682002-08-27 22:33:45 +0000104 default:
Chris Lattner02868352003-04-22 21:22:33 +0000105 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
Chris Lattnera34c5682002-08-27 22:33:45 +0000106 abort();
Chris Lattner04e2ad72003-04-21 22:43:32 +0000107 return GenericValue();
Chris Lattnera34c5682002-08-27 22:33:45 +0000108 }
109 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000110 return TheEE->getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000111 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000112 return PTOGV(TheEE->getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000113 } else {
114 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000115 unsigned OpSlot = getOperandSlot(V);
116 assert(TyP < SF.Values.size() &&
117 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000118 return SF.Values[TyP][getOperandSlot(V)];
119 }
120}
121
122static void printOperandInfo(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000123 if (isa<Constant>(V)) {
Chris Lattner02868352003-04-22 21:22:33 +0000124 std::cout << "Constant Pool Value\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000125 } else if (isa<GlobalValue>(V)) {
Chris Lattner02868352003-04-22 21:22:33 +0000126 std::cout << "Global Value\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000127 } else {
128 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
129 unsigned Slot = getOperandSlot(V);
Chris Lattner02868352003-04-22 21:22:33 +0000130 std::cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
131 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
132 << " Contents=0x";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000133
134 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
135 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
136 unsigned char Cur = Buf[i];
Chris Lattner02868352003-04-22 21:22:33 +0000137 std::cout << ( Cur >= 160?char((Cur>>4)+'A'-10):char((Cur>>4) + '0'))
138 << ((Cur&15) >= 10?char((Cur&15)+'A'-10):char((Cur&15) + '0'));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000139 }
Chris Lattner02868352003-04-22 21:22:33 +0000140 std::cout << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000141 }
142}
143
144
145
146static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
147 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
148
Chris Lattner02868352003-04-22 21:22:33 +0000149 //std::cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)]<< "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000150 SF.Values[TyP][getOperandSlot(V)] = Val;
151}
152
153
154//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000155// Annotation Wrangling code
156//===----------------------------------------------------------------------===//
157
158void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000159 TheEE = this;
Chris Lattnerda82ed52003-05-08 16:18:31 +0000160 AnnotationManager::registerAnnotationFactory(FunctionInfoAID,
161 &FunctionInfo::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000162 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000163}
164
Chris Lattner2adcd832002-05-03 19:52:30 +0000165//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000166// Binary Instruction Implementations
167//===----------------------------------------------------------------------===//
168
169#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
170 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
171
172static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000173 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000174 GenericValue Dest;
175 switch (Ty->getPrimitiveID()) {
176 IMPLEMENT_BINARY_OPERATOR(+, UByte);
177 IMPLEMENT_BINARY_OPERATOR(+, SByte);
178 IMPLEMENT_BINARY_OPERATOR(+, UShort);
179 IMPLEMENT_BINARY_OPERATOR(+, Short);
180 IMPLEMENT_BINARY_OPERATOR(+, UInt);
181 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000182 IMPLEMENT_BINARY_OPERATOR(+, ULong);
183 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000184 IMPLEMENT_BINARY_OPERATOR(+, Float);
185 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000186 default:
Chris Lattner02868352003-04-22 21:22:33 +0000187 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
188 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000189 }
190 return Dest;
191}
192
193static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000194 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000195 GenericValue Dest;
196 switch (Ty->getPrimitiveID()) {
197 IMPLEMENT_BINARY_OPERATOR(-, UByte);
198 IMPLEMENT_BINARY_OPERATOR(-, SByte);
199 IMPLEMENT_BINARY_OPERATOR(-, UShort);
200 IMPLEMENT_BINARY_OPERATOR(-, Short);
201 IMPLEMENT_BINARY_OPERATOR(-, UInt);
202 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000203 IMPLEMENT_BINARY_OPERATOR(-, ULong);
204 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000205 IMPLEMENT_BINARY_OPERATOR(-, Float);
206 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000207 default:
Chris Lattner02868352003-04-22 21:22:33 +0000208 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
209 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000210 }
211 return Dest;
212}
213
Chris Lattnerc2593162001-10-27 08:28:11 +0000214static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000215 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000216 GenericValue Dest;
217 switch (Ty->getPrimitiveID()) {
218 IMPLEMENT_BINARY_OPERATOR(*, UByte);
219 IMPLEMENT_BINARY_OPERATOR(*, SByte);
220 IMPLEMENT_BINARY_OPERATOR(*, UShort);
221 IMPLEMENT_BINARY_OPERATOR(*, Short);
222 IMPLEMENT_BINARY_OPERATOR(*, UInt);
223 IMPLEMENT_BINARY_OPERATOR(*, Int);
224 IMPLEMENT_BINARY_OPERATOR(*, ULong);
225 IMPLEMENT_BINARY_OPERATOR(*, Long);
226 IMPLEMENT_BINARY_OPERATOR(*, Float);
227 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000228 default:
Chris Lattner02868352003-04-22 21:22:33 +0000229 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
230 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000231 }
232 return Dest;
233}
234
235static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000236 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000237 GenericValue Dest;
238 switch (Ty->getPrimitiveID()) {
239 IMPLEMENT_BINARY_OPERATOR(/, UByte);
240 IMPLEMENT_BINARY_OPERATOR(/, SByte);
241 IMPLEMENT_BINARY_OPERATOR(/, UShort);
242 IMPLEMENT_BINARY_OPERATOR(/, Short);
243 IMPLEMENT_BINARY_OPERATOR(/, UInt);
244 IMPLEMENT_BINARY_OPERATOR(/, Int);
245 IMPLEMENT_BINARY_OPERATOR(/, ULong);
246 IMPLEMENT_BINARY_OPERATOR(/, Long);
247 IMPLEMENT_BINARY_OPERATOR(/, Float);
248 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000249 default:
Chris Lattner02868352003-04-22 21:22:33 +0000250 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
251 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000252 }
253 return Dest;
254}
255
256static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000257 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000258 GenericValue Dest;
259 switch (Ty->getPrimitiveID()) {
260 IMPLEMENT_BINARY_OPERATOR(%, UByte);
261 IMPLEMENT_BINARY_OPERATOR(%, SByte);
262 IMPLEMENT_BINARY_OPERATOR(%, UShort);
263 IMPLEMENT_BINARY_OPERATOR(%, Short);
264 IMPLEMENT_BINARY_OPERATOR(%, UInt);
265 IMPLEMENT_BINARY_OPERATOR(%, Int);
266 IMPLEMENT_BINARY_OPERATOR(%, ULong);
267 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000268 case Type::FloatTyID:
269 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
270 break;
271 case Type::DoubleTyID:
272 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
273 break;
274 default:
Chris Lattner02868352003-04-22 21:22:33 +0000275 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
276 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000277 }
278 return Dest;
279}
280
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000281static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000282 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000283 GenericValue Dest;
284 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000285 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000286 IMPLEMENT_BINARY_OPERATOR(&, UByte);
287 IMPLEMENT_BINARY_OPERATOR(&, SByte);
288 IMPLEMENT_BINARY_OPERATOR(&, UShort);
289 IMPLEMENT_BINARY_OPERATOR(&, Short);
290 IMPLEMENT_BINARY_OPERATOR(&, UInt);
291 IMPLEMENT_BINARY_OPERATOR(&, Int);
292 IMPLEMENT_BINARY_OPERATOR(&, ULong);
293 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000294 default:
Chris Lattner02868352003-04-22 21:22:33 +0000295 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
296 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000297 }
298 return Dest;
299}
300
301
302static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000303 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000304 GenericValue Dest;
305 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000306 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000307 IMPLEMENT_BINARY_OPERATOR(|, UByte);
308 IMPLEMENT_BINARY_OPERATOR(|, SByte);
309 IMPLEMENT_BINARY_OPERATOR(|, UShort);
310 IMPLEMENT_BINARY_OPERATOR(|, Short);
311 IMPLEMENT_BINARY_OPERATOR(|, UInt);
312 IMPLEMENT_BINARY_OPERATOR(|, Int);
313 IMPLEMENT_BINARY_OPERATOR(|, ULong);
314 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000315 default:
Chris Lattner02868352003-04-22 21:22:33 +0000316 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
317 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000318 }
319 return Dest;
320}
321
322
323static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000324 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000325 GenericValue Dest;
326 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000327 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000328 IMPLEMENT_BINARY_OPERATOR(^, UByte);
329 IMPLEMENT_BINARY_OPERATOR(^, SByte);
330 IMPLEMENT_BINARY_OPERATOR(^, UShort);
331 IMPLEMENT_BINARY_OPERATOR(^, Short);
332 IMPLEMENT_BINARY_OPERATOR(^, UInt);
333 IMPLEMENT_BINARY_OPERATOR(^, Int);
334 IMPLEMENT_BINARY_OPERATOR(^, ULong);
335 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000336 default:
Chris Lattner02868352003-04-22 21:22:33 +0000337 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
338 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000339 }
340 return Dest;
341}
342
343
Chris Lattner92101ac2001-08-23 17:05:04 +0000344#define IMPLEMENT_SETCC(OP, TY) \
345 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
346
Chris Lattnerfd506f52003-04-23 19:55:35 +0000347// Handle pointers specially because they must be compared with only as much
348// width as the host has. We _do not_ want to be comparing 64 bit values when
349// running on a 32-bit target, otherwise the upper 32 bits might mess up
350// comparisons if they contain garbage.
351#define IMPLEMENT_POINTERSETCC(OP) \
352 case Type::PointerTyID: \
353 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
354 (void*)(intptr_t)Src2.PointerVal; break
355
Chris Lattner92101ac2001-08-23 17:05:04 +0000356static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000357 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000358 GenericValue Dest;
359 switch (Ty->getPrimitiveID()) {
360 IMPLEMENT_SETCC(==, UByte);
361 IMPLEMENT_SETCC(==, SByte);
362 IMPLEMENT_SETCC(==, UShort);
363 IMPLEMENT_SETCC(==, Short);
364 IMPLEMENT_SETCC(==, UInt);
365 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000366 IMPLEMENT_SETCC(==, ULong);
367 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000368 IMPLEMENT_SETCC(==, Float);
369 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000370 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000371 default:
Chris Lattner02868352003-04-22 21:22:33 +0000372 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
373 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000374 }
375 return Dest;
376}
377
378static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000379 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000380 GenericValue Dest;
381 switch (Ty->getPrimitiveID()) {
382 IMPLEMENT_SETCC(!=, UByte);
383 IMPLEMENT_SETCC(!=, SByte);
384 IMPLEMENT_SETCC(!=, UShort);
385 IMPLEMENT_SETCC(!=, Short);
386 IMPLEMENT_SETCC(!=, UInt);
387 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000388 IMPLEMENT_SETCC(!=, ULong);
389 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000390 IMPLEMENT_SETCC(!=, Float);
391 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000392 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000393
Chris Lattner92101ac2001-08-23 17:05:04 +0000394 default:
Chris Lattner02868352003-04-22 21:22:33 +0000395 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
396 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000397 }
398 return Dest;
399}
400
401static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000402 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000403 GenericValue Dest;
404 switch (Ty->getPrimitiveID()) {
405 IMPLEMENT_SETCC(<=, UByte);
406 IMPLEMENT_SETCC(<=, SByte);
407 IMPLEMENT_SETCC(<=, UShort);
408 IMPLEMENT_SETCC(<=, Short);
409 IMPLEMENT_SETCC(<=, UInt);
410 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000411 IMPLEMENT_SETCC(<=, ULong);
412 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000413 IMPLEMENT_SETCC(<=, Float);
414 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000415 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000416 default:
Chris Lattner02868352003-04-22 21:22:33 +0000417 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
418 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000419 }
420 return Dest;
421}
422
423static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000424 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000425 GenericValue Dest;
426 switch (Ty->getPrimitiveID()) {
427 IMPLEMENT_SETCC(>=, UByte);
428 IMPLEMENT_SETCC(>=, SByte);
429 IMPLEMENT_SETCC(>=, UShort);
430 IMPLEMENT_SETCC(>=, Short);
431 IMPLEMENT_SETCC(>=, UInt);
432 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000433 IMPLEMENT_SETCC(>=, ULong);
434 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000435 IMPLEMENT_SETCC(>=, Float);
436 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000437 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000438 default:
Chris Lattner02868352003-04-22 21:22:33 +0000439 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
440 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000441 }
442 return Dest;
443}
444
445static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000446 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000447 GenericValue Dest;
448 switch (Ty->getPrimitiveID()) {
449 IMPLEMENT_SETCC(<, UByte);
450 IMPLEMENT_SETCC(<, SByte);
451 IMPLEMENT_SETCC(<, UShort);
452 IMPLEMENT_SETCC(<, Short);
453 IMPLEMENT_SETCC(<, UInt);
454 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000455 IMPLEMENT_SETCC(<, ULong);
456 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000457 IMPLEMENT_SETCC(<, Float);
458 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000459 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000460 default:
Chris Lattner02868352003-04-22 21:22:33 +0000461 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
462 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000463 }
464 return Dest;
465}
466
467static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000468 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000469 GenericValue Dest;
470 switch (Ty->getPrimitiveID()) {
471 IMPLEMENT_SETCC(>, UByte);
472 IMPLEMENT_SETCC(>, SByte);
473 IMPLEMENT_SETCC(>, UShort);
474 IMPLEMENT_SETCC(>, Short);
475 IMPLEMENT_SETCC(>, UInt);
476 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000477 IMPLEMENT_SETCC(>, ULong);
478 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000479 IMPLEMENT_SETCC(>, Float);
480 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000481 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000482 default:
Chris Lattner02868352003-04-22 21:22:33 +0000483 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
484 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000485 }
486 return Dest;
487}
488
Chris Lattnerd7916e92003-05-10 21:22:39 +0000489void Interpreter::visitBinaryOperator(BinaryOperator &I) {
490 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000491 const Type *Ty = I.getOperand(0)->getType();
492 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
493 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000494 GenericValue R; // Result
495
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000496 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000497 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
498 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
499 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
500 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
501 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
502 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
503 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
504 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
505 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
506 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
507 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
508 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
509 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
510 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000511 default:
Chris Lattner02868352003-04-22 21:22:33 +0000512 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
513 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000514 }
515
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000516 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000517}
518
Chris Lattner92101ac2001-08-23 17:05:04 +0000519//===----------------------------------------------------------------------===//
520// Terminator Instruction Implementations
521//===----------------------------------------------------------------------===//
522
Chris Lattnera95c6992001-11-12 16:28:48 +0000523static void PerformExitStuff() {
524#ifdef PROFILE_STRUCTURE_FIELDS
525 // Print out structure field accounting information...
526 if (!FieldAccessCounts.empty()) {
Chris Lattner84efe092001-11-12 20:13:14 +0000527 CW << "Profile Field Access Counts:\n";
Chris Lattner02868352003-04-22 21:22:33 +0000528 std::map<const StructType *, std::vector<unsigned> >::iterator
Chris Lattnera95c6992001-11-12 16:28:48 +0000529 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
530 for (; I != E; ++I) {
Chris Lattner02868352003-04-22 21:22:33 +0000531 std::vector<unsigned> &OfC = I->second;
Chris Lattnera95c6992001-11-12 16:28:48 +0000532 CW << " '" << (Value*)I->first << "'\t- Sum=";
533
534 unsigned Sum = 0;
535 for (unsigned i = 0; i < OfC.size(); ++i)
536 Sum += OfC[i];
537 CW << Sum << " - ";
538
539 for (unsigned i = 0; i < OfC.size(); ++i) {
540 if (i) CW << ", ";
541 CW << OfC[i];
542 }
Chris Lattner697954c2002-01-20 22:54:45 +0000543 CW << "\n";
Chris Lattnera95c6992001-11-12 16:28:48 +0000544 }
Chris Lattner697954c2002-01-20 22:54:45 +0000545 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000546
547 CW << "Profile Field Access Percentages:\n";
Chris Lattner02868352003-04-22 21:22:33 +0000548 std::cout.precision(3);
Chris Lattner84efe092001-11-12 20:13:14 +0000549 for (I = FieldAccessCounts.begin(); I != E; ++I) {
Chris Lattner02868352003-04-22 21:22:33 +0000550 std::vector<unsigned> &OfC = I->second;
Chris Lattner84efe092001-11-12 20:13:14 +0000551 unsigned Sum = 0;
552 for (unsigned i = 0; i < OfC.size(); ++i)
553 Sum += OfC[i];
554
555 CW << " '" << (Value*)I->first << "'\t- ";
556 for (unsigned i = 0; i < OfC.size(); ++i) {
557 if (i) CW << ", ";
558 CW << double(OfC[i])/Sum;
559 }
Chris Lattner697954c2002-01-20 22:54:45 +0000560 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000561 }
Chris Lattner697954c2002-01-20 22:54:45 +0000562 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000563
Chris Lattnera95c6992001-11-12 16:28:48 +0000564 FieldAccessCounts.clear();
565 }
566#endif
567}
568
Chris Lattnere43db882001-10-27 04:15:57 +0000569void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000570 if (!QuietMode) {
Chris Lattner02868352003-04-22 21:22:33 +0000571 std::cout << "Program returned ";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000572 print(Type::IntTy, GV);
Chris Lattner02868352003-04-22 21:22:33 +0000573 std::cout << " via 'void exit(int)'\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000574 }
Chris Lattnere43db882001-10-27 04:15:57 +0000575
576 ExitCode = GV.SByteVal;
577 ECStack.clear();
Chris Lattnera95c6992001-11-12 16:28:48 +0000578 PerformExitStuff();
Chris Lattnere43db882001-10-27 04:15:57 +0000579}
580
Chris Lattnerd7916e92003-05-10 21:22:39 +0000581void Interpreter::visitReturnInst(ReturnInst &I) {
582 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000583 const Type *RetTy = 0;
584 GenericValue Result;
585
586 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000587 if (I.getNumOperands()) {
588 RetTy = I.getReturnValue()->getType();
589 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000590 }
591
592 // Save previously executing meth
Chris Lattnerda82ed52003-05-08 16:18:31 +0000593 const Function *M = ECStack.back().CurFunction;
Chris Lattner92101ac2001-08-23 17:05:04 +0000594
595 // Pop the current stack frame... this invalidates SF
596 ECStack.pop_back();
597
598 if (ECStack.empty()) { // Finished main. Put result into exit code...
599 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000600 if (!QuietMode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000601 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattnerf23eb852001-12-14 16:49:29 +0000602 << "\" returned ";
603 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000604 std::cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000605 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000606
607 if (RetTy->isIntegral())
Chris Lattnerf4dca802002-05-02 19:28:45 +0000608 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000609 } else {
610 ExitCode = 0;
611 }
Chris Lattnere2409062001-11-12 16:19:45 +0000612
Chris Lattnera95c6992001-11-12 16:28:48 +0000613 PerformExitStuff();
Chris Lattner92101ac2001-08-23 17:05:04 +0000614 return;
615 }
616
617 // If we have a previous stack frame, and we have a previous call, fill in
618 // the return value...
619 //
620 ExecutionContext &NewSF = ECStack.back();
621 if (NewSF.Caller) {
622 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
623 SetValue(NewSF.Caller, Result, NewSF);
624
625 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000626 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000627 // This must be a function that is executing because of a user 'call'
628 // instruction.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000629 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000630 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000631 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000632 std::cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000633 }
634}
635
Chris Lattnerd7916e92003-05-10 21:22:39 +0000636void Interpreter::visitBranchInst(BranchInst &I) {
637 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000638 BasicBlock *Dest;
639
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000640 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
641 if (!I.isUnconditional()) {
642 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000643 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000644 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000645 }
Chris Lattner77113b62003-05-10 20:21:16 +0000646 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000647}
648
Chris Lattnerd7916e92003-05-10 21:22:39 +0000649void Interpreter::visitSwitchInst(SwitchInst &I) {
650 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000651 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
652 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000653
654 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000655 BasicBlock *Dest = 0;
656 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000657 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000658 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000659 Dest = cast<BasicBlock>(I.getOperand(i+1));
660 break;
661 }
Chris Lattner09e93922003-04-22 20:34:47 +0000662
663 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000664 SwitchToNewBasicBlock(Dest, SF);
665}
666
667// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
668// This function handles the actual updating of block and instruction iterators
669// as well as execution of all of the PHI nodes in the destination block.
670//
671// This method does this because all of the PHI nodes must be executed
672// atomically, reading their inputs before any of the results are updated. Not
673// doing this can cause problems if the PHI nodes depend on other PHI nodes for
674// their inputs. If the input PHI node is updated before it is read, incorrect
675// results can happen. Thus we use a two phase approach.
676//
677void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
678 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
679 SF.CurBB = Dest; // Update CurBB to branch destination
680 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
681
682 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
683
684 // Loop over all of the PHI nodes in the current block, reading their inputs.
685 std::vector<GenericValue> ResultValues;
686
687 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000688 if (Trace) CW << "Run:" << PN;
689
Chris Lattner77113b62003-05-10 20:21:16 +0000690 // Search for the value corresponding to this previous bb...
691 int i = PN->getBasicBlockIndex(PrevBB);
692 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
693 Value *IncomingValue = PN->getIncomingValue(i);
694
695 // Save the incoming value for this PHI node...
696 ResultValues.push_back(getOperandValue(IncomingValue, SF));
697 }
698
699 // Now loop over all of the PHI nodes setting their values...
700 SF.CurInst = SF.CurBB->begin();
701 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
702 ++SF.CurInst, ++i)
703 SetValue(PN, ResultValues[i], SF);
Chris Lattner09e93922003-04-22 20:34:47 +0000704}
705
706
Chris Lattner92101ac2001-08-23 17:05:04 +0000707//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000708// Memory Instruction Implementations
709//===----------------------------------------------------------------------===//
710
Chris Lattnerd7916e92003-05-10 21:22:39 +0000711void Interpreter::visitAllocationInst(AllocationInst &I) {
712 ExecutionContext &SF = ECStack.back();
713
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000714 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000715
Chris Lattnercc82cc12002-04-28 21:57:33 +0000716 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000717 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000718
719 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000720 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000721 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
722
Chris Lattnerfe11a972002-12-23 23:59:41 +0000723 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000724 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000725 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000726
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000727 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000728 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000729}
730
Chris Lattnerd7916e92003-05-10 21:22:39 +0000731void Interpreter::visitFreeInst(FreeInst &I) {
732 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000733 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
734 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000735 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000736 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000737}
738
Chris Lattner95c3af52001-10-29 19:32:19 +0000739
Chris Lattnera34c5682002-08-27 22:33:45 +0000740// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000741//
Chris Lattnerfe11a972002-12-23 23:59:41 +0000742GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
743 User::op_iterator E,
744 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000745 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000746 "Cannot getElementOffset of a nonpointer type!");
747
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000748 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000749 const Type *Ty = Ptr->getType();
750
751 for (; I != E; ++I) {
Chris Lattner782b9392001-11-26 18:18:18 +0000752 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
753 const StructLayout *SLO = TD.getStructLayout(STy);
754
755 // Indicies must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000756 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000757 assert(CPU->getType() == Type::UByteTy);
758 unsigned Index = CPU->getValue();
759
Chris Lattnere2409062001-11-12 16:19:45 +0000760#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner782b9392001-11-26 18:18:18 +0000761 if (ProfileStructureFields) {
762 // Do accounting for this field...
Chris Lattner02868352003-04-22 21:22:33 +0000763 std::vector<unsigned> &OfC = FieldAccessCounts[STy];
Chris Lattner782b9392001-11-26 18:18:18 +0000764 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
765 OfC[Index]++;
766 }
Chris Lattnere2409062001-11-12 16:19:45 +0000767#endif
Chris Lattner782b9392001-11-26 18:18:18 +0000768
769 Total += SLO->MemberOffsets[Index];
770 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000771 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000772
Chris Lattner006a4a52003-02-25 21:14:59 +0000773 // Get the index number for the array... which must be long type...
Chris Lattner0374b8d2002-09-11 01:21:35 +0000774 assert((*I)->getType() == Type::LongTy);
Chris Lattnere8b3e9b2002-09-13 23:30:42 +0000775 unsigned Idx = getOperandValue(*I, SF).LongVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000776 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000777 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattner02868352003-04-22 21:22:33 +0000778 std::cerr << "Out of range memory access to element #" << Idx
779 << " of a " << AT->getNumElements() << " element array."
780 << " Subscript #" << *I << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000781 // Get outta here!!!
Chris Lattner74030252002-02-12 15:47:23 +0000782 siglongjmp(SignalRecoverBuffer, SIGTRAP);
Chris Lattnerf23eb852001-12-14 16:49:29 +0000783 }
Chris Lattner782b9392001-11-26 18:18:18 +0000784
Chris Lattnerf23eb852001-12-14 16:49:29 +0000785 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000786 unsigned Size = TD.getTypeSize(Ty);
787 Total += Size*Idx;
788 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000789 }
790
Chris Lattnera34c5682002-08-27 22:33:45 +0000791 GenericValue Result;
792 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
793 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000794}
795
Chris Lattnerd7916e92003-05-10 21:22:39 +0000796void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
797 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000798 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattnera34c5682002-08-27 22:33:45 +0000799 I.idx_begin(), I.idx_end(), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000800}
801
Chris Lattnerd7916e92003-05-10 21:22:39 +0000802void Interpreter::visitLoadInst(LoadInst &I) {
803 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000804 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000805 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000806 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000807 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000808}
809
Chris Lattnerd7916e92003-05-10 21:22:39 +0000810void Interpreter::visitStoreInst(StoreInst &I) {
811 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000812 GenericValue Val = getOperandValue(I.getOperand(0), SF);
813 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000814 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000815 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000816}
817
Chris Lattner86660982001-08-27 05:16:50 +0000818
Chris Lattnerab2dea52002-11-07 19:29:31 +0000819
Chris Lattner86660982001-08-27 05:16:50 +0000820//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000821// Miscellaneous Instruction Implementations
822//===----------------------------------------------------------------------===//
823
Chris Lattnerd7916e92003-05-10 21:22:39 +0000824void Interpreter::visitCallInst(CallInst &I) {
825 ExecutionContext &SF = ECStack.back();
826 SF.Caller = &I;
Chris Lattner02868352003-04-22 21:22:33 +0000827 std::vector<GenericValue> ArgVals;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000828 ArgVals.reserve(I.getNumOperands()-1);
Chris Lattner93780132003-01-13 00:58:52 +0000829 for (unsigned i = 1; i < I.getNumOperands(); ++i) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000830 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
Chris Lattner93780132003-01-13 00:58:52 +0000831 // Promote all integral types whose size is < sizeof(int) into ints. We do
832 // this by zero or sign extending the value as appropriate according to the
833 // source type.
834 if (I.getOperand(i)->getType()->isIntegral() &&
835 I.getOperand(i)->getType()->getPrimitiveSize() < 4) {
836 const Type *Ty = I.getOperand(i)->getType();
837 if (Ty == Type::ShortTy)
838 ArgVals.back().IntVal = ArgVals.back().ShortVal;
839 else if (Ty == Type::UShortTy)
840 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
841 else if (Ty == Type::SByteTy)
842 ArgVals.back().IntVal = ArgVals.back().SByteVal;
843 else if (Ty == Type::UByteTy)
844 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
845 else if (Ty == Type::BoolTy)
846 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
847 else
848 assert(0 && "Unknown type!");
849 }
850 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000851
Chris Lattner070cf5e2001-11-07 20:12:30 +0000852 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000853 // and treat it as a function pointer.
Chris Lattnerd7916e92003-05-10 21:22:39 +0000854 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000855 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000856}
857
Chris Lattner86660982001-08-27 05:16:50 +0000858#define IMPLEMENT_SHIFT(OP, TY) \
859 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
860
Chris Lattnerd7916e92003-05-10 21:22:39 +0000861void Interpreter::visitShl(ShiftInst &I) {
862 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000863 const Type *Ty = I.getOperand(0)->getType();
864 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
865 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000866 GenericValue Dest;
867
868 switch (Ty->getPrimitiveID()) {
869 IMPLEMENT_SHIFT(<<, UByte);
870 IMPLEMENT_SHIFT(<<, SByte);
871 IMPLEMENT_SHIFT(<<, UShort);
872 IMPLEMENT_SHIFT(<<, Short);
873 IMPLEMENT_SHIFT(<<, UInt);
874 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000875 IMPLEMENT_SHIFT(<<, ULong);
876 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000877 default:
Chris Lattner02868352003-04-22 21:22:33 +0000878 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000879 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000880 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000881}
882
Chris Lattnerd7916e92003-05-10 21:22:39 +0000883void Interpreter::visitShr(ShiftInst &I) {
884 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000885 const Type *Ty = I.getOperand(0)->getType();
886 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
887 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000888 GenericValue Dest;
889
890 switch (Ty->getPrimitiveID()) {
891 IMPLEMENT_SHIFT(>>, UByte);
892 IMPLEMENT_SHIFT(>>, SByte);
893 IMPLEMENT_SHIFT(>>, UShort);
894 IMPLEMENT_SHIFT(>>, Short);
895 IMPLEMENT_SHIFT(>>, UInt);
896 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000897 IMPLEMENT_SHIFT(>>, ULong);
898 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000899 default:
Chris Lattner02868352003-04-22 21:22:33 +0000900 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
901 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000902 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000903 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000904}
905
906#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000907 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000908
909#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
910 case Type::DESTTY##TyID: \
911 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000912 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000913 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
914 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
915 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
916 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
917 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000918 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
919 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000920 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
921 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000922
923#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
924 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
925 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
926
927#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner02868352003-04-22 21:22:33 +0000928 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
929 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000930 } \
931 break
932
933#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
934 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
935 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000936 IMPLEMENT_CAST_CASE_END()
937
Chris Lattnera34c5682002-08-27 22:33:45 +0000938static GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
939 ExecutionContext &SF) {
940 const Type *SrcTy = SrcVal->getType();
941 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000942
943 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000944 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
945 IMPLEMENT_CAST_CASE(SByte , ( signed char));
946 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000947 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000948 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
949 IMPLEMENT_CAST_CASE(Int , ( signed int ));
950 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
951 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000952 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000953 IMPLEMENT_CAST_CASE(Float , (float));
954 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +0000955 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +0000956 default:
Chris Lattner02868352003-04-22 21:22:33 +0000957 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +0000958 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000959 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000960
961 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000962}
Chris Lattner92101ac2001-08-23 17:05:04 +0000963
964
Chris Lattnerd7916e92003-05-10 21:22:39 +0000965void Interpreter::visitCastInst(CastInst &I) {
966 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +0000967 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
968}
Chris Lattner92101ac2001-08-23 17:05:04 +0000969
Chris Lattnerd7916e92003-05-10 21:22:39 +0000970void Interpreter::visitVarArgInst(VarArgInst &I) {
971 ExecutionContext &SF = ECStack.back();
972
Chris Lattner374344c2003-05-08 16:52:43 +0000973 // Get the pointer to the valist element. LLI treats the valist in memory as
974 // an integer.
975 GenericValue VAListPtr = getOperandValue(I.getOperand(0), SF);
976
977 // Load the pointer
978 GenericValue VAList =
979 TheEE->LoadValueFromMemory((GenericValue *)GVTOP(VAListPtr), Type::UIntTy);
980
981 unsigned Argument = VAList.IntVal++;
982
983 // Update the valist to point to the next argument...
984 TheEE->StoreValueToMemory(VAList, (GenericValue *)GVTOP(VAListPtr),
985 Type::UIntTy);
986
987 // Set the value...
988 assert(Argument < SF.VarArgs.size() &&
989 "Accessing past the last vararg argument!");
990 SetValue(&I, SF.VarArgs[Argument], SF);
991}
Chris Lattner92101ac2001-08-23 17:05:04 +0000992
993//===----------------------------------------------------------------------===//
994// Dispatch and Execution Code
995//===----------------------------------------------------------------------===//
996
Chris Lattnerda82ed52003-05-08 16:18:31 +0000997FunctionInfo::FunctionInfo(Function *F) : Annotation(FunctionInfoAID) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000998 // Assign slot numbers to the function arguments...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000999 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
1000 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001001
1002 // Iterate over all of the instructions...
1003 unsigned InstNum = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001004 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
1005 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
1006 // For each instruction... Add Annote
1007 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001008}
1009
Chris Lattnerda82ed52003-05-08 16:18:31 +00001010unsigned FunctionInfo::getValueSlot(const Value *V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001011 unsigned Plane = V->getType()->getUniqueID();
1012 if (Plane >= NumPlaneElements.size())
1013 NumPlaneElements.resize(Plane+1, 0);
1014 return NumPlaneElements[Plane]++;
1015}
1016
1017
Chris Lattner92101ac2001-08-23 17:05:04 +00001018//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001019// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001020//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001021void Interpreter::callFunction(Function *F,
1022 const std::vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +00001023 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1024 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1025 "Incorrect number of arguments passed into function call!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001026 if (F->isExternal()) {
Chris Lattnerda82ed52003-05-08 16:18:31 +00001027 GenericValue Result = callExternalFunction(F, ArgVals);
Chris Lattnercdf51782003-05-08 16:06:52 +00001028 const Type *RetTy = F->getReturnType();
Chris Lattnerbb76f022001-10-30 20:27:31 +00001029
1030 // Copy the result back into the result variable if we are not returning
1031 // void.
1032 if (RetTy != Type::VoidTy) {
1033 if (!ECStack.empty() && ECStack.back().Caller) {
1034 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +00001035 SetValue(SF.Caller, Result, SF);
1036
1037 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +00001038 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001039 // print it.
Chris Lattnercdf51782003-05-08 16:06:52 +00001040 CW << "Function " << F->getType() << " \"" << F->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +00001041 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001042 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +00001043 std::cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001044
1045 if (RetTy->isIntegral())
Chris Lattner0c4e8862002-09-03 01:08:28 +00001046 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattnerbb76f022001-10-30 20:27:31 +00001047 }
1048 }
1049
Chris Lattner92101ac2001-08-23 17:05:04 +00001050 return;
1051 }
1052
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001053 // Process the function, assigning instruction numbers to the instructions in
1054 // the function. Also calculate the number of values for each type slot
1055 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +00001056 //
Chris Lattnerda82ed52003-05-08 16:18:31 +00001057 FunctionInfo *FuncInfo =
1058 (FunctionInfo*)F->getOrCreateAnnotation(FunctionInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001059 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001060
Chris Lattner92101ac2001-08-23 17:05:04 +00001061 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
Chris Lattnerda82ed52003-05-08 16:18:31 +00001062 StackFrame.CurFunction = F;
Chris Lattnercdf51782003-05-08 16:06:52 +00001063 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001064 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001065 StackFrame.FuncInfo = FuncInfo;
Chris Lattner92101ac2001-08-23 17:05:04 +00001066
1067 // Initialize the values to nothing...
Chris Lattnerda82ed52003-05-08 16:18:31 +00001068 StackFrame.Values.resize(FuncInfo->NumPlaneElements.size());
1069 for (unsigned i = 0; i < FuncInfo->NumPlaneElements.size(); ++i) {
1070 StackFrame.Values[i].resize(FuncInfo->NumPlaneElements[i]);
Chris Lattner92101ac2001-08-23 17:05:04 +00001071
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001072 // Taint the initial values of stuff
1073 memset(&StackFrame.Values[i][0], 42,
Chris Lattnerda82ed52003-05-08 16:18:31 +00001074 FuncInfo->NumPlaneElements[i]*sizeof(GenericValue));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001075 }
1076
Chris Lattner92101ac2001-08-23 17:05:04 +00001077
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001078 // Run through the function arguments and initialize their values...
Chris Lattnercdf51782003-05-08 16:06:52 +00001079 assert((ArgVals.size() == F->asize() ||
1080 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001081 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001082
1083 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001084 unsigned i = 0;
Chris Lattnercdf51782003-05-08 16:06:52 +00001085 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001086 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001087
1088 // Handle varargs arguments...
1089 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001090}
1091
1092// executeInstruction - Interpret a single instruction, increment the "PC", and
1093// return true if the next instruction is a breakpoint...
1094//
1095bool Interpreter::executeInstruction() {
1096 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1097
1098 ExecutionContext &SF = ECStack.back(); // Current stack frame
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001099 Instruction &I = *SF.CurInst++; // Increment before execute
Chris Lattner92101ac2001-08-23 17:05:04 +00001100
Chris Lattnerd7916e92003-05-10 21:22:39 +00001101 if (Trace) CW << "Run:" << I;
Chris Lattner5af0c482001-11-07 04:23:00 +00001102
Chris Lattnerbbdabce2002-12-08 05:51:08 +00001103 // Track the number of dynamic instructions executed.
1104 ++NumDynamicInsts;
1105
Chris Lattner5af0c482001-11-07 04:23:00 +00001106 // Set a sigsetjmp buffer so that we can recover if an error happens during
1107 // instruction execution...
1108 //
1109 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1110 --SF.CurInst; // Back up to erroring instruction
Chris Lattner74030252002-02-12 15:47:23 +00001111 if (SigNo != SIGINT) {
Chris Lattner02868352003-04-22 21:22:33 +00001112 std::cout << "EXCEPTION OCCURRED [" << strsignal(SigNo) << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001113 printStackTrace();
Chris Lattner74030252002-02-12 15:47:23 +00001114 // If -abort-on-exception was specified, terminate LLI instead of trying
1115 // to debug it.
1116 //
1117 if (AbortOnExceptions) exit(1);
Chris Lattner782b9392001-11-26 18:18:18 +00001118 } else if (SigNo == SIGINT) {
Chris Lattner02868352003-04-22 21:22:33 +00001119 std::cout << "CTRL-C Detected, execution halted.\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001120 }
1121 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001122 return true;
1123 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001124
Chris Lattner461f02f2001-11-07 05:31:27 +00001125 InInstruction = true;
Chris Lattnerd7916e92003-05-10 21:22:39 +00001126 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner461f02f2001-11-07 05:31:27 +00001127 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001128
1129 // Reset the current frame location to the top of stack
1130 CurFrame = ECStack.size()-1;
1131
1132 if (CurFrame == -1) return false; // No breakpoint if no code
1133
1134 // Return true if there is a breakpoint annotation on the instruction...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001135 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
Chris Lattner92101ac2001-08-23 17:05:04 +00001136}
1137
1138void Interpreter::stepInstruction() { // Do the 'step' command
1139 if (ECStack.empty()) {
Chris Lattner02868352003-04-22 21:22:33 +00001140 std::cout << "Error: no program running, cannot step!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001141 return;
1142 }
1143
1144 // Run an instruction...
1145 executeInstruction();
1146
1147 // Print the next instruction to execute...
1148 printCurrentInstruction();
1149}
1150
1151// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001152void Interpreter::nextInstruction() { // Do the 'next' command
1153 if (ECStack.empty()) {
Chris Lattner02868352003-04-22 21:22:33 +00001154 std::cout << "Error: no program running, cannot 'next'!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001155 return;
1156 }
1157
1158 // If this is a call instruction, step over the call instruction...
1159 // TODO: ICALL, CALL WITH, ...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001160 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001161 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001162 // Step into the function...
1163 if (executeInstruction()) {
1164 // Hit a breakpoint, print current instruction, then return to user...
Chris Lattner02868352003-04-22 21:22:33 +00001165 std::cout << "Breakpoint hit!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001166 printCurrentInstruction();
1167 return;
1168 }
1169
Chris Lattnera74a6b52001-10-29 14:08:33 +00001170 // If we we able to step into the function, finish it now. We might not be
1171 // able the step into a function, if it's external for example.
1172 if (ECStack.size() != StackSize)
1173 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001174 else
1175 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001176
Chris Lattner92101ac2001-08-23 17:05:04 +00001177 } else {
1178 // Normal instruction, just step...
1179 stepInstruction();
1180 }
1181}
1182
1183void Interpreter::run() {
1184 if (ECStack.empty()) {
Chris Lattner02868352003-04-22 21:22:33 +00001185 std::cout << "Error: no program running, cannot run!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001186 return;
1187 }
1188
1189 bool HitBreakpoint = false;
1190 while (!ECStack.empty() && !HitBreakpoint) {
1191 // Run an instruction...
1192 HitBreakpoint = executeInstruction();
1193 }
1194
Chris Lattner02868352003-04-22 21:22:33 +00001195 if (HitBreakpoint)
1196 std::cout << "Breakpoint hit!\n";
1197
Chris Lattner92101ac2001-08-23 17:05:04 +00001198 // Print the next instruction to execute...
1199 printCurrentInstruction();
1200}
1201
1202void Interpreter::finish() {
1203 if (ECStack.empty()) {
Chris Lattner02868352003-04-22 21:22:33 +00001204 std::cout << "Error: no program running, cannot run!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001205 return;
1206 }
1207
1208 unsigned StackSize = ECStack.size();
1209 bool HitBreakpoint = false;
1210 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1211 // Run an instruction...
1212 HitBreakpoint = executeInstruction();
1213 }
1214
Chris Lattner02868352003-04-22 21:22:33 +00001215 if (HitBreakpoint)
1216 std::cout << "Breakpoint hit!\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001217
1218 // Print the next instruction to execute...
1219 printCurrentInstruction();
1220}
1221
1222
1223
1224// printCurrentInstruction - Print out the instruction that the virtual PC is
1225// at, or fail silently if no program is running.
1226//
1227void Interpreter::printCurrentInstruction() {
1228 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001229 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
Chris Lattner02868352003-04-22 21:22:33 +00001230 WriteAsOperand(std::cout, ECStack.back().CurBB) << ":\n";
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001231
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001232 Instruction &I = *ECStack.back().CurInst;
1233 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001234 assert(IN && "Instruction has no numbering annotation!");
Chris Lattner02868352003-04-22 21:22:33 +00001235 std::cout << "#" << IN->InstNum << I;
Chris Lattner92101ac2001-08-23 17:05:04 +00001236 }
1237}
1238
1239void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001240 switch (Ty->getPrimitiveID()) {
Chris Lattner02868352003-04-22 21:22:33 +00001241 case Type::BoolTyID: std::cout << (V.BoolVal?"true":"false"); break;
Chris Lattner65629d52002-08-13 20:45:11 +00001242 case Type::SByteTyID:
Chris Lattner02868352003-04-22 21:22:33 +00001243 std::cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
Chris Lattner65629d52002-08-13 20:45:11 +00001244 case Type::UByteTyID:
Chris Lattner02868352003-04-22 21:22:33 +00001245 std::cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
1246 case Type::ShortTyID: std::cout << V.ShortVal; break;
1247 case Type::UShortTyID: std::cout << V.UShortVal; break;
1248 case Type::IntTyID: std::cout << V.IntVal; break;
1249 case Type::UIntTyID: std::cout << V.UIntVal; break;
1250 case Type::LongTyID: std::cout << (long)V.LongVal; break;
1251 case Type::ULongTyID: std::cout << (unsigned long)V.ULongVal; break;
1252 case Type::FloatTyID: std::cout << V.FloatVal; break;
1253 case Type::DoubleTyID: std::cout << V.DoubleVal; break;
1254 case Type::PointerTyID:std::cout << (void*)GVTOP(V); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001255 default:
Chris Lattner02868352003-04-22 21:22:33 +00001256 std::cout << "- Don't know how to print value of this type!";
Chris Lattner92101ac2001-08-23 17:05:04 +00001257 break;
1258 }
1259}
1260
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001261void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001262 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001263 printValue(Ty, V);
1264}
1265
Chris Lattner697954c2002-01-20 22:54:45 +00001266void Interpreter::print(const std::string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001267 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1268 if (!PickedVal) return;
1269
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001270 if (const Function *F = dyn_cast<const Function>(PickedVal)) {
1271 CW << F; // Print the function
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001272 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001273 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001274 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1275 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001276 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001277 print(PickedVal->getType(),
1278 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner02868352003-04-22 21:22:33 +00001279 std::cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001280 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001281}
1282
Chris Lattner697954c2002-01-20 22:54:45 +00001283void Interpreter::infoValue(const std::string &Name) {
Chris Lattner86660982001-08-27 05:16:50 +00001284 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1285 if (!PickedVal) return;
1286
Chris Lattner02868352003-04-22 21:22:33 +00001287 std::cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001288 print(PickedVal->getType(),
1289 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner02868352003-04-22 21:22:33 +00001290 std::cout << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001291 printOperandInfo(PickedVal, ECStack[CurFrame]);
1292}
1293
Chris Lattner461f02f2001-11-07 05:31:27 +00001294// printStackFrame - Print information about the specified stack frame, or -1
1295// for the default one.
1296//
Chris Lattner601d7152002-07-25 17:37:05 +00001297void Interpreter::printStackFrame(int FrameNo) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001298 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattnerda82ed52003-05-08 16:18:31 +00001299 Function *F = ECStack[FrameNo].CurFunction;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001300 const Type *RetTy = F->getReturnType();
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001301
1302 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001303 << (Value*)RetTy << " \"" << F->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001304
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001305 unsigned i = 0;
1306 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
Chris Lattner02868352003-04-22 21:22:33 +00001307 if (i != 0) std::cout << ", ";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001308 CW << *I << "=";
Chris Lattner461f02f2001-11-07 05:31:27 +00001309
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001310 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001311 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001312
Chris Lattner02868352003-04-22 21:22:33 +00001313 std::cout << ")\n";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001314
1315 if (FrameNo != int(ECStack.size()-1)) {
1316 BasicBlock::iterator I = ECStack[FrameNo].CurInst;
1317 CW << --I;
1318 } else {
1319 CW << *ECStack[FrameNo].CurInst;
1320 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001321}
Chris Lattner461f02f2001-11-07 05:31:27 +00001322