showbc.c 11.8 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;

Damien George's avatar
Damien George committed
173
174
175
176
177
178
179
180
181
182
            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;

183
            case MP_BC_DUP_TOP:
184
                printf("DUP_TOP");
185
186
                break;

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

191
            case MP_BC_POP_TOP:
192
193
194
                printf("POP_TOP");
                break;

195
            case MP_BC_ROT_TWO:
196
                printf("ROT_TWO");
197
198
                break;

199
            case MP_BC_ROT_THREE:
Damien's avatar
Damien committed
200
                printf("ROT_THREE");
201
202
                break;

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

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

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

218
            case MP_BC_JUMP_IF_TRUE_OR_POP:
219
                DECODE_SLABEL;
220
                printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, ip + unum - ip_start);
221
222
                break;

223
            case MP_BC_JUMP_IF_FALSE_OR_POP:
224
                DECODE_SLABEL;
225
                printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, ip + unum - ip_start);
226
227
                break;

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;

233
234
235
236
            case MP_BC_UNWIND_JUMP:
                DECODE_SLABEL;
                printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - ip_start, *ip);
                ip += 1;
237
238
                break;

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

244
245
246
247
248
            case MP_BC_SETUP_FINALLY:
                DECODE_ULABEL; // except labels are always forward
                printf("SETUP_FINALLY " UINT_FMT, ip + unum - ip_start);
                break;

249
            case MP_BC_END_FINALLY:
250
251
252
253
                // 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
254
                printf("END_FINALLY");
255
256
                break;

257
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
258
                printf("GET_ITER");
259
260
                break;

261
            case MP_BC_FOR_ITER:
262
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
263
                printf("FOR_ITER " UINT_FMT, ip + unum - ip_start);
264
265
                break;

266
            case MP_BC_POP_BLOCK:
267
                // pops block and restores the stack
268
                printf("POP_BLOCK");
269
270
                break;

271
            case MP_BC_POP_EXCEPT:
272
                // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
273
                printf("POP_EXCEPT");
274
275
                break;

276
277
278
279
            case MP_BC_NOT:
                printf("NOT");
                break;

280
            case MP_BC_UNARY_OP:
281
                unum = *ip++;
282
                printf("UNARY_OP " UINT_FMT, unum);
283
284
                break;

285
            case MP_BC_BINARY_OP:
286
                unum = *ip++;
287
                printf("BINARY_OP " UINT_FMT, unum);
288
289
                break;

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

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

300
            case MP_BC_LIST_APPEND:
301
                DECODE_UINT;
302
                printf("LIST_APPEND " UINT_FMT, unum);
303
304
                break;

305
            case MP_BC_BUILD_MAP:
306
                DECODE_UINT;
307
                printf("BUILD_MAP " UINT_FMT, unum);
308
309
                break;

310
            case MP_BC_STORE_MAP:
311
                printf("STORE_MAP");
312
313
                break;

314
                /*
315
            case MP_BC_MAP_ADD:
316
317
318
319
320
                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;
321
                */
322

323
            case MP_BC_BUILD_SET:
324
                DECODE_UINT;
325
                printf("BUILD_SET " UINT_FMT, unum);
326
327
                break;

328
            case MP_BC_SET_ADD:
329
                DECODE_UINT;
330
                printf("SET_ADD " UINT_FMT, unum);
331
332
                break;

333
334
335
336
337
338
339
#if MICROPY_ENABLE_SLICE
            case MP_BC_BUILD_SLICE:
                DECODE_UINT;
                printf("BUILD_SLICE " UINT_FMT, unum);
                break;
#endif

340
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
341
                DECODE_UINT;
342
                printf("UNPACK_SEQUENCE " UINT_FMT, unum);
Damien's avatar
Damien committed
343
344
                break;

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

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

Damien George's avatar
Damien George committed
355
356
357
358
359
            case MP_BC_MAKE_CLOSURE:
                DECODE_UINT;
                printf("MAKE_CLOSURE " UINT_FMT, unum);
                break;

360
            case MP_BC_CALL_FUNCTION:
361
                DECODE_UINT;
362
                printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
363
364
                break;

365
            case MP_BC_CALL_METHOD:
366
                DECODE_UINT;
367
                printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
368
369
                break;

370
            case MP_BC_RETURN_VALUE:
371
372
373
                printf("RETURN_VALUE");
                break;

374
375
376
377
378
            case MP_BC_RAISE_VARARGS:
                unum = *ip++;
                printf("RAISE_VARARGS " UINT_FMT, unum);
                break;

379
            case MP_BC_YIELD_VALUE:
380
381
                printf("YIELD_VALUE");
                break;
382

383
384
            case MP_BC_IMPORT_NAME:
                DECODE_QSTR;
385
                printf("IMPORT_NAME %s", qstr_str(qstr));
386
387
388
389
                break;

            case MP_BC_IMPORT_FROM:
                DECODE_QSTR;
390
                printf("IMPORT_FROM %s", qstr_str(qstr));
391
392
                break;

393
394
395
396
397
398
399
400
            default:
                printf("code %p, byte code 0x%02x not implemented\n", ip, op);
                assert(0);
                return;
        }
        printf("\n");
    }
}
401

402
#endif // MICROPY_DEBUG_PRINTERS