16.3.5 Bit Fields

H2INC translates C bit fields into MASM records. H2INC looks at a structure definition; if it consists only of bit fields of the same type and if the total size of the bit fields does not exceed the type of the bit fields, then H2INC outputs a RECORD definition with the name of the structure. All bit-field names are modified to include the structure name for uniqueness, since record fields have global scope in MASM.

For example,

struct s

{

int i:4;

int j:4;

int k:4;

}

becomes:

s RECORD @tag_0:4,

k@s:4,

j@s:4,

i@s:4

The @tag variable pads out the record to the type size of the bit fields so alignment of the structures will be correct.

If the bit fields are too large, are not of the same type, or are mixed with fields that are not bit fields, H2INC generates a RECORD definition inside the structure and then uses the definition.

For example,

struct t

{

int i;

unsigned char a:4;

int j:9;

int k:9;

long l;

} m;

becomes:

t STRUCT 2t

i SWORD ?

rec@t_0 RECORD @tag_1:4,

a@t:4

@bit_0 rec@t_0 <>

rec@t_1 RECORD @tag_2:7,

j@t:9

@bit_1 rec@t_1 <>

rec@t_2 RECORD @tag_3:7,

k@t:9

@bit_2 rec@t_2 <>

l SDWORD ?

t ENDS

EXTERNDEF C m:t

Notice that j and k are not packed because their total size exceeds the 16 bits of an integer in C.

Since the @bit field names are local to the structure, these begin with 0 for each structure type; the @rec variables have global scope and so their number always increases.

The C bit-field declaration

struct SCREENMODE

{

unsigned int disp_mode : 4;

unsigned int fg_color : 3;

unsigned int bg_color : 3;

};

is converted into the following MASM record:

SCREENMODE RECORD disp_mode@SCREENMODE:4,

fg_color@SCREENMODE:3,

bg_color@SCREENMODE:3

See Section 5.3 for information about MASM records.