1 | /* |
2 | * This file is part of the MicroPython 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 | |
27 | #include <stdio.h> |
28 | |
29 | #include "py/builtin.h" |
30 | #include "py/stackctrl.h" |
31 | #include "py/runtime.h" |
32 | #include "py/gc.h" |
33 | #include "py/mphal.h" |
34 | |
35 | // Various builtins specific to MicroPython runtime, |
36 | // living in micropython module |
37 | |
38 | #if MICROPY_ENABLE_COMPILER |
39 | STATIC mp_obj_t mp_micropython_opt_level(size_t n_args, const mp_obj_t *args) { |
40 | if (n_args == 0) { |
41 | return MP_OBJ_NEW_SMALL_INT(MP_STATE_VM(mp_optimise_value)); |
42 | } else { |
43 | MP_STATE_VM(mp_optimise_value) = mp_obj_get_int(args[0]); |
44 | return mp_const_none; |
45 | } |
46 | } |
47 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_opt_level_obj, 0, 1, mp_micropython_opt_level); |
48 | #endif |
49 | |
50 | #if MICROPY_PY_MICROPYTHON_MEM_INFO |
51 | |
52 | #if MICROPY_MEM_STATS |
53 | STATIC mp_obj_t mp_micropython_mem_total(void) { |
54 | return MP_OBJ_NEW_SMALL_INT(m_get_total_bytes_allocated()); |
55 | } |
56 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_total_obj, mp_micropython_mem_total); |
57 | |
58 | STATIC mp_obj_t mp_micropython_mem_current(void) { |
59 | return MP_OBJ_NEW_SMALL_INT(m_get_current_bytes_allocated()); |
60 | } |
61 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_current_obj, mp_micropython_mem_current); |
62 | |
63 | STATIC mp_obj_t mp_micropython_mem_peak(void) { |
64 | return MP_OBJ_NEW_SMALL_INT(m_get_peak_bytes_allocated()); |
65 | } |
66 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_peak_obj, mp_micropython_mem_peak); |
67 | #endif |
68 | |
69 | mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args) { |
70 | (void)args; |
71 | #if MICROPY_MEM_STATS |
72 | mp_printf(&mp_plat_print, "mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n" , |
73 | (mp_uint_t)m_get_total_bytes_allocated(), (mp_uint_t)m_get_current_bytes_allocated(), (mp_uint_t)m_get_peak_bytes_allocated()); |
74 | #endif |
75 | #if MICROPY_STACK_CHECK |
76 | mp_printf(&mp_plat_print, "stack: " UINT_FMT " out of " UINT_FMT "\n" , |
77 | mp_stack_usage(), (mp_uint_t)MP_STATE_THREAD(stack_limit)); |
78 | #else |
79 | mp_printf(&mp_plat_print, "stack: " UINT_FMT "\n" , mp_stack_usage()); |
80 | #endif |
81 | #if MICROPY_ENABLE_GC |
82 | gc_dump_info(); |
83 | if (n_args == 1) { |
84 | // arg given means dump gc allocation table |
85 | gc_dump_alloc_table(); |
86 | } |
87 | #else |
88 | (void)n_args; |
89 | #endif |
90 | return mp_const_none; |
91 | } |
92 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_mem_info_obj, 0, 1, mp_micropython_mem_info); |
93 | |
94 | STATIC mp_obj_t mp_micropython_qstr_info(size_t n_args, const mp_obj_t *args) { |
95 | (void)args; |
96 | size_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes; |
97 | qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes); |
98 | mp_printf(&mp_plat_print, "qstr pool: n_pool=%u, n_qstr=%u, n_str_data_bytes=%u, n_total_bytes=%u\n" , |
99 | n_pool, n_qstr, n_str_data_bytes, n_total_bytes); |
100 | if (n_args == 1) { |
101 | // arg given means dump qstr data |
102 | qstr_dump_data(); |
103 | } |
104 | return mp_const_none; |
105 | } |
106 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_qstr_info_obj, 0, 1, mp_micropython_qstr_info); |
107 | |
108 | #endif // MICROPY_PY_MICROPYTHON_MEM_INFO |
109 | |
110 | #if MICROPY_PY_MICROPYTHON_STACK_USE |
111 | STATIC mp_obj_t mp_micropython_stack_use(void) { |
112 | return MP_OBJ_NEW_SMALL_INT(mp_stack_usage()); |
113 | } |
114 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_stack_use_obj, mp_micropython_stack_use); |
115 | #endif |
116 | |
117 | #if MICROPY_ENABLE_PYSTACK |
118 | STATIC mp_obj_t mp_micropython_pystack_use(void) { |
119 | return MP_OBJ_NEW_SMALL_INT(mp_pystack_usage()); |
120 | } |
121 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_pystack_use_obj, mp_micropython_pystack_use); |
122 | #endif |
123 | |
124 | #if MICROPY_ENABLE_GC |
125 | STATIC mp_obj_t mp_micropython_heap_lock(void) { |
126 | gc_lock(); |
127 | return mp_const_none; |
128 | } |
129 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_lock_obj, mp_micropython_heap_lock); |
130 | |
131 | STATIC mp_obj_t mp_micropython_heap_unlock(void) { |
132 | gc_unlock(); |
133 | return MP_OBJ_NEW_SMALL_INT(MP_STATE_MEM(gc_lock_depth)); |
134 | } |
135 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_unlock_obj, mp_micropython_heap_unlock); |
136 | |
137 | #if MICROPY_PY_MICROPYTHON_HEAP_LOCKED |
138 | STATIC mp_obj_t mp_micropython_heap_locked(void) { |
139 | return MP_OBJ_NEW_SMALL_INT(MP_STATE_MEM(gc_lock_depth)); |
140 | } |
141 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_locked_obj, mp_micropython_heap_locked); |
142 | #endif |
143 | #endif |
144 | |
145 | #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0) |
146 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_emergency_exception_buf); |
147 | #endif |
148 | |
149 | #if MICROPY_KBD_EXCEPTION |
150 | STATIC mp_obj_t mp_micropython_kbd_intr(mp_obj_t int_chr_in) { |
151 | mp_hal_set_interrupt_char(mp_obj_get_int(int_chr_in)); |
152 | return mp_const_none; |
153 | } |
154 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_micropython_kbd_intr_obj, mp_micropython_kbd_intr); |
155 | #endif |
156 | |
157 | #if MICROPY_ENABLE_SCHEDULER |
158 | STATIC mp_obj_t mp_micropython_schedule(mp_obj_t function, mp_obj_t arg) { |
159 | if (!mp_sched_schedule(function, arg)) { |
160 | mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("schedule queue full" )); |
161 | } |
162 | return mp_const_none; |
163 | } |
164 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_micropython_schedule_obj, mp_micropython_schedule); |
165 | #endif |
166 | |
167 | STATIC const mp_rom_map_elem_t mp_module_micropython_globals_table[] = { |
168 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_micropython) }, |
169 | { MP_ROM_QSTR(MP_QSTR_const), MP_ROM_PTR(&mp_identity_obj) }, |
170 | #if MICROPY_ENABLE_COMPILER |
171 | { MP_ROM_QSTR(MP_QSTR_opt_level), MP_ROM_PTR(&mp_micropython_opt_level_obj) }, |
172 | #endif |
173 | #if MICROPY_PY_MICROPYTHON_MEM_INFO |
174 | #if MICROPY_MEM_STATS |
175 | { MP_ROM_QSTR(MP_QSTR_mem_total), MP_ROM_PTR(&mp_micropython_mem_total_obj) }, |
176 | { MP_ROM_QSTR(MP_QSTR_mem_current), MP_ROM_PTR(&mp_micropython_mem_current_obj) }, |
177 | { MP_ROM_QSTR(MP_QSTR_mem_peak), MP_ROM_PTR(&mp_micropython_mem_peak_obj) }, |
178 | #endif |
179 | { MP_ROM_QSTR(MP_QSTR_mem_info), MP_ROM_PTR(&mp_micropython_mem_info_obj) }, |
180 | { MP_ROM_QSTR(MP_QSTR_qstr_info), MP_ROM_PTR(&mp_micropython_qstr_info_obj) }, |
181 | #endif |
182 | #if MICROPY_PY_MICROPYTHON_STACK_USE |
183 | { MP_ROM_QSTR(MP_QSTR_stack_use), MP_ROM_PTR(&mp_micropython_stack_use_obj) }, |
184 | #endif |
185 | #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0) |
186 | { MP_ROM_QSTR(MP_QSTR_alloc_emergency_exception_buf), MP_ROM_PTR(&mp_alloc_emergency_exception_buf_obj) }, |
187 | #endif |
188 | #if MICROPY_ENABLE_PYSTACK |
189 | { MP_ROM_QSTR(MP_QSTR_pystack_use), MP_ROM_PTR(&mp_micropython_pystack_use_obj) }, |
190 | #endif |
191 | #if MICROPY_ENABLE_GC |
192 | { MP_ROM_QSTR(MP_QSTR_heap_lock), MP_ROM_PTR(&mp_micropython_heap_lock_obj) }, |
193 | { MP_ROM_QSTR(MP_QSTR_heap_unlock), MP_ROM_PTR(&mp_micropython_heap_unlock_obj) }, |
194 | #if MICROPY_PY_MICROPYTHON_HEAP_LOCKED |
195 | { MP_ROM_QSTR(MP_QSTR_heap_locked), MP_ROM_PTR(&mp_micropython_heap_locked_obj) }, |
196 | #endif |
197 | #endif |
198 | #if MICROPY_KBD_EXCEPTION |
199 | { MP_ROM_QSTR(MP_QSTR_kbd_intr), MP_ROM_PTR(&mp_micropython_kbd_intr_obj) }, |
200 | #endif |
201 | #if MICROPY_ENABLE_SCHEDULER |
202 | { MP_ROM_QSTR(MP_QSTR_schedule), MP_ROM_PTR(&mp_micropython_schedule_obj) }, |
203 | #endif |
204 | }; |
205 | |
206 | STATIC MP_DEFINE_CONST_DICT(mp_module_micropython_globals, mp_module_micropython_globals_table); |
207 | |
208 | const mp_obj_module_t mp_module_micropython = { |
209 | .base = { &mp_type_module }, |
210 | .globals = (mp_obj_dict_t *)&mp_module_micropython_globals, |
211 | }; |
212 | |