VALUE llvm_module_write_bitcode(VALUE self, VALUE file_name) { Check_Type(file_name, T_STRING); // Don't really know how to handle c++ streams well, // dumping all into string buffer and then saving std::ofstream file; file.open(StringValuePtr(file_name)); WriteBitcodeToFile(LLVM_MODULE(self), file); // Convert value into a string. return Qtrue; }
void villCompiler::save() { // create the LLVM bitcode (Intermediate Representation) file const char * bitcode_suffix = ".bc"; char * command; char * bitcode_filename = (char *) malloc( strlen(output_filename) + strlen(bitcode_suffix) + 1); strcpy( bitcode_filename, output_filename ); strcat( bitcode_filename, bitcode_suffix ); raw_ostream *ostream; std::string ErrInfo; //change for llvm 2.7 ostream = new raw_fd_ostream( bitcode_filename, ErrInfo, raw_fd_ostream::F_Binary ); // llvm 2.6 //ostream = new raw_fd_ostream( bitcode_filename, true, true, ErrInfo ); WriteBitcodeToFile(villModule, *ostream); delete ostream; // run the command to convert bitcode to native executable const char * loader_prefix = "llvm-ld -native -o "; command = (char *) malloc( strlen(loader_prefix) + strlen(bitcode_filename) + strlen(output_filename) + 2 ); strcpy( command, loader_prefix ); strcat( command, output_filename ); strcat( command, " " ); strcat( command, bitcode_filename ); // fprintf(stderr,"LLVM loader command: %s", command); int status = system(command); assert( status == 0 ); // run the command to delete the bitcode file const char * remove_prefix = "rm "; command = (char *) realloc( command, strlen(remove_prefix) + strlen(bitcode_filename) + 1 ); // + 1 for the \0 terminator strcpy( command, remove_prefix ); strcat( command, bitcode_filename ); status = system(command); assert( status == 0 ); // clean up free( command ); free( bitcode_filename ); }
// takes the running content that has collected in the shadow module and dump it to disk // this builds the object file portion of the sysimage files for fast startup static void jl_dump_shadow(char *fname, int jit_model, const char *sysimg_data, size_t sysimg_len, bool dump_as_bc) { #ifdef LLVM36 std::error_code err; StringRef fname_ref = StringRef(fname); raw_fd_ostream OS(fname_ref, err, sys::fs::F_None); #elif defined(LLVM35) std::string err; raw_fd_ostream OS(fname, err, sys::fs::F_None); #else std::string err; raw_fd_ostream OS(fname, err); #endif #ifdef LLVM37 // 3.7 simplified formatted output; just use the raw stream alone raw_fd_ostream& FOS(OS); #else formatted_raw_ostream FOS(OS); #endif // We don't want to use MCJIT's target machine because // it uses the large code model and we may potentially // want less optimizations there. Triple TheTriple = Triple(jl_TargetMachine->getTargetTriple()); #if defined(_OS_WINDOWS_) && defined(FORCE_ELF) #ifdef LLVM35 TheTriple.setObjectFormat(Triple::COFF); #else TheTriple.setEnvironment(Triple::UnknownEnvironment); #endif #elif defined(_OS_DARWIN_) && defined(FORCE_ELF) #ifdef LLVM35 TheTriple.setObjectFormat(Triple::MachO); #else TheTriple.setEnvironment(Triple::MachO); #endif #endif #ifdef LLVM35 std::unique_ptr<TargetMachine> #else OwningPtr<TargetMachine> #endif TM(jl_TargetMachine->getTarget().createTargetMachine( TheTriple.getTriple(), jl_TargetMachine->getTargetCPU(), jl_TargetMachine->getTargetFeatureString(), jl_TargetMachine->Options, #if defined(_OS_LINUX_) || defined(_OS_FREEBSD_) Reloc::PIC_, #else jit_model ? Reloc::PIC_ : Reloc::Default, #endif jit_model ? CodeModel::JITDefault : CodeModel::Default, CodeGenOpt::Aggressive // -O3 TODO: respect command -O0 flag? )); #ifdef LLVM38 legacy::PassManager PM; #else PassManager PM; #endif #ifndef LLVM37 PM.add(new TargetLibraryInfo(Triple(TM->getTargetTriple()))); #else PM.add(new TargetLibraryInfoWrapperPass(Triple(TM->getTargetTriple()))); #endif // set up optimization passes #ifdef LLVM37 // No DataLayout pass needed anymore. #elif defined(LLVM36) PM.add(new DataLayoutPass()); #elif defined(LLVM35) PM.add(new DataLayoutPass(*jl_ExecutionEngine->getDataLayout())); #else PM.add(new DataLayout(*jl_ExecutionEngine->getDataLayout())); #endif addOptimizationPasses(&PM); if (!dump_as_bc) { if (TM->addPassesToEmitFile(PM, FOS, TargetMachine::CGFT_ObjectFile, false)) { jl_error("Could not generate obj file for this target"); } } // now copy the module, since PM.run may modify it ValueToValueMapTy VMap; #ifdef LLVM38 Module *clone = CloneModule(shadow_output, VMap).release(); #else Module *clone = CloneModule(shadow_output, VMap); #endif #ifdef LLVM37 // Reset the target triple to make sure it matches the new target machine clone->setTargetTriple(TM->getTargetTriple().str()); #ifdef LLVM38 clone->setDataLayout(TM->createDataLayout()); #else clone->setDataLayout(TM->getDataLayout()->getStringRepresentation()); #endif #endif // add metadata information jl_gen_llvm_globaldata(clone, VMap, sysimg_data, sysimg_len); // do the actual work PM.run(*clone); if (dump_as_bc) WriteBitcodeToFile(clone, FOS); delete clone; }
void compile_llvm_module_to_llvm_bitcode(llvm::Module &module, Internal::LLVMOStream& out) { WriteBitcodeToFile(&module, out); }
static void writeModuleToFile(Module &TheModule, StringRef Filename) { std::error_code EC; raw_fd_ostream OS(Filename, EC, sys::fs::OpenFlags::F_None); error(EC, "error opening the file '" + Filename + "'"); WriteBitcodeToFile(&TheModule, OS, true, false); }
void compile_llvm_module_to_llvm_bitcode(llvm::Module *module, const std::string &filename) { llvm::raw_fd_ostream *file = new_raw_fd_ostream(filename); WriteBitcodeToFile(module, *file); delete file; }