2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
2014-05-13 06:44:45 +01:00
|
|
|
* Copyright (c) 2014 Paul Sokolovsky
|
2014-05-03 23:27:38 +01: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 18:17:45 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/objtuple.h"
|
|
|
|
#include "py/objfun.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/bc.h"
|
|
|
|
#include "py/stackctrl.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2017-07-24 17:55:14 +01:00
|
|
|
#if MICROPY_DEBUG_VERBOSE // print debugging info
|
2014-02-16 16:30:49 +00:00
|
|
|
#define DEBUG_PRINT (1)
|
|
|
|
#else // don't print debugging info
|
2014-11-05 21:16:41 +00:00
|
|
|
#define DEBUG_PRINT (0)
|
2014-02-26 22:40:35 +00:00
|
|
|
#define DEBUG_printf(...) (void)0
|
2014-02-16 16:30:49 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-11 21:07:15 +00: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 11:50:06 +01:00
|
|
|
|
2014-08-24 16:28:17 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
/* builtin functions */
|
|
|
|
|
2016-10-18 01:49:27 +01: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;
|
2019-01-30 07:49:52 +00:00
|
|
|
assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_0));
|
2016-10-18 01:49:27 +01:00
|
|
|
mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 0, false);
|
|
|
|
return self->fun._0();
|
|
|
|
}
|
|
|
|
|
|
|
|
const mp_obj_type_t mp_type_fun_builtin_0 = {
|
|
|
|
{ &mp_type_type },
|
2020-06-28 00:39:20 +01:00
|
|
|
.flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
|
2016-10-18 01:49:27 +01:00
|
|
|
.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) {
|
2019-01-30 07:49:52 +00:00
|
|
|
assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_1));
|
2016-10-18 01:49:27 +01:00
|
|
|
mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
|
|
|
return self->fun._1(args[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const mp_obj_type_t mp_type_fun_builtin_1 = {
|
|
|
|
{ &mp_type_type },
|
2020-06-28 00:39:20 +01:00
|
|
|
.flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
|
2016-10-18 01:49:27 +01:00
|
|
|
.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) {
|
2019-01-30 07:49:52 +00:00
|
|
|
assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_2));
|
2016-10-18 01:49:27 +01:00
|
|
|
mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_arg_check_num(n_args, n_kw, 2, 2, false);
|
|
|
|
return self->fun._2(args[0], args[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const mp_obj_type_t mp_type_fun_builtin_2 = {
|
|
|
|
{ &mp_type_type },
|
2020-06-28 00:39:20 +01:00
|
|
|
.flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
|
2016-10-18 01:49:27 +01:00
|
|
|
.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) {
|
2019-01-30 07:49:52 +00:00
|
|
|
assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_3));
|
2016-10-18 01:49:27 +01:00
|
|
|
mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_arg_check_num(n_args, n_kw, 3, 3, false);
|
|
|
|
return self->fun._3(args[0], args[1], args[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const mp_obj_type_t mp_type_fun_builtin_3 = {
|
|
|
|
{ &mp_type_type },
|
2020-06-28 00:39:20 +01:00
|
|
|
.flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
|
2016-10-18 01:49:27 +01:00
|
|
|
.name = MP_QSTR_function,
|
|
|
|
.call = fun_builtin_3_call,
|
|
|
|
.unary_op = mp_generic_unary_op,
|
|
|
|
};
|
2014-08-24 16:28:17 +01:00
|
|
|
|
2016-10-18 01:49:27 +01: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) {
|
2019-01-30 07:49:52 +00:00
|
|
|
assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_var));
|
2016-10-18 01:49:27 +01:00
|
|
|
mp_obj_fun_builtin_var_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-01-18 14:10:48 +00:00
|
|
|
|
2014-01-13 19:55:18 +00:00
|
|
|
// check number of arguments
|
2018-09-14 04:39:17 +01:00
|
|
|
mp_arg_check_num_sig(n_args, n_kw, self->sig);
|
2014-01-18 14:10:48 +00:00
|
|
|
|
2018-09-14 04:39:17 +01:00
|
|
|
if (self->sig & 1) {
|
2014-01-18 14:10:48 +00:00
|
|
|
// function allows keywords
|
|
|
|
|
2014-02-08 18:53:41 +00: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 14:10:48 +00:00
|
|
|
|
2016-01-03 11:53:44 +00:00
|
|
|
return self->fun.kw(n_args, args, &kw_args);
|
2014-01-18 14:10:48 +00:00
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
} else {
|
2014-01-18 14:10:48 +00:00
|
|
|
// function takes a variable number of arguments, but no keywords
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2016-01-03 11:53:44 +00:00
|
|
|
return self->fun.var(n_args, args);
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-18 01:49:27 +01:00
|
|
|
const mp_obj_type_t mp_type_fun_builtin_var = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
2020-06-28 00:39:20 +01:00
|
|
|
.flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_function,
|
2016-10-18 01:49:27 +01:00
|
|
|
.call = fun_builtin_var_call,
|
2015-05-11 13:25:19 +01:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2013-12-21 18:17:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* byte code functions */
|
|
|
|
|
2015-02-14 17:44:31 +00:00
|
|
|
qstr mp_obj_code_get_name(const byte *code_info) {
|
2019-09-25 06:45:47 +01:00
|
|
|
MP_BC_PRELUDE_SIZE_DECODE(code_info);
|
2015-11-02 17:27:18 +00:00
|
|
|
#if MICROPY_PERSISTENT_CODE
|
|
|
|
return code_info[0] | (code_info[1] << 8);
|
|
|
|
#else
|
2017-06-09 04:31:57 +01:00
|
|
|
return mp_decode_uint_value(code_info);
|
2015-11-02 17:27:18 +00:00
|
|
|
#endif
|
2014-04-30 23:32:58 +01:00
|
|
|
}
|
|
|
|
|
2015-04-06 22:38:53 +01:00
|
|
|
#if MICROPY_EMIT_NATIVE
|
|
|
|
STATIC const mp_obj_type_t mp_type_fun_native;
|
|
|
|
#endif
|
|
|
|
|
2015-02-14 17:44:31 +00:00
|
|
|
qstr mp_obj_fun_get_name(mp_const_obj_t fun_in) {
|
2015-11-27 17:01:44 +00:00
|
|
|
const mp_obj_fun_bc_t *fun = MP_OBJ_TO_PTR(fun_in);
|
2015-04-06 22:38:53 +01:00
|
|
|
#if MICROPY_EMIT_NATIVE
|
2018-10-01 04:07:04 +01:00
|
|
|
if (fun->base.type == &mp_type_fun_native || fun->base.type == &mp_type_native_gen_wrap) {
|
2015-04-06 22:38:53 +01:00
|
|
|
// TODO native functions don't have name stored
|
|
|
|
return MP_QSTR_;
|
|
|
|
}
|
|
|
|
#endif
|
2015-03-18 17:47:47 +00:00
|
|
|
|
|
|
|
const byte *bc = fun->bytecode;
|
2019-09-16 13:12:59 +01:00
|
|
|
MP_BC_PRELUDE_SIG_DECODE(bc);
|
2015-03-18 17:47:47 +00:00
|
|
|
return mp_obj_code_get_name(bc);
|
2014-05-01 20:20:07 +01:00
|
|
|
}
|
|
|
|
|
2014-04-30 23:32:58 +01:00
|
|
|
#if MICROPY_CPYTHON_COMPAT
|
2015-04-09 23:56:15 +01:00
|
|
|
STATIC void fun_bc_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)kind;
|
2015-11-27 17:01:44 +00: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 23:32:58 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-16 16:30:49 +00:00
|
|
|
#if DEBUG_PRINT
|
2017-02-16 05:34:53 +00:00
|
|
|
STATIC void dump_args(const mp_obj_t *a, size_t sz) {
|
2014-02-16 16:30:49 +00:00
|
|
|
DEBUG_printf("%p: ", a);
|
2017-02-16 05:34:53 +00:00
|
|
|
for (size_t i = 0; i < sz; i++) {
|
2014-02-16 16:30:49 +00:00
|
|
|
DEBUG_printf("%p ", a[i]);
|
|
|
|
}
|
|
|
|
DEBUG_printf("\n");
|
|
|
|
}
|
2014-03-31 15:59:25 +01:00
|
|
|
#else
|
|
|
|
#define dump_args(...) (void)0
|
|
|
|
#endif
|
2014-02-16 16:30:49 +00:00
|
|
|
|
2014-06-07 14:16:08 +01: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 22:07:27 +01:00
|
|
|
// than this will try to use the heap, with fallback to stack allocation.
|
2020-03-09 23:09:49 +00:00
|
|
|
#define VM_MAX_STATE_ON_STACK (sizeof(mp_uint_t) * 11)
|
2014-06-07 14:16:08 +01:00
|
|
|
|
2017-12-09 07:19:34 +00:00
|
|
|
#define DECODE_CODESTATE_SIZE(bytecode, n_state_out_var, state_size_out_var) \
|
|
|
|
{ \
|
2019-09-16 13:12:59 +01:00
|
|
|
const uint8_t *ip = bytecode; \
|
|
|
|
size_t n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_args; \
|
|
|
|
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); \
|
|
|
|
\
|
2017-12-09 07:19:34 +00:00
|
|
|
/* state size in bytes */ \
|
2020-06-13 19:05:48 +01:00
|
|
|
state_size_out_var = n_state_out_var * sizeof(mp_obj_t) \
|
|
|
|
+ n_exc_stack * sizeof(mp_exc_stack_t); \
|
2017-12-09 07:19:34 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 06:57:08 +01:00
|
|
|
#define INIT_CODESTATE(code_state, _fun_bc, _n_state, n_args, n_kw, args) \
|
2017-12-09 10:45:28 +00:00
|
|
|
code_state->fun_bc = _fun_bc; \
|
|
|
|
code_state->ip = 0; \
|
2019-09-24 06:57:08 +01:00
|
|
|
code_state->n_state = _n_state; \
|
2017-12-09 10:45:28 +00:00
|
|
|
mp_setup_code_state(code_state, n_args, n_kw, args); \
|
|
|
|
code_state->old_globals = mp_globals_get();
|
|
|
|
|
2015-04-01 23:31:30 +01:00
|
|
|
#if MICROPY_STACKLESS
|
2016-08-27 14:21:00 +01: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 23:14:44 +00:00
|
|
|
MP_STACK_CHECK();
|
2015-12-17 12:32:41 +00:00
|
|
|
mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-03-27 23:14:44 +00:00
|
|
|
|
2017-12-09 07:19:34 +00:00
|
|
|
size_t n_state, state_size;
|
|
|
|
DECODE_CODESTATE_SIZE(self->bytecode, n_state, state_size);
|
2015-03-27 23:14:44 +00:00
|
|
|
|
2016-08-27 14:21:00 +01:00
|
|
|
mp_code_state_t *code_state;
|
2017-11-26 12:37:19 +00:00
|
|
|
#if MICROPY_ENABLE_PYSTACK
|
|
|
|
code_state = mp_pystack_alloc(sizeof(mp_code_state_t) + state_size);
|
|
|
|
#else
|
2017-12-09 08:57:40 +00: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 14:21:00 +01:00
|
|
|
code_state = m_new_obj_var_maybe(mp_code_state_t, byte, state_size);
|
2015-03-27 23:14:44 +00:00
|
|
|
if (!code_state) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-11-26 12:37:19 +00:00
|
|
|
#endif
|
2015-03-27 23:14:44 +00:00
|
|
|
|
2019-09-24 06:57:08 +01:00
|
|
|
INIT_CODESTATE(code_state, self, n_state, n_args, n_kw, args);
|
2015-03-27 23:14:44 +00:00
|
|
|
|
|
|
|
// execute the byte code with the correct globals context
|
|
|
|
mp_globals_set(self->globals);
|
|
|
|
|
|
|
|
return code_state;
|
|
|
|
}
|
2015-04-01 23:31:30 +01:00
|
|
|
#endif
|
2015-03-27 23:14:44 +00:00
|
|
|
|
2016-01-03 09:59:18 +00:00
|
|
|
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) {
|
2014-07-01 00:13:42 +01:00
|
|
|
MP_STACK_CHECK();
|
2014-06-11 15:56:43 +01:00
|
|
|
|
2014-09-23 10:59:05 +01:00
|
|
|
DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
|
2014-06-11 15:56:43 +01: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 05:17:24 +01:00
|
|
|
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-06-11 15:56:43 +01:00
|
|
|
|
2017-12-09 07:19:34 +00:00
|
|
|
size_t n_state, state_size;
|
|
|
|
DECODE_CODESTATE_SIZE(self->bytecode, n_state, state_size);
|
2014-06-11 15:56:43 +01:00
|
|
|
|
|
|
|
// allocate state for locals and stack
|
2016-08-27 14:21:00 +01:00
|
|
|
mp_code_state_t *code_state = NULL;
|
2017-11-26 12:37:19 +00:00
|
|
|
#if MICROPY_ENABLE_PYSTACK
|
|
|
|
code_state = mp_pystack_alloc(sizeof(mp_code_state_t) + state_size);
|
|
|
|
#else
|
2014-06-11 15:56:43 +01:00
|
|
|
if (state_size > VM_MAX_STATE_ON_STACK) {
|
2016-08-27 14:21:00 +01:00
|
|
|
code_state = m_new_obj_var_maybe(mp_code_state_t, byte, state_size);
|
2019-01-04 06:09:41 +00:00
|
|
|
#if MICROPY_DEBUG_VM_STACK_OVERFLOW
|
|
|
|
if (code_state != NULL) {
|
|
|
|
memset(code_state->state, 0, state_size);
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-08 22:07:27 +01:00
|
|
|
}
|
|
|
|
if (code_state == NULL) {
|
2016-08-27 14:21:00 +01:00
|
|
|
code_state = alloca(sizeof(mp_code_state_t) + state_size);
|
2019-01-04 06:09:41 +00:00
|
|
|
#if MICROPY_DEBUG_VM_STACK_OVERFLOW
|
|
|
|
memset(code_state->state, 0, state_size);
|
|
|
|
#endif
|
2015-06-08 22:07:27 +01:00
|
|
|
state_size = 0; // indicate that we allocated using alloca
|
2014-06-11 15:56:43 +01:00
|
|
|
}
|
2017-11-26 12:37:19 +00:00
|
|
|
#endif
|
2014-06-11 15:56:43 +01:00
|
|
|
|
2019-09-24 06:57:08 +01:00
|
|
|
INIT_CODESTATE(code_state, self, n_state, n_args, n_kw, args);
|
2014-06-08 00:37:40 +01:00
|
|
|
|
|
|
|
// execute the byte code with the correct globals context
|
|
|
|
mp_globals_set(self->globals);
|
2014-06-07 14:16:08 +01:00
|
|
|
mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
|
2015-02-15 09:12:58 +00:00
|
|
|
mp_globals_set(code_state->old_globals);
|
2014-06-07 14:16:08 +01:00
|
|
|
|
2019-01-04 06:09:41 +00:00
|
|
|
#if MICROPY_DEBUG_VM_STACK_OVERFLOW
|
2014-06-07 14:16:08 +01:00
|
|
|
if (vm_return_kind == MP_VM_RETURN_NORMAL) {
|
|
|
|
if (code_state->sp < code_state->state) {
|
2019-01-04 06:09:41 +00:00
|
|
|
mp_printf(MICROPY_DEBUG_PRINTER, "VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
|
2014-06-07 14:16:08 +01:00
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 13:12:59 +01:00
|
|
|
const byte *bytecode_ptr = self->bytecode;
|
|
|
|
size_t n_state_unused, n_exc_stack_unused, scope_flags_unused;
|
|
|
|
size_t n_pos_args, n_kwonly_args, n_def_args_unused;
|
|
|
|
MP_BC_PRELUDE_SIG_DECODE_INTO(bytecode_ptr, n_state_unused, n_exc_stack_unused,
|
|
|
|
scope_flags_unused, n_pos_args, n_kwonly_args, n_def_args_unused);
|
2019-01-04 06:09:41 +00:00
|
|
|
// We can't check the case when an exception is returned in state[0]
|
2014-06-07 14:16:08 +01: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 06:09:41 +00:00
|
|
|
if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && n_pos_args + n_kwonly_args == 0)) {
|
2014-06-07 14:16:08 +01:00
|
|
|
// Just check to see that we have at least 1 null object left in the state.
|
|
|
|
bool overflow = true;
|
2019-01-04 06:09:41 +00:00
|
|
|
for (size_t i = 0; i < n_state - n_pos_args - n_kwonly_args; ++i) {
|
2014-06-07 14:16:08 +01:00
|
|
|
if (code_state->state[i] == MP_OBJ_NULL) {
|
|
|
|
overflow = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (overflow) {
|
2019-01-04 06:09:41 +00: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 14:16:08 +01:00
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 06:09:41 +00:00
|
|
|
#endif
|
2014-06-07 14:16:08 +01:00
|
|
|
|
2014-06-08 00:37:40 +01:00
|
|
|
mp_obj_t result;
|
2016-09-27 14:08:10 +01: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 06:22:40 +00:00
|
|
|
// returned exception is in state[0]
|
2018-09-29 14:25:08 +01:00
|
|
|
result = code_state->state[0];
|
2014-06-07 14:16:08 +01:00
|
|
|
}
|
|
|
|
|
2017-11-26 12:37:19 +00:00
|
|
|
#if MICROPY_ENABLE_PYSTACK
|
|
|
|
mp_pystack_free(code_state);
|
|
|
|
#else
|
2014-06-07 14:16:08 +01:00
|
|
|
// free the state if it was allocated on the heap
|
2015-06-08 22:07:27 +01:00
|
|
|
if (state_size != 0) {
|
2016-08-27 14:21:00 +01:00
|
|
|
m_del_var(mp_code_state_t, byte, state_size, code_state);
|
2014-06-07 14:16:08 +01:00
|
|
|
}
|
2017-11-26 12:37:19 +00:00
|
|
|
#endif
|
2014-06-07 14:16:08 +01:00
|
|
|
|
2014-02-15 22:55:00 +00:00
|
|
|
if (vm_return_kind == MP_VM_RETURN_NORMAL) {
|
|
|
|
return result;
|
|
|
|
} else { // MP_VM_RETURN_EXCEPTION
|
2014-04-05 18:32:08 +01:00
|
|
|
nlr_raise(result);
|
2014-02-15 22:55:00 +00:00
|
|
|
}
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
|
2015-02-14 17:44:31 +00:00
|
|
|
#if MICROPY_PY_FUNCTION_ATTRS
|
2018-07-10 07:21:34 +01:00
|
|
|
void mp_obj_fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
2015-04-01 15:10:50 +01:00
|
|
|
if (dest[0] != MP_OBJ_NULL) {
|
|
|
|
// not load attribute
|
|
|
|
return;
|
|
|
|
}
|
2015-04-09 16:29:54 +01:00
|
|
|
if (attr == MP_QSTR___name__) {
|
2015-02-14 17:44:31 +00:00
|
|
|
dest[0] = MP_OBJ_NEW_QSTR(mp_obj_fun_get_name(self_in));
|
|
|
|
}
|
2020-09-25 16:17:11 +01:00
|
|
|
if (attr == MP_QSTR___globals__) {
|
|
|
|
mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
dest[0] = MP_OBJ_FROM_PTR(self->globals);
|
|
|
|
}
|
2015-02-14 17:44:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-29 13:43:38 +00:00
|
|
|
const mp_obj_type_t mp_type_fun_bc = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
2020-06-28 00:39:20 +01:00
|
|
|
.flags = MP_TYPE_FLAG_BINDS_SELF,
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_function,
|
2020-02-27 04:36:53 +00:00
|
|
|
#if MICROPY_CPYTHON_COMPAT
|
2014-04-30 23:32:58 +01:00
|
|
|
.print = fun_bc_print,
|
2020-02-27 04:36:53 +00:00
|
|
|
#endif
|
2014-01-18 14:10:48 +00:00
|
|
|
.call = fun_bc_call,
|
2015-05-11 13:25:19 +01:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2020-02-27 04:36:53 +00:00
|
|
|
#if MICROPY_PY_FUNCTION_ATTRS
|
2018-07-10 07:21:34 +01:00
|
|
|
.attr = mp_obj_fun_bc_attr,
|
2020-02-27 04:36:53 +00:00
|
|
|
#endif
|
2013-12-21 18:17:45 +00:00
|
|
|
};
|
|
|
|
|
2015-10-23 01:23:11 +01: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 05:34:53 +00:00
|
|
|
size_t n_def_args = 0;
|
|
|
|
size_t n_extra_args = 0;
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_tuple_t *def_args = MP_OBJ_TO_PTR(def_args_in);
|
|
|
|
if (def_args_in != MP_OBJ_NULL) {
|
2019-01-30 07:49:52 +00:00
|
|
|
assert(mp_obj_is_type(def_args_in, &mp_type_tuple));
|
2014-02-01 13:05:04 +00:00
|
|
|
n_def_args = def_args->len;
|
2014-02-16 00:01:29 +00:00
|
|
|
n_extra_args = def_args->len;
|
|
|
|
}
|
2014-06-07 22:01:00 +01:00
|
|
|
if (def_kw_args != MP_OBJ_NULL) {
|
|
|
|
n_extra_args += 1;
|
|
|
|
}
|
2014-02-16 00:01:29 +00: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 13:43:38 +00:00
|
|
|
o->base.type = &mp_type_fun_bc;
|
2014-03-30 13:35:08 +01:00
|
|
|
o->globals = mp_globals_get();
|
2014-01-03 14:03:48 +00:00
|
|
|
o->bytecode = code;
|
2015-10-23 01:23:11 +01:00
|
|
|
o->const_table = const_table;
|
2015-11-27 17:01:44 +00:00
|
|
|
if (def_args != NULL) {
|
2014-06-08 00:37:40 +01:00
|
|
|
memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
|
2014-06-07 22:01:00 +01:00
|
|
|
}
|
|
|
|
if (def_kw_args != MP_OBJ_NULL) {
|
2014-06-08 00:37:40 +01:00
|
|
|
o->extra_args[n_def_args] = def_kw_args;
|
2014-04-27 15:50:52 +01:00
|
|
|
}
|
2015-11-27 17:01:44 +00:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
|
2014-08-24 16:28:17 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
/* native functions */
|
|
|
|
|
|
|
|
#if MICROPY_EMIT_NATIVE
|
|
|
|
|
2016-01-03 09:59:18 +00: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 22:38:53 +01:00
|
|
|
MP_STACK_CHECK();
|
|
|
|
mp_obj_fun_bc_t *self = self_in;
|
2020-02-27 04:36:53 +00:00
|
|
|
mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void *)self->bytecode);
|
2015-04-06 22:38:53 +01:00
|
|
|
return fun(self_in, n_args, n_kw, args);
|
2014-08-24 16:28:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t mp_type_fun_native = {
|
|
|
|
{ &mp_type_type },
|
2020-06-28 00:39:20 +01:00
|
|
|
.flags = MP_TYPE_FLAG_BINDS_SELF,
|
2014-08-24 16:28:17 +01:00
|
|
|
.name = MP_QSTR_function,
|
|
|
|
.call = fun_native_call,
|
2015-05-11 13:25:19 +01:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2014-08-24 16:28:17 +01:00
|
|
|
};
|
|
|
|
|
2015-10-23 01:23:11 +01: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) {
|
2020-02-27 04:36:53 +00: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 16:28:17 +01:00
|
|
|
o->base.type = &mp_type_fun_native;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MICROPY_EMIT_NATIVE
|
2014-08-15 16:45:41 +01:00
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
/* inline assembler functions */
|
|
|
|
|
2016-12-09 02:17:49 +00:00
|
|
|
#if MICROPY_EMIT_INLINE_ASM
|
2014-08-15 16:45:41 +01:00
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
typedef struct _mp_obj_fun_asm_t {
|
|
|
|
mp_obj_base_t base;
|
2017-02-16 05:34:53 +00:00
|
|
|
size_t n_args;
|
2019-02-20 02:08:42 +00:00
|
|
|
const void *fun_data; // GC must be able to trace this pointer
|
2016-01-15 15:20:43 +00:00
|
|
|
mp_uint_t type_sig;
|
2013-12-21 18:17:45 +00:00
|
|
|
} mp_obj_fun_asm_t;
|
|
|
|
|
2015-01-12 22:45:35 +00:00
|
|
|
typedef mp_uint_t (*inline_asm_fun_0_t)(void);
|
2014-07-03 13:25:24 +01: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 08:22:26 +00: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 18:17:45 +00:00
|
|
|
|
2017-06-30 08:22:17 +01:00
|
|
|
// convert a MicroPython object to a sensible value for inline asm
|
2014-07-03 13:25:24 +01:00
|
|
|
STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
|
2013-12-21 18:17:45 +00:00
|
|
|
// TODO for byte_array, pass pointer to the array
|
2019-01-30 07:49:52 +00:00
|
|
|
if (mp_obj_is_small_int(obj)) {
|
2013-12-21 18:17:45 +00:00
|
|
|
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;
|
2019-01-30 07:49:52 +00:00
|
|
|
} else if (mp_obj_is_type(obj, &mp_type_int)) {
|
2015-02-13 10:43:05 +00:00
|
|
|
return mp_obj_int_get_truncated(obj);
|
2019-01-30 07:49:52 +00:00
|
|
|
} else if (mp_obj_is_str(obj)) {
|
2013-12-21 18:17:45 +00:00
|
|
|
// pointer to the string (it's probably constant though!)
|
2017-03-25 08:48:18 +00:00
|
|
|
size_t l;
|
2014-07-03 13:25:24 +01:00
|
|
|
return (mp_uint_t)mp_obj_str_get_data(obj, &l);
|
2014-04-13 00:30:32 +01:00
|
|
|
} else {
|
2020-01-09 00:01:14 +00:00
|
|
|
const mp_obj_type_t *type = mp_obj_get_type(obj);
|
2020-02-19 13:17:13 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2019-05-05 04:29:43 +01:00
|
|
|
if (type == &mp_type_float) {
|
2014-04-13 00:30:32 +01:00
|
|
|
// convert float to int (could also pass in float registers)
|
2014-07-03 13:25:24 +01:00
|
|
|
return (mp_int_t)mp_obj_float_get(obj);
|
2020-02-19 13:17:13 +00:00
|
|
|
}
|
|
|
|
#endif
|
2019-05-05 04:29:43 +01:00
|
|
|
if (type == &mp_type_tuple || type == &mp_type_list) {
|
2014-04-13 00:30:32 +01:00
|
|
|
// pointer to start of tuple (could pass length, but then could use len(x) for that)
|
2017-03-25 08:35:08 +00:00
|
|
|
size_t len;
|
2014-04-13 00:30:32 +01:00
|
|
|
mp_obj_t *items;
|
2017-03-04 11:29:20 +00:00
|
|
|
mp_obj_get_array(obj, &len, &items);
|
2014-07-03 13:25:24 +01:00
|
|
|
return (mp_uint_t)items;
|
2014-04-13 00:30:32 +01:00
|
|
|
} else {
|
2014-04-18 22:29:21 +01:00
|
|
|
mp_buffer_info_t bufinfo;
|
2019-08-05 07:32:10 +01:00
|
|
|
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_READ)) {
|
2014-04-13 12:08:52 +01:00
|
|
|
// supports the buffer protocol, return a pointer to the data
|
2014-07-03 13:25:24 +01:00
|
|
|
return (mp_uint_t)bufinfo.buf;
|
2014-04-13 12:08:52 +01:00
|
|
|
} else {
|
|
|
|
// just pass along a pointer to the object
|
2014-07-03 13:25:24 +01:00
|
|
|
return (mp_uint_t)obj;
|
2014-04-13 12:08:52 +01:00
|
|
|
}
|
2014-04-13 00:30:32 +01:00
|
|
|
}
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-03 09:59:18 +00: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 18:17:45 +00:00
|
|
|
mp_obj_fun_asm_t *self = self_in;
|
|
|
|
|
2014-05-10 13:40:46 +01:00
|
|
|
mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2019-02-20 02:08:42 +00:00
|
|
|
const void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
|
2014-08-24 16:28:17 +01:00
|
|
|
|
2014-07-03 13:25:24 +01:00
|
|
|
mp_uint_t ret;
|
2013-12-21 18:17:45 +00:00
|
|
|
if (n_args == 0) {
|
2014-08-24 16:28:17 +01:00
|
|
|
ret = ((inline_asm_fun_0_t)fun)();
|
2013-12-21 18:17:45 +00:00
|
|
|
} else if (n_args == 1) {
|
2014-08-24 16:28:17 +01:00
|
|
|
ret = ((inline_asm_fun_1_t)fun)(convert_obj_for_inline_asm(args[0]));
|
2013-12-21 18:17:45 +00:00
|
|
|
} else if (n_args == 2) {
|
2014-08-24 16:28:17 +01: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 18:17:45 +00:00
|
|
|
} else if (n_args == 3) {
|
2014-08-24 16:28:17 +01: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 18:17:45 +00:00
|
|
|
} else {
|
2016-03-16 08:22:26 +00: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])
|
2020-02-27 04:36:53 +00:00
|
|
|
);
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 23:22:09 +00:00
|
|
|
return mp_native_to_obj(ret, self->type_sig);
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 13:43:38 +00:00
|
|
|
STATIC const mp_obj_type_t mp_type_fun_asm = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
2020-06-28 00:39:20 +01:00
|
|
|
.flags = MP_TYPE_FLAG_BINDS_SELF,
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_function,
|
2014-01-18 14:10:48 +00:00
|
|
|
.call = fun_asm_call,
|
2015-05-11 13:25:19 +01:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2013-12-21 18:17:45 +00:00
|
|
|
};
|
|
|
|
|
2019-02-20 02:08:42 +00:00
|
|
|
mp_obj_t mp_obj_new_fun_asm(size_t n_args, const void *fun_data, mp_uint_t type_sig) {
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
|
2014-03-29 13:43:38 +00:00
|
|
|
o->base.type = &mp_type_fun_asm;
|
2013-12-21 18:17:45 +00:00
|
|
|
o->n_args = n_args;
|
2014-08-24 16:28:17 +01:00
|
|
|
o->fun_data = fun_data;
|
2016-01-15 15:20:43 +00:00
|
|
|
o->type_sig = type_sig;
|
2013-12-21 18:17:45 +00:00
|
|
|
return o;
|
|
|
|
}
|
2014-08-15 16:45:41 +01:00
|
|
|
|
2016-12-09 02:17:49 +00:00
|
|
|
#endif // MICROPY_EMIT_INLINE_ASM
|