Skip to content

Commit

Permalink
Merge pull request #3680 from bjorn3/remove_code_sink
Browse files Browse the repository at this point in the history
Remove the CodeSink interface in favor of MachBufferFinalized
  • Loading branch information
cfallin authored Jan 12, 2022
2 parents eeca41d + 17021bc commit 13f17db
Show file tree
Hide file tree
Showing 30 changed files with 342 additions and 899 deletions.
189 changes: 0 additions & 189 deletions cranelift/codegen/src/binemit/memorysink.rs

This file was deleted.

41 changes: 0 additions & 41 deletions cranelift/codegen/src/binemit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
//! The `binemit` module contains code for translating Cranelift's intermediate representation into
//! binary machine code.
mod memorysink;
mod stack_map;

pub use self::memorysink::{
MemoryCodeSink, NullRelocSink, NullStackMapSink, NullTrapSink, RelocSink, StackMapSink,
TrapSink,
};
pub use self::stack_map::StackMap;
use crate::ir::{ExternalName, Opcode, SourceLoc, TrapCode};
use core::fmt;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -99,38 +93,3 @@ pub struct CodeInfo {
/// Number of bytes in total.
pub total_size: CodeOffset,
}

/// Abstract interface for adding bytes to the code segment.
///
/// A `CodeSink` will receive all of the machine code for a function. It also accepts relocations
/// which are locations in the code section that need to be fixed up when linking.
pub trait CodeSink {
/// Get the current position.
fn offset(&self) -> CodeOffset;

/// Add 1 byte to the code section.
fn put1(&mut self, _: u8);

/// Add 2 bytes to the code section.
fn put2(&mut self, _: u16);

/// Add 4 bytes to the code section.
fn put4(&mut self, _: u32);

/// Add 8 bytes to the code section.
fn put8(&mut self, _: u64);

/// Add a relocation referencing an external symbol plus the addend at the current offset.
fn reloc_external(&mut self, _: SourceLoc, _: Reloc, _: &ExternalName, _: Addend);

/// Add trap information for the current offset.
fn trap(&mut self, _: TrapCode, _: SourceLoc);

/// Read-only data output is complete, we're done.
fn end_codegen(&mut self);

/// Add a call site for a call with the given opcode, returning at the current offset.
fn add_call_site(&mut self, _: Opcode, _: SourceLoc) {
// Default implementation doesn't need to do anything.
}
}
39 changes: 10 additions & 29 deletions cranelift/codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! contexts concurrently. Typically, you would have one context per compilation thread and only a
//! single ISA instance.
use crate::binemit::{CodeInfo, MemoryCodeSink, RelocSink, StackMapSink, TrapSink};
use crate::binemit::CodeInfo;
use crate::dce::do_dce;
use crate::dominator_tree::DominatorTree;
use crate::flowgraph::ControlFlowGraph;
Expand All @@ -18,7 +18,7 @@ use crate::isa::TargetIsa;
use crate::legalizer::simple_legalize;
use crate::licm::do_licm;
use crate::loop_analysis::LoopAnalysis;
use crate::machinst::{MachCompileResult, MachStackMap};
use crate::machinst::MachCompileResult;
use crate::nan_canonicalization::do_nan_canonicalization;
use crate::remove_constant_phis::do_remove_constant_phis;
use crate::result::CodegenResult;
Expand Down Expand Up @@ -111,16 +111,11 @@ impl Context {
&mut self,
isa: &dyn TargetIsa,
mem: &mut Vec<u8>,
relocs: &mut dyn RelocSink,
traps: &mut dyn TrapSink,
stack_maps: &mut dyn StackMapSink,
) -> CodegenResult<()> {
let info = self.compile(isa)?;
let old_len = mem.len();
mem.resize(old_len + info.total_size as usize, 0);
let new_info = unsafe {
self.emit_to_memory(mem.as_mut_ptr().add(old_len), relocs, traps, stack_maps)
};
let new_info = unsafe { self.emit_to_memory(mem.as_mut_ptr().add(old_len)) };
debug_assert!(new_info == info);
Ok(())
}
Expand Down Expand Up @@ -186,32 +181,18 @@ impl Context {
/// and it can't guarantee that the `mem` pointer is valid.
///
/// Returns information about the emitted code and data.
pub unsafe fn emit_to_memory(
&self,
mem: *mut u8,
relocs: &mut dyn RelocSink,
traps: &mut dyn TrapSink,
stack_maps: &mut dyn StackMapSink,
) -> CodeInfo {
#[deny(unsafe_op_in_unsafe_fn)]
pub unsafe fn emit_to_memory(&self, mem: *mut u8) -> CodeInfo {
let _tt = timing::binemit();
let mut sink = MemoryCodeSink::new(mem, relocs, traps);
let result = self
.mach_compile_result
.as_ref()
.expect("only using mach backend now");
result.buffer.emit(&mut sink);
let info = sink.info;
// New backends do not emit StackMaps through the `CodeSink` because its interface
// requires `Value`s; instead, the `StackMap` objects are directly accessible via
// `result.buffer.stack_maps()`.
for &MachStackMap {
offset_end,
ref stack_map,
..
} in result.buffer.stack_maps()
{
stack_maps.add_stack_map(offset_end, stack_map.clone());
}
let info = result.code_info();

let mem = unsafe { std::slice::from_raw_parts_mut(mem, info.total_size as usize) };
mem.copy_from_slice(result.buffer.data());

info
}

Expand Down
5 changes: 1 addition & 4 deletions cranelift/codegen/src/isa/aarch64/inst/emit_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::ir::types::*;
use crate::ir::TrapCode;
use crate::isa::aarch64::inst::*;
use crate::isa::test_utils;
use crate::isa::CallConv;
use crate::settings;

Expand Down Expand Up @@ -6523,12 +6522,10 @@ fn test_aarch64_binemit() {
let actual_printing = insn.show_rru(Some(&rru));
assert_eq!(expected_printing, actual_printing);

let mut sink = test_utils::TestCodeSink::new();
let mut buffer = MachBuffer::new();
insn.emit(&mut buffer, &emit_info, &mut Default::default());
let buffer = buffer.finish();
buffer.emit(&mut sink);
let actual_encoding = &sink.stringify();
let actual_encoding = &buffer.stringify_code_bytes();
assert_eq!(expected_encoding, actual_encoding);
}
}
Expand Down
6 changes: 3 additions & 3 deletions cranelift/codegen/src/isa/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl TargetIsa for AArch64Backend {
Some(UnwindInfo::SystemV(
crate::isa::unwind::systemv::create_unwind_info_from_insts(
&result.buffer.unwind_info[..],
result.buffer.data.len(),
result.buffer.data().len(),
&mapper,
)?,
))
Expand Down Expand Up @@ -216,7 +216,7 @@ mod test {
isa_flags,
);
let buffer = backend.compile_function(&mut func, false).unwrap().buffer;
let code = &buffer.data[..];
let code = buffer.data();

// mov x1, #0x1234
// add w0, w0, w1
Expand Down Expand Up @@ -271,7 +271,7 @@ mod test {
let result = backend
.compile_function(&mut func, /* want_disasm = */ false)
.unwrap();
let code = &result.buffer.data[..];
let code = result.buffer.data();

// mov x1, #0x1234 // #4660
// add w0, w0, w1
Expand Down
5 changes: 1 addition & 4 deletions cranelift/codegen/src/isa/arm32/inst/emit_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::isa::arm32::inst::*;
use crate::isa::test_utils;
use crate::settings;

use alloc::vec::Vec;
Expand Down Expand Up @@ -1948,12 +1947,10 @@ fn test_arm32_emit() {
// Check the printed text is as expected.
let actual_printing = insn.show_rru(Some(&rru));
assert_eq!(expected_printing, actual_printing);
let mut sink = test_utils::TestCodeSink::new();
let mut buffer = MachBuffer::new();
insn.emit(&mut buffer, &flags, &mut Default::default());
let buffer = buffer.finish();
buffer.emit(&mut sink);
let actual_encoding = &sink.stringify();
let actual_encoding = &buffer.stringify_code_bytes();
assert_eq!(expected_encoding, actual_encoding, "{}", expected_printing);
}
}
3 changes: 0 additions & 3 deletions cranelift/codegen/src/isa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ pub mod unwind;

mod call_conv;

#[cfg(test)]
mod test_utils;

/// Returns a builder that can create a corresponding `TargetIsa`
/// or `Err(LookupError::SupportDisabled)` if not enabled.
macro_rules! isa_builder {
Expand Down
Loading

0 comments on commit 13f17db

Please sign in to comment.