aboutsummaryrefslogtreecommitdiff
path: root/BinaryEncoding.md
diff options
context:
space:
mode:
authorAndreas Rossberg <rossberg@chromium.org>2016-03-01 12:11:42 +0100
committerAndreas Rossberg <rossberg@chromium.org>2016-03-01 12:11:42 +0100
commit19b7cf90f07ad23a0261f775f301febab753edfa (patch)
treeb913107e515a80e7ab2486d69c4aa884f7a43095 /BinaryEncoding.md
parent13e0758ad4ca3f880c3b7fb3afe41ad3377308dd (diff)
parentc8137d9a6bdefd015f5daadd3f0eb233dc6996b6 (diff)
downloadnanowasm-design-19b7cf90f07ad23a0261f775f301febab753edfa.tar.gz
Merge branch 'master' into locals
Diffstat (limited to 'BinaryEncoding.md')
-rw-r--r--BinaryEncoding.md140
1 files changed, 109 insertions, 31 deletions
diff --git a/BinaryEncoding.md b/BinaryEncoding.md
index ae97f42..1c3c5df 100644
--- a/BinaryEncoding.md
+++ b/BinaryEncoding.md
@@ -39,7 +39,7 @@ A two-byte little endian unsigned integer.
A four-byte little endian unsigned integer.
### varuint32
-A [LEB128](https://en.wikipedia.org/wiki/LEB128) variable-length integer, limited to uint32 values.
+A [LEB128](https://en.wikipedia.org/wiki/LEB128) variable-length integer, limited to uint32 values. `varuint32` values may contain leading zeros.
### value_type
A single-byte unsigned integer indicating a [value type](AstSemantics.md#types). These types are encoded as:
@@ -73,44 +73,83 @@ Strings referenced by the module (i.e. function names) are encoded as null-termi
The following documents the current prototype format. This format is based on and supersedes the v8-native prototype format, originally in a [public design doc](https://docs.google.com/document/d/1-G11CnMA0My20KI9D7dBR6ZCPOBCRD0oCH6SHCPFGx0/edit?usp=sharing).
## High-level structure
-A module contains a sequence of sections, each identified by a *section identifier byte* whose encodings
-are listed below.
+
+The module starts with a magic number and version as follows.
+
+| Field | Type | Description |
+| ----- | ----- | ----- |
+| magic number | `uint32 0x6d736100` | Magic number '\0asm' |
+| version | `uint32` | Version number, currently 10. The version for MVP will be reset to 1. |
+
+This is followed by a sequence of sections. Sections can in general be repeated, but some can occur only once or have dependant sections that must preceed them but not immediately as unknown sections can occur in any order. Each section is identified by an immediate string. Sections whose identity is unknown to the WebAssembly implementation are ignored and this is supported by including the size in bytes for all sections. The encoding of all sections begins as follows:
+
+| Field | Type | Description |
+| ----- | ----- | ----- |
+| size | `varuint32` | size of this section in bytes, excluding this size |
+| id_len | `varuint32` | section identifier string length |
+| id_str | `bytes` | section identifier string of id_len bytes |
### Memory section
+
+ID: `memory`
+
The memory section declares the size and characteristics of the memory associated with the module.
A module may contain at most one memory section.
| Field | Type | Description |
-| ----- | ----- | ----- |
-| id = `0x00` | `uint8` | section identifier for memory |
-| min_mem_size | `uint8` | minimize memory size as a power of `2` |
-| max_mem_size | `uint8` | maximum memory size as a power of `2` |
+| ----- | ----- | ----- |
+| min_mem_pages | `varuint32` | minimize memory size in 64KiB pages |
+| max_mem_pages | `varuint32` | maximum memory size in 64KiB pages |
| exported | `uint8` | `1` if the memory is visible outside the module |
### Signatures section
+
+ID: `signatures`
+
The signatures section declares all function signatures that will be used in the module.
A module may contain at most one signatures section.
| Field | Type | Description |
-| ----- | ----- | ----- |
-| id = `0x01` | `uint8` | section identifier for signatures |
-| count | `varuint32` | count of signature entries to follow |
+| ----- | ----- | ----- |
+| count | `varuint32` | count of signature entries to follow |
| entries | `signature_entry*` | repeated signature entries as described below |
#### Signature entry
| Field | Type | Description |
-| ----- | ----- | ----- |
+| ----- | ----- | ----- |
| param_count | `uint8` | the number of parameters to the function |
| return_type | `value_type?` | the return type of the function, with `0` indicating no return type |
| param_types | `value_type*` | the parameter types of the function |
+### Import table section
+
+ID: `import_table`
+
+The import section declares all imports that will be used in the module.
+A module may contain at most one import table section.
+
+| Field | Type | Description |
+| ----- | ----- | ----- |
+| count | `varuint32` | count of import entries to follow |
+| entries | `import_entry*` | repeated import entries as described below |
+
+#### Import entry
+| Field | Type | Description |
+| ----- | ----- | ----- |
+| sig_index | `uint16` | signature index of the import |
+| module_name | `uint32` | offset from the start of the module of the string representing the module name |
+| func_name | `uint32` | offset from the start of the module of the string representing the function name |
+
+
### Functions section
+
+ID: `functions`
+
The Functions section declares the functions in the module and must be preceded by a [Signatures](#signatures-section) section. A module may contain at most one functions section.
| Field | Type | Description |
-| ----- | ----- | ----- |
-| id = `0x02` | `uint8` | section identifier for functions |
-| count | `varuint32` | count of function entries to follow |
+| ----- | ----- | ----- |
+| count | `varuint32` | count of function entries to follow |
| entries | `function_entry*` | repeated function entries as described below |
#### Function Entry
@@ -119,7 +158,7 @@ Each function entry describes a function that can be optionally named, imported
must contain a function body. Imported and exported functions must have a name. Imported functions do not contain a body.
| Field | Type | Present? | Description |
-| ----- | ----- | ----- | ----- |
+| ----- | ----- | ----- | ----- |
| flags | `uint8` | always | flags indicating attributes of a function <br>bit `0` : a name is present<br>bit `1` : the function is an import<br>bit `2` : the function has local variables<br>bit `3` : the function is an export |
| signature | `uint16` | always | index into the Signature section |
| name | `uint32` | `flags[0] == 1` | name of the function as an offset within the module |
@@ -138,14 +177,47 @@ It is legal to have several entries with the same type.
| count | `varuint32` | number of local variables of the following type |
| type | `value_type` | type of the variables |
+### Export Table section
+
+ID: `export_table`
+
+The export table section declares all exports from the module.
+A module may contain at most one export table section.
+This section must be preceded by a [Functions](#functions-section) section.
+
+| Field | Type | Description |
+| ----- | ----- | ----- |
+| count | `varuint32` | count of export entries to follow |
+| entries | `export_entry*` | repeated export entries as described below |
+
+#### Export entry
+| Field | Type | Description |
+| ----- | ----- | ----- |
+| sig_index | `uint16` | signature index of the export |
+| func_index | `uint16` | index into the function table |
+| func_name | `uint32` | offset from the start of the module of the string representing the export name |
+
+### Start Function section
+
+ID: `start_function`
+
+A module may contain at most one start fuction section.
+This section must be preceded by a [Functions](#functions-section) section.
+
+| Field | Type | Description |
+| ----- | ----- | ----- |
+| index | `varuint32` | start function index |
+
### Data Segments section
+
+ID: `data_segments`
+
The data segemnts section declares the initialized data that should be loaded into the linear memory.
A module may only contain one data segments section.
| Field | Type | Description |
-| ----- | ----- | ----- |
-| id = `0x04` | `uint8` | section identifier for data segments |
-| count | `varuint32` | count of data segments to follow |
+| ----- | ----- | ----- |
+| count | `varuint32` | count of data segments to follow |
| entries | `data_segment*` | repeated data segments as described below |
* ```varuint32```: The number of data segments in the section.
@@ -156,32 +228,38 @@ A module may only contain one data segments section.
- ```uint8```: ```1``` if the segment's data should be automatically loaded into memory at module load time.
### Indirect Function Table section
+
+ID: `function_table`
+
The indirect function table section declares the size and entries of the indirect function table, which consist
of indexes into the [Functions](#functions-section) section.
This section must be preceded by a [Functions](#functions-section) section.
| Field | Type | Description |
-| ----- | ----- | ----- |
-| id = `0x05` | `uint8` | section identifier for indirect function table |
-| count | `varuint32` | count of entries to follow |
+| ----- | ----- | ----- |
+| count | `varuint32` | count of entries to follow |
| entries | `uint16*` | repeated indexes into the function table |
### End section
+
+ID: `end`
+
This indicates the end of the sections. Additional data that follows this section marker is not interpreted
by the decoder. It can used, for example, to store function names or data segment bodies.
| Field | Type | Description |
-| ----- | ----- | ----- |
-| id = `0x06` | `uint8` | section identifier for end of sections |
+| ----- | ----- | ----- |
### Nonstandard: Globals section
+
+ID: `globals`
+
A module may only contain one globals section. This section is currently for V8 internal use.
| Field | Type | Description |
| ----- | ----- | ----- |
-| id = `0x03` | `uint8` | section identifier for globals |
-| count | `varuint32` | count of global variable entries to follow |
+| count | `varuint32` | count of global variable entries to follow |
| entries | `global_variable*` | repeated global variable entries as described below |
#### Global Variable entry
@@ -190,18 +268,18 @@ A global variable entry describes a variable outside the WASM linear memory. A G
stored to from dedicated instructions.
| Field | Type | Description |
-| ----- | ----- | ----- |
-| name | `uint32` | name of the global variable as an offset to a string in the module |
+| ----- | ----- | ----- |
+| name | `uint32` | name of the global variable as an offset to a string in the module |
| type | `uint8` | the type of the global, as a memory type |
| exported | `uint8` | a boolean indicating whether the global variable is exported |
-### WorkInProgress: WLL section
+### Unknown sections
| Field | Type | Description |
| ----- | ----- | ----- |
-| id = `0x11` | `uint8` | section identifier for globals |
-| size | `varuint32` | size of this section in bytes |
-| body | `bytes` | contents of this section |
+| body | `bytes` | contents of this section |
+
+Sections whose ID is unknown to the WebAssembly implementation are ignored.
# AST Encoding