简体   繁体   English

GHC Core是“字节码”吗?

[英]GHC Core as “bytecode”?

As I understand it, GHC (the Glorious Glasgow Haskell Compiler) compiles Haskell to "Core", and then compiles that Core into machine code. 据我了解,GHC(Glorious Glasgow Haskell编译器)将Haskell编译为“ Core”,然后将该Core编译为机器代码。 Would it be at all practical to distribute Haskell programs as GHC Core, as if it were "bytecode"? 将Haskell程序作为GHC Core分发,就好像它是“字节码”一样,完全可行吗? Would there be any benefit to such a distribution? 这样的分配会有好处吗? Why or why not? 为什么或者为什么不?

This wouldn't be practical; 这不切实际; GHC Core is not portable. GHC Core不可移植。 For example, on a 32-bit machine, 64-bit arithmetic is compiled down to foreign function calls in the Core, but on a 64-bit machine, it uses native machine-word arithmetic. 例如,在32位计算机上,将64位算术编译为Core中的外部函数调用,但是在64位计算机上,它使用本机字算法。

More importantly, GHC can't actually read Core; 更重要的是,GHC实际上无法读取 Core。 it can print it out in a few formats, but there is no actual code to read any of those formats back in. I'm not sure if there would be any major obstacle to doing so, but it's been the documented situation for many years, so I wouldn't expect support to appear any time soon. 它可以以几种格式打印出来,但是没有实际的代码可以读回其中的任何一种格式。我不确定这样做是否会遇到重大障碍,但这已经有很多年了,因此我希望支持不会很快出现。

Core is also pretty close to Haskell in general; 总的来说,Core也非常接近Haskell。 it's not clear what you'd buy from distributing code in that form. 不清楚您将以这种形式分发代码会买到什么。 The time it takes to turn Haskell into Core is usually going to be less than the time it takes to do things like link the final program, so it usually wouldn't save much on compilation time at all. 将Haskell转换为Core所需的时间通常少于完成链接最终程序之类的操作所需的时间,因此通常根本不会节省太多编译时间。

Also, less checking is done to Core than Haskell source code (although I think -dcore-lint would mitigate this), and sandboxing it effectively would be difficult (there's Safe Haskell, but no Safe Core). 此外,对Core的检查要比Haskell源代码少(尽管我认为-dcore-lint可以减轻这种情况),并且将其有效沙箱化将很困难(有Safe Haskell,但没有Safe Core)。 Of course, these disadvantages don't apply if the source of the bytecode is trusted. 当然,如果字节码的源是可信任的,那么这些缺点就不会应用。

Basically, GHC Core is very much a compiler's intermediate language, as opposed to portable bytecode formats designed for the purpose, like Python bytecode and the JVM. 基本上,GHC Core基本上是编译器的中间语言,与为此目的而设计的可移植字节码格式(例如Python字节码和JVM)相反。

As a side note, GHC does have a bytecode interpreter, as used by GHCi. 附带说明一下,GHC 确实具有GHCi使用的字节码解释器。 The bytecode used there is also non-portable, so there are no advantages I can think of compared to the machine code GHC produces in normal operation. 那里使用的字节码也是不可移植的,因此与GHC在正常操作中产生的机器代码相比,我没有想到的优势。

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

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