| ... | @@ -962,11 +962,6 @@ fn parseAtom(self: *Wasm, atom: *Atom, kind: Kind) !void { | ... | @@ -962,11 +962,6 @@ fn parseAtom(self: *Wasm, atom: *Atom, kind: Kind) !void { |
| 962 | | 962 | |
| 963 | const segment: *Segment = &self.segments.items[final_index]; | 963 | const segment: *Segment = &self.segments.items[final_index]; |
| 964 | segment.alignment = std.math.max(segment.alignment, atom.alignment); | 964 | segment.alignment = std.math.max(segment.alignment, atom.alignment); |
| 965 | segment.size = std.mem.alignForwardGeneric( | | |
| 966 | u32, | | |
| 967 | std.mem.alignForwardGeneric(u32, segment.size, atom.alignment) + atom.size, | | |
| 968 | segment.alignment, | | |
| 969 | ); | | |
| 970 | | 965 | |
| 971 | if (self.atoms.getPtr(final_index)) |last| { | 966 | if (self.atoms.getPtr(final_index)) |last| { |
| 972 | last.*.next = atom; | 967 | last.*.next = atom; |
| ... | @@ -978,9 +973,10 @@ fn parseAtom(self: *Wasm, atom: *Atom, kind: Kind) !void { | ... | @@ -978,9 +973,10 @@ fn parseAtom(self: *Wasm, atom: *Atom, kind: Kind) !void { |
| 978 | } | 973 | } |
| 979 | | 974 | |
| 980 | fn allocateAtoms(self: *Wasm) !void { | 975 | fn allocateAtoms(self: *Wasm) !void { |
| 981 | var it = self.atoms.valueIterator(); | 976 | var it = self.atoms.iterator(); |
| 982 | while (it.next()) |current_atom| { | 977 | while (it.next()) |entry| { |
| 983 | var atom: *Atom = current_atom.*.getFirst(); | 978 | const segment = &self.segments.items[entry.key_ptr.*]; |
| | 979 | var atom: *Atom = entry.value_ptr.*.getFirst(); |
| 984 | var offset: u32 = 0; | 980 | var offset: u32 = 0; |
| 985 | while (true) { | 981 | while (true) { |
| 986 | offset = std.mem.alignForwardGeneric(u32, offset, atom.alignment); | 982 | offset = std.mem.alignForwardGeneric(u32, offset, atom.alignment); |
| ... | @@ -996,6 +992,7 @@ fn allocateAtoms(self: *Wasm) !void { | ... | @@ -996,6 +992,7 @@ fn allocateAtoms(self: *Wasm) !void { |
| 996 | self.symbol_atom.putAssumeCapacity(atom.symbolLoc(), atom); // Update atom pointers | 992 | self.symbol_atom.putAssumeCapacity(atom.symbolLoc(), atom); // Update atom pointers |
| 997 | atom = atom.next orelse break; | 993 | atom = atom.next orelse break; |
| 998 | } | 994 | } |
| | 995 | segment.size = std.mem.alignForwardGeneric(u32, offset, segment.alignment); |
| 999 | } | 996 | } |
| 1000 | } | 997 | } |
| 1001 | | 998 | |
| ... | @@ -1566,8 +1563,8 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { | ... | @@ -1566,8 +1563,8 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 1566 | try self.objects.items[object_index].parseIntoAtoms(self.base.allocator, object_index, self); | 1563 | try self.objects.items[object_index].parseIntoAtoms(self.base.allocator, object_index, self); |
| 1567 | } | 1564 | } |
| 1568 | | 1565 | |
| 1569 | try self.setupMemory(); | | |
| 1570 | try self.allocateAtoms(); | 1566 | try self.allocateAtoms(); |
| | 1567 | try self.setupMemory(); |
| 1571 | self.mapFunctionTable(); | 1568 | self.mapFunctionTable(); |
| 1572 | try self.mergeSections(); | 1569 | try self.mergeSections(); |
| 1573 | try self.mergeTypes(); | 1570 | try self.mergeTypes(); |
| ... | @@ -1832,7 +1829,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { | ... | @@ -1832,7 +1829,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 1832 | segment_count += 1; | 1829 | segment_count += 1; |
| 1833 | const atom_index = entry.value_ptr.*; | 1830 | const atom_index = entry.value_ptr.*; |
| 1834 | var atom: *Atom = self.atoms.getPtr(atom_index).?.*.getFirst(); | 1831 | var atom: *Atom = self.atoms.getPtr(atom_index).?.*.getFirst(); |
| 1835 | var segment = self.segments.items[atom_index]; | 1832 | const segment = self.segments.items[atom_index]; |
| 1836 | | 1833 | |
| 1837 | // flag and index to memory section (currently, there can only be 1 memory section in wasm) | 1834 | // flag and index to memory section (currently, there can only be 1 memory section in wasm) |
| 1838 | try leb.writeULEB128(writer, @as(u32, 0)); | 1835 | try leb.writeULEB128(writer, @as(u32, 0)); |