From f74c7c6ad06175b2e78073caab3771c350fef7c0 Mon Sep 17 00:00:00 2001 From: Edd Barrett Date: Mon, 16 Mar 2026 14:41:41 +0000 Subject: [PATCH] Hack in debug strings. - Only shows the PC value for now. This is enough if you have a bytecode dump to hand. - Nothing is freed. --- .buildbot.sh | 6 ++++++ README.md | 5 ++++- ports/unix/Makefile | 9 ++++++--- py/emitbc.c | 25 +++++++++++++++++++++++++ py/emitglue.c | 6 ++++++ py/emitglue.h | 6 ++++++ py/persistentcode.c | 3 +++ py/vm.c | 6 ++++++ 8 files changed, 62 insertions(+), 4 deletions(-) diff --git a/.buildbot.sh b/.buildbot.sh index 08f65d4121134..e4723a6ed5bbc 100644 --- a/.buildbot.sh +++ b/.buildbot.sh @@ -78,3 +78,9 @@ cd .. YK_BUILD_TYPE=release-with-asserts make -j "$(nproc)" V=1 # FIXME: add tests once upstream test has been fixed. See above. + +# Check it builds with debug strings. +make clean +YK_BUILD_TYPE=release-with-asserts make -j "$(nproc)" V=1 \ + CFLAGS_EXTRA=-DYKMP_DEBUG_STRS=1 +# FIXME: add tests once upstream test has been fixed. See above. diff --git a/README.md b/README.md index dd5061aa6b46a..7991d3bf4992a 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,12 @@ $ make submodules Then to build with yk support, do: ``` -PATH=/path/to/yk/bin:$PATH YK_BUILD_TYPE= V=1 +PATH=/path/to/yk/bin:$PATH YK_BUILD_TYPE= make V=1 ``` + (To build with yk debug string support, add + `CFLAGS_EXTRA="-DYKMP_DEBUG_STRS=1"` at the end of the `make` invocation) + Then the vm executable can be found at `./build-standard/micropython`. ## yk-related tips diff --git a/ports/unix/Makefile b/ports/unix/Makefile index 44ed2bd9e3ccd..d648e9456736b 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -47,12 +47,15 @@ INC += -I$(TOP) INC += -I$(BUILD) # compiler settings -ifneq ($(strip $(YK_BUILD_TYPE)),) +ifeq ($(strip $(YK_BUILD_TYPE)),) +YK_CFLAGS= +YK_LDFLAGS= +else CC= `yk-config ${YK_BUILD_TYPE} --cc` AR= `yk-config ${YK_BUILD_TYPE} --ar` rcu RANLIB= `yk-config ${YK_BUILD_TYPE} --ranlib` -CFLAGS_EXTRA+= `yk-config ${YK_BUILD_TYPE} --cflags --cppflags` -DUSE_YK -LDFLAGS_EXTRA+= `yk-config ${YK_BUILD_TYPE} --ldflags` \ +CFLAGS+= `yk-config ${YK_BUILD_TYPE} --cflags --cppflags` -DUSE_YK +LDFLAGS+= `yk-config ${YK_BUILD_TYPE} --ldflags` \ `yk-config ${YK_BUILD_TYPE} --libs` -lm endif diff --git a/py/emitbc.c b/py/emitbc.c index b2994e053d47b..91faea251aaac 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -424,10 +424,28 @@ bool mp_emit_bc_end_pass(emit_t *emit) { // If we are jitting, assign locations. // XXX: figure out where to free this. YkLocation *yklocs = m_new(YkLocation, bytecode_len); +#ifdef YKMP_DEBUG_STRS + // XXX: figure out where to free this. + char **ykdstrs = m_new(char *, bytecode_len); +#endif + // Initialise all positions with null locations to start with. for (size_t idx = 0; idx < emit->code_info_size + emit->bytecode_size; idx++) { yklocs[idx] = yk_location_null(); + +#ifdef YKMP_DEBUG_STRS + if (idx >= emit->code_info_size) { + size_t bcoff = idx - emit->code_info_size; + char *dstr; + // XXX Figure out where to free these allocations. + if (asprintf(&dstr, "PC=%zu", bcoff) == -1) { + mp_raise_msg(&mp_type_RuntimeError, + MP_ERROR_TEXT("asprintf failed")); + } + ykdstrs[idx] = dstr; + } +#endif } // Then overwrite places traces can start with proper locations. // XXX: instructions are variable length -- because here we simply bump @@ -450,6 +468,10 @@ bool mp_emit_bc_end_pass(emit_t *emit) { if ((mp_int_t)slab < 0) { const byte *target_ip = ip + slab; yklocs[target_ip - emit->code_base] = yk_location_new(); +#ifdef YKMP_DEBUG_STRS + yk_location_set_debug_str(&yklocs[target_ip - emit->code_base], + ykdstrs[target_ip - emit->code_base]); +#endif continue; } } @@ -464,6 +486,9 @@ bool mp_emit_bc_end_pass(emit_t *emit) { #endif #ifdef USE_YK yklocs, + #ifdef YKMP_DEBUG_STRS + ykdstrs, + #endif #endif emit->scope->scope_flags); } diff --git a/py/emitglue.c b/py/emitglue.c index 24434c6e6943b..25bc1dfcc1639 100644 --- a/py/emitglue.c +++ b/py/emitglue.c @@ -69,6 +69,9 @@ void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, #endif #ifdef USE_YK YkLocation *yklocs, + #ifdef YKMP_DEBUG_STRS + char **ykdstrs, + #endif #endif uint16_t scope_flags) { @@ -77,6 +80,9 @@ void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, rc->fun_data = code; #ifdef USE_YK rc->yklocs = yklocs; +#ifdef YKMP_DEBUG_STRS + rc->ykdstrs = ykdstrs; +#endif #endif rc->children = children; diff --git a/py/emitglue.h b/py/emitglue.h index 7267f0cdde6af..4598bcd8596eb 100644 --- a/py/emitglue.h +++ b/py/emitglue.h @@ -82,6 +82,9 @@ typedef struct _mp_raw_code_t { const void *fun_data; #ifdef USE_YK YkLocation *yklocs; +#ifdef YKMP_DEBUG_STRS + char **ykdstrs; +#endif #endif struct _mp_raw_code_t **children; #if MICROPY_PERSISTENT_CODE_SAVE @@ -137,6 +140,9 @@ void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, #endif #ifdef USE_YK YkLocation *yklocs, + #ifdef YKMP_DEBUG_STRS + char **ykdstrs, + #endif #endif uint16_t scope_flags); diff --git a/py/persistentcode.c b/py/persistentcode.c index 5f0e6f1aa5ffa..3b5146338a1f4 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -404,6 +404,9 @@ static mp_raw_code_t *load_raw_code(mp_reader_t *reader, mp_module_context_t *co #endif #ifdef USE_YK NULL, + #ifdef YKMP_DEBUG_STRS + NULL, + #endif #endif scope_flags); diff --git a/py/vm.c b/py/vm.c index e60c3aa0b8947..3d8c2f13ec591 100644 --- a/py/vm.c +++ b/py/vm.c @@ -310,6 +310,9 @@ FRAME_SETUP(); #ifdef USE_YK YkLocation *yklocs = code_state->fun_bc->rc->yklocs; +#ifdef YKMP_DEBUG_STRS + char **ykdstrs = code_state->fun_bc->rc->ykdstrs; +#endif #endif // outer exception handling loop @@ -352,6 +355,9 @@ FRAME_SETUP(); yk_mt_control_point(mp_state_ctx.vm.ykmt, &yklocs[locidx]); ip = (const byte *) yk_promote((void *) ip); byte opcode = load_inst(ip++); +#ifdef YKMP_DEBUG_STRS + yk_debug_str(ykdstrs[locidx]); +#endif #else byte opcode = *ip++; #endif