void VM_start(bstring binary, bstring filename) { STATE = State_new(); Runtime_init(state); VALUE lobby = Lobby_new(state); // toplevel object state->lobby = lobby; state->binary = binary; BytecodeFile *file = BytecodeFile_new(state, filename); int fn_count = DArray_count(file->function_names); for(int j=0; j < fn_count; j++) { bstring fn_name = (bstring)DArray_at(file->function_names, j); Function *fn = (Function*)Hashmap_get(file->functions, fn_name); Hashmap_set(state->functions, fn_name, fn); } bstring main_fn = bfromcstr("0_main"); CallFrame *top_frame = CallFrame_new(lobby, STATE_FN(main_fn), NULL); bdestroy(main_fn); top_frame->name = "main"; Stack_push(FRAMES, top_frame); // now we're ready to bootstrap State_bootstrap(state); // and run the codes! VM_run(state); State_destroy(state); }
void runvm (VM *vm, Program *p, int *es) { if (VM_getDebugMode ()) { puts ("Running the following program: "); Program_print (p); } *es = VM_run (vm); VM_destroy (vm); }
int main(int argc, char** argv) { VM* vm = VM_new(); const s32 code[] = { 0x05, 0x00, 0x14, -3, 0x16, 0x00, 0x0b, 0x01, 0x0a, 0x00, 0x17, 0x00, 0x18, 0x09, 0x01, 0x0a, 0x01, 0x17, 0x00, 0x01, 0x01, 0x02, 0x16, 0x00, 0x17, 0x00, 0x18, 0x01, 0x00, 0x12, 0x0d, 0x00, 0x09, 0x02, 0x08, 0x06, 0x15, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x01, 0x07, 0x19, 0x03, 0x07, 0x00, 0x01, 0x00 }; VM_run(vm, code); return 0; }
void VM_runFromString(VM* vm, const char* code) { s32 generated_code[1024]; int index = 0, bufptr = 0, codeptr = 0; char buf[128]; while (1) { if (code[index] == ' ' || code[index] == '\0') { char* hexa = malloc(bufptr); for (int i = 0; i < bufptr; i++) { hexa[i] = buf[i]; } bufptr = 0; memset(buf, 0, sizeof(buf)); generated_code[codeptr++] = (s32)strtol(hexa, NULL, 16); free(hexa); if (code[index] == '\0') { break; } } else { buf[bufptr++] = code[index]; } index++; } VM_run(vm, generated_code); }