Commit 44e6e348 authored by Damien George's avatar Damien George
Browse files

py/emitglue: Implement persistent saving and loading of const objects.

parent 39a8deb9
...@@ -226,8 +226,13 @@ STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_ ...@@ -226,8 +226,13 @@ STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_
#if MICROPY_PERSISTENT_CODE_LOAD #if MICROPY_PERSISTENT_CODE_LOAD
#include "py/parsenum.h"
#include "py/bc0.h" #include "py/bc0.h"
STATIC int read_byte(mp_reader_t *reader) {
return reader->read_byte(reader->data);
}
STATIC void read_bytes(mp_reader_t *reader, byte *buf, size_t len) { STATIC void read_bytes(mp_reader_t *reader, byte *buf, size_t len) {
while (len-- > 0) { while (len-- > 0) {
*buf++ = reader->read_byte(reader->data); *buf++ = reader->read_byte(reader->data);
...@@ -256,9 +261,23 @@ STATIC qstr load_qstr(mp_reader_t *reader) { ...@@ -256,9 +261,23 @@ STATIC qstr load_qstr(mp_reader_t *reader) {
} }
STATIC mp_obj_t load_obj(mp_reader_t *reader) { STATIC mp_obj_t load_obj(mp_reader_t *reader) {
(void)reader; byte obj_type = read_byte(reader);
assert(0); if (obj_type == 'e') {
return MP_OBJ_NULL; return (mp_obj_t)&mp_const_ellipsis_obj;
} else {
size_t len = read_uint(reader);
vstr_t vstr;
vstr_init_len(&vstr, len);
read_bytes(reader, (byte*)vstr.buf, len);
if (obj_type == 's' || obj_type == 'b') {
return mp_obj_new_str_from_vstr(obj_type == 's' ? &mp_type_str : &mp_type_bytes, &vstr);
} else if (obj_type == 'i') {
return mp_parse_num_integer(vstr.buf, vstr.len, 10, NULL);
} else {
assert(obj_type == 'f' || obj_type == 'c');
return mp_parse_num_decimal(vstr.buf, vstr.len, obj_type == 'c', false, NULL);
}
}
} }
STATIC void load_bytecode_qstrs(mp_reader_t *reader, byte *ip, byte *ip_top) { STATIC void load_bytecode_qstrs(mp_reader_t *reader, byte *ip, byte *ip_top) {
...@@ -453,6 +472,9 @@ mp_raw_code_t *mp_raw_code_load_file(const char *filename) { ...@@ -453,6 +472,9 @@ mp_raw_code_t *mp_raw_code_load_file(const char *filename) {
#endif // MICROPY_PERSISTENT_CODE_LOAD #endif // MICROPY_PERSISTENT_CODE_LOAD
#if MICROPY_PERSISTENT_CODE_SAVE #if MICROPY_PERSISTENT_CODE_SAVE
#include "py/objstr.h"
STATIC void mp_print_bytes(mp_print_t *print, const byte *data, size_t len) { STATIC void mp_print_bytes(mp_print_t *print, const byte *data, size_t len) {
print->print_strn(print->data, (const char*)data, len); print->print_strn(print->data, (const char*)data, len);
} }
...@@ -477,38 +499,41 @@ STATIC void save_qstr(mp_print_t *print, qstr qst) { ...@@ -477,38 +499,41 @@ STATIC void save_qstr(mp_print_t *print, qstr qst) {
} }
STATIC void save_obj(mp_print_t *print, mp_obj_t o) { STATIC void save_obj(mp_print_t *print, mp_obj_t o) {
if (MP_OBJ_IS_STR(o)) { if (MP_OBJ_IS_STR_OR_BYTES(o)) {
byte buf[] = {'s'}; byte obj_type;
mp_print_bytes(print, buf, 1); if (MP_OBJ_IS_STR(o)) {
mp_uint_t len; obj_type = 's';
const char *str = mp_obj_str_get_data(o, &len); } else {
mp_print_uint(print, len); obj_type = 'b';
mp_print_bytes(print, (const byte*)str, len); }
} else if (MP_OBJ_IS_TYPE(o, &mp_type_bytes)) {
byte buf[] = {'b'};
mp_print_bytes(print, buf, 1);
mp_uint_t len; mp_uint_t len;
const char *str = mp_obj_str_get_data(o, &len); const char *str = mp_obj_str_get_data(o, &len);
mp_print_bytes(print, &obj_type, 1);
mp_print_uint(print, len); mp_print_uint(print, len);
mp_print_bytes(print, (const byte*)str, len); mp_print_bytes(print, (const byte*)str, len);
} else if (MP_OBJ_IS_TYPE(o, &mp_type_int)) {
byte buf[] = {'i'};
mp_print_bytes(print, buf, 1);
// TODO
} else if (MP_OBJ_IS_TYPE(o, &mp_type_float)) {
byte buf[] = {'f'};
mp_print_bytes(print, buf, 1);
// TODO
} else if (MP_OBJ_IS_TYPE(o, &mp_type_complex)) {
byte buf[] = {'c'};
mp_print_bytes(print, buf, 1);
// TODO
} else if (o == &mp_const_ellipsis_obj) { } else if (o == &mp_const_ellipsis_obj) {
byte buf[] = {'e'}; byte obj_type = 'e';
mp_print_bytes(print, buf, 1); mp_print_bytes(print, &obj_type, 1);
} else { } else {
mp_obj_print(o, PRINT_STR); // we save numbers using a simplistic text representation
assert(0); // TODO could be improved
byte obj_type;
if (MP_OBJ_IS_TYPE(o, &mp_type_int)) {
obj_type = 'i';
} else if (MP_OBJ_IS_TYPE(o, &mp_type_float)) {
obj_type = 'f';
} else {
assert(MP_OBJ_IS_TYPE(o, &mp_type_complex));
obj_type = 'c';
}
vstr_t vstr;
mp_print_t pr;
vstr_init_print(&vstr, 10, &pr);
mp_obj_print_helper(&pr, o, PRINT_REPR);
mp_print_bytes(print, &obj_type, 1);
mp_print_uint(print, vstr.len);
mp_print_bytes(print, (const byte*)vstr.buf, vstr.len);
vstr_clear(&vstr);
} }
} }
......
Supports Markdown
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