Colin Hogben / micropython

Dependents:   micropython-repl

Committer:
pythontech
Date:
Sat Apr 16 17:11:56 2016 +0000
Revision:
0:5868e8752d44
Split off library from repl

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pythontech 0:5868e8752d44 1 /*
pythontech 0:5868e8752d44 2 * This file is part of the Micro Python project, http://micropython.org/
pythontech 0:5868e8752d44 3 *
pythontech 0:5868e8752d44 4 * The MIT License (MIT)
pythontech 0:5868e8752d44 5 *
pythontech 0:5868e8752d44 6 * Copyright (c) 2013, 2014 Damien P. George
pythontech 0:5868e8752d44 7 * Copyright (c) 2014 Paul Sokolovsky
pythontech 0:5868e8752d44 8 *
pythontech 0:5868e8752d44 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
pythontech 0:5868e8752d44 10 * of this software and associated documentation files (the "Software"), to deal
pythontech 0:5868e8752d44 11 * in the Software without restriction, including without limitation the rights
pythontech 0:5868e8752d44 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pythontech 0:5868e8752d44 13 * copies of the Software, and to permit persons to whom the Software is
pythontech 0:5868e8752d44 14 * furnished to do so, subject to the following conditions:
pythontech 0:5868e8752d44 15 *
pythontech 0:5868e8752d44 16 * The above copyright notice and this permission notice shall be included in
pythontech 0:5868e8752d44 17 * all copies or substantial portions of the Software.
pythontech 0:5868e8752d44 18 *
pythontech 0:5868e8752d44 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pythontech 0:5868e8752d44 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pythontech 0:5868e8752d44 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pythontech 0:5868e8752d44 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pythontech 0:5868e8752d44 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pythontech 0:5868e8752d44 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pythontech 0:5868e8752d44 25 * THE SOFTWARE.
pythontech 0:5868e8752d44 26 */
pythontech 0:5868e8752d44 27
pythontech 0:5868e8752d44 28 #include <string.h>
pythontech 0:5868e8752d44 29 #include <assert.h>
pythontech 0:5868e8752d44 30
pythontech 0:5868e8752d44 31 #include "py/nlr.h"
pythontech 0:5868e8752d44 32 #include "py/objtuple.h"
pythontech 0:5868e8752d44 33 #include "py/objfun.h"
pythontech 0:5868e8752d44 34 #include "py/runtime0.h"
pythontech 0:5868e8752d44 35 #include "py/runtime.h"
pythontech 0:5868e8752d44 36 #include "py/bc.h"
pythontech 0:5868e8752d44 37 #include "py/stackctrl.h"
pythontech 0:5868e8752d44 38
pythontech 0:5868e8752d44 39 #if 0 // print debugging info
pythontech 0:5868e8752d44 40 #define DEBUG_PRINT (1)
pythontech 0:5868e8752d44 41 #else // don't print debugging info
pythontech 0:5868e8752d44 42 #define DEBUG_PRINT (0)
pythontech 0:5868e8752d44 43 #define DEBUG_printf(...) (void)0
pythontech 0:5868e8752d44 44 #endif
pythontech 0:5868e8752d44 45
pythontech 0:5868e8752d44 46 // Note: the "name" entry in mp_obj_type_t for a function type must be
pythontech 0:5868e8752d44 47 // MP_QSTR_function because it is used to determine if an object is of generic
pythontech 0:5868e8752d44 48 // function type.
pythontech 0:5868e8752d44 49
pythontech 0:5868e8752d44 50 /******************************************************************************/
pythontech 0:5868e8752d44 51 /* builtin functions */
pythontech 0:5868e8752d44 52
pythontech 0:5868e8752d44 53 // mp_obj_fun_builtin_t defined in obj.h
pythontech 0:5868e8752d44 54
pythontech 0:5868e8752d44 55 STATIC mp_obj_t fun_builtin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
pythontech 0:5868e8752d44 56 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin));
pythontech 0:5868e8752d44 57 mp_obj_fun_builtin_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 58
pythontech 0:5868e8752d44 59 // check number of arguments
pythontech 0:5868e8752d44 60 mp_arg_check_num(n_args, n_kw, self->n_args_min, self->n_args_max, self->is_kw);
pythontech 0:5868e8752d44 61
pythontech 0:5868e8752d44 62 if (self->is_kw) {
pythontech 0:5868e8752d44 63 // function allows keywords
pythontech 0:5868e8752d44 64
pythontech 0:5868e8752d44 65 // we create a map directly from the given args array
pythontech 0:5868e8752d44 66 mp_map_t kw_args;
pythontech 0:5868e8752d44 67 mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
pythontech 0:5868e8752d44 68
pythontech 0:5868e8752d44 69 return self->fun.kw(n_args, args, &kw_args);
pythontech 0:5868e8752d44 70
pythontech 0:5868e8752d44 71 } else if (self->n_args_min <= 3 && self->n_args_min == self->n_args_max) {
pythontech 0:5868e8752d44 72 // function requires a fixed number of arguments
pythontech 0:5868e8752d44 73
pythontech 0:5868e8752d44 74 // dispatch function call
pythontech 0:5868e8752d44 75 switch (self->n_args_min) {
pythontech 0:5868e8752d44 76 case 0:
pythontech 0:5868e8752d44 77 return self->fun._0();
pythontech 0:5868e8752d44 78
pythontech 0:5868e8752d44 79 case 1:
pythontech 0:5868e8752d44 80 return self->fun._1(args[0]);
pythontech 0:5868e8752d44 81
pythontech 0:5868e8752d44 82 case 2:
pythontech 0:5868e8752d44 83 return self->fun._2(args[0], args[1]);
pythontech 0:5868e8752d44 84
pythontech 0:5868e8752d44 85 case 3:
pythontech 0:5868e8752d44 86 default:
pythontech 0:5868e8752d44 87 return self->fun._3(args[0], args[1], args[2]);
pythontech 0:5868e8752d44 88 }
pythontech 0:5868e8752d44 89
pythontech 0:5868e8752d44 90 } else {
pythontech 0:5868e8752d44 91 // function takes a variable number of arguments, but no keywords
pythontech 0:5868e8752d44 92
pythontech 0:5868e8752d44 93 return self->fun.var(n_args, args);
pythontech 0:5868e8752d44 94 }
pythontech 0:5868e8752d44 95 }
pythontech 0:5868e8752d44 96
pythontech 0:5868e8752d44 97 const mp_obj_type_t mp_type_fun_builtin = {
pythontech 0:5868e8752d44 98 { &mp_type_type },
pythontech 0:5868e8752d44 99 .name = MP_QSTR_function,
pythontech 0:5868e8752d44 100 .call = fun_builtin_call,
pythontech 0:5868e8752d44 101 .unary_op = mp_generic_unary_op,
pythontech 0:5868e8752d44 102 };
pythontech 0:5868e8752d44 103
pythontech 0:5868e8752d44 104 /******************************************************************************/
pythontech 0:5868e8752d44 105 /* byte code functions */
pythontech 0:5868e8752d44 106
pythontech 0:5868e8752d44 107 qstr mp_obj_code_get_name(const byte *code_info) {
pythontech 0:5868e8752d44 108 mp_decode_uint(&code_info); // skip code_info_size entry
pythontech 0:5868e8752d44 109 #if MICROPY_PERSISTENT_CODE
pythontech 0:5868e8752d44 110 return code_info[0] | (code_info[1] << 8);
pythontech 0:5868e8752d44 111 #else
pythontech 0:5868e8752d44 112 return mp_decode_uint(&code_info);
pythontech 0:5868e8752d44 113 #endif
pythontech 0:5868e8752d44 114 }
pythontech 0:5868e8752d44 115
pythontech 0:5868e8752d44 116 #if MICROPY_EMIT_NATIVE
pythontech 0:5868e8752d44 117 STATIC const mp_obj_type_t mp_type_fun_native;
pythontech 0:5868e8752d44 118 #endif
pythontech 0:5868e8752d44 119
pythontech 0:5868e8752d44 120 qstr mp_obj_fun_get_name(mp_const_obj_t fun_in) {
pythontech 0:5868e8752d44 121 const mp_obj_fun_bc_t *fun = MP_OBJ_TO_PTR(fun_in);
pythontech 0:5868e8752d44 122 #if MICROPY_EMIT_NATIVE
pythontech 0:5868e8752d44 123 if (fun->base.type == &mp_type_fun_native) {
pythontech 0:5868e8752d44 124 // TODO native functions don't have name stored
pythontech 0:5868e8752d44 125 return MP_QSTR_;
pythontech 0:5868e8752d44 126 }
pythontech 0:5868e8752d44 127 #endif
pythontech 0:5868e8752d44 128
pythontech 0:5868e8752d44 129 const byte *bc = fun->bytecode;
pythontech 0:5868e8752d44 130 mp_decode_uint(&bc); // skip n_state
pythontech 0:5868e8752d44 131 mp_decode_uint(&bc); // skip n_exc_stack
pythontech 0:5868e8752d44 132 bc++; // skip scope_params
pythontech 0:5868e8752d44 133 bc++; // skip n_pos_args
pythontech 0:5868e8752d44 134 bc++; // skip n_kwonly_args
pythontech 0:5868e8752d44 135 bc++; // skip n_def_pos_args
pythontech 0:5868e8752d44 136 return mp_obj_code_get_name(bc);
pythontech 0:5868e8752d44 137 }
pythontech 0:5868e8752d44 138
pythontech 0:5868e8752d44 139 #if MICROPY_CPYTHON_COMPAT
pythontech 0:5868e8752d44 140 STATIC void fun_bc_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
pythontech 0:5868e8752d44 141 (void)kind;
pythontech 0:5868e8752d44 142 mp_obj_fun_bc_t *o = MP_OBJ_TO_PTR(o_in);
pythontech 0:5868e8752d44 143 mp_printf(print, "<function %q at 0x%p>", mp_obj_fun_get_name(o_in), o);
pythontech 0:5868e8752d44 144 }
pythontech 0:5868e8752d44 145 #endif
pythontech 0:5868e8752d44 146
pythontech 0:5868e8752d44 147 #if DEBUG_PRINT
pythontech 0:5868e8752d44 148 STATIC void dump_args(const mp_obj_t *a, mp_uint_t sz) {
pythontech 0:5868e8752d44 149 DEBUG_printf("%p: ", a);
pythontech 0:5868e8752d44 150 for (mp_uint_t i = 0; i < sz; i++) {
pythontech 0:5868e8752d44 151 DEBUG_printf("%p ", a[i]);
pythontech 0:5868e8752d44 152 }
pythontech 0:5868e8752d44 153 DEBUG_printf("\n");
pythontech 0:5868e8752d44 154 }
pythontech 0:5868e8752d44 155 #else
pythontech 0:5868e8752d44 156 #define dump_args(...) (void)0
pythontech 0:5868e8752d44 157 #endif
pythontech 0:5868e8752d44 158
pythontech 0:5868e8752d44 159 // With this macro you can tune the maximum number of function state bytes
pythontech 0:5868e8752d44 160 // that will be allocated on the stack. Any function that needs more
pythontech 0:5868e8752d44 161 // than this will try to use the heap, with fallback to stack allocation.
pythontech 0:5868e8752d44 162 #define VM_MAX_STATE_ON_STACK (11 * sizeof(mp_uint_t))
pythontech 0:5868e8752d44 163
pythontech 0:5868e8752d44 164 // Set this to enable a simple stack overflow check.
pythontech 0:5868e8752d44 165 #define VM_DETECT_STACK_OVERFLOW (0)
pythontech 0:5868e8752d44 166
pythontech 0:5868e8752d44 167 #if MICROPY_STACKLESS
pythontech 0:5868e8752d44 168 mp_code_state *mp_obj_fun_bc_prepare_codestate(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
pythontech 0:5868e8752d44 169 MP_STACK_CHECK();
pythontech 0:5868e8752d44 170 mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 171
pythontech 0:5868e8752d44 172 // get start of bytecode
pythontech 0:5868e8752d44 173 const byte *ip = self->bytecode;
pythontech 0:5868e8752d44 174
pythontech 0:5868e8752d44 175 // bytecode prelude: state size and exception stack size
pythontech 0:5868e8752d44 176 size_t n_state = mp_decode_uint(&ip);
pythontech 0:5868e8752d44 177 size_t n_exc_stack = mp_decode_uint(&ip);
pythontech 0:5868e8752d44 178
pythontech 0:5868e8752d44 179 // allocate state for locals and stack
pythontech 0:5868e8752d44 180 size_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
pythontech 0:5868e8752d44 181 mp_code_state *code_state;
pythontech 0:5868e8752d44 182 code_state = m_new_obj_var_maybe(mp_code_state, byte, state_size);
pythontech 0:5868e8752d44 183 if (!code_state) {
pythontech 0:5868e8752d44 184 return NULL;
pythontech 0:5868e8752d44 185 }
pythontech 0:5868e8752d44 186
pythontech 0:5868e8752d44 187 code_state->ip = (byte*)(ip - self->bytecode); // offset to after n_state/n_exc_stack
pythontech 0:5868e8752d44 188 code_state->n_state = n_state;
pythontech 0:5868e8752d44 189 mp_setup_code_state(code_state, self, n_args, n_kw, args);
pythontech 0:5868e8752d44 190
pythontech 0:5868e8752d44 191 // execute the byte code with the correct globals context
pythontech 0:5868e8752d44 192 code_state->old_globals = mp_globals_get();
pythontech 0:5868e8752d44 193 mp_globals_set(self->globals);
pythontech 0:5868e8752d44 194
pythontech 0:5868e8752d44 195 return code_state;
pythontech 0:5868e8752d44 196 }
pythontech 0:5868e8752d44 197 #endif
pythontech 0:5868e8752d44 198
pythontech 0:5868e8752d44 199 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) {
pythontech 0:5868e8752d44 200 MP_STACK_CHECK();
pythontech 0:5868e8752d44 201
pythontech 0:5868e8752d44 202 DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
pythontech 0:5868e8752d44 203 DEBUG_printf("Input pos args: ");
pythontech 0:5868e8752d44 204 dump_args(args, n_args);
pythontech 0:5868e8752d44 205 DEBUG_printf("Input kw args: ");
pythontech 0:5868e8752d44 206 dump_args(args + n_args, n_kw * 2);
pythontech 0:5868e8752d44 207 mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 208 DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
pythontech 0:5868e8752d44 209
pythontech 0:5868e8752d44 210 // get start of bytecode
pythontech 0:5868e8752d44 211 const byte *ip = self->bytecode;
pythontech 0:5868e8752d44 212
pythontech 0:5868e8752d44 213 // bytecode prelude: state size and exception stack size
pythontech 0:5868e8752d44 214 mp_uint_t n_state = mp_decode_uint(&ip);
pythontech 0:5868e8752d44 215 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
pythontech 0:5868e8752d44 216
pythontech 0:5868e8752d44 217 #if VM_DETECT_STACK_OVERFLOW
pythontech 0:5868e8752d44 218 n_state += 1;
pythontech 0:5868e8752d44 219 #endif
pythontech 0:5868e8752d44 220
pythontech 0:5868e8752d44 221 // allocate state for locals and stack
pythontech 0:5868e8752d44 222 mp_uint_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
pythontech 0:5868e8752d44 223 mp_code_state *code_state = NULL;
pythontech 0:5868e8752d44 224 if (state_size > VM_MAX_STATE_ON_STACK) {
pythontech 0:5868e8752d44 225 code_state = m_new_obj_var_maybe(mp_code_state, byte, state_size);
pythontech 0:5868e8752d44 226 }
pythontech 0:5868e8752d44 227 if (code_state == NULL) {
pythontech 0:5868e8752d44 228 code_state = alloca(sizeof(mp_code_state) + state_size);
pythontech 0:5868e8752d44 229 state_size = 0; // indicate that we allocated using alloca
pythontech 0:5868e8752d44 230 }
pythontech 0:5868e8752d44 231
pythontech 0:5868e8752d44 232 code_state->ip = (byte*)(ip - self->bytecode); // offset to after n_state/n_exc_stack
pythontech 0:5868e8752d44 233 code_state->n_state = n_state;
pythontech 0:5868e8752d44 234 mp_setup_code_state(code_state, self, n_args, n_kw, args);
pythontech 0:5868e8752d44 235
pythontech 0:5868e8752d44 236 // execute the byte code with the correct globals context
pythontech 0:5868e8752d44 237 code_state->old_globals = mp_globals_get();
pythontech 0:5868e8752d44 238 mp_globals_set(self->globals);
pythontech 0:5868e8752d44 239 mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
pythontech 0:5868e8752d44 240 mp_globals_set(code_state->old_globals);
pythontech 0:5868e8752d44 241
pythontech 0:5868e8752d44 242 #if VM_DETECT_STACK_OVERFLOW
pythontech 0:5868e8752d44 243 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
pythontech 0:5868e8752d44 244 if (code_state->sp < code_state->state) {
pythontech 0:5868e8752d44 245 printf("VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
pythontech 0:5868e8752d44 246 assert(0);
pythontech 0:5868e8752d44 247 }
pythontech 0:5868e8752d44 248 }
pythontech 0:5868e8752d44 249 // We can't check the case when an exception is returned in state[n_state - 1]
pythontech 0:5868e8752d44 250 // and there are no arguments, because in this case our detection slot may have
pythontech 0:5868e8752d44 251 // been overwritten by the returned exception (which is allowed).
pythontech 0:5868e8752d44 252 if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && self->n_pos_args + self->n_kwonly_args == 0)) {
pythontech 0:5868e8752d44 253 // Just check to see that we have at least 1 null object left in the state.
pythontech 0:5868e8752d44 254 bool overflow = true;
pythontech 0:5868e8752d44 255 for (mp_uint_t i = 0; i < n_state - self->n_pos_args - self->n_kwonly_args; i++) {
pythontech 0:5868e8752d44 256 if (code_state->state[i] == MP_OBJ_NULL) {
pythontech 0:5868e8752d44 257 overflow = false;
pythontech 0:5868e8752d44 258 break;
pythontech 0:5868e8752d44 259 }
pythontech 0:5868e8752d44 260 }
pythontech 0:5868e8752d44 261 if (overflow) {
pythontech 0:5868e8752d44 262 printf("VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
pythontech 0:5868e8752d44 263 assert(0);
pythontech 0:5868e8752d44 264 }
pythontech 0:5868e8752d44 265 }
pythontech 0:5868e8752d44 266 #endif
pythontech 0:5868e8752d44 267
pythontech 0:5868e8752d44 268 mp_obj_t result;
pythontech 0:5868e8752d44 269 switch (vm_return_kind) {
pythontech 0:5868e8752d44 270 case MP_VM_RETURN_NORMAL:
pythontech 0:5868e8752d44 271 // return value is in *sp
pythontech 0:5868e8752d44 272 result = *code_state->sp;
pythontech 0:5868e8752d44 273 break;
pythontech 0:5868e8752d44 274
pythontech 0:5868e8752d44 275 case MP_VM_RETURN_EXCEPTION:
pythontech 0:5868e8752d44 276 // return value is in state[n_state - 1]
pythontech 0:5868e8752d44 277 result = code_state->state[n_state - 1];
pythontech 0:5868e8752d44 278 break;
pythontech 0:5868e8752d44 279
pythontech 0:5868e8752d44 280 case MP_VM_RETURN_YIELD: // byte-code shouldn't yield
pythontech 0:5868e8752d44 281 default:
pythontech 0:5868e8752d44 282 assert(0);
pythontech 0:5868e8752d44 283 result = mp_const_none;
pythontech 0:5868e8752d44 284 vm_return_kind = MP_VM_RETURN_NORMAL;
pythontech 0:5868e8752d44 285 break;
pythontech 0:5868e8752d44 286 }
pythontech 0:5868e8752d44 287
pythontech 0:5868e8752d44 288 // free the state if it was allocated on the heap
pythontech 0:5868e8752d44 289 if (state_size != 0) {
pythontech 0:5868e8752d44 290 m_del_var(mp_code_state, byte, state_size, code_state);
pythontech 0:5868e8752d44 291 }
pythontech 0:5868e8752d44 292
pythontech 0:5868e8752d44 293 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
pythontech 0:5868e8752d44 294 return result;
pythontech 0:5868e8752d44 295 } else { // MP_VM_RETURN_EXCEPTION
pythontech 0:5868e8752d44 296 nlr_raise(result);
pythontech 0:5868e8752d44 297 }
pythontech 0:5868e8752d44 298 }
pythontech 0:5868e8752d44 299
pythontech 0:5868e8752d44 300 #if MICROPY_PY_FUNCTION_ATTRS
pythontech 0:5868e8752d44 301 STATIC void fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
pythontech 0:5868e8752d44 302 if (dest[0] != MP_OBJ_NULL) {
pythontech 0:5868e8752d44 303 // not load attribute
pythontech 0:5868e8752d44 304 return;
pythontech 0:5868e8752d44 305 }
pythontech 0:5868e8752d44 306 if (attr == MP_QSTR___name__) {
pythontech 0:5868e8752d44 307 dest[0] = MP_OBJ_NEW_QSTR(mp_obj_fun_get_name(self_in));
pythontech 0:5868e8752d44 308 }
pythontech 0:5868e8752d44 309 }
pythontech 0:5868e8752d44 310 #endif
pythontech 0:5868e8752d44 311
pythontech 0:5868e8752d44 312 const mp_obj_type_t mp_type_fun_bc = {
pythontech 0:5868e8752d44 313 { &mp_type_type },
pythontech 0:5868e8752d44 314 .name = MP_QSTR_function,
pythontech 0:5868e8752d44 315 #if MICROPY_CPYTHON_COMPAT
pythontech 0:5868e8752d44 316 .print = fun_bc_print,
pythontech 0:5868e8752d44 317 #endif
pythontech 0:5868e8752d44 318 .call = fun_bc_call,
pythontech 0:5868e8752d44 319 .unary_op = mp_generic_unary_op,
pythontech 0:5868e8752d44 320 #if MICROPY_PY_FUNCTION_ATTRS
pythontech 0:5868e8752d44 321 .attr = fun_bc_attr,
pythontech 0:5868e8752d44 322 #endif
pythontech 0:5868e8752d44 323 };
pythontech 0:5868e8752d44 324
pythontech 0:5868e8752d44 325 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) {
pythontech 0:5868e8752d44 326 mp_uint_t n_def_args = 0;
pythontech 0:5868e8752d44 327 mp_uint_t n_extra_args = 0;
pythontech 0:5868e8752d44 328 mp_obj_tuple_t *def_args = MP_OBJ_TO_PTR(def_args_in);
pythontech 0:5868e8752d44 329 if (def_args_in != MP_OBJ_NULL) {
pythontech 0:5868e8752d44 330 assert(MP_OBJ_IS_TYPE(def_args_in, &mp_type_tuple));
pythontech 0:5868e8752d44 331 n_def_args = def_args->len;
pythontech 0:5868e8752d44 332 n_extra_args = def_args->len;
pythontech 0:5868e8752d44 333 }
pythontech 0:5868e8752d44 334 if (def_kw_args != MP_OBJ_NULL) {
pythontech 0:5868e8752d44 335 n_extra_args += 1;
pythontech 0:5868e8752d44 336 }
pythontech 0:5868e8752d44 337 mp_obj_fun_bc_t *o = m_new_obj_var(mp_obj_fun_bc_t, mp_obj_t, n_extra_args);
pythontech 0:5868e8752d44 338 o->base.type = &mp_type_fun_bc;
pythontech 0:5868e8752d44 339 o->globals = mp_globals_get();
pythontech 0:5868e8752d44 340 o->bytecode = code;
pythontech 0:5868e8752d44 341 o->const_table = const_table;
pythontech 0:5868e8752d44 342 if (def_args != NULL) {
pythontech 0:5868e8752d44 343 memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
pythontech 0:5868e8752d44 344 }
pythontech 0:5868e8752d44 345 if (def_kw_args != MP_OBJ_NULL) {
pythontech 0:5868e8752d44 346 o->extra_args[n_def_args] = def_kw_args;
pythontech 0:5868e8752d44 347 }
pythontech 0:5868e8752d44 348 return MP_OBJ_FROM_PTR(o);
pythontech 0:5868e8752d44 349 }
pythontech 0:5868e8752d44 350
pythontech 0:5868e8752d44 351 /******************************************************************************/
pythontech 0:5868e8752d44 352 /* native functions */
pythontech 0:5868e8752d44 353
pythontech 0:5868e8752d44 354 #if MICROPY_EMIT_NATIVE
pythontech 0:5868e8752d44 355
pythontech 0:5868e8752d44 356 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) {
pythontech 0:5868e8752d44 357 MP_STACK_CHECK();
pythontech 0:5868e8752d44 358 mp_obj_fun_bc_t *self = self_in;
pythontech 0:5868e8752d44 359 mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void*)self->bytecode);
pythontech 0:5868e8752d44 360 return fun(self_in, n_args, n_kw, args);
pythontech 0:5868e8752d44 361 }
pythontech 0:5868e8752d44 362
pythontech 0:5868e8752d44 363 STATIC const mp_obj_type_t mp_type_fun_native = {
pythontech 0:5868e8752d44 364 { &mp_type_type },
pythontech 0:5868e8752d44 365 .name = MP_QSTR_function,
pythontech 0:5868e8752d44 366 .call = fun_native_call,
pythontech 0:5868e8752d44 367 .unary_op = mp_generic_unary_op,
pythontech 0:5868e8752d44 368 };
pythontech 0:5868e8752d44 369
pythontech 0:5868e8752d44 370 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) {
pythontech 0:5868e8752d44 371 mp_obj_fun_bc_t *o = mp_obj_new_fun_bc(def_args_in, def_kw_args, (const byte*)fun_data, const_table);
pythontech 0:5868e8752d44 372 o->base.type = &mp_type_fun_native;
pythontech 0:5868e8752d44 373 return o;
pythontech 0:5868e8752d44 374 }
pythontech 0:5868e8752d44 375
pythontech 0:5868e8752d44 376 #endif // MICROPY_EMIT_NATIVE
pythontech 0:5868e8752d44 377
pythontech 0:5868e8752d44 378 /******************************************************************************/
pythontech 0:5868e8752d44 379 /* viper functions */
pythontech 0:5868e8752d44 380
pythontech 0:5868e8752d44 381 #if MICROPY_EMIT_NATIVE
pythontech 0:5868e8752d44 382
pythontech 0:5868e8752d44 383 typedef struct _mp_obj_fun_viper_t {
pythontech 0:5868e8752d44 384 mp_obj_base_t base;
pythontech 0:5868e8752d44 385 mp_uint_t n_args;
pythontech 0:5868e8752d44 386 void *fun_data; // GC must be able to trace this pointer
pythontech 0:5868e8752d44 387 mp_uint_t type_sig;
pythontech 0:5868e8752d44 388 } mp_obj_fun_viper_t;
pythontech 0:5868e8752d44 389
pythontech 0:5868e8752d44 390 typedef mp_uint_t (*viper_fun_0_t)(void);
pythontech 0:5868e8752d44 391 typedef mp_uint_t (*viper_fun_1_t)(mp_uint_t);
pythontech 0:5868e8752d44 392 typedef mp_uint_t (*viper_fun_2_t)(mp_uint_t, mp_uint_t);
pythontech 0:5868e8752d44 393 typedef mp_uint_t (*viper_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
pythontech 0:5868e8752d44 394 typedef mp_uint_t (*viper_fun_4_t)(mp_uint_t, mp_uint_t, mp_uint_t, mp_uint_t);
pythontech 0:5868e8752d44 395
pythontech 0:5868e8752d44 396 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) {
pythontech 0:5868e8752d44 397 mp_obj_fun_viper_t *self = self_in;
pythontech 0:5868e8752d44 398
pythontech 0:5868e8752d44 399 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
pythontech 0:5868e8752d44 400
pythontech 0:5868e8752d44 401 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
pythontech 0:5868e8752d44 402
pythontech 0:5868e8752d44 403 mp_uint_t ret;
pythontech 0:5868e8752d44 404 if (n_args == 0) {
pythontech 0:5868e8752d44 405 ret = ((viper_fun_0_t)fun)();
pythontech 0:5868e8752d44 406 } else if (n_args == 1) {
pythontech 0:5868e8752d44 407 ret = ((viper_fun_1_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 4));
pythontech 0:5868e8752d44 408 } else if (n_args == 2) {
pythontech 0:5868e8752d44 409 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));
pythontech 0:5868e8752d44 410 } else if (n_args == 3) {
pythontech 0:5868e8752d44 411 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));
pythontech 0:5868e8752d44 412 } else if (n_args == 4) {
pythontech 0:5868e8752d44 413 ret = ((viper_fun_4_t)fun)(
pythontech 0:5868e8752d44 414 mp_convert_obj_to_native(args[0], self->type_sig >> 4),
pythontech 0:5868e8752d44 415 mp_convert_obj_to_native(args[1], self->type_sig >> 8),
pythontech 0:5868e8752d44 416 mp_convert_obj_to_native(args[2], self->type_sig >> 12),
pythontech 0:5868e8752d44 417 mp_convert_obj_to_native(args[3], self->type_sig >> 16)
pythontech 0:5868e8752d44 418 );
pythontech 0:5868e8752d44 419 } else {
pythontech 0:5868e8752d44 420 // TODO 5 or more arguments not supported for viper call
pythontech 0:5868e8752d44 421 assert(0);
pythontech 0:5868e8752d44 422 ret = 0;
pythontech 0:5868e8752d44 423 }
pythontech 0:5868e8752d44 424
pythontech 0:5868e8752d44 425 return mp_convert_native_to_obj(ret, self->type_sig);
pythontech 0:5868e8752d44 426 }
pythontech 0:5868e8752d44 427
pythontech 0:5868e8752d44 428 STATIC const mp_obj_type_t mp_type_fun_viper = {
pythontech 0:5868e8752d44 429 { &mp_type_type },
pythontech 0:5868e8752d44 430 .name = MP_QSTR_function,
pythontech 0:5868e8752d44 431 .call = fun_viper_call,
pythontech 0:5868e8752d44 432 .unary_op = mp_generic_unary_op,
pythontech 0:5868e8752d44 433 };
pythontech 0:5868e8752d44 434
pythontech 0:5868e8752d44 435 mp_obj_t mp_obj_new_fun_viper(mp_uint_t n_args, void *fun_data, mp_uint_t type_sig) {
pythontech 0:5868e8752d44 436 mp_obj_fun_viper_t *o = m_new_obj(mp_obj_fun_viper_t);
pythontech 0:5868e8752d44 437 o->base.type = &mp_type_fun_viper;
pythontech 0:5868e8752d44 438 o->n_args = n_args;
pythontech 0:5868e8752d44 439 o->fun_data = fun_data;
pythontech 0:5868e8752d44 440 o->type_sig = type_sig;
pythontech 0:5868e8752d44 441 return o;
pythontech 0:5868e8752d44 442 }
pythontech 0:5868e8752d44 443
pythontech 0:5868e8752d44 444 #endif // MICROPY_EMIT_NATIVE
pythontech 0:5868e8752d44 445
pythontech 0:5868e8752d44 446 /******************************************************************************/
pythontech 0:5868e8752d44 447 /* inline assembler functions */
pythontech 0:5868e8752d44 448
pythontech 0:5868e8752d44 449 #if MICROPY_EMIT_INLINE_THUMB
pythontech 0:5868e8752d44 450
pythontech 0:5868e8752d44 451 typedef struct _mp_obj_fun_asm_t {
pythontech 0:5868e8752d44 452 mp_obj_base_t base;
pythontech 0:5868e8752d44 453 mp_uint_t n_args;
pythontech 0:5868e8752d44 454 void *fun_data; // GC must be able to trace this pointer
pythontech 0:5868e8752d44 455 mp_uint_t type_sig;
pythontech 0:5868e8752d44 456 } mp_obj_fun_asm_t;
pythontech 0:5868e8752d44 457
pythontech 0:5868e8752d44 458 typedef mp_uint_t (*inline_asm_fun_0_t)(void);
pythontech 0:5868e8752d44 459 typedef mp_uint_t (*inline_asm_fun_1_t)(mp_uint_t);
pythontech 0:5868e8752d44 460 typedef mp_uint_t (*inline_asm_fun_2_t)(mp_uint_t, mp_uint_t);
pythontech 0:5868e8752d44 461 typedef mp_uint_t (*inline_asm_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
pythontech 0:5868e8752d44 462 typedef mp_uint_t (*inline_asm_fun_4_t)(mp_uint_t, mp_uint_t, mp_uint_t, mp_uint_t);
pythontech 0:5868e8752d44 463
pythontech 0:5868e8752d44 464 // convert a Micro Python object to a sensible value for inline asm
pythontech 0:5868e8752d44 465 STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
pythontech 0:5868e8752d44 466 // TODO for byte_array, pass pointer to the array
pythontech 0:5868e8752d44 467 if (MP_OBJ_IS_SMALL_INT(obj)) {
pythontech 0:5868e8752d44 468 return MP_OBJ_SMALL_INT_VALUE(obj);
pythontech 0:5868e8752d44 469 } else if (obj == mp_const_none) {
pythontech 0:5868e8752d44 470 return 0;
pythontech 0:5868e8752d44 471 } else if (obj == mp_const_false) {
pythontech 0:5868e8752d44 472 return 0;
pythontech 0:5868e8752d44 473 } else if (obj == mp_const_true) {
pythontech 0:5868e8752d44 474 return 1;
pythontech 0:5868e8752d44 475 } else if (MP_OBJ_IS_TYPE(obj, &mp_type_int)) {
pythontech 0:5868e8752d44 476 return mp_obj_int_get_truncated(obj);
pythontech 0:5868e8752d44 477 } else if (MP_OBJ_IS_STR(obj)) {
pythontech 0:5868e8752d44 478 // pointer to the string (it's probably constant though!)
pythontech 0:5868e8752d44 479 mp_uint_t l;
pythontech 0:5868e8752d44 480 return (mp_uint_t)mp_obj_str_get_data(obj, &l);
pythontech 0:5868e8752d44 481 } else {
pythontech 0:5868e8752d44 482 mp_obj_type_t *type = mp_obj_get_type(obj);
pythontech 0:5868e8752d44 483 if (0) {
pythontech 0:5868e8752d44 484 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 485 } else if (type == &mp_type_float) {
pythontech 0:5868e8752d44 486 // convert float to int (could also pass in float registers)
pythontech 0:5868e8752d44 487 return (mp_int_t)mp_obj_float_get(obj);
pythontech 0:5868e8752d44 488 #endif
pythontech 0:5868e8752d44 489 } else if (type == &mp_type_tuple) {
pythontech 0:5868e8752d44 490 // pointer to start of tuple (could pass length, but then could use len(x) for that)
pythontech 0:5868e8752d44 491 mp_uint_t len;
pythontech 0:5868e8752d44 492 mp_obj_t *items;
pythontech 0:5868e8752d44 493 mp_obj_tuple_get(obj, &len, &items);
pythontech 0:5868e8752d44 494 return (mp_uint_t)items;
pythontech 0:5868e8752d44 495 } else if (type == &mp_type_list) {
pythontech 0:5868e8752d44 496 // pointer to start of list (could pass length, but then could use len(x) for that)
pythontech 0:5868e8752d44 497 mp_uint_t len;
pythontech 0:5868e8752d44 498 mp_obj_t *items;
pythontech 0:5868e8752d44 499 mp_obj_list_get(obj, &len, &items);
pythontech 0:5868e8752d44 500 return (mp_uint_t)items;
pythontech 0:5868e8752d44 501 } else {
pythontech 0:5868e8752d44 502 mp_buffer_info_t bufinfo;
pythontech 0:5868e8752d44 503 if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
pythontech 0:5868e8752d44 504 // supports the buffer protocol, return a pointer to the data
pythontech 0:5868e8752d44 505 return (mp_uint_t)bufinfo.buf;
pythontech 0:5868e8752d44 506 } else {
pythontech 0:5868e8752d44 507 // just pass along a pointer to the object
pythontech 0:5868e8752d44 508 return (mp_uint_t)obj;
pythontech 0:5868e8752d44 509 }
pythontech 0:5868e8752d44 510 }
pythontech 0:5868e8752d44 511 }
pythontech 0:5868e8752d44 512 }
pythontech 0:5868e8752d44 513
pythontech 0:5868e8752d44 514 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) {
pythontech 0:5868e8752d44 515 mp_obj_fun_asm_t *self = self_in;
pythontech 0:5868e8752d44 516
pythontech 0:5868e8752d44 517 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
pythontech 0:5868e8752d44 518
pythontech 0:5868e8752d44 519 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
pythontech 0:5868e8752d44 520
pythontech 0:5868e8752d44 521 mp_uint_t ret;
pythontech 0:5868e8752d44 522 if (n_args == 0) {
pythontech 0:5868e8752d44 523 ret = ((inline_asm_fun_0_t)fun)();
pythontech 0:5868e8752d44 524 } else if (n_args == 1) {
pythontech 0:5868e8752d44 525 ret = ((inline_asm_fun_1_t)fun)(convert_obj_for_inline_asm(args[0]));
pythontech 0:5868e8752d44 526 } else if (n_args == 2) {
pythontech 0:5868e8752d44 527 ret = ((inline_asm_fun_2_t)fun)(convert_obj_for_inline_asm(args[0]), convert_obj_for_inline_asm(args[1]));
pythontech 0:5868e8752d44 528 } else if (n_args == 3) {
pythontech 0:5868e8752d44 529 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]));
pythontech 0:5868e8752d44 530 } else {
pythontech 0:5868e8752d44 531 // compiler allows at most 4 arguments
pythontech 0:5868e8752d44 532 assert(n_args == 4);
pythontech 0:5868e8752d44 533 ret = ((inline_asm_fun_4_t)fun)(
pythontech 0:5868e8752d44 534 convert_obj_for_inline_asm(args[0]),
pythontech 0:5868e8752d44 535 convert_obj_for_inline_asm(args[1]),
pythontech 0:5868e8752d44 536 convert_obj_for_inline_asm(args[2]),
pythontech 0:5868e8752d44 537 convert_obj_for_inline_asm(args[3])
pythontech 0:5868e8752d44 538 );
pythontech 0:5868e8752d44 539 }
pythontech 0:5868e8752d44 540
pythontech 0:5868e8752d44 541 return mp_convert_native_to_obj(ret, self->type_sig);
pythontech 0:5868e8752d44 542 }
pythontech 0:5868e8752d44 543
pythontech 0:5868e8752d44 544 STATIC const mp_obj_type_t mp_type_fun_asm = {
pythontech 0:5868e8752d44 545 { &mp_type_type },
pythontech 0:5868e8752d44 546 .name = MP_QSTR_function,
pythontech 0:5868e8752d44 547 .call = fun_asm_call,
pythontech 0:5868e8752d44 548 .unary_op = mp_generic_unary_op,
pythontech 0:5868e8752d44 549 };
pythontech 0:5868e8752d44 550
pythontech 0:5868e8752d44 551 mp_obj_t mp_obj_new_fun_asm(mp_uint_t n_args, void *fun_data, mp_uint_t type_sig) {
pythontech 0:5868e8752d44 552 mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
pythontech 0:5868e8752d44 553 o->base.type = &mp_type_fun_asm;
pythontech 0:5868e8752d44 554 o->n_args = n_args;
pythontech 0:5868e8752d44 555 o->fun_data = fun_data;
pythontech 0:5868e8752d44 556 o->type_sig = type_sig;
pythontech 0:5868e8752d44 557 return o;
pythontech 0:5868e8752d44 558 }
pythontech 0:5868e8752d44 559
pythontech 0:5868e8752d44 560 #endif // MICROPY_EMIT_INLINE_THUMB