blob: 4570f1a239e4c140a9131ffb913a2fb1ce044517 [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 Lattnerda82ed52003-05-08 16:18:31 +0000123 AnnotationManager::registerAnnotationFactory(FunctionInfoAID,
124 &FunctionInfo::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000125 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000126}
127
Chris Lattner2adcd832002-05-03 19:52:30 +0000128//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000129// Binary Instruction Implementations
130//===----------------------------------------------------------------------===//
131
132#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
133 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
134
135static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000136 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000137 GenericValue Dest;
138 switch (Ty->getPrimitiveID()) {
139 IMPLEMENT_BINARY_OPERATOR(+, UByte);
140 IMPLEMENT_BINARY_OPERATOR(+, SByte);
141 IMPLEMENT_BINARY_OPERATOR(+, UShort);
142 IMPLEMENT_BINARY_OPERATOR(+, Short);
143 IMPLEMENT_BINARY_OPERATOR(+, UInt);
144 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000145 IMPLEMENT_BINARY_OPERATOR(+, ULong);
146 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000147 IMPLEMENT_BINARY_OPERATOR(+, Float);
148 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000149 default:
Chris Lattner02868352003-04-22 21:22:33 +0000150 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
151 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000152 }
153 return Dest;
154}
155
156static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000157 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000158 GenericValue Dest;
159 switch (Ty->getPrimitiveID()) {
160 IMPLEMENT_BINARY_OPERATOR(-, UByte);
161 IMPLEMENT_BINARY_OPERATOR(-, SByte);
162 IMPLEMENT_BINARY_OPERATOR(-, UShort);
163 IMPLEMENT_BINARY_OPERATOR(-, Short);
164 IMPLEMENT_BINARY_OPERATOR(-, UInt);
165 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000166 IMPLEMENT_BINARY_OPERATOR(-, ULong);
167 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000168 IMPLEMENT_BINARY_OPERATOR(-, Float);
169 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000170 default:
Chris Lattner02868352003-04-22 21:22:33 +0000171 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
172 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000173 }
174 return Dest;
175}
176
Chris Lattnerc2593162001-10-27 08:28:11 +0000177static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000178 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000179 GenericValue Dest;
180 switch (Ty->getPrimitiveID()) {
181 IMPLEMENT_BINARY_OPERATOR(*, UByte);
182 IMPLEMENT_BINARY_OPERATOR(*, SByte);
183 IMPLEMENT_BINARY_OPERATOR(*, UShort);
184 IMPLEMENT_BINARY_OPERATOR(*, Short);
185 IMPLEMENT_BINARY_OPERATOR(*, UInt);
186 IMPLEMENT_BINARY_OPERATOR(*, Int);
187 IMPLEMENT_BINARY_OPERATOR(*, ULong);
188 IMPLEMENT_BINARY_OPERATOR(*, Long);
189 IMPLEMENT_BINARY_OPERATOR(*, Float);
190 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000191 default:
Chris Lattner02868352003-04-22 21:22:33 +0000192 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
193 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000194 }
195 return Dest;
196}
197
198static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000199 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000200 GenericValue Dest;
201 switch (Ty->getPrimitiveID()) {
202 IMPLEMENT_BINARY_OPERATOR(/, UByte);
203 IMPLEMENT_BINARY_OPERATOR(/, SByte);
204 IMPLEMENT_BINARY_OPERATOR(/, UShort);
205 IMPLEMENT_BINARY_OPERATOR(/, Short);
206 IMPLEMENT_BINARY_OPERATOR(/, UInt);
207 IMPLEMENT_BINARY_OPERATOR(/, Int);
208 IMPLEMENT_BINARY_OPERATOR(/, ULong);
209 IMPLEMENT_BINARY_OPERATOR(/, Long);
210 IMPLEMENT_BINARY_OPERATOR(/, Float);
211 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000212 default:
Chris Lattner02868352003-04-22 21:22:33 +0000213 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
214 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000215 }
216 return Dest;
217}
218
219static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000220 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000221 GenericValue Dest;
222 switch (Ty->getPrimitiveID()) {
223 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 Lattnerbb76f022001-10-30 20:27:31 +0000231 case Type::FloatTyID:
232 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
233 break;
234 case Type::DoubleTyID:
235 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
236 break;
237 default:
Chris Lattner02868352003-04-22 21:22:33 +0000238 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
239 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000240 }
241 return Dest;
242}
243
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000244static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000245 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000246 GenericValue Dest;
247 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000248 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000249 IMPLEMENT_BINARY_OPERATOR(&, UByte);
250 IMPLEMENT_BINARY_OPERATOR(&, SByte);
251 IMPLEMENT_BINARY_OPERATOR(&, UShort);
252 IMPLEMENT_BINARY_OPERATOR(&, Short);
253 IMPLEMENT_BINARY_OPERATOR(&, UInt);
254 IMPLEMENT_BINARY_OPERATOR(&, Int);
255 IMPLEMENT_BINARY_OPERATOR(&, ULong);
256 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000257 default:
Chris Lattner02868352003-04-22 21:22:33 +0000258 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
259 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000260 }
261 return Dest;
262}
263
264
265static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000266 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000267 GenericValue Dest;
268 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000269 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000270 IMPLEMENT_BINARY_OPERATOR(|, UByte);
271 IMPLEMENT_BINARY_OPERATOR(|, SByte);
272 IMPLEMENT_BINARY_OPERATOR(|, UShort);
273 IMPLEMENT_BINARY_OPERATOR(|, Short);
274 IMPLEMENT_BINARY_OPERATOR(|, UInt);
275 IMPLEMENT_BINARY_OPERATOR(|, Int);
276 IMPLEMENT_BINARY_OPERATOR(|, ULong);
277 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000278 default:
Chris Lattner02868352003-04-22 21:22:33 +0000279 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
280 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000281 }
282 return Dest;
283}
284
285
286static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000287 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000288 GenericValue Dest;
289 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000290 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000291 IMPLEMENT_BINARY_OPERATOR(^, UByte);
292 IMPLEMENT_BINARY_OPERATOR(^, SByte);
293 IMPLEMENT_BINARY_OPERATOR(^, UShort);
294 IMPLEMENT_BINARY_OPERATOR(^, Short);
295 IMPLEMENT_BINARY_OPERATOR(^, UInt);
296 IMPLEMENT_BINARY_OPERATOR(^, Int);
297 IMPLEMENT_BINARY_OPERATOR(^, ULong);
298 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000299 default:
Chris Lattner02868352003-04-22 21:22:33 +0000300 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
301 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000302 }
303 return Dest;
304}
305
306
Chris Lattner92101ac2001-08-23 17:05:04 +0000307#define IMPLEMENT_SETCC(OP, TY) \
308 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
309
Chris Lattnerfd506f52003-04-23 19:55:35 +0000310// Handle pointers specially because they must be compared with only as much
311// width as the host has. We _do not_ want to be comparing 64 bit values when
312// running on a 32-bit target, otherwise the upper 32 bits might mess up
313// comparisons if they contain garbage.
314#define IMPLEMENT_POINTERSETCC(OP) \
315 case Type::PointerTyID: \
316 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
317 (void*)(intptr_t)Src2.PointerVal; break
318
Chris Lattner92101ac2001-08-23 17:05:04 +0000319static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000320 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000321 GenericValue Dest;
322 switch (Ty->getPrimitiveID()) {
323 IMPLEMENT_SETCC(==, UByte);
324 IMPLEMENT_SETCC(==, SByte);
325 IMPLEMENT_SETCC(==, UShort);
326 IMPLEMENT_SETCC(==, Short);
327 IMPLEMENT_SETCC(==, UInt);
328 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000329 IMPLEMENT_SETCC(==, ULong);
330 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000331 IMPLEMENT_SETCC(==, Float);
332 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000333 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000334 default:
Chris Lattner02868352003-04-22 21:22:33 +0000335 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
336 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000337 }
338 return Dest;
339}
340
341static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000342 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000343 GenericValue Dest;
344 switch (Ty->getPrimitiveID()) {
345 IMPLEMENT_SETCC(!=, UByte);
346 IMPLEMENT_SETCC(!=, SByte);
347 IMPLEMENT_SETCC(!=, UShort);
348 IMPLEMENT_SETCC(!=, Short);
349 IMPLEMENT_SETCC(!=, UInt);
350 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000351 IMPLEMENT_SETCC(!=, ULong);
352 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000353 IMPLEMENT_SETCC(!=, Float);
354 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000355 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000356
Chris Lattner92101ac2001-08-23 17:05:04 +0000357 default:
Chris Lattner02868352003-04-22 21:22:33 +0000358 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
359 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000360 }
361 return Dest;
362}
363
364static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000365 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000366 GenericValue Dest;
367 switch (Ty->getPrimitiveID()) {
368 IMPLEMENT_SETCC(<=, UByte);
369 IMPLEMENT_SETCC(<=, SByte);
370 IMPLEMENT_SETCC(<=, UShort);
371 IMPLEMENT_SETCC(<=, Short);
372 IMPLEMENT_SETCC(<=, UInt);
373 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000374 IMPLEMENT_SETCC(<=, ULong);
375 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000376 IMPLEMENT_SETCC(<=, Float);
377 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000378 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000379 default:
Chris Lattner02868352003-04-22 21:22:33 +0000380 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
381 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000382 }
383 return Dest;
384}
385
386static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000387 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000388 GenericValue Dest;
389 switch (Ty->getPrimitiveID()) {
390 IMPLEMENT_SETCC(>=, UByte);
391 IMPLEMENT_SETCC(>=, SByte);
392 IMPLEMENT_SETCC(>=, UShort);
393 IMPLEMENT_SETCC(>=, Short);
394 IMPLEMENT_SETCC(>=, UInt);
395 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000396 IMPLEMENT_SETCC(>=, ULong);
397 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000398 IMPLEMENT_SETCC(>=, Float);
399 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000400 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000401 default:
Chris Lattner02868352003-04-22 21:22:33 +0000402 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
403 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000404 }
405 return Dest;
406}
407
408static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000409 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000410 GenericValue Dest;
411 switch (Ty->getPrimitiveID()) {
412 IMPLEMENT_SETCC(<, UByte);
413 IMPLEMENT_SETCC(<, SByte);
414 IMPLEMENT_SETCC(<, UShort);
415 IMPLEMENT_SETCC(<, Short);
416 IMPLEMENT_SETCC(<, UInt);
417 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000418 IMPLEMENT_SETCC(<, ULong);
419 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000420 IMPLEMENT_SETCC(<, Float);
421 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000422 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000423 default:
Chris Lattner02868352003-04-22 21:22:33 +0000424 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
425 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000426 }
427 return Dest;
428}
429
430static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000431 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000432 GenericValue Dest;
433 switch (Ty->getPrimitiveID()) {
434 IMPLEMENT_SETCC(>, UByte);
435 IMPLEMENT_SETCC(>, SByte);
436 IMPLEMENT_SETCC(>, UShort);
437 IMPLEMENT_SETCC(>, Short);
438 IMPLEMENT_SETCC(>, UInt);
439 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000440 IMPLEMENT_SETCC(>, ULong);
441 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000442 IMPLEMENT_SETCC(>, Float);
443 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000444 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000445 default:
Chris Lattner02868352003-04-22 21:22:33 +0000446 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
447 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000448 }
449 return Dest;
450}
451
Chris Lattnerd7916e92003-05-10 21:22:39 +0000452void Interpreter::visitBinaryOperator(BinaryOperator &I) {
453 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000454 const Type *Ty = I.getOperand(0)->getType();
455 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
456 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000457 GenericValue R; // Result
458
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000459 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000460 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
461 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
462 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
463 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
464 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
465 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
466 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
467 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
468 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
469 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
470 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
471 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
472 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
473 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000474 default:
Chris Lattner02868352003-04-22 21:22:33 +0000475 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
476 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000477 }
478
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000479 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000480}
481
Chris Lattner92101ac2001-08-23 17:05:04 +0000482//===----------------------------------------------------------------------===//
483// Terminator Instruction Implementations
484//===----------------------------------------------------------------------===//
485
Chris Lattnere43db882001-10-27 04:15:57 +0000486void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000487 if (!QuietMode) {
Chris Lattner02868352003-04-22 21:22:33 +0000488 std::cout << "Program returned ";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000489 print(Type::IntTy, GV);
Chris Lattner02868352003-04-22 21:22:33 +0000490 std::cout << " via 'void exit(int)'\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000491 }
Chris Lattnere43db882001-10-27 04:15:57 +0000492
493 ExitCode = GV.SByteVal;
494 ECStack.clear();
495}
496
Chris Lattnerd7916e92003-05-10 21:22:39 +0000497void Interpreter::visitReturnInst(ReturnInst &I) {
498 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000499 const Type *RetTy = 0;
500 GenericValue Result;
501
502 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000503 if (I.getNumOperands()) {
504 RetTy = I.getReturnValue()->getType();
505 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000506 }
507
508 // Save previously executing meth
Chris Lattnerda82ed52003-05-08 16:18:31 +0000509 const Function *M = ECStack.back().CurFunction;
Chris Lattner92101ac2001-08-23 17:05:04 +0000510
511 // Pop the current stack frame... this invalidates SF
512 ECStack.pop_back();
513
514 if (ECStack.empty()) { // Finished main. Put result into exit code...
515 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000516 if (!QuietMode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000517 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattnerf23eb852001-12-14 16:49:29 +0000518 << "\" returned ";
519 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000520 std::cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000521 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000522
523 if (RetTy->isIntegral())
Chris Lattnerf4dca802002-05-02 19:28:45 +0000524 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000525 } else {
526 ExitCode = 0;
527 }
528 return;
529 }
530
531 // If we have a previous stack frame, and we have a previous call, fill in
532 // the return value...
533 //
534 ExecutionContext &NewSF = ECStack.back();
535 if (NewSF.Caller) {
536 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
537 SetValue(NewSF.Caller, Result, NewSF);
538
539 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000540 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000541 // This must be a function that is executing because of a user 'call'
542 // instruction.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000543 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000544 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000545 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000546 std::cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000547 }
548}
549
Chris Lattnerd7916e92003-05-10 21:22:39 +0000550void Interpreter::visitBranchInst(BranchInst &I) {
551 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000552 BasicBlock *Dest;
553
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000554 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
555 if (!I.isUnconditional()) {
556 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000557 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000558 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000559 }
Chris Lattner77113b62003-05-10 20:21:16 +0000560 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000561}
562
Chris Lattnerd7916e92003-05-10 21:22:39 +0000563void Interpreter::visitSwitchInst(SwitchInst &I) {
564 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000565 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
566 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000567
568 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000569 BasicBlock *Dest = 0;
570 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000571 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000572 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000573 Dest = cast<BasicBlock>(I.getOperand(i+1));
574 break;
575 }
Chris Lattner09e93922003-04-22 20:34:47 +0000576
577 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000578 SwitchToNewBasicBlock(Dest, SF);
579}
580
581// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
582// This function handles the actual updating of block and instruction iterators
583// as well as execution of all of the PHI nodes in the destination block.
584//
585// This method does this because all of the PHI nodes must be executed
586// atomically, reading their inputs before any of the results are updated. Not
587// doing this can cause problems if the PHI nodes depend on other PHI nodes for
588// their inputs. If the input PHI node is updated before it is read, incorrect
589// results can happen. Thus we use a two phase approach.
590//
591void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
592 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
593 SF.CurBB = Dest; // Update CurBB to branch destination
594 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
595
596 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
597
598 // Loop over all of the PHI nodes in the current block, reading their inputs.
599 std::vector<GenericValue> ResultValues;
600
601 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000602 if (Trace) CW << "Run:" << PN;
603
Chris Lattner77113b62003-05-10 20:21:16 +0000604 // Search for the value corresponding to this previous bb...
605 int i = PN->getBasicBlockIndex(PrevBB);
606 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
607 Value *IncomingValue = PN->getIncomingValue(i);
608
609 // Save the incoming value for this PHI node...
610 ResultValues.push_back(getOperandValue(IncomingValue, SF));
611 }
612
613 // Now loop over all of the PHI nodes setting their values...
614 SF.CurInst = SF.CurBB->begin();
615 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
616 ++SF.CurInst, ++i)
617 SetValue(PN, ResultValues[i], SF);
Chris Lattner09e93922003-04-22 20:34:47 +0000618}
619
620
Chris Lattner92101ac2001-08-23 17:05:04 +0000621//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000622// Memory Instruction Implementations
623//===----------------------------------------------------------------------===//
624
Chris Lattnerd7916e92003-05-10 21:22:39 +0000625void Interpreter::visitAllocationInst(AllocationInst &I) {
626 ExecutionContext &SF = ECStack.back();
627
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000628 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000629
Chris Lattnercc82cc12002-04-28 21:57:33 +0000630 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000631 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000632
633 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000634 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000635 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
636
Chris Lattnerfe11a972002-12-23 23:59:41 +0000637 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000638 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000639 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000640
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000641 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000642 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000643}
644
Chris Lattnerd7916e92003-05-10 21:22:39 +0000645void Interpreter::visitFreeInst(FreeInst &I) {
646 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000647 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
648 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000649 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000650 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000651}
652
Chris Lattner95c3af52001-10-29 19:32:19 +0000653
Chris Lattnera34c5682002-08-27 22:33:45 +0000654// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000655//
Chris Lattnerfe11a972002-12-23 23:59:41 +0000656GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
657 User::op_iterator E,
658 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000659 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000660 "Cannot getElementOffset of a nonpointer type!");
661
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000662 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000663 const Type *Ty = Ptr->getType();
664
665 for (; I != E; ++I) {
Chris Lattner782b9392001-11-26 18:18:18 +0000666 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
667 const StructLayout *SLO = TD.getStructLayout(STy);
668
669 // Indicies must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000670 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000671 assert(CPU->getType() == Type::UByteTy);
672 unsigned Index = CPU->getValue();
673
Chris Lattner782b9392001-11-26 18:18:18 +0000674 Total += SLO->MemberOffsets[Index];
675 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000676 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000677
Chris Lattner006a4a52003-02-25 21:14:59 +0000678 // Get the index number for the array... which must be long type...
Chris Lattner0374b8d2002-09-11 01:21:35 +0000679 assert((*I)->getType() == Type::LongTy);
Chris Lattnere8b3e9b2002-09-13 23:30:42 +0000680 unsigned Idx = getOperandValue(*I, SF).LongVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000681 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000682 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattner02868352003-04-22 21:22:33 +0000683 std::cerr << "Out of range memory access to element #" << Idx
684 << " of a " << AT->getNumElements() << " element array."
685 << " Subscript #" << *I << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000686 // Get outta here!!!
Chris Lattner74030252002-02-12 15:47:23 +0000687 siglongjmp(SignalRecoverBuffer, SIGTRAP);
Chris Lattnerf23eb852001-12-14 16:49:29 +0000688 }
Chris Lattner782b9392001-11-26 18:18:18 +0000689
Chris Lattnerf23eb852001-12-14 16:49:29 +0000690 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000691 unsigned Size = TD.getTypeSize(Ty);
692 Total += Size*Idx;
693 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000694 }
695
Chris Lattnera34c5682002-08-27 22:33:45 +0000696 GenericValue Result;
697 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
698 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000699}
700
Chris Lattnerd7916e92003-05-10 21:22:39 +0000701void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
702 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000703 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattnera34c5682002-08-27 22:33:45 +0000704 I.idx_begin(), I.idx_end(), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000705}
706
Chris Lattnerd7916e92003-05-10 21:22:39 +0000707void Interpreter::visitLoadInst(LoadInst &I) {
708 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000709 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000710 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000711 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000712 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000713}
714
Chris Lattnerd7916e92003-05-10 21:22:39 +0000715void Interpreter::visitStoreInst(StoreInst &I) {
716 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000717 GenericValue Val = getOperandValue(I.getOperand(0), SF);
718 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000719 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000720 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000721}
722
Chris Lattner86660982001-08-27 05:16:50 +0000723
Chris Lattnerab2dea52002-11-07 19:29:31 +0000724
Chris Lattner86660982001-08-27 05:16:50 +0000725//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000726// Miscellaneous Instruction Implementations
727//===----------------------------------------------------------------------===//
728
Chris Lattnerd7916e92003-05-10 21:22:39 +0000729void Interpreter::visitCallInst(CallInst &I) {
730 ExecutionContext &SF = ECStack.back();
731 SF.Caller = &I;
Chris Lattner02868352003-04-22 21:22:33 +0000732 std::vector<GenericValue> ArgVals;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000733 ArgVals.reserve(I.getNumOperands()-1);
Chris Lattner93780132003-01-13 00:58:52 +0000734 for (unsigned i = 1; i < I.getNumOperands(); ++i) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000735 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
Chris Lattner93780132003-01-13 00:58:52 +0000736 // Promote all integral types whose size is < sizeof(int) into ints. We do
737 // this by zero or sign extending the value as appropriate according to the
738 // source type.
739 if (I.getOperand(i)->getType()->isIntegral() &&
740 I.getOperand(i)->getType()->getPrimitiveSize() < 4) {
741 const Type *Ty = I.getOperand(i)->getType();
742 if (Ty == Type::ShortTy)
743 ArgVals.back().IntVal = ArgVals.back().ShortVal;
744 else if (Ty == Type::UShortTy)
745 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
746 else if (Ty == Type::SByteTy)
747 ArgVals.back().IntVal = ArgVals.back().SByteVal;
748 else if (Ty == Type::UByteTy)
749 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
750 else if (Ty == Type::BoolTy)
751 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
752 else
753 assert(0 && "Unknown type!");
754 }
755 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000756
Chris Lattner070cf5e2001-11-07 20:12:30 +0000757 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000758 // and treat it as a function pointer.
Chris Lattnerd7916e92003-05-10 21:22:39 +0000759 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000760 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000761}
762
Chris Lattner86660982001-08-27 05:16:50 +0000763#define IMPLEMENT_SHIFT(OP, TY) \
764 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
765
Chris Lattnerd7916e92003-05-10 21:22:39 +0000766void Interpreter::visitShl(ShiftInst &I) {
767 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000768 const Type *Ty = I.getOperand(0)->getType();
769 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
770 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000771 GenericValue Dest;
772
773 switch (Ty->getPrimitiveID()) {
774 IMPLEMENT_SHIFT(<<, UByte);
775 IMPLEMENT_SHIFT(<<, SByte);
776 IMPLEMENT_SHIFT(<<, UShort);
777 IMPLEMENT_SHIFT(<<, Short);
778 IMPLEMENT_SHIFT(<<, UInt);
779 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000780 IMPLEMENT_SHIFT(<<, ULong);
781 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000782 default:
Chris Lattner02868352003-04-22 21:22:33 +0000783 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000784 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000785 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000786}
787
Chris Lattnerd7916e92003-05-10 21:22:39 +0000788void Interpreter::visitShr(ShiftInst &I) {
789 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000790 const Type *Ty = I.getOperand(0)->getType();
791 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
792 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000793 GenericValue Dest;
794
795 switch (Ty->getPrimitiveID()) {
796 IMPLEMENT_SHIFT(>>, UByte);
797 IMPLEMENT_SHIFT(>>, SByte);
798 IMPLEMENT_SHIFT(>>, UShort);
799 IMPLEMENT_SHIFT(>>, Short);
800 IMPLEMENT_SHIFT(>>, UInt);
801 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000802 IMPLEMENT_SHIFT(>>, ULong);
803 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000804 default:
Chris Lattner02868352003-04-22 21:22:33 +0000805 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
806 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000807 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000808 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000809}
810
811#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000812 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000813
814#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
815 case Type::DESTTY##TyID: \
816 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000817 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000818 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
819 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
820 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
821 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
822 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000823 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
824 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000825 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
826 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000827
828#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
829 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
830 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
831
832#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner02868352003-04-22 21:22:33 +0000833 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
834 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000835 } \
836 break
837
838#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
839 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
840 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000841 IMPLEMENT_CAST_CASE_END()
842
Brian Gaeke29794cb2003-09-05 18:55:03 +0000843GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
844 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000845 const Type *SrcTy = SrcVal->getType();
846 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000847
848 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000849 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
850 IMPLEMENT_CAST_CASE(SByte , ( signed char));
851 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000852 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000853 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
854 IMPLEMENT_CAST_CASE(Int , ( signed int ));
855 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
856 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000857 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000858 IMPLEMENT_CAST_CASE(Float , (float));
859 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +0000860 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +0000861 default:
Chris Lattner02868352003-04-22 21:22:33 +0000862 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +0000863 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000864 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000865
866 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000867}
Chris Lattner92101ac2001-08-23 17:05:04 +0000868
869
Chris Lattnerd7916e92003-05-10 21:22:39 +0000870void Interpreter::visitCastInst(CastInst &I) {
871 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +0000872 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
873}
Chris Lattner92101ac2001-08-23 17:05:04 +0000874
Chris Lattnerd7916e92003-05-10 21:22:39 +0000875void Interpreter::visitVarArgInst(VarArgInst &I) {
876 ExecutionContext &SF = ECStack.back();
877
Chris Lattner374344c2003-05-08 16:52:43 +0000878 // Get the pointer to the valist element. LLI treats the valist in memory as
879 // an integer.
880 GenericValue VAListPtr = getOperandValue(I.getOperand(0), SF);
881
882 // Load the pointer
883 GenericValue VAList =
884 TheEE->LoadValueFromMemory((GenericValue *)GVTOP(VAListPtr), Type::UIntTy);
885
886 unsigned Argument = VAList.IntVal++;
887
888 // Update the valist to point to the next argument...
889 TheEE->StoreValueToMemory(VAList, (GenericValue *)GVTOP(VAListPtr),
890 Type::UIntTy);
891
892 // Set the value...
893 assert(Argument < SF.VarArgs.size() &&
894 "Accessing past the last vararg argument!");
895 SetValue(&I, SF.VarArgs[Argument], SF);
896}
Chris Lattner92101ac2001-08-23 17:05:04 +0000897
898//===----------------------------------------------------------------------===//
899// Dispatch and Execution Code
900//===----------------------------------------------------------------------===//
901
Chris Lattnerda82ed52003-05-08 16:18:31 +0000902FunctionInfo::FunctionInfo(Function *F) : Annotation(FunctionInfoAID) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000903 // Assign slot numbers to the function arguments...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000904 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
905 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
Chris Lattner92101ac2001-08-23 17:05:04 +0000906
907 // Iterate over all of the instructions...
908 unsigned InstNum = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000909 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
910 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
911 // For each instruction... Add Annote
912 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
Chris Lattner92101ac2001-08-23 17:05:04 +0000913}
914
Chris Lattnerda82ed52003-05-08 16:18:31 +0000915unsigned FunctionInfo::getValueSlot(const Value *V) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000916 unsigned Plane = V->getType()->getUniqueID();
917 if (Plane >= NumPlaneElements.size())
918 NumPlaneElements.resize(Plane+1, 0);
919 return NumPlaneElements[Plane]++;
920}
921
922
Chris Lattner92101ac2001-08-23 17:05:04 +0000923//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000924// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +0000925//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000926void Interpreter::callFunction(Function *F,
927 const std::vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000928 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
929 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
930 "Incorrect number of arguments passed into function call!");
Chris Lattnercdf51782003-05-08 16:06:52 +0000931 if (F->isExternal()) {
Chris Lattnerda82ed52003-05-08 16:18:31 +0000932 GenericValue Result = callExternalFunction(F, ArgVals);
Chris Lattnercdf51782003-05-08 16:06:52 +0000933 const Type *RetTy = F->getReturnType();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000934
935 // Copy the result back into the result variable if we are not returning
936 // void.
937 if (RetTy != Type::VoidTy) {
938 if (!ECStack.empty() && ECStack.back().Caller) {
939 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000940 SetValue(SF.Caller, Result, SF);
941
942 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000943 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000944 // print it.
Chris Lattnercdf51782003-05-08 16:06:52 +0000945 CW << "Function " << F->getType() << " \"" << F->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000946 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000947 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000948 std::cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000949
950 if (RetTy->isIntegral())
Chris Lattner0c4e8862002-09-03 01:08:28 +0000951 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattnerbb76f022001-10-30 20:27:31 +0000952 }
953 }
954
Chris Lattner92101ac2001-08-23 17:05:04 +0000955 return;
956 }
957
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000958 // Process the function, assigning instruction numbers to the instructions in
959 // the function. Also calculate the number of values for each type slot
960 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +0000961 //
Chris Lattnerda82ed52003-05-08 16:18:31 +0000962 FunctionInfo *FuncInfo =
963 (FunctionInfo*)F->getOrCreateAnnotation(FunctionInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +0000964 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +0000965
Chris Lattner92101ac2001-08-23 17:05:04 +0000966 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
Chris Lattnerda82ed52003-05-08 16:18:31 +0000967 StackFrame.CurFunction = F;
Chris Lattnercdf51782003-05-08 16:06:52 +0000968 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +0000969 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000970 StackFrame.FuncInfo = FuncInfo;
Chris Lattner92101ac2001-08-23 17:05:04 +0000971
972 // Initialize the values to nothing...
Chris Lattnerda82ed52003-05-08 16:18:31 +0000973 StackFrame.Values.resize(FuncInfo->NumPlaneElements.size());
974 for (unsigned i = 0; i < FuncInfo->NumPlaneElements.size(); ++i) {
975 StackFrame.Values[i].resize(FuncInfo->NumPlaneElements[i]);
Chris Lattner92101ac2001-08-23 17:05:04 +0000976
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000977 // Taint the initial values of stuff
978 memset(&StackFrame.Values[i][0], 42,
Chris Lattnerda82ed52003-05-08 16:18:31 +0000979 FuncInfo->NumPlaneElements[i]*sizeof(GenericValue));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000980 }
981
Chris Lattner92101ac2001-08-23 17:05:04 +0000982
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000983 // Run through the function arguments and initialize their values...
Chris Lattnercdf51782003-05-08 16:06:52 +0000984 assert((ArgVals.size() == F->asize() ||
985 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000986 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +0000987
988 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +0000989 unsigned i = 0;
Chris Lattnercdf51782003-05-08 16:06:52 +0000990 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000991 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +0000992
993 // Handle varargs arguments...
994 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +0000995}
996
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000997// executeInstruction - Interpret a single instruction & increment the "PC".
Chris Lattner92101ac2001-08-23 17:05:04 +0000998//
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000999void Interpreter::executeInstruction() {
Chris Lattner92101ac2001-08-23 17:05:04 +00001000 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1001
1002 ExecutionContext &SF = ECStack.back(); // Current stack frame
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001003 Instruction &I = *SF.CurInst++; // Increment before execute
Chris Lattner92101ac2001-08-23 17:05:04 +00001004
Chris Lattnerd7916e92003-05-10 21:22:39 +00001005 if (Trace) CW << "Run:" << I;
Chris Lattner5af0c482001-11-07 04:23:00 +00001006
Chris Lattnerbbdabce2002-12-08 05:51:08 +00001007 // Track the number of dynamic instructions executed.
1008 ++NumDynamicInsts;
1009
Chris Lattner5af0c482001-11-07 04:23:00 +00001010 // Set a sigsetjmp buffer so that we can recover if an error happens during
1011 // instruction execution...
1012 //
1013 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
Brian Gaekef58815e2003-09-04 22:21:24 +00001014 std::cout << "EXCEPTION OCCURRED [" << strsignal(SigNo) << "]\n";
1015 exit(1);
Chris Lattner5af0c482001-11-07 04:23:00 +00001016 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001017
Chris Lattner461f02f2001-11-07 05:31:27 +00001018 InInstruction = true;
Chris Lattnerd7916e92003-05-10 21:22:39 +00001019 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner461f02f2001-11-07 05:31:27 +00001020 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001021
1022 // Reset the current frame location to the top of stack
1023 CurFrame = ECStack.size()-1;
Chris Lattner92101ac2001-08-23 17:05:04 +00001024}
1025
Chris Lattner92101ac2001-08-23 17:05:04 +00001026void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001027 while (!ECStack.empty()) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001028 // Run an instruction...
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001029 executeInstruction();
Chris Lattner92101ac2001-08-23 17:05:04 +00001030 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001031}
1032
1033void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001034 switch (Ty->getPrimitiveID()) {
Chris Lattner02868352003-04-22 21:22:33 +00001035 case Type::BoolTyID: std::cout << (V.BoolVal?"true":"false"); break;
Chris Lattner65629d52002-08-13 20:45:11 +00001036 case Type::SByteTyID:
Chris Lattner02868352003-04-22 21:22:33 +00001037 std::cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
Chris Lattner65629d52002-08-13 20:45:11 +00001038 case Type::UByteTyID:
Chris Lattner02868352003-04-22 21:22:33 +00001039 std::cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
1040 case Type::ShortTyID: std::cout << V.ShortVal; break;
1041 case Type::UShortTyID: std::cout << V.UShortVal; break;
1042 case Type::IntTyID: std::cout << V.IntVal; break;
1043 case Type::UIntTyID: std::cout << V.UIntVal; break;
1044 case Type::LongTyID: std::cout << (long)V.LongVal; break;
1045 case Type::ULongTyID: std::cout << (unsigned long)V.ULongVal; break;
1046 case Type::FloatTyID: std::cout << V.FloatVal; break;
1047 case Type::DoubleTyID: std::cout << V.DoubleVal; break;
1048 case Type::PointerTyID:std::cout << (void*)GVTOP(V); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001049 default:
Chris Lattner02868352003-04-22 21:22:33 +00001050 std::cout << "- Don't know how to print value of this type!";
Chris Lattner92101ac2001-08-23 17:05:04 +00001051 break;
1052 }
1053}
1054
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001055void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001056 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001057 printValue(Ty, V);
1058}