Merge CompiledFunction and CompiledData

This commit is contained in:
bjorn3
2020-10-12 14:02:37 +02:00
parent 7a2a4bc903
commit 69513ee85e

View File

@@ -128,8 +128,8 @@ pub struct SimpleJITModule {
libcall_names: Box<dyn Fn(ir::LibCall) -> String>, libcall_names: Box<dyn Fn(ir::LibCall) -> String>,
memory: SimpleJITMemoryHandle, memory: SimpleJITMemoryHandle,
declarations: ModuleDeclarations, declarations: ModuleDeclarations,
functions: SecondaryMap<FuncId, Option<SimpleJITCompiledFunction>>, functions: SecondaryMap<FuncId, Option<SimpleJITCompiledBlob>>,
data_objects: SecondaryMap<DataId, Option<SimpleJITCompiledData>>, data_objects: SecondaryMap<DataId, Option<SimpleJITCompiledBlob>>,
functions_to_finalize: Vec<FuncId>, functions_to_finalize: Vec<FuncId>,
data_objects_to_finalize: Vec<DataId>, data_objects_to_finalize: Vec<DataId>,
} }
@@ -151,15 +151,8 @@ struct StackMapRecord {
} }
#[derive(Clone)] #[derive(Clone)]
struct SimpleJITCompiledFunction { struct SimpleJITCompiledBlob {
code: *mut u8, ptr: *mut u8,
size: usize,
relocs: Vec<RelocRecord>,
}
#[derive(Clone)]
struct SimpleJITCompiledData {
storage: *mut u8,
size: usize, size: usize,
relocs: Vec<RelocRecord>, relocs: Vec<RelocRecord>,
} }
@@ -176,8 +169,8 @@ struct SimpleJITMemoryHandle {
pub struct SimpleJITProduct { pub struct SimpleJITProduct {
memory: SimpleJITMemoryHandle, memory: SimpleJITMemoryHandle,
declarations: ModuleDeclarations, declarations: ModuleDeclarations,
functions: SecondaryMap<FuncId, Option<SimpleJITCompiledFunction>>, functions: SecondaryMap<FuncId, Option<SimpleJITCompiledBlob>>,
data_objects: SecondaryMap<DataId, Option<SimpleJITCompiledData>>, data_objects: SecondaryMap<DataId, Option<SimpleJITCompiledBlob>>,
} }
impl SimpleJITProduct { impl SimpleJITProduct {
@@ -205,7 +198,7 @@ impl SimpleJITProduct {
self.functions[func_id] self.functions[func_id]
.as_ref() .as_ref()
.unwrap_or_else(|| panic!("{} is not defined", func_id)) .unwrap_or_else(|| panic!("{} is not defined", func_id))
.code .ptr
} }
/// Return the address and size of a data object. /// Return the address and size of a data object.
@@ -213,7 +206,7 @@ impl SimpleJITProduct {
let data = self.data_objects[data_id] let data = self.data_objects[data_id]
.as_ref() .as_ref()
.unwrap_or_else(|| panic!("{} is not defined", data_id)); .unwrap_or_else(|| panic!("{} is not defined", data_id));
(data.storage, data.size) (data.ptr, data.size)
} }
} }
@@ -231,7 +224,7 @@ impl SimpleJITModule {
let (name, linkage) = if self.declarations.is_function(name) { let (name, linkage) = if self.declarations.is_function(name) {
let func_id = self.declarations.get_function_id(name); let func_id = self.declarations.get_function_id(name);
match &self.functions[func_id] { match &self.functions[func_id] {
Some(compiled) => return compiled.code, Some(compiled) => return compiled.ptr,
None => { None => {
let decl = self.declarations.get_function_decl(func_id); let decl = self.declarations.get_function_decl(func_id);
(&decl.name, decl.linkage) (&decl.name, decl.linkage)
@@ -240,7 +233,7 @@ impl SimpleJITModule {
} else { } else {
let data_id = self.declarations.get_data_id(name); let data_id = self.declarations.get_data_id(name);
match &self.data_objects[data_id] { match &self.data_objects[data_id] {
Some(compiled) => return compiled.storage, Some(compiled) => return compiled.ptr,
None => { None => {
let decl = self.declarations.get_data_decl(data_id); let decl = self.declarations.get_data_decl(data_id);
(&decl.name, decl.linkage) (&decl.name, decl.linkage)
@@ -273,7 +266,7 @@ impl SimpleJITModule {
); );
info.as_ref() info.as_ref()
.expect("function must be compiled before it can be finalized") .expect("function must be compiled before it can be finalized")
.code .ptr
} }
/// Returns the address and size of a finalized data object. /// Returns the address and size of a finalized data object.
@@ -287,7 +280,7 @@ impl SimpleJITModule {
.as_ref() .as_ref()
.expect("data object must be compiled before it can be finalized"); .expect("data object must be compiled before it can be finalized");
(compiled.storage, compiled.size) (compiled.ptr, compiled.size)
} }
fn record_function_for_perf(&self, ptr: *mut u8, size: usize, name: &str) { fn record_function_for_perf(&self, ptr: *mut u8, size: usize, name: &str) {
@@ -321,9 +314,8 @@ impl SimpleJITModule {
addend, addend,
} in &func.relocs } in &func.relocs
{ {
let ptr = func.code;
debug_assert!((offset as usize) < func.size); debug_assert!((offset as usize) < func.size);
let at = unsafe { ptr.offset(offset as isize) }; let at = unsafe { func.ptr.offset(offset as isize) };
let base = self.get_definition(name); let base = self.get_definition(name);
// TODO: Handle overflow. // TODO: Handle overflow.
let what = unsafe { base.offset(addend as isize) }; let what = unsafe { base.offset(addend as isize) };
@@ -369,9 +361,8 @@ impl SimpleJITModule {
addend, addend,
} in &data.relocs } in &data.relocs
{ {
let ptr = data.storage;
debug_assert!((offset as usize) < data.size); debug_assert!((offset as usize) < data.size);
let at = unsafe { ptr.offset(offset as isize) }; let at = unsafe { data.ptr.offset(offset as isize) };
let base = self.get_definition(name); let base = self.get_definition(name);
// TODO: Handle overflow. // TODO: Handle overflow.
let what = unsafe { base.offset(addend as isize) }; let what = unsafe { base.offset(addend as isize) };
@@ -524,8 +515,8 @@ impl<'simple_jit_backend> Module for SimpleJITModule {
) )
}; };
self.functions[id] = Some(SimpleJITCompiledFunction { self.functions[id] = Some(SimpleJITCompiledBlob {
code: ptr, ptr,
size, size,
relocs: reloc_sink.relocs, relocs: reloc_sink.relocs,
}); });
@@ -566,8 +557,8 @@ impl<'simple_jit_backend> Module for SimpleJITModule {
ptr::copy_nonoverlapping(bytes.as_ptr(), ptr, size); ptr::copy_nonoverlapping(bytes.as_ptr(), ptr, size);
} }
self.functions[id] = Some(SimpleJITCompiledFunction { self.functions[id] = Some(SimpleJITCompiledBlob {
code: ptr, ptr,
size, size,
relocs: vec![], relocs: vec![],
}); });
@@ -600,7 +591,7 @@ impl<'simple_jit_backend> Module for SimpleJITModule {
} = data.description(); } = data.description();
let size = init.size(); let size = init.size();
let storage = if decl.writable { let ptr = if decl.writable {
self.memory self.memory
.writable .writable
.allocate(size, align.unwrap_or(WRITABLE_DATA_ALIGNMENT)) .allocate(size, align.unwrap_or(WRITABLE_DATA_ALIGNMENT))
@@ -617,11 +608,11 @@ impl<'simple_jit_backend> Module for SimpleJITModule {
panic!("data is not initialized yet"); panic!("data is not initialized yet");
} }
Init::Zeros { .. } => { Init::Zeros { .. } => {
unsafe { ptr::write_bytes(storage, 0, size) }; unsafe { ptr::write_bytes(ptr, 0, size) };
} }
Init::Bytes { ref contents } => { Init::Bytes { ref contents } => {
let src = contents.as_ptr(); let src = contents.as_ptr();
unsafe { ptr::copy_nonoverlapping(src, storage, size) }; unsafe { ptr::copy_nonoverlapping(src, ptr, size) };
} }
} }
@@ -648,8 +639,8 @@ impl<'simple_jit_backend> Module for SimpleJITModule {
}); });
} }
self.data_objects[id] = Some(SimpleJITCompiledData { self.data_objects[id] = Some(SimpleJITCompiledBlob {
storage, ptr,
size, size,
relocs, relocs,
}); });