Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 3099b6f

Browse files
authored
[spec] Module instantiation & handling of host functions (#487)
1 parent ce506f8 commit 3099b6f

File tree

10 files changed

+888
-85
lines changed

10 files changed

+888
-85
lines changed

document/Makefile

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ DOWNLOADDIR = _download
1111
GHPAGESDIR = ../docs
1212
NAME = WebAssembly
1313

14+
# Hack until we have moved to more recent Sphinx.
15+
OLDMATHJAX = https://cdn.mathjax.org/mathjax/latest/MathJax.js
16+
NEWMATHJAX = https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js
17+
1418
# Internal variables.
1519
PAPEROPT_a4 = -D latex_paper_size=a4
1620
PAPEROPT_letter = -D latex_paper_size=letter
@@ -88,13 +92,17 @@ html:
8892
do \
8993
sed s:BASEDIR:.:g <$$file >$$file.out; \
9094
mv -f $$file.out $$file; \
95+
sed s~$(OLDMATHJAX)~$(NEWMATHJAX)~g <$$file >$$file.out; \
96+
mv -f $$file.out $$file; \
9197
done
9298
for file in `ls $(BUILDDIR)/html/*/*.html`; \
9399
do \
94100
sed s:BASEDIR:..:g <$$file >$$file.out; \
95-
sed 's;<body;<script type="text/javascript">MathJax.Hub.Config({TeX: {MAXBUFFER: 30*1024}})</script><body;' \
101+
sed 's; <body; <script type="text/javascript">MathJax.Hub.Config({TeX: {MAXBUFFER: 30*1024}})</script><body;' \
96102
<$$file.out >$$file; \
97103
rm -f $$file.out; \
104+
sed s~$(OLDMATHJAX)~$(NEWMATHJAX)~g <$$file >$$file.out; \
105+
mv -f $$file.out $$file; \
98106
done
99107
@echo
100108
@echo "Build finished. The HTML pages are in `pwd`/$(BUILDDIR)/html."

document/custom.css

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ a.reference:hover {
1111
border-bottom: 1px dotted #004BAB;
1212
}
1313

14+
div.document { width: 1000px; }
15+
div.footer { width: 1000px; }
16+
1417
div.body h1 { font-size: 200%; }
1518
div.body h2 { font-size: 150%; }
1619
div.body h3 { font-size: 120%; }
@@ -32,6 +35,14 @@ div.admonition p.admonition-title {
3235
}
3336

3437

38+
div.sphinxsidebar {
39+
z-index: 1;
40+
background: #FFF;
41+
margin-top: -30px;
42+
width: 230px;
43+
height: 100%;
44+
}
45+
3546
div.sphinxsidebar a {
3647
border-bottom: 0px;
3748
}

document/execution/conventions.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ The following conventions are adopted in stating these rules.
3737
that is modified by *pushing* or *popping*
3838
:ref:`values <syntax-value>`, :ref:`labels <syntax-label>`, and :ref:`frames <syntax-frame>`.
3939

40-
* Certain rules require the stack to contain at least one frame,
41-
which is referred to as the *current* frame.
40+
* Certain rules require the stack to contain at least one frame.
41+
The most recent frame is referred to as the *current* frame.
4242

4343
* Both the store and the current frame are mutated by *replacing* some of its components.
4444
Such replacement is assumed to apply globally.

document/execution/instructions.rst

+71-24
Original file line numberDiff line numberDiff line change
@@ -884,11 +884,9 @@ Control Instructions
884884

885885
14. Let :math:`\X{f}` be the :ref:`function instance <syntax-funcinst>` :math:`S.\FUNCS[a]`.
886886

887-
15. Assert: due to :ref:`validation <valid-func>`, :math:`\X{f}.\MODULE.\TYPES[\X{f}.\CODE.\TYPE]` exists.
887+
15. Let :math:`\X{ft}_{\F{actual}}` be the :ref:`function type <syntax-functype>` :math:`\X{f}.\TYPE`.
888888

889-
16. Let :math:`\X{ft}_{\F{actual}}` be the :ref:`function type <syntax-functype>` :math:`\X{f}.\MODULE.\TYPES[\X{f}.\CODE.\TYPE]`.
890-
891-
15. If :math:`\X{ft}_{\F{actual}}` and :math:`\X{ft}_{\F{expect}}` differ, then:
889+
16. If :math:`\X{ft}_{\F{actual}}` and :math:`\X{ft}_{\F{expect}}` differ, then:
892890

893891
a. Trap.
894892

@@ -903,7 +901,7 @@ Control Instructions
903901
\begin{array}[t]{@{}r@{~}l@{}}
904902
(\mbox{if} & S.\TABLES[F.\MODULE.\TABLES[0]].\ELEM[i] = a \\
905903
\wedge & S.\FUNCS[a] = f \\
906-
\wedge & F.\MODULE.\TYPES[x] = f.\MODULE.\TYPES[f.\CODE.\TYPE])
904+
\wedge & F.\MODULE.\TYPES[x] = f.\TYPE)
907905
\end{array} \\
908906
\begin{array}{lcl@{\qquad}l}
909907
S; F; (\I32.\CONST~i)~(\CALLINDIRECT~x) &\stepto& S; F; \TRAP
@@ -987,25 +985,23 @@ Invocation of :ref:`function address <syntax-funcaddr>` :math:`a`
987985

