What are Operators in WebAssembly?
In WebAssembly, operators are instructions executed on values pushed to the stack. Since WebAssembly is a stack-based language, all operations take their operands from the stack and push the results back onto the stack.
Operators are divided into several categories based on their functionality:
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Memory Operators
- Control Flow Operators
1. Arithmetic Operators
Arithmetic operators perform basic mathematical calculations like addition, subtraction, multiplication, and division. They are type-specific (i32, i64, f32, f64).
Common Arithmetic Operators:
Operator | Description | Example in WebAssembly |
---|---|---|
i32.add | Adds two 32-bit integers | 5 + 3 = 8 |
i64.sub | Subtracts two 64-bit integers | 10 – 7 = 3 |
f32.mul | Multiplies two 32-bit floats | 4.5 * 2.0 = 9.0 |
f64.div | Divides two 64-bit floats | 8.0 – 2.0 = 4.0 |
Example:
(module
(func $addNumbers (param $a i32) (param $b i32) (result i32)
local.get $a ;; Push $a onto the stack
local.get $b ;; Push $b onto the stack
i32.add ;; Add the two values
)
(export "addNumbers" (func $addNumbers))
)
2. Comparison Operators
Comparison operators evaluate two values and return a boolean result (0 for false, 1 for true). They are used for equality and relational checks.
Common Comparison Operators:
Operator | Description | Example in WebAssembly |
---|---|---|
i32.EQ | Checks if two values are equal | 5 == 5 -> 1 |
i32.ne | Checks if two values are not equal | 4 != 5 -> 1 |
i32.it_s | Checks if the first is less than the second (signed) | 3 < 5 -> 1 |
i64.gt_u | Checks if the first is greater than the second (unsigned) | 7 > 3 -> 1 |
Example:
(module
(func $compareValues (param $x i32) (param $y i32) (result i32)
local.get $x ;; Push $x onto the stack
local.get $y ;; Push $y onto the stack
i32.eq ;; Compare $x and $y for equality
)
(export "compareValues" (func $compareValues))
)
3. Logical Operators
Logical operators are used for Boolean logic operations like AND, OR and NOT.
Common Logical Operators:
Operator | Description | Example in WebAssembly |
---|---|---|
i32.and | Logical AND | 1 AND 0 -> 0 |
i32.or | Logical OR | 1 OR 0 -> 1 |
i32.xor | Logical XOR (exclusive OR) | 1 XOR 1 -> 0 |
Example:
(module
(func $logicalAnd (param $a i32) (param $b i32) (result i32)
local.get $a ;; Push $a onto the stack
local.get $b ;; Push $b onto the stack
i32.and ;; Perform AND operation
)
(export "logicalAnd" (func $logicalAnd))
)
4. Bitwise Operators
Bitwise operators manipulate individual bits of integer values.
Common Bitwise Operators:
Operator | Description | Example in WebAssembly |
---|---|---|
i32.shl | Left shift | 4 << 1 -> 8 |
i32.shr_u | Right shift (unsigned) | 8 >> 1 -> 4 |
i32.shr_s | Right shift (signed) | -8 >> 1 -> -4 |
Example:
(module
(func $bitwiseShift (param $x i32) (result i32)
local.get $x ;; Push $x onto the stack
i32.const 1 ;; Push the shift count
i32.shl ;; Perform left shift
)
(export "bitwiseShift" (func $bitwiseShift))
)
5. Memory Operators
Memory operators interact with the linear memory of the WebAssembly module.
Common Memory Operators:
Operator | Description | Example |
---|---|---|
i32.load | Loads a 32-bit value from memory | load from memory |
i32.store | Stores a 32-bit value into memory | Store to memory |
Example:
(module
(memory $mem 1) ;; Define a memory block
(func $storeValue
i32.const 0 ;; Push the memory offset
i32.const 42 ;; Push the value to store
i32.store ;; Store the value in memory
)
(func $loadValue (result i32)
i32.const 0 ;; Push the memory offset
i32.load ;; Load the value from memory
)
(export "storeValue" (func $storeValue))
(export "loadValue" (func $loadValue))
)
6. Control Flow Operators
Control flow operators manage the execution flow of the program, such as loops and conditionals.
Common Control Flow Operators:
Operator | Description |
---|---|
block | Defines a block of instructions |
loop | Defines a loop |
if | Executes instructions conditionally |
br | Breaks out of a block or loop |
Example:
(module
(func $conditionalCheck (param $x i32) (result i32)
local.get $x
i32.const 10
i32.lt_s
if (result i32)
i32.const 1 ;; Return 1 if $x < 10
else
i32.const 0 ;; Return 0 otherwise
end
)
(export "conditionalCheck" (func $conditionalCheck))
)