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
                /*
72
            case MP_BC_LOAD_CONST_DEC:
73
74
75
                DECODE_QSTR;
                PUSH(rt_load_const_dec(qstr));
                break;
Damien's avatar
Damien committed
76
                */
77

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

371
#endif // MICROPY_DEBUG_PRINTERS