Skip to content

Commit

Permalink
refactor(codegen): emit and link bitcode files instead of object files
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra committed Aug 23, 2020
1 parent 97f4ca8 commit 0cbdca0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 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
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 ObjectFile {
pub struct BitcodeFile {
target: spec::Target,
obj_file: NamedTempFile,
}

impl ObjectFile {
impl BitcodeFile {
/// 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> {
let obj = target_machine
.write_to_memory_buffer(&module, FileType::Object)
.map_err(|e| CodeGenerationError::CodeGenerationError(e.to_string()))?;
// Write the bitcode to a memory buffer
let obj = module.write_bitcode_to_memory();

// 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
2 changes: 0 additions & 2 deletions crates/mun_codegen/src/code_gen/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ pub enum CodeGenerationError {
ModuleLinkerError(String),
#[error("error creating object file")]
CouldNotCreateObjectFile(io::Error),
#[error("error generating machine code")]
CodeGenerationError(String),
}
10 changes: 3 additions & 7 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::object_file::ObjectFile;
use crate::code_gen::bitcode_file::BitcodeFile;
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<ObjectFile, anyhow::Error> {
pub fn build(self) -> Result<BitcodeFile, 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,10 +84,6 @@ impl<'db, 'ink, 'ctx> ModuleBuilder<'db, 'ink, 'ctx> {
// Debug print the IR
//println!("{}", assembly_module.print_to_string().to_string());

ObjectFile::new(
&self.code_gen.db.target(),
&self.code_gen.target_machine,
&self.assembly_module,
)
BitcodeFile::new(&self.code_gen.db.target(), &self.assembly_module)
}
}

0 comments on commit 0cbdca0

Please sign in to comment.