showbc.c 11.5 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
            case MP_BC_SETUP_LOOP:
                DECODE_ULABEL; // loop labels are always forward
                printf("SETUP_LOOP " UINT_FMT, ip + unum - ip_start);
                break;

223
224
225
226
            case MP_BC_UNWIND_JUMP:
                DECODE_SLABEL;
                printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - ip_start, *ip);
                ip += 1;
227
228
                break;

229
            case MP_BC_SETUP_EXCEPT:
230
                DECODE_ULABEL; // except labels are always forward
231
                printf("SETUP_EXCEPT " UINT_FMT, ip + unum - ip_start);
232
233
                break;

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

239
            case MP_BC_END_FINALLY:
240
241
242
243
                // 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
244
                printf("END_FINALLY");
245
246
                break;

247
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
248
                printf("GET_ITER");
249
250
                break;

251
            case MP_BC_FOR_ITER:
252
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
253
                printf("FOR_ITER " UINT_FMT, ip + unum - ip_start);
254
255
                break;

256
            case MP_BC_POP_BLOCK:
257
                // pops block and restores the stack
258
                printf("POP_BLOCK");
259
260
                break;

261
            case MP_BC_POP_EXCEPT:
262
                // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
263
                printf("POP_EXCEPT");
264
265
                break;

266
267
268
269
            case MP_BC_NOT:
                printf("NOT");
                break;

270
            case MP_BC_UNARY_OP:
271
                unum = *ip++;
272
                printf("UNARY_OP " UINT_FMT, unum);
273
274
                break;

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

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

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

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

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

300
            case MP_BC_STORE_MAP:
301
                printf("STORE_MAP");
302
303
                break;

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

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

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

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

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

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

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

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

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

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

360
            case MP_BC_RETURN_VALUE:
361
362
363
                printf("RETURN_VALUE");
                break;

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

369
            case MP_BC_YIELD_VALUE:
370
371
                printf("YIELD_VALUE");
                break;
372

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

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

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

392
#endif // MICROPY_DEBUG_PRINTERS