blob: 29f93870a8530fbce33db1bed96f28511b70316b [file] [log] [blame]
Damien Georgeb4b10fd2015-01-01 23:30:53 +00001/*
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"
Damien Georgec93d9ca2016-04-25 15:28:57 +000032#include "py/mpthread.h"
Damien Georgeb4b10fd2015-01-01 23:30:53 +000033#include "py/misc.h"
34#include "py/nlr.h"
35#include "py/obj.h"
Damien Georgee1e359f2015-02-07 17:24:10 +000036#include "py/objlist.h"
Damien Georgeb4b10fd2015-01-01 23:30:53 +000037#include "py/objexcept.h"
38
39// This file contains structures defining the state of the Micro Python
40// memory system, runtime and virtual machine. The state is a global
41// variable, but in the future it is hoped that the state can become local.
42
Damien Georgeea235202016-02-11 22:30:53 +000043// This structure contains dynamic configuration for the compiler.
44#if MICROPY_DYNAMIC_COMPILER
45typedef struct mp_dynamic_compiler_t {
46 uint8_t small_int_bits; // must be <= host small_int_bits
47 bool opt_cache_map_lookup_in_bytecode;
48 bool py_builtins_str_unicode;
49} mp_dynamic_compiler_t;
50extern mp_dynamic_compiler_t mp_dynamic_compiler;
51#endif
52
Damien George6e74d242017-02-16 18:05:06 +110053// These are the values for sched_state
54#define MP_SCHED_IDLE (1)
55#define MP_SCHED_LOCKED (-1)
56#define MP_SCHED_PENDING (0) // 0 so it's a quick check in the VM
57
58typedef struct _mp_sched_item_t {
59 mp_obj_t func;
60 mp_obj_t arg;
61} mp_sched_item_t;
62
Damien Georgeb4b10fd2015-01-01 23:30:53 +000063// This structure hold information about the memory allocation system.
64typedef struct _mp_state_mem_t {
65 #if MICROPY_MEM_STATS
66 size_t total_bytes_allocated;
67 size_t current_bytes_allocated;
68 size_t peak_bytes_allocated;
69 #endif
70
71 byte *gc_alloc_table_start;
Damien Georged977d262015-12-16 20:09:11 -050072 size_t gc_alloc_table_byte_len;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000073 #if MICROPY_ENABLE_FINALISER
74 byte *gc_finaliser_table_start;
75 #endif
Damien George94fe6e52015-11-27 13:07:48 +000076 byte *gc_pool_start;
77 byte *gc_pool_end;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000078
79 int gc_stack_overflow;
Damien Georged977d262015-12-16 20:09:11 -050080 size_t gc_stack[MICROPY_ALLOC_GC_STACK_SIZE];
81 size_t *gc_sp;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000082 uint16_t gc_lock_depth;
83
84 // This variable controls auto garbage collection. If set to 0 then the
85 // GC won't automatically run when gc_alloc can't find enough blocks. But
86 // you can still allocate/free memory and also explicitly call gc_collect.
87 uint16_t gc_auto_collect_enabled;
88
Paul Sokolovsky93e353e2016-07-21 00:37:30 +030089 #if MICROPY_GC_ALLOC_THRESHOLD
90 size_t gc_alloc_amount;
91 size_t gc_alloc_threshold;
92 #endif
93
Damien Georged977d262015-12-16 20:09:11 -050094 size_t gc_last_free_atb_index;
Damien Georgee1e359f2015-02-07 17:24:10 +000095
96 #if MICROPY_PY_GC_COLLECT_RETVAL
Damien Georged977d262015-12-16 20:09:11 -050097 size_t gc_collected;
Damien Georgee1e359f2015-02-07 17:24:10 +000098 #endif
Damien Georgec93d9ca2016-04-25 15:28:57 +000099
100 #if MICROPY_PY_THREAD
101 // This is a global mutex used to make the GC thread-safe.
102 mp_thread_mutex_t gc_mutex;
103 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000104} mp_state_mem_t;
105
106// This structure hold runtime and VM information. It includes a section
107// which contains root pointers that must be scanned by the GC.
108typedef struct _mp_state_vm_t {
109 ////////////////////////////////////////////////////////////
110 // START ROOT POINTER SECTION
111 // everything that needs GC scanning must go here
112 // this must start at the start of this structure
113 //
114
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000115 qstr_pool_t *last_pool;
116
117 // non-heap memory for creating an exception if we can't allocate RAM
118 mp_obj_exception_t mp_emergency_exception_obj;
119
120 // memory for exception arguments if we can't allocate RAM
121 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
122 #if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
123 // statically allocated buf
124 byte mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE];
125 #else
126 // dynamically allocated buf
127 byte *mp_emergency_exception_buf;
128 #endif
129 #endif
130
Damien George7f1da0a2016-12-15 13:00:19 +1100131 #if MICROPY_KBD_EXCEPTION
132 // exception object of type KeyboardInterrupt
133 mp_obj_exception_t mp_kbd_exception;
134 #endif
135
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200136 // dictionary with loaded modules (may be exposed as sys.modules)
137 mp_obj_dict_t mp_loaded_modules_dict;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000138
139 // pending exception object (MP_OBJ_NULL if not pending)
Damien George994ff732015-11-17 14:27:21 +0000140 volatile mp_obj_t mp_pending_exception;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000141
Damien George6e74d242017-02-16 18:05:06 +1100142 #if MICROPY_ENABLE_SCHEDULER
143 volatile int16_t sched_state;
144 uint16_t sched_sp;
145 mp_sched_item_t sched_stack[MICROPY_SCHEDULER_DEPTH];
146 #endif
147
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300148 // current exception being handled, for sys.exc_info()
149 #if MICROPY_PY_SYS_EXC_INFO
Damien George999cedb2015-11-27 17:01:44 +0000150 mp_obj_base_t *cur_exception;
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300151 #endif
152
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000153 // dictionary for the __main__ module
154 mp_obj_dict_t dict_main;
155
Damien Georgee1e359f2015-02-07 17:24:10 +0000156 // these two lists must be initialised per port, after the call to mp_init
157 mp_obj_list_t mp_sys_path_obj;
158 mp_obj_list_t mp_sys_argv_obj;
159
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000160 // dictionary for overridden builtins
161 #if MICROPY_CAN_OVERRIDE_BUILTINS
162 mp_obj_dict_t *mp_module_builtins_override_dict;
163 #endif
164
165 // include any root pointers defined by a port
166 MICROPY_PORT_ROOT_POINTERS
167
Paul Sokolovskye0d77402015-10-27 00:04:33 +0300168 // root pointers for extmod
Paul Sokolovsky00ee84e2016-01-01 14:19:26 +0200169
170 #if MICROPY_PY_OS_DUPTERM
171 mp_obj_t term_obj;
Paul Sokolovsky426112c2016-07-04 13:32:30 +0300172 mp_obj_t dupterm_arr_obj;
Paul Sokolovsky00ee84e2016-01-01 14:19:26 +0200173 #endif
174
Paul Sokolovskye0d77402015-10-27 00:04:33 +0300175 #if MICROPY_PY_LWIP_SLIP
176 mp_obj_t lwip_slip_stream;
177 #endif
178
Damien Georgedcb9ea72017-01-27 15:10:09 +1100179 #if MICROPY_VFS
Damien George3f6b4e02017-01-27 22:40:15 +1100180 struct _mp_vfs_mount_t *vfs_cur;
181 struct _mp_vfs_mount_t *vfs_mount_table;
Damien Georgedcb9ea72017-01-27 15:10:09 +1100182 #endif
183
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000184 //
185 // END ROOT POINTER SECTION
186 ////////////////////////////////////////////////////////////
187
Damien Georgeade9a052015-06-13 21:53:22 +0100188 // pointer and sizes to store interned string data
189 // (qstr_last_chunk can be root pointer but is also stored in qstr pool)
190 byte *qstr_last_chunk;
Damien George25784852015-12-17 12:41:40 +0000191 size_t qstr_last_alloc;
192 size_t qstr_last_used;
Damien Georgeade9a052015-06-13 21:53:22 +0100193
Damien George1f54ad22016-05-26 09:06:46 +0000194 #if MICROPY_PY_THREAD
195 // This is a global mutex used to make qstr interning thread-safe.
196 mp_thread_mutex_t qstr_mutex;
197 #endif
198
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000199 mp_uint_t mp_optimise_value;
200
201 // size of the emergency exception buf, if it's dynamically allocated
202 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0
203 mp_int_t mp_emergency_exception_buf_size;
204 #endif
Damien George4cec63a2016-05-26 10:42:53 +0000205
206 #if MICROPY_PY_THREAD_GIL
207 // This is a global mutex used to make the VM/runtime thread-safe.
208 mp_thread_mutex_t gil_mutex;
209 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000210} mp_state_vm_t;
211
Damien George330165a2016-04-22 22:44:56 +0000212// This structure holds state that is specific to a given thread.
213// Everything in this structure is scanned for root pointers.
214typedef struct _mp_state_thread_t {
Damien George05fe66f2017-02-27 23:56:46 +1100215 mp_obj_dict_t *dict_locals;
216 mp_obj_dict_t *dict_globals;
217
Damien George330165a2016-04-22 22:44:56 +0000218 // Note: nlr asm code has the offset of this hard-coded
219 nlr_buf_t *nlr_top; // ROOT POINTER
220
221 // Stack top at the start of program
Damien George330165a2016-04-22 22:44:56 +0000222 char *stack_top;
223
224 #if MICROPY_STACK_CHECK
225 size_t stack_limit;
226 #endif
227} mp_state_thread_t;
228
Damien George05fe66f2017-02-27 23:56:46 +1100229// This structure combines the above 3 structures.
230// The order of the entries are important for root pointer scanning in the GC to work.
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000231// Note: if this structure changes then revisit all nlr asm code since they
232// have the offset of nlr_top hard-coded.
233typedef struct _mp_state_ctx_t {
Damien George330165a2016-04-22 22:44:56 +0000234 mp_state_thread_t thread;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000235 mp_state_vm_t vm;
236 mp_state_mem_t mem;
237} mp_state_ctx_t;
238
239extern mp_state_ctx_t mp_state_ctx;
240
Damien George05fe66f2017-02-27 23:56:46 +1100241#define MP_STATE_CTX(x) MP_STATE_THREAD(x)
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000242#define MP_STATE_VM(x) (mp_state_ctx.vm.x)
243#define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
244
Damien George27cc0772016-04-22 22:52:33 +0000245#if MICROPY_PY_THREAD
246extern mp_state_thread_t *mp_thread_get_state(void);
247#define MP_STATE_THREAD(x) (mp_thread_get_state()->x)
248#else
Damien George330165a2016-04-22 22:44:56 +0000249#define MP_STATE_THREAD(x) (mp_state_ctx.thread.x)
Damien George27cc0772016-04-22 22:52:33 +0000250#endif
Damien George330165a2016-04-22 22:44:56 +0000251
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000252#endif // __MICROPY_INCLUDED_PY_MPSTATE_H__