blob: bc2f4ff5e75091aeb8f3bfc910615ec3b45737a9 [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 George99886182015-04-06 22:38:53 +010035#include "py/bc.h"
Damien George110ba352014-08-28 23:37:02 +010036
Damien George110ba352014-08-28 23:37:02 +010037#if 0 // 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
Damien George8f54c082016-01-15 15:20:43 +000043#if MICROPY_EMIT_NATIVE
44
Damien George110ba352014-08-28 23:37:02 +010045// convert a Micro Python object to a valid native value based on type
46mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type) {
47 DEBUG_printf("mp_convert_obj_to_native(%p, " UINT_FMT ")\n", obj, type);
Damien George5f3e0052016-02-02 23:16:05 +000048 switch (type & 0xf) {
Damien George110ba352014-08-28 23:37:02 +010049 case MP_NATIVE_TYPE_OBJ: return (mp_uint_t)obj;
50 case MP_NATIVE_TYPE_BOOL:
Damien George5f3e0052016-02-02 23:16:05 +000051 case MP_NATIVE_TYPE_INT:
52 case MP_NATIVE_TYPE_UINT: return mp_obj_get_int_truncated(obj);
Damien George3e02b1d2016-02-09 13:29:20 +000053 default: { // cast obj to a pointer
Damien Georgee9dac3b2014-09-29 22:10:41 +010054 mp_buffer_info_t bufinfo;
Damien George3e02b1d2016-02-09 13:29:20 +000055 if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_RW)) {
56 return (mp_uint_t)bufinfo.buf;
57 } else {
58 // assume obj is an integer that represents an address
59 return mp_obj_get_int_truncated(obj);
60 }
Damien Georgee9dac3b2014-09-29 22:10:41 +010061 }
Damien George110ba352014-08-28 23:37:02 +010062 }
63}
64
Damien George8f54c082016-01-15 15:20:43 +000065#endif
66
67#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_THUMB
68
Damien George110ba352014-08-28 23:37:02 +010069// convert a native value to a Micro Python object based on type
70mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type) {
71 DEBUG_printf("mp_convert_native_to_obj(" UINT_FMT ", " UINT_FMT ")\n", val, type);
Damien George5f3e0052016-02-02 23:16:05 +000072 switch (type & 0xf) {
Damien George110ba352014-08-28 23:37:02 +010073 case MP_NATIVE_TYPE_OBJ: return (mp_obj_t)val;
Paul Sokolovsky1b586f32015-10-11 12:09:43 +030074 case MP_NATIVE_TYPE_BOOL: return mp_obj_new_bool(val);
Damien George110ba352014-08-28 23:37:02 +010075 case MP_NATIVE_TYPE_INT: return mp_obj_new_int(val);
76 case MP_NATIVE_TYPE_UINT: return mp_obj_new_int_from_uint(val);
Damien George5f3e0052016-02-02 23:16:05 +000077 default: // a pointer
78 // we return just the value of the pointer as an integer
79 return mp_obj_new_int_from_uint(val);
Damien George110ba352014-08-28 23:37:02 +010080 }
81}
82
Damien George8f54c082016-01-15 15:20:43 +000083#endif
84
85#if MICROPY_EMIT_NATIVE
86
Damien George110ba352014-08-28 23:37:02 +010087// wrapper that accepts n_args and n_kw in one argument
88// (native emitter can only pass at most 3 arguments to a function)
Damien George4abff752014-08-30 14:59:21 +010089mp_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 +010090 return mp_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
91}
92
93// wrapper that makes raise obj and raises it
Damien Georgeb6e6b522015-01-21 17:00:01 +000094// END_FINALLY opcode requires that we don't raise if o==None
95void mp_native_raise(mp_obj_t o) {
96 if (o != mp_const_none) {
97 nlr_raise(mp_make_raise_obj(o));
98 }
Damien George110ba352014-08-28 23:37:02 +010099}
100
101// these must correspond to the respective enum in runtime0.h
102void *const mp_fun_table[MP_F_NUMBER_OF] = {
103 mp_convert_obj_to_native,
104 mp_convert_native_to_obj,
Damien George110ba352014-08-28 23:37:02 +0100105 mp_load_name,
106 mp_load_global,
107 mp_load_build_class,
108 mp_load_attr,
109 mp_load_method,
110 mp_store_name,
111 mp_store_global,
112 mp_store_attr,
113 mp_obj_subscr,
114 mp_obj_is_true,
115 mp_unary_op,
116 mp_binary_op,
117 mp_obj_new_tuple,
118 mp_obj_new_list,
119 mp_obj_list_append,
120 mp_obj_new_dict,
121 mp_obj_dict_store,
122#if MICROPY_PY_BUILTINS_SET
123 mp_obj_new_set,
124 mp_obj_set_store,
125#endif
126 mp_make_function_from_raw_code,
127 mp_native_call_function_n_kw,
128 mp_call_method_n_kw,
Damien George78772ad2015-04-06 22:48:21 +0100129 mp_call_method_n_kw_var,
Damien George110ba352014-08-28 23:37:02 +0100130 mp_getiter,
131 mp_iternext,
132 nlr_push,
133 nlr_pop,
134 mp_native_raise,
135 mp_import_name,
136 mp_import_from,
137 mp_import_all,
138#if MICROPY_PY_BUILTINS_SLICE
139 mp_obj_new_slice,
140#endif
141 mp_unpack_sequence,
142 mp_unpack_ex,
Damien Georgee6ce10a2014-09-06 18:38:20 +0100143 mp_delete_name,
144 mp_delete_global,
Damien George4cd9ced2015-01-15 14:41:41 +0000145 mp_obj_new_cell,
146 mp_make_closure_from_raw_code,
Damien George99886182015-04-06 22:38:53 +0100147 mp_setup_code_state,
Damien George110ba352014-08-28 23:37:02 +0100148};
149
150/*
151void mp_f_vector(mp_fun_kind_t fun_kind) {
152 (mp_f_table[fun_kind])();
153}
154*/
155
156#endif // MICROPY_EMIT_NATIVE