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