#pragma CONST_SEG: Constant Data Segment Definition

Scope

Next pragma CONST_SEG

Syntax
#pragma CONST_SEG (<Modif> <Name>|DEFAULT) 
Synonym
CONST_SECTION
Arguments
Modif 
Some of the following strings may be used:

__SHORT_SEG (compatibility alias: SHORT)

__DIRECT_SEG (compatibility alias: DIRECT)

__NEAR_SEG (compatibility alias: NEAR)

__CODE_SEG (compatibility alias: CODE)

__FAR_SEG (compatibility alias: FAR)

__LINEAR_SEG

Note: Do not use a compatibility alias in new code. Aliases exist for backwards compatibility purposes only.

The segment modifiers are backend-dependent. Refer to HC(S)08 Backend to find the supported modifiers and their meanings. The __SHORT_SEG modifier specifies a segment accessed with 8-bit addresses.

<Name>: The name of the segment. This name must be used in the link parameter file on the left side of the assignment in the PLACEMENT part. Refer to the linker manual for details.

Default
DEFAULT 
Description

This pragma allocates constant variables into the current data segment. The Compiler then allocates the segment to specific addresses in the link parameter file. The pragma CONST_SEG sets the current const segment. This segment places all constant variable declarations. Set the segment to default with:

#pragma CONST_SEG DEFAULT

#pragma DATA_SEG defines the current data segment in the HIWARE object-file format, unless the -Cc option is specified, and until the first #pragma CONST_SEG occurs in the source (see #pragma DATA_SEG: Data Segment Definition and -Cc: Allocate Const Objects into ROM). The-Cc option always allocates constants in constant segments in the ELF object-file format, and after the first #pragma CONST_SEG.

The CONST_SEG pragma also affects constant variable declarations and definitions. Ensure that all constant variable declarations and definitions are in the same const segment.

Some compiler optimizations assume that objects having the same segment are placed together. Backends supporting linear data access, for example, may set the page register only once for two accesses to two different variables in the same segment. This is also the case for the DEFAULT segment. When using a paged access to variables, place one segment on one page in the link parameter file.

When #pragma INTO_ROM is active, the current const segment is not used (see #pragma INTO_ROM: Put Next Variable Definition into ROM).

The synonym CONST_SECTION means exactly the same as CONST_SEG.

Examples

The following listing shows examples of the CONST_SEG pragma.

Listing: Examples of the CONST_SEG Pragma
/* Use the pragmas in a header file */
#pragma CONST_SEG __SHORT_SEG SHORT_CONST_MEMORY
extern const int i_short;
#pragma CONST_SEG CUSTOM_CONST_MEMORY
extern const int j_custom;
#pragma CONST_SEG DEFAULT

/* Some C file, which includes the above header file code */
void main(void) {
  int k= i; /* may use short access */
  k= j;
}

/* in the C file defining the constants : */
#pragma CONST_SEG __SHORT_SEG SHORT_CONST_MEMORY
extern const int i_short=7
#pragma CONST_SEG CUSTOM_CONST_MEMORY
extern const int j_custom=8;
#pragma CONST_SEG DEFAULT

The following listing shows code that uses the CONST_SEG pragma improperly.

Listing: Improper use of the CONST_SEG Pragma
#pragma DATA_SEG CONST1
#pragma CONST_SEG CONST1 /* error: segment name has different types!*/
#pragma CONST_SEG C2
#pragma CONST_SEG __SHORT_SEG C2 // error: segment name has modifiers!
#pragma CONST_SEG CONST1
extern int i;

#pragma CONST_SEG DEFAULT 
int i; /* error: i is declared in two different segments */
#pragma CONST_SEG __SHORT_SEG DEFAULT // error: modifiers for DEFAULT segment are not allowed
See also

HC(S)08 Backend

Segmentation

Linker Manual

#pragma CODE_SEG: Code Segment Definition

#pragma DATA_SEG: Data Segment Definition

#pragma STRING_SEG: String Segment Definition

-Cc: Allocate Const Objects into ROM compiler option

#pragma INTO_ROM: Put Next Variable Definition into ROM