如何使用 LLVM 作为其他语言(不是 C++)的后端?
How to use LLVM as backend in other language (not in C++)?
我正在尝试编写一种编程语言并卡在代码生成阶段。
经过深思熟虑,我决定使用LLVM作为我的后端,因为我不想处理晦涩难懂的低级东西(生成汇编对我来说很好,但我需要更多关于链接的知识才能完成我的作品)。
一个绊脚石是我的工作不是基于 C++。这意味着我无法在我的代码中使用现成的 LLVM 类。
能否生成字符串形式的LLVM IR代码,保存到文件(或者不需要?)然后编译?
在我可以的情况下,我可以生成任何其他形式来帮助 LLVM 运行 更快吗?
特别感谢任何建议。
Could I generate LLVM IR code in the form of characters string, save it to file (or no need?) and then compile it?
一些项目(例如 GHC)正在使用这种方法,但不推荐使用。相反,您可以使用 LLVM C 绑定。与 C 接口是许多语言的共同特征,因此应该不是问题。
正在复制 LLVM documentation
Your compiler front-end will communicate with LLVM by creating a
module in the LLVM intermediate representation (IR) format. Assuming
you want to write your language’s compiler in [something else than
C++], there are 3 major ways to tackle generating LLVM IR from a
front-end:
Call into the LLVM libraries code using your language’s FFI (foreign function interface).
- for: best tracks changes to the LLVM IR, .ll syntax, and .bc format
- for: enables running LLVM optimization passes without a emit/parse overhead
- for: adapts well to a JIT context
- against: lots of ugly glue code to write
Emit LLVM assembly from your compiler’s native language.
- for: very straightforward to get started
- against: the .ll parser is slower than the bitcode reader when interfacing to the middle end
- against: it may be harder to track changes to the IR
Emit LLVM bitcode from your compiler’s native language.
- for: can use the more-efficient bitcode reader when interfacing to the middle end
- against: you’ll have to re-engineer the LLVM IR object model and bitcode writer in your language
- against: it may be harder to track changes to the IR
你说的选项是2号。
我正在尝试编写一种编程语言并卡在代码生成阶段。
经过深思熟虑,我决定使用LLVM作为我的后端,因为我不想处理晦涩难懂的低级东西(生成汇编对我来说很好,但我需要更多关于链接的知识才能完成我的作品)。
一个绊脚石是我的工作不是基于 C++。这意味着我无法在我的代码中使用现成的 LLVM 类。
能否生成字符串形式的LLVM IR代码,保存到文件(或者不需要?)然后编译? 在我可以的情况下,我可以生成任何其他形式来帮助 LLVM 运行 更快吗?
特别感谢任何建议。
Could I generate LLVM IR code in the form of characters string, save it to file (or no need?) and then compile it?
一些项目(例如 GHC)正在使用这种方法,但不推荐使用。相反,您可以使用 LLVM C 绑定。与 C 接口是许多语言的共同特征,因此应该不是问题。
正在复制 LLVM documentation
Your compiler front-end will communicate with LLVM by creating a module in the LLVM intermediate representation (IR) format. Assuming you want to write your language’s compiler in [something else than C++], there are 3 major ways to tackle generating LLVM IR from a front-end:
Call into the LLVM libraries code using your language’s FFI (foreign function interface).
- for: best tracks changes to the LLVM IR, .ll syntax, and .bc format
- for: enables running LLVM optimization passes without a emit/parse overhead
- for: adapts well to a JIT context
- against: lots of ugly glue code to write
Emit LLVM assembly from your compiler’s native language.
- for: very straightforward to get started
- against: the .ll parser is slower than the bitcode reader when interfacing to the middle end
- against: it may be harder to track changes to the IR
Emit LLVM bitcode from your compiler’s native language.
- for: can use the more-efficient bitcode reader when interfacing to the middle end
- against: you’ll have to re-engineer the LLVM IR object model and bitcode writer in your language
- against: it may be harder to track changes to the IR
你说的选项是2号。