summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2022-04-07 22:18:37 +1000
committerDamien George <damien@micropython.org>2022-04-14 22:44:04 +1000
commit42d0bd2c17fd76860c3dc0ef5c62faa48efeb121 (patch)
tree9bba461ddcca0641750c9b761bdaa6d0aabf7433
parent988827b85a9b09f1164e101ea2520562dd31634e (diff)
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>
-rw-r--r--py/persistentcode.c30
-rw-r--r--py/persistentcode.h10
-rwxr-xr-xtools/mpy-tool.py26
3 files changed, 42 insertions, 24 deletions
diff --git a/py/persistentcode.c b/py/persistentcode.c
index f64e383a6..c9cfc3b5c 100644
--- a/py/persistentcode.c
+++ b/py/persistentcode.c
@@ -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;
diff --git a/py/persistentcode.h b/py/persistentcode.h
index 1991ba26f..7426029ad 100644
--- a/py/persistentcode.h
+++ b/py/persistentcode.h
@@ -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);
diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py
index 84c09a0c6..fc0b7c09c 100755
--- a/tools/mpy-tool.py
+++ b/tools/mpy-tool.py
@@ -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")