nac3artiq: switch ld.lld to nac3ld for non-host target

issue-136
occheung 2022-05-30 17:12:05 +08:00
parent a96371145d
commit ac560ba985
3 changed files with 43 additions and 29 deletions

1
Cargo.lock generated
View File

@ -489,6 +489,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"inkwell", "inkwell",
"nac3core", "nac3core",
"nac3ld",
"nac3parser", "nac3parser",
"parking_lot 0.11.2", "parking_lot 0.11.2",
"pyo3", "pyo3",

View File

@ -14,6 +14,7 @@ parking_lot = "0.11"
tempfile = "3" tempfile = "3"
nac3parser = { path = "../nac3parser" } nac3parser = { path = "../nac3parser" }
nac3core = { path = "../nac3core" } nac3core = { path = "../nac3core" }
nac3ld = { path = "../nac3ld" }
[dependencies.inkwell] [dependencies.inkwell]
git = "https://github.com/TheDan64/inkwell.git" git = "https://github.com/TheDan64/inkwell.git"

View File

@ -1,5 +1,6 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::fs; use std::fs;
use std::io::Write;
use std::process::Command; use std::process::Command;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
@ -36,6 +37,8 @@ use nac3core::{
typecheck::{type_inferencer::PrimitiveStore, typedef::Type}, typecheck::{type_inferencer::PrimitiveStore, typedef::Type},
}; };
use nac3ld::Linker;
use tempfile::{self, TempDir}; use tempfile::{self, TempDir};
use crate::codegen::attributes_writeback; use crate::codegen::attributes_writeback;
@ -854,7 +857,7 @@ impl Nac3 {
Isa::RiscV32IMA => (TargetTriple::create("riscv32-unknown-linux"), "+a,+m".to_string()), Isa::RiscV32IMA => (TargetTriple::create("riscv32-unknown-linux"), "+a,+m".to_string()),
Isa::CortexA9 => ( Isa::CortexA9 => (
TargetTriple::create("armv7-unknown-linux-gnueabihf"), TargetTriple::create("armv7-unknown-linux-gnueabihf"),
"+dsp,+fp16,+neon,+vfp3".to_string(), "+dsp,+fp16,+neon,+vfp3,+long-calls".to_string(),
), ),
}; };
let target = let target =
@ -869,37 +872,46 @@ impl Nac3 {
CodeModel::Default, CodeModel::Default,
) )
.expect("couldn't create target machine"); .expect("couldn't create target machine");
target_machine
.write_to_file(&main, FileType::Object, &working_directory.join("module.o")) if isa == Isa::Host {
.expect("couldn't write module to file"); target_machine
.write_to_file(&main, FileType::Object, &working_directory.join("module.o"))
.expect("couldn't write module to file");
let linker_args = vec![
"-shared".to_string(),
"--eh-frame-hdr".to_string(),
"-x".to_string(),
"-o".to_string(),
filename.to_string(),
working_directory.join("module.o").to_string_lossy().to_string(),
];
let mut linker_args = vec![ #[cfg(not(windows))]
"-shared".to_string(), let lld_command = "ld.lld";
"--eh-frame-hdr".to_string(), #[cfg(windows)]
"-x".to_string(), let lld_command = "ld.lld.exe";
"-o".to_string(), if let Ok(linker_status) = Command::new(lld_command).args(linker_args).status() {
filename.to_string(), if !linker_status.success() {
working_directory.join("module.o").to_string_lossy().to_string(), return Err(CompileError::new_err("failed to start linker"));
]; }
if isa != Isa::Host { } else {
linker_args.push( return Err(CompileError::new_err(
"-T".to_string() "linker returned non-zero status code",
+ self.working_directory.path().join("kernel.ld").to_str().unwrap(), ));
);
}
#[cfg(not(windows))]
let lld_command = "ld.lld";
#[cfg(windows)]
let lld_command = "ld.lld.exe";
if let Ok(linker_status) = Command::new(lld_command).args(linker_args).status() {
if !linker_status.success() {
return Err(CompileError::new_err("failed to start linker"));
} }
} else { } else {
return Err(CompileError::new_err( let object_mem = target_machine
"linker returned non-zero status code", .write_to_memory_buffer(&main, FileType::Object)
)); .expect("couldn't write module to object file buffer");
if let Ok(dyn_lib) = Linker::ld(object_mem.as_slice()) {
if let Ok(mut file) = fs::File::create(filename) {
file.write_all(&dyn_lib).expect("couldn't write linked library to file");
} else {
return Err(CompileError::new_err("failed to create file"));
}
} else {
return Err(CompileError::new_err("linker failed to process object file"));
}
} }
Ok(()) Ok(())