cancel
Showing results for 
Search instead for 
Did you mean: 

Sequential Variables declaration

nickabrham
Associate II
Posted on September 09, 2014 at 09:26

Hi,

The MAP file shows the variables declared in some hap hazard way. Is there any option in the compiler to make the variable appear sequentially in the MAP file as these variables are declared in the main file. I am working on STM32F103ZE using keil MDK-ARM.

Best regards,

Nick.
4 REPLIES 4
Danish1
Lead II
Posted on September 09, 2014 at 10:41

I don't know about MDK-ARM, but there can be good reasons not to store them in the order they are declared.

The case I have in mind are where the variables are of different types. Some types (e.g. int and long) are most efficient on a 4-byte boundary. If you declare a one-byte ''char'' type between two longs, then the compiler would have to add (i.e. waste) 3 bytes of padding to keep everything in the order you specified.

One thought: What about if you put all of these variables into a big struct - I think the compiler isn't supposed to reorder them then.

Hope this helps,

Danish

jpeacock
Associate III
Posted on September 09, 2014 at 14:09

In general it's not a desirable feature to have variables assigned in the order of declaration. It's the compiler's job to optimize storage by combining like types to maintain alignment (all longs, all shorts, all chars). This makes a difference in stack size when allocating from the stack during a procedure call.

Depending on what toolchain you use you can assign globals to specific sections to group them together. I do this with GCC to create a block of field modified configuration parameters that are saved in flash but copied to RAM on a reset. Here's a GCC example:

#if __SNMP_H_ 
// SNMP identification strings, only used if TCP/Ip and SNMP protocol enabled 
unsigned char __attribute__ ((section(''.infomem''))) Community[6] = ''public''; 
unsigned char __attribute__ ((section(''.infomem''))) TrapComm[6] = ''traps''; 
unsigned char __attribute__ ((section(''.infomem''))) Contact[32] = ''Jack Peacock''; 
unsigned char __attribute__ ((section(''.infomem''))) Domain[64] = ''engineering.local''; 
unsigned char __attribute__ ((section(''.infomem''))) Node[64] = ''DuoMini''; 
unsigned char __attribute__ ((section(''.infomem''))) Location[32] = ''Engineering''; 
#endif

The attribute and section keywords group all the globals into one memory section which can be placed by the linker:

/* Configuration parameter defaults 
INFOMEM is initialized from FLASH when no saved parameters are available. The 
FLASH image is copied to INFOMEM in SRAM1 inside the application. */ 
.infomem : AT (_sfdata) 
{ 
. = ALIGN(4); /* word align, follows runtime init */ 
_sinfo = .; /* start working configuration parameters */ 
*(.infomem*) /* default field parameters */ 
. = ALIGN(4); /* word align for fast init */ 
_einfo = .; /* end working config parameters */ 
} > SRAM1 

The section is mapped to a specific area of SRAM with some linker defined globals to mark the address so the program knows where the section is located. Jack Peacock
Andrew Neil
Evangelist III
Posted on September 09, 2014 at 21:23

Is there any option in the compiler to make the variable appear sequentially in the MAP file as these variables are declared in the main file.

Why would you want to do that? What do you hope to achieve by it?

http://www.catb.org/esr/faqs/smart-questions.html#goal

Posted on September 09, 2014 at 22:02

Collecting them in a structure (object) or section/segment seems to make the most sense if you want them grouped together for whatever purposes. Presumably a debug view of memory.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..