showbc.c 12.1 KB
Newer Older
1
2
3
4
5
6
7
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "misc.h"
8
#include "mpconfig.h"
9
#include "qstr.h"
10
#include "bc0.h"
11

12
#if MICROPY_DEBUG_PRINTERS
13

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

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

32
33
34
35
    // 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;

Damien George's avatar
Damien George committed
36
37
38
39
40
41
42
43
44
45
46
47
    // decode prelude
    {
        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;
    }

48
49
50
51
52
53
    machine_uint_t unum;
    qstr qstr;
    while (ip - ip_start < len) {
        printf("%02u ", (uint)(ip - ip_start));
        int op = *ip++;
        switch (op) {
54
            case MP_BC_LOAD_CONST_FALSE:
55
56
57
                printf("LOAD_CONST_FALSE");
                break;

58
            case MP_BC_LOAD_CONST_NONE:
59
60
61
                printf("LOAD_CONST_NONE");
                break;

62
            case MP_BC_LOAD_CONST_TRUE:
63
64
65
                printf("LOAD_CONST_TRUE");
                break;

Damien George's avatar
Damien George committed
66
67
68
69
            case MP_BC_LOAD_CONST_ELLIPSIS:
                printf("LOAD_CONST_ELLIPSIS");
                break;

70
71
72
73
74
75
76
77
78
79
            case MP_BC_LOAD_CONST_SMALL_INT: {
                int num = 0;
                if ((ip[0] & 0x40) != 0) {
                    // Number is negative
                    num--;
                }
                do {
                    num = (num << 7) | (*ip & 0x7f);
                } while ((*ip++ & 0x80) != 0);
                printf("LOAD_CONST_SMALL_INT %d", num);
80
                break;
81
            }
82

83
84
85
86
87
            case MP_BC_LOAD_CONST_INT:
                DECODE_QSTR;
                printf("LOAD_CONST_INT %s", qstr_str(qstr));
                break;

88
            case MP_BC_LOAD_CONST_DEC:
89
                DECODE_QSTR;
90
                printf("LOAD_CONST_DEC %s", qstr_str(qstr));
91
92
                break;

93
            case MP_BC_LOAD_CONST_ID:
94
                DECODE_QSTR;
Damien's avatar
Damien committed
95
                printf("LOAD_CONST_ID %s", qstr_str(qstr));
96
97
                break;

98
            case MP_BC_LOAD_CONST_STRING:
99
                DECODE_QSTR;
Damien's avatar
Damien committed
100
                printf("LOAD_CONST_STRING %s", qstr_str(qstr));
101
102
                break;

103
            case MP_BC_LOAD_FAST_0:
104
105
106
                printf("LOAD_FAST_0");
                break;

107
            case MP_BC_LOAD_FAST_1:
108
109
110
                printf("LOAD_FAST_1");
                break;

111
            case MP_BC_LOAD_FAST_2:
112
113
114
                printf("LOAD_FAST_2");
                break;

115
            case MP_BC_LOAD_FAST_N:
116
                DECODE_UINT;
117
                printf("LOAD_FAST_N " UINT_FMT, unum);
118
119
                break;

Damien George's avatar
Damien George committed
120
121
122
123
124
            case MP_BC_LOAD_DEREF:
                DECODE_UINT;
                printf("LOAD_DEREF " UINT_FMT, unum);
                break;

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

130
            case MP_BC_LOAD_GLOBAL:
131
132
133
134
                DECODE_QSTR;
                printf("LOAD_GLOBAL %s", qstr_str(qstr));
                break;

135
            case MP_BC_LOAD_ATTR:
136
                DECODE_QSTR;
Damien's avatar
Damien committed
137
                printf("LOAD_ATTR %s", qstr_str(qstr));
138
139
                break;

140
            case MP_BC_LOAD_METHOD:
141
142
143
144
                DECODE_QSTR;
                printf("LOAD_METHOD %s", qstr_str(qstr));
                break;

145
            case MP_BC_LOAD_BUILD_CLASS:
Damien's avatar
Damien committed
146
                printf("LOAD_BUILD_CLASS");
147
148
                break;

149
            case MP_BC_STORE_FAST_0:
150
151
152
                printf("STORE_FAST_0");
                break;

153
            case MP_BC_STORE_FAST_1:
154
155
156
                printf("STORE_FAST_1");
                break;

157
            case MP_BC_STORE_FAST_2:
158
159
160
                printf("STORE_FAST_2");
                break;

161
            case MP_BC_STORE_FAST_N:
162
                DECODE_UINT;
163
                printf("STORE_FAST_N " UINT_FMT, unum);
164
165
                break;

Damien George's avatar
Damien George committed
166
167
168
169
170
            case MP_BC_STORE_DEREF:
                DECODE_UINT;
                printf("STORE_DEREF " UINT_FMT, unum);
                break;

171
            case MP_BC_STORE_NAME:
172
173
174
175
                DECODE_QSTR;
                printf("STORE_NAME %s", qstr_str(qstr));
                break;

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

181
            case MP_BC_STORE_ATTR:
182
                DECODE_QSTR;
Damien's avatar
Damien committed
183
                printf("STORE_ATTR %s", qstr_str(qstr));
184
185
                break;

186
            case MP_BC_STORE_SUBSCR:
Damien's avatar
Damien committed
187
                printf("STORE_SUBSCR");
188
189
                break;

Damien George's avatar
Damien George committed
190
191
192
193
194
195
196
197
198
199
            case MP_BC_DELETE_FAST_N:
                DECODE_UINT;
                printf("DELETE_FAST_N " UINT_FMT, unum);
                break;

            case MP_BC_DELETE_NAME:
                DECODE_QSTR;
                printf("DELETE_NAME %s", qstr_str(qstr));
                break;

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

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

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

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

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

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

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

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

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

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

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

250
251
252
253
            case MP_BC_UNWIND_JUMP:
                DECODE_SLABEL;
                printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - ip_start, *ip);
                ip += 1;
254
255
                break;

256
            case MP_BC_SETUP_EXCEPT:
257
                DECODE_ULABEL; // except labels are always forward
258
                printf("SETUP_EXCEPT " UINT_FMT, ip + unum - ip_start);
259
260
                break;

261
262
263
264
265
            case MP_BC_SETUP_FINALLY:
                DECODE_ULABEL; // except labels are always forward
                printf("SETUP_FINALLY " UINT_FMT, ip + unum - ip_start);
                break;

266
            case MP_BC_END_FINALLY:
267
268
269
270
                // 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
271
                printf("END_FINALLY");
272
273
                break;

274
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
275
                printf("GET_ITER");
276
277
                break;

278
            case MP_BC_FOR_ITER:
279
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
280
                printf("FOR_ITER " UINT_FMT, ip + unum - ip_start);
281
282
                break;

283
            case MP_BC_POP_BLOCK:
284
                // pops block and restores the stack
285
                printf("POP_BLOCK");
286
287
                break;

288
            case MP_BC_POP_EXCEPT:
289
                // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
290
                printf("POP_EXCEPT");
291
292
                break;

293
294
295
296
            case MP_BC_NOT:
                printf("NOT");
                break;

297
            case MP_BC_UNARY_OP:
298
                unum = *ip++;
299
                printf("UNARY_OP " UINT_FMT, unum);
300
301
                break;

302
            case MP_BC_BINARY_OP:
303
                unum = *ip++;
304
                printf("BINARY_OP " UINT_FMT, unum);
305
306
                break;

307
            case MP_BC_BUILD_TUPLE:
308
                DECODE_UINT;
309
                printf("BUILD_TUPLE " UINT_FMT, unum);
310
311
                break;

312
            case MP_BC_BUILD_LIST:
313
                DECODE_UINT;
314
                printf("BUILD_LIST " UINT_FMT, unum);
315
316
                break;

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

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

327
            case MP_BC_STORE_MAP:
328
                printf("STORE_MAP");
329
330
                break;

331
                /*
332
            case MP_BC_MAP_ADD:
333
334
335
336
337
                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;
338
                */
339

340
            case MP_BC_BUILD_SET:
341
                DECODE_UINT;
342
                printf("BUILD_SET " UINT_FMT, unum);
343
344
                break;

345
            case MP_BC_SET_ADD:
346
                DECODE_UINT;
347
                printf("SET_ADD " UINT_FMT, unum);
348
349
                break;

350
351
352
353
354
355
356
#if MICROPY_ENABLE_SLICE
            case MP_BC_BUILD_SLICE:
                DECODE_UINT;
                printf("BUILD_SLICE " UINT_FMT, unum);
                break;
#endif

357
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
358
                DECODE_UINT;
359
                printf("UNPACK_SEQUENCE " UINT_FMT, unum);
Damien's avatar
Damien committed
360
361
                break;

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

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

Damien George's avatar
Damien George committed
372
373
374
375
376
            case MP_BC_MAKE_CLOSURE:
                DECODE_UINT;
                printf("MAKE_CLOSURE " UINT_FMT, unum);
                break;

377
            case MP_BC_CALL_FUNCTION:
378
                DECODE_UINT;
379
                printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
380
381
                break;

382
            case MP_BC_CALL_METHOD:
383
                DECODE_UINT;
384
                printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
385
386
                break;

387
            case MP_BC_RETURN_VALUE:
388
389
390
                printf("RETURN_VALUE");
                break;

391
392
393
394
395
            case MP_BC_RAISE_VARARGS:
                unum = *ip++;
                printf("RAISE_VARARGS " UINT_FMT, unum);
                break;

396
            case MP_BC_YIELD_VALUE:
397
398
                printf("YIELD_VALUE");
                break;
399

400
401
            case MP_BC_IMPORT_NAME:
                DECODE_QSTR;
402
                printf("IMPORT_NAME %s", qstr_str(qstr));
403
404
405
406
                break;

            case MP_BC_IMPORT_FROM:
                DECODE_QSTR;
407
                printf("IMPORT_FROM %s", qstr_str(qstr));
408
409
                break;

410
411
412
413
            case MP_BC_IMPORT_STAR:
                printf("IMPORT_STAR");
                break;

414
415
416
417
418
419
420
421
            default:
                printf("code %p, byte code 0x%02x not implemented\n", ip, op);
                assert(0);
                return;
        }
        printf("\n");
    }
}
422

423
#endif // MICROPY_DEBUG_PRINTERS