showbc.c 10.4 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
13
14
15

#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)

16
void mp_show_byte_code(const byte *ip, int len) {
17
    const byte *ip_start = ip;
Damien George's avatar
Damien George committed
18
19
20
21
22
23
24
25
26
27
28
29
30

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

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

41
            case MP_BC_LOAD_CONST_NONE:
42
43
44
                printf("LOAD_CONST_NONE");
                break;

45
            case MP_BC_LOAD_CONST_TRUE:
46
47
48
                printf("LOAD_CONST_TRUE");
                break;

49
            case MP_BC_LOAD_CONST_SMALL_INT:
50
51
52
53
54
55
                unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000;
                ip += 3;
                printf("LOAD_CONST_SMALL_INT %d", (int)unum);
                break;

                /*
56
            case MP_BC_LOAD_CONST_DEC:
57
58
59
                DECODE_QSTR;
                PUSH(rt_load_const_dec(qstr));
                break;
Damien's avatar
Damien committed
60
                */
61

62
            case MP_BC_LOAD_CONST_ID:
63
                DECODE_QSTR;
Damien's avatar
Damien committed
64
                printf("LOAD_CONST_ID %s", qstr_str(qstr));
65
66
                break;

67
            case MP_BC_LOAD_CONST_STRING:
68
                DECODE_QSTR;
Damien's avatar
Damien committed
69
                printf("LOAD_CONST_STRING %s", qstr_str(qstr));
70
71
                break;

72
            case MP_BC_LOAD_FAST_0:
73
74
75
                printf("LOAD_FAST_0");
                break;

76
            case MP_BC_LOAD_FAST_1:
77
78
79
                printf("LOAD_FAST_1");
                break;

80
            case MP_BC_LOAD_FAST_2:
81
82
83
                printf("LOAD_FAST_2");
                break;

84
            case MP_BC_LOAD_FAST_N:
85
                DECODE_UINT;
86
                printf("LOAD_FAST_N " UINT_FMT, unum);
87
88
                break;

Damien George's avatar
Damien George committed
89
90
91
92
93
            case MP_BC_LOAD_DEREF:
                DECODE_UINT;
                printf("LOAD_DEREF " UINT_FMT, unum);
                break;

94
            case MP_BC_LOAD_NAME:
95
96
97
98
                DECODE_QSTR;
                printf("LOAD_NAME %s", qstr_str(qstr));
                break;

99
            case MP_BC_LOAD_GLOBAL:
100
101
102
103
                DECODE_QSTR;
                printf("LOAD_GLOBAL %s", qstr_str(qstr));
                break;

104
            case MP_BC_LOAD_ATTR:
105
                DECODE_QSTR;
Damien's avatar
Damien committed
106
                printf("LOAD_ATTR %s", qstr_str(qstr));
107
108
                break;

109
            case MP_BC_LOAD_METHOD:
110
111
112
113
                DECODE_QSTR;
                printf("LOAD_METHOD %s", qstr_str(qstr));
                break;

114
            case MP_BC_LOAD_BUILD_CLASS:
Damien's avatar
Damien committed
115
                printf("LOAD_BUILD_CLASS");
116
117
                break;

118
            case MP_BC_STORE_FAST_0:
119
120
121
                printf("STORE_FAST_0");
                break;

122
            case MP_BC_STORE_FAST_1:
123
124
125
                printf("STORE_FAST_1");
                break;

126
            case MP_BC_STORE_FAST_2:
127
128
129
                printf("STORE_FAST_2");
                break;

130
            case MP_BC_STORE_FAST_N:
131
                DECODE_UINT;
132
                printf("STORE_FAST_N " UINT_FMT, unum);
133
134
                break;

Damien George's avatar
Damien George committed
135
136
137
138
139
            case MP_BC_STORE_DEREF:
                DECODE_UINT;
                printf("STORE_DEREF " UINT_FMT, unum);
                break;

140
            case MP_BC_STORE_NAME:
141
142
143
144
145
                DECODE_QSTR;
                printf("STORE_NAME %s", qstr_str(qstr));
                break;

                /*
146
            case MP_BC_STORE_GLOBAL:
147
148
149
                DECODE_QSTR;
                rt_store_global(qstr, POP());
                break;
Damien's avatar
Damien committed
150
                */
151

152
            case MP_BC_STORE_ATTR:
153
                DECODE_QSTR;
Damien's avatar
Damien committed
154
                printf("STORE_ATTR %s", qstr_str(qstr));
155
156
                break;

157
            case MP_BC_STORE_SUBSCR:
Damien's avatar
Damien committed
158
                printf("STORE_SUBSCR");
159
160
                break;

161
            case MP_BC_DUP_TOP:
162
                printf("DUP_TOP");
163
164
                break;

165
            case MP_BC_DUP_TOP_TWO:
Damien's avatar
Damien committed
166
                printf("DUP_TOP_TWO");
167
168
                break;

169
            case MP_BC_POP_TOP:
170
171
172
173
                printf("POP_TOP");
                break;

                /*
174
            case MP_BC_ROT_TWO:
175
176
177
178
                obj1 = sp[0];
                sp[0] = sp[1];
                sp[1] = obj1;
                break;
Damien's avatar
Damien committed
179
                */
180

181
            case MP_BC_ROT_THREE:
Damien's avatar
Damien committed
182
                printf("ROT_THREE");
183
184
                break;

185
            case MP_BC_JUMP:
186
                DECODE_SLABEL;
187
                printf("JUMP " UINT_FMT, ip + unum - ip_start);
188
189
                break;

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

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

Damien's avatar
Damien committed
200
                /*
201
            case MP_BC_JUMP_IF_TRUE_OR_POP:
202
203
204
205
206
207
208
209
                DECODE_SLABEL;
                if (rt_is_true(*sp)) {
                    ip += unum;
                } else {
                    sp++;
                }
                break;

210
            case MP_BC_JUMP_IF_FALSE_OR_POP:
211
212
213
214
215
216
217
                DECODE_SLABEL;
                if (rt_is_true(*sp)) {
                    sp++;
                } else {
                    ip += unum;
                }
                break;
218
                */
219

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_COMPARE_OP:
265
                unum = *ip++;
266
                printf("COMPARE_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;

Damien's avatar
Damien committed
279
                /*
280
            case MP_BC_LIST_APPEND:
281
282
283
284
285
                DECODE_UINT;
                // I think it's guaranteed by the compiler that sp[unum] is a list
                rt_list_append(sp[unum], sp[0]);
                sp++;
                break;
286
                */
287

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
346
                printf("RETURN_VALUE");
                break;

                /*
347
            case MP_BC_YIELD_VALUE:
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
                nlr_pop();
                *ip_in_out = ip;
                fastn[0] = fast0;
                fastn[1] = fast1;
                fastn[2] = fast2;
                *sp_in_out = sp;
                return true;
                */

            default:
                printf("code %p, byte code 0x%02x not implemented\n", ip, op);
                assert(0);
                return;
        }
        printf("\n");
    }
}