Skip to content

remove builtin @minValue and @maxValue and replace with stdlib implementation; #1476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ebd5b28
std/math/index.zig: @minValue implementation in stdlib;
kristate Sep 5, 2018
3252bcb
std/math/index.zig: @maxValue implementation in stdlib;
kristate Sep 5, 2018
83c8775
src/all_types.hpp: remove @minValue and @maxValue; ref #1466;
kristate Sep 5, 2018
45f8fc4
src/codegen.cpp: remove @minValue and @maxValue; ref #1466;
kristate Sep 5, 2018
dc9fc9a
src/ir.cpp: remove @minValue and @maxValue; ref #1466;
kristate Sep 5, 2018
7ea6f93
src/ir_print.cpp: remove @minValue and @maxValue; ref #1466;
kristate Sep 5, 2018
a935d81
src/ir.cpp: remove `ir_analyze_min_max`;
kristate Sep 5, 2018
463f637
std/math/index.zig: minValue and maxValue for Integers that we do not…
kristate Sep 5, 2018
e8e1fff
std: builtin @{min,max}Value to std.math.{min,max}Value;
kristate Sep 5, 2018
7bc9f82
std/math/index.zig: fix typo in `minValue`;
kristate Sep 5, 2018
eea90e6
test/compile_errors.zig: add test cases for attempting to get the min…
kristate Sep 5, 2018
95191a7
std/math/index.zig: change definition of {min,max}Value to {min,max}Int;
kristate Sep 5, 2018
c33f7b4
std/math/index.zig: already inside of std.math namespace;
kristate Sep 5, 2018
20fa3c5
{min,max}Value to {min,max}Int;
kristate Sep 5, 2018
6086ddf
tests: {min,max}Int tests from test/cases/misc.zig to std.math;
kristate Sep 5, 2018
caf7fc1
std/math/index.zig: remove cached values because comptime caches for us;
kristate Sep 6, 2018
f452652
std/math/index.zig: @typeOf(42) to `comptime_int`;
kristate Sep 7, 2018
e894685
doc/langref.html.in: remove @minValue and @maxValue from docs;
kristate Sep 13, 2018
50e7de8
doc/langref.html.in: builtin @{min,max}Value to std.math.{min,max}Int;
kristate Sep 13, 2018
5302839
std.rand: builtin @{min,max}Value to std.math.{min,max}Value;
kristate Oct 10, 2018
ef1ed41
test/cases/cast.zig: builtin @{min,max}Value to std.math.{min,max}Value;
kristate Oct 10, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 12 additions & 29 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ a +%= b{#endsyntax#}</pre></td>
</ul>
</td>
<td>
<pre>{#syntax#}u32(@maxValue(u32)) +% 1 == 0{#endsyntax#}</pre>
<pre>{#syntax#}u32(std.math.maxInt(u32)) +% 1 == 0{#endsyntax#}</pre>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -850,7 +850,7 @@ a -%= b{#endsyntax#}</pre></td>
</ul>
</td>
<td>
<pre>{#syntax#}u32(0) -% 1 == @maxValue(u32){#endsyntax#}</pre>
<pre>{#syntax#}u32(0) -% 1 == std.math.maxInt(u32){#endsyntax#}</pre>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -885,7 +885,7 @@ a -%= b{#endsyntax#}</pre></td>
</ul>
</td>
<td>
<pre>{#syntax#}-%i32(@minValue(i32)) == @minValue(i32){#endsyntax#}</pre>
<pre>{#syntax#}-%i32(std.math.minInt(i32)) == std.math.minInt(i32){#endsyntax#}</pre>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -3311,7 +3311,7 @@ fn charToDigit(c: u8) u8 {
'0' ... '9' => c - '0',
'A' ... 'Z' => c - 'A' + 10,
'a' ... 'z' => c - 'a' + 10,
else => @maxValue(u8),
else => @import("std").math.maxInt(u8),
};
}

Expand Down Expand Up @@ -5532,7 +5532,7 @@ test "main" {
<p>
Floored division. Rounds toward negative infinity. For unsigned integers it is
the same as {#syntax#}numerator / denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator != 0{#endsyntax#} and
{#syntax#}!(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1){#endsyntax#}.
{#syntax#}!(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == std.math.minInt(T) and denominator == -1){#endsyntax#}.
</p>
<ul>
<li>{#syntax#}@divFloor(-5, 3) == -2{#endsyntax#}</li>
Expand All @@ -5546,7 +5546,7 @@ test "main" {
<p>
Truncated division. Rounds toward zero. For unsigned integers it is
the same as {#syntax#}numerator / denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator != 0{#endsyntax#} and
{#syntax#}!(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1){#endsyntax#}.
{#syntax#}!(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == std.math.minInt(T) and denominator == -1){#endsyntax#}.
</p>
<ul>
<li>{#syntax#}@divTrunc(-5, 3) == -1{#endsyntax#}</li>
Expand Down Expand Up @@ -5809,15 +5809,6 @@ fn add(a: i32, b: i32) i32 { return a + b; }
This function returns an integer type with the given signness and bit count.
</p>
{#header_close#}
{#header_open|@maxValue#}
<pre>{#syntax#}@maxValue(comptime T: type) comptime_int{#endsyntax#}</pre>
<p>
This function returns the maximum value of the integer type {#syntax#}T{#endsyntax#}.
</p>
<p>
The result is a compile time constant.
</p>
{#header_close#}
{#header_open|@memberCount#}
<pre>{#syntax#}@memberCount(comptime T: type) comptime_int{#endsyntax#}</pre>
<p>
Expand Down Expand Up @@ -5878,15 +5869,6 @@ mem.copy(u8, dest[0...byte_count], source[0...byte_count]);{#endsyntax#}</pre>
<p>There is also a standard library function for this:</p>
<pre>{#syntax#}const mem = @import("std").mem;
mem.set(u8, dest, c);{#endsyntax#}</pre>
{#header_close#}
{#header_open|@minValue#}
<pre>{#syntax#}@minValue(comptime T: type) comptime_int{#endsyntax#}</pre>
<p>
This function returns the minimum value of the integer type T.
</p>
<p>
The result is a compile time constant.
</p>
{#header_close#}
{#header_open|@mod#}
<pre>{#syntax#}@mod(numerator: T, denominator: T) T{#endsyntax#}</pre>
Expand Down Expand Up @@ -6681,7 +6663,7 @@ pub fn main() void {
}
{#code_end#}
<p>
To obtain the maximum value of an unsigned integer, use {#link|@maxValue#}.
To obtain the maximum value of an unsigned integer, use <code>std.math.maxInt(type)</code>.
</p>
{#header_close#}
{#header_open|Cast Truncates Data#}
Expand Down Expand Up @@ -6803,14 +6785,15 @@ pub fn main() void {
<li>{#syntax#}*%{#endsyntax#} (wraparound multiplication)</li>
</ul>
{#code_begin|test#}
const assert = @import("std").debug.assert;
const std = @import("std");
const assert = std.debug.assert;

test "wraparound addition and subtraction" {
const x: i32 = @maxValue(i32);
const x: i32 = std.math.maxInt(i32);
const min_val = x +% 1;
assert(min_val == @minValue(i32));
assert(min_val == std.math.minInt(i32));
const max_val = min_val -% 1;
assert(max_val == @maxValue(i32));
assert(max_val == std.math.maxInt(i32));
}
{#code_end#}
{#header_close#}
Expand Down
6 changes: 3 additions & 3 deletions src-self-hosted/codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,15 @@ fn addLLVMAttrInt(
}

fn addLLVMFnAttr(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8) !void {
return addLLVMAttr(ofile, fn_val, @maxValue(llvm.AttributeIndex), attr_name);
return addLLVMAttr(ofile, fn_val, std.math.maxInt(llvm.AttributeIndex), attr_name);
}

fn addLLVMFnAttrStr(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8, attr_val: []const u8) !void {
return addLLVMAttrStr(ofile, fn_val, @maxValue(llvm.AttributeIndex), attr_name, attr_val);
return addLLVMAttrStr(ofile, fn_val, std.math.maxInt(llvm.AttributeIndex), attr_name, attr_val);
}

fn addLLVMFnAttrInt(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8, attr_val: u64) !void {
return addLLVMAttrInt(ofile, fn_val, @maxValue(llvm.AttributeIndex), attr_name, attr_val);
return addLLVMAttrInt(ofile, fn_val, std.math.maxInt(llvm.AttributeIndex), attr_name, attr_val);
}

fn renderLoadUntyped(
Expand Down
16 changes: 0 additions & 16 deletions src/all_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1343,8 +1343,6 @@ enum BuiltinFnId {
BuiltinFnIdMemset,
BuiltinFnIdSizeof,
BuiltinFnIdAlignOf,
BuiltinFnIdMaxValue,
BuiltinFnIdMinValue,
BuiltinFnIdMemberCount,
BuiltinFnIdMemberType,
BuiltinFnIdMemberName,
Expand Down Expand Up @@ -2081,8 +2079,6 @@ enum IrInstructionId {
IrInstructionIdCUndef,
IrInstructionIdArrayLen,
IrInstructionIdRef,
IrInstructionIdMinValue,
IrInstructionIdMaxValue,
IrInstructionIdCompileErr,
IrInstructionIdCompileLog,
IrInstructionIdErrName,
Expand Down Expand Up @@ -2609,18 +2605,6 @@ struct IrInstructionRef {
bool is_volatile;
};

struct IrInstructionMinValue {
IrInstruction base;

IrInstruction *value;
};

struct IrInstructionMaxValue {
IrInstruction base;

IrInstruction *value;
};

struct IrInstructionCompileErr {
IrInstruction base;

Expand Down
4 changes: 0 additions & 4 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5100,8 +5100,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
case IrInstructionIdSizeOf:
case IrInstructionIdSwitchTarget:
case IrInstructionIdContainerInitFields:
case IrInstructionIdMinValue:
case IrInstructionIdMaxValue:
case IrInstructionIdCompileErr:
case IrInstructionIdCompileLog:
case IrInstructionIdArrayLen:
Expand Down Expand Up @@ -6651,8 +6649,6 @@ static void define_builtin_fns(CodeGen *g) {
create_builtin_fn(g, BuiltinFnIdMemset, "memset", 3);
create_builtin_fn(g, BuiltinFnIdSizeof, "sizeOf", 1);
create_builtin_fn(g, BuiltinFnIdAlignOf, "alignOf", 1);
create_builtin_fn(g, BuiltinFnIdMaxValue, "maxValue", 1);
create_builtin_fn(g, BuiltinFnIdMinValue, "minValue", 1);
create_builtin_fn(g, BuiltinFnIdMemberCount, "memberCount", 1);
create_builtin_fn(g, BuiltinFnIdMemberType, "memberType", 2);
create_builtin_fn(g, BuiltinFnIdMemberName, "memberName", 2);
Expand Down
120 changes: 0 additions & 120 deletions src/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,14 +495,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionUnionInit *) {
return IrInstructionIdUnionInit;
}

static constexpr IrInstructionId ir_instruction_id(IrInstructionMinValue *) {
return IrInstructionIdMinValue;
}

static constexpr IrInstructionId ir_instruction_id(IrInstructionMaxValue *) {
return IrInstructionIdMaxValue;
}

static constexpr IrInstructionId ir_instruction_id(IrInstructionCompileErr *) {
return IrInstructionIdCompileErr;
}
Expand Down Expand Up @@ -1693,24 +1685,6 @@ static IrInstruction *ir_build_ref(IrBuilder *irb, Scope *scope, AstNode *source
return &instruction->base;
}

static IrInstruction *ir_build_min_value(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
IrInstructionMinValue *instruction = ir_build_instruction<IrInstructionMinValue>(irb, scope, source_node);
instruction->value = value;

ir_ref_instruction(value, irb->current_basic_block);

return &instruction->base;
}

static IrInstruction *ir_build_max_value(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
IrInstructionMaxValue *instruction = ir_build_instruction<IrInstructionMaxValue>(irb, scope, source_node);
instruction->value = value;

ir_ref_instruction(value, irb->current_basic_block);

return &instruction->base;
}

static IrInstruction *ir_build_compile_err(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *msg) {
IrInstructionCompileErr *instruction = ir_build_instruction<IrInstructionCompileErr>(irb, scope, source_node);
instruction->msg = msg;
Expand Down Expand Up @@ -3813,26 +3787,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
IrInstruction *c_undef = ir_build_c_undef(irb, scope, node, arg0_value);
return ir_lval_wrap(irb, scope, c_undef, lval);
}
case BuiltinFnIdMaxValue:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
if (arg0_value == irb->codegen->invalid_instruction)
return arg0_value;

IrInstruction *max_value = ir_build_max_value(irb, scope, node, arg0_value);
return ir_lval_wrap(irb, scope, max_value, lval);
}
case BuiltinFnIdMinValue:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
if (arg0_value == irb->codegen->invalid_instruction)
return arg0_value;

IrInstruction *min_value = ir_build_min_value(irb, scope, node, arg0_value);
return ir_lval_wrap(irb, scope, min_value, lval);
}
case BuiltinFnIdCompileErr:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
Expand Down Expand Up @@ -16516,74 +16470,6 @@ static IrInstruction *ir_analyze_instruction_container_init_fields(IrAnalyze *ir
instruction->field_count, instruction->fields);
}

static IrInstruction *ir_analyze_min_max(IrAnalyze *ira, IrInstruction *source_instruction,
IrInstruction *target_type_value, bool is_max)
{
ZigType *target_type = ir_resolve_type(ira, target_type_value);
if (type_is_invalid(target_type))
return ira->codegen->invalid_instruction;
switch (target_type->id) {
case ZigTypeIdInvalid:
zig_unreachable();
case ZigTypeIdInt:
{
IrInstruction *result = ir_const(ira, source_instruction,
ira->codegen->builtin_types.entry_num_lit_int);
eval_min_max_value(ira->codegen, target_type, &result->value, is_max);
return result;
}
case ZigTypeIdBool:
case ZigTypeIdVoid:
{
IrInstruction *result = ir_const(ira, source_instruction, target_type);
eval_min_max_value(ira->codegen, target_type, &result->value, is_max);
return result;
}
case ZigTypeIdEnum:
case ZigTypeIdFloat:
case ZigTypeIdMetaType:
case ZigTypeIdUnreachable:
case ZigTypeIdPointer:
case ZigTypeIdPromise:
case ZigTypeIdArray:
case ZigTypeIdStruct:
case ZigTypeIdComptimeFloat:
case ZigTypeIdComptimeInt:
case ZigTypeIdUndefined:
case ZigTypeIdNull:
case ZigTypeIdOptional:
case ZigTypeIdErrorUnion:
case ZigTypeIdErrorSet:
case ZigTypeIdUnion:
case ZigTypeIdFn:
case ZigTypeIdNamespace:
case ZigTypeIdBoundFn:
case ZigTypeIdArgTuple:
case ZigTypeIdOpaque:
{
const char *err_format = is_max ?
"no max value available for type '%s'" :
"no min value available for type '%s'";
ir_add_error(ira, source_instruction,
buf_sprintf(err_format, buf_ptr(&target_type->name)));
return ira->codegen->invalid_instruction;
}
}
zig_unreachable();
}

static IrInstruction *ir_analyze_instruction_min_value(IrAnalyze *ira,
IrInstructionMinValue *instruction)
{
return ir_analyze_min_max(ira, &instruction->base, instruction->value->child, false);
}

static IrInstruction *ir_analyze_instruction_max_value(IrAnalyze *ira,
IrInstructionMaxValue *instruction)
{
return ir_analyze_min_max(ira, &instruction->base, instruction->value->child, true);
}

static IrInstruction *ir_analyze_instruction_compile_err(IrAnalyze *ira,
IrInstructionCompileErr *instruction)
{
Expand Down Expand Up @@ -21161,10 +21047,6 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio
return ir_analyze_instruction_container_init_list(ira, (IrInstructionContainerInitList *)instruction);
case IrInstructionIdContainerInitFields:
return ir_analyze_instruction_container_init_fields(ira, (IrInstructionContainerInitFields *)instruction);
case IrInstructionIdMinValue:
return ir_analyze_instruction_min_value(ira, (IrInstructionMinValue *)instruction);
case IrInstructionIdMaxValue:
return ir_analyze_instruction_max_value(ira, (IrInstructionMaxValue *)instruction);
case IrInstructionIdCompileErr:
return ir_analyze_instruction_compile_err(ira, (IrInstructionCompileErr *)instruction);
case IrInstructionIdCompileLog:
Expand Down Expand Up @@ -21508,8 +21390,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
case IrInstructionIdSwitchTarget:
case IrInstructionIdUnionTag:
case IrInstructionIdRef:
case IrInstructionIdMinValue:
case IrInstructionIdMaxValue:
case IrInstructionIdEmbedFile:
case IrInstructionIdTruncate:
case IrInstructionIdIntType:
Expand Down
18 changes: 0 additions & 18 deletions src/ir_print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,18 +565,6 @@ static void ir_print_ref(IrPrint *irp, IrInstructionRef *instruction) {
ir_print_other_instruction(irp, instruction->value);
}

static void ir_print_min_value(IrPrint *irp, IrInstructionMinValue *instruction) {
fprintf(irp->f, "@minValue(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}

static void ir_print_max_value(IrPrint *irp, IrInstructionMaxValue *instruction) {
fprintf(irp->f, "@maxValue(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}

static void ir_print_compile_err(IrPrint *irp, IrInstructionCompileErr *instruction) {
fprintf(irp->f, "@compileError(");
ir_print_other_instruction(irp, instruction->msg);
Expand Down Expand Up @@ -1480,12 +1468,6 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
case IrInstructionIdRef:
ir_print_ref(irp, (IrInstructionRef *)instruction);
break;
case IrInstructionIdMinValue:
ir_print_min_value(irp, (IrInstructionMinValue *)instruction);
break;
case IrInstructionIdMaxValue:
ir_print_max_value(irp, (IrInstructionMaxValue *)instruction);
break;
case IrInstructionIdCompileErr:
ir_print_compile_err(irp, (IrInstructionCompileErr *)instruction);
break;
Expand Down
Loading