wadec 0.0.1

A library for decoding WebAssembly modules.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
.. index:: ! embedding, embedder, implementation, host
.. _embed:

Embedding
---------

A WebAssembly implementation will typically be *embedded* into a *host* environment.
An *embedder* implements the connection between such a host environment and the WebAssembly semantics as defined in the main body of this specification.
An embedder is expected to interact with the semantics in well-defined ways.

This section defines a suitable interface to the WebAssembly semantics in the form of entry points through which an embedder can access it.
The interface is intended to be complete, in the sense that an embedder does not need to reference other functional parts of the WebAssembly specification directly.

.. note::
   On the other hand, an embedder does not need to provide the host environment with access to all functionality defined in this interface.
   For example, an implementation may not support :ref:`parsing <embed-module-parse>` of the :ref:`text format <text>`.

Types
~~~~~

In the description of the embedder interface, syntactic classes from the :ref:`abstract syntax <syntax>` and the :ref:`runtime's abstract machine <syntax-runtime>` are used as names for variables that range over the possible objects from that class.
Hence, these syntactic classes can also be interpreted as types.

For numeric parameters, notation like :math:`n:\u32` is used to specify a symbolic name in addition to the respective value range.


.. _embed-error:

Errors
~~~~~~

Failure of an interface operation is indicated by an auxiliary syntactic class:

.. math::
   \begin{array}{llll}
   \production{error} & \error &::=& \ERROR \\
   \end{array}

In addition to the error conditions specified explicitly in this section, implementations may also return errors when specific :ref:`implementation limitations <impl>` are reached.

.. note::
   Errors are abstract and unspecific with this definition.
   Implementations can refine it to carry suitable classifications and diagnostic messages.


Pre- and Post-Conditions
~~~~~~~~~~~~~~~~~~~~~~~~

Some operations state *pre-conditions* about their arguments or *post-conditions* about their results.
It is the embedder's responsibility to meet the pre-conditions.
If it does, the post conditions are guaranteed by the semantics.

In addition to pre- and post-conditions explicitly stated with each operation, the specification adopts the following conventions for :ref:`runtime objects <syntax-runtime>` (:math:`\store`, :math:`\moduleinst`, :math:`\externval`, :ref:`addresses <syntax-addr>`):

* Every runtime object passed as a parameter must be :ref:`valid <valid-store>` per an implicit pre-condition.

* Every runtime object returned as a result is :ref:`valid <valid-store>` per an implicit post-condition.

.. note::
   As long as an embedder treats runtime objects as abstract and only creates and manipulates them through the interface defined here, all implicit pre-conditions are automatically met.



.. index:: allocation, store
.. _embed-store:

Store
~~~~~

.. _embed-store-init:

:math:`\F{store\_init}() : \store`
..................................

1. Return the empty :ref:`store <syntax-store>`.

.. math::
   \begin{array}{lclll}
   \F{store\_init}() &=& \{ \SFUNCS~\epsilon,~ \SMEMS~\epsilon,~ \STABLES~\epsilon,~ \SGLOBALS~\epsilon \} \\
   \end{array}



.. index:: module
.. _embed-module:

Modules
~~~~~~~

.. index:: binary format
.. _embed-module-decode:

:math:`\F{module\_decode}(\byte^\ast) : \module ~|~ \error`
...........................................................

1. If there exists a derivation for the :ref:`byte <syntax-byte>` sequence :math:`\byte^\ast` as a :math:`\Bmodule` according to the :ref:`binary grammar for modules <binary-module>`, yielding a :ref:`module <syntax-module>` :math:`m`, then return :math:`m`.

2. Else, return :math:`\ERROR`.

.. math::
   \begin{array}{lclll}
   \F{module\_decode}(b^\ast) &=& m && (\iff \Bmodule \stackrel\ast\Longrightarrow m{:}b^\ast) \\
   \F{module\_decode}(b^\ast) &=& \ERROR && (\otherwise) \\
   \end{array}


.. index:: text format
.. _embed-module-parse:

:math:`\F{module\_parse}(\char^\ast) : \module ~|~ \error`
..........................................................

