Reintroduced example fib.c via examples/ folder
This commit is contained in:
23
Makefile
23
Makefile
@@ -23,19 +23,24 @@ VM_DIST=$(DIST)/vm
|
|||||||
VM_SRC=vm
|
VM_SRC=vm
|
||||||
VM_CODE:=$(addprefix $(VM_SRC)/, 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)/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=$(DIST)/ovm.out
|
||||||
|
|
||||||
|
|
||||||
|
## EXAMPLES setup
|
||||||
|
EXAMPLES_DIST=$(DIST)/examples
|
||||||
|
EXAMPLES_SRC=examples
|
||||||
|
EXAMPLES_CFLAGS=$(CFLAGS)
|
||||||
|
EXAMPLES=$(DIST)/fib.out
|
||||||
|
|
||||||
# Things you want to build on `make`
|
# Things you want to build on `make`
|
||||||
all: $(DIST) lib vm examples
|
all: $(DIST) lib vm examples
|
||||||
|
|
||||||
lib: $(LIB_DIST) $(LIB_OBJECTS)
|
lib: $(LIB_DIST) $(LIB_OBJECTS)
|
||||||
vm: $(VM_DIST) $(VM_OUT)
|
vm: $(VM_DIST) $(VM_OUT)
|
||||||
examples: vm-examples
|
examples: $(EXAMPLES_DIST) $(EXAMPLES)
|
||||||
vm-examples: $(VM_DIST) $(VM_DIST)/fib.out
|
|
||||||
|
|
||||||
# Recipes
|
# Recipes
|
||||||
## LIB Recipes
|
## LIB Recipes
|
||||||
@@ -56,6 +61,15 @@ $(VM_DIST)/%.o: $(VM_SRC)/%.c
|
|||||||
@$(CC) $(VM_CFLAGS) -MMD -c $< -o $@ $(LIBS)
|
@$(CC) $(VM_CFLAGS) -MMD -c $< -o $@ $(LIBS)
|
||||||
@echo -e "$(TERM_GREEN)$@$(TERM_RESET)"
|
@echo -e "$(TERM_GREEN)$@$(TERM_RESET)"
|
||||||
|
|
||||||
|
## EXAMPLES recipes
|
||||||
|
$(DIST)/fib.out: $(LIB_OBJECTS) $(VM_OBJECTS) $(ASM_OBJECTS) $(EXAMPLES_DIST)/fib.o
|
||||||
|
@$(CC) $(EXAMPLES_CFLAGS) $^ -o $@ $(LIBS)
|
||||||
|
@echo -e "$(TERM_GREEN)$@$(TERM_RESET)"
|
||||||
|
|
||||||
|
$(EXAMPLES_DIST)/%.o: $(EXAMPLES_SRC)/%.c
|
||||||
|
@$(CC) $(EXAMPLES_CFLAGS) -MMD -c $< -o $@ $(LIBS)
|
||||||
|
@echo -e "$(TERM_GREEN)$@$(TERM_RESET)"
|
||||||
|
|
||||||
OUT=
|
OUT=
|
||||||
ARGS=
|
ARGS=
|
||||||
.PHONY: run
|
.PHONY: run
|
||||||
@@ -75,3 +89,6 @@ $(LIB_DIST):
|
|||||||
|
|
||||||
$(VM_DIST):
|
$(VM_DIST):
|
||||||
mkdir -p $(VM_DIST)
|
mkdir -p $(VM_DIST)
|
||||||
|
|
||||||
|
$(EXAMPLES_DIST):
|
||||||
|
mkdir -p $(EXAMPLES_DIST)
|
||||||
|
|||||||
79
examples/fib.c
Normal file
79
examples/fib.c
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
/* 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 "../vm/inst.h"
|
||||||
|
#include "../vm/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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user