Skip to content

Commit e3c2bcc

Browse files
author
Luke Wagner
committed
BinaryEncoding.md changes implied by #682
1 parent d67792a commit e3c2bcc

File tree

2 files changed

+143
-42
lines changed

2 files changed

+143
-42
lines changed

BinaryEncoding.md

+142-39
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,32 @@ A single-byte unsigned integer indicating a [value type](AstSemantics.md#types).
5959
* `3` indicating type `f32`
6060
* `4` indicating type `f64`
6161

62+
### definition_kind
63+
A single-byte unsigned integer indicating the kind of definition being imported or defined:
64+
* `0` indicating a `Function` [import](Modules.md#imports) or [definition](Modules.md#function-and-code-sections)
65+
* `1` indicating a `Table` [import](Modules.md#imports) or [definition](Modules.md#table-section)
66+
* `2` indicating a `Memory` [import](Modules.md#imports) or [definition](Modules.md#linear-memory-section)
67+
* `3` indicating a `Global` [import](Modules.md#imports) or [definition](Modules.md#global-section)
68+
69+
### resizable_definition
70+
A packed tuple that describes the import or definition of a resizable
71+
[table](AstSemantics.md#table) or [memory](AstSemantics.md#resizing):
72+
73+
| Field | Type | Description |
74+
| ----- | ----- | ----- |
75+
| flags | `varuint32` | described below |
76+
| initial | `varuint32` | initial length (in units of table elements or wasm pages) |
77+
| maximum | `varuint32`? | only present if specified by `flags` |
78+
79+
The `varuint32` "flags" field assigns the following meaning to the bits:
80+
* `0x1` : this is a *default* [table](AstSemantics.md#table)/[memory](AstSemantics.md#linear-memory)
81+
(in the MVP, *every* memory/table import/definition must be flagged as default)
82+
* `0x2` : indicates that the memory/table has a specified maximum
83+
84+
### init_expr
85+
The encoding of an [initializer expression](Modules.md#initializer-expression)
86+
is simply the encoding of the operator (`*.const` or `get_global`) followed by
87+
its immediate (constant value or global index).
6288

6389
# Definitions
6490

@@ -110,6 +136,7 @@ The content of each section is encoded in its `payload_str`.
110136
* [Function](#function-section) section
111137
* [Table](#table-section) section
112138
* [Memory](#memory-section) section
139+
* [Global](#global-section) section
113140
* [Export](#export-section) section
114141
* [Start](#start-section) section
115142
* [Code](#code-section) section
@@ -127,13 +154,13 @@ ID: `type`
127154
The type section declares all function signatures that will be used in the module.
128155

129156
| Field | Type | Description |
130-
| ----- | ----- | ----- |
157+
| ----- | ---- | ----------- |
131158
| count | `varuint32` | count of type entries to follow |
132159
| entries | `type_entry*` | repeated type entries as described below |
133160

134161
#### Type entry
135162
| Field | Type | Description |
136-
| ----- | ----- | ----- |
163+
| ----- | ---- | ----------- |
137164
| form | `varuint7` | `0x40`, indicating a function type |
138165
| param_count | `varuint32` | the number of parameters to the function |
139166
| param_types | `value_type*` | the parameter types of the function |
@@ -149,18 +176,45 @@ ID: `import`
149176
The import section declares all imports that will be used in the module.
150177

151178
| Field | Type | Description |
152-
| ----- | ----- | ----- |
179+
| ----- | ---- | ----------- |
153180
| count | `varuint32` | count of import entries to follow |
154181
| entries | `import_entry*` | repeated import entries as described below |
155182

156183
#### Import entry
157184
| Field | Type | Description |
158-
| ----- | ----- | ----- |
159-
| sig_index | `varuint32` | signature index of the import |
185+
| ----- | ---- | ----------- |
160186
| module_len | `varuint32` | module string length |
161187
| module_str | `bytes` | module string of `module_len` bytes |
162-
| function_len | `varuint32` | function string length |
163-
| function_str | `bytes` | function string of `function_len` bytes |
188+
| field_len | `varuint32` | field name length |
189+
| field_str | `bytes` | field name string of `field_len` bytes |
190+
| kind | `definition_kind` | the kind of definition being imported |
191+
192+
Followed by, if the `kind` is `Function`:
193+
194+
| Field | Type | Description |
195+
| ----- | ---- | ----------- |
196+
| sig_index | `varuint32` | signature index of the import |
197+
198+
or, if the `kind` is `Table`:
199+
200+
| Field | Type | Description |
201+
| ----- | ---- | ----------- |
202+
| table_index | `varuint32` | [table index](Modules.md#table-index-space); must be 0 in the MVP |
203+
| | `resizable_definition` | see [above](#resizable_definition) |
204+
205+
or, if the `kind` is `Memory`:
206+
207+
| Field | Type | Description |
208+
| ----- | ---- | ----------- |
209+
| memory_index | `varuint32` | [linear memory index](Modules.md#linear-memory-index-space); must be 0 in the MVP |
210+
| | `resizable_definition` | see [above](#resizable_definition) |
211+
212+
or, if the `kind` is `Global`:
213+
214+
| Field | Type | Description |
215+
| ----- | ---- | ----------- |
216+
| type | `value_type` | type of the imported global |
217+
| mutability | `uint8` | `0` if immutable, `1` if mutable; must be `0` in the MVP |
164218

165219
### Function section
166220

@@ -170,52 +224,79 @@ The function section _declares_ the signatures of all functions in the
170224
module (their definitions appear in the [code section](#code-section)).
171225

172226
| Field | Type | Description |
173-
| ----- | ----- | ----- |
227+
| ----- | ---- | ----------- |
174228
| count | `varuint32` | count of signature indices to follow |
175229
| types | `varuint32*` | sequence of indices into the type section |
176230

177231
### Table section
178232

179233
ID: `table`
180234

181-
The table section defines the module's
182-
[indirect function table](AstSemantics.md#calls).
235+
The encoding of a [Table section](Modules.md#table-section):
183236

184237
| Field | Type | Description |
185-
| ----- | ----- | ----- |
186-
| count | `varuint32` | count of entries to follow |
187-
| entries | `varuint32*` | repeated indexes into the function section |
238+
| ----- | ---- | ----------- |
239+
| element_type | `varuint7` | `0x40`, indicating [`anyfunc`](AstSemantics.md#table) |
240+
| | `resizable_definition` | see [above](#resizable_definition) |
188241

189242
### Memory section
190243

191244
ID: `memory`
192245

193-
The memory section declares the size and characteristics of the memory
194-
associated with the module.
246+
The encoding of a [Memory section](Modules.md#linear-memory-section) is simply
247+
a `resizable_definition`:
195248

196249
| Field | Type | Description |
197-
| ----- | ----- | ----- |
198-
| initial | `varuint32` | initial memory size in 64KiB pages |
199-
| maximum | `varuint32` | maximum memory size in 64KiB pages |
200-
| exported | `uint8` | `1` if the memory is visible outside the module |
250+
| ----- | ---- | ----------- |
251+
| | `resizable_definition` | see [above](#resizable_definition) |
252+
253+
Note that the initial/maximum fields are specified in units of
254+
[WebAssembly pages](AstSemantics.md#linear-memory).
255+
256+
### Global section
257+
258+
ID: `global`
259+
260+
The encoding of the [Global section](Modules.md#global-section):
261+
262+
| Field | Type | Description |
263+
| ----- | ---- | ----------- |
264+
| count | `variable_entry` | count of global [variable entries](#variable-entry) as described below |
265+
| globals | `global_entry*` | global variable entries, as described below |
266+
267+
#### Global Entry
268+
269+
Each `global_entry` declares a number of global variables of a given type and mutability.
270+
It is legal to have several entries with the same type.
271+
272+
| Field | Type | Description |
273+
| ----- | ---- | ----------- |
274+
| count | `varuint32` | number of global variables of the following type/mutability |
275+
| type | `value_type` | type of the variables |
276+
| mutability | `uint8` | `0` if immutable, `1` if mutable |
201277

202278
### Export section
203279

204280
ID: `export`
205281

206-
The export section declares all exports from the module.
282+
The encoding of the [Export section](Modules.md#exports):
207283

208284
| Field | Type | Description |
209-
| ----- | ----- | ----- |
285+
| ----- | ---- | ----------- |
210286
| count | `varuint32` | count of export entries to follow |
211287
| entries | `export_entry*` | repeated export entries as described below |
212288

213289
#### Export entry
214290
| Field | Type | Description |
215-
| ----- | ----- | ----- |
216-
| func_index | `varuint32` | index into the function table |
217-
| function_len | `varuint32` | function string length |
218-
| function_str | `bytes` | function string of `function_len` bytes |
291+
| ----- | ---- | ----------- |
292+
| field_len | `varuint32` | field name string length |
293+
| field_str | `bytes` | field name string of `field_len` bytes |
294+
| kind | `definition_kind` | the kind of definition being exported |
295+
| index | `varuint32` | the index into the corresponding [index space](Modules.md) |
296+
297+
For example, if the "kind" is `Function`, then "index" is a
298+
[function index](Modules.md#function-index-space). Note that, in the MVP, the
299+
only valid index value for a memory or table export is 0.
219300

220301
### Start section
221302

@@ -224,7 +305,7 @@ ID: `start`
224305
The start section declares the [start function](Modules.md#module-start-function).
225306

226307
| Field | Type | Description |
227-
| ----- | ----- | ----- |
308+
| ----- | ---- | ----------- |
228309
| index | `varuint32` | start function index |
229310

230311
### Code section
@@ -236,8 +317,8 @@ The count of function declared in the [function section](#function-section)
236317
and function bodies defined in this section must be the same and the `i`th
237318
declaration corresponds to the `i`th function body.
238319

239-
| Field | Type | Description |
240-
| ----- | ----- | ----- | ----- |
320+
| Field | Type | Description |
321+
| ----- | ---- | ----------- |
241322
| count | `varuint32` | count of function bodies to follow |
242323
| bodies | `function_body*` | sequence of [Function Bodies](#function-bodies) |
243324

@@ -249,18 +330,39 @@ The data section declares the initialized data that is loaded
249330
into the linear memory.
250331

251332
| Field | Type | Description |
252-
| ----- | ----- | ----- |
333+
| ----- | ---- | ----------- |
253334
| count | `varuint32` | count of data segments to follow |
254335
| entries | `data_segment*` | repeated data segments as described below |
255336

256337
a `data_segment` is:
257338

258339
| Field | Type | Description |
259-
| ----- | ----- | ----- |
260-
| offset | `varuint32` | the offset in linear memory at which to store the data |
340+
| ----- | ---- | ----------- |
341+
| index | `varuint32` | the [linear memory index](Modules.md#linear-memory-index-space) (0 in the MVP) |
342+
| offset | `init_expr` | an `i32` initializer expression that computes the offset at which to place the data |
261343
| size | `varuint32` | size of `data` (in bytes) |
262344
| data | `bytes` | sequence of `size` bytes |
263345

346+
### Element section
347+
348+
ID: `elem`
349+
350+
The encoding of the [Elements section](Modules.md#elements-section):
351+
352+
| Field | Type | Description |
353+
| ----- | ---- | ----------- |
354+
| count | `varuint32` | count of element segments to follow |
355+
| entries | `elem_segment*` | repeated element segments as described below |
356+
357+
a `elem_segment` is:
358+
359+
| Field | Type | Description |
360+
| ----- | ---- | ----------- |
361+
| index | `varuint32` | the [table index](Modules.md#table-index-space) (0 in the MVP) |
362+
| offset | `init_expr` | an `i32` initializer expression that computes the offset at which to place the elements |
363+
| num_elem | `varuint32` | number of elements to follow |
364+
| elems | `varuint32*` | sequence of [function indices](Modules.md#function-index-space) |
365+
264366
### Name section
265367

266368
ID: `name`
@@ -273,7 +375,7 @@ environment, the names in this section will be used as the names of functions
273375
and locals in the [text format](TextFormat.md).
274376

275377
| Field | Type | Description |
276-
| ----- | ----- | ----- |
378+
| ----- | ---- | ----------- |
277379
| count | `varuint32` | count of entries to follow |
278380
| entries | `function_names*` | sequence of names |
279381

@@ -284,7 +386,7 @@ functions.
284386
#### Function names
285387

286388
| Field | Type | Description |
287-
| ----- | ----- | ----- |
389+
| ----- | ---- | ----------- |
288390
| fun_name_len | `varuint32` | string length, in bytes |
289391
| fun_name_str | `bytes` | valid utf8 encoding |
290392
| local_count | `varuint32` | count of local names to follow |
@@ -296,7 +398,7 @@ count may be greater or less than the actual number of locals.
296398
#### Local name
297399

298400
| Field | Type | Description |
299-
| ----- | ----- | ----- |
401+
| ----- | ---- | ----------- |
300402
| local_name_len | `varuint32` | string length, in bytes |
301403
| local_name_str | `bytes` | valid utf8 encoding |
302404

@@ -309,8 +411,8 @@ Each node in the abstract syntax tree corresponds to an operator, such as `i32.a
309411
Operators are encoding by an opcode byte followed by immediate bytes (if any), followed by children
310412
nodes (if any).
311413

312-
| Field | Type |Description |
313-
| ----- | ----- | ----- |
414+
| Field | Type | Description |
415+
| ----- | ---- | ----------- |
314416
| body_size | `varuint32` | size of function body to follow, in bytes |
315417
| local_count | `varuint32` | number of local entries |
316418
| locals | `local_entry*` | local variables |
@@ -322,7 +424,7 @@ Each local entry declares a number of local variables of a given type.
322424
It is legal to have several entries with the same type.
323425

324426
| Field | Type | Description |
325-
| ----- | ----- | ----- |
427+
| ----- | ---- | ----------- |
326428
| count | `varuint32` | number of local variables of the following type |
327429
| type | `value_type` | type of the variables |
328430

@@ -374,9 +476,10 @@ out of range, `br_table` branches to the default target.
374476
| `get_local` | `0x14` | local_index : `varuint32` | read a local variable or parameter |
375477
| `set_local` | `0x15` | local_index : `varuint32` | write a local variable or parameter |
376478
| `tee_local` | `0x19` | local_index : `varuint32` | write a local variable or parameter and return the same value |
377-
| `call` | `0x16` | argument_count : `varuint1`, function_index : `varuint32` | call a function by its index |
479+
| `get_global` | `0x18` | global_index : `varuint32` | read a global variable |
480+
| `set_global` | `0x19` | global_index : `varuint32` | write a global variable |
481+
| `call` | `0x16` | argument_count : `varuint1`, function_index : `varuint32` | call a function by its [index](Modules.md#function-index-space) |
378482
| `call_indirect` | `0x17` | argument_count : `varuint1`, type_index : `varuint32` | call a function indirect with an expected signature |
379-
| `call_import` | `0x18` | argument_count : `varuint1`, import_index : `varuint32` | call an imported function by its index |
380483

381484
The counts following the different call opcodes specify the number of preceding operands taken as arguments.
382485

Modules.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,7 @@ specify the initial contents of fixed `(offset, length)` ranges of a given
285285
table, specified by its [table index](#table-index-space). The `length` is an
286286
integer constant value (defining the length of the given segment). The `offset`
287287
is an [initializer expression](#initializer-expression). Elements are specified
288-
with a `(type, index)` pair where `type` is the element type of an
289-
[index space](Modules.md) that is compatible with the table's element type and
290-
`index` is an integer immediate into `type`s index space.
288+
by their index into the corresponding [index space](Modules.md).
291289

292290
## Function and Code sections
293291

0 commit comments

Comments
 (0)