Nios II Classic Software Developer’s Handbook

ID 683282
Date 5/14/2015
Public
Document Table of Contents

6.15.4. Global Pointer Register

The global pointer register enables fast access to global data structures in Nios II programs. The Nios II compiler implements the global pointer, and determines which data structures to access with it. You do not need to do anything unless you want to change the default compiler behavior.

The global pointer register can access a single contiguous region of 64 KB. To avoid overflowing this region, the compiler only uses the global pointer with small global data structures. A data structure is considered “small” if its size is less than or equal to a specified threshold. By default, this threshold is 8 bytes.

The small data structures are allocated to the small global data sections, .sdata, .sdata2, .sbss, and .sbss2. The small global data sections are subsections of the .rwdata and .bss sections.

Figure 13. Small Global Data Sections

If the total size of the small global data structures is more than 64 KB, these data structures overflow the global pointer region. The linker produces an error message saying "Unable to reach <variable name> ... from the global pointer ... because the offset ... is out of the allowed range, -32678 to 32767."

The best solution is to use fewer global variables of size 8 bytes or less. This is the default setting for the -Gn switch, perhaps by combining some into records. Another solution is to place only the frequently used global variables inside the .sdata + .sbss.

If the previous solution fails, a quick and easy solution is to use the -mgpopt=none switch. This removes the 64 KB limit on the size of the .sdata + .sbss sections addressed by the GP register. This is accomplished at the cost of using bigger, slower (typically 3-instruction) code sequences to address such values, in place of the typically single-instruction code sequences otherwise used.

When the size of the global variable is greater than 8 bytes, by default, it gets placed outside of the .sdata + .sbss, and takes longer to access it. You can improve space-time performance by placing only the frequently used global variables inside the .sdata + .sbss leaving space available to add global variables greater than 8 bytes; and by annotating the code to place these global variables inside the .sdata + .sbss.

Annotating the code is show in mwh1416946807456.html#mwh1416946807456__example_cnm_wbq_yy.

For information about manipulating project settings, refer to "HAL BSP Settings".