Skip to content

Commit 38e082f

Browse files
committed
lots of updates to macros to make them use sq_ prefix;
1 parent a876d77 commit 38e082f

File tree

26 files changed

+413
-366
lines changed

26 files changed

+413
-366
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ifeq ($(MAKECMDGOALS),debug)
2323
CFLAGS+=-g -fsanitize=address,undefined -DSQ_LOG
2424
NJOKE=1
2525
else ifeq ($(MAKECMDGOALS),optimized)
26-
CFLAGS+=-flto -DNDEBUG -O3
26+
CFLAGS+=-flto -DNDEBUG -O3 --DSQ_RELEASE_FAST
2727
endif
2828

2929
ifdef NJOKE

examples/showcase.sq

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
onward

include/squire/book.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static inline struct sq_book *sq_book_new2(size_t length, sq_value *pages) {
3939

4040
// Creates a new book with the given capacity.
4141
static inline struct sq_book *sq_book_allocate(size_t capacity) {
42-
return sq_book_new(0, capacity, xmalloc(sizeof_array(sq_value, capacity)));
42+
return sq_book_new(0, capacity, sq_malloc(sizeof_array(sq_value, capacity)));
4343
}
4444

4545
/** Deallocates the memory associated with `book`.

include/squire/shared.h

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,72 @@
44
#include <stdio.h>
55
#include <stdlib.h>
66

7-
#if defined(__GNUC__) || defined(SQ_USE_ATTRIBUTES)
7+
#ifdef __has_builtin
8+
# define SQ_HAS_BUILTIN(x) __has_builtin(x)
9+
#else
10+
# define SQ_HAS_BUILTIN(x) 0
11+
#endif /* defined __has_builtin */
12+
13+
#ifdef __has_attribute
14+
# define SQ_HAS_ATTRIBUTE(x) __has_attribute(x)
15+
#else
16+
# define SQ_HAS_ATTRIBUTE(x) 0
17+
#endif /* defined __has_attribute */
18+
19+
#if defined(__GNUC__) || defined(__clang__)
820
# define SQ_ATTR(...) __attribute__((__VA_ARGS__))
921
#else
1022
# define SQ_ATTR(...)
11-
#endif /* __GNUC__ || SQ_USE_ATTRIBUTES */
23+
#endif /* __GNUC__ || __clang__ */
1224

13-
#define SQ_NONNULL SQ_ATTR(nonnull)
14-
#define sizeof_array(kind, length) (sizeof(kind) * (length))
25+
#if SQ_HAS_ATTRIBUTE(nonnull)
26+
# define SQ_NONNULL SQ_ATTR(nonnull)
27+
#else
28+
# define SQ_NONNULL
29+
#endif /* SQ_HAS_ATTRIBUTE(nonnull) */
1530

16-
#define SQ_UNLIKELY(x) (x)
17-
#define SQ_LIKELY(x) (x)
31+
#if SQ_HAS_BUILTIN(__builtin_expect)
32+
# define SQ_LIKELY(x) (__builtin_expect(1, !!(x)))
33+
#else
34+
# define SQ_LIKELY(x) (!!(x))
35+
#endif /* SQ_HAS_BUILTIN(__builtin_expect) */
36+
#define SQ_UNLIKELY(x) SQ_LIKELY(!(x))
1837

19-
#include <squire/exception.h>
38+
#if SQ_HAS_BUILTIN(__builtin_unreachable)
39+
# define SQ_UNREACHABLE do { __builtin_unreachable(); } while(0)
40+
#else
41+
# define SQ_UNREACHABLE do { abort(); } while(0)
42+
#endif /* SQ_HAS_BUILTIN(__builtin_unreachable) */
2043

21-
#define die sq_throw
22-
#define todo(...) (fprintf(stderr, __VA_ARGS__), exit(1))
23-
#define bug(...) (\
24-
fprintf(stderr, "bug at " __FILE__ ":%s:%d: ", __func__, __LINE__),\
25-
fprintf(stderr, __VA_ARGS__),\
26-
fputc('\n', stderr),\
27-
fflush(stderr),\
28-
abort())
44+
#ifdef SQ_RELEASE_FAST
45+
# define sq_bug(...) SQ_UNREACHABLE
46+
#else
47+
# define sq_bug(...) do {sq_bug_fn(__FILE__, __func__, __LINE__, __VA_ARGS__); } while(0)
48+
#endif /* defined(SQ_RELEASE_FAST) */
2949

