Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <stdint.h> |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include "nlr.h" |
| 7 | #include "misc.h" |
| 8 | #include "mpconfig.h" |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 9 | #include "qstr.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 10 | #include "obj.h" |
Paul Sokolovsky | 9075002 | 2014-02-01 15:05:04 +0200 | [diff] [blame] | 11 | #include "objtuple.h" |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 12 | #include "map.h" |
Damien George | 2e482cd | 2014-02-16 00:01:29 +0000 | [diff] [blame] | 13 | #include "runtime0.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 14 | #include "runtime.h" |
| 15 | #include "bc.h" |
| 16 | |
| 17 | /******************************************************************************/ |
| 18 | /* native functions */ |
| 19 | |
| 20 | // mp_obj_fun_native_t defined in obj.h |
| 21 | |
Paul Sokolovsky | d5df6cd | 2014-02-12 18:15:40 +0200 | [diff] [blame] | 22 | STATIC void check_nargs(mp_obj_fun_native_t *self, int n_args, int n_kw) { |
John R. Lenton | 88cb1e6 | 2014-01-13 19:55:18 +0000 | [diff] [blame] | 23 | if (n_kw && !self->is_kw) { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 24 | nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, |
John R. Lenton | 88cb1e6 | 2014-01-13 19:55:18 +0000 | [diff] [blame] | 25 | "function does not take keyword arguments")); |
| 26 | } |
| 27 | |
| 28 | if (self->n_args_min == self->n_args_max) { |
| 29 | if (n_args != self->n_args_min) { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 30 | nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
John R. Lenton | 88cb1e6 | 2014-01-13 19:55:18 +0000 | [diff] [blame] | 31 | "function takes %d positional arguments but %d were given", |
Damien George | 099a9cb | 2014-02-12 23:02:19 +0000 | [diff] [blame] | 32 | self->n_args_min, n_args)); |
John R. Lenton | 88cb1e6 | 2014-01-13 19:55:18 +0000 | [diff] [blame] | 33 | } |
| 34 | } else { |
| 35 | if (n_args < self->n_args_min) { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 36 | nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
John R. Lenton | 88cb1e6 | 2014-01-13 19:55:18 +0000 | [diff] [blame] | 37 | "<fun name>() missing %d required positional arguments: <list of names of params>", |
Damien George | 099a9cb | 2014-02-12 23:02:19 +0000 | [diff] [blame] | 38 | self->n_args_min - n_args)); |
John R. Lenton | 88cb1e6 | 2014-01-13 19:55:18 +0000 | [diff] [blame] | 39 | } else if (n_args > self->n_args_max) { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 40 | nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
John R. Lenton | 88cb1e6 | 2014-01-13 19:55:18 +0000 | [diff] [blame] | 41 | "<fun name> expected at most %d arguments, got %d", |
Damien George | 099a9cb | 2014-02-12 23:02:19 +0000 | [diff] [blame] | 42 | self->n_args_max, n_args)); |
John R. Lenton | 88cb1e6 | 2014-01-13 19:55:18 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
Paul Sokolovsky | d5df6cd | 2014-02-12 18:15:40 +0200 | [diff] [blame] | 47 | STATIC mp_obj_t fun_native_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) { |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 48 | assert(MP_OBJ_IS_TYPE(self_in, &fun_native_type)); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 49 | mp_obj_fun_native_t *self = self_in; |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 50 | |
John R. Lenton | 88cb1e6 | 2014-01-13 19:55:18 +0000 | [diff] [blame] | 51 | // check number of arguments |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 52 | check_nargs(self, n_args, n_kw); |
| 53 | |
John R. Lenton | c06763a | 2014-01-07 17:29:16 +0000 | [diff] [blame] | 54 | if (self->is_kw) { |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 55 | // function allows keywords |
| 56 | |
Damien George | 0a587b8 | 2014-02-08 18:53:41 +0000 | [diff] [blame] | 57 | // we create a map directly from the given args array |
| 58 | mp_map_t kw_args; |
| 59 | mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 60 | |
Damien George | 0a587b8 | 2014-02-08 18:53:41 +0000 | [diff] [blame] | 61 | return ((mp_fun_kw_t)self->fun)(n_args, args, &kw_args); |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 62 | |
Paul Sokolovsky | 9d95a2b | 2014-01-26 01:58:51 +0200 | [diff] [blame] | 63 | } 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] | 64 | // function requires a fixed number of arguments |
| 65 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 66 | // dispatch function call |
| 67 | switch (self->n_args_min) { |
| 68 | case 0: |
| 69 | return ((mp_fun_0_t)self->fun)(); |
| 70 | |
| 71 | case 1: |
| 72 | return ((mp_fun_1_t)self->fun)(args[0]); |
| 73 | |
| 74 | case 2: |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 75 | return ((mp_fun_2_t)self->fun)(args[0], args[1]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 76 | |
John R. Lenton | 45a8744 | 2014-01-04 01:15:01 +0000 | [diff] [blame] | 77 | case 3: |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 78 | 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] | 79 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 80 | default: |
| 81 | assert(0); |
| 82 | return mp_const_none; |
| 83 | } |
| 84 | |
| 85 | } else { |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 86 | // function takes a variable number of arguments, but no keywords |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 87 | |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 88 | return ((mp_fun_var_t)self->fun)(n_args, args); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | const mp_obj_type_t fun_native_type = { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 93 | { &mp_type_type }, |
Damien George | a71c83a | 2014-02-15 11:34:50 +0000 | [diff] [blame] | 94 | .name = MP_QSTR_function, |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 95 | .call = fun_native_call, |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
Damien George | f62d33a | 2014-01-13 19:50:05 +0000 | [diff] [blame] | 98 | // fun must have the correct signature for n_args fixed arguments |
| 99 | mp_obj_t rt_make_function_n(int n_args, void *fun) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 100 | mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); |
| 101 | o->base.type = &fun_native_type; |
Dave Hylands | 44332ec | 2014-01-13 08:42:43 -0800 | [diff] [blame] | 102 | o->is_kw = false; |
Damien George | f62d33a | 2014-01-13 19:50:05 +0000 | [diff] [blame] | 103 | o->n_args_min = n_args; |
| 104 | o->n_args_max = n_args; |
John R. Lenton | 45a8744 | 2014-01-04 01:15:01 +0000 | [diff] [blame] | 105 | o->fun = fun; |
| 106 | return o; |
| 107 | } |
| 108 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 109 | mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) { |
| 110 | mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); |
| 111 | o->base.type = &fun_native_type; |
Dave Hylands | 44332ec | 2014-01-13 08:42:43 -0800 | [diff] [blame] | 112 | o->is_kw = false; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 113 | o->n_args_min = n_args_min; |
| 114 | o->n_args_max = ~((machine_uint_t)0); |
| 115 | o->fun = fun; |
| 116 | return o; |
| 117 | } |
| 118 | |
| 119 | // min and max are inclusive |
| 120 | mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) { |
| 121 | mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); |
| 122 | o->base.type = &fun_native_type; |
Dave Hylands | 44332ec | 2014-01-13 08:42:43 -0800 | [diff] [blame] | 123 | o->is_kw = false; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 124 | o->n_args_min = n_args_min; |
| 125 | o->n_args_max = n_args_max; |
| 126 | o->fun = fun; |
| 127 | return o; |
| 128 | } |
| 129 | |
| 130 | /******************************************************************************/ |
| 131 | /* byte code functions */ |
| 132 | |
| 133 | typedef struct _mp_obj_fun_bc_t { |
| 134 | mp_obj_base_t base; |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 135 | mp_map_t *globals; // the context within which this function was defined |
Damien George | 2e482cd | 2014-02-16 00:01:29 +0000 | [diff] [blame] | 136 | struct { |
| 137 | machine_uint_t n_args : 15; // number of arguments this function takes |
| 138 | machine_uint_t n_def_args : 15; // number of default arguments |
| 139 | machine_uint_t takes_var_args : 1; // set if this function takes variable args |
| 140 | machine_uint_t takes_kw_args : 1; // set if this function takes keyword args |
| 141 | }; |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 142 | uint n_state; // total state size for the executing function (incl args, locals, stack) |
| 143 | const byte *bytecode; // bytecode for the function |
Damien George | e5d371b | 2014-02-16 00:17:42 +0000 | [diff] [blame^] | 144 | mp_obj_t extra_args[]; // values of default args (if any), plus a slot at the end for var args and/or kw args (if it takes them) |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 145 | } mp_obj_fun_bc_t; |
| 146 | |
Paul Sokolovsky | d5df6cd | 2014-02-12 18:15:40 +0200 | [diff] [blame] | 147 | STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 148 | mp_obj_fun_bc_t *self = self_in; |
| 149 | |
Damien George | e5d371b | 2014-02-16 00:17:42 +0000 | [diff] [blame^] | 150 | const mp_obj_t *kwargs = args + n_args; |
Damien George | 2e482cd | 2014-02-16 00:01:29 +0000 | [diff] [blame] | 151 | mp_obj_t *extra_args = self->extra_args + self->n_def_args; |
| 152 | uint n_extra_args = 0; |
| 153 | |
Damien George | e5d371b | 2014-02-16 00:17:42 +0000 | [diff] [blame^] | 154 | // check positional arguments |
| 155 | |
Damien George | 2e482cd | 2014-02-16 00:01:29 +0000 | [diff] [blame] | 156 | if (n_args > self->n_args) { |
| 157 | // given more than enough arguments |
| 158 | if (!self->takes_var_args) { |
| 159 | goto arg_error; |
| 160 | } |
| 161 | // put extra arguments in varargs tuple |
| 162 | *extra_args = mp_obj_new_tuple(n_args - self->n_args, args + self->n_args); |
| 163 | n_extra_args = 1; |
| 164 | n_args = self->n_args; |
| 165 | } else if (n_args >= self->n_args - self->n_def_args) { |
| 166 | // given enough arguments, but may need to use some default arguments |
| 167 | if (self->takes_var_args) { |
| 168 | *extra_args = mp_const_empty_tuple; |
| 169 | n_extra_args = 1; |
| 170 | } |
| 171 | extra_args -= self->n_args - n_args; |
| 172 | n_extra_args += self->n_args - n_args; |
| 173 | } else { |
| 174 | goto arg_error; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 175 | } |
Damien George | 2e482cd | 2014-02-16 00:01:29 +0000 | [diff] [blame] | 176 | |
Damien George | e5d371b | 2014-02-16 00:17:42 +0000 | [diff] [blame^] | 177 | // check keyword arguments |
| 178 | |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 179 | if (n_kw != 0) { |
Damien George | e5d371b | 2014-02-16 00:17:42 +0000 | [diff] [blame^] | 180 | // keyword arguments given |
| 181 | if (!self->takes_kw_args) { |
| 182 | nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments")); |
| 183 | } |
| 184 | mp_obj_t dict = mp_obj_new_dict(n_kw); |
| 185 | for (uint i = 0; i < n_kw; i++) { |
| 186 | mp_obj_dict_store(dict, kwargs[2 * i], kwargs[2 * i + 1]); |
| 187 | } |
| 188 | extra_args[n_extra_args] = dict; |
| 189 | n_extra_args += 1; |
| 190 | } else { |
| 191 | // no keyword arguments given |
| 192 | if (self->takes_kw_args) { |
| 193 | extra_args[n_extra_args] = mp_obj_new_dict(0); |
| 194 | n_extra_args += 1; |
| 195 | } |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 196 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 197 | |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 198 | mp_map_t *old_globals = rt_globals_get(); |
Damien George | fb083ea | 2014-02-01 18:29:40 +0000 | [diff] [blame] | 199 | rt_globals_set(self->globals); |
Damien George | c8f78bc | 2014-02-15 22:55:00 +0000 | [diff] [blame] | 200 | mp_obj_t result; |
Damien George | 2e482cd | 2014-02-16 00:01:29 +0000 | [diff] [blame] | 201 | mp_vm_return_kind_t vm_return_kind = mp_execute_byte_code(self->bytecode, args, n_args, extra_args, n_extra_args, self->n_state, &result); |
Damien George | fb083ea | 2014-02-01 18:29:40 +0000 | [diff] [blame] | 202 | rt_globals_set(old_globals); |
| 203 | |
Damien George | c8f78bc | 2014-02-15 22:55:00 +0000 | [diff] [blame] | 204 | if (vm_return_kind == MP_VM_RETURN_NORMAL) { |
| 205 | return result; |
| 206 | } else { // MP_VM_RETURN_EXCEPTION |
| 207 | nlr_jump(result); |
| 208 | } |
Damien George | 2e482cd | 2014-02-16 00:01:29 +0000 | [diff] [blame] | 209 | |
| 210 | arg_error: |
| 211 | nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "function takes %d positional arguments but %d were given", self->n_args, n_args)); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | const mp_obj_type_t fun_bc_type = { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 215 | { &mp_type_type }, |
Damien George | a71c83a | 2014-02-15 11:34:50 +0000 | [diff] [blame] | 216 | .name = MP_QSTR_function, |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 217 | .call = fun_bc_call, |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
Damien George | 2e482cd | 2014-02-16 00:01:29 +0000 | [diff] [blame] | 220 | mp_obj_t mp_obj_new_fun_bc(uint scope_flags, uint n_args, mp_obj_t def_args_in, uint n_state, const byte *code) { |
| 221 | uint n_def_args = 0; |
| 222 | uint n_extra_args = 0; |
Paul Sokolovsky | 9075002 | 2014-02-01 15:05:04 +0200 | [diff] [blame] | 223 | mp_obj_tuple_t *def_args = def_args_in; |
| 224 | if (def_args != MP_OBJ_NULL) { |
| 225 | n_def_args = def_args->len; |
Damien George | 2e482cd | 2014-02-16 00:01:29 +0000 | [diff] [blame] | 226 | n_extra_args = def_args->len; |
Paul Sokolovsky | 9075002 | 2014-02-01 15:05:04 +0200 | [diff] [blame] | 227 | } |
Damien George | 2e482cd | 2014-02-16 00:01:29 +0000 | [diff] [blame] | 228 | if ((scope_flags & MP_SCOPE_FLAG_VARARGS) != 0) { |
| 229 | n_extra_args += 1; |
| 230 | } |
Damien George | e5d371b | 2014-02-16 00:17:42 +0000 | [diff] [blame^] | 231 | if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) { |
| 232 | n_extra_args += 1; |
| 233 | } |
Damien George | 2e482cd | 2014-02-16 00:01:29 +0000 | [diff] [blame] | 234 | mp_obj_fun_bc_t *o = m_new_obj_var(mp_obj_fun_bc_t, mp_obj_t, n_extra_args); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 235 | o->base.type = &fun_bc_type; |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 236 | o->globals = rt_globals_get(); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 237 | o->n_args = n_args; |
Paul Sokolovsky | 9075002 | 2014-02-01 15:05:04 +0200 | [diff] [blame] | 238 | o->n_def_args = n_def_args; |
Damien George | 2e482cd | 2014-02-16 00:01:29 +0000 | [diff] [blame] | 239 | o->takes_var_args = (scope_flags & MP_SCOPE_FLAG_VARARGS) != 0; |
| 240 | o->takes_kw_args = (scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 241 | o->n_state = n_state; |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 242 | o->bytecode = code; |
Paul Sokolovsky | 9075002 | 2014-02-01 15:05:04 +0200 | [diff] [blame] | 243 | if (def_args != MP_OBJ_NULL) { |
Damien George | 2e482cd | 2014-02-16 00:01:29 +0000 | [diff] [blame] | 244 | memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t)); |
Paul Sokolovsky | 9075002 | 2014-02-01 15:05:04 +0200 | [diff] [blame] | 245 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 246 | return o; |
| 247 | } |
| 248 | |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 249 | void mp_obj_fun_bc_get(mp_obj_t self_in, int *n_args, uint *n_state, const byte **code) { |
| 250 | assert(MP_OBJ_IS_TYPE(self_in, &fun_bc_type)); |
| 251 | mp_obj_fun_bc_t *self = self_in; |
| 252 | *n_args = self->n_args; |
| 253 | *n_state = self->n_state; |
| 254 | *code = self->bytecode; |
| 255 | } |
| 256 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 257 | /******************************************************************************/ |
| 258 | /* inline assembler functions */ |
| 259 | |
| 260 | typedef struct _mp_obj_fun_asm_t { |
| 261 | mp_obj_base_t base; |
| 262 | int n_args; |
| 263 | void *fun; |
| 264 | } mp_obj_fun_asm_t; |
| 265 | |
| 266 | typedef machine_uint_t (*inline_asm_fun_0_t)(); |
| 267 | typedef machine_uint_t (*inline_asm_fun_1_t)(machine_uint_t); |
| 268 | typedef machine_uint_t (*inline_asm_fun_2_t)(machine_uint_t, machine_uint_t); |
| 269 | typedef machine_uint_t (*inline_asm_fun_3_t)(machine_uint_t, machine_uint_t, machine_uint_t); |
| 270 | |
| 271 | // convert a Micro Python object to a sensible value for inline asm |
Paul Sokolovsky | d5df6cd | 2014-02-12 18:15:40 +0200 | [diff] [blame] | 272 | STATIC machine_uint_t convert_obj_for_inline_asm(mp_obj_t obj) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 273 | // TODO for byte_array, pass pointer to the array |
| 274 | if (MP_OBJ_IS_SMALL_INT(obj)) { |
| 275 | return MP_OBJ_SMALL_INT_VALUE(obj); |
| 276 | } else if (obj == mp_const_none) { |
| 277 | return 0; |
| 278 | } else if (obj == mp_const_false) { |
| 279 | return 0; |
| 280 | } else if (obj == mp_const_true) { |
| 281 | return 1; |
Damien George | 5fa93b6 | 2014-01-22 14:35:10 +0000 | [diff] [blame] | 282 | } else if (MP_OBJ_IS_STR(obj)) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 283 | // pointer to the string (it's probably constant though!) |
Damien George | 5fa93b6 | 2014-01-22 14:35:10 +0000 | [diff] [blame] | 284 | uint l; |
| 285 | return (machine_uint_t)mp_obj_str_get_data(obj, &l); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 286 | #if MICROPY_ENABLE_FLOAT |
| 287 | } else if (MP_OBJ_IS_TYPE(obj, &float_type)) { |
| 288 | // convert float to int (could also pass in float registers) |
| 289 | return (machine_int_t)mp_obj_float_get(obj); |
| 290 | #endif |
| 291 | } else if (MP_OBJ_IS_TYPE(obj, &tuple_type)) { |
| 292 | // pointer to start of tuple (could pass length, but then could use len(x) for that) |
| 293 | uint len; |
| 294 | mp_obj_t *items; |
| 295 | mp_obj_tuple_get(obj, &len, &items); |
| 296 | return (machine_uint_t)items; |
| 297 | } else if (MP_OBJ_IS_TYPE(obj, &list_type)) { |
| 298 | // pointer to start of list (could pass length, but then could use len(x) for that) |
| 299 | uint len; |
| 300 | mp_obj_t *items; |
| 301 | mp_obj_list_get(obj, &len, &items); |
| 302 | return (machine_uint_t)items; |
| 303 | } else { |
| 304 | // just pass along a pointer to the object |
| 305 | return (machine_uint_t)obj; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // convert a return value from inline asm to a sensible Micro Python object |
Paul Sokolovsky | d5df6cd | 2014-02-12 18:15:40 +0200 | [diff] [blame] | 310 | STATIC mp_obj_t convert_val_from_inline_asm(machine_uint_t val) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 311 | return MP_OBJ_NEW_SMALL_INT(val); |
| 312 | } |
| 313 | |
Paul Sokolovsky | d5df6cd | 2014-02-12 18:15:40 +0200 | [diff] [blame] | 314 | STATIC mp_obj_t fun_asm_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 315 | mp_obj_fun_asm_t *self = self_in; |
| 316 | |
| 317 | if (n_args != self->n_args) { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 318 | nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "function takes %d positional arguments but %d were given", self->n_args, n_args)); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 319 | } |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 320 | if (n_kw != 0) { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 321 | nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments")); |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 322 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 323 | |
| 324 | machine_uint_t ret; |
| 325 | if (n_args == 0) { |
| 326 | ret = ((inline_asm_fun_0_t)self->fun)(); |
| 327 | } else if (n_args == 1) { |
| 328 | ret = ((inline_asm_fun_1_t)self->fun)(convert_obj_for_inline_asm(args[0])); |
| 329 | } else if (n_args == 2) { |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 330 | ret = ((inline_asm_fun_2_t)self->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] | 331 | } else if (n_args == 3) { |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 332 | ret = ((inline_asm_fun_3_t)self->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] | 333 | } else { |
| 334 | assert(0); |
| 335 | ret = 0; |
| 336 | } |
| 337 | |
| 338 | return convert_val_from_inline_asm(ret); |
| 339 | } |
| 340 | |
Paul Sokolovsky | d5df6cd | 2014-02-12 18:15:40 +0200 | [diff] [blame] | 341 | STATIC const mp_obj_type_t fun_asm_type = { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 342 | { &mp_type_type }, |
Damien George | a71c83a | 2014-02-15 11:34:50 +0000 | [diff] [blame] | 343 | .name = MP_QSTR_function, |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 344 | .call = fun_asm_call, |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun) { |
| 348 | mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t); |
| 349 | o->base.type = &fun_asm_type; |
| 350 | o->n_args = n_args; |
| 351 | o->fun = fun; |
| 352 | return o; |
| 353 | } |