showbc.c 16.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

27
28
29
#include <stdio.h>
#include <assert.h>

30
#include "mpconfig.h"
31
#include "misc.h"
32
#include "qstr.h"
33
34
#include "obj.h"
#include "runtime.h"
35
#include "bc0.h"
36
#include "bc.h"
37

38
#if MICROPY_DEBUG_PRINTERS
39

40
41
42
43
44
45
#define DECODE_UINT { \
    unum = 0; \
    do { \
        unum = (unum << 7) + (*ip & 0x7f); \
    } while ((*ip++ & 0x80) != 0); \
}
46
47
#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
48
49
50
51
52
53
#define DECODE_QSTR { \
    qstr = 0; \
    do { \
        qstr = (qstr << 7) + (*ip & 0x7f); \
    } while ((*ip++ & 0x80) != 0); \
}
54
#define DECODE_PTR do { \
55
56
57
    ip = (byte*)(((mp_uint_t)ip + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1))); /* align ip */ \
    unum = *(mp_uint_t*)ip; \
    ip += sizeof(mp_uint_t); \
58
} while (0)
59

60
void mp_bytecode_print(const void *descr, const byte *ip, mp_uint_t len) {
61
    const byte *ip_start = ip;
Damien George's avatar
Damien George committed
62

63
    // get code info size
64
    const byte *code_info = ip;
65
    mp_uint_t code_info_size = mp_decode_uint(&code_info);
66
67
    ip += code_info_size;

68
69
    qstr block_name = mp_decode_uint(&code_info);
    qstr source_file = mp_decode_uint(&code_info);
70
71
    printf("File %s, code block '%s' (descriptor: %p, bytecode @%p %d bytes)\n",
        qstr_str(source_file), qstr_str(block_name), descr, code_info, len);
72

73
74
    // bytecode prelude: state size and exception stack size; 16 bit uints
    {
75
76
        uint n_state = mp_decode_uint(&ip);
        uint n_exc_stack = mp_decode_uint(&ip);
77
78
79
80
81
        printf("(N_STATE %u)\n", n_state);
        printf("(N_EXC_STACK %u)\n", n_exc_stack);
    }

    // bytecode prelude: initialise closed over variables
Damien George's avatar
Damien George committed
82
83
84
85
86
87
88
89
90
91
92
    {
        uint n_local = *ip++;
        printf("(NUM_LOCAL %u)\n", n_local);
        for (; n_local > 0; n_local--) {
            uint local_num = *ip++;
            printf("(INIT_CELL %u)\n", local_num);
        }
        len -= ip - ip_start;
        ip_start = ip;
    }

93
94
    // print out line number info
    {
95
96
        mp_int_t bc = (code_info + code_info_size) - ip;
        mp_uint_t source_line = 1;
97
        printf("  bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
98
99
100
101
102
103
104
105
106
107
108
109
        for (const byte* ci = code_info + 12; *ci;) {
            if ((ci[0] & 0x80) == 0) {
                // 0b0LLBBBBB encoding
                bc += ci[0] & 0x1f;
                source_line += ci[0] >> 5;
                ci += 1;
            } else {
                // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
                bc += ci[0] & 0xf;
                source_line += ((ci[0] << 4) & 0x700) | ci[1];
                ci += 2;
            }
110
111
112
            printf("  bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
        }
    }
113
    mp_bytecode_print2(ip, len - 0);
114
}
115

116
void mp_bytecode_print2(const byte *ip, mp_uint_t len) {
117
    const byte *ip_start = ip;
118
    mp_uint_t unum;
119
120
121
    qstr qstr;
    while (ip - ip_start < len) {
        printf("%02u ", (uint)(ip - ip_start));
122
        switch (*ip++) {
123
            case MP_BC_LOAD_CONST_FALSE:
124
125
126
                printf("LOAD_CONST_FALSE");
                break;

127
            case MP_BC_LOAD_CONST_NONE:
128
129
130
                printf("LOAD_CONST_NONE");
                break;

131
            case MP_BC_LOAD_CONST_TRUE:
132
133
134
                printf("LOAD_CONST_TRUE");
                break;

Damien George's avatar
Damien George committed
135
136
137
138
            case MP_BC_LOAD_CONST_ELLIPSIS:
                printf("LOAD_CONST_ELLIPSIS");
                break;

139
            case MP_BC_LOAD_CONST_SMALL_INT: {
140
                mp_int_t num = 0;
141
142
143
144
145
146
147
                if ((ip[0] & 0x40) != 0) {
                    // Number is negative
                    num--;
                }
                do {
                    num = (num << 7) | (*ip & 0x7f);
                } while ((*ip++ & 0x80) != 0);
148
                printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
149
                break;
150
            }
151

152
153
154
155
156
            case MP_BC_LOAD_CONST_INT:
                DECODE_QSTR;
                printf("LOAD_CONST_INT %s", qstr_str(qstr));
                break;

157
            case MP_BC_LOAD_CONST_DEC:
158
                DECODE_QSTR;
159
                printf("LOAD_CONST_DEC %s", qstr_str(qstr));
160
161
                break;

162
163
164
165
166
            case MP_BC_LOAD_CONST_BYTES:
                DECODE_QSTR;
                printf("LOAD_CONST_BYTES %s", qstr_str(qstr));
                break;

167
            case MP_BC_LOAD_CONST_STRING:
168
                DECODE_QSTR;
169
                printf("LOAD_CONST_STRING '%s'", qstr_str(qstr));
170
171
                break;

Paul Sokolovsky's avatar
Paul Sokolovsky committed
172
173
174
175
            case MP_BC_LOAD_NULL:
                printf("LOAD_NULL");
                break;

176
            case MP_BC_LOAD_FAST_0:
177
178
179
                printf("LOAD_FAST_0");
                break;

180
            case MP_BC_LOAD_FAST_1:
181
182
183
                printf("LOAD_FAST_1");
                break;

184
            case MP_BC_LOAD_FAST_2:
185
186
187
                printf("LOAD_FAST_2");
                break;

188
            case MP_BC_LOAD_FAST_N:
189
                DECODE_UINT;
190
                printf("LOAD_FAST_N " UINT_FMT, unum);
191
192
                break;

Damien George's avatar
Damien George committed
193
194
195
196
197
            case MP_BC_LOAD_DEREF:
                DECODE_UINT;
                printf("LOAD_DEREF " UINT_FMT, unum);
                break;

198
            case MP_BC_LOAD_NAME:
199
200
201
202
                DECODE_QSTR;
                printf("LOAD_NAME %s", qstr_str(qstr));
                break;

203
            case MP_BC_LOAD_GLOBAL:
204
205
206
207
                DECODE_QSTR;
                printf("LOAD_GLOBAL %s", qstr_str(qstr));
                break;

208
            case MP_BC_LOAD_ATTR:
209
                DECODE_QSTR;
Damien's avatar
Damien committed
210
                printf("LOAD_ATTR %s", qstr_str(qstr));
211
212
                break;

213
            case MP_BC_LOAD_METHOD:
214
215
216
217
                DECODE_QSTR;
                printf("LOAD_METHOD %s", qstr_str(qstr));
                break;

218
            case MP_BC_LOAD_BUILD_CLASS:
Damien's avatar
Damien committed
219
                printf("LOAD_BUILD_CLASS");
220
221
                break;

222
223
224
225
            case MP_BC_LOAD_SUBSCR:
                printf("LOAD_SUBSCR");
                break;

226
            case MP_BC_STORE_FAST_0:
227
228
229
                printf("STORE_FAST_0");
                break;

230
            case MP_BC_STORE_FAST_1:
231
232
233
                printf("STORE_FAST_1");
                break;

234
            case MP_BC_STORE_FAST_2:
235
236
237
                printf("STORE_FAST_2");
                break;

238
            case MP_BC_STORE_FAST_N:
239
                DECODE_UINT;
240
                printf("STORE_FAST_N " UINT_FMT, unum);
241
242
                break;

Damien George's avatar
Damien George committed
243
244
245
246
247
            case MP_BC_STORE_DEREF:
                DECODE_UINT;
                printf("STORE_DEREF " UINT_FMT, unum);
                break;

248
            case MP_BC_STORE_NAME:
249
250
251
252
                DECODE_QSTR;
                printf("STORE_NAME %s", qstr_str(qstr));
                break;

253
            case MP_BC_STORE_GLOBAL:
254
                DECODE_QSTR;
255
                printf("STORE_GLOBAL %s", qstr_str(qstr));
256
257
                break;

258
            case MP_BC_STORE_ATTR:
259
                DECODE_QSTR;
Damien's avatar
Damien committed
260
                printf("STORE_ATTR %s", qstr_str(qstr));
261
262
                break;

263
            case MP_BC_STORE_SUBSCR:
Damien's avatar
Damien committed
264
                printf("STORE_SUBSCR");
265
266
                break;

267
268
269
270
271
272
273
274
275
276
            case MP_BC_DELETE_FAST:
                DECODE_UINT;
                printf("DELETE_FAST " UINT_FMT, unum);
                break;

            case MP_BC_DELETE_DEREF:
                DECODE_UINT;
                printf("DELETE_DEREF " UINT_FMT, unum);
                break;

Damien George's avatar
Damien George committed
277
278
279
280
281
            case MP_BC_DELETE_NAME:
                DECODE_QSTR;
                printf("DELETE_NAME %s", qstr_str(qstr));
                break;

282
            case MP_BC_DUP_TOP:
283
                printf("DUP_TOP");
284
285
                break;

286
            case MP_BC_DUP_TOP_TWO:
Damien's avatar
Damien committed
287
                printf("DUP_TOP_TWO");
288
289
                break;

290
            case MP_BC_POP_TOP:
291
292
293
                printf("POP_TOP");
                break;

294
            case MP_BC_ROT_TWO:
295
                printf("ROT_TWO");
296
297
                break;

298
            case MP_BC_ROT_THREE:
Damien's avatar
Damien committed
299
                printf("ROT_THREE");
300
301
                break;

302
            case MP_BC_JUMP:
303
                DECODE_SLABEL;
304
                printf("JUMP " UINT_FMT, ip + unum - ip_start);
305
306
                break;

307
            case MP_BC_POP_JUMP_IF_TRUE:
308
                DECODE_SLABEL;
309
                printf("POP_JUMP_IF_TRUE " UINT_FMT, ip + unum - ip_start);
310
311
                break;

312
            case MP_BC_POP_JUMP_IF_FALSE:
313
                DECODE_SLABEL;
314
                printf("POP_JUMP_IF_FALSE " UINT_FMT, ip + unum - ip_start);
315
316
                break;

317
            case MP_BC_JUMP_IF_TRUE_OR_POP:
318
                DECODE_SLABEL;
319
                printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, ip + unum - ip_start);
320
321
                break;

322
            case MP_BC_JUMP_IF_FALSE_OR_POP:
323
                DECODE_SLABEL;
324
                printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, ip + unum - ip_start);
325
326
                break;

327
328
329
330
331
332
333
334
335
            case MP_BC_SETUP_WITH:
                DECODE_ULABEL; // loop-like labels are always forward
                printf("SETUP_WITH " UINT_FMT, ip + unum - ip_start);
                break;

            case MP_BC_WITH_CLEANUP:
                printf("WITH_CLEANUP");
                break;

336
337
338
339
            case MP_BC_UNWIND_JUMP:
                DECODE_SLABEL;
                printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - ip_start, *ip);
                ip += 1;
340
341
                break;

342
            case MP_BC_SETUP_EXCEPT:
343
                DECODE_ULABEL; // except labels are always forward
344
                printf("SETUP_EXCEPT " UINT_FMT, ip + unum - ip_start);
345
346
                break;

347
348
349
350
351
            case MP_BC_SETUP_FINALLY:
                DECODE_ULABEL; // except labels are always forward
                printf("SETUP_FINALLY " UINT_FMT, ip + unum - ip_start);
                break;

352
            case MP_BC_END_FINALLY:
353
354
355
356
                // if TOS is an exception, reraises the exception (3 values on TOS)
                // if TOS is an integer, does something else
                // if TOS is None, just pops it and continues
                // else error
357
                printf("END_FINALLY");
358
359
                break;

360
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
361
                printf("GET_ITER");
362
363
                break;

364
            case MP_BC_FOR_ITER:
365
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
366
                printf("FOR_ITER " UINT_FMT, ip + unum - ip_start);
367
368
                break;

369
            case MP_BC_POP_BLOCK:
370
                // pops block and restores the stack
371
                printf("POP_BLOCK");
372
373
                break;

374
            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
378
                break;

379
380
381
382
            case MP_BC_NOT:
                printf("NOT");
                break;

383
            case MP_BC_UNARY_OP:
384
                unum = *ip++;
385
                printf("UNARY_OP " UINT_FMT, unum);
386
387
                break;

388
            case MP_BC_BINARY_OP:
389
                unum = *ip++;
390
                printf("BINARY_OP " UINT_FMT, unum);
391
392
                break;

393
            case MP_BC_BUILD_TUPLE:
394
                DECODE_UINT;
395
                printf("BUILD_TUPLE " UINT_FMT, unum);
396
397
                break;

398
            case MP_BC_BUILD_LIST:
399
                DECODE_UINT;
400
                printf("BUILD_LIST " UINT_FMT, unum);
401
402
                break;

403
            case MP_BC_LIST_APPEND:
404
                DECODE_UINT;
405
                printf("LIST_APPEND " UINT_FMT, unum);
406
407
                break;

408
            case MP_BC_BUILD_MAP:
409
                DECODE_UINT;
410
                printf("BUILD_MAP " UINT_FMT, unum);
411
412
                break;

413
            case MP_BC_STORE_MAP:
414
                printf("STORE_MAP");
415
416
                break;

417
            case MP_BC_MAP_ADD:
418
                DECODE_UINT;
Paul Sokolovsky's avatar
Paul Sokolovsky committed
419
                printf("MAP_ADD " UINT_FMT, unum);
420
421
                break;

422
            case MP_BC_BUILD_SET:
423
                DECODE_UINT;
424
                printf("BUILD_SET " UINT_FMT, unum);
425
426
                break;

427
            case MP_BC_SET_ADD:
428
                DECODE_UINT;
429
                printf("SET_ADD " UINT_FMT, unum);
430
431
                break;

432
#if MICROPY_PY_BUILTINS_SLICE
433
434
435
436
437
438
            case MP_BC_BUILD_SLICE:
                DECODE_UINT;
                printf("BUILD_SLICE " UINT_FMT, unum);
                break;
#endif

439
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
440
                DECODE_UINT;
441
                printf("UNPACK_SEQUENCE " UINT_FMT, unum);
Damien's avatar
Damien committed
442
443
                break;

444
            case MP_BC_MAKE_FUNCTION:
445
                DECODE_PTR;
446
                printf("MAKE_FUNCTION %p", (void*)unum);
447
448
                break;

449
            case MP_BC_MAKE_FUNCTION_DEFARGS:
450
                DECODE_PTR;
451
                printf("MAKE_FUNCTION_DEFARGS %p", (void*)unum);
452
453
                break;

454
            case MP_BC_MAKE_CLOSURE: {
455
                DECODE_PTR;
456
                mp_uint_t n_closed_over = *ip++;
457
                printf("MAKE_CLOSURE %p " UINT_FMT, (void*)unum, n_closed_over);
Damien George's avatar
Damien George committed
458
                break;
459
            }
Damien George's avatar
Damien George committed
460

461
            case MP_BC_MAKE_CLOSURE_DEFARGS: {
462
                DECODE_PTR;
463
                mp_uint_t n_closed_over = *ip++;
464
                printf("MAKE_CLOSURE_DEFARGS %p " UINT_FMT, (void*)unum, n_closed_over);
465
                break;
466
            }
467

468
            case MP_BC_CALL_FUNCTION:
469
                DECODE_UINT;
470
                printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
471
472
                break;

473
474
475
476
477
            case MP_BC_CALL_FUNCTION_VAR_KW:
                DECODE_UINT;
                printf("CALL_FUNCTION_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
                break;

478
            case MP_BC_CALL_METHOD:
479
                DECODE_UINT;
480
                printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
481
482
                break;

483
484
485
486
487
            case MP_BC_CALL_METHOD_VAR_KW:
                DECODE_UINT;
                printf("CALL_METHOD_VAR_KW n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
                break;

488
            case MP_BC_RETURN_VALUE:
489
490
491
                printf("RETURN_VALUE");
                break;

492
493
494
495
496
            case MP_BC_RAISE_VARARGS:
                unum = *ip++;
                printf("RAISE_VARARGS " UINT_FMT, unum);
                break;

497
            case MP_BC_YIELD_VALUE:
498
499
                printf("YIELD_VALUE");
                break;
500

Paul Sokolovsky's avatar
Paul Sokolovsky committed
501
502
503
504
            case MP_BC_YIELD_FROM:
                printf("YIELD_FROM");
                break;

505
506
            case MP_BC_IMPORT_NAME:
                DECODE_QSTR;
507
                printf("IMPORT_NAME '%s'", qstr_str(qstr));
508
509
510
511
                break;

            case MP_BC_IMPORT_FROM:
                DECODE_QSTR;
512
                printf("IMPORT_FROM '%s'", qstr_str(qstr));
513
514
                break;

515
516
517
518
            case MP_BC_IMPORT_STAR:
                printf("IMPORT_STAR");
                break;

519
            default:
520
                printf("code %p, byte code 0x%02x not implemented\n", ip, ip[-1]);
521
522
523
524
525
526
                assert(0);
                return;
        }
        printf("\n");
    }
}
527

528
#endif // MICROPY_DEBUG_PRINTERS