Skip to content

Commit

Permalink
make test on grain's gc
Browse files Browse the repository at this point in the history
  • Loading branch information
coned committed Jul 3, 2022
1 parent 7b07c47 commit 1c7fdca
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 62 deletions.
26 changes: 10 additions & 16 deletions hazel/hazel_util.gr
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,48 @@ import WasmI32 from "runtime/unsafe/wasmi32"
import { coerceNumberToWasmI32 } from "runtime/numbers"
import PrintWasm from "runtime/unsafe/printWasm"
import * from "syntax_tree"
import Memory from "runtime/unsafe/memory"
import Gc from "runtime/gc"

@disableGC
export let rec to_number = (arr0 : DHExp) => {
let arr = WasmI32.fromGrain(arr0)
let arr = WasmI32.add(arr, 2n)
let arr = WasmI32.add(arr, 4n)
let ret = WasmI32.toGrain(arr) : Number
Memory.decRef(WasmI32.fromGrain(arr0))
Memory.decRef(WasmI32.fromGrain(to_number))
Gc.decRef(WasmI32.fromGrain(arr0))
ret
}

@disableGC
export let rec to_DHExp = (x : Number) => {
let arr = WasmI32.fromGrain(x)
let arr = WasmI32.sub(arr, 2n)
let arr = WasmI32.sub(arr, 4n)
let ret = WasmI32.toGrain(arr) : (DHExp)
Memory.decRef(WasmI32.fromGrain(x))
Memory.decRef(WasmI32.fromGrain(to_DHExp))
//Gc.decRef(WasmI32.fromGrain(x))
ret
}

@disableGC
export let rec bool_to_number = (b0 : Bool) => {
let b = WasmI32.fromGrain(b0)
let ret = WasmI32.toGrain(b) : (Number)
Memory.decRef(WasmI32.fromGrain(b0))
Memory.decRef(WasmI32.fromGrain(bool_to_number))
Gc.decRef(WasmI32.fromGrain(b0))
ret
}

@disableGC
export let rec to_bool = (x : Number) => {
let arr = WasmI32.fromGrain(x)
let ret = WasmI32.toGrain(arr) : (Bool)
Memory.decRef(WasmI32.fromGrain(x))
Memory.decRef(WasmI32.fromGrain(to_bool))
Gc.decRef(WasmI32.fromGrain(x))
ret
}

@disableGC
export let rec is_indet = (x : Number) => {
let addr = WasmI32.fromGrain(x)
let addr = WasmI32.remU(addr, 8n)
let ret = WasmI32.eq(addr, 2n)
Memory.decRef(WasmI32.fromGrain(x))
Memory.decRef(WasmI32.fromGrain(is_indet))
let ret = WasmI32.eq(addr, 4n)
Gc.decRef(WasmI32.fromGrain(x))
ret
}

Expand All @@ -58,7 +53,6 @@ export let rec is_const = (x : Number) => {
let addr = WasmI32.fromGrain(x)
let addr = WasmI32.remU(addr, 8n)
let ret = WasmI32.eq(addr, 6n)
Memory.decRef(WasmI32.fromGrain(x))
Memory.decRef(WasmI32.fromGrain(is_const))
Gc.decRef(WasmI32.fromGrain(x))
ret
}
9 changes: 8 additions & 1 deletion hazel/syntax_tree_helper.gr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * from "./syntax_tree"
import * from "./hazel_util"
import Gc from "runtime/gc"

