Skip to content

Commit

Permalink
fix: Support 65536 pages of memory in TLSF (#2654)
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrCai0907 committed Apr 25, 2023
1 parent c0d86e6 commit e28f536
Show file tree
Hide file tree
Showing 158 changed files with 6,746 additions and 3,363 deletions.
11 changes: 6 additions & 5 deletions std/assembly/rt/tlsf.ts
Expand Up @@ -373,8 +373,9 @@ function prepareBlock(root: Root, block: Block, size: usize): void {
}

/** Adds more memory to the pool. */
function addMemory(root: Root, start: usize, end: usize): bool {
if (DEBUG) assert(start <= end); // must be valid
function addMemory(root: Root, start: usize, endU64: u64): bool {
let end = <usize>endU64;
if (DEBUG) assert(<u64>start <= endU64); // must be valid
start = ((start + BLOCK_OVERHEAD + AL_MASK) & ~AL_MASK) - BLOCK_OVERHEAD;
end &= ~AL_MASK;

Expand Down Expand Up @@ -442,7 +443,7 @@ function growMemory(root: Root, size: usize): void {
if (memory.grow(pagesNeeded) < 0) unreachable();
}
let pagesAfter = memory.size();
addMemory(root, <usize>pagesBefore << 16, <usize>pagesAfter << 16);
addMemory(root, <usize>pagesBefore << 16, <u64>pagesAfter << 16);
}

/** Computes the size (excl. header) of a block. */
Expand Down Expand Up @@ -477,11 +478,11 @@ function initialize(): void {
}
let memStart = rootOffset + ROOT_SIZE;
if (ASC_LOW_MEMORY_LIMIT) {
const memEnd = <usize>ASC_LOW_MEMORY_LIMIT & ~AL_MASK;
const memEnd = <u64>ASC_LOW_MEMORY_LIMIT & ~AL_MASK;
if (memStart <= memEnd) addMemory(root, memStart, memEnd);
else unreachable(); // low memory limit already exceeded
} else {
addMemory(root, memStart, memory.size() << 16);
addMemory(root, memStart, <u64>memory.size() << 16);
}
ROOT = root;
}
Expand Down
56 changes: 32 additions & 24 deletions tests/compiler/bindings/esm.debug.wat
Expand Up @@ -7,14 +7,15 @@
(type $i32_i32_i32_=>_none (func (param i32 i32 i32)))
(type $none_=>_i32 (func (result i32)))
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
(type $i32_i64_=>_none (func (param i32 i64)))
(type $i32_i32_f64_f64_f64_f64_f64_=>_none (func (param i32 i32 f64 f64 f64 f64 f64)))
(type $f64_=>_f64 (func (param f64) (result f64)))
(type $i64_i64_=>_i64 (func (param i64 i64) (result i64)))
(type $none_=>_i64 (func (result i64)))
(type $i32_i32_i64_=>_i32 (func (param i32 i32 i64) (result i32)))
(type $i32_f32_=>_none (func (param i32 f32)))
(type $i32_f64_=>_none (func (param i32 f64)))
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
(type $i32_i32_i64_=>_none (func (param i32 i32 i64)))
(type $i32_i32_=>_f32 (func (param i32 i32) (result f32)))
(import "env" "Math.E" (global $~lib/bindings/dom/Math.E f64))
Expand Down Expand Up @@ -1123,25 +1124,30 @@
local.get $slMap
i32.store $0 offset=4
)
(func $~lib/rt/tlsf/addMemory (param $root i32) (param $start i32) (param $end i32) (result i32)
(local $root|3 i32)
(func $~lib/rt/tlsf/addMemory (param $root i32) (param $start i32) (param $endU64 i64) (result i32)
(local $end i32)
(local $root|4 i32)
(local $tail i32)
(local $tailInfo i32)
(local $size i32)
(local $leftSize i32)
(local $left i32)
(local $root|9 i32)
(local $tail|10 i32)
(local $root|10 i32)
(local $tail|11 i32)
local.get $endU64
i32.wrap_i64
local.set $end
i32.const 1
drop
local.get $start
local.get $end
i32.le_u
i64.extend_i32_u
local.get $endU64
i64.le_u
i32.eqz
if
i32.const 0
i32.const 672
i32.const 377
i32.const 378
i32.const 14
call $~lib/builtins/abort
unreachable
Expand All @@ -1166,8 +1172,8 @@
local.set $end
block $~lib/rt/tlsf/GETTAIL|inlined.0 (result i32)
local.get $root
local.set $root|3
local.get $root|3
local.set $root|4
local.get $root|4
i32.load $0 offset=1568
br $~lib/rt/tlsf/GETTAIL|inlined.0
end
Expand All @@ -1187,7 +1193,7 @@
if
i32.const 0
i32.const 672
i32.const 384
i32.const 385
i32.const 16
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1220,7 +1226,7 @@
if
i32.const 0
i32.const 672
i32.const 397
i32.const 398
i32.const 5
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1276,11 +1282,11 @@
i32.or
call $~lib/rt/common/BLOCK#set:mmInfo
local.get $root
local.set $root|9
local.set $root|10
local.get $tail
local.set $tail|10
local.get $root|9
local.get $tail|10
local.set $tail|11
local.get $root|10
local.get $tail|11
i32.store $0 offset=1568
local.get $root
local.get $left
Expand Down Expand Up @@ -1426,8 +1432,9 @@
local.get $root
local.get $memStart
memory.size $0
i32.const 16
i32.shl
i64.extend_i32_s
i64.const 16
i64.shl
call $~lib/rt/tlsf/addMemory
drop
local.get $root
Expand Down Expand Up @@ -1463,7 +1470,7 @@
if
i32.const 0
i32.const 672
i32.const 559
i32.const 560
i32.const 3
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1792,7 +1799,7 @@
if
i32.const 336
i32.const 672
i32.const 458
i32.const 459
i32.const 29
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -2094,8 +2101,9 @@
i32.const 16
i32.shl
local.get $pagesAfter
i32.const 16
i32.shl
i64.extend_i32_s
i64.const 16
i64.shl
call $~lib/rt/tlsf/addMemory
drop
)
Expand Down Expand Up @@ -2235,7 +2243,7 @@
if
i32.const 0
i32.const 672
i32.const 496
i32.const 497
i32.const 16
call $~lib/builtins/abort
unreachable
Expand All @@ -2255,7 +2263,7 @@
if
i32.const 0
i32.const 672
i32.const 498
i32.const 499
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down
48 changes: 26 additions & 22 deletions tests/compiler/bindings/esm.release.wat
@@ -1,17 +1,17 @@
(module
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $i32_=>_none (func (param i32)))
(type $i32_i32_i32_=>_none (func (param i32 i32 i32)))
(type $i32_=>_i32 (func (param i32) (result i32)))
(type $none_=>_i32 (func (result i32)))
(type $none_=>_none (func))
(type $i32_i32_i32_=>_none (func (param i32 i32 i32)))
(type $i32_i32_=>_none (func (param i32 i32)))
(type $i32_i32_i64_=>_none (func (param i32 i32 i64)))
(type $i32_i32_f64_f64_f64_f64_f64_=>_none (func (param i32 i32 f64 f64 f64 f64 f64)))
(type $f64_=>_f64 (func (param f64) (result f64)))
(type $i64_i64_=>_i64 (func (param i64 i64) (result i64)))
(type $none_=>_i64 (func (result i64)))
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(type $i32_i32_i64_=>_none (func (param i32 i32 i64)))
(import "env" "Math.E" (global $~lib/bindings/dom/Math.E f64))
(import "env" "trace" (func $~lib/builtins/trace (param i32 i32 f64 f64 f64 f64 f64)))
(import "env" "console.log" (func $~lib/bindings/dom/console.log (param i32)))
Expand Down Expand Up @@ -731,16 +731,17 @@
i32.or
i32.store $0 offset=4
)
(func $~lib/rt/tlsf/addMemory (param $0 i32) (param $1 i32) (param $2 i32)
(func $~lib/rt/tlsf/addMemory (param $0 i32) (param $1 i32) (param $2 i64)
(local $3 i32)
(local $4 i32)
local.get $1
local.get $2
i32.gt_u
local.get $1
i64.extend_i32_u
i64.lt_u
if
i32.const 0
i32.const 1696
i32.const 377
i32.const 378
i32.const 14
call $~lib/builtins/abort
unreachable
Expand All @@ -765,7 +766,7 @@
if
i32.const 0
i32.const 1696
i32.const 384
i32.const 385
i32.const 16
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -793,18 +794,19 @@
if
i32.const 0
i32.const 1696
i32.const 397
i32.const 398
i32.const 5
call $~lib/builtins/abort
unreachable
end
end
local.get $2
i32.wrap_i64
i32.const -16
i32.and
local.get $1
i32.sub
local.tee $2
local.tee $4
i32.const 20
i32.lt_u
if
Expand All @@ -814,10 +816,10 @@
local.get $3
i32.const 2
i32.and
local.get $2
local.get $4
i32.const 8
i32.sub
local.tee $2
local.tee $3
i32.const 1
i32.or
i32.or
Expand All @@ -831,13 +833,13 @@
local.get $1
i32.const 4
i32.add
local.get $2
local.get $3
i32.add
local.tee $2
local.tee $3
i32.const 2
i32.store $0
local.get $0
local.get $2
local.get $3
i32.store $0 offset=1568
local.get $0
local.get $1
Expand Down Expand Up @@ -916,8 +918,9 @@
i32.const 35056
i32.const 36628
memory.size $0
i32.const 16
i32.shl
i64.extend_i32_s
i64.const 16
i64.shl
call $~lib/rt/tlsf/addMemory
i32.const 35056
global.set $~lib/rt/tlsf/ROOT
Expand Down Expand Up @@ -1158,7 +1161,7 @@
if
i32.const 0
i32.const 1696
i32.const 559
i32.const 560
i32.const 3
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1393,7 +1396,7 @@
if
i32.const 1360
i32.const 1696
i32.const 458
i32.const 459
i32.const 29
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1472,8 +1475,9 @@
i32.const 16
i32.shl
memory.size $0
i32.const 16
i32.shl
i64.extend_i32_s
i64.const 16
i64.shl
call $~lib/rt/tlsf/addMemory
local.get $4
local.get $5
Expand All @@ -1483,7 +1487,7 @@
if
i32.const 0
i32.const 1696
i32.const 496
i32.const 497
i32.const 16
call $~lib/builtins/abort
unreachable
Expand All @@ -1498,7 +1502,7 @@
if
i32.const 0
i32.const 1696
i32.const 498
i32.const 499
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down

0 comments on commit e28f536

Please sign in to comment.