MyNixOS website logo
Description

A pretty printer for LLVM IR.

A pretty printer for the LLVM AST types provided by llvm-hs.

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.

Note: It is possible to construct llvm-hs-pure ASTs that are invalid ASTs and as such there is no meaningful way to print them. Always run the LLVM verifier on your AST to test it is sound. If you encounter incomplete pattern matches using this library you likely have constructed invalid IR.

Usage

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

import LLVM.AST
import LLVM.Pretty (ppllvm)

ppllvm :: Module -> Text

Individual LLVM elements (constants, instructions) can be printed using the the polymorphic ppll function for any LLVM structure that implements the PP typeclass.

Tests

# This is only necessary for running the test suite
sudo apt-get install llvm-9-dev

The test suite currently consists of round tripping a LLVM IR from correct IR outputted by the llc toolchain, parsing into llvm-hs 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:

$ stack build
$ stack test

Using cabal:

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

If you're using Nix then:

$ nix-shell
$ cabal run

Example

To try out the standalone example run:

$ stack repl
$ :load Example.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-2020, Stephen Diehl Copyright (c) 2015 Cedric Shock.

Metadata

Version

0.9.0.0

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows