showbc.c 15.9 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
#include "bc0.h"
34

35
#if MICROPY_DEBUG_PRINTERS
36

37
38
39
40
41
42
#define DECODE_UINT { \
    unum = 0; \
    do { \
        unum = (unum << 7) + (*ip & 0x7f); \
    } while ((*ip++ & 0x80) != 0); \
}
43
44
#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)
45
46
47
48
49
50
#define DECODE_QSTR { \
    qstr = 0; \
    do { \
        qstr = (qstr << 7) + (*ip & 0x7f); \
    } while ((*ip++ & 0x80) != 0); \
}
51
52
53
54
55
#define DECODE_PTR do { \
    ip = (byte*)(((machine_uint_t)ip + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1))); /* align ip */ \
    unum = *(machine_uint_t*)ip; \
    ip += sizeof(machine_uint_t); \
} while (0)
56

57
void mp_bytecode_print2(const byte *ip, int len);
58

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

62
63
    // get code info size
    machine_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
64
    const byte *code_info = ip;
65
66
    ip += code_info_size;

67
68
69
70
71
72
73
74
75
76
    // bytecode prelude: state size and exception stack size; 16 bit uints
    {
        uint n_state = ip[0] | (ip[1] << 8);
        uint n_exc_stack = ip[2] | (ip[3] << 8);
        ip += 4;
        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
77
78
79
80
81
82
83
84
85
86
87
    {
        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;
    }

88
89
90
91
    // print out line number info
    {
        qstr source_file = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24);
        qstr block_name = code_info[8] | (code_info[9] << 8) | (code_info[10] << 16) | (code_info[11] << 24);
92
        printf("File %s, block '%s'\n", qstr_str(source_file), qstr_str(block_name));
93
94
95
96
97
98
99
100
101
        machine_int_t bc = (code_info + code_info_size) - ip;
        machine_uint_t source_line = 1;
        printf("  bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
        for (const byte* ci = code_info + 12; *ci; ci++) {
            bc += *ci & 31;
            source_line += *ci >> 5;
            printf("  bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
        }
    }
102
    mp_bytecode_print2(ip, len - 0);
103
}
104

105
void mp_bytecode_print2(const byte *ip, int len) {
106
    const byte *ip_start = ip;
107
108
109
110
111
112
    machine_uint_t unum;
    qstr qstr;
    while (ip - ip_start < len) {
        printf("%02u ", (uint)(ip - ip_start));
        int op = *ip++;
        switch (op) {
113
            case MP_BC_LOAD_CONST_FALSE:
114
115
116
                printf("LOAD_CONST_FALSE");
                break;

117
            case MP_BC_LOAD_CONST_NONE:
118
119
120
                printf("LOAD_CONST_NONE");
                break;

121
            case MP_BC_LOAD_CONST_TRUE:
122
123
124
                printf("LOAD_CONST_TRUE");
                break;

Damien George's avatar
Damien George committed
125
126
127
128
            case MP_BC_LOAD_CONST_ELLIPSIS:
                printf("LOAD_CONST_ELLIPSIS");
                break;

129
            case MP_BC_LOAD_CONST_SMALL_INT: {
130
                machine_int_t num = 0;
131
132
133
134
135
136
137
                if ((ip[0] & 0x40) != 0) {
                    // Number is negative
                    num--;
                }
                do {
                    num = (num << 7) | (*ip & 0x7f);
                } while ((*ip++ & 0x80) != 0);
138
                printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
139
                break;
140
            }
141

142
143
144
145
146
            case MP_BC_LOAD_CONST_INT:
                DECODE_QSTR;
                printf("LOAD_CONST_INT %s", qstr_str(qstr));
                break;

147
            case MP_BC_LOAD_CONST_DEC:
148
                DECODE_QSTR;
149
                printf("LOAD_CONST_DEC %s", qstr_str(qstr));
150
151
                break;

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

157
            case MP_BC_LOAD_CONST_STRING:
158
                DECODE_QSTR;
159
                printf("LOAD_CONST_STRING '%s'", qstr_str(qstr));
160
161
                break;

Paul Sokolovsky's avatar
Paul Sokolovsky committed
162
163
164
165
            case MP_BC_LOAD_NULL:
                printf("LOAD_NULL");
                break;

166
            case MP_BC_LOAD_FAST_0:
167
168
169
                printf("LOAD_FAST_0");
                break;

170
            case MP_BC_LOAD_FAST_1:
171
172
173
                printf("LOAD_FAST_1");
                break;

174
            case MP_BC_LOAD_FAST_2:
175
176
177
                printf("LOAD_FAST_2");
                break;

178
            case MP_BC_LOAD_FAST_N:
179
                DECODE_UINT;
180
                printf("LOAD_FAST_N " UINT_FMT, unum);
181
182
                break;

Damien George's avatar
Damien George committed
183
184
185
186
187
            case MP_BC_LOAD_DEREF:
                DECODE_UINT;
                printf("LOAD_DEREF " UINT_FMT, unum);
                break;

188
            case MP_BC_LOAD_NAME:
189
190
191
192
                DECODE_QSTR;
                printf("LOAD_NAME %s", qstr_str(qstr));
                break;

193
            case MP_BC_LOAD_GLOBAL:
194
195
196
197
                DECODE_QSTR;
                printf("LOAD_GLOBAL %s", qstr_str(qstr));
                break;

198
            case MP_BC_LOAD_ATTR:
199
                DECODE_QSTR;
Damien's avatar
Damien committed
200
                printf("LOAD_ATTR %s", qstr_str(qstr));
201
202
                break;

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

208
            case MP_BC_LOAD_BUILD_CLASS:
Damien's avatar
Damien committed
209
                printf("LOAD_BUILD_CLASS");
210
211
                break;

212
213
214
215
            case MP_BC_LOAD_SUBSCR:
                printf("LOAD_SUBSCR");
                break;

216
            case MP_BC_STORE_FAST_0:
217
218
219
                printf("STORE_FAST_0");
                break;

220
            case MP_BC_STORE_FAST_1:
221
222
223
                printf("STORE_FAST_1");
                break;

224
            case MP_BC_STORE_FAST_2:
225
226
227
                printf("STORE_FAST_2");
                break;

228
            case MP_BC_STORE_FAST_N:
229
                DECODE_UINT;
230
                printf("STORE_FAST_N " UINT_FMT, unum);
231
232
                break;

Damien George's avatar
Damien George committed
233
234
235
236
237
            case MP_BC_STORE_DEREF:
                DECODE_UINT;
                printf("STORE_DEREF " UINT_FMT, unum);
                break;

238
            case MP_BC_STORE_NAME:
239
240
241
242
                DECODE_QSTR;
                printf("STORE_NAME %s", qstr_str(qstr));
                break;

243
            case MP_BC_STORE_GLOBAL:
244
                DECODE_QSTR;
245
                printf("STORE_GLOBAL %s", qstr_str(qstr));
246
247
                break;

248
            case MP_BC_STORE_ATTR:
249
                DECODE_QSTR;
Damien's avatar
Damien committed
250
                printf("STORE_ATTR %s", qstr_str(qstr));
251
252
                break;

253
            case MP_BC_STORE_SUBSCR:
Damien's avatar
Damien committed
254
                printf("STORE_SUBSCR");
255
256
                break;

257
258
259
260
261
262
263
264
265
266
            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
267
268
269
270
271
            case MP_BC_DELETE_NAME:
                DECODE_QSTR;
                printf("DELETE_NAME %s", qstr_str(qstr));
                break;

272
            case MP_BC_DUP_TOP:
273
                printf("DUP_TOP");
274
275
                break;

276
            case MP_BC_DUP_TOP_TWO:
Damien's avatar
Damien committed
277
                printf("DUP_TOP_TWO");
278
279
                break;

280
            case MP_BC_POP_TOP:
281
282
283
                printf("POP_TOP");
                break;

284
            case MP_BC_ROT_TWO:
285
                printf("ROT_TWO");
286
287
                break;

288
            case MP_BC_ROT_THREE:
Damien's avatar
Damien committed
289
                printf("ROT_THREE");
290
291
                break;

292
            case MP_BC_JUMP:
293
                DECODE_SLABEL;
294
                printf("JUMP " UINT_FMT, ip + unum - ip_start);
295
296
                break;

297
            case MP_BC_POP_JUMP_IF_TRUE:
298
                DECODE_SLABEL;
299
                printf("POP_JUMP_IF_TRUE " UINT_FMT, ip + unum - ip_start);
300
301
                break;

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

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

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

317
318
319
320
321
322
323
324
325
            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;

326
327
328
329
            case MP_BC_UNWIND_JUMP:
                DECODE_SLABEL;
                printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - ip_start, *ip);
                ip += 1;
330
331
                break;

332
            case MP_BC_SETUP_EXCEPT:
333
                DECODE_ULABEL; // except labels are always forward
334
                printf("SETUP_EXCEPT " UINT_FMT, ip + unum - ip_start);
335
336
                break;

337
338
339
340
341
            case MP_BC_SETUP_FINALLY:
                DECODE_ULABEL; // except labels are always forward
                printf("SETUP_FINALLY " UINT_FMT, ip + unum - ip_start);
                break;

342
            case MP_BC_END_FINALLY:
343
344
345
346
                // 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
347
                printf("END_FINALLY");
348
349
                break;

350
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
351
                printf("GET_ITER");
352
353
                break;

354
            case MP_BC_FOR_ITER:
355
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
356
                printf("FOR_ITER " UINT_FMT, ip + unum - ip_start);
357
358
                break;

359
            case MP_BC_POP_BLOCK:
360
                // pops block and restores the stack
361
                printf("POP_BLOCK");
362
363
                break;

364
            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
368
                break;

369
370
371
372
            case MP_BC_NOT:
                printf("NOT");
                break;

373
            case MP_BC_UNARY_OP:
374
                unum = *ip++;
375
                printf("UNARY_OP " UINT_FMT, unum);
376
377
                break;

378
            case MP_BC_BINARY_OP:
379
                unum = *ip++;
380
                printf("BINARY_OP " UINT_FMT, unum);
381
382
                break;

383
            case MP_BC_BUILD_TUPLE:
384
                DECODE_UINT;
385
                printf("BUILD_TUPLE " UINT_FMT, unum);
386
387
                break;

388
            case MP_BC_BUILD_LIST:
389
                DECODE_UINT;
390
                printf("BUILD_LIST " UINT_FMT, unum);
391
392
                break;

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

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

403
            case MP_BC_STORE_MAP:
404
                printf("STORE_MAP");
405
406
                break;

407
            case MP_BC_MAP_ADD:
408
                DECODE_UINT;
Paul Sokolovsky's avatar
Paul Sokolovsky committed
409
                printf("MAP_ADD " UINT_FMT, unum);
410
411
                break;

412
            case MP_BC_BUILD_SET:
413
                DECODE_UINT;
414
                printf("BUILD_SET " UINT_FMT, unum);
415
416
                break;

417
            case MP_BC_SET_ADD:
418
                DECODE_UINT;
419
                printf("SET_ADD " UINT_FMT, unum);
420
421
                break;

422
#if MICROPY_PY_SLICE
423
424
425
426
427
428
            case MP_BC_BUILD_SLICE:
                DECODE_UINT;
                printf("BUILD_SLICE " UINT_FMT, unum);
                break;
#endif

429
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
430
                DECODE_UINT;
431
                printf("UNPACK_SEQUENCE " UINT_FMT, unum);
Damien's avatar
Damien committed
432
433
                break;

434
            case MP_BC_MAKE_FUNCTION:
435
                DECODE_PTR;
436
                printf("MAKE_FUNCTION " UINT_FMT, unum);
437
438
                break;

439
            case MP_BC_MAKE_FUNCTION_DEFARGS:
440
                DECODE_PTR;
441
442
443
                printf("MAKE_FUNCTION_DEFARGS " UINT_FMT, unum);
                break;

444
            case MP_BC_MAKE_CLOSURE: {
445
                DECODE_PTR;
446
447
                machine_uint_t n_closed_over = *ip++;
                printf("MAKE_CLOSURE " UINT_FMT " " UINT_FMT, unum, n_closed_over);
Damien George's avatar
Damien George committed
448
                break;
449
            }
Damien George's avatar
Damien George committed
450

451
            case MP_BC_MAKE_CLOSURE_DEFARGS: {
452
                DECODE_PTR;
453
454
                machine_uint_t n_closed_over = *ip++;
                printf("MAKE_CLOSURE_DEFARGS " UINT_FMT " " UINT_FMT, unum, n_closed_over);
455
                break;
456
            }
457

458
            case MP_BC_CALL_FUNCTION:
459
                DECODE_UINT;
460
                printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
461
462
                break;

463
464
465
466
467
            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;

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

473
474
475
476
477
            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;

478
            case MP_BC_RETURN_VALUE:
479
480
481
                printf("RETURN_VALUE");
                break;

482
483
484
485
486
            case MP_BC_RAISE_VARARGS:
                unum = *ip++;
                printf("RAISE_VARARGS " UINT_FMT, unum);
                break;

487
            case MP_BC_YIELD_VALUE:
488
489
                printf("YIELD_VALUE");
                break;
490

Paul Sokolovsky's avatar
Paul Sokolovsky committed
491
492
493
494
            case MP_BC_YIELD_FROM:
                printf("YIELD_FROM");
                break;

495
496
            case MP_BC_IMPORT_NAME:
                DECODE_QSTR;
497
                printf("IMPORT_NAME '%s'", qstr_str(qstr));
498
499
500
501
                break;

            case MP_BC_IMPORT_FROM:
                DECODE_QSTR;
502
                printf("IMPORT_FROM '%s'", qstr_str(qstr));
503
504
                break;

505
506
507
508
            case MP_BC_IMPORT_STAR:
                printf("IMPORT_STAR");
                break;

509
510
511
512
513
514
515
516
            default:
                printf("code %p, byte code 0x%02x not implemented\n", ip, op);
                assert(0);
                return;
        }
        printf("\n");
    }
}
517

518
#endif // MICROPY_DEBUG_PRINTERS