| ... | ... | @@ -986,31 +986,23 @@ pub fn deinit(wasm: *Wasm) void { |
| 986 | 986 | } |
| 987 | 987 | } |
| 988 | 988 | |
| 989 | | pub fn allocateDeclIndexes(wasm: *Wasm, decl_index: Module.Decl.Index) !void { |
| 990 | | if (wasm.llvm_object) |_| return; |
| 991 | | const decl = wasm.base.options.module.?.declPtr(decl_index); |
| 992 | | if (decl.link.wasm.sym_index != 0) return; |
| 993 | | |
| 989 | /// Allocates a new symbol and returns its index. |
| 990 | /// Will re-use slots when a symbol was freed at an earlier stage. |
| 991 | pub fn allocateSymbol(wasm: *Wasm) !u32 { |
| 994 | 992 | try wasm.symbols.ensureUnusedCapacity(wasm.base.allocator, 1); |
| 995 | | try wasm.decls.putNoClobber(wasm.base.allocator, decl_index, {}); |
| 996 | | |
| 997 | | const atom = &decl.link.wasm; |
| 998 | | |
| 999 | 993 | var symbol: Symbol = .{ |
| 1000 | 994 | .name = undefined, // will be set after updateDecl |
| 1001 | 995 | .flags = @enumToInt(Symbol.Flag.WASM_SYM_BINDING_LOCAL), |
| 1002 | 996 | .tag = undefined, // will be set after updateDecl |
| 1003 | 997 | .index = undefined, // will be set after updateDecl |
| 1004 | 998 | }; |
| 1005 | | |
| 1006 | 999 | if (wasm.symbols_free_list.popOrNull()) |index| { |
| 1007 | | atom.sym_index = index; |
| 1008 | 1000 | wasm.symbols.items[index] = symbol; |
| 1009 | | } else { |
| 1010 | | atom.sym_index = @intCast(u32, wasm.symbols.items.len); |
| 1011 | | wasm.symbols.appendAssumeCapacity(symbol); |
| 1001 | return index; |
| 1012 | 1002 | } |
| 1013 | | try wasm.symbol_atom.putNoClobber(wasm.base.allocator, atom.symbolLoc(), atom); |
| 1003 | const index = @intCast(u32, wasm.symbols.items.len); |
| 1004 | wasm.symbols.appendAssumeCapacity(symbol); |
| 1005 | return index; |
| 1014 | 1006 | } |
| 1015 | 1007 | |
| 1016 | 1008 | pub fn updateFunc(wasm: *Wasm, mod: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void { |
| ... | ... | @@ -1026,9 +1018,12 @@ pub fn updateFunc(wasm: *Wasm, mod: *Module, func: *Module.Fn, air: Air, livenes |
| 1026 | 1018 | |
| 1027 | 1019 | const decl_index = func.owner_decl; |
| 1028 | 1020 | const decl = mod.declPtr(decl_index); |
| 1029 | | assert(decl.link.wasm.sym_index != 0); // Must call allocateDeclIndexes() |
| 1030 | | |
| 1031 | | decl.link.wasm.clear(); |
| 1021 | const atom = &decl.link.wasm; |
| 1022 | try atom.ensureInitialized(wasm); |
| 1023 | const gop = try wasm.decls.getOrPut(wasm.base.allocator, decl_index); |
| 1024 | if (gop.found_existing) { |
| 1025 | atom.clear(); |
| 1026 | } else gop.value_ptr.* = {}; |
| 1032 | 1027 | |
| 1033 | 1028 | var decl_state: ?Dwarf.DeclState = if (wasm.dwarf) |*dwarf| try dwarf.initDeclState(mod, decl_index) else null; |
| 1034 | 1029 | defer if (decl_state) |*ds| ds.deinit(); |
| ... | ... | @@ -1083,16 +1078,19 @@ pub fn updateDecl(wasm: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !voi |
| 1083 | 1078 | defer tracy.end(); |
| 1084 | 1079 | |
| 1085 | 1080 | const decl = mod.declPtr(decl_index); |
| 1086 | | assert(decl.link.wasm.sym_index != 0); // Must call allocateDeclIndexes() |
| 1087 | | |
| 1088 | | decl.link.wasm.clear(); |
| 1089 | | |
| 1090 | 1081 | if (decl.val.castTag(.function)) |_| { |
| 1091 | 1082 | return; |
| 1092 | 1083 | } else if (decl.val.castTag(.extern_fn)) |_| { |
| 1093 | 1084 | return; |
| 1094 | 1085 | } |
| 1095 | 1086 | |
| 1087 | const atom = &decl.link.wasm; |
| 1088 | try atom.ensureInitialized(wasm); |
| 1089 | const gop = try wasm.decls.getOrPut(wasm.base.allocator, decl_index); |
| 1090 | if (gop.found_existing) { |
| 1091 | atom.clear(); |
| 1092 | } else gop.value_ptr.* = {}; |
| 1093 | |
| 1096 | 1094 | if (decl.isExtern()) { |
| 1097 | 1095 | const variable = decl.getVariable().?; |
| 1098 | 1096 | const name = mem.sliceTo(decl.name, 0); |
| ... | ... | @@ -1148,8 +1146,8 @@ fn finishUpdateDecl(wasm: *Wasm, decl: *Module.Decl, code: []const u8) !void { |
| 1148 | 1146 | try atom.code.appendSlice(wasm.base.allocator, code); |
| 1149 | 1147 | try wasm.resolved_symbols.put(wasm.base.allocator, atom.symbolLoc(), {}); |
| 1150 | 1148 | |
| 1151 | | if (code.len == 0) return; |
| 1152 | 1149 | atom.size = @intCast(u32, code.len); |
| 1150 | if (code.len == 0) return; |
| 1153 | 1151 | atom.alignment = decl.ty.abiAlignment(wasm.base.options.target); |
| 1154 | 1152 | } |
| 1155 | 1153 | |
| ... | ... | @@ -1211,28 +1209,19 @@ pub fn lowerUnnamedConst(wasm: *Wasm, tv: TypedValue, decl_index: Module.Decl.In |
| 1211 | 1209 | defer wasm.base.allocator.free(fqdn); |
| 1212 | 1210 | const name = try std.fmt.allocPrintZ(wasm.base.allocator, "__unnamed_{s}_{d}", .{ fqdn, local_index }); |
| 1213 | 1211 | defer wasm.base.allocator.free(name); |
| 1214 | | var symbol: Symbol = .{ |
| 1215 | | .name = try wasm.string_table.put(wasm.base.allocator, name), |
| 1216 | | .flags = 0, |
| 1217 | | .tag = .data, |
| 1218 | | .index = undefined, |
| 1219 | | }; |
| 1220 | | symbol.setFlag(.WASM_SYM_BINDING_LOCAL); |
| 1221 | 1212 | |
| 1222 | 1213 | const atom = try decl.link.wasm.locals.addOne(wasm.base.allocator); |
| 1223 | 1214 | atom.* = Atom.empty; |
| 1215 | try atom.ensureInitialized(wasm); |
| 1224 | 1216 | atom.alignment = tv.ty.abiAlignment(wasm.base.options.target); |
| 1225 | | try wasm.symbols.ensureUnusedCapacity(wasm.base.allocator, 1); |
| 1217 | wasm.symbols.items[atom.sym_index] = .{ |
| 1218 | .name = try wasm.string_table.put(wasm.base.allocator, name), |
| 1219 | .flags = @enumToInt(Symbol.Flag.WASM_SYM_BINDING_LOCAL), |
| 1220 | .tag = .data, |
| 1221 | .index = undefined, |
| 1222 | }; |
| 1226 | 1223 | |
| 1227 | | if (wasm.symbols_free_list.popOrNull()) |index| { |
| 1228 | | atom.sym_index = index; |
| 1229 | | wasm.symbols.items[index] = symbol; |
| 1230 | | } else { |
| 1231 | | atom.sym_index = @intCast(u32, wasm.symbols.items.len); |
| 1232 | | wasm.symbols.appendAssumeCapacity(symbol); |
| 1233 | | } |
| 1234 | 1224 | try wasm.resolved_symbols.putNoClobber(wasm.base.allocator, atom.symbolLoc(), {}); |
| 1235 | | try wasm.symbol_atom.putNoClobber(wasm.base.allocator, atom.symbolLoc(), atom); |
| 1236 | 1225 | |
| 1237 | 1226 | var value_bytes = std.ArrayList(u8).init(wasm.base.allocator); |
| 1238 | 1227 | defer value_bytes.deinit(); |
| ... | ... | @@ -1304,8 +1293,8 @@ pub fn getDeclVAddr( |
| 1304 | 1293 | ) !u64 { |
| 1305 | 1294 | const mod = wasm.base.options.module.?; |
| 1306 | 1295 | const decl = mod.declPtr(decl_index); |
| 1296 | try decl.link.wasm.ensureInitialized(wasm); |
| 1307 | 1297 | const target_symbol_index = decl.link.wasm.sym_index; |
| 1308 | | assert(target_symbol_index != 0); |
| 1309 | 1298 | assert(reloc_info.parent_atom_index != 0); |
| 1310 | 1299 | const atom = wasm.symbol_atom.get(.{ .file = null, .index = reloc_info.parent_atom_index }).?; |
| 1311 | 1300 | const is_wasm32 = wasm.base.options.target.cpu.arch == .wasm32; |
| ... | ... | @@ -1363,6 +1352,7 @@ pub fn updateDeclExports( |
| 1363 | 1352 | } |
| 1364 | 1353 | |
| 1365 | 1354 | const decl = mod.declPtr(decl_index); |
| 1355 | if (decl.link.wasm.getSymbolIndex() == null) return; // unititialized |
| 1366 | 1356 | |
| 1367 | 1357 | for (exports) |exp| { |
| 1368 | 1358 | if (exp.options.section) |section| { |