break up into arch

This commit is contained in:
zongor 2025-07-13 10:56:02 -04:00
parent 66adb19578
commit 27d9c3a686
9 changed files with 58 additions and 41 deletions

View File

@ -2,19 +2,22 @@
# -----------------------
# Native build (gcc)
CC_NATIVE = gcc
CFLAGS_NATIVE = -g -std=c89 -Wall -Wextra -Werror -Wno-unused-parameter
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
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
# 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)
# ------------------------

View File

@ -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 <emscripten.h>
#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;
}

17
src/arch/linux/main.c Normal file
View File

@ -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;
}

View File

@ -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:

View File

@ -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) */

View File

@ -2,6 +2,7 @@
#include "debug.h"
#include <string.h>
/* no inline fn in ANSI C :( */
#define COMPARE_AND_JUMP(type, accessor, op) \
do { \
type value = vm->frames[vm->fp].registers[src1].accessor; \