Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slice/Array intrinsics: __slice and __elem_at #6282

Merged
merged 31 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9d872af
__slice to create slices from arrays
xunilrj Jul 10, 2024
4138fd9
__slice intrinsics create a typed slice
xunilrj Jul 11, 2024
b8a4cef
__slice_elem intrinsics returning elem as value
xunilrj Jul 11, 2024
bc1b10e
slice of slices
xunilrj Jul 12, 2024
36b361e
better error messages for __slice indices
xunilrj Jul 12, 2024
1793f38
fix control flow for empty arrays
xunilrj Jul 16, 2024
9273749
fmt and clippy issues
xunilrj Jul 16, 2024
2895bab
vec build on top on slice
xunilrj Jul 19, 2024
919c922
better dbg
xunilrj Jul 19, 2024
a448d14
fix commit
xunilrj Jul 22, 2024
5a84e32
fmt sway cor and stdlib
xunilrj Jul 22, 2024
dde3e19
__slice and __slice_elem for const
xunilrj Jul 22, 2024
a0fbb0c
fmt and clippy issues
xunilrj Jul 22, 2024
d8ef997
better error messages for slice in constants
xunilrj Jul 23, 2024
ea88ad9
removing todos
xunilrj Jul 24, 2024
fc828ac
removing dbg
xunilrj Jul 24, 2024
d8788bd
removing a.txt
xunilrj Jul 24, 2024
06a612b
__elem_at working for slices and arrays
xunilrj Jul 25, 2024
771411b
clippy and fmt issues
xunilrj Jul 25, 2024
eb05ef6
fix register usage
xunilrj Jul 25, 2024
06a29fe
fmt and clippy issues
xunilrj Jul 25, 2024
2abb613
testing wrong index types
xunilrj Jul 26, 2024
0cf2752
fix comment
xunilrj Jul 27, 2024
1b20c39
remove abiencode impl for slices, because it cannot be tested
xunilrj Jul 27, 2024
ef231ac
remove impl for slices, because it cannot be tested
xunilrj Jul 27, 2024
52378fc
improve documentation of slices
xunilrj Jul 31, 2024
cdcb863
improve documentation of slices
xunilrj Jul 31, 2024
bd38195
more sound impl for mutability on __slice and __elem_at
xunilrj Jul 31, 2024
11c96d3
removing uncecessary log
xunilrj Aug 4, 2024
4a9384b
Merge branch 'master' into xunilrj/slice-intrinsics
xunilrj Aug 5, 2024
967275a
Merge branch 'master' into xunilrj/slice-intrinsics
IGI-111 Aug 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 80 additions & 33 deletions docs/book/src/reference/compiler_intrinsics.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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
__size_of_val<T>(val: T) -> u64
Expand All @@ -12,7 +12,7 @@ __size_of_val<T>(val: T) -> u64

**Constraints:** None.

___
---

```sway
__size_of<T>() -> u64
Expand All @@ -22,7 +22,7 @@ __size_of<T>() -> u64

**Constraints:** None.

___
---

```sway
__size_of_str_array<T>() -> u64
Expand All @@ -32,7 +32,7 @@ __size_of_str_array<T>() -> u64

**Constraints:** None.

___
---

```sway
__assert_is_str_array<T>()
Expand All @@ -42,7 +42,7 @@ __assert_is_str_array<T>()

**Constraints:** None.

___
---

```sway
__to_str_array(s: str) -> str[N]
Expand All @@ -52,7 +52,7 @@ __to_str_array(s: str) -> str[N]

**Constraints:** None.

___
---

```sway
__is_reference_type<T>() -> bool
Expand All @@ -62,7 +62,7 @@ __is_reference_type<T>() -> bool

**Constraints:** None.

___
---

```sway
__is_str_array<T>() -> bool
Expand All @@ -72,7 +72,7 @@ __is_str_array<T>() -> bool

**Constraints:** None.

___
---

