aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/actualize.c42
-rw-r--r--src/ast.c39
-rw-r--r--src/compiler.c2
-rw-r--r--src/lower.c175
-rw-r--r--src/vec.c67
5 files changed, 134 insertions, 191 deletions
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 <ek/actualize.h>
#include <ek/compiler.h>
#include <ek/debug.h>
-#include <ek/vec.h>
#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 <conts/vec.h>
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 <math.h>
#include <ek/ast.h>
-#include <ek/vec.h>
#include <ek/scope.h>
-static struct vec nodes = {0};
-static struct vec types = {0};
+#define VEC_NAME ast_vec
+#define VEC_TYPE struct ast *
+#include <conts/vec.h>
+
+#define VEC_NAME type_vec
+#define VEC_TYPE struct type *
+#include <conts/vec.h>
+
+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 <conts/map.h>
/* 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 <ek/lower.h>
#include <ek/scope.h>
-#include <ek/vec.h>
#define UNUSED(x) (void)x
@@ -24,26 +23,38 @@ struct retval {
char *s;
};
+#define VEC_NAME str_vec
+#define VEC_TYPE char *
+#include <conts/vec.h>
+
+#define VEC_NAME ast_vec
+#define VEC_TYPE struct ast *
+#include <conts/vec.h>
+
+#define VEC_NAME retval_vec
+#define VEC_TYPE struct retval
+#include <conts/vec.h>
+
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);
+ 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);
- vec_destroy(&state->top);
- vec_destroy(&state->bottom);
- vec_destroy(&state->out);
- vec_destroy(&state->dealloc);
- vec_destroy(&state->procs);
+ 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 <stdlib.h>
-#include <assert.h>
-#include <string.h>
-
-#include <ek/vec.h>
-
-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);
-}