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