showbc.c 15 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
32
void mp_byte_code_print2(const byte *ip, int len);

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

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

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

62
63
64
65
66
67
68
69
70
71
72
73
74
75
    // 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);
        }
    }
76
77
    mp_byte_code_print2(ip, len - 0);
}
78

79
80
void mp_byte_code_print2(const byte *ip, int len) {
    const byte *ip_start = ip;
81
82
83
84
85
86
    machine_uint_t unum;
    qstr qstr;
    while (ip - ip_start < len) {
        printf("%02u ", (uint)(ip - ip_start));
        int op = *ip++;
        switch (op) {
87
            case MP_BC_LOAD_CONST_FALSE:
88
89
90
                printf("LOAD_CONST_FALSE");
                break;

91
            case MP_BC_LOAD_CONST_NONE:
92
93
94
                printf("LOAD_CONST_NONE");
                break;

95
            case MP_BC_LOAD_CONST_TRUE:
96
97
98
                printf("LOAD_CONST_TRUE");
                break;

Damien George's avatar
Damien George committed
99
100
101
102
            case MP_BC_LOAD_CONST_ELLIPSIS:
                printf("LOAD_CONST_ELLIPSIS");
                break;

103
            case MP_BC_LOAD_CONST_SMALL_INT: {
104
                machine_int_t num = 0;
105
106
107
108
109
110
111
                if ((ip[0] & 0x40) != 0) {
                    // Number is negative
                    num--;
                }
                do {
                    num = (num << 7) | (*ip & 0x7f);
                } while ((*ip++ & 0x80) != 0);
112
                printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
113
                break;
114
            }
115

116
117
118
119
120
            case MP_BC_LOAD_CONST_INT:
                DECODE_QSTR;
                printf("LOAD_CONST_INT %s", qstr_str(qstr));
                break;

121
            case MP_BC_LOAD_CONST_DEC:
122
                DECODE_QSTR;
123
                printf("LOAD_CONST_DEC %s", qstr_str(qstr));
124
125
                break;

126
            case MP_BC_LOAD_CONST_ID:
127
                DECODE_QSTR;
128
                printf("LOAD_CONST_ID '%s'", qstr_str(qstr));
129
130
                break;

131
132
133
134
135
            case MP_BC_LOAD_CONST_BYTES:
                DECODE_QSTR;
                printf("LOAD_CONST_BYTES %s", qstr_str(qstr));
                break;

136
            case MP_BC_LOAD_CONST_STRING:
137
                DECODE_QSTR;
138
                printf("LOAD_CONST_STRING '%s'", qstr_str(qstr));
139
140
                break;

Paul Sokolovsky's avatar
Paul Sokolovsky committed
141
142
143
144
            case MP_BC_LOAD_NULL:
                printf("LOAD_NULL");
                break;

145
            case MP_BC_LOAD_FAST_0:
146
147
148
                printf("LOAD_FAST_0");
                break;

149
            case MP_BC_LOAD_FAST_1:
150
151
152
                printf("LOAD_FAST_1");
                break;

153
            case MP_BC_LOAD_FAST_2:
154
155
156
                printf("LOAD_FAST_2");
                break;

157
            case MP_BC_LOAD_FAST_N:
158
                DECODE_UINT;
159
                printf("LOAD_FAST_N " UINT_FMT, unum);
160
161
                break;

Damien George's avatar
Damien George committed
162
163
164
165
166
            case MP_BC_LOAD_DEREF:
                DECODE_UINT;
                printf("LOAD_DEREF " UINT_FMT, unum);
                break;

167
            case MP_BC_LOAD_NAME:
168
169
170
171
                DECODE_QSTR;
                printf("LOAD_NAME %s", qstr_str(qstr));
                break;

172
            case MP_BC_LOAD_GLOBAL:
173
174
175
176
                DECODE_QSTR;
                printf("LOAD_GLOBAL %s", qstr_str(qstr));
                break;

177
            case MP_BC_LOAD_ATTR:
178
                DECODE_QSTR;
Damien's avatar
Damien committed
179
                printf("LOAD_ATTR %s", qstr_str(qstr));
180
181
                break;

182
            case MP_BC_LOAD_METHOD:
183
184
185
186
                DECODE_QSTR;
                printf("LOAD_METHOD %s", qstr_str(qstr));
                break;

187
            case MP_BC_LOAD_BUILD_CLASS:
Damien's avatar
Damien committed
188
                printf("LOAD_BUILD_CLASS");
189
190
                break;

191
192
193
194
            case MP_BC_LOAD_SUBSCR:
                printf("LOAD_SUBSCR");
                break;

195
            case MP_BC_STORE_FAST_0:
196
197
198
                printf("STORE_FAST_0");
                break;

199
            case MP_BC_STORE_FAST_1:
200
201
202
                printf("STORE_FAST_1");
                break;

203
            case MP_BC_STORE_FAST_2:
204
205
206
                printf("STORE_FAST_2");
                break;

207
            case MP_BC_STORE_FAST_N:
208
                DECODE_UINT;
209
                printf("STORE_FAST_N " UINT_FMT, unum);
210
211
                break;

Damien George's avatar
Damien George committed
212
213
214
215
216
            case MP_BC_STORE_DEREF:
                DECODE_UINT;
                printf("STORE_DEREF " UINT_FMT, unum);
                break;

217
            case MP_BC_STORE_NAME:
218
219
220
221
                DECODE_QSTR;
                printf("STORE_NAME %s", qstr_str(qstr));
                break;

222
            case MP_BC_STORE_GLOBAL:
223
                DECODE_QSTR;
224
                printf("STORE_GLOBAL %s", qstr_str(qstr));
225
226
                break;

227
            case MP_BC_STORE_ATTR:
228
                DECODE_QSTR;
Damien's avatar
Damien committed
229
                printf("STORE_ATTR %s", qstr_str(qstr));
230
231
                break;

232
            case MP_BC_STORE_SUBSCR:
Damien's avatar
Damien committed
233
                printf("STORE_SUBSCR");
234
235
                break;

236
237
238
239
240
241
242
243
244
245
            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
246
247
248
249
250
            case MP_BC_DELETE_NAME:
                DECODE_QSTR;
                printf("DELETE_NAME %s", qstr_str(qstr));
                break;

251
            case MP_BC_DUP_TOP:
252
                printf("DUP_TOP");
253
254
                break;

255
            case MP_BC_DUP_TOP_TWO:
Damien's avatar
Damien committed
256
                printf("DUP_TOP_TWO");
257
258
                break;

259
            case MP_BC_POP_TOP:
260
261
262
                printf("POP_TOP");
                break;

263
            case MP_BC_ROT_TWO:
264
                printf("ROT_TWO");
265
266
                break;

267
            case MP_BC_ROT_THREE:
Damien's avatar
Damien committed
268
                printf("ROT_THREE");
269
270
                break;

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

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

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

286
            case MP_BC_JUMP_IF_TRUE_OR_POP:
287
                DECODE_SLABEL;
288
                printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, ip + unum - ip_start);
289
290
                break;

291
            case MP_BC_JUMP_IF_FALSE_OR_POP:
292
                DECODE_SLABEL;
293
                printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, ip + unum - ip_start);
294
295
                break;

296
297
298
299
300
301
302
303
304
            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;

305
306
307
308
            case MP_BC_UNWIND_JUMP:
                DECODE_SLABEL;
                printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - ip_start, *ip);
                ip += 1;
