我的编译器在 C/C++、OCaml 和 F# 之间犹豫
我想开始开发一个小型编译器并构建它,我在几种不同的语言之间犹豫不决。
我的要求很简单,我希望能够发出 LLVM-IR 代码,因为我有一个 LLVM 后端,我想重用它来针对特定平台。
所以现在我有以下选择:
使用 OCaml 和 LLVM 绑定 - 高效,LLVM 附带 OCaml 绑定,但使用 OCaml(IDE、支持)的编码体验不是最好的。
使用 C/C++ 和 LLVM 绑定 - 我想说的最明显的方式,但我想使用函数式语言,因为这个主题对我来说是新的,我想学习新的东西。
使用 F# - 我喜欢这种语言,但没有官方的 LLVM 绑定。所以我想我可以通过使用 System.Reflection.Emit 来做同样的事情。尽管这里似乎有一个针对 LLVM 的 F# 绑定的倡议 - https://github.com/keithshep /llvm-fs
我很想了解您对此的想法。
I want to start working on a little compiler and to build it I am hesitating between several different languages.
My requirements are simple, I want to be able to emit LLVM-IR code cause I have a LLVM backend I would like to reuse to target a specific platform.
So right now I have the following choices :
Use OCaml and the LLVM bindings - Efficient, LLVM ships with the OCaml bindings, but the coding experience with OCaml (IDE, support) is not the best.
Use C/C++ and the LLVM bindings - The most obvious way I would say, but I would like to use a functional language as this topic is new to me and I want to learn something new.
Use F# - I felt in love with this language, but there are no official LLVM bindings. So I guess I could do the same through the use of System.Reflection.Emit. Even though it seems like there is an initiative here for F# binding for LLVM - https://github.com/keithshep/llvm-fs
I would love to get your thoughts on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
元编程是 C++ 的一个真正的弱点。你的大部分精力将花在操纵树木上。在这种情况下,OCaml 和 F# 的核心优势是相对于联合类型(而不是函数式编程)的模式匹配,因为这使得操作树变得更加容易。从历史上看,OCaml 和 F# 都来自 ML 语言家族,并且是专门针对该应用程序领域而培育的。
我通过 OCaml 绑定使用 LLVM 来编写 HLVM,其中包括独立编译和本地 JIT 编译代码、支持多核的垃圾收集、外部函数接口、尾部调用优化和许多其他功能。这次经历非常愉快。我唯一的建议是跟踪哪些 LLVM 功能经过尝试和测试,哪些是实验性的,因为您不想依赖任何实验性的东西(例如,我编写 HLVM 时的 GC 支持)。
您可以轻松地使用 System.Reflection.Emit 从 F# 生成 CIL,但显然您无法通过这样做来利用 LLVM 后端,当然,尽管您确实免费获得了垃圾收集器。 .NET 与 LLVM 的绑定是一种选择。我不熟悉您引用的内容,但编写与 LLVM 的 C API 的绑定相对简单。不过,我不确定 LLVM 在 Windows 平台上的支持情况如何。
关于 OCaml 与 F#,两者都有优点和缺点,但我想说在这种情况下总体差异相对较小。由于缺乏通用打印,在 OCaml 中编写函数来打印大联合类型的值是很乏味的,尽管这可以使用一些第三方宏来自动化。 F# 提供通用打印,但缺少一些有用的功能,例如多态变体和结构类型对象。
Metaprogramming is a real weak point of C++. Most of your effort will be expended trying to manipulate trees. The core advantage of OCaml and F# in this context is pattern matching over union types (and not functional programming) precisely because this makes it so much easier to manipulate trees. Historically, OCaml and F# come from the ML family of languages and were bred specifically for this application domain.
I used LLVM via its OCaml bindings to write HLVM, which includes both standalone and JIT compilation to native code, multicore-capable garbage collection, foreign function interface, tail call optimization and many other features. The experience was very pleasant. My only advice would be to keep track of which LLVM features are tried-and-tested and which are experimental because you don't want to depend on anything experimental (e.g. the GC support when I wrote HLVM).
You can easily use
System.Reflection.Emit
to generate CIL from F# but you obviously won't be able to leverage your LLVM backend by doing so, although you do get a garbage collector for free, of course. .NET bindings to LLVM are an option. I am not familiar with the ones you cite but writing bindings to LLVM's C API is relatively straightforward. However, I am not sure how well supported LLVM is on the Windows platform.Regarding OCaml vs F#, both have advantages and disadvantages but I'd say the overall difference is relatively small in this context. Writing functions to print values of big union types due to the lack of generic printing is tedious in OCaml, although this can be automated using some third-party macros. F# provides generic printing but is missing some useful features such as polymorphic variants and structurally-typed objects.