wings-codegen 0.0.4

Cranelift AOT machine-code backend for Wings VM
Documentation
use super::*;
use crate::emit::{
    BudgetVars, ScalarParams, ShadowRoots, emit_pure_scalar, is_pure_scalar, poll_control,
};
use crate::sra::RecordAnalysis;

pub(super) struct SuperblockResult {
    pub(super) next_index: usize,
    pub(super) tail_polled: bool,
}

#[allow(clippy::too_many_arguments)]
pub(super) fn try_emit_superblock(
    builder: &mut FunctionBuilder<'_>,
    pointer_type: types::Type,
    context_ptr: Value,
    control_ptr: Value,
    budget: BudgetVars,
    slow_signature: SigRef,
    exit: Block,
    roots: Option<ShadowRoots>,
    function: &Function,
    kind: BodyKind,
    parameters: &[Value],
    analysis: &RecordAnalysis,
    plan: &wings_ir::RootPlan,
    registers: &mut Vec<Value>,
    published_roots: &mut Option<&[usize]>,
    deferred_steps: &mut usize,
    index: usize,
) -> Option<SuperblockResult> {
    let is_superblock_op = |idx: usize| -> bool {
        is_pure_scalar(&function.instructions[idx].operation) || analysis.is_no_op_heap(idx)
    };
    if !is_superblock_op(index) {
        return None;
    }
    let max_end = (index + 32).min(function.instructions.len());
    let mut end = index + 1;
    while end < max_end && is_superblock_op(end) {
        end += 1;
    }
    let is_tail = end == function.instructions.len() && (end - index) < 32;
    let block_steps = (end - index) + usize::from(is_tail);
    let total_steps = *deferred_steps + block_steps;
    if block_steps < 2 && *deferred_steps == 0 {
        return None;
    }
    // Emit a single amortized safepoint covering any prior deferred steps plus
    // the entire pure-scalar superblock, then emit the body once without
    // duplicating fast/slow machine code or a multi-argument join block.
    poll_control(
        builder,
        pointer_type,
        context_ptr,
        control_ptr,
        budget,
        slow_signature,
        exit,
        roots,
        &plan.at[index],
        registers,
        total_steps as i64,
    );
    if *published_roots != Some(plan.at[index].as_slice()) {
        *published_roots = None;
    }
    *deferred_steps = 0;

    for current in index..end {
        let value = if let Some(target) = analysis.forwarded[current] {
            registers[target]
        } else if analysis.elided[current] {
            builder.ins().iconst(types::I64, 0)
        } else {
            emit_pure_scalar(
                builder,
                &function.instructions[current].operation,
                match kind {
                    BodyKind::Handler
                    | BodyKind::LeafHandler
                    | BodyKind::InlineLeafHandler { .. } => ScalarParams::Handler {
                        state: parameters[0],
                        inline_args: [parameters[1], parameters[2], parameters[3]],
                        args_ptr: parameters[4],
                    },
                    BodyKind::Procedure { .. } => ScalarParams::Procedure(parameters),
                },
                registers,
                exit,
            )
        };
        registers.push(value);
    }
    Some(SuperblockResult {
        next_index: end,
        tail_polled: is_tail,
    })
}