Ro5bert / avra

Assembler for the Atmel AVR microcontroller family
GNU General Public License v2.0
153 stars 39 forks source link

.elseif doesn't seem to work inside macros #41

Open sdt opened 2 years ago

sdt commented 2 years ago

Hello,

I seem to have run into a problem trying to create a macro that uses .elseif

The code below fails with:

broken.asm(14) : Error : [Macro: broken.asm: 8:] Found no label/variable/constant named

; broken.asm
.macro BROKEN
.endm

.macro BROKEN_i
    .if @0 % 3 == 0
        .message "fizz"
    .elseif @0 % 5 == 0
        .message "buzz"
    .else
        .message @0
    .endif
.endm
BROKEN [7]

The same logic split into nested .else .if blocks works as expected.

; working.asm
.macro WORKING
.endm

.macro WORKING_i
    .if @0 % 3 == 0
        .message "fizz"
    .else
        .if @0 % 5 == 0
            .message "buzz"
        .else
            .message @0
        .endif
    .endif
.endm
WORKING [7]

The .elseif code works normally outside of macros.

Thanks!

sdt commented 2 years ago

I've a had a little bit of a dig around. It looks as though the elseif processing in check_conditional or spool_conditional gets ahead of the macro expansion somehow.

Tracing out the data string that get_expr sees, in the working case, @0 is already expanded out:

7 % 3 == 0
7 % 5 == 0

In the broken case, the .if expression is expanded, but the .elseif expression is not:

7 % 3 == 0
@0 % 5 == 0