From 6d4f167b97306e0fb1691b9da653ec28e7094bc1 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Tue, 11 Jun 2024 18:07:15 +0200 Subject: Code reshuffling --- make.c | 8 -------- src/alloc.h | 2 ++ src/codegen.c | 1 + src/common.h | 13 +++++++++++++ src/errors.h | 2 ++ src/parser.c | 5 +++-- src/types.h | 5 ++++- src/unicode.h | 14 ++++++++------ 8 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 src/common.h diff --git a/make.c b/make.c index 2a29fe6..d9b8bf3 100644 --- a/make.c +++ b/make.c @@ -31,14 +31,6 @@ static char *cflags_all[] = { #if __GLIBC__ "-D_GNU_SOURCE", #endif -#if __GNUC__ - "-Dlikely(x)=__builtin_expect(!!(x), 1)", - "-Dunlikely(x)=__builtin_expect(!!(x), 0)", -#else - "-D__attribute__(x)=", - "-Dlikely(x)=(x)", - "-Dunlikely(x)=(x)", -#endif }; static char *cflags_dbg[] = { diff --git a/src/alloc.h b/src/alloc.h index 8e8b365..301971c 100644 --- a/src/alloc.h +++ b/src/alloc.h @@ -3,6 +3,8 @@ #include +#include "common.h" + /* Allocate a buffer of NMEMB elements of size SIZE. If PTR is non-null then reallocate the buffer it points to. Aborts on out-of-memory or overflow. */ void *bufalloc(void *ptr, size_t nmemb, size_t size) diff --git a/src/codegen.c b/src/codegen.c index abdf311..ec7a3ef 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -7,6 +7,7 @@ #include "alloc.h" #include "codegen.h" +#include "common.h" #include "errors.h" #include "parser.h" diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..be88f0b --- /dev/null +++ b/src/common.h @@ -0,0 +1,13 @@ +#ifndef ORYX_COMMON_H +#define ORYX_COMMON_H + +#ifdef __GNUC__ +# define likely(x) __builtin_expect(!!(x), 1) +# define unlikely(x) __builtin_expect(!!(x), 0) +#else +# define __attribute__(x) +# define likely(x) (x) +# define unlikely(x) (x) +#endif + +#endif /* !ORYX_COMMON_H */ diff --git a/src/errors.h b/src/errors.h index eebd607..669e4e5 100644 --- a/src/errors.h +++ b/src/errors.h @@ -3,6 +3,8 @@ #include +#include "common.h" + noreturn void err(const char *, ...) __attribute__((format(printf, 1, 2))); #endif /* !ORYX_ERRORS_H */ diff --git a/src/parser.c b/src/parser.c index d194bac..a1028aa 100644 --- a/src/parser.c +++ b/src/parser.c @@ -7,6 +7,7 @@ #include #include "alloc.h" +#include "common.h" #include "errors.h" #include "parser.h" @@ -22,9 +23,9 @@ static parsefn parseblk, parsestmt, parsetype; -static idx_t_ astalloc(struct ast *); static struct ast mkast(void); -static void astresz(struct ast *); +static idx_t_ astalloc(struct ast *) __attribute__((nonnull)); +static void astresz(struct ast *) __attribute__((nonnull)); static size_t toksidx; diff --git a/src/types.h b/src/types.h index 34441b5..443aa12 100644 --- a/src/types.h +++ b/src/types.h @@ -4,8 +4,11 @@ #include #include -typedef unsigned char uchar; typedef uint32_t idx_t_; +typedef uint32_t rune; +typedef unsigned char uchar; + +#define RUNE_C(x) UINT32_C(x) struct strview { const uchar *p; diff --git a/src/unicode.h b/src/unicode.h index 36bbc81..8c38409 100644 --- a/src/unicode.h +++ b/src/unicode.h @@ -3,10 +3,9 @@ #include #include -#include -#define RUNE_C(x) UINT32_C(x) -typedef uint32_t rune; +#include "common.h" +#include "types.h" /* Assert that CH has the Unicode Pattern_White_Space property */ bool rune_is_pat_ws(rune ch) __attribute__((const)); @@ -21,17 +20,20 @@ bool rune_is_xidc(rune ch) __attribute__((const)); /* Decode the first UTF-8 rune in S, and point S to the next rune in the stream. This function assumes that S points to a buffer that’s padded to a length of 4 bytes, and doesn’t perform any form of input validation. */ -rune utf8_decode(const unsigned char **s); +rune utf8_decode(const uchar **s) + __attribute__((nonnull)); /* Return the offset of the first invalid byte in the UTF-8 string S of length LEN. This function assumes that S points to a buffer that’s padded to a length of 4 bytes (although LEN should not reflect any added padding). */ -size_t utf8_validate_off(const unsigned char *s, size_t len); +size_t utf8_validate_off(const uchar *s, size_t len) + __attribute__((nonnull)); #if ORYX_SIMD /* Assert whether the UTF-8 string S of length LEN is valid, using SIMD intristics to speed-up computation. */ -bool utf8_validate_simd(const unsigned char *s, size_t len); +bool utf8_validate_simd(const uchar *s, size_t len) + __attribute__((nonnull)); #endif #endif /* !ORYX_UNICODE_H */ -- cgit v1.2.3