showbc.c 12.1 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
61
62
63
64
65
66
67
68
69
            case MP_BC_LOAD_CONST_SMALL_INT: {
                int num = 0;
                if ((ip[0] & 0x40) != 0) {
                    // Number is negative
                    num--;
                }
                do {
                    num = (num << 7) | (*ip & 0x7f);
                } while ((*ip++ & 0x80) != 0);
                printf("LOAD_CONST_SMALL_INT %d", num);
70
                break;
71
            }
72

73
74
75
76
77
            case MP_BC_LOAD_CONST_INT:
                DECODE_QSTR;
                printf("LOAD_CONST_INT %s", qstr_str(qstr));
                break;

78
            case MP_BC_LOAD_CONST_DEC:
79
                DECODE_QSTR;
80
                printf("LOAD_CONST_DEC %s", qstr_str(qstr));
81
82
                break;

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

88
            case MP_BC_LOAD_CONST_STRING:
89
                DECODE_QSTR;
Damien's avatar
Damien committed
90
                printf("LOAD_CONST_STRING %s", qstr_str(qstr));
91
92
                break;

93
            case MP_BC_LOAD_FAST_0:
94
95
96
                printf("LOAD_FAST_0");
                break;

97
            case MP_BC_LOAD_FAST_1:
98
99
100
                printf("LOAD_FAST_1");
                break;

101
            case MP_BC_LOAD_FAST_2:
102
103
104
                printf("LOAD_FAST_2");
                break;

105
            case MP_BC_LOAD_FAST_N:
106
                DECODE_UINT;
107
                printf("LOAD_FAST_N " UINT_FMT, unum);
108
109
                break;

Damien George's avatar
Damien George committed
110
111
112
113
114
            case MP_BC_LOAD_DEREF:
                DECODE_UINT;
                printf("LOAD_DEREF " UINT_FMT, unum);
                break;

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

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

125
            case MP_BC_LOAD_ATTR:
126
                DECODE_QSTR;
Damien's avatar
Damien committed
127
                printf("LOAD_ATTR %s", qstr_str(qstr));
128
129
                break;

130
            case MP_BC_LOAD_METHOD:
131
132
133
134
                DECODE_QSTR;
                printf("LOAD_METHOD %s", qstr_str(qstr));
                break;

135
            case MP_BC_LOAD_BUILD_CLASS:
Damien's avatar
Damien committed
136
                printf("LOAD_BUILD_CLASS");
137
138
                break;

139
            case MP_BC_STORE_FAST_0:
140
141
142
                printf("STORE_FAST_0");
                break;

143
            case MP_BC_STORE_FAST_1:
144
145
146
                printf("STORE_FAST_1");
                break;

147
            case MP_BC_STORE_FAST_2:
148
149
150
                printf("STORE_FAST_2");
                break;

151
            case MP_BC_STORE_FAST_N:
152
                DECODE_UINT;
153
                printf("STORE_FAST_N " UINT_FMT, unum);
154
155
                break;

Damien George's avatar
Damien George committed
156
157
158
159
160
            case MP_BC_STORE_DEREF:
                DECODE_UINT;
                printf("STORE_DEREF " UINT_FMT, unum);
                break;

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

166
            case MP_BC_STORE_GLOBAL:
167
                DECODE_QSTR;
168
                printf("STORE_GLOBAL %s", qstr_str(qstr));
169
170
                break;

171
            case MP_BC_STORE_ATTR:
172
                DECODE_QSTR;
Damien's avatar
Damien committed
173
                printf("STORE_ATTR %s", qstr_str(qstr));
174
175
                break;

176
            case MP_BC_STORE_SUBSCR:
Damien's avatar
Damien committed
177
                printf("STORE_SUBSCR");
178
179
                break;

Damien George's avatar
Damien George committed
180
181
182
183
184
185
186
187
188
189
            case MP_BC_DELETE_FAST_N:
                DECODE_UINT;
                printf("DELETE_FAST_N " UINT_FMT, unum);
                break;

            case MP_BC_DELETE_NAME:
                DECODE_QSTR;
                printf("DELETE_NAME %s", qstr_str(qstr));
                break;

190
            case MP_BC_DUP_TOP:
191
                printf("DUP_TOP");
192
193
                break;

194
            case MP_BC_DUP_TOP_TWO:
Damien's avatar
Damien committed
195
                printf("DUP_TOP_TWO");
196
197
                break;

198
            case MP_BC_POP_TOP:
199
200
201
                printf("POP_TOP");
                break;

202
            case MP_BC_ROT_TWO:
203
                printf("ROT_TWO");
204
205
                break;

206
            case MP_BC_ROT_THREE:
Damien's avatar
Damien committed
207
                printf("ROT_THREE");
208
209
                break;

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

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

220
            case MP_BC_POP_JUMP_IF_FALSE:
221
                DECODE_SLABEL;
222
                printf("POP_JUMP_IF_FALSE " UINT_FMT, ip + unum - ip_start);
223
224
                break;

225
            case MP_BC_JUMP_IF_TRUE_OR_POP:
226
                DECODE_SLABEL;
227
                printf("JUMP_IF_TRUE_OR_POP " UINT_FMT, ip + unum - ip_start);
