dynruntime.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2019 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_DYNRUNTIME_H
  27. #define MICROPY_INCLUDED_PY_DYNRUNTIME_H
  28. // This header file contains definitions to dynamically implement the static
  29. // MicroPython runtime API defined in py/obj.h and py/runtime.h.
  30. #include "py/nativeglue.h"
  31. #include "py/objstr.h"
  32. #undef MP_ROM_QSTR
  33. #undef MP_OBJ_QSTR_VALUE
  34. #undef MP_OBJ_NEW_QSTR
  35. #undef mp_const_none
  36. #undef mp_const_false
  37. #undef mp_const_true
  38. #undef mp_const_empty_tuple
  39. #undef nlr_raise
  40. /******************************************************************************/
  41. // Memory allocation
  42. #define m_malloc(n) (m_malloc_dyn((n)))
  43. #define m_free(ptr) (m_free_dyn((ptr)))
  44. #define m_realloc(ptr, new_num_bytes) (m_realloc_dyn((ptr), (new_num_bytes)))
  45. static inline void *m_malloc_dyn(size_t n) {
  46. // TODO won't raise on OOM
  47. return mp_fun_table.realloc_(NULL, n, false);
  48. }
  49. static inline void m_free_dyn(void *ptr) {
  50. mp_fun_table.realloc_(ptr, 0, false);
  51. }
  52. static inline void *m_realloc_dyn(void *ptr, size_t new_num_bytes) {
  53. // TODO won't raise on OOM
  54. return mp_fun_table.realloc_(ptr, new_num_bytes, true);
  55. }
  56. /******************************************************************************/
  57. // Printing
  58. #define mp_plat_print (*mp_fun_table.plat_print)
  59. #define mp_printf(p, ...) (mp_fun_table.printf_((p), __VA_ARGS__))
  60. #define mp_vprintf(p, fmt, args) (mp_fun_table.vprintf_((p), (fmt), (args)))
  61. /******************************************************************************/
  62. // Types and objects
  63. #define MP_OBJ_NEW_QSTR(x) MP_OBJ_NEW_QSTR_ ## x
  64. #define mp_type_type (*mp_fun_table.type_type)
  65. #define mp_type_str (*mp_fun_table.type_str)
  66. #define mp_type_list (*mp_fun_table.type_list)
  67. #define mp_type_EOFError (*(mp_obj_type_t*)(mp_load_global(MP_QSTR_EOFError)))
  68. #define mp_type_IndexError (*(mp_obj_type_t*)(mp_load_global(MP_QSTR_IndexError)))
  69. #define mp_type_KeyError (*(mp_obj_type_t*)(mp_load_global(MP_QSTR_KeyError)))
  70. #define mp_type_NotImplementedError (*(mp_obj_type_t*)(mp_load_global(MP_QSTR_NotImplementedError)))
  71. #define mp_type_RuntimeError (*(mp_obj_type_t*)(mp_load_global(MP_QSTR_RuntimeError)))
  72. #define mp_type_TypeError (*(mp_obj_type_t*)(mp_load_global(MP_QSTR_TypeError)))
  73. #define mp_type_ValueError (*(mp_obj_type_t*)(mp_load_global(MP_QSTR_ValueError)))
  74. #define mp_stream_read_obj (*mp_fun_table.stream_read_obj)
  75. #define mp_stream_readinto_obj (*mp_fun_table.stream_readinto_obj)
  76. #define mp_stream_unbuffered_readline_obj (*mp_fun_table.stream_unbuffered_readline_obj)
  77. #define mp_stream_write_obj (*mp_fun_table.stream_write_obj)
  78. #define mp_const_none ((mp_obj_t)mp_fun_table.const_none)
  79. #define mp_const_false ((mp_obj_t)mp_fun_table.const_false)
  80. #define mp_const_true ((mp_obj_t)mp_fun_table.const_true)
  81. #define mp_const_empty_tuple (mp_fun_table.new_tuple(0, NULL))
  82. #define mp_obj_new_bool(b) ((b) ? (mp_obj_t)mp_fun_table.const_true : (mp_obj_t)mp_fun_table.const_false)
  83. #define mp_obj_new_int(i) (mp_fun_table.native_to_obj(i, MP_NATIVE_TYPE_INT))
  84. #define mp_obj_new_int_from_uint(i) (mp_fun_table.native_to_obj(i, MP_NATIVE_TYPE_UINT))
  85. #define mp_obj_new_str(data, len) (mp_fun_table.obj_new_str((data), (len)))
  86. #define mp_obj_new_str_of_type(t, d, l) (mp_obj_new_str_of_type_dyn((t), (d), (l)))
  87. #define mp_obj_new_bytes(data, len) (mp_fun_table.obj_new_bytes((data), (len)))
  88. #define mp_obj_new_bytearray_by_ref(n, i) (mp_fun_table.obj_new_bytearray_by_ref((n), (i)))
  89. #define mp_obj_new_tuple(n, items) (mp_fun_table.new_tuple((n), (items)))
  90. #define mp_obj_new_list(n, items) (mp_fun_table.new_list((n), (items)))
  91. #define mp_obj_get_type(o) (mp_fun_table.obj_get_type((o)))
  92. #define mp_obj_get_int(o) (mp_fun_table.native_from_obj(o, MP_NATIVE_TYPE_INT))
  93. #define mp_obj_get_int_truncated(o) (mp_fun_table.native_from_obj(o, MP_NATIVE_TYPE_UINT))
  94. #define mp_obj_str_get_str(s) ((void*)mp_fun_table.native_from_obj(s, MP_NATIVE_TYPE_PTR))
  95. #define mp_obj_str_get_data(o, len) (mp_obj_str_get_data_dyn((o), (len)))
  96. #define mp_get_buffer_raise(o, bufinfo, fl) (mp_fun_table.get_buffer_raise((o), (bufinfo), (fl)))
  97. #define mp_get_stream_raise(s, flags) (mp_fun_table.get_stream_raise((s), (flags)))
  98. #define mp_obj_len(o) (mp_obj_len_dyn(o))
  99. #define mp_obj_subscr(base, index, val) (mp_fun_table.obj_subscr((base), (index), (val)))
  100. #define mp_obj_list_append(list, item) (mp_fun_table.list_append((list), (item)))
  101. static inline mp_obj_t mp_obj_new_str_of_type_dyn(const mp_obj_type_t *type, const byte* data, size_t len) {
  102. if (type == &mp_type_str) {
  103. return mp_obj_new_str((const char*)data, len);
  104. } else {
  105. return mp_obj_new_bytes(data, len);
  106. }
  107. }
  108. static inline void *mp_obj_str_get_data_dyn(mp_obj_t o, size_t *l) {
  109. mp_buffer_info_t bufinfo;
  110. mp_get_buffer_raise(o, &bufinfo, MP_BUFFER_READ);
  111. *l = bufinfo.len;
  112. return bufinfo.buf;
  113. }
  114. static inline mp_obj_t mp_obj_len_dyn(mp_obj_t o) {
  115. // If bytes implemented MP_UNARY_OP_LEN could use: mp_unary_op(MP_UNARY_OP_LEN, o)
  116. return mp_fun_table.call_function_n_kw(mp_fun_table.load_name(MP_QSTR_len), 1, &o);
  117. }
  118. /******************************************************************************/
  119. // General runtime functions
  120. #define mp_load_name(qst) (mp_fun_table.load_name(qst))
  121. #define mp_load_global(qst) (mp_fun_table.load_global(qst))
  122. #define mp_store_global(qst, obj) (mp_fun_table.store_global((qst), (obj)))
  123. #define mp_unary_op(op, obj) (mp_fun_table.unary_op((op), (obj)))
  124. #define mp_binary_op(op, lhs, rhs) (mp_fun_table.binary_op((op), (lhs), (rhs)))
  125. #define mp_make_function_from_raw_code(rc, def_args, def_kw_args) \
  126. (mp_fun_table.make_function_from_raw_code((rc), (def_args), (def_kw_args)))
  127. #define mp_call_function_n_kw(fun, n_args, n_kw, args) \
  128. (mp_fun_table.call_function_n_kw((fun), (n_args) | ((n_kw) << 8), args))
  129. #define mp_arg_check_num(n_args, n_kw, n_args_min, n_args_max, takes_kw) \
  130. (mp_fun_table.arg_check_num_sig((n_args), (n_kw), MP_OBJ_FUN_MAKE_SIG((n_args_min), (n_args_max), (takes_kw))))
  131. #define MP_DYNRUNTIME_INIT_ENTRY \
  132. mp_obj_t old_globals = mp_fun_table.swap_globals(self->globals); \
  133. mp_raw_code_t rc; \
  134. rc.kind = MP_CODE_NATIVE_VIPER; \
  135. rc.scope_flags = 0; \
  136. rc.const_table = (void*)self->const_table; \
  137. (void)rc;
  138. #define MP_DYNRUNTIME_INIT_EXIT \
  139. mp_fun_table.swap_globals(old_globals); \
  140. return mp_const_none;
  141. #define MP_DYNRUNTIME_MAKE_FUNCTION(f) \
  142. (mp_make_function_from_raw_code((rc.fun_data = (f), &rc), MP_OBJ_NULL, MP_OBJ_NULL))
  143. /******************************************************************************/
  144. // Exceptions
  145. #define mp_obj_new_exception(o) ((mp_obj_t)(o)) // Assumes returned object will be raised, will create instance then
  146. #define mp_obj_new_exception_arg1(e_type, arg) (mp_obj_new_exception_arg1_dyn((e_type), (arg)))
  147. #define nlr_raise(o) (mp_raise_dyn(o))
  148. #define mp_raise_msg(type, msg) (mp_fun_table.raise_msg((type), (msg)))
  149. #define mp_raise_OSError(er) (mp_raise_OSError_dyn(er))
  150. #define mp_raise_NotImplementedError(msg) (mp_raise_msg(&mp_type_NotImplementedError, (msg)))
  151. #define mp_raise_TypeError(msg) (mp_raise_msg(&mp_type_TypeError, (msg)))
  152. #define mp_raise_ValueError(msg) (mp_raise_msg(&mp_type_ValueError, (msg)))
  153. static inline mp_obj_t mp_obj_new_exception_arg1_dyn(const mp_obj_type_t *exc_type, mp_obj_t arg) {
  154. mp_obj_t args[1] = { arg };
  155. return mp_call_function_n_kw(MP_OBJ_FROM_PTR(exc_type), 1, 0, &args[0]);
  156. }
  157. static NORETURN inline void mp_raise_dyn(mp_obj_t o) {
  158. mp_fun_table.raise(o);
  159. for (;;) {
  160. }
  161. }
  162. static inline void mp_raise_OSError_dyn(int er) {
  163. mp_obj_t args[1] = { MP_OBJ_NEW_SMALL_INT(er) };
  164. nlr_raise(mp_call_function_n_kw(mp_load_global(MP_QSTR_OSError), 1, 0, &args[0]));
  165. }
  166. /******************************************************************************/
  167. // Floating point
  168. #define mp_obj_new_float_from_f(f) (mp_fun_table.obj_new_float_from_f((f)))
  169. #define mp_obj_new_float_from_d(d) (mp_fun_table.obj_new_float_from_d((d)))
  170. #define mp_obj_get_float_to_f(o) (mp_fun_table.obj_get_float_to_f((o)))
  171. #define mp_obj_get_float_to_d(o) (mp_fun_table.obj_get_float_to_d((o)))
  172. #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
  173. #define mp_obj_new_float(f) (mp_obj_new_float_from_f((f)))
  174. #define mp_obj_get_float(o) (mp_obj_get_float_to_f((o)))
  175. #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
  176. #define mp_obj_new_float(f) (mp_obj_new_float_from_d((f)))
  177. #define mp_obj_get_float(o) (mp_obj_get_float_to_d((o)))
  178. #endif
  179. #endif // MICROPY_INCLUDED_PY_DYNRUNTIME_H