showbc.c 14.7 KB
Newer Older
1
2
3
4
#include <stdio.h>
#include <assert.h>

#include "misc.h"
5
#include "mpconfig.h"
6
#include "qstr.h"
7
#include "bc0.h"
8

9
#if MICROPY_DEBUG_PRINTERS
10

11
12
13
14
15
16
#define DECODE_UINT { \
    unum = 0; \
    do { \
        unum = (unum << 7) + (*ip & 0x7f); \
    } while ((*ip++ & 0x80) != 0); \
}
17
18
#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)
19
20
21
22
23
24
#define DECODE_QSTR { \
    qstr = 0; \
    do { \
        qstr = (qstr << 7) + (*ip & 0x7f); \
    } while ((*ip++ & 0x80) != 0); \
}
25
26
27
28
29
#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)
30

31
void mp_byte_code_print(const byte *ip, int len) {
32
    const byte *ip_start = ip;
Damien George's avatar
Damien George committed
33

34
35
    // get code info size
    machine_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
36
    const byte *code_info = ip;
37
38
    ip += code_info_size;

39
40
41
42
43
44
45
46
47
48
    // 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
49
50
51
52
53
54
55
56
57
58
59
    {
        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;
    }

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    // 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);
        printf("File %s, block %s\n", qstr_str(source_file), qstr_str(block_name));
        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);
        }
    }

75
76
77
78
79
80
    machine_uint_t unum;
    qstr qstr;
    while (ip - ip_start < len) {
        printf("%02u ", (uint)(ip - ip_start));
        int op = *ip++;
        switch (op) {
81
            case MP_BC_LOAD_CONST_FALSE:
82
83
84
                printf("LOAD_CONST_FALSE");
                break;

85
            case MP_BC_LOAD_CONST_NONE:
86
87
88
                printf("LOAD_CONST_NONE");
                break;

89
            case MP_BC_LOAD_CONST_TRUE:
90
91
92
                printf("LOAD_CONST_TRUE");
                break;

Damien George's avatar
Damien George committed
93
94
95
96
            case MP_BC_LOAD_CONST_ELLIPSIS:
                printf("LOAD_CONST_ELLIPSIS");
                break;

97
            case MP_BC_LOAD_CONST_SMALL_INT: {
98
                machine_int_t num = 0;
99
100
101
102
103
104
105
                if ((ip[0] & 0x40) != 0) {
                    // Number is negative
                    num--;
                }
                do {
                    num = (num << 7) | (*ip & 0x7f);
                } while ((*ip++ & 0x80) != 0);
106
                printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
107
                break;
108
            }
109

110
111
112
113
114
            case MP_BC_LOAD_CONST_INT:
                DECODE_QSTR;
                printf("LOAD_CONST_INT %s", qstr_str(qstr));
                break;

115
            case MP_BC_LOAD_CONST_DEC:
116
                DECODE_QSTR;
117
                printf("LOAD_CONST_DEC %s", qstr_str(qstr));
118
119
                break;

120
            case MP_BC_LOAD_CONST_ID:
121
                DECODE_QSTR;
122
                printf("LOAD_CONST_ID '%s'", qstr_str(qstr));
123
124
                break;

125
126
127
128
129
            case MP_BC_LOAD_CONST_BYTES:
                DECODE_QSTR;
                printf("LOAD_CONST_BYTES %s", qstr_str(qstr));
                break;

130
            case MP_BC_LOAD_CONST_STRING:
131
                DECODE_QSTR;
132
                printf("LOAD_CONST_STRING '%s'", qstr_str(qstr));
133
134
                break;

Paul Sokolovsky's avatar
Paul Sokolovsky committed
135
136
137
138
            case MP_BC_LOAD_NULL:
                printf("LOAD_NULL");
                break;

139
            case MP_BC_LOAD_FAST_0:
140
141
142
                printf("LOAD_FAST_0");
                break;

143
            case MP_BC_LOAD_FAST_1:
144
145
146
                printf("LOAD_FAST_1");
                break;

147
            case MP_BC_LOAD_FAST_2:
148
149
150
                printf("LOAD_FAST_2");
                break;

151
            case MP_BC_LOAD_FAST_N:
152
                DECODE_UINT;
153
                printf("LOAD_FAST_N " UINT_FMT, unum);
154
155
                break;

Damien George's avatar
Damien George committed
156
157
158
159
160
            case MP_BC_LOAD_DEREF:
                DECODE_UINT;
                printf("LOAD_DEREF " UINT_FMT, unum);
                break;

161
            case MP_BC_LOAD_NAME:
162
163
164
165
                DECODE_QSTR;
                printf("LOAD_NAME %s", qstr_str(qstr));
                break;

166
            case MP_BC_LOAD_GLOBAL:
167
168
169
170
                DECODE_QSTR;
                printf("LOAD_GLOBAL %s", qstr_str(qstr));
                break;

171
            case MP_BC_LOAD_ATTR:
172
                DECODE_QSTR;
Damien's avatar
Damien committed
173
                printf("LOAD_ATTR %s", qstr_str(qstr));
174
175
                break;

176
            case MP_BC_LOAD_METHOD:
177
178
179
180
                DECODE_QSTR;
                printf("LOAD_METHOD %s", qstr_str(qstr));
                break;

181
            case MP_BC_LOAD_BUILD_CLASS:
Damien's avatar
Damien committed
182
                printf("LOAD_BUILD_CLASS");
183
184
                break;

185
            case MP_BC_STORE_FAST_0:
186
187
188
                printf("STORE_FAST_0");
                break;

189
            case MP_BC_STORE_FAST_1:
190
191
192
                printf("STORE_FAST_1");
                break;

193
            case MP_BC_STORE_FAST_2:
194
195
196
                printf("STORE_FAST_2");
                break;

197
            case MP_BC_STORE_FAST_N:
198
                DECODE_UINT;
199
                printf("STORE_FAST_N " UINT_FMT, unum);
200
201
                break;

Damien George's avatar
Damien George committed
202
203
204
205
206
            case MP_BC_STORE_DEREF:
                DECODE_UINT;
                printf("STORE_DEREF " UINT_FMT, unum);
                break;

207
            case MP_BC_STORE_NAME:
208
209
210
211
                DECODE_QSTR;
                printf("STORE_NAME %s", qstr_str(qstr));
                break;

212
            case MP_BC_STORE_GLOBAL:
213
                DECODE_QSTR;
214
                printf("STORE_GLOBAL %s", qstr_str(qstr));
215
216
                break;

217
            case MP_BC_STORE_ATTR:
218
                DECODE_QSTR;
Damien's avatar
Damien committed
219
                printf("STORE_ATTR %s", qstr_str(qstr));
220
221
                break;

222
            case MP_BC_STORE_SUBSCR:
Damien's avatar
Damien committed
223
                printf("STORE_SUBSCR");
224
225
                break;

226
227
228
229
230
231
232
233
234
235
            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
236
237
238
239
240
            case MP_BC_DELETE_NAME:
                DECODE_QSTR;
                printf("DELETE_NAME %s", qstr_str(qstr));
                break;

241
            case MP_BC_DUP_TOP:
242
                printf("DUP_TOP");
243
244
                break;

245
            case MP_BC_DUP_TOP_TWO:
Damien's avatar
Damien committed
246
                printf("DUP_TOP_TWO");
247
248
                break;

249
            case MP_BC_POP_TOP:
250
251
252
                printf("POP_TOP");
                break;

253
            case MP_BC_ROT_TWO:
254
                printf("ROT_TWO");
255
256
                break;

257
            case MP_BC_ROT_THREE:
Damien's avatar
Damien committed
258
                printf("ROT_THREE");
259
260
                break;

261
            case MP_BC_JUMP:
262
                DECODE_SLABEL;
263
                printf("JUMP " UINT_FMT, ip + unum - ip_start);
264
265
                break;

266
            case MP_BC_POP_JUMP_IF_TRUE:
267
                DECODE_SLABEL;
268
                printf("POP_JUMP_IF_TRUE " UINT_FMT, ip + unum - ip_start);
269
270
                break;

271
            case MP_BC_POP_JUMP_IF_FALSE:
272
                DECODE_SLABEL;
273
                printf("POP_JUMP_IF_FALSE " UINT_FMT, ip + unum - ip_start);
274
275
                break;

276
            case MP_BC_JUMP_IF_TRUE_OR_POP:
277
                DECODE_SLABEL;
278
                printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, ip + unum - ip_start);
279
280
                break;

281
            case MP_BC_JUMP_IF_FALSE_OR_POP:
282
                DECODE_SLABEL;
283
                printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, ip + unum - ip_start);
284
285
                break;

286
287
288
289
290
            case MP_BC_SETUP_LOOP:
                DECODE_ULABEL; // loop labels are always forward
                printf("SETUP_LOOP " UINT_FMT, ip + unum - ip_start);
                break;

291
292
293
294
295
296
297
298
299
            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;

300
301
302
303
            case MP_BC_UNWIND_JUMP:
                DECODE_SLABEL;
                printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - ip_start, *ip);
                ip += 1;
304
305
                break;

306
            case MP_BC_SETUP_EXCEPT:
307
                DECODE_ULABEL; // except labels are always forward
308
                printf("SETUP_EXCEPT " UINT_FMT, ip + unum - ip_start);
309
310
                break;

311
312
313
314
315
            case MP_BC_SETUP_FINALLY:
                DECODE_ULABEL; // except labels are always forward
                printf("SETUP_FINALLY " UINT_FMT, ip + unum - ip_start);
                break;

316
            case MP_BC_END_FINALLY:
317
318
319
320
                // 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
321
                printf("END_FINALLY");
322
323
                break;

324
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
325
                printf("GET_ITER");
326
327
                break;

328
            case MP_BC_FOR_ITER:
329
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
330
                printf("FOR_ITER " UINT_FMT, ip + unum - ip_start);
331
332
                break;

333
            case MP_BC_POP_BLOCK:
