blob: 51a1671f69087bb543b0c16354e0e347539cf4c7 [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
Paul Sokolovsky1ee17852014-12-28 21:41:58 +020033extern const qstr mp_binary_op_method_name[];
Damienf03001f2013-11-17 13:19:33 +000034
Damien Georgecbd2f742014-01-19 11:48:48 +000035#if MICROPY_DEBUG_PRINTERS
Damien Georged3ebe482014-01-07 15:20:33 +000036
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020037#define DECODE_UINT { \
38 unum = 0; \
39 do { \
40 unum = (unum << 7) + (*ip & 0x7f); \
41 } while ((*ip++ & 0x80) != 0); \
42}
Damienf03001f2013-11-17 13:19:33 +000043#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
44#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
Paul Sokolovsky1d30b112014-02-21 02:31:05 +020045#define DECODE_QSTR { \
46 qstr = 0; \
47 do { \
48 qstr = (qstr << 7) + (*ip & 0x7f); \
49 } while ((*ip++ & 0x80) != 0); \
50}
Damien George3d484d92014-04-13 11:22:44 +010051#define DECODE_PTR do { \
Damien George40f3c022014-07-03 13:25:24 +010052 ip = (byte*)(((mp_uint_t)ip + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1))); /* align ip */ \
53 unum = *(mp_uint_t*)ip; \
54 ip += sizeof(mp_uint_t); \
Damien George3d484d92014-04-13 11:22:44 +010055} while (0)
Damienf03001f2013-11-17 13:19:33 +000056
Paul Sokolovskydf103462014-12-28 21:34:45 +020057const byte *mp_showbc_code_start;
Paul Sokolovsky343266e2014-12-27 05:00:08 +020058
Damien George1084b0f2014-10-25 15:07:02 +010059void mp_bytecode_print(const void *descr, mp_uint_t n_total_args, const byte *ip, mp_uint_t len) {
Paul Sokolovskydf103462014-12-28 21:34:45 +020060 mp_showbc_code_start = ip;
Damien George6baf76e2013-12-30 22:32:17 +000061
Damien George08335002014-01-18 23:24:36 +000062 // get code info size
Damien George73496fb2014-04-13 14:51:56 +010063 const byte *code_info = ip;
Damien Georgeb534e1b2014-09-04 14:44:01 +010064 mp_uint_t code_info_size = mp_decode_uint(&code_info);
Damien George08335002014-01-18 23:24:36 +000065 ip += code_info_size;
66
Damien Georgeb534e1b2014-09-04 14:44:01 +010067 qstr block_name = mp_decode_uint(&code_info);
68 qstr source_file = mp_decode_uint(&code_info);
Damien George3eaa0c32014-10-03 17:54:25 +000069 printf("File %s, code block '%s' (descriptor: %p, bytecode @%p " UINT_FMT " bytes)\n",
Paul Sokolovskya4ac5b92014-06-03 01:24:29 +030070 qstr_str(source_file), qstr_str(block_name), descr, code_info, len);
Paul Sokolovsky8bf84042014-06-02 16:11:16 +030071
Damien George564963a2014-10-24 14:42:50 +000072 // raw bytecode dump
73 printf("Raw bytecode (code_info_size=" UINT_FMT ", bytecode_size=" UINT_FMT "):\n", code_info_size, len - code_info_size);
74 for (mp_uint_t i = 0; i < len; i++) {
75 if (i > 0 && i % 16 == 0) {
76 printf("\n");
77 }
Paul Sokolovskydf103462014-12-28 21:34:45 +020078 printf(" %02x", mp_showbc_code_start[i]);
Damien George564963a2014-10-24 14:42:50 +000079 }
80 printf("\n");
81
Damien George1084b0f2014-10-25 15:07:02 +010082 // bytecode prelude: arg names (as qstr objects)
83 printf("arg names:");
Damien George963a5a32015-01-16 17:47:07 +000084 for (mp_uint_t i = 0; i < n_total_args; i++) {
Damien George1084b0f2014-10-25 15:07:02 +010085 printf(" %s", qstr_str(MP_OBJ_QSTR_VALUE(*(mp_obj_t*)ip)));
86 ip += sizeof(mp_obj_t);
87 }
88 printf("\n");
89
Damien George440f0412014-03-28 18:38:20 +000090 // bytecode prelude: state size and exception stack size; 16 bit uints
91 {
Damien Georgeb534e1b2014-09-04 14:44:01 +010092 uint n_state = mp_decode_uint(&ip);
93 uint n_exc_stack = mp_decode_uint(&ip);
Damien George440f0412014-03-28 18:38:20 +000094 printf("(N_STATE %u)\n", n_state);
95 printf("(N_EXC_STACK %u)\n", n_exc_stack);
96 }
97
98 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +000099 {
100 uint n_local = *ip++;
101 printf("(NUM_LOCAL %u)\n", n_local);
102 for (; n_local > 0; n_local--) {
103 uint local_num = *ip++;
104 printf("(INIT_CELL %u)\n", local_num);
105 }
Paul Sokolovskydf103462014-12-28 21:34:45 +0200106 len -= ip - mp_showbc_code_start;
Damien George6baf76e2013-12-30 22:32:17 +0000107 }
108
Damien George73496fb2014-04-13 14:51:56 +0100109 // print out line number info
110 {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200111 mp_int_t bc = (mp_showbc_code_start + code_info_size) - ip; // start counting from the prelude
Damien George40f3c022014-07-03 13:25:24 +0100112 mp_uint_t source_line = 1;
Damien George73496fb2014-04-13 14:51:56 +0100113 printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
Damien George564963a2014-10-24 14:42:50 +0000114 for (const byte* ci = code_info; *ci;) {
Damien George4747bec2014-07-31 16:12:01 +0000115 if ((ci[0] & 0x80) == 0) {
116 // 0b0LLBBBBB encoding
117 bc += ci[0] & 0x1f;
118 source_line += ci[0] >> 5;
119 ci += 1;
120 } else {
121 // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
122 bc += ci[0] & 0xf;
123 source_line += ((ci[0] << 4) & 0x700) | ci[1];
124 ci += 2;
125 }
Damien George73496fb2014-04-13 14:51:56 +0100126 printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
127 }
128 }
Damien George3417bc22014-05-10 10:36:38 +0100129 mp_bytecode_print2(ip, len - 0);
Paul Sokolovskyc5e32c62014-04-23 03:40:24 +0300130}
Damien George73496fb2014-04-13 14:51:56 +0100131
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200132const byte *mp_bytecode_print_str(const byte *ip) {
Damien George40f3c022014-07-03 13:25:24 +0100133 mp_uint_t unum;
Damienf03001f2013-11-17 13:19:33 +0000134 qstr qstr;
Damienf03001f2013-11-17 13:19:33 +0000135
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200136 switch (*ip++) {
137 case MP_BC_LOAD_CONST_FALSE:
138 printf("LOAD_CONST_FALSE");
139 break;
Damienf03001f2013-11-17 13:19:33 +0000140
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200141 case MP_BC_LOAD_CONST_NONE:
142 printf("LOAD_CONST_NONE");
143 break;
Damienf03001f2013-11-17 13:19:33 +0000144
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200145 case MP_BC_LOAD_CONST_TRUE:
146 printf("LOAD_CONST_TRUE");
147 break;
Damien Georgee9906ac2014-01-04 18:44:46 +0000148
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200149 case MP_BC_LOAD_CONST_ELLIPSIS:
150 printf("LOAD_CONST_ELLIPSIS");
151 break;
152
153 case MP_BC_LOAD_CONST_SMALL_INT: {
154 mp_int_t num = 0;
155 if ((ip[0] & 0x40) != 0) {
156 // Number is negative
157 num--;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200158 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200159 do {
160 num = (num << 7) | (*ip & 0x7f);
161 } while ((*ip++ & 0x80) != 0);
162 printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
163 break;
164 }
Damienf03001f2013-11-17 13:19:33 +0000165
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200166 case MP_BC_LOAD_CONST_INT:
167 DECODE_QSTR;
168 printf("LOAD_CONST_INT %s", qstr_str(qstr));
169 break;
Paul Sokolovsky4b919d02014-01-10 04:16:50 +0200170
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200171 case MP_BC_LOAD_CONST_DEC:
172 DECODE_QSTR;
173 printf("LOAD_CONST_DEC %s", qstr_str(qstr));
174 break;
Damienf03001f2013-11-17 13:19:33 +0000175
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200176 case MP_BC_LOAD_CONST_BYTES:
177 DECODE_QSTR;
178 printf("LOAD_CONST_BYTES %s", qstr_str(qstr));
179 break;
Paul Sokolovskyb9b1c002014-04-12 00:34:57 +0300180
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200181 case MP_BC_LOAD_CONST_STRING:
182 DECODE_QSTR;
183 printf("LOAD_CONST_STRING '%s'", qstr_str(qstr));
184 break;
Damienf03001f2013-11-17 13:19:33 +0000185
Damien Georged6ed6702015-01-13 23:08:47 +0000186 case MP_BC_LOAD_CONST_OBJ:
187 DECODE_PTR;
188 printf("LOAD_CONST_OBJ %p=", (void*)unum);
189 mp_obj_print((mp_obj_t)unum, PRINT_REPR);
190 break;
191
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200192 case MP_BC_LOAD_NULL:
193 printf("LOAD_NULL");
194 break;
Paul Sokolovsky00a9d132014-04-12 00:32:38 +0300195
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200196 case MP_BC_LOAD_FAST_N:
197 DECODE_UINT;
198 printf("LOAD_FAST_N " UINT_FMT, unum);
199 break;
Damienf03001f2013-11-17 13:19:33 +0000200
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200201 case MP_BC_LOAD_DEREF:
202 DECODE_UINT;
203 printf("LOAD_DEREF " UINT_FMT, unum);
204 break;
Damien George6baf76e2013-12-30 22:32:17 +0000205
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200206 case MP_BC_LOAD_NAME:
207 DECODE_QSTR;
208 printf("LOAD_NAME %s", qstr_str(qstr));
Damien Georged6ed6702015-01-13 23:08:47 +0000209 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
210 printf(" (cache=%u)", *ip++);
211 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200212 break;
Damienf03001f2013-11-17 13:19:33 +0000213
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200214 case MP_BC_LOAD_GLOBAL:
215 DECODE_QSTR;
216 printf("LOAD_GLOBAL %s", qstr_str(qstr));
Damien Georged6ed6702015-01-13 23:08:47 +0000217 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
218 printf(" (cache=%u)", *ip++);
219 }
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_ATTR:
223 DECODE_QSTR;
224 printf("LOAD_ATTR %s", qstr_str(qstr));
Damien Georged6ed6702015-01-13 23:08:47 +0000225 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
226 printf(" (cache=%u)", *ip++);
227 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200228 break;
Damienf03001f2013-11-17 13:19:33 +0000229
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200230 case MP_BC_LOAD_METHOD:
231 DECODE_QSTR;
232 printf("LOAD_METHOD %s", qstr_str(qstr));
233 break;
Damienf03001f2013-11-17 13:19:33 +0000234
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200235 case MP_BC_LOAD_BUILD_CLASS:
236 printf("LOAD_BUILD_CLASS");
237 break;
Damienf03001f2013-11-17 13:19:33 +0000238
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200239 case MP_BC_LOAD_SUBSCR:
240 printf("LOAD_SUBSCR");
241 break;
Damien George729f7b42014-04-17 22:10:53 +0100242
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200243 case MP_BC_STORE_FAST_N:
244 DECODE_UINT;
245 printf("STORE_FAST_N " UINT_FMT, unum);
246 break;
Damienf03001f2013-11-17 13:19:33 +0000247
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200248 case MP_BC_STORE_DEREF:
249 DECODE_UINT;
250 printf("STORE_DEREF " UINT_FMT, unum);
251 break;
Damien George6baf76e2013-12-30 22:32:17 +0000252
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200253 case MP_BC_STORE_NAME:
254 DECODE_QSTR;
255 printf("STORE_NAME %s", qstr_str(qstr));
256 break;
Damienf03001f2013-11-17 13:19:33 +0000257
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200258 case MP_BC_STORE_GLOBAL:
259 DECODE_QSTR;
260 printf("STORE_GLOBAL %s", qstr_str(qstr));
261 break;
Damienf03001f2013-11-17 13:19:33 +0000262
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200263 case MP_BC_STORE_ATTR:
264 DECODE_QSTR;
265 printf("STORE_ATTR %s", qstr_str(qstr));
Damien Georged6ed6702015-01-13 23:08:47 +0000266 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
267 printf(" (cache=%u)", *ip++);
268 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200269 break;
Damienf03001f2013-11-17 13:19:33 +0000270
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200271 case MP_BC_STORE_SUBSCR:
272 printf("STORE_SUBSCR");
273 break;
Damienf03001f2013-11-17 13:19:33 +0000274
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200275 case MP_BC_DELETE_FAST:
276 DECODE_UINT;
277 printf("DELETE_FAST " UINT_FMT, unum);
278 break;
Damien George2bf7c092014-04-09 15:26:46 +0100279
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200280 case MP_BC_DELETE_DEREF:
281 DECODE_UINT;
282 printf("DELETE_DEREF " UINT_FMT, unum);
283 break;
Damien George2bf7c092014-04-09 15:26:46 +0100284
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200285 case MP_BC_DELETE_NAME:
286 DECODE_QSTR;
287 printf("DELETE_NAME %s", qstr_str(qstr));
288 break;
Damien Georgeddaf6c12014-02-06 20:31:32 +0000289
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200290 case MP_BC_DUP_TOP:
291 printf("DUP_TOP");
292 break;
Damienf03001f2013-11-17 13:19:33 +0000293
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200294 case MP_BC_DUP_TOP_TWO:
295 printf("DUP_TOP_TWO");
296 break;
Damienf03001f2013-11-17 13:19:33 +0000297
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200298 case MP_BC_POP_TOP:
299 printf("POP_TOP");
300 break;
Damienf03001f2013-11-17 13:19:33 +0000301
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200302 case MP_BC_ROT_TWO:
303 printf("ROT_TWO");
304 break;
Damienf03001f2013-11-17 13:19:33 +0000305
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200306 case MP_BC_ROT_THREE:
307 printf("ROT_THREE");
308 break;
Damienf03001f2013-11-17 13:19:33 +0000309
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200310 case MP_BC_JUMP:
311 DECODE_SLABEL;
Paul Sokolovskydf103462014-12-28 21:34:45 +0200312 printf("JUMP " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200313 break;
Damienf03001f2013-11-17 13:19:33 +0000314
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200315 case MP_BC_POP_JUMP_IF_TRUE:
316 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200317 printf("POP_JUMP_IF_TRUE " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200318 break;
Damienf03001f2013-11-17 13:19:33 +0000319
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200320 case MP_BC_POP_JUMP_IF_FALSE:
321 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200322 printf("POP_JUMP_IF_FALSE " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200323 break;
Damienf03001f2013-11-17 13:19:33 +0000324
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200325 case MP_BC_JUMP_IF_TRUE_OR_POP:
326 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200327 printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, 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_JUMP_IF_FALSE_OR_POP:
331 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200332 printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, 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_SETUP_WITH:
336 DECODE_ULABEL; // loop-like labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200337 printf("SETUP_WITH " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200338 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200339
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200340 case MP_BC_WITH_CLEANUP:
341 printf("WITH_CLEANUP");
342 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200343
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200344 case MP_BC_UNWIND_JUMP:
345 DECODE_SLABEL;
Paul Sokolovskydf103462014-12-28 21:34:45 +0200346 printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - mp_showbc_code_start, *ip);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200347 ip += 1;
348 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200349
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200350 case MP_BC_SETUP_EXCEPT:
351 DECODE_ULABEL; // except labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200352 printf("SETUP_EXCEPT " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200353 break;
Damienf03001f2013-11-17 13:19:33 +0000354
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200355 case MP_BC_SETUP_FINALLY:
356 DECODE_ULABEL; // except labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200357 printf("SETUP_FINALLY " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200358 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200359
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200360 case MP_BC_END_FINALLY:
361 // if TOS is an exception, reraises the exception (3 values on TOS)
362 // if TOS is an integer, does something else
363 // if TOS is None, just pops it and continues
364 // else error
365 printf("END_FINALLY");
366 break;
Damienf03001f2013-11-17 13:19:33 +0000367
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200368 case MP_BC_GET_ITER:
369 printf("GET_ITER");
370 break;
Damienf03001f2013-11-17 13:19:33 +0000371
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200372 case MP_BC_FOR_ITER:
373 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200374 printf("FOR_ITER " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200375 break;
Damienf03001f2013-11-17 13:19:33 +0000376
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200377 case MP_BC_POP_BLOCK:
378 // pops block and restores the stack
379 printf("POP_BLOCK");
380 break;
Damienf03001f2013-11-17 13:19:33 +0000381
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200382 case MP_BC_POP_EXCEPT:
383 // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
384 printf("POP_EXCEPT");
385 break;
Damienf03001f2013-11-17 13:19:33 +0000386
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200387 case MP_BC_NOT:
388 printf("NOT");
389 break;
Damien George9aa2a522014-02-01 23:04:09 +0000390
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200391 case MP_BC_BUILD_TUPLE:
392 DECODE_UINT;
393 printf("BUILD_TUPLE " UINT_FMT, unum);
394 break;
Damienf03001f2013-11-17 13:19:33 +0000395
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200396 case MP_BC_BUILD_LIST:
397 DECODE_UINT;
398 printf("BUILD_LIST " UINT_FMT, unum);
399 break;
Damienf03001f2013-11-17 13:19:33 +0000400
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200401 case MP_BC_LIST_APPEND:
402 DECODE_UINT;
403 printf("LIST_APPEND " UINT_FMT, unum);
404 break;
Damienf03001f2013-11-17 13:19:33 +0000405
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200406 case MP_BC_BUILD_MAP:
407 DECODE_UINT;
408 printf("BUILD_MAP " UINT_FMT, unum);
409 break;
Damienf03001f2013-11-17 13:19:33 +0000410
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200411 case MP_BC_STORE_MAP:
412 printf("STORE_MAP");
413 break;
Damienf03001f2013-11-17 13:19:33 +0000414
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200415 case MP_BC_MAP_ADD:
416 DECODE_UINT;
417 printf("MAP_ADD " UINT_FMT, unum);
418 break;
Damienf03001f2013-11-17 13:19:33 +0000419
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200420 case MP_BC_BUILD_SET:
421 DECODE_UINT;
422 printf("BUILD_SET " UINT_FMT, unum);
423 break;
Damienf03001f2013-11-17 13:19:33 +0000424
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200425 case MP_BC_SET_ADD:
426 DECODE_UINT;
427 printf("SET_ADD " 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
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200437 case MP_BC_UNPACK_SEQUENCE:
438 DECODE_UINT;
439 printf("UNPACK_SEQUENCE " UINT_FMT, unum);
440 break;
Damienff099f32013-11-26 15:14:50 +0000441
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200442 case MP_BC_MAKE_FUNCTION:
443 DECODE_PTR;
444 printf("MAKE_FUNCTION %p", (void*)unum);
445 break;
Damienf03001f2013-11-17 13:19:33 +0000446
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200447 case MP_BC_MAKE_FUNCTION_DEFARGS:
448 DECODE_PTR;
449 printf("MAKE_FUNCTION_DEFARGS %p", (void*)unum);
450 break;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200451
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200452 case MP_BC_MAKE_CLOSURE: {
453 DECODE_PTR;
454 mp_uint_t n_closed_over = *ip++;
455 printf("MAKE_CLOSURE %p " UINT_FMT, (void*)unum, n_closed_over);
456 break;
Damienf03001f2013-11-17 13:19:33 +0000457 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200458
459 case MP_BC_MAKE_CLOSURE_DEFARGS: {
460 DECODE_PTR;
461 mp_uint_t n_closed_over = *ip++;
462 printf("MAKE_CLOSURE_DEFARGS %p " UINT_FMT, (void*)unum, n_closed_over);
463 break;
464 }
465
466 case MP_BC_CALL_FUNCTION:
467 DECODE_UINT;
468 printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
469 break;
470
471 case MP_BC_CALL_FUNCTION_VAR_KW:
472 DECODE_UINT;
473 printf("CALL_FUNCTION_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
474 break;
475
476 case MP_BC_CALL_METHOD:
477 DECODE_UINT;
478 printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
479 break;
480
481 case MP_BC_CALL_METHOD_VAR_KW:
482 DECODE_UINT;
483 printf("CALL_METHOD_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
484 break;
485
486 case MP_BC_RETURN_VALUE:
487 printf("RETURN_VALUE");
488 break;
489
490 case MP_BC_RAISE_VARARGS:
491 unum = *ip++;
492 printf("RAISE_VARARGS " UINT_FMT, unum);
493 break;
494
495 case MP_BC_YIELD_VALUE:
496 printf("YIELD_VALUE");
497 break;
498
499 case MP_BC_YIELD_FROM:
500 printf("YIELD_FROM");
501 break;
502
503 case MP_BC_IMPORT_NAME:
504 DECODE_QSTR;
505 printf("IMPORT_NAME '%s'", qstr_str(qstr));
506 break;
507
508 case MP_BC_IMPORT_FROM:
509 DECODE_QSTR;
510 printf("IMPORT_FROM '%s'", qstr_str(qstr));
511 break;
512
513 case MP_BC_IMPORT_STAR:
514 printf("IMPORT_STAR");
515 break;
516
517 default:
518 if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) {
519 printf("LOAD_CONST_SMALL_INT " INT_FMT, (mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16);
520 } else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) {
521 printf("LOAD_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI);
522 } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) {
523 printf("STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI);
524 } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + 5) {
525 printf("UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI);
526 } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + 35) {
Paul Sokolovsky1ee17852014-12-28 21:41:58 +0200527 mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI;
528 printf("BINARY_OP " UINT_FMT " %s", op, qstr_str(mp_binary_op_method_name[op]));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200529 } else {
530 printf("code %p, byte code 0x%02x not implemented\n", ip, ip[-1]);
531 assert(0);
532 return ip;
533 }
534 break;
535 }
536
537 return ip;
538}
539
540void mp_bytecode_print2(const byte *ip, mp_uint_t len) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200541 mp_showbc_code_start = ip;
Damien George963a5a32015-01-16 17:47:07 +0000542 while (ip < len + mp_showbc_code_start) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200543 printf("%02u ", (uint)(ip - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200544 ip = mp_bytecode_print_str(ip);
Damienf03001f2013-11-17 13:19:33 +0000545 printf("\n");
546 }
547}
Damien Georged3ebe482014-01-07 15:20:33 +0000548
Damien Georgecbd2f742014-01-19 11:48:48 +0000549#endif // MICROPY_DEBUG_PRINTERS