Skip to content

Commit

Permalink
WIP: Switch to LuaJIT
Browse files Browse the repository at this point in the history
  • Loading branch information
dwhinham committed Apr 10, 2023
1 parent 9b54b7f commit 856b844
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@ INIHHOME=$(realpath external/inih)

LUAHOME=$(realpath external/lua)
LUALIB=$(LUAHOME)/liblua.a

LUAJITHOME=$(realpath external/luajit)
LUAJITLIB=$(LUAJITHOME)/src/libluajit.a
4 changes: 2 additions & 2 deletions Kernel.mk
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ INCLUDE += -I $(FLUIDSYNTHBUILDDIR)/include \
-I $(FLUIDSYNTHHOME)/include
EXTRALIBS += $(FLUIDSYNTHLIB)

INCLUDE += -I $(LUAHOME)
EXTRALIBS += $(LUALIB)
INCLUDE += -I $(LUAJITHOME)/src
EXTRALIBS += $(LUAJITLIB)

#
# Generate version string from git tag
Expand Down
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,20 @@ $(LUAHOME)/liblua.a:
CFLAGS="$(CFLAGS_EXTERNAL) \$$(MYCFLAGS) -Wall -Werror -O3 -fno-stack-protector -fno-common" \
MYCFLAGS="\$$(LOCAL)"

luajit: $(LUAJITLIB)

$(LUAJITLIB):
@${APPLY_PATCH} $(LUAJITHOME) patches/luajit-2.1-circle.patch

$(MAKE) -C $(LUAJITHOME)/src \
CROSS="$(PREFIX)" \
XCFLAGS="-DLJ_TARGET_BAREMETAL -DLUAJIT_USE_SYSMALLOC=1 -DLUAJIT_DISABLE_PROFILE=1 -DLUAJIT_SECURITY_PRNG=0" \
libluajit.a

#
# Build kernel itself
#
all: circle-stdlib mt32emu fluidsynth lua
all: circle-stdlib mt32emu fluidsynth lua luajit
@$(MAKE) -f Kernel.mk $(KERNEL).img $(KERNEL).hex

#
Expand All @@ -169,6 +179,7 @@ mrproper: clean
@${REVERSE_PATCH} $(CIRCLEHOME) patches/circle-45-minimal-usb-drivers.patch
@${REVERSE_PATCH} $(FLUIDSYNTHHOME) patches/fluidsynth-2.3.1-circle.patch
@${REVERSE_PATCH} $(LUAHOME) patches/lua-5.4.4-circle.patch
@${REVERSE_PATCH} $(LUAJITHOME) patches/luajit-2.1-circle.patch

# Clean circle-stdlib
@if [ -f $(CIRCLE_STDLIB_CONFIG) ]; then $(MAKE) -C $(CIRCLESTDLIBHOME) mrproper; fi
Expand All @@ -182,3 +193,6 @@ mrproper: clean

# Clean Lua
@$(MAKE) -C $(LUAHOME) clean

# Clean LuaJIT
@$(MAKE) -C $(LUAJITHOME) clean
29 changes: 29 additions & 0 deletions patches/luajit-2.1-circle.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
diff --git a/src/lj_mcode.c b/src/lj_mcode.c
index 163aada4..c456da00 100644
--- a/src/lj_mcode.c
+++ b/src/lj_mcode.c
@@ -124,6 +124,24 @@ static int mcode_setprot(void *p, size_t sz, int prot)
return mprotect(p, sz, prot);
}

+#elif LJ_TARGET_BAREMETAL
+#define MCPROT_RW (1 << 0)
+#define MCPROT_RX (1 << 1)
+#define MCPROT_RWX (1 << 2)
+
+static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot)
+{
+ return NULL;
+}
+
+static void mcode_free(jit_State *J, void *p, size_t sz)
+{
+}
+
+static int mcode_setprot(void *p, size_t sz, int prot)
+{
+ return 0;
+}
#else

#error "Missing OS support for explicit placement of executable memory"
12 changes: 9 additions & 3 deletions src/mt32pi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,9 @@ void CMT32Pi::MainTask()
// Run scripting engine tasks
if (m_pLuaState)
{
if (lua_getglobal(m_pLuaState, "OnUpdate") != LUA_TFUNCTION)
lua_getglobal(m_pLuaState, "OnMIDISysExMessage");

if (!lua_isfunction(m_pLuaState, 1))
lua_pop(m_pLuaState, 1);
else
{
Expand Down Expand Up @@ -700,7 +702,9 @@ void CMT32Pi::OnShortMessage(u32 nMessage)
const int nStackSize = lua_gettop(m_pLuaState);

// Ensure function exists
if (lua_getglobal(m_pLuaState, "OnMIDIShortMessage") != LUA_TFUNCTION)
lua_getglobal(m_pLuaState, "OnMIDIShortMessage");

if (!lua_isfunction(m_pLuaState, 1))
lua_pop(m_pLuaState, 1);
else
{
Expand Down Expand Up @@ -756,7 +760,9 @@ void CMT32Pi::OnSysExMessage(const u8* pData, size_t nSize)
if (m_pLuaState)
{
// Ensure function exists
if (lua_getglobal(m_pLuaState, "OnMIDISysExMessage") != LUA_TFUNCTION)
lua_getglobal(m_pLuaState, "OnMIDISysExMessage");

if (!lua_isfunction(m_pLuaState, 1))
lua_pop(m_pLuaState, 1);
else
{
Expand Down

0 comments on commit 856b844

Please sign in to comment.