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
25
26
27
28
29
30
31
32

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

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

43
            case MP_BC_LOAD_CONST_NONE:
44
45
46
                printf("LOAD_CONST_NONE");
                break;

47
            case MP_BC_LOAD_CONST_TRUE:
48
49
50
                printf("LOAD_CONST_TRUE");
                break;

Damien George's avatar
Damien George committed
51
52
53
54
            case MP_BC_LOAD_CONST_ELLIPSIS:
                printf("LOAD_CONST_ELLIPSIS");
                break;

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

61
62
63
64
65
            case MP_BC_LOAD_CONST_INT:
                DECODE_QSTR;
                printf("LOAD_CONST_INT %s", qstr_str(qstr));
                break;

66
                /*
67
            case MP_BC_LOAD_CONST_DEC:
68
69
70
                DECODE_QSTR;
                PUSH(rt_load_const_dec(qstr));
                break;
Damien's avatar
Damien committed
71
                */
72

73
            case MP_BC_LOAD_CONST_ID:
74
                DECODE_QSTR;
Damien's avatar
Damien committed
75
                printf("LOAD_CONST_ID %s", qstr_str(qstr));
76
77
                break;

78
            case MP_BC_LOAD_CONST_STRING:
79
                DECODE_QSTR;
Damien's avatar
Damien committed
80
                printf("LOAD_CONST_STRING %s", qstr_str(qstr));
81
82
                break;

83
            case MP_BC_LOAD_FAST_0:
84
85
86
                printf("LOAD_FAST_0");
                break;

87
            case MP_BC_LOAD_FAST_1:
88
89
90
                printf("LOAD_FAST_1");
                break;

91
            case MP_BC_LOAD_FAST_2:
92
93
94
                printf("LOAD_FAST_2");
                break;

95
            case MP_BC_LOAD_FAST_N:
96
                DECODE_UINT;
97
                printf("LOAD_FAST_N " UINT_FMT, unum);
98
99
                break;

Damien George's avatar
Damien George committed
100
101
102
103
104
            case MP_BC_LOAD_DEREF:
                DECODE_UINT;
                printf("LOAD_DEREF " UINT_FMT, unum);
                break;

105
            case MP_BC_LOAD_NAME:
106
107
108
109
                DECODE_QSTR;
                printf("LOAD_NAME %s", qstr_str(qstr));
                break;

110
            case MP_BC_LOAD_GLOBAL:
111
112
113
114
                DECODE_QSTR;
                printf("LOAD_GLOBAL %s", qstr_str(qstr));
                break;

115
            case MP_BC_LOAD_ATTR:
116
                DECODE_QSTR;
Damien's avatar
Damien committed
117
                printf("LOAD_ATTR %s", qstr_str(qstr));
118
119
                break;

120
            case MP_BC_LOAD_METHOD:
121
122
123
124
                DECODE_QSTR;
                printf("LOAD_METHOD %s", qstr_str(qstr));
                break;

125
            case MP_BC_LOAD_BUILD_CLASS:
Damien's avatar
Damien committed
126
                printf("LOAD_BUILD_CLASS");
127
128
                break;

129
            case MP_BC_STORE_FAST_0:
130
131
132
                printf("STORE_FAST_0");
                break;

133
            case MP_BC_STORE_FAST_1:
134
135
136
                printf("STORE_FAST_1");
                break;

137
            case MP_BC_STORE_FAST_2:
138
139
140
                printf("STORE_FAST_2");
                break;

141
            case MP_BC_STORE_FAST_N:
142
                DECODE_UINT;
143
                printf("STORE_FAST_N " UINT_FMT, unum);
144
145
                break;

Damien George's avatar
Damien George committed
146
147
148
149
150
            case MP_BC_STORE_DEREF:
                DECODE_UINT;
                printf("STORE_DEREF " UINT_FMT, unum);
                break;

151
            case MP_BC_STORE_NAME:
152
153
154
155
                DECODE_QSTR;
                printf("STORE_NAME %s", qstr_str(qstr));
                break;

156
            case MP_BC_STORE_GLOBAL:
157
                DECODE_QSTR;
158
                printf("STORE_GLOBAL %s", qstr_str(qstr));
159
160
                break;

161
            case MP_BC_STORE_ATTR:
162
                DECODE_QSTR;
Damien's avatar
Damien committed
163
                printf("STORE_ATTR %s", qstr_str(qstr));
164
165
                break;

166
            case MP_BC_STORE_SUBSCR:
Damien's avatar
Damien committed
167
                printf("STORE_SUBSCR");
168
169
                break;

170
            case MP_BC_DUP_TOP:
171
                printf("DUP_TOP");
172
173
                break;

174
            case MP_BC_DUP_TOP_TWO:
Damien's avatar
Damien committed
175
                printf("DUP_TOP_TWO");
176
177
                break;

178
            case MP_BC_POP_TOP:
179
180
181
                printf("POP_TOP");
                break;

182
            case MP_BC_ROT_TWO:
183
                printf("ROT_TWO");
184
185
                break;

186
            case MP_BC_ROT_THREE:
Damien's avatar
Damien committed
187
                printf("ROT_THREE");
188
189
                break;

190
            case MP_BC_JUMP:
