Ada record representation

2019-08-15 07:17发布

问题:

I have a record type as follows:

type Rec_T is record
   a : Bit_12_T;
   b : Bit_4_T;
end record;

where Bit_12_T is mod 2**12 and Bit_4_T is mod 2**4.

To tell the compiler the precise alignment of this record, I use the the for use record statement. However, I want to split the a field bewteen the bytes, so I try to do it as follows:

for Rec_T use record
   a at 0 range 0 .. 7; -- The 1st byte
   a at 1 range 0 .. 3; -- The 2nd byte
   b at 1 range 4 .. 7;
end record;

Clearly, this is not the way to do it as the compiler complains that "component clause is previously given at line ...".

Question: Is it possible to have a component split between the bytes and how to do it? If it is not possible, should I have a_high and a_low and then use some bit operations to merge them together?

回答1:

Think of the location as an array of bits, not as a sequence of bytes. Hence this should work:

for Rec_T use record
   a at 0 range 0 .. 11; -- bits 0..7 of the 1st byte and bits 0..3 of the 2nd byte
   b at 0 range 12 .. 15; -- bits 4..7 of the second byte
end record;
for Rec_T'Size use 16;

For more information see documentation here, specifically the example at then end of the page, that shows a similar situation.



标签: record ada