Question

I'm looking for a way of parsing LLVM IR code (preferably in assembly form) into some internal Haskell data type that I can use for later processing. I presume there is some Haskell package for doing this, but I can't seem to find one.

Any suggestions?

Was it helpful?

Solution

The modern LLVM package for Haskell is the llvm-general library. It has bindings to convert a LLVM bytecode module from string into a Haskell ADT for the AST.

See: withModuleFromString

import LLVM.General
import LLVM.General.Analysis
import LLVM.General.Context
import Control.Monad.Error

main :: String -> IO ()
main src = do
  _ <- withContext $ \context -> do
    runErrorT $ withModuleFromString context src $ \mod -> do
      ast <- moduleAST mod
      -- Your logic here.
  return ()

OTHER TIPS

There are LLVM bindings, but they're far from being complete in their parsing part and might lag behind recent LLVM releases. IR generation is far more complete. Although, it's not that hard to add what's missing as soon as you bump into it. I've used these bindings to write a compiler backend, they've never been a showstopper.

Any suggestions?

Use the LLVM-C API via FFI; specifically, the LLVMParseBitcode function should do the trick.

It's not exactly what you wanted, and you'll have to use other LLVM-C functions to query or modify the module, but at least you'll have support for the latest LLVM IR version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top