9

I have come across following definition in an embedded C source file:

const preamble_t OAD_Preamble @ ".preamble" =
{
  HAL_OAD_RC_MAX,       // Default program length of max if not using post-processing tool.
  OAD_MANUFACTURER_ID,  // Manufacturer ID
  OAD_TYPE_ID,          // Image Type
  0x00000001            // Image Version
};

I have no idea about the @ part, can you please help me with this?

Edit: This is in IAR compiler, used with TI SoCs.

doubleE
  • 805
  • 8
  • 26

1 Answers1

9

This is the way you can specify a memory address or a section in which you would like to place your variable.

  • ".preamble" is the name of a section,
  • OAD_Preamble is the variable to be placed there.

You can also specify physical address after the at @ sign:

const unsigned char port_bit @ 0x1800 = BIT0;

More information is in this document.

Note: this is a non-portable compiler extension, not a part of the standard C syntax.

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
  • So this is an extension recognized by certain compilers, yes? As far as I know or can determine, it is not specified by the standard. – John Bollinger Sep 17 '15 at 18:06
  • @JohnBollinger Yes, this is a compiler extension. I saw this syntax in other embedded compilers, but I am pretty sure it's not standard. – Sergey Kalinichenko Sep 17 '15 at 18:09
  • 1
    Definitely a compiler extension. I use the same processor with (I suspect) a different compiler, and we use the #pragma location = ADDRESS syntax on the line before. I happen to find that clearer, though less compact, and I think it's more likely to be portable. – Erik Johnson Sep 17 '15 at 18:24
  • I suggest mentioning in your answer that this is a non-portable extension, not part of the C language. – Keith Thompson Sep 17 '15 at 18:24
  • @KeithThompson Great suggestion, thank you very much! – Sergey Kalinichenko Sep 17 '15 at 18:31
  • I should have mentioned this before: it would also be helpful to mention which compiler provides it. – Keith Thompson Sep 17 '15 at 18:33
  • @KeithThompson This syntax is not unique to one compiler. I've seen this syntax used in several compilers, which come from different manufacturers, and generate code for different platforms. Some of these compilers date as far as 20 years back, so I wouldn't even recall their manufacturers. – Sergey Kalinichenko Sep 17 '15 at 18:42
  • Thanks a lot for help. I am using IAR for compiling, which is the only one that works with TI CC253X series. – doubleE Sep 17 '15 at 18:54
  • @dandikain : The CC253X has an 8051 core; as such compilers are widely available from many vendors. IAR does however have CC253X specific support. – Clifford Sep 17 '15 at 20:14
  • 2
    @ErikJohnson : While `#pragma` is a standard compiler directive, the pragmas themselves are compiler specific. They are portable only in the sense that a directives not recognised by the compiler are required to be ignored. So the code would compile on another compiler, but the data may not be located as required. Directives that communicate information to the linker are always tool-chain specific. – Clifford Sep 17 '15 at 20:17
  • 1
    The non-standard `@` symbol is a common non-standard extension in embedded systems, always with the meaning "allocate memory at specific address". There is a lot of embedded C compilers using it. The syntax however, varies from compiler to compiler, so while many compilers support some sort of @ symbol, their non-standard extensions are not necessarily compatible with each other. – Lundin Sep 18 '15 at 14:20
  • 1
    @Clifford You are absolutely correct that the individual pragmas are compiler specific, but I've found that particular structure on multiple compilers, so I consider it slightly more likely to be portable than the @ structure. Absolutely, if you're writing code with the intent to be truly portable, direct assignment to memory locations is a really bad idea. On the other hand, when writing for bare-metal embedded (where constructs like this are most common), portability is a lower priority - you're writing to the hardware you have, and care less if it runs elsewhere. – Erik Johnson Sep 18 '15 at 21:00
  • 1
    @ErikJohnson : Note that in this case the @ is not being used to locate at an explicit location, but rather as a directive to the linker to locate the object in a specific linker section. Often compilers support pragmas with similar names but differing syntax or semantics, and a compiler need not even issue a diagnostic for an unrecognised pragma - in many cases it is better to have the code break completely when ported so you know where modifications are required. GCC's `__attribute__` syntax is preferable in most cases (and also supported on several other compilers). – Clifford Sep 18 '15 at 21:30