简体   繁体   English

编译到 GHC 核心

[英]Compiling to GHC Core

I would like to create a frontend for a simple language that would produce GHC Core.我想为一种可以生成 GHC Core 的简单语言创建一个前端。 I would like to then take this output and run it through the normal GHC pipeline.然后我想获取这个输出并通过正常的 GHC 管道运行它。 According to this page , it is not directly possible from the ghc command.根据此页面,无法直接通过 ghc 命令执行此操作。 I am wondering if there is any way to do it.我想知道是否有任何方法可以做到这一点。

I am ideally expecting a few function calls to the ghc-api but I am also open to any suggestions that include (not-so-extensive) hacking in the source of GHC.理想情况下,我期望对 ghc-api 进行一些函数调用,但我也愿意接受任何建议,包括(不那么广泛的)GHC 源代码中的黑客攻击。 Any pointers would help!任何指针都会有所帮助!

Note that Core is an explicitly typed language, which can make it quite difficult to generate from other languages (the GHC type checker has inferred all the types so it's no problem there).请注意,Core 是一种显式类型的语言,这使得从其他语言生成它变得非常困难(GHC 类型检查器已推断出所有类型,因此没有问题)。 For example, the usual identity function (id = \\x -> x :: forall a. a -> a) becomes例如,通常的恒等函数(id = \\x -> x :: forall a. a -> a)变成

id = \(a :: *) (x :: a) -> a

where a is a type variable of kind * .其中a是类型为*的类型变量。 It is a term-level place-holder for the type-level forall binding.它是类型级forall绑定的术语级占位符。 Similarly, when calling id you need to give it a type as its first argument, so the Haskell expression (id 42) gets translated into (id Int 42) .类似地,当调用id你需要给它一个类型作为它的第一个参数,所以 Haskell 表达式(id 42)被转换为(id Int 42) Such type bindings and type applications won't be present in the generated machine code, but they are useful to verify compiler transformations are correct.此类类型绑定和类型应用程序不会出现在生成的机器代码中,但它们可用于验证编译器转换是否正确。

On the bright side, it might be possible to just generate Haskell -- if you can generate the code in such a way that GHC will always be able to determine its type then you are essentially just using a tiny subset of Haskell.从好的方面来说,可能只生成 Haskell —— 如果您可以以 GHC 始终能够确定其类型的方式生成代码,那么您实际上只是使用 Haskell 的一个很小的子集。 Whether this can work depends very much on your source language, though.不过,这是否可行在很大程度上取决于您的源语言。

There's still no way to read External Core files, whether via the ghc command or the API.无论是通过ghc命令还是 API,仍然无法读取外部核心文件。 Sorry :(对不起 :(

It's probably theoretically possible to build the Core syntax tree up from your representation using the GHC API, but that sounds very painful.理论上可能可以使用 GHC API 从您的表示中构建核心语法树,但这听起来非常痛苦。 I would recommend targeting some other backend.我建议针对其他一些后端。 You don't necessarily have to stop using GHC;你不一定要停止使用 GHC; straightforward Haskell with unboxed types and unsafeCoerce lets you get pretty close to the resulting Core, so you could define your own simple "Core-ish" language and compile it to that.带有未装箱类型和unsafeCoerce简单 Haskell 使您可以非常接近生成的 Core,因此您可以定义自己的简单“Core-ish”语言并将其编译为该语言。 (Indeed, you could probably even compile GHC Core itself , but that's a bit too meta for my tastes.) (事实上​​,你甚至可以自己编译 GHC Core ,但这对我的口味来说有点太元了。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM