py/persistentcode: Define enum values for obj types instead of letters.

To keep the separate parts of the code that use these values in sync.  And
make it easier to add new object types.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2022-04-07 22:18:37 +10:00
parent 988827b85a
commit 42d0bd2c17
3 changed files with 42 additions and 24 deletions

View File

@ -198,29 +198,29 @@ STATIC qstr load_qstr(mp_reader_t *reader) {
STATIC mp_obj_t load_obj(mp_reader_t *reader) {
byte obj_type = read_byte(reader);
#if MICROPY_EMIT_MACHINE_CODE
if (obj_type == 't') {
if (obj_type == MP_PERSISTENT_OBJ_FUN_TABLE) {
return MP_OBJ_FROM_PTR(&mp_fun_table);
} else
#endif
if (obj_type == 'e') {
if (obj_type == MP_PERSISTENT_OBJ_ELLIPSIS) {
return MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj);
} else {
size_t len = read_uint(reader);
if (len == 0 && obj_type == 'b') {
if (len == 0 && obj_type == MP_PERSISTENT_OBJ_BYTES) {
read_byte(reader); // skip null terminator
return mp_const_empty_bytes;
}
vstr_t vstr;
vstr_init_len(&vstr, len);
read_bytes(reader, (byte *)vstr.buf, len);
if (obj_type == 's' || obj_type == 'b') {
if (obj_type == MP_PERSISTENT_OBJ_STR || obj_type == MP_PERSISTENT_OBJ_BYTES) {
read_byte(reader); // skip null terminator
return mp_obj_new_str_from_vstr(obj_type == 's' ? &mp_type_str : &mp_type_bytes, &vstr);
} else if (obj_type == 'i') {
return mp_obj_new_str_from_vstr(obj_type == MP_PERSISTENT_OBJ_STR ? &mp_type_str : &mp_type_bytes, &vstr);
} else if (obj_type == MP_PERSISTENT_OBJ_INT) {
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);
assert(obj_type == MP_PERSISTENT_OBJ_FLOAT || obj_type == MP_PERSISTENT_OBJ_COMPLEX);
return mp_parse_num_decimal(vstr.buf, vstr.len, obj_type == MP_PERSISTENT_OBJ_COMPLEX, false, NULL);
}
}
}
@ -498,16 +498,16 @@ STATIC void save_qstr(mp_print_t *print, qstr qst) {
STATIC void save_obj(mp_print_t *print, mp_obj_t o) {
#if MICROPY_EMIT_MACHINE_CODE
if (o == MP_OBJ_FROM_PTR(&mp_fun_table)) {
byte obj_type = 't';
byte obj_type = MP_PERSISTENT_OBJ_FUN_TABLE;
mp_print_bytes(print, &obj_type, 1);
} else
#endif
if (mp_obj_is_str_or_bytes(o)) {
byte obj_type;
if (mp_obj_is_str(o)) {
obj_type = 's';
obj_type = MP_PERSISTENT_OBJ_STR;
} else {
obj_type = 'b';
obj_type = MP_PERSISTENT_OBJ_BYTES;
}
size_t len;
const char *str = mp_obj_str_get_data(o, &len);
@ -515,21 +515,21 @@ STATIC void save_obj(mp_print_t *print, mp_obj_t o) {
mp_print_uint(print, len);
mp_print_bytes(print, (const byte *)str, len + 1); // +1 to store null terminator
} else if (MP_OBJ_TO_PTR(o) == &mp_const_ellipsis_obj) {
byte obj_type = 'e';
byte obj_type = MP_PERSISTENT_OBJ_ELLIPSIS;
mp_print_bytes(print, &obj_type, 1);
} else {
// we save numbers using a simplistic text representation
// TODO could be improved
byte obj_type;
if (mp_obj_is_int(o)) {
obj_type = 'i';
obj_type = MP_PERSISTENT_OBJ_INT;
#if MICROPY_PY_BUILTINS_COMPLEX
} else if (mp_obj_is_type(o, &mp_type_complex)) {
obj_type = 'c';
obj_type = MP_PERSISTENT_OBJ_COMPLEX;
#endif
} else {
assert(mp_obj_is_float(o));
obj_type = 'f';
obj_type = MP_PERSISTENT_OBJ_FLOAT;
}
vstr_t vstr;
mp_print_t pr;

View File

@ -102,6 +102,16 @@ enum {
MP_NATIVE_ARCH_XTENSAWIN,
};
enum {
MP_PERSISTENT_OBJ_FUN_TABLE = 0,
MP_PERSISTENT_OBJ_ELLIPSIS,
MP_PERSISTENT_OBJ_STR,
MP_PERSISTENT_OBJ_BYTES,
MP_PERSISTENT_OBJ_INT,
MP_PERSISTENT_OBJ_FLOAT,
MP_PERSISTENT_OBJ_COMPLEX,
};
mp_compiled_module_t mp_raw_code_load(mp_reader_t *reader, mp_module_context_t *ctx);
mp_compiled_module_t mp_raw_code_load_mem(const byte *buf, size_t len, mp_module_context_t *ctx);
mp_compiled_module_t mp_raw_code_load_file(const char *filename, mp_module_context_t *ctx);

View File

@ -121,6 +121,14 @@ MP_NATIVE_ARCH_ARMV7EMDP = 8
MP_NATIVE_ARCH_XTENSA = 9
MP_NATIVE_ARCH_XTENSAWIN = 10
MP_PERSISTENT_OBJ_FUN_TABLE = 0
MP_PERSISTENT_OBJ_ELLIPSIS = 1
MP_PERSISTENT_OBJ_STR = 2
MP_PERSISTENT_OBJ_BYTES = 3
MP_PERSISTENT_OBJ_INT = 4
MP_PERSISTENT_OBJ_FLOAT = 5
MP_PERSISTENT_OBJ_COMPLEX = 6
MP_SCOPE_FLAG_VIPERRELOC = 0x10
MP_SCOPE_FLAG_VIPERRODATA = 0x20
MP_SCOPE_FLAG_VIPERBSS = 0x40
@ -1104,26 +1112,26 @@ def read_qstr(reader, segments):
def read_obj(reader, segments):
obj_type = reader.read_bytes(1)
if obj_type == b"t":
obj_type = reader.read_byte()
if obj_type == MP_PERSISTENT_OBJ_FUN_TABLE:
return MPFunTable()
elif obj_type == b"e":
elif obj_type == MP_PERSISTENT_OBJ_ELLIPSIS:
return Ellipsis
else:
ln = reader.read_uint()
start_pos = reader.tell()
buf = reader.read_bytes(ln)
if obj_type in (b"s", b"b"):
if obj_type in (MP_PERSISTENT_OBJ_STR, MP_PERSISTENT_OBJ_BYTES):
reader.read_byte() # read and discard null terminator
if obj_type == b"s":
if obj_type == MP_PERSISTENT_OBJ_STR:
obj = str_cons(buf, "utf8")
elif obj_type == b"b":
elif obj_type == MP_PERSISTENT_OBJ_BYTES:
obj = buf
elif obj_type == b"i":
elif obj_type == MP_PERSISTENT_OBJ_INT:
obj = int(str_cons(buf, "ascii"), 10)
elif obj_type == b"f":
elif obj_type == MP_PERSISTENT_OBJ_FLOAT:
obj = float(str_cons(buf, "ascii"))
elif obj_type == b"c":
elif obj_type == MP_PERSISTENT_OBJ_COMPLEX:
obj = complex(str_cons(buf, "ascii"))
else:
raise MPYReadError(reader.filename, "corrupt .mpy file")