summaryrefslogtreecommitdiff
path: root/py/emitglue.h
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2024-02-09 17:41:48 +1100
committerDamien George <damien@micropython.org>2024-02-16 14:17:01 +1100
commite2ff00e81113d7a3f32f860652017644b5d68bf1 (patch)
tree061a1d917a23be78706c3a4cfd6a46b1307a4ebf /py/emitglue.h
parent5e3006f1172d0eabbbefeb3268dfb942ec7cf9cd (diff)
py/emitglue: Introduce mp_proto_fun_t as a more general mp_raw_code_t.
Allows bytecode itself to be used instead of an mp_raw_code_t in the simple and common cases of a bytecode function without any children. This can be used to further reduce frozen code size, and has the potential to optimise other areas like importing. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'py/emitglue.h')
-rw-r--r--py/emitglue.h27
1 files changed, 22 insertions, 5 deletions
diff --git a/py/emitglue.h b/py/emitglue.h
index 125ab2ccd..b1c10a1ee 100644
--- a/py/emitglue.h
+++ b/py/emitglue.h
@@ -31,6 +31,11 @@
// These variables and functions glue the code emitters to the runtime.
+// Used with mp_raw_code_t::proto_fun_indicator to detect if a mp_proto_fun_t is a
+// mp_raw_code_t struct or a direct pointer to bytecode.
+#define MP_PROTO_FUN_INDICATOR_RAW_CODE_0 (0)
+#define MP_PROTO_FUN_INDICATOR_RAW_CODE_1 (0)
+
// These must fit in 8 bits; see scope.h
enum {
MP_EMIT_OPT_NONE,
@@ -49,14 +54,25 @@ typedef enum {
MP_CODE_NATIVE_ASM,
} mp_raw_code_kind_t;
-// This mp_raw_code_t struct holds static information about a non-instantiated function.
+// An mp_proto_fun_t points to static information about a non-instantiated function.
// A function object is created from this information, and that object can then be executed.
-//
-// This struct appears in the following places:
+// It points either to bytecode, or an mp_raw_code_t struct.
+typedef const void *mp_proto_fun_t;
+
+// Bytecode is distinguished from an mp_raw_code_t struct by the first two bytes: bytecode
+// is guaranteed to have either its first or second byte non-zero. So if both bytes are
+// zero then the mp_proto_fun_t pointer must be an mp_raw_code_t.
+static inline bool mp_proto_fun_is_bytecode(mp_proto_fun_t proto_fun) {
+ const uint8_t *header = proto_fun;
+ return (header[0] | (header[1] << 8)) != (MP_PROTO_FUN_INDICATOR_RAW_CODE_0 | (MP_PROTO_FUN_INDICATOR_RAW_CODE_1 << 8));
+}
+
+// The mp_raw_code_t struct appears in the following places:
// compiled bytecode: instance in RAM, referenced by outer scope, usually freed after first (and only) use
// mpy file: instance in RAM, created when .mpy file is loaded (same comments as above)
// frozen: instance in ROM
typedef struct _mp_raw_code_t {
+ uint8_t proto_fun_indicator[2];
uint8_t kind; // of type mp_raw_code_kind_t; only 3 bits used
bool is_generator;
const void *fun_data;
@@ -88,6 +104,7 @@ typedef struct _mp_raw_code_t {
// only needed when the kind is MP_CODE_NATIVE_ASM. So this struct can be used when the
// kind is MP_CODE_BYTECODE, MP_CODE_NATIVE_PY or MP_CODE_NATIVE_VIPER, to reduce its size.
typedef struct _mp_raw_code_truncated_t {
+ uint8_t proto_fun_indicator[2];
uint8_t kind;
bool is_generator;
const void *fun_data;
@@ -127,7 +144,7 @@ void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void
#endif
uint16_t scope_flags, uint32_t asm_n_pos_args, uint32_t asm_type_sig);
-mp_obj_t mp_make_function_from_raw_code(const mp_raw_code_t *rc, const mp_module_context_t *context, const mp_obj_t *def_args);
-mp_obj_t mp_make_closure_from_raw_code(const mp_raw_code_t *rc, const mp_module_context_t *context, mp_uint_t n_closed_over, const mp_obj_t *args);
+mp_obj_t mp_make_function_from_proto_fun(mp_proto_fun_t proto_fun, const mp_module_context_t *context, const mp_obj_t *def_args);
+mp_obj_t mp_make_closure_from_proto_fun(mp_proto_fun_t proto_fun, const mp_module_context_t *context, mp_uint_t n_closed_over, const mp_obj_t *args);
#endif // MICROPY_INCLUDED_PY_EMITGLUE_H