```sway
__eq<T>(lhs: T, rhs: T) -> bool
Expand All @@ -82,7 +82,7 @@ __eq<T>(lhs: T, rhs: T) -> bool

**Constraints:** `T` is `bool`, `u8`, `u16`, `u32`, `u64`, `u256`, `b256` or `raw_ptr`.

___
---

```sway
__gt<T>(lhs: T, rhs: T) -> bool
Expand All @@ -91,7 +91,8 @@ __gt<T>(lhs: T, rhs: T) -> bool
**Description:** Returns whether `lhs` is greater than `rhs`.

**Constraints:** `T` is `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.
___

---

```sway
__lt<T>(lhs: T, rhs: T) -> bool
Expand All @@ -100,7 +101,8 @@ __lt<T>(lhs: T, rhs: T) -> bool
**Description:** Returns whether `lhs` is less than `rhs`.

**Constraints:** `T` is `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.
___

---

```sway
__gtf<T>(index: u64, tx_field_id: u64) -> T
Expand All @@ -110,7 +112,7 @@ __gtf<T>(index: u64, tx_field_id: u64) -> T

**Constraints:** None.

___
---

```sway
__addr_of<T>(val: T) -> raw_ptr
Expand All @@ -120,7 +122,7 @@ __addr_of<T>(val: T) -> raw_ptr

**Constraints:** `T` is a reference type.

___
---

```sway
__state_load_word(key: b256) -> u64
Expand All @@ -130,7 +132,7 @@ __state_load_word(key: b256) -> u64

**Constraints:** None.

___
---

```sway
__state_load_quad(key: b256, ptr: raw_ptr, slots: u64) -> bool
Expand All @@ -140,7 +142,7 @@ __state_load_quad(key: b256, ptr: raw_ptr, slots: u64) -> bool

**Constraints:** None.

___
---

```sway
__state_store_word(key: b256, val: u64) -> bool
Expand All @@ -150,7 +152,7 @@ __state_store_word(key: b256, val: u64) -> bool

**Constraints:** None.

___
---

```sway
__state_store_quad(key: b256, ptr: raw_ptr, slots: u64) -> bool
Expand All @@ -160,7 +162,7 @@ __state_store_quad(key: b256, ptr: raw_ptr, slots: u64) -> bool

**Constraints:** None.

___
---

```sway
__log<T>(val: T)
Expand All @@ -170,7 +172,7 @@ __log<T>(val: T)

**Constraints:** None.

___
---

```sway
__add<T>(lhs: T, rhs: T) -> T
Expand All @@ -180,7 +182,7 @@ __add<T>(lhs: T, rhs: T) -> T

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

___
---

```sway
__sub<T>(lhs: T, rhs: T) -> T
Expand All @@ -190,7 +192,7 @@ __sub<T>(lhs: T, rhs: T) -> T

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

___
---

```sway
__mul<T>(lhs: T, rhs: T) -> T
Expand All @@ -200,7 +202,7 @@ __mul<T>(lhs: T, rhs: T) -> T

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

___
---

```sway
__div<T>(lhs: T, rhs: T) -> T
Expand All @@ -210,7 +212,7 @@ __div<T>(lhs: T, rhs: T) -> T

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

___
---

```sway
__and<T>(lhs: T, rhs: T) -> T
Expand All @@ -220,7 +222,7 @@ __and<T>(lhs: T, rhs: T) -> T

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.

___
---

```sway
__or<T>(lhs: T, rhs: T) -> T
Expand All @@ -230,7 +232,7 @@ __or<T>(lhs: T, rhs: T) -> T

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.

___
---

```sway
__xor<T>(lhs: T, rhs: T) -> T
Expand All @@ -239,7 +241,8 @@ __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`, `u256`, `b256`.
___

---

```sway
__mod<T>(lhs: T, rhs: T) -> T
Expand All @@ -248,7 +251,8 @@ __mod<T>(lhs: T, rhs: T) -> T
**Description:** Modulo of `lhs` by `rhs`.

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`.
___

---

```sway
__rsh<T>(lhs: T, rhs: u64) -> T
Expand All @@ -257,7 +261,8 @@ __rsh<T>(lhs: T, rhs: u64) -> T
**Description:** Logical right shift of `lhs` by `rhs`.

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.
___

---

```sway
__lsh<T>(lhs: T, rhs: u64) -> T
Expand All @@ -266,7 +271,8 @@ __lsh<T>(lhs: T, rhs: u64) -> T
**Description:** Logical left shift of `lhs` by `rhs`.

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.
___

