Skip to main content

MachBuffer

Struct MachBuffer 

Source
pub struct MachBuffer<I: VCodeInst> { /* private fields */ }
Expand description

A buffer of output to be produced, fixed up, and then emitted to a CodeSink in bulk.

This struct uses SmallVecs to support small-ish function bodies without any heap allocation. As such, it will be several kilobytes large. This is likely fine as long as it is stack-allocated for function emission then thrown away; but beware if many buffer objects are retained persistently.

Implementations§

Source§

impl<I: VCodeInst> MachBuffer<I>

Source

pub fn new() -> MachBuffer<I>

Create a new section, known to start at start_offset and with a size limited to length_limit.

Source

pub fn cur_offset(&self) -> CodeOffset

Current offset from start of buffer.

Source

pub fn put1(&mut self, value: u8)

Add a byte.

Source

pub fn put2(&mut self, value: u16)

Add 2 bytes.

Source

pub fn put4(&mut self, value: u32)

Add 4 bytes.

Source

pub fn put8(&mut self, value: u64)

Add 8 bytes.

Source

pub fn put_data(&mut self, data: &[u8])

Add a slice of bytes.

Source

pub fn get_appended_space(&mut self, len: usize) -> &mut [u8] ⓘ

Reserve appended space and return a mutable slice referring to it.

Source

pub fn align_to(&mut self, align_to: CodeOffset)

Align up to the given alignment.

Source

pub fn start_patchable(&mut self) -> OpenPatchRegion

Begin a region of patchable code. There is one requirement for the code that is emitted: It must not introduce any instructions that could be chomped (branches are an example of this). In other words, you must not call MachBuffer::add_cond_branch or MachBuffer::add_uncond_branch between calls to this method and MachBuffer::end_patchable.

Source

pub fn end_patchable(&mut self, open: OpenPatchRegion) -> PatchRegion

End a region of patchable code, yielding a PatchRegion value that can be consumed later to produce a one-off mutable slice to the associated region of the data buffer.

Source

pub fn get_label(&mut self) -> MachLabel

Allocate a Label to refer to some offset. May not be bound to a fixed offset yet.

Source

pub fn reserve_labels_for_blocks(&mut self, blocks: usize)

Reserve the first N MachLabels for blocks.

Source

pub fn register_constants(&mut self, constants: &VCodeConstants)

Registers metadata in this MachBuffer about the constants provided.

This will record the size/alignment of all constants which will prepare them for emission later on.

Source

pub fn register_constant( &mut self, constant: &VCodeConstant, data: &VCodeConstantData, )

Similar to MachBuffer::register_constants but registers a single constant metadata. This function is useful in situations where not all constants are known at the time of emission.

Source

pub fn get_label_for_constant(&mut self, constant: VCodeConstant) -> MachLabel

Returns a label that can be used to refer to the constant provided.

This will automatically defer a new constant to be emitted for constant if it has not been previously emitted. Note that this function may return a different label for the same constant at different points in time. The label is valid to use only from the current location; the MachBuffer takes care to emit the same constant multiple times if needed so the constant is always in range.

Source

pub fn bind_label(&mut self, label: MachLabel, ctrl_plane: &mut ControlPlane)

Bind a label to the current offset. A label can only be bound once.

Source

pub fn use_label_at_offset( &mut self, offset: CodeOffset, label: MachLabel, kind: I::LabelUse, )

Emit a reference to the given label with the given reference type (i.e., branch-instruction format) at the current offset. This is like a relocation, but handled internally.

This can be called before the branch is actually emitted; fixups will not happen until an island is emitted or the buffer is finished.

Source

pub fn add_uncond_branch( &mut self, start: CodeOffset, end: CodeOffset, target: MachLabel, )

Inform the buffer of an unconditional branch at the given offset, targeting the given label. May be used to optimize branches. The last added label-use must correspond to this branch. This must be called when the current offset is equal to start; i.e., before actually emitting the branch. This implies that for a branch that uses a label and is eligible for optimizations by the MachBuffer, the proper sequence is:

  • Call use_label_at_offset() to emit the fixup record.
  • Call add_uncond_branch() to make note of the branch.
  • Emit the bytes for the branch’s machine code.

Additional requirement: no labels may be bound between start and end (exclusive on both ends).

Source

pub fn add_cond_branch( &mut self, start: CodeOffset, end: CodeOffset, target: MachLabel, inverted: &[u8], )

Inform the buffer of a conditional branch at the given offset, targeting the given label. May be used to optimize branches. The last added label-use must correspond to this branch.

Additional requirement: no labels may be bound between start and end (exclusive on both ends).

Source

pub fn optimize_branches(&mut self, ctrl_plane: &mut ControlPlane)

Performs various optimizations on branches pointing at the current label.

Source

pub fn defer_trap(&mut self, code: TrapCode) -> MachLabel

Emit a trap at some point in the future with the specified code and stack map.

This function returns a MachLabel which will be the future address of the trap. Jumps should refer to this label, likely by using the MachBuffer::use_label_at_offset method, to get a relocation patched in once the address of the trap is known.

This will batch all traps into the end of the function.

Source

pub fn island_needed(&self, distance: CodeOffset) -> bool

Is an island needed within the next N bytes?

Source

pub fn emit_island( &mut self, distance: CodeOffset, ctrl_plane: &mut ControlPlane, )

