int main(int argc, char* argv[]) { const char *input_luac, *opcodes_def; int i; lua_State* L; Proto* f; FILE* D; gargc = argc; gargv = argv; i = doargs(argc,argv); argc -= i; argv += i; if (argc < 1) { usage("need 1 arguments at least", NULL); } L = lua_open(); glstate = L; //luaB_opentests(L); input_luac = argv[0]; if (luaL_loadfile(L, input_luac) != 0) { fatal(lua_tostring(L, -1)); } f = toproto(L, -1); if (cmp_gen_lua || cmp_gen_luac) { Proto* input_proto = f; Proto* allopcodes_proto; const char* buff = cmp_gen_lua ? allopcodes_lua : allopcodes_luac; int bufflen = cmp_gen_lua ? allopcodes_lua_len : allopcodes_luac_len; if (luaL_loadbuffer(L, buff, bufflen, "allopcodes.lua") != 0) { fatal(lua_tostring(L, -1)); } allopcodes_proto = toproto(L, -1); CompareAndGenOpcodes(input_proto, allopcodes_proto); } else { opcodes_def = (argv[1]) ? (argv[1]) : OPCODES_TXT; if (!generateOp2op(opcodes_def)) { fprintf(stderr, "opcodes.txt file: %s format error!", opcodes_def); return EXIT_FAILURE; } swapOpCode(f); D = (output == NULL) ? stdout : fopen(output, "wb"); if (D == NULL) cannot("open", output); lua_lock(L); luaU_dump(L, f, writer, D, 0); lua_unlock(L); if (ferror(D)) cannot("write", output); if (fclose(D)) cannot("close", output); printf("%s generated!\n", output); } lua_close(L); return EXIT_SUCCESS; }
void swapOpCode(Proto* f) { int pc, i; for (pc = 0; pc < f->sizecode; pc++) { Instruction ins = f->code[pc]; OpCode op = GET_OPCODE(ins); OpCode newop = op2op[op]; f->code[pc] = SET_OPCODE(ins, newop); } for (i = 0; i < f->sizep; i++) { swapOpCode(f->p[i]); } }
void swapOpCode(Proto* f) { int pc, fi; for (pc = 0; pc < f->sizecode; pc++){ Instruction i = f->code[pc]; OpCode op = GET_OPCODE(i); OpCode newop = op2op[op]; f->code[pc] = SET_OPCODE(i, newop); } for (fi = 0; fi < f->sizep; fi++){ swapOpCode(f->p[fi]); } }