Lines
92.42 %
Functions
88.46 %
Branches
100 %
//! Comparison operands whose type is known only once they are compiled: each
//! is compiled exactly once, in source order, and parked in a local (or kept
//! as a literal), so the dimension is chosen from the types actually pushed.
use crate::ast::{Expr, Number, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{compile_for_stack, compile_raising, compile_to_local, push_number};
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
use super::super::shared::validate_cmp_args;
use super::emit::{CmpOperands, emit_runtime_cmp};
use super::identity::{IdentityOperands, emit_identity};
enum Slot {
Number(Number),
/// An operand that always raises: it has no value and no dimension.
Raises,
/// A value parked in a local; `folded` is its constant when the
/// evaluator folded the operand.
Local {
local: u32,
ty: WasmType,
folded: Option<Number>,
},
}
struct Spilled<'a> {
ctx: &'a mut CompileContext,
emit: &'a mut FunctionEmitter,
symbols: &'a mut SymbolTable,
slots: Vec<Slot>,
/// Compiles every operand once and classifies it by what it pushed.
fn spill(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
args: &[Expr],
resolved: &[Expr],
) -> Result<(Vec<Slot>, Vec<Expr>)> {
let mut slots = Vec::with_capacity(args.len());
let mut classified = Vec::with_capacity(args.len());
for (arg, value) in args.iter().zip(resolved) {
if let (Expr::Number(n), Expr::Number(_)) = (value, arg) {
slots.push(Slot::Number(*n));
classified.push(value.clone());
continue;
if compile_raising(ctx, emit, symbols, arg)? {
slots.push(Slot::Raises);
classified.push(Expr::Number(Number::Index(1)));
let Some((local, ty)) = compile_to_local(ctx, emit, symbols, arg)? else {
};
let folded = match value {
Expr::Number(n) => Some(*n),
_ => None,
slots.push(Slot::Local { local, ty, folded });
classified.push(if value.is_wasm_runtime() {
Expr::WasmRuntime(ty)
} else {
value.clone()
});
Ok((slots, classified))
impl Spilled<'_> {
fn slot(&self, index: usize) -> Result<&Slot> {
self.slots
.get(index)
.ok_or_else(|| Error::Compile(format!("comparison has no operand {index}")))
impl CmpOperands for Spilled<'_> {
fn operand(&mut self, index: usize, target: WasmType) -> Result<()> {
match *self.slot(index)? {
Slot::Number(n)
| Slot::Local {
folded: Some(n), ..
} => push_number(self.ctx, self.emit, n).map(drop),
Slot::Raises => {
self.emit.unreachable();
Ok(())
Slot::Local {
local,
ty,
folded: None,
} if ty == target => {
self.emit.local_get(local);
Slot::Local { ty, .. } => Err(Error::Compile(format!(
"comparison requires {target} operands, got {ty}"
))),
fn parts(&mut self) -> (&mut CompileContext, &mut FunctionEmitter) {
(self.ctx, self.emit)
impl IdentityOperands for Spilled<'_> {
fn stack(&mut self, index: usize) -> Result<WasmType> {
Slot::Number(n) => {
compile_for_stack(self.ctx, self.emit, self.symbols, &Expr::Number(n))
Slot::Raises => compile_for_stack(
self.ctx,
self.emit,
self.symbols,
&Expr::Number(Number::Index(1)),
),
Slot::Local { local, ty, .. } => {
Ok(ty)
fn effect(&mut self, _index: usize) -> Result<()> {
/// The operands `start` and `start + 1` of a [`Spilled`] chain, as the two
/// operands of one comparison.
struct Window<'a, 'b> {
spilled: &'a mut Spilled<'b>,
start: usize,
impl CmpOperands for Window<'_, '_> {
self.spilled.operand(self.start + index, target)
self.spilled.parts()
/// The comparison `name` of operands `resolved` classified by the evaluator,
/// either of which only compiling can type or of which there are more than two:
/// every adjacent pair must hold.
pub(super) fn compile_spilled_cmp(
name: &str,
) -> Result<WasmType> {
let (slots, classified) = spill(ctx, emit, symbols, args, resolved)?;
let mut operands = Spilled {
ctx,
emit,
symbols,
slots,
let typed: Vec<Expr> = classified
.into_iter()
.zip(&operands.slots)
.filter(|(_, slot)| !matches!(slot, Slot::Raises))
.map(|(operand, _)| operand)
.collect();
let dim = validate_cmp_args(&typed, name)?;
for start in 0..args.len() - 1 {
emit_runtime_cmp(
&mut Window {
spilled: &mut operands,
start,
name,
dim,
)?;
if start > 0 {
operands.emit.i32_and();
Ok(WasmType::Bool)
/// The identity test of two operands, at least one of which only compiling
/// can type.
pub(super) fn compile_spilled_identity(
structural: bool,
if operands
.slots
.iter()
.any(|slot| matches!(slot, Slot::Raises))
{
operands.emit.i32_const(0);
return Ok(WasmType::Bool);
emit_identity(&mut operands, &classified[0], &classified[1], structural)
#[cfg(test)]
#[path = "spilled_tests.rs"]
mod tests;