8000 Load qstrs separately in load_raw_code_native by aykevl · Pull Request #3 · dpgeorge/micropython · GitHub
[go: up one dir, main page]

Skip to content

Load qstrs separately in load_raw_code_native #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions examples/modx/elftompy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,42 @@
"""

import sys
import struct

def to_uint(n):
if n < 0:
raise ValueError('unsigned integer cannot be < 0')
b = bytearray()
while n:
b.insert(0, (n & 0x7f) | 0x80)
n >>= 7
if len(b) == 0:
return b'\x00'
b[-1] &= 0x7f # clear high bit: it's the last byte
return bytes(b)

def align(n, a):
return (n + a - 1) // a * a

def process_file(filename):
with open(filename, 'rb') as f:
data = bytearray(f.read())
size = len(data) - 16
data[8] = size & 0xff
data[9] = size >> 8
header = f.read(6)
num_qstrs = struct.unpack('H', f.read(2))[0]
qstr_strings_len = struct.unpack('Q', f.read(8))[0]
qstr_buffer_len = align(qstr_strings_len, 8)
qstrbuf = f.read(qstr_buffer_len)[:qstr_strings_len]
text = f.read()
qstrs = qstrbuf.rstrip('\0').split(b'\0')
if len(qstrs) != num_qstrs:
raise ValueError('expected len(qstrs) == num_qstrs')
with open(filename, 'wb') as f:
f.write(data)
f.write(header)
f.write(to_uint(num_qstrs))
for qstr in qstrs:
f.write(to_uint(len(qstr)))
f.write(qstr)
f.write(to_uint(len(text)))
f.write(text)

if __name__ == '__main__':
process_file(sys.argv[1])
19 changes: 11 additions & 8 deletions examples/modx/modx.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ enum {
MP_LOCAL_QSTR_number_of,
};

__attribute__((section(".qstr")))
struct {
#define QDEF(id, str) const char id[sizeof(str)];
QSTR_DEFINES
#undef QDEF
} QSTR_VALUE = {
#define QDEF(id, str) str,
QSTR_DEFINES
#undef QDEF
};

STATIC mp_obj_t modx_add1(CONTEXT mp_obj_t x) {
return RT(mp_binary_op)(MP_BINARY_OP_ADD, x, MP_OBJ_NEW_SMALL_INT(1));
}
Expand All @@ -34,14 +45,6 @@ MP_PERSISTENT_NATIVE_HEADER

MP_PERSISTENT_NATIVE_INIT
void init(CONTEXT_ALONE) {
// create qstrs
{
qstr *q = pnd->qstr_table;
#define QDEF(id, str) *q++ = RT(qstr_from_str)(str);
QSTR_DEFINES
#undef QDEF
}

// constants
RT(mp_store_global)(QSTR(VAL1), CONST(_true));
RT(mp_store_global)(QSTR(VAL2), MP_OBJ_NEW_SMALL_INT(123));
Expand Down
5 changes: 5 additions & 0 deletions examples/modx/persistnative.ld
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ SECTIONS
{
. = ALIGN(4);
KEEP(*(.mpyheader))
QUAD(_qstr_end - _qstr_start)
_qstr_start = .;
KEEP(*(.qstr))
_qstr_end = .;
. = ALIGN(8);
KEEP(*(.mpytext))
*(.text*)
*(.rodata*)
Expand Down
2 changes: 1 addition & 1 deletion py/emitglue.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ typedef struct _mp_persistent_native_data_t {
void *data;
} mp_persistent_native_data_t;

mp_persistent_native_data_t *mp_new_persistent_native_data(size_t num_qstrs);
mp_persistent_native_data_t *mp_new_persistent_native_data(qstr *qstr_table);
#endif

typedef struct _mp_raw_code_t {
Expand Down
4 changes: 2 additions & 2 deletions py/nativeglue.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ const void *const mp_fun_table[MP_F_NUMBER_OF] = {
};

#if MICROPY_PERSISTENT_NATIVE
mp_persistent_native_data_t *mp_new_persistent_native_data(size_t num_qstrs) {
mp_persistent_native_data_t *mp_new_persistent_native_data(qstr *qstr_table) {
mp_persistent_native_data_t *data = m_new_obj(mp_persistent_native_data_t);
data->fun_table = mp_fun_table;
data->qstr_table = m_new0(qstr, num_qstrs);
data->qstr_table = qstr_table;
data->data = NULL;
return data;
}
Expand Down
18 changes: 12 additions & 6 deletions py/persistentcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,27 @@ STATIC mp_raw_code_t *load_raw_code_bytecode(mp_reader_t *reader) {

#if MICROPY_PERSISTENT_NATIVE
mp_raw_code_t *load_raw_code_native(mp_reader_t *reader) {
byte header[11];
read_bytes(reader, header, 11);
if (header[0] != MP_PERSISTENT_ARCH_CURRENT) {
byte arch = reader->readbyte(reader->data);
if (arch != MP_PERSISTENT_ARCH_CURRENT) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, ".mpy has wrong arch"));
}
uint num_qstrs = header[1] | (header[2] << 8);

// load qstrs
uint num_qstrs = read_uint(reader);
qstr *qstr_table = m_new0(qstr, num_qstrs);
qstr *q = qstr_table;
for (size_t i=0; i<num_qstrs; i++) {
*q++ = load_qstr(reader);
}

// load machine code
mp_uint_t len = header[3] | (header[4] << 8);
mp_uint_t len = read_uint(reader);
void *data;
size_t alloc;
MP_PLAT_ALLOC_EXEC(len, &data, &alloc);
read_bytes(reader, data, len);

mp_persistent_native_data_t *per_nat_data = mp_new_persistent_native_data(num_qstrs);
mp_persistent_native_data_t *per_nat_data = mp_new_persistent_native_data(qstr_table);

// create raw_code and return it
mp_raw_code_t *rc = mp_emit_glue_new_raw_code();
Expand Down
4 changes: 1 addition & 3 deletions py/persistnative.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
// TODO: consolidate the first 4 bytes here with stuff in persistentcode.c
#define MP_PERSISTENT_NATIVE_HEADER \
__attribute__((section(".mpyheader"))) \
const byte header[16] = { \
const byte header[8] = { \
'M', \
2, \
((MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) << 0) \
Expand All @@ -61,8 +61,6 @@
MP_CODE_PERSISTENT_NATIVE, \
MP_PERSISTENT_ARCH_CURRENT, \
(MP_LOCAL_QSTR_number_of & 0xff), (MP_LOCAL_QSTR_number_of >> 8), \
4A3D 0, 0, /* size of text section, to be patched later */ \
0, 0, 0, 0, 0, 0, /* padding */ \
};

#define MP_PERSISTENT_NATIVE_INIT \
Expand Down
0