blob: 7d9d727e76674ecdd824cc7639af1e1a9bdd3d60 [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"
Brian Gaeke235b2002003-10-10 17:03:22 +000016#include <cmath> // For fmod
Chris Lattner2e42d3a2001-10-15 05:51:48 +000017
Chris Lattnerfe11a972002-12-23 23:59:41 +000018Interpreter *TheEE = 0;
19
Chris Lattnerbbdabce2002-12-08 05:51:08 +000020namespace {
21 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
Chris Lattner138b0cd2002-12-08 06:01:34 +000022
23 cl::opt<bool>
Chris Lattnerfe11a972002-12-23 23:59:41 +000024 QuietMode("quiet", cl::desc("Do not emit any non-program output"),
25 cl::init(true));
Chris Lattner138b0cd2002-12-08 06:01:34 +000026
27 cl::alias
28 QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode));
29
30 cl::opt<bool>
31 ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks"));
Chris Lattnerbbdabce2002-12-08 05:51:08 +000032}
33
Chris Lattner2e42d3a2001-10-15 05:51:48 +000034// Create a TargetData structure to handle memory addressing and size/alignment
35// computations
36//
Chris Lattnerea38c0e2001-11-07 19:46:27 +000037CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000038
Chris Lattner2e42d3a2001-10-15 05:51:48 +000039
40//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000041// Value Manipulation code
42//===----------------------------------------------------------------------===//
43
44static unsigned getOperandSlot(Value *V) {
45 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
46 assert(SN && "Operand does not have a slot number annotation!");
47 return SN->SlotNum;
48}
49
Chris Lattnera34c5682002-08-27 22:33:45 +000050// Operations used by constant expr implementations...
51static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
52 ExecutionContext &SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000053static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000054 const Type *Ty);
Chris Lattnera34c5682002-08-27 22:33:45 +000055
Chris Lattnerfddc7552002-10-15 20:34:05 +000056
Brian Gaeke29794cb2003-09-05 18:55:03 +000057GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +000058 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
59 switch (CE->getOpcode()) {
60 case Instruction::Cast:
61 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
62 case Instruction::GetElementPtr:
Chris Lattnerfe11a972002-12-23 23:59:41 +000063 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
64 CE->op_end(), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000065 case Instruction::Add:
66 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
67 getOperandValue(CE->getOperand(1), SF),
Chris Lattnerb945e4d2003-04-22 20:37:39 +000068 CE->getType());
Chris Lattnera34c5682002-08-27 22:33:45 +000069 default:
Chris Lattner02868352003-04-22 21:22:33 +000070 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
Chris Lattnera34c5682002-08-27 22:33:45 +000071 abort();
Chris Lattner04e2ad72003-04-21 22:43:32 +000072 return GenericValue();
Chris Lattnera34c5682002-08-27 22:33:45 +000073 }
74 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000075 return TheEE->getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +000076 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000077 return PTOGV(TheEE->getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +000078 } else {
79 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +000080 unsigned OpSlot = getOperandSlot(V);
81 assert(TyP < SF.Values.size() &&
82 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +000083 return SF.Values[TyP][getOperandSlot(V)];
84 }
85}
86
Chris Lattner39bb5b42001-10-15 13:25:40 +000087static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
88 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
89
Chris Lattner02868352003-04-22 21:22:33 +000090 //std::cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)]<< "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +000091 SF.Values[TyP][getOperandSlot(V)] = Val;
92}
93
Chris Lattner39bb5b42001-10-15 13:25:40 +000094//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +000095// Annotation Wrangling code
96//===----------------------------------------------------------------------===//
97
98void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +000099 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000100}
101
Chris Lattner2adcd832002-05-03 19:52:30 +0000102//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000103// Binary Instruction Implementations
104//===----------------------------------------------------------------------===//
105
106#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
107 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
108
109static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000110 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000111 GenericValue Dest;
112 switch (Ty->getPrimitiveID()) {
113 IMPLEMENT_BINARY_OPERATOR(+, UByte);
114 IMPLEMENT_BINARY_OPERATOR(+, SByte);
115 IMPLEMENT_BINARY_OPERATOR(+, UShort);
116 IMPLEMENT_BINARY_OPERATOR(+, Short);
117 IMPLEMENT_BINARY_OPERATOR(+, UInt);
118 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000119 IMPLEMENT_BINARY_OPERATOR(+, ULong);
120 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000121 IMPLEMENT_BINARY_OPERATOR(+, Float);
122 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000123 default:
Chris Lattner02868352003-04-22 21:22:33 +0000124 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
125 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000126 }
127 return Dest;
128}
129
130static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000131 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000132 GenericValue Dest;
133 switch (Ty->getPrimitiveID()) {
134 IMPLEMENT_BINARY_OPERATOR(-, UByte);
135 IMPLEMENT_BINARY_OPERATOR(-, SByte);
136 IMPLEMENT_BINARY_OPERATOR(-, UShort);
137 IMPLEMENT_BINARY_OPERATOR(-, Short);
138 IMPLEMENT_BINARY_OPERATOR(-, UInt);
139 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000140 IMPLEMENT_BINARY_OPERATOR(-, ULong);
141 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000142 IMPLEMENT_BINARY_OPERATOR(-, Float);
143 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000144 default:
Chris Lattner02868352003-04-22 21:22:33 +0000145 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
146 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000147 }
148 return Dest;
149}
150
Chris Lattnerc2593162001-10-27 08:28:11 +0000151static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000152 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000153 GenericValue Dest;
154 switch (Ty->getPrimitiveID()) {
155 IMPLEMENT_BINARY_OPERATOR(*, UByte);
156 IMPLEMENT_BINARY_OPERATOR(*, SByte);
157 IMPLEMENT_BINARY_OPERATOR(*, UShort);
158 IMPLEMENT_BINARY_OPERATOR(*, Short);
159 IMPLEMENT_BINARY_OPERATOR(*, UInt);
160 IMPLEMENT_BINARY_OPERATOR(*, Int);
161 IMPLEMENT_BINARY_OPERATOR(*, ULong);
162 IMPLEMENT_BINARY_OPERATOR(*, Long);
163 IMPLEMENT_BINARY_OPERATOR(*, Float);
164 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000165 default:
Chris Lattner02868352003-04-22 21:22:33 +0000166 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
167 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000168 }
169 return Dest;
170}
171
172static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000173 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +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);
182 IMPLEMENT_BINARY_OPERATOR(/, ULong);
183 IMPLEMENT_BINARY_OPERATOR(/, Long);
184 IMPLEMENT_BINARY_OPERATOR(/, Float);
185 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000186 default:
Chris Lattner02868352003-04-22 21:22:33 +0000187 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
188 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000189 }
190 return Dest;
191}
192
193static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000194 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +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);
203 IMPLEMENT_BINARY_OPERATOR(%, ULong);
204 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000205 case Type::FloatTyID:
206 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
207 break;
208 case Type::DoubleTyID:
209 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
210 break;
211 default:
Chris Lattner02868352003-04-22 21:22:33 +0000212 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
213 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000214 }
215 return Dest;
216}
217
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000218static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000219 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000220 GenericValue Dest;
221 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000222 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000223 IMPLEMENT_BINARY_OPERATOR(&, UByte);
224 IMPLEMENT_BINARY_OPERATOR(&, SByte);
225 IMPLEMENT_BINARY_OPERATOR(&, UShort);
226 IMPLEMENT_BINARY_OPERATOR(&, Short);
227 IMPLEMENT_BINARY_OPERATOR(&, UInt);
228 IMPLEMENT_BINARY_OPERATOR(&, Int);
229 IMPLEMENT_BINARY_OPERATOR(&, ULong);
230 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000231 default:
Chris Lattner02868352003-04-22 21:22:33 +0000232 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
233 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000234 }
235 return Dest;
236}
237
238
239static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000240 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000241 GenericValue Dest;
242 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000243 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000244 IMPLEMENT_BINARY_OPERATOR(|, UByte);
245 IMPLEMENT_BINARY_OPERATOR(|, SByte);
246 IMPLEMENT_BINARY_OPERATOR(|, UShort);
247 IMPLEMENT_BINARY_OPERATOR(|, Short);
248 IMPLEMENT_BINARY_OPERATOR(|, UInt);
249 IMPLEMENT_BINARY_OPERATOR(|, Int);
250 IMPLEMENT_BINARY_OPERATOR(|, ULong);
251 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000252 default:
Chris Lattner02868352003-04-22 21:22:33 +0000253 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
254 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000255 }
256 return Dest;
257}
258
259
260static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000261 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000262 GenericValue Dest;
263 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000264 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000265 IMPLEMENT_BINARY_OPERATOR(^, UByte);
266 IMPLEMENT_BINARY_OPERATOR(^, SByte);
267 IMPLEMENT_BINARY_OPERATOR(^, UShort);
268 IMPLEMENT_BINARY_OPERATOR(^, Short);
269 IMPLEMENT_BINARY_OPERATOR(^, UInt);
270 IMPLEMENT_BINARY_OPERATOR(^, Int);
271 IMPLEMENT_BINARY_OPERATOR(^, ULong);
272 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000273 default:
Chris Lattner02868352003-04-22 21:22:33 +0000274 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
275 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000276 }
277 return Dest;
278}
279
280
Chris Lattner92101ac2001-08-23 17:05:04 +0000281#define IMPLEMENT_SETCC(OP, TY) \
282 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
283
Chris Lattnerfd506f52003-04-23 19:55:35 +0000284// Handle pointers specially because they must be compared with only as much
285// width as the host has. We _do not_ want to be comparing 64 bit values when
286// running on a 32-bit target, otherwise the upper 32 bits might mess up
287// comparisons if they contain garbage.
288#define IMPLEMENT_POINTERSETCC(OP) \
289 case Type::PointerTyID: \
290 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
291 (void*)(intptr_t)Src2.PointerVal; break
292
Chris Lattner92101ac2001-08-23 17:05:04 +0000293static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000294 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000295 GenericValue Dest;
296 switch (Ty->getPrimitiveID()) {
297 IMPLEMENT_SETCC(==, UByte);
298 IMPLEMENT_SETCC(==, SByte);
299 IMPLEMENT_SETCC(==, UShort);
300 IMPLEMENT_SETCC(==, Short);
301 IMPLEMENT_SETCC(==, UInt);
302 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000303 IMPLEMENT_SETCC(==, ULong);
304 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000305 IMPLEMENT_SETCC(==, Float);
306 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000307 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000308 default:
Chris Lattner02868352003-04-22 21:22:33 +0000309 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
310 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000311 }
312 return Dest;
313}
314
315static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000316 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000317 GenericValue Dest;
318 switch (Ty->getPrimitiveID()) {
319 IMPLEMENT_SETCC(!=, UByte);
320 IMPLEMENT_SETCC(!=, SByte);
321 IMPLEMENT_SETCC(!=, UShort);
322 IMPLEMENT_SETCC(!=, Short);
323 IMPLEMENT_SETCC(!=, UInt);
324 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000325 IMPLEMENT_SETCC(!=, ULong);
326 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000327 IMPLEMENT_SETCC(!=, Float);
328 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000329 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000330
Chris Lattner92101ac2001-08-23 17:05:04 +0000331 default:
Chris Lattner02868352003-04-22 21:22:33 +0000332 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
333 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000334 }
335 return Dest;
336}
337
338static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000339 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000340 GenericValue Dest;
341 switch (Ty->getPrimitiveID()) {
342 IMPLEMENT_SETCC(<=, UByte);
343 IMPLEMENT_SETCC(<=, SByte);
344 IMPLEMENT_SETCC(<=, UShort);
345 IMPLEMENT_SETCC(<=, Short);
346 IMPLEMENT_SETCC(<=, UInt);
347 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000348 IMPLEMENT_SETCC(<=, ULong);
349 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000350 IMPLEMENT_SETCC(<=, Float);
351 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000352 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000353 default:
Chris Lattner02868352003-04-22 21:22:33 +0000354 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
355 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000356 }
357 return Dest;
358}
359
360static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000361 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000362 GenericValue Dest;
363 switch (Ty->getPrimitiveID()) {
364 IMPLEMENT_SETCC(>=, UByte);
365 IMPLEMENT_SETCC(>=, SByte);
366 IMPLEMENT_SETCC(>=, UShort);
367 IMPLEMENT_SETCC(>=, Short);
368 IMPLEMENT_SETCC(>=, UInt);
369 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000370 IMPLEMENT_SETCC(>=, ULong);
371 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000372 IMPLEMENT_SETCC(>=, Float);
373 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000374 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000375 default:
Chris Lattner02868352003-04-22 21:22:33 +0000376 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
377 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000378 }
379 return Dest;
380}
381
382static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000383 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000384 GenericValue Dest;
385 switch (Ty->getPrimitiveID()) {
386 IMPLEMENT_SETCC(<, UByte);
387 IMPLEMENT_SETCC(<, SByte);
388 IMPLEMENT_SETCC(<, UShort);
389 IMPLEMENT_SETCC(<, Short);
390 IMPLEMENT_SETCC(<, UInt);
391 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000392 IMPLEMENT_SETCC(<, ULong);
393 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000394 IMPLEMENT_SETCC(<, Float);
395 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000396 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000397 default:
Chris Lattner02868352003-04-22 21:22:33 +0000398 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
399 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000400 }
401 return Dest;
402}
403
404static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000405 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000406 GenericValue Dest;
407 switch (Ty->getPrimitiveID()) {
408 IMPLEMENT_SETCC(>, UByte);
409 IMPLEMENT_SETCC(>, SByte);
410 IMPLEMENT_SETCC(>, UShort);
411 IMPLEMENT_SETCC(>, Short);
412 IMPLEMENT_SETCC(>, UInt);
413 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000414 IMPLEMENT_SETCC(>, ULong);
415 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000416 IMPLEMENT_SETCC(>, Float);
417 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000418 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000419 default:
Chris Lattner02868352003-04-22 21:22:33 +0000420 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
421 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000422 }
423 return Dest;
424}
425
Chris Lattnerd7916e92003-05-10 21:22:39 +0000426void Interpreter::visitBinaryOperator(BinaryOperator &I) {
427 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000428 const Type *Ty = I.getOperand(0)->getType();
429 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
430 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000431 GenericValue R; // Result
432
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000433 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000434 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
435 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
436 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
437 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
438 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
439 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
440 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
441 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
442 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
443 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
444 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
445 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
446 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
447 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000448 default:
Chris Lattner02868352003-04-22 21:22:33 +0000449 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
450 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000451 }
452
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000453 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000454}
455
Chris Lattner92101ac2001-08-23 17:05:04 +0000456//===----------------------------------------------------------------------===//
457// Terminator Instruction Implementations
458//===----------------------------------------------------------------------===//
459
Chris Lattnere43db882001-10-27 04:15:57 +0000460void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000461 if (!QuietMode) {
Chris Lattner02868352003-04-22 21:22:33 +0000462 std::cout << "Program returned ";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000463 print(Type::IntTy, GV);
Chris Lattner02868352003-04-22 21:22:33 +0000464 std::cout << " via 'void exit(int)'\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000465 }
Chris Lattnere43db882001-10-27 04:15:57 +0000466
467 ExitCode = GV.SByteVal;
468 ECStack.clear();
469}
470
Chris Lattnerd7916e92003-05-10 21:22:39 +0000471void Interpreter::visitReturnInst(ReturnInst &I) {
472 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000473 const Type *RetTy = 0;
474 GenericValue Result;
475
476 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000477 if (I.getNumOperands()) {
478 RetTy = I.getReturnValue()->getType();
479 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000480 }
481
482 // Save previously executing meth
Chris Lattnerda82ed52003-05-08 16:18:31 +0000483 const Function *M = ECStack.back().CurFunction;
Chris Lattner92101ac2001-08-23 17:05:04 +0000484
485 // Pop the current stack frame... this invalidates SF
486 ECStack.pop_back();
487
488 if (ECStack.empty()) { // Finished main. Put result into exit code...
489 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000490 if (!QuietMode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000491 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattnerf23eb852001-12-14 16:49:29 +0000492 << "\" returned ";
493 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000494 std::cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000495 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000496
497 if (RetTy->isIntegral())
Chris Lattnerf4dca802002-05-02 19:28:45 +0000498 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000499 } else {
500 ExitCode = 0;
501 }
502 return;
503 }
504
505 // If we have a previous stack frame, and we have a previous call, fill in
506 // the return value...
507 //
508 ExecutionContext &NewSF = ECStack.back();
509 if (NewSF.Caller) {
510 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
511 SetValue(NewSF.Caller, Result, NewSF);
512
513 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000514 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000515 // This must be a function that is executing because of a user 'call'
516 // instruction.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000517 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000518 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000519 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000520 std::cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000521 }
522}
523
Chris Lattnerd7916e92003-05-10 21:22:39 +0000524void Interpreter::visitBranchInst(BranchInst &I) {
525 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000526 BasicBlock *Dest;
527
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000528 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
529 if (!I.isUnconditional()) {
530 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000531 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000532 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000533 }
Chris Lattner77113b62003-05-10 20:21:16 +0000534 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000535}
536
Chris Lattnerd7916e92003-05-10 21:22:39 +0000537void Interpreter::visitSwitchInst(SwitchInst &I) {
538 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000539 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
540 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000541
542 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000543 BasicBlock *Dest = 0;
544 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000545 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000546 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000547 Dest = cast<BasicBlock>(I.getOperand(i+1));
548 break;
549 }
Chris Lattner09e93922003-04-22 20:34:47 +0000550
551 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000552 SwitchToNewBasicBlock(Dest, SF);
553}
554
555// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
556// This function handles the actual updating of block and instruction iterators
557// as well as execution of all of the PHI nodes in the destination block.
558//
559// This method does this because all of the PHI nodes must be executed
560// atomically, reading their inputs before any of the results are updated. Not
561// doing this can cause problems if the PHI nodes depend on other PHI nodes for
562// their inputs. If the input PHI node is updated before it is read, incorrect
563// results can happen. Thus we use a two phase approach.
564//
565void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
566 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
567 SF.CurBB = Dest; // Update CurBB to branch destination
568 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
569
570 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
571
572 // Loop over all of the PHI nodes in the current block, reading their inputs.
573 std::vector<GenericValue> ResultValues;
574
575 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000576 if (Trace) CW << "Run:" << PN;
577
Chris Lattner77113b62003-05-10 20:21:16 +0000578 // Search for the value corresponding to this previous bb...
579 int i = PN->getBasicBlockIndex(PrevBB);
580 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
581 Value *IncomingValue = PN->getIncomingValue(i);
582
583 // Save the incoming value for this PHI node...
584 ResultValues.push_back(getOperandValue(IncomingValue, SF));
585 }
586
587 // Now loop over all of the PHI nodes setting their values...
588 SF.CurInst = SF.CurBB->begin();
589 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
590 ++SF.CurInst, ++i)
591 SetValue(PN, ResultValues[i], SF);
Chris Lattner09e93922003-04-22 20:34:47 +0000592}
593
594
Chris Lattner92101ac2001-08-23 17:05:04 +0000595//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000596// Memory Instruction Implementations
597//===----------------------------------------------------------------------===//
598
Chris Lattnerd7916e92003-05-10 21:22:39 +0000599void Interpreter::visitAllocationInst(AllocationInst &I) {
600 ExecutionContext &SF = ECStack.back();
601
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000602 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000603
Chris Lattnercc82cc12002-04-28 21:57:33 +0000604 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000605 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000606
607 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000608 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000609 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
610
Chris Lattnerfe11a972002-12-23 23:59:41 +0000611 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000612 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000613 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000614
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000615 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000616 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000617}
618
Chris Lattnerd7916e92003-05-10 21:22:39 +0000619void Interpreter::visitFreeInst(FreeInst &I) {
620 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000621 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
622 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000623 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000624 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000625}
626
Chris Lattner95c3af52001-10-29 19:32:19 +0000627
Chris Lattnera34c5682002-08-27 22:33:45 +0000628// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000629//
Chris Lattnerfe11a972002-12-23 23:59:41 +0000630GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
631 User::op_iterator E,
632 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000633 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000634 "Cannot getElementOffset of a nonpointer type!");
635
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000636 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000637 const Type *Ty = Ptr->getType();
638
639 for (; I != E; ++I) {
Chris Lattner782b9392001-11-26 18:18:18 +0000640 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
641 const StructLayout *SLO = TD.getStructLayout(STy);
642
Misha Brukmand5d96b92003-10-10 17:42:19 +0000643 // Indices must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000644 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000645 assert(CPU->getType() == Type::UByteTy);
646 unsigned Index = CPU->getValue();
647
Chris Lattner782b9392001-11-26 18:18:18 +0000648 Total += SLO->MemberOffsets[Index];
649 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000650 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000651
Chris Lattner006a4a52003-02-25 21:14:59 +0000652 // Get the index number for the array... which must be long type...
Chris Lattner0374b8d2002-09-11 01:21:35 +0000653 assert((*I)->getType() == Type::LongTy);
Chris Lattnere8b3e9b2002-09-13 23:30:42 +0000654 unsigned Idx = getOperandValue(*I, SF).LongVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000655 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000656 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattner02868352003-04-22 21:22:33 +0000657 std::cerr << "Out of range memory access to element #" << Idx
658 << " of a " << AT->getNumElements() << " element array."
659 << " Subscript #" << *I << "\n";
Brian Gaeke235b2002003-10-10 17:03:22 +0000660 abort();
Chris Lattnerf23eb852001-12-14 16:49:29 +0000661 }
Chris Lattner782b9392001-11-26 18:18:18 +0000662
Chris Lattnerf23eb852001-12-14 16:49:29 +0000663 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000664 unsigned Size = TD.getTypeSize(Ty);
665 Total += Size*Idx;
666 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000667 }
668
Chris Lattnera34c5682002-08-27 22:33:45 +0000669 GenericValue Result;
670 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
671 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000672}
673
Chris Lattnerd7916e92003-05-10 21:22:39 +0000674void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
675 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000676 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattnera34c5682002-08-27 22:33:45 +0000677 I.idx_begin(), I.idx_end(), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000678}
679
Chris Lattnerd7916e92003-05-10 21:22:39 +0000680void Interpreter::visitLoadInst(LoadInst &I) {
681 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000682 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000683 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000684 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000685 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000686}
687
Chris Lattnerd7916e92003-05-10 21:22:39 +0000688void Interpreter::visitStoreInst(StoreInst &I) {
689 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000690 GenericValue Val = getOperandValue(I.getOperand(0), SF);
691 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000692 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000693 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000694}
695
Chris Lattner86660982001-08-27 05:16:50 +0000696
Chris Lattnerab2dea52002-11-07 19:29:31 +0000697
Chris Lattner86660982001-08-27 05:16:50 +0000698//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000699// Miscellaneous Instruction Implementations
700//===----------------------------------------------------------------------===//
701
Chris Lattnerd7916e92003-05-10 21:22:39 +0000702void Interpreter::visitCallInst(CallInst &I) {
703 ExecutionContext &SF = ECStack.back();
704 SF.Caller = &I;
Chris Lattner02868352003-04-22 21:22:33 +0000705 std::vector<GenericValue> ArgVals;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000706 ArgVals.reserve(I.getNumOperands()-1);
Chris Lattner93780132003-01-13 00:58:52 +0000707 for (unsigned i = 1; i < I.getNumOperands(); ++i) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000708 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
Chris Lattner93780132003-01-13 00:58:52 +0000709 // Promote all integral types whose size is < sizeof(int) into ints. We do
710 // this by zero or sign extending the value as appropriate according to the
711 // source type.
712 if (I.getOperand(i)->getType()->isIntegral() &&
713 I.getOperand(i)->getType()->getPrimitiveSize() < 4) {
714 const Type *Ty = I.getOperand(i)->getType();
715 if (Ty == Type::ShortTy)
716 ArgVals.back().IntVal = ArgVals.back().ShortVal;
717 else if (Ty == Type::UShortTy)
718 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
719 else if (Ty == Type::SByteTy)
720 ArgVals.back().IntVal = ArgVals.back().SByteVal;
721 else if (Ty == Type::UByteTy)
722 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
723 else if (Ty == Type::BoolTy)
724 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
725 else
726 assert(0 && "Unknown type!");
727 }
728 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000729
Chris Lattner070cf5e2001-11-07 20:12:30 +0000730 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000731 // and treat it as a function pointer.
Chris Lattnerd7916e92003-05-10 21:22:39 +0000732 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000733 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000734}
735
Chris Lattner86660982001-08-27 05:16:50 +0000736#define IMPLEMENT_SHIFT(OP, TY) \
737 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
738
Chris Lattnerd7916e92003-05-10 21:22:39 +0000739void Interpreter::visitShl(ShiftInst &I) {
740 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000741 const Type *Ty = I.getOperand(0)->getType();
742 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
743 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000744 GenericValue Dest;
745
746 switch (Ty->getPrimitiveID()) {
747 IMPLEMENT_SHIFT(<<, UByte);
748 IMPLEMENT_SHIFT(<<, SByte);
749 IMPLEMENT_SHIFT(<<, UShort);
750 IMPLEMENT_SHIFT(<<, Short);
751 IMPLEMENT_SHIFT(<<, UInt);
752 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000753 IMPLEMENT_SHIFT(<<, ULong);
754 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000755 default:
Chris Lattner02868352003-04-22 21:22:33 +0000756 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000757 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000758 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000759}
760
Chris Lattnerd7916e92003-05-10 21:22:39 +0000761void Interpreter::visitShr(ShiftInst &I) {
762 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000763 const Type *Ty = I.getOperand(0)->getType();
764 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
765 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000766 GenericValue Dest;
767
768 switch (Ty->getPrimitiveID()) {
769 IMPLEMENT_SHIFT(>>, UByte);
770 IMPLEMENT_SHIFT(>>, SByte);
771 IMPLEMENT_SHIFT(>>, UShort);
772 IMPLEMENT_SHIFT(>>, Short);
773 IMPLEMENT_SHIFT(>>, UInt);
774 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000775 IMPLEMENT_SHIFT(>>, ULong);
776 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000777 default:
Chris Lattner02868352003-04-22 21:22:33 +0000778 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
779 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000780 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000781 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000782}
783
784#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000785 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000786
787#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
788 case Type::DESTTY##TyID: \
789 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000790 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000791 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
792 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
793 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
794 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
795 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000796 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
797 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000798 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
799 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000800
801#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
802 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
803 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
804
805#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner02868352003-04-22 21:22:33 +0000806 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
807 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000808 } \
809 break
810
811#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
812 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
813 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000814 IMPLEMENT_CAST_CASE_END()
815
Brian Gaeke29794cb2003-09-05 18:55:03 +0000816GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
817 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000818 const Type *SrcTy = SrcVal->getType();
819 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000820
821 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000822 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
823 IMPLEMENT_CAST_CASE(SByte , ( signed char));
824 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000825 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000826 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
827 IMPLEMENT_CAST_CASE(Int , ( signed int ));
828 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
829 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000830 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000831 IMPLEMENT_CAST_CASE(Float , (float));
832 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +0000833 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +0000834 default:
Chris Lattner02868352003-04-22 21:22:33 +0000835 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +0000836 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000837 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000838
839 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000840}
Chris Lattner92101ac2001-08-23 17:05:04 +0000841
842
Chris Lattnerd7916e92003-05-10 21:22:39 +0000843void Interpreter::visitCastInst(CastInst &I) {
844 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +0000845 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
846}
Chris Lattner92101ac2001-08-23 17:05:04 +0000847
Chris Lattnerd7916e92003-05-10 21:22:39 +0000848void Interpreter::visitVarArgInst(VarArgInst &I) {
849 ExecutionContext &SF = ECStack.back();
850
Chris Lattner374344c2003-05-08 16:52:43 +0000851 // Get the pointer to the valist element. LLI treats the valist in memory as
852 // an integer.
853 GenericValue VAListPtr = getOperandValue(I.getOperand(0), SF);
854
855 // Load the pointer
856 GenericValue VAList =
857 TheEE->LoadValueFromMemory((GenericValue *)GVTOP(VAListPtr), Type::UIntTy);
858
859 unsigned Argument = VAList.IntVal++;
860
861 // Update the valist to point to the next argument...
862 TheEE->StoreValueToMemory(VAList, (GenericValue *)GVTOP(VAListPtr),
863 Type::UIntTy);
864
865 // Set the value...
866 assert(Argument < SF.VarArgs.size() &&
867 "Accessing past the last vararg argument!");
868 SetValue(&I, SF.VarArgs[Argument], SF);
869}
Chris Lattner92101ac2001-08-23 17:05:04 +0000870
871//===----------------------------------------------------------------------===//
872// Dispatch and Execution Code
873//===----------------------------------------------------------------------===//
874
Chris Lattner63bd6132003-09-17 17:26:22 +0000875FunctionInfo::FunctionInfo(Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000876 // Assign slot numbers to the function arguments...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000877 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
878 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
Chris Lattner92101ac2001-08-23 17:05:04 +0000879
880 // Iterate over all of the instructions...
881 unsigned InstNum = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000882 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
883 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
884 // For each instruction... Add Annote
885 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
Chris Lattner92101ac2001-08-23 17:05:04 +0000886}
887
Chris Lattnerda82ed52003-05-08 16:18:31 +0000888unsigned FunctionInfo::getValueSlot(const Value *V) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000889 unsigned Plane = V->getType()->getUniqueID();
890 if (Plane >= NumPlaneElements.size())
891 NumPlaneElements.resize(Plane+1, 0);
892 return NumPlaneElements[Plane]++;
893}
894
895
Chris Lattner92101ac2001-08-23 17:05:04 +0000896//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000897// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +0000898//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000899void Interpreter::callFunction(Function *F,
900 const std::vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000901 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
902 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
903 "Incorrect number of arguments passed into function call!");
Chris Lattnercdf51782003-05-08 16:06:52 +0000904 if (F->isExternal()) {
Chris Lattnerda82ed52003-05-08 16:18:31 +0000905 GenericValue Result = callExternalFunction(F, ArgVals);
Chris Lattnercdf51782003-05-08 16:06:52 +0000906 const Type *RetTy = F->getReturnType();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000907
908 // Copy the result back into the result variable if we are not returning
909 // void.
910 if (RetTy != Type::VoidTy) {
911 if (!ECStack.empty() && ECStack.back().Caller) {
912 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000913 SetValue(SF.Caller, Result, SF);
914
915 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000916 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000917 // print it.
Chris Lattnercdf51782003-05-08 16:06:52 +0000918 CW << "Function " << F->getType() << " \"" << F->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000919 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000920 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000921 std::cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000922
923 if (RetTy->isIntegral())
Chris Lattner0c4e8862002-09-03 01:08:28 +0000924 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattnerbb76f022001-10-30 20:27:31 +0000925 }
926 }
927
Chris Lattner92101ac2001-08-23 17:05:04 +0000928 return;
929 }
930
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000931 // Process the function, assigning instruction numbers to the instructions in
932 // the function. Also calculate the number of values for each type slot
933 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +0000934 //
Chris Lattner63bd6132003-09-17 17:26:22 +0000935 FunctionInfo *&FuncInfo = FunctionInfoMap[F];
936 if (!FuncInfo) FuncInfo = new FunctionInfo(F);
Chris Lattner86660982001-08-27 05:16:50 +0000937
Chris Lattner63bd6132003-09-17 17:26:22 +0000938 // Make a new stack frame... and fill it in.
939 ECStack.push_back(ExecutionContext());
940 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000941 StackFrame.CurFunction = F;
Chris Lattnercdf51782003-05-08 16:06:52 +0000942 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +0000943 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000944 StackFrame.FuncInfo = FuncInfo;
Chris Lattner92101ac2001-08-23 17:05:04 +0000945
946 // Initialize the values to nothing...
Chris Lattnerda82ed52003-05-08 16:18:31 +0000947 StackFrame.Values.resize(FuncInfo->NumPlaneElements.size());
948 for (unsigned i = 0; i < FuncInfo->NumPlaneElements.size(); ++i) {
949 StackFrame.Values[i].resize(FuncInfo->NumPlaneElements[i]);
Chris Lattner92101ac2001-08-23 17:05:04 +0000950
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000951 // Taint the initial values of stuff
952 memset(&StackFrame.Values[i][0], 42,
Chris Lattnerda82ed52003-05-08 16:18:31 +0000953 FuncInfo->NumPlaneElements[i]*sizeof(GenericValue));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000954 }
955
Chris Lattner92101ac2001-08-23 17:05:04 +0000956
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000957 // Run through the function arguments and initialize their values...
Chris Lattnercdf51782003-05-08 16:06:52 +0000958 assert((ArgVals.size() == F->asize() ||
959 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000960 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +0000961
962 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +0000963 unsigned i = 0;
Chris Lattnercdf51782003-05-08 16:06:52 +0000964 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000965 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +0000966
967 // Handle varargs arguments...
968 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +0000969}
970
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000971// executeInstruction - Interpret a single instruction & increment the "PC".
Chris Lattner92101ac2001-08-23 17:05:04 +0000972//
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000973void Interpreter::executeInstruction() {
Chris Lattner92101ac2001-08-23 17:05:04 +0000974 assert(!ECStack.empty() && "No program running, cannot execute inst!");
975
976 ExecutionContext &SF = ECStack.back(); // Current stack frame
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000977 Instruction &I = *SF.CurInst++; // Increment before execute
Chris Lattner92101ac2001-08-23 17:05:04 +0000978
Chris Lattnerd7916e92003-05-10 21:22:39 +0000979 if (Trace) CW << "Run:" << I;
Chris Lattner5af0c482001-11-07 04:23:00 +0000980
Chris Lattnerbbdabce2002-12-08 05:51:08 +0000981 // Track the number of dynamic instructions executed.
982 ++NumDynamicInsts;
983
Chris Lattnerd7916e92003-05-10 21:22:39 +0000984 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +0000985
986 // Reset the current frame location to the top of stack
987 CurFrame = ECStack.size()-1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000988}
989
Chris Lattner92101ac2001-08-23 17:05:04 +0000990void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000991 while (!ECStack.empty()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000992 // Run an instruction...
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000993 executeInstruction();
Chris Lattner92101ac2001-08-23 17:05:04 +0000994 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000995}
996
997void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000998 switch (Ty->getPrimitiveID()) {
Chris Lattner02868352003-04-22 21:22:33 +0000999 case Type::BoolTyID: std::cout << (V.BoolVal?"true":"false"); break;
Chris Lattner65629d52002-08-13 20:45:11 +00001000 case Type::SByteTyID:
Chris Lattner02868352003-04-22 21:22:33 +00001001 std::cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
Chris Lattner65629d52002-08-13 20:45:11 +00001002 case Type::UByteTyID:
Chris Lattner02868352003-04-22 21:22:33 +00001003 std::cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
1004 case Type::ShortTyID: std::cout << V.ShortVal; break;
1005 case Type::UShortTyID: std::cout << V.UShortVal; break;
1006 case Type::IntTyID: std::cout << V.IntVal; break;
1007 case Type::UIntTyID: std::cout << V.UIntVal; break;
1008 case Type::LongTyID: std::cout << (long)V.LongVal; break;
1009 case Type::ULongTyID: std::cout << (unsigned long)V.ULongVal; break;
1010 case Type::FloatTyID: std::cout << V.FloatVal; break;
1011 case Type::DoubleTyID: std::cout << V.DoubleVal; break;
1012 case Type::PointerTyID:std::cout << (void*)GVTOP(V); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001013 default:
Chris Lattner02868352003-04-22 21:22:33 +00001014 std::cout << "- Don't know how to print value of this type!";
Chris Lattner92101ac2001-08-23 17:05:04 +00001015 break;
1016 }
1017}
1018
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001019void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001020 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001021 printValue(Ty, V);
1022}