1

我正在尝试通过ghc 7.4.1 上的llvm-3.0.0.0包在 llvm 中创建一个结构并访问其中的元素,并且遇到了一些毛茸茸的类型。这是一个示例代码,我正在尝试获取结构的第二个元素(即Word32在 TestStruct.

{-# LANGUAGE TypeOperators #-}
import LLVM.Core
import Data.Word

type TestStruct = Struct (Word16 :& Word32 :& Word64)
getTestPtr :: Value (Ptr TestStruct) -> CodeGenFunction r (Value (Ptr Word32))
getTestPtr u = getElementPtr u (0 :: Word32, (1  :: Word32, ()))

但我在 ghc 7.4.1 上收到以下错误:

Test.hs:8:16:
    Overlapping instances for llvm-3.0.0.0:LLVM.Core.Instructions.GetField
                                (Word32, Word64) i Word32
      arising from a use of `getElementPtr'
    Matching instances:
      instance [overlap ok] (llvm-3.0.0.0:LLVM.Core.Instructions.GetField
                               as i b,
                             Succ i i') =>
                            llvm-3.0.0.0:LLVM.Core.Instructions.GetField (a, as) i' b
        -- Defined in `llvm-3.0.0.0:LLVM.Core.Instructions'
      instance [overlap ok] llvm-3.0.0.0:LLVM.Core.Instructions.GetField
                              (a, as) D0 a
        -- Defined in `llvm-3.0.0.0:LLVM.Core.Instructions'
    (The choice depends on the instantiation of `i'
     To pick the first instance above, use -XIncoherentInstances
     when compiling the other instance declarations)
    In the expression: getElementPtr u (0 :: Word32, (0 :: Word32, ()))
    In an equation for `getTestPtr':
        getTestPtr u = getElementPtr u (0 :: Word32, (0 :: Word32, ()))

Test.hs:8:16:
    No instances for (Data.TypeLevel.Num.Sets.PosI Word32,
                      Data.TypeLevel.Num.Ops.IsZero Word32 yz,
                      DivMod10 Word32 yi yl)
      arising from a use of `getElementPtr'
    Possible fix:
      add instance declarations for
      (Data.TypeLevel.Num.Sets.PosI Word32,
       Data.TypeLevel.Num.Ops.IsZero Word32 yz,
       DivMod10 Word32 yi yl)
    In the expression: getElementPtr u (0 :: Word32, (0 :: Word32, ()))
    In an equation for `getTestPtr':
        getTestPtr u = getElementPtr u (0 :: Word32, (0 :: Word32, ()))
4

1 回答 1

1

Struct对包中的a 进行索引llvm需要类型级别的数字,以便可以静态计算字段类型。这就是描述记录字段的嵌套元组的原因。如果您使用getElementPtr第一个索引值需要是一个常规整数(Word32或其他),或者只是使用getElementPtr0

{-# LANGUAGE TypeOperators #-}
import LLVM.Core
import Data.TypeLevel
import Data.Word

type TestStruct = Struct (Word16 :& Word32 :& Word64)
getTestPtr :: Value (Ptr TestStruct) -> CodeGenFunction r (Value (Ptr Word32))
-- getTestPtr u = getElementPtr u ((0 :: Word32) & d1 & ())
getTestPtr u = getElementPtr0 u (d1 & ())
于 2012-04-28T00:55:49.147 回答