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/nlr.h"
|
|
|
|
#include "py/objtuple.h"
|
|
|
|
#include "py/objfun.h"
|
|
|
|
#include "py/runtime0.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/bc.h"
|
|
|
|
#include "py/stackctrl.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2014-02-16 16:30:49 +00:00
|
|
|
#if 0 // print debugging info
|
|
|
|
#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;
|
|
|
|
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);
|
|
|
|
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 },
|
|
|
|
.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);
|
|
|
|
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 },
|
|
|
|
.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);
|
|
|
|
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 },
|
|
|
|
.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);
|
|
|
|
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 },
|
|
|
|
.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) {
|
|
|
|
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 14:10:48 +00:00
|
|
|
|
2014-01-13 19:55:18 +00:00
|
|
|
// check number of arguments
|
2014-04-20 00:13:22 +01:00
|
|
|
mp_arg_check_num(n_args, n_kw, self->n_args_min, self->n_args_max, self->is_kw);
|
2014-01-18 14:10:48 +00:00
|
|
|
|
2014-01-07 17:29:16 +00:00
|
|
|
if (self->is_kw) {
|
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 },
|
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) {
|
2017-06-09 04:31:57 +01:00
|
|
|
code_info = mp_decode_uint_skip(code_info); // skip code_info_size entry
|
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
|
|
|
|
if (fun->base.type == &mp_type_fun_native) {
|
|
|
|
// 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;
|
2017-06-09 04:31:57 +01:00
|
|
|
bc = mp_decode_uint_skip(bc); // skip n_state
|
|
|
|
bc = mp_decode_uint_skip(bc); // skip n_exc_stack
|
2015-10-22 23:45:37 +01:00
|
|
|
bc++; // skip scope_params
|
2015-10-23 01:23:11 +01:00
|
|
|
bc++; // skip n_pos_args
|
|
|
|
bc++; // skip n_kwonly_args
|
2015-10-22 23:45:37 +01:00
|
|
|
bc++; // skip n_def_pos_args
|
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.
|
2015-02-15 09:12:58 +00:00
|
|
|
#define VM_MAX_STATE_ON_STACK (11 * sizeof(mp_uint_t))
|
2014-06-07 14:16:08 +01:00
|
|
|
|
|
|
|
// Set this to enable a simple stack overflow check.
|
|
|
|
#define VM_DETECT_STACK_OVERFLOW (0)
|
|
|
|
|
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
|
|
|
|
|
|
|
// bytecode prelude: state size and exception stack size
|
2017-06-09 04:31:57 +01:00
|
|
|
size_t n_state = mp_decode_uint_value(self->bytecode);
|
|
|
|
size_t n_exc_stack = mp_decode_uint_value(mp_decode_uint_skip(self->bytecode));
|
2015-03-27 23:14:44 +00:00
|
|
|
|
|
|
|
// allocate state for locals and stack
|
2015-12-17 12:33:42 +00:00
|
|
|
size_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
|
2016-08-27 14:21:00 +01:00
|
|
|
mp_code_state_t *code_state;
|
|
|
|
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;
|
|
|
|
}
|
2015-03-27 23:14:44 +00:00
|
|
|
|
2017-03-17 03:54:53 +00:00
|
|
|
code_state->fun_bc = self;
|
|
|
|
code_state->ip = 0;
|
|
|
|
mp_setup_code_state(code_state, n_args, n_kw, args);
|
2015-03-27 23:14:44 +00:00
|
|
|
|
|
|
|
// execute the byte code with the correct globals context
|
|
|
|
code_state->old_globals = mp_globals_get();
|
|
|
|
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);
|
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
|
|
|
DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
|
|
|
|
|
2014-10-25 15:07:02 +01:00
|
|
|
// bytecode prelude: state size and exception stack size
|
2017-06-09 04:31:57 +01:00
|
|
|
size_t n_state = mp_decode_uint_value(self->bytecode);
|
|
|
|
size_t n_exc_stack = mp_decode_uint_value(mp_decode_uint_skip(self->bytecode));
|
2014-06-11 15:56:43 +01:00
|
|
|
|
|
|
|
#if VM_DETECT_STACK_OVERFLOW
|
|
|
|
n_state += 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// allocate state for locals and stack
|
2017-02-16 05:34:53 +00:00
|
|
|
size_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
|
2016-08-27 14:21:00 +01:00
|
|
|
mp_code_state_t *code_state = NULL;
|
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);
|
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);
|
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-03-17 03:54:53 +00:00
|
|
|
code_state->fun_bc = self;
|
|
|
|
code_state->ip = 0;
|
|
|
|
mp_setup_code_state(code_state, n_args, n_kw, args);
|
2014-06-08 00:37:40 +01:00
|
|
|
|
|
|
|
// execute the byte code with the correct globals context
|
2015-02-15 09:12:58 +00:00
|
|
|
code_state->old_globals = mp_globals_get();
|
2014-06-08 00:37:40 +01:00
|
|
|
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
|
|
|
|
|
|
|
#if VM_DETECT_STACK_OVERFLOW
|
|
|
|
if (vm_return_kind == MP_VM_RETURN_NORMAL) {
|
|
|
|
if (code_state->sp < code_state->state) {
|
|
|
|
printf("VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We can't check the case when an exception is returned in state[n_state - 1]
|
|
|
|
// and there are no arguments, because in this case our detection slot may have
|
|
|
|
// been overwritten by the returned exception (which is allowed).
|
2014-06-08 00:37:40 +01:00
|
|
|
if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && self->n_pos_args + self->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;
|
2017-02-16 05:34:53 +00:00
|
|
|
for (size_t i = 0; i < n_state - self->n_pos_args - self->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) {
|
|
|
|
printf("VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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);
|
|
|
|
// return value is in fastn[0]==state[n_state - 1]
|
|
|
|
result = code_state->state[n_state - 1];
|
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
|
|
|
}
|
|
|
|
|
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
|
2015-04-01 15:10:50 +01:00
|
|
|
STATIC void fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#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 },
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_function,
|
2014-04-30 23:32:58 +01:00
|
|
|
#if MICROPY_CPYTHON_COMPAT
|
|
|
|
.print = fun_bc_print,
|
|
|
|
#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,
|
2015-02-14 17:44:31 +00:00
|
|
|
#if MICROPY_PY_FUNCTION_ATTRS
|
2015-04-01 15:10:50 +01:00
|
|
|
.attr = fun_bc_attr,
|
2015-02-14 17:44:31 +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) {
|
|
|
|
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;
|
|
|
|
mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void*)self->bytecode);
|
|
|
|
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 },
|
|
|
|
.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) {
|
|
|
|
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
|
|
|
/******************************************************************************/
|
|
|
|
/* viper functions */
|
|
|
|
|
|
|
|
#if MICROPY_EMIT_NATIVE
|
|
|
|
|
|
|
|
typedef struct _mp_obj_fun_viper_t {
|
|
|
|
mp_obj_base_t base;
|
2017-02-16 05:34:53 +00:00
|
|
|
size_t n_args;
|
2014-08-24 16:28:17 +01:00
|
|
|
void *fun_data; // GC must be able to trace this pointer
|
2014-08-15 16:45:41 +01:00
|
|
|
mp_uint_t type_sig;
|
|
|
|
} mp_obj_fun_viper_t;
|
|
|
|
|
2015-01-12 22:45:35 +00:00
|
|
|
typedef mp_uint_t (*viper_fun_0_t)(void);
|
2014-08-15 16:45:41 +01:00
|
|
|
typedef mp_uint_t (*viper_fun_1_t)(mp_uint_t);
|
|
|
|
typedef mp_uint_t (*viper_fun_2_t)(mp_uint_t, mp_uint_t);
|
|
|
|
typedef mp_uint_t (*viper_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
|
2015-07-23 14:11:29 +01:00
|
|
|
typedef mp_uint_t (*viper_fun_4_t)(mp_uint_t, mp_uint_t, mp_uint_t, mp_uint_t);
|
2014-08-15 16:45:41 +01:00
|
|
|
|
2016-01-03 09:59:18 +00:00
|
|
|
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) {
|
2014-08-15 16:45:41 +01:00
|
|
|
mp_obj_fun_viper_t *self = self_in;
|
|
|
|
|
|
|
|
mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
|
|
|
|
|
2014-08-24 16:28:17 +01:00
|
|
|
void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
|
|
|
|
|
2014-08-15 16:45:41 +01:00
|
|
|
mp_uint_t ret;
|
|
|
|
if (n_args == 0) {
|
2014-08-24 16:28:17 +01:00
|
|
|
ret = ((viper_fun_0_t)fun)();
|
2014-08-15 16:45:41 +01:00
|
|
|
} else if (n_args == 1) {
|
2016-02-02 23:16:05 +00:00
|
|
|
ret = ((viper_fun_1_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 4));
|
2014-08-15 16:45:41 +01:00
|
|
|
} else if (n_args == 2) {
|
2016-02-02 23:16:05 +00:00
|
|
|
ret = ((viper_fun_2_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 4), mp_convert_obj_to_native(args[1], self->type_sig >> 8));
|
2014-08-15 16:45:41 +01:00
|
|
|
} else if (n_args == 3) {
|
2016-02-02 23:16:05 +00:00
|
|
|
ret = ((viper_fun_3_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 4), mp_convert_obj_to_native(args[1], self->type_sig >> 8), mp_convert_obj_to_native(args[2], self->type_sig >> 12));
|
2016-09-27 14:05:51 +01:00
|
|
|
} else {
|
|
|
|
// compiler allows at most 4 arguments
|
|
|
|
assert(n_args == 4);
|
2015-07-23 14:11:29 +01:00
|
|
|
ret = ((viper_fun_4_t)fun)(
|
2016-02-02 23:16:05 +00:00
|
|
|
mp_convert_obj_to_native(args[0], self->type_sig >> 4),
|
|
|
|
mp_convert_obj_to_native(args[1], self->type_sig >> 8),
|
|
|
|
mp_convert_obj_to_native(args[2], self->type_sig >> 12),
|
|
|
|
mp_convert_obj_to_native(args[3], self->type_sig >> 16)
|
2015-07-23 14:11:29 +01:00
|
|
|
);
|
2014-08-15 16:45:41 +01:00
|
|
|
}
|
|
|
|
|
2014-08-15 23:47:59 +01:00
|
|
|
return mp_convert_native_to_obj(ret, self->type_sig);
|
2014-08-15 16:45:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t mp_type_fun_viper = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_function,
|
|
|
|
.call = fun_viper_call,
|
2015-05-11 13:25:19 +01:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2014-08-15 16:45:41 +01:00
|
|
|
};
|
|
|
|
|
2017-02-16 05:34:53 +00:00
|
|
|
mp_obj_t mp_obj_new_fun_viper(size_t n_args, void *fun_data, mp_uint_t type_sig) {
|
2014-08-15 16:45:41 +01:00
|
|
|
mp_obj_fun_viper_t *o = m_new_obj(mp_obj_fun_viper_t);
|
|
|
|
o->base.type = &mp_type_fun_viper;
|
|
|
|
o->n_args = n_args;
|
2014-08-24 16:28:17 +01:00
|
|
|
o->fun_data = fun_data;
|
2014-08-15 16:45:41 +01:00
|
|
|
o->type_sig = type_sig;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MICROPY_EMIT_NATIVE
|
|
|
|
|
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;
|
2014-08-24 16:28:17 +01:00
|
|
|
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
|
|
|
|
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 10:43:05 +00:00
|
|
|
} else if (MP_OBJ_IS_TYPE(obj, &mp_type_int)) {
|
|
|
|
return mp_obj_int_get_truncated(obj);
|
2014-01-22 14:35:10 +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 {
|
|
|
|
mp_obj_type_t *type = mp_obj_get_type(obj);
|
|
|
|
if (0) {
|
2014-06-01 13:32:54 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-04-13 00:30:32 +01:00
|
|
|
} else if (type == &mp_type_float) {
|
|
|
|
// 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);
|
2013-12-21 18:17:45 +00:00
|
|
|
#endif
|
2017-03-04 11:29:20 +00:00
|
|
|
} else 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;
|
2014-04-18 22:59:24 +01:00
|
|
|
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
|
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
|
|
|
|
2014-08-24 16:28:17 +01:00
|
|
|
void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
|
|
|
|
|
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])
|
|
|
|
);
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 15:20:43 +00:00
|
|
|
return mp_convert_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 },
|
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
|
|
|
};
|
|
|
|
|
2017-02-16 05:34:53 +00:00
|
|
|
mp_obj_t mp_obj_new_fun_asm(size_t n_args, 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
|