[WIP] Module API (#294)
* Initial skeleton. * Add basic faerie support. This adds enough functionality to enable simple .o file writing through faerie. This included adding the functionality to Module to support RelocSink implementations. * Add basic SimpleJIT support. This adds enough functionality to enable a simple program to be jitted and executed. * Make declare_func_in_func take a Function instead of a Context. It only needs the Function, and sometimes it's useful to call it from places that don't have a full Context. * Temporarily disable local and exported global variables in the Faerie backend. Faerie assumes these variables use pc-relative offset instructions, and Cretonne is not yet emitting those instructions. * FaerieBackend depends on PIC. Faerie itself only supports PIC objects for now, so add an assert to Cretonne to check that it's using a PIC target flag. * SimpleJIT support for data objects. * Preliminary faerie support for data objects. * Support for data objects in faerie using the new colocated flag. * Unit tests for DataContext and friends. * Add a Module::consume() function. This consumes the Module and returns the contained Backend, so that users can call Backend-specific functions with it. For example, the Faerie backend has functions to write an object file. * Update the new crates to version 0.4.4. * Make FaerieBackend own its TargetIsa. This simplifies its interface, as it eliminates a lifetime parameter. While we may eventually want to look into allowing multiple clients to share a TargetIsa, it isn't worth the complexity for FaerieBackend right now. * Don't try to protect faerie from multiple declarations. Let faerie decide for itself whether it wants to consider two declarations to be compatible. * Use debug_assert_eq rather than debug_assert with ==. * Fix FaerieRelocSink's reloc_external to handle data object names. * Relax the asserts in get_function_definition and get_data_definition. These functions don't require definable symbols, but they do require that definable symbols be defined. This is needed for the simplejit backend. * Add a function to the faerie backend to retrieve the artifact name. * Sync up with cretonne changes.
This commit is contained in:
298
lib/faerie/src/backend.rs
Normal file
298
lib/faerie/src/backend.rs
Normal file
@@ -0,0 +1,298 @@
|
||||
//! Defines `FaerieBackend`.
|
||||
|
||||
use container;
|
||||
use cretonne_codegen::binemit::{Addend, CodeOffset, Reloc, RelocSink, TrapSink};
|
||||
use cretonne_codegen::isa::TargetIsa;
|
||||
use cretonne_codegen::result::CtonError;
|
||||
use cretonne_codegen::{self, binemit, ir};
|
||||
use cretonne_module::{Backend, DataContext, Linkage, ModuleNamespace, Init, DataDescription};
|
||||
use faerie;
|
||||
use failure::Error;
|
||||
use std::fs::File;
|
||||
use target;
|
||||
|
||||
pub struct FaerieCompiledFunction {}
|
||||
|
||||
pub struct FaerieCompiledData {}
|
||||
|
||||
/// A `FaerieBackend` implements `Backend` and emits ".o" files using the `faerie` library.
|
||||
pub struct FaerieBackend {
|
||||
isa: Box<TargetIsa>,
|
||||
artifact: faerie::Artifact,
|
||||
format: container::Format,
|
||||
}
|
||||
|
||||
impl FaerieBackend {
|
||||
/// Create a new `FaerieBackend` using the given Cretonne target.
|
||||
pub fn new(
|
||||
isa: Box<TargetIsa>,
|
||||
name: String,
|
||||
format: container::Format,
|
||||
) -> Result<Self, Error> {
|
||||
debug_assert!(isa.flags().is_pic(), "faerie requires PIC");
|
||||
let faerie_target = target::translate(&*isa)?;
|
||||
Ok(Self {
|
||||
isa,
|
||||
artifact: faerie::Artifact::new(faerie_target, name),
|
||||
format,
|
||||
})
|
||||
}
|
||||
|
||||
/// Return the name of the output file. This is the name passed into `new`.
|
||||
pub fn name(&self) -> &str {
|
||||
&self.artifact.name
|
||||
}
|
||||
|
||||
/// Call `emit` on the faerie `Artifact`, producing bytes in memory.
|
||||
pub fn emit(&self) -> Result<Vec<u8>, Error> {
|
||||
match self.format {
|
||||
container::Format::ELF => self.artifact.emit::<faerie::Elf>(),
|
||||
container::Format::MachO => self.artifact.emit::<faerie::Mach>(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Call `write` on the faerie `Artifact`, writing to a file.
|
||||
pub fn write(&self, sink: File) -> Result<(), Error> {
|
||||
match self.format {
|
||||
container::Format::ELF => self.artifact.write::<faerie::Elf>(sink),
|
||||
container::Format::MachO => self.artifact.write::<faerie::Mach>(sink),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Backend for FaerieBackend {
|
||||
type CompiledFunction = FaerieCompiledFunction;
|
||||
type CompiledData = FaerieCompiledData;
|
||||
|
||||
// There's no need to return invidual artifacts; we're writing them into
|
||||
// the output file instead.
|
||||
type FinalizedFunction = ();
|
||||
type FinalizedData = ();
|
||||
|
||||
fn isa(&self) -> &TargetIsa {
|
||||
&*self.isa
|
||||
}
|
||||
|
||||
fn declare_function(&mut self, name: &str, linkage: Linkage) {
|
||||
self.artifact
|
||||
.declare(name, translate_function_linkage(linkage))
|
||||
.expect("inconsistent declarations");
|
||||
}
|
||||
|
||||
fn declare_data(&mut self, name: &str, linkage: Linkage, writable: bool) {
|
||||
self.artifact
|
||||
.declare(name, translate_data_linkage(linkage, writable))
|
||||
.expect("inconsistent declarations");
|
||||
}
|
||||
|
||||
fn define_function(
|
||||
&mut self,
|
||||
name: &str,
|
||||
ctx: &cretonne_codegen::Context,
|
||||
namespace: &ModuleNamespace<Self>,
|
||||
code_size: u32,
|
||||
) -> Result<FaerieCompiledFunction, CtonError> {
|
||||
let mut code: Vec<u8> = Vec::with_capacity(code_size as usize);
|
||||
code.resize(code_size as usize, 0);
|
||||
|
||||
// Non-lexical lifetimes would obviate the braces here.
|
||||
{
|
||||
let mut reloc_sink = FaerieRelocSink {
|
||||
format: self.format,
|
||||
artifact: &mut self.artifact,
|
||||
name,
|
||||
namespace,
|
||||
};
|
||||
let mut trap_sink = FaerieTrapSink {};
|
||||
|
||||
ctx.emit_to_memory(
|
||||
code.as_mut_ptr(),
|
||||
&mut reloc_sink,
|
||||
&mut trap_sink,
|
||||
&*self.isa,
|
||||
);
|
||||
}
|
||||
|
||||
self.artifact.define(name, code).expect(
|
||||
"inconsistent declaration",
|
||||
);
|
||||
Ok(FaerieCompiledFunction {})
|
||||
}
|
||||
|
||||
fn define_data(
|
||||
&mut self,
|
||||
name: &str,
|
||||
data_ctx: &DataContext,
|
||||
namespace: &ModuleNamespace<Self>,
|
||||
) -> Result<FaerieCompiledData, CtonError> {
|
||||
let &DataDescription {
|
||||
writable: _writable,
|
||||
ref init,
|
||||
ref function_decls,
|
||||
ref data_decls,
|
||||
ref function_relocs,
|
||||
ref data_relocs,
|
||||
} = data_ctx.description();
|
||||
|
||||
let size = init.size();
|
||||
let mut bytes = Vec::with_capacity(size);
|
||||
match *init {
|
||||
Init::Uninitialized => {
|
||||
panic!("data is not initialized yet");
|
||||
}
|
||||
Init::Zeros { .. } => {
|
||||
bytes.resize(size, 0);
|
||||
}
|
||||
Init::Bytes { ref contents } => {
|
||||
bytes.extend_from_slice(contents);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Change the signature of this function to use something other
|
||||
// than `CtonError`, as `CtonError` can't convey faerie's errors.
|
||||
for &(offset, id) in function_relocs {
|
||||
let to = &namespace.get_function_decl(&function_decls[id]).name;
|
||||
self.artifact
|
||||
.link(faerie::Link {
|
||||
from: name,
|
||||
to,
|
||||
at: offset as usize,
|
||||
})
|
||||
.map_err(|_e| CtonError::InvalidInput)?;
|
||||
}
|
||||
for &(offset, id, addend) in data_relocs {
|
||||
debug_assert_eq!(
|
||||
addend,
|
||||
0,
|
||||
"faerie doesn't support addends in data section relocations yet"
|
||||
);
|
||||
let to = &namespace.get_data_decl(&data_decls[id]).name;
|
||||
self.artifact
|
||||
.link(faerie::Link {
|
||||
from: name,
|
||||
to,
|
||||
at: offset as usize,
|
||||
})
|
||||
.map_err(|_e| CtonError::InvalidInput)?;
|
||||
}
|
||||
|
||||
self.artifact.define(name, bytes).expect(
|
||||
"inconsistent declaration",
|
||||
);
|
||||
Ok(FaerieCompiledData {})
|
||||
}
|
||||
|
||||
fn write_data_funcaddr(
|
||||
&mut self,
|
||||
_data: &mut FaerieCompiledData,
|
||||
_offset: usize,
|
||||
_what: ir::FuncRef,
|
||||
) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn write_data_dataaddr(
|
||||
&mut self,
|
||||
_data: &mut FaerieCompiledData,
|
||||
_offset: usize,
|
||||
_what: ir::GlobalVar,
|
||||
_usize: binemit::Addend,
|
||||
) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn finalize_function(
|
||||
&mut self,
|
||||
_func: &FaerieCompiledFunction,
|
||||
_namespace: &ModuleNamespace<Self>,
|
||||
) {
|
||||
// Nothing to do.
|
||||
}
|
||||
|
||||
fn finalize_data(&mut self, _data: &FaerieCompiledData, _namespace: &ModuleNamespace<Self>) {
|
||||
// Nothing to do.
|
||||
}
|
||||
}
|
||||
|
||||
fn translate_function_linkage(linkage: Linkage) -> faerie::Decl {
|
||||
match linkage {
|
||||
Linkage::Import => faerie::Decl::FunctionImport,
|
||||
Linkage::Local => faerie::Decl::Function { global: false },
|
||||
Linkage::Preemptible | Linkage::Export => faerie::Decl::Function { global: true },
|
||||
}
|
||||
}
|
||||
|
||||
fn translate_data_linkage(linkage: Linkage, writable: bool) -> faerie::Decl {
|
||||
match linkage {
|
||||
Linkage::Import => faerie::Decl::DataImport,
|
||||
Linkage::Local => {
|
||||
faerie::Decl::Data {
|
||||
global: false,
|
||||
writeable: writable,
|
||||
}
|
||||
}
|
||||
Linkage::Export => {
|
||||
faerie::Decl::Data {
|
||||
global: true,
|
||||
writeable: writable,
|
||||
}
|
||||
}
|
||||
Linkage::Preemptible => {
|
||||
unimplemented!("faerie doesn't support preemptible globals yet");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct FaerieRelocSink<'a> {
|
||||
format: container::Format,
|
||||
artifact: &'a mut faerie::Artifact,
|
||||
name: &'a str,
|
||||
namespace: &'a ModuleNamespace<'a, FaerieBackend>,
|
||||
}
|
||||
|
||||
impl<'a> RelocSink for FaerieRelocSink<'a> {
|
||||
fn reloc_ebb(&mut self, _offset: CodeOffset, _reloc: Reloc, _ebb_offset: CodeOffset) {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn reloc_external(
|
||||
&mut self,
|
||||
offset: CodeOffset,
|
||||
reloc: Reloc,
|
||||
name: &ir::ExternalName,
|
||||
addend: Addend,
|
||||
) {
|
||||
let ref_name = if self.namespace.is_function(name) {
|
||||
&self.namespace.get_function_decl(name).name
|
||||
} else {
|
||||
&self.namespace.get_data_decl(name).name
|
||||
};
|
||||
let addend_i32 = addend as i32;
|
||||
debug_assert!(addend_i32 as i64 == addend);
|
||||
let raw_reloc = container::raw_relocation(reloc, self.format);
|
||||
self.artifact
|
||||
.link_with(
|
||||
faerie::Link {
|
||||
from: self.name,
|
||||
to: ref_name,
|
||||
at: offset as usize,
|
||||
},
|
||||
faerie::RelocOverride {
|
||||
reloc: raw_reloc,
|
||||
addend: addend_i32,
|
||||
},
|
||||
)
|
||||
.expect("faerie relocation error");
|
||||
}
|
||||
|
||||
fn reloc_jt(&mut self, _offset: CodeOffset, _reloc: Reloc, _jt: ir::JumpTable) {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
|
||||
struct FaerieTrapSink {}
|
||||
|
||||
impl TrapSink for FaerieTrapSink {
|
||||
// Ignore traps for now. For now, frontends should just avoid generating code that traps.
|
||||
fn trap(&mut self, _offset: CodeOffset, _srcloc: ir::SourceLoc, _code: ir::TrapCode) {}
|
||||
}
|
||||
Reference in New Issue
Block a user