988986
2. Let :math:`f` be the :ref:`function instance <syntax-funcinst>`, :math:`S.\FUNCS[a]`.
989987

990-
3. Assert: due to :ref:`validation <valid-func>`, :math:`f.\MODULE.\TYPES[f.\CODE.\TYPE]` exists.
991-
992-
4. Let :math:`[t_1^n] \to [t_2^m]` be the :ref:`function type <syntax-functype>` :math:`f.\MODULE.\TYPES[f.\CODE.\TYPE]`.
988+
3. Let :math:`[t_1^n] \to [t_2^m]` be the :ref:`function type <syntax-functype>` :math:`f.\TYPE`.
993989

994-
5. Let :math:`t^\ast` be the list of :ref:`value types <syntax-valtype>` :math:`f.\CODE.\LOCALS`.
990+
4. Let :math:`t^\ast` be the list of :ref:`value types <syntax-valtype>` :math:`f.\CODE.\LOCALS`.
995991

996-
6. Let :math:`\instr^\ast~\END` be the :ref:`expression <syntax-expr>` :math:`f.\CODE.\BODY`.
992+
5. Let :math:`\instr^\ast~\END` be the :ref:`expression <syntax-expr>` :math:`f.\CODE.\BODY`.
997993

998-
7. Assert: due to :ref:`validation <valid-call>`, :math:`n` values are on the top of the stack.
994+
6. Assert: due to :ref:`validation <valid-call>`, :math:`n` values are on the top of the stack.
999995

1000-
8. Pop the values :math:`\val^n` from the stack.
996+
7. Pop the values :math:`\val^n` from the stack.
1001997

1002-
9. Let :math:`\val_0^\ast` be the list of zero values of types :math:`t^\ast`.
998+
8. Let :math:`\val_0^\ast` be the list of zero values of types :math:`t^\ast`.
1003999

1004-
10. Let :math:`F` be the :ref:`frame <syntax-frame>` :math:`\{ \MODULE~f.\MODULE, \LOCALS~\val^n~\val_0^\ast \}`.
1000+
9. Let :math:`F` be the :ref:`frame <syntax-frame>` :math:`\{ \MODULE~f.\MODULE, \LOCALS~\val^n~\val_0^\ast \}`.
10051001

1006-
11. Push the activation of :math:`F` with arity :math:`m` to the stack.
1002+
10. Push the activation of :math:`F` with arity :math:`m` to the stack.
10071003

1008-
12. :ref:`Execute <exec-block>` the instruction :math:`\BLOCK~[t_2^m]~\instr^\ast~\END`.
1004+
11. :ref:`Execute <exec-block>` the instruction :math:`\BLOCK~[t_2^m]~\instr^\ast~\END`.
10091005

