Page 2 of 2

Re: CC65, array of structures supposedly bad?

Posted: Mon Jun 25, 2018 12:34 am
by DRW
Banshaku wrote:without passing parameter from the C side and how much it would be worth it.
I do it like this:

Code: Select all

void MyFunction(int a, int b, int c);
becomes:

Code: Select all

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_();\
}

Re: CC65, array of structures supposedly bad?

Posted: Mon Jun 25, 2018 6:48 am
by tepples
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: Select all

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();

Re: CC65, array of structures supposedly bad?

Posted: Mon Jun 25, 2018 8:02 am
by DRW
Yeah, I'm already doing this.
Or rather, I did macros for this as well:

Code: Select all

#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: Select all

#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)