blob: 3deb18cd31dcf61ac33d186c12e3b5d04592d12e [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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
Damienf03001f2013-11-17 13:19:33 +000027#include <stdio.h>
Damienf03001f2013-11-17 13:19:33 +000028#include <assert.h>
29
Damien George51dfcb42015-01-01 20:27:54 +000030#include "py/bc0.h"
31#include "py/bc.h"
32
Damien Georgecbd2f742014-01-19 11:48:48 +000033#if MICROPY_DEBUG_PRINTERS
Damien Georged3ebe482014-01-07 15:20:33 +000034
Damien Georgefbddea92016-09-20 11:30:54 +100035// redirect all printfs in this file to the platform print stream
36#define printf(...) mp_printf(&mp_plat_print, __VA_ARGS__)
37
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020038#define DECODE_UINT { \
39 unum = 0; \
40 do { \
41 unum = (unum << 7) + (*ip & 0x7f); \
42 } while ((*ip++ & 0x80) != 0); \
43}
Damienf03001f2013-11-17 13:19:33 +000044#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
45#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
Damien Georgec8e9c0d2015-11-02 17:27:18 +000046
47#if MICROPY_PERSISTENT_CODE
48
49#define DECODE_QSTR \
50 qst = ip[0] | ip[1] << 8; \
51 ip += 2;
52#define DECODE_PTR \
53 DECODE_UINT; \
54 unum = mp_showbc_const_table[unum]
Damien George999cedb2015-11-27 17:01:44 +000055#define DECODE_OBJ \
56 DECODE_UINT; \
57 unum = mp_showbc_const_table[unum]
Damien Georgec8e9c0d2015-11-02 17:27:18 +000058
59#else
60
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020061#define DECODE_QSTR { \
Damien George50912e72015-01-20 11:55:10 +000062 qst = 0; \
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020063 do { \
Damien George50912e72015-01-20 11:55:10 +000064 qst = (qst << 7) + (*ip & 0x7f); \
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020065 } while ((*ip++ & 0x80) != 0); \
66}
Damien George3d484d92014-04-13 11:22:44 +010067#define DECODE_PTR do { \
Damien George999cedb2015-11-27 17:01:44 +000068 ip = (byte*)MP_ALIGN(ip, sizeof(void*)); \
69 unum = (uintptr_t)*(void**)ip; \
70 ip += sizeof(void*); \
71} while (0)
72#define DECODE_OBJ do { \
73 ip = (byte*)MP_ALIGN(ip, sizeof(mp_obj_t)); \
74 unum = (mp_uint_t)*(mp_obj_t*)ip; \
75 ip += sizeof(mp_obj_t); \
Damien George3d484d92014-04-13 11:22:44 +010076} while (0)
Damienf03001f2013-11-17 13:19:33 +000077
Damien Georgec8e9c0d2015-11-02 17:27:18 +000078#endif
79
Paul Sokolovskydf103462014-12-28 21:34:45 +020080const byte *mp_showbc_code_start;
Damien Georgec8e9c0d2015-11-02 17:27:18 +000081const mp_uint_t *mp_showbc_const_table;
Paul Sokolovsky343266e2014-12-27 05:00:08 +020082
Damien George713ea182015-10-23 01:23:11 +010083void mp_bytecode_print(const void *descr, const byte *ip, mp_uint_t len, const mp_uint_t *const_table) {
Paul Sokolovskydf103462014-12-28 21:34:45 +020084 mp_showbc_code_start = ip;
Damien George6baf76e2013-12-30 22:32:17 +000085
Damien George3a3db4d2015-10-22 23:45:37 +010086 // get bytecode parameters
Damien George9b7f5832015-03-18 17:47:47 +000087 mp_uint_t n_state = mp_decode_uint(&ip);
88 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
Damien George3a3db4d2015-10-22 23:45:37 +010089 /*mp_uint_t scope_flags =*/ ip++;
90 mp_uint_t n_pos_args = *ip++;
91 mp_uint_t n_kwonly_args = *ip++;
92 /*mp_uint_t n_def_pos_args =*/ ip++;
Damien George9b7f5832015-03-18 17:47:47 +000093
Damien George73496fb2014-04-13 14:51:56 +010094 const byte *code_info = ip;
Damien Georgeb534e1b2014-09-04 14:44:01 +010095 mp_uint_t code_info_size = mp_decode_uint(&code_info);
Damien George08335002014-01-18 23:24:36 +000096 ip += code_info_size;
97
Damien Georgec8e9c0d2015-11-02 17:27:18 +000098 #if MICROPY_PERSISTENT_CODE
99 qstr block_name = code_info[0] | (code_info[1] << 8);
100 qstr source_file = code_info[2] | (code_info[3] << 8);
Damien Georgefbddea92016-09-20 11:30:54 +1000101 code_info += 4;
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000102 #else
Damien Georgeb534e1b2014-09-04 14:44:01 +0100103 qstr block_name = mp_decode_uint(&code_info);
104 qstr source_file = mp_decode_uint(&code_info);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000105 #endif
Damien George3eaa0c32014-10-03 17:54:25 +0000106 printf("File %s, code block '%s' (descriptor: %p, bytecode @%p " UINT_FMT " bytes)\n",
Damien George9b7f5832015-03-18 17:47:47 +0000107 qstr_str(source_file), qstr_str(block_name), descr, mp_showbc_code_start, len);
Paul Sokolovsky8bf84042014-06-02 16:11:16 +0300108
Damien George564963a2014-10-24 14:42:50 +0000109 // raw bytecode dump
110 printf("Raw bytecode (code_info_size=" UINT_FMT ", bytecode_size=" UINT_FMT "):\n", code_info_size, len - code_info_size);
111 for (mp_uint_t i = 0; i < len; i++) {
112 if (i > 0 && i % 16 == 0) {
113 printf("\n");
114 }
Paul Sokolovskydf103462014-12-28 21:34:45 +0200115 printf(" %02x", mp_showbc_code_start[i]);
Damien George564963a2014-10-24 14:42:50 +0000116 }
117 printf("\n");
118
Damien George1084b0f2014-10-25 15:07:02 +0100119 // bytecode prelude: arg names (as qstr objects)
120 printf("arg names:");
Damien George3a3db4d2015-10-22 23:45:37 +0100121 for (mp_uint_t i = 0; i < n_pos_args + n_kwonly_args; i++) {
Damien George713ea182015-10-23 01:23:11 +0100122 printf(" %s", qstr_str(MP_OBJ_QSTR_VALUE(const_table[i])));
Damien George1084b0f2014-10-25 15:07:02 +0100123 }
124 printf("\n");
125
Damien George9b7f5832015-03-18 17:47:47 +0000126 printf("(N_STATE " UINT_FMT ")\n", n_state);
127 printf("(N_EXC_STACK " UINT_FMT ")\n", n_exc_stack);
128
129 // for printing line number info
130 const byte *bytecode_start = ip;
Damien George440f0412014-03-28 18:38:20 +0000131
132 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000133 {
Damien Georgec9aa1882015-04-07 00:08:17 +0100134 uint local_num;
135 while ((local_num = *ip++) != 255) {
Damien George6baf76e2013-12-30 22:32:17 +0000136 printf("(INIT_CELL %u)\n", local_num);
137 }
Paul Sokolovskydf103462014-12-28 21:34:45 +0200138 len -= ip - mp_showbc_code_start;
Damien George6baf76e2013-12-30 22:32:17 +0000139 }
140
Damien George73496fb2014-04-13 14:51:56 +0100141 // print out line number info
142 {
Damien George9b7f5832015-03-18 17:47:47 +0000143 mp_int_t bc = bytecode_start - ip;
Damien George40f3c022014-07-03 13:25:24 +0100144 mp_uint_t source_line = 1;
Damien George73496fb2014-04-13 14:51:56 +0100145 printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
Damien George564963a2014-10-24 14:42:50 +0000146 for (const byte* ci = code_info; *ci;) {
Damien George4747bec2014-07-31 16:12:01 +0000147 if ((ci[0] & 0x80) == 0) {
148 // 0b0LLBBBBB encoding
149 bc += ci[0] & 0x1f;
150 source_line += ci[0] >> 5;
151 ci += 1;
152 } else {
153 // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
154 bc += ci[0] & 0xf;
155 source_line += ((ci[0] << 4) & 0x700) | ci[1];
156 ci += 2;
157 }
Damien George73496fb2014-04-13 14:51:56 +0100158 printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
159 }
160 }
Damien Georgecc4c1ad2017-01-27 12:34:09 +1100161 mp_bytecode_print2(ip, len - 0, const_table);
Paul Sokolovskyc5e32c62014-04-23 03:40:24 +0300162}
Damien George73496fb2014-04-13 14:51:56 +0100163
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200164const byte *mp_bytecode_print_str(const byte *ip) {
Damien George40f3c022014-07-03 13:25:24 +0100165 mp_uint_t unum;
Damien George50912e72015-01-20 11:55:10 +0000166 qstr qst;
Damienf03001f2013-11-17 13:19:33 +0000167
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200168 switch (*ip++) {
169 case MP_BC_LOAD_CONST_FALSE:
170 printf("LOAD_CONST_FALSE");
171 break;
Damienf03001f2013-11-17 13:19:33 +0000172
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200173 case MP_BC_LOAD_CONST_NONE:
174 printf("LOAD_CONST_NONE");
175 break;
Damienf03001f2013-11-17 13:19:33 +0000176
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200177 case MP_BC_LOAD_CONST_TRUE:
178 printf("LOAD_CONST_TRUE");
179 break;
Damien Georgee9906ac2014-01-04 18:44:46 +0000180
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200181 case MP_BC_LOAD_CONST_SMALL_INT: {
182 mp_int_t num = 0;
183 if ((ip[0] & 0x40) != 0) {
184 // Number is negative
185 num--;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200186 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200187 do {
188 num = (num << 7) | (*ip & 0x7f);
189 } while ((*ip++ & 0x80) != 0);
190 printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
191 break;
192 }
Damienf03001f2013-11-17 13:19:33 +0000193
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200194 case MP_BC_LOAD_CONST_STRING:
195 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000196 printf("LOAD_CONST_STRING '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200197 break;
Damienf03001f2013-11-17 13:19:33 +0000198
Damien Georged6ed6702015-01-13 23:08:47 +0000199 case MP_BC_LOAD_CONST_OBJ:
Damien George999cedb2015-11-27 17:01:44 +0000200 DECODE_OBJ;
201 printf("LOAD_CONST_OBJ %p=", MP_OBJ_TO_PTR(unum));
Damien George59fba2d2015-06-25 14:42:13 +0000202 mp_obj_print_helper(&mp_plat_print, (mp_obj_t)unum, PRINT_REPR);
Damien Georged6ed6702015-01-13 23:08:47 +0000203 break;
204
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200205 case MP_BC_LOAD_NULL:
206 printf("LOAD_NULL");
207 break;
Paul Sokolovsky00a9d132014-04-12 00:32:38 +0300208
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200209 case MP_BC_LOAD_FAST_N:
210 DECODE_UINT;
211 printf("LOAD_FAST_N " UINT_FMT, unum);
212 break;
Damienf03001f2013-11-17 13:19:33 +0000213
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200214 case MP_BC_LOAD_DEREF:
215 DECODE_UINT;
216 printf("LOAD_DEREF " UINT_FMT, unum);
217 break;
Damien George6baf76e2013-12-30 22:32:17 +0000218
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200219 case MP_BC_LOAD_NAME:
220 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000221 printf("LOAD_NAME %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000222 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
223 printf(" (cache=%u)", *ip++);
224 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200225 break;
Damienf03001f2013-11-17 13:19:33 +0000226
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200227 case MP_BC_LOAD_GLOBAL:
228 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000229 printf("LOAD_GLOBAL %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000230 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
231 printf(" (cache=%u)", *ip++);
232 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200233 break;
Damienf03001f2013-11-17 13:19:33 +0000234
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200235 case MP_BC_LOAD_ATTR:
236 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000237 printf("LOAD_ATTR %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000238 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
239 printf(" (cache=%u)", *ip++);
240 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200241 break;
Damienf03001f2013-11-17 13:19:33 +0000242
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200243 case MP_BC_LOAD_METHOD:
244 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000245 printf("LOAD_METHOD %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200246 break;
Damienf03001f2013-11-17 13:19:33 +0000247
Damien Georgedd11af22017-04-19 09:45:59 +1000248 case MP_BC_LOAD_SUPER_METHOD:
249 DECODE_QSTR;
250 printf("LOAD_SUPER_METHOD %s", qstr_str(qst));
251 break;
252
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200253 case MP_BC_LOAD_BUILD_CLASS:
254 printf("LOAD_BUILD_CLASS");
255 break;
Damienf03001f2013-11-17 13:19:33 +0000256
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200257 case MP_BC_LOAD_SUBSCR:
258 printf("LOAD_SUBSCR");
259 break;
Damien George729f7b42014-04-17 22:10:53 +0100260
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200261 case MP_BC_STORE_FAST_N:
262 DECODE_UINT;
263 printf("STORE_FAST_N " UINT_FMT, unum);
264 break;
Damienf03001f2013-11-17 13:19:33 +0000265
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200266 case MP_BC_STORE_DEREF:
267 DECODE_UINT;
268 printf("STORE_DEREF " UINT_FMT, unum);
269 break;
Damien George6baf76e2013-12-30 22:32:17 +0000270
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200271 case MP_BC_STORE_NAME:
272 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000273 printf("STORE_NAME %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200274 break;
Damienf03001f2013-11-17 13:19:33 +0000275
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200276 case MP_BC_STORE_GLOBAL:
277 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000278 printf("STORE_GLOBAL %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200279 break;
Damienf03001f2013-11-17 13:19:33 +0000280
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200281 case MP_BC_STORE_ATTR:
282 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000283 printf("STORE_ATTR %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000284 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
285 printf(" (cache=%u)", *ip++);
286 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200287 break;
Damienf03001f2013-11-17 13:19:33 +0000288
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200289 case MP_BC_STORE_SUBSCR:
290 printf("STORE_SUBSCR");
291 break;
Damienf03001f2013-11-17 13:19:33 +0000292
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200293 case MP_BC_DELETE_FAST:
294 DECODE_UINT;
295 printf("DELETE_FAST " UINT_FMT, unum);
296 break;
Damien George2bf7c092014-04-09 15:26:46 +0100297
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200298 case MP_BC_DELETE_DEREF:
299 DECODE_UINT;
300 printf("DELETE_DEREF " UINT_FMT, unum);
301 break;
Damien George2bf7c092014-04-09 15:26:46 +0100302
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200303 case MP_BC_DELETE_NAME:
304 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000305 printf("DELETE_NAME %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200306 break;
Damien Georgeddaf6c12014-02-06 20:31:32 +0000307
Damien George8e9a7122015-03-20 17:12:09 +0000308 case MP_BC_DELETE_GLOBAL:
309 DECODE_QSTR;
310 printf("DELETE_GLOBAL %s", qstr_str(qst));
311 break;
312
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200313 case MP_BC_DUP_TOP:
314 printf("DUP_TOP");
315 break;
Damienf03001f2013-11-17 13:19:33 +0000316
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200317 case MP_BC_DUP_TOP_TWO:
318 printf("DUP_TOP_TWO");
319 break;
Damienf03001f2013-11-17 13:19:33 +0000320
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200321 case MP_BC_POP_TOP:
322 printf("POP_TOP");
323 break;
Damienf03001f2013-11-17 13:19:33 +0000324
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200325 case MP_BC_ROT_TWO:
326 printf("ROT_TWO");
327 break;
Damienf03001f2013-11-17 13:19:33 +0000328
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200329 case MP_BC_ROT_THREE:
330 printf("ROT_THREE");
331 break;
Damienf03001f2013-11-17 13:19:33 +0000332
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200333 case MP_BC_JUMP:
334 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000335 printf("JUMP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200336 break;
Damienf03001f2013-11-17 13:19:33 +0000337
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200338 case MP_BC_POP_JUMP_IF_TRUE:
339 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000340 printf("POP_JUMP_IF_TRUE " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200341 break;
Damienf03001f2013-11-17 13:19:33 +0000342
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200343 case MP_BC_POP_JUMP_IF_FALSE:
344 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000345 printf("POP_JUMP_IF_FALSE " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200346 break;
Damienf03001f2013-11-17 13:19:33 +0000347
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200348 case MP_BC_JUMP_IF_TRUE_OR_POP:
349 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000350 printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200351 break;
Damienf03001f2013-11-17 13:19:33 +0000352
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200353 case MP_BC_JUMP_IF_FALSE_OR_POP:
354 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000355 printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200356 break;
Damienf03001f2013-11-17 13:19:33 +0000357
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200358 case MP_BC_SETUP_WITH:
359 DECODE_ULABEL; // loop-like labels are always forward
Damien George999cedb2015-11-27 17:01:44 +0000360 printf("SETUP_WITH " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200361 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200362
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200363 case MP_BC_WITH_CLEANUP:
364 printf("WITH_CLEANUP");
365 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200366
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200367 case MP_BC_UNWIND_JUMP:
368 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000369 printf("UNWIND_JUMP " UINT_FMT " %d", (mp_uint_t)(ip + unum - mp_showbc_code_start), *ip);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200370 ip += 1;
371 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200372
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200373 case MP_BC_SETUP_EXCEPT:
374 DECODE_ULABEL; // except labels are always forward
Damien George999cedb2015-11-27 17:01:44 +0000375 printf("SETUP_EXCEPT " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200376 break;
Damienf03001f2013-11-17 13:19:33 +0000377
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200378 case MP_BC_SETUP_FINALLY:
379 DECODE_ULABEL; // except labels are always forward
Damien George999cedb2015-11-27 17:01:44 +0000380 printf("SETUP_FINALLY " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200381 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200382
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200383 case MP_BC_END_FINALLY:
384 // if TOS is an exception, reraises the exception (3 values on TOS)
385 // if TOS is an integer, does something else
386 // if TOS is None, just pops it and continues
387 // else error
388 printf("END_FINALLY");
389 break;
Damienf03001f2013-11-17 13:19:33 +0000390
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200391 case MP_BC_GET_ITER:
392 printf("GET_ITER");
393 break;
Damienf03001f2013-11-17 13:19:33 +0000394
Damien Georgef4df3aa2016-01-09 23:59:52 +0000395 case MP_BC_GET_ITER_STACK:
396 printf("GET_ITER_STACK");
397 break;
398
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200399 case MP_BC_FOR_ITER:
400 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damien George999cedb2015-11-27 17:01:44 +0000401 printf("FOR_ITER " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200402 break;
Damienf03001f2013-11-17 13:19:33 +0000403
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200404 case MP_BC_POP_BLOCK:
405 // pops block and restores the stack
406 printf("POP_BLOCK");
407 break;
Damienf03001f2013-11-17 13:19:33 +0000408
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200409 case MP_BC_POP_EXCEPT:
410 // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
411 printf("POP_EXCEPT");
412 break;
Damienf03001f2013-11-17 13:19:33 +0000413
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200414 case MP_BC_BUILD_TUPLE:
415 DECODE_UINT;
416 printf("BUILD_TUPLE " UINT_FMT, unum);
417 break;
Damienf03001f2013-11-17 13:19:33 +0000418
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200419 case MP_BC_BUILD_LIST:
420 DECODE_UINT;
421 printf("BUILD_LIST " UINT_FMT, unum);
422 break;
Damienf03001f2013-11-17 13:19:33 +0000423
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200424 case MP_BC_BUILD_MAP:
425 DECODE_UINT;
426 printf("BUILD_MAP " UINT_FMT, unum);
427 break;
Damienf03001f2013-11-17 13:19:33 +0000428
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200429 case MP_BC_STORE_MAP:
430 printf("STORE_MAP");
431 break;
Damienf03001f2013-11-17 13:19:33 +0000432
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200433 case MP_BC_BUILD_SET:
434 DECODE_UINT;
435 printf("BUILD_SET " UINT_FMT, unum);
436 break;
Damienf03001f2013-11-17 13:19:33 +0000437
Damien Georgefb510b32014-06-01 13:32:54 +0100438#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200439 case MP_BC_BUILD_SLICE:
440 DECODE_UINT;
441 printf("BUILD_SLICE " UINT_FMT, unum);
442 break;
Damien George20006db2014-01-18 14:10:48 +0000443#endif
444
Damien Georgeadaf0d82016-09-19 08:46:01 +1000445 case MP_BC_STORE_COMP:
446 DECODE_UINT;
447 printf("STORE_COMP " UINT_FMT, unum);
448 break;
449
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200450 case MP_BC_UNPACK_SEQUENCE:
451 DECODE_UINT;
452 printf("UNPACK_SEQUENCE " UINT_FMT, unum);
453 break;
Damienff099f32013-11-26 15:14:50 +0000454
Damien Georgec8870b72015-06-18 15:12:17 +0000455 case MP_BC_UNPACK_EX:
456 DECODE_UINT;
457 printf("UNPACK_EX " UINT_FMT, unum);
458 break;
459
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200460 case MP_BC_MAKE_FUNCTION:
461 DECODE_PTR;
Damien George999cedb2015-11-27 17:01:44 +0000462 printf("MAKE_FUNCTION %p", (void*)(uintptr_t)unum);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200463 break;
Damienf03001f2013-11-17 13:19:33 +0000464
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200465 case MP_BC_MAKE_FUNCTION_DEFARGS:
466 DECODE_PTR;
Damien George999cedb2015-11-27 17:01:44 +0000467 printf("MAKE_FUNCTION_DEFARGS %p", (void*)(uintptr_t)unum);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200468 break;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200469
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200470 case MP_BC_MAKE_CLOSURE: {
471 DECODE_PTR;
472 mp_uint_t n_closed_over = *ip++;
Damien George999cedb2015-11-27 17:01:44 +0000473 printf("MAKE_CLOSURE %p " UINT_FMT, (void*)(uintptr_t)unum, n_closed_over);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200474 break;
Damienf03001f2013-11-17 13:19:33 +0000475 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200476
477 case MP_BC_MAKE_CLOSURE_DEFARGS: {
478 DECODE_PTR;
479 mp_uint_t n_closed_over = *ip++;
Damien George999cedb2015-11-27 17:01:44 +0000480 printf("MAKE_CLOSURE_DEFARGS %p " UINT_FMT, (void*)(uintptr_t)unum, n_closed_over);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200481 break;
482 }
483
484 case MP_BC_CALL_FUNCTION:
485 DECODE_UINT;
486 printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
487 break;
488
489 case MP_BC_CALL_FUNCTION_VAR_KW:
490 DECODE_UINT;
491 printf("CALL_FUNCTION_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
492 break;
493
494 case MP_BC_CALL_METHOD:
495 DECODE_UINT;
496 printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
497 break;
498
499 case MP_BC_CALL_METHOD_VAR_KW:
500 DECODE_UINT;
501 printf("CALL_METHOD_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
502 break;
503
504 case MP_BC_RETURN_VALUE:
505 printf("RETURN_VALUE");
506 break;
507
508 case MP_BC_RAISE_VARARGS:
509 unum = *ip++;
510 printf("RAISE_VARARGS " UINT_FMT, unum);
511 break;
512
513 case MP_BC_YIELD_VALUE:
514 printf("YIELD_VALUE");
515 break;
516
517 case MP_BC_YIELD_FROM:
518 printf("YIELD_FROM");
519 break;
520
521 case MP_BC_IMPORT_NAME:
522 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000523 printf("IMPORT_NAME '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200524 break;
525
526 case MP_BC_IMPORT_FROM:
527 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000528 printf("IMPORT_FROM '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200529 break;
530
531 case MP_BC_IMPORT_STAR:
532 printf("IMPORT_STAR");
533 break;
534
535 default:
536 if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) {
537 printf("LOAD_CONST_SMALL_INT " INT_FMT, (mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16);
538 } else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) {
539 printf("LOAD_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI);
540 } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) {
541 printf("STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI);
Damien George0864a692017-10-03 23:34:28 +1100542 } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NUM_BYTECODE) {
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200543 printf("UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI);
Damien George0864a692017-10-03 23:34:28 +1100544 } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + MP_BINARY_OP_NUM_BYTECODE) {
Paul Sokolovsky1ee17852014-12-28 21:41:58 +0200545 mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI;
546 printf("BINARY_OP " UINT_FMT " %s", op, qstr_str(mp_binary_op_method_name[op]));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200547 } else {
548 printf("code %p, byte code 0x%02x not implemented\n", ip, ip[-1]);
549 assert(0);
550 return ip;
551 }
552 break;
553 }
554
555 return ip;
556}
557
Damien Georgecc4c1ad2017-01-27 12:34:09 +1100558void mp_bytecode_print2(const byte *ip, size_t len, const mp_uint_t *const_table) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200559 mp_showbc_code_start = ip;
Damien Georgecc4c1ad2017-01-27 12:34:09 +1100560 mp_showbc_const_table = const_table;
Damien George963a5a32015-01-16 17:47:07 +0000561 while (ip < len + mp_showbc_code_start) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200562 printf("%02u ", (uint)(ip - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200563 ip = mp_bytecode_print_str(ip);
Damienf03001f2013-11-17 13:19:33 +0000564 printf("\n");
565 }
566}
Damien Georged3ebe482014-01-07 15:20:33 +0000567
Damien Georgecbd2f742014-01-19 11:48:48 +0000568#endif // MICROPY_DEBUG_PRINTERS