blob: d52eeaeaa5f260e2a77cbc04e7956e560d088e8f [file] [log] [blame]
Damien George110ba352014-08-28 23:37:02 +01001/*
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 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27#include <stdio.h>
28#include <string.h>
29#include <assert.h>
30
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/nlr.h"
32#include "py/runtime0.h"
33#include "py/runtime.h"
34#include "py/emitglue.h"
Damien George110ba352014-08-28 23:37:02 +010035
36#if MICROPY_EMIT_NATIVE
37
38#if 0 // print debugging info
39#define DEBUG_printf DEBUG_printf
40#else // don't print debugging info
41#define DEBUG_printf(...) (void)0
42#endif
43
44// convert a Micro Python object to a valid native value based on type
45mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type) {
46 DEBUG_printf("mp_convert_obj_to_native(%p, " UINT_FMT ")\n", obj, type);
47 switch (type & 3) {
48 case MP_NATIVE_TYPE_OBJ: return (mp_uint_t)obj;
49 case MP_NATIVE_TYPE_BOOL:
Damien Georgee9dac3b2014-09-29 22:10:41 +010050 case MP_NATIVE_TYPE_INT: return mp_obj_get_int(obj);
51 case MP_NATIVE_TYPE_UINT: {
52 mp_buffer_info_t bufinfo;
53 if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_RW)) {
54 return (mp_uint_t)bufinfo.buf;
55 } else {
56 // TODO should be mp_obj_get_uint_truncated or something
57 return mp_obj_get_int(obj);
58 }
59 }
Damien George110ba352014-08-28 23:37:02 +010060 default: assert(0); return 0;
61 }
62}
63
64// convert a native value to a Micro Python object based on type
65mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type) {
66 DEBUG_printf("mp_convert_native_to_obj(" UINT_FMT ", " UINT_FMT ")\n", val, type);
67 switch (type & 3) {
68 case MP_NATIVE_TYPE_OBJ: return (mp_obj_t)val;
69 case MP_NATIVE_TYPE_BOOL: return MP_BOOL(val);
70 case MP_NATIVE_TYPE_INT: return mp_obj_new_int(val);
71 case MP_NATIVE_TYPE_UINT: return mp_obj_new_int_from_uint(val);
72 default: assert(0); return mp_const_none;
73 }
74}
75
76// wrapper that accepts n_args and n_kw in one argument
77// (native emitter can only pass at most 3 arguments to a function)
Damien George4abff752014-08-30 14:59:21 +010078mp_obj_t mp_native_call_function_n_kw(mp_obj_t fun_in, mp_uint_t n_args_kw, const mp_obj_t *args) {
Damien George110ba352014-08-28 23:37:02 +010079 return mp_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
80}
81
82// wrapper that makes raise obj and raises it
83NORETURN void mp_native_raise(mp_obj_t o) {
84 nlr_raise(mp_make_raise_obj(o));
85}
86
87// these must correspond to the respective enum in runtime0.h
88void *const mp_fun_table[MP_F_NUMBER_OF] = {
89 mp_convert_obj_to_native,
90 mp_convert_native_to_obj,
91 mp_load_const_int,
92 mp_load_const_dec,
93 mp_load_const_str,
94 mp_load_const_bytes,
95 mp_load_name,
96 mp_load_global,
97 mp_load_build_class,
98 mp_load_attr,
99 mp_load_method,
100 mp_store_name,
101 mp_store_global,
102 mp_store_attr,
103 mp_obj_subscr,
104 mp_obj_is_true,
105 mp_unary_op,
106 mp_binary_op,
107 mp_obj_new_tuple,
108 mp_obj_new_list,
109 mp_obj_list_append,
110 mp_obj_new_dict,
111 mp_obj_dict_store,
112#if MICROPY_PY_BUILTINS_SET
113 mp_obj_new_set,
114 mp_obj_set_store,
115#endif
116 mp_make_function_from_raw_code,
117 mp_native_call_function_n_kw,
118 mp_call_method_n_kw,
119 mp_getiter,
120 mp_iternext,
121 nlr_push,
122 nlr_pop,
123 mp_native_raise,
124 mp_import_name,
125 mp_import_from,
126 mp_import_all,
127#if MICROPY_PY_BUILTINS_SLICE
128 mp_obj_new_slice,
129#endif
130 mp_unpack_sequence,
131 mp_unpack_ex,
Damien Georgee6ce10a2014-09-06 18:38:20 +0100132 mp_delete_name,
133 mp_delete_global,
Damien George110ba352014-08-28 23:37:02 +0100134};
135
136/*
137void mp_f_vector(mp_fun_kind_t fun_kind) {
138 (mp_f_table[fun_kind])();
139}
140*/
141
142#endif // MICROPY_EMIT_NATIVE