authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-21 14:33:02+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-27 15:13:14+02:00
log54160e7f6aecb4628df633ceaef4c6d956429a3d
tree8b523c4dad676be508f680fa8840fa3430a14191
parentaf9a9a13747b1e7007e29ff4f76e700f5bd7f7cf

Sema: make overflow arithmetic builtins return tuples


9 files changed, 104 insertions(+), 175 deletions(-)

src/AstGen.zig+4-24
......@@ -2505,7 +2505,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
25052505 .err_union_code,
25062506 .err_union_code_ptr,
25072507 .ptr_type,
2508 .overflow_arithmetic_ptr,
25092508 .enum_literal,
25102509 .merge_error_sets,
25112510 .error_union_type,
......@@ -2543,7 +2542,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
25432542 .type_info,
25442543 .size_of,
25452544 .bit_size_of,
2546 .log2_int_type,
25472545 .typeof_log2_int_type,
25482546 .ptr_to_int,
25492547 .align_of,
......@@ -8236,21 +8234,7 @@ fn builtinCall(
82368234 .add_with_overflow => return overflowArithmetic(gz, scope, ri, node, params, .add_with_overflow),
82378235 .sub_with_overflow => return overflowArithmetic(gz, scope, ri, node, params, .sub_with_overflow),
82388236 .mul_with_overflow => return overflowArithmetic(gz, scope, ri, node, params, .mul_with_overflow),
8239 .shl_with_overflow => {
8240 const int_type = try typeExpr(gz, scope, params[0]);
8241 const log2_int_type = try gz.addUnNode(.log2_int_type, int_type, params[0]);
8242 const ptr_type = try gz.addUnNode(.overflow_arithmetic_ptr, int_type, params[0]);
8243 const lhs = try expr(gz, scope, .{ .rl = .{ .ty = int_type } }, params[1]);
8244 const rhs = try expr(gz, scope, .{ .rl = .{ .ty = log2_int_type } }, params[2]);
8245 const ptr = try expr(gz, scope, .{ .rl = .{ .ty = ptr_type } }, params[3]);
8246 const result = try gz.addExtendedPayload(.shl_with_overflow, Zir.Inst.OverflowArithmetic{
8247 .node = gz.nodeIndexToRelative(node),
8248 .lhs = lhs,
8249 .rhs = rhs,
8250 .ptr = ptr,
8251 });
8252 return rvalue(gz, ri, result, node);
8253 },
8237 .shl_with_overflow => return overflowArithmetic(gz, scope, ri, node, params, .shl_with_overflow),
82548238
82558239 .atomic_load => {
82568240 const result = try gz.addPlNode(.atomic_load, node, Zir.Inst.AtomicLoad{
......@@ -8691,16 +8675,12 @@ fn overflowArithmetic(
86918675 params: []const Ast.Node.Index,
86928676 tag: Zir.Inst.Extended,
86938677) InnerError!Zir.Inst.Ref {
8694 const int_type = try typeExpr(gz, scope, params[0]);
8695 const ptr_type = try gz.addUnNode(.overflow_arithmetic_ptr, int_type, params[0]);
8696 const lhs = try expr(gz, scope, .{ .rl = .{ .ty = int_type } }, params[1]);
8697 const rhs = try expr(gz, scope, .{ .rl = .{ .ty = int_type } }, params[2]);
8698 const ptr = try expr(gz, scope, .{ .rl = .{ .ty = ptr_type } }, params[3]);
8699 const result = try gz.addExtendedPayload(tag, Zir.Inst.OverflowArithmetic{
8678 const lhs = try expr(gz, scope, .{ .rl = .none }, params[0]);
8679 const rhs = try expr(gz, scope, .{ .rl = .none }, params[1]);
8680 const result = try gz.addExtendedPayload(tag, Zir.Inst.BinNode{
87008681 .node = gz.nodeIndexToRelative(node),
87018682 .lhs = lhs,
87028683 .rhs = rhs,
8703 .ptr = ptr,
87048684 });
87058685 return rvalue(gz, ri, result, node);
87068686}
src/Autodoc.zig-20
......@@ -1510,26 +1510,6 @@ fn walkInstruction(
15101510
15111511 // return operand;
15121512 // },
1513 .overflow_arithmetic_ptr => {
1514 const un_node = data[inst_index].un_node;
1515
1516 const elem_type_ref = try self.walkRef(file, parent_scope, parent_src, un_node.operand, false);
1517 const type_slot_index = self.types.items.len;
1518 try self.types.append(self.arena, .{
1519 .Pointer = .{
1520 .size = .One,
1521 .child = elem_type_ref.expr,
1522 .is_mutable = true,
1523 .is_volatile = false,
1524 .is_allowzero = false,
1525 },
1526 });
1527
1528 return DocData.WalkResult{
1529 .typeRef = .{ .type = @enumToInt(Ref.type_type) },
1530 .expr = .{ .type = type_slot_index },
1531 };
1532 },
15331513 .ptr_type => {
15341514 const ptr = data[inst_index].ptr_type;
15351515 const extra = file.zir.extraData(Zir.Inst.PtrType, ptr.payload_index);
src/BuiltinFn.zig+4-4
......@@ -154,7 +154,7 @@ pub const list = list: {
154154 "@addWithOverflow",
155155 .{
156156 .tag = .add_with_overflow,
157 .param_count = 4,
157 .param_count = 2,
158158 },
159159 },
160160 .{
......@@ -636,7 +636,7 @@ pub const list = list: {
636636 "@mulWithOverflow",
637637 .{
638638 .tag = .mul_with_overflow,
639 .param_count = 4,
639 .param_count = 2,
640640 },
641641 },
642642 .{
......@@ -741,7 +741,7 @@ pub const list = list: {
741741 "@shlWithOverflow",
742742 .{
743743 .tag = .shl_with_overflow,
744 .param_count = 4,
744 .param_count = 2,
745745 },
746746 },
747747 .{
......@@ -889,7 +889,7 @@ pub const list = list: {
889889 "@subWithOverflow",
890890 .{
891891 .tag = .sub_with_overflow,
892 .param_count = 4,
892 .param_count = 2,
893893 },
894894 },
895895 .{
src/Sema.zig+78-88
......@@ -971,7 +971,6 @@ fn analyzeBodyInner(
971971 .optional_payload_unsafe_ptr => try sema.zirOptionalPayloadPtr(block, inst, false),
972972 .optional_type => try sema.zirOptionalType(block, inst),
973973 .ptr_type => try sema.zirPtrType(block, inst),
974 .overflow_arithmetic_ptr => try sema.zirOverflowArithmeticPtr(block, inst),
975974 .ref => try sema.zirRef(block, inst),
976975 .ret_err_value_code => try sema.zirRetErrValueCode(inst),
977976 .shr => try sema.zirShr(block, inst, .shr),
......@@ -993,7 +992,6 @@ fn analyzeBodyInner(
993992 .bit_size_of => try sema.zirBitSizeOf(block, inst),
994993 .typeof => try sema.zirTypeof(block, inst),
995994 .typeof_builtin => try sema.zirTypeofBuiltin(block, inst),
996 .log2_int_type => try sema.zirLog2IntType(block, inst),
997995 .typeof_log2_int_type => try sema.zirTypeofLog2IntType(block, inst),
998996 .xor => try sema.zirBitwise(block, inst, .xor),
999997 .struct_init_empty => try sema.zirStructInitEmpty(block, inst),
......@@ -11762,7 +11760,7 @@ fn zirShl(
1176211760 if (scalar_ty.zigTypeTag() == .ComptimeInt) {
1176311761 break :val shifted.wrapped_result;
1176411762 }
11765 if (shifted.overflowed.compareAllWithZero(.eq)) {
11763 if (shifted.overflow_bit.compareAllWithZero(.eq)) {
1176611764 break :val shifted.wrapped_result;
1176711765 }
1176811766 return sema.fail(block, src, "operation caused overflow", .{});
......@@ -13783,24 +13781,37 @@ fn zirOverflowArithmetic(
1378313781 const tracy = trace(@src());
1378413782 defer tracy.end();
1378513783
13786 const extra = sema.code.extraData(Zir.Inst.OverflowArithmetic, extended.operand).data;
13784 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
1378713785 const src = LazySrcLoc.nodeOffset(extra.node);
1378813786
1378913787 const lhs_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
1379013788 const rhs_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = extra.node };
13791 const ptr_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = extra.node };
1379213789
13793 const lhs = try sema.resolveInst(extra.lhs);
13794 const rhs = try sema.resolveInst(extra.rhs);
13795 const ptr = try sema.resolveInst(extra.ptr);
13790 const uncasted_lhs = try sema.resolveInst(extra.lhs);
13791 const uncasted_rhs = try sema.resolveInst(extra.rhs);
1379613792
13797 const lhs_ty = sema.typeOf(lhs);
13798 const rhs_ty = sema.typeOf(rhs);
13793 const lhs_ty = sema.typeOf(uncasted_lhs);
13794 const rhs_ty = sema.typeOf(uncasted_rhs);
1379913795 const mod = sema.mod;
1380013796
13801 // Note, the types of lhs/rhs (also for shifting)/ptr are already correct as ensured by astgen.
1380213797 try sema.checkVectorizableBinaryOperands(block, src, lhs_ty, rhs_ty, lhs_src, rhs_src);
13803 const dest_ty = lhs_ty;
13798
13799 const instructions = &[_]Air.Inst.Ref{ uncasted_lhs, uncasted_rhs };
13800 const dest_ty = if (zir_tag == .shl_with_overflow)
13801 lhs_ty
13802 else
13803 try sema.resolvePeerTypes(block, src, instructions, .{
13804 .override = &[_]LazySrcLoc{ lhs_src, rhs_src },
13805 });
13806
13807 const rhs_dest_ty = if (zir_tag == .shl_with_overflow)
13808 try sema.log2IntType(block, lhs_ty, src)
13809 else
13810 dest_ty;
13811
13812 const lhs = try sema.coerce(block, dest_ty, uncasted_lhs, lhs_src);
13813 const rhs = try sema.coerce(block, rhs_dest_ty, uncasted_rhs, rhs_src);
13814
1380413815 if (dest_ty.scalarType().zigTypeTag() != .Int) {
1380513816 return sema.fail(block, src, "expected vector of integers or integer tag type, found '{}'", .{dest_ty.fmt(mod)});
1380613817 }
......@@ -13809,14 +13820,11 @@ fn zirOverflowArithmetic(
1380913820 const maybe_rhs_val = try sema.resolveMaybeUndefVal(rhs);
1381013821
1381113822 const tuple_ty = try sema.overflowArithmeticTupleType(dest_ty);
13812 // TODO: Remove and use `ov_ty` instead.
13813 // This is a temporary type used until overflow arithmetic properly returns `u1` instead of `bool`.
13814 const overflowed_ty = if (dest_ty.zigTypeTag() == .Vector) try Type.vector(sema.arena, dest_ty.vectorLen(), Type.bool) else Type.bool;
13815
13816 const result: struct {
13817 /// TODO: Rename to `overflow_bit` and make of type `u1`.
13818 overflowed: Air.Inst.Ref,
13819 wrapped: Air.Inst.Ref,
13823
13824 var result: struct {
13825 inst: Air.Inst.Ref = .none,
13826 wrapped: Value = Value.initTag(.unreachable_value),
13827 overflow_bit: Value,
1382013828 } = result: {
1382113829 switch (zir_tag) {
1382213830 .add_with_overflow => {
......@@ -13825,24 +13833,22 @@ fn zirOverflowArithmetic(
1382513833 // Otherwise, if either of the argument is undefined, undefined is returned.
1382613834 if (maybe_lhs_val) |lhs_val| {
1382713835 if (!lhs_val.isUndef() and (try lhs_val.compareAllWithZeroAdvanced(.eq, sema))) {
13828 break :result .{ .overflowed = try sema.addBool(overflowed_ty, false), .wrapped = rhs };
13836 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = rhs };
1382913837 }
1383013838 }
1383113839 if (maybe_rhs_val) |rhs_val| {
1383213840 if (!rhs_val.isUndef() and (try rhs_val.compareAllWithZeroAdvanced(.eq, sema))) {
13833 break :result .{ .overflowed = try sema.addBool(overflowed_ty, false), .wrapped = lhs };
13841 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = lhs };
1383413842 }
1383513843 }
1383613844 if (maybe_lhs_val) |lhs_val| {
1383713845 if (maybe_rhs_val) |rhs_val| {
1383813846 if (lhs_val.isUndef() or rhs_val.isUndef()) {
13839 break :result .{ .overflowed = try sema.addConstUndef(overflowed_ty), .wrapped = try sema.addConstUndef(dest_ty) };
13847 break :result .{ .overflow_bit = Value.undef, .wrapped = Value.undef };
1384013848 }
1384113849
1384213850 const result = try sema.intAddWithOverflow(lhs_val, rhs_val, dest_ty);
13843 const overflowed = try sema.addConstant(overflowed_ty, result.overflowed);
13844 const wrapped = try sema.addConstant(dest_ty, result.wrapped_result);
13845 break :result .{ .overflowed = overflowed, .wrapped = wrapped };
13851 break :result .{ .overflow_bit = result.overflow_bit, .wrapped = result.wrapped_result };
1384613852 }
1384713853 }
1384813854 },
......@@ -13851,18 +13857,16 @@ fn zirOverflowArithmetic(
1385113857 // Otherwise, if either result is undefined, both results are undefined.
1385213858 if (maybe_rhs_val) |rhs_val| {
1385313859 if (rhs_val.isUndef()) {
13854 break :result .{ .overflowed = try sema.addConstUndef(overflowed_ty), .wrapped = try sema.addConstUndef(dest_ty) };
13860 break :result .{ .overflow_bit = Value.undef, .wrapped = Value.undef };
1385513861 } else if (try rhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
13856 break :result .{ .overflowed = try sema.addBool(overflowed_ty, false), .wrapped = lhs };
13862 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = lhs };
1385713863 } else if (maybe_lhs_val) |lhs_val| {
1385813864 if (lhs_val.isUndef()) {
13859 break :result .{ .overflowed = try sema.addConstUndef(overflowed_ty), .wrapped = try sema.addConstUndef(dest_ty) };
13865 break :result .{ .overflow_bit = Value.undef, .wrapped = Value.undef };
1386013866 }
1386113867
1386213868 const result = try sema.intSubWithOverflow(lhs_val, rhs_val, dest_ty);
13863 const overflowed = try sema.addConstant(overflowed_ty, result.overflowed);
13864 const wrapped = try sema.addConstant(dest_ty, result.wrapped_result);
13865 break :result .{ .overflowed = overflowed, .wrapped = wrapped };
13869 break :result .{ .overflow_bit = result.overflow_bit, .wrapped = result.wrapped_result };
1386613870 }
1386713871 }
1386813872 },
......@@ -13873,9 +13877,9 @@ fn zirOverflowArithmetic(
1387313877 if (maybe_lhs_val) |lhs_val| {
1387413878 if (!lhs_val.isUndef()) {
1387513879 if (try lhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
13876 break :result .{ .overflowed = try sema.addBool(overflowed_ty, false), .wrapped = lhs };
13877 } else if (try sema.compareAll(lhs_val, .eq, Value.one, dest_ty)) {
13878 break :result .{ .overflowed = try sema.addBool(overflowed_ty, false), .wrapped = rhs };
13880 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = lhs };
13881 } else if (try sema.compareAll(lhs_val, .eq, try maybeRepeated(sema, dest_ty, Value.one), dest_ty)) {
13882 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = rhs };
1387913883 }
1388013884 }
1388113885 }
......@@ -13883,9 +13887,9 @@ fn zirOverflowArithmetic(
1388313887 if (maybe_rhs_val) |rhs_val| {
1388413888 if (!rhs_val.isUndef()) {
1388513889 if (try rhs_val.compareAllWithZeroAdvanced(.eq, sema)) {
13886 break :result .{ .overflowed = try sema.addBool(overflowed_ty, false), .wrapped = rhs };
13887 } else if (try sema.compareAll(rhs_val, .eq, Value.one, dest_ty)) {
13888 break :result .{ .overflowed = try sema.addBool(overflowed_ty, false), .wrapped = lhs };
13890 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = rhs };
13891 } else if (try sema.compareAll(rhs_val, .eq, try maybeRepeated(sema, dest_ty, Value.one), dest_ty)) {
13892 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = lhs };
1388913893 }
1389013894 }
1389113895 }
......@@ -13893,13 +13897,11 @@ fn zirOverflowArithmetic(
1389313897 if (maybe_lhs_val) |lhs_val| {
1389413898 if (maybe_rhs_val) |rhs_val| {
1389513899 if (lhs_val.isUndef() or rhs_val.isUndef()) {
13896 break :result .{ .overflowed = try sema.addConstUndef(overflowed_ty), .wrapped = try sema.addConstUndef(dest_ty) };
13900 break :result .{ .overflow_bit = Value.undef, .wrapped = Value.undef };
1389713901 }
1389813902
1389913903 const result = try lhs_val.intMulWithOverflow(rhs_val, dest_ty, sema.arena, mod);
13900 const overflowed = try sema.addConstant(overflowed_ty, result.overflowed);
13901 const wrapped = try sema.addConstant(dest_ty, result.wrapped_result);
13902 break :result .{ .overflowed = overflowed, .wrapped = wrapped };
13904 break :result .{ .overflow_bit = result.overflow_bit, .wrapped = result.wrapped_result };
1390313905 }
1390413906 }
1390513907 },
......@@ -13909,24 +13911,22 @@ fn zirOverflowArithmetic(
1390913911 // Oterhwise if either of the arguments is undefined, both results are undefined.
1391013912 if (maybe_lhs_val) |lhs_val| {
1391113913 if (!lhs_val.isUndef() and (try lhs_val.compareAllWithZeroAdvanced(.eq, sema))) {
13912 break :result .{ .overflowed = try sema.addBool(overflowed_ty, false), .wrapped = lhs };
13914 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = lhs };
1391313915 }
1391413916 }
1391513917 if (maybe_rhs_val) |rhs_val| {
1391613918 if (!rhs_val.isUndef() and (try rhs_val.compareAllWithZeroAdvanced(.eq, sema))) {
13917 break :result .{ .overflowed = try sema.addBool(overflowed_ty, false), .wrapped = lhs };
13919 break :result .{ .overflow_bit = try maybeRepeated(sema, dest_ty, Value.zero), .inst = lhs };
1391813920 }
1391913921 }
1392013922 if (maybe_lhs_val) |lhs_val| {
1392113923 if (maybe_rhs_val) |rhs_val| {
1392213924 if (lhs_val.isUndef() or rhs_val.isUndef()) {
13923 break :result .{ .overflowed = try sema.addConstUndef(overflowed_ty), .wrapped = try sema.addConstUndef(dest_ty) };
13925 break :result .{ .overflow_bit = Value.undef, .wrapped = Value.undef };
1392413926 }
1392513927
1392613928 const result = try lhs_val.shlWithOverflow(rhs_val, dest_ty, sema.arena, sema.mod);
13927 const overflowed = try sema.addConstant(overflowed_ty, result.overflowed);
13928 const wrapped = try sema.addConstant(dest_ty, result.wrapped_result);
13929 break :result .{ .overflowed = overflowed, .wrapped = wrapped };
13929 break :result .{ .overflow_bit = result.overflow_bit, .wrapped = result.wrapped_result };
1393013930 }
1393113931 }
1393213932 },
......@@ -13944,7 +13944,7 @@ fn zirOverflowArithmetic(
1394413944 const runtime_src = if (maybe_lhs_val == null) lhs_src else rhs_src;
1394513945 try sema.requireRuntimeBlock(block, src, runtime_src);
1394613946
13947 const tuple = try block.addInst(.{
13947 return block.addInst(.{
1394813948 .tag = air_tag,
1394913949 .data = .{ .ty_pl = .{
1395013950 .ty = try block.sema.addType(tuple_ty),
......@@ -13954,16 +13954,32 @@ fn zirOverflowArithmetic(
1395413954 }),
1395513955 } },
1395613956 });
13957 };
1395713958
13958 const wrapped = try sema.tupleFieldValByIndex(block, src, tuple, 0, tuple_ty);
13959 try sema.storePtr2(block, src, ptr, ptr_src, wrapped, src, .store);
13959 if (result.inst != .none) {
13960 if (try sema.resolveMaybeUndefVal(result.inst)) |some| {
13961 result.wrapped = some;
13962 result.inst = .none;
13963 }
13964 }
1396013965
13961 const overflow_bit = try sema.tupleFieldValByIndex(block, src, tuple, 1, tuple_ty);
13962 return block.addBitCast(overflowed_ty, overflow_bit);
13963 };
13966 if (result.inst == .none) {
13967 const values = try sema.arena.alloc(Value, 2);
13968 values[0] = result.wrapped;
13969 values[1] = result.overflow_bit;
13970 const tuple_val = try Value.Tag.aggregate.create(sema.arena, values);
13971 return sema.addConstant(tuple_ty, tuple_val);
13972 }
13973
13974 const element_refs = try sema.arena.alloc(Air.Inst.Ref, 2);
13975 element_refs[0] = result.inst;
13976 element_refs[1] = try sema.addConstant(tuple_ty.structFieldType(1), result.overflow_bit);
13977 return block.addAggregateInit(tuple_ty, element_refs);
13978}
1396413979
13965 try sema.storePtr2(block, src, ptr, ptr_src, result.wrapped, src, .store);
13966 return result.overflowed;
13980fn maybeRepeated(sema: *Sema, ty: Type, val: Value) !Value {
13981 if (ty.zigTypeTag() != .Vector) return val;
13982 return Value.Tag.repeated.create(sema.arena, val);
1396713983}
1396813984
1396913985fn overflowArithmeticTupleType(sema: *Sema, ty: Type) !Type {
......@@ -16211,14 +16227,6 @@ fn zirTypeofLog2IntType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil
1621116227 return sema.addType(res_ty);
1621216228}
1621316229
16214fn zirLog2IntType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
16215 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
16216 const src = inst_data.src();
16217 const operand = try sema.resolveType(block, src, inst_data.operand);
16218 const res_ty = try sema.log2IntType(block, operand, src);
16219 return sema.addType(res_ty);
16220}
16221
1622216230fn log2IntType(sema: *Sema, block: *Block, operand: Type, src: LazySrcLoc) CompileError!Type {
1622316231 switch (operand.zigTypeTag()) {
1622416232 .ComptimeInt => return Type.comptime_int,
......@@ -17039,24 +17047,6 @@ fn floatOpAllowed(tag: Zir.Inst.Tag) bool {
1703917047 };
1704017048}
1704117049
17042fn zirOverflowArithmeticPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
17043 const tracy = trace(@src());
17044 defer tracy.end();
17045
17046 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
17047 const elem_ty_src = inst_data.src();
17048 const elem_type = try sema.resolveType(block, elem_ty_src, inst_data.operand);
17049 const ty = try Type.ptr(sema.arena, sema.mod, .{
17050 .pointee_type = elem_type,
17051 .@"addrspace" = .generic,
17052 .mutable = true,
17053 .@"allowzero" = false,
17054 .@"volatile" = false,
17055 .size = .One,
17056 });
17057 return sema.addType(ty);
17058}
17059
1706017050fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1706117051 const tracy = trace(@src());
1706217052 defer tracy.end();
......@@ -32613,11 +32603,11 @@ fn intSubWithOverflow(
3261332603 const lhs_elem = lhs.elemValueBuffer(sema.mod, i, &lhs_buf);
3261432604 const rhs_elem = rhs.elemValueBuffer(sema.mod, i, &rhs_buf);
3261532605 const of_math_result = try sema.intSubWithOverflowScalar(lhs_elem, rhs_elem, ty.scalarType());
32616 overflowed_data[i] = of_math_result.overflowed;
32606 overflowed_data[i] = of_math_result.overflow_bit;
3261732607 scalar.* = of_math_result.wrapped_result;
3261832608 }
3261932609 return Value.OverflowArithmeticResult{
32620 .overflowed = try Value.Tag.aggregate.create(sema.arena, overflowed_data),
32610 .overflow_bit = try Value.Tag.aggregate.create(sema.arena, overflowed_data),
3262132611 .wrapped_result = try Value.Tag.aggregate.create(sema.arena, result_data),
3262232612 };
3262332613 }
......@@ -32645,7 +32635,7 @@ fn intSubWithOverflowScalar(
3264532635 const overflowed = result_bigint.subWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits);
3264632636 const wrapped_result = try Value.fromBigInt(sema.arena, result_bigint.toConst());
3264732637 return Value.OverflowArithmeticResult{
32648 .overflowed = Value.makeBool(overflowed),
32638 .overflow_bit = Value.boolToInt(overflowed),
3264932639 .wrapped_result = wrapped_result,
3265032640 };
3265132641}
......@@ -32964,11 +32954,11 @@ fn intAddWithOverflow(
3296432954 const lhs_elem = lhs.elemValueBuffer(sema.mod, i, &lhs_buf);
3296532955 const rhs_elem = rhs.elemValueBuffer(sema.mod, i, &rhs_buf);
3296632956 const of_math_result = try sema.intAddWithOverflowScalar(lhs_elem, rhs_elem, ty.scalarType());
32967 overflowed_data[i] = of_math_result.overflowed;
32957 overflowed_data[i] = of_math_result.overflow_bit;
3296832958 scalar.* = of_math_result.wrapped_result;
3296932959 }
3297032960 return Value.OverflowArithmeticResult{
32971 .overflowed = try Value.Tag.aggregate.create(sema.arena, overflowed_data),
32961 .overflow_bit = try Value.Tag.aggregate.create(sema.arena, overflowed_data),
3297232962 .wrapped_result = try Value.Tag.aggregate.create(sema.arena, result_data),
3297332963 };
3297432964 }
......@@ -32996,7 +32986,7 @@ fn intAddWithOverflowScalar(
3299632986 const overflowed = result_bigint.addWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits);
3299732987 const result = try Value.fromBigInt(sema.arena, result_bigint.toConst());
3299832988 return Value.OverflowArithmeticResult{
32999 .overflowed = Value.makeBool(overflowed),
32989 .overflow_bit = Value.boolToInt(overflowed),
3300032990 .wrapped_result = result,
3300132991 };
3300232992}
src/TypedValue.zig+1-3
......@@ -225,9 +225,7 @@ pub fn print(
225225 .one => return writer.writeAll("1"),
226226 .void_value => return writer.writeAll("{}"),
227227 .unreachable_value => return writer.writeAll("unreachable"),
228 .the_only_possible_value => {
229 val = ty.onePossibleValue().?;
230 },
228 .the_only_possible_value => return writer.writeAll("0"),
231229 .bool_true => return writer.writeAll("true"),
232230 .bool_false => return writer.writeAll("false"),
233231 .ty => return val.castTag(.ty).?.data.print(writer, mod),
src/Zir.zig+4-23
......@@ -539,9 +539,6 @@ pub const Inst = struct {
539539 /// Obtains the return type of the in-scope function.
540540 /// Uses the `node` union field.
541541 ret_type,
542 /// Create a pointer type for overflow arithmetic.
543 /// TODO remove when doing https://github.com/ziglang/zig/issues/10248
544 overflow_arithmetic_ptr,
545542 /// Create a pointer type which can have a sentinel, alignment, address space, and/or bit range.
546543 /// Uses the `ptr_type` union field.
547544 ptr_type,
......@@ -600,9 +597,6 @@ pub const Inst = struct {
600597 /// Returns the integer type for the RHS of a shift operation.
601598 /// Uses the `un_node` field.
602599 typeof_log2_int_type,
603 /// Given an integer type, returns the integer type for the RHS of a shift operation.
604 /// Uses the `un_node` field.
605 log2_int_type,
606600 /// Asserts control-flow will not reach this instruction (`unreachable`).
607601 /// Uses the `unreachable` union field.
608602 @"unreachable",
......@@ -1121,7 +1115,6 @@ pub const Inst = struct {
11211115 .err_union_code,
11221116 .err_union_code_ptr,
11231117 .ptr_type,
1124 .overflow_arithmetic_ptr,
11251118 .enum_literal,
11261119 .merge_error_sets,
11271120 .error_union_type,
......@@ -1132,7 +1125,6 @@ pub const Inst = struct {
11321125 .slice_sentinel,
11331126 .import,
11341127 .typeof_log2_int_type,
1135 .log2_int_type,
11361128 .resolve_inferred_alloc,
11371129 .set_eval_branch_quota,
11381130 .switch_capture,
......@@ -1422,7 +1414,6 @@ pub const Inst = struct {
14221414 .err_union_code,
14231415 .err_union_code_ptr,
14241416 .ptr_type,
1425 .overflow_arithmetic_ptr,
14261417 .enum_literal,
14271418 .merge_error_sets,
14281419 .error_union_type,
......@@ -1433,7 +1424,6 @@ pub const Inst = struct {
14331424 .slice_sentinel,
14341425 .import,
14351426 .typeof_log2_int_type,
1436 .log2_int_type,
14371427 .switch_capture,
14381428 .switch_capture_ref,
14391429 .switch_capture_multi,
......@@ -1664,7 +1654,6 @@ pub const Inst = struct {
16641654 .ret_err_value_code = .str_tok,
16651655 .ret_ptr = .node,
16661656 .ret_type = .node,
1667 .overflow_arithmetic_ptr = .un_node,
16681657 .ptr_type = .ptr_type,
16691658 .slice_start = .pl_node,
16701659 .slice_end = .pl_node,
......@@ -1678,7 +1667,6 @@ pub const Inst = struct {
16781667 .negate_wrap = .un_node,
16791668 .typeof = .un_node,
16801669 .typeof_log2_int_type = .un_node,
1681 .log2_int_type = .un_node,
16821670 .@"unreachable" = .@"unreachable",
16831671 .xor = .pl_node,
16841672 .optional_type = .un_node,
......@@ -1916,19 +1904,19 @@ pub const Inst = struct {
19161904 /// The AST node is the builtin call.
19171905 typeof_peer,
19181906 /// Implements the `@addWithOverflow` builtin.
1919 /// `operand` is payload index to `OverflowArithmetic`.
1907 /// `operand` is payload index to `BinNode`.
19201908 /// `small` is unused.
19211909 add_with_overflow,
19221910 /// Implements the `@subWithOverflow` builtin.
1923 /// `operand` is payload index to `OverflowArithmetic`.
1911 /// `operand` is payload index to `BinNode`.
19241912 /// `small` is unused.
19251913 sub_with_overflow,
19261914 /// Implements the `@mulWithOverflow` builtin.
1927 /// `operand` is payload index to `OverflowArithmetic`.
1915 /// `operand` is payload index to `BinNode`.
19281916 /// `small` is unused.
19291917 mul_with_overflow,
19301918 /// Implements the `@shlWithOverflow` builtin.
1931 /// `operand` is payload index to `OverflowArithmetic`.
1919 /// `operand` is payload index to `BinNode`.
19321920 /// `small` is unused.
19331921 shl_with_overflow,
19341922 /// `operand` is payload index to `UnNode`.
......@@ -3430,13 +3418,6 @@ pub const Inst = struct {
34303418 field_name: Ref,
34313419 };
34323420
3433 pub const OverflowArithmetic = struct {
3434 node: i32,
3435 lhs: Ref,
3436 rhs: Ref,
3437 ptr: Ref,
3438 };
3439
34403421 pub const Cmpxchg = struct {
34413422 node: i32,
34423423 ptr: Ref,
src/print_zir.zig+1-5
......@@ -185,7 +185,6 @@ const Writer = struct {
185185 .size_of,
186186 .bit_size_of,
187187 .typeof_log2_int_type,
188 .log2_int_type,
189188 .ptr_to_int,
190189 .compile_error,
191190 .set_eval_branch_quota,
......@@ -230,7 +229,6 @@ const Writer = struct {
230229 .validate_struct_init_ty,
231230 .make_ptr_const,
232231 .validate_deref,
233 .overflow_arithmetic_ptr,
234232 .check_comptime_control_flow,
235233 => try self.writeUnNode(stream, inst),
236234
......@@ -1153,14 +1151,12 @@ const Writer = struct {
11531151 }
11541152
11551153 fn writeOverflowArithmetic(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
1156 const extra = self.code.extraData(Zir.Inst.OverflowArithmetic, extended.operand).data;
1154 const extra = self.code.extraData(Zir.Inst.BinNode, extended.operand).data;
11571155 const src = LazySrcLoc.nodeOffset(extra.node);
11581156
11591157 try self.writeInstRef(stream, extra.lhs);
11601158 try stream.writeAll(", ");
11611159 try self.writeInstRef(stream, extra.rhs);
1162 try stream.writeAll(", ");
1163 try self.writeInstRef(stream, extra.ptr);
11641160 try stream.writeAll(")) ");
11651161 try self.writeSrc(stream, src);
11661162 }
src/type.zig+1
......@@ -3123,6 +3123,7 @@ pub const Type = extern union {
31233123 for (tuple.types) |field_ty, i| {
31243124 const val = tuple.values[i];
31253125 if (val.tag() != .unreachable_value) continue; // comptime field
3126 if (!(field_ty.hasRuntimeBits())) continue;
31263127
31273128 switch (try field_ty.abiAlignmentAdvanced(target, strat)) {
31283129 .scalar => |field_align| big_align = @max(big_align, field_align),
src/value.zig+11-8
......@@ -3259,8 +3259,7 @@ pub const Value = extern union {
32593259 }
32603260
32613261 pub const OverflowArithmeticResult = struct {
3262 /// TODO: Rename to `overflow_bit` and make of type `u1`.
3263 overflowed: Value,
3262 overflow_bit: Value,
32643263 wrapped_result: Value,
32653264 };
32663265
......@@ -3395,11 +3394,11 @@ pub const Value = extern union {
33953394 const lhs_elem = lhs.elemValueBuffer(mod, i, &lhs_buf);
33963395 const rhs_elem = rhs.elemValueBuffer(mod, i, &rhs_buf);
33973396 const of_math_result = try intMulWithOverflowScalar(lhs_elem, rhs_elem, ty.scalarType(), arena, target);
3398 overflowed_data[i] = of_math_result.overflowed;
3397 overflowed_data[i] = of_math_result.overflow_bit;
33993398 scalar.* = of_math_result.wrapped_result;
34003399 }
34013400 return OverflowArithmeticResult{
3402 .overflowed = try Value.Tag.aggregate.create(arena, overflowed_data),
3401 .overflow_bit = try Value.Tag.aggregate.create(arena, overflowed_data),
34033402 .wrapped_result = try Value.Tag.aggregate.create(arena, result_data),
34043403 };
34053404 }
......@@ -3436,7 +3435,7 @@ pub const Value = extern union {
34363435 }
34373436
34383437 return OverflowArithmeticResult{
3439 .overflowed = makeBool(overflowed),
3438 .overflow_bit = boolToInt(overflowed),
34403439 .wrapped_result = try fromBigInt(arena, result_bigint.toConst()),
34413440 };
34423441 }
......@@ -4141,11 +4140,11 @@ pub const Value = extern union {
41414140 const lhs_elem = lhs.elemValueBuffer(mod, i, &lhs_buf);
41424141 const rhs_elem = rhs.elemValueBuffer(mod, i, &rhs_buf);
41434142 const of_math_result = try shlWithOverflowScalar(lhs_elem, rhs_elem, ty.scalarType(), allocator, target);
4144 overflowed_data[i] = of_math_result.overflowed;
4143 overflowed_data[i] = of_math_result.overflow_bit;
41454144 scalar.* = of_math_result.wrapped_result;
41464145 }
41474146 return OverflowArithmeticResult{
4148 .overflowed = try Value.Tag.aggregate.create(allocator, overflowed_data),
4147 .overflow_bit = try Value.Tag.aggregate.create(allocator, overflowed_data),
41494148 .wrapped_result = try Value.Tag.aggregate.create(allocator, result_data),
41504149 };
41514150 }
......@@ -4178,7 +4177,7 @@ pub const Value = extern union {
41784177 result_bigint.truncate(result_bigint.toConst(), info.signedness, info.bits);
41794178 }
41804179 return OverflowArithmeticResult{
4181 .overflowed = makeBool(overflowed),
4180 .overflow_bit = boolToInt(overflowed),
41824181 .wrapped_result = try fromBigInt(allocator, result_bigint.toConst()),
41834182 };
41844183 }
......@@ -5492,6 +5491,10 @@ pub const Value = extern union {
54925491 return if (x) Value.true else Value.false;
54935492 }
54945493
5494 pub fn boolToInt(x: bool) Value {
5495 return if (x) Value.one else Value.zero;
5496 }
5497
54955498 pub const RuntimeIndex = enum(u32) {
54965499 zero = 0,
54975500 comptime_field_ptr = std.math.maxInt(u32),