nativeglue.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * This file is part of the MicroPython 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. #include <stdio.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. #include "py/runtime.h"
  30. #include "py/smallint.h"
  31. #include "py/emitglue.h"
  32. #include "py/bc.h"
  33. #if MICROPY_DEBUG_VERBOSE // print debugging info
  34. #define DEBUG_printf DEBUG_printf
  35. #else // don't print debugging info
  36. #define DEBUG_printf(...) (void)0
  37. #endif
  38. #if MICROPY_EMIT_NATIVE
  39. // convert a MicroPython object to a valid native value based on type
  40. mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type) {
  41. DEBUG_printf("mp_convert_obj_to_native(%p, " UINT_FMT ")\n", obj, type);
  42. switch (type & 0xf) {
  43. case MP_NATIVE_TYPE_OBJ: return (mp_uint_t)obj;
  44. case MP_NATIVE_TYPE_BOOL:
  45. case MP_NATIVE_TYPE_INT:
  46. case MP_NATIVE_TYPE_UINT: return mp_obj_get_int_truncated(obj);
  47. default: { // cast obj to a pointer
  48. mp_buffer_info_t bufinfo;
  49. if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_RW)) {
  50. return (mp_uint_t)bufinfo.buf;
  51. } else {
  52. // assume obj is an integer that represents an address
  53. return mp_obj_get_int_truncated(obj);
  54. }
  55. }
  56. }
  57. }
  58. #endif
  59. #if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM
  60. // convert a native value to a MicroPython object based on type
  61. mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type) {
  62. DEBUG_printf("mp_convert_native_to_obj(" UINT_FMT ", " UINT_FMT ")\n", val, type);
  63. switch (type & 0xf) {
  64. case MP_NATIVE_TYPE_OBJ: return (mp_obj_t)val;
  65. case MP_NATIVE_TYPE_BOOL: return mp_obj_new_bool(val);
  66. case MP_NATIVE_TYPE_INT: return mp_obj_new_int(val);
  67. case MP_NATIVE_TYPE_UINT: return mp_obj_new_int_from_uint(val);
  68. default: // a pointer
  69. // we return just the value of the pointer as an integer
  70. return mp_obj_new_int_from_uint(val);
  71. }
  72. }
  73. #endif
  74. #if MICROPY_EMIT_NATIVE
  75. mp_obj_dict_t *mp_native_swap_globals(mp_obj_dict_t *new_globals) {
  76. if (new_globals == NULL) {
  77. // Globals were the originally the same so don't restore them
  78. return NULL;
  79. }
  80. mp_obj_dict_t *old_globals = mp_globals_get();
  81. if (old_globals == new_globals) {
  82. // Don't set globals if they are the same, and return NULL to indicate this
  83. return NULL;
  84. }
  85. mp_globals_set(new_globals);
  86. return old_globals;
  87. }
  88. // wrapper that accepts n_args and n_kw in one argument
  89. // (native emitter can only pass at most 3 arguments to a function)
  90. mp_obj_t mp_native_call_function_n_kw(mp_obj_t fun_in, size_t n_args_kw, const mp_obj_t *args) {
  91. return mp_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
  92. }
  93. // wrapper that makes raise obj and raises it
  94. // END_FINALLY opcode requires that we don't raise if o==None
  95. void mp_native_raise(mp_obj_t o) {
  96. if (o != MP_OBJ_NULL && o != mp_const_none) {
  97. nlr_raise(mp_make_raise_obj(o));
  98. }
  99. }
  100. // wrapper that handles iterator buffer
  101. STATIC mp_obj_t mp_native_getiter(mp_obj_t obj, mp_obj_iter_buf_t *iter) {
  102. if (iter == NULL) {
  103. return mp_getiter(obj, NULL);
  104. } else {
  105. obj = mp_getiter(obj, iter);
  106. if (obj != MP_OBJ_FROM_PTR(iter)) {
  107. // Iterator didn't use the stack so indicate that with MP_OBJ_NULL.
  108. iter->base.type = MP_OBJ_NULL;
  109. iter->buf[0] = obj;
  110. }
  111. return NULL;
  112. }
  113. }
  114. // wrapper that handles iterator buffer
  115. STATIC mp_obj_t mp_native_iternext(mp_obj_iter_buf_t *iter) {
  116. mp_obj_t obj;
  117. if (iter->base.type == MP_OBJ_NULL) {
  118. obj = iter->buf[0];
  119. } else {
  120. obj = MP_OBJ_FROM_PTR(iter);
  121. }
  122. return mp_iternext(obj);
  123. }
  124. STATIC bool mp_native_yield_from(mp_obj_t gen, mp_obj_t send_value, mp_obj_t *ret_value) {
  125. mp_vm_return_kind_t ret_kind;
  126. nlr_buf_t nlr_buf;
  127. mp_obj_t throw_value = *ret_value;
  128. if (nlr_push(&nlr_buf) == 0) {
  129. if (throw_value != MP_OBJ_NULL) {
  130. send_value = MP_OBJ_NULL;
  131. }
  132. ret_kind = mp_resume(gen, send_value, throw_value, ret_value);
  133. nlr_pop();
  134. } else {
  135. ret_kind = MP_VM_RETURN_EXCEPTION;
  136. *ret_value = nlr_buf.ret_val;
  137. }
  138. if (ret_kind == MP_VM_RETURN_YIELD) {
  139. return true;
  140. } else if (ret_kind == MP_VM_RETURN_NORMAL) {
  141. if (*ret_value == MP_OBJ_STOP_ITERATION) {
  142. *ret_value = mp_const_none;
  143. }
  144. } else {
  145. assert(ret_kind == MP_VM_RETURN_EXCEPTION);
  146. if (!mp_obj_exception_match(*ret_value, MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
  147. nlr_raise(*ret_value);
  148. }
  149. *ret_value = mp_obj_exception_get_value(*ret_value);
  150. }
  151. if (throw_value != MP_OBJ_NULL && mp_obj_exception_match(throw_value, MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) {
  152. nlr_raise(mp_make_raise_obj(throw_value));
  153. }
  154. return false;
  155. }
  156. // these must correspond to the respective enum in runtime0.h
  157. const void *const mp_fun_table[MP_F_NUMBER_OF] = {
  158. &mp_const_none_obj,
  159. &mp_const_false_obj,
  160. &mp_const_true_obj,
  161. mp_convert_obj_to_native,
  162. mp_convert_native_to_obj,
  163. mp_native_swap_globals,
  164. mp_load_name,
  165. mp_load_global,
  166. mp_load_build_class,
  167. mp_load_attr,
  168. mp_load_method,
  169. mp_load_super_method,
  170. mp_store_name,
  171. mp_store_global,
  172. mp_store_attr,
  173. mp_obj_subscr,
  174. mp_obj_is_true,
  175. mp_unary_op,
  176. mp_binary_op,
  177. mp_obj_new_tuple,
  178. mp_obj_new_list,
  179. mp_obj_list_append,
  180. mp_obj_new_dict,
  181. mp_obj_dict_store,
  182. #if MICROPY_PY_BUILTINS_SET
  183. mp_obj_set_store,
  184. mp_obj_new_set,
  185. #endif
  186. mp_make_function_from_raw_code,
  187. mp_native_call_function_n_kw,
  188. mp_call_method_n_kw,
  189. mp_call_method_n_kw_var,
  190. mp_native_getiter,
  191. mp_native_iternext,
  192. nlr_push,
  193. nlr_pop,
  194. mp_native_raise,
  195. mp_import_name,
  196. mp_import_from,
  197. mp_import_all,
  198. #if MICROPY_PY_BUILTINS_SLICE
  199. mp_obj_new_slice,
  200. #endif
  201. mp_unpack_sequence,
  202. mp_unpack_ex,
  203. mp_delete_name,
  204. mp_delete_global,
  205. mp_obj_new_cell,
  206. mp_make_closure_from_raw_code,
  207. mp_arg_check_num_sig,
  208. mp_setup_code_state,
  209. mp_small_int_floor_divide,
  210. mp_small_int_modulo,
  211. mp_native_yield_from,
  212. };
  213. /*
  214. void mp_f_vector(mp_fun_kind_t fun_kind) {
  215. (mp_f_table[fun_kind])();
  216. }
  217. */
  218. #endif // MICROPY_EMIT_NATIVE