228
229
                break;

230
            case MP_BC_JUMP_IF_FALSE_OR_POP:
231
                DECODE_SLABEL;
232
                printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, ip + unum - ip_start);
233
234
                break;

235
236
237
238
239
            case MP_BC_SETUP_LOOP:
                DECODE_ULABEL; // loop labels are always forward
                printf("SETUP_LOOP " UINT_FMT, ip + unum - ip_start);
                break;

240
241
242
243
            case MP_BC_UNWIND_JUMP:
                DECODE_SLABEL;
                printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - ip_start, *ip);
                ip += 1;
244
245
                break;

246
            case MP_BC_SETUP_EXCEPT:
247
                DECODE_ULABEL; // except labels are always forward
248
                printf("SETUP_EXCEPT " UINT_FMT, ip + unum - ip_start);
249
250
                break;

251
252
253
254
255
            case MP_BC_SETUP_FINALLY:
                DECODE_ULABEL; // except labels are always forward
                printf("SETUP_FINALLY " UINT_FMT, ip + unum - ip_start);
                break;

256
            case MP_BC_END_FINALLY:
257
258
259
260
                // 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
261
                printf("END_FINALLY");
262
263
                break;

264
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
265
                printf("GET_ITER");
266
267
                break;

268
            case MP_BC_FOR_ITER:
269
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
270
                printf("FOR_ITER " UINT_FMT, ip + unum - ip_start);
271
272
                break;

273
            case MP_BC_POP_BLOCK:
274
                // pops block and restores the stack
275
                printf("POP_BLOCK");
276
277
                break;

278
            case MP_BC_POP_EXCEPT:
279
                // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
280
                printf("POP_EXCEPT");
281
282
                break;

283
284
285
286
            case MP_BC_NOT:
                printf("NOT");
                break;

287
            case MP_BC_UNARY_OP:
288
                unum = *ip++;
289
                printf("UNARY_OP " UINT_FMT, unum);
290
291
                break;

292
            case MP_BC_BINARY_OP:
293
                unum = *ip++;
294
                printf("BINARY_OP " UINT_FMT, unum);
295
296
                break;

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

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

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

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

317
            case MP_BC_STORE_MAP:
318
                printf("STORE_MAP");
319
320
                break;

321
                /*
322
            case MP_BC_MAP_ADD:
323
324
325
326
327
                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;
328
                */
329

330
            case MP_BC_BUILD_SET:
331
                DECODE_UINT;
332
                printf("BUILD_SET " UINT_FMT, unum);
333
334
                break;

335
            case MP_BC_SET_ADD:
336
                DECODE_UINT;
337
                printf("SET_ADD " UINT_FMT, unum);
338
339
                break;

340
341
342
343
344
345
346
#if MICROPY_ENABLE_SLICE
            case MP_BC_BUILD_SLICE:
                DECODE_UINT;
                printf("BUILD_SLICE " UINT_FMT, unum);
                break;
#endif

347
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
348
                DECODE_UINT;
349
                printf("UNPACK_SEQUENCE " UINT_FMT, unum);
Damien's avatar
Damien committed
350
351
                break;

352
            case MP_BC_MAKE_FUNCTION:
353
                DECODE_UINT;
354
                printf("MAKE_FUNCTION " UINT_FMT, unum);
355
356
                break;

357
358
359
360
361
            case MP_BC_MAKE_FUNCTION_DEFARGS:
                DECODE_UINT;
                printf("MAKE_FUNCTION_DEFARGS " UINT_FMT, unum);
                break;

Damien George's avatar
Damien George committed
362
363
364
365
366
            case MP_BC_MAKE_CLOSURE:
                DECODE_UINT;
                printf("MAKE_CLOSURE " UINT_FMT, unum);
                break;

367
            case MP_BC_CALL_FUNCTION:
368
                DECODE_UINT;
369
                printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
370
371
                break;

372
            case MP_BC_CALL_METHOD:
373
                DECODE_UINT;
374
                printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
375
376
                break;

377
            case MP_BC_RETURN_VALUE:
378
379
380
                printf("RETURN_VALUE");
                break;

381
382
383
384
385
            case MP_BC_RAISE_VARARGS:
                unum = *ip++;
                printf("RAISE_VARARGS " UINT_FMT, unum);
                break;

386
            case MP_BC_YIELD_VALUE:
387
388
                printf("YIELD_VALUE");
                break;
389

390
391
            case MP_BC_IMPORT_NAME:
                DECODE_QSTR;
392
                printf("IMPORT_NAME %s", qstr_str(qstr));
393
394
395
396
                break;

            case MP_BC_IMPORT_FROM:
                DECODE_QSTR;
397
                printf("IMPORT_FROM %s", qstr_str(qstr));
398
399
                break;

400
401
402
403
            case MP_BC_IMPORT_STAR:
                printf("IMPORT_STAR");
                break;

404
405
406
407
408
409
410
411
            default:
                printf("code %p, byte code 0x%02x not implemented\n", ip, op);
                assert(0);
                return;
        }
        printf("\n");
    }
}
412

413
#endif // MICROPY_DEBUG_PRINTERS