Files
c3c/src/compiler/number.c
Christoffer Lerno e293c435af 0.6.0: init_new/init_temp removed. LinkedList API rewritten. List "pop" and "remove" function now return Optionals. RingBuffer API rewritten. Allocator interface changed. Deprecated Allocator, DString and mem functions removed. "identity" functions are now constants for Matrix and Complex numbers. @default implementations for interfaces removed. any* => any, same for interfaces. Emit local/private globals as "private" in LLVM, following C "static". Updated enum syntax. Add support [rgba] properties in vectors. Improved checks of aliased "void". Subarray -> slice. Fix of llvm codegen enum check. Improved alignment handling. Add --output-dir #1155. Removed List/Object append. GenericList renamed AnyList. Remove unused "unwrap". Fixes to cond. Optimize output in dead branches. Better checking of operator methods. Disallow any from implementing dynamic methods. Check for operator mismatch. Remove unnecessary bitfield. Remove numbering in --list* commands Old style enum declaration for params/type, but now the type is optional. Add note on #1086. Allow making distinct types out of "void", "typeid", "anyfault" and faults. Remove system linker build options. "Try" expressions must be simple expressions. Add optimized build to Mac tests. Register int. assert(false) only allowed in unused branches or in tests. Compile time failed asserts is a compile time error. Remove current_block_is_target. Bug when assigning an optional from an optional. Remove unused emit_zstring. Simplify phi code. Remove unnecessary unreachable blocks and remove unnecessary current_block NULL assignments. Proper handling of '.' and Win32 '//server' paths. Add "no discard" to expression blocks with a return value. Detect "unsigned >= 0" as errors. Fix issue with distinct void as a member #1147. Improve callstack debug information #1184. Fix issue with absolute output-dir paths. Lambdas were not type checked thoroughly #1185. Fix compilation warning #1187. Request jump table using @jump for switches. Path normalization - fix possible null terminator out of bounds. Improved error messages on inlined macros.
Upgrade of mingw in CI. Fix problems using reflection on interface types #1203. Improved debug information on defer. $foreach doesn't create an implicit syntactic scope.
Error if `@if` depends on `@if`. Updated Linux stacktrace. Fix of default argument stacktrace. Allow linking libraries directly by file path. Improve inlining warning messages. Added `index_of_char_from`. Compiler crash using enum nameof from different module #1205. Removed unused fields in find_msvc. Use vswhere to find msvc. Update tests for LLVM 19
2024-06-12 10:14:26 +02:00

248 lines
6.3 KiB
C

