Commit 8b0535e2 authored by Damien George's avatar Damien George
Browse files

py: Change module globals from mp_map_t* to mp_obj_dict_t*.

Towards addressing issue #424.

Had a small increase to ROM usage (order 60 bytes).
parent 60be1cf3
......@@ -157,7 +157,7 @@ STATIC mp_obj_t mp_builtin_dir(uint n_args, const mp_obj_t *args) {
} else { // n_args == 1
// make a list of names in the given object
if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) {
map = mp_obj_module_get_globals(args[0]);
map = mp_obj_dict_get_map(mp_obj_module_get_globals(args[0]));
} else {
mp_obj_type_t *type;
if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) {
......@@ -174,7 +174,7 @@ STATIC mp_obj_t mp_builtin_dir(uint n_args, const mp_obj_t *args) {
mp_obj_t dir = mp_obj_new_list(0, NULL);
if (map != NULL) {
for (uint i = 0; i < map->alloc; i++) {
if (map->table[i].key != MP_OBJ_NULL) {
if (MP_MAP_SLOT_IS_FILLED(map, i)) {
mp_obj_list_append(dir, map->table[i].key);
}
}
......
......@@ -87,8 +87,8 @@ void do_load(mp_obj_t module_obj, vstr_t *file) {
mp_map_t *old_globals = mp_globals_get();
// set the new context
mp_locals_set(mp_obj_module_get_globals(module_obj));
mp_globals_set(mp_obj_module_get_globals(module_obj));
mp_locals_set(mp_obj_dict_get_map(mp_obj_module_get_globals(module_obj)));
mp_globals_set(mp_obj_dict_get_map(mp_obj_module_get_globals(module_obj)));
// parse the imported script
mp_parse_error_kind_t parse_error_kind;
......
#include <stdlib.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
......@@ -285,7 +286,7 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t looku
mp_obj_t mp_set_remove_first(mp_set_t *set) {
for (uint pos = 0; pos < set->alloc; pos++) {
if (set->table[pos] != MP_OBJ_NULL && set->table[pos] != MP_OBJ_SENTINEL) {
if (MP_SET_SLOT_IS_FILLED(set, pos)) {
mp_obj_t elem = set->table[pos];
// delete element
set->used--;
......
......@@ -9,16 +9,19 @@ STATIC const mp_map_elem_t mp_module_array_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_array), (mp_obj_t)&mp_type_array },
};
STATIC const mp_map_t mp_module_array_globals = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(mp_module_array_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(mp_module_array_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)mp_module_array_globals_table,
STATIC const mp_obj_dict_t mp_module_array_globals = {
.base = {&mp_type_dict},
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(mp_module_array_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(mp_module_array_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)mp_module_array_globals_table,
},
};
const mp_obj_module_t mp_module_array = {
.base = { &mp_type_module },
.name = MP_QSTR_array,
.globals = (mp_map_t*)&mp_module_array_globals,
.globals = (mp_obj_dict_t*)&mp_module_array_globals,
};
......@@ -9,16 +9,19 @@ STATIC const mp_map_elem_t mp_module_collections_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_namedtuple), (mp_obj_t)&mp_namedtuple_obj },
};
STATIC const mp_map_t mp_module_collections_globals = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(mp_module_collections_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(mp_module_collections_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)mp_module_collections_globals_table,
STATIC const mp_obj_dict_t mp_module_collections_globals = {
.base = {&mp_type_dict},
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(mp_module_collections_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(mp_module_collections_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)mp_module_collections_globals_table,
},
};
const mp_obj_module_t mp_module_collections = {
.base = { &mp_type_module },
.name = MP_QSTR_collections,
.globals = (mp_map_t*)&mp_module_collections_globals,
.globals = (mp_obj_dict_t*)&mp_module_collections_globals,
};
......@@ -13,18 +13,21 @@ STATIC const mp_map_elem_t mp_module_io_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },
};
STATIC const mp_map_t mp_module_io_globals = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(mp_module_io_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(mp_module_io_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)mp_module_io_globals_table,
STATIC const mp_obj_dict_t mp_module_io_globals = {
.base = {&mp_type_dict},
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(mp_module_io_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(mp_module_io_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)mp_module_io_globals_table,
},
};
const mp_obj_module_t mp_module_io = {
.base = { &mp_type_module },
.name = MP_QSTR_io,
.globals = (mp_map_t*)&mp_module_io_globals,
.globals = (mp_obj_dict_t*)&mp_module_io_globals,
};
#endif
......@@ -139,18 +139,21 @@ STATIC const mp_map_elem_t mp_module_math_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_lgamma), (mp_obj_t)&mp_math_lgamma_obj },
};
STATIC const mp_map_t mp_module_math_globals = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(mp_module_math_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(mp_module_math_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)mp_module_math_globals_table,
STATIC const mp_obj_dict_t mp_module_math_globals = {
.base = {&mp_type_dict},
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(mp_module_math_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(mp_module_math_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)mp_module_math_globals_table,
},
};
const mp_obj_module_t mp_module_math = {
.base = { &mp_type_module },
.name = MP_QSTR_math,
.globals = (mp_map_t*)&mp_module_math_globals,
.globals = (mp_obj_dict_t*)&mp_module_math_globals,
};
#endif // MICROPY_ENABLE_FLOAT
......@@ -34,16 +34,19 @@ STATIC const mp_map_elem_t mp_module_micropython_globals_table[] = {
#endif
};
STATIC const mp_map_t mp_module_micropython_globals = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(mp_module_micropython_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(mp_module_micropython_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)mp_module_micropython_globals_table,
STATIC const mp_obj_dict_t mp_module_micropython_globals = {
.base = {&mp_type_dict},
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(mp_module_micropython_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(mp_module_micropython_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)mp_module_micropython_globals_table,
},
};
const mp_obj_module_t mp_module_micropython = {
.base = { &mp_type_module },
.name = MP_QSTR_micropython,
.globals = (mp_map_t*)&mp_module_micropython_globals,
.globals = (mp_obj_dict_t*)&mp_module_micropython_globals,
};
......@@ -115,6 +115,8 @@ typedef enum _mp_map_lookup_kind_t {
MP_MAP_LOOKUP_REMOVE_IF_FOUND, // 2
} mp_map_lookup_kind_t;
#define MP_MAP_SLOT_IS_FILLED(map, pos) ((map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL)
void mp_map_init(mp_map_t *map, int n);
void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table);
mp_map_t *mp_map_new(int n);
......@@ -132,6 +134,8 @@ typedef struct _mp_set_t {
mp_obj_t *table;
} mp_set_t;
#define MP_SET_SLOT_IS_FILLED(set, pos) ((set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL)
void mp_set_init(mp_set_t *set, int n);
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
mp_obj_t mp_set_remove_first(mp_set_t *set);
......@@ -444,6 +448,7 @@ typedef struct _mp_obj_dict_t {
mp_obj_base_t base;
mp_map_t map;
} mp_obj_dict_t;
void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args);
uint mp_obj_dict_len(mp_obj_t self_in);
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
......@@ -483,9 +488,9 @@ MP_DECLARE_CONST_FUN_OBJ(mp_identity_obj);
typedef struct _mp_obj_module_t {
mp_obj_base_t base;
qstr name;
mp_map_t *globals;
mp_obj_dict_t *globals;
} mp_obj_module_t;
mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in);
mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t self_in);
// staticmethod and classmethod types; defined here so we can make const versions
// this structure is used for instances of both staticmethod and classmethod
......
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
......
......@@ -100,12 +100,12 @@ typedef struct _mp_obj_dict_it_t {
STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in) {
mp_obj_dict_it_t *self = self_in;
machine_uint_t max = self->dict->map.alloc;
mp_map_elem_t *table = self->dict->map.table;
mp_map_t *map = &self->dict->map;
for (int i = self->cur; i < max; i++) {
if (table[i].key != MP_OBJ_NULL && table[i].key != MP_OBJ_SENTINEL) {
if (MP_MAP_SLOT_IS_FILLED(map, i)) {
self->cur = i + 1;
return &(table[i]);
return &(map->table[i]);
}
}
......@@ -463,10 +463,14 @@ const mp_obj_type_t mp_type_dict = {
.locals_dict = (mp_obj_t)&dict_locals_dict,
};
void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args) {
dict->base.type = &mp_type_dict;
mp_map_init(&dict->map, n_args);
}
mp_obj_t mp_obj_new_dict(int n_args) {
mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
o->base.type = &mp_type_dict;
mp_map_init(&o->map, n_args);
mp_obj_dict_init(o, n_args);
return o;
}
......
......@@ -19,7 +19,7 @@ STATIC void module_print(void (*print)(void *env, const char *fmt, ...), void *e
STATIC void module_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
mp_obj_module_t *self = self_in;
mp_map_elem_t *elem = mp_map_lookup(self->globals, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
if (elem != NULL) {
dest[0] = elem->value;
}
......@@ -28,7 +28,7 @@ STATIC void module_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
STATIC bool module_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
mp_obj_module_t *self = self_in;
// TODO CPython allows STORE_ATTR to a module, but is this the correct implementation?
mp_map_lookup(self->globals, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
mp_obj_dict_store(self->globals, MP_OBJ_NEW_QSTR(attr), value);
return true;
}
......@@ -52,10 +52,10 @@ mp_obj_t mp_obj_new_module(qstr module_name) {
mp_obj_module_t *o = m_new_obj(mp_obj_module_t);
o->base.type = &mp_type_module;
o->name = module_name;
o->globals = mp_map_new(1);
o->globals = mp_obj_new_dict(1);
// store __name__ entry in the module
mp_map_lookup(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = MP_OBJ_NEW_QSTR(module_name);
mp_obj_dict_store(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(module_name));
// store the new module into the slot in the global dict holding all modules
el->value = o;
......@@ -64,7 +64,7 @@ mp_obj_t mp_obj_new_module(qstr module_name) {
return o;
}
mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in) {
mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t self_in) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_module));
mp_obj_module_t *self = self_in;
return self->globals;
......
......@@ -32,7 +32,7 @@ STATIC void set_print(void (*print)(void *env, const char *fmt, ...), void *env,
bool first = true;
print(env, "{");
for (int i = 0; i < self->set.alloc; i++) {
if (self->set.table[i] != MP_OBJ_NULL && self->set.table[i] != MP_OBJ_SENTINEL) {
if (MP_SET_SLOT_IS_FILLED(&self->set, i)) {
if (!first) {
print(env, ", ");
}
......@@ -80,12 +80,12 @@ STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set_it));
mp_obj_set_it_t *self = self_in;
machine_uint_t max = self->set->set.alloc;
mp_obj_t *table = self->set->set.table;
mp_set_t *set = &self->set->set;
for (machine_uint_t i = self->cur; i < max; i++) {
if (table[i] != MP_OBJ_NULL && table[i] != MP_OBJ_SENTINEL) {
if (MP_SET_SLOT_IS_FILLED(set, i)) {
self->cur = i + 1;
return table[i];
return set->table[i];
}
}
......
......@@ -33,31 +33,26 @@ STATIC mp_map_t *map_locals;
STATIC mp_map_t *map_globals;
STATIC mp_map_t map_builtins;
STATIC mp_map_t map_main;
STATIC mp_obj_dict_t dict_main;
const mp_obj_module_t mp_module___main__ = {
.base = { &mp_type_module },
.name = MP_QSTR___main__,
.globals = (mp_map_t*)&map_main,
.globals = (mp_obj_dict_t*)&dict_main,
};
// a good optimising compiler will inline this if necessary
STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
}
void mp_init(void) {
mp_emit_glue_init();
// init global module stuff
mp_module_init();
mp_map_init(&map_main, 1);
mp_obj_dict_init(&dict_main, 1);
// add some builtins that can't be done in ROM
mp_map_add_qstr(&map_main, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
mp_obj_dict_store(&dict_main, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
// locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
map_locals = map_globals = &map_main;
map_locals = map_globals = &dict_main.map;
// init built-in hash table
mp_map_init(&map_builtins, 3);
......@@ -1017,9 +1012,9 @@ mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
void mp_import_all(mp_obj_t module) {
DEBUG_printf("import all %p\n", module);
mp_map_t *map = mp_obj_module_get_globals(module);
mp_map_t *map = mp_obj_dict_get_map(mp_obj_module_get_globals(module));
for (uint i = 0; i < map->alloc; i++) {
if (map->table[i].key != MP_OBJ_NULL) {
if (MP_MAP_SLOT_IS_FILLED(map, i)) {
mp_store_name(MP_OBJ_QSTR_VALUE(map->table[i].key), map->table[i].value);
}
}
......
......@@ -273,16 +273,19 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_Exti), (mp_obj_t)&exti_obj_type },
};
STATIC const mp_map_t pyb_module_globals = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(pyb_module_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(pyb_module_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)pyb_module_globals_table,
STATIC const mp_obj_dict_t pyb_module_globals = {
.base = {&mp_type_dict},
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(pyb_module_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(pyb_module_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)pyb_module_globals_table,
},
};
const mp_obj_module_t pyb_module = {
.base = { &mp_type_module },
.name = MP_QSTR_pyb,
.globals = (mp_map_t*)&pyb_module_globals,
.globals = (mp_obj_dict_t*)&pyb_module_globals,
};
......@@ -59,7 +59,7 @@ STATIC mp_obj_t pyb_help(uint n_args, const mp_obj_t *args) {
mp_map_t *map = NULL;
if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) {
map = mp_obj_module_get_globals(args[0]);
map = mp_obj_dict_get_map(mp_obj_module_get_globals(args[0]));
} else {
mp_obj_type_t *type;
if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) {
......
......@@ -162,16 +162,19 @@ STATIC const mp_map_elem_t os_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_urandom), (mp_obj_t)&os_urandom_obj },
};
STATIC const mp_map_t os_module_globals = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(os_module_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(os_module_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)os_module_globals_table,
STATIC const mp_obj_dict_t os_module_globals = {
.base = {&mp_type_dict},
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(os_module_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(os_module_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)os_module_globals_table,
},
};
const mp_obj_module_t os_module = {
.base = { &mp_type_module },
.name = MP_QSTR_os,
.globals = (mp_map_t*)&os_module_globals,
.globals = (mp_obj_dict_t*)&os_module_globals,
};
......@@ -287,16 +287,19 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_Exti), (mp_obj_t)&exti_obj_type },
};
STATIC const mp_map_t pyb_module_globals = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(pyb_module_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(pyb_module_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)pyb_module_globals_table,
STATIC const mp_obj_dict_t pyb_module_globals = {
.base = {&mp_type_dict},
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(pyb_module_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(pyb_module_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)pyb_module_globals_table,
},
};
const mp_obj_module_t pyb_module = {
.base = { &mp_type_module },
.name = MP_QSTR_pyb,
.globals = (mp_map_t*)&pyb_module_globals,
.globals = (mp_obj_dict_t*)&pyb_module_globals,
};
......@@ -28,16 +28,19 @@ STATIC const mp_map_elem_t time_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&time_sleep_obj },
};
STATIC const mp_map_t time_module_globals = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(time_module_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(time_module_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)time_module_globals_table,
STATIC const mp_obj_dict_t time_module_globals = {
.base = {&mp_type_dict},
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(time_module_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(time_module_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)time_module_globals_table,
},
};
const mp_obj_module_t time_module = {
.base = { &mp_type_module },
.name = MP_QSTR_time,
.globals = (mp_map_t*)&time_module_globals,
.globals = (mp_obj_dict_t*)&time_module_globals,
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment