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

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

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

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

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

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

76
            case MP_BC_LOAD_FAST_0:
77
78
79
                printf("LOAD_FAST_0");
                break;

80
            case MP_BC_LOAD_FAST_1:
81
82
83
                printf("LOAD_FAST_1");
                break;

84
            case MP_BC_LOAD_FAST_2:
85
86
87
                printf("LOAD_FAST_2");
                break;

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

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

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

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

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

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

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

122
            case MP_BC_STORE_FAST_0:
123
124
125
                printf("STORE_FAST_0");
                break;

126
            case MP_BC_STORE_FAST_1:
127
128
129
                printf("STORE_FAST_1");
                break;

130
            case MP_BC_STORE_FAST_2:
131
132
133
                printf("STORE_FAST_2");
                break;

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

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

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

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

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

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

163
            case MP_BC_DUP_TOP:
164
                printf("DUP_TOP");
165
166
                break;

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

171
            case MP_BC_POP_TOP:
172
173
174
175
                printf("POP_TOP");
                break;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

266
            case MP_BC_COMPARE_OP:
267
                unum = *ip++;
268
                printf("COMPARE_OP " UINT_FMT, unum);
269
270
                break;

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

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

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

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

291
                /*
292
            case MP_BC_STORE_MAP:
293
294
295
296
                sp += 2;
                rt_store_map(sp[0], sp[-2], sp[-1]);
                break;

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

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

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

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

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

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

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

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

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

344
            case MP_BC_YIELD_VALUE:
345
346
                printf("YIELD_VALUE");
                break;
347

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

358
359
360
361
362
363
364
365
            default:
                printf("code %p, byte code 0x%02x not implemented\n", ip, op);
                assert(0);
                return;
        }
        printf("\n");
    }
}