blob: d4fa7592f76d7ecafca381833d09f6312f9ab3c2 [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damiend99b0522013-12-21 18:17:45 +00002#include <stdlib.h>
Damiend99b0522013-12-21 18:17:45 +00003#include <string.h>
4#include <assert.h>
5
6#include "nlr.h"
7#include "misc.h"
8#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00009#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "obj.h"
Paul Sokolovsky90750022014-02-01 15:05:04 +020011#include "objtuple.h"
Damien George66028ab2014-01-03 14:03:48 +000012#include "map.h"
Damien George2e482cd2014-02-16 00:01:29 +000013#include "runtime0.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime.h"
15#include "bc.h"
16
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +020017#if 0 // print debugging info
18#define DEBUG_PRINT (1)
19#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000020#define DEBUG_printf(...) (void)0
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +020021#endif
22
Damiend99b0522013-12-21 18:17:45 +000023/******************************************************************************/
24/* native functions */
25
26// mp_obj_fun_native_t defined in obj.h
27
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020028STATIC void check_nargs(mp_obj_fun_native_t *self, int n_args, int n_kw) {
Damien Georged17926d2014-03-30 13:35:08 +010029 mp_check_nargs(n_args, self->n_args_min, self->n_args_max, n_kw, self->is_kw);
Dave Hylands51dabac2014-02-17 17:57:13 -080030}
31
Damien Georged17926d2014-03-30 13:35:08 +010032void mp_check_nargs(int n_args, machine_uint_t n_args_min, machine_uint_t n_args_max, int n_kw, bool is_kw) {
Dave Hylands51dabac2014-02-17 17:57:13 -080033 if (n_kw && !is_kw) {
Damien Georgec5966122014-02-15 16:10:44 +000034 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lenton88cb1e62014-01-13 19:55:18 +000035 "function does not take keyword arguments"));
36 }
37
Dave Hylands51dabac2014-02-17 17:57:13 -080038 if (n_args_min == n_args_max) {
39 if (n_args != n_args_min) {
Damien Georgec5966122014-02-15 16:10:44 +000040 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
John R. Lenton88cb1e62014-01-13 19:55:18 +000041 "function takes %d positional arguments but %d were given",
Dave Hylands51dabac2014-02-17 17:57:13 -080042 n_args_min, n_args));
John R. Lenton88cb1e62014-01-13 19:55:18 +000043 }
44 } else {
Dave Hylands51dabac2014-02-17 17:57:13 -080045 if (n_args < n_args_min) {
Damien Georgec5966122014-02-15 16:10:44 +000046 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
John R. Lenton88cb1e62014-01-13 19:55:18 +000047 "<fun name>() missing %d required positional arguments: <list of names of params>",
Dave Hylands51dabac2014-02-17 17:57:13 -080048 n_args_min - n_args));
49 } else if (n_args > n_args_max) {
Damien Georgec5966122014-02-15 16:10:44 +000050 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
John R. Lenton88cb1e62014-01-13 19:55:18 +000051 "<fun name> expected at most %d arguments, got %d",
Dave Hylands51dabac2014-02-17 17:57:13 -080052 n_args_max, n_args));
John R. Lenton88cb1e62014-01-13 19:55:18 +000053 }
54 }
55}
56
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020057STATIC mp_obj_t fun_native_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George3e1a5c12014-03-29 13:43:38 +000058 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_native));
Damiend99b0522013-12-21 18:17:45 +000059 mp_obj_fun_native_t *self = self_in;
Damien George20006db2014-01-18 14:10:48 +000060
John R. Lenton88cb1e62014-01-13 19:55:18 +000061 // check number of arguments
Damien George20006db2014-01-18 14:10:48 +000062 check_nargs(self, n_args, n_kw);
63
John R. Lentonc06763a2014-01-07 17:29:16 +000064 if (self->is_kw) {
Damien George20006db2014-01-18 14:10:48 +000065 // function allows keywords
66
Damien George0a587b82014-02-08 18:53:41 +000067 // we create a map directly from the given args array
68 mp_map_t kw_args;
69 mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
Damien George20006db2014-01-18 14:10:48 +000070
Damien George0a587b82014-02-08 18:53:41 +000071 return ((mp_fun_kw_t)self->fun)(n_args, args, &kw_args);
Damien George20006db2014-01-18 14:10:48 +000072
Paul Sokolovsky9d95a2b2014-01-26 01:58:51 +020073 } else if (self->n_args_min <= 3 && self->n_args_min == self->n_args_max) {
Damiend99b0522013-12-21 18:17:45 +000074 // function requires a fixed number of arguments
75
Damiend99b0522013-12-21 18:17:45 +000076 // dispatch function call
77 switch (self->n_args_min) {
78 case 0:
79 return ((mp_fun_0_t)self->fun)();
80
81 case 1:
82 return ((mp_fun_1_t)self->fun)(args[0]);
83
84 case 2:
Damien George20006db2014-01-18 14:10:48 +000085 return ((mp_fun_2_t)self->fun)(args[0], args[1]);
Damiend99b0522013-12-21 18:17:45 +000086
John R. Lenton45a87442014-01-04 01:15:01 +000087 case 3:
Damien George20006db2014-01-18 14:10:48 +000088 return ((mp_fun_3_t)self->fun)(args[0], args[1], args[2]);
John R. Lenton45a87442014-01-04 01:15:01 +000089
Damiend99b0522013-12-21 18:17:45 +000090 default:
91 assert(0);
92 return mp_const_none;
93 }
94
95 } else {
Damien George20006db2014-01-18 14:10:48 +000096 // function takes a variable number of arguments, but no keywords
Damiend99b0522013-12-21 18:17:45 +000097
Damien George20006db2014-01-18 14:10:48 +000098 return ((mp_fun_var_t)self->fun)(n_args, args);
Damiend99b0522013-12-21 18:17:45 +000099 }
100}
101
Damien George3e1a5c12014-03-29 13:43:38 +0000102const mp_obj_type_t mp_type_fun_native = {
Damien Georgec5966122014-02-15 16:10:44 +0000103 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000104 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000105 .call = fun_native_call,
Damiend99b0522013-12-21 18:17:45 +0000106};
107
Damien Georgef62d33a2014-01-13 19:50:05 +0000108// fun must have the correct signature for n_args fixed arguments
Damien Georged17926d2014-03-30 13:35:08 +0100109mp_obj_t mp_make_function_n(int n_args, void *fun) {
Damiend99b0522013-12-21 18:17:45 +0000110 mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000111 o->base.type = &mp_type_fun_native;
Dave Hylands44332ec2014-01-13 08:42:43 -0800112 o->is_kw = false;
Damien Georgef62d33a2014-01-13 19:50:05 +0000113 o->n_args_min = n_args;
114 o->n_args_max = n_args;
John R. Lenton45a87442014-01-04 01:15:01 +0000115 o->fun = fun;
116 return o;
117}
118
Damien Georged17926d2014-03-30 13:35:08 +0100119mp_obj_t mp_make_function_var(int n_args_min, mp_fun_var_t fun) {
Damiend99b0522013-12-21 18:17:45 +0000120 mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000121 o->base.type = &mp_type_fun_native;
Dave Hylands44332ec2014-01-13 08:42:43 -0800122 o->is_kw = false;
Damiend99b0522013-12-21 18:17:45 +0000123 o->n_args_min = n_args_min;
Damien Georged5e81822014-02-26 17:47:05 +0000124 o->n_args_max = MP_OBJ_FUN_ARGS_MAX;
Damiend99b0522013-12-21 18:17:45 +0000125 o->fun = fun;
126 return o;
127}
128
129// min and max are inclusive
Damien Georged17926d2014-03-30 13:35:08 +0100130mp_obj_t mp_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) {
Damiend99b0522013-12-21 18:17:45 +0000131 mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000132 o->base.type = &mp_type_fun_native;
Dave Hylands44332ec2014-01-13 08:42:43 -0800133 o->is_kw = false;
Damiend99b0522013-12-21 18:17:45 +0000134 o->n_args_min = n_args_min;
135 o->n_args_max = n_args_max;
136 o->fun = fun;
137 return o;
138}
139
140/******************************************************************************/
141/* byte code functions */
142
143typedef struct _mp_obj_fun_bc_t {
144 mp_obj_base_t base;
Damien George66028ab2014-01-03 14:03:48 +0000145 mp_map_t *globals; // the context within which this function was defined
Damien George51047752014-02-26 17:40:52 +0000146 machine_uint_t n_args : 15; // number of arguments this function takes
147 machine_uint_t n_def_args : 15; // number of default arguments
148 machine_uint_t takes_var_args : 1; // set if this function takes variable args
149 machine_uint_t takes_kw_args : 1; // set if this function takes keyword args
Damien George66028ab2014-01-03 14:03:48 +0000150 const byte *bytecode; // bytecode for the function
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200151 qstr *args; // argument names (needed to resolve positional args passed as keywords)
Damien Georgee5d371b2014-02-16 00:17:42 +0000152 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)
Damiend99b0522013-12-21 18:17:45 +0000153} mp_obj_fun_bc_t;
154
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200155void dump_args(const mp_obj_t *a, int sz) {
156#if DEBUG_PRINT
157 DEBUG_printf("%p: ", a);
158 for (int i = 0; i < sz; i++) {
159 DEBUG_printf("%p ", a[i]);
160 }
161 DEBUG_printf("\n");
162#endif
163}
164
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200165STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200166 DEBUG_printf("Input: ");
167 dump_args(args, n_args);
Damiend99b0522013-12-21 18:17:45 +0000168 mp_obj_fun_bc_t *self = self_in;
169
Damien Georgee5d371b2014-02-16 00:17:42 +0000170 const mp_obj_t *kwargs = args + n_args;
Damien George2e482cd2014-02-16 00:01:29 +0000171 mp_obj_t *extra_args = self->extra_args + self->n_def_args;
172 uint n_extra_args = 0;
173
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200174
Damien Georgee5d371b2014-02-16 00:17:42 +0000175 // check positional arguments
176
Damien George2e482cd2014-02-16 00:01:29 +0000177 if (n_args > self->n_args) {
178 // given more than enough arguments
179 if (!self->takes_var_args) {
180 goto arg_error;
181 }
182 // put extra arguments in varargs tuple
183 *extra_args = mp_obj_new_tuple(n_args - self->n_args, args + self->n_args);
184 n_extra_args = 1;
185 n_args = self->n_args;
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200186 } else {
Damien George2e482cd2014-02-16 00:01:29 +0000187 if (self->takes_var_args) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200188 DEBUG_printf("passing empty tuple as *args\n");
Damien George2e482cd2014-02-16 00:01:29 +0000189 *extra_args = mp_const_empty_tuple;
190 n_extra_args = 1;
191 }
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200192 // Apply processing and check below only if we don't have kwargs,
193 // otherwise, kw handling code below has own extensive checks.
194 if (n_kw == 0) {
195 if (n_args >= self->n_args - self->n_def_args) {
196 // given enough arguments, but may need to use some default arguments
197 extra_args -= self->n_args - n_args;
198 n_extra_args += self->n_args - n_args;
199 } else {
200 goto arg_error;
201 }
202 }
Damiend99b0522013-12-21 18:17:45 +0000203 }
Damien George2e482cd2014-02-16 00:01:29 +0000204
Damien Georgee5d371b2014-02-16 00:17:42 +0000205 // check keyword arguments
206
Damien George20006db2014-01-18 14:10:48 +0000207 if (n_kw != 0) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200208 // We cannot use dynamically-sized array here, because GCC indeed
209 // deallocates it on leaving defining scope (unlike most static stack allocs).
210 // So, we have 2 choices: allocate it unconditionally at the top of function
211 // (wastes stack), or use alloca which is guaranteed to dealloc on func exit.
212 //mp_obj_t flat_args[self->n_args];
213 mp_obj_t *flat_args = alloca(self->n_args * sizeof(mp_obj_t));
214 for (int i = self->n_args - 1; i >= 0; i--) {
215 flat_args[i] = MP_OBJ_NULL;
Damien Georgee5d371b2014-02-16 00:17:42 +0000216 }
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200217 memcpy(flat_args, args, sizeof(*args) * n_args);
218 DEBUG_printf("Initial args: ");
219 dump_args(flat_args, self->n_args);
220
221 mp_obj_t dict = MP_OBJ_NULL;
222 if (self->takes_kw_args) {
223 dict = mp_obj_new_dict(n_kw); // TODO: better go conservative with 0?
224 }
Damien Georgee5d371b2014-02-16 00:17:42 +0000225 for (uint i = 0; i < n_kw; i++) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200226 qstr arg_name = MP_OBJ_QSTR_VALUE(kwargs[2 * i]);
227 for (uint j = 0; j < self->n_args; j++) {
228 if (arg_name == self->args[j]) {
229 if (flat_args[j] != MP_OBJ_NULL) {
230 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
231 "function got multiple values for argument '%s'", qstr_str(arg_name)));
232 }
233 flat_args[j] = kwargs[2 * i + 1];
234 goto continue2;
235 }
236 }
237 // Didn't find name match with positional args
238 if (!self->takes_kw_args) {
239 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
240 }
Damien Georgee5d371b2014-02-16 00:17:42 +0000241 mp_obj_dict_store(dict, kwargs[2 * i], kwargs[2 * i + 1]);
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200242continue2:;
Damien Georgee5d371b2014-02-16 00:17:42 +0000243 }
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200244 DEBUG_printf("Args with kws flattened: ");
245 dump_args(flat_args, self->n_args);
246
247 // Now fill in defaults
248 mp_obj_t *d = &flat_args[self->n_args - 1];
249 mp_obj_t *s = &self->extra_args[self->n_def_args - 1];
250 for (int i = self->n_def_args; i > 0; i--) {
Damien George25f5a302014-03-03 23:25:08 +0000251 if (*d == MP_OBJ_NULL) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200252 *d-- = *s--;
253 }
254 }
255 DEBUG_printf("Args after filling defaults: ");
256 dump_args(flat_args, self->n_args);
257
258 // Now check that all mandatory args specified
259 while (d >= flat_args) {
260 if (*d-- == MP_OBJ_NULL) {
261 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
262 "function missing required positional argument #%d", d - flat_args));
263 }
264 }
265
266 args = flat_args;
267 n_args = self->n_args;
268
269 if (self->takes_kw_args) {
270 extra_args[n_extra_args] = dict;
271 n_extra_args += 1;
272 }
Damien Georgee5d371b2014-02-16 00:17:42 +0000273 } else {
274 // no keyword arguments given
275 if (self->takes_kw_args) {
276 extra_args[n_extra_args] = mp_obj_new_dict(0);
277 n_extra_args += 1;
278 }
Damien George20006db2014-01-18 14:10:48 +0000279 }
Damiend99b0522013-12-21 18:17:45 +0000280
Damien Georged17926d2014-03-30 13:35:08 +0100281 mp_map_t *old_globals = mp_globals_get();
282 mp_globals_set(self->globals);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000283 mp_obj_t result;
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200284 DEBUG_printf("Calling: args=%p, n_args=%d, extra_args=%p, n_extra_args=%d\n", args, n_args, extra_args, n_extra_args);
285 dump_args(args, n_args);
286 dump_args(extra_args, n_extra_args);
Damien Georgebee17b02014-03-27 11:07:04 +0000287 mp_vm_return_kind_t vm_return_kind = mp_execute_byte_code(self->bytecode, args, n_args, extra_args, n_extra_args, &result);
Damien Georged17926d2014-03-30 13:35:08 +0100288 mp_globals_set(old_globals);
Damien Georgefb083ea2014-02-01 18:29:40 +0000289
Damien Georgec8f78bc2014-02-15 22:55:00 +0000290 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
291 return result;
292 } else { // MP_VM_RETURN_EXCEPTION
293 nlr_jump(result);
294 }
Damien George2e482cd2014-02-16 00:01:29 +0000295
296arg_error:
297 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));
Damiend99b0522013-12-21 18:17:45 +0000298}
299
Damien George3e1a5c12014-03-29 13:43:38 +0000300const mp_obj_type_t mp_type_fun_bc = {
Damien Georgec5966122014-02-15 16:10:44 +0000301 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000302 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000303 .call = fun_bc_call,
Damiend99b0522013-12-21 18:17:45 +0000304};
305
Damien Georgebee17b02014-03-27 11:07:04 +0000306mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_args, mp_obj_t def_args_in, const byte *code) {
Damien George2e482cd2014-02-16 00:01:29 +0000307 uint n_def_args = 0;
308 uint n_extra_args = 0;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200309 mp_obj_tuple_t *def_args = def_args_in;
310 if (def_args != MP_OBJ_NULL) {
311 n_def_args = def_args->len;
Damien George2e482cd2014-02-16 00:01:29 +0000312 n_extra_args = def_args->len;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200313 }
Damien George2e482cd2014-02-16 00:01:29 +0000314 if ((scope_flags & MP_SCOPE_FLAG_VARARGS) != 0) {
315 n_extra_args += 1;
316 }
Damien Georgee5d371b2014-02-16 00:17:42 +0000317 if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
318 n_extra_args += 1;
319 }
Damien George2e482cd2014-02-16 00:01:29 +0000320 mp_obj_fun_bc_t *o = m_new_obj_var(mp_obj_fun_bc_t, mp_obj_t, n_extra_args);
Damien George3e1a5c12014-03-29 13:43:38 +0000321 o->base.type = &mp_type_fun_bc;
Damien Georged17926d2014-03-30 13:35:08 +0100322 o->globals = mp_globals_get();
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200323 o->args = args;
Damiend99b0522013-12-21 18:17:45 +0000324 o->n_args = n_args;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200325 o->n_def_args = n_def_args;
Damien George2e482cd2014-02-16 00:01:29 +0000326 o->takes_var_args = (scope_flags & MP_SCOPE_FLAG_VARARGS) != 0;
327 o->takes_kw_args = (scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0;
Damien George66028ab2014-01-03 14:03:48 +0000328 o->bytecode = code;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200329 if (def_args != MP_OBJ_NULL) {
Damien George2e482cd2014-02-16 00:01:29 +0000330 memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
Paul Sokolovsky90750022014-02-01 15:05:04 +0200331 }
Damiend99b0522013-12-21 18:17:45 +0000332 return o;
333}
334
Damien Georgebee17b02014-03-27 11:07:04 +0000335void mp_obj_fun_bc_get(mp_obj_t self_in, int *n_args, const byte **code) {
Damien George3e1a5c12014-03-29 13:43:38 +0000336 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_bc));
Damien George66028ab2014-01-03 14:03:48 +0000337 mp_obj_fun_bc_t *self = self_in;
338 *n_args = self->n_args;
Damien George66028ab2014-01-03 14:03:48 +0000339 *code = self->bytecode;
340}
341
Damiend99b0522013-12-21 18:17:45 +0000342/******************************************************************************/
343/* inline assembler functions */
344
345typedef struct _mp_obj_fun_asm_t {
346 mp_obj_base_t base;
347 int n_args;
348 void *fun;
349} mp_obj_fun_asm_t;
350
351typedef machine_uint_t (*inline_asm_fun_0_t)();
352typedef machine_uint_t (*inline_asm_fun_1_t)(machine_uint_t);
353typedef machine_uint_t (*inline_asm_fun_2_t)(machine_uint_t, machine_uint_t);
354typedef machine_uint_t (*inline_asm_fun_3_t)(machine_uint_t, machine_uint_t, machine_uint_t);
355
356// convert a Micro Python object to a sensible value for inline asm
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200357STATIC machine_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
Damiend99b0522013-12-21 18:17:45 +0000358 // TODO for byte_array, pass pointer to the array
359 if (MP_OBJ_IS_SMALL_INT(obj)) {
360 return MP_OBJ_SMALL_INT_VALUE(obj);
361 } else if (obj == mp_const_none) {
362 return 0;
363 } else if (obj == mp_const_false) {
364 return 0;
365 } else if (obj == mp_const_true) {
366 return 1;
Damien George5fa93b62014-01-22 14:35:10 +0000367 } else if (MP_OBJ_IS_STR(obj)) {
Damiend99b0522013-12-21 18:17:45 +0000368 // pointer to the string (it's probably constant though!)
Damien George5fa93b62014-01-22 14:35:10 +0000369 uint l;
370 return (machine_uint_t)mp_obj_str_get_data(obj, &l);
Damiend99b0522013-12-21 18:17:45 +0000371#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000372 } else if (MP_OBJ_IS_TYPE(obj, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000373 // convert float to int (could also pass in float registers)
374 return (machine_int_t)mp_obj_float_get(obj);
375#endif
Damien George07ddab52014-03-29 13:15:08 +0000376 } else if (MP_OBJ_IS_TYPE(obj, &mp_type_tuple)) {
Damiend99b0522013-12-21 18:17:45 +0000377 // pointer to start of tuple (could pass length, but then could use len(x) for that)
378 uint len;
379 mp_obj_t *items;
380 mp_obj_tuple_get(obj, &len, &items);
381 return (machine_uint_t)items;
Damien George3e1a5c12014-03-29 13:43:38 +0000382 } else if (MP_OBJ_IS_TYPE(obj, &mp_type_list)) {
Damiend99b0522013-12-21 18:17:45 +0000383 // pointer to start of list (could pass length, but then could use len(x) for that)
384 uint len;
385 mp_obj_t *items;
386 mp_obj_list_get(obj, &len, &items);
387 return (machine_uint_t)items;
388 } else {
389 // just pass along a pointer to the object
390 return (machine_uint_t)obj;
391 }
392}
393
394// convert a return value from inline asm to a sensible Micro Python object
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200395STATIC mp_obj_t convert_val_from_inline_asm(machine_uint_t val) {
Damiend99b0522013-12-21 18:17:45 +0000396 return MP_OBJ_NEW_SMALL_INT(val);
397}
398
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200399STATIC mp_obj_t fun_asm_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damiend99b0522013-12-21 18:17:45 +0000400 mp_obj_fun_asm_t *self = self_in;
401
402 if (n_args != self->n_args) {
Damien Georgec5966122014-02-15 16:10:44 +0000403 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));
Damiend99b0522013-12-21 18:17:45 +0000404 }
Damien George20006db2014-01-18 14:10:48 +0000405 if (n_kw != 0) {
Damien Georgec5966122014-02-15 16:10:44 +0000406 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
Damien George20006db2014-01-18 14:10:48 +0000407 }
Damiend99b0522013-12-21 18:17:45 +0000408
409 machine_uint_t ret;
410 if (n_args == 0) {
411 ret = ((inline_asm_fun_0_t)self->fun)();
412 } else if (n_args == 1) {
413 ret = ((inline_asm_fun_1_t)self->fun)(convert_obj_for_inline_asm(args[0]));
414 } else if (n_args == 2) {
Damien George20006db2014-01-18 14:10:48 +0000415 ret = ((inline_asm_fun_2_t)self->fun)(convert_obj_for_inline_asm(args[0]), convert_obj_for_inline_asm(args[1]));
Damiend99b0522013-12-21 18:17:45 +0000416 } else if (n_args == 3) {
Damien George20006db2014-01-18 14:10:48 +0000417 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]));
Damiend99b0522013-12-21 18:17:45 +0000418 } else {
419 assert(0);
420 ret = 0;
421 }
422
423 return convert_val_from_inline_asm(ret);
424}
425
Damien George3e1a5c12014-03-29 13:43:38 +0000426STATIC const mp_obj_type_t mp_type_fun_asm = {
Damien Georgec5966122014-02-15 16:10:44 +0000427 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000428 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000429 .call = fun_asm_call,
Damiend99b0522013-12-21 18:17:45 +0000430};
431
432mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun) {
433 mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000434 o->base.type = &mp_type_fun_asm;
Damiend99b0522013-12-21 18:17:45 +0000435 o->n_args = n_args;
436 o->fun = fun;
437 return o;
438}