From bbe70e37f97f6151d67ee668eb84c9d8b831a11f Mon Sep 17 00:00:00 2001 From: Dmitri Makarov Date: Sun, 17 Sep 2023 21:31:46 -0400 Subject: [PATCH] Refactor write to file of LLVM module moving it to Module --- language/solana/move-to-solana/src/lib.rs | 54 +------------------ .../move-to-solana/src/stackless/llvm.rs | 47 ++++++++++++++++ 2 files changed, 49 insertions(+), 52 deletions(-) diff --git a/language/solana/move-to-solana/src/lib.rs b/language/solana/move-to-solana/src/lib.rs index 8aad85d7aa..a63813f0ca 100644 --- a/language/solana/move-to-solana/src/lib.rs +++ b/language/solana/move-to-solana/src/lib.rs @@ -14,7 +14,6 @@ use crate::{ use anyhow::Context; use codespan_reporting::{diagnostic::Severity, term::termcolor::WriteColor}; -use llvm_sys::prelude::LLVMModuleRef; use log::{debug, Level}; use move_binary_format::{ binary_views::BinaryIndexedView, @@ -129,54 +128,6 @@ fn get_sbf_tools() -> anyhow::Result { Ok(sbf_tools) } -fn llvm_write_to_file( - module: LLVMModuleRef, - llvm_ir: bool, - output_file_name: &String, -) -> anyhow::Result<()> { - use crate::cstr::SafeCStr; - use llvm_sys::{ - bit_writer::LLVMWriteBitcodeToFD, - core::{LLVMDisposeMessage, LLVMPrintModuleToFile, LLVMPrintModuleToString}, - }; - use std::{ffi::CStr, fs::File, os::unix::io::AsRawFd, ptr}; - - unsafe { - if llvm_ir { - if output_file_name != "-" { - let mut err_string = ptr::null_mut(); - let filename = output_file_name.cstr(); - let res = LLVMPrintModuleToFile(module, filename, &mut err_string); - - if res != 0 { - assert!(!err_string.is_null()); - let msg = CStr::from_ptr(err_string).to_string_lossy(); - LLVMDisposeMessage(err_string); - anyhow::bail!("{}", msg); - } - } else { - let buf = LLVMPrintModuleToString(module); - assert!(!buf.is_null()); - let cstr = CStr::from_ptr(buf); - print!("{}", cstr.to_string_lossy()); - LLVMDisposeMessage(buf); - } - } else { - if output_file_name == "-" { - anyhow::bail!("Not writing bitcode to stdout"); - } - let bc_file = File::create(output_file_name)?; - let res = LLVMWriteBitcodeToFD(module, bc_file.as_raw_fd(), false as i32, true as i32); - - if res != 0 { - anyhow::bail!("Failed to write bitcode to file"); - } - } - } - - Ok(()) -} - fn get_runtime(out_path: &PathBuf, sbf_tools: &PlatformTools) -> anyhow::Result { debug!("building move-native runtime for sbf in {out_path:?}"); let archive_file = out_path @@ -449,7 +400,7 @@ fn compile(global_env: &GlobalEnv, options: &Options) -> anyhow::Result<()> { let module = global_env.get_module(mod_id); let modname = module.llvm_module_name(); debug!("Generating code for module {}", modname); - let mut llmod = global_cx.llvm_cx.create_module(&modname); + let llmod = global_cx.llvm_cx.create_module(&modname); let mod_cx = global_cx.create_module_context(mod_id, &llmod, options); mod_cx.translate(); @@ -465,8 +416,7 @@ fn compile(global_env: &GlobalEnv, options: &Options) -> anyhow::Result<()> { output_file = path.to_string_lossy().to_string(); } debug!("Output generated code to {}", output_file); - llvm_write_to_file(llmod.as_mut(), options.llvm_ir, &output_file)?; - drop(llmod); + llmod.write_to_file(options.llvm_ir, &output_file)?; } else { if options.compile { output_file = options.output.clone(); diff --git a/language/solana/move-to-solana/src/stackless/llvm.rs b/language/solana/move-to-solana/src/stackless/llvm.rs index a427272ab6..eab6b81a2d 100644 --- a/language/solana/move-to-solana/src/stackless/llvm.rs +++ b/language/solana/move-to-solana/src/stackless/llvm.rs @@ -377,6 +377,53 @@ impl Module { Global(v) } } + + pub fn write_to_file(self, llvm_ir: bool, filename: &str) -> anyhow::Result<()> { + use std::{fs::File, os::unix::io::AsRawFd}; + + unsafe { + if llvm_ir { + if filename != "-" { + let mut err_string = ptr::null_mut(); + let filename = CString::new(filename.to_string()).expect("interior nul byte"); + let mut filename = filename.into_bytes_with_nul(); + let filename: *mut u8 = filename.as_mut_ptr(); + let filename = filename as *mut libc::c_char; + let res = LLVMPrintModuleToFile(self.0, filename, &mut err_string); + + if res != 0 { + assert!(!err_string.is_null()); + let msg = CStr::from_ptr(err_string).to_string_lossy(); + LLVMDisposeMessage(err_string); + anyhow::bail!("{}", msg); + } + } else { + let buf = LLVMPrintModuleToString(self.0); + assert!(!buf.is_null()); + let cstr = CStr::from_ptr(buf); + print!("{}", cstr.to_string_lossy()); + LLVMDisposeMessage(buf); + } + } else { + if filename == "-" { + anyhow::bail!("Not writing bitcode to stdout"); + } + let bc_file = File::create(filename)?; + let res = llvm_sys::bit_writer::LLVMWriteBitcodeToFD( + self.0, + bc_file.as_raw_fd(), + false as i32, + true as i32, + ); + + if res != 0 { + anyhow::bail!("Failed to write bitcode to file"); + } + } + } + + Ok(()) + } } pub struct Builder(LLVMBuilderRef);