Make it possible to define data alignment

This commit is contained in:
bjorn3
2019-03-30 16:45:58 +01:00
committed by Benjamin Bouvier
parent 443e48aee1
commit cb6268118c
5 changed files with 33 additions and 14 deletions

View File

@@ -127,9 +127,9 @@ impl Backend for FaerieBackend {
.expect("inconsistent declarations"); .expect("inconsistent declarations");
} }
fn declare_data(&mut self, name: &str, linkage: Linkage, writable: bool) { fn declare_data(&mut self, name: &str, linkage: Linkage, writable: bool, align: Option<u8>) {
self.artifact self.artifact
.declare(name, translate_data_linkage(linkage, writable)) .declare(name, translate_data_linkage(linkage, writable, align))
.expect("inconsistent declarations"); .expect("inconsistent declarations");
} }
@@ -190,6 +190,7 @@ impl Backend for FaerieBackend {
&mut self, &mut self,
name: &str, name: &str,
_writable: bool, _writable: bool,
_align: Option<u8>,
data_ctx: &DataContext, data_ctx: &DataContext,
namespace: &ModuleNamespace<Self>, namespace: &ModuleNamespace<Self>,
) -> ModuleResult<FaerieCompiledData> { ) -> ModuleResult<FaerieCompiledData> {
@@ -334,12 +335,13 @@ fn translate_function_linkage(linkage: Linkage) -> faerie::Decl {
} }
} }
fn translate_data_linkage(linkage: Linkage, writable: bool) -> faerie::Decl { fn translate_data_linkage(linkage: Linkage, writable: bool, align: Option<u8>) -> faerie::Decl {
let align = align.map(|align| usize::from(align));
match linkage { match linkage {
Linkage::Import => faerie::Decl::data_import().into(), Linkage::Import => faerie::Decl::data_import().into(),
Linkage::Local => faerie::Decl::data().with_writable(writable).into(), Linkage::Local => faerie::Decl::data().with_writable(writable).with_align(align).into(),
Linkage::Export => faerie::Decl::data().global().with_writable(writable).into(), Linkage::Export => faerie::Decl::data().global().with_writable(writable).with_align(align).into(),
Linkage::Preemptible => faerie::Decl::data().weak().with_writable(writable).into(), Linkage::Preemptible => faerie::Decl::data().weak().with_writable(writable).with_align(align).into(),
} }
} }

View File

@@ -59,7 +59,7 @@ where
fn declare_function(&mut self, name: &str, linkage: Linkage); fn declare_function(&mut self, name: &str, linkage: Linkage);
/// Declare a data object. /// Declare a data object.
fn declare_data(&mut self, name: &str, linkage: Linkage, writable: bool); fn declare_data(&mut self, name: &str, linkage: Linkage, writable: bool, align: Option<u8>);
/// Define a function, producing the function body from the given `Context`. /// Define a function, producing the function body from the given `Context`.
/// ///
@@ -79,6 +79,7 @@ where
&mut self, &mut self,
name: &str, name: &str,
writable: bool, writable: bool,
align: Option<u8>,
data_ctx: &DataContext, data_ctx: &DataContext,
namespace: &ModuleNamespace<Self>, namespace: &ModuleNamespace<Self>,
) -> ModuleResult<Self::CompiledData>; ) -> ModuleResult<Self::CompiledData>;

View File

