showbc.c 13.2 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
void mp_byte_code_print(const byte *ip, int len) {
27
    const byte *ip_start = ip;
Damien George's avatar
Damien George committed
28

29
30
31
32
    // get code info size
    machine_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
    ip += code_info_size;

33
34
35
36
37
38
39
40
41
42
    // 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
43
44
45
46
47
48
49
50
51
52
53
    {
        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;
    }

54
55
56
57
58
59
    machine_uint_t unum;
    qstr qstr;
    while (ip - ip_start < len) {
        printf("%02u ", (uint)(ip - ip_start));
        int op = *ip++;
        switch (op) {
60
            case MP_BC_LOAD_CONST_FALSE:
61
62
63
                printf("LOAD_CONST_FALSE");
                break;

64
            case MP_BC_LOAD_CONST_NONE:
65
66
67
                printf("LOAD_CONST_NONE");
                break;

68
            case MP_BC_LOAD_CONST_TRUE:
69
70
71
                printf("LOAD_CONST_TRUE");
                break;

Damien George's avatar
Damien George committed
72
73
74
75
            case MP_BC_LOAD_CONST_ELLIPSIS:
                printf("LOAD_CONST_ELLIPSIS");
                break;

76
            case MP_BC_LOAD_CONST_SMALL_INT: {
77
                machine_int_t num = 0;
78
79
80
81
82
83
84
                if ((ip[0] & 0x40) != 0) {
                    // Number is negative
                    num--;
                }
                do {
                    num = (num << 7) | (*ip & 0x7f);
                } while ((*ip++ & 0x80) != 0);
85
                printf("LOAD_CONST_SMALL_INT " INT_FMT, num);
86
                break;
87
            }
88

89
90
91
92
93
            case MP_BC_LOAD_CONST_INT:
                DECODE_QSTR;
                printf("LOAD_CONST_INT %s", qstr_str(qstr));
                break;

94
            case MP_BC_LOAD_CONST_DEC:
95
                DECODE_QSTR;
96
                printf("LOAD_CONST_DEC %s", qstr_str(qstr));
97
98
                break;

99
            case MP_BC_LOAD_CONST_ID:
100
                DECODE_QSTR;
Damien's avatar
Damien committed
101
                printf("LOAD_CONST_ID %s", qstr_str(qstr));
102
103
                break;

104
            case MP_BC_LOAD_CONST_STRING:
105
                DECODE_QSTR;
Damien's avatar
Damien committed
106
                printf("LOAD_CONST_STRING %s", qstr_str(qstr));
107
108
                break;

109
            case MP_BC_LOAD_FAST_0:
110
111
112
                printf("LOAD_FAST_0");
                break;

113
            case MP_BC_LOAD_FAST_1:
114
115
116
                printf("LOAD_FAST_1");
                break;

117
            case MP_BC_LOAD_FAST_2:
118
119
120
                printf("LOAD_FAST_2");
                break;

121
            case MP_BC_LOAD_FAST_N:
122
                DECODE_UINT;
123
                printf("LOAD_FAST_N " UINT_FMT, unum);
124
125
                break;

Damien George's avatar
Damien George committed
126
127
128
129
130
            case MP_BC_LOAD_DEREF:
                DECODE_UINT;
                printf("LOAD_DEREF " UINT_FMT, unum);
                break;

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

136
            case MP_BC_LOAD_GLOBAL:
137
138
139
140
                DECODE_QSTR;
                printf("LOAD_GLOBAL %s", qstr_str(qstr));
                break;

141
            case MP_BC_LOAD_ATTR:
142
                DECODE_QSTR;
Damien's avatar
Damien committed
143
                printf("LOAD_ATTR %s", qstr_str(qstr));
144
145
                break;

146
            case MP_BC_LOAD_METHOD:
147
148
149
150
                DECODE_QSTR;
                printf("LOAD_METHOD %s", qstr_str(qstr));
                break;

151
            case MP_BC_LOAD_BUILD_CLASS:
Damien's avatar
Damien committed
152
                printf("LOAD_BUILD_CLASS");
153
154
                break;

155
            case MP_BC_STORE_FAST_0:
156
157
158
                printf("STORE_FAST_0");
                break;

159
            case MP_BC_STORE_FAST_1:
160
161
162
                printf("STORE_FAST_1");
                break;

163
            case MP_BC_STORE_FAST_2:
164
165
166
                printf("STORE_FAST_2");
                break;

167
            case MP_BC_STORE_FAST_N:
168
                DECODE_UINT;
169
                printf("STORE_FAST_N " UINT_FMT, unum);
170
171
                break;

Damien George's avatar
Damien George committed
172
173
174
175
176
            case MP_BC_STORE_DEREF:
                DECODE_UINT;
                printf("STORE_DEREF " UINT_FMT, unum);
                break;

177
            case MP_BC_STORE_NAME:
178
179
180
181
                DECODE_QSTR;
                printf("STORE_NAME %s", qstr_str(qstr));
                break;

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

187
            case MP_BC_STORE_ATTR:
188
                DECODE_QSTR;
Damien's avatar
Damien committed
189
                printf("STORE_ATTR %s", qstr_str(qstr));
190
191
                break;

192
            case MP_BC_STORE_SUBSCR:
Damien's avatar
Damien committed
193
                printf("STORE_SUBSCR");
194
195
                break;

Damien George's avatar
Damien George committed
196
197
198
199
200
            case MP_BC_DELETE_NAME:
                DECODE_QSTR;
                printf("DELETE_NAME %s", qstr_str(qstr));
                break;

201
            case MP_BC_DUP_TOP:
202
                printf("DUP_TOP");
203
204
                break;

205
            case MP_BC_DUP_TOP_TWO:
Damien's avatar
Damien committed
206
                printf("DUP_TOP_TWO");
207
208
                break;

209
            case MP_BC_POP_TOP:
210
211
212
                printf("POP_TOP");
                break;

213
            case MP_BC_ROT_TWO:
214
                printf("ROT_TWO");
215
216
                break;

217
            case MP_BC_ROT_THREE:
Damien's avatar
Damien committed
218
                printf("ROT_THREE");
219
220
                break;

221
            case MP_BC_JUMP:
222
                DECODE_SLABEL;
223
                printf("JUMP " UINT_FMT, ip + unum - ip_start);
224
225
                break;

226
            case MP_BC_POP_JUMP_IF_TRUE:
227
                DECODE_SLABEL;
228
                printf("POP_JUMP_IF_TRUE " UINT_FMT, ip + unum - ip_start);
229
230
                break;

231
            case MP_BC_POP_JUMP_IF_FALSE:
232
                DECODE_SLABEL;
233
                printf("POP_JUMP_IF_FALSE " UINT_FMT, ip + unum - ip_start);
234
235
                break;

236
            case MP_BC_JUMP_IF_TRUE_OR_POP:
237
                DECODE_SLABEL;
238
                printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, ip + unum - ip_start);
239
240
                break;

241
            case MP_BC_JUMP_IF_FALSE_OR_POP:
242
                DECODE_SLABEL;
243
                printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, ip + unum - ip_start);
244
245
                break;

246
247
248
249
250
            case MP_BC_SETUP_LOOP:
                DECODE_ULABEL; // loop labels are always forward
                printf("SETUP_LOOP " UINT_FMT, ip + unum - ip_start);
                break;

251
252
253
254
255
256
257
258
259
            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;

260
261
262
263
            case MP_BC_UNWIND_JUMP:
                DECODE_SLABEL;
                printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - ip_start, *ip);
                ip += 1;
