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


i intrigued when heard ghc can output file c .

the glasgow haskell compiler (ghc) compiles native code on number of different architectures—as ansi c—using c-- intermediate language.

so installed haskell platform , created simple .hs file.

main = putstrln "hello, world!" 

and according manual.

-c stop after generating c (.hc file)

now run command.

ghc -c test.hs 

but doesn't create .hc file, nor stop mid-compilation.

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

if want understand how haskell programs execute @ low level, best study core instead (-ddump-simpl). after point code becomes hard read experts. main reason ghc's stack , heap management gets hard-coded. result, low-level generated haskell code giant mess of tiny procedures, doing complex pointer arithmetic before finishing indirect jumps unknown locations. worst kind of spaghetti code.

to provide of actual answer - could generate c going on 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 result illustrate point. trivial fib function take 80 lines of code.

in case interested in gory details, recommend reading through this blog post edward z. yang. walks through whole compilation pipeline involved in translating simple piece of haskell code.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -