Skip to content

Commit b37b578

Browse files
committed
py/persistentcode: Remove remaining native qstr linking support.
Support for architecture-specific qstr linking was removed in d4d53e9, where native code was changed to access qstr values via qstr_table. The only remaining use for the special qstr link table in persistentcode.c is to support native module written in C, linked via mpy_ld.py. But native modules can also use the standard module-level qstr_table (and obj_table) which was introduced in the .mpy file reworking in f2040bf. This commit removes the remaining native qstr liking support in persistentcode.c's load_raw_code function, and adds two new relocation options for constants.qstr_table and constants.obj_table. mpy_ld.py is updated to use these relocations options instead of the native qstr link table. Signed-off-by: Damien George <damien@micropython.org>
1 parent 2111ca0 commit b37b578

File tree

5 files changed

+56
-96
lines changed

5 files changed

+56
-96
lines changed

py/persistentcode.c

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ STATIC size_t read_uint(mp_reader_t *reader);
7171

7272
typedef struct _reloc_info_t {
7373
mp_reader_t *reader;
74+
mp_module_context_t *context;
7475
uint8_t *rodata;
7576
uint8_t *bss;
7677
} reloc_info_t;
@@ -112,11 +113,17 @@ void mp_native_relocate(void *ri_in, uint8_t *text, uintptr_t reloc_text) {
112113
dest = (uintptr_t)ri->bss;
113114
}
114115
} else if (op == 6) {
116+
// Destination is qstr_table
117+
dest = (uintptr_t)ri->context->constants.qstr_table;
118+
} else if (op == 7) {
119+
// Destination is obj_table
120+
dest = (uintptr_t)ri->context->constants.obj_table;
121+
} else if (op == 8) {
115122
// Destination is mp_fun_table itself
116123
dest = (uintptr_t)&mp_fun_table;
117124
} else {
118125
// Destination is an entry in mp_fun_table
119-
dest = ((uintptr_t *)&mp_fun_table)[op - 7];
126+
dest = ((uintptr_t *)&mp_fun_table)[op - 9];
120127
}
121128
while (n--) {
122129
*addr_to_adjust++ += dest;
@@ -205,7 +212,7 @@ STATIC mp_obj_t load_obj(mp_reader_t *reader) {
205212
}
206213
}
207214

