From 7b5abecd2bef6b7441b0d850f95d451b441a6f76 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Tue, 18 Mar 2025 19:58:30 +0200 Subject: move to generic conts --- .gitmodules | 3 + deps/conts | 1 + include/ek/conts.h | 14 -- include/ek/map.h | 105 ------------- include/ek/scope.h | 6 +- include/ek/sptree.h | 426 ---------------------------------------------------- include/ek/vec.h | 46 ------ scripts/makefile | 2 +- src/actualize.c | 42 +++--- src/ast.c | 39 ++--- src/compiler.c | 2 +- src/lower.c | 177 +++++++++++----------- src/vec.c | 67 --------- 13 files changed, 143 insertions(+), 787 deletions(-) create mode 100644 .gitmodules create mode 160000 deps/conts delete mode 100644 include/ek/conts.h delete mode 100644 include/ek/map.h delete mode 100644 include/ek/sptree.h delete mode 100644 include/ek/vec.h delete mode 100644 src/vec.c diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..bf47852 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "deps/conts"] + path = deps/conts + url = https://metanimi.dy.fi/git/conts diff --git a/deps/conts b/deps/conts new file mode 160000 index 0000000..4f647dc --- /dev/null +++ b/deps/conts @@ -0,0 +1 @@ +Subproject commit 4f647dc8520a9186b367400c5a337b681aec5565 diff --git a/include/ek/conts.h b/include/ek/conts.h deleted file mode 100644 index c1e4d24..0000000 --- a/include/ek/conts.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef CONTS_H -#define CONTS_H - -#define CONTS_JOIN2(a, b) a##_##b -#define CONTS_JOIN(a, b) CONTS_JOIN2(a, b) - -#define CONTAINER_OF(ptr, type, member) \ - (type *)((char *)(ptr) - offsetof(type, member)) - -#define foreach(name, i, s) \ - for (auto i = CONTS_JOIN(name, begin)(s); \ - !CONTS_JOIN(name, end)(s, i); \ - i = CONTS_JOIN(name, next)(i)) -#endif /* CONTS_H */ diff --git a/include/ek/map.h b/include/ek/map.h deleted file mode 100644 index 5a6a22d..0000000 --- a/include/ek/map.h +++ /dev/null @@ -1,105 +0,0 @@ -#ifndef MAP_KEY -#error "Need map key" -#endif - -#ifndef MAP_TYPE -#error "Need map type" -#endif - -#ifndef MAP_CMP -#error "Need map cmp" -#endif - -#ifndef MAP_NAME -#error "Need map name" -#endif - -#include "conts.h" - -#define MAP(a) CONTS_JOIN(MAP_NAME, a) - -#define MAP_NODE MAP(node) -#define MAP_ROOT MAP_NAME - -struct MAP_NODE { - MAP_KEY key; - MAP_TYPE data; -}; - -static inline int MAP(cmp)(struct MAP_NODE a, struct MAP_NODE b) -{ - return MAP_CMP(a.key, b.key); -} - -#define BASE(a) CONTS_JOIN(MAP(map_base), a) - -#define SPTREE_TYPE struct MAP_NODE -#define SPTREE_CMP MAP(cmp) -#define SPTREE_NAME MAP(map_base) -#include "sptree.h" - -struct MAP_ROOT { - struct MAP(map_base) root; -}; - -static inline struct MAP_ROOT MAP(create)() -{ - return (struct MAP_ROOT){.root = BASE(create)()}; -} - -static inline void MAP(destroy)(struct MAP_ROOT *root) -{ - BASE(destroy)(&root->root); -} - -static inline MAP_TYPE *MAP(insert)(struct MAP_ROOT *root, MAP_KEY key, - MAP_TYPE data) -{ - struct MAP_NODE node = {.key = key, .data = data}; - struct MAP_NODE *res = BASE(insert)(&root->root, node); - if (!res) - return NULL; - - return &res->data; -} - -static inline MAP_TYPE *MAP(find)(struct MAP_ROOT *root, MAP_KEY key) -{ - struct MAP_NODE node = {.key = key}; - struct MAP_NODE *res = BASE(find)(&root->root, node); - if (!res) - return NULL; - - return &res->data; -} - -static inline void MAP(remove)(struct MAP_ROOT *root, MAP_KEY key) -{ - struct MAP_NODE node = {.key = key}; - BASE(remove)(&root->root, node); -} - -static inline struct MAP_NODE *MAP(begin)(struct MAP_ROOT *root) -{ - return BASE(begin)(&root->root); -} - -static inline struct MAP_NODE *MAP(next)(struct MAP_NODE *n) -{ - return BASE(next)(n); -} - -static inline bool MAP(end)(struct MAP_ROOT *root, struct MAP_NODE *n) -{ - return BASE(end)(&root->root, n); -} - -static inline size_t MAP(len)(struct MAP_ROOT *root) -{ - return BASE(len)(&root->root); -} - -#undef MAP_KEY -#undef MAP_TYPE -#undef MAP_CMP -#undef MAP_NAME diff --git a/include/ek/scope.h b/include/ek/scope.h index 810f1cc..8c56640 100644 --- a/include/ek/scope.h +++ b/include/ek/scope.h @@ -33,7 +33,7 @@ struct visible_tuple { #define MAP_TYPE struct ast * #define MAP_CMP(a, b) strcmp((a), (b)) #define MAP_NAME visible -#include "map.h" +#include struct expanded_key { struct ast *def; @@ -54,12 +54,12 @@ static inline int expanded_key_cmp(struct expanded_key a, struct expanded_key b) #define MAP_TYPE struct ast * #define MAP_CMP(a, b) expanded_key_cmp((a), (b)) #define MAP_NAME expanded -#include "map.h" +#include #define SPTREE_TYPE struct ast * #define SPTREE_CMP(a, b) ((uintptr_t)(a) - (uintptr_t)(b)) #define SPTREE_NAME exported -#include "sptree.h" +#include /** * Scope. diff --git a/include/ek/sptree.h b/include/ek/sptree.h deleted file mode 100644 index 8ba6d6c..0000000 --- a/include/ek/sptree.h +++ /dev/null @@ -1,426 +0,0 @@ -#include -#include -#include -#include -#include - -#ifndef SPTREE_TYPE -#error "Need sptree type" -#endif - -#ifndef SPTREE_CMP -#error "Need sptree cmp" -#endif - -#ifndef SPTREE_NAME -#error "Need sptree name" -#endif - -#include "conts.h" - -#define SPTREE(a) CONTS_JOIN(SPTREE_NAME, a) - -#define SPNODE SPTREE(node) -#define SPROOT SPTREE_NAME - -#ifndef SPTREE_H -#define SPTREE_H - -#define sp_left(n) ((n)->left) -#define sp_right(n) ((n)->right) -#define sp_paren(n) ((n)->parent) -#define sp_lparen(n) (sp_left(n)->parent) -#define sp_rparen(n) (sp_right(n)->parent) - -#endif /* SPTREE_H */ - -struct SPNODE { - int_fast16_t hint; - struct SPNODE *left, *right, *parent; - SPTREE_TYPE data; -}; - -struct SPROOT { - size_t n; - struct SPNODE *root; -}; - -static inline struct SPROOT SPTREE(create)() -{ - return (struct SPROOT){.n = 0, .root = NULL}; -} - -static inline size_t SPTREE(len)(struct SPROOT *s) -{ - return s->n; -} - -static inline struct SPNODE *SPTREE(first)(struct SPNODE *n) -{ - while (sp_left(n)) - n = sp_left(n); - - return n; -} - -static inline struct SPNODE *SPTREE(last)(struct SPNODE *n) -{ - while (sp_right(n)) - n = sp_right(n); - - return n; -} - -static inline SPTREE_TYPE *SPTREE(begin)(struct SPROOT *s) -{ - if (SPTREE(len)(s) == 0) - return NULL; - - return &SPTREE(first)(s->root)->data; -} - -static inline SPTREE_TYPE *SPTREE(next)(SPTREE_TYPE *prev) -{ - struct SPNODE *n = CONTAINER_OF(prev, struct SPNODE, data); - if (sp_right(n)) { - n = sp_right(n); - while (sp_left(n)) - n = sp_left(n); - - return &n->data; - } - - while (n) { - struct SPNODE *p = sp_paren(n); - if (!p) - return NULL; - - if (sp_left(p) == n) - return &p->data; - - n = p; - } - - return NULL; -} - -static inline bool SPTREE(end)(struct SPROOT *s, SPTREE_TYPE *prev) -{ - (void)s; - return prev == NULL; -} - -static inline void SPTREE(turn_left)(struct SPNODE *n) -{ - struct SPNODE *l = sp_left(n); - struct SPNODE *p = sp_paren(n); - - assert(l); - - sp_paren(l) = sp_paren(n); - sp_left(n) = sp_right(l); - sp_paren(n) = l; - sp_right(l) = n; - - if (p && sp_left(p) == n) - sp_left(p) = l; - else if (p) - sp_right(p) = l; - - if (sp_left(n)) - sp_lparen(n) = n; -} - -static inline void SPTREE(turn_right)(struct SPNODE *n) -{ - struct SPNODE *r = sp_right(n); - struct SPNODE *p = sp_paren(n); - - assert(r); - - sp_paren(r) = sp_paren(n); - sp_right(n) = sp_left(r); - sp_paren(n) = r; - sp_left(r) = n; - - if (p && sp_left(p) == n) - sp_left(p) = r; - else if (p) - sp_right(p) = r; - - if (sp_right(n)) - sp_rparen(n) = n; -} - -static inline int_fast16_t SPTREE(leaning)(struct SPNODE *n) -{ - int_fast16_t l = 0; - int_fast16_t r = 0; - - if (sp_left(n)) - l = sp_left(n)->hint + 1; - - if (sp_right(n)) - r = sp_right(n)->hint + 1; - - return l - r; -} - -static inline int_fast16_t SPTREE(max_hint)(struct SPNODE *n) -{ - int_fast16_t l = 0; - int_fast16_t r = 0; - - if (sp_left(n)) - l = sp_left(n)->hint + 1; - - if (sp_right(n)) - r = sp_right(n)->hint + 1; - - if (l > r) - return l; - else - return r; -} - -static inline void SPTREE(update)(struct SPROOT *r, struct SPNODE *n) -{ - while (n) { - int b = SPTREE(leaning)(n); - int prev_hint = n->hint; - struct SPNODE *p = sp_paren(n); - - if (b < -1) { - /* leaning to the right */ - if (n == r->root) - r->root = sp_right(n); - - SPTREE(turn_right)(n); - } - - else if (b > 1) { - /* leaning to the left */ - if (n == r->root) - r->root = sp_left(n); - - SPTREE(turn_left)(n); - } - - n->hint = SPTREE(max_hint)(n); - if (n->hint == 0 || n->hint != prev_hint) - n = p; - else - return; - } -} - -static inline SPTREE_TYPE *SPTREE(insert)(struct SPROOT *s, SPTREE_TYPE data) -{ - if (!s->root) { - assert(s->n == 0); - struct SPNODE *new = malloc(sizeof(struct SPNODE)); - if (!new) - return NULL; - - new->left = new->right = new->parent = NULL; - new->data = data; - new->hint = 0; - - s->root = new; - s->n = 1; - return &new->data; - } - - bool insert_left = false; - - struct SPNODE *n = s->root; - struct SPNODE *p = NULL; - while (n) { - p = n; - int c = SPTREE_CMP(n->data, data); - if (c < 0) { - n = sp_left(n); - insert_left = true; - continue; - } - if (c > 0) { - n = sp_right(n); - insert_left = false; - continue; - } - - /* we already have a node like this */ - return &n->data; - } - - struct SPNODE *new = malloc(sizeof(struct SPNODE)); - if (!new) - return NULL; - - new->left = new->right = NULL; - new->parent = p; - new->data = data; - new->hint = 0; - - if (insert_left) - sp_left(p) = new; - else - sp_right(p) = new; - - SPTREE(update)(s, new); - s->n++; - return &new->data; -} - -static inline void SPTREE(replace_right)(struct SPNODE *n, struct SPNODE *r) -{ - struct SPNODE *p = sp_paren(n); - struct SPNODE *rp = sp_paren(r); - - if (sp_left(rp) == r) { - sp_left(rp) = sp_right(r); - if (sp_right(r)) - sp_rparen(r) = rp; - } - - if (sp_paren(rp) == n) - sp_paren(rp) = r; - - sp_paren(r) = p; - sp_left(r) = sp_left(n); - - if (sp_right(n) != r) { - sp_right(r) = sp_right(n); - sp_rparen(n) = r; - } - - if (p && sp_left(p) == n) - sp_left(p) = r; - else if (p) - sp_right(p) = r; - - if (sp_left(n)) - sp_lparen(n) = r; -} - -static inline void SPTREE(replace_left)(struct SPNODE *n, struct SPNODE *l) -{ - struct SPNODE *p = sp_paren(n); - struct SPNODE *lp = sp_paren(l); - - if (sp_right(lp) == l) { - sp_right(lp) = sp_left(l); - if (sp_left(l)) - sp_lparen(l) = lp; - } - - if (sp_paren(lp) == n) - sp_paren(lp) = l; - - sp_paren(l) = p; - sp_right(l) = sp_right(n); - - if (sp_left(n) != l) { - sp_left(l) = sp_left(n); - sp_lparen(n) = l; - } - - if (p && sp_left(p) == n) - sp_left(p) = l; - else if (p) - sp_right(p) = l; - - if (sp_right(n)) - sp_rparen(n) = l; -} - -static inline SPTREE_TYPE *SPTREE(find)(struct SPROOT *s, SPTREE_TYPE data) -{ - struct SPNODE *n = s->root; - while (n) { - int c = SPTREE_CMP(n->data, data); - if (c < 0) { - n = n->left; - continue; - } - - if (c > 0) { - n = n->right; - continue; - } - - return &n->data; - } - - return NULL; -} - -static inline void SPTREE(remove_found)(struct SPROOT *s, SPTREE_TYPE *found) -{ - s->n--; - struct SPNODE *del = CONTAINER_OF(found, struct SPNODE, data); - if (sp_right(del)) { - struct SPNODE *least = SPTREE(first)(sp_right(del)); - - if (del == s->root) - s->root = least; - - SPTREE(replace_right)(del, least); - SPTREE(update)(s, sp_right(least)); - return; - } - - if (sp_left(del)) { - struct SPNODE *most = SPTREE(last)(sp_left(del)); - - if (del == s->root) - s->root = most; - - SPTREE(replace_left)(del, most); - SPTREE(update)(s, sp_left(most)); - return; - } - - if (del == s->root) { - s->root = NULL; - return; - } - - /* empty node */ - struct SPNODE *paren = sp_paren(del); - - if (sp_left(paren) == del) - sp_left(paren) = NULL; - else - sp_right(paren) = NULL; - - SPTREE(update)(s, paren); -} - -static inline void SPTREE(remove)(struct SPROOT *s, SPTREE_TYPE data) -{ - SPTREE_TYPE *found = SPTREE(find)(s, data); - if (!found) - return; - - SPTREE(remove_found)(s, found); - struct SPNODE *del = CONTAINER_OF(found, struct SPNODE, data); - free(del); -} - -static inline void SPTREE(destroy)(struct SPROOT *s) -{ - while (s->root) { - SPTREE_TYPE *top = &s->root->data; - SPTREE(remove_found)(s, top); - struct SPNODE *del = CONTAINER_OF(top, struct SPNODE, data); - free(del); - } -} - -#undef SPTREE -#undef SPNODE -#undef SPROOT -#undef SPTREE_NAME -#undef SPTREE_TYPE -#undef SPTREE_CMP diff --git a/include/ek/vec.h b/include/ek/vec.h deleted file mode 100644 index 08a608a..0000000 --- a/include/ek/vec.h +++ /dev/null @@ -1,46 +0,0 @@ -/* SPDX-License-Identifier: copyleft-next-0.3.1 */ - -#ifndef VEC_H -#define VEC_H - -#include - -struct vec { - size_t n; - size_t s; - size_t ns; - void *buf; -}; - -struct vec vec_create(size_t s); -void vec_destroy(struct vec *v); -void vec_reset(struct vec *v); - -size_t vec_len(struct vec *v); -void *vec_at(struct vec *v, size_t i); -void *vec_back(struct vec *v); -void *vec_pop(struct vec *v); -void vec_append(struct vec *v, void *n); - -typedef int (*vec_comp_t)(const void *, const void *); -void vec_sort(struct vec *v, vec_comp_t comp); - -#define foreach_vec(iter, v) \ - for (size_t iter = 0; iter < vec_len(&v); ++iter) - -#define vect_at(type, v, i) \ - *(type *)vec_at(&v, i) - -#define vect_append(type, v, e) \ - vec_append(&v, (type *)(e)) - -#define vect_back(type, v) \ - *(type *)vec_back(&v) - -#define vect_pop(type, v) \ - *(type *)vec_pop(&v) - -#define vec_uninit(v) \ - (v.buf == NULL) - -#endif /* VEC_H */ diff --git a/scripts/makefile b/scripts/makefile index 0ee0e7d..7d86089 100644 --- a/scripts/makefile +++ b/scripts/makefile @@ -48,7 +48,7 @@ WARNFLAGS := -Wall -Wextra COMPILE_FLAGS := $(CFLAGS) $(WARNFLAGS) $(OPTFLAGS) $(OBFLAGS) $(ASSERTFLAGS) \ $(DEBUGFLAGS) -INCLUDE_FLAGS := -I include +INCLUDE_FLAGS := -I include -I deps/conts/include COMPILE = $(COMPILER) \ $(COMPILE_FLAGS) $(DEPFLAGS) $(INCLUDE_FLAGS) diff --git a/src/actualize.c b/src/actualize.c index 903d283..a846d10 100644 --- a/src/actualize.c +++ b/src/actualize.c @@ -15,7 +15,6 @@ #include #include #include -#include #define UNUSED(x) do { (void)(x); } while (0) #define MAYBE_UNUSED(x) UNUSED(x) @@ -2796,11 +2795,14 @@ struct init_helper { struct ast *n; }; -static int init_sort(const struct init_helper *a, const struct init_helper *b) +static int init_sort(struct init_helper *a, struct init_helper *b) { return strcmp(a->id, b->id); } +#define VEC_NAME init_helper_vec +#define VEC_TYPE struct init_helper +#include static int actualize_init(struct act_state *state, struct scope *scope, struct ast *node) @@ -2821,8 +2823,8 @@ static int actualize_init(struct act_state *state, if (!def) return -1; - struct vec init_args = vec_create(sizeof(struct init_helper)); - struct vec struct_members = vec_create(sizeof(struct init_helper)); + struct init_helper_vec init_args = init_helper_vec_create(0); + struct init_helper_vec struct_members = init_helper_vec_create(0); struct ast *base = chain_base(def); foreach_node(n, struct_body(base)) { @@ -2830,7 +2832,7 @@ static int actualize_init(struct act_state *state, continue; struct init_helper h = {var_id(n), n}; - vect_append(struct init_helper, struct_members, &h); + init_helper_vec_append(&struct_members, h); } foreach_node(n, init_body(node)) { @@ -2841,27 +2843,23 @@ static int actualize_init(struct act_state *state, set_type(n, (var_init(n))->t); struct init_helper h = {var_id(n), n}; - vect_append(struct init_helper, init_args, &h); + init_helper_vec_append(&init_args, h); } - vec_sort(&init_args, (vec_comp_t)init_sort); - vec_sort(&struct_members, (vec_comp_t)init_sort); + init_helper_vec_sort(&init_args, init_sort); + init_helper_vec_sort(&struct_members, init_sort); - if (vec_len(&init_args) != vec_len(&struct_members)) { + if (init_helper_vec_len(&init_args) != init_helper_vec_len(&struct_members)) { semantic_error(scope->fctx, node, "expected %zu args, got %zu", - vec_len(&struct_members), - vec_len(&init_args)); + init_helper_vec_len(&struct_members), + init_helper_vec_len(&init_args)); goto err; } /* not insanely fast but good enough I suppose */ - foreach_vec(ai, init_args) { - struct init_helper arg = vect_at(struct init_helper, init_args, - ai); - - struct init_helper mem = vect_at(struct init_helper, - struct_members, - ai); + for (size_t ai = 0; ai < init_helper_vec_len(&init_args); ++ai){ + struct init_helper arg = *init_helper_vec_at(&init_args, ai); + struct init_helper mem = *init_helper_vec_at(&struct_members, ai); /* not the best error message but works for now */ if (!same_id(arg.id, mem.id)) { @@ -2876,14 +2874,14 @@ static int actualize_init(struct act_state *state, } } - vec_destroy(&init_args); - vec_destroy(&struct_members); + init_helper_vec_destroy(&init_args); + init_helper_vec_destroy(&struct_members); set_type(node, def->t); return 0; err: - vec_destroy(&init_args); - vec_destroy(&struct_members); + init_helper_vec_destroy(&init_args); + init_helper_vec_destroy(&struct_members); return -1; } diff --git a/src/ast.c b/src/ast.c index 5561745..6e6b849 100644 --- a/src/ast.c +++ b/src/ast.c @@ -15,11 +15,18 @@ #include #include -#include #include -static struct vec nodes = {0}; -static struct vec types = {0}; +#define VEC_NAME ast_vec +#define VEC_TYPE struct ast * +#include + +#define VEC_NAME type_vec +#define VEC_TYPE struct type * +#include + +static struct ast_vec nodes; +static struct type_vec types; static void destroy_ast_node(struct ast *n) { @@ -45,22 +52,20 @@ static void destroy_type(struct type *n) void destroy_ast_nodes() { - foreach_vec(ni, nodes) { - struct ast *n = vect_at(struct ast *, nodes, ni); - destroy_ast_node(n); + foreach(ast_vec, n, &nodes) { + destroy_ast_node(*n); } - vec_destroy(&nodes); + ast_vec_destroy(&nodes); } void destroy_types() { - foreach_vec(ti, types) { - struct type *t = vect_at(struct type *, types, ti); - destroy_type(t); + foreach(type_vec, t, &types) { + destroy_type(*t); } - vec_destroy(&types); + type_vec_destroy(&types); } void destroy_allocs() @@ -71,8 +76,8 @@ void destroy_allocs() static struct ast *create_empty_ast() { - if (vec_uninit(nodes)) { - nodes = vec_create(sizeof(struct ast *)); + if (ast_vec_uninit(&nodes)) { + nodes = ast_vec_create(0); } struct ast *n = calloc(1, sizeof(struct ast)); @@ -81,14 +86,14 @@ static struct ast *create_empty_ast() /* just to be safe */ n->k = AST_EMPTY; - vect_append(struct ast *, nodes, &n); + ast_vec_append(&nodes, n); return n; } static struct type *create_empty_type() { - if (vec_uninit(types)) { - types = vec_create(sizeof(struct type *)); + if (type_vec_uninit(&types)) { + types = type_vec_create(0); } struct type *n = calloc(1, sizeof(struct type)); @@ -98,7 +103,7 @@ static struct type *create_empty_type() /* just to be safe */ n->k = TYPE_VOID; n->size = -1; - vect_append(struct ast *, types, &n); + type_vec_append(&types, n); return n; } diff --git a/src/compiler.c b/src/compiler.c index d865ea5..6f24ee6 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -113,7 +113,7 @@ static int process(struct scope *scope, const char *file) #define MAP_TYPE struct scope * #define MAP_CMP(a, b) strcmp((a), (b)) #define MAP_NAME scopes -#include "ek/map.h" +#include /* ugly global for now */ static struct scopes scopes; diff --git a/src/lower.c b/src/lower.c index 8aefd68..c6e2f72 100644 --- a/src/lower.c +++ b/src/lower.c @@ -8,7 +8,6 @@ #include #include -#include #define UNUSED(x) (void)x @@ -24,26 +23,38 @@ struct retval { char *s; }; +#define VEC_NAME str_vec +#define VEC_TYPE char * +#include + +#define VEC_NAME ast_vec +#define VEC_TYPE struct ast * +#include + +#define VEC_NAME retval_vec +#define VEC_TYPE struct retval +#include + struct lower_state { - struct vec top; - struct vec bottom; - struct vec out; + struct str_vec top; + struct str_vec bottom; + struct str_vec out; int64_t uniq; - struct vec dealloc; + struct str_vec dealloc; size_t deallocs; - struct vec procs; + struct ast_vec procs; }; static struct lower_state create_state() { struct lower_state state; - state.top = vec_create(sizeof(char *)); - state.bottom = vec_create(sizeof(char *)); - state.out = vec_create(sizeof(char *)); - state.dealloc = vec_create(sizeof(char *)); - state.procs = vec_create(sizeof(struct ast *)); + state.top = str_vec_create(0); + state.bottom = str_vec_create(0); + state.out = str_vec_create(0); + state.dealloc = str_vec_create(0); + state.procs = ast_vec_create(0); state.deallocs = 0; state.uniq = 0; @@ -52,41 +63,41 @@ static struct lower_state create_state() static void destroy_state(struct lower_state *state) { - assert(vec_len(&state->top) == 0); - assert(vec_len(&state->bottom) == 0); - assert(vec_len(&state->out) == 0); - assert(vec_len(&state->dealloc) == 0); - - vec_destroy(&state->top); - vec_destroy(&state->bottom); - vec_destroy(&state->out); - vec_destroy(&state->dealloc); - vec_destroy(&state->procs); + assert(str_vec_len(&state->top) == 0); + assert(str_vec_len(&state->bottom) == 0); + assert(str_vec_len(&state->out) == 0); + assert(str_vec_len(&state->dealloc) == 0); + + str_vec_destroy(&state->top); + str_vec_destroy(&state->bottom); + str_vec_destroy(&state->out); + str_vec_destroy(&state->dealloc); + ast_vec_destroy(&state->procs); } static void add_proc(struct lower_state *s, struct ast *proc) { assert(proc->k == AST_PROC_DEF); - vec_append(&s->procs, &proc); + ast_vec_append(&s->procs, proc); } static void add_dealloc(struct lower_state *s, char *dealloc) { - vect_append(char *, s->dealloc, &dealloc); + str_vec_append(&s->dealloc, dealloc); } static void push_loop(struct lower_state *s, char *top, char *bottom, char *out) { - vect_append(char *, s->top, &top); - vect_append(char *, s->bottom, &bottom); - vect_append(char *, s->out, &out); + str_vec_append(&s->top, top); + str_vec_append(&s->bottom, bottom); + str_vec_append(&s->out, out); } static void pop_loop(struct lower_state *s) { - char *top = vect_pop(char *, s->top); - char *bottom = vect_pop(char *, s->bottom); - char *out = vect_pop(char *, s->out); + char *top = *str_vec_pop(&s->top); + char *bottom = *str_vec_pop(&s->bottom); + char *out = *str_vec_pop(&s->out); free(top); free(bottom); @@ -94,7 +105,7 @@ static void pop_loop(struct lower_state *s) } #define label_peek(v) \ - vect_back(char *, v) + (*str_vec_back(&(v))) static const char *retval_kind_str(enum retval_kind kind) { @@ -147,14 +158,13 @@ static struct retval build_retval(enum retval_kind kind, char *s) return (struct retval){.kind = kind, .s = s}; } -static void strvec_destroy(struct vec *v) +static void destroy_retval_vec(struct retval_vec *v) { - foreach_vec(vi, *v) { - char *s = vect_at(char *, *v, vi); - free(s); + foreach(retval_vec, r, v) { + free(r->s); } - vec_destroy(v); + retval_vec_destroy(v); } static __attribute__((format (printf, 1, 2))) @@ -258,7 +268,7 @@ static int lower_simple_param(struct lower_state *s, struct ast *p) struct struct_param_helper { char *name; - struct vec *fixups; + struct str_vec *fixups; }; static int collect_struct_param(struct lower_state *s, struct ast *n, @@ -273,13 +283,13 @@ static int collect_struct_param(struct lower_state *s, struct ast *n, printf("%s %s, ", type, pname); char *f = build_str("%s >> %s %s %zd;\n", pname, type, h->name, offset); - vect_append(char *, *h->fixups, &f); + str_vec_append(h->fixups, f); free(pname); return 0; } -static int lower_param(struct lower_state *s, struct ast *p, struct vec *fixups) +static int lower_param(struct lower_state *s, struct ast *p, struct str_vec *fixups) { UNUSED(s); assert(p->k == AST_VAR_DEF); @@ -303,7 +313,7 @@ static int lower_param(struct lower_state *s, struct ast *p, struct vec *fixups) /* alloc param struct */ size_t size = type_size(p->t); char *alloc = build_str("i27 %s = ^ %zd;\n", name, size); - vect_append(char *, *fixups, &alloc); + str_vec_append(fixups, alloc); struct struct_param_helper h = {name, fixups}; int ret = visit_struct(s, p->t->d, 0, @@ -313,7 +323,7 @@ static int lower_param(struct lower_state *s, struct ast *p, struct vec *fixups) } static int lower_params(struct lower_state *s, struct ast *params, - struct vec *fixups) + struct str_vec *fixups) { /** @todo fix structs, struct arguments must be stored to some * structures on the stack */ @@ -528,7 +538,7 @@ static int lower_id(struct lower_state *s, struct ast *id, struct struct_return_helper { char *name; - struct vec *locs; + struct str_vec *locs; }; static int lower_struct_return(struct lower_state *s, struct ast *n, size_t o, @@ -540,7 +550,7 @@ static int lower_struct_return(struct lower_state *s, struct ast *n, size_t o, char *type = size == 1 ? "i9" : "i27"; char *rname = build_str("%s_%zd", h->name, o); - vect_append(char *, *h->locs, &rname); + str_vec_append(h->locs, rname); printf("%s %s << %s %zd;\n", type, rname, h->name, o); return 0; } @@ -579,19 +589,19 @@ static int lower_return(struct lower_state *s, struct ast *r, } assert(r->t->k == TYPE_STRUCT); - struct vec locs = vec_create(sizeof(char *)); + struct str_vec locs = str_vec_create(0); struct struct_return_helper h = {name, &locs}; if (visit_struct(s, r->t->d, 0, (visit_struct_t)lower_struct_return, &h) < 0) return -1; printf("=> ("); - foreach_vec(li, locs) { - char *l = vect_at(char *, locs, li); - printf("%s, ", l); - free(l); + foreach(str_vec, l, &locs) { + printf("%s, ", *l); + free(*l); } - vec_destroy(&locs); + + str_vec_destroy(&locs); printf(");\n"); return 0; } @@ -773,7 +783,7 @@ static int lower_comparison(struct lower_state *s, struct ast *i, } static int lower_simple_arg(struct lower_state *s, struct ast *c, - struct vec *retval) + struct retval_vec *retval) { struct retval arg = retval_create(); if (lower_expr(s, c, &arg)) { @@ -781,13 +791,13 @@ static int lower_simple_arg(struct lower_state *s, struct ast *c, return -1; } - vec_append(retval, &arg); + retval_vec_append(retval, arg); return 0; } struct struct_arg_helper { char *name; - struct vec *args; + struct retval_vec *args; }; static int collect_struct_arg(struct lower_state *s, struct ast *n, @@ -801,12 +811,12 @@ static int collect_struct_arg(struct lower_state *s, struct ast *n, printf("%s %s << %s %zd;\n", type, tmp, h->name, offset); struct retval r = build_retval(size == 1 ? REG_I9 : REG_I27, tmp); - vect_append(struct retval, *h->args, &r); + retval_vec_append(h->args, r); return 0; } static int lower_struct_arg(struct lower_state *s, struct ast *c, - struct vec *args) + struct retval_vec *args) { struct retval arg = retval_create(); if (lower_expr(s, c, &arg)) { @@ -817,15 +827,16 @@ static int lower_struct_arg(struct lower_state *s, struct ast *c, struct ast *def = c->t->d; char *name = arg.s; struct struct_arg_helper h = {name, args}; - int ret = visit_struct(s, def, 0, (visit_struct_t)collect_struct_arg, - &h) < 0; + int ret = visit_struct(s, def, 0, + (visit_struct_t)collect_struct_arg, &h) < 0; + retval_destroy(&arg); return ret; } struct struct_retval_helper { char *rbuf; - struct vec *stores; + struct str_vec *stores; }; static int collect_struct_retval(struct lower_state *s, struct ast *n, @@ -840,7 +851,7 @@ static int collect_struct_retval(struct lower_state *s, struct ast *n, char *store = build_str("%s >> %s %s %zd;\n", tmp, type, h->rbuf, offset); - vect_append(char *, *h->stores, &store); + str_vec_append(h->stores, store); free(tmp); return 0; } @@ -849,23 +860,22 @@ static void lower_struct_retval(struct lower_state *s, struct type *rtype, char *rbuf, struct retval *retval) { struct ast *def = rtype->d; - struct vec stores = vec_create(sizeof(char *)); + struct str_vec stores = str_vec_create(0); printf("("); struct struct_retval_helper h = {rbuf, &stores}; if (visit_struct(s, def, 0, (visit_struct_t)collect_struct_retval, &h) < 0) { - vec_destroy(&stores); + str_vec_destroy(&stores); return; } printf(");\n"); - foreach_vec(si, stores) { - char *store = vect_at(char *, stores, si); - printf("%s", store); - free(store); + foreach(str_vec, store, &stores) { + printf("%s", *store); + free(*store); } - vec_destroy(&stores); + str_vec_destroy(&stores); *retval = build_retval(REG_I27, rbuf); } @@ -934,11 +944,11 @@ static int lower_call(struct lower_state *s, struct ast *c, } /* collect all args */ - struct vec args = vec_create(sizeof(struct retval)); + struct retval_vec args = retval_vec_create(0); foreach_node(a, call_args(c)) { if (a->t->k == TYPE_STRUCT) { if (lower_struct_arg(s, a, &args)) { - strvec_destroy(&args); + destroy_retval_vec(&args); return -1; } @@ -946,7 +956,7 @@ static int lower_call(struct lower_state *s, struct ast *c, } if (lower_simple_arg(s, a, &args)) { - strvec_destroy(&args); + destroy_retval_vec(&args); return -1; } } @@ -954,12 +964,11 @@ static int lower_call(struct lower_state *s, struct ast *c, printf("%s (", call.s); retval_destroy(&call); - foreach_vec(ai, args) { - struct retval r = vect_at(struct retval, args, ai); - printf("%s, ", r.s); - free(r.s); + foreach(retval_vec, r, &args) { + printf("%s, ", r->s); + free(r->s); } - vec_destroy(&args); + retval_vec_destroy(&args); printf(") => "); @@ -1191,7 +1200,7 @@ static int lower_block(struct lower_state *s, struct ast *block) * block, and deallocs_bottom where the parent block's dealloc stack * is. Stuff like continue and break will probably need this * information, which is why it's attached to lower_state */ - size_t deallocs_top = vec_len(&s->dealloc); + size_t deallocs_top = str_vec_len(&s->dealloc); size_t deallocs_parent = s->deallocs; s->deallocs = deallocs_top; @@ -1203,12 +1212,12 @@ static int lower_block(struct lower_state *s, struct ast *block) if (lower_deferred(s, block_defers(block))) return -1; - assert(deallocs_top <= vec_len(&s->dealloc)); + assert(deallocs_top <= str_vec_len(&s->dealloc)); s->deallocs = deallocs_parent; - while (vec_len(&s->dealloc) > deallocs_top) { + while (str_vec_len(&s->dealloc) > deallocs_top) { /* perform all deallocs that were queued within this block */ - char *dealloc = vect_pop(char *, s->dealloc); + char *dealloc = *str_vec_pop(&s->dealloc); /* slight hack, top block doesn't need to care about freeing * anything as it must return, this sidesteps the case where qbt @@ -1318,7 +1327,7 @@ static int lower_proc(struct ast *n) /* args */ printf("("); - struct vec fixups = vec_create(sizeof(char *)); + struct str_vec fixups = str_vec_create(0); if (lower_params(&state, proc_params(n), &fixups)) { destroy_state(&state); return -1; @@ -1330,13 +1339,12 @@ static int lower_proc(struct ast *n) /* body */ printf("{\n"); - foreach_vec(fi, fixups) { - char *f = vect_at(char *, fixups, fi); - printf("%s", f); - free(f); + foreach(str_vec, f, &fixups) { + printf("%s", *f); + free(*f); } - vec_destroy(&fixups); + str_vec_destroy(&fixups); if (lower_block(&state, proc_body(n))) { destroy_state(&state); @@ -1345,9 +1353,8 @@ static int lower_proc(struct ast *n) printf("}\n"); - foreach_vec(pi, state.procs) { - struct ast *proc = vect_at(struct ast*, state.procs, pi); - if (lower_proc(proc)) { + foreach(ast_vec, p, &state.procs) { + if (lower_proc(*p)) { destroy_state(&state); return -1; } diff --git a/src/vec.c b/src/vec.c deleted file mode 100644 index 89c5500..0000000 --- a/src/vec.c +++ /dev/null @@ -1,67 +0,0 @@ -/* SPDX-License-Identifier: copyleft-next-0.3.1 */ - -#include -#include -#include - -#include - -struct vec vec_create(size_t ns) -{ - return (struct vec) { - .n = 0, - .s = 1, - .ns = ns, - .buf = malloc(ns), - }; -} - -size_t vec_len(struct vec *v) -{ - return v->n; -} - -void *vec_at(struct vec *v, size_t i) -{ - assert(i < v->n && "out of vector bounds"); - return v->buf + i * v->ns; -} - -void *vec_back(struct vec *v) -{ - assert(v->n); - return v->buf + (v->n - 1) * v->ns; -} - -void *vec_pop(struct vec *v) -{ - assert(v->n && "attempting to pop empty vector"); - v->n--; - return v->buf + v->n * v->ns; -} - -void vec_append(struct vec *v, void *n) -{ - v->n++; - if (v->n >= v->s) { - v->s *= 2; - v->buf = realloc(v->buf, v->s * v->ns); - } - - void *p = vec_at(v, v->n - 1); - memcpy(p, n, v->ns); -} - -void vec_reset(struct vec *v) -{ - v->n = 0; -} - -void vec_destroy(struct vec *v) { - free(v->buf); -} - -void vec_sort(struct vec *v, vec_comp_t comp) -{ - qsort(v->buf, v->n, v->ns, comp); -} -- cgit v1.3