1
//! Access to a user binding that may live in a shared cell: a binding both
2
//! assigned and captured by a closure in its scope is one `$cell_<ty>` struct
3
//! that the scope and every closure reach by reference.
4

            
5
use crate::ast::{Place, WasmType};
6
use crate::compiler::context::CompileContext;
7
use crate::compiler::emit::FunctionEmitter;
8
use crate::error::{Error, Result};
9

            
10
/// Pushes the value of user binding `idx`, reading through its cell if any.
11
3454613
pub(in crate::compiler) fn emit_local_read(
12
3454613
    ctx: &CompileContext,
13
3454613
    emit: &mut FunctionEmitter,
14
3454613
    idx: u32,
15
3454613
) {
16
3454613
    emit.local_get(idx);
17
3454613
    if let Some(cell) = ctx.cell_of(idx) {
18
11761
        emit.struct_get(cell, 0);
19
3442852
    }
20
3454613
}
21

            
22
/// Runs before the stored value is pushed: a cell needs its ref below it.
23
190511
pub(in crate::compiler) fn store_begin(ctx: &CompileContext, emit: &mut FunctionEmitter, idx: u32) {
24
190511
    if ctx.cell_of(idx).is_some() {
25
6106
        emit.local_get(idx);
26
184405
    }
27
190511
}
28

            
29
/// Runs after the value is pushed; `keep` leaves the stored value on the stack.
30
181626
pub(in crate::compiler) fn store_end(
31
181626
    ctx: &CompileContext,
32
181626
    emit: &mut FunctionEmitter,
33
181626
    idx: u32,
34
181626
    keep: bool,
35
181626
) {
36
181626
    match (ctx.cell_of(idx), keep) {
37
        (Some(cell), _) => {
38
5886
            emit.struct_set(cell, 0);
39
5886
            if keep {
40
1533
                emit_local_read(ctx, emit, idx);
41
4353
            }
42
        }
43
2759
        (None, true) => emit.local_tee(idx),
44
172981
        (None, false) => emit.local_set(idx),
45
    }
46
181626
}
47

            
48
/// [`emit_local_read`] for a [`Place`].
49
106535
pub(in crate::compiler) fn emit_place_read(
50
106535
    ctx: &mut CompileContext,
51
106535
    emit: &mut FunctionEmitter,
52
106535
    place: Place,
53
106535
) -> Result<()> {
54
106535
    match place {
55
        Place::Local(idx) => {
56
82394
            emit_local_read(ctx, emit, idx);
57
82394
            Ok(())
58
        }
59
24141
        Place::Global(idx) => ctx.emit_global_read(emit, idx),
60
    }
61
106535
}
62

            
63
/// [`store_begin`] for a [`Place`]: a global needs nothing below the value.
64
593137
pub(in crate::compiler) fn store_begin_place(
65
593137
    ctx: &CompileContext,
66
593137
    emit: &mut FunctionEmitter,
67
593137
    place: Place,
68
593137
) {
69
593137
    if let Place::Local(idx) = place {
70
184625
        store_begin(ctx, emit, idx);
71
408512
    }
72
593137
}
73

            
74
/// [`store_end`] for a [`Place`].
75
574856
pub(in crate::compiler) fn store_end_place(
76
574856
    ctx: &CompileContext,
77
574856
    emit: &mut FunctionEmitter,
78
574856
    place: Place,
79
574856
    keep: bool,
80
574856
) {
81
574856
    match place {
82
175952
        Place::Local(idx) => store_end(ctx, emit, idx, keep),
83
        Place::Global(idx) => {
84
398904
            emit.global_set(idx);
85
398904
            if keep {
86
45877
                emit.global_get(idx);
87
353027
            }
88
        }
89
    }
90
574856
}
91

            
92
/// Binds the value on the stack to a fresh local, in a fresh cell when
93
/// `shared`, so each binding entry gets its own cell.
94
496705
pub(in crate::compiler) fn bind_local(
95
496705
    ctx: &mut CompileContext,
96
496705
    emit: &mut FunctionEmitter,
97
496705
    ty: WasmType,
98
496705
    shared: bool,
99
496705
) -> Result<u32> {
100
496705
    if !shared {
101
490853
        let idx = ctx.alloc_local(ty)?;
102
490853
        emit.local_set(idx);
103
490853
        return Ok(idx);
104
5852
    }
105
5852
    let idx = ctx.alloc_cell_local(ty)?;
106
5852
    let cell = ctx
107
5852
        .cell_of(idx)
108
        .ok_or_else(|| Error::Compile("a fresh cell local has no cell type".to_string()))?;
109
5852
    emit.struct_new(cell);
110
5852
    emit.local_set(idx);
111
5852
    Ok(idx)
112
496705
}
113

            
114
#[cfg(test)]
115
#[path = "local_cell_tests.rs"]
116
mod tests;