3050
#ifdef SQ_LOG
31-
#define LOG printf
51+
#define sq_log printf
3252
#else
33-
#define LOG(...) ((void) 0)
53+
#define sq_log(...) ((void) 0)
3454
#endif
3555

36-
void *xcalloc(size_t count, size_t size) SQ_ATTR(malloc);
37-
void *xmalloc(size_t size) SQ_ATTR(malloc);
38-
void *xrealloc(void *ptr, size_t size);
39-
void *memdup(void *ptr, size_t size);
4056

41-
void sq_memory_error(char *msg, ...) SQ_ATTR(cold,noreturn);
42-
char *read_file(const char *filename);
57+
#include <squire/exception.h>
58+
#define die sq_throw
59+
#define sq_todo(...) (fprintf(stderr, __VA_ARGS__), exit(1))
60+
61+
#define sq_
62+
#define sizeof_array(kind, length) (sizeof(kind) * (length))
63+
64+
void *sq_calloc(size_t count, size_t size) SQ_ATTR(malloc) ;
65+
void *sq_malloc(size_t size) SQ_ATTR(malloc);
66+
void *sq_realloc(void *ptr, size_t size);
67+
void *sq_memdup(void *ptr, size_t size);
68+
69+
void sq_bug_fn(
70+
const char *file, const char *function, long long line, const char *fmt, ...
71+
) SQ_ATTR(cold,noreturn);
72+
void sq_memory_error(const char *fmt, ...) SQ_ATTR(cold,noreturn);
73+
char *sq_read_file(const char *filename);
4374

4475
#endif /* !SQ_SHARED_H */

