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 | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 27 | #include <stdio.h> |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 28 | |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 29 | #include "py/mpstate.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 30 | #include "py/builtin.h" |
| 31 | #include "py/stackctrl.h" |
Damien George | 6e74d24 | 2017-02-16 18:05:06 +1100 | [diff] [blame] | 32 | #include "py/runtime.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 33 | #include "py/gc.h" |
Paul Sokolovsky | 440cc3f | 2014-01-20 01:53:15 +0200 | [diff] [blame] | 34 | |
| 35 | // Various builtins specific to MicroPython runtime, |
| 36 | // living in micropython module |
| 37 | |
Damien George | 7dc2345 | 2016-10-07 15:59:50 +1100 | [diff] [blame] | 38 | STATIC mp_obj_t mp_micropython_opt_level(size_t n_args, const mp_obj_t *args) { |
| 39 | if (n_args == 0) { |
| 40 | return MP_OBJ_NEW_SMALL_INT(MP_STATE_VM(mp_optimise_value)); |
| 41 | } else { |
| 42 | MP_STATE_VM(mp_optimise_value) = mp_obj_get_int(args[0]); |
| 43 | return mp_const_none; |
| 44 | } |
| 45 | } |
| 46 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_opt_level_obj, 0, 1, mp_micropython_opt_level); |
| 47 | |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 48 | #if MICROPY_PY_MICROPYTHON_MEM_INFO |
| 49 | |
Paul Sokolovsky | 440cc3f | 2014-01-20 01:53:15 +0200 | [diff] [blame] | 50 | #if MICROPY_MEM_STATS |
Damien George | abc1959 | 2015-01-12 22:34:38 +0000 | [diff] [blame] | 51 | STATIC mp_obj_t mp_micropython_mem_total(void) { |
Damien George | bb4c6f3 | 2014-07-31 10:49:14 +0100 | [diff] [blame] | 52 | return MP_OBJ_NEW_SMALL_INT(m_get_total_bytes_allocated()); |
Paul Sokolovsky | 440cc3f | 2014-01-20 01:53:15 +0200 | [diff] [blame] | 53 | } |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 54 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_total_obj, mp_micropython_mem_total); |
Paul Sokolovsky | 440cc3f | 2014-01-20 01:53:15 +0200 | [diff] [blame] | 55 | |
Damien George | abc1959 | 2015-01-12 22:34:38 +0000 | [diff] [blame] | 56 | STATIC mp_obj_t mp_micropython_mem_current(void) { |
Damien George | bb4c6f3 | 2014-07-31 10:49:14 +0100 | [diff] [blame] | 57 | return MP_OBJ_NEW_SMALL_INT(m_get_current_bytes_allocated()); |
Paul Sokolovsky | 440cc3f | 2014-01-20 01:53:15 +0200 | [diff] [blame] | 58 | } |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 59 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_current_obj, mp_micropython_mem_current); |
Paul Sokolovsky | 440cc3f | 2014-01-20 01:53:15 +0200 | [diff] [blame] | 60 | |
Damien George | abc1959 | 2015-01-12 22:34:38 +0000 | [diff] [blame] | 61 | STATIC mp_obj_t mp_micropython_mem_peak(void) { |
Damien George | bb4c6f3 | 2014-07-31 10:49:14 +0100 | [diff] [blame] | 62 | return MP_OBJ_NEW_SMALL_INT(m_get_peak_bytes_allocated()); |
Paul Sokolovsky | 440cc3f | 2014-01-20 01:53:15 +0200 | [diff] [blame] | 63 | } |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 64 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_peak_obj, mp_micropython_mem_peak); |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 65 | #endif |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 66 | |
Damien George | 4b72b3a | 2016-01-03 14:21:40 +0000 | [diff] [blame] | 67 | mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 68 | (void)args; |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 69 | #if MICROPY_MEM_STATS |
Damien George | e72cda9 | 2015-04-11 12:15:47 +0100 | [diff] [blame] | 70 | mp_printf(&mp_plat_print, "mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n", |
Damien George | e5039c6 | 2015-02-15 13:17:11 +0000 | [diff] [blame] | 71 | (mp_uint_t)m_get_total_bytes_allocated(), (mp_uint_t)m_get_current_bytes_allocated(), (mp_uint_t)m_get_peak_bytes_allocated()); |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 72 | #endif |
| 73 | #if MICROPY_STACK_CHECK |
Damien George | 330165a | 2016-04-22 22:44:56 +0000 | [diff] [blame] | 74 | mp_printf(&mp_plat_print, "stack: " UINT_FMT " out of " INT_FMT "\n", mp_stack_usage(), MP_STATE_THREAD(stack_limit)); |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 75 | #else |
Damien George | e72cda9 | 2015-04-11 12:15:47 +0100 | [diff] [blame] | 76 | mp_printf(&mp_plat_print, "stack: " UINT_FMT "\n", mp_stack_usage()); |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 77 | #endif |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 78 | #if MICROPY_ENABLE_GC |
| 79 | gc_dump_info(); |
| 80 | if (n_args == 1) { |
| 81 | // arg given means dump gc allocation table |
| 82 | gc_dump_alloc_table(); |
| 83 | } |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 84 | #else |
| 85 | (void)n_args; |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 86 | #endif |
| 87 | return mp_const_none; |
| 88 | } |
| 89 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_mem_info_obj, 0, 1, mp_micropython_mem_info); |
| 90 | |
Damien George | 4b72b3a | 2016-01-03 14:21:40 +0000 | [diff] [blame] | 91 | STATIC mp_obj_t mp_micropython_qstr_info(size_t n_args, const mp_obj_t *args) { |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 92 | (void)args; |
Damien George | 2578485 | 2015-12-17 12:41:40 +0000 | [diff] [blame] | 93 | size_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes; |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 94 | qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes); |
Damien George | 2578485 | 2015-12-17 12:41:40 +0000 | [diff] [blame] | 95 | mp_printf(&mp_plat_print, "qstr pool: n_pool=%u, n_qstr=%u, n_str_data_bytes=%u, n_total_bytes=%u\n", |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 96 | n_pool, n_qstr, n_str_data_bytes, n_total_bytes); |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 97 | if (n_args == 1) { |
| 98 | // arg given means dump qstr data |
| 99 | qstr_dump_data(); |
| 100 | } |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 101 | return mp_const_none; |
| 102 | } |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 103 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_qstr_info_obj, 0, 1, mp_micropython_qstr_info); |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 104 | |
Damien George | df4ce93 | 2016-01-22 16:16:38 +0000 | [diff] [blame] | 105 | #if MICROPY_STACK_CHECK |
| 106 | STATIC mp_obj_t mp_micropython_stack_use(void) { |
| 107 | return MP_OBJ_NEW_SMALL_INT(mp_stack_usage()); |
| 108 | } |
| 109 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_stack_use_obj, mp_micropython_stack_use); |
| 110 | #endif |
| 111 | |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 112 | #endif // MICROPY_PY_MICROPYTHON_MEM_INFO |
Damien George | 91d457a | 2014-01-20 10:30:24 +0000 | [diff] [blame] | 113 | |
Damien George | df4ce93 | 2016-01-22 16:16:38 +0000 | [diff] [blame] | 114 | #if MICROPY_ENABLE_GC |
| 115 | STATIC mp_obj_t mp_micropython_heap_lock(void) { |
| 116 | gc_lock(); |
| 117 | return mp_const_none; |
| 118 | } |
| 119 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_lock_obj, mp_micropython_heap_lock); |
| 120 | |
| 121 | STATIC mp_obj_t mp_micropython_heap_unlock(void) { |
| 122 | gc_unlock(); |
| 123 | return mp_const_none; |
| 124 | } |
| 125 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_unlock_obj, mp_micropython_heap_unlock); |
| 126 | #endif |
| 127 | |
Dave Hylands | 5b7fd20 | 2014-07-01 23:46:53 -0700 | [diff] [blame] | 128 | #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0) |
| 129 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_emergency_exception_buf); |
| 130 | #endif |
| 131 | |
Damien George | 6e74d24 | 2017-02-16 18:05:06 +1100 | [diff] [blame] | 132 | #if MICROPY_ENABLE_SCHEDULER |
| 133 | STATIC mp_obj_t mp_micropython_schedule(mp_obj_t function, mp_obj_t arg) { |
| 134 | if (!mp_sched_schedule(function, arg)) { |
| 135 | mp_raise_msg(&mp_type_RuntimeError, "schedule stack full"); |
| 136 | } |
| 137 | return mp_const_none; |
| 138 | } |
| 139 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_micropython_schedule_obj, mp_micropython_schedule); |
| 140 | #endif |
| 141 | |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 142 | STATIC const mp_rom_map_elem_t mp_module_micropython_globals_table[] = { |
| 143 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_micropython) }, |
Damien George | 791b65f | 2016-09-27 13:34:21 +1000 | [diff] [blame] | 144 | { MP_ROM_QSTR(MP_QSTR_const), MP_ROM_PTR(&mp_identity_obj) }, |
Damien George | 7dc2345 | 2016-10-07 15:59:50 +1100 | [diff] [blame] | 145 | { MP_ROM_QSTR(MP_QSTR_opt_level), MP_ROM_PTR(&mp_micropython_opt_level_obj) }, |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 146 | #if MICROPY_PY_MICROPYTHON_MEM_INFO |
Damien George | 91d457a | 2014-01-20 10:30:24 +0000 | [diff] [blame] | 147 | #if MICROPY_MEM_STATS |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 148 | { MP_ROM_QSTR(MP_QSTR_mem_total), MP_ROM_PTR(&mp_micropython_mem_total_obj) }, |
| 149 | { MP_ROM_QSTR(MP_QSTR_mem_current), MP_ROM_PTR(&mp_micropython_mem_current_obj) }, |
| 150 | { MP_ROM_QSTR(MP_QSTR_mem_peak), MP_ROM_PTR(&mp_micropython_mem_peak_obj) }, |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 151 | #endif |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 152 | { MP_ROM_QSTR(MP_QSTR_mem_info), MP_ROM_PTR(&mp_micropython_mem_info_obj) }, |
| 153 | { MP_ROM_QSTR(MP_QSTR_qstr_info), MP_ROM_PTR(&mp_micropython_qstr_info_obj) }, |
Damien George | df4ce93 | 2016-01-22 16:16:38 +0000 | [diff] [blame] | 154 | #if MICROPY_STACK_CHECK |
| 155 | { MP_ROM_QSTR(MP_QSTR_stack_use), MP_ROM_PTR(&mp_micropython_stack_use_obj) }, |
| 156 | #endif |
Damien George | 91d457a | 2014-01-20 10:30:24 +0000 | [diff] [blame] | 157 | #endif |
Dave Hylands | 5b7fd20 | 2014-07-01 23:46:53 -0700 | [diff] [blame] | 158 | #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0) |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 159 | { MP_ROM_QSTR(MP_QSTR_alloc_emergency_exception_buf), MP_ROM_PTR(&mp_alloc_emergency_exception_buf_obj) }, |
Dave Hylands | 5b7fd20 | 2014-07-01 23:46:53 -0700 | [diff] [blame] | 160 | #endif |
Damien George | df4ce93 | 2016-01-22 16:16:38 +0000 | [diff] [blame] | 161 | #if MICROPY_ENABLE_GC |
| 162 | { MP_ROM_QSTR(MP_QSTR_heap_lock), MP_ROM_PTR(&mp_micropython_heap_lock_obj) }, |
| 163 | { MP_ROM_QSTR(MP_QSTR_heap_unlock), MP_ROM_PTR(&mp_micropython_heap_unlock_obj) }, |
| 164 | #endif |
Damien George | 6e74d24 | 2017-02-16 18:05:06 +1100 | [diff] [blame] | 165 | #if MICROPY_ENABLE_SCHEDULER |
| 166 | { MP_ROM_QSTR(MP_QSTR_schedule), MP_ROM_PTR(&mp_micropython_schedule_obj) }, |
| 167 | #endif |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
Damien George | 3b603f2 | 2014-11-29 14:39:27 +0000 | [diff] [blame] | 170 | STATIC MP_DEFINE_CONST_DICT(mp_module_micropython_globals, mp_module_micropython_globals_table); |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 171 | |
| 172 | const mp_obj_module_t mp_module_micropython = { |
| 173 | .base = { &mp_type_module }, |
Damien George | 8b0535e | 2014-04-05 21:53:54 +0100 | [diff] [blame] | 174 | .globals = (mp_obj_dict_t*)&mp_module_micropython_globals, |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 175 | }; |