#1844 multiline macros with __asm become C multiple statements

closed-fixed
Maarten Brock
5
2013-05-25
2011-09-10
Maarten Brock
No

When the preprcossor changes a multiline assembly macro into multiple __asm / __endasm blocks in order to preserve line endings it breaks up a single C statement into multiple C statements. This can break code if the statement is after a condition. E.g.

#define NOPS \ __asm \ nop \ nop \ __endasm

void test(void)
{
if (condition)
NOPS;
}

This will be changed into

void test(void)
{
if (condition)
__asm nop __endasm;
__asm nop __endasm;
}

Discussion

  • Borut Ražem
    Borut Ražem
    2011-09-11

    This problem raised with introduction of backslash replacement by "__endasm; __asm" in the macro definition, where a single C statement is automagically converted into multiple C statements by the preprocessor.

    The solution is:
    [code]
    #define NOPS \ do {
    __asm \ nop \ nop \ __endasm
    } while (0)
    [/code]

    See also bug #3407198.

    Borut

     
  • Borut Ražem
    Borut Ražem
    2011-09-11

    In my previous post the semicolon and backslash after __endasm are missing.

    The correct macro definition is:

    #define NOPS \ do {
    __asm \ nop \ nop \ __endasm; \ } while (0)

    or much better and cleaner:

    #define NOPS \ do { \ __asm nop __endasm; \ __asm nop __endasm; \ } while (0)

     
  • Maarten Brock
    Maarten Brock
    2011-09-14

    Fixed in SDCC 3.0.4 #6852 by inserting '‡' (0x87) instead of '__endasm; __asm' for line continuations inside __asm / __endasm blocks.

     
  • Maarten Brock
    Maarten Brock
    2011-09-14

    • milestone: --> fixed
    • assigned_to: nobody --> maartenbrock
    • status: open --> closed-fixed