This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to conditionally NOAREG a fn?

Hi All.

Imagine a function that looks like this:

DEFINED_SYMBOL1
type FnName(args) DEFINED_SYMBOL2
{
 ...;
}
By appropriate definition of DEFINED_SYMBOL1 and DEFINED_SYMBOL2, I would like to end up with:
type FnName(args)
{
 ...;
}
or
#pragma NOAREGS
type FnName(args) reentrant
{
 ...;
}
The DEFINED_SYMBOL2 part is really easy, either
#define DEFINED_SYMBOL2
or
>#define DEFINED_SYMBOL2 reentrant
But the DEFINED_SYMBOL1 isn't so easy, because C51 won't allow me to
#define DEFINED_SYMBOL1 #pragma NOAREGS
("misused # operator"). So how should I proceed?

BTW, the source file has multiple functuons in it, so compiling with NAREGS on the command line is not an option.

Thanks,

  • How about:

    #if defined( USE_REENTRANT )
    // Use the Keil C51 'reentrant' extended keyword
    #define REENTRANT reentrant
    #else
    // Do not use 'reentrant' - the expansion is nothing!
    #define REENTRANT
    #endif
    
    #if defined( USE_NOAREGS )
    // The NOAREGS Compiler Option has been requested
    #pragma NOAREGS
    #endif
    type FnName(args) REENTRANT
    {
     ...;
    }
    Where your predefined symbols are USE_REENTRANT and USE_NOAREGS

    NB: The USE_REENTRANT techniques illustrates a good way to "isolate" all your compiler-dependencies - just use __C51__ as your predefined symbol

  • One C rule you must obey is that you can't have a pre-proc. directive in a pre-proc. directive. E.g.

    #define ILLEGAL  #pragma 
    won't work.

    There is an exception for token pasting and concatenation, e.g.
    #define STRINGER(x)    #x
    #define STRING_CAT(x, y) STRINGER(x) ## STRINGER(y)
    Which can be fun.

  • Hi Guys.

    Thanks for the replies.

    The method I'm now using is like what Andrew suggested, but taken one step further because we try to avoid having any compiler-specific code in our *.c source files.

    How do we do that?

    Well, the reentrant stuff is easy because it's a keyword. For the pragmas, we conditionally include a file just prior to the function of interest. In that file, if the compiler is C51 AND that particular function is the one we're interested in AND it needs the pragma (all based on values of defined symbols), then the pragma is all that comes out of that file. And we undo the pragma after the file ...

    With this "include-file method" we're able to keep the *.c source files reasonably legible ...

    Regards,

    Andrew

  • "we try to avoid having any compiler-specific code in our *.c source files.
    "


    Excellent!

    "For the pragmas, we conditionally include a file..."

    That sounds clever - I'll bear it in mind if I need to do such a thing.