WebAssembly Specification Release 1.1 (Draft 2021-12-18) - page 11

 

  Главная      Manuals     WebAssembly Specification Release 1.1 (Draft 2021-12-18)

 

Search            copyright infringement  

 

 

 

 

 

 

 

 

 

 

 

Content      ..     9      10      11      12     ..

 

 

 

WebAssembly Specification Release 1.1 (Draft 2021-12-18) - page 11

 

 

5.3.3 Reference Types

Reference types

are also encoded by a single byte.

reftype

::=

0x70

⇒

funcref

|

0x6F

⇒

externref

5.3.4 Value Types

Value types

are encoded with their respective encoding as a

number type

or

reference type

.

valtype

::=

𝑡

:

numtype

⇒

𝑡

|

𝑡

:

reftype

⇒

𝑡

Note:

Value types can occur in contexts where

type indices

are also allowed, such as in the case of

block types

.

Thus, the binary format for types corresponds to the

signed LEB128

34

encoding

of small negative

s

𝑁

values, so

that they can coexist with (positive) type indices in the future.

5.3.5 Result Types

Result types

are encoded by the respective

vectors

of

value types `

.

resulttype

::=

𝑡

*

:

vec

(

valtype

)

⇒

[

𝑡

*

]

5.3.6 Function Types

Function types

are encoded by the byte

0x60

followed by the respective

vectors

of parameter and result types.

functype

::=

0x60

rt

1

:

resulttype

rt

2

:

resulttype

⇒

rt

1

→

rt

2

5.3.7 Limits

Limits

are encoded with a preceding flag indicating whether a maximum is present.

limits

::=

0x00

𝑛

:

u32

⇒

{

min

𝑛,

max

𝜖

}

|

0x01

𝑛

:

u32

𝑚

:

u32

⇒

{

min

𝑛,

max

𝑚

}

5.3.8 Memory Types

Memory types

are encoded with their

limits

.

memtype

::=

lim

:

limits

⇒

lim

34

https://en.wikipedia.org/wiki/LEB128#Signed_LEB128

5.3. Types

135

5.3.9 Table Types

Table types

are encoded with their

limits

and the encoding of their element

reference type

.

tabletype

::=

et

:

reftype

lim

:

limits

⇒

lim et

5.3.10 Global Types

Global types

are encoded by their

value type

and a flag for their

mutability

.

globaltype

::=

𝑡

:

valtype

𝑚

:

mut

⇒

𝑚 𝑡

mut

::=

0x00

⇒

const

|

0x01

⇒

var

5.4 Instructions

Instructions

are encoded by

opcodes

. Each opcode is represented by a single byte, and is followed by the instruc-

tion’s immediate arguments, where present. The only exception are

structured control instructions

, which consist

of several opcodes bracketing their nested instruction sequences.

Note:

Gaps in the byte code ranges for encoding instructions are reserved for future extensions.

5.4.1 Control Instructions

Control instructions

have varying encodings. For structured instructions, the instruction sequences forming nested

blocks are terminated with explicit opcodes for

end

and

else

.

Block types

are encoded in special compressed form, by either the byte

0x40

indicating the empty type, as a single

value type

, or as a

type index

encoded as a positive

signed integer

.

blocktype

::=

0x40

⇒

𝜖

|

𝑡

:

valtype

⇒

𝑡

|

𝑥

:

s33

⇒

𝑥

(

if

𝑥

≥

0)

instr

::=

0x00

⇒

unreachable

|

0x01

⇒

nop

|

0x02

bt

:

blocktype

(

in

:

instr

)

*

0x0B

⇒

block

bt in

*

end

|

0x03

bt

:

blocktype

(

in

:

instr

)

*

0x0B

⇒

loop

bt in

*

end

|

0x04

bt

:

blocktype

(

in

:

instr

)

*

0x0B

⇒

if

bt in

*

else

𝜖

end

|

0x04

bt

:

blocktype

(

in

1

:

instr

)

*

0x05

(

in

2

:

instr

)

*

0x0B

⇒

if

bt in

*

1

else

in

*

2

end

|

0x0C

𝑙

:

labelidx

⇒

br

𝑙

|

0x0D

𝑙

:

labelidx

⇒

br

_

if

𝑙

|

0x0E

𝑙

*

:

vec

(

labelidx

)

𝑙

𝑁

:

labelidx

⇒

br

_

table

𝑙

*

𝑙

𝑁

|

0x0F

⇒

return

|

0x10

𝑥

:

funcidx

⇒

call

𝑥

|

0x11

𝑦

:

typeidx

𝑥

:

tableidx

⇒

call

_

indirect

𝑥 𝑦

Note:

The

else

opcode

0x05

in the encoding of an

if

instruction can be omitted if the following instruction

sequence is empty.

136

Chapter 5. Binary Format

Unlike any

other occurrence

, the

type index

in a

block type

is encoded as a positive

signed integer

, so that its signed

LEB128 bit pattern cannot collide with the encoding of

value types

or the special code

0x40

, which correspond to

the LEB128 encoding of negative integers. To avoid any loss in the range of allowed indices, it is treated as a 33

bit signed integer.

5.4.2 Reference Instructions

Reference instructions

are represented by single byte codes.

instr

::=

. . .

|

0xD0

𝑡

:

reftype

⇒

ref

.

null

𝑡

|

0xD1

⇒

ref

.

is

_

null

|

0xD2

𝑥

:

funcidx

⇒

ref

.

func

𝑥

5.4.3 Parametric Instructions

Parametric instructions

are represented by single byte codes, possibly followed by a type annotation.

instr

::=

. . .

|

0x1A

⇒

drop

|

0x1B

⇒

select

|

0x1C

𝑡

*

:

vec

(

valtype

)

⇒

select

𝑡

*

5.4.4 Variable Instructions

Variable instructions

are represented by byte codes followed by the encoding of the respective

index

.

instr

::=

. . .

|

0x20

𝑥

:

localidx

⇒

local

.

get

𝑥

|

0x21

𝑥

:

localidx

⇒

local

.

set

𝑥

|

0x22

𝑥

:

localidx

⇒

local

.

tee

𝑥

|

0x23

𝑥

:

globalidx

⇒

global

.

get

𝑥

|

0x24

𝑥

:

globalidx

⇒

global

.

set

𝑥

5.4.5 Table Instructions

Table instructions

are represented either by a single byte or a one byte prefix followed by a variable-length

unsigned

integer

.

instr

::=

. . .

|

0x25

𝑥

:

tableidx

⇒

table

.

get

𝑥

|

0x26

𝑥

:

tableidx

⇒

table

.

set

𝑥

|

0xFC

12:

u32

𝑦

:

elemidx

𝑥

:

tableidx

⇒

table

.

init

𝑥 𝑦

|

0xFC

13:

u32

𝑥

:

elemidx

⇒

elem

.

drop

𝑥

|

0xFC

14:

u32

𝑥

:

tableidx

𝑦

:

tableidx

⇒

table

.

copy

𝑥 𝑦

|

0xFC

15:

u32

𝑥

:

tableidx

⇒

table

.

grow

𝑥

|

0xFC

16:

u32

𝑥

:

tableidx

⇒

table

.

size

𝑥

|

0xFC

17:

u32

𝑥

:

tableidx

⇒

table

.

fill

𝑥

5.4. Instructions

137

5.4.6 Memory Instructions

Each variant of

memory instruction

is encoded with a different byte code. Loads and stores are followed by the

encoding of their

memarg

immediate.

memarg

::=

𝑎

:

u32

𝑜

:

u32

⇒

{

align

𝑎,

offset

𝑜

}

instr

::=

. . .

|

0x28

𝑚

:

memarg

⇒

i32

.

load

𝑚

|

0x29

𝑚

:

memarg

⇒

i64

.

load

𝑚

|

0x2A

𝑚

:

memarg

⇒

f32

.

load

𝑚

|

0x2B

𝑚

:

memarg

⇒

f64

.

load

𝑚

|

0x2C

𝑚

:

memarg

⇒

i32

.

load

8

_

s

𝑚

|

0x2D

𝑚

:

memarg

⇒

i32

.

load

8

_

u

𝑚

|

0x2E

𝑚

:

memarg

⇒

i32

.

load

16

_

s

𝑚

|

0x2F

𝑚

:

memarg

⇒

i32

.

load

16

_

u

𝑚

|

0x30

𝑚

:

memarg

⇒

i64

.

load

8

_

s

𝑚

|

0x31

𝑚

:

memarg

⇒

i64

.

load

8

_

u

𝑚

|

0x32

𝑚

:

memarg

⇒

i64

.

load

16

_

s

𝑚

|

0x33

𝑚

:

memarg

⇒

i64

.

load

16

_

u

𝑚

|

0x34

𝑚

:

memarg

⇒

i64

.

load

32

_

s

𝑚

|

0x35

𝑚

:

memarg

⇒

i64

.

load

32

_

u

𝑚

|

0x36

𝑚

:

memarg

⇒

i32

.

store

𝑚

|

0x37

𝑚

:

memarg

⇒

i64

.

store

𝑚

|

0x38

𝑚

:

memarg

⇒

f32

.

store

𝑚

|

0x39

𝑚

:

memarg

⇒

f64

.

store

𝑚

|

0x3A

𝑚

:

memarg

⇒

i32

.

store

8

𝑚

|

0x3B

𝑚

:

memarg

⇒

i32

.

store

16

𝑚

|

0x3C

𝑚

:

memarg

⇒

i64

.

store

8

𝑚

|

0x3D

𝑚

:

memarg

⇒

i64

.

store

16

𝑚

|

0x3E

𝑚

:

memarg

⇒

i64

.

store

32

𝑚

|

0x3F 0x00

⇒

memory

.

size

|

0x40 0x00

⇒

memory

.

grow

|

0xFC

8:

u32

𝑥

:

dataidx

0x00

⇒

memory

.

init

𝑥

|

0xFC

9:

u32

𝑥

:

dataidx

⇒

data

.

drop

𝑥

|

0xFC

10:

u32

0x00 0x00

⇒

memory

.

copy

|

0xFC

11:

u32

0x00

⇒

memory

.

fill

Note:

In future versions of WebAssembly, the additional zero bytes occurring in the encoding of the

memory

.

size

,

memory

.

grow

,

memory

.

copy

, and

memory

.

fill

instructions may be used to index additional memories.

5.4.7 Numeric Instructions

All variants of

numeric instructions

are represented by separate byte codes.

The

const

instructions are followed by the respective literal.

instr

::=

. . .

|

0x41

𝑛

:

i32

⇒

i32

.

const

𝑛

|

0x42

𝑛

:

i64

⇒

i64

.

const

𝑛

|

0x43

𝑧

:

f32

⇒

f32

.

const

𝑧

|

0x44

𝑧

:

f64

⇒

f64

.

const

𝑧

All other numeric instructions are plain opcodes without any immediates.

138

Chapter 5. Binary Format

instr

::=

. . .

|

0x45

⇒

i32

.

eqz

|

0x46

⇒

i32

.

eq

|

0x47

⇒

i32

.

ne

|

0x48

⇒

i32

.

lt

_

s

|

0x49

⇒

i32

.

lt

_

u

|

0x4A

⇒

i32

.

gt

_

s

|

0x4B

⇒

i32

.

gt

_

u

|

0x4C

⇒

i32

.

le

_

s

|

0x4D

⇒

i32

.

le

_

u

|

0x4E

⇒

i32

.

ge

_

s

|

0x4F

⇒

i32

.

ge

_

u

|

0x50

⇒

i64

.

eqz

|

0x51

⇒

i64

.

eq

|

0x52

⇒

i64

.

ne

|

0x53

⇒

i64

.

lt

_

s

|

0x54

⇒

i64

.

lt

_

u

|

0x55

⇒

i64

.

gt

_

s

|

0x56

⇒

i64

.

gt

_

u

|

0x57

⇒

i64

.

le

_

s

|

0x58

⇒

i64

.

le

_

u

|

0x59

⇒

i64

.

ge

_

s

|

0x5A

⇒

i64

.

ge

_

u

|

0x5B

⇒

f32

.

eq

|

0x5C

⇒

f32

.

ne

|

0x5D

⇒

f32

.

lt

|

0x5E

⇒

f32

.

gt

|

0x5F

⇒

f32

.

le

|

0x60

⇒

f32

.

ge

|

0x61

⇒

f64

.

eq

|

0x62

⇒

f64

.

ne

|

0x63

⇒

f64

.

lt

|

0x64

⇒

f64

.

gt

|

0x65

⇒

f64

.

le

|

0x66

⇒

f64

.

ge

|

0x67

⇒

i32

.

clz

|

0x68

⇒

i32

.

ctz

|

0x69

⇒

i32

.

popcnt

|

0x6A

⇒

i32

.

add

|

0x6B

⇒

i32

.

sub

|

0x6C

⇒

i32

.

mul

|

0x6D

⇒

i32

.

div

_

s

|

0x6E

⇒

i32

.

div

_

u

|

0x6F

⇒

i32

.

rem

_

s

|

0x70

⇒

i32

.

rem

_

u

|

0x71

⇒

i32

.

and

|

0x72

⇒

i32

.

or

|

0x73

⇒

i32

.

xor

|

0x74

⇒

i32

.

shl

|

0x75

⇒

i32

.

shr

_

s

|

0x76

⇒

i32

.

shr

_

u

|

0x77

⇒

i32

.

rotl

|

0x78

⇒

i32

.

rotr

5.4. Instructions

139

|

0x79

⇒

i64

.

clz

|

0x7A

⇒

i64

.

ctz

|

0x7B

⇒

i64

.

popcnt

|

0x7C

⇒

i64

.

add

|

0x7D

⇒

i64

.

sub

|

0x7E

⇒

i64

.

mul

|

0x7F

⇒

i64

.

div

_

s

|

0x80

⇒

i64

.

div

_

u

|

0x81

⇒

i64

.

rem

_

s

|

0x82

⇒

i64

.

rem

_

u

|

0x83

⇒

i64

.

and

|

0x84

⇒

i64

.

or

|

0x85

⇒

i64

.

xor

|

0x86

⇒

i64

.

shl

|

0x87

⇒

i64

.

shr

_

s

|

0x88

⇒

i64

.

shr

_

u

|

0x89

⇒

i64

.

rotl

|

0x8A

⇒

i64

.

rotr

|

0x8B

⇒

f32

.

abs

|

0x8C

⇒

f32

.

neg

|

0x8D

⇒

f32

.

ceil

|

0x8E

⇒

f32

.

floor

|

0x8F

⇒

f32

.

trunc

|

0x90

⇒

f32

.

nearest

|

0x91

⇒

f32

.

sqrt

|

0x92

⇒

f32

.

add

|

0x93

⇒

f32

.

sub

|

0x94

⇒

f32

.

mul

|

0x95

⇒

f32

.

div

|

0x96

⇒

f32

.

min

|

0x97

⇒

f32

.

max

|

0x98

⇒

f32

.

copysign

|

0x99

⇒

f64

.

abs

|

0x9A

⇒

f64

.

neg

|

0x9B

⇒

f64

.

ceil

|

0x9C

⇒

f64

.

floor

|

0x9D

⇒

f64

.

trunc

|

0x9E

⇒

f64

.

nearest

|

0x9F

⇒

f64

.

sqrt

|

0xA0

⇒

f64

.

add

|

0xA1

⇒

f64

.

sub

|

0xA2

⇒

f64

.

mul

|

0xA3

⇒

f64

.

div

|

0xA4

⇒

f64

.

min

|

0xA5

⇒

f64

.

max

|

0xA6

⇒

f64

.

copysign

140

Chapter 5. Binary Format

|

0xA7

⇒

i32

.

wrap

_

i64

|

0xA8

⇒

i32

.

trunc

_

f32

_

s

|

0xA9

⇒

i32

.

trunc

_

f32

_

u

|

0xAA

⇒

i32

.

trunc

_

f64

_

s

|

0xAB

⇒

i32

.

trunc

_

f64

_

u

|

0xAC

⇒

i64

.

extend

_

i32

_

s

|

0xAD

⇒

i64

.

extend

_

i32

_

u

|

0xAE

⇒

i64

.

trunc

_

f32

_

s

|

0xAF

⇒

i64

.

trunc

_

f32

_

u

|

0xB0

⇒

i64

.

trunc

_

f64

_

s

|

0xB1

⇒

i64

.

trunc

_

f64

_

u

|

0xB2

⇒

f32

.

convert

_

i32

_

s

|

0xB3

⇒

f32

.

convert

_

i32

_

u

|

0xB4

⇒

f32

.

convert

_

i64

_

s

|

0xB5

⇒

f32

.

convert

_

i64

_

u

|

0xB6

⇒

f32

.

demote

_

f64

|

0xB7

⇒

f64

.

convert

_

i32

_

s

|

0xB8

⇒

f64

.

convert

_

i32

_

u

|

0xB9

⇒

f64

.

convert

_

i64

_

s

|

0xBA

⇒

f64

.

convert

_

i64

_

u

|

0xBB

⇒

f64

.

promote

_

f32

|

0xBC

⇒

i32

.

reinterpret

_

f32

|

0xBD

⇒

i64

.

reinterpret

_

f64

|

0xBE

⇒

f32

.

reinterpret

_

i32

|

0xBF

⇒

f64

.

reinterpret

_

i64

|

0xC0

⇒

i32

.

extend

8

_

s

|

0xC1

⇒

i32

.

extend

16

_

s

|

0xC2

⇒

i64

.

extend

8

_

s

|

0xC3

⇒

i64

.

extend

16

_

s

|

0xC4

⇒

i64

.

extend

32

_

s

The saturating truncation instructions all have a one byte prefix, whereas the actual opcode is encoded by a variable-

length

unsigned integer

.

instr

::=

. . .

|

0xFC

0:

u32

⇒

i32

.

trunc

_

sat

_

f32

_

s

|

0xFC

1:

u32

⇒

i32

.

trunc

_

sat

_

f32

_

u

|

0xFC

2:

u32

⇒

i32

.

trunc

_

sat

_

f64

_

s

|

0xFC

3:

u32

⇒

i32

.

trunc

_

sat

_

f64

_

u

|

0xFC

4:

u32

⇒

i64

.

trunc

_

sat

_

f32

_

s

|

0xFC

5:

u32

⇒

i64

.

trunc

_

sat

_

f32

_

u

|

0xFC

6:

u32

⇒

i64

.

trunc

_

sat

_

f64

_

s

|

0xFC

7:

u32

⇒

i64

.

trunc

_

sat

_

f64

_

u

5.4.8 Vector Instructions

All variants of

vector instructions

are represented by separate byte codes. They all have a one byte prefix, whereas

the actual opcode is encoded by a variable-length

unsigned integer

.

Vector loads and stores are followed by the encoding of their

memarg

immediate.

5.4. Instructions

141

laneidx

::=

𝑙

:

byte

⇒

𝑙

instr

::=

. . .

|

0xFD

0:

u32

𝑚

:

memarg

⇒

v128

.

load

𝑚

|

0xFD

1:

u32

𝑚

:

memarg

⇒

v128

.

load

8x8

_

s

𝑚

|

0xFD

2:

u32

𝑚

:

memarg

⇒

v128

.

load

8x8

_

u

𝑚

|

0xFD

3:

u32

𝑚

:

memarg

⇒

v128

.

load

16x4

_

s

𝑚

|

0xFD

4:

u32

𝑚

:

memarg

⇒

v128

.

load

16x4

_

u

𝑚

|

0xFD

5:

u32

𝑚

:

memarg

⇒

v128

.

load

32x2

_

s

𝑚

|

0xFD

6:

u32

𝑚

:

memarg

⇒

v128

.

load

32x2

_

u

𝑚

|

0xFD

7:

u32

𝑚

:

memarg

⇒

v128

.

load

8

_

splat

𝑚

|

0xFD

8:

u32

𝑚

:

memarg

⇒

v128

.

load

16

_

splat

𝑚

|

0xFD

9:

u32

𝑚

:

memarg

⇒

v128

.

load

32

_

splat

𝑚

|

0xFD

10:

u32

𝑚

:

memarg

⇒

v128

.

load

64

_

splat

𝑚

|

0xFD

92:

u32

𝑚

:

memarg

⇒

v128

.

load

32

_

zero

𝑚

|

0xFD

93:

u32

𝑚

:

memarg

⇒

v128

.

load

64

_

zero

𝑚

|

0xFD

11:

u32

𝑚

:

memarg

⇒

v128

.

store

𝑚

|

0xFD

84:

u32

𝑚

:

memarg

𝑙

:

laneidx

⇒

v128

.

load

8

_

lane

𝑚 𝑙

|

0xFD

85:

u32

𝑚

:

memarg

𝑙

:

laneidx

⇒

v128

.

load

16

_

lane

𝑚 𝑙

|

0xFD

86:

u32

𝑚

:

memarg

𝑙

:

laneidx

⇒

v128

.

load

32

_

lane

𝑚 𝑙

|

0xFD

87:

u32

𝑚

:

memarg

𝑙

:

laneidx

⇒

v128

.

load

64

_

lane

𝑚 𝑙

|

0xFD

88:

u32

𝑚

:

memarg

𝑙

:

laneidx

⇒

v128

.

store

8

_

lane

𝑚 𝑙

|

0xFD

89:

u32

𝑚

:

memarg

𝑙

:

laneidx

⇒

v128

.

store

16

_

lane

𝑚 𝑙

|

0xFD

90:

u32

𝑚

:

memarg

𝑙

:

laneidx

⇒

v128

.

store

32

_

lane

𝑚 𝑙

|

0xFD

91:

u32

𝑚

:

memarg

𝑙

:

laneidx

⇒

v128

.

store

64

_

lane

𝑚 𝑙

The

const

instruction is followed by 16 immediate bytes, which are converted into a

i128

in

littleendian

byte

order:

instr

::=

. . .

|

0xFD

12:

u32

(

𝑏

:

byte

)

16

⇒

v128

.

const

𝑏𝑦𝑡𝑒𝑠

−

1

i128

(

𝑏

0

. . . 𝑏

15

)

The

shuffle

instruction is also followed by the encoding of 16

laneidx

immediates.

instr

::=

. . .

|

0xFD

13:

u32

(

𝑙

:

laneidx

)

16

⇒

i8x16

.

shuffle

𝑙

16

extract

_

lane

and

replace

_

lane

instructions are followed by the encoding of a

laneidx

immediate.

instr

::=

. . .

|

0xFD

21:

u32

𝑙

:

laneidx

⇒

i8x16

.

extract

_

lane

_

s

𝑙

|

0xFD

22:

u32

𝑙

:

laneidx

⇒

i8x16

.

extract

_

lane

_

u

𝑙

|

0xFD

23:

u32

𝑙

:

laneidx

⇒

i8x16

.

replace

_

lane

𝑙

|

0xFD

24:

u32

𝑙

:

laneidx

⇒

i16x8

.

extract

_

lane

_

s

𝑙

|

0xFD

25:

u32

𝑙

:

laneidx

⇒

i16x8

.

extract

_

lane

_

u

𝑙

|

0xFD

26:

u32

𝑙

:

laneidx

⇒

i16x8

.

replace

_

lane

𝑙

|

0xFD

27:

u32

𝑙

:

laneidx

⇒

i32x4

.

extract

_

lane

𝑙

|

0xFD

28:

u32

𝑙

:

laneidx

⇒

i32x4

.

replace

_

lane

𝑙

|

0xFD

29:

u32

𝑙

:

laneidx

⇒

i64x2

.

extract

_

lane

𝑙

|

0xFD

30:

u32

𝑙

:

laneidx

⇒

i64x2

.

replace

_

lane

𝑙

|

0xFD

31:

u32

𝑙

:

laneidx

⇒

f32x4

.

extract

_

lane

𝑙

|

0xFD

32:

u32

𝑙

:

laneidx

⇒

f32x4

.

replace

_

lane

𝑙

|

0xFD

33:

u32

𝑙

:

laneidx

⇒

f64x2

.

extract

_

lane

𝑙

|

0xFD

34:

u32

𝑙

:

laneidx

⇒

f64x2

.

replace

_

lane

𝑙

142

Chapter 5. Binary Format

All other vector instructions are plain opcodes without any immediates.

instr

::=

. . .

|

0xFD

14:

u32

⇒

i8x16

.

swizzle

|

0xFD

15:

u32

⇒

i8x16

.

splat

|

0xFD

16:

u32

⇒

i16x8

.

splat

|

0xFD

17:

u32

⇒

i32x4

.

splat

|

0xFD

18:

u32

⇒

i64x2

.

splat

|

0xFD

19:

u32

⇒

f32x4

.

splat

|

0xFD

20:

u32

⇒

f64x2

.

splat

|

0xFD

35:

u32

⇒

i8x16

.

eq

|

0xFD

36:

u32

⇒

i8x16

.

ne

|

0xFD

37:

u32

⇒

i8x16

.

lt

_

s

|

0xFD

38:

u32

⇒

i8x16

.

lt

_

u

|

0xFD

39:

u32

⇒

i8x16

.

gt

_

s

|

0xFD

40:

u32

⇒

i8x16

.

gt

_

u

|

0xFD

41:

u32

⇒

i8x16

.

le

_

s

|

0xFD

42:

u32

⇒

i8x16

.

le

_

u

|

0xFD

43:

u32

⇒

i8x16

.

ge

_

s

|

0xFD

44:

u32

⇒

i8x16

.

ge

_

u

|

0xFD

45:

u32

⇒

i16x8

.

eq

|

0xFD

46:

u32

⇒

i16x8

.

ne

|

0xFD

47:

u32

⇒

i16x8

.

lt

_

s

|

0xFD

48:

u32

⇒

i16x8

.

lt

_

u

|

0xFD

49:

u32

⇒

i16x8

.

gt

_

s

|

0xFD

50:

u32

⇒

i16x8

.

gt

_

u

|

0xFD

51:

u32

⇒

i16x8

.

le

_

s

|

0xFD

52:

u32

⇒

i16x8

.

le

_

u

|

0xFD

53:

u32

⇒

i16x8

.

ge

_

s

|

0xFD

54:

u32

⇒

i16x8

.

ge

_

u

|

0xFD

55:

u32

⇒

i32x4

.

eq

|

0xFD

56:

u32

⇒

i32x4

.

ne

|

0xFD

57:

u32

⇒

i32x4

.

lt

_

s

|

0xFD

58:

u32

⇒

i32x4

.

lt

_

u

|

0xFD

59:

u32

⇒

i32x4

.

gt

_

s

|

0xFD

60:

u32

⇒

i32x4

.

gt

_

u

|

0xFD

61:

u32

⇒

i32x4

.

le

_

s

|

0xFD

62:

u32

⇒

i32x4

.

le

_

u

|

0xFD

63:

u32

⇒

i32x4

.

ge

_

s

|

0xFD

64:

u32

⇒

i32x4

.

ge

_

u

|

0xFD

214:

u32

⇒

i64x2

.

eq

|

0xFD

215:

u32

⇒

i64x2

.

ne

|

0xFD

216:

u32

⇒

i64x2

.

lt

_

s

|

0xFD

217:

u32

⇒

i64x2

.

gt

_

s

|

0xFD

218:

u32

⇒

i64x2

.

le

_

s

|

0xFD

219:

u32

⇒

i64x2

.

ge

_

s

|

|

0xFD

65:

u32

⇒

f32x4

.

eq

|

0xFD

66:

u32

⇒

f32x4

.

ne

|

0xFD

67:

u32

⇒

f32x4

.

lt

|

0xFD

68:

u32

⇒

f32x4

.

gt

|

0xFD

69:

u32

⇒

f32x4

.

le

|

0xFD

70:

u32

⇒

f32x4

.

ge

5.4. Instructions

143

|

0xFD

71:

u32

⇒

f64x2

.

eq

|

0xFD

72:

u32

⇒

f64x2

.

ne

|

0xFD

73:

u32

⇒

f64x2

.

lt

|

0xFD

74:

u32

⇒

f64x2

.

gt

|

0xFD

75:

u32

⇒

f64x2

.

le

|

0xFD

76:

u32

⇒

f64x2

.

ge

|

0xFD

77:

u32

⇒

v128

.

not

|

0xFD

78:

u32

⇒

v128

.

and

|

0xFD

79:

u32

⇒

v128

.

andnot

|

0xFD

80:

u32

⇒

v128

.

or

|

0xFD

81:

u32

⇒

v128

.

xor

|

0xFD

82:

u32

⇒

v128

.

bitselect

|

0xFD

83:

u32

⇒

v128

.

any

_

true

|

0xFD

96:

u32

⇒

i8x16

.

abs

|

0xFD

97:

u32

⇒

i8x16

.

neg

|

0xFD

98:

u32

⇒

i8x16

.

popcnt

|

0xFD

99:

u32

⇒

i8x16

.

all

_

true

|

0xFD

100:

u32

⇒

i8x16

.

bitmask

|

0xFD

101:

u32

⇒

i8x16

.

narrow

_

i16x8

_

s

|

0xFD

102:

u32

⇒

i8x16

.

narrow

_

i16x8

_

u

|

0xFD

107:

u32

⇒

i8x16

.

shl

|

0xFD

108:

u32

⇒

i8x16

.

shr

_

s

|

0xFD

109:

u32

⇒

i8x16

.

shr

_

u

|

0xFD

110:

u32

⇒

i8x16

.

add

|

0xFD

111:

u32

⇒

i8x16

.

add

_

sat

_

s

|

0xFD

112:

u32

⇒

i8x16

.

add

_

sat

_

u

|

0xFD

113:

u32

⇒

i8x16

.

sub

|

0xFD

114:

u32

⇒

i8x16

.

sub

_

sat

_

s

|

0xFD

115:

u32

⇒

i8x16

.

sub

_

sat

_

u

|

0xFD

118:

u32

⇒

i8x16

.

min

_

s

|

0xFD

119:

u32

⇒

i8x16

.

min

_

u

|

0xFD

120:

u32

⇒

i8x16

.

max

_

s

|

0xFD

121:

u32

⇒

i8x16

.

max

_

u

|

0xFD

123:

u32

⇒

i8x16

.

avgr

_

u

144

Chapter 5. Binary Format

|

0xFD

124:

u32

⇒

i16x8

.

extadd

_

pairwise

_

i8x16

_

s

|

0xFD

125:

u32

⇒

i16x8

.

extadd

_

pairwise

_

i8x16

_

u

|

0xFD

128:

u32

⇒

i16x8

.

abs

|

0xFD

129:

u32

⇒

i16x8

.

neg

|

0xFD

130:

u32

⇒

i16x8

.

q15mulr

_

sat

_

s

|

0xFD

131:

u32

⇒

i16x8

.

all

_

true

|

0xFD

132:

u32

⇒

i16x8

.

bitmask

|

0xFD

133:

u32

⇒

i16x8

.

narrow

_

i32x4

_

s

|

0xFD

134:

u32

⇒

i16x8

.

narrow

_

i32x4

_

u

|

0xFD

135:

u32

⇒

i16x8

.

extend

_

low

_

i8x16

_

s

|

0xFD

136:

u32

⇒

i16x8

.

extend

_

high

_

i8x16

_

s

|

0xFD

137:

u32

⇒

i16x8

.

extend

_

low

_

i8x16

_

u

|

0xFD

138:

u32

⇒

i16x8

.

extend

_

high

_

i8x16

_

u

|

0xFD

139:

u32

⇒

i16x8

.

shl

|

0xFD

140:

u32

⇒

i16x8

.

shr

_

s

|

0xFD

141:

u32

⇒

i16x8

.

shr

_

u

|

0xFD

142:

u32

⇒

i16x8

.

add

|

0xFD

143:

u32

⇒

i16x8

.

add

_

sat

_

s

|

0xFD

144:

u32

⇒

i16x8

.

add

_

sat

_

u

|

0xFD

145:

u32

⇒

i16x8

.

sub

|

0xFD

146:

u32

⇒

i16x8

.

sub

_

sat

_

s

|

0xFD

147:

u32

⇒

i16x8

.

sub

_

sat

_

u

|

0xFD

149:

u32

⇒

i16x8

.

mul

|

0xFD

150:

u32

⇒

i16x8

.

min

_

s

|

0xFD

151:

u32

⇒

i16x8

.

min

_

u

|

0xFD

152:

u32

⇒

i16x8

.

max

_

s

|

0xFD

153:

u32

⇒

i16x8

.

max

_

u

|

0xFD

155:

u32

⇒

i16x8

.

avgr

_

u

|

0xFD

156:

u32

⇒

i16x8

.

extmul

_

low

_

i8x16

_

s

|

0xFD

157:

u32

⇒

i16x8

.

extmul

_

high

_

i8x16

_

s

|

0xFD

158:

u32

⇒

i16x8

.

extmul

_

low

_

i8x16

_

u

|

0xFD

159:

u32

⇒

i16x8

.

extmul

_

high

_

i8x16

_

u

|

0xFD

126:

u32

⇒

i32x4

.

extadd

_

pairwise

_

i16x8

_

s

|

0xFD

127:

u32

⇒

i32x4

.

extadd

_

pairwise

_

i16x8

_

u

|

0xFD

160:

u32

⇒

i32x4

.

abs

|

0xFD

161:

u32

⇒

i32x4

.

neg

|

0xFD

163:

u32

⇒

i32x4

.

all

_

true

|

0xFD

164:

u32

⇒

i32x4

.

bitmask

|

0xFD

167:

u32

⇒

i32x4

.

extend

_

low

_

i16x8

_

s

|

0xFD

168:

u32

⇒

i32x4

.

extend

_

high

_

i16x8

_

s

|

0xFD

169:

u32

⇒

i32x4

.

extend

_

low

_

i16x8

_

u

|

0xFD

170:

u32

⇒

i32x4

.

extend

_

high

_

i16x8

_

u

|

0xFD

171:

u32

⇒

i32x4

.

shl

|

0xFD

172:

u32

⇒

i32x4

.

shr

_

s

|

0xFD

173:

u32

⇒

i32x4

.

shr

_

u

|

0xFD

174:

u32

⇒

i32x4

.

add

|

0xFD

177:

u32

⇒

i32x4

.

sub

|

0xFD

181:

u32

⇒

i32x4

.

mul

|

0xFD

182:

u32

⇒

i32x4

.

min

_

s

|

0xFD

183:

u32

⇒

i32x4

.

min

_

u

|

0xFD

184:

u32

⇒

i32x4

.

max

_

s

|

0xFD

185:

u32

⇒

i32x4

.

max

_

u

|

0xFD

186:

u32

⇒

i32x4

.

dot

_

i16x8

_

s

|

0xFD

188:

u32

⇒

i32x4

.

extmul

_

low

_

i16x8

_

s

|

0xFD

189:

u32

⇒

i32x4

.

extmul

_

high

_

i16x8

_

s

|

0xFD

190:

u32

⇒

i32x4

.

extmul

_

low

_

i16x8

_

u

|

0xFD

191:

u32

⇒

i32x4

.

extmul

_

high

_

i16x8

_

u

5.4. Instructions

145

|

0xFD

192:

u32

⇒

i64x2

.

abs

|

0xFD

193:

u32

⇒

i64x2

.

neg

|

0xFD

195:

u32

⇒

i64x2

.

all

_

true

|

0xFD

196:

u32

⇒

i64x2

.

bitmask

|

0xFD

199:

u32

⇒

i64x2

.

extend

_

low

_

i32x4

_

s

|

0xFD

200:

u32

⇒

i64x2

.

extend

_

high

_

i32x4

_

s

|

0xFD

201:

u32

⇒

i64x2

.

extend

_

low

_

i32x4

_

u

|

0xFD

202:

u32

⇒

i64x2

.

extend

_

high

_

i32x4

_

u

|

0xFD

203:

u32

⇒

i64x2

.

shl

|

0xFD

204:

u32

⇒

i64x2

.

shr

_

s

|

0xFD

205:

u32

⇒

i64x2

.

shr

_

u

|

0xFD

206:

u32

⇒

i64x2

.

add

|

0xFD

209:

u32

⇒

i64x2

.

sub

|

0xFD

213:

u32

⇒

i64x2

.

mul

|

0xFD

220:

u32

⇒

i64x2

.

extmul

_

low

_

i32x4

_

s

|

0xFD

221:

u32

⇒

i64x2

.

extmul

_

high

_

i32x4

_

s

|

0xFD

222:

u32

⇒

i64x2

.

extmul

_

low

_

i32x4

_

u

|

0xFD

223:

u32

⇒

i64x2

.

extmul

_

high

_

i32x4

_

u

|

0xFD

103:

u32

⇒

f32x4

.

ceil

|

0xFD

104:

u32

⇒

f32x4

.

floor

|

0xFD

105:

u32

⇒

f32x4

.

trunc

|

0xFD

106:

u32

⇒

f32x4

.

nearest

|

0xFD

224:

u32

⇒

f32x4

.

abs

|

0xFD

225:

u32

⇒

f32x4

.

neg

|

0xFD

227:

u32

⇒

f32x4

.

sqrt

|

0xFD

228:

u32

⇒

f32x4

.

add

|

0xFD

229:

u32

⇒

f32x4

.

sub

|

0xFD

230:

u32

⇒

f32x4

.

mul

|

0xFD

231:

u32

⇒

f32x4

.

div

|

0xFD

232:

u32

⇒

f32x4

.

min

|

0xFD

233:

u32

⇒

f32x4

.

max

|

0xFD

234:

u32

⇒

f32x4

.

pmin

|

0xFD

235:

u32

⇒

f32x4

.

pmax

|

0xFD

116:

u32

⇒

f64x2

.

ceil

|

0xFD

117:

u32

⇒

f64x2

.

floor

|

0xFD

122:

u32

⇒

f64x2

.

trunc

|

0xFD

148:

u32

⇒

f64x2

.

nearest

|

0xFD

236:

u32

⇒

f64x2

.

abs

|

0xFD

237:

u32

⇒

f64x2

.

neg

|

0xFD

239:

u32

⇒

f64x2

.

sqrt

|

0xFD

240:

u32

⇒

f64x2

.

add

|

0xFD

241:

u32

⇒

f64x2

.

sub

|

0xFD

242:

u32

⇒

f64x2

.

mul

|

0xFD

243:

u32

⇒

f64x2

.

div

|

0xFD

244:

u32

⇒

f64x2

.

min

|

0xFD

245:

u32

⇒

f64x2

.

max

|

0xFD

246:

u32

⇒

f64x2

.

pmin

|

0xFD

247:

u32

⇒

f64x2

.

pmax

146

Chapter 5. Binary Format

|

0xFD

248:

u32

⇒

i32x4

.

trunc

_

sat

_

f32x4

_

s

|

0xFD

249:

u32

⇒

i32x4

.

trunc

_

sat

_

f32x4

_

u

|

0xFD

250:

u32

⇒

f32x4

.

convert

_

i32x4

_

s

|

0xFD

251:

u32

⇒

f32x4

.

convert

_

i32x4

_

u

|

0xFD

252:

u32

⇒

i32x4

.

trunc

_

sat

_

f64x2

_

s

_

zero

|

0xFD

253:

u32

⇒

i32x4

.

trunc

_

sat

_

f64x2

_

u

_

zero

|

0xFD

254:

u32

⇒

f64x2

.

convert

_

low

_

i32x4

_

s

|

0xFD

255:

u32

⇒

f64x2

.

convert

_

low

_

i32x4

_

u

|

0xFD

94:

u32

⇒

f32x4

.

demote

_

f64x2

_

zero

|

0xFD

95:

u32

⇒

f64x2

.

promote

_

low

_

f32x4

5.4.9 Expressions

Expressions

are encoded by their instruction sequence terminated with an explicit

0x0B

opcode for

end

.

expr

::=

(

in

:

instr

)

*

0x0B

⇒

in

*

end

5.5 Modules

The binary encoding of modules is organized into

sections

. Most sections correspond to one component of a

module

record, except that

function definitions

are split into two sections, separating their type declarations in the

function section

from their bodies in the

code section

.

Note:

This separation enables

parallel

and

streaming

compilation of the functions in a module.

5.5.1 Indices

All

indices

are encoded with their respective value.

typeidx

::=

𝑥

:

u32

⇒

𝑥

funcidx

::=

𝑥

:

u32

⇒

𝑥

tableidx

::=

𝑥

:

u32

⇒

𝑥

memidx

::=

𝑥

:

u32

⇒

𝑥

globalidx

::=

𝑥

:

u32

⇒

𝑥

elemidx

::=

𝑥

:

u32

⇒

𝑥

dataidx

::=

𝑥

:

u32

⇒

𝑥

localidx

::=

𝑥

:

u32

⇒

𝑥

labelidx

::=

𝑙

:

u32

⇒

𝑙

5.5.2 Sections

Each section consists of

• a one-byte section

id

,

• the

u32

size

of the contents, in bytes,

• the actual

contents

, whose structure is depended on the section id.

Every section is optional; an omitted section is equivalent to the section being present with empty contents.

5.5. Modules

147

The following parameterized grammar rule defines the generic structure of a section with id

𝑁

and contents de-

scribed by the grammar

B

.

section

𝑁

(

B

)

::=

𝑁

:

byte

size

:

u32

cont

:

B

⇒

cont

(

if

size

=

||

B

||

)

|

𝜖

⇒

𝜖

For most sections, the contents

B

encodes a

vector

. In these cases, the empty result

𝜖

is interpreted as the empty

vector.

Note:

Other than for unknown

custom sections

, the

size

is not required for decoding, but can be used to skip

sections when navigating through a binary. The module is malformed if the size does not match the length of the

binary contents

B

.

The following section ids are used:

Id

Section

0

custom section

1

type section

2

import section

3

function section

4

table section

5

memory section

6

global section

7

export section

8

start section

9

element section

10

code section

11

data section

12

data count section

5.5.3 Custom Section

Custom sections

have the id 0. They are intended to be used for debugging information or third-party extensions,

and are ignored by the WebAssembly semantics. Their contents consist of a

name

further identifying the custom

section, followed by an uninterpreted sequence of bytes for custom use.

customsec

::=

section

0

(

custom

)

custom

::=

name byte

*

Note:

If an implementation interprets the data of a custom section, then errors in that data, or the placement of

the section, must not invalidate the module.

5.5.4 Type Section

The

type section

has the id 1. It decodes into a vector of

function types

that represent the

types

component of a

module

.

typesec

::=

ft

*

:

section

1

(

vec

(

functype

))

⇒

ft

*

148

Chapter 5. Binary Format

5.5.5 Import Section

The

import section

has the id 2. It decodes into a vector of

imports

that represent the

imports

component of a

module

.

importsec

::=

im

*

:

section

2

(

vec

(

import

))

⇒

im

*

import

::=

mod

:

name

nm

:

name

𝑑

:

importdesc

⇒

{

module

mod

,

name

nm

,

desc

𝑑

}

importdesc

::=

0x00

𝑥

:

typeidx

⇒

func

𝑥

|

0x01

tt

:

tabletype

⇒

table

tt

|

0x02

mt

:

memtype

⇒

mem

mt

|

0x03

gt

:

globaltype

⇒

global

gt

5.5.6 Function Section

The

function section

has the id 3. It decodes into a vector of

type indices

that represent the

type

fields of the

functions

in the

funcs

component of a

module

. The

locals

and

body

fields of the respective functions are encoded

separately in the

code section

.

funcsec

::=

𝑥

*

:

section

3

(

vec

(

typeidx

))

⇒

𝑥

*

5.5.7 Table Section

The

table section

has the id 4. It decodes into a vector of

tables

that represent the

tables

component of a

module

.

tablesec

::=

tab

*

:

section

4

(

vec

(

table

))

⇒

tab

*

table

::=

tt

:

tabletype

⇒

{

type

tt

}

5.5.8 Memory Section

The

memory section

has the id 5. It decodes into a vector of

memories

that represent the

mems

component of a

module

.

memsec

::=

mem

*

:

section

5

(

vec

(

mem

))

⇒

mem

*

mem

::=

mt

:

memtype

⇒

{

type

mt

}

5.5.9 Global Section

The

global section

has the id 6. It decodes into a vector of

globals

that represent the

globals

component of a

module

.

globalsec

::=

glob

*

:

section

6

(

vec

(

global

))

⇒

glob

*

global

::=

gt

:

globaltype

𝑒

:

expr

⇒

{

type

gt

,

init

𝑒

}

5.5. Modules

149

5.5.10 Export Section

The

export section

has the id 7. It decodes into a vector of

exports

that represent the

exports

component of a

module

.

exportsec

::=

ex

*

:

section

7

(

vec

(

export

))

⇒

ex

*

export

::=

nm

:

name

𝑑

:

exportdesc

⇒

{

name

nm

,

desc

𝑑

}

exportdesc

::=

0x00

𝑥

:

funcidx

⇒

func

𝑥

|

0x01

𝑥

:

tableidx

⇒

table

𝑥

|

0x02

𝑥

:

memidx

⇒

mem

𝑥

|

0x03

𝑥

:

globalidx

⇒

global

𝑥

5.5.11 Start Section

The

start section

has the id 8. It decodes into an optional

start function

that represents the

start

component of a

module

.

startsec

::=

st

?

:

section

8

(

start

)

⇒

st

?

start

::=

𝑥

:

funcidx

⇒

{

func

𝑥

}

5.5.12 Element Section

The

element section

has the id 9. It decodes into a vector of

element segments

that represent the

elems

component

of a

module

.

elemsec

::=

seg

*

:

section

9

(

vec

(

elem

))

⇒

seg

elem

::=

0x00

𝑒

:

expr

𝑦

*

:

vec

(

funcidx

)

⇒

{

type funcref

,

init

((

ref

.

func

𝑦

)

end

)

*

,

mode active

{

table

0

,

offset

𝑒

}}

|

0x01

et

:

elemkind

𝑦

*

:

vec

(

funcidx

)

⇒

{

type

et

,

init

((

ref

.

func

𝑦

)

end

)

*

,

mode passive

}

|

0x02

𝑥

:

tableidx

𝑒

:

expr

et

:

elemkind

𝑦

*

:

vec

(

funcidx

)

⇒

{

type

et

,

init

((

ref

.

func

𝑦

)

end

)

*

,

mode active

{

table

𝑥,

offset

𝑒

}}

|

0x03

et

:

elemkind

𝑦

*

:

vec

(

funcidx

)

⇒

{

type

et

,

init

((

ref

.

func

𝑦

)

end

)

*

,

mode declarative

}

|

0x04

𝑒

:

expr

el

*

:

vec

(

expr

)

⇒

{

type funcref

,

init

el

*

,

mode active

{

table

0

,

offset

𝑒

}}

|

0x05

et

:

reftype

el

*

:

vec

(

expr

)

⇒

{

type

𝑒𝑡,

init

el

*

,

mode passive

}

|

0x06

𝑥

:

tableidx

𝑒

:

expr

et

:

reftype

el

*

:

vec

(

expr

)

⇒

{

type

𝑒𝑡,

init

el

*

,

mode active

{

table

𝑥,

offset

𝑒

}}

|

0x07

et

:

reftype

el

*

:

vec

(

expr

)

⇒

{

type

𝑒𝑡,

init

el

*

,

mode declarative

}

elemkind

::=

0x00

⇒

funcref

Note:

The initial byte can be interpreted as a bitfield. Bit 0 indicates a passive or declarative segment, bit 1

indicates the presence of an explicit table index for an active segment and otherwise distinguishes passive from

declarative segments, bit 2 indicates the use of element type and element

expressions

instead of element kind and

element indices.
Additional element kinds may be added in future versions of WebAssembly.

150

Chapter 5. Binary Format

 

 

 

 

 

 

 

Content      ..     9      10      11      12     ..