aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2025-05-01 13:51:36 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2025-05-01 13:51:36 +0300
commit5c11915931ad43617ba6f62189bb11630b9624d4 (patch)
tree2074c44eb54d115b04486c7243ada96a7381d7b5
parentce2f4acb9a66a5b9f473185af6c8d645e776f1fa (diff)
downloadejit-5c11915931ad43617ba6f62189bb11630b9624d4.tar.gz
ejit-5c11915931ad43617ba6f62189bb11630b9624d4.zip
use conts/vec instead of vendored versionHEADmaster
-rw-r--r--.gitmodules3
m---------deps/conts0
-rw-r--r--scripts/makefile2
-rw-r--r--src/common.h16
-rw-r--r--src/compile/compile.c67
-rw-r--r--src/ejit.c18
-rw-r--r--src/vec.h119
7 files changed, 55 insertions, 170 deletions
diff --git a/.gitmodules b/.gitmodules
index a514045..24f5f47 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,3 +1,6 @@
[submodule "deps/lightening"]
path = deps/lightening
url = https://gitlab.com/Kimplul/lightening.git
+[submodule "deps/conts"]
+ path = deps/conts
+ url = https://metanimi.dy.fi/cgit/conts
diff --git a/deps/conts b/deps/conts
new file mode 160000
+Subproject 5a4cb1b5a8ba258a23a62feab1e66cc7d0eba3b
diff --git a/scripts/makefile b/scripts/makefile
index dbb7a1c..6e3b972 100644
--- a/scripts/makefile
+++ b/scripts/makefile
@@ -45,7 +45,7 @@ WARNFLAGS := -Wall -Wextra
COMPILE_FLAGS := $(CFLAGS) $(WARNFLAGS) $(OPTFLAGS) $(LTOFLAGS) \
$(OBFLAGS) $(DEBUGFLAGS)
-INCLUDE_FLAGS := -I include
+INCLUDE_FLAGS := -I include -I deps/conts/include
COMPILE = $(COMPILER) \
$(COMPILE_FLAGS) $(DEPFLAGS) $(INCLUDE_FLAGS)
diff --git a/src/common.h b/src/common.h
index dc970f0..1eb762e 100644
--- a/src/common.h
+++ b/src/common.h
@@ -6,19 +6,19 @@
#define VEC_TYPE struct ejit_arg
#define VEC_NAME args
-#include "vec.h"
+#include <conts/vec.h>
#define VEC_TYPE int64_t
#define VEC_NAME gprs
-#include "vec.h"
+#include <conts/vec.h>
#define VEC_TYPE double
#define VEC_NAME fprs
-#include "vec.h"
+#include <conts/vec.h>
#define VEC_TYPE size_t
#define VEC_NAME labels
-#include "vec.h"
+#include <conts/vec.h>
enum ejit_opcode {
EJIT_OP_MOVI,
@@ -263,11 +263,11 @@ struct ejit_insn {
#define VEC_TYPE struct ejit_insn
#define VEC_NAME insns
-#include "vec.h"
+#include <conts/vec.h>
#define VEC_TYPE enum ejit_type
#define VEC_NAME types
-#include "vec.h"
+#include <conts/vec.h>
struct fpr_stat {
struct ejit_fpr f;
@@ -276,7 +276,7 @@ struct fpr_stat {
#define VEC_NAME fpr_stats
#define VEC_TYPE struct fpr_stat
-#include "vec.h"
+#include <conts/vec.h>
struct gpr_stat {
struct ejit_gpr r;
@@ -285,7 +285,7 @@ struct gpr_stat {
#define VEC_NAME gpr_stats
#define VEC_TYPE struct gpr_stat
-#include "vec.h"
+#include <conts/vec.h>
struct ejit_func {
struct types sign;
diff --git a/src/compile/compile.c b/src/compile/compile.c
index 5432bc1..f552ae3 100644
--- a/src/compile/compile.c
+++ b/src/compile/compile.c
@@ -4,7 +4,7 @@
#define VEC_TYPE jit_operand_t
#define VEC_NAME operands
-#include "../vec.h"
+#include <conts/vec.h>
struct reloc_helper {
jit_reloc_t r;
@@ -13,11 +13,11 @@ struct reloc_helper {
#define VEC_TYPE struct reloc_helper
#define VEC_NAME relocs
-#include "../vec.h"
+#include <conts/vec.h>
#define VEC_TYPE jit_addr_t
#define VEC_NAME addrs
-#include "../vec.h"
+#include <conts/vec.h>
/* skip assertions since we know they must be valid due to type checking earlier */
static long checked_run_i(struct ejit_func *f, size_t argc, struct ejit_arg args[argc])
@@ -1880,13 +1880,11 @@ static jit_off_t type_offset(struct ejit_insn i)
static void fixup_operands(struct operands *operands, size_t fixup)
{
- foreach_vec(i, *operands) {
- jit_operand_t op = *operands_at(operands, i);
- if (op.kind != JIT_OPERAND_KIND_MEM)
+ foreach(operands, op, operands) {
+ if (op->kind != JIT_OPERAND_KIND_MEM)
continue;
- op.loc.mem.offset += fixup;
- *operands_at(operands, i) = op;
+ op->loc.mem.offset += fixup;
}
}
@@ -1922,17 +1920,22 @@ static void compile_trampoline(struct ejit_func *f, jit_state_t *j)
struct operands args = operands_create(0);
- foreach_vec(ii, f->insns) {
- struct ejit_insn i = *insns_at(&f->insns, ii);
- switch (i.op) {
+ foreach(insns, i, &f->insns) {
+ switch (i->op) {
case EJIT_OP_PARAM: {
- jit_operand_t p = jit_operand_mem(jit_abi_from(i.r1), JIT_R1, arg_offset(i));
+ jit_operand_t p = jit_operand_mem(
+ jit_abi_from(i->r1),
+ JIT_R1, arg_offset(*i));
+
operands_append(&args, p);
break;
}
case EJIT_OP_PARAM_F: {
- jit_operand_t p = jit_operand_mem(jit_abi_from(i.r1), JIT_R1, arg_offset(i));
+ jit_operand_t p = jit_operand_mem(
+ jit_abi_from(i->r1),
+ JIT_R1, arg_offset(*i));
+
operands_append(&args, p);
break;
}
@@ -1975,13 +1978,13 @@ static void resolve_top_reloc(jit_state_t *j, struct relocs *relocs, struct addr
static void resolve_relocs(jit_state_t *j, struct relocs *relocs, struct addrs *addrs, size_t ii)
{
- foreach_vec(ri, *relocs) {
- struct reloc_helper h = *relocs_at(relocs, ri);
- if (h.to != ii)
+ for (size_t ri = 0; ri < relocs_len(relocs); ++ri) {
+ struct reloc_helper *h = relocs_at(relocs, ri);
+ if (h->to != ii)
continue;
jit_addr_t a = *addrs_at(addrs, ii);
- jit_reloc_t r = h.r;
+ jit_reloc_t r = h->r;
assert(a);
jit_patch_there(j, r, a);
@@ -2052,16 +2055,16 @@ static size_t compile_fn_body(struct ejit_func *f, jit_state_t *j, void *arena,
size_t frame = jit_enter_jit_abi(j, gprs, fprs, 0);
size_t stack = jit_align_stack(j, stack_size(f));
- struct operands src = operands_create();
- struct operands dst = operands_create();
- struct operands direct = operands_create();
+ struct operands src = operands_create(f->max_args);
+ struct operands dst = operands_create(f->max_args);
+ struct operands direct = operands_create(f->max_args);
- struct relocs relocs = relocs_create();
- struct addrs addrs = addrs_create();
+ struct relocs relocs = relocs_create(labels_len(&f->labels));
+ struct addrs addrs = addrs_create(0);
addrs_reserve(&addrs, insns_len(&f->insns));
size_t label = 0;
- foreach_vec(ii, f->insns) {
+ for (size_t ii = 0; ii < insns_len(&f->insns); ++ii) {
/* if we've hit a label, add it to our vector of label addresses */
if (label < labels_len(&f->labels)) {
if (*labels_at(&f->labels, label) == ii) {
@@ -2530,7 +2533,7 @@ static size_t compile_fn_body(struct ejit_func *f, jit_state_t *j, void *arena,
/* now move args into place */
jit_operand_t args[2] = {};
- foreach_vec(oi, direct) {
+ for (size_t oi = 0; oi < operands_len(&direct); ++oi) {
args[oi] = *operands_at(&direct, oi);
}
@@ -2843,7 +2846,7 @@ struct alive_slot {
#define VEC_NAME alive
#define VEC_TYPE struct alive_slot
-#include "../vec.h"
+#include <conts/vec.h>
static int spill_cost_sort(struct alive_slot *a, struct alive_slot *b)
{
@@ -2871,7 +2874,7 @@ static void calculate_alive(struct alive *alive, size_t idx,
long max_cost_idx = -1;
size_t max_cost = 0;
long counter = 0;
- foreach_vec(ai, *alive) {
+ for (size_t ai = 0; ai < alive_len(alive); ++ai) {
/* skip oneshot */
if (ai == 0)
goto next;
@@ -2916,7 +2919,7 @@ static int gpr_dead(void *regs, size_t idx, size_t start)
static void linear_gpr_alloc(struct ejit_func *f)
{
- foreach_vec(gi, f->gpr) {
+ for (size_t gi = 0; gi < gpr_stats_len(&f->gpr); ++gi) {
gpr_stats_at(&f->gpr, gi)->rno = gi;
}
}
@@ -2934,7 +2937,7 @@ static void assign_gprs(struct ejit_func *f)
struct alive_slot a = {.r = -1, .cost = 0, .idx = 0};
alive_append(&alive, a);
- foreach_vec(gi, f->gpr) {
+ for (size_t gi = 0; gi < gpr_stats_len(&f->gpr); ++gi) {
struct gpr_stat *gpr = gpr_stats_at(&f->gpr, gi);
calculate_alive(&alive, gi,
gpr->prio, gpr->start, gpr->end, &gpr->rno,
@@ -2943,7 +2946,7 @@ static void assign_gprs(struct ejit_func *f)
/* sort so that the highest spill cost register classes are at the front and
* as such more likely to be placed in registers */
- alive_sort(&alive, (vec_comp_t)spill_cost_sort);
+ alive_sort(&alive, (alive_comp_t)spill_cost_sort);
/* update remapping info */
for(size_t i = 0; i < alive_len(&alive); ++i) {
@@ -2969,7 +2972,7 @@ static int fpr_dead(void *regs, size_t idx, size_t start)
static void linear_fpr_alloc(struct ejit_func *f)
{
- foreach_vec(fi, f->fpr) {
+ for (size_t fi = 0; fi < fpr_stats_len(&f->fpr); ++fi) {
fpr_stats_at(&f->fpr, fi)->fno = fi;
}
}
@@ -2986,7 +2989,7 @@ static void assign_fprs(struct ejit_func *f)
struct alive_slot a = {.r = -1, .cost = 0, .idx = 0};
alive_append(&alive, a);
- foreach_vec(fi, f->fpr) {
+ for (size_t fi = 0; fi < fpr_stats_len(&f->fpr); ++fi) {
struct fpr_stat *fpr = fpr_stats_at(&f->fpr, fi);
calculate_alive(&alive, fi,
fpr->prio, fpr->start, fpr->end, &fpr->fno,
@@ -2995,7 +2998,7 @@ static void assign_fprs(struct ejit_func *f)
/* sort so that the highest spill cost register classes are at the front and
* as such more likely to be placed in registers */
- alive_sort(&alive, (vec_comp_t)spill_cost_sort);
+ alive_sort(&alive, (alive_comp_t)spill_cost_sort);
/* update remapping info */
for(size_t i = 0; i < alive_len(&alive); ++i) {
diff --git a/src/ejit.c b/src/ejit.c
index 6a0ffb0..2341890 100644
--- a/src/ejit.c
+++ b/src/ejit.c
@@ -338,11 +338,11 @@ struct ejit_func *ejit_create_func(enum ejit_type rtype, size_t argc,
f->rtype = rtype;
- f->sign = types_create();
- f->insns = insns_create();
- f->labels = labels_create();
- f->gpr = gpr_stats_create();
- f->fpr = fpr_stats_create();
+ f->sign = types_create(0);
+ f->insns = insns_create(0);
+ f->labels = labels_create(0);
+ f->gpr = gpr_stats_create(0);
+ f->fpr = fpr_stats_create(0);
f->arena = NULL;
f->direct_call = NULL;
f->extern_call = NULL;
@@ -416,12 +416,10 @@ void ejit_select_compile_func(struct ejit_func *f, size_t gpr, size_t fpr,
/* just get labels, don't actually run anything yet */
ejit_run(f, 0, NULL, &labels);
- foreach_vec(ii, f->insns) {
- struct ejit_insn i = *insns_at(&f->insns, ii);
- void *addr = labels[i.op];
+ foreach(insns, i, &f->insns) {
+ void *addr = labels[i->op];
assert(addr);
- i.addr = addr;
- *insns_at(&f->insns, ii) = i;
+ i->addr = addr;
}
/* doesn't really matter what we put here as long as it isn't 0 */
diff --git a/src/vec.h b/src/vec.h
deleted file mode 100644
index f5a6fd9..0000000
--- a/src/vec.h
+++ /dev/null
@@ -1,119 +0,0 @@
-#ifndef VEC_TYPE
-#error "Need vector type"
-#endif
-
-#ifndef VEC_NAME
-#error "Need vector name"
-#endif
-
-#include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#define UNDERSCORE2(a, b) a##_##b
-#define UNDERSCORE(a, b) UNDERSCORE2(a, b)
-#define VEC(n) UNDERSCORE(VEC_NAME, n)
-
-
-#define VEC_STRUCT VEC_NAME
-struct VEC_STRUCT {
- size_t n;
- size_t s;
- VEC_TYPE *buf;
-};
-
-#ifndef VEC_H
-#define VEC_H
-
-#define foreach_vec(iter, v) \
- for (size_t iter = 0; iter < (v).n; ++iter)
-
-#define vec_uninit(v) \
- (v.buf == NULL)
-#endif
-
-static inline struct VEC_STRUCT VEC(create)()
-{
- const size_t s = 8;
- return (struct VEC_STRUCT) {
- .n = 0,
- .s = s,
- .buf = malloc(s * sizeof(VEC_TYPE)),
- };
-}
-
-static inline size_t VEC(len)(struct VEC_STRUCT *v)
-{
- return v->n;
-}
-
-static inline VEC_TYPE *VEC(at)(struct VEC_STRUCT *v, size_t i)
-{
- assert(i < v->n && "out of vector bounds");
- return &v->buf[i];
-}
-
-static inline VEC_TYPE *VEC(back)(struct VEC_STRUCT *v)
-{
- assert(v->n);
- return &v->buf[v->n - 1];
-}
-
-static inline VEC_TYPE *VEC(pop)(struct VEC_STRUCT *v)
-{
- assert(v->n && "attempting to pop empty vector");
- v->n--;
- return &v->buf[v->n];
-}
-
-static inline void VEC(append)(struct VEC_STRUCT *v, VEC_TYPE n)
-{
- v->n++;
- if (v->n >= v->s) {
- v->s *= 2;
- v->buf = realloc(v->buf, v->s * sizeof(VEC_TYPE));
- assert(v->buf);
- }
-
- v->buf[v->n - 1] = n;
-}
-
-static inline void VEC(reset)(struct VEC_STRUCT *v)
-{
- v->n = 0;
-}
-
-static inline void VEC(destroy)(struct VEC_STRUCT *v) {
- free(v->buf);
-}
-
-typedef int (*vec_comp_t)(void *a, void *b);
-static inline void VEC(sort)(struct VEC_STRUCT *v, vec_comp_t comp)
-{
- qsort(v->buf, v->n, sizeof(VEC_TYPE), (__compar_fn_t)comp);
-}
-
-static inline void VEC(reserve)(struct VEC_STRUCT *v, size_t n)
-{
- if (v->n >= n)
- return;
-
- v->n = n;
- if (v->s >= v->n)
- return;
-
- while (v->s < v->n)
- v->s *= 2;
-
- v->buf = realloc(v->buf, v->s * sizeof(VEC_TYPE));
-}
-
-static inline void VEC(shrink)(struct VEC_STRUCT *v, size_t n)
-{
- /* assert(v->n >= n); */
- v->n = n;
-}
-
-#undef VEC_TYPE
-#undef VEC_NAME