From ac57e32a0222a3f18e138f0a358d1d01921be3a0 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Mon, 23 Oct 2023 04:24:28 +0100 Subject: Added lib folder for general stuff, introduced as target to Makefile --- Makefile | 31 ++++++++++++++++++------ lib/base.h | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/darr.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/darr.h | 39 ++++++++++++++++++++++++++++++ vm/base.h | 70 ----------------------------------------------------- vm/darr.c | 77 ---------------------------------------------------------- vm/darr.h | 39 ------------------------------ vm/fib.c | 79 ------------------------------------------------------------ vm/inst.h | 4 +-- vm/runtime.h | 2 +- 10 files changed, 213 insertions(+), 275 deletions(-) create mode 100644 lib/base.h create mode 100644 lib/darr.c create mode 100644 lib/darr.h delete mode 100644 vm/base.h delete mode 100644 vm/darr.c delete mode 100644 vm/darr.h delete mode 100644 vm/fib.c diff --git a/Makefile b/Makefile index 57be35b..f4bde0f 100644 --- a/Makefile +++ b/Makefile @@ -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/lib/base.h b/lib/base.h new file mode 100644 index 0000000..dbeec80 --- /dev/null +++ b/lib/base.h @@ -0,0 +1,70 @@ +/* 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-15 + * Author: Aryadev Chavali + * Description: Basic types and routines + */ + +#ifndef BASE_H +#define BASE_H + +#include + +#define ARR_SIZE(xs) (sizeof(xs) / sizeof(xs[0])) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) > (b) ? (b) : (a)) + +// Flags +#ifndef VERBOSE +#define VERBOSE 0 +#endif + +typedef uint64_t u64; +typedef uint32_t u32; +typedef int32_t i32; +typedef int64_t i64; +typedef float f32; +typedef double f64; + +typedef uint8_t byte; +typedef u32 hword; +typedef u64 word; + +typedef union +{ + byte as_byte; + hword as_hword; + word as_word; +} data_t; + +typedef enum +{ + DATA_TYPE_NIL = 0, + DATA_TYPE_BYTE, + DATA_TYPE_HWORD, + DATA_TYPE_WORD, +} data_type_t; + +#define DBYTE(BYTE) ((data_t){.as_byte = (BYTE)}) +#define DHWORD(HWORD) ((data_t){.as_hword = (HWORD)}) +#define DWORD(WORD) ((data_t){.as_word = (WORD)}) + +#define HWORD_SIZE sizeof(hword) +#define WORD_SIZE sizeof(word) + +// Macros to extract the nth byte or nth hword from a word +#define WORD_NTH_BYTE(WORD, N) (((WORD) >> ((N)*8)) & 0xff) +#define WORD_NTH_HWORD(WORD, N) (((WORD) >> ((N)*2)) & 0xFFFFFFFF) + +// Assume array contains 4 bytes. +hword convert_bytes_to_hword(byte *); +void convert_hword_to_bytes(hword, byte *); +// Assume array contains 8 bytes. +word convert_bytes_to_word(byte *); +void convert_word_to_bytes(word, byte *); + +#endif diff --git a/lib/darr.c b/lib/darr.c new file mode 100644 index 0000000..4393c4b --- /dev/null +++ b/lib/darr.c @@ -0,0 +1,77 @@ +/* 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-15 + * Author: Aryadev Chavali + * Description: Dynamically sized byte array + */ + +#include +#include +#include + +#include "./darr.h" + +void darr_init(darr_t *darr, size_t size) +{ + if (size == 0) + size = DARR_DEFAULT_SIZE; + *darr = (darr_t){ + .data = calloc(size, 1), + .used = 0, + .available = size, + }; +} + +void darr_ensure_capacity(darr_t *darr, size_t requested) +{ + if (darr->used + requested >= darr->available) + { + darr->available = + MAX(darr->used + requested, darr->available * DARR_REALLOC_MULT); + darr->data = realloc(darr->data, darr->available); + } +} + +void darr_append_byte(darr_t *darr, byte byte) +{ + darr_ensure_capacity(darr, 1); + darr->data[darr->used++] = byte; +} + +void darr_append_bytes(darr_t *darr, byte *bytes, size_t n) +{ + darr_ensure_capacity(darr, n); + memcpy(darr->data + darr->used, bytes, n); + darr->used += n; +} + +byte darr_at(darr_t *darr, size_t index) +{ + if (index >= darr->used) + // TODO: Error (index is out of bounds) + return 0; + return darr->data[index]; +} + +void darr_write_file(darr_t *bytes, FILE *fp) +{ + size_t size = fwrite(bytes->data, bytes->used, 1, fp); + assert(size == 1); +} + +darr_t darr_read_file(FILE *fp) +{ + darr_t darr = {0}; + fseek(fp, 0, SEEK_END); + long size = ftell(fp); + darr_init(&darr, size); + fseek(fp, 0, SEEK_SET); + size_t read = fread(darr.data, size, 1, fp); + assert(read == 1); + return darr; +} diff --git a/lib/darr.h b/lib/darr.h new file mode 100644 index 0000000..1d5820b --- /dev/null +++ b/lib/darr.h @@ -0,0 +1,39 @@ +/* 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-15 + * Author: Aryadev Chavali + * Description: Dynamically sized byte array + */ + +#ifndef DARR_H +#define DARR_H + +#include +#include + +#include "./base.h" + +typedef struct +{ + byte *data; + size_t used, available; +} darr_t; + +#define DARR_DEFAULT_SIZE 8 +#define DARR_REALLOC_MULT 1.5 + +void darr_init(darr_t *, size_t); +void darr_ensure_capacity(darr_t *, size_t); +void darr_append_byte(darr_t *, byte); +void darr_append_bytes(darr_t *, byte *, size_t); +byte darr_at(darr_t *, size_t); + +void darr_write_file(darr_t *, FILE *); +darr_t darr_read_file(FILE *); + +#endif diff --git a/vm/base.h b/vm/base.h deleted file mode 100644 index dbeec80..0000000 --- a/vm/base.h +++ /dev/null @@ -1,70 +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-15 - * Author: Aryadev Chavali - * Description: Basic types and routines - */ - -#ifndef BASE_H -#define BASE_H - -#include - -#define ARR_SIZE(xs) (sizeof(xs) / sizeof(xs[0])) -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -#define MIN(a, b) ((a) > (b) ? (b) : (a)) - -// Flags -#ifndef VERBOSE -#define VERBOSE 0 -#endif - -typedef uint64_t u64; -typedef uint32_t u32; -typedef int32_t i32; -typedef int64_t i64; -typedef float f32; -typedef double f64; - -typedef uint8_t byte; -typedef u32 hword; -typedef u64 word; - -typedef union -{ - byte as_byte; - hword as_hword; - word as_word; -} data_t; - -typedef enum -{ - DATA_TYPE_NIL = 0, - DATA_TYPE_BYTE, - DATA_TYPE_HWORD, - DATA_TYPE_WORD, -} data_type_t; - -#define DBYTE(BYTE) ((data_t){.as_byte = (BYTE)}) -#define DHWORD(HWORD) ((data_t){.as_hword = (HWORD)}) -#define DWORD(WORD) ((data_t){.as_word = (WORD)}) - -#define HWORD_SIZE sizeof(hword) -#define WORD_SIZE sizeof(word) - -// Macros to extract the nth byte or nth hword from a word -#define WORD_NTH_BYTE(WORD, N) (((WORD) >> ((N)*8)) & 0xff) -#define WORD_NTH_HWORD(WORD, N) (((WORD) >> ((N)*2)) & 0xFFFFFFFF) - -// Assume array contains 4 bytes. -hword convert_bytes_to_hword(byte *); -void convert_hword_to_bytes(hword, byte *); -// Assume array contains 8 bytes. -word convert_bytes_to_word(byte *); -void convert_word_to_bytes(word, byte *); - -#endif diff --git a/vm/darr.c b/vm/darr.c deleted file mode 100644 index 4393c4b..0000000 --- a/vm/darr.c +++ /dev/null @@ -1,77 +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-15 - * Author: Aryadev Chavali - * Description: Dynamically sized byte array - */ - -#include -#include -#include - -#include "./darr.h" - -void darr_init(darr_t *darr, size_t size) -{ - if (size == 0) - size = DARR_DEFAULT_SIZE; - *darr = (darr_t){ - .data = calloc(size, 1), - .used = 0, - .available = size, - }; -} - -void darr_ensure_capacity(darr_t *darr, size_t requested) -{ - if (darr->used + requested >= darr->available) - { - darr->available = - MAX(darr->used + requested, darr->available * DARR_REALLOC_MULT); - darr->data = realloc(darr->data, darr->available); - } -} - -void darr_append_byte(darr_t *darr, byte byte) -{ - darr_ensure_capacity(darr, 1); - darr->data[darr->used++] = byte; -} - -void darr_append_bytes(darr_t *darr, byte *bytes, size_t n) -{ - darr_ensure_capacity(darr, n); - memcpy(darr->data + darr->used, bytes, n); - darr->used += n; -} - -byte darr_at(darr_t *darr, size_t index) -{ - if (index >= darr->used) - // TODO: Error (index is out of bounds) - return 0; - return darr->data[index]; -} - -void darr_write_file(darr_t *bytes, FILE *fp) -{ - size_t size = fwrite(bytes->data, bytes->used, 1, fp); - assert(size == 1); -} - -darr_t darr_read_file(FILE *fp) -{ - darr_t darr = {0}; - fseek(fp, 0, SEEK_END); - long size = ftell(fp); - darr_init(&darr, size); - fseek(fp, 0, SEEK_SET); - size_t read = fread(darr.data, size, 1, fp); - assert(read == 1); - return darr; -} diff --git a/vm/darr.h b/vm/darr.h deleted file mode 100644 index 1d5820b..0000000 --- a/vm/darr.h +++ /dev/null @@ -1,39 +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-15 - * Author: Aryadev Chavali - * Description: Dynamically sized byte array - */ - -#ifndef DARR_H -#define DARR_H - -#include -#include - -#include "./base.h" - -typedef struct -{ - byte *data; - size_t used, available; -} darr_t; - -#define DARR_DEFAULT_SIZE 8 -#define DARR_REALLOC_MULT 1.5 - -void darr_init(darr_t *, size_t); -void darr_ensure_capacity(darr_t *, size_t); -void darr_append_byte(darr_t *, byte); -void darr_append_bytes(darr_t *, byte *, size_t); -byte darr_at(darr_t *, size_t); - -void darr_write_file(darr_t *, FILE *); -darr_t darr_read_file(FILE *); - -#endif 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 -#include -#include - -#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; -} diff --git a/vm/inst.h b/vm/inst.h index 8902757..5882e4f 100644 --- a/vm/inst.h +++ b/vm/inst.h @@ -16,8 +16,8 @@ #include #include -#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 #include -#include "./base.h" +#include "../lib/base.h" #include "./inst.h" typedef enum -- cgit v1.2.3-13-gbd6f