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 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 reserve_labels_for_constants(&mut self, constants: &VCodeConstants)

Reserve the next N MachLabels for constants.

source

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

Retrieve the reserved label for a constant.

source

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

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, targetting 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, targetting 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 defer_constant( &mut self, label: MachLabel, align: CodeOffset, data: &[u8], max_distance: CodeOffset )

Emit a constant at some point in the future, binding the given label to its offset. The constant will be placed at most max_distance from the current offset.

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)

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.

source

pub fn finish(self) -> MachBufferFinalized<Stencil>

Finish any deferred emissions and/or fixups.

source

pub fn add_reloc(&mut self, kind: Reloc, name: &ExternalName, 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, opcode: Opcode)

Add a call-site record at the current offset.

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)

Set the SourceLoc for code from this offset until the offset at the next call to end_srcloc().

source

pub fn end_srcloc(&mut self)

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

source

pub fn add_stack_map(&mut self, extent: StackMapExtent, stack_map: StackMap)

Add stack map metadata for this program point: a set of stack offsets (from SP upward) that contain live references.

The offset_to_fp value is the offset from the nominal SP (at which the stack_offsets are based) and the FP value. By subtracting offset_to_fp from each stack_offsets element, one can obtain live-reference offsets from FP instead.

Trait Implementations§

source§

impl ByteSink for MachBuffer<Inst>

source§

fn put1(&mut self, value: u8)

Add 1 byte to the code section.
source§

fn put2(&mut self, value: u16)

Add 2 bytes to the code section.
source§

fn put4(&mut self, value: u32)

Add 4 bytes to the code section.
source§

fn put8(&mut self, value: u64)

Add 8 bytes to the code section.

Auto Trait Implementations§

§

impl<I> RefUnwindSafe for MachBuffer<I>where <I as MachInst>::LabelUse: RefUnwindSafe,

§

impl<I> Send for MachBuffer<I>where <I as MachInst>::LabelUse: Send,

§

impl<I> Sync for MachBuffer<I>where <I as MachInst>::LabelUse: Sync,

§

impl<I> Unpin for MachBuffer<I>where <I as MachInst>::LabelUse: Unpin,

§

impl<I> UnwindSafe for MachBuffer<I>where <I as MachInst>::LabelUse: UnwindSafe + RefUnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.