summaryrefslogtreecommitdiff
path: root/py/objfun.h
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2024-02-16 16:53:47 +1100
committerDamien George <damien@micropython.org>2024-02-20 10:56:24 +1100
commit6d403eb6972b7f6137838d89dba1ae3f76846c8b (patch)
tree2d466c2a66a011be1e5896d2939b66e7af7fd236 /py/objfun.h
parent9400229766624e80db6a6f95af287a5542dc1b43 (diff)
py/emitnative: Simplify layout and loading of native function prelude.
Now native functions and native generators have similar behaviour: the first machine-word of their code is an index to get to the prelude. This simplifies the handling of these types of functions, and also reduces the size of the emitted native machine code by no longer requiring special code at the start of the function to load a pointer to the prelude. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'py/objfun.h')
-rw-r--r--py/objfun.h28
1 files changed, 28 insertions, 0 deletions
diff --git a/py/objfun.h b/py/objfun.h
index ddb148dd1..af7c33485 100644
--- a/py/objfun.h
+++ b/py/objfun.h
@@ -69,6 +69,34 @@ static inline mp_obj_t mp_obj_new_fun_viper(const void *fun_data, const mp_modul
return MP_OBJ_FROM_PTR(o);
}
+static inline const uint8_t *mp_obj_fun_native_get_prelude_ptr(const mp_obj_fun_bc_t *fun_native) {
+ // Obtain a pointer to the start of the function prelude, based on prelude_ptr_index.
+ uintptr_t prelude_ptr_index = ((uintptr_t *)fun_native->bytecode)[0];
+ const uint8_t *prelude_ptr;
+ if (prelude_ptr_index == 0) {
+ prelude_ptr = (const uint8_t *)fun_native->child_table;
+ } else {
+ prelude_ptr = (const uint8_t *)fun_native->child_table[prelude_ptr_index];
+ }
+ return prelude_ptr;
+}
+
+static inline void *mp_obj_fun_native_get_function_start(const mp_obj_fun_bc_t *fun_native) {
+ // Obtain a pointer to the start of the function executable machine code.
+ return MICROPY_MAKE_POINTER_CALLABLE((void *)(fun_native->bytecode + sizeof(uintptr_t)));
+}
+
+static inline void *mp_obj_fun_native_get_generator_start(const mp_obj_fun_bc_t *fun_native) {
+ // Obtain a pointer to the start of the generator executable machine code.
+ uintptr_t start_offset = ((uintptr_t *)fun_native->bytecode)[1];
+ return MICROPY_MAKE_POINTER_CALLABLE((void *)(fun_native->bytecode + start_offset));
+}
+
+static inline void *mp_obj_fun_native_get_generator_resume(const mp_obj_fun_bc_t *fun_native) {
+ // Obtain a pointer to the resume location of the generator executable machine code.
+ return MICROPY_MAKE_POINTER_CALLABLE((void *)&((uintptr_t *)fun_native->bytecode)[2]);
+}
+
#endif
#if MICROPY_EMIT_INLINE_ASM