Can't output from .hs to .hc with GHC in Haskell

I was intrigued when I heard that GHC can output a file to C .

The Glasgow Haskell Compiler (GHC) compiles to native code on a number of different architectures—as well as to ANSI C—using C-- as an intermediate language.

So I installed the Haskell Platform and created a simple .hs file.

main = putStrLn "Hello, World!"

And according to the manual.

-C Stop after generating C (.hc file)

Now I run the command.

ghc -C test.hs

But it doesn't create a .hc file, nor does it stop mid-compilation.

$ ls
test.exe  test.hi  test.hs  test.o

If you want to understand how Haskell programs execute at a low level, it is probably best to study Core instead ( -ddump-simpl ). After that point the code becomes really hard to read even for experts. The main reason is that GHC's stack and heap management gets hard-coded. As a result, low-level generated Haskell code is basically a giant mess of tiny procedures, doing complex pointer arithmetic before finishing with indirect jumps to unknown locations. The worst kind of spaghetti code.

To provide something of an actual answer - you could generate C by going over the LLVM backend:

ghc -ddump-llvm -ddump-to-file hello_world.hs
opt -O2 hello_world.dump-llvm | llc -O2 -march c -o hello_world.c

But the result will just illustrate the point. Even a trivial fib function will take about 80 lines of code.

In case you are really interested in the gory details, I would recommend reading through this blog post by Edward Z. Yang. He basically walks through the whole compilation pipeline involved in translating a simple piece of Haskell code.

链接地址: http://www.djcxy.com/p/5696.html

上一篇: 与GHC的Windows上的位DLL?

下一篇: 使用GHC在Haskell中不能从.hs输出到.hc