forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR: - Introduces the `raw_ptr` type. - Changes RawVec's `ptr` field from `u64` to `raw_ptr`. - Disallows returning `raw_ptr`s (and aggregate types that contain it) from being returned from `main()` fns. ![image](https://user-images.githubusercontent.com/412180/195128463-1809a7f4-3964-419c-9eac-03df799dedc3.png) --- Note to @sezna: On our meeting we discussed `checks.rs`, which led me to this implementation: FuelLabs@090a4d1 That didn't work because `raw_ptr`s are converted to `sway_ir::irtype::Type::Uint(64)`s so there wasn't a way to differentiate them. I've reverted that and went with this instead: FuelLabs@fc6bf34 Co-authored-by: Andrew Cann <[email protected]>
- Loading branch information
Showing
56 changed files
with
344 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
library core; | ||
|
||
dep num; | ||
dep raw_ptr; | ||
dep ops; | ||
dep prelude; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
library prelude; | ||
|
||
//! Defines the Sway core library prelude. | ||
//! The prelude consists of implicitly available items, | ||
//! for which `use` is not required. | ||
use ::num::*; | ||
use ::raw_ptr::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
library raw_ptr; | ||
|
||
impl raw_ptr { | ||
/// Returns `true` if the pointer is null. | ||
pub fn is_null(self) -> bool { | ||
let addr = asm(ptr: self) { ptr: u64 }; | ||
__eq(addr, 0) | ||
} | ||
|
||
/// Gets the address of the pointer. | ||
pub fn addr(self) -> u64 { | ||
asm(ptr: self) { ptr: u64 } | ||
} | ||
|
||
/// Calculates the offset from the pointer. | ||
pub fn add(self, count: u64) -> raw_ptr { | ||
let addr = asm(ptr: self) { ptr: u64 }; | ||
let addr = __add(addr, count); | ||
asm(ptr: addr) { ptr: raw_ptr } | ||
} | ||
|
||
/// Calculates the offset from the pointer. | ||
pub fn sub(self, count: u64) -> raw_ptr { | ||
let addr = asm(ptr: self) { ptr: u64 }; | ||
let addr = __sub(addr, count); | ||
asm(ptr: addr) { ptr: raw_ptr } | ||
} | ||
|
||
/// Reads the given type of value from the address. | ||
pub fn read<T>(self) -> T { | ||
if __is_reference_type::<T>() { | ||
asm(ptr: self) { ptr: T } | ||
} else { | ||
asm(ptr: self, val) { | ||
lw val ptr i0; | ||
val: T | ||
} | ||
} | ||
} | ||
|
||
/// Copies `size` bytes from `self` to `dst`. | ||
pub fn copy_to(self, dst: raw_ptr, count: u64) { | ||
asm(dst: dst, src: self, count: count) { | ||
mcp dst src count; | ||
}; | ||
} | ||
|
||
/// Writes the given value to the address. | ||
pub fn write<T>(self, val: T) { | ||
if __is_reference_type::<T>() { | ||
asm(dst: self, src: val, count: __size_of_val(val)) { | ||
mcp dst src count; | ||
}; | ||
} else { | ||
asm(ptr: self, val: val) { | ||
sw ptr val i0; | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.