showbc.c 11.6 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
#define DECODE_UINT do { unum = *ip++; if (unum > 127) { unum = ((unum & 0x3f) << 8) | (*ip++); } } while (0)
#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)
#define DECODE_QSTR do { qstr = *ip++; if (qstr > 127) { qstr = ((qstr & 0x3f) << 8) | (*ip++); } } while (0)

19
void mp_byte_code_print(const byte *ip, int len) {
20
    const byte *ip_start = ip;
Damien George's avatar
Damien George committed
21

22
23
24
25
    // 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
26
27
28
29
30
31
32
33
34
35
36
37
    // 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;
    }

38
39
40
41
42
43
    machine_uint_t unum;
    qstr qstr;
    while (ip - ip_start < len) {
        printf("%02u ", (uint)(ip - ip_start));
        int op = *ip++;
        switch (op) {
44
            case MP_BC_LOAD_CONST_FALSE:
45
46
47
                printf("LOAD_CONST_FALSE");
                break;

48
            case MP_BC_LOAD_CONST_NONE:
49
50
51
                printf("LOAD_CONST_NONE");
                break;

52
            case MP_BC_LOAD_CONST_TRUE:
53
54
55
                printf("LOAD_CONST_TRUE");
                break;

Damien George's avatar
Damien George committed
56
57
58
59
            case MP_BC_LOAD_CONST_ELLIPSIS:
                printf("LOAD_CONST_ELLIPSIS");
                break;

60
            case MP_BC_LOAD_CONST_SMALL_INT:
61
62
63
64
65
                unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000;
                ip += 3;
                printf("LOAD_CONST_SMALL_INT %d", (int)unum);
                break;

66
67
68
69
70
            case MP_BC_LOAD_CONST_INT:
                DECODE_QSTR;
                printf("LOAD_CONST_INT %s", qstr_str(qstr));
                break;

71
            case MP_BC_LOAD_CONST_DEC:
72
                DECODE_QSTR;
73
                printf("LOAD_CONST_DEC %s", qstr_str(qstr));
74
75
                break;

76
            case MP_BC_LOAD_CONST_ID:
77
                DECODE_QSTR;
Damien's avatar
Damien committed
78
                printf("LOAD_CONST_ID %s", qstr_str(qstr));
79
80
                break;

81
            case MP_BC_LOAD_CONST_STRING:
82
                DECODE_QSTR;
Damien's avatar
Damien committed
83
                printf("LOAD_CONST_STRING %s", qstr_str(qstr));
84
85
                break;

86
            case MP_BC_LOAD_FAST_0:
87
88
89
                printf("LOAD_FAST_0");
                break;

90
            case MP_BC_LOAD_FAST_1:
91
92
93
                printf("LOAD_FAST_1");
                break;

94
            case MP_BC_LOAD_FAST_2:
95
96
97
                printf("LOAD_FAST_2");
                break;

98
            case MP_BC_LOAD_FAST_N:
99
                DECODE_UINT;
100
                printf("LOAD_FAST_N " UINT_FMT, unum);
101
102
                break;

Damien George's avatar
Damien George committed
103
104
105
106
107
            case MP_BC_LOAD_DEREF:
                DECODE_UINT;
                printf("LOAD_DEREF " UINT_FMT, unum);
                break;

108
            case MP_BC_LOAD_NAME:
109
110
111
112
                DECODE_QSTR;
                printf("LOAD_NAME %s", qstr_str(qstr));
                break;

113
            case MP_BC_LOAD_GLOBAL:
114
115
116
117
                DECODE_QSTR;
                printf("LOAD_GLOBAL %s", qstr_str(qstr));
                break;

118
            case MP_BC_LOAD_ATTR:
119
                DECODE_QSTR;
Damien's avatar
Damien committed
120
                printf("LOAD_ATTR %s", qstr_str(qstr));
121
122
                break;

123
            case MP_BC_LOAD_METHOD:
124
125
126
127
                DECODE_QSTR;
                printf("LOAD_METHOD %s", qstr_str(qstr));
                break;

128
            case MP_BC_LOAD_BUILD_CLASS:
Damien's avatar
Damien committed
129
                printf("LOAD_BUILD_CLASS");
130
131
                break;

132
            case MP_BC_STORE_FAST_0:
133
134
135
                printf("STORE_FAST_0");
                break;

136
            case MP_BC_STORE_FAST_1:
137
138
139
                printf("STORE_FAST_1");
                break;

140
            case MP_BC_STORE_FAST_2:
141
142
143
                printf("STORE_FAST_2");
                break;

144
            case MP_BC_STORE_FAST_N:
145
                DECODE_UINT;
146
                printf("STORE_FAST_N " UINT_FMT, unum);
147
148
                break;

Damien George's avatar
Damien George committed
149
150
151
152
153
            case MP_BC_STORE_DEREF:
                DECODE_UINT;
                printf("STORE_DEREF " UINT_FMT, unum);
                break;

154
            case MP_BC_STORE_NAME:
155
156
157
158
                DECODE_QSTR;
                printf("STORE_NAME %s", qstr_str(qstr));
                break;

159
            case MP_BC_STORE_GLOBAL:
160
                DECODE_QSTR;
161
                printf("STORE_GLOBAL %s", qstr_str(qstr));
162
163
                break;

164
            case MP_BC_STORE_ATTR:
165
                DECODE_QSTR;
Damien's avatar
Damien committed
166
                printf("STORE_ATTR %s", qstr_str(qstr));
167
168
                break;

169
            case MP_BC_STORE_SUBSCR:
Damien's avatar
Damien committed
170
                printf("STORE_SUBSCR");
171
172
                break;

173
            case MP_BC_DUP_TOP:
174
                printf("DUP_TOP");
175
176
                break;

177
            case MP_BC_DUP_TOP_TWO:
Damien's avatar
Damien committed
178
                printf("DUP_TOP_TWO");
179
180
                break;

181
            case MP_BC_POP_TOP:
182
183
184
                printf("POP_TOP");
                break;

185
            case MP_BC_ROT_TWO:
186
                printf("ROT_TWO");
187
188
                break;

189
            case MP_BC_ROT_THREE:
Damien's avatar
Damien committed
190
                printf("ROT_THREE");
191
192
                break;

193
            case MP_BC_JUMP:
194
                DECODE_SLABEL;
195
                printf("JUMP " UINT_FMT, ip + unum - ip_start);
196
197
                break;

198
            case MP_BC_POP_JUMP_IF_TRUE:
199
                DECODE_SLABEL;
200
                printf("POP_JUMP_IF_TRUE " UINT_FMT, ip + unum - ip_start);
201
202
                break;

203
            case MP_BC_POP_JUMP_IF_FALSE:
204
                DECODE_SLABEL;
205
                printf("POP_JUMP_IF_FALSE " UINT_FMT, ip + unum - ip_start);
206
207
                break;

208
            case MP_BC_JUMP_IF_TRUE_OR_POP:
209
                DECODE_SLABEL;
210
                printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, ip + unum - ip_start);
211
212
                break;

213
            case MP_BC_JUMP_IF_FALSE_OR_POP:
214
                DECODE_SLABEL;
215
                printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, ip + unum - ip_start);
