| ... | ... | @@ -586,7 +586,7 @@ pub const NavExe = extern struct { |
| 586 | 586 | pub const ZcuFunc = extern struct { |
| 587 | 587 | function: CodeGen.Function, |
| 588 | 588 | |
| 589 | | /// Index into `zcu_funcs`. |
| 589 | /// Index into `Wasm.zcu_funcs`. |
| 590 | 590 | /// Note that swapRemove is sometimes performed on `zcu_funcs`. |
| 591 | 591 | pub const Index = enum(u32) { |
| 592 | 592 | _, |
| ... | ... | @@ -641,7 +641,7 @@ pub const FunctionImport = extern struct { |
| 641 | 641 | __wasm_init_tls, |
| 642 | 642 | __zig_error_names, |
| 643 | 643 | // Next, index into `object_functions`. |
| 644 | | // Next, index into `navs_exe` or `navs_obj` depending on whether emitting an object. |
| 644 | // Next, index into `zcu_funcs`. |
| 645 | 645 | _, |
| 646 | 646 | |
| 647 | 647 | const first_object_function = @intFromEnum(Resolution.__zig_error_names) + 1; |
| ... | ... | @@ -654,8 +654,7 @@ pub const FunctionImport = extern struct { |
| 654 | 654 | __wasm_init_tls, |
| 655 | 655 | __zig_error_names, |
| 656 | 656 | object_function: ObjectFunctionIndex, |
| 657 | | nav_exe: NavExe.Index, |
| 658 | | nav_obj: NavObj.Index, |
| 657 | zcu_func: ZcuFunc.Index, |
| 659 | 658 | }; |
| 660 | 659 | |
| 661 | 660 | pub fn unpack(r: Resolution, wasm: *const Wasm) Unpacked { |
| ... | ... | @@ -669,16 +668,13 @@ pub const FunctionImport = extern struct { |
| 669 | 668 | _ => { |
| 670 | 669 | const i: u32 = @intFromEnum(r); |
| 671 | 670 | const object_function_index = i - first_object_function; |
| 672 | | if (object_function_index < wasm.object_functions.items.len) |
| 671 | if (object_function_index < wasm.object_functions.items.len) { |
| 673 | 672 | return .{ .object_function = @enumFromInt(object_function_index) }; |
| 674 | | const comp = wasm.base.comp; |
| 675 | | const is_obj = comp.config.output_mode == .Obj; |
| 676 | | const nav_index = object_function_index - wasm.object_functions.items.len; |
| 677 | | return if (is_obj) .{ |
| 678 | | .nav_obj = @enumFromInt(nav_index), |
| 679 | | } else .{ |
| 680 | | .nav_exe = @enumFromInt(nav_index), |
| 681 | | }; |
| 673 | } else { |
| 674 | return .{ |
| 675 | .zcu_func = @enumFromInt(object_function_index - wasm.object_functions.items.len), |
| 676 | }; |
| 677 | } |
| 682 | 678 | }, |
| 683 | 679 | }; |
| 684 | 680 | } |
| ... | ... | @@ -692,24 +688,22 @@ pub const FunctionImport = extern struct { |
| 692 | 688 | .__wasm_init_tls => .__wasm_init_tls, |
| 693 | 689 | .__zig_error_names => .__zig_error_names, |
| 694 | 690 | .object_function => |i| @enumFromInt(first_object_function + @intFromEnum(i)), |
| 695 | | .nav_obj => |i| @enumFromInt(first_object_function + wasm.object_functions.items.len + @intFromEnum(i)), |
| 696 | | .nav_exe => |i| @enumFromInt(first_object_function + wasm.object_functions.items.len + @intFromEnum(i)), |
| 691 | .zcu_func => |i| @enumFromInt(first_object_function + wasm.object_functions.items.len + @intFromEnum(i)), |
| 697 | 692 | }; |
| 698 | 693 | } |
| 699 | 694 | |
| 700 | | pub fn fromIpNav(wasm: *const Wasm, ip_nav: InternPool.Nav.Index) Resolution { |
| 701 | | const comp = wasm.base.comp; |
| 702 | | const is_obj = comp.config.output_mode == .Obj; |
| 703 | | return pack(wasm, if (is_obj) .{ |
| 704 | | .nav_obj = @enumFromInt(wasm.navs_obj.getIndex(ip_nav).?), |
| 705 | | } else .{ |
| 706 | | .nav_exe = @enumFromInt(wasm.navs_exe.getIndex(ip_nav).?), |
| 695 | pub fn fromIpNav(wasm: *const Wasm, nav_index: InternPool.Nav.Index) Resolution { |
| 696 | const zcu = wasm.base.comp.zcu.?; |
| 697 | const ip = &zcu.intern_pool; |
| 698 | const nav = ip.getNav(nav_index); |
| 699 | return pack(wasm, .{ |
| 700 | .zcu_func = @enumFromInt(wasm.zcu_funcs.getIndex(nav.status.resolved.val).?), |
| 707 | 701 | }); |
| 708 | 702 | } |
| 709 | 703 | |
| 710 | 704 | pub fn isNavOrUnresolved(r: Resolution, wasm: *const Wasm) bool { |
| 711 | 705 | return switch (r.unpack(wasm)) { |
| 712 | | .unresolved, .nav_obj, .nav_exe => true, |
| 706 | .unresolved, .zcu_func => true, |
| 713 | 707 | else => false, |
| 714 | 708 | }; |
| 715 | 709 | } |
| ... | ... | @@ -723,8 +717,7 @@ pub const FunctionImport = extern struct { |
| 723 | 717 | .__wasm_init_tls => @panic("TODO"), |
| 724 | 718 | .__zig_error_names => @panic("TODO"), |
| 725 | 719 | .object_function => |i| i.ptr(wasm).type_index, |
| 726 | | .nav_exe => @panic("TODO"), |
| 727 | | .nav_obj => @panic("TODO"), |
| 720 | .zcu_func => @panic("TODO"), |
| 728 | 721 | }; |
| 729 | 722 | } |
| 730 | 723 | }; |
| ... | ... | @@ -1940,13 +1933,18 @@ pub fn updateFunc(wasm: *Wasm, pt: Zcu.PerThread, func_index: InternPool.Index, |
| 1940 | 1933 | |
| 1941 | 1934 | dev.check(.wasm_backend); |
| 1942 | 1935 | |
| 1936 | const gpa = pt.zcu.gpa; |
| 1937 | try wasm.functions.ensureUnusedCapacity(gpa, 1); |
| 1938 | try wasm.zcu_funcs.ensureUnusedCapacity(gpa, 1); |
| 1939 | |
| 1943 | 1940 | // This converts AIR to MIR but does not yet lower to wasm code. |
| 1944 | 1941 | // That lowering happens during `flush`, after garbage collection, which |
| 1945 | 1942 | // can affect function and global indexes, which affects the LEB integer |
| 1946 | 1943 | // encoding, which affects the output binary size. |
| 1947 | | try wasm.zcu_funcs.put(pt.zcu.gpa, func_index, .{ |
| 1944 | wasm.zcu_funcs.putAssumeCapacity(func_index, .{ |
| 1948 | 1945 | .function = try CodeGen.function(wasm, pt, func_index, air, liveness), |
| 1949 | 1946 | }); |
| 1947 | wasm.functions.putAssumeCapacity(.pack(wasm, .{ .zcu_func = @enumFromInt(wasm.zcu_funcs.entries.len - 1) }), {}); |
| 1950 | 1948 | } |
| 1951 | 1949 | |
| 1952 | 1950 | // Generate code for the "Nav", storing it in memory to be later written to |
| ... | ... | @@ -1963,19 +1961,14 @@ pub fn updateNav(wasm: *Wasm, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index |
| 1963 | 1961 | const gpa = comp.gpa; |
| 1964 | 1962 | const is_obj = comp.config.output_mode == .Obj; |
| 1965 | 1963 | |
| 1966 | | const nav_val = zcu.navValue(nav_index); |
| 1967 | | const is_extern, const nav_init = switch (ip.indexToKey(nav_val.toIntern())) { |
| 1968 | | .variable => |variable| .{ false, Value.fromInterned(variable.init) }, |
| 1969 | | .func => unreachable, |
| 1970 | | .@"extern" => b: { |
| 1971 | | assert(!ip.isFunctionType(nav.typeOf(ip))); |
| 1972 | | break :b .{ true, nav_val }; |
| 1973 | | }, |
| 1974 | | else => .{ false, nav_val }, |
| 1964 | const is_extern, const nav_init = switch (ip.indexToKey(nav.status.resolved.val)) { |
| 1965 | .func => return, |
| 1966 | .@"extern" => .{ true, .none }, |
| 1967 | .variable => |variable| .{ false, variable.init }, |
| 1968 | else => .{ false, nav.status.resolved.val }, |
| 1975 | 1969 | }; |
| 1976 | | |
| 1977 | | if (!nav_init.typeOf(zcu).hasRuntimeBits(zcu)) { |
| 1978 | | _ = wasm.imports.swapRemove(nav_index); |
| 1970 | if (is_extern) { |
| 1971 | try wasm.imports.put(gpa, nav_index, {}); |
| 1979 | 1972 | if (is_obj) { |
| 1980 | 1973 | if (wasm.navs_obj.swapRemove(nav_index)) @panic("TODO reclaim resources"); |
| 1981 | 1974 | } else { |
| ... | ... | @@ -1983,9 +1976,9 @@ pub fn updateNav(wasm: *Wasm, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index |
| 1983 | 1976 | } |
| 1984 | 1977 | return; |
| 1985 | 1978 | } |
| 1979 | _ = wasm.imports.swapRemove(nav_index); |
| 1986 | 1980 | |
| 1987 | | if (is_extern) { |
| 1988 | | try wasm.imports.put(gpa, nav_index, {}); |
| 1981 | if (nav_init != .none and !Value.fromInterned(nav_init).typeOf(zcu).hasRuntimeBits(zcu)) { |
| 1989 | 1982 | if (is_obj) { |
| 1990 | 1983 | if (wasm.navs_obj.swapRemove(nav_index)) @panic("TODO reclaim resources"); |
| 1991 | 1984 | } else { |
| ... | ... | @@ -2002,7 +1995,7 @@ pub fn updateNav(wasm: *Wasm, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index |
| 2002 | 1995 | &wasm.base, |
| 2003 | 1996 | pt, |
| 2004 | 1997 | zcu.navSrcLoc(nav_index), |
| 2005 | | nav_init, |
| 1998 | Value.fromInterned(nav_init), |
| 2006 | 1999 | &wasm.string_bytes, |
| 2007 | 2000 | .none, |
| 2008 | 2001 | ); |
| ... | ... | @@ -2030,11 +2023,6 @@ pub fn updateNav(wasm: *Wasm, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index |
| 2030 | 2023 | |
| 2031 | 2024 | if (is_obj) { |
| 2032 | 2025 | const gop = try wasm.navs_obj.getOrPut(gpa, nav_index); |
| 2033 | | if (gop.found_existing) { |
| 2034 | | @panic("TODO reuse these resources"); |
| 2035 | | } else { |
| 2036 | | _ = wasm.imports.swapRemove(nav_index); |
| 2037 | | } |
| 2038 | 2026 | gop.value_ptr.* = .{ |
| 2039 | 2027 | .code = code, |
| 2040 | 2028 | .relocs = .{ |
| ... | ... | @@ -2047,11 +2035,6 @@ pub fn updateNav(wasm: *Wasm, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index |
| 2047 | 2035 | assert(relocs_len == 0); |
| 2048 | 2036 | |
| 2049 | 2037 | const gop = try wasm.navs_exe.getOrPut(gpa, nav_index); |
| 2050 | | if (gop.found_existing) { |
| 2051 | | @panic("TODO reuse these resources"); |
| 2052 | | } else { |
| 2053 | | _ = wasm.imports.swapRemove(nav_index); |
| 2054 | | } |
| 2055 | 2038 | gop.value_ptr.* = .{ |
| 2056 | 2039 | .code = code, |
| 2057 | 2040 | }; |
| ... | ... | @@ -2072,9 +2055,13 @@ pub fn deleteExport( |
| 2072 | 2055 | |
| 2073 | 2056 | const zcu = wasm.base.comp.zcu.?; |
| 2074 | 2057 | const ip = &zcu.intern_pool; |
| 2075 | | const export_name = wasm.getExistingString(name.toSlice(ip)).?; |
| 2058 | const name_slice = name.toSlice(ip); |
| 2059 | const export_name = wasm.getExistingString(name_slice).?; |
| 2076 | 2060 | switch (exported) { |
| 2077 | | .nav => |nav_index| assert(wasm.nav_exports.swapRemove(.{ .nav_index = nav_index, .name = export_name })), |
| 2061 | .nav => |nav_index| { |
| 2062 | log.debug("deleteExport '{s}' nav={d}", .{ name_slice, @intFromEnum(nav_index) }); |
| 2063 | assert(wasm.nav_exports.swapRemove(.{ .nav_index = nav_index, .name = export_name })); |
| 2064 | }, |
| 2078 | 2065 | .uav => |uav_index| assert(wasm.uav_exports.swapRemove(.{ .uav_index = uav_index, .name = export_name })), |
| 2079 | 2066 | } |
| 2080 | 2067 | wasm.any_exports_updated = true; |
| ... | ... | @@ -2096,9 +2083,13 @@ pub fn updateExports( |
| 2096 | 2083 | const ip = &zcu.intern_pool; |
| 2097 | 2084 | for (export_indices) |export_idx| { |
| 2098 | 2085 | const exp = export_idx.ptr(zcu); |
| 2099 | | const name = try wasm.internString(exp.opts.name.toSlice(ip)); |
| 2086 | const name_slice = exp.opts.name.toSlice(ip); |
| 2087 | const name = try wasm.internString(name_slice); |
| 2100 | 2088 | switch (exported) { |
| 2101 | | .nav => |nav_index| try wasm.nav_exports.put(gpa, .{ .nav_index = nav_index, .name = name }, export_idx), |
| 2089 | .nav => |nav_index| { |
| 2090 | log.debug("updateExports '{s}' nav={d}", .{ name_slice, @intFromEnum(nav_index) }); |
| 2091 | try wasm.nav_exports.put(gpa, .{ .nav_index = nav_index, .name = name }, export_idx); |
| 2092 | }, |
| 2102 | 2093 | .uav => |uav_index| try wasm.uav_exports.put(gpa, .{ .uav_index = uav_index, .name = name }, export_idx), |
| 2103 | 2094 | } |
| 2104 | 2095 | } |