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