Ada记录表示 [英] Ada record representation

查看:74
本文介绍了Ada记录表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的记录类型如下:

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

其中 Bit_12_T是mod 2 ** 12 Bit_4_T是mod 2 ** 4

为了告诉编译器该记录的精确对齐,我使用了 for use record 语句。但是,我想在字节之间分割 a 字段,所以我尝试如下操作:

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;

很显然,这不是这样做的方法,因为编译器抱怨以前给出了component子句在行...。

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

问题:是否可以在字节之间拆分一个组件,并且该怎么做?如果不可能,我是否应该使用a_high和a_low,然后使用一些位运算将它们合并在一起?

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.

这篇关于Ada记录表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