264
265
                break;

266
            case MP_BC_SETUP_EXCEPT:
267
                DECODE_ULABEL; // except labels are always forward
268
                printf("SETUP_EXCEPT " UINT_FMT, ip + unum - ip_start);
269
270
                break;

271
272
273
274
275
            case MP_BC_SETUP_FINALLY:
                DECODE_ULABEL; // except labels are always forward
                printf("SETUP_FINALLY " UINT_FMT, ip + unum - ip_start);
                break;

276
            case MP_BC_END_FINALLY:
277
278
279
280
                // 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
281
                printf("END_FINALLY");
282
283
                break;

284
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
285
                printf("GET_ITER");
286
287
                break;

288
            case MP_BC_FOR_ITER:
289
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
290
                printf("FOR_ITER " UINT_FMT, ip + unum - ip_start);
291
292
                break;

293
            case MP_BC_POP_BLOCK:
294
                // pops block and restores the stack
295
                printf("POP_BLOCK");
296
297
                break;

298
            case MP_BC_POP_EXCEPT:
299
                // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
300
                printf("POP_EXCEPT");
301
302
                break;

303
304
305
306
            case MP_BC_NOT:
                printf("NOT");
                break;

307
            case MP_BC_UNARY_OP:
308
                unum = *ip++;
309
                printf("UNARY_OP " UINT_FMT, unum);
310
311
                break;

312
            case MP_BC_BINARY_OP:
313
                unum = *ip++;
314
                printf("BINARY_OP " UINT_FMT, unum);
315
316
                break;

317
            case MP_BC_BUILD_TUPLE:
318
                DECODE_UINT;
319
                printf("BUILD_TUPLE " UINT_FMT, unum);
320
321
                break;

322
            case MP_BC_BUILD_LIST:
323
                DECODE_UINT;
324
                printf("BUILD_LIST " UINT_FMT, unum);
325
326
                break;

327
            case MP_BC_LIST_APPEND:
328
                DECODE_UINT;
329
                printf("LIST_APPEND " UINT_FMT, unum);
330
331
                break;

332
            case MP_BC_BUILD_MAP:
333
                DECODE_UINT;
334
                printf("BUILD_MAP " UINT_FMT, unum);
335
336
                break;

337
            case MP_BC_STORE_MAP:
338
                printf("STORE_MAP");
339
340
                break;

341
                /*
342
            case MP_BC_MAP_ADD:
343
344
345
346
347
                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;
348
                */
349

350
            case MP_BC_BUILD_SET:
351
                DECODE_UINT;
352
                printf("BUILD_SET " UINT_FMT, unum);
353
354
                break;

355
            case MP_BC_SET_ADD:
356
                DECODE_UINT;
357
                printf("SET_ADD " UINT_FMT, unum);
358
359
                break;

360
361
362
363
364
365
366
#if MICROPY_ENABLE_SLICE
            case MP_BC_BUILD_SLICE:
                DECODE_UINT;
                printf("BUILD_SLICE " UINT_FMT, unum);
                break;
#endif

367
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
368
                DECODE_UINT;
369
                printf("UNPACK_SEQUENCE " UINT_FMT, unum);
Damien's avatar
Damien committed
370
371
                break;

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

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

Damien George's avatar
Damien George committed
382
383
384
385
386
            case MP_BC_MAKE_CLOSURE:
                DECODE_UINT;
                printf("MAKE_CLOSURE " UINT_FMT, unum);
                break;

387
388
389
390
391
            case MP_BC_MAKE_CLOSURE_DEFARGS:
                DECODE_UINT;
                printf("MAKE_CLOSURE_DEFARGS " UINT_FMT, unum);
                break;

392
            case MP_BC_CALL_FUNCTION:
393
                DECODE_UINT;
394
                printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
395
396
                break;

397
398
399
400
401
            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;

402
            case MP_BC_CALL_METHOD:
403
                DECODE_UINT;
404
                printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
405
406
                break;

407
408
409
410
411
            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;

412
            case MP_BC_RETURN_VALUE:
413
414
415
                printf("RETURN_VALUE");
                break;

416
417
418
419
420
            case MP_BC_RAISE_VARARGS:
                unum = *ip++;
                printf("RAISE_VARARGS " UINT_FMT, unum);
                break;

421
            case MP_BC_YIELD_VALUE:
422
423
                printf("YIELD_VALUE");
                break;
424

Paul Sokolovsky's avatar
Paul Sokolovsky committed
425
426
427
428
            case MP_BC_YIELD_FROM:
                printf("YIELD_FROM");
                break;

429
430
            case MP_BC_IMPORT_NAME:
                DECODE_QSTR;
431
                printf("IMPORT_NAME %s", qstr_str(qstr));
432
433
434
435
                break;

            case MP_BC_IMPORT_FROM:
                DECODE_QSTR;
436
                printf("IMPORT_FROM %s", qstr_str(qstr));
437
438
                break;

439
440
441
442
            case MP_BC_IMPORT_STAR:
                printf("IMPORT_STAR");
                break;

443
444
445
446
447
448
449
450
            default:
                printf("code %p, byte code 0x%02x not implemented\n", ip, op);
                assert(0);
                return;
        }
        printf("\n");
    }
}
451

452
#endif // MICROPY_DEBUG_PRINTERS