Error in Compiling haskell .ll file with llvm backend

前端 未结 1 1929
感动是毒
感动是毒 2021-01-23 02:27

I want to compile haskell using ghc front-end and llvm back-end.

I have following code in my haskell hello.hs file:

main = putStrLn \"Hello World!\"


        
相关标签:
1条回答
  • 2021-01-23 03:16

    The llc program doesn't create executables. It compiles LLVM bytecode to native assembly source (by default) or an object file. If you run:

    llc hello.bc -o hello
    

    and then inspect the resulting "hello" file with a text editor, you'll see that it's an Intel assembly file, and the syntax errors you're getting are because you're trying to run it as a shell script.

    Instead, you can compile the LLVM bytecode directly to an object file:

    llc hello.bc -filetype=obj -o hello.o
    

    and then try to link it into an executable:

    ld hello.o -o hello
    

    This will generate a bunch of linkage errors because it needs to be linked with the Haskell runtime. It turns out that the best way to link in the Haskell runtime is to use ghc itself to do the linking:

    ghc hello.o -o hello
    

    Note that this command does not actually recompile the Haskell source. It just does the linking. If you want to see the linkage commands it runs, use:

    ghc -v hello.o -o hello
    

    and you'll understand why it's best to leave the linking to ghc!!

    Finally, after all this, you should be able to run "hello". The full set of commands is:

    $ ghc -fllvm -keep-llvm-files -fforce-recomp hello.hs                                                           
    [1 of 1] Compiling Main             ( hello.hs, hello.o )                                                       
    Linking hello ...
    $ rm hello                                                                                               
    $ llvm-as hello.ll -o hello.bc                                                                                  
    $ llc hello.bc -filetype=obj -o hello.o                                                                         
    $ ghc hello.o -o hello                                                                                          
    $ ./hello                                                                                                       
    Hello, world!  
    $
    
    0 讨论(0)
提交回复
热议问题