github.com/goccy/go-jit@v0.0.0-20200514131505-ff78d45cf6af/internal/ccall/context.go (about) 1 package ccall 2 3 /* 4 #cgo CFLAGS: -I../ 5 #cgo CFLAGS: -Iinclude 6 7 #include <jit/jit.h> 8 9 extern void *get_crosscall2addr(); 10 extern void *get_cgo_wait_runtime_init_done_addr(); 11 */ 12 import "C" 13 14 var ( 15 JIT_OPTION_CACHE_LIMIT = C.JIT_OPTION_CACHE_LIMIT 16 JIT_OPTION_CACHE_PAGE_SIZE = C.JIT_OPTION_CACHE_PAGE_SIZE 17 JIT_OPTION_PRE_COMPILE = C.JIT_OPTION_PRE_COMPILE 18 JIT_OPTION_DONT_FOLD = C.JIT_OPTION_DONT_FOLD 19 JIT_OPTION_POSITION_INDEPENDENT = C.JIT_OPTION_POSITION_INDEPENDENT 20 JIT_OPTION_CACHE_MAX_PAGE_FACTOR = C.JIT_OPTION_CACHE_MAX_PAGE_FACTOR 21 ) 22 23 type Context struct { 24 c C.jit_context_t 25 crosscall2 *Function 26 cgo_wait_runtime_init_done *Function 27 } 28 29 func toContext(c C.jit_context_t) *Context { 30 return &Context{c: c} 31 } 32 33 func CreateContext() *Context { 34 ctx := toContext(C.jit_context_create()) 35 ctx.crosscall2 = ctx.createCrossCall2() 36 ctx.cgo_wait_runtime_init_done = ctx.createCgoWaitRuntimeInitDone() 37 return ctx 38 } 39 40 func (c *Context) Destroy() { 41 C.jit_context_destroy(c.c) 42 } 43 44 func (c *Context) BuildStart() { 45 C.jit_context_build_start(c.c) 46 } 47 48 func (c *Context) BuildEnd() { 49 C.jit_context_build_end(c.c) 50 } 51 52 func (c *Context) CreateFunction(signature *Type) *Function { 53 fn := toFunction(C.jit_function_create(c.c, signature.c)) 54 fn.crosscall2 = c.crosscall2 55 fn.cgo_wait_runtime_init_done = c.cgo_wait_runtime_init_done 56 return fn 57 } 58 59 func (c *Context) CreateNestedFunction(signature *Type, parent *Function) *Function { 60 return toFunction(C.jit_function_create_nested(c.c, signature.c, parent.c)) 61 } 62 63 func (c *Context) createCrossCall2() *Function { 64 sig := CreateSignature([]*Type{TypeVoidPtr, TypeVoidPtr, TypeInt, TypeInt}, nil) 65 fn := c.CreateFunction(sig) 66 defer sig.Free() 67 C.jit_function_setup_entry(fn.c, C.get_crosscall2addr()) 68 return fn 69 } 70 71 func (c *Context) createCgoWaitRuntimeInitDone() *Function { 72 sig := CreateSignature(nil, TypeInt) 73 fn := c.CreateFunction(sig) 74 defer sig.Free() 75 C.jit_function_setup_entry(fn.c, C.get_cgo_wait_runtime_init_done_addr()) 76 return fn 77 }