showbc.c 10.3 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
                DECODE_QSTR;
                printf("STORE_NAME %s", qstr_str(qstr));
                break;

145
            case MP_BC_STORE_GLOBAL:
146
                DECODE_QSTR;
147
                printf("STORE_GLOBAL %s", qstr_str(qstr));
148
149
                break;

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

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

159
            case MP_BC_DUP_TOP:
160
                printf("DUP_TOP");
161
162
                break;

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

167
            case MP_BC_POP_TOP:
168
169
170
171
                printf("POP_TOP");
                break;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

262
            case MP_BC_COMPARE_OP:
263
                unum = *ip++;
264
                printf("COMPARE_OP " UINT_FMT, unum);
265
266
                break;

267
            case MP_BC_BUILD_TUPLE:
268
                DECODE_UINT;
269
                printf("BUILD_TUPLE " UINT_FMT, unum);
270
271
                break;

272
            case MP_BC_BUILD_LIST:
273
                DECODE_UINT;
274
                printf("BUILD_LIST " UINT_FMT, unum);
275
276
                break;

277
            case MP_BC_LIST_APPEND:
278
                DECODE_UINT;
279
                printf("LIST_APPEND " UINT_FMT, unum);
280
281
                break;

282
            case MP_BC_BUILD_MAP:
283
                DECODE_UINT;
284
                printf("BUILD_MAP " UINT_FMT, unum);
285
286
                break;

287
                /*
288
            case MP_BC_STORE_MAP:
289
290
291
292
                sp += 2;
                rt_store_map(sp[0], sp[-2], sp[-1]);
                break;

293
            case MP_BC_MAP_ADD:
294
295
296
297
298
                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;
299
                */
300

301
            case MP_BC_BUILD_SET:
302
                DECODE_UINT;
303
                printf("BUILD_SET " UINT_FMT, unum);
304
305
                break;

306
            case MP_BC_SET_ADD:
307
                DECODE_UINT;
308
                printf("SET_ADD " UINT_FMT, unum);
309
310
                break;

311
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
312
                DECODE_UINT;
313
                printf("UNPACK_SEQUENCE " UINT_FMT, unum);
Damien's avatar
Damien committed
314
315
                break;

316
            case MP_BC_MAKE_FUNCTION:
317
                DECODE_UINT;
318
                printf("MAKE_FUNCTION " UINT_FMT, unum);
319
320
                break;

Damien George's avatar
Damien George committed
321
322
323
324
325
            case MP_BC_MAKE_CLOSURE:
                DECODE_UINT;
                printf("MAKE_CLOSURE " UINT_FMT, unum);
                break;

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

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

336
            case MP_BC_RETURN_VALUE:
337
338
339
                printf("RETURN_VALUE");
                break;

340
            case MP_BC_YIELD_VALUE:
341
342
                printf("YIELD_VALUE");
                break;
343

344
345
346
347
348
349
350
351
352
353
            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;

354
355
356
357
358
359
360
361
            default:
                printf("code %p, byte code 0x%02x not implemented\n", ip, op);
                assert(0);
                return;
        }
        printf("\n");
    }
}