Giter Club home page Giter Club logo

go-jit's Introduction

go-jit

GoDoc

JIT compile library for Go

Status

WIP

Synopsis

Basic Operation

package main

import (
  "fmt"

  "github.com/goccy/go-jit"
)

// func f(x, y, z int) int {
//   temp1 := x * y
//   temp2 := temp1 + z
//   return temp2
// }

func main() {
  ctx := jit.NewContext()
  defer ctx.Close()
  f, err := ctx.Build(func(ctx *jit.Context) (*jit.Function, error) {
    f := ctx.CreateFunction([]*jit.Type{jit.TypeInt, jit.TypeInt, jit.TypeInt}, jit.TypeInt)
    x := f.Param(0)
    y := f.Param(1)
    z := f.Param(2)
    temp1 := f.Mul(x, y)
    temp2 := f.Add(temp1, z)
    f.Return(temp2)
    f.Compile()
    return f, nil
  })
  if err != nil {
    panic(err)
  }
  fmt.Println("result = ", f.Run(2, 3, 4))
}

Call defined Go function during JIT runtime

package main

import (
  "fmt"

  "github.com/goccy/go-jit"
)

// func f() int {
//   return callback(7, 8)
// }

func callback(i, j int) int {
  fmt.Printf("callback: i = %d j = %d\n", i, j)
  return i * j
}

func main() {
  ctx := jit.NewContext()
  defer ctx.Close()
  f, err := ctx.Build(func(ctx *jit.Context) (*jit.Function, error) {
    f := ctx.CreateFunction(nil, jit.TypeInt)
    rvalues, err := f.GoCall(callback, []*jit.Value{
      f.CreateIntValue(7), f.CreateIntValue(8),
    })
    if err != nil {
      return nil, err
    }
    f.Return(rvalues[0])
    f.Compile()
    return f, nil
  })
  if err != nil {
    panic(err)
  }
  fmt.Println("result = ", f.Run(nil))
}

Installation

go get github.com/goccy/go-jit

go-jit's People

Contributors

goccy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

go-jit's Issues

This project seems to have wrong license?

It seems like this project is licensed as MIT, but it contains LGPL code from libjit. Those two licenses are not compatible to be used as source files in the same project. As far as I know (I am no lawyer) this project should probably link with libjit dynamically, to be able to use it legally.

error: use of unknown builtin '__builtin_apply' [-Wimplicit-function-declaration]

hi, I use this repo and run its demo code in my M1 Mac book pro. It seems it can not build up. The error is below:
# github.com/goccy/go-jit/internal/ccall jit-apply.c:357:3: error: use of unknown builtin '__builtin_apply' [-Wimplicit-function-declaration] ../../../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/jit-apply-func.h:47:19: note: expanded from macro 'jit_builtin_apply' jit-apply.c:357:3: note: did you mean '__builtin_alloca'? ../../../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/jit-apply-func.h:47:19: note: expanded from macro 'jit_builtin_apply' jit-apply.c:335:2: note: '__builtin_alloca' declared here ../../../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/jit-apply-rules.h:151:47: note: expanded from macro 'jit_apply_builder_init' /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/alloca.h:39:22: note: expanded from macro 'alloca' /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/alloca.h:40:24: note: expanded from macro '__alloca' jit-apply.c:357:3: warning: incompatible integer to pointer conversion assigning to 'jit_apply_return *' from 'int' [-Wint-conversion] ../../../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/jit-apply-func.h:47:17: note: expanded from macro 'jit_builtin_apply' jit-apply.c:362:3: error: use of unknown builtin '__builtin_apply' [-Wimplicit-function-declaration] ../../../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/jit-apply-func.h:47:19: note: expanded from macro 'jit_builtin_apply' jit-apply.c:362:3: warning: incompatible integer to pointer conversion assigning to 'jit_apply_return *' from 'int' [-Wint-conversion] ../../../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/jit-apply-func.h:47:17: note: expanded from macro 'jit_builtin_apply' jit-apply.c:397:3: error: use of unknown builtin '__builtin_apply' [-Wimplicit-function-declaration] ../../../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/jit-apply-func.h:47:19: note: expanded from macro 'jit_builtin_apply' jit-apply.c:397:3: warning: incompatible integer to pointer conversion assigning to 'jit_apply_return *' from 'int' [-Wint-conversion] ../../../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/jit-apply-func.h:47:17: note: expanded from macro 'jit_builtin_apply' jit-apply.c:401:3: error: use of unknown builtin '__builtin_apply' [-Wimplicit-function-declaration] ../../../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/jit-apply-func.h:47:19: note: expanded from macro 'jit_builtin_apply' jit-apply.c:401:3: warning: incompatible integer to pointer conversion assigning to 'jit_apply_return *' from 'int' [-Wint-conversion] ../../../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/jit-apply-func.h:47:17: note: expanded from macro 'jit_builtin_apply'

