showbc.c 10 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;

Damien's avatar
Damien committed
138
                /*
139
            case MP_BC_DUP_TOP:
140
141
142
                obj1 = *sp;
                PUSH(obj1);
                break;
Damien's avatar
Damien committed
143
                */
144

145
            case MP_BC_DUP_TOP_TWO:
Damien's avatar
Damien committed
146
                printf("DUP_TOP_TWO");
147
148
                break;

149
            case MP_BC_POP_TOP:
150
151
152
153
                printf("POP_TOP");
                break;

                /*
154
            case MP_BC_ROT_TWO:
155
156
157
158
                obj1 = sp[0];
                sp[0] = sp[1];
                sp[1] = obj1;
                break;
Damien's avatar
Damien committed
159
                */
160

161
            case MP_BC_ROT_THREE:
Damien's avatar
Damien committed
162
                printf("ROT_THREE");
163
164
                break;

165
            case MP_BC_JUMP:
166
167
168
169
                DECODE_SLABEL;
                printf("JUMP %lu", ip + unum - ip_start);
                break;

170
            case MP_BC_POP_JUMP_IF_TRUE:
171
172
173
174
                DECODE_SLABEL;
                printf("POP_JUMP_IF_TRUE %lu", ip + unum - ip_start);
                break;

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

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

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

199
            case MP_BC_SETUP_EXCEPT:
200
201
202
203
204
                DECODE_ULABEL; // except labels are always forward
                *++exc_sp = (machine_uint_t)ip + unum;
                *++exc_sp = (machine_uint_t)sp;
                break;

205
            case MP_BC_END_FINALLY:
206
207
208
209
210
211
212
                // not implemented
                // 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
                assert(0);
                break;
Damien's avatar
Damien committed
213
                */
214

215
            case MP_BC_GET_ITER:
Damien's avatar
Damien committed
216
                printf("GET_ITER");
217
218
                break;

219
            case MP_BC_FOR_ITER:
220
                DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damien's avatar
Damien committed
221
                printf("FOR_ITER %lu", ip + unum - ip_start);
222
223
                break;

Damien's avatar
Damien committed
224
                /*
225
            case MP_BC_POP_BLOCK:
226
227
228
229
                // pops block and restores the stack
                assert(0);
                break;

230
            case MP_BC_POP_EXCEPT:
231
232
233
                // TODO need to work out how blocks work etc
                // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
                assert(exc_sp >= &exc_stack[0]);
234
                //sp = (mp_obj_t*)(*exc_sp--);
235
236
237
238
239
                //exc_sp--; // discard ip
                exc_sp -= 2;
                //sp += 3; // pop 3 exception values
                break;

240
            case MP_BC_UNARY_OP:
241
242
243
244
245
                unum = *ip++;
                *sp = rt_unary_op(unum, *sp);
                break;
                */

246
            case MP_BC_BINARY_OP:
247
248
249
250
                unum = *ip++;
                printf("BINARY_OP %lu", unum);
                break;

251
            case MP_BC_COMPARE_OP:
252
253
254
255
                unum = *ip++;
                printf("COMPARE_OP %lu", unum);
                break;

256
            case MP_BC_BUILD_TUPLE:
257
                DECODE_UINT;
Damien's avatar
Damien committed
258
                printf("BUILD_TUPLE %lu", unum);
259
260
                break;

261
            case MP_BC_BUILD_LIST:
262
                DECODE_UINT;
Damien's avatar
Damien committed
263
                printf("BUILD_LIST %lu", unum);
264
265
                break;

Damien's avatar
Damien committed
266
                /*
267
            case MP_BC_LIST_APPEND:
268
269
270
271
272
273
                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;

274
            case MP_BC_BUILD_MAP:
275
276
277
278
                DECODE_UINT;
                PUSH(rt_build_map(unum));
                break;

279
            case MP_BC_STORE_MAP:
280
281
282
283
                sp += 2;
                rt_store_map(sp[0], sp[-2], sp[-1]);
                break;

284
            case MP_BC_MAP_ADD:
285
286
287
288
289
290
                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;

291
            case MP_BC_BUILD_SET:
292
293
294
295
296
297
                DECODE_UINT;
                obj1 = rt_build_set(unum, sp);
                sp += unum - 1;
                *sp = obj1;
                break;

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

306
            case MP_BC_UNPACK_SEQUENCE:
Damien's avatar
Damien committed
307
308
309
310
                DECODE_UINT;
                printf("UNPACK_SEQUENCE %lu", unum);
                break;

311
            case MP_BC_MAKE_FUNCTION:
312
313
314
315
                DECODE_UINT;
                printf("MAKE_FUNCTION %lu", unum);
                break;

316
            case MP_BC_CALL_FUNCTION:
317
                DECODE_UINT;
Damien's avatar
Damien committed
318
                printf("CALL_FUNCTION n=%lu nkw=%lu", unum & 0xff, (unum >> 8) & 0xff);
319
320
                break;

321
            case MP_BC_CALL_METHOD:
322
                DECODE_UINT;
Damien's avatar
Damien committed
323
                printf("CALL_METHOD n=%lu nkw=%lu", unum & 0xff, (unum >> 8) & 0xff);
324
325
                break;

326
            case MP_BC_RETURN_VALUE:
327
328
329
330
                printf("RETURN_VALUE");
                break;

                /*
331
            case MP_BC_YIELD_VALUE:
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
                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");
    }
}