Compiler Intrinsics
The Sway compiler supports a list of intrinsics that perform various low level operations that are useful for building libraries. Compiler intrinsics should rarely be used but are preferred over asm
blocks because they are type-checked and are safer overall. Below is a list of all available compiler intrinsics:
__size_of_val<T>(val: T) -> u64
Description: Return the size of type T
in bytes.
Constraints: None.
__size_of<T>() -> u64
Description: Return the size of type T
in bytes.
Constraints: None.
__is_reference_type<T>() -> bool
Description: Returns true
if T
is a reference type and false
otherwise.
Constraints: None.
__eq<T>(lhs: T, rhs: T) -> bool
Description: Returns whether lhs
and rhs
are equal.
Constraints: T
is bool
, u8
, u16
, u32
, u64
, or raw_ptr
.
__gtf<T>(index: u64, tx_field_id: u64) -> T
Description: Returns transaction field with ID tx_field_id
at index index
, if applicable. This is a wrapper around FuelVM's gtf
instruction. The resuting field is cast to T
.
Constraints: None.
__addr_of<T>(val: T) -> raw_ptr
Description: Returns the address in memory where val
is stored.
Constraints: T
is a reference type.
__state_load_word(key: b256) -> u64
Description: Reads and returns a single word from storage at key key
.
Constraints: None.
__state_load_quad(key: b256, ptr: raw_ptr, slots: u64)
Description: Reads slots
number of slots (b256
each) from storage starting at key key
and stores them in memory starting at address ptr
Constraints: None.
__state_store_word(key: b256, val: u64)
Description: Stores a single word val
into storage at key key
.
Constraints: None.
__state_store_quad(key: b256, ptr: raw_ptr, slots: u64)
Description: Stores slots
number of slots (b256
each) starting at address ptr
in memory into storage starting at key key
.
Constraints: None.
__log<T>(val: T)
Description: Logs value val
.
Constraints: None.
__add<T>(lhs: T, rhs: T) -> T
Description: Adds lhs
and rhs
and returns the result.
Constraints: T
is an integer type, i.e. u8
, u16
, u32
, u64
.
__sub<T>(lhs: T, rhs: T) -> T
Description: Subtracts rhs
from lhs
.
Constraints: T
is an integer type, i.e. u8
, u16
, u32
, u64
.
__mul<T>(lhs: T, rhs: T) -> T
Description: Multiplies lhs
by rhs
.
Constraints: T
is an integer type, i.e. u8
, u16
, u32
, u64
.
__div<T>(lhs: T, rhs: T) -> T
Description: Divides lhs
by rhs
.
Constraints: T
is an integer type, i.e. u8
, u16
, u32
, u64
.
__revert(code: u64)
Description: Reverts with error code code
.
Constraints: None.
__ptr_add(ptr: raw_ptr, offset: u64)
Description: Adds offset
to the raw value of pointer ptr
.
Constraints: None.
__ptr_sub(ptr: raw_ptr, offset: u64)
Description: Subtracts offset
to the raw value of pointer ptr
.
Constraints: None.
__smo<T>(recipient: b256, data: T, output_index: u64, coins: u64)
Description: Sends a message data
of arbitrary type T
and coins
amount of the base asset to address recipient
. This intrinsic assumes that an OutputMessage is available at index output_index
.
Constraints: None.