llvm-hs/llvm-hs

Name: llvm-hs

Owner: llvm-hs

Description: Haskell bindings for LLVM

Created: 2017-01-06 19:25:26.0

Updated: 2018-05-16 06:05:45.0

Pushed: 2018-05-08 18:49:32.0

Homepage:

Size: 1529

Language: Haskell

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

llvm-hs - Haskell bindings for LLVM

Build Status Hackage

This project aims to provide a relatively complete set of bindings for the LLVM API. If you find that anything is missing please open an issue! We generally try to stay close to the LLVM C++-API so you can consult the LLVM documentation and reuse existing resources.

Getting started

If you?ve worked with LLVM before, take a look at the examples in the llvm-hs-examples repo. If not, you can find a translation of the official LLVM tutorial at https://github.com/llvm-hs/llvm-hs-kaleidoscope. In general, we try to stay very close to the API and AST provided by LLVM itself, so the LLVM language reference is also very useful.

Contributing

We love all kinds of contributions so feel free to open issues for missing LLVM features, report & fix bugs or report API inconveniences.

Installing LLVM
Homebrew

Example using Homebrew on macOS:

ew install llvm-hs/llvm/llvm-6.0
Debian/Ubuntu

For Debian/Ubuntu based Linux distributions, the LLVM.org website provides binary distribution packages. Check apt.llvm.org for instructions for adding the correct package database for your OS version, and then:

t-get install llvm-6.0-dev
Nix

Nix users can use the following commands to build the library:

x-shell
bal new-build llvm-hs

The Nix shell uses a pinned version of nixpkgs by default. You can define the nixpkgs argument to use a different nixpkgs tree:

x-shell --arg nixpkgs '<nixpkgs>'
Building from source

Example of building LLVM from source. Detailed build instructions are available on the LLVM.org website here. CMake 3.4.3 and a recent C++ compiler are required, at least Clang 3.1, GCC 4.8, or Visual Studio 2015 (Update 3).

  1. Download and unpack the LLVM-6.0 source code. We'll refer to the path the source tree was unpacked to as LLVM_SRC.

  2. Create a temporary build directory and cd to it, for example:

    ir /tmp/build
    /tmp/build
    
  3. Execute the following to configure the build. Here, INSTALL_PREFIX is where LLVM is to be installed, for example /usr/local:

    ke $LLVM_SRC -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX -DLLVM_BUILD_LLVM_DYLIB=True -DLLVM_LINK_LLVM_DYLIB=True
    

    See options and variables for a list of additional build parameters you can specify.

  4. Build and install:

    ke --build .
    ke --build . --target install
    
  5. For macOS only, some additional steps are useful to work around issues related to System Integrity Protection:

    $INSTALL_PREFIX/lib
    -s libLLVM.dylib libLLVM-6.0.dylib
    tall_name_tool -id $PWD/libLTO.dylib libLTO.dylib
    tall_name_tool -id $PWD/libLLVM.dylib libLLVM.dylib
    tall_name_tool -change '@rpath/libLLVM.dylib' $PWD/libLLVM.dylib libLTO.dylib
    
Versioning

Trying to represent the version of LLVM in the version number but also allowing for version bumps in the bindings themselves while respecting the PVP can be tricky. Luckily LLVM is switching to a new versioning scheme of major.0.patch starting from version 4.0. This means that we can use the last two components for these bindings while the first component indicates the version of LLVM. A special case are the versions 3.major.minor that represent bindings to LLVM 3.9. Bindings to earlier versions are not provided.

How is this related to llvm-general?

This project is a fork of the venerable llvm-general that aims to improve the public release story, and better provide the interfaces needed for any Haskell project looking to leverage LLVM. Contributions are encouraged.

IRBuilder

A IRBuilder, starting out as a thin reinterpretation of the C++ IRBuilder inside of a Haskell State monad. Goal is to eliminate a lot of boilerplate around the most common uses of llvm-hs as a compiler backend.

Example LLVM module that adds two numbers:

duleID = 'exampleModule'

ne external ccc i32 @add(i32 %a, i32 %b){
y:
 = add i32 %a, %b
t i32 %0

askell
LANGUAGE OverloadedStrings #-}
LANGUAGE RecursiveDo #-}

rt Data.Text.Lazy.IO as T

rt LLVM.Pretty  -- from the llvm-hs-pretty package
rt LLVM.AST hiding (function)
rt LLVM.AST.Type as AST
rt qualified LLVM.AST.Float as F
rt qualified LLVM.AST.Constant as C

rt LLVM.IRBuilder.Module
rt LLVM.IRBuilder.Monad
rt LLVM.IRBuilder.Instruction

le :: IO ()
le = T.putStrLn $ ppllvm $ buildModule "exampleModule" $ mdo

nction "add" [(i32, "a"), (i32, "b")] i32 $ \[a, b] -> mdo

entry <- block `named` "entry"; do
  c <- add a b
  ret c

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.