309
310
                break;

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

316
317
318
319
320
            case MP_BC_SETUP_FINALLY:
                DECODE_ULABEL; // except labels are always forward
                printf("SETUP_FINALLY " UINT_FMT, ip + unum - ip_start);
                break;

321
            case MP_BC_END_FINALLY:
322
323
324
325
                // 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
326
                printf("END_FINALLY");
327
328
                break;

329
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
330
                printf("GET_ITER");
331
332
                break;

333
            case MP_BC_FOR_ITER:
334
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
335
                printf("FOR_ITER " UINT_FMT, ip + unum - ip_start);
336
337
                break;

338
            case MP_BC_POP_BLOCK:
339
                // pops block and restores the stack
340
                printf("POP_BLOCK");
341
342
                break;

343
            case MP_BC_POP_EXCEPT:
344
                // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
345
                printf("POP_EXCEPT");
346
347
                break;

348
349
350
351
            case MP_BC_NOT:
                printf("NOT");
                break;

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

357
            case MP_BC_BINARY_OP:
358
                unum = *ip++;
359
                printf("BINARY_OP " UINT_FMT, unum);
360
361
                break;

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

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

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

377
            case MP_BC_BUILD_MAP:
378
                DECODE_UINT;
379
                printf("BUILD_MAP " UINT_FMT, unum);
