ExecutorUtil.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "Executor.h"
00011
00012 #include "klee/Expr.h"
00013 #include "klee/Interpreter.h"
00014 #include "klee/Machine.h"
00015 #include "klee/Solver.h"
00016
00017 #include "klee/Internal/Module/KModule.h"
00018
00019 #include "llvm/Constants.h"
00020 #include "llvm/Function.h"
00021 #include "llvm/Instructions.h"
00022 #include "llvm/Module.h"
00023 #include "llvm/ModuleProvider.h"
00024 #include "llvm/Support/CallSite.h"
00025 #include "llvm/Support/GetElementPtrTypeIterator.h"
00026 #include "llvm/Support/Streams.h"
00027 #include "llvm/Target/TargetData.h"
00028 #include <iostream>
00029 #include <cassert>
00030
00031 using namespace klee;
00032 using namespace llvm;
00033
00034 namespace klee {
00035
00036 ref<Expr>
00037 Executor::evalConstantExpr(llvm::ConstantExpr *ce) {
00038 const llvm::Type *type = ce->getType();
00039
00040 ref<Expr> op1(0), op2(0), op3(0);
00041 int numOperands = ce->getNumOperands();
00042
00043 if (numOperands > 0) op1 = evalConstant(ce->getOperand(0));
00044 if (numOperands > 1) op2 = evalConstant(ce->getOperand(1));
00045 if (numOperands > 2) op3 = evalConstant(ce->getOperand(2));
00046
00047 switch (ce->getOpcode()) {
00048 default :
00049 assert(0 && "unknown ConstantExpr type");
00050
00051 case Instruction::Trunc: return ExtractExpr::createByteOff(op1,
00052 0,
00053 Expr::getWidthForLLVMType(type));
00054 case Instruction::ZExt: return ZExtExpr::create(op1,
00055 Expr::getWidthForLLVMType(type));
00056 case Instruction::SExt: return SExtExpr::create(op1,
00057 Expr::getWidthForLLVMType(type));
00058 case Instruction::Add: return AddExpr::create(op1, op2);
00059 case Instruction::Sub: return SubExpr::create(op1, op2);
00060 case Instruction::Mul: return MulExpr::create(op1, op2);
00061 case Instruction::SDiv: return SDivExpr::create(op1, op2);
00062 case Instruction::UDiv: return UDivExpr::create(op1, op2);
00063 case Instruction::SRem: return SRemExpr::create(op1, op2);
00064 case Instruction::URem: return URemExpr::create(op1, op2);
00065 case Instruction::And: return AndExpr::create(op1, op2);
00066 case Instruction::Or: return OrExpr::create(op1, op2);
00067 case Instruction::Xor: return XorExpr::create(op1, op2);
00068 case Instruction::Shl: return ShlExpr::create(op1, op2);
00069 case Instruction::LShr: return LShrExpr::create(op1, op2);
00070 case Instruction::AShr: return AShrExpr::create(op1, op2);
00071 case Instruction::BitCast: return op1;
00072
00073 case Instruction::IntToPtr: {
00074 return ZExtExpr::create(op1, Expr::getWidthForLLVMType(type));
00075 }
00076
00077 case Instruction::PtrToInt: {
00078 return ZExtExpr::create(op1, Expr::getWidthForLLVMType(type));
00079 }
00080
00081 case Instruction::GetElementPtr: {
00082 ref<Expr> base = op1;
00083
00084 for (gep_type_iterator ii = gep_type_begin(ce), ie = gep_type_end(ce);
00085 ii != ie; ++ii) {
00086 ref<Expr> addend = ConstantExpr::alloc(0, kMachinePointerType);
00087
00088 if (const StructType *st = dyn_cast<StructType>(*ii)) {
00089 const StructLayout *sl = kmodule->targetData->getStructLayout(st);
00090 const ConstantInt *ci = cast<ConstantInt>(ii.getOperand());
00091
00092 addend = Expr::createPointer(sl->getElementOffset((unsigned)
00093 ci->getZExtValue()));
00094 } else {
00095 const SequentialType *st = cast<SequentialType>(*ii);
00096 ref<Expr> index = evalConstant(cast<Constant>(ii.getOperand()));
00097 unsigned elementSize = kmodule->targetData->getTypeStoreSize(st->getElementType());
00098
00099 index = Expr::createCoerceToPointerType(index);
00100 addend = MulExpr::create(index,
00101 Expr::createPointer(elementSize));
00102 }
00103
00104 base = AddExpr::create(base, addend);
00105 }
00106
00107 return base;
00108 }
00109
00110 case Instruction::ICmp: {
00111 switch(ce->getPredicate()) {
00112 case ICmpInst::ICMP_EQ: return EqExpr::create(op1, op2);
00113 case ICmpInst::ICMP_NE: return NeExpr::create(op1, op2);
00114 case ICmpInst::ICMP_UGT: return UgtExpr::create(op1, op2);
00115 case ICmpInst::ICMP_UGE: return UgeExpr::create(op1, op2);
00116 case ICmpInst::ICMP_ULT: return UltExpr::create(op1, op2);
00117 case ICmpInst::ICMP_ULE: return UleExpr::create(op1, op2);
00118 case ICmpInst::ICMP_SGT: return SgtExpr::create(op1, op2);
00119 case ICmpInst::ICMP_SGE: return SgeExpr::create(op1, op2);
00120 case ICmpInst::ICMP_SLT: return SltExpr::create(op1, op2);
00121 case ICmpInst::ICMP_SLE: return SleExpr::create(op1, op2);
00122 default:
00123 assert(0 && "unhandled ICmp predicate");
00124 }
00125 }
00126
00127 case Instruction::Select: {
00128 return SelectExpr::create(op1, op2, op3);
00129 }
00130
00131 case Instruction::FDiv:
00132 case Instruction::FRem:
00133 case Instruction::FPTrunc:
00134 case Instruction::FPExt:
00135 case Instruction::UIToFP:
00136 case Instruction::SIToFP:
00137 case Instruction::FPToUI:
00138 case Instruction::FPToSI:
00139 case Instruction::FCmp:
00140 assert(0 && "floating point ConstantExprs unsupported");
00141 }
00142 }
00143
00144 }