blob: eb75e492e704df8e99acc8487b4c0efd1c6c6dcf [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/Statistic.h"
18#include "llvm/CodeGen/IntrinsicLowering.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/Instructions.h"
Chris Lattner663ceeb2008-07-08 17:25:49 +000022#include "llvm/Support/CommandLine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000024#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/Support/GetElementPtrTypeIterator.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>
Chris Lattner4af6de82003-11-25 20:44:56 +000029using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000030
Chris Lattnercecf56b2006-12-19 22:56:53 +000031STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner663ceeb2008-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 Lattner2e42d3a2001-10-15 05:51:48 +000036//===----------------------------------------------------------------------===//
Reid Spencere1aa0662007-03-03 06:22:22 +000037// Various Helper Functions
Chris Lattner39bb5b42001-10-15 13:25:40 +000038//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000039
Chris Lattner39bb5b42001-10-15 13:25:40 +000040static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000041 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +000042}
43
Chris Lattner2adcd832002-05-03 19:52:30 +000044//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000045// Binary Instruction Implementations
46//===----------------------------------------------------------------------===//
47
48#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
Reid Spencerf9536332007-03-06 03:09:31 +000049 case Type::TY##TyID: \
50 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
51 break
Chris Lattner92101ac2001-08-23 17:05:04 +000052
Dan Gohmanae3a0be2009-06-04 22:49:04 +000053static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000054 GenericValue Src2, Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000055 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +000056 IMPLEMENT_BINARY_OPERATOR(+, Float);
57 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000058 default:
David Greeneef573a32010-01-05 01:27:23 +000059 dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +000060 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +000061 }
Chris Lattner92101ac2001-08-23 17:05:04 +000062}
63
Dan Gohmanae3a0be2009-06-04 22:49:04 +000064static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000065 GenericValue Src2, Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000066 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +000067 IMPLEMENT_BINARY_OPERATOR(-, Float);
68 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000069 default:
David Greeneef573a32010-01-05 01:27:23 +000070 dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +000071 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +000072 }
Chris Lattner92101ac2001-08-23 17:05:04 +000073}
74
Dan Gohmanae3a0be2009-06-04 22:49:04 +000075static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000076 GenericValue Src2, Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000077 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +000078 IMPLEMENT_BINARY_OPERATOR(*, Float);
79 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +000080 default:
David Greeneef573a32010-01-05 01:27:23 +000081 dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +000082 llvm_unreachable(0);
Chris Lattnerc2593162001-10-27 08:28:11 +000083 }
Chris Lattnerc2593162001-10-27 08:28:11 +000084}
85
Reid Spencere1aa0662007-03-03 06:22:22 +000086static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000087 GenericValue Src2, Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000088 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +000089 IMPLEMENT_BINARY_OPERATOR(/, Float);
90 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +000091 default:
David Greeneef573a32010-01-05 01:27:23 +000092 dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +000093 llvm_unreachable(0);
Chris Lattnerbb76f022001-10-30 20:27:31 +000094 }
Chris Lattnerbb76f022001-10-30 20:27:31 +000095}
96
Reid Spencere1aa0662007-03-03 06:22:22 +000097static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000098 GenericValue Src2, Type *Ty) {
Reid Spencer0a783f72006-11-02 01:53:59 +000099 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-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 Greeneef573a32010-01-05 01:27:23 +0000107 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000108 llvm_unreachable(0);
Chris Lattnerc2593162001-10-27 08:28:11 +0000109 }
Chris Lattnerc2593162001-10-27 08:28:11 +0000110}
111
Reid Spencerf9536332007-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 Lattner92101ac2001-08-23 17:05:04 +0000116
Nadav Rotemaffe8892013-04-12 20:45:20 +0000117#define IMPLEMENT_VECTOR_INTEGER_ICMP(OP, TY) \
118 case Type::VectorTyID: { \
119 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \
120 Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \
121 for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \
122 Dest.AggregateVal[_i].IntVal = APInt(1, \
123 Src1.AggregateVal[_i].IntVal.OP(Src2.AggregateVal[_i].IntVal));\
124 } break;
125
Chris Lattnerfd506f52003-04-23 19:55:35 +0000126// Handle pointers specially because they must be compared with only as much
127// width as the host has. We _do not_ want to be comparing 64 bit values when
128// running on a 32-bit target, otherwise the upper 32 bits might mess up
129// comparisons if they contain garbage.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000130#define IMPLEMENT_POINTER_ICMP(OP) \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000131 case Type::PointerTyID: \
Reid Spencerf9536332007-03-06 03:09:31 +0000132 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
133 (void*)(intptr_t)Src2.PointerVal); \
134 break;
Chris Lattnerfd506f52003-04-23 19:55:35 +0000135
Reid Spencere4d87aa2006-12-23 06:05:41 +0000136static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000137 Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000138 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000139 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000140 IMPLEMENT_INTEGER_ICMP(eq,Ty);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000141 IMPLEMENT_VECTOR_INTEGER_ICMP(eq,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000142 IMPLEMENT_POINTER_ICMP(==);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000143 default:
David Greeneef573a32010-01-05 01:27:23 +0000144 dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000145 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000146 }
147 return Dest;
148}
149
150static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000151 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000152 GenericValue Dest;
153 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000154 IMPLEMENT_INTEGER_ICMP(ne,Ty);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000155 IMPLEMENT_VECTOR_INTEGER_ICMP(ne,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000156 IMPLEMENT_POINTER_ICMP(!=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000157 default:
David Greeneef573a32010-01-05 01:27:23 +0000158 dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000159 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000160 }
161 return Dest;
162}
163
164static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000165 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000166 GenericValue Dest;
167 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000168 IMPLEMENT_INTEGER_ICMP(ult,Ty);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000169 IMPLEMENT_VECTOR_INTEGER_ICMP(ult,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000170 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000171 default:
David Greeneef573a32010-01-05 01:27:23 +0000172 dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000173 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000174 }
175 return Dest;
176}
177
178static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000179 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000180 GenericValue Dest;
181 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000182 IMPLEMENT_INTEGER_ICMP(slt,Ty);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000183 IMPLEMENT_VECTOR_INTEGER_ICMP(slt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000184 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000185 default:
David Greeneef573a32010-01-05 01:27:23 +0000186 dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000187 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000188 }
189 return Dest;
190}
191
192static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000193 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000194 GenericValue Dest;
195 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000196 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000197 IMPLEMENT_VECTOR_INTEGER_ICMP(ugt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000198 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000199 default:
David Greeneef573a32010-01-05 01:27:23 +0000200 dbgs() << "Unhandled type for ICMP_UGT 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_SGT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000207 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000208 GenericValue Dest;
209 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000210 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000211 IMPLEMENT_VECTOR_INTEGER_ICMP(sgt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000212 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000213 default:
David Greeneef573a32010-01-05 01:27:23 +0000214 dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000215 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000216 }
217 return Dest;
218}
219
220static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000221 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000222 GenericValue Dest;
223 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000224 IMPLEMENT_INTEGER_ICMP(ule,Ty);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000225 IMPLEMENT_VECTOR_INTEGER_ICMP(ule,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000226 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000227 default:
David Greeneef573a32010-01-05 01:27:23 +0000228 dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000229 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000230 }
231 return Dest;
232}
233
234static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000235 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000236 GenericValue Dest;
237 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000238 IMPLEMENT_INTEGER_ICMP(sle,Ty);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000239 IMPLEMENT_VECTOR_INTEGER_ICMP(sle,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000240 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000241 default:
David Greeneef573a32010-01-05 01:27:23 +0000242 dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000243 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000244 }
245 return Dest;
246}
247
248static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000249 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000250 GenericValue Dest;
251 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000252 IMPLEMENT_INTEGER_ICMP(uge,Ty);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000253 IMPLEMENT_VECTOR_INTEGER_ICMP(uge,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000254 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000255 default:
David Greeneef573a32010-01-05 01:27:23 +0000256 dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000257 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000258 }
259 return Dest;
260}
261
262static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000263 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000264 GenericValue Dest;
265 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000266 IMPLEMENT_INTEGER_ICMP(sge,Ty);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000267 IMPLEMENT_VECTOR_INTEGER_ICMP(sge,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000268 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000269 default:
David Greeneef573a32010-01-05 01:27:23 +0000270 dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000271 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000272 }
273 return Dest;
274}
275
Reid Spencere49661b2006-12-31 05:51:36 +0000276void Interpreter::visitICmpInst(ICmpInst &I) {
277 ExecutionContext &SF = ECStack.back();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000278 Type *Ty = I.getOperand(0)->getType();
Reid Spencere49661b2006-12-31 05:51:36 +0000279 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
280 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
281 GenericValue R; // Result
282
283 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000284 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
285 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
Reid Spencere49661b2006-12-31 05:51:36 +0000286 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
287 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
288 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
289 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
290 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
291 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
292 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
293 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
294 default:
David Greeneef573a32010-01-05 01:27:23 +0000295 dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I;
Torok Edwinc23197a2009-07-14 16:55:14 +0000296 llvm_unreachable(0);
Reid Spencere49661b2006-12-31 05:51:36 +0000297 }
298
299 SetValue(&I, R, SF);
300}
301
302#define IMPLEMENT_FCMP(OP, TY) \
Reid Spencerf9536332007-03-06 03:09:31 +0000303 case Type::TY##TyID: \
304 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
305 break
Reid Spencere4d87aa2006-12-23 06:05:41 +0000306
Nadav Rotemaffe8892013-04-12 20:45:20 +0000307#define IMPLEMENT_VECTOR_FCMP_T(OP, TY) \
308 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \
309 Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \
310 for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \
311 Dest.AggregateVal[_i].IntVal = APInt(1, \
312 Src1.AggregateVal[_i].TY##Val OP Src2.AggregateVal[_i].TY##Val);\
313 break;
314
315#define IMPLEMENT_VECTOR_FCMP(OP) \
316 case Type::VectorTyID: \
317 if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) { \
318 IMPLEMENT_VECTOR_FCMP_T(OP, Float); \
319 } else { \
320 IMPLEMENT_VECTOR_FCMP_T(OP, Double); \
321 }
322
Reid Spencera54b7cb2007-01-12 07:05:14 +0000323static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000324 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000325 GenericValue Dest;
326 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000327 IMPLEMENT_FCMP(==, Float);
328 IMPLEMENT_FCMP(==, Double);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000329 IMPLEMENT_VECTOR_FCMP(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000330 default:
David Greeneef573a32010-01-05 01:27:23 +0000331 dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000332 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +0000333 }
334 return Dest;
335}
336
Nadav Rotemaffe8892013-04-12 20:45:20 +0000337#define IMPLEMENT_SCALAR_NANS(TY, X,Y) \
338 if (TY->isFloatTy()) { \
339 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
340 Dest.IntVal = APInt(1,false); \
341 return Dest; \
342 } \
343 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
344 Dest.IntVal = APInt(1,false); \
345 return Dest; \
346 }
347
348#define MASK_VECTOR_NANS_T(X,Y, TZ, FLAG) \
349 assert(X.AggregateVal.size() == Y.AggregateVal.size()); \
350 Dest.AggregateVal.resize( X.AggregateVal.size() ); \
351 for( uint32_t _i=0;_i<X.AggregateVal.size();_i++) { \
352 if (X.AggregateVal[_i].TZ##Val != X.AggregateVal[_i].TZ##Val || \
353 Y.AggregateVal[_i].TZ##Val != Y.AggregateVal[_i].TZ##Val) \
354 Dest.AggregateVal[_i].IntVal = APInt(1,FLAG); \
355 else { \
356 Dest.AggregateVal[_i].IntVal = APInt(1,!FLAG); \
357 } \
358 }
359
360#define MASK_VECTOR_NANS(TY, X,Y, FLAG) \
361 if (TY->isVectorTy()) \
362 if (dyn_cast<VectorType>(TY)->getElementType()->isFloatTy()) { \
363 MASK_VECTOR_NANS_T(X, Y, Float, FLAG) \
364 } else { \
365 MASK_VECTOR_NANS_T(X, Y, Double, FLAG) \
366 } \
367
368
369
Reid Spencera54b7cb2007-01-12 07:05:14 +0000370static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
Nadav Rotemaffe8892013-04-12 20:45:20 +0000371 Type *Ty)
372{
Chris Lattner92101ac2001-08-23 17:05:04 +0000373 GenericValue Dest;
Nadav Rotemaffe8892013-04-12 20:45:20 +0000374 // if input is scalar value and Src1 or Src2 is NaN return false
375 IMPLEMENT_SCALAR_NANS(Ty, Src1, Src2)
376 // if vector input detect NaNs and fill mask
377 MASK_VECTOR_NANS(Ty, Src1, Src2, false)
378 GenericValue DestMask = Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000379 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000380 IMPLEMENT_FCMP(!=, Float);
381 IMPLEMENT_FCMP(!=, Double);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000382 IMPLEMENT_VECTOR_FCMP(!=);
383 default:
384 dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
385 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +0000386 }
Nadav Rotemaffe8892013-04-12 20:45:20 +0000387 // in vector case mask out NaN elements
388 if (Ty->isVectorTy())
389 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
390 if (DestMask.AggregateVal[_i].IntVal == false)
391 Dest.AggregateVal[_i].IntVal = APInt(1,false);
392
Chris Lattner92101ac2001-08-23 17:05:04 +0000393 return Dest;
394}
395
Reid Spencera54b7cb2007-01-12 07:05:14 +0000396static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000397 Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000398 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000399 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000400 IMPLEMENT_FCMP(<=, Float);
401 IMPLEMENT_FCMP(<=, Double);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000402 IMPLEMENT_VECTOR_FCMP(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000403 default:
David Greeneef573a32010-01-05 01:27:23 +0000404 dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000405 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +0000406 }
407 return Dest;
408}
409
Reid Spencera54b7cb2007-01-12 07:05:14 +0000410static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000411 Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000412 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000413 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000414 IMPLEMENT_FCMP(>=, Float);
415 IMPLEMENT_FCMP(>=, Double);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000416 IMPLEMENT_VECTOR_FCMP(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000417 default:
David Greeneef573a32010-01-05 01:27:23 +0000418 dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000419 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +0000420 }
421 return Dest;
422}
423
Reid Spencera54b7cb2007-01-12 07:05:14 +0000424static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000425 Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000426 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000427 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000428 IMPLEMENT_FCMP(<, Float);
429 IMPLEMENT_FCMP(<, Double);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000430 IMPLEMENT_VECTOR_FCMP(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000431 default:
David Greeneef573a32010-01-05 01:27:23 +0000432 dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000433 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +0000434 }
435 return Dest;
436}
437
Reid Spencera54b7cb2007-01-12 07:05:14 +0000438static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000439 Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000440 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000441 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000442 IMPLEMENT_FCMP(>, Float);
443 IMPLEMENT_FCMP(>, Double);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000444 IMPLEMENT_VECTOR_FCMP(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000445 default:
David Greeneef573a32010-01-05 01:27:23 +0000446 dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000447 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +0000448 }
449 return Dest;
450}
451
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000452#define IMPLEMENT_UNORDERED(TY, X,Y) \
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000453 if (TY->isFloatTy()) { \
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000454 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
455 Dest.IntVal = APInt(1,true); \
456 return Dest; \
457 } \
458 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
459 Dest.IntVal = APInt(1,true); \
460 return Dest; \
461 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000462
Nadav Rotemaffe8892013-04-12 20:45:20 +0000463#define IMPLEMENT_VECTOR_UNORDERED(TY, X,Y, _FUNC) \
464 if (TY->isVectorTy()) { \
465 GenericValue DestMask = Dest; \
466 Dest = _FUNC(Src1, Src2, Ty); \
467 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++) \
468 if (DestMask.AggregateVal[_i].IntVal == true) \
469 Dest.AggregateVal[_i].IntVal = APInt(1,true); \
470 return Dest; \
471 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000472
473static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000474 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000475 GenericValue Dest;
476 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotemaffe8892013-04-12 20:45:20 +0000477 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
478 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OEQ)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000479 return executeFCMP_OEQ(Src1, Src2, Ty);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000480
Reid Spencera54b7cb2007-01-12 07:05:14 +0000481}
482
483static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000484 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000485 GenericValue Dest;
486 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotemaffe8892013-04-12 20:45:20 +0000487 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
488 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_ONE)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000489 return executeFCMP_ONE(Src1, Src2, Ty);
490}
491
492static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000493 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000494 GenericValue Dest;
495 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotemaffe8892013-04-12 20:45:20 +0000496 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
497 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLE)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000498 return executeFCMP_OLE(Src1, Src2, Ty);
499}
500
501static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000502 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000503 GenericValue Dest;
504 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotemaffe8892013-04-12 20:45:20 +0000505 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
506 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGE)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000507 return executeFCMP_OGE(Src1, Src2, Ty);
508}
509
510static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000511 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000512 GenericValue Dest;
513 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotemaffe8892013-04-12 20:45:20 +0000514 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
515 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLT)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000516 return executeFCMP_OLT(Src1, Src2, Ty);
517}
518
519static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000520 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000521 GenericValue Dest;
522 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotemaffe8892013-04-12 20:45:20 +0000523 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
524 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGT)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000525 return executeFCMP_OGT(Src1, Src2, Ty);
526}
527
528static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000529 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000530 GenericValue Dest;
Nadav Rotemaffe8892013-04-12 20:45:20 +0000531 if(Ty->isVectorTy()) {
532 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
533 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
534 if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
535 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
536 Dest.AggregateVal[_i].IntVal = APInt(1,
537 ( (Src1.AggregateVal[_i].FloatVal ==
538 Src1.AggregateVal[_i].FloatVal) &&
539 (Src2.AggregateVal[_i].FloatVal ==
540 Src2.AggregateVal[_i].FloatVal)));
541 } else {
542 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
543 Dest.AggregateVal[_i].IntVal = APInt(1,
544 ( (Src1.AggregateVal[_i].DoubleVal ==
545 Src1.AggregateVal[_i].DoubleVal) &&
546 (Src2.AggregateVal[_i].DoubleVal ==
547 Src2.AggregateVal[_i].DoubleVal)));
548 }
549 } else if (Ty->isFloatTy())
Reid Spencerf9536332007-03-06 03:09:31 +0000550 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
551 Src2.FloatVal == Src2.FloatVal));
Nadav Rotemaffe8892013-04-12 20:45:20 +0000552 else {
Reid Spencerf9536332007-03-06 03:09:31 +0000553 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
554 Src2.DoubleVal == Src2.DoubleVal));
Nadav Rotemaffe8892013-04-12 20:45:20 +0000555 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000556 return Dest;
557}
558
559static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000560 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000561 GenericValue Dest;
Nadav Rotemaffe8892013-04-12 20:45:20 +0000562 if(Ty->isVectorTy()) {
563 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
564 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
565 if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
566 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
567 Dest.AggregateVal[_i].IntVal = APInt(1,
568 ( (Src1.AggregateVal[_i].FloatVal !=
569 Src1.AggregateVal[_i].FloatVal) ||
570 (Src2.AggregateVal[_i].FloatVal !=
571 Src2.AggregateVal[_i].FloatVal)));
572 } else {
573 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
574 Dest.AggregateVal[_i].IntVal = APInt(1,
575 ( (Src1.AggregateVal[_i].DoubleVal !=
576 Src1.AggregateVal[_i].DoubleVal) ||
577 (Src2.AggregateVal[_i].DoubleVal !=
578 Src2.AggregateVal[_i].DoubleVal)));
579 }
580 } else if (Ty->isFloatTy())
Reid Spencerf9536332007-03-06 03:09:31 +0000581 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
582 Src2.FloatVal != Src2.FloatVal));
Nadav Rotemaffe8892013-04-12 20:45:20 +0000583 else {
Reid Spencerf9536332007-03-06 03:09:31 +0000584 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
585 Src2.DoubleVal != Src2.DoubleVal));
Nadav Rotemaffe8892013-04-12 20:45:20 +0000586 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000587 return Dest;
588}
589
Nadav Rotemaffe8892013-04-12 20:45:20 +0000590static GenericValue executeFCMP_BOOL(GenericValue Src1, GenericValue Src2,
591 const Type *Ty, const bool val) {
592 GenericValue Dest;
593 if(Ty->isVectorTy()) {
594 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
595 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
596 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
597 Dest.AggregateVal[_i].IntVal = APInt(1,val);
598 } else {
599 Dest.IntVal = APInt(1, val);
600 }
601
602 return Dest;
603}
604
Reid Spencere4d87aa2006-12-23 06:05:41 +0000605void Interpreter::visitFCmpInst(FCmpInst &I) {
606 ExecutionContext &SF = ECStack.back();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000607 Type *Ty = I.getOperand(0)->getType();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000608 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
609 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
610 GenericValue R; // Result
611
612 switch (I.getPredicate()) {
Nadav Rotemaffe8892013-04-12 20:45:20 +0000613 default:
614 dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I;
615 llvm_unreachable(0);
616 break;
617 case FCmpInst::FCMP_FALSE: R = executeFCMP_BOOL(Src1, Src2, Ty, false);
618 break;
619 case FCmpInst::FCMP_TRUE: R = executeFCMP_BOOL(Src1, Src2, Ty, true);
620 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000621 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
622 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
623 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
624 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
625 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
626 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
627 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
628 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
629 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
630 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
631 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
632 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
633 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
634 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000635 }
636
637 SetValue(&I, R, SF);
638}
639
Reid Spencere4d87aa2006-12-23 06:05:41 +0000640static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000641 GenericValue Src2, Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000642 GenericValue Result;
643 switch (predicate) {
644 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
645 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
646 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
647 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
648 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
649 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
650 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
651 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
652 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
653 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000654 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
655 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
656 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
657 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
658 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
659 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
660 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
661 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
662 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
663 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
664 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
665 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
666 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
667 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
Nadav Rotemaffe8892013-04-12 20:45:20 +0000668 case FCmpInst::FCMP_FALSE: return executeFCMP_BOOL(Src1, Src2, Ty, false);
669 case FCmpInst::FCMP_TRUE: return executeFCMP_BOOL(Src1, Src2, Ty, true);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000670 default:
David Greeneef573a32010-01-05 01:27:23 +0000671 dbgs() << "Unhandled Cmp predicate\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000672 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000673 }
674}
675
Chris Lattnerd7916e92003-05-10 21:22:39 +0000676void Interpreter::visitBinaryOperator(BinaryOperator &I) {
677 ExecutionContext &SF = ECStack.back();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000678 Type *Ty = I.getOperand(0)->getType();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000679 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
680 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000681 GenericValue R; // Result
682
Nadav Rotemaffe8892013-04-12 20:45:20 +0000683 // First process vector operation
684 if (Ty->isVectorTy()) {
685 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
686 R.AggregateVal.resize(Src1.AggregateVal.size());
Chris Lattner92101ac2001-08-23 17:05:04 +0000687
Nadav Rotemaffe8892013-04-12 20:45:20 +0000688 // Macros to execute binary operation 'OP' over integer vectors
689#define INTEGER_VECTOR_OPERATION(OP) \
690 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
691 R.AggregateVal[i].IntVal = \
692 Src1.AggregateVal[i].IntVal OP Src2.AggregateVal[i].IntVal;
693
694 // Additional macros to execute binary operations udiv/sdiv/urem/srem since
695 // they have different notation.
696#define INTEGER_VECTOR_FUNCTION(OP) \
697 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
698 R.AggregateVal[i].IntVal = \
699 Src1.AggregateVal[i].IntVal.OP(Src2.AggregateVal[i].IntVal);
700
701 // Macros to execute binary operation 'OP' over floating point type TY
702 // (float or double) vectors
703#define FLOAT_VECTOR_FUNCTION(OP, TY) \
704 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
705 R.AggregateVal[i].TY = \
706 Src1.AggregateVal[i].TY OP Src2.AggregateVal[i].TY;
707
708 // Macros to choose appropriate TY: float or double and run operation
709 // execution
710#define FLOAT_VECTOR_OP(OP) { \
711 if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) \
712 FLOAT_VECTOR_FUNCTION(OP, FloatVal) \
713 else { \
714 if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy()) \
715 FLOAT_VECTOR_FUNCTION(OP, DoubleVal) \
716 else { \
717 dbgs() << "Unhandled type for OP instruction: " << *Ty << "\n"; \
718 llvm_unreachable(0); \
719 } \
720 } \
721}
722
723 switch(I.getOpcode()){
724 default:
725 dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
726 llvm_unreachable(0);
727 break;
728 case Instruction::Add: INTEGER_VECTOR_OPERATION(+) break;
729 case Instruction::Sub: INTEGER_VECTOR_OPERATION(-) break;
730 case Instruction::Mul: INTEGER_VECTOR_OPERATION(*) break;
731 case Instruction::UDiv: INTEGER_VECTOR_FUNCTION(udiv) break;
732 case Instruction::SDiv: INTEGER_VECTOR_FUNCTION(sdiv) break;
733 case Instruction::URem: INTEGER_VECTOR_FUNCTION(urem) break;
734 case Instruction::SRem: INTEGER_VECTOR_FUNCTION(srem) break;
735 case Instruction::And: INTEGER_VECTOR_OPERATION(&) break;
736 case Instruction::Or: INTEGER_VECTOR_OPERATION(|) break;
737 case Instruction::Xor: INTEGER_VECTOR_OPERATION(^) break;
738 case Instruction::FAdd: FLOAT_VECTOR_OP(+) break;
739 case Instruction::FSub: FLOAT_VECTOR_OP(-) break;
740 case Instruction::FMul: FLOAT_VECTOR_OP(*) break;
741 case Instruction::FDiv: FLOAT_VECTOR_OP(/) break;
742 case Instruction::FRem:
743 if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy())
744 for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
745 R.AggregateVal[i].FloatVal =
746 fmod(Src1.AggregateVal[i].FloatVal, Src2.AggregateVal[i].FloatVal);
747 else {
748 if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy())
749 for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
750 R.AggregateVal[i].DoubleVal =
751 fmod(Src1.AggregateVal[i].DoubleVal, Src2.AggregateVal[i].DoubleVal);
752 else {
753 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
754 llvm_unreachable(0);
755 }
756 }
757 break;
758 }
759 } else {
760 switch (I.getOpcode()) {
761 default:
762 dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
763 llvm_unreachable(0);
764 break;
765 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
766 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
767 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
768 case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;
769 case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;
770 case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;
771 case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;
772 case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;
773 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
774 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
775 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
776 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
777 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
778 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
779 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
780 }
781 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000782 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000783}
784
Misha Brukmand1c881a2005-04-21 22:43:08 +0000785static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000786 GenericValue Src3) {
Reid Spencerf9536332007-03-06 03:09:31 +0000787 return Src1.IntVal == 0 ? Src3 : Src2;
Chris Lattner759d34f2004-04-20 16:43:21 +0000788}
789
790void Interpreter::visitSelectInst(SelectInst &I) {
791 ExecutionContext &SF = ECStack.back();
792 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
793 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
794 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
Reid Spencerf9536332007-03-06 03:09:31 +0000795 GenericValue R = executeSelectInst(Src1, Src2, Src3);
Chris Lattner759d34f2004-04-20 16:43:21 +0000796 SetValue(&I, R, SF);
797}
798
799
Chris Lattner92101ac2001-08-23 17:05:04 +0000800//===----------------------------------------------------------------------===//
801// Terminator Instruction Implementations
802//===----------------------------------------------------------------------===//
803
Chris Lattnere43db882001-10-27 04:15:57 +0000804void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000805 // runAtExitHandlers() assumes there are no stack frames, but
806 // if exit() was called, then it had a stack frame. Blow away
807 // the stack before interpreting atexit handlers.
Chris Lattnerf32a6a32009-10-29 05:26:09 +0000808 ECStack.clear();
809 runAtExitHandlers();
810 exit(GV.IntVal.zextOrTrunc(32).getZExtValue());
Chris Lattnere43db882001-10-27 04:15:57 +0000811}
812
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000813/// Pop the last stack frame off of ECStack and then copy the result
814/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000815/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000816/// CallInst if there was a previous stack frame. This method may
817/// invalidate any ECStack iterators you have. This method also takes
818/// care of switching to the normal destination BB, if we are returning
819/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000820///
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000821void Interpreter::popStackAndReturnValueToCaller(Type *RetTy,
Chris Lattnerf32a6a32009-10-29 05:26:09 +0000822 GenericValue Result) {
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000823 // Pop the current stack frame.
824 ECStack.pop_back();
825
Misha Brukmand1c881a2005-04-21 22:43:08 +0000826 if (ECStack.empty()) { // Finished main. Put result into exit code...
Dan Gohman96343312010-06-18 02:01:10 +0000827 if (RetTy && !RetTy->isVoidTy()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000828 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000829 } else {
Reid Spencere7707872007-06-01 22:23:29 +0000830 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000831 }
832 } else {
833 // If we have a previous stack frame, and we have a previous call,
834 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000835 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000836 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000837 // Save result...
Benjamin Kramerf0127052010-01-05 13:12:22 +0000838 if (!CallingSF.Caller.getType()->isVoidTy())
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000839 SetValue(I, Result, CallingSF);
840 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
841 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000842 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000843 }
844 }
845}
846
Chris Lattnerd7916e92003-05-10 21:22:39 +0000847void Interpreter::visitReturnInst(ReturnInst &I) {
848 ExecutionContext &SF = ECStack.back();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000849 Type *RetTy = Type::getVoidTy(I.getContext());
Chris Lattner92101ac2001-08-23 17:05:04 +0000850 GenericValue Result;
851
852 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000853 if (I.getNumOperands()) {
854 RetTy = I.getReturnValue()->getType();
855 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000856 }
857
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000858 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000859}
860
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000861void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Chris Lattner75361b62010-04-07 22:58:41 +0000862 report_fatal_error("Program executed an 'unreachable' instruction!");
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000863}
864
Chris Lattnerd7916e92003-05-10 21:22:39 +0000865void Interpreter::visitBranchInst(BranchInst &I) {
866 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000867 BasicBlock *Dest;
868
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000869 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
870 if (!I.isUnconditional()) {
871 Value *Cond = I.getCondition();
Reid Spencerf9536332007-03-06 03:09:31 +0000872 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000873 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000874 }
Chris Lattner77113b62003-05-10 20:21:16 +0000875 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000876}
877
Chris Lattnerd7916e92003-05-10 21:22:39 +0000878void Interpreter::visitSwitchInst(SwitchInst &I) {
879 ExecutionContext &SF = ECStack.back();
Eli Friedmanbb5a7442011-09-29 20:21:17 +0000880 Value* Cond = I.getCondition();
881 Type *ElTy = Cond->getType();
882 GenericValue CondVal = getOperandValue(Cond, SF);
Chris Lattner09e93922003-04-22 20:34:47 +0000883
884 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000885 BasicBlock *Dest = 0;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +0000886 for (SwitchInst::CaseIt i = I.case_begin(), e = I.case_end(); i != e; ++i) {
Stepan Dyatkovskiy47cbc4e2012-06-23 10:58:58 +0000887 IntegersSubset& Case = i.getCaseValueEx();
888 if (Case.isSingleNumber()) {
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +0000889 // FIXME: Currently work with ConstantInt based numbers.
Stepan Dyatkovskiy47cbc4e2012-06-23 10:58:58 +0000890 const ConstantInt *CI = Case.getSingleNumber(0).toConstantInt();
891 GenericValue Val = getOperandValue(const_cast<ConstantInt*>(CI), SF);
892 if (executeICMP_EQ(Val, CondVal, ElTy).IntVal != 0) {
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +0000893 Dest = cast<BasicBlock>(i.getCaseSuccessor());
894 break;
895 }
Chris Lattner09e93922003-04-22 20:34:47 +0000896 }
Stepan Dyatkovskiy47cbc4e2012-06-23 10:58:58 +0000897 if (Case.isSingleNumbersOnly()) {
898 for (unsigned n = 0, en = Case.getNumItems(); n != en; ++n) {
899 // FIXME: Currently work with ConstantInt based numbers.
900 const ConstantInt *CI = Case.getSingleNumber(n).toConstantInt();
901 GenericValue Val = getOperandValue(const_cast<ConstantInt*>(CI), SF);
902 if (executeICMP_EQ(Val, CondVal, ElTy).IntVal != 0) {
903 Dest = cast<BasicBlock>(i.getCaseSuccessor());
904 break;
905 }
906 }
907 } else
908 for (unsigned n = 0, en = Case.getNumItems(); n != en; ++n) {
909 IntegersSubset::Range r = Case.getItem(n);
910 // FIXME: Currently work with ConstantInt based numbers.
911 const ConstantInt *LowCI = r.getLow().toConstantInt();
912 const ConstantInt *HighCI = r.getHigh().toConstantInt();
913 GenericValue Low = getOperandValue(const_cast<ConstantInt*>(LowCI), SF);
914 GenericValue High = getOperandValue(const_cast<ConstantInt*>(HighCI), SF);
915 if (executeICMP_ULE(Low, CondVal, ElTy).IntVal != 0 &&
916 executeICMP_ULE(CondVal, High, ElTy).IntVal != 0) {
917 Dest = cast<BasicBlock>(i.getCaseSuccessor());
918 break;
919 }
920 }
Eli Friedmanbb5a7442011-09-29 20:21:17 +0000921 }
Chris Lattner09e93922003-04-22 20:34:47 +0000922 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000923 SwitchToNewBasicBlock(Dest, SF);
924}
925
Chris Lattnerf32a6a32009-10-29 05:26:09 +0000926void Interpreter::visitIndirectBrInst(IndirectBrInst &I) {
927 ExecutionContext &SF = ECStack.back();
928 void *Dest = GVTOP(getOperandValue(I.getAddress(), SF));
929 SwitchToNewBasicBlock((BasicBlock*)Dest, SF);
930}
931
932
Chris Lattner77113b62003-05-10 20:21:16 +0000933// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
934// This function handles the actual updating of block and instruction iterators
935// as well as execution of all of the PHI nodes in the destination block.
936//
937// This method does this because all of the PHI nodes must be executed
938// atomically, reading their inputs before any of the results are updated. Not
939// doing this can cause problems if the PHI nodes depend on other PHI nodes for
940// their inputs. If the input PHI node is updated before it is read, incorrect
941// results can happen. Thus we use a two phase approach.
942//
943void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
944 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
945 SF.CurBB = Dest; // Update CurBB to branch destination
946 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
947
948 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
949
950 // Loop over all of the PHI nodes in the current block, reading their inputs.
951 std::vector<GenericValue> ResultValues;
952
953 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
954 // Search for the value corresponding to this previous bb...
955 int i = PN->getBasicBlockIndex(PrevBB);
956 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
957 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000958
Chris Lattner77113b62003-05-10 20:21:16 +0000959 // Save the incoming value for this PHI node...
960 ResultValues.push_back(getOperandValue(IncomingValue, SF));
961 }
962
963 // Now loop over all of the PHI nodes setting their values...
964 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000965 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
966 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000967 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000968 }
Chris Lattner09e93922003-04-22 20:34:47 +0000969}
970
Chris Lattner92101ac2001-08-23 17:05:04 +0000971//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000972// Memory Instruction Implementations
973//===----------------------------------------------------------------------===//
974
Victor Hernandez7b929da2009-10-23 21:09:37 +0000975void Interpreter::visitAllocaInst(AllocaInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000976 ExecutionContext &SF = ECStack.back();
977
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000978 Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000979
Chris Lattnercc82cc12002-04-28 21:57:33 +0000980 // Get the number of elements being allocated by the array...
Reid Spencerf9536332007-03-06 03:09:31 +0000981 unsigned NumElements =
982 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
983
Duncan Sands777d2302009-05-09 07:06:46 +0000984 unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
Reid Spencerf9536332007-03-06 03:09:31 +0000985
Gabor Greif724441e2007-10-11 19:40:35 +0000986 // Avoid malloc-ing zero bytes, use max()...
987 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
Chris Lattner86660982001-08-27 05:16:50 +0000988
989 // Allocate enough memory to hold the type...
Reid Spencerf9536332007-03-06 03:09:31 +0000990 void *Memory = malloc(MemToAlloc);
991
David Greeneef573a32010-01-05 01:27:23 +0000992 DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
Chris Lattner79c5d3f2009-08-23 04:52:46 +0000993 << NumElements << " (Total: " << MemToAlloc << ") at "
994 << uintptr_t(Memory) << '\n');
Chris Lattner9bffa732002-02-19 18:50:09 +0000995
Chris Lattnerfe11a972002-12-23 23:59:41 +0000996 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000997 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000998 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000999
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001000 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +00001001 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +00001002}
1003
Chris Lattnera34c5682002-08-27 22:33:45 +00001004// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +00001005//
Chris Lattner4af6de82003-11-25 20:44:56 +00001006GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001007 gep_type_iterator E,
1008 ExecutionContext &SF) {
Duncan Sands1df98592010-02-16 11:11:14 +00001009 assert(Ptr->getType()->isPointerTy() &&
Chris Lattner95c3af52001-10-29 19:32:19 +00001010 "Cannot getElementOffset of a nonpointer type!");
1011
Reid Spencerf9536332007-03-06 03:09:31 +00001012 uint64_t Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +00001013
1014 for (; I != E; ++I) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001015 if (StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +00001016 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +00001017
Reid Spencerb83eb642006-10-20 07:07:24 +00001018 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
1019 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +00001020
Reid Spencerf9536332007-03-06 03:09:31 +00001021 Total += SLO->getElementOffset(Index);
Chris Lattner4af6de82003-11-25 20:44:56 +00001022 } else {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001023 SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +00001024 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +00001025 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
1026
Reid Spencera54b7cb2007-01-12 07:05:14 +00001027 int64_t Idx;
1028 unsigned BitWidth =
1029 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
Reid Spencer23e28832007-01-18 01:25:42 +00001030 if (BitWidth == 32)
Reid Spencerf9536332007-03-06 03:09:31 +00001031 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
Chris Lattneraee7f212008-04-06 21:50:58 +00001032 else {
1033 assert(BitWidth == 64 && "Invalid index type for getelementptr");
Reid Spencerf9536332007-03-06 03:09:31 +00001034 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
Chris Lattneraee7f212008-04-06 21:50:58 +00001035 }
Duncan Sands777d2302009-05-09 07:06:46 +00001036 Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001037 }
Chris Lattner95c3af52001-10-29 19:32:19 +00001038 }
1039
Chris Lattnera34c5682002-08-27 22:33:45 +00001040 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +00001041 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
David Greeneef573a32010-01-05 01:27:23 +00001042 DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n");
Chris Lattnera34c5682002-08-27 22:33:45 +00001043 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +00001044}
1045
Chris Lattnerd7916e92003-05-10 21:22:39 +00001046void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1047 ExecutionContext &SF = ECStack.back();
Owen Andersonfdca74c2009-06-26 16:46:15 +00001048 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +00001049 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +00001050}
1051
Chris Lattnerd7916e92003-05-10 21:22:39 +00001052void Interpreter::visitLoadInst(LoadInst &I) {
1053 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001054 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +00001055 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Reid Spencere0929362007-03-03 08:38:04 +00001056 GenericValue Result;
Reid Spencere0929362007-03-03 08:38:04 +00001057 LoadValueFromMemory(Result, Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001058 SetValue(&I, Result, SF);
Chris Lattner663ceeb2008-07-08 17:25:49 +00001059 if (I.isVolatile() && PrintVolatile)
David Greeneef573a32010-01-05 01:27:23 +00001060 dbgs() << "Volatile load " << I;
Chris Lattner86660982001-08-27 05:16:50 +00001061}
1062
Chris Lattnerd7916e92003-05-10 21:22:39 +00001063void Interpreter::visitStoreInst(StoreInst &I) {
1064 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +00001065 GenericValue Val = getOperandValue(I.getOperand(0), SF);
1066 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +00001067 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +00001068 I.getOperand(0)->getType());
Chris Lattner663ceeb2008-07-08 17:25:49 +00001069 if (I.isVolatile() && PrintVolatile)
David Greeneef573a32010-01-05 01:27:23 +00001070 dbgs() << "Volatile store: " << I;
Chris Lattnerfddc7552002-10-15 20:34:05 +00001071}
1072
Chris Lattner86660982001-08-27 05:16:50 +00001073//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +00001074// Miscellaneous Instruction Implementations
1075//===----------------------------------------------------------------------===//
1076
Brian Gaekefea483d2003-11-07 20:04:22 +00001077void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001078 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +00001079
1080 // Check to see if this is an intrinsic function call...
Reid Spencer4ccf4622007-04-16 21:50:40 +00001081 Function *F = CS.getCalledFunction();
Chris Lattnerf32a6a32009-10-29 05:26:09 +00001082 if (F && F->isDeclaration())
Chris Lattner73011782003-12-28 09:44:37 +00001083 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +00001084 case Intrinsic::not_intrinsic:
1085 break;
Chris Lattner317201d2004-03-13 00:24:00 +00001086 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +00001087 GenericValue ArgIndex;
1088 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1089 ArgIndex.UIntPairVal.second = 0;
1090 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +00001091 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +00001092 }
Chris Lattner317201d2004-03-13 00:24:00 +00001093 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +00001094 return;
Chris Lattner317201d2004-03-13 00:24:00 +00001095 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +00001096 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1097 return;
1098 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +00001099 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +00001100 // class to transform it into hopefully tasty LLVM code.
1101 //
Chris Lattnerb8e237b2007-04-17 17:38:28 +00001102 BasicBlock::iterator me(CS.getInstruction());
Chris Lattner73011782003-12-28 09:44:37 +00001103 BasicBlock *Parent = CS.getInstruction()->getParent();
Chris Lattnerb8e237b2007-04-17 17:38:28 +00001104 bool atBegin(Parent->begin() == me);
1105 if (!atBegin)
1106 --me;
Chris Lattner73011782003-12-28 09:44:37 +00001107 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1108
1109 // Restore the CurInst pointer to the first instruction newly inserted, if
1110 // any.
Chris Lattnerb8e237b2007-04-17 17:38:28 +00001111 if (atBegin) {
Chris Lattner73011782003-12-28 09:44:37 +00001112 SF.CurInst = Parent->begin();
1113 } else {
Chris Lattnerb8e237b2007-04-17 17:38:28 +00001114 SF.CurInst = me;
Chris Lattner73011782003-12-28 09:44:37 +00001115 ++SF.CurInst;
1116 }
Brian Gaekeb440dea2004-04-23 18:05:28 +00001117 return;
Chris Lattner73011782003-12-28 09:44:37 +00001118 }
1119
Reid Spencer4ccf4622007-04-16 21:50:40 +00001120
Brian Gaekefea483d2003-11-07 20:04:22 +00001121 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +00001122 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +00001123 const unsigned NumArgs = SF.Caller.arg_size();
1124 ArgVals.reserve(NumArgs);
Reid Spencer4ccf4622007-04-16 21:50:40 +00001125 uint16_t pNum = 1;
Brian Gaekeae2495a2003-11-07 19:59:08 +00001126 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
Reid Spencer4ccf4622007-04-16 21:50:40 +00001127 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
Brian Gaekeae2495a2003-11-07 19:59:08 +00001128 Value *V = *i;
1129 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +00001130 }
Chris Lattner365a76e2001-09-10 04:49:44 +00001131
Misha Brukmand1c881a2005-04-21 22:43:08 +00001132 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001133 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +00001134 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +00001135 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +00001136}
1137
Reid Spencer832254e2007-02-02 02:16:23 +00001138void Interpreter::visitShl(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001139 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +00001140 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1141 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1142 GenericValue Dest;
Chris Lattner9029cb82009-01-16 20:17:02 +00001143 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
1144 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
1145 else
1146 Dest.IntVal = Src1.IntVal;
1147
Brian Gaeke63438cc2003-12-11 00:22:59 +00001148 SetValue(&I, Dest, SF);
1149}
1150
Reid Spencer832254e2007-02-02 02:16:23 +00001151void Interpreter::visitLShr(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001152 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +00001153 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1154 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1155 GenericValue Dest;
Chris Lattner9029cb82009-01-16 20:17:02 +00001156 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
1157 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
1158 else
1159 Dest.IntVal = Src1.IntVal;
1160
Reid Spencer3822ff52006-11-08 06:47:33 +00001161 SetValue(&I, Dest, SF);
1162}
1163
Reid Spencer832254e2007-02-02 02:16:23 +00001164void Interpreter::visitAShr(BinaryOperator &I) {
Reid Spencer3822ff52006-11-08 06:47:33 +00001165 ExecutionContext &SF = ECStack.back();
Reid Spencer3822ff52006-11-08 06:47:33 +00001166 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1167 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner9029cb82009-01-16 20:17:02 +00001168 GenericValue Dest;
1169 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
1170 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
1171 else
1172 Dest.IntVal = Src1.IntVal;
1173
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001174 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001175}
1176
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001177GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001178 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001179 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001180 IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001181 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +00001182 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
Chris Lattnera34c5682002-08-27 22:33:45 +00001183 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001184}
Chris Lattner92101ac2001-08-23 17:05:04 +00001185
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001186GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001187 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001188 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001189 IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001190 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +00001191 Dest.IntVal = Src.IntVal.sext(DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001192 return Dest;
1193}
1194
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001195GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001196 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001197 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001198 IntegerType *DITy = cast<IntegerType>(DstTy);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001199 unsigned DBitWidth = DITy->getBitWidth();
Reid Spencerf9536332007-03-06 03:09:31 +00001200 Dest.IntVal = Src.IntVal.zext(DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001201 return Dest;
1202}
1203
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001204GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001205 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001206 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001207 assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001208 "Invalid FPTrunc instruction");
1209 Dest.FloatVal = (float) Src.DoubleVal;
1210 return Dest;
1211}
1212
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001213GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001214 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001215 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001216 assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001217 "Invalid FPTrunc instruction");
1218 Dest.DoubleVal = (double) Src.FloatVal;
1219 return Dest;
1220}
1221
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001222GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001223 ExecutionContext &SF) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001224 Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +00001225 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001226 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001227 assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001228
Reid Spencera54b7cb2007-01-12 07:05:14 +00001229 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001230 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001231 else
Reid Spencerf9536332007-03-06 03:09:31 +00001232 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001233 return Dest;
1234}
1235
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001236GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001237 ExecutionContext &SF) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001238 Type *SrcTy = SrcVal->getType();
Reid Spencerf9536332007-03-06 03:09:31 +00001239 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001240 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001241 assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001242
Reid Spencera54b7cb2007-01-12 07:05:14 +00001243 if (SrcTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001244 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001245 else
Reid Spencerf9536332007-03-06 03:09:31 +00001246 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001247 return Dest;
1248}
1249
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001250GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001251 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001252 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001253 assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001254
Reid Spencera54b7cb2007-01-12 07:05:14 +00001255 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001256 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001257 else
Reid Spencerf9536332007-03-06 03:09:31 +00001258 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001259 return Dest;
1260}
1261
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001262GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001263 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001264 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001265 assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001266
Reid Spencera54b7cb2007-01-12 07:05:14 +00001267 if (DstTy->getTypeID() == Type::FloatTyID)
Reid Spencerf9536332007-03-06 03:09:31 +00001268 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001269 else
Reid Spencerf9536332007-03-06 03:09:31 +00001270 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001271 return Dest;
Reid Spencerf9536332007-03-06 03:09:31 +00001272
Reid Spencera54b7cb2007-01-12 07:05:14 +00001273}
1274
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001275GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001276 ExecutionContext &SF) {
Reid Spencerf9536332007-03-06 03:09:31 +00001277 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001278 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sands1df98592010-02-16 11:11:14 +00001279 assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001280
Reid Spencerf9536332007-03-06 03:09:31 +00001281 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001282 return Dest;
1283}
1284
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001285GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001286 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001287 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sands1df98592010-02-16 11:11:14 +00001288 assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001289
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001290 uint32_t PtrSize = TD.getPointerSizeInBits();
Reid Spencer7553c342007-03-06 03:41:50 +00001291 if (PtrSize != Src.IntVal.getBitWidth())
Reid Spencer57631052007-03-06 03:46:41 +00001292 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
Reid Spencer7553c342007-03-06 03:41:50 +00001293
Reid Spencer6bc63332007-04-26 18:19:35 +00001294 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
Reid Spencera54b7cb2007-01-12 07:05:14 +00001295 return Dest;
1296}
1297
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001298GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001299 ExecutionContext &SF) {
1300
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001301 Type *SrcTy = SrcVal->getType();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001302 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sands1df98592010-02-16 11:11:14 +00001303 if (DstTy->isPointerTy()) {
1304 assert(SrcTy->isPointerTy() && "Invalid BitCast");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001305 Dest.PointerVal = Src.PointerVal;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001306 } else if (DstTy->isIntegerTy()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001307 if (SrcTy->isFloatTy()) {
Jay Foade4d19c92010-11-28 21:04:48 +00001308 Dest.IntVal = APInt::floatToBits(Src.FloatVal);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001309 } else if (SrcTy->isDoubleTy()) {
Jay Foade4d19c92010-11-28 21:04:48 +00001310 Dest.IntVal = APInt::doubleToBits(Src.DoubleVal);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001311 } else if (SrcTy->isIntegerTy()) {
Reid Spencerf9536332007-03-06 03:09:31 +00001312 Dest.IntVal = Src.IntVal;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001313 } else
Torok Edwinc23197a2009-07-14 16:55:14 +00001314 llvm_unreachable("Invalid BitCast");
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001315 } else if (DstTy->isFloatTy()) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001316 if (SrcTy->isIntegerTy())
Reid Spencerf9536332007-03-06 03:09:31 +00001317 Dest.FloatVal = Src.IntVal.bitsToFloat();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001318 else
1319 Dest.FloatVal = Src.FloatVal;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001320 } else if (DstTy->isDoubleTy()) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001321 if (SrcTy->isIntegerTy())
Reid Spencerf9536332007-03-06 03:09:31 +00001322 Dest.DoubleVal = Src.IntVal.bitsToDouble();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001323 else
1324 Dest.DoubleVal = Src.DoubleVal;
1325 } else
Torok Edwinc23197a2009-07-14 16:55:14 +00001326 llvm_unreachable("Invalid Bitcast");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001327
1328 return Dest;
1329}
1330
1331void Interpreter::visitTruncInst(TruncInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001332 ExecutionContext &SF = ECStack.back();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001333 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1334}
1335
1336void Interpreter::visitSExtInst(SExtInst &I) {
1337 ExecutionContext &SF = ECStack.back();
1338 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1339}
1340
1341void Interpreter::visitZExtInst(ZExtInst &I) {
1342 ExecutionContext &SF = ECStack.back();
1343 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1344}
1345
1346void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1347 ExecutionContext &SF = ECStack.back();
1348 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1349}
1350
1351void Interpreter::visitFPExtInst(FPExtInst &I) {
1352 ExecutionContext &SF = ECStack.back();
1353 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1354}
1355
1356void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1357 ExecutionContext &SF = ECStack.back();
1358 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1359}
1360
1361void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1362 ExecutionContext &SF = ECStack.back();
1363 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1364}
1365
1366void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1367 ExecutionContext &SF = ECStack.back();
1368 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1369}
1370
1371void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1372 ExecutionContext &SF = ECStack.back();
1373 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1374}
1375
1376void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1377 ExecutionContext &SF = ECStack.back();
1378 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1379}
1380
1381void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1382 ExecutionContext &SF = ECStack.back();
1383 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1384}
1385
1386void Interpreter::visitBitCastInst(BitCastInst &I) {
1387 ExecutionContext &SF = ECStack.back();
1388 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001389}
Chris Lattner92101ac2001-08-23 17:05:04 +00001390
Brian Gaekec1a2be12003-11-07 21:20:47 +00001391#define IMPLEMENT_VAARG(TY) \
1392 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1393
1394void Interpreter::visitVAArgInst(VAArgInst &I) {
1395 ExecutionContext &SF = ECStack.back();
1396
Brian Gaeke9d20b712004-02-25 23:01:48 +00001397 // Get the incoming valist parameter. LLI treats the valist as a
1398 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001399 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001400 GenericValue Dest;
1401 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Reid Spencerf9536332007-03-06 03:09:31 +00001402 .VarArgs[VAList.UIntPairVal.second];
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001403 Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001404 switch (Ty->getTypeID()) {
Jim Grosbachcce07c92013-01-31 19:46:59 +00001405 case Type::IntegerTyID:
1406 Dest.IntVal = Src.IntVal;
Jim Grosbachcce07c92013-01-31 19:46:59 +00001407 break;
Jim Grosbach9f285c62013-02-01 18:57:06 +00001408 IMPLEMENT_VAARG(Pointer);
1409 IMPLEMENT_VAARG(Float);
1410 IMPLEMENT_VAARG(Double);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001411 default:
David Greeneef573a32010-01-05 01:27:23 +00001412 dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001413 llvm_unreachable(0);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001414 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001415
Brian Gaekec1a2be12003-11-07 21:20:47 +00001416 // Set the Value of this Instruction.
1417 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001418
1419 // Move the pointer to the next vararg.
1420 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001421}
1422
Nadav Rotem953783e2013-04-01 15:53:30 +00001423void Interpreter::visitExtractElementInst(ExtractElementInst &I) {
1424 ExecutionContext &SF = ECStack.back();
1425 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1426 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1427 GenericValue Dest;
1428
1429 Type *Ty = I.getType();
1430 const unsigned indx = unsigned(Src2.IntVal.getZExtValue());
1431
1432 if(Src1.AggregateVal.size() > indx) {
1433 switch (Ty->getTypeID()) {
1434 default:
1435 dbgs() << "Unhandled destination type for extractelement instruction: "
1436 << *Ty << "\n";
1437 llvm_unreachable(0);
1438 break;
1439 case Type::IntegerTyID:
1440 Dest.IntVal = Src1.AggregateVal[indx].IntVal;
1441 break;
1442 case Type::FloatTyID:
1443 Dest.FloatVal = Src1.AggregateVal[indx].FloatVal;
1444 break;
1445 case Type::DoubleTyID:
1446 Dest.DoubleVal = Src1.AggregateVal[indx].DoubleVal;
1447 break;
1448 }
1449 } else {
1450 dbgs() << "Invalid index in extractelement instruction\n";
1451 }
1452
1453 SetValue(&I, Dest, SF);
1454}
1455
Reid Spencere1aa0662007-03-03 06:22:22 +00001456GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1457 ExecutionContext &SF) {
1458 switch (CE->getOpcode()) {
1459 case Instruction::Trunc:
1460 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1461 case Instruction::ZExt:
1462 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1463 case Instruction::SExt:
1464 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1465 case Instruction::FPTrunc:
1466 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1467 case Instruction::FPExt:
1468 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1469 case Instruction::UIToFP:
1470 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1471 case Instruction::SIToFP:
1472 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1473 case Instruction::FPToUI:
1474 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1475 case Instruction::FPToSI:
1476 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1477 case Instruction::PtrToInt:
1478 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1479 case Instruction::IntToPtr:
1480 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1481 case Instruction::BitCast:
1482 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1483 case Instruction::GetElementPtr:
1484 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1485 gep_type_end(CE), SF);
Reid Spencere0929362007-03-03 08:38:04 +00001486 case Instruction::FCmp:
1487 case Instruction::ICmp:
1488 return executeCmpInst(CE->getPredicate(),
1489 getOperandValue(CE->getOperand(0), SF),
1490 getOperandValue(CE->getOperand(1), SF),
1491 CE->getOperand(0)->getType());
1492 case Instruction::Select:
1493 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1494 getOperandValue(CE->getOperand(1), SF),
1495 getOperandValue(CE->getOperand(2), SF));
Reid Spencere1aa0662007-03-03 06:22:22 +00001496 default :
1497 break;
1498 }
Reid Spencere0929362007-03-03 08:38:04 +00001499
1500 // The cases below here require a GenericValue parameter for the result
1501 // so we initialize one, compute it and then return it.
Reid Spencerf9536332007-03-06 03:09:31 +00001502 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1503 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
Reid Spencere1aa0662007-03-03 06:22:22 +00001504 GenericValue Dest;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001505 Type * Ty = CE->getOperand(0)->getType();
Reid Spencere1aa0662007-03-03 06:22:22 +00001506 switch (CE->getOpcode()) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001507 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
1508 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
1509 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
1510 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
1511 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
1512 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
Reid Spencerf9536332007-03-06 03:09:31 +00001513 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1514 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1515 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1516 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1517 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1518 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001519 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
1520 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
1521 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
Reid Spencerf9536332007-03-06 03:09:31 +00001522 case Instruction::Shl:
1523 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1524 break;
1525 case Instruction::LShr:
1526 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1527 break;
1528 case Instruction::AShr:
1529 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1530 break;
Reid Spencere1aa0662007-03-03 06:22:22 +00001531 default:
David Greeneef573a32010-01-05 01:27:23 +00001532 dbgs() << "Unhandled ConstantExpr: " << *CE << "\n";
Ahmed Charlesb0934ab2012-02-19 11:37:01 +00001533 llvm_unreachable("Unhandled ConstantExpr");
Reid Spencere1aa0662007-03-03 06:22:22 +00001534 }
Reid Spencere0929362007-03-03 08:38:04 +00001535 return Dest;
Reid Spencere1aa0662007-03-03 06:22:22 +00001536}
1537
1538GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1539 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1540 return getConstantExprValue(CE, SF);
1541 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1542 return getConstantValue(CPV);
1543 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1544 return PTOGV(getPointerToGlobal(GV));
1545 } else {
1546 return SF.Values[V];
1547 }
1548}
1549
Chris Lattner92101ac2001-08-23 17:05:04 +00001550//===----------------------------------------------------------------------===//
1551// Dispatch and Execution Code
1552//===----------------------------------------------------------------------===//
1553
Chris Lattner92101ac2001-08-23 17:05:04 +00001554//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001555// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001556//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001557void Interpreter::callFunction(Function *F,
1558 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001559 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1560 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1561 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001562 // Make a new stack frame... and fill it in.
1563 ECStack.push_back(ExecutionContext());
1564 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001565 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001566
1567 // Special handling for external functions.
Reid Spencer5cbf9852007-01-30 20:08:39 +00001568 if (F->isDeclaration()) {
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001569 GenericValue Result = callExternalFunction (F, ArgVals);
1570 // Simulate a 'ret' instruction of the appropriate type.
1571 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1572 return;
1573 }
1574
1575 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001576 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001577 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001578
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001579 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001580 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001581 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001582 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001583
1584 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001585 unsigned i = 0;
Reid Spencer4ccf4622007-04-16 21:50:40 +00001586 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1587 AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001588 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001589
1590 // Handle varargs arguments...
1591 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001592}
1593
Reid Spencer951418b2007-05-16 02:05:13 +00001594
Chris Lattner92101ac2001-08-23 17:05:04 +00001595void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001596 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001597 // Interpret a single instruction & increment the "PC".
1598 ExecutionContext &SF = ECStack.back(); // Current stack frame
1599 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001600
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001601 // Track the number of dynamic instructions executed.
1602 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001603
David Greeneef573a32010-01-05 01:27:23 +00001604 DEBUG(dbgs() << "About to interpret: " << I);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001605 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner27725bf2007-09-21 18:30:39 +00001606#if 0
1607 // This is not safe, as visiting the instruction could lower it and free I.
Chris Lattnerbdff5482009-08-23 04:37:46 +00001608DEBUG(
Reid Spencer951418b2007-05-16 02:05:13 +00001609 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1610 I.getType() != Type::VoidTy) {
David Greeneef573a32010-01-05 01:27:23 +00001611 dbgs() << " --> ";
Chris Lattner27725bf2007-09-21 18:30:39 +00001612 const GenericValue &Val = SF.Values[&I];
1613 switch (I.getType()->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001614 default: llvm_unreachable("Invalid GenericValue Type");
David Greeneef573a32010-01-05 01:27:23 +00001615 case Type::VoidTyID: dbgs() << "void"; break;
1616 case Type::FloatTyID: dbgs() << "float " << Val.FloatVal; break;
1617 case Type::DoubleTyID: dbgs() << "double " << Val.DoubleVal; break;
1618 case Type::PointerTyID: dbgs() << "void* " << intptr_t(Val.PointerVal);
Chris Lattner27725bf2007-09-21 18:30:39 +00001619 break;
1620 case Type::IntegerTyID:
David Greeneef573a32010-01-05 01:27:23 +00001621 dbgs() << "i" << Val.IntVal.getBitWidth() << " "
Chris Lattnerbdff5482009-08-23 04:37:46 +00001622 << Val.IntVal.toStringUnsigned(10)
1623 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
Chris Lattner27725bf2007-09-21 18:30:39 +00001624 break;
1625 }
Chris Lattnerbdff5482009-08-23 04:37:46 +00001626 });
Chris Lattner27725bf2007-09-21 18:30:39 +00001627#endif
Chris Lattner92101ac2001-08-23 17:05:04 +00001628 }
1629}