Branch data Line data Source code
1 : : /*
2 : : * This file is part of the MicroPython 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 <stdarg.h>
28 : : #include <stdio.h>
29 : : #include <string.h>
30 : : #include <assert.h>
31 : :
32 : : #include "py/runtime.h"
33 : : #include "py/smallint.h"
34 : : #include "py/nativeglue.h"
35 : : #include "py/gc.h"
36 : :
37 : : #if MICROPY_DEBUG_VERBOSE // print debugging info
38 : : #define DEBUG_printf DEBUG_printf
39 : : #else // don't print debugging info
40 : : #define DEBUG_printf(...) (void)0
41 : : #endif
42 : :
43 : : #if MICROPY_EMIT_NATIVE
44 : :
45 : 1154 : int mp_native_type_from_qstr(qstr qst) {
46 [ + + + + : 1154 : switch (qst) {
+ + + +
+ ]
47 : : case MP_QSTR_object:
48 : : return MP_NATIVE_TYPE_OBJ;
49 : 16 : case MP_QSTR_bool:
50 : 16 : return MP_NATIVE_TYPE_BOOL;
51 : 314 : case MP_QSTR_int:
52 : 314 : return MP_NATIVE_TYPE_INT;
53 : 70 : case MP_QSTR_uint:
54 : 70 : return MP_NATIVE_TYPE_UINT;
55 : 20 : case MP_QSTR_ptr:
56 : 20 : return MP_NATIVE_TYPE_PTR;
57 : 42 : case MP_QSTR_ptr8:
58 : 42 : return MP_NATIVE_TYPE_PTR8;
59 : 28 : case MP_QSTR_ptr16:
60 : 28 : return MP_NATIVE_TYPE_PTR16;
61 : 36 : case MP_QSTR_ptr32:
62 : 36 : return MP_NATIVE_TYPE_PTR32;
63 : 620 : default:
64 : 620 : return -1;
65 : : }
66 : : }
67 : :
68 : : // convert a MicroPython object to a valid native value based on type
69 : 1540 : mp_uint_t mp_native_from_obj(mp_obj_t obj, mp_uint_t type) {
70 : 1540 : DEBUG_printf("mp_native_from_obj(%p, " UINT_FMT ")\n", obj, type);
71 [ - + + + ]: 1540 : switch (type & 0xf) {
72 : 0 : case MP_NATIVE_TYPE_OBJ:
73 : 0 : return (mp_uint_t)obj;
74 : 32 : case MP_NATIVE_TYPE_BOOL:
75 : 32 : return mp_obj_is_true(obj);
76 : 1372 : case MP_NATIVE_TYPE_INT:
77 : : case MP_NATIVE_TYPE_UINT:
78 : 1372 : return mp_obj_get_int_truncated(obj);
79 : 136 : default: { // cast obj to a pointer
80 : 136 : mp_buffer_info_t bufinfo;
81 [ + + ]: 136 : if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_READ)) {
82 : 128 : return (mp_uint_t)bufinfo.buf;
83 : : } else {
84 : : // assume obj is an integer that represents an address
85 : 8 : return mp_obj_get_int_truncated(obj);
86 : : }
87 : : }
88 : : }
89 : : }
90 : :
91 : : #endif
92 : :
93 : : #if MICROPY_EMIT_MACHINE_CODE
94 : :
95 : : // convert a native value to a MicroPython object based on type
96 : 1192 : mp_obj_t mp_native_to_obj(mp_uint_t val, mp_uint_t type) {
97 : 1192 : DEBUG_printf("mp_native_to_obj(" UINT_FMT ", " UINT_FMT ")\n", val, type);
98 [ - + + + : 1192 : switch (type & 0xf) {
- + ]
99 : 0 : case MP_NATIVE_TYPE_OBJ:
100 : 0 : return (mp_obj_t)val;
101 : 104 : case MP_NATIVE_TYPE_BOOL:
102 [ + + ]: 104 : return mp_obj_new_bool(val);
103 : 912 : case MP_NATIVE_TYPE_INT:
104 : 912 : return mp_obj_new_int(val);
105 : 172 : case MP_NATIVE_TYPE_UINT:
106 : 172 : return mp_obj_new_int_from_uint(val);
107 : 0 : case MP_NATIVE_TYPE_QSTR:
108 : 0 : return MP_OBJ_NEW_QSTR(val);
109 : 4 : default: // a pointer
110 : : // we return just the value of the pointer as an integer
111 : 4 : return mp_obj_new_int_from_uint(val);
112 : : }
113 : : }
114 : :
115 : : #endif
116 : :
117 : : #if MICROPY_EMIT_NATIVE && !MICROPY_DYNAMIC_COMPILER
118 : :
119 : : #if !MICROPY_PY_BUILTINS_SET
120 : : mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items) {
121 : : (void)n_args;
122 : : (void)items;
123 : : mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("set unsupported"));
124 : : }
125 : :
126 : : void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
127 : : (void)self_in;
128 : : (void)item;
129 : : mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("set unsupported"));
130 : : }
131 : : #endif
132 : :
133 : : #if !MICROPY_PY_BUILTINS_SLICE
134 : : mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
135 : : (void)ostart;
136 : : (void)ostop;
137 : : (void)ostep;
138 : : mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("slice unsupported"));
139 : : }
140 : : #endif
141 : :
142 : 647401 : STATIC mp_obj_dict_t *mp_native_swap_globals(mp_obj_dict_t *new_globals) {
143 [ + + ]: 647401 : if (new_globals == NULL) {
144 : : // Globals were the originally the same so don't restore them
145 : : return NULL;
146 : : }
147 : 645725 : mp_obj_dict_t *old_globals = mp_globals_get();
148 [ + + ]: 645725 : if (old_globals == new_globals) {
149 : : // Don't set globals if they are the same, and return NULL to indicate this
150 : : return NULL;
151 : : }
152 : 466608 : mp_globals_set(new_globals);
153 : 466608 : return old_globals;
154 : : }
155 : :
156 : : // wrapper that accepts n_args and n_kw in one argument
157 : : // (native emitter can only pass at most 3 arguments to a function)
158 : 826027 : STATIC mp_obj_t mp_native_call_function_n_kw(mp_obj_t fun_in, size_t n_args_kw, const mp_obj_t *args) {
159 : 826027 : return mp_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
160 : : }
161 : :
162 : : // wrapper that makes raise obj and raises it
163 : : // END_FINALLY opcode requires that we don't raise if o==None
164 : 170328 : STATIC void mp_native_raise(mp_obj_t o) {
165 [ + + ]: 170328 : if (o != MP_OBJ_NULL && o != mp_const_none) {
166 : 156031 : nlr_raise(mp_make_raise_obj(o));
167 : : }
168 : 14297 : }
169 : :
170 : : // wrapper that handles iterator buffer
171 : 161236 : STATIC mp_obj_t mp_native_getiter(mp_obj_t obj, mp_obj_iter_buf_t *iter) {
172 [ + + ]: 161236 : if (iter == NULL) {
173 : 78313 : return mp_getiter(obj, NULL);
174 : : } else {
175 : 82923 : obj = mp_getiter(obj, iter);
176 [ + + ]: 82919 : if (obj != MP_OBJ_FROM_PTR(iter)) {
177 : : // Iterator didn't use the stack so indicate that with MP_OBJ_NULL.
178 : 80825 : iter->base.type = MP_OBJ_NULL;
179 : 80825 : iter->buf[0] = obj;
180 : : }
181 : 82919 : return NULL;
182 : : }
183 : : }
184 : :
185 : : // wrapper that handles iterator buffer
186 : 123286 : STATIC mp_obj_t mp_native_iternext(mp_obj_iter_buf_t *iter) {
187 : 123286 : mp_obj_t obj;
188 [ + + ]: 123286 : if (iter->base.type == MP_OBJ_NULL) {
189 : 108843 : obj = iter->buf[0];
190 : : } else {
191 : : obj = MP_OBJ_FROM_PTR(iter);
192 : : }
193 : 123286 : return mp_iternext(obj);
194 : : }
195 : :
196 : 156063 : STATIC bool mp_native_yield_from(mp_obj_t gen, mp_obj_t send_value, mp_obj_t *ret_value) {
197 : 156063 : mp_vm_return_kind_t ret_kind;
198 : 156063 : nlr_buf_t nlr_buf;
199 : 156063 : mp_obj_t throw_value = *ret_value;
200 [ + + ]: 156063 : if (nlr_push(&nlr_buf) == 0) {
201 [ + + ]: 156063 : if (throw_value != MP_OBJ_NULL) {
202 : 58 : send_value = MP_OBJ_NULL;
203 : : }
204 : 156063 : ret_kind = mp_resume(gen, send_value, throw_value, ret_value);
205 : 78350 : nlr_pop();
206 : : } else {
207 : 77713 : ret_kind = MP_VM_RETURN_EXCEPTION;
208 : 77713 : *ret_value = nlr_buf.ret_val;
209 : : }
210 : :
211 [ + + ]: 156063 : if (ret_kind == MP_VM_RETURN_YIELD) {
212 : : return true;
213 [ + + ]: 78131 : } else if (ret_kind == MP_VM_RETURN_NORMAL) {
214 [ - + ]: 157 : if (*ret_value == MP_OBJ_STOP_ITERATION) {
215 : 0 : *ret_value = mp_const_none;
216 : : }
217 : : } else {
218 [ - + ]: 77974 : assert(ret_kind == MP_VM_RETURN_EXCEPTION);
219 [ + + ]: 77974 : if (!mp_obj_exception_match(*ret_value, MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
220 : 276 : nlr_raise(*ret_value);
221 : : }
222 : 77698 : *ret_value = mp_obj_exception_get_value(*ret_value);
223 : : }
224 : :
225 [ + + + + ]: 77855 : if (throw_value != MP_OBJ_NULL && mp_obj_exception_match(throw_value, MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) {
226 : 4 : nlr_raise(mp_make_raise_obj(throw_value));
227 : : }
228 : :
229 : : return false;
230 : : }
231 : :
232 : : #if !MICROPY_PY_BUILTINS_FLOAT
233 : :
234 : : STATIC mp_obj_t mp_obj_new_float_from_f(float f) {
235 : : (void)f;
236 : : mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("float unsupported"));
237 : : }
238 : :
239 : : STATIC mp_obj_t mp_obj_new_float_from_d(double d) {
240 : : (void)d;
241 : : mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("float unsupported"));
242 : : }
243 : :
244 : : STATIC float mp_obj_get_float_to_f(mp_obj_t o) {
245 : : (void)o;
246 : : mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("float unsupported"));
247 : : }
248 : :
249 : : STATIC double mp_obj_get_float_to_d(mp_obj_t o) {
250 : : (void)o;
251 : : mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("float unsupported"));
252 : : }
253 : :
254 : : #endif
255 : :
256 : : // these must correspond to the respective enum in nativeglue.h
257 : : const mp_fun_table_t mp_fun_table = {
258 : : mp_const_none,
259 : : mp_const_false,
260 : : mp_const_true,
261 : : mp_native_from_obj,
262 : : mp_native_to_obj,
263 : : mp_native_swap_globals,
264 : : mp_load_name,
265 : : mp_load_global,
266 : : mp_load_build_class,
267 : : mp_load_attr,
268 : : mp_load_method,
269 : : mp_load_super_method,
270 : : mp_store_name,
271 : : mp_store_global,
272 : : mp_store_attr,
273 : : mp_obj_subscr,
274 : : mp_obj_is_true,
275 : : mp_unary_op,
276 : : mp_binary_op,
277 : : mp_obj_new_tuple,
278 : : mp_obj_new_list,
279 : : mp_obj_new_dict,
280 : : mp_obj_new_set,
281 : : mp_obj_set_store,
282 : : mp_obj_list_append,
283 : : mp_obj_dict_store,
284 : : mp_make_function_from_raw_code,
285 : : mp_native_call_function_n_kw,
286 : : mp_call_method_n_kw,
287 : : mp_call_method_n_kw_var,
288 : : mp_native_getiter,
289 : : mp_native_iternext,
290 : : #if MICROPY_NLR_SETJMP
291 : : nlr_push_tail,
292 : : #else
293 : : nlr_push,
294 : : #endif
295 : : nlr_pop,
296 : : mp_native_raise,
297 : : mp_import_name,
298 : : mp_import_from,
299 : : mp_import_all,
300 : : mp_obj_new_slice,
301 : : mp_unpack_sequence,
302 : : mp_unpack_ex,
303 : : mp_delete_name,
304 : : mp_delete_global,
305 : : mp_obj_new_closure,
306 : : mp_arg_check_num_sig,
307 : : mp_setup_code_state_native,
308 : : mp_small_int_floor_divide,
309 : : mp_small_int_modulo,
310 : : mp_native_yield_from,
311 : : #if MICROPY_NLR_SETJMP
312 : : setjmp,
313 : : #else
314 : : NULL,
315 : : #endif
316 : : // Additional entries for dynamic runtime, starts at index 50
317 : : memset,
318 : : memmove,
319 : : gc_realloc,
320 : : mp_printf,
321 : : mp_vprintf,
322 : : mp_raise_msg,
323 : : mp_obj_get_type,
324 : : mp_obj_new_str,
325 : : mp_obj_new_bytes,
326 : : mp_obj_new_bytearray_by_ref,
327 : : mp_obj_new_float_from_f,
328 : : mp_obj_new_float_from_d,
329 : : mp_obj_get_float_to_f,
330 : : mp_obj_get_float_to_d,
331 : : mp_get_buffer_raise,
332 : : mp_get_stream_raise,
333 : : &mp_plat_print,
334 : : &mp_type_type,
335 : : &mp_type_str,
336 : : &mp_type_list,
337 : : &mp_type_dict,
338 : : &mp_type_fun_builtin_0,
339 : : &mp_type_fun_builtin_1,
340 : : &mp_type_fun_builtin_2,
341 : : &mp_type_fun_builtin_3,
342 : : &mp_type_fun_builtin_var,
343 : : &mp_stream_read_obj,
344 : : &mp_stream_readinto_obj,
345 : : &mp_stream_unbuffered_readline_obj,
346 : : &mp_stream_write_obj,
347 : : };
348 : :
349 : : #elif MICROPY_EMIT_NATIVE && MICROPY_DYNAMIC_COMPILER
350 : :
351 : : const int mp_fun_table;
352 : :
353 : : #endif // MICROPY_EMIT_NATIVE
|