blob: 9b08fa6d6387952653ea0996570035ae77b5126f [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]
Damien George999cedb2015-11-27 17:01:44 +000052#define DECODE_OBJ \
53 DECODE_UINT; \
54 unum = mp_showbc_const_table[unum]
Damien Georgec8e9c0d2015-11-02 17:27:18 +000055
56#else
57
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020058#define DECODE_QSTR { \
Damien George50912e72015-01-20 11:55:10 +000059 qst = 0; \
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020060 do { \
Damien George50912e72015-01-20 11:55:10 +000061 qst = (qst << 7) + (*ip & 0x7f); \
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020062 } while ((*ip++ & 0x80) != 0); \
63}
Damien George3d484d92014-04-13 11:22:44 +010064#define DECODE_PTR do { \
Damien George999cedb2015-11-27 17:01:44 +000065 ip = (byte*)MP_ALIGN(ip, sizeof(void*)); \
66 unum = (uintptr_t)*(void**)ip; \
67 ip += sizeof(void*); \
68} while (0)
69#define DECODE_OBJ do { \
70 ip = (byte*)MP_ALIGN(ip, sizeof(mp_obj_t)); \
71 unum = (mp_uint_t)*(mp_obj_t*)ip; \
72 ip += sizeof(mp_obj_t); \
Damien George3d484d92014-04-13 11:22:44 +010073} while (0)
Damienf03001f2013-11-17 13:19:33 +000074
Damien Georgec8e9c0d2015-11-02 17:27:18 +000075#endif
76
Paul Sokolovskydf103462014-12-28 21:34:45 +020077const byte *mp_showbc_code_start;
Damien Georgec8e9c0d2015-11-02 17:27:18 +000078const mp_uint_t *mp_showbc_const_table;
Paul Sokolovsky343266e2014-12-27 05:00:08 +020079
Damien George713ea182015-10-23 01:23:11 +010080void 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 +020081 mp_showbc_code_start = ip;
Damien Georgec8e9c0d2015-11-02 17:27:18 +000082 mp_showbc_const_table = const_table;
Damien George6baf76e2013-12-30 22:32:17 +000083
Damien George3a3db4d2015-10-22 23:45:37 +010084 // get bytecode parameters
Damien George9b7f5832015-03-18 17:47:47 +000085 mp_uint_t n_state = mp_decode_uint(&ip);
86 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
Damien George3a3db4d2015-10-22 23:45:37 +010087 /*mp_uint_t scope_flags =*/ ip++;
88 mp_uint_t n_pos_args = *ip++;
89 mp_uint_t n_kwonly_args = *ip++;
90 /*mp_uint_t n_def_pos_args =*/ ip++;
Damien George9b7f5832015-03-18 17:47:47 +000091
Damien George73496fb2014-04-13 14:51:56 +010092 const byte *code_info = ip;
Damien Georgeb534e1b2014-09-04 14:44:01 +010093 mp_uint_t code_info_size = mp_decode_uint(&code_info);
Damien George08335002014-01-18 23:24:36 +000094 ip += code_info_size;
95
Damien Georgec8e9c0d2015-11-02 17:27:18 +000096 #if MICROPY_PERSISTENT_CODE
97 qstr block_name = code_info[0] | (code_info[1] << 8);
98 qstr source_file = code_info[2] | (code_info[3] << 8);
99 #else
Damien Georgeb534e1b2014-09-04 14:44:01 +0100100 qstr block_name = mp_decode_uint(&code_info);
101 qstr source_file = mp_decode_uint(&code_info);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000102 #endif
Damien George3eaa0c32014-10-03 17:54:25 +0000103 printf("File %s, code block '%s' (descriptor: %p, bytecode @%p " UINT_FMT " bytes)\n",
Damien George9b7f5832015-03-18 17:47:47 +0000104 qstr_str(source_file), qstr_str(block_name), descr, mp_showbc_code_start, len);
Paul Sokolovsky8bf84042014-06-02 16:11:16 +0300105
Damien George564963a2014-10-24 14:42:50 +0000106 // raw bytecode dump
107 printf("Raw bytecode (code_info_size=" UINT_FMT ", bytecode_size=" UINT_FMT "):\n", code_info_size, len - code_info_size);
108 for (mp_uint_t i = 0; i < len; i++) {
109 if (i > 0 && i % 16 == 0) {
110 printf("\n");
111 }
Paul Sokolovskydf103462014-12-28 21:34:45 +0200112 printf(" %02x", mp_showbc_code_start[i]);
Damien George564963a2014-10-24 14:42:50 +0000113 }
114 printf("\n");
115
Damien George1084b0f2014-10-25 15:07:02 +0100116 // bytecode prelude: arg names (as qstr objects)
117 printf("arg names:");
Damien George3a3db4d2015-10-22 23:45:37 +0100118 for (mp_uint_t i = 0; i < n_pos_args + n_kwonly_args; i++) {
Damien George713ea182015-10-23 01:23:11 +0100119 printf(" %s", qstr_str(MP_OBJ_QSTR_VALUE(const_table[i])));
Damien George1084b0f2014-10-25 15:07:02 +0100120 }
121 printf("\n");
122
Damien George9b7f5832015-03-18 17:47:47 +0000123 printf("(N_STATE " UINT_FMT ")\n", n_state);
124 printf("(N_EXC_STACK " UINT_FMT ")\n", n_exc_stack);
125
126 // for printing line number info
127 const byte *bytecode_start = ip;
Damien George440f0412014-03-28 18:38:20 +0000128
129 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000130 {
Damien Georgec9aa1882015-04-07 00:08:17 +0100131 uint local_num;
132 while ((local_num = *ip++) != 255) {
Damien George6baf76e2013-12-30 22:32:17 +0000133 printf("(INIT_CELL %u)\n", local_num);
134 }
Paul Sokolovskydf103462014-12-28 21:34:45 +0200135 len -= ip - mp_showbc_code_start;
Damien George6baf76e2013-12-30 22:32:17 +0000136 }
137
Damien George73496fb2014-04-13 14:51:56 +0100138 // print out line number info
139 {
Damien George9b7f5832015-03-18 17:47:47 +0000140 mp_int_t bc = bytecode_start - ip;
Damien George40f3c022014-07-03 13:25:24 +0100141 mp_uint_t source_line = 1;
Damien George73496fb2014-04-13 14:51:56 +0100142 printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
Damien George564963a2014-10-24 14:42:50 +0000143 for (const byte* ci = code_info; *ci;) {
Damien George4747bec2014-07-31 16:12:01 +0000144 if ((ci[0] & 0x80) == 0) {
145 // 0b0LLBBBBB encoding
146 bc += ci[0] & 0x1f;
147 source_line += ci[0] >> 5;
148 ci += 1;
149 } else {
150 // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
151 bc += ci[0] & 0xf;
152 source_line += ((ci[0] << 4) & 0x700) | ci[1];
153 ci += 2;
154 }
Damien George73496fb2014-04-13 14:51:56 +0100155 printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
156 }
157 }
Damien George3417bc22014-05-10 10:36:38 +0100158 mp_bytecode_print2(ip, len - 0);
Paul Sokolovskyc5e32c62014-04-23 03:40:24 +0300159}
Damien George73496fb2014-04-13 14:51:56 +0100160
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200161const byte *mp_bytecode_print_str(const byte *ip) {
Damien George40f3c022014-07-03 13:25:24 +0100162 mp_uint_t unum;
Damien George50912e72015-01-20 11:55:10 +0000163 qstr qst;
Damienf03001f2013-11-17 13:19:33 +0000164
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200165 switch (*ip++) {
166 case MP_BC_LOAD_CONST_FALSE:
167 printf("LOAD_CONST_FALSE");
168 break;
Damienf03001f2013-11-17 13:19:33 +0000169
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200170 case MP_BC_LOAD_CONST_NONE:
171 printf("LOAD_CONST_NONE");
172 break;
Damienf03001f2013-11-17 13:19:33 +0000173
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200174 case MP_BC_LOAD_CONST_TRUE:
175 printf("LOAD_CONST_TRUE");
176 break;
Damien Georgee9906ac2014-01-04 18:44:46 +0000177
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200178 case MP_BC_LOAD_CONST_SMALL_INT: {
179 mp_int_t num = 0;
180 if ((ip[0] & 0x40) != 0) {
181 // Number is negative
182 num--;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200183 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200184 do {
185 num = (num << 7) | (*ip & 0x7f);
186 } while ((*ip++ & 0x80) != 0);
187 printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
188 break;
189 }
Damienf03001f2013-11-17 13:19:33 +0000190
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200191 case MP_BC_LOAD_CONST_STRING:
192 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000193 printf("LOAD_CONST_STRING '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200194 break;
Damienf03001f2013-11-17 13:19:33 +0000195
Damien Georged6ed6702015-01-13 23:08:47 +0000196 case MP_BC_LOAD_CONST_OBJ:
Damien George999cedb2015-11-27 17:01:44 +0000197 DECODE_OBJ;
198 printf("LOAD_CONST_OBJ %p=", MP_OBJ_TO_PTR(unum));
Damien George59fba2d2015-06-25 14:42:13 +0000199 mp_obj_print_helper(&mp_plat_print, (mp_obj_t)unum, PRINT_REPR);
Damien Georged6ed6702015-01-13 23:08:47 +0000200 break;
201
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200202 case MP_BC_LOAD_NULL:
203 printf("LOAD_NULL");
204 break;
Paul Sokolovsky00a9d132014-04-12 00:32:38 +0300205
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200206 case MP_BC_LOAD_FAST_N:
207 DECODE_UINT;
208 printf("LOAD_FAST_N " UINT_FMT, unum);
209 break;
Damienf03001f2013-11-17 13:19:33 +0000210
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200211 case MP_BC_LOAD_DEREF:
212 DECODE_UINT;
213 printf("LOAD_DEREF " UINT_FMT, unum);
214 break;
Damien George6baf76e2013-12-30 22:32:17 +0000215
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200216 case MP_BC_LOAD_NAME:
217 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000218 printf("LOAD_NAME %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_GLOBAL:
225 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000226 printf("LOAD_GLOBAL %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_ATTR:
233 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000234 printf("LOAD_ATTR %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000235 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
236 printf(" (cache=%u)", *ip++);
237 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200238 break;
Damienf03001f2013-11-17 13:19:33 +0000239
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200240 case MP_BC_LOAD_METHOD:
241 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000242 printf("LOAD_METHOD %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200243 break;
Damienf03001f2013-11-17 13:19:33 +0000244
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200245 case MP_BC_LOAD_BUILD_CLASS:
246 printf("LOAD_BUILD_CLASS");
247 break;
Damienf03001f2013-11-17 13:19:33 +0000248
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200249 case MP_BC_LOAD_SUBSCR:
250 printf("LOAD_SUBSCR");
251 break;
Damien George729f7b42014-04-17 22:10:53 +0100252
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200253 case MP_BC_STORE_FAST_N:
254 DECODE_UINT;
255 printf("STORE_FAST_N " UINT_FMT, unum);
256 break;
Damienf03001f2013-11-17 13:19:33 +0000257
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200258 case MP_BC_STORE_DEREF:
259 DECODE_UINT;
260 printf("STORE_DEREF " UINT_FMT, unum);
261 break;
Damien George6baf76e2013-12-30 22:32:17 +0000262
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200263 case MP_BC_STORE_NAME:
264 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000265 printf("STORE_NAME %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200266 break;
Damienf03001f2013-11-17 13:19:33 +0000267
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200268 case MP_BC_STORE_GLOBAL:
269 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000270 printf("STORE_GLOBAL %s", qstr_str(qst));
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_ATTR:
274 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000275 printf("STORE_ATTR %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000276 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
277 printf(" (cache=%u)", *ip++);
278 }
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_SUBSCR:
282 printf("STORE_SUBSCR");
283 break;
Damienf03001f2013-11-17 13:19:33 +0000284
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200285 case MP_BC_DELETE_FAST:
286 DECODE_UINT;
287 printf("DELETE_FAST " UINT_FMT, unum);
288 break;
Damien George2bf7c092014-04-09 15:26:46 +0100289
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200290 case MP_BC_DELETE_DEREF:
291 DECODE_UINT;
292 printf("DELETE_DEREF " UINT_FMT, unum);
293 break;
Damien George2bf7c092014-04-09 15:26:46 +0100294
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200295 case MP_BC_DELETE_NAME:
296 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000297 printf("DELETE_NAME %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200298 break;
Damien Georgeddaf6c12014-02-06 20:31:32 +0000299
Damien George8e9a7122015-03-20 17:12:09 +0000300 case MP_BC_DELETE_GLOBAL:
301 DECODE_QSTR;
302 printf("DELETE_GLOBAL %s", qstr_str(qst));
303 break;
304
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200305 case MP_BC_DUP_TOP:
306 printf("DUP_TOP");
307 break;
Damienf03001f2013-11-17 13:19:33 +0000308
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200309 case MP_BC_DUP_TOP_TWO:
310 printf("DUP_TOP_TWO");
311 break;
Damienf03001f2013-11-17 13:19:33 +0000312
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200313 case MP_BC_POP_TOP:
314 printf("POP_TOP");
315 break;
Damienf03001f2013-11-17 13:19:33 +0000316
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200317 case MP_BC_ROT_TWO:
318 printf("ROT_TWO");
319 break;
Damienf03001f2013-11-17 13:19:33 +0000320
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200321 case MP_BC_ROT_THREE:
322 printf("ROT_THREE");
323 break;
Damienf03001f2013-11-17 13:19:33 +0000324
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200325 case MP_BC_JUMP:
326 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000327 printf("JUMP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200328 break;
Damienf03001f2013-11-17 13:19:33 +0000329
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200330 case MP_BC_POP_JUMP_IF_TRUE:
331 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000332 printf("POP_JUMP_IF_TRUE " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200333 break;
Damienf03001f2013-11-17 13:19:33 +0000334
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200335 case MP_BC_POP_JUMP_IF_FALSE:
336 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000337 printf("POP_JUMP_IF_FALSE " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200338 break;
Damienf03001f2013-11-17 13:19:33 +0000339
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200340 case MP_BC_JUMP_IF_TRUE_OR_POP:
341 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000342 printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200343 break;
Damienf03001f2013-11-17 13:19:33 +0000344
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200345 case MP_BC_JUMP_IF_FALSE_OR_POP:
346 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000347 printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200348 break;
Damienf03001f2013-11-17 13:19:33 +0000349
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200350 case MP_BC_SETUP_WITH:
351 DECODE_ULABEL; // loop-like labels are always forward
Damien George999cedb2015-11-27 17:01:44 +0000352 printf("SETUP_WITH " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200353 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200354
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200355 case MP_BC_WITH_CLEANUP:
356 printf("WITH_CLEANUP");
357 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200358
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200359 case MP_BC_UNWIND_JUMP:
360 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000361 printf("UNWIND_JUMP " UINT_FMT " %d", (mp_uint_t)(ip + unum - mp_showbc_code_start), *ip);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200362 ip += 1;
363 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200364
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200365 case MP_BC_SETUP_EXCEPT:
366 DECODE_ULABEL; // except labels are always forward
Damien George999cedb2015-11-27 17:01:44 +0000367 printf("SETUP_EXCEPT " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200368 break;
Damienf03001f2013-11-17 13:19:33 +0000369
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200370 case MP_BC_SETUP_FINALLY:
371 DECODE_ULABEL; // except labels are always forward
Damien George999cedb2015-11-27 17:01:44 +0000372 printf("SETUP_FINALLY " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200373 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200374
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200375 case MP_BC_END_FINALLY:
376 // if TOS is an exception, reraises the exception (3 values on TOS)
377 // if TOS is an integer, does something else
378 // if TOS is None, just pops it and continues
379 // else error
380 printf("END_FINALLY");
381 break;
Damienf03001f2013-11-17 13:19:33 +0000382
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200383 case MP_BC_GET_ITER:
384 printf("GET_ITER");
385 break;
Damienf03001f2013-11-17 13:19:33 +0000386
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200387 case MP_BC_FOR_ITER:
388 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damien George999cedb2015-11-27 17:01:44 +0000389 printf("FOR_ITER " UINT_FMT, (mp_uint_t)(ip + unum - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200390 break;
Damienf03001f2013-11-17 13:19:33 +0000391
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200392 case MP_BC_POP_BLOCK:
393 // pops block and restores the stack
394 printf("POP_BLOCK");
395 break;
Damienf03001f2013-11-17 13:19:33 +0000396
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200397 case MP_BC_POP_EXCEPT:
398 // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
399 printf("POP_EXCEPT");
400 break;
Damienf03001f2013-11-17 13:19:33 +0000401
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200402 case MP_BC_NOT:
403 printf("NOT");
404 break;
Damien George9aa2a522014-02-01 23:04:09 +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_LIST_APPEND:
417 DECODE_UINT;
418 printf("LIST_APPEND " UINT_FMT, unum);
419 break;
Damienf03001f2013-11-17 13:19:33 +0000420
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200421 case MP_BC_BUILD_MAP:
422 DECODE_UINT;
423 printf("BUILD_MAP " UINT_FMT, unum);
424 break;
Damienf03001f2013-11-17 13:19:33 +0000425
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200426 case MP_BC_STORE_MAP:
427 printf("STORE_MAP");
428 break;
Damienf03001f2013-11-17 13:19:33 +0000429
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200430 case MP_BC_MAP_ADD:
431 DECODE_UINT;
432 printf("MAP_ADD " UINT_FMT, unum);
433 break;
Damienf03001f2013-11-17 13:19:33 +0000434
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200435 case MP_BC_BUILD_SET:
436 DECODE_UINT;
437 printf("BUILD_SET " UINT_FMT, unum);
438 break;
Damienf03001f2013-11-17 13:19:33 +0000439
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200440 case MP_BC_SET_ADD:
441 DECODE_UINT;
442 printf("SET_ADD " UINT_FMT, unum);
443 break;
Damienf03001f2013-11-17 13:19:33 +0000444
Damien Georgefb510b32014-06-01 13:32:54 +0100445#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200446 case MP_BC_BUILD_SLICE:
447 DECODE_UINT;
448 printf("BUILD_SLICE " UINT_FMT, unum);
449 break;
Damien George20006db2014-01-18 14:10:48 +0000450#endif
451
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200452 case MP_BC_UNPACK_SEQUENCE:
453 DECODE_UINT;
454 printf("UNPACK_SEQUENCE " UINT_FMT, unum);
455 break;
Damienff099f32013-11-26 15:14:50 +0000456
Damien Georgec8870b72015-06-18 15:12:17 +0000457 case MP_BC_UNPACK_EX:
458 DECODE_UINT;
459 printf("UNPACK_EX " UINT_FMT, unum);
460 break;
461
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200462 case MP_BC_MAKE_FUNCTION:
463 DECODE_PTR;
Damien George999cedb2015-11-27 17:01:44 +0000464 printf("MAKE_FUNCTION %p", (void*)(uintptr_t)unum);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200465 break;
Damienf03001f2013-11-17 13:19:33 +0000466
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200467 case MP_BC_MAKE_FUNCTION_DEFARGS:
468 DECODE_PTR;
Damien George999cedb2015-11-27 17:01:44 +0000469 printf("MAKE_FUNCTION_DEFARGS %p", (void*)(uintptr_t)unum);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200470 break;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200471
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200472 case MP_BC_MAKE_CLOSURE: {
473 DECODE_PTR;
474 mp_uint_t n_closed_over = *ip++;
Damien George999cedb2015-11-27 17:01:44 +0000475 printf("MAKE_CLOSURE %p " UINT_FMT, (void*)(uintptr_t)unum, n_closed_over);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200476 break;
Damienf03001f2013-11-17 13:19:33 +0000477 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200478
479 case MP_BC_MAKE_CLOSURE_DEFARGS: {
480 DECODE_PTR;
481 mp_uint_t n_closed_over = *ip++;
Damien George999cedb2015-11-27 17:01:44 +0000482 printf("MAKE_CLOSURE_DEFARGS %p " UINT_FMT, (void*)(uintptr_t)unum, n_closed_over);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200483 break;
484 }
485
486 case MP_BC_CALL_FUNCTION:
487 DECODE_UINT;
488 printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
489 break;
490
491 case MP_BC_CALL_FUNCTION_VAR_KW:
492 DECODE_UINT;
493 printf("CALL_FUNCTION_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
494 break;
495
496 case MP_BC_CALL_METHOD:
497 DECODE_UINT;
498 printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
499 break;
500
501 case MP_BC_CALL_METHOD_VAR_KW:
502 DECODE_UINT;
503 printf("CALL_METHOD_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
504 break;
505
506 case MP_BC_RETURN_VALUE:
507 printf("RETURN_VALUE");
508 break;
509
510 case MP_BC_RAISE_VARARGS:
511 unum = *ip++;
512 printf("RAISE_VARARGS " UINT_FMT, unum);
513 break;
514
515 case MP_BC_YIELD_VALUE:
516 printf("YIELD_VALUE");
517 break;
518
519 case MP_BC_YIELD_FROM:
520 printf("YIELD_FROM");
521 break;
522
523 case MP_BC_IMPORT_NAME:
524 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000525 printf("IMPORT_NAME '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200526 break;
527
528 case MP_BC_IMPORT_FROM:
529 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000530 printf("IMPORT_FROM '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200531 break;
532
533 case MP_BC_IMPORT_STAR:
534 printf("IMPORT_STAR");
535 break;
536
537 default:
538 if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) {
539 printf("LOAD_CONST_SMALL_INT " INT_FMT, (mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16);
540 } else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) {
541 printf("LOAD_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI);
542 } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) {
543 printf("STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI);
Damien Georgec8870b72015-06-18 15:12:17 +0000544 } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + 6) {
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200545 printf("UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI);
Damien Georgec8870b72015-06-18 15:12:17 +0000546 } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + 36) {
Paul Sokolovsky1ee17852014-12-28 21:41:58 +0200547 mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI;
548 printf("BINARY_OP " UINT_FMT " %s", op, qstr_str(mp_binary_op_method_name[op]));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200549 } else {
550 printf("code %p, byte code 0x%02x not implemented\n", ip, ip[-1]);
551 assert(0);
552 return ip;
553 }
554 break;
555 }
556
557 return ip;
558}
559
560void mp_bytecode_print2(const byte *ip, mp_uint_t len) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200561 mp_showbc_code_start = ip;
Damien George963a5a32015-01-16 17:47:07 +0000562 while (ip < len + mp_showbc_code_start) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200563 printf("%02u ", (uint)(ip - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200564 ip = mp_bytecode_print_str(ip);
Damienf03001f2013-11-17 13:19:33 +0000565 printf("\n");
566 }
567}
Damien Georged3ebe482014-01-07 15:20:33 +0000568
Damien Georgecbd2f742014-01-19 11:48:48 +0000569#endif // MICROPY_DEBUG_PRINTERS