export let buildBoolLit = (x : Bool) => {
to_number(BoolLit(x))
Expand All @@ -12,7 +13,13 @@ export let buildBinBoolOpAnd = (x, y) => {
}

export let buildEmptyHole = (metavar : Number) => {
to_number(EmptyHole(metavar))
let exp = EmptyHole(metavar)
print(Gc.getRefCount(exp))
let ret = to_number(exp)
print(Gc.getRefCount(ret))
print(Gc.getRefCount(ret))
//print(Gc.getRefCount(exp))
ret
}

export let builder = (x : Number) => {
Expand Down
27 changes: 15 additions & 12 deletions hazel/test5.gr
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import Util from "hazel_util"
import Hazel from "hazel"

let p1 = Hazel.htrue;
let p11 = true;
// print(Gc.getRefCount(p1))
// print(Gc.getRefCount(p11))
let p2 = Hazel.hfalse;
let p3 = Hazel.buildEmptyHole(312);
let result = Hazel.and(p1, p3)
let h = BoolLit(true)
print(Gc.getRefCount(h))
let a = Util.to_number(h)
print(Gc.getRefCount(h))
Util.decNumberRef(a)
print(Gc.getRefCount(h))
print(h)
print(Gc.getRefCount(h))
let hh = Util.to_DHExp(a)
print(Gc.getRefCount(hh))
print(Gc.getRefCount(h))
print(Gc.getRefCount(p3))
// let result = Hazel.and(p1, p3)
// let h = BoolLit(true)
// print(Gc.getRefCount(h))
// let a = Util.to_number(h)
// print(Gc.getRefCount(h))
// print(Gc.getRefCount(h))
// print(h)
// print(Gc.getRefCount(h))
// let hh = Util.to_DHExp(a)
// print(Gc.getRefCount(hh))
// print(Gc.getRefCount(h))
22 changes: 22 additions & 0 deletions hazel/test6.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import WasmI32 from "runtime/unsafe/wasmi32"
import PrintWasm from "runtime/unsafe/printWasm"
import Gc from "runtime/gc"
import * from "syntax_tree"

Gc.setDebug(true)

@disableGC
let rec to_number = (arr0 : DHExp) => {
let arr = WasmI32.fromGrain(arr0)
let arr = WasmI32.add(arr, 4n)
let ret = WasmI32.toGrain(arr) : Number
Gc.decRef(WasmI32.fromGrain(arr0))
ret
}

let arr = EmptyHole(0)
print(Gc.getRefCount(arr))
let num = to_number(arr)
print(Gc.getRefCount(arr))
print(Gc.getRefCount(num))

63 changes: 30 additions & 33 deletions stdlib/runtime/gc.gr
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ let ptr_to_indet = (userPtr: WasmI32, is_indet: Bool) => {
userPtr
}
}


/*
//Debugging functions:
import foreign wasm fd_sync : (WasmI32) -> WasmI32 from "wasi_snapshot_preview1"

Expand Down Expand Up @@ -157,7 +156,7 @@ let logDecRef = (userPtr: WasmI32, refCount: WasmI32, ignoreZeros) => {
Malloc.free(refCountStr)
}


*/

let getRefCount = (userPtr: WasmI32) => {
WasmI32.load(userPtr - _HEADER_SIZE, 0n)
Expand All @@ -181,6 +180,7 @@ export let malloc = (size: WasmI32) => {

export let free = (userPtr: WasmI32) => {
// Free the raw pointer
let (userPtr, _) = indet_to_ptr(userPtr)
Malloc.free(userPtr - _HEADER_SIZE)
}

Expand All @@ -189,10 +189,10 @@ let incRef = (userPtr: WasmI32) => {
WasmI32.eqz(userPtr & Tags._GRAIN_GENERIC_TAG_MASK) &&
WasmI32.ne(userPtr, 0n)
) {
if (_DEBUG) {
logIncRef(userPtr, getRefCount(userPtr))
void;
}
// if (_DEBUG) {
// logIncRef(userPtr, getRefCount(userPtr))
// void;
// }
setRefCount(userPtr, getRefCount(userPtr) + 1n)
}
userPtr
Expand All @@ -204,10 +204,10 @@ let rec decRef = (userPtr: WasmI32, ignoreZeros: Bool) => {
WasmI32.ne(userPtr, 0n)
) {
let refCount = getRefCount(userPtr)
if (_DEBUG) {
logDecRef(userPtr, refCount, ignoreZeros)
void
}
// if (_DEBUG) {
// logDecRef(userPtr, refCount, ignoreZeros)
// void
// }

if (WasmI32.eqz(refCount)) {
if (ignoreZeros) {
Expand Down Expand Up @@ -284,29 +284,26 @@ let indet_closure = (userPtr: WasmI32, f) => {
}

export let incRef = (userPtr: WasmI32) => indet_closure(userPtr, incRef)
let decRefIgnoreZeros = userPtr => decRef(userPtr, true)

let decRef = userPtr => decRef(userPtr, false)
export let decRefIgnoreZeros = (userPtr: WasmI32) => indet_closure(userPtr, decRefIgnoreZeros)
export let decRef = (userPtr: WasmI32) => indet_closure(userPtr, decRef)

// For debugging:

// export let getRefCount = (value) => {
// let userPtr = WasmI32.fromGrain(value)
// let (userPtr, _) = indet_to_ptr(userPtr)
// let ret = if (
// WasmI32.eqz(userPtr & Tags._GRAIN_GENERIC_TAG_MASK) &&
// WasmI32.ne(userPtr, 0n)
// ) {
// WasmI32.toGrain((getRefCount(userPtr) * 2n) + 1n) : Number
// } else {
// 0
// }
// decRef(userPtr)
// ret
// }

// export let rec setDebug = (enabled: Bool) => {
// _DEBUG = enabled
// decRef(WasmI32.fromGrain(setDebug))
// void
// }
export let getRefCount = (value) => {
let userPtr = WasmI32.fromGrain(value)
let (userPtr, _) = indet_to_ptr(userPtr)
let ret = if (WasmI32.eqz(userPtr & Tags._GRAIN_GENERIC_TAG_MASK) && WasmI32.ne(userPtr, 0n)) {
WasmI32.toGrain((getRefCount(userPtr) * 2n) + 1n) : Number
} else {
0
}
decRef(userPtr)
ret
}

export let rec setDebug = (enabled: Bool) => {
_DEBUG = enabled
decRef(WasmI32.fromGrain(setDebug))
void
}

0 comments on commit 1c7fdca

Please sign in to comment.