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
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;

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

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

219
            case MP_BC_SETUP_EXCEPT:
220
                DECODE_ULABEL; // except labels are always forward
221
                printf("SETUP_EXCEPT " UINT_FMT, ip + unum - ip_start);
222
223
                break;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

351
352
353
354
355
356
357
358
359
360
            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;

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

#endif // MICROPY_SHOW_BC