blob: c9eeaff9fa0f5ca49825ff52d86c6596562793e6 [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 George1084b0f2014-10-25 15:07:02 +010057void 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 +020058 mp_showbc_code_start = ip;
Damien George6baf76e2013-12-30 22:32:17 +000059
Damien George08335002014-01-18 23:24:36 +000060 // get code info size
Damien George73496fb2014-04-13 14:51:56 +010061 const byte *code_info = ip;
Damien Georgeb534e1b2014-09-04 14:44:01 +010062 mp_uint_t code_info_size = mp_decode_uint(&code_info);
Damien George08335002014-01-18 23:24:36 +000063 ip += code_info_size;
64
Damien Georgeb534e1b2014-09-04 14:44:01 +010065 qstr block_name = mp_decode_uint(&code_info);
66 qstr source_file = mp_decode_uint(&code_info);
Damien George3eaa0c32014-10-03 17:54:25 +000067 printf("File %s, code block '%s' (descriptor: %p, bytecode @%p " UINT_FMT " bytes)\n",
Paul Sokolovskya4ac5b92014-06-03 01:24:29 +030068 qstr_str(source_file), qstr_str(block_name), descr, code_info, len);
Paul Sokolovsky8bf84042014-06-02 16:11:16 +030069
Damien George564963a2014-10-24 14:42:50 +000070 // raw bytecode dump
71 printf("Raw bytecode (code_info_size=" UINT_FMT ", bytecode_size=" UINT_FMT "):\n", code_info_size, len - code_info_size);
72 for (mp_uint_t i = 0; i < len; i++) {
73 if (i > 0 && i % 16 == 0) {
74 printf("\n");
75 }
Paul Sokolovskydf103462014-12-28 21:34:45 +020076 printf(" %02x", mp_showbc_code_start[i]);
Damien George564963a2014-10-24 14:42:50 +000077 }
78 printf("\n");
79
Damien George1084b0f2014-10-25 15:07:02 +010080 // bytecode prelude: arg names (as qstr objects)
81 printf("arg names:");
Damien George963a5a32015-01-16 17:47:07 +000082 for (mp_uint_t i = 0; i < n_total_args; i++) {
Damien George1084b0f2014-10-25 15:07:02 +010083 printf(" %s", qstr_str(MP_OBJ_QSTR_VALUE(*(mp_obj_t*)ip)));
84 ip += sizeof(mp_obj_t);
85 }
86 printf("\n");
87
Damien George440f0412014-03-28 18:38:20 +000088 // bytecode prelude: state size and exception stack size; 16 bit uints
89 {
Damien Georgeb534e1b2014-09-04 14:44:01 +010090 uint n_state = mp_decode_uint(&ip);
91 uint n_exc_stack = mp_decode_uint(&ip);
Damien George440f0412014-03-28 18:38:20 +000092 printf("(N_STATE %u)\n", n_state);
93 printf("(N_EXC_STACK %u)\n", n_exc_stack);
94 }
95
96 // bytecode prelude: initialise closed over variables
Damien George6baf76e2013-12-30 22:32:17 +000097 {
Damien Georgec9aa1882015-04-07 00:08:17 +010098 uint local_num;
99 while ((local_num = *ip++) != 255) {
Damien George6baf76e2013-12-30 22:32:17 +0000100 printf("(INIT_CELL %u)\n", local_num);
101 }
Paul Sokolovskydf103462014-12-28 21:34:45 +0200102 len -= ip - mp_showbc_code_start;
Damien George6baf76e2013-12-30 22:32:17 +0000103 }
104
Damien George73496fb2014-04-13 14:51:56 +0100105 // print out line number info
106 {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200107 mp_int_t bc = (mp_showbc_code_start + code_info_size) - ip; // start counting from the prelude
Damien George40f3c022014-07-03 13:25:24 +0100108 mp_uint_t source_line = 1;
Damien George73496fb2014-04-13 14:51:56 +0100109 printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
Damien George564963a2014-10-24 14:42:50 +0000110 for (const byte* ci = code_info; *ci;) {
Damien George4747bec2014-07-31 16:12:01 +0000111 if ((ci[0] & 0x80) == 0) {
112 // 0b0LLBBBBB encoding
113 bc += ci[0] & 0x1f;
114 source_line += ci[0] >> 5;
115 ci += 1;
116 } else {
117 // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
118 bc += ci[0] & 0xf;
119 source_line += ((ci[0] << 4) & 0x700) | ci[1];
120 ci += 2;
121 }
Damien George73496fb2014-04-13 14:51:56 +0100122 printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
123 }
124 }
Damien George3417bc22014-05-10 10:36:38 +0100125 mp_bytecode_print2(ip, len - 0);
Paul Sokolovskyc5e32c62014-04-23 03:40:24 +0300126}
Damien George73496fb2014-04-13 14:51:56 +0100127
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200128const byte *mp_bytecode_print_str(const byte *ip) {
Damien George40f3c022014-07-03 13:25:24 +0100129 mp_uint_t unum;
Damien George50912e72015-01-20 11:55:10 +0000130 qstr qst;
Damienf03001f2013-11-17 13:19:33 +0000131
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200132 switch (*ip++) {
133 case MP_BC_LOAD_CONST_FALSE:
134 printf("LOAD_CONST_FALSE");
135 break;
Damienf03001f2013-11-17 13:19:33 +0000136
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200137 case MP_BC_LOAD_CONST_NONE:
138 printf("LOAD_CONST_NONE");
139 break;
Damienf03001f2013-11-17 13:19:33 +0000140
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200141 case MP_BC_LOAD_CONST_TRUE:
142 printf("LOAD_CONST_TRUE");
143 break;
Damien Georgee9906ac2014-01-04 18:44:46 +0000144
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200145 case MP_BC_LOAD_CONST_ELLIPSIS:
146 printf("LOAD_CONST_ELLIPSIS");
147 break;
148
149 case MP_BC_LOAD_CONST_SMALL_INT: {
150 mp_int_t num = 0;
151 if ((ip[0] & 0x40) != 0) {
152 // Number is negative
153 num--;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200154 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200155 do {
156 num = (num << 7) | (*ip & 0x7f);
157 } while ((*ip++ & 0x80) != 0);
158 printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
159 break;
160 }
Damienf03001f2013-11-17 13:19:33 +0000161
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200162 case MP_BC_LOAD_CONST_BYTES:
163 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000164 printf("LOAD_CONST_BYTES %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200165 break;
Paul Sokolovskyb9b1c002014-04-12 00:34:57 +0300166
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200167 case MP_BC_LOAD_CONST_STRING:
168 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000169 printf("LOAD_CONST_STRING '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200170 break;
Damienf03001f2013-11-17 13:19:33 +0000171
Damien Georged6ed6702015-01-13 23:08:47 +0000172 case MP_BC_LOAD_CONST_OBJ:
173 DECODE_PTR;
174 printf("LOAD_CONST_OBJ %p=", (void*)unum);
175 mp_obj_print((mp_obj_t)unum, PRINT_REPR);
176 break;
177
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200178 case MP_BC_LOAD_NULL:
179 printf("LOAD_NULL");
180 break;
Paul Sokolovsky00a9d132014-04-12 00:32:38 +0300181
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200182 case MP_BC_LOAD_FAST_N:
183 DECODE_UINT;
184 printf("LOAD_FAST_N " UINT_FMT, unum);
185 break;
Damienf03001f2013-11-17 13:19:33 +0000186
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200187 case MP_BC_LOAD_DEREF:
188 DECODE_UINT;
189 printf("LOAD_DEREF " UINT_FMT, unum);
190 break;
Damien George6baf76e2013-12-30 22:32:17 +0000191
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200192 case MP_BC_LOAD_NAME:
193 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000194 printf("LOAD_NAME %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000195 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
196 printf(" (cache=%u)", *ip++);
197 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200198 break;
Damienf03001f2013-11-17 13:19:33 +0000199
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200200 case MP_BC_LOAD_GLOBAL:
201 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000202 printf("LOAD_GLOBAL %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000203 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
204 printf(" (cache=%u)", *ip++);
205 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200206 break;
Damienf03001f2013-11-17 13:19:33 +0000207
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200208 case MP_BC_LOAD_ATTR:
209 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000210 printf("LOAD_ATTR %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000211 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
212 printf(" (cache=%u)", *ip++);
213 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200214 break;
Damienf03001f2013-11-17 13:19:33 +0000215
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200216 case MP_BC_LOAD_METHOD:
217 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000218 printf("LOAD_METHOD %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200219 break;
Damienf03001f2013-11-17 13:19:33 +0000220
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200221 case MP_BC_LOAD_BUILD_CLASS:
222 printf("LOAD_BUILD_CLASS");
223 break;
Damienf03001f2013-11-17 13:19:33 +0000224
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200225 case MP_BC_LOAD_SUBSCR:
226 printf("LOAD_SUBSCR");
227 break;
Damien George729f7b42014-04-17 22:10:53 +0100228
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200229 case MP_BC_STORE_FAST_N:
230 DECODE_UINT;
231 printf("STORE_FAST_N " UINT_FMT, unum);
232 break;
Damienf03001f2013-11-17 13:19:33 +0000233
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200234 case MP_BC_STORE_DEREF:
235 DECODE_UINT;
236 printf("STORE_DEREF " UINT_FMT, unum);
237 break;
Damien George6baf76e2013-12-30 22:32:17 +0000238
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200239 case MP_BC_STORE_NAME:
240 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000241 printf("STORE_NAME %s", qstr_str(qst));
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_STORE_GLOBAL:
245 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000246 printf("STORE_GLOBAL %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_STORE_ATTR:
250 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000251 printf("STORE_ATTR %s", qstr_str(qst));
Damien Georged6ed6702015-01-13 23:08:47 +0000252 if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
253 printf(" (cache=%u)", *ip++);
254 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200255 break;
Damienf03001f2013-11-17 13:19:33 +0000256
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200257 case MP_BC_STORE_SUBSCR:
258 printf("STORE_SUBSCR");
259 break;
Damienf03001f2013-11-17 13:19:33 +0000260
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200261 case MP_BC_DELETE_FAST:
262 DECODE_UINT;
263 printf("DELETE_FAST " UINT_FMT, unum);
264 break;
Damien George2bf7c092014-04-09 15:26:46 +0100265
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200266 case MP_BC_DELETE_DEREF:
267 DECODE_UINT;
268 printf("DELETE_DEREF " UINT_FMT, unum);
269 break;
Damien George2bf7c092014-04-09 15:26:46 +0100270
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200271 case MP_BC_DELETE_NAME:
272 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000273 printf("DELETE_NAME %s", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200274 break;
Damien Georgeddaf6c12014-02-06 20:31:32 +0000275
Damien George8e9a7122015-03-20 17:12:09 +0000276 case MP_BC_DELETE_GLOBAL:
277 DECODE_QSTR;
278 printf("DELETE_GLOBAL %s", qstr_str(qst));
279 break;
280
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200281 case MP_BC_DUP_TOP:
282 printf("DUP_TOP");
283 break;
Damienf03001f2013-11-17 13:19:33 +0000284
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200285 case MP_BC_DUP_TOP_TWO:
286 printf("DUP_TOP_TWO");
287 break;
Damienf03001f2013-11-17 13:19:33 +0000288
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200289 case MP_BC_POP_TOP:
290 printf("POP_TOP");
291 break;
Damienf03001f2013-11-17 13:19:33 +0000292
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200293 case MP_BC_ROT_TWO:
294 printf("ROT_TWO");
295 break;
Damienf03001f2013-11-17 13:19:33 +0000296
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200297 case MP_BC_ROT_THREE:
298 printf("ROT_THREE");
299 break;
Damienf03001f2013-11-17 13:19:33 +0000300
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200301 case MP_BC_JUMP:
302 DECODE_SLABEL;
Paul Sokolovskydf103462014-12-28 21:34:45 +0200303 printf("JUMP " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200304 break;
Damienf03001f2013-11-17 13:19:33 +0000305
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200306 case MP_BC_POP_JUMP_IF_TRUE:
307 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200308 printf("POP_JUMP_IF_TRUE " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200309 break;
Damienf03001f2013-11-17 13:19:33 +0000310
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200311 case MP_BC_POP_JUMP_IF_FALSE:
312 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200313 printf("POP_JUMP_IF_FALSE " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200314 break;
Damienf03001f2013-11-17 13:19:33 +0000315
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200316 case MP_BC_JUMP_IF_TRUE_OR_POP:
317 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200318 printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200319 break;
Damienf03001f2013-11-17 13:19:33 +0000320
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200321 case MP_BC_JUMP_IF_FALSE_OR_POP:
322 DECODE_SLABEL;
Paul Sokolovskyd8bfd772015-01-07 00:24:34 +0200323 printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200324 break;
Damienf03001f2013-11-17 13:19:33 +0000325
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200326 case MP_BC_SETUP_WITH:
327 DECODE_ULABEL; // loop-like labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200328 printf("SETUP_WITH " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200329 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200330
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200331 case MP_BC_WITH_CLEANUP:
332 printf("WITH_CLEANUP");
333 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200334
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200335 case MP_BC_UNWIND_JUMP:
336 DECODE_SLABEL;
Paul Sokolovskydf103462014-12-28 21:34:45 +0200337 printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - mp_showbc_code_start, *ip);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200338 ip += 1;
339 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200340
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200341 case MP_BC_SETUP_EXCEPT:
342 DECODE_ULABEL; // except labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200343 printf("SETUP_EXCEPT " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200344 break;
Damienf03001f2013-11-17 13:19:33 +0000345
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200346 case MP_BC_SETUP_FINALLY:
347 DECODE_ULABEL; // except labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200348 printf("SETUP_FINALLY " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200349 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200350
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200351 case MP_BC_END_FINALLY:
352 // if TOS is an exception, reraises the exception (3 values on TOS)
353 // if TOS is an integer, does something else
354 // if TOS is None, just pops it and continues
355 // else error
356 printf("END_FINALLY");
357 break;
Damienf03001f2013-11-17 13:19:33 +0000358
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200359 case MP_BC_GET_ITER:
360 printf("GET_ITER");
361 break;
Damienf03001f2013-11-17 13:19:33 +0000362
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200363 case MP_BC_FOR_ITER:
364 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200365 printf("FOR_ITER " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200366 break;
Damienf03001f2013-11-17 13:19:33 +0000367
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200368 case MP_BC_POP_BLOCK:
369 // pops block and restores the stack
370 printf("POP_BLOCK");
371 break;
Damienf03001f2013-11-17 13:19:33 +0000372
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200373 case MP_BC_POP_EXCEPT:
374 // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
375 printf("POP_EXCEPT");
376 break;
Damienf03001f2013-11-17 13:19:33 +0000377
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200378 case MP_BC_NOT:
379 printf("NOT");
380 break;
Damien George9aa2a522014-02-01 23:04:09 +0000381
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200382 case MP_BC_BUILD_TUPLE:
383 DECODE_UINT;
384 printf("BUILD_TUPLE " UINT_FMT, unum);
385 break;
Damienf03001f2013-11-17 13:19:33 +0000386
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200387 case MP_BC_BUILD_LIST:
388 DECODE_UINT;
389 printf("BUILD_LIST " UINT_FMT, unum);
390 break;
Damienf03001f2013-11-17 13:19:33 +0000391
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200392 case MP_BC_LIST_APPEND:
393 DECODE_UINT;
394 printf("LIST_APPEND " UINT_FMT, unum);
395 break;
Damienf03001f2013-11-17 13:19:33 +0000396
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200397 case MP_BC_BUILD_MAP:
398 DECODE_UINT;
399 printf("BUILD_MAP " UINT_FMT, unum);
400 break;
Damienf03001f2013-11-17 13:19:33 +0000401
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200402 case MP_BC_STORE_MAP:
403 printf("STORE_MAP");
404 break;
Damienf03001f2013-11-17 13:19:33 +0000405
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200406 case MP_BC_MAP_ADD:
407 DECODE_UINT;
408 printf("MAP_ADD " UINT_FMT, unum);
409 break;
Damienf03001f2013-11-17 13:19:33 +0000410
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200411 case MP_BC_BUILD_SET:
412 DECODE_UINT;
413 printf("BUILD_SET " UINT_FMT, unum);
414 break;
Damienf03001f2013-11-17 13:19:33 +0000415
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200416 case MP_BC_SET_ADD:
417 DECODE_UINT;
418 printf("SET_ADD " UINT_FMT, unum);
419 break;
Damienf03001f2013-11-17 13:19:33 +0000420
Damien Georgefb510b32014-06-01 13:32:54 +0100421#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200422 case MP_BC_BUILD_SLICE:
423 DECODE_UINT;
424 printf("BUILD_SLICE " UINT_FMT, unum);
425 break;
Damien George20006db2014-01-18 14:10:48 +0000426#endif
427
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200428 case MP_BC_UNPACK_SEQUENCE:
429 DECODE_UINT;
430 printf("UNPACK_SEQUENCE " UINT_FMT, unum);
431 break;
Damienff099f32013-11-26 15:14:50 +0000432
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200433 case MP_BC_MAKE_FUNCTION:
434 DECODE_PTR;
435 printf("MAKE_FUNCTION %p", (void*)unum);
436 break;
Damienf03001f2013-11-17 13:19:33 +0000437
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200438 case MP_BC_MAKE_FUNCTION_DEFARGS:
439 DECODE_PTR;
440 printf("MAKE_FUNCTION_DEFARGS %p", (void*)unum);
441 break;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200442
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200443 case MP_BC_MAKE_CLOSURE: {
444 DECODE_PTR;
445 mp_uint_t n_closed_over = *ip++;
446 printf("MAKE_CLOSURE %p " UINT_FMT, (void*)unum, n_closed_over);
447 break;
Damienf03001f2013-11-17 13:19:33 +0000448 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200449
450 case MP_BC_MAKE_CLOSURE_DEFARGS: {
451 DECODE_PTR;
452 mp_uint_t n_closed_over = *ip++;
453 printf("MAKE_CLOSURE_DEFARGS %p " UINT_FMT, (void*)unum, n_closed_over);
454 break;
455 }
456
457 case MP_BC_CALL_FUNCTION:
458 DECODE_UINT;
459 printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
460 break;
461
462 case MP_BC_CALL_FUNCTION_VAR_KW:
463 DECODE_UINT;
464 printf("CALL_FUNCTION_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
465 break;
466
467 case MP_BC_CALL_METHOD:
468 DECODE_UINT;
469 printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
470 break;
471
472 case MP_BC_CALL_METHOD_VAR_KW:
473 DECODE_UINT;
474 printf("CALL_METHOD_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
475 break;
476
477 case MP_BC_RETURN_VALUE:
478 printf("RETURN_VALUE");
479 break;
480
481 case MP_BC_RAISE_VARARGS:
482 unum = *ip++;
483 printf("RAISE_VARARGS " UINT_FMT, unum);
484 break;
485
486 case MP_BC_YIELD_VALUE:
487 printf("YIELD_VALUE");
488 break;
489
490 case MP_BC_YIELD_FROM:
491 printf("YIELD_FROM");
492 break;
493
494 case MP_BC_IMPORT_NAME:
495 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000496 printf("IMPORT_NAME '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200497 break;
498
499 case MP_BC_IMPORT_FROM:
500 DECODE_QSTR;
Damien George50912e72015-01-20 11:55:10 +0000501 printf("IMPORT_FROM '%s'", qstr_str(qst));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200502 break;
503
504 case MP_BC_IMPORT_STAR:
505 printf("IMPORT_STAR");
506 break;
507
508 default:
509 if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) {
510 printf("LOAD_CONST_SMALL_INT " INT_FMT, (mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16);
511 } else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) {
512 printf("LOAD_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI);
513 } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) {
514 printf("STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI);
515 } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + 5) {
516 printf("UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI);
517 } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + 35) {
Paul Sokolovsky1ee17852014-12-28 21:41:58 +0200518 mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI;
519 printf("BINARY_OP " UINT_FMT " %s", op, qstr_str(mp_binary_op_method_name[op]));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200520 } else {
521 printf("code %p, byte code 0x%02x not implemented\n", ip, ip[-1]);
522 assert(0);
523 return ip;
524 }
525 break;
526 }
527
528 return ip;
529}
530
531void mp_bytecode_print2(const byte *ip, mp_uint_t len) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200532 mp_showbc_code_start = ip;
Damien George963a5a32015-01-16 17:47:07 +0000533 while (ip < len + mp_showbc_code_start) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200534 printf("%02u ", (uint)(ip - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200535 ip = mp_bytecode_print_str(ip);
Damienf03001f2013-11-17 13:19:33 +0000536 printf("\n");
537 }
538}
Damien Georged3ebe482014-01-07 15:20:33 +0000539
Damien Georgecbd2f742014-01-19 11:48:48 +0000540#endif // MICROPY_DEBUG_PRINTERS