@@ -185,6 +185,7 @@ pub struct DataDeclaration {
pub name: String, pub name: String,
pub linkage: Linkage, pub linkage: Linkage,
pub writable: bool, pub writable: bool,
pub align: Option<u8>,
} }
/// A data object belonging to a `Module`. /// A data object belonging to a `Module`.
@@ -438,6 +439,7 @@ where
name: &str, name: &str,
linkage: Linkage, linkage: Linkage,
writable: bool, writable: bool,
align: Option<u8>, // An alignment bigger than 128 is unlikely
) -> ModuleResult<DataId> { ) -> ModuleResult<DataId> {
// TODO: Can we avoid allocating names so often? // TODO: Can we avoid allocating names so often?
use super::hash_map::Entry::*; use super::hash_map::Entry::*;
@@ -447,7 +449,7 @@ where
let existing = &mut self.contents.data_objects[id]; let existing = &mut self.contents.data_objects[id];
existing.merge(linkage, writable); existing.merge(linkage, writable);
self.backend self.backend
.declare_data(name, existing.decl.linkage, existing.decl.writable); .declare_data(name, existing.decl.linkage, existing.decl.writable, existing.decl.align);
Ok(id) Ok(id)
} }
@@ -461,11 +463,12 @@ where
name: name.to_owned(), name: name.to_owned(),
linkage, linkage,
writable, writable,
align,
}, },
compiled: None, compiled: None,
}); });
entry.insert(FuncOrDataId::Data(id)); entry.insert(FuncOrDataId::Data(id));
self.backend.declare_data(name, linkage, writable); self.backend.declare_data(name, linkage, writable, align);
Ok(id) Ok(id)
} }
} }
@@ -563,6 +566,7 @@ where
Some(self.backend.define_data( Some(self.backend.define_data(
&info.decl.name, &info.decl.name,
info.decl.writable, info.decl.writable,
info.decl.align,
data_ctx, data_ctx,
&ModuleNamespace::<B> { &ModuleNamespace::<B> {
contents: &self.contents, contents: &self.contents,

View File

@@ -209,7 +209,13 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
// Nothing to do. // Nothing to do.
} }
fn declare_data(&mut self, _name: &str, _linkage: Linkage, _writable: bool) { fn declare_data(
&mut self,
_name: &str,
_linkage: Linkage,
_writable: bool,
_align: Option<u8>,
) {
// Nothing to do. // Nothing to do.
} }
@@ -223,7 +229,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
let size = code_size as usize; let size = code_size as usize;
let ptr = self let ptr = self
.code_memory .code_memory
.allocate(size) .allocate(size, 0x10)
.expect("TODO: handle OOM etc."); .expect("TODO: handle OOM etc.");
if cfg!(target_os = "linux") && ::std::env::var_os("PERF_BUILDID_DIR").is_some() { if cfg!(target_os = "linux") && ::std::env::var_os("PERF_BUILDID_DIR").is_some() {
@@ -253,6 +259,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
&mut self, &mut self,
_name: &str, _name: &str,
writable: bool, writable: bool,
align: Option<u8>,
data: &DataContext, data: &DataContext,
_namespace: &ModuleNamespace<Self>, _namespace: &ModuleNamespace<Self>,
) -> ModuleResult<Self::CompiledData> { ) -> ModuleResult<Self::CompiledData> {
@@ -267,11 +274,11 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
let size = init.size(); let size = init.size();
let storage = if writable { let storage = if writable {
self.writable_memory self.writable_memory
.allocate(size) .allocate(size, align.unwrap_or(0x8))
.expect("TODO: handle OOM etc.") .expect("TODO: handle OOM etc.")
} else { } else {
self.readonly_memory self.readonly_memory
.allocate(size) .allocate(size, align.unwrap_or(1))
.expect("TODO: handle OOM etc.") .expect("TODO: handle OOM etc.")
}; };

View File

@@ -97,7 +97,12 @@ impl Memory {
} }
/// TODO: Use a proper error type. /// TODO: Use a proper error type.
pub fn allocate(&mut self, size: usize) -> Result<*mut u8, String> { pub fn allocate(&mut self, size: usize, align: u8) -> Result<*mut u8, String> {
if self.position % align as usize != 0 {
self.position += align as usize - self.position % align as usize;
debug_assert!(self.position % align as usize == 0);
}
if size <= self.current.len - self.position { if size <= self.current.len - self.position {
// TODO: Ensure overflow is not possible. // TODO: Ensure overflow is not possible.
let ptr = unsafe { self.current.ptr.add(self.position) }; let ptr = unsafe { self.current.ptr.add(self.position) };