src/exception.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ void sq_exception_init(struct sq_program *program) {
2121
sq_exception_form.refcount = 1;
2222

2323
sq_exception_form.nmatter = 1;
24-
sq_exception_form.matter = xmalloc(sizeof(struct sq_form_matter));
24+
sq_exception_form.matter = sq_malloc(sizeof(struct sq_form_matter));
2525
sq_exception_form.matter[0].name = "msg";
2626
sq_exception_form.matter[0].genus = SQ_UNDEFINED; // todo: make it text.
2727

2828
sq_exception_form.nchanges = 1;
29-
sq_exception_form.changes = xmalloc(sizeof(struct sq_journey *));
30-
struct sq_journey *to_text = sq_exception_form.changes[0] = xmalloc(sizeof(struct sq_journey));
29+
sq_exception_form.changes = sq_malloc(sizeof(struct sq_journey *));
30+
struct sq_journey *to_text = sq_exception_form.changes[0] = sq_malloc(sizeof(struct sq_journey));
3131

3232
(void) to_text;
3333
(void) program;
@@ -42,7 +42,7 @@ void sq_exception_init(struct sq_program *program) {
4242
// to_text->consts = NULL;
4343
// to_text->program = program;
4444
// to_text->is_method = true;
45-
// to_text->bytecode = xmalloc(sizeof_array(union sq_bytecode, 4));
45+
// to_text->bytecode = sq_malloc(sizeof_array(union sq_bytecode, 4));
4646
// to_text->bytecode[0].opcode = SQ_OC_ILOAD;
4747
// to_text->bytecode[1].index = 0;
4848
// to_text->bytecode[2].index = 0;

src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int main(int argc, const char **argv) {
1818
if (argv[1][1] == 'e') {
1919
sq_program_compile(&program, argv[2]);
2020
} else {
21-
char *contents = read_file(argv[2]);
21+
char *contents = sq_read_file(argv[2]);
2222
sq_program_compile(&program, contents);
2323
free(contents);
2424
}

src/other/io/envoy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void sq_envoy_initialize(struct sq_envoy *envoy, const char *filename) {
1313
envoy->symlen = 0;
1414
envoy->symcap = 4;
1515
envoy->filename = strdup(filename);
16-
envoy->syms = xmalloc(sizeof_array(struct sq_envoy_sym, envoy->symcap));
16+
envoy->syms = sq_malloc(sizeof_array(struct sq_envoy_sym, envoy->symcap));
1717
}
1818

1919
void sq_envoy_deallocate(struct sq_envoy *envoy) {

src/other/io/scroll.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void sq_scroll_init(struct sq_scroll *scroll, const char *filename, const char *
3030

3131
struct sq_other *other;
3232
#define NEW_JOURNEY(_name, _nargs) \
33-
_name##_journey = sq_value_new(other = xmalloc(sizeof(struct sq_other))); \
33+
_name##_journey = sq_value_new(other = sq_malloc(sizeof(struct sq_other))); \
3434
other->refcount = 1; \
3535
other->kind = SQ_OK_BUILTIN_JOURNEY; \
3636
other->builtin_journey.name = "Scroll."#_name; \
@@ -98,7 +98,7 @@ struct sq_text *sq_scroll_read(struct sq_scroll *scroll, size_t length) {
9898
}
9999

100100
struct sq_text *sq_scroll_read_all(struct sq_scroll *scroll) {
101-
return sq_text_new(read_file(scroll->filename));
101+
return sq_text_new(sq_read_file(scroll->filename));
102102
}
103103

104104
void sq_scroll_write(struct sq_scroll *scroll, const char *ptr, size_t length) {
@@ -145,7 +145,7 @@ static sq_value read_func(struct sq_args args) {
145145

146146
case SQ_G_TEXT:
147147
if (strcmp(sq_value_as_text(arg)->ptr, "\n"))
148-
die("todo: non-newline gets");
148+
sq_throw("todo: non-newline gets");
149149

150150
{
151151
size_t length;

src/other/kingdom.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
void sq_kingdom_initialize(struct sq_kingdom *kingdom, unsigned capacity) {
88
kingdom->nsubjects = 0;
99
kingdom->subject_cap = capacity;
10-
kingdom->subjects = xmalloc(sizeof_array(struct sq_kingdom_subject, capacity));
10+
kingdom->subjects = sq_malloc(sizeof_array(struct sq_kingdom_subject, capacity));
1111
}
1212

1313
void sq_kingdom_dump(FILE *out, const struct sq_kingdom *kingdom) {
@@ -62,7 +62,7 @@ bool sq_kingdom_set_attr(struct sq_kingdom *kingdom, const char *name, sq_value
6262
if (kingdom->subject_cap == kingdom->nsubjects) {
6363
kingdom->subject_cap *= 2;
6464
kingdom->subjects =
65-
xrealloc(
65+
sq_realloc(
6666
kingdom->subjects,
6767
sizeof_array(struct sq_kingdom_subject, kingdom->subject_cap)
6868
);

src/other/other.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ struct sq_text *sq_other_to_text(const struct sq_other *other) {
143143
case SQ_OK_BUILTIN_JOURNEY:
144144
case SQ_OK_CITATION:
145145
case SQ_OK_PAT_HELPER:
146-
todo("others to text");
146+
sq_todo("others to text");
147147
}
148148
}
149149

src/posix.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct _nothing_to_see_here;
99
char *strdup(char *str) {
1010
size_t length = strlen(str) + 1;
1111

12-
return memcpy(xmalloc(length), str, length);
12+
return memcpy(sq_malloc(length), str, length);
1313
}
1414

1515
char *strndup(char *str, size_t maxlen) {
@@ -18,7 +18,7 @@ char *strndup(char *str, size_t maxlen) {
1818
if (length < maxlen)
1919
return strdup(str);
2020

21-
char *result = xmalloc(maxlen);
21+
char *result = sq_malloc(maxlen);
2222
memcpy(result, str, maxlen);
2323
result[maxlen] = '\0';
2424

src/program/bytecode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const char *sq_interrupt_repr(enum sq_interrupt interrupt) {
3434

3535
case SQ_INT_FOPEN: return "SQ_INT_FOPEN";
3636
case SQ_INT_ASCII: return "SQ_INT_ASCII";
37-
case SQ_INT_UNDEFINED: bug("undefined encountered");
37+
case SQ_INT_UNDEFINED: sq_bug("undefined encountered");
3838
}
3939
}
4040

0 commit comments

Comments
 (0)