blob: da1fe54dfa398d680aac3e8f9c502e811cb1cc66 [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00002//
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.
Misha Brukmand1c881a2005-04-21 22:43:08 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00009//
Chris Lattner92101ac2001-08-23 17:05:04 +000010// This file contains the actual instruction interpreter.
11//
12//===----------------------------------------------------------------------===//
13
Brian Gaeke63438cc2003-12-11 00:22:59 +000014#define DEBUG_TYPE "interpreter"
Chris Lattner92101ac2001-08-23 17:05:04 +000015#include "Interpreter.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner73011782003-12-28 09:44:37 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/Instructions.h"
Chris Lattner30483732004-06-20 07:49:54 +000019#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattner4af6de82003-11-25 20:44:56 +000020#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/Statistic.h"
22#include "llvm/Support/Debug.h"
Brian Gaeke235b2002003-10-10 17:03:22 +000023#include <cmath> // For fmod
Chris Lattner4af6de82003-11-25 20:44:56 +000024using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000025
Chris Lattnerbbdabce2002-12-08 05:51:08 +000026namespace {
27 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
Chris Lattnerbbdabce2002-12-08 05:51:08 +000028
Chris Lattner4af6de82003-11-25 20:44:56 +000029 Interpreter *TheEE = 0;
30}
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Chris Lattner73011782003-12-28 09:44:37 +000032
Chris Lattner2e42d3a2001-10-15 05:51:48 +000033//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000034// Value Manipulation code
35//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000036
Misha Brukmand1c881a2005-04-21 22:43:08 +000037static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
38 const Type *Ty);
39static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
40 const Type *Ty);
41static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
42 const Type *Ty);
Reid Spencer1628cec2006-10-26 06:15:43 +000043static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
44 const Type *Ty);
45static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
46 const Type *Ty);
47static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
48 const Type *Ty);
Reid Spencer0a783f72006-11-02 01:53:59 +000049static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
50 const Type *Ty);
51static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
52 const Type *Ty);
53static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
54 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000055static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
56 const Type *Ty);
57static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
58 const Type *Ty);
59static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
60 const Type *Ty);
61static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
62 const Type *Ty);
63static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
64 const Type *Ty);
65static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
66 const Type *Ty);
67static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
68 const Type *Ty);
69static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
70 const Type *Ty);
71static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
72 const Type *Ty);
73static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
74 const Type *Ty);
Reid Spencer3822ff52006-11-08 06:47:33 +000075static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
76 const Type *Ty);
77static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
78 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000079static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +000080 GenericValue Src3);
81
Brian Gaeke63438cc2003-12-11 00:22:59 +000082GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
83 ExecutionContext &SF) {
84 switch (CE->getOpcode()) {
85 case Instruction::Cast:
86 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
87 case Instruction::GetElementPtr:
88 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
89 gep_type_end(CE), SF);
90 case Instruction::Add:
91 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
92 getOperandValue(CE->getOperand(1), SF),
93 CE->getOperand(0)->getType());
94 case Instruction::Sub:
95 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
96 getOperandValue(CE->getOperand(1), SF),
97 CE->getOperand(0)->getType());
98 case Instruction::Mul:
99 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
100 getOperandValue(CE->getOperand(1), SF),
101 CE->getOperand(0)->getType());
Reid Spencer1628cec2006-10-26 06:15:43 +0000102 case Instruction::SDiv:
103 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
104 getOperandValue(CE->getOperand(1), SF),
105 CE->getOperand(0)->getType());
106 case Instruction::UDiv:
107 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
108 getOperandValue(CE->getOperand(1), SF),
109 CE->getOperand(0)->getType());
110 case Instruction::FDiv:
111 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
112 getOperandValue(CE->getOperand(1), SF),
113 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000114 case Instruction::URem:
115 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
Brian Gaeke63438cc2003-12-11 00:22:59 +0000116 getOperandValue(CE->getOperand(1), SF),
117 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000118 case Instruction::SRem:
119 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
120 getOperandValue(CE->getOperand(1), SF),
121 CE->getOperand(0)->getType());
122 case Instruction::FRem:
123 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
124 getOperandValue(CE->getOperand(1), SF),
125 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000126 case Instruction::And:
127 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
128 getOperandValue(CE->getOperand(1), SF),
129 CE->getOperand(0)->getType());
130 case Instruction::Or:
131 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
132 getOperandValue(CE->getOperand(1), SF),
133 CE->getOperand(0)->getType());
134 case Instruction::Xor:
135 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
136 getOperandValue(CE->getOperand(1), SF),
137 CE->getOperand(0)->getType());
138 case Instruction::SetEQ:
139 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
140 getOperandValue(CE->getOperand(1), SF),
141 CE->getOperand(0)->getType());
142 case Instruction::SetNE:
143 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
144 getOperandValue(CE->getOperand(1), SF),
145 CE->getOperand(0)->getType());
146 case Instruction::SetLE:
147 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
148 getOperandValue(CE->getOperand(1), SF),
149 CE->getOperand(0)->getType());
150 case Instruction::SetGE:
151 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
152 getOperandValue(CE->getOperand(1), SF),
153 CE->getOperand(0)->getType());
154 case Instruction::SetLT:
155 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
156 getOperandValue(CE->getOperand(1), SF),
157 CE->getOperand(0)->getType());
158 case Instruction::SetGT:
159 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
160 getOperandValue(CE->getOperand(1), SF),
161 CE->getOperand(0)->getType());
162 case Instruction::Shl:
163 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
164 getOperandValue(CE->getOperand(1), SF),
165 CE->getOperand(0)->getType());
Reid Spencer3822ff52006-11-08 06:47:33 +0000166 case Instruction::LShr:
167 return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
168 getOperandValue(CE->getOperand(1), SF),
169 CE->getOperand(0)->getType());
170 case Instruction::AShr:
171 return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
172 getOperandValue(CE->getOperand(1), SF),
173 CE->getOperand(0)->getType());
Chris Lattner759d34f2004-04-20 16:43:21 +0000174 case Instruction::Select:
175 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
176 getOperandValue(CE->getOperand(1), SF),
177 getOperandValue(CE->getOperand(2), SF));
Brian Gaeke63438cc2003-12-11 00:22:59 +0000178 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000179 std::cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Brian Gaeke63438cc2003-12-11 00:22:59 +0000180 abort();
181 return GenericValue();
182 }
183}
Chris Lattnera34c5682002-08-27 22:33:45 +0000184
Brian Gaeke29794cb2003-09-05 18:55:03 +0000185GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000186 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000187 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000188 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000189 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000190 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000191 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000192 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000193 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000194 }
195}
196
Chris Lattner39bb5b42001-10-15 13:25:40 +0000197static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000198 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000199}
200
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000201void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000202 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000203}
204
Chris Lattner2adcd832002-05-03 19:52:30 +0000205//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000206// Binary Instruction Implementations
207//===----------------------------------------------------------------------===//
208
209#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
210 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
211
Misha Brukmand1c881a2005-04-21 22:43:08 +0000212static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
213 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000214 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000215 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000216 IMPLEMENT_BINARY_OPERATOR(+, UByte);
217 IMPLEMENT_BINARY_OPERATOR(+, SByte);
218 IMPLEMENT_BINARY_OPERATOR(+, UShort);
219 IMPLEMENT_BINARY_OPERATOR(+, Short);
220 IMPLEMENT_BINARY_OPERATOR(+, UInt);
221 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000222 IMPLEMENT_BINARY_OPERATOR(+, ULong);
223 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000224 IMPLEMENT_BINARY_OPERATOR(+, Float);
225 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000226 default:
Chris Lattner02868352003-04-22 21:22:33 +0000227 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
228 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000229 }
230 return Dest;
231}
232
Misha Brukmand1c881a2005-04-21 22:43:08 +0000233static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
234 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000235 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000236 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000237 IMPLEMENT_BINARY_OPERATOR(-, UByte);
238 IMPLEMENT_BINARY_OPERATOR(-, SByte);
239 IMPLEMENT_BINARY_OPERATOR(-, UShort);
240 IMPLEMENT_BINARY_OPERATOR(-, Short);
241 IMPLEMENT_BINARY_OPERATOR(-, UInt);
242 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000243 IMPLEMENT_BINARY_OPERATOR(-, ULong);
244 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000245 IMPLEMENT_BINARY_OPERATOR(-, Float);
246 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000247 default:
Chris Lattner02868352003-04-22 21:22:33 +0000248 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
249 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000250 }
251 return Dest;
252}
253
Misha Brukmand1c881a2005-04-21 22:43:08 +0000254static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
255 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000256 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000257 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000258 IMPLEMENT_BINARY_OPERATOR(*, UByte);
259 IMPLEMENT_BINARY_OPERATOR(*, SByte);
260 IMPLEMENT_BINARY_OPERATOR(*, UShort);
261 IMPLEMENT_BINARY_OPERATOR(*, Short);
262 IMPLEMENT_BINARY_OPERATOR(*, UInt);
263 IMPLEMENT_BINARY_OPERATOR(*, Int);
264 IMPLEMENT_BINARY_OPERATOR(*, ULong);
265 IMPLEMENT_BINARY_OPERATOR(*, Long);
266 IMPLEMENT_BINARY_OPERATOR(*, Float);
267 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000268 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000269 std::cout << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000270 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000271 }
272 return Dest;
273}
274
Reid Spencerfe855262006-11-01 03:41:05 +0000275#define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
276 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
277
Reid Spencer1628cec2006-10-26 06:15:43 +0000278static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
279 const Type *Ty) {
280 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000281 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000282 IMPLEMENT_SIGNLESS_BINOP(/, UByte, SByte);
283 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
284 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
285 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
Reid Spencer1628cec2006-10-26 06:15:43 +0000286 default:
287 std::cout << "Unhandled type for UDiv instruction: " << *Ty << "\n";
288 abort();
289 }
290 return Dest;
291}
292
293static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
294 const Type *Ty) {
295 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000296 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000297 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
298 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
299 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
300 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
Reid Spencer1628cec2006-10-26 06:15:43 +0000301 default:
302 std::cout << "Unhandled type for SDiv instruction: " << *Ty << "\n";
303 abort();
304 }
305 return Dest;
306}
307
308static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000309 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000310 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000311 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000312 IMPLEMENT_BINARY_OPERATOR(/, Float);
313 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000314 default:
Chris Lattner02868352003-04-22 21:22:33 +0000315 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
316 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000317 }
318 return Dest;
319}
320
Reid Spencer0a783f72006-11-02 01:53:59 +0000321static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000322 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000323 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000324 switch (Ty->getTypeID()) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000325 IMPLEMENT_SIGNLESS_BINOP(%, UByte, SByte);
326 IMPLEMENT_SIGNLESS_BINOP(%, UShort, Short);
327 IMPLEMENT_SIGNLESS_BINOP(%, UInt, Int);
328 IMPLEMENT_SIGNLESS_BINOP(%, ULong, Long);
329 default:
330 std::cout << "Unhandled type for URem instruction: " << *Ty << "\n";
331 abort();
332 }
333 return Dest;
334}
335
336static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
337 const Type *Ty) {
338 GenericValue Dest;
339 switch (Ty->getTypeID()) {
340 IMPLEMENT_SIGNLESS_BINOP(%, SByte, UByte);
341 IMPLEMENT_SIGNLESS_BINOP(%, Short, UShort);
342 IMPLEMENT_SIGNLESS_BINOP(%, Int, UInt);
343 IMPLEMENT_SIGNLESS_BINOP(%, Long, ULong);
344 default:
345 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
346 abort();
347 }
348 return Dest;
349}
350
351static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
352 const Type *Ty) {
353 GenericValue Dest;
354 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000355 case Type::FloatTyID:
356 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
357 break;
358 case Type::DoubleTyID:
359 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
360 break;
361 default:
Chris Lattner02868352003-04-22 21:22:33 +0000362 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
363 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000364 }
365 return Dest;
366}
367
Misha Brukmand1c881a2005-04-21 22:43:08 +0000368static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
369 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000370 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000371 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000372 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000373 IMPLEMENT_BINARY_OPERATOR(&, UByte);
374 IMPLEMENT_BINARY_OPERATOR(&, SByte);
375 IMPLEMENT_BINARY_OPERATOR(&, UShort);
376 IMPLEMENT_BINARY_OPERATOR(&, Short);
377 IMPLEMENT_BINARY_OPERATOR(&, UInt);
378 IMPLEMENT_BINARY_OPERATOR(&, Int);
379 IMPLEMENT_BINARY_OPERATOR(&, ULong);
380 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000381 default:
Chris Lattner02868352003-04-22 21:22:33 +0000382 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
383 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000384 }
385 return Dest;
386}
387
Misha Brukmand1c881a2005-04-21 22:43:08 +0000388static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000389 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000390 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000391 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000392 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000393 IMPLEMENT_BINARY_OPERATOR(|, UByte);
394 IMPLEMENT_BINARY_OPERATOR(|, SByte);
395 IMPLEMENT_BINARY_OPERATOR(|, UShort);
396 IMPLEMENT_BINARY_OPERATOR(|, Short);
397 IMPLEMENT_BINARY_OPERATOR(|, UInt);
398 IMPLEMENT_BINARY_OPERATOR(|, Int);
399 IMPLEMENT_BINARY_OPERATOR(|, ULong);
400 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000401 default:
Chris Lattner02868352003-04-22 21:22:33 +0000402 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
403 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000404 }
405 return Dest;
406}
407
Misha Brukmand1c881a2005-04-21 22:43:08 +0000408static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000409 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000410 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000411 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000412 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000413 IMPLEMENT_BINARY_OPERATOR(^, UByte);
414 IMPLEMENT_BINARY_OPERATOR(^, SByte);
415 IMPLEMENT_BINARY_OPERATOR(^, UShort);
416 IMPLEMENT_BINARY_OPERATOR(^, Short);
417 IMPLEMENT_BINARY_OPERATOR(^, UInt);
418 IMPLEMENT_BINARY_OPERATOR(^, Int);
419 IMPLEMENT_BINARY_OPERATOR(^, ULong);
420 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000421 default:
Chris Lattner02868352003-04-22 21:22:33 +0000422 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
423 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000424 }
425 return Dest;
426}
427
Chris Lattner92101ac2001-08-23 17:05:04 +0000428#define IMPLEMENT_SETCC(OP, TY) \
429 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
430
Chris Lattnerfd506f52003-04-23 19:55:35 +0000431// Handle pointers specially because they must be compared with only as much
432// width as the host has. We _do not_ want to be comparing 64 bit values when
433// running on a 32-bit target, otherwise the upper 32 bits might mess up
434// comparisons if they contain garbage.
435#define IMPLEMENT_POINTERSETCC(OP) \
436 case Type::PointerTyID: \
437 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
438 (void*)(intptr_t)Src2.PointerVal; break
439
Misha Brukmand1c881a2005-04-21 22:43:08 +0000440static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
441 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000442 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000443 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000444 IMPLEMENT_SETCC(==, UByte);
445 IMPLEMENT_SETCC(==, SByte);
446 IMPLEMENT_SETCC(==, UShort);
447 IMPLEMENT_SETCC(==, Short);
448 IMPLEMENT_SETCC(==, UInt);
449 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000450 IMPLEMENT_SETCC(==, ULong);
451 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000452 IMPLEMENT_SETCC(==, Float);
453 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000454 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000455 default:
Chris Lattner02868352003-04-22 21:22:33 +0000456 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
457 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000458 }
459 return Dest;
460}
461
Misha Brukmand1c881a2005-04-21 22:43:08 +0000462static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
463 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000464 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000465 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000466 IMPLEMENT_SETCC(!=, UByte);
467 IMPLEMENT_SETCC(!=, SByte);
468 IMPLEMENT_SETCC(!=, UShort);
469 IMPLEMENT_SETCC(!=, Short);
470 IMPLEMENT_SETCC(!=, UInt);
471 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000472 IMPLEMENT_SETCC(!=, ULong);
473 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000474 IMPLEMENT_SETCC(!=, Float);
475 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000476 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000477
Chris Lattner92101ac2001-08-23 17:05:04 +0000478 default:
Chris Lattner02868352003-04-22 21:22:33 +0000479 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
480 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000481 }
482 return Dest;
483}
484
Misha Brukmand1c881a2005-04-21 22:43:08 +0000485static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
486 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000487 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000488 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000489 IMPLEMENT_SETCC(<=, UByte);
490 IMPLEMENT_SETCC(<=, SByte);
491 IMPLEMENT_SETCC(<=, UShort);
492 IMPLEMENT_SETCC(<=, Short);
493 IMPLEMENT_SETCC(<=, UInt);
494 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000495 IMPLEMENT_SETCC(<=, ULong);
496 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000497 IMPLEMENT_SETCC(<=, Float);
498 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000499 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000500 default:
Chris Lattnered6c0732004-07-15 02:51:32 +0000501 std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000502 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000503 }
504 return Dest;
505}
506
Misha Brukmand1c881a2005-04-21 22:43:08 +0000507static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
508 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000509 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000510 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000511 IMPLEMENT_SETCC(>=, UByte);
512 IMPLEMENT_SETCC(>=, SByte);
513 IMPLEMENT_SETCC(>=, UShort);
514 IMPLEMENT_SETCC(>=, Short);
515 IMPLEMENT_SETCC(>=, UInt);
516 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000517 IMPLEMENT_SETCC(>=, ULong);
518 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000519 IMPLEMENT_SETCC(>=, Float);
520 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000521 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000522 default:
Chris Lattner02868352003-04-22 21:22:33 +0000523 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
524 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000525 }
526 return Dest;
527}
528
Misha Brukmand1c881a2005-04-21 22:43:08 +0000529static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
530 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000531 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000532 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000533 IMPLEMENT_SETCC(<, UByte);
534 IMPLEMENT_SETCC(<, SByte);
535 IMPLEMENT_SETCC(<, UShort);
536 IMPLEMENT_SETCC(<, Short);
537 IMPLEMENT_SETCC(<, UInt);
538 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000539 IMPLEMENT_SETCC(<, ULong);
540 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000541 IMPLEMENT_SETCC(<, Float);
542 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000543 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000544 default:
Chris Lattner02868352003-04-22 21:22:33 +0000545 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
546 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000547 }
548 return Dest;
549}
550
Misha Brukmand1c881a2005-04-21 22:43:08 +0000551static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
552 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000553 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000554 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000555 IMPLEMENT_SETCC(>, UByte);
556 IMPLEMENT_SETCC(>, SByte);
557 IMPLEMENT_SETCC(>, UShort);
558 IMPLEMENT_SETCC(>, Short);
559 IMPLEMENT_SETCC(>, UInt);
560 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000561 IMPLEMENT_SETCC(>, ULong);
562 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000563 IMPLEMENT_SETCC(>, Float);
564 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000565 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000566 default:
Chris Lattner02868352003-04-22 21:22:33 +0000567 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
568 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000569 }
570 return Dest;
571}
572
Chris Lattnerd7916e92003-05-10 21:22:39 +0000573void Interpreter::visitBinaryOperator(BinaryOperator &I) {
574 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000575 const Type *Ty = I.getOperand(0)->getType();
576 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
577 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000578 GenericValue R; // Result
579
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000580 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000581 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
582 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
583 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000584 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
585 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
586 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
Reid Spencer0a783f72006-11-02 01:53:59 +0000587 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
588 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
589 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000590 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
591 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
592 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
593 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
594 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
595 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
596 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
597 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
598 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000599 default:
Chris Lattner02868352003-04-22 21:22:33 +0000600 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
601 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000602 }
603
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000604 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000605}
606
Misha Brukmand1c881a2005-04-21 22:43:08 +0000607static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000608 GenericValue Src3) {
609 return Src1.BoolVal ? Src2 : Src3;
610}
611
612void Interpreter::visitSelectInst(SelectInst &I) {
613 ExecutionContext &SF = ECStack.back();
614 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
615 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
616 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
617 GenericValue R = executeSelectInst(Src1, Src2, Src3);
618 SetValue(&I, R, SF);
619}
620
621
Chris Lattner92101ac2001-08-23 17:05:04 +0000622//===----------------------------------------------------------------------===//
623// Terminator Instruction Implementations
624//===----------------------------------------------------------------------===//
625
Chris Lattnere43db882001-10-27 04:15:57 +0000626void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000627 // runAtExitHandlers() assumes there are no stack frames, but
628 // if exit() was called, then it had a stack frame. Blow away
629 // the stack before interpreting atexit handlers.
630 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000631 runAtExitHandlers ();
632 exit (GV.IntVal);
Chris Lattnere43db882001-10-27 04:15:57 +0000633}
634
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000635/// Pop the last stack frame off of ECStack and then copy the result
636/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000637/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000638/// CallInst if there was a previous stack frame. This method may
639/// invalidate any ECStack iterators you have. This method also takes
640/// care of switching to the normal destination BB, if we are returning
641/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000642///
643void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
644 GenericValue Result) {
645 // Pop the current stack frame.
646 ECStack.pop_back();
647
Misha Brukmand1c881a2005-04-21 22:43:08 +0000648 if (ECStack.empty()) { // Finished main. Put result into exit code...
649 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000650 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000651 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000652 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000653 }
654 } else {
655 // If we have a previous stack frame, and we have a previous call,
656 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000657 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000658 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000659 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000660 SetValue(I, Result, CallingSF);
661 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
662 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000663 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000664 }
665 }
666}
667
Chris Lattnerd7916e92003-05-10 21:22:39 +0000668void Interpreter::visitReturnInst(ReturnInst &I) {
669 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000670 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000671 GenericValue Result;
672
673 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000674 if (I.getNumOperands()) {
675 RetTy = I.getReturnValue()->getType();
676 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000677 }
678
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000679 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000680}
681
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000682void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000683 // Unwind stack
684 Instruction *Inst;
685 do {
686 ECStack.pop_back ();
687 if (ECStack.empty ())
688 abort ();
689 Inst = ECStack.back ().Caller.getInstruction ();
690 } while (!(Inst && isa<InvokeInst> (Inst)));
691
692 // Return from invoke
693 ExecutionContext &InvokingSF = ECStack.back ();
694 InvokingSF.Caller = CallSite ();
695
696 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000697 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000698}
699
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000700void Interpreter::visitUnreachableInst(UnreachableInst &I) {
701 std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
702 abort();
703}
704
Chris Lattnerd7916e92003-05-10 21:22:39 +0000705void Interpreter::visitBranchInst(BranchInst &I) {
706 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000707 BasicBlock *Dest;
708
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000709 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
710 if (!I.isUnconditional()) {
711 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000712 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000713 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000714 }
Chris Lattner77113b62003-05-10 20:21:16 +0000715 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000716}
717
Chris Lattnerd7916e92003-05-10 21:22:39 +0000718void Interpreter::visitSwitchInst(SwitchInst &I) {
719 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000720 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
721 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000722
723 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000724 BasicBlock *Dest = 0;
725 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000726 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000727 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000728 Dest = cast<BasicBlock>(I.getOperand(i+1));
729 break;
730 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000731
Chris Lattner09e93922003-04-22 20:34:47 +0000732 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000733 SwitchToNewBasicBlock(Dest, SF);
734}
735
736// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
737// This function handles the actual updating of block and instruction iterators
738// as well as execution of all of the PHI nodes in the destination block.
739//
740// This method does this because all of the PHI nodes must be executed
741// atomically, reading their inputs before any of the results are updated. Not
742// doing this can cause problems if the PHI nodes depend on other PHI nodes for
743// their inputs. If the input PHI node is updated before it is read, incorrect
744// results can happen. Thus we use a two phase approach.
745//
746void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
747 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
748 SF.CurBB = Dest; // Update CurBB to branch destination
749 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
750
751 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
752
753 // Loop over all of the PHI nodes in the current block, reading their inputs.
754 std::vector<GenericValue> ResultValues;
755
756 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
757 // Search for the value corresponding to this previous bb...
758 int i = PN->getBasicBlockIndex(PrevBB);
759 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
760 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000761
Chris Lattner77113b62003-05-10 20:21:16 +0000762 // Save the incoming value for this PHI node...
763 ResultValues.push_back(getOperandValue(IncomingValue, SF));
764 }
765
766 // Now loop over all of the PHI nodes setting their values...
767 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000768 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
769 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000770 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000771 }
Chris Lattner09e93922003-04-22 20:34:47 +0000772}
773
Chris Lattner92101ac2001-08-23 17:05:04 +0000774//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000775// Memory Instruction Implementations
776//===----------------------------------------------------------------------===//
777
Chris Lattnerd7916e92003-05-10 21:22:39 +0000778void Interpreter::visitAllocationInst(AllocationInst &I) {
779 ExecutionContext &SF = ECStack.back();
780
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000781 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000782
Chris Lattnercc82cc12002-04-28 21:57:33 +0000783 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000784 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000785
786 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +0000787 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000788
Chris Lattnerfe11a972002-12-23 23:59:41 +0000789 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000790 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000791 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000792
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000793 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000794 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000795}
796
Chris Lattnerd7916e92003-05-10 21:22:39 +0000797void Interpreter::visitFreeInst(FreeInst &I) {
798 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000799 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
800 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000801 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000802 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000803}
804
Chris Lattnera34c5682002-08-27 22:33:45 +0000805// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000806//
Chris Lattner4af6de82003-11-25 20:44:56 +0000807GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000808 gep_type_iterator E,
809 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000810 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000811 "Cannot getElementOffset of a nonpointer type!");
812
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000813 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000814
815 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000816 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000817 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000818
Reid Spencerb83eb642006-10-20 07:07:24 +0000819 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
820 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000821
Chris Lattnerd5644962005-01-08 20:05:34 +0000822 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000823 } else {
824 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000825 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000826 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
827
828 uint64_t Idx;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000829 switch (I.getOperand()->getType()->getTypeID()) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000830 default: assert(0 && "Illegal getelementptr index for sequential type!");
831 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
832 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
833 case Type::IntTyID: Idx = IdxGV.IntVal; break;
834 case Type::LongTyID: Idx = IdxGV.LongVal; break;
835 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
836 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
837 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
838 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
839 }
Chris Lattnerd5644962005-01-08 20:05:34 +0000840 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000841 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000842 }
843
Chris Lattnera34c5682002-08-27 22:33:45 +0000844 GenericValue Result;
845 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
846 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000847}
848
Chris Lattnerd7916e92003-05-10 21:22:39 +0000849void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
850 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000851 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000852 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000853}
854
Chris Lattnerd7916e92003-05-10 21:22:39 +0000855void Interpreter::visitLoadInst(LoadInst &I) {
856 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000857 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000858 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000859 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000860 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000861}
862
Chris Lattnerd7916e92003-05-10 21:22:39 +0000863void Interpreter::visitStoreInst(StoreInst &I) {
864 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000865 GenericValue Val = getOperandValue(I.getOperand(0), SF);
866 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000867 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000868 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000869}
870
Chris Lattner86660982001-08-27 05:16:50 +0000871//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000872// Miscellaneous Instruction Implementations
873//===----------------------------------------------------------------------===//
874
Brian Gaekefea483d2003-11-07 20:04:22 +0000875void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000876 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000877
878 // Check to see if this is an intrinsic function call...
879 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +0000880 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +0000881 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000882 case Intrinsic::not_intrinsic:
883 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000884 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000885 GenericValue ArgIndex;
886 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
887 ArgIndex.UIntPairVal.second = 0;
888 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000889 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000890 }
Chris Lattner317201d2004-03-13 00:24:00 +0000891 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000892 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000893 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000894 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
895 return;
896 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000897 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000898 // class to transform it into hopefully tasty LLVM code.
899 //
900 Instruction *Prev = CS.getInstruction()->getPrev();
901 BasicBlock *Parent = CS.getInstruction()->getParent();
902 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
903
904 // Restore the CurInst pointer to the first instruction newly inserted, if
905 // any.
906 if (!Prev) {
907 SF.CurInst = Parent->begin();
908 } else {
909 SF.CurInst = Prev;
910 ++SF.CurInst;
911 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000912 return;
Chris Lattner73011782003-12-28 09:44:37 +0000913 }
914
Brian Gaekefea483d2003-11-07 20:04:22 +0000915 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000916 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000917 const unsigned NumArgs = SF.Caller.arg_size();
918 ArgVals.reserve(NumArgs);
919 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
920 e = SF.Caller.arg_end(); i != e; ++i) {
921 Value *V = *i;
922 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000923 // Promote all integral types whose size is < sizeof(int) into ints. We do
924 // this by zero or sign extending the value as appropriate according to the
925 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000926 const Type *Ty = V->getType();
927 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000928 if (Ty == Type::ShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000929 ArgVals.back().IntVal = ArgVals.back().ShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000930 else if (Ty == Type::UShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000931 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000932 else if (Ty == Type::SByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000933 ArgVals.back().IntVal = ArgVals.back().SByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000934 else if (Ty == Type::UByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000935 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000936 else if (Ty == Type::BoolTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000937 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
Chris Lattner93780132003-01-13 00:58:52 +0000938 else
Misha Brukmand1c881a2005-04-21 22:43:08 +0000939 assert(0 && "Unknown type!");
Chris Lattner93780132003-01-13 00:58:52 +0000940 }
941 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000942
Misha Brukmand1c881a2005-04-21 22:43:08 +0000943 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000944 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000945 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000946 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000947}
948
Chris Lattner86660982001-08-27 05:16:50 +0000949#define IMPLEMENT_SHIFT(OP, TY) \
950 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
951
Reid Spencer3822ff52006-11-08 06:47:33 +0000952#define IMPLEMENT_SIGNLESS_SHIFT(OP, TY1, TY2) \
953 case Type::TY2##TyID: \
954 IMPLEMENT_SHIFT(OP, TY1)
955
Brian Gaeke63438cc2003-12-11 00:22:59 +0000956static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
957 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000958 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000959 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000960 IMPLEMENT_SHIFT(<<, UByte);
961 IMPLEMENT_SHIFT(<<, SByte);
962 IMPLEMENT_SHIFT(<<, UShort);
963 IMPLEMENT_SHIFT(<<, Short);
964 IMPLEMENT_SHIFT(<<, UInt);
965 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000966 IMPLEMENT_SHIFT(<<, ULong);
967 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000968 default:
Chris Lattner02868352003-04-22 21:22:33 +0000969 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000970 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000971 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000972}
973
Reid Spencer3822ff52006-11-08 06:47:33 +0000974static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
975 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000976 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000977 switch (Ty->getTypeID()) {
Reid Spencer3822ff52006-11-08 06:47:33 +0000978 IMPLEMENT_SIGNLESS_SHIFT(>>, UByte, SByte);
979 IMPLEMENT_SIGNLESS_SHIFT(>>, UShort, Short);
980 IMPLEMENT_SIGNLESS_SHIFT(>>, UInt, Int);
981 IMPLEMENT_SIGNLESS_SHIFT(>>, ULong, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000982 default:
Reid Spencer3822ff52006-11-08 06:47:33 +0000983 std::cout << "Unhandled type for LShr instruction: " << *Ty << "\n";
984 abort();
985 }
986 return Dest;
987}
988
989static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
990 const Type *Ty) {
991 GenericValue Dest;
992 switch (Ty->getTypeID()) {
993 IMPLEMENT_SIGNLESS_SHIFT(>>, SByte, UByte);
994 IMPLEMENT_SIGNLESS_SHIFT(>>, Short, UShort);
995 IMPLEMENT_SIGNLESS_SHIFT(>>, Int, UInt);
996 IMPLEMENT_SIGNLESS_SHIFT(>>, Long, ULong);
997 default:
998 std::cout << "Unhandled type for AShr instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000999 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001000 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001001 return Dest;
1002}
1003
1004void Interpreter::visitShl(ShiftInst &I) {
1005 ExecutionContext &SF = ECStack.back();
1006 const Type *Ty = I.getOperand(0)->getType();
1007 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1008 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1009 GenericValue Dest;
1010 Dest = executeShlInst (Src1, Src2, Ty);
1011 SetValue(&I, Dest, SF);
1012}
1013
Reid Spencer3822ff52006-11-08 06:47:33 +00001014void Interpreter::visitLShr(ShiftInst &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001015 ExecutionContext &SF = ECStack.back();
1016 const Type *Ty = I.getOperand(0)->getType();
1017 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1018 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1019 GenericValue Dest;
Reid Spencer3822ff52006-11-08 06:47:33 +00001020 Dest = executeLShrInst (Src1, Src2, Ty);
1021 SetValue(&I, Dest, SF);
1022}
1023
1024void Interpreter::visitAShr(ShiftInst &I) {
1025 ExecutionContext &SF = ECStack.back();
1026 const Type *Ty = I.getOperand(0)->getType();
1027 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1028 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1029 GenericValue Dest;
1030 Dest = executeAShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001031 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001032}
1033
1034#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001035 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +00001036
1037#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
1038 case Type::DESTTY##TyID: \
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001039 switch (SrcTy->getTypeID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +00001040 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +00001041 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
1042 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
1043 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1044 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1045 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +00001046 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1047 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +00001048 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
1049 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +00001050
1051#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
1052 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
1053 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
1054
1055#define IMPLEMENT_CAST_CASE_END() \
Chris Lattnered6c0732004-07-15 02:51:32 +00001056 default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
Chris Lattner02868352003-04-22 21:22:33 +00001057 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +00001058 } \
1059 break
1060
1061#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
1062 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1063 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +00001064 IMPLEMENT_CAST_CASE_END()
1065
Brian Gaeke29794cb2003-09-05 18:55:03 +00001066GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001067 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001068 const Type *SrcTy = SrcVal->getType();
1069 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001070
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001071 switch (Ty->getTypeID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001072 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1073 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1074 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +00001075 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001076 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1077 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1078 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1079 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +00001080 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001081 IMPLEMENT_CAST_CASE(Float , (float));
1082 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +00001083 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +00001084 default:
Chris Lattner02868352003-04-22 21:22:33 +00001085 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +00001086 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001087 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001088
1089 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001090}
Chris Lattner92101ac2001-08-23 17:05:04 +00001091
Chris Lattnerd7916e92003-05-10 21:22:39 +00001092void Interpreter::visitCastInst(CastInst &I) {
1093 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +00001094 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1095}
Chris Lattner92101ac2001-08-23 17:05:04 +00001096
Brian Gaekec1a2be12003-11-07 21:20:47 +00001097#define IMPLEMENT_VAARG(TY) \
1098 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1099
1100void Interpreter::visitVAArgInst(VAArgInst &I) {
1101 ExecutionContext &SF = ECStack.back();
1102
Brian Gaeke9d20b712004-02-25 23:01:48 +00001103 // Get the incoming valist parameter. LLI treats the valist as a
1104 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001105 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001106 GenericValue Dest;
1107 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001108 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001109 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001110 switch (Ty->getTypeID()) {
Brian Gaekec1a2be12003-11-07 21:20:47 +00001111 IMPLEMENT_VAARG(UByte);
1112 IMPLEMENT_VAARG(SByte);
1113 IMPLEMENT_VAARG(UShort);
1114 IMPLEMENT_VAARG(Short);
1115 IMPLEMENT_VAARG(UInt);
1116 IMPLEMENT_VAARG(Int);
1117 IMPLEMENT_VAARG(ULong);
1118 IMPLEMENT_VAARG(Long);
1119 IMPLEMENT_VAARG(Pointer);
1120 IMPLEMENT_VAARG(Float);
1121 IMPLEMENT_VAARG(Double);
1122 IMPLEMENT_VAARG(Bool);
1123 default:
1124 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1125 abort();
1126 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001127
Brian Gaekec1a2be12003-11-07 21:20:47 +00001128 // Set the Value of this Instruction.
1129 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001130
1131 // Move the pointer to the next vararg.
1132 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001133}
1134
Chris Lattner92101ac2001-08-23 17:05:04 +00001135//===----------------------------------------------------------------------===//
1136// Dispatch and Execution Code
1137//===----------------------------------------------------------------------===//
1138
Chris Lattner92101ac2001-08-23 17:05:04 +00001139//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001140// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001141//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001142void Interpreter::callFunction(Function *F,
1143 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001144 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1145 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1146 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001147 // Make a new stack frame... and fill it in.
1148 ECStack.push_back(ExecutionContext());
1149 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001150 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001151
1152 // Special handling for external functions.
1153 if (F->isExternal()) {
1154 GenericValue Result = callExternalFunction (F, ArgVals);
1155 // Simulate a 'ret' instruction of the appropriate type.
1156 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1157 return;
1158 }
1159
1160 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001161 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001162 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001163
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001164 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001165 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001166 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001167 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001168
1169 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001170 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001171 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001172 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001173
1174 // Handle varargs arguments...
1175 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001176}
1177
Chris Lattner92101ac2001-08-23 17:05:04 +00001178void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001179 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001180 // Interpret a single instruction & increment the "PC".
1181 ExecutionContext &SF = ECStack.back(); // Current stack frame
1182 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001183
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001184 // Track the number of dynamic instructions executed.
1185 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001186
Brian Gaeke63438cc2003-12-11 00:22:59 +00001187 DEBUG(std::cerr << "About to interpret: " << I);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001188 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001189 }
1190}