Skip to content
This repository has been archived by the owner on Mar 20, 2024. It is now read-only.

Commit

Permalink
Refactor write to file of LLVM module moving it to Module
Browse files Browse the repository at this point in the history
  • Loading branch information
dmakarov committed Sep 18, 2023
1 parent 3d4777d commit bbe70e3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 52 deletions.
54 changes: 2 additions & 52 deletions language/solana/move-to-solana/src/lib.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -129,54 +128,6 @@ fn get_sbf_tools() -> anyhow::Result<PlatformTools> {
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<PathBuf> {
debug!("building move-native runtime for sbf in {out_path:?}");
let archive_file = out_path
Expand Down Expand Up @@ -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();

Expand All @@ -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();
Expand Down
47 changes: 47 additions & 0 deletions language/solana/move-to-solana/src/stackless/llvm.rs
Expand Up @@ -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);
Expand Down

0 comments on commit bbe70e3

Please sign in to comment.