Build Error

Hello, I when I try to build, I get this error

jit-insn.c: In function ‘initialize_setjmp_block’:
jit-insn.c:7222:59: error: ‘sigsetjmp’ undeclared (first use in this function); did you mean ‘sigset_t’?
 7222 |  value = jit_insn_call_native(func, "sigsetjmp", (void *) sigsetjmp,
      |                                                           ^~~~~~~~~
      |                                                           sigset_t
jit-insn.c:7222:59: note: each undeclared identifier is reported only once for each function it appears in
jit-insn.c: In function ‘jit_insn_convert’:
jit-insn.c:4283:15: note: the ABI of passing union with ‘long double’ has changed in GCC 4.4
 4283 |   const_value = jit_value_get_constant(value);
      |   ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Building Readme example error

/projects/go-test ❯ go run .   
# github.com/goccy/go-jit/internal/ccall
jit-dump.c: In function ‘jit_dump_value’:
jit-dump.c:151:29: note: the ABI of passing union with ‘long double’ has changed in GCC 4.4
  151 |                 const_value = jit_value_get_constant(value);
      |                 ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jit-dump.c: In function ‘dump_object_code’:
jit-dump.c:722:9: warning: ignoring return value of ‘system’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  722 |         system(cmdline);
      |         ^~~~~~~~~~~~~~~
jit-dump.c:725:9: warning: ignoring return value of ‘system’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  725 |         system(cmdline);
      |         ^~~~~~~~~~~~~~~
# github.com/goccy/go-jit/internal/ccall
In file included from /usr/include/features.h:486,
                 from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33,
                 from /usr/include/stdio.h:27,
                 from ../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/include/jit/jit-dump.h:24,
                 from ../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/include/jit/jit.h:34,
                 from jit-internal.h:26,
                 from jit-elf-read.c:23:
In function ‘read’,
    inlined from ‘jit_readelf_open’ at jit-elf-read.c:877:5:
/usr/include/x86_64-linux-gnu/bits/unistd.h:38:10: warning: ‘__read_alias’ writing 48 bytes into a region of size 2 overflows the destination [-Wstringop-overflow=]
   38 |   return __glibc_fortify (read, __nbytes, sizeof (char),
      |          ^~~~~~~~~~~~~~~
In file included from jit-elf-read.c:25:
/usr/include/x86_64-linux-gnu/bits/unistd.h: In function ‘jit_readelf_open’:
jit-elf-defs.h:91:17: note: destination object ‘e_type’ of size 2
   91 |   Elf64_Half    e_type;                 /* Object file type */
      |                 ^~~~~~
In file included from /usr/include/features.h:486,
                 from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33,
                 from /usr/include/stdio.h:27,
                 from ../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/include/jit/jit-dump.h:24,
                 from ../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/include/jit/jit.h:34,
                 from jit-internal.h:26,
                 from jit-elf-read.c:23:
/usr/include/x86_64-linux-gnu/bits/unistd.h:26:16: note: in a call to function ‘__read_alias’ declared with attribute ‘access (write_only, 2, 3)’
   26 | extern ssize_t __REDIRECT (__read_alias, (int __fd, void *__buf,
      |                ^~~~~~~~~~
# github.com/goccy/go-jit/internal/ccall
jit-insn.c: In function ‘initialize_setjmp_block’:
jit-insn.c:7222:66: error: ‘sigsetjmp’ undeclared (first use in this function); did you mean ‘sigset_t’?
 7222 |         value = jit_insn_call_native(func, "sigsetjmp", (void *) sigsetjmp,
      |                                                                  ^~~~~~~~~
      |                                                                  sigset_t
jit-insn.c:7222:66: note: each undeclared identifier is reported only once for each function it appears in
jit-insn.c: In function ‘jit_insn_convert’:
jit-insn.c:4283:29: note: the ABI of passing union with ‘long double’ has changed in GCC 4.4
 4283 |                 const_value = jit_value_get_constant(value);
      |                 ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~/projects/go-test 6s ❯ sudo apt install gcc                   
[sudo] password for joetifa: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
gcc is already the newest version (4:11.2.0-1ubuntu1).
gcc set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 73 not upgraded.
~/projects/go-test ❯ gcc -v  
~/projects/go-test ❯ 
~/projects/go-test ❯ go build .      
# github.com/goccy/go-jit/internal/ccall
jit-dump.c: In function ‘jit_dump_value’:
jit-dump.c:151:29: note: the ABI of passing union with ‘long double’ has changed in GCC 4.4
  151 |                 const_value = jit_value_get_constant(value);
      |                 ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jit-dump.c: In function ‘dump_object_code’:
jit-dump.c:722:9: warning: ignoring return value of ‘system’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  722 |         system(cmdline);
      |         ^~~~~~~~~~~~~~~
jit-dump.c:725:9: warning: ignoring return value of ‘system’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  725 |         system(cmdline);
      |         ^~~~~~~~~~~~~~~
# github.com/goccy/go-jit/internal/ccall
In file included from /usr/include/features.h:486,
                 from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33,
                 from /usr/include/stdio.h:27,
                 from ../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/include/jit/jit-dump.h:24,
                 from ../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/include/jit/jit.h:34,
                 from jit-internal.h:26,
                 from jit-elf-read.c:23:
In function ‘read’,
    inlined from ‘jit_readelf_open’ at jit-elf-read.c:877:5:
/usr/include/x86_64-linux-gnu/bits/unistd.h:38:10: warning: ‘__read_alias’ writing 48 bytes into a region of size 2 overflows the destination [-Wstringop-overflow=]
   38 |   return __glibc_fortify (read, __nbytes, sizeof (char),
      |          ^~~~~~~~~~~~~~~
In file included from jit-elf-read.c:25:
/usr/include/x86_64-linux-gnu/bits/unistd.h: In function ‘jit_readelf_open’:
jit-elf-defs.h:91:17: note: destination object ‘e_type’ of size 2
   91 |   Elf64_Half    e_type;                 /* Object file type */
      |                 ^~~~~~
In file included from /usr/include/features.h:486,
                 from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33,
                 from /usr/include/stdio.h:27,
                 from ../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/include/jit/jit-dump.h:24,
                 from ../../go/pkg/mod/github.com/goccy/[email protected]/internal/ccall/include/jit/jit.h:34,
                 from jit-internal.h:26,
                 from jit-elf-read.c:23:
/usr/include/x86_64-linux-gnu/bits/unistd.h:26:16: note: in a call to function ‘__read_alias’ declared with attribute ‘access (write_only, 2, 3)’
   26 | extern ssize_t __REDIRECT (__read_alias, (int __fd, void *__buf,
      |                ^~~~~~~~~~
# github.com/goccy/go-jit/internal/ccall
jit-insn.c: In function ‘initialize_setjmp_block’:
jit-insn.c:7222:66: error: ‘sigsetjmp’ undeclared (first use in this function); did you mean ‘sigset_t’?
 7222 |         value = jit_insn_call_native(func, "sigsetjmp", (void *) sigsetjmp,
      |                                                                  ^~~~~~~~~
      |                                                                  sigset_t
jit-insn.c:7222:66: note: each undeclared identifier is reported only once for each function it appears in
jit-insn.c: In function ‘jit_insn_convert’:
jit-insn.c:4283:29: note: the ABI of passing union with ‘long double’ has changed in GCC 4.4
 4283 |                 const_value = jit_value_get_constant(value);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.