Make it possible to define data alignment
This commit is contained in:
@@ -127,9 +127,9 @@ impl Backend for FaerieBackend {
|
||||
.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
|
||||
.declare(name, translate_data_linkage(linkage, writable))
|
||||
.declare(name, translate_data_linkage(linkage, writable, align))
|
||||
.expect("inconsistent declarations");
|
||||
}
|
||||
|
||||
@@ -190,6 +190,7 @@ impl Backend for FaerieBackend {
|
||||
&mut self,
|
||||
name: &str,
|
||||
_writable: bool,
|
||||
_align: Option<u8>,
|
||||
data_ctx: &DataContext,
|
||||
namespace: &ModuleNamespace<Self>,
|
||||
) -> 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 {
|
||||
Linkage::Import => faerie::Decl::data_import().into(),
|
||||
Linkage::Local => faerie::Decl::data().with_writable(writable).into(),
|
||||
Linkage::Export => faerie::Decl::data().global().with_writable(writable).into(),
|
||||
Linkage::Preemptible => faerie::Decl::data().weak().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).with_align(align).into(),
|
||||
Linkage::Preemptible => faerie::Decl::data().weak().with_writable(writable).with_align(align).into(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ where
|
||||
fn declare_function(&mut self, name: &str, linkage: Linkage);
|
||||
|
||||
/// 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`.
|
||||
///
|
||||
@@ -79,6 +79,7 @@ where
|
||||
&mut self,
|
||||
name: &str,
|
||||
writable: bool,
|
||||
align: Option<u8>,
|
||||
data_ctx: &DataContext,
|
||||
namespace: &ModuleNamespace<Self>,
|
||||
) -> ModuleResult<Self::CompiledData>;
|
||||
|
||||
@@ -185,6 +185,7 @@ pub struct DataDeclaration {
|
||||
pub name: String,
|
||||
pub linkage: Linkage,
|
||||
pub writable: bool,
|
||||
pub align: Option<u8>,
|
||||
}
|
||||
|
||||
/// A data object belonging to a `Module`.
|
||||
@@ -438,6 +439,7 @@ where
|
||||
name: &str,
|
||||
linkage: Linkage,
|
||||
writable: bool,
|
||||
align: Option<u8>, // An alignment bigger than 128 is unlikely
|
||||
) -> ModuleResult<DataId> {
|
||||
// TODO: Can we avoid allocating names so often?
|
||||
use super::hash_map::Entry::*;
|
||||
@@ -447,7 +449,7 @@ where
|
||||
let existing = &mut self.contents.data_objects[id];
|
||||
existing.merge(linkage, writable);
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -461,11 +463,12 @@ where
|
||||
name: name.to_owned(),
|
||||
linkage,
|
||||
writable,
|
||||
align,
|
||||
},
|
||||
compiled: None,
|
||||
});
|
||||
entry.insert(FuncOrDataId::Data(id));
|
||||
self.backend.declare_data(name, linkage, writable);
|
||||
self.backend.declare_data(name, linkage, writable, align);
|
||||
Ok(id)
|
||||
}
|
||||
}
|
||||
@@ -563,6 +566,7 @@ where
|
||||
Some(self.backend.define_data(
|
||||
&info.decl.name,
|
||||
info.decl.writable,
|
||||
info.decl.align,
|
||||
data_ctx,
|
||||
&ModuleNamespace::<B> {
|
||||
contents: &self.contents,
|
||||
|
||||
@@ -209,7 +209,13 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
|
||||
// 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.
|
||||
}
|
||||
|
||||
@@ -223,7 +229,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
|
||||
let size = code_size as usize;
|
||||
let ptr = self
|
||||
.code_memory
|
||||
.allocate(size)
|
||||
.allocate(size, 0x10)
|
||||
.expect("TODO: handle OOM etc.");
|
||||
|
||||
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,
|
||||
_name: &str,
|
||||
writable: bool,
|
||||
align: Option<u8>,
|
||||
data: &DataContext,
|
||||
_namespace: &ModuleNamespace<Self>,
|
||||
) -> ModuleResult<Self::CompiledData> {
|
||||
@@ -267,11 +274,11 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
|
||||
let size = init.size();
|
||||
let storage = if writable {
|
||||
self.writable_memory
|
||||
.allocate(size)
|
||||
.allocate(size, align.unwrap_or(0x8))
|
||||
.expect("TODO: handle OOM etc.")
|
||||
} else {
|
||||
self.readonly_memory
|
||||
.allocate(size)
|
||||
.allocate(size, align.unwrap_or(1))
|
||||
.expect("TODO: handle OOM etc.")
|
||||
};
|
||||
|
||||
|
||||
@@ -97,7 +97,12 @@ impl Memory {
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
// TODO: Ensure overflow is not possible.
|
||||
let ptr = unsafe { self.current.ptr.add(self.position) };
|
||||
|
||||
Reference in New Issue
Block a user