objfun.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. MP_DEFINE_CONST_OBJ_TYPE(
  53. mp_type_fun_builtin_0, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
  54. call, fun_builtin_0_call
  55. );
  56. 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) {
  57. assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_1));
  58. mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
  59. mp_arg_check_num(n_args, n_kw, 1, 1, false);
  60. return self->fun._1(args[0]);
  61. }
  62. MP_DEFINE_CONST_OBJ_TYPE(
  63. mp_type_fun_builtin_1, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
  64. call, fun_builtin_1_call
  65. );
  66. 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) {
  67. assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_2));
  68. mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
  69. mp_arg_check_num(n_args, n_kw, 2, 2, false);
  70. return self->fun._2(args[0], args[1]);
  71. }
  72. MP_DEFINE_CONST_OBJ_TYPE(
  73. mp_type_fun_builtin_2, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
  74. call, fun_builtin_2_call
  75. );
  76. 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) {
  77. assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_3));
  78. mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
  79. mp_arg_check_num(n_args, n_kw, 3, 3, false);
  80. return self->fun._3(args[0], args[1], args[2]);
  81. }
  82. MP_DEFINE_CONST_OBJ_TYPE(
  83. mp_type_fun_builtin_3, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
  84. call, fun_builtin_3_call
  85. );
  86. 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) {
  87. assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_var));
  88. mp_obj_fun_builtin_var_t *self = MP_OBJ_TO_PTR(self_in);
  89. // check number of arguments
  90. mp_arg_check_num_sig(n_args, n_kw, self->sig);
  91. if (self->sig & 1) {
  92. // function allows keywords
  93. // we create a map directly from the given args array
  94. mp_map_t kw_args;
  95. mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
  96. return self->fun.kw(n_args, args, &kw_args);
  97. } else {
  98. // function takes a variable number of arguments, but no keywords
  99. return self->fun.var(n_args, args);
  100. }
  101. }
  102. MP_DEFINE_CONST_OBJ_TYPE(
  103. mp_type_fun_builtin_var, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
  104. call, fun_builtin_var_call
  105. );
  106. /******************************************************************************/
  107. /* byte code functions */
  108. STATIC qstr mp_obj_code_get_name(const mp_obj_fun_bc_t *fun, const byte *code_info) {
  109. MP_BC_PRELUDE_SIZE_DECODE(code_info);
  110. mp_uint_t name = mp_decode_uint_value(code_info);
  111. #if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE
  112. name = fun->context->constants.qstr_table[name];
  113. #endif
  114. return name;
  115. }
  116. #if MICROPY_EMIT_NATIVE
  117. STATIC const mp_obj_type_t mp_type_fun_native;
  118. #endif
  119. qstr mp_obj_fun_get_name(mp_const_obj_t fun_in) {
  120. const mp_obj_fun_bc_t *fun = MP_OBJ_TO_PTR(fun_in);
  121. #if MICROPY_EMIT_NATIVE
  122. if (fun->base.type == &mp_type_fun_native || fun->base.type == &mp_type_native_gen_wrap) {
  123. // TODO native functions don't have name stored
  124. return MP_QSTR_;
  125. }
  126. #endif
  127. const byte *bc = fun->bytecode;
  128. MP_BC_PRELUDE_SIG_DECODE(bc);
  129. return mp_obj_code_get_name(fun, bc);
  130. }
  131. #if MICROPY_CPYTHON_COMPAT
  132. STATIC void fun_bc_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
  133. (void)kind;
  134. mp_obj_fun_bc_t *o = MP_OBJ_TO_PTR(o_in);
  135. mp_printf(print, "<function %q at 0x%p>", mp_obj_fun_get_name(o_in), o);
  136. }
  137. #endif
  138. #if DEBUG_PRINT
  139. STATIC void dump_args(const mp_obj_t *a, size_t sz) {
  140. DEBUG_printf("%p: ", a);
  141. for (size_t i = 0; i < sz; i++) {
  142. DEBUG_printf("%p ", a[i]);
  143. }
  144. DEBUG_printf("\n");
  145. }
  146. #else
  147. #define dump_args(...) (void)0
  148. #endif
  149. // With this macro you can tune the maximum number of function state bytes
  150. // that will be allocated on the stack. Any function that needs more
  151. // than this will try to use the heap, with fallback to stack allocation.
  152. #define VM_MAX_STATE_ON_STACK (sizeof(mp_uint_t) * 11)
  153. #define DECODE_CODESTATE_SIZE(bytecode, n_state_out_var, state_size_out_var) \
  154. { \
  155. const uint8_t *ip = bytecode; \
  156. size_t n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_args; \
  157. MP_BC_PRELUDE_SIG_DECODE_INTO(ip, n_state_out_var, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_args); \
  158. (void)scope_flags; (void)n_pos_args; (void)n_kwonly_args; (void)n_def_args; \
  159. \
  160. /* state size in bytes */ \
  161. state_size_out_var = n_state_out_var * sizeof(mp_obj_t) \
  162. + n_exc_stack * sizeof(mp_exc_stack_t); \
  163. }
  164. #define INIT_CODESTATE(code_state, _fun_bc, _n_state, n_args, n_kw, args) \
  165. code_state->fun_bc = _fun_bc; \
  166. code_state->n_state = _n_state; \
  167. mp_setup_code_state(code_state, n_args, n_kw, args); \
  168. code_state->old_globals = mp_globals_get();
  169. #if MICROPY_STACKLESS
  170. 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) {
  171. MP_STACK_CHECK();
  172. mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
  173. size_t n_state, state_size;
  174. DECODE_CODESTATE_SIZE(self->bytecode, n_state, state_size);
  175. mp_code_state_t *code_state;
  176. #if MICROPY_ENABLE_PYSTACK
  177. code_state = mp_pystack_alloc(sizeof(mp_code_state_t) + state_size);
  178. #else
  179. // If we use m_new_obj_var(), then on no memory, MemoryError will be
  180. // raised. But this is not correct exception for a function call,
  181. // RuntimeError should be raised instead. So, we use m_new_obj_var_maybe(),
  182. // return NULL, then vm.c takes the needed action (either raise
  183. // RuntimeError or fallback to stack allocation).
  184. code_state = m_new_obj_var_maybe(mp_code_state_t, state, byte, state_size);
  185. if (!code_state) {
  186. return NULL;
  187. }
  188. #endif
  189. INIT_CODESTATE(code_state, self, n_state, n_args, n_kw, args);
  190. // execute the byte code with the correct globals context
  191. mp_globals_set(self->context->module.globals);
  192. return code_state;
  193. }
  194. #endif
  195. 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) {
  196. MP_STACK_CHECK();
  197. DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
  198. DEBUG_printf("Input pos args: ");
  199. dump_args(args, n_args);
  200. DEBUG_printf("Input kw args: ");
  201. dump_args(args + n_args, n_kw * 2);
  202. mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
  203. size_t n_state, state_size;
  204. DECODE_CODESTATE_SIZE(self->bytecode, n_state, state_size);
  205. // allocate state for locals and stack
  206. mp_code_state_t *code_state = NULL;
  207. #if MICROPY_ENABLE_PYSTACK
  208. code_state = mp_pystack_alloc(offsetof(mp_code_state_t, state) + state_size);
  209. #else
  210. if (state_size > VM_MAX_STATE_ON_STACK) {
  211. code_state = m_new_obj_var_maybe(mp_code_state_t, state, byte, state_size);
  212. #if MICROPY_DEBUG_VM_STACK_OVERFLOW
  213. if (code_state != NULL) {
  214. memset(code_state->state, 0, state_size);
  215. }
  216. #endif
  217. }
  218. if (code_state == NULL) {
  219. code_state = alloca(offsetof(mp_code_state_t, state) + state_size);
  220. #if MICROPY_DEBUG_VM_STACK_OVERFLOW
  221. memset(code_state->state, 0, state_size);
  222. #endif
  223. state_size = 0; // indicate that we allocated using alloca
  224. }
  225. #endif
  226. INIT_CODESTATE(code_state, self, n_state, n_args, n_kw, args);
  227. // execute the byte code with the correct globals context
  228. mp_globals_set(self->context->module.globals);
  229. mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
  230. mp_globals_set(code_state->old_globals);
  231. #if MICROPY_DEBUG_VM_STACK_OVERFLOW
  232. if (vm_return_kind == MP_VM_RETURN_NORMAL) {
  233. if (code_state->sp < code_state->state) {
  234. mp_printf(MICROPY_DEBUG_PRINTER, "VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
  235. assert(0);
  236. }
  237. }
  238. const byte *bytecode_ptr = self->bytecode;
  239. size_t n_state_unused, n_exc_stack_unused, scope_flags_unused;
  240. size_t n_pos_args, n_kwonly_args, n_def_args_unused;
  241. MP_BC_PRELUDE_SIG_DECODE_INTO(bytecode_ptr, n_state_unused, n_exc_stack_unused,
  242. scope_flags_unused, n_pos_args, n_kwonly_args, n_def_args_unused);
  243. // We can't check the case when an exception is returned in state[0]
  244. // and there are no arguments, because in this case our detection slot may have
  245. // been overwritten by the returned exception (which is allowed).
  246. if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && n_pos_args + n_kwonly_args == 0)) {
  247. // Just check to see that we have at least 1 null object left in the state.
  248. bool overflow = true;
  249. for (size_t i = 0; i < n_state - n_pos_args - n_kwonly_args; ++i) {
  250. if (code_state->state[i] == MP_OBJ_NULL) {
  251. overflow = false;
  252. break;
  253. }
  254. }
  255. if (overflow) {
  256. mp_printf(MICROPY_DEBUG_PRINTER, "VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
  257. assert(0);
  258. }
  259. }
  260. #endif
  261. mp_obj_t result;
  262. if (vm_return_kind == MP_VM_RETURN_NORMAL) {
  263. // return value is in *sp
  264. result = *code_state->sp;
  265. } else {
  266. // must be an exception because normal functions can't yield
  267. assert(vm_return_kind == MP_VM_RETURN_EXCEPTION);
  268. // returned exception is in state[0]
  269. result = code_state->state[0];
  270. }
  271. #if MICROPY_ENABLE_PYSTACK
  272. mp_pystack_free(code_state);
  273. #else
  274. // free the state if it was allocated on the heap
  275. if (state_size != 0) {
  276. m_del_var(mp_code_state_t, state, byte, state_size, code_state);
  277. }
  278. #endif
  279. if (vm_return_kind == MP_VM_RETURN_NORMAL) {
  280. return result;
  281. } else { // MP_VM_RETURN_EXCEPTION
  282. nlr_raise(result);
  283. }
  284. }
  285. #if MICROPY_PY_FUNCTION_ATTRS
  286. void mp_obj_fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  287. if (dest[0] != MP_OBJ_NULL) {
  288. // not load attribute
  289. return;
  290. }
  291. if (attr == MP_QSTR___name__) {
  292. dest[0] = MP_OBJ_NEW_QSTR(mp_obj_fun_get_name(self_in));
  293. }
  294. if (attr == MP_QSTR___globals__) {
  295. mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
  296. dest[0] = MP_OBJ_FROM_PTR(self->context->module.globals);
  297. }
  298. }
  299. #endif
  300. #if MICROPY_CPYTHON_COMPAT
  301. #define FUN_BC_TYPE_PRINT print, fun_bc_print,
  302. #else
  303. #define FUN_BC_TYPE_PRINT
  304. #endif
  305. #if MICROPY_PY_FUNCTION_ATTRS
  306. #define FUN_BC_TYPE_ATTR attr, mp_obj_fun_bc_attr,
  307. #else
  308. #define FUN_BC_TYPE_ATTR
  309. #endif
  310. MP_DEFINE_CONST_OBJ_TYPE(
  311. mp_type_fun_bc,
  312. MP_QSTR_function,
  313. MP_TYPE_FLAG_BINDS_SELF,
  314. FUN_BC_TYPE_PRINT
  315. FUN_BC_TYPE_ATTR
  316. call, fun_bc_call
  317. );
  318. mp_obj_t mp_obj_new_fun_bc(const mp_obj_t *def_args, const byte *code, const mp_module_context_t *context, struct _mp_raw_code_t *const *child_table) {
  319. size_t n_def_args = 0;
  320. size_t n_extra_args = 0;
  321. mp_obj_tuple_t *def_pos_args = NULL;
  322. mp_obj_t def_kw_args = MP_OBJ_NULL;
  323. if (def_args != NULL && def_args[0] != MP_OBJ_NULL) {
  324. assert(mp_obj_is_type(def_args[0], &mp_type_tuple));
  325. def_pos_args = MP_OBJ_TO_PTR(def_args[0]);
  326. n_def_args = def_pos_args->len;
  327. n_extra_args = def_pos_args->len;
  328. }
  329. if (def_args != NULL && def_args[1] != MP_OBJ_NULL) {
  330. assert(mp_obj_is_type(def_args[1], &mp_type_dict));
  331. def_kw_args = def_args[1];
  332. n_extra_args += 1;
  333. }
  334. mp_obj_fun_bc_t *o = mp_obj_malloc_var(mp_obj_fun_bc_t, mp_obj_t, n_extra_args, &mp_type_fun_bc);
  335. o->bytecode = code;
  336. o->context = context;
  337. o->child_table = child_table;
  338. if (def_pos_args != NULL) {
  339. memcpy(o->extra_args, def_pos_args->items, n_def_args * sizeof(mp_obj_t));
  340. }
  341. if (def_kw_args != MP_OBJ_NULL) {
  342. o->extra_args[n_def_args] = def_kw_args;
  343. }
  344. return MP_OBJ_FROM_PTR(o);
  345. }
  346. /******************************************************************************/
  347. /* native functions */
  348. #if MICROPY_EMIT_NATIVE
  349. 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) {
  350. MP_STACK_CHECK();
  351. mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
  352. mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void *)self->bytecode);
  353. return fun(self_in, n_args, n_kw, args);
  354. }
  355. #if MICROPY_CPYTHON_COMPAT
  356. #define FUN_BC_TYPE_PRINT print, fun_bc_print,
  357. #else
  358. #define FUN_BC_TYPE_PRINT
  359. #endif
  360. #if MICROPY_PY_FUNCTION_ATTRS
  361. #define FUN_BC_TYPE_ATTR attr, mp_obj_fun_bc_attr,
  362. #else
  363. #define FUN_BC_TYPE_ATTR
  364. #endif
  365. STATIC MP_DEFINE_CONST_OBJ_TYPE(
  366. mp_type_fun_native,
  367. MP_QSTR_function,
  368. MP_TYPE_FLAG_BINDS_SELF,
  369. FUN_BC_TYPE_PRINT
  370. FUN_BC_TYPE_ATTR
  371. call, fun_native_call
  372. );
  373. mp_obj_t mp_obj_new_fun_native(const mp_obj_t *def_args, const void *fun_data, const mp_module_context_t *mc, struct _mp_raw_code_t *const *child_table) {
  374. mp_obj_fun_bc_t *o = MP_OBJ_TO_PTR(mp_obj_new_fun_bc(def_args, (const byte *)fun_data, mc, child_table));
  375. o->base.type = &mp_type_fun_native;
  376. return MP_OBJ_FROM_PTR(o);
  377. }
  378. #endif // MICROPY_EMIT_NATIVE
  379. /******************************************************************************/
  380. /* inline assembler functions */
  381. #if MICROPY_EMIT_INLINE_ASM
  382. typedef struct _mp_obj_fun_asm_t {
  383. mp_obj_base_t base;
  384. size_t n_args;
  385. const void *fun_data; // GC must be able to trace this pointer
  386. mp_uint_t type_sig;
  387. } mp_obj_fun_asm_t;
  388. typedef mp_uint_t (*inline_asm_fun_0_t)(void);
  389. typedef mp_uint_t (*inline_asm_fun_1_t)(mp_uint_t);
  390. typedef mp_uint_t (*inline_asm_fun_2_t)(mp_uint_t, mp_uint_t);
  391. typedef mp_uint_t (*inline_asm_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
  392. typedef mp_uint_t (*inline_asm_fun_4_t)(mp_uint_t, mp_uint_t, mp_uint_t, mp_uint_t);
  393. // convert a MicroPython object to a sensible value for inline asm
  394. STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
  395. // TODO for byte_array, pass pointer to the array
  396. if (mp_obj_is_small_int(obj)) {
  397. return MP_OBJ_SMALL_INT_VALUE(obj);
  398. } else if (obj == mp_const_none) {
  399. return 0;
  400. } else if (obj == mp_const_false) {
  401. return 0;
  402. } else if (obj == mp_const_true) {
  403. return 1;
  404. } else if (mp_obj_is_exact_type(obj, &mp_type_int)) {
  405. return mp_obj_int_get_truncated(obj);
  406. } else if (mp_obj_is_str(obj)) {
  407. // pointer to the string (it's probably constant though!)
  408. size_t l;
  409. return (mp_uint_t)mp_obj_str_get_data(obj, &l);
  410. } else {
  411. const mp_obj_type_t *type = mp_obj_get_type(obj);
  412. #if MICROPY_PY_BUILTINS_FLOAT
  413. if (type == &mp_type_float) {
  414. // convert float to int (could also pass in float registers)
  415. return (mp_int_t)mp_obj_float_get(obj);
  416. }
  417. #endif
  418. if (type == &mp_type_tuple || type == &mp_type_list) {
  419. // pointer to start of tuple (could pass length, but then could use len(x) for that)
  420. size_t len;
  421. mp_obj_t *items;
  422. mp_obj_get_array(obj, &len, &items);
  423. return (mp_uint_t)items;
  424. } else {
  425. mp_buffer_info_t bufinfo;
  426. if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_READ)) {
  427. // supports the buffer protocol, return a pointer to the data
  428. return (mp_uint_t)bufinfo.buf;
  429. } else {
  430. // just pass along a pointer to the object
  431. return (mp_uint_t)obj;
  432. }
  433. }
  434. }
  435. }
  436. 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) {
  437. mp_obj_fun_asm_t *self = MP_OBJ_TO_PTR(self_in);
  438. mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
  439. const void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
  440. mp_uint_t ret;
  441. if (n_args == 0) {
  442. ret = ((inline_asm_fun_0_t)fun)();
  443. } else if (n_args == 1) {
  444. ret = ((inline_asm_fun_1_t)fun)(convert_obj_for_inline_asm(args[0]));
  445. } else if (n_args == 2) {
  446. ret = ((inline_asm_fun_2_t)fun)(convert_obj_for_inline_asm(args[0]), convert_obj_for_inline_asm(args[1]));
  447. } else if (n_args == 3) {
  448. 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]));
  449. } else {
  450. // compiler allows at most 4 arguments
  451. assert(n_args == 4);
  452. ret = ((inline_asm_fun_4_t)fun)(
  453. convert_obj_for_inline_asm(args[0]),
  454. convert_obj_for_inline_asm(args[1]),
  455. convert_obj_for_inline_asm(args[2]),
  456. convert_obj_for_inline_asm(args[3])
  457. );
  458. }
  459. return mp_native_to_obj(ret, self->type_sig);
  460. }
  461. STATIC MP_DEFINE_CONST_OBJ_TYPE(
  462. mp_type_fun_asm,
  463. MP_QSTR_function,
  464. MP_TYPE_FLAG_BINDS_SELF,
  465. call, fun_asm_call
  466. );
  467. mp_obj_t mp_obj_new_fun_asm(size_t n_args, const void *fun_data, mp_uint_t type_sig) {
  468. mp_obj_fun_asm_t *o = mp_obj_malloc(mp_obj_fun_asm_t, &mp_type_fun_asm);
  469. o->n_args = n_args;
  470. o->fun_data = fun_data;
  471. o->type_sig = type_sig;
  472. return MP_OBJ_FROM_PTR(o);
  473. }
  474. #endif // MICROPY_EMIT_INLINE_ASM