10101006
.. math::
10111007
\begin{array}{l}
@@ -1016,7 +1012,7 @@ Invocation of :ref:`function address <syntax-funcaddr>` :math:`a`
10161012
\begin{array}[t]{@{}r@{~}l@{}}
10171013
(\mbox{if} & S.\FUNCS[a] = f \\
10181014
\wedge & f.\CODE = \{ \TYPE~x, \LOCALS~t^k, \BODY~\instr^\ast~\END \} \\
1019-
\wedge & f.\MODULE.\TYPES[x] = [t_1^n] \to [t_2^m] \\
1015+
\wedge & f.\TYPE = [t_1^n] \to [t_2^m] \\
10201016
\wedge & F = \{ \MODULE~f.\MODULE, ~\LOCALS~\val^n~(t.\CONST~0)^k \})
10211017
\end{array} \\
10221018
\end{array}
@@ -1051,24 +1047,75 @@ When the end of a funtion is reached without a jump (|RETURN|) or trap aborting
10511047
\end{array}
10521048
10531049
1050+
.. _exec-invoke-host:
1051+
1052+
Host Functions
1053+
..............
1054+
1055+
Invoking a :ref:`host function <syntax-hostfunc>` has non-deterministic behavior.
1056+
It may either terminate with a :ref:`trap <trap>` or return regularly.
1057+
However, the latter case, it is assumed that it consumes and produces the right number and types of WebAssembly :ref:`values <syntax-val>` on the stack,
1058+
according to its type.
1059+
A host function may also modify the :ref:`store <syntax-store>` when invoked.
1060+
1061+
.. math::
1062+
\begin{array}{l}
1063+
\begin{array}{lcl@{\qquad}l}
1064+
S; \val^n~(\INVOKE~a) &\stepto& S'; {\val'\,}^m
1065+
\end{array}
1066+
\\ \qquad
1067+
\begin{array}[t]{@{}r@{~}l@{}}
1068+
(\mbox{if} & S.\FUNCS[a] = \{ \TYPE~[t_1^n] \to [t_2^m], \HOST~\dots \} \\
1069+
\wedge & \val^n = (t_1.\CONST~c_1)^n \\
1070+
\wedge & {\val'\,}^m = (t_2.\CONST~c_2)^m \\
1071+
\wedge & S' \succ S) \\
1072+
\end{array} \\
1073+
\begin{array}{lcl@{\qquad}l}
1074+
S; \val^n~(\INVOKE~a) &\stepto& S'; \TRAP
1075+
\end{array}
1076+
\\ \qquad
1077+
\begin{array}[t]{@{}r@{~}l@{}}
1078+
(\mbox{if} & S.\FUNCS[a] = \{ \TYPE~\X{ft}, \HOST~\dots \} \\
1079+
\wedge & S' \succ S) \\
1080+
\end{array} \\
1081+
\end{array}
1082+
1083+
Here, :math:`S' \succ S` expresses that the new store :math:`S'` is *reachable* from :math:`S`.
1084+
Such a store must not contain fewer addresses than the original store,
1085+
it must not differ in elements that are not mutable,
1086+
and it must still be well-typed.
1087+
1088+
.. todo:: Define more precisely?
1089+
1090+
.. note::
1091+
A host function can call back into WebAssembly by :ref:`invoking <invocation>` a function :ref:`exported <syntax-export>` from a :ref:`module <syntax-module>`.
1092+
However, the effects of any such call are subsumed by the non-deterministic behavior allowed for a host function.
1093+
1094+
1095+
10541096
.. _exec-expr:
10551097
.. index:: expression
10561098
pair: execution; expression
10571099
single: abstract syntax; expression
1058-
single: expression; constant
10591100

10601101
Expressions
10611102
~~~~~~~~~~~
10621103

1063-
:math:`\instr^\ast~\END`
1064-
........................
1104+
An :ref:`expression <syntax-expr>` is *evaluated* relative to a :ref:`current <exec-notation-textual>` :ref:`frame <syntax-frame>` pointing to its containing :ref:`module instance <syntax-modinst>` :math:`\moduleinst`.
1105+
1106+
1. :ref:`Jump <exec-jump>` to the start of the instruction sequence :math:`\instr^\ast` of the expression.
1107+
1108+
2. Execute of the instruction sequence.
1109+
1110+
3. Assert: due to :ref:`validation <valid-expr>`, the top of the stack contains a :ref:`value <syntax-val>`.
1111+
1112+
4. Pop the the :ref:`value <syntax-val>` :math:`\val` from the stack.
10651113

1066-
.. todo::
1067-
Define
1114+
The value :math:`\val` is the result of the evaluation.
10681115

10691116
.. math::
10701117
\frac{
1071-
S; F; \instr^\ast \stepto^\ast S; F; v
1118+
S; F; \instr^\ast \stepto^\ast S'; F'; v
10721119
}{
1073-
S; F; \instr^\ast~\END \stepto^\ast S; F; v
1120+
S; F; \instr^\ast~\END \stepto^\ast S'; F'; v
10741121
}

0 commit comments

Comments
 (0)