Error in Compiling haskell .ll file with llvm backend

让人想犯罪 __ 提交于 2019-12-20 03:14:34

问题


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!"

I compile hello.hs with ghc using following command

ghc -fllvm -keep-llvm-files -force-recomp -hello.hs

which generate a hello.ll file along with other files. I then try to compile this .ll file into a .bc file.

llvm-as hello.ll -o hello.bc

and then this .bc file to executable file

llc hello.bc -o hello

which generate an executable file. The problem is when I run this file I found the following error

./hello: line 1: .file: command not found
./hello: line 2: .section: command not found
./hello: line 3: .globl: command not found
./hello: line 4: .align: command not found
./hello: line 5: .type: command not found
./hello: line 6: Main_main2_info:: command not found
./hello: line 8: subq: command not found
./hello: line 9: syntax error near unexpected token `('
./hello: line 9: `                 movq                                                                      
%r13, 112(%rsp)'

and when I use lli on .bc file I get

main function not found in module error

I am running ghc and llvm on docker. I have version 3.4 of llvm and version 7.6.3 of ghc.

A "similar" error occur when I try to to do this on mac. I don't know why I am getting this error.


回答1:


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!  
$


来源:https://stackoverflow.com/questions/46465081/error-in-compiling-haskell-ll-file-with-llvm-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!