简体   繁体   English

比较ghc中生成的代码

[英]Compare generated code in ghc

A common practice in the C world to compare two fragmets of C is to see what assembly they generate. 在C世界中比较C的两个碎片的一个常见做法是查看它们产生的组件。 I wanted to know what code GHC would generate in the case of: 我想知道GHC在以下情况下会产生什么代码:

afmap :: Functor f => (a -> b -> c) -> f b -> a -> f c
afmap fn fb a' = (fn a') <$> fb

and

afmap  = flip . (((.).(.)) fmap ($))

So I tried: 所以我尝试过:

$ ghc -S test.hs -o test.S

Which (unsurprisingly) yielded more or less unreadable code. 其中(不出所料)产生了或多或少不可读的代码。

What is the correct way (if any) to evaluate how ghc optimizes code? 评估ghc如何优化代码的正确方法(如果有的话)是什么?

Assembly is probably a bit too low-level. 装配可能有点太低级了。 You probably want to look at Core, GHC's intermediate optimisation language. 你可能想看看GHC的中间优化语言Core。

Essentially, GHC translates Haskell to Core, does a wide variety of optimisation passes on it, and eventually transforms Core to STG and then on to C-- and the native code generator (ie, assembly) or via LLVM (I don't know much about that particular pathway). 本质上,GHC将Haskell转换为Core,对其进行各种优化传递,最终将Core转换为STG,然后转换为C--和本机代码生成器(即汇编)或通过LLVM(我不知道)很多关于那个特定的途径)。

In particular, Core is still reasonably high-level, and somewhat similar to Haskell (ie, it has similar abstractions like pattern-matching and lazy evaluation). 特别是,Core仍然相当高级,并且有点类似于Haskell(即,它具有类似的抽象,如模式匹配和惰性评估)。 If two programs produce the same Core, then obviously they produce the same machine code. 如果两个程序生成相同的Core,那么显然它们会生成相同的机器代码。

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

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