Skip to content

Commit

Permalink
[WebAssembly] Use absolute pointers #ifdef __WASM__
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxDesiatov committed Aug 8, 2021
1 parent 6c50414 commit 9b94510
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions include/swift/Basic/RelativePointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ static inline uintptr_t applyRelativeOffset(BasePtrTy *basePtr, Offset offset) {
std::is_signed<Offset>::value,
"offset type should be signed integer");

#if defined(__WASM__)
// WebAssembly target uses only absolute pointers.
// See https://forums.swift.org/t/wasm-support/16087/17 for more details.
return (uintptr_t)(intptr_t)offset;
#endif

auto base = reinterpret_cast<uintptr_t>(basePtr);
// We want to do wrapping arithmetic, but with a sign-extended
// offset. To do this in C, we need to do signed promotion to get
Expand All @@ -165,6 +171,14 @@ static inline Offset measureRelativeOffset(A *referent, B *base) {
std::is_signed<Offset>::value,
"offset type should be signed integer");

#if defined(__WASM__)
// WebAssembly target uses only absolute pointers.
auto offset = (Offset)(uintptr_t)referent;
assert((intptr_t)offset == (intptr_t)(uintptr_t)referent
&& "pointer too large to fit in offset type");
return offset;
#endif

auto distance = (uintptr_t)referent - (uintptr_t)base;
// Truncate as unsigned, then wrap around to signed.
auto truncatedDistance =
Expand Down

0 comments on commit 9b94510

Please sign in to comment.