authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-08-22 16:36:47+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-10-16 15:54:16+02:00
logb9b20b14ea5886aa862927daa7164073aab56132
treee2b60819d8beb2e3fc7c6dcf6f7d65d1ff37b093
parent99c3578f697fe0b0151049f74208b86244b4d171
signaturelock-open Commit is signed but in an unrecognized format.

wasm: use liveness analysis for locals

This hooks reusal of locals into liveness analysis. Meaning that when an operand dies, and is a local, it will automatically be freed so it can be re-used when a new local is required. The result of this, is a lower allocation required for locals. Having less locals means smaller binary size, as well as faster compilation speed when loaded by the runtime.

1 files changed, 814 insertions(+), 626 deletions(-)

src/arch/wasm/CodeGen.zig+814-626
......@@ -1,4 +1,5 @@
11const std = @import("std");
2const builtin = @import("builtin");
23const Allocator = std.mem.Allocator;
34const ArrayList = std.ArrayList;
45const assert = std.debug.assert;
......@@ -91,11 +92,14 @@ const WValue = union(enum) {
9192
9293 /// Marks a local as no longer being referenced and essentially allows
9394 /// us to re-use it somewhere else within the function.
94 /// The valtype of the local is deducted by using the index of the given.
95 /// The valtype of the local is deducted by using the index of the given `WValue`.
9596 fn free(value: *WValue, gen: *Self) void {
9697 if (value.* != .local) return;
9798 const local_value = value.local;
98 const index = local_value - gen.args.len - @boolToInt(gen.return_value != .none);
99 const reserved = gen.args.len + @boolToInt(gen.return_value != .none) + 2; // 2 for stack locals
100 if (local_value < reserved) return; // reserved locals may never be re-used.
101
102 const index = local_value - reserved;
99103 const valtype = @intToEnum(wasm.Valtype, gen.locals.items[index]);
100104 switch (valtype) {
101105 .i32 => gen.free_locals_i32.append(gen.gpa, local_value) catch return, // It's ok to fail any of those, a new local can be allocated instead
......@@ -650,6 +654,13 @@ free_locals_f32: std.ArrayListUnmanaged(u32) = .{},
650654/// It is illegal to store a non-i32 valtype in this list.
651655free_locals_f64: std.ArrayListUnmanaged(u32) = .{},
652656
657/// When in debug mode, this tracks if no `finishAir` was missed.
658/// Forgetting to call `finishAir` will cause the result to not be
659/// stored in our `values` map and therefore cause bugs.
660air_bookkeeping: @TypeOf(bookkeeping_init) = bookkeeping_init,
661
662const bookkeeping_init = if (builtin.mode == .Debug) @as(usize, 0) else {};
663
653664const InnerError = error{
654665 OutOfMemory,
655666 /// An error occurred when trying to lower AIR to MIR.
......@@ -711,6 +722,65 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!WValue {
711722 return result;
712723}
713724
725fn finishAir(self: *Self, inst: Air.Inst.Index, result: WValue, operands: []const Air.Inst.Ref) void {
726 assert(operands.len <= Liveness.bpi - 1);
727 var tomb_bits = self.liveness.getTombBits(inst);
728 for (operands) |operand| {
729 const dies = @truncate(u1, tomb_bits) != 0;
730 tomb_bits >>= 1;
731 if (!dies) continue;
732 processDeath(self, operand);
733 }
734
735 // results of `none` can never be referenced.
736 if (result != .none) {
737 assert(result != .stack); // it's illegal to store a stack value as we cannot track its position
738 self.values.putAssumeCapacityNoClobber(Air.indexToRef(inst), result);
739 }
740
741 if (builtin.mode == .Debug) {
742 self.air_bookkeeping += 1;
743 }
744}
745
746const BigTomb = struct {
747 gen: *Self,
748 inst: Air.Inst.Index,
749 lbt: Liveness.BigTomb,
750
751 fn feed(bt: *BigTomb, op_ref: Air.Inst.Ref) void {
752 _ = Air.refToIndex(op_ref) orelse return; // constants do not have to be freed regardless
753 const dies = bt.lbt.feed();
754 if (!dies) return;
755 processDeath(bt.gen, op_ref);
756 }
757
758 fn finishAir(bt: *BigTomb, result: WValue) void {
759 assert(result != .stack);
760 if (result != .none) {
761 bt.gen.values.putAssumeCapacityNoClobber(Air.indexToRef(bt.inst), result);
762 }
763
764 bt.gen.air_bookkeeping += 1;
765 }
766};
767
768fn iterateBigTomb(self: *Self, inst: Air.Inst.Index, operand_count: usize) !BigTomb {
769 try self.values.ensureUnusedCapacity(self.gpa, @intCast(u32, operand_count + 1));
770 return BigTomb{
771 .gen = self,
772 .inst = inst,
773 .lbt = self.liveness.iterateBigTomb(inst),
774 };
775}
776
777fn processDeath(self: *Self, ref: Air.Inst.Ref) void {
778 const inst = Air.refToIndex(ref) orelse return;
779 if (self.air.instructions.items(.tag)[inst] == .constant) return;
780 var value = self.values.get(ref) orelse return;
781 value.free(self);
782}
783
714784/// Appends a MIR instruction and returns its index within the list of instructions
715785fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!void {
716786 try self.mir_instructions.append(self.gpa, inst);
......@@ -1502,7 +1572,7 @@ fn buildPointerOffset(self: *Self, ptr_value: WValue, offset: u64, action: enum
15021572 return result_ptr;
15031573}
15041574
1505fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
1575fn genInst(self: *Self, inst: Air.Inst.Index) InnerError!void {
15061576 const air_tags = self.air.instructions.items(.tag);
15071577 return switch (air_tags[inst]) {
15081578 .constant => unreachable,
......@@ -1581,7 +1651,7 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
15811651 .dbg_inline_end,
15821652 .dbg_block_begin,
15831653 .dbg_block_end,
1584 => WValue.none,
1654 => self.finishAir(inst, .none, &.{}),
15851655
15861656 .dbg_var_ptr => self.airDbgVar(inst, true),
15871657 .dbg_var_val => self.airDbgVar(inst, false),
......@@ -1730,15 +1800,24 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
17301800
17311801fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
17321802 for (body) |inst| {
1733 const result = try self.genInst(inst);
1734 if (result != .none) {
1735 assert(result != .stack); // not allowed to store stack values as we cannot keep track of where they are on the stack
1736 try self.values.putNoClobber(self.gpa, Air.indexToRef(inst), result);
1803 const old_bookkeeping_value = self.air_bookkeeping;
1804 try self.values.ensureUnusedCapacity(self.gpa, Liveness.bpi);
1805 try self.genInst(inst);
1806
1807 if (builtin.mode == .Debug and self.air_bookkeeping < old_bookkeeping_value + 1) {
1808 std.debug.panic("Missing call to `finishAir` in AIR instruction %{d} ('{}')", .{
1809 inst,
1810 self.air.instructions.items(.tag)[inst],
1811 });
17371812 }
1813 // if (result != .none) {
1814 // assert(result != .stack); // not allowed to store stack values as we cannot keep track of where they are on the stack
1815 // try self.values.putNoClobber(self.gpa, Air.indexToRef(inst), result);
1816 // }
17381817 }
17391818}
17401819
1741fn airRet(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
1820fn airRet(self: *Self, inst: Air.Inst.Index) InnerError!void {
17421821 const un_op = self.air.instructions.items(.data)[inst].un_op;
17431822 const operand = try self.resolveInst(un_op);
17441823 const fn_info = self.decl.ty.fnInfo();
......@@ -1776,25 +1855,30 @@ fn airRet(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
17761855 }
17771856 try self.restoreStackPointer();
17781857 try self.addTag(.@"return");
1779 return WValue{ .none = {} };
1858
1859 self.finishAir(inst, .none, &.{un_op});
17801860}
17811861
1782fn airRetPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
1862fn airRetPtr(self: *Self, inst: Air.Inst.Index) InnerError!void {
17831863 const child_type = self.air.typeOfIndex(inst).childType();
17841864
1785 if (!child_type.isFnOrHasRuntimeBitsIgnoreComptime()) {
1786 return self.allocStack(Type.usize); // create pointer to void
1787 }
1865 var result = result: {
1866 if (!child_type.isFnOrHasRuntimeBitsIgnoreComptime()) {
1867 break :result try self.allocStack(Type.usize); // create pointer to void
1868 }
17881869
1789 const fn_info = self.decl.ty.fnInfo();
1790 if (firstParamSRet(fn_info.cc, fn_info.return_type, self.target)) {
1791 return self.return_value;
1792 }
1870 const fn_info = self.decl.ty.fnInfo();
1871 if (firstParamSRet(fn_info.cc, fn_info.return_type, self.target)) {
1872 break :result self.return_value;
1873 }
17931874
1794 return self.allocStackPtr(inst);
1875 break :result try self.allocStackPtr(inst);
1876 };
1877
1878 self.finishAir(inst, result, &.{});
17951879}
17961880
1797fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
1881fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!void {
17981882 const un_op = self.air.instructions.items(.data)[inst].un_op;
17991883 const operand = try self.resolveInst(un_op);
18001884 const ret_ty = self.air.typeOf(un_op).childType();
......@@ -1802,7 +1886,7 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
18021886 if (ret_ty.isError()) {
18031887 try self.addImm32(0);
18041888 } else {
1805 return WValue.none;
1889 return self.finishAir(inst, .none, &.{});
18061890 }
18071891 }
18081892
......@@ -1814,14 +1898,14 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
18141898
18151899 try self.restoreStackPointer();
18161900 try self.addTag(.@"return");
1817 return .none;
1901 return self.finishAir(inst, .none, &.{});
18181902}
18191903
1820fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.Modifier) InnerError!WValue {
1904fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.Modifier) InnerError!void {
18211905 if (modifier == .always_tail) return self.fail("TODO implement tail calls for wasm", .{});
18221906 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
18231907 const extra = self.air.extraData(Air.Call, pl_op.payload);
1824 const args = self.air.extra[extra.end..][0..extra.data.args_len];
1908 const args = @ptrCast([]const Air.Inst.Ref, self.air.extra[extra.end..][0..extra.data.args_len]);
18251909 const ty = self.air.typeOf(pl_op.operand);
18261910
18271911 const fn_ty = switch (ty.zigTypeTag()) {
......@@ -1865,10 +1949,9 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
18651949 } else WValue{ .none = {} };
18661950
18671951 for (args) |arg| {
1868 const arg_ref = @intToEnum(Air.Inst.Ref, arg);
1869 const arg_val = try self.resolveInst(arg_ref);
1952 const arg_val = try self.resolveInst(arg);
18701953
1871 const arg_ty = self.air.typeOf(arg_ref);
1954 const arg_ty = self.air.typeOf(arg);
18721955 if (!arg_ty.hasRuntimeBitsIgnoreComptime()) continue;
18731956
18741957 try self.lowerArg(fn_ty.fnInfo().cc, arg_ty, arg_val);
......@@ -1890,33 +1973,41 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
18901973 try self.addLabel(.call_indirect, fn_type_index);
18911974 }
18921975
1893 if (self.liveness.isUnused(inst) or (!ret_ty.hasRuntimeBitsIgnoreComptime() and !ret_ty.isError())) {
1894 return WValue.none;
1895 } else if (ret_ty.isNoReturn()) {
1896 try self.addTag(.@"unreachable");
1897 return WValue.none;
1898 } else if (first_param_sret) {
1899 return sret;
1900 // TODO: Make this less fragile and optimize
1901 } else if (fn_ty.fnInfo().cc == .C and ret_ty.zigTypeTag() == .Struct or ret_ty.zigTypeTag() == .Union) {
1902 const result_local = try self.allocLocal(ret_ty);
1903 try self.addLabel(.local_set, result_local.local);
1904 const scalar_type = abi.scalarType(ret_ty, self.target);
1905 const result = try self.allocStack(scalar_type);
1906 try self.store(result, result_local, scalar_type, 0);
1907 return result;
1908 } else {
1909 const result_local = try self.allocLocal(ret_ty);
1910 try self.addLabel(.local_set, result_local.local);
1911 return result_local;
1912 }
1976 const result_value = result_value: {
1977 if (self.liveness.isUnused(inst) or (!ret_ty.hasRuntimeBitsIgnoreComptime() and !ret_ty.isError())) {
1978 break :result_value WValue{ .none = {} };
1979 } else if (ret_ty.isNoReturn()) {
1980 try self.addTag(.@"unreachable");
1981 break :result_value WValue{ .none = {} };
1982 } else if (first_param_sret) {
1983 break :result_value sret;
1984 // TODO: Make this less fragile and optimize
1985 } else if (fn_ty.fnInfo().cc == .C and ret_ty.zigTypeTag() == .Struct or ret_ty.zigTypeTag() == .Union) {
1986 const result_local = try self.allocLocal(ret_ty);
1987 try self.addLabel(.local_set, result_local.local);
1988 const scalar_type = abi.scalarType(ret_ty, self.target);
1989 const result = try self.allocStack(scalar_type);
1990 try self.store(result, result_local, scalar_type, 0);
1991 break :result_value result;
1992 } else {
1993 const result_local = try self.allocLocal(ret_ty);
1994 try self.addLabel(.local_set, result_local.local);
1995 break :result_value result_local;
1996 }
1997 };
1998
1999 var bt = try self.iterateBigTomb(inst, 1 + args.len);
2000 bt.feed(pl_op.operand);
2001 for (args) |arg| bt.feed(arg);
2002 return bt.finishAir(result_value);
19132003}
19142004
1915fn airAlloc(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
1916 return self.allocStackPtr(inst);
2005fn airAlloc(self: *Self, inst: Air.Inst.Index) InnerError!void {
2006 const value = try self.allocStackPtr(inst);
2007 self.finishAir(inst, value, &.{});
19172008}
19182009
1919fn airStore(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2010fn airStore(self: *Self, inst: Air.Inst.Index) InnerError!void {
19202011 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
19212012
19222013 const lhs = try self.resolveInst(bin_op.lhs);
......@@ -1924,7 +2015,7 @@ fn airStore(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
19242015 const ty = self.air.typeOf(bin_op.lhs).childType();
19252016
19262017 try self.store(lhs, rhs, ty, 0);
1927 return WValue{ .none = {} };
2018 self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
19282019}
19292020
19302021fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerError!void {
......@@ -2007,21 +2098,24 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro
20072098 );
20082099}
20092100
2010fn airLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2101fn airLoad(self: *Self, inst: Air.Inst.Index) InnerError!void {
20112102 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
20122103 const operand = try self.resolveInst(ty_op.operand);
20132104 const ty = self.air.getRefType(ty_op.ty);
20142105
2015 if (!ty.hasRuntimeBitsIgnoreComptime()) return WValue{ .none = {} };
2106 if (!ty.hasRuntimeBitsIgnoreComptime()) return self.finishAir(inst, .none, &.{ty_op.operand});
20162107
2017 if (isByRef(ty, self.target)) {
2018 const new_local = try self.allocStack(ty);
2019 try self.store(new_local, operand, ty, 0);
2020 return new_local;
2021 }
2108 const result = result: {
2109 if (isByRef(ty, self.target)) {
2110 const new_local = try self.allocStack(ty);
2111 try self.store(new_local, operand, ty, 0);
2112 break :result new_local;
2113 }
20222114
2023 const stack_loaded = try self.load(operand, ty, 0);
2024 return stack_loaded.toLocal(self, ty);
2115 const stack_loaded = try self.load(operand, ty, 0);
2116 break :result try stack_loaded.toLocal(self, ty);
2117 };
2118 self.finishAir(inst, result, &.{ty_op.operand});
20252119}
20262120
20272121/// Loads an operand from the linear memory section.
......@@ -2046,7 +2140,7 @@ fn load(self: *Self, operand: WValue, ty: Type, offset: u32) InnerError!WValue {
20462140 return WValue{ .stack = {} };
20472141}
20482142
2049fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2143fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!void {
20502144 const arg_index = self.arg_index;
20512145 const arg = self.args[arg_index];
20522146 const cc = self.decl.ty.fnInfo().cc;
......@@ -2071,7 +2165,7 @@ fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
20712165 const result = try self.allocStack(arg_ty);
20722166 try self.store(result, arg, Type.u64, 0);
20732167 try self.store(result, self.args[arg_index + 1], Type.u64, 8);
2074 return result;
2168 return self.finishAir(inst, arg, &.{});
20752169 }
20762170 } else {
20772171 self.arg_index += 1;
......@@ -2102,19 +2196,19 @@ fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
21022196 },
21032197 else => {},
21042198 }
2105 return arg;
2106}
21072199
2108fn airBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue {
2109 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
2200 self.finishAir(inst, arg, &.{});
2201}
21102202
2203fn airBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void {
21112204 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
2205 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
21122206 const lhs = try self.resolveInst(bin_op.lhs);
21132207 const rhs = try self.resolveInst(bin_op.rhs);
21142208 const ty = self.air.typeOf(bin_op.lhs);
21152209
21162210 const stack_value = try self.binOp(lhs, rhs, ty, op);
2117 return stack_value.toLocal(self, ty);
2211 self.finishAir(inst, try stack_value.toLocal(self, ty), &.{ bin_op.lhs, bin_op.rhs });
21182212}
21192213
21202214/// Performs a binary operation on the given `WValue`'s
......@@ -2195,17 +2289,20 @@ fn binOpBigInt(self: *Self, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerErr
21952289 return result;
21962290}
21972291
2198fn airWrapBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue {
2292fn airWrapBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void {
21992293 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
2294 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
2295
22002296 const lhs = try self.resolveInst(bin_op.lhs);
22012297 const rhs = try self.resolveInst(bin_op.rhs);
2202
22032298 const ty = self.air.typeOf(bin_op.lhs);
2299
22042300 if (ty.zigTypeTag() == .Vector) {
22052301 return self.fail("TODO: Implement wrapping arithmetic for vectors", .{});
22062302 }
22072303
2208 return (try self.wrapBinOp(lhs, rhs, ty, op)).toLocal(self, ty);
2304 const result = try (try self.wrapBinOp(lhs, rhs, ty, op)).toLocal(self, ty);
2305 self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs });
22092306}
22102307
22112308/// Performs a wrapping binary operation.
......@@ -2582,7 +2679,7 @@ fn valueAsI32(self: Self, val: Value, ty: Type) i32 {
25822679 }
25832680}
25842681
2585fn airBlock(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2682fn airBlock(self: *Self, inst: Air.Inst.Index) InnerError!void {
25862683 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
25872684 const block_ty = self.air.getRefType(ty_pl.ty);
25882685 const wasm_block_ty = genBlockType(block_ty, self.target);
......@@ -2592,7 +2689,7 @@ fn airBlock(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
25922689 // if wasm_block_ty is non-empty, we create a register to store the temporary value
25932690 const block_result: WValue = if (wasm_block_ty != wasm.block_empty) blk: {
25942691 const ty: Type = if (isByRef(block_ty, self.target)) Type.u32 else block_ty;
2595 break :blk try self.allocLocal(ty);
2692 break :blk try self.ensureAllocLocal(ty); // make sure it's a clean local as it may never get overwritten
25962693 } else WValue.none;
25972694
25982695 try self.startBlock(.block, wasm.block_empty);
......@@ -2605,7 +2702,7 @@ fn airBlock(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
26052702 try self.genBody(body);
26062703 try self.endBlock();
26072704
2608 return block_result;
2705 self.finishAir(inst, block_result, &.{});
26092706}
26102707
26112708/// appends a new wasm block to the code section and increases the `block_depth` by 1
......@@ -2623,7 +2720,7 @@ fn endBlock(self: *Self) !void {
26232720 self.block_depth -= 1;
26242721}
26252722
2626fn airLoop(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2723fn airLoop(self: *Self, inst: Air.Inst.Index) InnerError!void {
26272724 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
26282725 const loop = self.air.extraData(Air.Block, ty_pl.payload);
26292726 const body = self.air.extra[loop.end..][0..loop.data.body_len];
......@@ -2637,16 +2734,16 @@ fn airLoop(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
26372734 try self.addLabel(.br, 0);
26382735 try self.endBlock();
26392736
2640 return .none;
2737 self.finishAir(inst, .none, &.{});
26412738}
26422739
2643fn airCondBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2740fn airCondBr(self: *Self, inst: Air.Inst.Index) InnerError!void {
26442741 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
26452742 const condition = try self.resolveInst(pl_op.operand);
26462743 const extra = self.air.extraData(Air.CondBr, pl_op.payload);
26472744 const then_body = self.air.extra[extra.end..][0..extra.data.then_body_len];
26482745 const else_body = self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len];
2649 // TODO: Handle death instructions for then and else body
2746 // const liveness_condbr = self.liveness.getCondBr(inst);
26502747
26512748 // result type is always noreturn, so use `block_empty` as type.
26522749 try self.startBlock(.block, wasm.block_empty);
......@@ -2664,15 +2761,18 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
26642761 // Outer block that matches the condition
26652762 try self.genBody(then_body);
26662763
2667 return .none;
2764 self.finishAir(inst, .none, &.{});
26682765}
26692766
2670fn airCmp(self: *Self, inst: Air.Inst.Index, op: std.math.CompareOperator) InnerError!WValue {
2767fn airCmp(self: *Self, inst: Air.Inst.Index, op: std.math.CompareOperator) InnerError!void {
26712768 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
2769 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
2770
26722771 const lhs = try self.resolveInst(bin_op.lhs);
26732772 const rhs = try self.resolveInst(bin_op.rhs);
26742773 const operand_ty = self.air.typeOf(bin_op.lhs);
2675 return (try self.cmp(lhs, rhs, operand_ty, op)).toLocal(self, Type.u32); // comparison result is always 32 bits
2774 const result = try (try self.cmp(lhs, rhs, operand_ty, op)).toLocal(self, Type.u32); // comparison result is always 32 bits
2775 self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs });
26762776}
26772777
26782778/// Compares two operands.
......@@ -2746,14 +2846,12 @@ fn cmpFloat16(self: *Self, lhs: WValue, rhs: WValue, op: std.math.CompareOperato
27462846 return WValue{ .stack = {} };
27472847}
27482848
2749fn airCmpVector(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2849fn airCmpVector(self: *Self, inst: Air.Inst.Index) InnerError!void {
27502850 _ = inst;
27512851 return self.fail("TODO implement airCmpVector for wasm", .{});
27522852}
27532853
2754fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2755 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
2756
2854fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) InnerError!void {
27572855 const un_op = self.air.instructions.items(.data)[inst].un_op;
27582856 const operand = try self.resolveInst(un_op);
27592857
......@@ -2761,7 +2859,7 @@ fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
27612859 return self.fail("TODO implement airCmpLtErrorsLen for wasm", .{});
27622860}
27632861
2764fn airBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2862fn airBr(self: *Self, inst: Air.Inst.Index) InnerError!void {
27652863 const br = self.air.instructions.items(.data)[inst].br;
27662864 const block = self.blocks.get(br.block_inst).?;
27672865
......@@ -2780,76 +2878,82 @@ fn airBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
27802878 const idx: u32 = self.block_depth - block.label;
27812879 try self.addLabel(.br, idx);
27822880
2783 return .none;
2881 self.finishAir(inst, .none, &.{br.operand});
27842882}
27852883
2786fn airNot(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2884fn airNot(self: *Self, inst: Air.Inst.Index) InnerError!void {
27872885 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
2886 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
27882887
27892888 const operand = try self.resolveInst(ty_op.operand);
27902889 const operand_ty = self.air.typeOf(ty_op.operand);
27912890
2792 if (operand_ty.zigTypeTag() == .Bool) {
2793 try self.emitWValue(operand);
2794 try self.addTag(.i32_eqz);
2795 const not_tmp = try self.allocLocal(operand_ty);
2796 try self.addLabel(.local_set, not_tmp.local);
2797 return not_tmp;
2798 } else {
2799 const operand_bits = operand_ty.intInfo(self.target).bits;
2800 const wasm_bits = toWasmBits(operand_bits) orelse {
2801 return self.fail("TODO: Implement binary NOT for integer with bitsize '{d}'", .{operand_bits});
2802 };
2891 const result = result: {
2892 if (operand_ty.zigTypeTag() == .Bool) {
2893 try self.emitWValue(operand);
2894 try self.addTag(.i32_eqz);
2895 const not_tmp = try self.allocLocal(operand_ty);
2896 try self.addLabel(.local_set, not_tmp.local);
2897 break :result not_tmp;
2898 } else {
2899 const operand_bits = operand_ty.intInfo(self.target).bits;
2900 const wasm_bits = toWasmBits(operand_bits) orelse {
2901 return self.fail("TODO: Implement binary NOT for integer with bitsize '{d}'", .{operand_bits});
2902 };
28032903
2804 switch (wasm_bits) {
2805 32 => {
2806 const bin_op = try self.binOp(operand, .{ .imm32 = ~@as(u32, 0) }, operand_ty, .xor);
2807 return (try self.wrapOperand(bin_op, operand_ty)).toLocal(self, operand_ty);
2808 },
2809 64 => {
2810 const bin_op = try self.binOp(operand, .{ .imm64 = ~@as(u64, 0) }, operand_ty, .xor);
2811 return (try self.wrapOperand(bin_op, operand_ty)).toLocal(self, operand_ty);
2812 },
2813 128 => {
2814 const result_ptr = try self.allocStack(operand_ty);
2815 try self.emitWValue(result_ptr);
2816 const msb = try self.load(operand, Type.u64, 0);
2817 const msb_xor = try self.binOp(msb, .{ .imm64 = ~@as(u64, 0) }, Type.u64, .xor);
2818 try self.store(.{ .stack = {} }, msb_xor, Type.u64, 0 + result_ptr.offset());
2819
2820 try self.emitWValue(result_ptr);
2821 const lsb = try self.load(operand, Type.u64, 8);
2822 const lsb_xor = try self.binOp(lsb, .{ .imm64 = ~@as(u64, 0) }, Type.u64, .xor);
2823 try self.store(result_ptr, lsb_xor, Type.u64, 8 + result_ptr.offset());
2824 return result_ptr;
2825 },
2826 else => unreachable,
2904 switch (wasm_bits) {
2905 32 => {
2906 const bin_op = try self.binOp(operand, .{ .imm32 = ~@as(u32, 0) }, operand_ty, .xor);
2907 break :result try (try self.wrapOperand(bin_op, operand_ty)).toLocal(self, operand_ty);
2908 },
2909 64 => {
2910 const bin_op = try self.binOp(operand, .{ .imm64 = ~@as(u64, 0) }, operand_ty, .xor);
2911 break :result try (try self.wrapOperand(bin_op, operand_ty)).toLocal(self, operand_ty);
2912 },
2913 128 => {
2914 const result_ptr = try self.allocStack(operand_ty);
2915 try self.emitWValue(result_ptr);
2916 const msb = try self.load(operand, Type.u64, 0);
2917 const msb_xor = try self.binOp(msb, .{ .imm64 = ~@as(u64, 0) }, Type.u64, .xor);
2918 try self.store(.{ .stack = {} }, msb_xor, Type.u64, 0 + result_ptr.offset());
2919
2920 try self.emitWValue(result_ptr);
2921 const lsb = try self.load(operand, Type.u64, 8);
2922 const lsb_xor = try self.binOp(lsb, .{ .imm64 = ~@as(u64, 0) }, Type.u64, .xor);
2923 try self.store(result_ptr, lsb_xor, Type.u64, 8 + result_ptr.offset());
2924 break :result result_ptr;
2925 },
2926 else => unreachable,
2927 }
28272928 }
2828 }
2929 };
2930 self.finishAir(inst, result, &.{ty_op.operand});
28292931}
28302932
2831fn airBreakpoint(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2832 _ = self;
2833 _ = inst;
2933fn airBreakpoint(self: *Self, inst: Air.Inst.Index) InnerError!void {
28342934 // unsupported by wasm itself. Can be implemented once we support DWARF
28352935 // for wasm
2836 return .none;
2936 self.finishAir(inst, .none, &.{});
28372937}
28382938
2839fn airUnreachable(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2840 _ = inst;
2939fn airUnreachable(self: *Self, inst: Air.Inst.Index) InnerError!void {
28412940 try self.addTag(.@"unreachable");
2842 return .none;
2941 self.finishAir(inst, .none, &.{});
28432942}
28442943
2845fn airBitcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2944fn airBitcast(self: *Self, inst: Air.Inst.Index) InnerError!void {
28462945 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
2847 return self.resolveInst(ty_op.operand);
2946 const result = if (!self.liveness.isUnused(inst)) result: {
2947 break :result try self.resolveInst(ty_op.operand);
2948 } else WValue{ .none = {} };
2949 self.finishAir(inst, result, &.{});
28482950}
28492951
2850fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2952fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) InnerError!void {
28512953 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
28522954 const extra = self.air.extraData(Air.StructField, ty_pl.payload);
2955 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{extra.data.struct_operand});
2956
28532957 const struct_ptr = try self.resolveInst(extra.data.struct_operand);
28542958 const struct_ty = self.air.typeOf(extra.data.struct_operand).childType();
28552959 const offset = std.math.cast(u32, struct_ty.structFieldOffset(extra.data.field_index, self.target)) orelse {
......@@ -2858,11 +2962,13 @@ fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
28582962 struct_ty.structFieldType(extra.data.field_index).fmt(module),
28592963 });
28602964 };
2861 return self.structFieldPtr(struct_ptr, offset);
2965 const result = try self.structFieldPtr(struct_ptr, offset);
2966 self.finishAir(inst, result, &.{extra.data.struct_operand});
28622967}
28632968
2864fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u32) InnerError!WValue {
2969fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u32) InnerError!void {
28652970 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
2971 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
28662972 const struct_ptr = try self.resolveInst(ty_op.operand);
28672973 const struct_ty = self.air.typeOf(ty_op.operand).childType();
28682974 const field_ty = struct_ty.structFieldType(index);
......@@ -2872,7 +2978,8 @@ fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u32) InnerEr
28722978 field_ty.fmt(module),
28732979 });
28742980 };
2875 return self.structFieldPtr(struct_ptr, offset);
2981 const result = try self.structFieldPtr(struct_ptr, offset);
2982 self.finishAir(inst, result, &.{ty_op.operand});
28762983}
28772984
28782985fn structFieldPtr(self: *Self, struct_ptr: WValue, offset: u32) InnerError!WValue {
......@@ -2884,35 +2991,39 @@ fn structFieldPtr(self: *Self, struct_ptr: WValue, offset: u32) InnerError!WValu
28842991 }
28852992}
28862993
2887fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2888 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
2889
2994fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) InnerError!void {
28902995 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
28912996 const struct_field = self.air.extraData(Air.StructField, ty_pl.payload).data;
2997 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{struct_field.struct_operand});
2998
28922999 const struct_ty = self.air.typeOf(struct_field.struct_operand);
28933000 const operand = try self.resolveInst(struct_field.struct_operand);
28943001 const field_index = struct_field.field_index;
28953002 const field_ty = struct_ty.structFieldType(field_index);
2896 if (!field_ty.hasRuntimeBitsIgnoreComptime()) return WValue{ .none = {} };
3003 if (!field_ty.hasRuntimeBitsIgnoreComptime()) return self.finishAir(inst, .none, &.{struct_field.struct_operand});
3004
28973005 const offset = std.math.cast(u32, struct_ty.structFieldOffset(field_index, self.target)) orelse {
28983006 const module = self.bin_file.base.options.module.?;
28993007 return self.fail("Field type '{}' too big to fit into stack frame", .{field_ty.fmt(module)});
29003008 };
29013009
2902 if (isByRef(field_ty, self.target)) {
2903 switch (operand) {
2904 .stack_offset => |stack_offset| {
2905 return WValue{ .stack_offset = stack_offset + offset };
2906 },
2907 else => return self.buildPointerOffset(operand, offset, .new),
3010 const result = result: {
3011 if (isByRef(field_ty, self.target)) {
3012 switch (operand) {
3013 .stack_offset => |stack_offset| {
3014 break :result WValue{ .stack_offset = stack_offset + offset };
3015 },
3016 else => break :result try self.buildPointerOffset(operand, offset, .new),
3017 }
29083018 }
2909 }
29103019
2911 const field = try self.load(operand, field_ty, offset);
2912 return field.toLocal(self, field_ty);
3020 const field = try self.load(operand, field_ty, offset);
3021 break :result try field.toLocal(self, field_ty);
3022 };
3023 self.finishAir(inst, result, &.{struct_field.struct_operand});
29133024}
29143025
2915fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3026fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void {
29163027 // result type is always 'noreturn'
29173028 const blocktype = wasm.block_empty;
29183029 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
......@@ -3071,133 +3182,149 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
30713182 try self.genBody(else_body);
30723183 try self.endBlock();
30733184 }
3074 return .none;
3185 self.finishAir(inst, .none, &.{});
30753186}
30763187
3077fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!WValue {
3188fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!void {
30783189 const un_op = self.air.instructions.items(.data)[inst].un_op;
3190 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{un_op});
30793191 const operand = try self.resolveInst(un_op);
30803192 const err_union_ty = self.air.typeOf(un_op);
30813193 const pl_ty = err_union_ty.errorUnionPayload();
30823194
3083 if (err_union_ty.errorUnionSet().errorSetIsEmpty()) {
3084 switch (opcode) {
3085 .i32_ne => return WValue{ .imm32 = 0 },
3086 .i32_eq => return WValue{ .imm32 = 1 },
3087 else => unreachable,
3195 const result = result: {
3196 if (err_union_ty.errorUnionSet().errorSetIsEmpty()) {
3197 switch (opcode) {
3198 .i32_ne => break :result WValue{ .imm32 = 0 },
3199 .i32_eq => break :result WValue{ .imm32 = 1 },
3200 else => unreachable,
3201 }
30883202 }
3089 }
30903203
3091 try self.emitWValue(operand);
3092 if (pl_ty.hasRuntimeBitsIgnoreComptime()) {
3093 try self.addMemArg(.i32_load16_u, .{
3094 .offset = operand.offset() + @intCast(u32, errUnionErrorOffset(pl_ty, self.target)),
3095 .alignment = Type.anyerror.abiAlignment(self.target),
3096 });
3097 }
3204 try self.emitWValue(operand);
3205 if (pl_ty.hasRuntimeBitsIgnoreComptime()) {
3206 try self.addMemArg(.i32_load16_u, .{
3207 .offset = operand.offset() + @intCast(u32, errUnionErrorOffset(pl_ty, self.target)),
3208 .alignment = Type.anyerror.abiAlignment(self.target),
3209 });
3210 }
30983211
3099 // Compare the error value with '0'
3100 try self.addImm32(0);
3101 try self.addTag(Mir.Inst.Tag.fromOpcode(opcode));
3212 // Compare the error value with '0'
3213 try self.addImm32(0);
3214 try self.addTag(Mir.Inst.Tag.fromOpcode(opcode));
31023215
3103 const is_err_tmp = try self.allocLocal(Type.i32);
3104 try self.addLabel(.local_set, is_err_tmp.local);
3105 return is_err_tmp;
3216 const is_err_tmp = try self.allocLocal(Type.i32);
3217 try self.addLabel(.local_set, is_err_tmp.local);
3218 break :result is_err_tmp;
3219 };
3220 self.finishAir(inst, result, &.{un_op});
31063221}
31073222
3108fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!WValue {
3109 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3223fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void {
31103224 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3225 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
3226
31113227 const operand = try self.resolveInst(ty_op.operand);
31123228 const op_ty = self.air.typeOf(ty_op.operand);
31133229 const err_ty = if (op_is_ptr) op_ty.childType() else op_ty;
31143230 const payload_ty = err_ty.errorUnionPayload();
31153231
3116 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) return WValue{ .none = {} };
3232 const result = result: {
3233 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) break :result WValue{ .none = {} };
31173234
3118 const pl_offset = @intCast(u32, errUnionPayloadOffset(payload_ty, self.target));
3119 if (op_is_ptr or isByRef(payload_ty, self.target)) {
3120 return self.buildPointerOffset(operand, pl_offset, .new);
3121 }
3235 const pl_offset = @intCast(u32, errUnionPayloadOffset(payload_ty, self.target));
3236 if (op_is_ptr or isByRef(payload_ty, self.target)) {
3237 break :result try self.buildPointerOffset(operand, pl_offset, .new);
3238 }
31223239
3123 const payload = try self.load(operand, payload_ty, pl_offset);
3124 return payload.toLocal(self, payload_ty);
3240 const payload = try self.load(operand, payload_ty, pl_offset);
3241 break :result try payload.toLocal(self, payload_ty);
3242 };
3243 self.finishAir(inst, result, &.{ty_op.operand});
31253244}
31263245
3127fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!WValue {
3128 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3129
3246fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void {
31303247 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3248 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
3249
31313250 const operand = try self.resolveInst(ty_op.operand);
31323251 const op_ty = self.air.typeOf(ty_op.operand);
31333252 const err_ty = if (op_is_ptr) op_ty.childType() else op_ty;
31343253 const payload_ty = err_ty.errorUnionPayload();
31353254
3136 if (err_ty.errorUnionSet().errorSetIsEmpty()) {
3137 return WValue{ .imm32 = 0 };
3138 }
3255 const result = result: {
3256 if (err_ty.errorUnionSet().errorSetIsEmpty()) {
3257 break :result WValue{ .imm32 = 0 };
3258 }
31393259
3140 if (op_is_ptr or !payload_ty.hasRuntimeBitsIgnoreComptime()) {
3141 return operand;
3142 }
3260 if (op_is_ptr or !payload_ty.hasRuntimeBitsIgnoreComptime()) {
3261 break :result operand;
3262 }
31433263
3144 const error_val = try self.load(operand, Type.anyerror, @intCast(u32, errUnionErrorOffset(payload_ty, self.target)));
3145 return error_val.toLocal(self, Type.anyerror);
3264 const error_val = try self.load(operand, Type.anyerror, @intCast(u32, errUnionErrorOffset(payload_ty, self.target)));
3265 break :result try error_val.toLocal(self, Type.anyerror);
3266 };
3267 self.finishAir(inst, result, &.{ty_op.operand});
31463268}
31473269
3148fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3149 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3150
3270fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!void {
31513271 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3272 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
3273
31523274 const operand = try self.resolveInst(ty_op.operand);
31533275 const err_ty = self.air.typeOfIndex(inst);
31543276
31553277 const pl_ty = self.air.typeOf(ty_op.operand);
3156 if (!pl_ty.hasRuntimeBitsIgnoreComptime()) {
3157 return operand;
3158 }
3159
3160 const err_union = try self.allocStack(err_ty);
3161 const payload_ptr = try self.buildPointerOffset(err_union, @intCast(u32, errUnionPayloadOffset(pl_ty, self.target)), .new);
3162 try self.store(payload_ptr, operand, pl_ty, 0);
3278 const result = result: {
3279 if (!pl_ty.hasRuntimeBitsIgnoreComptime()) {
3280 break :result operand;
3281 }
31633282
3164 // ensure we also write '0' to the error part, so any present stack value gets overwritten by it.
3165 try self.emitWValue(err_union);
3166 try self.addImm32(0);
3167 const err_val_offset = @intCast(u32, errUnionErrorOffset(pl_ty, self.target));
3168 try self.addMemArg(.i32_store16, .{ .offset = err_union.offset() + err_val_offset, .alignment = 2 });
3283 const err_union = try self.allocStack(err_ty);
3284 const payload_ptr = try self.buildPointerOffset(err_union, @intCast(u32, errUnionPayloadOffset(pl_ty, self.target)), .new);
3285 try self.store(payload_ptr, operand, pl_ty, 0);
31693286
3170 return err_union;
3287 // ensure we also write '0' to the error part, so any present stack value gets overwritten by it.
3288 try self.emitWValue(err_union);
3289 try self.addImm32(0);
3290 const err_val_offset = @intCast(u32, errUnionErrorOffset(pl_ty, self.target));
3291 try self.addMemArg(.i32_store16, .{ .offset = err_union.offset() + err_val_offset, .alignment = 2 });
3292 break :result err_union;
3293 };
3294 self.finishAir(inst, result, &.{ty_op.operand});
31713295}
31723296
3173fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3174 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3175
3297fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) InnerError!void {
31763298 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3299 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
3300
31773301 const operand = try self.resolveInst(ty_op.operand);
31783302 const err_ty = self.air.getRefType(ty_op.ty);
31793303 const pl_ty = err_ty.errorUnionPayload();
31803304
3181 if (!pl_ty.hasRuntimeBitsIgnoreComptime()) {
3182 return operand;
3183 }
3305 const result = result: {
3306 if (!pl_ty.hasRuntimeBitsIgnoreComptime()) {
3307 break :result operand;
3308 }
31843309
3185 const err_union = try self.allocStack(err_ty);
3186 // store error value
3187 try self.store(err_union, operand, Type.anyerror, @intCast(u32, errUnionErrorOffset(pl_ty, self.target)));
3310 const err_union = try self.allocStack(err_ty);
3311 // store error value
3312 try self.store(err_union, operand, Type.anyerror, @intCast(u32, errUnionErrorOffset(pl_ty, self.target)));
31883313
3189 // write 'undefined' to the payload
3190 const payload_ptr = try self.buildPointerOffset(err_union, @intCast(u32, errUnionPayloadOffset(pl_ty, self.target)), .new);
3191 const len = @intCast(u32, err_ty.errorUnionPayload().abiSize(self.target));
3192 try self.memset(payload_ptr, .{ .imm32 = len }, .{ .imm32 = 0xaaaaaaaa });
3314 // write 'undefined' to the payload
3315 const payload_ptr = try self.buildPointerOffset(err_union, @intCast(u32, errUnionPayloadOffset(pl_ty, self.target)), .new);
3316 const len = @intCast(u32, err_ty.errorUnionPayload().abiSize(self.target));
3317 try self.memset(payload_ptr, .{ .imm32 = len }, .{ .imm32 = 0xaaaaaaaa });
31933318
3194 return err_union;
3319 break :result err_union;
3320 };
3321 self.finishAir(inst, result, &.{ty_op.operand});
31953322}
31963323
3197fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3198 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3199
3324fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!void {
32003325 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3326 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
3327
32013328 const ty = self.air.getRefType(ty_op.ty);
32023329 const operand = try self.resolveInst(ty_op.operand);
32033330 const operand_ty = self.air.typeOf(ty_op.operand);
......@@ -3208,7 +3335,8 @@ fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
32083335 return self.fail("todo Wasm intcast for bitsize > 128", .{});
32093336 }
32103337
3211 return (try self.intcast(operand, operand_ty, ty)).toLocal(self, ty);
3338 const result = try (try self.intcast(operand, operand_ty, ty)).toLocal(self, ty);
3339 self.finishAir(inst, result, &.{ty_op.operand});
32123340}
32133341
32143342/// Upcasts or downcasts an integer based on the given and wanted types,
......@@ -3263,14 +3391,16 @@ fn intcast(self: *Self, operand: WValue, given: Type, wanted: Type) InnerError!W
32633391 return WValue{ .stack = {} };
32643392}
32653393
3266fn airIsNull(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: enum { value, ptr }) InnerError!WValue {
3394fn airIsNull(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: enum { value, ptr }) InnerError!void {
32673395 const un_op = self.air.instructions.items(.data)[inst].un_op;
3396 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{un_op});
32683397 const operand = try self.resolveInst(un_op);
32693398
32703399 const op_ty = self.air.typeOf(un_op);
32713400 const optional_ty = if (op_kind == .ptr) op_ty.childType() else op_ty;
32723401 const is_null = try self.isNull(operand, optional_ty, opcode);
3273 return is_null.toLocal(self, optional_ty);
3402 const result = try is_null.toLocal(self, optional_ty);
3403 self.finishAir(inst, result, &.{un_op});
32743404}
32753405
32763406/// For a given type and operand, checks if it's considered `null`.
......@@ -3294,43 +3424,50 @@ fn isNull(self: *Self, operand: WValue, optional_ty: Type, opcode: wasm.Opcode)
32943424 return WValue{ .stack = {} };
32953425}
32963426
3297fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3298 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3427fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) InnerError!void {
32993428 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3300 const operand = try self.resolveInst(ty_op.operand);
33013429 const opt_ty = self.air.typeOf(ty_op.operand);
33023430 const payload_ty = self.air.typeOfIndex(inst);
3303 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) return WValue{ .none = {} };
3304 if (opt_ty.optionalReprIsPayload()) return operand;
3431 if (self.liveness.isUnused(inst) or !payload_ty.hasRuntimeBitsIgnoreComptime()) {
3432 return self.finishAir(inst, .none, &.{ty_op.operand});
3433 }
33053434
3306 const offset = opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target);
3435 const result = result: {
3436 const operand = try self.resolveInst(ty_op.operand);
3437 if (opt_ty.optionalReprIsPayload()) break :result operand;
33073438
3308 if (isByRef(payload_ty, self.target)) {
3309 return self.buildPointerOffset(operand, offset, .new);
3310 }
3439 const offset = opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target);
33113440
3312 const payload = try self.load(operand, payload_ty, @intCast(u32, offset));
3313 return payload.toLocal(self, payload_ty);
3314}
3441 if (isByRef(payload_ty, self.target)) {
3442 break :result try self.buildPointerOffset(operand, offset, .new);
3443 }
33153444
3316fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3317 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3445 const payload = try self.load(operand, payload_ty, @intCast(u32, offset));
3446 break :result try payload.toLocal(self, payload_ty);
3447 };
3448 self.finishAir(inst, result, &.{ty_op.operand});
3449}
33183450
3451fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) InnerError!void {
33193452 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3453 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
33203454 const operand = try self.resolveInst(ty_op.operand);
33213455 const opt_ty = self.air.typeOf(ty_op.operand).childType();
33223456
3323 var buf: Type.Payload.ElemType = undefined;
3324 const payload_ty = opt_ty.optionalChild(&buf);
3325 if (!payload_ty.hasRuntimeBitsIgnoreComptime() or opt_ty.optionalReprIsPayload()) {
3326 return operand;
3327 }
3457 const result = result: {
3458 var buf: Type.Payload.ElemType = undefined;
3459 const payload_ty = opt_ty.optionalChild(&buf);
3460 if (!payload_ty.hasRuntimeBitsIgnoreComptime() or opt_ty.optionalReprIsPayload()) {
3461 break :result operand;
3462 }
33283463
3329 const offset = opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target);
3330 return self.buildPointerOffset(operand, offset, .new);
3464 const offset = opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target);
3465 break :result try self.buildPointerOffset(operand, offset, .new);
3466 };
3467 self.finishAir(inst, result, &.{ty_op.operand});
33313468}
33323469
3333fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3470fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!void {
33343471 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
33353472 const operand = try self.resolveInst(ty_op.operand);
33363473 const opt_ty = self.air.typeOf(ty_op.operand).childType();
......@@ -3341,7 +3478,7 @@ fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue
33413478 }
33423479
33433480 if (opt_ty.optionalReprIsPayload()) {
3344 return operand;
3481 return self.finishAir(inst, operand, &.{ty_op.operand});
33453482 }
33463483
33473484 const offset = std.math.cast(u32, opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target)) orelse {
......@@ -3353,49 +3490,53 @@ fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue
33533490 try self.addImm32(1);
33543491 try self.addMemArg(.i32_store8, .{ .offset = operand.offset(), .alignment = 1 });
33553492
3356 return self.buildPointerOffset(operand, offset, .new);
3493 const result = try self.buildPointerOffset(operand, offset, .new);
3494 return self.finishAir(inst, result, &.{ty_op.operand});
33573495}
33583496
3359fn airWrapOptional(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3360 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3361
3497fn airWrapOptional(self: *Self, inst: Air.Inst.Index) InnerError!void {
33623498 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3499 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
33633500 const payload_ty = self.air.typeOf(ty_op.operand);
3364 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
3365 const non_null_bit = try self.allocStack(Type.initTag(.u1));
3366 try self.emitWValue(non_null_bit);
3367 try self.addImm32(1);
3368 try self.addMemArg(.i32_store8, .{ .offset = non_null_bit.offset(), .alignment = 1 });
3369 return non_null_bit;
3370 }
33713501
3372 const operand = try self.resolveInst(ty_op.operand);
3373 const op_ty = self.air.typeOfIndex(inst);
3374 if (op_ty.optionalReprIsPayload()) {
3375 return operand;
3376 }
3377 const offset = std.math.cast(u32, op_ty.abiSize(self.target) - payload_ty.abiSize(self.target)) orelse {
3378 const module = self.bin_file.base.options.module.?;
3379 return self.fail("Optional type {} too big to fit into stack frame", .{op_ty.fmt(module)});
3380 };
3502 const result = result: {
3503 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
3504 const non_null_bit = try self.allocStack(Type.initTag(.u1));
3505 try self.emitWValue(non_null_bit);
3506 try self.addImm32(1);
3507 try self.addMemArg(.i32_store8, .{ .offset = non_null_bit.offset(), .alignment = 1 });
3508 break :result non_null_bit;
3509 }
33813510
3382 // Create optional type, set the non-null bit, and store the operand inside the optional type
3383 const result = try self.allocStack(op_ty);
3384 try self.emitWValue(result);
3385 try self.addImm32(1);
3386 try self.addMemArg(.i32_store8, .{ .offset = result.offset(), .alignment = 1 });
3511 const operand = try self.resolveInst(ty_op.operand);
3512 const op_ty = self.air.typeOfIndex(inst);
3513 if (op_ty.optionalReprIsPayload()) {
3514 break :result operand;
3515 }
3516 const offset = std.math.cast(u32, op_ty.abiSize(self.target) - payload_ty.abiSize(self.target)) orelse {
3517 const module = self.bin_file.base.options.module.?;
3518 return self.fail("Optional type {} too big to fit into stack frame", .{op_ty.fmt(module)});
3519 };
33873520
3388 const payload_ptr = try self.buildPointerOffset(result, offset, .new);
3389 try self.store(payload_ptr, operand, payload_ty, 0);
3521 // Create optional type, set the non-null bit, and store the operand inside the optional type
3522 const result_ptr = try self.allocStack(op_ty);
3523 try self.emitWValue(result_ptr);
3524 try self.addImm32(1);
3525 try self.addMemArg(.i32_store8, .{ .offset = result_ptr.offset(), .alignment = 1 });
33903526
3391 return result;
3392}
3527 const payload_ptr = try self.buildPointerOffset(result_ptr, offset, .new);
3528 try self.store(payload_ptr, operand, payload_ty, 0);
3529 break :result result_ptr;
3530 };
33933531
3394fn airSlice(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3395 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3532 self.finishAir(inst, result, &.{ty_op.operand});
3533}
33963534
3535fn airSlice(self: *Self, inst: Air.Inst.Index) InnerError!void {
33973536 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
33983537 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
3538 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
3539
33993540 const lhs = try self.resolveInst(bin_op.lhs);
34003541 const rhs = try self.resolveInst(bin_op.rhs);
34013542 const slice_ty = self.air.typeOfIndex(inst);
......@@ -3404,23 +3545,23 @@ fn airSlice(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
34043545 try self.store(slice, lhs, Type.usize, 0);
34053546 try self.store(slice, rhs, Type.usize, self.ptrSize());
34063547
3407 return slice;
3548 self.finishAir(inst, slice, &.{ bin_op.lhs, bin_op.rhs });
34083549}
34093550
3410fn airSliceLen(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3411 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3412
3551fn airSliceLen(self: *Self, inst: Air.Inst.Index) InnerError!void {
34133552 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3414 const operand = try self.resolveInst(ty_op.operand);
3553 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
34153554
3555 const operand = try self.resolveInst(ty_op.operand);
34163556 const len = try self.load(operand, Type.usize, self.ptrSize());
3417 return len.toLocal(self, Type.usize);
3557 const result = try len.toLocal(self, Type.usize);
3558 self.finishAir(inst, result, &.{ty_op.operand});
34183559}
34193560
3420fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3421 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3422
3561fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!void {
34233562 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
3563 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
3564
34243565 const slice_ty = self.air.typeOf(bin_op.lhs);
34253566 const slice = try self.resolveInst(bin_op.lhs);
34263567 const index = try self.resolveInst(bin_op.rhs);
......@@ -3436,21 +3577,22 @@ fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
34363577 try self.addTag(.i32_mul);
34373578 try self.addTag(.i32_add);
34383579
3439 const result = try self.allocLocal(elem_ty);
3440 try self.addLabel(.local_set, result.local);
3580 const result_ptr = try self.allocLocal(elem_ty);
3581 try self.addLabel(.local_set, result_ptr.local);
34413582
3442 if (isByRef(elem_ty, self.target)) {
3443 return result;
3444 }
3583 const result = if (!isByRef(elem_ty, self.target)) result: {
3584 const elem_val = try self.load(result_ptr, elem_ty, 0);
3585 break :result try elem_val.toLocal(self, elem_ty);
3586 } else result_ptr;
34453587
3446 const elem_val = try self.load(result, elem_ty, 0);
3447 return elem_val.toLocal(self, elem_ty);
3588 self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs });
34483589}
34493590
3450fn airSliceElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3451 if (self.liveness.isUnused(inst)) return WValue.none;
3591fn airSliceElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!void {
34523592 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
34533593 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
3594 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
3595
34543596 const elem_ty = self.air.getRefType(ty_pl.ty).childType();
34553597 const elem_size = elem_ty.abiSize(self.target);
34563598
......@@ -3467,20 +3609,22 @@ fn airSliceElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
34673609
34683610 const result = try self.allocLocal(Type.i32);
34693611 try self.addLabel(.local_set, result.local);
3470 return result;
3612 self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs });
34713613}
34723614
3473fn airSlicePtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3474 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3615fn airSlicePtr(self: *Self, inst: Air.Inst.Index) InnerError!void {
34753616 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3617 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
34763618 const operand = try self.resolveInst(ty_op.operand);
34773619 const ptr = try self.load(operand, Type.usize, 0);
3478 return ptr.toLocal(self, Type.usize);
3620 const result = try ptr.toLocal(self, Type.usize);
3621 self.finishAir(inst, result, &.{ty_op.operand});
34793622}
34803623
3481fn airTrunc(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3482 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3624fn airTrunc(self: *Self, inst: Air.Inst.Index) InnerError!void {
34833625 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3626 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
3627
34843628 const operand = try self.resolveInst(ty_op.operand);
34853629 const wanted_ty = self.air.getRefType(ty_op.ty);
34863630 const op_ty = self.air.typeOf(ty_op.operand);
......@@ -3496,16 +3640,24 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
34963640 if (wasm_bits != wanted_bits) {
34973641 result = try self.wrapOperand(result, wanted_ty);
34983642 }
3499 return result.toLocal(self, wanted_ty);
3643
3644 self.finishAir(inst, try result.toLocal(self, wanted_ty), &.{ty_op.operand});
35003645}
35013646
3502fn airBoolToInt(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3647fn airBoolToInt(self: *Self, inst: Air.Inst.Index) InnerError!void {
35033648 const un_op = self.air.instructions.items(.data)[inst].un_op;
3504 return self.resolveInst(un_op);
3649 const result = if (self.liveness.isUnused(inst))
3650 WValue{ .none = {} }
3651 else
3652 try self.resolveInst(un_op);
3653
3654 self.finishAir(inst, result, &.{un_op});
35053655}
35063656
3507fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3657fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) InnerError!void {
35083658 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3659 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
3660
35093661 const operand = try self.resolveInst(ty_op.operand);
35103662 const array_ty = self.air.typeOf(ty_op.operand).childType();
35113663 const slice_ty = self.air.getRefType(ty_op.ty);
......@@ -3522,25 +3674,26 @@ fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
35223674 const len = WValue{ .imm32 = @intCast(u32, array_ty.arrayLen()) };
35233675 try self.store(slice_local, len, Type.usize, self.ptrSize());
35243676
3525 return slice_local;
3677 self.finishAir(inst, slice_local, &.{ty_op.operand});
35263678}
35273679
3528fn airPtrToInt(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3529 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3680fn airPtrToInt(self: *Self, inst: Air.Inst.Index) InnerError!void {
35303681 const un_op = self.air.instructions.items(.data)[inst].un_op;
3682 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{un_op});
35313683 const operand = try self.resolveInst(un_op);
35323684
3533 switch (operand) {
3685 const result = switch (operand) {
35343686 // for stack offset, return a pointer to this offset.
3535 .stack_offset => return self.buildPointerOffset(operand, 0, .new),
3536 else => return operand,
3537 }
3687 .stack_offset => try self.buildPointerOffset(operand, 0, .new),
3688 else => operand,
3689 };
3690 self.finishAir(inst, result, &.{un_op});
35383691}
35393692
3540fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3541 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3542
3693fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) InnerError!void {
35433694 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
3695 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
3696
35443697 const ptr_ty = self.air.typeOf(bin_op.lhs);
35453698 const ptr = try self.resolveInst(bin_op.lhs);
35463699 const index = try self.resolveInst(bin_op.rhs);
......@@ -3560,21 +3713,25 @@ fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
35603713 try self.addTag(.i32_mul);
35613714 try self.addTag(.i32_add);
35623715
3563 var result = try self.allocLocal(elem_ty);
3564 try self.addLabel(.local_set, result.local);
3565 if (isByRef(elem_ty, self.target)) {
3566 return result;
3567 }
3568 defer result.free(self); // only free if it's not returned like above
3716 const elem_result = val: {
3717 var result = try self.allocLocal(elem_ty);
3718 try self.addLabel(.local_set, result.local);
3719 if (isByRef(elem_ty, self.target)) {
3720 break :val result;
3721 }
3722 defer result.free(self); // only free if it's not returned like above
35693723
3570 const elem_val = try self.load(result, elem_ty, 0);
3571 return elem_val.toLocal(self, elem_ty);
3724 const elem_val = try self.load(result, elem_ty, 0);
3725 break :val try elem_val.toLocal(self, elem_ty);
3726 };
3727 self.finishAir(inst, elem_result, &.{ bin_op.lhs, bin_op.rhs });
35723728}
35733729
3574fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3575 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3730fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!void {
35763731 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
35773732 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
3733 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
3734
35783735 const ptr_ty = self.air.typeOf(bin_op.lhs);
35793736 const elem_ty = self.air.getRefType(ty_pl.ty).childType();
35803737 const elem_size = elem_ty.abiSize(self.target);
......@@ -3597,13 +3754,14 @@ fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
35973754
35983755 const result = try self.allocLocal(Type.i32);
35993756 try self.addLabel(.local_set, result.local);
3600 return result;
3757 self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs });
36013758}
36023759
3603fn airPtrBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue {
3604 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3760fn airPtrBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void {
36053761 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
36063762 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
3763 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
3764
36073765 const ptr = try self.resolveInst(bin_op.lhs);
36083766 const offset = try self.resolveInst(bin_op.rhs);
36093767 const ptr_ty = self.air.typeOf(bin_op.lhs);
......@@ -3624,10 +3782,10 @@ fn airPtrBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue {
36243782
36253783 const result = try self.allocLocal(Type.usize);
36263784 try self.addLabel(.local_set, result.local);
3627 return result;
3785 self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs });
36283786}
36293787
3630fn airMemset(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3788fn airMemset(self: *Self, inst: Air.Inst.Index) InnerError!void {
36313789 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
36323790 const bin_op = self.air.extraData(Air.Bin, pl_op.payload).data;
36333791
......@@ -3636,7 +3794,7 @@ fn airMemset(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
36363794 const len = try self.resolveInst(bin_op.rhs);
36373795 try self.memset(ptr, len, value);
36383796
3639 return WValue{ .none = {} };
3797 self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
36403798}
36413799
36423800/// Sets a region of memory at `ptr` to the value of `value`
......@@ -3724,10 +3882,10 @@ fn memset(self: *Self, ptr: WValue, len: WValue, value: WValue) InnerError!void
37243882 }
37253883}
37263884
3727fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3728 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3729
3885fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) InnerError!void {
37303886 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
3887 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
3888
37313889 const array_ty = self.air.typeOf(bin_op.lhs);
37323890 const array = try self.resolveInst(bin_op.lhs);
37333891 const index = try self.resolveInst(bin_op.rhs);
......@@ -3740,22 +3898,26 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
37403898 try self.addTag(.i32_mul);
37413899 try self.addTag(.i32_add);
37423900
3743 var result = try self.allocLocal(Type.usize);
3744 try self.addLabel(.local_set, result.local);
3901 const elem_result = val: {
3902 var result = try self.allocLocal(Type.usize);
3903 try self.addLabel(.local_set, result.local);
37453904
3746 if (isByRef(elem_ty, self.target)) {
3747 return result;
3748 }
3749 defer result.free(self); // only free if no longer needed and not returned like above
3905 if (isByRef(elem_ty, self.target)) {
3906 break :val result;
3907 }
3908 defer result.free(self); // only free if no longer needed and not returned like above
37503909
3751 const elem_val = try self.load(result, elem_ty, 0);
3752 return elem_val.toLocal(self, elem_ty);
3753}
3910 const elem_val = try self.load(result, elem_ty, 0);
3911 break :val try elem_val.toLocal(self, elem_ty);
3912 };
37543913
3755fn airFloatToInt(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3756 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3914 self.finishAir(inst, elem_result, &.{ bin_op.lhs, bin_op.rhs });
3915}
37573916
3917fn airFloatToInt(self: *Self, inst: Air.Inst.Index) InnerError!void {
37583918 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3919 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
3920
37593921 const operand = try self.resolveInst(ty_op.operand);
37603922 const dest_ty = self.air.typeOfIndex(inst);
37613923 const op_ty = self.air.typeOf(ty_op.operand);
......@@ -3773,13 +3935,14 @@ fn airFloatToInt(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
37733935 });
37743936 try self.addTag(Mir.Inst.Tag.fromOpcode(op));
37753937 const wrapped = try self.wrapOperand(.{ .stack = {} }, dest_ty);
3776 return wrapped.toLocal(self, dest_ty);
3938 const result = try wrapped.toLocal(self, dest_ty);
3939 self.finishAir(inst, result, &.{ty_op.operand});
37773940}
37783941
3779fn airIntToFloat(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3780 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3781
3942fn airIntToFloat(self: *Self, inst: Air.Inst.Index) InnerError!void {
37823943 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3944 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
3945
37833946 const operand = try self.resolveInst(ty_op.operand);
37843947 const dest_ty = self.air.typeOfIndex(inst);
37853948 const op_ty = self.air.typeOf(ty_op.operand);
......@@ -3799,12 +3962,10 @@ fn airIntToFloat(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
37993962
38003963 const result = try self.allocLocal(dest_ty);
38013964 try self.addLabel(.local_set, result.local);
3802 return result;
3965 self.finishAir(inst, result, &.{ty_op.operand});
38033966}
38043967
3805fn airSplat(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3806 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3807
3968fn airSplat(self: *Self, inst: Air.Inst.Index) InnerError!void {
38083969 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
38093970 const operand = try self.resolveInst(ty_op.operand);
38103971
......@@ -3812,9 +3973,7 @@ fn airSplat(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
38123973 return self.fail("TODO: Implement wasm airSplat", .{});
38133974}
38143975
3815fn airSelect(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3816 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3817
3976fn airSelect(self: *Self, inst: Air.Inst.Index) InnerError!void {
38183977 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
38193978 const operand = try self.resolveInst(pl_op.operand);
38203979
......@@ -3822,9 +3981,7 @@ fn airSelect(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
38223981 return self.fail("TODO: Implement wasm airSelect", .{});
38233982}
38243983
3825fn airShuffle(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3826 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3827
3984fn airShuffle(self: *Self, inst: Air.Inst.Index) InnerError!void {
38283985 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
38293986 const operand = try self.resolveInst(ty_op.operand);
38303987
......@@ -3832,9 +3989,7 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
38323989 return self.fail("TODO: Implement wasm airShuffle", .{});
38333990}
38343991
3835fn airReduce(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3836 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3837
3992fn airReduce(self: *Self, inst: Air.Inst.Index) InnerError!void {
38383993 const reduce = self.air.instructions.items(.data)[inst].reduce;
38393994 const operand = try self.resolveInst(reduce.operand);
38403995
......@@ -3842,126 +3997,130 @@ fn airReduce(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
38423997 return self.fail("TODO: Implement wasm airReduce", .{});
38433998}
38443999
3845fn airAggregateInit(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3846 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3847
4000fn airAggregateInit(self: *Self, inst: Air.Inst.Index) InnerError!void {
38484001 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
38494002 const result_ty = self.air.typeOfIndex(inst);
38504003 const len = @intCast(usize, result_ty.arrayLen());
38514004 const elements = @ptrCast([]const Air.Inst.Ref, self.air.extra[ty_pl.payload..][0..len]);
38524005
3853 switch (result_ty.zigTypeTag()) {
3854 .Vector => return self.fail("TODO: Wasm backend: implement airAggregateInit for vectors", .{}),
3855 .Array => {
3856 const result = try self.allocStack(result_ty);
3857 const elem_ty = result_ty.childType();
3858 const elem_size = @intCast(u32, elem_ty.abiSize(self.target));
3859
3860 // When the element type is by reference, we must copy the entire
3861 // value. It is therefore safer to move the offset pointer and store
3862 // each value individually, instead of using store offsets.
3863 if (isByRef(elem_ty, self.target)) {
3864 // copy stack pointer into a temporary local, which is
3865 // moved for each element to store each value in the right position.
3866 const offset = try self.buildPointerOffset(result, 0, .new);
3867 for (elements) |elem, elem_index| {
3868 const elem_val = try self.resolveInst(elem);
3869 try self.store(offset, elem_val, elem_ty, 0);
4006 const result: WValue = result_value: {
4007 if (self.liveness.isUnused(inst)) break :result_value WValue.none;
4008 switch (result_ty.zigTypeTag()) {
4009 .Array => {
4010 const result = try self.allocStack(result_ty);
4011 const elem_ty = result_ty.childType();
4012 const elem_size = @intCast(u32, elem_ty.abiSize(self.target));
38704013
3871 if (elem_index < elements.len - 1) {
3872 _ = try self.buildPointerOffset(offset, elem_size, .modify);
4014 // When the element type is by reference, we must copy the entire
4015 // value. It is therefore safer to move the offset pointer and store
4016 // each value individually, instead of using store offsets.
4017 if (isByRef(elem_ty, self.target)) {
4018 // copy stack pointer into a temporary local, which is
4019 // moved for each element to store each value in the right position.
4020 const offset = try self.buildPointerOffset(result, 0, .new);
4021 for (elements) |elem, elem_index| {
4022 const elem_val = try self.resolveInst(elem);
4023 try self.store(offset, elem_val, elem_ty, 0);
4024
4025 if (elem_index < elements.len - 1) {
4026 _ = try self.buildPointerOffset(offset, elem_size, .modify);
4027 }
4028 }
4029 } else {
4030 var offset: u32 = 0;
4031 for (elements) |elem| {
4032 const elem_val = try self.resolveInst(elem);
4033 try self.store(result, elem_val, elem_ty, offset);
4034 offset += elem_size;
38734035 }
38744036 }
3875 } else {
3876 var offset: u32 = 0;
3877 for (elements) |elem| {
3878 const elem_val = try self.resolveInst(elem);
3879 try self.store(result, elem_val, elem_ty, offset);
3880 offset += elem_size;
3881 }
3882 }
3883 return result;
3884 },
3885 .Struct => {
3886 const result = try self.allocStack(result_ty);
3887 const offset = try self.buildPointerOffset(result, 0, .new); // pointer to offset
3888 for (elements) |elem, elem_index| {
3889 if (result_ty.structFieldValueComptime(elem_index) != null) continue;
4037 break :result_value result;
4038 },
4039 .Struct => {
4040 const result = try self.allocStack(result_ty);
4041 const offset = try self.buildPointerOffset(result, 0, .new); // pointer to offset
4042 for (elements) |elem, elem_index| {
4043 if (result_ty.structFieldValueComptime(elem_index) != null) continue;
38904044
3891 const elem_ty = result_ty.structFieldType(elem_index);
3892 const elem_size = @intCast(u32, elem_ty.abiSize(self.target));
3893 const value = try self.resolveInst(elem);
3894 try self.store(offset, value, elem_ty, 0);
4045 const elem_ty = result_ty.structFieldType(elem_index);
4046 const elem_size = @intCast(u32, elem_ty.abiSize(self.target));
4047 const value = try self.resolveInst(elem);
4048 try self.store(offset, value, elem_ty, 0);
38954049
3896 if (elem_index < elements.len - 1) {
3897 _ = try self.buildPointerOffset(offset, elem_size, .modify);
4050 if (elem_index < elements.len - 1) {
4051 _ = try self.buildPointerOffset(offset, elem_size, .modify);
4052 }
38984053 }
3899 }
39004054
3901 return result;
3902 },
3903 else => unreachable,
3904 }
4055 break :result_value result;
4056 },
4057 .Vector => return self.fail("TODO: Wasm backend: implement airAggregateInit for vectors", .{}),
4058 else => unreachable,
4059 }
4060 };
4061 self.finishAir(inst, result, &.{});
39054062}
39064063
3907fn airUnionInit(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3908 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3909
4064fn airUnionInit(self: *Self, inst: Air.Inst.Index) InnerError!void {
39104065 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
39114066 const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data;
3912 const union_ty = self.air.typeOfIndex(inst);
3913 const layout = union_ty.unionGetLayout(self.target);
3914 if (layout.payload_size == 0) {
3915 if (layout.tag_size == 0) {
3916 return WValue{ .none = {} };
4067 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{extra.init});
4068
4069 const result = result: {
4070 const union_ty = self.air.typeOfIndex(inst);
4071 const layout = union_ty.unionGetLayout(self.target);
4072 if (layout.payload_size == 0) {
4073 if (layout.tag_size == 0) {
4074 break :result WValue{ .none = {} };
4075 }
4076 assert(!isByRef(union_ty, self.target));
4077 break :result WValue{ .imm32 = extra.field_index };
39174078 }
3918 assert(!isByRef(union_ty, self.target));
3919 return WValue{ .imm32 = extra.field_index };
3920 }
3921 assert(isByRef(union_ty, self.target));
4079 assert(isByRef(union_ty, self.target));
39224080
3923 const result_ptr = try self.allocStack(union_ty);
3924 const payload = try self.resolveInst(extra.init);
3925 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
3926 assert(union_obj.haveFieldTypes());
3927 const field = union_obj.fields.values()[extra.field_index];
4081 const result_ptr = try self.allocStack(union_ty);
4082 const payload = try self.resolveInst(extra.init);
4083 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
4084 assert(union_obj.haveFieldTypes());
4085 const field = union_obj.fields.values()[extra.field_index];
39284086
3929 if (layout.tag_align >= layout.payload_align) {
3930 const payload_ptr = try self.buildPointerOffset(result_ptr, layout.tag_size, .new);
3931 try self.store(payload_ptr, payload, field.ty, 0);
3932 } else {
3933 try self.store(result_ptr, payload, field.ty, 0);
3934 }
4087 if (layout.tag_align >= layout.payload_align) {
4088 const payload_ptr = try self.buildPointerOffset(result_ptr, layout.tag_size, .new);
4089 try self.store(payload_ptr, payload, field.ty, 0);
4090 } else {
4091 try self.store(result_ptr, payload, field.ty, 0);
4092 }
4093 break :result result_ptr;
4094 };
39354095
3936 return result_ptr;
4096 self.finishAir(inst, result, &.{extra.init});
39374097}
39384098
3939fn airPrefetch(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4099fn airPrefetch(self: *Self, inst: Air.Inst.Index) InnerError!void {
39404100 const prefetch = self.air.instructions.items(.data)[inst].prefetch;
3941 _ = prefetch;
3942 return WValue{ .none = {} };
4101 self.finishAir(inst, .none, &.{prefetch.ptr});
39434102}
39444103
3945fn airWasmMemorySize(self: *Self, inst: Air.Inst.Index) !WValue {
3946 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3947
4104fn airWasmMemorySize(self: *Self, inst: Air.Inst.Index) InnerError!void {
39484105 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
4106 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{pl_op.operand});
39494107
39504108 const result = try self.allocLocal(self.air.typeOfIndex(inst));
39514109 try self.addLabel(.memory_size, pl_op.payload);
39524110 try self.addLabel(.local_set, result.local);
3953 return result;
4111 self.finishAir(inst, result, &.{pl_op.operand});
39544112}
39554113
3956fn airWasmMemoryGrow(self: *Self, inst: Air.Inst.Index) !WValue {
4114fn airWasmMemoryGrow(self: *Self, inst: Air.Inst.Index) !void {
39574115 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
3958 const operand = try self.resolveInst(pl_op.operand);
4116 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{pl_op.operand});
39594117
4118 const operand = try self.resolveInst(pl_op.operand);
39604119 const result = try self.allocLocal(self.air.typeOfIndex(inst));
39614120 try self.emitWValue(operand);
39624121 try self.addLabel(.memory_grow, pl_op.payload);
39634122 try self.addLabel(.local_set, result.local);
3964 return result;
4123 self.finishAir(inst, result, &.{pl_op.operand});
39654124}
39664125
39674126fn cmpOptionals(self: *Self, lhs: WValue, rhs: WValue, operand_ty: Type, op: std.math.CompareOperator) InnerError!WValue {
......@@ -4042,17 +4201,18 @@ fn cmpBigInt(self: *Self, lhs: WValue, rhs: WValue, operand_ty: Type, op: std.ma
40424201 return WValue{ .stack = {} };
40434202}
40444203
4045fn airSetUnionTag(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4204fn airSetUnionTag(self: *Self, inst: Air.Inst.Index) InnerError!void {
40464205 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
40474206 const un_ty = self.air.typeOf(bin_op.lhs).childType();
40484207 const tag_ty = self.air.typeOf(bin_op.rhs);
40494208 const layout = un_ty.unionGetLayout(self.target);
4050 if (layout.tag_size == 0) return WValue{ .none = {} };
4209 if (layout.tag_size == 0) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
4210
40514211 const union_ptr = try self.resolveInst(bin_op.lhs);
40524212 const new_tag = try self.resolveInst(bin_op.rhs);
40534213 if (layout.payload_size == 0) {
40544214 try self.store(union_ptr, new_tag, tag_ty, 0);
4055 return WValue{ .none = {} };
4215 return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
40564216 }
40574217
40584218 // when the tag alignment is smaller than the payload, the field will be stored
......@@ -4061,37 +4221,38 @@ fn airSetUnionTag(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
40614221 break :blk @intCast(u32, layout.payload_size);
40624222 } else @as(u32, 0);
40634223 try self.store(union_ptr, new_tag, tag_ty, offset);
4064 return WValue{ .none = {} };
4224 self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
40654225}
40664226
4067fn airGetUnionTag(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4068 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
4069
4227fn airGetUnionTag(self: *Self, inst: Air.Inst.Index) InnerError!void {
40704228 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
4229 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
4230
40714231 const un_ty = self.air.typeOf(ty_op.operand);
40724232 const tag_ty = self.air.typeOfIndex(inst);
40734233 const layout = un_ty.unionGetLayout(self.target);
4074 if (layout.tag_size == 0) return WValue{ .none = {} };
4075 const operand = try self.resolveInst(ty_op.operand);
4234 if (layout.tag_size == 0) return self.finishAir(inst, .none, &.{ty_op.operand});
40764235
4236 const operand = try self.resolveInst(ty_op.operand);
40774237 // when the tag alignment is smaller than the payload, the field will be stored
40784238 // after the payload.
40794239 const offset = if (layout.tag_align < layout.payload_align) blk: {
40804240 break :blk @intCast(u32, layout.payload_size);
40814241 } else @as(u32, 0);
40824242 const tag = try self.load(operand, tag_ty, offset);
4083 return tag.toLocal(self, tag_ty);
4243 const result = try tag.toLocal(self, tag_ty);
4244 self.finishAir(inst, result, &.{ty_op.operand});
40844245}
40854246
4086fn airFpext(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4087 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
4088
4247fn airFpext(self: *Self, inst: Air.Inst.Index) InnerError!void {
40894248 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
4249 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
4250
40904251 const dest_ty = self.air.typeOfIndex(inst);
40914252 const operand = try self.resolveInst(ty_op.operand);
4092
40934253 const extended = try self.fpext(operand, self.air.typeOf(ty_op.operand), dest_ty);
4094 return extended.toLocal(self, dest_ty);
4254 const result = try extended.toLocal(self, dest_ty);
4255 self.finishAir(inst, result, &.{ty_op.operand});
40954256}
40964257
40974258/// Extends a float from a given `Type` to a larger wanted `Type`
......@@ -4127,14 +4288,15 @@ fn fpext(self: *Self, operand: WValue, given: Type, wanted: Type) InnerError!WVa
41274288 }
41284289}
41294290
4130fn airFptrunc(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4131 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
4132
4291fn airFptrunc(self: *Self, inst: Air.Inst.Index) InnerError!void {
41334292 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
4293 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
4294
41344295 const dest_ty = self.air.typeOfIndex(inst);
41354296 const operand = try self.resolveInst(ty_op.operand);
41364297 const trunc = try self.fptrunc(operand, self.air.typeOf(ty_op.operand), dest_ty);
4137 return trunc.toLocal(self, dest_ty);
4298 const result = try trunc.toLocal(self, dest_ty);
4299 self.finishAir(inst, result, &.{ty_op.operand});
41384300}
41394301
41404302/// Truncates a float from a given `Type` to its wanted `Type`
......@@ -4162,8 +4324,10 @@ fn fptrunc(self: *Self, operand: WValue, given: Type, wanted: Type) InnerError!W
41624324 }
41634325}
41644326
4165fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4327fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!void {
41664328 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
4329 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
4330
41674331 const err_set_ty = self.air.typeOf(ty_op.operand).childType();
41684332 const payload_ty = err_set_ty.errorUnionPayload();
41694333 const operand = try self.resolveInst(ty_op.operand);
......@@ -4176,50 +4340,54 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue
41764340 @intCast(u32, errUnionErrorOffset(payload_ty, self.target)),
41774341 );
41784342
4179 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
4343 const result = result: {
4344 if (self.liveness.isUnused(inst)) break :result WValue{ .none = {} };
41804345
4181 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
4182 return operand;
4183 }
4346 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
4347 break :result operand;
4348 }
41844349
4185 return self.buildPointerOffset(operand, @intCast(u32, errUnionPayloadOffset(payload_ty, self.target)), .new);
4350 break :result try self.buildPointerOffset(operand, @intCast(u32, errUnionPayloadOffset(payload_ty, self.target)), .new);
4351 };
4352 self.finishAir(inst, result, &.{ty_op.operand});
41864353}
41874354
4188fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4189 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
4190
4355fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) InnerError!void {
41914356 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
41924357 const extra = self.air.extraData(Air.FieldParentPtr, ty_pl.payload).data;
4193 const field_ptr = try self.resolveInst(extra.field_ptr);
4358 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{extra.field_ptr});
41944359
4360 const field_ptr = try self.resolveInst(extra.field_ptr);
41954361 const struct_ty = self.air.getRefType(ty_pl.ty).childType();
41964362 const field_offset = struct_ty.structFieldOffset(extra.field_index, self.target);
41974363
4198 if (field_offset == 0) {
4199 return field_ptr;
4200 }
4364 const result = if (field_offset != 0) result: {
4365 const base = try self.buildPointerOffset(field_ptr, 0, .new);
4366 try self.addLabel(.local_get, base.local);
4367 try self.addImm32(@bitCast(i32, @intCast(u32, field_offset)));
4368 try self.addTag(.i32_sub);
4369 try self.addLabel(.local_set, base.local);
4370 break :result base;
4371 } else field_ptr;
42014372
4202 const base = try self.buildPointerOffset(field_ptr, 0, .new);
4203 try self.addLabel(.local_get, base.local);
4204 try self.addImm32(@bitCast(i32, @intCast(u32, field_offset)));
4205 try self.addTag(.i32_sub);
4206 try self.addLabel(.local_set, base.local);
4207 return base;
4373 self.finishAir(inst, result, &.{extra.field_ptr});
42084374}
42094375
4210fn airMemcpy(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4376fn airMemcpy(self: *Self, inst: Air.Inst.Index) InnerError!void {
42114377 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
42124378 const bin_op = self.air.extraData(Air.Bin, pl_op.payload).data;
42134379 const dst = try self.resolveInst(pl_op.operand);
42144380 const src = try self.resolveInst(bin_op.lhs);
42154381 const len = try self.resolveInst(bin_op.rhs);
42164382 try self.memcpy(dst, src, len);
4217 return WValue{ .none = {} };
4383
4384 self.finishAir(inst, .none, &.{ pl_op.operand, bin_op.lhs, bin_op.rhs });
42184385}
42194386
4220fn airPopcount(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4221 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
4387fn airPopcount(self: *Self, inst: Air.Inst.Index) InnerError!void {
42224388 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
4389 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
4390
42234391 const operand = try self.resolveInst(ty_op.operand);
42244392 const op_ty = self.air.typeOf(ty_op.operand);
42254393 const result_ty = self.air.typeOfIndex(inst);
......@@ -4258,15 +4426,14 @@ fn airPopcount(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
42584426
42594427 const result = try self.allocLocal(result_ty);
42604428 try self.addLabel(.local_set, result.local);
4261 return result;
4429 self.finishAir(inst, result, &.{ty_op.operand});
42624430}
42634431
4264fn airErrorName(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4265 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
4266
4432fn airErrorName(self: *Self, inst: Air.Inst.Index) InnerError!void {
42674433 const un_op = self.air.instructions.items(.data)[inst].un_op;
4268 const operand = try self.resolveInst(un_op);
4434 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{un_op});
42694435
4436 const operand = try self.resolveInst(un_op);
42704437 // First retrieve the symbol index to the error name table
42714438 // that will be used to emit a relocation for the pointer
42724439 // to the error name table.
......@@ -4301,21 +4468,23 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
43014468
43024469 const result_ptr = try self.allocLocal(Type.usize);
43034470 try self.addLabel(.local_set, result_ptr.local);
4304 return result_ptr;
4471 self.finishAir(inst, result_ptr, &.{un_op});
43054472}
43064473
4307fn airPtrSliceFieldPtr(self: *Self, inst: Air.Inst.Index, offset: u32) InnerError!WValue {
4308 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
4309
4474fn airPtrSliceFieldPtr(self: *Self, inst: Air.Inst.Index, offset: u32) InnerError!void {
43104475 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
4476 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
43114477 const slice_ptr = try self.resolveInst(ty_op.operand);
4312 return self.buildPointerOffset(slice_ptr, offset, .new);
4478 const result = try self.buildPointerOffset(slice_ptr, offset, .new);
4479 self.finishAir(inst, result, &.{ty_op.operand});
43134480}
43144481
4315fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue {
4482fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void {
43164483 assert(op == .add or op == .sub);
43174484 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
43184485 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
4486 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ extra.lhs, extra.rhs });
4487
43194488 const lhs_op = try self.resolveInst(extra.lhs);
43204489 const rhs_op = try self.resolveInst(extra.rhs);
43214490 const lhs_ty = self.air.typeOf(extra.lhs);
......@@ -4331,7 +4500,8 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!W
43314500 };
43324501
43334502 if (wasm_bits == 128) {
4334 return self.airAddSubWithOverflowBigInt(lhs_op, rhs_op, lhs_ty, self.air.typeOfIndex(inst), op);
4503 const result = try self.addSubWithOverflowBigInt(lhs_op, rhs_op, lhs_ty, self.air.typeOfIndex(inst), op);
4504 return self.finishAir(inst, result, &.{ extra.lhs, extra.rhs });
43354505 }
43364506
43374507 const zero = switch (wasm_bits) {
......@@ -4349,6 +4519,15 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!W
43494519 break :blk try (try self.signAbsValue(rhs_op, lhs_ty)).toLocal(self, lhs_ty);
43504520 } else rhs_op;
43514521
4522 // in this case, we performed a signAbsValue which created a temporary local
4523 // so let's free this so it can be re-used instead.
4524 // In the other case we do not want to free it, because that would free the
4525 // resolved instructions which may be referenced by other instructions.
4526 defer if (wasm_bits != int_info.bits and is_signed) {
4527 lhs.free(self);
4528 rhs.free(self);
4529 };
4530
43524531 var bin_op = try (try self.binOp(lhs, rhs, lhs_ty, op)).toLocal(self, lhs_ty);
43534532 defer bin_op.free(self);
43544533 var result = if (wasm_bits != int_info.bits) blk: {
......@@ -4377,19 +4556,10 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!W
43774556 const offset = @intCast(u32, lhs_ty.abiSize(self.target));
43784557 try self.store(result_ptr, overflow_local, Type.initTag(.u1), offset);
43794558
4380 // in this case, we performed a signAbsValue which created a temporary local
4381 // so let's free this so it can be re-used instead.
4382 // In the other case we do not want to free it, because that would free the
4383 // resolved instructions which may be referenced by other instructions.
4384 if (wasm_bits != int_info.bits and is_signed) {
4385 lhs.free(self);
4386 rhs.free(self);
4387 }
4388
4389 return result_ptr;
4559 self.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs });
43904560}
43914561
4392fn airAddSubWithOverflowBigInt(self: *Self, lhs: WValue, rhs: WValue, ty: Type, result_ty: Type, op: Op) InnerError!WValue {
4562fn addSubWithOverflowBigInt(self: *Self, lhs: WValue, rhs: WValue, ty: Type, result_ty: Type, op: Op) InnerError!WValue {
43934563 assert(op == .add or op == .sub);
43944564 const int_info = ty.intInfo(self.target);
43954565 const is_signed = int_info.signedness == .signed;
......@@ -4453,9 +4623,11 @@ fn airAddSubWithOverflowBigInt(self: *Self, lhs: WValue, rhs: WValue, ty: Type,
44534623 return result_ptr;
44544624}
44554625
4456fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4626fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!void {
44574627 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
44584628 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
4629 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ extra.lhs, extra.rhs });
4630
44594631 const lhs = try self.resolveInst(extra.lhs);
44604632 const rhs = try self.resolveInst(extra.rhs);
44614633 const lhs_ty = self.air.typeOf(extra.lhs);
......@@ -4496,12 +4668,14 @@ fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
44964668 const offset = @intCast(u32, lhs_ty.abiSize(self.target));
44974669 try self.store(result_ptr, overflow_local, Type.initTag(.u1), offset);
44984670
4499 return result_ptr;
4671 self.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs });
45004672}
45014673
4502fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4674fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!void {
45034675 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
45044676 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
4677 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ extra.lhs, extra.rhs });
4678
45054679 const lhs = try self.resolveInst(extra.lhs);
45064680 const rhs = try self.resolveInst(extra.rhs);
45074681 const lhs_ty = self.air.typeOf(extra.lhs);
......@@ -4581,12 +4755,13 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
45814755 const offset = @intCast(u32, lhs_ty.abiSize(self.target));
45824756 try self.store(result_ptr, overflow_bit, Type.initTag(.u1), offset);
45834757
4584 return result_ptr;
4758 self.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs });
45854759}
45864760
4587fn airMaxMin(self: *Self, inst: Air.Inst.Index, op: enum { max, min }) InnerError!WValue {
4588 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
4761fn airMaxMin(self: *Self, inst: Air.Inst.Index, op: enum { max, min }) InnerError!void {
45894762 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
4763 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
4764
45904765 const ty = self.air.typeOfIndex(inst);
45914766 if (ty.zigTypeTag() == .Vector) {
45924767 return self.fail("TODO: `@maximum` and `@minimum` for vectors", .{});
......@@ -4611,13 +4786,14 @@ fn airMaxMin(self: *Self, inst: Air.Inst.Index, op: enum { max, min }) InnerErro
46114786 const result_ty = if (isByRef(ty, self.target)) Type.u32 else ty;
46124787 const result = try self.allocLocal(result_ty);
46134788 try self.addLabel(.local_set, result.local);
4614 return result;
4789 self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs });
46154790}
46164791
4617fn airMulAdd(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4618 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
4792fn airMulAdd(self: *Self, inst: Air.Inst.Index) InnerError!void {
46194793 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
46204794 const bin_op = self.air.extraData(Air.Bin, pl_op.payload).data;
4795 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
4796
46214797 const ty = self.air.typeOfIndex(inst);
46224798 if (ty.zigTypeTag() == .Vector) {
46234799 return self.fail("TODO: `@mulAdd` for vectors", .{});
......@@ -4627,7 +4803,7 @@ fn airMulAdd(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
46274803 const lhs = try self.resolveInst(bin_op.lhs);
46284804 const rhs = try self.resolveInst(bin_op.rhs);
46294805
4630 if (ty.floatBits(self.target) == 16) {
4806 const result = if (ty.floatBits(self.target) == 16) fl_result: {
46314807 const rhs_ext = try self.fpext(rhs, ty, Type.f32);
46324808 const lhs_ext = try self.fpext(lhs, ty, Type.f32);
46334809 const addend_ext = try self.fpext(addend, ty, Type.f32);
......@@ -4638,16 +4814,19 @@ fn airMulAdd(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
46384814 Type.f32,
46394815 &.{ rhs_ext, lhs_ext, addend_ext },
46404816 );
4641 return try (try self.fptrunc(result, Type.f32, ty)).toLocal(self, ty);
4642 }
4817 break :fl_result try (try self.fptrunc(result, Type.f32, ty)).toLocal(self, ty);
4818 } else result: {
4819 const mul_result = try self.binOp(lhs, rhs, ty, .mul);
4820 break :result try (try self.binOp(mul_result, addend, ty, .add)).toLocal(self, ty);
4821 };
46434822
4644 const mul_result = try self.binOp(lhs, rhs, ty, .mul);
4645 return (try self.binOp(mul_result, addend, ty, .add)).toLocal(self, ty);
4823 self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs });
46464824}
46474825
4648fn airClz(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4649 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
4826fn airClz(self: *Self, inst: Air.Inst.Index) InnerError!void {
46504827 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
4828 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
4829
46514830 const ty = self.air.typeOf(ty_op.operand);
46524831 const result_ty = self.air.typeOfIndex(inst);
46534832 if (ty.zigTypeTag() == .Vector) {
......@@ -4694,12 +4873,13 @@ fn airClz(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
46944873
46954874 const result = try self.allocLocal(result_ty);
46964875 try self.addLabel(.local_set, result.local);
4697 return result;
4876 self.finishAir(inst, result, &.{ty_op.operand});
46984877}
46994878
4700fn airCtz(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4701 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
4879fn airCtz(self: *Self, inst: Air.Inst.Index) InnerError!void {
47024880 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
4881 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
4882
47034883 const ty = self.air.typeOf(ty_op.operand);
47044884 const result_ty = self.air.typeOfIndex(inst);
47054885
......@@ -4758,11 +4938,11 @@ fn airCtz(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
47584938
47594939 const result = try self.allocLocal(result_ty);
47604940 try self.addLabel(.local_set, result.local);
4761 return result;
4941 self.finishAir(inst, result, &.{ty_op.operand});
47624942}
47634943
4764fn airDbgVar(self: *Self, inst: Air.Inst.Index, is_ptr: bool) !WValue {
4765 if (self.debug_output != .dwarf) return WValue{ .none = {} };
4944fn airDbgVar(self: *Self, inst: Air.Inst.Index, is_ptr: bool) !void {
4945 if (self.debug_output != .dwarf) return self.finishAir(inst, .none, &.{});
47664946
47674947 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
47684948 const ty = self.air.typeOf(pl_op.operand);
......@@ -4799,11 +4979,11 @@ fn airDbgVar(self: *Self, inst: Air.Inst.Index, is_ptr: bool) !WValue {
47994979 try self.addDbgInfoTypeReloc(op_ty);
48004980 dbg_info.appendSliceAssumeCapacity(name);
48014981 dbg_info.appendAssumeCapacity(0);
4802 return WValue{ .none = {} };
4982 self.finishAir(inst, .none, &.{});
48034983}
48044984
4805fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !WValue {
4806 if (self.debug_output != .dwarf) return WValue{ .none = {} };
4985fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void {
4986 if (self.debug_output != .dwarf) return self.finishAir(inst, .none, &.{});
48074987
48084988 const dbg_stmt = self.air.instructions.items(.data)[inst].dbg_stmt;
48094989 try self.addInst(.{ .tag = .dbg_line, .data = .{
......@@ -4812,25 +4992,27 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !WValue {
48124992 .column = dbg_stmt.column,
48134993 }),
48144994 } });
4815 return WValue{ .none = {} };
4995 self.finishAir(inst, .none, &.{});
48164996}
48174997
4818fn airTry(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4998fn airTry(self: *Self, inst: Air.Inst.Index) InnerError!void {
48194999 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
48205000 const err_union = try self.resolveInst(pl_op.operand);
48215001 const extra = self.air.extraData(Air.Try, pl_op.payload);
48225002 const body = self.air.extra[extra.end..][0..extra.data.body_len];
48235003 const err_union_ty = self.air.typeOf(pl_op.operand);
4824 return lowerTry(self, err_union, body, err_union_ty, false);
5004 const result = try lowerTry(self, err_union, body, err_union_ty, false);
5005 self.finishAir(inst, result, &.{pl_op.operand});
48255006}
48265007
4827fn airTryPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
5008fn airTryPtr(self: *Self, inst: Air.Inst.Index) InnerError!void {
48285009 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
48295010 const extra = self.air.extraData(Air.TryPtr, ty_pl.payload);
48305011 const err_union_ptr = try self.resolveInst(extra.data.ptr);
48315012 const body = self.air.extra[extra.end..][0..extra.data.body_len];
48325013 const err_union_ty = self.air.typeOf(extra.data.ptr).childType();
4833 return lowerTry(self, err_union_ptr, body, err_union_ty, true);
5014 const result = try lowerTry(self, err_union_ptr, body, err_union_ty, true);
5015 self.finishAir(inst, result, &.{extra.data.ptr});
48345016}
48355017
48365018fn lowerTry(
......@@ -4879,12 +5061,10 @@ fn lowerTry(
48795061 return payload.toLocal(self, pl_ty);
48805062}
48815063
4882fn airByteSwap(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4883 if (self.liveness.isUnused(inst)) {
4884 return WValue{ .none = {} };
4885 }
4886
5064fn airByteSwap(self: *Self, inst: Air.Inst.Index) InnerError!void {
48875065 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
5066 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand});
5067
48885068 const ty = self.air.typeOfIndex(inst);
48895069 const operand = try self.resolveInst(ty_op.operand);
48905070
......@@ -4895,84 +5075,89 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
48955075
48965076 // bytes are no-op
48975077 if (int_info.bits == 8) {
4898 return operand;
4899 }
4900
4901 switch (int_info.bits) {
4902 16 => {
4903 const shl_res = try self.binOp(operand, .{ .imm32 = 8 }, ty, .shl);
4904 const lhs = try self.binOp(shl_res, .{ .imm32 = 0xFF00 }, ty, .@"and");
4905 const shr_res = try self.binOp(operand, .{ .imm32 = 8 }, ty, .shr);
4906 const res = if (int_info.signedness == .signed) blk: {
4907 break :blk try self.wrapOperand(shr_res, Type.u8);
4908 } else shr_res;
4909 return (try self.binOp(lhs, res, ty, .@"or")).toLocal(self, ty);
4910 },
4911 24 => {
4912 var msb = try (try self.wrapOperand(operand, Type.u16)).toLocal(self, Type.u16);
4913 defer msb.free(self);
4914
4915 const shl_res = try self.binOp(msb, .{ .imm32 = 8 }, Type.u16, .shl);
4916 const lhs = try self.binOp(shl_res, .{ .imm32 = 0xFF0000 }, Type.u16, .@"and");
4917 const shr_res = try self.binOp(msb, .{ .imm32 = 8 }, ty, .shr);
4918
4919 const res = if (int_info.signedness == .signed) blk: {
4920 break :blk try self.wrapOperand(shr_res, Type.u8);
4921 } else shr_res;
4922 const lhs_tmp = try self.binOp(lhs, res, ty, .@"or");
4923 const lhs_result = try self.binOp(lhs_tmp, .{ .imm32 = 8 }, ty, .shr);
4924 const rhs_wrap = try self.wrapOperand(msb, Type.u8);
4925 const rhs_result = try self.binOp(rhs_wrap, .{ .imm32 = 16 }, ty, .shl);
4926
4927 const lsb = try self.wrapBinOp(operand, .{ .imm32 = 16 }, Type.u8, .shr);
4928 const tmp = try self.binOp(lhs_result, rhs_result, ty, .@"or");
4929 return (try self.binOp(tmp, lsb, ty, .@"or")).toLocal(self, ty);
4930 },
4931 32 => {
4932 const shl_tmp = try self.binOp(operand, .{ .imm32 = 8 }, ty, .shl);
4933 var lhs = try (try self.binOp(shl_tmp, .{ .imm32 = 0xFF00FF00 }, ty, .@"and")).toLocal(self, ty);
4934 defer lhs.free(self);
4935 const shr_tmp = try self.binOp(operand, .{ .imm32 = 8 }, ty, .shr);
4936 var rhs = try (try self.binOp(shr_tmp, .{ .imm32 = 0xFF00FF }, ty, .@"and")).toLocal(self, ty);
4937 defer rhs.free(self);
4938 var tmp_or = try (try self.binOp(lhs, rhs, ty, .@"or")).toLocal(self, ty);
4939 defer tmp_or.free(self);
4940
4941 const shl = try self.binOp(tmp_or, .{ .imm32 = 16 }, ty, .shl);
4942 const shr = try self.binOp(tmp_or, .{ .imm32 = 16 }, ty, .shr);
4943 const res = if (int_info.signedness == .signed) blk: {
4944 break :blk try self.wrapOperand(shr, Type.u16);
4945 } else shr;
4946 return (try self.binOp(shl, res, ty, .@"or")).toLocal(self, ty);
4947 },
4948 else => return self.fail("TODO: @byteSwap for integers with bitsize {d}", .{int_info.bits}),
4949 }
5078 return self.finishAir(inst, operand, &.{ty_op.operand});
5079 }
5080
5081 const result = result: {
5082 switch (int_info.bits) {
5083 16 => {
5084 const shl_res = try self.binOp(operand, .{ .imm32 = 8 }, ty, .shl);
5085 const lhs = try self.binOp(shl_res, .{ .imm32 = 0xFF00 }, ty, .@"and");
5086 const shr_res = try self.binOp(operand, .{ .imm32 = 8 }, ty, .shr);
5087 const res = if (int_info.signedness == .signed) blk: {
5088 break :blk try self.wrapOperand(shr_res, Type.u8);
5089 } else shr_res;
5090 break :result try (try self.binOp(lhs, res, ty, .@"or")).toLocal(self, ty);
5091 },
5092 24 => {
5093 var msb = try (try self.wrapOperand(operand, Type.u16)).toLocal(self, Type.u16);
5094 defer msb.free(self);
5095
5096 const shl_res = try self.binOp(msb, .{ .imm32 = 8 }, Type.u16, .shl);
5097 const lhs = try self.binOp(shl_res, .{ .imm32 = 0xFF0000 }, Type.u16, .@"and");
5098 const shr_res = try self.binOp(msb, .{ .imm32 = 8 }, ty, .shr);
5099
5100 const res = if (int_info.signedness == .signed) blk: {
5101 break :blk try self.wrapOperand(shr_res, Type.u8);
5102 } else shr_res;
5103 const lhs_tmp = try self.binOp(lhs, res, ty, .@"or");
5104 const lhs_result = try self.binOp(lhs_tmp, .{ .imm32 = 8 }, ty, .shr);
5105 const rhs_wrap = try self.wrapOperand(msb, Type.u8);
5106 const rhs_result = try self.binOp(rhs_wrap, .{ .imm32 = 16 }, ty, .shl);
5107
5108 const lsb = try self.wrapBinOp(operand, .{ .imm32 = 16 }, Type.u8, .shr);
5109 const tmp = try self.binOp(lhs_result, rhs_result, ty, .@"or");
5110 break :result try (try self.binOp(tmp, lsb, ty, .@"or")).toLocal(self, ty);
5111 },
5112 32 => {
5113 const shl_tmp = try self.binOp(operand, .{ .imm32 = 8 }, ty, .shl);
5114 var lhs = try (try self.binOp(shl_tmp, .{ .imm32 = 0xFF00FF00 }, ty, .@"and")).toLocal(self, ty);
5115 defer lhs.free(self);
5116 const shr_tmp = try self.binOp(operand, .{ .imm32 = 8 }, ty, .shr);
5117 var rhs = try (try self.binOp(shr_tmp, .{ .imm32 = 0xFF00FF }, ty, .@"and")).toLocal(self, ty);
5118 defer rhs.free(self);
5119 var tmp_or = try (try self.binOp(lhs, rhs, ty, .@"or")).toLocal(self, ty);
5120 defer tmp_or.free(self);
5121
5122 const shl = try self.binOp(tmp_or, .{ .imm32 = 16 }, ty, .shl);
5123 const shr = try self.binOp(tmp_or, .{ .imm32 = 16 }, ty, .shr);
5124 const res = if (int_info.signedness == .signed) blk: {
5125 break :blk try self.wrapOperand(shr, Type.u16);
5126 } else shr;
5127 break :result try (try self.binOp(shl, res, ty, .@"or")).toLocal(self, ty);
5128 },
5129 else => return self.fail("TODO: @byteSwap for integers with bitsize {d}", .{int_info.bits}),
5130 }
5131 };
5132 self.finishAir(inst, result, &.{ty_op.operand});
49505133}
49515134
4952fn airDiv(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4953 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
4954
5135fn airDiv(self: *Self, inst: Air.Inst.Index) InnerError!void {
49555136 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
5137 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
5138
49565139 const ty = self.air.typeOfIndex(inst);
49575140 const lhs = try self.resolveInst(bin_op.lhs);
49585141 const rhs = try self.resolveInst(bin_op.rhs);
49595142
4960 if (ty.isSignedInt()) {
4961 return self.divSigned(lhs, rhs, ty);
4962 }
4963 return (try self.binOp(lhs, rhs, ty, .div)).toLocal(self, ty);
5143 const result = if (ty.isSignedInt())
5144 try self.divSigned(lhs, rhs, ty)
5145 else
5146 try (try self.binOp(lhs, rhs, ty, .div)).toLocal(self, ty);
5147 self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs });
49645148}
49655149
4966fn airDivFloor(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
4967 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
4968
5150fn airDivFloor(self: *Self, inst: Air.Inst.Index) InnerError!void {
49695151 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
5152 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
5153
49705154 const ty = self.air.typeOfIndex(inst);
49715155 const lhs = try self.resolveInst(bin_op.lhs);
49725156 const rhs = try self.resolveInst(bin_op.rhs);
49735157
49745158 if (ty.isUnsignedInt()) {
4975 return (try self.binOp(lhs, rhs, ty, .div)).toLocal(self, ty);
5159 const result = try (try self.binOp(lhs, rhs, ty, .div)).toLocal(self, ty);
5160 return self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs });
49765161 } else if (ty.isSignedInt()) {
49775162 const int_bits = ty.intInfo(self.target).bits;
49785163 const wasm_bits = toWasmBits(int_bits) orelse {
......@@ -5048,7 +5233,7 @@ fn airDivFloor(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
50485233
50495234 const result = try self.allocLocal(ty);
50505235 try self.addLabel(.local_set, result.local);
5051 return result;
5236 self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs });
50525237}
50535238
50545239fn divSigned(self: *Self, lhs: WValue, rhs: WValue, ty: Type) InnerError!WValue {
......@@ -5110,10 +5295,10 @@ fn signAbsValue(self: *Self, operand: WValue, ty: Type) InnerError!WValue {
51105295 return WValue{ .stack = {} };
51115296}
51125297
5113fn airCeilFloorTrunc(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue {
5114 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
5115
5298fn airCeilFloorTrunc(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void {
51165299 const un_op = self.air.instructions.items(.data)[inst].un_op;
5300 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{un_op});
5301
51175302 const ty = self.air.typeOfIndex(inst);
51185303 const float_bits = ty.floatBits(self.target);
51195304 const is_f16 = float_bits == 16;
......@@ -5139,14 +5324,14 @@ fn airCeilFloorTrunc(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValu
51395324
51405325 const result = try self.allocLocal(ty);
51415326 try self.addLabel(.local_set, result.local);
5142 return result;
5327 self.finishAir(inst, result, &.{un_op});
51435328}
51445329
5145fn airSatBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue {
5330fn airSatBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void {
51465331 assert(op == .add or op == .sub);
5147 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
5148
51495332 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
5333 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
5334
51505335 const ty = self.air.typeOfIndex(inst);
51515336 const lhs = try self.resolveInst(bin_op.lhs);
51525337 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -5159,7 +5344,8 @@ fn airSatBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue {
51595344 }
51605345
51615346 if (is_signed) {
5162 return signedSat(self, lhs, rhs, ty, op);
5347 const result = try signedSat(self, lhs, rhs, ty, op);
5348 return self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs });
51635349 }
51645350
51655351 const wasm_bits = toWasmBits(int_info.bits).?;
......@@ -5189,7 +5375,7 @@ fn airSatBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue {
51895375 try self.addTag(.select);
51905376 const result = try self.allocLocal(ty);
51915377 try self.addLabel(.local_set, result.local);
5192 return result;
5378 return self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs });
51935379}
51945380
51955381fn signedSat(self: *Self, lhs_operand: WValue, rhs_operand: WValue, ty: Type, op: Op) InnerError!WValue {
......@@ -5255,10 +5441,10 @@ fn signedSat(self: *Self, lhs_operand: WValue, rhs_operand: WValue, ty: Type, op
52555441 }
52565442}
52575443
5258fn airShlSat(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
5259 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
5260
5444fn airShlSat(self: *Self, inst: Air.Inst.Index) InnerError!void {
52615445 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
5446 if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });
5447
52625448 const ty = self.air.typeOfIndex(inst);
52635449 const int_info = ty.intInfo(self.target);
52645450 const is_signed = int_info.signedness == .signed;
......@@ -5271,7 +5457,7 @@ fn airShlSat(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
52715457 const wasm_bits = toWasmBits(int_info.bits).?;
52725458 const result = try self.allocLocal(ty);
52735459
5274 if (wasm_bits == int_info.bits) {
5460 if (wasm_bits == int_info.bits) outer_blk: {
52755461 var shl = try (try self.binOp(lhs, rhs, ty, .shl)).toLocal(self, ty);
52765462 defer shl.free(self);
52775463 var shr = try (try self.binOp(shl, rhs, ty, .shr)).toLocal(self, ty);
......@@ -5304,7 +5490,7 @@ fn airShlSat(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
53045490 _ = try self.cmp(lhs, shr, ty, .neq);
53055491 try self.addTag(.select);
53065492 try self.addLabel(.local_set, result.local);
5307 return result;
5493 break :outer_blk;
53085494 } else {
53095495 const shift_size = wasm_bits - int_info.bits;
53105496 const shift_value = switch (wasm_bits) {
......@@ -5353,8 +5539,10 @@ fn airShlSat(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
53535539 if (is_signed) {
53545540 shift_result = try self.wrapOperand(shift_result, ty);
53555541 }
5356 return shift_result.toLocal(self, ty);
5542 try self.addLabel(.local_set, result.local);
53575543 }
5544
5545 return self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs });
53585546}
53595547
53605548/// Calls a compiler-rt intrinsic by creating an undefined symbol,