Everything
A.1.1 Allocating to sections accessible with short instructions

The CC-RL provides an attribute among far, near, or saddr to variables or constants, and allocates them to areas with the same names as their attributes; far area, near area, or saddr area.

Since the size of instructions to access variables or constants in these area becomes smaller in this order, the code size can be reduced by specifying the near attribute (or saddr attribute) for variables or constants and then allocating them to the near area (or saddr area).

As the size of the area also becomes smaller in this order, it is necessary to prioritize which area to allocate each variable for a large total size of variables or constants.

 

The specification method of each attribute, each area, and its allocation method are described in detail in the following.

(1)

Methods for specifying the attribute for variables or constants

Options or keywords are used to specify the attribute for variables or constants.

The following four specification methods are available and the priority gets higher in this order.

For details, see each option or keyword and "2.6.6 Relationship with near and far".

(a)

-cpu option

The default attribute for variables or constants is always the near attribute.

(b)

-memory_model option

The default attribute for variables or constants is always the near attribute.

(c)

-far_rom option

The ROM data has far attribute by default.

(d)

__near/__far/_saddr keyword

A variable has the attribute specified by each keyword.

Example 1.

When the -far_rom option is not specified

long                x;      //near attribute
long __near         y;      //near attribute
long __far          z;      //far attribute
const int           c;      //near attribute
const int __near    d;      //near attribute
const int __far     e;      //far attribute
char __saddr        s;      //saddr attribute

Example 2.

When the -far_rom option is specified

long                x;      //near attribute
long __near         y;      //near attribute
long __far          z;      //far attribute
const int           c;      //far attribute
const int __near    d;      //near attribute
const int __far     e;      //far attribute
char __saddr        s;      //saddr attribute

(2)

Priority for allocating variables or constants to each area

In general, if variables with higher static reference counts are allocated to an area accessible with a small size of instructions, the code size of the entire program gets smaller.

However, an area accessible with a small size of instructions needs to be used efficiently because the size of the area is also small.

Therefore, it is necessary to maximize the reduction efficiency for one byte of the area, namely, it is preferable to allocate variables whose value obtained by dividing the static reference count of the variable with the variable size is larger to an area accessible with a smaller size of instructions.

Accordingly, the priority is conceived as follows

Priority = static reference count of the variable / size of the variable

 

Here, the static reference count of the variable indicates the reference count in the object code, and this will differ from the reference count in the C source program.

In the RL78, instructions for variable reference have only a 1-byte or 2-byte access width. Thus, for example, two instructions are needed to reference a 4-byte variable.

Therefore, one reference to a 4-byte variable in a C source program corresponds to two references in the object code.

The reference count in the object code can be shown by specifying -show=reference option to linker.

(3)

Area

The areas placing variables or constants is divided into three, based on the address range.

The far area is the entire area including the saddr area or near area.

(a)

saddr area

The area from addresses 0xFFE20 to 0xFFF1F is called the saddr area.

The saddr-attribute variables located in this address range are accessed by instructions with the smallest size.

The saddr area includes some of the special function registers (SFRs) and all general-purpose registers.

Variables cannot be allocated to the SFRs or general-purpose registers.

(b)

near area

The area from addresses 0xF0000 to 0xFFE1F is called the near area.

This area includes internal RAM, mirror region, and data flash memory.

The near-attribute variables located in this address range are accessed by instructions with the second smallest size after the saddr area.

The near area also includes the extended special function registers (2nd SFRs).

Variables cannot be allocated to the 2nd SFRs.

(c)

far area

The entire area including the saddr area or near area is called the far area.

This area includes RAM other than internal RAM and the constant area in code flash memory other than the mirror region.

The far-attribute variables located in this address range are accessed by instructions with the largest size.

(4)

Method for allocating variables or constants to each area

Variables or constants are allocated to each area by using link options to specify the allocation address of the section including each variable or constant.

Example 1.

The default sections for the allocation of saddr variables are .sbss and .sdata.
.sbss is the section for the allocation of saddr-attribute variables without initial values and .sdata is the section for the allocation of saddr-attribute variables with initial values.
When you create a ROM image, allocate the .sdata section to a desired address and give an appropriate name, .sdataR in the example below, to the destination section for transfer in RAM in the case of the transfer of data from the .sdata section to RAM.

-rom=.sdata=.sdataR
-start=.sdata/5000
-start=.sbss,.sdataR/ffe20

Example 2.

The default sections for allocation to the near area are .bss, .data and .const.
.bss is the section for the allocation of near-attribute variables without initial values and .data is the section for the allocation of near-attribute variables with initial values.
When you create a ROM image, allocate the .data section to a desired address and give an appropriate name, .dataR in the example below, for the destination section for transfer in RAM in the case of the transfer of data from the .data section to RAM.

-rom=.data=.dataR
-start=.const/2000
-start=.data/4000
-start=.bss,.dataR/fe000

Example 3.

The default sections allocated to the far area are .bssf, .dataf and .constf.
.bssf or .dataf is normally not used when only internal RAM is to be used as RAM.
.constf is a section for allocating constants with far attribute.
.constf is allocated to code flash memory.

-start=.constf/6000