diff --git a/std/assembly/memory/arena.ts b/std/assembly/memory/arena.ts index 298d56fd..e5601c07 100644 --- a/std/assembly/memory/arena.ts +++ b/std/assembly/memory/arena.ts @@ -1,23 +1,25 @@ -// An simple arena allocator that provides a `clear_memory` function to reset -// everything to the initial state. A user must have to make sure that there -// a no more references to cleared memory. +// A simple arena allocator that provides a `clear_memory` function to reset +// the heap to its initial state. A user has to make sure that there are no +// more references to cleared memory afterwards. Always aligns to 8 bytes. const ALIGN_LOG2: usize = 3; const ALIGN_SIZE: usize = 1 << ALIGN_LOG2; const ALIGN_MASK: usize = ALIGN_SIZE - 1; -var HEAP_OFFSET: usize = HEAP_BASE; // generated by the compiler +var HEAP_OFFSET: usize = HEAP_BASE; export function allocate_memory(size: usize): usize { if (!size) return 0; - var len: i32 = current_memory(); - if (HEAP_OFFSET + size > len << 16) - if(grow_memory(max(ceil(size / 65536), len * 2 - len)) < 0) - unreachable(); - var ptr: usize = HEAP_OFFSET; - if ((HEAP_OFFSET += size) & ALIGN_MASK) // align next offset - HEAP_OFFSET = (HEAP_OFFSET | ALIGN_MASK) + 1; - set_memory(ptr, 0, size); + var ptr = HEAP_OFFSET; + var off = (ptr + size + ALIGN_MASK) & ~ALIGN_MASK; + var avail = current_memory() << 16; + if (off > avail && grow_memory( + max( + (((off + 0xffff) & ~0xffff) - avail) >> 16, // minimum required pages + avail >> 16 // at least double memory + ) + ) < 0) unreachable(); // out of memory + HEAP_OFFSET = off; return ptr; } diff --git a/tests/compiler/std/array.optimized.wast b/tests/compiler/std/array.optimized.wast index 3c4d0037..432504c5 100644 --- a/tests/compiler/std/array.optimized.wast +++ b/tests/compiler/std/array.optimized.wast @@ -1,6 +1,6 @@ (module - (type $iiiv (func (param i32 i32 i32))) (type $ii (func (param i32) (result i32))) + (type $iiiv (func (param i32 i32 i32))) (type $iv (func (param i32))) (type $iiv (func (param i32 i32))) (type $iii (func (param i32 i32) (result i32))) @@ -13,342 +13,10 @@ (memory $0 1) (export "memory" (memory $0)) (start $start) - (func $std:memory/set_memory (; 0 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) - (local $3 i64) - (local $4 i32) - (if - (i32.eqz - (get_local $2) - ) - (return) - ) - (i32.store8 - (get_local $0) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 1) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 2) - ) - (return) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 1) - ) - (get_local $1) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 2) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 2) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 3) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 6) - ) - (return) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 3) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 4) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 8) - ) - (return) - ) - (i32.store - (tee_local $0 - (i32.add - (get_local $0) - (tee_local $4 - (i32.and - (i32.sub - (i32.const 0) - (get_local $0) - ) - (i32.const 3) - ) - ) - ) - ) - (tee_local $1 - (i32.mul - (get_local $1) - (i32.const 16843009) - ) - ) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (tee_local $2 - (i32.and - (i32.sub - (get_local $2) - (get_local $4) - ) - (i32.const -4) - ) - ) - ) - (i32.const 4) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 8) - ) - (return) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 4) - ) - (get_local $1) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 8) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 12) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 8) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 24) - ) - (return) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 12) - ) - (get_local $1) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 16) - ) - (get_local $1) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 20) - ) - (get_local $1) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 24) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 28) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 24) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 20) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 16) - ) - (get_local $1) - ) - (set_local $0 - (i32.add - (get_local $0) - (tee_local $4 - (i32.add - (i32.and - (get_local $0) - (i32.const 4) - ) - (i32.const 24) - ) - ) - ) - ) - (set_local $2 - (i32.sub - (get_local $2) - (get_local $4) - ) - ) - (set_local $3 - (i64.or - (i64.extend_u/i32 - (get_local $1) - ) - (i64.shl - (i64.extend_u/i32 - (get_local $1) - ) - (i64.const 32) - ) - ) - ) - (loop $continue|0 - (if - (i32.ge_u - (get_local $2) - (i32.const 32) - ) - (block - (i64.store - (get_local $0) - (get_local $3) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 8) - ) - (get_local $3) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 16) - ) - (get_local $3) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 24) - ) - (get_local $3) - ) - (set_local $2 - (i32.sub - (get_local $2) - (i32.const 32) - ) - ) - (set_local $0 - (i32.add - (get_local $0) - (i32.const 32) - ) - ) - (br $continue|0) - ) - ) - ) - ) - (func $std:memory/arena/allocate_memory (; 1 ;) (type $ii) (param $0 i32) (result i32) + (func $std:memory/arena/allocate_memory (; 0 ;) (type $ii) (param $0 i32) (result i32) (local $1 i32) (local $2 i32) + (local $3 i32) (if (i32.eqz (get_local $0) @@ -357,95 +25,83 @@ (i32.const 0) ) ) - (set_local $1 - (current_memory) - ) (if - (i32.gt_u - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) - (get_local $0) - ) - (i32.shl - (get_local $1) - (i32.const 16) - ) - ) - (if - (i32.lt_s - (grow_memory - (select - (tee_local $2 - (i32.trunc_s/f64 - (f64.ceil - (f64.div - (f64.convert_u/i32 - (get_local $0) - ) - (f64.const 65536) + (i32.and + (if (result i32) + (i32.gt_u + (tee_local $0 + (i32.and + (i32.add + (i32.add + (tee_local $2 + (get_global $std:memory/arena/HEAP_OFFSET) ) + (get_local $0) ) + (i32.const 7) ) + (i32.const -8) ) - (tee_local $1 - (i32.sub - (i32.mul - (get_local $1) - (i32.const 2) - ) - (get_local $1) - ) - ) - (i32.gt_s - (get_local $2) - (get_local $1) + ) + (tee_local $1 + (i32.shl + (current_memory) + (i32.const 16) ) ) ) - (i32.const 0) - ) - (unreachable) - ) - ) - (set_local $1 - (get_global $std:memory/arena/HEAP_OFFSET) - ) - (if - (block (result i32) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) + (i32.lt_s + (grow_memory + (select + (tee_local $3 + (i32.shr_u + (i32.sub + (i32.and + (i32.add + (get_local $0) + (i32.const 65535) + ) + (i32.const -65536) + ) + (get_local $1) + ) + (i32.const 16) + ) + ) + (tee_local $1 + (i32.shr_u + (get_local $1) + (i32.const 16) + ) + ) + (i32.gt_u + (get_local $3) + (get_local $1) + ) + ) + ) + (i32.const 0) + ) + (i32.gt_u (get_local $0) + (get_local $1) ) ) - (i32.and - (get_global $std:memory/arena/HEAP_OFFSET) - (i32.const 7) - ) - ) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (i32.or - (get_global $std:memory/arena/HEAP_OFFSET) - (i32.const 7) - ) - (i32.const 1) - ) + (i32.const 1) ) + (unreachable) ) - (call $std:memory/set_memory - (get_local $1) - (i32.const 0) + (set_global $std:memory/arena/HEAP_OFFSET (get_local $0) ) - (get_local $1) + (get_local $2) ) - (func $std:array/Array#get:length (; 2 ;) (type $ii) (param $0 i32) (result i32) + (func $std:array/Array#get:length (; 1 ;) (type $ii) (param $0 i32) (result i32) (i32.load offset=8 (get_local $0) ) ) - (func $std:memory/copy_memory (; 3 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) + (func $std:memory/copy_memory (; 2 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) (local $3 i32) (local $4 i32) (loop $continue|0 @@ -2021,7 +1677,7 @@ ) ) ) - (func $std:memory/move_memory (; 4 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) + (func $std:memory/move_memory (; 3 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) (local $3 i32) (if (i32.eq @@ -2311,10 +1967,10 @@ ) ) ) - (func $std:memory/arena/free_memory (; 5 ;) (type $iv) (param $0 i32) + (func $std:memory/arena/free_memory (; 4 ;) (type $iv) (param $0 i32) (nop) ) - (func $std:array/Array#__grow (; 6 ;) (type $iiv) (param $0 i32) (param $1 i32) + (func $std:array/Array#__grow (; 5 ;) (type $iiv) (param $0 i32) (param $1 i32) (local $2 i32) (if (i32.le_s @@ -2366,7 +2022,7 @@ (get_local $1) ) ) - (func $std:array/Array#push (; 7 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) + (func $std:array/Array#push (; 6 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (local $2 i32) (if (i32.eq @@ -2420,7 +2076,7 @@ ) (get_local $2) ) - (func $std:array/Array#__get (; 8 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) + (func $std:array/Array#__get (; 7 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (if (i32.ge_u (get_local $1) @@ -2442,7 +2098,7 @@ ) ) ) - (func $std:array/Array#pop (; 9 ;) (type $ii) (param $0 i32) (result i32) + (func $std:array/Array#pop (; 8 ;) (type $ii) (param $0 i32) (result i32) (local $1 i32) (if (i32.lt_s @@ -2478,7 +2134,7 @@ ) ) ) - (func $std:array/Array#unshift (; 10 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) + (func $std:array/Array#unshift (; 9 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (local $2 i32) (local $3 i32) (local $4 i32) @@ -2589,6 +2245,339 @@ ) (get_local $0) ) + (func $std:memory/set_memory (; 10 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) + (local $3 i64) + (local $4 i32) + (if + (i32.eqz + (get_local $2) + ) + (return) + ) + (i32.store8 + (get_local $0) + (get_local $1) + ) + (i32.store8 + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 1) + ) + (get_local $1) + ) + (if + (i32.le_u + (get_local $2) + (i32.const 2) + ) + (return) + ) + (i32.store8 + (i32.add + (get_local $0) + (i32.const 1) + ) + (get_local $1) + ) + (i32.store8 + (i32.add + (get_local $0) + (i32.const 2) + ) + (get_local $1) + ) + (i32.store8 + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 2) + ) + (get_local $1) + ) + (i32.store8 + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 3) + ) + (get_local $1) + ) + (if + (i32.le_u + (get_local $2) + (i32.const 6) + ) + (return) + ) + (i32.store8 + (i32.add + (get_local $0) + (i32.const 3) + ) + (get_local $1) + ) + (i32.store8 + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 4) + ) + (get_local $1) + ) + (if + (i32.le_u + (get_local $2) + (i32.const 8) + ) + (return) + ) + (i32.store + (tee_local $0 + (i32.add + (get_local $0) + (tee_local $4 + (i32.and + (i32.sub + (i32.const 0) + (get_local $0) + ) + (i32.const 3) + ) + ) + ) + ) + (tee_local $1 + (i32.mul + (get_local $1) + (i32.const 16843009) + ) + ) + ) + (i32.store + (i32.sub + (i32.add + (get_local $0) + (tee_local $2 + (i32.and + (i32.sub + (get_local $2) + (get_local $4) + ) + (i32.const -4) + ) + ) + ) + (i32.const 4) + ) + (get_local $1) + ) + (if + (i32.le_u + (get_local $2) + (i32.const 8) + ) + (return) + ) + (i32.store + (i32.add + (get_local $0) + (i32.const 4) + ) + (get_local $1) + ) + (i32.store + (i32.add + (get_local $0) + (i32.const 8) + ) + (get_local $1) + ) + (i32.store + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 12) + ) + (get_local $1) + ) + (i32.store + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 8) + ) + (get_local $1) + ) + (if + (i32.le_u + (get_local $2) + (i32.const 24) + ) + (return) + ) + (i32.store + (i32.add + (get_local $0) + (i32.const 12) + ) + (get_local $1) + ) + (i32.store + (i32.add + (get_local $0) + (i32.const 16) + ) + (get_local $1) + ) + (i32.store + (i32.add + (get_local $0) + (i32.const 20) + ) + (get_local $1) + ) + (i32.store + (i32.add + (get_local $0) + (i32.const 24) + ) + (get_local $1) + ) + (i32.store + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 28) + ) + (get_local $1) + ) + (i32.store + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 24) + ) + (get_local $1) + ) + (i32.store + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 20) + ) + (get_local $1) + ) + (i32.store + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 16) + ) + (get_local $1) + ) + (set_local $0 + (i32.add + (get_local $0) + (tee_local $4 + (i32.add + (i32.and + (get_local $0) + (i32.const 4) + ) + (i32.const 24) + ) + ) + ) + ) + (set_local $2 + (i32.sub + (get_local $2) + (get_local $4) + ) + ) + (set_local $3 + (i64.or + (i64.extend_u/i32 + (get_local $1) + ) + (i64.shl + (i64.extend_u/i32 + (get_local $1) + ) + (i64.const 32) + ) + ) + ) + (loop $continue|0 + (if + (i32.ge_u + (get_local $2) + (i32.const 32) + ) + (block + (i64.store + (get_local $0) + (get_local $3) + ) + (i64.store + (i32.add + (get_local $0) + (i32.const 8) + ) + (get_local $3) + ) + (i64.store + (i32.add + (get_local $0) + (i32.const 16) + ) + (get_local $3) + ) + (i64.store + (i32.add + (get_local $0) + (i32.const 24) + ) + (get_local $3) + ) + (set_local $2 + (i32.sub + (get_local $2) + (i32.const 32) + ) + ) + (set_local $0 + (i32.add + (get_local $0) + (i32.const 32) + ) + ) + (br $continue|0) + ) + ) + ) + ) (func $std:array/Array#shift (; 11 ;) (type $ii) (param $0 i32) (result i32) (local $1 i32) (if diff --git a/tests/compiler/std/array.wast b/tests/compiler/std/array.wast index 8b11e5ed..2bc348a5 100644 --- a/tests/compiler/std/array.wast +++ b/tests/compiler/std/array.wast @@ -1,7 +1,7 @@ (module (type $i (func (result i32))) - (type $iiiv (func (param i32 i32 i32))) (type $ii (func (param i32) (result i32))) + (type $iiiv (func (param i32 i32 i32))) (type $iv (func (param i32))) (type $iiv (func (param i32 i32))) (type $iii (func (param i32 i32) (result i32))) @@ -17,366 +17,12 @@ (memory $0 1) (export "memory" (memory $0)) (start $start) - (func $std:memory/set_memory (; 0 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) - (local $3 i32) - (local $4 i32) - (local $5 i64) - (if - (i32.eqz - (get_local $2) - ) - (return) - ) - (i32.store8 - (get_local $0) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 1) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 2) - ) - (return) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 1) - ) - (get_local $1) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 2) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 2) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 3) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 6) - ) - (return) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 3) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 4) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 8) - ) - (return) - ) - (set_local $3 - (i32.and - (i32.sub - (i32.const 0) - (get_local $0) - ) - (i32.const 3) - ) - ) - (set_local $0 - (i32.add - (get_local $0) - (get_local $3) - ) - ) - (set_local $2 - (i32.sub - (get_local $2) - (get_local $3) - ) - ) - (set_local $2 - (i32.and - (get_local $2) - (i32.sub - (i32.const 0) - (i32.const 4) - ) - ) - ) - (set_local $4 - (i32.mul - (i32.div_u - (i32.sub - (i32.const 0) - (i32.const 1) - ) - (i32.const 255) - ) - (get_local $1) - ) - ) - (i32.store - (get_local $0) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 4) - ) - (get_local $4) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 8) - ) - (return) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 4) - ) - (get_local $4) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 8) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 12) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 8) - ) - (get_local $4) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 24) - ) - (return) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 12) - ) - (get_local $4) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 16) - ) - (get_local $4) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 20) - ) - (get_local $4) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 24) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 28) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 24) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 20) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 16) - ) - (get_local $4) - ) - (set_local $3 - (i32.add - (i32.const 24) - (i32.and - (get_local $0) - (i32.const 4) - ) - ) - ) - (set_local $0 - (i32.add - (get_local $0) - (get_local $3) - ) - ) - (set_local $2 - (i32.sub - (get_local $2) - (get_local $3) - ) - ) - (set_local $5 - (i64.or - (i64.extend_u/i32 - (get_local $4) - ) - (i64.shl - (i64.extend_u/i32 - (get_local $4) - ) - (i64.const 32) - ) - ) - ) - (block $break|0 - (loop $continue|0 - (if - (i32.ge_u - (get_local $2) - (i32.const 32) - ) - (block - (block - (i64.store - (get_local $0) - (get_local $5) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 8) - ) - (get_local $5) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 16) - ) - (get_local $5) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 24) - ) - (get_local $5) - ) - (set_local $2 - (i32.sub - (get_local $2) - (i32.const 32) - ) - ) - (set_local $0 - (i32.add - (get_local $0) - (i32.const 32) - ) - ) - ) - (br $continue|0) - ) - ) - ) - ) - ) - (func $std:memory/arena/allocate_memory (; 1 ;) (type $ii) (param $0 i32) (result i32) + (func $std:memory/arena/allocate_memory (; 0 ;) (type $ii) (param $0 i32) (result i32) (local $1 i32) (local $2 i32) (local $3 i32) (local $4 i32) + (local $5 i32) (if (i32.eqz (get_local $0) @@ -386,98 +32,98 @@ ) ) (set_local $1 - (current_memory) - ) - (if - (i32.gt_u - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) - (get_local $0) - ) - (i32.shl - (get_local $1) - (i32.const 16) - ) - ) - (if - (i32.lt_s - (grow_memory - (select - (tee_local $2 - (i32.trunc_s/f64 - (f64.ceil - (f64.div - (f64.convert_u/i32 - (get_local $0) - ) - (f64.const 65536) - ) - ) - ) - ) - (tee_local $3 - (i32.sub - (i32.mul - (get_local $1) - (i32.const 2) - ) - (get_local $1) - ) - ) - (i32.gt_s - (get_local $2) - (get_local $3) - ) - ) - ) - (i32.const 0) - ) - (unreachable) - ) - ) - (set_local $4 (get_global $std:memory/arena/HEAP_OFFSET) ) + (set_local $2 + (i32.and + (i32.add + (i32.add + (get_local $1) + (get_local $0) + ) + (i32.const 7) + ) + (i32.xor + (i32.const 7) + (i32.const -1) + ) + ) + ) + (set_local $3 + (i32.shl + (current_memory) + (i32.const 16) + ) + ) (if (i32.and - (block (result i32) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) - (get_local $0) + (if (result i32) + (i32.ne + (i32.gt_u + (get_local $2) + (get_local $3) ) + (i32.const 0) ) - (get_global $std:memory/arena/HEAP_OFFSET) - ) - (i32.const 7) - ) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (i32.or - (get_global $std:memory/arena/HEAP_OFFSET) - (i32.const 7) + (i32.lt_s + (grow_memory + (select + (tee_local $4 + (i32.shr_u + (i32.sub + (i32.and + (i32.add + (get_local $2) + (i32.const 65535) + ) + (i32.xor + (i32.const 65535) + (i32.const -1) + ) + ) + (get_local $3) + ) + (i32.const 16) + ) + ) + (tee_local $5 + (i32.shr_u + (get_local $3) + (i32.const 16) + ) + ) + (i32.gt_u + (get_local $4) + (get_local $5) + ) + ) + ) + (i32.const 0) + ) + (i32.gt_u + (get_local $2) + (get_local $3) ) - (i32.const 1) ) + (i32.const 1) ) + (unreachable) ) - (call $std:memory/set_memory - (get_local $4) - (i32.const 0) - (get_local $0) + (set_global $std:memory/arena/HEAP_OFFSET + (get_local $2) ) (return - (get_local $4) + (get_local $1) ) ) - (func $std:array/Array#get:length (; 2 ;) (type $ii) (param $0 i32) (result i32) + (func $std:array/Array#get:length (; 1 ;) (type $ii) (param $0 i32) (result i32) (return (i32.load offset=8 (get_local $0) ) ) ) - (func $std:memory/copy_memory (; 3 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) + (func $std:memory/copy_memory (; 2 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) (local $3 i32) (local $4 i32) (local $5 i32) @@ -2277,7 +1923,7 @@ ) ) ) - (func $std:memory/move_memory (; 4 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) + (func $std:memory/move_memory (; 3 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) (local $3 i32) (if (i32.eq @@ -2603,9 +2249,9 @@ ) ) ) - (func $std:memory/arena/free_memory (; 5 ;) (type $iv) (param $0 i32) + (func $std:memory/arena/free_memory (; 4 ;) (type $iv) (param $0 i32) ) - (func $std:array/Array#__grow (; 6 ;) (type $iiv) (param $0 i32) (param $1 i32) + (func $std:array/Array#__grow (; 5 ;) (type $iiv) (param $0 i32) (param $1 i32) (local $2 i32) (if (i32.eqz @@ -2659,7 +2305,7 @@ (get_local $1) ) ) - (func $std:array/Array#push (; 7 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) + (func $std:array/Array#push (; 6 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (local $2 i32) (if (i32.eq @@ -2718,7 +2364,7 @@ ) ) ) - (func $std:array/Array#__get (; 8 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) + (func $std:array/Array#__get (; 7 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (if (i32.ge_u (get_local $1) @@ -2742,7 +2388,7 @@ ) ) ) - (func $std:array/Array#pop (; 9 ;) (type $ii) (param $0 i32) (result i32) + (func $std:array/Array#pop (; 8 ;) (type $ii) (param $0 i32) (result i32) (local $1 i32) (if (i32.lt_s @@ -2781,7 +2427,7 @@ ) ) ) - (func $std:array/Array#unshift (; 10 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) + (func $std:array/Array#unshift (; 9 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (local $2 i32) (local $3 i32) (local $4 i32) @@ -2902,6 +2548,361 @@ ) ) ) + (func $std:memory/set_memory (; 10 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) + (local $3 i32) + (local $4 i32) + (local $5 i64) + (if + (i32.eqz + (get_local $2) + ) + (return) + ) + (i32.store8 + (get_local $0) + (get_local $1) + ) + (i32.store8 + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 1) + ) + (get_local $1) + ) + (if + (i32.le_u + (get_local $2) + (i32.const 2) + ) + (return) + ) + (i32.store8 + (i32.add + (get_local $0) + (i32.const 1) + ) + (get_local $1) + ) + (i32.store8 + (i32.add + (get_local $0) + (i32.const 2) + ) + (get_local $1) + ) + (i32.store8 + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 2) + ) + (get_local $1) + ) + (i32.store8 + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 3) + ) + (get_local $1) + ) + (if + (i32.le_u + (get_local $2) + (i32.const 6) + ) + (return) + ) + (i32.store8 + (i32.add + (get_local $0) + (i32.const 3) + ) + (get_local $1) + ) + (i32.store8 + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 4) + ) + (get_local $1) + ) + (if + (i32.le_u + (get_local $2) + (i32.const 8) + ) + (return) + ) + (set_local $3 + (i32.and + (i32.sub + (i32.const 0) + (get_local $0) + ) + (i32.const 3) + ) + ) + (set_local $0 + (i32.add + (get_local $0) + (get_local $3) + ) + ) + (set_local $2 + (i32.sub + (get_local $2) + (get_local $3) + ) + ) + (set_local $2 + (i32.and + (get_local $2) + (i32.sub + (i32.const 0) + (i32.const 4) + ) + ) + ) + (set_local $4 + (i32.mul + (i32.div_u + (i32.sub + (i32.const 0) + (i32.const 1) + ) + (i32.const 255) + ) + (get_local $1) + ) + ) + (i32.store + (get_local $0) + (get_local $4) + ) + (i32.store + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 4) + ) + (get_local $4) + ) + (if + (i32.le_u + (get_local $2) + (i32.const 8) + ) + (return) + ) + (i32.store + (i32.add + (get_local $0) + (i32.const 4) + ) + (get_local $4) + ) + (i32.store + (i32.add + (get_local $0) + (i32.const 8) + ) + (get_local $4) + ) + (i32.store + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 12) + ) + (get_local $4) + ) + (i32.store + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 8) + ) + (get_local $4) + ) + (if + (i32.le_u + (get_local $2) + (i32.const 24) + ) + (return) + ) + (i32.store + (i32.add + (get_local $0) + (i32.const 12) + ) + (get_local $4) + ) + (i32.store + (i32.add + (get_local $0) + (i32.const 16) + ) + (get_local $4) + ) + (i32.store + (i32.add + (get_local $0) + (i32.const 20) + ) + (get_local $4) + ) + (i32.store + (i32.add + (get_local $0) + (i32.const 24) + ) + (get_local $4) + ) + (i32.store + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 28) + ) + (get_local $4) + ) + (i32.store + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 24) + ) + (get_local $4) + ) + (i32.store + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 20) + ) + (get_local $4) + ) + (i32.store + (i32.sub + (i32.add + (get_local $0) + (get_local $2) + ) + (i32.const 16) + ) + (get_local $4) + ) + (set_local $3 + (i32.add + (i32.const 24) + (i32.and + (get_local $0) + (i32.const 4) + ) + ) + ) + (set_local $0 + (i32.add + (get_local $0) + (get_local $3) + ) + ) + (set_local $2 + (i32.sub + (get_local $2) + (get_local $3) + ) + ) + (set_local $5 + (i64.or + (i64.extend_u/i32 + (get_local $4) + ) + (i64.shl + (i64.extend_u/i32 + (get_local $4) + ) + (i64.const 32) + ) + ) + ) + (block $break|0 + (loop $continue|0 + (if + (i32.ge_u + (get_local $2) + (i32.const 32) + ) + (block + (block + (i64.store + (get_local $0) + (get_local $5) + ) + (i64.store + (i32.add + (get_local $0) + (i32.const 8) + ) + (get_local $5) + ) + (i64.store + (i32.add + (get_local $0) + (i32.const 16) + ) + (get_local $5) + ) + (i64.store + (i32.add + (get_local $0) + (i32.const 24) + ) + (get_local $5) + ) + (set_local $2 + (i32.sub + (get_local $2) + (i32.const 32) + ) + ) + (set_local $0 + (i32.add + (get_local $0) + (i32.const 32) + ) + ) + ) + (br $continue|0) + ) + ) + ) + ) + ) (func $std:array/Array#shift (; 11 ;) (type $ii) (param $0 i32) (result i32) (local $1 i32) (if diff --git a/tests/compiler/std/heap.optimized.wast b/tests/compiler/std/heap.optimized.wast index b77a9696..084742b5 100644 --- a/tests/compiler/std/heap.optimized.wast +++ b/tests/compiler/std/heap.optimized.wast @@ -1,6 +1,6 @@ (module - (type $iiiv (func (param i32 i32 i32))) (type $ii (func (param i32) (result i32))) + (type $iiiv (func (param i32 i32 i32))) (type $iiii (func (param i32 i32 i32) (result i32))) (type $iv (func (param i32))) (type $v (func)) @@ -12,7 +12,90 @@ (memory $0 1) (export "memory" (memory $0)) (start $start) - (func $std:memory/set_memory (; 0 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) + (func $std:memory/arena/allocate_memory (; 0 ;) (type $ii) (param $0 i32) (result i32) + (local $1 i32) + (local $2 i32) + (local $3 i32) + (if + (i32.eqz + (get_local $0) + ) + (return + (i32.const 0) + ) + ) + (if + (i32.and + (if (result i32) + (i32.gt_u + (tee_local $0 + (i32.and + (i32.add + (i32.add + (tee_local $2 + (get_global $std:memory/arena/HEAP_OFFSET) + ) + (get_local $0) + ) + (i32.const 7) + ) + (i32.const -8) + ) + ) + (tee_local $1 + (i32.shl + (current_memory) + (i32.const 16) + ) + ) + ) + (i32.lt_s + (grow_memory + (select + (tee_local $3 + (i32.shr_u + (i32.sub + (i32.and + (i32.add + (get_local $0) + (i32.const 65535) + ) + (i32.const -65536) + ) + (get_local $1) + ) + (i32.const 16) + ) + ) + (tee_local $1 + (i32.shr_u + (get_local $1) + (i32.const 16) + ) + ) + (i32.gt_u + (get_local $3) + (get_local $1) + ) + ) + ) + (i32.const 0) + ) + (i32.gt_u + (get_local $0) + (get_local $1) + ) + ) + (i32.const 1) + ) + (unreachable) + ) + (set_global $std:memory/arena/HEAP_OFFSET + (get_local $0) + ) + (get_local $2) + ) + (func $std:memory/set_memory (; 1 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) (local $3 i64) (local $4 i32) (if @@ -345,100 +428,6 @@ ) ) ) - (func $std:memory/arena/allocate_memory (; 1 ;) (type $ii) (param $0 i32) (result i32) - (local $1 i32) - (local $2 i32) - (if - (i32.eqz - (get_local $0) - ) - (return - (i32.const 0) - ) - ) - (set_local $1 - (current_memory) - ) - (if - (i32.gt_u - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) - (get_local $0) - ) - (i32.shl - (get_local $1) - (i32.const 16) - ) - ) - (if - (i32.lt_s - (grow_memory - (select - (tee_local $2 - (i32.trunc_s/f64 - (f64.ceil - (f64.div - (f64.convert_u/i32 - (get_local $0) - ) - (f64.const 65536) - ) - ) - ) - ) - (tee_local $1 - (i32.sub - (i32.mul - (get_local $1) - (i32.const 2) - ) - (get_local $1) - ) - ) - (i32.gt_s - (get_local $2) - (get_local $1) - ) - ) - ) - (i32.const 0) - ) - (unreachable) - ) - ) - (set_local $1 - (get_global $std:memory/arena/HEAP_OFFSET) - ) - (if - (block (result i32) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) - (get_local $0) - ) - ) - (i32.and - (get_global $std:memory/arena/HEAP_OFFSET) - (i32.const 7) - ) - ) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (i32.or - (get_global $std:memory/arena/HEAP_OFFSET) - (i32.const 7) - ) - (i32.const 1) - ) - ) - ) - (call $std:memory/set_memory - (get_local $1) - (i32.const 0) - (get_local $0) - ) - (get_local $1) - ) (func $std:memory/copy_memory (; 2 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) (local $3 i32) (local $4 i32) @@ -2477,5 +2466,20 @@ (call $std:memory/arena/free_memory (get_global $std/heap/ptr2) ) + (set_global $std:memory/arena/HEAP_OFFSET + (get_global $HEAP_BASE) + ) + (set_global $std/heap/ptr1 + (call $std:memory/arena/allocate_memory + (i32.const 42) + ) + ) + (if + (i32.ne + (get_global $std/heap/ptr1) + (get_global $HEAP_BASE) + ) + (unreachable) + ) ) ) diff --git a/tests/compiler/std/heap.ts b/tests/compiler/std/heap.ts index 05a8c7a1..1bc48fb6 100644 --- a/tests/compiler/std/heap.ts +++ b/tests/compiler/std/heap.ts @@ -19,3 +19,8 @@ assert(compare_memory(ptr1, ptr2, size) == 0); free_memory(ptr1); free_memory(ptr2); + +// arena +clear_memory(); +ptr1 = allocate_memory(size); +assert(ptr1 == HEAP_BASE); diff --git a/tests/compiler/std/heap.wast b/tests/compiler/std/heap.wast index c8f9da39..2b37a614 100644 --- a/tests/compiler/std/heap.wast +++ b/tests/compiler/std/heap.wast @@ -1,7 +1,7 @@ (module (type $i (func (result i32))) - (type $iiiv (func (param i32 i32 i32))) (type $ii (func (param i32) (result i32))) + (type $iiiv (func (param i32 i32 i32))) (type $iiii (func (param i32 i32 i32) (result i32))) (type $iv (func (param i32))) (type $v (func)) @@ -17,7 +17,106 @@ (memory $0 1) (export "memory" (memory $0)) (start $start) - (func $std:memory/set_memory (; 0 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) + (func $std:memory/arena/allocate_memory (; 0 ;) (type $ii) (param $0 i32) (result i32) + (local $1 i32) + (local $2 i32) + (local $3 i32) + (local $4 i32) + (local $5 i32) + (if + (i32.eqz + (get_local $0) + ) + (return + (i32.const 0) + ) + ) + (set_local $1 + (get_global $std:memory/arena/HEAP_OFFSET) + ) + (set_local $2 + (i32.and + (i32.add + (i32.add + (get_local $1) + (get_local $0) + ) + (i32.const 7) + ) + (i32.xor + (i32.const 7) + (i32.const -1) + ) + ) + ) + (set_local $3 + (i32.shl + (current_memory) + (i32.const 16) + ) + ) + (if + (i32.and + (if (result i32) + (i32.ne + (i32.gt_u + (get_local $2) + (get_local $3) + ) + (i32.const 0) + ) + (i32.lt_s + (grow_memory + (select + (tee_local $4 + (i32.shr_u + (i32.sub + (i32.and + (i32.add + (get_local $2) + (i32.const 65535) + ) + (i32.xor + (i32.const 65535) + (i32.const -1) + ) + ) + (get_local $3) + ) + (i32.const 16) + ) + ) + (tee_local $5 + (i32.shr_u + (get_local $3) + (i32.const 16) + ) + ) + (i32.gt_u + (get_local $4) + (get_local $5) + ) + ) + ) + (i32.const 0) + ) + (i32.gt_u + (get_local $2) + (get_local $3) + ) + ) + (i32.const 1) + ) + (unreachable) + ) + (set_global $std:memory/arena/HEAP_OFFSET + (get_local $2) + ) + (return + (get_local $1) + ) + ) + (func $std:memory/set_memory (; 1 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) (local $3 i32) (local $4 i32) (local $5 i64) @@ -372,104 +471,6 @@ ) ) ) - (func $std:memory/arena/allocate_memory (; 1 ;) (type $ii) (param $0 i32) (result i32) - (local $1 i32) - (local $2 i32) - (local $3 i32) - (local $4 i32) - (if - (i32.eqz - (get_local $0) - ) - (return - (i32.const 0) - ) - ) - (set_local $1 - (current_memory) - ) - (if - (i32.gt_u - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) - (get_local $0) - ) - (i32.shl - (get_local $1) - (i32.const 16) - ) - ) - (if - (i32.lt_s - (grow_memory - (select - (tee_local $2 - (i32.trunc_s/f64 - (f64.ceil - (f64.div - (f64.convert_u/i32 - (get_local $0) - ) - (f64.const 65536) - ) - ) - ) - ) - (tee_local $3 - (i32.sub - (i32.mul - (get_local $1) - (i32.const 2) - ) - (get_local $1) - ) - ) - (i32.gt_s - (get_local $2) - (get_local $3) - ) - ) - ) - (i32.const 0) - ) - (unreachable) - ) - ) - (set_local $4 - (get_global $std:memory/arena/HEAP_OFFSET) - ) - (if - (i32.and - (block (result i32) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) - (get_local $0) - ) - ) - (get_global $std:memory/arena/HEAP_OFFSET) - ) - (i32.const 7) - ) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (i32.or - (get_global $std:memory/arena/HEAP_OFFSET) - (i32.const 7) - ) - (i32.const 1) - ) - ) - ) - (call $std:memory/set_memory - (get_local $4) - (i32.const 0) - (get_local $0) - ) - (return - (get_local $4) - ) - ) (func $std:memory/copy_memory (; 2 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) (local $3 i32) (local $4 i32) @@ -2667,7 +2668,12 @@ ) (func $std:memory/arena/free_memory (; 5 ;) (type $iv) (param $0 i32) ) - (func $start (; 6 ;) (type $v) + (func $std:memory/arena/clear_memory (; 6 ;) (type $v) + (set_global $std:memory/arena/HEAP_OFFSET + (get_global $HEAP_BASE) + ) + ) + (func $start (; 7 ;) (type $v) (set_global $std:memory/arena/HEAP_OFFSET (get_global $HEAP_BASE) ) @@ -2791,6 +2797,21 @@ (call $std:memory/arena/free_memory (get_global $std/heap/ptr2) ) + (call $std:memory/arena/clear_memory) + (set_global $std/heap/ptr1 + (call $std:memory/arena/allocate_memory + (i32.const 42) + ) + ) + (if + (i32.eqz + (i32.eq + (get_global $std/heap/ptr1) + (get_global $HEAP_BASE) + ) + ) + (unreachable) + ) ) ) (; diff --git a/tests/compiler/std/new.optimized.wast b/tests/compiler/std/new.optimized.wast index 097f32c7..0df5590f 100644 --- a/tests/compiler/std/new.optimized.wast +++ b/tests/compiler/std/new.optimized.wast @@ -1,5 +1,4 @@ (module - (type $iiiv (func (param i32 i32 i32))) (type $ii (func (param i32) (result i32))) (type $v (func)) (global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0)) @@ -8,342 +7,10 @@ (memory $0 1) (export "memory" (memory $0)) (start $start) - (func $std:memory/set_memory (; 0 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) - (local $3 i64) - (local $4 i32) - (if - (i32.eqz - (get_local $2) - ) - (return) - ) - (i32.store8 - (get_local $0) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 1) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 2) - ) - (return) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 1) - ) - (get_local $1) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 2) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 2) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 3) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 6) - ) - (return) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 3) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 4) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 8) - ) - (return) - ) - (i32.store - (tee_local $0 - (i32.add - (get_local $0) - (tee_local $4 - (i32.and - (i32.sub - (i32.const 0) - (get_local $0) - ) - (i32.const 3) - ) - ) - ) - ) - (tee_local $1 - (i32.mul - (get_local $1) - (i32.const 16843009) - ) - ) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (tee_local $2 - (i32.and - (i32.sub - (get_local $2) - (get_local $4) - ) - (i32.const -4) - ) - ) - ) - (i32.const 4) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 8) - ) - (return) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 4) - ) - (get_local $1) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 8) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 12) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 8) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 24) - ) - (return) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 12) - ) - (get_local $1) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 16) - ) - (get_local $1) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 20) - ) - (get_local $1) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 24) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 28) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 24) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 20) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 16) - ) - (get_local $1) - ) - (set_local $0 - (i32.add - (get_local $0) - (tee_local $4 - (i32.add - (i32.and - (get_local $0) - (i32.const 4) - ) - (i32.const 24) - ) - ) - ) - ) - (set_local $2 - (i32.sub - (get_local $2) - (get_local $4) - ) - ) - (set_local $3 - (i64.or - (i64.extend_u/i32 - (get_local $1) - ) - (i64.shl - (i64.extend_u/i32 - (get_local $1) - ) - (i64.const 32) - ) - ) - ) - (loop $continue|0 - (if - (i32.ge_u - (get_local $2) - (i32.const 32) - ) - (block - (i64.store - (get_local $0) - (get_local $3) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 8) - ) - (get_local $3) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 16) - ) - (get_local $3) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 24) - ) - (get_local $3) - ) - (set_local $2 - (i32.sub - (get_local $2) - (i32.const 32) - ) - ) - (set_local $0 - (i32.add - (get_local $0) - (i32.const 32) - ) - ) - (br $continue|0) - ) - ) - ) - ) - (func $std:memory/arena/allocate_memory (; 1 ;) (type $ii) (param $0 i32) (result i32) + (func $std:memory/arena/allocate_memory (; 0 ;) (type $ii) (param $0 i32) (result i32) (local $1 i32) (local $2 i32) + (local $3 i32) (if (i32.eqz (get_local $0) @@ -352,90 +19,78 @@ (i32.const 0) ) ) - (set_local $1 - (current_memory) - ) (if - (i32.gt_u - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) - (get_local $0) - ) - (i32.shl - (get_local $1) - (i32.const 16) - ) - ) - (if - (i32.lt_s - (grow_memory - (select - (tee_local $2 - (i32.trunc_s/f64 - (f64.ceil - (f64.div - (f64.convert_u/i32 - (get_local $0) - ) - (f64.const 65536) + (i32.and + (if (result i32) + (i32.gt_u + (tee_local $0 + (i32.and + (i32.add + (i32.add + (tee_local $2 + (get_global $std:memory/arena/HEAP_OFFSET) ) + (get_local $0) ) + (i32.const 7) ) + (i32.const -8) ) - (tee_local $1 - (i32.sub - (i32.mul - (get_local $1) - (i32.const 2) - ) - (get_local $1) - ) - ) - (i32.gt_s - (get_local $2) - (get_local $1) + ) + (tee_local $1 + (i32.shl + (current_memory) + (i32.const 16) ) ) ) - (i32.const 0) - ) - (unreachable) - ) - ) - (set_local $1 - (get_global $std:memory/arena/HEAP_OFFSET) - ) - (if - (block (result i32) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) + (i32.lt_s + (grow_memory + (select + (tee_local $3 + (i32.shr_u + (i32.sub + (i32.and + (i32.add + (get_local $0) + (i32.const 65535) + ) + (i32.const -65536) + ) + (get_local $1) + ) + (i32.const 16) + ) + ) + (tee_local $1 + (i32.shr_u + (get_local $1) + (i32.const 16) + ) + ) + (i32.gt_u + (get_local $3) + (get_local $1) + ) + ) + ) + (i32.const 0) + ) + (i32.gt_u (get_local $0) + (get_local $1) ) ) - (i32.and - (get_global $std:memory/arena/HEAP_OFFSET) - (i32.const 7) - ) - ) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (i32.or - (get_global $std:memory/arena/HEAP_OFFSET) - (i32.const 7) - ) - (i32.const 1) - ) + (i32.const 1) ) + (unreachable) ) - (call $std:memory/set_memory - (get_local $1) - (i32.const 0) + (set_global $std:memory/arena/HEAP_OFFSET (get_local $0) ) - (get_local $1) + (get_local $2) ) - (func $start (; 2 ;) (type $v) + (func $start (; 1 ;) (type $v) (local $0 i32) (local $1 i32) (set_global $std:memory/arena/HEAP_OFFSET diff --git a/tests/compiler/std/new.wast b/tests/compiler/std/new.wast index 77f99f11..c41f8fdb 100644 --- a/tests/compiler/std/new.wast +++ b/tests/compiler/std/new.wast @@ -1,6 +1,5 @@ (module (type $i (func (result i32))) - (type $iiiv (func (param i32 i32 i32))) (type $ii (func (param i32) (result i32))) (type $ifv (func (param i32 f32))) (type $v (func)) @@ -13,366 +12,12 @@ (memory $0 1) (export "memory" (memory $0)) (start $start) - (func $std:memory/set_memory (; 0 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) - (local $3 i32) - (local $4 i32) - (local $5 i64) - (if - (i32.eqz - (get_local $2) - ) - (return) - ) - (i32.store8 - (get_local $0) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 1) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 2) - ) - (return) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 1) - ) - (get_local $1) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 2) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 2) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 3) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 6) - ) - (return) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 3) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 4) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 8) - ) - (return) - ) - (set_local $3 - (i32.and - (i32.sub - (i32.const 0) - (get_local $0) - ) - (i32.const 3) - ) - ) - (set_local $0 - (i32.add - (get_local $0) - (get_local $3) - ) - ) - (set_local $2 - (i32.sub - (get_local $2) - (get_local $3) - ) - ) - (set_local $2 - (i32.and - (get_local $2) - (i32.sub - (i32.const 0) - (i32.const 4) - ) - ) - ) - (set_local $4 - (i32.mul - (i32.div_u - (i32.sub - (i32.const 0) - (i32.const 1) - ) - (i32.const 255) - ) - (get_local $1) - ) - ) - (i32.store - (get_local $0) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 4) - ) - (get_local $4) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 8) - ) - (return) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 4) - ) - (get_local $4) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 8) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 12) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 8) - ) - (get_local $4) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 24) - ) - (return) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 12) - ) - (get_local $4) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 16) - ) - (get_local $4) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 20) - ) - (get_local $4) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 24) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 28) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 24) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 20) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 16) - ) - (get_local $4) - ) - (set_local $3 - (i32.add - (i32.const 24) - (i32.and - (get_local $0) - (i32.const 4) - ) - ) - ) - (set_local $0 - (i32.add - (get_local $0) - (get_local $3) - ) - ) - (set_local $2 - (i32.sub - (get_local $2) - (get_local $3) - ) - ) - (set_local $5 - (i64.or - (i64.extend_u/i32 - (get_local $4) - ) - (i64.shl - (i64.extend_u/i32 - (get_local $4) - ) - (i64.const 32) - ) - ) - ) - (block $break|0 - (loop $continue|0 - (if - (i32.ge_u - (get_local $2) - (i32.const 32) - ) - (block - (block - (i64.store - (get_local $0) - (get_local $5) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 8) - ) - (get_local $5) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 16) - ) - (get_local $5) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 24) - ) - (get_local $5) - ) - (set_local $2 - (i32.sub - (get_local $2) - (i32.const 32) - ) - ) - (set_local $0 - (i32.add - (get_local $0) - (i32.const 32) - ) - ) - ) - (br $continue|0) - ) - ) - ) - ) - ) - (func $std:memory/arena/allocate_memory (; 1 ;) (type $ii) (param $0 i32) (result i32) + (func $std:memory/arena/allocate_memory (; 0 ;) (type $ii) (param $0 i32) (result i32) (local $1 i32) (local $2 i32) (local $3 i32) (local $4 i32) + (local $5 i32) (if (i32.eqz (get_local $0) @@ -382,91 +27,91 @@ ) ) (set_local $1 - (current_memory) - ) - (if - (i32.gt_u - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) - (get_local $0) - ) - (i32.shl - (get_local $1) - (i32.const 16) - ) - ) - (if - (i32.lt_s - (grow_memory - (select - (tee_local $2 - (i32.trunc_s/f64 - (f64.ceil - (f64.div - (f64.convert_u/i32 - (get_local $0) - ) - (f64.const 65536) - ) - ) - ) - ) - (tee_local $3 - (i32.sub - (i32.mul - (get_local $1) - (i32.const 2) - ) - (get_local $1) - ) - ) - (i32.gt_s - (get_local $2) - (get_local $3) - ) - ) - ) - (i32.const 0) - ) - (unreachable) - ) - ) - (set_local $4 (get_global $std:memory/arena/HEAP_OFFSET) ) + (set_local $2 + (i32.and + (i32.add + (i32.add + (get_local $1) + (get_local $0) + ) + (i32.const 7) + ) + (i32.xor + (i32.const 7) + (i32.const -1) + ) + ) + ) + (set_local $3 + (i32.shl + (current_memory) + (i32.const 16) + ) + ) (if (i32.and - (block (result i32) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) - (get_local $0) + (if (result i32) + (i32.ne + (i32.gt_u + (get_local $2) + (get_local $3) ) + (i32.const 0) ) - (get_global $std:memory/arena/HEAP_OFFSET) - ) - (i32.const 7) - ) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (i32.or - (get_global $std:memory/arena/HEAP_OFFSET) - (i32.const 7) + (i32.lt_s + (grow_memory + (select + (tee_local $4 + (i32.shr_u + (i32.sub + (i32.and + (i32.add + (get_local $2) + (i32.const 65535) + ) + (i32.xor + (i32.const 65535) + (i32.const -1) + ) + ) + (get_local $3) + ) + (i32.const 16) + ) + ) + (tee_local $5 + (i32.shr_u + (get_local $3) + (i32.const 16) + ) + ) + (i32.gt_u + (get_local $4) + (get_local $5) + ) + ) + ) + (i32.const 0) + ) + (i32.gt_u + (get_local $2) + (get_local $3) ) - (i32.const 1) ) + (i32.const 1) ) + (unreachable) ) - (call $std:memory/set_memory - (get_local $4) - (i32.const 0) - (get_local $0) + (set_global $std:memory/arena/HEAP_OFFSET + (get_local $2) ) (return - (get_local $4) + (get_local $1) ) ) - (func $std/new/AClass#constructor (; 2 ;) (type $ifv) (param $0 i32) (param $1 f32) + (func $std/new/AClass#constructor (; 1 ;) (type $ifv) (param $0 i32) (param $1 f32) (i32.store (get_local $0) (i32.add @@ -481,7 +126,7 @@ (get_local $1) ) ) - (func $start (; 3 ;) (type $v) + (func $start (; 2 ;) (type $v) (local $0 i32) (set_global $std:memory/arena/HEAP_OFFSET (get_global $HEAP_BASE) diff --git a/tests/compiler/std/set.optimized.wast b/tests/compiler/std/set.optimized.wast index d422e7a3..187bad0c 100644 --- a/tests/compiler/std/set.optimized.wast +++ b/tests/compiler/std/set.optimized.wast @@ -1,6 +1,6 @@ (module - (type $iiiv (func (param i32 i32 i32))) (type $ii (func (param i32) (result i32))) + (type $iiiv (func (param i32 i32 i32))) (type $iii (func (param i32 i32) (result i32))) (type $v (func)) (global $std:memory/arena/HEAP_OFFSET (mut i32) (i32.const 0)) @@ -9,342 +9,10 @@ (memory $0 1) (export "memory" (memory $0)) (start $start) - (func $std:memory/set_memory (; 0 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) - (local $3 i64) - (local $4 i32) - (if - (i32.eqz - (get_local $2) - ) - (return) - ) - (i32.store8 - (get_local $0) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 1) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 2) - ) - (return) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 1) - ) - (get_local $1) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 2) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 2) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 3) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 6) - ) - (return) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 3) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 4) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 8) - ) - (return) - ) - (i32.store - (tee_local $0 - (i32.add - (get_local $0) - (tee_local $4 - (i32.and - (i32.sub - (i32.const 0) - (get_local $0) - ) - (i32.const 3) - ) - ) - ) - ) - (tee_local $1 - (i32.mul - (get_local $1) - (i32.const 16843009) - ) - ) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (tee_local $2 - (i32.and - (i32.sub - (get_local $2) - (get_local $4) - ) - (i32.const -4) - ) - ) - ) - (i32.const 4) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 8) - ) - (return) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 4) - ) - (get_local $1) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 8) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 12) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 8) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 24) - ) - (return) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 12) - ) - (get_local $1) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 16) - ) - (get_local $1) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 20) - ) - (get_local $1) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 24) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 28) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 24) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 20) - ) - (get_local $1) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 16) - ) - (get_local $1) - ) - (set_local $0 - (i32.add - (get_local $0) - (tee_local $4 - (i32.add - (i32.and - (get_local $0) - (i32.const 4) - ) - (i32.const 24) - ) - ) - ) - ) - (set_local $2 - (i32.sub - (get_local $2) - (get_local $4) - ) - ) - (set_local $3 - (i64.or - (i64.extend_u/i32 - (get_local $1) - ) - (i64.shl - (i64.extend_u/i32 - (get_local $1) - ) - (i64.const 32) - ) - ) - ) - (loop $continue|0 - (if - (i32.ge_u - (get_local $2) - (i32.const 32) - ) - (block - (i64.store - (get_local $0) - (get_local $3) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 8) - ) - (get_local $3) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 16) - ) - (get_local $3) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 24) - ) - (get_local $3) - ) - (set_local $2 - (i32.sub - (get_local $2) - (i32.const 32) - ) - ) - (set_local $0 - (i32.add - (get_local $0) - (i32.const 32) - ) - ) - (br $continue|0) - ) - ) - ) - ) - (func $std:memory/arena/allocate_memory (; 1 ;) (type $ii) (param $0 i32) (result i32) + (func $std:memory/arena/allocate_memory (; 0 ;) (type $ii) (param $0 i32) (result i32) (local $1 i32) (local $2 i32) + (local $3 i32) (if (i32.eqz (get_local $0) @@ -353,95 +21,83 @@ (i32.const 0) ) ) - (set_local $1 - (current_memory) - ) (if - (i32.gt_u - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) - (get_local $0) - ) - (i32.shl - (get_local $1) - (i32.const 16) - ) - ) - (if - (i32.lt_s - (grow_memory - (select - (tee_local $2 - (i32.trunc_s/f64 - (f64.ceil - (f64.div - (f64.convert_u/i32 - (get_local $0) - ) - (f64.const 65536) + (i32.and + (if (result i32) + (i32.gt_u + (tee_local $0 + (i32.and + (i32.add + (i32.add + (tee_local $2 + (get_global $std:memory/arena/HEAP_OFFSET) ) + (get_local $0) ) + (i32.const 7) ) + (i32.const -8) ) - (tee_local $1 - (i32.sub - (i32.mul - (get_local $1) - (i32.const 2) - ) - (get_local $1) - ) - ) - (i32.gt_s - (get_local $2) - (get_local $1) + ) + (tee_local $1 + (i32.shl + (current_memory) + (i32.const 16) ) ) ) - (i32.const 0) - ) - (unreachable) - ) - ) - (set_local $1 - (get_global $std:memory/arena/HEAP_OFFSET) - ) - (if - (block (result i32) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) + (i32.lt_s + (grow_memory + (select + (tee_local $3 + (i32.shr_u + (i32.sub + (i32.and + (i32.add + (get_local $0) + (i32.const 65535) + ) + (i32.const -65536) + ) + (get_local $1) + ) + (i32.const 16) + ) + ) + (tee_local $1 + (i32.shr_u + (get_local $1) + (i32.const 16) + ) + ) + (i32.gt_u + (get_local $3) + (get_local $1) + ) + ) + ) + (i32.const 0) + ) + (i32.gt_u (get_local $0) + (get_local $1) ) ) - (i32.and - (get_global $std:memory/arena/HEAP_OFFSET) - (i32.const 7) - ) - ) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (i32.or - (get_global $std:memory/arena/HEAP_OFFSET) - (i32.const 7) - ) - (i32.const 1) - ) + (i32.const 1) ) + (unreachable) ) - (call $std:memory/set_memory - (get_local $1) - (i32.const 0) + (set_global $std:memory/arena/HEAP_OFFSET (get_local $0) ) - (get_local $1) + (get_local $2) ) - (func $std:set/Set#get:size (; 2 ;) (type $ii) (param $0 i32) (result i32) + (func $std:set/Set#get:size (; 1 ;) (type $ii) (param $0 i32) (result i32) (i32.load offset=8 (get_local $0) ) ) - (func $std:memory/copy_memory (; 3 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) + (func $std:memory/copy_memory (; 2 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) (local $3 i32) (local $4 i32) (loop $continue|0 @@ -2017,7 +1673,7 @@ ) ) ) - (func $std:memory/move_memory (; 4 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) + (func $std:memory/move_memory (; 3 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) (local $3 i32) (if (i32.eq @@ -2307,7 +1963,7 @@ ) ) ) - (func $std:set/Set#add (; 5 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) + (func $std:set/Set#add (; 4 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (local $2 i32) (local $3 i32) (if @@ -2409,7 +2065,7 @@ ) (get_local $0) ) - (func $std:set/Set#has (; 6 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) + (func $std:set/Set#has (; 5 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (local $2 i32) (local $3 i32) (if @@ -2461,7 +2117,7 @@ ) (i32.const 0) ) - (func $std:set/Set#delete (; 7 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) + (func $std:set/Set#delete (; 6 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (local $2 i32) (local $3 i32) (if @@ -2567,7 +2223,7 @@ ) (i32.const 0) ) - (func $start (; 8 ;) (type $v) + (func $start (; 7 ;) (type $v) (local $0 i32) (set_global $std:memory/arena/HEAP_OFFSET (get_global $HEAP_BASE) diff --git a/tests/compiler/std/set.wast b/tests/compiler/std/set.wast index f81d864d..3208097e 100644 --- a/tests/compiler/std/set.wast +++ b/tests/compiler/std/set.wast @@ -1,7 +1,7 @@ (module (type $i (func (result i32))) - (type $iiiv (func (param i32 i32 i32))) (type $ii (func (param i32) (result i32))) + (type $iiiv (func (param i32 i32 i32))) (type $iv (func (param i32))) (type $iii (func (param i32 i32) (result i32))) (type $v (func)) @@ -14,366 +14,12 @@ (memory $0 1) (export "memory" (memory $0)) (start $start) - (func $std:memory/set_memory (; 0 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) - (local $3 i32) - (local $4 i32) - (local $5 i64) - (if - (i32.eqz - (get_local $2) - ) - (return) - ) - (i32.store8 - (get_local $0) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 1) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 2) - ) - (return) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 1) - ) - (get_local $1) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 2) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 2) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 3) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 6) - ) - (return) - ) - (i32.store8 - (i32.add - (get_local $0) - (i32.const 3) - ) - (get_local $1) - ) - (i32.store8 - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 4) - ) - (get_local $1) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 8) - ) - (return) - ) - (set_local $3 - (i32.and - (i32.sub - (i32.const 0) - (get_local $0) - ) - (i32.const 3) - ) - ) - (set_local $0 - (i32.add - (get_local $0) - (get_local $3) - ) - ) - (set_local $2 - (i32.sub - (get_local $2) - (get_local $3) - ) - ) - (set_local $2 - (i32.and - (get_local $2) - (i32.sub - (i32.const 0) - (i32.const 4) - ) - ) - ) - (set_local $4 - (i32.mul - (i32.div_u - (i32.sub - (i32.const 0) - (i32.const 1) - ) - (i32.const 255) - ) - (get_local $1) - ) - ) - (i32.store - (get_local $0) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 4) - ) - (get_local $4) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 8) - ) - (return) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 4) - ) - (get_local $4) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 8) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 12) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 8) - ) - (get_local $4) - ) - (if - (i32.le_u - (get_local $2) - (i32.const 24) - ) - (return) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 12) - ) - (get_local $4) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 16) - ) - (get_local $4) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 20) - ) - (get_local $4) - ) - (i32.store - (i32.add - (get_local $0) - (i32.const 24) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 28) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 24) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 20) - ) - (get_local $4) - ) - (i32.store - (i32.sub - (i32.add - (get_local $0) - (get_local $2) - ) - (i32.const 16) - ) - (get_local $4) - ) - (set_local $3 - (i32.add - (i32.const 24) - (i32.and - (get_local $0) - (i32.const 4) - ) - ) - ) - (set_local $0 - (i32.add - (get_local $0) - (get_local $3) - ) - ) - (set_local $2 - (i32.sub - (get_local $2) - (get_local $3) - ) - ) - (set_local $5 - (i64.or - (i64.extend_u/i32 - (get_local $4) - ) - (i64.shl - (i64.extend_u/i32 - (get_local $4) - ) - (i64.const 32) - ) - ) - ) - (block $break|0 - (loop $continue|0 - (if - (i32.ge_u - (get_local $2) - (i32.const 32) - ) - (block - (block - (i64.store - (get_local $0) - (get_local $5) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 8) - ) - (get_local $5) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 16) - ) - (get_local $5) - ) - (i64.store - (i32.add - (get_local $0) - (i32.const 24) - ) - (get_local $5) - ) - (set_local $2 - (i32.sub - (get_local $2) - (i32.const 32) - ) - ) - (set_local $0 - (i32.add - (get_local $0) - (i32.const 32) - ) - ) - ) - (br $continue|0) - ) - ) - ) - ) - ) - (func $std:memory/arena/allocate_memory (; 1 ;) (type $ii) (param $0 i32) (result i32) + (func $std:memory/arena/allocate_memory (; 0 ;) (type $ii) (param $0 i32) (result i32) (local $1 i32) (local $2 i32) (local $3 i32) (local $4 i32) + (local $5 i32) (if (i32.eqz (get_local $0) @@ -383,98 +29,98 @@ ) ) (set_local $1 - (current_memory) - ) - (if - (i32.gt_u - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) - (get_local $0) - ) - (i32.shl - (get_local $1) - (i32.const 16) - ) - ) - (if - (i32.lt_s - (grow_memory - (select - (tee_local $2 - (i32.trunc_s/f64 - (f64.ceil - (f64.div - (f64.convert_u/i32 - (get_local $0) - ) - (f64.const 65536) - ) - ) - ) - ) - (tee_local $3 - (i32.sub - (i32.mul - (get_local $1) - (i32.const 2) - ) - (get_local $1) - ) - ) - (i32.gt_s - (get_local $2) - (get_local $3) - ) - ) - ) - (i32.const 0) - ) - (unreachable) - ) - ) - (set_local $4 (get_global $std:memory/arena/HEAP_OFFSET) ) + (set_local $2 + (i32.and + (i32.add + (i32.add + (get_local $1) + (get_local $0) + ) + (i32.const 7) + ) + (i32.xor + (i32.const 7) + (i32.const -1) + ) + ) + ) + (set_local $3 + (i32.shl + (current_memory) + (i32.const 16) + ) + ) (if (i32.and - (block (result i32) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (get_global $std:memory/arena/HEAP_OFFSET) - (get_local $0) + (if (result i32) + (i32.ne + (i32.gt_u + (get_local $2) + (get_local $3) ) + (i32.const 0) ) - (get_global $std:memory/arena/HEAP_OFFSET) - ) - (i32.const 7) - ) - (set_global $std:memory/arena/HEAP_OFFSET - (i32.add - (i32.or - (get_global $std:memory/arena/HEAP_OFFSET) - (i32.const 7) + (i32.lt_s + (grow_memory + (select + (tee_local $4 + (i32.shr_u + (i32.sub + (i32.and + (i32.add + (get_local $2) + (i32.const 65535) + ) + (i32.xor + (i32.const 65535) + (i32.const -1) + ) + ) + (get_local $3) + ) + (i32.const 16) + ) + ) + (tee_local $5 + (i32.shr_u + (get_local $3) + (i32.const 16) + ) + ) + (i32.gt_u + (get_local $4) + (get_local $5) + ) + ) + ) + (i32.const 0) + ) + (i32.gt_u + (get_local $2) + (get_local $3) ) - (i32.const 1) ) + (i32.const 1) ) + (unreachable) ) - (call $std:memory/set_memory - (get_local $4) - (i32.const 0) - (get_local $0) + (set_global $std:memory/arena/HEAP_OFFSET + (get_local $2) ) (return - (get_local $4) + (get_local $1) ) ) - (func $std:set/Set#get:size (; 2 ;) (type $ii) (param $0 i32) (result i32) + (func $std:set/Set#get:size (; 1 ;) (type $ii) (param $0 i32) (result i32) (return (i32.load offset=8 (get_local $0) ) ) ) - (func $std:memory/copy_memory (; 3 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) + (func $std:memory/copy_memory (; 2 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) (local $3 i32) (local $4 i32) (local $5 i32) @@ -2274,7 +1920,7 @@ ) ) ) - (func $std:memory/move_memory (; 4 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) + (func $std:memory/move_memory (; 3 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32) (local $3 i32) (if (i32.eq @@ -2600,9 +2246,9 @@ ) ) ) - (func $std:memory/arena/free_memory (; 5 ;) (type $iv) (param $0 i32) + (func $std:memory/arena/free_memory (; 4 ;) (type $iv) (param $0 i32) ) - (func $std:set/Set#add (; 6 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) + (func $std:set/Set#add (; 5 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (local $2 i32) (local $3 i32) (local $4 i32) @@ -2714,7 +2360,7 @@ (get_local $0) ) ) - (func $std:set/Set#has (; 7 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) + (func $std:set/Set#has (; 6 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (local $2 i32) (local $3 i32) (if @@ -2778,7 +2424,7 @@ (i32.const 0) ) ) - (func $std:set/Set#delete (; 8 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) + (func $std:set/Set#delete (; 7 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (local $2 i32) (local $3 i32) (if @@ -2896,7 +2542,7 @@ (i32.const 0) ) ) - (func $std:set/Set#clear (; 9 ;) (type $iv) (param $0 i32) + (func $std:set/Set#clear (; 8 ;) (type $iv) (param $0 i32) (if (i32.eqz (i32.ne @@ -2911,7 +2557,7 @@ (i32.const 0) ) ) - (func $start (; 10 ;) (type $v) + (func $start (; 9 ;) (type $v) (set_global $std:memory/arena/HEAP_OFFSET (get_global $HEAP_BASE) )