Skip to content

Commit

Permalink
feat(stdlib)!: Convert unsafe Wasm functions to operators (#1734)
Browse files Browse the repository at this point in the history
* feat!: Move `WasmF32` to operators

* feat: Move `WasmF64` to operators

* feat: Move `WasmI64` to operators

* Feat: `WasmI32` to operators

* chore: Update tests and docs

* chore: Undo Global Scope's

* chore: Apply suggestions from code review

* fix: mistake in rebase

* chore: Revert changes to `package-lock`
  • Loading branch information
spotandjake committed Apr 6, 2023
1 parent 2f0f57b commit 114d17b
Show file tree
Hide file tree
Showing 63 changed files with 1,895 additions and 1,995 deletions.
3 changes: 2 additions & 1 deletion cli/__test__/index.gr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ include "runtime/unsafe/wasmi64"
include "./nearEnv" as Env

provide let hello = () => {
from WasmI32 use { (+) }
let value = "Hello, World!"
let length = WasmI64.load32U(WasmI32.fromGrain(value), 4n)
let ptr = WasmI64.extendI32U(WasmI32.add(WasmI32.fromGrain(value), 8n))
let ptr = WasmI64.extendI32U(WasmI32.fromGrain(value) + 8n)
Env.valueReturn(length, ptr)
}
3 changes: 2 additions & 1 deletion compiler/test/input/letMutForLoop.gr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ include "runtime/debugPrint"

@unsafe
let foo = () => {
for (let mut x = 0N; WasmI64.ltS(x, 5N); x = WasmI64.add(x, 1N)) {
from WasmI64 use { (+), (<) }
for (let mut x = 0N; x < 5N; x += 1N) {
DebugPrint.printI64(x)
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/input/mallocTight.gr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module MallocTight

include "runtime/malloc"
include "runtime/unsafe/wasmi32"
from WasmI32 use { add as (+), sub as (-), mul as (*), eq as (==), ne as (!=) }
from WasmI32 use { (+), (-), (*), (==), (!=) }

primitive assert = "@assert"
primitive ignore = "@ignore"
Expand Down
7 changes: 4 additions & 3 deletions compiler/test/input/memoryBase/asserts.gr
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ primitive typeMetadata = "@heap.type_metadata"

@unsafe
let doTest = () => {
assert WasmI32.eq(heapBase, 0x110000n)
assert WasmI32.eq(heapStart(), 0x1101A8n)
assert WasmI32.eq(typeMetadata, 0x110008n)
from WasmI32 use { (==) }
assert heapBase == 0x110000n
assert heapStart() == 0x1101A8n
assert typeMetadata == 0x110008n
}

doTest()
92 changes: 47 additions & 45 deletions compiler/test/stdlib/wasmf32.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,60 @@ include "runtime/unsafe/conv"

@unsafe
let test = () => {
let eq = (==)
from WasmF32 use { (+), (-), (*), (/), (==), (!=), (<), (<=), (>), (>=) }
// unary tests
assert WasmF32.eq(WasmF32.neg(-1.23w), 1.23w)
assert WasmF32.eq(WasmF32.neg(1.23w), -1.23w)
assert WasmF32.eq(WasmF32.abs(1.23w), 1.23w)
assert WasmF32.eq(WasmF32.abs(-1.23w), 1.23w)
assert WasmF32.eq(WasmF32.ceil(1.5w), 2.0w)
assert WasmF32.eq(WasmF32.ceil(-1.5w), -1.0w)
assert WasmF32.eq(WasmF32.floor(1.5w), 1.0w)
assert WasmF32.eq(WasmF32.floor(-1.5w), -2.0w)
assert WasmF32.eq(WasmF32.trunc(1.23w), 1.0w)
assert WasmF32.eq(WasmF32.trunc(-1.23w), -1.0w)
assert WasmF32.eq(WasmF32.nearest(1.23w), 1.0w)
assert WasmF32.eq(WasmF32.nearest(1.73w), 2.0w)
assert WasmF32.eq(WasmF32.nearest(-1.23w), -1.0w)
assert WasmF32.eq(WasmF32.nearest(-1.73w), -2.0w)
assert WasmF32.eq(WasmF32.sqrt(16.0w), 4.0w)
assert WasmF32.neg(-1.23w) == 1.23w
assert WasmF32.neg(1.23w) == -1.23w
assert WasmF32.abs(1.23w) == 1.23w
assert WasmF32.abs(-1.23w) == 1.23w
assert WasmF32.ceil(1.5w) == 2.0w
assert WasmF32.ceil(-1.5w) == -1.0w
assert WasmF32.floor(1.5w) == 1.0w
assert WasmF32.floor(-1.5w) == -2.0w
assert WasmF32.trunc(1.23w) == 1.0w
assert WasmF32.trunc(-1.23w) == -1.0w
assert WasmF32.nearest(1.23w) == 1.0w
assert WasmF32.nearest(1.73w) == 2.0w
assert WasmF32.nearest(-1.23w) == -1.0w
assert WasmF32.nearest(-1.73w) == -2.0w
assert WasmF32.sqrt(16.0w) == 4.0w

// binary tests
assert WasmF32.eq(WasmF32.add(2.5w, 4.0w), 6.5w)
assert WasmF32.eq(WasmF32.sub(2.5w, 4.0w), -1.5w)
assert WasmF32.eq(WasmF32.mul(2.5w, 4.0w), 10.0w)
assert WasmF32.eq(WasmF32.div(2.5w, 4.0w), 0.625w)
assert WasmF32.eq(WasmF32.copySign(2.5w, 4.0w), 2.5w)
assert WasmF32.eq(WasmF32.copySign(2.5w, -4.0w), -2.5w)
assert WasmF32.eq(WasmF32.min(2.5w, 4.0w), 2.5w)
assert WasmF32.eq(WasmF32.max(2.5w, 4.0w), 4.0w)
assert WasmF32.eq(1.23w, 1.23w)
assert !WasmF32.eq(1.23w, -1.23w)
assert WasmF32.ne(1.23w, -1.23w)
assert !WasmF32.ne(1.23w, 1.23w)
assert WasmF32.lt(1.23w, 1.24w)
assert !WasmF32.lt(1.23w, 1.22w)
assert WasmF32.le(1.23w, 1.24w)
assert WasmF32.le(1.23w, 1.23w)
assert !WasmF32.le(1.23w, 1.22w)
assert WasmF32.gt(1.23w, 1.22w)
assert !WasmF32.gt(1.23w, 1.24w)
assert WasmF32.ge(1.23w, 1.22w)
assert WasmF32.ge(1.23w, 1.23w)
assert !WasmF32.ge(1.23w, 1.24w)
assert 2.5w + 4.0w == 6.5w
assert 2.5w - 4.0w == -1.5w
assert 2.5w * 4.0w == 10.0w
assert 2.5w / 4.0w == 0.625w
assert WasmF32.copySign(2.5w, 4.0w) == 2.5w
assert WasmF32.copySign(2.5w, -4.0w) == -2.5w
assert WasmF32.min(2.5w, 4.0w) == 2.5w
assert WasmF32.max(2.5w, 4.0w) == 4.0w
assert 1.23w == 1.23w
assert !(1.23w == -1.23w)
assert 1.23w != -1.23w
assert !(1.23w != 1.23w)
assert 1.23w < 1.24w
assert !(1.23w < 1.22w)
assert 1.23w <= 1.24w
assert 1.23w <= 1.23w
assert !(1.23w <= 1.22w)
assert 1.23w > 1.22w
assert !(1.23w > 1.24w)
assert 1.23w >= 1.22w
assert 1.23w >= 1.23w
assert !(1.23w >= 1.24w)

// conversion tests
assert WasmF32.eq(WasmF32.reinterpretI32(0x42280000n), 42.0w)
assert WasmF32.eq(WasmF32.convertI32S(-1n), -1.0w)
assert WasmF32.eq(WasmF32.convertI32U(-1n), 4294967296.0w)
assert WasmF32.eq(WasmF32.convertI64S(-1N), -1.0w)
assert WasmF32.eq(WasmF32.convertI64U(-1N), 18446744073709552000.0w)
assert WasmF32.eq(WasmF32.demoteF64(1.23W), 1.23w)
assert WasmF32.reinterpretI32(0x42280000n) == 42.0w
assert WasmF32.convertI32S(-1n) == -1.0w
assert WasmF32.convertI32U(-1n) == 4294967296.0w
assert WasmF32.convertI64S(-1N) == -1.0w
assert WasmF32.convertI64U(-1N) == 18446744073709552000.0w
assert WasmF32.demoteF64(1.23W) == 1.23w

// Grain conversion tests
assert Conv.toFloat32(1.23w) == 1.23f
assert WasmF32.eq(Conv.fromFloat32(1.23f), 1.23w)
assert Conv.fromFloat32(1.23f) == 1.23w
assert eq(Conv.toFloat32(1.23w), 1.23f)
}

test()
92 changes: 47 additions & 45 deletions compiler/test/stdlib/wasmf64.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,60 @@ include "runtime/unsafe/conv"

@unsafe
let test = () => {
let eq = (==)
from WasmF64 use { (+), (-), (*), (/), (==), (!=), (<), (<=), (>), (>=) }
// unary tests
assert WasmF64.eq(WasmF64.neg(-1.23W), 1.23W)
assert WasmF64.eq(WasmF64.neg(1.23W), -1.23W)
assert WasmF64.eq(WasmF64.abs(1.23W), 1.23W)
assert WasmF64.eq(WasmF64.abs(-1.23W), 1.23W)
assert WasmF64.eq(WasmF64.ceil(1.5W), 2.0W)
assert WasmF64.eq(WasmF64.ceil(-1.5W), -1.0W)
assert WasmF64.eq(WasmF64.floor(1.5W), 1.0W)
assert WasmF64.eq(WasmF64.floor(-1.5W), -2.0W)
assert WasmF64.eq(WasmF64.trunc(1.23W), 1.0W)
assert WasmF64.eq(WasmF64.trunc(-1.23W), -1.0W)
assert WasmF64.eq(WasmF64.nearest(1.23W), 1.0W)
assert WasmF64.eq(WasmF64.nearest(1.73W), 2.0W)
assert WasmF64.eq(WasmF64.nearest(-1.23W), -1.0W)
assert WasmF64.eq(WasmF64.nearest(-1.73W), -2.0W)
assert WasmF64.eq(WasmF64.sqrt(16.0W), 4.0W)
assert WasmF64.neg(-1.23W) == 1.23W
assert WasmF64.neg(1.23W) == -1.23W
assert WasmF64.abs(1.23W) == 1.23W
assert WasmF64.abs(-1.23W) == 1.23W
assert WasmF64.ceil(1.5W) == 2.0W
assert WasmF64.ceil(-1.5W) == -1.0W
assert WasmF64.floor(1.5W) == 1.0W
assert WasmF64.floor(-1.5W) == -2.0W
assert WasmF64.trunc(1.23W) == 1.0W
assert WasmF64.trunc(-1.23W) == -1.0W
assert WasmF64.nearest(1.23W) == 1.0W
assert WasmF64.nearest(1.73W) == 2.0W
assert WasmF64.nearest(-1.23W) == -1.0W
assert WasmF64.nearest(-1.73W) == -2.0W
assert WasmF64.sqrt(16.0W) == 4.0W

// binary tests
assert WasmF64.eq(WasmF64.add(2.5W, 4.0W), 6.5W)
assert WasmF64.eq(WasmF64.sub(2.5W, 4.0W), -1.5W)
assert WasmF64.eq(WasmF64.mul(2.5W, 4.0W), 10.0W)
assert WasmF64.eq(WasmF64.div(2.5W, 4.0W), 0.625W)
assert WasmF64.eq(WasmF64.copySign(2.5W, 4.0W), 2.5W)
assert WasmF64.eq(WasmF64.copySign(2.5W, -4.0W), -2.5W)
assert WasmF64.eq(WasmF64.min(2.5W, 4.0W), 2.5W)
assert WasmF64.eq(WasmF64.max(2.5W, 4.0W), 4.0W)
assert WasmF64.eq(1.23W, 1.23W)
assert !WasmF64.eq(1.23W, -1.23W)
assert WasmF64.ne(1.23W, -1.23W)
assert !WasmF64.ne(1.23W, 1.23W)
assert WasmF64.lt(1.23W, 1.24W)
assert !WasmF64.lt(1.23W, 1.22W)
assert WasmF64.le(1.23W, 1.24W)
assert WasmF64.le(1.23W, 1.23W)
assert !WasmF64.le(1.23W, 1.22W)
assert WasmF64.gt(1.23W, 1.22W)
assert !WasmF64.gt(1.23W, 1.24W)
assert WasmF64.ge(1.23W, 1.22W)
assert WasmF64.ge(1.23W, 1.23W)
assert !WasmF64.ge(1.23W, 1.24W)
assert 2.5W + 4.0W == 6.5W
assert 2.5W - 4.0W == -1.5W
assert 2.5W * 4.0W == 10.0W
assert 2.5W / 4.0W == 0.625W
assert WasmF64.copySign(2.5W, 4.0W) == 2.5W
assert WasmF64.copySign(2.5W, -4.0W) == -2.5W
assert WasmF64.min(2.5W, 4.0W) == 2.5W
assert WasmF64.max(2.5W, 4.0W) == 4.0W
assert 1.23W == 1.23W
assert !(1.23W == -1.23W)
assert 1.23W != -1.23W
assert !(1.23W != 1.23W)
assert 1.23W < 1.24W
assert !(1.23W < 1.22W)
assert 1.23W <= 1.24W
assert 1.23W <= 1.23W
assert !(1.23W <= 1.22W)
assert 1.23W > 1.22W
assert !(1.23W > 1.24W)
assert 1.23W >= 1.22W
assert 1.23W >= 1.23W
assert !(1.23W >= 1.24W)

// conversion tests
assert WasmF64.eq(WasmF64.reinterpretI64(0x4045000000000000N), 42.0W)
assert WasmF64.eq(WasmF64.convertI32S(-1n), -1.0W)
assert WasmF64.eq(WasmF64.convertI32U(-1n), 4294967295.0W)
assert WasmF64.eq(WasmF64.convertI64S(-1N), -1.0W)
assert WasmF64.eq(WasmF64.convertI64U(-1N), 18446744073709552000.0W)
assert WasmF64.eq(WasmF64.promoteF32(1.5w), 1.5W)
assert WasmF64.reinterpretI64(0x4045000000000000N) == 42.0W
assert WasmF64.convertI32S(-1n) == -1.0W
assert WasmF64.convertI32U(-1n) == 4294967295.0W
assert WasmF64.convertI64S(-1N) == -1.0W
assert WasmF64.convertI64U(-1N) == 18446744073709552000.0W
assert WasmF64.promoteF32(1.5w) == 1.5W

// Grain conversion tests
assert Conv.toFloat64(1.23W) == 1.23d
assert WasmF64.eq(Conv.fromFloat64(1.23d), 1.23W)
assert eq(Conv.toFloat64(1.23W), 1.23d)
assert Conv.fromFloat64(1.23d) == 1.23W
}

test()
Loading

0 comments on commit 114d17b

Please sign in to comment.