Lines
90 %
Functions
75 %
Branches
100 %
//! The runtime comparison emitter: once the operands' shared dimension is
//! known, the one implementation of which instructions each operator emits.
//! The caller supplies only its operands, through [`CmpOperands`].
use crate::ast::{Expr, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{compile_for_effect, compile_for_stack, compile_for_stack_as};
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
use super::super::shared::compile_for_stack_as_commodity;
use super::identity::IdentityOperands;
/// How a pipeline emits the operands of a runtime comparison.
pub(in crate::compiler) trait CmpOperands {
/// Emits operand `index` coerced to `target`.
fn operand(&mut self, index: usize, target: WasmType) -> Result<()>;
fn parts(&mut self) -> (&mut CompileContext, &mut FunctionEmitter);
fn ctx(&mut self) -> &mut CompileContext {
self.parts().0
}
fn emit(&mut self) -> &mut FunctionEmitter {
self.parts().1
/// Emits a two-operand runtime comparison `name` at dimension `dim` (as
/// `validate_cmp_args` classified it). Operands are evaluated in source
/// order; `>` and `<=` park both in locals and compare them reversed.
pub(in crate::compiler) fn emit_runtime_cmp(
operands: &mut impl CmpOperands,
name: &str,
dim: WasmType,
) -> Result<WasmType> {
match dim {
WasmType::I32 => {
operands.operand(0, WasmType::I32)?;
operands.operand(1, WasmType::I32)?;
let emit = operands.emit();
match name {
"=" => emit.i32_eq(),
"/=" => emit.i32_ne(),
"<" => emit.i32_lt_s(),
">" => emit.i32_gt_s(),
"<=" => emit.i32_le_s(),
">=" => emit.i32_ge_s(),
other => return Err(unknown(other)),
WasmType::Ratio | WasmType::Commodity => ordered(operands, name, dim)?,
WasmType::Bool
| WasmType::PairRef(_)
| WasmType::StringRef
| WasmType::SymbolRef
| WasmType::EntityRef(_)
| WasmType::StructRef(_)
| WasmType::Closure(_)
| WasmType::AnyRef => {
return Err(Error::Compile(format!(
"cannot compare cons cells with {name}"
)));
Ok(WasmType::Bool)
/// A scalar or money comparison through its `*_lt` / `*_eq` helpers.
fn ordered(operands: &mut impl CmpOperands, name: &str, dim: WasmType) -> Result<()> {
let ids = &operands.ctx().ids;
let (lt, eq) = if dim == WasmType::Commodity {
(ids.commodity_lt, ids.commodity_eq)
} else {
(ids.ratio_lt, ids.ratio_eq)
};
">" => reversed(operands, dim, lt),
"<=" => {
reversed(operands, dim, lt)?;
operands.emit().i32_eqz();
Ok(())
"=" | "<" | "/=" | ">=" => {
operands.operand(0, dim)?;
operands.operand(1, dim)?;
"=" => emit.call(eq),
"<" => emit.call(lt),
"/=" => {
emit.call(eq);
emit.i32_eqz();
_ => {
emit.call(lt);
other => Err(unknown(other)),
/// `b < a` with both operands evaluated in source order into locals.
fn reversed(operands: &mut impl CmpOperands, dim: WasmType, lt: u32) -> Result<()> {
let local_a = operands.ctx().alloc_local(dim)?;
operands.emit().local_set(local_a);
let local_b = operands.ctx().alloc_local(dim)?;
emit.local_set(local_b);
emit.local_get(local_b);
emit.local_get(local_a);
fn unknown(name: &str) -> Error {
Error::Compile(format!("no runtime comparison for operator '{name}'"))
/// Source-expression operands, compiled in place.
pub(super) struct ExprOperands<'a> {
pub(super) ctx: &'a mut CompileContext,
pub(super) emit: &'a mut FunctionEmitter,
pub(super) symbols: &'a mut SymbolTable,
pub(super) args: &'a [Expr],
impl<'a> ExprOperands<'a> {
fn arg(&self, index: usize) -> Result<&'a Expr> {
self.args
.get(index)
.ok_or_else(|| Error::Compile(format!("comparison has no operand {index}")))
impl CmpOperands for ExprOperands<'_> {
fn operand(&mut self, index: usize, target: WasmType) -> Result<()> {
let arg = self.arg(index)?;
match target {
WasmType::Commodity => {
compile_for_stack_as_commodity(self.ctx, self.emit, self.symbols, arg)
_ => compile_for_stack_as(self.ctx, self.emit, self.symbols, arg, target),
fn parts(&mut self) -> (&mut CompileContext, &mut FunctionEmitter) {
(self.ctx, self.emit)
impl IdentityOperands for ExprOperands<'_> {
fn stack(&mut self, index: usize) -> Result<WasmType> {
compile_for_stack(self.ctx, self.emit, self.symbols, arg)
fn effect(&mut self, index: usize) -> Result<()> {
compile_for_effect(self.ctx, self.emit, self.symbols, arg)