Skip to content

Commit

Permalink
Fix encoding inconsistencies (WebAssembly#414)
Browse files Browse the repository at this point in the history
Fix various places in the spec text and interpreter where binary encodings were
inconsistent with those we decided on in WebAssembly#372.
  • Loading branch information
tlively authored Aug 28, 2023
1 parent 1d9585f commit 95ae953
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions document/core/appendix/index-instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ def Instruction(name, opcode, type=None, validation=None, execution=None, operat
Instruction(r'\REFNULL~\X{ht}', r'\hex{D0}', r'[] \to [(\REF~\NULL~\X{ht})]', r'valid-ref.null', r'exec-ref.null'),
Instruction(r'\REFISNULL', r'\hex{D1}', r'[(\REF~\NULL~\X{ht})] \to [\I32]', r'valid-ref.is_null', r'exec-ref.is_null'),
Instruction(r'\REFFUNC~x', r'\hex{D2}', r'[] \to [\FUNCREF]', r'valid-ref.func', r'exec-ref.func'),
Instruction(r'\REFASNONNULL', r'\hex{D3}', r'[(\REF~\NULL~\X{ht})] \to [(\REF~\X{ht})]', r'valid-ref.as_non_null', r'exec-ref.as_non_null'),
Instruction(r'\BRONNULL~l', r'\hex{D4}', r'[t^\ast~(\REF~\NULL~\X{ht})] \to [t^\ast~(\REF~\X{ht})]', r'valid-br_on_null', r'exec-br_on_null'),
Instruction(r'\REFEQ', r'\hex{D5}', r'[\EQREF~\EQREF] \to [\I32]', r'valid-ref.eq', r'exec-ref.eq'),
Instruction(r'\REFEQ', r'\hex{D3}', r'[\EQREF~\EQREF] \to [\I32]', r'valid-ref.eq', r'exec-ref.eq'),
Instruction(r'\REFASNONNULL', r'\hex{D4}', r'[(\REF~\NULL~\X{ht})] \to [(\REF~\X{ht})]', r'valid-ref.as_non_null', r'exec-ref.as_non_null'),
Instruction(r'\BRONNULL~l', r'\hex{D5}', r'[t^\ast~(\REF~\NULL~\X{ht})] \to [t^\ast~(\REF~\X{ht})]', r'valid-br_on_null', r'exec-br_on_null'),
Instruction(r'\BRONNONNULL~l', r'\hex{D6}', r'[t^\ast~(\REF~\NULL~\X{ht})] \to [t^\ast]', r'valid-br_on_non_null', r'exec-br_on_non_null'),
Instruction(None, r'\hex{D7}'),
Instruction(None, r'\hex{D8}'),
Expand Down
3 changes: 2 additions & 1 deletion document/core/appendix/index-types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ Category Constructor
:ref:`Heap type <syntax-heaptype>` |I31| :math:`\hex{6C}` (-20 as |Bs7|)
:ref:`Heap type <syntax-heaptype>` |STRUCT| :math:`\hex{6B}` (-21 as |Bs7|)
:ref:`Heap type <syntax-heaptype>` |ARRAY| :math:`\hex{6A}` (-22 as |Bs7|)
(reserved) :math:`\hex{69}` .. :math:`\hex{65}`
:ref:`Reference type <syntax-reftype>` |REF| :math:`\hex{64}` (-28 as |Bs7|)
:ref:`Reference type <syntax-reftype>` |REF| |NULL| :math:`\hex{63}` (-29 as |Bs7|)
(reserved) :math:`\hex{64}` .. :math:`\hex{61}`
(reserved) :math:`\hex{62}` .. :math:`\hex{61}`
:ref:`Composite type <syntax-comptype>` :math:`\TFUNC~[\valtype^\ast] \toF[\valtype^\ast]` :math:`\hex{60}` (-32 as |Bs7|)
:ref:`Composite type <syntax-comptype>` :math:`\TSTRUCT~\fieldtype^\ast` :math:`\hex{5F}` (-33 as |Bs7|)
:ref:`Composite type <syntax-comptype>` :math:`\TARRAY~\fieldtype` :math:`\hex{5E}` (-34 as |Bs7|)
Expand Down
6 changes: 3 additions & 3 deletions document/core/binary/instructions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Control Instructions
\hex{13}~~y{:}\Btypeidx~~x{:}\Btableidx &\Rightarrow& \RETURNCALLINDIRECT~x~y \\ &&|&
\hex{14}~~x{:}\Btypeidx &\Rightarrow& \CALLREF~x \\ &&|&
\hex{15}~~x{:}\Btypeidx &\Rightarrow& \RETURNCALLREF~x \\ &&|&
\hex{D4}~~l{:}\Blabelidx &\Rightarrow& \BRONNULL~l \\ &&|&
\hex{D5}~~l{:}\Blabelidx &\Rightarrow& \BRONNULL~l \\ &&|&
\hex{D6}~~l{:}\Blabelidx &\Rightarrow& \BRONNONNULL~l \\ &&|&
\hex{FB}~~24{:}\Bu32~~(\NULL_1^?,\NULL_2^?){:}\Bcastflags\\&&&~~~~l{:}\Blabelidx~~\X{ht}_1{:}\Bheaptype~~\X{ht}_2{:}\Bheaptype &\Rightarrow& \BRONCAST~l~(\REF~\NULL_1^?~\X{ht}_1)~(\REF~\NULL_2^?~\X{ht}_2) \\ &&|&
\hex{FB}~~25{:}\Bu32~~(\NULL_1^?,\NULL_2^?){:}\Bcastflags\\&&&~~~~l{:}\Blabelidx~~\X{ht}_1{:}\Bheaptype~~\X{ht}_2{:}\Bheaptype &\Rightarrow& \BRONCASTFAIL~l~(\REF~\NULL_1^?~\X{ht}_1)~(\REF~\NULL_2^?~\X{ht}_2) \\
Expand Down Expand Up @@ -140,8 +140,8 @@ Generic :ref:`reference instructions <syntax-instr-ref>` are represented by sing
\hex{D0}~~t{:}\Bheaptype &\Rightarrow& \REFNULL~t \\ &&|&
\hex{D1} &\Rightarrow& \REFISNULL \\ &&|&
\hex{D2}~~x{:}\Bfuncidx &\Rightarrow& \REFFUNC~x \\ &&|&
\hex{D3} &\Rightarrow& \REFASNONNULL \\ &&|&
\hex{D5} &\Rightarrow& \REFEQ \\ &&|&
\hex{D3} &\Rightarrow& \REFEQ \\ &&|&
\hex{D4} &\Rightarrow& \REFASNONNULL \\ &&|&
\hex{FB}~~0{:}\Bu32~~x{:}\Btypeidx &\Rightarrow& \STRUCTNEW~x \\ &&|&
\hex{FB}~~1{:}\Bu32~~x{:}\Btypeidx &\Rightarrow& \STRUCTNEWDEFAULT~x \\ &&|&
\hex{FB}~~2{:}\Bu32~~x{:}\Btypeidx~~i{:}\Bu32 &\Rightarrow& \STRUCTGET~x~i \\ &&|&
Expand Down
6 changes: 3 additions & 3 deletions interpreter/binary/decode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,9 @@ let rec instr s =
| 0xd0 -> ref_null (heap_type s)
| 0xd1 -> ref_is_null
| 0xd2 -> ref_func (at var s)
| 0xd3 -> ref_as_non_null
| 0xd4 -> br_on_null (at var s)
| 0xd5 -> ref_eq
| 0xd3 -> ref_eq
| 0xd4 -> ref_as_non_null
| 0xd5 -> br_on_null (at var s)
| 0xd6 -> br_on_non_null (at var s)

| 0xfb as b ->
Expand Down
8 changes: 4 additions & 4 deletions interpreter/binary/encode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ struct
| Br x -> op 0x0c; var x
| BrIf x -> op 0x0d; var x
| BrTable (xs, x) -> op 0x0e; vec var xs; var x
| BrOnNull x -> op 0xd4; var x
| BrOnNull x -> op 0xd5; var x
| BrOnNonNull x -> op 0xd6; var x
| BrOnCast (x, (nul1, t1), (nul2, t2)) ->
let flags = bit 0 (nul1 = Null) + bit 1 (nul2 = Null) in
Expand Down Expand Up @@ -376,15 +376,15 @@ struct
| RefNull t -> op 0xd0; heap_type t
| RefFunc x -> op 0xd2; var x

| RefEq -> op 0xd3

| RefIsNull -> op 0xd1
| RefAsNonNull -> op 0xd3
| RefAsNonNull -> op 0xd4
| RefTest (NoNull, t) -> op 0xfb; op 0x14; heap_type t
| RefTest (Null, t) -> op 0xfb; op 0x15; heap_type t
| RefCast (NoNull, t) -> op 0xfb; op 0x16; heap_type t
| RefCast (Null, t) -> op 0xfb; op 0x17; heap_type t

| RefEq -> op 0xd5

| I31New -> op 0xfb; op 0x1c
| I31Get SX -> op 0xfb; op 0x1d
| I31Get ZX -> op 0xfb; op 0x1e
Expand Down

0 comments on commit 95ae953

Please sign in to comment.