| ... | ... | @@ -1,4 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); |
| 2 | 3 | const Allocator = std.mem.Allocator; |
| 3 | 4 | const ArrayList = std.ArrayList; |
| 4 | 5 | const assert = std.debug.assert; |
| ... | ... | @@ -91,11 +92,14 @@ const WValue = union(enum) { |
| 91 | 92 | |
| 92 | 93 | /// Marks a local as no longer being referenced and essentially allows |
| 93 | 94 | /// 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`. |
| 95 | 96 | fn free(value: *WValue, gen: *Self) void { |
| 96 | 97 | if (value.* != .local) return; |
| 97 | 98 | 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; |
| 99 | 103 | const valtype = @intToEnum(wasm.Valtype, gen.locals.items[index]); |
| 100 | 104 | switch (valtype) { |
| 101 | 105 | .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) = .{}, |
| 650 | 654 | /// It is illegal to store a non-i32 valtype in this list. |
| 651 | 655 | free_locals_f64: std.ArrayListUnmanaged(u32) = .{}, |
| 652 | 656 | |
| 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. |
| 660 | air_bookkeeping: @TypeOf(bookkeeping_init) = bookkeeping_init, |
| 661 | |
| 662 | const bookkeeping_init = if (builtin.mode == .Debug) @as(usize, 0) else {}; |
| 663 | |
| 653 | 664 | const InnerError = error{ |
| 654 | 665 | OutOfMemory, |
| 655 | 666 | /// An error occurred when trying to lower AIR to MIR. |
| ... | ... | @@ -711,6 +722,65 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!WValue { |
| 711 | 722 | return result; |
| 712 | 723 | } |
| 713 | 724 | |
| 725 | fn 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 | |
| 746 | const 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 | |
| 768 | fn 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 | |
| 777 | fn 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 | |
| 714 | 784 | /// Appends a MIR instruction and returns its index within the list of instructions |
| 715 | 785 | fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!void { |
| 716 | 786 | try self.mir_instructions.append(self.gpa, inst); |
| ... | ... | @@ -1502,7 +1572,7 @@ fn buildPointerOffset(self: *Self, ptr_value: WValue, offset: u64, action: enum |
| 1502 | 1572 | return result_ptr; |
| 1503 | 1573 | } |
| 1504 | 1574 | |
| 1505 | | fn genInst(self: *Self, inst: Air.Inst.Index) !WValue { |
| 1575 | fn genInst(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 1506 | 1576 | const air_tags = self.air.instructions.items(.tag); |
| 1507 | 1577 | return switch (air_tags[inst]) { |
| 1508 | 1578 | .constant => unreachable, |
| ... | ... | @@ -1581,7 +1651,7 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue { |
| 1581 | 1651 | .dbg_inline_end, |
| 1582 | 1652 | .dbg_block_begin, |
| 1583 | 1653 | .dbg_block_end, |
| 1584 | | => WValue.none, |
| 1654 | => self.finishAir(inst, .none, &.{}), |
| 1585 | 1655 | |
| 1586 | 1656 | .dbg_var_ptr => self.airDbgVar(inst, true), |
| 1587 | 1657 | .dbg_var_val => self.airDbgVar(inst, false), |
| ... | ... | @@ -1730,15 +1800,24 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue { |
| 1730 | 1800 | |
| 1731 | 1801 | fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 1732 | 1802 | 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 | }); |
| 1737 | 1812 | } |
| 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 | // } |
| 1738 | 1817 | } |
| 1739 | 1818 | } |
| 1740 | 1819 | |
| 1741 | | fn airRet(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1820 | fn airRet(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 1742 | 1821 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 1743 | 1822 | const operand = try self.resolveInst(un_op); |
| 1744 | 1823 | const fn_info = self.decl.ty.fnInfo(); |
| ... | ... | @@ -1776,25 +1855,30 @@ fn airRet(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1776 | 1855 | } |
| 1777 | 1856 | try self.restoreStackPointer(); |
| 1778 | 1857 | try self.addTag(.@"return"); |
| 1779 | | return WValue{ .none = {} }; |
| 1858 | |
| 1859 | self.finishAir(inst, .none, &.{un_op}); |
| 1780 | 1860 | } |
| 1781 | 1861 | |
| 1782 | | fn airRetPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1862 | fn airRetPtr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 1783 | 1863 | const child_type = self.air.typeOfIndex(inst).childType(); |
| 1784 | 1864 | |
| 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 | } |
| 1788 | 1869 | |
| 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 | } |
| 1793 | 1874 | |
| 1794 | | return self.allocStackPtr(inst); |
| 1875 | break :result try self.allocStackPtr(inst); |
| 1876 | }; |
| 1877 | |
| 1878 | self.finishAir(inst, result, &.{}); |
| 1795 | 1879 | } |
| 1796 | 1880 | |
| 1797 | | fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1881 | fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 1798 | 1882 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 1799 | 1883 | const operand = try self.resolveInst(un_op); |
| 1800 | 1884 | const ret_ty = self.air.typeOf(un_op).childType(); |
| ... | ... | @@ -1802,7 +1886,7 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1802 | 1886 | if (ret_ty.isError()) { |
| 1803 | 1887 | try self.addImm32(0); |
| 1804 | 1888 | } else { |
| 1805 | | return WValue.none; |
| 1889 | return self.finishAir(inst, .none, &.{}); |
| 1806 | 1890 | } |
| 1807 | 1891 | } |
| 1808 | 1892 | |
| ... | ... | @@ -1814,14 +1898,14 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1814 | 1898 | |
| 1815 | 1899 | try self.restoreStackPointer(); |
| 1816 | 1900 | try self.addTag(.@"return"); |
| 1817 | | return .none; |
| 1901 | return self.finishAir(inst, .none, &.{}); |
| 1818 | 1902 | } |
| 1819 | 1903 | |
| 1820 | | fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.Modifier) InnerError!WValue { |
| 1904 | fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.Modifier) InnerError!void { |
| 1821 | 1905 | if (modifier == .always_tail) return self.fail("TODO implement tail calls for wasm", .{}); |
| 1822 | 1906 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 1823 | 1907 | 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]); |
| 1825 | 1909 | const ty = self.air.typeOf(pl_op.operand); |
| 1826 | 1910 | |
| 1827 | 1911 | const fn_ty = switch (ty.zigTypeTag()) { |
| ... | ... | @@ -1865,10 +1949,9 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. |
| 1865 | 1949 | } else WValue{ .none = {} }; |
| 1866 | 1950 | |
| 1867 | 1951 | 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); |
| 1870 | 1953 | |
| 1871 | | const arg_ty = self.air.typeOf(arg_ref); |
| 1954 | const arg_ty = self.air.typeOf(arg); |
| 1872 | 1955 | if (!arg_ty.hasRuntimeBitsIgnoreComptime()) continue; |
| 1873 | 1956 | |
| 1874 | 1957 | 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. |
| 1890 | 1973 | try self.addLabel(.call_indirect, fn_type_index); |
| 1891 | 1974 | } |
| 1892 | 1975 | |
| 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); |
| 1913 | 2003 | } |
| 1914 | 2004 | |
| 1915 | | fn airAlloc(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1916 | | return self.allocStackPtr(inst); |
| 2005 | fn airAlloc(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2006 | const value = try self.allocStackPtr(inst); |
| 2007 | self.finishAir(inst, value, &.{}); |
| 1917 | 2008 | } |
| 1918 | 2009 | |
| 1919 | | fn airStore(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2010 | fn airStore(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 1920 | 2011 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 1921 | 2012 | |
| 1922 | 2013 | const lhs = try self.resolveInst(bin_op.lhs); |
| ... | ... | @@ -1924,7 +2015,7 @@ fn airStore(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1924 | 2015 | const ty = self.air.typeOf(bin_op.lhs).childType(); |
| 1925 | 2016 | |
| 1926 | 2017 | try self.store(lhs, rhs, ty, 0); |
| 1927 | | return WValue{ .none = {} }; |
| 2018 | self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 1928 | 2019 | } |
| 1929 | 2020 | |
| 1930 | 2021 | fn 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 |
| 2007 | 2098 | ); |
| 2008 | 2099 | } |
| 2009 | 2100 | |
| 2010 | | fn airLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2101 | fn airLoad(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2011 | 2102 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2012 | 2103 | const operand = try self.resolveInst(ty_op.operand); |
| 2013 | 2104 | const ty = self.air.getRefType(ty_op.ty); |
| 2014 | 2105 | |
| 2015 | | if (!ty.hasRuntimeBitsIgnoreComptime()) return WValue{ .none = {} }; |
| 2106 | if (!ty.hasRuntimeBitsIgnoreComptime()) return self.finishAir(inst, .none, &.{ty_op.operand}); |
| 2016 | 2107 | |
| 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 | } |
| 2022 | 2114 | |
| 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}); |
| 2025 | 2119 | } |
| 2026 | 2120 | |
| 2027 | 2121 | /// Loads an operand from the linear memory section. |
| ... | ... | @@ -2046,7 +2140,7 @@ fn load(self: *Self, operand: WValue, ty: Type, offset: u32) InnerError!WValue { |
| 2046 | 2140 | return WValue{ .stack = {} }; |
| 2047 | 2141 | } |
| 2048 | 2142 | |
| 2049 | | fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2143 | fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2050 | 2144 | const arg_index = self.arg_index; |
| 2051 | 2145 | const arg = self.args[arg_index]; |
| 2052 | 2146 | const cc = self.decl.ty.fnInfo().cc; |
| ... | ... | @@ -2071,7 +2165,7 @@ fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2071 | 2165 | const result = try self.allocStack(arg_ty); |
| 2072 | 2166 | try self.store(result, arg, Type.u64, 0); |
| 2073 | 2167 | try self.store(result, self.args[arg_index + 1], Type.u64, 8); |
| 2074 | | return result; |
| 2168 | return self.finishAir(inst, arg, &.{}); |
| 2075 | 2169 | } |
| 2076 | 2170 | } else { |
| 2077 | 2171 | self.arg_index += 1; |
| ... | ... | @@ -2102,19 +2196,19 @@ fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2102 | 2196 | }, |
| 2103 | 2197 | else => {}, |
| 2104 | 2198 | } |
| 2105 | | return arg; |
| 2106 | | } |
| 2107 | 2199 | |
| 2108 | | fn 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 | } |
| 2110 | 2202 | |
| 2203 | fn airBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 2111 | 2204 | 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 }); |
| 2112 | 2206 | const lhs = try self.resolveInst(bin_op.lhs); |
| 2113 | 2207 | const rhs = try self.resolveInst(bin_op.rhs); |
| 2114 | 2208 | const ty = self.air.typeOf(bin_op.lhs); |
| 2115 | 2209 | |
| 2116 | 2210 | 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 }); |
| 2118 | 2212 | } |
| 2119 | 2213 | |
| 2120 | 2214 | /// 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 |
| 2195 | 2289 | return result; |
| 2196 | 2290 | } |
| 2197 | 2291 | |
| 2198 | | fn airWrapBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { |
| 2292 | fn airWrapBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 2199 | 2293 | 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 | |
| 2200 | 2296 | const lhs = try self.resolveInst(bin_op.lhs); |
| 2201 | 2297 | const rhs = try self.resolveInst(bin_op.rhs); |
| 2202 | | |
| 2203 | 2298 | const ty = self.air.typeOf(bin_op.lhs); |
| 2299 | |
| 2204 | 2300 | if (ty.zigTypeTag() == .Vector) { |
| 2205 | 2301 | return self.fail("TODO: Implement wrapping arithmetic for vectors", .{}); |
| 2206 | 2302 | } |
| 2207 | 2303 | |
| 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 }); |
| 2209 | 2306 | } |
| 2210 | 2307 | |
| 2211 | 2308 | /// Performs a wrapping binary operation. |
| ... | ... | @@ -2582,7 +2679,7 @@ fn valueAsI32(self: Self, val: Value, ty: Type) i32 { |
| 2582 | 2679 | } |
| 2583 | 2680 | } |
| 2584 | 2681 | |
| 2585 | | fn airBlock(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2682 | fn airBlock(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2586 | 2683 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 2587 | 2684 | const block_ty = self.air.getRefType(ty_pl.ty); |
| 2588 | 2685 | const wasm_block_ty = genBlockType(block_ty, self.target); |
| ... | ... | @@ -2592,7 +2689,7 @@ fn airBlock(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2592 | 2689 | // if wasm_block_ty is non-empty, we create a register to store the temporary value |
| 2593 | 2690 | const block_result: WValue = if (wasm_block_ty != wasm.block_empty) blk: { |
| 2594 | 2691 | 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 |
| 2596 | 2693 | } else WValue.none; |
| 2597 | 2694 | |
| 2598 | 2695 | try self.startBlock(.block, wasm.block_empty); |
| ... | ... | @@ -2605,7 +2702,7 @@ fn airBlock(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2605 | 2702 | try self.genBody(body); |
| 2606 | 2703 | try self.endBlock(); |
| 2607 | 2704 | |
| 2608 | | return block_result; |
| 2705 | self.finishAir(inst, block_result, &.{}); |
| 2609 | 2706 | } |
| 2610 | 2707 | |
| 2611 | 2708 | /// 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 { |
| 2623 | 2720 | self.block_depth -= 1; |
| 2624 | 2721 | } |
| 2625 | 2722 | |
| 2626 | | fn airLoop(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2723 | fn airLoop(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2627 | 2724 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 2628 | 2725 | const loop = self.air.extraData(Air.Block, ty_pl.payload); |
| 2629 | 2726 | 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 { |
| 2637 | 2734 | try self.addLabel(.br, 0); |
| 2638 | 2735 | try self.endBlock(); |
| 2639 | 2736 | |
| 2640 | | return .none; |
| 2737 | self.finishAir(inst, .none, &.{}); |
| 2641 | 2738 | } |
| 2642 | 2739 | |
| 2643 | | fn airCondBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2740 | fn airCondBr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2644 | 2741 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 2645 | 2742 | const condition = try self.resolveInst(pl_op.operand); |
| 2646 | 2743 | const extra = self.air.extraData(Air.CondBr, pl_op.payload); |
| 2647 | 2744 | const then_body = self.air.extra[extra.end..][0..extra.data.then_body_len]; |
| 2648 | 2745 | 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); |
| 2650 | 2747 | |
| 2651 | 2748 | // result type is always noreturn, so use `block_empty` as type. |
| 2652 | 2749 | try self.startBlock(.block, wasm.block_empty); |
| ... | ... | @@ -2664,15 +2761,18 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2664 | 2761 | // Outer block that matches the condition |
| 2665 | 2762 | try self.genBody(then_body); |
| 2666 | 2763 | |
| 2667 | | return .none; |
| 2764 | self.finishAir(inst, .none, &.{}); |
| 2668 | 2765 | } |
| 2669 | 2766 | |
| 2670 | | fn airCmp(self: *Self, inst: Air.Inst.Index, op: std.math.CompareOperator) InnerError!WValue { |
| 2767 | fn airCmp(self: *Self, inst: Air.Inst.Index, op: std.math.CompareOperator) InnerError!void { |
| 2671 | 2768 | 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 | |
| 2672 | 2771 | const lhs = try self.resolveInst(bin_op.lhs); |
| 2673 | 2772 | const rhs = try self.resolveInst(bin_op.rhs); |
| 2674 | 2773 | 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 }); |
| 2676 | 2776 | } |
| 2677 | 2777 | |
| 2678 | 2778 | /// Compares two operands. |
| ... | ... | @@ -2746,14 +2846,12 @@ fn cmpFloat16(self: *Self, lhs: WValue, rhs: WValue, op: std.math.CompareOperato |
| 2746 | 2846 | return WValue{ .stack = {} }; |
| 2747 | 2847 | } |
| 2748 | 2848 | |
| 2749 | | fn airCmpVector(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2849 | fn airCmpVector(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2750 | 2850 | _ = inst; |
| 2751 | 2851 | return self.fail("TODO implement airCmpVector for wasm", .{}); |
| 2752 | 2852 | } |
| 2753 | 2853 | |
| 2754 | | fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2755 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 2756 | | |
| 2854 | fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2757 | 2855 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 2758 | 2856 | const operand = try self.resolveInst(un_op); |
| 2759 | 2857 | |
| ... | ... | @@ -2761,7 +2859,7 @@ fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2761 | 2859 | return self.fail("TODO implement airCmpLtErrorsLen for wasm", .{}); |
| 2762 | 2860 | } |
| 2763 | 2861 | |
| 2764 | | fn airBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2862 | fn airBr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2765 | 2863 | const br = self.air.instructions.items(.data)[inst].br; |
| 2766 | 2864 | const block = self.blocks.get(br.block_inst).?; |
| 2767 | 2865 | |
| ... | ... | @@ -2780,76 +2878,82 @@ fn airBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2780 | 2878 | const idx: u32 = self.block_depth - block.label; |
| 2781 | 2879 | try self.addLabel(.br, idx); |
| 2782 | 2880 | |
| 2783 | | return .none; |
| 2881 | self.finishAir(inst, .none, &.{br.operand}); |
| 2784 | 2882 | } |
| 2785 | 2883 | |
| 2786 | | fn airNot(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2884 | fn airNot(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2787 | 2885 | 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}); |
| 2788 | 2887 | |
| 2789 | 2888 | const operand = try self.resolveInst(ty_op.operand); |
| 2790 | 2889 | const operand_ty = self.air.typeOf(ty_op.operand); |
| 2791 | 2890 | |
| 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 | }; |
| 2803 | 2903 | |
| 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 | } |
| 2827 | 2928 | } |
| 2828 | | } |
| 2929 | }; |
| 2930 | self.finishAir(inst, result, &.{ty_op.operand}); |
| 2829 | 2931 | } |
| 2830 | 2932 | |
| 2831 | | fn airBreakpoint(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2832 | | _ = self; |
| 2833 | | _ = inst; |
| 2933 | fn airBreakpoint(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2834 | 2934 | // unsupported by wasm itself. Can be implemented once we support DWARF |
| 2835 | 2935 | // for wasm |
| 2836 | | return .none; |
| 2936 | self.finishAir(inst, .none, &.{}); |
| 2837 | 2937 | } |
| 2838 | 2938 | |
| 2839 | | fn airUnreachable(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2840 | | _ = inst; |
| 2939 | fn airUnreachable(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2841 | 2940 | try self.addTag(.@"unreachable"); |
| 2842 | | return .none; |
| 2941 | self.finishAir(inst, .none, &.{}); |
| 2843 | 2942 | } |
| 2844 | 2943 | |
| 2845 | | fn airBitcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2944 | fn airBitcast(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2846 | 2945 | 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, &.{}); |
| 2848 | 2950 | } |
| 2849 | 2951 | |
| 2850 | | fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2952 | fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2851 | 2953 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 2852 | 2954 | 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 | |
| 2853 | 2957 | const struct_ptr = try self.resolveInst(extra.data.struct_operand); |
| 2854 | 2958 | const struct_ty = self.air.typeOf(extra.data.struct_operand).childType(); |
| 2855 | 2959 | 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 { |
| 2858 | 2962 | struct_ty.structFieldType(extra.data.field_index).fmt(module), |
| 2859 | 2963 | }); |
| 2860 | 2964 | }; |
| 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}); |
| 2862 | 2967 | } |
| 2863 | 2968 | |
| 2864 | | fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u32) InnerError!WValue { |
| 2969 | fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u32) InnerError!void { |
| 2865 | 2970 | 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}); |
| 2866 | 2972 | const struct_ptr = try self.resolveInst(ty_op.operand); |
| 2867 | 2973 | const struct_ty = self.air.typeOf(ty_op.operand).childType(); |
| 2868 | 2974 | const field_ty = struct_ty.structFieldType(index); |
| ... | ... | @@ -2872,7 +2978,8 @@ fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u32) InnerEr |
| 2872 | 2978 | field_ty.fmt(module), |
| 2873 | 2979 | }); |
| 2874 | 2980 | }; |
| 2875 | | return self.structFieldPtr(struct_ptr, offset); |
| 2981 | const result = try self.structFieldPtr(struct_ptr, offset); |
| 2982 | self.finishAir(inst, result, &.{ty_op.operand}); |
| 2876 | 2983 | } |
| 2877 | 2984 | |
| 2878 | 2985 | fn 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 |
| 2884 | 2991 | } |
| 2885 | 2992 | } |
| 2886 | 2993 | |
| 2887 | | fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2888 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 2889 | | |
| 2994 | fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2890 | 2995 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 2891 | 2996 | 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 | |
| 2892 | 2999 | const struct_ty = self.air.typeOf(struct_field.struct_operand); |
| 2893 | 3000 | const operand = try self.resolveInst(struct_field.struct_operand); |
| 2894 | 3001 | const field_index = struct_field.field_index; |
| 2895 | 3002 | 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 | |
| 2897 | 3005 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(field_index, self.target)) orelse { |
| 2898 | 3006 | const module = self.bin_file.base.options.module.?; |
| 2899 | 3007 | return self.fail("Field type '{}' too big to fit into stack frame", .{field_ty.fmt(module)}); |
| 2900 | 3008 | }; |
| 2901 | 3009 | |
| 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 | } |
| 2908 | 3018 | } |
| 2909 | | } |
| 2910 | 3019 | |
| 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}); |
| 2913 | 3024 | } |
| 2914 | 3025 | |
| 2915 | | fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3026 | fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2916 | 3027 | // result type is always 'noreturn' |
| 2917 | 3028 | const blocktype = wasm.block_empty; |
| 2918 | 3029 | 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 { |
| 3071 | 3182 | try self.genBody(else_body); |
| 3072 | 3183 | try self.endBlock(); |
| 3073 | 3184 | } |
| 3074 | | return .none; |
| 3185 | self.finishAir(inst, .none, &.{}); |
| 3075 | 3186 | } |
| 3076 | 3187 | |
| 3077 | | fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!WValue { |
| 3188 | fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!void { |
| 3078 | 3189 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 3190 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{un_op}); |
| 3079 | 3191 | const operand = try self.resolveInst(un_op); |
| 3080 | 3192 | const err_union_ty = self.air.typeOf(un_op); |
| 3081 | 3193 | const pl_ty = err_union_ty.errorUnionPayload(); |
| 3082 | 3194 | |
| 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 | } |
| 3088 | 3202 | } |
| 3089 | | } |
| 3090 | 3203 | |
| 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 | } |
| 3098 | 3211 | |
| 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)); |
| 3102 | 3215 | |
| 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}); |
| 3106 | 3221 | } |
| 3107 | 3222 | |
| 3108 | | fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!WValue { |
| 3109 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3223 | fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void { |
| 3110 | 3224 | 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 | |
| 3111 | 3227 | const operand = try self.resolveInst(ty_op.operand); |
| 3112 | 3228 | const op_ty = self.air.typeOf(ty_op.operand); |
| 3113 | 3229 | const err_ty = if (op_is_ptr) op_ty.childType() else op_ty; |
| 3114 | 3230 | const payload_ty = err_ty.errorUnionPayload(); |
| 3115 | 3231 | |
| 3116 | | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) return WValue{ .none = {} }; |
| 3232 | const result = result: { |
| 3233 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) break :result WValue{ .none = {} }; |
| 3117 | 3234 | |
| 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 | } |
| 3122 | 3239 | |
| 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}); |
| 3125 | 3244 | } |
| 3126 | 3245 | |
| 3127 | | fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!WValue { |
| 3128 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3129 | | |
| 3246 | fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void { |
| 3130 | 3247 | 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 | |
| 3131 | 3250 | const operand = try self.resolveInst(ty_op.operand); |
| 3132 | 3251 | const op_ty = self.air.typeOf(ty_op.operand); |
| 3133 | 3252 | const err_ty = if (op_is_ptr) op_ty.childType() else op_ty; |
| 3134 | 3253 | const payload_ty = err_ty.errorUnionPayload(); |
| 3135 | 3254 | |
| 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 | } |
| 3139 | 3259 | |
| 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 | } |
| 3143 | 3263 | |
| 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}); |
| 3146 | 3268 | } |
| 3147 | 3269 | |
| 3148 | | fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3149 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3150 | | |
| 3270 | fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3151 | 3271 | 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 | |
| 3152 | 3274 | const operand = try self.resolveInst(ty_op.operand); |
| 3153 | 3275 | const err_ty = self.air.typeOfIndex(inst); |
| 3154 | 3276 | |
| 3155 | 3277 | 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 | } |
| 3163 | 3282 | |
| 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); |
| 3169 | 3286 | |
| 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}); |
| 3171 | 3295 | } |
| 3172 | 3296 | |
| 3173 | | fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3174 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3175 | | |
| 3297 | fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3176 | 3298 | 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 | |
| 3177 | 3301 | const operand = try self.resolveInst(ty_op.operand); |
| 3178 | 3302 | const err_ty = self.air.getRefType(ty_op.ty); |
| 3179 | 3303 | const pl_ty = err_ty.errorUnionPayload(); |
| 3180 | 3304 | |
| 3181 | | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3182 | | return operand; |
| 3183 | | } |
| 3305 | const result = result: { |
| 3306 | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3307 | break :result operand; |
| 3308 | } |
| 3184 | 3309 | |
| 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))); |
| 3188 | 3313 | |
| 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 }); |
| 3193 | 3318 | |
| 3194 | | return err_union; |
| 3319 | break :result err_union; |
| 3320 | }; |
| 3321 | self.finishAir(inst, result, &.{ty_op.operand}); |
| 3195 | 3322 | } |
| 3196 | 3323 | |
| 3197 | | fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3198 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3199 | | |
| 3324 | fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3200 | 3325 | 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 | |
| 3201 | 3328 | const ty = self.air.getRefType(ty_op.ty); |
| 3202 | 3329 | const operand = try self.resolveInst(ty_op.operand); |
| 3203 | 3330 | const operand_ty = self.air.typeOf(ty_op.operand); |
| ... | ... | @@ -3208,7 +3335,8 @@ fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3208 | 3335 | return self.fail("todo Wasm intcast for bitsize > 128", .{}); |
| 3209 | 3336 | } |
| 3210 | 3337 | |
| 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}); |
| 3212 | 3340 | } |
| 3213 | 3341 | |
| 3214 | 3342 | /// 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 |
| 3263 | 3391 | return WValue{ .stack = {} }; |
| 3264 | 3392 | } |
| 3265 | 3393 | |
| 3266 | | fn airIsNull(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: enum { value, ptr }) InnerError!WValue { |
| 3394 | fn airIsNull(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: enum { value, ptr }) InnerError!void { |
| 3267 | 3395 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 3396 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{un_op}); |
| 3268 | 3397 | const operand = try self.resolveInst(un_op); |
| 3269 | 3398 | |
| 3270 | 3399 | const op_ty = self.air.typeOf(un_op); |
| 3271 | 3400 | const optional_ty = if (op_kind == .ptr) op_ty.childType() else op_ty; |
| 3272 | 3401 | 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}); |
| 3274 | 3404 | } |
| 3275 | 3405 | |
| 3276 | 3406 | /// 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) |
| 3294 | 3424 | return WValue{ .stack = {} }; |
| 3295 | 3425 | } |
| 3296 | 3426 | |
| 3297 | | fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3298 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3427 | fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3299 | 3428 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 3300 | | const operand = try self.resolveInst(ty_op.operand); |
| 3301 | 3429 | const opt_ty = self.air.typeOf(ty_op.operand); |
| 3302 | 3430 | 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 | } |
| 3305 | 3434 | |
| 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; |
| 3307 | 3438 | |
| 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); |
| 3311 | 3440 | |
| 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 | } |
| 3315 | 3444 | |
| 3316 | | fn 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 | } |
| 3318 | 3450 | |
| 3451 | fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3319 | 3452 | 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}); |
| 3320 | 3454 | const operand = try self.resolveInst(ty_op.operand); |
| 3321 | 3455 | const opt_ty = self.air.typeOf(ty_op.operand).childType(); |
| 3322 | 3456 | |
| 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 | } |
| 3328 | 3463 | |
| 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}); |
| 3331 | 3468 | } |
| 3332 | 3469 | |
| 3333 | | fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3470 | fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3334 | 3471 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 3335 | 3472 | const operand = try self.resolveInst(ty_op.operand); |
| 3336 | 3473 | const opt_ty = self.air.typeOf(ty_op.operand).childType(); |
| ... | ... | @@ -3341,7 +3478,7 @@ fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue |
| 3341 | 3478 | } |
| 3342 | 3479 | |
| 3343 | 3480 | if (opt_ty.optionalReprIsPayload()) { |
| 3344 | | return operand; |
| 3481 | return self.finishAir(inst, operand, &.{ty_op.operand}); |
| 3345 | 3482 | } |
| 3346 | 3483 | |
| 3347 | 3484 | 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 |
| 3353 | 3490 | try self.addImm32(1); |
| 3354 | 3491 | try self.addMemArg(.i32_store8, .{ .offset = operand.offset(), .alignment = 1 }); |
| 3355 | 3492 | |
| 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}); |
| 3357 | 3495 | } |
| 3358 | 3496 | |
| 3359 | | fn airWrapOptional(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3360 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3361 | | |
| 3497 | fn airWrapOptional(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3362 | 3498 | 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}); |
| 3363 | 3500 | 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 | | } |
| 3371 | 3501 | |
| 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 | } |
| 3381 | 3510 | |
| 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 | }; |
| 3387 | 3520 | |
| 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 }); |
| 3390 | 3526 | |
| 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 | }; |
| 3393 | 3531 | |
| 3394 | | fn 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 | } |
| 3396 | 3534 | |
| 3535 | fn airSlice(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3397 | 3536 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 3398 | 3537 | 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 | |
| 3399 | 3540 | const lhs = try self.resolveInst(bin_op.lhs); |
| 3400 | 3541 | const rhs = try self.resolveInst(bin_op.rhs); |
| 3401 | 3542 | const slice_ty = self.air.typeOfIndex(inst); |
| ... | ... | @@ -3404,23 +3545,23 @@ fn airSlice(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3404 | 3545 | try self.store(slice, lhs, Type.usize, 0); |
| 3405 | 3546 | try self.store(slice, rhs, Type.usize, self.ptrSize()); |
| 3406 | 3547 | |
| 3407 | | return slice; |
| 3548 | self.finishAir(inst, slice, &.{ bin_op.lhs, bin_op.rhs }); |
| 3408 | 3549 | } |
| 3409 | 3550 | |
| 3410 | | fn airSliceLen(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3411 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3412 | | |
| 3551 | fn airSliceLen(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3413 | 3552 | 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}); |
| 3415 | 3554 | |
| 3555 | const operand = try self.resolveInst(ty_op.operand); |
| 3416 | 3556 | 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}); |
| 3418 | 3559 | } |
| 3419 | 3560 | |
| 3420 | | fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3421 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3422 | | |
| 3561 | fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3423 | 3562 | 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 | |
| 3424 | 3565 | const slice_ty = self.air.typeOf(bin_op.lhs); |
| 3425 | 3566 | const slice = try self.resolveInst(bin_op.lhs); |
| 3426 | 3567 | const index = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -3436,21 +3577,22 @@ fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3436 | 3577 | try self.addTag(.i32_mul); |
| 3437 | 3578 | try self.addTag(.i32_add); |
| 3438 | 3579 | |
| 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); |
| 3441 | 3582 | |
| 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; |
| 3445 | 3587 | |
| 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 }); |
| 3448 | 3589 | } |
| 3449 | 3590 | |
| 3450 | | fn airSliceElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3451 | | if (self.liveness.isUnused(inst)) return WValue.none; |
| 3591 | fn airSliceElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3452 | 3592 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 3453 | 3593 | 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 | |
| 3454 | 3596 | const elem_ty = self.air.getRefType(ty_pl.ty).childType(); |
| 3455 | 3597 | const elem_size = elem_ty.abiSize(self.target); |
| 3456 | 3598 | |
| ... | ... | @@ -3467,20 +3609,22 @@ fn airSliceElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3467 | 3609 | |
| 3468 | 3610 | const result = try self.allocLocal(Type.i32); |
| 3469 | 3611 | try self.addLabel(.local_set, result.local); |
| 3470 | | return result; |
| 3612 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 3471 | 3613 | } |
| 3472 | 3614 | |
| 3473 | | fn airSlicePtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3474 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3615 | fn airSlicePtr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3475 | 3616 | 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}); |
| 3476 | 3618 | const operand = try self.resolveInst(ty_op.operand); |
| 3477 | 3619 | 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}); |
| 3479 | 3622 | } |
| 3480 | 3623 | |
| 3481 | | fn airTrunc(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3482 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3624 | fn airTrunc(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3483 | 3625 | 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 | |
| 3484 | 3628 | const operand = try self.resolveInst(ty_op.operand); |
| 3485 | 3629 | const wanted_ty = self.air.getRefType(ty_op.ty); |
| 3486 | 3630 | const op_ty = self.air.typeOf(ty_op.operand); |
| ... | ... | @@ -3496,16 +3640,24 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3496 | 3640 | if (wasm_bits != wanted_bits) { |
| 3497 | 3641 | result = try self.wrapOperand(result, wanted_ty); |
| 3498 | 3642 | } |
| 3499 | | return result.toLocal(self, wanted_ty); |
| 3643 | |
| 3644 | self.finishAir(inst, try result.toLocal(self, wanted_ty), &.{ty_op.operand}); |
| 3500 | 3645 | } |
| 3501 | 3646 | |
| 3502 | | fn airBoolToInt(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3647 | fn airBoolToInt(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3503 | 3648 | 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}); |
| 3505 | 3655 | } |
| 3506 | 3656 | |
| 3507 | | fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3657 | fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3508 | 3658 | 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 | |
| 3509 | 3661 | const operand = try self.resolveInst(ty_op.operand); |
| 3510 | 3662 | const array_ty = self.air.typeOf(ty_op.operand).childType(); |
| 3511 | 3663 | const slice_ty = self.air.getRefType(ty_op.ty); |
| ... | ... | @@ -3522,25 +3674,26 @@ fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3522 | 3674 | const len = WValue{ .imm32 = @intCast(u32, array_ty.arrayLen()) }; |
| 3523 | 3675 | try self.store(slice_local, len, Type.usize, self.ptrSize()); |
| 3524 | 3676 | |
| 3525 | | return slice_local; |
| 3677 | self.finishAir(inst, slice_local, &.{ty_op.operand}); |
| 3526 | 3678 | } |
| 3527 | 3679 | |
| 3528 | | fn airPtrToInt(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3529 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3680 | fn airPtrToInt(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3530 | 3681 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 3682 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{un_op}); |
| 3531 | 3683 | const operand = try self.resolveInst(un_op); |
| 3532 | 3684 | |
| 3533 | | switch (operand) { |
| 3685 | const result = switch (operand) { |
| 3534 | 3686 | // 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}); |
| 3538 | 3691 | } |
| 3539 | 3692 | |
| 3540 | | fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3541 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3542 | | |
| 3693 | fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3543 | 3694 | 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 | |
| 3544 | 3697 | const ptr_ty = self.air.typeOf(bin_op.lhs); |
| 3545 | 3698 | const ptr = try self.resolveInst(bin_op.lhs); |
| 3546 | 3699 | const index = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -3560,21 +3713,25 @@ fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3560 | 3713 | try self.addTag(.i32_mul); |
| 3561 | 3714 | try self.addTag(.i32_add); |
| 3562 | 3715 | |
| 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 |
| 3569 | 3723 | |
| 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 }); |
| 3572 | 3728 | } |
| 3573 | 3729 | |
| 3574 | | fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3575 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3730 | fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3576 | 3731 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 3577 | 3732 | 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 | |
| 3578 | 3735 | const ptr_ty = self.air.typeOf(bin_op.lhs); |
| 3579 | 3736 | const elem_ty = self.air.getRefType(ty_pl.ty).childType(); |
| 3580 | 3737 | const elem_size = elem_ty.abiSize(self.target); |
| ... | ... | @@ -3597,13 +3754,14 @@ fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3597 | 3754 | |
| 3598 | 3755 | const result = try self.allocLocal(Type.i32); |
| 3599 | 3756 | try self.addLabel(.local_set, result.local); |
| 3600 | | return result; |
| 3757 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 3601 | 3758 | } |
| 3602 | 3759 | |
| 3603 | | fn airPtrBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { |
| 3604 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3760 | fn airPtrBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 3605 | 3761 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 3606 | 3762 | 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 | |
| 3607 | 3765 | const ptr = try self.resolveInst(bin_op.lhs); |
| 3608 | 3766 | const offset = try self.resolveInst(bin_op.rhs); |
| 3609 | 3767 | 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 { |
| 3624 | 3782 | |
| 3625 | 3783 | const result = try self.allocLocal(Type.usize); |
| 3626 | 3784 | try self.addLabel(.local_set, result.local); |
| 3627 | | return result; |
| 3785 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 3628 | 3786 | } |
| 3629 | 3787 | |
| 3630 | | fn airMemset(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3788 | fn airMemset(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3631 | 3789 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 3632 | 3790 | const bin_op = self.air.extraData(Air.Bin, pl_op.payload).data; |
| 3633 | 3791 | |
| ... | ... | @@ -3636,7 +3794,7 @@ fn airMemset(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3636 | 3794 | const len = try self.resolveInst(bin_op.rhs); |
| 3637 | 3795 | try self.memset(ptr, len, value); |
| 3638 | 3796 | |
| 3639 | | return WValue{ .none = {} }; |
| 3797 | self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 3640 | 3798 | } |
| 3641 | 3799 | |
| 3642 | 3800 | /// 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 |
| 3724 | 3882 | } |
| 3725 | 3883 | } |
| 3726 | 3884 | |
| 3727 | | fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3728 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3729 | | |
| 3885 | fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3730 | 3886 | 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 | |
| 3731 | 3889 | const array_ty = self.air.typeOf(bin_op.lhs); |
| 3732 | 3890 | const array = try self.resolveInst(bin_op.lhs); |
| 3733 | 3891 | const index = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -3740,22 +3898,26 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3740 | 3898 | try self.addTag(.i32_mul); |
| 3741 | 3899 | try self.addTag(.i32_add); |
| 3742 | 3900 | |
| 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); |
| 3745 | 3904 | |
| 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 |
| 3750 | 3909 | |
| 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 | }; |
| 3754 | 3913 | |
| 3755 | | fn 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 | } |
| 3757 | 3916 | |
| 3917 | fn airFloatToInt(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3758 | 3918 | 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 | |
| 3759 | 3921 | const operand = try self.resolveInst(ty_op.operand); |
| 3760 | 3922 | const dest_ty = self.air.typeOfIndex(inst); |
| 3761 | 3923 | const op_ty = self.air.typeOf(ty_op.operand); |
| ... | ... | @@ -3773,13 +3935,14 @@ fn airFloatToInt(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3773 | 3935 | }); |
| 3774 | 3936 | try self.addTag(Mir.Inst.Tag.fromOpcode(op)); |
| 3775 | 3937 | 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}); |
| 3777 | 3940 | } |
| 3778 | 3941 | |
| 3779 | | fn airIntToFloat(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3780 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3781 | | |
| 3942 | fn airIntToFloat(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3782 | 3943 | 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 | |
| 3783 | 3946 | const operand = try self.resolveInst(ty_op.operand); |
| 3784 | 3947 | const dest_ty = self.air.typeOfIndex(inst); |
| 3785 | 3948 | const op_ty = self.air.typeOf(ty_op.operand); |
| ... | ... | @@ -3799,12 +3962,10 @@ fn airIntToFloat(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3799 | 3962 | |
| 3800 | 3963 | const result = try self.allocLocal(dest_ty); |
| 3801 | 3964 | try self.addLabel(.local_set, result.local); |
| 3802 | | return result; |
| 3965 | self.finishAir(inst, result, &.{ty_op.operand}); |
| 3803 | 3966 | } |
| 3804 | 3967 | |
| 3805 | | fn airSplat(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3806 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3807 | | |
| 3968 | fn airSplat(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3808 | 3969 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 3809 | 3970 | const operand = try self.resolveInst(ty_op.operand); |
| 3810 | 3971 | |
| ... | ... | @@ -3812,9 +3973,7 @@ fn airSplat(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3812 | 3973 | return self.fail("TODO: Implement wasm airSplat", .{}); |
| 3813 | 3974 | } |
| 3814 | 3975 | |
| 3815 | | fn airSelect(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3816 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3817 | | |
| 3976 | fn airSelect(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3818 | 3977 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 3819 | 3978 | const operand = try self.resolveInst(pl_op.operand); |
| 3820 | 3979 | |
| ... | ... | @@ -3822,9 +3981,7 @@ fn airSelect(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3822 | 3981 | return self.fail("TODO: Implement wasm airSelect", .{}); |
| 3823 | 3982 | } |
| 3824 | 3983 | |
| 3825 | | fn airShuffle(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3826 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3827 | | |
| 3984 | fn airShuffle(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3828 | 3985 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 3829 | 3986 | const operand = try self.resolveInst(ty_op.operand); |
| 3830 | 3987 | |
| ... | ... | @@ -3832,9 +3989,7 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3832 | 3989 | return self.fail("TODO: Implement wasm airShuffle", .{}); |
| 3833 | 3990 | } |
| 3834 | 3991 | |
| 3835 | | fn airReduce(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3836 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3837 | | |
| 3992 | fn airReduce(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3838 | 3993 | const reduce = self.air.instructions.items(.data)[inst].reduce; |
| 3839 | 3994 | const operand = try self.resolveInst(reduce.operand); |
| 3840 | 3995 | |
| ... | ... | @@ -3842,126 +3997,130 @@ fn airReduce(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3842 | 3997 | return self.fail("TODO: Implement wasm airReduce", .{}); |
| 3843 | 3998 | } |
| 3844 | 3999 | |
| 3845 | | fn airAggregateInit(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3846 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3847 | | |
| 4000 | fn airAggregateInit(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3848 | 4001 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 3849 | 4002 | const result_ty = self.air.typeOfIndex(inst); |
| 3850 | 4003 | const len = @intCast(usize, result_ty.arrayLen()); |
| 3851 | 4004 | const elements = @ptrCast([]const Air.Inst.Ref, self.air.extra[ty_pl.payload..][0..len]); |
| 3852 | 4005 | |
| 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)); |
| 3870 | 4013 | |
| 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; |
| 3873 | 4035 | } |
| 3874 | 4036 | } |
| 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; |
| 3890 | 4044 | |
| 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); |
| 3895 | 4049 | |
| 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 | } |
| 3898 | 4053 | } |
| 3899 | | } |
| 3900 | 4054 | |
| 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, &.{}); |
| 3905 | 4062 | } |
| 3906 | 4063 | |
| 3907 | | fn airUnionInit(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3908 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3909 | | |
| 4064 | fn airUnionInit(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3910 | 4065 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 3911 | 4066 | 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 }; |
| 3917 | 4078 | } |
| 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)); |
| 3922 | 4080 | |
| 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]; |
| 3928 | 4086 | |
| 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 | }; |
| 3935 | 4095 | |
| 3936 | | return result_ptr; |
| 4096 | self.finishAir(inst, result, &.{extra.init}); |
| 3937 | 4097 | } |
| 3938 | 4098 | |
| 3939 | | fn airPrefetch(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4099 | fn airPrefetch(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3940 | 4100 | const prefetch = self.air.instructions.items(.data)[inst].prefetch; |
| 3941 | | _ = prefetch; |
| 3942 | | return WValue{ .none = {} }; |
| 4101 | self.finishAir(inst, .none, &.{prefetch.ptr}); |
| 3943 | 4102 | } |
| 3944 | 4103 | |
| 3945 | | fn airWasmMemorySize(self: *Self, inst: Air.Inst.Index) !WValue { |
| 3946 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3947 | | |
| 4104 | fn airWasmMemorySize(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3948 | 4105 | 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}); |
| 3949 | 4107 | |
| 3950 | 4108 | const result = try self.allocLocal(self.air.typeOfIndex(inst)); |
| 3951 | 4109 | try self.addLabel(.memory_size, pl_op.payload); |
| 3952 | 4110 | try self.addLabel(.local_set, result.local); |
| 3953 | | return result; |
| 4111 | self.finishAir(inst, result, &.{pl_op.operand}); |
| 3954 | 4112 | } |
| 3955 | 4113 | |
| 3956 | | fn airWasmMemoryGrow(self: *Self, inst: Air.Inst.Index) !WValue { |
| 4114 | fn airWasmMemoryGrow(self: *Self, inst: Air.Inst.Index) !void { |
| 3957 | 4115 | 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}); |
| 3959 | 4117 | |
| 4118 | const operand = try self.resolveInst(pl_op.operand); |
| 3960 | 4119 | const result = try self.allocLocal(self.air.typeOfIndex(inst)); |
| 3961 | 4120 | try self.emitWValue(operand); |
| 3962 | 4121 | try self.addLabel(.memory_grow, pl_op.payload); |
| 3963 | 4122 | try self.addLabel(.local_set, result.local); |
| 3964 | | return result; |
| 4123 | self.finishAir(inst, result, &.{pl_op.operand}); |
| 3965 | 4124 | } |
| 3966 | 4125 | |
| 3967 | 4126 | fn 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 |
| 4042 | 4201 | return WValue{ .stack = {} }; |
| 4043 | 4202 | } |
| 4044 | 4203 | |
| 4045 | | fn airSetUnionTag(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4204 | fn airSetUnionTag(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4046 | 4205 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 4047 | 4206 | const un_ty = self.air.typeOf(bin_op.lhs).childType(); |
| 4048 | 4207 | const tag_ty = self.air.typeOf(bin_op.rhs); |
| 4049 | 4208 | 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 | |
| 4051 | 4211 | const union_ptr = try self.resolveInst(bin_op.lhs); |
| 4052 | 4212 | const new_tag = try self.resolveInst(bin_op.rhs); |
| 4053 | 4213 | if (layout.payload_size == 0) { |
| 4054 | 4214 | 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 }); |
| 4056 | 4216 | } |
| 4057 | 4217 | |
| 4058 | 4218 | // 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 { |
| 4061 | 4221 | break :blk @intCast(u32, layout.payload_size); |
| 4062 | 4222 | } else @as(u32, 0); |
| 4063 | 4223 | 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 }); |
| 4065 | 4225 | } |
| 4066 | 4226 | |
| 4067 | | fn airGetUnionTag(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4068 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 4069 | | |
| 4227 | fn airGetUnionTag(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4070 | 4228 | 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 | |
| 4071 | 4231 | const un_ty = self.air.typeOf(ty_op.operand); |
| 4072 | 4232 | const tag_ty = self.air.typeOfIndex(inst); |
| 4073 | 4233 | 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}); |
| 4076 | 4235 | |
| 4236 | const operand = try self.resolveInst(ty_op.operand); |
| 4077 | 4237 | // when the tag alignment is smaller than the payload, the field will be stored |
| 4078 | 4238 | // after the payload. |
| 4079 | 4239 | const offset = if (layout.tag_align < layout.payload_align) blk: { |
| 4080 | 4240 | break :blk @intCast(u32, layout.payload_size); |
| 4081 | 4241 | } else @as(u32, 0); |
| 4082 | 4242 | 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}); |
| 4084 | 4245 | } |
| 4085 | 4246 | |
| 4086 | | fn airFpext(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4087 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 4088 | | |
| 4247 | fn airFpext(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4089 | 4248 | 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 | |
| 4090 | 4251 | const dest_ty = self.air.typeOfIndex(inst); |
| 4091 | 4252 | const operand = try self.resolveInst(ty_op.operand); |
| 4092 | | |
| 4093 | 4253 | 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}); |
| 4095 | 4256 | } |
| 4096 | 4257 | |
| 4097 | 4258 | /// 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 |
| 4127 | 4288 | } |
| 4128 | 4289 | } |
| 4129 | 4290 | |
| 4130 | | fn airFptrunc(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4131 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 4132 | | |
| 4291 | fn airFptrunc(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4133 | 4292 | 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 | |
| 4134 | 4295 | const dest_ty = self.air.typeOfIndex(inst); |
| 4135 | 4296 | const operand = try self.resolveInst(ty_op.operand); |
| 4136 | 4297 | 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}); |
| 4138 | 4300 | } |
| 4139 | 4301 | |
| 4140 | 4302 | /// 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 |
| 4162 | 4324 | } |
| 4163 | 4325 | } |
| 4164 | 4326 | |
| 4165 | | fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4327 | fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4166 | 4328 | 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 | |
| 4167 | 4331 | const err_set_ty = self.air.typeOf(ty_op.operand).childType(); |
| 4168 | 4332 | const payload_ty = err_set_ty.errorUnionPayload(); |
| 4169 | 4333 | const operand = try self.resolveInst(ty_op.operand); |
| ... | ... | @@ -4176,50 +4340,54 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue |
| 4176 | 4340 | @intCast(u32, errUnionErrorOffset(payload_ty, self.target)), |
| 4177 | 4341 | ); |
| 4178 | 4342 | |
| 4179 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 4343 | const result = result: { |
| 4344 | if (self.liveness.isUnused(inst)) break :result WValue{ .none = {} }; |
| 4180 | 4345 | |
| 4181 | | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 4182 | | return operand; |
| 4183 | | } |
| 4346 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 4347 | break :result operand; |
| 4348 | } |
| 4184 | 4349 | |
| 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}); |
| 4186 | 4353 | } |
| 4187 | 4354 | |
| 4188 | | fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4189 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 4190 | | |
| 4355 | fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4191 | 4356 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 4192 | 4357 | 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}); |
| 4194 | 4359 | |
| 4360 | const field_ptr = try self.resolveInst(extra.field_ptr); |
| 4195 | 4361 | const struct_ty = self.air.getRefType(ty_pl.ty).childType(); |
| 4196 | 4362 | const field_offset = struct_ty.structFieldOffset(extra.field_index, self.target); |
| 4197 | 4363 | |
| 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; |
| 4201 | 4372 | |
| 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}); |
| 4208 | 4374 | } |
| 4209 | 4375 | |
| 4210 | | fn airMemcpy(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4376 | fn airMemcpy(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4211 | 4377 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 4212 | 4378 | const bin_op = self.air.extraData(Air.Bin, pl_op.payload).data; |
| 4213 | 4379 | const dst = try self.resolveInst(pl_op.operand); |
| 4214 | 4380 | const src = try self.resolveInst(bin_op.lhs); |
| 4215 | 4381 | const len = try self.resolveInst(bin_op.rhs); |
| 4216 | 4382 | 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 }); |
| 4218 | 4385 | } |
| 4219 | 4386 | |
| 4220 | | fn airPopcount(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4221 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 4387 | fn airPopcount(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4222 | 4388 | 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 | |
| 4223 | 4391 | const operand = try self.resolveInst(ty_op.operand); |
| 4224 | 4392 | const op_ty = self.air.typeOf(ty_op.operand); |
| 4225 | 4393 | const result_ty = self.air.typeOfIndex(inst); |
| ... | ... | @@ -4258,15 +4426,14 @@ fn airPopcount(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4258 | 4426 | |
| 4259 | 4427 | const result = try self.allocLocal(result_ty); |
| 4260 | 4428 | try self.addLabel(.local_set, result.local); |
| 4261 | | return result; |
| 4429 | self.finishAir(inst, result, &.{ty_op.operand}); |
| 4262 | 4430 | } |
| 4263 | 4431 | |
| 4264 | | fn airErrorName(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4265 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 4266 | | |
| 4432 | fn airErrorName(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4267 | 4433 | 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}); |
| 4269 | 4435 | |
| 4436 | const operand = try self.resolveInst(un_op); |
| 4270 | 4437 | // First retrieve the symbol index to the error name table |
| 4271 | 4438 | // that will be used to emit a relocation for the pointer |
| 4272 | 4439 | // to the error name table. |
| ... | ... | @@ -4301,21 +4468,23 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4301 | 4468 | |
| 4302 | 4469 | const result_ptr = try self.allocLocal(Type.usize); |
| 4303 | 4470 | try self.addLabel(.local_set, result_ptr.local); |
| 4304 | | return result_ptr; |
| 4471 | self.finishAir(inst, result_ptr, &.{un_op}); |
| 4305 | 4472 | } |
| 4306 | 4473 | |
| 4307 | | fn airPtrSliceFieldPtr(self: *Self, inst: Air.Inst.Index, offset: u32) InnerError!WValue { |
| 4308 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 4309 | | |
| 4474 | fn airPtrSliceFieldPtr(self: *Self, inst: Air.Inst.Index, offset: u32) InnerError!void { |
| 4310 | 4475 | 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}); |
| 4311 | 4477 | 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}); |
| 4313 | 4480 | } |
| 4314 | 4481 | |
| 4315 | | fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { |
| 4482 | fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 4316 | 4483 | assert(op == .add or op == .sub); |
| 4317 | 4484 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 4318 | 4485 | 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 | |
| 4319 | 4488 | const lhs_op = try self.resolveInst(extra.lhs); |
| 4320 | 4489 | const rhs_op = try self.resolveInst(extra.rhs); |
| 4321 | 4490 | const lhs_ty = self.air.typeOf(extra.lhs); |
| ... | ... | @@ -4331,7 +4500,8 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!W |
| 4331 | 4500 | }; |
| 4332 | 4501 | |
| 4333 | 4502 | 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 }); |
| 4335 | 4505 | } |
| 4336 | 4506 | |
| 4337 | 4507 | const zero = switch (wasm_bits) { |
| ... | ... | @@ -4349,6 +4519,15 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!W |
| 4349 | 4519 | break :blk try (try self.signAbsValue(rhs_op, lhs_ty)).toLocal(self, lhs_ty); |
| 4350 | 4520 | } else rhs_op; |
| 4351 | 4521 | |
| 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 | |
| 4352 | 4531 | var bin_op = try (try self.binOp(lhs, rhs, lhs_ty, op)).toLocal(self, lhs_ty); |
| 4353 | 4532 | defer bin_op.free(self); |
| 4354 | 4533 | 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 |
| 4377 | 4556 | const offset = @intCast(u32, lhs_ty.abiSize(self.target)); |
| 4378 | 4557 | try self.store(result_ptr, overflow_local, Type.initTag(.u1), offset); |
| 4379 | 4558 | |
| 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 }); |
| 4390 | 4560 | } |
| 4391 | 4561 | |
| 4392 | | fn airAddSubWithOverflowBigInt(self: *Self, lhs: WValue, rhs: WValue, ty: Type, result_ty: Type, op: Op) InnerError!WValue { |
| 4562 | fn addSubWithOverflowBigInt(self: *Self, lhs: WValue, rhs: WValue, ty: Type, result_ty: Type, op: Op) InnerError!WValue { |
| 4393 | 4563 | assert(op == .add or op == .sub); |
| 4394 | 4564 | const int_info = ty.intInfo(self.target); |
| 4395 | 4565 | const is_signed = int_info.signedness == .signed; |
| ... | ... | @@ -4453,9 +4623,11 @@ fn airAddSubWithOverflowBigInt(self: *Self, lhs: WValue, rhs: WValue, ty: Type, |
| 4453 | 4623 | return result_ptr; |
| 4454 | 4624 | } |
| 4455 | 4625 | |
| 4456 | | fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4626 | fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4457 | 4627 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 4458 | 4628 | 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 | |
| 4459 | 4631 | const lhs = try self.resolveInst(extra.lhs); |
| 4460 | 4632 | const rhs = try self.resolveInst(extra.rhs); |
| 4461 | 4633 | const lhs_ty = self.air.typeOf(extra.lhs); |
| ... | ... | @@ -4496,12 +4668,14 @@ fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4496 | 4668 | const offset = @intCast(u32, lhs_ty.abiSize(self.target)); |
| 4497 | 4669 | try self.store(result_ptr, overflow_local, Type.initTag(.u1), offset); |
| 4498 | 4670 | |
| 4499 | | return result_ptr; |
| 4671 | self.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); |
| 4500 | 4672 | } |
| 4501 | 4673 | |
| 4502 | | fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4674 | fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4503 | 4675 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 4504 | 4676 | 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 | |
| 4505 | 4679 | const lhs = try self.resolveInst(extra.lhs); |
| 4506 | 4680 | const rhs = try self.resolveInst(extra.rhs); |
| 4507 | 4681 | const lhs_ty = self.air.typeOf(extra.lhs); |
| ... | ... | @@ -4581,12 +4755,13 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4581 | 4755 | const offset = @intCast(u32, lhs_ty.abiSize(self.target)); |
| 4582 | 4756 | try self.store(result_ptr, overflow_bit, Type.initTag(.u1), offset); |
| 4583 | 4757 | |
| 4584 | | return result_ptr; |
| 4758 | self.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); |
| 4585 | 4759 | } |
| 4586 | 4760 | |
| 4587 | | fn airMaxMin(self: *Self, inst: Air.Inst.Index, op: enum { max, min }) InnerError!WValue { |
| 4588 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 4761 | fn airMaxMin(self: *Self, inst: Air.Inst.Index, op: enum { max, min }) InnerError!void { |
| 4589 | 4762 | 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 | |
| 4590 | 4765 | const ty = self.air.typeOfIndex(inst); |
| 4591 | 4766 | if (ty.zigTypeTag() == .Vector) { |
| 4592 | 4767 | 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 |
| 4611 | 4786 | const result_ty = if (isByRef(ty, self.target)) Type.u32 else ty; |
| 4612 | 4787 | const result = try self.allocLocal(result_ty); |
| 4613 | 4788 | try self.addLabel(.local_set, result.local); |
| 4614 | | return result; |
| 4789 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 4615 | 4790 | } |
| 4616 | 4791 | |
| 4617 | | fn airMulAdd(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4618 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 4792 | fn airMulAdd(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4619 | 4793 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 4620 | 4794 | 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 | |
| 4621 | 4797 | const ty = self.air.typeOfIndex(inst); |
| 4622 | 4798 | if (ty.zigTypeTag() == .Vector) { |
| 4623 | 4799 | return self.fail("TODO: `@mulAdd` for vectors", .{}); |
| ... | ... | @@ -4627,7 +4803,7 @@ fn airMulAdd(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4627 | 4803 | const lhs = try self.resolveInst(bin_op.lhs); |
| 4628 | 4804 | const rhs = try self.resolveInst(bin_op.rhs); |
| 4629 | 4805 | |
| 4630 | | if (ty.floatBits(self.target) == 16) { |
| 4806 | const result = if (ty.floatBits(self.target) == 16) fl_result: { |
| 4631 | 4807 | const rhs_ext = try self.fpext(rhs, ty, Type.f32); |
| 4632 | 4808 | const lhs_ext = try self.fpext(lhs, ty, Type.f32); |
| 4633 | 4809 | const addend_ext = try self.fpext(addend, ty, Type.f32); |
| ... | ... | @@ -4638,16 +4814,19 @@ fn airMulAdd(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4638 | 4814 | Type.f32, |
| 4639 | 4815 | &.{ rhs_ext, lhs_ext, addend_ext }, |
| 4640 | 4816 | ); |
| 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 | }; |
| 4643 | 4822 | |
| 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 }); |
| 4646 | 4824 | } |
| 4647 | 4825 | |
| 4648 | | fn airClz(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4649 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 4826 | fn airClz(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4650 | 4827 | 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 | |
| 4651 | 4830 | const ty = self.air.typeOf(ty_op.operand); |
| 4652 | 4831 | const result_ty = self.air.typeOfIndex(inst); |
| 4653 | 4832 | if (ty.zigTypeTag() == .Vector) { |
| ... | ... | @@ -4694,12 +4873,13 @@ fn airClz(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4694 | 4873 | |
| 4695 | 4874 | const result = try self.allocLocal(result_ty); |
| 4696 | 4875 | try self.addLabel(.local_set, result.local); |
| 4697 | | return result; |
| 4876 | self.finishAir(inst, result, &.{ty_op.operand}); |
| 4698 | 4877 | } |
| 4699 | 4878 | |
| 4700 | | fn airCtz(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4701 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 4879 | fn airCtz(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4702 | 4880 | 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 | |
| 4703 | 4883 | const ty = self.air.typeOf(ty_op.operand); |
| 4704 | 4884 | const result_ty = self.air.typeOfIndex(inst); |
| 4705 | 4885 | |
| ... | ... | @@ -4758,11 +4938,11 @@ fn airCtz(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4758 | 4938 | |
| 4759 | 4939 | const result = try self.allocLocal(result_ty); |
| 4760 | 4940 | try self.addLabel(.local_set, result.local); |
| 4761 | | return result; |
| 4941 | self.finishAir(inst, result, &.{ty_op.operand}); |
| 4762 | 4942 | } |
| 4763 | 4943 | |
| 4764 | | fn airDbgVar(self: *Self, inst: Air.Inst.Index, is_ptr: bool) !WValue { |
| 4765 | | if (self.debug_output != .dwarf) return WValue{ .none = {} }; |
| 4944 | fn airDbgVar(self: *Self, inst: Air.Inst.Index, is_ptr: bool) !void { |
| 4945 | if (self.debug_output != .dwarf) return self.finishAir(inst, .none, &.{}); |
| 4766 | 4946 | |
| 4767 | 4947 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 4768 | 4948 | const ty = self.air.typeOf(pl_op.operand); |
| ... | ... | @@ -4799,11 +4979,11 @@ fn airDbgVar(self: *Self, inst: Air.Inst.Index, is_ptr: bool) !WValue { |
| 4799 | 4979 | try self.addDbgInfoTypeReloc(op_ty); |
| 4800 | 4980 | dbg_info.appendSliceAssumeCapacity(name); |
| 4801 | 4981 | dbg_info.appendAssumeCapacity(0); |
| 4802 | | return WValue{ .none = {} }; |
| 4982 | self.finishAir(inst, .none, &.{}); |
| 4803 | 4983 | } |
| 4804 | 4984 | |
| 4805 | | fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !WValue { |
| 4806 | | if (self.debug_output != .dwarf) return WValue{ .none = {} }; |
| 4985 | fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void { |
| 4986 | if (self.debug_output != .dwarf) return self.finishAir(inst, .none, &.{}); |
| 4807 | 4987 | |
| 4808 | 4988 | const dbg_stmt = self.air.instructions.items(.data)[inst].dbg_stmt; |
| 4809 | 4989 | try self.addInst(.{ .tag = .dbg_line, .data = .{ |
| ... | ... | @@ -4812,25 +4992,27 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !WValue { |
| 4812 | 4992 | .column = dbg_stmt.column, |
| 4813 | 4993 | }), |
| 4814 | 4994 | } }); |
| 4815 | | return WValue{ .none = {} }; |
| 4995 | self.finishAir(inst, .none, &.{}); |
| 4816 | 4996 | } |
| 4817 | 4997 | |
| 4818 | | fn airTry(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4998 | fn airTry(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4819 | 4999 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 4820 | 5000 | const err_union = try self.resolveInst(pl_op.operand); |
| 4821 | 5001 | const extra = self.air.extraData(Air.Try, pl_op.payload); |
| 4822 | 5002 | const body = self.air.extra[extra.end..][0..extra.data.body_len]; |
| 4823 | 5003 | 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}); |
| 4825 | 5006 | } |
| 4826 | 5007 | |
| 4827 | | fn airTryPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 5008 | fn airTryPtr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4828 | 5009 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 4829 | 5010 | const extra = self.air.extraData(Air.TryPtr, ty_pl.payload); |
| 4830 | 5011 | const err_union_ptr = try self.resolveInst(extra.data.ptr); |
| 4831 | 5012 | const body = self.air.extra[extra.end..][0..extra.data.body_len]; |
| 4832 | 5013 | 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}); |
| 4834 | 5016 | } |
| 4835 | 5017 | |
| 4836 | 5018 | fn lowerTry( |
| ... | ... | @@ -4879,12 +5061,10 @@ fn lowerTry( |
| 4879 | 5061 | return payload.toLocal(self, pl_ty); |
| 4880 | 5062 | } |
| 4881 | 5063 | |
| 4882 | | fn airByteSwap(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4883 | | if (self.liveness.isUnused(inst)) { |
| 4884 | | return WValue{ .none = {} }; |
| 4885 | | } |
| 4886 | | |
| 5064 | fn airByteSwap(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4887 | 5065 | 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 | |
| 4888 | 5068 | const ty = self.air.typeOfIndex(inst); |
| 4889 | 5069 | const operand = try self.resolveInst(ty_op.operand); |
| 4890 | 5070 | |
| ... | ... | @@ -4895,84 +5075,89 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4895 | 5075 | |
| 4896 | 5076 | // bytes are no-op |
| 4897 | 5077 | 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}); |
| 4950 | 5133 | } |
| 4951 | 5134 | |
| 4952 | | fn airDiv(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4953 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 4954 | | |
| 5135 | fn airDiv(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4955 | 5136 | 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 | |
| 4956 | 5139 | const ty = self.air.typeOfIndex(inst); |
| 4957 | 5140 | const lhs = try self.resolveInst(bin_op.lhs); |
| 4958 | 5141 | const rhs = try self.resolveInst(bin_op.rhs); |
| 4959 | 5142 | |
| 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 }); |
| 4964 | 5148 | } |
| 4965 | 5149 | |
| 4966 | | fn airDivFloor(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 4967 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 4968 | | |
| 5150 | fn airDivFloor(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4969 | 5151 | 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 | |
| 4970 | 5154 | const ty = self.air.typeOfIndex(inst); |
| 4971 | 5155 | const lhs = try self.resolveInst(bin_op.lhs); |
| 4972 | 5156 | const rhs = try self.resolveInst(bin_op.rhs); |
| 4973 | 5157 | |
| 4974 | 5158 | 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 }); |
| 4976 | 5161 | } else if (ty.isSignedInt()) { |
| 4977 | 5162 | const int_bits = ty.intInfo(self.target).bits; |
| 4978 | 5163 | const wasm_bits = toWasmBits(int_bits) orelse { |
| ... | ... | @@ -5048,7 +5233,7 @@ fn airDivFloor(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 5048 | 5233 | |
| 5049 | 5234 | const result = try self.allocLocal(ty); |
| 5050 | 5235 | try self.addLabel(.local_set, result.local); |
| 5051 | | return result; |
| 5236 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 5052 | 5237 | } |
| 5053 | 5238 | |
| 5054 | 5239 | fn 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 { |
| 5110 | 5295 | return WValue{ .stack = {} }; |
| 5111 | 5296 | } |
| 5112 | 5297 | |
| 5113 | | fn airCeilFloorTrunc(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { |
| 5114 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 5115 | | |
| 5298 | fn airCeilFloorTrunc(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 5116 | 5299 | 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 | |
| 5117 | 5302 | const ty = self.air.typeOfIndex(inst); |
| 5118 | 5303 | const float_bits = ty.floatBits(self.target); |
| 5119 | 5304 | const is_f16 = float_bits == 16; |
| ... | ... | @@ -5139,14 +5324,14 @@ fn airCeilFloorTrunc(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValu |
| 5139 | 5324 | |
| 5140 | 5325 | const result = try self.allocLocal(ty); |
| 5141 | 5326 | try self.addLabel(.local_set, result.local); |
| 5142 | | return result; |
| 5327 | self.finishAir(inst, result, &.{un_op}); |
| 5143 | 5328 | } |
| 5144 | 5329 | |
| 5145 | | fn airSatBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { |
| 5330 | fn airSatBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 5146 | 5331 | assert(op == .add or op == .sub); |
| 5147 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 5148 | | |
| 5149 | 5332 | 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 | |
| 5150 | 5335 | const ty = self.air.typeOfIndex(inst); |
| 5151 | 5336 | const lhs = try self.resolveInst(bin_op.lhs); |
| 5152 | 5337 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -5159,7 +5344,8 @@ fn airSatBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { |
| 5159 | 5344 | } |
| 5160 | 5345 | |
| 5161 | 5346 | 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 }); |
| 5163 | 5349 | } |
| 5164 | 5350 | |
| 5165 | 5351 | const wasm_bits = toWasmBits(int_info.bits).?; |
| ... | ... | @@ -5189,7 +5375,7 @@ fn airSatBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { |
| 5189 | 5375 | try self.addTag(.select); |
| 5190 | 5376 | const result = try self.allocLocal(ty); |
| 5191 | 5377 | try self.addLabel(.local_set, result.local); |
| 5192 | | return result; |
| 5378 | return self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 5193 | 5379 | } |
| 5194 | 5380 | |
| 5195 | 5381 | fn 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 |
| 5255 | 5441 | } |
| 5256 | 5442 | } |
| 5257 | 5443 | |
| 5258 | | fn airShlSat(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 5259 | | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 5260 | | |
| 5444 | fn airShlSat(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 5261 | 5445 | 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 | |
| 5262 | 5448 | const ty = self.air.typeOfIndex(inst); |
| 5263 | 5449 | const int_info = ty.intInfo(self.target); |
| 5264 | 5450 | const is_signed = int_info.signedness == .signed; |
| ... | ... | @@ -5271,7 +5457,7 @@ fn airShlSat(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 5271 | 5457 | const wasm_bits = toWasmBits(int_info.bits).?; |
| 5272 | 5458 | const result = try self.allocLocal(ty); |
| 5273 | 5459 | |
| 5274 | | if (wasm_bits == int_info.bits) { |
| 5460 | if (wasm_bits == int_info.bits) outer_blk: { |
| 5275 | 5461 | var shl = try (try self.binOp(lhs, rhs, ty, .shl)).toLocal(self, ty); |
| 5276 | 5462 | defer shl.free(self); |
| 5277 | 5463 | 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 { |
| 5304 | 5490 | _ = try self.cmp(lhs, shr, ty, .neq); |
| 5305 | 5491 | try self.addTag(.select); |
| 5306 | 5492 | try self.addLabel(.local_set, result.local); |
| 5307 | | return result; |
| 5493 | break :outer_blk; |
| 5308 | 5494 | } else { |
| 5309 | 5495 | const shift_size = wasm_bits - int_info.bits; |
| 5310 | 5496 | const shift_value = switch (wasm_bits) { |
| ... | ... | @@ -5353,8 +5539,10 @@ fn airShlSat(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 5353 | 5539 | if (is_signed) { |
| 5354 | 5540 | shift_result = try self.wrapOperand(shift_result, ty); |
| 5355 | 5541 | } |
| 5356 | | return shift_result.toLocal(self, ty); |
| 5542 | try self.addLabel(.local_set, result.local); |
| 5357 | 5543 | } |
| 5544 | |
| 5545 | return self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 5358 | 5546 | } |
| 5359 | 5547 | |
| 5360 | 5548 | /// Calls a compiler-rt intrinsic by creating an undefined symbol, |