blob: 45180c91f07d5b4e59f00f93071921683dc66a94 [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 Edwinc23197a2009-07-14 16:55:14 +000061 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +000072 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +000083 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +000094 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000109 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000136 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000149 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000162 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000175 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000188 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000201 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000214 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000227 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000240 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000253 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000278 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000297 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000311 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000324 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000337 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000350 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000363 llvm_unreachable(0);
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) \
Owen Anderson1d0be152009-08-13 21:58:54 +0000369 if (TY == Type::getFloatTy(Ty->getContext())) { \
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000370 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;
Owen Anderson1d0be152009-08-13 21:58:54 +0000425 if (Ty == Type::getFloatTy(Ty->getContext()))
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;
Owen Anderson1d0be152009-08-13 21:58:54 +0000437 if (Ty == Type::getFloatTy(Ty->getContext()))
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 Edwinc23197a2009-07-14 16:55:14 +0000472 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000518 llvm_unreachable(0);
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 Edwinc23197a2009-07-14 16:55:14 +0000547 llvm_unreachable(0);
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()) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000605 // Save result...
606 if (CallingSF.Caller.getType() != Type::getVoidTy(RetTy->getContext()))
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000607 SetValue(I, Result, CallingSF);
608 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
609 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000610 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000611 }
612 }
613}
614
Chris Lattnerd7916e92003-05-10 21:22:39 +0000615void Interpreter::visitReturnInst(ReturnInst &I) {
616 ExecutionContext &SF = ECStack.back();
Owen Anderson1d0be152009-08-13 21:58:54 +0000617 const Type *RetTy = Type::getVoidTy(I.getContext());
Chris Lattner92101ac2001-08-23 17:05:04 +0000618 GenericValue Result;
619
620 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000621 if (I.getNumOperands()) {
622 RetTy = I.getReturnValue()->getType();
623 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000624 }
625
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000626 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000627}
628
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000629void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000630 // Unwind stack
631 Instruction *Inst;
632 do {
633 ECStack.pop_back ();
634 if (ECStack.empty ())
Torok Edwin7d696d82009-07-11 13:10:19 +0000635 llvm_report_error("Empty stack during unwind!");
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000636 Inst = ECStack.back ().Caller.getInstruction ();
637 } while (!(Inst && isa<InvokeInst> (Inst)));
638
639 // Return from invoke
640 ExecutionContext &InvokingSF = ECStack.back ();
641 InvokingSF.Caller = CallSite ();
642
643 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000644 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000645}
646
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000647void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +0000648 llvm_report_error("Program executed an 'unreachable' instruction!");
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000649}
650
Chris Lattnerd7916e92003-05-10 21:22:39 +0000651void Interpreter::visitBranchInst(BranchInst &I) {
652 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000653 BasicBlock *Dest;
654
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000655 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
656 if (!I.isUnconditional()) {
657 Value *Cond = I.getCondition();
Reid Spencerf9536332007-03-06 03:09:31 +0000658 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000659 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000660 }
Chris Lattner77113b62003-05-10 20:21:16 +0000661 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000662}
663
Chris Lattnerd7916e92003-05-10 21:22:39 +0000664void Interpreter::visitSwitchInst(SwitchInst &I) {
665 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000666 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
667 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000668
669 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000670 BasicBlock *Dest = 0;
671 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Reid Spencerf9536332007-03-06 03:09:31 +0000672 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
673 .IntVal != 0) {
Chris Lattner09e93922003-04-22 20:34:47 +0000674 Dest = cast<BasicBlock>(I.getOperand(i+1));
675 break;
676 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000677
Chris Lattner09e93922003-04-22 20:34:47 +0000678 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000679 SwitchToNewBasicBlock(Dest, SF);
680}
681
682// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
683// This function handles the actual updating of block and instruction iterators
684// as well as execution of all of the PHI nodes in the destination block.
685//
686// This method does this because all of the PHI nodes must be executed
687// atomically, reading their inputs before any of the results are updated. Not
688// doing this can cause problems if the PHI nodes depend on other PHI nodes for
689// their inputs. If the input PHI node is updated before it is read, incorrect
690// results can happen. Thus we use a two phase approach.
691//
692void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
693 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
694 SF.CurBB = Dest; // Update CurBB to branch destination
695 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
696
697 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
698
699 // Loop over all of the PHI nodes in the current block, reading their inputs.
700 std::vector<GenericValue> ResultValues;
701
702 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
703 // Search for the value corresponding to this previous bb...
704 int i = PN->getBasicBlockIndex(PrevBB);
705 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
706 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000707
Chris Lattner77113b62003-05-10 20:21:16 +0000708 // Save the incoming value for this PHI node...
709 ResultValues.push_back(getOperandValue(IncomingValue, SF));
710 }
711
712 // Now loop over all of the PHI nodes setting their values...
713 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000714 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
715 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000716 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000717 }
Chris Lattner09e93922003-04-22 20:34:47 +0000718}
719
Chris Lattner92101ac2001-08-23 17:05:04 +0000720//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000721// Memory Instruction Implementations
722//===----------------------------------------------------------------------===//
723
Chris Lattnerd7916e92003-05-10 21:22:39 +0000724void Interpreter::visitAllocationInst(AllocationInst &I) {
725 ExecutionContext &SF = ECStack.back();
726
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000727 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000728
Chris Lattnercc82cc12002-04-28 21:57:33 +0000729 // Get the number of elements being allocated by the array...
Reid Spencerf9536332007-03-06 03:09:31 +0000730 unsigned NumElements =
731 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
732
Duncan Sands777d2302009-05-09 07:06:46 +0000733 unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
Reid Spencerf9536332007-03-06 03:09:31 +0000734
Gabor Greif724441e2007-10-11 19:40:35 +0000735 // Avoid malloc-ing zero bytes, use max()...
736 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
Chris Lattner86660982001-08-27 05:16:50 +0000737
738 // Allocate enough memory to hold the type...
Reid Spencerf9536332007-03-06 03:09:31 +0000739 void *Memory = malloc(MemToAlloc);
740
741 DOUT << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
742 << NumElements << " (Total: " << MemToAlloc << ") at "
Bill Wendling47992ea2007-03-08 23:37:24 +0000743 << uintptr_t(Memory) << '\n';
Chris Lattner9bffa732002-02-19 18:50:09 +0000744
Chris Lattnerfe11a972002-12-23 23:59:41 +0000745 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000746 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000747 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000748
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000749 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000750 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000751}
752
Chris Lattnerd7916e92003-05-10 21:22:39 +0000753void Interpreter::visitFreeInst(FreeInst &I) {
754 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000755 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
756 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000757 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000758 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000759}
760
Chris Lattnera34c5682002-08-27 22:33:45 +0000761// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000762//
Chris Lattner4af6de82003-11-25 20:44:56 +0000763GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000764 gep_type_iterator E,
765 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000766 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000767 "Cannot getElementOffset of a nonpointer type!");
768
Reid Spencerf9536332007-03-06 03:09:31 +0000769 uint64_t Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000770
771 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000772 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000773 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000774
Reid Spencerb83eb642006-10-20 07:07:24 +0000775 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
776 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000777
Reid Spencerf9536332007-03-06 03:09:31 +0000778 Total += SLO->getElementOffset(Index);
Chris Lattner4af6de82003-11-25 20:44:56 +0000779 } else {
780 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000781 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000782 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
783
Reid Spencera54b7cb2007-01-12 07:05:14 +0000784 int64_t Idx;
785 unsigned BitWidth =
786 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
Reid Spencer23e28832007-01-18 01:25:42 +0000787 if (BitWidth == 32)
Reid Spencerf9536332007-03-06 03:09:31 +0000788 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
Chris Lattneraee7f212008-04-06 21:50:58 +0000789 else {
790 assert(BitWidth == 64 && "Invalid index type for getelementptr");
Reid Spencerf9536332007-03-06 03:09:31 +0000791 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
Chris Lattneraee7f212008-04-06 21:50:58 +0000792 }
Duncan Sands777d2302009-05-09 07:06:46 +0000793 Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000794 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000795 }
796
Chris Lattnera34c5682002-08-27 22:33:45 +0000797 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +0000798 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
799 DOUT << "GEP Index " << Total << " bytes.\n";
Chris Lattnera34c5682002-08-27 22:33:45 +0000800 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000801}
802
Chris Lattnerd7916e92003-05-10 21:22:39 +0000803void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
804 ExecutionContext &SF = ECStack.back();
Owen Andersonfdca74c2009-06-26 16:46:15 +0000805 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000806 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000807}
808
Chris Lattnerd7916e92003-05-10 21:22:39 +0000809void Interpreter::visitLoadInst(LoadInst &I) {
810 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000811 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000812 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Reid Spencere0929362007-03-03 08:38:04 +0000813 GenericValue Result;
Reid Spencere0929362007-03-03 08:38:04 +0000814 LoadValueFromMemory(Result, Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000815 SetValue(&I, Result, SF);
Chris Lattner663ceeb2008-07-08 17:25:49 +0000816 if (I.isVolatile() && PrintVolatile)
817 cerr << "Volatile load " << I;
Chris Lattner86660982001-08-27 05:16:50 +0000818}
819
Chris Lattnerd7916e92003-05-10 21:22:39 +0000820void Interpreter::visitStoreInst(StoreInst &I) {
821 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000822 GenericValue Val = getOperandValue(I.getOperand(0), SF);
823 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000824 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000825 I.getOperand(0)->getType());
Chris Lattner663ceeb2008-07-08 17:25:49 +0000826 if (I.isVolatile() && PrintVolatile)
827 cerr << "Volatile store: " << I;
Chris Lattnerfddc7552002-10-15 20:34:05 +0000828}
829
Chris Lattner86660982001-08-27 05:16:50 +0000830//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000831// Miscellaneous Instruction Implementations
832//===----------------------------------------------------------------------===//
833
Brian Gaekefea483d2003-11-07 20:04:22 +0000834void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000835 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000836
837 // Check to see if this is an intrinsic function call...
Reid Spencer4ccf4622007-04-16 21:50:40 +0000838 Function *F = CS.getCalledFunction();
839 if (F && F->isDeclaration ())
Chris Lattner73011782003-12-28 09:44:37 +0000840 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000841 case Intrinsic::not_intrinsic:
842 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000843 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000844 GenericValue ArgIndex;
845 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
846 ArgIndex.UIntPairVal.second = 0;
847 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000848 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000849 }
Chris Lattner317201d2004-03-13 00:24:00 +0000850 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000851 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000852 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000853 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
854 return;
855 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000856 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000857 // class to transform it into hopefully tasty LLVM code.
858 //
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000859 BasicBlock::iterator me(CS.getInstruction());
Chris Lattner73011782003-12-28 09:44:37 +0000860 BasicBlock *Parent = CS.getInstruction()->getParent();
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000861 bool atBegin(Parent->begin() == me);
862 if (!atBegin)
863 --me;
Chris Lattner73011782003-12-28 09:44:37 +0000864 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
865
866 // Restore the CurInst pointer to the first instruction newly inserted, if
867 // any.
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000868 if (atBegin) {
Chris Lattner73011782003-12-28 09:44:37 +0000869 SF.CurInst = Parent->begin();
870 } else {
Chris Lattnerb8e237b2007-04-17 17:38:28 +0000871 SF.CurInst = me;
Chris Lattner73011782003-12-28 09:44:37 +0000872 ++SF.CurInst;
873 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000874 return;
Chris Lattner73011782003-12-28 09:44:37 +0000875 }
876
Reid Spencer4ccf4622007-04-16 21:50:40 +0000877
Brian Gaekefea483d2003-11-07 20:04:22 +0000878 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000879 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000880 const unsigned NumArgs = SF.Caller.arg_size();
881 ArgVals.reserve(NumArgs);
Reid Spencer4ccf4622007-04-16 21:50:40 +0000882 uint16_t pNum = 1;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000883 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
Reid Spencer4ccf4622007-04-16 21:50:40 +0000884 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
Brian Gaekeae2495a2003-11-07 19:59:08 +0000885 Value *V = *i;
886 ArgVals.push_back(getOperandValue(V, SF));
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000887 // Promote all integral types whose size is < sizeof(i32) into i32.
888 // We do this by zero or sign extending the value as appropriate
889 // according to the parameter attributes
890 const Type *Ty = V->getType();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000891 if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32)) {
Devang Patel05988662008-09-25 21:00:45 +0000892 if (CS.paramHasAttr(pNum, Attribute::ZExt))
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000893 ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
Devang Patel05988662008-09-25 21:00:45 +0000894 else if (CS.paramHasAttr(pNum, Attribute::SExt))
Duncan Sandsafa3b6d2007-11-28 17:07:01 +0000895 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000896 }
Chris Lattner93780132003-01-13 00:58:52 +0000897 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000898
Misha Brukmand1c881a2005-04-21 22:43:08 +0000899 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000900 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000901 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000902 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000903}
904
Reid Spencer832254e2007-02-02 02:16:23 +0000905void Interpreter::visitShl(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000906 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000907 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
908 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
909 GenericValue Dest;
Chris Lattner9029cb82009-01-16 20:17:02 +0000910 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
911 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
912 else
913 Dest.IntVal = Src1.IntVal;
914
Brian Gaeke63438cc2003-12-11 00:22:59 +0000915 SetValue(&I, Dest, SF);
916}
917
Reid Spencer832254e2007-02-02 02:16:23 +0000918void Interpreter::visitLShr(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000919 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000920 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
921 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
922 GenericValue Dest;
Chris Lattner9029cb82009-01-16 20:17:02 +0000923 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
924 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
925 else
926 Dest.IntVal = Src1.IntVal;
927
Reid Spencer3822ff52006-11-08 06:47:33 +0000928 SetValue(&I, Dest, SF);
929}
930
Reid Spencer832254e2007-02-02 02:16:23 +0000931void Interpreter::visitAShr(BinaryOperator &I) {
Reid Spencer3822ff52006-11-08 06:47:33 +0000932 ExecutionContext &SF = ECStack.back();
Reid Spencer3822ff52006-11-08 06:47:33 +0000933 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
934 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner9029cb82009-01-16 20:17:02 +0000935 GenericValue Dest;
936 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
937 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
938 else
939 Dest.IntVal = Src1.IntVal;
940
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000941 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000942}
943
Reid Spencera54b7cb2007-01-12 07:05:14 +0000944GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
945 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000946 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000947 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000948 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +0000949 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
Chris Lattnera34c5682002-08-27 22:33:45 +0000950 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000951}
Chris Lattner92101ac2001-08-23 17:05:04 +0000952
Reid Spencera54b7cb2007-01-12 07:05:14 +0000953GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
954 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000955 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
956 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000957 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +0000958 Dest.IntVal = Src.IntVal.sext(DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000959 return Dest;
960}
961
962GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
963 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000964 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
965 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000966 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +0000967 Dest.IntVal = Src.IntVal.zext(DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000968 return Dest;
969}
970
971GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
972 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000973 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Owen Anderson1d0be152009-08-13 21:58:54 +0000974 assert(SrcVal->getType() == Type::getDoubleTy(SrcVal->getContext()) &&
975 DstTy == Type::getFloatTy(SrcVal->getContext()) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +0000976 "Invalid FPTrunc instruction");
977 Dest.FloatVal = (float) Src.DoubleVal;
978 return Dest;
979}
980
981GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
982 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000983 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Owen Anderson1d0be152009-08-13 21:58:54 +0000984 assert(SrcVal->getType() == Type::getFloatTy(SrcVal->getContext()) &&
985 DstTy == Type::getDoubleTy(SrcVal->getContext()) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +0000986 "Invalid FPTrunc instruction");
987 Dest.DoubleVal = (double) Src.FloatVal;
988 return Dest;
989}
990
991GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
992 ExecutionContext &SF) {
993 const Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +0000994 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000995 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000996 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
Reid Spencere0929362007-03-03 08:38:04 +0000997
Reid Spencera54b7cb2007-01-12 07:05:14 +0000998 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +0000999 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001000 else
Reid Spencerf9536332007-03-06 03:09:31 +00001001 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001002 return Dest;
1003}
1004
1005GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1006 ExecutionContext &SF) {
1007 const Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +00001008 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001009 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001010 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001011
Reid Spencera54b7cb2007-01-12 07:05:14 +00001012 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001013 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001014 else
Reid Spencerf9536332007-03-06 03:09:31 +00001015 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001016 return Dest;
1017}
1018
1019GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1020 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001021 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001022 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001023
Reid Spencera54b7cb2007-01-12 07:05:14 +00001024 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001025 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001026 else
Reid Spencerf9536332007-03-06 03:09:31 +00001027 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001028 return Dest;
1029}
1030
1031GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1032 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001033 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencer24d6da52007-01-21 00:29:26 +00001034 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001035
Reid Spencera54b7cb2007-01-12 07:05:14 +00001036 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001037 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001038 else
Reid Spencerf9536332007-03-06 03:09:31 +00001039 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001040 return Dest;
Reid Spencerf9536332007-03-06 03:09:31 +00001041
Reid Spencera54b7cb2007-01-12 07:05:14 +00001042}
1043
1044GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1045 ExecutionContext &SF) {
Reid Spencerf9536332007-03-06 03:09:31 +00001046 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001047 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattneraee7f212008-04-06 21:50:58 +00001048 assert(isa<PointerType>(SrcVal->getType()) && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001049
Reid Spencerf9536332007-03-06 03:09:31 +00001050 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001051 return Dest;
1052}
1053
1054GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1055 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001056 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001057 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001058
Reid Spencer57631052007-03-06 03:46:41 +00001059 uint32_t PtrSize = TD.getPointerSizeInBits();
Reid Spencer7553c342007-03-06 03:41:50 +00001060 if (PtrSize != Src.IntVal.getBitWidth())
Reid Spencer57631052007-03-06 03:46:41 +00001061 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
Reid Spencer7553c342007-03-06 03:41:50 +00001062
Reid Spencer6bc63332007-04-26 18:19:35 +00001063 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
Reid Spencera54b7cb2007-01-12 07:05:14 +00001064 return Dest;
1065}
1066
1067GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1068 ExecutionContext &SF) {
1069
1070 const Type *SrcTy = SrcVal->getType();
1071 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1072 if (isa<PointerType>(DstTy)) {
1073 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1074 Dest.PointerVal = Src.PointerVal;
Chris Lattner42a75512007-01-15 02:27:26 +00001075 } else if (DstTy->isInteger()) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001076 if (SrcTy == Type::getFloatTy(SrcVal->getContext())) {
Dan Gohmande551f92009-04-01 18:45:54 +00001077 Dest.IntVal.zext(sizeof(Src.FloatVal) * CHAR_BIT);
Reid Spencerf9536332007-03-06 03:09:31 +00001078 Dest.IntVal.floatToBits(Src.FloatVal);
Owen Anderson1d0be152009-08-13 21:58:54 +00001079 } else if (SrcTy == Type::getDoubleTy(SrcVal->getContext())) {
Dan Gohmande551f92009-04-01 18:45:54 +00001080 Dest.IntVal.zext(sizeof(Src.DoubleVal) * CHAR_BIT);
Reid Spencerf9536332007-03-06 03:09:31 +00001081 Dest.IntVal.doubleToBits(Src.DoubleVal);
Chris Lattner42a75512007-01-15 02:27:26 +00001082 } else if (SrcTy->isInteger()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001083 Dest.IntVal = Src.IntVal;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001084 } else
Torok Edwinc23197a2009-07-14 16:55:14 +00001085 llvm_unreachable("Invalid BitCast");
Owen Anderson1d0be152009-08-13 21:58:54 +00001086 } else if (DstTy == Type::getFloatTy(SrcVal->getContext())) {
Chris Lattner42a75512007-01-15 02:27:26 +00001087 if (SrcTy->isInteger())
Reid Spencerf9536332007-03-06 03:09:31 +00001088 Dest.FloatVal = Src.IntVal.bitsToFloat();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001089 else
1090 Dest.FloatVal = Src.FloatVal;
Owen Anderson1d0be152009-08-13 21:58:54 +00001091 } else if (DstTy == Type::getDoubleTy(SrcVal->getContext())) {
Chris Lattner42a75512007-01-15 02:27:26 +00001092 if (SrcTy->isInteger())
Reid Spencerf9536332007-03-06 03:09:31 +00001093 Dest.DoubleVal = Src.IntVal.bitsToDouble();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001094 else
1095 Dest.DoubleVal = Src.DoubleVal;
1096 } else
Torok Edwinc23197a2009-07-14 16:55:14 +00001097 llvm_unreachable("Invalid Bitcast");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001098
1099 return Dest;
1100}
1101
1102void Interpreter::visitTruncInst(TruncInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001103 ExecutionContext &SF = ECStack.back();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001104 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1105}
1106
1107void Interpreter::visitSExtInst(SExtInst &I) {
1108 ExecutionContext &SF = ECStack.back();
1109 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1110}
1111
1112void Interpreter::visitZExtInst(ZExtInst &I) {
1113 ExecutionContext &SF = ECStack.back();
1114 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1115}
1116
1117void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1118 ExecutionContext &SF = ECStack.back();
1119 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1120}
1121
1122void Interpreter::visitFPExtInst(FPExtInst &I) {
1123 ExecutionContext &SF = ECStack.back();
1124 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1125}
1126
1127void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1128 ExecutionContext &SF = ECStack.back();
1129 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1130}
1131
1132void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1133 ExecutionContext &SF = ECStack.back();
1134 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1135}
1136
1137void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1138 ExecutionContext &SF = ECStack.back();
1139 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1140}
1141
1142void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1143 ExecutionContext &SF = ECStack.back();
1144 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1145}
1146
1147void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1148 ExecutionContext &SF = ECStack.back();
1149 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1150}
1151
1152void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1153 ExecutionContext &SF = ECStack.back();
1154 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1155}
1156
1157void Interpreter::visitBitCastInst(BitCastInst &I) {
1158 ExecutionContext &SF = ECStack.back();
1159 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001160}
Chris Lattner92101ac2001-08-23 17:05:04 +00001161
Brian Gaekec1a2be12003-11-07 21:20:47 +00001162#define IMPLEMENT_VAARG(TY) \
1163 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1164
1165void Interpreter::visitVAArgInst(VAArgInst &I) {
1166 ExecutionContext &SF = ECStack.back();
1167
Brian Gaeke9d20b712004-02-25 23:01:48 +00001168 // Get the incoming valist parameter. LLI treats the valist as a
1169 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001170 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001171 GenericValue Dest;
1172 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Reid Spencerf9536332007-03-06 03:09:31 +00001173 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001174 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001175 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001176 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001177 IMPLEMENT_VAARG(Pointer);
1178 IMPLEMENT_VAARG(Float);
1179 IMPLEMENT_VAARG(Double);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001180 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001181 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001182 llvm_unreachable(0);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001183 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001184
Brian Gaekec1a2be12003-11-07 21:20:47 +00001185 // Set the Value of this Instruction.
1186 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001187
1188 // Move the pointer to the next vararg.
1189 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001190}
1191
Reid Spencere1aa0662007-03-03 06:22:22 +00001192GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1193 ExecutionContext &SF) {
1194 switch (CE->getOpcode()) {
1195 case Instruction::Trunc:
1196 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1197 case Instruction::ZExt:
1198 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1199 case Instruction::SExt:
1200 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1201 case Instruction::FPTrunc:
1202 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1203 case Instruction::FPExt:
1204 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1205 case Instruction::UIToFP:
1206 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1207 case Instruction::SIToFP:
1208 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1209 case Instruction::FPToUI:
1210 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1211 case Instruction::FPToSI:
1212 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1213 case Instruction::PtrToInt:
1214 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1215 case Instruction::IntToPtr:
1216 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1217 case Instruction::BitCast:
1218 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1219 case Instruction::GetElementPtr:
1220 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1221 gep_type_end(CE), SF);
Reid Spencere0929362007-03-03 08:38:04 +00001222 case Instruction::FCmp:
1223 case Instruction::ICmp:
1224 return executeCmpInst(CE->getPredicate(),
1225 getOperandValue(CE->getOperand(0), SF),
1226 getOperandValue(CE->getOperand(1), SF),
1227 CE->getOperand(0)->getType());
1228 case Instruction::Select:
1229 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1230 getOperandValue(CE->getOperand(1), SF),
1231 getOperandValue(CE->getOperand(2), SF));
Reid Spencere1aa0662007-03-03 06:22:22 +00001232 default :
1233 break;
1234 }
Reid Spencere0929362007-03-03 08:38:04 +00001235
1236 // The cases below here require a GenericValue parameter for the result
1237 // so we initialize one, compute it and then return it.
Reid Spencerf9536332007-03-06 03:09:31 +00001238 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1239 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
Reid Spencere1aa0662007-03-03 06:22:22 +00001240 GenericValue Dest;
Reid Spencerf9536332007-03-06 03:09:31 +00001241 const Type * Ty = CE->getOperand(0)->getType();
Reid Spencere1aa0662007-03-03 06:22:22 +00001242 switch (CE->getOpcode()) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001243 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
1244 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
1245 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
1246 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
1247 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
1248 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
Reid Spencerf9536332007-03-06 03:09:31 +00001249 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1250 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1251 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1252 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1253 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1254 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001255 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
1256 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
1257 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
Reid Spencerf9536332007-03-06 03:09:31 +00001258 case Instruction::Shl:
1259 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1260 break;
1261 case Instruction::LShr:
1262 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1263 break;
1264 case Instruction::AShr:
1265 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1266 break;
Reid Spencere1aa0662007-03-03 06:22:22 +00001267 default:
1268 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001269 llvm_unreachable(0);
Reid Spencere1aa0662007-03-03 06:22:22 +00001270 return GenericValue();
1271 }
Reid Spencere0929362007-03-03 08:38:04 +00001272 return Dest;
Reid Spencere1aa0662007-03-03 06:22:22 +00001273}
1274
1275GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1276 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1277 return getConstantExprValue(CE, SF);
1278 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1279 return getConstantValue(CPV);
1280 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1281 return PTOGV(getPointerToGlobal(GV));
1282 } else {
1283 return SF.Values[V];
1284 }
1285}
1286
Chris Lattner92101ac2001-08-23 17:05:04 +00001287//===----------------------------------------------------------------------===//
1288// Dispatch and Execution Code
1289//===----------------------------------------------------------------------===//
1290
Chris Lattner92101ac2001-08-23 17:05:04 +00001291//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001292// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001293//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001294void Interpreter::callFunction(Function *F,
1295 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001296 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1297 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1298 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001299 // Make a new stack frame... and fill it in.
1300 ECStack.push_back(ExecutionContext());
1301 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001302 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001303
1304 // Special handling for external functions.
Reid Spencer5cbf9852007-01-30 20:08:39 +00001305 if (F->isDeclaration()) {
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001306 GenericValue Result = callExternalFunction (F, ArgVals);
1307 // Simulate a 'ret' instruction of the appropriate type.
1308 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1309 return;
1310 }
1311
1312 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001313 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001314 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001315
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001316 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001317 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001318 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001319 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001320
1321 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001322 unsigned i = 0;
Reid Spencer4ccf4622007-04-16 21:50:40 +00001323 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1324 AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001325 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001326
1327 // Handle varargs arguments...
1328 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001329}
1330
Reid Spencer951418b2007-05-16 02:05:13 +00001331
Chris Lattner92101ac2001-08-23 17:05:04 +00001332void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001333 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001334 // Interpret a single instruction & increment the "PC".
1335 ExecutionContext &SF = ECStack.back(); // Current stack frame
1336 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001337
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001338 // Track the number of dynamic instructions executed.
1339 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001340
Bill Wendling480f0932006-11-27 23:54:50 +00001341 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001342 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner27725bf2007-09-21 18:30:39 +00001343#if 0
1344 // This is not safe, as visiting the instruction could lower it and free I.
Reid Spencer951418b2007-05-16 02:05:13 +00001345#ifndef NDEBUG
1346 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1347 I.getType() != Type::VoidTy) {
1348 DOUT << " --> ";
Chris Lattner27725bf2007-09-21 18:30:39 +00001349 const GenericValue &Val = SF.Values[&I];
1350 switch (I.getType()->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001351 default: llvm_unreachable("Invalid GenericValue Type");
Chris Lattner27725bf2007-09-21 18:30:39 +00001352 case Type::VoidTyID: DOUT << "void"; break;
1353 case Type::FloatTyID: DOUT << "float " << Val.FloatVal; break;
1354 case Type::DoubleTyID: DOUT << "double " << Val.DoubleVal; break;
1355 case Type::PointerTyID: DOUT << "void* " << intptr_t(Val.PointerVal);
1356 break;
1357 case Type::IntegerTyID:
1358 DOUT << "i" << Val.IntVal.getBitWidth() << " "
1359 << Val.IntVal.toStringUnsigned(10)
1360 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
1361 break;
1362 }
Reid Spencer951418b2007-05-16 02:05:13 +00001363 }
1364#endif
Chris Lattner27725bf2007-09-21 18:30:39 +00001365#endif
Chris Lattner92101ac2001-08-23 17:05:04 +00001366 }
1367}