From fe3d9c7dfbe4190ecf0919abe474e4da4c019566 Mon Sep 17 00:00:00 2001
From: Kimplul <kimi.h.kuparinen@gmail.com>
Date: Mon, 24 Jun 2024 20:33:56 +0300
Subject: add initial compiler framework

---
 deps/lightening         |  2 +-
 scripts/makefile        |  5 -----
 scripts/select-compile  | 16 ++++++++++++++++
 src/common.h            |  2 ++
 src/compile/compile.c   |  8 ++++++++
 src/compile/nocompile.c |  7 +++++++
 src/ejit.c              | 25 ++++++++++++++++++++++++-
 src/interp.c            | 12 ------------
 src/source.mk           |  5 +++--
 9 files changed, 61 insertions(+), 21 deletions(-)
 create mode 100755 scripts/select-compile
 create mode 100644 src/compile/compile.c
 create mode 100644 src/compile/nocompile.c

diff --git a/deps/lightening b/deps/lightening
index 4f1aa83..cd08241 160000
--- a/deps/lightening
+++ b/deps/lightening
@@ -1 +1 @@
-Subproject commit 4f1aa83d2cb0cc882790ea578828efe72df5ff1a
+Subproject commit cd0824122864613ae58c07393de599ff9460c35e
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)
-- 
cgit v1.2.3