Fix serilaization bug. Still need to serialize c functions.

This commit is contained in:
Calvin Rose 2017-05-23 14:48:54 -04:00
parent 145688b49f
commit f066047112
7 changed files with 36 additions and 41 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
// Place your settings in this file to overwrite default and user settings.
{
}

View File

@ -26,17 +26,17 @@
#define OP_WIDTH 20
/* Print various register and arguments to instructions */
static void dasm_print_slot(FILE * out, uint16_t index) { fprintf(out, "%d ", index); }
static void dasm_print_i16(FILE * out, int16_t number) { fprintf(out, "#%d ", number); }
static void dasm_print_i32(FILE * out, int32_t number) { fprintf(out, "#%d ", number); }
static void dasm_print_f64(FILE * out, double number) { fprintf(out, "#%f ", number); }
static void dasm_print_literal(FILE * out, uint16_t index) { fprintf(out, "(%d) ", index); }
static void dasm_print_upvalue(FILE * out, uint16_t level, uint16_t index) {
static void dasm_print_slot(FILE *out, uint16_t index) { fprintf(out, "%d ", index); }
static void dasm_print_i16(FILE *out, int16_t number) { fprintf(out, "#%d ", number); }
static void dasm_print_i32(FILE *out, int32_t number) { fprintf(out, "#%d ", number); }
static void dasm_print_f64(FILE *out, double number) { fprintf(out, "#%f ", number); }
static void dasm_print_literal(FILE *out, uint16_t index) { fprintf(out, "(%d) ", index); }
static void dasm_print_upvalue(FILE *out, uint16_t level, uint16_t index) {
fprintf(out, "<%d, %d> ", level, index);
}
/* Print the name of the argument but pad it */
static void dasm_print_arg(FILE * out, const char * name) {
static void dasm_print_arg(FILE *out, const char *name) {
uint32_t i = 0;
char c;
while ((c = *name++)) {
@ -48,7 +48,7 @@ static void dasm_print_arg(FILE * out, const char * name) {
}
/* Print instructions that take a fixed number of arguments */
static uint32_t dasm_fixed_op(FILE * out, const uint16_t * current,
static uint32_t dasm_fixed_op(FILE *out, const uint16_t *current,
const char * name, uint32_t size) {
uint32_t i;
dasm_print_arg(out, name);
@ -58,7 +58,7 @@ static uint32_t dasm_fixed_op(FILE * out, const uint16_t * current,
}
/* Print instructions that take a variable number of arguments */
static uint32_t dasm_varg_op(FILE * out, const uint16_t * current,
static uint32_t dasm_varg_op(FILE *out, const uint16_t *current,
const char * name, uint32_t extra) {
uint32_t i, argCount;
dasm_print_arg(out, name);
@ -75,17 +75,17 @@ static uint32_t dasm_varg_op(FILE * out, const uint16_t * current,
}
/* Print the disassembly for a function definition */
void gst_dasm_funcdef(FILE * out, GstFuncDef * def) {
void gst_dasm_funcdef(FILE *out, GstFuncDef *def) {
gst_dasm(out, def->byteCode, def->byteCodeLen);
}
/* Print the disassembly for a function */
void gst_dasm_function(FILE * out, GstFunction * f) {
void gst_dasm_function(FILE *out, GstFunction *f) {
gst_dasm(out, f->def->byteCode, f->def->byteCodeLen);
}
/* Disassemble some bytecode and display it as opcode + arguments assembly */
void gst_dasm(FILE * out, uint16_t *byteCode, uint32_t len) {
void gst_dasm(FILE *out, uint16_t *byteCode, uint32_t len) {
uint16_t *current = byteCode;
uint16_t *end = byteCode + len;

View File

@ -339,7 +339,7 @@ static const char *gst_deserialize_impl(
if (err != NULL) return err;
}
read_u32(byteCodeLen);
deser_datacheck(byteCodeLen);
deser_datacheck(byteCodeLen * sizeof(uint16_t));
def->byteCode = gst_alloc(vm, byteCodeLen * sizeof(uint16_t));
def->byteCodeLen = byteCodeLen;
for (i = 0; i < byteCodeLen; ++i) {
@ -412,8 +412,12 @@ static const char *gst_deserialize_impl(
case 216: /* C function */
/* TODO - add registry for c functions */
read_u32(length);
ret.type = GST_NIL;
/* For now just add 64 bit integer */
{
int64_t rawp;
read_i64(rawp);
ret.type = GST_NIL;
}
break;
case 217: /* Reference */
@ -443,7 +447,6 @@ const char *gst_deserialize(
const char *err;
GstArray *visited = gst_array(vm, 10);
err = gst_deserialize_impl(vm, data, data + len, nextData, visited, &ret);
printf("Read %ld\n", *nextData - data);
if (err != NULL) return err;
*out = ret;
return NULL;
@ -453,7 +456,7 @@ const char *gst_deserialize(
BUFFER_DEFINE(real, GstReal)
BUFFER_DEFINE(integer, GstInteger)
BUFFER_DEFINE(u32, uint32_t)
BUFFER_DEFINE(u16, uint32_t)
BUFFER_DEFINE(u16, uint16_t)
/* Serialize a value and write to a buffer. Returns possible
* error messages. */
@ -541,13 +544,13 @@ const char *gst_serialize_impl(
if (err != NULL) return err;
}
}
/* Record reference */
/* Record reference after serialization */
gst_table_put(vm, visited, x, gst_wrap_integer(*nextId));
*nextId = *nextId + 1;
return NULL;
}
/* Record reference */
/* Record reference before serialization */
gst_table_put(vm, visited, x, gst_wrap_integer(*nextId));
*nextId = *nextId + 1;

View File

@ -162,6 +162,9 @@ int gst_stl_length(Gst *vm) {
case GST_STRUCT:
ret.data.integer = gst_struct_length(x.data.st);
break;
case GST_FUNCDEF:
ret.data.integer = x.data.def->byteCodeLen;
break;
}
gst_c_return(vm, ret);
}
@ -404,8 +407,9 @@ int gst_stl_thread(Gst *vm) {
GstThread *t;
GstValue callee = gst_arg(vm, 0);
if (callee.type != GST_FUNCTION && callee.type != GST_CFUNCTION)
gst_c_throwc(vm, "expected function");
gst_c_throwc(vm, "expected function in thread constructor");
t = gst_thread(vm, callee, 10);
t->parent = vm->thread;
gst_c_return(vm, gst_wrap_thread(t));
}

View File

@ -41,7 +41,7 @@ GstThread *gst_thread(Gst *vm, GstValue callee, uint32_t capacity) {
gst_frame_env(stack) = NULL;
gst_frame_callee(stack) = callee;
gst_thread_endframe(vm, thread);
thread->parent = vm->thread;
thread->parent = NULL; /* Need to set parent manually */
return thread;
}
@ -143,8 +143,6 @@ void gst_thread_endframe(Gst *vm, GstThread *thread) {
gst_thread_pushnil(vm, thread, locals - gst_frame_size(stack));
}
}
stack = thread->data + thread->count;
gst_frame_args(stack) = gst_frame_size(stack) + GST_FRAME_SIZE;
}
}

View File

@ -397,14 +397,6 @@ int gst_continue(Gst *vm) {
if (vm->thread->parent == NULL)
return GST_RETURN_ERROR;
vm->thread = vm->thread->parent;
while (vm->thread->count < GST_FRAME_SIZE) {
if (vm->thread->parent) {
vm->thread->status = GST_THREAD_DEAD;
vm->thread = vm->thread->parent;
} else {
return GST_RETURN_ERROR;
}
}
stack = vm->thread->data + vm->thread->count;
stack[gst_frame_ret(stack)] = vm->ret;
pc = gst_frame_pc(stack);
@ -424,21 +416,16 @@ int gst_continue(Gst *vm) {
/* Run the vm with a given function. This function is
* called to start the vm. */
int gst_run(Gst *vm, GstValue callee) {
int status;
GstValue *stack;
vm->thread = gst_thread(vm, callee, 64);
if (vm->thread == NULL)
return GST_RETURN_CRASH;
stack = gst_thread_stack(vm->thread);
/* If callee was not actually a function, get the delegate function */
callee = gst_frame_callee(stack);
if (callee.type == GST_CFUNCTION) {
vm->ret.type = GST_NIL;
status = callee.data.cfunction(vm);
gst_thread_popframe(vm, vm->thread);
return status;
} else {
return callee.data.cfunction(vm);
} else if (callee.type == GST_FUNCTION) {
return gst_continue(vm);
} else {
return GST_RETURN_CRASH;
}
}

View File

@ -352,7 +352,7 @@ const uint8_t *gst_buffer_to_string(Gst *vm, GstBuffer *buffer);
/* Define a push function for pushing a certain type to the buffer */
#define BUFFER_DEFINE(name, type) \
static void gst_buffer_push_##name(Gst * vm, GstBuffer * buffer, type x) { \
static void gst_buffer_push_##name(Gst *vm, GstBuffer *buffer, type x) { \
union { type t; uint8_t bytes[sizeof(type)]; } u; \
u.t = x; gst_buffer_append(vm, buffer, u.bytes, sizeof(type)); \
}