1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-25 22:53:16 +00:00

Remove dsiasm from binary.

This commit is contained in:
Calvin Rose 2017-03-08 10:54:50 -05:00
parent 1e8c1bb74c
commit 68f834f03b
5 changed files with 53 additions and 59 deletions

View File

@ -6,8 +6,8 @@ TARGET=interp
PREFIX=/usr/local
# C sources
HEADERS=vm.h ds.h compile.h parse.h value.h disasm.h datatypes.h gc.h util.h
SOURCES=main.c parse.c value.c vm.c ds.c compile.c disasm.c gc.c
HEADERS=vm.h ds.h compile.h parse.h value.h datatypes.h gc.h util.h
SOURCES=main.c parse.c value.c vm.c ds.c compile.c gc.c
OBJECTS=$(patsubst %.c,%.o,$(SOURCES))
all: $(TARGET)

12
main.c
View File

@ -5,8 +5,8 @@
#include "parse.h"
#include "compile.h"
#include "value.h"
#include "disasm.h"
/* Simple printer for gst strings */
void string_put(FILE *out, uint8_t * string) {
uint32_t i;
uint32_t len = gst_string_length(string);
@ -91,16 +91,8 @@ void debug_repl(FILE *in, FILE *out) {
continue;
}
/* Print asm */
if (out) {
fprintf(out, "\n");
gst_dasm_function(out, func.data.function);
fprintf(out, "\n");
}
/* Execute function */
gst_load(&vm, func);
if (gst_start(&vm)) {
if (gst_start(&vm, func)) {
if (out) {
if (vm.crash) {
fprintf(out, "VM crash: %s\n", vm.crash);

View File

@ -9,4 +9,10 @@
# Unlike most lisps, it is not a pure functional language. Also unlike lisp, gst does
# not make much use of a list data structure, instead using arrays and objects for
# better performance at runtime.
(do
(:= a 1)
(while (< a 1025)
(print a)
(:= a (* a 2))
)
)

81
vm.c
View File

@ -18,19 +18,52 @@ static GstValue gst_vm_literal(Gst *vm, GstFunction *fn, uint16_t index) {
return fn->def->literals[index];
}
/* Load a function into the VM. The function will be called with
* no arguments when run */
static void gst_load(Gst *vm, GstValue callee) {
uint32_t startCapacity;
uint32_t locals, i;
uint16_t *pc;
GstStackFrame *frame;
GstThread *thread = gst_alloc(vm, sizeof(GstThread));
if (callee.type == GST_FUNCTION) {
locals = callee.data.function->def->locals;
pc = callee.data.function->def->byteCode;
} else if (callee.type == GST_CFUNCTION) {
locals = 0;
pc = NULL;
} else {
return;
}
startCapacity = locals + GST_FRAME_SIZE + 10;
thread->data = gst_alloc(vm, sizeof(GstValue) * startCapacity);
thread->capacity = startCapacity;
thread->count = GST_FRAME_SIZE;
vm->thread = thread;
frame = (GstStackFrame *)thread->data;
frame->prevSize = 0;
frame->size = locals;
frame->callee = callee;
frame->errorJump = NULL;
frame->env = NULL;
frame->pc = pc;
/* Nil arguments */
for (i = 0; i < locals; ++i)
thread->data[GST_FRAME_SIZE + i].type = GST_NIL;
}
/* Start running the VM */
int gst_start(Gst *vm) {
int gst_start(Gst *vm, GstValue func) {
/* VM state */
GstThread thread = *vm->thread;
GstThread thread;
GstValue *stack;
GstStackFrame frame;
GstValue temp, v1, v2;
uint16_t *pc;
/* Check for proper initialization */
if (thread.count == 0) {
gst_error(vm, "need thread in vm state");
}
/* Load the callee */
gst_load(vm, func);
thread = *vm->thread;
stack = thread.data + thread.count;
frame = *((GstStackFrame *)(stack - GST_FRAME_SIZE));
pc = frame.pc;
@ -572,40 +605,6 @@ void gst_init(Gst *vm) {
vm->thread = NULL;
}
/* Load a function into the VM. The function will be called with
* no arguments when run */
void gst_load(Gst *vm, GstValue callee) {
uint32_t startCapacity;
uint32_t locals, i;
uint16_t *pc;
GstStackFrame *frame;
GstThread *thread = gst_alloc(vm, sizeof(GstThread));
if (callee.type == GST_FUNCTION) {
locals = callee.data.function->def->locals;
pc = callee.data.function->def->byteCode;
} else if (callee.type == GST_CFUNCTION) {
locals = 0;
pc = NULL;
} else {
return;
}
startCapacity = locals + GST_FRAME_SIZE + 10;
thread->data = gst_alloc(vm, sizeof(GstValue) * startCapacity);
thread->capacity = startCapacity;
thread->count = GST_FRAME_SIZE;
vm->thread = thread;
frame = (GstStackFrame *)thread->data;
frame->prevSize = 0;
frame->size = locals;
frame->callee = callee;
frame->errorJump = NULL;
frame->env = NULL;
frame->pc = pc;
/* Nil arguments */
for (i = 0; i < locals; ++i)
thread->data[GST_FRAME_SIZE + i].type = GST_NIL;
}
/* Clear all memory associated with the VM */
void gst_deinit(Gst *vm) {
gst_clear_memory(vm);

7
vm.h
View File

@ -27,11 +27,8 @@ void gst_init(Gst * vm);
/* Deinitialize the VM */
void gst_deinit(Gst * vm);
/* Load a function to be run on the VM */
void gst_load(Gst * vm, GstValue func);
/* Start running the VM */
int gst_start(Gst * vm);
/* Start running the VM with a given entry point */
int gst_start(Gst * vm, GstValue func);
/* Run garbage collection */
void gst_collect(Gst * vm);