break up into arch
This commit is contained in:
parent
66adb19578
commit
27d9c3a686
34
src/Makefile
34
src/Makefile
|
@ -2,19 +2,22 @@
|
||||||
# -----------------------
|
# -----------------------
|
||||||
# Native build (gcc)
|
# Native build (gcc)
|
||||||
CC_NATIVE = 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 =
|
LDFLAGS_NATIVE =
|
||||||
LDLIBS_NATIVE =
|
LDLIBS_NATIVE =
|
||||||
|
|
||||||
# WASM build (emscripten)
|
# WASM build (emscripten)
|
||||||
CC_WASM = emcc
|
CC_WASM = emcc
|
||||||
CFLAGS_WASM = -g -std=c89 -Wall -Wextra -Werror -Wno-unused-parameter #-s WASM=1
|
CFLAGS_WASM = -g -std=c89 -Wall -Wextra -Werror -Wno-unused-parameter -I.
|
||||||
LDFLAGS_WASM = #-s WASM=1
|
LDFLAGS_WASM = -s WASM=1 -g -s USE_SDL=2
|
||||||
LDLIBS_WASM =
|
LDLIBS_WASM =
|
||||||
|
|
||||||
# Source and build configuration
|
# 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_NATIVE = zre
|
||||||
EXEC_WASM = zre.wasm
|
EXEC_WASM = zre.wasm
|
||||||
|
|
||||||
|
@ -23,28 +26,31 @@ OBJ_DIR_NATIVE = build/native/obj
|
||||||
OBJ_DIR_WASM = build/wasm/obj
|
OBJ_DIR_WASM = build/wasm/obj
|
||||||
|
|
||||||
# Create output paths
|
# Create output paths
|
||||||
OBJ_NATIVE = $(addprefix $(OBJ_DIR_NATIVE)/,$(notdir $(SRC:.c=.o)))
|
OBJ_NATIVE = $(addprefix $(OBJ_DIR_NATIVE)/,$(notdir $(COMMON_SRC:.c=.o)))
|
||||||
OBJ_WASM = $(addprefix $(OBJ_DIR_WASM)/,$(notdir $(SRC:.c=.o)))
|
OBJ_WASM = $(addprefix $(OBJ_DIR_WASM)/,$(notdir $(COMMON_SRC:.c=.o)))
|
||||||
|
|
||||||
# Phony targets
|
# Phony targets
|
||||||
# -------------
|
.PHONY: all clean install wasm native emscripten linux macos
|
||||||
.PHONY: all clean install wasm
|
|
||||||
|
|
||||||
# Default target builds both versions
|
# Default target builds the native version
|
||||||
all: native
|
all: native
|
||||||
|
|
||||||
# Native build rules
|
# 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 $@
|
$(CC_NATIVE) $(LDFLAGS_NATIVE) $^ $(LDLIBS_NATIVE) -o $@
|
||||||
|
|
||||||
# WASM build rules
|
# 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 $@
|
$(CC_WASM) $(LDFLAGS_WASM) $^ $(LDLIBS_WASM) -o $@
|
||||||
|
|
||||||
# Object file rules
|
# Object file rules
|
||||||
|
@ -60,7 +66,7 @@ $(OBJ_DIR_WASM)/%.o: %.c
|
||||||
# Clean build artifacts
|
# Clean build artifacts
|
||||||
# ---------------------
|
# ---------------------
|
||||||
clean:
|
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)
|
# Install target (example)
|
||||||
# ------------------------
|
# ------------------------
|
||||||
|
|
|
@ -1,20 +1,13 @@
|
||||||
#include "test.h"
|
#include "../../debug.h"
|
||||||
#include "debug.h"
|
#include "../../test.h"
|
||||||
#include "vm.h"
|
#include "../../vm.h"
|
||||||
#ifdef __EMSCRIPTEN__
|
|
||||||
#include <emscripten.h>
|
#include <emscripten.h>
|
||||||
#endif
|
|
||||||
|
|
||||||
VM vm = {0};
|
VM vm = {0};
|
||||||
|
|
||||||
void mainloop() {
|
void mainloop() {
|
||||||
if (!step_vm(&vm)) {
|
if (!step_vm(&vm)) {
|
||||||
#ifdef __EMSCRIPTEN__
|
|
||||||
emscripten_cancel_main_loop(); /* this should "kill" the app. */
|
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.return_stack_size = STACK_SIZE;
|
||||||
vm.stack_size = STACK_SIZE;
|
vm.stack_size = STACK_SIZE;
|
||||||
vm.memory_size = MEMORY_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);
|
emscripten_set_main_loop(mainloop, 0, 1);
|
||||||
#else
|
return 0;
|
||||||
while (1) {
|
|
||||||
mainloop();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
}
|
|
@ -1,5 +1,8 @@
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dumps the vm memory to a file.
|
||||||
|
*/
|
||||||
int core_dump(VM *vm) {
|
int core_dump(VM *vm) {
|
||||||
FILE *file = fopen("memory_dump.bin", "wb");
|
FILE *file = fopen("memory_dump.bin", "wb");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
|
@ -18,6 +21,9 @@ int core_dump(VM *vm) {
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print opcode.
|
||||||
|
*/
|
||||||
void printOp(uint8_t op, uint8_t dest, uint8_t src1, uint8_t src2) {
|
void printOp(uint8_t op, uint8_t dest, uint8_t src1, uint8_t src2) {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case OP_HALT:
|
case OP_HALT:
|
||||||
|
|
|
@ -3,20 +3,20 @@
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
typedef union {
|
typedef union value_u {
|
||||||
int32_t i; /* Integers */
|
int32_t i; /* Integers */
|
||||||
float f; /* Float */
|
float f; /* Float */
|
||||||
uint32_t u; /* Unsigned integers, also used for pointer address */
|
uint32_t u; /* Unsigned integers, also used for pointer address */
|
||||||
char c[4]; /* 4 Byte char array for string packing */
|
char c[4]; /* 4 Byte char array for string packing */
|
||||||
} Value;
|
} Value;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct slice_s {
|
||||||
uint32_t start;
|
uint32_t start;
|
||||||
uint32_t end;
|
uint32_t end;
|
||||||
} Slice;
|
} Slice;
|
||||||
|
|
||||||
#define MAX_REGS 32
|
#define MAX_REGS 32
|
||||||
typedef struct {
|
typedef struct frame_s {
|
||||||
Value registers[MAX_REGS]; /* R0-R31 */
|
Value registers[MAX_REGS]; /* R0-R31 */
|
||||||
uint32_t rp; /* register pointer (last unused) */
|
uint32_t rp; /* register pointer (last unused) */
|
||||||
Slice allocated; /* start and end of global allocated block */
|
Slice allocated; /* start and end of global allocated block */
|
||||||
|
@ -25,7 +25,7 @@ typedef struct {
|
||||||
#define MEMORY_SIZE 65536
|
#define MEMORY_SIZE 65536
|
||||||
#define FRAMES_SIZE 128
|
#define FRAMES_SIZE 128
|
||||||
#define STACK_SIZE 256
|
#define STACK_SIZE 256
|
||||||
typedef struct {
|
typedef struct vm_s {
|
||||||
uint32_t pc; /* Program counter */
|
uint32_t pc; /* Program counter */
|
||||||
uint32_t fp; /* Frame pointer (last allocated value) */
|
uint32_t fp; /* Frame pointer (last allocated value) */
|
||||||
uint32_t sp; /* stack pointer (top of stack) */
|
uint32_t sp; /* stack pointer (top of stack) */
|
||||||
|
|
1
src/vm.c
1
src/vm.c
|
@ -2,6 +2,7 @@
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
/* no inline fn in ANSI C :( */
|
||||||
#define COMPARE_AND_JUMP(type, accessor, op) \
|
#define COMPARE_AND_JUMP(type, accessor, op) \
|
||||||
do { \
|
do { \
|
||||||
type value = vm->frames[vm->fp].registers[src1].accessor; \
|
type value = vm->frames[vm->fp].registers[src1].accessor; \
|
||||||
|
|
Loading…
Reference in New Issue