From cc81618ead515a83254e362308d783a7b524bc3d Mon Sep 17 00:00:00 2001 From: FeepingCreature Date: Wed, 3 Jan 2024 08:37:59 +0100 Subject: [PATCH] Communicate to llvm/gcc that refcount and index errors are very unlikely. Make refcount errors fatal. --- src/neat/runtime/array.nt | 7 +++---- src/runtime.c | 11 ++++++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/neat/runtime/array.nt b/src/neat/runtime/array.nt index d880909b..9b395fe7 100644 --- a/src/neat/runtime/array.nt +++ b/src/neat/runtime/array.nt @@ -1,12 +1,11 @@ module neat.runtime.array; +extern(C) void neat_runtime_index_oob(size_t index); + size_t checkIndex(size_t index, size_t length) { // < 0 overflows to >= length. if (index >= length) { - import neat.runtime.stdlib : exit, fprintf, stderr; - - fprintf(stderr, "Array index out of bounds: %zd\n".ptr, index); - exit(1); + neat_runtime_index_oob(index); } return index; } diff --git a/src/runtime.c b/src/runtime.c index f63db61d..82c2c823 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -172,10 +172,19 @@ void print_backtrace() #endif } -void neat_runtime_refcount_violation(struct String s, ptrdiff_t *ptr) +#define FATALERROR __attribute__((cold)) __attribute__((noinline)) __attribute__((noreturn)) + +void FATALERROR neat_runtime_refcount_violation(struct String s, ptrdiff_t *ptr) { printf("<%.*s: refcount logic violated: %zd at %p\n", (int) s.length, s.ptr, *ptr, ptr); print_backtrace(); + exit(1); +} + +void FATALERROR neat_runtime_index_oob(size_t index) +{ + fprintf(stderr, "Array index out of bounds: %zd\n", index); + exit(1); } void neat_runtime_refcount_inc(struct String s, ptrdiff_t *ptr)