showbc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <assert.h>
  28. #include "py/bc0.h"
  29. #include "py/bc.h"
  30. #if MICROPY_DEBUG_PRINTERS
  31. // redirect all printfs in this file to the platform print stream
  32. #define printf(...) mp_printf(&mp_plat_print, __VA_ARGS__)
  33. #define DECODE_UINT { \
  34. unum = 0; \
  35. do { \
  36. unum = (unum << 7) + (*ip & 0x7f); \
  37. } while ((*ip++ & 0x80) != 0); \
  38. }
  39. #define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
  40. #define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
  41. #if MICROPY_PERSISTENT_CODE
  42. #define DECODE_QSTR \
  43. qst = ip[0] | ip[1] << 8; \
  44. ip += 2;
  45. #define DECODE_PTR \
  46. DECODE_UINT; \
  47. unum = mp_showbc_const_table[unum]
  48. #define DECODE_OBJ \
  49. DECODE_UINT; \
  50. unum = mp_showbc_const_table[unum]
  51. #else
  52. #define DECODE_QSTR { \
  53. qst = 0; \
  54. do { \
  55. qst = (qst << 7) + (*ip & 0x7f); \
  56. } while ((*ip++ & 0x80) != 0); \
  57. }
  58. #define DECODE_PTR do { \
  59. ip = (byte*)MP_ALIGN(ip, sizeof(void*)); \
  60. unum = (uintptr_t)*(void**)ip; \
  61. ip += sizeof(void*); \
  62. } while (0)
  63. #define DECODE_OBJ do { \
  64. ip = (byte*)MP_ALIGN(ip, sizeof(mp_obj_t)); \
  65. unum = (mp_uint_t)*(mp_obj_t*)ip; \
  66. ip += sizeof(mp_obj_t); \
  67. } while (0)
  68. #endif
  69. const byte *mp_showbc_code_start;
  70. const mp_uint_t *mp_showbc_const_table;
  71. void mp_bytecode_print(const void *descr, const byte *ip, mp_uint_t len, const mp_uint_t *const_table) {
  72. mp_showbc_code_start = ip;
  73. // get bytecode parameters
  74. mp_uint_t n_state = mp_decode_uint(&ip);
  75. mp_uint_t n_exc_stack = mp_decode_uint(&ip);
  76. /*mp_uint_t scope_flags =*/ ip++;
  77. mp_uint_t n_pos_args = *ip++;
  78. mp_uint_t n_kwonly_args = *ip++;
  79. /*mp_uint_t n_def_pos_args =*/ ip++;
  80. const byte *code_info = ip;
  81. mp_uint_t code_info_size = mp_decode_uint(&code_info);
  82. ip += code_info_size;
  83. #if MICROPY_PERSISTENT_CODE
  84. qstr block_name = code_info[0] | (code_info[1] << 8);
  85. qstr source_file = code_info[2] | (code_info[3] << 8);
  86. code_info += 4;
  87. #else
  88. qstr block_name = mp_decode_uint(&code_info);
  89. qstr source_file = mp_decode_uint(&code_info);
  90. #endif
  91. printf("File %s, code block '%s' (descriptor: %p, bytecode @%p " UINT_FMT " bytes)\n",
  92. qstr_str(source_file), qstr_str(block_name), descr, mp_showbc_code_start, len);
  93. // raw bytecode dump
  94. printf("Raw bytecode (code_info_size=" UINT_FMT ", bytecode_size=" UINT_FMT "):\n", code_info_size, len - code_info_size);
  95. for (mp_uint_t i = 0; i < len; i++) {
  96. if (i > 0 && i % 16 == 0) {
  97. printf("\n");
  98. }
  99. printf(" %02x", mp_showbc_code_start[i]);
  100. }
  101. printf("\n");
  102. // bytecode prelude: arg names (as qstr objects)
  103. printf("arg names:");
  104. for (mp_uint_t i = 0; i < n_pos_args + n_kwonly_args; i++) {
  105. printf(" %s", qstr_str(MP_OBJ_QSTR_VALUE(const_table[i])));
  106. }
  107. printf("\n");
  108. printf("(N_STATE " UINT_FMT ")\n", n_state);
  109. printf("(N_EXC_STACK " UINT_FMT ")\n", n_exc_stack);
  110. // for printing line number info
  111. const byte *bytecode_start = ip;
  112. // bytecode prelude: initialise closed over variables
  113. {
  114. uint local_num;
  115. while ((local_num = *ip++) != 255) {
  116. printf("(INIT_CELL %u)\n", local_num);
  117. }
  118. len -= ip - mp_showbc_code_start;
  119. }
  120. // print out line number info
  121. {
  122. mp_int_t bc = bytecode_start - ip;
  123. mp_uint_t source_line = 1;
  124. printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
  125. for (const byte* ci = code_info; *ci;) {
  126. if ((ci[0] & 0x80) == 0) {
  127. // 0b0LLBBBBB encoding
  128. bc += ci[0] & 0x1f;
  129. source_line += ci[0] >> 5;
  130. ci += 1;
  131. } else {
  132. // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
  133. bc += ci[0] & 0xf;
  134. source_line += ((ci[0] << 4) & 0x700) | ci[1];
  135. ci += 2;
  136. }
  137. printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
  138. }
  139. }
  140. mp_bytecode_print2(ip, len - 0, const_table);
  141. }
  142. const byte *mp_bytecode_print_str(const byte *ip) {
  143. mp_uint_t unum;
  144. qstr qst;
  145. switch (*ip++) {
  146. case MP_BC_LOAD_CONST_FALSE:
  147. printf("LOAD_CONST_FALSE");
  148. break;
  149. case MP_BC_LOAD_CONST_NONE:
  150. printf("LOAD_CONST_NONE");
  151. break;
  152. case MP_BC_LOAD_CONST_TRUE:
  153. printf("LOAD_CONST_TRUE");
  154. break;
  155. case MP_BC_LOAD_CONST_SMALL_INT: {
  156. mp_int_t num = 0;
  157. if ((ip[0] & 0x40) != 0) {
  158. // Number is negative
  159. num--;
  160. }
  161. do {
  162. num = (num << 7) | (*ip & 0x7f);
  163. } while ((*ip++ & 0x80) != 0);
  164. printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
  165. break;
  166. }
  167. case MP_BC_LOAD_CONST_STRING:
  168. DECODE_QSTR;
  169. printf("LOAD_CONST_STRING '%s'", qstr_str(qst));
  170. break;
  171. case MP_BC_LOAD_CONST_OBJ:
  172. DECODE_OBJ;
  173. printf("LOAD_CONST_OBJ %p=", MP_OBJ_TO_PTR(unum));
  174. mp_obj_print_helper(&mp_plat_print, (mp_obj_t)unum, PRINT_REPR);
  175. break;
  176. case MP_BC_LOAD_NULL:
  177. printf("LOAD_NULL");
  178. break;
  179. case MP_BC_LOAD_FAST_N:
  180. DECODE_UINT;
  181. printf("LOAD_FAST_N " UINT_FMT, unum);
  182. break;
  183. case MP_BC_LOAD_DEREF:
  184. DECODE_UINT;
  185. printf("LOAD_DEREF " UINT_FMT, unum);
  186. break;
  187. case MP_BC_LOAD_NAME:
  188. DECODE_QSTR;
  189. printf("LOAD_NAME %s", qstr_str(qst));
  190. if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
  191. printf(" (cache=%u)", *ip++);
  192. }
  193. break;
  194. case MP_BC_LOAD_GLOBAL:
  195. DECODE_QSTR;
  196. printf("LOAD_GLOBAL %s", qstr_str(qst));
  197. if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
  198. printf(" (cache=%u)", *ip++);
  199. }
  200. break;
  201. case MP_BC_LOAD_ATTR:
  202. DECODE_QSTR;
  203. printf("LOAD_ATTR %s", qstr_str(qst));
  204. if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
  205. printf(" (cache=%u)", *ip++);
  206. }
  207. break;
  208. case MP_BC_LOAD_METHOD:
  209. DECODE_QSTR;
  210. printf("LOAD_METHOD %s", qstr_str(qst));
  211. break;
  212. case MP_BC_LOAD_SUPER_METHOD:
  213. DECODE_QSTR;
  214. printf("LOAD_SUPER_METHOD %s", qstr_str(qst));
  215. break;
  216. case MP_BC_LOAD_BUILD_CLASS:
  217. printf("LOAD_BUILD_CLASS");
  218. break;
  219. case MP_BC_LOAD_SUBSCR:
  220. printf("LOAD_SUBSCR");
  221. break;
  222. case MP_BC_STORE_FAST_N:
  223. DECODE_UINT;
  224. printf("STORE_FAST_N " UINT_FMT, unum);
  225. break;
  226. case MP_BC_STORE_DEREF:
  227. DECODE_UINT;
  228. printf("STORE_DEREF " UINT_FMT, unum);
  229. break;
  230. case MP_BC_STORE_NAME:
  231. DECODE_QSTR;
  232. printf("STORE_NAME %s", qstr_str(qst));
  233. break;
  234. case MP_BC_STORE_GLOBAL:
  235. DECODE_QSTR;
  236. printf("STORE_GLOBAL %s", qstr_str(qst));
  237. break;
  238. case MP_BC_STORE_ATTR:
  239. DECODE_QSTR;
  240. printf("STORE_ATTR %s", qstr_str(qst));
  241. if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
  242. printf(" (cache=%u)", *ip++);
  243. }
  244. break;
  245. case MP_BC_STORE_SUBSCR:
  246. printf("STORE_SUBSCR");
  247. break;
  248. case MP_BC_DELETE_FAST:
  249. DECODE_UINT;
  250. printf("DELETE_FAST " UINT_FMT, unum);
  251. break;
  252. case MP_BC_DELETE_DEREF:
  253. DECODE_UINT;
  254. printf("DELETE_DEREF " UINT_FMT, unum);
  255. break;
  256. case MP_BC_DELETE_NAME:
  257. DECODE_QSTR;
  258. printf("DELETE_NAME %s", qstr_str(qst));
  259. break;
  260. case MP_BC_DELETE_GLOBAL:
  261. DECODE_QSTR;
  262. printf("DELETE_GLOBAL %s", qstr_str(qst));
  263. break;
  264. case MP_BC_DUP_TOP:
  265. printf("DUP_TOP");
  266. break;
  267. case MP_BC_DUP_TOP_TWO:
  268. printf("DUP_TOP_TWO");
  269. break;
  270. case MP_BC_POP_TOP:
  271. printf("POP_TOP");
  272. break;
  273. case MP_BC_ROT_TWO:
  274. printf("ROT_TWO");
  275. break;
  276. case MP_BC_ROT_THREE:
  277. printf("ROT_THREE");
  278. break;
  279. case MP_BC_JUMP:
  280. DECODE_SLABEL;
  281. printf("JUMP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  282. break;
  283. case MP_BC_POP_JUMP_IF_TRUE:
  284. DECODE_SLABEL;
  285. printf("POP_JUMP_IF_TRUE " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  286. break;
  287. case MP_BC_POP_JUMP_IF_FALSE:
  288. DECODE_SLABEL;
  289. printf("POP_JUMP_IF_FALSE " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  290. break;
  291. case MP_BC_JUMP_IF_TRUE_OR_POP:
  292. DECODE_SLABEL;
  293. printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  294. break;
  295. case MP_BC_JUMP_IF_FALSE_OR_POP:
  296. DECODE_SLABEL;
  297. printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  298. break;
  299. case MP_BC_SETUP_WITH:
  300. DECODE_ULABEL; // loop-like labels are always forward
  301. printf("SETUP_WITH " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  302. break;
  303. case MP_BC_WITH_CLEANUP:
  304. printf("WITH_CLEANUP");
  305. break;
  306. case MP_BC_UNWIND_JUMP:
  307. DECODE_SLABEL;
  308. printf("UNWIND_JUMP " UINT_FMT " %d", (mp_uint_t)(ip + unum - mp_showbc_code_start), *ip);
  309. ip += 1;
  310. break;
  311. case MP_BC_SETUP_EXCEPT:
  312. DECODE_ULABEL; // except labels are always forward
  313. printf("SETUP_EXCEPT " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  314. break;
  315. case MP_BC_SETUP_FINALLY:
  316. DECODE_ULABEL; // except labels are always forward
  317. printf("SETUP_FINALLY " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  318. break;
  319. case MP_BC_END_FINALLY:
  320. // if TOS is an exception, reraises the exception (3 values on TOS)
  321. // if TOS is an integer, does something else
  322. // if TOS is None, just pops it and continues
  323. // else error
  324. printf("END_FINALLY");
  325. break;
  326. case MP_BC_GET_ITER:
  327. printf("GET_ITER");
  328. break;
  329. case MP_BC_GET_ITER_STACK:
  330. printf("GET_ITER_STACK");
  331. break;
  332. case MP_BC_FOR_ITER:
  333. DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
  334. printf("FOR_ITER " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  335. break;
  336. case MP_BC_POP_BLOCK:
  337. // pops block and restores the stack
  338. printf("POP_BLOCK");
  339. break;
  340. case MP_BC_POP_EXCEPT:
  341. // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
  342. printf("POP_EXCEPT");
  343. break;
  344. case MP_BC_BUILD_TUPLE:
  345. DECODE_UINT;
  346. printf("BUILD_TUPLE " UINT_FMT, unum);
  347. break;
  348. case MP_BC_BUILD_LIST:
  349. DECODE_UINT;
  350. printf("BUILD_LIST " UINT_FMT, unum);
  351. break;
  352. case MP_BC_BUILD_MAP:
  353. DECODE_UINT;
  354. printf("BUILD_MAP " UINT_FMT, unum);
  355. break;
  356. case MP_BC_STORE_MAP:
  357. printf("STORE_MAP");
  358. break;
  359. case MP_BC_BUILD_SET:
  360. DECODE_UINT;
  361. printf("BUILD_SET " UINT_FMT, unum);
  362. break;
  363. #if MICROPY_PY_BUILTINS_SLICE
  364. case MP_BC_BUILD_SLICE:
  365. DECODE_UINT;
  366. printf("BUILD_SLICE " UINT_FMT, unum);
  367. break;
  368. #endif
  369. case MP_BC_STORE_COMP:
  370. DECODE_UINT;
  371. printf("STORE_COMP " UINT_FMT, unum);
  372. break;
  373. case MP_BC_UNPACK_SEQUENCE:
  374. DECODE_UINT;
  375. printf("UNPACK_SEQUENCE " UINT_FMT, unum);
  376. break;
  377. case MP_BC_UNPACK_EX:
  378. DECODE_UINT;
  379. printf("UNPACK_EX " UINT_FMT, unum);
  380. break;
  381. case MP_BC_MAKE_FUNCTION:
  382. DECODE_PTR;
  383. printf("MAKE_FUNCTION %p", (void*)(uintptr_t)unum);
  384. break;
  385. case MP_BC_MAKE_FUNCTION_DEFARGS:
  386. DECODE_PTR;
  387. printf("MAKE_FUNCTION_DEFARGS %p", (void*)(uintptr_t)unum);
  388. break;
  389. case MP_BC_MAKE_CLOSURE: {
  390. DECODE_PTR;
  391. mp_uint_t n_closed_over = *ip++;
  392. printf("MAKE_CLOSURE %p " UINT_FMT, (void*)(uintptr_t)unum, n_closed_over);
  393. break;
  394. }
  395. case MP_BC_MAKE_CLOSURE_DEFARGS: {
  396. DECODE_PTR;
  397. mp_uint_t n_closed_over = *ip++;
  398. printf("MAKE_CLOSURE_DEFARGS %p " UINT_FMT, (void*)(uintptr_t)unum, n_closed_over);
  399. break;
  400. }
  401. case MP_BC_CALL_FUNCTION:
  402. DECODE_UINT;
  403. printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
  404. break;
  405. case MP_BC_CALL_FUNCTION_VAR_KW:
  406. DECODE_UINT;
  407. printf("CALL_FUNCTION_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
  408. break;
  409. case MP_BC_CALL_METHOD:
  410. DECODE_UINT;
  411. printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
  412. break;
  413. case MP_BC_CALL_METHOD_VAR_KW:
  414. DECODE_UINT;
  415. printf("CALL_METHOD_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
  416. break;
  417. case MP_BC_RETURN_VALUE:
  418. printf("RETURN_VALUE");
  419. break;
  420. case MP_BC_RAISE_VARARGS:
  421. unum = *ip++;
  422. printf("RAISE_VARARGS " UINT_FMT, unum);
  423. break;
  424. case MP_BC_YIELD_VALUE:
  425. printf("YIELD_VALUE");
  426. break;
  427. case MP_BC_YIELD_FROM:
  428. printf("YIELD_FROM");
  429. break;
  430. case MP_BC_IMPORT_NAME:
  431. DECODE_QSTR;
  432. printf("IMPORT_NAME '%s'", qstr_str(qst));
  433. break;
  434. case MP_BC_IMPORT_FROM:
  435. DECODE_QSTR;
  436. printf("IMPORT_FROM '%s'", qstr_str(qst));
  437. break;
  438. case MP_BC_IMPORT_STAR:
  439. printf("IMPORT_STAR");
  440. break;
  441. default:
  442. if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) {
  443. printf("LOAD_CONST_SMALL_INT " INT_FMT, (mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16);
  444. } else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) {
  445. printf("LOAD_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI);
  446. } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) {
  447. printf("STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI);
  448. } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NUM_BYTECODE) {
  449. printf("UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI);
  450. } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + MP_BINARY_OP_NUM_BYTECODE) {
  451. mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI;
  452. printf("BINARY_OP " UINT_FMT " %s", op, qstr_str(mp_binary_op_method_name[op]));
  453. } else {
  454. printf("code %p, byte code 0x%02x not implemented\n", ip, ip[-1]);
  455. assert(0);
  456. return ip;
  457. }
  458. break;
  459. }
  460. return ip;
  461. }
  462. void mp_bytecode_print2(const byte *ip, size_t len, const mp_uint_t *const_table) {
  463. mp_showbc_code_start = ip;
  464. mp_showbc_const_table = const_table;
  465. while (ip < len + mp_showbc_code_start) {
  466. printf("%02u ", (uint)(ip - mp_showbc_code_start));
  467. ip = mp_bytecode_print_str(ip);
  468. printf("\n");
  469. }
  470. }
  471. #endif // MICROPY_DEBUG_PRINTERS