You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 31, 2023. It is now read-only.
The algorithm uses two separate stacks: the *value stack* and the *control stack*.
42
41
The former tracks the :ref:`types <syntax-valtype>` of operand values on the :ref:`stack <stack>`,
@@ -48,19 +47,16 @@ the latter surrounding :ref:`structured control instructions <syntax-instr-contr
48
47
49
48
type ctrl_stack = stack(ctrl_frame)
50
49
type ctrl_frame = {
51
-
label_types : list(val_type)
50
+
opcode : opcode
51
+
start_types : list(val_type)
52
52
end_types : list(val_type)
53
53
height : nat
54
54
unreachable : bool
55
55
}
56
56
57
57
For each value, the value stack records its :ref:`value type <syntax-valtype>`.
58
58
59
-
For each entered block, the control stack records a *control frame* with the type of the associated :ref:`label <syntax-label>` (used to type-check branches), the result type of the block (used to check its result), the height of the operand stack at the start of the block (used to check that operands do not underflow the current block), and a flag recording whether the remainder of the block is unreachable (used to handle :ref:`stack-polymorphic <polymorphism>` typing after branches).
60
-
61
-
.. note::
62
-
In the presentation of this algorithm, multiple values are supported for the :ref:`result types <syntax-resulttype>` classifying blocks and labels.
63
-
With the current version of WebAssembly, the :code:`list` could be simplified to an optional value.
59
+
For each entered block, the control stack records a *control frame* with the originating opcode, the types on the top of the operand stack at the start and end of the block (used to check its result as well as branches), the height of the operand stack at the start of the block (used to check that operands do not underflow the current block), and a flag recording whether the remainder of the block is unreachable (used to handle :ref:`stack-polymorphic <polymorphism>` typing after branches).
64
60
65
61
For the purpose of presenting the algorithm, the operand and control stacks are simply maintained as global variables:
66
62
@@ -76,7 +72,7 @@ However, these variables are not manipulated directly by the main checking funct
76
72
func push_val(type : val_type) =
77
73
vals.push(type)
78
74
79
-
func pop_val() : val_type =
75
+
func pop_val() : val_type | Unknown =
80
76
if (vals.size() = ctrls[0].height && ctrls[0].unreachable) return Bot
81
77
error_if(vals.size() = ctrls[0].height)
82
78
return vals.pop()
@@ -114,17 +110,21 @@ The control stack is likewise manipulated through auxiliary functions:
114
110
115
111
.. code-block:: pseudo
116
112
117
-
func push_ctrl(label : list(val_type), out : list(val_type)) =
118
-
let frame = ctrl_frame(label, out, vals.size(), false)
113
+
func push_ctrl(opcode : opcode, in : list(val_type), out : list(val_type)) =
114
+
let frame = ctrl_frame(opcode, in, out, vals.size(), false)
return (if frame.opcode == loop then frame.start_types else frame.end_types)
128
128
129
129
func unreachable() =
130
130
vals.resize(ctrls[0].height)
@@ -137,6 +137,8 @@ Popping a frame first checks that the control stack is not empty.
137
137
It then verifies that the operand stack contains the right types of values expected at the end of the exited block and pops them off the operand stack.
138
138
Afterwards, it checks that the stack has shrunk back to its initial height.
139
139
140
+
The type of the :ref:`label <syntax-label>` associated with a control frame is either that of the stack at the start or the end of the frame, determined by the opcode that it originates from.
141
+
140
142
Finally, the current frame can be marked as unreachable.
141
143
In that case, all existing operand types are purged from the value stack, in order to allow for the :ref:`stack-polymorphism <polymorphism>` logic in :code:`pop_val` to take effect.
142
144
@@ -187,44 +189,48 @@ Other instructions are checked in a similar manner.
0 commit comments