[haskell-llvm] [Haskell-cafe] LLVM, type-level?
Henning Thielemann
lemming at henning-thielemann.de
Sat Dec 11 15:23:31 EST 2010
On Sat, 11 Dec 2010, Lally Singh wrote:
> I had tried that as well, but figured it would be best to post about
> exactly what you had told me :-)
>
> Heres' what I have:
> buildReaderFun :: String -> CodeGenModule (MainFunction)
> buildReaderFun nm = do
> puts <- newNamedFunction ExternalLinkage "puts" ::
> TFunction (Ptr Word8 -> IO Word32)
> let callPuts format = (
> createNamedFunction ExternalLinkage "main" $
> \ argc argv -> do
> tmp <- getElementPtr (argv ::Value (Ptr (Ptr Word8)))
> (0 :: Int32, (1 :: Int32, ()))
> _ <- call puts tmp
> ret (0 :: Int32)) :: CodeGenModule (MainFunction)
>
> withStringNul nm callPuts
>
> And the result:
> /research/phd/libmet/Listener.hs:51:17:
> No instance for (GetElementPtr (Ptr Word8) (Int32, ()) Word8)
> arising from a use of `getElementPtr'
> at /research/phd/libmet/Listener.hs:(51,17)-(52,47)
>
> Is there another instance I should add?
No, these GetElementPtr type errors really point you to a serioues
problem. GHC proposes to add instances because in the general case this
might be a solution. But here you try to do something that is simply not
supported by LLVM, and thus 'llvm' forbids it.
I look into
http://hackage.haskell.org/packages/archive/llvm/0.9.0.1/doc/html/LLVM-Core.html#t:GetElementPtr
and see that there is no instance for 'optr = Ptr a'. That means in order
to follow two pointers successively, you have to call getElementPtr twice.
I assume that with
> tmp <- getElementPtr (argv ::Value (Ptr (Ptr Word8)))
> (0 :: Int32, (1 :: Int32, ()))
you want to have a pointer to the first (not zeroth) character of the
zeroth of argv. Is that what you want? I think you have to write
> tmp0 <- getElementPtr (argv ::Value (Ptr (Ptr Word8))) (0 :: Int32, ())
> tmp1 <- load tmp0
> tmp <- getElementPtr tmp1 (1 :: Int32, ())
http://www.llvm.org/docs/GetElementPtr.html#firstptr
"Why is it okay to index through the first pointer, but subsequent pointers won't be dereferenced?"
More information about the Haskell-llvm
mailing list