diff options
-rw-r--r-- | Makefile | 31 | ||||
-rw-r--r-- | lib/base.h (renamed from vm/base.h) | 0 | ||||
-rw-r--r-- | lib/darr.c (renamed from vm/darr.c) | 0 | ||||
-rw-r--r-- | lib/darr.h (renamed from vm/darr.h) | 0 | ||||
-rw-r--r-- | vm/fib.c | 79 | ||||
-rw-r--r-- | vm/inst.h | 4 | ||||
-rw-r--r-- | vm/runtime.h | 2 |
7 files changed, 27 insertions, 89 deletions
@@ -10,29 +10,43 @@ TERM_GREEN:=$(shell echo -e "\e[0;32m") TERM_RESET:=$(shell echo -e "\e[0;0m") # Setup variables for source code, output, etc +## Lib setup +LIB_DIST=$(DIST)/lib +LIB_SRC=lib +LIB_CODE:=$(addprefix $(LIB_SRC)/, darr.c) +LIB_OBJECTS:=$(LIB_CODE:$(LIB_SRC)/%.c=$(LIB_DIST)/%.o) +LIB_DEPS:=$(LIB_OBJECTS:%.o=%.d) +LIB_CFLAGS=$(CFLAGS) + +## VM setup VM_DIST=$(DIST)/vm VM_SRC=vm -VM_CODE:=$(addprefix $(VM_SRC)/, darr.c inst.c runtime.c) +VM_CODE:=$(addprefix $(VM_SRC)/, inst.c runtime.c) VM_OBJECTS:=$(VM_CODE:$(VM_SRC)/%.c=$(VM_DIST)/%.o) VM_DEPS:=$(VM_OBJECTS:%.o=%.d) $(VM_DIST)/fib.d $(VM_DIST)/main.d VM_VERBOSE=0 VM_CFLAGS:=$(CFLAGS) -D VERBOSE=$(VM_VERBOSE) +VM_OUT=$(DIST)/ovm.out -VM_OUT=$(VM_DIST)/ovm.out # Things you want to build on `make` -all: $(DIST) vm examples +all: $(DIST) lib vm examples + +lib: $(LIB_DIST) $(LIB_OBJECTS) vm: $(VM_DIST) $(VM_OUT) examples: vm-examples vm-examples: $(VM_DIST) $(VM_DIST)/fib.out # Recipes +## LIB Recipes +-include $(LIB_DEPS) -$(VM_OUT): $(VM_OBJECTS) $(VM_DIST)/main.o - @$(CC) $(VM_CFLAGS) $^ -o $@ $(LIBS) +$(LIB_DIST)/%.o: $(LIB_SRC)/%.c + @$(CC) $(LIB_CFLAGS) -MMD -c $< -o $@ $(LIBS) @echo -e "$(TERM_GREEN)$@$(TERM_RESET)" -$(VM_DIST)/fib.out: $(VM_OBJECTS) $(VM_DIST)/fib.o +## VM Recipes +$(VM_OUT): $(LIB_OBJECTS) $(VM_OBJECTS) $(VM_DIST)/main.o @$(CC) $(VM_CFLAGS) $^ -o $@ $(LIBS) @echo -e "$(TERM_GREEN)$@$(TERM_RESET)" @@ -56,5 +70,8 @@ clean: $(DIST): mkdir -p $(DIST) -$(VM_DIST): $(DIST) +$(LIB_DIST): + mkdir -p $(LIB_DIST) + +$(VM_DIST): mkdir -p $(VM_DIST) diff --git a/vm/fib.c b/vm/fib.c deleted file mode 100644 index 7107c3e..0000000 --- a/vm/fib.c +++ /dev/null @@ -1,79 +0,0 @@ -/* Copyright (C) 2023 Aryadev Chavali - - * You may distribute and modify this code under the terms of the GPLv2 - * license. You should have received a copy of the GPLv2 license with - * this file. If not, please write to: aryadev@aryadevchavali.com. - - * Created: 2023-10-23 - * Author: Aryadev Chavali - * Description: An example virtual machine program which computes and - * prints fibonacci numbers. Note that by default the virtual machine - * just rolls overflows over, so this program will never terminate. - */ - -#include <assert.h> -#include <stdio.h> -#include <string.h> - -#include "./inst.h" -#include "./runtime.h" - -int main(void) -{ - inst_t instructions[] = { - // MOV the values 1 and 1 to REG[0] and REG[1] respectively - INST_PUSH(WORD, 1), - INST_MOV(WORD, 0), - INST_PUSH(WORD, 1), - INST_MOV(WORD, 1), - - // Print value at register 0 with newline. - INST_PUSH_REG(WORD, 0), // <-- # - INST_PRINT(WORD), - INST_PUSH(BYTE, '\n'), - INST_PRINT(CHAR), - - // Print value at register 1 with newline - INST_PUSH_REG(WORD, 1), - INST_PRINT(WORD), - INST_PUSH(BYTE, '\n'), - INST_PRINT(CHAR), - - /* Compute the next pair of fibonacci numbers */ - // REG[0] + REG[1] - INST_PUSH_REG(WORD, 0), - INST_PUSH_REG(WORD, 1), - INST_PLUS(WORD), - - // Mov REG[0] + REG[1] to REG[0] - INST_MOV(WORD, 0), - - // REG[0] + REG[1] - INST_PUSH_REG(WORD, 0), - INST_PUSH_REG(WORD, 1), - INST_PLUS(WORD), - - // Mov REG[0] + REG[1] to REG[1] - INST_MOV(WORD, 1), - - // Jump to the point # - INST_JUMP_ABS(4), - INST_HALT, - }; - - byte stack[256]; - vm_t vm = {0}; - vm_load_stack(&vm, stack, ARR_SIZE(stack)); - vm_load_program(&vm, instructions, ARR_SIZE(instructions)); - err_t err = vm_execute_all(&vm); - - if (err) - { - const char *error_str = err_as_cstr(err); - fprintf(stderr, "[ERROR]: %s\n", error_str); - fprintf(stderr, "[ERROR]: VM Trace:\n"); - vm_print_all(&vm, stderr); - return 255 - err; - } - return 0; -} @@ -16,8 +16,8 @@ #include <stdio.h> #include <stdlib.h> -#include "./base.h" -#include "./darr.h" +#include "../lib/base.h" +#include "../lib/darr.h" typedef enum { diff --git a/vm/runtime.h b/vm/runtime.h index 83ffa17..03cd21f 100644 --- a/vm/runtime.h +++ b/vm/runtime.h @@ -16,7 +16,7 @@ #include <stdio.h> #include <stdlib.h> -#include "./base.h" +#include "../lib/base.h" #include "./inst.h" typedef enum |