From 8bf6e22a2e452886183e54a2edd775fd31cee199 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 10 Aug 2025 11:13:51 -0500 Subject: [PATCH] core: Introduce MP_ROM_TABLE. Once all ROM tables used in a given port are converted to use this macro, investigation can commence into alternate memory representations such as the one proposed by the ROM_TABLE_SEPARATE macro. vim command for transforming map entries (split over 2 lines): ``` :'<,'>s/{ MP_ROM_QSTR(\(.*\)), \(MP_[A-Z_]*\)(\(.*\)) }, /((\1, \2, \3)) ``` This commit updates all tables used in ports/minimal. Signed-off-by: Jeff Epler --- py/makemoduledefs.py | 2 +- py/obj.h | 1 + py/objdict.c | 33 +++++++++++++++++---------------- py/objgenerator.c | 12 ++++++------ py/objint.c | 8 ++++---- py/objlist.c | 26 +++++++++++++------------- py/objmodule.c | 10 +++++----- py/objtuple.c | 8 ++++---- py/romtable.h | 6 +++--- 9 files changed, 54 insertions(+), 52 deletions(-) diff --git a/py/makemoduledefs.py b/py/makemoduledefs.py index 29162ab387..ccf0c4462e 100644 --- a/py/makemoduledefs.py +++ b/py/makemoduledefs.py @@ -79,7 +79,7 @@ def generate_module_table_header(modules): ( "extern const struct _mp_obj_module_t {obj_module};\n" "#undef {mod_def}\n" - "#define {mod_def} {{ MP_ROM_QSTR(MP_QSTR_{module_name}), MP_ROM_PTR(&{obj_module}) }},\n" + "#define {mod_def} (( MP_QSTR_{module_name}, MP_ROM_PTR, &{obj_module} ))\n" ).format( module_name=module_name, obj_module=obj_module, diff --git a/py/obj.h b/py/obj.h index 4ac0cc0c61..ae7b39aadc 100644 --- a/py/obj.h +++ b/py/obj.h @@ -32,6 +32,7 @@ #include "py/misc.h" #include "py/qstr.h" #include "py/mpprint.h" +#include "py/romtable.h" #include "py/runtime0.h" // This is the definition of the opaque MicroPython object type. diff --git a/py/objdict.c b/py/objdict.c index 451e873290..686eb0c269 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -590,24 +590,25 @@ static mp_obj_t dict_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) { /******************************************************************************/ /* dict constructors & public C API */ -static const mp_rom_map_elem_t dict_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&dict_clear_obj) }, - { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&dict_copy_obj) }, +#include "py/romtable.h" +MP_ROM_TABLE(static, dict_locals_dict_table, + ((MP_QSTR_clear, MP_ROM_PTR, &dict_clear_obj)) + ((MP_QSTR_copy, MP_ROM_PTR, &dict_copy_obj)) #if MICROPY_PY_BUILTINS_DICT_FROMKEYS - { MP_ROM_QSTR(MP_QSTR_fromkeys), MP_ROM_PTR(&dict_fromkeys_obj) }, + ((MP_QSTR_fromkeys, MP_ROM_PTR, &dict_fromkeys_obj)) #endif - { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&dict_get_obj) }, - { MP_ROM_QSTR(MP_QSTR_items), MP_ROM_PTR(&dict_items_obj) }, - { MP_ROM_QSTR(MP_QSTR_keys), MP_ROM_PTR(&dict_keys_obj) }, - { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&dict_pop_obj) }, - { MP_ROM_QSTR(MP_QSTR_popitem), MP_ROM_PTR(&dict_popitem_obj) }, - { MP_ROM_QSTR(MP_QSTR_setdefault), MP_ROM_PTR(&dict_setdefault_obj) }, - { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&dict_update_obj) }, - { MP_ROM_QSTR(MP_QSTR_values), MP_ROM_PTR(&dict_values_obj) }, - { MP_ROM_QSTR(MP_QSTR___getitem__), MP_ROM_PTR(&mp_op_getitem_obj) }, - { MP_ROM_QSTR(MP_QSTR___setitem__), MP_ROM_PTR(&mp_op_setitem_obj) }, - { MP_ROM_QSTR(MP_QSTR___delitem__), MP_ROM_PTR(&mp_op_delitem_obj) }, -}; + ((MP_QSTR_get, MP_ROM_PTR, &dict_get_obj)) + ((MP_QSTR_items, MP_ROM_PTR, &dict_items_obj)) + ((MP_QSTR_keys, MP_ROM_PTR, &dict_keys_obj)) + ((MP_QSTR_pop, MP_ROM_PTR, &dict_pop_obj)) + ((MP_QSTR_popitem, MP_ROM_PTR, &dict_popitem_obj)) + ((MP_QSTR_setdefault, MP_ROM_PTR, &dict_setdefault_obj)) + ((MP_QSTR_update, MP_ROM_PTR, &dict_update_obj)) + ((MP_QSTR_values, MP_ROM_PTR, &dict_values_obj)) + ((MP_QSTR___getitem__, MP_ROM_PTR, &mp_op_getitem_obj)) + ((MP_QSTR___setitem__, MP_ROM_PTR, &mp_op_setitem_obj)) + ((MP_QSTR___delitem__, MP_ROM_PTR, &mp_op_delitem_obj)) + ); static MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table); diff --git a/py/objgenerator.c b/py/objgenerator.c index df9701094b..58928ddfaf 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -343,14 +343,14 @@ static mp_obj_t gen_instance_pend_throw(mp_obj_t self_in, mp_obj_t exc_in) { static MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_pend_throw_obj, gen_instance_pend_throw); #endif -static const mp_rom_map_elem_t gen_instance_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&gen_instance_close_obj) }, - { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&gen_instance_send_obj) }, - { MP_ROM_QSTR(MP_QSTR_throw), MP_ROM_PTR(&gen_instance_throw_obj) }, +MP_ROM_TABLE(static, gen_instance_locals_dict_table, + ((MP_QSTR_close, MP_ROM_PTR, &gen_instance_close_obj)) + ((MP_QSTR_send, MP_ROM_PTR, &gen_instance_send_obj)) + ((MP_QSTR_throw, MP_ROM_PTR, &gen_instance_throw_obj)) #if MICROPY_PY_GENERATOR_PEND_THROW - { MP_ROM_QSTR(MP_QSTR_pend_throw), MP_ROM_PTR(&gen_instance_pend_throw_obj) }, + ((MP_QSTR_pend_throw, MP_ROM_PTR, &gen_instance_pend_throw_obj)) #endif -}; + ); static MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table); diff --git a/py/objint.c b/py/objint.c index 87d8a27852..c60d944b5c 100644 --- a/py/objint.c +++ b/py/objint.c @@ -471,10 +471,10 @@ static mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) { } static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_to_bytes_obj, 1, 4, int_to_bytes); -static const mp_rom_map_elem_t int_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_from_bytes), MP_ROM_PTR(&int_from_bytes_obj) }, - { MP_ROM_QSTR(MP_QSTR_to_bytes), MP_ROM_PTR(&int_to_bytes_obj) }, -}; +MP_ROM_TABLE(static, int_locals_dict_table, + ((MP_QSTR_from_bytes, MP_ROM_PTR, &int_from_bytes_obj)) + ((MP_QSTR_to_bytes, MP_ROM_PTR, &int_to_bytes_obj)) + ); static MP_DEFINE_CONST_DICT(int_locals_dict, int_locals_dict_table); diff --git a/py/objlist.c b/py/objlist.c index 71414a849f..32a3621e2b 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -423,19 +423,19 @@ static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, mp_obj_list_remove); static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse); static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 1, mp_obj_list_sort); -static const mp_rom_map_elem_t list_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&list_append_obj) }, - { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&list_clear_obj) }, - { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&list_copy_obj) }, - { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&list_count_obj) }, - { MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&list_extend_obj) }, - { MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&list_index_obj) }, - { MP_ROM_QSTR(MP_QSTR_insert), MP_ROM_PTR(&list_insert_obj) }, - { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&list_pop_obj) }, - { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&list_remove_obj) }, - { MP_ROM_QSTR(MP_QSTR_reverse), MP_ROM_PTR(&list_reverse_obj) }, - { MP_ROM_QSTR(MP_QSTR_sort), MP_ROM_PTR(&list_sort_obj) }, -}; +MP_ROM_TABLE(static, list_locals_dict_table, + ((MP_QSTR_append, MP_ROM_PTR, &list_append_obj)) + ((MP_QSTR_clear, MP_ROM_PTR, &list_clear_obj)) + ((MP_QSTR_copy, MP_ROM_PTR, &list_copy_obj)) + ((MP_QSTR_count, MP_ROM_PTR, &list_count_obj)) + ((MP_QSTR_extend, MP_ROM_PTR, &list_extend_obj)) + ((MP_QSTR_index, MP_ROM_PTR, &list_index_obj)) + ((MP_QSTR_insert, MP_ROM_PTR, &list_insert_obj)) + ((MP_QSTR_pop, MP_ROM_PTR, &list_pop_obj)) + ((MP_QSTR_remove, MP_ROM_PTR, &list_remove_obj)) + ((MP_QSTR_reverse, MP_ROM_PTR, &list_reverse_obj)) + ((MP_QSTR_sort, MP_ROM_PTR, &list_sort_obj)) + ); static MP_DEFINE_CONST_DICT(list_locals_dict, list_locals_dict_table); diff --git a/py/objmodule.c b/py/objmodule.c index 5ce373b83d..9940be3ef1 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -146,16 +146,16 @@ mp_obj_t mp_obj_new_module(qstr module_name) { /******************************************************************************/ // Global module table and related functions -static const mp_rom_map_elem_t mp_builtin_module_table[] = { - // built-in modules declared with MP_REGISTER_MODULE() +#include "py/romtable.h" +MP_ROM_TABLE(static, mp_builtin_module_table, MICROPY_REGISTERED_MODULES -}; + ); MP_DEFINE_CONST_MAP(mp_builtin_module_map, mp_builtin_module_table); -static const mp_rom_map_elem_t mp_builtin_extensible_module_table[] = { +MP_ROM_TABLE(static, mp_builtin_extensible_module_table, // built-in modules declared with MP_REGISTER_EXTENSIBLE_MODULE() MICROPY_REGISTERED_EXTENSIBLE_MODULES -}; + ); MP_DEFINE_CONST_MAP(mp_builtin_extensible_module_map, mp_builtin_extensible_module_table); #if MICROPY_MODULE_ATTR_DELEGATION && defined(MICROPY_MODULE_DELEGATIONS) diff --git a/py/objtuple.c b/py/objtuple.c index a8bd11c7e2..fcce22510a 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -214,10 +214,10 @@ static mp_obj_t tuple_index(size_t n_args, const mp_obj_t *args) { } static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tuple_index_obj, 2, 4, tuple_index); -static const mp_rom_map_elem_t tuple_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&tuple_count_obj) }, - { MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&tuple_index_obj) }, -}; +MP_ROM_TABLE(static, tuple_locals_dict_table, + ((MP_QSTR_count, MP_ROM_PTR, &tuple_count_obj)) + ((MP_QSTR_index, MP_ROM_PTR, &tuple_index_obj)) + ); static MP_DEFINE_CONST_DICT(tuple_locals_dict, tuple_locals_dict_table); diff --git a/py/romtable.h b/py/romtable.h index 3ff9c5eb3a..1a6ef8051f 100644 --- a/py/romtable.h +++ b/py/romtable.h @@ -44,10 +44,10 @@ #define MP_TABLE_KEY(r, x, element) BOOST_PP_TUPLE_ELEM(0, element), #define MP_TABLE_VALUE(r, x, element) \ BOOST_PP_EXPAND(BOOST_PP_TUPLE_ELEM(1, element) (BOOST_PP_TUPLE_ELEM(2, element))), -#define ROM_TABLE_SEPARATE(storage, name, contents) \ +#define MP_ROM_TABLE_SEPARATE(storage, name, contents) \ const storage struct { \ - qstr_short_t keys[BOOST_PP_SEQ_SIZE(contents)], \ - mp_rom_obj_t values[BOOST_PP_SEQ_SIZE(contents)], \ + qstr_short_t keys[BOOST_PP_SEQ_SIZE(contents)]; \ + mp_rom_obj_t values[BOOST_PP_SEQ_SIZE(contents)]; \ } name = { \ { BOOST_PP_SEQ_FOR_EACH(MP_TABLE_KEY, _, contents) }, \ { BOOST_PP_SEQ_FOR_EACH(MP_TABLE_VALUE, _, contents) }, \