| ... | @@ -37,12 +37,16 @@ segment_free_list: std.ArrayListUnmanaged(u32) = .{}, | ... | @@ -37,12 +37,16 @@ segment_free_list: std.ArrayListUnmanaged(u32) = .{}, |
| 37 | string_table: StringTable = .{}, | 37 | string_table: StringTable = .{}, |
| 38 | /// Map for storing anonymous declarations. Each anonymous decl maps to its Atom's index. | 38 | /// Map for storing anonymous declarations. Each anonymous decl maps to its Atom's index. |
| 39 | anon_decls: std.AutoArrayHashMapUnmanaged(InternPool.Index, Atom.Index) = .{}, | 39 | anon_decls: std.AutoArrayHashMapUnmanaged(InternPool.Index, Atom.Index) = .{}, |
| | 40 | /// List of atom indexes of functions that are generated by the backend. |
| | 41 | synthetic_functions: std.ArrayListUnmanaged(Atom.Index) = .{}, |
| 40 | /// Represents the symbol index of the error name table | 42 | /// Represents the symbol index of the error name table |
| 41 | /// When this is `null`, no code references an error using runtime `@errorName`. | 43 | /// When this is `null`, no code references an error using runtime `@errorName`. |
| 42 | /// During initializion, a symbol with corresponding atom will be created that is | 44 | /// During initializion, a symbol with corresponding atom will be created that is |
| 43 | /// used to perform relocations to the pointer of this table. | 45 | /// used to perform relocations to the pointer of this table. |
| 44 | /// The actual table is populated during `flush`. | 46 | /// The actual table is populated during `flush`. |
| 45 | error_table_symbol: Symbol.Index = .null, | 47 | error_table_symbol: Symbol.Index = .null, |
| | 48 | /// Atom index of the table of symbol names. This is stored so we can clean up the atom. |
| | 49 | error_names_atom: Atom.Index = .null, |
| 46 | /// Amount of functions in the `import` sections. | 50 | /// Amount of functions in the `import` sections. |
| 47 | imported_functions_count: u32 = 0, | 51 | imported_functions_count: u32 = 0, |
| 48 | /// Amount of globals in the `import` section. | 52 | /// Amount of globals in the `import` section. |
| ... | @@ -150,9 +154,6 @@ pub fn deinit(zig_object: *ZigObject, wasm_file: *Wasm) void { | ... | @@ -150,9 +154,6 @@ pub fn deinit(zig_object: *ZigObject, wasm_file: *Wasm) void { |
| 150 | gpa.free(segment_info.name); | 154 | gpa.free(segment_info.name); |
| 151 | } | 155 | } |
| 152 | | 156 | |
| 153 | // For decls and anon decls we free the memory of its atoms. | | |
| 154 | // The memory of atoms parsed from object files is managed by | | |
| 155 | // the object file itself, and therefore we can skip those. | | |
| 156 | { | 157 | { |
| 157 | var it = zig_object.decls_map.valueIterator(); | 158 | var it = zig_object.decls_map.valueIterator(); |
| 158 | while (it.next()) |decl_info| { | 159 | while (it.next()) |decl_info| { |
| ... | @@ -175,6 +176,31 @@ pub fn deinit(zig_object: *ZigObject, wasm_file: *Wasm) void { | ... | @@ -175,6 +176,31 @@ pub fn deinit(zig_object: *ZigObject, wasm_file: *Wasm) void { |
| 175 | atom.deinit(gpa); | 176 | atom.deinit(gpa); |
| 176 | } | 177 | } |
| 177 | } | 178 | } |
| | 179 | if (zig_object.findGlobalSymbol("__zig_errors_len")) |sym_index| { |
| | 180 | const atom_index = wasm_file.symbol_atom.get(.{ .file = zig_object.index, .index = sym_index }).?; |
| | 181 | wasm_file.getAtomPtr(atom_index).deinit(gpa); |
| | 182 | } |
| | 183 | if (wasm_file.symbol_atom.get(.{ .file = zig_object.index, .index = zig_object.error_table_symbol })) |atom_index| { |
| | 184 | const atom = wasm_file.getAtomPtr(atom_index); |
| | 185 | atom.deinit(gpa); |
| | 186 | } |
| | 187 | for (zig_object.synthetic_functions.items) |atom_index| { |
| | 188 | const atom = wasm_file.getAtomPtr(atom_index); |
| | 189 | atom.deinit(gpa); |
| | 190 | } |
| | 191 | zig_object.synthetic_functions.deinit(gpa); |
| | 192 | for (zig_object.func_types.items) |*ty| { |
| | 193 | ty.deinit(gpa); |
| | 194 | } |
| | 195 | if (zig_object.error_names_atom != .null) { |
| | 196 | const atom = wasm_file.getAtomPtr(zig_object.error_names_atom); |
| | 197 | atom.deinit(gpa); |
| | 198 | } |
| | 199 | zig_object.global_syms.deinit(gpa); |
| | 200 | zig_object.func_types.deinit(gpa); |
| | 201 | zig_object.atom_types.deinit(gpa); |
| | 202 | zig_object.functions.deinit(gpa); |
| | 203 | zig_object.imports.deinit(gpa); |
| 178 | zig_object.decls_map.deinit(gpa); | 204 | zig_object.decls_map.deinit(gpa); |
| 179 | zig_object.anon_decls.deinit(gpa); | 205 | zig_object.anon_decls.deinit(gpa); |
| 180 | zig_object.symbols.deinit(gpa); | 206 | zig_object.symbols.deinit(gpa); |
| ... | @@ -602,7 +628,7 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void { | ... | @@ -602,7 +628,7 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void { |
| 602 | const atom = wasm_file.getAtomPtr(atom_index); | 628 | const atom = wasm_file.getAtomPtr(atom_index); |
| 603 | | 629 | |
| 604 | const error_name = mod.intern_pool.stringToSlice(error_name_nts); | 630 | const error_name = mod.intern_pool.stringToSlice(error_name_nts); |
| 605 | const len = @as(u32, @intCast(error_name.len + 1)); // names are 0-termianted | 631 | const len: u32 = @intCast(error_name.len + 1); // names are 0-terminated |
| 606 | | 632 | |
| 607 | const slice_ty = Type.slice_const_u8_sentinel_0; | 633 | const slice_ty = Type.slice_const_u8_sentinel_0; |
| 608 | const offset = @as(u32, @intCast(atom.code.items.len)); | 634 | const offset = @as(u32, @intCast(atom.code.items.len)); |
| ... | @@ -614,9 +640,9 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void { | ... | @@ -614,9 +640,9 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void { |
| 614 | .index = @intFromEnum(names_atom.sym_index), | 640 | .index = @intFromEnum(names_atom.sym_index), |
| 615 | .relocation_type = .R_WASM_MEMORY_ADDR_I32, | 641 | .relocation_type = .R_WASM_MEMORY_ADDR_I32, |
| 616 | .offset = offset, | 642 | .offset = offset, |
| 617 | .addend = @as(i32, @intCast(addend)), | 643 | .addend = @intCast(addend), |
| 618 | }); | 644 | }); |
| 619 | atom.size += @as(u32, @intCast(slice_ty.abiSize(mod))); | 645 | atom.size += @intCast(slice_ty.abiSize(mod)); |
| 620 | addend += len; | 646 | addend += len; |
| 621 | | 647 | |
| 622 | // as we updated the error name table, we now store the actual name within the names atom | 648 | // as we updated the error name table, we now store the actual name within the names atom |
| ... | @@ -627,6 +653,7 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void { | ... | @@ -627,6 +653,7 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void { |
| 627 | log.debug("Populated error name: '{s}'", .{error_name}); | 653 | log.debug("Populated error name: '{s}'", .{error_name}); |
| 628 | } | 654 | } |
| 629 | names_atom.size = addend; | 655 | names_atom.size = addend; |
| | 656 | zig_object.error_names_atom = names_atom_index; |
| 630 | } | 657 | } |
| 631 | | 658 | |
| 632 | /// Either creates a new import, or updates one if existing. | 659 | /// Either creates a new import, or updates one if existing. |
| ... | @@ -1174,6 +1201,7 @@ pub fn createFunction( | ... | @@ -1174,6 +1201,7 @@ pub fn createFunction( |
| 1174 | atom.code = function_body.moveToUnmanaged(); | 1201 | atom.code = function_body.moveToUnmanaged(); |
| 1175 | atom.relocs = relocations.moveToUnmanaged(); | 1202 | atom.relocs = relocations.moveToUnmanaged(); |
| 1176 | | 1203 | |
| | 1204 | try zig_object.synthetic_functions.append(gpa, atom_index); |
| 1177 | return sym_index; | 1205 | return sym_index; |
| 1178 | } | 1206 | } |
| 1179 | | 1207 | |