crunch functions

seems useful to keep the number of runtime functions in a user's binary to a minimum
This commit is contained in:
dcode 2019-04-16 19:15:08 +02:00
parent 3e08e5c2d6
commit 0ba0d8ae7a
3 changed files with 777 additions and 630 deletions

View File

@ -15,6 +15,23 @@
/////////////////////// The TLSF (Two-Level Segregate Fit) memory allocator ///////////////////////
// see: http://www.gii.upv.es/tlsf/
/** Determines the first (LSB to MSB) set bit's index of a word. */
// @ts-ignore: decorator
@inline
function ffs<T extends number>(word: T): T {
return ctz<T>(word); // for word != 0
}
/** Determines the last (LSB to MSB) set bit's index of a word. */
// @ts-ignore: decorator
@inline
function fls<T extends number>(word: T): T {
// @ts-ignore: type
const inv: T = sizeof<T>() * 8 - 1;
// @ts-ignore: type
return inv - clz<T>(word);
}
// ╒══════════════ Block size interpretation (32-bit) ═════════════╕
// 3 2 1
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits
@ -34,8 +51,7 @@
@inline const SB_SIZE: usize = 1 << <usize>SB_BITS;
// @ts-ignore: decorator
@inline
const FL_BITS: u32 = 31 - SB_BITS;
@inline const FL_BITS: u32 = 31 - SB_BITS;
// [00]: < 256B (SB) [12]: < 1M
// [01]: < 512B [13]: < 2M
@ -109,20 +125,17 @@ const FL_BITS: u32 = 31 - SB_BITS;
@inline const BLOCK_MAXSIZE: usize = 1 << (FL_BITS + SB_BITS - 1); // exclusive
/** Gets the left block of a block. Only valid if the left block is free. */
// @ts-ignore: decorator
@inline
function getLeft(block: Block): Block {
if (DEBUG) assert(block.mmInfo & LEFTFREE); // left must be free or it doesn't contain 'back'
var left = load<Block>(changetype<usize>(block) - sizeof<usize>());
if (DEBUG) assert(left);
return left;
return load<Block>(changetype<usize>(block) - sizeof<usize>());
}
/** Gets the right block of of a block by advancing to the right by its size. */
// @ts-ignore: decorator
@inline
function getRight(block: Block): Block {
var mmInfo = block.mmInfo;
if (DEBUG) assert(mmInfo & ~TAGS_MASK); // can't skip beyond the tail block (the only valid empty block)
var right = changetype<Block>(changetype<usize>(block) + BLOCK_OVERHEAD + (mmInfo & ~TAGS_MASK));
if (DEBUG) assert(right);
return right;
return changetype<Block>(changetype<usize>(block) + BLOCK_OVERHEAD + (block.mmInfo & ~TAGS_MASK));
}
// ╒═════════════════════ Root layout (32-bit) ════════════════════╕
@ -166,18 +179,21 @@ function getRight(block: Block): Block {
var ROOT: Root;
// @ts-ignore: decorator
@inline
function getSLMap(root: Root, fl: usize): u32 {
if (DEBUG) assert(fl < FL_BITS); // fl out of range
return load<u32>(changetype<usize>(root) + (fl << alignof<u32>()), SL_START);
}
// @ts-ignore: decorator
@inline
function setSLMap(root: Root, fl: usize, value: u32): void {
if (DEBUG) assert(fl < FL_BITS); // fl out of range
store<u32>(changetype<usize>(root) + (fl << alignof<u32>()), value, SL_START);
}
// @ts-ignore: decorator
@inline
function getHead(root: Root, fl: usize, sl: u32): Block | null {
if (DEBUG) assert(fl < FL_BITS && sl < SL_SIZE); // fl/sl out of range
return changetype<Block>(
load<usize>(
changetype<usize>(root) + (fl * SL_SIZE + <usize>sl) * sizeof<usize>(),
@ -185,17 +201,22 @@ function getHead(root: Root, fl: usize, sl: u32): Block | null {
);
}
// @ts-ignore: decorator
@inline
function setHead(root: Root, fl: usize, sl: u32, value: Block | null): void {
if (DEBUG) assert(fl < FL_BITS && sl < SL_SIZE); // fl/sl out of range
store<usize>(
changetype<usize>(root) + (fl * SL_SIZE + <usize>sl) * sizeof<usize>() , changetype<usize>(value),
HL_START);
}
// @ts-ignore: decorator
@inline
function getTail(root: Root): Block {
return load<Block>(changetype<usize>(root), HL_END);
}
// @ts-ignore: decorator
@inline
function setTail(root: Root, tail: Block): void {
store<Block>(changetype<usize>(root), tail, HL_END);
}
@ -256,6 +277,7 @@ function insertBlock(root: Root, block: Block): void {
sl = <u32>((size >> (fl - SL_BITS)) ^ (1 << SL_BITS));
fl -= SB_BITS - 1;
}
if (DEBUG) assert(fl < FL_BITS && sl < SL_SIZE); // fl/sl out of range
// perform insertion
var head = getHead(root, fl, sl);
@ -286,6 +308,7 @@ function removeBlock(root: Root, block: Block): void {
sl = <u32>((size >> (fl - SL_BITS)) ^ (1 << SL_BITS));
fl -= SB_BITS - 1;
}
if (DEBUG) assert(fl < FL_BITS && sl < SL_SIZE); // fl/sl out of range
// link previous and next free block
var prev = block.prev;
@ -329,6 +352,7 @@ function searchBlock(root: Root, size: usize): Block | null {
sl = <u32>((requestSize >> (fl - SL_BITS)) ^ (1 << SL_BITS));
fl -= SB_BITS - 1;
}
if (DEBUG) assert(fl < FL_BITS && sl < SL_SIZE); // fl/sl out of range
// search second level
var slMap = getSLMap(root, fl) & (~0 << sl);
@ -470,21 +494,6 @@ function freeBlock(root: Root, block: Block): void {
insertBlock(root, block);
}
/** Determines the first (LSB to MSB) set bit's index of a word. */
function ffs<T extends number>(word: T): T {
if (DEBUG) assert(word != 0); // word cannot be 0
return ctz<T>(word); // differs from ffs only for 0
}
/** Determines the last (LSB to MSB) set bit's index of a word. */
function fls<T extends number>(word: T): T {
if (DEBUG) assert(word != 0); // word cannot be 0
// @ts-ignore: type
const inv: T = (sizeof<T>() << 3) - 1;
// @ts-ignore: type
return inv - clz<T>(word);
}
// Memory manager interface.
// @ts-ignore: decorator

View File

@ -1,13 +1,13 @@
(module
(type $FUNCSIG$ii (func (param i32) (result i32)))
(type $FUNCSIG$i (func (result i32)))
(type $FUNCSIG$vii (func (param i32 i32)))
(type $FUNCSIG$viii (func (param i32 i32 i32)))
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
(type $FUNCSIG$iiii (func (param i32 i32 i32) (result i32)))
(type $FUNCSIG$vii (func (param i32 i32)))
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
(type $FUNCSIG$iii (func (param i32 i32) (result i32)))
(type $FUNCSIG$vi (func (param i32)))
(type $FUNCSIG$v (func))
(type $FUNCSIG$viii (func (param i32 i32 i32)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(memory $0 1)
(data (i32.const 8) "\10\00\00\00\"")
@ -24,70 +24,7 @@
(export "__gc_retain" (func $assembly/index/__gc_retain))
(export "__gc_release" (func $assembly/index/__gc_release))
(export "__gc_collect" (func $assembly/index/collectCycles))
(func $assembly/index/setTail (; 1 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
local.get $0
local.get $1
i32.store offset=1568
)
(func $assembly/index/setSLMap (; 2 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32)
local.get $0
local.get $1
i32.const 2
i32.shl
i32.add
local.get $2
i32.store offset=4
)
(func $assembly/index/setHead (; 3 ;) (type $FUNCSIG$viiii) (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32)
local.get $0
local.get $1
i32.const 4
i32.shl
local.get $2
i32.add
i32.const 2
i32.shl
i32.add
local.get $3
i32.store offset=96
)
(func $assembly/index/getRight (; 4 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
local.get $0
i32.const 16
i32.add
local.get $0
i32.load
i32.const -4
i32.and
i32.add
)
(func $assembly/index/fls<usize> (; 5 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
i32.const 31
local.get $0
i32.clz
i32.sub
)
(func $assembly/index/getHead (; 6 ;) (type $FUNCSIG$iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
local.get $0
local.get $1
i32.const 4
i32.shl
local.get $2
i32.add
i32.const 2
i32.shl
i32.add
i32.load offset=96
)
(func $assembly/index/getSLMap (; 7 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
local.get $0
local.get $1
i32.const 2
i32.shl
i32.add
i32.load offset=4
)
(func $assembly/index/removeBlock (; 8 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
(func $assembly/index/removeBlock (; 1 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
@ -107,8 +44,10 @@
i32.const 0
else
local.get $2
i32.const 31
local.get $2
call $assembly/index/fls<usize>
i32.clz
i32.sub
local.tee $3
i32.const 4
i32.sub
@ -138,26 +77,44 @@
local.get $5
i32.store offset=16
end
local.get $1
local.get $0
local.get $3
i32.const 4
i32.shl
local.get $4
call $assembly/index/getHead
local.get $1
i32.add
i32.const 2
i32.shl
i32.add
i32.load offset=96
i32.eq
if
local.get $0
local.get $3
i32.const 4
i32.shl
local.get $4
i32.add
i32.const 2
i32.shl
i32.add
local.get $2
call $assembly/index/setHead
i32.store offset=96
local.get $2
i32.eqz
if
local.get $0
local.get $3
i32.const 2
i32.shl
i32.add
local.get $0
local.get $3
call $assembly/index/getSLMap
i32.const 2
i32.shl
i32.add
i32.load offset=4
i32.const 1
local.get $4
i32.shl
@ -165,7 +122,7 @@
i32.xor
i32.and
local.tee $1
call $assembly/index/setSLMap
i32.store offset=4
local.get $1
i32.eqz
if
@ -183,7 +140,7 @@
end
end
)
(func $assembly/index/insertBlock (; 9 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
(func $assembly/index/insertBlock (; 2 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
@ -192,16 +149,22 @@
(local $7 i32)
local.get $1
i32.load
local.set $3
local.set $2
local.get $1
call $assembly/index/getRight
i32.const 16
i32.add
local.get $1
i32.load
i32.const -4
i32.and
i32.add
local.tee $4
i32.load
local.tee $5
i32.const 1
i32.and
if
local.get $3
local.get $2
i32.const -4
i32.and
i32.const 16
@ -210,7 +173,7 @@
i32.const -4
i32.and
i32.add
local.tee $2
local.tee $3
i32.const 1073741824
i32.lt_u
if
@ -218,21 +181,27 @@
local.get $4
call $assembly/index/removeBlock
local.get $1
local.get $3
local.get $2
i32.const 3
i32.and
local.get $2
local.get $3
i32.or
local.tee $3
local.tee $2
i32.store
local.get $1
call $assembly/index/getRight
i32.const 16
i32.add
local.get $1
i32.load
i32.const -4
i32.and
i32.add
local.tee $4
i32.load
local.set $5
end
end
local.get $3
local.get $2
i32.const 2
i32.and
if
@ -240,14 +209,14 @@
i32.const 4
i32.sub
i32.load
local.tee $2
local.tee $3
i32.load
local.tee $6
i32.const -4
i32.and
i32.const 16
i32.add
local.get $3
local.get $2
i32.const -4
i32.and
i32.add
@ -256,17 +225,17 @@
i32.lt_u
if
local.get $0
local.get $2
local.get $3
call $assembly/index/removeBlock
local.get $2
local.get $3
local.get $6
i32.const 3
i32.and
local.get $7
i32.or
local.tee $3
local.tee $2
i32.store
local.get $2
local.get $3
local.set $1
end
end
@ -280,8 +249,7 @@
i32.sub
local.get $1
i32.store
local.get $0
local.get $3
local.get $2
i32.const -4
i32.and
local.tee $2
@ -291,26 +259,36 @@
local.get $2
i32.const 16
i32.div_u
local.set $3
local.set $2
i32.const 0
else
local.get $2
i32.const 31
local.get $2
call $assembly/index/fls<usize>
local.tee $2
i32.clz
i32.sub
local.tee $3
i32.const 4
i32.sub
i32.shr_u
i32.const 16
i32.xor
local.set $3
local.get $2
local.set $2
local.get $3
i32.const 7
i32.sub
end
local.tee $2
local.set $3
local.get $0
local.get $3
call $assembly/index/getHead
i32.const 4
i32.shl
local.get $2
i32.add
i32.const 2
i32.shl
i32.add
i32.load offset=96
local.set $4
local.get $1
i32.const 0
@ -325,30 +303,42 @@
i32.store offset=16
end
local.get $0
local.get $2
local.get $3
i32.const 4
i32.shl
local.get $2
i32.add
i32.const 2
i32.shl
i32.add
local.get $1
call $assembly/index/setHead
i32.store offset=96
local.get $0
local.get $0
i32.load
i32.const 1
local.get $2
local.get $3
i32.shl
i32.or
i32.store
local.get $0
local.get $2
local.get $0
local.get $2
call $assembly/index/getSLMap
i32.const 1
local.get $3
i32.const 2
i32.shl
i32.add
local.get $0
local.get $3
i32.const 2
i32.shl
i32.add
i32.load offset=4
i32.const 1
local.get $2
i32.shl
i32.or
call $assembly/index/setSLMap
i32.store offset=4
)
(func $assembly/index/addMemory (; 10 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32)
(func $assembly/index/addMemory (; 3 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32)
(local $3 i32)
local.get $2
block (result i32)
@ -407,12 +397,12 @@
i32.store
local.get $0
local.get $2
call $assembly/index/setTail
i32.store offset=1568
local.get $0
local.get $1
call $assembly/index/insertBlock
)
(func $assembly/index/initialize (; 11 ;) (type $FUNCSIG$i) (result i32)
(func $assembly/index/initialize (; 4 ;) (type $FUNCSIG$i) (result i32)
(local $0 i32)
(local $1 i32)
(local $2 i32)
@ -448,7 +438,7 @@
i32.store
local.get $0
i32.const 0
call $assembly/index/setTail
i32.store offset=1568
loop $repeat|0
block $break|0
local.get $2
@ -457,8 +447,11 @@
br_if $break|0
local.get $0
local.get $2
i32.const 2
i32.shl
i32.add
i32.const 0
call $assembly/index/setSLMap
i32.store offset=4
i32.const 0
local.set $1
loop $repeat|1
@ -469,9 +462,15 @@
br_if $break|1
local.get $0
local.get $2
i32.const 4
i32.shl
local.get $1
i32.add
i32.const 2
i32.shl
i32.add
i32.const 0
call $assembly/index/setHead
i32.store offset=96
local.get $1
i32.const 1
i32.add
@ -498,7 +497,7 @@
call $assembly/index/addMemory
local.get $0
)
(func $assembly/index/searchBlock (; 12 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
(func $assembly/index/searchBlock (; 5 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
local.get $0
local.get $1
@ -511,26 +510,25 @@
local.set $1
i32.const 0
else
block (result i32)
local.get $1
i32.const 536870912
i32.lt_u
if
local.get $1
i32.const 1
local.get $1
call $assembly/index/fls<usize>
i32.const 4
i32.sub
i32.shl
i32.add
i32.const 1
i32.sub
local.set $1
end
local.get $1
end
call $assembly/index/fls<usize>
i32.const 31
local.get $1
i32.const 1
i32.const 27
local.get $1
i32.clz
i32.sub
i32.shl
i32.add
i32.const 1
i32.sub
local.get $1
local.get $1
i32.const 536870912
i32.lt_u
select
local.tee $1
i32.clz
i32.sub
local.set $2
local.get $1
local.get $2
@ -545,7 +543,10 @@
i32.sub
end
local.tee $2
call $assembly/index/getSLMap
i32.const 2
i32.shl
i32.add
i32.load offset=4
i32.const -1
local.get $1
i32.shl
@ -553,10 +554,16 @@
local.tee $1
if (result i32)
local.get $0
local.get $2
local.get $1
i32.ctz
call $assembly/index/getHead
local.get $2
i32.const 4
i32.shl
i32.add
i32.const 2
i32.shl
i32.add
i32.load offset=96
else
local.get $0
i32.load
@ -568,21 +575,30 @@
i32.and
local.tee $1
if (result i32)
local.get $0
local.get $0
local.get $1
i32.ctz
local.tee $1
local.get $0
local.get $1
call $assembly/index/getSLMap
i32.const 2
i32.shl
i32.add
i32.load offset=4
i32.ctz
call $assembly/index/getHead
local.get $1
i32.const 4
i32.shl
i32.add
i32.const 2
i32.shl
i32.add
i32.load offset=96
else
i32.const 0
end
end
)
(func $assembly/index/growMemory (; 13 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
(func $assembly/index/growMemory (; 6 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
@ -623,7 +639,7 @@
i32.shl
call $assembly/index/addMemory
)
(func $assembly/index/prepareBlock (; 14 ;) (type $FUNCSIG$iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(func $assembly/index/prepareBlock (; 7 ;) (type $FUNCSIG$iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(local $3 i32)
(local $4 i32)
local.get $1
@ -670,9 +686,21 @@
i32.and
i32.store
local.get $1
call $assembly/index/getRight
i32.const 16
i32.add
local.get $1
call $assembly/index/getRight
i32.load
i32.const -4
i32.and
i32.add
local.get $1
i32.const 16
i32.add
local.get $1
i32.load
i32.const -4
i32.and
i32.add
i32.load
i32.const -3
i32.and
@ -682,7 +710,7 @@
i32.const 16
i32.add
)
(func $assembly/index/__mm_allocate (; 15 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
(func $assembly/index/__mm_allocate (; 8 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
(local $1 i32)
(local $2 i32)
global.get $assembly/index/ROOT
@ -699,7 +727,7 @@
if
i32.const 0
i32.const 24
i32.const 498
i32.const 507
i32.const 29
call $~lib/builtins/abort
unreachable
@ -744,7 +772,7 @@
local.get $1
call $assembly/index/prepareBlock
)
(func $assembly/index/freeBlock (; 16 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
(func $assembly/index/freeBlock (; 9 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
local.get $1
local.get $1
i32.load
@ -755,7 +783,7 @@
local.get $1
call $assembly/index/insertBlock
)
(func $assembly/index/__mm_free (; 17 ;) (type $FUNCSIG$vi) (param $0 i32)
(func $assembly/index/__mm_free (; 10 ;) (type $FUNCSIG$vi) (param $0 i32)
(local $1 i32)
local.get $0
if
@ -770,7 +798,7 @@
end
end
)
(func $assembly/index/decrement (; 18 ;) (type $FUNCSIG$vi) (param $0 i32)
(func $assembly/index/decrement (; 11 ;) (type $FUNCSIG$vi) (param $0 i32)
local.get $0
i32.load offset=4
i32.const 268435455
@ -785,7 +813,7 @@
end
unreachable
)
(func $assembly/index/markGray (; 19 ;) (type $FUNCSIG$vi) (param $0 i32)
(func $assembly/index/markGray (; 12 ;) (type $FUNCSIG$vi) (param $0 i32)
(local $1 i32)
local.get $0
i32.load offset=4
@ -805,7 +833,7 @@
unreachable
end
)
(func $assembly/index/scanBlack (; 20 ;) (type $FUNCSIG$vi) (param $0 i32)
(func $assembly/index/scanBlack (; 13 ;) (type $FUNCSIG$vi) (param $0 i32)
local.get $0
local.get $0
i32.load offset=4
@ -814,7 +842,7 @@
i32.store offset=4
unreachable
)
(func $assembly/index/scan (; 21 ;) (type $FUNCSIG$vi) (param $0 i32)
(func $assembly/index/scan (; 14 ;) (type $FUNCSIG$vi) (param $0 i32)
(local $1 i32)
local.get $0
i32.load offset=4
@ -844,7 +872,7 @@
end
end
)
(func $assembly/index/collectWhite (; 22 ;) (type $FUNCSIG$vi) (param $0 i32)
(func $assembly/index/collectWhite (; 15 ;) (type $FUNCSIG$vi) (param $0 i32)
(local $1 i32)
local.get $0
i32.load offset=4
@ -866,7 +894,7 @@
local.get $0
call $assembly/index/freeBlock
)
(func $assembly/index/__rt_visit (; 23 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
(func $assembly/index/__rt_visit (; 16 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
block $break|0
block $case4|0
block $case3|0
@ -916,7 +944,7 @@
call $assembly/index/collectWhite
end
)
(func $assembly/index/__gc_retain (; 24 ;) (type $FUNCSIG$vi) (param $0 i32)
(func $assembly/index/__gc_retain (; 17 ;) (type $FUNCSIG$vi) (param $0 i32)
local.get $0
if
local.get $0
@ -930,7 +958,7 @@
i32.store offset=4
end
)
(func $assembly/index/__gc_release (; 25 ;) (type $FUNCSIG$vi) (param $0 i32)
(func $assembly/index/__gc_release (; 18 ;) (type $FUNCSIG$vi) (param $0 i32)
local.get $0
if
local.get $0
@ -939,7 +967,7 @@
call $assembly/index/decrement
end
)
(func $assembly/index/collectCycles (; 26 ;) (type $FUNCSIG$v)
(func $assembly/index/collectCycles (; 19 ;) (type $FUNCSIG$v)
(local $0 i32)
(local $1 i32)
(local $2 i32)
@ -1061,7 +1089,7 @@
local.get $5
global.set $assembly/index/CUR
)
(func $null (; 27 ;) (type $FUNCSIG$v)
(func $null (; 20 ;) (type $FUNCSIG$v)
nop
)
)

File diff suppressed because it is too large Load Diff