showbc.c 9.74 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
18
19
20
21
22
23
    const byte *ip_start = ip;
    machine_uint_t unum;
    qstr qstr;
    while (ip - ip_start < len) {
        printf("%02u ", (uint)(ip - ip_start));
        int op = *ip++;
        switch (op) {
24
            case MP_BC_LOAD_CONST_FALSE:
25
26
27
                printf("LOAD_CONST_FALSE");
                break;

28
            case MP_BC_LOAD_CONST_NONE:
29
30
31
                printf("LOAD_CONST_NONE");
                break;

32
            case MP_BC_LOAD_CONST_TRUE:
33
34
35
                printf("LOAD_CONST_TRUE");
                break;

36
            case MP_BC_LOAD_CONST_SMALL_INT:
37
38
39
40
41
42
                unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000;
                ip += 3;
                printf("LOAD_CONST_SMALL_INT %d", (int)unum);
                break;

                /*
43
            case MP_BC_LOAD_CONST_DEC:
44
45
46
                DECODE_QSTR;
                PUSH(rt_load_const_dec(qstr));
                break;
Damien's avatar
Damien committed
47
                */
48

49
            case MP_BC_LOAD_CONST_ID:
50
                DECODE_QSTR;
Damien's avatar
Damien committed
51
                printf("LOAD_CONST_ID %s", qstr_str(qstr));
52
53
                break;

54
            case MP_BC_LOAD_CONST_STRING:
55
                DECODE_QSTR;
Damien's avatar
Damien committed
56
                printf("LOAD_CONST_STRING %s", qstr_str(qstr));
57
58
                break;

59
            case MP_BC_LOAD_FAST_0:
60
61
62
                printf("LOAD_FAST_0");
                break;

63
            case MP_BC_LOAD_FAST_1:
64
65
66
                printf("LOAD_FAST_1");
                break;

67
            case MP_BC_LOAD_FAST_2:
68
69
70
                printf("LOAD_FAST_2");
                break;

71
            case MP_BC_LOAD_FAST_N:
72
                DECODE_UINT;
Damien's avatar
Damien committed
73
                printf("LOAD_FAST_N %lu", unum);
74
75
                break;

76
            case MP_BC_LOAD_NAME:
77
78
79
80
                DECODE_QSTR;
                printf("LOAD_NAME %s", qstr_str(qstr));
                break;

81
            case MP_BC_LOAD_GLOBAL:
82
83
84
85
                DECODE_QSTR;
                printf("LOAD_GLOBAL %s", qstr_str(qstr));
                break;

86
            case MP_BC_LOAD_ATTR:
87
                DECODE_QSTR;
Damien's avatar
Damien committed
88
                printf("LOAD_ATTR %s", qstr_str(qstr));
89
90
                break;

91
            case MP_BC_LOAD_METHOD:
92
93
94
95
                DECODE_QSTR;
                printf("LOAD_METHOD %s", qstr_str(qstr));
                break;

96
            case MP_BC_LOAD_BUILD_CLASS:
Damien's avatar
Damien committed
97
                printf("LOAD_BUILD_CLASS");
98
99
                break;

100
            case MP_BC_STORE_FAST_0:
101
102
103
                printf("STORE_FAST_0");
                break;

104
            case MP_BC_STORE_FAST_1:
105
106
107
                printf("STORE_FAST_1");
                break;

108
            case MP_BC_STORE_FAST_2:
109
110
111
                printf("STORE_FAST_2");
                break;

112
            case MP_BC_STORE_FAST_N:
113
                DECODE_UINT;
Damien's avatar
Damien committed
114
                printf("STORE_FAST_N %lu", unum);
115
116
                break;

117
            case MP_BC_STORE_NAME:
118
119
120
121
122
                DECODE_QSTR;
                printf("STORE_NAME %s", qstr_str(qstr));
                break;

                /*
123
            case MP_BC_STORE_GLOBAL:
124
125
126
                DECODE_QSTR;
                rt_store_global(qstr, POP());
                break;
Damien's avatar
Damien committed
127
                */
128

129
            case MP_BC_STORE_ATTR:
130
                DECODE_QSTR;
Damien's avatar
Damien committed
131
                printf("STORE_ATTR %s", qstr_str(qstr));
132
133
                break;

134
            case MP_BC_STORE_SUBSCR:
Damien's avatar
Damien committed
135
                printf("STORE_SUBSCR");
136
137
                break;

138
            case MP_BC_DUP_TOP:
139
                printf("DUP_TOP");
140
141
                break;

142
            case MP_BC_DUP_TOP_TWO:
Damien's avatar
Damien committed
143
                printf("DUP_TOP_TWO");
144
145
                break;

146
            case MP_BC_POP_TOP:
147
148
149
150
                printf("POP_TOP");
                break;

                /*
151
            case MP_BC_ROT_TWO:
152
153
154
155
                obj1 = sp[0];
                sp[0] = sp[1];
                sp[1] = obj1;
                break;
Damien's avatar
Damien committed
156
                */
157

158
            case MP_BC_ROT_THREE:
Damien's avatar
Damien committed
159
                printf("ROT_THREE");
160
161
                break;

162
            case MP_BC_JUMP:
163
164
165
166
                DECODE_SLABEL;
                printf("JUMP %lu", ip + unum - ip_start);
                break;

167
            case MP_BC_POP_JUMP_IF_TRUE:
168
169
170
171
                DECODE_SLABEL;
                printf("POP_JUMP_IF_TRUE %lu", ip + unum - ip_start);
                break;

172
            case MP_BC_POP_JUMP_IF_FALSE:
173
                DECODE_SLABEL;
Damien's avatar
Damien committed
174
                printf("POP_JUMP_IF_FALSE %lu", ip + unum - ip_start);
175
176
                break;

Damien's avatar
Damien committed
177
                /*
178
            case MP_BC_JUMP_IF_TRUE_OR_POP:
179
180
181
182
183
184
185
186
                DECODE_SLABEL;
                if (rt_is_true(*sp)) {
                    ip += unum;
                } else {
                    sp++;
                }
                break;

187
            case MP_BC_JUMP_IF_FALSE_OR_POP:
188
189
190
191
192
193
194
                DECODE_SLABEL;
                if (rt_is_true(*sp)) {
                    sp++;
                } else {
                    ip += unum;
                }
                break;
195
                */
196

197
            case MP_BC_SETUP_EXCEPT:
198
                DECODE_ULABEL; // except labels are always forward
199
                printf("SETUP_EXCEPT %lu", ip + unum - ip_start);
200
201
                break;

202
            case MP_BC_END_FINALLY:
203
204
205
206
                // 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
207
                printf("END_FINALLY");
208
209
                break;

210
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
211
                printf("GET_ITER");
212
213
                break;

214
            case MP_BC_FOR_ITER:
215
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damien's avatar
Damien committed
216
                printf("FOR_ITER %lu", ip + unum - ip_start);
217
218
                break;

219
            case MP_BC_POP_BLOCK:
220
                // pops block and restores the stack
221
                printf("POP_BLOCK");
222
223
                break;

224
            case MP_BC_POP_EXCEPT:
225
                // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
226
                printf("POP_EXCEPT");
227
228
                break;

229
                /*
230
            case MP_BC_UNARY_OP:
231
232
233
234
235
                unum = *ip++;
                *sp = rt_unary_op(unum, *sp);
                break;
                */

236
            case MP_BC_BINARY_OP:
237
238
239
240
                unum = *ip++;
                printf("BINARY_OP %lu", unum);
                break;

241
            case MP_BC_COMPARE_OP:
242
243
244
245
                unum = *ip++;
                printf("COMPARE_OP %lu", unum);
                break;

246
            case MP_BC_BUILD_TUPLE:
247
                DECODE_UINT;
Damien's avatar
Damien committed
248
                printf("BUILD_TUPLE %lu", unum);
249
250
                break;

251
            case MP_BC_BUILD_LIST:
252
                DECODE_UINT;
Damien's avatar
Damien committed
253
                printf("BUILD_LIST %lu", unum);
254
255
                break;

Damien's avatar
Damien committed
256
                /*
257
            case MP_BC_LIST_APPEND:
258
259
260
261
262
                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;
263
                */
264

265
            case MP_BC_BUILD_MAP:
266
                DECODE_UINT;
267
                printf("BUILD_MAP %lu", unum);
268
269
                break;

270
                /*
271
            case MP_BC_STORE_MAP:
272
273
274
275
                sp += 2;
                rt_store_map(sp[0], sp[-2], sp[-1]);
                break;

276
            case MP_BC_MAP_ADD:
277
278
279
280
281
282
                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;

283
            case MP_BC_BUILD_SET:
284
285
286
287
288
289
                DECODE_UINT;
                obj1 = rt_build_set(unum, sp);
                sp += unum - 1;
                *sp = obj1;
                break;

290
            case MP_BC_SET_ADD:
291
292
293
294
295
296
297
                DECODE_UINT;
                // I think it's guaranteed by the compiler that sp[unum] is a set
                rt_store_set(sp[unum], sp[0]);
                sp++;
                break;
                */

298
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
299
300
301
302
                DECODE_UINT;
                printf("UNPACK_SEQUENCE %lu", unum);
                break;

303
            case MP_BC_MAKE_FUNCTION:
304
305
306
307
                DECODE_UINT;
                printf("MAKE_FUNCTION %lu", unum);
                break;

308
            case MP_BC_CALL_FUNCTION:
309
                DECODE_UINT;
Damien's avatar
Damien committed
310
                printf("CALL_FUNCTION n=%lu nkw=%lu", unum & 0xff, (unum >> 8) & 0xff);
311
312
                break;

313
            case MP_BC_CALL_METHOD:
314
                DECODE_UINT;
Damien's avatar
Damien committed
315
                printf("CALL_METHOD n=%lu nkw=%lu", unum & 0xff, (unum >> 8) & 0xff);
316
317
                break;

318
            case MP_BC_RETURN_VALUE:
319
320
321
322
                printf("RETURN_VALUE");
                break;

                /*
323
            case MP_BC_YIELD_VALUE:
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
                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");
    }
}