blob: cf41abd646c275a97adb5268e9ca550cffba61df [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);
75static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
76 const Type *Ty);
77static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +000078 GenericValue Src3);
79
Brian Gaeke63438cc2003-12-11 00:22:59 +000080GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
81 ExecutionContext &SF) {
82 switch (CE->getOpcode()) {
83 case Instruction::Cast:
84 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
85 case Instruction::GetElementPtr:
86 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
87 gep_type_end(CE), SF);
88 case Instruction::Add:
89 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
90 getOperandValue(CE->getOperand(1), SF),
91 CE->getOperand(0)->getType());
92 case Instruction::Sub:
93 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
94 getOperandValue(CE->getOperand(1), SF),
95 CE->getOperand(0)->getType());
96 case Instruction::Mul:
97 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
98 getOperandValue(CE->getOperand(1), SF),
99 CE->getOperand(0)->getType());
Reid Spencer1628cec2006-10-26 06:15:43 +0000100 case Instruction::SDiv:
101 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
102 getOperandValue(CE->getOperand(1), SF),
103 CE->getOperand(0)->getType());
104 case Instruction::UDiv:
105 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
106 getOperandValue(CE->getOperand(1), SF),
107 CE->getOperand(0)->getType());
108 case Instruction::FDiv:
109 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
110 getOperandValue(CE->getOperand(1), SF),
111 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000112 case Instruction::URem:
113 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
Brian Gaeke63438cc2003-12-11 00:22:59 +0000114 getOperandValue(CE->getOperand(1), SF),
115 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000116 case Instruction::SRem:
117 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
118 getOperandValue(CE->getOperand(1), SF),
119 CE->getOperand(0)->getType());
120 case Instruction::FRem:
121 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
122 getOperandValue(CE->getOperand(1), SF),
123 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000124 case Instruction::And:
125 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
126 getOperandValue(CE->getOperand(1), SF),
127 CE->getOperand(0)->getType());
128 case Instruction::Or:
129 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
130 getOperandValue(CE->getOperand(1), SF),
131 CE->getOperand(0)->getType());
132 case Instruction::Xor:
133 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
134 getOperandValue(CE->getOperand(1), SF),
135 CE->getOperand(0)->getType());
136 case Instruction::SetEQ:
137 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
138 getOperandValue(CE->getOperand(1), SF),
139 CE->getOperand(0)->getType());
140 case Instruction::SetNE:
141 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
142 getOperandValue(CE->getOperand(1), SF),
143 CE->getOperand(0)->getType());
144 case Instruction::SetLE:
145 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
146 getOperandValue(CE->getOperand(1), SF),
147 CE->getOperand(0)->getType());
148 case Instruction::SetGE:
149 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
150 getOperandValue(CE->getOperand(1), SF),
151 CE->getOperand(0)->getType());
152 case Instruction::SetLT:
153 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
154 getOperandValue(CE->getOperand(1), SF),
155 CE->getOperand(0)->getType());
156 case Instruction::SetGT:
157 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
158 getOperandValue(CE->getOperand(1), SF),
159 CE->getOperand(0)->getType());
160 case Instruction::Shl:
161 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
162 getOperandValue(CE->getOperand(1), SF),
163 CE->getOperand(0)->getType());
164 case Instruction::Shr:
165 return executeShrInst(getOperandValue(CE->getOperand(0), SF),
166 getOperandValue(CE->getOperand(1), SF),
167 CE->getOperand(0)->getType());
Chris Lattner759d34f2004-04-20 16:43:21 +0000168 case Instruction::Select:
169 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
170 getOperandValue(CE->getOperand(1), SF),
171 getOperandValue(CE->getOperand(2), SF));
Brian Gaeke63438cc2003-12-11 00:22:59 +0000172 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000173 std::cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Brian Gaeke63438cc2003-12-11 00:22:59 +0000174 abort();
175 return GenericValue();
176 }
177}
Chris Lattnera34c5682002-08-27 22:33:45 +0000178
Brian Gaeke29794cb2003-09-05 18:55:03 +0000179GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000180 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000181 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000182 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000183 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000184 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000185 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000186 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000187 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000188 }
189}
190
Chris Lattner39bb5b42001-10-15 13:25:40 +0000191static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000192 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000193}
194
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000195void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000196 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000197}
198
Chris Lattner2adcd832002-05-03 19:52:30 +0000199//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000200// Binary Instruction Implementations
201//===----------------------------------------------------------------------===//
202
203#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
204 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
205
Misha Brukmand1c881a2005-04-21 22:43:08 +0000206static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
207 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000208 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000209 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000210 IMPLEMENT_BINARY_OPERATOR(+, UByte);
211 IMPLEMENT_BINARY_OPERATOR(+, SByte);
212 IMPLEMENT_BINARY_OPERATOR(+, UShort);
213 IMPLEMENT_BINARY_OPERATOR(+, Short);
214 IMPLEMENT_BINARY_OPERATOR(+, UInt);
215 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000216 IMPLEMENT_BINARY_OPERATOR(+, ULong);
217 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000218 IMPLEMENT_BINARY_OPERATOR(+, Float);
219 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000220 default:
Chris Lattner02868352003-04-22 21:22:33 +0000221 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
222 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000223 }
224 return Dest;
225}
226
Misha Brukmand1c881a2005-04-21 22:43:08 +0000227static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
228 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000229 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000230 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000231 IMPLEMENT_BINARY_OPERATOR(-, UByte);
232 IMPLEMENT_BINARY_OPERATOR(-, SByte);
233 IMPLEMENT_BINARY_OPERATOR(-, UShort);
234 IMPLEMENT_BINARY_OPERATOR(-, Short);
235 IMPLEMENT_BINARY_OPERATOR(-, UInt);
236 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000237 IMPLEMENT_BINARY_OPERATOR(-, ULong);
238 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000239 IMPLEMENT_BINARY_OPERATOR(-, Float);
240 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000241 default:
Chris Lattner02868352003-04-22 21:22:33 +0000242 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
243 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000244 }
245 return Dest;
246}
247
Misha Brukmand1c881a2005-04-21 22:43:08 +0000248static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
249 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000250 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000251 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000252 IMPLEMENT_BINARY_OPERATOR(*, UByte);
253 IMPLEMENT_BINARY_OPERATOR(*, SByte);
254 IMPLEMENT_BINARY_OPERATOR(*, UShort);
255 IMPLEMENT_BINARY_OPERATOR(*, Short);
256 IMPLEMENT_BINARY_OPERATOR(*, UInt);
257 IMPLEMENT_BINARY_OPERATOR(*, Int);
258 IMPLEMENT_BINARY_OPERATOR(*, ULong);
259 IMPLEMENT_BINARY_OPERATOR(*, Long);
260 IMPLEMENT_BINARY_OPERATOR(*, Float);
261 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000262 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000263 std::cout << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000264 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000265 }
266 return Dest;
267}
268
Reid Spencerfe855262006-11-01 03:41:05 +0000269#define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
270 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
271
Reid Spencer1628cec2006-10-26 06:15:43 +0000272static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
273 const Type *Ty) {
274 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000275 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000276 IMPLEMENT_SIGNLESS_BINOP(/, UByte, SByte);
277 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
278 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
279 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
Reid Spencer1628cec2006-10-26 06:15:43 +0000280 default:
281 std::cout << "Unhandled type for UDiv instruction: " << *Ty << "\n";
282 abort();
283 }
284 return Dest;
285}
286
287static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
288 const Type *Ty) {
289 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000290 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000291 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
292 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
293 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
294 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
Reid Spencer1628cec2006-10-26 06:15:43 +0000295 default:
296 std::cout << "Unhandled type for SDiv instruction: " << *Ty << "\n";
297 abort();
298 }
299 return Dest;
300}
301
302static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000303 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000304 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000305 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000306 IMPLEMENT_BINARY_OPERATOR(/, Float);
307 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000308 default:
Chris Lattner02868352003-04-22 21:22:33 +0000309 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
310 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000311 }
312 return Dest;
313}
314
Reid Spencer0a783f72006-11-02 01:53:59 +0000315static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000316 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000317 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000318 switch (Ty->getTypeID()) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000319 IMPLEMENT_SIGNLESS_BINOP(%, UByte, SByte);
320 IMPLEMENT_SIGNLESS_BINOP(%, UShort, Short);
321 IMPLEMENT_SIGNLESS_BINOP(%, UInt, Int);
322 IMPLEMENT_SIGNLESS_BINOP(%, ULong, Long);
323 default:
324 std::cout << "Unhandled type for URem instruction: " << *Ty << "\n";
325 abort();
326 }
327 return Dest;
328}
329
330static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
331 const Type *Ty) {
332 GenericValue Dest;
333 switch (Ty->getTypeID()) {
334 IMPLEMENT_SIGNLESS_BINOP(%, SByte, UByte);
335 IMPLEMENT_SIGNLESS_BINOP(%, Short, UShort);
336 IMPLEMENT_SIGNLESS_BINOP(%, Int, UInt);
337 IMPLEMENT_SIGNLESS_BINOP(%, Long, ULong);
338 default:
339 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
340 abort();
341 }
342 return Dest;
343}
344
345static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
346 const Type *Ty) {
347 GenericValue Dest;
348 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000349 case Type::FloatTyID:
350 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
351 break;
352 case Type::DoubleTyID:
353 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
354 break;
355 default:
Chris Lattner02868352003-04-22 21:22:33 +0000356 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
357 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000358 }
359 return Dest;
360}
361
Misha Brukmand1c881a2005-04-21 22:43:08 +0000362static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
363 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000364 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000365 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000366 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000367 IMPLEMENT_BINARY_OPERATOR(&, UByte);
368 IMPLEMENT_BINARY_OPERATOR(&, SByte);
369 IMPLEMENT_BINARY_OPERATOR(&, UShort);
370 IMPLEMENT_BINARY_OPERATOR(&, Short);
371 IMPLEMENT_BINARY_OPERATOR(&, UInt);
372 IMPLEMENT_BINARY_OPERATOR(&, Int);
373 IMPLEMENT_BINARY_OPERATOR(&, ULong);
374 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000375 default:
Chris Lattner02868352003-04-22 21:22:33 +0000376 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
377 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000378 }
379 return Dest;
380}
381
Misha Brukmand1c881a2005-04-21 22:43:08 +0000382static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000383 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000384 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000385 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000386 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000387 IMPLEMENT_BINARY_OPERATOR(|, UByte);
388 IMPLEMENT_BINARY_OPERATOR(|, SByte);
389 IMPLEMENT_BINARY_OPERATOR(|, UShort);
390 IMPLEMENT_BINARY_OPERATOR(|, Short);
391 IMPLEMENT_BINARY_OPERATOR(|, UInt);
392 IMPLEMENT_BINARY_OPERATOR(|, Int);
393 IMPLEMENT_BINARY_OPERATOR(|, ULong);
394 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000395 default:
Chris Lattner02868352003-04-22 21:22:33 +0000396 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
397 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000398 }
399 return Dest;
400}
401
Misha Brukmand1c881a2005-04-21 22:43:08 +0000402static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000403 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000404 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000405 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000406 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000407 IMPLEMENT_BINARY_OPERATOR(^, UByte);
408 IMPLEMENT_BINARY_OPERATOR(^, SByte);
409 IMPLEMENT_BINARY_OPERATOR(^, UShort);
410 IMPLEMENT_BINARY_OPERATOR(^, Short);
411 IMPLEMENT_BINARY_OPERATOR(^, UInt);
412 IMPLEMENT_BINARY_OPERATOR(^, Int);
413 IMPLEMENT_BINARY_OPERATOR(^, ULong);
414 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000415 default:
Chris Lattner02868352003-04-22 21:22:33 +0000416 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
417 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000418 }
419 return Dest;
420}
421
Chris Lattner92101ac2001-08-23 17:05:04 +0000422#define IMPLEMENT_SETCC(OP, TY) \
423 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
424
Chris Lattnerfd506f52003-04-23 19:55:35 +0000425// Handle pointers specially because they must be compared with only as much
426// width as the host has. We _do not_ want to be comparing 64 bit values when
427// running on a 32-bit target, otherwise the upper 32 bits might mess up
428// comparisons if they contain garbage.
429#define IMPLEMENT_POINTERSETCC(OP) \
430 case Type::PointerTyID: \
431 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
432 (void*)(intptr_t)Src2.PointerVal; break
433
Misha Brukmand1c881a2005-04-21 22:43:08 +0000434static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
435 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000436 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000437 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000438 IMPLEMENT_SETCC(==, UByte);
439 IMPLEMENT_SETCC(==, SByte);
440 IMPLEMENT_SETCC(==, UShort);
441 IMPLEMENT_SETCC(==, Short);
442 IMPLEMENT_SETCC(==, UInt);
443 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000444 IMPLEMENT_SETCC(==, ULong);
445 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000446 IMPLEMENT_SETCC(==, Float);
447 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000448 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000449 default:
Chris Lattner02868352003-04-22 21:22:33 +0000450 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
451 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000452 }
453 return Dest;
454}
455
Misha Brukmand1c881a2005-04-21 22:43:08 +0000456static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
457 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000458 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000459 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000460 IMPLEMENT_SETCC(!=, UByte);
461 IMPLEMENT_SETCC(!=, SByte);
462 IMPLEMENT_SETCC(!=, UShort);
463 IMPLEMENT_SETCC(!=, Short);
464 IMPLEMENT_SETCC(!=, UInt);
465 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000466 IMPLEMENT_SETCC(!=, ULong);
467 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000468 IMPLEMENT_SETCC(!=, Float);
469 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000470 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000471
Chris Lattner92101ac2001-08-23 17:05:04 +0000472 default:
Chris Lattner02868352003-04-22 21:22:33 +0000473 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
474 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000475 }
476 return Dest;
477}
478
Misha Brukmand1c881a2005-04-21 22:43:08 +0000479static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
480 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000481 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000482 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000483 IMPLEMENT_SETCC(<=, UByte);
484 IMPLEMENT_SETCC(<=, SByte);
485 IMPLEMENT_SETCC(<=, UShort);
486 IMPLEMENT_SETCC(<=, Short);
487 IMPLEMENT_SETCC(<=, UInt);
488 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000489 IMPLEMENT_SETCC(<=, ULong);
490 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000491 IMPLEMENT_SETCC(<=, Float);
492 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000493 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000494 default:
Chris Lattnered6c0732004-07-15 02:51:32 +0000495 std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000496 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000497 }
498 return Dest;
499}
500
Misha Brukmand1c881a2005-04-21 22:43:08 +0000501static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
502 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000503 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000504 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000505 IMPLEMENT_SETCC(>=, UByte);
506 IMPLEMENT_SETCC(>=, SByte);
507 IMPLEMENT_SETCC(>=, UShort);
508 IMPLEMENT_SETCC(>=, Short);
509 IMPLEMENT_SETCC(>=, UInt);
510 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000511 IMPLEMENT_SETCC(>=, ULong);
512 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000513 IMPLEMENT_SETCC(>=, Float);
514 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000515 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000516 default:
Chris Lattner02868352003-04-22 21:22:33 +0000517 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
518 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000519 }
520 return Dest;
521}
522
Misha Brukmand1c881a2005-04-21 22:43:08 +0000523static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
524 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000525 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000526 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000527 IMPLEMENT_SETCC(<, UByte);
528 IMPLEMENT_SETCC(<, SByte);
529 IMPLEMENT_SETCC(<, UShort);
530 IMPLEMENT_SETCC(<, Short);
531 IMPLEMENT_SETCC(<, UInt);
532 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000533 IMPLEMENT_SETCC(<, ULong);
534 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000535 IMPLEMENT_SETCC(<, Float);
536 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000537 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000538 default:
Chris Lattner02868352003-04-22 21:22:33 +0000539 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
540 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000541 }
542 return Dest;
543}
544
Misha Brukmand1c881a2005-04-21 22:43:08 +0000545static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
546 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000547 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000548 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000549 IMPLEMENT_SETCC(>, UByte);
550 IMPLEMENT_SETCC(>, SByte);
551 IMPLEMENT_SETCC(>, UShort);
552 IMPLEMENT_SETCC(>, Short);
553 IMPLEMENT_SETCC(>, UInt);
554 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000555 IMPLEMENT_SETCC(>, ULong);
556 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000557 IMPLEMENT_SETCC(>, Float);
558 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000559 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000560 default:
Chris Lattner02868352003-04-22 21:22:33 +0000561 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
562 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000563 }
564 return Dest;
565}
566
Chris Lattnerd7916e92003-05-10 21:22:39 +0000567void Interpreter::visitBinaryOperator(BinaryOperator &I) {
568 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000569 const Type *Ty = I.getOperand(0)->getType();
570 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
571 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000572 GenericValue R; // Result
573
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000574 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000575 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
576 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
577 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000578 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
579 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
580 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
Reid Spencer0a783f72006-11-02 01:53:59 +0000581 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
582 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
583 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000584 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
585 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
586 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
587 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
588 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
589 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
590 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
591 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
592 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000593 default:
Chris Lattner02868352003-04-22 21:22:33 +0000594 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
595 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000596 }
597
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000598 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000599}
600
Misha Brukmand1c881a2005-04-21 22:43:08 +0000601static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000602 GenericValue Src3) {
603 return Src1.BoolVal ? Src2 : Src3;
604}
605
606void Interpreter::visitSelectInst(SelectInst &I) {
607 ExecutionContext &SF = ECStack.back();
608 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
609 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
610 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
611 GenericValue R = executeSelectInst(Src1, Src2, Src3);
612 SetValue(&I, R, SF);
613}
614
615
Chris Lattner92101ac2001-08-23 17:05:04 +0000616//===----------------------------------------------------------------------===//
617// Terminator Instruction Implementations
618//===----------------------------------------------------------------------===//
619
Chris Lattnere43db882001-10-27 04:15:57 +0000620void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000621 // runAtExitHandlers() assumes there are no stack frames, but
622 // if exit() was called, then it had a stack frame. Blow away
623 // the stack before interpreting atexit handlers.
624 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000625 runAtExitHandlers ();
626 exit (GV.IntVal);
Chris Lattnere43db882001-10-27 04:15:57 +0000627}
628
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000629/// Pop the last stack frame off of ECStack and then copy the result
630/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000631/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000632/// CallInst if there was a previous stack frame. This method may
633/// invalidate any ECStack iterators you have. This method also takes
634/// care of switching to the normal destination BB, if we are returning
635/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000636///
637void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
638 GenericValue Result) {
639 // Pop the current stack frame.
640 ECStack.pop_back();
641
Misha Brukmand1c881a2005-04-21 22:43:08 +0000642 if (ECStack.empty()) { // Finished main. Put result into exit code...
643 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000644 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000645 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000646 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000647 }
648 } else {
649 // If we have a previous stack frame, and we have a previous call,
650 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000651 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000652 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000653 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000654 SetValue(I, Result, CallingSF);
655 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
656 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000657 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000658 }
659 }
660}
661
Chris Lattnerd7916e92003-05-10 21:22:39 +0000662void Interpreter::visitReturnInst(ReturnInst &I) {
663 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000664 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000665 GenericValue Result;
666
667 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000668 if (I.getNumOperands()) {
669 RetTy = I.getReturnValue()->getType();
670 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000671 }
672
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000673 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000674}
675
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000676void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000677 // Unwind stack
678 Instruction *Inst;
679 do {
680 ECStack.pop_back ();
681 if (ECStack.empty ())
682 abort ();
683 Inst = ECStack.back ().Caller.getInstruction ();
684 } while (!(Inst && isa<InvokeInst> (Inst)));
685
686 // Return from invoke
687 ExecutionContext &InvokingSF = ECStack.back ();
688 InvokingSF.Caller = CallSite ();
689
690 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000691 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000692}
693
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000694void Interpreter::visitUnreachableInst(UnreachableInst &I) {
695 std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
696 abort();
697}
698
Chris Lattnerd7916e92003-05-10 21:22:39 +0000699void Interpreter::visitBranchInst(BranchInst &I) {
700 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000701 BasicBlock *Dest;
702
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000703 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
704 if (!I.isUnconditional()) {
705 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000706 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000707 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000708 }
Chris Lattner77113b62003-05-10 20:21:16 +0000709 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000710}
711
Chris Lattnerd7916e92003-05-10 21:22:39 +0000712void Interpreter::visitSwitchInst(SwitchInst &I) {
713 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000714 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
715 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000716
717 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000718 BasicBlock *Dest = 0;
719 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000720 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000721 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000722 Dest = cast<BasicBlock>(I.getOperand(i+1));
723 break;
724 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000725
Chris Lattner09e93922003-04-22 20:34:47 +0000726 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000727 SwitchToNewBasicBlock(Dest, SF);
728}
729
730// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
731// This function handles the actual updating of block and instruction iterators
732// as well as execution of all of the PHI nodes in the destination block.
733//
734// This method does this because all of the PHI nodes must be executed
735// atomically, reading their inputs before any of the results are updated. Not
736// doing this can cause problems if the PHI nodes depend on other PHI nodes for
737// their inputs. If the input PHI node is updated before it is read, incorrect
738// results can happen. Thus we use a two phase approach.
739//
740void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
741 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
742 SF.CurBB = Dest; // Update CurBB to branch destination
743 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
744
745 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
746
747 // Loop over all of the PHI nodes in the current block, reading their inputs.
748 std::vector<GenericValue> ResultValues;
749
750 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
751 // Search for the value corresponding to this previous bb...
752 int i = PN->getBasicBlockIndex(PrevBB);
753 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
754 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000755
Chris Lattner77113b62003-05-10 20:21:16 +0000756 // Save the incoming value for this PHI node...
757 ResultValues.push_back(getOperandValue(IncomingValue, SF));
758 }
759
760 // Now loop over all of the PHI nodes setting their values...
761 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000762 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
763 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000764 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000765 }
Chris Lattner09e93922003-04-22 20:34:47 +0000766}
767
Chris Lattner92101ac2001-08-23 17:05:04 +0000768//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000769// Memory Instruction Implementations
770//===----------------------------------------------------------------------===//
771
Chris Lattnerd7916e92003-05-10 21:22:39 +0000772void Interpreter::visitAllocationInst(AllocationInst &I) {
773 ExecutionContext &SF = ECStack.back();
774
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000775 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000776
Chris Lattnercc82cc12002-04-28 21:57:33 +0000777 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000778 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000779
780 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +0000781 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000782
Chris Lattnerfe11a972002-12-23 23:59:41 +0000783 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000784 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000785 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000786
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000787 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000788 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000789}
790
Chris Lattnerd7916e92003-05-10 21:22:39 +0000791void Interpreter::visitFreeInst(FreeInst &I) {
792 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000793 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
794 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000795 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000796 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000797}
798
Chris Lattnera34c5682002-08-27 22:33:45 +0000799// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000800//
Chris Lattner4af6de82003-11-25 20:44:56 +0000801GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000802 gep_type_iterator E,
803 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000804 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000805 "Cannot getElementOffset of a nonpointer type!");
806
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000807 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000808
809 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000810 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000811 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000812
Reid Spencerb83eb642006-10-20 07:07:24 +0000813 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
814 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000815
Chris Lattnerd5644962005-01-08 20:05:34 +0000816 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000817 } else {
818 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000819 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000820 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
821
822 uint64_t Idx;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000823 switch (I.getOperand()->getType()->getTypeID()) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000824 default: assert(0 && "Illegal getelementptr index for sequential type!");
825 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
826 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
827 case Type::IntTyID: Idx = IdxGV.IntVal; break;
828 case Type::LongTyID: Idx = IdxGV.LongVal; break;
829 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
830 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
831 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
832 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
833 }
Chris Lattnerd5644962005-01-08 20:05:34 +0000834 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000835 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000836 }
837
Chris Lattnera34c5682002-08-27 22:33:45 +0000838 GenericValue Result;
839 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
840 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000841}
842
Chris Lattnerd7916e92003-05-10 21:22:39 +0000843void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
844 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000845 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000846 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000847}
848
Chris Lattnerd7916e92003-05-10 21:22:39 +0000849void Interpreter::visitLoadInst(LoadInst &I) {
850 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000851 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000852 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000853 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000854 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000855}
856
Chris Lattnerd7916e92003-05-10 21:22:39 +0000857void Interpreter::visitStoreInst(StoreInst &I) {
858 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000859 GenericValue Val = getOperandValue(I.getOperand(0), SF);
860 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000861 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000862 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000863}
864
Chris Lattner86660982001-08-27 05:16:50 +0000865//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000866// Miscellaneous Instruction Implementations
867//===----------------------------------------------------------------------===//
868
Brian Gaekefea483d2003-11-07 20:04:22 +0000869void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000870 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000871
872 // Check to see if this is an intrinsic function call...
873 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +0000874 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +0000875 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000876 case Intrinsic::not_intrinsic:
877 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000878 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000879 GenericValue ArgIndex;
880 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
881 ArgIndex.UIntPairVal.second = 0;
882 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000883 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000884 }
Chris Lattner317201d2004-03-13 00:24:00 +0000885 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000886 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000887 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000888 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
889 return;
890 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000891 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000892 // class to transform it into hopefully tasty LLVM code.
893 //
894 Instruction *Prev = CS.getInstruction()->getPrev();
895 BasicBlock *Parent = CS.getInstruction()->getParent();
896 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
897
898 // Restore the CurInst pointer to the first instruction newly inserted, if
899 // any.
900 if (!Prev) {
901 SF.CurInst = Parent->begin();
902 } else {
903 SF.CurInst = Prev;
904 ++SF.CurInst;
905 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000906 return;
Chris Lattner73011782003-12-28 09:44:37 +0000907 }
908
Brian Gaekefea483d2003-11-07 20:04:22 +0000909 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000910 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000911 const unsigned NumArgs = SF.Caller.arg_size();
912 ArgVals.reserve(NumArgs);
913 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
914 e = SF.Caller.arg_end(); i != e; ++i) {
915 Value *V = *i;
916 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000917 // Promote all integral types whose size is < sizeof(int) into ints. We do
918 // this by zero or sign extending the value as appropriate according to the
919 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000920 const Type *Ty = V->getType();
921 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000922 if (Ty == Type::ShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000923 ArgVals.back().IntVal = ArgVals.back().ShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000924 else if (Ty == Type::UShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000925 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000926 else if (Ty == Type::SByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000927 ArgVals.back().IntVal = ArgVals.back().SByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000928 else if (Ty == Type::UByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000929 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000930 else if (Ty == Type::BoolTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000931 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
Chris Lattner93780132003-01-13 00:58:52 +0000932 else
Misha Brukmand1c881a2005-04-21 22:43:08 +0000933 assert(0 && "Unknown type!");
Chris Lattner93780132003-01-13 00:58:52 +0000934 }
935 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000936
Misha Brukmand1c881a2005-04-21 22:43:08 +0000937 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000938 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000939 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000940 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000941}
942
Chris Lattner86660982001-08-27 05:16:50 +0000943#define IMPLEMENT_SHIFT(OP, TY) \
944 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
945
Brian Gaeke63438cc2003-12-11 00:22:59 +0000946static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
947 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000948 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000949 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000950 IMPLEMENT_SHIFT(<<, UByte);
951 IMPLEMENT_SHIFT(<<, SByte);
952 IMPLEMENT_SHIFT(<<, UShort);
953 IMPLEMENT_SHIFT(<<, Short);
954 IMPLEMENT_SHIFT(<<, UInt);
955 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000956 IMPLEMENT_SHIFT(<<, ULong);
957 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000958 default:
Chris Lattner02868352003-04-22 21:22:33 +0000959 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000960 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000961 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000962}
963
Brian Gaeke63438cc2003-12-11 00:22:59 +0000964static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
965 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000966 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000967 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000968 IMPLEMENT_SHIFT(>>, UByte);
969 IMPLEMENT_SHIFT(>>, SByte);
970 IMPLEMENT_SHIFT(>>, UShort);
971 IMPLEMENT_SHIFT(>>, Short);
972 IMPLEMENT_SHIFT(>>, UInt);
973 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000974 IMPLEMENT_SHIFT(>>, ULong);
975 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000976 default:
Chris Lattner02868352003-04-22 21:22:33 +0000977 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
978 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000979 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000980 return Dest;
981}
982
983void Interpreter::visitShl(ShiftInst &I) {
984 ExecutionContext &SF = ECStack.back();
985 const Type *Ty = I.getOperand(0)->getType();
986 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
987 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
988 GenericValue Dest;
989 Dest = executeShlInst (Src1, Src2, Ty);
990 SetValue(&I, Dest, SF);
991}
992
993void Interpreter::visitShr(ShiftInst &I) {
994 ExecutionContext &SF = ECStack.back();
995 const Type *Ty = I.getOperand(0)->getType();
996 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
997 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
998 GenericValue Dest;
999 Dest = executeShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001000 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001001}
1002
1003#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001004 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +00001005
1006#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
1007 case Type::DESTTY##TyID: \
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001008 switch (SrcTy->getTypeID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +00001009 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +00001010 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
1011 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
1012 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1013 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1014 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +00001015 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1016 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +00001017 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
1018 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +00001019
1020#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
1021 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
1022 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
1023
1024#define IMPLEMENT_CAST_CASE_END() \
Chris Lattnered6c0732004-07-15 02:51:32 +00001025 default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
Chris Lattner02868352003-04-22 21:22:33 +00001026 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +00001027 } \
1028 break
1029
1030#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
1031 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1032 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +00001033 IMPLEMENT_CAST_CASE_END()
1034
Brian Gaeke29794cb2003-09-05 18:55:03 +00001035GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001036 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001037 const Type *SrcTy = SrcVal->getType();
1038 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001039
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001040 switch (Ty->getTypeID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001041 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1042 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1043 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +00001044 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001045 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1046 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1047 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1048 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +00001049 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001050 IMPLEMENT_CAST_CASE(Float , (float));
1051 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +00001052 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +00001053 default:
Chris Lattner02868352003-04-22 21:22:33 +00001054 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +00001055 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001056 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001057
1058 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001059}
Chris Lattner92101ac2001-08-23 17:05:04 +00001060
Chris Lattnerd7916e92003-05-10 21:22:39 +00001061void Interpreter::visitCastInst(CastInst &I) {
1062 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +00001063 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1064}
Chris Lattner92101ac2001-08-23 17:05:04 +00001065
Brian Gaekec1a2be12003-11-07 21:20:47 +00001066#define IMPLEMENT_VAARG(TY) \
1067 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1068
1069void Interpreter::visitVAArgInst(VAArgInst &I) {
1070 ExecutionContext &SF = ECStack.back();
1071
Brian Gaeke9d20b712004-02-25 23:01:48 +00001072 // Get the incoming valist parameter. LLI treats the valist as a
1073 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001074 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001075 GenericValue Dest;
1076 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001077 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001078 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001079 switch (Ty->getTypeID()) {
Brian Gaekec1a2be12003-11-07 21:20:47 +00001080 IMPLEMENT_VAARG(UByte);
1081 IMPLEMENT_VAARG(SByte);
1082 IMPLEMENT_VAARG(UShort);
1083 IMPLEMENT_VAARG(Short);
1084 IMPLEMENT_VAARG(UInt);
1085 IMPLEMENT_VAARG(Int);
1086 IMPLEMENT_VAARG(ULong);
1087 IMPLEMENT_VAARG(Long);
1088 IMPLEMENT_VAARG(Pointer);
1089 IMPLEMENT_VAARG(Float);
1090 IMPLEMENT_VAARG(Double);
1091 IMPLEMENT_VAARG(Bool);
1092 default:
1093 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1094 abort();
1095 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001096
Brian Gaekec1a2be12003-11-07 21:20:47 +00001097 // Set the Value of this Instruction.
1098 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001099
1100 // Move the pointer to the next vararg.
1101 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001102}
1103
Chris Lattner92101ac2001-08-23 17:05:04 +00001104//===----------------------------------------------------------------------===//
1105// Dispatch and Execution Code
1106//===----------------------------------------------------------------------===//
1107
Chris Lattner92101ac2001-08-23 17:05:04 +00001108//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001109// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001110//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001111void Interpreter::callFunction(Function *F,
1112 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001113 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1114 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1115 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001116 // Make a new stack frame... and fill it in.
1117 ECStack.push_back(ExecutionContext());
1118 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001119 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001120
1121 // Special handling for external functions.
1122 if (F->isExternal()) {
1123 GenericValue Result = callExternalFunction (F, ArgVals);
1124 // Simulate a 'ret' instruction of the appropriate type.
1125 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1126 return;
1127 }
1128
1129 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001130 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001131 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001132
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001133 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001134 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001135 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001136 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001137
1138 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001139 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001140 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001141 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001142
1143 // Handle varargs arguments...
1144 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001145}
1146
Chris Lattner92101ac2001-08-23 17:05:04 +00001147void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001148 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001149 // Interpret a single instruction & increment the "PC".
1150 ExecutionContext &SF = ECStack.back(); // Current stack frame
1151 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001152
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001153 // Track the number of dynamic instructions executed.
1154 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001155
Brian Gaeke63438cc2003-12-11 00:22:59 +00001156 DEBUG(std::cerr << "About to interpret: " << I);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001157 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001158 }
1159}