Skip to content

Commit

Permalink
Implement multi-argument @min/@max and notice bounds
Browse files Browse the repository at this point in the history
Resolves: ziglang#14039
  • Loading branch information
mlugg committed May 1, 2023
1 parent 440b3df commit 0151e31
Show file tree
Hide file tree
Showing 8 changed files with 373 additions and 69 deletions.
63 changes: 44 additions & 19 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7907,6 +7907,48 @@ fn typeOf(
return rvalue(gz, ri, typeof_inst, node);
}

fn minMax(
gz: *GenZir,
scope: *Scope,
ri: ResultInfo,
node: Ast.Node.Index,
args: []const Ast.Node.Index,
comptime op: enum { min, max },
) InnerError!Zir.Inst.Ref {
const astgen = gz.astgen;
if (args.len < 1) {
return astgen.failNode(node, "expected at least 1 argument, found 0", .{});
}
if (args.len == 2) {
const tag: Zir.Inst.Tag = switch (op) {
.min => .min,
.max => .max,
};
const a = try expr(gz, scope, .{ .rl = .none }, args[0]);
const b = try expr(gz, scope, .{ .rl = .none }, args[1]);
const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
.lhs = a,
.rhs = b,
});
return rvalue(gz, ri, result, node);
}
const payload_index = try addExtra(astgen, Zir.Inst.NodeMultiOp{
.src_node = gz.nodeIndexToRelative(node),
});
var extra_index = try reserveExtra(gz.astgen, args.len);
for (args) |arg| {
const arg_ref = try expr(gz, scope, .{ .rl = .none }, arg);
astgen.extra.items[extra_index] = @enumToInt(arg_ref);
extra_index += 1;
}
const tag: Zir.Inst.Extended = switch (op) {
.min => .min_multi,
.max => .max_multi,
};
const result = try gz.addExtendedMultiOpPayloadIndex(tag, payload_index, args.len);
return rvalue(gz, ri, result, node);
}

fn builtinCall(
gz: *GenZir,
scope: *Scope,
Expand Down Expand Up @@ -7997,6 +8039,8 @@ fn builtinCall(
.TypeOf => return typeOf( gz, scope, ri, node, params),
.union_init => return unionInit(gz, scope, ri, node, params),
.c_import => return cImport( gz, scope, node, params[0]),
.min => return minMax( gz, scope, ri, node, params, .min),
.max => return minMax( gz, scope, ri, node, params, .max),
// zig fmt: on

.@"export" => {
Expand Down Expand Up @@ -8358,25 +8402,6 @@ fn builtinCall(
return rvalue(gz, ri, result, node);
},

.max => {
const a = try expr(gz, scope, .{ .rl = .none }, params[0]);
const b = try expr(gz, scope, .{ .rl = .none }, params[1]);
const result = try gz.addPlNode(.max, node, Zir.Inst.Bin{
.lhs = a,
.rhs = b,
});
return rvalue(gz, ri, result, node);
},
.min => {
const a = try expr(gz, scope, .{ .rl = .none }, params[0]);
const b = try expr(gz, scope, .{ .rl = .none }, params[1]);
const result = try gz.addPlNode(.min, node, Zir.Inst.Bin{
.lhs = a,
.rhs = b,
});
return rvalue(gz, ri, result, node);
},

.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),
Expand Down
4 changes: 2 additions & 2 deletions src/BuiltinFn.zig
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ pub const list = list: {
"@max",
.{
.tag = .max,
.param_count = 2,
.param_count = null,
},
},
.{
Expand All @@ -629,7 +629,7 @@ pub const list = list: {
"@min",
.{
.tag = .min,
.param_count = 2,
.param_count = null,
},
},
.{
Expand Down
Loading

0 comments on commit 0151e31

Please sign in to comment.