216
217
                break;

218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
            case MP_BC_SETUP_LOOP:
                DECODE_ULABEL; // loop labels are always forward
                printf("SETUP_LOOP " UINT_FMT, ip + unum - ip_start);
                break;

            case MP_BC_BREAK_LOOP:
                DECODE_ULABEL; // loop labels are always forward
                printf("BREAK_LOOP " UINT_FMT, ip + unum - ip_start);
                break;

            case MP_BC_CONTINUE_LOOP:
                DECODE_ULABEL; // loop labels are always forward
                printf("CONTINUE_LOOP " UINT_FMT, ip + unum - ip_start);
                break;

233
            case MP_BC_SETUP_EXCEPT:
234
                DECODE_ULABEL; // except labels are always forward
235
                printf("SETUP_EXCEPT " UINT_FMT, ip + unum - ip_start);
236
237
                break;

238
239
240
241
242
            case MP_BC_SETUP_FINALLY:
                DECODE_ULABEL; // except labels are always forward
                printf("SETUP_FINALLY " UINT_FMT, ip + unum - ip_start);
                break;

243
            case MP_BC_END_FINALLY:
244
245
246
247
                // 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
248
                printf("END_FINALLY");
249
250
                break;

251
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
252
                printf("GET_ITER");
253
254
                break;

255
            case MP_BC_FOR_ITER:
