1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013-2019 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 #ifndef MICROPY_INCLUDED_PY_NATIVEGLUE_H
27 #define MICROPY_INCLUDED_PY_NATIVEGLUE_H
28 
29 #include <stdarg.h>
30 #include "py/obj.h"
31 #include "py/persistentcode.h"
32 #include "py/stream.h"
33 
34 typedef enum {
35     MP_F_CONST_NONE_OBJ = 0,
36     MP_F_CONST_FALSE_OBJ,
37     MP_F_CONST_TRUE_OBJ,
38     MP_F_CONVERT_OBJ_TO_NATIVE,
39     MP_F_CONVERT_NATIVE_TO_OBJ,
40     MP_F_NATIVE_SWAP_GLOBALS,
41     MP_F_LOAD_NAME,
42     MP_F_LOAD_GLOBAL,
43     MP_F_LOAD_BUILD_CLASS,
44     MP_F_LOAD_ATTR,
45     MP_F_LOAD_METHOD,
46     MP_F_LOAD_SUPER_METHOD,
47     MP_F_STORE_NAME,
48     MP_F_STORE_GLOBAL,
49     MP_F_STORE_ATTR,
50     MP_F_OBJ_SUBSCR,
51     MP_F_OBJ_IS_TRUE,
52     MP_F_UNARY_OP,
53     MP_F_BINARY_OP,
54     MP_F_BUILD_TUPLE,
55     MP_F_BUILD_LIST,
56     MP_F_BUILD_MAP,
57     MP_F_BUILD_SET,
58     MP_F_STORE_SET,
59     MP_F_LIST_APPEND,
60     MP_F_STORE_MAP,
61     MP_F_MAKE_FUNCTION_FROM_RAW_CODE,
62     MP_F_NATIVE_CALL_FUNCTION_N_KW,
63     MP_F_CALL_METHOD_N_KW,
64     MP_F_CALL_METHOD_N_KW_VAR,
65     MP_F_NATIVE_GETITER,
66     MP_F_NATIVE_ITERNEXT,
67     MP_F_NLR_PUSH,
68     MP_F_NLR_POP,
69     MP_F_NATIVE_RAISE,
70     MP_F_IMPORT_NAME,
71     MP_F_IMPORT_FROM,
72     MP_F_IMPORT_ALL,
73     MP_F_NEW_SLICE,
74     MP_F_UNPACK_SEQUENCE,
75     MP_F_UNPACK_EX,
76     MP_F_DELETE_NAME,
77     MP_F_DELETE_GLOBAL,
78     MP_F_MAKE_CLOSURE_FROM_RAW_CODE,
79     MP_F_ARG_CHECK_NUM_SIG,
80     MP_F_SETUP_CODE_STATE,
81     MP_F_SMALL_INT_FLOOR_DIVIDE,
82     MP_F_SMALL_INT_MODULO,
83     MP_F_NATIVE_YIELD_FROM,
84     MP_F_SETJMP,
85     MP_F_NUMBER_OF,
86 } mp_fun_kind_t;
87 
88 typedef struct _mp_fun_table_t {
89     mp_const_obj_t const_none;
90     mp_const_obj_t const_false;
91     mp_const_obj_t const_true;
92     mp_uint_t (*native_from_obj)(mp_obj_t obj, mp_uint_t type);
93     mp_obj_t (*native_to_obj)(mp_uint_t val, mp_uint_t type);
94     mp_obj_dict_t *(*swap_globals)(mp_obj_dict_t * new_globals);
95     mp_obj_t (*load_name)(qstr qst);
96     mp_obj_t (*load_global)(qstr qst);
97     mp_obj_t (*load_build_class)(void);
98     mp_obj_t (*load_attr)(mp_obj_t base, qstr attr);
99     void (*load_method)(mp_obj_t base, qstr attr, mp_obj_t *dest);
100     void (*load_super_method)(qstr attr, mp_obj_t *dest);
101     void (*store_name)(qstr qst, mp_obj_t obj);
102     void (*store_global)(qstr qst, mp_obj_t obj);
103     void (*store_attr)(mp_obj_t base, qstr attr, mp_obj_t val);
104     mp_obj_t (*obj_subscr)(mp_obj_t base, mp_obj_t index, mp_obj_t val);
105     bool (*obj_is_true)(mp_obj_t arg);
106     mp_obj_t (*unary_op)(mp_unary_op_t op, mp_obj_t arg);
107     mp_obj_t (*binary_op)(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs);
108     mp_obj_t (*new_tuple)(size_t n, const mp_obj_t *items);
109     mp_obj_t (*new_list)(size_t n, mp_obj_t *items);
110     mp_obj_t (*new_dict)(size_t n_args);
111     mp_obj_t (*new_set)(size_t n_args, mp_obj_t *items);
112     void (*set_store)(mp_obj_t self_in, mp_obj_t item);
113     mp_obj_t (*list_append)(mp_obj_t self_in, mp_obj_t arg);
114     mp_obj_t (*dict_store)(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
115     mp_obj_t (*make_function_from_raw_code)(const mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args);
116     mp_obj_t (*call_function_n_kw)(mp_obj_t fun_in, size_t n_args_kw, const mp_obj_t *args);
117     mp_obj_t (*call_method_n_kw)(size_t n_args, size_t n_kw, const mp_obj_t *args);
118     mp_obj_t (*call_method_n_kw_var)(bool have_self, size_t n_args_n_kw, const mp_obj_t *args);
119     mp_obj_t (*getiter)(mp_obj_t obj, mp_obj_iter_buf_t *iter);
120     mp_obj_t (*iternext)(mp_obj_iter_buf_t *iter);
121     unsigned int (*nlr_push)(nlr_buf_t *);
122     void (*nlr_pop)(void);
123     void (*raise)(mp_obj_t o);
124     mp_obj_t (*import_name)(qstr name, mp_obj_t fromlist, mp_obj_t level);
125     mp_obj_t (*import_from)(mp_obj_t module, qstr name);
126     void (*import_all)(mp_obj_t module);
127     mp_obj_t (*new_slice)(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
128     void (*unpack_sequence)(mp_obj_t seq, size_t num, mp_obj_t *items);
129     void (*unpack_ex)(mp_obj_t seq, size_t num, mp_obj_t *items);
130     void (*delete_name)(qstr qst);
131     void (*delete_global)(qstr qst);
132     mp_obj_t (*make_closure_from_raw_code)(const mp_raw_code_t *rc, mp_uint_t n_closed_over, const mp_obj_t *args);
133     void (*arg_check_num_sig)(size_t n_args, size_t n_kw, uint32_t sig);
134     void (*setup_code_state)(mp_code_state_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args);
135     mp_int_t (*small_int_floor_divide)(mp_int_t num, mp_int_t denom);
136     mp_int_t (*small_int_modulo)(mp_int_t dividend, mp_int_t divisor);
137     bool (*yield_from)(mp_obj_t gen, mp_obj_t send_value, mp_obj_t *ret_value);
138     void *setjmp_;
139     // Additional entries for dynamic runtime, starts at index 50
140     void *(*memset_)(void *s, int c, size_t n);
141     void *(*memmove_)(void *dest, const void *src, size_t n);
142     void *(*realloc_)(void *ptr, size_t n_bytes, bool allow_move);
143     int (*printf_)(const mp_print_t *print, const char *fmt, ...);
144     int (*vprintf_)(const mp_print_t *print, const char *fmt, va_list args);
145     #if defined(__GNUC__)
146     NORETURN // Only certain compilers support no-return attributes in function pointer declarations
147     #endif
148     void (*raise_msg)(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg);
149     const mp_obj_type_t *(*obj_get_type)(mp_const_obj_t o_in);
150     mp_obj_t (*obj_new_str)(const char *data, size_t len);
151     mp_obj_t (*obj_new_bytes)(const byte *data, size_t len);
152     mp_obj_t (*obj_new_bytearray_by_ref)(size_t n, void *items);
153     mp_obj_t (*obj_new_float_from_f)(float f);
154     mp_obj_t (*obj_new_float_from_d)(double d);
155     float (*obj_get_float_to_f)(mp_obj_t o);
156     double (*obj_get_float_to_d)(mp_obj_t o);
157     void (*get_buffer_raise)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
158     const mp_stream_p_t *(*get_stream_raise)(mp_obj_t self_in, int flags);
159     const mp_print_t *plat_print;
160     const mp_obj_type_t *type_type;
161     const mp_obj_type_t *type_str;
162     const mp_obj_type_t *type_list;
163     const mp_obj_type_t *type_dict;
164     const mp_obj_type_t *type_fun_builtin_0;
165     const mp_obj_type_t *type_fun_builtin_1;
166     const mp_obj_type_t *type_fun_builtin_2;
167     const mp_obj_type_t *type_fun_builtin_3;
168     const mp_obj_type_t *type_fun_builtin_var;
169     const mp_obj_fun_builtin_var_t *stream_read_obj;
170     const mp_obj_fun_builtin_var_t *stream_readinto_obj;
171     const mp_obj_fun_builtin_var_t *stream_unbuffered_readline_obj;
172     const mp_obj_fun_builtin_var_t *stream_write_obj;
173 } mp_fun_table_t;
174 
175 extern const mp_fun_table_t mp_fun_table;
176 
177 #endif // MICROPY_INCLUDED_PY_NATIVEGLUE_H
178