/llvm-hs-kaleidoscope

Kaleidoscope Tutorial using llvm-hs

Primary LanguageHaskellMIT LicenseMIT

llvm-tutorial-standalone

Build Status MIT License

A minimal LLVM builder. Basically the same code as the Haskell Kaleidoscope tutorial uses but without going through a frontend AST.

If you want to roll a custom LLVM compiler backend this might be a good starting point for the backend.

Install

Check that your installed LLVM version is precisely 9.0.

$ llvm-config --version
9.0

To build using stack:

$ stack build
$ stack exec main

To build using cabal:

$ cabal new-build

To run:

$ stack run
$ cabal run
Preprocessing executable 'standalone' for tutorial-0.2.0.0...
; ModuleID = 'my cool jit'

define double @main() {
  entry:
    ret double 3.000000e+01
}

Evaluated to: 30.0

Usage

Code is split across

The main program will use the embedded LLVM Monad to define a small program which will add two constants together.

initModule :: AST.Module
initModule = emptyModule "my cool jit"

logic :: LLVM ()
logic = do
  define double "main" [] $ \ptrToMain -> do
    let a = cons $ C.Float (F.Double 10)
    let b = cons $ C.Float (F.Double 20)
    res <- fadd a b
    ret res

main :: IO (AST.Module)
main = do
  let ast = runLLVM initModule logic
  runJIT ast
  return ast

This will generate and JIT compile into the following IR and use the LLVM execution engine to JIT it to machine code.

; ModuleID = 'my cool jit'

define double @main() {
entry:
  %1 = fadd double 1.000000e+01, 2.000000e+01
  ret double %1
}