Emit all pending constants and required pending veneers.

Should only be called if island_needed() returns true, i.e., if we actually reach a deadline. It’s not necessarily a problem to do so otherwise but it may result in unnecessary work during emission.

The current code-emission position must be a “safe” location for an island: i.e., no fallthrough into the island contents from the previous instruction is possible. Callers emitting inside a basic block should emit a jump-around branch.

Source

pub fn finish( self, constants: &VCodeConstants, ctrl_plane: &mut ControlPlane, ) -> MachBufferFinalized

Finish any deferred emissions and/or fixups.

Source

pub fn add_reloc_at_offset<T: Into<RelocTarget> + Clone>( &mut self, offset: CodeOffset, kind: Reloc, target: &T, addend: Addend, )

Add an external relocation at the given offset.

Source

pub fn add_reloc<T: Into<RelocTarget> + Clone>( &mut self, kind: Reloc, target: &T, addend: Addend, )

Add an external relocation at the current offset.

Source

pub fn add_trap(&mut self, code: TrapCode)

Add a trap record at the current offset.

Source

pub fn add_call_site(&mut self)

Add a call-site record at the current offset.

Source

pub fn add_try_call_site( &mut self, frame_offset: Option<u32>, exception_handlers: impl Iterator<Item = MachExceptionHandler>, )

Add a call-site record at the current offset with exception handlers.

Source

pub fn add_patchable_call_site(&mut self, len: u32)

Add a patchable call record at the current offset. The actual call is expected to have been emitted; the VCodeInst trait specifies how to NOP it out, and we carry that information to the finalized Machbuffer.

Source

pub fn add_unwind(&mut self, unwind: UnwindInst)

Add an unwind record at the current offset.

Source

pub fn start_srcloc(&mut self, loc: RelSourceLoc) -> (CodeOffset, RelSourceLoc)

Set the SourceLoc for code from this offset until the offset at the next call to end_srcloc(). Returns the current CodeOffset and RelSourceLoc.

Source

pub fn end_srcloc(&mut self)

Mark the end of the SourceLoc segment started at the last start_srcloc() call.

Source

pub fn push_user_stack_map( &mut self, frame_layout: &FrameLayout, pc: CodeOffset, stack_map: UserStackMap, )

Push a user stack map onto this buffer.

The stack map is associated with the given pc code offset. For a call, this is the return address: the PC of the instruction just after the call. For a resumable fault (such as interrupt_poll), this is the PC of the faulting instruction itself. For a stack switch, this is the PC at which execution resumes after the switch. In all cases, pc identifies the machine state described by the map.

Stack maps must be pushed in strictly increasing pc order.

Source

pub fn push_user_stack_map_sp_relative( &mut self, return_addr: CodeOffset, frame_size: u32, stack_map: UserStackMap, )

Push a user stack map whose offsets are already relative to the stack pointer at the safepoint, with an explicitly provided frame size.

Source

pub fn push_debug_tags(&mut self, pos: MachDebugTagPos, tags: &[DebugTag])

Push a debug tag associated with the current buffer offset.

Source

pub fn set_log2_min_function_alignment(&mut self, align_to: u8)

Increase the alignment of the buffer to the given alignment if bigger than the current alignment.

Source

pub fn set_frame_layout(&mut self, frame_layout: MachBufferFrameLayout)

Set the frame layout metadata.

Trait Implementations§

Source§

impl<I: VCodeInst> Deref for MachBuffer<I>

Source§

type Target = MachBufferInner

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<I: VCodeInst> DerefMut for MachBuffer<I>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<I: VCodeInst> Extend<u8> for MachBuffer<I>

Source§

fn extend<T: IntoIterator<Item = u8>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more

Auto Trait Implementations§

§

impl<I> Forget for MachBuffer<I>

§

impl<I> Freeze for MachBuffer<I>
where SmallVec<[MachLabelFixup<I>; 16]>: Freeze, BinaryHeap<MachLabelFixup<I>>: Freeze,

§

impl<I> Move for MachBuffer<I>

§

impl<I> RefUnwindSafe for MachBuffer<I>
where SmallVec<[MachLabelFixup<I>; 16]>: RefUnwindSafe, BinaryHeap<MachLabelFixup<I>>: RefUnwindSafe,

§

impl<I> Send for MachBuffer<I>
where SmallVec<[MachLabelFixup<I>; 16]>: Send, BinaryHeap<MachLabelFixup<I>>: Send,

§

impl<I> Sync for MachBuffer<I>
where SmallVec<[MachLabelFixup<I>; 16]>: Sync, BinaryHeap<MachLabelFixup<I>>: Sync,

§

impl<I> Unpin for MachBuffer<I>
where SmallVec<[MachLabelFixup<I>; 16]>: Unpin, BinaryHeap<MachLabelFixup<I>>: Unpin,

§

impl<I> UnsafeUnpin for MachBuffer<I>
where SmallVec<[MachLabelFixup<I>; 16]>: UnsafeUnpin, BinaryHeap<MachLabelFixup<I>>: UnsafeUnpin,

§

impl<I> UnwindSafe for MachBuffer<I>
where SmallVec<[MachLabelFixup<I>; 16]>: UnwindSafe, BinaryHeap<MachLabelFixup<I>>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.