Trait PreludeCodegen

Source
pub trait PreludeCodegen: Clone {
    // Provided methods
    fn usize_type<'c>(&self, session: &TypingSession<'c, '_>) -> IntType<'c> { ... }
    fn qubit_type<'c>(
        &self,
        session: &TypingSession<'c, '_>,
    ) -> impl BasicType<'c> { ... }
    fn error_type<'c>(
        &self,
        session: &TypingSession<'c, '_>,
    ) -> Result<impl BasicType<'c>> { ... }
    fn string_type<'c>(
        &self,
        session: &TypingSession<'c, '_>,
    ) -> Result<impl BasicType<'c>> { ... }
    fn emit_print<H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'_, '_, H>,
        text: BasicValueEnum<'_>,
    ) -> Result<()> { ... }
    fn emit_const_error<'c, H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'c, '_, H>,
        err: &ConstError,
    ) -> Result<BasicValueEnum<'c>> { ... }
    fn emit_make_error<'c, H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'c, '_, H>,
        signal: BasicValueEnum<'c>,
        message: BasicValueEnum<'c>,
    ) -> Result<BasicValueEnum<'c>> { ... }
    fn emit_panic<H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'_, '_, H>,
        err: BasicValueEnum<'_>,
    ) -> Result<()> { ... }
    fn emit_exit<H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'_, '_, H>,
        err: BasicValueEnum<'_>,
    ) -> Result<()> { ... }
    fn emit_const_string<'c, H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'c, '_, H>,
        str: &ConstString,
    ) -> Result<BasicValueEnum<'c>> { ... }
    fn emit_barrier<'c, H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'c, '_, H>,
        args: EmitOpArgs<'c, '_, ExtensionOp, H>,
    ) -> Result<()> { ... }
}
Expand description

A helper trait for customising the lowering hugr_core::extension::prelude types, CustomConsts, and ops.

All methods have sensible defaults provided, and DefaultPreludeCodegen is a trivial implementation of this trait which delegates everything to those default implementations.

Provided Methods§

Source

fn usize_type<'c>(&self, session: &TypingSession<'c, '_>) -> IntType<'c>

Return the llvm type of hugr_core::extension::prelude::usize_t. That type must be an IntType.

Source

fn qubit_type<'c>(&self, session: &TypingSession<'c, '_>) -> impl BasicType<'c>

Return the llvm type of hugr_core::extension::prelude::qb_t.

Source

fn error_type<'c>( &self, session: &TypingSession<'c, '_>, ) -> Result<impl BasicType<'c>>

Return the llvm type of hugr_core::extension::prelude::error_type().

The returned type must always match the type of the returned value of Self::emit_const_error, and the err argument of Self::emit_panic.

The default implementation is a struct type with an i32 field and an i8* field for the code and message.

Source

fn string_type<'c>( &self, session: &TypingSession<'c, '_>, ) -> Result<impl BasicType<'c>>

Return the llvm type of hugr_core::extension::prelude::string_type().

The returned type must always match the type of the returned value of Self::emit_const_string, and the text argument of Self::emit_print.

The default implementation is i8*.

Source

fn emit_print<H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'_, '_, H>, text: BasicValueEnum<'_>, ) -> Result<()>

Source

fn emit_const_error<'c, H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'c, '_, H>, err: &ConstError, ) -> Result<BasicValueEnum<'c>>

Emit instructions to materialise an LLVM value representing err.

The type of the returned value must match Self::error_type.

The default implementation materialises an LLVM struct with the ConstError::signal and ConstError::message of err.

Source

fn emit_make_error<'c, H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'c, '_, H>, signal: BasicValueEnum<'c>, message: BasicValueEnum<'c>, ) -> Result<BasicValueEnum<'c>>

Emit instructions to construct an error value from a signal and message.

The type of the returned value must match Self::error_type.

The default implementation constructs a struct with the given signal and message.

Source

fn emit_panic<H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'_, '_, H>, err: BasicValueEnum<'_>, ) -> Result<()>

Emit instructions to halt execution with the error err.

The type of err must match that returned from Self::error_type.

The default implementation emits calls to libc’s printf and abort.

Note that implementations of emit_panic must not emit unreachable terminators, that, if appropriate, is the responsibility of the caller.

Source

fn emit_exit<H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'_, '_, H>, err: BasicValueEnum<'_>, ) -> Result<()>

Emit instructions to halt execution with the error err.

The type of err must match that returned from Self::error_type.

The default implementation emits calls to libc’s printf and abort, matching the default implementation of Self::emit_panic.

Note that implementations of emit_panic must not emit unreachable terminators, that, if appropriate, is the responsibility of the caller.

Source

fn emit_const_string<'c, H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'c, '_, H>, str: &ConstString, ) -> Result<BasicValueEnum<'c>>

Emit instructions to materialise an LLVM value representing str.

The type of the returned value must match Self::string_type.

The default implementation creates a global C string.

Source

fn emit_barrier<'c, H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'c, '_, H>, args: EmitOpArgs<'c, '_, ExtensionOp, H>, ) -> Result<()>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§