Skip to content

Commit 36f8d55

Browse files
rossbergzilinc
authored andcommitted
[spec] Fix instr sequence typing rules to actually be composable (WebAssembly#2197)
1 parent 15a34d3 commit 36f8d55

10 files changed

Lines changed: 7212 additions & 6906 deletions

File tree

document/core/valid/instructions.rst

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,22 +1358,28 @@ $${rule-prose: Instrs_ok}
13581358

13591359
$${rule: Instrs_ok/empty}
13601360

1361+
$${rule: Instrs_ok/instr}
1362+
13611363
$${rule: Instrs_ok/seq}
13621364

1363-
$${rule: {Instrs_ok/sub Instrs_ok/frame}}
1365+
$${rule: Instrs_ok/sub}
13641366

13651367
.. note::
1366-
In combination with the previous rule,
1367-
subsumption allows to compose instructions whose types would not directly fit otherwise.
1368+
This *subsumption rule* allows to weaken the type of an instruction sequence to a supertype,
1369+
which includes the ability to drop init variables ${:x*} from the instruction type in a context where they are not needed, for example, at the end of the body of a :ref:`block <valid-block>`.
1370+
1371+
$${rule: Instrs_ok/frame}
1372+
1373+
.. note::
1374+
In combination with the previous two rules,
1375+
this *frame rule* allows to compose instructions whose types would not directly fit otherwise.
13681376
For example, consider the instruction sequence
13691377

13701378
$${instr*: (CONST I32 1) (CONST I32 2) (BINOP I32 ADD)}
13711379

13721380
To type this sequence, its subsequence ${instr*: (CONST I32 2) (BINOP I32 ADD)} needs to be valid with an intermediate type.
13731381
But the direct type of ${instr: (CONST I32 2)} is ${instrtype: eps -> I32}, not matching the two inputs expected by ${instr: $($(BINOP I32 ADD))}.
1374-
The subsumption rule allows to weaken the type of ${instr: (CONST I32 2)} to the supertype ${instrtype: I32 -> I32 I32}, such that it can be composed with ${instr: $($(BINOP I32 ADD))} and yields the intermediate type ${instrtype: I32 -> I32 I32} for the subsequence. That can in turn be composed with the first constant.
1375-
1376-
Furthermore, subsumption allows to drop init variables ${:x*} from the instruction type in a context where they are not needed, for example, at the end of the body of a :ref:`block <valid-block>`.
1382+
The rule allows to weaken the type of ${instr: (CONST I32 2)} to the supertype ${instrtype: I32 -> I32 I32}, such that it can be composed with ${instr: $($(BINOP I32 ADD))} and yields the intermediate type ${instrtype: I32 -> I32 I32} for the subsequence. That can in turn be composed with the first constant.
13771383

13781384

13791385
.. index:: expression, result type

specification/wasm-1.0/6-typing.spectec

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,13 @@ rule Expr_ok:
134134
rule Instrs_ok/empty:
135135
C |- eps : eps -> eps
136136

137+
rule Instrs_ok/instr:
138+
C |- instr : t_1* -> t_2*
139+
-- Instr_ok: C |- instr : t_1* -> t_2*
140+
137141
rule Instrs_ok/seq:
138-
C |- instr_1 instr_2* : t_1* -> t_3*
139-
-- Instr_ok: C |- instr_1 : t_1* -> t_2*
142+
C |- instr_1* instr_2* : t_1* -> t_3*
143+
-- Instrs_ok: C |- instr_1* : t_1* -> t_2*
140144
-- Instrs_ok: C |- instr_2* : t_2* -> t_3*
141145

142146
rule Instrs_ok/frame:

specification/wasm-2.0/6-typing.spectec

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,13 @@ rule Expr_ok:
151151
rule Instrs_ok/empty:
152152
C |- eps : eps -> eps
153153

154+
rule Instrs_ok/instr:
155+
C |- instr : t_1* -> t_2*
156+
-- Instr_ok: C |- instr : t_1* -> t_2*
157+
154158
rule Instrs_ok/seq:
155-
C |- instr_1 instr_2* : t_1* -> t_3*
156-
-- Instr_ok: C |- instr_1 : t_1* -> t_2*
159+
C |- instr_1* instr_2* : t_1* -> t_3*
160+
-- Instrs_ok: C |- instr_1* : t_1* -> t_2*
157161
-- Instrs_ok: C |- instr_2* : t_2* -> t_3*
158162

159163
rule Instrs_ok/sub:

specification/wasm-3.0/2.3-validation.instructions.spectec

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,14 @@ rule Instr_ok/vcvtop:
613613
rule Instrs_ok/empty:
614614
C |- eps : eps -> eps
615615

616+
rule Instrs_ok/instr:
617+
C |- instr : t_1* ->_(x*) t_2*
618+
-- Instr_ok: C |- instr : t_1* ->_(x*) t_2*
619+
616620
;; TODO(3, rossberg): enable x_1*#x_2* to avoid space
617621
rule Instrs_ok/seq:
618-
C |- instr_1 instr_2* : t_1* ->_(x_1* x_2*) t_3*
619-
-- Instr_ok: C |- instr_1 : t_1* ->_(x_1*) t_2*
622+
C |- instr_1* instr_2* : t_1* ->_(x_1* x_2*) t_3*
623+
-- Instrs_ok: C |- instr_1* : t_1* ->_(x_1*) t_2*
620624
-- (if C.LOCALS[x_1] = init t)*
621625
-- Instrs_ok: $with_locals(C, x_1*, (SET t)*) |- instr_2* : t_2* ->_(x_2*) t_3*
622626

specification/wasm-latest/2.3-validation.instructions.spectec

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,14 @@ rule Instr_ok/vcvtop:
613613
rule Instrs_ok/empty:
614614
C |- eps : eps -> eps
615615

616+
rule Instrs_ok/instr:
617+
C |- instr : t_1* ->_(x*) t_2*
618+
-- Instr_ok: C |- instr : t_1* ->_(x*) t_2*
619+
616620
;; TODO(3, rossberg): enable x_1*#x_2* to avoid space
617621
rule Instrs_ok/seq:
618-
C |- instr_1 instr_2* : t_1* ->_(x_1* x_2*) t_3*
619-
-- Instr_ok: C |- instr_1 : t_1* ->_(x_1*) t_2*
622+
C |- instr_1* instr_2* : t_1* ->_(x_1* x_2*) t_3*
623+
-- Instrs_ok: C |- instr_1* : t_1* ->_(x_1*) t_2*
620624
-- (if C.LOCALS[x_1] = init t)*
621625
-- Instrs_ok: $with_locals(C, x_1*, (SET t)*) |- instr_2* : t_2* ->_(x_2*) t_3*
622626

0 commit comments

Comments
 (0)