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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/goccy/go-jit"
     7  )
     8  
     9  // func f(x, y, z int) int {
    10  //   temp1 := x * y
    11  //   temp2 := temp1 + z
    12  //   return temp2
    13  // }
    14  
    15  func main() {
    16  	ctx := jit.NewContext()
    17  	defer ctx.Close()
    18  	f, err := ctx.Build(func(ctx *jit.Context) (*jit.Function, error) {
    19  		f := ctx.CreateFunction([]*jit.Type{jit.TypeInt, jit.TypeInt, jit.TypeInt}, jit.TypeInt)
    20  		x := f.Param(0)
    21  		y := f.Param(1)
    22  		z := f.Param(2)
    23  		temp1 := f.Mul(x, y)
    24  		temp2 := f.Add(temp1, z)
    25  		f.Return(temp2)
    26  		f.Compile()
    27  		return f, nil
    28  	})
    29  	if err != nil {
    30  		panic(err)
    31  	}
    32  	fmt.Println("result = ", f.Run(2, 3, 4))
    33  }