#pragma DATA_SEG: Data Segment Definition

Scope

Next pragma DATA_SEG

Syntax
#pragma DATA_SEG (<Modif> <Name>|DEFAULT) 
Synonym
 DATA_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)

Note: Do not use a compatibility alias in new code. It only exists for backwards compatibility.

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. Please refer to the linker manual for details.

Default
DEFAULT 
Description

This pragma allocates variables into the current data segment. This segment is then moved to specific addresses in the link parameter file.

The DATA_SEG pragma sets the current data segment. This segment is used to place all variable declarations. Set the default segment with:

#pragma DATA_SEG DEFAULT

Constants are also allocated in the current data segment in the HIWARE object-file format when the option -cc is not specified and no #pragma CONST_SEG occurred in the source. When using the -Cc: Allocate Const Objects into ROM compiler option and the ELF object-file format, constants are not allocated in the data segment.

The DATA_SEG pragma also affects data declarations, as well as definitions. Ensure that all variable declarations and definitions are in the same segment.

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

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

The DATA_SECTION synonym means exactly the same as DATA_SEG.

Example

The following listing is an example using the DATA_SEG pragma.

Listing: Using the DATA_SEG Pragma
/* in a header file */
#pragma DATA_SEG __SHORT_SEG SHORT_MEMORY

extern int i_short;

#pragma DATA_SEG CUSTOM_MEMORY

extern int j_custom;

#pragma DATA_SEG DEFAULT

/* in the corresponding C file : */

#pragma DATA_SEG __SHORT_SEG SHORT_MEMORY

int i_short;

#pragma DATA_SEG CUSTOM_MEMORY

int j_custom;

#pragma DATA_SEG DEFAULT

void main(void) {

  i = 1; /* may use short access */

  j = 5;

}

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

Listing: Improper Use of the DATA_SEG Pragma
#pragma DATA_SEG DATA1
#pragma CONST_SEG DATA1 /* error: segment name has different types! */

#pragma DATA_SEG DATA1

#pragma DATA_SEG __SHORT_SEG DATA1

/* error: segment name has modifiers! */

#pragma DATA_SEG DATA1

extern int i;

#pragma DATA_SEG DEFAULT

int i; /* error: i is declared in different segments */

#pragma DATA_SEG __SHORT_SEG DEFAULT

/* error: modifiers for DEFAULT segment are not allowed */
See also

HC(S)08 Backend

Segmentation

Linker section of the Build Tool Utilities manual

#pragma CODE_SEG: Code Segment Definition

#pragma CONST_SEG: Constant 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