Meta language to code generate packed structs for ANSI-C and C# Structs

青春壹個敷衍的年華 提交于 2019-12-09 23:14:38

问题


I'm trying to find a "meta language" that can be used to define a structure and get/set code for members. The catch is that the structure already exists in code, and this "meta language" would serve as bit-for-bit replacement of the original hand-coded structure to allow the headers describing the structures to be generated. The point is that the structures are used as part of a protocol between a C# application and an embedded device (not linux based, think smaller and more constrained like a PIC or CM0.) The meta language would act as

  • Documentation for the structure members
  • Generate C# structs and implementation for get/set operations
  • Generate packed ANSI-C structs and get/set functions

The meta language would need to support

  • enumeration definitions (of a specified size - ie uint16_t, uint8_t, or smaller as for multi-bit enumerations)
  • bit-arrays (of specified size - ie 48-bit array is packed into 6 bytes,)
  • bit-structure/enumeration arrays (of specified size - ie a 2-bit structure of 48 indexes is 12 bytes,)
  • specification for endianness and bit-order,
  • generate binary structures that can be read directly by either the generated ANSI-C code or the C-sharp code for the purpose of sending over a network.

It would also be nice to have some limited validation of the data when received.

So far I've looked at

  • BSON
  • Etch
  • Hessian Avro
  • ICE
  • MessagePack
  • Protocol Buffers
  • Thrift

All of these are great for documentation and when building a new protocol, but trying to maintain compatibility with an existing protocol and these fall short due to the type encoding inherent in the data marshaling.

I've also looked at ASN.1 for ECN encoding, but that seems to be too unreadable causing issue with Documentation.

I've looked at Generating C# structure from C structure but there wasn't a good option there.

Any recommendations?


回答1:


What you want is a Program Transformation System.

These are tools that can read arbitrary computer language instances, and then transform those into other valid language instances, sometimes in the same language, sometimes into a different language. They are general in that you can provide them the description of the languages you want to manipulate, and they can then operate on those languages.

Good tools in this space let you write the code transformations in terms of the ("surface") syntax of the languages of interest, essentially in the form of "if you see this, replace it by that".

For OP's scenario, the essential transformations are "if you see this slot in a structure replace it by corresponding getters and setters, and a replacement struct member for the target language.

In your case, you need to choose between 3 scenarios:

  • Define an abstract language for specifying data structures, and build program transforms that map from the specification language to both C# and C.
  • Decide that the C data declarations are the reference, and generate corresponding C# code.
  • Decide the C# data declarations is the reference, and generate corresponding C code.

Then you'll have to sit down, define the languages to the tool (if they aren't already defined), and construct the transforms.

(Full disclosure: I build such a tool. See my bio).



来源:https://stackoverflow.com/questions/36772952/meta-language-to-code-generate-packed-structs-for-ansi-c-and-c-sharp-structs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!