简体   繁体   English

按位结构定义语言生成C ++代码

[英]Bitwise structure definition language generating c++ code

Before any question is asked: I am dealing with actual hardware. 在问任何问题之前:我正在处理实际的硬件。

I am searching for a meta-language that would allow me to specify data structure contents where fields have different bit length (this includes fields like 1, 3 or 24 or 48 bits long), with respect to endianess, and would generate C++ code accessing the data. 我正在寻找一种元语言,该语言将允许我指定数据结构的内容,其中字段的位长不同(这包括字段长度为1、3或24或48位),涉及字节长度,并且会生成C ++代码访问数据。

The question was put on hold due to being too vague, so I'll try to make it as clear as possible: 由于过于模糊,该问题被搁置了,因此我将尝试使其尽可能清楚:

I am searching for a language that: 我正在寻找一种语言:

  • accepts simple structure description and generate useful C++ code, 接受简单的结构描述并生成有用的C ++代码,
  • would allow to precisely specify integers ranging from 1 bit to multiple (up to 8) bytes long, along with data (typically string), 将允许精确地指定从1位到多个(最多8个)字节长的整数以及数据(通常是字符串),
  • would isolate me from need to convert endianess, 会使我摆脱转变忍耐的需要,
  • produces exact, predictable output that does not come with overhead (like in protocol buffers) 产生不带开销的精确,可预测的输出(例如协议缓冲区中的输出)

ASN.1 sounds almost good for the purpose, but it adds its own overhead (meaning, I cannot produce a simple structure that has 2 bytes split into 4 nibbles) - what i'm looking for is a language that would offer exact representation of the structure. ASN.1听起来几乎可以达到目的,但是它增加了自己的开销(这意味着,我无法生成将2个字节分成4个半字节的简单结构)-我正在寻找的是一种语言,可以提供以下内容的精确表示:结构。

For example, I would want to abstract this: 例如,我想对此进行抽象:

struct Command {
  struct Record {
    int8_t track;
    int8_t point;
    int8_t index;
    int16_t start_position;  // big endian, misaligned
    int32_t length;          // big endian, misaligned;
  } __attribute__((packed)); // structure length = 11 bytes.

  int8_t current       : 1;
  int8_t command       : 7;
  int8_t reserved;
  int16_t side         : 3;  // entire int16_t needs to be
  int16_t layer        : 3;  // converted from big endian, because
  int16_t laser_mark   : 3;  // this field spans across bytes.
  int16_t laser_power  : 3;
  int16_t reserved_pad : 2;
  int16_t laser_tag    : 2;
  int32_t mode_number  : 8;  // again, entire 32 bit field needs to be converted
  int32_t record_count : 24; // from big endian to read this count properly.

  Record records[];
} __attribute__((packed));

the above needs to be packed exactly to the structure carrying 8 + record_count * 11 bytes, all formed accurately, no additional data, no additional bits or bytes set. 上面的内容需要精确地打包为8 + record_count * 11个字节的结构,所有结构都准确无误,没有其他数据,没有设置其他位或字节。

The above is just an example . 以上只是一个例子 it's made simple so that I don't clog the site with actual structures that have oftentimes hundreds of fields. 它很简单,因此我不会用经常包含数百个字段的实际结构来阻塞网站。 It has been simplified , but shows many of the features that I am looking forward to see (two remaining features are 48 or 64-bit integers and plain data (bytes[])) 它已经简化 ,但是显示了我期待看到的许多功能(剩下的两个功能是48或64位整数和纯数据(bytes []))

If this question is still too vague, please explain what it is that I should add in the comments. 如果这个问题仍然太模糊,请解释一下我应该在评论中添加什么。 thanks! 谢谢!

A simple table that tracks individual field sizes and is used to spin out offsets of each element into your structure sounds like the easiest solution. 一个简单的表可以跟踪各个字段的大小,并用于将每个元素的偏移量分配到您的结构中,这听起来是最简单的解决方案。 This won't scale to deeply nested structures, but could be tuned to support handling of the unassigned bit cases you identify. 这不会扩展到深层嵌套的结构,但可以进行调整以支持处理您标识的未分配位情况。

Then, you can use this to generate constants or even named property accessors to extract and update the individual fields. 然后,您可以使用它来生成常量,甚至可以使用命名属性访问器来提取和更新各个字段。 Given the size of the individual elements, macros are likely to make life even harder, but any mainstream compiler should inline the code. 考虑到单个元素的大小,宏可能会使工作变得更加艰难,但是任何主流编译器都应内联代码。 You mileage could vary with a template-based implementation. 您的里程可能因基于模板的实施而异。

If would help if you could use a common representation for both sides of the application (host and device) to further reduce the likelihood of transcription errors. 如果可以在应用程序的两侧(主机和设备)使用通用表示形式来进一步降低转录错误的可能性,则可以。

The PLC world has a number of different mechanisms for layout, but these are all very hardwired into their eco-systems and so would not really help. PLC世界中有许多不同的布局机制,但是这些机制都被硬连线到其生态系统中,因此并没有真正的帮助。

Alternately, if you have the tooling available, you could consider something like ASN.1 structures for the representation. 或者,如果您拥有可用的工具,则可以考虑使用类似于ASN.1的表示形式。 In the extreme, you could even use an open source generator to come up with an unencoded generator directly from the MIB. 在极端情况下,您甚至可以使用开源生成器直接从MIB中提出未编码的生成器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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