简体   繁体   中英

Ada record representation

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM