[cranelift] Rejigger the compile API (#4540)

* Move `emit_to_memory` to `MachCompileResult`

This small refactoring makes it clearer to me that emitting to memory
doesn't require anything else from the compilation `Context`. While it's
a trivial change, it's a small public API change that shouldn't cause
too much trouble, and doesn't seem RFC-worthy. Happy to hear different
opinions about this, though!

* hide the MachCompileResult behind a method

* Add a `CompileError` wrapper type that references a `Function`

* Rename MachCompileResult to CompiledCode

* Additionally remove the last unsafe API in cranelift-codegen
This commit is contained in:
Benjamin Bouvier
2022-08-02 21:05:40 +02:00
committed by GitHub
parent 37cd96beff
commit ff37c9d8a4
17 changed files with 156 additions and 198 deletions

View File

@@ -272,7 +272,7 @@ pub trait MachInstEmitState<I: MachInst>: Default + Clone + Debug {
/// The result of a `MachBackend::compile_function()` call. Contains machine
/// code (as bytes) and a disassembly, if requested.
pub struct MachCompileResult {
pub struct CompiledCode {
/// Machine code.
pub buffer: MachBufferFinalized,
/// Size of stack frame, in bytes.
@@ -299,13 +299,18 @@ pub struct MachCompileResult {
pub bb_edges: Vec<(CodeOffset, CodeOffset)>,
}
impl MachCompileResult {
impl CompiledCode {
/// Get a `CodeInfo` describing section sizes from this compilation result.
pub fn code_info(&self) -> CodeInfo {
CodeInfo {
total_size: self.buffer.total_size(),
}
}
/// Returns a reference to the machine code generated for this function compilation.
pub fn code_buffer(&self) -> &[u8] {
self.buffer.data()
}
}
/// An object that can be used to create the text section of an executable.