380
381
                break;

382
            case MP_BC_STORE_MAP:
383
                printf("STORE_MAP");
384
385
                break;

386
                /*
387
            case MP_BC_MAP_ADD:
388
389
390
391
392
                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;
393
                */
394

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

400
            case MP_BC_SET_ADD:
401
                DECODE_UINT;
402
                printf("SET_ADD " UINT_FMT, unum);
403
404
                break;

405
406
407
408
409
410
411
#if MICROPY_ENABLE_SLICE
            case MP_BC_BUILD_SLICE:
                DECODE_UINT;
                printf("BUILD_SLICE " UINT_FMT, unum);
                break;
#endif

412
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
413
                DECODE_UINT;
414
                printf("UNPACK_SEQUENCE " UINT_FMT, unum);
Damien's avatar
Damien committed
415
416
                break;

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

422
            case MP_BC_MAKE_FUNCTION_DEFARGS:
423
                DECODE_PTR;
424
425
426
                printf("MAKE_FUNCTION_DEFARGS " UINT_FMT, unum);
                break;

427
            case MP_BC_MAKE_CLOSURE: {
428
                DECODE_PTR;
429
430
                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
431
                break;
432
            }
Damien George's avatar
Damien George committed
433

434
            case MP_BC_MAKE_CLOSURE_DEFARGS: {
435
                DECODE_PTR;
436
437
                machine_uint_t n_closed_over = *ip++;
                printf("MAKE_CLOSURE_DEFARGS " UINT_FMT " " UINT_FMT, unum, n_closed_over);
438
                break;
439
            }
440

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

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

451
            case MP_BC_CALL_METHOD:
452
                DECODE_UINT;
453
                printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
454
455
                break;

456
457
458
459
460
            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;

461
            case MP_BC_RETURN_VALUE:
462
463
464
                printf("RETURN_VALUE");
                break;

465
466
467
468
469
            case MP_BC_RAISE_VARARGS:
                unum = *ip++;
                printf("RAISE_VARARGS " UINT_FMT, unum);
                break;

470
            case MP_BC_YIELD_VALUE:
471
472
                printf("YIELD_VALUE");
                break;
473

Paul Sokolovsky's avatar
Paul Sokolovsky committed
474
475
476
477
            case MP_BC_YIELD_FROM:
                printf("YIELD_FROM");
                break;

478
479
            case MP_BC_IMPORT_NAME:
                DECODE_QSTR;
480
                printf("IMPORT_NAME '%s'", qstr_str(qstr));
481
482
483
484
                break;

            case MP_BC_IMPORT_FROM:
                DECODE_QSTR;
485
                printf("IMPORT_FROM '%s'", qstr_str(qstr));
486
487
                break;

488
489
490
491
            case MP_BC_IMPORT_STAR:
                printf("IMPORT_STAR");
                break;

492
493
494
495
496
497
498
499
            default:
                printf("code %p, byte code 0x%02x not implemented\n", ip, op);
                assert(0);
                return;
        }
        printf("\n");
    }
}
500

501
#endif // MICROPY_DEBUG_PRINTERS