diff --git a/byterun/include/utils.h b/byterun/include/utils.h index f3986781e..13219725c 100644 --- a/byterun/include/utils.h +++ b/byterun/include/utils.h @@ -9,14 +9,14 @@ /* The unpacked representation of bytecode file */ typedef struct { - char *string_ptr; /* A pointer to the beginning of the string table */ - int *public_ptr; /* A pointer to the beginning of publics table */ - char *code_ptr; /* A pointer to the bytecode itself */ - int *global_ptr; /* A pointer to the global area */ - int code_size; /* The size (in bytes) of code */ - int stringtab_size; /* The size (in bytes) of the string table */ - int global_area_size; /* The size (in words) of global area */ - int public_symbols_number; /* The number of public symbols */ + char *string_ptr; /* A pointer to the beginning of the string table */ + int *public_ptr; /* A pointer to the beginning of publics table */ + char *code_ptr; /* A pointer to the bytecode itself */ + int *global_ptr; /* A pointer to the global area */ + int code_size; /* The size (in bytes) of code */ + uint stringtab_size; /* The size (in bytes) of the string table */ + uint global_area_size; /* The size (in words) of global area */ + uint public_symbols_number; /* The number of public symbols */ char buffer[0]; } bytefile; @@ -36,7 +36,7 @@ static inline const char *get_string(const bytefile *f, size_t pos) { /* Gets a name for a public symbol */ static inline const char *get_public_name(const bytefile *f, size_t i) { - if (i * 2 >= f->public_symbols_number) { + if (i >= f->public_symbols_number) { failure("public number is out of range: %zu >= %i\n", i * 2, f->public_symbols_number); } @@ -45,7 +45,7 @@ static inline const char *get_public_name(const bytefile *f, size_t i) { /* Gets an offset for a publie symbol */ static inline size_t get_public_offset(const bytefile *f, size_t i) { - if (i * 2 + 1 >= f->public_symbols_number) { + if (i + 1 >= f->public_symbols_number) { failure("public number is out of range: %zu >= %i\n", i * 2 + 1, f->public_symbols_number); } diff --git a/byterun/src/interpreter.c b/byterun/src/interpreter.c index d4d4f402a..f117197eb 100644 --- a/byterun/src/interpreter.c +++ b/byterun/src/interpreter.c @@ -424,19 +424,20 @@ void run(bytefile *bf, int argc, char **argv) { case CMD_BUILTIN_Barray: { // CALL Barray %d size_t elem_count = ip_read_int(&s.ip); - // void **opr_buffer = elem_count > BUFFER_SIZE - // ? alloc(elem_count * sizeof(void *)) - // : buffer; - // for (size_t i = 0; i < elem_count; ++i) { - // opr_buffer[elem_count - i - 1] = s_pop(); - // } + void **opr_buffer = elem_count > BUFFER_SIZE + ? alloc(elem_count * sizeof(void *)) + : buffer; + for (size_t i = 0; i < elem_count; ++i) { + opr_buffer[elem_count - i - 1] = s_pop(); + } - s_rotate_n(elem_count); - // void *array = - // Barray((aint *)opr_buffer, - // BOX(elem_count)); // NOTE: not shure if elems should be - // added - void *array = Barray((aint *)s_peek(), BOX(elem_count)); + // s_rotate_n(elem_count); + void *array = + Barray((aint *)opr_buffer, + BOX(elem_count)); // NOTE: not shure if elems should be + // added + + // void *array = Barray((aint *)s_peek(), BOX(elem_count)); s_push(array); break; } diff --git a/byterun/src/parser.c b/byterun/src/parser.c index c06604c46..6a704365f 100644 --- a/byterun/src/parser.c +++ b/byterun/src/parser.c @@ -49,14 +49,17 @@ bytefile* read_file (char *fname) { if (file->buffer + public_symbols_size >= file_end) { failure ("public symbols are out of the file size\n"); } + file->string_ptr = &file->buffer[public_symbols_size]; if (file->string_ptr + file->stringtab_size > file_end) { failure ("strings table is out of the file size\n"); } + if (file->stringtab_size > 0 && file->string_ptr[file->stringtab_size - 1] != 0) { + failure ("strings table is not zero-ended\n"); + } if (file->code_size < 0 || public_symbols_size < 0 || file->stringtab_size < 0) { failure ("file zones sizes should be >= 0\n"); } - file->string_ptr = &file->buffer [public_symbols_size]; file->public_ptr = (int*) file->buffer; file->code_ptr = &file->string_ptr [file->stringtab_size]; file->global_ptr = (int*) calloc (file->global_area_size, sizeof (int));