blob: 684d9af0c0b0269c37cf71ec769e56d1f54b391a [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
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 Georgec8e9c0d2015-11-02 17:27:18 +000085 mp_showbc_const_table = const_table;
Damien George6baf76e2013-12-30 22:32:17 +000086
Damien George3a3db4d2015-10-22 23:45:37 +010087 // get bytecode parameters
Damien George9b7f5832015-03-18 17:47:47 +000088 mp_uint_t n_state = mp_decode_uint(&ip);
89 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
Damien George3a3db4d2015-10-22 23:45:37 +010090 /*mp_uint_t scope_flags =*/ ip++;
91 mp_uint_t n_pos_args = *ip++;
92 mp_uint_t n_kwonly_args = *ip++;
93 /*mp_uint_t n_def_pos_args =*/ ip++;
Damien George9b7f5832015-03-18 17:47:47 +000094
Damien George73496fb2014-04-13 14:51:56 +010095 const byte *code_info = ip;
Damien Georgeb534e1b2014-09-04 14:44:01 +010096 mp_uint_t code_info_size = mp_decode_uint(&code_info);
Damien George08335002014-01-18 23:24:36 +000097 ip += code_info_size;
98
Damien Georgec8e9c0d2015-11-02 17:27:18 +000099 #if MICROPY_PERSISTENT_CODE
100 qstr block_name = code_info[0] | (code_info[1] << 8);
101 qstr source_file = code_info[2] | (code_info[3] << 8);
Damien Georgefbddea92016-09-20 11:30:54 +1000102 code_info += 4;
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000103 #else
Damien Georgeb534e1b2014-09-04 14:44:01 +0100104 qstr block_name = mp_decode_uint(&code_info);
105 qstr source_file = mp_decode_uint(&code_info);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000106 #endif
Damien George3eaa0c32014-10-03 17:54:25 +0000107 printf("File %s, code block '%s' (descriptor: %p, bytecode @%p " UINT_FMT " bytes)\n",
Damien George9b7f5832015-03-18 17:47:47 +0000108 qstr_str(source_file), qstr_str(block_name), descr, mp_showbc_code_start, len);
Paul Sokolovsky8bf84042014-06-02 16:11:16 +0300109
Damien George564963a2014-10-24 14:42:50 +0000110 // raw bytecode dump
111 printf("Raw bytecode (code_info_size=" UINT_FMT ", bytecode_size=" UINT_FMT "):\n", code_info_size, len - code_info_size);
112 for (mp_uint_t i = 0; i < len; i++) {
113 if (i > 0 && i % 16 == 0) {
114 printf("\n");
115 }
Paul Sokolovskydf103462014-12-28 21:34:45 +0200116 printf(" %02x", mp_showbc_code_start[i]);
Damien George564963a2014-10-24 14:42:50 +0000117 }
118 printf("\n");
119
Damien George1084b0f2014-10-25 15:07:02 +0100120 // bytecode prelude: arg names (as qstr objects)
121 printf("arg names:");
Damien George3a3db4d2015-10-22 23:45:37 +0100122 for (mp_uint_t i = 0; i < n_pos_args + n_kwonly_args; i++) {
Damien George713ea182015-10-23 01:23:11 +0100123 printf(" %s", qstr_str(MP_OBJ_QSTR_VALUE(const_table[i])));
Damien George1084b0f2014-10-25 15:07:02 +0100124 }
125 printf("\n");
126
Damien George9b7f5832015-03-18 17:47:47 +0000127 printf("(N_STATE " UINT_FMT ")\n", n_state);
128 printf("(N_EXC_STACK " UINT_FMT ")\n", n_exc_stack);
129
130 // for printing line number info
131 const byte *bytecode_start = ip;
Damien George440f0412014-03-28 18:38:20 +0000132
133 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000134 {
Damien Georgec9aa1882015-04-07 00:08:17 +0100135 uint local_num;
136 while ((local_num = *ip++) != 255) {
Damien George6baf76e2013-12-30 22:32:17 +0000137 printf("(INIT_CELL %u)\n", local_num);
138 }
Paul Sokolovskydf103462014-12-28 21:34:45 +0200139 len -= ip - mp_showbc_code_start;
Damien George6baf76e2013-12-30 22:32:17 +0000140 }
141
Damien George73496fb2014-04-13 14:51:56 +0100142 // print out line number info
143 {
Damien George9b7f5832015-03-18 17:47:47 +0000144 mp_int_t bc = bytecode_start - ip;
Damien George40f3c022014-07-03 13:25:24 +0100145 mp_uint_t source_line = 1;
Damien George73496fb2014-04-13 14:51:56 +0100146 printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
Damien George564963a2014-10-24 14:42:50 +0000147 for (const byte* ci = code_info; *ci;) {
Damien George4747bec2014-07-31 16:12:01 +0000148 if ((ci[0] & 0x80) == 0) {
149 // 0b0LLBBBBB encoding
150 bc += ci[0] & 0x1f;
151 source_line += ci[0] >> 5;
152 ci += 1;
153 } else {
154 // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
155 bc += ci[0] & 0xf;
156 source_line += ((ci[0] << 4) & 0x700) | ci[1];
157 ci += 2;
158 }
Damien George73496fb2014-04-13 14:51:56 +0100159 printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
160 }
161 }
Damien George3417bc22014-05-10 10:36:38 +0100162 mp_bytecode_print2(ip, len - 0);
Paul Sokolovskyc5e32c62014-04-23 03:40:24 +0300163}
Damien George73496fb2014-04-13 14:51:56 +0100164
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200165const byte *mp_bytecode_print_str(const byte *ip) {
Damien George40f3c022014-07-03 13:25:24 +0100166 mp_uint_t unum;
Damien George50912e72015-01-20 11:55:10 +0000167 qstr qst;
Damienf03001f2013-11-17 13:19:33 +0000168
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200169 switch (*ip++) {
170 case MP_BC_LOAD_CONST_FALSE:
171 printf("LOAD_CONST_FALSE");
172 break;
Damienf03001f2013-11-17 13:19:33 +0000173
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200174 case MP_BC_LOAD_CONST_NONE:
175 printf("LOAD_CONST_NONE");
176 break;
Damienf03001f2013-11-17 13:19:33 +0000177
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200178 case MP_BC_LOAD_CONST_TRUE:
179 printf("LOAD_CONST_TRUE");
180 break;
Damien Georgee9906ac2014-01-04 18:44:46 +0000181
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200182 case MP_BC_LOAD_CONST_SMALL_INT: {
183 mp_int_t num = 0;
184 if ((ip[0] & 0x40) != 0) {
185 // Number is negative
186 num--;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200187 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200188 do {
189 num = (num << 7) | (*ip & 0x7f);
190 } while ((*ip++ & 0x80) != 0);
191 printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
192 break;
193 }
Damienf03001f2013-11-17 13:19:33 +0000194
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200195 case MP_BC_LOAD_CONST_STRING:
196 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000197 printf("LOAD_CONST_STRING '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200198 break;
Damienf03001f2013-11-17 13:19:33 +0000199
Damien Georged6ed6702015-01-13 23:08:47 +0000200 case MP_BC_LOAD_CONST_OBJ:
Damien George999cedb2015-11-27 17:01:44 +0000201 DECODE_OBJ;
202 printf("LOAD_CONST_OBJ %p=", MP_OBJ_TO_PTR(unum));
Damien George59fba2d2015-06-25 14:42:13 +0000203 mp_obj_print_helper(&mp_plat_print, (mp_obj_t)unum, PRINT_REPR);
Damien Georged6ed6702015-01-13 23:08:47 +0000204 break;
205
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200206 case MP_BC_LOAD_NULL:
207 printf("LOAD_NULL");
208 break;
Paul Sokolovsky00a9d132014-04-12 00:32:38 +0300209
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200210 case MP_BC_LOAD_FAST_N:
211 DECODE_UINT;
212 printf("LOAD_FAST_N " UINT_FMT, unum);
213 break;
Damienf03001f2013-11-17 13:19:33 +0000214
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200215 case MP_BC_LOAD_DEREF:
216 DECODE_UINT;
217 printf("LOAD_DEREF " UINT_FMT, unum);
218 break;
Damien George6baf76e2013-12-30 22:32:17 +0000219
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200220 case MP_BC_LOAD_NAME:
221 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000222 printf("LOAD_NAME %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000223 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
224 printf(" (cache=%u)", *ip++);
225 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200226 break;
Damienf03001f2013-11-17 13:19:33 +0000227
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200228 case MP_BC_LOAD_GLOBAL:
229 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000230 printf("LOAD_GLOBAL %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000231 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
232 printf(" (cache=%u)", *ip++);
233 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200234 break;
Damienf03001f2013-11-17 13:19:33 +0000235
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200236 case MP_BC_LOAD_ATTR:
237 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000238 printf("LOAD_ATTR %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000239 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
240 printf(" (cache=%u)", *ip++);
241 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200242 break;
Damienf03001f2013-11-17 13:19:33 +0000243
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200244 case MP_BC_LOAD_METHOD:
245 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000246 printf("LOAD_METHOD %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200247 break;
Damienf03001f2013-11-17 13:19:33 +0000248
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200249 case MP_BC_LOAD_BUILD_CLASS:
250 printf("LOAD_BUILD_CLASS");
251 break;
Damienf03001f2013-11-17 13:19:33 +0000252
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200253 case MP_BC_LOAD_SUBSCR:
254 printf("LOAD_SUBSCR");
255 break;
Damien George729f7b42014-04-17 22:10:53 +0100256
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200257 case MP_BC_STORE_FAST_N:
258 DECODE_UINT;
259 printf("STORE_FAST_N " UINT_FMT, unum);
260 break;
Damienf03001f2013-11-17 13:19:33 +0000261
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200262 case MP_BC_STORE_DEREF:
263 DECODE_UINT;
264 printf("STORE_DEREF " UINT_FMT, unum);
265 break;
Damien George6baf76e2013-12-30 22:32:17 +0000266
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200267 case MP_BC_STORE_NAME:
268 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000269 printf("STORE_NAME %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200270 break;
Damienf03001f2013-11-17 13:19:33 +0000271
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200272 case MP_BC_STORE_GLOBAL:
273 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000274 printf("STORE_GLOBAL %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200275 break;
Damienf03001f2013-11-17 13:19:33 +0000276
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200277 case MP_BC_STORE_ATTR:
278 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000279 printf("STORE_ATTR %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000280 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
281 printf(" (cache=%u)", *ip++);
282 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200283 break;
Damienf03001f2013-11-17 13:19:33 +0000284
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200285 case MP_BC_STORE_SUBSCR:
286 printf("STORE_SUBSCR");
287 break;
Damienf03001f2013-11-17 13:19:33 +0000288
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200289 case MP_BC_DELETE_FAST:
290 DECODE_UINT;
291 printf("DELETE_FAST " UINT_FMT, unum);
292 break;
Damien George2bf7c092014-04-09 15:26:46 +0100293
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200294 case MP_BC_DELETE_DEREF:
295 DECODE_UINT;
296 printf("DELETE_DEREF " UINT_FMT, unum);
297 break;
Damien George2bf7c092014-04-09 15:26:46 +0100298
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200299 case MP_BC_DELETE_NAME:
300 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000301 printf("DELETE_NAME %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200302 break;
Damien Georgeddaf6c12014-02-06 20:31:32 +0000303
Damien George8e9a7122015-03-20 17:12:09 +0000304 case MP_BC_DELETE_GLOBAL:
305 DECODE_QSTR;
306 printf("DELETE_GLOBAL %s", qstr_str(qst));
307 break;
308
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200309 case MP_BC_DUP_TOP:
310 printf("DUP_TOP");
311 break;
Damienf03001f2013-11-17 13:19:33 +0000312
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200313 case MP_BC_DUP_TOP_TWO:
314 printf("DUP_TOP_TWO");
315 break;
Damienf03001f2013-11-17 13:19:33 +0000316
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200317 case MP_BC_POP_TOP:
318 printf("POP_TOP");
319 break;
Damienf03001f2013-11-17 13:19:33 +0000320
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200321 case MP_BC_ROT_TWO:
322 printf("ROT_TWO");
323 break;
Damienf03001f2013-11-17 13:19:33 +0000324
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200325 case MP_BC_ROT_THREE:
326 printf("ROT_THREE");
327 break;
Damienf03001f2013-11-17 13:19:33 +0000328
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200329 case MP_BC_JUMP:
330 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000331 printf("JUMP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200332 break;
Damienf03001f2013-11-17 13:19:33 +0000333
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200334 case MP_BC_POP_JUMP_IF_TRUE:
335 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000336 printf("POP_JUMP_IF_TRUE " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200337 break;
Damienf03001f2013-11-17 13:19:33 +0000338
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200339 case MP_BC_POP_JUMP_IF_FALSE:
340 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000341 printf("POP_JUMP_IF_FALSE " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200342 break;
Damienf03001f2013-11-17 13:19:33 +0000343
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200344 case MP_BC_JUMP_IF_TRUE_OR_POP:
345 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000346 printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200347 break;
Damienf03001f2013-11-17 13:19:33 +0000348
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200349 case MP_BC_JUMP_IF_FALSE_OR_POP:
350 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000351 printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200352 break;
Damienf03001f2013-11-17 13:19:33 +0000353
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200354 case MP_BC_SETUP_WITH:
355 DECODE_ULABEL; // loop-like labels are always forward
Damien George999cedb2015-11-27 17:01:44 +0000356 printf("SETUP_WITH " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200357 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200358
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200359 case MP_BC_WITH_CLEANUP:
360 printf("WITH_CLEANUP");
361 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200362
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200363 case MP_BC_UNWIND_JUMP:
364 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000365 printf("UNWIND_JUMP " UINT_FMT " %d", (mp_uint_t)(ip + unum - mp_showbc_code_start), *ip);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200366 ip += 1;
367 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200368
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200369 case MP_BC_SETUP_EXCEPT:
370 DECODE_ULABEL; // except labels are always forward
Damien George999cedb2015-11-27 17:01:44 +0000371 printf("SETUP_EXCEPT " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200372 break;
Damienf03001f2013-11-17 13:19:33 +0000373
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200374 case MP_BC_SETUP_FINALLY:
375 DECODE_ULABEL; // except labels are always forward
Damien George999cedb2015-11-27 17:01:44 +0000376 printf("SETUP_FINALLY " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200377 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200378
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200379 case MP_BC_END_FINALLY:
380 // if TOS is an exception, reraises the exception (3 values on TOS)
381 // if TOS is an integer, does something else
382 // if TOS is None, just pops it and continues
383 // else error
384 printf("END_FINALLY");
385 break;
Damienf03001f2013-11-17 13:19:33 +0000386
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200387 case MP_BC_GET_ITER:
388 printf("GET_ITER");
389 break;
Damienf03001f2013-11-17 13:19:33 +0000390
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200391 case MP_BC_FOR_ITER:
392 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damien George999cedb2015-11-27 17:01:44 +0000393 printf("FOR_ITER " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200394 break;
Damienf03001f2013-11-17 13:19:33 +0000395
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200396 case MP_BC_POP_BLOCK:
397 // pops block and restores the stack
398 printf("POP_BLOCK");
399 break;
Damienf03001f2013-11-17 13:19:33 +0000400
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200401 case MP_BC_POP_EXCEPT:
402 // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
403 printf("POP_EXCEPT");
404 break;
Damienf03001f2013-11-17 13:19:33 +0000405
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200406 case MP_BC_BUILD_TUPLE:
407 DECODE_UINT;
408 printf("BUILD_TUPLE " UINT_FMT, unum);
409 break;
Damienf03001f2013-11-17 13:19:33 +0000410
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200411 case MP_BC_BUILD_LIST:
412 DECODE_UINT;
413 printf("BUILD_LIST " UINT_FMT, unum);
414 break;
Damienf03001f2013-11-17 13:19:33 +0000415
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200416 case MP_BC_BUILD_MAP:
417 DECODE_UINT;
418 printf("BUILD_MAP " UINT_FMT, unum);
419 break;
Damienf03001f2013-11-17 13:19:33 +0000420
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200421 case MP_BC_STORE_MAP:
422 printf("STORE_MAP");
423 break;
Damienf03001f2013-11-17 13:19:33 +0000424
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200425 case MP_BC_BUILD_SET:
426 DECODE_UINT;
427 printf("BUILD_SET " UINT_FMT, unum);
428 break;
Damienf03001f2013-11-17 13:19:33 +0000429
Damien Georgefb510b32014-06-01 13:32:54 +0100430#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200431 case MP_BC_BUILD_SLICE:
432 DECODE_UINT;
433 printf("BUILD_SLICE " UINT_FMT, unum);
434 break;
Damien George20006db2014-01-18 14:10:48 +0000435#endif
436
Damien Georgeadaf0d82016-09-19 08:46:01 +1000437 case MP_BC_STORE_COMP:
438 DECODE_UINT;
439 printf("STORE_COMP " UINT_FMT, unum);
440 break;
441
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200442 case MP_BC_UNPACK_SEQUENCE:
443 DECODE_UINT;
444 printf("UNPACK_SEQUENCE " UINT_FMT, unum);
445 break;
Damienff099f32013-11-26 15:14:50 +0000446
Damien Georgec8870b72015-06-18 15:12:17 +0000447 case MP_BC_UNPACK_EX:
448 DECODE_UINT;
449 printf("UNPACK_EX " UINT_FMT, unum);
450 break;
451
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200452 case MP_BC_MAKE_FUNCTION:
453 DECODE_PTR;
Damien George999cedb2015-11-27 17:01:44 +0000454 printf("MAKE_FUNCTION %p", (void*)(uintptr_t)unum);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200455 break;
Damienf03001f2013-11-17 13:19:33 +0000456
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200457 case MP_BC_MAKE_FUNCTION_DEFARGS:
458 DECODE_PTR;
Damien George999cedb2015-11-27 17:01:44 +0000459 printf("MAKE_FUNCTION_DEFARGS %p", (void*)(uintptr_t)unum);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200460 break;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200461
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200462 case MP_BC_MAKE_CLOSURE: {
463 DECODE_PTR;
464 mp_uint_t n_closed_over = *ip++;
Damien George999cedb2015-11-27 17:01:44 +0000465 printf("MAKE_CLOSURE %p " UINT_FMT, (void*)(uintptr_t)unum, n_closed_over);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200466 break;
Damienf03001f2013-11-17 13:19:33 +0000467 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200468
469 case MP_BC_MAKE_CLOSURE_DEFARGS: {
470 DECODE_PTR;
471 mp_uint_t n_closed_over = *ip++;
Damien George999cedb2015-11-27 17:01:44 +0000472 printf("MAKE_CLOSURE_DEFARGS %p " UINT_FMT, (void*)(uintptr_t)unum, n_closed_over);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200473 break;
474 }
475
476 case MP_BC_CALL_FUNCTION:
477 DECODE_UINT;
478 printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
479 break;
480
481 case MP_BC_CALL_FUNCTION_VAR_KW:
482 DECODE_UINT;
483 printf("CALL_FUNCTION_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
484 break;
485
486 case MP_BC_CALL_METHOD:
487 DECODE_UINT;
488 printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
489 break;
490
491 case MP_BC_CALL_METHOD_VAR_KW:
492 DECODE_UINT;
493 printf("CALL_METHOD_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
494 break;
495
496 case MP_BC_RETURN_VALUE:
497 printf("RETURN_VALUE");
498 break;
499
500 case MP_BC_RAISE_VARARGS:
501 unum = *ip++;
502 printf("RAISE_VARARGS " UINT_FMT, unum);
503 break;
504
505 case MP_BC_YIELD_VALUE:
506 printf("YIELD_VALUE");
507 break;
508
509 case MP_BC_YIELD_FROM:
510 printf("YIELD_FROM");
511 break;
512
513 case MP_BC_IMPORT_NAME:
514 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000515 printf("IMPORT_NAME '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200516 break;
517
518 case MP_BC_IMPORT_FROM:
519 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000520 printf("IMPORT_FROM '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200521 break;
522
523 case MP_BC_IMPORT_STAR:
524 printf("IMPORT_STAR");
525 break;
526
527 default:
528 if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) {
529 printf("LOAD_CONST_SMALL_INT " INT_FMT, (mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16);
530 } else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) {
531 printf("LOAD_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI);
532 } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) {
533 printf("STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI);
Damien Georgebdbe8c92015-12-08 12:28:11 +0000534 } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + 7) {
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200535 printf("UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI);
Damien Georgec8870b72015-06-18 15:12:17 +0000536 } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + 36) {
Paul Sokolovsky1ee17852014-12-28 21:41:58 +0200537 mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI;
538 printf("BINARY_OP " UINT_FMT " %s", op, qstr_str(mp_binary_op_method_name[op]));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200539 } else {
540 printf("code %p, byte code 0x%02x not implemented\n", ip, ip[-1]);
541 assert(0);
542 return ip;
543 }
544 break;
545 }
546
547 return ip;
548}
549
550void mp_bytecode_print2(const byte *ip, mp_uint_t len) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200551 mp_showbc_code_start = ip;
Damien George963a5a32015-01-16 17:47:07 +0000552 while (ip < len + mp_showbc_code_start) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200553 printf("%02u ", (uint)(ip - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200554 ip = mp_bytecode_print_str(ip);
Damienf03001f2013-11-17 13:19:33 +0000555 printf("\n");
556 }
557}
Damien Georged3ebe482014-01-07 15:20:33 +0000558
Damien Georgecbd2f742014-01-19 11:48:48 +0000559#endif // MICROPY_DEBUG_PRINTERS