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