dis.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "pocketpy/pocketpy.h"
  2. #include "pocketpy/common/sstream.h"
  3. #include "pocketpy/interpreter/vm.h"
  4. #include <stdbool.h>
  5. static bool disassemble(CodeObject* co) {
  6. c11_vector /*T=int*/ jumpTargets;
  7. c11_vector__ctor(&jumpTargets, sizeof(int));
  8. for(int i = 0; i < co->codes.length; i++) {
  9. Bytecode* bc = c11__at(Bytecode, &co->codes, i);
  10. if(Bytecode__is_forward_jump(bc)) {
  11. int target = (int16_t)bc->arg + i;
  12. c11_vector__push(int, &jumpTargets, target);
  13. }
  14. }
  15. c11_sbuf ss;
  16. c11_sbuf__ctor(&ss);
  17. int prev_line = -1;
  18. for(int i = 0; i < co->codes.length; i++) {
  19. Bytecode byte = c11__getitem(Bytecode, &co->codes, i);
  20. BytecodeEx ex = c11__getitem(BytecodeEx, &co->codes_ex, i);
  21. char line[8] = "";
  22. if(ex.lineno == prev_line) {
  23. // do nothing
  24. } else {
  25. snprintf(line, sizeof(line), "%d", ex.lineno);
  26. if(prev_line != -1) c11_sbuf__write_char(&ss, '\n');
  27. prev_line = ex.lineno;
  28. }
  29. char pointer[4] = "";
  30. c11__foreach(int, &jumpTargets, it) {
  31. if(*it == i) {
  32. snprintf(pointer, sizeof(pointer), "->");
  33. break;
  34. }
  35. }
  36. char buf[32];
  37. snprintf(buf, sizeof(buf), "%-8s%-3s%-3d ", line, pointer, i);
  38. c11_sbuf__write_cstr(&ss, buf);
  39. c11_sbuf__write_cstr(&ss, pk_opname(byte.op));
  40. c11_sbuf__write_char(&ss, ex.is_virtual ? '*' : ' ');
  41. int padding = 24 - strlen(pk_opname(byte.op));
  42. for(int j = 0; j < padding; j++)
  43. c11_sbuf__write_char(&ss, ' ');
  44. do {
  45. if(Bytecode__is_forward_jump(&byte)) {
  46. pk_sprintf(&ss, "%d (to %d)", (int16_t)byte.arg, (int16_t)byte.arg + i);
  47. break;
  48. }
  49. c11_sbuf__write_int(&ss, byte.arg);
  50. switch(byte.op) {
  51. // TODO: see `dis.py` there is a memory issue
  52. case OP_LOAD_CONST: {
  53. py_Ref value = c11__at(py_TValue, &co->consts, byte.arg);
  54. if(py_repr(value)) {
  55. pk_sprintf(&ss, " (%v)", py_tosv(py_retval()));
  56. } else {
  57. return false;
  58. }
  59. break;
  60. }
  61. case OP_FORMAT_STRING:
  62. case OP_IMPORT_PATH: {
  63. py_Ref path = c11__at(py_TValue, &co->consts, byte.arg);
  64. pk_sprintf(&ss, " (%q)", py_tosv(path));
  65. break;
  66. }
  67. case OP_LOAD_NAME:
  68. case OP_LOAD_GLOBAL:
  69. case OP_LOAD_NONLOCAL:
  70. case OP_STORE_GLOBAL:
  71. case OP_LOAD_ATTR:
  72. case OP_LOAD_METHOD:
  73. case OP_STORE_ATTR:
  74. case OP_DELETE_ATTR:
  75. case OP_BEGIN_CLASS:
  76. case OP_DELETE_GLOBAL:
  77. case OP_STORE_CLASS_ATTR: {
  78. pk_sprintf(&ss, " (%n)", byte.arg);
  79. break;
  80. }
  81. case OP_LOAD_FAST:
  82. case OP_STORE_FAST:
  83. case OP_DELETE_FAST: {
  84. py_Name name = c11__getitem(py_Name, &co->varnames, byte.arg);
  85. pk_sprintf(&ss, " (%n)", name);
  86. break;
  87. }
  88. case OP_LOAD_FUNCTION: {
  89. const FuncDecl* decl = c11__getitem(FuncDecl*, &co->func_decls, byte.arg);
  90. pk_sprintf(&ss, " (%s)", decl->code.name->data);
  91. break;
  92. }
  93. case OP_BINARY_OP: {
  94. py_Name name = byte.arg & 0xFF;
  95. pk_sprintf(&ss, " (%s)", pk_op2str(name));
  96. break;
  97. }
  98. }
  99. } while(0);
  100. if(i != co->codes.length - 1) c11_sbuf__write_char(&ss, '\n');
  101. }
  102. c11_string* output = c11_sbuf__submit(&ss);
  103. pk_current_vm->callbacks.print(output->data);
  104. pk_current_vm->callbacks.print("\n");
  105. c11_string__delete(output);
  106. c11_vector__dtor(&jumpTargets);
  107. return true;
  108. }
  109. static bool dis_dis(int argc, py_Ref argv) {
  110. PY_CHECK_ARGC(1);
  111. CodeObject* code = NULL;
  112. if(py_istype(argv, tp_function)) {
  113. Function* ud = py_touserdata(argv);
  114. code = &ud->decl->code;
  115. } else if(py_istype(argv, tp_code)) {
  116. code = py_touserdata(argv);
  117. } else {
  118. return TypeError("dis() expected a code object");
  119. }
  120. if(!disassemble(code)) return false;
  121. py_newnone(py_retval());
  122. return true;
  123. }
  124. void pk__add_module_dis() {
  125. py_Ref mod = py_newmodule("dis");
  126. py_bindfunc(mod, "dis", dis_dis);
  127. }