334
                // pops block and restores the stack
335
                printf("POP_BLOCK");
336
337
                break;

338
            case MP_BC_POP_EXCEPT:
339
                // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
340
                printf("POP_EXCEPT");
341
342
                break;

343
344
345
346
            case MP_BC_NOT:
                printf("NOT");
                break;

347
            case MP_BC_UNARY_OP:
348
                unum = *ip++;
349
                printf("UNARY_OP " UINT_FMT, unum);
350
351
                break;

352
            case MP_BC_BINARY_OP:
353
                unum = *ip++;
354
                printf("BINARY_OP " UINT_FMT, unum);
355
356
                break;

357
            case MP_BC_BUILD_TUPLE:
358
                DECODE_UINT;
359
                printf("BUILD_TUPLE " UINT_FMT, unum);
360
361
                break;

362
            case MP_BC_BUILD_LIST:
363
                DECODE_UINT;
364
                printf("BUILD_LIST " UINT_FMT, unum);
365
366
                break;

367
            case MP_BC_LIST_APPEND:
368
                DECODE_UINT;
369
                printf("LIST_APPEND " UINT_FMT, unum);
370
371
                break;

372
            case MP_BC_BUILD_MAP:
373
                DECODE_UINT;
374
                printf("BUILD_MAP " UINT_FMT, unum);
375
376
                break;

377
            case MP_BC_STORE_MAP:
378
                printf("STORE_MAP");
379
380
                break;

381
                /*
382
            case MP_BC_MAP_ADD:
383
384
385
386
387
                DECODE_UINT;
                // I think it's guaranteed by the compiler that sp[unum + 1] is a map
                rt_store_map(sp[unum + 1], sp[0], sp[1]);
                sp += 2;
                break;
388
                */
389

390
            case MP_BC_BUILD_SET:
391
                DECODE_UINT;
392
                printf("BUILD_SET " UINT_FMT, unum);
393
394
                break;

395
            case MP_BC_SET_ADD:
396
                DECODE_UINT;
397
                printf("SET_ADD " UINT_FMT, unum);
398
399
                break;

400
401
402
403
404
405
406
#if MICROPY_ENABLE_SLICE
            case MP_BC_BUILD_SLICE:
                DECODE_UINT;
                printf("BUILD_SLICE " UINT_FMT, unum);
                break;
#endif

407
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
408
                DECODE_UINT;
409
                printf("UNPACK_SEQUENCE " UINT_FMT, unum);
Damien's avatar
Damien committed
410
411
                break;

412
            case MP_BC_MAKE_FUNCTION:
413
                DECODE_PTR;
414
                printf("MAKE_FUNCTION " UINT_FMT, unum);
415
416
                break;

417
            case MP_BC_MAKE_FUNCTION_DEFARGS:
418
                DECODE_PTR;
419
420
421
                printf("MAKE_FUNCTION_DEFARGS " UINT_FMT, unum);
                break;

Damien George's avatar
Damien George committed
422
            case MP_BC_MAKE_CLOSURE:
423
                DECODE_PTR;
Damien George's avatar
Damien George committed
424
425
426
                printf("MAKE_CLOSURE " UINT_FMT, unum);
                break;

427
            case MP_BC_MAKE_CLOSURE_DEFARGS:
428
                DECODE_PTR;
429
430
431
                printf("MAKE_CLOSURE_DEFARGS " UINT_FMT, unum);
                break;

432
            case MP_BC_CALL_FUNCTION:
433
                DECODE_UINT;
434
                printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
435
436
                break;

437
438
439
440
441
            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;

442
            case MP_BC_CALL_METHOD:
443
                DECODE_UINT;
444
                printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
445
446
                break;

447
448
449
450
451
            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;

452
            case MP_BC_RETURN_VALUE:
453
454
455
                printf("RETURN_VALUE");
                break;

456
457
458
459
460
            case MP_BC_RAISE_VARARGS:
                unum = *ip++;
                printf("RAISE_VARARGS " UINT_FMT, unum);
                break;

461
            case MP_BC_YIELD_VALUE:
462
463
                printf("YIELD_VALUE");
                break;
464

Paul Sokolovsky's avatar
Paul Sokolovsky committed
465
466
467
468
            case MP_BC_YIELD_FROM:
                printf("YIELD_FROM");
                break;

469
470
            case MP_BC_IMPORT_NAME:
                DECODE_QSTR;
471
                printf("IMPORT_NAME '%s'", qstr_str(qstr));
472
473
474
475
                break;

            case MP_BC_IMPORT_FROM:
                DECODE_QSTR;
476
                printf("IMPORT_FROM '%s'", qstr_str(qstr));
477
478
                break;

479
480
481
482
            case MP_BC_IMPORT_STAR:
                printf("IMPORT_STAR");
                break;

483
484
485
486
487
488
489
490
            default:
                printf("code %p, byte code 0x%02x not implemented\n", ip, op);
                assert(0);
                return;
        }
        printf("\n");
    }
}
491

492
#endif // MICROPY_DEBUG_PRINTERS