根据这篇SO文章,Haskell程序编译成C的方式不再被官方支持。因此,我想尝试将Haskell程序编译成LLVM IR。我选择了与提到的文章相同的程序:
quicksort [] = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
where
lesser = filter (< p) xs
greater = filter (>= p) xs
main = print(quicksort([5,2,1,0,8,3]))
然后尝试使用以下命令将其编译为LLVM IR:
$ ghc -fllvm main.hs
然后我遇到了有关LLVM版本的错误:
<no location info>: error:
Warning: Couldn't figure out LLVM version!
Make sure you have installed LLVM 3.7
ghc: could not execute: opt-3.7
当我检查我的opt版本时,它是3.8.0,比较大:
$ opt --version
LLVM (http://llvm.org/):
LLVM version 3.8.0
DEBUG build with assertions.
Built Jun 20 2018 (14:59:34).
Default target: x86_64-unknown-linux-gnu
Host CPU: broadwell
发生了什么? GHC 是否只能期望版本 3.7.0,而不能使用其他版本?!
编辑:
安装 LLVM 3.7.0 并将 opt
和 llc
复制为带有 3.7 后缀的文件。
$ cp opt opt-3.7
$ cp llc llc-3.7
使用以下命令进行编译为LLVM时没有出现错误:
$ ghc -keep-llvm-files main.hs
并创建了一个名为main.ll的文件。