589ac7f99dc62a9ec67fdb03e8f48ae063b04f6d
[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 ExitCode, 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       ExitCode = Result.IntVal;   // Capture the exit code of the program 
570     } else { 
571       ExitCode = 0; 
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::visitBranchInst(BranchInst &I) {
620   ExecutionContext &SF = ECStack.back();
621   BasicBlock *Dest;
622
623   Dest = I.getSuccessor(0);          // Uncond branches have a fixed dest...
624   if (!I.isUnconditional()) {
625     Value *Cond = I.getCondition();
626     if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
627       Dest = I.getSuccessor(1);    
628   }
629   SwitchToNewBasicBlock(Dest, SF);
630 }
631
632 void Interpreter::visitSwitchInst(SwitchInst &I) {
633   ExecutionContext &SF = ECStack.back();
634   GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
635   const Type *ElTy = I.getOperand(0)->getType();
636
637   // Check to see if any of the cases match...
638   BasicBlock *Dest = 0;
639   for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
640     if (executeSetEQInst(CondVal,
641                          getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
642       Dest = cast<BasicBlock>(I.getOperand(i+1));
643       break;
644     }
645   
646   if (!Dest) Dest = I.getDefaultDest();   // No cases matched: use default
647   SwitchToNewBasicBlock(Dest, SF);
648 }
649
650 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
651 // This function handles the actual updating of block and instruction iterators
652 // as well as execution of all of the PHI nodes in the destination block.
653 //
654 // This method does this because all of the PHI nodes must be executed
655 // atomically, reading their inputs before any of the results are updated.  Not
656 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
657 // their inputs.  If the input PHI node is updated before it is read, incorrect
658 // results can happen.  Thus we use a two phase approach.
659 //
660 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
661   BasicBlock *PrevBB = SF.CurBB;      // Remember where we came from...
662   SF.CurBB   = Dest;                  // Update CurBB to branch destination
663   SF.CurInst = SF.CurBB->begin();     // Update new instruction ptr...
664
665   if (!isa<PHINode>(SF.CurInst)) return;  // Nothing fancy to do
666
667   // Loop over all of the PHI nodes in the current block, reading their inputs.
668   std::vector<GenericValue> ResultValues;
669
670   for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
671     // Search for the value corresponding to this previous bb...
672     int i = PN->getBasicBlockIndex(PrevBB);
673     assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
674     Value *IncomingValue = PN->getIncomingValue(i);
675     
676     // Save the incoming value for this PHI node...
677     ResultValues.push_back(getOperandValue(IncomingValue, SF));
678   }
679
680   // Now loop over all of the PHI nodes setting their values...
681   SF.CurInst = SF.CurBB->begin();
682   for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
683     PHINode *PN = cast<PHINode>(SF.CurInst);
684     SetValue(PN, ResultValues[i], SF);
685   }
686 }
687
688 //===----------------------------------------------------------------------===//
689 //                     Memory Instruction Implementations
690 //===----------------------------------------------------------------------===//
691
692 void Interpreter::visitAllocationInst(AllocationInst &I) {
693   ExecutionContext &SF = ECStack.back();
694
695   const Type *Ty = I.getType()->getElementType();  // Type to be allocated
696
697   // Get the number of elements being allocated by the array...
698   unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
699
700   // Allocate enough memory to hold the type...
701   void *Memory = malloc(NumElements * TD.getTypeSize(Ty));
702
703   GenericValue Result = PTOGV(Memory);
704   assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
705   SetValue(&I, Result, SF);
706
707   if (I.getOpcode() == Instruction::Alloca)
708     ECStack.back().Allocas.add(Memory);
709 }
710
711 void Interpreter::visitFreeInst(FreeInst &I) {
712   ExecutionContext &SF = ECStack.back();
713   assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
714   GenericValue Value = getOperandValue(I.getOperand(0), SF);
715   // TODO: Check to make sure memory is allocated
716   free(GVTOP(Value));   // Free memory
717 }
718
719 // getElementOffset - The workhorse for getelementptr.
720 //
721 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
722                                               gep_type_iterator E,
723                                               ExecutionContext &SF) {
724   assert(isa<PointerType>(Ptr->getType()) &&
725          "Cannot getElementOffset of a nonpointer type!");
726
727   PointerTy Total = 0;
728
729   for (; I != E; ++I) {
730     if (const StructType *STy = dyn_cast<StructType>(*I)) {
731       const StructLayout *SLO = TD.getStructLayout(STy);
732       
733       const ConstantUInt *CPU = cast<ConstantUInt>(I.getOperand());
734       unsigned Index = CPU->getValue();
735       
736       Total += SLO->MemberOffsets[Index];
737     } else {
738       const SequentialType *ST = cast<SequentialType>(*I);
739       // Get the index number for the array... which must be long type...
740       GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
741
742       uint64_t Idx;
743       switch (I.getOperand()->getType()->getTypeID()) {
744       default: assert(0 && "Illegal getelementptr index for sequential type!");
745       case Type::SByteTyID:  Idx = IdxGV.SByteVal; break;
746       case Type::ShortTyID:  Idx = IdxGV.ShortVal; break;
747       case Type::IntTyID:    Idx = IdxGV.IntVal; break;
748       case Type::LongTyID:   Idx = IdxGV.LongVal; break;
749       case Type::UByteTyID:  Idx = IdxGV.UByteVal; break;
750       case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
751       case Type::UIntTyID:   Idx = IdxGV.UIntVal; break;
752       case Type::ULongTyID:  Idx = IdxGV.ULongVal; break;
753       }
754       Total += TD.getTypeSize(ST->getElementType())*Idx;
755     }
756   }
757
758   GenericValue Result;
759   Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
760   return Result;
761 }
762
763 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
764   ExecutionContext &SF = ECStack.back();
765   SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
766                                    gep_type_begin(I), gep_type_end(I), SF), SF);
767 }
768
769 void Interpreter::visitLoadInst(LoadInst &I) {
770   ExecutionContext &SF = ECStack.back();
771   GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
772   GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
773   GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
774   SetValue(&I, Result, SF);
775 }
776
777 void Interpreter::visitStoreInst(StoreInst &I) {
778   ExecutionContext &SF = ECStack.back();
779   GenericValue Val = getOperandValue(I.getOperand(0), SF);
780   GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
781   StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
782                      I.getOperand(0)->getType());
783 }
784
785 //===----------------------------------------------------------------------===//
786 //                 Miscellaneous Instruction Implementations
787 //===----------------------------------------------------------------------===//
788
789 void Interpreter::visitCallSite(CallSite CS) {
790   ExecutionContext &SF = ECStack.back();
791
792   // Check to see if this is an intrinsic function call...
793   if (Function *F = CS.getCalledFunction())
794    if (F->isExternal ())
795     switch (F->getIntrinsicID()) {
796     case Intrinsic::not_intrinsic:
797       break;
798     case Intrinsic::vastart: { // va_start
799       GenericValue ArgIndex;
800       ArgIndex.UIntPairVal.first = ECStack.size() - 1;
801       ArgIndex.UIntPairVal.second = 0;
802       SetValue(CS.getInstruction(), ArgIndex, SF);
803       return;
804     }
805     case Intrinsic::vaend:    // va_end is a noop for the interpreter
806       return;
807     case Intrinsic::vacopy:   // va_copy: dest = src
808       SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
809       return;
810     default:
811       // If it is an unknown intrinsic function, use the intrinsic lowering
812       // class to transform it into hopefully tasty LLVM code.
813       //
814       Instruction *Prev = CS.getInstruction()->getPrev();
815       BasicBlock *Parent = CS.getInstruction()->getParent();
816       IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
817
818       // Restore the CurInst pointer to the first instruction newly inserted, if
819       // any.
820       if (!Prev) {
821         SF.CurInst = Parent->begin();
822       } else {
823         SF.CurInst = Prev;
824         ++SF.CurInst;
825       }
826       return;
827     }
828
829   SF.Caller = CS;
830   std::vector<GenericValue> ArgVals;
831   const unsigned NumArgs = SF.Caller.arg_size();
832   ArgVals.reserve(NumArgs);
833   for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
834          e = SF.Caller.arg_end(); i != e; ++i) {
835     Value *V = *i;
836     ArgVals.push_back(getOperandValue(V, SF));
837     // Promote all integral types whose size is < sizeof(int) into ints.  We do
838     // this by zero or sign extending the value as appropriate according to the
839     // source type.
840     const Type *Ty = V->getType();
841     if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
842       if (Ty == Type::ShortTy)
843         ArgVals.back().IntVal = ArgVals.back().ShortVal;
844       else if (Ty == Type::UShortTy)
845         ArgVals.back().UIntVal = ArgVals.back().UShortVal;
846       else if (Ty == Type::SByteTy)
847         ArgVals.back().IntVal = ArgVals.back().SByteVal;
848       else if (Ty == Type::UByteTy)
849         ArgVals.back().UIntVal = ArgVals.back().UByteVal;
850       else if (Ty == Type::BoolTy)
851         ArgVals.back().UIntVal = ArgVals.back().BoolVal;
852       else
853         assert(0 && "Unknown type!");
854     }
855   }
856
857   // To handle indirect calls, we must get the pointer value from the argument 
858   // and treat it as a function pointer.
859   GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);  
860   callFunction((Function*)GVTOP(SRC), ArgVals);
861 }
862
863 #define IMPLEMENT_SHIFT(OP, TY) \
864    case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
865
866 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
867                                    const Type *Ty) {
868   GenericValue Dest;
869   switch (Ty->getTypeID()) {
870     IMPLEMENT_SHIFT(<<, UByte);
871     IMPLEMENT_SHIFT(<<, SByte);
872     IMPLEMENT_SHIFT(<<, UShort);
873     IMPLEMENT_SHIFT(<<, Short);
874     IMPLEMENT_SHIFT(<<, UInt);
875     IMPLEMENT_SHIFT(<<, Int);
876     IMPLEMENT_SHIFT(<<, ULong);
877     IMPLEMENT_SHIFT(<<, Long);
878   default:
879     std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
880   }
881   return Dest;
882 }
883
884 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
885                                    const Type *Ty) {
886   GenericValue Dest;
887   switch (Ty->getTypeID()) {
888     IMPLEMENT_SHIFT(>>, UByte);
889     IMPLEMENT_SHIFT(>>, SByte);
890     IMPLEMENT_SHIFT(>>, UShort);
891     IMPLEMENT_SHIFT(>>, Short);
892     IMPLEMENT_SHIFT(>>, UInt);
893     IMPLEMENT_SHIFT(>>, Int);
894     IMPLEMENT_SHIFT(>>, ULong);
895     IMPLEMENT_SHIFT(>>, Long);
896   default:
897     std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
898     abort();
899   }
900   return Dest;
901 }
902
903 void Interpreter::visitShl(ShiftInst &I) {
904   ExecutionContext &SF = ECStack.back();
905   const Type *Ty    = I.getOperand(0)->getType();
906   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
907   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
908   GenericValue Dest;
909   Dest = executeShlInst (Src1, Src2, Ty);
910   SetValue(&I, Dest, SF);
911 }
912
913 void Interpreter::visitShr(ShiftInst &I) {
914   ExecutionContext &SF = ECStack.back();
915   const Type *Ty    = I.getOperand(0)->getType();
916   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
917   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
918   GenericValue Dest;
919   Dest = executeShrInst (Src1, Src2, Ty);
920   SetValue(&I, Dest, SF);
921 }
922
923 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
924    case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
925
926 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY)    \
927   case Type::DESTTY##TyID:                      \
928     switch (SrcTy->getTypeID()) {          \
929       IMPLEMENT_CAST(DESTTY, DESTCTY, Bool);    \
930       IMPLEMENT_CAST(DESTTY, DESTCTY, UByte);   \
931       IMPLEMENT_CAST(DESTTY, DESTCTY, SByte);   \
932       IMPLEMENT_CAST(DESTTY, DESTCTY, UShort);  \
933       IMPLEMENT_CAST(DESTTY, DESTCTY, Short);   \
934       IMPLEMENT_CAST(DESTTY, DESTCTY, UInt);    \
935       IMPLEMENT_CAST(DESTTY, DESTCTY, Int);     \
936       IMPLEMENT_CAST(DESTTY, DESTCTY, ULong);   \
937       IMPLEMENT_CAST(DESTTY, DESTCTY, Long);    \
938       IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
939
940 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
941       IMPLEMENT_CAST(DESTTY, DESTCTY, Float);   \
942       IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
943
944 #define IMPLEMENT_CAST_CASE_END()    \
945     default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
946       abort();                                  \
947     }                                           \
948     break
949
950 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
951    IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY);   \
952    IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
953    IMPLEMENT_CAST_CASE_END()
954
955 GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
956                                                ExecutionContext &SF) {
957   const Type *SrcTy = SrcVal->getType();
958   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
959
960   switch (Ty->getTypeID()) {
961     IMPLEMENT_CAST_CASE(UByte  , (unsigned char));
962     IMPLEMENT_CAST_CASE(SByte  , (  signed char));
963     IMPLEMENT_CAST_CASE(UShort , (unsigned short));
964     IMPLEMENT_CAST_CASE(Short  , (  signed short));
965     IMPLEMENT_CAST_CASE(UInt   , (unsigned int ));
966     IMPLEMENT_CAST_CASE(Int    , (  signed int ));
967     IMPLEMENT_CAST_CASE(ULong  , (uint64_t));
968     IMPLEMENT_CAST_CASE(Long   , ( int64_t));
969     IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
970     IMPLEMENT_CAST_CASE(Float  , (float));
971     IMPLEMENT_CAST_CASE(Double , (double));
972     IMPLEMENT_CAST_CASE(Bool   , (bool));
973   default:
974     std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
975     abort();
976   }
977
978   return Dest;
979 }
980
981 void Interpreter::visitCastInst(CastInst &I) {
982   ExecutionContext &SF = ECStack.back();
983   SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
984 }
985
986 void Interpreter::visitVANextInst(VANextInst &I) {
987   ExecutionContext &SF = ECStack.back();
988
989   // Get the incoming valist parameter.  LLI treats the valist as a
990   // (ec-stack-depth var-arg-index) pair.
991   GenericValue VAList = getOperandValue(I.getOperand(0), SF);
992   
993   // Move the pointer to the next vararg.
994   ++VAList.UIntPairVal.second;
995   SetValue(&I, VAList, SF);
996 }
997
998 #define IMPLEMENT_VAARG(TY) \
999    case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1000
1001 void Interpreter::visitVAArgInst(VAArgInst &I) {
1002   ExecutionContext &SF = ECStack.back();
1003
1004   // Get the incoming valist parameter.  LLI treats the valist as a
1005   // (ec-stack-depth var-arg-index) pair.
1006   GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1007   GenericValue Dest;
1008   GenericValue Src = ECStack[VAList.UIntPairVal.first]
1009         .VarArgs[VAList.UIntPairVal.second];
1010   const Type *Ty = I.getType();
1011   switch (Ty->getTypeID()) {
1012     IMPLEMENT_VAARG(UByte);
1013     IMPLEMENT_VAARG(SByte);
1014     IMPLEMENT_VAARG(UShort);
1015     IMPLEMENT_VAARG(Short);
1016     IMPLEMENT_VAARG(UInt);
1017     IMPLEMENT_VAARG(Int);
1018     IMPLEMENT_VAARG(ULong);
1019     IMPLEMENT_VAARG(Long);
1020     IMPLEMENT_VAARG(Pointer);
1021     IMPLEMENT_VAARG(Float);
1022     IMPLEMENT_VAARG(Double);
1023     IMPLEMENT_VAARG(Bool);
1024   default:
1025     std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1026     abort();
1027   }
1028   
1029   // Set the Value of this Instruction.
1030   SetValue(&I, Dest, SF);
1031 }
1032
1033 //===----------------------------------------------------------------------===//
1034 //                        Dispatch and Execution Code
1035 //===----------------------------------------------------------------------===//
1036
1037 //===----------------------------------------------------------------------===//
1038 // callFunction - Execute the specified function...
1039 //
1040 void Interpreter::callFunction(Function *F,
1041                                const std::vector<GenericValue> &ArgVals) {
1042   assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 || 
1043           ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1044          "Incorrect number of arguments passed into function call!");
1045   // Make a new stack frame... and fill it in.
1046   ECStack.push_back(ExecutionContext());
1047   ExecutionContext &StackFrame = ECStack.back();
1048   StackFrame.CurFunction = F;
1049
1050   // Special handling for external functions.
1051   if (F->isExternal()) {
1052     GenericValue Result = callExternalFunction (F, ArgVals);
1053     // Simulate a 'ret' instruction of the appropriate type.
1054     popStackAndReturnValueToCaller (F->getReturnType (), Result);
1055     return;
1056   }
1057
1058   // Get pointers to first LLVM BB & Instruction in function.
1059   StackFrame.CurBB     = F->begin();
1060   StackFrame.CurInst   = StackFrame.CurBB->begin();
1061
1062   // Run through the function arguments and initialize their values...
1063   assert((ArgVals.size() == F->asize() ||
1064          (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
1065          "Invalid number of values passed to function invocation!");
1066
1067   // Handle non-varargs arguments...
1068   unsigned i = 0;
1069   for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
1070     SetValue(AI, ArgVals[i], StackFrame);
1071
1072   // Handle varargs arguments...
1073   StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1074 }
1075
1076 void Interpreter::run() {
1077   while (!ECStack.empty()) {
1078     // Interpret a single instruction & increment the "PC".
1079     ExecutionContext &SF = ECStack.back();  // Current stack frame
1080     Instruction &I = *SF.CurInst++;         // Increment before execute
1081     
1082     // Track the number of dynamic instructions executed.
1083     ++NumDynamicInsts;
1084
1085     DEBUG(std::cerr << "About to interpret: " << I);
1086     visit(I);   // Dispatch to one of the visit* methods...
1087   }
1088 }