aboutsummaryrefslogtreecommitdiff
path: root/src/ejit.c
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2024-06-25 20:44:20 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2024-06-25 20:44:20 +0300
commit8f44960246c39845df83464ea9b4bc59220265aa (patch)
tree6965b61ec7c193378bc38386450e05beea145048 /src/ejit.c
parentfe3d9c7dfbe4190ecf0919abe474e4da4c019566 (diff)
downloadejit-8f44960246c39845df83464ea9b4bc59220265aa.tar.gz
ejit-8f44960246c39845df83464ea9b4bc59220265aa.zip
example compiles under jit
Diffstat (limited to 'src/ejit.c')
-rw-r--r--src/ejit.c20
1 files changed, 11 insertions, 9 deletions
diff --git a/src/ejit.c b/src/ejit.c
index 7fe2095..69ade9a 100644
--- a/src/ejit.c
+++ b/src/ejit.c
@@ -40,16 +40,15 @@ struct ejit_func *ejit_create_func(enum ejit_type rtype, size_t argc, const stru
f->arena = NULL;
f->size = 0;
- emit_insn_i(f, START, 0, 0, 0);
-
for (size_t i = 0; i < argc; ++i) {
switch (args[i].kind) {
- case EJIT_OPERAND_GPR: emit_insn_i(f, PARAM, args[i].r, 0, i); break;
- case EJIT_OPERAND_FPR: emit_insn_i(f, PARAM_F, args[i].r, 0, i); break;
+ case EJIT_OPERAND_GPR: emit_insn_i(f, PARAM, args[i].r, 0, args[i].type); break;
+ case EJIT_OPERAND_FPR: emit_insn_i(f, PARAM_F, args[i].r, 0, args[i].type); break;
default: abort();
}
}
+ emit_insn_i(f, START, 0, 0, 0);
return f;
}
@@ -58,6 +57,7 @@ void ejit_compile_func(struct ejit_func *f, size_t gpr, size_t fpr)
f->gpr = gpr;
f->fpr = fpr;
+ /* try to jit compile if possible */
if (ejit_compile(f))
return;
@@ -84,7 +84,9 @@ void ejit_destroy_func(struct ejit_func *f)
struct ejit_label ejit_label(struct ejit_func *f)
{
- return (struct ejit_label){.addr = vec_len(&f->insns)};
+ size_t addr = vec_len(&f->insns);
+ emit_insn_r(f, LABEL, 0, 0, 0);
+ return (struct ejit_label){.addr = addr};
}
void ejit_patch(struct ejit_func *f, struct ejit_reloc r, struct ejit_label l)
@@ -187,9 +189,9 @@ void ejit_movi(struct ejit_func *s, struct ejit_gpr r0, long o)
struct ejit_reloc ejit_bltr(struct ejit_func *s, struct ejit_gpr r0, struct ejit_gpr r1)
{
- struct ejit_label label = ejit_label(s);
+ size_t addr = vec_len(&s->insns);
emit_insn_i(s, BLTR, r0.r, r1.r, 0);
- return (struct ejit_reloc){.insn = label.addr};
+ return (struct ejit_reloc){.insn = addr};
}
long ejit_run_func(struct ejit_func *f, size_t argc, struct ejit_arg args[argc])
@@ -197,7 +199,7 @@ long ejit_run_func(struct ejit_func *f, size_t argc, struct ejit_arg args[argc])
assert(f->gpr && "trying to run a function that hasn't been compiled");
assert(f->rtype == EJIT_VOID || ejit_int_type(f->rtype));
if (f->arena)
- return ((ejit_escape_f_t)f)(argc, args);
+ return ((ejit_escape_t)f->arena)(argc, args);
return ejit_interp(f, argc, args, true, NULL).r;
}
@@ -207,7 +209,7 @@ double ejit_run_func_f(struct ejit_func *f, size_t argc, struct ejit_arg args[ar
assert(f->fpr && "trying to run a function that hasn't been compiled");
assert(ejit_float_type(f->rtype));
if (f->arena)
- return ((ejit_escape_f_t)f)(argc, args);
+ return ((ejit_escape_f_t)f->arena)(argc, args);
return ejit_interp(f, argc, args, true, NULL).d;
}