Damien George | b534e1b | 2014-09-04 14:44:01 +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) 2014 Damien P. George |
| 7 | * Copyright (c) 2014 Paul Sokolovsky |
| 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 | |
| 28 | #include <stdbool.h> |
| 29 | #include <string.h> |
| 30 | #include <assert.h> |
| 31 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 32 | #include "py/nlr.h" |
| 33 | #include "py/objfun.h" |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 34 | #include "py/runtime0.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 35 | #include "py/bc.h" |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 36 | |
| 37 | #if 0 // print debugging info |
| 38 | #define DEBUG_PRINT (1) |
| 39 | #else // don't print debugging info |
Damien George | 7860c2a | 2014-11-05 21:16:41 +0000 | [diff] [blame] | 40 | #define DEBUG_PRINT (0) |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 41 | #define DEBUG_printf(...) (void)0 |
| 42 | #endif |
| 43 | |
| 44 | mp_uint_t mp_decode_uint(const byte **ptr) { |
| 45 | mp_uint_t unum = 0; |
| 46 | byte val; |
| 47 | const byte *p = *ptr; |
| 48 | do { |
| 49 | val = *p++; |
| 50 | unum = (unum << 7) | (val & 0x7f); |
| 51 | } while ((val & 0x80) != 0); |
| 52 | *ptr = p; |
| 53 | return unum; |
| 54 | } |
| 55 | |
| 56 | STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, mp_uint_t expected, mp_uint_t given) { |
| 57 | #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE |
Damien George | 7f23384 | 2015-01-01 15:33:50 +0000 | [diff] [blame] | 58 | // generic message, used also for other argument issues |
Damien George | 3a2171e | 2015-09-04 16:53:46 +0100 | [diff] [blame] | 59 | (void)f; |
| 60 | (void)expected; |
| 61 | (void)given; |
Damien George | 7f23384 | 2015-01-01 15:33:50 +0000 | [diff] [blame] | 62 | mp_arg_error_terse_mismatch(); |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 63 | #elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL |
Damien George | 3a2171e | 2015-09-04 16:53:46 +0100 | [diff] [blame] | 64 | (void)f; |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 65 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
| 66 | "function takes %d positional arguments but %d were given", expected, given)); |
| 67 | #elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED |
| 68 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
Damien George | 044c473 | 2015-04-11 13:03:37 +0100 | [diff] [blame] | 69 | "%q() takes %d positional arguments but %d were given", |
| 70 | mp_obj_fun_get_name(f), expected, given)); |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 71 | #endif |
| 72 | } |
| 73 | |
| 74 | #if DEBUG_PRINT |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 75 | STATIC void dump_args(const mp_obj_t *a, mp_uint_t sz) { |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 76 | DEBUG_printf("%p: ", a); |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 77 | for (mp_uint_t i = 0; i < sz; i++) { |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 78 | DEBUG_printf("%p ", a[i]); |
| 79 | } |
| 80 | DEBUG_printf("\n"); |
| 81 | } |
| 82 | #else |
| 83 | #define dump_args(...) (void)0 |
| 84 | #endif |
| 85 | |
Damien George | 9988618 | 2015-04-06 22:38:53 +0100 | [diff] [blame] | 86 | // On entry code_state should be allocated somewhere (stack/heap) and |
| 87 | // contain the following valid entries: |
Damien George | 9988618 | 2015-04-06 22:38:53 +0100 | [diff] [blame] | 88 | // - code_state->ip should contain the offset in bytes from the start of |
Damien George | 9b7f583 | 2015-03-18 17:47:47 +0000 | [diff] [blame] | 89 | // the bytecode chunk to just after n_state and n_exc_stack |
Damien George | 9988618 | 2015-04-06 22:38:53 +0100 | [diff] [blame] | 90 | // - code_state->n_state should be set to the state size (locals plus stack) |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 91 | void mp_setup_code_state(mp_code_state *code_state, mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { |
| 92 | // This function is pretty complicated. It's main aim is to be efficient in speed and RAM |
| 93 | // usage for the common case of positional only args. |
| 94 | mp_obj_fun_bc_t *self = self_in; |
| 95 | mp_uint_t n_state = code_state->n_state; |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 96 | |
Damien George | 9b7f583 | 2015-03-18 17:47:47 +0000 | [diff] [blame] | 97 | // ip comes in as an offset into bytecode, so turn it into a true pointer |
| 98 | code_state->ip = self->bytecode + (mp_uint_t)code_state->ip; |
| 99 | |
Damien George | 713ea18 | 2015-10-23 01:23:11 +0100 | [diff] [blame] | 100 | // store pointer to constant table |
| 101 | code_state->const_table = self->const_table; |
| 102 | |
Paul Sokolovsky | 2039757 | 2015-03-28 01:14:44 +0200 | [diff] [blame] | 103 | #if MICROPY_STACKLESS |
| 104 | code_state->prev = NULL; |
| 105 | #endif |
Damien George | 9b7f583 | 2015-03-18 17:47:47 +0000 | [diff] [blame] | 106 | |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 107 | // get params |
| 108 | mp_uint_t scope_flags = *code_state->ip++; |
| 109 | mp_uint_t n_pos_args = *code_state->ip++; |
| 110 | mp_uint_t n_kwonly_args = *code_state->ip++; |
| 111 | mp_uint_t n_def_pos_args = *code_state->ip++; |
| 112 | |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 113 | code_state->sp = &code_state->state[0] - 1; |
| 114 | code_state->exc_sp = (mp_exc_stack_t*)(code_state->state + n_state) - 1; |
| 115 | |
| 116 | // zero out the local stack to begin with |
| 117 | memset(code_state->state, 0, n_state * sizeof(*code_state->state)); |
| 118 | |
| 119 | const mp_obj_t *kwargs = args + n_args; |
| 120 | |
| 121 | // var_pos_kw_args points to the stack where the var-args tuple, and var-kw dict, should go (if they are needed) |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 122 | mp_obj_t *var_pos_kw_args = &code_state->state[n_state - 1 - n_pos_args - n_kwonly_args]; |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 123 | |
| 124 | // check positional arguments |
| 125 | |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 126 | if (n_args > n_pos_args) { |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 127 | // given more than enough arguments |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 128 | if ((scope_flags & MP_SCOPE_FLAG_VARARGS) == 0) { |
| 129 | fun_pos_args_mismatch(self, n_pos_args, n_args); |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 130 | } |
| 131 | // put extra arguments in varargs tuple |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 132 | *var_pos_kw_args-- = mp_obj_new_tuple(n_args - n_pos_args, args + n_pos_args); |
| 133 | n_args = n_pos_args; |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 134 | } else { |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 135 | if ((scope_flags & MP_SCOPE_FLAG_VARARGS) != 0) { |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 136 | DEBUG_printf("passing empty tuple as *args\n"); |
| 137 | *var_pos_kw_args-- = mp_const_empty_tuple; |
| 138 | } |
| 139 | // Apply processing and check below only if we don't have kwargs, |
| 140 | // otherwise, kw handling code below has own extensive checks. |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 141 | if (n_kw == 0 && (scope_flags & MP_SCOPE_FLAG_DEFKWARGS) == 0) { |
| 142 | if (n_args >= (mp_uint_t)(n_pos_args - n_def_pos_args)) { |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 143 | // given enough arguments, but may need to use some default arguments |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 144 | for (mp_uint_t i = n_args; i < n_pos_args; i++) { |
| 145 | code_state->state[n_state - 1 - i] = self->extra_args[i - (n_pos_args - n_def_pos_args)]; |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 146 | } |
| 147 | } else { |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 148 | fun_pos_args_mismatch(self, n_pos_args - n_def_pos_args, n_args); |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // copy positional args into state |
| 154 | for (mp_uint_t i = 0; i < n_args; i++) { |
| 155 | code_state->state[n_state - 1 - i] = args[i]; |
| 156 | } |
| 157 | |
| 158 | // check keyword arguments |
| 159 | |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 160 | if (n_kw != 0 || (scope_flags & MP_SCOPE_FLAG_DEFKWARGS) != 0) { |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 161 | DEBUG_printf("Initial args: "); |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 162 | dump_args(code_state->state + n_state - n_pos_args - n_kwonly_args, n_pos_args + n_kwonly_args); |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 163 | |
| 164 | mp_obj_t dict = MP_OBJ_NULL; |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 165 | if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) { |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 166 | dict = mp_obj_new_dict(n_kw); // TODO: better go conservative with 0? |
| 167 | *var_pos_kw_args = dict; |
| 168 | } |
| 169 | |
Damien George | 9b7f583 | 2015-03-18 17:47:47 +0000 | [diff] [blame] | 170 | // get pointer to arg_names array |
Damien George | 713ea18 | 2015-10-23 01:23:11 +0100 | [diff] [blame] | 171 | const mp_obj_t *arg_names = (const mp_obj_t*)code_state->const_table; |
Damien George | 1084b0f | 2014-10-25 15:07:02 +0100 | [diff] [blame] | 172 | |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 173 | for (mp_uint_t i = 0; i < n_kw; i++) { |
Damien George | 1084b0f | 2014-10-25 15:07:02 +0100 | [diff] [blame] | 174 | mp_obj_t wanted_arg_name = kwargs[2 * i]; |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 175 | for (mp_uint_t j = 0; j < n_pos_args + n_kwonly_args; j++) { |
Damien George | 1084b0f | 2014-10-25 15:07:02 +0100 | [diff] [blame] | 176 | if (wanted_arg_name == arg_names[j]) { |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 177 | if (code_state->state[n_state - 1 - j] != MP_OBJ_NULL) { |
| 178 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
Damien George | 044c473 | 2015-04-11 13:03:37 +0100 | [diff] [blame] | 179 | "function got multiple values for argument '%q'", MP_OBJ_QSTR_VALUE(wanted_arg_name))); |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 180 | } |
| 181 | code_state->state[n_state - 1 - j] = kwargs[2 * i + 1]; |
| 182 | goto continue2; |
| 183 | } |
| 184 | } |
| 185 | // Didn't find name match with positional args |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 186 | if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) == 0) { |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 187 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments")); |
| 188 | } |
| 189 | mp_obj_dict_store(dict, kwargs[2 * i], kwargs[2 * i + 1]); |
| 190 | continue2:; |
| 191 | } |
| 192 | |
| 193 | DEBUG_printf("Args with kws flattened: "); |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 194 | dump_args(code_state->state + n_state - n_pos_args - n_kwonly_args, n_pos_args + n_kwonly_args); |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 195 | |
| 196 | // fill in defaults for positional args |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 197 | mp_obj_t *d = &code_state->state[n_state - n_pos_args]; |
| 198 | mp_obj_t *s = &self->extra_args[n_def_pos_args - 1]; |
| 199 | for (mp_uint_t i = n_def_pos_args; i > 0; i--, d++, s--) { |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 200 | if (*d == MP_OBJ_NULL) { |
| 201 | *d = *s; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | DEBUG_printf("Args after filling default positional: "); |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 206 | dump_args(code_state->state + n_state - n_pos_args - n_kwonly_args, n_pos_args + n_kwonly_args); |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 207 | |
| 208 | // Check that all mandatory positional args are specified |
| 209 | while (d < &code_state->state[n_state]) { |
| 210 | if (*d++ == MP_OBJ_NULL) { |
| 211 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
| 212 | "function missing required positional argument #%d", &code_state->state[n_state] - d)); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Check that all mandatory keyword args are specified |
| 217 | // Fill in default kw args if we have them |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 218 | for (mp_uint_t i = 0; i < n_kwonly_args; i++) { |
| 219 | if (code_state->state[n_state - 1 - n_pos_args - i] == MP_OBJ_NULL) { |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 220 | mp_map_elem_t *elem = NULL; |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 221 | if ((scope_flags & MP_SCOPE_FLAG_DEFKWARGS) != 0) { |
| 222 | elem = mp_map_lookup(&((mp_obj_dict_t*)self->extra_args[n_def_pos_args])->map, arg_names[n_pos_args + i], MP_MAP_LOOKUP); |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 223 | } |
| 224 | if (elem != NULL) { |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 225 | code_state->state[n_state - 1 - n_pos_args - i] = elem->value; |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 226 | } else { |
| 227 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 228 | "function missing required keyword argument '%q'", MP_OBJ_QSTR_VALUE(arg_names[n_pos_args + i]))); |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | } else { |
| 234 | // no keyword arguments given |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 235 | if (n_kwonly_args != 0) { |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 236 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, |
| 237 | "function missing keyword-only argument")); |
| 238 | } |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 239 | if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) { |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 240 | *var_pos_kw_args = mp_obj_new_dict(0); |
| 241 | } |
| 242 | } |
| 243 | |
Damien George | 9b7f583 | 2015-03-18 17:47:47 +0000 | [diff] [blame] | 244 | // get the ip and skip argument names |
| 245 | const byte *ip = code_state->ip; |
Damien George | 9b7f583 | 2015-03-18 17:47:47 +0000 | [diff] [blame] | 246 | |
| 247 | // store pointer to code_info and jump over it |
| 248 | { |
| 249 | code_state->code_info = ip; |
| 250 | const byte *ip2 = ip; |
| 251 | mp_uint_t code_info_size = mp_decode_uint(&ip2); |
| 252 | ip += code_info_size; |
| 253 | } |
| 254 | |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 255 | // bytecode prelude: initialise closed over variables |
Damien George | c9aa188 | 2015-04-07 00:08:17 +0100 | [diff] [blame] | 256 | mp_uint_t local_num; |
| 257 | while ((local_num = *ip++) != 255) { |
| 258 | code_state->state[n_state - 1 - local_num] = |
| 259 | mp_obj_new_cell(code_state->state[n_state - 1 - local_num]); |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // now that we skipped over the prelude, set the ip for the VM |
| 263 | code_state->ip = ip; |
| 264 | |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 265 | DEBUG_printf("Calling: n_pos_args=%d, n_kwonly_args=%d\n", n_pos_args, n_kwonly_args); |
| 266 | dump_args(code_state->state + n_state - n_pos_args - n_kwonly_args, n_pos_args + n_kwonly_args); |
Damien George | b534e1b | 2014-09-04 14:44:01 +0100 | [diff] [blame] | 267 | dump_args(code_state->state, n_state); |
| 268 | } |