1. If there exists a derivation for the :ref:`source <text-source>` :math:`\char^\ast` as a :math:`\Tmodule` according to the :ref:`text grammar for modules <text-module>`, yielding a :ref:`module <syntax-module>` :math:`m`, then return :math:`m`.

2. Else, return :math:`\ERROR`.

.. math::
   \begin{array}{lclll}
   \F{module\_parse}(c^\ast) &=& m && (\iff \Tmodule \stackrel\ast\Longrightarrow m{:}c^\ast) \\
   \F{module\_parse}(c^\ast) &=& \ERROR && (\otherwise) \\
   \end{array}


.. index:: validation
.. _embed-module-validate:

:math:`\F{module\_validate}(\module) : \error^?`
................................................

1. If :math:`\module` is :ref:`valid <valid-module>`, then return nothing.

2. Else, return :math:`\ERROR`.

.. math::
   \begin{array}{lclll}
   \F{module\_validate}(m) &=& \epsilon && (\iff {} \vdashmodule m : \externtype^\ast \to {\externtype'}^\ast) \\
   \F{module\_validate}(m) &=& \ERROR && (\otherwise) \\
   \end{array}


.. index:: instantiation, module instance
.. _embed-module-instantiate:

:math:`\F{module\_instantiate}(\store, \module, \externval^\ast) : (\store, \moduleinst ~|~ \error)`
....................................................................................................

1. Try :ref:`instantiating <exec-instantiation>` :math:`\module` in :math:`\store` with :ref:`external values <syntax-externval>` :math:`\externval^\ast` as imports:

  a. If it succeeds with a :ref:`module instance <syntax-moduleinst>` :math:`\moduleinst`, then let :math:`\X{result}` be :math:`\moduleinst`.

  b. Else, let :math:`\X{result}` be :math:`\ERROR`.

2. Return the new store paired with :math:`\X{result}`.

.. math::
   \begin{array}{lclll}
   \F{module\_instantiate}(S, m, \X{ev}^\ast) &=& (S', F.\AMODULE) && (\iff \instantiate(S, m, \X{ev}^\ast) \stepto^\ast S'; F; \epsilon) \\
   \F{module\_instantiate}(S, m, \X{ev}^\ast) &=& (S', \ERROR) && (\iff \instantiate(S, m, \X{ev}^\ast) \stepto^\ast S'; F; \TRAP) \\
   \end{array}

.. note::
   The store may be modified even in case of an error.


.. index:: import
.. _embed-module-imports:

:math:`\F{module\_imports}(\module) : (\name, \name, \externtype)^\ast`
.......................................................................

1. Pre-condition: :math:`\module` is :ref:`valid <valid-module>` with external import types :math:`\externtype^\ast` and external export types :math:`{\externtype'}^\ast`.

2. Let :math:`\import^\ast` be the :ref:`imports <syntax-import>` :math:`\module.\MIMPORTS`.

3. Assert: the length of :math:`\import^\ast` equals the length of :math:`\externtype^\ast`.

4. For each :math:`\import_i` in :math:`\import^\ast` and corresponding :math:`\externtype_i` in :math:`\externtype^\ast`, do:

  a. Let :math:`\X{result}_i` be the triple :math:`(\import_i.\IMODULE, \import_i.\INAME, \externtype_i)`.

5. Return the concatenation of all :math:`\X{result}_i`, in index order.

6. Post-condition: each :math:`\externtype_i` is :ref:`valid <valid-externtype>`.

.. math::
   ~ \\
   \begin{array}{lclll}
   \F{module\_imports}(m) &=& (\X{im}.\IMODULE, \X{im}.\INAME, \externtype)^\ast \\
     && \qquad (\iff \X{im}^\ast = m.\MIMPORTS \wedge {} \vdashmodule m : \externtype^\ast \to {\externtype'}^\ast) \\
   \end{array}


.. index:: export
.. _embed-module-exports:

:math:`\F{module\_exports}(\module) : (\name, \externtype)^\ast`
................................................................

1. Pre-condition: :math:`\module` is :ref:`valid <valid-module>` with external import types :math:`\externtype^\ast` and external export types :math:`{\externtype'}^\ast`.

2. Let :math:`\export^\ast` be the :ref:`exports <syntax-export>` :math:`\module.\MEXPORTS`.

3. Assert: the length of :math:`\export^\ast` equals the length of :math:`{\externtype'}^\ast`.

4. For each :math:`\export_i` in :math:`\export^\ast` and corresponding :math:`\externtype'_i` in :math:`{\externtype'}^\ast`, do:

  a. Let :math:`\X{result}_i` be the pair :math:`(\export_i.\ENAME, \externtype'_i)`.

5. Return the concatenation of all :math:`\X{result}_i`, in index order.

6. Post-condition: each :math:`\externtype'_i` is :ref:`valid <valid-externtype>`.

.. math::
   ~ \\
   \begin{array}{lclll}
   \F{module\_exports}(m) &=& (\X{ex}.\ENAME, \externtype')^\ast \\
     && \qquad (\iff \X{ex}^\ast = m.\MEXPORTS \wedge {} \vdashmodule m : \externtype^\ast \to {\externtype'}^\ast) \\
   \end{array}


.. index:: module, module instance
.. _embed-instance:

Module Instances
~~~~~~~~~~~~~~~~

.. index:: export, export instance

.. _embed-instance-export:

:math:`\F{instance\_export}(\moduleinst, \name) : \externval ~|~ \error`
........................................................................

1. Assert: due to :ref:`validity <valid-moduleinst>` of the :ref:`module instance <syntax-moduleinst>` :math:`\moduleinst`, all its :ref:`export names <syntax-exportinst>` are different.

2. If there exists an :math:`\exportinst_i` in :math:`\moduleinst.\MIEXPORTS` such that :ref:`name <syntax-name>` :math:`\exportinst_i.\EINAME` equals :math:`\name`, then:

   a. Return the :ref:`external value <syntax-externval>` :math:`\exportinst_i.\EIVALUE`.

3. Else, return :math:`\ERROR`.

.. math::
   ~ \\
   \begin{array}{lclll}
   \F{instance\_export}(m, \name) &=& m.\MIEXPORTS[i].\EIVALUE && (\iff m.\MEXPORTS[i].\EINAME = \name) \\
   \F{instance\_export}(m, \name) &=& \ERROR && (\otherwise) \\
   \end{array}


.. index:: function, host function, function address, function instance, function type, store
.. _embed-func:

Functions
~~~~~~~~~

.. _embed-func-alloc:

:math:`\F{func\_alloc}(\store, \functype, \hostfunc) : (\store, \funcaddr)`
...........................................................................

1. Pre-condition: :math:`\functype` is :ref:`valid <valid-functype>`.

2. Let :math:`\funcaddr` be the result of :ref:`allocating a host function <alloc-func>` in :math:`\store` with :ref:`function type <syntax-functype>` :math:`\functype` and host function code :math:`\hostfunc`.

3. Return the new store paired with :math:`\funcaddr`.

.. math::
   \begin{array}{lclll}
   \F{func\_alloc}(S, \X{ft}, \X{code}) &=& (S', \X{a}) && (\iff \allochostfunc(S, \X{ft}, \X{code}) = S', \X{a}) \\
   \end{array}

.. note::
   This operation assumes that :math:`\hostfunc` satisfies the :ref:`pre- and post-conditions <exec-invoke-host>` required for a function instance with type :math:`\functype`.

   Regular (non-host) function instances can only be created indirectly through :ref:`module instantiation <embed-module-instantiate>`.


.. _embed-func-type:

:math:`\F{func\_type}(\store, \funcaddr) : \functype`
.....................................................

1. Return :math:`S.\SFUNCS[a].\FITYPE`.

2. Post-condition: the returned :ref:`function type <syntax-functype>` is :ref:`valid <valid-functype>`.

.. math::
   \begin{array}{lclll}
   \F{func\_type}(S, a) &=& S.\SFUNCS[a].\FITYPE \\
   \end{array}


.. index:: invocation, value, result
.. _embed-func-invoke:

:math:`\F{func\_invoke}(\store, \funcaddr, \val^\ast) : (\store, \val^\ast ~|~ \error)`
........................................................................................

1. Try :ref:`invoking <exec-invocation>` the function :math:`\funcaddr` in :math:`\store` with :ref:`values <syntax-val>` :math:`\val^\ast` as arguments:

  a. If it succeeds with :ref:`values <syntax-val>` :math:`{\val'}^\ast` as results, then let :math:`\X{result}` be :math:`{\val'}^\ast`.

  b. Else it has trapped, hence let :math:`\X{result}` be :math:`\ERROR`.

2. Return the new store paired with :math:`\X{result}`.

.. math::
   ~ \\
   \begin{array}{lclll}
   \F{func\_invoke}(S, a, v^\ast) &=& (S', {v'}^\ast) && (\iff \invoke(S, a, v^\ast) \stepto^\ast S'; F; {v'}^\ast) \\
   \F{func\_invoke}(S, a, v^\ast) &=& (S', \ERROR) && (\iff \invoke(S, a, v^\ast) \stepto^\ast S'; F; \TRAP) \\
   \end{array}

.. note::
   The store may be modified even in case of an error.


.. index:: table, table address, store, table instance, table type, element, function address
.. _embed-table:

Tables
~~~~~~

.. _embed-table-alloc:

:math:`\F{table\_alloc}(\store, \tabletype, \reff) : (\store, \tableaddr)`
..........................................................................

1. Pre-condition: :math:`\tabletype` is :ref:`valid <valid-tabletype>`.

2. Let :math:`\tableaddr` be the result of :ref:`allocating a table <alloc-table>` in :math:`\store` with :ref:`table type <syntax-tabletype>` :math:`\tabletype` and initialization value :math:`\reff`.

3. Return the new store paired with :math:`\tableaddr`.

.. math::
   \begin{array}{lclll}
   \F{table\_alloc}(S, \X{tt}, r) &=& (S', \X{a}) && (\iff \alloctable(S, \X{tt}, r) = S', \X{a}) \\
   \end{array}


.. _embed-table-type:

:math:`\F{table\_type}(\store, \tableaddr) : \tabletype`
........................................................

1. Return :math:`S.\STABLES[a].\TITYPE`.

2. Post-condition: the returned :ref:`table type <syntax-tabletype>` is :ref:`valid <valid-tabletype>`.

.. math::
   \begin{array}{lclll}
   \F{table\_type}(S, a) &=& S.\STABLES[a].\TITYPE \\
   \end{array}


.. _embed-table-read:

:math:`\F{table\_read}(\store, \tableaddr, i:\u32) : \reff ~|~ \error`
......................................................................

1. Let :math:`\X{ti}` be the :ref:`table instance <syntax-tableinst>` :math:`\store.\STABLES[\tableaddr]`.

2. If :math:`i` is larger than or equal to the length of :math:`\X{ti}.\TIELEM`, then return :math:`\ERROR`.

3. Else, return the :ref:`reference value <syntax-ref>` :math:`\X{ti}.\TIELEM[i]`.

.. math::
   \begin{array}{lclll}
   \F{table\_read}(S, a, i) &=& r && (\iff S.\STABLES[a].\TIELEM[i] = r) \\
   \F{table\_read}(S, a, i) &=& \ERROR && (\otherwise) \\
   \end{array}


.. _embed-table-write:

:math:`\F{table\_write}(\store, \tableaddr, i:\u32, \reff) : \store ~|~ \error`
...............................................................................

1. Let :math:`\X{ti}` be the :ref:`table instance <syntax-tableinst>` :math:`\store.\STABLES[\tableaddr]`.

2. If :math:`i` is larger than or equal to the length of :math:`\X{ti}.\TIELEM`, then return :math:`\ERROR`.

3. Replace :math:`\X{ti}.\TIELEM[i]` with the :ref:`reference value <syntax-ref>` :math:`\reff`.

4. Return the updated store.

.. math::
   \begin{array}{lclll}
   \F{table\_write}(S, a, i, r) &=& S' && (\iff S' = S \with \STABLES[a].\TIELEM[i] = r) \\
   \F{table\_write}(S, a, i, r) &=& \ERROR && (\otherwise) \\
   \end{array}


.. _embed-table-size:

:math:`\F{table\_size}(\store, \tableaddr) : \u32`
..................................................

1. Return the length of :math:`\store.\STABLES[\tableaddr].\TIELEM`.

.. math::
   ~ \\
   \begin{array}{lclll}
   \F{table\_size}(S, a) &=& n &&
     (\iff |S.\STABLES[a].\TIELEM| = n) \\
   \end{array}



.. _embed-table-grow:

:math:`\F{table\_grow}(\store, \tableaddr, n:\u32, \reff) : \store ~|~ \error`
..............................................................................

1. Try :ref:`growing <grow-table>` the :ref:`table instance <syntax-tableinst>` :math:`\store.\STABLES[\tableaddr]` by :math:`n` elements with initialization value :math:`\reff`:

   a. If it succeeds, return the updated store.

   b. Else, return :math:`\ERROR`.

.. math::
   ~ \\
   \begin{array}{lclll}
   \F{table\_grow}(S, a, n, r) &=& S' &&
     (\iff S' = S \with \STABLES[a] = \growtable(S.\STABLES[a], n, r)) \\
   \F{table\_grow}(S, a, n, r) &=& \ERROR && (\otherwise) \\
   \end{array}


.. index:: memory, memory address, store, memory instance, memory type, byte
.. _embed-mem:

Memories
~~~~~~~~

.. _embed-mem-alloc:

:math:`\F{mem\_alloc}(\store, \memtype) : (\store, \memaddr)`
................................................................

1. Pre-condition: :math:`\memtype` is :ref:`valid <valid-memtype>`.

2. Let :math:`\memaddr` be the result of :ref:`allocating a memory <alloc-mem>` in :math:`\store` with :ref:`memory type <syntax-memtype>` :math:`\memtype`.

3. Return the new store paired with :math:`\memaddr`.

.. math::
   \begin{array}{lclll}
   \F{mem\_alloc}(S, \X{mt}) &=& (S', \X{a}) && (\iff \allocmem(S, \X{mt}) = S', \X{a}) \\
   \end{array}


.. _embed-mem-type:

:math:`\F{mem\_type}(\store, \memaddr) : \memtype`
..................................................

1. Return :math:`S.\SMEMS[a].\MITYPE`.

2. Post-condition: the returned :ref:`memory type <syntax-memtype>` is :ref:`valid <valid-memtype>`.

.. math::
   \begin{array}{lclll}
   \F{mem\_type}(S, a) &=& S.\SMEMS[a].\MITYPE \\
   \end{array}


.. _embed-mem-read:

:math:`\F{mem\_read}(\store, \memaddr, i:\u32) : \byte ~|~ \error`
..................................................................

1. Let :math:`\X{mi}` be the :ref:`memory instance <syntax-meminst>` :math:`\store.\SMEMS[\memaddr]`.

2. If :math:`i` is larger than or equal to the length of :math:`\X{mi}.\MIDATA`, then return :math:`\ERROR`.

3. Else, return the  :ref:`byte <syntax-byte>` :math:`\X{mi}.\MIDATA[i]`.

.. math::
   \begin{array}{lclll}
   \F{mem\_read}(S, a, i) &=& b && (\iff S.\SMEMS[a].\MIDATA[i] = b) \\
   \F{mem\_read}(S, a, i) &=& \ERROR && (\otherwise) \\
   \end{array}


.. _embed-mem-write:

:math:`\F{mem\_write}(\store, \memaddr, i:\u32, \byte) : \store ~|~ \error`
...........................................................................

1. Let :math:`\X{mi}` be the :ref:`memory instance <syntax-meminst>` :math:`\store.\SMEMS[\memaddr]`.

2. If :math:`\u32` is larger than or equal to the length of :math:`\X{mi}.\MIDATA`, then return :math:`\ERROR`.

3. Replace :math:`\X{mi}.\MIDATA[i]` with :math:`\byte`.

4. Return the updated store.

.. math::
   \begin{array}{lclll}
   \F{mem\_write}(S, a, i, b) &=& S' && (\iff S' = S \with \SMEMS[a].\MIDATA[i] = b) \\
   \F{mem\_write}(S, a, i, b) &=& \ERROR && (\otherwise) \\
   \end{array}


.. _embed-mem-size:

:math:`\F{mem\_size}(\store, \memaddr) : \u32`
..............................................

1. Return the length of :math:`\store.\SMEMS[\memaddr].\MIDATA` divided by the :ref:`page size <page-size>`.

.. math::
   ~ \\
   \begin{array}{lclll}
   \F{mem\_size}(S, a) &=& n &&
     (\iff |S.\SMEMS[a].\MIDATA| = n \cdot 64\,\F{Ki}) \\
   \end{array}



.. _embed-mem-grow:

:math:`\F{mem\_grow}(\store, \memaddr, n:\u32) : \store ~|~ \error`
...................................................................

1. Try :ref:`growing <grow-mem>` the :ref:`memory instance <syntax-meminst>` :math:`\store.\SMEMS[\memaddr]` by :math:`n` :ref:`pages <page-size>`:

   a. If it succeeds, return the updated store.

   b. Else, return :math:`\ERROR`.

.. math::
   ~ \\
   \begin{array}{lclll}
   \F{mem\_grow}(S, a, n) &=& S' &&
     (\iff S' = S \with \SMEMS[a] = \growmem(S.\SMEMS[a], n)) \\
   \F{mem\_grow}(S, a, n) &=& \ERROR && (\otherwise) \\
   \end{array}



.. index:: global, global address, store, global instance, global type, value
.. _embed-global:

Globals
~~~~~~~

.. _embed-global-alloc:

:math:`\F{global\_alloc}(\store, \globaltype, \val) : (\store, \globaladdr)`
............................................................................

1. Pre-condition: :math:`\globaltype` is :ref:`valid <valid-globaltype>`.

2. Let :math:`\globaladdr` be the result of :ref:`allocating a global <alloc-global>` in :math:`\store` with :ref:`global type <syntax-globaltype>` :math:`\globaltype` and initialization value :math:`\val`.

3. Return the new store paired with :math:`\globaladdr`.

.. math::
   \begin{array}{lclll}
   \F{global\_alloc}(S, \X{gt}, v) &=& (S', \X{a}) && (\iff \allocglobal(S, \X{gt}, v) = S', \X{a}) \\
   \end{array}


.. _embed-global-type:

:math:`\F{global\_type}(\store, \globaladdr) : \globaltype`
...........................................................

1. Return :math:`S.\SGLOBALS[a].\GITYPE`.

2. Post-condition: the returned :ref:`global type <syntax-globaltype>` is :ref:`valid <valid-globaltype>`.

.. math::
   \begin{array}{lclll}
   \F{global\_type}(S, a) &=& S.\SGLOBALS[a].\GITYPE \\
   \end{array}


.. _embed-global-read:

:math:`\F{global\_read}(\store, \globaladdr) : \val`
....................................................

1. Let :math:`\X{gi}` be the :ref:`global instance <syntax-globalinst>` :math:`\store.\SGLOBALS[\globaladdr]`.

2. Return the :ref:`value <syntax-val>` :math:`\X{gi}.\GIVALUE`.

.. math::
   \begin{array}{lclll}
   \F{global\_read}(S, a) &=& v && (\iff S.\SGLOBALS[a].\GIVALUE = v) \\
   \end{array}


.. _embed-global-write:

:math:`\F{global\_write}(\store, \globaladdr, \val) : \store ~|~ \error`
........................................................................

1. Let :math:`\X{gi}` be the :ref:`global instance <syntax-globalinst>` :math:`\store.\SGLOBALS[\globaladdr]`.

2. Let :math:`\mut~t` be the structure of the :ref:`global type <syntax-globaltype>` :math:`\X{gi}.\GITYPE`.

3. If :math:`\mut` is not :math:`\MVAR`, then return :math:`\ERROR`.

4. Replace :math:`\X{gi}.\GIVALUE` with the :ref:`value <syntax-val>` :math:`\val`.

5. Return the updated store.

.. math::
   ~ \\
   \begin{array}{lclll}
   \F{global\_write}(S, a, v) &=& S' && (\iff S.\SGLOBALS[a].\GITYPE = \MVAR~t \wedge S' = S \with \SGLOBALS[a].\GIVALUE = v) \\
   \F{global\_write}(S, a, v) &=& \ERROR && (\otherwise) \\
   \end{array}