4

I need macro which expands into multiple lines of code. For example:

#define foo(...)
foo(something, something_else, ...)
...

Should be converted into:

something
something_else
...

And not into:

something something_else ...

Also if you wonder why I need such thing. I need to generate such code, new line is part of inline assembly syntax.

_asm
{
    mov eax, 3
    div 5
}

I'm interested in any form of achieving this, so all suggestions are welcome.

Just an idea after reading this answer. Would it be possible to have a macro for new line and call foo(something, NL, something_else, NL, ...)?


I'm also more interested into variadic version but knowing simpler version may also help.

Community
  • 1
  • 1
ST3
  • 8,826
  • 3
  • 68
  • 92
  • 4
    You cannot. This doesn't even make sense, as there are no longer any lines after preprocessing. There is only an unbroken stream of tokens. – n. m. could be an AI Jul 27 '15 at 08:36
  • 1
    You *can* nest _asm statements and/or give each instruction its own _asm prefix. You don't need newlines in this case. – n. m. could be an AI Jul 27 '15 at 08:47
  • Depending how your code generation works exactly, you could also get away with just `#include`ing different snippets of code which make use of variables you define prior to `#include`ing. – Frerich Raabe Jul 27 '15 at 08:49
  • @n.m. Have been thinking about that. However instruction may have or may not have comma, so that annoys me. – ST3 Jul 27 '15 at 08:49
  • I have to ask why? What does it achieve to put your assembly language instructions in a macro? – JeremyP Jul 27 '15 at 08:58
  • The compiler used here might be relevant. With `gcc`, inline assembly in a single line wouldn't be an issue... –  Jul 27 '15 at 09:03
  • @JeremyP It is long story. In short it saves time. – ST3 Jul 27 '15 at 09:03
  • @FelixPalmen however VS-2012 is used. – ST3 Jul 27 '15 at 09:05
  • 1
    @ST3 how does writing `foo( .... )` save time over writing `_asm { ... }`? The answer is important because, depending on what it is, I might have a solution for you that isn't what you asked for exactly but will do the job. – JeremyP Jul 27 '15 at 09:07
  • @ST3 look here: https://msdn.microsoft.com/en-us/library/352sth8z.aspx -- the syntax looks a bit silly, but from how I read it, you get your `__asm` block all in one line by prefixing each single instruction with `__asm` –  Jul 27 '15 at 09:08
  • 1
    You may be able to use this http://coliru.stacked-crooked.com/a/27875b7bbe4cc6aa – n. m. could be an AI Jul 27 '15 at 09:11
  • @n.m. Good suggestion, it is slightly more typing but works. Thanks! You can add this as an answer. – ST3 Jul 27 '15 at 10:58

1 Answers1

1

This may provide a little bit of help , to just give you an idea

    #include "stdio.h"
    #define EXPAND_MULTIPLE_LINES(X, Y, Z) /*
    */ X /*
    */ Y /*
    */ Z

    int main()
    {
        int X, Y, Z;
        EXPAND_MULTIPLE_LINES(X, Y, Z);
        return 0;
    }

use gcc -E -CC testMacro.c

Output like

     # 844 "/usr/include/stdio.h" 3 4

     # 2 "testMacro.c" 2
     # 12 "testMacro.c"
     int main()

     {

          int X, Y, Z;
          /*

          */ X /*
          */ Y /*
          */ Z;
          # 18 "testMacro.c"
          return 0;
     }