Clone or download
Latest commit 8da2963 Jun 19, 2017
Type Name Latest commit message Commit time
Failed to load latest commit information.
src clarify main module Jan 6, 2016
.gitignore initial commit Jan 2, 2016
.travis.yml travis build Jan 4, 2016
LICENSE update for modern stack lts Jun 18, 2017 travis build Jan 4, 2016
package.yaml bump vector upper bound Jun 19, 2017
stack.yaml update for modern stack lts Jun 18, 2017
tinyjit.cabal bump upper bound for vector Jun 19, 2017

Haskell JIT Example

Build Status MIT License

Tiny example of building a intermediate language that JIT compiles Haskell DSL into x86-64 machine code.


The factorial function can be written in assembly, taking the input value in %rcx and computing the resulting value in %rax.

.global main

        mov rcx, 5
        mov rax, 1
        mul rcx
        loop .factor

In our Haskell logic we compose these operations inside of the X86 monad.

factorial :: Int64 -> X86 ()
factorial n = do
  mov rcx (I n)
  mov rax (I 1)
  l1 <- label
  mul rcx
  loop l1

The resulting logic can be JIT compiled inside of Haskell and invoked from inside the Haskell runtime by calling out to the JIT'd memory.

main :: IO ()
main = do
  let jitsize = 256*1024
  mem <- allocateMemory jitsize
  let jitm = assemble mem (factorial 5)

  case jitm of
    Left err -> putStrLn err
    Right jitst -> do
      let machCode = _mach jitst
      fn <- jit mem machCode
      res <- fn
      putStrLn $ "Result: " <> show res

The machine code is generated.

48 c7 c1 05 00 00 00 48 c7 c0 01 00 00 00 48 f7 e1 e2 fc c3

And executed to yield the result:

Result: 120


Released under the MIT License. Copyright (c) 2016, Stephen Diehl