From 15e93b55266186c159688e3b54bead26da206ee2 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Tue, 13 Feb 2024 17:21:56 +0100 Subject: Make unreachable() useful when debugging, and move common macros --- make.c | 4 ++-- src/c8asm/assembler.c | 6 +----- src/c8asm/common.h | 8 -------- src/c8asm/lexer.c | 1 + src/c8asm/main.c | 1 + src/c8asm/parser.c | 6 +----- src/common/macros.h | 27 +++++++++++++++++++++++++++ 7 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 src/common/macros.h diff --git a/make.c b/make.c index ed8765b..d9161fb 100644 --- a/make.c +++ b/make.c @@ -19,7 +19,7 @@ #define CC "cc" #define CFLAGS WARN, "-std=c2x" -#define CFLAGS_DBG CFLAGS, "-Og", "-ggdb3" +#define CFLAGS_DBG CFLAGS, "-Og", "-ggdb3", "-DDEBUG=1" #ifdef __APPLE__ # define CFLAGS_RLS CFLAGS, "-O3" #else @@ -209,7 +209,7 @@ build_c8asm(void) cmdaddv(&c, sv.buf, sv.len); if (FLAGSET('l')) cmdadd(&c, "-flto"); - cmdadd(&c, "-o", "c8asm"); + cmdadd(&c, "-o", c.dst); cmdaddv(&c, objs, g.gl_pathc); cmdadd(&c, "src/common/cerr.o", "vendor/librune/librune.a"); CMDPRC2(c); diff --git a/src/c8asm/assembler.c b/src/c8asm/assembler.c index 353d904..c682ccb 100644 --- a/src/c8asm/assembler.c +++ b/src/c8asm/assembler.c @@ -10,13 +10,9 @@ #include "assembler.h" #include "cerr.h" #include "common.h" +#include "macros.h" #include "parser.h" -/* TODO: Remove */ -#ifndef unreachable -# define unreachable() __builtin_unreachable() -#endif - #define E_LEXISTS "label ‘%.*s’ has already been declared" #define E_LNEXISTS "label ‘%.*s’ hasn’t been declared" diff --git a/src/c8asm/common.h b/src/c8asm/common.h index 165e6bc..f5899f0 100644 --- a/src/c8asm/common.h +++ b/src/c8asm/common.h @@ -3,14 +3,6 @@ #include -#define MIN(x, y) ((x) < (y) ? (x) : (y)) -#define MAX(x, y) ((x) > (y) ? (x) : (y)) - -#define lengthof(a) (sizeof(a) / sizeof(*(a))) -#define memeq(x, y, n) (!memcmp(x, y, n)) -#define streq(x, y) (!strcmp(x, y)) -#define u8eq(x, y) (!u8cmp(x, y)) - extern size_t filesize; extern const char *filename; extern const char8_t *baseptr; diff --git a/src/c8asm/lexer.c b/src/c8asm/lexer.c index 16a9d5e..04fb8ad 100644 --- a/src/c8asm/lexer.c +++ b/src/c8asm/lexer.c @@ -5,6 +5,7 @@ #include "cerr.h" #include "common.h" #include "lexer.h" +#include "macros.h" #define ISDIGIT(n) ((n) >= '0' && (n) <= '9') #define U8MOV(sv, n) ((sv)->p += (n), (sv)->len -= (n)) diff --git a/src/c8asm/main.c b/src/c8asm/main.c index 0c8a28a..c70aed5 100644 --- a/src/c8asm/main.c +++ b/src/c8asm/main.c @@ -13,6 +13,7 @@ #include "cerr.h" #include "common.h" #include "lexer.h" +#include "macros.h" #include "parser.h" static void asmfile(int, const char *); diff --git a/src/c8asm/parser.c b/src/c8asm/parser.c index 357a082..d6e79f9 100644 --- a/src/c8asm/parser.c +++ b/src/c8asm/parser.c @@ -8,13 +8,9 @@ #include "cerr.h" #include "common.h" #include "lexer.h" +#include "macros.h" #include "parser.h" -/* TODO: Remove */ -#ifndef unreachable -# define unreachable() __builtin_unreachable() -#endif - #define E_BADLABEL "identifier ‘%.*s’ cannot be used as a label" #define E_EARLY "expected %s but input ended prematurely" #define E_EXPECTED2 "expected %s but got %s" diff --git a/src/common/macros.h b/src/common/macros.h new file mode 100644 index 0000000..6daf08f --- /dev/null +++ b/src/common/macros.h @@ -0,0 +1,27 @@ +#ifndef AHOY_COMMON_MACROS_H +#define AHOY_COMMON_MACROS_H + +#define MIN(x, y) ((x) < (y) ? (x) : (y)) +#define MAX(x, y) ((x) > (y) ? (x) : (y)) + +#define lengthof(a) (sizeof(a) / sizeof(*(a))) +#define memeq(x, y, n) (!memcmp(x, y, n)) +#define streq(x, y) (!strcmp(x, y)) +#define u8eq(x, y) (!u8cmp(x, y)) + +#if DEBUG || !defined(unreachable) +# if DEBUG +# include "cerr.h" +# ifdef unreachable +# undef unreachable +# endif +# define unreachable() \ + diex("%s:%d: hit unreachable in %s()", __FILE__, __LINE__, __func__) +# elifdef __clang__ +# define unreachable() __builtin_unreachable() +# else +# include +# endif +#endif + +#endif /* !AHOY_COMMON_MACROS_H */ -- cgit v1.2.3