
Is there any work being done to read LLVM object code into Haskell? I've looked through the llvm library [1], but it appears focused on code generation. -Tom [1] http://hackage.haskell.org/package/llvm

On May 30, 2010, at 7:01 AM, Tom Hawkins wrote:
Is there any work being done to read LLVM object code into Haskell? I've looked through the llvm library [1], but it appears focused on code generation.
-Tom
I was just looking through the Haddock pages for the llvm package with the same question in mind, and realized that there is some support for reading and dissecting bitcode files. It has a pretty low-level and un-Haskell-ish interface, though. For instance, the LLVM.FFI.BitReader module has some functions that'll get you a ModuleRef from some bitcode. It looks as though getFirstGlobal and getNextGlobal will iterate through top-level things in a ModuleRef, including functions. Alternatively, it looks like getNamedFunction will give you a handle to a particular function. And there are functions to get the basic blocks of a function, and to get the instructions of a basic block. All of these functions are in the IO monad, though, so they'd only really be pleasant to use if you immediately built up a new AST, perhaps in some special-purpose intermediate language, or perhaps in something resembling LLVM structure, before doing any analysis. If anyone knows of a better way, I'm also very interested. Or, if anyone has made any attempt to build higher-level abstractions out of the low-level functions provided, I'm very interested. Maybe at some point I'll take a crack at the latter myself. Aaron

On Sun, May 30, 2010 at 11:26 PM, Aaron Tomb
On May 30, 2010, at 7:01 AM, Tom Hawkins wrote:
Is there any work being done to read LLVM object code into Haskell? I've looked through the llvm library [1], but it appears focused on code generation.
-Tom
I was just looking through the Haddock pages for the llvm package with the same question in mind, and realized that there is some support for reading and dissecting bitcode files. It has a pretty low-level and un-Haskell-ish interface, though.
For instance, the LLVM.FFI.BitReader module has some functions that'll get you a ModuleRef from some bitcode. It looks as though getFirstGlobal and getNextGlobal will iterate through top-level things in a ModuleRef, including functions. Alternatively, it looks like getNamedFunction will give you a handle to a particular function. And there are functions to get the basic blocks of a function, and to get the instructions of a basic block.
All of these functions are in the IO monad, though, so they'd only really be pleasant to use if you immediately built up a new AST, perhaps in some special-purpose intermediate language, or perhaps in something resembling LLVM structure, before doing any analysis.
If anyone knows of a better way, I'm also very interested. Or, if anyone has made any attempt to build higher-level abstractions out of the low-level functions provided, I'm very interested. Maybe at some point I'll take a crack at the latter myself.
Not quite related, but I'm currently hacking around to get the LLVM.FFI bits of the llvm library rewritten to use c2hs, hopefully with a slightly nicer low-level API that may help. - Dave
Aaron
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

For instance, the LLVM.FFI.BitReader module has some functions that'll get you a ModuleRef from some bitcode.
getBitcodeModuleInContext :: ContextRef -> MemoryBufferRef -> Ptr ModuleRef -> Ptr CString -> IO Bool type ModuleRef = Ptr Module data Module I'm confused how this works. How does one get a Ptr ModuleRef to call this function? Module is not Storable, so you can't use alloca or malloc. And I don't see any functions in the library that returns a Ptr ModuleRef. What am I missing? (Sorry, I lack experience with Foreign.Ptr.) To get started, I want to make a function that reads an llvm object file and returns a ModuleRef: readObj :: FilePath -> IO ModuleRef -Tom

On May 31, 2010, at 3:02 PM, Tom Hawkins wrote:
For instance, the LLVM.FFI.BitReader module has some functions that'll get you a ModuleRef from some bitcode.
getBitcodeModuleInContext :: ContextRef -> MemoryBufferRef -> Ptr ModuleRef -> Ptr CString -> IO Bool type ModuleRef = Ptr Module data Module
I'm confused how this works. How does one get a Ptr ModuleRef to call this function? Module is not Storable, so you can't use alloca or malloc. And I don't see any functions in the library that returns a Ptr ModuleRef. What am I missing? (Sorry, I lack experience with Foreign.Ptr.)
That's a good question, and I'm not sure of the answer. I based my previous email purely on a quick scan the documentation. I've never actually tried to use it. :) Aaron
participants (3)
-
Aaron Tomb
-
David Anderson
-
Tom Hawkins