1
//! The `$t` singleton (ADR-0033): a module global of an empty struct type,
2
//! `t`'s sole runtime representation. Recognised by reference identity
3
//! (`ref.eq`), never by type test — WasmGC struct types are structural, so an
4
//! empty struct type alone would not be unique.
5

            
6
use wasm_encoder::{ConstExpr, ExportKind, GlobalType, HeapType, Instruction, RefType, ValType};
7

            
8
use super::CompileContext;
9
use crate::ast::WasmType;
10
use crate::error::{Error, Result};
11

            
12
/// The export name of the mutable global `idx` a unit allocated for a runtime
13
/// global; the host reads it back after the unit runs.
14
#[must_use]
15
4907496
pub fn global_export_name(idx: u32) -> String {
16
4907496
    format!("g{idx}")
17
4907496
}
18

            
19
impl CompileContext {
20
    /// Registers the `$t_ty` empty struct type and the exported `t` global
21
    /// initialised with `struct.new $t_ty`. Called once from `new_skeleton`,
22
    /// alongside the other struct-type registrations, so `ids.ty_t` /
23
    /// `ids.global_t` are set before any helper body needs to `global.get` it.
24
2352713
    pub(super) fn register_t_global(&mut self) -> Result<()> {
25
2352713
        let ty_t = self.register_struct_type(&[])?;
26
2352713
        self.ids.ty_t = ty_t;
27
2352713
        let global_ty = GlobalType {
28
2352713
            val_type: ValType::Ref(RefType {
29
2352713
                nullable: false,
30
2352713
                heap_type: HeapType::Concrete(ty_t),
31
2352713
            }),
32
2352713
            mutable: false,
33
2352713
            shared: false,
34
2352713
        };
35
2352713
        let init = ConstExpr::extended([Instruction::StructNew(ty_t)]);
36
2352713
        let global_idx = self.push_global(global_ty, &init)?;
37
2352713
        self.ids.global_t = global_idx;
38
2352713
        self.exports.export("t", ExportKind::Global, global_idx);
39
2352713
        Ok(())
40
2352713
    }
41

            
42
    /// Allocates a mutable global of `ty`, null (or zero) until the program
43
    /// stores to it, exports it as [`global_export_name`] and returns its index.
44
2988053
    pub(crate) fn alloc_mutable_global(&mut self, ty: WasmType) -> Result<u32> {
45
2988053
        let val_type = self.wasm_val_type(ty)?;
46
2988053
        let init = match val_type {
47
220340
            ValType::Ref(ref_ty) => ConstExpr::ref_null(ref_ty.heap_type),
48
2767713
            _ => ConstExpr::i32_const(0),
49
        };
50
2988053
        let global_ty = GlobalType {
51
2988053
            val_type,
52
2988053
            mutable: true,
53
2988053
            shared: false,
54
2988053
        };
55
2988053
        let idx = self.push_global(global_ty, &init)?;
56
2988053
        self.exports
57
2988053
            .export(&global_export_name(idx), ExportKind::Global, idx);
58
2988053
        Ok(idx)
59
2988053
    }
60

            
61
9858165
    pub(super) fn push_global(&mut self, global_ty: GlobalType, init: &ConstExpr) -> Result<u32> {
62
9858165
        self.globals.global(global_ty, init);
63
9858165
        let idx = self.global_count;
64
9858165
        self.global_count = idx
65
9858165
            .checked_add(1)
66
            .ok_or_else(|| Error::Compile("wasm global index space exhausted".into()))?;
67
9858165
        Ok(idx)
68
9858165
    }
69
}