blob: 59ebe6e2a8850eec351f92cfd58c2597d83ea3a9 [file] [log] [blame]
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
Misha Brukman91fb9ab2005-04-21 22:43:08 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman91fb9ab2005-04-21 22:43:08 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukman91fb9ab2005-04-21 22:43:08 +00009//
Chris Lattnerd7ff5782001-08-23 17:05:04 +000010// This file contains the actual instruction interpreter.
11//
12//===----------------------------------------------------------------------===//
13
Brian Gaeke5a8ec7d2003-12-11 00:22:59 +000014#define DEBUG_TYPE "interpreter"
Chris Lattnerd7ff5782001-08-23 17:05:04 +000015#include "Interpreter.h"
Chris Lattnerca142372002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattnerc8c6c032003-12-28 09:44:37 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/Instructions.h"
Chris Lattnerbcdadf32004-06-20 07:49:54 +000019#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerf0788082003-11-25 20:44:56 +000020#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer54c6e842007-03-03 06:22:22 +000021#include "llvm/ADT/APInt.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/ADT/Statistic.h"
Chris Lattner2528d632008-07-08 17:25:49 +000023#include "llvm/Support/CommandLine.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000025#include "llvm/Support/ErrorHandling.h"
Reid Spencer7a9c62b2007-01-12 07:05:14 +000026#include "llvm/Support/MathExtras.h"
Gabor Greifcb6832e2007-10-11 19:40:35 +000027#include <algorithm>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000028#include <cmath>
Chris Lattnerf0788082003-11-25 20:44:56 +000029using namespace llvm;
Chris Lattnera0d7b082002-12-23 23:59:41 +000030
Chris Lattnere712a5a2006-12-19 22:56:53 +000031STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
Brian Gaeke960707c2003-11-11 22:41:34 +000032
Chris Lattner2528d632008-07-08 17:25:49 +000033static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
34 cl::desc("make the interpreter print every volatile load and store"));
35
Chris Lattnerc62e2e52001-10-15 05:51:48 +000036//===----------------------------------------------------------------------===//
Reid Spencer54c6e842007-03-03 06:22:22 +000037// Various Helper Functions
Chris Lattner78244c42001-10-15 13:25:40 +000038//===----------------------------------------------------------------------===//
Chris Lattnerc8c6c032003-12-28 09:44:37 +000039
Chris Lattner78244c42001-10-15 13:25:40 +000040static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaekee278c222003-10-24 19:59:01 +000041 SF.Values[V] = Val;
Chris Lattner78244c42001-10-15 13:25:40 +000042}
43
Chris Lattner79baf912002-05-03 19:52:30 +000044//===----------------------------------------------------------------------===//
Chris Lattnerd7ff5782001-08-23 17:05:04 +000045// Binary Instruction Implementations
46//===----------------------------------------------------------------------===//
47
48#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
Reid Spencer40015aa2007-03-06 03:09:31 +000049 case Type::TY##TyID: \
50 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
51 break
Chris Lattnerd7ff5782001-08-23 17:05:04 +000052
Dan Gohmana5b96452009-06-04 22:49:04 +000053static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
54 GenericValue Src2, const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000055 switch (Ty->getTypeID()) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +000056 IMPLEMENT_BINARY_OPERATOR(+, Float);
57 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerd7ff5782001-08-23 17:05:04 +000058 default:
David Greene8121a812010-01-05 01:27:23 +000059 dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +000060 llvm_unreachable(0);
Chris Lattnerd7ff5782001-08-23 17:05:04 +000061 }
Chris Lattnerd7ff5782001-08-23 17:05:04 +000062}
63
Dan Gohmana5b96452009-06-04 22:49:04 +000064static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
65 GenericValue Src2, const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000066 switch (Ty->getTypeID()) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +000067 IMPLEMENT_BINARY_OPERATOR(-, Float);
68 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerd7ff5782001-08-23 17:05:04 +000069 default:
David Greene8121a812010-01-05 01:27:23 +000070 dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +000071 llvm_unreachable(0);
Chris Lattnerd7ff5782001-08-23 17:05:04 +000072 }
Chris Lattnerd7ff5782001-08-23 17:05:04 +000073}
74
Dan Gohmana5b96452009-06-04 22:49:04 +000075static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
76 GenericValue Src2, const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000077 switch (Ty->getTypeID()) {
Chris Lattner0b00b312001-10-27 08:28:11 +000078 IMPLEMENT_BINARY_OPERATOR(*, Float);
79 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattner0b00b312001-10-27 08:28:11 +000080 default:
David Greene8121a812010-01-05 01:27:23 +000081 dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +000082 llvm_unreachable(0);
Chris Lattner0b00b312001-10-27 08:28:11 +000083 }
Chris Lattner0b00b312001-10-27 08:28:11 +000084}
85
Reid Spencer54c6e842007-03-03 06:22:22 +000086static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
87 GenericValue Src2, const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000088 switch (Ty->getTypeID()) {
Chris Lattner0b00b312001-10-27 08:28:11 +000089 IMPLEMENT_BINARY_OPERATOR(/, Float);
90 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattner0b00b312001-10-27 08:28:11 +000091 default:
David Greene8121a812010-01-05 01:27:23 +000092 dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +000093 llvm_unreachable(0);
Chris Lattner5946b112001-10-30 20:27:31 +000094 }
Chris Lattner5946b112001-10-30 20:27:31 +000095}
96
Reid Spencer54c6e842007-03-03 06:22:22 +000097static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
98 GenericValue Src2, const Type *Ty) {
Reid Spencer7eb55b32006-11-02 01:53:59 +000099 switch (Ty->getTypeID()) {
Chris Lattner5946b112001-10-30 20:27:31 +0000100 case Type::FloatTyID:
101 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
102 break;
103 case Type::DoubleTyID:
104 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
105 break;
106 default:
David Greene8121a812010-01-05 01:27:23 +0000107 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000108 llvm_unreachable(0);
Chris Lattner0b00b312001-10-27 08:28:11 +0000109 }
Chris Lattner0b00b312001-10-27 08:28:11 +0000110}
111
Reid Spencer40015aa2007-03-06 03:09:31 +0000112#define IMPLEMENT_INTEGER_ICMP(OP, TY) \
113 case Type::IntegerTyID: \
114 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
115 break;
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000116
Chris Lattnered27f842003-04-23 19:55:35 +0000117// Handle pointers specially because they must be compared with only as much
118// width as the host has. We _do not_ want to be comparing 64 bit values when
119// running on a 32-bit target, otherwise the upper 32 bits might mess up
120// comparisons if they contain garbage.
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000121#define IMPLEMENT_POINTER_ICMP(OP) \
Chris Lattnered27f842003-04-23 19:55:35 +0000122 case Type::PointerTyID: \
Reid Spencer40015aa2007-03-06 03:09:31 +0000123 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
124 (void*)(intptr_t)Src2.PointerVal); \
125 break;
Chris Lattnered27f842003-04-23 19:55:35 +0000126
Reid Spencer266e42b2006-12-23 06:05:41 +0000127static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
128 const Type *Ty) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000129 GenericValue Dest;
Chris Lattner6b727592004-06-17 18:19:28 +0000130 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000131 IMPLEMENT_INTEGER_ICMP(eq,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000132 IMPLEMENT_POINTER_ICMP(==);
Reid Spencer266e42b2006-12-23 06:05:41 +0000133 default:
David Greene8121a812010-01-05 01:27:23 +0000134 dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000135 llvm_unreachable(0);
Reid Spencer266e42b2006-12-23 06:05:41 +0000136 }
137 return Dest;
138}
139
140static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
141 const Type *Ty) {
142 GenericValue Dest;
143 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000144 IMPLEMENT_INTEGER_ICMP(ne,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000145 IMPLEMENT_POINTER_ICMP(!=);
Reid Spencer266e42b2006-12-23 06:05:41 +0000146 default:
David Greene8121a812010-01-05 01:27:23 +0000147 dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000148 llvm_unreachable(0);
Reid Spencer266e42b2006-12-23 06:05:41 +0000149 }
150 return Dest;
151}
152
153static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
154 const Type *Ty) {
155 GenericValue Dest;
156 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000157 IMPLEMENT_INTEGER_ICMP(ult,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000158 IMPLEMENT_POINTER_ICMP(<);
Reid Spencer266e42b2006-12-23 06:05:41 +0000159 default:
David Greene8121a812010-01-05 01:27:23 +0000160 dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000161 llvm_unreachable(0);
Reid Spencer266e42b2006-12-23 06:05:41 +0000162 }
163 return Dest;
164}
165
166static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
167 const Type *Ty) {
168 GenericValue Dest;
169 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000170 IMPLEMENT_INTEGER_ICMP(slt,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000171 IMPLEMENT_POINTER_ICMP(<);
Reid Spencer266e42b2006-12-23 06:05:41 +0000172 default:
David Greene8121a812010-01-05 01:27:23 +0000173 dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000174 llvm_unreachable(0);
Reid Spencer266e42b2006-12-23 06:05:41 +0000175 }
176 return Dest;
177}
178
179static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
180 const Type *Ty) {
181 GenericValue Dest;
182 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000183 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000184 IMPLEMENT_POINTER_ICMP(>);
Reid Spencer266e42b2006-12-23 06:05:41 +0000185 default:
David Greene8121a812010-01-05 01:27:23 +0000186 dbgs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000187 llvm_unreachable(0);
Reid Spencer266e42b2006-12-23 06:05:41 +0000188 }
189 return Dest;
190}
191
192static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
193 const Type *Ty) {
194 GenericValue Dest;
195 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000196 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000197 IMPLEMENT_POINTER_ICMP(>);
Reid Spencer266e42b2006-12-23 06:05:41 +0000198 default:
David Greene8121a812010-01-05 01:27:23 +0000199 dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000200 llvm_unreachable(0);
Reid Spencer266e42b2006-12-23 06:05:41 +0000201 }
202 return Dest;
203}
204
205static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
206 const Type *Ty) {
207 GenericValue Dest;
208 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000209 IMPLEMENT_INTEGER_ICMP(ule,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000210 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencer266e42b2006-12-23 06:05:41 +0000211 default:
David Greene8121a812010-01-05 01:27:23 +0000212 dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000213 llvm_unreachable(0);
Reid Spencer266e42b2006-12-23 06:05:41 +0000214 }
215 return Dest;
216}
217
218static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
219 const Type *Ty) {
220 GenericValue Dest;
221 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000222 IMPLEMENT_INTEGER_ICMP(sle,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000223 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencer266e42b2006-12-23 06:05:41 +0000224 default:
David Greene8121a812010-01-05 01:27:23 +0000225 dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000226 llvm_unreachable(0);
Reid Spencer266e42b2006-12-23 06:05:41 +0000227 }
228 return Dest;
229}
230
231static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
232 const Type *Ty) {
233 GenericValue Dest;
234 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000235 IMPLEMENT_INTEGER_ICMP(uge,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000236 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencer266e42b2006-12-23 06:05:41 +0000237 default:
David Greene8121a812010-01-05 01:27:23 +0000238 dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000239 llvm_unreachable(0);
Reid Spencer266e42b2006-12-23 06:05:41 +0000240 }
241 return Dest;
242}
243
244static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
245 const Type *Ty) {
246 GenericValue Dest;
247 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000248 IMPLEMENT_INTEGER_ICMP(sge,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000249 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencer266e42b2006-12-23 06:05:41 +0000250 default:
David Greene8121a812010-01-05 01:27:23 +0000251 dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000252 llvm_unreachable(0);
Reid Spencer266e42b2006-12-23 06:05:41 +0000253 }
254 return Dest;
255}
256
Reid Spencer0d54e782006-12-31 05:51:36 +0000257void Interpreter::visitICmpInst(ICmpInst &I) {
258 ExecutionContext &SF = ECStack.back();
259 const Type *Ty = I.getOperand(0)->getType();
260 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
261 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
262 GenericValue R; // Result
263
264 switch (I.getPredicate()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000265 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
266 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
Reid Spencer0d54e782006-12-31 05:51:36 +0000267 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
268 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
269 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
270 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
271 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
272 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
273 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
274 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
275 default:
David Greene8121a812010-01-05 01:27:23 +0000276 dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I;
Torok Edwinfbcc6632009-07-14 16:55:14 +0000277 llvm_unreachable(0);
Reid Spencer0d54e782006-12-31 05:51:36 +0000278 }
279
280 SetValue(&I, R, SF);
281}
282
283#define IMPLEMENT_FCMP(OP, TY) \
Reid Spencer40015aa2007-03-06 03:09:31 +0000284 case Type::TY##TyID: \
285 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
286 break
Reid Spencer266e42b2006-12-23 06:05:41 +0000287
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000288static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
Reid Spencer266e42b2006-12-23 06:05:41 +0000289 const Type *Ty) {
290 GenericValue Dest;
291 switch (Ty->getTypeID()) {
Reid Spencer0d54e782006-12-31 05:51:36 +0000292 IMPLEMENT_FCMP(==, Float);
293 IMPLEMENT_FCMP(==, Double);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000294 default:
David Greene8121a812010-01-05 01:27:23 +0000295 dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000296 llvm_unreachable(0);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000297 }
298 return Dest;
299}
300
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000301static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
Reid Spencer266e42b2006-12-23 06:05:41 +0000302 const Type *Ty) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000303 GenericValue Dest;
Chris Lattner6b727592004-06-17 18:19:28 +0000304 switch (Ty->getTypeID()) {
Reid Spencer0d54e782006-12-31 05:51:36 +0000305 IMPLEMENT_FCMP(!=, Float);
306 IMPLEMENT_FCMP(!=, Double);
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000307
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000308 default:
David Greene8121a812010-01-05 01:27:23 +0000309 dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000310 llvm_unreachable(0);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000311 }
312 return Dest;
313}
314
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000315static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
Reid Spencer266e42b2006-12-23 06:05:41 +0000316 const Type *Ty) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000317 GenericValue Dest;
Chris Lattner6b727592004-06-17 18:19:28 +0000318 switch (Ty->getTypeID()) {
Reid Spencer0d54e782006-12-31 05:51:36 +0000319 IMPLEMENT_FCMP(<=, Float);
320 IMPLEMENT_FCMP(<=, Double);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000321 default:
David Greene8121a812010-01-05 01:27:23 +0000322 dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000323 llvm_unreachable(0);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000324 }
325 return Dest;
326}
327
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000328static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
Reid Spencer266e42b2006-12-23 06:05:41 +0000329 const Type *Ty) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000330 GenericValue Dest;
Chris Lattner6b727592004-06-17 18:19:28 +0000331 switch (Ty->getTypeID()) {
Reid Spencer0d54e782006-12-31 05:51:36 +0000332 IMPLEMENT_FCMP(>=, Float);
333 IMPLEMENT_FCMP(>=, Double);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000334 default:
David Greene8121a812010-01-05 01:27:23 +0000335 dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000336 llvm_unreachable(0);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000337 }
338 return Dest;
339}
340
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000341static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
Reid Spencer266e42b2006-12-23 06:05:41 +0000342 const Type *Ty) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000343 GenericValue Dest;
Chris Lattner6b727592004-06-17 18:19:28 +0000344 switch (Ty->getTypeID()) {
Reid Spencer0d54e782006-12-31 05:51:36 +0000345 IMPLEMENT_FCMP(<, Float);
346 IMPLEMENT_FCMP(<, Double);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000347 default:
David Greene8121a812010-01-05 01:27:23 +0000348 dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000349 llvm_unreachable(0);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000350 }
351 return Dest;
352}
353
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000354static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000355 const Type *Ty) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000356 GenericValue Dest;
Chris Lattner6b727592004-06-17 18:19:28 +0000357 switch (Ty->getTypeID()) {
Reid Spencer0d54e782006-12-31 05:51:36 +0000358 IMPLEMENT_FCMP(>, Float);
359 IMPLEMENT_FCMP(>, Double);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000360 default:
David Greene8121a812010-01-05 01:27:23 +0000361 dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000362 llvm_unreachable(0);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000363 }
364 return Dest;
365}
366
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000367#define IMPLEMENT_UNORDERED(TY, X,Y) \
Chris Lattnerfdd87902009-10-05 05:54:46 +0000368 if (TY->isFloatTy()) { \
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000369 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
370 Dest.IntVal = APInt(1,true); \
371 return Dest; \
372 } \
373 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
374 Dest.IntVal = APInt(1,true); \
375 return Dest; \
376 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000377
378
379static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
380 const Type *Ty) {
381 GenericValue Dest;
382 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
383 return executeFCMP_OEQ(Src1, Src2, Ty);
384}
385
386static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
387 const Type *Ty) {
388 GenericValue Dest;
389 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
390 return executeFCMP_ONE(Src1, Src2, Ty);
391}
392
393static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
394 const Type *Ty) {
395 GenericValue Dest;
396 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
397 return executeFCMP_OLE(Src1, Src2, Ty);
398}
399
400static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
401 const Type *Ty) {
402 GenericValue Dest;
403 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
404 return executeFCMP_OGE(Src1, Src2, Ty);
405}
406
407static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
408 const Type *Ty) {
409 GenericValue Dest;
410 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
411 return executeFCMP_OLT(Src1, Src2, Ty);
412}
413
414static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
415 const Type *Ty) {
416 GenericValue Dest;
417 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
418 return executeFCMP_OGT(Src1, Src2, Ty);
419}
420
421static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
422 const Type *Ty) {
423 GenericValue Dest;
Chris Lattnerfdd87902009-10-05 05:54:46 +0000424 if (Ty->isFloatTy())
Reid Spencer40015aa2007-03-06 03:09:31 +0000425 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
426 Src2.FloatVal == Src2.FloatVal));
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000427 else
Reid Spencer40015aa2007-03-06 03:09:31 +0000428 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
429 Src2.DoubleVal == Src2.DoubleVal));
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000430 return Dest;
431}
432
433static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
434 const Type *Ty) {
435 GenericValue Dest;
Chris Lattnerfdd87902009-10-05 05:54:46 +0000436 if (Ty->isFloatTy())
Reid Spencer40015aa2007-03-06 03:09:31 +0000437 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
438 Src2.FloatVal != Src2.FloatVal));
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000439 else
Reid Spencer40015aa2007-03-06 03:09:31 +0000440 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
441 Src2.DoubleVal != Src2.DoubleVal));
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000442 return Dest;
443}
444
Reid Spencer266e42b2006-12-23 06:05:41 +0000445void Interpreter::visitFCmpInst(FCmpInst &I) {
446 ExecutionContext &SF = ECStack.back();
447 const Type *Ty = I.getOperand(0)->getType();
448 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
449 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
450 GenericValue R; // Result
451
452 switch (I.getPredicate()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000453 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
454 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000455 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
456 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
457 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
458 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
459 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
460 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
461 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
462 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
463 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
464 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
465 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
466 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
467 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
468 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
Reid Spencer266e42b2006-12-23 06:05:41 +0000469 default:
David Greene8121a812010-01-05 01:27:23 +0000470 dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I;
Torok Edwinfbcc6632009-07-14 16:55:14 +0000471 llvm_unreachable(0);
Reid Spencer266e42b2006-12-23 06:05:41 +0000472 }
473
474 SetValue(&I, R, SF);
475}
476
Reid Spencer266e42b2006-12-23 06:05:41 +0000477static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
478 GenericValue Src2, const Type *Ty) {
479 GenericValue Result;
480 switch (predicate) {
481 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
482 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
483 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
484 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
485 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
486 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
487 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
488 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
489 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
490 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000491 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
492 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
493 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
494 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
495 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
496 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
497 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
498 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
499 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
500 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
501 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
502 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
503 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
504 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
Reid Spencer266e42b2006-12-23 06:05:41 +0000505 case FCmpInst::FCMP_FALSE: {
506 GenericValue Result;
Reid Spencer40015aa2007-03-06 03:09:31 +0000507 Result.IntVal = APInt(1, false);
Reid Spencer266e42b2006-12-23 06:05:41 +0000508 return Result;
509 }
510 case FCmpInst::FCMP_TRUE: {
511 GenericValue Result;
Reid Spencer40015aa2007-03-06 03:09:31 +0000512 Result.IntVal = APInt(1, true);
Reid Spencer266e42b2006-12-23 06:05:41 +0000513 return Result;
514 }
515 default:
David Greene8121a812010-01-05 01:27:23 +0000516 dbgs() << "Unhandled Cmp predicate\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000517 llvm_unreachable(0);
Reid Spencer266e42b2006-12-23 06:05:41 +0000518 }
519}
520
Chris Lattner185045c2003-05-10 21:22:39 +0000521void Interpreter::visitBinaryOperator(BinaryOperator &I) {
522 ExecutionContext &SF = ECStack.back();
Chris Lattner7076ff22002-06-25 16:13:21 +0000523 const Type *Ty = I.getOperand(0)->getType();
524 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
525 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000526 GenericValue R; // Result
527
Chris Lattner7076ff22002-06-25 16:13:21 +0000528 switch (I.getOpcode()) {
Dan Gohmana5b96452009-06-04 22:49:04 +0000529 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
530 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
531 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
532 case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;
533 case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;
534 case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;
535 case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;
536 case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;
Reid Spencer40015aa2007-03-06 03:09:31 +0000537 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
538 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
539 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
540 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
541 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
542 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
543 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000544 default:
David Greene8121a812010-01-05 01:27:23 +0000545 dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
Torok Edwinfbcc6632009-07-14 16:55:14 +0000546 llvm_unreachable(0);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000547 }
548
Chris Lattner7076ff22002-06-25 16:13:21 +0000549 SetValue(&I, R, SF);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000550}
551
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000552static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner2b2d7a92004-04-20 16:43:21 +0000553 GenericValue Src3) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000554 return Src1.IntVal == 0 ? Src3 : Src2;
Chris Lattner2b2d7a92004-04-20 16:43:21 +0000555}
556
557void Interpreter::visitSelectInst(SelectInst &I) {
558 ExecutionContext &SF = ECStack.back();
559 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
560 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
561 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
Reid Spencer40015aa2007-03-06 03:09:31 +0000562 GenericValue R = executeSelectInst(Src1, Src2, Src3);
Chris Lattner2b2d7a92004-04-20 16:43:21 +0000563 SetValue(&I, R, SF);
564}
565
566
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000567//===----------------------------------------------------------------------===//
568// Terminator Instruction Implementations
569//===----------------------------------------------------------------------===//
570
Chris Lattner15157b82001-10-27 04:15:57 +0000571void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeke51f10072004-02-13 05:48:00 +0000572 // runAtExitHandlers() assumes there are no stack frames, but
573 // if exit() was called, then it had a stack frame. Blow away
574 // the stack before interpreting atexit handlers.
Chris Lattner0c778f72009-10-29 05:26:09 +0000575 ECStack.clear();
576 runAtExitHandlers();
577 exit(GV.IntVal.zextOrTrunc(32).getZExtValue());
Chris Lattner15157b82001-10-27 04:15:57 +0000578}
579
Brian Gaeke65cac902003-11-07 05:22:49 +0000580/// Pop the last stack frame off of ECStack and then copy the result
581/// back into the result variable if we are not returning void. The
Jeff Cohen24396692006-02-07 05:29:44 +0000582/// result variable may be the ExitValue, or the Value of the calling
Brian Gaeke85baf8c2003-11-07 20:44:58 +0000583/// CallInst if there was a previous stack frame. This method may
584/// invalidate any ECStack iterators you have. This method also takes
585/// care of switching to the normal destination BB, if we are returning
586/// from an invoke.
Brian Gaeke65cac902003-11-07 05:22:49 +0000587///
Chris Lattner0c778f72009-10-29 05:26:09 +0000588void Interpreter::popStackAndReturnValueToCaller(const Type *RetTy,
589 GenericValue Result) {
Brian Gaeke65cac902003-11-07 05:22:49 +0000590 // Pop the current stack frame.
591 ECStack.pop_back();
592
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000593 if (ECStack.empty()) { // Finished main. Put result into exit code...
Dan Gohmanc61056a2010-06-18 02:01:10 +0000594 if (RetTy && !RetTy->isVoidTy()) { // Nonvoid return type?
Jeff Cohen24396692006-02-07 05:29:44 +0000595 ExitValue = Result; // Capture the exit value of the program
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000596 } else {
Reid Spencerff38cf82007-06-01 22:23:29 +0000597 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000598 }
599 } else {
600 // If we have a previous stack frame, and we have a previous call,
601 // fill in the return value...
Brian Gaeke65cac902003-11-07 05:22:49 +0000602 ExecutionContext &CallingSF = ECStack.back();
Brian Gaeke85baf8c2003-11-07 20:44:58 +0000603 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000604 // Save result...
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000605 if (!CallingSF.Caller.getType()->isVoidTy())
Brian Gaeke85baf8c2003-11-07 20:44:58 +0000606 SetValue(I, Result, CallingSF);
607 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
608 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke18b59572003-11-07 19:26:23 +0000609 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaeke65cac902003-11-07 05:22:49 +0000610 }
611 }
612}
613
Chris Lattner185045c2003-05-10 21:22:39 +0000614void Interpreter::visitReturnInst(ReturnInst &I) {
615 ExecutionContext &SF = ECStack.back();
Owen Anderson55f1c092009-08-13 21:58:54 +0000616 const Type *RetTy = Type::getVoidTy(I.getContext());
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000617 GenericValue Result;
618
619 // Save away the return value... (if we are not 'ret void')
Chris Lattner7076ff22002-06-25 16:13:21 +0000620 if (I.getNumOperands()) {
621 RetTy = I.getReturnValue()->getType();
622 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000623 }
624
Brian Gaeke65cac902003-11-07 05:22:49 +0000625 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000626}
627
Brian Gaeke6d145eb2003-11-07 20:07:06 +0000628void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaeke85baf8c2003-11-07 20:44:58 +0000629 // Unwind stack
630 Instruction *Inst;
631 do {
Chris Lattner0c778f72009-10-29 05:26:09 +0000632 ECStack.pop_back();
633 if (ECStack.empty())
Chris Lattner2104b8d2010-04-07 22:58:41 +0000634 report_fatal_error("Empty stack during unwind!");
Chris Lattner0c778f72009-10-29 05:26:09 +0000635 Inst = ECStack.back().Caller.getInstruction();
636 } while (!(Inst && isa<InvokeInst>(Inst)));
Brian Gaeke85baf8c2003-11-07 20:44:58 +0000637
638 // Return from invoke
Chris Lattner0c778f72009-10-29 05:26:09 +0000639 ExecutionContext &InvokingSF = ECStack.back();
640 InvokingSF.Caller = CallSite();
Brian Gaeke85baf8c2003-11-07 20:44:58 +0000641
642 // Go to exceptional destination BB of invoke instruction
Chris Lattnerfae8ab32004-02-08 21:44:31 +0000643 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke6d145eb2003-11-07 20:07:06 +0000644}
645
Chris Lattner98e54142004-10-16 18:21:33 +0000646void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000647 report_fatal_error("Program executed an 'unreachable' instruction!");
Chris Lattner98e54142004-10-16 18:21:33 +0000648}
649
Chris Lattner185045c2003-05-10 21:22:39 +0000650void Interpreter::visitBranchInst(BranchInst &I) {
651 ExecutionContext &SF = ECStack.back();
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000652 BasicBlock *Dest;
653
Chris Lattner7076ff22002-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 Spencer40015aa2007-03-06 03:09:31 +0000657 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000658 Dest = I.getSuccessor(1);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000659 }
Chris Lattnerbd6771c2003-05-10 20:21:16 +0000660 SwitchToNewBasicBlock(Dest, SF);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000661}
662
Chris Lattner185045c2003-05-10 21:22:39 +0000663void Interpreter::visitSwitchInst(SwitchInst &I) {
664 ExecutionContext &SF = ECStack.back();
Chris Lattnerd215af02003-04-22 20:34:47 +0000665 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
666 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattnerd215af02003-04-22 20:34:47 +0000667
668 // Check to see if any of the cases match...
Chris Lattnerbd6771c2003-05-10 20:21:16 +0000669 BasicBlock *Dest = 0;
670 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Reid Spencer40015aa2007-03-06 03:09:31 +0000671 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
672 .IntVal != 0) {
Chris Lattnerd215af02003-04-22 20:34:47 +0000673 Dest = cast<BasicBlock>(I.getOperand(i+1));
674 break;
675 }
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000676
Chris Lattnerd215af02003-04-22 20:34:47 +0000677 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattnerbd6771c2003-05-10 20:21:16 +0000678 SwitchToNewBasicBlock(Dest, SF);
679}
680
Chris Lattner0c778f72009-10-29 05:26:09 +0000681void Interpreter::visitIndirectBrInst(IndirectBrInst &I) {
682 ExecutionContext &SF = ECStack.back();
683 void *Dest = GVTOP(getOperandValue(I.getAddress(), SF));
684 SwitchToNewBasicBlock((BasicBlock*)Dest, SF);
685}
686
687
Chris Lattnerbd6771c2003-05-10 20:21:16 +0000688// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
689// This function handles the actual updating of block and instruction iterators
690// as well as execution of all of the PHI nodes in the destination block.
691//
692// This method does this because all of the PHI nodes must be executed
693// atomically, reading their inputs before any of the results are updated. Not
694// doing this can cause problems if the PHI nodes depend on other PHI nodes for
695// their inputs. If the input PHI node is updated before it is read, incorrect
696// results can happen. Thus we use a two phase approach.
697//
698void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
699 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
700 SF.CurBB = Dest; // Update CurBB to branch destination
701 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
702
703 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
704
705 // Loop over all of the PHI nodes in the current block, reading their inputs.
706 std::vector<GenericValue> ResultValues;
707
708 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
709 // Search for the value corresponding to this previous bb...
710 int i = PN->getBasicBlockIndex(PrevBB);
711 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
712 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000713
Chris Lattnerbd6771c2003-05-10 20:21:16 +0000714 // Save the incoming value for this PHI node...
715 ResultValues.push_back(getOperandValue(IncomingValue, SF));
716 }
717
718 // Now loop over all of the PHI nodes setting their values...
719 SF.CurInst = SF.CurBB->begin();
Reid Spencer66149462004-09-15 17:06:42 +0000720 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
721 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattnerbd6771c2003-05-10 20:21:16 +0000722 SetValue(PN, ResultValues[i], SF);
Reid Spencer66149462004-09-15 17:06:42 +0000723 }
Chris Lattnerd215af02003-04-22 20:34:47 +0000724}
725
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000726//===----------------------------------------------------------------------===//
Chris Lattner2c1a98e2001-08-27 05:16:50 +0000727// Memory Instruction Implementations
728//===----------------------------------------------------------------------===//
729
Victor Hernandez8acf2952009-10-23 21:09:37 +0000730void Interpreter::visitAllocaInst(AllocaInst &I) {
Chris Lattner185045c2003-05-10 21:22:39 +0000731 ExecutionContext &SF = ECStack.back();
732
Chris Lattner7076ff22002-06-25 16:13:21 +0000733 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner2c1a98e2001-08-27 05:16:50 +0000734
Chris Lattner0ebb7932002-04-28 21:57:33 +0000735 // Get the number of elements being allocated by the array...
Reid Spencer40015aa2007-03-06 03:09:31 +0000736 unsigned NumElements =
737 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
738
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000739 unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
Reid Spencer40015aa2007-03-06 03:09:31 +0000740
Gabor Greifcb6832e2007-10-11 19:40:35 +0000741 // Avoid malloc-ing zero bytes, use max()...
742 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
Chris Lattner2c1a98e2001-08-27 05:16:50 +0000743
744 // Allocate enough memory to hold the type...
Reid Spencer40015aa2007-03-06 03:09:31 +0000745 void *Memory = malloc(MemToAlloc);
746
David Greene8121a812010-01-05 01:27:23 +0000747 DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
Chris Lattner3924bb52009-08-23 04:52:46 +0000748 << NumElements << " (Total: " << MemToAlloc << ") at "
749 << uintptr_t(Memory) << '\n');
Chris Lattnerfb55ba02002-02-19 18:50:09 +0000750
Chris Lattnera0d7b082002-12-23 23:59:41 +0000751 GenericValue Result = PTOGV(Memory);
Chris Lattner31e9e4d2001-11-07 19:46:27 +0000752 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner7076ff22002-06-25 16:13:21 +0000753 SetValue(&I, Result, SF);
Chris Lattner2c1a98e2001-08-27 05:16:50 +0000754
Chris Lattner7076ff22002-06-25 16:13:21 +0000755 if (I.getOpcode() == Instruction::Alloca)
Chris Lattnerfb55ba02002-02-19 18:50:09 +0000756 ECStack.back().Allocas.add(Memory);
Chris Lattner2c1a98e2001-08-27 05:16:50 +0000757}
758
Chris Lattnerc837dbc2002-08-27 22:33:45 +0000759// getElementOffset - The workhorse for getelementptr.
Chris Lattner05fbeed2001-10-29 19:32:19 +0000760//
Chris Lattnerf0788082003-11-25 20:44:56 +0000761GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000762 gep_type_iterator E,
763 ExecutionContext &SF) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000764 assert(Ptr->getType()->isPointerTy() &&
Chris Lattner05fbeed2001-10-29 19:32:19 +0000765 "Cannot getElementOffset of a nonpointer type!");
766
Reid Spencer40015aa2007-03-06 03:09:31 +0000767 uint64_t Total = 0;
Chris Lattnerc837dbc2002-08-27 22:33:45 +0000768
769 for (; I != E; ++I) {
Chris Lattnerf0788082003-11-25 20:44:56 +0000770 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner5ba75732001-11-26 18:18:18 +0000771 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000772
Reid Spencere0fc4df2006-10-20 07:07:24 +0000773 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
774 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000775
Reid Spencer40015aa2007-03-06 03:09:31 +0000776 Total += SLO->getElementOffset(Index);
Chris Lattnerf0788082003-11-25 20:44:56 +0000777 } else {
778 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner13b3e5b2003-02-25 21:14:59 +0000779 // Get the index number for the array... which must be long type...
Chris Lattnerf0788082003-11-25 20:44:56 +0000780 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
781
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000782 int64_t Idx;
783 unsigned BitWidth =
784 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
Reid Spencerfab44b62007-01-18 01:25:42 +0000785 if (BitWidth == 32)
Reid Spencer40015aa2007-03-06 03:09:31 +0000786 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
Chris Lattner982e8502008-04-06 21:50:58 +0000787 else {
788 assert(BitWidth == 64 && "Invalid index type for getelementptr");
Reid Spencer40015aa2007-03-06 03:09:31 +0000789 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
Chris Lattner982e8502008-04-06 21:50:58 +0000790 }
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000791 Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
Brian Gaekee278c222003-10-24 19:59:01 +0000792 }
Chris Lattner05fbeed2001-10-29 19:32:19 +0000793 }
794
Chris Lattnerc837dbc2002-08-27 22:33:45 +0000795 GenericValue Result;
Reid Spencer40015aa2007-03-06 03:09:31 +0000796 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
David Greene8121a812010-01-05 01:27:23 +0000797 DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n");
Chris Lattnerc837dbc2002-08-27 22:33:45 +0000798 return Result;
Chris Lattner05fbeed2001-10-29 19:32:19 +0000799}
800
Chris Lattner185045c2003-05-10 21:22:39 +0000801void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
802 ExecutionContext &SF = ECStack.back();
Owen Anderson455df542009-06-26 16:46:15 +0000803 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
Chris Lattnerf0788082003-11-25 20:44:56 +0000804 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner05fbeed2001-10-29 19:32:19 +0000805}
806
Chris Lattner185045c2003-05-10 21:22:39 +0000807void Interpreter::visitLoadInst(LoadInst &I) {
808 ExecutionContext &SF = ECStack.back();
Chris Lattner7076ff22002-06-25 16:13:21 +0000809 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnera0d7b082002-12-23 23:59:41 +0000810 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Reid Spencerec8c51c2007-03-03 08:38:04 +0000811 GenericValue Result;
Reid Spencerec8c51c2007-03-03 08:38:04 +0000812 LoadValueFromMemory(Result, Ptr, I.getType());
Chris Lattner7076ff22002-06-25 16:13:21 +0000813 SetValue(&I, Result, SF);
Chris Lattner2528d632008-07-08 17:25:49 +0000814 if (I.isVolatile() && PrintVolatile)
David Greene8121a812010-01-05 01:27:23 +0000815 dbgs() << "Volatile load " << I;
Chris Lattner2c1a98e2001-08-27 05:16:50 +0000816}
817
Chris Lattner185045c2003-05-10 21:22:39 +0000818void Interpreter::visitStoreInst(StoreInst &I) {
819 ExecutionContext &SF = ECStack.back();
Chris Lattner6a1a65f2002-10-15 20:34:05 +0000820 GenericValue Val = getOperandValue(I.getOperand(0), SF);
821 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnera0d7b082002-12-23 23:59:41 +0000822 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner7fe1f7c2002-10-26 01:57:15 +0000823 I.getOperand(0)->getType());
Chris Lattner2528d632008-07-08 17:25:49 +0000824 if (I.isVolatile() && PrintVolatile)
David Greene8121a812010-01-05 01:27:23 +0000825 dbgs() << "Volatile store: " << I;
Chris Lattner6a1a65f2002-10-15 20:34:05 +0000826}
827
Chris Lattner2c1a98e2001-08-27 05:16:50 +0000828//===----------------------------------------------------------------------===//
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000829// Miscellaneous Instruction Implementations
830//===----------------------------------------------------------------------===//
831
Brian Gaekea6454d352003-11-07 20:04:22 +0000832void Interpreter::visitCallSite(CallSite CS) {
Chris Lattner185045c2003-05-10 21:22:39 +0000833 ExecutionContext &SF = ECStack.back();
Chris Lattnerc8c6c032003-12-28 09:44:37 +0000834
835 // Check to see if this is an intrinsic function call...
Reid Spencerc57be6c2007-04-16 21:50:40 +0000836 Function *F = CS.getCalledFunction();
Chris Lattner0c778f72009-10-29 05:26:09 +0000837 if (F && F->isDeclaration())
Chris Lattnerc8c6c032003-12-28 09:44:37 +0000838 switch (F->getIntrinsicID()) {
Brian Gaeke1c0133f2004-01-14 06:02:53 +0000839 case Intrinsic::not_intrinsic:
840 break;
Chris Lattner071a5e52004-03-13 00:24:00 +0000841 case Intrinsic::vastart: { // va_start
Brian Gaeke7b4be132004-02-25 23:01:48 +0000842 GenericValue ArgIndex;
843 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
844 ArgIndex.UIntPairVal.second = 0;
845 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattnerc8c6c032003-12-28 09:44:37 +0000846 return;
Brian Gaeke7b4be132004-02-25 23:01:48 +0000847 }
Chris Lattner071a5e52004-03-13 00:24:00 +0000848 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattnerc8c6c032003-12-28 09:44:37 +0000849 return;
Chris Lattner071a5e52004-03-13 00:24:00 +0000850 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattnerc8c6c032003-12-28 09:44:37 +0000851 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
852 return;
853 default:
Brian Gaeke1c0133f2004-01-14 06:02:53 +0000854 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattnerc8c6c032003-12-28 09:44:37 +0000855 // class to transform it into hopefully tasty LLVM code.
856 //
Chris Lattnerd9b75112007-04-17 17:38:28 +0000857 BasicBlock::iterator me(CS.getInstruction());
Chris Lattnerc8c6c032003-12-28 09:44:37 +0000858 BasicBlock *Parent = CS.getInstruction()->getParent();
Chris Lattnerd9b75112007-04-17 17:38:28 +0000859 bool atBegin(Parent->begin() == me);
860 if (!atBegin)
861 --me;
Chris Lattnerc8c6c032003-12-28 09:44:37 +0000862 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
863
864 // Restore the CurInst pointer to the first instruction newly inserted, if
865 // any.
Chris Lattnerd9b75112007-04-17 17:38:28 +0000866 if (atBegin) {
Chris Lattnerc8c6c032003-12-28 09:44:37 +0000867 SF.CurInst = Parent->begin();
868 } else {
Chris Lattnerd9b75112007-04-17 17:38:28 +0000869 SF.CurInst = me;
Chris Lattnerc8c6c032003-12-28 09:44:37 +0000870 ++SF.CurInst;
871 }
Brian Gaekead373c82004-04-23 18:05:28 +0000872 return;
Chris Lattnerc8c6c032003-12-28 09:44:37 +0000873 }
874
Reid Spencerc57be6c2007-04-16 21:50:40 +0000875
Brian Gaekea6454d352003-11-07 20:04:22 +0000876 SF.Caller = CS;
Chris Lattneraee56b82003-04-22 21:22:33 +0000877 std::vector<GenericValue> ArgVals;
Brian Gaekea6d48e42003-11-07 19:59:08 +0000878 const unsigned NumArgs = SF.Caller.arg_size();
879 ArgVals.reserve(NumArgs);
Reid Spencerc57be6c2007-04-16 21:50:40 +0000880 uint16_t pNum = 1;
Brian Gaekea6d48e42003-11-07 19:59:08 +0000881 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
Reid Spencerc57be6c2007-04-16 21:50:40 +0000882 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
Brian Gaekea6d48e42003-11-07 19:59:08 +0000883 Value *V = *i;
884 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner4e7aa442003-01-13 00:58:52 +0000885 }
Chris Lattner676d4112001-09-10 04:49:44 +0000886
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000887 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner62b7fd12002-04-07 20:49:59 +0000888 // and treat it as a function pointer.
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000889 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattner470754e2003-05-08 16:18:31 +0000890 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000891}
892
Reid Spencer2341c222007-02-02 02:16:23 +0000893void Interpreter::visitShl(BinaryOperator &I) {
Brian Gaeke5a8ec7d2003-12-11 00:22:59 +0000894 ExecutionContext &SF = ECStack.back();
Brian Gaeke5a8ec7d2003-12-11 00:22:59 +0000895 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
896 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
897 GenericValue Dest;
Chris Lattner762edbc2009-01-16 20:17:02 +0000898 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
899 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
900 else
901 Dest.IntVal = Src1.IntVal;
902
Brian Gaeke5a8ec7d2003-12-11 00:22:59 +0000903 SetValue(&I, Dest, SF);
904}
905
Reid Spencer2341c222007-02-02 02:16:23 +0000906void Interpreter::visitLShr(BinaryOperator &I) {
Brian Gaeke5a8ec7d2003-12-11 00:22:59 +0000907 ExecutionContext &SF = ECStack.back();
Brian Gaeke5a8ec7d2003-12-11 00:22:59 +0000908 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
909 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
910 GenericValue Dest;
Chris Lattner762edbc2009-01-16 20:17:02 +0000911 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
912 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
913 else
914 Dest.IntVal = Src1.IntVal;
915
Reid Spencerfdff9382006-11-08 06:47:33 +0000916 SetValue(&I, Dest, SF);
917}
918
Reid Spencer2341c222007-02-02 02:16:23 +0000919void Interpreter::visitAShr(BinaryOperator &I) {
Reid Spencerfdff9382006-11-08 06:47:33 +0000920 ExecutionContext &SF = ECStack.back();
Reid Spencerfdff9382006-11-08 06:47:33 +0000921 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
922 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner762edbc2009-01-16 20:17:02 +0000923 GenericValue Dest;
924 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
925 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
926 else
927 Dest.IntVal = Src1.IntVal;
928
Chris Lattner7076ff22002-06-25 16:13:21 +0000929 SetValue(&I, Dest, SF);
Chris Lattner2c1a98e2001-08-27 05:16:50 +0000930}
931
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000932GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
933 ExecutionContext &SF) {
Chris Lattnerc837dbc2002-08-27 22:33:45 +0000934 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000935 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000936 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencer40015aa2007-03-06 03:09:31 +0000937 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
Chris Lattnerc837dbc2002-08-27 22:33:45 +0000938 return Dest;
Chris Lattner2c1a98e2001-08-27 05:16:50 +0000939}
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000940
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000941GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
942 ExecutionContext &SF) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000943 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
944 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000945 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencer40015aa2007-03-06 03:09:31 +0000946 Dest.IntVal = Src.IntVal.sext(DBitWidth);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000947 return Dest;
948}
949
950GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
951 ExecutionContext &SF) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000952 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
953 const IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000954 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencer40015aa2007-03-06 03:09:31 +0000955 Dest.IntVal = Src.IntVal.zext(DBitWidth);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000956 return Dest;
957}
958
959GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
960 ExecutionContext &SF) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000961 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000962 assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000963 "Invalid FPTrunc instruction");
964 Dest.FloatVal = (float) Src.DoubleVal;
965 return Dest;
966}
967
968GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
969 ExecutionContext &SF) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000970 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000971 assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000972 "Invalid FPTrunc instruction");
973 Dest.DoubleVal = (double) Src.FloatVal;
974 return Dest;
975}
976
977GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
978 ExecutionContext &SF) {
979 const Type *SrcTy = SrcVal->getType();
Reid Spencer40015aa2007-03-06 03:09:31 +0000980 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000981 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sands9dff9be2010-02-15 16:12:20 +0000982 assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction");
Reid Spencerec8c51c2007-03-03 08:38:04 +0000983
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000984 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencer40015aa2007-03-06 03:09:31 +0000985 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000986 else
Reid Spencer40015aa2007-03-06 03:09:31 +0000987 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000988 return Dest;
989}
990
991GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
992 ExecutionContext &SF) {
993 const Type *SrcTy = SrcVal->getType();
Reid Spencer40015aa2007-03-06 03:09:31 +0000994 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000995 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sands9dff9be2010-02-15 16:12:20 +0000996 assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction");
Reid Spencerec8c51c2007-03-03 08:38:04 +0000997
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000998 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencer40015aa2007-03-06 03:09:31 +0000999 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001000 else
Reid Spencer40015aa2007-03-06 03:09:31 +00001001 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001002 return Dest;
1003}
1004
1005GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1006 ExecutionContext &SF) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001007 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sands9dff9be2010-02-15 16:12:20 +00001008 assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction");
Reid Spencerec8c51c2007-03-03 08:38:04 +00001009
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001010 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencer40015aa2007-03-06 03:09:31 +00001011 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001012 else
Reid Spencer40015aa2007-03-06 03:09:31 +00001013 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001014 return Dest;
1015}
1016
1017GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1018 ExecutionContext &SF) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001019 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sands9dff9be2010-02-15 16:12:20 +00001020 assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");
Reid Spencerec8c51c2007-03-03 08:38:04 +00001021
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001022 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencer40015aa2007-03-06 03:09:31 +00001023 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001024 else
Reid Spencer40015aa2007-03-06 03:09:31 +00001025 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001026 return Dest;
Reid Spencer40015aa2007-03-06 03:09:31 +00001027
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001028}
1029
1030GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1031 ExecutionContext &SF) {
Reid Spencer40015aa2007-03-06 03:09:31 +00001032 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001033 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sands19d0b472010-02-16 11:11:14 +00001034 assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction");
Reid Spencerec8c51c2007-03-03 08:38:04 +00001035
Reid Spencer40015aa2007-03-06 03:09:31 +00001036 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001037 return Dest;
1038}
1039
1040GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1041 ExecutionContext &SF) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001042 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sands19d0b472010-02-16 11:11:14 +00001043 assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction");
Reid Spencerec8c51c2007-03-03 08:38:04 +00001044
Reid Spencer1efc4aa2007-03-06 03:46:41 +00001045 uint32_t PtrSize = TD.getPointerSizeInBits();
Reid Spencer10dbc1e2007-03-06 03:41:50 +00001046 if (PtrSize != Src.IntVal.getBitWidth())
Reid Spencer1efc4aa2007-03-06 03:46:41 +00001047 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
Reid Spencer10dbc1e2007-03-06 03:41:50 +00001048
Reid Spencer1d482072007-04-26 18:19:35 +00001049 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001050 return Dest;
1051}
1052
1053GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1054 ExecutionContext &SF) {
1055
1056 const Type *SrcTy = SrcVal->getType();
1057 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sands19d0b472010-02-16 11:11:14 +00001058 if (DstTy->isPointerTy()) {
1059 assert(SrcTy->isPointerTy() && "Invalid BitCast");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001060 Dest.PointerVal = Src.PointerVal;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001061 } else if (DstTy->isIntegerTy()) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00001062 if (SrcTy->isFloatTy()) {
Dan Gohmancff69532009-04-01 18:45:54 +00001063 Dest.IntVal.zext(sizeof(Src.FloatVal) * CHAR_BIT);
Reid Spencer40015aa2007-03-06 03:09:31 +00001064 Dest.IntVal.floatToBits(Src.FloatVal);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001065 } else if (SrcTy->isDoubleTy()) {
Dan Gohmancff69532009-04-01 18:45:54 +00001066 Dest.IntVal.zext(sizeof(Src.DoubleVal) * CHAR_BIT);
Reid Spencer40015aa2007-03-06 03:09:31 +00001067 Dest.IntVal.doubleToBits(Src.DoubleVal);
Duncan Sands9dff9be2010-02-15 16:12:20 +00001068 } else if (SrcTy->isIntegerTy()) {
Reid Spencer40015aa2007-03-06 03:09:31 +00001069 Dest.IntVal = Src.IntVal;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001070 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +00001071 llvm_unreachable("Invalid BitCast");
Chris Lattnerfdd87902009-10-05 05:54:46 +00001072 } else if (DstTy->isFloatTy()) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001073 if (SrcTy->isIntegerTy())
Reid Spencer40015aa2007-03-06 03:09:31 +00001074 Dest.FloatVal = Src.IntVal.bitsToFloat();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001075 else
1076 Dest.FloatVal = Src.FloatVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00001077 } else if (DstTy->isDoubleTy()) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001078 if (SrcTy->isIntegerTy())
Reid Spencer40015aa2007-03-06 03:09:31 +00001079 Dest.DoubleVal = Src.IntVal.bitsToDouble();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001080 else
1081 Dest.DoubleVal = Src.DoubleVal;
1082 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +00001083 llvm_unreachable("Invalid Bitcast");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001084
1085 return Dest;
1086}
1087
1088void Interpreter::visitTruncInst(TruncInst &I) {
Chris Lattner185045c2003-05-10 21:22:39 +00001089 ExecutionContext &SF = ECStack.back();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001090 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1091}
1092
1093void Interpreter::visitSExtInst(SExtInst &I) {
1094 ExecutionContext &SF = ECStack.back();
1095 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1096}
1097
1098void Interpreter::visitZExtInst(ZExtInst &I) {
1099 ExecutionContext &SF = ECStack.back();
1100 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1101}
1102
1103void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1104 ExecutionContext &SF = ECStack.back();
1105 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1106}
1107
1108void Interpreter::visitFPExtInst(FPExtInst &I) {
1109 ExecutionContext &SF = ECStack.back();
1110 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1111}
1112
1113void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1114 ExecutionContext &SF = ECStack.back();
1115 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1116}
1117
1118void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1119 ExecutionContext &SF = ECStack.back();
1120 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1121}
1122
1123void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1124 ExecutionContext &SF = ECStack.back();
1125 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1126}
1127
1128void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1129 ExecutionContext &SF = ECStack.back();
1130 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1131}
1132
1133void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1134 ExecutionContext &SF = ECStack.back();
1135 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1136}
1137
1138void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1139 ExecutionContext &SF = ECStack.back();
1140 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1141}
1142
1143void Interpreter::visitBitCastInst(BitCastInst &I) {
1144 ExecutionContext &SF = ECStack.back();
1145 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
Chris Lattnerc837dbc2002-08-27 22:33:45 +00001146}
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001147
Brian Gaeke9ef636c2003-11-07 21:20:47 +00001148#define IMPLEMENT_VAARG(TY) \
1149 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1150
1151void Interpreter::visitVAArgInst(VAArgInst &I) {
1152 ExecutionContext &SF = ECStack.back();
1153
Brian Gaeke7b4be132004-02-25 23:01:48 +00001154 // Get the incoming valist parameter. LLI treats the valist as a
1155 // (ec-stack-depth var-arg-index) pair.
Brian Gaeke9ef636c2003-11-07 21:20:47 +00001156 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke7b4be132004-02-25 23:01:48 +00001157 GenericValue Dest;
1158 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Reid Spencer40015aa2007-03-06 03:09:31 +00001159 .VarArgs[VAList.UIntPairVal.second];
Brian Gaeke9ef636c2003-11-07 21:20:47 +00001160 const Type *Ty = I.getType();
Chris Lattner6b727592004-06-17 18:19:28 +00001161 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +00001162 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
Brian Gaeke9ef636c2003-11-07 21:20:47 +00001163 IMPLEMENT_VAARG(Pointer);
1164 IMPLEMENT_VAARG(Float);
1165 IMPLEMENT_VAARG(Double);
Brian Gaeke9ef636c2003-11-07 21:20:47 +00001166 default:
David Greene8121a812010-01-05 01:27:23 +00001167 dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +00001168 llvm_unreachable(0);
Brian Gaeke9ef636c2003-11-07 21:20:47 +00001169 }
Misha Brukman91fb9ab2005-04-21 22:43:08 +00001170
Brian Gaeke9ef636c2003-11-07 21:20:47 +00001171 // Set the Value of this Instruction.
1172 SetValue(&I, Dest, SF);
Andrew Lenharth9144ec42005-06-18 18:34:52 +00001173
1174 // Move the pointer to the next vararg.
1175 ++VAList.UIntPairVal.second;
Brian Gaeke9ef636c2003-11-07 21:20:47 +00001176}
1177
Reid Spencer54c6e842007-03-03 06:22:22 +00001178GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1179 ExecutionContext &SF) {
1180 switch (CE->getOpcode()) {
1181 case Instruction::Trunc:
1182 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1183 case Instruction::ZExt:
1184 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1185 case Instruction::SExt:
1186 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1187 case Instruction::FPTrunc:
1188 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1189 case Instruction::FPExt:
1190 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1191 case Instruction::UIToFP:
1192 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1193 case Instruction::SIToFP:
1194 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1195 case Instruction::FPToUI:
1196 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1197 case Instruction::FPToSI:
1198 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1199 case Instruction::PtrToInt:
1200 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1201 case Instruction::IntToPtr:
1202 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1203 case Instruction::BitCast:
1204 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1205 case Instruction::GetElementPtr:
1206 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1207 gep_type_end(CE), SF);
Reid Spencerec8c51c2007-03-03 08:38:04 +00001208 case Instruction::FCmp:
1209 case Instruction::ICmp:
1210 return executeCmpInst(CE->getPredicate(),
1211 getOperandValue(CE->getOperand(0), SF),
1212 getOperandValue(CE->getOperand(1), SF),
1213 CE->getOperand(0)->getType());
1214 case Instruction::Select:
1215 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1216 getOperandValue(CE->getOperand(1), SF),
1217 getOperandValue(CE->getOperand(2), SF));
Reid Spencer54c6e842007-03-03 06:22:22 +00001218 default :
1219 break;
1220 }
Reid Spencerec8c51c2007-03-03 08:38:04 +00001221
1222 // The cases below here require a GenericValue parameter for the result
1223 // so we initialize one, compute it and then return it.
Reid Spencer40015aa2007-03-06 03:09:31 +00001224 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1225 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
Reid Spencer54c6e842007-03-03 06:22:22 +00001226 GenericValue Dest;
Reid Spencer40015aa2007-03-06 03:09:31 +00001227 const Type * Ty = CE->getOperand(0)->getType();
Reid Spencer54c6e842007-03-03 06:22:22 +00001228 switch (CE->getOpcode()) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001229 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
1230 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
1231 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
1232 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
1233 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
1234 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
Reid Spencer40015aa2007-03-06 03:09:31 +00001235 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1236 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1237 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1238 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1239 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1240 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
Dan Gohmana5b96452009-06-04 22:49:04 +00001241 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
1242 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
1243 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
Reid Spencer40015aa2007-03-06 03:09:31 +00001244 case Instruction::Shl:
1245 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1246 break;
1247 case Instruction::LShr:
1248 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1249 break;
1250 case Instruction::AShr:
1251 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1252 break;
Reid Spencer54c6e842007-03-03 06:22:22 +00001253 default:
David Greene8121a812010-01-05 01:27:23 +00001254 dbgs() << "Unhandled ConstantExpr: " << *CE << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +00001255 llvm_unreachable(0);
Reid Spencer54c6e842007-03-03 06:22:22 +00001256 return GenericValue();
1257 }
Reid Spencerec8c51c2007-03-03 08:38:04 +00001258 return Dest;
Reid Spencer54c6e842007-03-03 06:22:22 +00001259}
1260
1261GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1262 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1263 return getConstantExprValue(CE, SF);
1264 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1265 return getConstantValue(CPV);
1266 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1267 return PTOGV(getPointerToGlobal(GV));
1268 } else {
1269 return SF.Values[V];
1270 }
1271}
1272
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001273//===----------------------------------------------------------------------===//
1274// Dispatch and Execution Code
1275//===----------------------------------------------------------------------===//
1276
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001277//===----------------------------------------------------------------------===//
Chris Lattner470754e2003-05-08 16:18:31 +00001278// callFunction - Execute the specified function...
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001279//
Chris Lattner470754e2003-05-08 16:18:31 +00001280void Interpreter::callFunction(Function *F,
1281 const std::vector<GenericValue> &ArgVals) {
Misha Brukman91fb9ab2005-04-21 22:43:08 +00001282 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1283 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1284 "Incorrect number of arguments passed into function call!");
Chris Lattnera28e1f22003-09-17 17:26:22 +00001285 // Make a new stack frame... and fill it in.
1286 ECStack.push_back(ExecutionContext());
1287 ExecutionContext &StackFrame = ECStack.back();
Chris Lattner470754e2003-05-08 16:18:31 +00001288 StackFrame.CurFunction = F;
Brian Gaeke65cac902003-11-07 05:22:49 +00001289
1290 // Special handling for external functions.
Reid Spencer5301e7c2007-01-30 20:08:39 +00001291 if (F->isDeclaration()) {
Brian Gaeke65cac902003-11-07 05:22:49 +00001292 GenericValue Result = callExternalFunction (F, ArgVals);
1293 // Simulate a 'ret' instruction of the appropriate type.
1294 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1295 return;
1296 }
1297
1298 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattner22e90432003-05-08 16:06:52 +00001299 StackFrame.CurBB = F->begin();
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001300 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001301
Chris Lattner62b7fd12002-04-07 20:49:59 +00001302 // Run through the function arguments and initialize their values...
Chris Lattner531f9e92005-03-15 04:54:21 +00001303 assert((ArgVals.size() == F->arg_size() ||
Misha Brukman91fb9ab2005-04-21 22:43:08 +00001304 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner62b7fd12002-04-07 20:49:59 +00001305 "Invalid number of values passed to function invocation!");
Chris Lattner22e90432003-05-08 16:06:52 +00001306
1307 // Handle non-varargs arguments...
Chris Lattner676d4112001-09-10 04:49:44 +00001308 unsigned i = 0;
Reid Spencerc57be6c2007-04-16 21:50:40 +00001309 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1310 AI != E; ++AI, ++i)
Chris Lattner7076ff22002-06-25 16:13:21 +00001311 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattner22e90432003-05-08 16:06:52 +00001312
1313 // Handle varargs arguments...
1314 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001315}
1316
Reid Spencer65ba7502007-05-16 02:05:13 +00001317
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001318void Interpreter::run() {
Brian Gaeke411281c2003-09-04 23:15:40 +00001319 while (!ECStack.empty()) {
Brian Gaekee278c222003-10-24 19:59:01 +00001320 // Interpret a single instruction & increment the "PC".
1321 ExecutionContext &SF = ECStack.back(); // Current stack frame
1322 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukman91fb9ab2005-04-21 22:43:08 +00001323
Brian Gaekee278c222003-10-24 19:59:01 +00001324 // Track the number of dynamic instructions executed.
1325 ++NumDynamicInsts;
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001326
David Greene8121a812010-01-05 01:27:23 +00001327 DEBUG(dbgs() << "About to interpret: " << I);
Brian Gaekee278c222003-10-24 19:59:01 +00001328 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner87ce0be2007-09-21 18:30:39 +00001329#if 0
1330 // This is not safe, as visiting the instruction could lower it and free I.
Chris Lattnerb25de3f2009-08-23 04:37:46 +00001331DEBUG(
Reid Spencer65ba7502007-05-16 02:05:13 +00001332 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1333 I.getType() != Type::VoidTy) {
David Greene8121a812010-01-05 01:27:23 +00001334 dbgs() << " --> ";
Chris Lattner87ce0be2007-09-21 18:30:39 +00001335 const GenericValue &Val = SF.Values[&I];
1336 switch (I.getType()->getTypeID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001337 default: llvm_unreachable("Invalid GenericValue Type");
David Greene8121a812010-01-05 01:27:23 +00001338 case Type::VoidTyID: dbgs() << "void"; break;
1339 case Type::FloatTyID: dbgs() << "float " << Val.FloatVal; break;
1340 case Type::DoubleTyID: dbgs() << "double " << Val.DoubleVal; break;
1341 case Type::PointerTyID: dbgs() << "void* " << intptr_t(Val.PointerVal);
Chris Lattner87ce0be2007-09-21 18:30:39 +00001342 break;
1343 case Type::IntegerTyID:
David Greene8121a812010-01-05 01:27:23 +00001344 dbgs() << "i" << Val.IntVal.getBitWidth() << " "
Chris Lattnerb25de3f2009-08-23 04:37:46 +00001345 << Val.IntVal.toStringUnsigned(10)
1346 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
Chris Lattner87ce0be2007-09-21 18:30:39 +00001347 break;
1348 }
Chris Lattnerb25de3f2009-08-23 04:37:46 +00001349 });
Chris Lattner87ce0be2007-09-21 18:30:39 +00001350#endif
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001351 }
1352}