| ... | ... | @@ -1818,18 +1818,103 @@ fn airWrapBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { |
| 1818 | 1818 | return bin_local; |
| 1819 | 1819 | } |
| 1820 | 1820 | |
| 1821 | fn lowerParentPtr(self: *Self, ptr_val: Value, ptr_child_ty: Type) InnerError!WValue { |
| 1822 | switch (ptr_val.tag()) { |
| 1823 | .decl_ref_mut => { |
| 1824 | const decl = ptr_val.castTag(.decl_ref_mut).?.data.decl; |
| 1825 | return self.lowerParentPtrDecl(ptr_val, decl); |
| 1826 | }, |
| 1827 | .decl_ref => { |
| 1828 | const decl = ptr_val.castTag(.decl_ref).?.data; |
| 1829 | return self.lowerParentPtrDecl(ptr_val, decl); |
| 1830 | }, |
| 1831 | .variable => { |
| 1832 | const decl = ptr_val.castTag(.variable).?.data.owner_decl; |
| 1833 | return self.lowerParentPtrDecl(ptr_val, decl); |
| 1834 | }, |
| 1835 | .field_ptr => { |
| 1836 | const field_ptr = ptr_val.castTag(.field_ptr).?.data; |
| 1837 | const parent_ty = field_ptr.container_ty; |
| 1838 | const parent_ptr = try self.lowerParentPtr(field_ptr.container_ptr, parent_ty); |
| 1839 | |
| 1840 | const offset = switch (parent_ty.zigTypeTag()) { |
| 1841 | .Struct => blk: { |
| 1842 | const offset = parent_ty.structFieldOffset(field_ptr.field_index, self.target); |
| 1843 | break :blk offset; |
| 1844 | }, |
| 1845 | .Union => blk: { |
| 1846 | const layout: Module.Union.Layout = parent_ty.unionGetLayout(self.target); |
| 1847 | if (layout.payload_size == 0) break :blk 0; |
| 1848 | if (layout.payload_align > layout.tag_align) break :blk 0; |
| 1849 | |
| 1850 | // tag is stored first so calculate offset from where payload starts |
| 1851 | const offset = @intCast(u32, std.mem.alignForwardGeneric(u64, layout.tag_size, layout.tag_align)); |
| 1852 | break :blk offset; |
| 1853 | }, |
| 1854 | else => unreachable, |
| 1855 | }; |
| 1856 | |
| 1857 | return switch (parent_ptr) { |
| 1858 | .memory => |ptr| WValue{ |
| 1859 | .memory_offset = .{ |
| 1860 | .pointer = ptr, |
| 1861 | .offset = @intCast(u32, offset), |
| 1862 | }, |
| 1863 | }, |
| 1864 | .memory_offset => |mem_off| WValue{ |
| 1865 | .memory_offset = .{ |
| 1866 | .pointer = mem_off.pointer, |
| 1867 | .offset = @intCast(u32, offset) + mem_off.offset, |
| 1868 | }, |
| 1869 | }, |
| 1870 | else => unreachable, |
| 1871 | }; |
| 1872 | }, |
| 1873 | .elem_ptr => { |
| 1874 | const elem_ptr = ptr_val.castTag(.elem_ptr).?.data; |
| 1875 | const index = elem_ptr.index; |
| 1876 | const offset = index * ptr_child_ty.abiSize(self.target); |
| 1877 | const array_ptr = try self.lowerParentPtr(elem_ptr.array_ptr, elem_ptr.elem_ty); |
| 1878 | |
| 1879 | return WValue{ .memory_offset = .{ |
| 1880 | .pointer = array_ptr.memory, |
| 1881 | .offset = @intCast(u32, offset), |
| 1882 | } }; |
| 1883 | }, |
| 1884 | else => |tag| return self.fail("TODO: Implement lowerParentPtr for tag: {}", .{tag}), |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | fn lowerParentPtrDecl(self: *Self, ptr_val: Value, decl: *Module.Decl) InnerError!WValue { |
| 1889 | decl.markAlive(); |
| 1890 | var ptr_ty_payload: Type.Payload.ElemType = .{ |
| 1891 | .base = .{ .tag = .single_mut_pointer }, |
| 1892 | .data = decl.ty, |
| 1893 | }; |
| 1894 | const ptr_ty = Type.initPayload(&ptr_ty_payload.base); |
| 1895 | return self.lowerDeclRefValue(.{ .ty = ptr_ty, .val = ptr_val }, decl); |
| 1896 | } |
| 1897 | |
| 1898 | fn lowerDeclRefValue(self: *Self, tv: TypedValue, decl: *Module.Decl) InnerError!WValue { |
| 1899 | if (tv.ty.isSlice()) { |
| 1900 | return WValue{ .memory = try self.bin_file.lowerUnnamedConst(self.decl, tv) }; |
| 1901 | } else if (decl.ty.zigTypeTag() != .Fn and !decl.ty.hasRuntimeBitsIgnoreComptime()) { |
| 1902 | return WValue{ .imm32 = 0xaaaaaaaa }; |
| 1903 | } |
| 1904 | |
| 1905 | decl.markAlive(); |
| 1906 | const target_sym_index = decl.link.wasm.sym_index; |
| 1907 | if (decl.ty.zigTypeTag() == .Fn) { |
| 1908 | try self.bin_file.addTableFunction(target_sym_index); |
| 1909 | return WValue{ .function_index = target_sym_index }; |
| 1910 | } else return WValue{ .memory = target_sym_index }; |
| 1911 | } |
| 1912 | |
| 1821 | 1913 | fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 1822 | 1914 | if (val.isUndefDeep()) return self.emitUndefined(ty); |
| 1823 | 1915 | if (val.castTag(.decl_ref)) |decl_ref| { |
| 1824 | 1916 | const decl = decl_ref.data; |
| 1825 | | decl.markAlive(); |
| 1826 | | const target_sym_index = decl.link.wasm.sym_index; |
| 1827 | | if (ty.isSlice()) { |
| 1828 | | return WValue{ .memory = try self.bin_file.lowerUnnamedConst(self.decl, .{ .ty = ty, .val = val }) }; |
| 1829 | | } else if (decl.ty.zigTypeTag() == .Fn) { |
| 1830 | | try self.bin_file.addTableFunction(target_sym_index); |
| 1831 | | return WValue{ .function_index = target_sym_index }; |
| 1832 | | } else return WValue{ .memory = target_sym_index }; |
| 1917 | return self.lowerDeclRefValue(.{ .ty = ty, .val = val }, decl); |
| 1833 | 1918 | } |
| 1834 | 1919 | |
| 1835 | 1920 | switch (ty.zigTypeTag()) { |
| ... | ... | @@ -1856,37 +1941,8 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 1856 | 1941 | else => unreachable, |
| 1857 | 1942 | }, |
| 1858 | 1943 | .Pointer => switch (val.tag()) { |
| 1859 | | .elem_ptr => { |
| 1860 | | const elem_ptr = val.castTag(.elem_ptr).?.data; |
| 1861 | | const index = elem_ptr.index; |
| 1862 | | const offset = index * ty.childType().abiSize(self.target); |
| 1863 | | const array_ptr = try self.lowerConstant(elem_ptr.array_ptr, ty); |
| 1864 | | |
| 1865 | | return WValue{ .memory_offset = .{ |
| 1866 | | .pointer = array_ptr.memory, |
| 1867 | | .offset = @intCast(u32, offset), |
| 1868 | | } }; |
| 1869 | | }, |
| 1870 | | .field_ptr => { |
| 1871 | | const field_ptr = val.castTag(.field_ptr).?.data; |
| 1872 | | const container = field_ptr.container_ptr; |
| 1873 | | const parent_ptr = try self.lowerConstant(container, ty); |
| 1874 | | |
| 1875 | | const offset = switch (container.tag()) { |
| 1876 | | .decl_ref => blk: { |
| 1877 | | const decl_ref = container.castTag(.decl_ref).?.data; |
| 1878 | | if (decl_ref.ty.castTag(.@"struct")) |_| { |
| 1879 | | const offset = decl_ref.ty.structFieldOffset(field_ptr.field_index, self.target); |
| 1880 | | break :blk offset; |
| 1881 | | } |
| 1882 | | return self.fail("Wasm TODO: field_ptr decl_ref for type '{}'", .{decl_ref.ty}); |
| 1883 | | }, |
| 1884 | | else => |tag| return self.fail("Wasm TODO: Implement field_ptr for value tag: '{s}'", .{tag}), |
| 1885 | | }; |
| 1886 | | return WValue{ .memory_offset = .{ |
| 1887 | | .pointer = parent_ptr.memory, |
| 1888 | | .offset = @intCast(u32, offset), |
| 1889 | | } }; |
| 1944 | .field_ptr, .elem_ptr => { |
| 1945 | return self.lowerParentPtr(val, ty.childType()); |
| 1890 | 1946 | }, |
| 1891 | 1947 | .int_u64, .one => return WValue{ .imm32 = @intCast(u32, val.toUnsignedInt()) }, |
| 1892 | 1948 | .zero, .null_value => return WValue{ .imm32 = 0 }, |
| ... | ... | @@ -1999,6 +2055,11 @@ fn valueAsI32(self: Self, val: Value, ty: Type) i32 { |
| 1999 | 2055 | return self.valueAsI32(tag_val, enum_full.tag_ty); |
| 2000 | 2056 | } else return @bitCast(i32, field_index.data); |
| 2001 | 2057 | }, |
| 2058 | .enum_numbered => { |
| 2059 | const index = field_index.data; |
| 2060 | const enum_data = ty.castTag(.enum_numbered).?.data; |
| 2061 | return self.valueAsI32(enum_data.values.keys()[index], enum_data.tag_ty); |
| 2062 | }, |
| 2002 | 2063 | else => unreachable, |
| 2003 | 2064 | } |
| 2004 | 2065 | } else { |
| ... | ... | @@ -2124,8 +2185,16 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: std.math.CompareOperator) Inner |
| 2124 | 2185 | return self.cmpBigInt(lhs, rhs, operand_ty, op); |
| 2125 | 2186 | } |
| 2126 | 2187 | |
| 2127 | | try self.emitWValue(lhs); |
| 2128 | | try self.emitWValue(rhs); |
| 2188 | // ensure that when we compare pointers, we emit |
| 2189 | // the true pointer of a stack value, rather than the stack pointer. |
| 2190 | switch (lhs) { |
| 2191 | .stack_offset => try self.emitWValue(try self.buildPointerOffset(lhs, 0, .new)), |
| 2192 | else => try self.emitWValue(lhs), |
| 2193 | } |
| 2194 | switch (rhs) { |
| 2195 | .stack_offset => try self.emitWValue(try self.buildPointerOffset(rhs, 0, .new)), |
| 2196 | else => try self.emitWValue(rhs), |
| 2197 | } |
| 2129 | 2198 | |
| 2130 | 2199 | const signedness: std.builtin.Signedness = blk: { |
| 2131 | 2200 | // by default we tell the operand type is unsigned (i.e. bools and enum values) |
| ... | ... | @@ -3161,16 +3230,16 @@ fn airReduce(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3161 | 3230 | fn airAggregateInit(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3162 | 3231 | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3163 | 3232 | |
| 3164 | | const vector_ty = self.air.typeOfIndex(inst); |
| 3165 | | const len = vector_ty.vectorLen(); |
| 3166 | 3233 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 3234 | const result_ty = self.air.typeOfIndex(inst); |
| 3235 | const len = @intCast(usize, result_ty.arrayLen()); |
| 3167 | 3236 | const elements = @bitCast([]const Air.Inst.Ref, self.air.extra[ty_pl.payload..][0..len]); |
| 3168 | 3237 | |
| 3169 | | switch (vector_ty.zigTypeTag()) { |
| 3238 | switch (result_ty.zigTypeTag()) { |
| 3170 | 3239 | .Vector => return self.fail("TODO: Wasm backend: implement airAggregateInit for vectors", .{}), |
| 3171 | 3240 | .Array => { |
| 3172 | | const result = try self.allocStack(vector_ty); |
| 3173 | | const elem_ty = vector_ty.childType(); |
| 3241 | const result = try self.allocStack(result_ty); |
| 3242 | const elem_ty = result_ty.childType(); |
| 3174 | 3243 | const elem_size = @intCast(u32, elem_ty.abiSize(self.target)); |
| 3175 | 3244 | |
| 3176 | 3245 | // When the element type is by reference, we must copy the entire |
| ... | ... | @@ -3199,13 +3268,12 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3199 | 3268 | return result; |
| 3200 | 3269 | }, |
| 3201 | 3270 | .Struct => { |
| 3202 | | const tuple = vector_ty.castTag(.tuple).?.data; |
| 3203 | | const result = try self.allocStack(vector_ty); |
| 3271 | const result = try self.allocStack(result_ty); |
| 3204 | 3272 | const offset = try self.buildPointerOffset(result, 0, .new); // pointer to offset |
| 3205 | 3273 | for (elements) |elem, elem_index| { |
| 3206 | | if (tuple.values[elem_index].tag() != .unreachable_value) continue; |
| 3274 | if (result_ty.structFieldValueComptime(elem_index) != null) continue; |
| 3207 | 3275 | |
| 3208 | | const elem_ty = tuple.types[elem_index]; |
| 3276 | const elem_ty = result_ty.structFieldType(elem_index); |
| 3209 | 3277 | const elem_size = @intCast(u32, elem_ty.abiSize(self.target)); |
| 3210 | 3278 | const value = try self.resolveInst(elem); |
| 3211 | 3279 | try self.store(offset, value, elem_ty, 0); |
| ... | ... | @@ -3223,7 +3291,34 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3223 | 3291 | |
| 3224 | 3292 | fn airUnionInit(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 3225 | 3293 | if (self.liveness.isUnused(inst)) return WValue{ .none = {} }; |
| 3226 | | return self.fail("TODO: Wasm backend: implement airUnionInit", .{}); |
| 3294 | |
| 3295 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 3296 | const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| 3297 | const union_ty = self.air.typeOfIndex(inst); |
| 3298 | const layout = union_ty.unionGetLayout(self.target); |
| 3299 | if (layout.payload_size == 0) { |
| 3300 | if (layout.tag_size == 0) { |
| 3301 | return WValue{ .none = {} }; |
| 3302 | } |
| 3303 | assert(!isByRef(union_ty, self.target)); |
| 3304 | return WValue{ .imm32 = extra.field_index }; |
| 3305 | } |
| 3306 | assert(isByRef(union_ty, self.target)); |
| 3307 | |
| 3308 | const result_ptr = try self.allocStack(union_ty); |
| 3309 | const payload = try self.resolveInst(extra.init); |
| 3310 | const union_obj = union_ty.cast(Type.Payload.Union).?.data; |
| 3311 | assert(union_obj.haveFieldTypes()); |
| 3312 | const field = union_obj.fields.values()[extra.field_index]; |
| 3313 | |
| 3314 | if (layout.tag_align >= layout.payload_align) { |
| 3315 | const payload_ptr = try self.buildPointerOffset(result_ptr, layout.tag_size, .new); |
| 3316 | try self.store(payload_ptr, payload, field.ty, 0); |
| 3317 | } else { |
| 3318 | try self.store(result_ptr, payload, field.ty, 0); |
| 3319 | } |
| 3320 | |
| 3321 | return result_ptr; |
| 3227 | 3322 | } |
| 3228 | 3323 | |
| 3229 | 3324 | fn airPrefetch(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |