2014-05-03 22:27:38 +00: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 05:44:45 +00:00
|
|
|
* Copyright (c) 2014 Paul Sokolovsky
|
2014-05-03 22:27:38 +00: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 10:50:06 +00:00
|
|
|
|
2014-08-24 15:28:17 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
/* builtin functions */
|
|
|
|
|
|
|
|
// mp_obj_fun_builtin_t defined in obj.h
|
|
|
|
|
2014-08-29 23:35:11 +00: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 15:28:17 +00:00
|
|
|
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin));
|
|
|
|
mp_obj_fun_builtin_t *self = self_in;
|
2014-01-18 14:10:48 +00:00
|
|
|
|
2014-01-13 19:55:18 +00:00
|
|
|
// check number of arguments
|
2014-04-19 23:13:22 +00: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
|
|
|
|
2014-02-08 18:53:41 +00:00
|
|
|
return ((mp_fun_kw_t)self->fun)(n_args, args, &kw_args);
|
2014-01-18 14:10:48 +00:00
|
|
|
|
2014-01-25 23:58:51 +00:00
|
|
|
} else if (self->n_args_min <= 3 && self->n_args_min == self->n_args_max) {
|
2013-12-21 18:17:45 +00: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 14:10:48 +00:00
|
|
|
return ((mp_fun_2_t)self->fun)(args[0], args[1]);
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2014-01-04 01:15:01 +00:00
|
|
|
case 3:
|
2013-12-21 18:17:45 +00:00
|
|
|
default:
|
2015-01-14 21:32:42 +00:00
|
|
|
return ((mp_fun_3_t)self->fun)(args[0], args[1], args[2]);
|
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
|
|
|
|
2014-01-18 14:10:48 +00:00
|
|
|
return ((mp_fun_var_t)self->fun)(n_args, args);
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-24 15:28:17 +00:00
|
|
|
const mp_obj_type_t mp_type_fun_builtin = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_function,
|
2014-08-24 15:28:17 +00:00
|
|
|
.call = fun_builtin_call,
|
2013-12-21 18:17:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* byte code functions */
|
|
|
|
|
2014-05-01 19:20:07 +00:00
|
|
|
const char *mp_obj_code_get_name(const byte *code_info) {
|
2014-09-04 13:44:01 +00:00
|
|
|
mp_decode_uint(&code_info); // skip code_info_size entry
|
|
|
|
return qstr_str(mp_decode_uint(&code_info));
|
2014-04-30 22:32:58 +00:00
|
|
|
}
|
|
|
|
|
2014-05-17 08:08:33 +00: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 11:10:34 +00:00
|
|
|
const byte *code_info = fun->bytecode;
|
2014-05-01 19:20:07 +00:00
|
|
|
return mp_obj_code_get_name(code_info);
|
|
|
|
}
|
|
|
|
|
2014-04-30 22:32:58 +00: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) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)kind;
|
2014-04-30 22:32:58 +00:00
|
|
|
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 16:30:49 +00:00
|
|
|
#if DEBUG_PRINT
|
2014-10-03 18:52:22 +00:00
|
|
|
STATIC void dump_args(const mp_obj_t *a, mp_uint_t sz) {
|
2014-02-16 16:30:49 +00:00
|
|
|
DEBUG_printf("%p: ", a);
|
2014-10-03 18:52:22 +00:00
|
|
|
for (mp_uint_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 14:59:25 +00:00
|
|
|
#else
|
|
|
|
#define dump_args(...) (void)0
|
|
|
|
#endif
|
2014-02-16 16:30:49 +00:00
|
|
|
|
2014-06-07 13:16:08 +00: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.
|
2015-02-15 09:12:58 +00:00
|
|
|
#define VM_MAX_STATE_ON_STACK (11 * sizeof(mp_uint_t))
|
2014-06-07 13:16:08 +00:00
|
|
|
|
|
|
|
// Set this to enable a simple stack overflow check.
|
|
|
|
#define VM_DETECT_STACK_OVERFLOW (0)
|
|
|
|
|
2014-08-29 23:35:11 +00: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 23:13:42 +00:00
|
|
|
MP_STACK_CHECK();
|
2014-06-11 14:56:43 +00:00
|
|
|
|
2014-09-23 09:59:05 +00:00
|
|
|
DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
|
2014-06-11 14:56:43 +00: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 13:44:01 +00: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 14:56:43 +00:00
|
|
|
|
2014-10-25 14:07:02 +00: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 13:44:01 +00:00
|
|
|
mp_uint_t n_state = mp_decode_uint(&ip);
|
|
|
|
mp_uint_t n_exc_stack = mp_decode_uint(&ip);
|
2014-06-11 14:56:43 +00:00
|
|
|
|
|
|
|
#if VM_DETECT_STACK_OVERFLOW
|
|
|
|
n_state += 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// allocate state for locals and stack
|
2014-08-24 15:28:17 +00:00
|
|
|
mp_uint_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
|
2014-06-11 14:56:43 +00:00
|
|
|
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 23:37:40 +00: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-07 23:37:40 +00:00
|
|
|
mp_globals_set(self->globals);
|
2014-06-07 13:16:08 +00: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 13:16:08 +00: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 23:37:40 +00:00
|
|
|
if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && self->n_pos_args + self->n_kwonly_args == 0)) {
|
2014-06-07 13:16:08 +00:00
|
|
|
// Just check to see that we have at least 1 null object left in the state.
|
|
|
|
bool overflow = true;
|
2014-08-24 15:28:17 +00:00
|
|
|
for (mp_uint_t i = 0; i < n_state - self->n_pos_args - self->n_kwonly_args; i++) {
|
2014-06-07 13:16:08 +00: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 23:37:40 +00:00
|
|
|
mp_obj_t result;
|
2014-06-07 13:16:08 +00: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 22:55:00 +00:00
|
|
|
if (vm_return_kind == MP_VM_RETURN_NORMAL) {
|
|
|
|
return result;
|
|
|
|
} else { // MP_VM_RETURN_EXCEPTION
|
2014-04-05 17:32:08 +00:00
|
|
|
nlr_raise(result);
|
2014-02-15 22:55:00 +00:00
|
|
|
}
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
|
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 22:32:58 +00:00
|
|
|
#if MICROPY_CPYTHON_COMPAT
|
|
|
|
.print = fun_bc_print,
|
|
|
|
#endif
|
2014-01-18 14:10:48 +00:00
|
|
|
.call = fun_bc_call,
|
2013-12-21 18:17:45 +00:00
|
|
|
};
|
|
|
|
|
2014-10-25 14:07:02 +00: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 15:28:17 +00:00
|
|
|
mp_uint_t n_def_args = 0;
|
|
|
|
mp_uint_t n_extra_args = 0;
|
2014-02-01 13:05:04 +00:00
|
|
|
mp_obj_tuple_t *def_args = def_args_in;
|
|
|
|
if (def_args != MP_OBJ_NULL) {
|
2014-04-11 13:38:30 +00:00
|
|
|
assert(MP_OBJ_IS_TYPE(def_args, &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 21:01:00 +00: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 12:35:08 +00:00
|
|
|
o->globals = mp_globals_get();
|
2014-04-27 14:50:52 +00:00
|
|
|
o->n_pos_args = n_pos_args;
|
|
|
|
o->n_kwonly_args = n_kwonly_args;
|
2014-02-01 13:05:04 +00:00
|
|
|
o->n_def_args = n_def_args;
|
2014-06-07 21:01:00 +00:00
|
|
|
o->has_def_kw_args = def_kw_args != MP_OBJ_NULL;
|
2014-02-16 00:01:29 +00: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 14:03:48 +00:00
|
|
|
o->bytecode = code;
|
2014-02-01 13:05:04 +00:00
|
|
|
if (def_args != MP_OBJ_NULL) {
|
2014-06-07 23:37:40 +00:00
|
|
|
memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
|
2014-06-07 21:01:00 +00:00
|
|
|
}
|
|
|
|
if (def_kw_args != MP_OBJ_NULL) {
|
2014-06-07 23:37:40 +00:00
|
|
|
o->extra_args[n_def_args] = def_kw_args;
|
2014-04-27 14:50:52 +00:00
|
|
|
}
|
2013-12-21 18:17:45 +00:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-08-24 15:28:17 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
/* native functions */
|
|
|
|
|
|
|
|
#if MICROPY_EMIT_NATIVE
|
|
|
|
|
|
|
|
typedef struct _mp_obj_fun_native_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_uint_t n_args;
|
|
|
|
void *fun_data; // GC must be able to trace this pointer
|
|
|
|
// TODO add mp_map_t *globals
|
|
|
|
} mp_obj_fun_native_t;
|
|
|
|
|
2015-01-12 22:45:35 +00:00
|
|
|
typedef mp_obj_t (*native_fun_0_t)(void);
|
2014-08-24 15:28:17 +00:00
|
|
|
typedef mp_obj_t (*native_fun_1_t)(mp_obj_t);
|
|
|
|
typedef mp_obj_t (*native_fun_2_t)(mp_obj_t, mp_obj_t);
|
|
|
|
typedef mp_obj_t (*native_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
|
|
|
|
|
2014-08-29 23:35:11 +00: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) {
|
2014-08-24 15:28:17 +00:00
|
|
|
mp_obj_fun_native_t *self = self_in;
|
|
|
|
|
|
|
|
mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
|
|
|
|
|
|
|
|
void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
|
|
|
|
|
|
|
|
switch (n_args) {
|
|
|
|
case 0:
|
|
|
|
return ((native_fun_0_t)fun)();
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
return ((native_fun_1_t)fun)(args[0]);
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
return ((native_fun_2_t)fun)(args[0], args[1]);
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
return ((native_fun_3_t)fun)(args[0], args[1], args[2]);
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t mp_type_fun_native = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_function,
|
|
|
|
.call = fun_native_call,
|
|
|
|
};
|
|
|
|
|
|
|
|
mp_obj_t mp_obj_new_fun_native(mp_uint_t n_args, void *fun_data) {
|
|
|
|
assert(0 <= n_args && n_args <= 3);
|
|
|
|
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
|
|
|
|
o->base.type = &mp_type_fun_native;
|
|
|
|
o->n_args = n_args;
|
|
|
|
o->fun_data = fun_data;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MICROPY_EMIT_NATIVE
|
|
|
|
|
2014-08-15 15:45:41 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
/* viper functions */
|
|
|
|
|
|
|
|
#if MICROPY_EMIT_NATIVE
|
|
|
|
|
|
|
|
typedef struct _mp_obj_fun_viper_t {
|
|
|
|
mp_obj_base_t base;
|
2014-08-24 15:28:17 +00:00
|
|
|
mp_uint_t n_args;
|
|
|
|
void *fun_data; // GC must be able to trace this pointer
|
2014-08-15 15:45:41 +00: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 15:45:41 +00: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);
|
|
|
|
|
2014-08-29 23:35:11 +00: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 15:45:41 +00: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 15:28:17 +00:00
|
|
|
void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
|
|
|
|
|
2014-08-15 15:45:41 +00:00
|
|
|
mp_uint_t ret;
|
|
|
|
if (n_args == 0) {
|
2014-08-24 15:28:17 +00:00
|
|
|
ret = ((viper_fun_0_t)fun)();
|
2014-08-15 15:45:41 +00:00
|
|
|
} else if (n_args == 1) {
|
2014-08-24 15:28:17 +00:00
|
|
|
ret = ((viper_fun_1_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 2));
|
2014-08-15 15:45:41 +00:00
|
|
|
} else if (n_args == 2) {
|
2014-08-24 15:28:17 +00: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 15:45:41 +00:00
|
|
|
} else if (n_args == 3) {
|
2014-08-24 15:28:17 +00: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));
|
2014-08-15 15:45:41 +00:00
|
|
|
} else {
|
|
|
|
assert(0);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
2014-08-15 22:47:59 +00:00
|
|
|
return mp_convert_native_to_obj(ret, self->type_sig);
|
2014-08-15 15:45:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t mp_type_fun_viper = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_function,
|
|
|
|
.call = fun_viper_call,
|
|
|
|
};
|
|
|
|
|
2014-08-24 15:28:17 +00: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 15:45:41 +00: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 15:28:17 +00:00
|
|
|
o->fun_data = fun_data;
|
2014-08-15 15:45:41 +00:00
|
|
|
o->type_sig = type_sig;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MICROPY_EMIT_NATIVE
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
/* inline assembler functions */
|
|
|
|
|
2014-08-15 15:45:41 +00:00
|
|
|
#if MICROPY_EMIT_INLINE_THUMB
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
typedef struct _mp_obj_fun_asm_t {
|
|
|
|
mp_obj_base_t base;
|
2014-08-24 15:28:17 +00:00
|
|
|
mp_uint_t n_args;
|
|
|
|
void *fun_data; // GC must be able to trace this pointer
|
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 12:25:24 +00: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 18:17:45 +00:00
|
|
|
|
|
|
|
// convert a Micro Python object to a sensible value for inline asm
|
2014-07-03 12:25:24 +00: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!)
|
2014-09-29 15:26:39 +00:00
|
|
|
mp_uint_t l;
|
2014-07-03 12:25:24 +00:00
|
|
|
return (mp_uint_t)mp_obj_str_get_data(obj, &l);
|
2014-04-12 23:30:32 +00:00
|
|
|
} else {
|
|
|
|
mp_obj_type_t *type = mp_obj_get_type(obj);
|
|
|
|
if (0) {
|
2014-06-01 12:32:54 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-04-12 23:30:32 +00:00
|
|
|
} else if (type == &mp_type_float) {
|
|
|
|
// convert float to int (could also pass in float registers)
|
2014-07-03 12:25:24 +00:00
|
|
|
return (mp_int_t)mp_obj_float_get(obj);
|
2013-12-21 18:17:45 +00:00
|
|
|
#endif
|
2014-04-12 23:30:32 +00: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 15:26:39 +00:00
|
|
|
mp_uint_t len;
|
2014-04-12 23:30:32 +00:00
|
|
|
mp_obj_t *items;
|
|
|
|
mp_obj_tuple_get(obj, &len, &items);
|
2014-07-03 12:25:24 +00:00
|
|
|
return (mp_uint_t)items;
|
2014-04-12 23:30:32 +00: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 15:26:39 +00:00
|
|
|
mp_uint_t len;
|
2014-04-12 23:30:32 +00:00
|
|
|
mp_obj_t *items;
|
|
|
|
mp_obj_list_get(obj, &len, &items);
|
2014-07-03 12:25:24 +00:00
|
|
|
return (mp_uint_t)items;
|
2014-04-12 23:30:32 +00:00
|
|
|
} else {
|
2014-04-18 21:29:21 +00:00
|
|
|
mp_buffer_info_t bufinfo;
|
2014-04-18 21:59:24 +00:00
|
|
|
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
|
2014-04-13 11:08:52 +00:00
|
|
|
// supports the buffer protocol, return a pointer to the data
|
2014-07-03 12:25:24 +00:00
|
|
|
return (mp_uint_t)bufinfo.buf;
|
2014-04-13 11:08:52 +00:00
|
|
|
} else {
|
|
|
|
// just pass along a pointer to the object
|
2014-07-03 12:25:24 +00:00
|
|
|
return (mp_uint_t)obj;
|
2014-04-13 11:08:52 +00:00
|
|
|
}
|
2014-04-12 23:30:32 +00:00
|
|
|
}
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert a return value from inline asm to a sensible Micro Python object
|
2014-07-03 12:25:24 +00:00
|
|
|
STATIC mp_obj_t convert_val_from_inline_asm(mp_uint_t val) {
|
2013-12-21 18:17:45 +00:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(val);
|
|
|
|
}
|
|
|
|
|
2014-08-29 23:35:11 +00: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 18:17:45 +00:00
|
|
|
mp_obj_fun_asm_t *self = self_in;
|
|
|
|
|
2014-05-10 12:40:46 +00: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 15:28:17 +00:00
|
|
|
void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
|
|
|
|
|
2014-07-03 12:25:24 +00:00
|
|
|
mp_uint_t ret;
|
2013-12-21 18:17:45 +00:00
|
|
|
if (n_args == 0) {
|
2014-08-24 15:28:17 +00:00
|
|
|
ret = ((inline_asm_fun_0_t)fun)();
|
2013-12-21 18:17:45 +00:00
|
|
|
} else if (n_args == 1) {
|
2014-08-24 15:28:17 +00: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 15:28:17 +00: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 15:28:17 +00: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 {
|
|
|
|
assert(0);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return convert_val_from_inline_asm(ret);
|
|
|
|
}
|
|
|
|
|
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,
|
2013-12-21 18:17:45 +00:00
|
|
|
};
|
|
|
|
|
2014-08-24 15:28:17 +00:00
|
|
|
mp_obj_t mp_obj_new_fun_asm(mp_uint_t n_args, void *fun_data) {
|
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 15:28:17 +00:00
|
|
|
o->fun_data = fun_data;
|
2013-12-21 18:17:45 +00:00
|
|
|
return o;
|
|
|
|
}
|
2014-08-15 15:45:41 +00:00
|
|
|
|
|
|
|
#endif // MICROPY_EMIT_INLINE_THUMB
|