1 //===-- Execution.cpp - Implement code to simulate the program ------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains the actual instruction interpreter.
12 //===----------------------------------------------------------------------===//
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"
26 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
27 static Interpreter *TheEE = 0;
30 //===----------------------------------------------------------------------===//
31 // Value Manipulation code
32 //===----------------------------------------------------------------------===//
34 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
36 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
38 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
40 static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
42 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
44 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
46 static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
48 static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
50 static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
52 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
54 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
56 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
58 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
59 GenericValue Src2, const Type *Ty);
60 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
62 static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
64 static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
66 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
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));
160 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
162 return GenericValue();
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));
178 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
182 void Interpreter::initializeExecutionEngine() {
186 //===----------------------------------------------------------------------===//
187 // Binary Instruction Implementations
188 //===----------------------------------------------------------------------===//
190 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
191 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
193 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
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);
208 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
214 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
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);
229 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
235 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
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);
250 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
256 #define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
257 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
259 static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
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);
268 cerr << "Unhandled type for UDiv instruction: " << *Ty << "\n";
274 static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
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);
283 cerr << "Unhandled type for SDiv instruction: " << *Ty << "\n";
289 static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
292 switch (Ty->getTypeID()) {
293 IMPLEMENT_BINARY_OPERATOR(/, Float);
294 IMPLEMENT_BINARY_OPERATOR(/, Double);
296 cerr << "Unhandled type for Div instruction: " << *Ty << "\n";
302 static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
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);
311 cerr << "Unhandled type for URem instruction: " << *Ty << "\n";
317 static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
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);
326 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
332 static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
335 switch (Ty->getTypeID()) {
336 case Type::FloatTyID:
337 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
339 case Type::DoubleTyID:
340 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
343 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
349 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
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);
363 cerr << "Unhandled type for And instruction: " << *Ty << "\n";
369 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
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);
383 cerr << "Unhandled type for Or instruction: " << *Ty << "\n";
389 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
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);
403 cerr << "Unhandled type for Xor instruction: " << *Ty << "\n";
409 #define IMPLEMENT_CMP(OP, TY1, TY2) \
410 case Type::TY1##TyID: Dest.BoolVal = Src1.TY2##Val OP Src2.TY2##Val; break
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
421 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
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(==);
435 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
441 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
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(!=);
455 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
461 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
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(<);
475 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
481 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
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(<);
495 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
501 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
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(>);
515 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
521 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
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(>);
535 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
541 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
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(<=);
555 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
561 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
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(<=);
575 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
581 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
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(>=);
595 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
601 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
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(>=);
615 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
621 #define IMPLEMENT_SETCC(OP, TY) \
622 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
624 static GenericValue executeFCMP_EQ(GenericValue Src1, GenericValue Src2,
627 switch (Ty->getTypeID()) {
628 IMPLEMENT_SETCC(==, Float);
629 IMPLEMENT_SETCC(==, Double);
631 cerr << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
637 static GenericValue executeFCMP_NE(GenericValue Src1, GenericValue Src2,
640 switch (Ty->getTypeID()) {
641 IMPLEMENT_SETCC(!=, Float);
642 IMPLEMENT_SETCC(!=, Double);
645 cerr << "Unhandled type for SetNE instruction: " << *Ty << "\n";
651 static GenericValue executeFCMP_LE(GenericValue Src1, GenericValue Src2,
654 switch (Ty->getTypeID()) {
655 IMPLEMENT_SETCC(<=, Float);
656 IMPLEMENT_SETCC(<=, Double);
658 cerr << "Unhandled type for SetLE instruction: " << *Ty << "\n";
664 static GenericValue executeFCMP_GE(GenericValue Src1, GenericValue Src2,
667 switch (Ty->getTypeID()) {
668 IMPLEMENT_SETCC(>=, Float);
669 IMPLEMENT_SETCC(>=, Double);
671 cerr << "Unhandled type for SetGE instruction: " << *Ty << "\n";
677 static GenericValue executeFCMP_LT(GenericValue Src1, GenericValue Src2,
680 switch (Ty->getTypeID()) {
681 IMPLEMENT_SETCC(<, Float);
682 IMPLEMENT_SETCC(<, Double);
684 cerr << "Unhandled type for SetLT instruction: " << *Ty << "\n";
690 static GenericValue executeFCMP_GT(GenericValue Src1, GenericValue Src2,
693 switch (Ty->getTypeID()) {
694 IMPLEMENT_SETCC(>, Float);
695 IMPLEMENT_SETCC(>, Double);
697 cerr << "Unhandled type for SetGT instruction: " << *Ty << "\n";
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
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;
728 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
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
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;
754 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
761 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
762 GenericValue Src2, const Type *Ty) {
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: {
791 Result.BoolVal = false;
794 case FCmpInst::FCMP_TRUE: {
796 Result.BoolVal = true;
800 cerr << "Unhandled Cmp predicate\n";
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
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;
826 cerr << "Don't know how to handle this binary operator!\n-->" << I;
833 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
835 return Src1.BoolVal ? Src2 : Src3;
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);
848 //===----------------------------------------------------------------------===//
849 // Terminator Instruction Implementations
850 //===----------------------------------------------------------------------===//
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.
857 runAtExitHandlers ();
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
869 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
870 GenericValue Result) {
871 // Pop the current stack frame.
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
878 memset(&ExitValue, 0, sizeof(ExitValue));
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...
894 void Interpreter::visitReturnInst(ReturnInst &I) {
895 ExecutionContext &SF = ECStack.back();
896 const Type *RetTy = Type::VoidTy;
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);
905 popStackAndReturnValueToCaller(RetTy, Result);
908 void Interpreter::visitUnwindInst(UnwindInst &I) {
913 if (ECStack.empty ())
915 Inst = ECStack.back ().Caller.getInstruction ();
916 } while (!(Inst && isa<InvokeInst> (Inst)));
918 // Return from invoke
919 ExecutionContext &InvokingSF = ECStack.back ();
920 InvokingSF.Caller = CallSite ();
922 // Go to exceptional destination BB of invoke instruction
923 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
926 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
927 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
931 void Interpreter::visitBranchInst(BranchInst &I) {
932 ExecutionContext &SF = ECStack.back();
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);
941 SwitchToNewBasicBlock(Dest, SF);
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();
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));
958 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
959 SwitchToNewBasicBlock(Dest, SF);
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.
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.
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...
977 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
979 // Loop over all of the PHI nodes in the current block, reading their inputs.
980 std::vector<GenericValue> ResultValues;
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);
988 // Save the incoming value for this PHI node...
989 ResultValues.push_back(getOperandValue(IncomingValue, SF));
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);
1000 //===----------------------------------------------------------------------===//
1001 // Memory Instruction Implementations
1002 //===----------------------------------------------------------------------===//
1004 void Interpreter::visitAllocationInst(AllocationInst &I) {
1005 ExecutionContext &SF = ECStack.back();
1007 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
1009 // Get the number of elements being allocated by the array...
1010 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
1012 // Allocate enough memory to hold the type...
1013 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
1015 GenericValue Result = PTOGV(Memory);
1016 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
1017 SetValue(&I, Result, SF);
1019 if (I.getOpcode() == Instruction::Alloca)
1020 ECStack.back().Allocas.add(Memory);
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
1031 // getElementOffset - The workhorse for getelementptr.
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!");
1039 PointerTy Total = 0;
1041 for (; I != E; ++I) {
1042 if (const StructType *STy = dyn_cast<StructType>(*I)) {
1043 const StructLayout *SLO = TD.getStructLayout(STy);
1045 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
1046 unsigned Index = unsigned(CPU->getZExtValue());
1048 Total += (PointerTy)SLO->MemberOffsets[Index];
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);
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;
1066 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
1070 GenericValue Result;
1071 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
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);
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);
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());
1097 //===----------------------------------------------------------------------===//
1098 // Miscellaneous Instruction Implementations
1099 //===----------------------------------------------------------------------===//
1101 void Interpreter::visitCallSite(CallSite CS) {
1102 ExecutionContext &SF = ECStack.back();
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:
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);
1117 case Intrinsic::vaend: // va_end is a noop for the interpreter
1119 case Intrinsic::vacopy: // va_copy: dest = src
1120 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1123 // If it is an unknown intrinsic function, use the intrinsic lowering
1124 // class to transform it into hopefully tasty LLVM code.
1126 Instruction *Prev = CS.getInstruction()->getPrev();
1127 BasicBlock *Parent = CS.getInstruction()->getParent();
1128 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1130 // Restore the CurInst pointer to the first instruction newly inserted, if
1133 SF.CurInst = Parent->begin();
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) {
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
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;
1165 assert(0 && "Unknown type!");
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);
1175 #define IMPLEMENT_SHIFT(OP, TY) \
1176 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
1178 #define IMPLEMENT_SIGNLESS_SHIFT(OP, TY1, TY2) \
1179 case Type::TY2##TyID: \
1180 IMPLEMENT_SHIFT(OP, TY1)
1182 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
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);
1195 cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
1200 static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
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);
1209 cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
1215 static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
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);
1224 cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
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);
1236 Dest = executeShlInst (Src1, Src2, Ty);
1237 SetValue(&I, Dest, SF);
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);
1246 Dest = executeLShrInst (Src1, Src2, Ty);
1247 SetValue(&I, Dest, SF);
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);
1256 Dest = executeAShrInst (Src1, Src2, Ty);
1257 SetValue(&I, Dest, SF);
1260 #define IMPLEMENT_CAST_START \
1261 switch (DstTy->getTypeID()) {
1263 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
1264 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
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) \
1282 cerr << "Unhandled cast: " \
1283 << *SrcTy << " to " << *DstTy << "\n"; \
1288 #define IMPLEMENT_CAST_END \
1290 << "Unhandled dest type for cast instruction: " \
1291 << *DstTy << "\n"; \
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);
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;
1314 assert(0 && "Can't trunc a non-integer!");
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;
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 ));
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));
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));
1360 case Instruction::FPTrunc: // src double, dest float
1361 IMPLEMENT_CAST_START
1362 IMPLEMENT_CAST_CASE(Float , (float));
1365 case Instruction::FPExt: // src float, dest double
1366 IMPLEMENT_CAST_START
1367 IMPLEMENT_CAST_CASE(Double , (double));
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));
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));
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));
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));
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));
1421 case Instruction::IntToPtr: // src integral, dest pointer
1422 IMPLEMENT_CAST_START
1423 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
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));
1443 cerr << "Invalid cast opcode for cast instruction: " << opcode << "\n";
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);
1455 #define IMPLEMENT_VAARG(TY) \
1456 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1458 void Interpreter::visitVAArgInst(VAArgInst &I) {
1459 ExecutionContext &SF = ECStack.back();
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);
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);
1482 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1486 // Set the Value of this Instruction.
1487 SetValue(&I, Dest, SF);
1489 // Move the pointer to the next vararg.
1490 ++VAList.UIntPairVal.second;
1493 //===----------------------------------------------------------------------===//
1494 // Dispatch and Execution Code
1495 //===----------------------------------------------------------------------===//
1497 //===----------------------------------------------------------------------===//
1498 // callFunction - Execute the specified function...
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;
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);
1518 // Get pointers to first LLVM BB & Instruction in function.
1519 StackFrame.CurBB = F->begin();
1520 StackFrame.CurInst = StackFrame.CurBB->begin();
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!");
1527 // Handle non-varargs arguments...
1529 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
1530 SetValue(AI, ArgVals[i], StackFrame);
1532 // Handle varargs arguments...
1533 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
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
1542 // Track the number of dynamic instructions executed.
1545 DOUT << "About to interpret: " << I;
1546 visit(I); // Dispatch to one of the visit* methods...