NezVMInstruction *nez_LoadMachineCode(ParsingContext context, const char *fileName, const char *nonTerminalName) { NezVMInstruction *inst = NULL; NezVMInstruction *head = NULL; size_t len; char *buf = loadFile(fileName, &len); byteCodeInfo info; info.pos = 0; /* load bytecode header */ info.version0 = buf[info.pos++]; /* version info */ info.version1 = buf[info.pos++]; info.filename_length = read32(buf, &info); /* file name */ info.filename = malloc(sizeof(uint8_t) * info.filename_length + 1); for (uint32_t i = 0; i < info.filename_length; i++) { info.filename[i] = buf[info.pos++]; } info.filename[info.filename_length] = 0; /* rule size */ int rule_size = read32(buf, &info); context->call_table = (int *)malloc(sizeof(int) * rule_size); /* bytecode length */ info.bytecode_length = read64(buf, &info); dump_byteCodeInfo(&info); free(info.filename); /* ** head is a tmporary variable that indecates the begining ** of the instruction sequence */ head = inst = __malloc(sizeof(*inst) * info.bytecode_length); memset(inst, 0, sizeof(*inst) * info.bytecode_length); /* init bytecode loader */ ByteCodeLoader loader; loader.input = buf; loader.info = &info; loader.head = head; /* f_convert[] is function pointer that emit instruction */ for (uint64_t i = 0; i < info.bytecode_length; i++) { inst->op = buf[info.pos++]; // fprintf(stderr, "%s\n", get_opname(inst->op)); nez_EmitInstruction(inst, &loader, context); inst++; } #if PEGVM_DEBUG dump_NezVMInstructions(inst, info.bytecode_length); #endif context->bytecode_length = info.bytecode_length; #if defined(NEZVM_COUNT_BYTECODE_MALLOCED_SIZE) fprintf(stderr, "instruction_size=%zd\n", sizeof(*inst)); fprintf(stderr, "malloced_size=%zd[Byte], %zd[Byte]\n", (sizeof(*inst) * info.bytecode_length), bytecode_malloced_size); #endif free(buf); return head; }
NezVMInstruction *nez_LoadMachineCode(ParsingContext context, const char *fileName, const char *nonTerminalName) { NezVMInstruction *inst = NULL; NezVMInstruction *head = NULL; size_t len; char *buf = loadFile(fileName, &len); byteCodeInfo info; info.pos = 0; /* load bytecode header */ info.version0 = buf[info.pos++]; /* version info */ info.version1 = buf[info.pos++]; info.filename_length = read32(buf, &info); /* file name */ info.filename = malloc(sizeof(uint8_t) * info.filename_length + 1); for (uint32_t i = 0; i < info.filename_length; i++) { info.filename[i] = buf[info.pos++]; } info.filename[info.filename_length] = 0; /* call table size */ context->call_table_size = read32(buf, &info); context->call_table = (int *)malloc(sizeof(int) * (int)context->call_table_size); /* set table size */ context->set_table_size = read32(buf, &info); context->set_table = (bitset_ptr_t *)malloc(sizeof(bitset_t) * (int)context->set_table_size); /* str table size */ context->str_table_size = read32(buf, &info); context->str_table = (nezvm_string_ptr_t *)malloc(sizeof(nezvm_string_ptr_t) * (int)context->str_table_size); /* bytecode length */ info.bytecode_length = read64(buf, &info); free(info.filename); /* ** head is a tmporary variable that indecates the begining ** of the instruction sequence */ head = inst = malloc(sizeof(*inst) * info.bytecode_length); memset(inst, 0, sizeof(*inst) * info.bytecode_length); /* init bytecode loader */ ByteCodeLoader *loader = malloc(sizeof(ByteCodeLoader)); loader->input = buf; loader->info = &info; loader->head = head; /* f_convert[] is function pointer that emit instruction */ for (uint64_t i = 0; i < info.bytecode_length; i++) { if (i == 723) { fprintf(stderr, "debug\n"); } inst->op = buf[info.pos++]; fprintf(stderr, "[%llu]%s\n", i, get_opname(inst->op)); nez_EmitInstruction(inst, loader, context); inst++; } #if PEGVM_DEBUG dump_NezVMInstructions(inst, info.bytecode_length); #endif context->bytecode_length = info.bytecode_length; free(buf); return head; }