//main function int main(int argc, char* argv[]) { puts("Under construction!"); puts("Please pardon our mess!"); puts("Currently testing Integer objects."); Integer* intOne = createInteger("intOne", 3, false); Integer* constInt = createInteger("constInt", 4, true); printf("Before changing, the value of %s is %d.\n", intOne->getName(intOne), intOne->getValue(intOne)); printf("Before changing, the value of %s is %d.\n", constInt->getName(constInt), constInt->getValue(constInt)); intOne->setValue(intOne, 5); constInt->setValue(constInt, 6); printf("After changing, the value of %s is %d.\n", intOne->getName(intOne), intOne->getValue(intOne)); printf("After changing, the value of %s is %d.\n", constInt->getName(constInt), constInt->getValue(constInt)); destroyInteger(intOne); destroyInteger(constInt); return EXIT_NO_ERR; }
Domain * Constructors::createGetElementPtr(const llvm::ConstantExpr &value, const std::vector<const Domain*> &operands, const llvm::Value &place) const { std::vector<Domain*> offsets; std::vector<const Domain*>::const_iterator it = operands.begin() + 1, itend = operands.end(); for (; it != itend; ++it) { unsigned bitWidth = Integer::Utils::getBitWidth(**it); CANAL_ASSERT_MSG(bitWidth <= 64, "Cannot handle GetElementPtr offset" " with more than 64 bits."); if (bitWidth < 64) { Domain *offset = createInteger(64); offset->zext(**it); offsets.push_back(offset); } else offsets.push_back((*it)->clone()); } const llvm::PointerType &pointerType = checkedCast<const llvm::PointerType>(*value.getType()); // GetElementPtr on a Pointer const Pointer::Pointer *pointer = dynCast<Pointer::Pointer>(*operands.begin()); if (pointer) { return pointer->getElementPtr(offsets, pointerType, *this); } else { // GetElementPtr on anything except a pointer. For example, it is // called on arrays and structures. Domain *result = createPointer(pointerType); Pointer::Utils::addTarget(*result, Pointer::Target::Block, &place, *value.op_begin(), offsets, NULL); return result; } }
Domain * Constructors::create(const llvm::Type &type) const { CANAL_ASSERT_MSG(!type.isVoidTy(), "Cannot create value of type Void."); if (type.isIntegerTy()) { llvm::IntegerType &integerType = checkedCast<llvm::IntegerType>(type); return createInteger(integerType.getBitWidth()); } if (type.isFloatingPointTy()) { const llvm::fltSemantics &semantics = Float::Utils::getSemantics(type); return createFloat(semantics); } if (type.isPointerTy()) { const llvm::PointerType &pointerType = checkedCast<llvm::PointerType>(type); return createPointer(pointerType); } if (type.isArrayTy() || type.isVectorTy()) { const llvm::SequentialType &stype = checkedCast<llvm::SequentialType>(type); return createArray(stype); } if (type.isStructTy()) { const llvm::StructType &structType = checkedCast<llvm::StructType>(type); std::vector<Domain*> members; for (unsigned i = 0; i < structType.getNumElements(); i ++) members.push_back(create(*structType.getElementType(i))); return createStructure(structType, members); } CANAL_DIE_MSG("Unsupported llvm::Type::TypeID: " << type.getTypeID()); }
void Kontext::createBuiltins() { std::vector<llvm::Type*> printf_arg_types; printf_arg_types.push_back(llvm::Type::getInt8PtrTy(this->getContext())); //char* llvm::FunctionType* printf_type = llvm::FunctionType::get( llvm::Type::getInt32Ty(this->getContext()), printf_arg_types, true); llvm::Function *func = llvm::Function::Create( printf_type, llvm::Function::ExternalLinkage, llvm::Twine("printf"), this->_module ); func->setCallingConv(llvm::CallingConv::C); createInteger(); }
Domain * Constructors::create(const llvm::Constant &value, const llvm::Value &place, const State *state) const { if (llvm::isa<llvm::UndefValue>(value)) return create(*value.getType()); if (llvm::isa<llvm::ConstantInt>(value)) { const llvm::ConstantInt &intValue = checkedCast<llvm::ConstantInt>(value); const llvm::APInt &i = intValue.getValue(); return createInteger(i); } if (llvm::isa<llvm::ConstantPointerNull>(value)) { const llvm::ConstantPointerNull &nullValue = checkedCast<llvm::ConstantPointerNull>(value); const llvm::PointerType &pointerType = *nullValue.getType(); Domain *constPointer = createPointer(pointerType); constPointer->setZero(&place); return constPointer; } if (llvm::isa<llvm::ConstantExpr>(value)) { const llvm::ConstantExpr &exprValue = checkedCast<llvm::ConstantExpr>(value); return createConstantExpr(exprValue, place, state); } if (llvm::isa<llvm::ConstantFP>(value)) { const llvm::ConstantFP &fp = checkedCast<llvm::ConstantFP>(value); const llvm::APFloat &number = fp.getValueAPF(); return createFloat(number); } if (llvm::isa<llvm::ConstantStruct>(value)) { const llvm::ConstantStruct &structValue = checkedCast<llvm::ConstantStruct>(value); uint64_t elementCount = structValue.getType()->getNumElements(); std::vector<Domain*> members; for (uint64_t i = 0; i < elementCount; ++i) { members.push_back(create(*structValue.getOperand(i), *structValue.getOperand(i), state)); } return createStructure(*structValue.getType(), members); } if (llvm::isa<llvm::ConstantVector>(value)) { const llvm::ConstantVector &vectorValue = checkedCast<llvm::ConstantVector>(value); // VectorType::getNumElements returns unsigned int. unsigned elementCount = vectorValue.getType()->getNumElements(); std::vector<Domain*> values; for (unsigned i = 0; i < elementCount; ++i) { values.push_back(create(*vectorValue.getOperand(i), *vectorValue.getOperand(i), state)); } return createArray(*vectorValue.getType(), values); } if (llvm::isa<llvm::ConstantArray>(value)) { const llvm::ConstantArray &arrayValue = checkedCast<llvm::ConstantArray>(value); // ArrayType::getNumElements returns uint64_t. uint64_t elementCount = arrayValue.getType()->getNumElements(); std::vector<Domain*> values; for (uint64_t i = 0; i < elementCount; ++i) { values.push_back(create(*arrayValue.getOperand(i), *arrayValue.getOperand(i), state)); } return createArray(*arrayValue.getType(), values); } #if (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 1) || LLVM_VERSION_MAJOR > 3 // llvm::isa<llvm::ConstantDataSequential> returns false for an // llvm::ConstantDataArray/Vector instance at least on on LLVM // 3.1. if (llvm::isa<llvm::ConstantDataVector>(value) || llvm::isa<llvm::ConstantDataArray>(value)) { const llvm::ConstantDataSequential &sequentialValue = checkedCast<llvm::ConstantDataSequential>(value); unsigned elementCount = sequentialValue.getNumElements(); std::vector<Domain*> values; for (unsigned i = 0; i < elementCount; ++i) { values.push_back(create(*sequentialValue.getElementAsConstant(i), place, state)); } return createArray(*sequentialValue.getType(), values); } #endif if (llvm::isa<llvm::ConstantAggregateZero>(value)) { const llvm::Type *type = value.getType(); Domain *result = Constructors::create(*type); result->setZero(&place); return result; } if (llvm::isa<llvm::Function>(value)) { const llvm::Function &functionValue = checkedCast<llvm::Function>(value); Domain *constPointer; constPointer = createPointer(*llvm::PointerType::getUnqual( functionValue.getFunctionType())); Pointer::Utils::addTarget(*constPointer, Pointer::Target::Function, &place, &value, std::vector<Domain*>(), NULL); return constPointer; } CANAL_NOT_IMPLEMENTED(); }