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