Truly independent web browser
LibWasm: Store GC fields and elements at their natural size
Andreas Klingf963a8c07ce8
diff --git a/Libraries/LibWasm/Engine/src/runtime/gc.rs b/Libraries/LibWasm/Engine/src/runtime/gc.rsindex f68ab33523e..dff2a245fa5 100644--- a/Libraries/LibWasm/Engine/src/runtime/gc.rs+++ b/Libraries/LibWasm/Engine/src/runtime/gc.rs@@ -24,7 +24,7 @@ use crate::abi::{RTT_KIND_STRUCT, Rtt, STORAGE_32, STORAGE_64, STORAGE_FUNCTION_REFERENCE, STORAGE_I8, STORAGE_I16,STORAGE_REFERENCE, STORAGE_V128,};-use crate::types::{CompositeType, HeapType, StorageType, SubType, ValType};+use crate::types::{CompositeType, HeapType, StorageType, SubType, ValType, struct_layout};use core::ffi::c_void;const STORE_ROOT_STORE_OFFSET: usize = 16;@@ -121,13 +121,8 @@ pub fn allocate_rtt(store: &Store, identity: u32) -> *const Rtt {.iter().map(|field| storage_kind(field.storage, &store.types)).collect();- let mut offsets = Vec::with_capacity(kinds.len());- let mut offset = 0;- for field in fields {- offsets.push(offset);- offset += field.storage.object_field_size();- }- (RTT_KIND_STRUCT, kinds, offsets, offset)+ let (offsets, size) = struct_layout(fields);+ (RTT_KIND_STRUCT, kinds, offsets, size)}CompositeType::Array(element) => {let kind = storage_kind(element.storage, &store.types);@@ -299,8 +294,8 @@ unsafe fn numeric_array_element(array: u64, index: u32) -> (*mut u8, u8) {/// `array` must be a live array reference.pub unsafe fn array_element(array: u64, index: u32) -> u64 {unsafe {- let (address, _) = numeric_array_element(array, index);- address.cast::<u64>().read()+ let (address, kind) = numeric_array_element(array, index);+ read_number(address, kind)}}@@ -312,13 +307,31 @@ pub unsafe fn array_element(array: u64, index: u32) -> u64 {pub unsafe fn set_array_element(array: u64, index: u32, bits: u64) {unsafe {let (address, kind) = numeric_array_element(array, index);- let mask = match kind {- STORAGE_I8 => 0xff,- STORAGE_I16 => 0xffff,- STORAGE_32 => 0xffff_ffff,- _ => u64::MAX,- };- address.cast::<u64>().write(bits & mask);+ write_number(address, kind, bits);+ }+}++/// The bits of the number of storage `kind` (neither a reference nor a v128) at `address`, zero-extended.+unsafe fn read_number(address: *const u8, kind: u8) -> u64 {+ unsafe {+ match kind {+ STORAGE_I8 => u64::from(address.read()),+ STORAGE_I16 => u64::from(address.cast::<u16>().read()),+ STORAGE_32 => u64::from(address.cast::<u32>().read()),+ _ => address.cast::<u64>().read(),+ }+ }+}++/// Store the low bits of `bits` that a field or element of storage `kind` (not a v128) holds at `address`.+unsafe fn write_number(address: *mut u8, kind: u8, bits: u64) {+ unsafe {+ match kind {+ STORAGE_I8 => address.write(bits as u8),+ STORAGE_I16 => address.cast::<u16>().write(bits as u16),+ STORAGE_32 => address.cast::<u32>().write(bits as u32),+ _ => address.cast::<u64>().write(bits),+ }}}@@ -510,15 +523,12 @@ impl Store {unsafe fn store_field(address: *mut u8, kind: u8, value: Value) {let (slots, _) = value.to_slots();- let mask = match kind {- STORAGE_I8 => 0xff,- STORAGE_I16 => 0xffff,- _ => u64::MAX,- };unsafe {- address.cast::<u64>().write(slots[0] & mask);if kind == STORAGE_V128 {+ address.cast::<u64>().write(slots[0]);address.cast::<u64>().add(1).write(slots[1]);+ } else {+ write_number(address, kind, slots[0]);}}}diff --git a/Libraries/LibWasm/Interpreter/interpreter.flap b/Libraries/LibWasm/Interpreter/interpreter.flapindex 84eb9026794..fdb36ff0974 100644--- a/Libraries/LibWasm/Interpreter/interpreter.flap+++ b/Libraries/LibWasm/Interpreter/interpreter.flap@@ -7245,18 +7245,6 @@ inline fn rtt_is_subtype(subtype: u64, identity: u64) -> u64 {result}-# A field value as it is stored: packed fields keep only their bits.-inline fn packed_value(value: u64, kind: u64) -> u64 {- let mut stored = value;- if kind == STORAGE_I8 {- stored = value & 0xff;- }- if kind == STORAGE_I16 {- stored = value & 0xffff;- }- stored-}-# A packed value sign-extended to an i32.inline fn sign_extend_packed(value: u64, kind: u64) -> u64 {let mut shift: u64 = 48;@@ -7271,10 +7259,46 @@ inline fn sign_extend_packed(value: u64, kind: u64) -> u64 {result}+# The bits of the field or element at `address`, whose storage is `kind`, zero-extended (the low half of a v128).+inline fn load_stored(address: u64, kind: u64) -> u64 {+ let mut value: u64 = 0;+ if kind == STORAGE_I8 {+ load8(value, [address, 0]);+ } else {+ if kind == STORAGE_I16 {+ load16(value, [address, 0]);+ } else {+ if kind == STORAGE_32 {+ load32(value, [address, 0]);+ } else {+ load64(value, [address, 0]);+ }+ }+ }+ value+}++# Store the bits of `value` that a field or element at `address` whose storage is `kind`, not v128, holds.+inline fn store_number(address: u64, kind: u64, value: u64) {+ if kind == STORAGE_I8 {+ store8([address, 0], value);+ } else {+ if kind == STORAGE_I16 {+ store16([address, 0], value);+ } else {+ if kind == STORAGE_32 {+ store32([address, 0], value);+ } else {+ store64([address, 0], value);+ }+ }+ }+}+# Push the value of the field or element at `address`, whose storage is `kind`, replacing `popped` slots.inline fn push_stored(address: u64, kind: u64, popped: u64) {- let mut low: u64;- load64(low, [address, 0]);+ # The low half of a v128 is a load of 64 bits too.+ let low = load_stored(address, kind);if kind == STORAGE_V128 {let mut high: u64;load64(high, [address, 8]);@@ -7295,8 +7319,7 @@ inline fn pop_into_storage(address: u64, kind: u64) {store64([address, 0], low);store64([address, 8], high);} else {- let stored = packed_value(low, kind);- store64([address, 0], stored);+ store_number(address, kind, low);}}@@ -7307,27 +7330,20 @@ inline fn pop_storage_value(kind: u64) -> (u64, u64) {if kind == STORAGE_V128 {high = pop();}- let stored = packed_value(low, kind);- (stored, high)+ (low, high)}inline fn store_storage_value(address: u64, kind: u64, low: u64, high: u64) {- store64([address, 0], low);if kind == STORAGE_V128 {+ store64([address, 0], low);store64([address, 8], high);+ } else {+ store_number(address, kind, low);}}+# The bytes an element of `kind` takes, in an array and in a data segment alike.inline fn storage_size(kind: u64) -> u64 {- let mut size: u64 = 8;- if kind == STORAGE_V128 {- size = 16;- }- size-}--# The bytes an element of `kind` takes in a data segment.-inline fn data_size(kind: u64) -> u64 {let mut size: u64 = 8;if kind == STORAGE_I8 {size = 1;@@ -7344,35 +7360,10 @@ inline fn data_size(kind: u64) -> u64 {size}-# Copy `count` elements of `kind` from a data segment's bytes at `source` into elements at `destination`.+# Copy `count` elements of `kind` from a data segment's bytes at `source` into elements at `destination`, which+# are stored as the segment holds them.inline fn copy_data_elements(destination: u64, source: u64, count: u64, kind: u64) {- let size = data_size(kind);- let element_size = storage_size(kind);- let mut index: u64 = 0;- while index < count {- let from = source + (index * size);- let to = destination + (index * element_size);- let mut low: u64 = 0;- let mut high: u64 = 0;- if kind == STORAGE_I8 {- load8(low, [from, 0]);- }- if kind == STORAGE_I16 {- load16(low, [from, 0]);- }- if kind == STORAGE_32 {- load32(low, [from, 0]);- }- if kind == STORAGE_64 {- load64(low, [from, 0]);- }- if kind == STORAGE_V128 {- load64(low, [from, 0]);- load64(high, [from, 8]);- }- store_storage_value(to, kind, low, high);- index = index + 1;- }+ copy_bytes(destination, source, count * storage_size(kind));}inline fn non_null(reference: u64) -> u64 {@@ -7414,8 +7405,16 @@ inline fn check_array_range(start: u64, count: u64, length: u64) {};}-inline fn element_address(array: u64, index: u64, kind: u64) -> u64 {- let size = storage_size(kind);+# The bytes an element of `array` takes.+inline fn array_element_size(array: u64) -> u64 {+ let mut rtt: u64;+ load64(rtt, [array, 0]);+ let mut size: u64;+ load32(size, [rtt, RTT_SIZE]);+ size+}++inline fn element_address(array: u64, index: u64, size: u64) -> u64 {let data = object_data(array);data + (index * size)}@@ -7500,8 +7499,7 @@ handler StructGetS() @opcode(0xfb, 0x3) {let [type_index, field_index, length] = two_immediates();let object = non_null(unary_operand());let [address, kind] = field_of(object, type_index, field_index);- let mut value: u64;- load64(value, [address, 0]);+ let value = load_stored(address, kind);let extended = sign_extend_packed(value, kind);set_top(extended);advance(length);@@ -7510,9 +7508,8 @@ handler StructGetS() @opcode(0xfb, 0x3) {handler StructGetU() @opcode(0xfb, 0x4) {let [type_index, field_index, length] = two_immediates();let object = non_null(unary_operand());- let [address, _] = field_of(object, type_index, field_index);- let mut value: u64;- load64(value, [address, 0]);+ let [address, kind] = field_of(object, type_index, field_index);+ let value = load_stored(address, kind);set_top(value);advance(length);}@@ -7546,10 +7543,11 @@ handler ArrayNew() @opcode(0xfb, 0x6) {let wide_count: u64 = alias(count);let array = allocate_array(type_index, wide_count);let kind = array_element_kind(array);+ let size = array_element_size(array);let [low, high] = pop_storage_value(kind);let mut index: u64 = 0;while index < wide_count {- let address = element_address(array, index, kind);+ let address = element_address(array, index, size);store_storage_value(address, kind, low, high);index = index + 1;}@@ -7570,11 +7568,12 @@ handler ArrayNewFixed() @opcode(0xfb, 0x8) {let [type_index, count, length] = two_immediates();let array = allocate_array(type_index, count);let kind = array_element_kind(array);+ let size = array_element_size(array);# The last element is on top.let mut index = count;while index != 0 {index = index - 1;- let address = element_address(array, index, kind);+ let address = element_address(array, index, size);pop_into_storage(address, kind);}push(array);@@ -7595,7 +7594,7 @@ handler ArrayNewData() @opcode(0xfb, 0x9) {let segment = segment_at(INSTANCE_DATA_SEGMENTS, data_index);let mut segment_length: u64;load64(segment_length, [segment, SEGMENT_LENGTH]);- let byte_count = wide_count * data_size(kind);+ let byte_count = wide_count * storage_size(kind);check_range(wide_source, byte_count, segment_length, TRAP_MEMORY_OUT_OF_BOUNDS);let array = allocate_array(type_index, wide_count);let mut contents: u64;@@ -7631,7 +7630,7 @@ inline fn checked_element(array: u64, index: u64) -> (u64, u64) {trap(TRAP_ARRAY_OUT_OF_BOUNDS);};let kind = array_element_kind(checked);- let address = element_address(checked, index, kind);+ let address = element_address(checked, index, array_element_size(checked));(address, kind)}@@ -7647,8 +7646,7 @@ handler ArrayGetS() @opcode(0xfb, 0xc) {let [_, length] = one_immediate();let [array, index] = binary_operands();let [address, kind] = checked_element(array, index);- let mut value: u64;- load64(value, [address, 0]);+ let value = load_stored(address, kind);let extended = sign_extend_packed(value, kind);set_top(extended);advance(length);@@ -7657,9 +7655,8 @@ handler ArrayGetS() @opcode(0xfb, 0xc) {handler ArrayGetU() @opcode(0xfb, 0xd) {let [_, length] = one_immediate();let [array, index] = binary_operands();- let [address, _] = checked_element(array, index);- let mut value: u64;- load64(value, [address, 0]);+ let [address, kind] = checked_element(array, index);+ let value = load_stored(address, kind);set_top(value);advance(length);}@@ -7699,9 +7696,10 @@ handler ArrayFill() @opcode(0xfb, 0x10) {let start = pop();let array = non_null(pop());check_array_range(start, count, array_length(array));+ let size = array_element_size(array);let mut index: u64 = 0;while index < count {- let address = element_address(array, start + index, kind);+ let address = element_address(array, start + index, size);store_storage_value(address, kind, low, high);index = index + 1;}@@ -7719,8 +7717,8 @@ handler ArrayCopy() @opcode(0xfb, 0x11) {check_array_range(destination_start, count, array_length(destination));let kind = array_element_kind(destination);let size = storage_size(kind);- let from = element_address(source, source_start, kind);- let to = element_address(destination, destination_start, kind);+ let from = element_address(source, source_start, size);+ let to = element_address(destination, destination_start, size);copy_bytes(to, from, count * size);advance(length);}@@ -7736,11 +7734,11 @@ handler ArrayInitData() @opcode(0xfb, 0x12) {let segment = segment_at(INSTANCE_DATA_SEGMENTS, data_index);let mut segment_length: u64;load64(segment_length, [segment, SEGMENT_LENGTH]);- let byte_count = count * data_size(kind);+ let byte_count = count * storage_size(kind);check_range(source, byte_count, segment_length, TRAP_MEMORY_OUT_OF_BOUNDS);let mut contents: u64;load64(contents, [segment, SEGMENT_CONTENTS]);- let to = element_address(array, destination_start, kind);+ let to = element_address(array, destination_start, array_element_size(array));copy_data_elements(to, contents + source, count, kind);advance(length);}@@ -7758,7 +7756,7 @@ handler ArrayInitElem() @opcode(0xfb, 0x13) {check_range(source, count, segment_length, TRAP_TABLE_OUT_OF_BOUNDS);let mut contents: u64;load64(contents, [segment, SEGMENT_CONTENTS]);- let to = element_address(array, destination_start, STORAGE_64);+ let to = element_address(array, destination_start, 8);copy_elements(to, contents + (source << 3), count);advance(length);}diff --git a/Libraries/LibWasm/JIT/src/builder/gc.rs b/Libraries/LibWasm/JIT/src/builder/gc.rsindex 9384588df03..1502a25f09a 100644--- a/Libraries/LibWasm/JIT/src/builder/gc.rs+++ b/Libraries/LibWasm/JIT/src/builder/gc.rs@@ -9,8 +9,8 @@//! Objects are cells of the runtime's heap (see `abi::Rtt`): the Rtt first, the data's address at OBJECT_DATA_OFFSET//! and an array's length at OBJECT_LENGTH_OFFSET. Data of up to `MAX_INLINE_DATA_SIZE` bytes is inline at//! OBJECT_INLINE_DATA_OFFSET, so a struct's fields are at constant offsets from the object where the struct is small-//! enough and its type is final. Fields take `StorageType::object_field_size()` bytes, with packed and 32-bit values-//! zero-extended in them: the runtime's layout, which `abi` and `types` define for both.+//! enough and its type is final. Fields take `StorageType::object_field_size()` bytes, at the offsets `struct_layout`+//! gives them: the runtime's layout, which `abi` and `types` define for both.//!//! GC references in compiled frames need nothing special: the collector scans the stack conservatively and recognizes//! pointers into cells, and every allocation is a call of the runtime, around which values live in stack slots or in@@ -27,29 +27,12 @@ use libwasm_module::abi::{};use libwasm_module::opcode::gc::*;use libwasm_module::opcode::simd::I64X2_EXTRACT_LANE;-use libwasm_module::types::{CompositeType, HeapType, StorageType, ValType};+use libwasm_module::types::{CompositeType, HeapType, StorageType, ValType, struct_layout};use std::mem::offset_of;-/// The bytes an element of `storage` takes in a data segment.-fn data_bytes(storage: StorageType) -> u64 {- match storage {- StorageType::I8 => 1,- StorageType::I16 => 2,- StorageType::Val(ValType::I32 | ValType::F32) => 4,- StorageType::Val(ValType::V128) => 16,- StorageType::Val(_) => 8,- }-}--/// The log2 of the bytes a store of a value of `storage` writes.-fn store_size_log2(storage: StorageType) -> u8 {- match storage {- StorageType::I8 => 0,- StorageType::I16 => 1,- StorageType::Val(ValType::I32 | ValType::F32) => 2,- StorageType::Val(ValType::V128) => 4,- StorageType::Val(_) => 3,- }+/// The log2 of the bytes a field or element of `storage` takes, which its stores write.+fn size_log2(storage: StorageType) -> u8 {+ storage.object_field_size().trailing_zeros() as u8}fn struct_fields(module: &Module, type_index: u32) -> &[libwasm_module::types::FieldType] {@@ -84,14 +67,13 @@ impl Builder {if let Some(offset) = self.field_offsets.get(key) {return offset;}- let mut offset = 0;- for (index, field_type) in struct_fields(module, type_index).iter().enumerate() {+ let (offsets, size) = struct_layout(struct_fields(module, type_index));+ for (index, offset) in offsets.into_iter().enumerate() {self.field_offsets.insert((u64::from(type_index) << 32) | index as u64, offset);- offset += field_type.storage.object_field_size();}let count = struct_fields(module, type_index).len() as u64;- self.field_offsets.insert((u64::from(type_index) << 32) | count, offset);+ self.field_offsets.insert((u64::from(type_index) << 32) | count, size);self.field_offsets.get(key).expect("every field has an offset")}@@ -276,7 +258,7 @@ impl Builder {}let storage = struct_fields(module, first)[index].storage;let offset = data_offset + self.field_offset(module, first, index as u32);- self.store_at(func, store_size_log2(storage), base, offset, value);+ self.store_at(func, size_log2(storage), base, offset, value);}}self.push(object);@@ -299,7 +281,7 @@ impl Builder {let (base, data_offset) = Self::object_data(func, object, size);let offset = data_offset + self.field_offset(module, first, second);let storage = struct_fields(module, first)[second as usize].storage;- self.store_at(func, store_size_log2(storage), base, offset, value);+ self.store_at(func, size_log2(storage), base, offset, value);}ARRAY_NEW | ARRAY_NEW_DEFAULT => {let count = self.pop();@@ -335,7 +317,7 @@ impl Builder {continue;}let offset = data_offset + index as u32 * element_size;- self.store_at(func, store_size_log2(storage), base, offset, value);+ self.store_at(func, size_log2(storage), base, offset, value);}self.push(array);}@@ -346,7 +328,7 @@ impl Builder {let (segments, unit, trap, init) = if sub_opcode == ARRAY_NEW_DATA {(offset_of!(InstanceData, data_segments),- data_bytes(storage),+ u64::from(storage.object_field_size()),TRAP_MEMORY_OUT_OF_BOUNDS,Helper::ArrayInitData,)@@ -380,10 +362,10 @@ impl Builder {let index = func.resolve(self.pop());let array = func.resolve(self.pop());let storage = array_storage(module, first);- let size_log2 = storage.object_field_size().trailing_zeros() as u8;+ let shift = size_log2(storage);let address = Self::append(func,- Node::new(Op::ElementAddress, Type::Pointer, size_log2, [array, index, 0]),+ Node::new(Op::ElementAddress, Type::Pointer, shift, [array, index, 0]),);let value = self.load_field(func, address, 0, storage, sub_opcode == ARRAY_GET_S);self.push(value);@@ -393,12 +375,12 @@ impl Builder {let index = func.resolve(self.pop());let array = func.resolve(self.pop());let storage = array_storage(module, first);- let size_log2 = storage.object_field_size().trailing_zeros() as u8;+ let shift = size_log2(storage);let address = Self::append(func,- Node::new(Op::ElementAddress, Type::Pointer, size_log2, [array, index, 0]),+ Node::new(Op::ElementAddress, Type::Pointer, shift, [array, index, 0]),);- self.store_at(func, store_size_log2(storage), address, 0, value);+ self.store_at(func, shift, address, 0, value);}ARRAY_LEN => {let array = func.resolve(self.pop());diff --git a/Libraries/LibWasm/JIT/src/codegen/aarch64/gc.rs b/Libraries/LibWasm/JIT/src/codegen/aarch64/gc.rsindex acbd406537f..85eef967d19 100644--- a/Libraries/LibWasm/JIT/src/codegen/aarch64/gc.rs+++ b/Libraries/LibWasm/JIT/src/codegen/aarch64/gc.rs@@ -120,13 +120,9 @@ impl CodeGenerator<'_> {self.asm.b_cond(Cond::Hs, out_of_bounds);let operand = self.operand(array, i64::from(field(OBJECT_DATA_OFFSET)), 3);self.asm.ldr(Width::W64, output, operand);- // Elements take 8 bytes each, or 16 for v128s.+ // Elements take 1 << flags bytes each.self.asm- .add_extended(Width::W64, output, output, index, Extend::Uxtx, 3);- if node.flags == 4 {- self.asm- .add_extended(Width::W64, output, output, index, Extend::Uxtx, 3);- }+ .add_extended(Width::W64, output, output, index, Extend::Uxtx, node.flags);}Op::RefTest => self.ref_test(value),_ => unreachable!("{} is no GC or atomic operation", node.op.name()),diff --git a/Libraries/LibWasm/JIT/src/codegen/x86_64/gc.rs b/Libraries/LibWasm/JIT/src/codegen/x86_64/gc.rsindex f150ba1a839..0bafec9812b 100644--- a/Libraries/LibWasm/JIT/src/codegen/x86_64/gc.rs+++ b/Libraries/LibWasm/JIT/src/codegen/x86_64/gc.rs@@ -133,8 +133,15 @@ impl CodeGenerator<'_> {self.asm.jcc(Cond::AboveOrEqual, out_of_bounds);self.asm.mov_rm(Width::W64, output, &Address::new(array, field(OBJECT_DATA_OFFSET)));+ // Elements take 1 << flags bytes each. A scale goes up to 8, so a v128 adds the index twice.+ let scale = match node.flags {+ 0 => Scale::One,+ 1 => Scale::Two,+ 2 => Scale::Four,+ _ => Scale::Eight,+ };self.asm- .lea(Width::W64, output, &Address::indexed(output, index, Scale::Eight, 0));+ .lea(Width::W64, output, &Address::indexed(output, index, scale, 0));if node.flags == 4 {self.asm.lea(Width::W64, output, &Address::indexed(output, index, Scale::Eight, 0));diff --git a/Libraries/LibWasm/JIT/src/runtime.rs b/Libraries/LibWasm/JIT/src/runtime.rsindex 12876ef38a9..cbd0d47f6cc 100644--- a/Libraries/LibWasm/JIT/src/runtime.rs+++ b/Libraries/LibWasm/JIT/src/runtime.rs@@ -13,8 +13,8 @@ use crate::RuntimeInfo;use crate::ir::Helper;use libjs_runtime_macros::jit_abi;use libwasm_module::abi::{- ExecutionContext, MemoryData, OBJECT_DATA_OFFSET, OBJECT_LENGTH_OFFSET, Rtt, STORAGE_32, STORAGE_64, STORAGE_I8,- STORAGE_I16, STORAGE_V128, Segment, TRAP_ARRAY_OUT_OF_BOUNDS, TRAP_MEMORY_OUT_OF_BOUNDS, TRAP_NULL_REFERENCE,+ ExecutionContext, MemoryData, OBJECT_DATA_OFFSET, OBJECT_LENGTH_OFFSET, Rtt, STORAGE_32, STORAGE_I8, STORAGE_I16,+ STORAGE_V128, Segment, TRAP_ARRAY_OUT_OF_BOUNDS, TRAP_MEMORY_OUT_OF_BOUNDS, TRAP_NULL_REFERENCE,TRAP_TABLE_OUT_OF_BOUNDS, TableData,};@@ -211,7 +211,7 @@ unsafe extern "C" fn table_init(context: *mut ExecutionContext) -> u64 {}// GC arrays. An array is a cell with its data's address at OBJECT_DATA_OFFSET, its length at OBJECT_LENGTH_OFFSET, and-// its Rtt first; elements take 8 bytes, or 16 for v128 (see abi.rs).+// its Rtt first; elements take their natural size (see abi.rs)./// An array's data, length, and element storage kind.unsafe fn array_parts(array: u64) -> (*mut u8, u64, u8) {@@ -224,12 +224,8 @@ unsafe fn array_parts(array: u64) -> (*mut u8, u64, u8) {}}+/// The bytes an element of storage `kind` takes, in an array and in a data segment alike.fn element_size(kind: u8) -> usize {- if kind == STORAGE_V128 { 16 } else { 8 }-}--/// The bytes an element of storage `kind` takes in a data segment.-fn data_size(kind: u8) -> usize {match kind {STORAGE_I8 => 1,STORAGE_I16 => 2,@@ -250,17 +246,18 @@ unsafe extern "C" fn array_fill(context: *mut ExecutionContext) -> u64 {if !in_range(start, count, length) {return u64::from(TRAP_ARRAY_OUT_OF_BOUNDS);}- let low = match kind {- STORAGE_I8 => low & 0xff,- STORAGE_I16 => low & 0xffff,- _ => low,- };let size = element_size(kind);for index in start..start + count {- let element = data.add(index as usize * size).cast::<u64>();- element.write(low);- if kind == STORAGE_V128 {- element.add(1).write(high);+ let element = data.add(index as usize * size);+ match kind {+ STORAGE_I8 => element.write(low as u8),+ STORAGE_I16 => element.cast::<u16>().write(low as u16),+ STORAGE_32 => element.cast::<u32>().write(low as u32),+ STORAGE_V128 => {+ element.cast::<u64>().write(low);+ element.cast::<u64>().add(1).write(high);+ }+ _ => element.cast::<u64>().write(low),}}0@@ -304,29 +301,17 @@ unsafe extern "C" fn array_init_data(context: *mut ExecutionContext) -> u64 {return u64::from(TRAP_ARRAY_OUT_OF_BOUNDS);}let segment = &*(segment as *const Segment);- let bytes = data_size(kind);+ let size = element_size(kind);// NB: The count is below 2^32 and the size at most 16, so their product does not wrap.- if !in_range(source, count * bytes as u64, segment.length) {+ if !in_range(source, count * size as u64, segment.length) {return u64::from(TRAP_MEMORY_OUT_OF_BOUNDS);}- let size = element_size(kind);- for index in 0..count as usize {- let from = segment.contents.add(source as usize + index * bytes);- let to = data.add((destination as usize + index) * size).cast::<u64>();- match kind {- STORAGE_I8 => to.write(u64::from(from.read())),- STORAGE_I16 => to.write(u64::from(from.cast::<u16>().read_unaligned())),- STORAGE_32 => to.write(u64::from(from.cast::<u32>().read_unaligned())),- STORAGE_V128 => {- to.write(from.cast::<u64>().read_unaligned());- to.add(1).write(from.cast::<u64>().add(1).read_unaligned());- }- _ => {- debug_assert_eq!(kind, STORAGE_64);- to.write(from.cast::<u64>().read_unaligned());- }- }- }+ // Elements are stored as data segments hold them.+ std::ptr::copy_nonoverlapping(+ segment.contents.add(source as usize),+ data.add(destination as usize * size),+ count as usize * size,+ );0}}diff --git a/Libraries/LibWasm/Module/src/abi.rs b/Libraries/LibWasm/Module/src/abi.rsindex c77b85baeac..b074cc01d91 100644--- a/Libraries/LibWasm/Module/src/abi.rs+++ b/Libraries/LibWasm/Module/src/abi.rs@@ -241,9 +241,9 @@ pub const RTT_KIND_FUNCTION: u32 = 0;pub const RTT_KIND_STRUCT: u32 = 1;pub const RTT_KIND_ARRAY: u32 = 2;-/// How a struct field or array element is stored: one 8-byte word, with packed and 32-bit values zero-extended-/// in it, or 16 bytes for a v128. References the collector follows (those of the any, extern, and exn hierarchies)-/// have a kind of their own.+/// How a struct field or array element is stored: in its natural size (`StorageType::object_field_size()`), as data+/// segments hold it. References the collector follows (those of the any, extern, and exn hierarchies) have a kind of+/// their own, and take 8 bytes.pub const STORAGE_64: u8 = 0;pub const STORAGE_I8: u8 = 1;pub const STORAGE_I16: u8 = 2;@@ -267,8 +267,8 @@ pub const OBJECT_DATA_SIZE_CLASSES: [usize; 17] = [/// The most data an object keeps inline.pub const MAX_INLINE_DATA_SIZE: usize = OBJECT_DATA_SIZE_CLASSES[OBJECT_DATA_SIZE_CLASSES.len() - 1];-// NB: The bytes a field or element takes in an object's data are `StorageType::object_field_size()` (types.rs); a-// struct's fields follow each other in order from offset 0.+// NB: The bytes a field or element takes in an object's data are `StorageType::object_field_size()`, and the offsets of+// a struct's fields come from `struct_layout()` (types.rs)./// An exception is a cell with the identity of its tag, the address of its payload (its values' slots in operand/// stack order, the last value in the lowest slot), the payload's length in slots, and how the collector followsdiff --git a/Libraries/LibWasm/Module/src/types.rs b/Libraries/LibWasm/Module/src/types.rsindex fdd9ccd213f..e568a6d21ce 100644--- a/Libraries/LibWasm/Module/src/types.rs+++ b/Libraries/LibWasm/Module/src/types.rs@@ -117,9 +117,20 @@ pub enum StorageType {impl StorageType {/// The bytes a struct field or array element of this storage type takes in a GC object's data (see- /// `abi::STORAGE_64`): 16 for a v128, else 8.+ /// `abi::STORAGE_64`): its natural size, with references taking 8.pub fn object_field_size(self) -> u32 {- if self == Self::Val(ValType::V128) { 16 } else { 8 }+ match self {+ Self::I8 => 1,+ Self::I16 => 2,+ Self::Val(ValType::I32 | ValType::F32) => 4,+ Self::Val(ValType::V128) => 16,+ Self::Val(_) => 8,+ }+ }++ /// The alignment of a struct field of this storage type in a GC object's data, which is at most 8.+ pub fn object_field_alignment(self) -> u32 {+ self.object_field_size().min(8)}pub fn is_packed(self) -> bool {@@ -141,6 +152,48 @@ pub struct FieldType {pub mutable: bool,}+/// The byte offsets of the fields of a struct in a GC object's data, and the size of the data rounded up to 8 bytes.+/// Each field is aligned, and takes the first gap the fields before it left that it fits in, or else goes at the end.+/// A field's place depends only on the fields before it, so the fields a subtype shares with its supertype are where+/// they are in the supertype.+pub fn struct_layout(fields: &[FieldType]) -> (Vec<u32>, u32) {+ let mut offsets = Vec::with_capacity(fields.len());+ // The gaps alignment has left so far, as (start, end), in order.+ let mut gaps: Vec<(u32, u32)> = Vec::new();+ let mut end: u32 = 0;+ for field in fields {+ let size = field.storage.object_field_size();+ let alignment = field.storage.object_field_alignment();+ let fitting_gap = gaps.iter().enumerate().find_map(|(index, &(start, gap_end))| {+ let offset = start.next_multiple_of(alignment);+ (offset + size <= gap_end).then_some((index, offset))+ });+ let offset = match fitting_gap {+ Some((index, offset)) => {+ let (start, gap_end) = gaps[index];+ gaps.remove(index);+ if offset + size < gap_end {+ gaps.insert(index, (offset + size, gap_end));+ }+ if start < offset {+ gaps.insert(index, (start, offset));+ }+ offset+ }+ None => {+ let offset = end.next_multiple_of(alignment);+ if end < offset {+ gaps.push((end, offset));+ }+ end = offset + size;+ offset+ }+ };+ offsets.push(offset);+ }+ (offsets, end.next_multiple_of(8))+}+#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]pub struct FuncType {pub params: Vec<ValType>,