objfun.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. * Copyright (c) 2014 Paul Sokolovsky
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <string.h>
  28. #include <assert.h>
  29. #include "py/objtuple.h"
  30. #include "py/objfun.h"
  31. #include "py/runtime.h"
  32. #include "py/bc.h"
  33. #include "py/stackctrl.h"
  34. #if MICROPY_DEBUG_VERBOSE // print debugging info
  35. #define DEBUG_PRINT (1)
  36. #else // don't print debugging info
  37. #define DEBUG_PRINT (0)
  38. #define DEBUG_printf(...) (void)0
  39. #endif
  40. // Note: the "name" entry in mp_obj_type_t for a function type must be
  41. // MP_QSTR_function because it is used to determine if an object is of generic
  42. // function type.
  43. /******************************************************************************/
  44. /* builtin functions */
  45. STATIC mp_obj_t fun_builtin_0_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  46. (void)args;
  47. assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_0));
  48. mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
  49. mp_arg_check_num(n_args, n_kw, 0, 0, false);
  50. return self->fun._0();
  51. }
  52. const mp_obj_type_t mp_type_fun_builtin_0 = {
  53. { &mp_type_type },
  54. .name = MP_QSTR_function,
  55. .call = fun_builtin_0_call,
  56. .unary_op = mp_generic_unary_op,
  57. };
  58. STATIC mp_obj_t fun_builtin_1_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  59. assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_1));
  60. mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
  61. mp_arg_check_num(n_args, n_kw, 1, 1, false);
  62. return self->fun._1(args[0]);
  63. }
  64. const mp_obj_type_t mp_type_fun_builtin_1 = {
  65. { &mp_type_type },
  66. .name = MP_QSTR_function,
  67. .call = fun_builtin_1_call,
  68. .unary_op = mp_generic_unary_op,
  69. };
  70. STATIC mp_obj_t fun_builtin_2_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  71. assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_2));
  72. mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
  73. mp_arg_check_num(n_args, n_kw, 2, 2, false);
  74. return self->fun._2(args[0], args[1]);
  75. }
  76. const mp_obj_type_t mp_type_fun_builtin_2 = {
  77. { &mp_type_type },
  78. .name = MP_QSTR_function,
  79. .call = fun_builtin_2_call,
  80. .unary_op = mp_generic_unary_op,
  81. };
  82. STATIC mp_obj_t fun_builtin_3_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  83. assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_3));
  84. mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
  85. mp_arg_check_num(n_args, n_kw, 3, 3, false);
  86. return self->fun._3(args[0], args[1], args[2]);
  87. }
  88. const mp_obj_type_t mp_type_fun_builtin_3 = {
  89. { &mp_type_type },
  90. .name = MP_QSTR_function,
  91. .call = fun_builtin_3_call,
  92. .unary_op = mp_generic_unary_op,
  93. };
  94. STATIC mp_obj_t fun_builtin_var_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  95. assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_var));
  96. mp_obj_fun_builtin_var_t *self = MP_OBJ_TO_PTR(self_in);
  97. // check number of arguments
  98. mp_arg_check_num(n_args, n_kw, self->n_args_min, self->n_args_max, self->is_kw);
  99. if (self->is_kw) {
  100. // function allows keywords
  101. // we create a map directly from the given args array
  102. mp_map_t kw_args;
  103. mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
  104. return self->fun.kw(n_args, args, &kw_args);
  105. } else {
  106. // function takes a variable number of arguments, but no keywords
  107. return self->fun.var(n_args, args);
  108. }
  109. }
  110. const mp_obj_type_t mp_type_fun_builtin_var = {
  111. { &mp_type_type },
  112. .name = MP_QSTR_function,
  113. .call = fun_builtin_var_call,
  114. .unary_op = mp_generic_unary_op,
  115. };
  116. /******************************************************************************/
  117. /* byte code functions */
  118. qstr mp_obj_code_get_name(const byte *code_info) {
  119. code_info = mp_decode_uint_skip(code_info); // skip code_info_size entry
  120. #if MICROPY_PERSISTENT_CODE
  121. return code_info[0] | (code_info[1] << 8);
  122. #else
  123. return mp_decode_uint_value(code_info);
  124. #endif
  125. }
  126. #if MICROPY_EMIT_NATIVE
  127. STATIC const mp_obj_type_t mp_type_fun_native;
  128. #endif
  129. qstr mp_obj_fun_get_name(mp_const_obj_t fun_in) {
  130. const mp_obj_fun_bc_t *fun = MP_OBJ_TO_PTR(fun_in);
  131. #if MICROPY_EMIT_NATIVE
  132. if (fun->base.type == &mp_type_fun_native) {
  133. // TODO native functions don't have name stored
  134. return MP_QSTR_;
  135. }
  136. #endif
  137. const byte *bc = fun->bytecode;
  138. bc = mp_decode_uint_skip(bc); // skip n_state
  139. bc = mp_decode_uint_skip(bc); // skip n_exc_stack
  140. bc++; // skip scope_params
  141. bc++; // skip n_pos_args
  142. bc++; // skip n_kwonly_args
  143. bc++; // skip n_def_pos_args
  144. return mp_obj_code_get_name(bc);
  145. }
  146. #if MICROPY_CPYTHON_COMPAT
  147. STATIC void fun_bc_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
  148. (void)kind;
  149. mp_obj_fun_bc_t *o = MP_OBJ_TO_PTR(o_in);
  150. mp_printf(print, "<function %q at 0x%p>", mp_obj_fun_get_name(o_in), o);
  151. }
  152. #endif
  153. #if DEBUG_PRINT
  154. STATIC void dump_args(const mp_obj_t *a, size_t sz) {
  155. DEBUG_printf("%p: ", a);
  156. for (size_t i = 0; i < sz; i++) {
  157. DEBUG_printf("%p ", a[i]);
  158. }
  159. DEBUG_printf("\n");
  160. }
  161. #else
  162. #define dump_args(...) (void)0
  163. #endif
  164. // With this macro you can tune the maximum number of function state bytes
  165. // that will be allocated on the stack. Any function that needs more
  166. // than this will try to use the heap, with fallback to stack allocation.
  167. #define VM_MAX_STATE_ON_STACK (11 * sizeof(mp_uint_t))
  168. // Set this to 1 to enable a simple stack overflow check.
  169. #define VM_DETECT_STACK_OVERFLOW (0)
  170. #define DECODE_CODESTATE_SIZE(bytecode, n_state_out_var, state_size_out_var) \
  171. { \
  172. /* bytecode prelude: state size and exception stack size */ \
  173. n_state_out_var = mp_decode_uint_value(bytecode); \
  174. size_t n_exc_stack = mp_decode_uint_value(mp_decode_uint_skip(bytecode)); \
  175. \
  176. n_state += VM_DETECT_STACK_OVERFLOW; \
  177. \
  178. /* state size in bytes */ \
  179. state_size_out_var = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t); \
  180. }
  181. #define INIT_CODESTATE(code_state, _fun_bc, n_args, n_kw, args) \
  182. code_state->fun_bc = _fun_bc; \
  183. code_state->ip = 0; \
  184. mp_setup_code_state(code_state, n_args, n_kw, args); \
  185. code_state->old_globals = mp_globals_get();
  186. #if MICROPY_STACKLESS
  187. mp_code_state_t *mp_obj_fun_bc_prepare_codestate(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  188. MP_STACK_CHECK();
  189. mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
  190. size_t n_state, state_size;
  191. DECODE_CODESTATE_SIZE(self->bytecode, n_state, state_size);
  192. mp_code_state_t *code_state;
  193. #if MICROPY_ENABLE_PYSTACK
  194. code_state = mp_pystack_alloc(sizeof(mp_code_state_t) + state_size);
  195. #else
  196. // If we use m_new_obj_var(), then on no memory, MemoryError will be
  197. // raised. But this is not correct exception for a function call,
  198. // RuntimeError should be raised instead. So, we use m_new_obj_var_maybe(),
  199. // return NULL, then vm.c takes the needed action (either raise
  200. // RuntimeError or fallback to stack allocation).
  201. code_state = m_new_obj_var_maybe(mp_code_state_t, byte, state_size);
  202. if (!code_state) {
  203. return NULL;
  204. }
  205. #endif
  206. INIT_CODESTATE(code_state, self, n_args, n_kw, args);
  207. // execute the byte code with the correct globals context
  208. mp_globals_set(self->globals);
  209. return code_state;
  210. }
  211. #endif
  212. STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  213. MP_STACK_CHECK();
  214. DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
  215. DEBUG_printf("Input pos args: ");
  216. dump_args(args, n_args);
  217. DEBUG_printf("Input kw args: ");
  218. dump_args(args + n_args, n_kw * 2);
  219. mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
  220. DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
  221. size_t n_state, state_size;
  222. DECODE_CODESTATE_SIZE(self->bytecode, n_state, state_size);
  223. // allocate state for locals and stack
  224. mp_code_state_t *code_state = NULL;
  225. #if MICROPY_ENABLE_PYSTACK
  226. code_state = mp_pystack_alloc(sizeof(mp_code_state_t) + state_size);
  227. #else
  228. if (state_size > VM_MAX_STATE_ON_STACK) {
  229. code_state = m_new_obj_var_maybe(mp_code_state_t, byte, state_size);
  230. }
  231. if (code_state == NULL) {
  232. code_state = alloca(sizeof(mp_code_state_t) + state_size);
  233. state_size = 0; // indicate that we allocated using alloca
  234. }
  235. #endif
  236. INIT_CODESTATE(code_state, self, n_args, n_kw, args);
  237. // execute the byte code with the correct globals context
  238. mp_globals_set(self->globals);
  239. mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
  240. mp_globals_set(code_state->old_globals);
  241. #if VM_DETECT_STACK_OVERFLOW
  242. if (vm_return_kind == MP_VM_RETURN_NORMAL) {
  243. if (code_state->sp < code_state->state) {
  244. printf("VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
  245. assert(0);
  246. }
  247. }
  248. // We can't check the case when an exception is returned in state[n_state - 1]
  249. // and there are no arguments, because in this case our detection slot may have
  250. // been overwritten by the returned exception (which is allowed).
  251. if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && self->n_pos_args + self->n_kwonly_args == 0)) {
  252. // Just check to see that we have at least 1 null object left in the state.
  253. bool overflow = true;
  254. for (size_t i = 0; i < n_state - self->n_pos_args - self->n_kwonly_args; i++) {
  255. if (code_state->state[i] == MP_OBJ_NULL) {
  256. overflow = false;
  257. break;
  258. }
  259. }
  260. if (overflow) {
  261. printf("VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
  262. assert(0);
  263. }
  264. }
  265. #endif
  266. mp_obj_t result;
  267. if (vm_return_kind == MP_VM_RETURN_NORMAL) {
  268. // return value is in *sp
  269. result = *code_state->sp;
  270. } else {
  271. // must be an exception because normal functions can't yield
  272. assert(vm_return_kind == MP_VM_RETURN_EXCEPTION);
  273. // return value is in fastn[0]==state[n_state - 1]
  274. result = code_state->state[n_state - 1];
  275. }
  276. #if MICROPY_ENABLE_PYSTACK
  277. mp_pystack_free(code_state);
  278. #else
  279. // free the state if it was allocated on the heap
  280. if (state_size != 0) {
  281. m_del_var(mp_code_state_t, byte, state_size, code_state);
  282. }
  283. #endif
  284. if (vm_return_kind == MP_VM_RETURN_NORMAL) {
  285. return result;
  286. } else { // MP_VM_RETURN_EXCEPTION
  287. nlr_raise(result);
  288. }
  289. }
  290. #if MICROPY_PY_FUNCTION_ATTRS
  291. STATIC void fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  292. if (dest[0] != MP_OBJ_NULL) {
  293. // not load attribute
  294. return;
  295. }
  296. if (attr == MP_QSTR___name__) {
  297. dest[0] = MP_OBJ_NEW_QSTR(mp_obj_fun_get_name(self_in));
  298. }
  299. }
  300. #endif
  301. const mp_obj_type_t mp_type_fun_bc = {
  302. { &mp_type_type },
  303. .name = MP_QSTR_function,
  304. #if MICROPY_CPYTHON_COMPAT
  305. .print = fun_bc_print,
  306. #endif
  307. .call = fun_bc_call,
  308. .unary_op = mp_generic_unary_op,
  309. #if MICROPY_PY_FUNCTION_ATTRS
  310. .attr = fun_bc_attr,
  311. #endif
  312. };
  313. mp_obj_t mp_obj_new_fun_bc(mp_obj_t def_args_in, mp_obj_t def_kw_args, const byte *code, const mp_uint_t *const_table) {
  314. size_t n_def_args = 0;
  315. size_t n_extra_args = 0;
  316. mp_obj_tuple_t *def_args = MP_OBJ_TO_PTR(def_args_in);
  317. if (def_args_in != MP_OBJ_NULL) {
  318. assert(MP_OBJ_IS_TYPE(def_args_in, &mp_type_tuple));
  319. n_def_args = def_args->len;
  320. n_extra_args = def_args->len;
  321. }
  322. if (def_kw_args != MP_OBJ_NULL) {
  323. n_extra_args += 1;
  324. }
  325. mp_obj_fun_bc_t *o = m_new_obj_var(mp_obj_fun_bc_t, mp_obj_t, n_extra_args);
  326. o->base.type = &mp_type_fun_bc;
  327. o->globals = mp_globals_get();
  328. o->bytecode = code;
  329. o->const_table = const_table;
  330. if (def_args != NULL) {
  331. memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
  332. }
  333. if (def_kw_args != MP_OBJ_NULL) {
  334. o->extra_args[n_def_args] = def_kw_args;
  335. }
  336. return MP_OBJ_FROM_PTR(o);
  337. }
  338. /******************************************************************************/
  339. /* native functions */
  340. #if MICROPY_EMIT_NATIVE
  341. STATIC mp_obj_t fun_native_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  342. MP_STACK_CHECK();
  343. mp_obj_fun_bc_t *self = self_in;
  344. mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void*)self->bytecode);
  345. return fun(self_in, n_args, n_kw, args);
  346. }
  347. STATIC const mp_obj_type_t mp_type_fun_native = {
  348. { &mp_type_type },
  349. .name = MP_QSTR_function,
  350. .call = fun_native_call,
  351. .unary_op = mp_generic_unary_op,
  352. };
  353. mp_obj_t mp_obj_new_fun_native(mp_obj_t def_args_in, mp_obj_t def_kw_args, const void *fun_data, const mp_uint_t *const_table) {
  354. mp_obj_fun_bc_t *o = mp_obj_new_fun_bc(def_args_in, def_kw_args, (const byte*)fun_data, const_table);
  355. o->base.type = &mp_type_fun_native;
  356. return o;
  357. }
  358. #endif // MICROPY_EMIT_NATIVE
  359. /******************************************************************************/
  360. /* viper functions */
  361. #if MICROPY_EMIT_NATIVE
  362. typedef struct _mp_obj_fun_viper_t {
  363. mp_obj_base_t base;
  364. size_t n_args;
  365. void *fun_data; // GC must be able to trace this pointer
  366. mp_uint_t type_sig;
  367. } mp_obj_fun_viper_t;
  368. typedef mp_uint_t (*viper_fun_0_t)(void);
  369. typedef mp_uint_t (*viper_fun_1_t)(mp_uint_t);
  370. typedef mp_uint_t (*viper_fun_2_t)(mp_uint_t, mp_uint_t);
  371. typedef mp_uint_t (*viper_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
  372. typedef mp_uint_t (*viper_fun_4_t)(mp_uint_t, mp_uint_t, mp_uint_t, mp_uint_t);
  373. STATIC mp_obj_t fun_viper_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  374. mp_obj_fun_viper_t *self = self_in;
  375. mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
  376. void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
  377. mp_uint_t ret;
  378. if (n_args == 0) {
  379. ret = ((viper_fun_0_t)fun)();
  380. } else if (n_args == 1) {
  381. ret = ((viper_fun_1_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 4));
  382. } else if (n_args == 2) {
  383. ret = ((viper_fun_2_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 4), mp_convert_obj_to_native(args[1], self->type_sig >> 8));
  384. } else if (n_args == 3) {
  385. ret = ((viper_fun_3_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 4), mp_convert_obj_to_native(args[1], self->type_sig >> 8), mp_convert_obj_to_native(args[2], self->type_sig >> 12));
  386. } else {
  387. // compiler allows at most 4 arguments
  388. assert(n_args == 4);
  389. ret = ((viper_fun_4_t)fun)(
  390. mp_convert_obj_to_native(args[0], self->type_sig >> 4),
  391. mp_convert_obj_to_native(args[1], self->type_sig >> 8),
  392. mp_convert_obj_to_native(args[2], self->type_sig >> 12),
  393. mp_convert_obj_to_native(args[3], self->type_sig >> 16)
  394. );
  395. }
  396. return mp_convert_native_to_obj(ret, self->type_sig);
  397. }
  398. STATIC const mp_obj_type_t mp_type_fun_viper = {
  399. { &mp_type_type },
  400. .name = MP_QSTR_function,
  401. .call = fun_viper_call,
  402. .unary_op = mp_generic_unary_op,
  403. };
  404. mp_obj_t mp_obj_new_fun_viper(size_t n_args, void *fun_data, mp_uint_t type_sig) {
  405. mp_obj_fun_viper_t *o = m_new_obj(mp_obj_fun_viper_t);
  406. o->base.type = &mp_type_fun_viper;
  407. o->n_args = n_args;
  408. o->fun_data = fun_data;
  409. o->type_sig = type_sig;
  410. return o;
  411. }
  412. #endif // MICROPY_EMIT_NATIVE
  413. /******************************************************************************/
  414. /* inline assembler functions */
  415. #if MICROPY_EMIT_INLINE_ASM
  416. typedef struct _mp_obj_fun_asm_t {
  417. mp_obj_base_t base;
  418. size_t n_args;
  419. void *fun_data; // GC must be able to trace this pointer
  420. mp_uint_t type_sig;
  421. } mp_obj_fun_asm_t;
  422. typedef mp_uint_t (*inline_asm_fun_0_t)(void);
  423. typedef mp_uint_t (*inline_asm_fun_1_t)(mp_uint_t);
  424. typedef mp_uint_t (*inline_asm_fun_2_t)(mp_uint_t, mp_uint_t);
  425. typedef mp_uint_t (*inline_asm_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
  426. typedef mp_uint_t (*inline_asm_fun_4_t)(mp_uint_t, mp_uint_t, mp_uint_t, mp_uint_t);
  427. // convert a MicroPython object to a sensible value for inline asm
  428. STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
  429. // TODO for byte_array, pass pointer to the array
  430. if (MP_OBJ_IS_SMALL_INT(obj)) {
  431. return MP_OBJ_SMALL_INT_VALUE(obj);
  432. } else if (obj == mp_const_none) {
  433. return 0;
  434. } else if (obj == mp_const_false) {
  435. return 0;
  436. } else if (obj == mp_const_true) {
  437. return 1;
  438. } else if (MP_OBJ_IS_TYPE(obj, &mp_type_int)) {
  439. return mp_obj_int_get_truncated(obj);
  440. } else if (MP_OBJ_IS_STR(obj)) {
  441. // pointer to the string (it's probably constant though!)
  442. size_t l;
  443. return (mp_uint_t)mp_obj_str_get_data(obj, &l);
  444. } else {
  445. mp_obj_type_t *type = mp_obj_get_type(obj);
  446. if (0) {
  447. #if MICROPY_PY_BUILTINS_FLOAT
  448. } else if (type == &mp_type_float) {
  449. // convert float to int (could also pass in float registers)
  450. return (mp_int_t)mp_obj_float_get(obj);
  451. #endif
  452. } else if (type == &mp_type_tuple || type == &mp_type_list) {
  453. // pointer to start of tuple (could pass length, but then could use len(x) for that)
  454. size_t len;
  455. mp_obj_t *items;
  456. mp_obj_get_array(obj, &len, &items);
  457. return (mp_uint_t)items;
  458. } else {
  459. mp_buffer_info_t bufinfo;
  460. if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
  461. // supports the buffer protocol, return a pointer to the data
  462. return (mp_uint_t)bufinfo.buf;
  463. } else {
  464. // just pass along a pointer to the object
  465. return (mp_uint_t)obj;
  466. }
  467. }
  468. }
  469. }
  470. STATIC mp_obj_t fun_asm_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  471. mp_obj_fun_asm_t *self = self_in;
  472. mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
  473. void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
  474. mp_uint_t ret;
  475. if (n_args == 0) {
  476. ret = ((inline_asm_fun_0_t)fun)();
  477. } else if (n_args == 1) {
  478. ret = ((inline_asm_fun_1_t)fun)(convert_obj_for_inline_asm(args[0]));
  479. } else if (n_args == 2) {
  480. ret = ((inline_asm_fun_2_t)fun)(convert_obj_for_inline_asm(args[0]), convert_obj_for_inline_asm(args[1]));
  481. } else if (n_args == 3) {
  482. ret = ((inline_asm_fun_3_t)fun)(convert_obj_for_inline_asm(args[0]), convert_obj_for_inline_asm(args[1]), convert_obj_for_inline_asm(args[2]));
  483. } else {
  484. // compiler allows at most 4 arguments
  485. assert(n_args == 4);
  486. ret = ((inline_asm_fun_4_t)fun)(
  487. convert_obj_for_inline_asm(args[0]),
  488. convert_obj_for_inline_asm(args[1]),
  489. convert_obj_for_inline_asm(args[2]),
  490. convert_obj_for_inline_asm(args[3])
  491. );
  492. }
  493. return mp_convert_native_to_obj(ret, self->type_sig);
  494. }
  495. STATIC const mp_obj_type_t mp_type_fun_asm = {
  496. { &mp_type_type },
  497. .name = MP_QSTR_function,
  498. .call = fun_asm_call,
  499. .unary_op = mp_generic_unary_op,
  500. };
  501. mp_obj_t mp_obj_new_fun_asm(size_t n_args, void *fun_data, mp_uint_t type_sig) {
  502. mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
  503. o->base.type = &mp_type_fun_asm;
  504. o->n_args = n_args;
  505. o->fun_data = fun_data;
  506. o->type_sig = type_sig;
  507. return o;
  508. }
  509. #endif // MICROPY_EMIT_INLINE_ASM