Implement APInt support for the binary operators.
[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/APInt.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/MathExtras.h"
25 #include <cmath>
26 using namespace llvm;
27
28 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
29 static Interpreter *TheEE = 0;
30
31 //===----------------------------------------------------------------------===//
32 //                     Various Helper Functions
33 //===----------------------------------------------------------------------===//
34
35 inline void initializeAPInt(GenericValue &GV, const Type* Ty, 
36                              ExecutionContext &SF) {
37   if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) 
38     GV.APIntVal = SF.getAPInt(ITy->getBitWidth());
39 }
40
41 static inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
42   // Determine if the value is signed or not
43   bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
44   // If its signed, extend the sign bits
45   if (isSigned)
46     Val |= ~ITy->getBitMask();
47   return Val;
48 }
49
50 static inline void maskToBitWidth(GenericValue& GV, unsigned BitWidth) {
51   uint64_t BitMask = ~(uint64_t)(0ull) >> (64-BitWidth);
52   if (BitWidth <= 8)
53     GV.Int8Val &= BitMask;
54   else if (BitWidth <= 16)
55     GV.Int16Val &= BitMask;
56   else if (BitWidth <= 32)
57     GV.Int32Val &= BitMask;
58   else if (BitWidth <= 64)
59     GV.Int64Val &= BitMask;
60   else {
61     assert(GV.APIntVal && "Unallocated GV.APIntVal");
62     *(GV.APIntVal) &= APInt::getAllOnesValue(BitWidth);
63   }
64 }
65
66 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
67   SF.Values[V] = Val;
68 }
69
70 void Interpreter::initializeExecutionEngine() {
71   TheEE = this;
72 }
73
74 //===----------------------------------------------------------------------===//
75 //                    Binary Instruction Implementations
76 //===----------------------------------------------------------------------===//
77
78 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
79    case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
80
81 #define IMPLEMENT_INTEGER_BINOP(OP, TY) \
82    case Type::IntegerTyID: { \
83      unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
84      if (BitWidth == 1) {\
85        Dest.Int1Val = Src1.Int1Val OP Src2.Int1Val; \
86        maskToBitWidth(Dest, BitWidth); \
87      } else if (BitWidth <= 8) {\
88        Dest.Int8Val = Src1.Int8Val OP Src2.Int8Val; \
89        maskToBitWidth(Dest, BitWidth); \
90      } else if (BitWidth <= 16) {\
91        Dest.Int16Val = Src1.Int16Val OP Src2.Int16Val; \
92        maskToBitWidth(Dest, BitWidth); \
93      } else if (BitWidth <= 32) {\
94        Dest.Int32Val = Src1.Int32Val OP Src2.Int32Val; \
95        maskToBitWidth(Dest, BitWidth); \
96      } else if (BitWidth <= 64) {\
97        Dest.Int64Val = Src1.Int64Val OP Src2.Int64Val; \
98        maskToBitWidth(Dest, BitWidth); \
99      } else \
100        *(Dest.APIntVal) = *(Src1.APIntVal) OP *(Src2.APIntVal); \
101      break; \
102    }
103
104 #define IMPLEMENT_SIGNED_BINOP(OP, TY, APOP) \
105    if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
106      unsigned BitWidth = ITy->getBitWidth(); \
107      if (BitWidth <= 8) { \
108        Dest.Int8Val  = ((int8_t)Src1.Int8Val)   OP ((int8_t)Src2.Int8Val); \
109        maskToBitWidth(Dest, BitWidth); \
110      } else if (BitWidth <= 16) { \
111        Dest.Int16Val = ((int16_t)Src1.Int16Val) OP ((int16_t)Src2.Int16Val); \
112        maskToBitWidth(Dest, BitWidth); \
113      } else if (BitWidth <= 32) { \
114        Dest.Int32Val = ((int32_t)Src1.Int32Val) OP ((int32_t)Src2.Int32Val); \
115        maskToBitWidth(Dest, BitWidth); \
116      } else if (BitWidth <= 64) { \
117        Dest.Int64Val = ((int64_t)Src1.Int64Val) OP ((int64_t)Src2.Int64Val); \
118        maskToBitWidth(Dest, BitWidth); \
119      } else \
120        *(Dest.APIntVal) = Src1.APIntVal->APOP(*(Src2.APIntVal)); \
121    } else { \
122     cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
123     abort(); \
124    }
125
126 #define IMPLEMENT_UNSIGNED_BINOP(OP, TY, APOP) \
127    if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \
128      unsigned BitWidth = ITy->getBitWidth(); \
129      if (BitWidth <= 8) {\
130        Dest.Int8Val  = ((uint8_t)Src1.Int8Val)   OP ((uint8_t)Src2.Int8Val); \
131        maskToBitWidth(Dest, BitWidth); \
132      } else if (BitWidth <= 16) {\
133        Dest.Int16Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
134        maskToBitWidth(Dest, BitWidth); \
135      } else if (BitWidth <= 32) {\
136        Dest.Int32Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
137        maskToBitWidth(Dest, BitWidth); \
138      } else if (BitWidth <= 64) {\
139        Dest.Int64Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
140        maskToBitWidth(Dest, BitWidth); \
141      } else \
142        *(Dest.APIntVal) = Src1.APIntVal->APOP(*(Src2.APIntVal)); \
143    } else { \
144     cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \
145     abort(); \
146   }
147
148 static void executeAddInst(GenericValue &Dest, GenericValue Src1, 
149                            GenericValue Src2, const Type *Ty) {
150   switch (Ty->getTypeID()) {
151     IMPLEMENT_INTEGER_BINOP(+, Ty);
152     IMPLEMENT_BINARY_OPERATOR(+, Float);
153     IMPLEMENT_BINARY_OPERATOR(+, Double);
154   default:
155     cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
156     abort();
157   }
158 }
159
160 static void executeSubInst(GenericValue &Dest, GenericValue Src1, 
161                            GenericValue Src2, const Type *Ty) {
162   switch (Ty->getTypeID()) {
163     IMPLEMENT_INTEGER_BINOP(-, Ty);
164     IMPLEMENT_BINARY_OPERATOR(-, Float);
165     IMPLEMENT_BINARY_OPERATOR(-, Double);
166   default:
167     cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
168     abort();
169   }
170 }
171
172 static void executeMulInst(GenericValue &Dest, GenericValue Src1, 
173                            GenericValue Src2, const Type *Ty) {
174   switch (Ty->getTypeID()) {
175     IMPLEMENT_INTEGER_BINOP(*, Ty);
176     IMPLEMENT_BINARY_OPERATOR(*, Float);
177     IMPLEMENT_BINARY_OPERATOR(*, Double);
178   default:
179     cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
180     abort();
181   }
182 }
183
184 static void executeUDivInst(GenericValue &Dest, GenericValue Src1, 
185                             GenericValue Src2, const Type *Ty) {
186   IMPLEMENT_UNSIGNED_BINOP(/,Ty,udiv)
187 }
188
189 static void executeSDivInst(GenericValue &Dest, GenericValue Src1, 
190                             GenericValue Src2, const Type *Ty) {
191   IMPLEMENT_SIGNED_BINOP(/,Ty,sdiv)
192 }
193
194 static void executeFDivInst(GenericValue &Dest, GenericValue Src1, 
195                             GenericValue Src2, const Type *Ty) {
196   switch (Ty->getTypeID()) {
197     IMPLEMENT_BINARY_OPERATOR(/, Float);
198     IMPLEMENT_BINARY_OPERATOR(/, Double);
199   default:
200     cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
201     abort();
202   }
203 }
204
205 static void executeURemInst(GenericValue &Dest, GenericValue Src1, 
206                             GenericValue Src2, const Type *Ty) {
207   IMPLEMENT_UNSIGNED_BINOP(%,Ty,urem)
208 }
209
210 static void executeSRemInst(GenericValue &Dest, GenericValue Src1, 
211                             GenericValue Src2, const Type *Ty) {
212   IMPLEMENT_SIGNED_BINOP(%,Ty,srem)
213 }
214
215 static void executeFRemInst(GenericValue &Dest, GenericValue Src1, 
216                             GenericValue Src2, const Type *Ty) {
217   switch (Ty->getTypeID()) {
218   case Type::FloatTyID:
219     Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
220     break;
221   case Type::DoubleTyID:
222     Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
223     break;
224   default:
225     cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
226     abort();
227   }
228 }
229
230 static void executeAndInst(GenericValue &Dest, GenericValue Src1, 
231                            GenericValue Src2, const Type *Ty) {
232   IMPLEMENT_UNSIGNED_BINOP(&,Ty,And)
233 }
234
235 static void executeOrInst(GenericValue &Dest, GenericValue Src1, 
236                           GenericValue Src2, const Type *Ty) {
237   IMPLEMENT_UNSIGNED_BINOP(|,Ty,Or)
238 }
239
240 static void executeXorInst(GenericValue &Dest, GenericValue Src1, 
241                            GenericValue Src2, const Type *Ty) {
242   IMPLEMENT_UNSIGNED_BINOP(^,Ty,Xor)
243 }
244
245 #define IMPLEMENT_SIGNED_ICMP(OP, TY) \
246    case Type::IntegerTyID: {  \
247      const IntegerType* ITy = cast<IntegerType>(TY); \
248      unsigned BitWidth = ITy->getBitWidth(); \
249      int64_t LHS = 0, RHS = 0; \
250      if (BitWidth <= 8) { \
251        LHS = int64_t(doSignExtension(uint64_t(Src1.Int8Val), ITy)); \
252        RHS = int64_t(doSignExtension(uint64_t(Src2.Int8Val), ITy)); \
253      } else if (BitWidth <= 16) { \
254        LHS = int64_t(doSignExtension(uint64_t(Src1.Int16Val), ITy)); \
255        RHS = int64_t(doSignExtension(uint64_t(Src2.Int16Val), ITy)); \
256     } else if (BitWidth <= 32) { \
257        LHS = int64_t(doSignExtension(uint64_t(Src1.Int32Val), ITy)); \
258        RHS = int64_t(doSignExtension(uint64_t(Src2.Int32Val), ITy)); \
259     } else if (BitWidth <= 64) { \
260        LHS = int64_t(doSignExtension(uint64_t(Src1.Int64Val), ITy)); \
261        RHS = int64_t(doSignExtension(uint64_t(Src2.Int64Val), ITy)); \
262     } else { \
263       cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
264        abort(); \
265      } \
266      Dest.Int1Val = LHS OP RHS; \
267      break; \
268    }
269
270 #define IMPLEMENT_UNSIGNED_ICMP(OP, TY) \
271    case Type::IntegerTyID: { \
272      unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \
273      if (BitWidth == 1) \
274        Dest.Int1Val = ((uint8_t)Src1.Int1Val)   OP ((uint8_t)Src2.Int1Val); \
275      else if (BitWidth <= 8) \
276        Dest.Int1Val = ((uint8_t)Src1.Int8Val)   OP ((uint8_t)Src2.Int8Val); \
277      else if (BitWidth <= 16) \
278        Dest.Int1Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \
279      else if (BitWidth <= 32) \
280        Dest.Int1Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \
281      else if (BitWidth <= 64) \
282        Dest.Int1Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \
283      else { \
284       cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
285        abort(); \
286      } \
287      maskToBitWidth(Dest, BitWidth); \
288      break; \
289    }
290
291 // Handle pointers specially because they must be compared with only as much
292 // width as the host has.  We _do not_ want to be comparing 64 bit values when
293 // running on a 32-bit target, otherwise the upper 32 bits might mess up
294 // comparisons if they contain garbage.
295 #define IMPLEMENT_POINTER_ICMP(OP) \
296    case Type::PointerTyID: \
297         Dest.Int1Val = (void*)(intptr_t)Src1.PointerVal OP \
298                        (void*)(intptr_t)Src2.PointerVal; break
299
300 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
301                                    const Type *Ty) {
302   GenericValue Dest;
303   switch (Ty->getTypeID()) {
304     IMPLEMENT_UNSIGNED_ICMP(==, Ty);
305     IMPLEMENT_POINTER_ICMP(==);
306   default:
307     cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
308     abort();
309   }
310   return Dest;
311 }
312
313 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
314                                    const Type *Ty) {
315   GenericValue Dest;
316   switch (Ty->getTypeID()) {
317     IMPLEMENT_UNSIGNED_ICMP(!=, Ty);
318     IMPLEMENT_POINTER_ICMP(!=);
319   default:
320     cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
321     abort();
322   }
323   return Dest;
324 }
325
326 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
327                                     const Type *Ty) {
328   GenericValue Dest;
329   switch (Ty->getTypeID()) {
330     IMPLEMENT_UNSIGNED_ICMP(<, Ty);
331     IMPLEMENT_POINTER_ICMP(<);
332   default:
333     cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
334     abort();
335   }
336   return Dest;
337 }
338
339 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
340                                     const Type *Ty) {
341   GenericValue Dest;
342   switch (Ty->getTypeID()) {
343     IMPLEMENT_SIGNED_ICMP(<, Ty);
344     IMPLEMENT_POINTER_ICMP(<);
345   default:
346     cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
347     abort();
348   }
349   return Dest;
350 }
351
352 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
353                                     const Type *Ty) {
354   GenericValue Dest;
355   switch (Ty->getTypeID()) {
356     IMPLEMENT_UNSIGNED_ICMP(>, Ty);
357     IMPLEMENT_POINTER_ICMP(>);
358   default:
359     cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
360     abort();
361   }
362   return Dest;
363 }
364
365 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
366                                     const Type *Ty) {
367   GenericValue Dest;
368   switch (Ty->getTypeID()) {
369     IMPLEMENT_SIGNED_ICMP(>, Ty);
370     IMPLEMENT_POINTER_ICMP(>);
371   default:
372     cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
373     abort();
374   }
375   return Dest;
376 }
377
378 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
379                                     const Type *Ty) {
380   GenericValue Dest;
381   switch (Ty->getTypeID()) {
382     IMPLEMENT_UNSIGNED_ICMP(<=, Ty);
383     IMPLEMENT_POINTER_ICMP(<=);
384   default:
385     cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
386     abort();
387   }
388   return Dest;
389 }
390
391 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
392                                     const Type *Ty) {
393   GenericValue Dest;
394   switch (Ty->getTypeID()) {
395     IMPLEMENT_SIGNED_ICMP(<=, Ty);
396     IMPLEMENT_POINTER_ICMP(<=);
397   default:
398     cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
399     abort();
400   }
401   return Dest;
402 }
403
404 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
405                                     const Type *Ty) {
406   GenericValue Dest;
407   switch (Ty->getTypeID()) {
408     IMPLEMENT_UNSIGNED_ICMP(>=,Ty);
409     IMPLEMENT_POINTER_ICMP(>=);
410   default:
411     cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
412     abort();
413   }
414   return Dest;
415 }
416
417 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
418                                     const Type *Ty) {
419   GenericValue Dest;
420   switch (Ty->getTypeID()) {
421     IMPLEMENT_SIGNED_ICMP(>=, Ty);
422     IMPLEMENT_POINTER_ICMP(>=);
423   default:
424     cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
425     abort();
426   }
427   return Dest;
428 }
429
430 void Interpreter::visitICmpInst(ICmpInst &I) {
431   ExecutionContext &SF = ECStack.back();
432   const Type *Ty    = I.getOperand(0)->getType();
433   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
434   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
435   GenericValue R;   // Result
436   
437   switch (I.getPredicate()) {
438   case ICmpInst::ICMP_EQ:  R = executeICMP_EQ(Src1,  Src2, Ty); break;
439   case ICmpInst::ICMP_NE:  R = executeICMP_NE(Src1,  Src2, Ty); break;
440   case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
441   case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
442   case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
443   case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
444   case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
445   case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
446   case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
447   case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
448   default:
449     cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
450     abort();
451   }
452  
453   SetValue(&I, R, SF);
454 }
455
456 #define IMPLEMENT_FCMP(OP, TY) \
457    case Type::TY##TyID: Dest.Int1Val = Src1.TY##Val OP Src2.TY##Val; break
458
459 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
460                                    const Type *Ty) {
461   GenericValue Dest;
462   switch (Ty->getTypeID()) {
463     IMPLEMENT_FCMP(==, Float);
464     IMPLEMENT_FCMP(==, Double);
465   default:
466     cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
467     abort();
468   }
469   return Dest;
470 }
471
472 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
473                                    const Type *Ty) {
474   GenericValue Dest;
475   switch (Ty->getTypeID()) {
476     IMPLEMENT_FCMP(!=, Float);
477     IMPLEMENT_FCMP(!=, Double);
478
479   default:
480     cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
481     abort();
482   }
483   return Dest;
484 }
485
486 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
487                                    const Type *Ty) {
488   GenericValue Dest;
489   switch (Ty->getTypeID()) {
490     IMPLEMENT_FCMP(<=, Float);
491     IMPLEMENT_FCMP(<=, Double);
492   default:
493     cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
494     abort();
495   }
496   return Dest;
497 }
498
499 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
500                                    const Type *Ty) {
501   GenericValue Dest;
502   switch (Ty->getTypeID()) {
503     IMPLEMENT_FCMP(>=, Float);
504     IMPLEMENT_FCMP(>=, Double);
505   default:
506     cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
507     abort();
508   }
509   return Dest;
510 }
511
512 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
513                                    const Type *Ty) {
514   GenericValue Dest;
515   switch (Ty->getTypeID()) {
516     IMPLEMENT_FCMP(<, Float);
517     IMPLEMENT_FCMP(<, Double);
518   default:
519     cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
520     abort();
521   }
522   return Dest;
523 }
524
525 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
526                                      const Type *Ty) {
527   GenericValue Dest;
528   switch (Ty->getTypeID()) {
529     IMPLEMENT_FCMP(>, Float);
530     IMPLEMENT_FCMP(>, Double);
531   default:
532     cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
533     abort();
534   }
535   return Dest;
536 }
537
538 #define IMPLEMENT_UNORDERED(TY, X,Y) \
539    if (TY == Type::FloatTy) \
540      if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
541        Dest.Int1Val = true; \
542        return Dest; \
543      } \
544    else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
545      Dest.Int1Val = true; \
546      return Dest; \
547    }
548
549
550 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
551                                    const Type *Ty) {
552   GenericValue Dest;
553   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
554   return executeFCMP_OEQ(Src1, Src2, Ty);
555 }
556
557 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
558                                    const Type *Ty) {
559   GenericValue Dest;
560   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
561   return executeFCMP_ONE(Src1, Src2, Ty);
562 }
563
564 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
565                                    const Type *Ty) {
566   GenericValue Dest;
567   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
568   return executeFCMP_OLE(Src1, Src2, Ty);
569 }
570
571 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
572                                    const Type *Ty) {
573   GenericValue Dest;
574   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
575   return executeFCMP_OGE(Src1, Src2, Ty);
576 }
577
578 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
579                                    const Type *Ty) {
580   GenericValue Dest;
581   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
582   return executeFCMP_OLT(Src1, Src2, Ty);
583 }
584
585 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
586                                      const Type *Ty) {
587   GenericValue Dest;
588   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
589   return executeFCMP_OGT(Src1, Src2, Ty);
590 }
591
592 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
593                                      const Type *Ty) {
594   GenericValue Dest;
595   if (Ty == Type::FloatTy)
596     Dest.Int1Val = (Src1.FloatVal == Src1.FloatVal && 
597                     Src2.FloatVal == Src2.FloatVal);
598   else
599     Dest.Int1Val = (Src1.DoubleVal == Src1.DoubleVal && 
600                     Src2.DoubleVal == Src2.DoubleVal);
601   return Dest;
602 }
603
604 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
605                                      const Type *Ty) {
606   GenericValue Dest;
607   if (Ty == Type::FloatTy)
608     Dest.Int1Val = (Src1.FloatVal != Src1.FloatVal || 
609                     Src2.FloatVal != Src2.FloatVal);
610   else
611     Dest.Int1Val = (Src1.DoubleVal != Src1.DoubleVal || 
612                     Src2.DoubleVal != Src2.DoubleVal);
613   return Dest;
614 }
615
616 void Interpreter::visitFCmpInst(FCmpInst &I) {
617   ExecutionContext &SF = ECStack.back();
618   const Type *Ty    = I.getOperand(0)->getType();
619   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
620   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
621   GenericValue R;   // Result
622   
623   switch (I.getPredicate()) {
624   case FCmpInst::FCMP_FALSE: R.Int1Val = false; break;
625   case FCmpInst::FCMP_TRUE:  R.Int1Val = true; break;
626   case FCmpInst::FCMP_ORD:   R = executeFCMP_ORD(Src1, Src2, Ty); break;
627   case FCmpInst::FCMP_UNO:   R = executeFCMP_UNO(Src1, Src2, Ty); break;
628   case FCmpInst::FCMP_UEQ:   R = executeFCMP_UEQ(Src1, Src2, Ty); break;
629   case FCmpInst::FCMP_OEQ:   R = executeFCMP_OEQ(Src1, Src2, Ty); break;
630   case FCmpInst::FCMP_UNE:   R = executeFCMP_UNE(Src1, Src2, Ty); break;
631   case FCmpInst::FCMP_ONE:   R = executeFCMP_ONE(Src1, Src2, Ty); break;
632   case FCmpInst::FCMP_ULT:   R = executeFCMP_ULT(Src1, Src2, Ty); break;
633   case FCmpInst::FCMP_OLT:   R = executeFCMP_OLT(Src1, Src2, Ty); break;
634   case FCmpInst::FCMP_UGT:   R = executeFCMP_UGT(Src1, Src2, Ty); break;
635   case FCmpInst::FCMP_OGT:   R = executeFCMP_OGT(Src1, Src2, Ty); break;
636   case FCmpInst::FCMP_ULE:   R = executeFCMP_ULE(Src1, Src2, Ty); break;
637   case FCmpInst::FCMP_OLE:   R = executeFCMP_OLE(Src1, Src2, Ty); break;
638   case FCmpInst::FCMP_UGE:   R = executeFCMP_UGE(Src1, Src2, Ty); break;
639   case FCmpInst::FCMP_OGE:   R = executeFCMP_OGE(Src1, Src2, Ty); break;
640   default:
641     cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
642     abort();
643   }
644  
645   SetValue(&I, R, SF);
646 }
647
648 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1, 
649                                    GenericValue Src2, const Type *Ty) {
650   GenericValue Result;
651   switch (predicate) {
652   case ICmpInst::ICMP_EQ:    return executeICMP_EQ(Src1, Src2, Ty);
653   case ICmpInst::ICMP_NE:    return executeICMP_NE(Src1, Src2, Ty);
654   case ICmpInst::ICMP_UGT:   return executeICMP_UGT(Src1, Src2, Ty);
655   case ICmpInst::ICMP_SGT:   return executeICMP_SGT(Src1, Src2, Ty);
656   case ICmpInst::ICMP_ULT:   return executeICMP_ULT(Src1, Src2, Ty);
657   case ICmpInst::ICMP_SLT:   return executeICMP_SLT(Src1, Src2, Ty);
658   case ICmpInst::ICMP_UGE:   return executeICMP_UGE(Src1, Src2, Ty);
659   case ICmpInst::ICMP_SGE:   return executeICMP_SGE(Src1, Src2, Ty);
660   case ICmpInst::ICMP_ULE:   return executeICMP_ULE(Src1, Src2, Ty);
661   case ICmpInst::ICMP_SLE:   return executeICMP_SLE(Src1, Src2, Ty);
662   case FCmpInst::FCMP_ORD:   return executeFCMP_ORD(Src1, Src2, Ty);
663   case FCmpInst::FCMP_UNO:   return executeFCMP_UNO(Src1, Src2, Ty);
664   case FCmpInst::FCMP_OEQ:   return executeFCMP_OEQ(Src1, Src2, Ty);
665   case FCmpInst::FCMP_UEQ:   return executeFCMP_UEQ(Src1, Src2, Ty);
666   case FCmpInst::FCMP_ONE:   return executeFCMP_ONE(Src1, Src2, Ty);
667   case FCmpInst::FCMP_UNE:   return executeFCMP_UNE(Src1, Src2, Ty);
668   case FCmpInst::FCMP_OLT:   return executeFCMP_OLT(Src1, Src2, Ty);
669   case FCmpInst::FCMP_ULT:   return executeFCMP_ULT(Src1, Src2, Ty);
670   case FCmpInst::FCMP_OGT:   return executeFCMP_OGT(Src1, Src2, Ty);
671   case FCmpInst::FCMP_UGT:   return executeFCMP_UGT(Src1, Src2, Ty);
672   case FCmpInst::FCMP_OLE:   return executeFCMP_OLE(Src1, Src2, Ty);
673   case FCmpInst::FCMP_ULE:   return executeFCMP_ULE(Src1, Src2, Ty);
674   case FCmpInst::FCMP_OGE:   return executeFCMP_OGE(Src1, Src2, Ty);
675   case FCmpInst::FCMP_UGE:   return executeFCMP_UGE(Src1, Src2, Ty);
676   case FCmpInst::FCMP_FALSE: { 
677     GenericValue Result;
678     Result.Int1Val = false; 
679     return Result;
680   }
681   case FCmpInst::FCMP_TRUE: {
682     GenericValue Result;
683     Result.Int1Val = true;
684     return Result;
685   }
686   default:
687     cerr << "Unhandled Cmp predicate\n";
688     abort();
689   }
690 }
691
692 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
693   ExecutionContext &SF = ECStack.back();
694   const Type *Ty    = I.getOperand(0)->getType();
695   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
696   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
697   GenericValue R;   // Result
698   initializeAPInt(R, Ty, SF);
699
700   switch (I.getOpcode()) {
701   case Instruction::Add:   executeAddInst  (R, Src1, Src2, Ty); break;
702   case Instruction::Sub:   executeSubInst  (R, Src1, Src2, Ty); break;
703   case Instruction::Mul:   executeMulInst  (R, Src1, Src2, Ty); break;
704   case Instruction::UDiv:  executeUDivInst (R, Src1, Src2, Ty); break;
705   case Instruction::SDiv:  executeSDivInst (R, Src1, Src2, Ty); break;
706   case Instruction::FDiv:  executeFDivInst (R, Src1, Src2, Ty); break;
707   case Instruction::URem:  executeURemInst (R, Src1, Src2, Ty); break;
708   case Instruction::SRem:  executeSRemInst (R, Src1, Src2, Ty); break;
709   case Instruction::FRem:  executeFRemInst (R, Src1, Src2, Ty); break;
710   case Instruction::And:   executeAndInst  (R, Src1, Src2, Ty); break;
711   case Instruction::Or:    executeOrInst   (R, Src1, Src2, Ty); break;
712   case Instruction::Xor:   executeXorInst  (R, Src1, Src2, Ty); break;
713   default:
714     cerr << "Don't know how to handle this binary operator!\n-->" << I;
715     abort();
716   }
717
718   SetValue(&I, R, SF);
719 }
720
721 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
722                                       GenericValue Src3) {
723   return Src1.Int1Val ? Src2 : Src3;
724 }
725
726 void Interpreter::visitSelectInst(SelectInst &I) {
727   ExecutionContext &SF = ECStack.back();
728   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
729   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
730   GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
731   GenericValue R;
732   initializeAPInt(R, I.getOperand(1)->getType(), SF);
733   R = executeSelectInst(Src1, Src2, Src3);
734   SetValue(&I, R, SF);
735 }
736
737
738 //===----------------------------------------------------------------------===//
739 //                     Terminator Instruction Implementations
740 //===----------------------------------------------------------------------===//
741
742 void Interpreter::exitCalled(GenericValue GV) {
743   // runAtExitHandlers() assumes there are no stack frames, but
744   // if exit() was called, then it had a stack frame. Blow away
745   // the stack before interpreting atexit handlers.
746   ECStack.clear ();
747   runAtExitHandlers ();
748   exit (GV.Int32Val);
749 }
750
751 /// Pop the last stack frame off of ECStack and then copy the result
752 /// back into the result variable if we are not returning void. The
753 /// result variable may be the ExitValue, or the Value of the calling
754 /// CallInst if there was a previous stack frame. This method may
755 /// invalidate any ECStack iterators you have. This method also takes
756 /// care of switching to the normal destination BB, if we are returning
757 /// from an invoke.
758 ///
759 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
760                                                   GenericValue Result) {
761   // Pop the current stack frame.
762   ECStack.pop_back();
763
764   if (ECStack.empty()) {  // Finished main.  Put result into exit code...
765     if (RetTy && RetTy->isInteger()) {          // Nonvoid return type?
766       ExitValue = Result;   // Capture the exit value of the program
767     } else {
768       memset(&ExitValue, 0, sizeof(ExitValue));
769     }
770   } else {
771     // If we have a previous stack frame, and we have a previous call,
772     // fill in the return value...
773     ExecutionContext &CallingSF = ECStack.back();
774     if (Instruction *I = CallingSF.Caller.getInstruction()) {
775       if (CallingSF.Caller.getType() != Type::VoidTy)      // Save result...
776         SetValue(I, Result, CallingSF);
777       if (InvokeInst *II = dyn_cast<InvokeInst> (I))
778         SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
779       CallingSF.Caller = CallSite();          // We returned from the call...
780     }
781   }
782 }
783
784 void Interpreter::visitReturnInst(ReturnInst &I) {
785   ExecutionContext &SF = ECStack.back();
786   const Type *RetTy = Type::VoidTy;
787   GenericValue Result;
788
789   // Save away the return value... (if we are not 'ret void')
790   if (I.getNumOperands()) {
791     RetTy  = I.getReturnValue()->getType();
792     Result = getOperandValue(I.getReturnValue(), SF);
793   }
794
795   popStackAndReturnValueToCaller(RetTy, Result);
796 }
797
798 void Interpreter::visitUnwindInst(UnwindInst &I) {
799   // Unwind stack
800   Instruction *Inst;
801   do {
802     ECStack.pop_back ();
803     if (ECStack.empty ())
804       abort ();
805     Inst = ECStack.back ().Caller.getInstruction ();
806   } while (!(Inst && isa<InvokeInst> (Inst)));
807
808   // Return from invoke
809   ExecutionContext &InvokingSF = ECStack.back ();
810   InvokingSF.Caller = CallSite ();
811
812   // Go to exceptional destination BB of invoke instruction
813   SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
814 }
815
816 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
817   cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
818   abort();
819 }
820
821 void Interpreter::visitBranchInst(BranchInst &I) {
822   ExecutionContext &SF = ECStack.back();
823   BasicBlock *Dest;
824
825   Dest = I.getSuccessor(0);          // Uncond branches have a fixed dest...
826   if (!I.isUnconditional()) {
827     Value *Cond = I.getCondition();
828     if (getOperandValue(Cond, SF).Int1Val == 0) // If false cond...
829       Dest = I.getSuccessor(1);
830   }
831   SwitchToNewBasicBlock(Dest, SF);
832 }
833
834 void Interpreter::visitSwitchInst(SwitchInst &I) {
835   ExecutionContext &SF = ECStack.back();
836   GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
837   const Type *ElTy = I.getOperand(0)->getType();
838
839   // Check to see if any of the cases match...
840   BasicBlock *Dest = 0;
841   for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
842     if (executeICMP_EQ(CondVal,
843                        getOperandValue(I.getOperand(i), SF), ElTy).Int1Val) {
844       Dest = cast<BasicBlock>(I.getOperand(i+1));
845       break;
846     }
847
848   if (!Dest) Dest = I.getDefaultDest();   // No cases matched: use default
849   SwitchToNewBasicBlock(Dest, SF);
850 }
851
852 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
853 // This function handles the actual updating of block and instruction iterators
854 // as well as execution of all of the PHI nodes in the destination block.
855 //
856 // This method does this because all of the PHI nodes must be executed
857 // atomically, reading their inputs before any of the results are updated.  Not
858 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
859 // their inputs.  If the input PHI node is updated before it is read, incorrect
860 // results can happen.  Thus we use a two phase approach.
861 //
862 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
863   BasicBlock *PrevBB = SF.CurBB;      // Remember where we came from...
864   SF.CurBB   = Dest;                  // Update CurBB to branch destination
865   SF.CurInst = SF.CurBB->begin();     // Update new instruction ptr...
866
867   if (!isa<PHINode>(SF.CurInst)) return;  // Nothing fancy to do
868
869   // Loop over all of the PHI nodes in the current block, reading their inputs.
870   std::vector<GenericValue> ResultValues;
871
872   for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
873     // Search for the value corresponding to this previous bb...
874     int i = PN->getBasicBlockIndex(PrevBB);
875     assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
876     Value *IncomingValue = PN->getIncomingValue(i);
877
878     // Save the incoming value for this PHI node...
879     ResultValues.push_back(getOperandValue(IncomingValue, SF));
880   }
881
882   // Now loop over all of the PHI nodes setting their values...
883   SF.CurInst = SF.CurBB->begin();
884   for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
885     PHINode *PN = cast<PHINode>(SF.CurInst);
886     SetValue(PN, ResultValues[i], SF);
887   }
888 }
889
890 //===----------------------------------------------------------------------===//
891 //                     Memory Instruction Implementations
892 //===----------------------------------------------------------------------===//
893
894 void Interpreter::visitAllocationInst(AllocationInst &I) {
895   ExecutionContext &SF = ECStack.back();
896
897   const Type *Ty = I.getType()->getElementType();  // Type to be allocated
898
899   // Get the number of elements being allocated by the array...
900   unsigned NumElements = getOperandValue(I.getOperand(0), SF).Int32Val;
901
902   // Allocate enough memory to hold the type...
903   void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
904
905   GenericValue Result = PTOGV(Memory);
906   assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
907   SetValue(&I, Result, SF);
908
909   if (I.getOpcode() == Instruction::Alloca)
910     ECStack.back().Allocas.add(Memory);
911 }
912
913 void Interpreter::visitFreeInst(FreeInst &I) {
914   ExecutionContext &SF = ECStack.back();
915   assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
916   GenericValue Value = getOperandValue(I.getOperand(0), SF);
917   // TODO: Check to make sure memory is allocated
918   free(GVTOP(Value));   // Free memory
919 }
920
921 // getElementOffset - The workhorse for getelementptr.
922 //
923 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
924                                               gep_type_iterator E,
925                                               ExecutionContext &SF) {
926   assert(isa<PointerType>(Ptr->getType()) &&
927          "Cannot getElementOffset of a nonpointer type!");
928
929   PointerTy Total = 0;
930
931   for (; I != E; ++I) {
932     if (const StructType *STy = dyn_cast<StructType>(*I)) {
933       const StructLayout *SLO = TD.getStructLayout(STy);
934
935       const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
936       unsigned Index = unsigned(CPU->getZExtValue());
937
938       Total += (PointerTy)SLO->getElementOffset(Index);
939     } else {
940       const SequentialType *ST = cast<SequentialType>(*I);
941       // Get the index number for the array... which must be long type...
942       GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
943
944       int64_t Idx;
945       unsigned BitWidth = 
946         cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
947       if (BitWidth == 32)
948         Idx = (int64_t)(int32_t)IdxGV.Int32Val;
949       else if (BitWidth == 64)
950         Idx = (int64_t)IdxGV.Int64Val;
951       else 
952         assert(0 && "Invalid index type for getelementptr");
953       Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
954     }
955   }
956
957   GenericValue Result;
958   Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
959   return Result;
960 }
961
962 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
963   ExecutionContext &SF = ECStack.back();
964   SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
965                                    gep_type_begin(I), gep_type_end(I), SF), SF);
966 }
967
968 void Interpreter::visitLoadInst(LoadInst &I) {
969   ExecutionContext &SF = ECStack.back();
970   GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
971   GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
972   GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
973   SetValue(&I, Result, SF);
974 }
975
976 void Interpreter::visitStoreInst(StoreInst &I) {
977   ExecutionContext &SF = ECStack.back();
978   GenericValue Val = getOperandValue(I.getOperand(0), SF);
979   GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
980   StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
981                      I.getOperand(0)->getType());
982 }
983
984 //===----------------------------------------------------------------------===//
985 //                 Miscellaneous Instruction Implementations
986 //===----------------------------------------------------------------------===//
987
988 void Interpreter::visitCallSite(CallSite CS) {
989   ExecutionContext &SF = ECStack.back();
990
991   // Check to see if this is an intrinsic function call...
992   if (Function *F = CS.getCalledFunction())
993    if (F->isDeclaration ())
994     switch (F->getIntrinsicID()) {
995     case Intrinsic::not_intrinsic:
996       break;
997     case Intrinsic::vastart: { // va_start
998       GenericValue ArgIndex;
999       ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1000       ArgIndex.UIntPairVal.second = 0;
1001       SetValue(CS.getInstruction(), ArgIndex, SF);
1002       return;
1003     }
1004     case Intrinsic::vaend:    // va_end is a noop for the interpreter
1005       return;
1006     case Intrinsic::vacopy:   // va_copy: dest = src
1007       SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1008       return;
1009     default:
1010       // If it is an unknown intrinsic function, use the intrinsic lowering
1011       // class to transform it into hopefully tasty LLVM code.
1012       //
1013       Instruction *Prev = CS.getInstruction()->getPrev();
1014       BasicBlock *Parent = CS.getInstruction()->getParent();
1015       IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1016
1017       // Restore the CurInst pointer to the first instruction newly inserted, if
1018       // any.
1019       if (!Prev) {
1020         SF.CurInst = Parent->begin();
1021       } else {
1022         SF.CurInst = Prev;
1023         ++SF.CurInst;
1024       }
1025       return;
1026     }
1027
1028   SF.Caller = CS;
1029   std::vector<GenericValue> ArgVals;
1030   const unsigned NumArgs = SF.Caller.arg_size();
1031   ArgVals.reserve(NumArgs);
1032   for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1033          e = SF.Caller.arg_end(); i != e; ++i) {
1034     Value *V = *i;
1035     ArgVals.push_back(getOperandValue(V, SF));
1036     // Promote all integral types whose size is < sizeof(int) into ints.  We do
1037     // this by zero or sign extending the value as appropriate according to the
1038     // source type.
1039     const Type *Ty = V->getType();
1040     if (Ty->isInteger()) {
1041       if (Ty->getPrimitiveSizeInBits() == 1)
1042         ArgVals.back().Int32Val = ArgVals.back().Int1Val;
1043       else if (Ty->getPrimitiveSizeInBits() <= 8)
1044         ArgVals.back().Int32Val = ArgVals.back().Int8Val;
1045       else if (Ty->getPrimitiveSizeInBits() <= 16)
1046         ArgVals.back().Int32Val = ArgVals.back().Int16Val;
1047     }
1048   }
1049
1050   // To handle indirect calls, we must get the pointer value from the argument
1051   // and treat it as a function pointer.
1052   GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
1053   callFunction((Function*)GVTOP(SRC), ArgVals);
1054 }
1055
1056 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
1057                                    const Type *Ty) {
1058   GenericValue Dest;
1059   if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1060     unsigned BitWidth = ITy->getBitWidth();
1061     if (BitWidth <= 8)
1062       Dest.Int8Val  = ((uint8_t)Src1.Int8Val)   << ((uint32_t)Src2.Int8Val);
1063     else if (BitWidth <= 16)
1064       Dest.Int16Val = ((uint16_t)Src1.Int16Val) << ((uint32_t)Src2.Int8Val);
1065     else if (BitWidth <= 32)
1066       Dest.Int32Val = ((uint32_t)Src1.Int32Val) << ((uint32_t)Src2.Int8Val);
1067     else if (BitWidth <= 64)
1068       Dest.Int64Val = ((uint64_t)Src1.Int64Val) << ((uint32_t)Src2.Int8Val);
1069     else {
1070       cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1071       abort();
1072     }
1073     maskToBitWidth(Dest, BitWidth);
1074   } else {
1075     cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
1076     abort();
1077   }
1078   return Dest;
1079 }
1080
1081 static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
1082                                     const Type *Ty) {
1083   GenericValue Dest;
1084   if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1085     unsigned BitWidth = ITy->getBitWidth();
1086     if (BitWidth <= 8)
1087       Dest.Int8Val = ((uint8_t)Src1.Int8Val)   >> ((uint32_t)Src2.Int8Val);
1088     else if (BitWidth <= 16)
1089       Dest.Int16Val = ((uint16_t)Src1.Int16Val) >> ((uint32_t)Src2.Int8Val);
1090     else if (BitWidth <= 32)
1091       Dest.Int32Val = ((uint32_t)Src1.Int32Val) >> ((uint32_t)Src2.Int8Val);
1092     else if (BitWidth <= 64)
1093       Dest.Int64Val = ((uint64_t)Src1.Int64Val) >> ((uint32_t)Src2.Int8Val);
1094     else {
1095       cerr << "Integer types > 64 bits not supported: " << *Ty << "\n";
1096       abort();
1097     }
1098     maskToBitWidth(Dest, BitWidth);
1099   } else {
1100     cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
1101     abort();
1102   }
1103   return Dest;
1104 }
1105
1106 static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
1107                                     const Type *Ty) {
1108   GenericValue Dest;
1109   if (const IntegerType *ITy = cast<IntegerType>(Ty)) {
1110     unsigned BitWidth = ITy->getBitWidth();
1111     if (BitWidth <= 8)
1112       Dest.Int8Val  = ((int8_t)Src1.Int8Val)   >> ((int32_t)Src2.Int8Val);
1113     else if (BitWidth <= 16)
1114       Dest.Int16Val = ((int16_t)Src1.Int16Val) >> ((int32_t)Src2.Int8Val);
1115     else if (BitWidth <= 32)
1116       Dest.Int32Val = ((int32_t)Src1.Int32Val) >> ((int32_t)Src2.Int8Val);
1117     else if (BitWidth <= 64)
1118       Dest.Int64Val = ((int64_t)Src1.Int64Val) >> ((int32_t)Src2.Int8Val);
1119     else {
1120       cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \
1121       abort();
1122     } 
1123     maskToBitWidth(Dest, BitWidth);
1124   } else { 
1125     cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
1126     abort();
1127   }
1128   return Dest;
1129 }
1130
1131 void Interpreter::visitShl(BinaryOperator &I) {
1132   ExecutionContext &SF = ECStack.back();
1133   const Type *Ty    = I.getOperand(0)->getType();
1134   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1135   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1136   GenericValue Dest;
1137   Dest = executeShlInst (Src1, Src2, Ty);
1138   SetValue(&I, Dest, SF);
1139 }
1140
1141 void Interpreter::visitLShr(BinaryOperator &I) {
1142   ExecutionContext &SF = ECStack.back();
1143   const Type *Ty    = I.getOperand(0)->getType();
1144   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1145   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1146   GenericValue Dest;
1147   Dest = executeLShrInst (Src1, Src2, Ty);
1148   SetValue(&I, Dest, SF);
1149 }
1150
1151 void Interpreter::visitAShr(BinaryOperator &I) {
1152   ExecutionContext &SF = ECStack.back();
1153   const Type *Ty    = I.getOperand(0)->getType();
1154   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1155   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1156   GenericValue Dest;
1157   Dest = executeAShrInst (Src1, Src2, Ty);
1158   SetValue(&I, Dest, SF);
1159 }
1160
1161 #define INTEGER_ASSIGN(DEST, BITWIDTH, VAL)     \
1162   {                                             \
1163     uint64_t Mask = ~(uint64_t)(0ull) >> (64-BITWIDTH);     \
1164     if (BITWIDTH == 1) {                        \
1165       Dest.Int1Val = (bool) (VAL & Mask);       \
1166     } else if (BITWIDTH <= 8) {                 \
1167       Dest.Int8Val = (uint8_t) (VAL & Mask);    \
1168     } else if (BITWIDTH <= 16) {                \
1169       Dest.Int16Val = (uint16_t) (VAL & Mask);  \
1170     } else if (BITWIDTH <= 32) {                \
1171       Dest.Int32Val = (uint32_t) (VAL & Mask);  \
1172     } else                                      \
1173       Dest.Int64Val = (uint64_t) (VAL & Mask);  \
1174   }
1175
1176 GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
1177                                            ExecutionContext &SF) {
1178   const Type *SrcTy = SrcVal->getType();
1179   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1180   const IntegerType *DITy = cast<IntegerType>(DstTy);
1181   const IntegerType *SITy = cast<IntegerType>(SrcTy);
1182   unsigned DBitWidth = DITy->getBitWidth();
1183   unsigned SBitWidth = SITy->getBitWidth();
1184   assert(SBitWidth > DBitWidth && "Invalid truncate");
1185
1186   if (DBitWidth > 64) {
1187     // Both values are APInt, just use the APInt trunc
1188     initializeAPInt(Dest, DstTy, SF);
1189     *(Dest.APIntVal) = Src.APIntVal->trunc(DBitWidth);
1190     return Dest;
1191   }
1192
1193   uint64_t MaskedVal = 0;
1194   uint64_t Mask = (1ULL << DBitWidth) - 1;
1195
1196   // Mask the source value to its actual bit width. This ensures that any
1197   // high order bits are cleared.
1198   if (SBitWidth <= 8)
1199     MaskedVal = Src.Int8Val  & Mask;
1200   else if (SBitWidth <= 16)
1201     MaskedVal = Src.Int16Val & Mask;
1202   else if (SBitWidth <= 32)
1203     MaskedVal = Src.Int32Val & Mask;
1204   else if (SBitWidth <= 64)
1205     MaskedVal = Src.Int64Val & Mask;
1206   else
1207     MaskedVal = Src.APIntVal->trunc(DBitWidth).getZExtValue();
1208
1209   INTEGER_ASSIGN(Dest, DBitWidth, MaskedVal);
1210   return Dest;
1211 }
1212
1213 GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
1214                                           ExecutionContext &SF) {
1215   const Type *SrcTy = SrcVal->getType();
1216   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1217   const IntegerType *DITy = cast<IntegerType>(DstTy);
1218   const IntegerType *SITy = cast<IntegerType>(SrcTy);
1219   unsigned DBitWidth = DITy->getBitWidth();
1220   unsigned SBitWidth = SITy->getBitWidth();
1221   assert(SBitWidth <= 64 && DBitWidth <= 64  && 
1222          "Integer types > 64 bits not supported");
1223   assert(SBitWidth < DBitWidth && "Invalid sign extend");
1224
1225   // Normalize to a 64-bit value.
1226   uint64_t Normalized = 0;
1227   if (SBitWidth <= 8)
1228     Normalized = Src.Int8Val;
1229   else if (SBitWidth <= 16)
1230     Normalized = Src.Int16Val;
1231   else if (SBitWidth <= 32)
1232     Normalized = Src.Int32Val;
1233   else 
1234     Normalized = Src.Int64Val;
1235
1236   Normalized = doSignExtension(Normalized, SITy);
1237
1238   // Now that we have a sign extended value, assign it to the destination
1239   INTEGER_ASSIGN(Dest, DBitWidth, Normalized);
1240   return Dest;
1241 }
1242
1243 GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
1244                                           ExecutionContext &SF) {
1245   const Type *SrcTy = SrcVal->getType();
1246   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1247   const IntegerType *DITy = cast<IntegerType>(DstTy);
1248   const IntegerType *SITy = cast<IntegerType>(SrcTy);
1249   unsigned DBitWidth = DITy->getBitWidth();
1250   unsigned SBitWidth = SITy->getBitWidth();
1251   assert(SBitWidth <= 64 && DBitWidth <= 64  && 
1252          "Integer types > 64 bits not supported");
1253   assert(SBitWidth < DBitWidth && "Invalid sign extend");
1254   uint64_t Extended = 0;
1255   if (SBitWidth == 1)
1256     // For sign extension from bool, we must extend the source bits.
1257     Extended = (uint64_t) (Src.Int1Val & 1);
1258   else if (SBitWidth <= 8)
1259     Extended = (uint64_t) (uint8_t)Src.Int8Val;
1260   else if (SBitWidth <= 16)
1261     Extended = (uint64_t) (uint16_t)Src.Int16Val;
1262   else if (SBitWidth <= 32)
1263     Extended = (uint64_t) (uint32_t)Src.Int32Val;
1264   else 
1265     Extended = (uint64_t) Src.Int64Val;
1266
1267   // Now that we have a sign extended value, assign it to the destination
1268   INTEGER_ASSIGN(Dest, DBitWidth, Extended);
1269   return Dest;
1270 }
1271
1272 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
1273                                              ExecutionContext &SF) {
1274   const Type *SrcTy = SrcVal->getType();
1275   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1276   assert(SrcTy == Type::DoubleTy && DstTy == Type::FloatTy &&
1277          "Invalid FPTrunc instruction");
1278   Dest.FloatVal = (float) Src.DoubleVal;
1279   return Dest;
1280 }
1281
1282 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
1283                                            ExecutionContext &SF) {
1284   const Type *SrcTy = SrcVal->getType();
1285   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1286   assert(SrcTy == Type::FloatTy && DstTy == Type::DoubleTy &&
1287          "Invalid FPTrunc instruction");
1288   Dest.DoubleVal = (double) Src.FloatVal;
1289   return Dest;
1290 }
1291
1292 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
1293                                             ExecutionContext &SF) {
1294   const Type *SrcTy = SrcVal->getType();
1295   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1296   const IntegerType *DITy = cast<IntegerType>(DstTy);
1297   unsigned DBitWidth = DITy->getBitWidth();
1298   assert(DBitWidth <= 64  && "Integer types > 64 bits not supported");
1299   assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
1300   uint64_t Converted = 0;
1301   if (SrcTy->getTypeID() == Type::FloatTyID)
1302     Converted = (uint64_t) Src.FloatVal;
1303   else
1304     Converted = (uint64_t) Src.DoubleVal;
1305
1306   INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1307   return Dest;
1308 }
1309
1310 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
1311                                             ExecutionContext &SF) {
1312   const Type *SrcTy = SrcVal->getType();
1313   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1314   const IntegerType *DITy = cast<IntegerType>(DstTy);
1315   unsigned DBitWidth = DITy->getBitWidth();
1316   assert(DBitWidth <= 64  && "Integer types > 64 bits not supported");
1317   assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1318   int64_t Converted = 0;
1319   if (SrcTy->getTypeID() == Type::FloatTyID)
1320     Converted = (int64_t) Src.FloatVal;
1321   else
1322     Converted = (int64_t) Src.DoubleVal;
1323
1324   INTEGER_ASSIGN(Dest, DBitWidth, Converted);
1325   return Dest;
1326 }
1327
1328 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1329                                             ExecutionContext &SF) {
1330   const Type *SrcTy = SrcVal->getType();
1331   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1332   const IntegerType *SITy = cast<IntegerType>(SrcTy);
1333   unsigned SBitWidth = SITy->getBitWidth();
1334   assert(SBitWidth <= 64  && "Integer types > 64 bits not supported");
1335   assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1336   uint64_t Converted = 0;
1337   if (SBitWidth == 1)
1338     Converted = (uint64_t) Src.Int1Val;
1339   else if (SBitWidth <= 8)
1340     Converted = (uint64_t) Src.Int8Val;
1341   else if (SBitWidth <= 16)
1342     Converted = (uint64_t) Src.Int16Val;
1343   else if (SBitWidth <= 32)
1344     Converted = (uint64_t) Src.Int32Val;
1345   else 
1346     Converted = (uint64_t) Src.Int64Val;
1347
1348   if (DstTy->getTypeID() == Type::FloatTyID)
1349     Dest.FloatVal = (float) Converted;
1350   else
1351     Dest.DoubleVal = (double) Converted;
1352   return Dest;
1353 }
1354
1355 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1356                                             ExecutionContext &SF) {
1357   const Type *SrcTy = SrcVal->getType();
1358   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1359   const IntegerType *SITy = cast<IntegerType>(SrcTy);
1360   unsigned SBitWidth = SITy->getBitWidth();
1361   assert(SBitWidth <= 64  && "Integer types > 64 bits not supported");
1362   assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
1363   int64_t Converted = 0;
1364   if (SBitWidth == 1)
1365     Converted = 0LL - Src.Int1Val;
1366   else if (SBitWidth <= 8)
1367     Converted = (int64_t) (int8_t)Src.Int8Val;
1368   else if (SBitWidth <= 16)
1369     Converted = (int64_t) (int16_t)Src.Int16Val;
1370   else if (SBitWidth <= 32)
1371     Converted = (int64_t) (int32_t)Src.Int32Val;
1372   else 
1373     Converted = (int64_t) Src.Int64Val;
1374
1375   if (DstTy->getTypeID() == Type::FloatTyID)
1376     Dest.FloatVal = (float) Converted;
1377   else
1378     Dest.DoubleVal = (double) Converted;
1379   return Dest;
1380 }
1381
1382 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1383                                               ExecutionContext &SF) {
1384   const Type *SrcTy = SrcVal->getType();
1385   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1386   const IntegerType *DITy = cast<IntegerType>(DstTy);
1387   unsigned DBitWidth = DITy->getBitWidth();
1388   assert(DBitWidth <= 64  && "Integer types > 64 bits not supported");
1389   assert(isa<PointerType>(SrcTy) && "Invalid PtrToInt instruction");
1390   INTEGER_ASSIGN(Dest, DBitWidth, (intptr_t) Src.PointerVal);
1391   return Dest;
1392 }
1393
1394 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1395                                               ExecutionContext &SF) {
1396   const Type *SrcTy = SrcVal->getType();
1397   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1398   const IntegerType *SITy = cast<IntegerType>(SrcTy);
1399   unsigned SBitWidth = SITy->getBitWidth();
1400   assert(SBitWidth <= 64  && "Integer types > 64 bits not supported");
1401   assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1402   uint64_t Converted = 0;
1403   if (SBitWidth == 1)
1404     Converted = (uint64_t) Src.Int1Val;
1405   else if (SBitWidth <= 8)
1406     Converted = (uint64_t) Src.Int8Val;
1407   else if (SBitWidth <= 16)
1408     Converted = (uint64_t) Src.Int16Val;
1409   else if (SBitWidth <= 32)
1410     Converted = (uint64_t) Src.Int32Val;
1411   else 
1412     Converted = (uint64_t) Src.Int64Val;
1413
1414   Dest.PointerVal = (PointerTy) Converted;
1415   return Dest;
1416 }
1417
1418 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1419                                              ExecutionContext &SF) {
1420   
1421   const Type *SrcTy = SrcVal->getType();
1422   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1423   if (isa<PointerType>(DstTy)) {
1424     assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1425     Dest.PointerVal = Src.PointerVal;
1426   } else if (DstTy->isInteger()) {
1427     const IntegerType *DITy = cast<IntegerType>(DstTy);
1428     unsigned DBitWidth = DITy->getBitWidth();
1429     if (SrcTy == Type::FloatTy) {
1430       Dest.Int32Val = FloatToBits(Src.FloatVal);
1431     } else if (SrcTy == Type::DoubleTy) {
1432       Dest.Int64Val = DoubleToBits(Src.DoubleVal);
1433     } else if (SrcTy->isInteger()) {
1434       const IntegerType *SITy = cast<IntegerType>(SrcTy);
1435       unsigned SBitWidth = SITy->getBitWidth();
1436       assert(SBitWidth <= 64  && "Integer types > 64 bits not supported");
1437       assert(SBitWidth == DBitWidth && "Invalid BitCast");
1438       if (SBitWidth == 1)
1439         Dest.Int1Val = Src.Int1Val;
1440       else if (SBitWidth <= 8)
1441         Dest.Int8Val =  Src.Int8Val;
1442       else if (SBitWidth <= 16)
1443         Dest.Int16Val = Src.Int16Val;
1444       else if (SBitWidth <= 32)
1445         Dest.Int32Val = Src.Int32Val;
1446       else 
1447         Dest.Int64Val = Src.Int64Val;
1448       maskToBitWidth(Dest, DBitWidth);
1449     } else 
1450       assert(0 && "Invalid BitCast");
1451   } else if (DstTy == Type::FloatTy) {
1452     if (SrcTy->isInteger())
1453       Dest.FloatVal = BitsToFloat(Src.Int32Val);
1454     else
1455       Dest.FloatVal = Src.FloatVal;
1456   } else if (DstTy == Type::DoubleTy) {
1457     if (SrcTy->isInteger())
1458       Dest.DoubleVal = BitsToDouble(Src.Int64Val);
1459     else
1460       Dest.DoubleVal = Src.DoubleVal;
1461   } else
1462     assert(0 && "Invalid Bitcast");
1463
1464   return Dest;
1465 }
1466
1467 void Interpreter::visitTruncInst(TruncInst &I) {
1468   ExecutionContext &SF = ECStack.back();
1469   SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1470 }
1471
1472 void Interpreter::visitSExtInst(SExtInst &I) {
1473   ExecutionContext &SF = ECStack.back();
1474   SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1475 }
1476
1477 void Interpreter::visitZExtInst(ZExtInst &I) {
1478   ExecutionContext &SF = ECStack.back();
1479   SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1480 }
1481
1482 void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1483   ExecutionContext &SF = ECStack.back();
1484   SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1485 }
1486
1487 void Interpreter::visitFPExtInst(FPExtInst &I) {
1488   ExecutionContext &SF = ECStack.back();
1489   SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1490 }
1491
1492 void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1493   ExecutionContext &SF = ECStack.back();
1494   SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1495 }
1496
1497 void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1498   ExecutionContext &SF = ECStack.back();
1499   SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1500 }
1501
1502 void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1503   ExecutionContext &SF = ECStack.back();
1504   SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1505 }
1506
1507 void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1508   ExecutionContext &SF = ECStack.back();
1509   SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1510 }
1511
1512 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1513   ExecutionContext &SF = ECStack.back();
1514   SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1515 }
1516
1517 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1518   ExecutionContext &SF = ECStack.back();
1519   SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1520 }
1521
1522 void Interpreter::visitBitCastInst(BitCastInst &I) {
1523   ExecutionContext &SF = ECStack.back();
1524   SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1525 }
1526
1527 #define IMPLEMENT_VAARG(TY) \
1528    case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1529
1530 void Interpreter::visitVAArgInst(VAArgInst &I) {
1531   ExecutionContext &SF = ECStack.back();
1532
1533   // Get the incoming valist parameter.  LLI treats the valist as a
1534   // (ec-stack-depth var-arg-index) pair.
1535   GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1536   GenericValue Dest;
1537   GenericValue Src = ECStack[VAList.UIntPairVal.first]
1538    .VarArgs[VAList.UIntPairVal.second];
1539   const Type *Ty = I.getType();
1540   switch (Ty->getTypeID()) {
1541     case Type::IntegerTyID: {
1542       unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
1543       if (BitWidth == 1)
1544         Dest.Int1Val = Src.Int1Val;
1545       else if (BitWidth <= 8)
1546         Dest.Int8Val = Src.Int8Val;
1547       else if (BitWidth <= 16)
1548         Dest.Int16Val = Src.Int16Val;
1549       else if (BitWidth <= 32)
1550         Dest.Int32Val = Src.Int32Val;
1551       else if (BitWidth <= 64)
1552         Dest.Int64Val = Src.Int64Val;
1553       else
1554         assert(0 && "Integer types > 64 bits not supported");
1555       maskToBitWidth(Dest, BitWidth);
1556     }
1557     IMPLEMENT_VAARG(Pointer);
1558     IMPLEMENT_VAARG(Float);
1559     IMPLEMENT_VAARG(Double);
1560   default:
1561     cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1562     abort();
1563   }
1564
1565   // Set the Value of this Instruction.
1566   SetValue(&I, Dest, SF);
1567
1568   // Move the pointer to the next vararg.
1569   ++VAList.UIntPairVal.second;
1570 }
1571
1572 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1573                                                 ExecutionContext &SF) {
1574   switch (CE->getOpcode()) {
1575   case Instruction::Trunc:   
1576       return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1577   case Instruction::ZExt:
1578       return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1579   case Instruction::SExt:
1580       return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1581   case Instruction::FPTrunc:
1582       return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1583   case Instruction::FPExt:
1584       return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1585   case Instruction::UIToFP:
1586       return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1587   case Instruction::SIToFP:
1588       return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1589   case Instruction::FPToUI:
1590       return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1591   case Instruction::FPToSI:
1592       return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1593   case Instruction::PtrToInt:
1594       return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1595   case Instruction::IntToPtr:
1596       return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1597   case Instruction::BitCast:
1598       return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1599   case Instruction::GetElementPtr:
1600     return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1601                                gep_type_end(CE), SF);
1602   default :
1603     break;
1604   }
1605   GenericValue Dest;
1606   initializeAPInt(Dest, CE->getType(), SF);
1607   switch (CE->getOpcode()) {
1608   case Instruction::Add:
1609     executeAddInst(Dest, getOperandValue(CE->getOperand(0), SF),
1610                          getOperandValue(CE->getOperand(1), SF),
1611                          CE->getOperand(0)->getType());
1612   case Instruction::Sub:
1613     executeSubInst(Dest, getOperandValue(CE->getOperand(0), SF),
1614                          getOperandValue(CE->getOperand(1), SF),
1615                          CE->getOperand(0)->getType());
1616   case Instruction::Mul:
1617     executeMulInst(Dest, getOperandValue(CE->getOperand(0), SF),
1618                          getOperandValue(CE->getOperand(1), SF),
1619                          CE->getOperand(0)->getType());
1620   case Instruction::SDiv:
1621     executeSDivInst(Dest, getOperandValue(CE->getOperand(0), SF),
1622                           getOperandValue(CE->getOperand(1), SF),
1623                           CE->getOperand(0)->getType());
1624   case Instruction::UDiv:
1625     executeUDivInst(Dest, getOperandValue(CE->getOperand(0), SF),
1626                           getOperandValue(CE->getOperand(1), SF),
1627                           CE->getOperand(0)->getType());
1628   case Instruction::FDiv:
1629     executeFDivInst(Dest, getOperandValue(CE->getOperand(0), SF),
1630                           getOperandValue(CE->getOperand(1), SF),
1631                           CE->getOperand(0)->getType());
1632   case Instruction::URem:
1633     executeURemInst(Dest, getOperandValue(CE->getOperand(0), SF),
1634                           getOperandValue(CE->getOperand(1), SF),
1635                           CE->getOperand(0)->getType());
1636   case Instruction::SRem:
1637     executeSRemInst(Dest, getOperandValue(CE->getOperand(0), SF),
1638                           getOperandValue(CE->getOperand(1), SF),
1639                           CE->getOperand(0)->getType());
1640   case Instruction::FRem:
1641     executeFRemInst(Dest, getOperandValue(CE->getOperand(0), SF),
1642                           getOperandValue(CE->getOperand(1), SF),
1643                           CE->getOperand(0)->getType());
1644   case Instruction::And:
1645     executeAndInst(Dest, getOperandValue(CE->getOperand(0), SF),
1646                          getOperandValue(CE->getOperand(1), SF),
1647                          CE->getOperand(0)->getType());
1648   case Instruction::Or:
1649     executeOrInst(Dest, getOperandValue(CE->getOperand(0), SF),
1650                         getOperandValue(CE->getOperand(1), SF),
1651                         CE->getOperand(0)->getType());
1652   case Instruction::Xor:
1653     executeXorInst(Dest, getOperandValue(CE->getOperand(0), SF),
1654                          getOperandValue(CE->getOperand(1), SF),
1655                          CE->getOperand(0)->getType());
1656   case Instruction::FCmp:
1657   case Instruction::ICmp:
1658     return executeCmpInst(CE->getPredicate(),
1659                           getOperandValue(CE->getOperand(0), SF),
1660                           getOperandValue(CE->getOperand(1), SF),
1661                           CE->getOperand(0)->getType());
1662   case Instruction::Shl:
1663     return executeShlInst(getOperandValue(CE->getOperand(0), SF),
1664                           getOperandValue(CE->getOperand(1), SF),
1665                           CE->getOperand(0)->getType());
1666   case Instruction::LShr:
1667     return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
1668                            getOperandValue(CE->getOperand(1), SF),
1669                            CE->getOperand(0)->getType());
1670   case Instruction::AShr:
1671     return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
1672                            getOperandValue(CE->getOperand(1), SF),
1673                            CE->getOperand(0)->getType());
1674   case Instruction::Select:
1675     return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1676                              getOperandValue(CE->getOperand(1), SF),
1677                              getOperandValue(CE->getOperand(2), SF));
1678   default:
1679     cerr << "Unhandled ConstantExpr: " << *CE << "\n";
1680     abort();
1681     return GenericValue();
1682   }
1683 }
1684
1685 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1686   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1687     return getConstantExprValue(CE, SF);
1688   } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1689     return getConstantValue(CPV);
1690   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1691     return PTOGV(getPointerToGlobal(GV));
1692   } else {
1693     return SF.Values[V];
1694   }
1695 }
1696
1697 //===----------------------------------------------------------------------===//
1698 //                        Dispatch and Execution Code
1699 //===----------------------------------------------------------------------===//
1700
1701 //===----------------------------------------------------------------------===//
1702 // callFunction - Execute the specified function...
1703 //
1704 void Interpreter::callFunction(Function *F,
1705                                const std::vector<GenericValue> &ArgVals) {
1706   assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1707           ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1708          "Incorrect number of arguments passed into function call!");
1709   // Make a new stack frame... and fill it in.
1710   ECStack.push_back(ExecutionContext());
1711   ExecutionContext &StackFrame = ECStack.back();
1712   StackFrame.CurFunction = F;
1713
1714   // Special handling for external functions.
1715   if (F->isDeclaration()) {
1716     GenericValue Result = callExternalFunction (F, ArgVals);
1717     // Simulate a 'ret' instruction of the appropriate type.
1718     popStackAndReturnValueToCaller (F->getReturnType (), Result);
1719     return;
1720   }
1721
1722   // Get pointers to first LLVM BB & Instruction in function.
1723   StackFrame.CurBB     = F->begin();
1724   StackFrame.CurInst   = StackFrame.CurBB->begin();
1725
1726   // Run through the function arguments and initialize their values...
1727   assert((ArgVals.size() == F->arg_size() ||
1728          (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1729          "Invalid number of values passed to function invocation!");
1730
1731   // Handle non-varargs arguments...
1732   unsigned i = 0;
1733   for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
1734     SetValue(AI, ArgVals[i], StackFrame);
1735
1736   // Handle varargs arguments...
1737   StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1738 }
1739
1740 void Interpreter::run() {
1741   while (!ECStack.empty()) {
1742     // Interpret a single instruction & increment the "PC".
1743     ExecutionContext &SF = ECStack.back();  // Current stack frame
1744     Instruction &I = *SF.CurInst++;         // Increment before execute
1745
1746     // Track the number of dynamic instructions executed.
1747     ++NumDynamicInsts;
1748
1749     DOUT << "About to interpret: " << I;
1750     visit(I);   // Dispatch to one of the visit* methods...
1751   }
1752 }