256
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
257
                printf("FOR_ITER " UINT_FMT, ip + unum - ip_start);
258
259
                break;

260
            case MP_BC_POP_BLOCK:
261
                // pops block and restores the stack
262
                printf("POP_BLOCK");
263
264
                break;

265
            case MP_BC_POP_EXCEPT:
266
                // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
267
                printf("POP_EXCEPT");
268
269
                break;

270
                /*
271
            case MP_BC_UNARY_OP:
272
273
274
275
276
                unum = *ip++;
                *sp = rt_unary_op(unum, *sp);
                break;
                */

277
            case MP_BC_BINARY_OP:
278
                unum = *ip++;
279
                printf("BINARY_OP " UINT_FMT, unum);
280
281
                break;

282
            case MP_BC_BUILD_TUPLE:
283
                DECODE_UINT;
284
                printf("BUILD_TUPLE " UINT_FMT, unum);
285
286
                break;

287
            case MP_BC_BUILD_LIST:
288
                DECODE_UINT;
289
                printf("BUILD_LIST " UINT_FMT, unum);
290
291
                break;

292
            case MP_BC_LIST_APPEND:
293
                DECODE_UINT;
294
                printf("LIST_APPEND " UINT_FMT, unum);
295
296
                break;

297
            case MP_BC_BUILD_MAP:
298
                DECODE_UINT;
299
                printf("BUILD_MAP " UINT_FMT, unum);
300
301
                break;

302
            case MP_BC_STORE_MAP:
303
                printf("STORE_MAP");
304
305
                break;

306
                /*
307
            case MP_BC_MAP_ADD:
308
309
310
311
312
                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;
313
                */
314

315
            case MP_BC_BUILD_SET:
316
                DECODE_UINT;
317
                printf("BUILD_SET " UINT_FMT, unum);
318
319
                break;

320
            case MP_BC_SET_ADD:
321
                DECODE_UINT;
322
                printf("SET_ADD " UINT_FMT, unum);
323
324
                break;

325
326
327
328
329
330
331
#if MICROPY_ENABLE_SLICE
            case MP_BC_BUILD_SLICE:
                DECODE_UINT;
                printf("BUILD_SLICE " UINT_FMT, unum);
                break;
#endif

332
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
333
                DECODE_UINT;
334
                printf("UNPACK_SEQUENCE " UINT_FMT, unum);
Damien's avatar
Damien committed
335
336
                break;

337
            case MP_BC_MAKE_FUNCTION:
338
                DECODE_UINT;
339
                printf("MAKE_FUNCTION " UINT_FMT, unum);
340
341
                break;

342
343
344
345
346
            case MP_BC_MAKE_FUNCTION_DEFARGS:
                DECODE_UINT;
                printf("MAKE_FUNCTION_DEFARGS " UINT_FMT, unum);
                break;

Damien George's avatar
Damien George committed
347
348
349
350
351
            case MP_BC_MAKE_CLOSURE:
                DECODE_UINT;
                printf("MAKE_CLOSURE " UINT_FMT, unum);
                break;

352
            case MP_BC_CALL_FUNCTION:
353
                DECODE_UINT;
354
                printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
355
356
                break;

357
            case MP_BC_CALL_METHOD:
358
                DECODE_UINT;
359
                printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
360
361
                break;

362
            case MP_BC_RETURN_VALUE:
363
364
365
                printf("RETURN_VALUE");
                break;

366
367
368
369
370
            case MP_BC_RAISE_VARARGS:
                unum = *ip++;
                printf("RAISE_VARARGS " UINT_FMT, unum);
                break;

371
            case MP_BC_YIELD_VALUE:
372
373
                printf("YIELD_VALUE");
                break;
374

375
376
            case MP_BC_IMPORT_NAME:
                DECODE_QSTR;
377
                printf("IMPORT_NAME %s", qstr_str(qstr));
378
379
380
381
                break;

            case MP_BC_IMPORT_FROM:
                DECODE_QSTR;
382
                printf("IMPORT_FROM %s", qstr_str(qstr));
383
384
                break;

385
386
387
388
389
390
391
392
            default:
                printf("code %p, byte code 0x%02x not implemented\n", ip, op);
                assert(0);
                return;
        }
        printf("\n");
    }
}
393

394
#endif // MICROPY_DEBUG_PRINTERS