blob: d3818063d00017ded4a190c4ed4d0aa3e7fe44d3 [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
2//
3// This file contains the actual instruction interpreter.
4//
5//===----------------------------------------------------------------------===//
6
7#include "Interpreter.h"
8#include "ExecutionAnnotations.h"
Chris Lattnerd5bc41a2003-04-25 04:21:19 +00009#include "llvm/Module.h"
10#include "llvm/Instructions.h"
Chris Lattnere2cbbce2002-04-29 18:56:45 +000011#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000012#include "llvm/Constants.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000013#include "llvm/Assembly/Writer.h"
Chris Lattnerf23eb852001-12-14 16:49:29 +000014#include "Support/CommandLine.h"
Chris Lattnerbbdabce2002-12-08 05:51:08 +000015#include "Support/Statistic.h"
Chris Lattnerbb76f022001-10-30 20:27:31 +000016#include <math.h> // For fmod
Chris Lattner5af0c482001-11-07 04:23:00 +000017#include <signal.h>
18#include <setjmp.h>
Chris Lattner2e42d3a2001-10-15 05:51:48 +000019
Chris Lattnerfe11a972002-12-23 23:59:41 +000020Interpreter *TheEE = 0;
21
Chris Lattnerbbdabce2002-12-08 05:51:08 +000022namespace {
23 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
Chris Lattner138b0cd2002-12-08 06:01:34 +000024
25 cl::opt<bool>
Chris Lattnerfe11a972002-12-23 23:59:41 +000026 QuietMode("quiet", cl::desc("Do not emit any non-program output"),
27 cl::init(true));
Chris Lattner138b0cd2002-12-08 06:01:34 +000028
29 cl::alias
30 QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode));
31
32 cl::opt<bool>
33 ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks"));
Chris Lattnerbbdabce2002-12-08 05:51:08 +000034}
35
Chris Lattner2e42d3a2001-10-15 05:51:48 +000036// Create a TargetData structure to handle memory addressing and size/alignment
37// computations
38//
Chris Lattnerea38c0e2001-11-07 19:46:27 +000039CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000040
Chris Lattner5af0c482001-11-07 04:23:00 +000041sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000042static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000043
44extern "C" {
45static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000046 if (InInstruction)
47 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000048}
49}
50
51static void initializeSignalHandlers() {
52 struct sigaction Action;
53 Action.sa_handler = SigHandler;
54 Action.sa_flags = SA_SIGINFO;
55 sigemptyset(&Action.sa_mask);
56 sigaction(SIGSEGV, &Action, 0);
57 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000058 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000059 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000060}
61
Chris Lattner2e42d3a2001-10-15 05:51:48 +000062
63//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000064// Value Manipulation code
65//===----------------------------------------------------------------------===//
66
67static unsigned getOperandSlot(Value *V) {
68 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
69 assert(SN && "Operand does not have a slot number annotation!");
70 return SN->SlotNum;
71}
72
Chris Lattnera34c5682002-08-27 22:33:45 +000073// Operations used by constant expr implementations...
74static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
75 ExecutionContext &SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000076static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000077 const Type *Ty);
Chris Lattnera34c5682002-08-27 22:33:45 +000078
Chris Lattnerfddc7552002-10-15 20:34:05 +000079
Brian Gaeke29794cb2003-09-05 18:55:03 +000080GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +000081 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
82 switch (CE->getOpcode()) {
83 case Instruction::Cast:
84 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
85 case Instruction::GetElementPtr:
Chris Lattnerfe11a972002-12-23 23:59:41 +000086 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
87 CE->op_end(), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000088 case Instruction::Add:
89 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
90 getOperandValue(CE->getOperand(1), SF),
Chris Lattnerb945e4d2003-04-22 20:37:39 +000091 CE->getType());
Chris Lattnera34c5682002-08-27 22:33:45 +000092 default:
Chris Lattner02868352003-04-22 21:22:33 +000093 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
Chris Lattnera34c5682002-08-27 22:33:45 +000094 abort();
Chris Lattner04e2ad72003-04-21 22:43:32 +000095 return GenericValue();
Chris Lattnera34c5682002-08-27 22:33:45 +000096 }
97 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000098 return TheEE->getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +000099 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000100 return PTOGV(TheEE->getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000101 } else {
102 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000103 unsigned OpSlot = getOperandSlot(V);
104 assert(TyP < SF.Values.size() &&
105 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000106 return SF.Values[TyP][getOperandSlot(V)];
107 }
108}
109
Chris Lattner39bb5b42001-10-15 13:25:40 +0000110static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
111 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
112
Chris Lattner02868352003-04-22 21:22:33 +0000113 //std::cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)]<< "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000114 SF.Values[TyP][getOperandSlot(V)] = Val;
115}
116
Chris Lattner39bb5b42001-10-15 13:25:40 +0000117//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000118// Annotation Wrangling code
119//===----------------------------------------------------------------------===//
120
121void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000122 TheEE = this;
Chris Lattner5af0c482001-11-07 04:23:00 +0000123 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000124}
125
Chris Lattner2adcd832002-05-03 19:52:30 +0000126//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000127// Binary Instruction Implementations
128//===----------------------------------------------------------------------===//
129
130#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
131 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
132
133static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000134 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000135 GenericValue Dest;
136 switch (Ty->getPrimitiveID()) {
137 IMPLEMENT_BINARY_OPERATOR(+, UByte);
138 IMPLEMENT_BINARY_OPERATOR(+, SByte);
139 IMPLEMENT_BINARY_OPERATOR(+, UShort);
140 IMPLEMENT_BINARY_OPERATOR(+, Short);
141 IMPLEMENT_BINARY_OPERATOR(+, UInt);
142 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000143 IMPLEMENT_BINARY_OPERATOR(+, ULong);
144 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000145 IMPLEMENT_BINARY_OPERATOR(+, Float);
146 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000147 default:
Chris Lattner02868352003-04-22 21:22:33 +0000148 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
149 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000150 }
151 return Dest;
152}
153
154static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000155 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000156 GenericValue Dest;
157 switch (Ty->getPrimitiveID()) {
158 IMPLEMENT_BINARY_OPERATOR(-, UByte);
159 IMPLEMENT_BINARY_OPERATOR(-, SByte);
160 IMPLEMENT_BINARY_OPERATOR(-, UShort);
161 IMPLEMENT_BINARY_OPERATOR(-, Short);
162 IMPLEMENT_BINARY_OPERATOR(-, UInt);
163 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000164 IMPLEMENT_BINARY_OPERATOR(-, ULong);
165 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000166 IMPLEMENT_BINARY_OPERATOR(-, Float);
167 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000168 default:
Chris Lattner02868352003-04-22 21:22:33 +0000169 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
170 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000171 }
172 return Dest;
173}
174
Chris Lattnerc2593162001-10-27 08:28:11 +0000175static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000176 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000177 GenericValue Dest;
178 switch (Ty->getPrimitiveID()) {
179 IMPLEMENT_BINARY_OPERATOR(*, UByte);
180 IMPLEMENT_BINARY_OPERATOR(*, SByte);
181 IMPLEMENT_BINARY_OPERATOR(*, UShort);
182 IMPLEMENT_BINARY_OPERATOR(*, Short);
183 IMPLEMENT_BINARY_OPERATOR(*, UInt);
184 IMPLEMENT_BINARY_OPERATOR(*, Int);
185 IMPLEMENT_BINARY_OPERATOR(*, ULong);
186 IMPLEMENT_BINARY_OPERATOR(*, Long);
187 IMPLEMENT_BINARY_OPERATOR(*, Float);
188 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000189 default:
Chris Lattner02868352003-04-22 21:22:33 +0000190 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
191 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000192 }
193 return Dest;
194}
195
196static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000197 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000198 GenericValue Dest;
199 switch (Ty->getPrimitiveID()) {
200 IMPLEMENT_BINARY_OPERATOR(/, UByte);
201 IMPLEMENT_BINARY_OPERATOR(/, SByte);
202 IMPLEMENT_BINARY_OPERATOR(/, UShort);
203 IMPLEMENT_BINARY_OPERATOR(/, Short);
204 IMPLEMENT_BINARY_OPERATOR(/, UInt);
205 IMPLEMENT_BINARY_OPERATOR(/, Int);
206 IMPLEMENT_BINARY_OPERATOR(/, ULong);
207 IMPLEMENT_BINARY_OPERATOR(/, Long);
208 IMPLEMENT_BINARY_OPERATOR(/, Float);
209 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000210 default:
Chris Lattner02868352003-04-22 21:22:33 +0000211 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
212 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000213 }
214 return Dest;
215}
216
217static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000218 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000219 GenericValue Dest;
220 switch (Ty->getPrimitiveID()) {
221 IMPLEMENT_BINARY_OPERATOR(%, UByte);
222 IMPLEMENT_BINARY_OPERATOR(%, SByte);
223 IMPLEMENT_BINARY_OPERATOR(%, UShort);
224 IMPLEMENT_BINARY_OPERATOR(%, Short);
225 IMPLEMENT_BINARY_OPERATOR(%, UInt);
226 IMPLEMENT_BINARY_OPERATOR(%, Int);
227 IMPLEMENT_BINARY_OPERATOR(%, ULong);
228 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000229 case Type::FloatTyID:
230 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
231 break;
232 case Type::DoubleTyID:
233 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
234 break;
235 default:
Chris Lattner02868352003-04-22 21:22:33 +0000236 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
237 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000238 }
239 return Dest;
240}
241
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000242static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000243 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000244 GenericValue Dest;
245 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000246 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000247 IMPLEMENT_BINARY_OPERATOR(&, UByte);
248 IMPLEMENT_BINARY_OPERATOR(&, SByte);
249 IMPLEMENT_BINARY_OPERATOR(&, UShort);
250 IMPLEMENT_BINARY_OPERATOR(&, Short);
251 IMPLEMENT_BINARY_OPERATOR(&, UInt);
252 IMPLEMENT_BINARY_OPERATOR(&, Int);
253 IMPLEMENT_BINARY_OPERATOR(&, ULong);
254 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000255 default:
Chris Lattner02868352003-04-22 21:22:33 +0000256 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
257 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000258 }
259 return Dest;
260}
261
262
263static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000264 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000265 GenericValue Dest;
266 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000267 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000268 IMPLEMENT_BINARY_OPERATOR(|, UByte);
269 IMPLEMENT_BINARY_OPERATOR(|, SByte);
270 IMPLEMENT_BINARY_OPERATOR(|, UShort);
271 IMPLEMENT_BINARY_OPERATOR(|, Short);
272 IMPLEMENT_BINARY_OPERATOR(|, UInt);
273 IMPLEMENT_BINARY_OPERATOR(|, Int);
274 IMPLEMENT_BINARY_OPERATOR(|, ULong);
275 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000276 default:
Chris Lattner02868352003-04-22 21:22:33 +0000277 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
278 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000279 }
280 return Dest;
281}
282
283
284static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000285 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000286 GenericValue Dest;
287 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000288 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000289 IMPLEMENT_BINARY_OPERATOR(^, UByte);
290 IMPLEMENT_BINARY_OPERATOR(^, SByte);
291 IMPLEMENT_BINARY_OPERATOR(^, UShort);
292 IMPLEMENT_BINARY_OPERATOR(^, Short);
293 IMPLEMENT_BINARY_OPERATOR(^, UInt);
294 IMPLEMENT_BINARY_OPERATOR(^, Int);
295 IMPLEMENT_BINARY_OPERATOR(^, ULong);
296 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000297 default:
Chris Lattner02868352003-04-22 21:22:33 +0000298 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
299 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000300 }
301 return Dest;
302}
303
304
Chris Lattner92101ac2001-08-23 17:05:04 +0000305#define IMPLEMENT_SETCC(OP, TY) \
306 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
307
Chris Lattnerfd506f52003-04-23 19:55:35 +0000308// Handle pointers specially because they must be compared with only as much
309// width as the host has. We _do not_ want to be comparing 64 bit values when
310// running on a 32-bit target, otherwise the upper 32 bits might mess up
311// comparisons if they contain garbage.
312#define IMPLEMENT_POINTERSETCC(OP) \
313 case Type::PointerTyID: \
314 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
315 (void*)(intptr_t)Src2.PointerVal; break
316
Chris Lattner92101ac2001-08-23 17:05:04 +0000317static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000318 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000319 GenericValue Dest;
320 switch (Ty->getPrimitiveID()) {
321 IMPLEMENT_SETCC(==, UByte);
322 IMPLEMENT_SETCC(==, SByte);
323 IMPLEMENT_SETCC(==, UShort);
324 IMPLEMENT_SETCC(==, Short);
325 IMPLEMENT_SETCC(==, UInt);
326 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000327 IMPLEMENT_SETCC(==, ULong);
328 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000329 IMPLEMENT_SETCC(==, Float);
330 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000331 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000332 default:
Chris Lattner02868352003-04-22 21:22:33 +0000333 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
334 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000335 }
336 return Dest;
337}
338
339static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000340 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000341 GenericValue Dest;
342 switch (Ty->getPrimitiveID()) {
343 IMPLEMENT_SETCC(!=, UByte);
344 IMPLEMENT_SETCC(!=, SByte);
345 IMPLEMENT_SETCC(!=, UShort);
346 IMPLEMENT_SETCC(!=, Short);
347 IMPLEMENT_SETCC(!=, UInt);
348 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000349 IMPLEMENT_SETCC(!=, ULong);
350 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000351 IMPLEMENT_SETCC(!=, Float);
352 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000353 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000354
Chris Lattner92101ac2001-08-23 17:05:04 +0000355 default:
Chris Lattner02868352003-04-22 21:22:33 +0000356 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
357 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000358 }
359 return Dest;
360}
361
362static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000363 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000364 GenericValue Dest;
365 switch (Ty->getPrimitiveID()) {
366 IMPLEMENT_SETCC(<=, UByte);
367 IMPLEMENT_SETCC(<=, SByte);
368 IMPLEMENT_SETCC(<=, UShort);
369 IMPLEMENT_SETCC(<=, Short);
370 IMPLEMENT_SETCC(<=, UInt);
371 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000372 IMPLEMENT_SETCC(<=, ULong);
373 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000374 IMPLEMENT_SETCC(<=, Float);
375 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000376 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000377 default:
Chris Lattner02868352003-04-22 21:22:33 +0000378 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
379 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000380 }
381 return Dest;
382}
383
384static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000385 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000386 GenericValue Dest;
387 switch (Ty->getPrimitiveID()) {
388 IMPLEMENT_SETCC(>=, UByte);
389 IMPLEMENT_SETCC(>=, SByte);
390 IMPLEMENT_SETCC(>=, UShort);
391 IMPLEMENT_SETCC(>=, Short);
392 IMPLEMENT_SETCC(>=, UInt);
393 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000394 IMPLEMENT_SETCC(>=, ULong);
395 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000396 IMPLEMENT_SETCC(>=, Float);
397 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000398 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000399 default:
Chris Lattner02868352003-04-22 21:22:33 +0000400 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
401 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000402 }
403 return Dest;
404}
405
406static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000407 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000408 GenericValue Dest;
409 switch (Ty->getPrimitiveID()) {
410 IMPLEMENT_SETCC(<, UByte);
411 IMPLEMENT_SETCC(<, SByte);
412 IMPLEMENT_SETCC(<, UShort);
413 IMPLEMENT_SETCC(<, Short);
414 IMPLEMENT_SETCC(<, UInt);
415 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000416 IMPLEMENT_SETCC(<, ULong);
417 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000418 IMPLEMENT_SETCC(<, Float);
419 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000420 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000421 default:
Chris Lattner02868352003-04-22 21:22:33 +0000422 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
423 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000424 }
425 return Dest;
426}
427
428static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000429 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000430 GenericValue Dest;
431 switch (Ty->getPrimitiveID()) {
432 IMPLEMENT_SETCC(>, UByte);
433 IMPLEMENT_SETCC(>, SByte);
434 IMPLEMENT_SETCC(>, UShort);
435 IMPLEMENT_SETCC(>, Short);
436 IMPLEMENT_SETCC(>, UInt);
437 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000438 IMPLEMENT_SETCC(>, ULong);
439 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000440 IMPLEMENT_SETCC(>, Float);
441 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000442 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000443 default:
Chris Lattner02868352003-04-22 21:22:33 +0000444 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
445 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000446 }
447 return Dest;
448}
449
Chris Lattnerd7916e92003-05-10 21:22:39 +0000450void Interpreter::visitBinaryOperator(BinaryOperator &I) {
451 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000452 const Type *Ty = I.getOperand(0)->getType();
453 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
454 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000455 GenericValue R; // Result
456
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000457 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000458 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
459 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
460 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
461 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
462 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
463 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
464 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
465 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
466 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
467 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
468 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
469 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
470 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
471 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000472 default:
Chris Lattner02868352003-04-22 21:22:33 +0000473 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
474 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000475 }
476
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000477 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000478}
479
Chris Lattner92101ac2001-08-23 17:05:04 +0000480//===----------------------------------------------------------------------===//
481// Terminator Instruction Implementations
482//===----------------------------------------------------------------------===//
483
Chris Lattnere43db882001-10-27 04:15:57 +0000484void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000485 if (!QuietMode) {
Chris Lattner02868352003-04-22 21:22:33 +0000486 std::cout << "Program returned ";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000487 print(Type::IntTy, GV);
Chris Lattner02868352003-04-22 21:22:33 +0000488 std::cout << " via 'void exit(int)'\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000489 }
Chris Lattnere43db882001-10-27 04:15:57 +0000490
491 ExitCode = GV.SByteVal;
492 ECStack.clear();
493}
494
Chris Lattnerd7916e92003-05-10 21:22:39 +0000495void Interpreter::visitReturnInst(ReturnInst &I) {
496 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000497 const Type *RetTy = 0;
498 GenericValue Result;
499
500 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000501 if (I.getNumOperands()) {
502 RetTy = I.getReturnValue()->getType();
503 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000504 }
505
506 // Save previously executing meth
Chris Lattnerda82ed52003-05-08 16:18:31 +0000507 const Function *M = ECStack.back().CurFunction;
Chris Lattner92101ac2001-08-23 17:05:04 +0000508
509 // Pop the current stack frame... this invalidates SF
510 ECStack.pop_back();
511
512 if (ECStack.empty()) { // Finished main. Put result into exit code...
513 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000514 if (!QuietMode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000515 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattnerf23eb852001-12-14 16:49:29 +0000516 << "\" returned ";
517 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000518 std::cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000519 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000520
521 if (RetTy->isIntegral())
Chris Lattnerf4dca802002-05-02 19:28:45 +0000522 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000523 } else {
524 ExitCode = 0;
525 }
526 return;
527 }
528
529 // If we have a previous stack frame, and we have a previous call, fill in
530 // the return value...
531 //
532 ExecutionContext &NewSF = ECStack.back();
533 if (NewSF.Caller) {
534 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
535 SetValue(NewSF.Caller, Result, NewSF);
536
537 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000538 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000539 // This must be a function that is executing because of a user 'call'
540 // instruction.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000541 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000542 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000543 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000544 std::cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000545 }
546}
547
Chris Lattnerd7916e92003-05-10 21:22:39 +0000548void Interpreter::visitBranchInst(BranchInst &I) {
549 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000550 BasicBlock *Dest;
551
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000552 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
553 if (!I.isUnconditional()) {
554 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000555 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000556 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000557 }
Chris Lattner77113b62003-05-10 20:21:16 +0000558 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000559}
560
Chris Lattnerd7916e92003-05-10 21:22:39 +0000561void Interpreter::visitSwitchInst(SwitchInst &I) {
562 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000563 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
564 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000565
566 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000567 BasicBlock *Dest = 0;
568 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000569 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000570 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000571 Dest = cast<BasicBlock>(I.getOperand(i+1));
572 break;
573 }
Chris Lattner09e93922003-04-22 20:34:47 +0000574
575 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000576 SwitchToNewBasicBlock(Dest, SF);
577}
578
579// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
580// This function handles the actual updating of block and instruction iterators
581// as well as execution of all of the PHI nodes in the destination block.
582//
583// This method does this because all of the PHI nodes must be executed
584// atomically, reading their inputs before any of the results are updated. Not
585// doing this can cause problems if the PHI nodes depend on other PHI nodes for
586// their inputs. If the input PHI node is updated before it is read, incorrect
587// results can happen. Thus we use a two phase approach.
588//
589void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
590 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
591 SF.CurBB = Dest; // Update CurBB to branch destination
592 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
593
594 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
595
596 // Loop over all of the PHI nodes in the current block, reading their inputs.
597 std::vector<GenericValue> ResultValues;
598
599 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000600 if (Trace) CW << "Run:" << PN;
601
Chris Lattner77113b62003-05-10 20:21:16 +0000602 // Search for the value corresponding to this previous bb...
603 int i = PN->getBasicBlockIndex(PrevBB);
604 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
605 Value *IncomingValue = PN->getIncomingValue(i);
606
607 // Save the incoming value for this PHI node...
608 ResultValues.push_back(getOperandValue(IncomingValue, SF));
609 }
610
611 // Now loop over all of the PHI nodes setting their values...
612 SF.CurInst = SF.CurBB->begin();
613 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
614 ++SF.CurInst, ++i)
615 SetValue(PN, ResultValues[i], SF);
Chris Lattner09e93922003-04-22 20:34:47 +0000616}
617
618
Chris Lattner92101ac2001-08-23 17:05:04 +0000619//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000620// Memory Instruction Implementations
621//===----------------------------------------------------------------------===//
622
Chris Lattnerd7916e92003-05-10 21:22:39 +0000623void Interpreter::visitAllocationInst(AllocationInst &I) {
624 ExecutionContext &SF = ECStack.back();
625
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000626 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000627
Chris Lattnercc82cc12002-04-28 21:57:33 +0000628 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000629 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000630
631 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000632 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000633 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
634
Chris Lattnerfe11a972002-12-23 23:59:41 +0000635 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000636 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000637 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000638
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000639 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000640 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000641}
642
Chris Lattnerd7916e92003-05-10 21:22:39 +0000643void Interpreter::visitFreeInst(FreeInst &I) {
644 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000645 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
646 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000647 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000648 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000649}
650
Chris Lattner95c3af52001-10-29 19:32:19 +0000651
Chris Lattnera34c5682002-08-27 22:33:45 +0000652// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000653//
Chris Lattnerfe11a972002-12-23 23:59:41 +0000654GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
655 User::op_iterator E,
656 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000657 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000658 "Cannot getElementOffset of a nonpointer type!");
659
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000660 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000661 const Type *Ty = Ptr->getType();
662
663 for (; I != E; ++I) {
Chris Lattner782b9392001-11-26 18:18:18 +0000664 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
665 const StructLayout *SLO = TD.getStructLayout(STy);
666
667 // Indicies must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000668 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000669 assert(CPU->getType() == Type::UByteTy);
670 unsigned Index = CPU->getValue();
671
Chris Lattner782b9392001-11-26 18:18:18 +0000672 Total += SLO->MemberOffsets[Index];
673 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000674 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000675
Chris Lattner006a4a52003-02-25 21:14:59 +0000676 // Get the index number for the array... which must be long type...
Chris Lattner0374b8d2002-09-11 01:21:35 +0000677 assert((*I)->getType() == Type::LongTy);
Chris Lattnere8b3e9b2002-09-13 23:30:42 +0000678 unsigned Idx = getOperandValue(*I, SF).LongVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000679 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000680 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattner02868352003-04-22 21:22:33 +0000681 std::cerr << "Out of range memory access to element #" << Idx
682 << " of a " << AT->getNumElements() << " element array."
683 << " Subscript #" << *I << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000684 // Get outta here!!!
Chris Lattner74030252002-02-12 15:47:23 +0000685 siglongjmp(SignalRecoverBuffer, SIGTRAP);
Chris Lattnerf23eb852001-12-14 16:49:29 +0000686 }
Chris Lattner782b9392001-11-26 18:18:18 +0000687
Chris Lattnerf23eb852001-12-14 16:49:29 +0000688 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000689 unsigned Size = TD.getTypeSize(Ty);
690 Total += Size*Idx;
691 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000692 }
693
Chris Lattnera34c5682002-08-27 22:33:45 +0000694 GenericValue Result;
695 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
696 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000697}
698
Chris Lattnerd7916e92003-05-10 21:22:39 +0000699void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
700 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000701 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattnera34c5682002-08-27 22:33:45 +0000702 I.idx_begin(), I.idx_end(), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000703}
704
Chris Lattnerd7916e92003-05-10 21:22:39 +0000705void Interpreter::visitLoadInst(LoadInst &I) {
706 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000707 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000708 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000709 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000710 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000711}
712
Chris Lattnerd7916e92003-05-10 21:22:39 +0000713void Interpreter::visitStoreInst(StoreInst &I) {
714 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000715 GenericValue Val = getOperandValue(I.getOperand(0), SF);
716 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000717 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000718 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000719}
720
Chris Lattner86660982001-08-27 05:16:50 +0000721
Chris Lattnerab2dea52002-11-07 19:29:31 +0000722
Chris Lattner86660982001-08-27 05:16:50 +0000723//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000724// Miscellaneous Instruction Implementations
725//===----------------------------------------------------------------------===//
726
Chris Lattnerd7916e92003-05-10 21:22:39 +0000727void Interpreter::visitCallInst(CallInst &I) {
728 ExecutionContext &SF = ECStack.back();
729 SF.Caller = &I;
Chris Lattner02868352003-04-22 21:22:33 +0000730 std::vector<GenericValue> ArgVals;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000731 ArgVals.reserve(I.getNumOperands()-1);
Chris Lattner93780132003-01-13 00:58:52 +0000732 for (unsigned i = 1; i < I.getNumOperands(); ++i) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000733 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
Chris Lattner93780132003-01-13 00:58:52 +0000734 // Promote all integral types whose size is < sizeof(int) into ints. We do
735 // this by zero or sign extending the value as appropriate according to the
736 // source type.
737 if (I.getOperand(i)->getType()->isIntegral() &&
738 I.getOperand(i)->getType()->getPrimitiveSize() < 4) {
739 const Type *Ty = I.getOperand(i)->getType();
740 if (Ty == Type::ShortTy)
741 ArgVals.back().IntVal = ArgVals.back().ShortVal;
742 else if (Ty == Type::UShortTy)
743 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
744 else if (Ty == Type::SByteTy)
745 ArgVals.back().IntVal = ArgVals.back().SByteVal;
746 else if (Ty == Type::UByteTy)
747 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
748 else if (Ty == Type::BoolTy)
749 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
750 else
751 assert(0 && "Unknown type!");
752 }
753 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000754
Chris Lattner070cf5e2001-11-07 20:12:30 +0000755 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000756 // and treat it as a function pointer.
Chris Lattnerd7916e92003-05-10 21:22:39 +0000757 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000758 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000759}
760
Chris Lattner86660982001-08-27 05:16:50 +0000761#define IMPLEMENT_SHIFT(OP, TY) \
762 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
763
Chris Lattnerd7916e92003-05-10 21:22:39 +0000764void Interpreter::visitShl(ShiftInst &I) {
765 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000766 const Type *Ty = I.getOperand(0)->getType();
767 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
768 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000769 GenericValue Dest;
770
771 switch (Ty->getPrimitiveID()) {
772 IMPLEMENT_SHIFT(<<, UByte);
773 IMPLEMENT_SHIFT(<<, SByte);
774 IMPLEMENT_SHIFT(<<, UShort);
775 IMPLEMENT_SHIFT(<<, Short);
776 IMPLEMENT_SHIFT(<<, UInt);
777 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000778 IMPLEMENT_SHIFT(<<, ULong);
779 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000780 default:
Chris Lattner02868352003-04-22 21:22:33 +0000781 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000782 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000783 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000784}
785
Chris Lattnerd7916e92003-05-10 21:22:39 +0000786void Interpreter::visitShr(ShiftInst &I) {
787 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000788 const Type *Ty = I.getOperand(0)->getType();
789 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
790 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000791 GenericValue Dest;
792
793 switch (Ty->getPrimitiveID()) {
794 IMPLEMENT_SHIFT(>>, UByte);
795 IMPLEMENT_SHIFT(>>, SByte);
796 IMPLEMENT_SHIFT(>>, UShort);
797 IMPLEMENT_SHIFT(>>, Short);
798 IMPLEMENT_SHIFT(>>, UInt);
799 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000800 IMPLEMENT_SHIFT(>>, ULong);
801 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000802 default:
Chris Lattner02868352003-04-22 21:22:33 +0000803 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
804 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000805 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000806 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000807}
808
809#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000810 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000811
812#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
813 case Type::DESTTY##TyID: \
814 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000815 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000816 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
817 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
818 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
819 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
820 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000821 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
822 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000823 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
824 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000825
826#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
827 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
828 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
829
830#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner02868352003-04-22 21:22:33 +0000831 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
832 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000833 } \
834 break
835
836#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
837 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
838 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000839 IMPLEMENT_CAST_CASE_END()
840
Brian Gaeke29794cb2003-09-05 18:55:03 +0000841GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
842 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000843 const Type *SrcTy = SrcVal->getType();
844 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000845
846 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000847 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
848 IMPLEMENT_CAST_CASE(SByte , ( signed char));
849 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000850 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000851 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
852 IMPLEMENT_CAST_CASE(Int , ( signed int ));
853 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
854 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000855 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000856 IMPLEMENT_CAST_CASE(Float , (float));
857 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +0000858 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +0000859 default:
Chris Lattner02868352003-04-22 21:22:33 +0000860 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +0000861 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000862 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000863
864 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000865}
Chris Lattner92101ac2001-08-23 17:05:04 +0000866
867
Chris Lattnerd7916e92003-05-10 21:22:39 +0000868void Interpreter::visitCastInst(CastInst &I) {
869 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +0000870 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
871}
Chris Lattner92101ac2001-08-23 17:05:04 +0000872
Chris Lattnerd7916e92003-05-10 21:22:39 +0000873void Interpreter::visitVarArgInst(VarArgInst &I) {
874 ExecutionContext &SF = ECStack.back();
875
Chris Lattner374344c2003-05-08 16:52:43 +0000876 // Get the pointer to the valist element. LLI treats the valist in memory as
877 // an integer.
878 GenericValue VAListPtr = getOperandValue(I.getOperand(0), SF);
879
880 // Load the pointer
881 GenericValue VAList =
882 TheEE->LoadValueFromMemory((GenericValue *)GVTOP(VAListPtr), Type::UIntTy);
883
884 unsigned Argument = VAList.IntVal++;
885
886 // Update the valist to point to the next argument...
887 TheEE->StoreValueToMemory(VAList, (GenericValue *)GVTOP(VAListPtr),
888 Type::UIntTy);
889
890 // Set the value...
891 assert(Argument < SF.VarArgs.size() &&
892 "Accessing past the last vararg argument!");
893 SetValue(&I, SF.VarArgs[Argument], SF);
894}
Chris Lattner92101ac2001-08-23 17:05:04 +0000895
896//===----------------------------------------------------------------------===//
897// Dispatch and Execution Code
898//===----------------------------------------------------------------------===//
899
Chris Lattner63bd6132003-09-17 17:26:22 +0000900FunctionInfo::FunctionInfo(Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000901 // Assign slot numbers to the function arguments...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000902 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
903 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
Chris Lattner92101ac2001-08-23 17:05:04 +0000904
905 // Iterate over all of the instructions...
906 unsigned InstNum = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000907 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
908 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
909 // For each instruction... Add Annote
910 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
Chris Lattner92101ac2001-08-23 17:05:04 +0000911}
912
Chris Lattnerda82ed52003-05-08 16:18:31 +0000913unsigned FunctionInfo::getValueSlot(const Value *V) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000914 unsigned Plane = V->getType()->getUniqueID();
915 if (Plane >= NumPlaneElements.size())
916 NumPlaneElements.resize(Plane+1, 0);
917 return NumPlaneElements[Plane]++;
918}
919
920
Chris Lattner92101ac2001-08-23 17:05:04 +0000921//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000922// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +0000923//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000924void Interpreter::callFunction(Function *F,
925 const std::vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000926 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
927 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
928 "Incorrect number of arguments passed into function call!");
Chris Lattnercdf51782003-05-08 16:06:52 +0000929 if (F->isExternal()) {
Chris Lattnerda82ed52003-05-08 16:18:31 +0000930 GenericValue Result = callExternalFunction(F, ArgVals);
Chris Lattnercdf51782003-05-08 16:06:52 +0000931 const Type *RetTy = F->getReturnType();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000932
933 // Copy the result back into the result variable if we are not returning
934 // void.
935 if (RetTy != Type::VoidTy) {
936 if (!ECStack.empty() && ECStack.back().Caller) {
937 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000938 SetValue(SF.Caller, Result, SF);
939
940 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000941 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000942 // print it.
Chris Lattnercdf51782003-05-08 16:06:52 +0000943 CW << "Function " << F->getType() << " \"" << F->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000944 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000945 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000946 std::cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000947
948 if (RetTy->isIntegral())
Chris Lattner0c4e8862002-09-03 01:08:28 +0000949 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattnerbb76f022001-10-30 20:27:31 +0000950 }
951 }
952
Chris Lattner92101ac2001-08-23 17:05:04 +0000953 return;
954 }
955
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000956 // Process the function, assigning instruction numbers to the instructions in
957 // the function. Also calculate the number of values for each type slot
958 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +0000959 //
Chris Lattner63bd6132003-09-17 17:26:22 +0000960 FunctionInfo *&FuncInfo = FunctionInfoMap[F];
961 if (!FuncInfo) FuncInfo = new FunctionInfo(F);
Chris Lattner86660982001-08-27 05:16:50 +0000962
Chris Lattner63bd6132003-09-17 17:26:22 +0000963 // Make a new stack frame... and fill it in.
964 ECStack.push_back(ExecutionContext());
965 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000966 StackFrame.CurFunction = F;
Chris Lattnercdf51782003-05-08 16:06:52 +0000967 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +0000968 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000969 StackFrame.FuncInfo = FuncInfo;
Chris Lattner92101ac2001-08-23 17:05:04 +0000970
971 // Initialize the values to nothing...
Chris Lattnerda82ed52003-05-08 16:18:31 +0000972 StackFrame.Values.resize(FuncInfo->NumPlaneElements.size());
973 for (unsigned i = 0; i < FuncInfo->NumPlaneElements.size(); ++i) {
974 StackFrame.Values[i].resize(FuncInfo->NumPlaneElements[i]);
Chris Lattner92101ac2001-08-23 17:05:04 +0000975
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000976 // Taint the initial values of stuff
977 memset(&StackFrame.Values[i][0], 42,
Chris Lattnerda82ed52003-05-08 16:18:31 +0000978 FuncInfo->NumPlaneElements[i]*sizeof(GenericValue));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000979 }
980
Chris Lattner92101ac2001-08-23 17:05:04 +0000981
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000982 // Run through the function arguments and initialize their values...
Chris Lattnercdf51782003-05-08 16:06:52 +0000983 assert((ArgVals.size() == F->asize() ||
984 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000985 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +0000986
987 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +0000988 unsigned i = 0;
Chris Lattnercdf51782003-05-08 16:06:52 +0000989 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000990 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +0000991
992 // Handle varargs arguments...
993 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +0000994}
995
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000996// executeInstruction - Interpret a single instruction & increment the "PC".
Chris Lattner92101ac2001-08-23 17:05:04 +0000997//
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000998void Interpreter::executeInstruction() {
Chris Lattner92101ac2001-08-23 17:05:04 +0000999 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1000
1001 ExecutionContext &SF = ECStack.back(); // Current stack frame
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001002 Instruction &I = *SF.CurInst++; // Increment before execute
Chris Lattner92101ac2001-08-23 17:05:04 +00001003
Chris Lattnerd7916e92003-05-10 21:22:39 +00001004 if (Trace) CW << "Run:" << I;
Chris Lattner5af0c482001-11-07 04:23:00 +00001005
Chris Lattnerbbdabce2002-12-08 05:51:08 +00001006 // Track the number of dynamic instructions executed.
1007 ++NumDynamicInsts;
1008
Chris Lattner5af0c482001-11-07 04:23:00 +00001009 // Set a sigsetjmp buffer so that we can recover if an error happens during
1010 // instruction execution...
1011 //
1012 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
Brian Gaekef58815e2003-09-04 22:21:24 +00001013 std::cout << "EXCEPTION OCCURRED [" << strsignal(SigNo) << "]\n";
1014 exit(1);
Chris Lattner5af0c482001-11-07 04:23:00 +00001015 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001016
Chris Lattner461f02f2001-11-07 05:31:27 +00001017 InInstruction = true;
Chris Lattnerd7916e92003-05-10 21:22:39 +00001018 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner461f02f2001-11-07 05:31:27 +00001019 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001020
1021 // Reset the current frame location to the top of stack
1022 CurFrame = ECStack.size()-1;
Chris Lattner92101ac2001-08-23 17:05:04 +00001023}
1024
Chris Lattner92101ac2001-08-23 17:05:04 +00001025void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001026 while (!ECStack.empty()) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001027 // Run an instruction...
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001028 executeInstruction();
Chris Lattner92101ac2001-08-23 17:05:04 +00001029 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001030}
1031
1032void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001033 switch (Ty->getPrimitiveID()) {
Chris Lattner02868352003-04-22 21:22:33 +00001034 case Type::BoolTyID: std::cout << (V.BoolVal?"true":"false"); break;
Chris Lattner65629d52002-08-13 20:45:11 +00001035 case Type::SByteTyID:
Chris Lattner02868352003-04-22 21:22:33 +00001036 std::cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
Chris Lattner65629d52002-08-13 20:45:11 +00001037 case Type::UByteTyID:
Chris Lattner02868352003-04-22 21:22:33 +00001038 std::cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
1039 case Type::ShortTyID: std::cout << V.ShortVal; break;
1040 case Type::UShortTyID: std::cout << V.UShortVal; break;
1041 case Type::IntTyID: std::cout << V.IntVal; break;
1042 case Type::UIntTyID: std::cout << V.UIntVal; break;
1043 case Type::LongTyID: std::cout << (long)V.LongVal; break;
1044 case Type::ULongTyID: std::cout << (unsigned long)V.ULongVal; break;
1045 case Type::FloatTyID: std::cout << V.FloatVal; break;
1046 case Type::DoubleTyID: std::cout << V.DoubleVal; break;
1047 case Type::PointerTyID:std::cout << (void*)GVTOP(V); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001048 default:
Chris Lattner02868352003-04-22 21:22:33 +00001049 std::cout << "- Don't know how to print value of this type!";
Chris Lattner92101ac2001-08-23 17:05:04 +00001050 break;
1051 }
1052}
1053
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001054void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001055 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001056 printValue(Ty, V);
1057}