208-
STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) {
215+
STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, mp_module_context_t *context) {
209216
// Load function kind and data length
210217
size_t kind_len = read_uint(reader);
211218
int kind = (kind_len & 3) + MP_CODE_BYTECODE;
@@ -239,24 +246,6 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) {
239246
MP_PLAT_ALLOC_EXEC(fun_data_len, (void **)&fun_data, &fun_alloc);
240247
read_bytes(reader, fun_data, fun_data_len);
241248

242-
if (kind == MP_CODE_NATIVE_PY || kind == MP_CODE_NATIVE_VIPER) {
243-
// Parse qstr link table and link native code
244-
size_t n_qstr_link = read_uint(reader);
245-
for (size_t i = 0; i < n_qstr_link; ++i) {
246-
size_t off = read_uint(reader);
247-
qstr qst = load_qstr(reader);
248-
uint8_t *dest = fun_data + (off >> 2);
249-
if ((off & 3) == 0) {
250-
// Generic 16-bit link
251-
dest[0] = qst & 0xff;
252-
dest[1] = (qst >> 8) & 0xff;
253-
} else if ((off & 3) == 3) {
254-
// Generic, aligned qstr-object link
255-
*(mp_obj_t *)dest = MP_OBJ_NEW_QSTR(qst);
256-
}
257-
}
258-
}
259-
260249
if (kind == MP_CODE_NATIVE_PY) {
261250
// Read prelude offset within fun_data, and extract scope flags.
262251
prelude_offset = read_uint(reader);
@@ -315,7 +304,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) {
315304
n_children = read_uint(reader);
316305
children = m_new(mp_raw_code_t *, n_children + (kind == MP_CODE_NATIVE_PY));
317306
for (size_t i = 0; i < n_children; ++i) {
318-
children[i] = load_raw_code(reader);
307+
children[i] = load_raw_code(reader, context);
319308
}
320309
}
321310

@@ -349,7 +338,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) {
349338
#endif
350339

351340
// Relocate and commit code to executable address space
352-
reloc_info_t ri = {reader, rodata, bss};
341+
reloc_info_t ri = {reader, context, rodata, bss};
353342
#if defined(MP_PLAT_COMMIT_EXEC)
354343
void *opt_ri = (native_scope_flags & MP_SCOPE_FLAG_VIPERRELOC) ? &ri : NULL;
355344
fun_data = MP_PLAT_COMMIT_EXEC(fun_data, fun_data_len, opt_ri);
@@ -429,7 +418,7 @@ mp_compiled_module_t mp_raw_code_load(mp_reader_t *reader, mp_module_context_t *
429418

430419
// Load top-level module.
431420
mp_compiled_module_t cm2;
432-
cm2.rc = load_raw_code(reader);
421+
cm2.rc = load_raw_code(reader, context);
433422
cm2.context = context;
434423

435424
#if MICROPY_PERSISTENT_CODE_SAVE
@@ -567,11 +556,6 @@ STATIC void save_raw_code(mp_print_t *print, const mp_raw_code_t *rc) {
567556
mp_print_bytes(print, rc->fun_data, rc->fun_data_len);
568557

569558
#if MICROPY_EMIT_MACHINE_CODE
570-
if (rc->kind == MP_CODE_NATIVE_PY || rc->kind == MP_CODE_NATIVE_VIPER) {
571-
// Save qstr link table for native code
572-
mp_print_uint(print, 0);
573-
}
574-
575559
if (rc->kind == MP_CODE_NATIVE_PY) {
576560
// Save prelude size
577561
mp_print_uint(print, rc->prelude_offset);

tests/micropython/import_mpy_native.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ def open(self, path, mode):
7474
b'\x02' # 2 children
7575

7676
b'\x42' # 8 bytes, no children, viper code
77-
b'\x00\x00\x00\x00\x00\x00' # dummy machine code
78-
b'\x00\x00' # slot for qstr0
79-
b'\x01\x0c\x0aprint\x00' # n_qstr=1, qstr0
77+
b'\x00\x00\x00\x00\x00\x00\x00\x00' # dummy machine code
8078
b'\x00' # scope_flags
8179

8280
b'\x43' # 8 bytes, no children, asm code
@@ -102,7 +100,6 @@ def open(self, path, mode):
102100

103101
b'\x22' # 4 bytes, no children, viper code
104102
b'\x00\x00\x00\x00' # dummy machine code
105-
b'\x00' # n_qstr=0
106103
b'\x70' # scope_flags: VIPERBSS | VIPERRODATA | VIPERRELOC
107104
b'\x06\x04' # rodata=6 bytes, bss=4 bytes
108105
b'rodata' # rodata content

tests/micropython/import_mpy_native_gc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def open(self, path, mode):
4949
# by the required value of sys.implementation._mpy.
5050
features0_file_contents = {
5151
# -march=x64
52-
0x806: b'M\x06\b\x1f\x01\x004build/features0.native.mpy\x00\x8aB\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\xb78\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\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x85\x00\x12factorial\x00\x10\r$\x01&\x9f \x01"\xff',
52+
0x806: b'M\x06\x08\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&\xa3 \x01"\xff',
5353
# -march=armv6m
54-
0x1006: b'M\x06\x10\x1f\x01\x004build/features0.native.mpy\x00\x88"\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\xf3XyD\x18\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\x80\x00\x00\x00\x00\x00\x00\x00\x01\x84\x00\x12factorial\x00\x10\r<\x01>\x9f8\x01:\xff',
54+
0x1006: b"M\x06\x10\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>\xa38\x01:\xff",
5555
}
5656

5757
# Populate armv7m-derived archs based on armv6m.

tools/mpy-tool.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,6 @@ def __init__(
976976
kind,
977977
fun_data,
978978
prelude_offset,
979-
qstr_links,
980979
scope_flags,
981980
n_pos_args,
982981
type_sig,
@@ -989,7 +988,6 @@ def __init__(
989988
self.scope_flags = scope_flags
990989
self.n_pos_args = n_pos_args
991990

992-
self.qstr_links = qstr_links
993991
self.type_sig = type_sig
994992
if config.native_arch in (
995993
MP_NATIVE_ARCH_X86,
@@ -1196,15 +1194,6 @@ def read_raw_code(reader, cm_escaped_name, qstr_table, obj_table, segments):
11961194
rc = RawCodeBytecode(cm_escaped_name, qstr_table, obj_table, fun_data)
11971195
else:
11981196
# Create native raw code.
1199-
qstr_links = []
1200-
if kind in (MP_CODE_NATIVE_PY, MP_CODE_NATIVE_VIPER):
1201-
# Read qstr link table.
1202-
n_qstr_link = reader.read_uint()
1203-
for _ in range(n_qstr_link):
1204-
off = reader.read_uint()
1205-
qst = read_qstr(reader, segments)
1206-
qstr_links.append((off >> 2, off & 3, qst))
1207-
12081197
native_scope_flags = 0
12091198
native_n_pos_args = 0
12101199
native_type_sig = 0
@@ -1242,7 +1231,6 @@ def read_raw_code(reader, cm_escaped_name, qstr_table, obj_table, segments):
12421231
kind,
12431232
fun_data,
12441233
prelude_offset,
1245-
qstr_links,
12461234
native_scope_flags,
12471235
native_n_pos_args,
12481236
native_type_sig,

0 commit comments

Comments
 (0)