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