diff --git a/std/assembly/rt/index.d.ts b/std/assembly/rt/index.d.ts index 29af9f0baf..3841f24382 100644 --- a/std/assembly/rt/index.d.ts +++ b/std/assembly/rt/index.d.ts @@ -1,6 +1,7 @@ declare function __alloc(size: usize, id: u32): usize; declare function __realloc(ref: usize, size: usize): usize; declare function __free(ref: usize): void; +declare function __reset(): void; declare function __retain(ref: usize): usize; declare function __release(ref: usize): void; declare function __collect(): void; diff --git a/std/assembly/rt/pure.ts b/std/assembly/rt/pure.ts index f97603f644..e2daadbcf8 100644 --- a/std/assembly/rt/pure.ts +++ b/std/assembly/rt/pure.ts @@ -46,9 +46,9 @@ import { onincrement, ondecrement, onfree, onalloc } from "./rtrace"; // @ts-ignore: decorator @inline const COLOR_PURPLE: u32 = 3 << COLOR_SHIFT; // @ts-ignore: decorator -@inline const COLOR_RED: u32 = 4 << COLOR_SHIFT; +// @inline const COLOR_RED: u32 = 4 << COLOR_SHIFT; // @ts-ignore: decorator -@inline const COLOR_ORANGE: u32 = 5 << COLOR_SHIFT; +// @inline const COLOR_ORANGE: u32 = 5 << COLOR_SHIFT; // @ts-ignore: decorator @inline const VISIT_DECREMENT = 1; // guard 0 @@ -260,3 +260,8 @@ export function __retain(ref: usize): usize { export function __release(ref: usize): void { if (ref > __heap_base) decrement(changetype(ref - BLOCK_OVERHEAD)); } + +// @ts-ignore: decorator +@global @unsafe +export function __reset(): void { +} diff --git a/std/assembly/rt/stub.ts b/std/assembly/rt/stub.ts index 6efff7088d..b7585d903a 100644 --- a/std/assembly/rt/stub.ts +++ b/std/assembly/rt/stub.ts @@ -44,22 +44,21 @@ export function __realloc(ptr: usize, size: usize): usize { var block = changetype(ptr - BLOCK_OVERHEAD); var actualSize = block.mmInfo; if (DEBUG) assert(block.gcInfo == -1); + var isLast = ptr + actualSize == offset; + var alignedSize = (size + AL_MASK) & ~AL_MASK; if (size > actualSize) { - if (ptr + actualSize == offset) { // last block: grow + if (isLast) { // last block: grow if (size > BLOCK_MAXSIZE) unreachable(); - actualSize = (size + AL_MASK) & ~AL_MASK; - maybeGrowMemory(ptr + actualSize); - block.mmInfo = actualSize; + maybeGrowMemory(ptr + alignedSize); + block.mmInfo = alignedSize; } else { // copy to new block at least double the size - actualSize = max((size + AL_MASK) & ~AL_MASK, actualSize << 1); - let newPtr = __alloc(actualSize, block.rtId); + let newPtr = __alloc(max(alignedSize, actualSize << 1), block.rtId); memory.copy(newPtr, ptr, block.rtSize); block = changetype((ptr = newPtr) - BLOCK_OVERHEAD); } - } else if (ptr + actualSize == offset) { // last block: shrink - actualSize = (size + AL_MASK) & ~AL_MASK; - offset = ptr + actualSize; - block.mmInfo = actualSize; + } else if (isLast) { // last block: shrink + offset = ptr + alignedSize; + block.mmInfo = alignedSize; } block.rtSize = size; return ptr; @@ -78,7 +77,7 @@ export function __free(ptr: usize): void { // @ts-ignore: decorator @unsafe @global -function __reset(): void { // special +export function __reset(): void { // special offset = startOffset; } @@ -95,7 +94,7 @@ export function __release(ref: usize): void { // @ts-ignore: decorator @global @unsafe -export function __visit(ref: usize, cookie: u32): void { +function __visit(ref: usize, cookie: u32): void { } // @ts-ignore: decorator diff --git a/tests/compiler/number.optimized.wat b/tests/compiler/number.optimized.wat index 3bbcfc0314..08a41a70b1 100644 --- a/tests/compiler/number.optimized.wat +++ b/tests/compiler/number.optimized.wat @@ -1455,7 +1455,7 @@ if i32.const 0 i32.const 1304 - i32.const 71 + i32.const 70 i32.const 2 call $~lib/builtins/abort unreachable @@ -1470,7 +1470,7 @@ if i32.const 0 i32.const 1304 - i32.const 73 + i32.const 72 i32.const 13 call $~lib/builtins/abort unreachable diff --git a/tests/compiler/number.untouched.wat b/tests/compiler/number.untouched.wat index 8e5511c588..53ccc27823 100644 --- a/tests/compiler/number.untouched.wat +++ b/tests/compiler/number.untouched.wat @@ -3298,7 +3298,7 @@ if i32.const 0 i32.const 1752 - i32.const 71 + i32.const 70 i32.const 2 call $~lib/builtins/abort unreachable @@ -3315,7 +3315,7 @@ if i32.const 0 i32.const 1752 - i32.const 73 + i32.const 72 i32.const 13 call $~lib/builtins/abort unreachable diff --git a/tests/compiler/resolve-binary.optimized.wat b/tests/compiler/resolve-binary.optimized.wat index 2aaa88dace..6fbb8880aa 100644 --- a/tests/compiler/resolve-binary.optimized.wat +++ b/tests/compiler/resolve-binary.optimized.wat @@ -1630,7 +1630,7 @@ if i32.const 0 i32.const 1424 - i32.const 71 + i32.const 70 i32.const 2 call $~lib/builtins/abort unreachable @@ -1645,7 +1645,7 @@ if i32.const 0 i32.const 1424 - i32.const 73 + i32.const 72 i32.const 13 call $~lib/builtins/abort unreachable diff --git a/tests/compiler/resolve-binary.untouched.wat b/tests/compiler/resolve-binary.untouched.wat index 82a2527b16..2c44f55f27 100644 --- a/tests/compiler/resolve-binary.untouched.wat +++ b/tests/compiler/resolve-binary.untouched.wat @@ -4494,7 +4494,7 @@ if i32.const 0 i32.const 1872 - i32.const 71 + i32.const 70 i32.const 2 call $~lib/builtins/abort unreachable @@ -4511,7 +4511,7 @@ if i32.const 0 i32.const 1872 - i32.const 73 + i32.const 72 i32.const 13 call $~lib/builtins/abort unreachable diff --git a/tests/compiler/resolve-elementaccess.optimized.wat b/tests/compiler/resolve-elementaccess.optimized.wat index df670b7b79..43b8352ec0 100644 --- a/tests/compiler/resolve-elementaccess.optimized.wat +++ b/tests/compiler/resolve-elementaccess.optimized.wat @@ -1540,7 +1540,7 @@ if i32.const 0 i32.const 1432 - i32.const 71 + i32.const 70 i32.const 2 call $~lib/builtins/abort unreachable @@ -1555,7 +1555,7 @@ if i32.const 0 i32.const 1432 - i32.const 73 + i32.const 72 i32.const 13 call $~lib/builtins/abort unreachable diff --git a/tests/compiler/resolve-elementaccess.untouched.wat b/tests/compiler/resolve-elementaccess.untouched.wat index 98f6f46613..1b82fefefc 100644 --- a/tests/compiler/resolve-elementaccess.untouched.wat +++ b/tests/compiler/resolve-elementaccess.untouched.wat @@ -3430,7 +3430,7 @@ if i32.const 0 i32.const 1880 - i32.const 71 + i32.const 70 i32.const 2 call $~lib/builtins/abort unreachable @@ -3447,7 +3447,7 @@ if i32.const 0 i32.const 1880 - i32.const 73 + i32.const 72 i32.const 13 call $~lib/builtins/abort unreachable diff --git a/tests/compiler/rt/stub-realloc.optimized.wat b/tests/compiler/rt/stub-realloc.optimized.wat index bbbc60c392..3a55459a93 100644 --- a/tests/compiler/rt/stub-realloc.optimized.wat +++ b/tests/compiler/rt/stub-realloc.optimized.wat @@ -299,6 +299,7 @@ (local $2 i32) (local $3 i32) (local $4 i32) + (local $5 i32) local.get $0 i32.const 15 i32.and @@ -320,7 +321,7 @@ i32.sub local.tee $3 i32.load - local.set $2 + local.set $4 local.get $3 i32.load offset=4 i32.const -1 @@ -333,15 +334,23 @@ call $~lib/builtins/abort unreachable end + global.get $~lib/rt/stub/offset + local.get $0 + local.get $4 + i32.add + i32.eq + local.set $5 local.get $1 - local.get $2 + i32.const 15 + i32.add + i32.const -16 + i32.and + local.set $2 + local.get $1 + local.get $4 i32.gt_u if - global.get $~lib/rt/stub/offset - local.get $0 - local.get $2 - i32.add - i32.eq + local.get $5 if local.get $1 i32.const 1073741808 @@ -349,31 +358,21 @@ if unreachable end - local.get $1 - i32.const 15 - i32.add - i32.const -16 - i32.and - local.tee $2 local.get $0 + local.get $2 i32.add call $~lib/rt/stub/maybeGrowMemory local.get $3 local.get $2 i32.store else - local.get $1 - i32.const 15 - i32.add - i32.const -16 - i32.and - local.tee $4 local.get $2 + local.get $4 i32.const 1 i32.shl - local.tee $2 - local.get $4 + local.tee $4 local.get $2 + local.get $4 i32.gt_u select local.get $3 @@ -391,19 +390,10 @@ local.set $3 end else - global.get $~lib/rt/stub/offset - local.get $0 - local.get $2 - i32.add - i32.eq + local.get $5 if - local.get $1 - i32.const 15 - i32.add - i32.const -16 - i32.and - local.tee $2 local.get $0 + local.get $2 i32.add global.set $~lib/rt/stub/offset local.get $3 @@ -429,7 +419,7 @@ if i32.const 0 i32.const 24 - i32.const 71 + i32.const 70 i32.const 2 call $~lib/builtins/abort unreachable @@ -444,7 +434,7 @@ if i32.const 0 i32.const 24 - i32.const 73 + i32.const 72 i32.const 13 call $~lib/builtins/abort unreachable diff --git a/tests/compiler/rt/stub-realloc.untouched.wat b/tests/compiler/rt/stub-realloc.untouched.wat index a30e7f9e57..80c05aa2ab 100644 --- a/tests/compiler/rt/stub-realloc.untouched.wat +++ b/tests/compiler/rt/stub-realloc.untouched.wat @@ -1402,6 +1402,8 @@ (local $3 i32) (local $4 i32) (local $5 i32) + (local $6 i32) + (local $7 i32) local.get $0 i32.const 0 i32.ne @@ -1442,15 +1444,25 @@ call $~lib/builtins/abort unreachable end + local.get $0 + local.get $3 + i32.add + global.get $~lib/rt/stub/offset + i32.eq + local.set $4 + local.get $1 + i32.const 15 + i32.add + i32.const 15 + i32.const -1 + i32.xor + i32.and + local.set $5 local.get $1 local.get $3 i32.gt_u if - local.get $0 - local.get $3 - i32.add - global.get $~lib/rt/stub/offset - i32.eq + local.get $4 if local.get $1 i32.const 1073741808 @@ -1458,76 +1470,48 @@ if unreachable end - local.get $1 - i32.const 15 - i32.add - i32.const 15 - i32.const -1 - i32.xor - i32.and - local.set $3 local.get $0 - local.get $3 + local.get $5 i32.add call $~lib/rt/stub/maybeGrowMemory local.get $2 - local.get $3 + local.get $5 i32.store else - local.get $1 - i32.const 15 - i32.add - i32.const 15 - i32.const -1 - i32.xor - i32.and - local.tee $4 + local.get $5 + local.tee $6 local.get $3 i32.const 1 i32.shl - local.tee $5 - local.get $4 - local.get $5 + local.tee $7 + local.get $6 + local.get $7 i32.gt_u select - local.set $3 - local.get $3 local.get $2 i32.load offset=8 call $~lib/rt/stub/__alloc - local.set $4 - local.get $4 + local.set $6 + local.get $6 local.get $0 local.get $2 i32.load offset=12 call $~lib/memory/memory.copy - local.get $4 + local.get $6 local.tee $0 i32.const 16 i32.sub local.set $2 end else - local.get $0 - local.get $3 - i32.add - global.get $~lib/rt/stub/offset - i32.eq + local.get $4 if - local.get $1 - i32.const 15 - i32.add - i32.const 15 - i32.const -1 - i32.xor - i32.and - local.set $3 local.get $0 - local.get $3 + local.get $5 i32.add global.set $~lib/rt/stub/offset local.get $2 - local.get $3 + local.get $5 i32.store end end @@ -1553,7 +1537,7 @@ if i32.const 0 i32.const 24 - i32.const 71 + i32.const 70 i32.const 2 call $~lib/builtins/abort unreachable @@ -1570,7 +1554,7 @@ if i32.const 0 i32.const 24 - i32.const 73 + i32.const 72 i32.const 13 call $~lib/builtins/abort unreachable diff --git a/tests/compiler/std/static-array.optimized.wat b/tests/compiler/std/static-array.optimized.wat index 423beb38d5..cded33d95f 100644 --- a/tests/compiler/std/static-array.optimized.wat +++ b/tests/compiler/std/static-array.optimized.wat @@ -316,6 +316,7 @@ (local $2 i32) (local $3 i32) (local $4 i32) + (local $5 i32) local.get $0 i32.const 15 i32.and @@ -337,7 +338,7 @@ i32.sub local.tee $3 i32.load - local.set $2 + local.set $4 local.get $3 i32.load offset=4 i32.const -1 @@ -350,15 +351,23 @@ call $~lib/builtins/abort unreachable end + global.get $~lib/rt/stub/offset + local.get $0 + local.get $4 + i32.add + i32.eq + local.set $5 local.get $1 - local.get $2 + i32.const 15 + i32.add + i32.const -16 + i32.and + local.set $2 + local.get $1 + local.get $4 i32.gt_u if - global.get $~lib/rt/stub/offset - local.get $0 - local.get $2 - i32.add - i32.eq + local.get $5 if local.get $1 i32.const 1073741808 @@ -366,31 +375,21 @@ if unreachable end - local.get $1 - i32.const 15 - i32.add - i32.const -16 - i32.and - local.tee $2 local.get $0 + local.get $2 i32.add call $~lib/rt/stub/maybeGrowMemory local.get $3 local.get $2 i32.store else - local.get $1 - i32.const 15 - i32.add - i32.const -16 - i32.and - local.tee $4 local.get $2 + local.get $4 i32.const 1 i32.shl - local.tee $2 - local.get $4 + local.tee $4 local.get $2 + local.get $4 i32.gt_u select local.get $3 @@ -408,19 +407,10 @@ local.set $3 end else - global.get $~lib/rt/stub/offset - local.get $0 - local.get $2 - i32.add - i32.eq + local.get $5 if - local.get $1 - i32.const 15 - i32.add - i32.const -16 - i32.and - local.tee $2 local.get $0 + local.get $2 i32.add global.set $~lib/rt/stub/offset local.get $3 diff --git a/tests/compiler/std/static-array.untouched.wat b/tests/compiler/std/static-array.untouched.wat index f56218cec2..d30b00a4cd 100644 --- a/tests/compiler/std/static-array.untouched.wat +++ b/tests/compiler/std/static-array.untouched.wat @@ -1436,6 +1436,8 @@ (local $3 i32) (local $4 i32) (local $5 i32) + (local $6 i32) + (local $7 i32) local.get $0 i32.const 0 i32.ne @@ -1476,15 +1478,25 @@ call $~lib/builtins/abort unreachable end + local.get $0 + local.get $3 + i32.add + global.get $~lib/rt/stub/offset + i32.eq + local.set $4 + local.get $1 + i32.const 15 + i32.add + i32.const 15 + i32.const -1 + i32.xor + i32.and + local.set $5 local.get $1 local.get $3 i32.gt_u if - local.get $0 - local.get $3 - i32.add - global.get $~lib/rt/stub/offset - i32.eq + local.get $4 if local.get $1 i32.const 1073741808 @@ -1492,76 +1504,48 @@ if unreachable end - local.get $1 - i32.const 15 - i32.add - i32.const 15 - i32.const -1 - i32.xor - i32.and - local.set $3 local.get $0 - local.get $3 + local.get $5 i32.add call $~lib/rt/stub/maybeGrowMemory local.get $2 - local.get $3 + local.get $5 i32.store else - local.get $1 - i32.const 15 - i32.add - i32.const 15 - i32.const -1 - i32.xor - i32.and - local.tee $4 + local.get $5 + local.tee $6 local.get $3 i32.const 1 i32.shl - local.tee $5 - local.get $4 - local.get $5 + local.tee $7 + local.get $6 + local.get $7 i32.gt_u select - local.set $3 - local.get $3 local.get $2 i32.load offset=8 call $~lib/rt/stub/__alloc - local.set $4 - local.get $4 + local.set $6 + local.get $6 local.get $0 local.get $2 i32.load offset=12 call $~lib/memory/memory.copy - local.get $4 + local.get $6 local.tee $0 i32.const 16 i32.sub local.set $2 end else - local.get $0 - local.get $3 - i32.add - global.get $~lib/rt/stub/offset - i32.eq + local.get $4 if - local.get $1 - i32.const 15 - i32.add - i32.const 15 - i32.const -1 - i32.xor - i32.and - local.set $3 local.get $0 - local.get $3 + local.get $5 i32.add global.set $~lib/rt/stub/offset local.get $2 - local.get $3 + local.get $5 i32.store end end