Added lib folder for general stuff, introduced as target to Makefile
This commit is contained in:
31
Makefile
31
Makefile
@@ -10,29 +10,43 @@ TERM_GREEN:=$(shell echo -e "\e[0;32m")
|
|||||||
TERM_RESET:=$(shell echo -e "\e[0;0m")
|
TERM_RESET:=$(shell echo -e "\e[0;0m")
|
||||||
|
|
||||||
# Setup variables for source code, output, etc
|
# 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_DIST=$(DIST)/vm
|
||||||
VM_SRC=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_OBJECTS:=$(VM_CODE:$(VM_SRC)/%.c=$(VM_DIST)/%.o)
|
||||||
VM_DEPS:=$(VM_OBJECTS:%.o=%.d) $(VM_DIST)/fib.d $(VM_DIST)/main.d
|
VM_DEPS:=$(VM_OBJECTS:%.o=%.d) $(VM_DIST)/fib.d $(VM_DIST)/main.d
|
||||||
VM_VERBOSE=0
|
VM_VERBOSE=0
|
||||||
VM_CFLAGS:=$(CFLAGS) -D VERBOSE=$(VM_VERBOSE)
|
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`
|
# 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)
|
vm: $(VM_DIST) $(VM_OUT)
|
||||||
examples: vm-examples
|
examples: vm-examples
|
||||||
vm-examples: $(VM_DIST) $(VM_DIST)/fib.out
|
vm-examples: $(VM_DIST) $(VM_DIST)/fib.out
|
||||||
|
|
||||||
# Recipes
|
# Recipes
|
||||||
|
## LIB Recipes
|
||||||
|
-include $(LIB_DEPS)
|
||||||
|
|
||||||
$(VM_OUT): $(VM_OBJECTS) $(VM_DIST)/main.o
|
$(LIB_DIST)/%.o: $(LIB_SRC)/%.c
|
||||||
@$(CC) $(VM_CFLAGS) $^ -o $@ $(LIBS)
|
@$(CC) $(LIB_CFLAGS) -MMD -c $< -o $@ $(LIBS)
|
||||||
@echo -e "$(TERM_GREEN)$@$(TERM_RESET)"
|
@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)
|
@$(CC) $(VM_CFLAGS) $^ -o $@ $(LIBS)
|
||||||
@echo -e "$(TERM_GREEN)$@$(TERM_RESET)"
|
@echo -e "$(TERM_GREEN)$@$(TERM_RESET)"
|
||||||
|
|
||||||
@@ -56,5 +70,8 @@ clean:
|
|||||||
$(DIST):
|
$(DIST):
|
||||||
mkdir -p $(DIST)
|
mkdir -p $(DIST)
|
||||||
|
|
||||||
$(VM_DIST): $(DIST)
|
$(LIB_DIST):
|
||||||
|
mkdir -p $(LIB_DIST)
|
||||||
|
|
||||||
|
$(VM_DIST):
|
||||||
mkdir -p $(VM_DIST)
|
mkdir -p $(VM_DIST)
|
||||||
|
|||||||
79
vm/fib.c
79
vm/fib.c
@@ -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 <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "./base.h"
|
#include "../lib/base.h"
|
||||||
#include "./darr.h"
|
#include "../lib/darr.h"
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "./base.h"
|
#include "../lib/base.h"
|
||||||
#include "./inst.h"
|
#include "./inst.h"
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
|
|||||||
Reference in New Issue
Block a user