Skip to content

Commit caac542

Browse files
committed
Proper support for registering builtin modules in ROM.
Comes with some refactoring of code and renaming of files. All modules are now named mod*.[ch].
1 parent 1dfde89 commit caac542

26 files changed

+297
-196
lines changed

py/builtin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);
3232

3333
MP_DECLARE_CONST_FUN_OBJ(mp_namedtuple_obj);
3434

35+
extern const mp_obj_module_t mp_module_array;
36+
extern const mp_obj_module_t mp_module_collections;
3537
extern const mp_obj_module_t mp_module_math;
3638
extern const mp_obj_module_t mp_module_micropython;

py/builtinimport.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "lexerunix.h"
1313
#include "parse.h"
1414
#include "obj.h"
15+
#include "objmodule.h"
1516
#include "parsehelper.h"
1617
#include "compile.h"
1718
#include "runtime0.h"
@@ -156,7 +157,7 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
156157
const char *mod_str = (const char*)mp_obj_str_get_data(args[0], &mod_len);
157158

158159
// check if module already exists
159-
mp_obj_t module_obj = mp_obj_module_get(mp_obj_str_get_qstr(args[0]));
160+
mp_obj_t module_obj = mp_module_get(mp_obj_str_get_qstr(args[0]));
160161
if (module_obj != MP_OBJ_NULL) {
161162
// If it's not a package, return module right away
162163
char *p = strchr(mod_str, '.');
@@ -169,7 +170,7 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
169170
}
170171
// Otherwise, we need to return top-level package
171172
qstr pkg_name = qstr_from_strn(mod_str, p - mod_str);
172-
return mp_obj_module_get(pkg_name);
173+
return mp_module_get(pkg_name);
173174
}
174175

175176
uint last = 0;
@@ -200,7 +201,7 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
200201
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "ImportError: No module named '%s'", qstr_str(mod_name)));
201202
}
202203

