选择与 LLVM 一起使用的函数式编程语言时,有哪些权衡?

发布于 2024-08-11 11:15:10 字数 247 浏览 3 评论 0 原文

我们暂时假设 C++ 不是函数式编程语言。如果你想使用 LLVM 为后端编写一个编译器,并且你想使用函数式编程语言及其与 LLVM 的绑定来完成你的工作,据我所知,你有两个选择:Obj​​ective Caml 和 Haskell。如果还有其他人,那么我也想了解这些。

我不是在征求主观意见,所以请不要给它加上主观标签。我想对此做出自己的决定,但我不确定我是否知道所有的权衡是什么。所以,StackOverflow 来救援了。有哪些权衡?

Let's assume for the moment that C++ is not a functional programming language. If you want to write a compiler using LLVM for the back-end, and you want to use a functional programming language and its bindings to LLVM to do your work, you have two choices as far as I know: Objective Caml and Haskell. If there are others, then I'd like to know about those too.

I'm not asking for subjective opinions, so please don't give this the subjective tag. I want to make up my own mind about this, but I'm not sure I know what are all the trade-offs. So, StackOverflow to the rescue. What are the trade-offs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很糊涂小朋友 2024-08-18 11:15:10

OCaml 或 Haskell 都是不错的选择。为什么不查看每种语言的 LLVM 教程? OCaml 的 LLVM 教程位于:http://llvm.org/docs/tutorial/OCamlLangImpl1.html

Haskell 如今势头强劲,但 OCaml 也有很多优秀的解析库,包括 PEG 解析器生成器 AurochsMenhir 和 GLR解析器生成器 Dypgen。另请查看 pcl 上的演示文稿,这是一个用于 OCaml 的单子解析器组合器库(例如 Haskell 的 Parsec),其中有一些比较 Haskell 和 OCaml 方法的好信息: http://osp.janestreet.com/files/pcl.pdf

有人会说,惰性使 Haskell 在解析方面具有优势,但 OCaml 中也存在惰性。

Either OCaml or Haskell would be a good choice. Why not check out the LLVM tutorials for each language? The LLVM tutorial for OCaml is here: http://llvm.org/docs/tutorial/OCamlLangImpl1.html

Haskell has more momentum these days, but there are plenty of good parsing libraries for OCaml as well including the PEG parser generator Aurochs, Menhir, and the GLR parser generator Dypgen. Also check out this presentation on pcl a monadic parser combinator library for OCaml (like Parsec for Haskell) there's some good info in there comparing Haskell's and OCaml's approach: http://osp.janestreet.com/files/pcl.pdf

Some will say that laziness gives Haskell the edge in parsing, but you can get laziness in OCaml as well.

是你 2024-08-18 11:15:10

Haskell 比 OCaml 具有更高级别的 LLVM 绑定(Haskell 提供了一些有趣的类型安全保证),并且 Haskell 拥有更多的库可供使用(http://hackage.haskell.org)使得将组件粘合在一起变得更容易。

Haskell has higher level bindings to LLVM than OCaml (the Haskell ones provide some interesting type safety guarantees) and Haskell has by far more libraries to use (1700 packages on http://hackage.haskell.org) making it easier to glue together components.

避讳 2024-08-18 11:15:10

本机绑定的可用性不必限制您对语言的选择。除了使用绑定或直接生成 IR 文本之外,还有第三种选择:

您可以使用语言中立的序列化格式(例如 Google 的 Protocol Buffers)作为从前端到后端的桥梁。毕竟,Protocol buffer 只是伪装的 AST。

用函数式语言实现的前端,然后执行其最擅长的操作——解析、类型检查、脱糖、核心到核心转换等——而 C++ 后端从前端获取 IR 并使用 LLVM 的功能- 完整的定义本机 C++ API,用于从您的语言 IR 降低到 LLVM IR。这使得处理 LLVM 的“高级”功能(例如调试元数据)变得更加容易。

我将此策略与 hprotoc 以及相关的 Haskell 协议缓冲区绑定一起使用,并且对结果非常感到满意。使用正确的工具来完成工作有很多话要说!

Availability of native bindings need not constrain your choice of language. There is a third option, apart from using bindings or generating IR text directly:

You can use a language-neutral serialization format, such as Google's Protocol Buffers, to serve as the bridge from your front-end to your back-end. Protocol buffers are, after all, just ASTs in disguise.

Your front end, implemented in a functional language, then does what it is best at -- parsing, type checking, desugaring, core-to-core transformations, etc -- and the C++ backend takes the IR from your frontend and uses LLVM's feature-complete-by-definition native C++ API to do lowering from your-language-IR to LLVM IR. This makes it much easier to handle "advanced" features of LLVM such as debug metadata.

I'm using this strategy with hprotoc and associated Haskell bindings for protocol buffers, and am very happy with the results. There is much to be said for using the right tool for the job!

风铃鹿 2024-08-18 11:15:10

OCaml 是唯一一种LLVM 发行版本身具有绑定的函数式语言llvm.org 上的文档,例如万花筒教程。如果您在构建和安装 LLVM 时安装了 OCaml,那么它也会自动构建和安装 OCaml 的 LLVM 绑定。此外,这些 OCaml 绑定已经使用多年所以他们成熟可靠。

我一直在使用标准 LLVM 绑定在 OCaml 中开发 HLVM ,并发现 OCaml+LLVM 是一个极其强大的组合。 HLVM 提供元组、数组、联合、所有尾部调用的 TCO、通用打印、FFI 到 C、JIT 编译和并行垃圾收集,VM 的 OCaml 代码量低于 2kLOC,从头开始开发只需要几个人周的时间。 HLVM 的数值性能已经远远超过当今最快的开放源 FPL,包括 OCaml 本身。我在 OCaml Journal 上发表了文章,描述了如何在 OCaml 中使用 LLVM 来处理以下所有内容:从基本表达式评估到并行性和垃圾收集等高级主题。您可能还会喜欢这个迷你示例

OCaml is the only functional language with bindings in the LLVM distro itself and documentation on llvm.org such as the Kaleidoscope tutorial. If you have OCaml installed when you build and install LLVM then it will automatically build and install the LLVM bindings for OCaml as well. Moreover, these OCaml bindings have been in use for years so they are mature and reliable.

I have been developing HLVM in OCaml using the standard LLVM bindings and found OCaml+LLVM to be an extremely powerful combination. HLVM provides tuples, arrays, unions, TCO of all tail calls, generic printing, FFI to C, JIT compilation and parallel garbage collection with a VM weighing in at under 2kLOC of OCaml code that took only a few man-weeks to develop from scratch. HLVM's numerical performance already far exceeds that of today's fastest open source FPLs including OCaml itself. I have published articles in the OCaml Journal describing how LLVM can be used from OCaml for everything from basic expression evaluation to advanced topics such as parallelism and garbage collection. You may also like this mini example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文