当在Haskell中使用llvm绑定时,模块中找不到'main'函数 [英] 'main' function not found in module when using llvm bindings with Haskell

查看:355
本文介绍了当在Haskell中使用llvm绑定时,模块中找不到'main'函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Haskell的LLVM绑定来创建一个非常简单的'hello world'独立应用程序。这个想法是,当我运行我的Haskell应用程序时,它会吐出一些可以运行的字节码,并输出hello world!。

I'm trying to use Haskell's LLVM bindings to create a very simple 'hello world' standalone app. The idea is, when I run my Haskell application, it will spit out some bytecode that can in turn be run and will output "hello world!"

-- hellofunc prints out "hello world"
hellofunc :: CodeGenModule (Function (IO ()))


_main :: (Function (IO ())) -> CodeGenModule (Function (IO ()))
_main func = createNamedFunction ExternalLinkage "main" $ do
        call func
        ret ()

main = writeCodeGenModule "hello.bc" (liftA _main hellofunc)

当我运行这个时,我看到以下错误:

When I run this, I see the following error:

'main' function not found in module.

我明确地创建了 main 函数使用 createNamedFunction 。我缺少什么?

I'm explicitly creating the main function using createNamedFunction. What am I missing?

推荐答案

问题在于使用 liftA fmap 而不是 = << 。当使用类型签名时,它变得更加明显(构建中断)。 _main不是在liftA情况下的CodeGenModule中计算的,所以它不会出现在输出文件中。

The problem lies in the use of liftA or fmap rather than =<<. It becomes more obvious (a build break) when type signatures are used. _main is not evaluated in CodeGenModule in the liftA case, and so it doesn't appear in the output file.

修改可能是合理的, writeCodeGenModule 取得 CodeGenModule()而不是 CodeGenModule a 。它使得LLVM JIT用户的生活变得更加复杂,但是可以帮助防止出现这种错误。

It may be reasonable to modify writeCodeGenModule to take CodeGenModule () instead of CodeGenModule a. It makes life just a bit more complicated for users of the LLVM JIT but would help prevent errors such as this one.

import Control.Applicative
import LLVM.Core
import LLVM.ExecutionEngine
import LLVM.Util.File

--
-- hellofunc prints out "hello world"
hellofunc :: CodeGenModule (Function (IO ()))
hellofunc = createNamedFunction ExternalLinkage "hello" $ do
  ret ()

_main :: (Function (IO ())) -> CodeGenModule (Function (IO ()))
_main func = createNamedFunction ExternalLinkage "main" $ do
  call func
  ret ()

generateModuleBind :: CodeGenModule (Function (IO ()))
generateModuleBind = _main =<< hellofunc

-- Couldn't match expected type `Function (IO ())'
--             with actual type `CodeGenModule (Function (IO ()))'
--
-- this is the desired type:
-- generateModuleLiftA :: CodeGenModule (Function (IO ()))
--
-- but this is the actual type:
generateModuleLiftA :: CodeGenModule (CodeGenModule (Function (IO ())))
generateModuleLiftA = liftA _main hellofunc

main :: IO ()
main = writeCodeGenModule "hello.bc" generateModuleBind

这篇关于当在Haskell中使用llvm绑定时,模块中找不到'main'函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