Struct MirGenContext

Source
pub struct MirGenContext { /* private fields */ }
Available on crate feature gen only.
Expand description

The MIR context for native code generation.

Implementations§

Source§

impl MirGenContext

Source

pub fn new(ctx: MirContext) -> Self

Initialize the codegen context on top of an existing MIR context.

Source

pub fn set_opt_level(&self, level: u32)

Set the optimization level to use.

See details in MIR documentation.

Source

pub fn enable_debug(&self, level: c_int)

Available on crate feature gen-debug only.

Enable internal debug logging with specific level.

Logs will be collected in memory and can be retrieved by Self::get_debug_output.

Source

pub fn get_debug_output(&self) -> String

Available on crate feature gen-debug only.

Retrieve the debug logs collected so far.

Note: logs will not be cleared after the call. Memory can only be freed after destroyed the context.

§Panics

Panics if debug logging is not enabled before.

Link loaded modules and external names, preparing to be codegen.

§Panics

Panic from C on unresolved names.

Link loaded modules and external names with custom resolver, preparing to be codegen.

§Safety

resolver must return valid function pointers with prototype expected by generated code, or NULL if unresolved.

§Panics

Panic from C on unresolved names.

Source

pub fn codegen_func(&self, func: FuncItemRef<'_>) -> *mut c_void

Generate native code and return the function pointer to func.

This function is idempotic, that is, once code is generated for func, all later calls will do nothing and simply return the same function pointer.

Methods from Deref<Target = MirContext>§

Available on crate feature interp only.

Link loaded modules and external names, preparing to be interpreted.

§Panics

Panic from C on unresolved names.

Available on crate feature interp only.

Link loaded modules and external names with custom resolver, preparing to be interpreted.

§Safety

resolver must return valid function pointers with prototype expected by generated code, or NULL if unresolved.

§Panics

Panic from C on unresolved names.

Source

pub unsafe fn interpret_unchecked( &self, func: FuncItemRef<'_>, results: &mut [Val], args: &[Val], )

Available on crate feature interp only.

Execute a function using the interpreter.

§Safety

The types and lengths of arguments and results must match the function signature.

§Panics

Panic from C on (detectable) interpretation errors.

Source

pub fn as_raw(&self) -> *mut MIR_context

Get the underlying pointer for FFI.

Source

pub fn dump(&self) -> String

Dump the content in a textual representation for human consumption.

Source

pub fn get_modules(&self) -> Vec<MirModuleRef<'_>>

Get the list of all modules in the context.

This includes all modules created in this context, not necessarily loaded or linked.

Source

pub fn serialize(&self) -> Vec<u8>

Available on crate feature io only.

Serialize the context (including all modules) into bytes.

The serialization format is stable across executions, but may not be stable across MIR versions.

Source

pub unsafe fn deserialize(&self, bytes: &[u8])

Available on crate feature io only.

Deserialize context or modules from bytes.

It will create one or more modules in bytes. The deserialized modules will be as if they are created manually, not loaded or linked yet.

§Safety

bytes must be trusted and produced from previous serialization.

§Panics

Panic if there is any unfinished module. Panic from C if the bytes cannot be parsed or contain errors.

Source

pub fn enter_new_module(&self, name: &CStr) -> MirModuleBuilder<'_>

Create a new module and enter it.

The MIR context is stateful. When entering a new module, it should be correctly finished before creating another module. See MirModuleBuilder for more details.

§Panics

Panic if there is any unfinished module.

Source

pub fn load_module(&self, module: MirModuleRef<'_>)

Load an MIR module for linking.

Source

pub unsafe fn load_external(&self, name: &CStr, addr: *mut c_void)

Load an external name pointing to addr for linking.

§Example
// let ctx: &MirContext;
unsafe { ctx.load_external(c"memset", libc::memset as _) };
§Safety

addr must be a valid function pointer with prototype expected by generated code.

Link loaded modules and external names with given custom interface and resolver.

§Safety

set_interface should be one of MIR_set_*_interface. resolver must return valid function pointers with prototype expected by generated code.

Trait Implementations§

Source§

impl Debug for MirGenContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for MirGenContext

Source§

type Target = MirContext

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl Drop for MirGenContext

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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<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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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