EncodeException"序列化的GlobalReference具有类型PointerType"

时间:2018-02-17 23:04:25

标签: haskell llvm

作为一名CS学生,我被要求用LLVM编写一个函数式语言编译器。我选择了Haskell,尽管事实上我对它很陌生并且我不了解源代码示例中的所有内容,这就是为什么我的代码可能看起来很蹩脚。

在真正开始我的项目之前,我想尝试一下LLVM的Haskell绑定并创建一个调用另一个函数的函数,它返回作为参数传递的两个整数之间的差异。

运行代码时,我有以下异常:

  

EncodeException"序列化的GlobalReference的类型为PointerType {pointerReferent = FunctionType {resultType = IntegerType {typeBits = 32},argumentTypes = [IntegerType {typeBits = 32},IntegerType {typeBits = 32}],isVarArg = False} ,pointerAddrSpace = AddrSpace 0}但应该有IntegerType类型{typeBits = 32}

我真的不明白我的代码有什么问题。

这是我的完整源代码,感谢您的帮助。

{-# LANGUAGE OverloadedStrings #-}

module Main where


import Control.Monad.Except

-- Pretty Printer

import LLVM.Pretty (ppllvm, ppll)

import LLVM.Module
import LLVM.Context
import LLVM.Module

-- AST
import LLVM.AST
import qualified LLVM.AST as AST
import LLVM.AST.Global
import LLVM.AST.CallingConvention
import LLVM.AST.Constant as Kokai
import qualified Data.ByteString.Char8 as B


int :: Type
int = IntegerType 32

defAdd :: Definition
defAdd = GlobalDefinition functionDefaults
  { name = Name "subbing"
  , parameters =
      ( [ Parameter int (Name "a") []
        , Parameter int (Name "b") [] ]
      , False )
  , returnType = int
  , basicBlocks = [block]
  }
  where

block :: BasicBlock
block = BasicBlock
    (Name "entry")
    [ Name "result" :=
      AST.Sub False
      False
      (LocalReference int (Name "a"))
      (LocalReference int (Name "b"))
      [] ]
    (Do $ Ret (Just (LocalReference int (Name "result"))) [])

foo :: Definition
foo = GlobalDefinition functionDefaults
  { name = Name "random_func"
  , parameters = ([], False)
  , returnType = int
  , basicBlocks = [calli]
  }
  where
calli :: BasicBlock
calli = BasicBlock
            (Name "entry")
            [Name "res" :=
             Call
                Nothing
                C
                []
                (Right $ ConstantOperand $ GlobalReference int "subbing")
                [(ConstantOperand $ Int 32 10, []),
                (ConstantOperand $ Int 32 7, [])]
                []
                []
                    ]
            (Do $ Ret (Just (ConstantOperand $ Int 32 10)) [])
            --(Do $ Ret (Just (LocalReference int (Name "res"))) [])

astModule :: AST.Module
astModule = defaultModule
  { moduleName = "my-module"
  , moduleDefinitions = [defAdd, foo]
  }

nimoft :: IO B.ByteString
nimoft = withContext $ \context ->
    withModuleFromAST context astModule $ \m -> do
      llstr <- moduleLLVMAssembly m
      B.putStrLn llstr
      return llstr

main :: IO ()
main = do
  lol <- nimoft
  Prelude.putStrLn "hello"

1 个答案:

答案 0 :(得分:1)

刚发现错误,我用以下方式更改了呼叫线路:

(Right $ ConstantOperand $ GlobalReference (PointerType (FunctionType int [int, int] False) (A.AddrSpace 0)) (Name "subbing"))