vm.c 61.3 KB
Newer Older
1
2
3
4
5
6
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
7
 * Copyright (c) 2014 Paul Sokolovsky
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

Damien's avatar
Damien committed
28
#include <stdio.h>
29
#include <string.h>
Damien's avatar
Damien committed
30
31
#include <assert.h>

32
#include "py/mpstate.h"
33
34
#include "py/nlr.h"
#include "py/emitglue.h"
35
#include "py/objtype.h"
36
37
38
#include "py/runtime.h"
#include "py/bc0.h"
#include "py/bc.h"
Damien's avatar
Damien committed
39

40
#if 0
41
#define TRACE(ip) printf("sp=%d ", (int)(sp - &code_state->state[0] + 1)); mp_bytecode_print2(ip, 1, code_state->fun_bc->const_table);
42
43
44
#else
#define TRACE(ip)
#endif
45

Paul Sokolovsky's avatar
Paul Sokolovsky committed
46
47
48
49
50
51
52
// Value stack grows up (this makes it incompatible with native C stack, but
// makes sure that arguments to functions are in natural order arg1..argN
// (Python semantics mandates left-to-right evaluation order, including for
// function arguments). Stack pointer is pre-incremented and points at the
// top element.
// Exception stack also grows up, top element is also pointed at.

53
// Exception stack unwind reasons (WHY_* in CPython-speak)
54
55
// TODO perhaps compress this to RETURN=0, JUMP>0, with number of unwinds
// left to do encoded in the JUMP number
56
57
typedef enum {
    UNWIND_RETURN = 1,
58
    UNWIND_JUMP,
59
60
} mp_unwind_reason_t;

61
62
#define DECODE_UINT \
    mp_uint_t unum = 0; \
63
64
    do { \
        unum = (unum << 7) + (*ip & 0x7f); \
65
    } while ((*ip++ & 0x80) != 0)
66
67
#define DECODE_ULABEL size_t ulab = (ip[0] | (ip[1] << 8)); ip += 2
#define DECODE_SLABEL size_t slab = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2
68
69
70
71
72
73
74
75

#if MICROPY_PERSISTENT_CODE

#define DECODE_QSTR \
    qstr qst = ip[0] | ip[1] << 8; \
    ip += 2;
#define DECODE_PTR \
    DECODE_UINT; \
76
    void *ptr = (void*)(uintptr_t)code_state->fun_bc->const_table[unum]
77
78
#define DECODE_OBJ \
    DECODE_UINT; \
79
    mp_obj_t obj = (mp_obj_t)code_state->fun_bc->const_table[unum]
80
81
82

#else

83
#define DECODE_QSTR qstr qst = 0; \
84
85
    do { \
        qst = (qst << 7) + (*ip & 0x7f); \
86
    } while ((*ip++ & 0x80) != 0)
87
#define DECODE_PTR \
88
89
90
91
92
93
94
    ip = (byte*)MP_ALIGN(ip, sizeof(void*)); \
    void *ptr = *(void**)ip; \
    ip += sizeof(void*)
#define DECODE_OBJ \
    ip = (byte*)MP_ALIGN(ip, sizeof(mp_obj_t)); \
    mp_obj_t obj = *(mp_obj_t*)ip; \
    ip += sizeof(mp_obj_t)
95
96
97

#endif

98
99
#define PUSH(val) *++sp = (val)
#define POP() (*sp--)
100
101
#define TOP() (*sp)
#define SET_TOP(val) *sp = (val)
Damien's avatar
Damien committed
102

103
#if MICROPY_PY_SYS_EXC_INFO
104
#define CLEAR_SYS_EXC_INFO() MP_STATE_VM(cur_exception) = NULL;
105
106
107
108
#else
#define CLEAR_SYS_EXC_INFO()
#endif

109
#define PUSH_EXC_BLOCK(with_or_finally) do { \
110
111
    DECODE_ULABEL; /* except labels are always forward */ \
    ++exc_sp; \
112
    exc_sp->handler = ip + ulab; \
113
    exc_sp->val_sp = MP_TAGPTR_MAKE(sp, ((with_or_finally) << 1) | currently_in_except_block); \
114
    exc_sp->prev_exc = NULL; \
115
116
    currently_in_except_block = 0; /* in a try block now */ \
} while (0)
117

118
#define POP_EXC_BLOCK() \
119
    currently_in_except_block = MP_TAGPTR_TAG0(exc_sp->val_sp); /* restore previous state */ \
120
121
    exc_sp--; /* pop back to previous exception handler */ \
    CLEAR_SYS_EXC_INFO() /* just clear sys.exc_info(), not compliant, but it shouldn't be used in 1st place */
122

