Lines
96.72 %
Functions
96.97 %
Branches
100 %
//! Dynamic binding of a special (ISS-208): a binder of a special saves the
//! special's global, stores the new value and restores the saved one on every
//! exit — normal, a crossing `return-from` / `go`, and a throw — by running
//! the restore as the cleanup of the UNWIND-PROTECT lowering. A callee or a
//! closure reads the global, so it sees the binding in force when it runs.
//! A special an earlier unit defined holds a constant and has no global: the
//! unit that binds it makes it a cell for the unit.
use crate::ast::{Expr, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::error::{Error, Result};
use crate::runtime::{Rebind, SymbolTable};
use wasm_encoder::BlockType;
/// A special a binder binds dynamically: its global and the global's type,
/// and the bound flag of a cell.
#[derive(Clone, Copy)]
pub(super) struct DynamicHome {
pub(super) global: u32,
pub(super) ty: WasmType,
pub(super) flag: Option<u32>,
}
/// A special's global and type, the local holding the value to restore, the
/// bound flag with the local holding its value to restore, and its binding
/// depth global with the local holding the depth to restore.
pub(super) struct Saved {
pub(super) local: u32,
pub(super) flag: Option<(u32, u32)>,
pub(super) depth_local: u32,
pub(super) depth: u32,
/// The home of the special `name` when a binder of it binds dynamically; a
/// global a `SETF` created is not special. A macro body evaluates at compile
/// time, where no global holds a value, so its binders stay lexical.
pub(super) fn dynamic_home(symbols: &SymbolTable, name: &str) -> Option<DynamicHome> {
match symbols.lookup(name)?.value()? {
Expr::WasmGlobal(idx, ty)
if !symbols.is_macro_expansion()
&& symbols.is_special(name)
&& !symbols.is_created_global(name) =>
{
Some(DynamicHome {
global: *idx,
ty: *ty,
flag: symbols.cell_flag(name),
})
_ => None,
/// A binder of a unit special no source LET / LET* binds can only come from a
/// macro the scan could not read, so it must not silently bind lexically.
pub(super) fn reject_missed_specials<'a>(
symbols: &SymbolTable,
names: impl IntoIterator<Item = &'a str>,
) -> Result<()> {
if symbols.is_macro_expansion() {
return Ok(());
for name in names {
if symbols.is_unit_special(name) && !symbols.is_dynamic_special(name) {
return Err(Error::Compile(format!(
"{name}: a macro expansion rebinds the special {name}; not supported yet"
)));
Ok(())
/// Saves the current value and bound flag of each distinct global among
/// `homes`, and counts the binding in the global's depth.
pub(super) fn save_all(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
homes: &[DynamicHome],
) -> Result<Vec<Saved>> {
let mut saved: Vec<Saved> = Vec::new();
for &home in homes {
if saved.iter().any(|entry| entry.global == home.global) {
continue;
let local = save(ctx, emit, home.global, home.ty)?;
let flag = home
.flag
.map(|flag| save(ctx, emit, flag, WasmType::I32).map(|local| (flag, local)))
.transpose()?;
let (depth, depth_local) = enter(ctx, emit, symbols, home)?;
saved.push(Saved {
global: home.global,
ty: home.ty,
local,
flag,
depth_local,
depth,
});
Ok(saved)
/// Counts one more binding of the special `home`; the outermost one keeps the
/// value and flag outside it for a host to carry when the unit traps inside.
fn enter(
home: DynamicHome,
) -> Result<(u32, u32)> {
let rebind = match symbols.rebind_of(home.global) {
Some(rebind) => rebind,
None => {
let rebind = Rebind {
depth: ctx.alloc_mutable_global(WasmType::I32)?,
outer: ctx.alloc_mutable_global(home.ty)?,
outer_bound: ctx.alloc_mutable_global(WasmType::I32)?,
};
symbols.note_rebind(home.global, rebind);
rebind
let depth_local = save(ctx, emit, rebind.depth, WasmType::I32)?;
emit.global_get(rebind.depth);
emit.i32_eqz();
emit.if_block(BlockType::Empty);
emit.global_get(home.global);
emit.global_set(rebind.outer);
match home.flag {
Some(flag) => emit.global_get(flag),
None => emit.i32_const(1),
emit.global_set(rebind.outer_bound);
emit.block_end()?;
emit.i32_const(1);
emit.i32_add();
emit.global_set(rebind.depth);
Ok((rebind.depth, depth_local))
fn save(
global: u32,
ty: WasmType,
) -> Result<u32> {
let local = ctx.alloc_local(ty)?;
emit.global_get(global);
emit.local_set(local);
Ok(local)
/// Stores the value on the stack into the binding `home` and marks it bound.
pub(super) fn bind(emit: &mut FunctionEmitter, home: DynamicHome) {
emit.global_set(home.global);
if let Some(flag) = home.flag {
emit.global_set(flag);
/// The cleanup forms restoring `saved` and its binding depth,
/// last-bound first.
pub(super) fn restore_forms(saved: &[Saved]) -> Vec<Expr> {
saved
.iter()
.rev()
.flat_map(|entry| {
let value = restore(entry.global, entry.ty, entry.local);
let flag = entry
.map(|(flag, local)| restore(flag, WasmType::I32, local));
let depth = restore(entry.depth, WasmType::I32, entry.depth_local);
std::iter::once(value).chain(flag).chain([depth])
.collect()
fn restore(global: u32, ty: WasmType, local: u32) -> Expr {
Expr::List(vec![
Expr::symbol("SETF"),
Expr::WasmGlobal(global, ty),
Expr::WasmLocal(local, ty),
])