Skip to content

Commit

Permalink
Overload intrinsics to support operating on raw_ptrs
Browse files Browse the repository at this point in the history
  • Loading branch information
AlicanC committed Oct 27, 2022
1 parent 166f72b commit 2ae963d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,10 @@ impl ty::TyIntrinsicFunctionKind {
warnings,
errors
);
let is_valid_arg_ty = matches!(arg_ty, TypeInfo::UnsignedInteger(_));
let is_valid_arg_ty = matches!(
arg_ty,
TypeInfo::UnsignedInteger(_) | TypeInfo::RawUntypedPtr
);
if !is_valid_arg_ty {
errors.push(CompileError::IntrinsicUnsupportedArgType {
name: kind.to_string(),
Expand All @@ -601,7 +604,13 @@ impl ty::TyIntrinsicFunctionKind {
let ctx = ctx
.by_ref()
.with_help_text("Incorrect argument type")
.with_type_annotation(lhs.return_type);
.with_type_annotation(match arg_ty {
// Expect a u64 at the RHS if the LHS is a raw_ptr
TypeInfo::RawUntypedPtr => {
insert_type(TypeInfo::UnsignedInteger(IntegerBits::SixtyFour))
}
_ => lhs.return_type,
});
let rhs = check!(
ty::TyExpression::type_check(ctx, rhs),
return err(warnings, errors),
Expand Down
14 changes: 5 additions & 9 deletions sway-lib-core/src/raw_ptr.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ 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)
let null_ptr = asm() { zero: raw_ptr };
__eq(self, null_ptr)
}

/// Gets the address of the pointer.
Expand All @@ -14,16 +14,12 @@ impl raw_ptr {

/// 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 }
__add(self, count)
}

/// 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 }
__sub(self, count)
}

/// Reads the given type of value from the address.
Expand All @@ -37,7 +33,7 @@ impl raw_ptr {
}
}
}

/// Copies `size` bytes from `self` to `dst`.
pub fn copy_to(self, dst: raw_ptr, count: u64) {
asm(dst: dst, src: self, count: count) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use basic_storage_abi::{StoreU64, Quad};
use std::assert::assert;

fn main() -> u64 {
let addr = abi(StoreU64, 0x5c452aa39ace5925513ac6a078eee827fc78245258c71ed9851ffe2251719b9d);
let addr = abi(StoreU64, 0x4a70196dbef39aeddf4fff88a2d9e54591d8743da0154649bd298cf86cc12c6f);
let key = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
let value = 4242;

Expand Down

0 comments on commit 2ae963d

Please sign in to comment.