mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 23:58:50 +00:00
Add -integer-division-by-zero:all-bits
This commit is contained in:
+14
-5
@@ -353,14 +353,18 @@ enum OptInFeatureFlags : u64 {
|
|||||||
OptInFeatureFlag_NONE = 0,
|
OptInFeatureFlag_NONE = 0,
|
||||||
OptInFeatureFlag_DynamicLiterals = 1u<<0,
|
OptInFeatureFlag_DynamicLiterals = 1u<<0,
|
||||||
|
|
||||||
OptInFeatureFlag_IntegerDivisionByZero_Trap = 1u<<1,
|
OptInFeatureFlag_GlobalContext = 1u<<1,
|
||||||
OptInFeatureFlag_IntegerDivisionByZero_Zero = 1u<<2,
|
|
||||||
OptInFeatureFlag_IntegerDivisionByZero_Self = 1u<<3,
|
|
||||||
|
|
||||||
OptInFeatureFlag_GlobalContext = 1u<<4,
|
OptInFeatureFlag_IntegerDivisionByZero_Trap = 1u<<2,
|
||||||
|
OptInFeatureFlag_IntegerDivisionByZero_Zero = 1u<<3,
|
||||||
|
OptInFeatureFlag_IntegerDivisionByZero_Self = 1u<<4,
|
||||||
|
OptInFeatureFlag_IntegerDivisionByZero_AllBits = 1u<<5,
|
||||||
|
|
||||||
|
|
||||||
OptInFeatureFlag_IntegerDivisionByZero_ALL = OptInFeatureFlag_IntegerDivisionByZero_Trap|OptInFeatureFlag_IntegerDivisionByZero_Zero|OptInFeatureFlag_IntegerDivisionByZero_Self,
|
OptInFeatureFlag_IntegerDivisionByZero_ALL = OptInFeatureFlag_IntegerDivisionByZero_Trap|
|
||||||
|
OptInFeatureFlag_IntegerDivisionByZero_Zero|
|
||||||
|
OptInFeatureFlag_IntegerDivisionByZero_Self|
|
||||||
|
OptInFeatureFlag_IntegerDivisionByZero_AllBits,
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -377,6 +381,10 @@ u64 get_feature_flag_from_name(String const &name) {
|
|||||||
if (name == "integer-division-by-zero:self") {
|
if (name == "integer-division-by-zero:self") {
|
||||||
return OptInFeatureFlag_IntegerDivisionByZero_Self;
|
return OptInFeatureFlag_IntegerDivisionByZero_Self;
|
||||||
}
|
}
|
||||||
|
if (name == "integer-division-by-zero:all-bits") {
|
||||||
|
return OptInFeatureFlag_IntegerDivisionByZero_AllBits;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (name == "global-context") {
|
if (name == "global-context") {
|
||||||
return OptInFeatureFlag_GlobalContext;
|
return OptInFeatureFlag_GlobalContext;
|
||||||
@@ -431,6 +439,7 @@ enum IntegerDivisionByZeroKind : u8 {
|
|||||||
IntegerDivisionByZero_Trap,
|
IntegerDivisionByZero_Trap,
|
||||||
IntegerDivisionByZero_Zero,
|
IntegerDivisionByZero_Zero,
|
||||||
IntegerDivisionByZero_Self,
|
IntegerDivisionByZero_Self,
|
||||||
|
IntegerDivisionByZero_AllBits,
|
||||||
};
|
};
|
||||||
|
|
||||||
// This stores the information for the specify architecture of this build
|
// This stores the information for the specify architecture of this build
|
||||||
|
|||||||
+25
-6
@@ -4377,12 +4377,23 @@ gb_internal void check_binary_expr(CheckerContext *c, Operand *x, Ast *node, Typ
|
|||||||
b.kind == ExactValue_Integer && big_int_is_zero(&b.value_integer) &&
|
b.kind == ExactValue_Integer && big_int_is_zero(&b.value_integer) &&
|
||||||
(op.kind == Token_QuoEq || op.kind == Token_Mod || op.kind == Token_ModMod)) {
|
(op.kind == Token_QuoEq || op.kind == Token_Mod || op.kind == Token_ModMod)) {
|
||||||
if (op.kind == Token_QuoEq) {
|
if (op.kind == Token_QuoEq) {
|
||||||
if (zero_behaviour == IntegerDivisionByZero_Zero) {
|
switch (zero_behaviour) {
|
||||||
|
case IntegerDivisionByZero_Zero:
|
||||||
// x/0 == 0
|
// x/0 == 0
|
||||||
x->value = b;
|
x->value = b;
|
||||||
} else {
|
break;
|
||||||
|
case IntegerDivisionByZero_Self:
|
||||||
// x/0 == x
|
// x/0 == x
|
||||||
x->value = a;
|
x->value = a;
|
||||||
|
break;
|
||||||
|
case IntegerDivisionByZero_AllBits:
|
||||||
|
// x/0 == 0b111...111
|
||||||
|
if (is_type_untyped(x->type)) {
|
||||||
|
x->value = exact_value_i64(-1);
|
||||||
|
} else {
|
||||||
|
x->value = exact_unary_operator_value(Token_Xor, b, cast(i32)(8*type_size_of(x->type)), is_type_unsigned(x->type));
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/*
|
/*
|
||||||
@@ -4391,16 +4402,21 @@ gb_internal void check_binary_expr(CheckerContext *c, Operand *x, Ast *node, Typ
|
|||||||
truncated: r = a - b*trunc(a/b)
|
truncated: r = a - b*trunc(a/b)
|
||||||
floored: r = a - b*floor(a/b)
|
floored: r = a - b*floor(a/b)
|
||||||
|
|
||||||
IFF a/0 == 0, then (a%0 == a) or (a%%0 == a)
|
IFF a/0 == 0, then (a%0 == a) or (a%%0 == a)
|
||||||
IFF a/0 == a, then (a%0 == 0) or (a%%0 == 0)
|
IFF a/0 == a, then (a%0 == 0) or (a%%0 == 0)
|
||||||
|
IFF a/0 == 0b111..., then (a%0 == a) or (a%%0 == a)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (zero_behaviour == IntegerDivisionByZero_Zero) {
|
switch (zero_behaviour) {
|
||||||
|
case IntegerDivisionByZero_Zero:
|
||||||
|
case IntegerDivisionByZero_AllBits:
|
||||||
// x%0 == x
|
// x%0 == x
|
||||||
x->value = a;
|
x->value = a;
|
||||||
} else {
|
break;
|
||||||
|
case IntegerDivisionByZero_Self:
|
||||||
// x%0 == 0
|
// x%0 == 0
|
||||||
x->value = b;
|
x->value = b;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -9670,6 +9686,9 @@ gb_internal IntegerDivisionByZeroKind check_for_integer_division_by_zero(Checker
|
|||||||
if ((flags & OptInFeatureFlag_IntegerDivisionByZero_Self) != 0) {
|
if ((flags & OptInFeatureFlag_IntegerDivisionByZero_Self) != 0) {
|
||||||
return IntegerDivisionByZero_Self;
|
return IntegerDivisionByZero_Self;
|
||||||
}
|
}
|
||||||
|
if ((flags & OptInFeatureFlag_IntegerDivisionByZero_AllBits) != 0) {
|
||||||
|
return IntegerDivisionByZero_AllBits;
|
||||||
|
}
|
||||||
return build_context.integer_division_by_zero_behaviour;
|
return build_context.integer_division_by_zero_behaviour;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -305,6 +305,9 @@ gb_internal IntegerDivisionByZeroKind lb_check_for_integer_division_by_zero_beha
|
|||||||
if (flags & OptInFeatureFlag_IntegerDivisionByZero_Self) {
|
if (flags & OptInFeatureFlag_IntegerDivisionByZero_Self) {
|
||||||
return IntegerDivisionByZero_Self;
|
return IntegerDivisionByZero_Self;
|
||||||
}
|
}
|
||||||
|
if (flags & OptInFeatureFlag_IntegerDivisionByZero_AllBits) {
|
||||||
|
return IntegerDivisionByZero_AllBits;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return build_context.integer_division_by_zero_behaviour;
|
return build_context.integer_division_by_zero_behaviour;
|
||||||
}
|
}
|
||||||
@@ -1140,6 +1143,7 @@ gb_internal LLVMValueRef lb_integer_division(lbProcedure *p, LLVMValueRef lhs, L
|
|||||||
GB_ASSERT(LLVMTypeOf(lhs) == type);
|
GB_ASSERT(LLVMTypeOf(lhs) == type);
|
||||||
|
|
||||||
LLVMValueRef zero = LLVMConstNull(type);
|
LLVMValueRef zero = LLVMConstNull(type);
|
||||||
|
LLVMValueRef all_bits = LLVMConstNot(zero);
|
||||||
auto behaviour = lb_check_for_integer_division_by_zero_behaviour(p);
|
auto behaviour = lb_check_for_integer_division_by_zero_behaviour(p);
|
||||||
|
|
||||||
auto *call = is_signed ? LLVMBuildSDiv : LLVMBuildUDiv;
|
auto *call = is_signed ? LLVMBuildSDiv : LLVMBuildUDiv;
|
||||||
@@ -1151,6 +1155,9 @@ gb_internal LLVMValueRef lb_integer_division(lbProcedure *p, LLVMValueRef lhs, L
|
|||||||
return lhs;
|
return lhs;
|
||||||
case IntegerDivisionByZero_Zero:
|
case IntegerDivisionByZero_Zero:
|
||||||
return zero;
|
return zero;
|
||||||
|
case IntegerDivisionByZero_AllBits:
|
||||||
|
// return all_bits;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!is_signed && lb_sizeof(type) <= 8) {
|
if (!is_signed && lb_sizeof(type) <= 8) {
|
||||||
@@ -1198,6 +1205,9 @@ gb_internal LLVMValueRef lb_integer_division(lbProcedure *p, LLVMValueRef lhs, L
|
|||||||
case IntegerDivisionByZero_Self:
|
case IntegerDivisionByZero_Self:
|
||||||
incoming_values[1] = lhs;
|
incoming_values[1] = lhs;
|
||||||
break;
|
break;
|
||||||
|
case IntegerDivisionByZero_AllBits:
|
||||||
|
incoming_values[1] = all_bits;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
lb_emit_jump(p, done_block);
|
lb_emit_jump(p, done_block);
|
||||||
@@ -1211,6 +1221,7 @@ gb_internal LLVMValueRef lb_integer_division(lbProcedure *p, LLVMValueRef lhs, L
|
|||||||
res = incoming_values[0];
|
res = incoming_values[0];
|
||||||
break;
|
break;
|
||||||
case IntegerDivisionByZero_Zero:
|
case IntegerDivisionByZero_Zero:
|
||||||
|
case IntegerDivisionByZero_AllBits:
|
||||||
res = LLVMBuildPhi(p->builder, type, "");
|
res = LLVMBuildPhi(p->builder, type, "");
|
||||||
|
|
||||||
GB_ASSERT(p->curr_block->preds.count >= 2);
|
GB_ASSERT(p->curr_block->preds.count >= 2);
|
||||||
@@ -1229,6 +1240,7 @@ gb_internal LLVMValueRef lb_integer_division_intrinsics(lbProcedure *p, LLVMValu
|
|||||||
GB_ASSERT(LLVMTypeOf(lhs) == type);
|
GB_ASSERT(LLVMTypeOf(lhs) == type);
|
||||||
|
|
||||||
LLVMValueRef zero = LLVMConstNull(type);
|
LLVMValueRef zero = LLVMConstNull(type);
|
||||||
|
LLVMValueRef all_bits = LLVMConstNot(zero);
|
||||||
auto behaviour = lb_check_for_integer_division_by_zero_behaviour(p);
|
auto behaviour = lb_check_for_integer_division_by_zero_behaviour(p);
|
||||||
|
|
||||||
auto const do_op = [&]() -> LLVMValueRef {
|
auto const do_op = [&]() -> LLVMValueRef {
|
||||||
@@ -1285,6 +1297,9 @@ gb_internal LLVMValueRef lb_integer_division_intrinsics(lbProcedure *p, LLVMValu
|
|||||||
case IntegerDivisionByZero_Self:
|
case IntegerDivisionByZero_Self:
|
||||||
incoming_values[1] = lhs;
|
incoming_values[1] = lhs;
|
||||||
break;
|
break;
|
||||||
|
case IntegerDivisionByZero_AllBits:
|
||||||
|
incoming_values[1] = all_bits;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
lb_emit_jump(p, done_block);
|
lb_emit_jump(p, done_block);
|
||||||
@@ -1298,6 +1313,7 @@ gb_internal LLVMValueRef lb_integer_division_intrinsics(lbProcedure *p, LLVMValu
|
|||||||
res = incoming_values[0];
|
res = incoming_values[0];
|
||||||
break;
|
break;
|
||||||
case IntegerDivisionByZero_Zero:
|
case IntegerDivisionByZero_Zero:
|
||||||
|
case IntegerDivisionByZero_AllBits:
|
||||||
res = LLVMBuildPhi(p->builder, type, "");
|
res = LLVMBuildPhi(p->builder, type, "");
|
||||||
|
|
||||||
GB_ASSERT(p->curr_block->preds.count >= 2);
|
GB_ASSERT(p->curr_block->preds.count >= 2);
|
||||||
@@ -1344,6 +1360,7 @@ gb_internal LLVMValueRef lb_integer_modulo(lbProcedure *p, LLVMValueRef lhs, LLV
|
|||||||
case IntegerDivisionByZero_Self:
|
case IntegerDivisionByZero_Self:
|
||||||
return zero;
|
return zero;
|
||||||
case IntegerDivisionByZero_Zero:
|
case IntegerDivisionByZero_Zero:
|
||||||
|
case IntegerDivisionByZero_AllBits:
|
||||||
return lhs;
|
return lhs;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -1386,6 +1403,7 @@ gb_internal LLVMValueRef lb_integer_modulo(lbProcedure *p, LLVMValueRef lhs, LLV
|
|||||||
LLVMBuildUnreachable(p->builder);
|
LLVMBuildUnreachable(p->builder);
|
||||||
break;
|
break;
|
||||||
case IntegerDivisionByZero_Zero:
|
case IntegerDivisionByZero_Zero:
|
||||||
|
case IntegerDivisionByZero_AllBits:
|
||||||
incoming_values[1] = lhs;
|
incoming_values[1] = lhs;
|
||||||
break;
|
break;
|
||||||
case IntegerDivisionByZero_Self:
|
case IntegerDivisionByZero_Self:
|
||||||
@@ -1404,6 +1422,7 @@ gb_internal LLVMValueRef lb_integer_modulo(lbProcedure *p, LLVMValueRef lhs, LLV
|
|||||||
res = incoming_values[0];
|
res = incoming_values[0];
|
||||||
break;
|
break;
|
||||||
case IntegerDivisionByZero_Zero:
|
case IntegerDivisionByZero_Zero:
|
||||||
|
case IntegerDivisionByZero_AllBits:
|
||||||
res = LLVMBuildPhi(p->builder, type, "");
|
res = LLVMBuildPhi(p->builder, type, "");
|
||||||
|
|
||||||
GB_ASSERT(p->curr_block->preds.count >= 2);
|
GB_ASSERT(p->curr_block->preds.count >= 2);
|
||||||
|
|||||||
@@ -2589,6 +2589,7 @@ gb_internal int print_show_help(String const arg0, String command, String option
|
|||||||
print_usage_line(3, "-integer-division-by-zero:trap Trap on division/modulo/remainder by zero");
|
print_usage_line(3, "-integer-division-by-zero:trap Trap on division/modulo/remainder by zero");
|
||||||
print_usage_line(3, "-integer-division-by-zero:zero x/0 == 0 and x%%0 == x and x%%%%0 == x");
|
print_usage_line(3, "-integer-division-by-zero:zero x/0 == 0 and x%%0 == x and x%%%%0 == x");
|
||||||
print_usage_line(3, "-integer-division-by-zero:self x/0 == x and x%%0 == 0 and x%%%%0 == 0");
|
print_usage_line(3, "-integer-division-by-zero:self x/0 == x and x%%0 == 0 and x%%%%0 == 0");
|
||||||
|
print_usage_line(3, "-integer-division-by-zero:all-bits x/0 == ~T(0) and x%%0 == x and x%%%%0 == x");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user