blob: 7c89c470009c93b28a4cba3265c6835c10086be0 [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#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 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
20#define DEBUG_printf(args...) (void)0
21#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) {
Dave Hylands51dabac2014-02-17 17:57:13 -080029 rt_check_nargs(n_args, self->n_args_min, self->n_args_max, n_kw, self->is_kw);
30}
31
32void rt_check_nargs(int n_args, machine_uint_t n_args_min, machine_uint_t n_args_max, int n_kw, bool is_kw) {
33 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 George20006db2014-01-18 14:10:48 +000058 assert(MP_OBJ_IS_TYPE(self_in, &fun_native_type));
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
102const mp_obj_type_t fun_native_type = {
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
109mp_obj_t rt_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);
111 o->base.type = &fun_native_type;
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
Damiend99b0522013-12-21 18:17:45 +0000119mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) {
120 mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
121 o->base.type = &fun_native_type;
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;
124 o->n_args_max = ~((machine_uint_t)0);
125 o->fun = fun;
126 return o;
127}
128
129// min and max are inclusive
130mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) {
131 mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
132 o->base.type = &fun_native_type;
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 George2e482cd2014-02-16 00:01:29 +0000146 struct {
147 machine_uint_t n_args : 15; // number of arguments this function takes
148 machine_uint_t n_def_args : 15; // number of default arguments
149 machine_uint_t takes_var_args : 1; // set if this function takes variable args
150 machine_uint_t takes_kw_args : 1; // set if this function takes keyword args
151 };
Damien George66028ab2014-01-03 14:03:48 +0000152 uint n_state; // total state size for the executing function (incl args, locals, stack)
153 const byte *bytecode; // bytecode for the function
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200154 qstr *args; // argument names (needed to resolve positional args passed as keywords)
Damien Georgee5d371b2014-02-16 00:17:42 +0000155 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 +0000156} mp_obj_fun_bc_t;
157
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200158void dump_args(const mp_obj_t *a, int sz) {
159#if DEBUG_PRINT
160 DEBUG_printf("%p: ", a);
161 for (int i = 0; i < sz; i++) {
162 DEBUG_printf("%p ", a[i]);
163 }
164 DEBUG_printf("\n");
165#endif
166}
167
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200168STATIC 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 +0200169 DEBUG_printf("Input: ");
170 dump_args(args, n_args);
Damiend99b0522013-12-21 18:17:45 +0000171 mp_obj_fun_bc_t *self = self_in;
172
Damien Georgee5d371b2014-02-16 00:17:42 +0000173 const mp_obj_t *kwargs = args + n_args;
Damien George2e482cd2014-02-16 00:01:29 +0000174 mp_obj_t *extra_args = self->extra_args + self->n_def_args;
175 uint n_extra_args = 0;
176
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200177
Damien Georgee5d371b2014-02-16 00:17:42 +0000178 // check positional arguments
179
Damien George2e482cd2014-02-16 00:01:29 +0000180 if (n_args > self->n_args) {
181 // given more than enough arguments
182 if (!self->takes_var_args) {
183 goto arg_error;
184 }
185 // put extra arguments in varargs tuple
186 *extra_args = mp_obj_new_tuple(n_args - self->n_args, args + self->n_args);
187 n_extra_args = 1;
188 n_args = self->n_args;
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200189 } else {
Damien George2e482cd2014-02-16 00:01:29 +0000190 if (self->takes_var_args) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200191 DEBUG_printf("passing empty tuple as *args\n");
Damien George2e482cd2014-02-16 00:01:29 +0000192 *extra_args = mp_const_empty_tuple;
193 n_extra_args = 1;
194 }
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200195 // Apply processing and check below only if we don't have kwargs,
196 // otherwise, kw handling code below has own extensive checks.
197 if (n_kw == 0) {
198 if (n_args >= self->n_args - self->n_def_args) {
199 // given enough arguments, but may need to use some default arguments
200 extra_args -= self->n_args - n_args;
201 n_extra_args += self->n_args - n_args;
202 } else {
203 goto arg_error;
204 }
205 }
Damiend99b0522013-12-21 18:17:45 +0000206 }
Damien George2e482cd2014-02-16 00:01:29 +0000207
Damien Georgee5d371b2014-02-16 00:17:42 +0000208 // check keyword arguments
209
Damien George20006db2014-01-18 14:10:48 +0000210 if (n_kw != 0) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200211 // We cannot use dynamically-sized array here, because GCC indeed
212 // deallocates it on leaving defining scope (unlike most static stack allocs).
213 // So, we have 2 choices: allocate it unconditionally at the top of function
214 // (wastes stack), or use alloca which is guaranteed to dealloc on func exit.
215 //mp_obj_t flat_args[self->n_args];
216 mp_obj_t *flat_args = alloca(self->n_args * sizeof(mp_obj_t));
217 for (int i = self->n_args - 1; i >= 0; i--) {
218 flat_args[i] = MP_OBJ_NULL;
Damien Georgee5d371b2014-02-16 00:17:42 +0000219 }
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200220 memcpy(flat_args, args, sizeof(*args) * n_args);
221 DEBUG_printf("Initial args: ");
222 dump_args(flat_args, self->n_args);
223
224 mp_obj_t dict = MP_OBJ_NULL;
225 if (self->takes_kw_args) {
226 dict = mp_obj_new_dict(n_kw); // TODO: better go conservative with 0?
227 }
Damien Georgee5d371b2014-02-16 00:17:42 +0000228 for (uint i = 0; i < n_kw; i++) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200229 qstr arg_name = MP_OBJ_QSTR_VALUE(kwargs[2 * i]);
230 for (uint j = 0; j < self->n_args; j++) {
231 if (arg_name == self->args[j]) {
232 if (flat_args[j] != MP_OBJ_NULL) {
233 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
234 "function got multiple values for argument '%s'", qstr_str(arg_name)));
235 }
236 flat_args[j] = kwargs[2 * i + 1];
237 goto continue2;
238 }
239 }
240 // Didn't find name match with positional args
241 if (!self->takes_kw_args) {
242 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
243 }
Damien Georgee5d371b2014-02-16 00:17:42 +0000244 mp_obj_dict_store(dict, kwargs[2 * i], kwargs[2 * i + 1]);
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200245continue2:;
Damien Georgee5d371b2014-02-16 00:17:42 +0000246 }
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200247 DEBUG_printf("Args with kws flattened: ");
248 dump_args(flat_args, self->n_args);
249
250 // Now fill in defaults
251 mp_obj_t *d = &flat_args[self->n_args - 1];
252 mp_obj_t *s = &self->extra_args[self->n_def_args - 1];
253 for (int i = self->n_def_args; i > 0; i--) {
254 if (*d != MP_OBJ_NULL) {
255 *d-- = *s--;
256 }
257 }
258 DEBUG_printf("Args after filling defaults: ");
259 dump_args(flat_args, self->n_args);
260
261 // Now check that all mandatory args specified
262 while (d >= flat_args) {
263 if (*d-- == MP_OBJ_NULL) {
264 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
265 "function missing required positional argument #%d", d - flat_args));
266 }
267 }
268
269 args = flat_args;
270 n_args = self->n_args;
271
272 if (self->takes_kw_args) {
273 extra_args[n_extra_args] = dict;
274 n_extra_args += 1;
275 }
Damien Georgee5d371b2014-02-16 00:17:42 +0000276 } else {
277 // no keyword arguments given
278 if (self->takes_kw_args) {
279 extra_args[n_extra_args] = mp_obj_new_dict(0);
280 n_extra_args += 1;
281 }
Damien George20006db2014-01-18 14:10:48 +0000282 }
Damiend99b0522013-12-21 18:17:45 +0000283
Damien George66028ab2014-01-03 14:03:48 +0000284 mp_map_t *old_globals = rt_globals_get();
Damien Georgefb083ea2014-02-01 18:29:40 +0000285 rt_globals_set(self->globals);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000286 mp_obj_t result;
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200287 DEBUG_printf("Calling: args=%p, n_args=%d, extra_args=%p, n_extra_args=%d\n", args, n_args, extra_args, n_extra_args);
288 dump_args(args, n_args);
289 dump_args(extra_args, n_extra_args);
Damien George2e482cd2014-02-16 00:01:29 +0000290 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 Georgefb083ea2014-02-01 18:29:40 +0000291 rt_globals_set(old_globals);
292
Damien Georgec8f78bc2014-02-15 22:55:00 +0000293 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
294 return result;
295 } else { // MP_VM_RETURN_EXCEPTION
296 nlr_jump(result);
297 }
Damien George2e482cd2014-02-16 00:01:29 +0000298
299arg_error:
300 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 +0000301}
302
303const mp_obj_type_t fun_bc_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000304 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000305 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000306 .call = fun_bc_call,
Damiend99b0522013-12-21 18:17:45 +0000307};
308
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200309mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_args, mp_obj_t def_args_in, uint n_state, const byte *code) {
Damien George2e482cd2014-02-16 00:01:29 +0000310 uint n_def_args = 0;
311 uint n_extra_args = 0;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200312 mp_obj_tuple_t *def_args = def_args_in;
313 if (def_args != MP_OBJ_NULL) {
314 n_def_args = def_args->len;
Damien George2e482cd2014-02-16 00:01:29 +0000315 n_extra_args = def_args->len;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200316 }
Damien George2e482cd2014-02-16 00:01:29 +0000317 if ((scope_flags & MP_SCOPE_FLAG_VARARGS) != 0) {
318 n_extra_args += 1;
319 }
Damien Georgee5d371b2014-02-16 00:17:42 +0000320 if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
321 n_extra_args += 1;
322 }
Damien George2e482cd2014-02-16 00:01:29 +0000323 mp_obj_fun_bc_t *o = m_new_obj_var(mp_obj_fun_bc_t, mp_obj_t, n_extra_args);
Damiend99b0522013-12-21 18:17:45 +0000324 o->base.type = &fun_bc_type;
Damien George66028ab2014-01-03 14:03:48 +0000325 o->globals = rt_globals_get();
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200326 o->args = args;
Damiend99b0522013-12-21 18:17:45 +0000327 o->n_args = n_args;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200328 o->n_def_args = n_def_args;
Damien George2e482cd2014-02-16 00:01:29 +0000329 o->takes_var_args = (scope_flags & MP_SCOPE_FLAG_VARARGS) != 0;
330 o->takes_kw_args = (scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0;
Damiend99b0522013-12-21 18:17:45 +0000331 o->n_state = n_state;
Damien George66028ab2014-01-03 14:03:48 +0000332 o->bytecode = code;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200333 if (def_args != MP_OBJ_NULL) {
Damien George2e482cd2014-02-16 00:01:29 +0000334 memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
Paul Sokolovsky90750022014-02-01 15:05:04 +0200335 }
Damiend99b0522013-12-21 18:17:45 +0000336 return o;
337}
338
Damien George66028ab2014-01-03 14:03:48 +0000339void mp_obj_fun_bc_get(mp_obj_t self_in, int *n_args, uint *n_state, const byte **code) {
340 assert(MP_OBJ_IS_TYPE(self_in, &fun_bc_type));
341 mp_obj_fun_bc_t *self = self_in;
342 *n_args = self->n_args;
343 *n_state = self->n_state;
344 *code = self->bytecode;
345}
346
Damiend99b0522013-12-21 18:17:45 +0000347/******************************************************************************/
348/* inline assembler functions */
349
350typedef struct _mp_obj_fun_asm_t {
351 mp_obj_base_t base;
352 int n_args;
353 void *fun;
354} mp_obj_fun_asm_t;
355
356typedef machine_uint_t (*inline_asm_fun_0_t)();
357typedef machine_uint_t (*inline_asm_fun_1_t)(machine_uint_t);
358typedef machine_uint_t (*inline_asm_fun_2_t)(machine_uint_t, machine_uint_t);
359typedef machine_uint_t (*inline_asm_fun_3_t)(machine_uint_t, machine_uint_t, machine_uint_t);
360
361// convert a Micro Python object to a sensible value for inline asm
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200362STATIC machine_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
Damiend99b0522013-12-21 18:17:45 +0000363 // TODO for byte_array, pass pointer to the array
364 if (MP_OBJ_IS_SMALL_INT(obj)) {
365 return MP_OBJ_SMALL_INT_VALUE(obj);
366 } else if (obj == mp_const_none) {
367 return 0;
368 } else if (obj == mp_const_false) {
369 return 0;
370 } else if (obj == mp_const_true) {
371 return 1;
Damien George5fa93b62014-01-22 14:35:10 +0000372 } else if (MP_OBJ_IS_STR(obj)) {
Damiend99b0522013-12-21 18:17:45 +0000373 // pointer to the string (it's probably constant though!)
Damien George5fa93b62014-01-22 14:35:10 +0000374 uint l;
375 return (machine_uint_t)mp_obj_str_get_data(obj, &l);
Damiend99b0522013-12-21 18:17:45 +0000376#if MICROPY_ENABLE_FLOAT
377 } else if (MP_OBJ_IS_TYPE(obj, &float_type)) {
378 // convert float to int (could also pass in float registers)
379 return (machine_int_t)mp_obj_float_get(obj);
380#endif
381 } else if (MP_OBJ_IS_TYPE(obj, &tuple_type)) {
382 // pointer to start of tuple (could pass length, but then could use len(x) for that)
383 uint len;
384 mp_obj_t *items;
385 mp_obj_tuple_get(obj, &len, &items);
386 return (machine_uint_t)items;
387 } else if (MP_OBJ_IS_TYPE(obj, &list_type)) {
388 // pointer to start of list (could pass length, but then could use len(x) for that)
389 uint len;
390 mp_obj_t *items;
391 mp_obj_list_get(obj, &len, &items);
392 return (machine_uint_t)items;
393 } else {
394 // just pass along a pointer to the object
395 return (machine_uint_t)obj;
396 }
397}
398
399// convert a return value from inline asm to a sensible Micro Python object
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200400STATIC mp_obj_t convert_val_from_inline_asm(machine_uint_t val) {
Damiend99b0522013-12-21 18:17:45 +0000401 return MP_OBJ_NEW_SMALL_INT(val);
402}
403
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200404STATIC 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 +0000405 mp_obj_fun_asm_t *self = self_in;
406
407 if (n_args != self->n_args) {
Damien Georgec5966122014-02-15 16:10:44 +0000408 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 +0000409 }
Damien George20006db2014-01-18 14:10:48 +0000410 if (n_kw != 0) {
Damien Georgec5966122014-02-15 16:10:44 +0000411 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
Damien George20006db2014-01-18 14:10:48 +0000412 }
Damiend99b0522013-12-21 18:17:45 +0000413
414 machine_uint_t ret;
415 if (n_args == 0) {
416 ret = ((inline_asm_fun_0_t)self->fun)();
417 } else if (n_args == 1) {
418 ret = ((inline_asm_fun_1_t)self->fun)(convert_obj_for_inline_asm(args[0]));
419 } else if (n_args == 2) {
Damien George20006db2014-01-18 14:10:48 +0000420 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 +0000421 } else if (n_args == 3) {
Damien George20006db2014-01-18 14:10:48 +0000422 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 +0000423 } else {
424 assert(0);
425 ret = 0;
426 }
427
428 return convert_val_from_inline_asm(ret);
429}
430
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200431STATIC const mp_obj_type_t fun_asm_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000432 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000433 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000434 .call = fun_asm_call,
Damiend99b0522013-12-21 18:17:45 +0000435};
436
437mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun) {
438 mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
439 o->base.type = &fun_asm_type;
440 o->n_args = n_args;
441 o->fun = fun;
442 return o;
443}