dis.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. case OP_LOAD_CONST: {
  52. py_Ref value = c11__at(py_TValue, &co->consts, byte.arg);
  53. if(py_repr(value)) {
  54. pk_sprintf(&ss, " (%s)", py_tosv(py_retval()));
  55. } else {
  56. return false;
  57. }
  58. break;
  59. }
  60. case OP_FORMAT_STRING:
  61. case OP_IMPORT_PATH: {
  62. py_Ref path = c11__at(py_TValue, &co->consts, byte.arg);
  63. pk_sprintf(&ss, " (%q)", py_tosv(path));
  64. break;
  65. }
  66. case OP_LOAD_NAME:
  67. case OP_LOAD_GLOBAL:
  68. case OP_LOAD_NONLOCAL:
  69. case OP_STORE_GLOBAL:
  70. case OP_LOAD_ATTR:
  71. case OP_LOAD_METHOD:
  72. case OP_STORE_ATTR:
  73. case OP_DELETE_ATTR:
  74. case OP_BEGIN_CLASS:
  75. case OP_DELETE_GLOBAL:
  76. case OP_STORE_CLASS_ATTR: {
  77. pk_sprintf(&ss, " (%n)", byte.arg);
  78. break;
  79. }
  80. case OP_LOAD_FAST:
  81. case OP_STORE_FAST:
  82. case OP_DELETE_FAST: {
  83. py_Name name = c11__getitem(py_Name, &co->varnames, byte.arg);
  84. pk_sprintf(&ss, " (%n)", name);
  85. break;
  86. }
  87. case OP_LOAD_FUNCTION: {
  88. const FuncDecl* decl = c11__getitem(FuncDecl*, &co->func_decls, byte.arg);
  89. pk_sprintf(&ss, " (%s)", decl->code.name->data);
  90. break;
  91. }
  92. case OP_BINARY_OP: {
  93. py_Name name = byte.arg & 0xFF;
  94. pk_sprintf(&ss, " (%s)", pk_op2str(name));
  95. break;
  96. }
  97. }
  98. } while(0);
  99. if(i != co->codes.length - 1) c11_sbuf__write_char(&ss, '\n');
  100. }
  101. c11_string* output = c11_sbuf__submit(&ss);
  102. pk_current_vm->callbacks.print(output->data);
  103. pk_current_vm->callbacks.print("\n");
  104. c11_string__delete(output);
  105. c11_vector__dtor(&jumpTargets);
  106. return true;
  107. }
  108. static bool dis_dis(int argc, py_Ref argv) {
  109. PY_CHECK_ARGC(1);
  110. CodeObject* code = NULL;
  111. if(py_istype(argv, tp_function)) {
  112. Function* ud = py_touserdata(argv);
  113. code = &ud->decl->code;
  114. } else if(py_istype(argv, tp_code)) {
  115. code = py_touserdata(argv);
  116. } else {
  117. return TypeError("dis() expected a code object");
  118. }
  119. if(!disassemble(code)) return false;
  120. py_newnone(py_retval());
  121. return true;
  122. }
  123. void pk__add_module_dis() {
  124. py_Ref mod = py_newmodule("dis");
  125. py_bindfunc(mod, "dis", dis_dis);
  126. }