aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2024-06-24 20:33:56 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2024-06-24 20:33:56 +0300
commitfe3d9c7dfbe4190ecf0919abe474e4da4c019566 (patch)
tree91dd12b6a9980e5945e7dbde761afadd9d118d09
parent5a4da8bac457f0e70e690a572eba9cf754e69a37 (diff)
downloadejit-fe3d9c7dfbe4190ecf0919abe474e4da4c019566.tar.gz
ejit-fe3d9c7dfbe4190ecf0919abe474e4da4c019566.zip
add initial compiler framework
m---------deps/lightening0
-rw-r--r--scripts/makefile5
-rwxr-xr-xscripts/select-compile16
-rw-r--r--src/common.h2
-rw-r--r--src/compile/compile.c8
-rw-r--r--src/compile/nocompile.c7
-rw-r--r--src/ejit.c25
-rw-r--r--src/interp.c12
-rw-r--r--src/source.mk5
9 files changed, 60 insertions, 20 deletions
diff --git a/deps/lightening b/deps/lightening
-Subproject 4f1aa83d2cb0cc882790ea578828efe72df5ff1
+Subproject cd0824122864613ae58c07393de599ff9460c35
diff --git a/scripts/makefile b/scripts/makefile
index 484d04f..b8978f6 100644
--- a/scripts/makefile
+++ b/scripts/makefile
@@ -9,11 +9,6 @@ DEBUGFLAGS != [ "$(DEBUG)" != "0" ] \
&& echo "-DDEBUG=1" \
|| echo "-DNDEBUG=1"
-ASSERT ?= 1
-ASSERTFLAGS != [ "$(ASSERT)" != "0" ] \
- && echo "-DASSERT=1" \
- || echo
-
DEPFLAGS = -MT $@ -MMD -MP -MF $@.d
LINTFLAGS := -fsyntax-only
PREPROCESS := -E
diff --git a/scripts/select-compile b/scripts/select-compile
new file mode 100755
index 0000000..be8b40d
--- /dev/null
+++ b/scripts/select-compile
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+ARCH="$1"
+if [ -z "$ARCH" ]; then
+ ARCH=$(uname -m)
+fi
+
+>&2 echo $ARCH
+
+JIT="src/compile/compile.c"
+NOJIT="src/compile/nocompile.c"
+
+case "$ARCH" in
+ x86_64) echo "$JIT" ;;
+ *) echo "$NOJIT" ;;
+esac
diff --git a/src/common.h b/src/common.h
index 21e881b..e93e920 100644
--- a/src/common.h
+++ b/src/common.h
@@ -123,4 +123,6 @@ union interp_ret {
union interp_ret ejit_interp(struct ejit_func *f, size_t argc, struct ejit_arg args[argc], bool run, void ***labels_wb);
+bool ejit_compile(struct ejit_func *f);
+
#endif /* EJIT_COMMON_H */
diff --git a/src/compile/compile.c b/src/compile/compile.c
new file mode 100644
index 0000000..513e0e1
--- /dev/null
+++ b/src/compile/compile.c
@@ -0,0 +1,8 @@
+#include <ejit/ejit.h>
+#include "../../deps/lightening/lightening/lightening.c"
+#include "../common.h"
+
+bool ejit_compile(struct ejit_func *f)
+{
+ return false;
+}
diff --git a/src/compile/nocompile.c b/src/compile/nocompile.c
new file mode 100644
index 0000000..065d466
--- /dev/null
+++ b/src/compile/nocompile.c
@@ -0,0 +1,7 @@
+#include <ejit/ejit.h>
+
+bool ejit_compile(struct ejit_func *f)
+{
+ (void)(f);
+ return false;
+}
diff --git a/src/ejit.c b/src/ejit.c
index bd5a652..7fe2095 100644
--- a/src/ejit.c
+++ b/src/ejit.c
@@ -57,7 +57,10 @@ void ejit_compile_func(struct ejit_func *f, size_t gpr, size_t fpr)
{
f->gpr = gpr;
f->fpr = fpr;
- /** @todo try to implement JIT */
+
+ if (ejit_compile(f))
+ return;
+
/* otherwise, convert opcodes to address labels */
void **labels;
@@ -188,3 +191,23 @@ struct ejit_reloc ejit_bltr(struct ejit_func *s, struct ejit_gpr r0, struct ejit
emit_insn_i(s, BLTR, r0.r, r1.r, 0);
return (struct ejit_reloc){.insn = label.addr};
}
+
+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_interp(f, argc, args, true, NULL).r;
+}
+
+double ejit_run_func_f(struct ejit_func *f, size_t argc, struct ejit_arg args[argc])
+{
+ 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_interp(f, argc, args, true, NULL).d;
+}
diff --git a/src/interp.c b/src/interp.c
index 4678aa1..bbf461b 100644
--- a/src/interp.c
+++ b/src/interp.c
@@ -176,15 +176,3 @@ out_float:
free(fpr);
return (union interp_ret){.d = retval_f};
}
-
-long ejit_run_func(struct ejit_func *f, size_t argc, struct ejit_arg args[argc])
-{
- assert(f->rtype == EJIT_VOID || ejit_int_type(f->rtype));
- return ejit_interp(f, argc, args, true, NULL).r;
-}
-
-double ejit_run_func_f(struct ejit_func *f, size_t argc, struct ejit_arg args[argc])
-{
- assert(ejit_float_type(f->rtype));
- return ejit_interp(f, argc, args, true, NULL).d;
-}
diff --git a/src/source.mk b/src/source.mk
index 907befa..870550d 100644
--- a/src/source.mk
+++ b/src/source.mk
@@ -1,2 +1,3 @@
-LOCAL_SOURCES != echo src/*.c
-EJIT_SOURCES := $(EJIT_SOURCES) $(LOCAL_SOURCES)
+SRCS != echo src/*.c
+COMPILE_SOURCE != ./scripts/select-compile $(ARCH)
+EJIT_SOURCES := $(EJIT_SOURCES) $(SRCS) $(COMPILE_SOURCE)