blob: e2151c5a5c73ba93ea20ec307a9e9f924e0e3150 [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:");
84 for (int i = 0; i < n_total_args; i++) {
85 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
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200186 case MP_BC_LOAD_NULL:
187 printf("LOAD_NULL");
188 break;
Paul Sokolovsky00a9d132014-04-12 00:32:38 +0300189
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200190 case MP_BC_LOAD_FAST_N:
191 DECODE_UINT;
192 printf("LOAD_FAST_N " UINT_FMT, unum);
193 break;
Damienf03001f2013-11-17 13:19:33 +0000194
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200195 case MP_BC_LOAD_DEREF:
196 DECODE_UINT;
197 printf("LOAD_DEREF " UINT_FMT, unum);
198 break;
Damien George6baf76e2013-12-30 22:32:17 +0000199
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200200 case MP_BC_LOAD_NAME:
201 DECODE_QSTR;
202 printf("LOAD_NAME %s", qstr_str(qstr));
203 break;
Damienf03001f2013-11-17 13:19:33 +0000204
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200205 case MP_BC_LOAD_GLOBAL:
206 DECODE_QSTR;
207 printf("LOAD_GLOBAL %s", qstr_str(qstr));
208 break;
Damienf03001f2013-11-17 13:19:33 +0000209
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200210 case MP_BC_LOAD_ATTR:
211 DECODE_QSTR;
212 printf("LOAD_ATTR %s", qstr_str(qstr));
213 break;
Damienf03001f2013-11-17 13:19:33 +0000214
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200215 case MP_BC_LOAD_METHOD:
216 DECODE_QSTR;
217 printf("LOAD_METHOD %s", qstr_str(qstr));
218 break;
Damienf03001f2013-11-17 13:19:33 +0000219
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200220 case MP_BC_LOAD_BUILD_CLASS:
221 printf("LOAD_BUILD_CLASS");
222 break;
Damienf03001f2013-11-17 13:19:33 +0000223
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200224 case MP_BC_LOAD_SUBSCR:
225 printf("LOAD_SUBSCR");
226 break;
Damien George729f7b42014-04-17 22:10:53 +0100227
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200228 case MP_BC_STORE_FAST_N:
229 DECODE_UINT;
230 printf("STORE_FAST_N " UINT_FMT, unum);
231 break;
Damienf03001f2013-11-17 13:19:33 +0000232
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200233 case MP_BC_STORE_DEREF:
234 DECODE_UINT;
235 printf("STORE_DEREF " UINT_FMT, unum);
236 break;
Damien George6baf76e2013-12-30 22:32:17 +0000237
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200238 case MP_BC_STORE_NAME:
239 DECODE_QSTR;
240 printf("STORE_NAME %s", qstr_str(qstr));
241 break;
Damienf03001f2013-11-17 13:19:33 +0000242
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200243 case MP_BC_STORE_GLOBAL:
244 DECODE_QSTR;
245 printf("STORE_GLOBAL %s", qstr_str(qstr));
246 break;
Damienf03001f2013-11-17 13:19:33 +0000247
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200248 case MP_BC_STORE_ATTR:
249 DECODE_QSTR;
250 printf("STORE_ATTR %s", qstr_str(qstr));
251 break;
Damienf03001f2013-11-17 13:19:33 +0000252
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200253 case MP_BC_STORE_SUBSCR:
254 printf("STORE_SUBSCR");
255 break;
Damienf03001f2013-11-17 13:19:33 +0000256
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200257 case MP_BC_DELETE_FAST:
258 DECODE_UINT;
259 printf("DELETE_FAST " UINT_FMT, unum);
260 break;
Damien George2bf7c092014-04-09 15:26:46 +0100261
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200262 case MP_BC_DELETE_DEREF:
263 DECODE_UINT;
264 printf("DELETE_DEREF " 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_NAME:
268 DECODE_QSTR;
269 printf("DELETE_NAME %s", qstr_str(qstr));
270 break;
Damien Georgeddaf6c12014-02-06 20:31:32 +0000271
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200272 case MP_BC_DUP_TOP:
273 printf("DUP_TOP");
274 break;
Damienf03001f2013-11-17 13:19:33 +0000275
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200276 case MP_BC_DUP_TOP_TWO:
277 printf("DUP_TOP_TWO");
278 break;
Damienf03001f2013-11-17 13:19:33 +0000279
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200280 case MP_BC_POP_TOP:
281 printf("POP_TOP");
282 break;
Damienf03001f2013-11-17 13:19:33 +0000283
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200284 case MP_BC_ROT_TWO:
285 printf("ROT_TWO");
286 break;
Damienf03001f2013-11-17 13:19:33 +0000287
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200288 case MP_BC_ROT_THREE:
289 printf("ROT_THREE");
290 break;
Damienf03001f2013-11-17 13:19:33 +0000291
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200292 case MP_BC_JUMP:
293 DECODE_SLABEL;
Paul Sokolovskydf103462014-12-28 21:34:45 +0200294 printf("JUMP " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200295 break;
Damienf03001f2013-11-17 13:19:33 +0000296
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200297 case MP_BC_POP_JUMP_IF_TRUE:
298 DECODE_SLABEL;
Paul Sokolovskydf103462014-12-28 21:34:45 +0200299 printf("POP_JUMP_IF_TRUE " INT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200300 break;
Damienf03001f2013-11-17 13:19:33 +0000301
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200302 case MP_BC_POP_JUMP_IF_FALSE:
303 DECODE_SLABEL;
Paul Sokolovskydf103462014-12-28 21:34:45 +0200304 printf("POP_JUMP_IF_FALSE " INT_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_JUMP_IF_TRUE_OR_POP:
308 DECODE_SLABEL;
Paul Sokolovskydf103462014-12-28 21:34:45 +0200309 printf("JUMP_IF_TRUE_OR_POP " INT_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_JUMP_IF_FALSE_OR_POP:
313 DECODE_SLABEL;
Paul Sokolovskydf103462014-12-28 21:34:45 +0200314 printf("JUMP_IF_FALSE_OR_POP " INT_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_SETUP_WITH:
318 DECODE_ULABEL; // loop-like labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200319 printf("SETUP_WITH " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200320 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200321
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200322 case MP_BC_WITH_CLEANUP:
323 printf("WITH_CLEANUP");
324 break;
Paul Sokolovsky182c31a2014-03-27 12:29:34 +0200325
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200326 case MP_BC_UNWIND_JUMP:
327 DECODE_SLABEL;
Paul Sokolovskydf103462014-12-28 21:34:45 +0200328 printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - mp_showbc_code_start, *ip);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200329 ip += 1;
330 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200331
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200332 case MP_BC_SETUP_EXCEPT:
333 DECODE_ULABEL; // except labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200334 printf("SETUP_EXCEPT " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200335 break;
Damienf03001f2013-11-17 13:19:33 +0000336
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200337 case MP_BC_SETUP_FINALLY:
338 DECODE_ULABEL; // except labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200339 printf("SETUP_FINALLY " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200340 break;
Paul Sokolovsky7ee8e462014-01-31 19:33:31 +0200341
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200342 case MP_BC_END_FINALLY:
343 // if TOS is an exception, reraises the exception (3 values on TOS)
344 // if TOS is an integer, does something else
345 // if TOS is None, just pops it and continues
346 // else error
347 printf("END_FINALLY");
348 break;
Damienf03001f2013-11-17 13:19:33 +0000349
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200350 case MP_BC_GET_ITER:
351 printf("GET_ITER");
352 break;
Damienf03001f2013-11-17 13:19:33 +0000353
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200354 case MP_BC_FOR_ITER:
355 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Paul Sokolovskydf103462014-12-28 21:34:45 +0200356 printf("FOR_ITER " UINT_FMT, ip + unum - mp_showbc_code_start);
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200357 break;
Damienf03001f2013-11-17 13:19:33 +0000358
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200359 case MP_BC_POP_BLOCK:
360 // pops block and restores the stack
361 printf("POP_BLOCK");
362 break;
Damienf03001f2013-11-17 13:19:33 +0000363
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200364 case MP_BC_POP_EXCEPT:
365 // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
366 printf("POP_EXCEPT");
367 break;
Damienf03001f2013-11-17 13:19:33 +0000368
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200369 case MP_BC_NOT:
370 printf("NOT");
371 break;
Damien George9aa2a522014-02-01 23:04:09 +0000372
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200373 case MP_BC_BUILD_TUPLE:
374 DECODE_UINT;
375 printf("BUILD_TUPLE " UINT_FMT, unum);
376 break;
Damienf03001f2013-11-17 13:19:33 +0000377
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200378 case MP_BC_BUILD_LIST:
379 DECODE_UINT;
380 printf("BUILD_LIST " UINT_FMT, unum);
381 break;
Damienf03001f2013-11-17 13:19:33 +0000382
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200383 case MP_BC_LIST_APPEND:
384 DECODE_UINT;
385 printf("LIST_APPEND " UINT_FMT, unum);
386 break;
Damienf03001f2013-11-17 13:19:33 +0000387
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200388 case MP_BC_BUILD_MAP:
389 DECODE_UINT;
390 printf("BUILD_MAP " UINT_FMT, unum);
391 break;
Damienf03001f2013-11-17 13:19:33 +0000392
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200393 case MP_BC_STORE_MAP:
394 printf("STORE_MAP");
395 break;
Damienf03001f2013-11-17 13:19:33 +0000396
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200397 case MP_BC_MAP_ADD:
398 DECODE_UINT;
399 printf("MAP_ADD " UINT_FMT, unum);
400 break;
Damienf03001f2013-11-17 13:19:33 +0000401
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200402 case MP_BC_BUILD_SET:
403 DECODE_UINT;
404 printf("BUILD_SET " UINT_FMT, unum);
405 break;
Damienf03001f2013-11-17 13:19:33 +0000406
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200407 case MP_BC_SET_ADD:
408 DECODE_UINT;
409 printf("SET_ADD " UINT_FMT, unum);
410 break;
Damienf03001f2013-11-17 13:19:33 +0000411
Damien Georgefb510b32014-06-01 13:32:54 +0100412#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200413 case MP_BC_BUILD_SLICE:
414 DECODE_UINT;
415 printf("BUILD_SLICE " UINT_FMT, unum);
416 break;
Damien George20006db2014-01-18 14:10:48 +0000417#endif
418
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200419 case MP_BC_UNPACK_SEQUENCE:
420 DECODE_UINT;
421 printf("UNPACK_SEQUENCE " UINT_FMT, unum);
422 break;
Damienff099f32013-11-26 15:14:50 +0000423
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200424 case MP_BC_MAKE_FUNCTION:
425 DECODE_PTR;
426 printf("MAKE_FUNCTION %p", (void*)unum);
427 break;
Damienf03001f2013-11-17 13:19:33 +0000428
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200429 case MP_BC_MAKE_FUNCTION_DEFARGS:
430 DECODE_PTR;
431 printf("MAKE_FUNCTION_DEFARGS %p", (void*)unum);
432 break;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200433
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200434 case MP_BC_MAKE_CLOSURE: {
435 DECODE_PTR;
436 mp_uint_t n_closed_over = *ip++;
437 printf("MAKE_CLOSURE %p " UINT_FMT, (void*)unum, n_closed_over);
438 break;
Damienf03001f2013-11-17 13:19:33 +0000439 }
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200440
441 case MP_BC_MAKE_CLOSURE_DEFARGS: {
442 DECODE_PTR;
443 mp_uint_t n_closed_over = *ip++;
444 printf("MAKE_CLOSURE_DEFARGS %p " UINT_FMT, (void*)unum, n_closed_over);
445 break;
446 }
447
448 case MP_BC_CALL_FUNCTION:
449 DECODE_UINT;
450 printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
451 break;
452
453 case MP_BC_CALL_FUNCTION_VAR_KW:
454 DECODE_UINT;
455 printf("CALL_FUNCTION_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
456 break;
457
458 case MP_BC_CALL_METHOD:
459 DECODE_UINT;
460 printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
461 break;
462
463 case MP_BC_CALL_METHOD_VAR_KW:
464 DECODE_UINT;
465 printf("CALL_METHOD_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
466 break;
467
468 case MP_BC_RETURN_VALUE:
469 printf("RETURN_VALUE");
470 break;
471
472 case MP_BC_RAISE_VARARGS:
473 unum = *ip++;
474 printf("RAISE_VARARGS " UINT_FMT, unum);
475 break;
476
477 case MP_BC_YIELD_VALUE:
478 printf("YIELD_VALUE");
479 break;
480
481 case MP_BC_YIELD_FROM:
482 printf("YIELD_FROM");
483 break;
484
485 case MP_BC_IMPORT_NAME:
486 DECODE_QSTR;
487 printf("IMPORT_NAME '%s'", qstr_str(qstr));
488 break;
489
490 case MP_BC_IMPORT_FROM:
491 DECODE_QSTR;
492 printf("IMPORT_FROM '%s'", qstr_str(qstr));
493 break;
494
495 case MP_BC_IMPORT_STAR:
496 printf("IMPORT_STAR");
497 break;
498
499 default:
500 if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) {
501 printf("LOAD_CONST_SMALL_INT " INT_FMT, (mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16);
502 } else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) {
503 printf("LOAD_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI);
504 } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) {
505 printf("STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI);
506 } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + 5) {
507 printf("UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI);
508 } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + 35) {
Paul Sokolovsky1ee17852014-12-28 21:41:58 +0200509 mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI;
510 printf("BINARY_OP " UINT_FMT " %s", op, qstr_str(mp_binary_op_method_name[op]));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200511 } else {
512 printf("code %p, byte code 0x%02x not implemented\n", ip, ip[-1]);
513 assert(0);
514 return ip;
515 }
516 break;
517 }
518
519 return ip;
520}
521
522void mp_bytecode_print2(const byte *ip, mp_uint_t len) {
Paul Sokolovskydf103462014-12-28 21:34:45 +0200523 mp_showbc_code_start = ip;
524 while (ip - mp_showbc_code_start < len) {
525 printf("%02u ", (uint)(ip - mp_showbc_code_start));
Paul Sokolovsky343266e2014-12-27 05:00:08 +0200526 ip = mp_bytecode_print_str(ip);
Damienf03001f2013-11-17 13:19:33 +0000527 printf("\n");
528 }
529}
Damien Georged3ebe482014-01-07 15:20:33 +0000530
Damien Georgecbd2f742014-01-19 11:48:48 +0000531#endif // MICROPY_DEBUG_PRINTERS