authorgravatar for paul.verigo@gmail.comPavel Verigo <paul.verigo@gmail.com> 2026-10-20 03:52:17+02:00
committergravatar for paul.verigo@gmail.comPavel Verigo <paul.verigo@gmail.com> 2026-10-20 23:10:13+02:00
log72f3d3aeea07c5b16fb97c789b898f18e05eda9e
treee1aa8b61c795c87c7565fdf6dd2fca449648d1ea
parent151597cc0dcfcad384218a2b92cae4b23b11ebe6

wasm: no pt in CodeGen


1 files changed, 123 insertions(+), 131 deletions(-)

src/codegen/wasm/CodeGen.zig+123-131
...@@ -157,7 +157,7 @@ simd_immediates: std.ArrayList([16]u8) = .empty,...@@ -157,7 +157,7 @@ simd_immediates: std.ArrayList([16]u8) = .empty,
157/// The Target we're emitting (used to call intInfo)157/// The Target we're emitting (used to call intInfo)
158target: *const std.Target,158target: *const std.Target,
159ptr_size: enum { wasm32, wasm64 },159ptr_size: enum { wasm32, wasm64 },
160pt: Zcu.PerThread,160zcu: *Zcu,
161/// List of MIR Instructions161/// List of MIR Instructions
162mir_instructions: std.MultiArrayList(Mir.Inst),162mir_instructions: std.MultiArrayList(Mir.Inst),
163/// Contains extra data for MIR163/// Contains extra data for MIR
...@@ -380,7 +380,7 @@ pub fn deinit(cg: *CodeGen) void {...@@ -380,7 +380,7 @@ pub fn deinit(cg: *CodeGen) void {
380}380}
381381
382pub fn fail(cg: *CodeGen, comptime fmt: []const u8, args: anytype) Error {382pub fn fail(cg: *CodeGen, comptime fmt: []const u8, args: anytype) Error {
383 const zcu = cg.pt.zcu;383 const zcu = cg.zcu;
384 const func = zcu.funcInfo(cg.func_index);384 const func = zcu.funcInfo(cg.func_index);
385 return zcu.codegenFail(func.owner_nav, fmt, args);385 return zcu.codegenFail(func.owner_nav, fmt, args);
386}386}
...@@ -395,8 +395,7 @@ fn resolveInst(cg: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue {...@@ -395,8 +395,7 @@ fn resolveInst(cg: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue {
395 const gop = try cg.values.getOrPut(cg.gpa, ref);395 const gop = try cg.values.getOrPut(cg.gpa, ref);
396 assert(!gop.found_existing);396 assert(!gop.found_existing);
397397
398 const pt = cg.pt;398 const zcu = cg.zcu;
399 const zcu = pt.zcu;
400 const val: Value = .fromInterned(ref.toInterned().?);399 const val: Value = .fromInterned(ref.toInterned().?);
401 const ty = cg.typeOf(ref);400 const ty = cg.typeOf(ref);
402 if (!ty.hasRuntimeBits(zcu) and !ty.isInt(zcu) and !ty.isError(zcu)) {401 if (!ty.hasRuntimeBits(zcu) and !ty.isInt(zcu) and !ty.isError(zcu)) {
...@@ -420,7 +419,7 @@ fn resolveInst(cg: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue {...@@ -420,7 +419,7 @@ fn resolveInst(cg: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue {
420}419}
421420
422fn resolveValue(cg: *CodeGen, val: Value) InnerError!WValue {421fn resolveValue(cg: *CodeGen, val: Value) InnerError!WValue {
423 const zcu = cg.pt.zcu;422 const zcu = cg.zcu;
424 const ty = val.typeOf(zcu);423 const ty = val.typeOf(zcu);
425424
426 return if (isByRef(ty, zcu, cg.target))425 return if (isByRef(ty, zcu, cg.target))
...@@ -761,7 +760,7 @@ fn emitWValue(cg: *CodeGen, value: WValue) InnerError!void {...@@ -761,7 +760,7 @@ fn emitWValue(cg: *CodeGen, value: WValue) InnerError!void {
761 .float32 => |val| try cg.addFloat32(val),760 .float32 => |val| try cg.addFloat32(val),
762 .float64 => |val| try cg.addFloat64(val),761 .float64 => |val| try cg.addFloat64(val),
763 .nav_ref => |nav_ref| {762 .nav_ref => |nav_ref| {
764 const zcu = cg.pt.zcu;763 const zcu = cg.zcu;
765 const ip = &zcu.intern_pool;764 const ip = &zcu.intern_pool;
766 if (ip.zigTypeTag(ip.getNav(nav_ref.nav_index).resolved.?.type) == .@"fn") {765 if (ip.zigTypeTag(ip.getNav(nav_ref.nav_index).resolved.?.type) == .@"fn") {
767 assert(nav_ref.offset == 0);766 assert(nav_ref.offset == 0);
...@@ -782,7 +781,7 @@ fn emitWValue(cg: *CodeGen, value: WValue) InnerError!void {...@@ -782,7 +781,7 @@ fn emitWValue(cg: *CodeGen, value: WValue) InnerError!void {
782 }781 }
783 },782 },
784 .uav_ref => |uav| {783 .uav_ref => |uav| {
785 const zcu = cg.pt.zcu;784 const zcu = cg.zcu;
786 const ip = &zcu.intern_pool;785 const ip = &zcu.intern_pool;
787 assert(!ip.isFunctionType(ip.typeOf(uav.ip_index)));786 assert(!ip.isFunctionType(ip.typeOf(uav.ip_index)));
788 const gop = try cg.mir_uavs.getOrPut(cg.gpa, uav.ip_index);787 const gop = try cg.mir_uavs.getOrPut(cg.gpa, uav.ip_index);
...@@ -853,7 +852,7 @@ fn releaseLocal(cg: *CodeGen, index: u32) void {...@@ -853,7 +852,7 @@ fn releaseLocal(cg: *CodeGen, index: u32) void {
853/// Creates one locals for a given `Type`.852/// Creates one locals for a given `Type`.
854/// Returns a corresponding `Wvalue` with `local` as active tag853/// Returns a corresponding `Wvalue` with `local` as active tag
855fn allocLocal(cg: *CodeGen, ty: Type) InnerError!WValue {854fn allocLocal(cg: *CodeGen, ty: Type) InnerError!WValue {
856 const zcu = cg.pt.zcu;855 const zcu = cg.zcu;
857 const valtype = typeToValtype(ty, zcu, cg.target);856 const valtype = typeToValtype(ty, zcu, cg.target);
858 const free_list = cg.local_pool.freeList(valtype);857 const free_list = cg.local_pool.freeList(valtype);
859 if (free_list.unwrap()) |index| {858 if (free_list.unwrap()) |index| {
...@@ -870,7 +869,7 @@ fn allocLocal(cg: *CodeGen, ty: Type) InnerError!WValue {...@@ -870,7 +869,7 @@ fn allocLocal(cg: *CodeGen, ty: Type) InnerError!WValue {
870/// Ensures a new local will be created. This is useful when it's useful869/// Ensures a new local will be created. This is useful when it's useful
871/// to use a zero-initialized local.870/// to use a zero-initialized local.
872fn ensureAllocLocal(cg: *CodeGen, ty: Type) InnerError!WValue {871fn ensureAllocLocal(cg: *CodeGen, ty: Type) InnerError!WValue {
873 const zcu = cg.pt.zcu;872 const zcu = cg.zcu;
874 try cg.mir_locals.ensureUnusedCapacity(cg.gpa, 1);873 try cg.mir_locals.ensureUnusedCapacity(cg.gpa, 1);
875 try cg.local_pool.locals.ensureUnusedCapacity(cg.gpa, 1);874 try cg.local_pool.locals.ensureUnusedCapacity(cg.gpa, 1);
876 assert(cg.mir_locals.items.len == cg.local_pool.locals.items.len);875 assert(cg.mir_locals.items.len == cg.local_pool.locals.items.len);
...@@ -905,7 +904,7 @@ pub fn generate(...@@ -905,7 +904,7 @@ pub fn generate(
905904
906 var code_gen: CodeGen = .{905 var code_gen: CodeGen = .{
907 .gpa = gpa,906 .gpa = gpa,
908 .pt = pt,907 .zcu = zcu,
909 .air = air.*,908 .air = air.*,
910 .liveness = liveness.*.?,909 .liveness = liveness.*.?,
911 .owner_nav = cg.owner_nav,910 .owner_nav = cg.owner_nav,
...@@ -940,7 +939,7 @@ pub fn generate(...@@ -940,7 +939,7 @@ pub fn generate(
940}939}
941940
942fn generateInner(cg: *CodeGen, any_returns: bool) InnerError!Mir {941fn generateInner(cg: *CodeGen, any_returns: bool) InnerError!Mir {
943 const zcu = cg.pt.zcu;942 const zcu = cg.zcu;
944 cg.wasm_param_count = @intCast(cg.args.len + @intFromBool(cg.return_value != .none) + @intFromBool(cg.varargs != .none));943 cg.wasm_param_count = @intCast(cg.args.len + @intFromBool(cg.return_value != .none) + @intFromBool(cg.varargs != .none));
945 cg.initial_stack_value = try cg.ensureAllocLocal(Type.usize);944 cg.initial_stack_value = try cg.ensureAllocLocal(Type.usize);
946 cg.bottom_stack_value = try cg.ensureAllocLocal(Type.usize);945 cg.bottom_stack_value = try cg.ensureAllocLocal(Type.usize);
...@@ -1099,7 +1098,7 @@ fn lowerArg(cg: *CodeGen, cc: std.lang.CallingConvention, ty: Type, value: WValu...@@ -1099,7 +1098,7 @@ fn lowerArg(cg: *CodeGen, cc: std.lang.CallingConvention, ty: Type, value: WValu
1099 return cg.emitWValue(value);1098 return cg.emitWValue(value);
1100 }1099 }
11011100
1102 const zcu = cg.pt.zcu;1101 const zcu = cg.zcu;
11031102
1104 switch (abi.classifyType(ty, zcu, cg.target)) {1103 switch (abi.classifyType(ty, zcu, cg.target)) {
1105 .direct => |scalar_ty| {1104 .direct => |scalar_ty| {
...@@ -1148,8 +1147,7 @@ fn restoreStackPointer(cg: *CodeGen) !void {...@@ -1148,8 +1147,7 @@ fn restoreStackPointer(cg: *CodeGen) !void {
1148///1147///
1149/// Asserts Type has codegenbits1148/// Asserts Type has codegenbits
1150fn allocStack(cg: *CodeGen, ty: Type) !WValue {1149fn allocStack(cg: *CodeGen, ty: Type) !WValue {
1151 const pt = cg.pt;1150 const zcu = cg.zcu;
1152 const zcu = pt.zcu;
1153 assert(ty.hasRuntimeBits(zcu));1151 assert(ty.hasRuntimeBits(zcu));
11541152
1155 const abi_size = std.math.cast(u32, ty.abiSize(zcu)) orelse {1153 const abi_size = std.math.cast(u32, ty.abiSize(zcu)) orelse {
...@@ -1187,8 +1185,7 @@ fn allocStackBytes(cg: *CodeGen, size: u32, alignment: Alignment) !WValue {...@@ -1187,8 +1185,7 @@ fn allocStackBytes(cg: *CodeGen, size: u32, alignment: Alignment) !WValue {
1187/// This is different from allocStack where this will use the pointer's alignment1185/// This is different from allocStack where this will use the pointer's alignment
1188/// if it is set, to ensure the stack alignment will be set correctly.1186/// if it is set, to ensure the stack alignment will be set correctly.
1189fn allocStackPtr(cg: *CodeGen, inst: Air.Inst.Index) !WValue {1187fn allocStackPtr(cg: *CodeGen, inst: Air.Inst.Index) !WValue {
1190 const pt = cg.pt;1188 const zcu = cg.zcu;
1191 const zcu = pt.zcu;
1192 const ptr_ty = cg.typeOfIndex(inst);1189 const ptr_ty = cg.typeOfIndex(inst);
1193 const pointee_ty = ptr_ty.childType(zcu);1190 const pointee_ty = ptr_ty.childType(zcu);
11941191
...@@ -1356,7 +1353,7 @@ pub fn determineSimdStoreStrategy(ty: Type, zcu: *const Zcu, target: *const std....@@ -1356,7 +1353,7 @@ pub fn determineSimdStoreStrategy(ty: Type, zcu: *const Zcu, target: *const std.
1356}1353}
13571354
1358fn genInst(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {1355fn genInst(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
1359 const zcu = cg.pt.zcu;1356 const zcu = cg.zcu;
1360 const air_tags = cg.air.instructions.items(.tag);1357 const air_tags = cg.air.instructions.items(.tag);
1361 return switch (air_tags[@backingInt(inst)]) {1358 return switch (air_tags[@backingInt(inst)]) {
1362 // No soft float legalizations are enabled.1359 // No soft float legalizations are enabled.
...@@ -1492,7 +1489,7 @@ fn genInst(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -1492,7 +1489,7 @@ fn genInst(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
1492 const rhs = try cg.resolveInst(bin_op.rhs);1489 const rhs = try cg.resolveInst(bin_op.rhs);
1493 const ty = cg.typeOfIndex(inst);1490 const ty = cg.typeOfIndex(inst);
14941491
1495 if (ty.zigTypeTag(cg.pt.zcu) == .vector) {1492 if (ty.zigTypeTag(cg.zcu) == .vector) {
1496 return cg.fail("TODO: implement AIR op: mul_add for vectors", .{});1493 return cg.fail("TODO: implement AIR op: mul_add for vectors", .{});
1497 }1494 }
14981495
...@@ -1510,7 +1507,7 @@ fn genInst(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -1510,7 +1507,7 @@ fn genInst(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
1510 const rhs = try cg.resolveInst(bin_op.rhs);1507 const rhs = try cg.resolveInst(bin_op.rhs);
1511 const ty = cg.typeOfIndex(inst);1508 const ty = cg.typeOfIndex(inst);
15121509
1513 if (ty.zigTypeTag(cg.pt.zcu) == .vector) {1510 if (ty.zigTypeTag(cg.zcu) == .vector) {
1514 return cg.fail("TODO: implement AIR op: {s} for vectors", .{@tagName(tag)});1511 return cg.fail("TODO: implement AIR op: {s} for vectors", .{@tagName(tag)});
1515 }1512 }
15161513
...@@ -1555,7 +1552,7 @@ fn genInst(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -1555,7 +1552,7 @@ fn genInst(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
1555 defer res_tmp.free(cg);1552 defer res_tmp.free(cg);
15561553
1557 const result = try cg.allocStack(cg.typeOfIndex(inst));1554 const result = try cg.allocStack(cg.typeOfIndex(inst));
1558 const offset: u32 = @intCast(ty.abiSize(cg.pt.zcu));1555 const offset: u32 = @intCast(ty.abiSize(cg.zcu));
15591556
1560 try cg.store(result, res_tmp, ty, .{});1557 try cg.store(result, res_tmp, ty, .{});
1561 try cg.store(result, ov_tmp, .u1, .{ .offset = offset });1558 try cg.store(result, ov_tmp, .u1, .{ .offset = offset });
...@@ -1675,7 +1672,7 @@ fn genInst(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -1675,7 +1672,7 @@ fn genInst(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
1675 const src_ty = cg.typeOf(ty_op.operand);1672 const src_ty = cg.typeOf(ty_op.operand);
1676 const dest_ty = cg.typeOfIndex(inst);1673 const dest_ty = cg.typeOfIndex(inst);
16771674
1678 if (dest_ty.zigTypeTag(cg.pt.zcu) == .vector) {1675 if (dest_ty.zigTypeTag(cg.zcu) == .vector) {
1679 return cg.fail("TODO: implement AIR op: {s} for vectors", .{@tagName(tag)});1676 return cg.fail("TODO: implement AIR op: {s} for vectors", .{@tagName(tag)});
1680 }1677 }
16811678
...@@ -1959,7 +1956,7 @@ fn genInst(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -1959,7 +1956,7 @@ fn genInst(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
1959}1956}
19601957
1961fn genBody(cg: *CodeGen, body: []const Air.Inst.Index) InnerError!void {1958fn genBody(cg: *CodeGen, body: []const Air.Inst.Index) InnerError!void {
1962 const zcu = cg.pt.zcu;1959 const zcu = cg.zcu;
1963 const ip = &zcu.intern_pool;1960 const ip = &zcu.intern_pool;
19641961
1965 for (body) |inst| {1962 for (body) |inst| {
...@@ -1979,7 +1976,7 @@ fn genBody(cg: *CodeGen, body: []const Air.Inst.Index) InnerError!void {...@@ -1979,7 +1976,7 @@ fn genBody(cg: *CodeGen, body: []const Air.Inst.Index) InnerError!void {
1979}1976}
19801977
1981fn airRet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {1978fn airRet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
1982 const zcu = cg.pt.zcu;1979 const zcu = cg.zcu;
1983 const un_op = cg.air.instructions.items(.data)[@backingInt(inst)].un_op;1980 const un_op = cg.air.instructions.items(.data)[@backingInt(inst)].un_op;
1984 const operand = try cg.resolveInst(un_op);1981 const operand = try cg.resolveInst(un_op);
1985 const fn_info = zcu.typeToFunc(zcu.navValue(cg.owner_nav).typeOf(zcu)).?;1982 const fn_info = zcu.typeToFunc(zcu.navValue(cg.owner_nav).typeOf(zcu)).?;
...@@ -2019,7 +2016,7 @@ fn airRet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -2019,7 +2016,7 @@ fn airRet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
2019}2016}
20202017
2021fn airRetPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {2018fn airRetPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
2022 const zcu = cg.pt.zcu;2019 const zcu = cg.zcu;
2023 const child_type = cg.typeOfIndex(inst).childType(zcu);2020 const child_type = cg.typeOfIndex(inst).childType(zcu);
20242021
2025 const result = result: {2022 const result = result: {
...@@ -2039,7 +2036,7 @@ fn airRetPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -2039,7 +2036,7 @@ fn airRetPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
2039}2036}
20402037
2041fn airRetLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {2038fn airRetLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
2042 const zcu = cg.pt.zcu;2039 const zcu = cg.zcu;
2043 const un_op = cg.air.instructions.items(.data)[@backingInt(inst)].un_op;2040 const un_op = cg.air.instructions.items(.data)[@backingInt(inst)].un_op;
2044 const operand = try cg.resolveInst(un_op);2041 const operand = try cg.resolveInst(un_op);
2045 const ret_ty = cg.typeOf(un_op).childType(zcu);2042 const ret_ty = cg.typeOf(un_op).childType(zcu);
...@@ -2076,8 +2073,7 @@ fn airCall(cg: *CodeGen, inst: Air.Inst.Index, modifier: std.lang.CallModifier)...@@ -2076,8 +2073,7 @@ fn airCall(cg: *CodeGen, inst: Air.Inst.Index, modifier: std.lang.CallModifier)
2076 const args = call.args;2073 const args = call.args;
2077 const ty = cg.typeOf(call.callee);2074 const ty = cg.typeOf(call.callee);
20782075
2079 const pt = cg.pt;2076 const zcu = cg.zcu;
2080 const zcu = pt.zcu;
2081 const ip = &zcu.intern_pool;2077 const ip = &zcu.intern_pool;
2082 const fn_ty = switch (ty.zigTypeTag(zcu)) {2078 const fn_ty = switch (ty.zigTypeTag(zcu)) {
2083 .@"fn" => ty,2079 .@"fn" => ty,
...@@ -2250,7 +2246,7 @@ fn airVaCopy(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -2250,7 +2246,7 @@ fn airVaCopy(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
2250}2246}
22512247
2252fn airVaArg(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {2248fn airVaArg(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
2253 const zcu = cg.pt.zcu;2249 const zcu = cg.zcu;
2254 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;2250 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
2255 const operand = try cg.resolveInst(ty_op.operand);2251 const operand = try cg.resolveInst(ty_op.operand);
22562252
...@@ -2322,8 +2318,7 @@ fn airAlloc(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -2322,8 +2318,7 @@ fn airAlloc(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
2322}2318}
23232319
2324fn airStore(cg: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void {2320fn airStore(cg: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void {
2325 const pt = cg.pt;2321 const zcu = cg.zcu;
2326 const zcu = pt.zcu;
2327 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;2322 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;
23282323
2329 const lhs = try cg.resolveInst(bin_op.lhs);2324 const lhs = try cg.resolveInst(bin_op.lhs);
...@@ -2366,8 +2361,7 @@ const MemArg = struct {...@@ -2366,8 +2361,7 @@ const MemArg = struct {
23662361
2367fn store(cg: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, memarg: MemArg) InnerError!void {2362fn store(cg: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, memarg: MemArg) InnerError!void {
2368 assert(!(lhs != .stack and rhs == .stack));2363 assert(!(lhs != .stack and rhs == .stack));
2369 const pt = cg.pt;2364 const zcu = cg.zcu;
2370 const zcu = pt.zcu;
2371 const abi_size = ty.abiSize(zcu);2365 const abi_size = ty.abiSize(zcu);
23722366
2373 if (!ty.hasRuntimeBits(zcu)) return;2367 if (!ty.hasRuntimeBits(zcu)) return;
...@@ -2432,8 +2426,7 @@ fn store(cg: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, memarg: MemArg) Inner...@@ -2432,8 +2426,7 @@ fn store(cg: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, memarg: MemArg) Inner
2432}2426}
24332427
2434fn airLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {2428fn airLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
2435 const pt = cg.pt;2429 const zcu = cg.zcu;
2436 const zcu = pt.zcu;
2437 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;2430 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
2438 const operand = try cg.resolveInst(ty_op.operand);2431 const operand = try cg.resolveInst(ty_op.operand);
2439 const elem_ty = ty_op.ty;2432 const elem_ty = ty_op.ty;
...@@ -2459,7 +2452,7 @@ fn airLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -2459,7 +2452,7 @@ fn airLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
2459/// Loads an operand from the linear memory section.2452/// Loads an operand from the linear memory section.
2460/// NOTE: Leaves the value on the stack, if isByRef == false.2453/// NOTE: Leaves the value on the stack, if isByRef == false.
2461fn load(cg: *CodeGen, operand: WValue, ty: Type, memarg: MemArg) InnerError!WValue {2454fn load(cg: *CodeGen, operand: WValue, ty: Type, memarg: MemArg) InnerError!WValue {
2462 const zcu = cg.pt.zcu;2455 const zcu = cg.zcu;
24632456
2464 const natural_alignment = ty.abiAlignment(zcu);2457 const natural_alignment = ty.abiAlignment(zcu);
2465 // wasm memory instruction alignment is <= access width2458 // wasm memory instruction alignment is <= access width
...@@ -2537,8 +2530,7 @@ fn load(cg: *CodeGen, operand: WValue, ty: Type, memarg: MemArg) InnerError!WVal...@@ -2537,8 +2530,7 @@ fn load(cg: *CodeGen, operand: WValue, ty: Type, memarg: MemArg) InnerError!WVal
2537}2530}
25382531
2539fn airArg(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {2532fn airArg(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
2540 const pt = cg.pt;2533 const zcu = cg.zcu;
2541 const zcu = pt.zcu;
2542 const arg_index = cg.arg_index;2534 const arg_index = cg.arg_index;
2543 const arg = cg.args[arg_index];2535 const arg = cg.args[arg_index];
2544 const cc = zcu.typeToFunc(zcu.navValue(cg.owner_nav).typeOf(zcu)).?.cc;2536 const cc = zcu.typeToFunc(zcu.navValue(cg.owner_nav).typeOf(zcu)).?.cc;
...@@ -2590,7 +2582,7 @@ const IntType = struct {...@@ -2590,7 +2582,7 @@ const IntType = struct {
2590 // Adapted from x86_64 backend2582 // Adapted from x86_64 backend
2591 // Differ from Type.intInfo as it treats pointers/booleans/packed/enums/errors as integer2583 // Differ from Type.intInfo as it treats pointers/booleans/packed/enums/errors as integer
2592 fn fromType(cg: *CodeGen, ty: Type) IntType {2584 fn fromType(cg: *CodeGen, ty: Type) IntType {
2593 const zcu = cg.pt.zcu;2585 const zcu = cg.zcu;
2594 const ip = &zcu.intern_pool;2586 const ip = &zcu.intern_pool;
2595 var ty_index = ty.ip_index;2587 var ty_index = ty.ip_index;
2596 while (true) switch (ip.indexToKey(ty_index)) {2588 while (true) switch (ip.indexToKey(ty_index)) {
...@@ -5041,8 +5033,7 @@ fn floatFromInt(cg: *CodeGen, dest_ty: FloatType, src_ty: IntType, operand: WVal...@@ -5041,8 +5033,7 @@ fn floatFromInt(cg: *CodeGen, dest_ty: FloatType, src_ty: IntType, operand: WVal
5041}5033}
50425034
5043fn lowerPtr(cg: *CodeGen, ptr_val: InternPool.Index, prev_offset: u64) InnerError!WValue {5035fn lowerPtr(cg: *CodeGen, ptr_val: InternPool.Index, prev_offset: u64) InnerError!WValue {
5044 const pt = cg.pt;5036 const zcu = cg.zcu;
5045 const zcu = pt.zcu;
5046 const ip = &zcu.intern_pool;5037 const ip = &zcu.intern_pool;
5047 const ptr = zcu.intern_pool.indexToKey(ptr_val).ptr;5038 const ptr = zcu.intern_pool.indexToKey(ptr_val).ptr;
5048 const offset: u64 = prev_offset + ptr.byte_offset;5039 const offset: u64 = prev_offset + ptr.byte_offset;
...@@ -5056,7 +5047,10 @@ fn lowerPtr(cg: *CodeGen, ptr_val: InternPool.Index, prev_offset: u64) InnerErro...@@ -5056,7 +5047,10 @@ fn lowerPtr(cg: *CodeGen, ptr_val: InternPool.Index, prev_offset: u64) InnerErro
5056 .{ .uav_ref = .{ .ip_index = uav.val, .offset = @intCast(offset), .orig_ptr_ty = uav.orig_ty } }5047 .{ .uav_ref = .{ .ip_index = uav.val, .offset = @intCast(offset), .orig_ptr_ty = uav.orig_ty } }
5057 else5048 else
5058 .{ .imm32 = @intCast(Type.fromInterned(uav.orig_ty).ptrAlignment(zcu).forward(@as(u32, 0xaaaaaaaa))) },5049 .{ .imm32 = @intCast(Type.fromInterned(uav.orig_ty).ptrAlignment(zcu).forward(@as(u32, 0xaaaaaaaa))) },
5059 .int => return cg.lowerConstant(try pt.intValue(.usize, offset)),5050 .int => return switch (cg.ptr_size) {
5051 .wasm32 => .{ .imm32 = @intCast(offset) },
5052 .wasm64 => .{ .imm64 = offset },
5053 },
5060 .eu_payload => |eu_ptr| try cg.lowerPtr(5054 .eu_payload => |eu_ptr| try cg.lowerPtr(
5061 eu_ptr,5055 eu_ptr,
5062 offset + codegen.errUnionPayloadOffset(5056 offset + codegen.errUnionPayloadOffset(
...@@ -5095,8 +5089,7 @@ fn lowerPtr(cg: *CodeGen, ptr_val: InternPool.Index, prev_offset: u64) InnerErro...@@ -5095,8 +5089,7 @@ fn lowerPtr(cg: *CodeGen, ptr_val: InternPool.Index, prev_offset: u64) InnerErro
50955089
5096/// Asserts that `isByRef` returns `false` for `val.typeOf(zcu)`.5090/// Asserts that `isByRef` returns `false` for `val.typeOf(zcu)`.
5097fn lowerConstant(cg: *CodeGen, val: Value) InnerError!WValue {5091fn lowerConstant(cg: *CodeGen, val: Value) InnerError!WValue {
5098 const pt = cg.pt;5092 const zcu = cg.zcu;
5099 const zcu = pt.zcu;
5100 const ty = val.typeOf(zcu);5093 const ty = val.typeOf(zcu);
5101 assert(!isByRef(ty, zcu, cg.target));5094 assert(!isByRef(ty, zcu, cg.target));
5102 const ip = &zcu.intern_pool;5095 const ip = &zcu.intern_pool;
...@@ -5154,22 +5147,16 @@ fn lowerConstant(cg: *CodeGen, val: Value) InnerError!WValue {...@@ -5154,22 +5147,16 @@ fn lowerConstant(cg: *CodeGen, val: Value) InnerError!WValue {
5154 }5147 }
5155 },5148 },
5156 .err => |err| {5149 .err => |err| {
5157 const int = try pt.getErrorValue(err.name);5150 const int = ip.getErrorValueIfExists(err.name).?;
5158 return .{ .imm32 = int };5151 return .{ .imm32 = int };
5159 },5152 },
5160 .error_union => |error_union| {5153 .error_union => |error_union| {
5161 const err_int_ty = try pt.errorIntType();
5162 const err_val: Value = switch (error_union.val) {
5163 .err_name => |err_name| .fromInterned(try pt.intern(.{ .err = .{
5164 .ty = ty.errorUnionSet(zcu).toIntern(),
5165 .name = err_name,
5166 } })),
5167 .payload => try pt.intValue(err_int_ty, 0),
5168 };
5169 const payload_type = ty.errorUnionPayload(zcu);5154 const payload_type = ty.errorUnionPayload(zcu);
5170 if (!payload_type.hasRuntimeBits(zcu)) {5155 if (!payload_type.hasRuntimeBits(zcu)) {
5171 // We use the error type directly as the type.5156 return .{ .imm32 = switch (error_union.val) {
5172 return cg.lowerConstant(err_val);5157 .err_name => |err_name| ip.getErrorValueIfExists(err_name).?,
5158 .payload => 0,
5159 } };
5173 }5160 }
51745161
5175 return cg.fail("Wasm TODO: lowerConstant error union with non-zero-bit payload type", .{});5162 return cg.fail("Wasm TODO: lowerConstant error union with non-zero-bit payload type", .{});
...@@ -5233,7 +5220,7 @@ fn storeSimdImmd(cg: *CodeGen, value: [16]u8) !WValue {...@@ -5233,7 +5220,7 @@ fn storeSimdImmd(cg: *CodeGen, value: [16]u8) !WValue {
5233}5220}
52345221
5235fn emitUndefined(cg: *CodeGen, ty: Type) InnerError!WValue {5222fn emitUndefined(cg: *CodeGen, ty: Type) InnerError!WValue {
5236 const zcu = cg.pt.zcu;5223 const zcu = cg.zcu;
5237 switch (ty.zigTypeTag(zcu)) {5224 switch (ty.zigTypeTag(zcu)) {
5238 .vector => return cg.storeSimdImmd(@splat(0xaa)),5225 .vector => return cg.storeSimdImmd(@splat(0xaa)),
5239 .bool, .error_set => return .{ .imm32 = 0xaaaaaaaa },5226 .bool, .error_set => return .{ .imm32 = 0xaaaaaaaa },
...@@ -5276,7 +5263,7 @@ fn airBlock(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -5276,7 +5263,7 @@ fn airBlock(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5276}5263}
52775264
5278fn lowerBlock(cg: *CodeGen, inst: Air.Inst.Index, block_ty: Type, body: []const Air.Inst.Index) InnerError!void {5265fn lowerBlock(cg: *CodeGen, inst: Air.Inst.Index, block_ty: Type, body: []const Air.Inst.Index) InnerError!void {
5279 const zcu = cg.pt.zcu;5266 const zcu = cg.zcu;
52805267
5281 if (block_ty.isNoReturn(zcu)) {5268 if (block_ty.isNoReturn(zcu)) {
5282 assert(cg.liveness.getBlock(inst).deaths.len == 0);5269 assert(cg.liveness.getBlock(inst).deaths.len == 0);
...@@ -5385,7 +5372,7 @@ fn airCmp(cg: *CodeGen, inst: Air.Inst.Index, op: std.math.CompareOperator) Inne...@@ -5385,7 +5372,7 @@ fn airCmp(cg: *CodeGen, inst: Air.Inst.Index, op: std.math.CompareOperator) Inne
5385 const lhs = try cg.resolveInst(bin_op.lhs);5372 const lhs = try cg.resolveInst(bin_op.lhs);
5386 const rhs = try cg.resolveInst(bin_op.rhs);5373 const rhs = try cg.resolveInst(bin_op.rhs);
5387 const operand_ty = cg.typeOf(bin_op.lhs);5374 const operand_ty = cg.typeOf(bin_op.lhs);
5388 const zcu = cg.pt.zcu;5375 const zcu = cg.zcu;
53895376
5390 const type_tag = operand_ty.zigTypeTag(zcu);5377 const type_tag = operand_ty.zigTypeTag(zcu);
53915378
...@@ -5617,10 +5604,10 @@ fn airCmpLteErrorsLen(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -5617,10 +5604,10 @@ fn airCmpLteErrorsLen(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5617 const operand = try cg.resolveInst(un_op);5604 const operand = try cg.resolveInst(un_op);
56185605
5619 try cg.emitWValue(operand);5606 try cg.emitWValue(operand);
5620 const pt = cg.pt;5607 const zcu = cg.zcu;
5621 const err_int_ty = try pt.errorIntType();5608 const err_int_ty: IntType = .{ .is_signed = false, .bits = zcu.errorSetBits() };
5622 try cg.addTag(.errors_len);5609 try cg.addTag(.errors_len);
5623 const result = try cg.intCmp(.fromType(cg, err_int_ty), .lt, .stack, .stack);5610 const result = try cg.intCmp(err_int_ty, .lt, .stack, .stack);
56245611
5625 return cg.finishAir(inst, result, &.{un_op});5612 return cg.finishAir(inst, result, &.{un_op});
5626}5613}
...@@ -5676,7 +5663,7 @@ fn airUnreachable(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -5676,7 +5663,7 @@ fn airUnreachable(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5676}5663}
56775664
5678fn airNopCast(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {5665fn airNopCast(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5679 const zcu = cg.pt.zcu;5666 const zcu = cg.zcu;
5680 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;5667 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
56815668
5682 const operand_ty = cg.typeOf(ty_op.operand);5669 const operand_ty = cg.typeOf(ty_op.operand);
...@@ -5690,7 +5677,7 @@ fn airNopCast(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -5690,7 +5677,7 @@ fn airNopCast(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5690}5677}
56915678
5692fn airIntFromPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {5679fn airIntFromPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5693 const zcu = cg.pt.zcu;5680 const zcu = cg.zcu;
5694 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;5681 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
56955682
5696 const operand_ty = cg.typeOf(ty_op.operand);5683 const operand_ty = cg.typeOf(ty_op.operand);
...@@ -5709,7 +5696,7 @@ fn airIntFromPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -5709,7 +5696,7 @@ fn airIntFromPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5709}5696}
57105697
5711fn airUnionFromEnum(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {5698fn airUnionFromEnum(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5712 const zcu = cg.pt.zcu;5699 const zcu = cg.zcu;
5713 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;5700 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
57145701
5715 const union_ty = cg.typeOfIndex(inst);5702 const union_ty = cg.typeOfIndex(inst);
...@@ -5741,7 +5728,7 @@ const BitcastClass = union(enum) {...@@ -5741,7 +5728,7 @@ const BitcastClass = union(enum) {
5741};5728};
57425729
5743fn bitcastClass(cg: *CodeGen, ty: Type) BitcastClass {5730fn bitcastClass(cg: *CodeGen, ty: Type) BitcastClass {
5744 const zcu = cg.pt.zcu;5731 const zcu = cg.zcu;
5745 return switch (ty.zigTypeTag(zcu)) {5732 return switch (ty.zigTypeTag(zcu)) {
5746 .bool,5733 .bool,
5747 .int,5734 .int,
...@@ -5759,7 +5746,7 @@ fn bitcastClass(cg: *CodeGen, ty: Type) BitcastClass {...@@ -5759,7 +5746,7 @@ fn bitcastClass(cg: *CodeGen, ty: Type) BitcastClass {
5759fn bitcast(cg: *CodeGen, dest_ty: Type, src_ty: Type, operand: WValue) InnerError!WValue {5746fn bitcast(cg: *CodeGen, dest_ty: Type, src_ty: Type, operand: WValue) InnerError!WValue {
5760 if (dest_ty.eql(src_ty)) return operand;5747 if (dest_ty.eql(src_ty)) return operand;
57615748
5762 const zcu = cg.pt.zcu;5749 const zcu = cg.zcu;
5763 const src_class = cg.bitcastClass(src_ty);5750 const src_class = cg.bitcastClass(src_ty);
5764 const dest_class = cg.bitcastClass(dest_ty);5751 const dest_class = cg.bitcastClass(dest_ty);
5765 const src_by_ref = isByRef(src_ty, zcu, cg.target);5752 const src_by_ref = isByRef(src_ty, zcu, cg.target);
...@@ -5835,7 +5822,7 @@ fn bitcast(cg: *CodeGen, dest_ty: Type, src_ty: Type, operand: WValue) InnerErro...@@ -5835,7 +5822,7 @@ fn bitcast(cg: *CodeGen, dest_ty: Type, src_ty: Type, operand: WValue) InnerErro
5835}5822}
58365823
5837fn airStructFieldPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {5824fn airStructFieldPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5838 const zcu = cg.pt.zcu;5825 const zcu = cg.zcu;
5839 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;5826 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;
5840 const extra = cg.air.extraData(Air.StructField, ty_pl.payload);5827 const extra = cg.air.extraData(Air.StructField, ty_pl.payload);
58415828
...@@ -5847,7 +5834,7 @@ fn airStructFieldPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -5847,7 +5834,7 @@ fn airStructFieldPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5847}5834}
58485835
5849fn airStructFieldPtrIndex(cg: *CodeGen, inst: Air.Inst.Index, index: u32) InnerError!void {5836fn airStructFieldPtrIndex(cg: *CodeGen, inst: Air.Inst.Index, index: u32) InnerError!void {
5850 const zcu = cg.pt.zcu;5837 const zcu = cg.zcu;
5851 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;5838 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
5852 const struct_ptr = try cg.resolveInst(ty_op.operand);5839 const struct_ptr = try cg.resolveInst(ty_op.operand);
5853 const struct_ptr_ty = cg.typeOf(ty_op.operand);5840 const struct_ptr_ty = cg.typeOf(ty_op.operand);
...@@ -5865,8 +5852,7 @@ fn structFieldPtr(...@@ -5865,8 +5852,7 @@ fn structFieldPtr(
5865 struct_ty: Type,5852 struct_ty: Type,
5866 index: u32,5853 index: u32,
5867) InnerError!WValue {5854) InnerError!WValue {
5868 const pt = cg.pt;5855 const zcu = cg.zcu;
5869 const zcu = pt.zcu;
5870 const result_ty = cg.typeOfIndex(inst);5856 const result_ty = cg.typeOfIndex(inst);
5871 const struct_ptr_ty_info = struct_ptr_ty.ptrInfo(zcu);5857 const struct_ptr_ty_info = struct_ptr_ty.ptrInfo(zcu);
58725858
...@@ -5888,8 +5874,7 @@ fn structFieldPtr(...@@ -5888,8 +5874,7 @@ fn structFieldPtr(
5888}5874}
58895875
5890fn airAggFieldVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {5876fn airAggFieldVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5891 const pt = cg.pt;5877 const zcu = cg.zcu;
5892 const zcu = pt.zcu;
5893 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;5878 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;
5894 const struct_field = cg.air.extraData(Air.StructField, ty_pl.payload).data;5879 const struct_field = cg.air.extraData(Air.StructField, ty_pl.payload).data;
58955880
...@@ -5917,8 +5902,7 @@ fn airAggFieldVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -5917,8 +5902,7 @@ fn airAggFieldVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
59175902
5918// Both `.switch_br` and `.loop_switch_br` are noreturn.5903// Both `.switch_br` and `.loop_switch_br` are noreturn.
5919fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index, is_dispatch_loop: bool) InnerError!void {5904fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index, is_dispatch_loop: bool) InnerError!void {
5920 const pt = cg.pt;5905 const zcu = cg.zcu;
5921 const zcu = pt.zcu;
59225906
5923 const switch_br = cg.air.unwrapSwitch(inst);5907 const switch_br = cg.air.unwrapSwitch(inst);
5924 const target_ty = cg.typeOf(switch_br.operand);5908 const target_ty = cg.typeOf(switch_br.operand);
...@@ -6146,7 +6130,7 @@ fn airSwitchDispatch(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -6146,7 +6130,7 @@ fn airSwitchDispatch(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6146}6130}
61476131
6148fn airIsErr(cg: *CodeGen, inst: Air.Inst.Index, opcode: std.wasm.Opcode, op_kind: enum { value, ptr }) InnerError!void {6132fn airIsErr(cg: *CodeGen, inst: Air.Inst.Index, opcode: std.wasm.Opcode, op_kind: enum { value, ptr }) InnerError!void {
6149 const zcu = cg.pt.zcu;6133 const zcu = cg.zcu;
6150 const un_op = cg.air.instructions.items(.data)[@backingInt(inst)].un_op;6134 const un_op = cg.air.instructions.items(.data)[@backingInt(inst)].un_op;
6151 const operand = try cg.resolveInst(un_op);6135 const operand = try cg.resolveInst(un_op);
6152 const err_union_ty = switch (op_kind) {6136 const err_union_ty = switch (op_kind) {
...@@ -6185,7 +6169,7 @@ fn airIsErr(cg: *CodeGen, inst: Air.Inst.Index, opcode: std.wasm.Opcode, op_kind...@@ -6185,7 +6169,7 @@ fn airIsErr(cg: *CodeGen, inst: Air.Inst.Index, opcode: std.wasm.Opcode, op_kind
6185/// E!T -> T op_is_ptr == false6169/// E!T -> T op_is_ptr == false
6186/// *(E!T) -> *T op_is_prt == true6170/// *(E!T) -> *T op_is_prt == true
6187fn airUnwrapErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void {6171fn airUnwrapErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void {
6188 const zcu = cg.pt.zcu;6172 const zcu = cg.zcu;
6189 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;6173 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
61906174
6191 const operand = try cg.resolveInst(ty_op.operand);6175 const operand = try cg.resolveInst(ty_op.operand);
...@@ -6217,7 +6201,7 @@ fn airUnwrapErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool)...@@ -6217,7 +6201,7 @@ fn airUnwrapErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool)
6217/// *(E!T) -> E op_is_ptr == true6201/// *(E!T) -> E op_is_ptr == true
6218/// NOTE: op_is_ptr will not change return type6202/// NOTE: op_is_ptr will not change return type
6219fn airUnwrapErrUnionError(cg: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void {6203fn airUnwrapErrUnionError(cg: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void {
6220 const zcu = cg.pt.zcu;6204 const zcu = cg.zcu;
6221 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;6205 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
62226206
6223 const operand = try cg.resolveInst(ty_op.operand);6207 const operand = try cg.resolveInst(ty_op.operand);
...@@ -6242,7 +6226,7 @@ fn airUnwrapErrUnionError(cg: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) I...@@ -6242,7 +6226,7 @@ fn airUnwrapErrUnionError(cg: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) I
6242}6226}
62436227
6244fn airWrapErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6228fn airWrapErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6245 const zcu = cg.pt.zcu;6229 const zcu = cg.zcu;
6246 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;6230 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
62476231
6248 const operand = try cg.resolveInst(ty_op.operand);6232 const operand = try cg.resolveInst(ty_op.operand);
...@@ -6271,7 +6255,7 @@ fn airWrapErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -6271,7 +6255,7 @@ fn airWrapErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6271}6255}
62726256
6273fn airWrapErrUnionErr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6257fn airWrapErrUnionErr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6274 const zcu = cg.pt.zcu;6258 const zcu = cg.zcu;
6275 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;6259 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
62766260
6277 const operand = try cg.resolveInst(ty_op.operand);6261 const operand = try cg.resolveInst(ty_op.operand);
...@@ -6311,8 +6295,7 @@ fn airIsNull(cg: *CodeGen, inst: Air.Inst.Index, opcode: std.wasm.Opcode, op_kin...@@ -6311,8 +6295,7 @@ fn airIsNull(cg: *CodeGen, inst: Air.Inst.Index, opcode: std.wasm.Opcode, op_kin
6311/// For a given type and operand, checks if it's considered `null`.6295/// For a given type and operand, checks if it's considered `null`.
6312/// NOTE: Leaves the result on the stack6296/// NOTE: Leaves the result on the stack
6313fn isNull(cg: *CodeGen, operand: WValue, op_ty: Type, opcode: std.wasm.Opcode, op_kind: OpKind) InnerError!WValue {6297fn isNull(cg: *CodeGen, operand: WValue, op_ty: Type, opcode: std.wasm.Opcode, op_kind: OpKind) InnerError!WValue {
6314 const pt = cg.pt;6298 const zcu = cg.zcu;
6315 const zcu = pt.zcu;
6316 const optional_ty = switch (op_kind) {6299 const optional_ty = switch (op_kind) {
6317 .value => op_ty,6300 .value => op_ty,
6318 .ptr => op_ty.childType(zcu),6301 .ptr => op_ty.childType(zcu),
...@@ -6357,7 +6340,7 @@ fn isNull(cg: *CodeGen, operand: WValue, op_ty: Type, opcode: std.wasm.Opcode, o...@@ -6357,7 +6340,7 @@ fn isNull(cg: *CodeGen, operand: WValue, op_ty: Type, opcode: std.wasm.Opcode, o
6357}6340}
63586341
6359fn airOptionalPayload(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6342fn airOptionalPayload(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6360 const zcu = cg.pt.zcu;6343 const zcu = cg.zcu;
6361 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;6344 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
6362 const opt_ty = cg.typeOf(ty_op.operand);6345 const opt_ty = cg.typeOf(ty_op.operand);
6363 const payload_ty = cg.typeOfIndex(inst);6346 const payload_ty = cg.typeOfIndex(inst);
...@@ -6379,7 +6362,7 @@ fn airOptionalPayload(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -6379,7 +6362,7 @@ fn airOptionalPayload(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6379}6362}
63806363
6381fn airOptionalPayloadPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6364fn airOptionalPayloadPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6382 const zcu = cg.pt.zcu;6365 const zcu = cg.zcu;
6383 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;6366 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
6384 const operand = try cg.resolveInst(ty_op.operand);6367 const operand = try cg.resolveInst(ty_op.operand);
6385 const opt_ty = cg.typeOf(ty_op.operand).childType(zcu);6368 const opt_ty = cg.typeOf(ty_op.operand).childType(zcu);
...@@ -6396,8 +6379,7 @@ fn airOptionalPayloadPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -6396,8 +6379,7 @@ fn airOptionalPayloadPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6396}6379}
63976380
6398fn airOptionalPayloadPtrSet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6381fn airOptionalPayloadPtrSet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6399 const pt = cg.pt;6382 const zcu = cg.zcu;
6400 const zcu = pt.zcu;
6401 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;6383 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
6402 const operand = try cg.resolveInst(ty_op.operand);6384 const operand = try cg.resolveInst(ty_op.operand);
6403 const opt_ty = cg.typeOf(ty_op.operand).childType(zcu);6385 const opt_ty = cg.typeOf(ty_op.operand).childType(zcu);
...@@ -6421,8 +6403,7 @@ fn airOptionalPayloadPtrSet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void...@@ -6421,8 +6403,7 @@ fn airOptionalPayloadPtrSet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void
6421fn airWrapOptional(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6403fn airWrapOptional(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6422 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;6404 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
6423 const payload_ty = cg.typeOf(ty_op.operand);6405 const payload_ty = cg.typeOf(ty_op.operand);
6424 const pt = cg.pt;6406 const zcu = cg.zcu;
6425 const zcu = pt.zcu;
64266407
6427 const result = result: {6408 const result = result: {
6428 if (!payload_ty.hasRuntimeBits(zcu)) {6409 if (!payload_ty.hasRuntimeBits(zcu)) {
...@@ -6478,7 +6459,7 @@ fn airSliceLen(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -6478,7 +6459,7 @@ fn airSliceLen(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6478}6459}
64796460
6480fn airSliceElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6461fn airSliceElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6481 const zcu = cg.pt.zcu;6462 const zcu = cg.zcu;
6482 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;6463 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;
64836464
6484 const slice_ty = cg.typeOf(bin_op.lhs);6465 const slice_ty = cg.typeOf(bin_op.lhs);
...@@ -6499,7 +6480,7 @@ fn airSliceElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -6499,7 +6480,7 @@ fn airSliceElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6499}6480}
65006481
6501fn airSliceElemPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6482fn airSliceElemPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6502 const zcu = cg.pt.zcu;6483 const zcu = cg.zcu;
6503 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;6484 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;
6504 const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data;6485 const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data;
65056486
...@@ -6534,7 +6515,7 @@ fn sliceLen(cg: *CodeGen, operand: WValue) InnerError!WValue {...@@ -6534,7 +6515,7 @@ fn sliceLen(cg: *CodeGen, operand: WValue) InnerError!WValue {
6534}6515}
65356516
6536fn airArrayToSlice(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6517fn airArrayToSlice(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6537 const zcu = cg.pt.zcu;6518 const zcu = cg.zcu;
6538 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;6519 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
65396520
6540 const operand = try cg.resolveInst(ty_op.operand);6521 const operand = try cg.resolveInst(ty_op.operand);
...@@ -6554,7 +6535,7 @@ fn airArrayToSlice(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -6554,7 +6535,7 @@ fn airArrayToSlice(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6554}6535}
65556536
6556fn airPtrElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6537fn airPtrElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6557 const zcu = cg.pt.zcu;6538 const zcu = cg.zcu;
6558 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;6539 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;
65596540
6560 const ptr_ty = cg.typeOf(bin_op.lhs);6541 const ptr_ty = cg.typeOf(bin_op.lhs);
...@@ -6579,7 +6560,7 @@ fn airPtrElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -6579,7 +6560,7 @@ fn airPtrElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6579}6560}
65806561
6581fn airPtrElemPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6562fn airPtrElemPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6582 const zcu = cg.pt.zcu;6563 const zcu = cg.zcu;
6583 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;6564 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;
6584 const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data;6565 const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data;
65856566
...@@ -6604,7 +6585,7 @@ fn airPtrElemPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -6604,7 +6585,7 @@ fn airPtrElemPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6604}6585}
66056586
6606fn airPtrBinOp(cg: *CodeGen, inst: Air.Inst.Index, op: PointerOffsetOp) InnerError!void {6587fn airPtrBinOp(cg: *CodeGen, inst: Air.Inst.Index, op: PointerOffsetOp) InnerError!void {
6607 const zcu = cg.pt.zcu;6588 const zcu = cg.zcu;
6608 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;6589 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;
6609 const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data;6590 const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data;
66106591
...@@ -6623,7 +6604,7 @@ fn airPtrBinOp(cg: *CodeGen, inst: Air.Inst.Index, op: PointerOffsetOp) InnerErr...@@ -6623,7 +6604,7 @@ fn airPtrBinOp(cg: *CodeGen, inst: Air.Inst.Index, op: PointerOffsetOp) InnerErr
6623}6604}
66246605
6625fn airMemset(cg: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void {6606fn airMemset(cg: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void {
6626 const zcu = cg.pt.zcu;6607 const zcu = cg.zcu;
6627 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;6608 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;
66286609
6629 const ptr = try cg.resolveInst(bin_op.lhs);6610 const ptr = try cg.resolveInst(bin_op.lhs);
...@@ -6657,7 +6638,7 @@ fn airMemset(cg: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void {...@@ -6657,7 +6638,7 @@ fn airMemset(cg: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void {
6657/// this to wasm's memset instruction. When the feature is not present,6638/// this to wasm's memset instruction. When the feature is not present,
6658/// we implement it manually.6639/// we implement it manually.
6659fn memset(cg: *CodeGen, elem_ty: Type, ptr: WValue, len: WValue, value: WValue) InnerError!void {6640fn memset(cg: *CodeGen, elem_ty: Type, ptr: WValue, len: WValue, value: WValue) InnerError!void {
6660 const zcu = cg.pt.zcu;6641 const zcu = cg.zcu;
6661 const abi_size = @as(u32, @intCast(elem_ty.abiSize(zcu)));6642 const abi_size = @as(u32, @intCast(elem_ty.abiSize(zcu)));
6662 const len_known_neq_0 = switch (len) {6643 const len_known_neq_0 = switch (len) {
6663 .imm32 => |imm| if (imm != 0) true else return,6644 .imm32 => |imm| if (imm != 0) true else return,
...@@ -6774,7 +6755,7 @@ fn memset(cg: *CodeGen, elem_ty: Type, ptr: WValue, len: WValue, value: WValue)...@@ -6774,7 +6755,7 @@ fn memset(cg: *CodeGen, elem_ty: Type, ptr: WValue, len: WValue, value: WValue)
6774}6755}
67756756
6776fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6757fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6777 const zcu = cg.pt.zcu;6758 const zcu = cg.zcu;
6778 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;6759 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;
67796760
6780 const array_ty = cg.typeOf(bin_op.lhs);6761 const array_ty = cg.typeOf(bin_op.lhs);
...@@ -6828,7 +6809,7 @@ fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -6828,7 +6809,7 @@ fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6828}6809}
68296810
6830fn airSplat(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6811fn airSplat(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6831 const zcu = cg.pt.zcu;6812 const zcu = cg.zcu;
6832 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;6813 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
6833 const operand = try cg.resolveInst(ty_op.operand);6814 const operand = try cg.resolveInst(ty_op.operand);
6834 const ty = cg.typeOfIndex(inst);6815 const ty = cg.typeOfIndex(inst);
...@@ -6899,8 +6880,7 @@ fn airSelect(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -6899,8 +6880,7 @@ fn airSelect(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6899}6880}
69006881
6901fn airShuffleOne(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6882fn airShuffleOne(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6902 const pt = cg.pt;6883 const zcu = cg.zcu;
6903 const zcu = pt.zcu;
69046884
6905 const unwrapped = cg.air.unwrapShuffleOne(zcu, inst);6885 const unwrapped = cg.air.unwrapShuffleOne(zcu, inst);
6906 const result_ty = unwrapped.result_ty;6886 const result_ty = unwrapped.result_ty;
...@@ -6931,8 +6911,7 @@ fn airShuffleOne(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -6931,8 +6911,7 @@ fn airShuffleOne(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6931}6911}
69326912
6933fn airShuffleTwo(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6913fn airShuffleTwo(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6934 const pt = cg.pt;6914 const zcu = cg.zcu;
6935 const zcu = pt.zcu;
69366915
6937 const unwrapped = cg.air.unwrapShuffleTwo(zcu, inst);6916 const unwrapped = cg.air.unwrapShuffleTwo(zcu, inst);
6938 const result_ty = unwrapped.result_ty;6917 const result_ty = unwrapped.result_ty;
...@@ -7008,8 +6987,7 @@ fn airReduce(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -7008,8 +6987,7 @@ fn airReduce(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7008}6987}
70096988
7010fn airAggregateInit(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {6989fn airAggregateInit(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7011 const pt = cg.pt;6990 const zcu = cg.zcu;
7012 const zcu = pt.zcu;
7013 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;6991 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;
7014 const result_ty = cg.typeOfIndex(inst);6992 const result_ty = cg.typeOfIndex(inst);
7015 const len = @as(usize, @intCast(result_ty.arrayLen(zcu)));6993 const len = @as(usize, @intCast(result_ty.arrayLen(zcu)));
...@@ -7067,9 +7045,10 @@ fn airAggregateInit(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -7067,9 +7045,10 @@ fn airAggregateInit(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7067 else => {7045 else => {
7068 const result = try cg.allocStack(result_ty);7046 const result = try cg.allocStack(result_ty);
7069 for (elements, 0..) |elem, elem_index| {7047 for (elements, 0..) |elem, elem_index| {
7070 if (try result_ty.structFieldValueComptime(pt, elem_index) != null) continue;7048 if (result_ty.structFieldIsComptime(elem_index, zcu)) continue;
70717049
7072 const elem_ty = result_ty.fieldType(elem_index, zcu);7050 const elem_ty = result_ty.fieldType(elem_index, zcu);
7051 if (!elem_ty.hasRuntimeBits(zcu)) continue;
7073 const field_offset = result_ty.structFieldOffset(elem_index, zcu);7052 const field_offset = result_ty.structFieldOffset(elem_index, zcu);
70747053
7075 const value = try cg.resolveInst(elem);7054 const value = try cg.resolveInst(elem);
...@@ -7089,8 +7068,7 @@ fn airAggregateInit(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -7089,8 +7068,7 @@ fn airAggregateInit(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7089}7068}
70907069
7091fn airUnionInit(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {7070fn airUnionInit(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7092 const pt = cg.pt;7071 const zcu = cg.zcu;
7093 const zcu = pt.zcu;
7094 const ip = &zcu.intern_pool;7072 const ip = &zcu.intern_pool;
7095 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;7073 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;
7096 const extra = cg.air.extraData(Air.UnionInit, ty_pl.payload).data;7074 const extra = cg.air.extraData(Air.UnionInit, ty_pl.payload).data;
...@@ -7102,11 +7080,27 @@ fn airUnionInit(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -7102,11 +7080,27 @@ fn airUnionInit(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7102 const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[extra.field_index]);7080 const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[extra.field_index]);
7103 const field_name = ip.loadEnumType(union_obj.enum_tag_type).field_names.get(ip)[extra.field_index];7081 const field_name = ip.loadEnumType(union_obj.enum_tag_type).field_names.get(ip)[extra.field_index];
71047082
7105 const tag_int = blk: {7083 const tag_int: WValue = blk: {
7106 const tag_ty = union_ty.unionTagTypeHypothetical(zcu);7084 const tag_ty = union_ty.unionTagTypeHypothetical(zcu);
7107 const enum_field_index = tag_ty.enumFieldIndex(field_name, zcu).?;7085 const enum_field_index = tag_ty.enumFieldIndex(field_name, zcu).?;
7108 const tag_val = try pt.enumValueFieldIndex(tag_ty, enum_field_index);7086 const enum_type = ip.loadEnumType(tag_ty.toIntern());
7109 break :blk try cg.lowerConstant(tag_val);7087 const int_ty = Type.fromInterned(enum_type.int_tag_type);
7088 const int_bits = int_ty.intInfo(zcu).bits;
7089 if (int_bits > 64) {
7090 return cg.fail("TODO: union tags > 64 bits", .{});
7091 }
7092
7093 if (enum_type.field_values.len == 0) {
7094 // Auto-numbered fields.
7095 break :blk switch (int_bits) {
7096 0...32 => .{ .imm32 = enum_field_index },
7097 33...64 => .{ .imm64 = enum_field_index },
7098 else => unreachable,
7099 };
7100 }
7101
7102 const int_value = enum_type.field_values.get(ip)[enum_field_index];
7103 break :blk try cg.lowerConstant(.fromInterned(int_value));
7110 };7104 };
7111 if (layout.payload_size == 0) {7105 if (layout.payload_size == 0) {
7112 if (layout.tag_size == 0) {7106 if (layout.tag_size == 0) {
...@@ -7167,8 +7161,7 @@ fn airWasmMemoryGrow(cg: *CodeGen, inst: Air.Inst.Index) !void {...@@ -7167,8 +7161,7 @@ fn airWasmMemoryGrow(cg: *CodeGen, inst: Air.Inst.Index) !void {
7167}7161}
71687162
7169fn airSetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {7163fn airSetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7170 const pt = cg.pt;7164 const zcu = cg.zcu;
7171 const zcu = pt.zcu;
7172 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;7165 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;
7173 const un_ty = cg.typeOf(bin_op.lhs).childType(zcu);7166 const un_ty = cg.typeOf(bin_op.lhs).childType(zcu);
7174 const tag_ty = cg.typeOf(bin_op.rhs);7167 const tag_ty = cg.typeOf(bin_op.rhs);
...@@ -7192,7 +7185,7 @@ fn airSetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -7192,7 +7185,7 @@ fn airSetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7192}7185}
71937186
7194fn airGetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {7187fn airGetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7195 const zcu = cg.pt.zcu;7188 const zcu = cg.zcu;
7196 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;7189 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
71977190
7198 const un_ty = cg.typeOf(ty_op.operand);7191 const un_ty = cg.typeOf(ty_op.operand);
...@@ -7212,7 +7205,7 @@ fn airGetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -7212,7 +7205,7 @@ fn airGetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7212}7205}
72137206
7214fn airErrUnionPayloadPtrSet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {7207fn airErrUnionPayloadPtrSet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7215 const zcu = cg.pt.zcu;7208 const zcu = cg.zcu;
7216 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;7209 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
72177210
7218 const err_set_ty = cg.typeOf(ty_op.operand).childType(zcu);7211 const err_set_ty = cg.typeOf(ty_op.operand).childType(zcu);
...@@ -7236,8 +7229,7 @@ fn airErrUnionPayloadPtrSet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void...@@ -7236,8 +7229,7 @@ fn airErrUnionPayloadPtrSet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void
7236}7229}
72377230
7238fn airFieldParentPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {7231fn airFieldParentPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7239 const pt = cg.pt;7232 const zcu = cg.zcu;
7240 const zcu = pt.zcu;
7241 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;7233 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;
7242 const extra = cg.air.extraData(Air.FieldParentPtr, ty_pl.payload).data;7234 const extra = cg.air.extraData(Air.FieldParentPtr, ty_pl.payload).data;
72437235
...@@ -7276,7 +7268,7 @@ fn airFieldParentPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -7276,7 +7268,7 @@ fn airFieldParentPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7276}7268}
72777269
7278fn sliceOrArrayPtr(cg: *CodeGen, ptr: WValue, ptr_ty: Type) InnerError!WValue {7270fn sliceOrArrayPtr(cg: *CodeGen, ptr: WValue, ptr_ty: Type) InnerError!WValue {
7279 const zcu = cg.pt.zcu;7271 const zcu = cg.zcu;
7280 if (ptr_ty.isSlice(zcu)) {7272 if (ptr_ty.isSlice(zcu)) {
7281 return cg.slicePtr(ptr);7273 return cg.slicePtr(ptr);
7282 } else {7274 } else {
...@@ -7285,7 +7277,7 @@ fn sliceOrArrayPtr(cg: *CodeGen, ptr: WValue, ptr_ty: Type) InnerError!WValue {...@@ -7285,7 +7277,7 @@ fn sliceOrArrayPtr(cg: *CodeGen, ptr: WValue, ptr_ty: Type) InnerError!WValue {
7285}7277}
72867278
7287fn airMemcpy(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {7279fn airMemcpy(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7288 const zcu = cg.pt.zcu;7280 const zcu = cg.zcu;
7289 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;7281 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;
7290 const dst = try cg.resolveInst(bin_op.lhs);7282 const dst = try cg.resolveInst(bin_op.lhs);
7291 const dst_ty = cg.typeOf(bin_op.lhs);7283 const dst_ty = cg.typeOf(bin_op.lhs);
...@@ -7319,7 +7311,7 @@ fn airMemcpy(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -7319,7 +7311,7 @@ fn airMemcpy(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7319}7311}
73207312
7321fn airMemmove(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {7313fn airMemmove(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7322 const zcu = cg.pt.zcu;7314 const zcu = cg.zcu;
7323 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;7315 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;
7324 const dst = try cg.resolveInst(bin_op.lhs);7316 const dst = try cg.resolveInst(bin_op.lhs);
7325 const dst_ty = cg.typeOf(bin_op.lhs);7317 const dst_ty = cg.typeOf(bin_op.lhs);
...@@ -7370,9 +7362,9 @@ fn airErrorName(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -7370,9 +7362,9 @@ fn airErrorName(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7370 //7362 //
7371 // As the names are global and the slice elements are constant, we do not have7363 // As the names are global and the slice elements are constant, we do not have
7372 // to make a copy of the ptr+value but can point towards them directly.7364 // to make a copy of the ptr+value but can point towards them directly.
7373 const pt = cg.pt;7365 const zcu = cg.zcu;
7374 const name_ty = Type.slice_const_u8_sentinel_0;7366 const name_ty = Type.slice_const_u8_sentinel_0;
7375 const abi_size = name_ty.abiSize(pt.zcu);7367 const abi_size = name_ty.abiSize(zcu);
73767368
7377 // Lowers to a i32.const or i64.const with the error table memory address.7369 // Lowers to a i32.const or i64.const with the error table memory address.
7378 cg.error_name_table_ref_count += 1;7370 cg.error_name_table_ref_count += 1;
...@@ -7433,7 +7425,7 @@ fn airTry(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -7433,7 +7425,7 @@ fn airTry(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7433}7425}
74347426
7435fn airTryPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {7427fn airTryPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7436 const zcu = cg.pt.zcu;7428 const zcu = cg.zcu;
7437 const unwrapped_try = cg.air.unwrapTryPtr(inst);7429 const unwrapped_try = cg.air.unwrapTryPtr(inst);
7438 const err_union_ptr = try cg.resolveInst(unwrapped_try.error_union_ptr);7430 const err_union_ptr = try cg.resolveInst(unwrapped_try.error_union_ptr);
7439 const body = unwrapped_try.else_body;7431 const body = unwrapped_try.else_body;
...@@ -7456,7 +7448,7 @@ fn lowerTry(...@@ -7456,7 +7448,7 @@ fn lowerTry(
7456 err_union_ty: Type,7448 err_union_ty: Type,
7457 operand_is_ptr: bool,7449 operand_is_ptr: bool,
7458) InnerError!WValue {7450) InnerError!WValue {
7459 const zcu = cg.pt.zcu;7451 const zcu = cg.zcu;
7460 const branch_liveness = cg.liveness.getCondBr(inst);7452 const branch_liveness = cg.liveness.getCondBr(inst);
74617453
7462 const pl_ty = err_union_ty.errorUnionPayload(zcu);7454 const pl_ty = err_union_ty.errorUnionPayload(zcu);
...@@ -7516,7 +7508,7 @@ fn callIntrinsic(...@@ -7516,7 +7508,7 @@ fn callIntrinsic(
7516 args: []const WValue,7508 args: []const WValue,
7517) InnerError!WValue {7509) InnerError!WValue {
7518 assert(param_types.len == args.len);7510 assert(param_types.len == args.len);
7519 const zcu = cg.pt.zcu;7511 const zcu = cg.zcu;
75207512
7521 // Always pass over C-ABI7513 // Always pass over C-ABI
75227514
...@@ -7574,7 +7566,7 @@ fn airIsNamedEnumValue(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -7574,7 +7566,7 @@ fn airIsNamedEnumValue(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7574}7566}
75757567
7576fn airErrorSetHasValue(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {7568fn airErrorSetHasValue(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7577 const zcu = cg.pt.zcu;7569 const zcu = cg.zcu;
7578 const ip = &zcu.intern_pool;7570 const ip = &zcu.intern_pool;
7579 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;7571 const ty_op = cg.air.instructions.items(.data)[@backingInt(inst)].ty_op;
75807572
...@@ -7661,7 +7653,7 @@ inline fn useAtomicFeature(cg: *const CodeGen) bool {...@@ -7661,7 +7653,7 @@ inline fn useAtomicFeature(cg: *const CodeGen) bool {
7661}7653}
76627654
7663fn airCmpxchg(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {7655fn airCmpxchg(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7664 const zcu = cg.pt.zcu;7656 const zcu = cg.zcu;
7665 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;7657 const ty_pl = cg.air.instructions.items(.data)[@backingInt(inst)].ty_pl;
7666 const extra = cg.air.extraData(Air.Cmpxchg, ty_pl.payload).data;7658 const extra = cg.air.extraData(Air.Cmpxchg, ty_pl.payload).data;
76677659
...@@ -7739,7 +7731,7 @@ fn airCmpxchg(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -7739,7 +7731,7 @@ fn airCmpxchg(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7739}7731}
77407732
7741fn airAtomicLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {7733fn airAtomicLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7742 const zcu = cg.pt.zcu;7734 const zcu = cg.zcu;
7743 const atomic_load = cg.air.instructions.items(.data)[@backingInt(inst)].atomic_load;7735 const atomic_load = cg.air.instructions.items(.data)[@backingInt(inst)].atomic_load;
7744 const ptr = try cg.resolveInst(atomic_load.ptr);7736 const ptr = try cg.resolveInst(atomic_load.ptr);
7745 const ty = cg.typeOfIndex(inst);7737 const ty = cg.typeOfIndex(inst);
...@@ -7765,7 +7757,7 @@ fn airAtomicLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -7765,7 +7757,7 @@ fn airAtomicLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7765}7757}
77667758
7767fn airAtomicRmw(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {7759fn airAtomicRmw(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7768 const zcu = cg.pt.zcu;7760 const zcu = cg.zcu;
7769 const pl_op = cg.air.instructions.items(.data)[@backingInt(inst)].pl_op;7761 const pl_op = cg.air.instructions.items(.data)[@backingInt(inst)].pl_op;
7770 const extra = cg.air.extraData(Air.AtomicRmw, pl_op.payload).data;7762 const extra = cg.air.extraData(Air.AtomicRmw, pl_op.payload).data;
77717763
...@@ -7972,7 +7964,7 @@ fn airAtomicRmw(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -7972,7 +7964,7 @@ fn airAtomicRmw(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7972}7964}
79737965
7974fn airAtomicStore(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {7966fn airAtomicStore(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7975 const zcu = cg.pt.zcu;7967 const zcu = cg.zcu;
7976 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;7968 const bin_op = cg.air.instructions.items(.data)[@backingInt(inst)].bin_op;
79777969
7978 const ptr = try cg.resolveInst(bin_op.lhs);7970 const ptr = try cg.resolveInst(bin_op.lhs);
...@@ -8008,7 +8000,7 @@ fn airFrameAddress(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -8008,7 +8000,7 @@ fn airFrameAddress(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
80088000
8009fn airRuntimeNavPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {8001fn airRuntimeNavPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
8010 const ty_nav = cg.air.instructions.items(.data)[@backingInt(inst)].ty_nav;8002 const ty_nav = cg.air.instructions.items(.data)[@backingInt(inst)].ty_nav;
8011 const mod = cg.pt.zcu.navFileScope(cg.owner_nav).mod.?;8003 const mod = cg.zcu.navFileScope(cg.owner_nav).mod.?;
8012 if (mod.single_threaded) {8004 if (mod.single_threaded) {
8013 const result: WValue = .{ .nav_ref = .{8005 const result: WValue = .{ .nav_ref = .{
8014 .nav_index = ty_nav.nav,8006 .nav_index = ty_nav.nav,
...@@ -8024,7 +8016,7 @@ fn airAsm(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -8024,7 +8016,7 @@ fn airAsm(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
8024 const outputs = unwrapped_asm.outputs;8016 const outputs = unwrapped_asm.outputs;
8025 const inputs = unwrapped_asm.inputs;8017 const inputs = unwrapped_asm.inputs;
80268018
8027 const zcu = cg.pt.zcu;8019 const zcu = cg.zcu;
8028 const output_ty = cg.typeOfIndex(inst);8020 const output_ty = cg.typeOfIndex(inst);
80298021
8030 const result: WValue = if (output_ty.hasRuntimeBits(zcu))8022 const result: WValue = if (output_ty.hasRuntimeBits(zcu))
...@@ -8091,11 +8083,11 @@ fn airAsm(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -8091,11 +8083,11 @@ fn airAsm(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
8091}8083}
80928084
8093fn typeOf(cg: *CodeGen, inst: Air.Inst.Ref) Type {8085fn typeOf(cg: *CodeGen, inst: Air.Inst.Ref) Type {
8094 const zcu = cg.pt.zcu;8086 const zcu = cg.zcu;
8095 return cg.air.typeOf(inst, &zcu.intern_pool);8087 return cg.air.typeOf(inst, &zcu.intern_pool);
8096}8088}
80978089
8098fn typeOfIndex(cg: *CodeGen, inst: Air.Inst.Index) Type {8090fn typeOfIndex(cg: *CodeGen, inst: Air.Inst.Index) Type {
8099 const zcu = cg.pt.zcu;8091 const zcu = cg.zcu;
8100 return cg.air.typeOfIndex(inst, &zcu.intern_pool);8092 return cg.air.typeOfIndex(inst, &zcu.intern_pool);
8101}8093}