”;
WebAssembly, also called WASM, is binary format low level code developed to be executed inside browsers in the most efficient way. WebAssembly code is structured with following concepts −
- Values
- Types
- Instructions
Let us learn them in detail now.
Values
Values in WebAssembly are meant to store complex data such as text, strings and vectors. WebAssembly supports the following −
- Bytes
- Integers
- Floating point
- Names
Bytes
Bytes is the simplest form of values supported in WebAssembly. The value is in hexadecimal format.
For example
Bytes represented as b, can also take natural numbers n, where n <256.
byte ::= 0x00| .... |0xFF
Integers
In WebAssembly, integers supported are as given below −
- i32: 32-bit integer
- i64: 64-bit integer
Floating Point
In WebAssembly floating point numbers supported are as follows −
- f32: 32-bit floating point
- f64: 64-bit floating point
Names
Names are sequence of character, with scalar values defined by Unicode, which is available at the link http://www.unicode.org/versions/Unicode12.1.0/ given herewith.
Types
The entities in WebAssembly are classified as types. The types supported are as stated below −
- Value Types
- Result Types
- Function Types
- Limits
- Memory Types
- Table Types
- Global Types
- External Types
Let us study them one by one.
Value Types
The values type supported by WebAssembly are as mentioned below −
- i32: 32-bit integer
- i64: 64-bit integer
- f32: 32-bit floating point
- f64: 64-bit floating point
valtype ::= i32|i64|f32|f64
Result Types
The values written inside brackets are executed and stored inside result types. The result type is the output of the execution of a block of code made up of values.
resulttype::=[valtype?]
Function Types
A function type will take in vector of parameters returns a vector of results.
functype::=[vec(valtype)]--> [vec(valtype)]
Limits
Limits are the storage range linked with memory and table types.
limits ::= {min u32, max u32}
Memory Types
Memory types deal with linear memories and the size range.
memtype ::= limits
Table Types
Table Types are classified by the element type assigned to it.
tabletype ::= limits elemtype elemtype ::= funcref
Table type is dependent on the limit for the minimum and maximum size assigned to it.
Global Types
Global Type holds the global variables that have the value, that can change or remain the same.
globaltype ::= mut valtype mut ::= const|var
External Types
External Types deals with imports and external values.
externtype ::= func functype | table tabletype | mem memtype | global globaltype
Instructions
WebAssembly code is a sequence of instructions that follows a stack machine model. As WebAssembly follows a stack machine model, the instructions are pushed on the stack.
The argument values for a function, for example, are popped from stack and the result is pushed back on the stack. In the end, there will be only one value in the stack and that is the result.
Some of the commonly used Instructions are as follows −
- Numeric Instructions
- Variable Instructions
Numeric Instructions
Numeric Instructions are operations, which are performed on numeric value.
For example
nn, mm ::= 32|64 ibinop ::= add|sub|mul|div_sx|rem_sx|and|or|xor irelop ::= eq | ne | lt_sx | gt_sx | le_sx | ge_sx frelop ::= eq | ne | lt | gt | le | ge
Variable Instructions
Variable instructions are about accessing the local and global variables.
For example
To access local variables −
get_local $a get_local $b
To set local variables −
set_local $a set_local $b
To access global variables −
get_global $a get_global $b
To set global variables −
set_global $a set_global $b
”;