Skip to content

Commit

Permalink
Hacky Target Machine Emit
Browse files Browse the repository at this point in the history
This doesn't work on macOS, but it might on Linux??

Still need to properly wrap the target machine.
  • Loading branch information
iwillspeak committed Oct 3, 2020
1 parent 00f88d2 commit 664e02a
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 296 deletions.
243 changes: 0 additions & 243 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,3 @@ tempfile = "3.1"
failure = "0.1"
libc = "0.2"
indexmap = "1.5"

[build-dependencies]
cc = "1.0"
bindgen = "0.53"
23 changes: 0 additions & 23 deletions build.rs

This file was deleted.

5 changes: 0 additions & 5 deletions llvmext/lib.cpp

This file was deleted.

1 change: 0 additions & 1 deletion llvmext/llvmext.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def run_spec(path):
run_cmd = subprocess.Popen(out, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = run_cmd.communicate()
if run_cmd.returncode != 0:
raise ExitCodeMismatchError("Expected successfull exit code")
raise ExitCodeMismatchError("Expected successfull exit code", run_cmd.returncode, output)
check_output(output[0].decode('utf-8'), expectations.expects)


Expand Down
2 changes: 1 addition & 1 deletion src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl Compilation {
if self.options.dump_ir {
module.dump();
}
module.write_to_file(temp_file.path(), kind)?;
module.write_to_file(&target, temp_file.path(), kind)?;

// Shell out to Clang to link the final assembly
let output = Command::new("clang")
Expand Down
1 change: 0 additions & 1 deletion src/low_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub mod pass_manager;
pub mod targets;
pub mod types;
pub mod value;
pub mod llvmext;

/// Prelude Module
///
Expand Down
6 changes: 0 additions & 6 deletions src/low_loader/llvmext.rs

This file was deleted.

55 changes: 44 additions & 11 deletions src/low_loader/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
use super::function::Function;
use super::llvm_sys::prelude::*;
use super::llvm_sys::target_machine;
use super::llvm_sys::{analysis, bit_writer, core};
use super::pass_manager::{OptLevel, OptSize, PassManagerBuilder};
use super::targets::Target;
use super::llvmext;

use std::ffi::{CStr, CString};
use std::path::Path;
Expand All @@ -28,12 +28,12 @@ pub struct Module {
/// Used when writing modules to disk.
#[derive(Debug, PartialEq)]
pub enum OutputFileKind {
/// LLVM IL files
LLVMIl,
/// LLVM Bitcode file
Bitcode,
/// Native executable object files
NativeObject
/// LLVM IL files
LLVMIl,
/// LLVM Bitcode file
Bitcode,
/// Native executable object files
NativeObject,
}

impl Module {
Expand Down Expand Up @@ -105,15 +105,48 @@ impl Module {
/// Write the Module to the Given File as LLVM IR or Bitcode
///
/// The kind of file written depends on `kind`.
pub fn write_to_file(&self, path: &Path, kind: OutputFileKind) -> Result<(), String> {
pub fn write_to_file(
&self,
target: &Target,
path: &Path,
kind: OutputFileKind,
) -> Result<(), String> {
let path = path.to_str().and_then(|s| CString::new(s).ok()).unwrap();

unsafe {
let mut message = ptr::null_mut();
let r = match kind {
OutputFileKind::LLVMIl => core::LLVMPrintModuleToFile(self.raw, path.as_ptr(), &mut message),
OutputFileKind::Bitcode => bit_writer::LLVMWriteBitcodeToFile(self.raw, path.as_ptr()),
OutputFileKind::NativeObject => llvmext::LLVMExtWriteModuleToObjectFile(),
OutputFileKind::LLVMIl => {
core::LLVMPrintModuleToFile(self.raw, path.as_ptr(), &mut message)
}
OutputFileKind::Bitcode => {
bit_writer::LLVMWriteBitcodeToFile(self.raw, path.as_ptr())
}
OutputFileKind::NativeObject => {
let trip = CString::new(target.triple()).unwrap();
// To emit code we need to do a few things:
// * Create an LLVM TargetMachine from our target.
// * Create a pass manager
// * Call targetMachine emit to file
let tm = target_machine::LLVMCreateTargetMachine(
target.as_llvm_target(),
trip.as_ptr(),
target_machine::LLVMGetHostCPUName(),
target_machine::LLVMGetHostCPUFeatures(),
target_machine::LLVMCodeGenOptLevel::LLVMCodeGenLevelDefault,
target_machine::LLVMRelocMode::LLVMRelocDefault,
target_machine::LLVMCodeModel::LLVMCodeModelJITDefault,
);
let r = target_machine::LLVMTargetMachineEmitToFile(
tm,
self.as_raw(),
path.as_ptr() as *mut _,
target_machine::LLVMCodeGenFileType::LLVMObjectFile,
&mut message,
);
target_machine::LLVMDisposeTargetMachine(tm);
r
}
};
if r == 0 {
Ok(())
Expand Down
5 changes: 5 additions & 0 deletions src/low_loader/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ impl Target {
})
}

/// Get the underlying LLVM target reference from the target
pub unsafe fn as_llvm_target(&self) -> LLVMTargetRef {
self.llvm_target
}

/// Get the Target name
///
/// Retrieves the logical name for this target
Expand Down

0 comments on commit 664e02a

Please sign in to comment.