203-
module_obj = mp_obj_module_get(mod_name);
204+
module_obj = mp_module_get(mod_name);
204205
if (module_obj == MP_OBJ_NULL) {
205206
// module not already loaded, so load it!
206207

py/builtintables.c

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#include <stdlib.h>
2+
3+
#include "misc.h"
4+
#include "mpconfig.h"
5+
#include "qstr.h"
6+
#include "obj.h"
7+
#include "builtin.h"
8+
#include "builtintables.h"
9+
#include "objarray.h"
10+
11+
// builtins
12+
// we put these tables in ROM because they're always needed and take up quite a bit of room in RAM
13+
// in fact, it uses less ROM here in table form than the equivalent in code form initialising a dynamic mp_map_t object in RAM
14+
// at the moment it's a linear table, but we could convert it to a const mp_map_t table with a simple preprocessing script
15+
16+
typedef struct _mp_builtin_elem_t {
17+
qstr qstr;
18+
mp_obj_t elem;
19+
} mp_builtin_elem_t;
20+
21+
STATIC const mp_builtin_elem_t builtin_object_table[] = {
22+
// built-in core functions
23+
{ MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj },
24+
{ MP_QSTR___import__, (mp_obj_t)&mp_builtin___import___obj },
25+
{ MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj },
26+
27+
// built-in types
28+
{ MP_QSTR_bool, (mp_obj_t)&bool_type },
29+
{ MP_QSTR_bytes, (mp_obj_t)&bytes_type },
30+
#if MICROPY_ENABLE_FLOAT
31+
{ MP_QSTR_complex, (mp_obj_t)&mp_type_complex },
32+
#endif
33+
{ MP_QSTR_dict, (mp_obj_t)&dict_type },
34+
{ MP_QSTR_enumerate, (mp_obj_t)&enumerate_type },
35+
{ MP_QSTR_filter, (mp_obj_t)&filter_type },
36+
#if MICROPY_ENABLE_FLOAT
37+
{ MP_QSTR_float, (mp_obj_t)&mp_type_float },
38+
#endif
39+
{ MP_QSTR_int, (mp_obj_t)&int_type },
40+
{ MP_QSTR_list, (mp_obj_t)&list_type },
41+
{ MP_QSTR_map, (mp_obj_t)&map_type },
42+
{ MP_QSTR_object, (mp_obj_t)&mp_type_object },
43+
{ MP_QSTR_set, (mp_obj_t)&set_type },
44+
{ MP_QSTR_str, (mp_obj_t)&str_type },
45+
{ MP_QSTR_super, (mp_obj_t)&super_type },
46+
{ MP_QSTR_tuple, (mp_obj_t)&tuple_type },
47+
{ MP_QSTR_type, (mp_obj_t)&mp_type_type },
48+
{ MP_QSTR_zip, (mp_obj_t)&zip_type },
49+
50+
{ MP_QSTR_classmethod, (mp_obj_t)&mp_type_classmethod },
51+
{ MP_QSTR_staticmethod, (mp_obj_t)&mp_type_staticmethod },
52+
53+
// built-in user functions
54+
{ MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj },
55+
{ MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj },
56+
{ MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj },
57+
{ MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj },
58+
{ MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj },
59+
{ MP_QSTR_dir, (mp_obj_t)&mp_builtin_dir_obj },
60+
{ MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj },
61+
{ MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj },
62+
{ MP_QSTR_exec, (mp_obj_t)&mp_builtin_exec_obj },
63+
{ MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj },
64+
{ MP_QSTR_id, (mp_obj_t)&mp_builtin_id_obj },
65+
{ MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj },
66+
{ MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj },
67+
{ MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj },
68+
{ MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj },
69+
{ MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj },
70+
{ MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj },
71+
{ MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj },
72+
{ MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj },
73+
{ MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj },
74+
{ MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj },
75+
{ MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj },
76+
{ MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj },
77+
{ MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj },
78+
{ MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj },
79+
{ MP_QSTR_bytearray, (mp_obj_t)&mp_builtin_bytearray_obj },
80+
81+
// built-in exceptions
82+
{ MP_QSTR_BaseException, (mp_obj_t)&mp_type_BaseException },
83+
{ MP_QSTR_ArithmeticError, (mp_obj_t)&mp_type_ArithmeticError },
84+
{ MP_QSTR_AssertionError, (mp_obj_t)&mp_type_AssertionError },
85+
{ MP_QSTR_AttributeError, (mp_obj_t)&mp_type_AttributeError },
86+
{ MP_QSTR_BufferError, (mp_obj_t)&mp_type_BufferError },
87+
{ MP_QSTR_EOFError, (mp_obj_t)&mp_type_EOFError },
88+
{ MP_QSTR_EnvironmentError, (mp_obj_t)&mp_type_EnvironmentError },
89+
{ MP_QSTR_Exception, (mp_obj_t)&mp_type_Exception },
90+
{ MP_QSTR_FloatingPointError, (mp_obj_t)&mp_type_FloatingPointError },
91+
{ MP_QSTR_GeneratorExit, (mp_obj_t)&mp_type_GeneratorExit },
92+
{ MP_QSTR_IOError, (mp_obj_t)&mp_type_IOError },
93+
{ MP_QSTR_ImportError, (mp_obj_t)&mp_type_ImportError },
94+
{ MP_QSTR_IndentationError, (mp_obj_t)&mp_type_IndentationError },
95+
{ MP_QSTR_IndexError, (mp_obj_t)&mp_type_IndexError },
96+
{ MP_QSTR_KeyError, (mp_obj_t)&mp_type_KeyError },
97+
{ MP_QSTR_LookupError, (mp_obj_t)&mp_type_LookupError },
98+
{ MP_QSTR_MemoryError, (mp_obj_t)&mp_type_MemoryError },
99+
{ MP_QSTR_NameError, (mp_obj_t)&mp_type_NameError },
100+
{ MP_QSTR_NotImplementedError, (mp_obj_t)&mp_type_NotImplementedError },
101+
{ MP_QSTR_OSError, (mp_obj_t)&mp_type_OSError },
102+
{ MP_QSTR_OverflowError, (mp_obj_t)&mp_type_OverflowError },
103+
{ MP_QSTR_ReferenceError, (mp_obj_t)&mp_type_ReferenceError },
104+
{ MP_QSTR_RuntimeError, (mp_obj_t)&mp_type_RuntimeError },
105+
{ MP_QSTR_SyntaxError, (mp_obj_t)&mp_type_SyntaxError },
106+
{ MP_QSTR_SystemError, (mp_obj_t)&mp_type_SystemError },
107+
{ MP_QSTR_SystemExit, (mp_obj_t)&mp_type_SystemExit },
108+
{ MP_QSTR_TabError, (mp_obj_t)&mp_type_TabError },
109+
{ MP_QSTR_TypeError, (mp_obj_t)&mp_type_TypeError },
110+
{ MP_QSTR_UnboundLocalError, (mp_obj_t)&mp_type_UnboundLocalError },
111+
{ MP_QSTR_ValueError, (mp_obj_t)&mp_type_ValueError },
112+
{ MP_QSTR_ZeroDivisionError, (mp_obj_t)&mp_type_ZeroDivisionError },
113+
{ MP_QSTR_StopIteration, (mp_obj_t)&mp_type_StopIteration },
114+
// Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
115+
// TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
116+
117+
// Extra builtins as defined by a port
118+
MICROPY_EXTRA_BUILTINS
119+
120+
{ MP_QSTR_, MP_OBJ_NULL }, // end of list sentinel
121+
};
122+
123+
STATIC const mp_builtin_elem_t builtin_module_table[] = {
124+
{ MP_QSTR_micropython, (mp_obj_t)&mp_module_micropython },
125+
126+
{ MP_QSTR_array, (mp_obj_t)&mp_module_array },
127+
{ MP_QSTR_collections, (mp_obj_t)&mp_module_collections },
128+
129+
#if MICROPY_ENABLE_FLOAT
130+
{ MP_QSTR_math, (mp_obj_t)&mp_module_math },
131+
#endif
132+
133+
// extra builtin modules as defined by a port
134+
MICROPY_EXTRA_BUILTIN_MODULES
135+
136+
{ MP_QSTR_, MP_OBJ_NULL }, // end of list sentinel
137+
};
138+
139+
STATIC mp_obj_t mp_builtin_tables_lookup(const mp_builtin_elem_t *table, qstr q) {
140+
for (; table->qstr != MP_QSTR_; table++) {
141+
if (table->qstr == q) {
142+
return table->elem;
143+
}
144+
}
145+
return MP_OBJ_NULL;
146+
}
147+
148+
mp_obj_t mp_builtin_tables_lookup_object(qstr q) {
149+
return mp_builtin_tables_lookup(&builtin_object_table[0], q);
150+
}
151+
152+
mp_obj_t mp_builtin_tables_lookup_module(qstr q) {
153+
return mp_builtin_tables_lookup(&builtin_module_table[0], q);
154+
}

py/builtintables.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mp_obj_t mp_builtin_tables_lookup_object(qstr q);
2+
mp_obj_t mp_builtin_tables_lookup_module(qstr q);

py/modarray.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "misc.h"
2+
#include "mpconfig.h"
3+
#include "qstr.h"
4+
#include "obj.h"
5+
#include "map.h"
6+
#include "builtin.h"
7+
8+
STATIC const mp_map_elem_t mp_module_array_globals_table[] = {
9+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_array) },
10+
{ MP_OBJ_NEW_QSTR(MP_QSTR_array), (mp_obj_t)&mp_type_array },
11+
};
12+
13+
STATIC const mp_map_t mp_module_array_globals = {
14+
.all_keys_are_qstrs = 1,
15+
.table_is_fixed_array = 1,
16+
.used = sizeof(mp_module_array_globals_table) / sizeof(mp_map_elem_t),
17+
.alloc = sizeof(mp_module_array_globals_table) / sizeof(mp_map_elem_t),
18+
.table = (mp_map_elem_t*)mp_module_array_globals_table,
19+
};
20+
21+
const mp_obj_module_t mp_module_array = {
22+
.base = { &mp_type_module },
23+
.name = MP_QSTR_array,
24+
.globals = (mp_map_t*)&mp_module_array_globals,
25+
};

