Skip to content

Commit

Permalink
Sema: make overflow arithmetic builtins return tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexu committed Dec 21, 2022
1 parent a52dcdd commit 3e39cd5
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 175 deletions.
28 changes: 4 additions & 24 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2505,7 +2505,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
.err_union_code,
.err_union_code_ptr,
.ptr_type,
.overflow_arithmetic_ptr,
.enum_literal,
.merge_error_sets,
.error_union_type,
Expand Down Expand Up @@ -2543,7 +2542,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
.type_info,
.size_of,
.bit_size_of,
.log2_int_type,
.typeof_log2_int_type,
.ptr_to_int,
.align_of,
Expand Down Expand Up @@ -8236,21 +8234,7 @@ fn builtinCall(
.add_with_overflow => return overflowArithmetic(gz, scope, ri, node, params, .add_with_overflow),
.sub_with_overflow => return overflowArithmetic(gz, scope, ri, node, params, .sub_with_overflow),
.mul_with_overflow => return overflowArithmetic(gz, scope, ri, node, params, .mul_with_overflow),
.shl_with_overflow => {
const int_type = try typeExpr(gz, scope, params[0]);
const log2_int_type = try gz.addUnNode(.log2_int_type, int_type, params[0]);
const ptr_type = try gz.addUnNode(.overflow_arithmetic_ptr, int_type, params[0]);
const lhs = try expr(gz, scope, .{ .rl = .{ .ty = int_type } }, params[1]);
const rhs = try expr(gz, scope, .{ .rl = .{ .ty = log2_int_type } }, params[2]);
const ptr = try expr(gz, scope, .{ .rl = .{ .ty = ptr_type } }, params[3]);
const result = try gz.addExtendedPayload(.shl_with_overflow, Zir.Inst.OverflowArithmetic{
.node = gz.nodeIndexToRelative(node),
.lhs = lhs,
.rhs = rhs,
.ptr = ptr,
});
return rvalue(gz, ri, result, node);
},
.shl_with_overflow => return overflowArithmetic(gz, scope, ri, node, params, .shl_with_overflow),

.atomic_load => {
const result = try gz.addPlNode(.atomic_load, node, Zir.Inst.AtomicLoad{
Expand Down Expand Up @@ -8691,16 +8675,12 @@ fn overflowArithmetic(
params: []const Ast.Node.Index,
tag: Zir.Inst.Extended,
) InnerError!Zir.Inst.Ref {
const int_type = try typeExpr(gz, scope, params[0]);
const ptr_type = try gz.addUnNode(.overflow_arithmetic_ptr, int_type, params[0]);
const lhs = try expr(gz, scope, .{ .rl = .{ .ty = int_type } }, params[1]);
const rhs = try expr(gz, scope, .{ .rl = .{ .ty = int_type } }, params[2]);
const ptr = try expr(gz, scope, .{ .rl = .{ .ty = ptr_type } }, params[3]);
const result = try gz.addExtendedPayload(tag, Zir.Inst.OverflowArithmetic{
const lhs = try expr(gz, scope, .{ .rl = .none }, params[0]);
const rhs = try expr(gz, scope, .{ .rl = .none }, params[1]);
const result = try gz.addExtendedPayload(tag, Zir.Inst.BinNode{
.node = gz.nodeIndexToRelative(node),
.lhs = lhs,
.rhs = rhs,
.ptr = ptr,
});
return rvalue(gz, ri, result, node);
}
Expand Down
20 changes: 0 additions & 20 deletions src/Autodoc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1510,26 +1510,6 @@ fn walkInstruction(

// return operand;
// },
.overflow_arithmetic_ptr => {
const un_node = data[inst_index].un_node;

const elem_type_ref = try self.walkRef(file, parent_scope, parent_src, un_node.operand, false);
const type_slot_index = self.types.items.len;
try self.types.append(self.arena, .{
.Pointer = .{
.size = .One,
.child = elem_type_ref.expr,
.is_mutable = true,
.is_volatile = false,
.is_allowzero = false,
},
});

return DocData.WalkResult{
.typeRef = .{ .type = @enumToInt(Ref.type_type) },
.expr = .{ .type = type_slot_index },
};
},
.ptr_type => {
const ptr = data[inst_index].ptr_type;
const extra = file.zir.extraData(Zir.Inst.PtrType, ptr.payload_index);
Expand Down
8 changes: 4 additions & 4 deletions src/BuiltinFn.zig
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub const list = list: {
"@addWithOverflow",
.{
.tag = .add_with_overflow,
.param_count = 4,
.param_count = 2,
},
},
.{
Expand Down Expand Up @@ -636,7 +636,7 @@ pub const list = list: {
"@mulWithOverflow",
.{
.tag = .mul_with_overflow,
.param_count = 4,
.param_count = 2,
},
},
.{
Expand Down Expand Up @@ -741,7 +741,7 @@ pub const list = list: {
"@shlWithOverflow",
.{
.tag = .shl_with_overflow,
.param_count = 4,
.param_count = 2,
},
},
.{
Expand Down Expand Up @@ -889,7 +889,7 @@ pub const list = list: {
"@subWithOverflow",
.{
.tag = .sub_with_overflow,
.param_count = 4,
.param_count = 2,
},
},
.{
Expand Down
Loading

0 comments on commit 3e39cd5

Please sign in to comment.