blob: 354d7ff9ca220d26a1a6d501e3b3d7a39222e83b [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) {
John R. Lenton88cb1e62014-01-13 19:55:18 +000029 if (n_kw && !self->is_kw) {
Damien Georgec5966122014-02-15 16:10:44 +000030 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lenton88cb1e62014-01-13 19:55:18 +000031 "function does not take keyword arguments"));
32 }
33
34 if (self->n_args_min == self->n_args_max) {
35 if (n_args != self->n_args_min) {
Damien Georgec5966122014-02-15 16:10:44 +000036 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
John R. Lenton88cb1e62014-01-13 19:55:18 +000037 "function takes %d positional arguments but %d were given",
Damien George099a9cb2014-02-12 23:02:19 +000038 self->n_args_min, n_args));
John R. Lenton88cb1e62014-01-13 19:55:18 +000039 }
40 } else {
41 if (n_args < self->n_args_min) {
Damien Georgec5966122014-02-15 16:10:44 +000042 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
John R. Lenton88cb1e62014-01-13 19:55:18 +000043 "<fun name>() missing %d required positional arguments: <list of names of params>",
Damien George099a9cb2014-02-12 23:02:19 +000044 self->n_args_min - n_args));
John R. Lenton88cb1e62014-01-13 19:55:18 +000045 } else if (n_args > self->n_args_max) {
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> expected at most %d arguments, got %d",
Damien George099a9cb2014-02-12 23:02:19 +000048 self->n_args_max, n_args));
John R. Lenton88cb1e62014-01-13 19:55:18 +000049 }
50 }
51}
52
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020053STATIC 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 +000054 assert(MP_OBJ_IS_TYPE(self_in, &fun_native_type));
Damiend99b0522013-12-21 18:17:45 +000055 mp_obj_fun_native_t *self = self_in;
Damien George20006db2014-01-18 14:10:48 +000056
John R. Lenton88cb1e62014-01-13 19:55:18 +000057 // check number of arguments
Damien George20006db2014-01-18 14:10:48 +000058 check_nargs(self, n_args, n_kw);
59
John R. Lentonc06763a2014-01-07 17:29:16 +000060 if (self->is_kw) {
Damien George20006db2014-01-18 14:10:48 +000061 // function allows keywords
62
Damien George0a587b82014-02-08 18:53:41 +000063 // we create a map directly from the given args array
64 mp_map_t kw_args;
65 mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
Damien George20006db2014-01-18 14:10:48 +000066
Damien George0a587b82014-02-08 18:53:41 +000067 return ((mp_fun_kw_t)self->fun)(n_args, args, &kw_args);
Damien George20006db2014-01-18 14:10:48 +000068
Paul Sokolovsky9d95a2b2014-01-26 01:58:51 +020069 } else if (self->n_args_min <= 3 && self->n_args_min == self->n_args_max) {
Damiend99b0522013-12-21 18:17:45 +000070 // function requires a fixed number of arguments
71
Damiend99b0522013-12-21 18:17:45 +000072 // dispatch function call
73 switch (self->n_args_min) {
74 case 0:
75 return ((mp_fun_0_t)self->fun)();
76
77 case 1:
78 return ((mp_fun_1_t)self->fun)(args[0]);
79
80 case 2:
Damien George20006db2014-01-18 14:10:48 +000081 return ((mp_fun_2_t)self->fun)(args[0], args[1]);
Damiend99b0522013-12-21 18:17:45 +000082
John R. Lenton45a87442014-01-04 01:15:01 +000083 case 3:
Damien George20006db2014-01-18 14:10:48 +000084 return ((mp_fun_3_t)self->fun)(args[0], args[1], args[2]);
John R. Lenton45a87442014-01-04 01:15:01 +000085
Damiend99b0522013-12-21 18:17:45 +000086 default:
87 assert(0);
88 return mp_const_none;
89 }
90
91 } else {
Damien George20006db2014-01-18 14:10:48 +000092 // function takes a variable number of arguments, but no keywords
Damiend99b0522013-12-21 18:17:45 +000093
Damien George20006db2014-01-18 14:10:48 +000094 return ((mp_fun_var_t)self->fun)(n_args, args);
Damiend99b0522013-12-21 18:17:45 +000095 }
96}
97
98const mp_obj_type_t fun_native_type = {
Damien Georgec5966122014-02-15 16:10:44 +000099 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000100 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000101 .call = fun_native_call,
Damiend99b0522013-12-21 18:17:45 +0000102};
103
Damien Georgef62d33a2014-01-13 19:50:05 +0000104// fun must have the correct signature for n_args fixed arguments
105mp_obj_t rt_make_function_n(int n_args, void *fun) {
Damiend99b0522013-12-21 18:17:45 +0000106 mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
107 o->base.type = &fun_native_type;
Dave Hylands44332ec2014-01-13 08:42:43 -0800108 o->is_kw = false;
Damien Georgef62d33a2014-01-13 19:50:05 +0000109 o->n_args_min = n_args;
110 o->n_args_max = n_args;
John R. Lenton45a87442014-01-04 01:15:01 +0000111 o->fun = fun;
112 return o;
113}
114
Damiend99b0522013-12-21 18:17:45 +0000115mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) {
116 mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
117 o->base.type = &fun_native_type;
Dave Hylands44332ec2014-01-13 08:42:43 -0800118 o->is_kw = false;
Damiend99b0522013-12-21 18:17:45 +0000119 o->n_args_min = n_args_min;
120 o->n_args_max = ~((machine_uint_t)0);
121 o->fun = fun;
122 return o;
123}
124
125// min and max are inclusive
126mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) {
127 mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
128 o->base.type = &fun_native_type;
Dave Hylands44332ec2014-01-13 08:42:43 -0800129 o->is_kw = false;
Damiend99b0522013-12-21 18:17:45 +0000130 o->n_args_min = n_args_min;
131 o->n_args_max = n_args_max;
132 o->fun = fun;
133 return o;
134}
135
136/******************************************************************************/
137/* byte code functions */
138
139typedef struct _mp_obj_fun_bc_t {
140 mp_obj_base_t base;
Damien George66028ab2014-01-03 14:03:48 +0000141 mp_map_t *globals; // the context within which this function was defined
Damien George2e482cd2014-02-16 00:01:29 +0000142 struct {
143 machine_uint_t n_args : 15; // number of arguments this function takes
144 machine_uint_t n_def_args : 15; // number of default arguments
145 machine_uint_t takes_var_args : 1; // set if this function takes variable args
146 machine_uint_t takes_kw_args : 1; // set if this function takes keyword args
147 };
Damien George66028ab2014-01-03 14:03:48 +0000148 uint n_state; // total state size for the executing function (incl args, locals, stack)
149 const byte *bytecode; // bytecode for the function
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200150 qstr *args; // argument names (needed to resolve positional args passed as keywords)
Damien Georgee5d371b2014-02-16 00:17:42 +0000151 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 +0000152} mp_obj_fun_bc_t;
153
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200154void dump_args(const mp_obj_t *a, int sz) {
155#if DEBUG_PRINT
156 DEBUG_printf("%p: ", a);
157 for (int i = 0; i < sz; i++) {
158 DEBUG_printf("%p ", a[i]);
159 }
160 DEBUG_printf("\n");
161#endif
162}
163
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200164STATIC 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 +0200165 DEBUG_printf("Input: ");
166 dump_args(args, n_args);
Damiend99b0522013-12-21 18:17:45 +0000167 mp_obj_fun_bc_t *self = self_in;
168
Damien Georgee5d371b2014-02-16 00:17:42 +0000169 const mp_obj_t *kwargs = args + n_args;
Damien George2e482cd2014-02-16 00:01:29 +0000170 mp_obj_t *extra_args = self->extra_args + self->n_def_args;
171 uint n_extra_args = 0;
172
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200173
Damien Georgee5d371b2014-02-16 00:17:42 +0000174 // check positional arguments
175
Damien George2e482cd2014-02-16 00:01:29 +0000176 if (n_args > self->n_args) {
177 // given more than enough arguments
178 if (!self->takes_var_args) {
179 goto arg_error;
180 }
181 // put extra arguments in varargs tuple
182 *extra_args = mp_obj_new_tuple(n_args - self->n_args, args + self->n_args);
183 n_extra_args = 1;
184 n_args = self->n_args;
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200185 } else {
Damien George2e482cd2014-02-16 00:01:29 +0000186 if (self->takes_var_args) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200187 DEBUG_printf("passing empty tuple as *args\n");
Damien George2e482cd2014-02-16 00:01:29 +0000188 *extra_args = mp_const_empty_tuple;
189 n_extra_args = 1;
190 }
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200191 // Apply processing and check below only if we don't have kwargs,
192 // otherwise, kw handling code below has own extensive checks.
193 if (n_kw == 0) {
194 if (n_args >= self->n_args - self->n_def_args) {
195 // given enough arguments, but may need to use some default arguments
196 extra_args -= self->n_args - n_args;
197 n_extra_args += self->n_args - n_args;
198 } else {
199 goto arg_error;
200 }
201 }
Damiend99b0522013-12-21 18:17:45 +0000202 }
Damien George2e482cd2014-02-16 00:01:29 +0000203
Damien Georgee5d371b2014-02-16 00:17:42 +0000204 // check keyword arguments
205
Damien George20006db2014-01-18 14:10:48 +0000206 if (n_kw != 0) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200207 // We cannot use dynamically-sized array here, because GCC indeed
208 // deallocates it on leaving defining scope (unlike most static stack allocs).
209 // So, we have 2 choices: allocate it unconditionally at the top of function
210 // (wastes stack), or use alloca which is guaranteed to dealloc on func exit.
211 //mp_obj_t flat_args[self->n_args];
212 mp_obj_t *flat_args = alloca(self->n_args * sizeof(mp_obj_t));
213 for (int i = self->n_args - 1; i >= 0; i--) {
214 flat_args[i] = MP_OBJ_NULL;
Damien Georgee5d371b2014-02-16 00:17:42 +0000215 }
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200216 memcpy(flat_args, args, sizeof(*args) * n_args);
217 DEBUG_printf("Initial args: ");
218 dump_args(flat_args, self->n_args);
219
220 mp_obj_t dict = MP_OBJ_NULL;
221 if (self->takes_kw_args) {
222 dict = mp_obj_new_dict(n_kw); // TODO: better go conservative with 0?
223 }
Damien Georgee5d371b2014-02-16 00:17:42 +0000224 for (uint i = 0; i < n_kw; i++) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200225 qstr arg_name = MP_OBJ_QSTR_VALUE(kwargs[2 * i]);
226 for (uint j = 0; j < self->n_args; j++) {
227 if (arg_name == self->args[j]) {
228 if (flat_args[j] != MP_OBJ_NULL) {
229 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
230 "function got multiple values for argument '%s'", qstr_str(arg_name)));
231 }
232 flat_args[j] = kwargs[2 * i + 1];
233 goto continue2;
234 }
235 }
236 // Didn't find name match with positional args
237 if (!self->takes_kw_args) {
238 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
239 }
Damien Georgee5d371b2014-02-16 00:17:42 +0000240 mp_obj_dict_store(dict, kwargs[2 * i], kwargs[2 * i + 1]);
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200241continue2:;
Damien Georgee5d371b2014-02-16 00:17:42 +0000242 }
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200243 DEBUG_printf("Args with kws flattened: ");
244 dump_args(flat_args, self->n_args);
245
246 // Now fill in defaults
247 mp_obj_t *d = &flat_args[self->n_args - 1];
248 mp_obj_t *s = &self->extra_args[self->n_def_args - 1];
249 for (int i = self->n_def_args; i > 0; i--) {
250 if (*d != MP_OBJ_NULL) {
251 *d-- = *s--;
252 }
253 }
254 DEBUG_printf("Args after filling defaults: ");
255 dump_args(flat_args, self->n_args);
256
257 // Now check that all mandatory args specified
258 while (d >= flat_args) {
259 if (*d-- == MP_OBJ_NULL) {
260 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
261 "function missing required positional argument #%d", d - flat_args));
262 }
263 }
264
265 args = flat_args;
266 n_args = self->n_args;
267
268 if (self->takes_kw_args) {
269 extra_args[n_extra_args] = dict;
270 n_extra_args += 1;
271 }
Damien Georgee5d371b2014-02-16 00:17:42 +0000272 } else {
273 // no keyword arguments given
274 if (self->takes_kw_args) {
275 extra_args[n_extra_args] = mp_obj_new_dict(0);
276 n_extra_args += 1;
277 }
Damien George20006db2014-01-18 14:10:48 +0000278 }
Damiend99b0522013-12-21 18:17:45 +0000279
Damien George66028ab2014-01-03 14:03:48 +0000280 mp_map_t *old_globals = rt_globals_get();
Damien Georgefb083ea2014-02-01 18:29:40 +0000281 rt_globals_set(self->globals);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000282 mp_obj_t result;
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200283 DEBUG_printf("Calling: args=%p, n_args=%d, extra_args=%p, n_extra_args=%d\n", args, n_args, extra_args, n_extra_args);
284 dump_args(args, n_args);
285 dump_args(extra_args, n_extra_args);
Damien George2e482cd2014-02-16 00:01:29 +0000286 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 +0000287 rt_globals_set(old_globals);
288
Damien Georgec8f78bc2014-02-15 22:55:00 +0000289 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
290 return result;
291 } else { // MP_VM_RETURN_EXCEPTION
292 nlr_jump(result);
293 }
Damien George2e482cd2014-02-16 00:01:29 +0000294
295arg_error:
296 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 +0000297}
298
299const mp_obj_type_t fun_bc_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000300 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000301 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000302 .call = fun_bc_call,
Damiend99b0522013-12-21 18:17:45 +0000303};
304
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200305mp_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 +0000306 uint n_def_args = 0;
307 uint n_extra_args = 0;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200308 mp_obj_tuple_t *def_args = def_args_in;
309 if (def_args != MP_OBJ_NULL) {
310 n_def_args = def_args->len;
Damien George2e482cd2014-02-16 00:01:29 +0000311 n_extra_args = def_args->len;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200312 }
Damien George2e482cd2014-02-16 00:01:29 +0000313 if ((scope_flags & MP_SCOPE_FLAG_VARARGS) != 0) {
314 n_extra_args += 1;
315 }
Damien Georgee5d371b2014-02-16 00:17:42 +0000316 if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
317 n_extra_args += 1;
318 }
Damien George2e482cd2014-02-16 00:01:29 +0000319 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 +0000320 o->base.type = &fun_bc_type;
Damien George66028ab2014-01-03 14:03:48 +0000321 o->globals = rt_globals_get();
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200322 o->args = args;
Damiend99b0522013-12-21 18:17:45 +0000323 o->n_args = n_args;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200324 o->n_def_args = n_def_args;
Damien George2e482cd2014-02-16 00:01:29 +0000325 o->takes_var_args = (scope_flags & MP_SCOPE_FLAG_VARARGS) != 0;
326 o->takes_kw_args = (scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0;
Damiend99b0522013-12-21 18:17:45 +0000327 o->n_state = n_state;
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 George66028ab2014-01-03 14:03:48 +0000335void mp_obj_fun_bc_get(mp_obj_t self_in, int *n_args, uint *n_state, const byte **code) {
336 assert(MP_OBJ_IS_TYPE(self_in, &fun_bc_type));
337 mp_obj_fun_bc_t *self = self_in;
338 *n_args = self->n_args;
339 *n_state = self->n_state;
340 *code = self->bytecode;
341}
342
Damiend99b0522013-12-21 18:17:45 +0000343/******************************************************************************/
344/* inline assembler functions */
345
346typedef struct _mp_obj_fun_asm_t {
347 mp_obj_base_t base;
348 int n_args;
349 void *fun;
350} mp_obj_fun_asm_t;
351
352typedef machine_uint_t (*inline_asm_fun_0_t)();
353typedef machine_uint_t (*inline_asm_fun_1_t)(machine_uint_t);
354typedef machine_uint_t (*inline_asm_fun_2_t)(machine_uint_t, machine_uint_t);
355typedef machine_uint_t (*inline_asm_fun_3_t)(machine_uint_t, machine_uint_t, machine_uint_t);
356
357// convert a Micro Python object to a sensible value for inline asm
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200358STATIC machine_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
Damiend99b0522013-12-21 18:17:45 +0000359 // TODO for byte_array, pass pointer to the array
360 if (MP_OBJ_IS_SMALL_INT(obj)) {
361 return MP_OBJ_SMALL_INT_VALUE(obj);
362 } else if (obj == mp_const_none) {
363 return 0;
364 } else if (obj == mp_const_false) {
365 return 0;
366 } else if (obj == mp_const_true) {
367 return 1;
Damien George5fa93b62014-01-22 14:35:10 +0000368 } else if (MP_OBJ_IS_STR(obj)) {
Damiend99b0522013-12-21 18:17:45 +0000369 // pointer to the string (it's probably constant though!)
Damien George5fa93b62014-01-22 14:35:10 +0000370 uint l;
371 return (machine_uint_t)mp_obj_str_get_data(obj, &l);
Damiend99b0522013-12-21 18:17:45 +0000372#if MICROPY_ENABLE_FLOAT
373 } else if (MP_OBJ_IS_TYPE(obj, &float_type)) {
374 // convert float to int (could also pass in float registers)
375 return (machine_int_t)mp_obj_float_get(obj);
376#endif
377 } else if (MP_OBJ_IS_TYPE(obj, &tuple_type)) {
378 // pointer to start of tuple (could pass length, but then could use len(x) for that)
379 uint len;
380 mp_obj_t *items;
381 mp_obj_tuple_get(obj, &len, &items);
382 return (machine_uint_t)items;
383 } else if (MP_OBJ_IS_TYPE(obj, &list_type)) {
384 // pointer to start of list (could pass length, but then could use len(x) for that)
385 uint len;
386 mp_obj_t *items;
387 mp_obj_list_get(obj, &len, &items);
388 return (machine_uint_t)items;
389 } else {
390 // just pass along a pointer to the object
391 return (machine_uint_t)obj;
392 }
393}
394
395// convert a return value from inline asm to a sensible Micro Python object
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200396STATIC mp_obj_t convert_val_from_inline_asm(machine_uint_t val) {
Damiend99b0522013-12-21 18:17:45 +0000397 return MP_OBJ_NEW_SMALL_INT(val);
398}
399
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200400STATIC 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 +0000401 mp_obj_fun_asm_t *self = self_in;
402
403 if (n_args != self->n_args) {
Damien Georgec5966122014-02-15 16:10:44 +0000404 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 +0000405 }
Damien George20006db2014-01-18 14:10:48 +0000406 if (n_kw != 0) {
Damien Georgec5966122014-02-15 16:10:44 +0000407 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
Damien George20006db2014-01-18 14:10:48 +0000408 }
Damiend99b0522013-12-21 18:17:45 +0000409
410 machine_uint_t ret;
411 if (n_args == 0) {
412 ret = ((inline_asm_fun_0_t)self->fun)();
413 } else if (n_args == 1) {
414 ret = ((inline_asm_fun_1_t)self->fun)(convert_obj_for_inline_asm(args[0]));
415 } else if (n_args == 2) {
Damien George20006db2014-01-18 14:10:48 +0000416 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 +0000417 } else if (n_args == 3) {
Damien George20006db2014-01-18 14:10:48 +0000418 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 +0000419 } else {
420 assert(0);
421 ret = 0;
422 }
423
424 return convert_val_from_inline_asm(ret);
425}
426
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200427STATIC const mp_obj_type_t fun_asm_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000428 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000429 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000430 .call = fun_asm_call,
Damiend99b0522013-12-21 18:17:45 +0000431};
432
433mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun) {
434 mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
435 o->base.type = &fun_asm_type;
436 o->n_args = n_args;
437 o->fun = fun;
438 return o;
439}