py/modcollections.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "misc.h"
2+
#include "mpconfig.h"
3+
#include "qstr.h"
4+
#include "obj.h"
5+
#include "map.h"
6+
#include "builtin.h"
7+
8+
STATIC const mp_map_elem_t mp_module_collections_globals_table[] = {
9+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_collections) },
10+
{ MP_OBJ_NEW_QSTR(MP_QSTR_namedtuple), (mp_obj_t)&mp_namedtuple_obj },
11+
};
12+
13+
STATIC const mp_map_t mp_module_collections_globals = {
14+
.all_keys_are_qstrs = 1,
15+
.table_is_fixed_array = 1,
16+
.used = sizeof(mp_module_collections_globals_table) / sizeof(mp_map_elem_t),
17+
.alloc = sizeof(mp_module_collections_globals_table) / sizeof(mp_map_elem_t),
18+
.table = (mp_map_elem_t*)mp_module_collections_globals_table,
19+
};
20+
21+
const mp_obj_module_t mp_module_collections = {
22+
.base = { &mp_type_module },
23+
.name = MP_QSTR_collections,
24+
.globals = (mp_map_t*)&mp_module_collections_globals,
25+
};
File renamed without changes.

py/builtinmp.c renamed to py/modmicropython.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#include "misc.h"
32
#include "mpconfig.h"
43
#include "qstr.h"

py/mpconfig.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,16 @@ typedef double mp_float_t;
125125
#define MICROPY_PATH_MAX (512)
126126
#endif
127127

128-
// Additional builtin function definitions - see runtime.c:builtin_table for format.
128+
// Additional builtin function definitions - see builtintables.c:builtin_object_table for format.
129129
#ifndef MICROPY_EXTRA_BUILTINS
130130
#define MICROPY_EXTRA_BUILTINS
131131
#endif
132+
133+
// Additional builtin module definitions - see builtintables.c:builtin_module_table for format.
134+
#ifndef MICROPY_EXTRA_BUILTIN_MODULES
135+
#define MICROPY_EXTRA_BUILTIN_MODULES
136+
#endif
137+
132138
/*****************************************************************************/
133139
/* Miscellaneous settings */
134140

py/obj.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *sto
417417
extern const mp_obj_type_t zip_type;
418418

419419
// array
420-
extern const mp_obj_type_t array_type;
420+
extern const mp_obj_type_t mp_type_array;
421421
uint mp_obj_array_len(mp_obj_t self_in);
422422
mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items);
423423

@@ -454,9 +454,6 @@ typedef struct _mp_obj_module_t {
454454
struct _mp_map_t *globals;
455455
} mp_obj_module_t;
456456
extern const mp_obj_type_t mp_type_module;
457-
mp_obj_t mp_obj_new_module(qstr module_name);
458-
mp_obj_t mp_obj_module_get(qstr module_name);
459-
void mp_obj_module_register(qstr qstr, mp_obj_t module); //use for loading statically allocated modules
460457
struct _mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in);
461458

462459
// staticmethod and classmethod types; defined here so we can make const versions

0 commit comments

Comments
 (0)