showbc.c 10.7 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
            case MP_BC_SETUP_EXCEPT:
219
                DECODE_ULABEL; // except labels are always forward
220
                printf("SETUP_EXCEPT " UINT_FMT, ip + unum - ip_start);
221
222
                break;

223
            case MP_BC_END_FINALLY:
224
225
226
227
                // 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
228
                printf("END_FINALLY");
229
230
                break;

231
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
232
                printf("GET_ITER");
233
234
                break;

235
            case MP_BC_FOR_ITER:
236
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
237
                printf("FOR_ITER " UINT_FMT, ip + unum - ip_start);
238
239
                break;

240
            case MP_BC_POP_BLOCK:
241
                // pops block and restores the stack
242
                printf("POP_BLOCK");
243
244
                break;

245
            case MP_BC_POP_EXCEPT:
246
                // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
247
                printf("POP_EXCEPT");
248
249
                break;

250
                /*
251
            case MP_BC_UNARY_OP:
252
253
254
255
256
                unum = *ip++;
                *sp = rt_unary_op(unum, *sp);
                break;
                */

257
            case MP_BC_BINARY_OP:
258
                unum = *ip++;
259
                printf("BINARY_OP " UINT_FMT, unum);
260
261
                break;

262
            case MP_BC_BUILD_TUPLE:
263
                DECODE_UINT;
264
                printf("BUILD_TUPLE " UINT_FMT, unum);
265
266
                break;

267
            case MP_BC_BUILD_LIST:
268
                DECODE_UINT;
269
                printf("BUILD_LIST " UINT_FMT, unum);
270
271
                break;

272
            case MP_BC_LIST_APPEND:
273
                DECODE_UINT;
274
                printf("LIST_APPEND " UINT_FMT, unum);
275
276
                break;

277
            case MP_BC_BUILD_MAP:
278
                DECODE_UINT;
279
                printf("BUILD_MAP " UINT_FMT, unum);
280
281
                break;

282
            case MP_BC_STORE_MAP:
283
                printf("STORE_MAP");
284
285
                break;

286
                /*
287
            case MP_BC_MAP_ADD:
288
289
290
291
292
                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;
293
                */
294

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

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

305
306
307
308
309
310
311
#if MICROPY_ENABLE_SLICE
            case MP_BC_BUILD_SLICE:
                DECODE_UINT;
                printf("BUILD_SLICE " UINT_FMT, unum);
                break;
#endif

312
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
313
                DECODE_UINT;
314
                printf("UNPACK_SEQUENCE " UINT_FMT, unum);
Damien's avatar
Damien committed
315
316
                break;

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

Damien George's avatar
Damien George committed
322
323
324
325
326
            case MP_BC_MAKE_CLOSURE:
                DECODE_UINT;
                printf("MAKE_CLOSURE " UINT_FMT, unum);
                break;

327
            case MP_BC_CALL_FUNCTION:
328
                DECODE_UINT;
329
                printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
330
331
                break;

332
            case MP_BC_CALL_METHOD:
333
                DECODE_UINT;
334
                printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
335
336
                break;

337
            case MP_BC_RETURN_VALUE:
338
339
340
                printf("RETURN_VALUE");
                break;

341
342
343
344
345
            case MP_BC_RAISE_VARARGS:
                unum = *ip++;
                printf("RAISE_VARARGS " UINT_FMT, unum);
                break;

346
            case MP_BC_YIELD_VALUE:
347
348
                printf("YIELD_VALUE");
                break;
349

350
351
            case MP_BC_IMPORT_NAME:
                DECODE_QSTR;
352
                printf("IMPORT_NAME %s", qstr_str(qstr));
353
354
355
356
                break;

            case MP_BC_IMPORT_FROM:
                DECODE_QSTR;
357
                printf("IMPORT_FROM %s", qstr_str(qstr));
358
359
                break;

360
361
362
363
364
365
366
367
            default:
                printf("code %p, byte code 0x%02x not implemented\n", ip, op);
                assert(0);
                return;
        }
        printf("\n");
    }
}
368

369
#endif // MICROPY_DEBUG_PRINTERS