showbc.c 10.5 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
61
                unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000;
                ip += 3;
                printf("LOAD_CONST_SMALL_INT %d", (int)unum);
                break;

                /*
62
            case MP_BC_LOAD_CONST_DEC:
63
64
65
                DECODE_QSTR;
                PUSH(rt_load_const_dec(qstr));
                break;
Damien's avatar
Damien committed
66
                */
67

68
            case MP_BC_LOAD_CONST_ID:
69
                DECODE_QSTR;
Damien's avatar
Damien committed
70
                printf("LOAD_CONST_ID %s", qstr_str(qstr));
71
72
                break;

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

78
            case MP_BC_LOAD_FAST_0:
79
80
81
                printf("LOAD_FAST_0");
                break;

82
            case MP_BC_LOAD_FAST_1:
83
84
85
                printf("LOAD_FAST_1");
                break;

86
            case MP_BC_LOAD_FAST_2:
87
88
89
                printf("LOAD_FAST_2");
                break;

90
            case MP_BC_LOAD_FAST_N:
91
                DECODE_UINT;
92
                printf("LOAD_FAST_N " UINT_FMT, unum);
93
94
                break;

Damien George's avatar
Damien George committed
95
96
97
98
99
            case MP_BC_LOAD_DEREF:
                DECODE_UINT;
                printf("LOAD_DEREF " UINT_FMT, unum);
                break;

100
            case MP_BC_LOAD_NAME:
101
102
103
104
                DECODE_QSTR;
                printf("LOAD_NAME %s", qstr_str(qstr));
                break;

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

110
            case MP_BC_LOAD_ATTR:
111
                DECODE_QSTR;
Damien's avatar
Damien committed
112
                printf("LOAD_ATTR %s", qstr_str(qstr));
113
114
                break;

115
            case MP_BC_LOAD_METHOD:
116
117
118
119
                DECODE_QSTR;
                printf("LOAD_METHOD %s", qstr_str(qstr));
                break;

120
            case MP_BC_LOAD_BUILD_CLASS:
Damien's avatar
Damien committed
121
                printf("LOAD_BUILD_CLASS");
122
123
                break;

124
            case MP_BC_STORE_FAST_0:
125
126
127
                printf("STORE_FAST_0");
                break;

128
            case MP_BC_STORE_FAST_1:
129
130
131
                printf("STORE_FAST_1");
                break;

132
            case MP_BC_STORE_FAST_2:
133
134
135
                printf("STORE_FAST_2");
                break;

136
            case MP_BC_STORE_FAST_N:
137
                DECODE_UINT;
138
                printf("STORE_FAST_N " UINT_FMT, unum);
139
140
                break;

Damien George's avatar
Damien George committed
141
142
143
144
145
            case MP_BC_STORE_DEREF:
                DECODE_UINT;
                printf("STORE_DEREF " UINT_FMT, unum);
                break;

146
            case MP_BC_STORE_NAME:
147
148
149
150
                DECODE_QSTR;
                printf("STORE_NAME %s", qstr_str(qstr));
                break;

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

156
            case MP_BC_STORE_ATTR:
157
                DECODE_QSTR;
Damien's avatar
Damien committed
158
                printf("STORE_ATTR %s", qstr_str(qstr));
159
160
                break;

161
            case MP_BC_STORE_SUBSCR:
Damien's avatar
Damien committed
162
                printf("STORE_SUBSCR");
163
164
                break;

165
            case MP_BC_DUP_TOP:
166
                printf("DUP_TOP");
167
168
                break;

169
            case MP_BC_DUP_TOP_TWO:
Damien's avatar
Damien committed
170
                printf("DUP_TOP_TWO");
171
172
                break;

173
            case MP_BC_POP_TOP:
174
175
176
177
                printf("POP_TOP");
                break;

                /*
178
            case MP_BC_ROT_TWO:
179
180
181
182
                obj1 = sp[0];
                sp[0] = sp[1];
                sp[1] = obj1;
                break;
Damien's avatar
Damien committed
183
                */
184

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

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

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

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

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

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

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

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

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

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

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

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

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

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

268
            case MP_BC_COMPARE_OP:
269
                unum = *ip++;
270
                printf("COMPARE_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
                /*
294
            case MP_BC_STORE_MAP:
295
296
297
298
                sp += 2;
                rt_store_map(sp[0], sp[-2], sp[-1]);
                break;

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

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

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

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

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

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

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

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

342
            case MP_BC_RETURN_VALUE:
343
344
345
                printf("RETURN_VALUE");
                break;

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

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

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_SHOW_BC