191
                DECODE_SLABEL;
192
                printf("JUMP " UINT_FMT, ip + unum - ip_start);
193
194
                break;

195
            case MP_BC_POP_JUMP_IF_TRUE:
196
                DECODE_SLABEL;
197
                printf("POP_JUMP_IF_TRUE " UINT_FMT, ip + unum - ip_start);
198
199
                break;

200
            case MP_BC_POP_JUMP_IF_FALSE:
201
                DECODE_SLABEL;
202
                printf("POP_JUMP_IF_FALSE " UINT_FMT, ip + unum - ip_start);
203
204
                break;

Damien's avatar
Damien committed
205
                /*
206
            case MP_BC_JUMP_IF_TRUE_OR_POP:
207
208
209
210
211
212
213
214
                DECODE_SLABEL;
                if (rt_is_true(*sp)) {
                    ip += unum;
                } else {
                    sp++;
                }
                break;

215
            case MP_BC_JUMP_IF_FALSE_OR_POP:
216
217
218
219
220
221
222
                DECODE_SLABEL;
                if (rt_is_true(*sp)) {
                    sp++;
                } else {
                    ip += unum;
                }
                break;
223
                */
224

225
            case MP_BC_SETUP_EXCEPT:
226
                DECODE_ULABEL; // except labels are always forward
227
                printf("SETUP_EXCEPT " UINT_FMT, ip + unum - ip_start);
228
229
                break;

230
            case MP_BC_END_FINALLY:
231
232
233
234
                // 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
235
                printf("END_FINALLY");
236
237
                break;

238
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
239
                printf("GET_ITER");
240
241
                break;

242
            case MP_BC_FOR_ITER:
243
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
244
                printf("FOR_ITER " UINT_FMT, ip + unum - ip_start);
245
246
                break;

247
            case MP_BC_POP_BLOCK:
248
                // pops block and restores the stack
249
                printf("POP_BLOCK");
250
251
                break;

252
            case MP_BC_POP_EXCEPT:
253
                // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
254
                printf("POP_EXCEPT");
255
256
                break;

257
                /*
258
            case MP_BC_UNARY_OP:
259
260
261
262
263
                unum = *ip++;
                *sp = rt_unary_op(unum, *sp);
                break;
                */

264
            case MP_BC_BINARY_OP:
265
                unum = *ip++;
266
                printf("BINARY_OP " UINT_FMT, unum);
267
268
                break;

269
            case MP_BC_BUILD_TUPLE:
270
                DECODE_UINT;
271
                printf("BUILD_TUPLE " UINT_FMT, unum);
272
273
                break;

274
            case MP_BC_BUILD_LIST:
275
                DECODE_UINT;
276
                printf("BUILD_LIST " UINT_FMT, unum);
277
278
                break;

279
            case MP_BC_LIST_APPEND:
280
                DECODE_UINT;
281
                printf("LIST_APPEND " UINT_FMT, unum);
282
283
                break;

284
            case MP_BC_BUILD_MAP:
285
                DECODE_UINT;
286
                printf("BUILD_MAP " UINT_FMT, unum);
287
288
                break;

289
            case MP_BC_STORE_MAP:
290
                printf("STORE_MAP");
291
292
                break;

293
                /*
294
            case MP_BC_MAP_ADD:
295
296
297
298
299
                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;
300
                */
301

302
            case MP_BC_BUILD_SET:
303
                DECODE_UINT;
304
                printf("BUILD_SET " UINT_FMT, unum);
305
306
                break;

307
            case MP_BC_SET_ADD:
308
                DECODE_UINT;
309
                printf("SET_ADD " UINT_FMT, unum);
310
311
                break;

312
313
314
315
316
317
318
#if MICROPY_ENABLE_SLICE
            case MP_BC_BUILD_SLICE:
                DECODE_UINT;
                printf("BUILD_SLICE " UINT_FMT, unum);
                break;
#endif

319
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
320
                DECODE_UINT;
321
                printf("UNPACK_SEQUENCE " UINT_FMT, unum);
Damien's avatar
Damien committed
322
323
                break;

324
            case MP_BC_MAKE_FUNCTION:
325
                DECODE_UINT;
326
                printf("MAKE_FUNCTION " UINT_FMT, unum);
327
328
                break;

Damien George's avatar
Damien George committed
329
330
331
332
333
            case MP_BC_MAKE_CLOSURE:
                DECODE_UINT;
                printf("MAKE_CLOSURE " UINT_FMT, unum);
                break;

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

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

344
            case MP_BC_RETURN_VALUE:
345
346
347
                printf("RETURN_VALUE");
                break;

348
349
350
351
352
            case MP_BC_RAISE_VARARGS:
                unum = *ip++;
                printf("RAISE_VARARGS " UINT_FMT, unum);
                break;

353
            case MP_BC_YIELD_VALUE:
354
355
                printf("YIELD_VALUE");
                break;
356

357
358
359
360
361
362
363
364
365
366
            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;

367
368
369
370
371
372
373
374
            default:
                printf("code %p, byte code 0x%02x not implemented\n", ip, op);
                assert(0);
                return;
        }
        printf("\n");
    }
}
375
376

#endif // MICROPY_SHOW_BC