[haskell-llvm] Generating a structure at runtime

Nathan Howell nathan.d.howell at gmail.com
Fri Jul 8 16:39:54 BST 2011


On Fri, Jul 8, 2011 at 5:31 AM, Henning Thielemann <
thunderbird at henning-thielemann.de> wrote:

> Nathan Howell wrote:
>
>> I managed to get something hacked together, I'm not sure if this is
>> optimal but it seems to work for my simplified test case.
>>
>> https://gist.github.com/**1071154 <https://gist.github.com/1071154>
>>
>
>  c :: Value (Ptr (Struct a)) <- bitcast args
>  val :: Value (Ptr Double) <- getElementPtr c (0::Int32, (0::Int32, ()))
>
> If c is a pointer to (Struct a), then you cannot access individual elements
> of the struct. If you know the offset of the field, then you may cast the
> pointer to Ptr Int8, then get the address of the field with (getElementPtr c
> (offset::Int32)), and then cast the field pointer to Ptr Double.
>
> Of course, coping with offsets is ugly and maybe not portable. You may
> succeed constructing the Struct type at runtime using RankNTypes.
>

To make it easier, I added a new function "unsafeGetElementPtr" which
doesn't enforce type safety at compile time. This is fine for my
application. The change is here:

https://github.com/alphaHeavy/llvm/commit/8840ccbb39b4788b70dc859154b5fa3634aee634

Which allows me to do this:

val :: Value (Ptr Double) <- unsafeGetElementPtr c [0::Int32, 0]
val' <- load val
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://projects.haskell.org/pipermail/haskell-llvm/attachments/20110708/59a60409/attachment.htm>


More information about the Haskell-llvm mailing list