Skip to content

Commit 5f141d6

Browse files
committed
AArch64 JIT backend
Enable HashLink VM on AArch64 (Apple Silicon, ARM Linux servers, etc.) by adding a new JIT backend alongside the existing x86/x64 one. - Rename jit.c to jit_x86.c, extract shared code into jit_common.h/jit_shared.c - Add jit_aarch64.c/jit_aarch64_emit.c for ARM64 instruction selection and encoding - Add jit_elf.c for GDB JIT debug interface - Architecture-aware JIT selection in Makefile and CMakeLists.txt - Add aarch64 support in hl.h, profile.c, hlmodule.h, module.c
1 parent 7b9076f commit 5f141d6

File tree

16 files changed

+8867
-136
lines changed

16 files changed

+8867
-136
lines changed

CMakeLists.txt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ include(FindPkgConfig)
2020
include(CTest)
2121

2222
set(WITH_VM_DEFAULT ON)
23-
if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm|aarch64" AND (NOT CMAKE_OSX_ARCHITECTURES MATCHES "x86_64"))
24-
set(WITH_VM_DEFAULT OFF)
25-
endif()
23+
# VM now supports x86, x86-64, and AArch64 architectures
2624

2725
option(WITH_VM "Whether to build the Hashlink virtual machine" ${WITH_VM_DEFAULT})
2826
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
@@ -224,9 +222,24 @@ else()
224222
endif()
225223

