Skip to content

py/emitnative: Let emitters know the compiled entity's name. #17572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

agatti
Copy link
Contributor

@agatti agatti commented Jun 27, 2025

Summary

This PR introduces an optional feature to provide to native emitters the fully qualified name of the entity they are compiling.

This is achieved by altering the generic ASM API to provide a third argument to the entry function, containing the name of the entity being compiled. Currently only the debug emitter uses this feature, as it is not really useful for other emitters for the time being; in fact the macros in question just strip the name away.

As discussed in #17548, this brings more descriptive output to the debug emitter, in preparation for a new machine-readable JSON output emitter that would need to know the entity name being processed as well.

Testing

Testing had to be performed manually, by checking the output of mpy-cross -X emit=native -march=debug for a variety of existing MicroPython test files containing all possible entities to be handled by the compiler framework along with their nested variants. The usual battery of CI tests was also ran through Github to be sure nothing out of the ordinary would happen.

Trade-offs and Alternatives

In theory there shouldn't be any changes in any other emitter that actually generates binary code, this code should only affect mpy-cross.

Copy link

github-actions bot commented Jun 27, 2025

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:   +40 +0.021% 
   unix x64:   +40 +0.005% standard
      stm32:    +8 +0.002% PYBV10
     mimxrt:    +8 +0.002% TEENSY40
        rp2:    +8 +0.001% RPI_PICO_W
       samd:    +8 +0.003% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:   +12 +0.003% VIRT_RV32

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jul 1, 2025
@agatti agatti force-pushed the emitnative-provide-entity-name branch from 6cbc681 to 524ce75 Compare July 1, 2025 09:45
Copy link

codecov bot commented Jul 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.56%. Comparing base (4bd9926) to head (6a7d117).
Report is 13 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #17572      +/-   ##
==========================================
- Coverage   98.57%   98.56%   -0.01%     
==========================================
  Files         169      169              
  Lines       21968    21952      -16     
==========================================
- Hits        21654    21638      -16     
  Misses        314      314              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

agatti added 2 commits July 1, 2025 12:04
This commit extends the vstr_t API with an additional function that lets
inserting a null-terminated string anywhere in a vstr_t buffer.

Insertion was only supported for two cases, a single byte or a single
character; both of them would use an internal function that allows
inserting an arbitrary number of bytes anywhere in the vstr_t buffer,
but with a fixed size of 1.

A new function call `vstr_ins_str` is thus added, that still uses the
same mechanism described above, but with the computed string length as
the inserted block size.  The same parameter checking caveats from
`vstr_ins_char` and `vstr_ins_byte` still apply here.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This commit introduces an optional feature to provide to native emitters
the fully qualified name of the entity they are compiling.

This is achieved by altering the generic ASM API to provide a third
argument to the entry function, containing the name of the entity being
compiled.  Currently only the debug emitter uses this feature, as it is
not really useful for other emitters for the time being; in fact the
macros in question just strip the name away.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
@agatti agatti force-pushed the emitnative-provide-entity-name branch from 524ce75 to 6a7d117 Compare July 1, 2025 10:04
@@ -364,6 +364,11 @@ static mp_obj_t extra_coverage(void) {
} else {
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
}

vstr_ins_str(vstr, 0, "\"tests\"");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is just to test vstr_ins_str maybe it should clear the vstr to start with, instead of using vstr_cut_tail_bytes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no other vstr_ins_* function that's called with 0 as its insertion point in this test file. Since all the insertion functions share the same extra prefix allocation bits I thought it'd be nice to cover that specific behaviour too.

But I can let it perform an insertion on an empty vstr too, or better yet cover both cases.

@@ -210,6 +210,12 @@ void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr) {
*s = chr;
}

void vstr_ins_str(vstr_t *vstr, size_t str_pos, const char *str) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this addition is increasing code size for all ports, which is unexpected (due to gc sections).

Needs investigation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for simplicity, just write this function inline where it's used (once)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get there I'd have to make vstr_ins_blank_bytes non-static, I'll check if that would increase the code footprint or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants