From cb1036d4c816132e321e44e5c267f075fc4beffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ralph=20K=C3=BCpper?= Date: Tue, 7 Jul 2026 23:03:30 +0200 Subject: [PATCH] fix(codegen): ILP32 class-field access must use target-aware object-header size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The inline class-field get/set fast paths hardcoded a 24-byte ObjectHeader skip when computing a field's address (`obj + 24 + slot*8`). On arm64_32 (ILP32: 32-bit pointers) the runtime `ObjectHeader` is 20 bytes (4×u32 + a 4-byte `keys_array` pointer), so these paths read and wrote every class field 4 bytes off — while the generic-PIC load, the runtime setters, and the GC all correctly derive the size from the target. Number fields survived because their read AND write both used the hardcoded 24, staying self-consistent. But typed-object *string* fields came back as a 32-bit word-swapped NaN-box: the write went through the class-field path (24) while the read fell through to the generic PIC (target-aware 20) — a 4-byte skew that straddles two slots. `interface LevelInfo { label: string; … }` read `levels[i].label` back as a garbage float (~1.5e-202) on Apple Watch Series 4–8 / SE. Derive the skip from `object_header_size_bytes(ctx.target_triple)` (20 on ILP32, 24 on LP64 — a no-op on 64-bit), matching every other field-access path. Verified on the arm64_32 IR: class-field get/set now emit `getelementptr i8, ptr, i64 20`, identical to the generic PIC's base. --- crates/perry-codegen/src/expr/property_get.rs | 14 ++++++++++++-- crates/perry-codegen/src/expr/property_set.rs | 12 +++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/crates/perry-codegen/src/expr/property_get.rs b/crates/perry-codegen/src/expr/property_get.rs index 2b30228ae04..2785b395178 100644 --- a/crates/perry-codegen/src/expr/property_get.rs +++ b/crates/perry-codegen/src/expr/property_get.rs @@ -1325,10 +1325,20 @@ pub(crate) fn lower(ctx: &mut FnCtx<'_>, expr: &Expr) -> Result { .cond_br(&guard_pass, &fast_label, &fallback_label); ctx.current_block = fast_idx; + // arm64_32 watchOS: the object fields region begins at + // `size_of::()` past the user pointer — 24 on + // 64-bit, 20 on ILP32 (the trailing `keys_array` pointer is 4 + // bytes there). A hardcoded 24 reads every class field 4 bytes + // off on a 32-bit watch, so this inline class-field load + // disagreed with the generic-PIC load / runtime setter (both + // target-aware) and typed-object string fields came back as + // word-swapped NaN-boxes. Derive it from the target triple + // (no-op on 64-bit; see `target_layout`). + let header_skip = + crate::target_layout::object_header_size_bytes(ctx.target_triple) + .to_string(); let blk = ctx.block(); let obj_ptr = blk.inttoptr(I64, &obj_handle); - // Skip the 24-byte ObjectHeader. - let header_skip = "24".to_string(); let fields_base = blk.gep(I8, &obj_ptr, &[(I64, &header_skip)]); let field_ptr = blk.gep(DOUBLE, &fields_base, &[(I64, &field_idx_str)]); let val_fast = blk.load(DOUBLE, &field_ptr); diff --git a/crates/perry-codegen/src/expr/property_set.rs b/crates/perry-codegen/src/expr/property_set.rs index 3e5e8b75c32..53e384f0c2c 100644 --- a/crates/perry-codegen/src/expr/property_set.rs +++ b/crates/perry-codegen/src/expr/property_set.rs @@ -650,9 +650,19 @@ pub(crate) fn lower(ctx: &mut FnCtx<'_>, expr: &Expr) -> Result { let field_set_barrier_needed = !expr_produces_non_pointer_bits_by_construction(ctx, value); let raw_stored_value = { + // arm64_32 watchOS: the object fields region begins at + // `size_of::()` past the user pointer — 24 on + // 64-bit, 20 on ILP32 (the trailing `keys_array` pointer is + // 4 bytes there). A hardcoded 24 writes every class field 4 + // bytes off on a 32-bit watch; the paired inline read + // (`property_get`) and the runtime setter must agree, so + // derive it from the target triple (no-op on 64-bit; see + // `target_layout`). + let header_skip = + crate::target_layout::object_header_size_bytes(ctx.target_triple) + .to_string(); let blk = ctx.block(); let obj_ptr = blk.inttoptr(I64, &obj_handle); - let header_skip = "24".to_string(); let fields_base = blk.gep(I8, &obj_ptr, &[(I64, &header_skip)]); let field_ptr = blk.gep(DOUBLE, &fields_base, &[(I64, &field_idx_str)]); let raw_stored_value = if requires_raw_f64 {