#pragma CODE_SEG: Code Segment Definition

Scope

Next pragma CODE_SEG

Syntax
  #pragma CODE_SEG (<Modif> <Name>|DEFAULT) 
  
Synonym
   CODE_SECTION 
  
Arguments

<Modif>: Some of the following strings may be used:

__DIRECT_SEG (compatibility alias: DIRECT)

__NEAR_CODE

__NEAR_SEG (compatibility alias: NEAR)

__CODE_SEG (compatibility alias: CODE)

__ FAR_CODE

__FAR_SEG (compatibility alias: FAR)

Note: Do not use the compatibility alias in new code. It exists only for backward compatibility. Some compatibility alias names conflict with defines found in certain header files, and using compatibility alias names can cause hard to detect problems. Avoid using compatibility alias names.

The meaning of these segment modifiers are backend-dependent. Refer to HC(S)08 Backend for information on supported modifiers and their definitions.

<Name>: The segment name. You must use the segment name in the link parameter file on the left side of the assignment in the PLACEMENT section. Refer to the Linker manual for details.

Default

DEFAULT

Description

This pragma specifies the allocated function segment. The segment modifiers also specify the function's calling convention. The CODE_SEG pragma sets the current code segment. This segment places all new function definitions. Also, all function declarations, when they occur, get the current code segment.

The CODE_SEG pragma affects function declarations as well as definitions. Ensure that all function declarations and their definitions are in the same segment.

The synonym CODE_SECTION means exactly the same as CODE_SEG. See the following listing for some CODE_SEG examples.

Listing: CODE_SEG examples
/* in a header file */
#pragma CODE_SEG __FAR_SEG MY_CODE1

extern void f(void);

#pragma CODE_SEG MY_CODE2

extern void h(void);

#pragma CODE_SEG DEFAULT

/* in the corresponding C file: */

#pragma CODE_SEG __FAR_SEG MY_CODE1

void f(void){ /* f has FAR calling convention */

  h(); /* calls h with default calling convention */

}

#pragma CODE_SEG MY_CODE2

void h(void){ /* f has default calling convention */

  f(); /* calls f with the FAR calling convention */

}

#pragma CODE_SEG DEFAULT
Note: Not all compiler backends support a FAR calling convention.
Note: The calling convention can also be specified with a supported keyword. The default calling convention is chosen with the memory model.

The following listing shows some errors when using pragmas.

Listing: Improper Pragma Usage
#pragma DATA_SEG DATA1
#pragma CODE_SEG DATA1

/* error: segment name has different types! */

#pragma CODE_SEG DATA1

#pragma CODE_SEG __FAR_SEG DATA1

/* error: segment name has modifiers! */

#pragma CODE_SEG DATA1

void g(void);

#pragma CODE_SEG DEFAULT

void g(void) {}

/* error: g is declared in two different segments */

#pragma CODE_SEG __FAR_SEG DEFAULT

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

HC(S)08 Backend

Segmentation

Linker Manual

#pragma CONST_SEG: Constant Data Segment Definition

#pragma DATA_SEG: Data Segment Definition

#pragma STRING_SEG: String Segment Definition

-Cc: Allocate Const Objects into ROM compiler option