nativeglue.c 10 KB

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