10.1 编译器内部函数(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:

Sway 编译器支持一系列内部函数,这些内部函数执行各种对构建库有用的低级操作。编译器内部函数应该很少使用,但比 asm 块更受欢迎,因为它们经过类型检查并且总体上更安全。下面是所有可用的编译器内在函数的列表:


__size_of_val<T>(val: T) -> u64

Description: Return the size of type T in bytes.

Constraints: None.

说明: 返回类型 T 的大小(以字节为单位)。

约束: 无。


__size_of<T>() -> u64

Description: Return the size of type T in bytes.

Constraints: None.

说明: 返回类型 T 的大小(以字节为单位)。

约束: 无。


__is_reference_type<T>() -> bool

Description: Returns true if T is a reference type and false otherwise.

Constraints: None.

说明: 如果 T 是 引用类型 ,则返回 true,否则返回 false。

约束: 无。


__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.

说明: 返回 lhsrhs 是否相等。

约束: Tboolu8u16u32u64raw_ptr


__gt<T>(lhs: T, rhs: T) -> bool

Description: Returns whether lhs is greater than rhs.

Constraints: T is u8, u16, u32, u64.

说明: 返回 lhs 是否大于 rhs

约束: Tu8u16u32u64


__lt<T>(lhs: T, rhs: T) -> bool

Description: Returns whether lhs is less than rhs.

Constraints: T is u8, u16, u32, u64.

说明: 返回 lhs 是否小于 rhs

约束: Tu8u16u32u64


__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.

说明: 返回索引为 index 的 ID 为 tx_field_id 的交易字段(如果适用)。这是 FuelVM 的 gtf 指令 的封装器。结果字段被转换为T

约束: 无。


__addr_of<T>(val: T) -> raw_ptr

Description: Returns the address in memory where val is stored.

Constraints: T is a reference type.

说明: 返回内存中存储 val 的地址。

约束: T 是引用类型。


__state_load_word(key: b256) -> u64

Description: Reads and returns a single word from storage at key key.

Constraints: None.

描述: 从存储中的键 key 读取并返回单个单词。

约束: 无。


__state_load_quad(key: b256, ptr: raw_ptr, slots: u64) -> bool

Description: Reads slots number of slots (b256 each) from storage starting at key key and stores them in memory starting at address ptr. Returns a Boolean describing whether all the storage slots were previously set.

Constraints: None.

描述: 从键 key 开始的存储中读取 slots 槽数(每个 b256),并将它们存储在从地址 ptr 开始的内存中。之后返回一个布尔值,描述是否所有存储槽都已预先设置。

约束: 无。


__state_store_word(key: b256, val: u64) -> bool

Description: Stores a single word val into storage at key key. Returns a Boolean describing whether the store slot was previously set.

Constraints: None.

描述: 将单个单词 val 存储到键 key 处的存储中。返回一个布尔值,描述是否先前设置了存储槽。

约束: 无。


__state_store_quad(key: b256, ptr: raw_ptr, slots: u64) -> bool

Description: Stores slots number of slots (b256 each) starting at address ptr in memory into storage starting at key key. Returns a Boolean describing whether the first storage slot was previously set.

Constraints: None.

描述: 将内存中从地址 ptr 开始的插槽数(每个 b256)存储到以键 key 开始的存储中。返回一个布尔值,描述第一个存储槽是否已设置。

约束: 无。


__log<T>(val: T)

Description: Logs value val.

Constraints: None.

说明: 记录值val

约束: 无。


__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.

说明: 添加 lhsrhs 并返回结果。

约束:T 是整数类型,即u8u16u32u64


__sub<T>(lhs: T, rhs: T) -> T

Description: Subtracts rhs from lhs.

Constraints: T is an integer type, i.e. u8, u16, u32, u64.

说明:lhs 中减去 rhs

约束:T 是整数类型,即u8u16u32u64


__mul<T>(lhs: T, rhs: T) -> T

Description: Multiplies lhs by rhs.

Constraints: T is an integer type, i.e. u8, u16, u32, u64.

说明:lhs 乘以 rhs

约束:T 是整数类型,即u8u16u32u64


__div<T>(lhs: T, rhs: T) -> T

Description: Divides lhs by rhs.

Constraints: T is an integer type, i.e. u8, u16, u32, u64.

说明:lhs 除以 rhs

约束:T 是整数类型,即u8u16u32u64


__and<T>(lhs: T, rhs: T) -> T

Description: Bitwise AND lhs and rhs.

Constraints: T is an integer type, i.e. u8, u16, u32, u64.

说明: 按位与 lhsrhs

约束:T 是整数类型,即u8u16u32u64


or<T>(lhs: T, rhs: T) -> T

Description: Bitwise OR lhs and rhs.

Constraints: T is an integer type, i.e. u8, u16, u32, u64.

说明: 按位或 lhsrhs

约束:T 是整数类型,即u8u16u32u64


__xor<T>(lhs: T, rhs: T) -> T

Description: Bitwise XOR lhs and rhs.

Constraints: T is an integer type, i.e. u8, u16, u32, u64.

说明: 按位异或 lhsrhs

约束:T 是整数类型,即u8u16u32u64


__revert(code: u64)

Description: Reverts with error code code.

Constraints: None.

说明: 返回错误代码为 code

约束: 无。


__ptr_add(ptr: raw_ptr, offset: u64)

Description: Adds offset to the raw value of pointer ptr.

Constraints: None.

说明:offset 添加到指针 ptr 的原始值。

约束: 无。


__ptr_sub(ptr: raw_ptr, offset: u64)

Description: Subtracts offset to the raw value of pointer ptr.

Constraints: None.

说明:offset 减去指针 ptr 的原始值。

约束: 无。


__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.

描述: 发送任意类型的消息data和基础资产的coins数量到地址recipient。此内在假设 OutputMessage 在索引 output_index 处可用。

约束: 无。

Last updated