objfun.c 19 KB

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