-bfield_gap_limit

Controls the maximum number of gap bits allowed.

Syntax
  -bfield_gap_limit <number>  

where,

<number> is the maximum number of bits in the gap

Default

0

Remarks

When performing bitfield allocation, the compiler tries to avoid crossing a byte boundary whenever possible. In doing so, it may insert some padding(gap) bits.

This command-line is equivalent to the pragma #pragma bfield_gap_limit <number>

The following example demonstrates how this option works on bitfield allocation. Suppose the specified maximum gap is 2, that is,

  -bfield_gap_limit 2  
Listing: Example -bfield_gap_limit
typedef struct
{

        unsigned char a: 7;

        unsigned char b: 5;

        unsigned char c: 4;

} SomeStruct;

The compiler allocates struct B with 3 bytes. First, the compiler allocates the 7 bits of 'a'. Then the compiler tries to allocate the 5 bits of 'b', but this would cross a byte boundary. Because the gap of 1 bit is smaller than the specified gap of 2 bits, 'b' should be allocated in the next byte. However, if the compiler uses byte as the allocation unit and 'b' is allocated in the next byte, there would be 3 bits left after its allocation.

Since the maximum gap has been set to 2, and the required gap (if 'c' is to be allocated in the next byte) would be 3, the compiler will in fact use a 16-bit word as the allocation unit. Both 'b' and 'c' will be allocated within this word.

Assuming we initialize an instance of SomeStruct as below:

  SomeStruct s = {2, 7, 5},  

we get the following memory layouts for s:

  -bfield_gap_limit 0 : 53 82  
  -bfield_gap_limit 2 : 02 00 A7  
  -bfield_gap_limit 3 : 02 07 05  
Note: The option can also be used to force word as the allocation unit for bitfields. To achieve this, either enable the option with argument 0xFF (or any other value that would represent a negative number on 8-bit precision) or use the equivalent pragma with a negative argument. This behavior is supported for the sake of backward compatibility with older S12 versions.

The following example shows how a negative gap limit works.

Listing: Example - Negative Gap Limit
typedef struct {
        unsigned char b0: 1;

        unsigned char b1: 1;

        unsigned char b2: 1;

        unsigned char b3: 1;

        unsigned char b4: 1;

        unsigned char b5: 1;

        unsigned char b6: 1;

        unsigned char b7: 1;

        unsigned char b8: 1;

        unsigned char b9: 1;

        unsigned char b10: 1;

        unsigned char b11: 1;

        unsigned char b12: 1;

        unsigned char b13: 1;

        unsigned char b14: 1;

        unsigned char b15: 1;

} SomeStruct;

Assuming we initialize an instance of SomeOtherStruct as below:

  SomeStruct s = {1,0,1,0,0,0,0,0,1,1,1,0,0,0,0,0};  

we get the following memory layouts for s:

  -bfield_gap_limit 0  (default)  : 05 07  
  -bfield_gap_limit 0xFF          : 07 05  

For more information, refer to the pragma bfield_gap_limit.