This commit is contained in:
ProgramSnail 2024-11-12 21:08:41 +03:00
parent 1df2624c25
commit 233fa95e14
6 changed files with 37 additions and 7 deletions

Binary file not shown.

View file

@ -13,6 +13,7 @@ typedef struct {
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 */

View file

@ -15,6 +15,7 @@ int main(int argc, char** argv) {
#endif
run(f, argc - 1, argv + 1);
free(f->global_ptr);
free(f);
return 0;

View file

@ -24,9 +24,9 @@ static inline char *ip_read_string(char **ip, bytefile *bf) {
const size_t BUFFER_SIZE = 1000;
void run(bytefile *bf, int argc, char **argv) {
void *stack[STACK_SIZE];
size_t stack[STACK_SIZE];
void *buffer[BUFFER_SIZE];
construct_state(bf, &s, stack);
construct_state(bf, &s, (void**)stack);
#ifdef DEBUG_VERSION
printf("--- interpreter run ---\n");
@ -74,6 +74,15 @@ void run(bytefile *bf, int argc, char **argv) {
// char *before_op_ip = s.ip; // save to set s.prev_ip
bool call_happened = false;
if (s.ip >= bf->code_ptr + bf->code_size) {
s_failure(&s, "instruction pointer is out of range (>= size)");
}
if (s.ip < bf->code_ptr) {
s_failure(&s, "instruction pointer is out of range (< 0)");
}
s.instr_ip = s.ip;
char x = ip_read_byte(&s.ip), h = (x & 0xF0) >> 4, l = x & 0x0F;
#ifdef DEBUG_VERSION

View file

@ -26,12 +26,16 @@ bytefile* read_file (char *fname) {
}
long size = ftell (f);
file = (bytefile*) malloc (size + sizeof(void*) * 4);
long additional_size = sizeof(void*) * 4 + sizeof(int);
file = (bytefile*) malloc (size + additional_size); // file itself + additional data
char* file_begin = (char*)file + additional_size;
char* file_end = file_begin + size;
if (file == 0) {
failure ("unable to allocate memory to store file data\n");
}
rewind (f);
if (size != fread (&file->stringtab_size, 1, size, f)) {
@ -39,12 +43,25 @@ bytefile* read_file (char *fname) {
}
fclose (f);
file->string_ptr = &file->buffer [file->public_symbols_number * 2 * sizeof(int)];
long public_symbols_size = file->public_symbols_number * 2 * sizeof(int);
if (file->buffer + public_symbols_size >= file_end) {
failure ("public symbols are out of the file size\n");
}
if (file->string_ptr + file->stringtab_size > file_end) {
failure ("strings table is out of the file size\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));
file->code_size = size - public_symbols_size - file->stringtab_size;
return file;
}

View file

@ -24,6 +24,8 @@ static void init_state(bytefile *bf, struct State* s, void** stack) {
s->stack[i] = NULL;
}
// printf("%p:%zu - %zu", s->stack, (size_t)s->stack, (size_t)s->stack & 0xF);
s->sp = s->stack + STACK_SIZE; // [top -> bottom] stack
s->fp = NULL;
}