Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions cranelift/jit/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cranelift_codegen::{binemit::Reloc, CodegenError};
use cranelift_control::ControlPlane;
use cranelift_entity::SecondaryMap;
use cranelift_module::{
DataContext, DataDescription, DataId, FuncId, Init, Linkage, Module, ModuleCompiledFunction,
DataDescription, DataId, FuncId, Init, Linkage, Module, ModuleCompiledFunction,
ModuleDeclarations, ModuleError, ModuleExtName, ModuleReloc, ModuleResult,
};
use log::info;
Expand Down Expand Up @@ -837,7 +837,7 @@ impl Module for JITModule {
Ok(ModuleCompiledFunction { size: total_size })
}

fn define_data(&mut self, id: DataId, data: &DataContext) -> ModuleResult<()> {
fn define_data(&mut self, id: DataId, data: &DataDescription) -> ModuleResult<()> {
let decl = self.declarations.get_data_decl(id);
if !decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
Expand All @@ -857,7 +857,7 @@ impl Module for JITModule {
data_relocs: _,
custom_segment_section: _,
align,
} = data.description();
} = data;

let size = init.size();
let ptr = if decl.writable {
Expand Down Expand Up @@ -896,10 +896,7 @@ impl Module for JITModule {
PointerWidth::U32 => Reloc::Abs4,
PointerWidth::U64 => Reloc::Abs8,
};
let relocs = data
.description()
.all_relocs(pointer_reloc)
.collect::<Vec<_>>();
let relocs = data.all_relocs(pointer_reloc).collect::<Vec<_>>();

self.compiled_data_objects[id] = Some(CompiledBlob { ptr, size, relocs });
self.data_objects_to_finalize.push(id);
Expand Down
220 changes: 96 additions & 124 deletions cranelift/module/src/data_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Init {
}

/// A description of a data object.
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct DataDescription {
/// How the data should be initialized.
pub init: Init,
Expand All @@ -60,88 +60,53 @@ pub struct DataDescription {
}

impl DataDescription {
/// An iterator over all relocations of the data object.
pub fn all_relocs<'a>(
&'a self,
pointer_reloc: Reloc,
) -> impl Iterator<Item = ModuleReloc> + 'a {
let func_relocs = self
.function_relocs
.iter()
.map(move |&(offset, id)| ModuleReloc {
kind: pointer_reloc,
offset,
name: self.function_decls[id].clone(),
addend: 0,
});
let data_relocs = self
.data_relocs
.iter()
.map(move |&(offset, id, addend)| ModuleReloc {
kind: pointer_reloc,
offset,
name: self.data_decls[id].clone(),
addend,
});
func_relocs.chain(data_relocs)
}
}

/// This is to data objects what cranelift_codegen::Context is to functions.
pub struct DataContext {
description: DataDescription,
}

impl DataContext {
/// Allocate a new context.
/// Allocate a new `DataDescription`.
pub fn new() -> Self {
Self {
description: DataDescription {
init: Init::Uninitialized,
function_decls: PrimaryMap::new(),
data_decls: PrimaryMap::new(),
function_relocs: vec![],
data_relocs: vec![],
custom_segment_section: None,
align: None,
},
init: Init::Uninitialized,
function_decls: PrimaryMap::new(),
data_decls: PrimaryMap::new(),
function_relocs: vec![],
data_relocs: vec![],
custom_segment_section: None,
align: None,
}
}

/// Clear all data structures in this context.
/// Clear all data structures in this `DataDescription`.
pub fn clear(&mut self) {
self.description.init = Init::Uninitialized;
self.description.function_decls.clear();
self.description.data_decls.clear();
self.description.function_relocs.clear();
self.description.data_relocs.clear();
self.description.custom_segment_section = None;
self.description.align = None;
self.init = Init::Uninitialized;
self.function_decls.clear();
self.data_decls.clear();
self.function_relocs.clear();
self.data_relocs.clear();
self.custom_segment_section = None;
self.align = None;
}

/// Define a zero-initialized object with the given size.
pub fn define_zeroinit(&mut self, size: usize) {
debug_assert_eq!(self.description.init, Init::Uninitialized);
self.description.init = Init::Zeros { size };
debug_assert_eq!(self.init, Init::Uninitialized);
self.init = Init::Zeros { size };
}

/// Define an object initialized with the given contents.
///
/// TODO: Can we avoid a Box here?
pub fn define(&mut self, contents: Box<[u8]>) {
debug_assert_eq!(self.description.init, Init::Uninitialized);
self.description.init = Init::Bytes { contents };
debug_assert_eq!(self.init, Init::Uninitialized);
self.init = Init::Bytes { contents };
}

/// Override the segment/section for data, only supported on Object backend
pub fn set_segment_section(&mut self, seg: &str, sec: &str) {
self.description.custom_segment_section = Some((seg.to_owned(), sec.to_owned()))
self.custom_segment_section = Some((seg.to_owned(), sec.to_owned()))
}

/// Set the alignment for data. The alignment must be a power of two.
pub fn set_align(&mut self, align: u64) {
assert!(align.is_power_of_two());
self.description.align = Some(align);
self.align = Some(align);
}

/// Declare an external function import.
Expand All @@ -150,7 +115,7 @@ impl DataContext {
/// `Module::declare_func_in_data` instead, as it takes care of generating
/// the appropriate `ExternalName`.
pub fn import_function(&mut self, name: ModuleExtName) -> ir::FuncRef {
self.description.function_decls.push(name)
self.function_decls.push(name)
}

/// Declares a global value import.
Expand All @@ -161,93 +126,100 @@ impl DataContext {
/// `Module::declare_data_in_data` instead, as it takes care of generating
/// the appropriate `ExternalName`.
pub fn import_global_value(&mut self, name: ModuleExtName) -> ir::GlobalValue {
self.description.data_decls.push(name)
self.data_decls.push(name)
}

/// Write the address of `func` into the data at offset `offset`.
pub fn write_function_addr(&mut self, offset: CodeOffset, func: ir::FuncRef) {
self.description.function_relocs.push((offset, func))
self.function_relocs.push((offset, func))
}

/// Write the address of `data` into the data at offset `offset`.
pub fn write_data_addr(&mut self, offset: CodeOffset, data: ir::GlobalValue, addend: Addend) {
self.description.data_relocs.push((offset, data, addend))
self.data_relocs.push((offset, data, addend))
}

/// Reference the initializer data.
pub fn description(&self) -> &DataDescription {
debug_assert!(
self.description.init != Init::Uninitialized,
"data must be initialized first"
);
&self.description
/// An iterator over all relocations of the data object.
pub fn all_relocs<'a>(
&'a self,
pointer_reloc: Reloc,
) -> impl Iterator<Item = ModuleReloc> + 'a {
let func_relocs = self
.function_relocs
.iter()
.map(move |&(offset, id)| ModuleReloc {
kind: pointer_reloc,
offset,
name: self.function_decls[id].clone(),
addend: 0,
});
let data_relocs = self
.data_relocs
.iter()
.map(move |&(offset, id, addend)| ModuleReloc {
kind: pointer_reloc,
offset,
name: self.data_decls[id].clone(),
addend,
});
func_relocs.chain(data_relocs)
}
}

#[cfg(test)]
mod tests {
use crate::ModuleExtName;

use super::{DataContext, Init};
use super::{DataDescription, Init};

#[test]
fn basic_data_context() {
let mut data_ctx = DataContext::new();
{
let description = &data_ctx.description;
assert_eq!(description.init, Init::Uninitialized);
assert!(description.function_decls.is_empty());
assert!(description.data_decls.is_empty());
assert!(description.function_relocs.is_empty());
assert!(description.data_relocs.is_empty());
}

data_ctx.define_zeroinit(256);

let _func_a = data_ctx.import_function(ModuleExtName::user(0, 0));
let func_b = data_ctx.import_function(ModuleExtName::user(0, 1));
let func_c = data_ctx.import_function(ModuleExtName::user(0, 2));
let _data_a = data_ctx.import_global_value(ModuleExtName::user(0, 3));
let data_b = data_ctx.import_global_value(ModuleExtName::user(0, 4));

data_ctx.write_function_addr(8, func_b);
data_ctx.write_function_addr(16, func_c);
data_ctx.write_data_addr(32, data_b, 27);

{
let description = data_ctx.description();
assert_eq!(description.init, Init::Zeros { size: 256 });
assert_eq!(description.function_decls.len(), 3);
assert_eq!(description.data_decls.len(), 2);
assert_eq!(description.function_relocs.len(), 2);
assert_eq!(description.data_relocs.len(), 1);
}

data_ctx.clear();
{
let description = &data_ctx.description;
assert_eq!(description.init, Init::Uninitialized);
assert!(description.function_decls.is_empty());
assert!(description.data_decls.is_empty());
assert!(description.function_relocs.is_empty());
assert!(description.data_relocs.is_empty());
}
let mut data = DataDescription::new();
assert_eq!(data.init, Init::Uninitialized);
assert!(data.function_decls.is_empty());
assert!(data.data_decls.is_empty());
assert!(data.function_relocs.is_empty());
assert!(data.data_relocs.is_empty());

data.define_zeroinit(256);

let _func_a = data.import_function(ModuleExtName::user(0, 0));
let func_b = data.import_function(ModuleExtName::user(0, 1));
let func_c = data.import_function(ModuleExtName::user(0, 2));
let _data_a = data.import_global_value(ModuleExtName::user(0, 3));
let data_b = data.import_global_value(ModuleExtName::user(0, 4));

data.write_function_addr(8, func_b);
data.write_function_addr(16, func_c);
data.write_data_addr(32, data_b, 27);

assert_eq!(data.init, Init::Zeros { size: 256 });
assert_eq!(data.function_decls.len(), 3);
assert_eq!(data.data_decls.len(), 2);
assert_eq!(data.function_relocs.len(), 2);
assert_eq!(data.data_relocs.len(), 1);

data.clear();

assert_eq!(data.init, Init::Uninitialized);
assert!(data.function_decls.is_empty());
assert!(data.data_decls.is_empty());
assert!(data.function_relocs.is_empty());
assert!(data.data_relocs.is_empty());

let contents = vec![33, 34, 35, 36];
let contents_clone = contents.clone();
data_ctx.define(contents.into_boxed_slice());
{
let description = data_ctx.description();
assert_eq!(
description.init,
Init::Bytes {
contents: contents_clone.into_boxed_slice()
}
);
assert_eq!(description.function_decls.len(), 0);
assert_eq!(description.data_decls.len(), 0);
assert_eq!(description.function_relocs.len(), 0);
assert_eq!(description.data_relocs.len(), 0);
}
data.define(contents.into_boxed_slice());

assert_eq!(
data.init,
Init::Bytes {
contents: contents_clone.into_boxed_slice()
}
);
assert_eq!(data.function_decls.len(), 0);
assert_eq!(data.data_decls.len(), 0);
assert_eq!(data.function_relocs.len(), 0);
assert_eq!(data.data_relocs.len(), 0);
}
}
2 changes: 1 addition & 1 deletion cranelift/module/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod data_context;
mod module;
mod traps;

pub use crate::data_context::{DataContext, DataDescription, Init};
pub use crate::data_context::{DataDescription, Init};
pub use crate::module::{
DataDeclaration, DataId, FuncId, FuncOrDataId, FunctionDeclaration, Linkage, Module,
ModuleCompiledFunction, ModuleDeclarations, ModuleError, ModuleExtName, ModuleReloc,
Expand Down
Loading