123
124
// fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc)
// sp points to bottom of stack which grows up
125
126
127
128
// returns:
//  MP_VM_RETURN_NORMAL, sp valid, return value in *sp
//  MP_VM_RETURN_YIELD, ip, sp valid, yielded value in *sp
//  MP_VM_RETURN_EXCEPTION, exception in fastn[0]
129
mp_vm_return_kind_t mp_execute_bytecode(mp_code_state_t *code_state, volatile mp_obj_t inject_exc) {
130
131
#define SELECTIVE_EXC_IP (0)
#if SELECTIVE_EXC_IP
132
#define MARK_EXC_IP_SELECTIVE() { code_state->ip = ip; } /* stores ip 1 byte past last opcode */
133
134
135
#define MARK_EXC_IP_GLOBAL()
#else
#define MARK_EXC_IP_SELECTIVE()
136
#define MARK_EXC_IP_GLOBAL() { code_state->ip = ip; } /* stores ip pointing to last opcode */
137
#endif
138
#if MICROPY_OPT_COMPUTED_GOTO
139
    #include "py/vmentrytable.h"
140
    #define DISPATCH() do { \
141
        TRACE(ip); \
142
        MARK_EXC_IP_GLOBAL(); \
143
        goto *entry_table[*ip++]; \
144
    } while (0)
145
    #define DISPATCH_WITH_PEND_EXC_CHECK() goto pending_exception_check
146
147
    #define ENTRY(op) entry_##op
    #define ENTRY_DEFAULT entry_default
148
#else
149
    #define DISPATCH() break
150
    #define DISPATCH_WITH_PEND_EXC_CHECK() goto pending_exception_check
151
152
    #define ENTRY(op) case op
    #define ENTRY_DEFAULT default
153
154
#endif

155
156
157
158
    // nlr_raise needs to be implemented as a goto, so that the C compiler's flow analyser
    // sees that it's possible for us to jump from the dispatch loop to the exception
    // handler.  Without this, the code may have a different stack layout in the dispatch
    // loop and the exception handler, leading to very obscure bugs.
159
    #define RAISE(o) do { nlr_pop(); nlr.ret_val = MP_OBJ_TO_PTR(o); goto exception_handler; } while (0)
Damien's avatar
Damien committed
160

161
162
163
#if MICROPY_STACKLESS
run_code_state: ;
#endif
164
    // Pointers which are constant for particular invocation of mp_execute_bytecode()
165
    size_t n_state = mp_decode_uint_value(code_state->fun_bc->bytecode);
166
167
    mp_obj_t * /*const*/ fastn = &code_state->state[n_state - 1];
    mp_exc_stack_t * /*const*/ exc_stack = (mp_exc_stack_t*)(code_state->state + n_state);
168

169
    // variables that are visible to the exception handler (declared volatile)
170
    volatile bool currently_in_except_block = MP_TAGPTR_TAG0(code_state->exc_sp); // 0 or 1, to detect nested exceptions
171
    mp_exc_stack_t *volatile exc_sp = MP_TAGPTR_PTR(code_state->exc_sp); // stack grows up, exc_sp points to top of stack
Damien's avatar
Damien committed
172

173
174
175
176
177
178
    #if MICROPY_PY_THREAD_GIL && MICROPY_PY_THREAD_GIL_VM_DIVISOR
    // This needs to be volatile and outside the VM loop so it persists across handling
    // of any exceptions.  Otherwise it's possible that the VM never gives up the GIL.
    volatile int gil_divisor = MICROPY_PY_THREAD_GIL_VM_DIVISOR;
    #endif

179
    // outer exception handling loop
