nesdev.com
http://forums.nesdev.com/

CC65, array of structures supposedly bad?
http://forums.nesdev.com/viewtopic.php?f=2&t=17465
Page 2 of 2

Author:  DRW [ Mon Jun 25, 2018 12:34 am ]
Post subject:  Re: CC65, array of structures supposedly bad?

Banshaku wrote:
without passing parameter from the C side and how much it would be worth it.

I do it like this:
Code:
void MyFunction(int a, int b, int c);

becomes:
Code:
int MyFunction_A;
int MyFunction_B;
int MyFunction_C;

void MyFunction_(void);

#define MyFunction(a, b, c)\
{\
    MyFunction_A = a;\
    MyFunction_B = b;\
    MyFunction_C = c;\
    MyFunction_();\
}

Author:  tepples [ Mon Jun 25, 2018 6:48 am ]
Post subject:  Re: CC65, array of structures supposedly bad?

You appear to be using a preprocessor macro as a workaround for lack of static inline. For this to work, you will need to change the { and } in your macro to do { and } while (0) respectively in order to keep the following semicolon from interfering with the parsing of if...else constructions. Otherwise, the macro will expand undesirably as follows:
Code:
if (some_condition)
    MyFunction(a, b, c);
else
    SomethingElse();

/* becomes, after preprocessing and reindenting */

if (some_condition)
    {
        MyFunction_A = a;
        MyFunction_B = b;
        MyFunction_C = c;
        MyFunction_();
    }

;  /* This ends the if statement because it is something other than else that directly follows its first statement or compound statement. */
else  /* This is a syntax error because it does not follow an open if statement. */
    SomethingElse();

/* But with my suggestion, it would become */

if (some_condition)
    do {
        MyFunction_A = a;
        MyFunction_B = b;
        MyFunction_C = c;
        MyFunction_();
    } while (0);  /* the do...while swallows up the semicolon */
else  /* properly recognized as belonging to the if statement */
    SomethingElse();

Author:  DRW [ Mon Jun 25, 2018 8:02 am ]
Post subject:  Re: CC65, array of structures supposedly bad?

Yeah, I'm already doing this.
Or rather, I did macros for this as well:
Code:
#define MACRO_START do {
#define MACRO_END } while (false)


I used the brackets in the example for simplicity's sake.

One thing to keep in mind in this construct:
If you ever feel the desire to include a break out of a loop inside a macro, you cannot do this if the loop exists outside the macro:
Code:
#define IfErrorThenBreak(errorCode)\
do\
{\
    if ((errorCode) != 0)\
        break; /* Will not work as intended since it refers\
                  to the do while loop, not to some outside loop. */\
}\
while (false)

Page 2 of 2 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/