nativeglue.c 10 KB

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