需要源到源编译器框架
我曾经使用 OpenC++ (http://opencxx.sourceforge.net/opencxx/html/ overview.html) 执行代码生成,如:
来源:
class MyKeyword A {
public:
void myMethod(inarg double x, inarg const std::vector<int>& y, outarg double& z);
};
生成:
class A {
public:
void myMethod(const string& x, double& y);
// generated method below:
void _myMehtod(const string& serializedInput, string& serializedOutput) {
double x;
std::vector<int> y;
// deserialized x and y from serializedInput
double z;
myMethod(x, y, z);
}
};
这种代码生成直接匹配 OpenC++ 教程中的用例 (http://www.csg.is.titech.ac.jp/~chiba/opencxx/tutorial.pdf)通过编写一个元级程序来处理“MyKeyword”、“inarg”和“outarg”并执行代码生成。然而,OpenC++ 现在有点过时且不活跃,我的代码生成器只能在 g++ 3.2 上运行,并且在解析更高版本的 g++ 头文件时触发错误。
我看过 VivaCore,但它没有提供编译元级程序的基础结构。我也在研究 LLVM,但我找不到指导我解决源到源编译用法的文档。我也知道ROSE编译器框架,但我不确定它是否适合我的使用,以及其专有的C++前端二进制文件是否可以在商业产品中使用,以及是否有Windows版本可用。
非常感谢任何对特定教程/论文/文档的评论和指示。
I used to use OpenC++ (http://opencxx.sourceforge.net/opencxx/html/overview.html) to perform code generation like:
Source:
class MyKeyword A {
public:
void myMethod(inarg double x, inarg const std::vector<int>& y, outarg double& z);
};
Generated:
class A {
public:
void myMethod(const string& x, double& y);
// generated method below:
void _myMehtod(const string& serializedInput, string& serializedOutput) {
double x;
std::vector<int> y;
// deserialized x and y from serializedInput
double z;
myMethod(x, y, z);
}
};
This kind of code generation directly matches the use case in the tutorial of OpenC++ (http://www.csg.is.titech.ac.jp/~chiba/opencxx/tutorial.pdf) by writing a meta-level program for handling "MyKeyword", "inarg" and "outarg" and performing the code generation. However, OpenC++ is sort of out-of-date and inactive now, and my code generator can only work on g++ 3.2 and it triggers error on parsing header files of g++ of higher version.
I have looked at VivaCore, but it does not provide the infra-structure for compiling meta-level program. I'm also looking at LLVM, but I cannot find documentation that tutor me on working out my source-to-source compilation usage. I'm also aware of the ROSE compiler framework, but I'm not sure whether it suits my usage, and whether its proprietary C++ front-end binary can be used in a commercial product, and whether a Windows version is available.
Any comments and pointers to specific tutorial/paper/documentation are much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道有任何现成的解决方案,但您可以花费相对较少的努力来构建自己的解决方案。一种可能的选择是 Elsa C++ 解析器,它有点过时,但易于使用且可扩展。另一种选择是篡改 Clang++ 生成的 XML AST。我在不同的场景中使用了这两种方法。
I do not know of any ready-to-use solution, but you could build your own with a relatively little effort. One possible option is Elsa C++ parser, a bit out of date, but easy to use and quite extendible. Another option is to tamper with XML ASTs produced by Clang++. I used both approaches in different scenarios.
您了解模板元编程的实践吗?如果您以前没有使用过它,它是 C++ 预处理器的应用程序,用于创建奇怪的元程序,感觉更像 LISP 而不是 C++。这个想法与上面相同——有一个预编译步骤,根据某些输入生成重复的代码。然而,它都是在编译时执行的(而 OpenC++ 看起来在运行时做了几件事)。
考虑到无论如何你都愿意学习一门新语言,你愿意用它作为替代“语言”吗?
Boost 提供了一个库,它使用此技术来提供简单的序列化,如上面所示。 摘自手册中的教程:
Are you aware of the practice of template meta-programming? If you haven't used it before, it's the application of the C++ preprocessor to create oddish meta-programs that feel more like LISP than C++. The idea is the same as above -- having a pre-compile step that generates repeated code based on certain inputs. However, it is all executed at compile time (whereas it looks like OpenC++ does several things at run time).
Considering it looks like you're willing to learn a new one regardless, would you be willing to use that as a replacement "language"?
Boost provides a library which uses this technique to provide easy serialization, like what you showed above. From the tutorial in its manual:
您可以考虑我们的 DMS 软件再工程工具包。 DMS 是将任意语言的源文本解析为编译器数据结构的通用基础
(AST、符号表、控制流图、数据流图,具体取决于您的了解程度)。
DMS 是一个通用源到源程序转换系统。您可以应用源到源模式定向转换,或编写程序转换(很像 OpenC++),
然后重新生成与转换后的程序相对应的可编译源文本。
DMS 通过显式语言定义进行参数化,并处理 C、C#、COBOL、Java、Python、javascript、Fortran。
它有一个完整的 C++ 前端,可以处理许多真实的 C++ 方言(ANSI 、GNU、MS),具有全名和类型解析。具有 C++ 前端的 DMS 可以在多个编译单元内和跨多个编译单元执行由“元程序”控制的转换。它被愤怒地用来对 C++ 软件系统进行彻底的重组,包括大规模重新架构任务航空电子软件(参见网站上的论文),最终用于无人机。
DMS 在 Windows 上运行,并使用 sh 脚本在 Wine 下透明地运行在 Linux 上。
编辑 2/3/2011:DMS 似乎在 Linux 和 Solaris 上的 Wine 下也运行良好。正在 OSX 下的 Wine 上测试 DMS。
编辑 3/1/2011:DMS 似乎也可以在 OSX 的 Wine 下运行。
编辑 2/21/2013:C++ 前端现在可以处理 ANSI C++11,以及 MS 和 GNU 版本的 C++11。
编辑 2/24/2015:现在可以处理 ANSI、MS 和 GNU 风格的 C++14。
编辑 2019 年 1 月 16 日:现在可以处理 ANSI、MS 和 GNU 风格的 C++17。
You might consider our DMS Software Reengineering Toolkit. DMS is a general foundation for parsing source text in arbitrary languages to compiler data structures
(ASTs, symbol tables, control flow graphs, data flow graphs depending on how far you take it).
DMS is a general purpose Source-to-source program transformation system. You can apply source-to-source pattern-directed transformations, or write procedural transformations (much like OpenC++),
and then regenerate compilable source text corresponding to the transformed program.
DMS is parameterized by explicit language definitions, and handles C, C#, COBOL, Java, Python, javascript, Fortran.
It has a full C++ Front End that handles many real dialects of C++ (ANSI, GNU, MS), with full name and type resolution. DMS with the C++ front end can carry out transformations controlled by "metaprograms" within and across multiple compilation units. It has been used in anger to do radical reorganizations of C++ software systems, including massive rearchitecting of mission avionics software (see papers at website), finally used in UAVs.
DMS runs on Windows, and transparently on Linux under Wine using sh scripts.
EDIT 2/3/2011: DMS seems run fine under Wine on Linux and Solaris, too. Testing underway for DMS on Wine under OSX.
EDIT 3/1/2011: DMS seems to run under Wine for OSX, too.
EDIT 2/21/2013: The C++ front end now handles ANSI C++11, as well as MS and GNU versions of C++11.
EDIT 2/24/2015: Now handles C++14 in ANSI, MS and GNU flavors.
EDIT 1/16/2019: Now handles C++17 in ANSI, MS and GNU flavors.