Skip to content

Commit 38aaa6e

Browse files
committed
Remove add_call_site from CodeSink and RelocSink
And introduce MachBufferFinalized::call_sites() in the place.
1 parent 379c9c6 commit 38aaa6e

4 files changed

Lines changed: 24 additions & 38 deletions

File tree

cranelift/codegen/src/binemit/memorysink.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! `CodeSink::put*` methods, so the performance impact of the virtual callbacks is less severe.
1616
use super::{Addend, CodeInfo, CodeOffset, CodeSink, Reloc};
1717
use crate::binemit::stack_map::StackMap;
18-
use crate::ir::{ExternalName, Opcode, SourceLoc, TrapCode};
18+
use crate::ir::{ExternalName, SourceLoc, TrapCode};
1919
use core::ptr::write_unaligned;
2020

2121
/// A `CodeSink` that writes binary machine code directly into memory.
@@ -77,10 +77,6 @@ pub trait RelocSink {
7777
_: &ExternalName,
7878
_: Addend,
7979
);
80-
81-
/// Track a call site whose return address is the given CodeOffset, for the given opcode. Does
82-
/// nothing in general, only useful for certain embedders (SpiderMonkey).
83-
fn add_call_site(&mut self, _: Opcode, _: CodeOffset, _: SourceLoc) {}
8480
}
8581

8682
/// A trait for receiving trap codes and offsets.
@@ -115,15 +111,6 @@ impl<'a> CodeSink for MemoryCodeSink<'a> {
115111
let ofs = self.offset as CodeOffset;
116112
self.traps.trap(ofs, srcloc, code);
117113
}
118-
119-
fn add_call_site(&mut self, opcode: Opcode, loc: SourceLoc) {
120-
debug_assert!(
121-
opcode.is_call(),
122-
"adding call site info for a non-call instruction."
123-
);
124-
let ret_addr = self.offset as CodeOffset;
125-
self.relocs.add_call_site(opcode, ret_addr, loc);
126-
}
127114
}
128115

129116
/// A `RelocSink` implementation that does nothing, which is convenient when

cranelift/codegen/src/binemit/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub use self::memorysink::{
1111
TrapSink,
1212
};
1313
pub use self::stack_map::StackMap;
14-
use crate::ir::{ExternalName, Opcode, SourceLoc, TrapCode};
14+
use crate::ir::{ExternalName, SourceLoc, TrapCode};
1515
use core::fmt;
1616
#[cfg(feature = "enable-serde")]
1717
use serde::{Deserialize, Serialize};
@@ -113,9 +113,4 @@ pub trait CodeSink {
113113

114114
/// Add trap information for the current offset.
115115
fn trap(&mut self, _: TrapCode, _: SourceLoc);
116-
117-
/// Add a call site for a call with the given opcode, returning at the current offset.
118-
fn add_call_site(&mut self, _: Opcode, _: SourceLoc) {
119-
// Default implementation doesn't need to do anything.
120-
}
121116
}

cranelift/codegen/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub mod verifier;
8888
pub mod write;
8989

9090
pub use crate::entity::packed_option;
91-
pub use crate::machinst::buffer::MachSrcLoc;
91+
pub use crate::machinst::buffer::{MachCallSite, MachSrcLoc};
9292
pub use crate::machinst::TextSectionBuilder;
9393

9494
mod bitset;

cranelift/codegen/src/machinst/buffer.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,10 @@ impl<I: VCodeInst> MachBuffer<I> {
13501350

13511351
/// Add a call-site record at the current offset.
13521352
pub fn add_call_site(&mut self, srcloc: SourceLoc, opcode: Opcode) {
1353+
debug_assert!(
1354+
opcode.is_call(),
1355+
"adding call site info for a non-call instruction."
1356+
);
13531357
self.call_sites.push(MachCallSite {
13541358
ret_addr: self.data.len() as CodeOffset,
13551359
srcloc,
@@ -1446,7 +1450,6 @@ impl MachBufferFinalized {
14461450

14471451
let mut next_reloc = 0;
14481452
let mut next_trap = 0;
1449-
let mut next_call_site = 0;
14501453
for (idx, byte) in self.data.iter().enumerate() {
14511454
while next_reloc < self.relocs.len()
14521455
&& self.relocs[next_reloc].offset == idx as CodeOffset
@@ -1461,13 +1464,6 @@ impl MachBufferFinalized {
14611464
sink.trap(trap.code, trap.srcloc);
14621465
next_trap += 1;
14631466
}
1464-
while next_call_site < self.call_sites.len()
1465-
&& self.call_sites[next_call_site].ret_addr == idx as CodeOffset
1466-
{
1467-
let call_site = &self.call_sites[next_call_site];
1468-
sink.add_call_site(call_site.opcode, call_site.srcloc);
1469-
next_call_site += 1;
1470-
}
14711467
sink.put1(*byte);
14721468
}
14731469
}
@@ -1476,6 +1472,11 @@ impl MachBufferFinalized {
14761472
pub fn stack_maps(&self) -> &[MachStackMap] {
14771473
&self.stack_maps[..]
14781474
}
1475+
1476+
/// Get the list of call sites for this code.
1477+
pub fn call_sites(&self) -> &[MachCallSite] {
1478+
&self.call_sites[..]
1479+
}
14791480
}
14801481

14811482
/// A constant that is deferred to the next constant-pool opportunity.
@@ -1531,13 +1532,14 @@ struct MachTrap {
15311532
}
15321533

15331534
/// A call site record resulting from a compilation.
1534-
struct MachCallSite {
1535+
#[derive(Clone, Debug)]
1536+
pub struct MachCallSite {
15351537
/// The offset of the call's return address, *relative to the containing section*.
1536-
ret_addr: CodeOffset,
1538+
pub ret_addr: CodeOffset,
15371539
/// The original source location.
1538-
srcloc: SourceLoc,
1540+
pub srcloc: SourceLoc,
15391541
/// The call's opcode.
1540-
opcode: Opcode,
1542+
pub opcode: Opcode,
15411543
}
15421544

15431545
/// A source-location mapping resulting from a compilation.
@@ -2073,7 +2075,6 @@ mod test {
20732075
struct TestCodeSink {
20742076
offset: CodeOffset,
20752077
traps: Vec<(CodeOffset, TrapCode)>,
2076-
callsites: Vec<(CodeOffset, Opcode)>,
20772078
relocs: Vec<(CodeOffset, Reloc)>,
20782079
}
20792080
impl CodeSink for TestCodeSink {
@@ -2086,9 +2087,6 @@ mod test {
20862087
fn trap(&mut self, t: TrapCode, _: SourceLoc) {
20872088
self.traps.push((self.offset, t));
20882089
}
2089-
fn add_call_site(&mut self, op: Opcode, _: SourceLoc) {
2090-
self.callsites.push((self.offset, op));
2091-
}
20922090
}
20932091

20942092
let mut sink = TestCodeSink::default();
@@ -2103,7 +2101,13 @@ mod test {
21032101
(2, TrapCode::IntegerDivisionByZero)
21042102
]
21052103
);
2106-
assert_eq!(sink.callsites, vec![(2, Opcode::Call),]);
2104+
assert_eq!(
2105+
buf.call_sites()
2106+
.iter()
2107+
.map(|call_site| (call_site.ret_addr, call_site.opcode))
2108+
.collect::<Vec<_>>(),
2109+
vec![(2, Opcode::Call)]
2110+
);
21072111
assert_eq!(sink.relocs, vec![(2, Reloc::Abs4), (3, Reloc::Abs8)]);
21082112
}
21092113
}

0 commit comments

Comments
 (0)