From 63e23603461b8d8010caecfb1cfa63e180a6912c Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Tue, 11 Jan 2022 16:42:52 +0100 Subject: [PATCH] Remove trap from CodeSink And introduce MachBufferFinalized::traps() in the place. --- cranelift/codegen/src/binemit/memorysink.rs | 8 ------ cranelift/codegen/src/binemit/mod.rs | 5 +--- cranelift/codegen/src/context.rs | 15 +++++++---- cranelift/codegen/src/lib.rs | 2 +- cranelift/codegen/src/machinst/buffer.rs | 29 +++++++++------------ cranelift/filetests/src/test_compile.rs | 1 - 6 files changed, 25 insertions(+), 35 deletions(-) diff --git a/cranelift/codegen/src/binemit/memorysink.rs b/cranelift/codegen/src/binemit/memorysink.rs index 8b5e02e738..d462dd7000 100644 --- a/cranelift/codegen/src/binemit/memorysink.rs +++ b/cranelift/codegen/src/binemit/memorysink.rs @@ -35,7 +35,6 @@ pub struct MemoryCodeSink<'a> { /// Offset is isize because its major consumer needs it in that form. offset: isize, relocs: &'a mut dyn RelocSink, - traps: &'a mut dyn TrapSink, } impl<'a> MemoryCodeSink<'a> { @@ -48,13 +47,11 @@ impl<'a> MemoryCodeSink<'a> { pub unsafe fn new( data: *mut u8, relocs: &'a mut dyn RelocSink, - traps: &'a mut dyn TrapSink, ) -> Self { Self { data, offset: 0, relocs, - traps, } } @@ -106,11 +103,6 @@ impl<'a> CodeSink for MemoryCodeSink<'a> { let ofs = self.offset as CodeOffset; self.relocs.reloc_external(ofs, srcloc, rel, name, addend); } - - fn trap(&mut self, code: TrapCode, srcloc: SourceLoc) { - let ofs = self.offset as CodeOffset; - self.traps.trap(ofs, srcloc, code); - } } /// A `RelocSink` implementation that does nothing, which is convenient when diff --git a/cranelift/codegen/src/binemit/mod.rs b/cranelift/codegen/src/binemit/mod.rs index b2c345a37a..8fe228789f 100644 --- a/cranelift/codegen/src/binemit/mod.rs +++ b/cranelift/codegen/src/binemit/mod.rs @@ -11,7 +11,7 @@ pub use self::memorysink::{ TrapSink, }; pub use self::stack_map::StackMap; -use crate::ir::{ExternalName, SourceLoc, TrapCode}; +use crate::ir::{ExternalName, SourceLoc}; use core::fmt; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; @@ -110,7 +110,4 @@ pub trait CodeSink { /// 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); } diff --git a/cranelift/codegen/src/context.rs b/cranelift/codegen/src/context.rs index d5adc868ec..fecf0a852b 100644 --- a/cranelift/codegen/src/context.rs +++ b/cranelift/codegen/src/context.rs @@ -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, MachStackMap, MachTrap}; use crate::nan_canonicalization::do_nan_canonicalization; use crate::remove_constant_phis::do_remove_constant_phis; use crate::result::CodegenResult; @@ -194,16 +194,21 @@ impl Context { stack_maps: &mut dyn StackMapSink, ) -> CodeInfo { let _tt = timing::binemit(); - let mut sink = MemoryCodeSink::new(mem, relocs, traps); + let mut sink = MemoryCodeSink::new(mem, relocs); 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 &MachTrap { + offset, + srcloc, + code, + } in result.buffer.traps() + { + traps.trap(offset, srcloc, code); + } for &MachStackMap { offset_end, ref stack_map, diff --git a/cranelift/codegen/src/lib.rs b/cranelift/codegen/src/lib.rs index 182de0a359..7e14b5380c 100644 --- a/cranelift/codegen/src/lib.rs +++ b/cranelift/codegen/src/lib.rs @@ -88,7 +88,7 @@ pub mod verifier; pub mod write; pub use crate::entity::packed_option; -pub use crate::machinst::buffer::{MachCallSite, MachSrcLoc}; +pub use crate::machinst::buffer::{MachCallSite, MachSrcLoc, MachTrap}; pub use crate::machinst::TextSectionBuilder; mod bitset; diff --git a/cranelift/codegen/src/machinst/buffer.rs b/cranelift/codegen/src/machinst/buffer.rs index 302a81f630..6f73649ff4 100644 --- a/cranelift/codegen/src/machinst/buffer.rs +++ b/cranelift/codegen/src/machinst/buffer.rs @@ -1449,7 +1449,6 @@ impl MachBufferFinalized { // to add the appropriate relocations in this case. let mut next_reloc = 0; - let mut next_trap = 0; for (idx, byte) in self.data.iter().enumerate() { while next_reloc < self.relocs.len() && self.relocs[next_reloc].offset == idx as CodeOffset @@ -1458,16 +1457,15 @@ impl MachBufferFinalized { sink.reloc_external(reloc.srcloc, reloc.kind, &reloc.name, reloc.addend); next_reloc += 1; } - while next_trap < self.traps.len() && self.traps[next_trap].offset == idx as CodeOffset - { - let trap = &self.traps[next_trap]; - sink.trap(trap.code, trap.srcloc); - next_trap += 1; - } sink.put1(*byte); } } + /// Get the list of trap records for this code. + pub fn traps(&self) -> &[MachTrap] { + &self.traps[..] + } + /// Get the stack map metadata for this code. pub fn stack_maps(&self) -> &[MachStackMap] { &self.stack_maps[..] @@ -1521,14 +1519,14 @@ struct MachReloc { } /// A trap record resulting from a compilation. -struct MachTrap { +pub struct MachTrap { /// The offset at which the trap instruction occurs, *relative to the /// containing section*. - offset: CodeOffset, + pub offset: CodeOffset, /// The original source location. - srcloc: SourceLoc, + pub srcloc: SourceLoc, /// The trap code. - code: TrapCode, + pub code: TrapCode, } /// A call site record resulting from a compilation. @@ -2074,7 +2072,6 @@ mod test { #[derive(Default)] struct TestCodeSink { offset: CodeOffset, - traps: Vec<(CodeOffset, TrapCode)>, relocs: Vec<(CodeOffset, Reloc)>, } impl CodeSink for TestCodeSink { @@ -2084,9 +2081,6 @@ mod test { fn reloc_external(&mut self, _: SourceLoc, r: Reloc, _: &ExternalName, _: Addend) { self.relocs.push((self.offset, r)); } - fn trap(&mut self, t: TrapCode, _: SourceLoc) { - self.traps.push((self.offset, t)); - } } let mut sink = TestCodeSink::default(); @@ -2094,7 +2088,10 @@ mod test { assert_eq!(sink.offset, 4); assert_eq!( - sink.traps, + buf.traps() + .iter() + .map(|trap| (trap.offset, trap.code)) + .collect::>(), vec![ (1, TrapCode::HeapOutOfBounds), (2, TrapCode::IntegerOverflow), diff --git a/cranelift/filetests/src/test_compile.rs b/cranelift/filetests/src/test_compile.rs index 5c03e96bec..c059f60d7d 100644 --- a/cranelift/filetests/src/test_compile.rs +++ b/cranelift/filetests/src/test_compile.rs @@ -93,7 +93,6 @@ impl binemit::CodeSink for SizeSink { _addend: binemit::Addend, ) { } - fn trap(&mut self, _code: ir::TrapCode, _srcloc: ir::SourceLoc) {} } fn check_precise_output(text: &str, context: &Context) -> Result<()> {