blob: 872bfeb737d55978e5846a1a9827ff650022cc5f [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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 Spencere1aa0662007-03-03 06:22:22 +000021#include "llvm/ADT/APInt.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/ADT/Statistic.h"
Chris Lattner663ceeb2008-07-08 17:25:49 +000023#include "llvm/Support/CommandLine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000025#include "llvm/Support/ErrorHandling.h"
Reid Spencera54b7cb2007-01-12 07:05:14 +000026#include "llvm/Support/MathExtras.h"
Gabor Greif724441e2007-10-11 19:40:35 +000027#include <algorithm>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000028#include <cmath>
29#include <cstring>
Chris Lattner4af6de82003-11-25 20:44:56 +000030using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000031
Chris Lattnercecf56b2006-12-19 22:56:53 +000032STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner663ceeb2008-07-08 17:25:49 +000034static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
35 cl::desc("make the interpreter print every volatile load and store"));
36
Chris Lattner2e42d3a2001-10-15 05:51:48 +000037//===----------------------------------------------------------------------===//
Reid Spencere1aa0662007-03-03 06:22:22 +000038// Various Helper Functions
Chris Lattner39bb5b42001-10-15 13:25:40 +000039//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000040
Chris Lattner39bb5b42001-10-15 13:25:40 +000041static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000042 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +000043}
44
Chris Lattner2adcd832002-05-03 19:52:30 +000045//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000046// Binary Instruction Implementations
47//===----------------------------------------------------------------------===//
48
49#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
Reid Spencerf9536332007-03-06 03:09:31 +000050 case Type::TY##TyID: \
51 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
52 break
Chris Lattner92101ac2001-08-23 17:05:04 +000053
Dan Gohmanae3a0be2009-06-04 22:49:04 +000054static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
55 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000056 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +000057 IMPLEMENT_BINARY_OPERATOR(+, Float);
58 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000059 default:
Dan Gohmanae3a0be2009-06-04 22:49:04 +000060 cerr << "Unhandled type for FAdd instruction: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +000061 llvm_unreachable();
Chris Lattner92101ac2001-08-23 17:05:04 +000062 }
Chris Lattner92101ac2001-08-23 17:05:04 +000063}
64
Dan Gohmanae3a0be2009-06-04 22:49:04 +000065static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
66 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000067 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +000068 IMPLEMENT_BINARY_OPERATOR(-, Float);
69 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000070 default:
Dan Gohmanae3a0be2009-06-04 22:49:04 +000071 cerr << "Unhandled type for FSub instruction: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +000072 llvm_unreachable();
Chris Lattner92101ac2001-08-23 17:05:04 +000073 }
Chris Lattner92101ac2001-08-23 17:05:04 +000074}
75
Dan Gohmanae3a0be2009-06-04 22:49:04 +000076static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
77 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000078 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +000079 IMPLEMENT_BINARY_OPERATOR(*, Float);
80 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +000081 default:
Dan Gohmanae3a0be2009-06-04 22:49:04 +000082 cerr << "Unhandled type for FMul instruction: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +000083 llvm_unreachable();
Chris Lattnerc2593162001-10-27 08:28:11 +000084 }
Chris Lattnerc2593162001-10-27 08:28:11 +000085}
86
Reid Spencere1aa0662007-03-03 06:22:22 +000087static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
88 GenericValue Src2, const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000089 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +000090 IMPLEMENT_BINARY_OPERATOR(/, Float);
91 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +000092 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +000093 cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +000094 llvm_unreachable();
Chris Lattnerbb76f022001-10-30 20:27:31 +000095 }
Chris Lattnerbb76f022001-10-30 20:27:31 +000096}
97
Reid Spencere1aa0662007-03-03 06:22:22 +000098static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
99 GenericValue Src2, const Type *Ty) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000100 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000101 case Type::FloatTyID:
102 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
103 break;
104 case Type::DoubleTyID:
105 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
106 break;
107 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000108 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000109 llvm_unreachable();
Chris Lattnerc2593162001-10-27 08:28:11 +0000110 }
Chris Lattnerc2593162001-10-27 08:28:11 +0000111}
112
Reid Spencerf9536332007-03-06 03:09:31 +0000113#define IMPLEMENT_INTEGER_ICMP(OP, TY) \
114 case Type::IntegerTyID: \
115 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
116 break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000117
Chris Lattnerfd506f52003-04-23 19:55:35 +0000118// Handle pointers specially because they must be compared with only as much
119// width as the host has. We _do not_ want to be comparing 64 bit values when
120// running on a 32-bit target, otherwise the upper 32 bits might mess up
121// comparisons if they contain garbage.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000122#define IMPLEMENT_POINTER_ICMP(OP) \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000123 case Type::PointerTyID: \
Reid Spencerf9536332007-03-06 03:09:31 +0000124 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
125 (void*)(intptr_t)Src2.PointerVal); \
126 break;
Chris Lattnerfd506f52003-04-23 19:55:35 +0000127
Reid Spencere4d87aa2006-12-23 06:05:41 +0000128static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
129 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000130 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000131 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000132 IMPLEMENT_INTEGER_ICMP(eq,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000133 IMPLEMENT_POINTER_ICMP(==);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000134 default:
135 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000136 llvm_unreachable();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000137 }
138 return Dest;
139}
140
141static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
142 const Type *Ty) {
143 GenericValue Dest;
144 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000145 IMPLEMENT_INTEGER_ICMP(ne,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000146 IMPLEMENT_POINTER_ICMP(!=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000147 default:
148 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000149 llvm_unreachable();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000150 }
151 return Dest;
152}
153
154static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
155 const Type *Ty) {
156 GenericValue Dest;
157 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000158 IMPLEMENT_INTEGER_ICMP(ult,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000159 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000160 default:
161 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000162 llvm_unreachable();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000163 }
164 return Dest;
165}
166
167static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
168 const Type *Ty) {
169 GenericValue Dest;
170 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000171 IMPLEMENT_INTEGER_ICMP(slt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000172 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000173 default:
174 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000175 llvm_unreachable();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000176 }
177 return Dest;
178}
179
180static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
181 const Type *Ty) {
182 GenericValue Dest;
183 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000184 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000185 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000186 default:
187 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000188 llvm_unreachable();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000189 }
190 return Dest;
191}
192
193static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
194 const Type *Ty) {
195 GenericValue Dest;
196 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000197 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000198 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000199 default:
200 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000201 llvm_unreachable();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000202 }
203 return Dest;
204}
205
206static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
207 const Type *Ty) {
208 GenericValue Dest;
209 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000210 IMPLEMENT_INTEGER_ICMP(ule,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000211 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000212 default:
213 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000214 llvm_unreachable();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000215 }
216 return Dest;
217}
218
219static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
220 const Type *Ty) {
221 GenericValue Dest;
222 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000223 IMPLEMENT_INTEGER_ICMP(sle,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000224 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000225 default:
226 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000227 llvm_unreachable();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000228 }
229 return Dest;
230}
231
232static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
233 const Type *Ty) {
234 GenericValue Dest;
235 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000236 IMPLEMENT_INTEGER_ICMP(uge,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000237 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000238 default:
239 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000240 llvm_unreachable();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000241 }
242 return Dest;
243}
244
245static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
246 const Type *Ty) {
247 GenericValue Dest;
248 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000249 IMPLEMENT_INTEGER_ICMP(sge,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000250 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000251 default:
252 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000253 llvm_unreachable();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000254 }
255 return Dest;
256}
257
Reid Spencere49661b2006-12-31 05:51:36 +0000258void Interpreter::visitICmpInst(ICmpInst &I) {
259 ExecutionContext &SF = ECStack.back();
260 const Type *Ty = I.getOperand(0)->getType();
261 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
262 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
263 GenericValue R; // Result
264
265 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000266 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
267 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
Reid Spencere49661b2006-12-31 05:51:36 +0000268 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
269 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
270 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
271 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
272 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
273 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
274 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
275 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
276 default:
277 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
Torok Edwin7d696d82009-07-11 13:10:19 +0000278 llvm_unreachable();
Reid Spencere49661b2006-12-31 05:51:36 +0000279 }
280
281 SetValue(&I, R, SF);
282}
283
284#define IMPLEMENT_FCMP(OP, TY) \
Reid Spencerf9536332007-03-06 03:09:31 +0000285 case Type::TY##TyID: \
286 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
287 break
Reid Spencere4d87aa2006-12-23 06:05:41 +0000288
Reid Spencera54b7cb2007-01-12 07:05:14 +0000289static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000290 const Type *Ty) {
291 GenericValue Dest;
292 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000293 IMPLEMENT_FCMP(==, Float);
294 IMPLEMENT_FCMP(==, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000295 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000296 cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000297 llvm_unreachable();
Chris Lattner92101ac2001-08-23 17:05:04 +0000298 }
299 return Dest;
300}
301
Reid Spencera54b7cb2007-01-12 07:05:14 +0000302static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000303 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000304 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000305 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000306 IMPLEMENT_FCMP(!=, Float);
307 IMPLEMENT_FCMP(!=, Double);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000308
Chris Lattner92101ac2001-08-23 17:05:04 +0000309 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000310 cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000311 llvm_unreachable();
Chris Lattner92101ac2001-08-23 17:05:04 +0000312 }
313 return Dest;
314}
315
Reid Spencera54b7cb2007-01-12 07:05:14 +0000316static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000317 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000318 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000319 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000320 IMPLEMENT_FCMP(<=, Float);
321 IMPLEMENT_FCMP(<=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000322 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000323 cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000324 llvm_unreachable();
Chris Lattner92101ac2001-08-23 17:05:04 +0000325 }
326 return Dest;
327}
328
Reid Spencera54b7cb2007-01-12 07:05:14 +0000329static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000330 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000331 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000332 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000333 IMPLEMENT_FCMP(>=, Float);
334 IMPLEMENT_FCMP(>=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000335 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000336 cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000337 llvm_unreachable();
Chris Lattner92101ac2001-08-23 17:05:04 +0000338 }
339 return Dest;
340}
341
Reid Spencera54b7cb2007-01-12 07:05:14 +0000342static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000343 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000344 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000345 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000346 IMPLEMENT_FCMP(<, Float);
347 IMPLEMENT_FCMP(<, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000348 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000349 cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000350 llvm_unreachable();
Chris Lattner92101ac2001-08-23 17:05:04 +0000351 }
352 return Dest;
353}
354
Reid Spencera54b7cb2007-01-12 07:05:14 +0000355static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000356 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000357 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000358 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000359 IMPLEMENT_FCMP(>, Float);
360 IMPLEMENT_FCMP(>, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000361 default:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000362 cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000363 llvm_unreachable();
Chris Lattner92101ac2001-08-23 17:05:04 +0000364 }
365 return Dest;
366}
367
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000368#define IMPLEMENT_UNORDERED(TY, X,Y) \
369 if (TY == Type::FloatTy) { \
370 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
371 Dest.IntVal = APInt(1,true); \
372 return Dest; \
373 } \
374 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
375 Dest.IntVal = APInt(1,true); \
376 return Dest; \
377 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000378
379
380static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
381 const Type *Ty) {
382 GenericValue Dest;
383 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
384 return executeFCMP_OEQ(Src1, Src2, Ty);
385}
386
387static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
388 const Type *Ty) {
389 GenericValue Dest;
390 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
391 return executeFCMP_ONE(Src1, Src2, Ty);
392}
393
394static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
395 const Type *Ty) {
396 GenericValue Dest;
397 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
398 return executeFCMP_OLE(Src1, Src2, Ty);
399}
400
401static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
402 const Type *Ty) {
403 GenericValue Dest;
404 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
405 return executeFCMP_OGE(Src1, Src2, Ty);
406}
407
408static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
409 const Type *Ty) {
410 GenericValue Dest;
411 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
412 return executeFCMP_OLT(Src1, Src2, Ty);
413}
414
415static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
416 const Type *Ty) {
417 GenericValue Dest;
418 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
419 return executeFCMP_OGT(Src1, Src2, Ty);
420}
421
422static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
423 const Type *Ty) {
424 GenericValue Dest;
425 if (Ty == Type::FloatTy)
Reid Spencerf9536332007-03-06 03:09:31 +0000426 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
427 Src2.FloatVal == Src2.FloatVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000428 else
Reid Spencerf9536332007-03-06 03:09:31 +0000429 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
430 Src2.DoubleVal == Src2.DoubleVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000431 return Dest;
432}
433
434static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
435 const Type *Ty) {
436 GenericValue Dest;
437 if (Ty == Type::FloatTy)
Reid Spencerf9536332007-03-06 03:09:31 +0000438 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
439 Src2.FloatVal != Src2.FloatVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000440 else
Reid Spencerf9536332007-03-06 03:09:31 +0000441 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
442 Src2.DoubleVal != Src2.DoubleVal));
Reid Spencera54b7cb2007-01-12 07:05:14 +0000443 return Dest;
444}
445
Reid Spencere4d87aa2006-12-23 06:05:41 +0000446void Interpreter::visitFCmpInst(FCmpInst &I) {
447 ExecutionContext &SF = ECStack.back();
448 const Type *Ty = I.getOperand(0)->getType();
449 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
450 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
451 GenericValue R; // Result
452
453 switch (I.getPredicate()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000454 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
455 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000456 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
457 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
458 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
459 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
460 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
461 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
462 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
463 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
464 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
465 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
466 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
467 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
468 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
469 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000470 default:
471 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
Torok Edwin7d696d82009-07-11 13:10:19 +0000472 llvm_unreachable();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000473 }
474
475 SetValue(&I, R, SF);
476}
477
Reid Spencere4d87aa2006-12-23 06:05:41 +0000478static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
479 GenericValue Src2, const Type *Ty) {
480 GenericValue Result;
481 switch (predicate) {
482 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
483 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
484 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
485 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
486 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
487 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
488 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
489 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
490 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
491 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000492 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
493 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
494 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
495 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
496 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
497 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
498 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
499 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
500 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
501 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
502 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
503 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
504 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
505 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000506 case FCmpInst::FCMP_FALSE: {
507 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000508 Result.IntVal = APInt(1, false);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000509 return Result;
510 }
511 case FCmpInst::FCMP_TRUE: {
512 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000513 Result.IntVal = APInt(1, true);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000514 return Result;
515 }
516 default:
517 cerr << "Unhandled Cmp predicate\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000518 llvm_unreachable();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000519 }
520}
521
Chris Lattnerd7916e92003-05-10 21:22:39 +0000522void Interpreter::visitBinaryOperator(BinaryOperator &I) {
523 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000524 const Type *Ty = I.getOperand(0)->getType();
525 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
526 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000527 GenericValue R; // Result
528
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000529 switch (I.getOpcode()) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000530 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
531 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
532 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
533 case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;
534 case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;
535 case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;
536 case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;
537 case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;
Reid Spencerf9536332007-03-06 03:09:31 +0000538 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
539 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
540 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
541 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
542 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
543 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
544 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000545 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000546 cerr << "Don't know how to handle this binary operator!\n-->" << I;
Torok Edwin7d696d82009-07-11 13:10:19 +0000547 llvm_unreachable();
Chris Lattner92101ac2001-08-23 17:05:04 +0000548 }
549
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000550 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000551}
552
Misha Brukmand1c881a2005-04-21 22:43:08 +0000553static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000554 GenericValue Src3) {
Reid Spencerf9536332007-03-06 03:09:31 +0000555 return Src1.IntVal == 0 ? Src3 : Src2;
Chris Lattner759d34f2004-04-20 16:43:21 +0000556}
557
558void Interpreter::visitSelectInst(SelectInst &I) {
559 ExecutionContext &SF = ECStack.back();
560 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
561 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
562 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
Reid Spencerf9536332007-03-06 03:09:31 +0000563 GenericValue R = executeSelectInst(Src1, Src2, Src3);
Chris Lattner759d34f2004-04-20 16:43:21 +0000564 SetValue(&I, R, SF);
565}
566
567
Chris Lattner92101ac2001-08-23 17:05:04 +0000568//===----------------------------------------------------------------------===//
569// Terminator Instruction Implementations
570//===----------------------------------------------------------------------===//
571
Chris Lattnere43db882001-10-27 04:15:57 +0000572void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000573 // runAtExitHandlers() assumes there are no stack frames, but
574 // if exit() was called, then it had a stack frame. Blow away
575 // the stack before interpreting atexit handlers.
576 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000577 runAtExitHandlers ();
Reid Spencerf9536332007-03-06 03:09:31 +0000578 exit (GV.IntVal.zextOrTrunc(32).getZExtValue());
Chris Lattnere43db882001-10-27 04:15:57 +0000579}
580
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000581/// Pop the last stack frame off of ECStack and then copy the result
582/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000583/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000584/// CallInst if there was a previous stack frame. This method may
585/// invalidate any ECStack iterators you have. This method also takes
586/// care of switching to the normal destination BB, if we are returning
587/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000588///
589void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
590 GenericValue Result) {
591 // Pop the current stack frame.
592 ECStack.pop_back();
593
Misha Brukmand1c881a2005-04-21 22:43:08 +0000594 if (ECStack.empty()) { // Finished main. Put result into exit code...
Chris Lattner42a75512007-01-15 02:27:26 +0000595 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000596 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000597 } else {
Reid Spencere7707872007-06-01 22:23:29 +0000598 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000599 }
600 } else {
601 // If we have a previous stack frame, and we have a previous call,
602 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000603 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000604 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000605 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000606 SetValue(I, Result, CallingSF);
607 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
608 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000609 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000610 }
611 }
612}
613
Chris Lattnerd7916e92003-05-10 21:22:39 +0000614void Interpreter::visitReturnInst(ReturnInst &I) {
615 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000616 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000617 GenericValue Result;
618
619 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000620 if (I.getNumOperands()) {
621 RetTy = I.getReturnValue()->getType();
622 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000623 }
624
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000625 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000626}
627
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000628void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000629 // Unwind stack
630 Instruction *Inst;
631 do {
632 ECStack.pop_back ();
633 if (ECStack.empty ())
Torok Edwin7d696d82009-07-11 13:10:19 +0000634 llvm_report_error("Empty stack during unwind!");
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000635 Inst = ECStack.back ().Caller.getInstruction ();
636 } while (!(Inst && isa<InvokeInst> (Inst)));
637
638 // Return from invoke
639 ExecutionContext &InvokingSF = ECStack.back ();
640 InvokingSF.Caller = CallSite ();
641
642 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000643 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000644}
645
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000646void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000647 llvm_report_error("ERROR: Program executed an 'unreachable' instruction!");
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000648}
649
Chris Lattnerd7916e92003-05-10 21:22:39 +0000650void Interpreter::visitBranchInst(BranchInst &I) {
651 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000652 BasicBlock *Dest;
653
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000654 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
655 if (!I.isUnconditional()) {
656 Value *Cond = I.getCondition();
Reid Spencerf9536332007-03-06 03:09:31 +0000657 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000658 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000659 }
Chris Lattner77113b62003-05-10 20:21:16 +0000660 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000661}
662
Chris Lattnerd7916e92003-05-10 21:22:39 +0000663void Interpreter::visitSwitchInst(SwitchInst &I) {
664 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000665 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
666 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000667
668 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000669 BasicBlock *Dest = 0;
670 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Reid Spencerf9536332007-03-06 03:09:31 +0000671 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
672 .IntVal != 0) {
Chris Lattner09e93922003-04-22 20:34:47 +0000673 Dest = cast<BasicBlock>(I.getOperand(i+1));
674 break;
675 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000676
Chris Lattner09e93922003-04-22 20:34:47 +0000677 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000678 SwitchToNewBasicBlock(Dest, SF);
679}
680
681// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
682// This function handles the actual updating of block and instruction iterators
683// as well as execution of all of the PHI nodes in the destination block.
684//
685// This method does this because all of the PHI nodes must be executed
686// atomically, reading their inputs before any of the results are updated. Not
687// doing this can cause problems if the PHI nodes depend on other PHI nodes for
688// their inputs. If the input PHI node is updated before it is read, incorrect
689// results can happen. Thus we use a two phase approach.
690//
691void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
692 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
693 SF.CurBB = Dest; // Update CurBB to branch destination
694 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
695
696 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
697
698 // Loop over all of the PHI nodes in the current block, reading their inputs.
699 std::vector<GenericValue> ResultValues;
700
701 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
702 // Search for the value corresponding to this previous bb...
703 int i = PN->getBasicBlockIndex(PrevBB);
704 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
705 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000706
Chris Lattner77113b62003-05-10 20:21:16 +0000707 // Save the incoming value for this PHI node...
708 ResultValues.push_back(getOperandValue(IncomingValue, SF));
709 }
710
711 // Now loop over all of the PHI nodes setting their values...
712 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000713 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
714 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000715 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000716 }
Chris Lattner09e93922003-04-22 20:34:47 +0000717}
718
Chris Lattner92101ac2001-08-23 17:05:04 +0000719//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000720// Memory Instruction Implementations
721//===----------------------------------------------------------------------===//
722
Chris Lattnerd7916e92003-05-10 21:22:39 +0000723void Interpreter::visitAllocationInst(AllocationInst &I) {
724 ExecutionContext &SF = ECStack.back();
725
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000726 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000727
Chris Lattnercc82cc12002-04-28 21:57:33 +0000728 // Get the number of elements being allocated by the array...
Reid Spencerf9536332007-03-06 03:09:31 +0000729 unsigned NumElements =
730 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
731
Duncan Sands777d2302009-05-09 07:06:46 +0000732 unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
Reid Spencerf9536332007-03-06 03:09:31 +0000733
Gabor Greif724441e2007-10-11 19:40:35 +0000734 // Avoid malloc-ing zero bytes, use max()...
735 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
Chris Lattner86660982001-08-27 05:16:50 +0000736
737 // Allocate enough memory to hold the type...
Reid Spencerf9536332007-03-06 03:09:31 +0000738 void *Memory = malloc(MemToAlloc);
739
740 DOUT << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
741 << NumElements << " (Total: " << MemToAlloc << ") at "
Bill Wendling47992ea2007-03-08 23:37:24 +0000742 << uintptr_t(Memory) << '\n';
Chris Lattner9bffa732002-02-19 18:50:09 +0000743
Chris Lattnerfe11a972002-12-23 23:59:41 +0000744 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000745 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000746 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000747
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000748 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000749 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000750}
751
Chris Lattnerd7916e92003-05-10 21:22:39 +0000752void Interpreter::visitFreeInst(FreeInst &I) {
753 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000754 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
755 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000756 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000757 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000758}
759
Chris Lattnera34c5682002-08-27 22:33:45 +0000760// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000761//
Chris Lattner4af6de82003-11-25 20:44:56 +0000762GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000763 gep_type_iterator E,
764 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000765 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000766 "Cannot getElementOffset of a nonpointer type!");
767
Reid Spencerf9536332007-03-06 03:09:31 +0000768 uint64_t Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000769
770 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000771 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000772 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000773
Reid Spencerb83eb642006-10-20 07:07:24 +0000774 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
775 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000776
Reid Spencerf9536332007-03-06 03:09:31 +0000777 Total += SLO->getElementOffset(Index);
Chris Lattner4af6de82003-11-25 20:44:56 +0000778 } else {
779 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000780 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000781 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
782
Reid Spencera54b7cb2007-01-12 07:05:14 +0000783 int64_t Idx;
784 unsigned BitWidth =
785 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
Reid Spencer23e28832007-01-18 01:25:42 +0000786 if (BitWidth == 32)
Reid Spencerf9536332007-03-06 03:09:31 +0000787 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
Chris Lattneraee7f212008-04-06 21:50:58 +0000788 else {
789 assert(BitWidth == 64 && "Invalid index type for getelementptr");
Reid Spencerf9536332007-03-06 03:09:31 +0000790 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
Chris Lattneraee7f212008-04-06 21:50:58 +0000791 }
Duncan Sands777d2302009-05-09 07:06:46 +0000792 Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000793 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000794 }
795
Chris Lattnera34c5682002-08-27 22:33:45 +0000796 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000797 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
798 DOUT << "GEP Index " << Total << " bytes.\n";
Chris Lattnera34c5682002-08-27 22:33:45 +0000799 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000800}
801
Chris Lattnerd7916e92003-05-10 21:22:39 +0000802void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
803 ExecutionContext &SF = ECStack.back();
Owen Andersonfdca74c2009-06-26 16:46:15 +0000804 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000805 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000806}
807
Chris Lattnerd7916e92003-05-10 21:22:39 +0000808void Interpreter::visitLoadInst(LoadInst &I) {
809 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000810 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000811 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Reid Spencere0929362007-03-03 08:38:04 +0000812 GenericValue Result;
Reid Spencere0929362007-03-03 08:38:04 +0000813 LoadValueFromMemory(Result, Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000814 SetValue(&I, Result, SF);
Chris Lattner663ceeb2008-07-08 17:25:49 +0000815 if (I.isVolatile() && PrintVolatile)
816 cerr << "Volatile load " << I;
Chris Lattner86660982001-08-27 05:16:50 +0000817}
818
Chris Lattnerd7916e92003-05-10 21:22:39 +0000819void Interpreter::visitStoreInst(StoreInst &I) {
820 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000821 GenericValue Val = getOperandValue(I.getOperand(0), SF);
822 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000823 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000824 I.getOperand(0)->getType());
Chris Lattner663ceeb2008-07-08 17:25:49 +0000825 if (I.isVolatile() && PrintVolatile)
826 cerr << "Volatile store: " << I;
Chris Lattnerfddc7552002-10-15 20:34:05 +0000827}
828
Chris Lattner86660982001-08-27 05:16:50 +0000829//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000830// Miscellaneous Instruction Implementations
831//===----------------------------------------------------------------------===//
832
Brian Gaekefea483d2003-11-07 20:04:22 +0000833void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000834 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000835
836 // Check to see if this is an intrinsic function call...
Reid Spencer4ccf4622007-04-16 21:50:40 +0000837 Function *F = CS.getCalledFunction();
838 if (F && F->isDeclaration ())
Chris Lattner73011782003-12-28 09:44:37 +0000839 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000840 case Intrinsic::not_intrinsic:
841 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000842 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000843 GenericValue ArgIndex;
844 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
845 ArgIndex.UIntPairVal.second = 0;
846 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000847 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000848 }
Chris Lattner317201d2004-03-13 00:24:00 +0000849 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000850 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000851 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000852 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
853 return;
854 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000855 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000856 // class to transform it into hopefully tasty LLVM code.
857 //
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000858 BasicBlock::iterator me(CS.getInstruction());
Chris Lattner73011782003-12-28 09:44:37 +0000859 BasicBlock *Parent = CS.getInstruction()->getParent();
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000860 bool atBegin(Parent->begin() == me);
861 if (!atBegin)
862 --me;
Chris Lattner73011782003-12-28 09:44:37 +0000863 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
864
865 // Restore the CurInst pointer to the first instruction newly inserted, if
866 // any.
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000867 if (atBegin) {
Chris Lattner73011782003-12-28 09:44:37 +0000868 SF.CurInst = Parent->begin();
869 } else {
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000870 SF.CurInst = me;
Chris Lattner73011782003-12-28 09:44:37 +0000871 ++SF.CurInst;
872 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000873 return;
Chris Lattner73011782003-12-28 09:44:37 +0000874 }
875
Reid Spencer4ccf4622007-04-16 21:50:40 +0000876
Brian Gaekefea483d2003-11-07 20:04:22 +0000877 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000878 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000879 const unsigned NumArgs = SF.Caller.arg_size();
880 ArgVals.reserve(NumArgs);
Reid Spencer4ccf4622007-04-16 21:50:40 +0000881 uint16_t pNum = 1;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000882 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
Reid Spencer4ccf4622007-04-16 21:50:40 +0000883 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
Brian Gaekeae2495a2003-11-07 19:59:08 +0000884 Value *V = *i;
885 ArgVals.push_back(getOperandValue(V, SF));
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000886 // Promote all integral types whose size is < sizeof(i32) into i32.
887 // We do this by zero or sign extending the value as appropriate
888 // according to the parameter attributes
889 const Type *Ty = V->getType();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000890 if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32)) {
Devang Patel05988662008-09-25 21:00:45 +0000891 if (CS.paramHasAttr(pNum, Attribute::ZExt))
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000892 ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
Devang Patel05988662008-09-25 21:00:45 +0000893 else if (CS.paramHasAttr(pNum, Attribute::SExt))
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000894 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000895 }
Chris Lattner93780132003-01-13 00:58:52 +0000896 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000897
Misha Brukmand1c881a2005-04-21 22:43:08 +0000898 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000899 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000900 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000901 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000902}
903
Reid Spencer832254e2007-02-02 02:16:23 +0000904void Interpreter::visitShl(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000905 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000906 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
907 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
908 GenericValue Dest;
Chris Lattner9029cb82009-01-16 20:17:02 +0000909 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
910 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
911 else
912 Dest.IntVal = Src1.IntVal;
913
Brian Gaeke63438cc2003-12-11 00:22:59 +0000914 SetValue(&I, Dest, SF);
915}
916
Reid Spencer832254e2007-02-02 02:16:23 +0000917void Interpreter::visitLShr(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000918 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000919 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
920 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
921 GenericValue Dest;
Chris Lattner9029cb82009-01-16 20:17:02 +0000922 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
923 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
924 else
925 Dest.IntVal = Src1.IntVal;
926
Reid Spencer3822ff52006-11-08 06:47:33 +0000927 SetValue(&I, Dest, SF);
928}
929
Reid Spencer832254e2007-02-02 02:16:23 +0000930void Interpreter::visitAShr(BinaryOperator &I) {
Reid Spencer3822ff52006-11-08 06:47:33 +0000931 ExecutionContext &SF = ECStack.back();
Reid Spencer3822ff52006-11-08 06:47:33 +0000932 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
933 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner9029cb82009-01-16 20:17:02 +0000934 GenericValue Dest;
935 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
936 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
937 else
938 Dest.IntVal = Src1.IntVal;
939
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000940 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000941}
942
Reid Spencera54b7cb2007-01-12 07:05:14 +0000943GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
944 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000945 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000946 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000947 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +0000948 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
Chris Lattnera34c5682002-08-27 22:33:45 +0000949 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000950}
Chris Lattner92101ac2001-08-23 17:05:04 +0000951
Reid Spencera54b7cb2007-01-12 07:05:14 +0000952GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
953 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000954 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
955 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000956 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +0000957 Dest.IntVal = Src.IntVal.sext(DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000958 return Dest;
959}
960
961GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
962 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000963 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
964 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000965 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +0000966 Dest.IntVal = Src.IntVal.zext(DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000967 return Dest;
968}
969
970GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
971 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000972 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattneraee7f212008-04-06 21:50:58 +0000973 assert(SrcVal->getType() == Type::DoubleTy && DstTy == Type::FloatTy &&
Reid Spencera54b7cb2007-01-12 07:05:14 +0000974 "Invalid FPTrunc instruction");
975 Dest.FloatVal = (float) Src.DoubleVal;
976 return Dest;
977}
978
979GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
980 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000981 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattneraee7f212008-04-06 21:50:58 +0000982 assert(SrcVal->getType() == Type::FloatTy && DstTy == Type::DoubleTy &&
Reid Spencera54b7cb2007-01-12 07:05:14 +0000983 "Invalid FPTrunc instruction");
984 Dest.DoubleVal = (double) Src.FloatVal;
985 return Dest;
986}
987
988GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
989 ExecutionContext &SF) {
990 const Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +0000991 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000992 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000993 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
Reid Spencere0929362007-03-03 08:38:04 +0000994
Reid Spencera54b7cb2007-01-12 07:05:14 +0000995 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +0000996 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000997 else
Reid Spencerf9536332007-03-06 03:09:31 +0000998 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000999 return Dest;
1000}
1001
1002GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1003 ExecutionContext &SF) {
1004 const Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +00001005 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001006 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001007 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001008
Reid Spencera54b7cb2007-01-12 07:05:14 +00001009 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001010 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001011 else
Reid Spencerf9536332007-03-06 03:09:31 +00001012 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001013 return Dest;
1014}
1015
1016GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1017 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001018 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001019 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001020
Reid Spencera54b7cb2007-01-12 07:05:14 +00001021 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001022 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001023 else
Reid Spencerf9536332007-03-06 03:09:31 +00001024 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001025 return Dest;
1026}
1027
1028GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1029 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001030 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencer24d6da52007-01-21 00:29:26 +00001031 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001032
Reid Spencera54b7cb2007-01-12 07:05:14 +00001033 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001034 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001035 else
Reid Spencerf9536332007-03-06 03:09:31 +00001036 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001037 return Dest;
Reid Spencerf9536332007-03-06 03:09:31 +00001038
Reid Spencera54b7cb2007-01-12 07:05:14 +00001039}
1040
1041GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1042 ExecutionContext &SF) {
Reid Spencerf9536332007-03-06 03:09:31 +00001043 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001044 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattneraee7f212008-04-06 21:50:58 +00001045 assert(isa<PointerType>(SrcVal->getType()) && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001046
Reid Spencerf9536332007-03-06 03:09:31 +00001047 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001048 return Dest;
1049}
1050
1051GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1052 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001053 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001054 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001055
Reid Spencer57631052007-03-06 03:46:41 +00001056 uint32_t PtrSize = TD.getPointerSizeInBits();
Reid Spencer7553c342007-03-06 03:41:50 +00001057 if (PtrSize != Src.IntVal.getBitWidth())
Reid Spencer57631052007-03-06 03:46:41 +00001058 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
Reid Spencer7553c342007-03-06 03:41:50 +00001059
Reid Spencer6bc63332007-04-26 18:19:35 +00001060 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
Reid Spencera54b7cb2007-01-12 07:05:14 +00001061 return Dest;
1062}
1063
1064GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1065 ExecutionContext &SF) {
1066
1067 const Type *SrcTy = SrcVal->getType();
1068 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1069 if (isa<PointerType>(DstTy)) {
1070 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1071 Dest.PointerVal = Src.PointerVal;
Chris Lattner42a75512007-01-15 02:27:26 +00001072 } else if (DstTy->isInteger()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001073 if (SrcTy == Type::FloatTy) {
Dan Gohmande551f92009-04-01 18:45:54 +00001074 Dest.IntVal.zext(sizeof(Src.FloatVal) * CHAR_BIT);
Reid Spencerf9536332007-03-06 03:09:31 +00001075 Dest.IntVal.floatToBits(Src.FloatVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001076 } else if (SrcTy == Type::DoubleTy) {
Dan Gohmande551f92009-04-01 18:45:54 +00001077 Dest.IntVal.zext(sizeof(Src.DoubleVal) * CHAR_BIT);
Reid Spencerf9536332007-03-06 03:09:31 +00001078 Dest.IntVal.doubleToBits(Src.DoubleVal);
Chris Lattner42a75512007-01-15 02:27:26 +00001079 } else if (SrcTy->isInteger()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001080 Dest.IntVal = Src.IntVal;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001081 } else
1082 assert(0 && "Invalid BitCast");
1083 } else if (DstTy == Type::FloatTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001084 if (SrcTy->isInteger())
Reid Spencerf9536332007-03-06 03:09:31 +00001085 Dest.FloatVal = Src.IntVal.bitsToFloat();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001086 else
1087 Dest.FloatVal = Src.FloatVal;
1088 } else if (DstTy == Type::DoubleTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001089 if (SrcTy->isInteger())
Reid Spencerf9536332007-03-06 03:09:31 +00001090 Dest.DoubleVal = Src.IntVal.bitsToDouble();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001091 else
1092 Dest.DoubleVal = Src.DoubleVal;
1093 } else
1094 assert(0 && "Invalid Bitcast");
1095
1096 return Dest;
1097}
1098
1099void Interpreter::visitTruncInst(TruncInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001100 ExecutionContext &SF = ECStack.back();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001101 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1102}
1103
1104void Interpreter::visitSExtInst(SExtInst &I) {
1105 ExecutionContext &SF = ECStack.back();
1106 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1107}
1108
1109void Interpreter::visitZExtInst(ZExtInst &I) {
1110 ExecutionContext &SF = ECStack.back();
1111 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1112}
1113
1114void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1115 ExecutionContext &SF = ECStack.back();
1116 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1117}
1118
1119void Interpreter::visitFPExtInst(FPExtInst &I) {
1120 ExecutionContext &SF = ECStack.back();
1121 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1122}
1123
1124void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1125 ExecutionContext &SF = ECStack.back();
1126 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1127}
1128
1129void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1130 ExecutionContext &SF = ECStack.back();
1131 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1132}
1133
1134void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1135 ExecutionContext &SF = ECStack.back();
1136 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1137}
1138
1139void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1140 ExecutionContext &SF = ECStack.back();
1141 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1142}
1143
1144void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1145 ExecutionContext &SF = ECStack.back();
1146 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1147}
1148
1149void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1150 ExecutionContext &SF = ECStack.back();
1151 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1152}
1153
1154void Interpreter::visitBitCastInst(BitCastInst &I) {
1155 ExecutionContext &SF = ECStack.back();
1156 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001157}
Chris Lattner92101ac2001-08-23 17:05:04 +00001158
Brian Gaekec1a2be12003-11-07 21:20:47 +00001159#define IMPLEMENT_VAARG(TY) \
1160 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1161
1162void Interpreter::visitVAArgInst(VAArgInst &I) {
1163 ExecutionContext &SF = ECStack.back();
1164
Brian Gaeke9d20b712004-02-25 23:01:48 +00001165 // Get the incoming valist parameter. LLI treats the valist as a
1166 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001167 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001168 GenericValue Dest;
1169 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Reid Spencerf9536332007-03-06 03:09:31 +00001170 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001171 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001172 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001173 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001174 IMPLEMENT_VAARG(Pointer);
1175 IMPLEMENT_VAARG(Float);
1176 IMPLEMENT_VAARG(Double);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001177 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001178 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001179 llvm_unreachable();
Brian Gaekec1a2be12003-11-07 21:20:47 +00001180 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001181
Brian Gaekec1a2be12003-11-07 21:20:47 +00001182 // Set the Value of this Instruction.
1183 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001184
1185 // Move the pointer to the next vararg.
1186 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001187}
1188
Reid Spencere1aa0662007-03-03 06:22:22 +00001189GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1190 ExecutionContext &SF) {
1191 switch (CE->getOpcode()) {
1192 case Instruction::Trunc:
1193 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1194 case Instruction::ZExt:
1195 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1196 case Instruction::SExt:
1197 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1198 case Instruction::FPTrunc:
1199 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1200 case Instruction::FPExt:
1201 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1202 case Instruction::UIToFP:
1203 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1204 case Instruction::SIToFP:
1205 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1206 case Instruction::FPToUI:
1207 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1208 case Instruction::FPToSI:
1209 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1210 case Instruction::PtrToInt:
1211 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1212 case Instruction::IntToPtr:
1213 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1214 case Instruction::BitCast:
1215 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1216 case Instruction::GetElementPtr:
1217 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1218 gep_type_end(CE), SF);
Reid Spencere0929362007-03-03 08:38:04 +00001219 case Instruction::FCmp:
1220 case Instruction::ICmp:
1221 return executeCmpInst(CE->getPredicate(),
1222 getOperandValue(CE->getOperand(0), SF),
1223 getOperandValue(CE->getOperand(1), SF),
1224 CE->getOperand(0)->getType());
1225 case Instruction::Select:
1226 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1227 getOperandValue(CE->getOperand(1), SF),
1228 getOperandValue(CE->getOperand(2), SF));
Reid Spencere1aa0662007-03-03 06:22:22 +00001229 default :
1230 break;
1231 }
Reid Spencere0929362007-03-03 08:38:04 +00001232
1233 // The cases below here require a GenericValue parameter for the result
1234 // so we initialize one, compute it and then return it.
Reid Spencerf9536332007-03-06 03:09:31 +00001235 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1236 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
Reid Spencere1aa0662007-03-03 06:22:22 +00001237 GenericValue Dest;
Reid Spencerf9536332007-03-06 03:09:31 +00001238 const Type * Ty = CE->getOperand(0)->getType();
Reid Spencere1aa0662007-03-03 06:22:22 +00001239 switch (CE->getOpcode()) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001240 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
1241 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
1242 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
1243 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
1244 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
1245 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
Reid Spencerf9536332007-03-06 03:09:31 +00001246 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1247 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1248 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1249 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1250 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1251 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001252 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
1253 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
1254 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
Reid Spencerf9536332007-03-06 03:09:31 +00001255 case Instruction::Shl:
1256 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1257 break;
1258 case Instruction::LShr:
1259 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1260 break;
1261 case Instruction::AShr:
1262 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1263 break;
Reid Spencere1aa0662007-03-03 06:22:22 +00001264 default:
1265 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001266 llvm_unreachable();
Reid Spencere1aa0662007-03-03 06:22:22 +00001267 return GenericValue();
1268 }
Reid Spencere0929362007-03-03 08:38:04 +00001269 return Dest;
Reid Spencere1aa0662007-03-03 06:22:22 +00001270}
1271
1272GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1273 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1274 return getConstantExprValue(CE, SF);
1275 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1276 return getConstantValue(CPV);
1277 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1278 return PTOGV(getPointerToGlobal(GV));
1279 } else {
1280 return SF.Values[V];
1281 }
1282}
1283
Chris Lattner92101ac2001-08-23 17:05:04 +00001284//===----------------------------------------------------------------------===//
1285// Dispatch and Execution Code
1286//===----------------------------------------------------------------------===//
1287
Chris Lattner92101ac2001-08-23 17:05:04 +00001288//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001289// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001290//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001291void Interpreter::callFunction(Function *F,
1292 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001293 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1294 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1295 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001296 // Make a new stack frame... and fill it in.
1297 ECStack.push_back(ExecutionContext());
1298 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001299 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001300
1301 // Special handling for external functions.
Reid Spencer5cbf9852007-01-30 20:08:39 +00001302 if (F->isDeclaration()) {
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001303 GenericValue Result = callExternalFunction (F, ArgVals);
1304 // Simulate a 'ret' instruction of the appropriate type.
1305 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1306 return;
1307 }
1308
1309 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001310 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001311 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001312
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001313 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001314 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001315 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001316 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001317
1318 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001319 unsigned i = 0;
Reid Spencer4ccf4622007-04-16 21:50:40 +00001320 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1321 AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001322 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001323
1324 // Handle varargs arguments...
1325 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001326}
1327
Reid Spencer951418b2007-05-16 02:05:13 +00001328
Chris Lattner92101ac2001-08-23 17:05:04 +00001329void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001330 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001331 // Interpret a single instruction & increment the "PC".
1332 ExecutionContext &SF = ECStack.back(); // Current stack frame
1333 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001334
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001335 // Track the number of dynamic instructions executed.
1336 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001337
Bill Wendling480f0932006-11-27 23:54:50 +00001338 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001339 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner27725bf2007-09-21 18:30:39 +00001340#if 0
1341 // This is not safe, as visiting the instruction could lower it and free I.
Reid Spencer951418b2007-05-16 02:05:13 +00001342#ifndef NDEBUG
1343 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1344 I.getType() != Type::VoidTy) {
1345 DOUT << " --> ";
Chris Lattner27725bf2007-09-21 18:30:39 +00001346 const GenericValue &Val = SF.Values[&I];
1347 switch (I.getType()->getTypeID()) {
1348 default: assert(0 && "Invalid GenericValue Type");
1349 case Type::VoidTyID: DOUT << "void"; break;
1350 case Type::FloatTyID: DOUT << "float " << Val.FloatVal; break;
1351 case Type::DoubleTyID: DOUT << "double " << Val.DoubleVal; break;
1352 case Type::PointerTyID: DOUT << "void* " << intptr_t(Val.PointerVal);
1353 break;
1354 case Type::IntegerTyID:
1355 DOUT << "i" << Val.IntVal.getBitWidth() << " "
1356 << Val.IntVal.toStringUnsigned(10)
1357 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
1358 break;
1359 }
Reid Spencer951418b2007-05-16 02:05:13 +00001360 }
1361#endif
Chris Lattner27725bf2007-09-21 18:30:39 +00001362#endif
Chris Lattner92101ac2001-08-23 17:05:04 +00001363 }
1364}