0

I would like to ask you about assign specific memory adress for variable in C language. I need to setup Understand SciTool software, and I have some issues about it.

Please have a look:

    #define dPU1      0xF0031
      __SFR_EXTERN__ __near __no_init volatile union
      {
      TByte         ioPU1;
      TBitfieldByte ioPU1_Bits;
      } @dPU1;

dPU1 is a register adress (Renesas RL78). Understand SciTool cant process it. I recived those messages:

[E] pasting formed '@dSMR02', an invalid preprocessing token;

[E] expected ';' after union

[E] expected identifier or '('

I can't find any information about "@" in C language. Any idea? Thanks!

raptor
  • 1
  • 2
  • 2
    `@dPU1` is an illegal identifier in C. It is a compiler extension and static analyzers will complain about it. Find how to disable checks for particular lines – 0___________ Sep 05 '22 at 16:35
  • 3
    @0___________: `@dPU1` is not an “illegal” identifier. It is allowed by C 2018 6.4.2.1 1, which says that an *identifier-nondigit* may be one of “other implementation-defined characters.” It is an extension but is not illegal. – Eric Postpischil Sep 05 '22 at 16:46
  • @EricPostpischil indeed - I should write: in standard C – 0___________ Sep 05 '22 at 17:33
  • 3
    @0___________: C 2018 6.4.2.1 1 is in the standard. Code using it is standard C. `@dPU1` is not an identifier in *strictly conforming* C code. It is *conforming* C code. One could say “`@dPU1` is not a fully portable C identifier.” But it is not illegal. – Eric Postpischil Sep 05 '22 at 17:47
  • @EricPostpischil : I think it is academic, the `@` is not part of the identifier; it is an operator (an extension in IAR's compilers). – Clifford Sep 05 '22 at 21:57
  • What compiler is this for. It does not look correct to me. You have a union declaration, but you cannot locate a _type_, you have no _instance_, and an anonymous union. Should be `... } dPU1 @ 0xF0031 ;` perhaps? (Instead of the macro, or otherwise `... } dDPU1_REG @ dPU1_ADDR ; ` if the address macro is also required, though then `&dDPU1_REG` would be better). – Clifford Sep 05 '22 at 22:28
  • You could help us help you more if you [edit] your question and add a link to the compiler's manual. – the busybee Sep 06 '22 at 05:54
  • @Clifford Yeah, that is a bit strange. Does the IAR compiler extend "anonymous struct/union" to allow an access only by the element's names? – the busybee Sep 06 '22 at 05:57

2 Answers2

1

Many compilers for embedded control accept certain extensions to place objects at absolute addresses.

Apparently your compiler allows to specify it via this notation.

In contrast, code analyzers are generic tools. They rarely know such extensions and so you receive this error message.

This is a good reason to wrap such an extension in a macro. This macro will be differently defined depending on the tool that parses the source. If your compiler reads the source, it provides the absolute address. If the analyzer reads the source, it expands to nothing.

This suggestion is untested:

#if defined(/* some macro that is automatically set by your compiler */)
#define AT(x) @x
#else
#define AT(x)
#endif

#define dPU1      0xF0031

__SFR_EXTERN__ __near __no_init volatile union
{
    TByte         ioPU1;
    TBitfieldByte ioPU1_Bits;
} AT(dPU1);
the busybee
  • 10,755
  • 3
  • 13
  • 30
  • I am a little surprised you don't need (Gnu extension) `#define AT(x) @ ## x` – abligh Sep 05 '22 at 22:25
  • @abligh You are probably right. As I wrote, it is untested, because I don't have that (unknown) compiler installed. It can be an IAR compiler, as I found a reference in Renesas' manual. – the busybee Sep 06 '22 at 05:54
  • Thanks for your explanation! But I cant use your solution. I shouldn't change source code. Have you any idea how to set extension in Understand? I am using IAR compiler ver. 1.40.6. [I found this information in IAR compiler][1] [1]: https://i.stack.imgur.com/NhHbQ.png – raptor Sep 06 '22 at 12:39
  • If you cannot change the offending source, and you cannot make Understand understand it (pun intended), you will have a hard time. There _are_ possibilities, but all of them mean a lot of work. A better alternative would be to use another analyzer, which accepts this extension. – the busybee Sep 06 '22 at 12:52
0

The @ operator is not standard C; you should find it documented as an extension in the manual for whatever compiler you are using.

The problem here is that static analysis tools need not be aware of such extensions.

Your compiler may offer an alternative method of locating objects that will not trouble the static analysis parser. For example the IAR compiler supports this extension, but has an alternative #pragma location directive:

#pragma location = 0xF0031
__SFR_EXTERN__ __near __no_init volatile union
{
    TByte         ioPU1;
    TBitfieldByte ioPU1_Bits;
} dPU1;

Since the required behaviour when encountering an unrecognised pragma is to ignore it, your analyser should accept this code.

Clifford
  • 88,407
  • 13
  • 85
  • 165