2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-03-16 03:14:26 -04:00
|
|
|
#include <stdbool.h>
|
2013-12-21 13:17:45 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2014-05-02 10:47:01 -04:00
|
|
|
#include "mpconfig.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
2014-01-21 16:40:13 -05:00
|
|
|
#include "qstr.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
#include "obj.h"
|
2014-02-01 08:05:04 -05:00
|
|
|
#include "objtuple.h"
|
2014-04-16 22:44:51 -04:00
|
|
|
#include "objfun.h"
|
2014-02-15 19:01:29 -05:00
|
|
|
#include "runtime0.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
#include "runtime.h"
|
|
|
|
#include "bc.h"
|
2014-06-24 20:03:34 -04:00
|
|
|
#include "stackctrl.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
|
2014-02-16 11:30:49 -05:00
|
|
|
#if 0 // print debugging info
|
|
|
|
#define DEBUG_PRINT (1)
|
|
|
|
#else // don't print debugging info
|
2014-02-26 17:40:35 -05:00
|
|
|
#define DEBUG_printf(...) (void)0
|
2014-02-16 11:30:49 -05:00
|
|
|
#endif
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
/* native functions */
|
|
|
|
|
|
|
|
// mp_obj_fun_native_t defined in obj.h
|
|
|
|
|
2014-04-05 06:50:06 -04:00
|
|
|
STATIC mp_obj_t fun_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
|
|
|
switch (op) {
|
|
|
|
case MP_BINARY_OP_EQUAL:
|
|
|
|
// These objects can be equal only if it's the same underlying structure,
|
|
|
|
// we don't even need to check for 2nd arg type.
|
|
|
|
return MP_BOOL(lhs_in == rhs_in);
|
|
|
|
}
|
2014-05-21 14:42:43 -04:00
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-04-05 06:50:06 -04:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t fun_native_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
|
2014-03-29 09:43:38 -04:00
|
|
|
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_native));
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_fun_native_t *self = self_in;
|
2014-01-18 09:10:48 -05:00
|
|
|
|
2014-01-13 14:55:18 -05:00
|
|
|
// check number of arguments
|
2014-04-19 19:13:22 -04:00
|
|
|
mp_arg_check_num(n_args, n_kw, self->n_args_min, self->n_args_max, self->is_kw);
|
2014-01-18 09:10:48 -05:00
|
|
|
|
2014-01-07 12:29:16 -05:00
|
|
|
if (self->is_kw) {
|
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
|
|
|
|
2014-02-08 13:53:41 -05:00
|
|
|
return ((mp_fun_kw_t)self->fun)(n_args, args, &kw_args);
|
2014-01-18 09:10:48 -05:00
|
|
|
|
2014-01-25 18:58:51 -05:00
|
|
|
} else if (self->n_args_min <= 3 && self->n_args_min == self->n_args_max) {
|
2013-12-21 13:17:45 -05:00
|
|
|
// function requires a fixed number of arguments
|
|
|
|
|
|
|
|
// dispatch function call
|
|
|
|
switch (self->n_args_min) {
|
|
|
|
case 0:
|
|
|
|
return ((mp_fun_0_t)self->fun)();
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
return ((mp_fun_1_t)self->fun)(args[0]);
|
|
|
|
|
|
|
|
case 2:
|
2014-01-18 09:10:48 -05:00
|
|
|
return ((mp_fun_2_t)self->fun)(args[0], args[1]);
|
2013-12-21 13:17:45 -05:00
|
|
|
|
2014-01-03 20:15:01 -05:00
|
|
|
case 3:
|
2014-01-18 09:10:48 -05:00
|
|
|
return ((mp_fun_3_t)self->fun)(args[0], args[1], args[2]);
|
2014-01-03 20:15:01 -05:00
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
} 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
|
|
|
|
2014-01-18 09:10:48 -05:00
|
|
|
return ((mp_fun_var_t)self->fun)(n_args, args);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-29 09:43:38 -04:00
|
|
|
const mp_obj_type_t mp_type_fun_native = {
|
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_native_call,
|
2014-04-05 06:50:06 -04:00
|
|
|
.binary_op = fun_binary_op,
|
2013-12-21 13:17:45 -05:00
|
|
|
};
|
|
|
|
|
2014-01-13 14:50:05 -05:00
|
|
|
// fun must have the correct signature for n_args fixed arguments
|
2014-03-30 08:35:08 -04:00
|
|
|
mp_obj_t mp_make_function_n(int n_args, void *fun) {
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
|
2014-03-29 09:43:38 -04:00
|
|
|
o->base.type = &mp_type_fun_native;
|
2014-01-13 11:42:43 -05:00
|
|
|
o->is_kw = false;
|
2014-01-13 14:50:05 -05:00
|
|
|
o->n_args_min = n_args;
|
|
|
|
o->n_args_max = n_args;
|
2014-01-03 20:15:01 -05:00
|
|
|
o->fun = fun;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
mp_obj_t mp_make_function_var(int n_args_min, mp_fun_var_t fun) {
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
|
2014-03-29 09:43:38 -04:00
|
|
|
o->base.type = &mp_type_fun_native;
|
2014-01-13 11:42:43 -05:00
|
|
|
o->is_kw = false;
|
2013-12-21 13:17:45 -05:00
|
|
|
o->n_args_min = n_args_min;
|
2014-02-26 12:47:05 -05:00
|
|
|
o->n_args_max = MP_OBJ_FUN_ARGS_MAX;
|
2013-12-21 13:17:45 -05:00
|
|
|
o->fun = fun;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
// min and max are inclusive
|
2014-03-30 08:35:08 -04:00
|
|
|
mp_obj_t mp_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) {
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
|
2014-03-29 09:43:38 -04:00
|
|
|
o->base.type = &mp_type_fun_native;
|
2014-01-13 11:42:43 -05:00
|
|
|
o->is_kw = false;
|
2013-12-21 13:17:45 -05:00
|
|
|
o->n_args_min = n_args_min;
|
|
|
|
o->n_args_max = n_args_max;
|
|
|
|
o->fun = fun;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* byte code functions */
|
|
|
|
|
2014-05-01 15:20:07 -04:00
|
|
|
const char *mp_obj_code_get_name(const byte *code_info) {
|
2014-04-30 18:32:58 -04:00
|
|
|
qstr block_name = code_info[8] | (code_info[9] << 8) | (code_info[10] << 16) | (code_info[11] << 24);
|
|
|
|
return qstr_str(block_name);
|
|
|
|
}
|
|
|
|
|
2014-05-17 04:08:33 -04:00
|
|
|
const char *mp_obj_fun_get_name(mp_const_obj_t fun_in) {
|
|
|
|
const mp_obj_fun_bc_t *fun = fun_in;
|
2014-05-03 07:10:34 -04:00
|
|
|
const byte *code_info = fun->bytecode;
|
2014-05-01 15:20:07 -04:00
|
|
|
return mp_obj_code_get_name(code_info);
|
|
|
|
}
|
|
|
|
|
2014-04-30 18:32:58 -04:00
|
|
|
#if MICROPY_CPYTHON_COMPAT
|
|
|
|
STATIC void fun_bc_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
|
|
|
mp_obj_fun_bc_t *o = o_in;
|
|
|
|
print(env, "<function %s at 0x%x>", mp_obj_fun_get_name(o), o);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-16 11:30:49 -05:00
|
|
|
#if DEBUG_PRINT
|
2014-03-31 10:59:25 -04:00
|
|
|
STATIC void dump_args(const mp_obj_t *a, int sz) {
|
2014-02-16 11:30:49 -05:00
|
|
|
DEBUG_printf("%p: ", a);
|
|
|
|
for (int i = 0; i < sz; i++) {
|
|
|
|
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-04-29 22:37:28 -04:00
|
|
|
STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, uint expected, uint given) {
|
2014-04-30 18:35:38 -04:00
|
|
|
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
|
|
|
// Generic message, to be reused for other argument issues
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
|
|
|
"argument num/types mismatch"));
|
|
|
|
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
|
2014-04-29 22:37:28 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
|
|
|
"function takes %d positional arguments but %d were given", expected, given));
|
2014-04-30 18:35:38 -04:00
|
|
|
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
|
|
|
"%s() takes %d positional arguments but %d were given",
|
|
|
|
mp_obj_fun_get_name(f), expected, given));
|
|
|
|
#endif
|
2014-04-29 22:37:28 -04: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
|
|
|
|
// than this will use the heap.
|
|
|
|
#define VM_MAX_STATE_ON_STACK (10 * sizeof(machine_uint_t))
|
|
|
|
|
|
|
|
// Set this to enable a simple stack overflow check.
|
|
|
|
#define VM_DETECT_STACK_OVERFLOW (0)
|
|
|
|
|
2014-06-11 10:56:43 -04:00
|
|
|
// code_state should have ->ip filled in (pointing past code info block),
|
|
|
|
// as well as ->n_state.
|
|
|
|
void mp_setup_code_state(mp_code_state *code_state, mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
|
2014-06-24 20:03:34 -04:00
|
|
|
// This function is pretty complicated. It's main aim is to be efficient in speed and RAM
|
|
|
|
// usage for the common case of positional only args.
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_fun_bc_t *self = self_in;
|
2014-06-11 10:56:43 -04:00
|
|
|
machine_uint_t n_state = code_state->n_state;
|
|
|
|
const byte *ip = code_state->ip;
|
2014-06-07 19:37:40 -04:00
|
|
|
|
|
|
|
code_state->code_info = self->bytecode;
|
|
|
|
code_state->sp = &code_state->state[0] - 1;
|
|
|
|
code_state->exc_sp = (mp_exc_stack_t*)(code_state->state + n_state) - 1;
|
|
|
|
|
|
|
|
// zero out the local stack to begin with
|
|
|
|
memset(code_state->state, 0, n_state * sizeof(*code_state->state));
|
|
|
|
|
2014-02-15 19:17:42 -05:00
|
|
|
const mp_obj_t *kwargs = args + n_args;
|
2014-06-07 19:37:40 -04:00
|
|
|
|
|
|
|
// var_pos_kw_args points to the stack where the var-args tuple, and var-kw dict, should go (if they are needed)
|
|
|
|
mp_obj_t *var_pos_kw_args = &code_state->state[n_state - 1 - self->n_pos_args - self->n_kwonly_args];
|
2014-02-15 19:01:29 -05:00
|
|
|
|
2014-02-15 19:17:42 -05:00
|
|
|
// check positional arguments
|
|
|
|
|
2014-04-27 10:50:52 -04:00
|
|
|
if (n_args > self->n_pos_args) {
|
2014-02-15 19:01:29 -05:00
|
|
|
// given more than enough arguments
|
|
|
|
if (!self->takes_var_args) {
|
2014-04-29 22:37:28 -04:00
|
|
|
fun_pos_args_mismatch(self, self->n_pos_args, n_args);
|
2014-02-15 19:01:29 -05:00
|
|
|
}
|
|
|
|
// put extra arguments in varargs tuple
|
2014-06-07 19:37:40 -04:00
|
|
|
*var_pos_kw_args-- = mp_obj_new_tuple(n_args - self->n_pos_args, args + self->n_pos_args);
|
2014-04-27 10:50:52 -04:00
|
|
|
n_args = self->n_pos_args;
|
2014-02-16 11:30:49 -05:00
|
|
|
} else {
|
2014-02-15 19:01:29 -05:00
|
|
|
if (self->takes_var_args) {
|
2014-02-16 11:30:49 -05:00
|
|
|
DEBUG_printf("passing empty tuple as *args\n");
|
2014-06-07 19:37:40 -04:00
|
|
|
*var_pos_kw_args-- = mp_const_empty_tuple;
|
2014-02-15 19:01:29 -05:00
|
|
|
}
|
2014-02-16 11:30:49 -05:00
|
|
|
// Apply processing and check below only if we don't have kwargs,
|
|
|
|
// otherwise, kw handling code below has own extensive checks.
|
2014-06-07 19:37:40 -04:00
|
|
|
if (n_kw == 0 && !self->has_def_kw_args) {
|
2014-04-27 10:50:52 -04:00
|
|
|
if (n_args >= self->n_pos_args - self->n_def_args) {
|
2014-02-16 11:30:49 -05:00
|
|
|
// given enough arguments, but may need to use some default arguments
|
2014-06-07 19:37:40 -04:00
|
|
|
for (uint i = n_args; i < self->n_pos_args; i++) {
|
|
|
|
code_state->state[n_state - 1 - i] = self->extra_args[i - (self->n_pos_args - self->n_def_args)];
|
|
|
|
}
|
2014-02-16 11:30:49 -05:00
|
|
|
} else {
|
2014-04-29 22:37:28 -04:00
|
|
|
fun_pos_args_mismatch(self, self->n_pos_args - self->n_def_args, n_args);
|
2014-02-16 11:30:49 -05:00
|
|
|
}
|
|
|
|
}
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
2014-02-15 19:01:29 -05:00
|
|
|
|
2014-06-07 19:37:40 -04:00
|
|
|
// copy positional args into state
|
|
|
|
for (uint i = 0; i < n_args; i++) {
|
|
|
|
code_state->state[n_state - 1 - i] = args[i];
|
|
|
|
}
|
|
|
|
|
2014-02-15 19:17:42 -05:00
|
|
|
// check keyword arguments
|
|
|
|
|
2014-06-07 17:01:00 -04:00
|
|
|
if (n_kw != 0 || self->has_def_kw_args) {
|
2014-02-16 11:30:49 -05:00
|
|
|
DEBUG_printf("Initial args: ");
|
2014-06-07 19:37:40 -04:00
|
|
|
dump_args(code_state->state + n_state - self->n_pos_args - self->n_kwonly_args, self->n_pos_args + self->n_kwonly_args);
|
2014-02-16 11:30:49 -05:00
|
|
|
|
|
|
|
mp_obj_t dict = MP_OBJ_NULL;
|
|
|
|
if (self->takes_kw_args) {
|
|
|
|
dict = mp_obj_new_dict(n_kw); // TODO: better go conservative with 0?
|
2014-06-07 19:37:40 -04:00
|
|
|
*var_pos_kw_args = dict;
|
2014-02-15 19:17:42 -05:00
|
|
|
}
|
2014-06-07 19:37:40 -04:00
|
|
|
|
2014-02-15 19:17:42 -05:00
|
|
|
for (uint i = 0; i < n_kw; i++) {
|
2014-02-16 11:30:49 -05:00
|
|
|
qstr arg_name = MP_OBJ_QSTR_VALUE(kwargs[2 * i]);
|
2014-04-27 10:50:52 -04:00
|
|
|
for (uint j = 0; j < self->n_pos_args + self->n_kwonly_args; j++) {
|
2014-02-16 11:30:49 -05:00
|
|
|
if (arg_name == self->args[j]) {
|
2014-06-07 19:37:40 -04:00
|
|
|
if (code_state->state[n_state - 1 - j] != MP_OBJ_NULL) {
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
2014-02-16 11:30:49 -05:00
|
|
|
"function got multiple values for argument '%s'", qstr_str(arg_name)));
|
|
|
|
}
|
2014-06-07 19:37:40 -04:00
|
|
|
code_state->state[n_state - 1 - j] = kwargs[2 * i + 1];
|
2014-02-16 11:30:49 -05:00
|
|
|
goto continue2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Didn't find name match with positional args
|
|
|
|
if (!self->takes_kw_args) {
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
|
2014-02-16 11:30:49 -05:00
|
|
|
}
|
2014-02-15 19:17:42 -05:00
|
|
|
mp_obj_dict_store(dict, kwargs[2 * i], kwargs[2 * i + 1]);
|
2014-02-16 11:30:49 -05:00
|
|
|
continue2:;
|
|
|
|
}
|
2014-06-07 19:37:40 -04:00
|
|
|
|
2014-02-16 11:30:49 -05:00
|
|
|
DEBUG_printf("Args with kws flattened: ");
|
2014-06-07 19:37:40 -04:00
|
|
|
dump_args(code_state->state + n_state - self->n_pos_args - self->n_kwonly_args, self->n_pos_args + self->n_kwonly_args);
|
2014-02-16 11:30:49 -05:00
|
|
|
|
2014-06-07 19:37:40 -04:00
|
|
|
// fill in defaults for positional args
|
|
|
|
mp_obj_t *d = &code_state->state[n_state - self->n_pos_args];
|
2014-02-16 11:30:49 -05:00
|
|
|
mp_obj_t *s = &self->extra_args[self->n_def_args - 1];
|
2014-06-07 19:37:40 -04:00
|
|
|
for (int i = self->n_def_args; i > 0; i--, d++, s--) {
|
2014-03-03 18:25:08 -05:00
|
|
|
if (*d == MP_OBJ_NULL) {
|
2014-04-09 22:39:38 -04:00
|
|
|
*d = *s;
|
2014-02-16 11:30:49 -05:00
|
|
|
}
|
|
|
|
}
|
2014-06-07 19:37:40 -04:00
|
|
|
|
|
|
|
DEBUG_printf("Args after filling default positional: ");
|
|
|
|
dump_args(code_state->state + n_state - self->n_pos_args - self->n_kwonly_args, self->n_pos_args + self->n_kwonly_args);
|
2014-02-16 11:30:49 -05:00
|
|
|
|
2014-04-27 10:50:52 -04:00
|
|
|
// Check that all mandatory positional args are specified
|
2014-06-07 19:37:40 -04:00
|
|
|
while (d < &code_state->state[n_state]) {
|
|
|
|
if (*d++ == MP_OBJ_NULL) {
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
2014-06-07 19:37:40 -04:00
|
|
|
"function missing required positional argument #%d", &code_state->state[n_state] - d));
|
2014-02-16 11:30:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 10:50:52 -04:00
|
|
|
// Check that all mandatory keyword args are specified
|
2014-06-07 17:01:00 -04:00
|
|
|
// Fill in default kw args if we have them
|
2014-06-07 19:37:40 -04:00
|
|
|
for (uint i = 0; i < self->n_kwonly_args; i++) {
|
|
|
|
if (code_state->state[n_state - 1 - self->n_pos_args - i] == MP_OBJ_NULL) {
|
2014-06-07 17:01:00 -04:00
|
|
|
mp_map_elem_t *elem = NULL;
|
|
|
|
if (self->has_def_kw_args) {
|
|
|
|
elem = mp_map_lookup(&((mp_obj_dict_t*)self->extra_args[self->n_def_args])->map, MP_OBJ_NEW_QSTR(self->args[self->n_pos_args + i]), MP_MAP_LOOKUP);
|
|
|
|
}
|
|
|
|
if (elem != NULL) {
|
2014-06-07 19:37:40 -04:00
|
|
|
code_state->state[n_state - 1 - self->n_pos_args - i] = elem->value;
|
2014-06-07 17:01:00 -04:00
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
|
|
|
"function missing required keyword argument '%s'", qstr_str(self->args[self->n_pos_args + i])));
|
|
|
|
}
|
2014-04-27 10:50:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 19:17:42 -05:00
|
|
|
} else {
|
|
|
|
// no keyword arguments given
|
2014-04-27 10:50:52 -04:00
|
|
|
if (self->n_kwonly_args != 0) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
|
|
|
"function missing keyword-only argument"));
|
|
|
|
}
|
2014-02-15 19:17:42 -05:00
|
|
|
if (self->takes_kw_args) {
|
2014-06-07 19:37:40 -04:00
|
|
|
*var_pos_kw_args = mp_obj_new_dict(0);
|
2014-02-15 19:17:42 -05:00
|
|
|
}
|
2014-01-18 09:10:48 -05:00
|
|
|
}
|
2013-12-21 13:17:45 -05:00
|
|
|
|
2014-06-07 09:16:08 -04:00
|
|
|
// bytecode prelude: initialise closed over variables
|
|
|
|
for (uint n_local = *ip++; n_local > 0; n_local--) {
|
|
|
|
uint local_num = *ip++;
|
|
|
|
code_state->state[n_state - 1 - local_num] = mp_obj_new_cell(code_state->state[n_state - 1 - local_num]);
|
|
|
|
}
|
|
|
|
|
2014-06-07 19:37:40 -04:00
|
|
|
// now that we skipped over the prelude, set the ip for the VM
|
2014-06-07 09:16:08 -04:00
|
|
|
code_state->ip = ip;
|
|
|
|
|
2014-06-07 19:37:40 -04:00
|
|
|
DEBUG_printf("Calling: n_pos_args=%d, n_kwonly_args=%d\n", self->n_pos_args, self->n_kwonly_args);
|
|
|
|
dump_args(code_state->state + n_state - self->n_pos_args - self->n_kwonly_args, self->n_pos_args + self->n_kwonly_args);
|
|
|
|
dump_args(code_state->state, n_state);
|
2014-06-11 10:56:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
|
2014-06-24 20:03:34 -04:00
|
|
|
STACK_CHECK();
|
2014-06-11 10:56:43 -04:00
|
|
|
|
|
|
|
DEBUG_printf("Input n_args: %d, n_kw: %d\n", n_args, n_kw);
|
|
|
|
DEBUG_printf("Input pos args: ");
|
|
|
|
dump_args(args, n_args);
|
|
|
|
DEBUG_printf("Input kw args: ");
|
|
|
|
dump_args(args + n_args, n_kw * 2);
|
|
|
|
mp_obj_fun_bc_t *self = self_in;
|
|
|
|
DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
|
|
|
|
|
|
|
|
const byte *ip = self->bytecode;
|
|
|
|
|
|
|
|
// get code info size, and skip line number table
|
|
|
|
machine_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
|
|
|
|
ip += code_info_size;
|
|
|
|
|
|
|
|
// bytecode prelude: state size and exception stack size; 16 bit uints
|
|
|
|
machine_uint_t n_state = ip[0] | (ip[1] << 8);
|
|
|
|
machine_uint_t n_exc_stack = ip[2] | (ip[3] << 8);
|
|
|
|
ip += 4;
|
|
|
|
|
|
|
|
#if VM_DETECT_STACK_OVERFLOW
|
|
|
|
n_state += 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// allocate state for locals and stack
|
|
|
|
uint state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
|
|
|
|
mp_code_state *code_state;
|
|
|
|
if (state_size > VM_MAX_STATE_ON_STACK) {
|
|
|
|
code_state = m_new_obj_var(mp_code_state, byte, state_size);
|
|
|
|
} else {
|
|
|
|
code_state = alloca(sizeof(mp_code_state) + state_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
code_state->n_state = n_state;
|
|
|
|
code_state->ip = ip;
|
|
|
|
mp_setup_code_state(code_state, self_in, n_args, n_kw, args);
|
2014-06-07 19:37:40 -04:00
|
|
|
|
|
|
|
// execute the byte code with the correct globals context
|
|
|
|
mp_obj_dict_t *old_globals = mp_globals_get();
|
|
|
|
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);
|
2014-06-07 19:37:40 -04:00
|
|
|
mp_globals_set(old_globals);
|
2014-06-07 09:16:08 -04: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-07 19:37:40 -04:00
|
|
|
if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && self->n_pos_args + self->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;
|
2014-06-07 19:37:40 -04:00
|
|
|
for (uint i = 0; i < n_state - self->n_pos_args - self->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) {
|
|
|
|
printf("VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-06-07 19:37:40 -04:00
|
|
|
mp_obj_t result;
|
2014-06-07 09:16:08 -04:00
|
|
|
switch (vm_return_kind) {
|
|
|
|
case MP_VM_RETURN_NORMAL:
|
|
|
|
// return value is in *sp
|
|
|
|
result = *code_state->sp;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_VM_RETURN_EXCEPTION:
|
|
|
|
// return value is in state[n_state - 1]
|
|
|
|
result = code_state->state[n_state - 1];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_VM_RETURN_YIELD: // byte-code shouldn't yield
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
result = mp_const_none;
|
|
|
|
vm_return_kind = MP_VM_RETURN_NORMAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// free the state if it was allocated on the heap
|
|
|
|
if (state_size > VM_MAX_STATE_ON_STACK) {
|
|
|
|
m_del_var(mp_code_state, byte, state_size, code_state);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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,
|
2014-04-30 18:32:58 -04:00
|
|
|
#if MICROPY_CPYTHON_COMPAT
|
|
|
|
.print = fun_bc_print,
|
|
|
|
#endif
|
2014-01-18 09:10:48 -05:00
|
|
|
.call = fun_bc_call,
|
2014-04-05 06:50:06 -04:00
|
|
|
.binary_op = fun_binary_op,
|
2013-12-21 13:17:45 -05:00
|
|
|
};
|
|
|
|
|
2014-06-07 17:01:00 -04:00
|
|
|
mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_pos_args, uint n_kwonly_args, mp_obj_t def_args_in, mp_obj_t def_kw_args, const byte *code) {
|
2014-02-15 19:01:29 -05:00
|
|
|
uint n_def_args = 0;
|
|
|
|
uint n_extra_args = 0;
|
2014-02-01 08:05:04 -05:00
|
|
|
mp_obj_tuple_t *def_args = def_args_in;
|
|
|
|
if (def_args != MP_OBJ_NULL) {
|
2014-04-11 09:38:30 -04:00
|
|
|
assert(MP_OBJ_IS_TYPE(def_args, &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-02-16 11:30:49 -05:00
|
|
|
o->args = args;
|
2014-04-27 10:50:52 -04:00
|
|
|
o->n_pos_args = n_pos_args;
|
|
|
|
o->n_kwonly_args = n_kwonly_args;
|
2014-02-01 08:05:04 -05:00
|
|
|
o->n_def_args = n_def_args;
|
2014-06-07 17:01:00 -04:00
|
|
|
o->has_def_kw_args = def_kw_args != MP_OBJ_NULL;
|
2014-02-15 19:01:29 -05:00
|
|
|
o->takes_var_args = (scope_flags & MP_SCOPE_FLAG_VARARGS) != 0;
|
|
|
|
o->takes_kw_args = (scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0;
|
2014-01-03 09:03:48 -05:00
|
|
|
o->bytecode = code;
|
2014-02-01 08:05:04 -05:00
|
|
|
if (def_args != MP_OBJ_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
|
|
|
}
|
2013-12-21 13:17:45 -05:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* inline assembler functions */
|
|
|
|
|
|
|
|
typedef struct _mp_obj_fun_asm_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
int n_args;
|
|
|
|
void *fun;
|
|
|
|
} mp_obj_fun_asm_t;
|
|
|
|
|
|
|
|
typedef machine_uint_t (*inline_asm_fun_0_t)();
|
|
|
|
typedef machine_uint_t (*inline_asm_fun_1_t)(machine_uint_t);
|
|
|
|
typedef machine_uint_t (*inline_asm_fun_2_t)(machine_uint_t, machine_uint_t);
|
|
|
|
typedef machine_uint_t (*inline_asm_fun_3_t)(machine_uint_t, machine_uint_t, machine_uint_t);
|
|
|
|
|
|
|
|
// convert a Micro Python object to a sensible value for inline asm
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC machine_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;
|
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!)
|
2014-01-22 09:35:10 -05:00
|
|
|
uint l;
|
|
|
|
return (machine_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) {
|
2014-06-01 08:32:54 -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)
|
|
|
|
return (machine_int_t)mp_obj_float_get(obj);
|
2013-12-21 13:17:45 -05:00
|
|
|
#endif
|
2014-04-12 19:30:32 -04:00
|
|
|
} else if (type == &mp_type_tuple) {
|
|
|
|
// pointer to start of tuple (could pass length, but then could use len(x) for that)
|
|
|
|
uint len;
|
|
|
|
mp_obj_t *items;
|
|
|
|
mp_obj_tuple_get(obj, &len, &items);
|
|
|
|
return (machine_uint_t)items;
|
|
|
|
} else if (type == &mp_type_list) {
|
|
|
|
// pointer to start of list (could pass length, but then could use len(x) for that)
|
|
|
|
uint len;
|
|
|
|
mp_obj_t *items;
|
|
|
|
mp_obj_list_get(obj, &len, &items);
|
|
|
|
return (machine_uint_t)items;
|
|
|
|
} 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
|
|
|
|
return (machine_uint_t)bufinfo.buf;
|
|
|
|
} else {
|
|
|
|
// just pass along a pointer to the object
|
|
|
|
return (machine_uint_t)obj;
|
|
|
|
}
|
2014-04-12 19:30:32 -04:00
|
|
|
}
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert a return value from inline asm to a sensible Micro Python object
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t convert_val_from_inline_asm(machine_uint_t val) {
|
2013-12-21 13:17:45 -05:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(val);
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t fun_asm_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_fun_asm_t *self = self_in;
|
|
|
|
|
2014-05-10 08:40:46 -04:00
|
|
|
mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
|
2013-12-21 13:17:45 -05:00
|
|
|
|
|
|
|
machine_uint_t ret;
|
|
|
|
if (n_args == 0) {
|
|
|
|
ret = ((inline_asm_fun_0_t)self->fun)();
|
|
|
|
} else if (n_args == 1) {
|
|
|
|
ret = ((inline_asm_fun_1_t)self->fun)(convert_obj_for_inline_asm(args[0]));
|
|
|
|
} else if (n_args == 2) {
|
2014-01-18 09:10:48 -05:00
|
|
|
ret = ((inline_asm_fun_2_t)self->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-01-18 09:10:48 -05:00
|
|
|
ret = ((inline_asm_fun_3_t)self->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 {
|
|
|
|
assert(0);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return convert_val_from_inline_asm(ret);
|
|
|
|
}
|
|
|
|
|
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,
|
2014-04-05 06:50:06 -04:00
|
|
|
.binary_op = fun_binary_op,
|
2013-12-21 13:17:45 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun) {
|
|
|
|
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;
|
|
|
|
o->fun = fun;
|
|
|
|
return o;
|
|
|
|
}
|