blob: f3191c3275ae8b8cf6b84a89924a5b123196a87d [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
2//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner92101ac2001-08-23 17:05:04 +000010// This file contains the actual instruction interpreter.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Interpreter.h"
15#include "ExecutionAnnotations.h"
Chris Lattnerd5bc41a2003-04-25 04:21:19 +000016#include "llvm/Module.h"
17#include "llvm/Instructions.h"
Chris Lattnere2cbbce2002-04-29 18:56:45 +000018#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000019#include "llvm/Constants.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000020#include "llvm/Assembly/Writer.h"
Chris Lattnerf23eb852001-12-14 16:49:29 +000021#include "Support/CommandLine.h"
Chris Lattnerbbdabce2002-12-08 05:51:08 +000022#include "Support/Statistic.h"
Brian Gaeke235b2002003-10-10 17:03:22 +000023#include <cmath> // For fmod
Chris Lattner2e42d3a2001-10-15 05:51:48 +000024
Chris Lattnerfe11a972002-12-23 23:59:41 +000025Interpreter *TheEE = 0;
26
Chris Lattnerbbdabce2002-12-08 05:51:08 +000027namespace {
28 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
Chris Lattner138b0cd2002-12-08 06:01:34 +000029
30 cl::opt<bool>
Chris Lattnerfe11a972002-12-23 23:59:41 +000031 QuietMode("quiet", cl::desc("Do not emit any non-program output"),
32 cl::init(true));
Chris Lattner138b0cd2002-12-08 06:01:34 +000033
34 cl::alias
35 QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode));
36
37 cl::opt<bool>
38 ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks"));
Chris Lattnerbbdabce2002-12-08 05:51:08 +000039}
40
Chris Lattner2e42d3a2001-10-15 05:51:48 +000041// Create a TargetData structure to handle memory addressing and size/alignment
42// computations
43//
Chris Lattnerea38c0e2001-11-07 19:46:27 +000044CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000045
Chris Lattner2e42d3a2001-10-15 05:51:48 +000046
47//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000048// Value Manipulation code
49//===----------------------------------------------------------------------===//
50
51static unsigned getOperandSlot(Value *V) {
52 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
53 assert(SN && "Operand does not have a slot number annotation!");
54 return SN->SlotNum;
55}
56
Chris Lattnera34c5682002-08-27 22:33:45 +000057// Operations used by constant expr implementations...
58static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
59 ExecutionContext &SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000060static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000061 const Type *Ty);
Chris Lattnera34c5682002-08-27 22:33:45 +000062
Chris Lattnerfddc7552002-10-15 20:34:05 +000063
Brian Gaeke29794cb2003-09-05 18:55:03 +000064GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +000065 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
66 switch (CE->getOpcode()) {
67 case Instruction::Cast:
68 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
69 case Instruction::GetElementPtr:
Chris Lattnerfe11a972002-12-23 23:59:41 +000070 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
71 CE->op_end(), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000072 case Instruction::Add:
73 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
74 getOperandValue(CE->getOperand(1), SF),
Chris Lattnerb945e4d2003-04-22 20:37:39 +000075 CE->getType());
Chris Lattnera34c5682002-08-27 22:33:45 +000076 default:
Chris Lattner02868352003-04-22 21:22:33 +000077 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
Chris Lattnera34c5682002-08-27 22:33:45 +000078 abort();
Chris Lattner04e2ad72003-04-21 22:43:32 +000079 return GenericValue();
Chris Lattnera34c5682002-08-27 22:33:45 +000080 }
81 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000082 return TheEE->getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +000083 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000084 return PTOGV(TheEE->getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +000085 } else {
86 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +000087 unsigned OpSlot = getOperandSlot(V);
88 assert(TyP < SF.Values.size() &&
89 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +000090 return SF.Values[TyP][getOperandSlot(V)];
91 }
92}
93
Chris Lattner39bb5b42001-10-15 13:25:40 +000094static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
95 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
96
Chris Lattner02868352003-04-22 21:22:33 +000097 //std::cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)]<< "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +000098 SF.Values[TyP][getOperandSlot(V)] = Val;
99}
100
Chris Lattner39bb5b42001-10-15 13:25:40 +0000101//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000102// Annotation Wrangling code
103//===----------------------------------------------------------------------===//
104
105void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000106 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000107}
108
Chris Lattner2adcd832002-05-03 19:52:30 +0000109//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000110// Binary Instruction Implementations
111//===----------------------------------------------------------------------===//
112
113#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
114 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
115
116static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000117 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000118 GenericValue Dest;
119 switch (Ty->getPrimitiveID()) {
120 IMPLEMENT_BINARY_OPERATOR(+, UByte);
121 IMPLEMENT_BINARY_OPERATOR(+, SByte);
122 IMPLEMENT_BINARY_OPERATOR(+, UShort);
123 IMPLEMENT_BINARY_OPERATOR(+, Short);
124 IMPLEMENT_BINARY_OPERATOR(+, UInt);
125 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000126 IMPLEMENT_BINARY_OPERATOR(+, ULong);
127 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000128 IMPLEMENT_BINARY_OPERATOR(+, Float);
129 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000130 default:
Chris Lattner02868352003-04-22 21:22:33 +0000131 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
132 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000133 }
134 return Dest;
135}
136
137static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000138 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000139 GenericValue Dest;
140 switch (Ty->getPrimitiveID()) {
141 IMPLEMENT_BINARY_OPERATOR(-, UByte);
142 IMPLEMENT_BINARY_OPERATOR(-, SByte);
143 IMPLEMENT_BINARY_OPERATOR(-, UShort);
144 IMPLEMENT_BINARY_OPERATOR(-, Short);
145 IMPLEMENT_BINARY_OPERATOR(-, UInt);
146 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000147 IMPLEMENT_BINARY_OPERATOR(-, ULong);
148 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000149 IMPLEMENT_BINARY_OPERATOR(-, Float);
150 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000151 default:
Chris Lattner02868352003-04-22 21:22:33 +0000152 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
153 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000154 }
155 return Dest;
156}
157
Chris Lattnerc2593162001-10-27 08:28:11 +0000158static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000159 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000160 GenericValue Dest;
161 switch (Ty->getPrimitiveID()) {
162 IMPLEMENT_BINARY_OPERATOR(*, UByte);
163 IMPLEMENT_BINARY_OPERATOR(*, SByte);
164 IMPLEMENT_BINARY_OPERATOR(*, UShort);
165 IMPLEMENT_BINARY_OPERATOR(*, Short);
166 IMPLEMENT_BINARY_OPERATOR(*, UInt);
167 IMPLEMENT_BINARY_OPERATOR(*, Int);
168 IMPLEMENT_BINARY_OPERATOR(*, ULong);
169 IMPLEMENT_BINARY_OPERATOR(*, Long);
170 IMPLEMENT_BINARY_OPERATOR(*, Float);
171 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000172 default:
Chris Lattner02868352003-04-22 21:22:33 +0000173 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
174 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000175 }
176 return Dest;
177}
178
179static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000180 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000181 GenericValue Dest;
182 switch (Ty->getPrimitiveID()) {
183 IMPLEMENT_BINARY_OPERATOR(/, UByte);
184 IMPLEMENT_BINARY_OPERATOR(/, SByte);
185 IMPLEMENT_BINARY_OPERATOR(/, UShort);
186 IMPLEMENT_BINARY_OPERATOR(/, Short);
187 IMPLEMENT_BINARY_OPERATOR(/, UInt);
188 IMPLEMENT_BINARY_OPERATOR(/, Int);
189 IMPLEMENT_BINARY_OPERATOR(/, ULong);
190 IMPLEMENT_BINARY_OPERATOR(/, Long);
191 IMPLEMENT_BINARY_OPERATOR(/, Float);
192 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000193 default:
Chris Lattner02868352003-04-22 21:22:33 +0000194 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
195 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000196 }
197 return Dest;
198}
199
200static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000201 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000202 GenericValue Dest;
203 switch (Ty->getPrimitiveID()) {
204 IMPLEMENT_BINARY_OPERATOR(%, UByte);
205 IMPLEMENT_BINARY_OPERATOR(%, SByte);
206 IMPLEMENT_BINARY_OPERATOR(%, UShort);
207 IMPLEMENT_BINARY_OPERATOR(%, Short);
208 IMPLEMENT_BINARY_OPERATOR(%, UInt);
209 IMPLEMENT_BINARY_OPERATOR(%, Int);
210 IMPLEMENT_BINARY_OPERATOR(%, ULong);
211 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000212 case Type::FloatTyID:
213 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
214 break;
215 case Type::DoubleTyID:
216 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
217 break;
218 default:
Chris Lattner02868352003-04-22 21:22:33 +0000219 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
220 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000221 }
222 return Dest;
223}
224
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000225static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000226 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000227 GenericValue Dest;
228 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000229 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000230 IMPLEMENT_BINARY_OPERATOR(&, UByte);
231 IMPLEMENT_BINARY_OPERATOR(&, SByte);
232 IMPLEMENT_BINARY_OPERATOR(&, UShort);
233 IMPLEMENT_BINARY_OPERATOR(&, Short);
234 IMPLEMENT_BINARY_OPERATOR(&, UInt);
235 IMPLEMENT_BINARY_OPERATOR(&, Int);
236 IMPLEMENT_BINARY_OPERATOR(&, ULong);
237 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000238 default:
Chris Lattner02868352003-04-22 21:22:33 +0000239 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
240 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000241 }
242 return Dest;
243}
244
245
246static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000247 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000248 GenericValue Dest;
249 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000250 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000251 IMPLEMENT_BINARY_OPERATOR(|, UByte);
252 IMPLEMENT_BINARY_OPERATOR(|, SByte);
253 IMPLEMENT_BINARY_OPERATOR(|, UShort);
254 IMPLEMENT_BINARY_OPERATOR(|, Short);
255 IMPLEMENT_BINARY_OPERATOR(|, UInt);
256 IMPLEMENT_BINARY_OPERATOR(|, Int);
257 IMPLEMENT_BINARY_OPERATOR(|, ULong);
258 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000259 default:
Chris Lattner02868352003-04-22 21:22:33 +0000260 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
261 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000262 }
263 return Dest;
264}
265
266
267static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000268 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000269 GenericValue Dest;
270 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000271 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000272 IMPLEMENT_BINARY_OPERATOR(^, UByte);
273 IMPLEMENT_BINARY_OPERATOR(^, SByte);
274 IMPLEMENT_BINARY_OPERATOR(^, UShort);
275 IMPLEMENT_BINARY_OPERATOR(^, Short);
276 IMPLEMENT_BINARY_OPERATOR(^, UInt);
277 IMPLEMENT_BINARY_OPERATOR(^, Int);
278 IMPLEMENT_BINARY_OPERATOR(^, ULong);
279 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000280 default:
Chris Lattner02868352003-04-22 21:22:33 +0000281 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
282 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000283 }
284 return Dest;
285}
286
287
Chris Lattner92101ac2001-08-23 17:05:04 +0000288#define IMPLEMENT_SETCC(OP, TY) \
289 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
290
Chris Lattnerfd506f52003-04-23 19:55:35 +0000291// Handle pointers specially because they must be compared with only as much
292// width as the host has. We _do not_ want to be comparing 64 bit values when
293// running on a 32-bit target, otherwise the upper 32 bits might mess up
294// comparisons if they contain garbage.
295#define IMPLEMENT_POINTERSETCC(OP) \
296 case Type::PointerTyID: \
297 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
298 (void*)(intptr_t)Src2.PointerVal; break
299
Chris Lattner92101ac2001-08-23 17:05:04 +0000300static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000301 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000302 GenericValue Dest;
303 switch (Ty->getPrimitiveID()) {
304 IMPLEMENT_SETCC(==, UByte);
305 IMPLEMENT_SETCC(==, SByte);
306 IMPLEMENT_SETCC(==, UShort);
307 IMPLEMENT_SETCC(==, Short);
308 IMPLEMENT_SETCC(==, UInt);
309 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000310 IMPLEMENT_SETCC(==, ULong);
311 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000312 IMPLEMENT_SETCC(==, Float);
313 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000314 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000315 default:
Chris Lattner02868352003-04-22 21:22:33 +0000316 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
317 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000318 }
319 return Dest;
320}
321
322static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000323 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000324 GenericValue Dest;
325 switch (Ty->getPrimitiveID()) {
326 IMPLEMENT_SETCC(!=, UByte);
327 IMPLEMENT_SETCC(!=, SByte);
328 IMPLEMENT_SETCC(!=, UShort);
329 IMPLEMENT_SETCC(!=, Short);
330 IMPLEMENT_SETCC(!=, UInt);
331 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000332 IMPLEMENT_SETCC(!=, ULong);
333 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000334 IMPLEMENT_SETCC(!=, Float);
335 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000336 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000337
Chris Lattner92101ac2001-08-23 17:05:04 +0000338 default:
Chris Lattner02868352003-04-22 21:22:33 +0000339 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
340 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000341 }
342 return Dest;
343}
344
345static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000346 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000347 GenericValue Dest;
348 switch (Ty->getPrimitiveID()) {
349 IMPLEMENT_SETCC(<=, UByte);
350 IMPLEMENT_SETCC(<=, SByte);
351 IMPLEMENT_SETCC(<=, UShort);
352 IMPLEMENT_SETCC(<=, Short);
353 IMPLEMENT_SETCC(<=, UInt);
354 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000355 IMPLEMENT_SETCC(<=, ULong);
356 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000357 IMPLEMENT_SETCC(<=, Float);
358 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000359 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000360 default:
Chris Lattner02868352003-04-22 21:22:33 +0000361 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
362 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000363 }
364 return Dest;
365}
366
367static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000368 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000369 GenericValue Dest;
370 switch (Ty->getPrimitiveID()) {
371 IMPLEMENT_SETCC(>=, UByte);
372 IMPLEMENT_SETCC(>=, SByte);
373 IMPLEMENT_SETCC(>=, UShort);
374 IMPLEMENT_SETCC(>=, Short);
375 IMPLEMENT_SETCC(>=, UInt);
376 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000377 IMPLEMENT_SETCC(>=, ULong);
378 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000379 IMPLEMENT_SETCC(>=, Float);
380 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000381 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000382 default:
Chris Lattner02868352003-04-22 21:22:33 +0000383 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
384 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000385 }
386 return Dest;
387}
388
389static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000390 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000391 GenericValue Dest;
392 switch (Ty->getPrimitiveID()) {
393 IMPLEMENT_SETCC(<, UByte);
394 IMPLEMENT_SETCC(<, SByte);
395 IMPLEMENT_SETCC(<, UShort);
396 IMPLEMENT_SETCC(<, Short);
397 IMPLEMENT_SETCC(<, UInt);
398 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000399 IMPLEMENT_SETCC(<, ULong);
400 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000401 IMPLEMENT_SETCC(<, Float);
402 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000403 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000404 default:
Chris Lattner02868352003-04-22 21:22:33 +0000405 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
406 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000407 }
408 return Dest;
409}
410
411static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000412 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000413 GenericValue Dest;
414 switch (Ty->getPrimitiveID()) {
415 IMPLEMENT_SETCC(>, UByte);
416 IMPLEMENT_SETCC(>, SByte);
417 IMPLEMENT_SETCC(>, UShort);
418 IMPLEMENT_SETCC(>, Short);
419 IMPLEMENT_SETCC(>, UInt);
420 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000421 IMPLEMENT_SETCC(>, ULong);
422 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000423 IMPLEMENT_SETCC(>, Float);
424 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000425 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000426 default:
Chris Lattner02868352003-04-22 21:22:33 +0000427 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
428 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000429 }
430 return Dest;
431}
432
Chris Lattnerd7916e92003-05-10 21:22:39 +0000433void Interpreter::visitBinaryOperator(BinaryOperator &I) {
434 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000435 const Type *Ty = I.getOperand(0)->getType();
436 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
437 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000438 GenericValue R; // Result
439
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000440 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000441 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
442 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
443 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
444 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
445 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
446 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
447 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
448 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
449 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
450 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
451 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
452 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
453 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
454 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000455 default:
Chris Lattner02868352003-04-22 21:22:33 +0000456 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
457 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000458 }
459
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000460 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000461}
462
Chris Lattner92101ac2001-08-23 17:05:04 +0000463//===----------------------------------------------------------------------===//
464// Terminator Instruction Implementations
465//===----------------------------------------------------------------------===//
466
Chris Lattnere43db882001-10-27 04:15:57 +0000467void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000468 if (!QuietMode) {
Chris Lattner02868352003-04-22 21:22:33 +0000469 std::cout << "Program returned ";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000470 print(Type::IntTy, GV);
Chris Lattner02868352003-04-22 21:22:33 +0000471 std::cout << " via 'void exit(int)'\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000472 }
Chris Lattnere43db882001-10-27 04:15:57 +0000473
474 ExitCode = GV.SByteVal;
475 ECStack.clear();
476}
477
Chris Lattnerd7916e92003-05-10 21:22:39 +0000478void Interpreter::visitReturnInst(ReturnInst &I) {
479 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000480 const Type *RetTy = 0;
481 GenericValue Result;
482
483 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000484 if (I.getNumOperands()) {
485 RetTy = I.getReturnValue()->getType();
486 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000487 }
488
489 // Save previously executing meth
Chris Lattnerda82ed52003-05-08 16:18:31 +0000490 const Function *M = ECStack.back().CurFunction;
Chris Lattner92101ac2001-08-23 17:05:04 +0000491
492 // Pop the current stack frame... this invalidates SF
493 ECStack.pop_back();
494
495 if (ECStack.empty()) { // Finished main. Put result into exit code...
496 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000497 if (!QuietMode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000498 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattnerf23eb852001-12-14 16:49:29 +0000499 << "\" returned ";
500 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000501 std::cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000502 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000503
504 if (RetTy->isIntegral())
Chris Lattnerf4dca802002-05-02 19:28:45 +0000505 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000506 } else {
507 ExitCode = 0;
508 }
509 return;
510 }
511
512 // If we have a previous stack frame, and we have a previous call, fill in
513 // the return value...
514 //
515 ExecutionContext &NewSF = ECStack.back();
516 if (NewSF.Caller) {
517 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
518 SetValue(NewSF.Caller, Result, NewSF);
519
520 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000521 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000522 // This must be a function that is executing because of a user 'call'
523 // instruction.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000524 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000525 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000526 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000527 std::cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000528 }
529}
530
Chris Lattnerd7916e92003-05-10 21:22:39 +0000531void Interpreter::visitBranchInst(BranchInst &I) {
532 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000533 BasicBlock *Dest;
534
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000535 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
536 if (!I.isUnconditional()) {
537 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000538 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000539 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000540 }
Chris Lattner77113b62003-05-10 20:21:16 +0000541 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000542}
543
Chris Lattnerd7916e92003-05-10 21:22:39 +0000544void Interpreter::visitSwitchInst(SwitchInst &I) {
545 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000546 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
547 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000548
549 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000550 BasicBlock *Dest = 0;
551 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000552 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000553 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000554 Dest = cast<BasicBlock>(I.getOperand(i+1));
555 break;
556 }
Chris Lattner09e93922003-04-22 20:34:47 +0000557
558 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000559 SwitchToNewBasicBlock(Dest, SF);
560}
561
562// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
563// This function handles the actual updating of block and instruction iterators
564// as well as execution of all of the PHI nodes in the destination block.
565//
566// This method does this because all of the PHI nodes must be executed
567// atomically, reading their inputs before any of the results are updated. Not
568// doing this can cause problems if the PHI nodes depend on other PHI nodes for
569// their inputs. If the input PHI node is updated before it is read, incorrect
570// results can happen. Thus we use a two phase approach.
571//
572void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
573 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
574 SF.CurBB = Dest; // Update CurBB to branch destination
575 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
576
577 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
578
579 // Loop over all of the PHI nodes in the current block, reading their inputs.
580 std::vector<GenericValue> ResultValues;
581
582 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000583 if (Trace) CW << "Run:" << PN;
584
Chris Lattner77113b62003-05-10 20:21:16 +0000585 // Search for the value corresponding to this previous bb...
586 int i = PN->getBasicBlockIndex(PrevBB);
587 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
588 Value *IncomingValue = PN->getIncomingValue(i);
589
590 // Save the incoming value for this PHI node...
591 ResultValues.push_back(getOperandValue(IncomingValue, SF));
592 }
593
594 // Now loop over all of the PHI nodes setting their values...
595 SF.CurInst = SF.CurBB->begin();
596 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
597 ++SF.CurInst, ++i)
598 SetValue(PN, ResultValues[i], SF);
Chris Lattner09e93922003-04-22 20:34:47 +0000599}
600
601
Chris Lattner92101ac2001-08-23 17:05:04 +0000602//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000603// Memory Instruction Implementations
604//===----------------------------------------------------------------------===//
605
Chris Lattnerd7916e92003-05-10 21:22:39 +0000606void Interpreter::visitAllocationInst(AllocationInst &I) {
607 ExecutionContext &SF = ECStack.back();
608
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000609 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000610
Chris Lattnercc82cc12002-04-28 21:57:33 +0000611 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000612 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000613
614 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000615 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000616 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
617
Chris Lattnerfe11a972002-12-23 23:59:41 +0000618 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000619 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000620 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000621
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000622 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000623 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000624}
625
Chris Lattnerd7916e92003-05-10 21:22:39 +0000626void Interpreter::visitFreeInst(FreeInst &I) {
627 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000628 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
629 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000630 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000631 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000632}
633
Chris Lattner95c3af52001-10-29 19:32:19 +0000634
Chris Lattnera34c5682002-08-27 22:33:45 +0000635// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000636//
Chris Lattnerfe11a972002-12-23 23:59:41 +0000637GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
638 User::op_iterator E,
639 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000640 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000641 "Cannot getElementOffset of a nonpointer type!");
642
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000643 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000644 const Type *Ty = Ptr->getType();
645
646 for (; I != E; ++I) {
Chris Lattner782b9392001-11-26 18:18:18 +0000647 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
648 const StructLayout *SLO = TD.getStructLayout(STy);
649
Misha Brukmand5d96b92003-10-10 17:42:19 +0000650 // Indices must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000651 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000652 assert(CPU->getType() == Type::UByteTy);
653 unsigned Index = CPU->getValue();
654
Chris Lattner782b9392001-11-26 18:18:18 +0000655 Total += SLO->MemberOffsets[Index];
656 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000657 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000658
Chris Lattner006a4a52003-02-25 21:14:59 +0000659 // Get the index number for the array... which must be long type...
Chris Lattner0374b8d2002-09-11 01:21:35 +0000660 assert((*I)->getType() == Type::LongTy);
Chris Lattnere8b3e9b2002-09-13 23:30:42 +0000661 unsigned Idx = getOperandValue(*I, SF).LongVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000662 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000663 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattner02868352003-04-22 21:22:33 +0000664 std::cerr << "Out of range memory access to element #" << Idx
665 << " of a " << AT->getNumElements() << " element array."
666 << " Subscript #" << *I << "\n";
Brian Gaeke235b2002003-10-10 17:03:22 +0000667 abort();
Chris Lattnerf23eb852001-12-14 16:49:29 +0000668 }
Chris Lattner782b9392001-11-26 18:18:18 +0000669
Chris Lattnerf23eb852001-12-14 16:49:29 +0000670 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000671 unsigned Size = TD.getTypeSize(Ty);
672 Total += Size*Idx;
673 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000674 }
675
Chris Lattnera34c5682002-08-27 22:33:45 +0000676 GenericValue Result;
677 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
678 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000679}
680
Chris Lattnerd7916e92003-05-10 21:22:39 +0000681void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
682 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000683 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattnera34c5682002-08-27 22:33:45 +0000684 I.idx_begin(), I.idx_end(), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000685}
686
Chris Lattnerd7916e92003-05-10 21:22:39 +0000687void Interpreter::visitLoadInst(LoadInst &I) {
688 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000689 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000690 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000691 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000692 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000693}
694
Chris Lattnerd7916e92003-05-10 21:22:39 +0000695void Interpreter::visitStoreInst(StoreInst &I) {
696 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000697 GenericValue Val = getOperandValue(I.getOperand(0), SF);
698 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000699 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000700 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000701}
702
Chris Lattner86660982001-08-27 05:16:50 +0000703
Chris Lattnerab2dea52002-11-07 19:29:31 +0000704
Chris Lattner86660982001-08-27 05:16:50 +0000705//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000706// Miscellaneous Instruction Implementations
707//===----------------------------------------------------------------------===//
708
Chris Lattnerd7916e92003-05-10 21:22:39 +0000709void Interpreter::visitCallInst(CallInst &I) {
710 ExecutionContext &SF = ECStack.back();
711 SF.Caller = &I;
Chris Lattner02868352003-04-22 21:22:33 +0000712 std::vector<GenericValue> ArgVals;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000713 ArgVals.reserve(I.getNumOperands()-1);
Chris Lattner93780132003-01-13 00:58:52 +0000714 for (unsigned i = 1; i < I.getNumOperands(); ++i) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000715 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
Chris Lattner93780132003-01-13 00:58:52 +0000716 // Promote all integral types whose size is < sizeof(int) into ints. We do
717 // this by zero or sign extending the value as appropriate according to the
718 // source type.
719 if (I.getOperand(i)->getType()->isIntegral() &&
720 I.getOperand(i)->getType()->getPrimitiveSize() < 4) {
721 const Type *Ty = I.getOperand(i)->getType();
722 if (Ty == Type::ShortTy)
723 ArgVals.back().IntVal = ArgVals.back().ShortVal;
724 else if (Ty == Type::UShortTy)
725 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
726 else if (Ty == Type::SByteTy)
727 ArgVals.back().IntVal = ArgVals.back().SByteVal;
728 else if (Ty == Type::UByteTy)
729 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
730 else if (Ty == Type::BoolTy)
731 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
732 else
733 assert(0 && "Unknown type!");
734 }
735 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000736
Chris Lattner070cf5e2001-11-07 20:12:30 +0000737 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000738 // and treat it as a function pointer.
Chris Lattnerd7916e92003-05-10 21:22:39 +0000739 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000740 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000741}
742
Chris Lattner86660982001-08-27 05:16:50 +0000743#define IMPLEMENT_SHIFT(OP, TY) \
744 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
745
Chris Lattnerd7916e92003-05-10 21:22:39 +0000746void Interpreter::visitShl(ShiftInst &I) {
747 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000748 const Type *Ty = I.getOperand(0)->getType();
749 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
750 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000751 GenericValue Dest;
752
753 switch (Ty->getPrimitiveID()) {
754 IMPLEMENT_SHIFT(<<, UByte);
755 IMPLEMENT_SHIFT(<<, SByte);
756 IMPLEMENT_SHIFT(<<, UShort);
757 IMPLEMENT_SHIFT(<<, Short);
758 IMPLEMENT_SHIFT(<<, UInt);
759 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000760 IMPLEMENT_SHIFT(<<, ULong);
761 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000762 default:
Chris Lattner02868352003-04-22 21:22:33 +0000763 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000764 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000765 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000766}
767
Chris Lattnerd7916e92003-05-10 21:22:39 +0000768void Interpreter::visitShr(ShiftInst &I) {
769 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000770 const Type *Ty = I.getOperand(0)->getType();
771 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
772 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000773 GenericValue Dest;
774
775 switch (Ty->getPrimitiveID()) {
776 IMPLEMENT_SHIFT(>>, UByte);
777 IMPLEMENT_SHIFT(>>, SByte);
778 IMPLEMENT_SHIFT(>>, UShort);
779 IMPLEMENT_SHIFT(>>, Short);
780 IMPLEMENT_SHIFT(>>, UInt);
781 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000782 IMPLEMENT_SHIFT(>>, ULong);
783 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000784 default:
Chris Lattner02868352003-04-22 21:22:33 +0000785 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
786 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000787 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000788 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000789}
790
791#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000792 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000793
794#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
795 case Type::DESTTY##TyID: \
796 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000797 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000798 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
799 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
800 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
801 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
802 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000803 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
804 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000805 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
806 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000807
808#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
809 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
810 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
811
812#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner02868352003-04-22 21:22:33 +0000813 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
814 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000815 } \
816 break
817
818#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
819 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
820 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000821 IMPLEMENT_CAST_CASE_END()
822
Brian Gaeke29794cb2003-09-05 18:55:03 +0000823GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
824 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000825 const Type *SrcTy = SrcVal->getType();
826 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000827
828 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000829 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
830 IMPLEMENT_CAST_CASE(SByte , ( signed char));
831 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000832 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000833 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
834 IMPLEMENT_CAST_CASE(Int , ( signed int ));
835 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
836 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000837 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000838 IMPLEMENT_CAST_CASE(Float , (float));
839 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +0000840 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +0000841 default:
Chris Lattner02868352003-04-22 21:22:33 +0000842 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +0000843 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000844 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000845
846 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000847}
Chris Lattner92101ac2001-08-23 17:05:04 +0000848
849
Chris Lattnerd7916e92003-05-10 21:22:39 +0000850void Interpreter::visitCastInst(CastInst &I) {
851 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +0000852 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
853}
Chris Lattner92101ac2001-08-23 17:05:04 +0000854
Chris Lattner4c665492003-10-18 05:55:25 +0000855void Interpreter::visitVANextInst(VANextInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000856 ExecutionContext &SF = ECStack.back();
857
Chris Lattner4c665492003-10-18 05:55:25 +0000858 // Get the incoming valist element. LLI treats the valist as an integer.
859 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
860
861 // Move to the next operand.
Chris Lattner374344c2003-05-08 16:52:43 +0000862 unsigned Argument = VAList.IntVal++;
Chris Lattner374344c2003-05-08 16:52:43 +0000863 assert(Argument < SF.VarArgs.size() &&
864 "Accessing past the last vararg argument!");
Chris Lattner4c665492003-10-18 05:55:25 +0000865 SetValue(&I, VAList, SF);
Chris Lattner374344c2003-05-08 16:52:43 +0000866}
Chris Lattner92101ac2001-08-23 17:05:04 +0000867
868//===----------------------------------------------------------------------===//
869// Dispatch and Execution Code
870//===----------------------------------------------------------------------===//
871
Chris Lattner63bd6132003-09-17 17:26:22 +0000872FunctionInfo::FunctionInfo(Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000873 // Assign slot numbers to the function arguments...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000874 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
875 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
Chris Lattner92101ac2001-08-23 17:05:04 +0000876
877 // Iterate over all of the instructions...
878 unsigned InstNum = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000879 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
880 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
881 // For each instruction... Add Annote
882 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
Chris Lattner92101ac2001-08-23 17:05:04 +0000883}
884
Chris Lattnerda82ed52003-05-08 16:18:31 +0000885unsigned FunctionInfo::getValueSlot(const Value *V) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000886 unsigned Plane = V->getType()->getUniqueID();
887 if (Plane >= NumPlaneElements.size())
888 NumPlaneElements.resize(Plane+1, 0);
889 return NumPlaneElements[Plane]++;
890}
891
892
Chris Lattner92101ac2001-08-23 17:05:04 +0000893//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000894// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +0000895//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000896void Interpreter::callFunction(Function *F,
897 const std::vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000898 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
899 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
900 "Incorrect number of arguments passed into function call!");
Chris Lattnercdf51782003-05-08 16:06:52 +0000901 if (F->isExternal()) {
Chris Lattnerda82ed52003-05-08 16:18:31 +0000902 GenericValue Result = callExternalFunction(F, ArgVals);
Chris Lattnercdf51782003-05-08 16:06:52 +0000903 const Type *RetTy = F->getReturnType();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000904
905 // Copy the result back into the result variable if we are not returning
906 // void.
907 if (RetTy != Type::VoidTy) {
908 if (!ECStack.empty() && ECStack.back().Caller) {
909 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000910 SetValue(SF.Caller, Result, SF);
911
912 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000913 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000914 // print it.
Chris Lattnercdf51782003-05-08 16:06:52 +0000915 CW << "Function " << F->getType() << " \"" << F->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000916 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000917 print(RetTy, Result);
Chris Lattner02868352003-04-22 21:22:33 +0000918 std::cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000919
920 if (RetTy->isIntegral())
Chris Lattner0c4e8862002-09-03 01:08:28 +0000921 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattnerbb76f022001-10-30 20:27:31 +0000922 }
923 }
924
Chris Lattner92101ac2001-08-23 17:05:04 +0000925 return;
926 }
927
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000928 // Process the function, assigning instruction numbers to the instructions in
929 // the function. Also calculate the number of values for each type slot
930 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +0000931 //
Chris Lattner63bd6132003-09-17 17:26:22 +0000932 FunctionInfo *&FuncInfo = FunctionInfoMap[F];
933 if (!FuncInfo) FuncInfo = new FunctionInfo(F);
Chris Lattner86660982001-08-27 05:16:50 +0000934
Chris Lattner63bd6132003-09-17 17:26:22 +0000935 // Make a new stack frame... and fill it in.
936 ECStack.push_back(ExecutionContext());
937 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000938 StackFrame.CurFunction = F;
Chris Lattnercdf51782003-05-08 16:06:52 +0000939 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +0000940 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000941 StackFrame.FuncInfo = FuncInfo;
Chris Lattner92101ac2001-08-23 17:05:04 +0000942
943 // Initialize the values to nothing...
Chris Lattnerda82ed52003-05-08 16:18:31 +0000944 StackFrame.Values.resize(FuncInfo->NumPlaneElements.size());
945 for (unsigned i = 0; i < FuncInfo->NumPlaneElements.size(); ++i) {
946 StackFrame.Values[i].resize(FuncInfo->NumPlaneElements[i]);
Chris Lattner92101ac2001-08-23 17:05:04 +0000947
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000948 // Taint the initial values of stuff
949 memset(&StackFrame.Values[i][0], 42,
Chris Lattnerda82ed52003-05-08 16:18:31 +0000950 FuncInfo->NumPlaneElements[i]*sizeof(GenericValue));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000951 }
952
Chris Lattner92101ac2001-08-23 17:05:04 +0000953
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000954 // Run through the function arguments and initialize their values...
Chris Lattnercdf51782003-05-08 16:06:52 +0000955 assert((ArgVals.size() == F->asize() ||
956 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000957 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +0000958
959 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +0000960 unsigned i = 0;
Chris Lattnercdf51782003-05-08 16:06:52 +0000961 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000962 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +0000963
964 // Handle varargs arguments...
965 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +0000966}
967
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000968// executeInstruction - Interpret a single instruction & increment the "PC".
Chris Lattner92101ac2001-08-23 17:05:04 +0000969//
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000970void Interpreter::executeInstruction() {
Chris Lattner92101ac2001-08-23 17:05:04 +0000971 assert(!ECStack.empty() && "No program running, cannot execute inst!");
972
973 ExecutionContext &SF = ECStack.back(); // Current stack frame
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000974 Instruction &I = *SF.CurInst++; // Increment before execute
Chris Lattner92101ac2001-08-23 17:05:04 +0000975
Chris Lattnerd7916e92003-05-10 21:22:39 +0000976 if (Trace) CW << "Run:" << I;
Chris Lattner5af0c482001-11-07 04:23:00 +0000977
Chris Lattnerbbdabce2002-12-08 05:51:08 +0000978 // Track the number of dynamic instructions executed.
979 ++NumDynamicInsts;
980
Chris Lattnerd7916e92003-05-10 21:22:39 +0000981 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +0000982
983 // Reset the current frame location to the top of stack
984 CurFrame = ECStack.size()-1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000985}
986
Chris Lattner92101ac2001-08-23 17:05:04 +0000987void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000988 while (!ECStack.empty()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000989 // Run an instruction...
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000990 executeInstruction();
Chris Lattner92101ac2001-08-23 17:05:04 +0000991 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000992}
993
994void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000995 switch (Ty->getPrimitiveID()) {
Chris Lattner02868352003-04-22 21:22:33 +0000996 case Type::BoolTyID: std::cout << (V.BoolVal?"true":"false"); break;
Chris Lattner65629d52002-08-13 20:45:11 +0000997 case Type::SByteTyID:
Chris Lattner02868352003-04-22 21:22:33 +0000998 std::cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
Chris Lattner65629d52002-08-13 20:45:11 +0000999 case Type::UByteTyID:
Chris Lattner02868352003-04-22 21:22:33 +00001000 std::cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
1001 case Type::ShortTyID: std::cout << V.ShortVal; break;
1002 case Type::UShortTyID: std::cout << V.UShortVal; break;
1003 case Type::IntTyID: std::cout << V.IntVal; break;
1004 case Type::UIntTyID: std::cout << V.UIntVal; break;
1005 case Type::LongTyID: std::cout << (long)V.LongVal; break;
1006 case Type::ULongTyID: std::cout << (unsigned long)V.ULongVal; break;
1007 case Type::FloatTyID: std::cout << V.FloatVal; break;
1008 case Type::DoubleTyID: std::cout << V.DoubleVal; break;
1009 case Type::PointerTyID:std::cout << (void*)GVTOP(V); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001010 default:
Chris Lattner02868352003-04-22 21:22:33 +00001011 std::cout << "- Don't know how to print value of this type!";
Chris Lattner92101ac2001-08-23 17:05:04 +00001012 break;
1013 }
1014}
1015
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001016void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001017 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001018 printValue(Ty, V);
1019}