使用llvm后端编译haskell .ll文件时出错 [英] Error in Compiling haskell .ll file with llvm backend
问题描述
我想使用ghc前端和llvm后端编译haskell.
I want to compile haskell using ghc front-end and llvm back-end.
我的haskell hello.hs文件中包含以下代码:
I have following code in my haskell hello.hs file:
main = putStrLn "Hello World!"
我使用以下命令用ghc编译hello.hs
I compile hello.hs with ghc using following command
ghc -fllvm -keep-llvm-files -force-recomp -hello.hs
会与其他文件一起生成hello.ll文件. 然后,我尝试将该.ll文件编译成.bc文件.
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
,然后将此.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)'
当我在.bc文件上使用lli时,我会得到
and when I use lli on .bc file I get
main function not found in module error
我正在docker上运行ghc和llvm.我有llvm的3.4版和ghc的7.6.3版.
I am running ghc and llvm on docker. I have version 3.4 of llvm and version 7.6.3 of ghc.
当我尝试在Mac上执行此操作时,发生类似"错误.我不知道为什么会收到此错误.
A "similar" error occur when I try to to do this on mac. I don't know why I am getting this error.
推荐答案
llc
程序不会创建可执行文件.它将LLVM字节码编译为本地程序集源(默认情况下)或目标文件.如果您运行:
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
,然后使用文本编辑器检查生成的"hello"文件,您将看到它是一个Intel汇编文件,并且由于尝试将其作为Shell脚本运行而收到语法错误.
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.
相反,您可以将LLVM字节码直接编译为目标文件:
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
这将产生一系列链接错误,因为它需要与Haskell运行时链接.事实证明,在Haskell运行时中进行链接的最佳方法是使用ghc
本身进行链接:
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
请注意,此命令实际上不会重新编译Haskell源.它只是进行链接.如果要查看它运行的链接命令,请使用:
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
您将了解为什么最好保留链接到ghc
的原因!!
and you'll understand why it's best to leave the linking to ghc
!!
最后,完成所有这些操作之后,您应该可以运行"hello"了.完整的命令集是:
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!
$
这篇关于使用llvm后端编译haskell .ll文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!