runtime.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #ifndef MICROPY_INCLUDED_PY_RUNTIME_H
  27. #define MICROPY_INCLUDED_PY_RUNTIME_H
  28. #include "py/mpstate.h"
  29. #include "py/pystack.h"
  30. #include "py/stackctrl.h"
  31. // For use with mp_call_function_1_from_nlr_jump_callback.
  32. #define MP_DEFINE_NLR_JUMP_CALLBACK_FUNCTION_1(ctx, f, a) \
  33. nlr_jump_callback_node_call_function_1_t ctx = { \
  34. .func = (void (*)(void *))(f), \
  35. .arg = (a), \
  36. }
  37. typedef enum {
  38. MP_VM_RETURN_NORMAL,
  39. MP_VM_RETURN_YIELD,
  40. MP_VM_RETURN_EXCEPTION,
  41. } mp_vm_return_kind_t;
  42. typedef enum {
  43. MP_ARG_BOOL = 0x001,
  44. MP_ARG_INT = 0x002,
  45. MP_ARG_OBJ = 0x003,
  46. MP_ARG_KIND_MASK = 0x0ff,
  47. MP_ARG_REQUIRED = 0x100,
  48. MP_ARG_KW_ONLY = 0x200,
  49. } mp_arg_flag_t;
  50. typedef union _mp_arg_val_t {
  51. bool u_bool;
  52. mp_int_t u_int;
  53. mp_obj_t u_obj;
  54. mp_rom_obj_t u_rom_obj;
  55. } mp_arg_val_t;
  56. typedef struct _mp_arg_t {
  57. uint16_t qst;
  58. uint16_t flags;
  59. mp_arg_val_t defval;
  60. } mp_arg_t;
  61. struct _mp_sched_node_t;
  62. typedef void (*mp_sched_callback_t)(struct _mp_sched_node_t *);
  63. typedef struct _mp_sched_node_t {
  64. mp_sched_callback_t callback;
  65. struct _mp_sched_node_t *next;
  66. } mp_sched_node_t;
  67. // For use with mp_globals_locals_set_from_nlr_jump_callback.
  68. typedef struct _nlr_jump_callback_node_globals_locals_t {
  69. nlr_jump_callback_node_t callback;
  70. mp_obj_dict_t *globals;
  71. mp_obj_dict_t *locals;
  72. } nlr_jump_callback_node_globals_locals_t;
  73. // For use with mp_call_function_1_from_nlr_jump_callback.
  74. typedef struct _nlr_jump_callback_node_call_function_1_t {
  75. nlr_jump_callback_node_t callback;
  76. void (*func)(void *);
  77. void *arg;
  78. } nlr_jump_callback_node_call_function_1_t;
  79. // Tables mapping operator enums to qstrs, defined in objtype.c
  80. extern const byte mp_unary_op_method_name[];
  81. extern const byte mp_binary_op_method_name[];
  82. void mp_init(void);
  83. void mp_deinit(void);
  84. void mp_sched_exception(mp_obj_t exc);
  85. void mp_sched_keyboard_interrupt(void);
  86. #if MICROPY_ENABLE_VM_ABORT
  87. void mp_sched_vm_abort(void);
  88. #endif
  89. void mp_handle_pending(bool raise_exc);
  90. #if MICROPY_ENABLE_SCHEDULER
  91. void mp_sched_lock(void);
  92. void mp_sched_unlock(void);
  93. #define mp_sched_num_pending() (MP_STATE_VM(sched_len))
  94. bool mp_sched_schedule(mp_obj_t function, mp_obj_t arg);
  95. bool mp_sched_schedule_node(mp_sched_node_t *node, mp_sched_callback_t callback);
  96. #endif
  97. // Handles any pending MicroPython events without waiting for an interrupt or event.
  98. void mp_event_handle_nowait(void);
  99. // Handles any pending MicroPython events and then suspends execution until the
  100. // next interrupt or event.
  101. //
  102. // Note: on "tickless" ports this can suspend execution for a long time,
  103. // don't call unless you know an interrupt is coming to continue execution.
  104. // On "ticked" ports it may return early due to the tick interrupt.
  105. void mp_event_wait_indefinite(void);
  106. // Handle any pending MicroPython events and then suspends execution until the
  107. // next interrupt or event, or until timeout_ms milliseconds have elapsed.
  108. //
  109. // On "ticked" ports it may return early due to the tick interrupt.
  110. void mp_event_wait_ms(mp_uint_t timeout_ms);
  111. // extra printing method specifically for mp_obj_t's which are integral type
  112. int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, int base, int base_char, int flags, char fill, int width, int prec);
  113. void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig);
  114. static inline void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw) {
  115. mp_arg_check_num_sig(n_args, n_kw, MP_OBJ_FUN_MAKE_SIG(n_args_min, n_args_max, takes_kw));
  116. }
  117. void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
  118. void mp_arg_parse_all_kw_array(size_t n_pos, size_t n_kw, const mp_obj_t *args, size_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
  119. NORETURN void mp_arg_error_terse_mismatch(void);
  120. NORETURN void mp_arg_error_unimpl_kw(void);
  121. static inline mp_obj_dict_t *mp_locals_get(void) {
  122. return MP_STATE_THREAD(dict_locals);
  123. }
  124. static inline void mp_locals_set(mp_obj_dict_t *d) {
  125. MP_STATE_THREAD(dict_locals) = d;
  126. }
  127. static inline mp_obj_dict_t *mp_globals_get(void) {
  128. return MP_STATE_THREAD(dict_globals);
  129. }
  130. static inline void mp_globals_set(mp_obj_dict_t *d) {
  131. MP_STATE_THREAD(dict_globals) = d;
  132. }
  133. void mp_globals_locals_set_from_nlr_jump_callback(void *ctx_in);
  134. void mp_call_function_1_from_nlr_jump_callback(void *ctx_in);
  135. #if MICROPY_PY_THREAD
  136. static inline void mp_thread_init_state(mp_state_thread_t *ts, size_t stack_size, mp_obj_dict_t *locals, mp_obj_dict_t *globals) {
  137. mp_thread_set_state(ts);
  138. mp_stack_set_top(ts + 1); // need to include ts in root-pointer scan
  139. mp_stack_set_limit(stack_size);
  140. // GC starts off unlocked
  141. ts->gc_lock_depth = 0;
  142. // There are no pending jump callbacks or exceptions yet
  143. ts->nlr_jump_callback_top = NULL;
  144. ts->mp_pending_exception = MP_OBJ_NULL;
  145. // If locals/globals are not given, inherit from main thread
  146. if (locals == NULL) {
  147. locals = mp_state_ctx.thread.dict_locals;
  148. }
  149. if (globals == NULL) {
  150. globals = mp_state_ctx.thread.dict_globals;
  151. }
  152. mp_locals_set(locals);
  153. mp_globals_set(globals);
  154. }
  155. #endif
  156. mp_obj_t mp_load_name(qstr qst);
  157. mp_obj_t mp_load_global(qstr qst);
  158. mp_obj_t mp_load_build_class(void);
  159. void mp_store_name(qstr qst, mp_obj_t obj);
  160. void mp_store_global(qstr qst, mp_obj_t obj);
  161. void mp_delete_name(qstr qst);
  162. void mp_delete_global(qstr qst);
  163. mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg);
  164. mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs);
  165. mp_obj_t mp_call_function_0(mp_obj_t fun);
  166. mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg);
  167. mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
  168. mp_obj_t mp_call_function_n_kw(mp_obj_t fun, size_t n_args, size_t n_kw, const mp_obj_t *args);
  169. mp_obj_t mp_call_method_n_kw(size_t n_args, size_t n_kw, const mp_obj_t *args);
  170. mp_obj_t mp_call_method_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args);
  171. mp_obj_t mp_call_method_self_n_kw(mp_obj_t meth, mp_obj_t self, size_t n_args, size_t n_kw, const mp_obj_t *args);
  172. // Call function and catch/dump exception - for Python callbacks from C code
  173. // (return MP_OBJ_NULL in case of exception).
  174. mp_obj_t mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg);
  175. mp_obj_t mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
  176. typedef struct _mp_call_args_t {
  177. mp_obj_t fun;
  178. size_t n_args, n_kw, n_alloc;
  179. mp_obj_t *args;
  180. } mp_call_args_t;
  181. #if MICROPY_STACKLESS
  182. // Takes arguments which are the most general mix of Python arg types, and
  183. // prepares argument array suitable for passing to ->call() method of a
  184. // function object (and mp_call_function_n_kw()).
  185. // (Only needed in stackless mode.)
  186. void mp_call_prepare_args_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args, mp_call_args_t *out_args);
  187. #endif
  188. void mp_unpack_sequence(mp_obj_t seq, size_t num, mp_obj_t *items);
  189. void mp_unpack_ex(mp_obj_t seq, size_t num, mp_obj_t *items);
  190. mp_obj_t mp_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value);
  191. mp_obj_t mp_load_attr(mp_obj_t base, qstr attr);
  192. void mp_convert_member_lookup(mp_obj_t obj, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest);
  193. void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest);
  194. void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest);
  195. void mp_load_method_protected(mp_obj_t obj, qstr attr, mp_obj_t *dest, bool catch_all_exc);
  196. void mp_load_super_method(qstr attr, mp_obj_t *dest);
  197. void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val);
  198. mp_obj_t mp_getiter(mp_obj_t o, mp_obj_iter_buf_t *iter_buf);
  199. mp_obj_t mp_iternext_allow_raise(mp_obj_t o); // may return MP_OBJ_STOP_ITERATION instead of raising StopIteration()
  200. mp_obj_t mp_iternext(mp_obj_t o); // will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration(...)
  201. mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val);
  202. static inline mp_obj_t mp_make_stop_iteration(mp_obj_t o) {
  203. MP_STATE_THREAD(stop_iteration_arg) = o;
  204. return MP_OBJ_STOP_ITERATION;
  205. }
  206. mp_obj_t mp_make_raise_obj(mp_obj_t o);
  207. mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level);
  208. mp_obj_t mp_import_from(mp_obj_t module, qstr name);
  209. void mp_import_all(mp_obj_t module);
  210. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
  211. NORETURN void mp_raise_type(const mp_obj_type_t *exc_type);
  212. NORETURN void mp_raise_ValueError_no_msg(void);
  213. NORETURN void mp_raise_TypeError_no_msg(void);
  214. NORETURN void mp_raise_NotImplementedError_no_msg(void);
  215. #define mp_raise_msg(exc_type, msg) mp_raise_type(exc_type)
  216. #define mp_raise_msg_varg(exc_type, ...) mp_raise_type(exc_type)
  217. #define mp_raise_ValueError(msg) mp_raise_ValueError_no_msg()
  218. #define mp_raise_TypeError(msg) mp_raise_TypeError_no_msg()
  219. #define mp_raise_NotImplementedError(msg) mp_raise_NotImplementedError_no_msg()
  220. #else
  221. #define mp_raise_type(exc_type) mp_raise_msg(exc_type, NULL)
  222. NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg);
  223. NORETURN void mp_raise_msg_varg(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, ...);
  224. NORETURN void mp_raise_ValueError(mp_rom_error_text_t msg);
  225. NORETURN void mp_raise_TypeError(mp_rom_error_text_t msg);
  226. NORETURN void mp_raise_NotImplementedError(mp_rom_error_text_t msg);
  227. #endif
  228. NORETURN void mp_raise_type_arg(const mp_obj_type_t *exc_type, mp_obj_t arg);
  229. NORETURN void mp_raise_StopIteration(mp_obj_t arg);
  230. NORETURN void mp_raise_TypeError_int_conversion(mp_const_obj_t arg);
  231. NORETURN void mp_raise_OSError(int errno_);
  232. NORETURN void mp_raise_OSError_with_filename(int errno_, const char *filename);
  233. NORETURN void mp_raise_recursion_depth(void);
  234. #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
  235. #undef mp_check_self
  236. #define mp_check_self(pred)
  237. #else
  238. // A port may define to raise TypeError for example
  239. #ifndef mp_check_self
  240. #define mp_check_self(pred) assert(pred)
  241. #endif
  242. #endif
  243. // helper functions for native/viper code
  244. int mp_native_type_from_qstr(qstr qst);
  245. mp_uint_t mp_native_from_obj(mp_obj_t obj, mp_uint_t type);
  246. mp_obj_t mp_native_to_obj(mp_uint_t val, mp_uint_t type);
  247. #if MICROPY_PY_SYS_PATH
  248. #define mp_sys_path (MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_PATH]))
  249. #endif
  250. #if MICROPY_PY_SYS_ARGV
  251. #define mp_sys_argv (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)))
  252. #endif
  253. #if MICROPY_WARNINGS
  254. #ifndef mp_warning
  255. void mp_warning(const char *category, const char *msg, ...);
  256. #endif
  257. #else
  258. #define mp_warning(...)
  259. #endif
  260. #endif // MICROPY_INCLUDED_PY_RUNTIME_H