github.com/goccy/go-jit@v0.0.0-20200514131505-ff78d45cf6af/README.md (about)

     1  # go-jit
     2  
     3  [![GoDoc](https://godoc.org/github.com/goccy/go-jit?status.svg)](https://pkg.go.dev/github.com/goccy/go-jit?tab=doc)
     4  
     5  JIT compile library for Go
     6  
     7  # Status
     8  
     9  WIP
    10  
    11  # Synopsis
    12  
    13  ## Basic Operation
    14  
    15  ```go
    16  package main
    17  
    18  import (
    19    "fmt"
    20  
    21    "github.com/goccy/go-jit"
    22  )
    23  
    24  // func f(x, y, z int) int {
    25  //   temp1 := x * y
    26  //   temp2 := temp1 + z
    27  //   return temp2
    28  // }
    29  
    30  func main() {
    31    ctx := jit.NewContext()
    32    defer ctx.Close()
    33    f, err := ctx.Build(func(ctx *jit.Context) (*jit.Function, error) {
    34      f := ctx.CreateFunction([]*jit.Type{jit.TypeInt, jit.TypeInt, jit.TypeInt}, jit.TypeInt)
    35      x := f.Param(0)
    36      y := f.Param(1)
    37      z := f.Param(2)
    38      temp1 := f.Mul(x, y)
    39      temp2 := f.Add(temp1, z)
    40      f.Return(temp2)
    41      f.Compile()
    42      return f, nil
    43    })
    44    if err != nil {
    45      panic(err)
    46    }
    47    fmt.Println("result = ", f.Run(2, 3, 4))
    48  }
    49  ```
    50  
    51  ## Call defined Go function during JIT runtime
    52  
    53  ```go
    54  package main
    55  
    56  import (
    57    "fmt"
    58  
    59    "github.com/goccy/go-jit"
    60  )
    61  
    62  // func f() int {
    63  //   return callback(7, 8)
    64  // }
    65  
    66  func callback(i, j int) int {
    67    fmt.Printf("callback: i = %d j = %d\n", i, j)
    68    return i * j
    69  }
    70  
    71  func main() {
    72    ctx := jit.NewContext()
    73    defer ctx.Close()
    74    f, err := ctx.Build(func(ctx *jit.Context) (*jit.Function, error) {
    75      f := ctx.CreateFunction(nil, jit.TypeInt)
    76      rvalues, err := f.GoCall(callback, []*jit.Value{
    77        f.CreateIntValue(7), f.CreateIntValue(8),
    78      })
    79      if err != nil {
    80        return nil, err
    81      }
    82      f.Return(rvalues[0])
    83      f.Compile()
    84      return f, nil
    85    })
    86    if err != nil {
    87      panic(err)
    88    }
    89    fmt.Println("result = ", f.Run(nil))
    90  }
    91  ```
    92  
    93  # Installation
    94  
    95  ```
    96  go get github.com/goccy/go-jit
    97  ```
    98