blob: 97411d3943c26f6f647f11e05e06639354176794 [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmand1c881a2005-04-21 22:43:08 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00009//
Chris Lattner92101ac2001-08-23 17:05:04 +000010// This file contains the actual instruction interpreter.
11//
12//===----------------------------------------------------------------------===//
13
Brian Gaeke63438cc2003-12-11 00:22:59 +000014#define DEBUG_TYPE "interpreter"
Chris Lattner92101ac2001-08-23 17:05:04 +000015#include "Interpreter.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner73011782003-12-28 09:44:37 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/Instructions.h"
Chris Lattner30483732004-06-20 07:49:54 +000019#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattner4af6de82003-11-25 20:44:56 +000020#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/Statistic.h"
22#include "llvm/Support/Debug.h"
Jeff Cohen97af7512006-12-02 02:22:01 +000023#include <cmath>
Chris Lattner4af6de82003-11-25 20:44:56 +000024using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000025
Chris Lattnercecf56b2006-12-19 22:56:53 +000026STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
27static Interpreter *TheEE = 0;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner73011782003-12-28 09:44:37 +000029
Chris Lattner2e42d3a2001-10-15 05:51:48 +000030//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000031// Value Manipulation code
32//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000033
Misha Brukmand1c881a2005-04-21 22:43:08 +000034static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
35 const Type *Ty);
36static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
37 const Type *Ty);
38static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
39 const Type *Ty);
Reid Spencer1628cec2006-10-26 06:15:43 +000040static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
41 const Type *Ty);
42static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
43 const Type *Ty);
44static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
45 const Type *Ty);
Reid Spencer0a783f72006-11-02 01:53:59 +000046static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
47 const Type *Ty);
48static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
49 const Type *Ty);
50static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
51 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000052static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
53 const Type *Ty);
54static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
55 const Type *Ty);
56static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
57 const Type *Ty);
Reid Spencere4d87aa2006-12-23 06:05:41 +000058static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
59 GenericValue Src2, const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000060static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
61 const Type *Ty);
Reid Spencer3822ff52006-11-08 06:47:33 +000062static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
63 const Type *Ty);
64static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
65 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000066static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +000067 GenericValue Src3);
68
Brian Gaeke63438cc2003-12-11 00:22:59 +000069GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
70 ExecutionContext &SF) {
71 switch (CE->getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +000072 case Instruction::Trunc:
73 case Instruction::ZExt:
74 case Instruction::SExt:
75 case Instruction::FPTrunc:
76 case Instruction::FPExt:
77 case Instruction::UIToFP:
78 case Instruction::SIToFP:
79 case Instruction::FPToUI:
80 case Instruction::FPToSI:
81 case Instruction::PtrToInt:
82 case Instruction::IntToPtr:
83 case Instruction::BitCast:
84 return executeCastOperation(Instruction::CastOps(CE->getOpcode()),
85 CE->getOperand(0), CE->getType(), SF);
Brian Gaeke63438cc2003-12-11 00:22:59 +000086 case Instruction::GetElementPtr:
87 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
88 gep_type_end(CE), SF);
89 case Instruction::Add:
90 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
91 getOperandValue(CE->getOperand(1), SF),
92 CE->getOperand(0)->getType());
93 case Instruction::Sub:
94 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
95 getOperandValue(CE->getOperand(1), SF),
96 CE->getOperand(0)->getType());
97 case Instruction::Mul:
98 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
99 getOperandValue(CE->getOperand(1), SF),
100 CE->getOperand(0)->getType());
Reid Spencer1628cec2006-10-26 06:15:43 +0000101 case Instruction::SDiv:
102 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
103 getOperandValue(CE->getOperand(1), SF),
104 CE->getOperand(0)->getType());
105 case Instruction::UDiv:
106 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
107 getOperandValue(CE->getOperand(1), SF),
108 CE->getOperand(0)->getType());
109 case Instruction::FDiv:
110 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
111 getOperandValue(CE->getOperand(1), SF),
112 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000113 case Instruction::URem:
114 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
Brian Gaeke63438cc2003-12-11 00:22:59 +0000115 getOperandValue(CE->getOperand(1), SF),
116 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000117 case Instruction::SRem:
118 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
119 getOperandValue(CE->getOperand(1), SF),
120 CE->getOperand(0)->getType());
121 case Instruction::FRem:
122 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
123 getOperandValue(CE->getOperand(1), SF),
124 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000125 case Instruction::And:
126 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
127 getOperandValue(CE->getOperand(1), SF),
128 CE->getOperand(0)->getType());
129 case Instruction::Or:
130 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
131 getOperandValue(CE->getOperand(1), SF),
132 CE->getOperand(0)->getType());
133 case Instruction::Xor:
134 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
135 getOperandValue(CE->getOperand(1), SF),
136 CE->getOperand(0)->getType());
Reid Spencere4d87aa2006-12-23 06:05:41 +0000137 case Instruction::FCmp:
138 case Instruction::ICmp:
139 return executeCmpInst(CE->getPredicate(),
140 getOperandValue(CE->getOperand(0), SF),
141 getOperandValue(CE->getOperand(1), SF),
142 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000143 case Instruction::Shl:
144 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
145 getOperandValue(CE->getOperand(1), SF),
146 CE->getOperand(0)->getType());
Reid Spencer3822ff52006-11-08 06:47:33 +0000147 case Instruction::LShr:
148 return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
149 getOperandValue(CE->getOperand(1), SF),
150 CE->getOperand(0)->getType());
151 case Instruction::AShr:
152 return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
153 getOperandValue(CE->getOperand(1), SF),
154 CE->getOperand(0)->getType());
Chris Lattner759d34f2004-04-20 16:43:21 +0000155 case Instruction::Select:
156 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
157 getOperandValue(CE->getOperand(1), SF),
158 getOperandValue(CE->getOperand(2), SF));
Brian Gaeke63438cc2003-12-11 00:22:59 +0000159 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000160 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Brian Gaeke63438cc2003-12-11 00:22:59 +0000161 abort();
162 return GenericValue();
163 }
164}
Chris Lattnera34c5682002-08-27 22:33:45 +0000165
Brian Gaeke29794cb2003-09-05 18:55:03 +0000166GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000167 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000168 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000169 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000170 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000171 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000172 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000173 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000174 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000175 }
176}
177
Chris Lattner39bb5b42001-10-15 13:25:40 +0000178static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000179 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000180}
181
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000182void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000183 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000184}
185
Chris Lattner2adcd832002-05-03 19:52:30 +0000186//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000187// Binary Instruction Implementations
188//===----------------------------------------------------------------------===//
189
190#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
191 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
192
Misha Brukmand1c881a2005-04-21 22:43:08 +0000193static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
194 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000195 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000196 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000197 IMPLEMENT_BINARY_OPERATOR(+, Int8);
198 IMPLEMENT_BINARY_OPERATOR(+, Int16);
199 IMPLEMENT_BINARY_OPERATOR(+, Int32);
200 IMPLEMENT_BINARY_OPERATOR(+, Int64);
Chris Lattner92101ac2001-08-23 17:05:04 +0000201 IMPLEMENT_BINARY_OPERATOR(+, Float);
202 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000203 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000204 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000205 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000206 }
207 return Dest;
208}
209
Misha Brukmand1c881a2005-04-21 22:43:08 +0000210static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
211 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000212 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000213 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000214 IMPLEMENT_BINARY_OPERATOR(-, Int8);
215 IMPLEMENT_BINARY_OPERATOR(-, Int16);
216 IMPLEMENT_BINARY_OPERATOR(-, Int32);
217 IMPLEMENT_BINARY_OPERATOR(-, Int64);
Chris Lattner92101ac2001-08-23 17:05:04 +0000218 IMPLEMENT_BINARY_OPERATOR(-, Float);
219 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000220 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000221 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000222 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000223 }
224 return Dest;
225}
226
Misha Brukmand1c881a2005-04-21 22:43:08 +0000227static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
228 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000229 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000230 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000231 IMPLEMENT_BINARY_OPERATOR(*, Int8);
232 IMPLEMENT_BINARY_OPERATOR(*, Int16);
233 IMPLEMENT_BINARY_OPERATOR(*, Int32);
234 IMPLEMENT_BINARY_OPERATOR(*, Int64);
Chris Lattnerc2593162001-10-27 08:28:11 +0000235 IMPLEMENT_BINARY_OPERATOR(*, Float);
236 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000237 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000238 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000239 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000240 }
241 return Dest;
242}
243
Reid Spencere49661b2006-12-31 05:51:36 +0000244#define IMPLEMENT_SIGNLESS_BINOP(OP, TY, CAST) \
245 case Type::TY##TyID: Dest.TY##Val = \
246 ((CAST)Src1.TY##Val) OP ((CAST)Src2.TY##Val); break
Reid Spencerfe855262006-11-01 03:41:05 +0000247
Reid Spencer1628cec2006-10-26 06:15:43 +0000248static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
249 const Type *Ty) {
250 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000251 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000252 IMPLEMENT_SIGNLESS_BINOP(/, Int8, uint8_t);
253 IMPLEMENT_SIGNLESS_BINOP(/, Int16, uint16_t);
254 IMPLEMENT_SIGNLESS_BINOP(/, Int32, uint32_t);
255 IMPLEMENT_SIGNLESS_BINOP(/, Int64, uint64_t);
Reid Spencer1628cec2006-10-26 06:15:43 +0000256 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000257 cerr << "Unhandled type for UDiv instruction: " << *Ty << "\n";
Reid Spencer1628cec2006-10-26 06:15:43 +0000258 abort();
259 }
260 return Dest;
261}
262
263static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
264 const Type *Ty) {
265 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000266 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000267 IMPLEMENT_SIGNLESS_BINOP(/, Int8, int8_t);
268 IMPLEMENT_SIGNLESS_BINOP(/, Int16, int16_t);
269 IMPLEMENT_SIGNLESS_BINOP(/, Int32, int32_t);
270 IMPLEMENT_SIGNLESS_BINOP(/, Int64, int64_t);
Reid Spencer1628cec2006-10-26 06:15:43 +0000271 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000272 cerr << "Unhandled type for SDiv instruction: " << *Ty << "\n";
Reid Spencer1628cec2006-10-26 06:15:43 +0000273 abort();
274 }
275 return Dest;
276}
277
278static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000279 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000280 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000281 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000282 IMPLEMENT_BINARY_OPERATOR(/, Float);
283 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000284 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000285 cerr << "Unhandled type for Div instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000286 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000287 }
288 return Dest;
289}
290
Reid Spencer0a783f72006-11-02 01:53:59 +0000291static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000292 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000293 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000294 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000295 IMPLEMENT_SIGNLESS_BINOP(%, Int8, uint8_t);
296 IMPLEMENT_SIGNLESS_BINOP(%, Int16, uint16_t);
297 IMPLEMENT_SIGNLESS_BINOP(%, Int32, uint32_t);
298 IMPLEMENT_SIGNLESS_BINOP(%, Int64, uint64_t );
Reid Spencer0a783f72006-11-02 01:53:59 +0000299 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000300 cerr << "Unhandled type for URem instruction: " << *Ty << "\n";
Reid Spencer0a783f72006-11-02 01:53:59 +0000301 abort();
302 }
303 return Dest;
304}
305
306static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
307 const Type *Ty) {
308 GenericValue Dest;
309 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000310 IMPLEMENT_SIGNLESS_BINOP(%, Int8, int8_t);
311 IMPLEMENT_SIGNLESS_BINOP(%, Int16, int16_t);
312 IMPLEMENT_SIGNLESS_BINOP(%, Int32, int32_t);
313 IMPLEMENT_SIGNLESS_BINOP(%, Int64, int64_t);
Reid Spencer0a783f72006-11-02 01:53:59 +0000314 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000315 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Reid Spencer0a783f72006-11-02 01:53:59 +0000316 abort();
317 }
318 return Dest;
319}
320
321static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
322 const Type *Ty) {
323 GenericValue Dest;
324 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000325 case Type::FloatTyID:
326 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
327 break;
328 case Type::DoubleTyID:
329 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
330 break;
331 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000332 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000333 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000334 }
335 return Dest;
336}
337
Misha Brukmand1c881a2005-04-21 22:43:08 +0000338static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
339 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000340 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000341 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000342 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Reid Spencere49661b2006-12-31 05:51:36 +0000343 IMPLEMENT_BINARY_OPERATOR(&, Int8);
344 IMPLEMENT_BINARY_OPERATOR(&, Int16);
345 IMPLEMENT_BINARY_OPERATOR(&, Int32);
346 IMPLEMENT_BINARY_OPERATOR(&, Int64);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000347 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000348 cerr << "Unhandled type for And instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000349 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000350 }
351 return Dest;
352}
353
Misha Brukmand1c881a2005-04-21 22:43:08 +0000354static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000355 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000356 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000357 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000358 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Reid Spencere49661b2006-12-31 05:51:36 +0000359 IMPLEMENT_BINARY_OPERATOR(|, Int8);
360 IMPLEMENT_BINARY_OPERATOR(|, Int16);
361 IMPLEMENT_BINARY_OPERATOR(|, Int32);
362 IMPLEMENT_BINARY_OPERATOR(|, Int64);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000363 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000364 cerr << "Unhandled type for Or instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000365 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000366 }
367 return Dest;
368}
369
Misha Brukmand1c881a2005-04-21 22:43:08 +0000370static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000371 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000372 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000373 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000374 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Reid Spencere49661b2006-12-31 05:51:36 +0000375 IMPLEMENT_BINARY_OPERATOR(^, Int8);
376 IMPLEMENT_BINARY_OPERATOR(^, Int16);
377 IMPLEMENT_BINARY_OPERATOR(^, Int32);
378 IMPLEMENT_BINARY_OPERATOR(^, Int64);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000379 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000380 cerr << "Unhandled type for Xor instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000381 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000382 }
383 return Dest;
384}
385
Reid Spencere49661b2006-12-31 05:51:36 +0000386#define IMPLEMENT_ICMP(OP, TY, CAST) \
387 case Type::TY##TyID: Dest.BoolVal = \
388 ((CAST)Src1.TY##Val) OP ((CAST)Src2.TY##Val); break
Chris Lattner92101ac2001-08-23 17:05:04 +0000389
Chris Lattnerfd506f52003-04-23 19:55:35 +0000390// Handle pointers specially because they must be compared with only as much
391// width as the host has. We _do not_ want to be comparing 64 bit values when
392// running on a 32-bit target, otherwise the upper 32 bits might mess up
393// comparisons if they contain garbage.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000394#define IMPLEMENT_POINTERCMP(OP) \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000395 case Type::PointerTyID: \
396 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
397 (void*)(intptr_t)Src2.PointerVal; break
398
Reid Spencere4d87aa2006-12-23 06:05:41 +0000399static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
400 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000401 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000402 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000403 IMPLEMENT_ICMP(==, Int8, uint8_t);
404 IMPLEMENT_ICMP(==, Int16, uint16_t);
405 IMPLEMENT_ICMP(==, Int32, uint32_t);
406 IMPLEMENT_ICMP(==, Int64, uint64_t);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000407 IMPLEMENT_POINTERCMP(==);
408 default:
409 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
410 abort();
411 }
412 return Dest;
413}
414
415static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
416 const Type *Ty) {
417 GenericValue Dest;
418 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000419 IMPLEMENT_ICMP(!=, Int8, uint8_t);
420 IMPLEMENT_ICMP(!=, Int16, uint16_t);
421 IMPLEMENT_ICMP(!=, Int32, uint32_t);
422 IMPLEMENT_ICMP(!=, Int64, uint64_t);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000423 IMPLEMENT_POINTERCMP(!=);
424 default:
425 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
426 abort();
427 }
428 return Dest;
429}
430
431static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
432 const Type *Ty) {
433 GenericValue Dest;
434 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000435 IMPLEMENT_ICMP(<, Int8, uint8_t);
436 IMPLEMENT_ICMP(<, Int16, uint16_t);
437 IMPLEMENT_ICMP(<, Int32, uint32_t);
438 IMPLEMENT_ICMP(<, Int64, uint64_t);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000439 IMPLEMENT_POINTERCMP(<);
440 default:
441 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
442 abort();
443 }
444 return Dest;
445}
446
447static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
448 const Type *Ty) {
449 GenericValue Dest;
450 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000451 IMPLEMENT_ICMP(<, Int8, int8_t);
452 IMPLEMENT_ICMP(<, Int16, int16_t);
453 IMPLEMENT_ICMP(<, Int32, int32_t);
454 IMPLEMENT_ICMP(<, Int64, int64_t);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000455 IMPLEMENT_POINTERCMP(<);
456 default:
457 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
458 abort();
459 }
460 return Dest;
461}
462
463static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
464 const Type *Ty) {
465 GenericValue Dest;
466 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000467 IMPLEMENT_ICMP(>, Int8, uint8_t);
468 IMPLEMENT_ICMP(>, Int16, uint16_t);
469 IMPLEMENT_ICMP(>, Int32, uint32_t);
470 IMPLEMENT_ICMP(>, Int64, uint64_t);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000471 IMPLEMENT_POINTERCMP(>);
472 default:
473 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
474 abort();
475 }
476 return Dest;
477}
478
479static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
480 const Type *Ty) {
481 GenericValue Dest;
482 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000483 IMPLEMENT_ICMP(>, Int8, int8_t);
484 IMPLEMENT_ICMP(>, Int16, int16_t);
485 IMPLEMENT_ICMP(>, Int32, int32_t);
486 IMPLEMENT_ICMP(>, Int64, int64_t);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000487 IMPLEMENT_POINTERCMP(>);
488 default:
489 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
490 abort();
491 }
492 return Dest;
493}
494
495static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
496 const Type *Ty) {
497 GenericValue Dest;
498 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000499 IMPLEMENT_ICMP(<=, Int8, uint8_t);
500 IMPLEMENT_ICMP(<=, Int16, uint16_t);
501 IMPLEMENT_ICMP(<=, Int32, uint32_t);
502 IMPLEMENT_ICMP(<=, Int64, uint64_t);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000503 IMPLEMENT_POINTERCMP(<=);
504 default:
505 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
506 abort();
507 }
508 return Dest;
509}
510
511static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
512 const Type *Ty) {
513 GenericValue Dest;
514 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000515 IMPLEMENT_ICMP(<=, Int8, int8_t);
516 IMPLEMENT_ICMP(<=, Int16, int16_t);
517 IMPLEMENT_ICMP(<=, Int32, int32_t);
518 IMPLEMENT_ICMP(<=, Int64, int64_t);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000519 IMPLEMENT_POINTERCMP(<=);
520 default:
521 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
522 abort();
523 }
524 return Dest;
525}
526
527static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
528 const Type *Ty) {
529 GenericValue Dest;
530 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000531 IMPLEMENT_ICMP(>=, Int8, uint8_t);
532 IMPLEMENT_ICMP(>=, Int16, uint16_t);
533 IMPLEMENT_ICMP(>=, Int32, uint32_t);
534 IMPLEMENT_ICMP(>=, Int64, uint64_t);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000535 IMPLEMENT_POINTERCMP(>=);
536 default:
537 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
538 abort();
539 }
540 return Dest;
541}
542
543static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
544 const Type *Ty) {
545 GenericValue Dest;
546 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000547 IMPLEMENT_ICMP(>=, Int8, int8_t);
548 IMPLEMENT_ICMP(>=, Int16, int16_t);
549 IMPLEMENT_ICMP(>=, Int32, int32_t);
550 IMPLEMENT_ICMP(>=, Int64, int64_t);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000551 IMPLEMENT_POINTERCMP(>=);
552 default:
553 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
554 abort();
555 }
556 return Dest;
557}
558
Reid Spencere49661b2006-12-31 05:51:36 +0000559void Interpreter::visitICmpInst(ICmpInst &I) {
560 ExecutionContext &SF = ECStack.back();
561 const Type *Ty = I.getOperand(0)->getType();
562 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
563 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
564 GenericValue R; // Result
565
566 switch (I.getPredicate()) {
567 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
568 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
569 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
570 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
571 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
572 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
573 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
574 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
575 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
576 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
577 default:
578 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
579 abort();
580 }
581
582 SetValue(&I, R, SF);
583}
584
585#define IMPLEMENT_FCMP(OP, TY) \
Reid Spencere4d87aa2006-12-23 06:05:41 +0000586 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
587
588static GenericValue executeFCMP_EQ(GenericValue Src1, GenericValue Src2,
589 const Type *Ty) {
590 GenericValue Dest;
591 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000592 IMPLEMENT_FCMP(==, Float);
593 IMPLEMENT_FCMP(==, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000594 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000595 cerr << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000596 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000597 }
598 return Dest;
599}
600
Reid Spencere4d87aa2006-12-23 06:05:41 +0000601static GenericValue executeFCMP_NE(GenericValue Src1, GenericValue Src2,
602 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000603 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000604 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000605 IMPLEMENT_FCMP(!=, Float);
606 IMPLEMENT_FCMP(!=, Double);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000607
Chris Lattner92101ac2001-08-23 17:05:04 +0000608 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000609 cerr << "Unhandled type for SetNE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000610 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000611 }
612 return Dest;
613}
614
Reid Spencere4d87aa2006-12-23 06:05:41 +0000615static GenericValue executeFCMP_LE(GenericValue Src1, GenericValue Src2,
616 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000617 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000618 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000619 IMPLEMENT_FCMP(<=, Float);
620 IMPLEMENT_FCMP(<=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000621 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000622 cerr << "Unhandled type for SetLE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000623 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000624 }
625 return Dest;
626}
627
Reid Spencere4d87aa2006-12-23 06:05:41 +0000628static GenericValue executeFCMP_GE(GenericValue Src1, GenericValue Src2,
629 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000630 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000631 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000632 IMPLEMENT_FCMP(>=, Float);
633 IMPLEMENT_FCMP(>=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000634 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000635 cerr << "Unhandled type for SetGE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000636 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000637 }
638 return Dest;
639}
640
Reid Spencere4d87aa2006-12-23 06:05:41 +0000641static GenericValue executeFCMP_LT(GenericValue Src1, GenericValue Src2,
642 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000643 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000644 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000645 IMPLEMENT_FCMP(<, Float);
646 IMPLEMENT_FCMP(<, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000647 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000648 cerr << "Unhandled type for SetLT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000649 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000650 }
651 return Dest;
652}
653
Reid Spencere4d87aa2006-12-23 06:05:41 +0000654static GenericValue executeFCMP_GT(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000655 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000656 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000657 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000658 IMPLEMENT_FCMP(>, Float);
659 IMPLEMENT_FCMP(>, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000660 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000661 cerr << "Unhandled type for SetGT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000662 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000663 }
664 return Dest;
665}
666
Reid Spencere4d87aa2006-12-23 06:05:41 +0000667void Interpreter::visitFCmpInst(FCmpInst &I) {
668 ExecutionContext &SF = ECStack.back();
669 const Type *Ty = I.getOperand(0)->getType();
670 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
671 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
672 GenericValue R; // Result
673
674 switch (I.getPredicate()) {
675 case FCmpInst::FCMP_FALSE: R.BoolVal = false;
676 case FCmpInst::FCMP_ORD: R = executeFCMP_EQ(Src1, Src2, Ty); break; ///???
677 case FCmpInst::FCMP_UNO: R = executeFCMP_NE(Src1, Src2, Ty); break; ///???
678 case FCmpInst::FCMP_OEQ:
679 case FCmpInst::FCMP_UEQ: R = executeFCMP_EQ(Src1, Src2, Ty); break;
680 case FCmpInst::FCMP_ONE:
681 case FCmpInst::FCMP_UNE: R = executeFCMP_NE(Src1, Src2, Ty); break;
682 case FCmpInst::FCMP_OLT:
683 case FCmpInst::FCMP_ULT: R = executeFCMP_LT(Src1, Src2, Ty); break;
684 case FCmpInst::FCMP_OGT:
685 case FCmpInst::FCMP_UGT: R = executeFCMP_GT(Src1, Src2, Ty); break;
686 case FCmpInst::FCMP_OLE:
687 case FCmpInst::FCMP_ULE: R = executeFCMP_LE(Src1, Src2, Ty); break;
688 case FCmpInst::FCMP_OGE:
689 case FCmpInst::FCMP_UGE: R = executeFCMP_GE(Src1, Src2, Ty); break;
690 case FCmpInst::FCMP_TRUE: R.BoolVal = true;
691 default:
692 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
693 abort();
694 }
695
696 SetValue(&I, R, SF);
697}
698
Reid Spencere4d87aa2006-12-23 06:05:41 +0000699static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
700 GenericValue Src2, const Type *Ty) {
701 GenericValue Result;
702 switch (predicate) {
703 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
704 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
705 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
706 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
707 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
708 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
709 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
710 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
711 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
712 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
713 case FCmpInst::FCMP_ORD: return executeFCMP_EQ(Src1, Src2, Ty); break;
714 case FCmpInst::FCMP_UNO: return executeFCMP_NE(Src1, Src2, Ty); break;
715 case FCmpInst::FCMP_OEQ:
716 case FCmpInst::FCMP_UEQ: return executeFCMP_EQ(Src1, Src2, Ty); break;
717 case FCmpInst::FCMP_ONE:
718 case FCmpInst::FCMP_UNE: return executeFCMP_NE(Src1, Src2, Ty); break;
719 case FCmpInst::FCMP_OLT:
720 case FCmpInst::FCMP_ULT: return executeFCMP_LT(Src1, Src2, Ty); break;
721 case FCmpInst::FCMP_OGT:
722 case FCmpInst::FCMP_UGT: return executeFCMP_GT(Src1, Src2, Ty); break;
723 case FCmpInst::FCMP_OLE:
724 case FCmpInst::FCMP_ULE: return executeFCMP_LE(Src1, Src2, Ty); break;
725 case FCmpInst::FCMP_OGE:
726 case FCmpInst::FCMP_UGE: return executeFCMP_GE(Src1, Src2, Ty); break;
727 case FCmpInst::FCMP_FALSE: {
728 GenericValue Result;
729 Result.BoolVal = false;
730 return Result;
731 }
732 case FCmpInst::FCMP_TRUE: {
733 GenericValue Result;
734 Result.BoolVal = true;
735 return Result;
736 }
737 default:
738 cerr << "Unhandled Cmp predicate\n";
739 abort();
740 }
741}
742
Chris Lattnerd7916e92003-05-10 21:22:39 +0000743void Interpreter::visitBinaryOperator(BinaryOperator &I) {
744 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000745 const Type *Ty = I.getOperand(0)->getType();
746 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
747 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000748 GenericValue R; // Result
749
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000750 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000751 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
752 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
753 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000754 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
755 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
756 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
Reid Spencer0a783f72006-11-02 01:53:59 +0000757 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
758 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
759 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000760 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
761 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
762 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000763 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000764 cerr << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner02868352003-04-22 21:22:33 +0000765 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000766 }
767
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000768 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000769}
770
Misha Brukmand1c881a2005-04-21 22:43:08 +0000771static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000772 GenericValue Src3) {
773 return Src1.BoolVal ? Src2 : Src3;
774}
775
776void Interpreter::visitSelectInst(SelectInst &I) {
777 ExecutionContext &SF = ECStack.back();
778 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
779 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
780 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
781 GenericValue R = executeSelectInst(Src1, Src2, Src3);
782 SetValue(&I, R, SF);
783}
784
785
Chris Lattner92101ac2001-08-23 17:05:04 +0000786//===----------------------------------------------------------------------===//
787// Terminator Instruction Implementations
788//===----------------------------------------------------------------------===//
789
Chris Lattnere43db882001-10-27 04:15:57 +0000790void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000791 // runAtExitHandlers() assumes there are no stack frames, but
792 // if exit() was called, then it had a stack frame. Blow away
793 // the stack before interpreting atexit handlers.
794 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000795 runAtExitHandlers ();
Reid Spencere49661b2006-12-31 05:51:36 +0000796 exit (GV.Int32Val);
Chris Lattnere43db882001-10-27 04:15:57 +0000797}
798
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000799/// Pop the last stack frame off of ECStack and then copy the result
800/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000801/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000802/// CallInst if there was a previous stack frame. This method may
803/// invalidate any ECStack iterators you have. This method also takes
804/// care of switching to the normal destination BB, if we are returning
805/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000806///
807void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
808 GenericValue Result) {
809 // Pop the current stack frame.
810 ECStack.pop_back();
811
Misha Brukmand1c881a2005-04-21 22:43:08 +0000812 if (ECStack.empty()) { // Finished main. Put result into exit code...
813 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000814 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000815 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000816 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000817 }
818 } else {
819 // If we have a previous stack frame, and we have a previous call,
820 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000821 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000822 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000823 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000824 SetValue(I, Result, CallingSF);
825 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
826 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000827 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000828 }
829 }
830}
831
Chris Lattnerd7916e92003-05-10 21:22:39 +0000832void Interpreter::visitReturnInst(ReturnInst &I) {
833 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000834 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000835 GenericValue Result;
836
837 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000838 if (I.getNumOperands()) {
839 RetTy = I.getReturnValue()->getType();
840 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000841 }
842
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000843 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000844}
845
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000846void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000847 // Unwind stack
848 Instruction *Inst;
849 do {
850 ECStack.pop_back ();
851 if (ECStack.empty ())
852 abort ();
853 Inst = ECStack.back ().Caller.getInstruction ();
854 } while (!(Inst && isa<InvokeInst> (Inst)));
855
856 // Return from invoke
857 ExecutionContext &InvokingSF = ECStack.back ();
858 InvokingSF.Caller = CallSite ();
859
860 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000861 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000862}
863
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000864void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000865 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000866 abort();
867}
868
Chris Lattnerd7916e92003-05-10 21:22:39 +0000869void Interpreter::visitBranchInst(BranchInst &I) {
870 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000871 BasicBlock *Dest;
872
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000873 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
874 if (!I.isUnconditional()) {
875 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000876 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000877 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000878 }
Chris Lattner77113b62003-05-10 20:21:16 +0000879 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000880}
881
Chris Lattnerd7916e92003-05-10 21:22:39 +0000882void Interpreter::visitSwitchInst(SwitchInst &I) {
883 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000884 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
885 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000886
887 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000888 BasicBlock *Dest = 0;
889 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Reid Spencere4d87aa2006-12-23 06:05:41 +0000890 if (executeICMP_EQ(CondVal,
891 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000892 Dest = cast<BasicBlock>(I.getOperand(i+1));
893 break;
894 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000895
Chris Lattner09e93922003-04-22 20:34:47 +0000896 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000897 SwitchToNewBasicBlock(Dest, SF);
898}
899
900// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
901// This function handles the actual updating of block and instruction iterators
902// as well as execution of all of the PHI nodes in the destination block.
903//
904// This method does this because all of the PHI nodes must be executed
905// atomically, reading their inputs before any of the results are updated. Not
906// doing this can cause problems if the PHI nodes depend on other PHI nodes for
907// their inputs. If the input PHI node is updated before it is read, incorrect
908// results can happen. Thus we use a two phase approach.
909//
910void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
911 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
912 SF.CurBB = Dest; // Update CurBB to branch destination
913 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
914
915 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
916
917 // Loop over all of the PHI nodes in the current block, reading their inputs.
918 std::vector<GenericValue> ResultValues;
919
920 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
921 // Search for the value corresponding to this previous bb...
922 int i = PN->getBasicBlockIndex(PrevBB);
923 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
924 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000925
Chris Lattner77113b62003-05-10 20:21:16 +0000926 // Save the incoming value for this PHI node...
927 ResultValues.push_back(getOperandValue(IncomingValue, SF));
928 }
929
930 // Now loop over all of the PHI nodes setting their values...
931 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000932 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
933 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000934 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000935 }
Chris Lattner09e93922003-04-22 20:34:47 +0000936}
937
Chris Lattner92101ac2001-08-23 17:05:04 +0000938//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000939// Memory Instruction Implementations
940//===----------------------------------------------------------------------===//
941
Chris Lattnerd7916e92003-05-10 21:22:39 +0000942void Interpreter::visitAllocationInst(AllocationInst &I) {
943 ExecutionContext &SF = ECStack.back();
944
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000945 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000946
Chris Lattnercc82cc12002-04-28 21:57:33 +0000947 // Get the number of elements being allocated by the array...
Reid Spencere49661b2006-12-31 05:51:36 +0000948 unsigned NumElements = getOperandValue(I.getOperand(0), SF).Int32Val;
Chris Lattner86660982001-08-27 05:16:50 +0000949
950 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +0000951 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000952
Chris Lattnerfe11a972002-12-23 23:59:41 +0000953 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000954 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000955 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000956
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000957 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000958 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000959}
960
Chris Lattnerd7916e92003-05-10 21:22:39 +0000961void Interpreter::visitFreeInst(FreeInst &I) {
962 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000963 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
964 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000965 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000966 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000967}
968
Chris Lattnera34c5682002-08-27 22:33:45 +0000969// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000970//
Chris Lattner4af6de82003-11-25 20:44:56 +0000971GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000972 gep_type_iterator E,
973 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000974 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000975 "Cannot getElementOffset of a nonpointer type!");
976
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000977 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000978
979 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000980 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000981 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000982
Reid Spencerb83eb642006-10-20 07:07:24 +0000983 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
984 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000985
Chris Lattnerd5644962005-01-08 20:05:34 +0000986 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000987 } else {
988 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000989 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000990 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
991
992 uint64_t Idx;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000993 switch (I.getOperand()->getType()->getTypeID()) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000994 default: assert(0 && "Illegal getelementptr index for sequential type!");
Reid Spencere49661b2006-12-31 05:51:36 +0000995 case Type::Int8TyID: Idx = IdxGV.Int8Val; break;
996 case Type::Int16TyID: Idx = IdxGV.Int16Val; break;
997 case Type::Int32TyID: Idx = IdxGV.Int32Val; break;
998 case Type::Int64TyID: Idx = IdxGV.Int64Val; break;
Chris Lattner4af6de82003-11-25 20:44:56 +0000999 }
Chris Lattnerd5644962005-01-08 20:05:34 +00001000 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001001 }
Chris Lattner95c3af52001-10-29 19:32:19 +00001002 }
1003
Chris Lattnera34c5682002-08-27 22:33:45 +00001004 GenericValue Result;
1005 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
1006 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +00001007}
1008
Chris Lattnerd7916e92003-05-10 21:22:39 +00001009void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1010 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +00001011 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +00001012 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +00001013}
1014
Chris Lattnerd7916e92003-05-10 21:22:39 +00001015void Interpreter::visitLoadInst(LoadInst &I) {
1016 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001017 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +00001018 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +00001019 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001020 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001021}
1022
Chris Lattnerd7916e92003-05-10 21:22:39 +00001023void Interpreter::visitStoreInst(StoreInst &I) {
1024 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +00001025 GenericValue Val = getOperandValue(I.getOperand(0), SF);
1026 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +00001027 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +00001028 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +00001029}
1030
Chris Lattner86660982001-08-27 05:16:50 +00001031//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +00001032// Miscellaneous Instruction Implementations
1033//===----------------------------------------------------------------------===//
1034
Brian Gaekefea483d2003-11-07 20:04:22 +00001035void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001036 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +00001037
1038 // Check to see if this is an intrinsic function call...
1039 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +00001040 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +00001041 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +00001042 case Intrinsic::not_intrinsic:
1043 break;
Chris Lattner317201d2004-03-13 00:24:00 +00001044 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +00001045 GenericValue ArgIndex;
1046 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1047 ArgIndex.UIntPairVal.second = 0;
1048 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +00001049 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +00001050 }
Chris Lattner317201d2004-03-13 00:24:00 +00001051 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +00001052 return;
Chris Lattner317201d2004-03-13 00:24:00 +00001053 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +00001054 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1055 return;
1056 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +00001057 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +00001058 // class to transform it into hopefully tasty LLVM code.
1059 //
1060 Instruction *Prev = CS.getInstruction()->getPrev();
1061 BasicBlock *Parent = CS.getInstruction()->getParent();
1062 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1063
1064 // Restore the CurInst pointer to the first instruction newly inserted, if
1065 // any.
1066 if (!Prev) {
1067 SF.CurInst = Parent->begin();
1068 } else {
1069 SF.CurInst = Prev;
1070 ++SF.CurInst;
1071 }
Brian Gaekeb440dea2004-04-23 18:05:28 +00001072 return;
Chris Lattner73011782003-12-28 09:44:37 +00001073 }
1074
Brian Gaekefea483d2003-11-07 20:04:22 +00001075 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +00001076 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +00001077 const unsigned NumArgs = SF.Caller.arg_size();
1078 ArgVals.reserve(NumArgs);
1079 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1080 e = SF.Caller.arg_end(); i != e; ++i) {
1081 Value *V = *i;
1082 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +00001083 // Promote all integral types whose size is < sizeof(int) into ints. We do
1084 // this by zero or sign extending the value as appropriate according to the
1085 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +00001086 const Type *Ty = V->getType();
1087 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Reid Spencere49661b2006-12-31 05:51:36 +00001088 if (Ty == Type::Int16Ty)
1089 ArgVals.back().Int32Val = ArgVals.back().Int16Val;
1090 else if (Ty == Type::Int8Ty)
1091 ArgVals.back().Int32Val = ArgVals.back().Int8Val;
Chris Lattner93780132003-01-13 00:58:52 +00001092 else if (Ty == Type::BoolTy)
Reid Spencere49661b2006-12-31 05:51:36 +00001093 ArgVals.back().Int32Val = ArgVals.back().BoolVal;
Chris Lattner93780132003-01-13 00:58:52 +00001094 else
Misha Brukmand1c881a2005-04-21 22:43:08 +00001095 assert(0 && "Unknown type!");
Chris Lattner93780132003-01-13 00:58:52 +00001096 }
1097 }
Chris Lattner365a76e2001-09-10 04:49:44 +00001098
Misha Brukmand1c881a2005-04-21 22:43:08 +00001099 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001100 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +00001101 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +00001102 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +00001103}
1104
Chris Lattner86660982001-08-27 05:16:50 +00001105#define IMPLEMENT_SHIFT(OP, TY) \
Reid Spencere49661b2006-12-31 05:51:36 +00001106 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.Int8Val; break
Chris Lattner86660982001-08-27 05:16:50 +00001107
Reid Spencere49661b2006-12-31 05:51:36 +00001108#define IMPLEMENT_SIGNLESS_SHIFT(OP, TY, CAST) \
1109 case Type::TY##TyID: Dest.TY##Val = ((CAST)Src1.TY##Val) OP Src2.Int8Val; \
1110 break
Reid Spencer3822ff52006-11-08 06:47:33 +00001111
Brian Gaeke63438cc2003-12-11 00:22:59 +00001112static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
1113 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +00001114 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001115 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +00001116 IMPLEMENT_SHIFT(<<, Int8);
1117 IMPLEMENT_SHIFT(<<, Int16);
1118 IMPLEMENT_SHIFT(<<, Int32);
1119 IMPLEMENT_SHIFT(<<, Int64);
Chris Lattner86660982001-08-27 05:16:50 +00001120 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001121 cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001122 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001123 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001124}
1125
Reid Spencer3822ff52006-11-08 06:47:33 +00001126static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
1127 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +00001128 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001129 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +00001130 IMPLEMENT_SIGNLESS_SHIFT(>>, Int8, uint8_t);
1131 IMPLEMENT_SIGNLESS_SHIFT(>>, Int16, uint16_t);
1132 IMPLEMENT_SIGNLESS_SHIFT(>>, Int32, uint32_t);
1133 IMPLEMENT_SIGNLESS_SHIFT(>>, Int64, uint64_t);
Chris Lattner86660982001-08-27 05:16:50 +00001134 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001135 cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
Reid Spencer3822ff52006-11-08 06:47:33 +00001136 abort();
1137 }
1138 return Dest;
1139}
1140
1141static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
1142 const Type *Ty) {
1143 GenericValue Dest;
1144 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +00001145 IMPLEMENT_SIGNLESS_SHIFT(>>, Int8, int8_t);
1146 IMPLEMENT_SIGNLESS_SHIFT(>>, Int16, int16_t);
1147 IMPLEMENT_SIGNLESS_SHIFT(>>, Int32, int32_t);
1148 IMPLEMENT_SIGNLESS_SHIFT(>>, Int64, int64_t);
Reid Spencer3822ff52006-11-08 06:47:33 +00001149 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001150 cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +00001151 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001152 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001153 return Dest;
1154}
1155
1156void Interpreter::visitShl(ShiftInst &I) {
1157 ExecutionContext &SF = ECStack.back();
1158 const Type *Ty = I.getOperand(0)->getType();
1159 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1160 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1161 GenericValue Dest;
1162 Dest = executeShlInst (Src1, Src2, Ty);
1163 SetValue(&I, Dest, SF);
1164}
1165
Reid Spencer3822ff52006-11-08 06:47:33 +00001166void Interpreter::visitLShr(ShiftInst &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001167 ExecutionContext &SF = ECStack.back();
1168 const Type *Ty = I.getOperand(0)->getType();
1169 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1170 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1171 GenericValue Dest;
Reid Spencer3822ff52006-11-08 06:47:33 +00001172 Dest = executeLShrInst (Src1, Src2, Ty);
1173 SetValue(&I, Dest, SF);
1174}
1175
1176void Interpreter::visitAShr(ShiftInst &I) {
1177 ExecutionContext &SF = ECStack.back();
1178 const Type *Ty = I.getOperand(0)->getType();
1179 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1180 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1181 GenericValue Dest;
1182 Dest = executeAShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001183 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001184}
1185
Reid Spencer3da59db2006-11-27 01:05:10 +00001186#define IMPLEMENT_CAST_START \
1187 switch (DstTy->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +00001188
Reid Spencere49661b2006-12-31 05:51:36 +00001189#define IMPLEMENT_CAST(STY, DTY, CAST) \
1190 case Type::STY##TyID: Dest.DTY##Val = (CAST(Src.STY##Val)); break;
Reid Spencer3da59db2006-11-27 01:05:10 +00001191
Reid Spencere49661b2006-12-31 05:51:36 +00001192#define IMPLEMENT_CAST_CASE(DTY, CAST) \
1193 case Type::DTY##TyID: \
Reid Spencer3da59db2006-11-27 01:05:10 +00001194 switch (SrcTy->getTypeID()) { \
Reid Spencere49661b2006-12-31 05:51:36 +00001195 IMPLEMENT_CAST(Bool, DTY, CAST); \
1196 IMPLEMENT_CAST(Int8, DTY, CAST); \
1197 IMPLEMENT_CAST(Int16, DTY, CAST); \
1198 IMPLEMENT_CAST(Int32, DTY, CAST); \
1199 IMPLEMENT_CAST(Int64, DTY, CAST); \
1200 IMPLEMENT_CAST(Pointer,DTY, CAST); \
1201 IMPLEMENT_CAST(Float, DTY, CAST); \
1202 IMPLEMENT_CAST(Double, DTY, CAST); \
Reid Spencer3da59db2006-11-27 01:05:10 +00001203 default: \
Bill Wendlinge8156192006-12-07 01:30:32 +00001204 cerr << "Unhandled cast: " \
Reid Spencer3da59db2006-11-27 01:05:10 +00001205 << *SrcTy << " to " << *DstTy << "\n"; \
Chris Lattner02868352003-04-22 21:22:33 +00001206 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +00001207 } \
1208 break
1209
Reid Spencer3da59db2006-11-27 01:05:10 +00001210#define IMPLEMENT_CAST_END \
Bill Wendlinge8156192006-12-07 01:30:32 +00001211 default: cerr \
Reid Spencer3da59db2006-11-27 01:05:10 +00001212 << "Unhandled dest type for cast instruction: " \
1213 << *DstTy << "\n"; \
1214 abort(); \
1215 }
Chris Lattner86660982001-08-27 05:16:50 +00001216
Reid Spencer3da59db2006-11-27 01:05:10 +00001217GenericValue Interpreter::executeCastOperation(Instruction::CastOps opcode,
1218 Value *SrcVal, const Type *DstTy,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001219 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001220 const Type *SrcTy = SrcVal->getType();
1221 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001222
Reid Spencer3da59db2006-11-27 01:05:10 +00001223 if (opcode == Instruction::Trunc && DstTy->getTypeID() == Type::BoolTyID) {
1224 // For truncations to bool, we must clear the high order bits of the source
1225 switch (SrcTy->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +00001226 case Type::BoolTyID: Src.BoolVal &= 1; break;
1227 case Type::Int8TyID: Src.Int8Val &= 1; break;
1228 case Type::Int16TyID: Src.Int16Val &= 1; break;
1229 case Type::Int32TyID: Src.Int32Val &= 1; break;
1230 case Type::Int64TyID: Src.Int64Val &= 1; break;
Reid Spencer3da59db2006-11-27 01:05:10 +00001231 default:
1232 assert(0 && "Can't trunc a non-integer!");
1233 break;
1234 }
1235 } else if (opcode == Instruction::SExt &&
1236 SrcTy->getTypeID() == Type::BoolTyID) {
1237 // For sign extension from bool, we must extend the source bits.
Reid Spencere49661b2006-12-31 05:51:36 +00001238 SrcTy = Type::Int64Ty;
1239 Src.Int64Val = 0 - Src.BoolVal;
Chris Lattner86660982001-08-27 05:16:50 +00001240 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001241
Reid Spencer3da59db2006-11-27 01:05:10 +00001242 switch (opcode) {
1243 case Instruction::Trunc: // src integer, dest integral (can't be long)
1244 IMPLEMENT_CAST_START
Reid Spencere49661b2006-12-31 05:51:36 +00001245 IMPLEMENT_CAST_CASE(Bool , (bool));
1246 IMPLEMENT_CAST_CASE(Int8 , (uint8_t));
1247 IMPLEMENT_CAST_CASE(Int16, (uint16_t));
1248 IMPLEMENT_CAST_CASE(Int32, (uint32_t));
1249 IMPLEMENT_CAST_CASE(Int64, (uint64_t));
Reid Spencer3da59db2006-11-27 01:05:10 +00001250 IMPLEMENT_CAST_END
1251 break;
1252 case Instruction::ZExt: // src integral (can't be long), dest integer
1253 IMPLEMENT_CAST_START
Reid Spencere49661b2006-12-31 05:51:36 +00001254 IMPLEMENT_CAST_CASE(Int8 , (uint8_t));
1255 IMPLEMENT_CAST_CASE(Int16, (uint16_t));
1256 IMPLEMENT_CAST_CASE(Int32, (uint32_t));
1257 IMPLEMENT_CAST_CASE(Int64, (uint64_t));
Reid Spencer3da59db2006-11-27 01:05:10 +00001258 IMPLEMENT_CAST_END
1259 break;
1260 case Instruction::SExt: // src integral (can't be long), dest integer
1261 IMPLEMENT_CAST_START
Reid Spencere49661b2006-12-31 05:51:36 +00001262 IMPLEMENT_CAST_CASE(Int8 , (uint8_t)(int8_t));
1263 IMPLEMENT_CAST_CASE(Int16, (uint16_t)(int16_t));
1264 IMPLEMENT_CAST_CASE(Int32, (uint32_t)(int32_t));
1265 IMPLEMENT_CAST_CASE(Int64, (uint64_t)(int64_t));
Reid Spencer3da59db2006-11-27 01:05:10 +00001266 IMPLEMENT_CAST_END
1267 break;
1268 case Instruction::FPTrunc: // src double, dest float
1269 IMPLEMENT_CAST_START
1270 IMPLEMENT_CAST_CASE(Float , (float));
1271 IMPLEMENT_CAST_END
1272 break;
1273 case Instruction::FPExt: // src float, dest double
1274 IMPLEMENT_CAST_START
1275 IMPLEMENT_CAST_CASE(Double , (double));
1276 IMPLEMENT_CAST_END
1277 break;
1278 case Instruction::UIToFP: // src integral, dest floating
1279 IMPLEMENT_CAST_START
1280 IMPLEMENT_CAST_CASE(Float , (float)(uint64_t));
1281 IMPLEMENT_CAST_CASE(Double , (double)(uint64_t));
1282 IMPLEMENT_CAST_END
1283 break;
1284 case Instruction::SIToFP: // src integeral, dest floating
1285 IMPLEMENT_CAST_START
1286 IMPLEMENT_CAST_CASE(Float , (float)(int64_t));
1287 IMPLEMENT_CAST_CASE(Double , (double)(int64_t));
1288 IMPLEMENT_CAST_END
1289 break;
1290 case Instruction::FPToUI: // src floating, dest integral
1291 IMPLEMENT_CAST_START
Reid Spencere49661b2006-12-31 05:51:36 +00001292 IMPLEMENT_CAST_CASE(Bool , (bool));
1293 IMPLEMENT_CAST_CASE(Int8 , (uint8_t));
1294 IMPLEMENT_CAST_CASE(Int16, (uint16_t));
1295 IMPLEMENT_CAST_CASE(Int32, (uint32_t ));
1296 IMPLEMENT_CAST_CASE(Int64, (uint64_t));
Reid Spencer3da59db2006-11-27 01:05:10 +00001297 IMPLEMENT_CAST_END
1298 break;
1299 case Instruction::FPToSI: // src floating, dest integral
1300 IMPLEMENT_CAST_START
Reid Spencere49661b2006-12-31 05:51:36 +00001301 IMPLEMENT_CAST_CASE(Bool , (bool));
1302 IMPLEMENT_CAST_CASE(Int8 , (uint8_t) (int8_t));
1303 IMPLEMENT_CAST_CASE(Int16, (uint16_t)(int16_t));
1304 IMPLEMENT_CAST_CASE(Int32, (uint32_t)(int32_t));
1305 IMPLEMENT_CAST_CASE(Int64, (uint64_t)(int64_t));
Reid Spencer3da59db2006-11-27 01:05:10 +00001306 IMPLEMENT_CAST_END
1307 break;
1308 case Instruction::PtrToInt: // src pointer, dest integral
1309 IMPLEMENT_CAST_START
Reid Spencere49661b2006-12-31 05:51:36 +00001310 IMPLEMENT_CAST_CASE(Bool , (bool));
1311 IMPLEMENT_CAST_CASE(Int8 , (uint8_t));
1312 IMPLEMENT_CAST_CASE(Int16, (uint16_t));
1313 IMPLEMENT_CAST_CASE(Int32, (uint32_t));
1314 IMPLEMENT_CAST_CASE(Int64, (uint64_t));
Reid Spencer3da59db2006-11-27 01:05:10 +00001315 IMPLEMENT_CAST_END
1316 break;
1317 case Instruction::IntToPtr: // src integral, dest pointer
1318 IMPLEMENT_CAST_START
1319 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1320 IMPLEMENT_CAST_END
1321 break;
1322 case Instruction::BitCast: // src any, dest any (same size)
1323 IMPLEMENT_CAST_START
1324 IMPLEMENT_CAST_CASE(Bool , (bool));
Reid Spencere49661b2006-12-31 05:51:36 +00001325 IMPLEMENT_CAST_CASE(Int8 , (uint8_t));
1326 IMPLEMENT_CAST_CASE(Int16 , (uint16_t));
1327 IMPLEMENT_CAST_CASE(Int32 , (uint32_t));
1328 IMPLEMENT_CAST_CASE(Int64 , (uint64_t));
Reid Spencer3da59db2006-11-27 01:05:10 +00001329 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1330 IMPLEMENT_CAST_CASE(Float , (float));
1331 IMPLEMENT_CAST_CASE(Double , (double));
1332 IMPLEMENT_CAST_END
1333 break;
1334 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001335 cerr << "Invalid cast opcode for cast instruction: " << opcode << "\n";
Reid Spencer3da59db2006-11-27 01:05:10 +00001336 abort();
1337 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001338 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001339}
Chris Lattner92101ac2001-08-23 17:05:04 +00001340
Chris Lattnerd7916e92003-05-10 21:22:39 +00001341void Interpreter::visitCastInst(CastInst &I) {
1342 ExecutionContext &SF = ECStack.back();
Reid Spencer3da59db2006-11-27 01:05:10 +00001343 SetValue(&I, executeCastOperation(I.getOpcode(), I.getOperand(0),
1344 I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001345}
Chris Lattner92101ac2001-08-23 17:05:04 +00001346
Brian Gaekec1a2be12003-11-07 21:20:47 +00001347#define IMPLEMENT_VAARG(TY) \
1348 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1349
1350void Interpreter::visitVAArgInst(VAArgInst &I) {
1351 ExecutionContext &SF = ECStack.back();
1352
Brian Gaeke9d20b712004-02-25 23:01:48 +00001353 // Get the incoming valist parameter. LLI treats the valist as a
1354 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001355 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001356 GenericValue Dest;
1357 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001358 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001359 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001360 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +00001361 IMPLEMENT_VAARG(Int8);
1362 IMPLEMENT_VAARG(Int16);
1363 IMPLEMENT_VAARG(Int32);
1364 IMPLEMENT_VAARG(Int64);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001365 IMPLEMENT_VAARG(Pointer);
1366 IMPLEMENT_VAARG(Float);
1367 IMPLEMENT_VAARG(Double);
1368 IMPLEMENT_VAARG(Bool);
1369 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001370 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Brian Gaekec1a2be12003-11-07 21:20:47 +00001371 abort();
1372 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001373
Brian Gaekec1a2be12003-11-07 21:20:47 +00001374 // Set the Value of this Instruction.
1375 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001376
1377 // Move the pointer to the next vararg.
1378 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001379}
1380
Chris Lattner92101ac2001-08-23 17:05:04 +00001381//===----------------------------------------------------------------------===//
1382// Dispatch and Execution Code
1383//===----------------------------------------------------------------------===//
1384
Chris Lattner92101ac2001-08-23 17:05:04 +00001385//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001386// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001387//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001388void Interpreter::callFunction(Function *F,
1389 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001390 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1391 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1392 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001393 // Make a new stack frame... and fill it in.
1394 ECStack.push_back(ExecutionContext());
1395 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001396 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001397
1398 // Special handling for external functions.
1399 if (F->isExternal()) {
1400 GenericValue Result = callExternalFunction (F, ArgVals);
1401 // Simulate a 'ret' instruction of the appropriate type.
1402 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1403 return;
1404 }
1405
1406 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001407 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001408 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001409
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001410 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001411 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001412 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001413 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001414
1415 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001416 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001417 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001418 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001419
1420 // Handle varargs arguments...
1421 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001422}
1423
Chris Lattner92101ac2001-08-23 17:05:04 +00001424void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001425 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001426 // Interpret a single instruction & increment the "PC".
1427 ExecutionContext &SF = ECStack.back(); // Current stack frame
1428 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001429
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001430 // Track the number of dynamic instructions executed.
1431 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001432
Bill Wendling480f0932006-11-27 23:54:50 +00001433 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001434 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001435 }
1436}