Damien's avatar
Damien committed
180
    for (;;) {
181
        nlr_buf_t nlr;
182
outer_dispatch_loop:
183
        if (nlr_push(&nlr) == 0) {
184
            // local variables that are not visible to the exception handler
185
186
            const byte *ip = code_state->ip;
            mp_obj_t *sp = code_state->sp;
187
            mp_obj_t obj_shared;
188
            MICROPY_VM_HOOK_INIT
189

190
191
192
            // If we have exception to inject, now that we finish setting up
            // execution context, raise it. This works as if RAISE_VARARGS
            // bytecode was executed.
193
194
195
            // Injecting exc into yield from generator is a special case,
            // handled by MP_BC_YIELD_FROM itself
            if (inject_exc != MP_OBJ_NULL && *ip != MP_BC_YIELD_FROM) {
196
                mp_obj_t exc = inject_exc;
197
                inject_exc = MP_OBJ_NULL;
198
199
                exc = mp_make_raise_obj(exc);
                RAISE(exc);
200
            }
201

202
203
            // loop to execute byte code
            for (;;) {
204
dispatch_loop:
205
#if MICROPY_OPT_COMPUTED_GOTO
206
207
                DISPATCH();
#else
208
                TRACE(ip);
209
                MARK_EXC_IP_GLOBAL();
210
                switch (*ip++) {
211
#endif
212

213
214
215
                ENTRY(MP_BC_LOAD_CONST_FALSE):
                    PUSH(mp_const_false);
                    DISPATCH();
216

217
218
219
                ENTRY(MP_BC_LOAD_CONST_NONE):
                    PUSH(mp_const_none);
                    DISPATCH();
220

221
222
223
                ENTRY(MP_BC_LOAD_CONST_TRUE):
                    PUSH(mp_const_true);
                    DISPATCH();
224

225
                ENTRY(MP_BC_LOAD_CONST_SMALL_INT): {
226
                    mp_int_t num = 0;
227
228
229
230
231
232
233
234
235
236
                    if ((ip[0] & 0x40) != 0) {
                        // Number is negative
                        num--;
                    }
                    do {
                        num = (num << 7) | (*ip & 0x7f);
                    } while ((*ip++ & 0x80) != 0);
                    PUSH(MP_OBJ_NEW_SMALL_INT(num));
                    DISPATCH();
                }
237

238
                ENTRY(MP_BC_LOAD_CONST_STRING): {
239
                    DECODE_QSTR;
240
                    PUSH(MP_OBJ_NEW_QSTR(qst));
241
                    DISPATCH();
242
                }
243

244
                ENTRY(MP_BC_LOAD_CONST_OBJ): {
245
246
                    DECODE_OBJ;
                    PUSH(obj);
247
248
249
                    DISPATCH();
                }

250
251
252
253
                ENTRY(MP_BC_LOAD_NULL):
                    PUSH(MP_OBJ_NULL);
                    DISPATCH();

254
                ENTRY(MP_BC_LOAD_FAST_N): {
255
                    DECODE_UINT;
256
                    obj_shared = fastn[-unum];
257
                    load_check:
258
259
                    if (obj_shared == MP_OBJ_NULL) {
                        local_name_error: {
260
                            MARK_EXC_IP_SELECTIVE();
261
262
263
                            mp_obj_t obj = mp_obj_new_exception_msg(&mp_type_NameError, "local variable referenced before assignment");
                            RAISE(obj);
                        }
264
                    }
265
                    PUSH(obj_shared);
266
                    DISPATCH();
267
                }
268

269
                ENTRY(MP_BC_LOAD_DEREF): {
270
                    DECODE_UINT;
271
                    obj_shared = mp_obj_cell_get(fastn[-unum]);
272
                    goto load_check;
273
                }
274

275
                #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
276
                ENTRY(MP_BC_LOAD_NAME): {
277
                    MARK_EXC_IP_SELECTIVE();
278
279
280
                    DECODE_QSTR;
                    PUSH(mp_load_name(qst));
                    DISPATCH();
281
                }
282
283
284
285
286
287
                #else
                ENTRY(MP_BC_LOAD_NAME): {
                    MARK_EXC_IP_SELECTIVE();
                    DECODE_QSTR;
                    mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
                    mp_uint_t x = *ip;
288
289
                    if (x < mp_locals_get()->map.alloc && mp_locals_get()->map.table[x].key == key) {
                        PUSH(mp_locals_get()->map.table[x].value);
290
                    } else {
291
                        mp_map_elem_t *elem = mp_map_lookup(&mp_locals_get()->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
292
                        if (elem != NULL) {
293
                            *(byte*)ip = (elem - &mp_locals_get()->map.table[0]) & 0xff;
294
295
296
297
298
299
300
301
302
                            PUSH(elem->value);
                        } else {
                            PUSH(mp_load_name(MP_OBJ_QSTR_VALUE(key)));
                        }
                    }
                    ip++;
                    DISPATCH();
                }
                #endif
303

304
                #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
305
                ENTRY(MP_BC_LOAD_GLOBAL): {
306
                    MARK_EXC_IP_SELECTIVE();
307
308
309
                    DECODE_QSTR;
                    PUSH(mp_load_global(qst));
                    DISPATCH();
310
                }
311
312
313
314
315
316
                #else
                ENTRY(MP_BC_LOAD_GLOBAL): {
                    MARK_EXC_IP_SELECTIVE();
                    DECODE_QSTR;
                    mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
                    mp_uint_t x = *ip;
317
318
                    if (x < mp_globals_get()->map.alloc && mp_globals_get()->map.table[x].key == key) {
                        PUSH(mp_globals_get()->map.table[x].value);
319
                    } else {
320
                        mp_map_elem_t *elem = mp_map_lookup(&mp_globals_get()->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
321
                        if (elem != NULL) {
322
                            *(byte*)ip = (elem - &mp_globals_get()->map.table[0]) & 0xff;
323
324
325
326
327
328
329
330
331
                            PUSH(elem->value);
                        } else {
                            PUSH(mp_load_global(MP_OBJ_QSTR_VALUE(key)));
                        }
                    }
                    ip++;
                    DISPATCH();
                }
                #endif
332

333
                #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
334
                ENTRY(MP_BC_LOAD_ATTR): {
335
                    MARK_EXC_IP_SELECTIVE();
336
337
338
                    DECODE_QSTR;
                    SET_TOP(mp_load_attr(TOP(), qst));
                    DISPATCH();
339
                }
340
341
342
343
344
                #else
                ENTRY(MP_BC_LOAD_ATTR): {
                    MARK_EXC_IP_SELECTIVE();
                    DECODE_QSTR;
                    mp_obj_t top = TOP();
345
                    if (mp_obj_get_type(top)->attr == mp_obj_instance_attr) {
346
                        mp_obj_instance_t *self = MP_OBJ_TO_PTR(top);
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
                        mp_uint_t x = *ip;
                        mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
                        mp_map_elem_t *elem;
                        if (x < self->members.alloc && self->members.table[x].key == key) {
                            elem = &self->members.table[x];
                        } else {
                            elem = mp_map_lookup(&self->members, key, MP_MAP_LOOKUP);
                            if (elem != NULL) {
                                *(byte*)ip = elem - &self->members.table[0];
                            } else {
                                goto load_attr_cache_fail;
                            }
                        }
                        SET_TOP(elem->value);
                        ip++;
                        DISPATCH();
                    }
                load_attr_cache_fail:
                    SET_TOP(mp_load_attr(top, qst));
                    ip++;
                    DISPATCH();
                }
                #endif
370

371
                ENTRY(MP_BC_LOAD_METHOD): {
372
                    MARK_EXC_IP_SELECTIVE();
373
374
375
376
                    DECODE_QSTR;
                    mp_load_method(*sp, qst, sp);
                    sp += 1;
                    DISPATCH();
377
                }
378

379
380
381
382
383
384
385
386
                ENTRY(MP_BC_LOAD_SUPER_METHOD): {
                    MARK_EXC_IP_SELECTIVE();
                    DECODE_QSTR;
                    sp -= 1;
                    mp_load_super_method(qst, sp - 1);
                    DISPATCH();
                }

387
                ENTRY(MP_BC_LOAD_BUILD_CLASS):
388
                    MARK_EXC_IP_SELECTIVE();
389
390
391
                    PUSH(mp_load_build_class());
                    DISPATCH();

392
                ENTRY(MP_BC_LOAD_SUBSCR): {
393
                    MARK_EXC_IP_SELECTIVE();
394
395
                    mp_obj_t index = POP();
                    SET_TOP(mp_obj_subscr(TOP(), index, MP_OBJ_SENTINEL));
396
                    DISPATCH();
397
                }
398

399
                ENTRY(MP_BC_STORE_FAST_N): {
400
401
402
                    DECODE_UINT;
                    fastn[-unum] = POP();
                    DISPATCH();
403
                }
404

405
                ENTRY(MP_BC_STORE_DEREF): {
406
407
408
                    DECODE_UINT;
                    mp_obj_cell_set(fastn[-unum], POP());
                    DISPATCH();
409
                }
410

411
                ENTRY(MP_BC_STORE_NAME): {
412
                    MARK_EXC_IP_SELECTIVE();
413
414
415
                    DECODE_QSTR;
                    mp_store_name(qst, POP());
                    DISPATCH();
416
                }
417

418
                ENTRY(MP_BC_STORE_GLOBAL): {
419
                    MARK_EXC_IP_SELECTIVE();
420
421
422
                    DECODE_QSTR;
                    mp_store_global(qst, POP());
                    DISPATCH();
423
                }
424

425
                #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
426
                ENTRY(MP_BC_STORE_ATTR): {
427
                    MARK_EXC_IP_SELECTIVE();
428
429
430
431
                    DECODE_QSTR;
                    mp_store_attr(sp[0], qst, sp[-1]);
                    sp -= 2;
                    DISPATCH();
432
                }
433
                #else
434
435
436
                // This caching code works with MICROPY_PY_BUILTINS_PROPERTY and/or
                // MICROPY_PY_DESCRIPTORS enabled because if the attr exists in
                // self->members then it can't be a property or have descriptors.  A
437
438
439
440
441
442
                // consequence of this is that we can't use MP_MAP_LOOKUP_ADD_IF_NOT_FOUND
                // in the fast-path below, because that store could override a property.
                ENTRY(MP_BC_STORE_ATTR): {
                    MARK_EXC_IP_SELECTIVE();
                    DECODE_QSTR;
                    mp_obj_t top = TOP();
443
                    if (mp_obj_get_type(top)->attr == mp_obj_instance_attr && sp[-1] != MP_OBJ_NULL) {
444
                        mp_obj_instance_t *self = MP_OBJ_TO_PTR(top);
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
                        mp_uint_t x = *ip;
                        mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
                        mp_map_elem_t *elem;
                        if (x < self->members.alloc && self->members.table[x].key == key) {
                            elem = &self->members.table[x];
                        } else {
                            elem = mp_map_lookup(&self->members, key, MP_MAP_LOOKUP);
                            if (elem != NULL) {
                                *(byte*)ip = elem - &self->members.table[0];
                            } else {
                                goto store_attr_cache_fail;
                            }
                        }
                        elem->value = sp[-1];
                        sp -= 2;
                        ip++;
                        DISPATCH();
                    }
                store_attr_cache_fail:
                    mp_store_attr(sp[0], qst, sp[-1]);
                    sp -= 2;
                    ip++;
                    DISPATCH();
                }
                #endif
470
471

                ENTRY(MP_BC_STORE_SUBSCR):
472
                    MARK_EXC_IP_SELECTIVE();
473
                    mp_obj_subscr(sp[-1], sp[0], sp[-2]);
474
475
476
                    sp -= 3;
                    DISPATCH();

477
                ENTRY(MP_BC_DELETE_FAST): {
478
                    MARK_EXC_IP_SELECTIVE();
479
480
481
482
483
484
                    DECODE_UINT;
                    if (fastn[-unum] == MP_OBJ_NULL) {
                        goto local_name_error;
                    }
                    fastn[-unum] = MP_OBJ_NULL;
                    DISPATCH();
485
                }
486

487
                ENTRY(MP_BC_DELETE_DEREF): {
488
                    MARK_EXC_IP_SELECTIVE();
489
490
491
492
493
494
                    DECODE_UINT;
                    if (mp_obj_cell_get(fastn[-unum]) == MP_OBJ_NULL) {
                        goto local_name_error;
                    }
                    mp_obj_cell_set(fastn[-unum], MP_OBJ_NULL);
                    DISPATCH();
495
                }
496

497
                ENTRY(MP_BC_DELETE_NAME): {
498
                    MARK_EXC_IP_SELECTIVE();
499
500
501
                    DECODE_QSTR;
                    mp_delete_name(qst);
                    DISPATCH();
502
                }
503

504
                ENTRY(MP_BC_DELETE_GLOBAL): {
505
                    MARK_EXC_IP_SELECTIVE();
506
507
508
                    DECODE_QSTR;
                    mp_delete_global(qst);
                    DISPATCH();
509
                }
510

511
512
513
                ENTRY(MP_BC_DUP_TOP): {
                    mp_obj_t top = TOP();
                    PUSH(top);
514
                    DISPATCH();
515
                }
516
517
518
519
520
521
522
523
524
525
526

                ENTRY(MP_BC_DUP_TOP_TWO):
                    sp += 2;
                    sp[0] = sp[-2];
                    sp[-1] = sp[-3];
                    DISPATCH();

                ENTRY(MP_BC_POP_TOP):
                    sp -= 1;
                    DISPATCH();

527
528
                ENTRY(MP_BC_ROT_TWO): {
                    mp_obj_t top = sp[0];
529
                    sp[0] = sp[-1];
530
                    sp[-1] = top;
531
                    DISPATCH();
532
                }
533

534
535
                ENTRY(MP_BC_ROT_THREE): {
                    mp_obj_t top = sp[0];
536
537
                    sp[0] = sp[-1];
                    sp[-1] = sp[-2];
538
                    sp[-2] = top;
539
                    DISPATCH();
540
                }
541

542
                ENTRY(MP_BC_JUMP): {
543
                    DECODE_SLABEL;
544
                    ip += slab;
545
                    DISPATCH_WITH_PEND_EXC_CHECK();
546
                }
547

548
                ENTRY(MP_BC_POP_JUMP_IF_TRUE): {
549
550
                    DECODE_SLABEL;
                    if (mp_obj_is_true(POP())) {
551
                        ip += slab;
552
                    }
553
                    DISPATCH_WITH_PEND_EXC_CHECK();
554
                }
Damien's avatar
Damien committed
555

556
                ENTRY(MP_BC_POP_JUMP_IF_FALSE): {
557
558
                    DECODE_SLABEL;
                    if (!mp_obj_is_true(POP())) {
559
                        ip += slab;
560
                    }
561
                    DISPATCH_WITH_PEND_EXC_CHECK();
562
                }
Damien's avatar
Damien committed
563

564
                ENTRY(MP_BC_JUMP_IF_TRUE_OR_POP): {
565
566
                    DECODE_SLABEL;
                    if (mp_obj_is_true(TOP())) {
567
                        ip += slab;
568
569
570
                    } else {
                        sp--;
                    }
571
                    DISPATCH_WITH_PEND_EXC_CHECK();
572
                }
573

574
                ENTRY(MP_BC_JUMP_IF_FALSE_OR_POP): {
575
576
577
578
                    DECODE_SLABEL;
                    if (mp_obj_is_true(TOP())) {
                        sp--;
                    } else {
579
                        ip += slab;
580
                    }
581
                    DISPATCH_WITH_PEND_EXC_CHECK();
582
                }
583

584
                ENTRY(MP_BC_SETUP_WITH): {
585
                    MARK_EXC_IP_SELECTIVE();
586
                    // stack: (..., ctx_mgr)
587
                    mp_obj_t obj = TOP();
588
589
590
591
                    mp_load_method(obj, MP_QSTR___exit__, sp);
                    mp_load_method(obj, MP_QSTR___enter__, sp + 2);
                    mp_obj_t ret = mp_call_method_n_kw(0, 0, sp + 2);
                    sp += 1;
592
                    PUSH_EXC_BLOCK(1);
593
                    PUSH(ret);
594
                    // stack: (..., __exit__, ctx_mgr, as_value)
595
                    DISPATCH();
596
                }
597
598

                ENTRY(MP_BC_WITH_CLEANUP): {
599
                    MARK_EXC_IP_SELECTIVE();
600
                    // Arriving here, there's "exception control block" on top of stack,
601
                    // and __exit__ method (with self) underneath it. Bytecode calls __exit__,
602
603
                    // and "deletes" it off stack, shifting "exception control block"
                    // to its place.
604
605
                    // The bytecode emitter ensures that there is enough space on the Python
                    // value stack to hold the __exit__ method plus an additional 4 entries.
606
                    if (TOP() == mp_const_none) {
607
608
609
610
611
                        // stack: (..., __exit__, ctx_mgr, None)
                        sp[1] = mp_const_none;
                        sp[2] = mp_const_none;
                        sp -= 2;
                        mp_call_method_n_kw(3, 0, sp);
612
613
                        SET_TOP(mp_const_none);
                    } else if (MP_OBJ_IS_SMALL_INT(TOP())) {
614
615
                        mp_int_t cause_val = MP_OBJ_SMALL_INT_VALUE(TOP());
                        if (cause_val == UNWIND_RETURN) {
616
617
618
619
620
621
622
623
                            // stack: (..., __exit__, ctx_mgr, ret_val, UNWIND_RETURN)
                            mp_obj_t ret_val = sp[-1];
                            sp[-1] = mp_const_none;
                            sp[0] = mp_const_none;
                            sp[1] = mp_const_none;
                            mp_call_method_n_kw(3, 0, sp - 3);
                            sp[-3] = ret_val;
                            sp[-2] = MP_OBJ_NEW_SMALL_INT(UNWIND_RETURN);
624
625
                        } else {
                            assert(cause_val == UNWIND_JUMP);
626
627
628
629
630
631
632
633
634
635
                            // stack: (..., __exit__, ctx_mgr, dest_ip, num_exc, UNWIND_JUMP)
                            mp_obj_t dest_ip = sp[-2];
                            mp_obj_t num_exc = sp[-1];
                            sp[-2] = mp_const_none;
                            sp[-1] = mp_const_none;
                            sp[0] = mp_const_none;
                            mp_call_method_n_kw(3, 0, sp - 4);
                            sp[-4] = dest_ip;
                            sp[-3] = num_exc;
                            sp[-2] = MP_OBJ_NEW_SMALL_INT(UNWIND_JUMP);
636
                        }
637
                        sp -= 2; // we removed (__exit__, ctx_mgr)
638
                    } else {
639
640
641
642
                        assert(mp_obj_is_exception_instance(TOP()));
                        // stack: (..., __exit__, ctx_mgr, exc_instance)
                        // Need to pass (exc_type, exc_instance, None) as arguments to __exit__.
                        sp[1] = sp[0];
643
                        sp[0] = MP_OBJ_FROM_PTR(mp_obj_get_type(sp[0]));
644
645
646
                        sp[2] = mp_const_none;
                        sp -= 2;
                        mp_obj_t ret_value = mp_call_method_n_kw(3, 0, sp);
647
                        if (mp_obj_is_true(ret_value)) {
648
649
650
651
652
                            // We need to silence/swallow the exception.  This is done
                            // by popping the exception and the __exit__ handler and
                            // replacing it with None, which signals END_FINALLY to just
                            // execute the finally handler normally.
                            SET_TOP(mp_const_none);
653
                            assert(exc_sp >= exc_stack);
654
                            POP_EXC_BLOCK();
655
                        } else {
656
                            // We need to re-raise the exception.  We pop __exit__ handler
657
658
                            // by copying the exception instance down to the new top-of-stack.
                            sp[0] = sp[3];
659
                        }
660
661
662
                    }
                    DISPATCH();
                }
663

664
                ENTRY(MP_BC_UNWIND_JUMP): {
665
                    MARK_EXC_IP_SELECTIVE();
666
                    DECODE_SLABEL;
667
668
                    PUSH((mp_obj_t)(mp_uint_t)(uintptr_t)(ip + slab)); // push destination ip for jump
                    PUSH((mp_obj_t)(mp_uint_t)(*ip)); // push number of exception handlers to unwind (0x80 bit set if we also need to pop stack)
669
670
unwind_jump:;
                    mp_uint_t unum = (mp_uint_t)POP(); // get number of exception handlers to unwind
671
                    while ((unum & 0x7f) > 0) {
672
                        unum -= 1;
673
                        assert(exc_sp >= exc_stack);
674
                        if (MP_TAGPTR_TAG1(exc_sp->val_sp)) {
675
676
677
678
                            // Getting here the stack looks like:
                            //     (..., X, dest_ip)
                            // where X is pointed to by exc_sp->val_sp and in the case
                            // of a "with" block contains the context manager info.
679
680
681
                            // We're going to run "finally" code as a coroutine
                            // (not calling it recursively). Set up a sentinel
                            // on a stack so it can return back to us when it is
682
                            // done (when WITH_CLEANUP or END_FINALLY reached).
683
                            PUSH((mp_obj_t)unum); // push number of exception handlers left to unwind
684
685
686
687
                            PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_JUMP)); // push sentinel
                            ip = exc_sp->handler; // get exception handler byte code address
                            exc_sp--; // pop exception handler
                            goto dispatch_loop; // run the exception handler
688
                        }
689
                        POP_EXC_BLOCK();
690
                    }
691
                    ip = (const byte*)MP_OBJ_TO_PTR(POP()); // pop destination ip for jump
692
                    if (unum != 0) {
693
                        // pop the exhausted iterator
694
                        sp -= MP_OBJ_ITER_BUF_NSLOTS;
695
                    }
696
                    DISPATCH_WITH_PEND_EXC_CHECK();
697
                }
698
699
700

                // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
                ENTRY(MP_BC_SETUP_EXCEPT):
701
                ENTRY(MP_BC_SETUP_FINALLY): {
702
                    MARK_EXC_IP_SELECTIVE();
703
704
705
706
707
                    #if SELECTIVE_EXC_IP
                    PUSH_EXC_BLOCK((code_state->ip[-1] == MP_BC_SETUP_FINALLY) ? 1 : 0);
                    #else
                    PUSH_EXC_BLOCK((code_state->ip[0] == MP_BC_SETUP_FINALLY) ? 1 : 0);
                    #endif
708
                    DISPATCH();
709
                }
710
711

                ENTRY(MP_BC_END_FINALLY):
712
                    MARK_EXC_IP_SELECTIVE();
713
                    // if TOS is None, just pops it and continues
714
715
                    // if TOS is an integer, finishes coroutine and returns control to caller
                    // if TOS is an exception, reraises the exception
716
                    if (TOP() == mp_const_none) {
717
                        sp--;
718
                    } else if (MP_OBJ_IS_SMALL_INT(TOP())) {
719
720
721
                        // We finished "finally" coroutine and now dispatch back
                        // to our caller, based on TOS value
                        mp_unwind_reason_t reason = MP_OBJ_SMALL_INT_VALUE(POP());
722
723
724
725
726
                        if (reason == UNWIND_RETURN) {
                            goto unwind_return;
                        } else {
                            assert(reason == UNWIND_JUMP);
                            goto unwind_jump;
727
                        }
728
729
730
                    } else {
                        assert(mp_obj_is_exception_instance(TOP()));
                        RAISE(TOP());
731
732
733
734
                    }
                    DISPATCH();

                ENTRY(MP_BC_GET_ITER):
735
                    MARK_EXC_IP_SELECTIVE();
736
                    SET_TOP(mp_getiter(TOP(), NULL));
737
738
                    DISPATCH();

739
740
741
                // An iterator for a for-loop takes MP_OBJ_ITER_BUF_NSLOTS slots on
                // the Python value stack.  These slots are either used to store the
                // iterator object itself, or the first slot is MP_OBJ_NULL and
742
                // the second slot holds a reference to the iterator object.
743
744
745
746
                ENTRY(MP_BC_GET_ITER_STACK): {
                    MARK_EXC_IP_SELECTIVE();
                    mp_obj_t obj = TOP();
                    mp_obj_iter_buf_t *iter_buf = (mp_obj_iter_buf_t*)sp;
747
                    sp += MP_OBJ_ITER_BUF_NSLOTS - 1;
748
749
750
                    obj = mp_getiter(obj, iter_buf);
                    if (obj != MP_OBJ_FROM_PTR(iter_buf)) {
                        // Iterator didn't use the stack so indicate that with MP_OBJ_NULL.
751
752
                        sp[-MP_OBJ_ITER_BUF_NSLOTS + 1] = MP_OBJ_NULL;
                        sp[-MP_OBJ_ITER_BUF_NSLOTS + 2] = obj;
753
                    }
754
755
756
                    DISPATCH();
                }

757
                ENTRY(MP_BC_FOR_ITER): {
758
                    MARK_EXC_IP_SELECTIVE();
759
                    DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
760
                    code_state->sp = sp;
761
                    mp_obj_t obj;
762
763
                    if (sp[-MP_OBJ_ITER_BUF_NSLOTS + 1] == MP_OBJ_NULL) {
                        obj = sp[-MP_OBJ_ITER_BUF_NSLOTS + 2];
764
                    } else {
765
                        obj = MP_OBJ_FROM_PTR(&sp[-MP_OBJ_ITER_BUF_NSLOTS + 1]);
766
767
                    }
                    mp_obj_t value = mp_iternext_allow_raise(obj);
768
                    if (value == MP_OBJ_STOP_ITERATION) {
769
                        sp -= MP_OBJ_ITER_BUF_NSLOTS; // pop the exhausted iterator
770
                        ip += ulab; // jump to after for-block
771
                    } else {
772
                        PUSH(value); // push the next iteration value
773
774
                    }
                    DISPATCH();
775
                }
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790

                // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
                ENTRY(MP_BC_POP_BLOCK):
                    // we are exiting an exception handler, so pop the last one of the exception-stack
                    assert(exc_sp >= exc_stack);
                    POP_EXC_BLOCK();
                    DISPATCH();

                // matched against: SETUP_EXCEPT
                ENTRY(MP_BC_POP_EXCEPT):
                    assert(exc_sp >= exc_stack);
                    assert(currently_in_except_block);
                    POP_EXC_BLOCK();
                    DISPATCH();

791
                ENTRY(MP_BC_BUILD_TUPLE): {
792
                    MARK_EXC_IP_SELECTIVE();
793
794
795
796
                    DECODE_UINT;
                    sp -= unum - 1;
                    SET_TOP(mp_obj_new_tuple(unum, sp));
                    DISPATCH();
797
                }
798

799
                ENTRY(MP_BC_BUILD_LIST): {
800
                    MARK_EXC_IP_SELECTIVE();
801
802
803
804
                    DECODE_UINT;
                    sp -= unum - 1;
                    SET_TOP(mp_obj_new_list(unum, sp));
                    DISPATCH();
805
                }
806

807
                ENTRY(MP_BC_BUILD_MAP): {
808
                    MARK_EXC_IP_SELECTIVE();
809
810
811
                    DECODE_UINT;
                    PUSH(mp_obj_new_dict(unum));
                    DISPATCH();
812
                }
813
814

                ENTRY(MP_BC_STORE_MAP):
815
                    MARK_EXC_IP_SELECTIVE();
816
817
818
819
                    sp -= 2;
                    mp_obj_dict_store(sp[0], sp[2], sp[1]);
                    DISPATCH();

820
#if MICROPY_PY_BUILTINS_SET
821
                ENTRY(MP_BC_BUILD_SET): {
822
                    MARK_EXC_IP_SELECTIVE();
823
824
825
826
                    DECODE_UINT;
                    sp -= unum - 1;
                    SET_TOP(mp_obj_new_set(unum, sp));
                    DISPATCH();
827
                }
828
#endif
Damien's avatar
Damien committed
829

830
#if MICROPY_PY_BUILTINS_SLICE
831
                ENTRY(MP_BC_BUILD_SLICE): {
832
                    MARK_EXC_IP_SELECTIVE();
833
834
                    DECODE_UINT;
                    if (unum == 2) {
835
836
837
                        mp_obj_t stop = POP();
                        mp_obj_t start = TOP();
                        SET_TOP(mp_obj_new_slice(start, stop, mp_const_none));
838
                    } else {
839
840
841
842
                        mp_obj_t step = POP();
                        mp_obj_t stop = POP();
                        mp_obj_t start = TOP();
                        SET_TOP(mp_obj_new_slice(start, stop, step));
843
844
                    }
                    DISPATCH();
845
                }
846
#endif
847

848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
                ENTRY(MP_BC_STORE_COMP): {
                    MARK_EXC_IP_SELECTIVE();
                    DECODE_UINT;
                    mp_obj_t obj = sp[-(unum >> 2)];
                    if ((unum & 3) == 0) {
                        mp_obj_list_append(obj, sp[0]);
                        sp--;
                    } else if (!MICROPY_PY_BUILTINS_SET || (unum & 3) == 1) {
                        mp_obj_dict_store(obj, sp[0], sp[-1]);
                        sp -= 2;
                    #if MICROPY_PY_BUILTINS_SET
                    } else {
                        mp_obj_set_store(obj, sp[0]);
                        sp--;
                    #endif
                    }
                    DISPATCH();
                }

867
                ENTRY(MP_BC_UNPACK_SEQUENCE): {
868
                    MARK_EXC_IP_SELECTIVE();
869
870
871
872
                    DECODE_UINT;
                    mp_unpack_sequence(sp[0], unum, sp);
                    sp += unum - 1;
                    DISPATCH();
873
                }