llvm-hs/llvm-hs-pretty

Name: llvm-hs-pretty

Owner: llvm-hs

Description: Pretty printer for LLVM AST to Textual IR

Created: 2017-03-20 13:01:32.0

Updated: 2018-05-12 15:35:07.0

Pushed: 2018-05-12 15:35:06.0

Homepage:

Size: 144

Language: LLVM

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

llvm-hs-pretty

Build Status Hackage

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

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

rt LLVM.AST
rt LLVM.Pretty (ppllvm)

vm :: Module -> Text
Tests
is is only necessary for running the test suite
 apt-get install llvm-dev-5.0

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:

ack build
ack test

Using cabal:

bal run
bal run -- tests/simple.ll

If you're using Nix then:

x-shell
bal run
Example

To try out the standalone example run:

ack repl
oad Example.hs

Consider the basic example LLVM module.

duleID = 'example-llvm-module'

ne i8 @f(i8 %x){
y:
t i8 %x

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

le Standalone where

retty Printer
rt LLVM.Pretty (ppllvm)

ST
rt qualified LLVM.AST as AST
rt qualified LLVM.AST.Linkage as Linkage
rt qualified LLVM.AST.Visibility as Visibility
rt qualified LLVM.AST.CallingConvention as Convention

rt Data.Text.Lazy.IO as TIO

odule :: AST.Module
odule = 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")
                            )
                        )
                        []
                    )
                )
            ]
        )
    ]
}

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

Released under the MIT License.

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


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.