226224
if (WITH_VM)
225+
# Select JIT backend based on architecture
226+
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
227+
set(JIT_SOURCES
228+
src/jit_aarch64.c
229+
src/jit_aarch64_emit.c
230+
src/jit_elf.c
231+
src/jit_shared.c
232+
)
233+
else()
234+
set(JIT_SOURCES
235+
src/jit_x86.c
236+
src/jit_shared.c
237+
)
238+
endif()
239+
227240
add_executable(hl
228241
src/code.c
229-
src/jit.c
242+
${JIT_SOURCES}
230243
src/main.c
231244
src/module.c
232245
src/debugger.c
@@ -246,6 +259,8 @@ if (WITH_VM)
246259

247260
if (WIN32)
248261
target_link_libraries(hl user32)
262+
else()
263+
target_link_libraries(hl dl) # For dlopen/dlsym in jit_elf.c
249264
endif()
250265
endif()
251266

Makefile

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,16 @@ STD = src/std/array.o src/std/buffer.o src/std/bytes.o src/std/cast.o src/std/da
4444
src/std/socket.o src/std/string.o src/std/sys.o src/std/types.o src/std/ucs2.o src/std/thread.o src/std/process.o \
4545
src/std/track.o
4646

47-
HL = src/code.o src/jit.o src/main.o src/module.o src/debugger.o src/profile.o
47+
# Conditional JIT backend selection based on architecture
48+
ifeq ($(ARCH),aarch64)
49+
HL_JIT = src/jit_aarch64.o src/jit_aarch64_emit.o src/jit_elf.o src/jit_shared.o
50+
else ifeq ($(ARCH),arm64)
51+
HL_JIT = src/jit_aarch64.o src/jit_aarch64_emit.o src/jit_elf.o src/jit_shared.o
52+
else
53+
HL_JIT = src/jit_x86.o src/jit_elf.o src/jit_shared.o
54+
endif
55+
56+
HL = src/code.o $(HL_JIT) src/main.o src/module.o src/debugger.o src/profile.o
4857

4958
FMT_INCLUDE = -I include/mikktspace -I include/minimp3
5059

@@ -200,8 +209,12 @@ LHL_LINK_FLAGS += -install_name @rpath/libhl.dylib
200209
else
201210

202211
# Linux
203-
CFLAGS += -m$(MARCH) -fPIC -pthread -fno-omit-frame-pointer $(shell pkg-config --cflags sdl2)
204-
LFLAGS += -lm -Wl,-rpath,.:'$$ORIGIN':$(INSTALL_LIB_DIR) -Wl,--no-undefined
212+
# -m32/-m64 is x86-specific; skip on aarch64
213+
ifneq ($(ARCH),aarch64)
214+
CFLAGS += -m$(MARCH)
215+
endif
216+
CFLAGS += -fPIC -pthread -fno-omit-frame-pointer $(shell pkg-config --cflags sdl2)
217+
LFLAGS += -lm -Wl,-rpath,.:'$$ORIGIN':$(INSTALL_LIB_DIR) -Wl,--export-dynamic -Wl,--no-undefined
205218

206219
ifeq ($(MARCH),32)
207220
CFLAGS += -I /usr/include/i386-linux-gnu -msse2 -mfpmath=sse
@@ -226,19 +239,12 @@ ifdef DEBUG
226239
CFLAGS += -g
227240
endif
228241

229-
all: libhl libs
230-
ifeq ($(ARCH),arm64)
231-
$(warning HashLink vm is not supported on arm64, skipping)
232-
else
233-
all: hl
234-
endif
242+
all: libhl libs hl
235243

236244
install:
237245
$(UNAME)==Darwin && ${MAKE} uninstall
238-
ifneq ($(ARCH),arm64)
239246
mkdir -p $(INSTALL_BIN_DIR)
240247
cp hl $(INSTALL_BIN_DIR)
241-
endif
242248
mkdir -p $(INSTALL_LIB_DIR)
243249
cp *.hdll $(INSTALL_LIB_DIR)
244250
cp libhl.${LIBEXT} $(INSTALL_LIB_DIR)
@@ -258,7 +264,7 @@ src/std/regexp.o: src/std/regexp.c
258264
${CC} ${CFLAGS} -o $@ -c $< ${PCRE_FLAGS}
259265

260266
libhl: ${LIB}
261-
${CC} ${CFLAGS} -o libhl.$(LIBEXT) -m${MARCH} ${LIBFLAGS} ${LHL_LINK_FLAGS} -shared $^ ${LIBHL_LDLIBS}
267+
${CC} ${CFLAGS} -o libhl.$(LIBEXT) ${LIBFLAGS} ${LHL_LINK_FLAGS} -shared $^ ${LIBHL_LDLIBS}
262268

263269
hlc: ${BOOT}
264270
${CC} ${CFLAGS} -o hlc ${BOOT} ${LFLAGS} ${EXTRA_LFLAGS} ${HLC_LDLIBS}

src/hl.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@
9393
# define HL_BSD
9494
#endif
9595

96-
#if defined(_64BITS) || defined(__x86_64__) || defined(_M_X64) || defined(__LP64__) || defined(__wasm64__)
96+
#if defined(_64BITS) || defined(__x86_64__) || defined(_M_X64) || defined(__LP64__) || defined(__wasm64__) || defined(__aarch64__)
9797
# define HL_64
9898
#endif
9999

100+
#if defined(__aarch64__) || defined(_M_ARM64)
101+
# define HL_ARM64
102+
#endif
103+
100104
#if defined(__GNUC__)
101105
# define HL_GCC
102106
#endif
@@ -880,6 +884,8 @@ typedef struct {
880884
int length;
881885
} vstring;
882886

887+
HL_API int hl_str_cmp( vstring *a, vstring *b );
888+
883889
#define DEFINE_PRIM(t,name,args) DEFINE_PRIM_WITH_NAME(t,name,args,name)
884890
#define _DEFINE_PRIM_WITH_NAME(t,name,args,realName) C_FUNCTION_BEGIN EXPORT void *hlp_##realName( const char **sign ) { *sign = _FUN(t,args); return (void*)(&HL_NAME(name)); } C_FUNCTION_END
885891

src/hlmodule.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ typedef struct {
115115
int *functions_indexes;
116116
} hl_code_hash;
117117

118+
struct jit_code_entry; /* Forward declaration for GDB JIT interface */
119+
118120
typedef struct {
119121
hl_code *code;
120122
int codesize;
@@ -127,6 +129,7 @@ typedef struct {
127129
hl_code_hash *hash;
128130
hl_debug_infos *jit_debug;
129131
jit_ctx *jit_ctx;
132+
struct jit_code_entry *gdb_jit_entry; /* GDB JIT interface registration (or NULL) */
130133
hl_module_context ctx;
131134
} hl_module;
132135

0 commit comments

Comments
 (0)