vyre-conform 0.1.0

Conformance suite for vyre backends — proves byte-identical output to CPU reference
Documentation
//! Generated scalar and byte literal expressions.

use crate::generate::archetypes::TestInput;
use crate::spec::types::OpSpec;
use crate::spec::Property;

use crate::generate::emit::cross_product::Route;

pub(super) fn value_inputs_expr(op: &OpSpec, input: &TestInput) -> String {
    value_inputs_from_words(&input_words(op, input))
}

pub(super) fn value_inputs_from_words(words: &[u32]) -> String {
    format!(
        "vec![vyre_conform::spec::value::Value::Bytes({input}), vyre_conform::spec::value::Value::Bytes(vec![0x00u8, 0x00u8, 0x00u8, 0x00u8])]",
        input = bytes_literal(&words_to_bytes(words)),
    )
}

pub(super) fn input_words(op: &OpSpec, input: &TestInput) -> Vec<u32> {
    (0..op.signature.inputs.len())
        .map(|index| input.values.get(index).copied().unwrap_or(0))
        .collect()
}

pub(super) fn input_words_expr(words: &[u32]) -> String {
    words
        .iter()
        .map(u32::to_string)
        .collect::<Vec<_>>()
        .join(", ")
}

fn words_to_bytes(words: &[u32]) -> Vec<u8> {
    words.iter().flat_map(|value| value.to_le_bytes()).collect()
}

pub(super) fn bytes_literal(bytes: &[u8]) -> String {
    let body = bytes
        .iter()
        .map(|byte| format!("0x{byte:02X}u8"))
        .collect::<Vec<_>>()
        .join(", ");
    format!("vec![{body}]")
}

pub(super) fn byte_expr(value: u32, index: usize) -> String {
    format!("0x{:02X}u8", value.to_le_bytes()[index])
}

/// Fixes architecture_deep_audit.md#10/#13: crate-private visibility keeps
/// generated cross-product helpers visible to exhaustive audits.
#[inline]
pub(crate) fn spec_row_index(property: &Property) -> Option<usize> {
    match property {
        Property::SpecTableRowMatches { row_index } => Some(*row_index),
        _ => None,
    }
}

/// Fixes architecture_deep_audit.md#10/#13: crate-private visibility keeps
/// generated cross-product helpers visible to exhaustive audits.
#[inline]
pub(crate) fn route_key(route: &Route) -> String {
    format!(
        "{:?}_{}_{}",
        route.template, route.oracle_name, route.route_id
    )
}

/// Fixes architecture_deep_audit.md#10/#13: crate-private visibility keeps
/// generated cross-product helpers visible to exhaustive audits.
#[inline]
pub(crate) fn expected_bytes(op: &OpSpec, route: &Route) -> String {
    spec_row_index(&route.property)
        .and_then(|index| super::super::spec_rows(op).get(index))
        .map(|row| bytes_literal(row.expected))
        .unwrap_or_else(|| "computed by oracle".to_string())
}