Skip to content

Commit c624a5c

Browse files
BrianPughdpgeorge
authored andcommitted
py/dynruntime: Export mp_load_method_maybe and mp_arg_parse_all* funcs.
Also define `mp_type_bytearray`. These all help to write native modules. Signed-off-by: Brian Pugh <[email protected]> Signed-off-by: Damien George <[email protected]>
1 parent a196468 commit c624a5c

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

py/dynruntime.h

+8
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ static inline void *m_realloc_dyn(void *ptr, size_t new_num_bytes) {
8888
#define mp_type_int (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_int)))
8989
#define mp_type_str (*mp_fun_table.type_str)
9090
#define mp_type_bytes (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_bytes)))
91+
#define mp_type_bytearray (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_bytearray)))
9192
#define mp_type_tuple (*((mp_obj_base_t *)mp_const_empty_tuple)->type)
9293
#define mp_type_list (*mp_fun_table.type_list)
9394
#define mp_type_Exception (*mp_fun_table.type_Exception)
@@ -194,6 +195,7 @@ static inline void *mp_obj_malloc_helper_dyn(size_t num_bytes, const mp_obj_type
194195
#define mp_load_global(qst) (mp_fun_table.load_global((qst)))
195196
#define mp_load_attr(base, attr) (mp_fun_table.load_attr((base), (attr)))
196197
#define mp_load_method(base, attr, dest) (mp_fun_table.load_method((base), (attr), (dest)))
198+
#define mp_load_method_maybe(base, attr, dest) (mp_fun_table.load_method_maybe((base), (attr), (dest)))
197199
#define mp_load_super_method(attr, dest) (mp_fun_table.load_super_method((attr), (dest)))
198200
#define mp_store_name(qst, obj) (mp_fun_table.store_name((qst), (obj)))
199201
#define mp_store_global(qst, obj) (mp_fun_table.store_global((qst), (obj)))
@@ -211,6 +213,12 @@ static inline void *mp_obj_malloc_helper_dyn(size_t num_bytes, const mp_obj_type
211213
#define mp_arg_check_num(n_args, n_kw, n_args_min, n_args_max, takes_kw) \
212214
(mp_fun_table.arg_check_num_sig((n_args), (n_kw), MP_OBJ_FUN_MAKE_SIG((n_args_min), (n_args_max), (takes_kw))))
213215

216+
#define mp_arg_parse_all(n_pos, pos, kws, n_allowed, allowed, out_vals) \
217+
(mp_fun_table.arg_parse_all((n_pos), (pos), (kws), (n_allowed), (allowed), (out_vals)))
218+
219+
#define mp_arg_parse_all_kw_array(n_pos, n_kw, args, n_allowed, allowed, out_vals) \
220+
(mp_fun_table.arg_parse_all_kw_array((n_pos), (n_kw), (args), (n_allowed), (allowed), (out_vals)))
221+
214222
#define MP_DYNRUNTIME_INIT_ENTRY \
215223
mp_obj_t old_globals = mp_fun_table.swap_globals(self->context->module.globals); \
216224
mp_raw_code_truncated_t rc; \

py/nativeglue.c

+3
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,11 @@ const mp_fun_table_t mp_fun_table = {
329329
mp_obj_new_float_from_d,
330330
mp_obj_get_float_to_f,
331331
mp_obj_get_float_to_d,
332+
mp_load_method_maybe,
332333
mp_get_buffer,
333334
mp_get_stream_raise,
335+
mp_arg_parse_all,
336+
mp_arg_parse_all_kw_array,
334337
mp_binary_get_size,
335338
mp_binary_get_val_array,
336339
mp_binary_set_val_array,

py/nativeglue.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,16 @@ typedef struct _mp_fun_table_t {
154154
mp_obj_t (*obj_new_float_from_d)(double d);
155155
float (*obj_get_float_to_f)(mp_obj_t o);
156156
double (*obj_get_float_to_d)(mp_obj_t o);
157+
void (*load_method_maybe)(mp_obj_t base, qstr attr, mp_obj_t *dest);
157158
bool (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
158159
const mp_stream_p_t *(*get_stream_raise)(mp_obj_t self_in, int flags);
160+
void (*arg_parse_all)(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
161+
void (*arg_parse_all_kw_array)(size_t n_pos, size_t n_kw, const mp_obj_t *args, size_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
159162
size_t (*binary_get_size)(char struct_type, char val_type, size_t *palign);
160163
mp_obj_t (*binary_get_val_array)(char typecode, void *p, size_t index);
161164
void (*binary_set_val_array)(char typecode, void *p, size_t index, mp_obj_t val_in);
162165
const mp_print_t *plat_print;
163-
// The following entries start at index 70 and are referenced by tools-mpy_ld.py,
166+
// The following entries start at index 73 and are referenced by tools-mpy_ld.py,
164167
// see constant MP_FUN_TABLE_MP_TYPE_TYPE_OFFSET.
165168
const mp_obj_type_t *type_type;
166169
const mp_obj_type_t *type_str;

tests/micropython/import_mpy_native_gc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ def open(self, path, mode):
5252
# cat features0.mpy | python -c 'import sys; print(sys.stdin.buffer.read())'
5353
features0_file_contents = {
5454
# -march=x64
55-
0x806: b'M\x06\x0b\x1f\x02\x004build/features0.native.mpy\x00\x12factorial\x00\x8a\x02\xe9/\x00\x00\x00SH\x8b\x1d\x83\x00\x00\x00\xbe\x02\x00\x00\x00\xffS\x18\xbf\x01\x00\x00\x00H\x85\xc0u\x0cH\x8bC \xbe\x02\x00\x00\x00[\xff\xe0H\x0f\xaf\xf8H\xff\xc8\xeb\xe6ATUSH\x8b\x1dQ\x00\x00\x00H\x8bG\x08L\x8bc(H\x8bx\x08A\xff\xd4H\x8d5+\x00\x00\x00H\x89\xc5H\x8b\x059\x00\x00\x00\x0f\xb7x\x02\xffShH\x89\xefA\xff\xd4H\x8b\x03[]A\\\xc3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x11$\r&\xa9 \x01"\xff',
55+
0x806: b'M\x06\x0b\x1f\x02\x004build/features0.native.mpy\x00\x12factorial\x00\x8a\x02\xe9/\x00\x00\x00SH\x8b\x1d\x83\x00\x00\x00\xbe\x02\x00\x00\x00\xffS\x18\xbf\x01\x00\x00\x00H\x85\xc0u\x0cH\x8bC \xbe\x02\x00\x00\x00[\xff\xe0H\x0f\xaf\xf8H\xff\xc8\xeb\xe6ATUSH\x8b\x1dQ\x00\x00\x00H\x8bG\x08L\x8bc(H\x8bx\x08A\xff\xd4H\x8d5+\x00\x00\x00H\x89\xc5H\x8b\x059\x00\x00\x00\x0f\xb7x\x02\xffShH\x89\xefA\xff\xd4H\x8b\x03[]A\\\xc3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x11$\r&\xaf \x01"\xff',
5656
# -march=armv6m
57-
0x1006: b"M\x06\x13\x1f\x02\x004build/features0.native.mpy\x00\x12factorial\x00\x88\x02\x18\xe0\x00\x00\x10\xb5\tK\tJ{D\x9cX\x02!\xe3h\x98G\x03\x00\x01 \x00+\x02\xd0XC\x01;\xfa\xe7\x02!#i\x98G\x10\xbd\xc0Fj\x00\x00\x00\x00\x00\x00\x00\xf8\xb5\nN\nK~D\xf4XChgiXh\xb8G\x05\x00\x07K\x08I\xf3XyDX\x88ck\x98G(\x00\xb8G h\xf8\xbd\xc0F:\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x1e\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x11<\r>\xa98\x01:\xff",
57+
0x1006: b"M\x06\x13\x1f\x02\x004build/features0.native.mpy\x00\x12factorial\x00\x88\x02\x18\xe0\x00\x00\x10\xb5\tK\tJ{D\x9cX\x02!\xe3h\x98G\x03\x00\x01 \x00+\x02\xd0XC\x01;\xfa\xe7\x02!#i\x98G\x10\xbd\xc0Fj\x00\x00\x00\x00\x00\x00\x00\xf8\xb5\nN\nK~D\xf4XChgiXh\xb8G\x05\x00\x07K\x08I\xf3XyDX\x88ck\x98G(\x00\xb8G h\xf8\xbd\xc0F:\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x1e\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x11<\r>\xaf8\x01:\xff",
5858
}
5959

6060
# Populate armv7m-derived archs based on armv6m.

tools/mpy_ld.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
MP_SCOPE_FLAG_VIPERRODATA = 0x20
5353
MP_SCOPE_FLAG_VIPERBSS = 0x40
5454
MP_SMALL_INT_BITS = 31
55-
MP_FUN_TABLE_MP_TYPE_TYPE_OFFSET = 70
55+
MP_FUN_TABLE_MP_TYPE_TYPE_OFFSET = 73
5656

5757
# ELF constants
5858
R_386_32 = 1

0 commit comments

Comments
 (0)