cranelift-module: expose trap information when defining functions

The current interface of `cranelift-module` requires consumers who want
to be informed about traps to discover that information through
`Module::Product`, which is backend-specific.  Since it's advantageous
to manipulate this information in a backend-agnostic way, this patch
changes `Module::define_function{,_bytes}` to return information about
the traps contained in the function being defined.
This commit is contained in:
Nathan Froyd
2020-03-03 11:27:45 -05:00
parent f7c2a58d23
commit 0f49a830c9
6 changed files with 60 additions and 37 deletions

View File

@@ -17,6 +17,7 @@ use object::write::{
use object::{RelocationEncoding, RelocationKind, SymbolFlags, SymbolKind, SymbolScope};
use std::collections::HashMap;
use std::mem;
use std::ops::IndexMut;
use target_lexicon::{BinaryFormat, PointerWidth};
#[derive(Debug)]
@@ -188,7 +189,7 @@ impl Backend for ObjectBackend {
ctx: &cranelift_codegen::Context,
_namespace: &ModuleNamespace<Self>,
code_size: u32,
) -> ModuleResult<ObjectCompiledFunction> {
) -> ModuleResult<(ObjectCompiledFunction, Option<&Vec<TrapSite>>)> {
let mut code: Vec<u8> = vec![0; code_size as usize];
let mut reloc_sink = ObjectRelocSink::new(self.object.format());
let mut trap_sink = ObjectTrapSink::default();
@@ -229,8 +230,9 @@ impl Backend for ObjectBackend {
relocs: reloc_sink.relocs,
});
}
self.traps[func_id] = trap_sink.sites;
Ok(ObjectCompiledFunction)
let trapref = self.traps.index_mut(func_id);
*trapref = trap_sink.sites;
Ok((ObjectCompiledFunction, Some(trapref)))
}
fn define_function_bytes(
@@ -240,14 +242,15 @@ impl Backend for ObjectBackend {
bytes: &[u8],
_namespace: &ModuleNamespace<Self>,
traps: Vec<TrapSite>,
) -> ModuleResult<ObjectCompiledFunction> {
) -> ModuleResult<(ObjectCompiledFunction, Option<&Vec<TrapSite>>)> {
let symbol = self.functions[func_id].unwrap();
let section = self.object.section_id(StandardSection::Text);
let _offset = self
.object
.add_symbol_data(symbol, section, bytes, self.function_alignment);
self.traps[func_id] = traps;
Ok(ObjectCompiledFunction)
let trapref = self.traps.index_mut(func_id);
*trapref = traps;
Ok((ObjectCompiledFunction, Some(trapref)))
}
fn define_data(