Skip to content
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

py: fix native emitter to work with async with statements #15310

Merged
merged 5 commits into from
Jun 21, 2024

Conversation

dpgeorge
Copy link
Member

This fixes the native emitter so that async with statements now work with it.


This PR also includes a new "debug native emitter" that can be used to print out pseudo instructions generated by the native emitter, to help debug issues with it. Eg with the following test.py file:

@micropython.native
def f():
    return 1

mpy-cross can be used as follows to get debug output from the native emitter:

$ ./build/mpy-cross -march=debug test.py
ENTRY(num_locals=6)
    load(r_fun_table, r_arg1, 1)
    load(r_local3, r_fun_table, 2)
    load(r_fun_table, r_fun_table, 3)
    load(r_fun_table, r_fun_table, 0)
    mov_local_reg(local_0, r_arg1)
    mov_reg_imm(r_arg1, 1=0x1)
    mov_local_reg(local_3, r_arg1)
    mov_reg_local_addr(r_arg1, local_0)
    call_ind(setup_code_state)
    mov_reg_imm(r_ret, 3=0x3)
    jump(label_0)
    dead_code load(r_ret, r_fun_table, 0)
    dead_code jump(label_0)
label(label_0)
EXIT(0)

Copy link

github-actions bot commented Jun 20, 2024

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:  +584 +0.071% standard
      stm32:  +232 +0.059% PYBV10
     mimxrt:  +240 +0.066% TEENSY40
        rp2:  +232 +0.026% RPI_PICO_W
       samd:  +240 +0.090% ADAFRUIT_ITSYBITSY_M4_EXPRESS

@dpgeorge dpgeorge force-pushed the py-emitnative-fix-async-with branch 2 times, most recently from 4e890e9 to 20bec40 Compare June 20, 2024 02:44
Copy link

codecov bot commented Jun 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.42%. Comparing base (c1882e4) to head (758f771).

Current head 758f771 differs from pull request most recent head 0619f26

Please upload reports for the commit 0619f26 to get more accurate results.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #15310   +/-   ##
=======================================
  Coverage   98.42%   98.42%           
=======================================
  Files         161      161           
  Lines       21204    21234   +30     
=======================================
+ Hits        20870    20900   +30     
  Misses        334      334           

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

@dpgeorge dpgeorge force-pushed the py-emitnative-fix-async-with branch from 20bec40 to 758f771 Compare June 20, 2024 02:53
Signed-off-by: Damien George <damien@micropython.org>
This emitter prints out pseudo-machine instructions, instead of the usual
output of the native emitter.  It can be enabled on any port via
`MICROPY_EMIT_NATIVE_DEBUG` (make sure other native emitters are disabled)
but the easiest way to use it is with mpy-cross:

    $ mpy-cross -march=debug file.py

Signed-off-by: Damien George <damien@micropython.org>
A value thrown/injected into a native generator needs to be stored in a
dedicated variable outside `nlr_buf_t`, following the `inject_exc` variable
in `py/vm.c`.

Signed-off-by: Damien George <damien@micropython.org>
The code generating the entry to the finally handler of an async-with
statement was simply wrong for the case of the native emitter.  Among other
things the layout of the stack was incorrect.

This is fixed by this commit.  The setup of the async-with finally handler
is now put in a dedicated emit function, for both the bytecode and native
emitters to implement in their own way (the bytecode emitter is unchanged,
just factored to a function).

With this fix all of the async-with tests now work when using the native
emitter.

Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the py-emitnative-fix-async-with branch from 758f771 to 0619f26 Compare June 21, 2024 06:23
@dpgeorge dpgeorge merged commit 0619f26 into micropython:master Jun 21, 2024
60 of 61 checks passed
@dpgeorge dpgeorge deleted the py-emitnative-fix-async-with branch June 21, 2024 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant