From 27d9c3a686b8effa089853910d37dc7f31d6a1fb Mon Sep 17 00:00:00 2001 From: zongor Date: Sun, 13 Jul 2025 10:56:02 -0400 Subject: [PATCH] break up into arch --- .../{src => }/client.zre | 0 .../{src => }/common.zre | 0 .../{src => }/server.zre | 0 src/Makefile | 42 +++++++++++-------- src/{ => arch/emscripten}/main.c | 25 +++-------- src/arch/linux/main.c | 17 ++++++++ src/debug.c | 6 +++ src/opcodes.h | 8 ++-- src/vm.c | 1 + 9 files changed, 58 insertions(+), 41 deletions(-) rename docs/project-syntax-example/{src => }/client.zre (100%) rename docs/project-syntax-example/{src => }/common.zre (100%) rename docs/project-syntax-example/{src => }/server.zre (100%) rename src/{ => arch/emscripten}/main.c (58%) create mode 100644 src/arch/linux/main.c diff --git a/docs/project-syntax-example/src/client.zre b/docs/project-syntax-example/client.zre similarity index 100% rename from docs/project-syntax-example/src/client.zre rename to docs/project-syntax-example/client.zre diff --git a/docs/project-syntax-example/src/common.zre b/docs/project-syntax-example/common.zre similarity index 100% rename from docs/project-syntax-example/src/common.zre rename to docs/project-syntax-example/common.zre diff --git a/docs/project-syntax-example/src/server.zre b/docs/project-syntax-example/server.zre similarity index 100% rename from docs/project-syntax-example/src/server.zre rename to docs/project-syntax-example/server.zre diff --git a/src/Makefile b/src/Makefile index 73e6392..d77da55 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,19 +2,22 @@ # ----------------------- # Native build (gcc) CC_NATIVE = gcc -CFLAGS_NATIVE = -g -std=c89 -Wall -Wextra -Werror -Wno-unused-parameter -LDFLAGS_NATIVE = -LDLIBS_NATIVE = +CFLAGS_NATIVE = -g -std=c89 -Wall -Wextra -Werror -Wno-unused-parameter -I. +LDFLAGS_NATIVE = +LDLIBS_NATIVE = # WASM build (emscripten) CC_WASM = emcc -CFLAGS_WASM = -g -std=c89 -Wall -Wextra -Werror -Wno-unused-parameter #-s WASM=1 -LDFLAGS_WASM = #-s WASM=1 -LDLIBS_WASM = +CFLAGS_WASM = -g -std=c89 -Wall -Wextra -Werror -Wno-unused-parameter -I. +LDFLAGS_WASM = -s WASM=1 -g -s USE_SDL=2 +LDLIBS_WASM = # Source and build configuration # ---------------------------- -SRC = $(wildcard *.c) +COMMON_SRC = $(wildcard *.c) +ARCH_SRC_EMSCRIPTEN = arch/emscripten/main.c +ARCH_SRC_LINUX = arch/linux/main.c + EXEC_NATIVE = zre EXEC_WASM = zre.wasm @@ -23,28 +26,31 @@ OBJ_DIR_NATIVE = build/native/obj OBJ_DIR_WASM = build/wasm/obj # Create output paths -OBJ_NATIVE = $(addprefix $(OBJ_DIR_NATIVE)/,$(notdir $(SRC:.c=.o))) -OBJ_WASM = $(addprefix $(OBJ_DIR_WASM)/,$(notdir $(SRC:.c=.o))) +OBJ_NATIVE = $(addprefix $(OBJ_DIR_NATIVE)/,$(notdir $(COMMON_SRC:.c=.o))) +OBJ_WASM = $(addprefix $(OBJ_DIR_WASM)/,$(notdir $(COMMON_SRC:.c=.o))) # Phony targets -# ------------- -.PHONY: all clean install wasm +.PHONY: all clean install wasm native emscripten linux macos -# Default target builds both versions -all: native +# Default target builds the native version +all: native # Native build rules # ------------------ -native: $(EXEC_NATIVE) +native: linux -$(EXEC_NATIVE): $(OBJ_NATIVE) +linux: $(EXEC_NATIVE) + +$(EXEC_NATIVE): $(OBJ_NATIVE) $(ARCH_SRC_LINUX) $(CC_NATIVE) $(LDFLAGS_NATIVE) $^ $(LDLIBS_NATIVE) -o $@ # WASM build rules # ---------------- -wasm: $(EXEC_WASM) +wasm: emscripten -$(EXEC_WASM): $(OBJ_WASM) +emscripten: $(EXEC_WASM) + +$(EXEC_WASM): $(OBJ_WASM) $(ARCH_SRC_EMSCRIPTEN) $(CC_WASM) $(LDFLAGS_WASM) $^ $(LDLIBS_WASM) -o $@ # Object file rules @@ -60,7 +66,7 @@ $(OBJ_DIR_WASM)/%.o: %.c # Clean build artifacts # --------------------- clean: - $(RM) -r $(OBJ_DIR_NATIVE) $(OBJ_DIR_WASM) $(EXEC_NATIVE) $(EXEC_WASM) + rm -rf $(OBJ_DIR_NATIVE) $(OBJ_DIR_WASM) $(EXEC_NATIVE) $(EXEC_WASM) # Install target (example) # ------------------------ diff --git a/src/main.c b/src/arch/emscripten/main.c similarity index 58% rename from src/main.c rename to src/arch/emscripten/main.c index d1e9cc9..b118d5f 100644 --- a/src/main.c +++ b/src/arch/emscripten/main.c @@ -1,20 +1,13 @@ -#include "test.h" -#include "debug.h" -#include "vm.h" -#ifdef __EMSCRIPTEN__ +#include "../../debug.h" +#include "../../test.h" +#include "../../vm.h" #include -#endif VM vm = {0}; void mainloop() { if (!step_vm(&vm)) { -#ifdef __EMSCRIPTEN__ emscripten_cancel_main_loop(); /* this should "kill" the app. */ -#else - core_dump(&vm); - exit(0); -#endif } } @@ -23,15 +16,9 @@ int main(int argc, char **argv) { vm.return_stack_size = STACK_SIZE; vm.stack_size = STACK_SIZE; vm.memory_size = MEMORY_SIZE; - test_loop_compile(vm.memory); - /* test_add_function_compile(vm.memory); */ - /* test_recursive_function_compile(vm.memory); */ -#ifdef __EMSCRIPTEN__ + test_add_function_compile(vm.memory); + /* test_recursive_function_compile(vm.memory); */ emscripten_set_main_loop(mainloop, 0, 1); -#else - while (1) { - mainloop(); - } -#endif + return 0; } diff --git a/src/arch/linux/main.c b/src/arch/linux/main.c new file mode 100644 index 0000000..78c527f --- /dev/null +++ b/src/arch/linux/main.c @@ -0,0 +1,17 @@ +#include "../../debug.h" +#include "../../test.h" +#include "../../vm.h" + +int main(int argc, char **argv) { + VM vm = {0}; + vm.frames_size = FRAMES_SIZE; + vm.return_stack_size = STACK_SIZE; + vm.stack_size = STACK_SIZE; + vm.memory_size = MEMORY_SIZE; + + test_loop_compile(vm.memory); + + while (step_vm(&vm)){} + core_dump(&vm); + return 0; +} diff --git a/src/debug.c b/src/debug.c index 778812f..c01c4dc 100644 --- a/src/debug.c +++ b/src/debug.c @@ -1,5 +1,8 @@ #include "debug.h" +/** + * Dumps the vm memory to a file. + */ int core_dump(VM *vm) { FILE *file = fopen("memory_dump.bin", "wb"); if (!file) { @@ -18,6 +21,9 @@ int core_dump(VM *vm) { return EXIT_SUCCESS; } +/** + * Print opcode. + */ void printOp(uint8_t op, uint8_t dest, uint8_t src1, uint8_t src2) { switch (op) { case OP_HALT: diff --git a/src/opcodes.h b/src/opcodes.h index c2d7a6b..0768d63 100644 --- a/src/opcodes.h +++ b/src/opcodes.h @@ -3,20 +3,20 @@ #include "common.h" -typedef union { +typedef union value_u { int32_t i; /* Integers */ float f; /* Float */ uint32_t u; /* Unsigned integers, also used for pointer address */ char c[4]; /* 4 Byte char array for string packing */ } Value; -typedef struct { +typedef struct slice_s { uint32_t start; uint32_t end; } Slice; #define MAX_REGS 32 -typedef struct { +typedef struct frame_s { Value registers[MAX_REGS]; /* R0-R31 */ uint32_t rp; /* register pointer (last unused) */ Slice allocated; /* start and end of global allocated block */ @@ -25,7 +25,7 @@ typedef struct { #define MEMORY_SIZE 65536 #define FRAMES_SIZE 128 #define STACK_SIZE 256 -typedef struct { +typedef struct vm_s { uint32_t pc; /* Program counter */ uint32_t fp; /* Frame pointer (last allocated value) */ uint32_t sp; /* stack pointer (top of stack) */ diff --git a/src/vm.c b/src/vm.c index 7543ec5..67097c9 100644 --- a/src/vm.c +++ b/src/vm.c @@ -2,6 +2,7 @@ #include "debug.h" #include +/* no inline fn in ANSI C :( */ #define COMPARE_AND_JUMP(type, accessor, op) \ do { \ type value = vm->frames[vm->fp].registers[src1].accessor; \