[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

@@ -4,7 +4,6 @@
use crate::subtest::{run_filecheck, Context, SubTest};
use anyhow::{bail, Result};
use cranelift_codegen::binemit::CodeInfo;
use cranelift_codegen::ir;
use cranelift_reader::{TestCommand, TestOption};
use log::info;
@@ -54,17 +53,12 @@ impl SubTest for TestCompile {
// With `MachBackend`s, we need to explicitly request dissassembly results.
comp_ctx.set_disasm(true);
let CodeInfo { total_size, .. } = comp_ctx
let compiled_code = comp_ctx
.compile(isa)
.map_err(|e| crate::pretty_anyhow_error(&comp_ctx.func, e))?;
.map_err(|e| crate::pretty_anyhow_error(&e.func, e.inner))?;
let total_size = compiled_code.code_info().total_size;
let disasm = comp_ctx
.mach_compile_result
.as_ref()
.unwrap()
.disasm
.as_ref()
.unwrap();
let disasm = compiled_code.disasm.as_ref().unwrap();
info!("Generated {} bytes of code:\n{}", total_size, disasm);