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.
|
|
|
|
*/
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 15:27:54 -05: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 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-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 */
|
|
|
|
|
|
|
|
// mp_obj_fun_builtin_t defined in obj.h
|
|
|
|
|
2014-08-29 19:35:11 -04:00
|
|
|
STATIC mp_obj_t fun_builtin_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
2014-08-24 11:28:17 -04:00
|
|
|
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin));
|
|
|
|
mp_obj_fun_builtin_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:
|
2013-12-21 13:17:45 -05:00
|
|
|
default:
|
2015-01-14 16:32:42 -05:00
|
|
|
return ((mp_fun_3_t)self->fun)(args[0], args[1], args[2]);
|
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
|
|
|
|
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-08-24 11:28:17 -04:00
|
|
|
const mp_obj_type_t mp_type_fun_builtin = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
2014-02-15 06:34:50 -05:00
|
|
|
.name = MP_QSTR_function,
|
2014-08-24 11:28:17 -04:00
|
|
|
.call = fun_builtin_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) {
|
2014-09-04 09:44:01 -04:00
|
|
|
mp_decode_uint(&code_info); // skip code_info_size entry
|
2015-02-14 12:44:31 -05:00
|
|
|
return mp_decode_uint(&code_info);
|
2014-04-30 18:32:58 -04:00
|
|
|
}
|
|
|
|
|
2015-04-06 17:38:53 -04:00
|
|
|
#if MICROPY_EMIT_NATIVE
|
|
|
|
STATIC const mp_obj_type_t mp_type_fun_native;
|
|
|
|
#endif
|
|
|
|
|
2015-02-14 12:44:31 -05:00
|
|
|
qstr mp_obj_fun_get_name(mp_const_obj_t fun_in) {
|
2014-05-17 04:08:33 -04:00
|
|
|
const mp_obj_fun_bc_t *fun = 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
|
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
|
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;
|
2014-04-30 18:32:58 -04:00
|
|
|
mp_obj_fun_bc_t *o = o_in;
|
2015-04-11 08:03:37 -04:00
|
|
|
mp_printf(print, "<function %q at 0x%x>", mp_obj_fun_get_name(o), o);
|
2014-04-30 18:32:58 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-16 11:30:49 -05:00
|
|
|
#if DEBUG_PRINT
|
2014-10-03 14:52:22 -04:00
|
|
|
STATIC void dump_args(const mp_obj_t *a, mp_uint_t sz) {
|
2014-02-16 11:30:49 -05:00
|
|
|
DEBUG_printf("%p: ", a);
|
2014-10-03 14:52:22 -04:00
|
|
|
for (mp_uint_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
|
|
|
|
|
|
|
// Set this to enable a simple stack overflow check.
|
|
|
|
#define VM_DETECT_STACK_OVERFLOW (0)
|
|
|
|
|
2015-04-01 18:31:30 -04:00
|
|
|
#if MICROPY_STACKLESS
|
2015-03-27 19:14:44 -04:00
|
|
|
mp_code_state *mp_obj_fun_bc_prepare_codestate(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
|
|
|
MP_STACK_CHECK();
|
|
|
|
mp_obj_fun_bc_t *self = self_in;
|
|
|
|
|
|
|
|
// skip code-info block
|
|
|
|
const byte *code_info = self->bytecode;
|
|
|
|
mp_uint_t code_info_size = mp_decode_uint(&code_info);
|
|
|
|
const byte *ip = self->bytecode + code_info_size;
|
|
|
|
|
|
|
|
// bytecode prelude: skip arg names
|
|
|
|
ip += (self->n_pos_args + self->n_kwonly_args) * sizeof(mp_obj_t);
|
|
|
|
|
|
|
|
// bytecode prelude: state size and exception stack size
|
|
|
|
mp_uint_t n_state = mp_decode_uint(&ip);
|
|
|
|
mp_uint_t n_exc_stack = mp_decode_uint(&ip);
|
|
|
|
|
|
|
|
// allocate state for locals and stack
|
|
|
|
mp_uint_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
|
|
|
|
mp_code_state *code_state;
|
2015-03-27 19:14:44 -04:00
|
|
|
code_state = m_new_obj_var_maybe(mp_code_state, byte, state_size);
|
|
|
|
if (!code_state) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-03-27 19:14:44 -04:00
|
|
|
|
|
|
|
code_state->n_state = n_state;
|
2015-04-06 17:38:53 -04:00
|
|
|
code_state->code_info = 0; // offset to code-info
|
2015-04-24 18:43:45 -04:00
|
|
|
code_state->ip = (byte*)(ip - self->bytecode); // offset to prelude
|
2015-03-27 19:14:44 -04:00
|
|
|
mp_setup_code_state(code_state, self_in, n_args, n_kw, args);
|
|
|
|
|
|
|
|
// 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 18:31:30 -04:00
|
|
|
#endif
|
2015-03-27 19:14:44 -04:00
|
|
|
|
2014-08-29 19:35:11 -04:00
|
|
|
STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_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);
|
|
|
|
mp_obj_fun_bc_t *self = self_in;
|
|
|
|
DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
|
|
|
|
|
2014-09-04 09:44:01 -04:00
|
|
|
// skip code-info block
|
|
|
|
const byte *code_info = self->bytecode;
|
|
|
|
mp_uint_t code_info_size = mp_decode_uint(&code_info);
|
|
|
|
const byte *ip = self->bytecode + code_info_size;
|
2014-06-11 10:56:43 -04:00
|
|
|
|
2014-10-25 10:07:02 -04:00
|
|
|
// bytecode prelude: skip arg names
|
|
|
|
ip += (self->n_pos_args + self->n_kwonly_args) * sizeof(mp_obj_t);
|
|
|
|
|
|
|
|
// bytecode prelude: state size and exception stack size
|
2014-09-04 09:44:01 -04:00
|
|
|
mp_uint_t n_state = mp_decode_uint(&ip);
|
|
|
|
mp_uint_t n_exc_stack = mp_decode_uint(&ip);
|
2014-06-11 10:56:43 -04:00
|
|
|
|
|
|
|
#if VM_DETECT_STACK_OVERFLOW
|
|
|
|
n_state += 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// allocate state for locals and stack
|
2014-08-24 11:28:17 -04:00
|
|
|
mp_uint_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
|
2015-06-08 17:07:27 -04:00
|
|
|
mp_code_state *code_state = NULL;
|
2014-06-11 10:56:43 -04:00
|
|
|
if (state_size > VM_MAX_STATE_ON_STACK) {
|
2015-06-08 17:07:27 -04:00
|
|
|
code_state = m_new_obj_var_maybe(mp_code_state, byte, state_size);
|
|
|
|
}
|
|
|
|
if (code_state == NULL) {
|
2014-06-11 10:56:43 -04:00
|
|
|
code_state = alloca(sizeof(mp_code_state) + state_size);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
code_state->n_state = n_state;
|
2015-04-06 17:38:53 -04:00
|
|
|
code_state->code_info = 0; // offset to code-info
|
|
|
|
code_state->ip = (byte*)(ip - self->bytecode); // offset to prelude
|
2014-06-11 10:56:43 -04:00
|
|
|
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
|
2015-02-15 04:12:58 -05:00
|
|
|
code_state->old_globals = mp_globals_get();
|
2014-06-07 19:37:40 -04:00
|
|
|
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
|
|
|
|
|
|
|
#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-08-24 11:28:17 -04:00
|
|
|
for (mp_uint_t 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
|
2015-06-08 17:07:27 -04:00
|
|
|
if (state_size != 0) {
|
2014-06-07 09:16:08 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-02-14 12:44:31 -05:00
|
|
|
#if MICROPY_PY_FUNCTION_ATTRS
|
2015-04-01 10:10:50 -04: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 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,
|
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,
|
2015-05-11 08:25:19 -04:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2015-02-14 12:44:31 -05:00
|
|
|
#if MICROPY_PY_FUNCTION_ATTRS
|
2015-04-01 10:10:50 -04:00
|
|
|
.attr = fun_bc_attr,
|
2015-02-14 12:44:31 -05:00
|
|
|
#endif
|
2013-12-21 13:17:45 -05:00
|
|
|
};
|
|
|
|
|
2014-10-25 10:07:02 -04:00
|
|
|
mp_obj_t mp_obj_new_fun_bc(mp_uint_t scope_flags, mp_uint_t n_pos_args, mp_uint_t n_kwonly_args, mp_obj_t def_args_in, mp_obj_t def_kw_args, const byte *code) {
|
2014-08-24 11:28:17 -04:00
|
|
|
mp_uint_t n_def_args = 0;
|
|
|
|
mp_uint_t 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-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;
|
|
|
|
}
|
|
|
|
|
2014-08-24 11:28:17 -04:00
|
|
|
/******************************************************************************/
|
|
|
|
/* native functions */
|
|
|
|
|
|
|
|
#if MICROPY_EMIT_NATIVE
|
|
|
|
|
2014-08-29 19:35:11 -04:00
|
|
|
STATIC mp_obj_t fun_native_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_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;
|
|
|
|
mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void*)self->bytecode);
|
|
|
|
return fun(self_in, n_args, n_kw, args);
|
2014-08-24 11:28:17 -04: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 08:25:19 -04:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2014-08-24 11:28:17 -04:00
|
|
|
};
|
|
|
|
|
2015-04-06 17:38:53 -04:00
|
|
|
mp_obj_t mp_obj_new_fun_native(mp_uint_t scope_flags, mp_uint_t n_pos_args, mp_uint_t n_kwonly_args, mp_obj_t def_args_in, mp_obj_t def_kw_args, const void *fun_data) {
|
|
|
|
mp_obj_fun_bc_t *o = mp_obj_new_fun_bc(scope_flags, n_pos_args, n_kwonly_args, def_args_in, def_kw_args, (const byte*)fun_data);
|
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
|
|
|
/******************************************************************************/
|
|
|
|
/* viper functions */
|
|
|
|
|
|
|
|
#if MICROPY_EMIT_NATIVE
|
|
|
|
|
|
|
|
typedef struct _mp_obj_fun_viper_t {
|
|
|
|
mp_obj_base_t base;
|
2014-08-24 11:28:17 -04:00
|
|
|
mp_uint_t n_args;
|
|
|
|
void *fun_data; // GC must be able to trace this pointer
|
2014-08-15 11:45:41 -04:00
|
|
|
mp_uint_t type_sig;
|
|
|
|
} mp_obj_fun_viper_t;
|
|
|
|
|
2015-01-12 17:45:35 -05:00
|
|
|
typedef mp_uint_t (*viper_fun_0_t)(void);
|
2014-08-15 11:45:41 -04: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 09:11:29 -04:00
|
|
|
typedef mp_uint_t (*viper_fun_4_t)(mp_uint_t, mp_uint_t, mp_uint_t, mp_uint_t);
|
2014-08-15 11:45:41 -04:00
|
|
|
|
2014-08-29 19:35:11 -04:00
|
|
|
STATIC mp_obj_t fun_viper_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
2014-08-15 11:45:41 -04: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 11:28:17 -04:00
|
|
|
void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
|
|
|
|
|
2014-08-15 11:45:41 -04:00
|
|
|
mp_uint_t ret;
|
|
|
|
if (n_args == 0) {
|
2014-08-24 11:28:17 -04:00
|
|
|
ret = ((viper_fun_0_t)fun)();
|
2014-08-15 11:45:41 -04:00
|
|
|
} else if (n_args == 1) {
|
2014-08-24 11:28:17 -04:00
|
|
|
ret = ((viper_fun_1_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 2));
|
2014-08-15 11:45:41 -04:00
|
|
|
} else if (n_args == 2) {
|
2014-08-24 11:28:17 -04:00
|
|
|
ret = ((viper_fun_2_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 2), mp_convert_obj_to_native(args[1], self->type_sig >> 4));
|
2014-08-15 11:45:41 -04:00
|
|
|
} else if (n_args == 3) {
|
2014-08-24 11:28:17 -04:00
|
|
|
ret = ((viper_fun_3_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 2), mp_convert_obj_to_native(args[1], self->type_sig >> 4), mp_convert_obj_to_native(args[2], self->type_sig >> 6));
|
2015-07-23 09:11:29 -04:00
|
|
|
} else if (n_args == 4) {
|
|
|
|
ret = ((viper_fun_4_t)fun)(
|
|
|
|
mp_convert_obj_to_native(args[0], self->type_sig >> 2),
|
|
|
|
mp_convert_obj_to_native(args[1], self->type_sig >> 4),
|
|
|
|
mp_convert_obj_to_native(args[2], self->type_sig >> 6),
|
|
|
|
mp_convert_obj_to_native(args[3], self->type_sig >> 8)
|
|
|
|
);
|
2014-08-15 11:45:41 -04:00
|
|
|
} else {
|
2015-07-23 09:11:29 -04:00
|
|
|
// TODO 5 or more arguments not supported for viper call
|
2014-08-15 11:45:41 -04:00
|
|
|
assert(0);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
2014-08-15 18:47:59 -04:00
|
|
|
return mp_convert_native_to_obj(ret, self->type_sig);
|
2014-08-15 11:45:41 -04: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 08:25:19 -04:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2014-08-15 11:45:41 -04:00
|
|
|
};
|
|
|
|
|
2014-08-24 11:28:17 -04:00
|
|
|
mp_obj_t mp_obj_new_fun_viper(mp_uint_t n_args, void *fun_data, mp_uint_t type_sig) {
|
2014-08-15 11:45:41 -04: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 11:28:17 -04:00
|
|
|
o->fun_data = fun_data;
|
2014-08-15 11:45:41 -04:00
|
|
|
o->type_sig = type_sig;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MICROPY_EMIT_NATIVE
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
/* inline assembler functions */
|
|
|
|
|
2014-08-15 11:45:41 -04:00
|
|
|
#if MICROPY_EMIT_INLINE_THUMB
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
typedef struct _mp_obj_fun_asm_t {
|
|
|
|
mp_obj_base_t base;
|
2014-08-24 11:28:17 -04:00
|
|
|
mp_uint_t n_args;
|
|
|
|
void *fun_data; // GC must be able to trace this pointer
|
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);
|
2013-12-21 13:17:45 -05:00
|
|
|
|
|
|
|
// convert a Micro Python 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!)
|
2014-09-29 11:26:39 -04:00
|
|
|
mp_uint_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) {
|
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)
|
2014-07-03 08:25:24 -04:00
|
|
|
return (mp_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)
|
2014-09-29 11:26:39 -04:00
|
|
|
mp_uint_t len;
|
2014-04-12 19:30:32 -04:00
|
|
|
mp_obj_t *items;
|
|
|
|
mp_obj_tuple_get(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 if (type == &mp_type_list) {
|
|
|
|
// pointer to start of list (could pass length, but then could use len(x) for that)
|
2014-09-29 11:26:39 -04:00
|
|
|
mp_uint_t len;
|
2014-04-12 19:30:32 -04:00
|
|
|
mp_obj_t *items;
|
|
|
|
mp_obj_list_get(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert a return value from inline asm to a sensible Micro Python object
|
2014-07-03 08:25:24 -04:00
|
|
|
STATIC mp_obj_t convert_val_from_inline_asm(mp_uint_t val) {
|
2013-12-21 13:17:45 -05:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(val);
|
|
|
|
}
|
|
|
|
|
2014-08-29 19:35:11 -04:00
|
|
|
STATIC mp_obj_t fun_asm_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t 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
|
|
|
|
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 {
|
|
|
|
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,
|
2015-05-11 08:25:19 -04:00
|
|
|
.unary_op = mp_generic_unary_op,
|
2013-12-21 13:17:45 -05:00
|
|
|
};
|
|
|
|
|
2014-08-24 11:28:17 -04:00
|
|
|
mp_obj_t mp_obj_new_fun_asm(mp_uint_t n_args, void *fun_data) {
|
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;
|
2013-12-21 13:17:45 -05:00
|
|
|
return o;
|
|
|
|
}
|
2014-08-15 11:45:41 -04:00
|
|
|
|
|
|
|
#endif // MICROPY_EMIT_INLINE_THUMB
|