showbc.c 10.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
9
#include "mpconfig.h"
#include "bc0.h"
10

11
12
#if MICROPY_SHOW_BC

13
14
15
16
17
#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)

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

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

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

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

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

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

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

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

70
                /*
71
            case MP_BC_LOAD_CONST_DEC:
72
73
74
                DECODE_QSTR;
                PUSH(rt_load_const_dec(qstr));
                break;
Damien's avatar
Damien committed
75
                */
76

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Damien's avatar
Damien committed
209
                /*
210
            case MP_BC_JUMP_IF_TRUE_OR_POP:
211
212
213
214
215
216
217
218
                DECODE_SLABEL;
                if (rt_is_true(*sp)) {
                    ip += unum;
                } else {
                    sp++;
                }
                break;

219
            case MP_BC_JUMP_IF_FALSE_OR_POP:
220
221
222
223
224
225
226
                DECODE_SLABEL;
                if (rt_is_true(*sp)) {
                    sp++;
                } else {
                    ip += unum;
                }
                break;
227
                */
228

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
            case MP_BC_END_FINALLY:
235
236
237
238
                // 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
239
                printf("END_FINALLY");
240
241
                break;

242
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
243
                printf("GET_ITER");
244
245
                break;

246
            case MP_BC_FOR_ITER:
247
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
248
                printf("FOR_ITER " UINT_FMT, ip + unum - ip_start);
249
250
                break;

251
            case MP_BC_POP_BLOCK:
252
                // pops block and restores the stack
253
                printf("POP_BLOCK");
254
255
                break;

256
            case MP_BC_POP_EXCEPT:
257
                // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
258
                printf("POP_EXCEPT");
259
260
                break;

261
                /*
262
            case MP_BC_UNARY_OP:
263
264
265
266
267
                unum = *ip++;
                *sp = rt_unary_op(unum, *sp);
                break;
                */

268
            case MP_BC_BINARY_OP:
269
                unum = *ip++;
270
                printf("BINARY_OP " UINT_FMT, unum);
271
272
                break;

273
            case MP_BC_BUILD_TUPLE:
274
                DECODE_UINT;
275
                printf("BUILD_TUPLE " UINT_FMT, unum);
276
277
                break;

278
            case MP_BC_BUILD_LIST:
279
                DECODE_UINT;
280
                printf("BUILD_LIST " UINT_FMT, unum);
281
282
                break;

283
            case MP_BC_LIST_APPEND:
284
                DECODE_UINT;
285
                printf("LIST_APPEND " UINT_FMT, unum);
286
287
                break;

288
            case MP_BC_BUILD_MAP:
289
                DECODE_UINT;
290
                printf("BUILD_MAP " UINT_FMT, unum);
291
292
                break;

293
            case MP_BC_STORE_MAP:
294
                printf("STORE_MAP");
295
296
                break;

297
                /*
298
            case MP_BC_MAP_ADD:
299
300
301
302
303
                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;
304
                */
305

306
            case MP_BC_BUILD_SET:
307
                DECODE_UINT;
308
                printf("BUILD_SET " UINT_FMT, unum);
309
310
                break;

311
            case MP_BC_SET_ADD:
312
                DECODE_UINT;
313
                printf("SET_ADD " UINT_FMT, unum);
314
315
                break;

316
317
318
319
320
321
322
#if MICROPY_ENABLE_SLICE
            case MP_BC_BUILD_SLICE:
                DECODE_UINT;
                printf("BUILD_SLICE " UINT_FMT, unum);
                break;
#endif

323
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
324
                DECODE_UINT;
325
                printf("UNPACK_SEQUENCE " UINT_FMT, unum);
Damien's avatar
Damien committed
326
327
                break;

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

Damien George's avatar
Damien George committed
333
334
335
336
337
            case MP_BC_MAKE_CLOSURE:
                DECODE_UINT;
                printf("MAKE_CLOSURE " UINT_FMT, unum);
                break;

338
            case MP_BC_CALL_FUNCTION:
339
                DECODE_UINT;
340
                printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
341
342
                break;

343
            case MP_BC_CALL_METHOD:
344
                DECODE_UINT;
345
                printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
346
347
                break;

348
            case MP_BC_RETURN_VALUE:
349
350
351
                printf("RETURN_VALUE");
                break;

352
353
354
355
356
            case MP_BC_RAISE_VARARGS:
                unum = *ip++;
                printf("RAISE_VARARGS " UINT_FMT, unum);
                break;

357
            case MP_BC_YIELD_VALUE:
358
359
                printf("YIELD_VALUE");
                break;
360

361
362
363
364
365
366
367
368
369
370
            case MP_BC_IMPORT_NAME:
                DECODE_QSTR;
                printf("IMPORT NAME %s", qstr_str(qstr));
                break;

            case MP_BC_IMPORT_FROM:
                DECODE_QSTR;
                printf("IMPORT NAME %s", qstr_str(qstr));
                break;

371
372
373
374
375
376
377
378
            default:
                printf("code %p, byte code 0x%02x not implemented\n", ip, op);
                assert(0);
                return;
        }
        printf("\n");
    }
}
379
380

#endif // MICROPY_SHOW_BC