blob: 346b6159524730d58bdf3c1886d5071700fb574a [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"
Jeff Cohen97af7512006-12-02 02:22:01 +000023#include <cmath>
Chris Lattner4af6de82003-11-25 20:44:56 +000024using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000025
Chris Lattnercecf56b2006-12-19 22:56:53 +000026STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
27static Interpreter *TheEE = 0;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner73011782003-12-28 09:44:37 +000029
Chris Lattner2e42d3a2001-10-15 05:51:48 +000030//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000031// Value Manipulation code
32//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000033
Misha Brukmand1c881a2005-04-21 22:43:08 +000034static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
35 const Type *Ty);
36static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
37 const Type *Ty);
38static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
39 const Type *Ty);
Reid Spencer1628cec2006-10-26 06:15:43 +000040static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
41 const Type *Ty);
42static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
43 const Type *Ty);
44static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
45 const Type *Ty);
Reid Spencer0a783f72006-11-02 01:53:59 +000046static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
47 const Type *Ty);
48static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
49 const Type *Ty);
50static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
51 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000052static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
53 const Type *Ty);
54static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
55 const Type *Ty);
56static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
57 const Type *Ty);
58static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
59 const Type *Ty);
60static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
61 const Type *Ty);
62static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
63 const Type *Ty);
64static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
65 const Type *Ty);
66static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
67 const Type *Ty);
68static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
69 const Type *Ty);
70static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
71 const Type *Ty);
Reid Spencer3822ff52006-11-08 06:47:33 +000072static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
73 const Type *Ty);
74static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
75 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000076static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +000077 GenericValue Src3);
78
Brian Gaeke63438cc2003-12-11 00:22:59 +000079GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
80 ExecutionContext &SF) {
81 switch (CE->getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +000082 case Instruction::Trunc:
83 case Instruction::ZExt:
84 case Instruction::SExt:
85 case Instruction::FPTrunc:
86 case Instruction::FPExt:
87 case Instruction::UIToFP:
88 case Instruction::SIToFP:
89 case Instruction::FPToUI:
90 case Instruction::FPToSI:
91 case Instruction::PtrToInt:
92 case Instruction::IntToPtr:
93 case Instruction::BitCast:
94 return executeCastOperation(Instruction::CastOps(CE->getOpcode()),
95 CE->getOperand(0), CE->getType(), SF);
Brian Gaeke63438cc2003-12-11 00:22:59 +000096 case Instruction::GetElementPtr:
97 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
98 gep_type_end(CE), SF);
99 case Instruction::Add:
100 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
101 getOperandValue(CE->getOperand(1), SF),
102 CE->getOperand(0)->getType());
103 case Instruction::Sub:
104 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
105 getOperandValue(CE->getOperand(1), SF),
106 CE->getOperand(0)->getType());
107 case Instruction::Mul:
108 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
109 getOperandValue(CE->getOperand(1), SF),
110 CE->getOperand(0)->getType());
Reid Spencer1628cec2006-10-26 06:15:43 +0000111 case Instruction::SDiv:
112 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
113 getOperandValue(CE->getOperand(1), SF),
114 CE->getOperand(0)->getType());
115 case Instruction::UDiv:
116 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
117 getOperandValue(CE->getOperand(1), SF),
118 CE->getOperand(0)->getType());
119 case Instruction::FDiv:
120 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
121 getOperandValue(CE->getOperand(1), SF),
122 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000123 case Instruction::URem:
124 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
Brian Gaeke63438cc2003-12-11 00:22:59 +0000125 getOperandValue(CE->getOperand(1), SF),
126 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000127 case Instruction::SRem:
128 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
129 getOperandValue(CE->getOperand(1), SF),
130 CE->getOperand(0)->getType());
131 case Instruction::FRem:
132 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
133 getOperandValue(CE->getOperand(1), SF),
134 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000135 case Instruction::And:
136 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
137 getOperandValue(CE->getOperand(1), SF),
138 CE->getOperand(0)->getType());
139 case Instruction::Or:
140 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
141 getOperandValue(CE->getOperand(1), SF),
142 CE->getOperand(0)->getType());
143 case Instruction::Xor:
144 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
145 getOperandValue(CE->getOperand(1), SF),
146 CE->getOperand(0)->getType());
147 case Instruction::SetEQ:
148 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
149 getOperandValue(CE->getOperand(1), SF),
150 CE->getOperand(0)->getType());
151 case Instruction::SetNE:
152 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
153 getOperandValue(CE->getOperand(1), SF),
154 CE->getOperand(0)->getType());
155 case Instruction::SetLE:
156 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
157 getOperandValue(CE->getOperand(1), SF),
158 CE->getOperand(0)->getType());
159 case Instruction::SetGE:
160 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
161 getOperandValue(CE->getOperand(1), SF),
162 CE->getOperand(0)->getType());
163 case Instruction::SetLT:
164 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
165 getOperandValue(CE->getOperand(1), SF),
166 CE->getOperand(0)->getType());
167 case Instruction::SetGT:
168 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
169 getOperandValue(CE->getOperand(1), SF),
170 CE->getOperand(0)->getType());
171 case Instruction::Shl:
172 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
173 getOperandValue(CE->getOperand(1), SF),
174 CE->getOperand(0)->getType());
Reid Spencer3822ff52006-11-08 06:47:33 +0000175 case Instruction::LShr:
176 return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
177 getOperandValue(CE->getOperand(1), SF),
178 CE->getOperand(0)->getType());
179 case Instruction::AShr:
180 return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
181 getOperandValue(CE->getOperand(1), SF),
182 CE->getOperand(0)->getType());
Chris Lattner759d34f2004-04-20 16:43:21 +0000183 case Instruction::Select:
184 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
185 getOperandValue(CE->getOperand(1), SF),
186 getOperandValue(CE->getOperand(2), SF));
Brian Gaeke63438cc2003-12-11 00:22:59 +0000187 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000188 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Brian Gaeke63438cc2003-12-11 00:22:59 +0000189 abort();
190 return GenericValue();
191 }
192}
Chris Lattnera34c5682002-08-27 22:33:45 +0000193
Brian Gaeke29794cb2003-09-05 18:55:03 +0000194GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000195 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000196 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000197 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000198 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000199 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000200 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000201 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000202 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000203 }
204}
205
Chris Lattner39bb5b42001-10-15 13:25:40 +0000206static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000207 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000208}
209
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000210void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000211 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000212}
213
Chris Lattner2adcd832002-05-03 19:52:30 +0000214//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000215// Binary Instruction Implementations
216//===----------------------------------------------------------------------===//
217
218#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
219 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
220
Misha Brukmand1c881a2005-04-21 22:43:08 +0000221static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
222 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000223 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000224 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000225 IMPLEMENT_BINARY_OPERATOR(+, UByte);
226 IMPLEMENT_BINARY_OPERATOR(+, SByte);
227 IMPLEMENT_BINARY_OPERATOR(+, UShort);
228 IMPLEMENT_BINARY_OPERATOR(+, Short);
229 IMPLEMENT_BINARY_OPERATOR(+, UInt);
230 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000231 IMPLEMENT_BINARY_OPERATOR(+, ULong);
232 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000233 IMPLEMENT_BINARY_OPERATOR(+, Float);
234 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000235 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000236 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000237 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000238 }
239 return Dest;
240}
241
Misha Brukmand1c881a2005-04-21 22:43:08 +0000242static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
243 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000244 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000245 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000246 IMPLEMENT_BINARY_OPERATOR(-, UByte);
247 IMPLEMENT_BINARY_OPERATOR(-, SByte);
248 IMPLEMENT_BINARY_OPERATOR(-, UShort);
249 IMPLEMENT_BINARY_OPERATOR(-, Short);
250 IMPLEMENT_BINARY_OPERATOR(-, UInt);
251 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000252 IMPLEMENT_BINARY_OPERATOR(-, ULong);
253 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000254 IMPLEMENT_BINARY_OPERATOR(-, Float);
255 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000256 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000257 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000258 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000259 }
260 return Dest;
261}
262
Misha Brukmand1c881a2005-04-21 22:43:08 +0000263static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
264 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000265 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000266 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000267 IMPLEMENT_BINARY_OPERATOR(*, UByte);
268 IMPLEMENT_BINARY_OPERATOR(*, SByte);
269 IMPLEMENT_BINARY_OPERATOR(*, UShort);
270 IMPLEMENT_BINARY_OPERATOR(*, Short);
271 IMPLEMENT_BINARY_OPERATOR(*, UInt);
272 IMPLEMENT_BINARY_OPERATOR(*, Int);
273 IMPLEMENT_BINARY_OPERATOR(*, ULong);
274 IMPLEMENT_BINARY_OPERATOR(*, Long);
275 IMPLEMENT_BINARY_OPERATOR(*, Float);
276 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000277 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000278 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000279 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000280 }
281 return Dest;
282}
283
Reid Spencerfe855262006-11-01 03:41:05 +0000284#define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
285 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
286
Reid Spencer1628cec2006-10-26 06:15:43 +0000287static GenericValue executeUDivInst(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(/, UByte, SByte);
292 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
293 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
294 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
Reid Spencer1628cec2006-10-26 06:15:43 +0000295 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000296 cerr << "Unhandled type for UDiv instruction: " << *Ty << "\n";
Reid Spencer1628cec2006-10-26 06:15:43 +0000297 abort();
298 }
299 return Dest;
300}
301
302static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
303 const Type *Ty) {
304 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000305 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000306 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
307 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
308 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
309 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
Reid Spencer1628cec2006-10-26 06:15:43 +0000310 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000311 cerr << "Unhandled type for SDiv instruction: " << *Ty << "\n";
Reid Spencer1628cec2006-10-26 06:15:43 +0000312 abort();
313 }
314 return Dest;
315}
316
317static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000318 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000319 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000320 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000321 IMPLEMENT_BINARY_OPERATOR(/, Float);
322 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000323 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000324 cerr << "Unhandled type for Div instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000325 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000326 }
327 return Dest;
328}
329
Reid Spencer0a783f72006-11-02 01:53:59 +0000330static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000331 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000332 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000333 switch (Ty->getTypeID()) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000334 IMPLEMENT_SIGNLESS_BINOP(%, UByte, SByte);
335 IMPLEMENT_SIGNLESS_BINOP(%, UShort, Short);
336 IMPLEMENT_SIGNLESS_BINOP(%, UInt, Int);
337 IMPLEMENT_SIGNLESS_BINOP(%, ULong, Long);
338 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000339 cerr << "Unhandled type for URem instruction: " << *Ty << "\n";
Reid Spencer0a783f72006-11-02 01:53:59 +0000340 abort();
341 }
342 return Dest;
343}
344
345static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
346 const Type *Ty) {
347 GenericValue Dest;
348 switch (Ty->getTypeID()) {
349 IMPLEMENT_SIGNLESS_BINOP(%, SByte, UByte);
350 IMPLEMENT_SIGNLESS_BINOP(%, Short, UShort);
351 IMPLEMENT_SIGNLESS_BINOP(%, Int, UInt);
352 IMPLEMENT_SIGNLESS_BINOP(%, Long, ULong);
353 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000354 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Reid Spencer0a783f72006-11-02 01:53:59 +0000355 abort();
356 }
357 return Dest;
358}
359
360static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
361 const Type *Ty) {
362 GenericValue Dest;
363 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000364 case Type::FloatTyID:
365 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
366 break;
367 case Type::DoubleTyID:
368 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
369 break;
370 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000371 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000372 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000373 }
374 return Dest;
375}
376
Misha Brukmand1c881a2005-04-21 22:43:08 +0000377static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
378 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000379 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000380 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000381 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000382 IMPLEMENT_BINARY_OPERATOR(&, UByte);
383 IMPLEMENT_BINARY_OPERATOR(&, SByte);
384 IMPLEMENT_BINARY_OPERATOR(&, UShort);
385 IMPLEMENT_BINARY_OPERATOR(&, Short);
386 IMPLEMENT_BINARY_OPERATOR(&, UInt);
387 IMPLEMENT_BINARY_OPERATOR(&, Int);
388 IMPLEMENT_BINARY_OPERATOR(&, ULong);
389 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000390 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000391 cerr << "Unhandled type for And instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000392 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000393 }
394 return Dest;
395}
396
Misha Brukmand1c881a2005-04-21 22:43:08 +0000397static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000398 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000399 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000400 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000401 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000402 IMPLEMENT_BINARY_OPERATOR(|, UByte);
403 IMPLEMENT_BINARY_OPERATOR(|, SByte);
404 IMPLEMENT_BINARY_OPERATOR(|, UShort);
405 IMPLEMENT_BINARY_OPERATOR(|, Short);
406 IMPLEMENT_BINARY_OPERATOR(|, UInt);
407 IMPLEMENT_BINARY_OPERATOR(|, Int);
408 IMPLEMENT_BINARY_OPERATOR(|, ULong);
409 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000410 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000411 cerr << "Unhandled type for Or instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000412 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000413 }
414 return Dest;
415}
416
Misha Brukmand1c881a2005-04-21 22:43:08 +0000417static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000418 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000419 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000420 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000421 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000422 IMPLEMENT_BINARY_OPERATOR(^, UByte);
423 IMPLEMENT_BINARY_OPERATOR(^, SByte);
424 IMPLEMENT_BINARY_OPERATOR(^, UShort);
425 IMPLEMENT_BINARY_OPERATOR(^, Short);
426 IMPLEMENT_BINARY_OPERATOR(^, UInt);
427 IMPLEMENT_BINARY_OPERATOR(^, Int);
428 IMPLEMENT_BINARY_OPERATOR(^, ULong);
429 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000430 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000431 cerr << "Unhandled type for Xor instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000432 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000433 }
434 return Dest;
435}
436
Chris Lattner92101ac2001-08-23 17:05:04 +0000437#define IMPLEMENT_SETCC(OP, TY) \
438 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
439
Chris Lattnerfd506f52003-04-23 19:55:35 +0000440// Handle pointers specially because they must be compared with only as much
441// width as the host has. We _do not_ want to be comparing 64 bit values when
442// running on a 32-bit target, otherwise the upper 32 bits might mess up
443// comparisons if they contain garbage.
444#define IMPLEMENT_POINTERSETCC(OP) \
445 case Type::PointerTyID: \
446 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
447 (void*)(intptr_t)Src2.PointerVal; break
448
Misha Brukmand1c881a2005-04-21 22:43:08 +0000449static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
450 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000451 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000452 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000453 IMPLEMENT_SETCC(==, UByte);
454 IMPLEMENT_SETCC(==, SByte);
455 IMPLEMENT_SETCC(==, UShort);
456 IMPLEMENT_SETCC(==, Short);
457 IMPLEMENT_SETCC(==, UInt);
458 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000459 IMPLEMENT_SETCC(==, ULong);
460 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000461 IMPLEMENT_SETCC(==, Float);
462 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000463 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000464 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000465 cerr << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000466 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000467 }
468 return Dest;
469}
470
Misha Brukmand1c881a2005-04-21 22:43:08 +0000471static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
472 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000473 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000474 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000475 IMPLEMENT_SETCC(!=, UByte);
476 IMPLEMENT_SETCC(!=, SByte);
477 IMPLEMENT_SETCC(!=, UShort);
478 IMPLEMENT_SETCC(!=, Short);
479 IMPLEMENT_SETCC(!=, UInt);
480 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000481 IMPLEMENT_SETCC(!=, ULong);
482 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000483 IMPLEMENT_SETCC(!=, Float);
484 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000485 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000486
Chris Lattner92101ac2001-08-23 17:05:04 +0000487 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000488 cerr << "Unhandled type for SetNE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000489 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000490 }
491 return Dest;
492}
493
Misha Brukmand1c881a2005-04-21 22:43:08 +0000494static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
495 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000496 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000497 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000498 IMPLEMENT_SETCC(<=, UByte);
499 IMPLEMENT_SETCC(<=, SByte);
500 IMPLEMENT_SETCC(<=, UShort);
501 IMPLEMENT_SETCC(<=, Short);
502 IMPLEMENT_SETCC(<=, UInt);
503 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000504 IMPLEMENT_SETCC(<=, ULong);
505 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000506 IMPLEMENT_SETCC(<=, Float);
507 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000508 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000509 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000510 cerr << "Unhandled type for SetLE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000511 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000512 }
513 return Dest;
514}
515
Misha Brukmand1c881a2005-04-21 22:43:08 +0000516static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
517 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000518 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000519 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000520 IMPLEMENT_SETCC(>=, UByte);
521 IMPLEMENT_SETCC(>=, SByte);
522 IMPLEMENT_SETCC(>=, UShort);
523 IMPLEMENT_SETCC(>=, Short);
524 IMPLEMENT_SETCC(>=, UInt);
525 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000526 IMPLEMENT_SETCC(>=, ULong);
527 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000528 IMPLEMENT_SETCC(>=, Float);
529 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000530 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000531 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000532 cerr << "Unhandled type for SetGE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000533 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000534 }
535 return Dest;
536}
537
Misha Brukmand1c881a2005-04-21 22:43:08 +0000538static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
539 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000540 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000541 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000542 IMPLEMENT_SETCC(<, UByte);
543 IMPLEMENT_SETCC(<, SByte);
544 IMPLEMENT_SETCC(<, UShort);
545 IMPLEMENT_SETCC(<, Short);
546 IMPLEMENT_SETCC(<, UInt);
547 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000548 IMPLEMENT_SETCC(<, ULong);
549 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000550 IMPLEMENT_SETCC(<, Float);
551 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000552 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000553 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000554 cerr << "Unhandled type for SetLT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000555 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000556 }
557 return Dest;
558}
559
Misha Brukmand1c881a2005-04-21 22:43:08 +0000560static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
561 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000562 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000563 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000564 IMPLEMENT_SETCC(>, UByte);
565 IMPLEMENT_SETCC(>, SByte);
566 IMPLEMENT_SETCC(>, UShort);
567 IMPLEMENT_SETCC(>, Short);
568 IMPLEMENT_SETCC(>, UInt);
569 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000570 IMPLEMENT_SETCC(>, ULong);
571 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000572 IMPLEMENT_SETCC(>, Float);
573 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000574 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000575 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000576 cerr << "Unhandled type for SetGT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000577 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000578 }
579 return Dest;
580}
581
Chris Lattnerd7916e92003-05-10 21:22:39 +0000582void Interpreter::visitBinaryOperator(BinaryOperator &I) {
583 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000584 const Type *Ty = I.getOperand(0)->getType();
585 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
586 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000587 GenericValue R; // Result
588
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000589 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000590 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
591 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
592 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000593 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
594 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
595 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
Reid Spencer0a783f72006-11-02 01:53:59 +0000596 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
597 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
598 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000599 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
600 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
601 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
602 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
603 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
604 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
605 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
606 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
607 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000608 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000609 cerr << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner02868352003-04-22 21:22:33 +0000610 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000611 }
612
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000613 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000614}
615
Misha Brukmand1c881a2005-04-21 22:43:08 +0000616static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000617 GenericValue Src3) {
618 return Src1.BoolVal ? Src2 : Src3;
619}
620
621void Interpreter::visitSelectInst(SelectInst &I) {
622 ExecutionContext &SF = ECStack.back();
623 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
624 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
625 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
626 GenericValue R = executeSelectInst(Src1, Src2, Src3);
627 SetValue(&I, R, SF);
628}
629
630
Chris Lattner92101ac2001-08-23 17:05:04 +0000631//===----------------------------------------------------------------------===//
632// Terminator Instruction Implementations
633//===----------------------------------------------------------------------===//
634
Chris Lattnere43db882001-10-27 04:15:57 +0000635void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000636 // runAtExitHandlers() assumes there are no stack frames, but
637 // if exit() was called, then it had a stack frame. Blow away
638 // the stack before interpreting atexit handlers.
639 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000640 runAtExitHandlers ();
641 exit (GV.IntVal);
Chris Lattnere43db882001-10-27 04:15:57 +0000642}
643
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000644/// Pop the last stack frame off of ECStack and then copy the result
645/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000646/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000647/// CallInst if there was a previous stack frame. This method may
648/// invalidate any ECStack iterators you have. This method also takes
649/// care of switching to the normal destination BB, if we are returning
650/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000651///
652void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
653 GenericValue Result) {
654 // Pop the current stack frame.
655 ECStack.pop_back();
656
Misha Brukmand1c881a2005-04-21 22:43:08 +0000657 if (ECStack.empty()) { // Finished main. Put result into exit code...
658 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000659 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000660 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000661 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000662 }
663 } else {
664 // If we have a previous stack frame, and we have a previous call,
665 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000666 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000667 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000668 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000669 SetValue(I, Result, CallingSF);
670 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
671 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000672 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000673 }
674 }
675}
676
Chris Lattnerd7916e92003-05-10 21:22:39 +0000677void Interpreter::visitReturnInst(ReturnInst &I) {
678 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000679 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000680 GenericValue Result;
681
682 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000683 if (I.getNumOperands()) {
684 RetTy = I.getReturnValue()->getType();
685 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000686 }
687
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000688 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000689}
690
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000691void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000692 // Unwind stack
693 Instruction *Inst;
694 do {
695 ECStack.pop_back ();
696 if (ECStack.empty ())
697 abort ();
698 Inst = ECStack.back ().Caller.getInstruction ();
699 } while (!(Inst && isa<InvokeInst> (Inst)));
700
701 // Return from invoke
702 ExecutionContext &InvokingSF = ECStack.back ();
703 InvokingSF.Caller = CallSite ();
704
705 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000706 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000707}
708
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000709void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000710 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000711 abort();
712}
713
Chris Lattnerd7916e92003-05-10 21:22:39 +0000714void Interpreter::visitBranchInst(BranchInst &I) {
715 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000716 BasicBlock *Dest;
717
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000718 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
719 if (!I.isUnconditional()) {
720 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000721 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000722 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000723 }
Chris Lattner77113b62003-05-10 20:21:16 +0000724 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000725}
726
Chris Lattnerd7916e92003-05-10 21:22:39 +0000727void Interpreter::visitSwitchInst(SwitchInst &I) {
728 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000729 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
730 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000731
732 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000733 BasicBlock *Dest = 0;
734 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000735 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000736 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000737 Dest = cast<BasicBlock>(I.getOperand(i+1));
738 break;
739 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000740
Chris Lattner09e93922003-04-22 20:34:47 +0000741 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000742 SwitchToNewBasicBlock(Dest, SF);
743}
744
745// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
746// This function handles the actual updating of block and instruction iterators
747// as well as execution of all of the PHI nodes in the destination block.
748//
749// This method does this because all of the PHI nodes must be executed
750// atomically, reading their inputs before any of the results are updated. Not
751// doing this can cause problems if the PHI nodes depend on other PHI nodes for
752// their inputs. If the input PHI node is updated before it is read, incorrect
753// results can happen. Thus we use a two phase approach.
754//
755void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
756 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
757 SF.CurBB = Dest; // Update CurBB to branch destination
758 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
759
760 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
761
762 // Loop over all of the PHI nodes in the current block, reading their inputs.
763 std::vector<GenericValue> ResultValues;
764
765 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
766 // Search for the value corresponding to this previous bb...
767 int i = PN->getBasicBlockIndex(PrevBB);
768 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
769 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000770
Chris Lattner77113b62003-05-10 20:21:16 +0000771 // Save the incoming value for this PHI node...
772 ResultValues.push_back(getOperandValue(IncomingValue, SF));
773 }
774
775 // Now loop over all of the PHI nodes setting their values...
776 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000777 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
778 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000779 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000780 }
Chris Lattner09e93922003-04-22 20:34:47 +0000781}
782
Chris Lattner92101ac2001-08-23 17:05:04 +0000783//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000784// Memory Instruction Implementations
785//===----------------------------------------------------------------------===//
786
Chris Lattnerd7916e92003-05-10 21:22:39 +0000787void Interpreter::visitAllocationInst(AllocationInst &I) {
788 ExecutionContext &SF = ECStack.back();
789
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000790 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000791
Chris Lattnercc82cc12002-04-28 21:57:33 +0000792 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000793 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000794
795 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +0000796 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000797
Chris Lattnerfe11a972002-12-23 23:59:41 +0000798 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000799 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000800 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000801
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000802 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000803 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000804}
805
Chris Lattnerd7916e92003-05-10 21:22:39 +0000806void Interpreter::visitFreeInst(FreeInst &I) {
807 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000808 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
809 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000810 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000811 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000812}
813
Chris Lattnera34c5682002-08-27 22:33:45 +0000814// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000815//
Chris Lattner4af6de82003-11-25 20:44:56 +0000816GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000817 gep_type_iterator E,
818 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000819 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000820 "Cannot getElementOffset of a nonpointer type!");
821
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000822 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000823
824 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000825 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000826 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000827
Reid Spencerb83eb642006-10-20 07:07:24 +0000828 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
829 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000830
Chris Lattnerd5644962005-01-08 20:05:34 +0000831 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000832 } else {
833 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000834 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000835 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
836
837 uint64_t Idx;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000838 switch (I.getOperand()->getType()->getTypeID()) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000839 default: assert(0 && "Illegal getelementptr index for sequential type!");
840 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
841 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
842 case Type::IntTyID: Idx = IdxGV.IntVal; break;
843 case Type::LongTyID: Idx = IdxGV.LongVal; break;
844 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
845 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
846 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
847 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
848 }
Chris Lattnerd5644962005-01-08 20:05:34 +0000849 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000850 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000851 }
852
Chris Lattnera34c5682002-08-27 22:33:45 +0000853 GenericValue Result;
854 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
855 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000856}
857
Chris Lattnerd7916e92003-05-10 21:22:39 +0000858void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
859 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000860 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000861 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000862}
863
Chris Lattnerd7916e92003-05-10 21:22:39 +0000864void Interpreter::visitLoadInst(LoadInst &I) {
865 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000866 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000867 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000868 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000869 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000870}
871
Chris Lattnerd7916e92003-05-10 21:22:39 +0000872void Interpreter::visitStoreInst(StoreInst &I) {
873 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000874 GenericValue Val = getOperandValue(I.getOperand(0), SF);
875 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000876 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000877 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000878}
879
Chris Lattner86660982001-08-27 05:16:50 +0000880//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000881// Miscellaneous Instruction Implementations
882//===----------------------------------------------------------------------===//
883
Brian Gaekefea483d2003-11-07 20:04:22 +0000884void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000885 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000886
887 // Check to see if this is an intrinsic function call...
888 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +0000889 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +0000890 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000891 case Intrinsic::not_intrinsic:
892 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000893 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000894 GenericValue ArgIndex;
895 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
896 ArgIndex.UIntPairVal.second = 0;
897 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000898 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000899 }
Chris Lattner317201d2004-03-13 00:24:00 +0000900 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000901 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000902 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000903 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
904 return;
905 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000906 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000907 // class to transform it into hopefully tasty LLVM code.
908 //
909 Instruction *Prev = CS.getInstruction()->getPrev();
910 BasicBlock *Parent = CS.getInstruction()->getParent();
911 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
912
913 // Restore the CurInst pointer to the first instruction newly inserted, if
914 // any.
915 if (!Prev) {
916 SF.CurInst = Parent->begin();
917 } else {
918 SF.CurInst = Prev;
919 ++SF.CurInst;
920 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000921 return;
Chris Lattner73011782003-12-28 09:44:37 +0000922 }
923
Brian Gaekefea483d2003-11-07 20:04:22 +0000924 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000925 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000926 const unsigned NumArgs = SF.Caller.arg_size();
927 ArgVals.reserve(NumArgs);
928 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
929 e = SF.Caller.arg_end(); i != e; ++i) {
930 Value *V = *i;
931 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000932 // Promote all integral types whose size is < sizeof(int) into ints. We do
933 // this by zero or sign extending the value as appropriate according to the
934 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000935 const Type *Ty = V->getType();
936 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000937 if (Ty == Type::ShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000938 ArgVals.back().IntVal = ArgVals.back().ShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000939 else if (Ty == Type::UShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000940 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000941 else if (Ty == Type::SByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000942 ArgVals.back().IntVal = ArgVals.back().SByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000943 else if (Ty == Type::UByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000944 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000945 else if (Ty == Type::BoolTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000946 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
Chris Lattner93780132003-01-13 00:58:52 +0000947 else
Misha Brukmand1c881a2005-04-21 22:43:08 +0000948 assert(0 && "Unknown type!");
Chris Lattner93780132003-01-13 00:58:52 +0000949 }
950 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000951
Misha Brukmand1c881a2005-04-21 22:43:08 +0000952 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000953 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000954 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000955 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000956}
957
Chris Lattner86660982001-08-27 05:16:50 +0000958#define IMPLEMENT_SHIFT(OP, TY) \
959 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
960
Reid Spencer3822ff52006-11-08 06:47:33 +0000961#define IMPLEMENT_SIGNLESS_SHIFT(OP, TY1, TY2) \
962 case Type::TY2##TyID: \
963 IMPLEMENT_SHIFT(OP, TY1)
964
Brian Gaeke63438cc2003-12-11 00:22:59 +0000965static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
966 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000967 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000968 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000969 IMPLEMENT_SHIFT(<<, UByte);
970 IMPLEMENT_SHIFT(<<, SByte);
971 IMPLEMENT_SHIFT(<<, UShort);
972 IMPLEMENT_SHIFT(<<, Short);
973 IMPLEMENT_SHIFT(<<, UInt);
974 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000975 IMPLEMENT_SHIFT(<<, ULong);
976 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000977 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000978 cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000979 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000980 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000981}
982
Reid Spencer3822ff52006-11-08 06:47:33 +0000983static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
984 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000985 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000986 switch (Ty->getTypeID()) {
Reid Spencer3822ff52006-11-08 06:47:33 +0000987 IMPLEMENT_SIGNLESS_SHIFT(>>, UByte, SByte);
988 IMPLEMENT_SIGNLESS_SHIFT(>>, UShort, Short);
989 IMPLEMENT_SIGNLESS_SHIFT(>>, UInt, Int);
990 IMPLEMENT_SIGNLESS_SHIFT(>>, ULong, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000991 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000992 cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
Reid Spencer3822ff52006-11-08 06:47:33 +0000993 abort();
994 }
995 return Dest;
996}
997
998static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
999 const Type *Ty) {
1000 GenericValue Dest;
1001 switch (Ty->getTypeID()) {
1002 IMPLEMENT_SIGNLESS_SHIFT(>>, SByte, UByte);
1003 IMPLEMENT_SIGNLESS_SHIFT(>>, Short, UShort);
1004 IMPLEMENT_SIGNLESS_SHIFT(>>, Int, UInt);
1005 IMPLEMENT_SIGNLESS_SHIFT(>>, Long, ULong);
1006 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001007 cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +00001008 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001009 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001010 return Dest;
1011}
1012
1013void Interpreter::visitShl(ShiftInst &I) {
1014 ExecutionContext &SF = ECStack.back();
1015 const Type *Ty = I.getOperand(0)->getType();
1016 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1017 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1018 GenericValue Dest;
1019 Dest = executeShlInst (Src1, Src2, Ty);
1020 SetValue(&I, Dest, SF);
1021}
1022
Reid Spencer3822ff52006-11-08 06:47:33 +00001023void Interpreter::visitLShr(ShiftInst &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001024 ExecutionContext &SF = ECStack.back();
1025 const Type *Ty = I.getOperand(0)->getType();
1026 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1027 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1028 GenericValue Dest;
Reid Spencer3822ff52006-11-08 06:47:33 +00001029 Dest = executeLShrInst (Src1, Src2, Ty);
1030 SetValue(&I, Dest, SF);
1031}
1032
1033void Interpreter::visitAShr(ShiftInst &I) {
1034 ExecutionContext &SF = ECStack.back();
1035 const Type *Ty = I.getOperand(0)->getType();
1036 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1037 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1038 GenericValue Dest;
1039 Dest = executeAShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001040 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001041}
1042
Reid Spencer3da59db2006-11-27 01:05:10 +00001043#define IMPLEMENT_CAST_START \
1044 switch (DstTy->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +00001045
Reid Spencer3da59db2006-11-27 01:05:10 +00001046#define IMPLEMENT_CAST(DTY, DCTY, STY) \
1047 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
1048
1049#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
Chris Lattner86660982001-08-27 05:16:50 +00001050 case Type::DESTTY##TyID: \
Reid Spencer3da59db2006-11-27 01:05:10 +00001051 switch (SrcTy->getTypeID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +00001052 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +00001053 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
1054 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
1055 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1056 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1057 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +00001058 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1059 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +00001060 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
Reid Spencer3da59db2006-11-27 01:05:10 +00001061 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer); \
Chris Lattner86660982001-08-27 05:16:50 +00001062 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
Reid Spencer3da59db2006-11-27 01:05:10 +00001063 IMPLEMENT_CAST(DESTTY, DESTCTY, Double) \
1064 default: \
Bill Wendlinge8156192006-12-07 01:30:32 +00001065 cerr << "Unhandled cast: " \
Reid Spencer3da59db2006-11-27 01:05:10 +00001066 << *SrcTy << " to " << *DstTy << "\n"; \
Chris Lattner02868352003-04-22 21:22:33 +00001067 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +00001068 } \
1069 break
1070
Reid Spencer3da59db2006-11-27 01:05:10 +00001071#define IMPLEMENT_CAST_END \
Bill Wendlinge8156192006-12-07 01:30:32 +00001072 default: cerr \
Reid Spencer3da59db2006-11-27 01:05:10 +00001073 << "Unhandled dest type for cast instruction: " \
1074 << *DstTy << "\n"; \
1075 abort(); \
1076 }
Chris Lattner86660982001-08-27 05:16:50 +00001077
Reid Spencer3da59db2006-11-27 01:05:10 +00001078GenericValue Interpreter::executeCastOperation(Instruction::CastOps opcode,
1079 Value *SrcVal, const Type *DstTy,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001080 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001081 const Type *SrcTy = SrcVal->getType();
1082 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001083
Reid Spencer3da59db2006-11-27 01:05:10 +00001084 if (opcode == Instruction::Trunc && DstTy->getTypeID() == Type::BoolTyID) {
1085 // For truncations to bool, we must clear the high order bits of the source
1086 switch (SrcTy->getTypeID()) {
1087 case Type::BoolTyID: Src.BoolVal &= 1; break;
1088 case Type::SByteTyID: Src.SByteVal &= 1; break;
1089 case Type::UByteTyID: Src.UByteVal &= 1; break;
1090 case Type::ShortTyID: Src.ShortVal &= 1; break;
1091 case Type::UShortTyID: Src.UShortVal &= 1; break;
1092 case Type::IntTyID: Src.IntVal &= 1; break;
1093 case Type::UIntTyID: Src.UIntVal &= 1; break;
1094 case Type::LongTyID: Src.LongVal &= 1; break;
1095 case Type::ULongTyID: Src.ULongVal &= 1; break;
1096 default:
1097 assert(0 && "Can't trunc a non-integer!");
1098 break;
1099 }
1100 } else if (opcode == Instruction::SExt &&
1101 SrcTy->getTypeID() == Type::BoolTyID) {
1102 // For sign extension from bool, we must extend the source bits.
1103 SrcTy = Type::LongTy;
1104 Src.LongVal = 0 - Src.BoolVal;
Chris Lattner86660982001-08-27 05:16:50 +00001105 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001106
Reid Spencer3da59db2006-11-27 01:05:10 +00001107 switch (opcode) {
1108 case Instruction::Trunc: // src integer, dest integral (can't be long)
1109 IMPLEMENT_CAST_START
1110 IMPLEMENT_CAST_CASE(Bool , (bool));
1111 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1112 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1113 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1114 IMPLEMENT_CAST_CASE(Short , ( signed short));
1115 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1116 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1117 IMPLEMENT_CAST_END
1118 break;
1119 case Instruction::ZExt: // src integral (can't be long), dest integer
1120 IMPLEMENT_CAST_START
1121 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1122 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1123 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1124 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1125 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1126 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int ));
1127 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1128 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1129 IMPLEMENT_CAST_END
1130 break;
1131 case Instruction::SExt: // src integral (can't be long), dest integer
1132 IMPLEMENT_CAST_START
1133 IMPLEMENT_CAST_CASE(UByte , (unsigned char)(signed char));
1134 IMPLEMENT_CAST_CASE(SByte , (signed char));
1135 IMPLEMENT_CAST_CASE(UShort , (unsigned short)(signed short));
1136 IMPLEMENT_CAST_CASE(Short , (signed short));
1137 IMPLEMENT_CAST_CASE(UInt , (unsigned int )(signed int));
1138 IMPLEMENT_CAST_CASE(Int , (signed int));
1139 IMPLEMENT_CAST_CASE(ULong , (uint64_t)(int64_t));
1140 IMPLEMENT_CAST_CASE(Long , (int64_t));
1141 IMPLEMENT_CAST_END
1142 break;
1143 case Instruction::FPTrunc: // src double, dest float
1144 IMPLEMENT_CAST_START
1145 IMPLEMENT_CAST_CASE(Float , (float));
1146 IMPLEMENT_CAST_END
1147 break;
1148 case Instruction::FPExt: // src float, dest double
1149 IMPLEMENT_CAST_START
1150 IMPLEMENT_CAST_CASE(Double , (double));
1151 IMPLEMENT_CAST_END
1152 break;
1153 case Instruction::UIToFP: // src integral, dest floating
1154 IMPLEMENT_CAST_START
1155 IMPLEMENT_CAST_CASE(Float , (float)(uint64_t));
1156 IMPLEMENT_CAST_CASE(Double , (double)(uint64_t));
1157 IMPLEMENT_CAST_END
1158 break;
1159 case Instruction::SIToFP: // src integeral, dest floating
1160 IMPLEMENT_CAST_START
1161 IMPLEMENT_CAST_CASE(Float , (float)(int64_t));
1162 IMPLEMENT_CAST_CASE(Double , (double)(int64_t));
1163 IMPLEMENT_CAST_END
1164 break;
1165 case Instruction::FPToUI: // src floating, dest integral
1166 IMPLEMENT_CAST_START
1167 IMPLEMENT_CAST_CASE(Bool , (bool));
1168 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1169 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1170 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1171 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1172 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1173 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int ));
1174 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1175 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1176 IMPLEMENT_CAST_END
1177 break;
1178 case Instruction::FPToSI: // src floating, dest integral
1179 IMPLEMENT_CAST_START
1180 IMPLEMENT_CAST_CASE(Bool , (bool));
1181 IMPLEMENT_CAST_CASE(UByte , (unsigned char)(signed char));
1182 IMPLEMENT_CAST_CASE(SByte , (signed char));
1183 IMPLEMENT_CAST_CASE(UShort , (unsigned short)(signed short));
1184 IMPLEMENT_CAST_CASE(Short , (signed short));
1185 IMPLEMENT_CAST_CASE(UInt , (unsigned int )(signed int));
1186 IMPLEMENT_CAST_CASE(Int , (signed int));
1187 IMPLEMENT_CAST_CASE(ULong , (uint64_t)(int64_t));
1188 IMPLEMENT_CAST_CASE(Long , (int64_t));
1189 IMPLEMENT_CAST_END
1190 break;
1191 case Instruction::PtrToInt: // src pointer, dest integral
1192 IMPLEMENT_CAST_START
1193 IMPLEMENT_CAST_CASE(Bool , (bool));
1194 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1195 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1196 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1197 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1198 IMPLEMENT_CAST_CASE(UInt , (unsigned int));
1199 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int));
1200 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1201 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1202 IMPLEMENT_CAST_END
1203 break;
1204 case Instruction::IntToPtr: // src integral, dest pointer
1205 IMPLEMENT_CAST_START
1206 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1207 IMPLEMENT_CAST_END
1208 break;
1209 case Instruction::BitCast: // src any, dest any (same size)
1210 IMPLEMENT_CAST_START
1211 IMPLEMENT_CAST_CASE(Bool , (bool));
1212 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1213 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1214 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1215 IMPLEMENT_CAST_CASE(Short , ( signed short));
1216 IMPLEMENT_CAST_CASE(UInt , (unsigned int));
1217 IMPLEMENT_CAST_CASE(Int , ( signed int));
1218 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1219 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1220 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1221 IMPLEMENT_CAST_CASE(Float , (float));
1222 IMPLEMENT_CAST_CASE(Double , (double));
1223 IMPLEMENT_CAST_END
1224 break;
1225 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001226 cerr << "Invalid cast opcode for cast instruction: " << opcode << "\n";
Reid Spencer3da59db2006-11-27 01:05:10 +00001227 abort();
1228 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001229 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001230}
Chris Lattner92101ac2001-08-23 17:05:04 +00001231
Chris Lattnerd7916e92003-05-10 21:22:39 +00001232void Interpreter::visitCastInst(CastInst &I) {
1233 ExecutionContext &SF = ECStack.back();
Reid Spencer3da59db2006-11-27 01:05:10 +00001234 SetValue(&I, executeCastOperation(I.getOpcode(), I.getOperand(0),
1235 I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001236}
Chris Lattner92101ac2001-08-23 17:05:04 +00001237
Brian Gaekec1a2be12003-11-07 21:20:47 +00001238#define IMPLEMENT_VAARG(TY) \
1239 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1240
1241void Interpreter::visitVAArgInst(VAArgInst &I) {
1242 ExecutionContext &SF = ECStack.back();
1243
Brian Gaeke9d20b712004-02-25 23:01:48 +00001244 // Get the incoming valist parameter. LLI treats the valist as a
1245 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001246 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001247 GenericValue Dest;
1248 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001249 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001250 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001251 switch (Ty->getTypeID()) {
Brian Gaekec1a2be12003-11-07 21:20:47 +00001252 IMPLEMENT_VAARG(UByte);
1253 IMPLEMENT_VAARG(SByte);
1254 IMPLEMENT_VAARG(UShort);
1255 IMPLEMENT_VAARG(Short);
1256 IMPLEMENT_VAARG(UInt);
1257 IMPLEMENT_VAARG(Int);
1258 IMPLEMENT_VAARG(ULong);
1259 IMPLEMENT_VAARG(Long);
1260 IMPLEMENT_VAARG(Pointer);
1261 IMPLEMENT_VAARG(Float);
1262 IMPLEMENT_VAARG(Double);
1263 IMPLEMENT_VAARG(Bool);
1264 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001265 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Brian Gaekec1a2be12003-11-07 21:20:47 +00001266 abort();
1267 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001268
Brian Gaekec1a2be12003-11-07 21:20:47 +00001269 // Set the Value of this Instruction.
1270 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001271
1272 // Move the pointer to the next vararg.
1273 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001274}
1275
Chris Lattner92101ac2001-08-23 17:05:04 +00001276//===----------------------------------------------------------------------===//
1277// Dispatch and Execution Code
1278//===----------------------------------------------------------------------===//
1279
Chris Lattner92101ac2001-08-23 17:05:04 +00001280//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001281// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001282//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001283void Interpreter::callFunction(Function *F,
1284 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001285 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1286 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1287 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001288 // Make a new stack frame... and fill it in.
1289 ECStack.push_back(ExecutionContext());
1290 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001291 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001292
1293 // Special handling for external functions.
1294 if (F->isExternal()) {
1295 GenericValue Result = callExternalFunction (F, ArgVals);
1296 // Simulate a 'ret' instruction of the appropriate type.
1297 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1298 return;
1299 }
1300
1301 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001302 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001303 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001304
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001305 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001306 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001307 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001308 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001309
1310 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001311 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001312 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001313 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001314
1315 // Handle varargs arguments...
1316 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001317}
1318
Chris Lattner92101ac2001-08-23 17:05:04 +00001319void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001320 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001321 // Interpret a single instruction & increment the "PC".
1322 ExecutionContext &SF = ECStack.back(); // Current stack frame
1323 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001324
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001325 // Track the number of dynamic instructions executed.
1326 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001327
Bill Wendling480f0932006-11-27 23:54:50 +00001328 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001329 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001330 }
1331}