blob: 538eddc40fcfa2f19a6b401fbef53cfe168640ac [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)
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020043#define DECODE_QSTR { \
Damien George50912e72015-01-20 11:55:10 +000044 qst = 0; \
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020045 do { \
Damien George50912e72015-01-20 11:55:10 +000046 qst = (qst << 7) + (*ip & 0x7f); \
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020047 } while ((*ip++ & 0x80) != 0); \
48}
Damien George3d484d92014-04-13 11:22:44 +010049#define DECODE_PTR do { \
Damien George40f3c022014-07-03 13:25:24 +010050 ip = (byte*)(((mp_uint_t)ip + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1))); /* align ip */ \
51 unum = *(mp_uint_t*)ip; \
52 ip += sizeof(mp_uint_t); \
Damien George3d484d92014-04-13 11:22:44 +010053} while (0)
Damienf03001f2013-11-17 13:19:33 +000054
Paul Sokolovskydf103462014-12-28 21:34:45 +020055const byte *mp_showbc_code_start;
Paul Sokolovsky343266e2014-12-27 05:00:08 +020056
Damien George3a3db4d2015-10-22 23:45:37 +010057void mp_bytecode_print(const void *descr, const byte *ip, mp_uint_t len) {
Paul Sokolovskydf103462014-12-28 21:34:45 +020058 mp_showbc_code_start = ip;
Damien George6baf76e2013-12-30 22:32:17 +000059
Damien George3a3db4d2015-10-22 23:45:37 +010060 // get bytecode parameters
Damien George9b7f5832015-03-18 17:47:47 +000061 mp_uint_t n_state = mp_decode_uint(&ip);
62 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
Damien George3a3db4d2015-10-22 23:45:37 +010063 /*mp_uint_t scope_flags =*/ ip++;
64 mp_uint_t n_pos_args = *ip++;
65 mp_uint_t n_kwonly_args = *ip++;
66 /*mp_uint_t n_def_pos_args =*/ ip++;
Damien George9b7f5832015-03-18 17:47:47 +000067
68 ip = MP_ALIGN(ip, sizeof(mp_uint_t));
69
70 // get and skip arg names
71 const mp_obj_t *arg_names = (const mp_obj_t*)ip;
Damien George3a3db4d2015-10-22 23:45:37 +010072 ip += (n_pos_args + n_kwonly_args) * sizeof(mp_uint_t);
Damien George9b7f5832015-03-18 17:47:47 +000073
Damien George73496fb2014-04-13 14:51:56 +010074 const byte *code_info = ip;
Damien Georgeb534e1b2014-09-04 14:44:01 +010075 mp_uint_t code_info_size = mp_decode_uint(&code_info);
Damien George08335002014-01-18 23:24:36 +000076 ip += code_info_size;
77
Damien Georgeb534e1b2014-09-04 14:44:01 +010078 qstr block_name = mp_decode_uint(&code_info);
79 qstr source_file = mp_decode_uint(&code_info);
Damien George3eaa0c32014-10-03 17:54:25 +000080 printf("File %s, code block '%s' (descriptor: %p, bytecode @%p " UINT_FMT " bytes)\n",
Damien George9b7f5832015-03-18 17:47:47 +000081 qstr_str(source_file), qstr_str(block_name), descr, mp_showbc_code_start, len);
Paul Sokolovsky8bf84042014-06-02 16:11:16 +030082
Damien George564963a2014-10-24 14:42:50 +000083 // raw bytecode dump
84 printf("Raw bytecode (code_info_size=" UINT_FMT ", bytecode_size=" UINT_FMT "):\n", code_info_size, len - code_info_size);
85 for (mp_uint_t i = 0; i < len; i++) {
86 if (i > 0 && i % 16 == 0) {
87 printf("\n");
88 }
Paul Sokolovskydf103462014-12-28 21:34:45 +020089 printf(" %02x", mp_showbc_code_start[i]);
Damien George564963a2014-10-24 14:42:50 +000090 }
91 printf("\n");
92
Damien George1084b0f2014-10-25 15:07:02 +010093 // bytecode prelude: arg names (as qstr objects)
94 printf("arg names:");
Damien George3a3db4d2015-10-22 23:45:37 +010095 for (mp_uint_t i = 0; i < n_pos_args + n_kwonly_args; i++) {
Damien George9b7f5832015-03-18 17:47:47 +000096 printf(" %s", qstr_str(MP_OBJ_QSTR_VALUE(arg_names[i])));
Damien George1084b0f2014-10-25 15:07:02 +010097 }
98 printf("\n");
99
Damien George9b7f5832015-03-18 17:47:47 +0000100 printf("(N_STATE " UINT_FMT ")\n", n_state);
101 printf("(N_EXC_STACK " UINT_FMT ")\n", n_exc_stack);
102
103 // for printing line number info
104 const byte *bytecode_start = ip;
Damien George440f0412014-03-28 18:38:20 +0000105
106 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +0000107 {
Damien Georgec9aa1882015-04-07 00:08:17 +0100108 uint local_num;
109 while ((local_num = *ip++) != 255) {
Damien George6baf76e2013-12-30 22:32:17 +0000110 printf("(INIT_CELL %u)\n", local_num);
111 }
Paul Sokolovskydf103462014-12-28 21:34:45 +0200112 len -= ip - mp_showbc_code_start;
Damien George6baf76e2013-12-30 22:32:17 +0000113 }
114
Damien George73496fb2014-04-13 14:51:56 +0100115 // print out line number info
116 {
Damien George9b7f5832015-03-18 17:47:47 +0000117 mp_int_t bc = bytecode_start - ip;
Damien George40f3c022014-07-03 13:25:24 +0100118 mp_uint_t source_line = 1;
Damien George73496fb2014-04-13 14:51:56 +0100119 printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
Damien George564963a2014-10-24 14:42:50 +0000120 for (const byte* ci = code_info; *ci;) {
Damien George4747bec2014-07-31 16:12:01 +0000121 if ((ci[0] & 0x80) == 0) {
122 // 0b0LLBBBBB encoding
123 bc += ci[0] & 0x1f;
124 source_line += ci[0] >> 5;
125 ci += 1;
126 } else {
127 // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
128 bc += ci[0] & 0xf;
129 source_line += ((ci[0] << 4) & 0x700) | ci[1];
130 ci += 2;
131 }
Damien George73496fb2014-04-13 14:51:56 +0100132 printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
133 }
134 }
Damien George3417bc22014-05-10 10:36:38 +0100135 mp_bytecode_print2(ip, len - 0);
Paul Sokolovskyc5e32c62014-04-23 03:40:24 +0300136}
Damien George73496fb2014-04-13 14:51:56 +0100137
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200138const byte *mp_bytecode_print_str(const byte *ip) {
Damien George40f3c022014-07-03 13:25:24 +0100139 mp_uint_t unum;
Damien George50912e72015-01-20 11:55:10 +0000140 qstr qst;
Damienf03001f2013-11-17 13:19:33 +0000141
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200142 switch (*ip++) {
143 case MP_BC_LOAD_CONST_FALSE:
144 printf("LOAD_CONST_FALSE");
145 break;
Damienf03001f2013-11-17 13:19:33 +0000146
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200147 case MP_BC_LOAD_CONST_NONE:
148 printf("LOAD_CONST_NONE");
149 break;
Damienf03001f2013-11-17 13:19:33 +0000150
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200151 case MP_BC_LOAD_CONST_TRUE:
152 printf("LOAD_CONST_TRUE");
153 break;
Damien Georgee9906ac2014-01-04 18:44:46 +0000154
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200155 case MP_BC_LOAD_CONST_SMALL_INT: {
156 mp_int_t num = 0;
157 if ((ip[0] & 0x40) != 0) {
158 // Number is negative
159 num--;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200160 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200161 do {
162 num = (num << 7) | (*ip & 0x7f);
163 } while ((*ip++ & 0x80) != 0);
164 printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
165 break;
166 }
Damienf03001f2013-11-17 13:19:33 +0000167
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200168 case MP_BC_LOAD_CONST_STRING:
169 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000170 printf("LOAD_CONST_STRING '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200171 break;
Damienf03001f2013-11-17 13:19:33 +0000172
Damien Georged6ed6702015-01-13 23:08:47 +0000173 case MP_BC_LOAD_CONST_OBJ:
174 DECODE_PTR;
175 printf("LOAD_CONST_OBJ %p=", (void*)unum);
Damien George59fba2d2015-06-25 14:42:13 +0000176 mp_obj_print_helper(&mp_plat_print, (mp_obj_t)unum, PRINT_REPR);
Damien Georged6ed6702015-01-13 23:08:47 +0000177 break;
178
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200179 case MP_BC_LOAD_NULL:
180 printf("LOAD_NULL");
181 break;
Paul Sokolovsky00a9d132014-04-12 00:32:38 +0300182
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200183 case MP_BC_LOAD_FAST_N:
184 DECODE_UINT;
185 printf("LOAD_FAST_N " UINT_FMT, unum);
186 break;
Damienf03001f2013-11-17 13:19:33 +0000187
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200188 case MP_BC_LOAD_DEREF:
189 DECODE_UINT;
190 printf("LOAD_DEREF " UINT_FMT, unum);
191 break;
Damien George6baf76e2013-12-30 22:32:17 +0000192
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200193 case MP_BC_LOAD_NAME:
194 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000195 printf("LOAD_NAME %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000196 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
197 printf(" (cache=%u)", *ip++);
198 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200199 break;
Damienf03001f2013-11-17 13:19:33 +0000200
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200201 case MP_BC_LOAD_GLOBAL:
202 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000203 printf("LOAD_GLOBAL %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000204 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
205 printf(" (cache=%u)", *ip++);
206 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200207 break;
Damienf03001f2013-11-17 13:19:33 +0000208
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200209 case MP_BC_LOAD_ATTR:
210 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000211 printf("LOAD_ATTR %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000212 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
213 printf(" (cache=%u)", *ip++);
214 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200215 break;
Damienf03001f2013-11-17 13:19:33 +0000216
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200217 case MP_BC_LOAD_METHOD:
218 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000219 printf("LOAD_METHOD %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200220 break;
Damienf03001f2013-11-17 13:19:33 +0000221
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200222 case MP_BC_LOAD_BUILD_CLASS:
223 printf("LOAD_BUILD_CLASS");
224 break;
Damienf03001f2013-11-17 13:19:33 +0000225
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200226 case MP_BC_LOAD_SUBSCR:
227 printf("LOAD_SUBSCR");
228 break;
Damien George729f7b42014-04-17 22:10:53 +0100229
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200230 case MP_BC_STORE_FAST_N:
231 DECODE_UINT;
232 printf("STORE_FAST_N " UINT_FMT, unum);
233 break;
Damienf03001f2013-11-17 13:19:33 +0000234
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200235 case MP_BC_STORE_DEREF:
236 DECODE_UINT;
237 printf("STORE_DEREF " UINT_FMT, unum);
238 break;
Damien George6baf76e2013-12-30 22:32:17 +0000239
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200240 case MP_BC_STORE_NAME:
241 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000242 printf("STORE_NAME %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_STORE_GLOBAL:
246 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000247 printf("STORE_GLOBAL %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200248 break;
Damienf03001f2013-11-17 13:19:33 +0000249
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200250 case MP_BC_STORE_ATTR:
251 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000252 printf("STORE_ATTR %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000253 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
254 printf(" (cache=%u)", *ip++);
255 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200256 break;
Damienf03001f2013-11-17 13:19:33 +0000257
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200258 case MP_BC_STORE_SUBSCR:
259 printf("STORE_SUBSCR");
260 break;
Damienf03001f2013-11-17 13:19:33 +0000261
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200262 case MP_BC_DELETE_FAST:
263 DECODE_UINT;
264 printf("DELETE_FAST " UINT_FMT, unum);
265 break;
Damien George2bf7c092014-04-09 15:26:46 +0100266
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200267 case MP_BC_DELETE_DEREF:
268 DECODE_UINT;
269 printf("DELETE_DEREF " UINT_FMT, unum);
270 break;
Damien George2bf7c092014-04-09 15:26:46 +0100271
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200272 case MP_BC_DELETE_NAME:
273 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000274 printf("DELETE_NAME %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200275 break;
Damien Georgeddaf6c12014-02-06 20:31:32 +0000276
Damien George8e9a7122015-03-20 17:12:09 +0000277 case MP_BC_DELETE_GLOBAL:
278 DECODE_QSTR;
279 printf("DELETE_GLOBAL %s", qstr_str(qst));
280 break;
281
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200282 case MP_BC_DUP_TOP:
283 printf("DUP_TOP");
284 break;
Damienf03001f2013-11-17 13:19:33 +0000285
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200286 case MP_BC_DUP_TOP_TWO:
287 printf("DUP_TOP_TWO");
288 break;
Damienf03001f2013-11-17 13:19:33 +0000289
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200290 case MP_BC_POP_TOP:
291 printf("POP_TOP");
292 break;
Damienf03001f2013-11-17 13:19:33 +0000293
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200294 case MP_BC_ROT_TWO:
295 printf("ROT_TWO");
296 break;
Damienf03001f2013-11-17 13:19:33 +0000297
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200298 case MP_BC_ROT_THREE:
299 printf("ROT_THREE");
300 break;
Damienf03001f2013-11-17 13:19:33 +0000301
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200302 case MP_BC_JUMP:
303 DECODE_SLABEL;
Paul Sokolovskydf103462014-12-28 21:34:45 +0200304 printf("JUMP " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200305 break;
Damienf03001f2013-11-17 13:19:33 +0000306
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200307 case MP_BC_POP_JUMP_IF_TRUE:
308 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200309 printf("POP_JUMP_IF_TRUE " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200310 break;
Damienf03001f2013-11-17 13:19:33 +0000311
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200312 case MP_BC_POP_JUMP_IF_FALSE:
313 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200314 printf("POP_JUMP_IF_FALSE " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200315 break;
Damienf03001f2013-11-17 13:19:33 +0000316
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200317 case MP_BC_JUMP_IF_TRUE_OR_POP:
318 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200319 printf("JUMP_IF_TRUE_OR_POP " 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_JUMP_IF_FALSE_OR_POP:
323 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200324 printf("JUMP_IF_FALSE_OR_POP " 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_SETUP_WITH:
328 DECODE_ULABEL; // loop-like labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200329 printf("SETUP_WITH " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200330 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200331
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200332 case MP_BC_WITH_CLEANUP:
333 printf("WITH_CLEANUP");
334 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200335
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200336 case MP_BC_UNWIND_JUMP:
337 DECODE_SLABEL;
Paul Sokolovskydf103462014-12-28 21:34:45 +0200338 printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - mp_showbc_code_start, *ip);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200339 ip += 1;
340 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200341
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200342 case MP_BC_SETUP_EXCEPT:
343 DECODE_ULABEL; // except labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200344 printf("SETUP_EXCEPT " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200345 break;
Damienf03001f2013-11-17 13:19:33 +0000346
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200347 case MP_BC_SETUP_FINALLY:
348 DECODE_ULABEL; // except labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200349 printf("SETUP_FINALLY " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200350 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200351
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200352 case MP_BC_END_FINALLY:
353 // if TOS is an exception, reraises the exception (3 values on TOS)
354 // if TOS is an integer, does something else
355 // if TOS is None, just pops it and continues
356 // else error
357 printf("END_FINALLY");
358 break;
Damienf03001f2013-11-17 13:19:33 +0000359
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200360 case MP_BC_GET_ITER:
361 printf("GET_ITER");
362 break;
Damienf03001f2013-11-17 13:19:33 +0000363
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200364 case MP_BC_FOR_ITER:
365 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200366 printf("FOR_ITER " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200367 break;
Damienf03001f2013-11-17 13:19:33 +0000368
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200369 case MP_BC_POP_BLOCK:
370 // pops block and restores the stack
371 printf("POP_BLOCK");
372 break;
Damienf03001f2013-11-17 13:19:33 +0000373
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200374 case MP_BC_POP_EXCEPT:
375 // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
376 printf("POP_EXCEPT");
377 break;
Damienf03001f2013-11-17 13:19:33 +0000378
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200379 case MP_BC_NOT:
380 printf("NOT");
381 break;
Damien George9aa2a522014-02-01 23:04:09 +0000382
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200383 case MP_BC_BUILD_TUPLE:
384 DECODE_UINT;
385 printf("BUILD_TUPLE " UINT_FMT, unum);
386 break;
Damienf03001f2013-11-17 13:19:33 +0000387
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200388 case MP_BC_BUILD_LIST:
389 DECODE_UINT;
390 printf("BUILD_LIST " UINT_FMT, unum);
391 break;
Damienf03001f2013-11-17 13:19:33 +0000392
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200393 case MP_BC_LIST_APPEND:
394 DECODE_UINT;
395 printf("LIST_APPEND " UINT_FMT, unum);
396 break;
Damienf03001f2013-11-17 13:19:33 +0000397
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200398 case MP_BC_BUILD_MAP:
399 DECODE_UINT;
400 printf("BUILD_MAP " UINT_FMT, unum);
401 break;
Damienf03001f2013-11-17 13:19:33 +0000402
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200403 case MP_BC_STORE_MAP:
404 printf("STORE_MAP");
405 break;
Damienf03001f2013-11-17 13:19:33 +0000406
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200407 case MP_BC_MAP_ADD:
408 DECODE_UINT;
409 printf("MAP_ADD " UINT_FMT, unum);
410 break;
Damienf03001f2013-11-17 13:19:33 +0000411
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200412 case MP_BC_BUILD_SET:
413 DECODE_UINT;
414 printf("BUILD_SET " UINT_FMT, unum);
415 break;
Damienf03001f2013-11-17 13:19:33 +0000416
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200417 case MP_BC_SET_ADD:
418 DECODE_UINT;
419 printf("SET_ADD " UINT_FMT, unum);
420 break;
Damienf03001f2013-11-17 13:19:33 +0000421
Damien Georgefb510b32014-06-01 13:32:54 +0100422#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200423 case MP_BC_BUILD_SLICE:
424 DECODE_UINT;
425 printf("BUILD_SLICE " UINT_FMT, unum);
426 break;
Damien George20006db2014-01-18 14:10:48 +0000427#endif
428
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200429 case MP_BC_UNPACK_SEQUENCE:
430 DECODE_UINT;
431 printf("UNPACK_SEQUENCE " UINT_FMT, unum);
432 break;
Damienff099f32013-11-26 15:14:50 +0000433
Damien Georgec8870b72015-06-18 15:12:17 +0000434 case MP_BC_UNPACK_EX:
435 DECODE_UINT;
436 printf("UNPACK_EX " UINT_FMT, unum);
437 break;
438
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200439 case MP_BC_MAKE_FUNCTION:
440 DECODE_PTR;
441 printf("MAKE_FUNCTION %p", (void*)unum);
442 break;
Damienf03001f2013-11-17 13:19:33 +0000443
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200444 case MP_BC_MAKE_FUNCTION_DEFARGS:
445 DECODE_PTR;
446 printf("MAKE_FUNCTION_DEFARGS %p", (void*)unum);
447 break;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200448
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200449 case MP_BC_MAKE_CLOSURE: {
450 DECODE_PTR;
451 mp_uint_t n_closed_over = *ip++;
452 printf("MAKE_CLOSURE %p " UINT_FMT, (void*)unum, n_closed_over);
453 break;
Damienf03001f2013-11-17 13:19:33 +0000454 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200455
456 case MP_BC_MAKE_CLOSURE_DEFARGS: {
457 DECODE_PTR;
458 mp_uint_t n_closed_over = *ip++;
459 printf("MAKE_CLOSURE_DEFARGS %p " UINT_FMT, (void*)unum, n_closed_over);
460 break;
461 }
462
463 case MP_BC_CALL_FUNCTION:
464 DECODE_UINT;
465 printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
466 break;
467
468 case MP_BC_CALL_FUNCTION_VAR_KW:
469 DECODE_UINT;
470 printf("CALL_FUNCTION_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
471 break;
472
473 case MP_BC_CALL_METHOD:
474 DECODE_UINT;
475 printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
476 break;
477
478 case MP_BC_CALL_METHOD_VAR_KW:
479 DECODE_UINT;
480 printf("CALL_METHOD_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
481 break;
482
483 case MP_BC_RETURN_VALUE:
484 printf("RETURN_VALUE");
485 break;
486
487 case MP_BC_RAISE_VARARGS:
488 unum = *ip++;
489 printf("RAISE_VARARGS " UINT_FMT, unum);
490 break;
491
492 case MP_BC_YIELD_VALUE:
493 printf("YIELD_VALUE");
494 break;
495
496 case MP_BC_YIELD_FROM:
497 printf("YIELD_FROM");
498 break;
499
500 case MP_BC_IMPORT_NAME:
501 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000502 printf("IMPORT_NAME '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200503 break;
504
505 case MP_BC_IMPORT_FROM:
506 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000507 printf("IMPORT_FROM '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200508 break;
509
510 case MP_BC_IMPORT_STAR:
511 printf("IMPORT_STAR");
512 break;
513
514 default:
515 if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) {
516 printf("LOAD_CONST_SMALL_INT " INT_FMT, (mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16);
517 } else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) {
518 printf("LOAD_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI);
519 } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) {
520 printf("STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI);
Damien Georgec8870b72015-06-18 15:12:17 +0000521 } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + 6) {
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200522 printf("UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI);
Damien Georgec8870b72015-06-18 15:12:17 +0000523 } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + 36) {
Paul Sokolovsky1ee17852014-12-28 21:41:58 +0200524 mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI;
525 printf("BINARY_OP " UINT_FMT " %s", op, qstr_str(mp_binary_op_method_name[op]));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200526 } else {
527 printf("code %p, byte code 0x%02x not implemented\n", ip, ip[-1]);
528 assert(0);
529 return ip;
530 }
531 break;
532 }
533
534 return ip;
535}
536
537void mp_bytecode_print2(const byte *ip, mp_uint_t len) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200538 mp_showbc_code_start = ip;
Damien George963a5a32015-01-16 17:47:07 +0000539 while (ip < len + mp_showbc_code_start) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200540 printf("%02u ", (uint)(ip - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200541 ip = mp_bytecode_print_str(ip);
Damienf03001f2013-11-17 13:19:33 +0000542 printf("\n");
543 }
544}
Damien Georged3ebe482014-01-07 15:20:33 +0000545
Damien Georgecbd2f742014-01-19 11:48:48 +0000546#endif // MICROPY_DEBUG_PRINTERS