From e82a7e7918669daa1585117c59c5058bdcfbb093 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 28 Aug 2022 19:14:26 +0200 Subject: [PATCH] Allow $$max and $$min to also work on ints. --- src/compiler/llvm_codegen_expr.c | 45 +++++++++++++++++++++++++++++++- src/compiler/sema_expr.c | 15 +++++++++-- src/version.h | 2 +- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 22e0a8103..925ed120f 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -4540,7 +4540,50 @@ void llvm_emit_builtin_call(GenContext *c, BEValue *result_value, Expr *expr) llvm_emit_syscall(c, result_value, expr); return; } - llvm_emit_intrinsic_expr(c, llvm_get_intrinsic(func), result_value, expr); + unsigned intrinsic; + if (func == BUILTIN_MAX) + { + Type *type = type_flatten(expr->call_expr.arguments[0]->type); + switch (type->type_kind) + { + case ALL_SIGNED_INTS: + intrinsic = intrinsic_id.smax; + break; + case TYPE_BOOL: + case ALL_UNSIGNED_INTS: + intrinsic = intrinsic_id.umax; + break; + case ALL_FLOATS: + intrinsic = intrinsic_id.maxnum; + break; + default: + UNREACHABLE + } + } + else if (func == BUILTIN_MIN) + { + Type *type = type_flatten(expr->call_expr.arguments[0]->type); + switch (type->type_kind) + { + case ALL_SIGNED_INTS: + intrinsic = intrinsic_id.smin; + break; + case TYPE_BOOL: + case ALL_UNSIGNED_INTS: + intrinsic = intrinsic_id.umin; + break; + case ALL_FLOATS: + intrinsic = intrinsic_id.minnum; + break; + default: + UNREACHABLE + } + } + else + { + intrinsic = llvm_get_intrinsic(func); + } + llvm_emit_intrinsic_expr(c, intrinsic, result_value, expr); } void llvm_add_abi_call_attributes(GenContext *c, LLVMValueRef call_value, int count, ABIArgInfo **infos) diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index bb527bc09..f5e063dba 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -2703,14 +2703,25 @@ static inline bool sema_expr_analyse_builtin_call(SemaContext *context, Expr *ex break; case BUILTIN_POW: - case BUILTIN_MAX: - case BUILTIN_MIN: if (!sema_check_builtin_args(args, (BuiltinArg[]) { BA_FLOATLIKE, BA_FLOATLIKE }, arg_count)) return false; if (!sema_check_builtin_args_match(args, arg_count)) return false; rtype = args[0]->type; break; + case BUILTIN_MAX: + case BUILTIN_MIN: + if (type_is_integer(args[0]->type)) + { + if (!sema_check_builtin_args(args, (BuiltinArg[]) { BA_INTLIKE, BA_INTLIKE }, arg_count)) return false; + } + else + { + if (!sema_check_builtin_args(args, (BuiltinArg[]) { BA_FLOATLIKE, BA_FLOATLIKE }, arg_count)) return false; + } + if (!sema_check_builtin_args_match(args, arg_count)) return false; + rtype = args[0]->type; + break; case BUILTIN_FMA: if (!sema_check_builtin_args(args, (BuiltinArg[]) { BA_FLOATLIKE, BA_FLOATLIKE, BA_FLOATLIKE }, diff --git a/src/version.h b/src/version.h index a11b521f2..6ff5e9b29 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.3.30" \ No newline at end of file +#define COMPILER_VERSION "0.3.31" \ No newline at end of file