// Copyright (c) 2020 Christoffer Lerno. All rights reserved.
// Use of this source code is governed by a LGPLv3.0
// a copy of which can be found in the LICENSE file.
#include "compiler_internal.h"
#define FLOAT32_LIMIT 340282346638528859811704183484516925440.0000000000000000
#define FLOAT64_LIMIT 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0000000000000000
#define FLOAT16_LIMIT 65504
#define BFLOAT_LIMIT 338953138925153547590470800371487866880.0000000000000000
static inline bool compare_bool(bool left, bool right, BinaryOp op)
{
switch (op)
{
case BINARYOP_EQ:
return left == right;
case BINARYOP_NE:
return left != right;
case BINARYOP_GT:
return left > right;
case BINARYOP_LT:
return left < right;
case BINARYOP_GE:
return left >= right;
case BINARYOP_LE:
return left <= right;
default:
UNREACHABLE
}
}
static inline bool compare_fps(Real left, Real right, BinaryOp op)
{
switch (op)
{
case BINARYOP_GE:
return left >= right;
case BINARYOP_LE:
return left <= right;
case BINARYOP_NE:
return left != right;
case BINARYOP_GT:
return left > right;
case BINARYOP_LT:
return left < right;
case BINARYOP_EQ:
return left == right;
default:
UNREACHABLE
}
}
bool expr_const_compare(const ExprConst *left, const ExprConst *right, BinaryOp op)
{
bool is_eq;
assert(left->const_kind == right->const_kind);
switch (left->const_kind)
{
case CONST_BOOL:
return compare_bool(left->b, right->b, op);
case CONST_INTEGER:
assert(right->const_kind != CONST_ENUM);
return int_comp(left->ixx, right->ixx, op);
case CONST_FLOAT:
return compare_fps(left->fxx.f, right->fxx.f, op);
case CONST_POINTER:
{
Int a = { .i.low = left->ptr, .type = TYPE_U64 };
Int b = { .i.low = right->ptr, .type = TYPE_U64 };
return int_comp(a, b, op);
}
case CONST_STRING:
if (left->bytes.len != right->bytes.len)
{
is_eq = false;
goto RETURN;
}
if (right->bytes.ptr == left->bytes.ptr)
{
is_eq = true;
goto RETURN;
}
is_eq = !strncmp(left->bytes.ptr, right->bytes.ptr, left->bytes.len);
goto RETURN;
case CONST_TYPEID:
is_eq = left->typeid == right->typeid;
goto RETURN;
case CONST_ERR:
case CONST_ENUM:
{
Decl *left_decl = left->enum_err_val;
// The error case
assert(right->const_kind == left->const_kind);
Decl *right_decl = right->enum_err_val;
// Non-matching cannot be compared.
if (right_decl->type != left_decl->type) return false;
int64_t right_ordinal = right->enum_err_val->enum_constant.ordinal;
switch (op)
{
case BINARYOP_GT:
return left_decl->enum_constant.ordinal > right_ordinal;
case BINARYOP_GE:
return left_decl->enum_constant.ordinal >= right_ordinal;
case BINARYOP_LT:
return left_decl->enum_constant.ordinal < right_ordinal;
case BINARYOP_LE:
return left_decl->enum_constant.ordinal <= right_ordinal;
case BINARYOP_NE:
return left_decl->enum_constant.ordinal != right_ordinal;
case BINARYOP_EQ:
return left_decl->enum_constant.ordinal == right_ordinal;
default:
return false;
}
}
case CONST_BYTES:
if (left->bytes.len != right->bytes.len)
{
is_eq = false;
goto RETURN;
}
if (right->bytes.ptr == left->bytes.ptr)
{
is_eq = true;
goto RETURN;
}
is_eq = !memcmp(left->bytes.ptr, right->bytes.ptr, left->bytes.len);
goto RETURN;
case CONST_INITIALIZER:
return false;
case CONST_UNTYPED_LIST:
return false;
case CONST_MEMBER:
is_eq = left->member.decl == right->member.decl;
goto RETURN;
}
UNREACHABLE
RETURN:
assert((op == BINARYOP_EQ) || (op == BINARYOP_NE));
return op == BINARYOP_EQ ? is_eq : !is_eq;
}
bool expr_const_in_range(const ExprConst *left, const ExprConst *right, const ExprConst *right_to)
{
if (right == right_to)
{
return expr_const_compare(left, right, BINARYOP_EQ);
}
return expr_const_compare(left, right, BINARYOP_GE) && expr_const_compare(left, right_to, BINARYOP_LE);
}
bool expr_const_float_fits_type(const ExprConst *expr_const, TypeKind kind)
{
Real hi_limit;
Real lo_limit;
switch (kind)
{
case TYPE_BF16:
lo_limit = hi_limit = BFLOAT_LIMIT;
break;
case TYPE_F16:
lo_limit = hi_limit = FLOAT16_LIMIT;
break;
case TYPE_F32:
lo_limit = hi_limit = FLOAT32_LIMIT;
break;
case TYPE_F64:
lo_limit = hi_limit = FLOAT64_LIMIT;
break;
case TYPE_F128:
// Assume this to be true
return true;
case TYPE_BOOL:
return true;
default:
UNREACHABLE
}
assert(expr_const->const_kind == CONST_FLOAT);
return expr_const->fxx.f >= -lo_limit && expr_const->fxx.f <= hi_limit;
}
bool expr_const_will_overflow(const ExprConst *expr, TypeKind kind)
{
switch (expr->const_kind)
{
case CONST_FLOAT:
return !expr_const_float_fits_type(expr, kind);
case CONST_INTEGER:
return !int_fits(expr->ixx, kind);
case CONST_BOOL:
return false;
case CONST_ENUM:
{
Int i = { .i = { .low = expr->enum_err_val->var.index }, .type = type_flatten(expr->enum_err_val->type)->type_kind };
return !int_fits(i, kind);
}
case CONST_ERR:
case CONST_BYTES:
case CONST_STRING:
case CONST_POINTER:
case CONST_TYPEID:
case CONST_INITIALIZER:
case CONST_UNTYPED_LIST:
case CONST_MEMBER:
UNREACHABLE;
}
UNREACHABLE;
}
const char *expr_const_to_error_string(const ExprConst *expr)
{
switch (expr->const_kind)
{
case CONST_POINTER:
if (!expr->ptr) return "null";
return str_printf("%p", (void*)expr->ptr);
case CONST_BOOL:
return expr->b ? "true" : "false";
case CONST_INTEGER:
return int_to_str(expr->ixx, 10);
case CONST_FLOAT:
return str_printf("%g", expr->fxx.f);
case CONST_STRING:
return str_printf("\"%*.s\"", expr->bytes.len, expr->bytes.ptr);
case CONST_BYTES:
return "<binary data>";
case CONST_ENUM:
case CONST_ERR:
return expr->enum_err_val->name;
case CONST_TYPEID:
return type_to_error_string(expr->typeid);
case CONST_MEMBER:
return "member";
case CONST_INITIALIZER:
return "constant list";
case CONST_UNTYPED_LIST:
return "untyped list";
}
UNREACHABLE
}