showbc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. // Decode prelude
  74. MP_BC_PRELUDE_SIG_DECODE(ip);
  75. MP_BC_PRELUDE_SIZE_DECODE(ip);
  76. const byte *code_info = ip;
  77. #if MICROPY_PERSISTENT_CODE
  78. qstr block_name = code_info[0] | (code_info[1] << 8);
  79. qstr source_file = code_info[2] | (code_info[3] << 8);
  80. code_info += 4;
  81. #else
  82. qstr block_name = mp_decode_uint(&code_info);
  83. qstr source_file = mp_decode_uint(&code_info);
  84. #endif
  85. printf("File %s, code block '%s' (descriptor: %p, bytecode @%p " UINT_FMT " bytes)\n",
  86. qstr_str(source_file), qstr_str(block_name), descr, mp_showbc_code_start, len);
  87. // raw bytecode dump
  88. size_t prelude_size = ip - mp_showbc_code_start + n_info + n_cell;
  89. printf("Raw bytecode (code_info_size=" UINT_FMT ", bytecode_size=" UINT_FMT "):\n",
  90. prelude_size, len - prelude_size);
  91. for (mp_uint_t i = 0; i < len; i++) {
  92. if (i > 0 && i % 16 == 0) {
  93. printf("\n");
  94. }
  95. printf(" %02x", mp_showbc_code_start[i]);
  96. }
  97. printf("\n");
  98. // bytecode prelude: arg names (as qstr objects)
  99. printf("arg names:");
  100. for (mp_uint_t i = 0; i < n_pos_args + n_kwonly_args; i++) {
  101. printf(" %s", qstr_str(MP_OBJ_QSTR_VALUE(const_table[i])));
  102. }
  103. printf("\n");
  104. printf("(N_STATE %u)\n", (unsigned)n_state);
  105. printf("(N_EXC_STACK %u)\n", (unsigned)n_exc_stack);
  106. // skip over code_info
  107. ip += n_info;
  108. // bytecode prelude: initialise closed over variables
  109. for (size_t i = 0; i < n_cell; ++i) {
  110. uint local_num = *ip++;
  111. printf("(INIT_CELL %u)\n", local_num);
  112. }
  113. // print out line number info
  114. {
  115. mp_int_t bc = 0;
  116. mp_uint_t source_line = 1;
  117. printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
  118. for (const byte* ci = code_info; *ci;) {
  119. if ((ci[0] & 0x80) == 0) {
  120. // 0b0LLBBBBB encoding
  121. bc += ci[0] & 0x1f;
  122. source_line += ci[0] >> 5;
  123. ci += 1;
  124. } else {
  125. // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
  126. bc += ci[0] & 0xf;
  127. source_line += ((ci[0] << 4) & 0x700) | ci[1];
  128. ci += 2;
  129. }
  130. printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
  131. }
  132. }
  133. mp_bytecode_print2(ip, len - prelude_size, const_table);
  134. }
  135. const byte *mp_bytecode_print_str(const byte *ip) {
  136. mp_uint_t unum;
  137. qstr qst;
  138. switch (*ip++) {
  139. case MP_BC_LOAD_CONST_FALSE:
  140. printf("LOAD_CONST_FALSE");
  141. break;
  142. case MP_BC_LOAD_CONST_NONE:
  143. printf("LOAD_CONST_NONE");
  144. break;
  145. case MP_BC_LOAD_CONST_TRUE:
  146. printf("LOAD_CONST_TRUE");
  147. break;
  148. case MP_BC_LOAD_CONST_SMALL_INT: {
  149. mp_int_t num = 0;
  150. if ((ip[0] & 0x40) != 0) {
  151. // Number is negative
  152. num--;
  153. }
  154. do {
  155. num = (num << 7) | (*ip & 0x7f);
  156. } while ((*ip++ & 0x80) != 0);
  157. printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
  158. break;
  159. }
  160. case MP_BC_LOAD_CONST_STRING:
  161. DECODE_QSTR;
  162. printf("LOAD_CONST_STRING '%s'", qstr_str(qst));
  163. break;
  164. case MP_BC_LOAD_CONST_OBJ:
  165. DECODE_OBJ;
  166. printf("LOAD_CONST_OBJ %p=", MP_OBJ_TO_PTR(unum));
  167. mp_obj_print_helper(&mp_plat_print, (mp_obj_t)unum, PRINT_REPR);
  168. break;
  169. case MP_BC_LOAD_NULL:
  170. printf("LOAD_NULL");
  171. break;
  172. case MP_BC_LOAD_FAST_N:
  173. DECODE_UINT;
  174. printf("LOAD_FAST_N " UINT_FMT, unum);
  175. break;
  176. case MP_BC_LOAD_DEREF:
  177. DECODE_UINT;
  178. printf("LOAD_DEREF " UINT_FMT, unum);
  179. break;
  180. case MP_BC_LOAD_NAME:
  181. DECODE_QSTR;
  182. printf("LOAD_NAME %s", qstr_str(qst));
  183. if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
  184. printf(" (cache=%u)", *ip++);
  185. }
  186. break;
  187. case MP_BC_LOAD_GLOBAL:
  188. DECODE_QSTR;
  189. printf("LOAD_GLOBAL %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_ATTR:
  195. DECODE_QSTR;
  196. printf("LOAD_ATTR %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_METHOD:
  202. DECODE_QSTR;
  203. printf("LOAD_METHOD %s", qstr_str(qst));
  204. break;
  205. case MP_BC_LOAD_SUPER_METHOD:
  206. DECODE_QSTR;
  207. printf("LOAD_SUPER_METHOD %s", qstr_str(qst));
  208. break;
  209. case MP_BC_LOAD_BUILD_CLASS:
  210. printf("LOAD_BUILD_CLASS");
  211. break;
  212. case MP_BC_LOAD_SUBSCR:
  213. printf("LOAD_SUBSCR");
  214. break;
  215. case MP_BC_STORE_FAST_N:
  216. DECODE_UINT;
  217. printf("STORE_FAST_N " UINT_FMT, unum);
  218. break;
  219. case MP_BC_STORE_DEREF:
  220. DECODE_UINT;
  221. printf("STORE_DEREF " UINT_FMT, unum);
  222. break;
  223. case MP_BC_STORE_NAME:
  224. DECODE_QSTR;
  225. printf("STORE_NAME %s", qstr_str(qst));
  226. break;
  227. case MP_BC_STORE_GLOBAL:
  228. DECODE_QSTR;
  229. printf("STORE_GLOBAL %s", qstr_str(qst));
  230. break;
  231. case MP_BC_STORE_ATTR:
  232. DECODE_QSTR;
  233. printf("STORE_ATTR %s", qstr_str(qst));
  234. if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
  235. printf(" (cache=%u)", *ip++);
  236. }
  237. break;
  238. case MP_BC_STORE_SUBSCR:
  239. printf("STORE_SUBSCR");
  240. break;
  241. case MP_BC_DELETE_FAST:
  242. DECODE_UINT;
  243. printf("DELETE_FAST " UINT_FMT, unum);
  244. break;
  245. case MP_BC_DELETE_DEREF:
  246. DECODE_UINT;
  247. printf("DELETE_DEREF " UINT_FMT, unum);
  248. break;
  249. case MP_BC_DELETE_NAME:
  250. DECODE_QSTR;
  251. printf("DELETE_NAME %s", qstr_str(qst));
  252. break;
  253. case MP_BC_DELETE_GLOBAL:
  254. DECODE_QSTR;
  255. printf("DELETE_GLOBAL %s", qstr_str(qst));
  256. break;
  257. case MP_BC_DUP_TOP:
  258. printf("DUP_TOP");
  259. break;
  260. case MP_BC_DUP_TOP_TWO:
  261. printf("DUP_TOP_TWO");
  262. break;
  263. case MP_BC_POP_TOP:
  264. printf("POP_TOP");
  265. break;
  266. case MP_BC_ROT_TWO:
  267. printf("ROT_TWO");
  268. break;
  269. case MP_BC_ROT_THREE:
  270. printf("ROT_THREE");
  271. break;
  272. case MP_BC_JUMP:
  273. DECODE_SLABEL;
  274. printf("JUMP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  275. break;
  276. case MP_BC_POP_JUMP_IF_TRUE:
  277. DECODE_SLABEL;
  278. printf("POP_JUMP_IF_TRUE " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  279. break;
  280. case MP_BC_POP_JUMP_IF_FALSE:
  281. DECODE_SLABEL;
  282. printf("POP_JUMP_IF_FALSE " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  283. break;
  284. case MP_BC_JUMP_IF_TRUE_OR_POP:
  285. DECODE_SLABEL;
  286. printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  287. break;
  288. case MP_BC_JUMP_IF_FALSE_OR_POP:
  289. DECODE_SLABEL;
  290. printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  291. break;
  292. case MP_BC_SETUP_WITH:
  293. DECODE_ULABEL; // loop-like labels are always forward
  294. printf("SETUP_WITH " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  295. break;
  296. case MP_BC_WITH_CLEANUP:
  297. printf("WITH_CLEANUP");
  298. break;
  299. case MP_BC_UNWIND_JUMP:
  300. DECODE_SLABEL;
  301. printf("UNWIND_JUMP " UINT_FMT " %d", (mp_uint_t)(ip + unum - mp_showbc_code_start), *ip);
  302. ip += 1;
  303. break;
  304. case MP_BC_SETUP_EXCEPT:
  305. DECODE_ULABEL; // except labels are always forward
  306. printf("SETUP_EXCEPT " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  307. break;
  308. case MP_BC_SETUP_FINALLY:
  309. DECODE_ULABEL; // except labels are always forward
  310. printf("SETUP_FINALLY " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  311. break;
  312. case MP_BC_END_FINALLY:
  313. // if TOS is an exception, reraises the exception (3 values on TOS)
  314. // if TOS is an integer, does something else
  315. // if TOS is None, just pops it and continues
  316. // else error
  317. printf("END_FINALLY");
  318. break;
  319. case MP_BC_GET_ITER:
  320. printf("GET_ITER");
  321. break;
  322. case MP_BC_GET_ITER_STACK:
  323. printf("GET_ITER_STACK");
  324. break;
  325. case MP_BC_FOR_ITER:
  326. DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
  327. printf("FOR_ITER " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  328. break;
  329. case MP_BC_POP_EXCEPT_JUMP:
  330. DECODE_ULABEL; // these labels are always forward
  331. printf("POP_EXCEPT_JUMP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
  332. break;
  333. case MP_BC_BUILD_TUPLE:
  334. DECODE_UINT;
  335. printf("BUILD_TUPLE " UINT_FMT, unum);
  336. break;
  337. case MP_BC_BUILD_LIST:
  338. DECODE_UINT;
  339. printf("BUILD_LIST " UINT_FMT, unum);
  340. break;
  341. case MP_BC_BUILD_MAP:
  342. DECODE_UINT;
  343. printf("BUILD_MAP " UINT_FMT, unum);
  344. break;
  345. case MP_BC_STORE_MAP:
  346. printf("STORE_MAP");
  347. break;
  348. case MP_BC_BUILD_SET:
  349. DECODE_UINT;
  350. printf("BUILD_SET " UINT_FMT, unum);
  351. break;
  352. #if MICROPY_PY_BUILTINS_SLICE
  353. case MP_BC_BUILD_SLICE:
  354. DECODE_UINT;
  355. printf("BUILD_SLICE " UINT_FMT, unum);
  356. break;
  357. #endif
  358. case MP_BC_STORE_COMP:
  359. DECODE_UINT;
  360. printf("STORE_COMP " UINT_FMT, unum);
  361. break;
  362. case MP_BC_UNPACK_SEQUENCE:
  363. DECODE_UINT;
  364. printf("UNPACK_SEQUENCE " UINT_FMT, unum);
  365. break;
  366. case MP_BC_UNPACK_EX:
  367. DECODE_UINT;
  368. printf("UNPACK_EX " UINT_FMT, unum);
  369. break;
  370. case MP_BC_MAKE_FUNCTION:
  371. DECODE_PTR;
  372. printf("MAKE_FUNCTION %p", (void*)(uintptr_t)unum);
  373. break;
  374. case MP_BC_MAKE_FUNCTION_DEFARGS:
  375. DECODE_PTR;
  376. printf("MAKE_FUNCTION_DEFARGS %p", (void*)(uintptr_t)unum);
  377. break;
  378. case MP_BC_MAKE_CLOSURE: {
  379. DECODE_PTR;
  380. mp_uint_t n_closed_over = *ip++;
  381. printf("MAKE_CLOSURE %p " UINT_FMT, (void*)(uintptr_t)unum, n_closed_over);
  382. break;
  383. }
  384. case MP_BC_MAKE_CLOSURE_DEFARGS: {
  385. DECODE_PTR;
  386. mp_uint_t n_closed_over = *ip++;
  387. printf("MAKE_CLOSURE_DEFARGS %p " UINT_FMT, (void*)(uintptr_t)unum, n_closed_over);
  388. break;
  389. }
  390. case MP_BC_CALL_FUNCTION:
  391. DECODE_UINT;
  392. printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
  393. break;
  394. case MP_BC_CALL_FUNCTION_VAR_KW:
  395. DECODE_UINT;
  396. printf("CALL_FUNCTION_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
  397. break;
  398. case MP_BC_CALL_METHOD:
  399. DECODE_UINT;
  400. printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
  401. break;
  402. case MP_BC_CALL_METHOD_VAR_KW:
  403. DECODE_UINT;
  404. printf("CALL_METHOD_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
  405. break;
  406. case MP_BC_RETURN_VALUE:
  407. printf("RETURN_VALUE");
  408. break;
  409. case MP_BC_RAISE_LAST:
  410. printf("RAISE_LAST");
  411. break;
  412. case MP_BC_RAISE_OBJ:
  413. printf("RAISE_OBJ");
  414. break;
  415. case MP_BC_RAISE_FROM:
  416. printf("RAISE_FROM");
  417. break;
  418. case MP_BC_YIELD_VALUE:
  419. printf("YIELD_VALUE");
  420. break;
  421. case MP_BC_YIELD_FROM:
  422. printf("YIELD_FROM");
  423. break;
  424. case MP_BC_IMPORT_NAME:
  425. DECODE_QSTR;
  426. printf("IMPORT_NAME '%s'", qstr_str(qst));
  427. break;
  428. case MP_BC_IMPORT_FROM:
  429. DECODE_QSTR;
  430. printf("IMPORT_FROM '%s'", qstr_str(qst));
  431. break;
  432. case MP_BC_IMPORT_STAR:
  433. printf("IMPORT_STAR");
  434. break;
  435. default:
  436. if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) {
  437. printf("LOAD_CONST_SMALL_INT " INT_FMT, (mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16);
  438. } else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) {
  439. printf("LOAD_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI);
  440. } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) {
  441. printf("STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI);
  442. } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NUM_BYTECODE) {
  443. printf("UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI);
  444. } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + MP_BINARY_OP_NUM_BYTECODE) {
  445. mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI;
  446. printf("BINARY_OP " UINT_FMT " %s", op, qstr_str(mp_binary_op_method_name[op]));
  447. } else {
  448. printf("code %p, byte code 0x%02x not implemented\n", ip - 1, ip[-1]);
  449. assert(0);
  450. return ip;
  451. }
  452. break;
  453. }
  454. return ip;
  455. }
  456. void mp_bytecode_print2(const byte *ip, size_t len, const mp_uint_t *const_table) {
  457. mp_showbc_code_start = ip;
  458. mp_showbc_const_table = const_table;
  459. while (ip < len + mp_showbc_code_start) {
  460. printf("%02u ", (uint)(ip - mp_showbc_code_start));
  461. ip = mp_bytecode_print_str(ip);
  462. printf("\n");
  463. }
  464. }
  465. #endif // MICROPY_DEBUG_PRINTERS