nativeglue.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 <stdarg.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <assert.h>
  30. #include "py/runtime.h"
  31. #include "py/smallint.h"
  32. #include "py/nativeglue.h"
  33. #include "py/gc.h"
  34. #if MICROPY_DEBUG_VERBOSE // print debugging info
  35. #define DEBUG_printf DEBUG_printf
  36. #else // don't print debugging info
  37. #define DEBUG_printf(...) (void)0
  38. #endif
  39. #if MICROPY_EMIT_NATIVE
  40. int mp_native_type_from_qstr(qstr qst) {
  41. switch (qst) {
  42. case MP_QSTR_object: return MP_NATIVE_TYPE_OBJ;
  43. case MP_QSTR_bool: return MP_NATIVE_TYPE_BOOL;
  44. case MP_QSTR_int: return MP_NATIVE_TYPE_INT;
  45. case MP_QSTR_uint: return MP_NATIVE_TYPE_UINT;
  46. case MP_QSTR_ptr: return MP_NATIVE_TYPE_PTR;
  47. case MP_QSTR_ptr8: return MP_NATIVE_TYPE_PTR8;
  48. case MP_QSTR_ptr16: return MP_NATIVE_TYPE_PTR16;
  49. case MP_QSTR_ptr32: return MP_NATIVE_TYPE_PTR32;
  50. default: return -1;
  51. }
  52. }
  53. // convert a MicroPython object to a valid native value based on type
  54. mp_uint_t mp_native_from_obj(mp_obj_t obj, mp_uint_t type) {
  55. DEBUG_printf("mp_native_from_obj(%p, " UINT_FMT ")\n", obj, type);
  56. switch (type & 0xf) {
  57. case MP_NATIVE_TYPE_OBJ: return (mp_uint_t)obj;
  58. case MP_NATIVE_TYPE_BOOL: return mp_obj_is_true(obj);
  59. case MP_NATIVE_TYPE_INT:
  60. case MP_NATIVE_TYPE_UINT: return mp_obj_get_int_truncated(obj);
  61. default: { // cast obj to a pointer
  62. mp_buffer_info_t bufinfo;
  63. if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_READ)) {
  64. return (mp_uint_t)bufinfo.buf;
  65. } else {
  66. // assume obj is an integer that represents an address
  67. return mp_obj_get_int_truncated(obj);
  68. }
  69. }
  70. }
  71. }
  72. #endif
  73. #if MICROPY_EMIT_MACHINE_CODE
  74. // convert a native value to a MicroPython object based on type
  75. mp_obj_t mp_native_to_obj(mp_uint_t val, mp_uint_t type) {
  76. DEBUG_printf("mp_native_to_obj(" UINT_FMT ", " UINT_FMT ")\n", val, type);
  77. switch (type & 0xf) {
  78. case MP_NATIVE_TYPE_OBJ: return (mp_obj_t)val;
  79. case MP_NATIVE_TYPE_BOOL: return mp_obj_new_bool(val);
  80. case MP_NATIVE_TYPE_INT: return mp_obj_new_int(val);
  81. case MP_NATIVE_TYPE_UINT: return mp_obj_new_int_from_uint(val);
  82. default: // a pointer
  83. // we return just the value of the pointer as an integer
  84. return mp_obj_new_int_from_uint(val);
  85. }
  86. }
  87. #endif
  88. #if MICROPY_EMIT_NATIVE && !MICROPY_DYNAMIC_COMPILER
  89. #if !MICROPY_PY_BUILTINS_SET
  90. mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items) {
  91. (void)n_args;
  92. (void)items;
  93. mp_raise_msg(&mp_type_RuntimeError, "set unsupported");
  94. }
  95. void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
  96. (void)self_in;
  97. (void)item;
  98. mp_raise_msg(&mp_type_RuntimeError, "set unsupported");
  99. }
  100. #endif
  101. #if !MICROPY_PY_BUILTINS_SLICE
  102. mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
  103. (void)ostart;
  104. (void)ostop;
  105. (void)ostep;
  106. mp_raise_msg(&mp_type_RuntimeError, "slice unsupported");
  107. }
  108. #endif
  109. STATIC mp_obj_dict_t *mp_native_swap_globals(mp_obj_dict_t *new_globals) {
  110. if (new_globals == NULL) {
  111. // Globals were the originally the same so don't restore them
  112. return NULL;
  113. }
  114. mp_obj_dict_t *old_globals = mp_globals_get();
  115. if (old_globals == new_globals) {
  116. // Don't set globals if they are the same, and return NULL to indicate this
  117. return NULL;
  118. }
  119. mp_globals_set(new_globals);
  120. return old_globals;
  121. }
  122. // wrapper that accepts n_args and n_kw in one argument
  123. // (native emitter can only pass at most 3 arguments to a function)
  124. STATIC mp_obj_t mp_native_call_function_n_kw(mp_obj_t fun_in, size_t n_args_kw, const mp_obj_t *args) {
  125. return mp_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
  126. }
  127. // wrapper that makes raise obj and raises it
  128. // END_FINALLY opcode requires that we don't raise if o==None
  129. STATIC void mp_native_raise(mp_obj_t o) {
  130. if (o != MP_OBJ_NULL && o != mp_const_none) {
  131. nlr_raise(mp_make_raise_obj(o));
  132. }
  133. }
  134. // wrapper that handles iterator buffer
  135. STATIC mp_obj_t mp_native_getiter(mp_obj_t obj, mp_obj_iter_buf_t *iter) {
  136. if (iter == NULL) {
  137. return mp_getiter(obj, NULL);
  138. } else {
  139. obj = mp_getiter(obj, iter);
  140. if (obj != MP_OBJ_FROM_PTR(iter)) {
  141. // Iterator didn't use the stack so indicate that with MP_OBJ_NULL.
  142. iter->base.type = MP_OBJ_NULL;
  143. iter->buf[0] = obj;
  144. }
  145. return NULL;
  146. }
  147. }
  148. // wrapper that handles iterator buffer
  149. STATIC mp_obj_t mp_native_iternext(mp_obj_iter_buf_t *iter) {
  150. mp_obj_t obj;
  151. if (iter->base.type == MP_OBJ_NULL) {
  152. obj = iter->buf[0];
  153. } else {
  154. obj = MP_OBJ_FROM_PTR(iter);
  155. }
  156. return mp_iternext(obj);
  157. }
  158. STATIC bool mp_native_yield_from(mp_obj_t gen, mp_obj_t send_value, mp_obj_t *ret_value) {
  159. mp_vm_return_kind_t ret_kind;
  160. nlr_buf_t nlr_buf;
  161. mp_obj_t throw_value = *ret_value;
  162. if (nlr_push(&nlr_buf) == 0) {
  163. if (throw_value != MP_OBJ_NULL) {
  164. send_value = MP_OBJ_NULL;
  165. }
  166. ret_kind = mp_resume(gen, send_value, throw_value, ret_value);
  167. nlr_pop();
  168. } else {
  169. ret_kind = MP_VM_RETURN_EXCEPTION;
  170. *ret_value = nlr_buf.ret_val;
  171. }
  172. if (ret_kind == MP_VM_RETURN_YIELD) {
  173. return true;
  174. } else if (ret_kind == MP_VM_RETURN_NORMAL) {
  175. if (*ret_value == MP_OBJ_STOP_ITERATION) {
  176. *ret_value = mp_const_none;
  177. }
  178. } else {
  179. assert(ret_kind == MP_VM_RETURN_EXCEPTION);
  180. if (!mp_obj_exception_match(*ret_value, MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
  181. nlr_raise(*ret_value);
  182. }
  183. *ret_value = mp_obj_exception_get_value(*ret_value);
  184. }
  185. if (throw_value != MP_OBJ_NULL && mp_obj_exception_match(throw_value, MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) {
  186. nlr_raise(mp_make_raise_obj(throw_value));
  187. }
  188. return false;
  189. }
  190. #if MICROPY_PY_BUILTINS_FLOAT
  191. STATIC mp_obj_t mp_obj_new_float_from_f(float f) {
  192. return mp_obj_new_float((mp_float_t)f);
  193. }
  194. STATIC mp_obj_t mp_obj_new_float_from_d(double d) {
  195. return mp_obj_new_float((mp_float_t)d);
  196. }
  197. STATIC float mp_obj_get_float_to_f(mp_obj_t o) {
  198. return (float)mp_obj_get_float(o);
  199. }
  200. STATIC double mp_obj_get_float_to_d(mp_obj_t o) {
  201. return (double)mp_obj_get_float(o);
  202. }
  203. #else
  204. STATIC mp_obj_t mp_obj_new_float_from_f(float f) {
  205. (void)f;
  206. mp_raise_msg(&mp_type_RuntimeError, "float unsupported");
  207. }
  208. STATIC mp_obj_t mp_obj_new_float_from_d(double d) {
  209. (void)d;
  210. mp_raise_msg(&mp_type_RuntimeError, "float unsupported");
  211. }
  212. STATIC float mp_obj_get_float_to_f(mp_obj_t o) {
  213. (void)o;
  214. mp_raise_msg(&mp_type_RuntimeError, "float unsupported");
  215. }
  216. STATIC double mp_obj_get_float_to_d(mp_obj_t o) {
  217. (void)o;
  218. mp_raise_msg(&mp_type_RuntimeError, "float unsupported");
  219. }
  220. #endif
  221. // these must correspond to the respective enum in runtime0.h
  222. const mp_fun_table_t mp_fun_table = {
  223. &mp_const_none_obj,
  224. &mp_const_false_obj,
  225. &mp_const_true_obj,
  226. mp_native_from_obj,
  227. mp_native_to_obj,
  228. mp_native_swap_globals,
  229. mp_load_name,
  230. mp_load_global,
  231. mp_load_build_class,
  232. mp_load_attr,
  233. mp_load_method,
  234. mp_load_super_method,
  235. mp_store_name,
  236. mp_store_global,
  237. mp_store_attr,
  238. mp_obj_subscr,
  239. mp_obj_is_true,
  240. mp_unary_op,
  241. mp_binary_op,
  242. mp_obj_new_tuple,
  243. mp_obj_new_list,
  244. mp_obj_new_dict,
  245. mp_obj_new_set,
  246. mp_obj_set_store,
  247. mp_obj_list_append,
  248. mp_obj_dict_store,
  249. mp_make_function_from_raw_code,
  250. mp_native_call_function_n_kw,
  251. mp_call_method_n_kw,
  252. mp_call_method_n_kw_var,
  253. mp_native_getiter,
  254. mp_native_iternext,
  255. #if MICROPY_NLR_SETJMP
  256. nlr_push_tail,
  257. #else
  258. nlr_push,
  259. #endif
  260. nlr_pop,
  261. mp_native_raise,
  262. mp_import_name,
  263. mp_import_from,
  264. mp_import_all,
  265. mp_obj_new_slice,
  266. mp_unpack_sequence,
  267. mp_unpack_ex,
  268. mp_delete_name,
  269. mp_delete_global,
  270. mp_make_closure_from_raw_code,
  271. mp_arg_check_num_sig,
  272. mp_setup_code_state,
  273. mp_small_int_floor_divide,
  274. mp_small_int_modulo,
  275. mp_native_yield_from,
  276. #if MICROPY_NLR_SETJMP
  277. setjmp,
  278. #else
  279. NULL,
  280. #endif
  281. // Additional entries for dynamic runtime, starts at index 50
  282. memset,
  283. memmove,
  284. gc_realloc,
  285. mp_printf,
  286. mp_vprintf,
  287. mp_raise_msg,
  288. mp_obj_get_type,
  289. mp_obj_new_str,
  290. mp_obj_new_bytes,
  291. mp_obj_new_bytearray_by_ref,
  292. mp_obj_new_float_from_f,
  293. mp_obj_new_float_from_d,
  294. mp_obj_get_float_to_f,
  295. mp_obj_get_float_to_d,
  296. mp_get_buffer_raise,
  297. mp_get_stream_raise,
  298. &mp_plat_print,
  299. &mp_type_type,
  300. &mp_type_str,
  301. &mp_type_list,
  302. &mp_type_dict,
  303. &mp_type_fun_builtin_0,
  304. &mp_type_fun_builtin_1,
  305. &mp_type_fun_builtin_2,
  306. &mp_type_fun_builtin_3,
  307. &mp_type_fun_builtin_var,
  308. &mp_stream_read_obj,
  309. &mp_stream_readinto_obj,
  310. &mp_stream_unbuffered_readline_obj,
  311. &mp_stream_write_obj,
  312. };
  313. #endif // MICROPY_EMIT_NATIVE