Skip to main content

CodeBuffer

Struct CodeBuffer 

Source
pub struct CodeBuffer { /* 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 CodeBuffer

Source

pub fn new(env: Environment) -> Self

Creates a buffer for env.

Source

pub fn host() -> Self

Creates a buffer for the host target.

Source

pub fn clear(&mut self)

Source

pub fn error(&self) -> Option<&AsmError>

Returns the first error recorded by a void assembler operation.

Source

pub fn env(&self) -> &Environment

Source

pub fn data(&self) -> &[u8] ⓘ

Source

pub fn relocs(&self) -> &[AsmReloc]

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

pub fn add_symbol( &mut self, name: impl Into<ExternalName>, distance: RelocDistance, ) -> Sym

Source

pub fn extern_sym( &mut self, name: impl Into<Cow<'static, str>>, distance: RelocDistance, ) -> Sym

Declares an external symbol by name, deduplicating: repeated calls with the same name return the same Sym (the distance of the first declaration wins).

The returned symbol can be passed to the architecture assemblers (e.g. ptr64_sym on x86) which then record the appropriate relocation based on the symbol’s distance.

Source

pub fn extern_user( &mut self, namespace: u32, index: u32, distance: RelocDistance, ) -> Sym

Declares an external symbol by user namespace+index, deduplicating: repeated calls with the same key return the same Sym (the distance of the first declaration wins).

Source

pub fn bind_symbol(&mut self, name: impl Into<ExternalName>, label: Label)

Exports label under name, making it a defined symbol that other modules can resolve at link time (see crate::core::linker::Linker).

name may be a string (ExternalName::Symbol) or a user key (ExternalName::User).

Source

pub fn symbol_distance(&self, sym: Sym) -> Option<RelocDistance>

Source

pub fn symbol_name(&self, sym: Sym) -> Option<&ExternalName>

Source

pub fn get_label(&mut self) -> Label

Source

pub fn is_bound(&self, label: Label) -> bool

Source

pub fn label_count(&self) -> u32

Number of labels created so far; label ids below this count are valid.

Source

pub fn get_label_for_constant(&mut self, constant: Constant) -> Label

Source

pub fn add_constant(&mut self, constant: impl Into<ConstantData>) -> Constant

Source

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

Source

pub fn try_align_to(&mut self, align_to: CodeOffset) -> Result<(), AsmError>

Align up to the given alignment.

Source

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

Source

pub fn cur_offset(&self) -> CodeOffset

Source

pub fn try_bind_label(&mut self, label: Label) -> Result<(), AsmError>

Source

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

Source

pub fn label_offset(&self, label: Label) -> u32

Source

pub fn add_reloc(&mut self, kind: Reloc, target: RelocTarget, addend: i64)

Source

pub fn add_reloc_at_offset( &mut self, offset: CodeOffset, kind: Reloc, target: RelocTarget, addend: i64, )

Source

pub fn reserve_patch_region( &mut self, size: CodeOffset, align: CodeOffset, ) -> Result<PatchableRegion, AsmError>

Reserves a nop-filled region for later rewriting.

Rewrite it after Self::finish with rewrite_region at CodeBufferFinalized::location_of the returned mark.

Source

pub fn emit_veneer( &mut self, label: Label, offset: CodeOffset, kind: LabelUse, ) -> Result<(), AsmError>

Emits a “veneer” the kind code at offset to jump to label.

This will generate extra machine code, using kind, to get a larger-jump-kind than kind allows. The code at offset is then patched to jump to our new code, and then the new code is enqueued for a fixup to get processed at some later time.

Source

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

Is an island needed within the next N bytes?

Source

pub fn emit_island(&mut self, distance: CodeOffset) -> Result<(), AsmError>

Emit all pending constants and required pending veneers.

Source

pub fn finish(&mut self) -> Result<CodeBufferFinalized, AsmError>

Finalizes the buffer: lays out pending constants, applies fixups and emits the veneers they need.

The buffer keeps its recordings (and allocations) so it can be used again after clear.

Source

pub fn finish_owned(&mut self) -> Result<CodeBufferFinalized, AsmError>

Like finish, but moves the recording vectors into the result instead of cloning them.

The buffer is left empty (without its heap allocations), so this is the cheap way to finish when the buffer can be dropped afterwards.

Auto Trait Implementations§

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<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.