tinyjit
所属分类:collect
开发工具:Haskell
文件大小:0KB
下载次数:0
上传日期:2020-01-19 12:06:10
上 传 者:
sh-1993
说明: 哈斯克尔JIT,
(Haskell JIT,)
文件列表:
.travis.yml (1146, 2020-01-19)
LICENSE (1063, 2020-01-19)
package.yaml (624, 2020-01-19)
src/ (0, 2020-01-19)
src/Assembler.hs (5333, 2020-01-19)
src/JIT.hs (3870, 2020-01-19)
src/Main.hs (1117, 2020-01-19)
stack.yaml (82, 2020-01-19)
tinyjit.cabal (1129, 2020-01-19)
Haskell JIT Example
-------------------
[![Build Status](https://travis-ci.org/sdiehl/tinyjit.svg)](https://travis-ci.org/sdiehl/tinyjit)
[![MIT License](http://img.shields.io/badge/license-mit-blue.svg)](https://github.com/sdiehl/tinyjit/blob/master/LICENSE)
Tiny example of building a intermediate language that JIT compiles Haskell DSL
into x86-64 machine code.
Usage
-----
The factorial function can be written in assembly, taking the input value in
``%rcx`` and computing the resulting value in ``%rax``.
```perl
.global main
main:
mov rcx, 5
mov rax, 1
.factor:
mul rcx
loop .factor
ret
```
In our Haskell logic we compose these operations inside of the ``X86`` monad.
```haskell
factorial :: Int64 -> X86 ()
factorial n = do
mov rcx (I n)
mov rax (I 1)
l1 <- label
mul rcx
loop l1
ret
```
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.
```haskell
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
```
License
-------
Released under the MIT License.
Copyright (c) 2016-2020, Stephen Diehl
近期下载者:
相关文件:
收藏者: