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