---

```sway
__revert(code: u64)
Expand All @@ -276,7 +282,7 @@ __revert(code: u64)

**Constraints:** None.

___
---

```sway
__ptr_add(ptr: raw_ptr, offset: u64)
Expand All @@ -286,7 +292,7 @@ __ptr_add(ptr: raw_ptr, offset: u64)

**Constraints:** None.

___
---

```sway
__ptr_sub(ptr: raw_ptr, offset: u64)
Expand All @@ -296,7 +302,7 @@ __ptr_sub(ptr: raw_ptr, offset: u64)

**Constraints:** None.

___
---

```sway
__smo<T>(recipient: b256, data: T, coins: u64)
Expand All @@ -306,7 +312,7 @@ __smo<T>(recipient: b256, data: T, coins: u64)

**Constraints:** None.

___
---

```sway
__not(op: T) -> T
Expand All @@ -316,7 +322,7 @@ __not(op: T) -> T

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.

___
---

```sway
__jmp_mem()
Expand All @@ -325,3 +331,44 @@ __jmp_mem()
**Description:** Jumps to `MEM[$hp]`.

**Constraints:** None.

---

```sway
__slice(item: &[T; N], start: u64, end: u64) -> &[T]
__slice(item: &[T], start: u64, end: u64) -> &[T]
__slice(item: &mut [T; N], start: u64, end: u64) -> &mut [T]
__slice(item: &mut [T], start: u64, end: u64) -> &mut [T]
```

**Description:** Slices an array or another slice.

This intrinsic returns a reference to a slice containing the range of elements inside `item`.
The mutability of reference is defined by the first parameter mutability.

Runtime bound checks are not generated, and must be done manually when and where appropriated. Compile time bound checks are done when possible.
xunilrj marked this conversation as resolved.
Show resolved Hide resolved

**Constraints:**

- `item` is an array or a slice;
- when `start` is a literal, it must be smaller than `item` length;
- when `end` is a literal, it must be smaller than or equal to `item` length;
- `end` must be greater than or equal to `start`

---

```sway
__elem_at(item: &[T; N], index: u64) -> &T
__elem_at(item: &[T], index: u64) -> &T
__elem_at(item: &mut [T; N], index: u64) -> &mut T
__elem_at(item: &mut [T], index: u64) -> &mut T
```

**Description:** Returns a reference to the indexed element. The mutability of reference is defined by the first parameter mutability.

Runtime bound checks are not generated, and must be done manually when and where appropriated. Compile time bound checks are done when possible.
xunilrj marked this conversation as resolved.
Show resolved Hide resolved

**Constraints:**

- `item` is a reference to an array or a reference to a slice;
- when `index` is a literal, it must be smaller than `item` length;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
14 changes: 14 additions & 0 deletions docs/reference/src/code/language/built-ins/slices/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[package]]
name = 'arrays'
source = 'root'
dependencies = ['std']

[[package]]
name = 'core'
source = 'path+from-root-54F92B42A645EA2B'
dependencies = []

[[package]]
name = 'std'
source = 'git+https://github.com/fuellabs/sway?tag=v0.24.5#e695606d8884a18664f6231681333a784e623bc9'
dependencies = ['core']
8 changes: 8 additions & 0 deletions docs/reference/src/code/language/built-ins/slices/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "lib.sw"
license = "Apache-2.0"
name = "slices"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library;

// ANCHOR: syntax
fn syntax(s: &[u64]) -> u64 {
s.len()
}
// ANCHOR_END: syntax
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ Sway has the following primitive types:
1. `str[n]` (fixed-length string of size n)
4. [Bytes](b256.md)
1. `b256` (256 bits / 32 bytes, i.e. a hash)
5. [Slices](slices.md)

<!-- TODO: The following sentence does not belong here. We need to convey the default size, including word size, somewhere however not on this page -->

The default numeric type is `u64`. The FuelVM's word size is 64 bits, and the cases where using a smaller numeric type to save space are minimal.

All other types in Sway are built up of these primitive types, or references to these primitive types.
Expand Down
Loading
Loading