2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2014-05-13 01:44:45 -04:00
|
|
|
* Copyright (c) 2014 Paul Sokolovsky
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/objtuple.h"
|
|
|
|
#include "py/objfun.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/bc.h"
|
|
|
|
#include "py/stackctrl.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
|
2020-01-08 23:32:45 -05:00
|
|
|
#include "supervisor/linker.h"
|
|
|
|
|
2017-07-24 12:55:14 -04:00
|
|
|
#if MICROPY_DEBUG_VERBOSE // print debugging info
|
2014-02-16 11:30:49 -05:00
|
|
|
#define DEBUG_PRINT (1)
|
|
|
|
#else // don't print debugging info
|
2014-11-05 16:16:41 -05:00
|
|
|
#define DEBUG_PRINT (0)
|
2014-02-26 17:40:35 -05:00
|
|
|
#define DEBUG_printf(...) (void)0
|
2014-02-16 11:30:49 -05:00
|
|
|
#endif
|
|
|
|
|
2015-01-11 16:07:15 -05:00
|
|
|
// Note: the "name" entry in mp_obj_type_t for a function type must be
|
|
|
|
// MP_QSTR_function because it is used to determine if an object is of generic
|
|
|
|
// function type.
|
2014-04-05 06:50:06 -04:00
|
|
|
|
2014-08-24 11:28:17 -04:00
|
|
|
/******************************************************************************/
|
|
|
|
/* builtin functions */
|
|
|
|
|
2016-10-17 20:49:27 -04:00
|
|
|
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) {
|
|
|
|
(void)args;
|
|
|
|
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_0));
|
|
|
|
mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-01-14 18:14:40 -05:00
|
|
|
mp_arg_check_num_kw_array(n_args, n_kw, 0, 0, false);
|
2016-10-17 20:49:27 -04:00
|
|
|
return self->fun._0();
|
|
|
|
}
|
|
|
|
|
|
|
|
const mp_obj_type_t mp_type_fun_builtin_0 = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_function,
|
|
|
|
.call = fun_builtin_0_call,
|
|
|
|
.unary_op = mp_generic_unary_op,
|
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
|
|
|
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_1));
|
|
|
|
mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-01-14 18:14:40 -05:00
|
|
|
mp_arg_check_num_kw_array(n_args, n_kw, 1, 1, false);
|
2016-10-17 20:49:27 -04:00
|
|
|
return self->fun._1(args[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const mp_obj_type_t mp_type_fun_builtin_1 = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_function,
|
|
|
|
.call = fun_builtin_1_call,
|
|
|
|
.unary_op = mp_generic_unary_op,
|
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
|
|
|
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_2));
|
|
|
|
mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-01-14 18:14:40 -05:00
|
|
|
mp_arg_check_num_kw_array(n_args, n_kw, 2, 2, false);
|
2016-10-17 20:49:27 -04:00
|
|
|
return self->fun._2(args[0], args[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const mp_obj_type_t mp_type_fun_builtin_2 = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_function,
|
|
|
|
.call = fun_builtin_2_call,
|
|
|
|
.unary_op = mp_generic_unary_op,
|
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
|
|
|
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_3));
|
|
|
|
mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-01-14 18:14:40 -05:00
|
|
|
mp_arg_check_num_kw_array(n_args, n_kw, 3, 3, false);
|
2016-10-17 20:49:27 -04:00
|
|
|
return self->fun._3(args[0], args[1], args[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const mp_obj_type_t mp_type_fun_builtin_3 = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_function,
|
|
|
|
.call = fun_builtin_3_call,
|
|
|
|
.unary_op = mp_generic_unary_op,
|
|
|
|
};
|
2014-08-24 11:28:17 -04:00
|
|
|
|
2016-10-17 20:49:27 -04:00
|
|
|
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) {
|
|
|
|
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_var));
|
|
|
|
mp_obj_fun_builtin_var_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-01-18 09:10:48 -05:00
|
|
|
|
2014-01-13 14:55:18 -05:00
|
|
|
// check number of arguments
|
2018-09-13 23:39:17 -04:00
|
|
|
mp_arg_check_num_sig(n_args, n_kw, self->sig);
|
2014-01-18 09:10:48 -05:00
|
|
|
|
2018-09-13 23:39:17 -04:00
|
|
|
if (self->sig & 1) {
|
2014-01-18 09:10:48 -05:00
|
|
|
// function allows keywords
|
|
|
|
|
2014-02-08 13:53:41 -05:00
|
|
|
// we create a map directly from the given args array
|
|
|
|
mp_map_t kw_args;
|
|
|
|
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
|
2014-01-18 09:10:48 -05:00
|
|
|
|
2016-01-03 06:53:44 -05:00
|
|
|
return self->fun.kw(n_args, args, &kw_args);
|
2014-01-18 09:10:48 -05:00
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
} else {
|
2014-01-18 09:10:48 -05:00
|
|
|
// function takes a variable number of arguments, but no keywords
|
2013-12-21 13:17:45 -05:00
|
|
|
|
2016-01-03 06:53:44 -05:00
|
|
|
return self->fun.var(n_args, args);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 20:49:27 -04:00
|
|
|
const mp_obj_type_t mp_type_fun_builtin_var = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
2014-02-15 06:34:50 -05:00
|
|
|
.name = MP_QSTR_function,
|
2016-10-17 20:49:27 -04:00
|
|
|
.call = fun_builtin_var_call,
|
2015-05-11 08:25:19 -04:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2013-12-21 13:17:45 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* byte code functions */
|
|
|
|
|
2015-02-14 12:44:31 -05:00
|
|
|
qstr mp_obj_code_get_name(const byte *code_info) {
|
2017-06-08 23:31:57 -04:00
|
|
|
code_info = mp_decode_uint_skip(code_info); // skip code_info_size entry
|
2015-11-02 12:27:18 -05:00
|
|
|
#if MICROPY_PERSISTENT_CODE
|
|
|
|
return code_info[0] | (code_info[1] << 8);
|
|
|
|
#else
|
2017-06-08 23:31:57 -04:00
|
|
|
return mp_decode_uint_value(code_info);
|
2015-11-02 12:27:18 -05:00
|
|
|
#endif
|
2014-04-30 18:32:58 -04:00
|
|
|
}
|
|
|
|
|
2015-02-14 12:44:31 -05:00
|
|
|
qstr mp_obj_fun_get_name(mp_const_obj_t fun_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
const mp_obj_fun_bc_t *fun = MP_OBJ_TO_PTR(fun_in);
|
2015-04-06 17:38:53 -04:00
|
|
|
#if MICROPY_EMIT_NATIVE
|
|
|
|
if (fun->base.type == &mp_type_fun_native) {
|
|
|
|
// TODO native functions don't have name stored
|
|
|
|
return MP_QSTR_;
|
|
|
|
}
|
|
|
|
#endif
|
2015-03-18 13:47:47 -04:00
|
|
|
|
|
|
|
const byte *bc = fun->bytecode;
|
2017-06-08 23:31:57 -04:00
|
|
|
bc = mp_decode_uint_skip(bc); // skip n_state
|
|
|
|
bc = mp_decode_uint_skip(bc); // skip n_exc_stack
|
2015-10-22 18:45:37 -04:00
|
|
|
bc++; // skip scope_params
|
2015-10-22 20:23:11 -04:00
|
|
|
bc++; // skip n_pos_args
|
|
|
|
bc++; // skip n_kwonly_args
|
2015-10-22 18:45:37 -04:00
|
|
|
bc++; // skip n_def_pos_args
|
2015-03-18 13:47:47 -04:00
|
|
|
return mp_obj_code_get_name(bc);
|
2014-05-01 15:20:07 -04:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:32:58 -04:00
|
|
|
#if MICROPY_CPYTHON_COMPAT
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void fun_bc_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_fun_bc_t *o = MP_OBJ_TO_PTR(o_in);
|
|
|
|
mp_printf(print, "<function %q at 0x%p>", mp_obj_fun_get_name(o_in), o);
|
2014-04-30 18:32:58 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-16 11:30:49 -05:00
|
|
|
#if DEBUG_PRINT
|
2017-02-16 00:34:53 -05:00
|
|
|
STATIC void dump_args(const mp_obj_t *a, size_t sz) {
|
2014-02-16 11:30:49 -05:00
|
|
|
DEBUG_printf("%p: ", a);
|
2017-02-16 00:34:53 -05:00
|
|
|
for (size_t i = 0; i < sz; i++) {
|
2014-02-16 11:30:49 -05:00
|
|
|
DEBUG_printf("%p ", a[i]);
|
|
|
|
}
|
|
|
|
DEBUG_printf("\n");
|
|
|
|
}
|
2014-03-31 10:59:25 -04:00
|
|
|
#else
|
|
|
|
#define dump_args(...) (void)0
|
|
|
|
#endif
|
2014-02-16 11:30:49 -05:00
|
|
|
|
2014-06-07 09:16:08 -04:00
|
|
|
// With this macro you can tune the maximum number of function state bytes
|
|
|
|
// that will be allocated on the stack. Any function that needs more
|
2015-06-08 17:07:27 -04:00
|
|
|
// than this will try to use the heap, with fallback to stack allocation.
|
2015-02-15 04:12:58 -05:00
|
|
|
#define VM_MAX_STATE_ON_STACK (11 * sizeof(mp_uint_t))
|
2014-06-07 09:16:08 -04:00
|
|
|
|
2017-12-09 02:19:34 -05:00
|
|
|
#define DECODE_CODESTATE_SIZE(bytecode, n_state_out_var, state_size_out_var) \
|
|
|
|
{ \
|
|
|
|
/* bytecode prelude: state size and exception stack size */ \
|
|
|
|
n_state_out_var = mp_decode_uint_value(bytecode); \
|
|
|
|
size_t n_exc_stack = mp_decode_uint_value(mp_decode_uint_skip(bytecode)); \
|
|
|
|
\
|
|
|
|
/* state size in bytes */ \
|
2018-05-16 20:08:34 -04:00
|
|
|
state_size_out_var = n_state_out_var * sizeof(mp_obj_t) \
|
2021-03-15 09:57:36 -04:00
|
|
|
+ n_exc_stack * sizeof(mp_exc_stack_t); \
|
2017-12-09 02:19:34 -05:00
|
|
|
}
|
|
|
|
|
2017-12-09 05:45:28 -05:00
|
|
|
#define INIT_CODESTATE(code_state, _fun_bc, n_args, n_kw, args) \
|
|
|
|
code_state->fun_bc = _fun_bc; \
|
|
|
|
code_state->ip = 0; \
|
|
|
|
mp_setup_code_state(code_state, n_args, n_kw, args); \
|
|
|
|
code_state->old_globals = mp_globals_get();
|
|
|
|
|
2015-04-01 18:31:30 -04:00
|
|
|
#if MICROPY_STACKLESS
|
2016-08-27 09:21:00 -04:00
|
|
|
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) {
|
2015-03-27 19:14:44 -04:00
|
|
|
MP_STACK_CHECK();
|
2015-12-17 07:32:41 -05:00
|
|
|
mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-03-27 19:14:44 -04:00
|
|
|
|
2017-12-09 02:19:34 -05:00
|
|
|
size_t n_state, state_size;
|
|
|
|
DECODE_CODESTATE_SIZE(self->bytecode, n_state, state_size);
|
2015-03-27 19:14:44 -04:00
|
|
|
|
2016-08-27 09:21:00 -04:00
|
|
|
mp_code_state_t *code_state;
|
2017-11-26 07:37:19 -05:00
|
|
|
#if MICROPY_ENABLE_PYSTACK
|
|
|
|
code_state = mp_pystack_alloc(sizeof(mp_code_state_t) + state_size);
|
|
|
|
#else
|
2017-12-09 03:57:40 -05:00
|
|
|
// If we use m_new_obj_var(), then on no memory, MemoryError will be
|
|
|
|
// raised. But this is not correct exception for a function call,
|
|
|
|
// RuntimeError should be raised instead. So, we use m_new_obj_var_maybe(),
|
|
|
|
// return NULL, then vm.c takes the needed action (either raise
|
|
|
|
// RuntimeError or fallback to stack allocation).
|
2016-08-27 09:21:00 -04:00
|
|
|
code_state = m_new_obj_var_maybe(mp_code_state_t, byte, state_size);
|
2015-03-27 19:14:44 -04:00
|
|
|
if (!code_state) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-11-26 07:37:19 -05:00
|
|
|
#endif
|
2015-03-27 19:14:44 -04:00
|
|
|
|
2017-12-09 05:45:28 -05:00
|
|
|
INIT_CODESTATE(code_state, self, n_args, n_kw, args);
|
2015-03-27 19:14:44 -04:00
|
|
|
|
|
|
|
// execute the byte code with the correct globals context
|
|
|
|
mp_globals_set(self->globals);
|
|
|
|
|
|
|
|
return code_state;
|
|
|
|
}
|
2015-04-01 18:31:30 -04:00
|
|
|
#endif
|
2015-03-27 19:14:44 -04:00
|
|
|
|
2020-01-08 23:32:45 -05:00
|
|
|
STATIC mp_obj_t PLACE_IN_ITCM(fun_bc_call)(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2014-06-30 19:13:42 -04:00
|
|
|
MP_STACK_CHECK();
|
2014-06-11 10:56:43 -04:00
|
|
|
|
2014-09-23 05:59:05 -04:00
|
|
|
DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
|
2014-06-11 10:56:43 -04:00
|
|
|
DEBUG_printf("Input pos args: ");
|
|
|
|
dump_args(args, n_args);
|
|
|
|
DEBUG_printf("Input kw args: ");
|
|
|
|
dump_args(args + n_args, n_kw * 2);
|
2018-08-02 00:17:24 -04:00
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-06-11 10:56:43 -04:00
|
|
|
|
2017-12-09 02:19:34 -05:00
|
|
|
size_t n_state, state_size;
|
|
|
|
DECODE_CODESTATE_SIZE(self->bytecode, n_state, state_size);
|
2014-06-11 10:56:43 -04:00
|
|
|
|
|
|
|
// allocate state for locals and stack
|
2016-08-27 09:21:00 -04:00
|
|
|
mp_code_state_t *code_state = NULL;
|
2017-11-26 07:37:19 -05:00
|
|
|
#if MICROPY_ENABLE_PYSTACK
|
|
|
|
code_state = mp_pystack_alloc(sizeof(mp_code_state_t) + state_size);
|
|
|
|
#else
|
2014-06-11 10:56:43 -04:00
|
|
|
if (state_size > VM_MAX_STATE_ON_STACK) {
|
2016-08-27 09:21:00 -04:00
|
|
|
code_state = m_new_obj_var_maybe(mp_code_state_t, byte, state_size);
|
2019-01-04 01:09:41 -05:00
|
|
|
#if MICROPY_DEBUG_VM_STACK_OVERFLOW
|
|
|
|
if (code_state != NULL) {
|
|
|
|
memset(code_state->state, 0, state_size);
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-08 17:07:27 -04:00
|
|
|
}
|
|
|
|
if (code_state == NULL) {
|
2016-08-27 09:21:00 -04:00
|
|
|
code_state = alloca(sizeof(mp_code_state_t) + state_size);
|
2019-01-04 01:09:41 -05:00
|
|
|
#if MICROPY_DEBUG_VM_STACK_OVERFLOW
|
|
|
|
memset(code_state->state, 0, state_size);
|
|
|
|
#endif
|
2015-06-08 17:07:27 -04:00
|
|
|
state_size = 0; // indicate that we allocated using alloca
|
2014-06-11 10:56:43 -04:00
|
|
|
}
|
2017-11-26 07:37:19 -05:00
|
|
|
#endif
|
2014-06-11 10:56:43 -04:00
|
|
|
|
2017-12-09 05:45:28 -05:00
|
|
|
INIT_CODESTATE(code_state, self, n_args, n_kw, args);
|
2014-06-07 19:37:40 -04:00
|
|
|
|
|
|
|
// execute the byte code with the correct globals context
|
|
|
|
mp_globals_set(self->globals);
|
2014-06-07 09:16:08 -04:00
|
|
|
mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
|
2015-02-15 04:12:58 -05:00
|
|
|
mp_globals_set(code_state->old_globals);
|
2014-06-07 09:16:08 -04:00
|
|
|
|
2019-01-04 01:09:41 -05:00
|
|
|
#if MICROPY_DEBUG_VM_STACK_OVERFLOW
|
2014-06-07 09:16:08 -04:00
|
|
|
if (vm_return_kind == MP_VM_RETURN_NORMAL) {
|
|
|
|
if (code_state->sp < code_state->state) {
|
2019-01-04 01:09:41 -05:00
|
|
|
mp_printf(MICROPY_DEBUG_PRINTER, "VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
|
2014-06-07 09:16:08 -04:00
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 01:09:41 -05:00
|
|
|
const byte *bytecode_ptr = mp_decode_uint_skip(mp_decode_uint_skip(self->bytecode));
|
|
|
|
size_t n_pos_args = bytecode_ptr[1];
|
|
|
|
size_t n_kwonly_args = bytecode_ptr[2];
|
|
|
|
// We can't check the case when an exception is returned in state[0]
|
2014-06-07 09:16:08 -04:00
|
|
|
// and there are no arguments, because in this case our detection slot may have
|
|
|
|
// been overwritten by the returned exception (which is allowed).
|
2019-01-04 01:09:41 -05:00
|
|
|
if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && n_pos_args + n_kwonly_args == 0)) {
|
2014-06-07 09:16:08 -04:00
|
|
|
// Just check to see that we have at least 1 null object left in the state.
|
|
|
|
bool overflow = true;
|
2019-01-04 01:09:41 -05:00
|
|
|
for (size_t i = 0; i < n_state - n_pos_args - n_kwonly_args; ++i) {
|
2014-06-07 09:16:08 -04:00
|
|
|
if (code_state->state[i] == MP_OBJ_NULL) {
|
|
|
|
overflow = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (overflow) {
|
2019-01-04 01:09:41 -05:00
|
|
|
mp_printf(MICROPY_DEBUG_PRINTER, "VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
|
2014-06-07 09:16:08 -04:00
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2014-06-07 09:16:08 -04:00
|
|
|
|
2014-06-07 19:37:40 -04:00
|
|
|
mp_obj_t result;
|
2016-09-27 09:08:10 -04:00
|
|
|
if (vm_return_kind == MP_VM_RETURN_NORMAL) {
|
|
|
|
// return value is in *sp
|
|
|
|
result = *code_state->sp;
|
|
|
|
} else {
|
|
|
|
// must be an exception because normal functions can't yield
|
|
|
|
assert(vm_return_kind == MP_VM_RETURN_EXCEPTION);
|
2019-01-04 01:22:40 -05:00
|
|
|
// returned exception is in state[0]
|
2018-09-29 09:25:08 -04:00
|
|
|
result = code_state->state[0];
|
2014-06-07 09:16:08 -04:00
|
|
|
}
|
|
|
|
|
2017-11-26 07:37:19 -05:00
|
|
|
#if MICROPY_ENABLE_PYSTACK
|
|
|
|
mp_pystack_free(code_state);
|
|
|
|
#else
|
2014-06-07 09:16:08 -04:00
|
|
|
// free the state if it was allocated on the heap
|
2015-06-08 17:07:27 -04:00
|
|
|
if (state_size != 0) {
|
2016-08-27 09:21:00 -04:00
|
|
|
m_del_var(mp_code_state_t, byte, state_size, code_state);
|
2014-06-07 09:16:08 -04:00
|
|
|
}
|
2017-11-26 07:37:19 -05:00
|
|
|
#endif
|
2014-06-07 09:16:08 -04:00
|
|
|
|
2014-02-15 17:55:00 -05:00
|
|
|
if (vm_return_kind == MP_VM_RETURN_NORMAL) {
|
|
|
|
return result;
|
|
|
|
} else { // MP_VM_RETURN_EXCEPTION
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(result);
|
2014-02-15 17:55:00 -05:00
|
|
|
}
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
|
2015-02-14 12:44:31 -05:00
|
|
|
#if MICROPY_PY_FUNCTION_ATTRS
|
2018-07-10 02:21:34 -04:00
|
|
|
void mp_obj_fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
2015-04-01 10:10:50 -04:00
|
|
|
if (dest[0] != MP_OBJ_NULL) {
|
|
|
|
// not load attribute
|
|
|
|
return;
|
|
|
|
}
|
2015-04-09 11:29:54 -04:00
|
|
|
if (attr == MP_QSTR___name__) {
|
2015-02-14 12:44:31 -05:00
|
|
|
dest[0] = MP_OBJ_NEW_QSTR(mp_obj_fun_get_name(self_in));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-29 09:43:38 -04:00
|
|
|
const mp_obj_type_t mp_type_fun_bc = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
2014-02-15 06:34:50 -05:00
|
|
|
.name = MP_QSTR_function,
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_CPYTHON_COMPAT
|
2014-04-30 18:32:58 -04:00
|
|
|
.print = fun_bc_print,
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2014-01-18 09:10:48 -05:00
|
|
|
.call = fun_bc_call,
|
2015-05-11 08:25:19 -04:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_PY_FUNCTION_ATTRS
|
2018-07-10 02:21:34 -04:00
|
|
|
.attr = mp_obj_fun_bc_attr,
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2013-12-21 13:17:45 -05:00
|
|
|
};
|
|
|
|
|
2015-10-22 20:23:11 -04:00
|
|
|
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) {
|
2017-02-16 00:34:53 -05:00
|
|
|
size_t n_def_args = 0;
|
|
|
|
size_t n_extra_args = 0;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_tuple_t *def_args = MP_OBJ_TO_PTR(def_args_in);
|
|
|
|
if (def_args_in != MP_OBJ_NULL) {
|
|
|
|
assert(MP_OBJ_IS_TYPE(def_args_in, &mp_type_tuple));
|
2014-02-01 08:05:04 -05:00
|
|
|
n_def_args = def_args->len;
|
2014-02-15 19:01:29 -05:00
|
|
|
n_extra_args = def_args->len;
|
|
|
|
}
|
2014-06-07 17:01:00 -04:00
|
|
|
if (def_kw_args != MP_OBJ_NULL) {
|
|
|
|
n_extra_args += 1;
|
|
|
|
}
|
2014-02-15 19:01:29 -05:00
|
|
|
mp_obj_fun_bc_t *o = m_new_obj_var(mp_obj_fun_bc_t, mp_obj_t, n_extra_args);
|
2014-03-29 09:43:38 -04:00
|
|
|
o->base.type = &mp_type_fun_bc;
|
2014-03-30 08:35:08 -04:00
|
|
|
o->globals = mp_globals_get();
|
2014-01-03 09:03:48 -05:00
|
|
|
o->bytecode = code;
|
2015-10-22 20:23:11 -04:00
|
|
|
o->const_table = const_table;
|
2015-11-27 12:01:44 -05:00
|
|
|
if (def_args != NULL) {
|
2014-06-07 19:37:40 -04:00
|
|
|
memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
|
2014-06-07 17:01:00 -04:00
|
|
|
}
|
|
|
|
if (def_kw_args != MP_OBJ_NULL) {
|
2014-06-07 19:37:40 -04:00
|
|
|
o->extra_args[n_def_args] = def_kw_args;
|
2014-04-27 10:50:52 -04:00
|
|
|
}
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
|
2014-08-24 11:28:17 -04:00
|
|
|
/******************************************************************************/
|
|
|
|
/* native functions */
|
|
|
|
|
|
|
|
#if MICROPY_EMIT_NATIVE
|
|
|
|
|
2016-01-03 04:59:18 -05:00
|
|
|
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) {
|
2015-04-06 17:38:53 -04:00
|
|
|
MP_STACK_CHECK();
|
|
|
|
mp_obj_fun_bc_t *self = self_in;
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void *)self->bytecode);
|
2015-04-06 17:38:53 -04:00
|
|
|
return fun(self_in, n_args, n_kw, args);
|
2014-08-24 11:28:17 -04:00
|
|
|
}
|
|
|
|
|
2021-04-20 21:04:23 -04:00
|
|
|
const mp_obj_type_t mp_type_fun_native = {
|
2014-08-24 11:28:17 -04:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_function,
|
|
|
|
.call = fun_native_call,
|
2015-05-11 08:25:19 -04:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2014-08-24 11:28:17 -04:00
|
|
|
};
|
|
|
|
|
2015-10-22 20:23:11 -04:00
|
|
|
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) {
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_obj_fun_bc_t *o = mp_obj_new_fun_bc(def_args_in, def_kw_args, (const byte *)fun_data, const_table);
|
2014-08-24 11:28:17 -04:00
|
|
|
o->base.type = &mp_type_fun_native;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MICROPY_EMIT_NATIVE
|
|
|
|
|
2014-08-15 11:45:41 -04:00
|
|
|
/******************************************************************************/
|
2013-12-21 13:17:45 -05:00
|
|
|
/* inline assembler functions */
|
|
|
|
|
2016-12-08 21:17:49 -05:00
|
|
|
#if MICROPY_EMIT_INLINE_ASM
|
2014-08-15 11:45:41 -04:00
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
typedef struct _mp_obj_fun_asm_t {
|
|
|
|
mp_obj_base_t base;
|
2017-02-16 00:34:53 -05:00
|
|
|
size_t n_args;
|
2014-08-24 11:28:17 -04:00
|
|
|
void *fun_data; // GC must be able to trace this pointer
|
2016-01-15 10:20:43 -05:00
|
|
|
mp_uint_t type_sig;
|
2013-12-21 13:17:45 -05:00
|
|
|
} mp_obj_fun_asm_t;
|
|
|
|
|
2015-01-12 17:45:35 -05:00
|
|
|
typedef mp_uint_t (*inline_asm_fun_0_t)(void);
|
2014-07-03 08:25:24 -04:00
|
|
|
typedef mp_uint_t (*inline_asm_fun_1_t)(mp_uint_t);
|
|
|
|
typedef mp_uint_t (*inline_asm_fun_2_t)(mp_uint_t, mp_uint_t);
|
|
|
|
typedef mp_uint_t (*inline_asm_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
|
2016-03-16 04:22:26 -04:00
|
|
|
typedef mp_uint_t (*inline_asm_fun_4_t)(mp_uint_t, mp_uint_t, mp_uint_t, mp_uint_t);
|
2013-12-21 13:17:45 -05:00
|
|
|
|
2017-06-30 03:22:17 -04:00
|
|
|
// convert a MicroPython object to a sensible value for inline asm
|
2014-07-03 08:25:24 -04:00
|
|
|
STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
|
2013-12-21 13:17:45 -05:00
|
|
|
// TODO for byte_array, pass pointer to the array
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(obj)) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(obj);
|
|
|
|
} else if (obj == mp_const_none) {
|
|
|
|
return 0;
|
|
|
|
} else if (obj == mp_const_false) {
|
|
|
|
return 0;
|
|
|
|
} else if (obj == mp_const_true) {
|
|
|
|
return 1;
|
2015-02-13 05:43:05 -05:00
|
|
|
} else if (MP_OBJ_IS_TYPE(obj, &mp_type_int)) {
|
|
|
|
return mp_obj_int_get_truncated(obj);
|
2014-01-22 09:35:10 -05:00
|
|
|
} else if (MP_OBJ_IS_STR(obj)) {
|
2013-12-21 13:17:45 -05:00
|
|
|
// pointer to the string (it's probably constant though!)
|
2017-03-25 04:48:18 -04:00
|
|
|
size_t l;
|
2014-07-03 08:25:24 -04:00
|
|
|
return (mp_uint_t)mp_obj_str_get_data(obj, &l);
|
2014-04-12 19:30:32 -04:00
|
|
|
} else {
|
|
|
|
mp_obj_type_t *type = mp_obj_get_type(obj);
|
|
|
|
if (0) {
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-04-12 19:30:32 -04:00
|
|
|
} else if (type == &mp_type_float) {
|
|
|
|
// convert float to int (could also pass in float registers)
|
2014-07-03 08:25:24 -04:00
|
|
|
return (mp_int_t)mp_obj_float_get(obj);
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2017-03-04 06:29:20 -05:00
|
|
|
} else if (type == &mp_type_tuple || type == &mp_type_list) {
|
2014-04-12 19:30:32 -04:00
|
|
|
// pointer to start of tuple (could pass length, but then could use len(x) for that)
|
2017-03-25 04:35:08 -04:00
|
|
|
size_t len;
|
2014-04-12 19:30:32 -04:00
|
|
|
mp_obj_t *items;
|
2017-03-04 06:29:20 -05:00
|
|
|
mp_obj_get_array(obj, &len, &items);
|
2014-07-03 08:25:24 -04:00
|
|
|
return (mp_uint_t)items;
|
2014-04-12 19:30:32 -04:00
|
|
|
} else {
|
2014-04-18 17:29:21 -04:00
|
|
|
mp_buffer_info_t bufinfo;
|
2014-04-18 17:59:24 -04:00
|
|
|
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
|
2014-04-13 07:08:52 -04:00
|
|
|
// supports the buffer protocol, return a pointer to the data
|
2014-07-03 08:25:24 -04:00
|
|
|
return (mp_uint_t)bufinfo.buf;
|
2014-04-13 07:08:52 -04:00
|
|
|
} else {
|
|
|
|
// just pass along a pointer to the object
|
2014-07-03 08:25:24 -04:00
|
|
|
return (mp_uint_t)obj;
|
2014-04-13 07:08:52 -04:00
|
|
|
}
|
2014-04-12 19:30:32 -04:00
|
|
|
}
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-03 04:59:18 -05:00
|
|
|
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) {
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_fun_asm_t *self = self_in;
|
|
|
|
|
2019-11-05 20:49:47 -05:00
|
|
|
mp_arg_check_num_kw_array(n_args, n_kw, self->n_args, self->n_args, false);
|
2013-12-21 13:17:45 -05:00
|
|
|
|
2014-08-24 11:28:17 -04:00
|
|
|
void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_uint_t ret;
|
2013-12-21 13:17:45 -05:00
|
|
|
if (n_args == 0) {
|
2014-08-24 11:28:17 -04:00
|
|
|
ret = ((inline_asm_fun_0_t)fun)();
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (n_args == 1) {
|
2014-08-24 11:28:17 -04:00
|
|
|
ret = ((inline_asm_fun_1_t)fun)(convert_obj_for_inline_asm(args[0]));
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (n_args == 2) {
|
2014-08-24 11:28:17 -04:00
|
|
|
ret = ((inline_asm_fun_2_t)fun)(convert_obj_for_inline_asm(args[0]), convert_obj_for_inline_asm(args[1]));
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (n_args == 3) {
|
2014-08-24 11:28:17 -04:00
|
|
|
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]));
|
2013-12-21 13:17:45 -05:00
|
|
|
} else {
|
2016-03-16 04:22:26 -04:00
|
|
|
// compiler allows at most 4 arguments
|
|
|
|
assert(n_args == 4);
|
|
|
|
ret = ((inline_asm_fun_4_t)fun)(
|
|
|
|
convert_obj_for_inline_asm(args[0]),
|
|
|
|
convert_obj_for_inline_asm(args[1]),
|
|
|
|
convert_obj_for_inline_asm(args[2]),
|
|
|
|
convert_obj_for_inline_asm(args[3])
|
2021-03-15 09:57:36 -04:00
|
|
|
);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
|
2016-01-15 10:20:43 -05:00
|
|
|
return mp_convert_native_to_obj(ret, self->type_sig);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
|
2014-03-29 09:43:38 -04:00
|
|
|
STATIC const mp_obj_type_t mp_type_fun_asm = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
2014-02-15 06:34:50 -05:00
|
|
|
.name = MP_QSTR_function,
|
2014-01-18 09:10:48 -05:00
|
|
|
.call = fun_asm_call,
|
2015-05-11 08:25:19 -04:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2013-12-21 13:17:45 -05:00
|
|
|
};
|
|
|
|
|
2017-02-16 00:34:53 -05:00
|
|
|
mp_obj_t mp_obj_new_fun_asm(size_t n_args, void *fun_data, mp_uint_t type_sig) {
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
|
2014-03-29 09:43:38 -04:00
|
|
|
o->base.type = &mp_type_fun_asm;
|
2013-12-21 13:17:45 -05:00
|
|
|
o->n_args = n_args;
|
2014-08-24 11:28:17 -04:00
|
|
|
o->fun_data = fun_data;
|
2016-01-15 10:20:43 -05:00
|
|
|
o->type_sig = type_sig;
|
2013-12-21 13:17:45 -05:00
|
|
|
return o;
|
|
|
|
}
|
2014-08-15 11:45:41 -04:00
|
|
|
|
2016-12-08 21:17:49 -05:00
|
|
|
#endif // MICROPY_EMIT_INLINE_ASM
|