/llvm-hs-pretty

Pretty printer for LLVM AST to Textual IR

Primary LanguageHaskellMIT LicenseMIT

llvm-hs-pretty

Build Status

A pretty printer for llvm-hs-pure. Goal is to be able to pretty print a sufficiently large subset of the LLVM AST from pure Haskell without having to go through the C++ API.

Usage

sudo apt-get install llvm-dev-4.0

There is a single function ppllvm that maps a LLVM.AST.Module to a String.

import LLVM.AST
import LLVM.Pretty (ppllvm)

ppllvm :: Module -> Text

Tests

The test suite currently consists of round tripping a LLVM IR from correct IR outputted by the llc toolchain, parsing into llvm-general AST and then printing it back out and comparing it with the original textual form to see if the pretty printer faithfully preserves the structure. The sample modules are in tests/.

Using stack:

$ stac build
$ stack test

Using cabal:

$ cabal run
$ cabal run -- tests/simple.ll

If you're using Nix then:

$ nix-shell
$ cabal run

Standalone Example

To try out the standalone example run:

$ stack repl
$ :load Standalone.hs
main

Consider the basic example LLVM module.

; ModuleID = 'example-llvm-module'

define i8 @f(i8 %x){
entry:
  ret i8 %x
}

Using the LLVM.AST we construct the type and feed it to the pretty printer.

module Standalone where

-- Pretty Printer
import LLVM.Pretty (ppllvm)

-- AST
import qualified LLVM.AST as AST
import qualified LLVM.AST.Linkage as Linkage
import qualified LLVM.AST.Visibility as Visibility
import qualified LLVM.AST.CallingConvention as Convention

import Data.Text.Lazy.IO as TIO

astModule :: AST.Module
astModule = AST.Module
    { AST.moduleName         = "example-llvm-module"
    , AST.moduleDataLayout   = Nothing
    , AST.moduleTargetTriple = Nothing
    , AST.moduleDefinitions  =
        [ AST.GlobalDefinition
            (AST.Function
                Linkage.External
                Visibility.Default
                Nothing
                Convention.C
                []
                (AST.IntegerType 8)
                (AST.Name "f")
                ([AST.Parameter (AST.IntegerType 8) (AST.Name "x") []], False)
                []
                Nothing
                Nothing
                0
                Nothing
                Nothing
                [ AST.BasicBlock
                    (AST.Name "entry")
                    []
                    (AST.Do
                        (AST.Ret
                            (Just
                                (AST.LocalReference
                                    (AST.IntegerType 8)
                                    (AST.Name "x")
                                )
                            )
                            []
                        )
                    )
                ]
            )
        ]
    }

main :: IO ()
main = TIO.putStrLn (ppllvm astModule)

License

Released under the MIT License.

Copyright (c) 2014-2017, Stephen Diehl Copyright (c) 2015 Cedric Shock