Replace ld.lld with nac3ld for non-host targets #292
|
@ -489,6 +489,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"inkwell",
|
||||
"nac3core",
|
||||
"nac3ld",
|
||||
"nac3parser",
|
||||
"parking_lot 0.11.2",
|
||||
"pyo3",
|
||||
|
@ -521,6 +522,13 @@ dependencies = [
|
|||
"test-case",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nac3ld"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nac3parser"
|
||||
version = "0.1.2"
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
[workspace]
|
||||
members = [
|
||||
"nac3ld",
|
||||
"nac3ast",
|
||||
"nac3parser",
|
||||
"nac3core",
|
||||
|
|
|
@ -14,6 +14,7 @@ parking_lot = "0.11"
|
|||
tempfile = "3"
|
||||
nac3parser = { path = "../nac3parser" }
|
||||
nac3core = { path = "../nac3core" }
|
||||
nac3ld = { path = "../nac3ld" }
|
||||
|
||||
[dependencies.inkwell]
|
||||
git = "https://github.com/TheDan64/inkwell.git"
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::process::Command;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use inkwell::{
|
||||
memory_buffer::MemoryBuffer,
|
||||
module::Linkage,
|
||||
module::{Linkage, Module},
|
||||
passes::{PassManager, PassManagerBuilder},
|
||||
targets::*,
|
||||
OptimizationLevel,
|
||||
|
@ -36,6 +37,8 @@ use nac3core::{
|
|||
typecheck::{type_inferencer::PrimitiveStore, typedef::Type},
|
||||
};
|
||||
|
||||
use nac3ld::Linker;
|
||||
|
||||
use tempfile::{self, TempDir};
|
||||
|
||||
use crate::codegen::attributes_writeback;
|
||||
|
@ -263,213 +266,16 @@ impl Nac3 {
|
|||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn add_exceptions(
|
||||
composer: &mut TopLevelComposer,
|
||||
builtin_def: &mut HashMap<StrRef, DefinitionId>,
|
||||
builtin_ty: &mut HashMap<StrRef, Type>,
|
||||
error_names: &[&str]
|
||||
) -> Vec<Type> {
|
||||
let mut types = Vec::new();
|
||||
// note: this is only for builtin exceptions, i.e. the exception name is "0:{exn}"
|
||||
for name in error_names {
|
||||
let def_id = composer.definition_ast_list.len();
|
||||
let (exception_fn, exception_class, exception_cons, exception_type) = get_exn_constructor(
|
||||
name,
|
||||
// class id
|
||||
def_id,
|
||||
// constructor id
|
||||
def_id + 1,
|
||||
&mut composer.unifier,
|
||||
&composer.primitives_ty
|
||||
);
|
||||
composer.definition_ast_list.push((Arc::new(RwLock::new(exception_class)), None));
|
||||
composer.definition_ast_list.push((Arc::new(RwLock::new(exception_fn)), None));
|
||||
builtin_ty.insert((*name).into(), exception_cons);
|
||||
builtin_def.insert((*name).into(), DefinitionId(def_id));
|
||||
types.push(exception_type);
|
||||
}
|
||||
types
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
impl Nac3 {
|
||||
#[new]
|
||||
fn new(isa: &str, py: Python) -> PyResult<Self> {
|
||||
let isa = match isa {
|
||||
"host" => Isa::Host,
|
||||
"rv32g" => Isa::RiscV32G,
|
||||
"rv32ima" => Isa::RiscV32IMA,
|
||||
"cortexa9" => Isa::CortexA9,
|
||||
_ => return Err(exceptions::PyValueError::new_err("invalid ISA")),
|
||||
};
|
||||
let time_fns: &(dyn TimeFns + Sync) = match isa {
|
||||
Isa::Host => &timeline::EXTERN_TIME_FNS,
|
||||
Isa::RiscV32G => &timeline::NOW_PINNING_TIME_FNS_64,
|
||||
Isa::RiscV32IMA => &timeline::NOW_PINNING_TIME_FNS,
|
||||
Isa::CortexA9 => &timeline::EXTERN_TIME_FNS,
|
||||
};
|
||||
let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0;
|
||||
let builtins = vec![
|
||||
(
|
||||
"now_mu".into(),
|
||||
FunSignature { args: vec![], ret: primitive.int64, vars: HashMap::new() },
|
||||
Arc::new(GenCall::new(Box::new(move |ctx, _, _, _, _| {
|
||||
Ok(Some(time_fns.emit_now_mu(ctx)))
|
||||
}))),
|
||||
),
|
||||
(
|
||||
"at_mu".into(),
|
||||
FunSignature {
|
||||
args: vec![FuncArg {
|
||||
name: "t".into(),
|
||||
ty: primitive.int64,
|
||||
default_value: None,
|
||||
}],
|
||||
ret: primitive.none,
|
||||
vars: HashMap::new(),
|
||||
},
|
||||
Arc::new(GenCall::new(Box::new(move |ctx, _, fun, args, generator| {
|
||||
let arg_ty = fun.0.args[0].ty;
|
||||
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty).unwrap();
|
||||
time_fns.emit_at_mu(ctx, arg);
|
||||
Ok(None)
|
||||
}))),
|
||||
),
|
||||
(
|
||||
"delay_mu".into(),
|
||||
FunSignature {
|
||||
args: vec![FuncArg {
|
||||
name: "dt".into(),
|
||||
ty: primitive.int64,
|
||||
default_value: None,
|
||||
}],
|
||||
ret: primitive.none,
|
||||
vars: HashMap::new(),
|
||||
},
|
||||
Arc::new(GenCall::new(Box::new(move |ctx, _, fun, args, generator| {
|
||||
let arg_ty = fun.0.args[0].ty;
|
||||
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty).unwrap();
|
||||
time_fns.emit_delay_mu(ctx, arg);
|
||||
Ok(None)
|
||||
}))),
|
||||
),
|
||||
];
|
||||
|
||||
let builtins_mod = PyModule::import(py, "builtins").unwrap();
|
||||
let id_fn = builtins_mod.getattr("id").unwrap();
|
||||
let numpy_mod = PyModule::import(py, "numpy").unwrap();
|
||||
let typing_mod = PyModule::import(py, "typing").unwrap();
|
||||
let types_mod = PyModule::import(py, "types").unwrap();
|
||||
|
||||
let get_id = |x| id_fn.call1((x,)).unwrap().extract().unwrap();
|
||||
let get_attr_id = |obj: &PyModule, attr| id_fn.call1((obj.getattr(attr).unwrap(),))
|
||||
.unwrap().extract().unwrap();
|
||||
let primitive_ids = PrimitivePythonId {
|
||||
virtual_id: get_id(
|
||||
builtins_mod
|
||||
.getattr("globals")
|
||||
.unwrap()
|
||||
.call0()
|
||||
.unwrap()
|
||||
.get_item("virtual")
|
||||
.unwrap(
|
||||
)),
|
||||
generic_alias: (
|
||||
get_attr_id(typing_mod, "_GenericAlias"),
|
||||
get_attr_id(types_mod, "GenericAlias"),
|
||||
),
|
||||
none: id_fn
|
||||
.call1((builtins_mod
|
||||
.getattr("globals")
|
||||
.unwrap()
|
||||
.call0()
|
||||
.unwrap()
|
||||
.get_item("none")
|
||||
.unwrap(),))
|
||||
.unwrap()
|
||||
.extract()
|
||||
.unwrap(),
|
||||
typevar: get_attr_id(typing_mod, "TypeVar"),
|
||||
int: get_attr_id(builtins_mod, "int"),
|
||||
int32: get_attr_id(numpy_mod, "int32"),
|
||||
int64: get_attr_id(numpy_mod, "int64"),
|
||||
uint32: get_attr_id(numpy_mod, "uint32"),
|
||||
uint64: get_attr_id(numpy_mod, "uint64"),
|
||||
bool: get_attr_id(builtins_mod, "bool"),
|
||||
float: get_attr_id(builtins_mod, "float"),
|
||||
float64: get_attr_id(numpy_mod, "float64"),
|
||||
list: get_attr_id(builtins_mod, "list"),
|
||||
tuple: get_attr_id(builtins_mod, "tuple"),
|
||||
exception: get_attr_id(builtins_mod, "Exception"),
|
||||
option: id_fn
|
||||
.call1((builtins_mod
|
||||
.getattr("globals")
|
||||
.unwrap()
|
||||
.call0()
|
||||
.unwrap()
|
||||
.get_item("Option")
|
||||
.unwrap(),))
|
||||
.unwrap()
|
||||
.extract()
|
||||
.unwrap(),
|
||||
};
|
||||
|
||||
let working_directory = tempfile::Builder::new().prefix("nac3-").tempdir().unwrap();
|
||||
fs::write(working_directory.path().join("kernel.ld"), include_bytes!("kernel.ld")).unwrap();
|
||||
|
||||
Ok(Nac3 {
|
||||
isa,
|
||||
time_fns,
|
||||
primitive,
|
||||
builtins,
|
||||
primitive_ids,
|
||||
top_levels: Default::default(),
|
||||
pyid_to_def: Default::default(),
|
||||
working_directory,
|
||||
string_store: Default::default(),
|
||||
exception_ids: Default::default(),
|
||||
deferred_eval_store: DeferredEvaluationStore::new(),
|
||||
})
|
||||
}
|
||||
|
||||
fn analyze(&mut self, functions: &PySet, classes: &PySet) -> PyResult<()> {
|
||||
let (modules, class_ids) =
|
||||
Python::with_gil(|py| -> PyResult<(HashMap<u64, PyObject>, HashSet<u64>)> {
|
||||
let mut modules: HashMap<u64, PyObject> = HashMap::new();
|
||||
let mut class_ids: HashSet<u64> = HashSet::new();
|
||||
|
||||
let id_fn = PyModule::import(py, "builtins")?.getattr("id")?;
|
||||
let getmodule_fn = PyModule::import(py, "inspect")?.getattr("getmodule")?;
|
||||
|
||||
for function in functions.iter() {
|
||||
let module = getmodule_fn.call1((function,))?.extract()?;
|
||||
modules.insert(id_fn.call1((&module,))?.extract()?, module);
|
||||
}
|
||||
for class in classes.iter() {
|
||||
let module = getmodule_fn.call1((class,))?.extract()?;
|
||||
modules.insert(id_fn.call1((&module,))?.extract()?, module);
|
||||
class_ids.insert(id_fn.call1((class,))?.extract()?);
|
||||
}
|
||||
Ok((modules, class_ids))
|
||||
})?;
|
||||
|
||||
for module in modules.into_values() {
|
||||
self.register_module(module, &class_ids)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compile_method_to_file(
|
||||
&mut self,
|
||||
fn compile_method<T>(
|
||||
&self,
|
||||
obj: &PyAny,
|
||||
method_name: &str,
|
||||
args: Vec<&PyAny>,
|
||||
filename: &str,
|
||||
embedding_map: &PyAny,
|
||||
py: Python,
|
||||
) -> PyResult<()> {
|
||||
link_fn: &dyn Fn(&Module) -> PyResult<T>,
|
||||
) -> PyResult<T> {
|
||||
let (mut composer, mut builtins_def, mut builtins_ty) = TopLevelComposer::new(
|
||||
self.builtins.clone(),
|
||||
ComposerConfig { kernel_ann: Some("Kernel"), kernel_invariant_ann: "KernelInvariant" },
|
||||
|
@ -756,8 +562,6 @@ impl Nac3 {
|
|||
calls: Arc::new(Default::default()),
|
||||
id: 0,
|
||||
};
|
||||
let isa = self.isa;
|
||||
let working_directory = self.working_directory.path().to_owned();
|
||||
|
||||
let membuffers: Arc<Mutex<Vec<Vec<u8>>>> = Default::default();
|
||||
|
||||
|
@ -843,7 +647,13 @@ impl Nac3 {
|
|||
builder.populate_module_pass_manager(&passes);
|
||||
passes.run_on(&main);
|
||||
|
||||
let (triple, features) = match isa {
|
||||
link_fn(&main)
|
||||
}
|
||||
|
||||
fn get_llvm_target_machine(
|
||||
&self,
|
||||
) -> TargetMachine {
|
||||
let (triple, features) = match self.isa {
|
||||
Isa::Host => (
|
||||
TargetMachine::get_default_triple(),
|
||||
TargetMachine::get_host_cpu_features().to_string(),
|
||||
|
@ -854,12 +664,12 @@ impl Nac3 {
|
|||
Isa::RiscV32IMA => (TargetTriple::create("riscv32-unknown-linux"), "+a,+m".to_string()),
|
||||
Isa::CortexA9 => (
|
||||
TargetTriple::create("armv7-unknown-linux-gnueabihf"),
|
||||
"+dsp,+fp16,+neon,+vfp3".to_string(),
|
||||
"+dsp,+fp16,+neon,+vfp3,+long-calls".to_string(),
|
||||
),
|
||||
};
|
||||
let target =
|
||||
Target::from_triple(&triple).expect("couldn't create target from target triple");
|
||||
let target_machine = target
|
||||
target
|
||||
.create_target_machine(
|
||||
&triple,
|
||||
"",
|
||||
|
@ -868,43 +678,282 @@ impl Nac3 {
|
|||
RelocMode::PIC,
|
||||
CodeModel::Default,
|
||||
)
|
||||
.expect("couldn't create target machine");
|
||||
target_machine
|
||||
.write_to_file(&main, FileType::Object, &working_directory.join("module.o"))
|
||||
.expect("couldn't write module to file");
|
||||
.expect("couldn't create target machine")
|
||||
}
|
||||
}
|
||||
|
||||
let mut 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(),
|
||||
fn link_with_lld(
|
||||
elf_filename: String,
|
||||
obj_filename: String,
|
||||
) -> PyResult<()>{
|
||||
let linker_args = vec![
|
||||
"-shared".to_string(),
|
||||
"--eh-frame-hdr".to_string(),
|
||||
"-x".to_string(),
|
||||
"-o".to_string(),
|
||||
elf_filename,
|
||||
obj_filename,
|
||||
];
|
||||
|
||||
#[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 {
|
||||
return Err(CompileError::new_err(
|
||||
"linker returned non-zero status code",
|
||||
));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_exceptions(
|
||||
composer: &mut TopLevelComposer,
|
||||
builtin_def: &mut HashMap<StrRef, DefinitionId>,
|
||||
builtin_ty: &mut HashMap<StrRef, Type>,
|
||||
error_names: &[&str]
|
||||
) -> Vec<Type> {
|
||||
let mut types = Vec::new();
|
||||
// note: this is only for builtin exceptions, i.e. the exception name is "0:{exn}"
|
||||
for name in error_names {
|
||||
let def_id = composer.definition_ast_list.len();
|
||||
let (exception_fn, exception_class, exception_cons, exception_type) = get_exn_constructor(
|
||||
name,
|
||||
// class id
|
||||
def_id,
|
||||
// constructor id
|
||||
def_id + 1,
|
||||
&mut composer.unifier,
|
||||
&composer.primitives_ty
|
||||
);
|
||||
composer.definition_ast_list.push((Arc::new(RwLock::new(exception_class)), None));
|
||||
composer.definition_ast_list.push((Arc::new(RwLock::new(exception_fn)), None));
|
||||
builtin_ty.insert((*name).into(), exception_cons);
|
||||
builtin_def.insert((*name).into(), DefinitionId(def_id));
|
||||
types.push(exception_type);
|
||||
}
|
||||
types
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
impl Nac3 {
|
||||
#[new]
|
||||
fn new(isa: &str, py: Python) -> PyResult<Self> {
|
||||
let isa = match isa {
|
||||
"host" => Isa::Host,
|
||||
"rv32g" => Isa::RiscV32G,
|
||||
"rv32ima" => Isa::RiscV32IMA,
|
||||
"cortexa9" => Isa::CortexA9,
|
||||
_ => return Err(exceptions::PyValueError::new_err("invalid ISA")),
|
||||
};
|
||||
let time_fns: &(dyn TimeFns + Sync) = match isa {
|
||||
Isa::Host => &timeline::EXTERN_TIME_FNS,
|
||||
Isa::RiscV32G => &timeline::NOW_PINNING_TIME_FNS_64,
|
||||
Isa::RiscV32IMA => &timeline::NOW_PINNING_TIME_FNS,
|
||||
Isa::CortexA9 => &timeline::EXTERN_TIME_FNS,
|
||||
};
|
||||
let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0;
|
||||
let builtins = vec![
|
||||
(
|
||||
"now_mu".into(),
|
||||
FunSignature { args: vec![], ret: primitive.int64, vars: HashMap::new() },
|
||||
Arc::new(GenCall::new(Box::new(move |ctx, _, _, _, _| {
|
||||
Ok(Some(time_fns.emit_now_mu(ctx)))
|
||||
}))),
|
||||
),
|
||||
(
|
||||
"at_mu".into(),
|
||||
FunSignature {
|
||||
args: vec![FuncArg {
|
||||
name: "t".into(),
|
||||
ty: primitive.int64,
|
||||
default_value: None,
|
||||
}],
|
||||
ret: primitive.none,
|
||||
vars: HashMap::new(),
|
||||
},
|
||||
Arc::new(GenCall::new(Box::new(move |ctx, _, fun, args, generator| {
|
||||
let arg_ty = fun.0.args[0].ty;
|
||||
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty).unwrap();
|
||||
time_fns.emit_at_mu(ctx, arg);
|
||||
Ok(None)
|
||||
}))),
|
||||
),
|
||||
(
|
||||
"delay_mu".into(),
|
||||
FunSignature {
|
||||
args: vec![FuncArg {
|
||||
name: "dt".into(),
|
||||
ty: primitive.int64,
|
||||
default_value: None,
|
||||
}],
|
||||
ret: primitive.none,
|
||||
vars: HashMap::new(),
|
||||
},
|
||||
Arc::new(GenCall::new(Box::new(move |ctx, _, fun, args, generator| {
|
||||
let arg_ty = fun.0.args[0].ty;
|
||||
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty).unwrap();
|
||||
time_fns.emit_delay_mu(ctx, arg);
|
||||
Ok(None)
|
||||
}))),
|
||||
),
|
||||
];
|
||||
if isa != Isa::Host {
|
||||
linker_args.push(
|
||||
"-T".to_string()
|
||||
+ 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 {
|
||||
return Err(CompileError::new_err(
|
||||
"linker returned non-zero status code",
|
||||
));
|
||||
}
|
||||
let builtins_mod = PyModule::import(py, "builtins").unwrap();
|
||||
let id_fn = builtins_mod.getattr("id").unwrap();
|
||||
let numpy_mod = PyModule::import(py, "numpy").unwrap();
|
||||
let typing_mod = PyModule::import(py, "typing").unwrap();
|
||||
let types_mod = PyModule::import(py, "types").unwrap();
|
||||
|
||||
let get_id = |x| id_fn.call1((x,)).unwrap().extract().unwrap();
|
||||
let get_attr_id = |obj: &PyModule, attr| id_fn.call1((obj.getattr(attr).unwrap(),))
|
||||
.unwrap().extract().unwrap();
|
||||
let primitive_ids = PrimitivePythonId {
|
||||
virtual_id: get_id(
|
||||
builtins_mod
|
||||
.getattr("globals")
|
||||
.unwrap()
|
||||
.call0()
|
||||
.unwrap()
|
||||
.get_item("virtual")
|
||||
.unwrap(
|
||||
)),
|
||||
generic_alias: (
|
||||
get_attr_id(typing_mod, "_GenericAlias"),
|
||||
get_attr_id(types_mod, "GenericAlias"),
|
||||
),
|
||||
none: id_fn
|
||||
.call1((builtins_mod
|
||||
.getattr("globals")
|
||||
.unwrap()
|
||||
.call0()
|
||||
.unwrap()
|
||||
.get_item("none")
|
||||
.unwrap(),))
|
||||
.unwrap()
|
||||
.extract()
|
||||
.unwrap(),
|
||||
typevar: get_attr_id(typing_mod, "TypeVar"),
|
||||
int: get_attr_id(builtins_mod, "int"),
|
||||
int32: get_attr_id(numpy_mod, "int32"),
|
||||
int64: get_attr_id(numpy_mod, "int64"),
|
||||
uint32: get_attr_id(numpy_mod, "uint32"),
|
||||
uint64: get_attr_id(numpy_mod, "uint64"),
|
||||
bool: get_attr_id(builtins_mod, "bool"),
|
||||
float: get_attr_id(builtins_mod, "float"),
|
||||
float64: get_attr_id(numpy_mod, "float64"),
|
||||
list: get_attr_id(builtins_mod, "list"),
|
||||
tuple: get_attr_id(builtins_mod, "tuple"),
|
||||
exception: get_attr_id(builtins_mod, "Exception"),
|
||||
option: id_fn
|
||||
.call1((builtins_mod
|
||||
.getattr("globals")
|
||||
.unwrap()
|
||||
.call0()
|
||||
.unwrap()
|
||||
.get_item("Option")
|
||||
.unwrap(),))
|
||||
.unwrap()
|
||||
.extract()
|
||||
.unwrap(),
|
||||
};
|
||||
|
||||
let working_directory = tempfile::Builder::new().prefix("nac3-").tempdir().unwrap();
|
||||
fs::write(working_directory.path().join("kernel.ld"), include_bytes!("kernel.ld")).unwrap();
|
||||
|
||||
Ok(Nac3 {
|
||||
isa,
|
||||
time_fns,
|
||||
primitive,
|
||||
builtins,
|
||||
primitive_ids,
|
||||
top_levels: Default::default(),
|
||||
pyid_to_def: Default::default(),
|
||||
working_directory,
|
||||
string_store: Default::default(),
|
||||
exception_ids: Default::default(),
|
||||
deferred_eval_store: DeferredEvaluationStore::new(),
|
||||
})
|
||||
}
|
||||
|
||||
fn analyze(&mut self, functions: &PySet, classes: &PySet) -> PyResult<()> {
|
||||
let (modules, class_ids) =
|
||||
Python::with_gil(|py| -> PyResult<(HashMap<u64, PyObject>, HashSet<u64>)> {
|
||||
let mut modules: HashMap<u64, PyObject> = HashMap::new();
|
||||
let mut class_ids: HashSet<u64> = HashSet::new();
|
||||
|
||||
let id_fn = PyModule::import(py, "builtins")?.getattr("id")?;
|
||||
let getmodule_fn = PyModule::import(py, "inspect")?.getattr("getmodule")?;
|
||||
|
||||
for function in functions.iter() {
|
||||
let module = getmodule_fn.call1((function,))?.extract()?;
|
||||
modules.insert(id_fn.call1((&module,))?.extract()?, module);
|
||||
}
|
||||
for class in classes.iter() {
|
||||
let module = getmodule_fn.call1((class,))?.extract()?;
|
||||
modules.insert(id_fn.call1((&module,))?.extract()?, module);
|
||||
class_ids.insert(id_fn.call1((class,))?.extract()?);
|
||||
}
|
||||
Ok((modules, class_ids))
|
||||
})?;
|
||||
|
||||
for module in modules.into_values() {
|
||||
self.register_module(module, &class_ids)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compile_method_to_file(
|
||||
&mut self,
|
||||
obj: &PyAny,
|
||||
method_name: &str,
|
||||
args: Vec<&PyAny>,
|
||||
filename: &str,
|
||||
embedding_map: &PyAny,
|
||||
py: Python,
|
||||
) -> PyResult<()> {
|
||||
let target_machine = self.get_llvm_target_machine();
|
||||
|
||||
if self.isa == Isa::Host {
|
||||
let link_fn = |module: &Module| {
|
||||
let working_directory = self.working_directory.path().to_owned();
|
||||
target_machine
|
||||
.write_to_file(module, FileType::Object, &working_directory.join("module.o"))
|
||||
.expect("couldn't write module to file");
|
||||
link_with_lld(
|
||||
filename.to_string(),
|
||||
working_directory.join("module.o").to_string_lossy().to_string()
|
||||
)?;
|
||||
Ok(())
|
||||
};
|
||||
|
||||
self.compile_method(obj, method_name, args, embedding_map, py, &link_fn)
|
||||
} else {
|
||||
let link_fn = |module: &Module| {
|
||||
let object_mem = target_machine
|
||||
.write_to_memory_buffer(module, 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");
|
||||
Ok(())
|
||||
} else {
|
||||
Err(CompileError::new_err("failed to create file"))
|
||||
}
|
||||
} else {
|
||||
Err(CompileError::new_err("linker failed to process object file"))
|
||||
}
|
||||
};
|
||||
|
||||
self.compile_method(obj, method_name, args, embedding_map, py, &link_fn)
|
||||
}
|
||||
}
|
||||
|
||||
fn compile_method_to_mem(
|
||||
&mut self,
|
||||
obj: &PyAny,
|
||||
|
@ -913,10 +962,40 @@ impl Nac3 {
|
|||
embedding_map: &PyAny,
|
||||
py: Python,
|
||||
) -> PyResult<PyObject> {
|
||||
let filename_path = self.working_directory.path().join("module.elf");
|
||||
let filename = filename_path.to_str().unwrap();
|
||||
self.compile_method_to_file(obj, method_name, args, filename, embedding_map, py)?;
|
||||
Ok(PyBytes::new(py, &fs::read(filename).unwrap()).into())
|
||||
let target_machine = self.get_llvm_target_machine();
|
||||
|
||||
if self.isa == Isa::Host {
|
||||
let link_fn = |module: &Module| {
|
||||
let working_directory = self.working_directory.path().to_owned();
|
||||
target_machine
|
||||
.write_to_file(&module, FileType::Object, &working_directory.join("module.o"))
|
||||
.expect("couldn't write module to file");
|
||||
|
||||
let filename_path = self.working_directory.path().join("module.elf");
|
||||
let filename = filename_path.to_str().unwrap();
|
||||
link_with_lld(
|
||||
filename.to_string(),
|
||||
working_directory.join("module.o").to_string_lossy().to_string()
|
||||
)?;
|
||||
|
||||
Ok(PyBytes::new(py, &fs::read(filename).unwrap()).into())
|
||||
};
|
||||
|
||||
self.compile_method(obj, method_name, args, embedding_map, py, &link_fn)
|
||||
} else {
|
||||
let link_fn = |module: &Module| {
|
||||
let object_mem = target_machine
|
||||
.write_to_memory_buffer(&module, FileType::Object)
|
||||
.expect("couldn't write module to object file buffer");
|
||||
if let Ok(dyn_lib) = Linker::ld(object_mem.as_slice()) {
|
||||
Ok(PyBytes::new(py, &dyn_lib).into())
|
||||
} else {
|
||||
Err(CompileError::new_err("linker failed to process object file"))
|
||||
}
|
||||
};
|
||||
|
||||
self.compile_method(obj, method_name, args, embedding_map, py, &link_fn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "nac3ld"
|
||||
version = "0.1.0"
|
||||
authors = ["M-Labs"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
byteorder = { version = "1.0", default-features = false }
|
|
@ -0,0 +1,392 @@
|
|||
#![allow(non_camel_case_types, non_upper_case_globals)]
|
||||
|
||||
use std::mem;
|
||||
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
|
||||
pub const DW_EH_PE_omit: u8 = 0xFF;
|
||||
pub const DW_EH_PE_absptr: u8 = 0x00;
|
||||
|
||||
pub const DW_EH_PE_uleb128: u8 = 0x01;
|
||||
pub const DW_EH_PE_udata2: u8 = 0x02;
|
||||
pub const DW_EH_PE_udata4: u8 = 0x03;
|
||||
pub const DW_EH_PE_udata8: u8 = 0x04;
|
||||
pub const DW_EH_PE_sleb128: u8 = 0x09;
|
||||
pub const DW_EH_PE_sdata2: u8 = 0x0A;
|
||||
pub const DW_EH_PE_sdata4: u8 = 0x0B;
|
||||
pub const DW_EH_PE_sdata8: u8 = 0x0C;
|
||||
|
||||
pub const DW_EH_PE_pcrel: u8 = 0x10;
|
||||
pub const DW_EH_PE_textrel: u8 = 0x20;
|
||||
pub const DW_EH_PE_datarel: u8 = 0x30;
|
||||
pub const DW_EH_PE_funcrel: u8 = 0x40;
|
||||
pub const DW_EH_PE_aligned: u8 = 0x50;
|
||||
|
||||
pub const DW_EH_PE_indirect: u8 = 0x80;
|
||||
|
||||
pub struct DwarfReader<'a> {
|
||||
pub slice: &'a [u8],
|
||||
pub virt_addr: u32,
|
||||
}
|
||||
|
||||
impl<'a> DwarfReader<'a> {
|
||||
pub fn new(slice: &[u8], virt_addr: u32) -> DwarfReader {
|
||||
DwarfReader { slice, virt_addr }
|
||||
}
|
||||
|
||||
pub fn offset(&mut self, offset: i32) {
|
||||
self.slice = &self.slice[offset as usize..];
|
||||
self.virt_addr = self.virt_addr.wrapping_add(offset as u32);
|
||||
}
|
||||
|
||||
// ULEB128 and SLEB128 encodings are defined in Section 7.6 - "Variable
|
||||
// Length Data".
|
||||
pub fn read_uleb128(&mut self) -> u64 {
|
||||
let mut shift: usize = 0;
|
||||
let mut result: u64 = 0;
|
||||
let mut byte: u8;
|
||||
loop {
|
||||
byte = self.read_u8();
|
||||
result |= ((byte & 0x7F) as u64) << shift;
|
||||
shift += 7;
|
||||
if byte & 0x80 == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
pub fn read_sleb128(&mut self) -> i64 {
|
||||
let mut shift: u32 = 0;
|
||||
let mut result: u64 = 0;
|
||||
let mut byte: u8;
|
||||
loop {
|
||||
byte = self.read_u8();
|
||||
result |= ((byte & 0x7F) as u64) << shift;
|
||||
shift += 7;
|
||||
if byte & 0x80 == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// sign-extend
|
||||
if shift < u64::BITS && (byte & 0x40) != 0 {
|
||||
result |= (!0 as u64) << shift;
|
||||
}
|
||||
result as i64
|
||||
}
|
||||
|
||||
pub fn read_u8(&mut self) -> u8 {
|
||||
let val = self.slice[0];
|
||||
self.slice = &self.slice[1..];
|
||||
val
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_read_fn {
|
||||
( $($type: ty, $byteorder_fn: ident);* ) => {
|
||||
impl<'a> DwarfReader<'a> {
|
||||
$(
|
||||
pub fn $byteorder_fn(&mut self) -> $type {
|
||||
let val = LittleEndian::$byteorder_fn(self.slice);
|
||||
self.slice = &self.slice[mem::size_of::<$type>()..];
|
||||
val
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_read_fn!(
|
||||
u16, read_u16;
|
||||
u32, read_u32;
|
||||
u64, read_u64;
|
||||
i16, read_i16;
|
||||
i32, read_i32;
|
||||
i64, read_i64
|
||||
);
|
||||
|
||||
pub struct DwarfWriter<'a> {
|
||||
pub slice: &'a mut [u8],
|
||||
pub offset: usize,
|
||||
}
|
||||
|
||||
impl<'a> DwarfWriter<'a> {
|
||||
pub fn new(slice: &mut [u8]) -> DwarfWriter {
|
||||
DwarfWriter { slice, offset: 0 }
|
||||
}
|
||||
|
||||
pub fn write_u8(&mut self, data: u8) {
|
||||
self.slice[self.offset] = data;
|
||||
self.offset += 1;
|
||||
}
|
||||
|
||||
pub fn write_u32(&mut self, data: u32) {
|
||||
LittleEndian::write_u32(&mut self.slice[self.offset..], data);
|
||||
self.offset += 4;
|
||||
}
|
||||
}
|
||||
|
||||
fn read_encoded_pointer(reader: &mut DwarfReader, encoding: u8) -> Result<usize, ()> {
|
||||
if encoding == DW_EH_PE_omit {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
// DW_EH_PE_aligned implies it's an absolute pointer value
|
||||
// However, we are linking library for 32-bits architecture
|
||||
// The size of variable should be 4 bytes instead
|
||||
if encoding == DW_EH_PE_aligned {
|
||||
let shifted_virt_addr = round_up(reader.virt_addr as usize, mem::size_of::<u32>())?;
|
||||
let addr_inc = shifted_virt_addr - reader.virt_addr as usize;
|
||||
|
||||
reader.slice = &reader.slice[addr_inc..];
|
||||
reader.virt_addr = shifted_virt_addr as u32;
|
||||
return Ok(reader.read_u32() as usize);
|
||||
}
|
||||
|
||||
match encoding & 0x0F {
|
||||
DW_EH_PE_absptr => Ok(reader.read_u32() as usize),
|
||||
DW_EH_PE_uleb128 => Ok(reader.read_uleb128() as usize),
|
||||
DW_EH_PE_udata2 => Ok(reader.read_u16() as usize),
|
||||
DW_EH_PE_udata4 => Ok(reader.read_u32() as usize),
|
||||
DW_EH_PE_udata8 => Ok(reader.read_u64() as usize),
|
||||
DW_EH_PE_sleb128 => Ok(reader.read_sleb128() as usize),
|
||||
DW_EH_PE_sdata2 => Ok(reader.read_i16() as usize),
|
||||
DW_EH_PE_sdata4 => Ok(reader.read_i32() as usize),
|
||||
DW_EH_PE_sdata8 => Ok(reader.read_i64() as usize),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
|
||||
fn read_encoded_pointer_with_pc(
|
||||
reader: &mut DwarfReader,
|
||||
encoding: u8,
|
||||
) -> Result<usize, ()> {
|
||||
let entry_virt_addr = reader.virt_addr;
|
||||
let mut result = read_encoded_pointer(reader, encoding)?;
|
||||
|
||||
// DW_EH_PE_aligned implies it's an absolute pointer value
|
||||
if encoding == DW_EH_PE_aligned {
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
result = match encoding & 0x70 {
|
||||
DW_EH_PE_pcrel => result.wrapping_add(entry_virt_addr as usize),
|
||||
|
||||
// .eh_frame normally would not have these kinds of relocations
|
||||
// These would not be supported by a dedicated linker relocation schemes for RISC-V
|
||||
DW_EH_PE_textrel | DW_EH_PE_datarel | DW_EH_PE_funcrel | DW_EH_PE_aligned => {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Other values should be impossible
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
if encoding & DW_EH_PE_indirect != 0 {
|
||||
// There should not be a need for indirect addressing, as assembly code from
|
||||
// the dynamic library should not be freely moved relative to the EH frame.
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn round_up(unrounded: usize, align: usize) -> Result<usize, ()> {
|
||||
if align.is_power_of_two() {
|
||||
Ok((unrounded + align - 1) & !(align - 1))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
// Minimalistic structure to store everything needed for parsing FDEs to synthesize
|
||||
// .eh_frame_hdr section. Since we are only linking 1 object file, there should only be 1 call
|
||||
// frame information (CFI) record, so there should be only 1 common information entry (CIE).
|
||||
// So the class parses the only CIE on init, cache the encoding info, then parse the FDE on
|
||||
// iterations based on the cached encoding format.
|
||||
pub struct EH_Frame<'a> {
|
||||
// It refers to the augmentation data that corresponds to 'R' in the augmentation string
|
||||
pub fde_pointer_encoding: u8,
|
||||
pub fde_reader: DwarfReader<'a>,
|
||||
pub fde_sz: usize,
|
||||
}
|
||||
|
||||
impl<'a> EH_Frame<'a> {
|
||||
pub fn new(eh_frame_slice: &[u8], eh_frame_addr: u32) -> Result<EH_Frame, ()> {
|
||||
let mut cie_reader = DwarfReader::new(eh_frame_slice, eh_frame_addr);
|
||||
let eh_frame_size = eh_frame_slice.len();
|
||||
let length = cie_reader.read_u32();
|
||||
let fde_reader = match length {
|
||||
// eh_frame with 0 lengths means the CIE is terminated
|
||||
// while length == u32::MAX means that the length is only representable with 64 bits,
|
||||
// which does not make sense in a system with 32-bit address.
|
||||
0 | 0xFFFFFFFF => unimplemented!(),
|
||||
_ => {
|
||||
let mut fde_reader = DwarfReader::new(cie_reader.slice, cie_reader.virt_addr);
|
||||
fde_reader.offset(length as i32);
|
||||
fde_reader
|
||||
}
|
||||
};
|
||||
let fde_sz = eh_frame_size - mem::size_of::<u32>() - length as usize;
|
||||
|
||||
// Routine check on the .eh_frame well-formness, in terms of CIE ID & Version args.
|
||||
assert_eq!(cie_reader.read_u32(), 0);
|
||||
assert_eq!(cie_reader.read_u8(), 1);
|
||||
|
||||
// Parse augmentation string
|
||||
// The first character must be 'z', there is no way to proceed otherwise
|
||||
assert_eq!(cie_reader.read_u8(), b'z');
|
||||
|
||||
// Establish a pointer that skips ahead of the string
|
||||
// Skip code/data alignment factors & return address register along the way as well
|
||||
// We only tackle the case where 'z' and 'R' are part of the augmentation string, otherwise
|
||||
// we cannot get the addresses to make .eh_frame_hdr
|
||||
let mut aug_data_reader = DwarfReader::new(cie_reader.slice, cie_reader.virt_addr);
|
||||
let mut aug_str_len = 0;
|
||||
loop {
|
||||
if aug_data_reader.read_u8() == b'\0' {
|
||||
break;
|
||||
}
|
||||
aug_str_len += 1;
|
||||
}
|
||||
if aug_str_len == 0 {
|
||||
unimplemented!();
|
||||
}
|
||||
aug_data_reader.read_uleb128(); // Code alignment factor
|
||||
aug_data_reader.read_sleb128(); // Data alignment factor
|
||||
aug_data_reader.read_uleb128(); // Return address register
|
||||
aug_data_reader.read_uleb128(); // Augmentation data length
|
||||
let mut fde_pointer_encoding = DW_EH_PE_omit;
|
||||
for _ in 0..aug_str_len {
|
||||
match cie_reader.read_u8() {
|
||||
b'L' => {
|
||||
aug_data_reader.read_u8();
|
||||
}
|
||||
|
||||
b'P' => {
|
||||
let encoding = aug_data_reader.read_u8();
|
||||
read_encoded_pointer(&mut aug_data_reader, encoding)?;
|
||||
}
|
||||
|
||||
b'R' => {
|
||||
fde_pointer_encoding = aug_data_reader.read_u8();
|
||||
}
|
||||
|
||||
// Other characters are not supported
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
assert_ne!(fde_pointer_encoding, DW_EH_PE_omit);
|
||||
|
||||
Ok(EH_Frame { fde_pointer_encoding, fde_reader, fde_sz })
|
||||
}
|
||||
|
||||
pub fn iterate_fde(&self, callback: &mut dyn FnMut(u32, u32)) -> Result<(), ()> {
|
||||
// Parse each FDE to obtain the starting address that the FDE applies to
|
||||
// Send the FDE offset and the mentioned address to a callback that write up the
|
||||
// .eh_frame_hdr section
|
||||
let mut remaining_len = self.fde_sz;
|
||||
let mut reader = DwarfReader::new(self.fde_reader.slice, self.fde_reader.virt_addr);
|
||||
loop {
|
||||
if remaining_len == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
let fde_virt_addr = reader.virt_addr;
|
||||
let length = match reader.read_u32() {
|
||||
0 | 0xFFFFFFFF => unimplemented!(),
|
||||
other => other,
|
||||
};
|
||||
|
||||
// Remove the length of the header and the content from the counter
|
||||
remaining_len -= length as usize + mem::size_of::<u32>();
|
||||
let mut next_fde_reader = DwarfReader::new(reader.slice, reader.virt_addr);
|
||||
next_fde_reader.offset(length as i32);
|
||||
|
||||
// Skip CIE pointer offset
|
||||
reader.read_u32();
|
||||
|
||||
// Parse PC Begin using the encoding scheme mentioned in the CIE
|
||||
let pc_begin = read_encoded_pointer_with_pc(&mut reader, self.fde_pointer_encoding)?;
|
||||
|
||||
callback(pc_begin as u32, fde_virt_addr);
|
||||
|
||||
reader = next_fde_reader;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EH_Frame_Hdr<'a> {
|
||||
fde_writer: DwarfWriter<'a>,
|
||||
eh_frame_hdr_addr: u32,
|
||||
fdes: Vec<(u32, u32)>,
|
||||
}
|
||||
|
||||
impl<'a> EH_Frame_Hdr<'a> {
|
||||
// Create a EH_Frame_Hdr object, and write out the fixed fields of .eh_frame_hdr to memory
|
||||
// eh_frame_ptr_enc will be 0x1B (PC-relative, 4 bytes)
|
||||
// table_enc will be 0x3B (Relative to the start of .eh_frame_hdr, 4 bytes)
|
||||
// Load address is not known at this point.
|
||||
pub fn new(
|
||||
eh_frame_hdr_slice: &mut [u8],
|
||||
eh_frame_hdr_addr: u32,
|
||||
eh_frame_addr: u32,
|
||||
) -> EH_Frame_Hdr {
|
||||
let mut writer = DwarfWriter::new(eh_frame_hdr_slice);
|
||||
writer.write_u8(1);
|
||||
writer.write_u8(0x1B);
|
||||
writer.write_u8(0x03);
|
||||
writer.write_u8(0x3B);
|
||||
|
||||
let eh_frame_offset =
|
||||
(eh_frame_addr).wrapping_sub(eh_frame_hdr_addr + ((mem::size_of::<u8>() as u32) * 4));
|
||||
writer.write_u32(eh_frame_offset);
|
||||
writer.write_u32(0);
|
||||
|
||||
EH_Frame_Hdr { fde_writer: writer, eh_frame_hdr_addr, fdes: Vec::new() }
|
||||
}
|
||||
|
||||
fn fde_count_offset() -> usize {
|
||||
8
|
||||
}
|
||||
|
||||
pub fn add_fde(&mut self, init_loc: u32, addr: u32) {
|
||||
self.fdes.push((
|
||||
init_loc.wrapping_sub(self.eh_frame_hdr_addr),
|
||||
addr.wrapping_sub(self.eh_frame_hdr_addr),
|
||||
));
|
||||
}
|
||||
|
||||
pub fn finalize_fde(mut self) {
|
||||
self.fdes
|
||||
.sort_by(|(left_init_loc, _), (right_init_loc, _)| left_init_loc.cmp(right_init_loc));
|
||||
for (init_loc, addr) in &self.fdes {
|
||||
self.fde_writer.write_u32(*init_loc);
|
||||
self.fde_writer.write_u32(*addr);
|
||||
}
|
||||
LittleEndian::write_u32(&mut self.fde_writer.slice[Self::fde_count_offset()..], self.fdes.len() as u32);
|
||||
}
|
||||
|
||||
pub fn size_from_eh_frame(eh_frame: &[u8]) -> usize {
|
||||
// The virtual address of the EH frame does not matter in this case
|
||||
// Calculation of size does not involve modifying any headers
|
||||
let mut reader = DwarfReader::new(eh_frame, 0);
|
||||
let mut fde_count = 0;
|
||||
while !reader.slice.is_empty() {
|
||||
// The original length field should be able to hold the entire value.
|
||||
// The device memory space is limited to 32-bits addresses anyway.
|
||||
let entry_length = reader.read_u32();
|
||||
if entry_length == 0 || entry_length == 0xFFFFFFFF {
|
||||
unimplemented!()
|
||||
}
|
||||
if reader.read_u32() != 0 {
|
||||
fde_count += 1;
|
||||
}
|
||||
reader.offset(entry_length as i32 - mem::size_of::<u32>() as i32)
|
||||
}
|
||||
12 + fde_count * 8
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue