__inline BOOL OffsetFromIndex ( LPSL lpslCur, WORD iPair, ULONG * lpulOff ) { BOOL fRet = FALSE; assert (lpslCur != NULL) assert (lpulOff != NULL) if (iPair < lpslCur->cLnOff) { if (GetTargetMachine() == mptia64) { //move slot number from bits 1:0 to bits 3:2 *lpulOff = (lpslCur->offset [ iPair ] & ~0xf) | ((lpslCur->offset [ iPair ] & 0xf) << 2) ; } else { *lpulOff = lpslCur->offset [ iPair ]; } fRet = TRUE; } return fRet; }
llvm::Value * Target::SizeOf(LLVM_TYPE_CONST llvm::Type *type, llvm::BasicBlock *insertAtEnd) { if (isa == Target::GENERIC && lGenericTypeLayoutIndeterminate(type)) { llvm::Value *index[1] = { LLVMInt32(1) }; LLVM_TYPE_CONST llvm::PointerType *ptrType = llvm::PointerType::get(type, 0); llvm::Value *voidPtr = llvm::ConstantPointerNull::get(ptrType); #if defined(LLVM_3_0) || defined(LLVM_3_0svn) || defined(LLVM_3_1svn) llvm::ArrayRef<llvm::Value *> arrayRef(&index[0], &index[1]); llvm::Instruction *gep = llvm::GetElementPtrInst::Create(voidPtr, arrayRef, "sizeof_gep", insertAtEnd); #else llvm::Instruction *gep = llvm::GetElementPtrInst::Create(voidPtr, &index[0], &index[1], "sizeof_gep", insertAtEnd); #endif if (is32Bit || g->opt.force32BitAddressing) return new llvm::PtrToIntInst(gep, LLVMTypes::Int32Type, "sizeof_int", insertAtEnd); else return new llvm::PtrToIntInst(gep, LLVMTypes::Int64Type, "sizeof_int", insertAtEnd); } const llvm::TargetData *td = GetTargetMachine()->getTargetData(); Assert(td != NULL); uint64_t byteSize = td->getTypeSizeInBits(type) / 8; if (is32Bit || g->opt.force32BitAddressing) return LLVMInt32((int32_t)byteSize); else return LLVMInt64(byteSize); }
llvm::Value * Target::SizeOf(LLVM_TYPE_CONST llvm::Type *type) { const llvm::TargetData *td = GetTargetMachine()->getTargetData(); Assert(td != NULL); uint64_t byteSize = td->getTypeSizeInBits(type) / 8; if (is32Bit || g->opt.force32BitAddressing) return LLVMInt32((int32_t)byteSize); else return LLVMInt64(byteSize); }
llvm::Value * Target::StructOffset(LLVM_TYPE_CONST llvm::Type *type, int element) { const llvm::TargetData *td = GetTargetMachine()->getTargetData(); Assert(td != NULL); LLVM_TYPE_CONST llvm::StructType *structType = llvm::dyn_cast<LLVM_TYPE_CONST llvm::StructType>(type); Assert(structType != NULL); const llvm::StructLayout *sl = td->getStructLayout(structType); Assert(sl != NULL); uint64_t offset = sl->getElementOffset(element); if (is32Bit || g->opt.force32BitAddressing) return LLVMInt32((int32_t)offset); else return LLVMInt64(offset); }
llvm::Value * Target::StructOffset(LLVM_TYPE_CONST llvm::Type *type, int element, llvm::BasicBlock *insertAtEnd) { if (isa == Target::GENERIC && lGenericTypeLayoutIndeterminate(type) == true) { llvm::Value *indices[2] = { LLVMInt32(0), LLVMInt32(element) }; LLVM_TYPE_CONST llvm::PointerType *ptrType = llvm::PointerType::get(type, 0); llvm::Value *voidPtr = llvm::ConstantPointerNull::get(ptrType); #if defined(LLVM_3_0) || defined(LLVM_3_0svn) || defined(LLVM_3_1svn) llvm::ArrayRef<llvm::Value *> arrayRef(&indices[0], &indices[2]); llvm::Instruction *gep = llvm::GetElementPtrInst::Create(voidPtr, arrayRef, "offset_gep", insertAtEnd); #else llvm::Instruction *gep = llvm::GetElementPtrInst::Create(voidPtr, &indices[0], &indices[2], "offset_gep", insertAtEnd); #endif if (is32Bit || g->opt.force32BitAddressing) return new llvm::PtrToIntInst(gep, LLVMTypes::Int32Type, "offset_int", insertAtEnd); else return new llvm::PtrToIntInst(gep, LLVMTypes::Int64Type, "offset_int", insertAtEnd); } const llvm::TargetData *td = GetTargetMachine()->getTargetData(); Assert(td != NULL); LLVM_TYPE_CONST llvm::StructType *structType = llvm::dyn_cast<LLVM_TYPE_CONST llvm::StructType>(type); Assert(structType != NULL); const llvm::StructLayout *sl = td->getStructLayout(structType); Assert(sl != NULL); uint64_t offset = sl->getElementOffset(element); if (is32Bit || g->opt.force32BitAddressing) return LLVMInt32((int32_t)offset); else return LLVMInt64(offset); }
int main(int argc, char **argv) { llvm::llvm_shutdown_obj shutdown; // calls llvm_shutdown() on exit llvm::cl::ParseCommandLineOptions(argc, argv, "llvm2bpl - LLVM bitcode to Boogie transformation\n"); llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); llvm::PrettyStackTraceProgram PSTP(argc, argv); llvm::EnableDebugBuffering = true; llvm::SMDiagnostic err; llvm::LLVMContext Context; InitializeAllTargets(); InitializeAllTargetMCs(); InitializeAllAsmPrinters(); InitializeAllAsmParsers(); std::unique_ptr<llvm::Module> module = llvm::parseIRFile(InputFilename, err, Context); if (!err.getMessage().empty()) check("Problem reading input bitcode/IR: " + err.getMessage().str()); auto &L = module.get()->getDataLayoutStr(); if (L.empty()) module.get()->setDataLayout(DefaultDataLayout); /////////////////////////////// // initialise and run passes // /////////////////////////////// llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry(); llvm::initializeAnalysis(Registry); llvm::legacy::PassManager pass_manager; pass_manager.add(llvm::createLowerSwitchPass()); //pass_manager.add(llvm::createCFGSimplificationPass()); pass_manager.add(llvm::createInternalizePass()); pass_manager.add(llvm::createPromoteMemoryToRegisterPass()); if (StaticUnroll) { pass_manager.add(llvm::createLoopSimplifyPass()); pass_manager.add(llvm::createLoopRotatePass()); //pass_manager.add(llvm::createIndVarSimplifyPass()); pass_manager.add(llvm::createLoopUnrollPass(32767)); } pass_manager.add(new llvm::StructRet()); pass_manager.add(new llvm::SimplifyEV()); pass_manager.add(new llvm::SimplifyIV()); pass_manager.add(new smack::ExtractContracts()); pass_manager.add(new smack::VerifierCodeMetadata()); pass_manager.add(llvm::createDeadCodeEliminationPass()); pass_manager.add(new smack::CodifyStaticInits()); if (!Modular) { pass_manager.add(new smack::RemoveDeadDefs()); } pass_manager.add(new llvm::MergeArrayGEP()); // pass_manager.add(new smack::SimplifyLibCalls()); pass_manager.add(new llvm::Devirtualize()); if (SplitStructs) pass_manager.add(new smack::SplitAggregateLoadStore()); if (smack::SmackOptions::MemorySafety) { pass_manager.add(new smack::MemorySafetyChecker()); } if (SignedIntegerOverflow) pass_manager.add(new smack::SignedIntegerOverflowChecker()); if(smack::SmackOptions::AddTiming){ Triple ModuleTriple(module->getTargetTriple()); assert (ModuleTriple.getArch() && "Module has no defined architecture: unable to add timing annotations"); const TargetOptions Options; /* = InitTargetOptionsFromCodeGenFlags();*/ std::string CPUStr = ""; /*getCPUStr();*/ std::string FeaturesStr = ""; /*getFeaturesStr();*/ TargetMachine *Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options); assert(Machine && "Module did not have a Target Machine: Cannot set up timing pass"); // Add an appropriate TargetLibraryInfo pass for the module's triple. TargetLibraryInfoImpl TLII(ModuleTriple); pass_manager.add(new TargetLibraryInfoWrapperPass(TLII)); // Add internal analysis passes from the target machine. pass_manager.add(createTargetTransformInfoWrapperPass(Machine->getTargetIRAnalysis())); pass_manager.add(new smack::AddTiming()); } std::vector<tool_output_file*> files; if (!FinalIrFilename.empty()) { std::error_code EC; auto F = new tool_output_file(FinalIrFilename.c_str(), EC, sys::fs::F_None); if (EC) check(EC.message()); F->keep(); files.push_back(F); pass_manager.add(llvm::createPrintModulePass(F->os())); } if (!OutputFilename.empty()) { std::error_code EC; auto F = new tool_output_file(OutputFilename.c_str(), EC, sys::fs::F_None); if (EC) check(EC.message()); F->keep(); files.push_back(F); pass_manager.add(new smack::SmackModuleGenerator()); pass_manager.add(new smack::BplFilePrinter(F->os())); } pass_manager.run(*module.get()); for (auto F : files) delete F; return 0; }