Branch data Line data Source code
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 : :
28 : : #include <string.h>
29 : : #include <assert.h>
30 : :
31 : : #include "py/emitglue.h"
32 : : #include "py/objcode.h"
33 : : #include "py/objtuple.h"
34 : : #include "py/objfun.h"
35 : : #include "py/runtime.h"
36 : : #include "py/bc.h"
37 : : #include "py/cstack.h"
38 : :
39 : : #if MICROPY_DEBUG_VERBOSE // print debugging info
40 : : #define DEBUG_PRINT (1)
41 : : #else // don't print debugging info
42 : : #define DEBUG_PRINT (0)
43 : : #define DEBUG_printf(...) (void)0
44 : : #endif
45 : :
46 : : // Note: the "name" entry in mp_obj_type_t for a function type must be
47 : : // MP_QSTR_function because it is used to determine if an object is of generic
48 : : // function type.
49 : :
50 : : /******************************************************************************/
51 : : /* builtin functions */
52 : :
53 : 5810011 : 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) {
54 : 5810011 : (void)args;
55 [ + - - + ]: 5810011 : assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_0));
56 : 5810011 : mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
57 : 5810011 : mp_arg_check_num(n_args, n_kw, 0, 0, false);
58 : 5810011 : return self->fun._0();
59 : : }
60 : :
61 : : MP_DEFINE_CONST_OBJ_TYPE(
62 : : mp_type_fun_builtin_0, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
63 : : call, fun_builtin_0_call
64 : : );
65 : :
66 : 799987 : 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) {
67 [ + - - + ]: 799987 : assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_1));
68 : 799987 : mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
69 : 799987 : mp_arg_check_num(n_args, n_kw, 1, 1, false);
70 : 799987 : return self->fun._1(args[0]);
71 : : }
72 : :
73 : : MP_DEFINE_CONST_OBJ_TYPE(
74 : : mp_type_fun_builtin_1, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
75 : : call, fun_builtin_1_call
76 : : );
77 : :
78 : 6131721 : 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) {
79 [ + - - + ]: 6131721 : assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_2));
80 : 6131721 : mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
81 : 6131721 : mp_arg_check_num(n_args, n_kw, 2, 2, false);
82 : 6131704 : return self->fun._2(args[0], args[1]);
83 : : }
84 : :
85 : : MP_DEFINE_CONST_OBJ_TYPE(
86 : : mp_type_fun_builtin_2, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
87 : : call, fun_builtin_2_call
88 : : );
89 : :
90 : 8005 : 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) {
91 [ + - - + ]: 8005 : assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_3));
92 : 8005 : mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
93 : 8005 : mp_arg_check_num(n_args, n_kw, 3, 3, false);
94 : 8003 : return self->fun._3(args[0], args[1], args[2]);
95 : : }
96 : :
97 : : MP_DEFINE_CONST_OBJ_TYPE(
98 : : mp_type_fun_builtin_3, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
99 : : call, fun_builtin_3_call
100 : : );
101 : :
102 : 1539326 : 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) {
103 [ + - - + ]: 1539326 : assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_var));
104 : 1539326 : mp_obj_fun_builtin_var_t *self = MP_OBJ_TO_PTR(self_in);
105 : :
106 : : // check number of arguments
107 : 1539326 : mp_arg_check_num_sig(n_args, n_kw, self->sig);
108 : :
109 [ + + ]: 1539317 : if (self->sig & 1) {
110 : : // function allows keywords
111 : :
112 : : // we create a map directly from the given args array; self->fun.kw does still
113 : : // expect args to have both positional and keyword arguments, ordered as:
114 : : // arg0 arg1 ... arg<n_args> key0 value0 key1 value1 ... key<n_kw> value<n_kw>
115 : 809352 : mp_map_t kw_args;
116 : 809352 : mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
117 : :
118 : 809352 : return self->fun.kw(n_args, args, &kw_args);
119 : :
120 : : } else {
121 : : // function takes a variable number of arguments, but no keywords
122 : :
123 : 729965 : return self->fun.var(n_args, args);
124 : : }
125 : : }
126 : :
127 : : MP_DEFINE_CONST_OBJ_TYPE(
128 : : mp_type_fun_builtin_var, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
129 : : call, fun_builtin_var_call
130 : : );
131 : :
132 : : /******************************************************************************/
133 : : /* byte code functions */
134 : :
135 : 65 : qstr mp_obj_fun_get_name(mp_const_obj_t fun_in) {
136 : 65 : const mp_obj_fun_bc_t *fun = MP_OBJ_TO_PTR(fun_in);
137 : 65 : const byte *bc = fun->bytecode;
138 : :
139 : : #if MICROPY_EMIT_NATIVE
140 [ + + + + ]: 65 : if (fun->base.type == &mp_type_fun_native || fun->base.type == &mp_type_native_gen_wrap) {
141 [ + + ]: 34 : bc = mp_obj_fun_native_get_prelude_ptr(fun);
142 : : }
143 : : #endif
144 : :
145 [ + + ]: 97 : MP_BC_PRELUDE_SIG_DECODE(bc);
146 [ - + ]: 65 : MP_BC_PRELUDE_SIZE_DECODE(bc);
147 : :
148 : 65 : mp_uint_t name = mp_decode_uint_value(bc);
149 : : #if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE
150 : 65 : name = fun->context->constants.qstr_table[name];
151 : : #endif
152 : :
153 : 65 : return name;
154 : : }
155 : :
156 : : #if MICROPY_PY_FUNCTION_ATTRS_CODE
157 : 19 : static mp_obj_t fun_bc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
158 : 19 : (void)type;
159 : 19 : mp_arg_check_num(n_args, n_kw, 2, 2, false);
160 : :
161 [ - + - + : 18 : if (!mp_obj_is_type(args[0], &mp_type_code)) {
- + - + +
+ - + ]
162 : 2 : mp_raise_TypeError(NULL);
163 : : }
164 [ - + - + : 16 : if (!mp_obj_is_type(args[1], &mp_type_dict)) {
- + - + +
+ - + ]
165 : 2 : mp_raise_TypeError(NULL);
166 : : }
167 : :
168 : 14 : mp_obj_code_t *code = MP_OBJ_TO_PTR(args[0]);
169 : 14 : mp_obj_t globals = args[1];
170 : :
171 : 14 : mp_module_context_t *module_context = m_new_obj(mp_module_context_t);
172 : 14 : module_context->module.base.type = &mp_type_module;
173 : 14 : module_context->module.globals = MP_OBJ_TO_PTR(globals);
174 : 14 : module_context->constants = *mp_code_get_constants(code);
175 : :
176 : 14 : return mp_make_function_from_proto_fun(mp_code_get_proto_fun(code), module_context, NULL);
177 : : }
178 : : #endif
179 : :
180 : : #if MICROPY_CPYTHON_COMPAT
181 : 5 : static void fun_bc_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
182 : 5 : (void)kind;
183 : 5 : mp_obj_fun_bc_t *o = MP_OBJ_TO_PTR(o_in);
184 : 5 : mp_printf(print, "<function %q at 0x%p>", mp_obj_fun_get_name(o_in), o);
185 : 5 : }
186 : : #endif
187 : :
188 : : #if DEBUG_PRINT
189 : : static void dump_args(const mp_obj_t *a, size_t sz) {
190 : : DEBUG_printf("%p: ", a);
191 : : for (size_t i = 0; i < sz; i++) {
192 : : DEBUG_printf("%p ", a[i]);
193 : : }
194 : : DEBUG_printf("\n");
195 : : }
196 : : #else
197 : : #define dump_args(...) (void)0
198 : : #endif
199 : :
200 : : // With this macro you can tune the maximum number of function state bytes
201 : : // that will be allocated on the stack. Any function that needs more
202 : : // than this will try to use the heap, with fallback to stack allocation.
203 : : #define VM_MAX_STATE_ON_STACK (sizeof(mp_uint_t) * 11)
204 : :
205 : : #define DECODE_CODESTATE_SIZE(bytecode, n_state_out_var, state_size_out_var) \
206 : : { \
207 : : const uint8_t *ip = bytecode; \
208 : : size_t n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_args; \
209 : : 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); \
210 : : (void)scope_flags; (void)n_pos_args; (void)n_kwonly_args; (void)n_def_args; \
211 : : \
212 : : /* state size in bytes */ \
213 : : state_size_out_var = n_state_out_var * sizeof(mp_obj_t) \
214 : : + n_exc_stack * sizeof(mp_exc_stack_t); \
215 : : }
216 : :
217 : : #define INIT_CODESTATE(code_state, _fun_bc, _n_state, n_args, n_kw, args) \
218 : : code_state->fun_bc = _fun_bc; \
219 : : code_state->n_state = _n_state; \
220 : : mp_setup_code_state(code_state, n_args, n_kw, args); \
221 : : code_state->old_globals = mp_globals_get();
222 : :
223 : : #if MICROPY_STACKLESS
224 : : 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) {
225 : : mp_cstack_check();
226 : : mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
227 : :
228 : : size_t n_state, state_size;
229 : : DECODE_CODESTATE_SIZE(self->bytecode, n_state, state_size);
230 : :
231 : : mp_code_state_t *code_state;
232 : : #if MICROPY_ENABLE_PYSTACK
233 : : code_state = mp_pystack_alloc(sizeof(mp_code_state_t) + state_size);
234 : : #else
235 : : // If we use m_new_obj_var(), then on no memory, MemoryError will be
236 : : // raised. But this is not correct exception for a function call,
237 : : // RuntimeError should be raised instead. So, we use m_new_obj_var_maybe(),
238 : : // return NULL, then vm.c takes the needed action (either raise
239 : : // RuntimeError or fallback to stack allocation).
240 : : code_state = m_new_obj_var_maybe(mp_code_state_t, state, byte, state_size);
241 : : if (!code_state) {
242 : : return NULL;
243 : : }
244 : : #endif
245 : :
246 : : INIT_CODESTATE(code_state, self, n_state, n_args, n_kw, args);
247 : :
248 : : // execute the byte code with the correct globals context
249 : : mp_globals_set(self->context->module.globals);
250 : :
251 : : return code_state;
252 : : }
253 : : #endif
254 : :
255 : 8147892 : 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) {
256 : 8147892 : mp_cstack_check();
257 : :
258 : 7933070 : DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
259 : 7933070 : DEBUG_printf("Input pos args: ");
260 : 7933070 : dump_args(args, n_args);
261 : 7933070 : DEBUG_printf("Input kw args: ");
262 : 7933070 : dump_args(args + n_args, n_kw * 2);
263 : :
264 : 7933070 : mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
265 : :
266 : 7933070 : size_t n_state, state_size;
267 [ + + ]: 8273633 : DECODE_CODESTATE_SIZE(self->bytecode, n_state, state_size);
268 : :
269 : : // allocate state for locals and stack
270 : 7933070 : mp_code_state_t *code_state = NULL;
271 : : #if MICROPY_ENABLE_PYSTACK
272 : : code_state = mp_pystack_alloc(offsetof(mp_code_state_t, state) + state_size);
273 : : #else
274 [ + + ]: 7933070 : if (state_size > VM_MAX_STATE_ON_STACK) {
275 : 319410 : code_state = m_new_obj_var_maybe(mp_code_state_t, state, byte, state_size);
276 : : #if MICROPY_DEBUG_VM_STACK_OVERFLOW
277 : : if (code_state != NULL) {
278 : : memset(code_state->state, 0, state_size);
279 : : }
280 : : #endif
281 : : }
282 [ + + ]: 319137 : if (code_state == NULL) {
283 : 7613676 : code_state = alloca(offsetof(mp_code_state_t, state) + state_size);
284 : : #if MICROPY_DEBUG_VM_STACK_OVERFLOW
285 : : memset(code_state->state, 0, state_size);
286 : : #endif
287 : 7613676 : state_size = 0; // indicate that we allocated using alloca
288 : : }
289 : : #endif
290 : :
291 : 7932797 : INIT_CODESTATE(code_state, self, n_state, n_args, n_kw, args);
292 : :
293 : : // execute the byte code with the correct globals context
294 : 8067738 : mp_globals_set(self->context->module.globals);
295 : 8126147 : mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
296 : 7877598 : mp_globals_set(code_state->old_globals);
297 : :
298 : : #if MICROPY_DEBUG_VM_STACK_OVERFLOW
299 : : if (vm_return_kind == MP_VM_RETURN_NORMAL) {
300 : : if (code_state->sp < code_state->state) {
301 : : mp_printf(MICROPY_DEBUG_PRINTER, "VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
302 : : assert(0);
303 : : }
304 : : }
305 : : const byte *bytecode_ptr = self->bytecode;
306 : : size_t n_state_unused, n_exc_stack_unused, scope_flags_unused;
307 : : size_t n_pos_args, n_kwonly_args, n_def_args_unused;
308 : : MP_BC_PRELUDE_SIG_DECODE_INTO(bytecode_ptr, n_state_unused, n_exc_stack_unused,
309 : : scope_flags_unused, n_pos_args, n_kwonly_args, n_def_args_unused);
310 : : // We can't check the case when an exception is returned in state[0]
311 : : // and there are no arguments, because in this case our detection slot may have
312 : : // been overwritten by the returned exception (which is allowed).
313 : : if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && n_pos_args + n_kwonly_args == 0)) {
314 : : // Just check to see that we have at least 1 null object left in the state.
315 : : bool overflow = true;
316 : : for (size_t i = 0; i < n_state - n_pos_args - n_kwonly_args; ++i) {
317 : : if (code_state->state[i] == MP_OBJ_NULL) {
318 : : overflow = false;
319 : : break;
320 : : }
321 : : }
322 : : if (overflow) {
323 : : mp_printf(MICROPY_DEBUG_PRINTER, "VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
324 : : assert(0);
325 : : }
326 : : }
327 : : #endif
328 : :
329 : 8184852 : mp_obj_t result;
330 [ + + ]: 8184852 : if (vm_return_kind == MP_VM_RETURN_NORMAL) {
331 : : // return value is in *sp
332 : 8052656 : result = *code_state->sp;
333 : : } else {
334 : : // must be an exception because normal functions can't yield
335 [ - + ]: 132196 : assert(vm_return_kind == MP_VM_RETURN_EXCEPTION);
336 : : // returned exception is in state[0]
337 : 132196 : result = code_state->state[0];
338 : : }
339 : :
340 : : #if MICROPY_ENABLE_PYSTACK
341 : : mp_pystack_free(code_state);
342 : : #else
343 : : // free the state if it was allocated on the heap
344 [ + + ]: 8184852 : if (state_size != 0) {
345 : 318087 : m_del_var(mp_code_state_t, state, byte, state_size, code_state);
346 : : }
347 : : #endif
348 : :
349 [ + + ]: 8186253 : if (vm_return_kind == MP_VM_RETURN_NORMAL) {
350 : 8054057 : return result;
351 : : } else { // MP_VM_RETURN_EXCEPTION
352 : 132196 : nlr_raise(result);
353 : : }
354 : : }
355 : :
356 : : #if MICROPY_PY_FUNCTION_ATTRS
357 : 11468 : void mp_obj_fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
358 [ + + ]: 11468 : if (dest[0] != MP_OBJ_NULL) {
359 : : // not load attribute
360 : : return;
361 : : }
362 [ + + ]: 11458 : if (attr == MP_QSTR___name__) {
363 : 44 : dest[0] = MP_OBJ_NEW_QSTR(mp_obj_fun_get_name(self_in));
364 : : }
365 [ + + ]: 11458 : if (attr == MP_QSTR___globals__) {
366 : 36 : mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
367 : 36 : dest[0] = MP_OBJ_FROM_PTR(self->context->module.globals);
368 : : }
369 : : #if MICROPY_PY_FUNCTION_ATTRS_CODE
370 [ + + ]: 11458 : if (attr == MP_QSTR___code__) {
371 : 32 : const mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
372 [ + + ]: 32 : if ((self->base.type == &mp_type_fun_bc
373 [ - + ]: 10 : || self->base.type == &mp_type_gen_wrap)
374 [ + + ]: 22 : && self->child_table == NULL) {
375 : : #if MICROPY_PY_BUILTINS_CODE <= MICROPY_PY_BUILTINS_CODE_BASIC
376 : 20 : dest[0] = mp_obj_new_code(self->context->constants, self->bytecode);
377 : : #else
378 : : dest[0] = mp_obj_new_code(self->context, self->rc, true);
379 : : #endif
380 : : }
381 : : }
382 : : #endif
383 : : }
384 : : #endif
385 : :
386 : : #if MICROPY_PY_FUNCTION_ATTRS_CODE
387 : : #define FUN_BC_MAKE_NEW make_new, fun_bc_make_new,
388 : : #else
389 : : #define FUN_BC_MAKE_NEW
390 : : #endif
391 : :
392 : : #if MICROPY_CPYTHON_COMPAT
393 : : #define FUN_BC_TYPE_PRINT print, fun_bc_print,
394 : : #else
395 : : #define FUN_BC_TYPE_PRINT
396 : : #endif
397 : :
398 : : #if MICROPY_PY_FUNCTION_ATTRS
399 : : #define FUN_BC_TYPE_ATTR attr, mp_obj_fun_bc_attr,
400 : : #else
401 : : #define FUN_BC_TYPE_ATTR
402 : : #endif
403 : :
404 : : MP_DEFINE_CONST_OBJ_TYPE(
405 : : mp_type_fun_bc,
406 : : MP_QSTR_function,
407 : : MP_TYPE_FLAG_BINDS_SELF,
408 : : FUN_BC_MAKE_NEW
409 : : FUN_BC_TYPE_PRINT
410 : : FUN_BC_TYPE_ATTR
411 : : call, fun_bc_call
412 : : );
413 : :
414 : 19241 : 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) {
415 : 19241 : size_t n_def_args = 0;
416 : 19241 : size_t n_extra_args = 0;
417 : 19241 : mp_obj_tuple_t *def_pos_args = NULL;
418 : 19241 : mp_obj_t def_kw_args = MP_OBJ_NULL;
419 [ + + + + ]: 19241 : if (def_args != NULL && def_args[0] != MP_OBJ_NULL) {
420 [ - + - + : 584 : assert(mp_obj_is_type(def_args[0], &mp_type_tuple));
- + - + +
- - + ]
421 : 584 : def_pos_args = MP_OBJ_TO_PTR(def_args[0]);
422 : 584 : n_def_args = def_pos_args->len;
423 : 584 : n_extra_args = def_pos_args->len;
424 : : }
425 [ + + ]: 622 : if (def_args != NULL && def_args[1] != MP_OBJ_NULL) {
426 [ - + - + : 42 : assert(mp_obj_is_type(def_args[1], &mp_type_dict));
- + - + +
- - + ]
427 : 42 : def_kw_args = def_args[1];
428 : 42 : n_extra_args += 1;
429 : : }
430 : 19241 : 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);
431 : 19241 : o->bytecode = code;
432 : 19241 : o->context = context;
433 : 19241 : o->child_table = child_table;
434 [ + + ]: 19241 : if (def_pos_args != NULL) {
435 : 584 : memcpy(o->extra_args, def_pos_args->items, n_def_args * sizeof(mp_obj_t));
436 : : }
437 [ + + ]: 19241 : if (def_kw_args != MP_OBJ_NULL) {
438 : 42 : o->extra_args[n_def_args] = def_kw_args;
439 : : }
440 : 19241 : return MP_OBJ_FROM_PTR(o);
441 : : }
442 : :
443 : : /******************************************************************************/
444 : : /* native functions */
445 : :
446 : : #if MICROPY_EMIT_NATIVE
447 : :
448 : 947482 : 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) {
449 : 947482 : mp_cstack_check();
450 : 947479 : mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
451 : 947479 : mp_call_fun_t fun = mp_obj_fun_native_get_function_start(self);
452 : 947479 : return fun(self_in, n_args, n_kw, args);
453 : : }
454 : :
455 : : #if MICROPY_CPYTHON_COMPAT
456 : : #define FUN_BC_TYPE_PRINT print, fun_bc_print,
457 : : #else
458 : : #define FUN_BC_TYPE_PRINT
459 : : #endif
460 : : #if MICROPY_PY_FUNCTION_ATTRS
461 : : #define FUN_BC_TYPE_ATTR attr, mp_obj_fun_bc_attr,
462 : : #else
463 : : #define FUN_BC_TYPE_ATTR
464 : : #endif
465 : :
466 : : MP_DEFINE_CONST_OBJ_TYPE(
467 : : mp_type_fun_native,
468 : : MP_QSTR_function,
469 : : MP_TYPE_FLAG_BINDS_SELF,
470 : : FUN_BC_TYPE_PRINT
471 : : FUN_BC_TYPE_ATTR
472 : : call, fun_native_call
473 : : );
474 : :
475 : : #endif // MICROPY_EMIT_NATIVE
476 : :
477 : : /******************************************************************************/
478 : : /* viper functions */
479 : :
480 : : #if MICROPY_EMIT_NATIVE
481 : :
482 : 932 : 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) {
483 : 932 : mp_cstack_check();
484 : 932 : mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
485 : 932 : mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void *)self->bytecode);
486 : 932 : return fun(self_in, n_args, n_kw, args);
487 : : }
488 : :
489 : : MP_DEFINE_CONST_OBJ_TYPE(
490 : : mp_type_fun_viper,
491 : : MP_QSTR_function,
492 : : MP_TYPE_FLAG_BINDS_SELF,
493 : : call, fun_viper_call
494 : : );
495 : :
496 : : #endif // MICROPY_EMIT_NATIVE
497 : :
498 : : /******************************************************************************/
499 : : /* inline assembler functions */
500 : :
501 : : #if MICROPY_EMIT_INLINE_ASM
502 : :
503 : : typedef mp_uint_t (*inline_asm_fun_0_t)(void);
504 : : typedef mp_uint_t (*inline_asm_fun_1_t)(mp_uint_t);
505 : : typedef mp_uint_t (*inline_asm_fun_2_t)(mp_uint_t, mp_uint_t);
506 : : typedef mp_uint_t (*inline_asm_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
507 : : typedef mp_uint_t (*inline_asm_fun_4_t)(mp_uint_t, mp_uint_t, mp_uint_t, mp_uint_t);
508 : :
509 : : // convert a MicroPython object to a sensible value for inline asm
510 : : static mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
511 : : // TODO for byte_array, pass pointer to the array
512 : : if (mp_obj_is_small_int(obj)) {
513 : : return MP_OBJ_SMALL_INT_VALUE(obj);
514 : : } else if (obj == mp_const_none) {
515 : : return 0;
516 : : } else if (obj == mp_const_false) {
517 : : return 0;
518 : : } else if (obj == mp_const_true) {
519 : : return 1;
520 : : } else if (mp_obj_is_exact_type(obj, &mp_type_int)) {
521 : : return mp_obj_int_get_truncated(obj);
522 : : } else if (mp_obj_is_str(obj)) {
523 : : // pointer to the string (it's probably constant though!)
524 : : size_t l;
525 : : return (mp_uint_t)mp_obj_str_get_data(obj, &l);
526 : : } else {
527 : : const mp_obj_type_t *type = mp_obj_get_type(obj);
528 : : #if MICROPY_PY_BUILTINS_FLOAT
529 : : if (type == &mp_type_float) {
530 : : // convert float to int (could also pass in float registers)
531 : : return (mp_int_t)mp_obj_float_get(obj);
532 : : }
533 : : #endif
534 : : if (type == &mp_type_tuple || type == &mp_type_list) {
535 : : // pointer to start of tuple (could pass length, but then could use len(x) for that)
536 : : size_t len;
537 : : mp_obj_t *items;
538 : : mp_obj_get_array(obj, &len, &items);
539 : : return (mp_uint_t)items;
540 : : } else {
541 : : mp_buffer_info_t bufinfo;
542 : : if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_READ)) {
543 : : // supports the buffer protocol, return a pointer to the data
544 : : return (mp_uint_t)bufinfo.buf;
545 : : } else {
546 : : // just pass along a pointer to the object
547 : : return (mp_uint_t)obj;
548 : : }
549 : : }
550 : : }
551 : : }
552 : :
553 : : 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) {
554 : : mp_obj_fun_asm_t *self = MP_OBJ_TO_PTR(self_in);
555 : :
556 : : mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
557 : :
558 : : const void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
559 : :
560 : : mp_uint_t ret;
561 : : if (n_args == 0) {
562 : : ret = ((inline_asm_fun_0_t)fun)();
563 : : } else if (n_args == 1) {
564 : : ret = ((inline_asm_fun_1_t)fun)(convert_obj_for_inline_asm(args[0]));
565 : : } else if (n_args == 2) {
566 : : ret = ((inline_asm_fun_2_t)fun)(convert_obj_for_inline_asm(args[0]), convert_obj_for_inline_asm(args[1]));
567 : : } else if (n_args == 3) {
568 : : 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]));
569 : : } else {
570 : : // compiler allows at most 4 arguments
571 : : assert(n_args == 4);
572 : : ret = ((inline_asm_fun_4_t)fun)(
573 : : convert_obj_for_inline_asm(args[0]),
574 : : convert_obj_for_inline_asm(args[1]),
575 : : convert_obj_for_inline_asm(args[2]),
576 : : convert_obj_for_inline_asm(args[3])
577 : : );
578 : : }
579 : :
580 : : return mp_native_to_obj(ret, self->type_sig);
581 : : }
582 : :
583 : : MP_DEFINE_CONST_OBJ_TYPE(
584 : : mp_type_fun_asm,
585 : : MP_QSTR_function,
586 : : MP_TYPE_FLAG_BINDS_SELF,
587 : : call, fun_asm_call
588 : : );
589 : :
590 : : #endif // MICROPY_EMIT_INLINE_ASM
|