Skip to content

Commit

Permalink
Merge pull request #261 from baszalmstra/fix/revert_bitcode
Browse files Browse the repository at this point in the history
fix: emit and link bitcode files instead of obj does not work on MacOS
  • Loading branch information
baszalmstra authored Aug 29, 2020
2 parents 742ddeb + 4e2fcc6 commit d9ebc18
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion crates/mun_codegen/src/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use inkwell::{
OptimizationLevel,
};

mod bitcode_file;
mod context;
mod error;
mod module_builder;
mod object_file;
pub mod symbols;

pub use context::CodeGenContext;
Expand Down
2 changes: 2 additions & 0 deletions crates/mun_codegen/src/code_gen/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ pub enum CodeGenerationError {
ModuleLinkerError(String),
#[error("error creating object file")]
CouldNotCreateObjectFile(io::Error),
#[error("error generating machine code")]
CodeGenerationError(String),
}
10 changes: 7 additions & 3 deletions crates/mun_codegen/src/code_gen/module_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::code_gen::bitcode_file::BitcodeFile;
use crate::code_gen::object_file::ObjectFile;
use crate::code_gen::{optimize_module, symbols, CodeGenContext, CodeGenerationError};
use crate::ir::file::gen_file_ir;
use crate::ir::file_group::gen_file_group_ir;
Expand Down Expand Up @@ -31,7 +31,7 @@ impl<'db, 'ink, 'ctx> ModuleBuilder<'db, 'ink, 'ctx> {
}

/// Constructs an object file.
pub fn build(self) -> Result<BitcodeFile, anyhow::Error> {
pub fn build(self) -> Result<ObjectFile, anyhow::Error> {
let group_ir = gen_file_group_ir(self.code_gen, self.file_id);
let file = gen_file_ir(self.code_gen, &group_ir, self.file_id);

Expand Down Expand Up @@ -84,6 +84,10 @@ impl<'db, 'ink, 'ctx> ModuleBuilder<'db, 'ink, 'ctx> {
// Debug print the IR
//println!("{}", assembly_module.print_to_string().to_string());

BitcodeFile::new(&self.code_gen.db.target(), &self.assembly_module)
ObjectFile::new(
&self.code_gen.db.target(),
&self.code_gen.target_machine,
&self.assembly_module,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
use crate::code_gen::CodeGenerationError;
use crate::linker;
use inkwell::targets::{FileType, TargetMachine};
use mun_target::spec;
use std::io::Write;
use std::path::Path;
use tempfile::NamedTempFile;

pub struct BitcodeFile {
pub struct ObjectFile {
target: spec::Target,
obj_file: NamedTempFile,
}

impl BitcodeFile {
impl ObjectFile {
/// Constructs a new object file from the specified `module` for `target`
pub fn new(
target: &spec::Target,
target_machine: &TargetMachine,
module: &inkwell::module::Module,
) -> Result<Self, anyhow::Error> {
// Write the bitcode to a memory buffer
let obj = module.write_bitcode_to_memory();
let obj = target_machine
.write_to_memory_buffer(&module, FileType::Object)
.map_err(|e| CodeGenerationError::CodeGenerationError(e.to_string()))?;

// Open a temporary file
let mut obj_file = tempfile::NamedTempFile::new()
.map_err(CodeGenerationError::CouldNotCreateObjectFile)?;

// Write the bitcode to the temporary file
obj_file
.write(obj.as_slice())
.map_err(CodeGenerationError::CouldNotCreateObjectFile)?;
Expand Down

0 comments on commit d9ebc18

Please sign in to comment.