For PR950:
[oota-llvm.git] / lib / ExecutionEngine / Interpreter / Execution.cpp
1 //===-- Execution.cpp - Implement code to simulate the program ------------===//
2 //
3 //                     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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file contains the actual instruction interpreter.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "interpreter"
15 #include "Interpreter.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/CodeGen/IntrinsicLowering.h"
20 #include "llvm/Support/GetElementPtrTypeIterator.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Support/Debug.h"
23 #include <cmath>  // For fmod
24 using namespace llvm;
25
26 namespace {
27   Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
28
29   Interpreter *TheEE = 0;
30 }
31
32
33 //===----------------------------------------------------------------------===//
34 //                     Value Manipulation code
35 //===----------------------------------------------------------------------===//
36
37 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
38                                    const Type *Ty);
39 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
40                                    const Type *Ty);
41 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
42                                    const Type *Ty);
43 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
44                                    const Type *Ty);
45 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
46                                    const Type *Ty);
47 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
48                                    const Type *Ty);
49 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
50                                    const Type *Ty);
51 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
52                                    const Type *Ty);
53 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
54                                    const Type *Ty);
55 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
56                                    const Type *Ty);
57 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
58                                    const Type *Ty);
59 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
60                                    const Type *Ty);
61 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
62                                    const Type *Ty);
63 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
64                                    const Type *Ty);
65 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
66                                    const Type *Ty);
67 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
68                                    const Type *Ty);
69 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
70                                       GenericValue Src3);
71
72 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
73                                                 ExecutionContext &SF) {
74   switch (CE->getOpcode()) {
75   case Instruction::Cast:
76     return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
77   case Instruction::GetElementPtr:
78     return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
79                                gep_type_end(CE), SF);
80   case Instruction::Add:
81     return executeAddInst(getOperandValue(CE->getOperand(0), SF),
82                           getOperandValue(CE->getOperand(1), SF),
83                           CE->getOperand(0)->getType());
84   case Instruction::Sub:
85     return executeSubInst(getOperandValue(CE->getOperand(0), SF),
86                           getOperandValue(CE->getOperand(1), SF),
87                           CE->getOperand(0)->getType());
88   case Instruction::Mul:
89     return executeMulInst(getOperandValue(CE->getOperand(0), SF),
90                           getOperandValue(CE->getOperand(1), SF),
91                           CE->getOperand(0)->getType());
92   case Instruction::Div:
93     return executeDivInst(getOperandValue(CE->getOperand(0), SF),
94                           getOperandValue(CE->getOperand(1), SF),
95                           CE->getOperand(0)->getType());
96   case Instruction::Rem:
97     return executeRemInst(getOperandValue(CE->getOperand(0), SF),
98                           getOperandValue(CE->getOperand(1), SF),
99                           CE->getOperand(0)->getType());
100   case Instruction::And:
101     return executeAndInst(getOperandValue(CE->getOperand(0), SF),
102                           getOperandValue(CE->getOperand(1), SF),
103                           CE->getOperand(0)->getType());
104   case Instruction::Or:
105     return executeOrInst(getOperandValue(CE->getOperand(0), SF),
106                          getOperandValue(CE->getOperand(1), SF),
107                          CE->getOperand(0)->getType());
108   case Instruction::Xor:
109     return executeXorInst(getOperandValue(CE->getOperand(0), SF),
110                           getOperandValue(CE->getOperand(1), SF),
111                           CE->getOperand(0)->getType());
112   case Instruction::SetEQ:
113     return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
114                             getOperandValue(CE->getOperand(1), SF),
115                             CE->getOperand(0)->getType());
116   case Instruction::SetNE:
117     return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
118                             getOperandValue(CE->getOperand(1), SF),
119                             CE->getOperand(0)->getType());
120   case Instruction::SetLE:
121     return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
122                             getOperandValue(CE->getOperand(1), SF),
123                             CE->getOperand(0)->getType());
124   case Instruction::SetGE:
125     return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
126                             getOperandValue(CE->getOperand(1), SF),
127                             CE->getOperand(0)->getType());
128   case Instruction::SetLT:
129     return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
130                             getOperandValue(CE->getOperand(1), SF),
131                             CE->getOperand(0)->getType());
132   case Instruction::SetGT:
133     return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
134                             getOperandValue(CE->getOperand(1), SF),
135                             CE->getOperand(0)->getType());
136   case Instruction::Shl:
137     return executeShlInst(getOperandValue(CE->getOperand(0), SF),
138                           getOperandValue(CE->getOperand(1), SF),
139                           CE->getOperand(0)->getType());
140   case Instruction::Shr:
141     return executeShrInst(getOperandValue(CE->getOperand(0), SF),
142                           getOperandValue(CE->getOperand(1), SF),
143                           CE->getOperand(0)->getType());
144   case Instruction::Select:
145     return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
146                              getOperandValue(CE->getOperand(1), SF),
147                              getOperandValue(CE->getOperand(2), SF));
148   default:
149     std::cerr << "Unhandled ConstantExpr: " << *CE << "\n";
150     abort();
151     return GenericValue();
152   }
153 }
154
155 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
156   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
157     return getConstantExprValue(CE, SF);
158   } else if (Constant *CPV = dyn_cast<Constant>(V)) {
159     return getConstantValue(CPV);
160   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
161     return PTOGV(getPointerToGlobal(GV));
162   } else {
163     return SF.Values[V];
164   }
165 }
166
167 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
168   SF.Values[V] = Val;
169 }
170
171 void Interpreter::initializeExecutionEngine() {
172   TheEE = this;
173 }
174
175 //===----------------------------------------------------------------------===//
176 //                    Binary Instruction Implementations
177 //===----------------------------------------------------------------------===//
178
179 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
180    case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
181
182 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
183                                    const Type *Ty) {
184   GenericValue Dest;
185   switch (Ty->getTypeID()) {
186     IMPLEMENT_BINARY_OPERATOR(+, UByte);
187     IMPLEMENT_BINARY_OPERATOR(+, SByte);
188     IMPLEMENT_BINARY_OPERATOR(+, UShort);
189     IMPLEMENT_BINARY_OPERATOR(+, Short);
190     IMPLEMENT_BINARY_OPERATOR(+, UInt);
191     IMPLEMENT_BINARY_OPERATOR(+, Int);
192     IMPLEMENT_BINARY_OPERATOR(+, ULong);
193     IMPLEMENT_BINARY_OPERATOR(+, Long);
194     IMPLEMENT_BINARY_OPERATOR(+, Float);
195     IMPLEMENT_BINARY_OPERATOR(+, Double);
196   default:
197     std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
198     abort();
199   }
200   return Dest;
201 }
202
203 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
204                                    const Type *Ty) {
205   GenericValue Dest;
206   switch (Ty->getTypeID()) {
207     IMPLEMENT_BINARY_OPERATOR(-, UByte);
208     IMPLEMENT_BINARY_OPERATOR(-, SByte);
209     IMPLEMENT_BINARY_OPERATOR(-, UShort);
210     IMPLEMENT_BINARY_OPERATOR(-, Short);
211     IMPLEMENT_BINARY_OPERATOR(-, UInt);
212     IMPLEMENT_BINARY_OPERATOR(-, Int);
213     IMPLEMENT_BINARY_OPERATOR(-, ULong);
214     IMPLEMENT_BINARY_OPERATOR(-, Long);
215     IMPLEMENT_BINARY_OPERATOR(-, Float);
216     IMPLEMENT_BINARY_OPERATOR(-, Double);
217   default:
218     std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
219     abort();
220   }
221   return Dest;
222 }
223
224 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
225                                    const Type *Ty) {
226   GenericValue Dest;
227   switch (Ty->getTypeID()) {
228     IMPLEMENT_BINARY_OPERATOR(*, UByte);
229     IMPLEMENT_BINARY_OPERATOR(*, SByte);
230     IMPLEMENT_BINARY_OPERATOR(*, UShort);
231     IMPLEMENT_BINARY_OPERATOR(*, Short);
232     IMPLEMENT_BINARY_OPERATOR(*, UInt);
233     IMPLEMENT_BINARY_OPERATOR(*, Int);
234     IMPLEMENT_BINARY_OPERATOR(*, ULong);
235     IMPLEMENT_BINARY_OPERATOR(*, Long);
236     IMPLEMENT_BINARY_OPERATOR(*, Float);
237     IMPLEMENT_BINARY_OPERATOR(*, Double);
238   default:
239     std::cout << "Unhandled type for Mul instruction: " << *Ty << "\n";
240     abort();
241   }
242   return Dest;
243 }
244
245 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
246                                    const Type *Ty) {
247   GenericValue Dest;
248   switch (Ty->getTypeID()) {
249     IMPLEMENT_BINARY_OPERATOR(/, UByte);
250     IMPLEMENT_BINARY_OPERATOR(/, SByte);
251     IMPLEMENT_BINARY_OPERATOR(/, UShort);
252     IMPLEMENT_BINARY_OPERATOR(/, Short);
253     IMPLEMENT_BINARY_OPERATOR(/, UInt);
254     IMPLEMENT_BINARY_OPERATOR(/, Int);
255     IMPLEMENT_BINARY_OPERATOR(/, ULong);
256     IMPLEMENT_BINARY_OPERATOR(/, Long);
257     IMPLEMENT_BINARY_OPERATOR(/, Float);
258     IMPLEMENT_BINARY_OPERATOR(/, Double);
259   default:
260     std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
261     abort();
262   }
263   return Dest;
264 }
265
266 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
267                                    const Type *Ty) {
268   GenericValue Dest;
269   switch (Ty->getTypeID()) {
270     IMPLEMENT_BINARY_OPERATOR(%, UByte);
271     IMPLEMENT_BINARY_OPERATOR(%, SByte);
272     IMPLEMENT_BINARY_OPERATOR(%, UShort);
273     IMPLEMENT_BINARY_OPERATOR(%, Short);
274     IMPLEMENT_BINARY_OPERATOR(%, UInt);
275     IMPLEMENT_BINARY_OPERATOR(%, Int);
276     IMPLEMENT_BINARY_OPERATOR(%, ULong);
277     IMPLEMENT_BINARY_OPERATOR(%, Long);
278   case Type::FloatTyID:
279     Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
280     break;
281   case Type::DoubleTyID:
282     Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
283     break;
284   default:
285     std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
286     abort();
287   }
288   return Dest;
289 }
290
291 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
292                                    const Type *Ty) {
293   GenericValue Dest;
294   switch (Ty->getTypeID()) {
295     IMPLEMENT_BINARY_OPERATOR(&, Bool);
296     IMPLEMENT_BINARY_OPERATOR(&, UByte);
297     IMPLEMENT_BINARY_OPERATOR(&, SByte);
298     IMPLEMENT_BINARY_OPERATOR(&, UShort);
299     IMPLEMENT_BINARY_OPERATOR(&, Short);
300     IMPLEMENT_BINARY_OPERATOR(&, UInt);
301     IMPLEMENT_BINARY_OPERATOR(&, Int);
302     IMPLEMENT_BINARY_OPERATOR(&, ULong);
303     IMPLEMENT_BINARY_OPERATOR(&, Long);
304   default:
305     std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
306     abort();
307   }
308   return Dest;
309 }
310
311 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
312                                   const Type *Ty) {
313   GenericValue Dest;
314   switch (Ty->getTypeID()) {
315     IMPLEMENT_BINARY_OPERATOR(|, Bool);
316     IMPLEMENT_BINARY_OPERATOR(|, UByte);
317     IMPLEMENT_BINARY_OPERATOR(|, SByte);
318     IMPLEMENT_BINARY_OPERATOR(|, UShort);
319     IMPLEMENT_BINARY_OPERATOR(|, Short);
320     IMPLEMENT_BINARY_OPERATOR(|, UInt);
321     IMPLEMENT_BINARY_OPERATOR(|, Int);
322     IMPLEMENT_BINARY_OPERATOR(|, ULong);
323     IMPLEMENT_BINARY_OPERATOR(|, Long);
324   default:
325     std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
326     abort();
327   }
328   return Dest;
329 }
330
331 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
332                                    const Type *Ty) {
333   GenericValue Dest;
334   switch (Ty->getTypeID()) {
335     IMPLEMENT_BINARY_OPERATOR(^, Bool);
336     IMPLEMENT_BINARY_OPERATOR(^, UByte);
337     IMPLEMENT_BINARY_OPERATOR(^, SByte);
338     IMPLEMENT_BINARY_OPERATOR(^, UShort);
339     IMPLEMENT_BINARY_OPERATOR(^, Short);
340     IMPLEMENT_BINARY_OPERATOR(^, UInt);
341     IMPLEMENT_BINARY_OPERATOR(^, Int);
342     IMPLEMENT_BINARY_OPERATOR(^, ULong);
343     IMPLEMENT_BINARY_OPERATOR(^, Long);
344   default:
345     std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
346     abort();
347   }
348   return Dest;
349 }
350
351 #define IMPLEMENT_SETCC(OP, TY) \
352    case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
353
354 // Handle pointers specially because they must be compared with only as much
355 // width as the host has.  We _do not_ want to be comparing 64 bit values when
356 // running on a 32-bit target, otherwise the upper 32 bits might mess up
357 // comparisons if they contain garbage.
358 #define IMPLEMENT_POINTERSETCC(OP) \
359    case Type::PointerTyID: \
360         Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
361                        (void*)(intptr_t)Src2.PointerVal; break
362
363 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
364                                      const Type *Ty) {
365   GenericValue Dest;
366   switch (Ty->getTypeID()) {
367     IMPLEMENT_SETCC(==, UByte);
368     IMPLEMENT_SETCC(==, SByte);
369     IMPLEMENT_SETCC(==, UShort);
370     IMPLEMENT_SETCC(==, Short);
371     IMPLEMENT_SETCC(==, UInt);
372     IMPLEMENT_SETCC(==, Int);
373     IMPLEMENT_SETCC(==, ULong);
374     IMPLEMENT_SETCC(==, Long);
375     IMPLEMENT_SETCC(==, Float);
376     IMPLEMENT_SETCC(==, Double);
377     IMPLEMENT_POINTERSETCC(==);
378   default:
379     std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
380     abort();
381   }
382   return Dest;
383 }
384
385 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
386                                      const Type *Ty) {
387   GenericValue Dest;
388   switch (Ty->getTypeID()) {
389     IMPLEMENT_SETCC(!=, UByte);
390     IMPLEMENT_SETCC(!=, SByte);
391     IMPLEMENT_SETCC(!=, UShort);
392     IMPLEMENT_SETCC(!=, Short);
393     IMPLEMENT_SETCC(!=, UInt);
394     IMPLEMENT_SETCC(!=, Int);
395     IMPLEMENT_SETCC(!=, ULong);
396     IMPLEMENT_SETCC(!=, Long);
397     IMPLEMENT_SETCC(!=, Float);
398     IMPLEMENT_SETCC(!=, Double);
399     IMPLEMENT_POINTERSETCC(!=);
400
401   default:
402     std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
403     abort();
404   }
405   return Dest;
406 }
407
408 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
409                                      const Type *Ty) {
410   GenericValue Dest;
411   switch (Ty->getTypeID()) {
412     IMPLEMENT_SETCC(<=, UByte);
413     IMPLEMENT_SETCC(<=, SByte);
414     IMPLEMENT_SETCC(<=, UShort);
415     IMPLEMENT_SETCC(<=, Short);
416     IMPLEMENT_SETCC(<=, UInt);
417     IMPLEMENT_SETCC(<=, Int);
418     IMPLEMENT_SETCC(<=, ULong);
419     IMPLEMENT_SETCC(<=, Long);
420     IMPLEMENT_SETCC(<=, Float);
421     IMPLEMENT_SETCC(<=, Double);
422     IMPLEMENT_POINTERSETCC(<=);
423   default:
424     std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
425     abort();
426   }
427   return Dest;
428 }
429
430 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
431                                      const Type *Ty) {
432   GenericValue Dest;
433   switch (Ty->getTypeID()) {
434     IMPLEMENT_SETCC(>=, UByte);
435     IMPLEMENT_SETCC(>=, SByte);
436     IMPLEMENT_SETCC(>=, UShort);
437     IMPLEMENT_SETCC(>=, Short);
438     IMPLEMENT_SETCC(>=, UInt);
439     IMPLEMENT_SETCC(>=, Int);
440     IMPLEMENT_SETCC(>=, ULong);
441     IMPLEMENT_SETCC(>=, Long);
442     IMPLEMENT_SETCC(>=, Float);
443     IMPLEMENT_SETCC(>=, Double);
444     IMPLEMENT_POINTERSETCC(>=);
445   default:
446     std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
447     abort();
448   }
449   return Dest;
450 }
451
452 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
453                                      const Type *Ty) {
454   GenericValue Dest;
455   switch (Ty->getTypeID()) {
456     IMPLEMENT_SETCC(<, UByte);
457     IMPLEMENT_SETCC(<, SByte);
458     IMPLEMENT_SETCC(<, UShort);
459     IMPLEMENT_SETCC(<, Short);
460     IMPLEMENT_SETCC(<, UInt);
461     IMPLEMENT_SETCC(<, Int);
462     IMPLEMENT_SETCC(<, ULong);
463     IMPLEMENT_SETCC(<, Long);
464     IMPLEMENT_SETCC(<, Float);
465     IMPLEMENT_SETCC(<, Double);
466     IMPLEMENT_POINTERSETCC(<);
467   default:
468     std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
469     abort();
470   }
471   return Dest;
472 }
473
474 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
475                                      const Type *Ty) {
476   GenericValue Dest;
477   switch (Ty->getTypeID()) {
478     IMPLEMENT_SETCC(>, UByte);
479     IMPLEMENT_SETCC(>, SByte);
480     IMPLEMENT_SETCC(>, UShort);
481     IMPLEMENT_SETCC(>, Short);
482     IMPLEMENT_SETCC(>, UInt);
483     IMPLEMENT_SETCC(>, Int);
484     IMPLEMENT_SETCC(>, ULong);
485     IMPLEMENT_SETCC(>, Long);
486     IMPLEMENT_SETCC(>, Float);
487     IMPLEMENT_SETCC(>, Double);
488     IMPLEMENT_POINTERSETCC(>);
489   default:
490     std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
491     abort();
492   }
493   return Dest;
494 }
495
496 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
497   ExecutionContext &SF = ECStack.back();
498   const Type *Ty    = I.getOperand(0)->getType();
499   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
500   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
501   GenericValue R;   // Result
502
503   switch (I.getOpcode()) {
504   case Instruction::Add:   R = executeAddInst  (Src1, Src2, Ty); break;
505   case Instruction::Sub:   R = executeSubInst  (Src1, Src2, Ty); break;
506   case Instruction::Mul:   R = executeMulInst  (Src1, Src2, Ty); break;
507   case Instruction::Div:   R = executeDivInst  (Src1, Src2, Ty); break;
508   case Instruction::Rem:   R = executeRemInst  (Src1, Src2, Ty); break;
509   case Instruction::And:   R = executeAndInst  (Src1, Src2, Ty); break;
510   case Instruction::Or:    R = executeOrInst   (Src1, Src2, Ty); break;
511   case Instruction::Xor:   R = executeXorInst  (Src1, Src2, Ty); break;
512   case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
513   case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
514   case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
515   case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
516   case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
517   case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
518   default:
519     std::cout << "Don't know how to handle this binary operator!\n-->" << I;
520     abort();
521   }
522
523   SetValue(&I, R, SF);
524 }
525
526 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
527                                       GenericValue Src3) {
528   return Src1.BoolVal ? Src2 : Src3;
529 }
530
531 void Interpreter::visitSelectInst(SelectInst &I) {
532   ExecutionContext &SF = ECStack.back();
533   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
534   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
535   GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
536   GenericValue R = executeSelectInst(Src1, Src2, Src3);
537   SetValue(&I, R, SF);
538 }
539
540
541 //===----------------------------------------------------------------------===//
542 //                     Terminator Instruction Implementations
543 //===----------------------------------------------------------------------===//
544
545 void Interpreter::exitCalled(GenericValue GV) {
546   // runAtExitHandlers() assumes there are no stack frames, but
547   // if exit() was called, then it had a stack frame. Blow away
548   // the stack before interpreting atexit handlers.
549   ECStack.clear ();
550   runAtExitHandlers ();
551   exit (GV.IntVal);
552 }
553
554 /// Pop the last stack frame off of ECStack and then copy the result
555 /// back into the result variable if we are not returning void. The
556 /// result variable may be the ExitValue, or the Value of the calling
557 /// CallInst if there was a previous stack frame. This method may
558 /// invalidate any ECStack iterators you have. This method also takes
559 /// care of switching to the normal destination BB, if we are returning
560 /// from an invoke.
561 ///
562 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
563                                                   GenericValue Result) {
564   // Pop the current stack frame.
565   ECStack.pop_back();
566
567   if (ECStack.empty()) {  // Finished main.  Put result into exit code...
568     if (RetTy && RetTy->isIntegral()) {          // Nonvoid return type?
569       ExitValue = Result;   // Capture the exit value of the program
570     } else {
571       memset(&ExitValue, 0, sizeof(ExitValue));
572     }
573   } else {
574     // If we have a previous stack frame, and we have a previous call,
575     // fill in the return value...
576     ExecutionContext &CallingSF = ECStack.back();
577     if (Instruction *I = CallingSF.Caller.getInstruction()) {
578       if (CallingSF.Caller.getType() != Type::VoidTy)      // Save result...
579         SetValue(I, Result, CallingSF);
580       if (InvokeInst *II = dyn_cast<InvokeInst> (I))
581         SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
582       CallingSF.Caller = CallSite();          // We returned from the call...
583     }
584   }
585 }
586
587 void Interpreter::visitReturnInst(ReturnInst &I) {
588   ExecutionContext &SF = ECStack.back();
589   const Type *RetTy = Type::VoidTy;
590   GenericValue Result;
591
592   // Save away the return value... (if we are not 'ret void')
593   if (I.getNumOperands()) {
594     RetTy  = I.getReturnValue()->getType();
595     Result = getOperandValue(I.getReturnValue(), SF);
596   }
597
598   popStackAndReturnValueToCaller(RetTy, Result);
599 }
600
601 void Interpreter::visitUnwindInst(UnwindInst &I) {
602   // Unwind stack
603   Instruction *Inst;
604   do {
605     ECStack.pop_back ();
606     if (ECStack.empty ())
607       abort ();
608     Inst = ECStack.back ().Caller.getInstruction ();
609   } while (!(Inst && isa<InvokeInst> (Inst)));
610
611   // Return from invoke
612   ExecutionContext &InvokingSF = ECStack.back ();
613   InvokingSF.Caller = CallSite ();
614
615   // Go to exceptional destination BB of invoke instruction
616   SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
617 }
618
619 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
620   std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
621   abort();
622 }
623
624 void Interpreter::visitBranchInst(BranchInst &I) {
625   ExecutionContext &SF = ECStack.back();
626   BasicBlock *Dest;
627
628   Dest = I.getSuccessor(0);          // Uncond branches have a fixed dest...
629   if (!I.isUnconditional()) {
630     Value *Cond = I.getCondition();
631     if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
632       Dest = I.getSuccessor(1);
633   }
634   SwitchToNewBasicBlock(Dest, SF);
635 }
636
637 void Interpreter::visitSwitchInst(SwitchInst &I) {
638   ExecutionContext &SF = ECStack.back();
639   GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
640   const Type *ElTy = I.getOperand(0)->getType();
641
642   // Check to see if any of the cases match...
643   BasicBlock *Dest = 0;
644   for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
645     if (executeSetEQInst(CondVal,
646                          getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
647       Dest = cast<BasicBlock>(I.getOperand(i+1));
648       break;
649     }
650
651   if (!Dest) Dest = I.getDefaultDest();   // No cases matched: use default
652   SwitchToNewBasicBlock(Dest, SF);
653 }
654
655 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
656 // This function handles the actual updating of block and instruction iterators
657 // as well as execution of all of the PHI nodes in the destination block.
658 //
659 // This method does this because all of the PHI nodes must be executed
660 // atomically, reading their inputs before any of the results are updated.  Not
661 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
662 // their inputs.  If the input PHI node is updated before it is read, incorrect
663 // results can happen.  Thus we use a two phase approach.
664 //
665 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
666   BasicBlock *PrevBB = SF.CurBB;      // Remember where we came from...
667   SF.CurBB   = Dest;                  // Update CurBB to branch destination
668   SF.CurInst = SF.CurBB->begin();     // Update new instruction ptr...
669
670   if (!isa<PHINode>(SF.CurInst)) return;  // Nothing fancy to do
671
672   // Loop over all of the PHI nodes in the current block, reading their inputs.
673   std::vector<GenericValue> ResultValues;
674
675   for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
676     // Search for the value corresponding to this previous bb...
677     int i = PN->getBasicBlockIndex(PrevBB);
678     assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
679     Value *IncomingValue = PN->getIncomingValue(i);
680
681     // Save the incoming value for this PHI node...
682     ResultValues.push_back(getOperandValue(IncomingValue, SF));
683   }
684
685   // Now loop over all of the PHI nodes setting their values...
686   SF.CurInst = SF.CurBB->begin();
687   for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
688     PHINode *PN = cast<PHINode>(SF.CurInst);
689     SetValue(PN, ResultValues[i], SF);
690   }
691 }
692
693 //===----------------------------------------------------------------------===//
694 //                     Memory Instruction Implementations
695 //===----------------------------------------------------------------------===//
696
697 void Interpreter::visitAllocationInst(AllocationInst &I) {
698   ExecutionContext &SF = ECStack.back();
699
700   const Type *Ty = I.getType()->getElementType();  // Type to be allocated
701
702   // Get the number of elements being allocated by the array...
703   unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
704
705   // Allocate enough memory to hold the type...
706   void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
707
708   GenericValue Result = PTOGV(Memory);
709   assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
710   SetValue(&I, Result, SF);
711
712   if (I.getOpcode() == Instruction::Alloca)
713     ECStack.back().Allocas.add(Memory);
714 }
715
716 void Interpreter::visitFreeInst(FreeInst &I) {
717   ExecutionContext &SF = ECStack.back();
718   assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
719   GenericValue Value = getOperandValue(I.getOperand(0), SF);
720   // TODO: Check to make sure memory is allocated
721   free(GVTOP(Value));   // Free memory
722 }
723
724 // getElementOffset - The workhorse for getelementptr.
725 //
726 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
727                                               gep_type_iterator E,
728                                               ExecutionContext &SF) {
729   assert(isa<PointerType>(Ptr->getType()) &&
730          "Cannot getElementOffset of a nonpointer type!");
731
732   PointerTy Total = 0;
733
734   for (; I != E; ++I) {
735     if (const StructType *STy = dyn_cast<StructType>(*I)) {
736       const StructLayout *SLO = TD.getStructLayout(STy);
737
738       const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
739       unsigned Index = unsigned(CPU->getZExtValue());
740
741       Total += (PointerTy)SLO->MemberOffsets[Index];
742     } else {
743       const SequentialType *ST = cast<SequentialType>(*I);
744       // Get the index number for the array... which must be long type...
745       GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
746
747       uint64_t Idx;
748       switch (I.getOperand()->getType()->getTypeID()) {
749       default: assert(0 && "Illegal getelementptr index for sequential type!");
750       case Type::SByteTyID:  Idx = IdxGV.SByteVal; break;
751       case Type::ShortTyID:  Idx = IdxGV.ShortVal; break;
752       case Type::IntTyID:    Idx = IdxGV.IntVal; break;
753       case Type::LongTyID:   Idx = IdxGV.LongVal; break;
754       case Type::UByteTyID:  Idx = IdxGV.UByteVal; break;
755       case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
756       case Type::UIntTyID:   Idx = IdxGV.UIntVal; break;
757       case Type::ULongTyID:  Idx = IdxGV.ULongVal; break;
758       }
759       Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
760     }
761   }
762
763   GenericValue Result;
764   Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
765   return Result;
766 }
767
768 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
769   ExecutionContext &SF = ECStack.back();
770   SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
771                                    gep_type_begin(I), gep_type_end(I), SF), SF);
772 }
773
774 void Interpreter::visitLoadInst(LoadInst &I) {
775   ExecutionContext &SF = ECStack.back();
776   GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
777   GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
778   GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
779   SetValue(&I, Result, SF);
780 }
781
782 void Interpreter::visitStoreInst(StoreInst &I) {
783   ExecutionContext &SF = ECStack.back();
784   GenericValue Val = getOperandValue(I.getOperand(0), SF);
785   GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
786   StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
787                      I.getOperand(0)->getType());
788 }
789
790 //===----------------------------------------------------------------------===//
791 //                 Miscellaneous Instruction Implementations
792 //===----------------------------------------------------------------------===//
793
794 void Interpreter::visitCallSite(CallSite CS) {
795   ExecutionContext &SF = ECStack.back();
796
797   // Check to see if this is an intrinsic function call...
798   if (Function *F = CS.getCalledFunction())
799    if (F->isExternal ())
800     switch (F->getIntrinsicID()) {
801     case Intrinsic::not_intrinsic:
802       break;
803     case Intrinsic::vastart: { // va_start
804       GenericValue ArgIndex;
805       ArgIndex.UIntPairVal.first = ECStack.size() - 1;
806       ArgIndex.UIntPairVal.second = 0;
807       SetValue(CS.getInstruction(), ArgIndex, SF);
808       return;
809     }
810     case Intrinsic::vaend:    // va_end is a noop for the interpreter
811       return;
812     case Intrinsic::vacopy:   // va_copy: dest = src
813       SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
814       return;
815     default:
816       // If it is an unknown intrinsic function, use the intrinsic lowering
817       // class to transform it into hopefully tasty LLVM code.
818       //
819       Instruction *Prev = CS.getInstruction()->getPrev();
820       BasicBlock *Parent = CS.getInstruction()->getParent();
821       IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
822
823       // Restore the CurInst pointer to the first instruction newly inserted, if
824       // any.
825       if (!Prev) {
826         SF.CurInst = Parent->begin();
827       } else {
828         SF.CurInst = Prev;
829         ++SF.CurInst;
830       }
831       return;
832     }
833
834   SF.Caller = CS;
835   std::vector<GenericValue> ArgVals;
836   const unsigned NumArgs = SF.Caller.arg_size();
837   ArgVals.reserve(NumArgs);
838   for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
839          e = SF.Caller.arg_end(); i != e; ++i) {
840     Value *V = *i;
841     ArgVals.push_back(getOperandValue(V, SF));
842     // Promote all integral types whose size is < sizeof(int) into ints.  We do
843     // this by zero or sign extending the value as appropriate according to the
844     // source type.
845     const Type *Ty = V->getType();
846     if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
847       if (Ty == Type::ShortTy)
848         ArgVals.back().IntVal = ArgVals.back().ShortVal;
849       else if (Ty == Type::UShortTy)
850         ArgVals.back().UIntVal = ArgVals.back().UShortVal;
851       else if (Ty == Type::SByteTy)
852         ArgVals.back().IntVal = ArgVals.back().SByteVal;
853       else if (Ty == Type::UByteTy)
854         ArgVals.back().UIntVal = ArgVals.back().UByteVal;
855       else if (Ty == Type::BoolTy)
856         ArgVals.back().UIntVal = ArgVals.back().BoolVal;
857       else
858         assert(0 && "Unknown type!");
859     }
860   }
861
862   // To handle indirect calls, we must get the pointer value from the argument
863   // and treat it as a function pointer.
864   GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
865   callFunction((Function*)GVTOP(SRC), ArgVals);
866 }
867
868 #define IMPLEMENT_SHIFT(OP, TY) \
869    case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
870
871 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
872                                    const Type *Ty) {
873   GenericValue Dest;
874   switch (Ty->getTypeID()) {
875     IMPLEMENT_SHIFT(<<, UByte);
876     IMPLEMENT_SHIFT(<<, SByte);
877     IMPLEMENT_SHIFT(<<, UShort);
878     IMPLEMENT_SHIFT(<<, Short);
879     IMPLEMENT_SHIFT(<<, UInt);
880     IMPLEMENT_SHIFT(<<, Int);
881     IMPLEMENT_SHIFT(<<, ULong);
882     IMPLEMENT_SHIFT(<<, Long);
883   default:
884     std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
885   }
886   return Dest;
887 }
888
889 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
890                                    const Type *Ty) {
891   GenericValue Dest;
892   switch (Ty->getTypeID()) {
893     IMPLEMENT_SHIFT(>>, UByte);
894     IMPLEMENT_SHIFT(>>, SByte);
895     IMPLEMENT_SHIFT(>>, UShort);
896     IMPLEMENT_SHIFT(>>, Short);
897     IMPLEMENT_SHIFT(>>, UInt);
898     IMPLEMENT_SHIFT(>>, Int);
899     IMPLEMENT_SHIFT(>>, ULong);
900     IMPLEMENT_SHIFT(>>, Long);
901   default:
902     std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
903     abort();
904   }
905   return Dest;
906 }
907
908 void Interpreter::visitShl(ShiftInst &I) {
909   ExecutionContext &SF = ECStack.back();
910   const Type *Ty    = I.getOperand(0)->getType();
911   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
912   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
913   GenericValue Dest;
914   Dest = executeShlInst (Src1, Src2, Ty);
915   SetValue(&I, Dest, SF);
916 }
917
918 void Interpreter::visitShr(ShiftInst &I) {
919   ExecutionContext &SF = ECStack.back();
920   const Type *Ty    = I.getOperand(0)->getType();
921   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
922   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
923   GenericValue Dest;
924   Dest = executeShrInst (Src1, Src2, Ty);
925   SetValue(&I, Dest, SF);
926 }
927
928 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
929    case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
930
931 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY)    \
932   case Type::DESTTY##TyID:                      \
933     switch (SrcTy->getTypeID()) {          \
934       IMPLEMENT_CAST(DESTTY, DESTCTY, Bool);    \
935       IMPLEMENT_CAST(DESTTY, DESTCTY, UByte);   \
936       IMPLEMENT_CAST(DESTTY, DESTCTY, SByte);   \
937       IMPLEMENT_CAST(DESTTY, DESTCTY, UShort);  \
938       IMPLEMENT_CAST(DESTTY, DESTCTY, Short);   \
939       IMPLEMENT_CAST(DESTTY, DESTCTY, UInt);    \
940       IMPLEMENT_CAST(DESTTY, DESTCTY, Int);     \
941       IMPLEMENT_CAST(DESTTY, DESTCTY, ULong);   \
942       IMPLEMENT_CAST(DESTTY, DESTCTY, Long);    \
943       IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
944
945 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
946       IMPLEMENT_CAST(DESTTY, DESTCTY, Float);   \
947       IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
948
949 #define IMPLEMENT_CAST_CASE_END()    \
950     default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
951       abort();                                  \
952     }                                           \
953     break
954
955 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
956    IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY);   \
957    IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
958    IMPLEMENT_CAST_CASE_END()
959
960 GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
961                                                ExecutionContext &SF) {
962   const Type *SrcTy = SrcVal->getType();
963   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
964
965   switch (Ty->getTypeID()) {
966     IMPLEMENT_CAST_CASE(UByte  , (unsigned char));
967     IMPLEMENT_CAST_CASE(SByte  , (  signed char));
968     IMPLEMENT_CAST_CASE(UShort , (unsigned short));
969     IMPLEMENT_CAST_CASE(Short  , (  signed short));
970     IMPLEMENT_CAST_CASE(UInt   , (unsigned int ));
971     IMPLEMENT_CAST_CASE(Int    , (  signed int ));
972     IMPLEMENT_CAST_CASE(ULong  , (uint64_t));
973     IMPLEMENT_CAST_CASE(Long   , ( int64_t));
974     IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
975     IMPLEMENT_CAST_CASE(Float  , (float));
976     IMPLEMENT_CAST_CASE(Double , (double));
977     IMPLEMENT_CAST_CASE(Bool   , (bool));
978   default:
979     std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
980     abort();
981   }
982
983   return Dest;
984 }
985
986 void Interpreter::visitCastInst(CastInst &I) {
987   ExecutionContext &SF = ECStack.back();
988   SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
989 }
990
991 #define IMPLEMENT_VAARG(TY) \
992    case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
993
994 void Interpreter::visitVAArgInst(VAArgInst &I) {
995   ExecutionContext &SF = ECStack.back();
996
997   // Get the incoming valist parameter.  LLI treats the valist as a
998   // (ec-stack-depth var-arg-index) pair.
999   GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1000   GenericValue Dest;
1001   GenericValue Src = ECStack[VAList.UIntPairVal.first]
1002    .VarArgs[VAList.UIntPairVal.second];
1003   const Type *Ty = I.getType();
1004   switch (Ty->getTypeID()) {
1005     IMPLEMENT_VAARG(UByte);
1006     IMPLEMENT_VAARG(SByte);
1007     IMPLEMENT_VAARG(UShort);
1008     IMPLEMENT_VAARG(Short);
1009     IMPLEMENT_VAARG(UInt);
1010     IMPLEMENT_VAARG(Int);
1011     IMPLEMENT_VAARG(ULong);
1012     IMPLEMENT_VAARG(Long);
1013     IMPLEMENT_VAARG(Pointer);
1014     IMPLEMENT_VAARG(Float);
1015     IMPLEMENT_VAARG(Double);
1016     IMPLEMENT_VAARG(Bool);
1017   default:
1018     std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1019     abort();
1020   }
1021
1022   // Set the Value of this Instruction.
1023   SetValue(&I, Dest, SF);
1024
1025   // Move the pointer to the next vararg.
1026   ++VAList.UIntPairVal.second;
1027 }
1028
1029 //===----------------------------------------------------------------------===//
1030 //                        Dispatch and Execution Code
1031 //===----------------------------------------------------------------------===//
1032
1033 //===----------------------------------------------------------------------===//
1034 // callFunction - Execute the specified function...
1035 //
1036 void Interpreter::callFunction(Function *F,
1037                                const std::vector<GenericValue> &ArgVals) {
1038   assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1039           ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1040          "Incorrect number of arguments passed into function call!");
1041   // Make a new stack frame... and fill it in.
1042   ECStack.push_back(ExecutionContext());
1043   ExecutionContext &StackFrame = ECStack.back();
1044   StackFrame.CurFunction = F;
1045
1046   // Special handling for external functions.
1047   if (F->isExternal()) {
1048     GenericValue Result = callExternalFunction (F, ArgVals);
1049     // Simulate a 'ret' instruction of the appropriate type.
1050     popStackAndReturnValueToCaller (F->getReturnType (), Result);
1051     return;
1052   }
1053
1054   // Get pointers to first LLVM BB & Instruction in function.
1055   StackFrame.CurBB     = F->begin();
1056   StackFrame.CurInst   = StackFrame.CurBB->begin();
1057
1058   // Run through the function arguments and initialize their values...
1059   assert((ArgVals.size() == F->arg_size() ||
1060          (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1061          "Invalid number of values passed to function invocation!");
1062
1063   // Handle non-varargs arguments...
1064   unsigned i = 0;
1065   for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
1066     SetValue(AI, ArgVals[i], StackFrame);
1067
1068   // Handle varargs arguments...
1069   StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1070 }
1071
1072 void Interpreter::run() {
1073   while (!ECStack.empty()) {
1074     // Interpret a single instruction & increment the "PC".
1075     ExecutionContext &SF = ECStack.back();  // Current stack frame
1076     Instruction &I = *SF.CurInst++;         // Increment before execute
1077
1078     // Track the number of dynamic instructions executed.
1079     ++NumDynamicInsts;
1080
1081     DEBUG(std::cerr << "About to interpret: " << I);
1082     visit(I);   // Dispatch to one of the visit* methods...
1083   }
1084 }