Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Paul Sokolovsky | c0abc28 | 2014-03-22 13:49:31 +0200 | [diff] [blame] | 27 | // Exception stack entry |
| 28 | typedef struct _mp_exc_stack { |
| 29 | const byte *handler; |
| 30 | // bit 0 is saved currently_in_except_block value |
Paul Sokolovsky | 1673420 | 2014-03-22 23:20:07 +0200 | [diff] [blame] | 31 | mp_obj_t *val_sp; |
Paul Sokolovsky | 0c904df | 2014-03-30 00:48:21 +0200 | [diff] [blame] | 32 | // Saved exception, valid if currently_in_except_block bit is 1 |
| 33 | mp_obj_t prev_exc; |
Paul Sokolovsky | c0abc28 | 2014-03-22 13:49:31 +0200 | [diff] [blame] | 34 | // We might only have 2 interesting cases here: SETUP_EXCEPT & SETUP_FINALLY, |
| 35 | // consider storing it in bit 1 of val_sp. TODO: SETUP_WITH? |
| 36 | byte opcode; |
Damien George | 89f94b5 | 2014-03-30 00:57:09 +0000 | [diff] [blame] | 37 | } mp_exc_stack_t; |
Paul Sokolovsky | c0abc28 | 2014-03-22 13:49:31 +0200 | [diff] [blame] | 38 | |
Paul Sokolovsky | b4ebad3 | 2014-05-31 16:50:46 +0300 | [diff] [blame] | 39 | typedef struct _mp_code_state { |
| 40 | const byte *code_info; |
| 41 | const byte *ip; |
| 42 | mp_obj_t *sp; |
| 43 | // bit 0 is saved currently_in_except_block value |
| 44 | mp_exc_stack_t *exc_sp; |
| 45 | uint n_state; |
| 46 | // Variable-length |
| 47 | mp_obj_t state[0]; |
| 48 | // Variable-length, never accessed by name, only as (void*)(state + n_state) |
| 49 | //mp_exc_stack_t exc_state[0]; |
| 50 | } mp_code_state; |
| 51 | |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 52 | mp_uint_t mp_decode_uint(const byte **ptr); |
| 53 | |
Damien George | aabd83e | 2014-06-07 14:16:08 +0100 | [diff] [blame] | 54 | mp_vm_return_kind_t mp_execute_bytecode(mp_code_state *code_state, volatile mp_obj_t inject_exc); |
Damien George | 3c658a4 | 2014-08-24 16:28:17 +0100 | [diff] [blame] | 55 | void mp_setup_code_state(mp_code_state *code_state, mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args); |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame^] | 56 | void mp_bytecode_print(const void *descr, const byte *code, mp_uint_t len); |
| 57 | void mp_bytecode_print2(const byte *code, mp_uint_t len); |
Paul Sokolovsky | 1673420 | 2014-03-22 23:20:07 +0200 | [diff] [blame] | 58 | |
| 59 | // Helper macros to access pointer with least significant bit holding a flag |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 60 | #define MP_TAGPTR_PTR(x) ((void*)((mp_uint_t)(x) & ~((mp_uint_t)1))) |
| 61 | #define MP_TAGPTR_TAG(x) ((mp_uint_t)(x) & 1) |
| 62 | #define MP_TAGPTR_MAKE(ptr, tag) ((void*)((mp_uint_t)(ptr) | tag)) |