来自 C++ 的中间代码
我想将 C++ 程序编译为中间代码。然后,我想使用当前处理器的所有资源来编译中间代码。
第一步是使用优化(-O2)编译 C++ 程序,运行链接器并完成大部分编译过程。此步骤必须独立于操作系统和体系结构。
第二步是在没有原始源代码的情况下,针对当前计算机的操作系统和处理器,使用处理器的优化和特殊指令(-march=native)编译第一步的结果。第二步应该很快并且软件要求最低。
我可以做吗?怎么做呢?
编辑:
我想这样做,因为我想分发一个独立于平台的程序,可以使用处理器的所有资源,而不需要原始源代码,而不是为每个平台和操作系统分发一个编译版本。如果第二步又快又简单就好了。
相同架构的处理器可能具有不同的功能。 X86 处理器可能具有 SSE1、SSE2 或其他处理器,并且它们可以是 32 位或 64 位。如果我针对通用 X86 进行编译,它将缺乏 SSE 优化。许多年后,处理器将具有新功能,并且需要为新处理器编译程序。
I want to compile a C++ program to an intermediate code. Then, I want to compile the intermediate code for the current processor with all of its resources.
The first step is to compile the C++ program with optimizations (-O2), run the linker and do most of the compilation procedure. This step must be independent of operating system and architecture.
The second step is to compile the result of the first step, without the original source code, for the operating system and processor of the current computer, with optimizations and special instructions of the processor (-march=native). The second step should be fast and with minimal software requirements.
Can I do it? How to do it?
Edit:
I want to do it, because I want to distribute a platform independent program that can use all resources of the processor, without the original source code, instead of distributing a compilation for each platform and operating system. It would be good if the second step be fast and easy.
Processors of the same architecture may have different features. X86 processors may have SSE1, SSE2 or others, and they can be 32 or 64 bit. If I compile for a generic X86, it will lack of SSE optimizations. After many years, processors will have new features, and the program will need to be compiled for new processors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是一个建议 - 谷歌 clang 和 LLVM。
Just a suggestion - google clang and LLVM.
关于编译器你了解多少?您似乎将“-O2”视为某种神奇的标志。
例如,寄存器分配就是一种典型的优化。您现在肯定需要知道有多少寄存器可用。将
foo
分配给寄存器 16,然后在第 2 阶段发现您的目标是 x86,这是没有意义的。这些依赖于架构的优化可能非常复杂。内联主要取决于调用成本,而调用成本又取决于架构。
How much do you know about compilers? You seem to treat "-O2" as some magical flag.
For instance, register assignment is a typical optimization. You definitely need to now how many registers are available. No point in assigning
foo
to register 16, and then discover in phase 2 that you're targetting an x86.And those architecture-dependent optimizations can be quite complex. Inlining depends critically on call cost, and that in turn depends on architecture.
一旦进行“特定于处理器”的优化,事情就会变得非常棘手。对于特定于平台的编译器来说,要在适当的“级别”生成对象或“中间”代码时真正做到“通用”确实很困难:除非它是类似“IL”(中间语言)代码(如 C#-IL)代码或 Java 字节码),对于给定的编译器来说,知道“在哪里停止”确实很困难(因为当目标平台知识存在时,优化会发生在编译的不同级别上)。
另一个想法:编译为“预处理”源代码(通常带有“
*.i
”扩展名),然后在不同架构上以分布式方式编译怎么样?例如,大多数(所有)C 和 C++ 编译器都支持以下内容:
...each 生成
MyFile.i
,这是预处理文件。现在该文件已包含全部标头和其他#defines
,您可以将该*.i
文件编译为目标对象文件(或可执行文件)在分发到其他系统后。 (如果您的预处理器宏特定于目标平台,您可能需要变得聪明,但对于您的构建系统来说,它应该非常简单,它应该生成命令行来执行此预处理。)这是使用的方法通过 distcc 在本地预处理文件,因此远程“构建农场”不需要 安装的任何标头或其他包。 (无论构建场中的机器如何配置,您都保证获得相同的构建产品。)
因此,它同样具有集中单个机器的“配置/预处理”的效果,但提供交叉- 以分布式方式进行编译、特定于平台的编译或构建场支持。
仅供参考 - 我真的很喜欢
distcc
概念,但该特定项目的最后一次更新是在 2008 年。因此,如果您找到其他类似的工具/产品,我会对它们感兴趣。 (与此同时,我正在编写一个类似的工具。)Once you get to "processor-specific" optimizations, things get really tricky. It's really tough for a platform-specific compiler to be truly "generic" in its generation of object or "intermediate" code at an appropriate "level": Unless it's something like "IL" (intermediate language) code (like the C#-IL code, or Java bytecode), it's really tough for a given compiler to know "where to stop" (since optimizations occur all over the place at different levels of the compilation when target platform knowledge exists).
Another thought: What about compiling to "preprocessed" source code, typically with a "
*.i
" extension, and then compile in a distributed manner on different architectures?For example, most (all) the C and C++ compilers support something like:
...each generates
MyFile.i
, which is the preprocessed file. Now that the file has included ALL the headers and other#defines
, you can compile that*.i
file to the target object file (or executable) after distributing it to other systems. (You might need to get clever if your preprocessor macros are specific to the target platform, but it should be quite straight-forward with your build system, which should generate the command line to do this pre-processing.)This is the approach used by distcc to preprocess the file locally, so remote "build farms" need not have any headers or other packages installed. (You are guaranteed to get the same build product, no matter how the machines in the build farm are configured.)
Thus, it would similarly have the effect of centralizing the "configuration/pre-processing" for a single machine, but provide cross-compiling, platform-specific compiling, or build-farm support in a distributed manner.
FYI -- I really like the
distcc
concept, but the last update for that particular project was in 2008. So, I'd be interested in other similar tools/products if you find them. (In the mean time, I'm writing a similar tool.)