From d2276f0d41c2fa66a224725fdb2411846c91cf1a Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 25 Mar 2024 13:10:12 +1100 Subject: [PATCH] py/dynruntime: Add mp_binary_get_size/get_val_array/set_val_array. These are needed to read/write array.array objects, which is useful in native code to provide fast extensions that work with big arrays of data. Signed-off-by: Damien George --- py/dynruntime.h | 5 +++++ py/nativeglue.c | 4 ++++ py/nativeglue.h | 5 +++++ tools/mpy_ld.py | 3 ++- 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/py/dynruntime.h b/py/dynruntime.h index 7765fca40d..d07b1dce2c 100644 --- a/py/dynruntime.h +++ b/py/dynruntime.h @@ -29,6 +29,7 @@ // This header file contains definitions to dynamically implement the static // MicroPython runtime API defined in py/obj.h and py/runtime.h. +#include "py/binary.h" #include "py/nativeglue.h" #include "py/objfun.h" #include "py/objstr.h" @@ -184,6 +185,10 @@ static inline void *mp_obj_malloc_helper_dyn(size_t num_bytes, const mp_obj_type /******************************************************************************/ // General runtime functions +#define mp_binary_get_size(struct_type, val_type, palign) (mp_fun_table.binary_get_size((struct_type), (val_type), (palign))) +#define mp_binary_get_val_array(typecode, p, index) (mp_fun_table.binary_get_val_array((typecode), (p), (index))) +#define mp_binary_set_val_array(typecode, p, index, val_in) (mp_fun_table.binary_set_val_array((typecode), (p), (index), (val_in))) + #define mp_load_name(qst) (mp_fun_table.load_name((qst))) #define mp_load_global(qst) (mp_fun_table.load_global((qst))) #define mp_load_attr(base, attr) (mp_fun_table.load_attr((base), (attr))) diff --git a/py/nativeglue.c b/py/nativeglue.c index 4cd090c0a2..ba3d93f760 100644 --- a/py/nativeglue.c +++ b/py/nativeglue.c @@ -29,6 +29,7 @@ #include #include +#include "py/binary.h" #include "py/runtime.h" #include "py/smallint.h" #include "py/nativeglue.h" @@ -330,6 +331,9 @@ const mp_fun_table_t mp_fun_table = { mp_obj_get_float_to_d, mp_get_buffer, mp_get_stream_raise, + mp_binary_get_size, + mp_binary_get_val_array, + mp_binary_set_val_array, &mp_plat_print, &mp_type_type, &mp_type_str, diff --git a/py/nativeglue.h b/py/nativeglue.h index 113f5fde65..1fa8593345 100644 --- a/py/nativeglue.h +++ b/py/nativeglue.h @@ -156,7 +156,12 @@ typedef struct _mp_fun_table_t { double (*obj_get_float_to_d)(mp_obj_t o); bool (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags); const mp_stream_p_t *(*get_stream_raise)(mp_obj_t self_in, int flags); + size_t (*binary_get_size)(char struct_type, char val_type, size_t *palign); + mp_obj_t (*binary_get_val_array)(char typecode, void *p, size_t index); + void (*binary_set_val_array)(char typecode, void *p, size_t index, mp_obj_t val_in); const mp_print_t *plat_print; + // The following entries start at index 70 and are referenced by tools-mpy_ld.py, + // see constant MP_FUN_TABLE_MP_TYPE_TYPE_OFFSET. const mp_obj_type_t *type_type; const mp_obj_type_t *type_str; const mp_obj_type_t *type_list; diff --git a/tools/mpy_ld.py b/tools/mpy_ld.py index d7d0b945c5..b768b109a8 100755 --- a/tools/mpy_ld.py +++ b/tools/mpy_ld.py @@ -52,6 +52,7 @@ MP_SCOPE_FLAG_VIPERRELOC = 0x10 MP_SCOPE_FLAG_VIPERRODATA = 0x20 MP_SCOPE_FLAG_VIPERBSS = 0x40 MP_SMALL_INT_BITS = 31 +MP_FUN_TABLE_MP_TYPE_TYPE_OFFSET = 70 # ELF constants R_386_32 = 1 @@ -754,7 +755,7 @@ def link_objects(env, native_qstr_vals_len): # Resolve unknown symbols mp_fun_table_sec = Section(".external.mp_fun_table", b"", 0) fun_table = { - key: 67 + idx + key: MP_FUN_TABLE_MP_TYPE_TYPE_OFFSET + idx for idx, key in enumerate( [ "mp_type_type",