|
@@ -77,112 +77,6 @@ const char* pk_opname(Opcode op) {
|
|
|
return OP_NAMES[op];
|
|
return OP_NAMES[op];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static void disassemble(CodeObject* co) {
|
|
|
|
|
- c11_vector /*T=int*/ jumpTargets;
|
|
|
|
|
- c11_vector__ctor(&jumpTargets, sizeof(int));
|
|
|
|
|
- for(int i = 0; i < co->codes.count; i++) {
|
|
|
|
|
- Bytecode* bc = c11__at(Bytecode, &co->codes, i);
|
|
|
|
|
- if(Bytecode__is_forward_jump(bc)) {
|
|
|
|
|
- int target = (int16_t)bc->arg + i;
|
|
|
|
|
- c11_vector__push(int, &jumpTargets, target);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- c11_sbuf ss;
|
|
|
|
|
- c11_sbuf__ctor(&ss);
|
|
|
|
|
-
|
|
|
|
|
- int prev_line = -1;
|
|
|
|
|
- for(int i = 0; i < co->codes.count; i++) {
|
|
|
|
|
- Bytecode byte = c11__getitem(Bytecode, &co->codes, i);
|
|
|
|
|
- BytecodeEx ex = c11__getitem(BytecodeEx, &co->codes_ex, i);
|
|
|
|
|
-
|
|
|
|
|
- char line[8] = "";
|
|
|
|
|
- if(ex.lineno == prev_line) {
|
|
|
|
|
- // do nothing
|
|
|
|
|
- } else {
|
|
|
|
|
- snprintf(line, sizeof(line), "%d", ex.lineno);
|
|
|
|
|
- if(prev_line != -1) c11_sbuf__write_char(&ss, '\n');
|
|
|
|
|
- prev_line = ex.lineno;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- char pointer[4] = "";
|
|
|
|
|
- c11__foreach(int, &jumpTargets, it) {
|
|
|
|
|
- if(*it == i) {
|
|
|
|
|
- snprintf(pointer, sizeof(pointer), "->");
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- char buf[32];
|
|
|
|
|
- snprintf(buf, sizeof(buf), "%-8s%-3s%-3d ", line, pointer, i);
|
|
|
|
|
- c11_sbuf__write_cstr(&ss, buf);
|
|
|
|
|
-
|
|
|
|
|
- c11_sbuf__write_cstr(&ss, pk_opname(byte.op));
|
|
|
|
|
- c11_sbuf__write_char(&ss, ex.is_virtual ? '*' : ' ');
|
|
|
|
|
- int padding = 24 - strlen(pk_opname(byte.op));
|
|
|
|
|
- for(int j = 0; j < padding; j++)
|
|
|
|
|
- c11_sbuf__write_char(&ss, ' ');
|
|
|
|
|
-
|
|
|
|
|
- do {
|
|
|
|
|
- if(Bytecode__is_forward_jump(&byte)) {
|
|
|
|
|
- pk_sprintf(&ss, "%d (to %d)", (int16_t)byte.arg, (int16_t)byte.arg + i);
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- c11_sbuf__write_int(&ss, byte.arg);
|
|
|
|
|
- switch(byte.op) {
|
|
|
|
|
- case OP_LOAD_CONST:
|
|
|
|
|
- case OP_FORMAT_STRING:
|
|
|
|
|
- case OP_IMPORT_PATH: {
|
|
|
|
|
- py_Ref path = c11__at(py_TValue, &co->consts, byte.arg);
|
|
|
|
|
- pk_sprintf(&ss, " (%q)", py_tosv(path));
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- case OP_LOAD_NAME:
|
|
|
|
|
- case OP_LOAD_GLOBAL:
|
|
|
|
|
- case OP_LOAD_NONLOCAL:
|
|
|
|
|
- case OP_STORE_GLOBAL:
|
|
|
|
|
- case OP_LOAD_ATTR:
|
|
|
|
|
- case OP_LOAD_METHOD:
|
|
|
|
|
- case OP_STORE_ATTR:
|
|
|
|
|
- case OP_DELETE_ATTR:
|
|
|
|
|
- case OP_BEGIN_CLASS:
|
|
|
|
|
- case OP_GOTO:
|
|
|
|
|
- case OP_DELETE_GLOBAL:
|
|
|
|
|
- case OP_STORE_CLASS_ATTR: {
|
|
|
|
|
- pk_sprintf(&ss, " (%n)", byte.arg);
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- case OP_LOAD_FAST:
|
|
|
|
|
- case OP_STORE_FAST:
|
|
|
|
|
- case OP_DELETE_FAST: {
|
|
|
|
|
- py_Name name = c11__getitem(py_Name, &co->varnames, byte.arg);
|
|
|
|
|
- pk_sprintf(&ss, " (%n)", name);
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- case OP_LOAD_FUNCTION: {
|
|
|
|
|
- const FuncDecl* decl = c11__getitem(FuncDecl*, &co->func_decls, byte.arg);
|
|
|
|
|
- pk_sprintf(&ss, " (%s)", decl->code.name->data);
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- case OP_BINARY_OP: {
|
|
|
|
|
- py_Name name = byte.arg & 0xFF;
|
|
|
|
|
- pk_sprintf(&ss, " (%n)", name);
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- } while(0);
|
|
|
|
|
-
|
|
|
|
|
- if(i != co->codes.count - 1) c11_sbuf__write_char(&ss, '\n');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- c11_string* output = c11_sbuf__submit(&ss);
|
|
|
|
|
- pk_current_vm->print(output->data);
|
|
|
|
|
- pk_current_vm->print("\n");
|
|
|
|
|
- c11_string__delete(output);
|
|
|
|
|
- c11_vector__dtor(&jumpTargets);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
bool py_exec(const char* source, const char* filename, enum py_CompileMode mode, py_Ref module) {
|
|
bool py_exec(const char* source, const char* filename, enum py_CompileMode mode, py_Ref module) {
|
|
|
VM* vm = pk_current_vm;
|
|
VM* vm = pk_current_vm;
|
|
|
CodeObject co;
|
|
CodeObject co;
|
|
@@ -197,7 +91,6 @@ bool py_exec(const char* source, const char* filename, enum py_CompileMode mode,
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // disassemble(&co);
|
|
|
|
|
if(!module) module = &vm->main;
|
|
if(!module) module = &vm->main;
|
|
|
|
|
|
|
|
Frame* frame = Frame__new(&co, module, NULL, vm->stack.sp, vm->stack.sp, &co);
|
|
Frame* frame = Frame__new(&co, module, NULL, vm->stack.sp, vm->stack.sp, &co);
|