raspberrypi / pico-sdk

BSD 3-Clause "New" or "Revised" License
3.24k stars 837 forks source link

.wrap_target at the end of a PIO program is allowed, but generates bad code #1712

Open davidgiven opened 1 month ago

davidgiven commented 1 month ago

I wanted a program which ran once and halted, spinning forever until explicitly restarted by code. So I wrote this:

.program test
    nop
    .wrap_target
    .wrap

And I got this:

#define test_wrap_target 1
#define test_wrap 0

This is technically correct. After the instruction at address 1 (the nop) is complete, the program counter is moved to... the address after. Which is correct because that's where I put the .wrap_target, but is meaningless.

What's happened here is that .wrap_target looks like it's an instruction which gets jumped to, but it's not. It just labels the next instruction, and the end of the program is not an instruction. This was a mistake on my part, but ideally the assembler should have caught this and complained that what I was trying to do wasn't valid. I only realised what was going on once I looked at the generated code.

Feature request: please spot when I do this and error out.

Thanks!