Skip to content

Truly independent web browser

LibJS: Compress the call path pointers of ECMAScript functions

Andreas Klingfc700f381e52
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
index e6e467c3f87..4bb39d9ee01 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
@@ -5895,60 +5895,65 @@ asm_handler_Call:
b .Lasm_Call.ssa_block_1
1:
tbz x0, #6, .Lasm_Call.ssa_block_3
- ldr x0, [x1, #80]
+ ldr w0, [x1, #76]
cbz x0, .Lasm_Call.assert_failure_0
- ldp x4, x0, [x0, #16]
+ add x0, x24, w0, uxtw #3
+ ldr w2, [x0, #12]
+ add x9, x24, w2, uxtw #3
+ cmp w2, #0
+ csel x2, x9, xzr, ne
+ ldr x0, [x0, #16]
tbnz x0, #32, 1f
b .Lasm_Call.ssa_block_1
1:
tbnz x0, #33, .Lasm_Call.ssa_block_2
movz x6, #0x7ffb, lsl #48
tbz x0, #34, .Lasm_Call.ssa_block_9
- ldr w2, [x21, #20]
- ldr x6, [x27, x2, lsl #3]
+ ldr w3, [x21, #20]
+ ldr x6, [x27, x3, lsl #3]
tbnz x0, #35, .Lasm_Call.ssa_block_9
- lsr x2, x6, #48
- mov x3, x2
- and w3, w3, #0xfffe
+ lsr x3, x6, #48
+ mov x4, x3
+ and w4, w4, #0xfffe
mov w9, #32766
- cmp w3, w9
+ cmp w4, w9
b.ne .Lasm_Call.ssa_block_13
- ldr x2, [x1, #24]
- cbz x2, .Lasm_Call.assert_failure_1
- ldr x2, [x2, #16]
- cbz x2, .Lasm_Call.assert_failure_2
- ldr x2, [x2, #32]
- cbz x2, .Lasm_Call.assert_failure_3
- ldr x6, [x2, #32]
+ ldr x3, [x1, #24]
+ cbz x3, .Lasm_Call.assert_failure_1
+ ldr x3, [x3, #16]
+ cbz x3, .Lasm_Call.assert_failure_2
+ ldr x3, [x3, #32]
+ cbz x3, .Lasm_Call.assert_failure_3
+ ldr x6, [x3, #32]
and x6, x6, #0x7ffffffff
- movz x2, #0xfff9, lsl #48
- orr x6, x6, x2
+ movz x3, #0xfff9, lsl #48
+ orr x6, x6, x3
b .Lasm_Call.ssa_block_9
.Lasm_Call.ssa_block_13:
mov w9, #65529
- cmp w2, w9
+ cmp w3, w9
b.ne .Lasm_Call.ssa_block_2
.Lasm_Call.ssa_block_9:
- ldr w3, [x21, #24]
- mov w2, w0
- cmp x2, x3
+ ldr w4, [x21, #24]
+ mov w3, w0
+ cmp x3, x4
b.hs .Lasm_Call.ssa_block_27
- mov x2, x3
+ mov x3, x4
.Lasm_Call.ssa_block_27:
- cbz x4, .Lasm_Call.assert_predicate_failure_280
- ldp w0, w3, [x4, #12]
- cmp x3, x0
+ cbz x2, .Lasm_Call.assert_predicate_failure_280
+ ldp w0, w4, [x2, #12]
+ cmp x4, x0
b.lo .Lasm_Call.assert_predicate_failure_286
movn w0, #0
- subs x0, x0, x3
- cmp x0, x2
+ subs x0, x0, x4
+ cmp x0, x3
b.lo .Lasm_Call.ssa_block_1
mov x0, x20
ldp x0, x8, [x0, #16]
cbz x0, .Lasm_Call.assert_failure_4
cbz x8, .Lasm_Call.assert_failure_5
- mov x7, x3
- adds x7, x7, x2
+ mov x7, x4
+ adds x7, x7, x3
lsl x5, x7, #3
add x5, x5, #128
adds x5, x5, x0
@@ -5958,7 +5963,7 @@ asm_handler_Call:
b.hi .Lasm_Call.ssa_block_1
mov x8, x20
str x5, [x8, #16]
- stp w7, w2, [x0, #116]
+ stp w7, w3, [x0, #116]
ldr w5, [x21, #24]
str w5, [x0, #104]
ldr x5, [x1, #24]
@@ -5966,26 +5971,33 @@ asm_handler_Call:
ldr x5, [x5, #16]
cbz x5, .Lasm_Call.assert_failure_7
stp x1, x5, [x0, #0]
- ldp x5, x7, [x1, #104]
+ ldr w5, [x1, #80]
+ add x9, x24, w5, uxtw #3
+ cmp w5, #0
+ csel x5, x9, xzr, ne
+ ldr w7, [x1, #84]
+ add x9, x24, w7, uxtw #3
+ cmp w7, #0
+ csel x7, x9, xzr, ne
cbz x5, .Lasm_Call.assert_predicate_failure_329
stp x5, x5, [x0, #32]
str x7, [x0, #48]
- stp x6, x4, [x0, #80]
- movz x4, #0x7ffb, lsl #48
- stp x4, x4, [x0, #128]
+ stp x6, x2, [x0, #80]
+ movz x2, #0x7ffb, lsl #48
+ stp x2, x2, [x0, #128]
str x6, [x0, #144]
- stp x4, x4, [x0, #152]
- add x4, x0, #16
- add x1, x1, #120
+ stp x2, x2, [x0, #152]
+ add x2, x0, #16
+ add x1, x1, #96
ldp x1, x5, [x1, #0]
- stp x1, x5, [x4, #0]
+ stp x1, x5, [x2, #0]
str wzr, [x0, #64]
- mov x4, x20
- ldr x1, [x4, #32]
+ mov x2, x20
+ ldr x1, [x2, #32]
cbz x1, .Lasm_Call.assert_failure_8
str x1, [x0, #56]
add x1, x1, #1
- str x1, [x4, #32]
+ str x1, [x2, #32]
str wzr, [x0, #68]
movn w9, #0
str w9, [x0, #72]
@@ -5996,36 +6008,36 @@ asm_handler_Call:
strb wzr, [x0, #124]
strb wzr, [x0, #125]
str x28, [x0, #96]
- ldp w4, w5, [x21, #8]
+ ldp w2, w5, [x21, #8]
sub w1, w21, w26
- adds x4, x4, x1
+ adds x2, x2, x1
str w1, [x28, #64]
- stp w4, w5, [x0, #108]
- ldr w4, [x21, #24]
- cmp x2, x4
+ stp w2, w5, [x0, #108]
+ ldr w2, [x21, #24]
+ cmp x3, x2
b.lo .Lasm_Call.assert_predicate_failure_398
add x5, x0, #128
add x6, x28, #128
add x7, x21, #32
mov x1, #0
.Lasm_Call.ssa_block_17:
- cmp x1, x4
+ cmp x1, x2
b.hs .Lasm_Call.ssa_block_16
ldr w8, [x7, x1, lsl #2]
ldr x8, [x6, x8, lsl #3]
- str x8, [x5, x3, lsl #3]
+ str x8, [x5, x4, lsl #3]
add x1, x1, #1
- add x3, x3, #1
+ add x4, x4, #1
b .Lasm_Call.ssa_block_17
.Lasm_Call.ssa_block_16:
movz x1, #0x7ffe, lsl #48
- adds x2, x2, x3
- subs x2, x2, x4
+ adds x3, x3, x4
+ subs x3, x3, x2
.Lasm_Call.ssa_block_20:
- cmp x3, x2
+ cmp x4, x3
b.hs .Lasm_Call.ssa_block_19
- str x1, [x5, x3, lsl #3]
- add x3, x3, #1
+ str x1, [x5, x4, lsl #3]
+ add x4, x4, #1
b .Lasm_Call.ssa_block_20
.Lasm_Call.ssa_block_19:
ldr x1, [x0, #88]
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
index 903f86e7479..1469c83f405 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
@@ -5982,11 +5982,15 @@ asm_handler_Call:
jz .Lasm_Call.ssa_block_1
test eax, 64
jz .Lasm_Call.ssa_block_3
- mov rax, QWORD PTR [rcx + 80]
+ mov eax, DWORD PTR [rcx + 76]
test rax, rax
jz .Lasm_Call.assert_failure_0
- mov rdi, QWORD PTR [rax + 16]
- mov rax, QWORD PTR [rax + 24]
+ lea rax, [r15 + rax * 8]
+ mov edx, DWORD PTR [rax + 12]
+ lea r11, [r15 + rdx * 8]
+ test edx, edx
+ cmovnz rdx, r11
+ mov rax, QWORD PTR [rax + 16]
bt rax, 32
jnc .Lasm_Call.ssa_block_1
bt rax, 33
@@ -5994,50 +5998,50 @@ asm_handler_Call:
movabs r9, 9221964661971222528
bt rax, 34
jnc .Lasm_Call.ssa_block_9
- mov edx, DWORD PTR [r14 + r13 + 20]
- mov r9, QWORD PTR [rbx + rdx * 8]
+ mov esi, DWORD PTR [r14 + r13 + 20]
+ mov r9, QWORD PTR [rbx + rsi * 8]
bt rax, 35
jc .Lasm_Call.ssa_block_9
- mov rdx, r9
- shr rdx, 48
- mov rsi, rdx
- and si, 65534
- cmp si, 32766
+ mov rsi, r9
+ shr rsi, 48
+ mov rdi, rsi
+ and di, 65534
+ cmp di, 32766
jne .Lasm_Call.ssa_block_13
- mov rdx, QWORD PTR [rcx + 24]
- test rdx, rdx
+ mov rsi, QWORD PTR [rcx + 24]
+ test rsi, rsi
jz .Lasm_Call.assert_failure_1
- mov rdx, QWORD PTR [rdx + 16]
- test rdx, rdx
+ mov rsi, QWORD PTR [rsi + 16]
+ test rsi, rsi
jz .Lasm_Call.assert_failure_2
- mov rdx, QWORD PTR [rdx + 32]
- test rdx, rdx
+ mov rsi, QWORD PTR [rsi + 32]
+ test rsi, rsi
jz .Lasm_Call.assert_failure_3
- mov r9, QWORD PTR [rdx + 32]
- movabs rdx, 34359738367
- and r9, rdx
- movabs rdx, -1970324836974592
- or r9, rdx
+ mov r9, QWORD PTR [rsi + 32]
+ movabs rsi, 34359738367
+ and r9, rsi
+ movabs rsi, -1970324836974592
+ or r9, rsi
jmp .Lasm_Call.ssa_block_9
.Lasm_Call.ssa_block_13:
- cmp dx, 65529
+ cmp si, 65529
jne .Lasm_Call.ssa_block_2
.Lasm_Call.ssa_block_9:
- mov esi, DWORD PTR [r14 + r13 + 24]
- mov edx, eax
- cmp rdx, rsi
+ mov edi, DWORD PTR [r14 + r13 + 24]
+ mov esi, eax
+ cmp rsi, rdi
jae .Lasm_Call.ssa_block_27
- mov rdx, rsi
+ mov rsi, rdi
.Lasm_Call.ssa_block_27:
- test rdi, rdi
+ test rdx, rdx
jz .Lasm_Call.assert_predicate_failure_280
- mov eax, DWORD PTR [rdi + 12]
- mov esi, DWORD PTR [rdi + 16]
- cmp rsi, rax
+ mov eax, DWORD PTR [rdx + 12]
+ mov edi, DWORD PTR [rdx + 16]
+ cmp rdi, rax
jb .Lasm_Call.assert_predicate_failure_286
mov eax, 4294967295
- sub rax, rsi
- cmp rax, rdx
+ sub rax, rdi
+ cmp rax, rsi
jb .Lasm_Call.ssa_block_1
mov rax, QWORD PTR [rbp - 48]
mov r11, QWORD PTR [rax + 24]
@@ -6046,8 +6050,8 @@ asm_handler_Call:
jz .Lasm_Call.assert_failure_4
test r11, r11
jz .Lasm_Call.assert_failure_5
- mov r10, rsi
- add r10, rdx
+ mov r10, rdi
+ add r10, rsi
mov r8, r10
shl r8, 3
lea r8, [r8 + 128]
@@ -6059,7 +6063,7 @@ asm_handler_Call:
mov r11, QWORD PTR [rbp - 48]
mov QWORD PTR [r11 + 16], r8
mov DWORD PTR [rax + 116], r10d
- mov DWORD PTR [rax + 120], edx
+ mov DWORD PTR [rax + 120], esi
mov r8d, DWORD PTR [r14 + r13 + 24]
mov DWORD PTR [rax + 104], r8d
mov r8, QWORD PTR [rcx + 24]
@@ -6070,35 +6074,41 @@ asm_handler_Call:
jz .Lasm_Call.assert_failure_7
mov QWORD PTR [rax], rcx
mov QWORD PTR [rax + 8], r8
- mov r8, QWORD PTR [rcx + 104]
- mov r10, QWORD PTR [rcx + 112]
+ mov r8d, DWORD PTR [rcx + 80]
+ lea r11, [r15 + r8 * 8]
+ test r8d, r8d
+ cmovnz r8, r11
+ mov r10d, DWORD PTR [rcx + 84]
+ lea r11, [r15 + r10 * 8]
+ test r10d, r10d
+ cmovnz r10, r11
test r8, r8
jz .Lasm_Call.assert_predicate_failure_329
mov QWORD PTR [rax + 32], r8
mov QWORD PTR [rax + 40], r8
mov QWORD PTR [rax + 48], r10
mov QWORD PTR [rax + 80], r9
- mov QWORD PTR [rax + 88], rdi
- movabs rdi, 9221964661971222528
- mov QWORD PTR [rax + 128], rdi
- mov QWORD PTR [rax + 136], rdi
+ mov QWORD PTR [rax + 88], rdx
+ movabs rdx, 9221964661971222528
+ mov QWORD PTR [rax + 128], rdx
+ mov QWORD PTR [rax + 136], rdx
mov QWORD PTR [rax + 144], r9
- mov QWORD PTR [rax + 152], rdi
- mov QWORD PTR [rax + 160], rdi
- lea rdi, [rax + 16]
- lea rcx, [rcx + 120]
+ mov QWORD PTR [rax + 152], rdx
+ mov QWORD PTR [rax + 160], rdx
+ lea rdx, [rax + 16]
+ lea rcx, [rcx + 96]
mov r8, QWORD PTR [rcx + 8]
mov rcx, QWORD PTR [rcx]
- mov QWORD PTR [rdi], rcx
- mov QWORD PTR [rdi + 8], r8
+ mov QWORD PTR [rdx], rcx
+ mov QWORD PTR [rdx + 8], r8
mov DWORD PTR [rax + 64], 0
- mov rdi, QWORD PTR [rbp - 48]
- mov rcx, QWORD PTR [rdi + 32]
+ mov rdx, QWORD PTR [rbp - 48]
+ mov rcx, QWORD PTR [rdx + 32]
test rcx, rcx
jz .Lasm_Call.assert_failure_8
mov QWORD PTR [rax + 56], rcx
lea rcx, [rcx + 1]
- mov QWORD PTR [rdi + 32], rcx
+ mov QWORD PTR [rdx + 32], rcx
mov DWORD PTR [rax + 68], 0
mov DWORD PTR [rax + 72], 4294967295
mov BYTE PTR [rax + 76], 0
@@ -6109,38 +6119,38 @@ asm_handler_Call:
mov BYTE PTR [rax + 125], 0
lea r11, [rbx - 128]
mov QWORD PTR [rax + 96], r11
- mov edi, DWORD PTR [r14 + r13 + 8]
+ mov edx, DWORD PTR [r14 + r13 + 8]
mov r8d, DWORD PTR [r14 + r13 + 12]
mov ecx, r13d
- add rdi, rcx
+ add rdx, rcx
mov DWORD PTR [rbx - 64], ecx
- mov DWORD PTR [rax + 108], edi
+ mov DWORD PTR [rax + 108], edx
mov DWORD PTR [rax + 112], r8d
- mov edi, DWORD PTR [r14 + r13 + 24]
- cmp rdx, rdi
+ mov edx, DWORD PTR [r14 + r13 + 24]
+ cmp rsi, rdx
jb .Lasm_Call.assert_predicate_failure_398
lea r8, [rax + 128]
lea r9, [rbx]
lea r10, [r14 + r13 + 32]
xor rcx, rcx
.Lasm_Call.ssa_block_17:
- cmp rcx, rdi
+ cmp rcx, rdx
jae .Lasm_Call.ssa_block_16
mov r11d, DWORD PTR [r10 + rcx * 4]
mov r11, QWORD PTR [r9 + r11 * 8]
- mov QWORD PTR [r8 + rsi * 8], r11
+ mov QWORD PTR [r8 + rdi * 8], r11
lea rcx, [rcx + 1]
- lea rsi, [rsi + 1]
+ lea rdi, [rdi + 1]
jmp .Lasm_Call.ssa_block_17
.Lasm_Call.ssa_block_16:
movabs rcx, 9222809086901354496
- add rdx, rsi
- sub rdx, rdi
+ add rsi, rdi
+ sub rsi, rdx
.Lasm_Call.ssa_block_20:
- cmp rsi, rdx
+ cmp rdi, rsi
jae .Lasm_Call.ssa_block_19
- mov QWORD PTR [r8 + rsi * 8], rcx
- lea rsi, [rsi + 1]
+ mov QWORD PTR [r8 + rdi * 8], rcx
+ lea rdi, [rdi + 1]
jmp .Lasm_Call.ssa_block_20
.Lasm_Call.ssa_block_19:
mov rcx, QWORD PTR [rax + 88]
diff --git a/Libraries/LibJS/Flap/tests/interpreter-layout.conf b/Libraries/LibJS/Flap/tests/interpreter-layout.conf
index 7fa4ee26595..6ec5f25f429 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-layout.conf
+++ b/Libraries/LibJS/Flap/tests/interpreter-layout.conf
@@ -368,25 +368,25 @@ const DIRECT_GETTER_FUNCTION_WEAK_IMPL_VALUE_WORD_OFFSET = 124
field DirectGetterFunction.weak_impl_value_word_offset u32 DIRECT_GETTER_FUNCTION_WEAK_IMPL_VALUE_WORD_OFFSET nullable scalar
# ECMAScriptFunctionObject layout
-const ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA = 80
-field ECMAScriptFunctionObject.shared_data SharedFunctionInstanceData ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA nonnull cell
-const ECMASCRIPT_FUNCTION_OBJECT_ENVIRONMENT = 104
-field ECMAScriptFunctionObject.environment Environment ECMASCRIPT_FUNCTION_OBJECT_ENVIRONMENT nullable cell environment_state
-const ECMASCRIPT_FUNCTION_OBJECT_PRIVATE_ENVIRONMENT = 112
-field ECMAScriptFunctionObject.private_environment PrivateEnvironment ECMASCRIPT_FUNCTION_OBJECT_PRIVATE_ENVIRONMENT nullable cell environment_state
-const ECMASCRIPT_FUNCTION_OBJECT_SCRIPT_OR_MODULE = 120
+const ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA = 76
+field ECMAScriptFunctionObject.shared_data SharedFunctionInstanceData ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA nonnull compressed
+const ECMASCRIPT_FUNCTION_OBJECT_ENVIRONMENT = 80
+field ECMAScriptFunctionObject.environment Environment ECMASCRIPT_FUNCTION_OBJECT_ENVIRONMENT nullable compressed
+const ECMASCRIPT_FUNCTION_OBJECT_PRIVATE_ENVIRONMENT = 84
+field ECMAScriptFunctionObject.private_environment PrivateEnvironment ECMASCRIPT_FUNCTION_OBJECT_PRIVATE_ENVIRONMENT nullable compressed
+const ECMASCRIPT_FUNCTION_OBJECT_SCRIPT_OR_MODULE = 96
field ECMAScriptFunctionObject.script_or_module ScriptOrModule ECMASCRIPT_FUNCTION_OBJECT_SCRIPT_OR_MODULE nullable scalar
# SharedFunctionInstanceData layout
-const SHARED_FUNCTION_INSTANCE_DATA_EXECUTABLE = 16
-field SharedFunctionInstanceData.executable Executable SHARED_FUNCTION_INSTANCE_DATA_EXECUTABLE nullable cell call_data
-const SHARED_FUNCTION_INSTANCE_DATA_ASM_CALL_METADATA = 24
-field SharedFunctionInstanceData.asm_call_metadata u64 SHARED_FUNCTION_INSTANCE_DATA_ASM_CALL_METADATA nullable scalar call_data
-const SHARED_FUNCTION_INSTANCE_DATA_FORMAL_PARAMETER_COUNT = 32
-const SHARED_FUNCTION_INSTANCE_DATA_STRICT = 36
-const SHARED_FUNCTION_INSTANCE_DATA_FUNCTION_ENVIRONMENT_NEEDED = 37
-const SHARED_FUNCTION_INSTANCE_DATA_USES_THIS = 38
-const SHARED_FUNCTION_INSTANCE_DATA_CAN_INLINE_CALL = 39
+const SHARED_FUNCTION_INSTANCE_DATA_EXECUTABLE = 12
+field SharedFunctionInstanceData.executable Executable SHARED_FUNCTION_INSTANCE_DATA_EXECUTABLE nullable compressed
+const SHARED_FUNCTION_INSTANCE_DATA_ASM_CALL_METADATA = 16
+field SharedFunctionInstanceData.asm_call_metadata u64 SHARED_FUNCTION_INSTANCE_DATA_ASM_CALL_METADATA nullable scalar
+const SHARED_FUNCTION_INSTANCE_DATA_FORMAL_PARAMETER_COUNT = 24
+const SHARED_FUNCTION_INSTANCE_DATA_STRICT = 28
+const SHARED_FUNCTION_INSTANCE_DATA_FUNCTION_ENVIRONMENT_NEEDED = 29
+const SHARED_FUNCTION_INSTANCE_DATA_USES_THIS = 30
+const SHARED_FUNCTION_INSTANCE_DATA_CAN_INLINE_CALL = 31
const SHARED_FUNCTION_INSTANCE_DATA_ASM_CALL_METADATA_CAN_INLINE_CALL = 4294967296
const SHARED_FUNCTION_INSTANCE_DATA_ASM_CALL_METADATA_NEEDS_ENVIRONMENT_OR_THIS_VALUE_RESOLUTION = 8589934592
const SHARED_FUNCTION_INSTANCE_DATA_ASM_CALL_METADATA_USES_THIS = 17179869184
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rs b/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rs
index dbc7f04157b..18577075e87 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rs
@@ -559,12 +559,10 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
.ok_or(CompileFailure::CodeGeneration)?;
let (entry, resume) = self.defer_allocation(node, AllocationCall::Environment(shape_cache, capacity));
let template_bytes = 8 * template.words.len() as u64;
- let stored = [template.binding_values_offset, template.outer_offset];
+ let stored = [(template.binding_values_offset, 8), (template.outer_offset, 8)];
let fits = template.allocator != 0
&& template_bytes <= u64::from(template.cell_size)
- && stored
- .iter()
- .all(|offset| offset.is_multiple_of(8) && u64::from(*offset) + 8 <= template_bytes);
+ && stored_fields_fit(&stored, template_bytes);
if !fits {
self.masm.jump(entry);
self.masm.bind(resume);
@@ -581,11 +579,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
self.emit_pop_free_list(template.allocator, environment, temp, entry);
self.emit_count_allocation(template.cell_size, template.cell_size, temp, false);
- let mut fields: Vec<ConstantField> = (0u32..)
- .zip(&template.words)
- .map(|(index, word)| (8 * index, 8, *word))
- .filter(|(offset, _, _)| !stored.contains(offset))
- .collect();
+ let mut fields = template_fields(&template.words, &stored);
self.emit_constant_fields(environment, temp, &mut fields)?;
if template.inline_binding_values {
self.masm
@@ -622,16 +616,14 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
let objects = self.runtime.object_allocation.clone();
let offsets = self.runtime.offsets;
let stored = [
- objects.named_properties_offset,
- offsets.ecmascript_function_environment,
- offsets.ecmascript_function_private_environment,
+ (objects.named_properties_offset, 8),
+ (offsets.ecmascript_function_environment, 4),
+ (offsets.ecmascript_function_private_environment, 4),
];
let template_bytes = 8 * closure.words.len() as u64;
let fits = info.allocator != 0
&& template_bytes <= u64::from(info.cell_size)
- && stored
- .iter()
- .all(|offset| offset.is_multiple_of(8) && u64::from(*offset) + 8 <= template_bytes);
+ && stored_fields_fit(&stored, template_bytes);
if !fits {
self.masm.jump(entry);
self.masm.bind(resume);
@@ -648,23 +640,56 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
self.emit_pop_free_list(info.allocator, function, temp, entry);
self.emit_count_allocation(info.cell_size, info.cell_size, temp, false);
- let mut fields: Vec<ConstantField> = (0u32..)
- .zip(&closure.words)
- .map(|(index, word)| (8 * index, 8, *word))
- .filter(|(offset, _, _)| !stored.contains(offset))
- .collect();
+ let mut fields = template_fields(&closure.words, &stored);
self.emit_constant_fields(function, temp, &mut fields)?;
self.masm
.load_effective_address(temp, &at(function, objects.inline_storage_offset));
self.masm.store64(&at(function, objects.named_properties_offset), temp);
- self.masm
- .store64(&at(function, offsets.ecmascript_function_environment), environment);
- self.masm.store64(
+ self.emit_store_compressed_cell(
+ &at(function, offsets.ecmascript_function_environment),
+ environment,
+ temp,
+ );
+ self.emit_store_compressed_cell(
&at(function, offsets.ecmascript_function_private_environment),
private_environment,
+ temp,
);
self.box_cell_with_tag(output, function, value::OBJECT_TAG);
self.masm.bind(resume);
Ok(())
}
}
+
+/// Whether the fields at `stored` (offsets and sizes of 4 or 8 bytes), which
+/// allocation stores itself, are aligned to their sizes and within the first
+/// `template_bytes` bytes of a cell.
+fn stored_fields_fit(stored: &[(u32, u32)], template_bytes: u64) -> bool {
+ stored.iter().all(|&(offset, size)| {
+ matches!(size, 4 | 8) && offset.is_multiple_of(size) && u64::from(offset) + u64::from(size) <= template_bytes
+ })
+}
+
+/// The constant fields of a cell image `words` without the fields at
+/// `stored` (see `stored_fields_fit()`): whole words, and the halves of the
+/// words that share theirs with a 4-byte stored field.
+fn template_fields(words: &[u64], stored: &[(u32, u32)]) -> Vec<ConstantField> {
+ let is_stored = |offset: u32, size: u32| {
+ stored
+ .iter()
+ .any(|&(start, length)| start < offset + size && offset < start + length)
+ };
+ let mut fields = Vec::with_capacity(words.len());
+ for (offset, word) in (0u32..).step_by(8).zip(words) {
+ if !is_stored(offset, 8) {
+ fields.push((offset, 8, *word));
+ continue;
+ }
+ for (half_offset, half) in [(offset, *word & 0xffff_ffff), (offset + 4, *word >> 32)] {
+ if !is_stored(half_offset, 4) {
+ fields.push((half_offset, 4, half));
+ }
+ }
+ }
+ fields
+}
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/call.rs b/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
index 6e5bbf9e9db..a67370ee528 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
@@ -464,12 +464,15 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
u32::from(layout.object_flag_is_ecmascript_function),
fail,
);
- self.masm.load64(
- scratch,
+ // OPTIMIZATION: The function's compressed shared data is the shared
+ // data's compressed pointer exactly when it is that
+ // shared data, so it is compared without decoding.
+ self.masm.branch32_memory_imm(
+ Condition::NotEqual,
&Address::new(temp, layout.ecmascript_function_shared_data as i32),
+ shared_data.compressed() as i32,
+ fail,
);
- self.masm.move_imm64(temp, shared_data.0);
- self.masm.branch64(Condition::NotEqual, scratch, temp, fail);
}
/// Branches to the dynamic call of `call` unless its callee is `target`,
@@ -680,13 +683,12 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
}
}
self.masm.store64(&field(offsets.execution_context_function), aux);
- self.masm
- .load64(temp, &Address::new(aux, offsets.ecmascript_function_environment as i32));
+ self.emit_load_compressed_cell(temp, &Address::new(aux, offsets.ecmascript_function_environment as i32));
self.masm
.store64(&field(offsets.execution_context_lexical_environment), temp);
self.masm
.store64(&field(offsets.execution_context_variable_environment), temp);
- self.masm.load64(
+ self.emit_load_nullable_compressed_cell(
temp,
&Address::new(aux, offsets.ecmascript_function_private_environment as i32),
);
@@ -1624,14 +1626,13 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
self.masm
.store64(&field(offsets.execution_context_script_or_module + word), temp);
}
- self.masm
- .load64(temp, &function_field(offsets.ecmascript_function_environment));
+ // NB: ECMAScript functions always have an environment.
+ self.emit_load_compressed_cell(temp, &function_field(offsets.ecmascript_function_environment));
self.masm
.store64(&field(offsets.execution_context_lexical_environment), temp);
self.masm
.store64(&field(offsets.execution_context_variable_environment), temp);
- self.masm
- .load64(temp, &function_field(offsets.ecmascript_function_private_environment));
+ self.emit_load_nullable_compressed_cell(temp, &function_field(offsets.ecmascript_function_private_environment));
self.masm
.store64(&field(offsets.execution_context_private_environment), temp);
// NB: Frames get their id when the debugger first needs it.
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/call_stub.rs b/Libraries/LibJS/JIT/Rust/src/codegen/call_stub.rs
index c8c0ebdbcc2..12054fa3d52 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/call_stub.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/call_stub.rs
@@ -156,13 +156,11 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
let prepared_this = self.stub_local(local::PREPARED_THIS);
let this_local = self.stub_local(local::THIS);
- self.masm
- .load64(template, &at(shared_data, layout.shared_data_call_environment_template));
- self.masm
- .branch_test64(Condition::Zero, template, u64::MAX, from_runtime);
- // NB: The template is a cell, whose pointer is decoded into the heap
- // region like a cell value.
- self.emit_unbox_cell(template, template);
+ self.emit_load_compressed_cell_or_branch(
+ template,
+ &at(shared_data, layout.shared_data_call_environment_template),
+ from_runtime,
+ );
// The `this` value: as is for strict callees and objects only for
// sloppy ones, like the runtime binds it then, or none.
@@ -231,7 +229,8 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
self.masm
.store64(&at(cell, layout.function_environment_binding_values), scratch);
self.masm.bind(outside_the_cell);
- self.masm.load64(
+ // NB: The outer environment is the callee's environment, which ECMAScript functions always have.
+ self.emit_load_compressed_cell(
scratch,
&at(function, self.runtime.offsets.ecmascript_function_environment),
);
@@ -346,12 +345,14 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
// An ECMAScript function the interpreter's call fast path calls in an
// inline frame.
self.masm.bind(ecmascript_function);
+ // NB: The executable is decoded once it is known not to be null,
+ // which it stays.
let load_function_data = |codegen: &mut Self| {
- codegen.masm.load64(
+ codegen.emit_load_compressed_cell(
other,
&Address::new(function_register, layout.ecmascript_function_shared_data as i32),
);
- codegen.masm.load64(
+ codegen.masm.load32(
executable_register,
&Address::new(other, layout.shared_data_executable as i32),
);
@@ -363,7 +364,9 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
load_function_data(self);
self.masm
.branch_test64(Condition::Zero, receiver, layout.metadata_can_inline_call, slow);
- self.masm.branch64_imm(Condition::Equal, executable_register, 0, slow);
+ self.masm
+ .branch_test32(Condition::Zero, executable_register, u32::MAX, slow);
+ self.emit_decompress_cell(executable_register);
let environment_local = self.stub_local(local::ENVIRONMENT);
self.masm.store_imm64(&environment_local, 0);
@@ -409,6 +412,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
self.masm.load64(temp, &callee_local);
self.emit_unbox_cell(function_register, temp);
load_function_data(self);
+ self.emit_decompress_cell(executable_register);
} else {
self.masm.branch_test64(
Condition::NonZero,
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
index d8fa6fa389b..b395f9a8595 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
@@ -106,6 +106,7 @@ use crate::regalloc::Location;
use crate::regalloc::Move;
use crate::regalloc::RegisterMask;
use crate::regalloc::RegisterSet;
+use crate::snapshot::COMPRESSED_POINTER_SHIFT;
use crate::snapshot::CellId;
use crate::snapshot::ExecutableSnapshot;
use crate::snapshot::RuntimeInfo;
@@ -1387,12 +1388,15 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
}
Op::LoadFunctionEnvironment { private } => {
let (output, function) = (self.output(node_id), self.input(node_id, 0));
- let offset = if *private {
- self.runtime.offsets.ecmascript_function_private_environment
+ let offsets = self.runtime.offsets;
+ if *private {
+ let address = Address::new(function, offsets.ecmascript_function_private_environment as i32);
+ self.emit_load_nullable_compressed_cell(output, &address);
} else {
- self.runtime.offsets.ecmascript_function_environment
- };
- self.masm.load64(output, &Address::new(function, offset as i32));
+ // NB: ECMAScript functions always have an environment.
+ let address = Address::new(function, offsets.ecmascript_function_environment as i32);
+ self.emit_load_compressed_cell(output, &address);
+ }
}
Op::CellAddress => {
// NB: Only objects get their cell addresses taken.
@@ -2093,6 +2097,47 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
self.masm.add64_imm(dst, dst, self.runtime.heap_region_base as i64);
}
+ /// `dst = heap_region_base + (dst << 3)`, the cell pointer the
+ /// compressed cell pointer in `dst` (zero-extended, not null) names,
+ /// which is in the heap region whatever its 32 bits are.
+ fn emit_decompress_cell(&mut self, dst: Gpr) {
+ self.masm.shl64_imm(dst, dst, COMPRESSED_POINTER_SHIFT);
+ self.masm.add64_imm(dst, dst, self.runtime.heap_region_base as i64);
+ }
+
+ /// Loads the compressed cell pointer field at `address`, which is not
+ /// null, into `dst` as a cell pointer.
+ fn emit_load_compressed_cell(&mut self, dst: Gpr, address: &Address) {
+ self.masm.load32(dst, address);
+ self.emit_decompress_cell(dst);
+ }
+
+ /// Loads the compressed cell pointer field at `address` into `dst` as a
+ /// cell pointer, or 0 if it is null.
+ fn emit_load_nullable_compressed_cell(&mut self, dst: Gpr, address: &Address) {
+ let null = self.masm.new_label();
+ self.masm.load32(dst, address);
+ self.masm.branch_test32(Condition::Zero, dst, u32::MAX, null);
+ self.emit_decompress_cell(dst);
+ self.masm.bind(null);
+ }
+
+ /// Loads the compressed cell pointer field at `address` into `dst` as a
+ /// cell pointer, or branches to `null` if it is null.
+ fn emit_load_compressed_cell_or_branch(&mut self, dst: Gpr, address: &Address, null: Label) {
+ self.masm.load32(dst, address);
+ self.masm.branch_test32(Condition::Zero, dst, u32::MAX, null);
+ self.emit_decompress_cell(dst);
+ }
+
+ /// Stores the cell pointer `cell` (or 0 for null) to the compressed cell
+ /// pointer field at `address`, through `temp`, which may be `cell`. The
+ /// heap region is aligned to its size, so compressing needs no base.
+ fn emit_store_compressed_cell(&mut self, address: &Address, cell: Gpr, temp: Gpr) {
+ self.masm.shr64_imm(temp, cell, COMPRESSED_POINTER_SHIFT);
+ self.masm.store32(address, temp);
+ }
+
/// The `Enter` bytecode: empty registers and locals, constants copied
/// into the frame, and the frame marked initialized (and published).
fn emit_initialize_frame(&mut self, node: NodeId, clobbers_floats: bool) -> Result<(), CompileFailure> {
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
index 962a1a82b54..5fe57f18a3e 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
@@ -572,6 +572,95 @@ fn runtime() -> RuntimeInfo {
}
}
+/// A stand-in for the GC heap region, for the fake cells of tests that hold
+/// compressed cell pointers, which only name cells in the region: a
+/// reservation as large as the runtime's, aligned to its size, whose start is
+/// committed. Code that runs on such cells decodes with the region's base
+/// (see `use_in()`), so cell values must point into the region as well.
+pub(super) mod fake_heap {
+ use crate::snapshot::COMPRESSED_POINTER_SHIFT;
+ use crate::snapshot::RuntimeInfo;
+ use std::sync::Mutex;
+ use std::sync::OnceLock;
+
+ const REGION_SIZE: usize = 1 << 35;
+ const COMMITTED_SIZE: usize = 64 << 20;
+ /// The bottom of the region is never a cell, so 0 is never a pointer.
+ const FIRST_CELL_OFFSET: usize = 4096;
+
+ struct Region {
+ base: usize,
+ next: Mutex<usize>,
+ }
+
+ fn region() -> &'static Region {
+ static REGION: OnceLock<Region> = OnceLock::new();
+ REGION.get_or_init(|| {
+ // SAFETY: A fresh reservation that nothing else uses.
+ unsafe {
+ let reservation = libc::mmap(
+ core::ptr::null_mut(),
+ 2 * REGION_SIZE,
+ libc::PROT_NONE,
+ libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | libc::MAP_NORESERVE,
+ -1,
+ 0,
+ );
+ assert_ne!(reservation, libc::MAP_FAILED);
+ let base = reservation.expose_provenance().next_multiple_of(REGION_SIZE);
+ let committed = libc::mprotect(
+ core::ptr::with_exposed_provenance_mut(base),
+ COMMITTED_SIZE,
+ libc::PROT_READ | libc::PROT_WRITE,
+ );
+ assert_eq!(committed, 0);
+ Region {
+ base,
+ next: Mutex::new(base + FIRST_CELL_OFFSET),
+ }
+ }
+ })
+ }
+
+ /// The region's base, which is aligned to its size.
+ pub fn base() -> u64 {
+ region().base as u64
+ }
+
+ /// Makes code of `runtime` decode cell values and compressed cell
+ /// pointers into the region.
+ pub fn use_in(runtime: &mut RuntimeInfo) {
+ runtime.heap_region_base = base();
+ runtime.heap_region_offset_mask = REGION_SIZE as u64 - 1;
+ }
+
+ /// A fake cell in the region with `words`, 16-byte aligned like cells.
+ /// It lives for the rest of the test process.
+ pub fn cell(words: &[u64]) -> &'static mut [u64] {
+ let region = region();
+ let mut next = region.next.lock().unwrap();
+ let address = *next;
+ *next = (address + 8 * words.len().max(1)).next_multiple_of(16);
+ assert!(*next <= region.base + COMMITTED_SIZE, "the fake heap is full");
+ // SAFETY: The words are committed, and no other cell has them.
+ let cell =
+ unsafe { core::slice::from_raw_parts_mut(core::ptr::with_exposed_provenance_mut(address), words.len()) };
+ cell.copy_from_slice(words);
+ cell
+ }
+
+ /// The compressed pointer to the cell at `address` in the region.
+ pub fn compressed(address: u64) -> u64 {
+ assert!(address >= base() && address - base() < REGION_SIZE as u64);
+ (address >> COMPRESSED_POINTER_SHIFT) & u64::from(u32::MAX)
+ }
+
+ /// The address of the cell the compressed pointer `compressed` names.
+ pub fn decompressed(compressed: u64) -> u64 {
+ base() + (compressed << COMPRESSED_POINTER_SHIFT)
+ }
+}
+
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct JitResult {
@@ -3117,7 +3206,8 @@ mod direct_calls {
/// The fake heap of one direct call test.
struct Callee {
function: Vec<u64>,
- executable: Vec<u64>,
+ /// In the fake heap region, for shared data that has it compressed.
+ executable: &'static mut [u64],
entry_table: Vec<u64>,
interpreter_stack: Vec<u64>,
}
@@ -3126,7 +3216,7 @@ mod direct_calls {
fn new() -> Self {
let mut callee = Self {
function: FUNCTION_WORDS.to_vec(),
- executable: vec![0; 7],
+ executable: fake_heap::cell(&[0; 7]),
entry_table: vec![0; 2 * ENTRY_TABLE_OWNERS_WORD],
interpreter_stack: vec![0xdead_beef_dead_beef; 512],
};
@@ -3544,12 +3634,16 @@ mod direct_calls {
}
/// A fake ECMAScript function for dynamic calls: shape, flags, shared
- /// data, then the words `FUNCTION_WORDS` has (see `dynamic_runtime()`).
+ /// data, then the words `FUNCTION_WORDS` has (see `dynamic_runtime()`),
+ /// whose environments are compressed. The function, its shared data and
+ /// its executable are in the fake heap region; shared data has the
+ /// executable compressed, then the metadata, then the compressed call
+ /// environment template.
struct DynamicCallee {
callee: Callee,
- function: Vec<u64>,
+ function: &'static mut [u64],
shape: Vec<u64>,
- shared_data: Vec<u64>,
+ shared_data: &'static mut [u64],
}
const ECMASCRIPT_FUNCTION_FLAG: u16 = 1 << 6;
@@ -3564,16 +3658,21 @@ mod direct_calls {
impl DynamicCallee {
fn new(formal_parameter_count: u64, flags: u64) -> Self {
- let mut callee = Callee::new();
+ let callee = Callee::new();
callee.executable[EXECUTABLE_ARGUMENTS_BASE_WORD] = 9;
let shape = vec![REALM];
- let shared_data = vec![callee.executable.as_ptr() as u64, formal_parameter_count | flags];
+ let shared_data = fake_heap::cell(&[
+ fake_heap::compressed(callee.executable.as_ptr() as u64),
+ formal_parameter_count | flags,
+ 0,
+ ]);
let mut function = vec![
shape.as_ptr() as u64,
u64::from(ECMASCRIPT_FUNCTION_FLAG),
- shared_data.as_ptr() as u64,
+ fake_heap::compressed(shared_data.as_ptr() as u64),
];
function.extend(FUNCTION_WORDS);
+ let function = fake_heap::cell(&function);
Self {
callee,
function,
@@ -3587,7 +3686,14 @@ mod direct_calls {
}
}
+ /// The environment a compressed environment word of `FUNCTION_WORDS`
+ /// names.
+ fn environment(word: u64) -> u64 {
+ fake_heap::decompressed(word)
+ }
+
fn dynamic_runtime(runtime: &mut crate::snapshot::RuntimeInfo, callee: &DynamicCallee) {
+ fake_heap::use_in(runtime);
runtime.offsets.ecmascript_function_environment = 24;
runtime.offsets.ecmascript_function_private_environment = 32;
runtime.offsets.ecmascript_function_script_or_module = 40;
@@ -3660,7 +3766,11 @@ mod direct_calls {
assert_eq!((word(80), word(88)), (FUNCTION_WORDS[2], FUNCTION_WORDS[3]));
assert_eq!(
(word(8), word(96), word(16)),
- (FUNCTION_WORDS[0], FUNCTION_WORDS[0], FUNCTION_WORDS[1])
+ (
+ environment(FUNCTION_WORDS[0]),
+ environment(FUNCTION_WORDS[0]),
+ environment(FUNCTION_WORDS[1])
+ )
);
assert_eq!(word(104), 0, "no frame id yet");
assert_eq!(word(120), value::EMPTY, "no this value");
@@ -3812,7 +3922,7 @@ mod direct_calls {
(ENVIRONMENT, ENVIRONMENT),
"environments"
);
- assert_eq!(frame[16 / 8], FUNCTION_WORDS[1], "private environment");
+ assert_eq!(frame[16 / 8], environment(FUNCTION_WORDS[1]), "private environment");
assert_eq!(frame[120 / 8], value::int32(5), "this");
assert_eq!(
frame[FRAME_HEADER_WORDS + crate::bytecode::THIS_VALUE_REGISTER as usize],
@@ -3861,15 +3971,14 @@ mod direct_calls {
let free_lists = vec![0, 0, free_list.as_mut_ptr() as u64, 0];
let mut template = vec![6, 64, if case.inline { 48 } else { 0 }, u64::from(case.binds_this)];
template.extend([0x1000, 0x1001, 0, 0, 0, 0]);
- callee
- .shared_data
- // NB: Bits above the heap region, which the stub drops.
- .push(if case.template {
- (0xbad << 48) | template.as_ptr() as u64
+ let template = fake_heap::cell(&template);
+ // NB: Bits in the other half of the word, which the stub ignores.
+ callee.shared_data[2] = (0xbad << 32)
+ | if case.template {
+ fake_heap::compressed(template.as_ptr() as u64)
} else {
0
- });
- callee.function[2] = callee.shared_data.as_ptr() as u64;
+ };
let arguments = [callee.value(), value::int32(7), case.this_value];
let (snapshot, compiled) = compile_program_with(&caller(), caller_layout(), &[], |snapshot| {
dynamic_runtime(&mut snapshot.runtime, &callee);
@@ -3945,7 +4054,7 @@ mod direct_calls {
assert_eq!(frame[120 / 8], this, "this");
assert_eq!(&cell[..2], &[0x1000, 0x1001]);
assert_eq!(cell[2], if in_cell { address + 48 } else { 0 });
- assert_eq!(cell[3], FUNCTION_WORDS[0], "outer environment");
+ assert_eq!(cell[3], environment(FUNCTION_WORDS[0]), "outer environment");
assert_eq!(cell[4], callee.function.as_ptr() as u64, "function object");
assert_eq!(cell[5], if binds_this { value::int32(3) } else { 0 });
assert_eq!(heap, [164, 10_000, 1064]);
@@ -4024,7 +4133,7 @@ mod direct_calls {
let mut callee = DynamicCallee::new(0, 0);
let shape = vec![REALM];
// Native function index 1, with a bit above the table's index mask that the call stub drops.
- let native = vec![shape.as_ptr() as u64, u64::from(RAW_NATIVE_FLAG), 0x1_0001];
+ let native = fake_heap::cell(&[shape.as_ptr() as u64, u64::from(RAW_NATIVE_FLAG), 0x1_0001]);
let table = vec![0, 0, fake_native as *const () as u64, 0];
let native_value = (u64::from(value::OBJECT_TAG) << 48) | native.as_ptr() as u64;
let call_function = (u64::from(value::OBJECT_TAG) << 48) | CALL_FUNCTION;
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index 87f419f64c6..dd43d57f9c8 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -26,6 +26,20 @@ use crate::code::ExitKind;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct CellId(pub u64);
+/// How far a cell's address is shifted right to compress it: cells store
+/// pointers to other cells as the low 32 bits of the shifted address (their
+/// offset into the 32 GiB heap region, which is aligned to its size, in units
+/// of the 8-byte cell alignment), and 0 for null.
+pub const COMPRESSED_POINTER_SHIFT: u8 = 3;
+
+impl CellId {
+ /// The cell's compressed pointer, which a field that holds the cell
+ /// compares equal to without decoding.
+ pub fn compressed(self) -> u32 {
+ (self.0 >> COMPRESSED_POINTER_SHIFT) as u32
+ }
+}
+
/// One bytecode executable: the function being compiled (index 0) or an
/// inlining candidate.
///
diff --git a/Libraries/LibJS/Rust/generate/layout_table.rs b/Libraries/LibJS/Rust/generate/layout_table.rs
index 038a6237016..54971badd7e 100644
--- a/Libraries/LibJS/Rust/generate/layout_table.rs
+++ b/Libraries/LibJS/Rust/generate/layout_table.rs
@@ -490,14 +490,14 @@ pub fn generate(configuration: &LayoutConfiguration) -> LayoutWriter {
field!(w, "DIRECT_GETTER_FUNCTION_WEAK_IMPL_VALUE_WORD_OFFSET", "DirectGetterFunction.weak_impl_value_word_offset", "u32", DirectGetterFunction, weak_impl_value_word_offset, 4, "nullable", "scalar");
w.section("ECMAScriptFunctionObject layout");
- field!(w, "ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA", "ECMAScriptFunctionObject.shared_data", "SharedFunctionInstanceData", EcmascriptFunctionObject, shared_data, 8, "nonnull", "cell");
- field!(w, "ECMASCRIPT_FUNCTION_OBJECT_ENVIRONMENT", "ECMAScriptFunctionObject.environment", "Environment", EcmascriptFunctionObject, environment, 8, "nullable", "cell", "environment_state");
- field!(w, "ECMASCRIPT_FUNCTION_OBJECT_PRIVATE_ENVIRONMENT", "ECMAScriptFunctionObject.private_environment", "PrivateEnvironment", EcmascriptFunctionObject, private_environment, 8, "nullable", "cell", "environment_state");
+ field!(w, "ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA", "ECMAScriptFunctionObject.shared_data", "SharedFunctionInstanceData", FunctionObject, ecmascript_shared_data, 4, "nonnull", "compressed");
+ field!(w, "ECMASCRIPT_FUNCTION_OBJECT_ENVIRONMENT", "ECMAScriptFunctionObject.environment", "Environment", EcmascriptFunctionObject, environment, 4, "nullable", "compressed");
+ field!(w, "ECMASCRIPT_FUNCTION_OBJECT_PRIVATE_ENVIRONMENT", "ECMAScriptFunctionObject.private_environment", "PrivateEnvironment", EcmascriptFunctionObject, private_environment, 4, "nullable", "compressed");
field!(w, "ECMASCRIPT_FUNCTION_OBJECT_SCRIPT_OR_MODULE", "ECMAScriptFunctionObject.script_or_module", "ScriptOrModule", EcmascriptFunctionObject, script_or_module, 16, "nullable", "scalar");
w.section("SharedFunctionInstanceData layout");
- field!(w, "SHARED_FUNCTION_INSTANCE_DATA_EXECUTABLE", "SharedFunctionInstanceData.executable", "Executable", SharedFunctionInstanceData, executable, 8, "nullable", "cell", "call_data");
- field!(w, "SHARED_FUNCTION_INSTANCE_DATA_ASM_CALL_METADATA", "SharedFunctionInstanceData.asm_call_metadata", "u64", SharedFunctionInstanceData, asm_call_metadata, 8, "nullable", "scalar", "call_data");
+ field!(w, "SHARED_FUNCTION_INSTANCE_DATA_EXECUTABLE", "SharedFunctionInstanceData.executable", "Executable", SharedFunctionInstanceData, executable, 4, "nullable", "compressed");
+ field!(w, "SHARED_FUNCTION_INSTANCE_DATA_ASM_CALL_METADATA", "SharedFunctionInstanceData.asm_call_metadata", "u64", SharedFunctionInstanceData, asm_call_metadata, 8, "nullable", "scalar");
offset!(w, "SHARED_FUNCTION_INSTANCE_DATA_FORMAL_PARAMETER_COUNT", SharedFunctionInstanceData, formal_parameter_count);
offset!(w, "SHARED_FUNCTION_INSTANCE_DATA_STRICT", SharedFunctionInstanceData, strict);
offset!(w, "SHARED_FUNCTION_INSTANCE_DATA_FUNCTION_ENVIRONMENT_NEEDED", SharedFunctionInstanceData, function_environment_needed);
diff --git a/Libraries/LibJS/Rust/src/jit/allocation.rs b/Libraries/LibJS/Rust/src/jit/allocation.rs
index a30f3859fbc..8f86543cd28 100644
--- a/Libraries/LibJS/Rust/src/jit/allocation.rs
+++ b/Libraries/LibJS/Rust/src/jit/allocation.rs
@@ -268,8 +268,7 @@ impl FunctionEnvironmentFreeLists {
/// of it, if they have none yet and JIT code can allocate them itself.
pub fn make_call_environment_template(vm: &Vm, function: Gc<EcmascriptFunctionObject>, environment: Gc<Environment>) {
let shared_data = function.shared_data();
- // SAFETY: Nothing else refers to the template while this looks at whether there is one.
- if unsafe { &*shared_data.call_environment_template.as_ptr() }.is_some() {
+ if !shared_data.call_environment_template.is_null() {
return;
}
let Some(environment) = environment.downcast::<FunctionEnvironment>() else {
@@ -555,7 +554,7 @@ pub extern "C" fn libjs_jit_clone_function(
let closure = EcmascriptFunctionObject::create_from_function_data_with_prototype(
vm,
realm,
- sample.shared_data.get(),
+ sample.shared_data(),
environment,
private_environment,
realm.function_prototype(),
diff --git a/Libraries/LibJS/Rust/src/jit/runtime_info.rs b/Libraries/LibJS/Rust/src/jit/runtime_info.rs
index c6174bd1905..f217ef9495b 100644
--- a/Libraries/LibJS/Rust/src/jit/runtime_info.rs
+++ b/Libraries/LibJS/Rust/src/jit/runtime_info.rs
@@ -71,6 +71,10 @@ use crate::runtime::object::{ORDINARY_OBJECT_METHODS, ObjectMethods, PropertyKin
use crate::runtime::string_object::StringObject;
use crate::runtime::string_prototype::StringPrototype;
+// JIT code compresses and decodes the compressed cell pointers of cells like the runtime does.
+const _: () =
+ assert!(libjs_jit::snapshot::COMPRESSED_POINTER_SHIFT as u32 == crate::layout::member::COMPRESSED_POINTER_SHIFT);
+
/// The addresses of the runtime functions JIT code calls, which every compile job's runtime info has.
pub struct RuntimeFunctionAddresses {
/// The address of the slow path JIT code calls for each generic opcode, indexed by opcode, or 0 for opcodes
@@ -558,7 +562,7 @@ fn dynamic_call_layout(vm: &Vm) -> DynamicCallLayout {
.expect("the VM has a JIT entry table while the JIT is on");
DynamicCallLayout {
object_flag_is_ecmascript_function: object_flag::IS_ECMASCRIPT_FUNCTION_OBJECT,
- ecmascript_function_shared_data: offset(offset_of!(EcmascriptFunctionObject, shared_data)),
+ ecmascript_function_shared_data: offset(offset_of!(FunctionObject, ecmascript_shared_data)),
shared_data_executable: offset(offset_of!(SharedFunctionInstanceData, executable)),
shared_data_asm_call_metadata: offset(offset_of!(SharedFunctionInstanceData, asm_call_metadata)),
executable_jit_entry_slot: offset(offset_of!(ExecutableHead, jit_entry_slot)),
diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rs
index 7d1f21be47a..a739656cc6e 100644
--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs
+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs
@@ -362,8 +362,12 @@ fn function_frame_fields(function: Gc<EcmascriptFunctionObject>) -> FunctionFram
word_of(function, offset_of!(EcmascriptFunctionObject, script_or_module)),
word_of(function, offset_of!(EcmascriptFunctionObject, script_or_module) + 8),
],
- environment: word_of(function, offset_of!(EcmascriptFunctionObject, environment)),
- private_environment: word_of(function, offset_of!(EcmascriptFunctionObject, private_environment)),
+ environment: function
+ .environment()
+ .map_or(0, |environment| environment.as_ptr() as u64),
+ private_environment: function
+ .private_environment()
+ .map_or(0, |environment| environment.as_ptr() as u64),
}
}
diff --git a/Libraries/LibJS/Rust/src/layout/function_object.rs b/Libraries/LibJS/Rust/src/layout/function_object.rs
index 454ec1aea38..ed2398bf38f 100644
--- a/Libraries/LibJS/Rust/src/layout/function_object.rs
+++ b/Libraries/LibJS/Rust/src/layout/function_object.rs
@@ -10,6 +10,7 @@ use super::cell::{CellHeader, Gc};
use super::environment::{DeclarativeEnvironment, Environment, PrivateEnvironment};
use super::executable::ExecutableHead;
use super::execution_context::ScriptOrModule;
+use super::member::NullableMember;
use super::object::Object;
use super::realm::Realm;
use crate::layout_forward::{
@@ -25,6 +26,9 @@ pub struct FunctionObject {
/// Flags of the classes extending FunctionObject, in what would otherwise be padding (see
/// ecmascript_function_flags).
pub flags: Cell<u8>,
+ /// The shared data of an ECMAScript function object, in what would otherwise be padding (null for other
+ /// functions). See EcmascriptFunctionObject::shared_data().
+ pub ecmascript_shared_data: NullableMember<SharedFunctionInstanceData>,
}
const _: () = assert!(size_of::<FunctionObject>() == 80);
@@ -75,10 +79,9 @@ pub struct NativeFunctionTableEntry {
#[repr(C)]
pub struct EcmascriptFunctionObject {
pub base: FunctionObject,
- pub shared_data: Cell<Gc<SharedFunctionInstanceData>>,
+ pub environment: NullableMember<Environment>,
+ pub private_environment: NullableMember<PrivateEnvironment>,
pub name: FlyStringSlot,
- pub environment: Cell<Option<Gc<Environment>>>,
- pub private_environment: Cell<Option<Gc<PrivateEnvironment>>>,
pub script_or_module: Cell<ScriptOrModule>,
pub storage: EcmascriptFunctionObjectStorage,
}
@@ -100,7 +103,7 @@ pub mod asm_call_metadata {
#[repr(C)]
pub struct SharedFunctionInstanceData {
pub header: CellHeader,
- pub executable: Cell<Option<Gc<ExecutableHead>>>,
+ pub executable: NullableMember<ExecutableHead>,
pub asm_call_metadata: Cell<u64>,
pub formal_parameter_count: Cell<u32>,
pub strict: Cell<bool>,
@@ -108,7 +111,7 @@ pub struct SharedFunctionInstanceData {
pub uses_this: Cell<bool>,
pub can_inline_call: Cell<bool>,
/// How JIT code allocates the function environments of calls of the functions, once the runtime made one.
- pub call_environment_template: Cell<Option<Gc<CallEnvironmentTemplate>>>,
+ pub call_environment_template: NullableMember<CallEnvironmentTemplate>,
pub storage: SharedFunctionInstanceDataStorage,
}
diff --git a/Libraries/LibJS/Rust/src/layout/member.rs b/Libraries/LibJS/Rust/src/layout/member.rs
index 3e30a5e5fa4..d0c297aa8ba 100644
--- a/Libraries/LibJS/Rust/src/layout/member.rs
+++ b/Libraries/LibJS/Rust/src/layout/member.rs
@@ -180,6 +180,20 @@ impl<T> NullableMember<T> {
Some(unsafe { Gc::from_non_null(NonNull::new_unchecked(core::ptr::without_provenance_mut(address))) })
}
+ /// The cell of a member that is known not to be null, without checking.
+ ///
+ /// # Safety
+ ///
+ /// The member must not be null.
+ #[inline(always)]
+ pub unsafe fn get_unchecked(&self) -> Gc<T> {
+ debug_assert!(!self.is_null());
+ let address = decompress_address(self.compressed());
+ // SAFETY: The member was set to a cell, which a member keeps alive. A compressed pointer that is not null
+ // decodes to an address in the heap region, which is never null.
+ unsafe { Gc::from_non_null(NonNull::new_unchecked(core::ptr::without_provenance_mut(address))) }
+ }
+
#[inline(always)]
pub fn set(&self, cell: Option<Gc<T>>) {
let compressed = Self::compress(cell);
diff --git a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
index c14c3dd80aa..f5779244e26 100644
--- a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
@@ -25,6 +25,7 @@ use crate::layout::environment::{Environment, PrivateEnvironment};
use crate::layout::execution_context::{ExecutionContext, ScriptOrModule};
pub use crate::layout::function_object::EcmascriptFunctionObject;
use crate::layout::function_object::ecmascript_function_flags;
+use crate::layout::member::NullableMember;
use crate::layout::object::{IndexedStorageKind, Object};
use crate::layout::value::Value;
use crate::layout_forward::FlyStringSlot;
@@ -75,7 +76,7 @@ pub struct EcmascriptFunctionObjectStorage {
}
// NB: Every ECMAScript function has these bytes, so keep what only some functions need in the rare data.
-const _: () = assert!(size_of::<EcmascriptFunctionObject>() == 136);
+const _: () = assert!(size_of::<EcmascriptFunctionObject>() == 120);
// SAFETY: Visits the cells of the rare data, if there is any.
unsafe impl Trace for EcmascriptFunctionObjectStorage {
@@ -118,7 +119,7 @@ unsafe impl Trace for EcmascriptFunctionObject {
self.base.trace(visitor);
self.environment.trace(visitor);
self.private_environment.trace(visitor);
- self.shared_data.trace(visitor);
+ visitor.visit_nullable_member(&self.base.ecmascript_shared_data);
self.storage.trace(visitor);
match self.script_or_module.get() {
ScriptOrModule::Empty => {}
@@ -216,6 +217,7 @@ impl EcmascriptFunctionObject {
let base =
FunctionObject::new_with_prototype(vm, Self::CLASS, prototype, MayInterfereWithIndexedPropertyAccess::No);
base.set_is_ecmascript_function_object();
+ base.ecmascript_shared_data.set(Some(shared_data));
// OPTIMIZATION: Start from a premade shape that already has this function kind's own properties in spec order.
// Arrow functions use the same shape as other functions of their kind, but never get a lazy prototype.
@@ -239,10 +241,9 @@ impl EcmascriptFunctionObject {
Self {
base,
- shared_data: Cell::new(shared_data),
+ environment: NullableMember::new(parent_environment),
+ private_environment: NullableMember::new(private_environment),
name: FlyStringSlot::new(None),
- environment: Cell::new(parent_environment),
- private_environment: Cell::new(private_environment),
script_or_module: Cell::new(script_or_module),
storage: EcmascriptFunctionObjectStorage::default(),
}
@@ -800,7 +801,8 @@ impl EcmascriptFunctionObject {
}
pub fn shared_data(&self) -> Gc<SharedFunctionInstanceData> {
- self.shared_data.get()
+ // SAFETY: An ECMAScript function object gets its shared data when it is created, and keeps it.
+ unsafe { self.base.ecmascript_shared_data.get_unchecked() }
}
pub fn is_module_wrapper(&self) -> bool {
diff --git a/Libraries/LibJS/Rust/src/runtime/function_object.rs b/Libraries/LibJS/Rust/src/runtime/function_object.rs
index 4bbd4cf07df..603b7372580 100644
--- a/Libraries/LibJS/Rust/src/runtime/function_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/function_object.rs
@@ -15,6 +15,7 @@ use crate::gc::visitor::{Trace, Visitor};
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
pub use crate::layout::function_object::FunctionObject;
+use crate::layout::member::NullableMember;
use crate::layout::object::Object;
use crate::layout::value::Value;
use crate::runtime::class_field_definition::ClassElementName;
@@ -95,6 +96,7 @@ impl FunctionObject {
builtin: Cell::new(0),
has_builtin: Cell::new(false),
flags: Cell::new(0),
+ ecmascript_shared_data: NullableMember::null(),
}
}
diff --git a/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs b/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs
index 2450ac52ab6..e39dc347f99 100644
--- a/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs
+++ b/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs
@@ -143,7 +143,7 @@ pub struct SharedFunctionInstanceDataStorage {
}
// NB: Every function has these bytes.
-const _: () = assert!(size_of::<SharedFunctionInstanceData>() == 280);
+const _: () = assert!(size_of::<SharedFunctionInstanceData>() == 272);
/// The most properties that constructions make room for ahead (the largest inline capacity of plain objects).
const MAX_CONSTRUCT_RESERVE: u32 = 16;
@@ -223,14 +223,14 @@ impl SharedFunctionInstanceData {
Self {
header: CellHeader::for_class(Self::CLASS),
- executable: Cell::new(None),
+ executable: NullableMember::null(),
asm_call_metadata: Cell::new(0),
formal_parameter_count: Cell::new(formal_parameter_count),
strict: Cell::new(strict),
function_environment_needed: Cell::new(false),
uses_this: Cell::new(false),
can_inline_call: Cell::new(false),
- call_environment_template: Cell::new(None),
+ call_environment_template: NullableMember::null(),
storage: SharedFunctionInstanceDataStorage {
name,
source_code: RefCell::new(None),
diff --git a/Meta/Linters/check_rust_process_wide_state.py b/Meta/Linters/check_rust_process_wide_state.py
index ba5dd086889..a765803014c 100755
--- a/Meta/Linters/check_rust_process_wide_state.py
+++ b/Meta/Linters/check_rust_process_wide_state.py
@@ -34,6 +34,7 @@ ALLOWED = {
"Libraries/LibCompositing/Rust/src/display_list/replay.rs:WARM_REPLAY_SCRATCH_STORAGE": "per-thread replay scratch; a second copy costs memory, never an answer",
"Libraries/LibJS/Flap/src/low_ir/lowering.rs:LABELS": "per-thread scratch buffer; a second copy costs memory, never an answer",
"Libraries/LibJS/JIT/Rust/src/asm/x86_64/sse.rs:FEATURES": "immutable CPU/OS feature cache; each copy computes the same supported instructions",
+ "Libraries/LibJS/JIT/Rust/src/codegen/tests.rs:REGION": "the fake heap region of JIT unit tests, in the cargo test binary that has no runtime",
"Libraries/LibWeb/HTML/Parser/Rust/src/token.rs:SPARE_ATTRIBUTE_LISTS": "per-thread allocation pool; a second copy costs memory, never an answer",
"Libraries/LibWeb/HTML/Parser/Rust/src/token.rs:SPARE_ATTRIBUTE_VALUES": "per-thread allocation pool; a second copy costs memory, never an answer",
}