Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [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) 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 | #ifndef __MICROPY_INCLUDED_PY_MPSTATE_H__ |
| 27 | #define __MICROPY_INCLUDED_PY_MPSTATE_H__ |
| 28 | |
| 29 | #include <stdint.h> |
| 30 | |
| 31 | #include "py/mpconfig.h" |
| 32 | #include "py/misc.h" |
| 33 | #include "py/nlr.h" |
| 34 | #include "py/obj.h" |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 35 | #include "py/objlist.h" |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 36 | #include "py/objexcept.h" |
| 37 | |
| 38 | // This file contains structures defining the state of the Micro Python |
| 39 | // memory system, runtime and virtual machine. The state is a global |
| 40 | // variable, but in the future it is hoped that the state can become local. |
| 41 | |
Damien George | ea23520 | 2016-02-11 22:30:53 +0000 | [diff] [blame^] | 42 | // This structure contains dynamic configuration for the compiler. |
| 43 | #if MICROPY_DYNAMIC_COMPILER |
| 44 | typedef struct mp_dynamic_compiler_t { |
| 45 | uint8_t small_int_bits; // must be <= host small_int_bits |
| 46 | bool opt_cache_map_lookup_in_bytecode; |
| 47 | bool py_builtins_str_unicode; |
| 48 | } mp_dynamic_compiler_t; |
| 49 | extern mp_dynamic_compiler_t mp_dynamic_compiler; |
| 50 | #endif |
| 51 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 52 | // This structure hold information about the memory allocation system. |
| 53 | typedef struct _mp_state_mem_t { |
| 54 | #if MICROPY_MEM_STATS |
| 55 | size_t total_bytes_allocated; |
| 56 | size_t current_bytes_allocated; |
| 57 | size_t peak_bytes_allocated; |
| 58 | #endif |
| 59 | |
| 60 | byte *gc_alloc_table_start; |
Damien George | d977d26 | 2015-12-16 20:09:11 -0500 | [diff] [blame] | 61 | size_t gc_alloc_table_byte_len; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 62 | #if MICROPY_ENABLE_FINALISER |
| 63 | byte *gc_finaliser_table_start; |
| 64 | #endif |
Damien George | 94fe6e5 | 2015-11-27 13:07:48 +0000 | [diff] [blame] | 65 | byte *gc_pool_start; |
| 66 | byte *gc_pool_end; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 67 | |
| 68 | int gc_stack_overflow; |
Damien George | d977d26 | 2015-12-16 20:09:11 -0500 | [diff] [blame] | 69 | size_t gc_stack[MICROPY_ALLOC_GC_STACK_SIZE]; |
| 70 | size_t *gc_sp; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 71 | uint16_t gc_lock_depth; |
| 72 | |
| 73 | // This variable controls auto garbage collection. If set to 0 then the |
| 74 | // GC won't automatically run when gc_alloc can't find enough blocks. But |
| 75 | // you can still allocate/free memory and also explicitly call gc_collect. |
| 76 | uint16_t gc_auto_collect_enabled; |
| 77 | |
Damien George | d977d26 | 2015-12-16 20:09:11 -0500 | [diff] [blame] | 78 | size_t gc_last_free_atb_index; |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 79 | |
| 80 | #if MICROPY_PY_GC_COLLECT_RETVAL |
Damien George | d977d26 | 2015-12-16 20:09:11 -0500 | [diff] [blame] | 81 | size_t gc_collected; |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 82 | #endif |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 83 | } mp_state_mem_t; |
| 84 | |
| 85 | // This structure hold runtime and VM information. It includes a section |
| 86 | // which contains root pointers that must be scanned by the GC. |
| 87 | typedef struct _mp_state_vm_t { |
| 88 | //////////////////////////////////////////////////////////// |
| 89 | // START ROOT POINTER SECTION |
| 90 | // everything that needs GC scanning must go here |
| 91 | // this must start at the start of this structure |
| 92 | // |
| 93 | |
| 94 | // Note: nlr asm code has the offset of this hard-coded |
| 95 | nlr_buf_t *nlr_top; |
| 96 | |
| 97 | qstr_pool_t *last_pool; |
| 98 | |
| 99 | // non-heap memory for creating an exception if we can't allocate RAM |
| 100 | mp_obj_exception_t mp_emergency_exception_obj; |
| 101 | |
| 102 | // memory for exception arguments if we can't allocate RAM |
| 103 | #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF |
| 104 | #if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0 |
| 105 | // statically allocated buf |
| 106 | byte mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE]; |
| 107 | #else |
| 108 | // dynamically allocated buf |
| 109 | byte *mp_emergency_exception_buf; |
| 110 | #endif |
| 111 | #endif |
| 112 | |
Paul Sokolovsky | 1a1d11f | 2015-12-05 00:09:10 +0200 | [diff] [blame] | 113 | // dictionary with loaded modules (may be exposed as sys.modules) |
| 114 | mp_obj_dict_t mp_loaded_modules_dict; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 115 | |
| 116 | // pending exception object (MP_OBJ_NULL if not pending) |
Damien George | 994ff73 | 2015-11-17 14:27:21 +0000 | [diff] [blame] | 117 | volatile mp_obj_t mp_pending_exception; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 118 | |
Paul Sokolovsky | 8b85d14 | 2015-04-25 03:17:41 +0300 | [diff] [blame] | 119 | // current exception being handled, for sys.exc_info() |
| 120 | #if MICROPY_PY_SYS_EXC_INFO |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 121 | mp_obj_base_t *cur_exception; |
Paul Sokolovsky | 8b85d14 | 2015-04-25 03:17:41 +0300 | [diff] [blame] | 122 | #endif |
| 123 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 124 | // dictionary for the __main__ module |
| 125 | mp_obj_dict_t dict_main; |
| 126 | |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 127 | // these two lists must be initialised per port, after the call to mp_init |
| 128 | mp_obj_list_t mp_sys_path_obj; |
| 129 | mp_obj_list_t mp_sys_argv_obj; |
| 130 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 131 | // dictionary for overridden builtins |
| 132 | #if MICROPY_CAN_OVERRIDE_BUILTINS |
| 133 | mp_obj_dict_t *mp_module_builtins_override_dict; |
| 134 | #endif |
| 135 | |
| 136 | // include any root pointers defined by a port |
| 137 | MICROPY_PORT_ROOT_POINTERS |
| 138 | |
Paul Sokolovsky | e0d7740 | 2015-10-27 00:04:33 +0300 | [diff] [blame] | 139 | // root pointers for extmod |
Paul Sokolovsky | 00ee84e | 2016-01-01 14:19:26 +0200 | [diff] [blame] | 140 | |
| 141 | #if MICROPY_PY_OS_DUPTERM |
| 142 | mp_obj_t term_obj; |
| 143 | #endif |
| 144 | |
Paul Sokolovsky | e0d7740 | 2015-10-27 00:04:33 +0300 | [diff] [blame] | 145 | #if MICROPY_PY_LWIP_SLIP |
| 146 | mp_obj_t lwip_slip_stream; |
| 147 | #endif |
| 148 | |
Paul Sokolovsky | 72085a6 | 2016-02-15 00:02:03 +0200 | [diff] [blame] | 149 | #if MICROPY_FSUSERMOUNT |
| 150 | // for user-mountable block device (max fixed at compile time) |
| 151 | struct _fs_user_mount_t *fs_user_mount[MICROPY_FATFS_VOLUMES]; |
| 152 | #endif |
| 153 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 154 | // |
| 155 | // END ROOT POINTER SECTION |
| 156 | //////////////////////////////////////////////////////////// |
| 157 | |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 158 | // pointer and sizes to store interned string data |
| 159 | // (qstr_last_chunk can be root pointer but is also stored in qstr pool) |
| 160 | byte *qstr_last_chunk; |
Damien George | 2578485 | 2015-12-17 12:41:40 +0000 | [diff] [blame] | 161 | size_t qstr_last_alloc; |
| 162 | size_t qstr_last_used; |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 163 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 164 | // Stack top at the start of program |
| 165 | // Note: this entry is used to locate the end of the root pointer section. |
| 166 | char *stack_top; |
| 167 | |
| 168 | #if MICROPY_STACK_CHECK |
| 169 | mp_uint_t stack_limit; |
| 170 | #endif |
| 171 | |
| 172 | mp_uint_t mp_optimise_value; |
| 173 | |
| 174 | // size of the emergency exception buf, if it's dynamically allocated |
| 175 | #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0 |
| 176 | mp_int_t mp_emergency_exception_buf_size; |
| 177 | #endif |
| 178 | } mp_state_vm_t; |
| 179 | |
| 180 | // This structure combines the above 2 structures, and adds the local |
| 181 | // and global dicts. |
| 182 | // Note: if this structure changes then revisit all nlr asm code since they |
| 183 | // have the offset of nlr_top hard-coded. |
| 184 | typedef struct _mp_state_ctx_t { |
| 185 | // these must come first for root pointer scanning in GC to work |
| 186 | mp_obj_dict_t *dict_locals; |
| 187 | mp_obj_dict_t *dict_globals; |
| 188 | // this must come next for root pointer scanning in GC to work |
| 189 | mp_state_vm_t vm; |
| 190 | mp_state_mem_t mem; |
| 191 | } mp_state_ctx_t; |
| 192 | |
| 193 | extern mp_state_ctx_t mp_state_ctx; |
| 194 | |
| 195 | #define MP_STATE_CTX(x) (mp_state_ctx.x) |
| 196 | #define MP_STATE_VM(x) (mp_state_ctx.vm.x) |
| 197 | #define MP_STATE_MEM(x) (mp_state_ctx.mem.x) |
| 198 | |
| 199 | #endif // __MICROPY_INCLUDED_PY_MPSTATE_H__ |