| ... | ... | @@ -793,7 +793,7 @@ fn resolveInst(func: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue { |
| 793 | 793 | const val = (try func.air.value(ref, pt)).?; |
| 794 | 794 | const ty = func.typeOf(ref); |
| 795 | 795 | if (!ty.hasRuntimeBitsIgnoreComptime(pt) and !ty.isInt(mod) and !ty.isError(mod)) { |
| 796 | | gop.value_ptr.* = WValue{ .none = {} }; |
| 796 | gop.value_ptr.* = .none; |
| 797 | 797 | return gop.value_ptr.*; |
| 798 | 798 | } |
| 799 | 799 | |
| ... | ... | @@ -803,16 +803,17 @@ fn resolveInst(func: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue { |
| 803 | 803 | // |
| 804 | 804 | // In the other cases, we will simply lower the constant to a value that fits |
| 805 | 805 | // into a single local (such as a pointer, integer, bool, etc). |
| 806 | | const result = if (isByRef(ty, pt)) blk: { |
| 807 | | const sym_index = try func.bin_file.lowerUnnamedConst(pt, val, func.decl_index); |
| 808 | | break :blk WValue{ .memory = sym_index }; |
| 809 | | } else try func.lowerConstant(val, ty); |
| 806 | const result: WValue = if (isByRef(ty, pt)) |
| 807 | .{ .memory = try func.bin_file.lowerUnnamedConst(pt, val, func.decl_index) } |
| 808 | else |
| 809 | try func.lowerConstant(val, ty); |
| 810 | 810 | |
| 811 | 811 | gop.value_ptr.* = result; |
| 812 | 812 | return result; |
| 813 | 813 | } |
| 814 | 814 | |
| 815 | | fn finishAir(func: *CodeGen, inst: Air.Inst.Index, result: WValue, operands: []const Air.Inst.Ref) void { |
| 815 | /// NOTE: if result == .stack, it will be stored in .local |
| 816 | fn finishAir(func: *CodeGen, inst: Air.Inst.Index, result: WValue, operands: []const Air.Inst.Ref) InnerError!void { |
| 816 | 817 | assert(operands.len <= Liveness.bpi - 1); |
| 817 | 818 | var tomb_bits = func.liveness.getTombBits(inst); |
| 818 | 819 | for (operands) |operand| { |
| ... | ... | @@ -824,9 +825,12 @@ fn finishAir(func: *CodeGen, inst: Air.Inst.Index, result: WValue, operands: []c |
| 824 | 825 | |
| 825 | 826 | // results of `none` can never be referenced. |
| 826 | 827 | if (result != .none) { |
| 827 | | assert(result != .stack); // it's illegal to store a stack value as we cannot track its position |
| 828 | const trackable_result = if (result != .stack) |
| 829 | result |
| 830 | else |
| 831 | try result.toLocal(func, func.typeOfIndex(inst)); |
| 828 | 832 | const branch = func.currentBranch(); |
| 829 | | branch.values.putAssumeCapacityNoClobber(inst.toRef(), result); |
| 833 | branch.values.putAssumeCapacityNoClobber(inst.toRef(), trackable_result); |
| 830 | 834 | } |
| 831 | 835 | |
| 832 | 836 | if (std.debug.runtime_safety) { |
| ... | ... | @@ -990,44 +994,44 @@ fn addExtraAssumeCapacity(func: *CodeGen, extra: anytype) error{OutOfMemory}!u32 |
| 990 | 994 | return result; |
| 991 | 995 | } |
| 992 | 996 | |
| 993 | | /// Using a given `Type`, returns the corresponding type |
| 997 | /// Using a given `Type`, returns the corresponding valtype for .auto callconv |
| 994 | 998 | fn typeToValtype(ty: Type, pt: Zcu.PerThread) wasm.Valtype { |
| 995 | 999 | const mod = pt.zcu; |
| 996 | 1000 | const target = mod.getTarget(); |
| 997 | 1001 | const ip = &mod.intern_pool; |
| 998 | 1002 | return switch (ty.zigTypeTag(mod)) { |
| 999 | 1003 | .Float => switch (ty.floatBits(target)) { |
| 1000 | | 16 => wasm.Valtype.i32, // stored/loaded as u16 |
| 1001 | | 32 => wasm.Valtype.f32, |
| 1002 | | 64 => wasm.Valtype.f64, |
| 1003 | | 80, 128 => wasm.Valtype.i64, |
| 1004 | 16 => .i32, // stored/loaded as u16 |
| 1005 | 32 => .f32, |
| 1006 | 64 => .f64, |
| 1007 | 80, 128 => .i32, |
| 1004 | 1008 | else => unreachable, |
| 1005 | 1009 | }, |
| 1006 | | .Int, .Enum => blk: { |
| 1007 | | const info = ty.intInfo(pt.zcu); |
| 1008 | | if (info.bits <= 32) break :blk wasm.Valtype.i32; |
| 1009 | | if (info.bits > 32 and info.bits <= 128) break :blk wasm.Valtype.i64; |
| 1010 | | break :blk wasm.Valtype.i32; // represented as pointer to stack |
| 1010 | .Int, .Enum => switch (ty.intInfo(pt.zcu).bits) { |
| 1011 | 0...32 => .i32, |
| 1012 | 33...64 => .i64, |
| 1013 | else => .i32, |
| 1011 | 1014 | }, |
| 1012 | | .Struct => { |
| 1015 | .Struct => blk: { |
| 1013 | 1016 | if (pt.zcu.typeToPackedStruct(ty)) |packed_struct| { |
| 1014 | | return typeToValtype(Type.fromInterned(packed_struct.backingIntTypeUnordered(ip)), pt); |
| 1017 | const backing_int_ty = Type.fromInterned(packed_struct.backingIntTypeUnordered(ip)); |
| 1018 | break :blk typeToValtype(backing_int_ty, pt); |
| 1015 | 1019 | } else { |
| 1016 | | return wasm.Valtype.i32; |
| 1020 | break :blk .i32; |
| 1017 | 1021 | } |
| 1018 | 1022 | }, |
| 1019 | 1023 | .Vector => switch (determineSimdStoreStrategy(ty, pt)) { |
| 1020 | | .direct => wasm.Valtype.v128, |
| 1021 | | .unrolled => wasm.Valtype.i32, |
| 1024 | .direct => .v128, |
| 1025 | .unrolled => .i32, |
| 1022 | 1026 | }, |
| 1023 | 1027 | .Union => switch (ty.containerLayout(pt.zcu)) { |
| 1024 | | .@"packed" => { |
| 1028 | .@"packed" => blk: { |
| 1025 | 1029 | const int_ty = pt.intType(.unsigned, @as(u16, @intCast(ty.bitSize(pt)))) catch @panic("out of memory"); |
| 1026 | | return typeToValtype(int_ty, pt); |
| 1030 | break :blk typeToValtype(int_ty, pt); |
| 1027 | 1031 | }, |
| 1028 | | else => wasm.Valtype.i32, |
| 1032 | else => .i32, |
| 1029 | 1033 | }, |
| 1030 | | else => wasm.Valtype.i32, // all represented as reference/immediate |
| 1034 | else => .i32, // all represented as reference/immediate |
| 1031 | 1035 | }; |
| 1032 | 1036 | } |
| 1033 | 1037 | |
| ... | ... | @@ -1105,30 +1109,18 @@ fn getResolvedInst(func: *CodeGen, ref: Air.Inst.Ref) *WValue { |
| 1105 | 1109 | fn allocLocal(func: *CodeGen, ty: Type) InnerError!WValue { |
| 1106 | 1110 | const pt = func.pt; |
| 1107 | 1111 | const valtype = typeToValtype(ty, pt); |
| 1108 | | switch (valtype) { |
| 1109 | | .i32 => if (func.free_locals_i32.popOrNull()) |index| { |
| 1110 | | log.debug("reusing local ({d}) of type {}", .{ index, valtype }); |
| 1111 | | return WValue{ .local = .{ .value = index, .references = 1 } }; |
| 1112 | | }, |
| 1113 | | .i64 => if (func.free_locals_i64.popOrNull()) |index| { |
| 1114 | | log.debug("reusing local ({d}) of type {}", .{ index, valtype }); |
| 1115 | | return WValue{ .local = .{ .value = index, .references = 1 } }; |
| 1116 | | }, |
| 1117 | | .f32 => if (func.free_locals_f32.popOrNull()) |index| { |
| 1118 | | log.debug("reusing local ({d}) of type {}", .{ index, valtype }); |
| 1119 | | return WValue{ .local = .{ .value = index, .references = 1 } }; |
| 1120 | | }, |
| 1121 | | .f64 => if (func.free_locals_f64.popOrNull()) |index| { |
| 1122 | | log.debug("reusing local ({d}) of type {}", .{ index, valtype }); |
| 1123 | | return WValue{ .local = .{ .value = index, .references = 1 } }; |
| 1124 | | }, |
| 1125 | | .v128 => if (func.free_locals_v128.popOrNull()) |index| { |
| 1126 | | log.debug("reusing local ({d}) of type {}", .{ index, valtype }); |
| 1127 | | return WValue{ .local = .{ .value = index, .references = 1 } }; |
| 1128 | | }, |
| 1112 | const index_or_null = switch (valtype) { |
| 1113 | .i32 => func.free_locals_i32.popOrNull(), |
| 1114 | .i64 => func.free_locals_i64.popOrNull(), |
| 1115 | .f32 => func.free_locals_f32.popOrNull(), |
| 1116 | .f64 => func.free_locals_f64.popOrNull(), |
| 1117 | .v128 => func.free_locals_v128.popOrNull(), |
| 1118 | }; |
| 1119 | if (index_or_null) |index| { |
| 1120 | log.debug("reusing local ({d}) of type {}", .{ index, valtype }); |
| 1121 | return .{ .local = .{ .value = index, .references = 1 } }; |
| 1129 | 1122 | } |
| 1130 | 1123 | log.debug("new local of type {}", .{valtype}); |
| 1131 | | // no local was free to be re-used, so allocate a new local instead |
| 1132 | 1124 | return func.ensureAllocLocal(ty); |
| 1133 | 1125 | } |
| 1134 | 1126 | |
| ... | ... | @@ -1139,7 +1131,7 @@ fn ensureAllocLocal(func: *CodeGen, ty: Type) InnerError!WValue { |
| 1139 | 1131 | try func.locals.append(func.gpa, genValtype(ty, pt)); |
| 1140 | 1132 | const initial_index = func.local_index; |
| 1141 | 1133 | func.local_index += 1; |
| 1142 | | return WValue{ .local = .{ .value = initial_index, .references = 1 } }; |
| 1134 | return .{ .local = .{ .value = initial_index, .references = 1 } }; |
| 1143 | 1135 | } |
| 1144 | 1136 | |
| 1145 | 1137 | /// Generates a `wasm.Type` from a given function type. |
| ... | ... | @@ -1180,20 +1172,20 @@ fn genFunctype( |
| 1180 | 1172 | switch (cc) { |
| 1181 | 1173 | .C => { |
| 1182 | 1174 | const param_classes = abi.classifyType(param_type, pt); |
| 1183 | | for (param_classes) |class| { |
| 1184 | | if (class == .none) continue; |
| 1185 | | if (class == .direct) { |
| 1175 | if (param_classes[1] == .none) { |
| 1176 | if (param_classes[0] == .direct) { |
| 1186 | 1177 | const scalar_type = abi.scalarType(param_type, pt); |
| 1187 | 1178 | try temp_params.append(typeToValtype(scalar_type, pt)); |
| 1188 | 1179 | } else { |
| 1189 | 1180 | try temp_params.append(typeToValtype(param_type, pt)); |
| 1190 | 1181 | } |
| 1182 | } else { |
| 1183 | // i128/f128 |
| 1184 | try temp_params.append(.i64); |
| 1185 | try temp_params.append(.i64); |
| 1191 | 1186 | } |
| 1192 | 1187 | }, |
| 1193 | | else => if (isByRef(param_type, pt)) |
| 1194 | | try temp_params.append(.i32) |
| 1195 | | else |
| 1196 | | try temp_params.append(typeToValtype(param_type, pt)), |
| 1188 | else => try temp_params.append(typeToValtype(param_type, pt)), |
| 1197 | 1189 | } |
| 1198 | 1190 | } |
| 1199 | 1191 | |
| ... | ... | @@ -1539,7 +1531,7 @@ fn allocStack(func: *CodeGen, ty: Type) !WValue { |
| 1539 | 1531 | const offset: u32 = @intCast(abi_align.forward(func.stack_size)); |
| 1540 | 1532 | defer func.stack_size = offset + abi_size; |
| 1541 | 1533 | |
| 1542 | | return WValue{ .stack_offset = .{ .value = offset, .references = 1 } }; |
| 1534 | return .{ .stack_offset = .{ .value = offset, .references = 1 } }; |
| 1543 | 1535 | } |
| 1544 | 1536 | |
| 1545 | 1537 | /// From a given AIR instruction generates a pointer to the stack where |
| ... | ... | @@ -1571,7 +1563,7 @@ fn allocStackPtr(func: *CodeGen, inst: Air.Inst.Index) !WValue { |
| 1571 | 1563 | const offset: u32 = @intCast(abi_alignment.forward(func.stack_size)); |
| 1572 | 1564 | defer func.stack_size = offset + abi_size; |
| 1573 | 1565 | |
| 1574 | | return WValue{ .stack_offset = .{ .value = offset, .references = 1 } }; |
| 1566 | return .{ .stack_offset = .{ .value = offset, .references = 1 } }; |
| 1575 | 1567 | } |
| 1576 | 1568 | |
| 1577 | 1569 | /// From given zig bitsize, returns the wasm bitsize |
| ... | ... | @@ -2135,7 +2127,7 @@ fn airRet(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2135 | 2127 | try func.restoreStackPointer(); |
| 2136 | 2128 | try func.addTag(.@"return"); |
| 2137 | 2129 | |
| 2138 | | func.finishAir(inst, .none, &.{un_op}); |
| 2130 | return func.finishAir(inst, .none, &.{un_op}); |
| 2139 | 2131 | } |
| 2140 | 2132 | |
| 2141 | 2133 | fn airRetPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -2156,7 +2148,7 @@ fn airRetPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2156 | 2148 | break :result try func.allocStackPtr(inst); |
| 2157 | 2149 | }; |
| 2158 | 2150 | |
| 2159 | | func.finishAir(inst, result, &.{}); |
| 2151 | return func.finishAir(inst, result, &.{}); |
| 2160 | 2152 | } |
| 2161 | 2153 | |
| 2162 | 2154 | fn airRetLoad(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -2234,11 +2226,11 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif |
| 2234 | 2226 | return func.fail("Expected a function, but instead found '{s}'", .{@tagName(ip.indexToKey(func_val.toIntern()))}); |
| 2235 | 2227 | }; |
| 2236 | 2228 | |
| 2237 | | const sret = if (first_param_sret) blk: { |
| 2229 | const sret: WValue = if (first_param_sret) blk: { |
| 2238 | 2230 | const sret_local = try func.allocStack(ret_ty); |
| 2239 | 2231 | try func.lowerToStack(sret_local); |
| 2240 | 2232 | break :blk sret_local; |
| 2241 | | } else WValue{ .none = {} }; |
| 2233 | } else .none; |
| 2242 | 2234 | |
| 2243 | 2235 | for (args) |arg| { |
| 2244 | 2236 | const arg_val = try func.resolveInst(arg); |
| ... | ... | @@ -2268,10 +2260,10 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif |
| 2268 | 2260 | |
| 2269 | 2261 | const result_value = result_value: { |
| 2270 | 2262 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt) and !ret_ty.isError(mod)) { |
| 2271 | | break :result_value WValue{ .none = {} }; |
| 2263 | break :result_value .none; |
| 2272 | 2264 | } else if (ret_ty.isNoReturn(mod)) { |
| 2273 | 2265 | try func.addTag(.@"unreachable"); |
| 2274 | | break :result_value WValue{ .none = {} }; |
| 2266 | break :result_value .none; |
| 2275 | 2267 | } else if (first_param_sret) { |
| 2276 | 2268 | break :result_value sret; |
| 2277 | 2269 | // TODO: Make this less fragile and optimize |
| ... | ... | @@ -2297,7 +2289,7 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif |
| 2297 | 2289 | |
| 2298 | 2290 | fn airAlloc(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2299 | 2291 | const value = try func.allocStackPtr(inst); |
| 2300 | | func.finishAir(inst, value, &.{}); |
| 2292 | return func.finishAir(inst, value, &.{}); |
| 2301 | 2293 | } |
| 2302 | 2294 | |
| 2303 | 2295 | fn airStore(func: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void { |
| ... | ... | @@ -2330,18 +2322,18 @@ fn airStore(func: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void |
| 2330 | 2322 | var mask = @as(u64, @intCast((@as(u65, 1) << @as(u7, @intCast(ty.bitSize(pt)))) - 1)); |
| 2331 | 2323 | mask <<= @as(u6, @intCast(ptr_info.packed_offset.bit_offset)); |
| 2332 | 2324 | mask ^= ~@as(u64, 0); |
| 2333 | | const shift_val = if (ptr_info.packed_offset.host_size <= 4) |
| 2334 | | WValue{ .imm32 = ptr_info.packed_offset.bit_offset } |
| 2325 | const shift_val: WValue = if (ptr_info.packed_offset.host_size <= 4) |
| 2326 | .{ .imm32 = ptr_info.packed_offset.bit_offset } |
| 2335 | 2327 | else |
| 2336 | | WValue{ .imm64 = ptr_info.packed_offset.bit_offset }; |
| 2337 | | const mask_val = if (ptr_info.packed_offset.host_size <= 4) |
| 2338 | | WValue{ .imm32 = @as(u32, @truncate(mask)) } |
| 2328 | .{ .imm64 = ptr_info.packed_offset.bit_offset }; |
| 2329 | const mask_val: WValue = if (ptr_info.packed_offset.host_size <= 4) |
| 2330 | .{ .imm32 = @as(u32, @truncate(mask)) } |
| 2339 | 2331 | else |
| 2340 | | WValue{ .imm64 = mask }; |
| 2341 | | const wrap_mask_val = if (ptr_info.packed_offset.host_size <= 4) |
| 2342 | | WValue{ .imm32 = @truncate(~@as(u64, 0) >> @intCast(64 - ty.bitSize(pt))) } |
| 2332 | .{ .imm64 = mask }; |
| 2333 | const wrap_mask_val: WValue = if (ptr_info.packed_offset.host_size <= 4) |
| 2334 | .{ .imm32 = @truncate(~@as(u64, 0) >> @intCast(64 - ty.bitSize(pt))) } |
| 2343 | 2335 | else |
| 2344 | | WValue{ .imm64 = ~@as(u64, 0) >> @intCast(64 - ty.bitSize(pt)) }; |
| 2336 | .{ .imm64 = ~@as(u64, 0) >> @intCast(64 - ty.bitSize(pt)) }; |
| 2345 | 2337 | |
| 2346 | 2338 | try func.emitWValue(lhs); |
| 2347 | 2339 | const loaded = try func.load(lhs, int_elem_ty, 0); |
| ... | ... | @@ -2356,7 +2348,7 @@ fn airStore(func: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void |
| 2356 | 2348 | try func.store(.stack, result, int_elem_ty, lhs.offset()); |
| 2357 | 2349 | } |
| 2358 | 2350 | |
| 2359 | | func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 2351 | return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 2360 | 2352 | } |
| 2361 | 2353 | |
| 2362 | 2354 | fn store(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerError!void { |
| ... | ... | @@ -2418,23 +2410,23 @@ fn store(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerE |
| 2418 | 2410 | // lower it to the stack so we do not have to store rhs into a local first |
| 2419 | 2411 | try func.emitWValue(lhs); |
| 2420 | 2412 | const ptr_local = try func.load(rhs, Type.usize, 0); |
| 2421 | | try func.store(.{ .stack = {} }, ptr_local, Type.usize, 0 + lhs.offset()); |
| 2413 | try func.store(.stack, ptr_local, Type.usize, 0 + lhs.offset()); |
| 2422 | 2414 | |
| 2423 | 2415 | // retrieve length from rhs, and store that alongside lhs as well |
| 2424 | 2416 | try func.emitWValue(lhs); |
| 2425 | 2417 | const len_local = try func.load(rhs, Type.usize, func.ptrSize()); |
| 2426 | | try func.store(.{ .stack = {} }, len_local, Type.usize, func.ptrSize() + lhs.offset()); |
| 2418 | try func.store(.stack, len_local, Type.usize, func.ptrSize() + lhs.offset()); |
| 2427 | 2419 | return; |
| 2428 | 2420 | } |
| 2429 | 2421 | }, |
| 2430 | 2422 | .Int, .Enum, .Float => if (abi_size > 8 and abi_size <= 16) { |
| 2431 | 2423 | try func.emitWValue(lhs); |
| 2432 | 2424 | const lsb = try func.load(rhs, Type.u64, 0); |
| 2433 | | try func.store(.{ .stack = {} }, lsb, Type.u64, 0 + lhs.offset()); |
| 2425 | try func.store(.stack, lsb, Type.u64, 0 + lhs.offset()); |
| 2434 | 2426 | |
| 2435 | 2427 | try func.emitWValue(lhs); |
| 2436 | 2428 | const msb = try func.load(rhs, Type.u64, 8); |
| 2437 | | try func.store(.{ .stack = {} }, msb, Type.u64, 8 + lhs.offset()); |
| 2429 | try func.store(.stack, msb, Type.u64, 8 + lhs.offset()); |
| 2438 | 2430 | return; |
| 2439 | 2431 | } else if (abi_size > 16) { |
| 2440 | 2432 | try func.memcpy(lhs, rhs, .{ .imm32 = @as(u32, @intCast(ty.abiSize(pt))) }); |
| ... | ... | @@ -2487,27 +2479,24 @@ fn airLoad(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2487 | 2479 | } |
| 2488 | 2480 | |
| 2489 | 2481 | if (ptr_info.packed_offset.host_size == 0) { |
| 2490 | | const stack_loaded = try func.load(operand, ty, 0); |
| 2491 | | break :result try stack_loaded.toLocal(func, ty); |
| 2482 | break :result try func.load(operand, ty, 0); |
| 2492 | 2483 | } |
| 2493 | 2484 | |
| 2494 | 2485 | // at this point we have a non-natural alignment, we must |
| 2495 | 2486 | // shift the value to obtain the correct bit. |
| 2496 | 2487 | const int_elem_ty = try pt.intType(.unsigned, ptr_info.packed_offset.host_size * 8); |
| 2497 | | const shift_val = if (ptr_info.packed_offset.host_size <= 4) |
| 2498 | | WValue{ .imm32 = ptr_info.packed_offset.bit_offset } |
| 2488 | const shift_val: WValue = if (ptr_info.packed_offset.host_size <= 4) |
| 2489 | .{ .imm32 = ptr_info.packed_offset.bit_offset } |
| 2499 | 2490 | else if (ptr_info.packed_offset.host_size <= 8) |
| 2500 | | WValue{ .imm64 = ptr_info.packed_offset.bit_offset } |
| 2491 | .{ .imm64 = ptr_info.packed_offset.bit_offset } |
| 2501 | 2492 | else |
| 2502 | 2493 | return func.fail("TODO: airLoad where ptr to bitfield exceeds 64 bits", .{}); |
| 2503 | 2494 | |
| 2504 | 2495 | const stack_loaded = try func.load(operand, int_elem_ty, 0); |
| 2505 | 2496 | const shifted = try func.binOp(stack_loaded, shift_val, int_elem_ty, .shr); |
| 2506 | | const result = try func.trunc(shifted, ty, int_elem_ty); |
| 2507 | | // const wrapped = try func.wrapOperand(shifted, ty); |
| 2508 | | break :result try result.toLocal(func, ty); |
| 2497 | break :result try func.trunc(shifted, ty, int_elem_ty); |
| 2509 | 2498 | }; |
| 2510 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 2499 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 2511 | 2500 | } |
| 2512 | 2501 | |
| 2513 | 2502 | /// Loads an operand from the linear memory section. |
| ... | ... | @@ -2528,7 +2517,7 @@ fn load(func: *CodeGen, operand: WValue, ty: Type, offset: u32) InnerError!WValu |
| 2528 | 2517 | @intCast(ty.abiAlignment(pt).toByteUnits().?), |
| 2529 | 2518 | }); |
| 2530 | 2519 | try func.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } }); |
| 2531 | | return WValue{ .stack = {} }; |
| 2520 | return .stack; |
| 2532 | 2521 | } |
| 2533 | 2522 | |
| 2534 | 2523 | const abi_size: u8 = @intCast(ty.abiSize(pt)); |
| ... | ... | @@ -2547,7 +2536,7 @@ fn load(func: *CodeGen, operand: WValue, ty: Type, offset: u32) InnerError!WValu |
| 2547 | 2536 | }, |
| 2548 | 2537 | ); |
| 2549 | 2538 | |
| 2550 | | return WValue{ .stack = {} }; |
| 2539 | return .stack; |
| 2551 | 2540 | } |
| 2552 | 2541 | |
| 2553 | 2542 | fn airArg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -2596,7 +2585,7 @@ fn airArg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2596 | 2585 | else => {}, |
| 2597 | 2586 | } |
| 2598 | 2587 | |
| 2599 | | func.finishAir(inst, arg, &.{}); |
| 2588 | return func.finishAir(inst, arg, &.{}); |
| 2600 | 2589 | } |
| 2601 | 2590 | |
| 2602 | 2591 | fn airBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| ... | ... | @@ -2613,25 +2602,21 @@ fn airBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 2613 | 2602 | // case we first coerce the operands to the same type before performing the operation. |
| 2614 | 2603 | // For big integers we can ignore this as we will call into compiler-rt which handles this. |
| 2615 | 2604 | const result = switch (op) { |
| 2616 | | .shr, .shl => res: { |
| 2605 | .shr, .shl => result: { |
| 2617 | 2606 | const lhs_wasm_bits = toWasmBits(@intCast(lhs_ty.bitSize(pt))) orelse { |
| 2618 | 2607 | return func.fail("TODO: implement '{s}' for types larger than 128 bits", .{@tagName(op)}); |
| 2619 | 2608 | }; |
| 2620 | 2609 | const rhs_wasm_bits = toWasmBits(@intCast(rhs_ty.bitSize(pt))).?; |
| 2621 | | const new_rhs = if (lhs_wasm_bits != rhs_wasm_bits and lhs_wasm_bits != 128) blk: { |
| 2622 | | const tmp = try func.intcast(rhs, rhs_ty, lhs_ty); |
| 2623 | | break :blk try tmp.toLocal(func, lhs_ty); |
| 2624 | | } else rhs; |
| 2625 | | const stack_result = try func.binOp(lhs, new_rhs, lhs_ty, op); |
| 2626 | | break :res try stack_result.toLocal(func, lhs_ty); |
| 2627 | | }, |
| 2628 | | else => res: { |
| 2629 | | const stack_result = try func.binOp(lhs, rhs, lhs_ty, op); |
| 2630 | | break :res try stack_result.toLocal(func, lhs_ty); |
| 2610 | const new_rhs = if (lhs_wasm_bits != rhs_wasm_bits and lhs_wasm_bits != 128) |
| 2611 | try (try func.intcast(rhs, rhs_ty, lhs_ty)).toLocal(func, lhs_ty) |
| 2612 | else |
| 2613 | rhs; |
| 2614 | break :result try func.binOp(lhs, new_rhs, lhs_ty, op); |
| 2631 | 2615 | }, |
| 2616 | else => try func.binOp(lhs, rhs, lhs_ty, op), |
| 2632 | 2617 | }; |
| 2633 | 2618 | |
| 2634 | | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 2619 | return func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 2635 | 2620 | } |
| 2636 | 2621 | |
| 2637 | 2622 | /// Performs a binary operation on the given `WValue`'s |
| ... | ... | @@ -2667,7 +2652,7 @@ fn binOp(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError! |
| 2667 | 2652 | |
| 2668 | 2653 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 2669 | 2654 | |
| 2670 | | return WValue{ .stack = {} }; |
| 2655 | return .stack; |
| 2671 | 2656 | } |
| 2672 | 2657 | |
| 2673 | 2658 | fn binOpBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError!WValue { |
| ... | ... | @@ -2839,6 +2824,7 @@ fn airAbs(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2839 | 2824 | try func.addTag(.i32_xor); |
| 2840 | 2825 | try func.emitWValue(tmp); |
| 2841 | 2826 | try func.addTag(.i32_sub); |
| 2827 | return func.finishAir(inst, .stack, &.{ty_op.operand}); |
| 2842 | 2828 | }, |
| 2843 | 2829 | 64 => { |
| 2844 | 2830 | try func.emitWValue(operand); |
| ... | ... | @@ -2854,6 +2840,7 @@ fn airAbs(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2854 | 2840 | try func.addTag(.i64_xor); |
| 2855 | 2841 | try func.emitWValue(tmp); |
| 2856 | 2842 | try func.addTag(.i64_sub); |
| 2843 | return func.finishAir(inst, .stack, &.{ty_op.operand}); |
| 2857 | 2844 | }, |
| 2858 | 2845 | 128 => { |
| 2859 | 2846 | const mask = try func.allocStack(Type.u128); |
| ... | ... | @@ -2874,18 +2861,14 @@ fn airAbs(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2874 | 2861 | const a = try func.binOpBigInt(operand, mask, Type.u128, .xor); |
| 2875 | 2862 | const b = try func.binOpBigInt(a, mask, Type.u128, .sub); |
| 2876 | 2863 | |
| 2877 | | func.finishAir(inst, b, &.{ty_op.operand}); |
| 2878 | | return; |
| 2864 | return func.finishAir(inst, b, &.{ty_op.operand}); |
| 2879 | 2865 | }, |
| 2880 | 2866 | else => unreachable, |
| 2881 | 2867 | } |
| 2882 | | |
| 2883 | | const result = try (WValue{ .stack = {} }).toLocal(func, ty); |
| 2884 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 2885 | 2868 | }, |
| 2886 | 2869 | .Float => { |
| 2887 | | const result = try (try func.floatOp(.fabs, ty, &.{operand})).toLocal(func, ty); |
| 2888 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 2870 | const result = try func.floatOp(.fabs, ty, &.{operand}); |
| 2871 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 2889 | 2872 | }, |
| 2890 | 2873 | else => unreachable, |
| 2891 | 2874 | } |
| ... | ... | @@ -2896,8 +2879,8 @@ fn airUnaryFloatOp(func: *CodeGen, inst: Air.Inst.Index, op: FloatOp) InnerError |
| 2896 | 2879 | const operand = try func.resolveInst(un_op); |
| 2897 | 2880 | const ty = func.typeOf(un_op); |
| 2898 | 2881 | |
| 2899 | | const result = try (try func.floatOp(op, ty, &.{operand})).toLocal(func, ty); |
| 2900 | | func.finishAir(inst, result, &.{un_op}); |
| 2882 | const result = try func.floatOp(op, ty, &.{operand}); |
| 2883 | return func.finishAir(inst, result, &.{un_op}); |
| 2901 | 2884 | } |
| 2902 | 2885 | |
| 2903 | 2886 | fn floatOp(func: *CodeGen, float_op: FloatOp, ty: Type, args: []const WValue) InnerError!WValue { |
| ... | ... | @@ -3027,22 +3010,18 @@ fn airWrapBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 3027 | 3010 | // case we first coerce the operands to the same type before performing the operation. |
| 3028 | 3011 | // For big integers we can ignore this as we will call into compiler-rt which handles this. |
| 3029 | 3012 | const result = switch (op) { |
| 3030 | | .shr, .shl => res: { |
| 3013 | .shr, .shl => result: { |
| 3031 | 3014 | const lhs_wasm_bits = toWasmBits(@intCast(lhs_ty.bitSize(pt))) orelse { |
| 3032 | 3015 | return func.fail("TODO: implement '{s}' for types larger than 128 bits", .{@tagName(op)}); |
| 3033 | 3016 | }; |
| 3034 | 3017 | const rhs_wasm_bits = toWasmBits(@intCast(rhs_ty.bitSize(pt))).?; |
| 3035 | | const new_rhs = if (lhs_wasm_bits != rhs_wasm_bits and lhs_wasm_bits != 128) blk: { |
| 3036 | | const tmp = try func.intcast(rhs, rhs_ty, lhs_ty); |
| 3037 | | break :blk try tmp.toLocal(func, lhs_ty); |
| 3038 | | } else rhs; |
| 3039 | | const stack_result = try func.wrapBinOp(lhs, new_rhs, lhs_ty, op); |
| 3040 | | break :res try stack_result.toLocal(func, lhs_ty); |
| 3041 | | }, |
| 3042 | | else => res: { |
| 3043 | | const stack_result = try func.wrapBinOp(lhs, rhs, lhs_ty, op); |
| 3044 | | break :res try stack_result.toLocal(func, lhs_ty); |
| 3018 | const new_rhs = if (lhs_wasm_bits != rhs_wasm_bits and lhs_wasm_bits != 128) |
| 3019 | try (try func.intcast(rhs, rhs_ty, lhs_ty)).toLocal(func, lhs_ty) |
| 3020 | else |
| 3021 | rhs; |
| 3022 | break :result try func.wrapBinOp(lhs, new_rhs, lhs_ty, op); |
| 3045 | 3023 | }, |
| 3024 | else => try func.wrapBinOp(lhs, rhs, lhs_ty, op), |
| 3046 | 3025 | }; |
| 3047 | 3026 | |
| 3048 | 3027 | return func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| ... | ... | @@ -3187,7 +3166,7 @@ fn lowerAnonDeclRef( |
| 3187 | 3166 | |
| 3188 | 3167 | const is_fn_body = ty.zigTypeTag(mod) == .Fn; |
| 3189 | 3168 | if (!is_fn_body and !ty.hasRuntimeBitsIgnoreComptime(pt)) { |
| 3190 | | return WValue{ .imm32 = 0xaaaaaaaa }; |
| 3169 | return .{ .imm32 = 0xaaaaaaaa }; |
| 3191 | 3170 | } |
| 3192 | 3171 | |
| 3193 | 3172 | const decl_align = mod.intern_pool.indexToKey(anon_decl.orig_ty).ptr_type.flags.alignment; |
| ... | ... | @@ -3202,10 +3181,10 @@ fn lowerAnonDeclRef( |
| 3202 | 3181 | const target_atom_index = func.bin_file.zigObjectPtr().?.anon_decls.get(decl_val).?; |
| 3203 | 3182 | const target_sym_index = @intFromEnum(func.bin_file.getAtom(target_atom_index).sym_index); |
| 3204 | 3183 | if (is_fn_body) { |
| 3205 | | return WValue{ .function_index = target_sym_index }; |
| 3184 | return .{ .function_index = target_sym_index }; |
| 3206 | 3185 | } else if (offset == 0) { |
| 3207 | | return WValue{ .memory = target_sym_index }; |
| 3208 | | } else return WValue{ .memory_offset = .{ .pointer = target_sym_index, .offset = offset } }; |
| 3186 | return .{ .memory = target_sym_index }; |
| 3187 | } else return .{ .memory_offset = .{ .pointer = target_sym_index, .offset = offset } }; |
| 3209 | 3188 | } |
| 3210 | 3189 | |
| 3211 | 3190 | fn lowerDeclRefValue(func: *CodeGen, decl_index: InternPool.DeclIndex, offset: u32) InnerError!WValue { |
| ... | ... | @@ -3226,7 +3205,7 @@ fn lowerDeclRefValue(func: *CodeGen, decl_index: InternPool.DeclIndex, offset: u |
| 3226 | 3205 | } |
| 3227 | 3206 | const decl_ty = decl.typeOf(mod); |
| 3228 | 3207 | if (decl_ty.zigTypeTag(mod) != .Fn and !decl_ty.hasRuntimeBitsIgnoreComptime(pt)) { |
| 3229 | | return WValue{ .imm32 = 0xaaaaaaaa }; |
| 3208 | return .{ .imm32 = 0xaaaaaaaa }; |
| 3230 | 3209 | } |
| 3231 | 3210 | |
| 3232 | 3211 | const atom_index = try func.bin_file.getOrCreateAtomForDecl(pt, decl_index); |
| ... | ... | @@ -3234,10 +3213,10 @@ fn lowerDeclRefValue(func: *CodeGen, decl_index: InternPool.DeclIndex, offset: u |
| 3234 | 3213 | |
| 3235 | 3214 | const target_sym_index = @intFromEnum(atom.sym_index); |
| 3236 | 3215 | if (decl_ty.zigTypeTag(mod) == .Fn) { |
| 3237 | | return WValue{ .function_index = target_sym_index }; |
| 3216 | return .{ .function_index = target_sym_index }; |
| 3238 | 3217 | } else if (offset == 0) { |
| 3239 | | return WValue{ .memory = target_sym_index }; |
| 3240 | | } else return WValue{ .memory_offset = .{ .pointer = target_sym_index, .offset = offset } }; |
| 3218 | return .{ .memory = target_sym_index }; |
| 3219 | } else return .{ .memory_offset = .{ .pointer = target_sym_index, .offset = offset } }; |
| 3241 | 3220 | } |
| 3242 | 3221 | |
| 3243 | 3222 | /// Asserts that `isByRef` returns `false` for `ty`. |
| ... | ... | @@ -3276,7 +3255,7 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { |
| 3276 | 3255 | .@"unreachable", |
| 3277 | 3256 | .generic_poison, |
| 3278 | 3257 | => unreachable, // non-runtime values |
| 3279 | | .false, .true => return WValue{ .imm32 = switch (simple_value) { |
| 3258 | .false, .true => return .{ .imm32 = switch (simple_value) { |
| 3280 | 3259 | .false => 0, |
| 3281 | 3260 | .true => 1, |
| 3282 | 3261 | else => unreachable, |
| ... | ... | @@ -3292,20 +3271,20 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { |
| 3292 | 3271 | const int_info = ty.intInfo(mod); |
| 3293 | 3272 | switch (int_info.signedness) { |
| 3294 | 3273 | .signed => switch (int_info.bits) { |
| 3295 | | 0...32 => return WValue{ .imm32 = @bitCast(@as(i32, @intCast(val.toSignedInt(pt)))) }, |
| 3296 | | 33...64 => return WValue{ .imm64 = @bitCast(val.toSignedInt(pt)) }, |
| 3274 | 0...32 => return .{ .imm32 = @bitCast(@as(i32, @intCast(val.toSignedInt(pt)))) }, |
| 3275 | 33...64 => return .{ .imm64 = @bitCast(val.toSignedInt(pt)) }, |
| 3297 | 3276 | else => unreachable, |
| 3298 | 3277 | }, |
| 3299 | 3278 | .unsigned => switch (int_info.bits) { |
| 3300 | | 0...32 => return WValue{ .imm32 = @intCast(val.toUnsignedInt(pt)) }, |
| 3301 | | 33...64 => return WValue{ .imm64 = val.toUnsignedInt(pt) }, |
| 3279 | 0...32 => return .{ .imm32 = @intCast(val.toUnsignedInt(pt)) }, |
| 3280 | 33...64 => return .{ .imm64 = val.toUnsignedInt(pt) }, |
| 3302 | 3281 | else => unreachable, |
| 3303 | 3282 | }, |
| 3304 | 3283 | } |
| 3305 | 3284 | }, |
| 3306 | 3285 | .err => |err| { |
| 3307 | 3286 | const int = try pt.getErrorValue(err.name); |
| 3308 | | return WValue{ .imm32 = int }; |
| 3287 | return .{ .imm32 = int }; |
| 3309 | 3288 | }, |
| 3310 | 3289 | .error_union => |error_union| { |
| 3311 | 3290 | const err_int_ty = try pt.errorIntType(); |
| ... | ... | @@ -3335,9 +3314,9 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { |
| 3335 | 3314 | return func.lowerConstant(Value.fromInterned(enum_tag.int), Type.fromInterned(int_tag_ty)); |
| 3336 | 3315 | }, |
| 3337 | 3316 | .float => |float| switch (float.storage) { |
| 3338 | | .f16 => |f16_val| return WValue{ .imm32 = @as(u16, @bitCast(f16_val)) }, |
| 3339 | | .f32 => |f32_val| return WValue{ .float32 = f32_val }, |
| 3340 | | .f64 => |f64_val| return WValue{ .float64 = f64_val }, |
| 3317 | .f16 => |f16_val| return .{ .imm32 = @as(u16, @bitCast(f16_val)) }, |
| 3318 | .f32 => |f32_val| return .{ .float32 = f32_val }, |
| 3319 | .f64 => |f64_val| return .{ .float64 = f64_val }, |
| 3341 | 3320 | else => unreachable, |
| 3342 | 3321 | }, |
| 3343 | 3322 | .slice => |slice| { |
| ... | ... | @@ -3357,10 +3336,10 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { |
| 3357 | 3336 | if (val.optionalValue(mod)) |payload| { |
| 3358 | 3337 | return func.lowerConstant(payload, pl_ty); |
| 3359 | 3338 | } else { |
| 3360 | | return WValue{ .imm32 = 0 }; |
| 3339 | return .{ .imm32 = 0 }; |
| 3361 | 3340 | } |
| 3362 | 3341 | } else { |
| 3363 | | return WValue{ .imm32 = @intFromBool(!val.isNull(mod)) }; |
| 3342 | return .{ .imm32 = @intFromBool(!val.isNull(mod)) }; |
| 3364 | 3343 | }, |
| 3365 | 3344 | .aggregate => switch (ip.indexToKey(ty.ip_index)) { |
| 3366 | 3345 | .array_type => return func.fail("Wasm TODO: LowerConstant for {}", .{ty.fmt(pt)}), |
| ... | ... | @@ -3406,7 +3385,7 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { |
| 3406 | 3385 | fn storeSimdImmd(func: *CodeGen, value: [16]u8) !WValue { |
| 3407 | 3386 | const index = @as(u32, @intCast(func.simd_immediates.items.len)); |
| 3408 | 3387 | try func.simd_immediates.append(func.gpa, value); |
| 3409 | | return WValue{ .imm128 = index }; |
| 3388 | return .{ .imm128 = index }; |
| 3410 | 3389 | } |
| 3411 | 3390 | |
| 3412 | 3391 | fn emitUndefined(func: *CodeGen, ty: Type) InnerError!WValue { |
| ... | ... | @@ -3414,21 +3393,21 @@ fn emitUndefined(func: *CodeGen, ty: Type) InnerError!WValue { |
| 3414 | 3393 | const mod = pt.zcu; |
| 3415 | 3394 | const ip = &mod.intern_pool; |
| 3416 | 3395 | switch (ty.zigTypeTag(mod)) { |
| 3417 | | .Bool, .ErrorSet => return WValue{ .imm32 = 0xaaaaaaaa }, |
| 3396 | .Bool, .ErrorSet => return .{ .imm32 = 0xaaaaaaaa }, |
| 3418 | 3397 | .Int, .Enum => switch (ty.intInfo(mod).bits) { |
| 3419 | | 0...32 => return WValue{ .imm32 = 0xaaaaaaaa }, |
| 3420 | | 33...64 => return WValue{ .imm64 = 0xaaaaaaaaaaaaaaaa }, |
| 3398 | 0...32 => return .{ .imm32 = 0xaaaaaaaa }, |
| 3399 | 33...64 => return .{ .imm64 = 0xaaaaaaaaaaaaaaaa }, |
| 3421 | 3400 | else => unreachable, |
| 3422 | 3401 | }, |
| 3423 | 3402 | .Float => switch (ty.floatBits(func.target)) { |
| 3424 | | 16 => return WValue{ .imm32 = 0xaaaaaaaa }, |
| 3425 | | 32 => return WValue{ .float32 = @as(f32, @bitCast(@as(u32, 0xaaaaaaaa))) }, |
| 3426 | | 64 => return WValue{ .float64 = @as(f64, @bitCast(@as(u64, 0xaaaaaaaaaaaaaaaa))) }, |
| 3403 | 16 => return .{ .imm32 = 0xaaaaaaaa }, |
| 3404 | 32 => return .{ .float32 = @as(f32, @bitCast(@as(u32, 0xaaaaaaaa))) }, |
| 3405 | 64 => return .{ .float64 = @as(f64, @bitCast(@as(u64, 0xaaaaaaaaaaaaaaaa))) }, |
| 3427 | 3406 | else => unreachable, |
| 3428 | 3407 | }, |
| 3429 | 3408 | .Pointer => switch (func.arch()) { |
| 3430 | | .wasm32 => return WValue{ .imm32 = 0xaaaaaaaa }, |
| 3431 | | .wasm64 => return WValue{ .imm64 = 0xaaaaaaaaaaaaaaaa }, |
| 3409 | .wasm32 => return .{ .imm32 = 0xaaaaaaaa }, |
| 3410 | .wasm64 => return .{ .imm64 = 0xaaaaaaaaaaaaaaaa }, |
| 3432 | 3411 | else => unreachable, |
| 3433 | 3412 | }, |
| 3434 | 3413 | .Optional => { |
| ... | ... | @@ -3436,10 +3415,10 @@ fn emitUndefined(func: *CodeGen, ty: Type) InnerError!WValue { |
| 3436 | 3415 | if (ty.optionalReprIsPayload(mod)) { |
| 3437 | 3416 | return func.emitUndefined(pl_ty); |
| 3438 | 3417 | } |
| 3439 | | return WValue{ .imm32 = 0xaaaaaaaa }; |
| 3418 | return .{ .imm32 = 0xaaaaaaaa }; |
| 3440 | 3419 | }, |
| 3441 | 3420 | .ErrorUnion => { |
| 3442 | | return WValue{ .imm32 = 0xaaaaaaaa }; |
| 3421 | return .{ .imm32 = 0xaaaaaaaa }; |
| 3443 | 3422 | }, |
| 3444 | 3423 | .Struct => { |
| 3445 | 3424 | const packed_struct = mod.typeToPackedStruct(ty).?; |
| ... | ... | @@ -3501,7 +3480,7 @@ fn lowerBlock(func: *CodeGen, inst: Air.Inst.Index, block_ty: Type, body: []cons |
| 3501 | 3480 | const block_result: WValue = if (wasm_block_ty != wasm.block_empty) blk: { |
| 3502 | 3481 | const ty: Type = if (isByRef(block_ty, pt)) Type.u32 else block_ty; |
| 3503 | 3482 | break :blk try func.ensureAllocLocal(ty); // make sure it's a clean local as it may never get overwritten |
| 3504 | | } else WValue.none; |
| 3483 | } else .none; |
| 3505 | 3484 | |
| 3506 | 3485 | try func.startBlock(.block, wasm.block_empty); |
| 3507 | 3486 | // Here we set the current block idx, so breaks know the depth to jump |
| ... | ... | @@ -3517,7 +3496,7 @@ fn lowerBlock(func: *CodeGen, inst: Air.Inst.Index, block_ty: Type, body: []cons |
| 3517 | 3496 | const liveness = func.liveness.getBlock(inst); |
| 3518 | 3497 | try func.currentBranch().values.ensureUnusedCapacity(func.gpa, liveness.deaths.len); |
| 3519 | 3498 | |
| 3520 | | func.finishAir(inst, block_result, &.{}); |
| 3499 | return func.finishAir(inst, block_result, &.{}); |
| 3521 | 3500 | } |
| 3522 | 3501 | |
| 3523 | 3502 | /// appends a new wasm block to the code section and increases the `block_depth` by 1 |
| ... | ... | @@ -3549,7 +3528,7 @@ fn airLoop(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3549 | 3528 | try func.addLabel(.br, 0); |
| 3550 | 3529 | try func.endBlock(); |
| 3551 | 3530 | |
| 3552 | | func.finishAir(inst, .none, &.{}); |
| 3531 | return func.finishAir(inst, .none, &.{}); |
| 3553 | 3532 | } |
| 3554 | 3533 | |
| 3555 | 3534 | fn airCondBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -3593,7 +3572,7 @@ fn airCondBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3593 | 3572 | try func.genBody(then_body); |
| 3594 | 3573 | } |
| 3595 | 3574 | |
| 3596 | | func.finishAir(inst, .none, &.{}); |
| 3575 | return func.finishAir(inst, .none, &.{}); |
| 3597 | 3576 | } |
| 3598 | 3577 | |
| 3599 | 3578 | fn airCmp(func: *CodeGen, inst: Air.Inst.Index, op: std.math.CompareOperator) InnerError!void { |
| ... | ... | @@ -3602,8 +3581,8 @@ fn airCmp(func: *CodeGen, inst: Air.Inst.Index, op: std.math.CompareOperator) In |
| 3602 | 3581 | const lhs = try func.resolveInst(bin_op.lhs); |
| 3603 | 3582 | const rhs = try func.resolveInst(bin_op.rhs); |
| 3604 | 3583 | const operand_ty = func.typeOf(bin_op.lhs); |
| 3605 | | const result = try (try func.cmp(lhs, rhs, operand_ty, op)).toLocal(func, Type.u32); // comparison result is always 32 bits |
| 3606 | | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 3584 | const result = try func.cmp(lhs, rhs, operand_ty, op); |
| 3585 | return func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 3607 | 3586 | } |
| 3608 | 3587 | |
| 3609 | 3588 | /// Compares two operands. |
| ... | ... | @@ -3654,7 +3633,7 @@ fn cmp(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: std.math.CompareO |
| 3654 | 3633 | }); |
| 3655 | 3634 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 3656 | 3635 | |
| 3657 | | return WValue{ .stack = {} }; |
| 3636 | return .stack; |
| 3658 | 3637 | } |
| 3659 | 3638 | |
| 3660 | 3639 | /// Compares two floats. |
| ... | ... | @@ -3694,7 +3673,7 @@ fn cmpFloat(func: *CodeGen, ty: Type, lhs: WValue, rhs: WValue, cmp_op: std.math |
| 3694 | 3673 | }) catch unreachable; |
| 3695 | 3674 | |
| 3696 | 3675 | const result = try func.callIntrinsic(fn_name, &.{ ty.ip_index, ty.ip_index }, Type.bool, &.{ lhs, rhs }); |
| 3697 | | return func.cmp(result, WValue{ .imm32 = 0 }, Type.i32, cmp_op); |
| 3676 | return func.cmp(result, .{ .imm32 = 0 }, Type.i32, cmp_op); |
| 3698 | 3677 | }, |
| 3699 | 3678 | else => unreachable, |
| 3700 | 3679 | } |
| ... | ... | @@ -3709,7 +3688,7 @@ fn airCmpLtErrorsLen(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3709 | 3688 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3710 | 3689 | const operand = try func.resolveInst(un_op); |
| 3711 | 3690 | const sym_index = try func.bin_file.getGlobalSymbol("__zig_errors_len", null); |
| 3712 | | const errors_len = WValue{ .memory = @intFromEnum(sym_index) }; |
| 3691 | const errors_len = .{ .memory = @intFromEnum(sym_index) }; |
| 3713 | 3692 | |
| 3714 | 3693 | try func.emitWValue(operand); |
| 3715 | 3694 | const pt = func.pt; |
| ... | ... | @@ -3717,7 +3696,7 @@ fn airCmpLtErrorsLen(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3717 | 3696 | const errors_len_val = try func.load(errors_len, err_int_ty, 0); |
| 3718 | 3697 | const result = try func.cmp(.stack, errors_len_val, err_int_ty, .lt); |
| 3719 | 3698 | |
| 3720 | | return func.finishAir(inst, try result.toLocal(func, Type.bool), &.{un_op}); |
| 3699 | return func.finishAir(inst, result, &.{un_op}); |
| 3721 | 3700 | } |
| 3722 | 3701 | |
| 3723 | 3702 | fn airBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -3740,7 +3719,7 @@ fn airBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3740 | 3719 | const idx: u32 = func.block_depth - block.label; |
| 3741 | 3720 | try func.addLabel(.br, idx); |
| 3742 | 3721 | |
| 3743 | | func.finishAir(inst, .none, &.{br.operand}); |
| 3722 | return func.finishAir(inst, .none, &.{br.operand}); |
| 3744 | 3723 | } |
| 3745 | 3724 | |
| 3746 | 3725 | fn airNot(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -3772,7 +3751,7 @@ fn airNot(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3772 | 3751 | .signed => ~@as(u32, 0), |
| 3773 | 3752 | }); |
| 3774 | 3753 | try func.addTag(.i32_xor); |
| 3775 | | break :result try @as(WValue, .stack).toLocal(func, operand_ty); |
| 3754 | break :result .stack; |
| 3776 | 3755 | }, |
| 3777 | 3756 | 64 => { |
| 3778 | 3757 | try func.emitWValue(operand); |
| ... | ... | @@ -3781,7 +3760,7 @@ fn airNot(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3781 | 3760 | .signed => ~@as(u64, 0), |
| 3782 | 3761 | }); |
| 3783 | 3762 | try func.addTag(.i64_xor); |
| 3784 | | break :result try @as(WValue, .stack).toLocal(func, operand_ty); |
| 3763 | break :result .stack; |
| 3785 | 3764 | }, |
| 3786 | 3765 | 128 => { |
| 3787 | 3766 | const ptr = try func.allocStack(operand_ty); |
| ... | ... | @@ -3807,24 +3786,24 @@ fn airNot(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3807 | 3786 | } |
| 3808 | 3787 | } |
| 3809 | 3788 | }; |
| 3810 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 3789 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 3811 | 3790 | } |
| 3812 | 3791 | |
| 3813 | 3792 | fn airTrap(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3814 | 3793 | try func.addTag(.@"unreachable"); |
| 3815 | | func.finishAir(inst, .none, &.{}); |
| 3794 | return func.finishAir(inst, .none, &.{}); |
| 3816 | 3795 | } |
| 3817 | 3796 | |
| 3818 | 3797 | fn airBreakpoint(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3819 | 3798 | // unsupported by wasm itfunc. Can be implemented once we support DWARF |
| 3820 | 3799 | // for wasm |
| 3821 | 3800 | try func.addTag(.@"unreachable"); |
| 3822 | | func.finishAir(inst, .none, &.{}); |
| 3801 | return func.finishAir(inst, .none, &.{}); |
| 3823 | 3802 | } |
| 3824 | 3803 | |
| 3825 | 3804 | fn airUnreachable(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3826 | 3805 | try func.addTag(.@"unreachable"); |
| 3827 | | func.finishAir(inst, .none, &.{}); |
| 3806 | return func.finishAir(inst, .none, &.{}); |
| 3828 | 3807 | } |
| 3829 | 3808 | |
| 3830 | 3809 | fn airBitcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -3841,35 +3820,34 @@ fn airBitcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3841 | 3820 | |
| 3842 | 3821 | const result = result: { |
| 3843 | 3822 | if (given_ty.isAnyFloat() or wanted_ty.isAnyFloat()) { |
| 3844 | | const bitcast_result = try func.bitcast(wanted_ty, given_ty, operand); |
| 3845 | | break :result try bitcast_result.toLocal(func, wanted_ty); |
| 3823 | break :result try func.bitcast(wanted_ty, given_ty, operand); |
| 3846 | 3824 | } |
| 3847 | 3825 | |
| 3848 | 3826 | if (isByRef(given_ty, pt) and !isByRef(wanted_ty, pt)) { |
| 3849 | 3827 | const loaded_memory = try func.load(operand, wanted_ty, 0); |
| 3850 | 3828 | if (needs_wrapping) { |
| 3851 | | break :result try (try func.wrapOperand(loaded_memory, wanted_ty)).toLocal(func, wanted_ty); |
| 3829 | break :result try func.wrapOperand(loaded_memory, wanted_ty); |
| 3852 | 3830 | } else { |
| 3853 | | break :result try loaded_memory.toLocal(func, wanted_ty); |
| 3831 | break :result loaded_memory; |
| 3854 | 3832 | } |
| 3855 | 3833 | } |
| 3856 | 3834 | if (!isByRef(given_ty, pt) and isByRef(wanted_ty, pt)) { |
| 3857 | 3835 | const stack_memory = try func.allocStack(wanted_ty); |
| 3858 | 3836 | try func.store(stack_memory, operand, given_ty, 0); |
| 3859 | 3837 | if (needs_wrapping) { |
| 3860 | | break :result try (try func.wrapOperand(stack_memory, wanted_ty)).toLocal(func, wanted_ty); |
| 3838 | break :result try func.wrapOperand(stack_memory, wanted_ty); |
| 3861 | 3839 | } else { |
| 3862 | 3840 | break :result stack_memory; |
| 3863 | 3841 | } |
| 3864 | 3842 | } |
| 3865 | 3843 | |
| 3866 | 3844 | if (needs_wrapping) { |
| 3867 | | break :result try (try func.wrapOperand(operand, wanted_ty)).toLocal(func, wanted_ty); |
| 3845 | break :result try func.wrapOperand(operand, wanted_ty); |
| 3868 | 3846 | } |
| 3869 | 3847 | |
| 3870 | 3848 | break :result func.reuseOperand(ty_op.operand, operand); |
| 3871 | 3849 | }; |
| 3872 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 3850 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 3873 | 3851 | } |
| 3874 | 3852 | |
| 3875 | 3853 | fn bitcast(func: *CodeGen, wanted_ty: Type, given_ty: Type, operand: WValue) InnerError!WValue { |
| ... | ... | @@ -3888,7 +3866,7 @@ fn bitcast(func: *CodeGen, wanted_ty: Type, given_ty: Type, operand: WValue) Inn |
| 3888 | 3866 | }); |
| 3889 | 3867 | try func.emitWValue(operand); |
| 3890 | 3868 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 3891 | | return WValue{ .stack = {} }; |
| 3869 | return .stack; |
| 3892 | 3870 | } |
| 3893 | 3871 | |
| 3894 | 3872 | fn airStructFieldPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -3901,7 +3879,7 @@ fn airStructFieldPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3901 | 3879 | const struct_ptr_ty = func.typeOf(extra.data.struct_operand); |
| 3902 | 3880 | const struct_ty = struct_ptr_ty.childType(mod); |
| 3903 | 3881 | const result = try func.structFieldPtr(inst, extra.data.struct_operand, struct_ptr, struct_ptr_ty, struct_ty, extra.data.field_index); |
| 3904 | | func.finishAir(inst, result, &.{extra.data.struct_operand}); |
| 3882 | return func.finishAir(inst, result, &.{extra.data.struct_operand}); |
| 3905 | 3883 | } |
| 3906 | 3884 | |
| 3907 | 3885 | fn airStructFieldPtrIndex(func: *CodeGen, inst: Air.Inst.Index, index: u32) InnerError!void { |
| ... | ... | @@ -3913,7 +3891,7 @@ fn airStructFieldPtrIndex(func: *CodeGen, inst: Air.Inst.Index, index: u32) Inne |
| 3913 | 3891 | const struct_ty = struct_ptr_ty.childType(mod); |
| 3914 | 3892 | |
| 3915 | 3893 | const result = try func.structFieldPtr(inst, ty_op.operand, struct_ptr, struct_ptr_ty, struct_ty, index); |
| 3916 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 3894 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 3917 | 3895 | } |
| 3918 | 3896 | |
| 3919 | 3897 | fn structFieldPtr( |
| ... | ... | @@ -3950,7 +3928,7 @@ fn structFieldPtr( |
| 3950 | 3928 | } |
| 3951 | 3929 | switch (struct_ptr) { |
| 3952 | 3930 | .stack_offset => |stack_offset| { |
| 3953 | | return WValue{ .stack_offset = .{ .value = stack_offset.value + @as(u32, @intCast(offset)), .references = 1 } }; |
| 3931 | return .{ .stack_offset = .{ .value = stack_offset.value + @as(u32, @intCast(offset)), .references = 1 } }; |
| 3954 | 3932 | }, |
| 3955 | 3933 | else => return func.buildPointerOffset(struct_ptr, offset, .new), |
| 3956 | 3934 | } |
| ... | ... | @@ -3969,7 +3947,7 @@ fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3969 | 3947 | const field_ty = struct_ty.structFieldType(field_index, mod); |
| 3970 | 3948 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) return func.finishAir(inst, .none, &.{struct_field.struct_operand}); |
| 3971 | 3949 | |
| 3972 | | const result = switch (struct_ty.containerLayout(mod)) { |
| 3950 | const result: WValue = switch (struct_ty.containerLayout(mod)) { |
| 3973 | 3951 | .@"packed" => switch (struct_ty.zigTypeTag(mod)) { |
| 3974 | 3952 | .Struct => result: { |
| 3975 | 3953 | const packed_struct = mod.typeToPackedStruct(struct_ty).?; |
| ... | ... | @@ -3978,10 +3956,10 @@ fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3978 | 3956 | const wasm_bits = toWasmBits(backing_ty.intInfo(mod).bits) orelse { |
| 3979 | 3957 | return func.fail("TODO: airStructFieldVal for packed structs larger than 128 bits", .{}); |
| 3980 | 3958 | }; |
| 3981 | | const const_wvalue = if (wasm_bits == 32) |
| 3982 | | WValue{ .imm32 = offset } |
| 3959 | const const_wvalue: WValue = if (wasm_bits == 32) |
| 3960 | .{ .imm32 = offset } |
| 3983 | 3961 | else if (wasm_bits == 64) |
| 3984 | | WValue{ .imm64 = offset } |
| 3962 | .{ .imm64 = offset } |
| 3985 | 3963 | else |
| 3986 | 3964 | return func.fail("TODO: airStructFieldVal for packed structs larger than 64 bits", .{}); |
| 3987 | 3965 | |
| ... | ... | @@ -3994,25 +3972,21 @@ fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3994 | 3972 | if (field_ty.zigTypeTag(mod) == .Float) { |
| 3995 | 3973 | const int_type = try pt.intType(.unsigned, @as(u16, @intCast(field_ty.bitSize(pt)))); |
| 3996 | 3974 | const truncated = try func.trunc(shifted_value, int_type, backing_ty); |
| 3997 | | const bitcasted = try func.bitcast(field_ty, int_type, truncated); |
| 3998 | | break :result try bitcasted.toLocal(func, field_ty); |
| 3975 | break :result try func.bitcast(field_ty, int_type, truncated); |
| 3999 | 3976 | } else if (field_ty.isPtrAtRuntime(mod) and packed_struct.field_types.len == 1) { |
| 4000 | 3977 | // In this case we do not have to perform any transformations, |
| 4001 | 3978 | // we can simply reuse the operand. |
| 4002 | 3979 | break :result func.reuseOperand(struct_field.struct_operand, operand); |
| 4003 | 3980 | } else if (field_ty.isPtrAtRuntime(mod)) { |
| 4004 | 3981 | const int_type = try pt.intType(.unsigned, @as(u16, @intCast(field_ty.bitSize(pt)))); |
| 4005 | | const truncated = try func.trunc(shifted_value, int_type, backing_ty); |
| 4006 | | break :result try truncated.toLocal(func, field_ty); |
| 3982 | break :result try func.trunc(shifted_value, int_type, backing_ty); |
| 4007 | 3983 | } |
| 4008 | | const truncated = try func.trunc(shifted_value, field_ty, backing_ty); |
| 4009 | | break :result try truncated.toLocal(func, field_ty); |
| 3984 | break :result try func.trunc(shifted_value, field_ty, backing_ty); |
| 4010 | 3985 | }, |
| 4011 | 3986 | .Union => result: { |
| 4012 | 3987 | if (isByRef(struct_ty, pt)) { |
| 4013 | 3988 | if (!isByRef(field_ty, pt)) { |
| 4014 | | const val = try func.load(operand, field_ty, 0); |
| 4015 | | break :result try val.toLocal(func, field_ty); |
| 3989 | break :result try func.load(operand, field_ty, 0); |
| 4016 | 3990 | } else { |
| 4017 | 3991 | const new_stack_val = try func.allocStack(field_ty); |
| 4018 | 3992 | try func.store(new_stack_val, operand, field_ty, 0); |
| ... | ... | @@ -4024,15 +3998,12 @@ fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4024 | 3998 | if (field_ty.zigTypeTag(mod) == .Float) { |
| 4025 | 3999 | const int_type = try pt.intType(.unsigned, @as(u16, @intCast(field_ty.bitSize(pt)))); |
| 4026 | 4000 | const truncated = try func.trunc(operand, int_type, union_int_type); |
| 4027 | | const bitcasted = try func.bitcast(field_ty, int_type, truncated); |
| 4028 | | break :result try bitcasted.toLocal(func, field_ty); |
| 4001 | break :result try func.bitcast(field_ty, int_type, truncated); |
| 4029 | 4002 | } else if (field_ty.isPtrAtRuntime(mod)) { |
| 4030 | 4003 | const int_type = try pt.intType(.unsigned, @as(u16, @intCast(field_ty.bitSize(pt)))); |
| 4031 | | const truncated = try func.trunc(operand, int_type, union_int_type); |
| 4032 | | break :result try truncated.toLocal(func, field_ty); |
| 4004 | break :result try func.trunc(operand, int_type, union_int_type); |
| 4033 | 4005 | } |
| 4034 | | const truncated = try func.trunc(operand, field_ty, union_int_type); |
| 4035 | | break :result try truncated.toLocal(func, field_ty); |
| 4006 | break :result try func.trunc(operand, field_ty, union_int_type); |
| 4036 | 4007 | }, |
| 4037 | 4008 | else => unreachable, |
| 4038 | 4009 | }, |
| ... | ... | @@ -4043,17 +4014,16 @@ fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4043 | 4014 | if (isByRef(field_ty, pt)) { |
| 4044 | 4015 | switch (operand) { |
| 4045 | 4016 | .stack_offset => |stack_offset| { |
| 4046 | | break :result WValue{ .stack_offset = .{ .value = stack_offset.value + offset, .references = 1 } }; |
| 4017 | break :result .{ .stack_offset = .{ .value = stack_offset.value + offset, .references = 1 } }; |
| 4047 | 4018 | }, |
| 4048 | 4019 | else => break :result try func.buildPointerOffset(operand, offset, .new), |
| 4049 | 4020 | } |
| 4050 | 4021 | } |
| 4051 | | const field = try func.load(operand, field_ty, offset); |
| 4052 | | break :result try field.toLocal(func, field_ty); |
| 4022 | break :result try func.load(operand, field_ty, offset); |
| 4053 | 4023 | }, |
| 4054 | 4024 | }; |
| 4055 | 4025 | |
| 4056 | | func.finishAir(inst, result, &.{struct_field.struct_operand}); |
| 4026 | return func.finishAir(inst, result, &.{struct_field.struct_operand}); |
| 4057 | 4027 | } |
| 4058 | 4028 | |
| 4059 | 4029 | fn airSwitchBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -4234,7 +4204,7 @@ fn airSwitchBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4234 | 4204 | try func.genBody(else_body); |
| 4235 | 4205 | try func.endBlock(); |
| 4236 | 4206 | } |
| 4237 | | func.finishAir(inst, .none, &.{}); |
| 4207 | return func.finishAir(inst, .none, &.{}); |
| 4238 | 4208 | } |
| 4239 | 4209 | |
| 4240 | 4210 | fn airIsErr(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!void { |
| ... | ... | @@ -4245,11 +4215,11 @@ fn airIsErr(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerErro |
| 4245 | 4215 | const err_union_ty = func.typeOf(un_op); |
| 4246 | 4216 | const pl_ty = err_union_ty.errorUnionPayload(mod); |
| 4247 | 4217 | |
| 4248 | | const result = result: { |
| 4218 | const result: WValue = result: { |
| 4249 | 4219 | if (err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) { |
| 4250 | 4220 | switch (opcode) { |
| 4251 | | .i32_ne => break :result WValue{ .imm32 = 0 }, |
| 4252 | | .i32_eq => break :result WValue{ .imm32 = 1 }, |
| 4221 | .i32_ne => break :result .{ .imm32 = 0 }, |
| 4222 | .i32_eq => break :result .{ .imm32 = 1 }, |
| 4253 | 4223 | else => unreachable, |
| 4254 | 4224 | } |
| 4255 | 4225 | } |
| ... | ... | @@ -4265,12 +4235,9 @@ fn airIsErr(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerErro |
| 4265 | 4235 | // Compare the error value with '0' |
| 4266 | 4236 | try func.addImm32(0); |
| 4267 | 4237 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 4268 | | |
| 4269 | | const is_err_tmp = try func.allocLocal(Type.i32); |
| 4270 | | try func.addLabel(.local_set, is_err_tmp.local.value); |
| 4271 | | break :result is_err_tmp; |
| 4238 | break :result .stack; |
| 4272 | 4239 | }; |
| 4273 | | func.finishAir(inst, result, &.{un_op}); |
| 4240 | return func.finishAir(inst, result, &.{un_op}); |
| 4274 | 4241 | } |
| 4275 | 4242 | |
| 4276 | 4243 | fn airUnwrapErrUnionPayload(func: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void { |
| ... | ... | @@ -4283,12 +4250,12 @@ fn airUnwrapErrUnionPayload(func: *CodeGen, inst: Air.Inst.Index, op_is_ptr: boo |
| 4283 | 4250 | const err_ty = if (op_is_ptr) op_ty.childType(mod) else op_ty; |
| 4284 | 4251 | const payload_ty = err_ty.errorUnionPayload(mod); |
| 4285 | 4252 | |
| 4286 | | const result = result: { |
| 4253 | const result: WValue = result: { |
| 4287 | 4254 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { |
| 4288 | 4255 | if (op_is_ptr) { |
| 4289 | 4256 | break :result func.reuseOperand(ty_op.operand, operand); |
| 4290 | 4257 | } |
| 4291 | | break :result WValue{ .none = {} }; |
| 4258 | break :result .none; |
| 4292 | 4259 | } |
| 4293 | 4260 | |
| 4294 | 4261 | const pl_offset = @as(u32, @intCast(errUnionPayloadOffset(payload_ty, pt))); |
| ... | ... | @@ -4296,10 +4263,9 @@ fn airUnwrapErrUnionPayload(func: *CodeGen, inst: Air.Inst.Index, op_is_ptr: boo |
| 4296 | 4263 | break :result try func.buildPointerOffset(operand, pl_offset, .new); |
| 4297 | 4264 | } |
| 4298 | 4265 | |
| 4299 | | const payload = try func.load(operand, payload_ty, pl_offset); |
| 4300 | | break :result try payload.toLocal(func, payload_ty); |
| 4266 | break :result try func.load(operand, payload_ty, pl_offset); |
| 4301 | 4267 | }; |
| 4302 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4268 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 4303 | 4269 | } |
| 4304 | 4270 | |
| 4305 | 4271 | fn airUnwrapErrUnionError(func: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void { |
| ... | ... | @@ -4312,19 +4278,18 @@ fn airUnwrapErrUnionError(func: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) |
| 4312 | 4278 | const err_ty = if (op_is_ptr) op_ty.childType(mod) else op_ty; |
| 4313 | 4279 | const payload_ty = err_ty.errorUnionPayload(mod); |
| 4314 | 4280 | |
| 4315 | | const result = result: { |
| 4281 | const result: WValue = result: { |
| 4316 | 4282 | if (err_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) { |
| 4317 | | break :result WValue{ .imm32 = 0 }; |
| 4283 | break :result .{ .imm32 = 0 }; |
| 4318 | 4284 | } |
| 4319 | 4285 | |
| 4320 | 4286 | if (op_is_ptr or !payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { |
| 4321 | 4287 | break :result func.reuseOperand(ty_op.operand, operand); |
| 4322 | 4288 | } |
| 4323 | 4289 | |
| 4324 | | const error_val = try func.load(operand, Type.anyerror, @intCast(errUnionErrorOffset(payload_ty, pt))); |
| 4325 | | break :result try error_val.toLocal(func, Type.anyerror); |
| 4290 | break :result try func.load(operand, Type.anyerror, @intCast(errUnionErrorOffset(payload_ty, pt))); |
| 4326 | 4291 | }; |
| 4327 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4292 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 4328 | 4293 | } |
| 4329 | 4294 | |
| 4330 | 4295 | fn airWrapErrUnionPayload(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -4354,7 +4319,7 @@ fn airWrapErrUnionPayload(func: *CodeGen, inst: Air.Inst.Index) InnerError!void |
| 4354 | 4319 | }); |
| 4355 | 4320 | break :result err_union; |
| 4356 | 4321 | }; |
| 4357 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4322 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 4358 | 4323 | } |
| 4359 | 4324 | |
| 4360 | 4325 | fn airWrapErrUnionErr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -4382,7 +4347,7 @@ fn airWrapErrUnionErr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4382 | 4347 | |
| 4383 | 4348 | break :result err_union; |
| 4384 | 4349 | }; |
| 4385 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4350 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 4386 | 4351 | } |
| 4387 | 4352 | |
| 4388 | 4353 | fn airIntcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -4405,9 +4370,9 @@ fn airIntcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4405 | 4370 | const result = if (op_bits == wanted_bits) |
| 4406 | 4371 | func.reuseOperand(ty_op.operand, operand) |
| 4407 | 4372 | else |
| 4408 | | try (try func.intcast(operand, operand_ty, ty)).toLocal(func, ty); |
| 4373 | try func.intcast(operand, operand_ty, ty); |
| 4409 | 4374 | |
| 4410 | | func.finishAir(inst, result, &.{}); |
| 4375 | return func.finishAir(inst, result, &.{}); |
| 4411 | 4376 | } |
| 4412 | 4377 | |
| 4413 | 4378 | /// Upcasts or downcasts an integer based on the given and wanted types, |
| ... | ... | @@ -4472,9 +4437,8 @@ fn airIsNull(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: |
| 4472 | 4437 | |
| 4473 | 4438 | const op_ty = func.typeOf(un_op); |
| 4474 | 4439 | const optional_ty = if (op_kind == .ptr) op_ty.childType(mod) else op_ty; |
| 4475 | | const is_null = try func.isNull(operand, optional_ty, opcode); |
| 4476 | | const result = try is_null.toLocal(func, optional_ty); |
| 4477 | | func.finishAir(inst, result, &.{un_op}); |
| 4440 | const result = try func.isNull(operand, optional_ty, opcode); |
| 4441 | return func.finishAir(inst, result, &.{un_op}); |
| 4478 | 4442 | } |
| 4479 | 4443 | |
| 4480 | 4444 | /// For a given type and operand, checks if it's considered `null`. |
| ... | ... | @@ -4505,7 +4469,7 @@ fn isNull(func: *CodeGen, operand: WValue, optional_ty: Type, opcode: wasm.Opcod |
| 4505 | 4469 | try func.addImm32(0); |
| 4506 | 4470 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 4507 | 4471 | |
| 4508 | | return WValue{ .stack = {} }; |
| 4472 | return .stack; |
| 4509 | 4473 | } |
| 4510 | 4474 | |
| 4511 | 4475 | fn airOptionalPayload(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -4526,10 +4490,9 @@ fn airOptionalPayload(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4526 | 4490 | break :result try func.buildPointerOffset(operand, 0, .new); |
| 4527 | 4491 | } |
| 4528 | 4492 | |
| 4529 | | const payload = try func.load(operand, payload_ty, 0); |
| 4530 | | break :result try payload.toLocal(func, payload_ty); |
| 4493 | break :result try func.load(operand, payload_ty, 0); |
| 4531 | 4494 | }; |
| 4532 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4495 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 4533 | 4496 | } |
| 4534 | 4497 | |
| 4535 | 4498 | fn airOptionalPayloadPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -4547,7 +4510,7 @@ fn airOptionalPayloadPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4547 | 4510 | |
| 4548 | 4511 | break :result try func.buildPointerOffset(operand, 0, .new); |
| 4549 | 4512 | }; |
| 4550 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4513 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 4551 | 4514 | } |
| 4552 | 4515 | |
| 4553 | 4516 | fn airOptionalPayloadPtrSet(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -4612,7 +4575,7 @@ fn airWrapOptional(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4612 | 4575 | break :result result_ptr; |
| 4613 | 4576 | }; |
| 4614 | 4577 | |
| 4615 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4578 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 4616 | 4579 | } |
| 4617 | 4580 | |
| 4618 | 4581 | fn airSlice(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -4627,14 +4590,14 @@ fn airSlice(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4627 | 4590 | try func.store(slice, lhs, Type.usize, 0); |
| 4628 | 4591 | try func.store(slice, rhs, Type.usize, func.ptrSize()); |
| 4629 | 4592 | |
| 4630 | | func.finishAir(inst, slice, &.{ bin_op.lhs, bin_op.rhs }); |
| 4593 | return func.finishAir(inst, slice, &.{ bin_op.lhs, bin_op.rhs }); |
| 4631 | 4594 | } |
| 4632 | 4595 | |
| 4633 | 4596 | fn airSliceLen(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4634 | 4597 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4635 | 4598 | |
| 4636 | 4599 | const operand = try func.resolveInst(ty_op.operand); |
| 4637 | | func.finishAir(inst, try func.sliceLen(operand), &.{ty_op.operand}); |
| 4600 | return func.finishAir(inst, try func.sliceLen(operand), &.{ty_op.operand}); |
| 4638 | 4601 | } |
| 4639 | 4602 | |
| 4640 | 4603 | fn airSliceElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -4657,15 +4620,12 @@ fn airSliceElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4657 | 4620 | try func.addTag(.i32_mul); |
| 4658 | 4621 | try func.addTag(.i32_add); |
| 4659 | 4622 | |
| 4660 | | const result_ptr = try func.allocLocal(Type.usize); |
| 4661 | | try func.addLabel(.local_set, result_ptr.local.value); |
| 4662 | | |
| 4663 | | const result = if (!isByRef(elem_ty, pt)) result: { |
| 4664 | | const elem_val = try func.load(result_ptr, elem_ty, 0); |
| 4665 | | break :result try elem_val.toLocal(func, elem_ty); |
| 4666 | | } else result_ptr; |
| 4623 | const elem_result = if (isByRef(elem_ty, pt)) |
| 4624 | .stack |
| 4625 | else |
| 4626 | try func.load(.stack, elem_ty, 0); |
| 4667 | 4627 | |
| 4668 | | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 4628 | return func.finishAir(inst, elem_result, &.{ bin_op.lhs, bin_op.rhs }); |
| 4669 | 4629 | } |
| 4670 | 4630 | |
| 4671 | 4631 | fn airSliceElemPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -4688,15 +4648,13 @@ fn airSliceElemPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4688 | 4648 | try func.addTag(.i32_mul); |
| 4689 | 4649 | try func.addTag(.i32_add); |
| 4690 | 4650 | |
| 4691 | | const result = try func.allocLocal(Type.i32); |
| 4692 | | try func.addLabel(.local_set, result.local.value); |
| 4693 | | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 4651 | return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs }); |
| 4694 | 4652 | } |
| 4695 | 4653 | |
| 4696 | 4654 | fn airSlicePtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4697 | 4655 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4698 | 4656 | const operand = try func.resolveInst(ty_op.operand); |
| 4699 | | func.finishAir(inst, try func.slicePtr(operand), &.{ty_op.operand}); |
| 4657 | return func.finishAir(inst, try func.slicePtr(operand), &.{ty_op.operand}); |
| 4700 | 4658 | } |
| 4701 | 4659 | |
| 4702 | 4660 | fn slicePtr(func: *CodeGen, operand: WValue) InnerError!WValue { |
| ... | ... | @@ -4717,7 +4675,7 @@ fn airTrunc(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4717 | 4675 | const op_ty = func.typeOf(ty_op.operand); |
| 4718 | 4676 | |
| 4719 | 4677 | const result = try func.trunc(operand, wanted_ty, op_ty); |
| 4720 | | func.finishAir(inst, try result.toLocal(func, wanted_ty), &.{ty_op.operand}); |
| 4678 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 4721 | 4679 | } |
| 4722 | 4680 | |
| 4723 | 4681 | /// Truncates a given operand to a given type, discarding any overflown bits. |
| ... | ... | @@ -4743,7 +4701,7 @@ fn airIntFromBool(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4743 | 4701 | const operand = try func.resolveInst(un_op); |
| 4744 | 4702 | const result = func.reuseOperand(un_op, operand); |
| 4745 | 4703 | |
| 4746 | | func.finishAir(inst, result, &.{un_op}); |
| 4704 | return func.finishAir(inst, result, &.{un_op}); |
| 4747 | 4705 | } |
| 4748 | 4706 | |
| 4749 | 4707 | fn airArrayToSlice(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -4764,10 +4722,10 @@ fn airArrayToSlice(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4764 | 4722 | } |
| 4765 | 4723 | |
| 4766 | 4724 | // store the length of the array in the slice |
| 4767 | | const len = WValue{ .imm32 = @as(u32, @intCast(array_ty.arrayLen(mod))) }; |
| 4768 | | try func.store(slice_local, len, Type.usize, func.ptrSize()); |
| 4725 | const array_len: u32 = @intCast(array_ty.arrayLen(mod)); |
| 4726 | try func.store(slice_local, .{ .imm32 = array_len }, Type.usize, func.ptrSize()); |
| 4769 | 4727 | |
| 4770 | | func.finishAir(inst, slice_local, &.{ty_op.operand}); |
| 4728 | return func.finishAir(inst, slice_local, &.{ty_op.operand}); |
| 4771 | 4729 | } |
| 4772 | 4730 | |
| 4773 | 4731 | fn airIntFromPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -4783,7 +4741,7 @@ fn airIntFromPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4783 | 4741 | .stack_offset => try func.buildPointerOffset(operand, 0, .new), |
| 4784 | 4742 | else => func.reuseOperand(un_op, operand), |
| 4785 | 4743 | }; |
| 4786 | | func.finishAir(inst, result, &.{un_op}); |
| 4744 | return func.finishAir(inst, result, &.{un_op}); |
| 4787 | 4745 | } |
| 4788 | 4746 | |
| 4789 | 4747 | fn airPtrElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -4810,18 +4768,12 @@ fn airPtrElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4810 | 4768 | try func.addTag(.i32_mul); |
| 4811 | 4769 | try func.addTag(.i32_add); |
| 4812 | 4770 | |
| 4813 | | const elem_result = val: { |
| 4814 | | var result = try func.allocLocal(Type.usize); |
| 4815 | | try func.addLabel(.local_set, result.local.value); |
| 4816 | | if (isByRef(elem_ty, pt)) { |
| 4817 | | break :val result; |
| 4818 | | } |
| 4819 | | defer result.free(func); // only free if it's not returned like above |
| 4771 | const elem_result = if (isByRef(elem_ty, pt)) |
| 4772 | .stack |
| 4773 | else |
| 4774 | try func.load(.stack, elem_ty, 0); |
| 4820 | 4775 | |
| 4821 | | const elem_val = try func.load(result, elem_ty, 0); |
| 4822 | | break :val try elem_val.toLocal(func, elem_ty); |
| 4823 | | }; |
| 4824 | | func.finishAir(inst, elem_result, &.{ bin_op.lhs, bin_op.rhs }); |
| 4776 | return func.finishAir(inst, elem_result, &.{ bin_op.lhs, bin_op.rhs }); |
| 4825 | 4777 | } |
| 4826 | 4778 | |
| 4827 | 4779 | fn airPtrElemPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -4850,9 +4802,7 @@ fn airPtrElemPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4850 | 4802 | try func.addTag(.i32_mul); |
| 4851 | 4803 | try func.addTag(.i32_add); |
| 4852 | 4804 | |
| 4853 | | const result = try func.allocLocal(Type.i32); |
| 4854 | | try func.addLabel(.local_set, result.local.value); |
| 4855 | | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 4805 | return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs }); |
| 4856 | 4806 | } |
| 4857 | 4807 | |
| 4858 | 4808 | fn airPtrBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| ... | ... | @@ -4879,9 +4829,7 @@ fn airPtrBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 4879 | 4829 | try func.addTag(Mir.Inst.Tag.fromOpcode(mul_opcode)); |
| 4880 | 4830 | try func.addTag(Mir.Inst.Tag.fromOpcode(bin_opcode)); |
| 4881 | 4831 | |
| 4882 | | const result = try func.allocLocal(Type.usize); |
| 4883 | | try func.addLabel(.local_set, result.local.value); |
| 4884 | | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 4832 | return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs }); |
| 4885 | 4833 | } |
| 4886 | 4834 | |
| 4887 | 4835 | fn airMemset(func: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void { |
| ... | ... | @@ -4911,7 +4859,7 @@ fn airMemset(func: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void |
| 4911 | 4859 | const dst_ptr = try func.sliceOrArrayPtr(ptr, ptr_ty); |
| 4912 | 4860 | try func.memset(elem_ty, dst_ptr, len, value); |
| 4913 | 4861 | |
| 4914 | | func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 4862 | return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 4915 | 4863 | } |
| 4916 | 4864 | |
| 4917 | 4865 | /// Sets a region of memory at `ptr` to the value of `value` |
| ... | ... | @@ -4932,9 +4880,9 @@ fn memset(func: *CodeGen, elem_ty: Type, ptr: WValue, len: WValue, value: WValue |
| 4932 | 4880 | return; |
| 4933 | 4881 | } |
| 4934 | 4882 | |
| 4935 | | const final_len = switch (len) { |
| 4936 | | .imm32 => |val| WValue{ .imm32 = val * abi_size }, |
| 4937 | | .imm64 => |val| WValue{ .imm64 = val * abi_size }, |
| 4883 | const final_len: WValue = switch (len) { |
| 4884 | .imm32 => |val| .{ .imm32 = val * abi_size }, |
| 4885 | .imm64 => |val| .{ .imm64 = val * abi_size }, |
| 4938 | 4886 | else => if (abi_size != 1) blk: { |
| 4939 | 4887 | const new_len = try func.ensureAllocLocal(Type.usize); |
| 4940 | 4888 | try func.emitWValue(len); |
| ... | ... | @@ -5045,7 +4993,7 @@ fn airArrayElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5045 | 4993 | try func.mir_extra.appendSlice(func.gpa, &operands); |
| 5046 | 4994 | try func.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } }); |
| 5047 | 4995 | |
| 5048 | | return func.finishAir(inst, try WValue.toLocal(.stack, func, elem_ty), &.{ bin_op.lhs, bin_op.rhs }); |
| 4996 | return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs }); |
| 5049 | 4997 | }, |
| 5050 | 4998 | else => { |
| 5051 | 4999 | const stack_vec = try func.allocStack(array_ty); |
| ... | ... | @@ -5061,20 +5009,12 @@ fn airArrayElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5061 | 5009 | } |
| 5062 | 5010 | } |
| 5063 | 5011 | |
| 5064 | | const elem_result = val: { |
| 5065 | | var result = try func.allocLocal(Type.usize); |
| 5066 | | try func.addLabel(.local_set, result.local.value); |
| 5067 | | |
| 5068 | | if (isByRef(elem_ty, pt)) { |
| 5069 | | break :val result; |
| 5070 | | } |
| 5071 | | defer result.free(func); // only free if no longer needed and not returned like above |
| 5072 | | |
| 5073 | | const elem_val = try func.load(result, elem_ty, 0); |
| 5074 | | break :val try elem_val.toLocal(func, elem_ty); |
| 5075 | | }; |
| 5012 | const elem_result = if (isByRef(elem_ty, pt)) |
| 5013 | .stack |
| 5014 | else |
| 5015 | try func.load(.stack, elem_ty, 0); |
| 5076 | 5016 | |
| 5077 | | func.finishAir(inst, elem_result, &.{ bin_op.lhs, bin_op.rhs }); |
| 5017 | return func.finishAir(inst, elem_result, &.{ bin_op.lhs, bin_op.rhs }); |
| 5078 | 5018 | } |
| 5079 | 5019 | |
| 5080 | 5020 | fn airIntFromFloat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -5106,7 +5046,7 @@ fn airIntFromFloat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5106 | 5046 | target_util.compilerRtIntAbbrev(dest_bitsize), |
| 5107 | 5047 | }) catch unreachable; |
| 5108 | 5048 | |
| 5109 | | const result = try (try func.callIntrinsic(fn_name, &.{op_ty.ip_index}, dest_ty, &.{operand})).toLocal(func, dest_ty); |
| 5049 | const result = try func.callIntrinsic(fn_name, &.{op_ty.ip_index}, dest_ty, &.{operand}); |
| 5110 | 5050 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 5111 | 5051 | } |
| 5112 | 5052 | |
| ... | ... | @@ -5118,9 +5058,8 @@ fn airIntFromFloat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5118 | 5058 | .signedness = dest_info.signedness, |
| 5119 | 5059 | }); |
| 5120 | 5060 | try func.addTag(Mir.Inst.Tag.fromOpcode(op)); |
| 5121 | | const wrapped = try func.wrapOperand(.{ .stack = {} }, dest_ty); |
| 5122 | | const result = try wrapped.toLocal(func, dest_ty); |
| 5123 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5061 | const result = try func.wrapOperand(.stack, dest_ty); |
| 5062 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 5124 | 5063 | } |
| 5125 | 5064 | |
| 5126 | 5065 | fn airFloatFromInt(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -5152,7 +5091,7 @@ fn airFloatFromInt(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5152 | 5091 | target_util.compilerRtFloatAbbrev(dest_bits), |
| 5153 | 5092 | }) catch unreachable; |
| 5154 | 5093 | |
| 5155 | | const result = try (try func.callIntrinsic(fn_name, &.{op_ty.ip_index}, dest_ty, &.{operand})).toLocal(func, dest_ty); |
| 5094 | const result = try func.callIntrinsic(fn_name, &.{op_ty.ip_index}, dest_ty, &.{operand}); |
| 5156 | 5095 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 5157 | 5096 | } |
| 5158 | 5097 | |
| ... | ... | @@ -5165,9 +5104,7 @@ fn airFloatFromInt(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5165 | 5104 | }); |
| 5166 | 5105 | try func.addTag(Mir.Inst.Tag.fromOpcode(op)); |
| 5167 | 5106 | |
| 5168 | | const result = try func.allocLocal(dest_ty); |
| 5169 | | try func.addLabel(.local_set, result.local.value); |
| 5170 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5107 | return func.finishAir(inst, .stack, &.{ty_op.operand}); |
| 5171 | 5108 | } |
| 5172 | 5109 | |
| 5173 | 5110 | fn airSplat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -5191,7 +5128,6 @@ fn airSplat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5191 | 5128 | 64 => std.wasm.simdOpcode(.v128_load64_splat), |
| 5192 | 5129 | else => break :blk, // Cannot make use of simd-instructions |
| 5193 | 5130 | }; |
| 5194 | | const result = try func.allocLocal(ty); |
| 5195 | 5131 | try func.emitWValue(operand); |
| 5196 | 5132 | // TODO: Add helper functions for simd opcodes |
| 5197 | 5133 | const extra_index = @as(u32, @intCast(func.mir_extra.items.len)); |
| ... | ... | @@ -5202,8 +5138,7 @@ fn airSplat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5202 | 5138 | @intCast(elem_ty.abiAlignment(pt).toByteUnits().?), |
| 5203 | 5139 | }); |
| 5204 | 5140 | try func.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } }); |
| 5205 | | try func.addLabel(.local_set, result.local.value); |
| 5206 | | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 5141 | return func.finishAir(inst, .stack, &.{ty_op.operand}); |
| 5207 | 5142 | }, |
| 5208 | 5143 | .local => { |
| 5209 | 5144 | const opcode = switch (elem_ty.bitSize(pt)) { |
| ... | ... | @@ -5213,13 +5148,11 @@ fn airSplat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5213 | 5148 | 64 => if (elem_ty.isInt(mod)) std.wasm.simdOpcode(.i64x2_splat) else std.wasm.simdOpcode(.f64x2_splat), |
| 5214 | 5149 | else => break :blk, // Cannot make use of simd-instructions |
| 5215 | 5150 | }; |
| 5216 | | const result = try func.allocLocal(ty); |
| 5217 | 5151 | try func.emitWValue(operand); |
| 5218 | 5152 | const extra_index = @as(u32, @intCast(func.mir_extra.items.len)); |
| 5219 | 5153 | try func.mir_extra.append(func.gpa, opcode); |
| 5220 | 5154 | try func.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } }); |
| 5221 | | try func.addLabel(.local_set, result.local.value); |
| 5222 | | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 5155 | return func.finishAir(inst, .stack, &.{ty_op.operand}); |
| 5223 | 5156 | }, |
| 5224 | 5157 | else => unreachable, |
| 5225 | 5158 | } |
| ... | ... | @@ -5308,7 +5241,7 @@ fn airShuffle(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5308 | 5241 | try func.mir_extra.appendSlice(func.gpa, &operands); |
| 5309 | 5242 | try func.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } }); |
| 5310 | 5243 | |
| 5311 | | return func.finishAir(inst, try WValue.toLocal(.stack, func, inst_ty), &.{ extra.a, extra.b }); |
| 5244 | return func.finishAir(inst, .stack, &.{ extra.a, extra.b }); |
| 5312 | 5245 | } |
| 5313 | 5246 | } |
| 5314 | 5247 | |
| ... | ... | @@ -5392,10 +5325,10 @@ fn airAggregateInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5392 | 5325 | const field_ty = Type.fromInterned(field_types.get(ip)[elem_index]); |
| 5393 | 5326 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; |
| 5394 | 5327 | |
| 5395 | | const shift_val = if (backing_type.bitSize(pt) <= 32) |
| 5396 | | WValue{ .imm32 = current_bit } |
| 5328 | const shift_val: WValue = if (backing_type.bitSize(pt) <= 32) |
| 5329 | .{ .imm32 = current_bit } |
| 5397 | 5330 | else |
| 5398 | | WValue{ .imm64 = current_bit }; |
| 5331 | .{ .imm64 = current_bit }; |
| 5399 | 5332 | |
| 5400 | 5333 | const value = try func.resolveInst(elem); |
| 5401 | 5334 | const value_bit_size: u16 = @intCast(field_ty.bitSize(pt)); |
| ... | ... | @@ -5473,7 +5406,7 @@ fn airUnionInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5473 | 5406 | }; |
| 5474 | 5407 | if (layout.payload_size == 0) { |
| 5475 | 5408 | if (layout.tag_size == 0) { |
| 5476 | | break :result WValue{ .none = {} }; |
| 5409 | break :result .none; |
| 5477 | 5410 | } |
| 5478 | 5411 | assert(!isByRef(union_ty, pt)); |
| 5479 | 5412 | break :result tag_int; |
| ... | ... | @@ -5511,15 +5444,12 @@ fn airUnionInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5511 | 5444 | if (field_ty.zigTypeTag(mod) == .Float) { |
| 5512 | 5445 | const int_type = try pt.intType(.unsigned, @intCast(field_ty.bitSize(pt))); |
| 5513 | 5446 | const bitcasted = try func.bitcast(field_ty, int_type, operand); |
| 5514 | | const casted = try func.trunc(bitcasted, int_type, union_int_type); |
| 5515 | | break :result try casted.toLocal(func, field_ty); |
| 5447 | break :result try func.trunc(bitcasted, int_type, union_int_type); |
| 5516 | 5448 | } else if (field_ty.isPtrAtRuntime(mod)) { |
| 5517 | 5449 | const int_type = try pt.intType(.unsigned, @intCast(field_ty.bitSize(pt))); |
| 5518 | | const casted = try func.intcast(operand, int_type, union_int_type); |
| 5519 | | break :result try casted.toLocal(func, field_ty); |
| 5450 | break :result try func.intcast(operand, int_type, union_int_type); |
| 5520 | 5451 | } |
| 5521 | | const casted = try func.intcast(operand, field_ty, union_int_type); |
| 5522 | | break :result try casted.toLocal(func, field_ty); |
| 5452 | break :result try func.intcast(operand, field_ty, union_int_type); |
| 5523 | 5453 | } |
| 5524 | 5454 | }; |
| 5525 | 5455 | |
| ... | ... | @@ -5528,27 +5458,23 @@ fn airUnionInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5528 | 5458 | |
| 5529 | 5459 | fn airPrefetch(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5530 | 5460 | const prefetch = func.air.instructions.items(.data)[@intFromEnum(inst)].prefetch; |
| 5531 | | func.finishAir(inst, .none, &.{prefetch.ptr}); |
| 5461 | return func.finishAir(inst, .none, &.{prefetch.ptr}); |
| 5532 | 5462 | } |
| 5533 | 5463 | |
| 5534 | 5464 | fn airWasmMemorySize(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5535 | 5465 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 5536 | 5466 | |
| 5537 | | const result = try func.allocLocal(func.typeOfIndex(inst)); |
| 5538 | 5467 | try func.addLabel(.memory_size, pl_op.payload); |
| 5539 | | try func.addLabel(.local_set, result.local.value); |
| 5540 | | func.finishAir(inst, result, &.{pl_op.operand}); |
| 5468 | return func.finishAir(inst, .stack, &.{pl_op.operand}); |
| 5541 | 5469 | } |
| 5542 | 5470 | |
| 5543 | 5471 | fn airWasmMemoryGrow(func: *CodeGen, inst: Air.Inst.Index) !void { |
| 5544 | 5472 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 5545 | 5473 | |
| 5546 | 5474 | const operand = try func.resolveInst(pl_op.operand); |
| 5547 | | const result = try func.allocLocal(func.typeOfIndex(inst)); |
| 5548 | 5475 | try func.emitWValue(operand); |
| 5549 | 5476 | try func.addLabel(.memory_grow, pl_op.payload); |
| 5550 | | try func.addLabel(.local_set, result.local.value); |
| 5551 | | func.finishAir(inst, result, &.{pl_op.operand}); |
| 5477 | return func.finishAir(inst, .stack, &.{pl_op.operand}); |
| 5552 | 5478 | } |
| 5553 | 5479 | |
| 5554 | 5480 | fn cmpOptionals(func: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op: std.math.CompareOperator) InnerError!WValue { |
| ... | ... | @@ -5582,7 +5508,7 @@ fn cmpOptionals(func: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op: |
| 5582 | 5508 | try func.emitWValue(result); |
| 5583 | 5509 | try func.addImm32(0); |
| 5584 | 5510 | try func.addTag(if (op == .eq) .i32_ne else .i32_eq); |
| 5585 | | return WValue{ .stack = {} }; |
| 5511 | return .stack; |
| 5586 | 5512 | } |
| 5587 | 5513 | |
| 5588 | 5514 | /// Compares big integers by checking both its high bits and low bits. |
| ... | ... | @@ -5628,7 +5554,7 @@ fn cmpBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op: std |
| 5628 | 5554 | }, |
| 5629 | 5555 | } |
| 5630 | 5556 | |
| 5631 | | return WValue{ .stack = {} }; |
| 5557 | return .stack; |
| 5632 | 5558 | } |
| 5633 | 5559 | |
| 5634 | 5560 | fn airSetUnionTag(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -5653,7 +5579,7 @@ fn airSetUnionTag(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5653 | 5579 | break :blk @intCast(layout.payload_size); |
| 5654 | 5580 | } else 0; |
| 5655 | 5581 | try func.store(union_ptr, new_tag, tag_ty, offset); |
| 5656 | | func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 5582 | return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 5657 | 5583 | } |
| 5658 | 5584 | |
| 5659 | 5585 | fn airGetUnionTag(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -5668,12 +5594,12 @@ fn airGetUnionTag(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5668 | 5594 | const operand = try func.resolveInst(ty_op.operand); |
| 5669 | 5595 | // when the tag alignment is smaller than the payload, the field will be stored |
| 5670 | 5596 | // after the payload. |
| 5671 | | const offset: u32 = if (layout.tag_align.compare(.lt, layout.payload_align)) blk: { |
| 5672 | | break :blk @intCast(layout.payload_size); |
| 5673 | | } else 0; |
| 5674 | | const tag = try func.load(operand, tag_ty, offset); |
| 5675 | | const result = try tag.toLocal(func, tag_ty); |
| 5676 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5597 | const offset: u32 = if (layout.tag_align.compare(.lt, layout.payload_align)) |
| 5598 | @intCast(layout.payload_size) |
| 5599 | else |
| 5600 | 0; |
| 5601 | const result = try func.load(operand, tag_ty, offset); |
| 5602 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 5677 | 5603 | } |
| 5678 | 5604 | |
| 5679 | 5605 | fn airFpext(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -5681,9 +5607,8 @@ fn airFpext(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5681 | 5607 | |
| 5682 | 5608 | const dest_ty = func.typeOfIndex(inst); |
| 5683 | 5609 | const operand = try func.resolveInst(ty_op.operand); |
| 5684 | | const extended = try func.fpext(operand, func.typeOf(ty_op.operand), dest_ty); |
| 5685 | | const result = try extended.toLocal(func, dest_ty); |
| 5686 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5610 | const result = try func.fpext(operand, func.typeOf(ty_op.operand), dest_ty); |
| 5611 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 5687 | 5612 | } |
| 5688 | 5613 | |
| 5689 | 5614 | /// Extends a float from a given `Type` to a larger wanted `Type` |
| ... | ... | @@ -5695,7 +5620,7 @@ fn fpext(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerError! |
| 5695 | 5620 | if (wanted_bits == 64 and given_bits == 32) { |
| 5696 | 5621 | try func.emitWValue(operand); |
| 5697 | 5622 | try func.addTag(.f64_promote_f32); |
| 5698 | | return WValue{ .stack = {} }; |
| 5623 | return .stack; |
| 5699 | 5624 | } else if (given_bits == 16 and wanted_bits <= 64) { |
| 5700 | 5625 | // call __extendhfsf2(f16) f32 |
| 5701 | 5626 | const f32_result = try func.callIntrinsic( |
| ... | ... | @@ -5709,7 +5634,7 @@ fn fpext(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerError! |
| 5709 | 5634 | if (wanted_bits == 64) { |
| 5710 | 5635 | try func.addTag(.f64_promote_f32); |
| 5711 | 5636 | } |
| 5712 | | return WValue{ .stack = {} }; |
| 5637 | return .stack; |
| 5713 | 5638 | } |
| 5714 | 5639 | |
| 5715 | 5640 | var fn_name_buf: [13]u8 = undefined; |
| ... | ... | @@ -5726,9 +5651,8 @@ fn airFptrunc(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5726 | 5651 | |
| 5727 | 5652 | const dest_ty = func.typeOfIndex(inst); |
| 5728 | 5653 | const operand = try func.resolveInst(ty_op.operand); |
| 5729 | | const truncated = try func.fptrunc(operand, func.typeOf(ty_op.operand), dest_ty); |
| 5730 | | const result = try truncated.toLocal(func, dest_ty); |
| 5731 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5654 | const result = try func.fptrunc(operand, func.typeOf(ty_op.operand), dest_ty); |
| 5655 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 5732 | 5656 | } |
| 5733 | 5657 | |
| 5734 | 5658 | /// Truncates a float from a given `Type` to its wanted `Type` |
| ... | ... | @@ -5740,12 +5664,12 @@ fn fptrunc(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerErro |
| 5740 | 5664 | if (wanted_bits == 32 and given_bits == 64) { |
| 5741 | 5665 | try func.emitWValue(operand); |
| 5742 | 5666 | try func.addTag(.f32_demote_f64); |
| 5743 | | return WValue{ .stack = {} }; |
| 5667 | return .stack; |
| 5744 | 5668 | } else if (wanted_bits == 16 and given_bits <= 64) { |
| 5745 | 5669 | const op: WValue = if (given_bits == 64) blk: { |
| 5746 | 5670 | try func.emitWValue(operand); |
| 5747 | 5671 | try func.addTag(.f32_demote_f64); |
| 5748 | | break :blk WValue{ .stack = {} }; |
| 5672 | break :blk .stack; |
| 5749 | 5673 | } else operand; |
| 5750 | 5674 | |
| 5751 | 5675 | // call __truncsfhf2(f32) f16 |
| ... | ... | @@ -5785,7 +5709,7 @@ fn airErrUnionPayloadPtrSet(func: *CodeGen, inst: Air.Inst.Index) InnerError!voi |
| 5785 | 5709 | |
| 5786 | 5710 | break :result try func.buildPointerOffset(operand, @as(u32, @intCast(errUnionPayloadOffset(payload_ty, pt))), .new); |
| 5787 | 5711 | }; |
| 5788 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5712 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 5789 | 5713 | } |
| 5790 | 5714 | |
| 5791 | 5715 | fn airFieldParentPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -5807,7 +5731,7 @@ fn airFieldParentPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5807 | 5731 | break :result base; |
| 5808 | 5732 | } else func.reuseOperand(extra.field_ptr, field_ptr); |
| 5809 | 5733 | |
| 5810 | | func.finishAir(inst, result, &.{extra.field_ptr}); |
| 5734 | return func.finishAir(inst, result, &.{extra.field_ptr}); |
| 5811 | 5735 | } |
| 5812 | 5736 | |
| 5813 | 5737 | fn sliceOrArrayPtr(func: *CodeGen, ptr: WValue, ptr_ty: Type) InnerError!WValue { |
| ... | ... | @@ -5849,12 +5773,12 @@ fn airMemcpy(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5849 | 5773 | const src_ptr = try func.sliceOrArrayPtr(src, src_ty); |
| 5850 | 5774 | try func.memcpy(dst_ptr, src_ptr, len); |
| 5851 | 5775 | |
| 5852 | | func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 5776 | return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 5853 | 5777 | } |
| 5854 | 5778 | |
| 5855 | 5779 | fn airRetAddr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5856 | 5780 | // TODO: Implement this properly once stack serialization is solved |
| 5857 | | func.finishAir(inst, switch (func.arch()) { |
| 5781 | return func.finishAir(inst, switch (func.arch()) { |
| 5858 | 5782 | .wasm32 => .{ .imm32 = 0 }, |
| 5859 | 5783 | .wasm64 => .{ .imm64 = 0 }, |
| 5860 | 5784 | else => unreachable, |
| ... | ... | @@ -5868,7 +5792,6 @@ fn airPopcount(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5868 | 5792 | |
| 5869 | 5793 | const operand = try func.resolveInst(ty_op.operand); |
| 5870 | 5794 | const op_ty = func.typeOf(ty_op.operand); |
| 5871 | | const result_ty = func.typeOfIndex(inst); |
| 5872 | 5795 | |
| 5873 | 5796 | if (op_ty.zigTypeTag(mod) == .Vector) { |
| 5874 | 5797 | return func.fail("TODO: Implement @popCount for vectors", .{}); |
| ... | ... | @@ -5911,9 +5834,7 @@ fn airPopcount(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5911 | 5834 | else => unreachable, |
| 5912 | 5835 | } |
| 5913 | 5836 | |
| 5914 | | const result = try func.allocLocal(result_ty); |
| 5915 | | try func.addLabel(.local_set, result.local.value); |
| 5916 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5837 | return func.finishAir(inst, .stack, &.{ty_op.operand}); |
| 5917 | 5838 | } |
| 5918 | 5839 | |
| 5919 | 5840 | fn airBitReverse(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -5942,12 +5863,11 @@ fn airBitReverse(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5942 | 5863 | Type.u32, |
| 5943 | 5864 | &.{operand}, |
| 5944 | 5865 | ); |
| 5945 | | const reversed = if (bits == 32) |
| 5866 | const result = if (bits == 32) |
| 5946 | 5867 | intrin_ret |
| 5947 | 5868 | else |
| 5948 | 5869 | try func.binOp(intrin_ret, .{ .imm32 = 32 - bits }, ty, .shr); |
| 5949 | | const result = try reversed.toLocal(func, ty); |
| 5950 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5870 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 5951 | 5871 | }, |
| 5952 | 5872 | 64 => { |
| 5953 | 5873 | const intrin_ret = try func.callIntrinsic( |
| ... | ... | @@ -5956,12 +5876,11 @@ fn airBitReverse(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5956 | 5876 | Type.u64, |
| 5957 | 5877 | &.{operand}, |
| 5958 | 5878 | ); |
| 5959 | | const reversed = if (bits == 64) |
| 5879 | const result = if (bits == 64) |
| 5960 | 5880 | intrin_ret |
| 5961 | 5881 | else |
| 5962 | 5882 | try func.binOp(intrin_ret, .{ .imm64 = 64 - bits }, ty, .shr); |
| 5963 | | const result = try reversed.toLocal(func, ty); |
| 5964 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5883 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 5965 | 5884 | }, |
| 5966 | 5885 | 128 => { |
| 5967 | 5886 | const result = try func.allocStack(ty); |
| ... | ... | @@ -6008,7 +5927,7 @@ fn airBitReverse(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6008 | 5927 | try func.addTag(.i64_or); |
| 6009 | 5928 | try func.store(.stack, .stack, Type.u64, result.offset()); |
| 6010 | 5929 | } |
| 6011 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5930 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 6012 | 5931 | }, |
| 6013 | 5932 | else => unreachable, |
| 6014 | 5933 | } |
| ... | ... | @@ -6051,16 +5970,14 @@ fn airErrorName(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6051 | 5970 | else => unreachable, |
| 6052 | 5971 | } |
| 6053 | 5972 | |
| 6054 | | const result_ptr = try func.allocLocal(Type.usize); |
| 6055 | | try func.addLabel(.local_set, result_ptr.local.value); |
| 6056 | | func.finishAir(inst, result_ptr, &.{un_op}); |
| 5973 | return func.finishAir(inst, .stack, &.{un_op}); |
| 6057 | 5974 | } |
| 6058 | 5975 | |
| 6059 | 5976 | fn airPtrSliceFieldPtr(func: *CodeGen, inst: Air.Inst.Index, offset: u32) InnerError!void { |
| 6060 | 5977 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6061 | 5978 | const slice_ptr = try func.resolveInst(ty_op.operand); |
| 6062 | 5979 | const result = try func.buildPointerOffset(slice_ptr, offset, .new); |
| 6063 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5980 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 6064 | 5981 | } |
| 6065 | 5982 | |
| 6066 | 5983 | fn airAddSubWithOverflow(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| ... | ... | @@ -6089,9 +6006,9 @@ fn airAddSubWithOverflow(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerErro |
| 6089 | 6006 | return func.finishAir(inst, result, &.{ extra.lhs, extra.rhs }); |
| 6090 | 6007 | } |
| 6091 | 6008 | |
| 6092 | | const zero = switch (wasm_bits) { |
| 6093 | | 32 => WValue{ .imm32 = 0 }, |
| 6094 | | 64 => WValue{ .imm64 = 0 }, |
| 6009 | const zero: WValue = switch (wasm_bits) { |
| 6010 | 32 => .{ .imm32 = 0 }, |
| 6011 | 64 => .{ .imm64 = 0 }, |
| 6095 | 6012 | else => unreachable, |
| 6096 | 6013 | }; |
| 6097 | 6014 | |
| ... | ... | @@ -6121,7 +6038,7 @@ fn airAddSubWithOverflow(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerErro |
| 6121 | 6038 | const offset = @as(u32, @intCast(lhs_ty.abiSize(pt))); |
| 6122 | 6039 | try func.store(result_ptr, overflow_local, Type.u1, offset); |
| 6123 | 6040 | |
| 6124 | | func.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); |
| 6041 | return func.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); |
| 6125 | 6042 | } |
| 6126 | 6043 | |
| 6127 | 6044 | fn addSubWithOverflowBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, result_ty: Type, op: Op) InnerError!WValue { |
| ... | ... | @@ -6177,7 +6094,7 @@ fn addSubWithOverflowBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, |
| 6177 | 6094 | _ = try func.cmp(tmp_op, lhs_low_bit, Type.u64, .eq); |
| 6178 | 6095 | try func.addTag(.select); |
| 6179 | 6096 | |
| 6180 | | break :blk WValue{ .stack = {} }; |
| 6097 | break :blk .stack; |
| 6181 | 6098 | }; |
| 6182 | 6099 | var overflow_local = try overflow_bit.toLocal(func, Type.u1); |
| 6183 | 6100 | defer overflow_local.free(func); |
| ... | ... | @@ -6228,7 +6145,7 @@ fn airShlWithOverflow(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6228 | 6145 | const overflow_bit = blk: { |
| 6229 | 6146 | try func.emitWValue(lhs); |
| 6230 | 6147 | const shr = try func.binOp(result, rhs_final, lhs_ty, .shr); |
| 6231 | | break :blk try func.cmp(.{ .stack = {} }, shr, lhs_ty, .neq); |
| 6148 | break :blk try func.cmp(.stack, shr, lhs_ty, .neq); |
| 6232 | 6149 | }; |
| 6233 | 6150 | var overflow_local = try overflow_bit.toLocal(func, Type.u1); |
| 6234 | 6151 | defer overflow_local.free(func); |
| ... | ... | @@ -6238,7 +6155,7 @@ fn airShlWithOverflow(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6238 | 6155 | const offset = @as(u32, @intCast(lhs_ty.abiSize(pt))); |
| 6239 | 6156 | try func.store(result_ptr, overflow_local, Type.u1, offset); |
| 6240 | 6157 | |
| 6241 | | func.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); |
| 6158 | return func.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); |
| 6242 | 6159 | } |
| 6243 | 6160 | |
| 6244 | 6161 | fn airMulWithOverflow(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -6265,9 +6182,9 @@ fn airMulWithOverflow(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6265 | 6182 | return func.fail("TODO: Implement `@mulWithOverflow` for integer bitsize: {d}", .{int_info.bits}); |
| 6266 | 6183 | }; |
| 6267 | 6184 | |
| 6268 | | const zero = switch (wasm_bits) { |
| 6269 | | 32 => WValue{ .imm32 = 0 }, |
| 6270 | | 64, 128 => WValue{ .imm64 = 0 }, |
| 6185 | const zero: WValue = switch (wasm_bits) { |
| 6186 | 32 => .{ .imm32 = 0 }, |
| 6187 | 64, 128 => .{ .imm64 = 0 }, |
| 6271 | 6188 | else => unreachable, |
| 6272 | 6189 | }; |
| 6273 | 6190 | |
| ... | ... | @@ -6303,10 +6220,10 @@ fn airMulWithOverflow(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6303 | 6220 | } else if (wasm_bits == 32) blk: { |
| 6304 | 6221 | var bin_op = try (try func.binOp(lhs, rhs, lhs_ty, .mul)).toLocal(func, lhs_ty); |
| 6305 | 6222 | defer bin_op.free(func); |
| 6306 | | const shift_imm = if (wasm_bits == 32) |
| 6307 | | WValue{ .imm32 = int_info.bits } |
| 6223 | const shift_imm: WValue = if (wasm_bits == 32) |
| 6224 | .{ .imm32 = int_info.bits } |
| 6308 | 6225 | else |
| 6309 | | WValue{ .imm64 = int_info.bits }; |
| 6226 | .{ .imm64 = int_info.bits }; |
| 6310 | 6227 | const shr = try func.binOp(bin_op, shift_imm, lhs_ty, .shr); |
| 6311 | 6228 | _ = try func.cmp(shr, zero, lhs_ty, .neq); |
| 6312 | 6229 | try func.addLabel(.local_set, overflow_bit.local.value); |
| ... | ... | @@ -6412,7 +6329,7 @@ fn airMulWithOverflow(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6412 | 6329 | const offset = @as(u32, @intCast(lhs_ty.abiSize(pt))); |
| 6413 | 6330 | try func.store(result_ptr, overflow_bit, Type.u1, offset); |
| 6414 | 6331 | |
| 6415 | | func.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); |
| 6332 | return func.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); |
| 6416 | 6333 | } |
| 6417 | 6334 | |
| 6418 | 6335 | fn airMaxMin(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| ... | ... | @@ -6454,11 +6371,7 @@ fn airMaxMin(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 6454 | 6371 | try func.addTag(.select); |
| 6455 | 6372 | } |
| 6456 | 6373 | |
| 6457 | | // store result in local |
| 6458 | | const result_ty = if (isByRef(ty, pt)) Type.u32 else ty; |
| 6459 | | const result = try func.allocLocal(result_ty); |
| 6460 | | try func.addLabel(.local_set, result.local.value); |
| 6461 | | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 6374 | return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs }); |
| 6462 | 6375 | } |
| 6463 | 6376 | |
| 6464 | 6377 | fn airMulAdd(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -6487,13 +6400,13 @@ fn airMulAdd(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6487 | 6400 | Type.f32, |
| 6488 | 6401 | &.{ rhs_ext, lhs_ext, addend_ext }, |
| 6489 | 6402 | ); |
| 6490 | | break :fl_result try (try func.fptrunc(result, Type.f32, ty)).toLocal(func, ty); |
| 6403 | break :fl_result try func.fptrunc(result, Type.f32, ty); |
| 6491 | 6404 | } else result: { |
| 6492 | 6405 | const mul_result = try func.binOp(lhs, rhs, ty, .mul); |
| 6493 | | break :result try (try func.binOp(mul_result, addend, ty, .add)).toLocal(func, ty); |
| 6406 | break :result try func.binOp(mul_result, addend, ty, .add); |
| 6494 | 6407 | }; |
| 6495 | 6408 | |
| 6496 | | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs, pl_op.operand }); |
| 6409 | return func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs, pl_op.operand }); |
| 6497 | 6410 | } |
| 6498 | 6411 | |
| 6499 | 6412 | fn airClz(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -6502,7 +6415,6 @@ fn airClz(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6502 | 6415 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6503 | 6416 | |
| 6504 | 6417 | const ty = func.typeOf(ty_op.operand); |
| 6505 | | const result_ty = func.typeOfIndex(inst); |
| 6506 | 6418 | if (ty.zigTypeTag(mod) == .Vector) { |
| 6507 | 6419 | return func.fail("TODO: `@clz` for vectors", .{}); |
| 6508 | 6420 | } |
| ... | ... | @@ -6545,9 +6457,7 @@ fn airClz(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6545 | 6457 | try func.addTag(.i32_sub); |
| 6546 | 6458 | } |
| 6547 | 6459 | |
| 6548 | | const result = try func.allocLocal(result_ty); |
| 6549 | | try func.addLabel(.local_set, result.local.value); |
| 6550 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 6460 | return func.finishAir(inst, .stack, &.{ty_op.operand}); |
| 6551 | 6461 | } |
| 6552 | 6462 | |
| 6553 | 6463 | fn airCtz(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -6556,7 +6466,6 @@ fn airCtz(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6556 | 6466 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6557 | 6467 | |
| 6558 | 6468 | const ty = func.typeOf(ty_op.operand); |
| 6559 | | const result_ty = func.typeOfIndex(inst); |
| 6560 | 6469 | |
| 6561 | 6470 | if (ty.zigTypeTag(mod) == .Vector) { |
| 6562 | 6471 | return func.fail("TODO: `@ctz` for vectors", .{}); |
| ... | ... | @@ -6611,9 +6520,7 @@ fn airCtz(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6611 | 6520 | else => unreachable, |
| 6612 | 6521 | } |
| 6613 | 6522 | |
| 6614 | | const result = try func.allocLocal(result_ty); |
| 6615 | | try func.addLabel(.local_set, result.local.value); |
| 6616 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 6523 | return func.finishAir(inst, .stack, &.{ty_op.operand}); |
| 6617 | 6524 | } |
| 6618 | 6525 | |
| 6619 | 6526 | fn airDbgStmt(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -6626,7 +6533,7 @@ fn airDbgStmt(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6626 | 6533 | .column = dbg_stmt.column, |
| 6627 | 6534 | }), |
| 6628 | 6535 | } }); |
| 6629 | | func.finishAir(inst, .none, &.{}); |
| 6536 | return func.finishAir(inst, .none, &.{}); |
| 6630 | 6537 | } |
| 6631 | 6538 | |
| 6632 | 6539 | fn airDbgInlineBlock(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -6659,7 +6566,7 @@ fn airDbgVar(func: *CodeGen, inst: Air.Inst.Index, is_ptr: bool) InnerError!void |
| 6659 | 6566 | }; |
| 6660 | 6567 | try func.debug_output.dwarf.genVarDbgInfo(name, ty, mod.funcOwnerDeclIndex(func.func_index), is_ptr, loc); |
| 6661 | 6568 | |
| 6662 | | func.finishAir(inst, .none, &.{}); |
| 6569 | return func.finishAir(inst, .none, &.{}); |
| 6663 | 6570 | } |
| 6664 | 6571 | |
| 6665 | 6572 | fn airTry(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -6669,7 +6576,7 @@ fn airTry(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6669 | 6576 | const body: []const Air.Inst.Index = @ptrCast(func.air.extra[extra.end..][0..extra.data.body_len]); |
| 6670 | 6577 | const err_union_ty = func.typeOf(pl_op.operand); |
| 6671 | 6578 | const result = try lowerTry(func, inst, err_union, body, err_union_ty, false); |
| 6672 | | func.finishAir(inst, result, &.{pl_op.operand}); |
| 6579 | return func.finishAir(inst, result, &.{pl_op.operand}); |
| 6673 | 6580 | } |
| 6674 | 6581 | |
| 6675 | 6582 | fn airTryPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -6681,7 +6588,7 @@ fn airTryPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6681 | 6588 | const body: []const Air.Inst.Index = @ptrCast(func.air.extra[extra.end..][0..extra.data.body_len]); |
| 6682 | 6589 | const err_union_ty = func.typeOf(extra.data.ptr).childType(mod); |
| 6683 | 6590 | const result = try lowerTry(func, inst, err_union_ptr, body, err_union_ty, true); |
| 6684 | | func.finishAir(inst, result, &.{extra.data.ptr}); |
| 6591 | return func.finishAir(inst, result, &.{extra.data.ptr}); |
| 6685 | 6592 | } |
| 6686 | 6593 | |
| 6687 | 6594 | fn lowerTry( |
| ... | ... | @@ -6730,7 +6637,7 @@ fn lowerTry( |
| 6730 | 6637 | |
| 6731 | 6638 | // if we reach here it means error was not set, and we want the payload |
| 6732 | 6639 | if (!pl_has_bits) { |
| 6733 | | return WValue{ .none = {} }; |
| 6640 | return .none; |
| 6734 | 6641 | } |
| 6735 | 6642 | |
| 6736 | 6643 | const pl_offset: u32 = @intCast(errUnionPayloadOffset(pl_ty, pt)); |
| ... | ... | @@ -6771,12 +6678,10 @@ fn airByteSwap(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6771 | 6678 | Type.u32, |
| 6772 | 6679 | &.{operand}, |
| 6773 | 6680 | ); |
| 6774 | | const swapped = if (int_info.bits == 32) |
| 6681 | break :result if (int_info.bits == 32) |
| 6775 | 6682 | intrin_ret |
| 6776 | 6683 | else |
| 6777 | 6684 | try func.binOp(intrin_ret, .{ .imm32 = 32 - int_info.bits }, ty, .shr); |
| 6778 | | |
| 6779 | | break :result try swapped.toLocal(func, ty); |
| 6780 | 6685 | }, |
| 6781 | 6686 | 64 => { |
| 6782 | 6687 | const intrin_ret = try func.callIntrinsic( |
| ... | ... | @@ -6785,17 +6690,15 @@ fn airByteSwap(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6785 | 6690 | Type.u64, |
| 6786 | 6691 | &.{operand}, |
| 6787 | 6692 | ); |
| 6788 | | const swapped = if (int_info.bits == 64) |
| 6693 | break :result if (int_info.bits == 64) |
| 6789 | 6694 | intrin_ret |
| 6790 | 6695 | else |
| 6791 | 6696 | try func.binOp(intrin_ret, .{ .imm64 = 64 - int_info.bits }, ty, .shr); |
| 6792 | | |
| 6793 | | break :result try swapped.toLocal(func, ty); |
| 6794 | 6697 | }, |
| 6795 | 6698 | else => return func.fail("TODO: @byteSwap for integers with bitsize {d}", .{int_info.bits}), |
| 6796 | 6699 | } |
| 6797 | 6700 | }; |
| 6798 | | func.finishAir(inst, result, &.{ty_op.operand}); |
| 6701 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 6799 | 6702 | } |
| 6800 | 6703 | |
| 6801 | 6704 | fn airDiv(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -6805,8 +6708,8 @@ fn airDiv(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6805 | 6708 | const lhs = try func.resolveInst(bin_op.lhs); |
| 6806 | 6709 | const rhs = try func.resolveInst(bin_op.rhs); |
| 6807 | 6710 | |
| 6808 | | const result = try (try func.binOp(lhs, rhs, ty, .div)).toLocal(func, ty); |
| 6809 | | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 6711 | const result = try func.binOp(lhs, rhs, ty, .div); |
| 6712 | return func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 6810 | 6713 | } |
| 6811 | 6714 | |
| 6812 | 6715 | fn airDivTrunc(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -6816,10 +6719,10 @@ fn airDivTrunc(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6816 | 6719 | const lhs = try func.resolveInst(bin_op.lhs); |
| 6817 | 6720 | const rhs = try func.resolveInst(bin_op.rhs); |
| 6818 | 6721 | |
| 6819 | | const div_result = try (try func.binOp(lhs, rhs, ty, .div)).toLocal(func, ty); |
| 6722 | const div_result = try func.binOp(lhs, rhs, ty, .div); |
| 6820 | 6723 | |
| 6821 | 6724 | if (ty.isAnyFloat()) { |
| 6822 | | const trunc_result = try (try func.floatOp(.trunc, ty, &.{div_result})).toLocal(func, ty); |
| 6725 | const trunc_result = try func.floatOp(.trunc, ty, &.{div_result}); |
| 6823 | 6726 | return func.finishAir(inst, trunc_result, &.{ bin_op.lhs, bin_op.rhs }); |
| 6824 | 6727 | } |
| 6825 | 6728 | |
| ... | ... | @@ -6847,9 +6750,9 @@ fn airDivFloor(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6847 | 6750 | return func.fail("TODO: `@divFloor` for signed integers larger than 64 bits ({d} bits requested)", .{int_bits}); |
| 6848 | 6751 | } |
| 6849 | 6752 | |
| 6850 | | const zero = switch (wasm_bits) { |
| 6851 | | 32 => WValue{ .imm32 = 0 }, |
| 6852 | | 64 => WValue{ .imm64 = 0 }, |
| 6753 | const zero: WValue = switch (wasm_bits) { |
| 6754 | 32 => .{ .imm32 = 0 }, |
| 6755 | 64 => .{ .imm64 = 0 }, |
| 6853 | 6756 | else => unreachable, |
| 6854 | 6757 | }; |
| 6855 | 6758 | |
| ... | ... | @@ -6895,7 +6798,7 @@ fn airDivFloor(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6895 | 6798 | // TODO: Should we be zeroing the high bits here or should we be ignoring the high bits |
| 6896 | 6799 | // when performing comparisons? |
| 6897 | 6800 | if (int_bits != wasm_bits) { |
| 6898 | | _ = try func.wrapOperand(.{ .stack = {} }, ty); |
| 6801 | _ = try func.wrapOperand(.stack, ty); |
| 6899 | 6802 | } |
| 6900 | 6803 | } else { |
| 6901 | 6804 | const float_bits = ty.floatBits(func.target); |
| ... | ... | @@ -6923,13 +6826,11 @@ fn airDivFloor(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6923 | 6826 | } |
| 6924 | 6827 | |
| 6925 | 6828 | if (is_f16) { |
| 6926 | | _ = try func.fptrunc(.{ .stack = {} }, Type.f32, Type.f16); |
| 6829 | _ = try func.fptrunc(.stack, Type.f32, Type.f16); |
| 6927 | 6830 | } |
| 6928 | 6831 | } |
| 6929 | 6832 | |
| 6930 | | const result = try func.allocLocal(ty); |
| 6931 | | try func.addLabel(.local_set, result.local.value); |
| 6932 | | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 6833 | return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs }); |
| 6933 | 6834 | } |
| 6934 | 6835 | |
| 6935 | 6836 | fn airRem(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -6941,8 +6842,7 @@ fn airRem(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6941 | 6842 | |
| 6942 | 6843 | const result = try func.binOp(lhs, rhs, ty, .rem); |
| 6943 | 6844 | |
| 6944 | | const return_local = try result.toLocal(func, ty); |
| 6945 | | func.finishAir(inst, return_local, &.{ bin_op.lhs, bin_op.rhs }); |
| 6845 | return func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 6946 | 6846 | } |
| 6947 | 6847 | |
| 6948 | 6848 | /// Remainder after floor division, defined by: |
| ... | ... | @@ -6979,9 +6879,7 @@ fn airMod(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6979 | 6879 | return func.fail("TODO: implement `@mod` on floating point types for {}", .{func.target.cpu.arch}); |
| 6980 | 6880 | } |
| 6981 | 6881 | |
| 6982 | | const result = try func.allocLocal(ty); |
| 6983 | | try func.addLabel(.local_set, result.local.value); |
| 6984 | | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 6882 | return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs }); |
| 6985 | 6883 | } |
| 6986 | 6884 | |
| 6987 | 6885 | fn airSatBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| ... | ... | @@ -7011,9 +6909,9 @@ fn airSatBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 7011 | 6909 | defer bin_result.free(func); |
| 7012 | 6910 | if (wasm_bits != int_info.bits and op == .add) { |
| 7013 | 6911 | const val: u64 = @as(u64, @intCast((@as(u65, 1) << @as(u7, @intCast(int_info.bits))) - 1)); |
| 7014 | | const imm_val = switch (wasm_bits) { |
| 7015 | | 32 => WValue{ .imm32 = @intCast(val) }, |
| 7016 | | 64 => WValue{ .imm64 = val }, |
| 6912 | const imm_val: WValue = switch (wasm_bits) { |
| 6913 | 32 => .{ .imm32 = @intCast(val) }, |
| 6914 | 64 => .{ .imm64 = val }, |
| 7017 | 6915 | else => unreachable, |
| 7018 | 6916 | }; |
| 7019 | 6917 | |
| ... | ... | @@ -7031,9 +6929,7 @@ fn airSatBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 7031 | 6929 | } |
| 7032 | 6930 | |
| 7033 | 6931 | try func.addTag(.select); |
| 7034 | | const result = try func.allocLocal(ty); |
| 7035 | | try func.addLabel(.local_set, result.local.value); |
| 7036 | | return func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 6932 | return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs }); |
| 7037 | 6933 | } |
| 7038 | 6934 | |
| 7039 | 6935 | fn signedSat(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError!WValue { |
| ... | ... | @@ -7046,14 +6942,14 @@ fn signedSat(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerEr |
| 7046 | 6942 | |
| 7047 | 6943 | const max_val: u64 = @as(u64, @intCast((@as(u65, 1) << @as(u7, @intCast(int_info.bits - 1))) - 1)); |
| 7048 | 6944 | const min_val: i64 = (-@as(i64, @intCast(@as(u63, @intCast(max_val))))) - 1; |
| 7049 | | const max_wvalue = switch (wasm_bits) { |
| 7050 | | 32 => WValue{ .imm32 = @as(u32, @truncate(max_val)) }, |
| 7051 | | 64 => WValue{ .imm64 = max_val }, |
| 6945 | const max_wvalue: WValue = switch (wasm_bits) { |
| 6946 | 32 => .{ .imm32 = @truncate(max_val) }, |
| 6947 | 64 => .{ .imm64 = max_val }, |
| 7052 | 6948 | else => unreachable, |
| 7053 | 6949 | }; |
| 7054 | | const min_wvalue = switch (wasm_bits) { |
| 7055 | | 32 => WValue{ .imm32 = @as(u32, @bitCast(@as(i32, @truncate(min_val)))) }, |
| 7056 | | 64 => WValue{ .imm64 = @as(u64, @bitCast(min_val)) }, |
| 6950 | const min_wvalue: WValue = switch (wasm_bits) { |
| 6951 | 32 => .{ .imm32 = @bitCast(@as(i32, @truncate(min_val))) }, |
| 6952 | 64 => .{ .imm64 = @bitCast(min_val) }, |
| 7057 | 6953 | else => unreachable, |
| 7058 | 6954 | }; |
| 7059 | 6955 | |
| ... | ... | @@ -7073,9 +6969,9 @@ fn signedSat(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerEr |
| 7073 | 6969 | try func.addLabel(.local_set, bin_result.local.value); // re-use local |
| 7074 | 6970 | return (try func.wrapOperand(bin_result, ty)).toLocal(func, ty); |
| 7075 | 6971 | } else { |
| 7076 | | const zero = switch (wasm_bits) { |
| 7077 | | 32 => WValue{ .imm32 = 0 }, |
| 7078 | | 64 => WValue{ .imm64 = 0 }, |
| 6972 | const zero: WValue = switch (wasm_bits) { |
| 6973 | 32 => .{ .imm32 = 0 }, |
| 6974 | 64 => .{ .imm64 = 0 }, |
| 7079 | 6975 | else => unreachable, |
| 7080 | 6976 | }; |
| 7081 | 6977 | try func.emitWValue(max_wvalue); |
| ... | ... | @@ -7110,7 +7006,7 @@ fn airShlSat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7110 | 7006 | const wasm_bits = toWasmBits(int_info.bits).?; |
| 7111 | 7007 | const result = try func.allocLocal(ty); |
| 7112 | 7008 | |
| 7113 | | if (wasm_bits == int_info.bits) outer_blk: { |
| 7009 | if (wasm_bits == int_info.bits) { |
| 7114 | 7010 | var shl = try (try func.binOp(lhs, rhs, ty, .shl)).toLocal(func, ty); |
| 7115 | 7011 | defer shl.free(func); |
| 7116 | 7012 | var shr = try (try func.binOp(shl, rhs, ty, .shr)).toLocal(func, ty); |
| ... | ... | @@ -7143,12 +7039,11 @@ fn airShlSat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7143 | 7039 | _ = try func.cmp(lhs, shr, ty, .neq); |
| 7144 | 7040 | try func.addTag(.select); |
| 7145 | 7041 | try func.addLabel(.local_set, result.local.value); |
| 7146 | | break :outer_blk; |
| 7147 | 7042 | } else { |
| 7148 | 7043 | const shift_size = wasm_bits - int_info.bits; |
| 7149 | | const shift_value = switch (wasm_bits) { |
| 7150 | | 32 => WValue{ .imm32 = shift_size }, |
| 7151 | | 64 => WValue{ .imm64 = shift_size }, |
| 7044 | const shift_value: WValue = switch (wasm_bits) { |
| 7045 | 32 => .{ .imm32 = shift_size }, |
| 7046 | 64 => .{ .imm64 = shift_size }, |
| 7152 | 7047 | else => unreachable, |
| 7153 | 7048 | }; |
| 7154 | 7049 | const ext_ty = try pt.intType(int_info.signedness, wasm_bits); |
| ... | ... | @@ -7232,7 +7127,7 @@ fn callIntrinsic( |
| 7232 | 7127 | const sret_local = try func.allocStack(return_type); |
| 7233 | 7128 | try func.lowerToStack(sret_local); |
| 7234 | 7129 | break :blk sret_local; |
| 7235 | | } else WValue{ .none = {} }; |
| 7130 | } else .none; |
| 7236 | 7131 | |
| 7237 | 7132 | // Lower all arguments to the stack before we call our function |
| 7238 | 7133 | for (args, 0..) |arg, arg_i| { |
| ... | ... | @@ -7245,14 +7140,14 @@ fn callIntrinsic( |
| 7245 | 7140 | try func.addLabel(.call, @intFromEnum(symbol_index)); |
| 7246 | 7141 | |
| 7247 | 7142 | if (!return_type.hasRuntimeBitsIgnoreComptime(pt)) { |
| 7248 | | return WValue.none; |
| 7143 | return .none; |
| 7249 | 7144 | } else if (return_type.isNoReturn(mod)) { |
| 7250 | 7145 | try func.addTag(.@"unreachable"); |
| 7251 | | return WValue.none; |
| 7146 | return .none; |
| 7252 | 7147 | } else if (want_sret_param) { |
| 7253 | 7148 | return sret; |
| 7254 | 7149 | } else { |
| 7255 | | return WValue{ .stack = {} }; |
| 7150 | return .stack; |
| 7256 | 7151 | } |
| 7257 | 7152 | } |
| 7258 | 7153 | |
| ... | ... | @@ -7571,7 +7466,7 @@ fn airCmpxchg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7571 | 7466 | break :val ptr_val; |
| 7572 | 7467 | }; |
| 7573 | 7468 | |
| 7574 | | const result_ptr = if (isByRef(result_ty, pt)) val: { |
| 7469 | const result = if (isByRef(result_ty, pt)) val: { |
| 7575 | 7470 | try func.emitWValue(cmp_result); |
| 7576 | 7471 | try func.addImm32(~@as(u32, 0)); |
| 7577 | 7472 | try func.addTag(.i32_xor); |
| ... | ... | @@ -7587,10 +7482,10 @@ fn airCmpxchg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7587 | 7482 | try func.emitWValue(ptr_val); |
| 7588 | 7483 | try func.emitWValue(cmp_result); |
| 7589 | 7484 | try func.addTag(.select); |
| 7590 | | break :val try WValue.toLocal(.stack, func, result_ty); |
| 7485 | break :val .stack; |
| 7591 | 7486 | }; |
| 7592 | 7487 | |
| 7593 | | return func.finishAir(inst, result_ptr, &.{ extra.ptr, extra.expected_value, extra.new_value }); |
| 7488 | return func.finishAir(inst, result, &.{ extra.ptr, extra.expected_value, extra.new_value }); |
| 7594 | 7489 | } |
| 7595 | 7490 | |
| 7596 | 7491 | fn airAtomicLoad(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -7616,8 +7511,7 @@ fn airAtomicLoad(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7616 | 7511 | _ = try func.load(ptr, ty, 0); |
| 7617 | 7512 | } |
| 7618 | 7513 | |
| 7619 | | const result = try WValue.toLocal(.stack, func, ty); |
| 7620 | | return func.finishAir(inst, result, &.{atomic_load.ptr}); |
| 7514 | return func.finishAir(inst, .stack, &.{atomic_load.ptr}); |
| 7621 | 7515 | } |
| 7622 | 7516 | |
| 7623 | 7517 | fn airAtomicRmw(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| ... | ... | @@ -7734,8 +7628,7 @@ fn airAtomicRmw(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7734 | 7628 | .offset = ptr.offset(), |
| 7735 | 7629 | .alignment = @intCast(ty.abiAlignment(pt).toByteUnits().?), |
| 7736 | 7630 | }); |
| 7737 | | const result = try WValue.toLocal(.stack, func, ty); |
| 7738 | | return func.finishAir(inst, result, &.{ pl_op.operand, extra.operand }); |
| 7631 | return func.finishAir(inst, .stack, &.{ pl_op.operand, extra.operand }); |
| 7739 | 7632 | }, |
| 7740 | 7633 | } |
| 7741 | 7634 | } else { |
| ... | ... | @@ -7847,8 +7740,7 @@ fn airFrameAddress(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7847 | 7740 | try func.initializeStack(); |
| 7848 | 7741 | } |
| 7849 | 7742 | try func.emitWValue(func.bottom_stack_value); |
| 7850 | | const result = try WValue.toLocal(.stack, func, Type.usize); |
| 7851 | | return func.finishAir(inst, result, &.{}); |
| 7743 | return func.finishAir(inst, .stack, &.{}); |
| 7852 | 7744 | } |
| 7853 | 7745 | |
| 7854 | 7746 | fn typeOf(func: *CodeGen, inst: Air.Inst.Ref) Type { |