blob: 0e1edb60d716a3378a799d5b9bf984197ab170b8 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python 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
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
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020035#define DECODE_UINT { \
36 unum = 0; \
37 do { \
38 unum = (unum << 7) + (*ip & 0x7f); \
39 } while ((*ip++ & 0x80) != 0); \
40}
Damienf03001f2013-11-17 13:19:33 +000041#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
42#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
Damien Georgec8e9c0d2015-11-02 17:27:18 +000043
44#if MICROPY_PERSISTENT_CODE
45
46#define DECODE_QSTR \
47 qst = ip[0] | ip[1] << 8; \
48 ip += 2;
49#define DECODE_PTR \
50 DECODE_UINT; \
51 unum = mp_showbc_const_table[unum]
52
53#else
54
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020055#define DECODE_QSTR { \
Damien George50912e72015-01-20 11:55:10 +000056 qst = 0; \
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020057 do { \
Damien George50912e72015-01-20 11:55:10 +000058 qst = (qst << 7) + (*ip & 0x7f); \
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020059 } while ((*ip++ & 0x80) != 0); \
60}
Damien George3d484d92014-04-13 11:22:44 +010061#define DECODE_PTR do { \
Damien George40f3c022014-07-03 13:25:24 +010062 ip = (byte*)(((mp_uint_t)ip + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1))); /* align ip */ \
63 unum = *(mp_uint_t*)ip; \
64 ip += sizeof(mp_uint_t); \
Damien George3d484d92014-04-13 11:22:44 +010065} while (0)
Damienf03001f2013-11-17 13:19:33 +000066
Damien Georgec8e9c0d2015-11-02 17:27:18 +000067#endif
68
Paul Sokolovskydf103462014-12-28 21:34:45 +020069const byte *mp_showbc_code_start;
Damien Georgec8e9c0d2015-11-02 17:27:18 +000070const mp_uint_t *mp_showbc_const_table;
Paul Sokolovsky343266e2014-12-27 05:00:08 +020071
Damien George713ea182015-10-23 01:23:11 +010072void 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 +020073 mp_showbc_code_start = ip;
Damien Georgec8e9c0d2015-11-02 17:27:18 +000074 mp_showbc_const_table = const_table;
Damien George6baf76e2013-12-30 22:32:17 +000075
Damien George3a3db4d2015-10-22 23:45:37 +010076 // get bytecode parameters
Damien George9b7f5832015-03-18 17:47:47 +000077 mp_uint_t n_state = mp_decode_uint(&ip);
78 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
Damien George3a3db4d2015-10-22 23:45:37 +010079 /*mp_uint_t scope_flags =*/ ip++;
80 mp_uint_t n_pos_args = *ip++;
81 mp_uint_t n_kwonly_args = *ip++;
82 /*mp_uint_t n_def_pos_args =*/ ip++;
Damien George9b7f5832015-03-18 17:47:47 +000083
Damien George73496fb2014-04-13 14:51:56 +010084 const byte *code_info = ip;
Damien Georgeb534e1b2014-09-04 14:44:01 +010085 mp_uint_t code_info_size = mp_decode_uint(&code_info);
Damien George08335002014-01-18 23:24:36 +000086 ip += code_info_size;
87
Damien Georgec8e9c0d2015-11-02 17:27:18 +000088 #if MICROPY_PERSISTENT_CODE
89 qstr block_name = code_info[0] | (code_info[1] << 8);
90 qstr source_file = code_info[2] | (code_info[3] << 8);
91 #else
Damien Georgeb534e1b2014-09-04 14:44:01 +010092 qstr block_name = mp_decode_uint(&code_info);
93 qstr source_file = mp_decode_uint(&code_info);
Damien Georgec8e9c0d2015-11-02 17:27:18 +000094 #endif
Damien George3eaa0c32014-10-03 17:54:25 +000095 printf("File %s, code block '%s' (descriptor: %p, bytecode @%p " UINT_FMT " bytes)\n",
Damien George9b7f5832015-03-18 17:47:47 +000096 qstr_str(source_file), qstr_str(block_name), descr, mp_showbc_code_start, len);
Paul Sokolovsky8bf84042014-06-02 16:11:16 +030097
Damien George564963a2014-10-24 14:42:50 +000098 // raw bytecode dump
99 printf("Raw bytecode (code_info_size=" UINT_FMT ", bytecode_size=" UINT_FMT "):\n", code_info_size, len - code_info_size);
100 for (mp_uint_t i = 0; i < len; i++) {
101 if (i > 0 && i % 16 == 0) {
102 printf("\n");
103 }
Paul Sokolovskydf103462014-12-28 21:34:45 +0200104 printf(" %02x", mp_showbc_code_start[i]);
Damien George564963a2014-10-24 14:42:50 +0000105 }
106 printf("\n");
107
Damien George1084b0f2014-10-25 15:07:02 +0100108 // bytecode prelude: arg names (as qstr objects)
109 printf("arg names:");
Damien George3a3db4d2015-10-22 23:45:37 +0100110 for (mp_uint_t i = 0; i < n_pos_args + n_kwonly_args; i++) {
Damien George713ea182015-10-23 01:23:11 +0100111 printf(" %s", qstr_str(MP_OBJ_QSTR_VALUE(const_table[i])));
Damien George1084b0f2014-10-25 15:07:02 +0100112 }
113 printf("\n");
114
Damien George9b7f5832015-03-18 17:47:47 +0000115 printf("(N_STATE " UINT_FMT ")\n", n_state);
116 printf("(N_EXC_STACK " UINT_FMT ")\n", n_exc_stack);
117
118 // for printing line number info
119 const byte *bytecode_start = ip;
Damien George440f0412014-03-28 18:38:20 +0000120
121 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000122 {
Damien Georgec9aa1882015-04-07 00:08:17 +0100123 uint local_num;
124 while ((local_num = *ip++) != 255) {
Damien George6baf76e2013-12-30 22:32:17 +0000125 printf("(INIT_CELL %u)\n", local_num);
126 }
Paul Sokolovskydf103462014-12-28 21:34:45 +0200127 len -= ip - mp_showbc_code_start;
Damien George6baf76e2013-12-30 22:32:17 +0000128 }
129
Damien George73496fb2014-04-13 14:51:56 +0100130 // print out line number info
131 {
Damien George9b7f5832015-03-18 17:47:47 +0000132 mp_int_t bc = bytecode_start - ip;
Damien George40f3c022014-07-03 13:25:24 +0100133 mp_uint_t source_line = 1;
Damien George73496fb2014-04-13 14:51:56 +0100134 printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
Damien George564963a2014-10-24 14:42:50 +0000135 for (const byte* ci = code_info; *ci;) {
Damien George4747bec2014-07-31 16:12:01 +0000136 if ((ci[0] & 0x80) == 0) {
137 // 0b0LLBBBBB encoding
138 bc += ci[0] & 0x1f;
139 source_line += ci[0] >> 5;
140 ci += 1;
141 } else {
142 // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
143 bc += ci[0] & 0xf;
144 source_line += ((ci[0] << 4) & 0x700) | ci[1];
145 ci += 2;
146 }
Damien George73496fb2014-04-13 14:51:56 +0100147 printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
148 }
149 }
Damien George3417bc22014-05-10 10:36:38 +0100150 mp_bytecode_print2(ip, len - 0);
Paul Sokolovskyc5e32c62014-04-23 03:40:24 +0300151}
Damien George73496fb2014-04-13 14:51:56 +0100152
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200153const byte *mp_bytecode_print_str(const byte *ip) {
Damien George40f3c022014-07-03 13:25:24 +0100154 mp_uint_t unum;
Damien George50912e72015-01-20 11:55:10 +0000155 qstr qst;
Damienf03001f2013-11-17 13:19:33 +0000156
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200157 switch (*ip++) {
158 case MP_BC_LOAD_CONST_FALSE:
159 printf("LOAD_CONST_FALSE");
160 break;
Damienf03001f2013-11-17 13:19:33 +0000161
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200162 case MP_BC_LOAD_CONST_NONE:
163 printf("LOAD_CONST_NONE");
164 break;
Damienf03001f2013-11-17 13:19:33 +0000165
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200166 case MP_BC_LOAD_CONST_TRUE:
167 printf("LOAD_CONST_TRUE");
168 break;
Damien Georgee9906ac2014-01-04 18:44:46 +0000169
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200170 case MP_BC_LOAD_CONST_SMALL_INT: {
171 mp_int_t num = 0;
172 if ((ip[0] & 0x40) != 0) {
173 // Number is negative
174 num--;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200175 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200176 do {
177 num = (num << 7) | (*ip & 0x7f);
178 } while ((*ip++ & 0x80) != 0);
179 printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
180 break;
181 }
Damienf03001f2013-11-17 13:19:33 +0000182
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200183 case MP_BC_LOAD_CONST_STRING:
184 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000185 printf("LOAD_CONST_STRING '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200186 break;
Damienf03001f2013-11-17 13:19:33 +0000187
Damien Georged6ed6702015-01-13 23:08:47 +0000188 case MP_BC_LOAD_CONST_OBJ:
189 DECODE_PTR;
190 printf("LOAD_CONST_OBJ %p=", (void*)unum);
Damien George59fba2d2015-06-25 14:42:13 +0000191 mp_obj_print_helper(&mp_plat_print, (mp_obj_t)unum, PRINT_REPR);
Damien Georged6ed6702015-01-13 23:08:47 +0000192 break;
193
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200194 case MP_BC_LOAD_NULL:
195 printf("LOAD_NULL");
196 break;
Paul Sokolovsky00a9d132014-04-12 00:32:38 +0300197
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200198 case MP_BC_LOAD_FAST_N:
199 DECODE_UINT;
200 printf("LOAD_FAST_N " UINT_FMT, unum);
201 break;
Damienf03001f2013-11-17 13:19:33 +0000202
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200203 case MP_BC_LOAD_DEREF:
204 DECODE_UINT;
205 printf("LOAD_DEREF " UINT_FMT, unum);
206 break;
Damien George6baf76e2013-12-30 22:32:17 +0000207
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200208 case MP_BC_LOAD_NAME:
209 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000210 printf("LOAD_NAME %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000211 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
212 printf(" (cache=%u)", *ip++);
213 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200214 break;
Damienf03001f2013-11-17 13:19:33 +0000215
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200216 case MP_BC_LOAD_GLOBAL:
217 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000218 printf("LOAD_GLOBAL %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000219 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
220 printf(" (cache=%u)", *ip++);
221 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200222 break;
Damienf03001f2013-11-17 13:19:33 +0000223
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200224 case MP_BC_LOAD_ATTR:
225 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000226 printf("LOAD_ATTR %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000227 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
228 printf(" (cache=%u)", *ip++);
229 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200230 break;
Damienf03001f2013-11-17 13:19:33 +0000231
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200232 case MP_BC_LOAD_METHOD:
233 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000234 printf("LOAD_METHOD %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200235 break;
Damienf03001f2013-11-17 13:19:33 +0000236
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200237 case MP_BC_LOAD_BUILD_CLASS:
238 printf("LOAD_BUILD_CLASS");
239 break;
Damienf03001f2013-11-17 13:19:33 +0000240
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200241 case MP_BC_LOAD_SUBSCR:
242 printf("LOAD_SUBSCR");
243 break;
Damien George729f7b42014-04-17 22:10:53 +0100244
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200245 case MP_BC_STORE_FAST_N:
246 DECODE_UINT;
247 printf("STORE_FAST_N " UINT_FMT, unum);
248 break;
Damienf03001f2013-11-17 13:19:33 +0000249
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200250 case MP_BC_STORE_DEREF:
251 DECODE_UINT;
252 printf("STORE_DEREF " UINT_FMT, unum);
253 break;
Damien George6baf76e2013-12-30 22:32:17 +0000254
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200255 case MP_BC_STORE_NAME:
256 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000257 printf("STORE_NAME %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200258 break;
Damienf03001f2013-11-17 13:19:33 +0000259
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200260 case MP_BC_STORE_GLOBAL:
261 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000262 printf("STORE_GLOBAL %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200263 break;
Damienf03001f2013-11-17 13:19:33 +0000264
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200265 case MP_BC_STORE_ATTR:
266 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000267 printf("STORE_ATTR %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000268 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
269 printf(" (cache=%u)", *ip++);
270 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200271 break;
Damienf03001f2013-11-17 13:19:33 +0000272
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200273 case MP_BC_STORE_SUBSCR:
274 printf("STORE_SUBSCR");
275 break;
Damienf03001f2013-11-17 13:19:33 +0000276
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200277 case MP_BC_DELETE_FAST:
278 DECODE_UINT;
279 printf("DELETE_FAST " UINT_FMT, unum);
280 break;
Damien George2bf7c092014-04-09 15:26:46 +0100281
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200282 case MP_BC_DELETE_DEREF:
283 DECODE_UINT;
284 printf("DELETE_DEREF " UINT_FMT, unum);
285 break;
Damien George2bf7c092014-04-09 15:26:46 +0100286
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200287 case MP_BC_DELETE_NAME:
288 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000289 printf("DELETE_NAME %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200290 break;
Damien Georgeddaf6c12014-02-06 20:31:32 +0000291
Damien George8e9a7122015-03-20 17:12:09 +0000292 case MP_BC_DELETE_GLOBAL:
293 DECODE_QSTR;
294 printf("DELETE_GLOBAL %s", qstr_str(qst));
295 break;
296
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200297 case MP_BC_DUP_TOP:
298 printf("DUP_TOP");
299 break;
Damienf03001f2013-11-17 13:19:33 +0000300
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200301 case MP_BC_DUP_TOP_TWO:
302 printf("DUP_TOP_TWO");
303 break;
Damienf03001f2013-11-17 13:19:33 +0000304
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200305 case MP_BC_POP_TOP:
306 printf("POP_TOP");
307 break;
Damienf03001f2013-11-17 13:19:33 +0000308
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200309 case MP_BC_ROT_TWO:
310 printf("ROT_TWO");
311 break;
Damienf03001f2013-11-17 13:19:33 +0000312
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200313 case MP_BC_ROT_THREE:
314 printf("ROT_THREE");
315 break;
Damienf03001f2013-11-17 13:19:33 +0000316
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200317 case MP_BC_JUMP:
318 DECODE_SLABEL;
Paul Sokolovskydf103462014-12-28 21:34:45 +0200319 printf("JUMP " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200320 break;
Damienf03001f2013-11-17 13:19:33 +0000321
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200322 case MP_BC_POP_JUMP_IF_TRUE:
323 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200324 printf("POP_JUMP_IF_TRUE " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200325 break;
Damienf03001f2013-11-17 13:19:33 +0000326
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200327 case MP_BC_POP_JUMP_IF_FALSE:
328 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200329 printf("POP_JUMP_IF_FALSE " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200330 break;
Damienf03001f2013-11-17 13:19:33 +0000331
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200332 case MP_BC_JUMP_IF_TRUE_OR_POP:
333 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200334 printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200335 break;
Damienf03001f2013-11-17 13:19:33 +0000336
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200337 case MP_BC_JUMP_IF_FALSE_OR_POP:
338 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200339 printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200340 break;
Damienf03001f2013-11-17 13:19:33 +0000341
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200342 case MP_BC_SETUP_WITH:
343 DECODE_ULABEL; // loop-like labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200344 printf("SETUP_WITH " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200345 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200346
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200347 case MP_BC_WITH_CLEANUP:
348 printf("WITH_CLEANUP");
349 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200350
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200351 case MP_BC_UNWIND_JUMP:
352 DECODE_SLABEL;
Paul Sokolovskydf103462014-12-28 21:34:45 +0200353 printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - mp_showbc_code_start, *ip);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200354 ip += 1;
355 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200356
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200357 case MP_BC_SETUP_EXCEPT:
358 DECODE_ULABEL; // except labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200359 printf("SETUP_EXCEPT " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200360 break;
Damienf03001f2013-11-17 13:19:33 +0000361
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200362 case MP_BC_SETUP_FINALLY:
363 DECODE_ULABEL; // except labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200364 printf("SETUP_FINALLY " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200365 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200366
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200367 case MP_BC_END_FINALLY:
368 // if TOS is an exception, reraises the exception (3 values on TOS)
369 // if TOS is an integer, does something else
370 // if TOS is None, just pops it and continues
371 // else error
372 printf("END_FINALLY");
373 break;
Damienf03001f2013-11-17 13:19:33 +0000374
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200375 case MP_BC_GET_ITER:
376 printf("GET_ITER");
377 break;
Damienf03001f2013-11-17 13:19:33 +0000378
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200379 case MP_BC_FOR_ITER:
380 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200381 printf("FOR_ITER " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200382 break;
Damienf03001f2013-11-17 13:19:33 +0000383
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200384 case MP_BC_POP_BLOCK:
385 // pops block and restores the stack
386 printf("POP_BLOCK");
387 break;
Damienf03001f2013-11-17 13:19:33 +0000388
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200389 case MP_BC_POP_EXCEPT:
390 // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
391 printf("POP_EXCEPT");
392 break;
Damienf03001f2013-11-17 13:19:33 +0000393
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200394 case MP_BC_NOT:
395 printf("NOT");
396 break;
Damien George9aa2a522014-02-01 23:04:09 +0000397
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200398 case MP_BC_BUILD_TUPLE:
399 DECODE_UINT;
400 printf("BUILD_TUPLE " UINT_FMT, unum);
401 break;
Damienf03001f2013-11-17 13:19:33 +0000402
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200403 case MP_BC_BUILD_LIST:
404 DECODE_UINT;
405 printf("BUILD_LIST " UINT_FMT, unum);
406 break;
Damienf03001f2013-11-17 13:19:33 +0000407
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200408 case MP_BC_LIST_APPEND:
409 DECODE_UINT;
410 printf("LIST_APPEND " UINT_FMT, unum);
411 break;
Damienf03001f2013-11-17 13:19:33 +0000412
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200413 case MP_BC_BUILD_MAP:
414 DECODE_UINT;
415 printf("BUILD_MAP " UINT_FMT, unum);
416 break;
Damienf03001f2013-11-17 13:19:33 +0000417
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200418 case MP_BC_STORE_MAP:
419 printf("STORE_MAP");
420 break;
Damienf03001f2013-11-17 13:19:33 +0000421
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200422 case MP_BC_MAP_ADD:
423 DECODE_UINT;
424 printf("MAP_ADD " UINT_FMT, unum);
425 break;
Damienf03001f2013-11-17 13:19:33 +0000426
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200427 case MP_BC_BUILD_SET:
428 DECODE_UINT;
429 printf("BUILD_SET " UINT_FMT, unum);
430 break;
Damienf03001f2013-11-17 13:19:33 +0000431
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200432 case MP_BC_SET_ADD:
433 DECODE_UINT;
434 printf("SET_ADD " UINT_FMT, unum);
435 break;
Damienf03001f2013-11-17 13:19:33 +0000436
Damien Georgefb510b32014-06-01 13:32:54 +0100437#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200438 case MP_BC_BUILD_SLICE:
439 DECODE_UINT;
440 printf("BUILD_SLICE " UINT_FMT, unum);
441 break;
Damien George20006db2014-01-18 14:10:48 +0000442#endif
443
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200444 case MP_BC_UNPACK_SEQUENCE:
445 DECODE_UINT;
446 printf("UNPACK_SEQUENCE " UINT_FMT, unum);
447 break;
Damienff099f32013-11-26 15:14:50 +0000448
Damien Georgec8870b72015-06-18 15:12:17 +0000449 case MP_BC_UNPACK_EX:
450 DECODE_UINT;
451 printf("UNPACK_EX " UINT_FMT, unum);
452 break;
453
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200454 case MP_BC_MAKE_FUNCTION:
455 DECODE_PTR;
456 printf("MAKE_FUNCTION %p", (void*)unum);
457 break;
Damienf03001f2013-11-17 13:19:33 +0000458
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200459 case MP_BC_MAKE_FUNCTION_DEFARGS:
460 DECODE_PTR;
461 printf("MAKE_FUNCTION_DEFARGS %p", (void*)unum);
462 break;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200463
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200464 case MP_BC_MAKE_CLOSURE: {
465 DECODE_PTR;
466 mp_uint_t n_closed_over = *ip++;
467 printf("MAKE_CLOSURE %p " UINT_FMT, (void*)unum, n_closed_over);
468 break;
Damienf03001f2013-11-17 13:19:33 +0000469 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200470
471 case MP_BC_MAKE_CLOSURE_DEFARGS: {
472 DECODE_PTR;
473 mp_uint_t n_closed_over = *ip++;
474 printf("MAKE_CLOSURE_DEFARGS %p " UINT_FMT, (void*)unum, n_closed_over);
475 break;
476 }
477
478 case MP_BC_CALL_FUNCTION:
479 DECODE_UINT;
480 printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
481 break;
482
483 case MP_BC_CALL_FUNCTION_VAR_KW:
484 DECODE_UINT;
485 printf("CALL_FUNCTION_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
486 break;
487
488 case MP_BC_CALL_METHOD:
489 DECODE_UINT;
490 printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
491 break;
492
493 case MP_BC_CALL_METHOD_VAR_KW:
494 DECODE_UINT;
495 printf("CALL_METHOD_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
496 break;
497
498 case MP_BC_RETURN_VALUE:
499 printf("RETURN_VALUE");
500 break;
501
502 case MP_BC_RAISE_VARARGS:
503 unum = *ip++;
504 printf("RAISE_VARARGS " UINT_FMT, unum);
505 break;
506
507 case MP_BC_YIELD_VALUE:
508 printf("YIELD_VALUE");
509 break;
510
511 case MP_BC_YIELD_FROM:
512 printf("YIELD_FROM");
513 break;
514
515 case MP_BC_IMPORT_NAME:
516 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000517 printf("IMPORT_NAME '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200518 break;
519
520 case MP_BC_IMPORT_FROM:
521 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000522 printf("IMPORT_FROM '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200523 break;
524
525 case MP_BC_IMPORT_STAR:
526 printf("IMPORT_STAR");
527 break;
528
529 default:
530 if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) {
531 printf("LOAD_CONST_SMALL_INT " INT_FMT, (mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16);
532 } else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) {
533 printf("LOAD_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI);
534 } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) {
535 printf("STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI);
Damien Georgec8870b72015-06-18 15:12:17 +0000536 } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + 6) {
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200537 printf("UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI);
Damien Georgec8870b72015-06-18 15:12:17 +0000538 } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + 36) {
Paul Sokolovsky1ee17852014-12-28 21:41:58 +0200539 mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI;
540 printf("BINARY_OP " UINT_FMT " %s", op, qstr_str(mp_binary_op_method_name[op]));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200541 } else {
542 printf("code %p, byte code 0x%02x not implemented\n", ip, ip[-1]);
543 assert(0);
544 return ip;
545 }
546 break;
547 }
548
549 return ip;
550}
551
552void mp_bytecode_print2(const byte *ip, mp_uint_t len) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200553 mp_showbc_code_start = ip;
Damien George963a5a32015-01-16 17:47:07 +0000554 while (ip < len + mp_showbc_code_start) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200555 printf("%02u ", (uint)(ip - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200556 ip = mp_bytecode_print_str(ip);
Damienf03001f2013-11-17 13:19:33 +0000557 printf("\n");
558 }
559}
Damien Georged3ebe482014-01-07 15:20:33 +0000560
Damien Georgecbd2f742014-01-19 11:48:48 +0000561#endif // MICROPY_DEBUG_PRINTERS