Skip to content
This repository was archived by the owner on Nov 3, 2021. It is now read-only.

Commit 5a18820

Browse files
committed
[spec/interpreter] Refactor and fix text format for elem and data
1 parent 723b878 commit 5a18820

File tree

7 files changed

+113
-58
lines changed

7 files changed

+113
-58
lines changed

document/core/text/modules.rst

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ A :ref:`start function <syntax-start>` is defined in terms of its index.
486486
.. _text-elem:
487487
.. _text-elemlist:
488488
.. _text-elemexpr:
489+
.. _text-tableuse:
489490

490491
Element Segments
491492
~~~~~~~~~~~~~~~~
@@ -497,13 +498,16 @@ Element segments allow for an optional :ref:`table index <text-tableidx>` to ide
497498
\production{element segment} & \Telem_I &::=&
498499
\text{(}~\text{elem}~~\Tid^?~~(et, y^\ast){:}\Telemlist~\text{)} \\ &&& \qquad
499500
\Rightarrow\quad \{ \ETYPE~et, \EINIT~y^\ast, \EMODE~\EPASSIVE \} \\ &&|&
500-
\text{(}~\text{elem}~~\Tid^?~~\text{(}~\text{table}~~x{:}\Ttableidx_I ~\text{)}~~\text{(}~\text{offset}~~e{:}\Texpr_I~\text{)}~~(et, y^\ast){:}\Telemlist~\text{)} \\ &&& \qquad
501+
\text{(}~\text{elem}~~\Tid^?~~x{:}\Ttableuse_I~~\text{(}~\text{offset}~~e{:}\Texpr_I~\text{)}~~(et, y^\ast){:}\Telemlist~\text{)} \\ &&& \qquad
501502
\Rightarrow\quad \{ \ETYPE~et, \EINIT~y^\ast, \EMODE~\EACTIVE~\{ \ETABLE~x, \EOFFSET~e \} \} \\
502503
\production{element list} & \Telemlist &::=&
503504
et{:}\Telemtype~~y^\ast{:}\Tvec(\Telemexpr_I) \qquad\Rightarrow\quad ( \ETYPE~et, \EINIT~y^\ast ) \\ &&|&
504505
\production{element expression} & \Telemexpr &::=&
505506
\text{(}~\text{ref.null}~\text{)} \\ &&|&
506507
\text{(}~\text{ref.func}~~\Tfuncidx_I~\text{)} \\
508+
\production{table use} & \Ttableuse_I &::=&
509+
\text{(}~\text{table}~~x{:}\Ttableidx_I ~\text{)}
510+
\quad\Rightarrow\quad x \\
507511
\end{array}
508512
509513
.. note::
@@ -532,16 +536,15 @@ Also, the element list may be written as just a sequence of :ref:`function indic
532536
\text{funcref}~~\Tvec(\text{(}~\text{ref.func}~~\Tfuncidx_I~\text{)})
533537
\end{array}
534538
535-
Also, the table index can be omitted, defaulting to :math:`\T{0}`.
536-
Furthermore, for backwards compatibility with earlier versions of WebAssembly, if the table index is omitted, the :math:`\text{func}` keyword can be omitted as well.
539+
Also, a table use can be omitted, defaulting to :math:`\T{0}`.
540+
Furthermore, for backwards compatibility with earlier versions of WebAssembly, if the table use is omitted, the :math:`\text{func}` keyword can be omitted as well.
537541

538542
.. math::
539543
\begin{array}{llclll}
544+
\production{table use} &
545+
\epsilon &\equiv& \text{(}~\text{table}~~\text{0}~\text{)} \\
540546
\production{element segment} &
541-
\text{(}~\text{elem}~~\Tid^?~~\text{(}~\text{offset}~~\Texpr_I~\text{)}~~\Telemlist~\text{)}
542-
&\equiv&
543-
\text{(}~\text{elem}~~\Tid^?~~\text{(}~\text{table}~~\text{0}~\text{)}~~\text{(}~\text{offset}~~\Texpr_I~\text{)}~~\Telemlist~\text{)}
544-
\text{(}~\text{elem}~~\Tid^?~~\text{(}~\text{offset}~~\Texpr_I~\text{)}~~\Tvec(\Tfuncidx_I)~\text{)}
547+
\text{(}~\text{elem}~~\Tid^?~~\text{(}~\text{offset}~~\Texpr_I~\text{)}~~\Tvec(\Tfuncidx_I)~\text{)}
545548
&\equiv&
546549
\text{(}~\text{elem}~~\Tid^?~~\text{(}~\text{table}~~\text{0}~\text{)}~~\text{(}~\text{offset}~~\Texpr_I~\text{)}~~\text{func}~~\Tvec(\Tfuncidx_I)~\text{)}
547550
\end{array}
@@ -555,6 +558,7 @@ As another abbreviation, element segments may also be specified inline with :ref
555558
single: data; segment
556559
.. _text-datastring:
557560
.. _text-data:
561+
.. _test-memuse:
558562

559563
Data Segments
560564
~~~~~~~~~~~~~
@@ -567,10 +571,13 @@ The data is written as a :ref:`string <text-string>`, which may be split up into
567571
\production{data segment} & \Tdata_I &::=&
568572
\text{(}~\text{data}~~\Tid^?~~b^\ast{:}\Tdatastring~\text{)} \\ &&& \qquad
569573
\Rightarrow\quad \{ \DINIT~b^\ast, \DMODE~\DPASSIVE \} \\ &&|&
570-
\text{(}~\text{data}~~\Tid^?~~\text{(}~\text{memory}~~x{:}\Tmemidx_I ~\text{)}~~\text{(}~\text{offset}~~e{:}\Texpr_I~\text{)}~~b^\ast{:}\Tdatastring~\text{)} \\ &&& \qquad
574+
\text{(}~\text{data}~~\Tid^?~~x{:}\Tmemuse_I~~\text{(}~\text{offset}~~e{:}\Texpr_I~\text{)}~~b^\ast{:}\Tdatastring~\text{)} \\ &&& \qquad
571575
\Rightarrow\quad \{ \DINIT~b^\ast, \DMODE~\DACTIVE~\{ \DMEM~x', \DOFFSET~e \} \} \\
572576
\production{data string} & \Tdatastring &::=&
573577
(b^\ast{:}\Tstring)^\ast \quad\Rightarrow\quad \concat((b^\ast)^\ast) \\
578+
\production{memory use} & \Tmemuse_I &::=&
579+
\text{(}~\text{memory}~~x{:}\Tmemidx_I ~\text{)}
580+
\quad\Rightarrow\quad x \\
574581
\end{array}
575582
576583
.. note::
@@ -590,14 +597,12 @@ As an abbreviation, a single instruction may occur in place of the offset of an
590597
\text{(}~\text{offset}~~\Tinstr~\text{)}
591598
\end{array}
592599
593-
Also, the memory index can be omitted, defaulting to :math:`\T{0}`.
600+
Also, a memory use can be omitted, defaulting to :math:`\T{0}`.
594601

595602
.. math::
596603
\begin{array}{llclll}
597-
\production{data segment} &
598-
\text{(}~\text{data}~~\Tid^?~~\text{(}~\text{offset}~~\Texpr_I~\text{)}~~\dots~\text{)}
599-
&\equiv&
600-
\text{(}~\text{data}~~\Tid^?~~\text{(}~\text{memory}~~\text{0}~\text{)}~~\text{(}~\text{offset}~~\Texpr_I~\text{)}~~\dots~\text{)}
604+
\production{memory use} &
605+
\epsilon &\equiv& \text{(}~\text{memory}~~\text{0}~\text{)} \\
601606
\end{array}
602607
603608
As another abbreviation, data segments may also be specified inline with :ref:`memory <text-mem>` definitions; see the respective section.

document/core/util/macros.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,11 +700,13 @@
700700
.. |Telem| mathdef:: \xref{text/modules}{text-elem}{\T{elem}}
701701
.. |Telemlist| mathdef:: \xref{text/modules}{text-elemlist}{\T{elemlist}}
702702
.. |Telemexpr| mathdef:: \xref{text/modules}{text-elemexpr}{\X{elemexpr}}
703+
.. |Ttableuse| mathdef:: \xref{text/modules}{text-tableuse}{\T{tableuse}}
703704
.. |Tcode| mathdef:: \xref{text/modules}{text-code}{\T{code}}
704705
.. |Tlocal| mathdef:: \xref{text/modules}{text-local}{\T{local}}
705706
.. |Tlocals| mathdef:: \xref{text/modules}{text-local}{\T{locals}}
706707
.. |Tdata| mathdef:: \xref{text/modules}{text-data}{\T{data}}
707708
.. |Tdatastring| mathdef:: \xref{text/modules}{text-datastring}{\T{datastring}}
709+
.. |Tmemuse| mathdef:: \xref{text/modules}{text-memuse}{\T{memuse}}
708710
.. |Tstart| mathdef:: \xref{text/modules}{text-start}{\T{start}}
709711

710712

interpreter/text/parser.mly

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,9 @@ func_body :
569569
table_use :
570570
| LPAR TABLE var RPAR { fun c -> $3 c }
571571
572+
memory_use :
573+
| LPAR MEMORY var RPAR { fun c -> $3 c }
574+
572575
offset :
573576
| LPAR OFFSET const_expr RPAR { $3 }
574577
| expr { let at = at () in fun c -> $1 c @@ at } /* Sugar */
@@ -597,29 +600,29 @@ elem_list :
597600
598601
599602
elem :
600-
| LPAR ELEM bind_var_opt offset elem_var_list RPAR /* Sugar */
601-
{ let at = at () in
602-
fun c -> ignore ($3 c anon_elem bind_elem);
603-
fun () ->
604-
{ etype = FuncRefType; einit = $5 c func;
605-
emode = Active {index = 0l @@ at; offset = $4 c} @@ at} @@ at }
606603
| LPAR ELEM bind_var_opt elem_list RPAR
607604
{ let at = at () in
608605
fun c -> ignore ($3 c anon_elem bind_elem);
609606
fun () ->
610-
{etype = (fst $4); einit = (snd $4) c; emode = Passive @@ at} @@ at }
607+
{ etype = (fst $4); einit = (snd $4) c; emode = Passive @@ at } @@ at }
611608
| LPAR ELEM bind_var_opt table_use offset elem_list RPAR
612609
{ let at = at () in
613610
fun c -> ignore ($3 c anon_elem bind_elem);
614611
fun () ->
615612
{ etype = (fst $6); einit = (snd $6) c;
616-
emode = Active {index = $4 c table; offset = $5 c} @@ at} @@ at }
613+
emode = Active {index = $4 c table; offset = $5 c} @@ at } @@ at }
617614
| LPAR ELEM bind_var_opt offset elem_list RPAR /* Sugar */
618615
{ let at = at () in
619616
fun c -> ignore ($3 c anon_elem bind_elem);
620617
fun () ->
621618
{ etype = (fst $5); einit = (snd $5) c;
622-
emode = Active {index = 0l @@ at; offset = $4 c} @@ at} @@ at }
619+
emode = Active {index = 0l @@ at; offset = $4 c} @@ at } @@ at }
620+
| LPAR ELEM bind_var_opt offset elem_var_list RPAR /* Sugar */
621+
{ let at = at () in
622+
fun c -> ignore ($3 c anon_elem bind_elem);
623+
fun () ->
624+
{ etype = FuncRefType; einit = $5 c func;
625+
emode = Active {index = 0l @@ at; offset = $4 c} @@ at } @@ at }
623626
624627
table :
625628
| LPAR TABLE bind_var_opt table_fields RPAR
@@ -662,21 +665,16 @@ data :
662665
{ let at = at () in
663666
fun c -> ignore ($3 c anon_data bind_data);
664667
fun () -> {dinit = $4; dmode = Passive @@ at} @@ at }
665-
| LPAR DATA bind_var var offset string_list RPAR
668+
| LPAR DATA bind_var_opt memory_use offset string_list RPAR
666669
{ let at = at () in
667-
fun c -> ignore (bind_data c $3);
670+
fun c -> ignore ($3 c anon_data bind_data);
668671
fun () ->
669672
{dinit = $6; dmode = Active {index = $4 c memory; offset = $5 c} @@ at} @@ at }
670-
| LPAR DATA var offset string_list RPAR
671-
{ let at = at () in
672-
fun c -> ignore (anon_data c);
673-
fun () ->
674-
{dinit = $5; dmode = Active {index = $3 c memory; offset = $4 c} @@ at} @@ at }
675-
| LPAR DATA offset string_list RPAR /* Sugar */
673+
| LPAR DATA bind_var_opt offset string_list RPAR /* Sugar */
676674
{ let at = at () in
677-
fun c -> ignore (anon_data c);
675+
fun c -> ignore ($3 c anon_data bind_data);
678676
fun () ->
679-
{dinit = $4; dmode = Active {index = 0l @@ at; offset = $3 c} @@ at} @@ at }
677+
{dinit = $5; dmode = Active {index = 0l @@ at; offset = $4 c} @@ at} @@ at }
680678
681679
memory :
682680
| LPAR MEMORY bind_var_opt memory_fields RPAR

test/core/bulk.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
(module
150150
(memory 1)
151151
(data $p "x")
152-
(data $a 0 (i32.const 0) "x")
152+
(data $a (memory 0) (i32.const 0) "x")
153153

154154
(func (export "drop_passive") (data.drop $p))
155155
(func (export "init_passive") (param $len i32)

test/core/data.wast

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,26 @@
88
(data (i32.const 1) "a" "" "bcd")
99
(data (offset (i32.const 0)))
1010
(data (offset (i32.const 0)) "" "a" "bc" "")
11-
(data 0 (i32.const 0))
12-
(data 0x0 (i32.const 1) "a" "" "bcd")
13-
(data 0x000 (offset (i32.const 0)))
14-
(data 0 (offset (i32.const 0)) "" "a" "bc" "")
15-
(data $m (i32.const 0))
16-
(data $m (i32.const 1) "a" "" "bcd")
17-
(data $m (offset (i32.const 0)))
18-
(data $m (offset (i32.const 0)) "" "a" "bc" "")
11+
(data (memory 0) (i32.const 0))
12+
(data (memory 0x0) (i32.const 1) "a" "" "bcd")
13+
(data (memory 0x000) (offset (i32.const 0)))
14+
(data (memory 0) (offset (i32.const 0)) "" "a" "bc" "")
15+
(data (memory $m) (i32.const 0))
16+
(data (memory $m) (i32.const 1) "a" "" "bcd")
17+
(data (memory $m) (offset (i32.const 0)))
18+
(data (memory $m) (offset (i32.const 0)) "" "a" "bc" "")
19+
(data $d1 (i32.const 0))
20+
(data $d2 (i32.const 1) "a" "" "bcd")
21+
(data $d3 (offset (i32.const 0)))
22+
(data $d4 (offset (i32.const 0)) "" "a" "bc" "")
23+
(data $d5 (memory 0) (i32.const 0))
24+
(data $d6 (memory 0x0) (i32.const 1) "a" "" "bcd")
25+
(data $d7 (memory 0x000) (offset (i32.const 0)))
26+
(data $d8 (memory 0) (offset (i32.const 0)) "" "a" "bc" "")
27+
(data $d9 (memory $m) (i32.const 0))
28+
(data $d10 (memory $m) (i32.const 1) "a" "" "bcd")
29+
(data $d11 (memory $m) (offset (i32.const 0)))
30+
(data $d12 (memory $m) (offset (i32.const 0)) "" "a" "bc" "")
1931
)
2032

2133
;; Basic use

test/core/elem.wast

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,29 @@
55
(table $t 10 funcref)
66
(func $f)
77
(func $g)
8-
(elem (table $t) (i32.const 0) func)
9-
(elem (i32.const 0))
10-
(elem (i32.const 0) $f $f)
11-
(elem (offset (i32.const 0)))
12-
(elem (offset (i32.const 0)) $f $f)
138

9+
;; Passive
10+
(elem funcref)
11+
(elem funcref (ref.func $f) (ref.func $f) (ref.null) (ref.func $g))
1412
(elem func)
15-
(elem $s func)
1613
(elem func $f $f $g $g)
17-
(elem $x func $f $f $g $g)
1814

15+
(elem $p1 funcref)
16+
(elem $p2 funcref (ref.func $f) (ref.func $f) (ref.null) (ref.func $g))
17+
(elem $p3 func)
18+
(elem $p4 func $f $f $g $g)
19+
20+
;; Active
21+
(elem (table $t) (i32.const 0) funcref)
22+
(elem (table $t) (i32.const 0) funcref (ref.func $f) (ref.null))
23+
(elem (table $t) (i32.const 0) func)
24+
(elem (table $t) (i32.const 0) func $f $g)
25+
(elem (table $t) (i32.const 0) funcref)
26+
(elem (table $t) (i32.const 0) funcref (ref.func $f) (ref.null))
27+
(elem (table $t) (i32.const 0) func)
28+
(elem (table $t) (i32.const 0) func $f $g)
29+
(elem (table $t) (offset (i32.const 0)) funcref)
30+
(elem (table $t) (offset (i32.const 0)) func $f $g)
1931
(elem (table 0) (i32.const 0) func)
2032
(elem (table 0x0) (i32.const 0) func $f $f)
2133
(elem (table 0x000) (offset (i32.const 0)) func)
@@ -24,15 +36,41 @@
2436
(elem (table $t) (i32.const 0) func $f $f)
2537
(elem (table $t) (offset (i32.const 0)) func)
2638
(elem (table $t) (offset (i32.const 0)) func $f $f)
39+
(elem (offset (i32.const 0)))
40+
(elem (offset (i32.const 0)) funcref (ref.func $f) (ref.null))
41+
(elem (offset (i32.const 0)) func $f $f)
42+
(elem (offset (i32.const 0)) $f $f)
43+
(elem (i32.const 0))
44+
(elem (i32.const 0) funcref (ref.func $f) (ref.null))
45+
(elem (i32.const 0) func $f $f)
46+
(elem (i32.const 0) $f $f)
2747

28-
(elem (i32.const 3) funcref (ref.func $f) (ref.func $g) (ref.func $f))
29-
(elem (i32.const 3) funcref (ref.func $f) (ref.null) (ref.func $f))
30-
31-
(elem funcref (ref.func $f) (ref.func $g) (ref.func $f))
32-
(elem funcref (ref.func $f) (ref.func $g) (ref.null))
33-
34-
(elem (table $t) (i32.const 3) funcref (ref.func $f) (ref.func $g) (ref.func $f))
35-
(elem (table $t) (i32.const 3) funcref (ref.func $f) (ref.null) (ref.func $g))
48+
(elem $a1 (table $t) (i32.const 0) funcref)
49+
(elem $a2 (table $t) (i32.const 0) funcref (ref.func $f) (ref.null))
50+
(elem $a3 (table $t) (i32.const 0) func)
51+
(elem $a4 (table $t) (i32.const 0) func $f $g)
52+
(elem $a5 (table $t) (i32.const 0) funcref)
53+
(elem $a6 (table $t) (i32.const 0) funcref (ref.func $f) (ref.null))
54+
(elem $a7 (table $t) (i32.const 0) func)
55+
(elem $a8 (table $t) (i32.const 0) func $f $g)
56+
(elem $a9 (table $t) (offset (i32.const 0)) funcref)
57+
(elem $a10 (table $t) (offset (i32.const 0)) func $f $g)
58+
(elem $a11 (table 0) (i32.const 0) func)
59+
(elem $a12 (table 0x0) (i32.const 0) func $f $f)
60+
(elem $a13 (table 0x000) (offset (i32.const 0)) func)
61+
(elem $a14 (table 0) (offset (i32.const 0)) func $f $f)
62+
(elem $a15 (table $t) (i32.const 0) func)
63+
(elem $a16 (table $t) (i32.const 0) func $f $f)
64+
(elem $a17 (table $t) (offset (i32.const 0)) func)
65+
(elem $a18 (table $t) (offset (i32.const 0)) func $f $f)
66+
(elem $a19 (offset (i32.const 0)))
67+
(elem $a20 (offset (i32.const 0)) funcref (ref.func $f) (ref.null))
68+
(elem $a21 (offset (i32.const 0)) func $f $f)
69+
(elem $a22 (offset (i32.const 0)) $f $f)
70+
(elem $a23 (i32.const 0))
71+
(elem $a24 (i32.const 0) funcref (ref.func $f) (ref.null))
72+
(elem $a25 (i32.const 0) func $f $f)
73+
(elem $a26 (i32.const 0) $f $f)
3674
)
3775

3876
(module

test/core/imports.wast

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@
380380

381381
(module
382382
(import "spectest" "memory" (memory 1 2))
383-
(data 0 (i32.const 10) "\10")
383+
(data (memory 0) (i32.const 10) "\10")
384384

385385
(func (export "load") (param i32) (result i32) (i32.load (local.get 0)))
386386
)
@@ -392,7 +392,7 @@
392392

393393
(module
394394
(memory (import "spectest" "memory") 1 2)
395-
(data 0 (i32.const 10) "\10")
395+
(data (memory 0) (i32.const 10) "\10")
396396

397397
(func (export "load") (param i32) (result i32) (i32.load (local.get 0)))
398398
)

0 commit comments

Comments
 (0)