From d6c527f662bc5d2d2246c92672883578a6aee236 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Sun, 24 Nov 2024 22:45:25 +0800 Subject: [PATCH 1/7] cgo: support macros --- _demo/cgomacro/cgomacro.go | 31 ++++++++++ cl/compile.go | 60 ++++++++++++-------- cl/import.go | 5 +- cl/instr.go | 8 --- internal/build/cgo.go | 113 +++++++++++++++++++++++++++---------- 5 files changed, 153 insertions(+), 64 deletions(-) create mode 100644 _demo/cgomacro/cgomacro.go diff --git a/_demo/cgomacro/cgomacro.go b/_demo/cgomacro/cgomacro.go new file mode 100644 index 000000000..21bb87373 --- /dev/null +++ b/_demo/cgomacro/cgomacro.go @@ -0,0 +1,31 @@ +package main + +/* +#cgo pkg-config: python3-embed +#include +#include + +void test_stdout() { + printf("stdout ptr: %p\n", stdout); + fputs("outputs to stdout\n", stdout); +} +*/ +import "C" +import ( + "unsafe" + + "github.com/goplus/llgo/c" +) + +func main() { + C.test_stdout() + C.fputs((*C.char)(unsafe.Pointer(c.Str("hello\n"))), C.stdout) + C.Py_Initialize() + defer C.Py_Finalize() + C.PyObject_Print(C.Py_True, C.stdout, 0) + C.fputs((*C.char)(unsafe.Pointer(c.Str("\n"))), C.stdout) + C.PyObject_Print(C.Py_False, C.stdout, 0) + C.fputs((*C.char)(unsafe.Pointer(c.Str("\n"))), C.stdout) + C.PyObject_Print(C.Py_None, C.stdout, 0) + C.fputs((*C.char)(unsafe.Pointer(c.Str("\n"))), C.stdout) +} diff --git a/cl/compile.go b/cl/compile.go index f4949b745..8c9eca464 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -189,8 +189,17 @@ var ( argvTy = types.NewPointer(types.NewPointer(types.Typ[types.Int8])) ) -func isCgoCfunc(f *ssa.Function) bool { - return strings.HasPrefix(f.Name(), "_Cfunc_") +func isCgoCfuncOrCmacro(f *ssa.Function) bool { + name := f.Name() + return isCgoCfunc(name) || isCgoCmacro(name) +} + +func isCgoCfunc(name string) bool { + return strings.HasPrefix(name, "_Cfunc_") +} + +func isCgoCmacro(name string) bool { + return strings.HasPrefix(name, "_Cmacro_") } func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Function, llssa.PyObjRef, int) { @@ -246,10 +255,11 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun fn.Inline(llssa.NoInline) } } + isCgo := isCgoCfuncOrCmacro(f) if nblk := len(f.Blocks); nblk > 0 { p.cgoCalled = false p.cgoArgs = nil - if isCgoCfunc(f) { + if isCgo { fn.MakeBlocks(1) } else { fn.MakeBlocks(nblk) // to set fn.HasBody() = true @@ -278,7 +288,7 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun } p.bvals = make(map[ssa.Value]llssa.Expr) off := make([]int, len(f.Blocks)) - if isCgoCfunc(f) { + if isCgo { p.cgoArgs = make([]llssa.Expr, len(f.Params)) for i, param := range f.Params { p.cgoArgs[i] = p.compileValue(b, param) @@ -295,7 +305,7 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun doMainInit := (i == 0 && name == "main") doModInit := (i == 1 && isInit) p.compileBlock(b, block, off[i], doMainInit, doModInit) - if isCgoCfunc(f) { + if isCgo { // just process first block for performance break } @@ -409,36 +419,46 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, do if funcs, ok := p.cgoFuncs[fname]; ok { cgoFuncs = funcs } + fnName := block.Parent().Name() cgoReturned := false + isCgoCfunc := isCgoCfunc(fnName) + isCgoCmacro := isCgoCmacro(fnName) for i, instr := range instrs { if i == 1 && doModInit && p.state == pkgInPatch { // in patch package but no pkgFNoOldInit initFnNameOld := initFnNameOfHasPatch(p.fn.Name()) fnOld := pkg.NewFunc(initFnNameOld, llssa.NoArgsNoRet, llssa.InC) b.Call(fnOld.Expr) } - if isCgoCfunc(block.Parent()) { + if isCgoCfunc || isCgoCmacro { switch instr := instr.(type) { case *ssa.Alloc: // return value allocation p.compileInstr(b, instr) case *ssa.UnOp: // load cgo function pointer - if instr.Op == token.MUL && strings.HasPrefix(instr.X.Name(), "_cgo_") { - cgoFuncs = append(cgoFuncs, instr.X.Name()) + varName := instr.X.Name() + if instr.Op == token.MUL && strings.HasPrefix(varName, "_cgo_") { + cgoFuncs = append(cgoFuncs, varName) p.cgoFuncs[fname] = cgoFuncs p.compileInstr(b, instr) } case *ssa.Call: - // call c function - p.compileInstr(b, instr) - p.cgoCalled = true + if isCgoCmacro { + p.cgoRet = p.compileValue(b, instr.Call.Args[0]) + p.cgoCalled = true + } else { + // call c function + p.compileInstr(b, instr) + p.cgoCalled = true + } case *ssa.Return: // return cgo function result - if len(instr.Results) > 0 { - b.Return(p.cgoRet) - } else { - b.Return(llssa.Nil) + if isCgoCmacro { + ty := p.type_(instr.Results[0].Type(), llssa.InGo) + p.cgoRet.Type = p.prog.Pointer(ty) + p.cgoRet = b.Load(p.cgoRet) } + b.Return(p.cgoRet) cgoReturned = true } } else { @@ -446,18 +466,14 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, do } } // is cgo cfunc but not return yet, some funcs has multiple blocks - if isCgoCfunc(block.Parent()) && !cgoReturned { + if (isCgoCfunc || isCgoCmacro) && !cgoReturned { if !p.cgoCalled { panic("cgo cfunc not called") } for _, block := range block.Parent().Blocks { for _, instr := range block.Instrs { - if instr, ok := instr.(*ssa.Return); ok { - if len(instr.Results) > 0 { - b.Return(p.cgoRet) - } else { - b.Return(llssa.Nil) - } + if _, ok := instr.(*ssa.Return); ok { + b.Return(p.cgoRet) goto end } } diff --git a/cl/import.go b/cl/import.go index fd2418499..8c1f7d3b2 100644 --- a/cl/import.go +++ b/cl/import.go @@ -421,7 +421,6 @@ const ( llgoCgoCMalloc = llgoCgoBase + 0x5 llgoCgoCheckPointer = llgoCgoBase + 0x6 llgoCgoCgocall = llgoCgoBase + 0x7 - llgoCgoUse = llgoCgoBase + 0x8 llgoAtomicOpLast = llgoAtomicOpBase + int(llssa.OpUMin) ) @@ -438,7 +437,7 @@ func (p *context) funcName(fn *ssa.Function, ignore bool) (*types.Package, strin if checkCgo(fname) { return nil, fname, llgoInstr } - if isCgoCfunc(fn) { + if isCgoCfuncOrCmacro(fn) { if _, ok := llgoInstrs[fname]; ok { return nil, fname, llgoInstr } @@ -450,7 +449,7 @@ func (p *context) funcName(fn *ssa.Function, ignore bool) (*types.Package, strin } p.ensureLoaded(pkg) orgName = funcName(pkg, fn, false) - if ignore && ignoreName(orgName) || checkCgo(fn.Name()) { + if ignore && ignoreName(orgName) { return nil, orgName, ignoredFunc } } diff --git a/cl/instr.go b/cl/instr.go index 6ae03ea70..e9c9effa7 100644 --- a/cl/instr.go +++ b/cl/instr.go @@ -130,11 +130,6 @@ func (p *context) cgoCgocall(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) return p.cgoRet } -// func _Cgo_use(v any) -func (p *context) cgoUse(b llssa.Builder, args []ssa.Value) { - // don't need to do anything -} - // ----------------------------------------------------------------------------- // func index(arr *T, idx int) T @@ -324,7 +319,6 @@ var llgoInstrs = map[string]int{ "_Cfunc__CMalloc": llgoCgoCMalloc, "_cgoCheckPointer": llgoCgoCheckPointer, "_cgo_runtime_cgocall": llgoCgoCgocall, - "_Cgo_use": llgoCgoUse, } // funcOf returns a function by name and set ftype = goFunc, cFunc, etc. @@ -468,8 +462,6 @@ func (p *context) call(b llssa.Builder, act llssa.DoAction, call *ssa.CallCommon p.cgoCheckPointer(b, args) case llgoCgoCgocall: p.cgoCgocall(b, args) - case llgoCgoUse: - p.cgoUse(b, args) case llgoAdvance: ret = p.advance(b, args) case llgoIndex: diff --git a/internal/build/cgo.go b/internal/build/cgo.go index 9615a73b8..ddbeb376c 100644 --- a/internal/build/cgo.go +++ b/internal/build/cgo.go @@ -88,18 +88,18 @@ func buildCgo(ctx *context, pkg *aPackage, files []*ast.File, externs map[string cgoLdflags = append(cgoLdflags, linkFile) }, verbose) } - re := regexp.MustCompile(`^(_cgo_[^_]+_Cfunc_)(.*)$`) - cgoFuncs := make(map[string]string) + re := regexp.MustCompile(`^(_cgo_[^_]+_(Cfunc|Cmacro)_)(.*)$`) + cgoSymbols := make(map[string]string) mallocFix := false - for _, funcs := range externs { - for _, funcName := range funcs { - if m := re.FindStringSubmatch(funcName); len(m) > 0 { - cgoFuncs[funcName] = m[2] + for _, symbols := range externs { + for _, symbolName := range symbols { + if m := re.FindStringSubmatch(symbolName); len(m) > 0 { + cgoSymbols[symbolName] = m[3] + pkgPrefix := m[1] // fix missing _cgo_9113e32b6599_Cfunc__Cmalloc - if !mallocFix { - pkgPrefix := m[1] + if !mallocFix && m[2] == "Cfunc" { mallocName := pkgPrefix + "_Cmalloc" - cgoFuncs[mallocName] = "_Cmalloc" + cgoSymbols[mallocName] = "_Cmalloc" mallocFix = true } } @@ -113,7 +113,10 @@ func buildCgo(ctx *context, pkg *aPackage, files []*ast.File, externs map[string tmpName := tmpFile.Name() defer os.Remove(tmpName) code := cgoHeader + "\n\n" + preamble.src - externDecls := genExternDeclsByClang(code, cflags, cgoFuncs) + externDecls, err := genExternDeclsByClang(code, cflags, cgoSymbols) + if err != nil { + return nil, err + } if err = os.WriteFile(tmpName, []byte(code+"\n\n"+externDecls), 0644); err != nil { return nil, err } @@ -134,42 +137,90 @@ type clangASTNode struct { Inner []clangASTNode `json:"inner,omitempty"` } -func genExternDeclsByClang(src string, cflags []string, cgoFuncs map[string]string) string { +func genExternDeclsByClang(src string, cflags []string, cgoSymbols map[string]string) (string, error) { tmpSrc, err := os.CreateTemp("", "cgo-src-*.c") if err != nil { - return "" + return "", err } defer os.Remove(tmpSrc.Name()) if err := os.WriteFile(tmpSrc.Name(), []byte(src), 0644); err != nil { - return "" + return "", err } - args := append([]string{"-Xclang", "-ast-dump=json", "-fsyntax-only"}, cflags...) - args = append(args, tmpSrc.Name()) - cmd := exec.Command("clang", args...) - output, err := cmd.Output() - if err != nil { - return "" + symbolNames := make(map[string]bool) + if err := getFuncNames(tmpSrc.Name(), cflags, symbolNames); err != nil { + return "", err } - var astRoot clangASTNode - if err := json.Unmarshal(output, &astRoot); err != nil { - return "" + macroNames := make(map[string]bool) + if err := getMacroNames(tmpSrc.Name(), cflags, macroNames); err != nil { + return "", err } - funcNames := make(map[string]bool) - extractFuncNames(&astRoot, funcNames) - b := strings.Builder{} var toRemove []string - for cgoFunc, funcName := range cgoFuncs { - if funcNames[funcName] { - b.WriteString(fmt.Sprintf("void* %s = (void*)%s;\n", cgoFunc, funcName)) - toRemove = append(toRemove, cgoFunc) + for cgoName, symbolName := range cgoSymbols { + if symbolNames[symbolName] { + b.WriteString(fmt.Sprintf("void* %s = (void*)%s;\n", cgoName, symbolName)) + toRemove = append(toRemove, cgoName) + } else if macroNames[symbolName] { + /* template: + typeof(stdout) _cgo_1574167f3838_Cmacro_stdout; + + __attribute__((constructor)) + static void _init__cgo_1574167f3838_Cmacro_stdout() { + _cgo_1574167f3838_Cmacro_stdout = stdout; + }*/ + b.WriteString(fmt.Sprintf(` +typeof(%s) %s; + +__attribute__((constructor)) +static void _init_%s() { + %s = %s; +} +`, symbolName, cgoName, cgoName, cgoName, symbolName)) + toRemove = append(toRemove, cgoName) } } for _, funcName := range toRemove { - delete(cgoFuncs, funcName) + delete(cgoSymbols, funcName) } - return b.String() + return b.String(), nil +} + +func getMacroNames(file string, cflags []string, macroNames map[string]bool) error { + args := append([]string{"-dM", "-E"}, cflags...) + args = append(args, file) + cmd := exec.Command("clang", args...) + output, err := cmd.Output() + if err != nil { + return err + } + for _, line := range strings.Split(string(output), "\n") { + if strings.HasPrefix(line, "#define ") { + define := strings.TrimPrefix(line, "#define ") + parts := strings.SplitN(define, " ", 2) + if len(parts) > 1 { + macroNames[parts[0]] = true + } + } + } + return nil +} + +func getFuncNames(file string, cflags []string, symbolNames map[string]bool) error { + args := append([]string{"-Xclang", "-ast-dump=json", "-fsyntax-only"}, cflags...) + args = append(args, file) + cmd := exec.Command("clang", args...) + output, err := cmd.Output() + if err != nil { + return err + } + var astRoot clangASTNode + if err := json.Unmarshal(output, &astRoot); err != nil { + return err + } + + extractFuncNames(&astRoot, symbolNames) + return nil } func extractFuncNames(node *clangASTNode, funcNames map[string]bool) { From 39dc68fa4e17971c116a59f40ef831a29d90e945 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Tue, 26 Nov 2024 09:20:06 +0800 Subject: [PATCH 2/7] demo/cgofull: add multi cgo module and macro calling --- _demo/cgofull/cgofull.go | 17 ++++++++++++++++- _demo/cgofull/pymod1/pymod1.go | 11 +++++++++++ _demo/cgofull/pymod2/pymod2.go | 11 +++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 _demo/cgofull/pymod1/pymod1.go create mode 100644 _demo/cgofull/pymod2/pymod2.go diff --git a/_demo/cgofull/cgofull.go b/_demo/cgofull/cgofull.go index 82c5e0f6f..038c9ef4c 100644 --- a/_demo/cgofull/cgofull.go +++ b/_demo/cgofull/cgofull.go @@ -6,7 +6,9 @@ package main #cgo windows,amd64 CFLAGS: -D_WIN64 #cgo linux,amd64 CFLAGS: -D_LINUX64 #cgo !windows,amd64 CFLAGS: -D_UNIX64 +#cgo pkg-config: python3-embed #include +#include #include "foo.h" typedef struct { int a; @@ -71,9 +73,18 @@ static void test_macros() { printf("UNIX64 is defined\n"); #endif } + +#define MY_VERSION "1.0.0" +#define MY_CODE 0x12345678 */ import "C" -import "fmt" +import ( + "fmt" + "unsafe" + + "github.com/goplus/llgo/_demo/cgofull/pymod1" + "github.com/goplus/llgo/_demo/cgofull/pymod2" +) func main() { runPy() @@ -86,10 +97,14 @@ func main() { if r != 35 { panic("test_structs failed") } + fmt.Println(C.MY_VERSION) + fmt.Println(int(C.MY_CODE)) } func runPy() { Initialize() defer Finalize() Run("print('Hello, Python!')") + C.PyObject_Print((*C.PyObject)(unsafe.Pointer(pymod1.Float(1.23))), C.stderr, 0) + C.PyObject_Print((*C.PyObject)(unsafe.Pointer(pymod2.Long(123))), C.stdout, 0) } diff --git a/_demo/cgofull/pymod1/pymod1.go b/_demo/cgofull/pymod1/pymod1.go new file mode 100644 index 000000000..480b1b4a5 --- /dev/null +++ b/_demo/cgofull/pymod1/pymod1.go @@ -0,0 +1,11 @@ +package pymod1 + +/* +#cgo pkg-config: python3-embed +#include +*/ +import "C" + +func Float(f float64) *C.PyObject { + return C.PyFloat_FromDouble(C.double(f)) +} diff --git a/_demo/cgofull/pymod2/pymod2.go b/_demo/cgofull/pymod2/pymod2.go new file mode 100644 index 000000000..8cc7dd27a --- /dev/null +++ b/_demo/cgofull/pymod2/pymod2.go @@ -0,0 +1,11 @@ +package pymod2 + +/* +#cgo pkg-config: python3-embed +#include +*/ +import "C" + +func Long(l int64) *C.PyObject { + return C.PyLong_FromLongLong(C.longlong(l)) +} From c9f436cc473b47a63689681288118b8473ab500c Mon Sep 17 00:00:00 2001 From: Li Jie Date: Tue, 26 Nov 2024 11:23:46 +0800 Subject: [PATCH 3/7] cgo: full test cgo: test returning void --- cl/_testgo/cgofull/bar.go | 16 ++++ cl/_testgo/cgofull/cgofull.go | 115 ++++++++++++++++++++++++++++ cl/_testgo/cgofull/foo.c | 6 ++ cl/_testgo/cgofull/foo.go | 16 ++++ cl/_testgo/cgofull/foo.h | 7 ++ cl/_testgo/cgofull/out.ll | 1 + cl/_testgo/cgofull/py.go | 24 ++++++ cl/_testgo/cgofull/pymod1/pymod1.go | 11 +++ cl/_testgo/cgofull/pymod2/pymod2.go | 11 +++ 9 files changed, 207 insertions(+) create mode 100644 cl/_testgo/cgofull/bar.go create mode 100644 cl/_testgo/cgofull/cgofull.go create mode 100644 cl/_testgo/cgofull/foo.c create mode 100644 cl/_testgo/cgofull/foo.go create mode 100644 cl/_testgo/cgofull/foo.h create mode 100644 cl/_testgo/cgofull/out.ll create mode 100644 cl/_testgo/cgofull/py.go create mode 100644 cl/_testgo/cgofull/pymod1/pymod1.go create mode 100644 cl/_testgo/cgofull/pymod2/pymod2.go diff --git a/cl/_testgo/cgofull/bar.go b/cl/_testgo/cgofull/bar.go new file mode 100644 index 000000000..08e6830f9 --- /dev/null +++ b/cl/_testgo/cgofull/bar.go @@ -0,0 +1,16 @@ +package main + +/* +#cgo CFLAGS: -DBAR +#include +#include "foo.h" +static void foo(Foo* f) { + printf("foo in bar: %d\n", f->a); +} +*/ +import "C" + +func Bar(f *C.Foo) { + C.print_foo(f) + C.foo(f) +} diff --git a/cl/_testgo/cgofull/cgofull.go b/cl/_testgo/cgofull/cgofull.go new file mode 100644 index 000000000..476b01674 --- /dev/null +++ b/cl/_testgo/cgofull/cgofull.go @@ -0,0 +1,115 @@ +package main + +/* +#cgo windows,!amd64 CFLAGS: -D_WIN32 +#cgo !windows CFLAGS: -D_POSIX +#cgo windows,amd64 CFLAGS: -D_WIN64 +#cgo linux,amd64 CFLAGS: -D_LINUX64 +#cgo !windows,amd64 CFLAGS: -D_UNIX64 +#cgo pkg-config: python3-embed +#include +#include +#include "foo.h" +typedef struct { + int a; +} s4; + +typedef struct { + int a; + int b; +} s8; + +typedef struct { + int a; + int b; + int c; +} s12; + +typedef struct { + int a; + int b; + int c; + int d; +} s16; + +typedef struct { + int a; + int b; + int c; + int d; + int e; +} s20; + +static int test_structs(s4* s4, s8* s8, s12* s12, s16* s16, s20* s20) { + printf("s4.a: %d\n", s4->a); + printf("s8.a: %d, s8.b: %d\n", s8->a, s8->b); + printf("s12.a: %d, s12.b: %d, s12.c: %d\n", s12->a, s12->b, s12->c); + printf("s16.a: %d, s16.b: %d, s16.c: %d, s16.d: %d\n", s16->a, s16->b, s16->c, s16->d); + printf("s20.a: %d, s20.b: %d, s20.c: %d, s20.d: %d, s20.e: %d\n", s20->a, s20->b, s20->c, s20->d, s20->e); + + return s4->a + s8->a + s8->b + s12->a + s12->b + s12->c + s16->a + s16->b + s16->c + s16->d + s20->a + s20->b + s20->c + s20->d + s20->e; +} + +static void test_macros() { +#ifdef FOO + printf("FOO is defined\n"); +#endif +#ifdef BAR + printf("BAR is defined\n"); +#endif +#ifdef _WIN32 + printf("WIN32 is defined\n"); +#endif +#ifdef _POSIX + printf("POSIX is defined\n"); +#endif +#ifdef _WIN64 + printf("WIN64 is defined\n"); +#endif +#ifdef _LINUX64 + printf("LINUX64 is defined\n"); +#endif +#ifdef _UNIX64 + printf("UNIX64 is defined\n"); +#endif +} + +#define MY_VERSION "1.0.0" +#define MY_CODE 0x12345678 + +void test_void() { + printf("test_void\n"); +} +*/ +import "C" +import ( + "fmt" + "unsafe" + + "github.com/goplus/llgo/_demo/cgofull/pymod1" + "github.com/goplus/llgo/_demo/cgofull/pymod2" +) + +func main() { + runPy() + f := &C.Foo{a: 1} + Foo(f) + Bar(f) + C.test_macros() + r := C.test_structs(&C.s4{a: 1}, &C.s8{a: 1, b: 2}, &C.s12{a: 1, b: 2, c: 3}, &C.s16{a: 1, b: 2, c: 3, d: 4}, &C.s20{a: 1, b: 2, c: 3, d: 4, e: 5}) + fmt.Println(r) + if r != 35 { + panic("test_structs failed") + } + fmt.Println(C.MY_VERSION) + fmt.Println(int(C.MY_CODE)) + C.test_void() +} + +func runPy() { + Initialize() + defer Finalize() + Run("print('Hello, Python!')") + C.PyObject_Print((*C.PyObject)(unsafe.Pointer(pymod1.Float(1.23))), C.stderr, 0) + C.PyObject_Print((*C.PyObject)(unsafe.Pointer(pymod2.Long(123))), C.stdout, 0) +} diff --git a/cl/_testgo/cgofull/foo.c b/cl/_testgo/cgofull/foo.c new file mode 100644 index 000000000..b3f07e049 --- /dev/null +++ b/cl/_testgo/cgofull/foo.c @@ -0,0 +1,6 @@ +#include +#include "foo.h" + +void print_foo(Foo* f) { + printf("print_foo: %d\n", f->a); +} diff --git a/cl/_testgo/cgofull/foo.go b/cl/_testgo/cgofull/foo.go new file mode 100644 index 000000000..1d6aa3b4b --- /dev/null +++ b/cl/_testgo/cgofull/foo.go @@ -0,0 +1,16 @@ +package main + +/* +#cgo CFLAGS: -DFOO +#include +#include "foo.h" +static void foo(Foo* f) { + printf("foo in bar: %d\n", f->a); +} +*/ +import "C" + +func Foo(f *C.Foo) { + C.print_foo(f) + C.foo(f) +} diff --git a/cl/_testgo/cgofull/foo.h b/cl/_testgo/cgofull/foo.h new file mode 100644 index 000000000..f714fec6d --- /dev/null +++ b/cl/_testgo/cgofull/foo.h @@ -0,0 +1,7 @@ +#pragma once + +typedef struct { + int a; +} Foo; + +extern void print_foo(Foo* f); diff --git a/cl/_testgo/cgofull/out.ll b/cl/_testgo/cgofull/out.ll new file mode 100644 index 000000000..1c8a0e797 --- /dev/null +++ b/cl/_testgo/cgofull/out.ll @@ -0,0 +1 @@ +; \ No newline at end of file diff --git a/cl/_testgo/cgofull/py.go b/cl/_testgo/cgofull/py.go new file mode 100644 index 000000000..509d88ca2 --- /dev/null +++ b/cl/_testgo/cgofull/py.go @@ -0,0 +1,24 @@ +package main + +/* +#cgo pkg-config: python3-embed +#include +*/ +import "C" +import "fmt" + +func Initialize() { + C.Py_Initialize() +} + +func Finalize() { + C.Py_Finalize() +} + +func Run(code string) error { + if C.PyRun_SimpleString(C.CString(code)) != 0 { + C.PyErr_Print() + return fmt.Errorf("failed to run code") + } + return nil +} diff --git a/cl/_testgo/cgofull/pymod1/pymod1.go b/cl/_testgo/cgofull/pymod1/pymod1.go new file mode 100644 index 000000000..480b1b4a5 --- /dev/null +++ b/cl/_testgo/cgofull/pymod1/pymod1.go @@ -0,0 +1,11 @@ +package pymod1 + +/* +#cgo pkg-config: python3-embed +#include +*/ +import "C" + +func Float(f float64) *C.PyObject { + return C.PyFloat_FromDouble(C.double(f)) +} diff --git a/cl/_testgo/cgofull/pymod2/pymod2.go b/cl/_testgo/cgofull/pymod2/pymod2.go new file mode 100644 index 000000000..8cc7dd27a --- /dev/null +++ b/cl/_testgo/cgofull/pymod2/pymod2.go @@ -0,0 +1,11 @@ +package pymod2 + +/* +#cgo pkg-config: python3-embed +#include +*/ +import "C" + +func Long(l int64) *C.PyObject { + return C.PyLong_FromLongLong(C.longlong(l)) +} From 5380ffa47115629411c969b735171fffb9b2b62b Mon Sep 17 00:00:00 2001 From: Li Jie Date: Tue, 26 Nov 2024 21:07:37 +0800 Subject: [PATCH 4/7] cgo: ignore funcs won't be compiled --- cl/import.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/cl/import.go b/cl/import.go index 8c1f7d3b2..b6877fa56 100644 --- a/cl/import.go +++ b/cl/import.go @@ -366,6 +366,17 @@ func checkCgo(fnName string) bool { (fnName[4] == '_' || strings.HasPrefix(fnName[4:], "Check")) } +var cgoIgnoredNames = map[string]none{ + "_Cgo_ptr": {}, + "_Cgo_use": {}, + "_cgoCheckResult": {}, +} + +func cgoIgnored(fnName string) bool { + _, ok := cgoIgnoredNames[fnName] + return ok +} + const ( ignoredFunc = iota goFunc = int(llssa.InGo) @@ -434,7 +445,7 @@ func (p *context) funcName(fn *ssa.Function, ignore bool) (*types.Package, strin orgName = funcName(pkg, origin, true) } else { fname := fn.Name() - if checkCgo(fname) { + if checkCgo(fname) && !cgoIgnored(fname) { return nil, fname, llgoInstr } if isCgoCfuncOrCmacro(fn) { From 90763de93cc784693e9aa17268dac825730ea0af Mon Sep 17 00:00:00 2001 From: Li Jie Date: Tue, 26 Nov 2024 22:34:19 +0800 Subject: [PATCH 5/7] cgo: supports c/go callback funcs --- cl/_testgo/cgofull/cgofull.go | 36 +++++++++++++++++++- cl/_testgo/cgofull/foo.c | 8 ++++- cl/_testgo/out.ll | 64 ----------------------------------- cl/_testrt/strlen/out.ll | 11 ++++++ cl/_testrt/struct/out.ll | 10 ++++++ cl/_testrt/typalias/out.ll | 11 ++++++ cl/compile.go | 38 +++++++++++++++++++-- cl/import.go | 2 +- internal/build/build.go | 2 +- internal/build/cgo.go | 51 ++++++++++++++++++++-------- ssa/decl.go | 4 +++ ssa/expr.go | 9 +++++ 12 files changed, 162 insertions(+), 84 deletions(-) delete mode 100644 cl/_testgo/out.ll diff --git a/cl/_testgo/cgofull/cgofull.go b/cl/_testgo/cgofull/cgofull.go index 476b01674..0380d127f 100644 --- a/cl/_testgo/cgofull/cgofull.go +++ b/cl/_testgo/cgofull/cgofull.go @@ -77,9 +77,26 @@ static void test_macros() { #define MY_VERSION "1.0.0" #define MY_CODE 0x12345678 -void test_void() { +static void test_void() { printf("test_void\n"); } + +typedef int (*Cb)(int); + +extern int go_callback(int); + +extern int c_callback(int i); + +static void test_callback(Cb cb) { + printf("test_callback, cb: %p, go_callback: %p, c_callback: %p\n", cb, go_callback, c_callback); + printf("test_callback, *cb: %p, *go_callback: %p, *c_callback: %p\n", *(void**)cb, *(void**)(go_callback), *(void**)(c_callback)); + printf("cb result: %d\n", cb(123)); + printf("done\n"); +} + +static void run_callback() { + test_callback(c_callback); +} */ import "C" import ( @@ -90,6 +107,11 @@ import ( "github.com/goplus/llgo/_demo/cgofull/pymod2" ) +//export go_callback +func go_callback(i C.int) C.int { + return i + 1 +} + func main() { runPy() f := &C.Foo{a: 1} @@ -104,6 +126,16 @@ func main() { fmt.Println(C.MY_VERSION) fmt.Println(int(C.MY_CODE)) C.test_void() + + println("call run_callback") + C.run_callback() + + // test _Cgo_ptr and _cgoCheckResult + println("call with go_callback") + C.test_callback((C.Cb)(C.go_callback)) + + println("call with c_callback") + C.test_callback((C.Cb)(C.c_callback)) } func runPy() { @@ -112,4 +144,6 @@ func runPy() { Run("print('Hello, Python!')") C.PyObject_Print((*C.PyObject)(unsafe.Pointer(pymod1.Float(1.23))), C.stderr, 0) C.PyObject_Print((*C.PyObject)(unsafe.Pointer(pymod2.Long(123))), C.stdout, 0) + // test _Cgo_use + C.PyObject_Print((*C.PyObject)(unsafe.Pointer(C.PyComplex_FromDoubles(C.double(1.23), C.double(4.56)))), C.stdout, 0) } diff --git a/cl/_testgo/cgofull/foo.c b/cl/_testgo/cgofull/foo.c index b3f07e049..aaa25c6a1 100644 --- a/cl/_testgo/cgofull/foo.c +++ b/cl/_testgo/cgofull/foo.c @@ -1,6 +1,12 @@ #include #include "foo.h" -void print_foo(Foo* f) { +void print_foo(Foo *f) +{ printf("print_foo: %d\n", f->a); } + +int c_callback(int i) +{ + return i + 1; +} \ No newline at end of file diff --git a/cl/_testgo/out.ll b/cl/_testgo/out.ll deleted file mode 100644 index 360888072..000000000 --- a/cl/_testgo/out.ll +++ /dev/null @@ -1,64 +0,0 @@ -; ModuleID = 'main' -source_filename = "main" - -%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 } - -@"main.init$guard" = global i1 false, align 1 -@0 = private unnamed_addr constant [5 x i8] c"hello", align 1 -@__llgo_argc = global i32 0, align 4 -@__llgo_argv = global ptr null, align 8 - -define i64 @main.Foo(%"github.com/goplus/llgo/internal/runtime.String" %0) { -_llgo_0: - %1 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %0, 1 - ret i64 %1 -} - -define void @main.Test() { -_llgo_0: - br label %_llgo_3 - -_llgo_1: ; preds = %_llgo_3 - %0 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 - %1 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %0, i32 0, i32 0 - store ptr @0, ptr %1, align 8 - %2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %0, i32 0, i32 1 - store i64 5, ptr %2, align 4 - %3 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %0, align 8 - %4 = call i64 @main.Foo(%"github.com/goplus/llgo/internal/runtime.String" %3) - %5 = add i64 %6, 1 - br label %_llgo_3 - -_llgo_2: ; preds = %_llgo_3 - ret void - -_llgo_3: ; preds = %_llgo_1, %_llgo_0 - %6 = phi i64 [ 0, %_llgo_0 ], [ %5, %_llgo_1 ] - %7 = icmp slt i64 %6, 1000000 - br i1 %7, label %_llgo_1, label %_llgo_2 -} - -define void @main.init() { -_llgo_0: - %0 = load i1, ptr @"main.init$guard", align 1 - br i1 %0, label %_llgo_2, label %_llgo_1 - -_llgo_1: ; preds = %_llgo_0 - store i1 true, ptr @"main.init$guard", align 1 - br label %_llgo_2 - -_llgo_2: ; preds = %_llgo_1, %_llgo_0 - ret void -} - -define i32 @main(i32 %0, ptr %1) { -_llgo_0: - store i32 %0, ptr @__llgo_argc, align 4 - store ptr %1, ptr @__llgo_argv, align 8 - call void @"github.com/goplus/llgo/internal/runtime.init"() - call void @main.init() - call void @main.Test() - ret i32 0 -} - -declare void @"github.com/goplus/llgo/internal/runtime.init"() diff --git a/cl/_testrt/strlen/out.ll b/cl/_testrt/strlen/out.ll index bd201f264..b6339870f 100644 --- a/cl/_testrt/strlen/out.ll +++ b/cl/_testrt/strlen/out.ll @@ -1,12 +1,23 @@ ; ModuleID = 'main' source_filename = "main" +%"github.com/goplus/llgo/internal/runtime.eface" = type { ptr, ptr } + @"github.com/goplus/llgo/internal/runtime.cgoAlwaysFalse" = external global i1, align 1 @main.format = global [10 x i8] zeroinitializer, align 1 @"main.init$guard" = global i1 false, align 1 @__llgo_argc = global i32 0, align 4 @__llgo_argv = global ptr null, align 8 +define ptr @main._Cgo_ptr(ptr %0) { +_llgo_0: + ret ptr %0 +} + +declare void @runtime.cgoUse(%"github.com/goplus/llgo/internal/runtime.eface") + +declare void @runtime.cgoCheckResult(%"github.com/goplus/llgo/internal/runtime.eface") + define void @main.init() { _llgo_0: %0 = load i1, ptr @"main.init$guard", align 1 diff --git a/cl/_testrt/struct/out.ll b/cl/_testrt/struct/out.ll index bdf1e2488..ec0606634 100644 --- a/cl/_testrt/struct/out.ll +++ b/cl/_testrt/struct/out.ll @@ -2,6 +2,7 @@ source_filename = "main" %main.Foo = type { i32, i1 } +%"github.com/goplus/llgo/internal/runtime.eface" = type { ptr, ptr } @"github.com/goplus/llgo/internal/runtime.cgoAlwaysFalse" = external global i1, align 1 @main.format = global [10 x i8] zeroinitializer, align 1 @@ -35,6 +36,15 @@ _llgo_0: ret void } +define ptr @main._Cgo_ptr(ptr %0) { +_llgo_0: + ret ptr %0 +} + +declare void @runtime.cgoUse(%"github.com/goplus/llgo/internal/runtime.eface") + +declare void @runtime.cgoCheckResult(%"github.com/goplus/llgo/internal/runtime.eface") + define void @main.init() { _llgo_0: %0 = load i1, ptr @"main.init$guard", align 1 diff --git a/cl/_testrt/typalias/out.ll b/cl/_testrt/typalias/out.ll index beb826c5f..54c4b87ef 100644 --- a/cl/_testrt/typalias/out.ll +++ b/cl/_testrt/typalias/out.ll @@ -1,6 +1,8 @@ ; ModuleID = 'main' source_filename = "main" +%"github.com/goplus/llgo/internal/runtime.eface" = type { ptr, ptr } + @"github.com/goplus/llgo/internal/runtime.cgoAlwaysFalse" = external global i1, align 1 @main.format = global [10 x i8] zeroinitializer, align 1 @"main.init$guard" = global i1 false, align 1 @@ -23,6 +25,15 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0 ret void } +define ptr @main._Cgo_ptr(ptr %0) { +_llgo_0: + ret ptr %0 +} + +declare void @runtime.cgoUse(%"github.com/goplus/llgo/internal/runtime.eface") + +declare void @runtime.cgoCheckResult(%"github.com/goplus/llgo/internal/runtime.eface") + define void @main.init() { _llgo_0: %0 = load i1, ptr @"main.init$guard", align 1 diff --git a/cl/compile.go b/cl/compile.go index 8c9eca464..eb4f1fcbf 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -189,7 +189,7 @@ var ( argvTy = types.NewPointer(types.NewPointer(types.Typ[types.Int8])) ) -func isCgoCfuncOrCmacro(f *ssa.Function) bool { +func isCgoExternSymbol(f *ssa.Function) bool { name := f.Name() return isCgoCfunc(name) || isCgoCmacro(name) } @@ -202,6 +202,10 @@ func isCgoCmacro(name string) bool { return strings.HasPrefix(name, "_Cmacro_") } +func isCgoVar(name string) bool { + return strings.HasPrefix(name, "__cgo_") +} + func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Function, llssa.PyObjRef, int) { pkgTypes, name, ftype := p.funcName(f, true) if ftype != goFunc { @@ -255,7 +259,7 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun fn.Inline(llssa.NoInline) } } - isCgo := isCgoCfuncOrCmacro(f) + isCgo := isCgoExternSymbol(f) if nblk := len(f.Blocks); nblk > 0 { p.cgoCalled = false p.cgoArgs = nil @@ -918,7 +922,17 @@ func (p *context) compileValue(b llssa.Builder, v ssa.Value) llssa.Expr { } return pyFn.Expr case *ssa.Global: + varName := v.Name() val := p.varOf(b, v) + if isCgoVar(varName) { + fname := p.fset.Position(v.Pos()).Filename + funcs, ok := p.cgoFuncs[fname] + if !ok { + funcs = make([]string, 0, 1) + } + funcs = append(funcs, val.Name()) + p.cgoFuncs[fname] = funcs + } if debugSymbols { pos := p.fset.Position(v.Pos()) b.DIGlobal(val, v.Name(), pos) @@ -1053,6 +1067,26 @@ func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files [ fn() } externs = ctx.cgoFuncs + // TODO(lijie): read export name + for _, funcs := range externs { + for _, funcName := range funcs { + if strings.Contains(funcName, ".__cgo_") { + goFnName := strings.Replace(funcName, ".__cgo_", ".", 1) + idx := strings.LastIndex(funcName, ".__cgo_") + cfuncName := funcName[idx+len(".__cgo_"):] + v := ret.VarOf(funcName) + if fn := ret.FuncOf(goFnName); fn != nil { + // TODO(lijie): naive go:export, need better way from comment + fn.SetName(cfuncName) + // Replace symbol instead of static linking + v.ReplaceAllUsesWith(fn.Expr) + } else if fn := ret.FuncOf(cfuncName); fn != nil { + // Replace symbol instead of static linking + v.ReplaceAllUsesWith(fn.Expr) + } + } + } + } return } diff --git a/cl/import.go b/cl/import.go index b6877fa56..643950b91 100644 --- a/cl/import.go +++ b/cl/import.go @@ -448,7 +448,7 @@ func (p *context) funcName(fn *ssa.Function, ignore bool) (*types.Package, strin if checkCgo(fname) && !cgoIgnored(fname) { return nil, fname, llgoInstr } - if isCgoCfuncOrCmacro(fn) { + if isCgoExternSymbol(fn) { if _, ok := llgoInstrs[fname]; ok { return nil, fname, llgoInstr } diff --git a/internal/build/build.go b/internal/build/build.go index e12e57ebb..25a0ff753 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -525,6 +525,7 @@ func buildPkg(ctx *context, aPkg *aPackage, verbose bool) (cgoLdflags []string, cl.SetDebug(0) } check(err) + aPkg.LPkg = ret cgoLdflags, err = buildCgo(ctx, aPkg, aPkg.Package.Syntax, externs, verbose) if needLLFile(ctx.mode) { pkg.ExportFile += ".ll" @@ -533,7 +534,6 @@ func buildPkg(ctx *context, aPkg *aPackage, verbose bool) (cgoLdflags []string, fmt.Fprintf(os.Stderr, "==> Export %s: %s\n", aPkg.PkgPath, pkg.ExportFile) } } - aPkg.LPkg = ret return } diff --git a/internal/build/cgo.go b/internal/build/cgo.go index ddbeb376c..ac2b31e81 100644 --- a/internal/build/cgo.go +++ b/internal/build/cgo.go @@ -21,6 +21,7 @@ import ( "fmt" "go/ast" "go/token" + "go/types" "os" "os/exec" "path/filepath" @@ -29,6 +30,7 @@ import ( "github.com/goplus/llgo/internal/buildtags" "github.com/goplus/llgo/internal/safesplit" + llssa "github.com/goplus/llgo/ssa" ) type cgoDecl struct { @@ -93,12 +95,21 @@ func buildCgo(ctx *context, pkg *aPackage, files []*ast.File, externs map[string mallocFix := false for _, symbols := range externs { for _, symbolName := range symbols { - if m := re.FindStringSubmatch(symbolName); len(m) > 0 { - cgoSymbols[symbolName] = m[3] - pkgPrefix := m[1] + lastPart := symbolName + lastDot := strings.LastIndex(symbolName, ".") + if lastDot != -1 { + lastPart = symbolName[lastDot+1:] + } + if strings.HasPrefix(lastPart, "__cgo_") { + // func ptr var: main.__cgo_func_name + cgoSymbols[symbolName] = lastPart + } else if m := re.FindStringSubmatch(symbolName); len(m) > 0 { + prefix := m[1] // _cgo_hash_(Cfunc|Cmacro)_ + name := m[3] // remaining part + cgoSymbols[symbolName] = name // fix missing _cgo_9113e32b6599_Cfunc__Cmalloc if !mallocFix && m[2] == "Cfunc" { - mallocName := pkgPrefix + "_Cmalloc" + mallocName := prefix + "_Cmalloc" cgoSymbols[mallocName] = "_Cmalloc" mallocFix = true } @@ -113,7 +124,7 @@ func buildCgo(ctx *context, pkg *aPackage, files []*ast.File, externs map[string tmpName := tmpFile.Name() defer os.Remove(tmpName) code := cgoHeader + "\n\n" + preamble.src - externDecls, err := genExternDeclsByClang(code, cflags, cgoSymbols) + externDecls, err := genExternDeclsByClang(pkg, code, cflags, cgoSymbols) if err != nil { return nil, err } @@ -137,7 +148,7 @@ type clangASTNode struct { Inner []clangASTNode `json:"inner,omitempty"` } -func genExternDeclsByClang(src string, cflags []string, cgoSymbols map[string]string) (string, error) { +func genExternDeclsByClang(pkg *aPackage, src string, cflags []string, cgoSymbols map[string]string) (string, error) { tmpSrc, err := os.CreateTemp("", "cgo-src-*.c") if err != nil { return "", err @@ -158,25 +169,37 @@ func genExternDeclsByClang(src string, cflags []string, cgoSymbols map[string]st b := strings.Builder{} var toRemove []string for cgoName, symbolName := range cgoSymbols { - if symbolNames[symbolName] { - b.WriteString(fmt.Sprintf("void* %s = (void*)%s;\n", cgoName, symbolName)) + if strings.HasPrefix(symbolName, "__cgo_") { + cfuncName := symbolName[len("__cgo_"):] + cfn := pkg.LPkg.NewFunc(cfuncName, types.NewSignature(nil, nil, nil, false), llssa.InC) + cgoVar := pkg.LPkg.VarOf(cgoName) + cgoVar.ReplaceAllUsesWith(cfn.Expr) toRemove = append(toRemove, cgoName) - } else if macroNames[symbolName] { + } else { + usePtr := "" + if symbolNames[symbolName] { + usePtr = "*" + } else if !macroNames[symbolName] { + continue + } /* template: - typeof(stdout) _cgo_1574167f3838_Cmacro_stdout; + typeof(fputs)* _cgo_1574167f3838_Cfunc_fputs; __attribute__((constructor)) - static void _init__cgo_1574167f3838_Cmacro_stdout() { - _cgo_1574167f3838_Cmacro_stdout = stdout; + static void _init__cgo_1574167f3838_Cfunc_fputs() { + _cgo_1574167f3838_Cfunc_fputs = fputs; }*/ b.WriteString(fmt.Sprintf(` -typeof(%s) %s; +typeof(%s)%s %s; __attribute__((constructor)) static void _init_%s() { %s = %s; } -`, symbolName, cgoName, cgoName, cgoName, symbolName)) +`, + symbolName, usePtr, cgoName, + cgoName, + cgoName, symbolName)) toRemove = append(toRemove, cgoName) } } diff --git a/ssa/decl.go b/ssa/decl.go index 165b5094e..d139c7cb2 100644 --- a/ssa/decl.go +++ b/ssa/decl.go @@ -117,6 +117,10 @@ func (g Global) InitNil() { g.impl.SetInitializer(llvm.ConstNull(g.impl.GlobalValueType())) } +func (g Global) ReplaceAllUsesWith(v Expr) { + g.impl.ReplaceAllUsesWith(v.impl) +} + // ----------------------------------------------------------------------------- // Function represents the parameters, results, and code of a function diff --git a/ssa/expr.go b/ssa/expr.go index f8fa7365b..7b4b0b019 100644 --- a/ssa/expr.go +++ b/ssa/expr.go @@ -59,6 +59,15 @@ func (v Expr) SetOrdering(ordering AtomicOrdering) Expr { return v } +func (v Expr) SetName(alias string) Expr { + v.impl.SetName(alias) + return v +} + +func (v Expr) Name() string { + return v.impl.Name() +} + // ----------------------------------------------------------------------------- type builtinTy struct { From e46b3e24d660c22bc10cd594623bd755e5eb6505 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Wed, 27 Nov 2024 17:14:19 +0800 Subject: [PATCH 6/7] test: move cgo demo into _testgo, and make links --- _demo/cgobasic | 1 + _demo/cgocfiles | 1 + _demo/cgodefer | 1 + _demo/cgofull | 1 + _demo/cgofull/bar.go | 16 - _demo/cgofull/cgofull.go | 110 ----- _demo/cgofull/foo.c | 6 - _demo/cgofull/foo.go | 16 - _demo/cgofull/foo.h | 7 - _demo/cgofull/py.go | 24 -- _demo/cgofull/pymod1/pymod1.go | 11 - _demo/cgofull/pymod2/pymod2.go | 11 - _demo/cgomacro | 1 + _demo/cgopython | 1 + {_demo => cl/_testgo}/cgobasic/cgobasic.go | 0 cl/_testgo/cgobasic/out.ll | 404 +++++++++++++++++++ {_demo => cl/_testgo}/cgocfiles/cgocfiles.go | 0 {_demo => cl/_testgo}/cgocfiles/in.c | 0 {_demo => cl/_testgo}/cgocfiles/in.h | 0 cl/_testgo/cgocfiles/out.ll | 180 +++++++++ {_demo => cl/_testgo}/cgodefer/cgodefer.go | 0 cl/_testgo/cgodefer/out.ll | 1 + cl/_testgo/cgofull/cgofull.go | 4 +- {_demo => cl/_testgo}/cgomacro/cgomacro.go | 0 cl/_testgo/cgomacro/out.ll | 1 + {_demo => cl/_testgo}/cgopython/cgopython.go | 0 cl/_testgo/cgopython/out.ll | 1 + 27 files changed, 595 insertions(+), 203 deletions(-) create mode 120000 _demo/cgobasic create mode 120000 _demo/cgocfiles create mode 120000 _demo/cgodefer create mode 120000 _demo/cgofull delete mode 100644 _demo/cgofull/bar.go delete mode 100644 _demo/cgofull/cgofull.go delete mode 100644 _demo/cgofull/foo.c delete mode 100644 _demo/cgofull/foo.go delete mode 100644 _demo/cgofull/foo.h delete mode 100644 _demo/cgofull/py.go delete mode 100644 _demo/cgofull/pymod1/pymod1.go delete mode 100644 _demo/cgofull/pymod2/pymod2.go create mode 120000 _demo/cgomacro create mode 120000 _demo/cgopython rename {_demo => cl/_testgo}/cgobasic/cgobasic.go (100%) create mode 100644 cl/_testgo/cgobasic/out.ll rename {_demo => cl/_testgo}/cgocfiles/cgocfiles.go (100%) rename {_demo => cl/_testgo}/cgocfiles/in.c (100%) rename {_demo => cl/_testgo}/cgocfiles/in.h (100%) create mode 100644 cl/_testgo/cgocfiles/out.ll rename {_demo => cl/_testgo}/cgodefer/cgodefer.go (100%) create mode 100644 cl/_testgo/cgodefer/out.ll rename {_demo => cl/_testgo}/cgomacro/cgomacro.go (100%) create mode 100644 cl/_testgo/cgomacro/out.ll rename {_demo => cl/_testgo}/cgopython/cgopython.go (100%) create mode 100644 cl/_testgo/cgopython/out.ll diff --git a/_demo/cgobasic b/_demo/cgobasic new file mode 120000 index 000000000..b6fb76ea6 --- /dev/null +++ b/_demo/cgobasic @@ -0,0 +1 @@ +../cl/_testgo/cgobasic \ No newline at end of file diff --git a/_demo/cgocfiles b/_demo/cgocfiles new file mode 120000 index 000000000..9007fceed --- /dev/null +++ b/_demo/cgocfiles @@ -0,0 +1 @@ +../cl/_testgo/cgocfiles \ No newline at end of file diff --git a/_demo/cgodefer b/_demo/cgodefer new file mode 120000 index 000000000..d4396bb2a --- /dev/null +++ b/_demo/cgodefer @@ -0,0 +1 @@ +../cl/_testgo/cgodefer \ No newline at end of file diff --git a/_demo/cgofull b/_demo/cgofull new file mode 120000 index 000000000..d01e01ffb --- /dev/null +++ b/_demo/cgofull @@ -0,0 +1 @@ +../cl/_testgo/cgofull \ No newline at end of file diff --git a/_demo/cgofull/bar.go b/_demo/cgofull/bar.go deleted file mode 100644 index 08e6830f9..000000000 --- a/_demo/cgofull/bar.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -/* -#cgo CFLAGS: -DBAR -#include -#include "foo.h" -static void foo(Foo* f) { - printf("foo in bar: %d\n", f->a); -} -*/ -import "C" - -func Bar(f *C.Foo) { - C.print_foo(f) - C.foo(f) -} diff --git a/_demo/cgofull/cgofull.go b/_demo/cgofull/cgofull.go deleted file mode 100644 index 038c9ef4c..000000000 --- a/_demo/cgofull/cgofull.go +++ /dev/null @@ -1,110 +0,0 @@ -package main - -/* -#cgo windows,!amd64 CFLAGS: -D_WIN32 -#cgo !windows CFLAGS: -D_POSIX -#cgo windows,amd64 CFLAGS: -D_WIN64 -#cgo linux,amd64 CFLAGS: -D_LINUX64 -#cgo !windows,amd64 CFLAGS: -D_UNIX64 -#cgo pkg-config: python3-embed -#include -#include -#include "foo.h" -typedef struct { - int a; -} s4; - -typedef struct { - int a; - int b; -} s8; - -typedef struct { - int a; - int b; - int c; -} s12; - -typedef struct { - int a; - int b; - int c; - int d; -} s16; - -typedef struct { - int a; - int b; - int c; - int d; - int e; -} s20; - -static int test_structs(s4* s4, s8* s8, s12* s12, s16* s16, s20* s20) { - printf("s4.a: %d\n", s4->a); - printf("s8.a: %d, s8.b: %d\n", s8->a, s8->b); - printf("s12.a: %d, s12.b: %d, s12.c: %d\n", s12->a, s12->b, s12->c); - printf("s16.a: %d, s16.b: %d, s16.c: %d, s16.d: %d\n", s16->a, s16->b, s16->c, s16->d); - printf("s20.a: %d, s20.b: %d, s20.c: %d, s20.d: %d, s20.e: %d\n", s20->a, s20->b, s20->c, s20->d, s20->e); - - return s4->a + s8->a + s8->b + s12->a + s12->b + s12->c + s16->a + s16->b + s16->c + s16->d + s20->a + s20->b + s20->c + s20->d + s20->e; -} - -static void test_macros() { -#ifdef FOO - printf("FOO is defined\n"); -#endif -#ifdef BAR - printf("BAR is defined\n"); -#endif -#ifdef _WIN32 - printf("WIN32 is defined\n"); -#endif -#ifdef _POSIX - printf("POSIX is defined\n"); -#endif -#ifdef _WIN64 - printf("WIN64 is defined\n"); -#endif -#ifdef _LINUX64 - printf("LINUX64 is defined\n"); -#endif -#ifdef _UNIX64 - printf("UNIX64 is defined\n"); -#endif -} - -#define MY_VERSION "1.0.0" -#define MY_CODE 0x12345678 -*/ -import "C" -import ( - "fmt" - "unsafe" - - "github.com/goplus/llgo/_demo/cgofull/pymod1" - "github.com/goplus/llgo/_demo/cgofull/pymod2" -) - -func main() { - runPy() - f := &C.Foo{a: 1} - Foo(f) - Bar(f) - C.test_macros() - r := C.test_structs(&C.s4{a: 1}, &C.s8{a: 1, b: 2}, &C.s12{a: 1, b: 2, c: 3}, &C.s16{a: 1, b: 2, c: 3, d: 4}, &C.s20{a: 1, b: 2, c: 3, d: 4, e: 5}) - fmt.Println(r) - if r != 35 { - panic("test_structs failed") - } - fmt.Println(C.MY_VERSION) - fmt.Println(int(C.MY_CODE)) -} - -func runPy() { - Initialize() - defer Finalize() - Run("print('Hello, Python!')") - C.PyObject_Print((*C.PyObject)(unsafe.Pointer(pymod1.Float(1.23))), C.stderr, 0) - C.PyObject_Print((*C.PyObject)(unsafe.Pointer(pymod2.Long(123))), C.stdout, 0) -} diff --git a/_demo/cgofull/foo.c b/_demo/cgofull/foo.c deleted file mode 100644 index b3f07e049..000000000 --- a/_demo/cgofull/foo.c +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include "foo.h" - -void print_foo(Foo* f) { - printf("print_foo: %d\n", f->a); -} diff --git a/_demo/cgofull/foo.go b/_demo/cgofull/foo.go deleted file mode 100644 index 1d6aa3b4b..000000000 --- a/_demo/cgofull/foo.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -/* -#cgo CFLAGS: -DFOO -#include -#include "foo.h" -static void foo(Foo* f) { - printf("foo in bar: %d\n", f->a); -} -*/ -import "C" - -func Foo(f *C.Foo) { - C.print_foo(f) - C.foo(f) -} diff --git a/_demo/cgofull/foo.h b/_demo/cgofull/foo.h deleted file mode 100644 index f714fec6d..000000000 --- a/_demo/cgofull/foo.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -typedef struct { - int a; -} Foo; - -extern void print_foo(Foo* f); diff --git a/_demo/cgofull/py.go b/_demo/cgofull/py.go deleted file mode 100644 index 509d88ca2..000000000 --- a/_demo/cgofull/py.go +++ /dev/null @@ -1,24 +0,0 @@ -package main - -/* -#cgo pkg-config: python3-embed -#include -*/ -import "C" -import "fmt" - -func Initialize() { - C.Py_Initialize() -} - -func Finalize() { - C.Py_Finalize() -} - -func Run(code string) error { - if C.PyRun_SimpleString(C.CString(code)) != 0 { - C.PyErr_Print() - return fmt.Errorf("failed to run code") - } - return nil -} diff --git a/_demo/cgofull/pymod1/pymod1.go b/_demo/cgofull/pymod1/pymod1.go deleted file mode 100644 index 480b1b4a5..000000000 --- a/_demo/cgofull/pymod1/pymod1.go +++ /dev/null @@ -1,11 +0,0 @@ -package pymod1 - -/* -#cgo pkg-config: python3-embed -#include -*/ -import "C" - -func Float(f float64) *C.PyObject { - return C.PyFloat_FromDouble(C.double(f)) -} diff --git a/_demo/cgofull/pymod2/pymod2.go b/_demo/cgofull/pymod2/pymod2.go deleted file mode 100644 index 8cc7dd27a..000000000 --- a/_demo/cgofull/pymod2/pymod2.go +++ /dev/null @@ -1,11 +0,0 @@ -package pymod2 - -/* -#cgo pkg-config: python3-embed -#include -*/ -import "C" - -func Long(l int64) *C.PyObject { - return C.PyLong_FromLongLong(C.longlong(l)) -} diff --git a/_demo/cgomacro b/_demo/cgomacro new file mode 120000 index 000000000..59e0ab5b5 --- /dev/null +++ b/_demo/cgomacro @@ -0,0 +1 @@ +../cl/_testgo/cgomacro \ No newline at end of file diff --git a/_demo/cgopython b/_demo/cgopython new file mode 120000 index 000000000..1176ad29f --- /dev/null +++ b/_demo/cgopython @@ -0,0 +1 @@ +../cl/_testgo/cgopython \ No newline at end of file diff --git a/_demo/cgobasic/cgobasic.go b/cl/_testgo/cgobasic/cgobasic.go similarity index 100% rename from _demo/cgobasic/cgobasic.go rename to cl/_testgo/cgobasic/cgobasic.go diff --git a/cl/_testgo/cgobasic/out.ll b/cl/_testgo/cgobasic/out.ll new file mode 100644 index 000000000..2598de4f5 --- /dev/null +++ b/cl/_testgo/cgobasic/out.ll @@ -0,0 +1,404 @@ +; ModuleID = 'main' +source_filename = "main" + +%"github.com/goplus/llgo/internal/runtime.eface" = type { ptr, ptr } +%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 } +%"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 } +%"github.com/goplus/llgo/internal/runtime.iface" = type { ptr, ptr } + +@"github.com/goplus/llgo/internal/runtime.cgoAlwaysFalse" = external global i1, align 1 +@_cgo_62905c3ec377_Cfunc__Cmalloc = external global i8, align 1 +@_cgo_62905c3ec377_Cfunc_cos = external global i8, align 1 +@_cgo_62905c3ec377_Cfunc_free = external global i8, align 1 +@_cgo_62905c3ec377_Cfunc_log = external global i8, align 1 +@_cgo_62905c3ec377_Cfunc_puts = external global i8, align 1 +@_cgo_62905c3ec377_Cfunc_sin = external global i8, align 1 +@_cgo_62905c3ec377_Cfunc_sqrt = external global i8, align 1 +@main._cgo_62905c3ec377_Cfunc__Cmalloc = global ptr null, align 8 +@main._cgo_62905c3ec377_Cfunc_cos = global ptr null, align 8 +@main._cgo_62905c3ec377_Cfunc_free = global ptr null, align 8 +@main._cgo_62905c3ec377_Cfunc_log = global ptr null, align 8 +@main._cgo_62905c3ec377_Cfunc_puts = global ptr null, align 8 +@main._cgo_62905c3ec377_Cfunc_sin = global ptr null, align 8 +@main._cgo_62905c3ec377_Cfunc_sqrt = global ptr null, align 8 +@"main.init$guard" = global i1 false, align 1 +@__llgo_argc = global i32 0, align 4 +@__llgo_argv = global ptr null, align 8 +@0 = private unnamed_addr constant [13 x i8] c"Hello, World!", align 1 +@1 = private unnamed_addr constant [29 x i8] c"Converted back to Go string: ", align 1 +@2 = private unnamed_addr constant [23 x i8] c"Length-limited string: ", align 1 +@3 = private unnamed_addr constant [33 x i8] c"Converted back to Go byte slice: ", align 1 +@_llgo_float64 = linkonce global ptr null, align 8 +@4 = private unnamed_addr constant [14 x i8] c"sqrt(%v) = %v\0A", align 1 +@5 = private unnamed_addr constant [13 x i8] c"sin(%v) = %v\0A", align 1 +@6 = private unnamed_addr constant [13 x i8] c"cos(%v) = %v\0A", align 1 +@7 = private unnamed_addr constant [13 x i8] c"log(%v) = %v\0A", align 1 +@_llgo_byte = linkonce global ptr null, align 8 +@"[]_llgo_byte" = linkonce global ptr null, align 8 +@_llgo_Pointer = linkonce global ptr null, align 8 + +define double @main._Cfunc_cos(double %0) { +_llgo_0: + %1 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %2 = load ptr, ptr @main._cgo_62905c3ec377_Cfunc_cos, align 8 + %3 = load ptr, ptr %2, align 8 + %4 = call double %3(double %0) + ret double %4 +} + +define [0 x i8] @main._Cfunc_free(ptr %0) { +_llgo_0: + %1 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %2 = load ptr, ptr @main._cgo_62905c3ec377_Cfunc_free, align 8 + %3 = load ptr, ptr %2, align 8 + %4 = call [0 x i8] %3(ptr %0) + ret [0 x i8] %4 +} + +define double @main._Cfunc_log(double %0) { +_llgo_0: + %1 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %2 = load ptr, ptr @main._cgo_62905c3ec377_Cfunc_log, align 8 + %3 = load ptr, ptr %2, align 8 + %4 = call double %3(double %0) + ret double %4 +} + +define i32 @main._Cfunc_puts(ptr %0) { +_llgo_0: + %1 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %2 = load ptr, ptr @main._cgo_62905c3ec377_Cfunc_puts, align 8 + %3 = load ptr, ptr %2, align 8 + %4 = call i32 %3(ptr %0) + ret i32 %4 +} + +define double @main._Cfunc_sin(double %0) { +_llgo_0: + %1 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %2 = load ptr, ptr @main._cgo_62905c3ec377_Cfunc_sin, align 8 + %3 = load ptr, ptr %2, align 8 + %4 = call double %3(double %0) + ret double %4 +} + +define double @main._Cfunc_sqrt(double %0) { +_llgo_0: + %1 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %2 = load ptr, ptr @main._cgo_62905c3ec377_Cfunc_sqrt, align 8 + %3 = load ptr, ptr %2, align 8 + %4 = call double %3(double %0) + ret double %4 +} + +define ptr @main._Cgo_ptr(ptr %0) { +_llgo_0: + ret ptr %0 +} + +declare void @runtime.cgoUse(%"github.com/goplus/llgo/internal/runtime.eface") + +declare void @runtime.cgoCheckResult(%"github.com/goplus/llgo/internal/runtime.eface") + +define void @main.init() { +_llgo_0: + %0 = load i1, ptr @"main.init$guard", align 1 + br i1 %0, label %_llgo_2, label %_llgo_1 + +_llgo_1: ; preds = %_llgo_0 + store i1 true, ptr @"main.init$guard", align 1 + call void @syscall.init() + call void @fmt.init() + call void @"main.init$after"() + store ptr @_cgo_62905c3ec377_Cfunc_cos, ptr @main._cgo_62905c3ec377_Cfunc_cos, align 8 + store ptr @_cgo_62905c3ec377_Cfunc_free, ptr @main._cgo_62905c3ec377_Cfunc_free, align 8 + store ptr @_cgo_62905c3ec377_Cfunc_log, ptr @main._cgo_62905c3ec377_Cfunc_log, align 8 + store ptr @_cgo_62905c3ec377_Cfunc_puts, ptr @main._cgo_62905c3ec377_Cfunc_puts, align 8 + store ptr @_cgo_62905c3ec377_Cfunc_sin, ptr @main._cgo_62905c3ec377_Cfunc_sin, align 8 + store ptr @_cgo_62905c3ec377_Cfunc_sqrt, ptr @main._cgo_62905c3ec377_Cfunc_sqrt, align 8 + store ptr @_cgo_62905c3ec377_Cfunc__Cmalloc, ptr @main._cgo_62905c3ec377_Cfunc__Cmalloc, align 8 + br label %_llgo_2 + +_llgo_2: ; preds = %_llgo_1, %_llgo_0 + ret void +} + +define i32 @main(i32 %0, ptr %1) { +_llgo_0: + store i32 %0, ptr @__llgo_argc, align 4 + store ptr %1, ptr @__llgo_argv, align 8 + call void @"github.com/goplus/llgo/internal/runtime.init"() + call void @main.init() + %2 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %3 = call ptr @"github.com/goplus/llgo/internal/runtime.CString"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @0, i64 13 }) + store ptr %3, ptr %2, align 8 + %4 = load ptr, ptr %2, align 8 + %5 = call i32 @main._Cfunc_puts(ptr %4) + %6 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 24) + %7 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 4) + %8 = getelementptr inbounds i8, ptr %7, i64 0 + store i8 65, ptr %8, align 1 + %9 = getelementptr inbounds i8, ptr %7, i64 1 + store i8 66, ptr %9, align 1 + %10 = getelementptr inbounds i8, ptr %7, i64 2 + store i8 67, ptr %10, align 1 + %11 = getelementptr inbounds i8, ptr %7, i64 3 + store i8 68, ptr %11, align 1 + %12 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" undef, ptr %7, 0 + %13 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %12, i64 4, 1 + %14 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %13, i64 4, 2 + store %"github.com/goplus/llgo/internal/runtime.Slice" %14, ptr %6, align 8 + %15 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %16 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 8) + %17 = getelementptr inbounds { ptr }, ptr %16, i32 0, i32 0 + store ptr %6, ptr %17, align 8 + %18 = insertvalue { ptr, ptr } { ptr @"main.main$1", ptr undef }, ptr %16, 1 + %19 = extractvalue { ptr, ptr } %18, 1 + %20 = extractvalue { ptr, ptr } %18, 0 + %21 = call ptr %20(ptr %19) + store ptr %21, ptr %15, align 8 + %22 = load ptr, ptr %2, align 8 + %23 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.GoString"(ptr %22) + call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @1, i64 29 }) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %23) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10) + %24 = load ptr, ptr %2, align 8 + %25 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.GoStringN"(ptr %24, i32 5) + call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @2, i64 23 }) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %25) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10) + %26 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 8) + %27 = getelementptr inbounds { ptr }, ptr %26, i32 0, i32 0 + store ptr %15, ptr %27, align 8 + %28 = insertvalue { ptr, ptr } { ptr @"main.main$2", ptr undef }, ptr %26, 1 + %29 = extractvalue { ptr, ptr } %28, 1 + %30 = extractvalue { ptr, ptr } %28, 0 + %31 = call %"github.com/goplus/llgo/internal/runtime.Slice" %30(ptr %29) + call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @3, i64 33 }) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/internal/runtime.PrintSlice"(%"github.com/goplus/llgo/internal/runtime.Slice" %31) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10) + %32 = call double @main._Cfunc_sqrt(double 2.000000e+00) + %33 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 32) + %34 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %33, i64 0 + %35 = load ptr, ptr @_llgo_float64, align 8 + %36 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %35, 0 + %37 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %36, ptr inttoptr (i64 4611686018427387904 to ptr), 1 + store %"github.com/goplus/llgo/internal/runtime.eface" %37, ptr %34, align 8 + %38 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %33, i64 1 + %39 = load ptr, ptr @_llgo_float64, align 8 + %40 = bitcast double %32 to i64 + %41 = inttoptr i64 %40 to ptr + %42 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %39, 0 + %43 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %42, ptr %41, 1 + store %"github.com/goplus/llgo/internal/runtime.eface" %43, ptr %38, align 8 + %44 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" undef, ptr %33, 0 + %45 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %44, i64 2, 1 + %46 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %45, i64 2, 2 + %47 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Printf(%"github.com/goplus/llgo/internal/runtime.String" { ptr @4, i64 14 }, %"github.com/goplus/llgo/internal/runtime.Slice" %46) + %48 = call double @main._Cfunc_sin(double 2.000000e+00) + %49 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 32) + %50 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %49, i64 0 + %51 = load ptr, ptr @_llgo_float64, align 8 + %52 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %51, 0 + %53 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %52, ptr inttoptr (i64 4611686018427387904 to ptr), 1 + store %"github.com/goplus/llgo/internal/runtime.eface" %53, ptr %50, align 8 + %54 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %49, i64 1 + %55 = load ptr, ptr @_llgo_float64, align 8 + %56 = bitcast double %48 to i64 + %57 = inttoptr i64 %56 to ptr + %58 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %55, 0 + %59 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %58, ptr %57, 1 + store %"github.com/goplus/llgo/internal/runtime.eface" %59, ptr %54, align 8 + %60 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" undef, ptr %49, 0 + %61 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %60, i64 2, 1 + %62 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %61, i64 2, 2 + %63 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Printf(%"github.com/goplus/llgo/internal/runtime.String" { ptr @5, i64 13 }, %"github.com/goplus/llgo/internal/runtime.Slice" %62) + %64 = call double @main._Cfunc_cos(double 2.000000e+00) + %65 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 32) + %66 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %65, i64 0 + %67 = load ptr, ptr @_llgo_float64, align 8 + %68 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %67, 0 + %69 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %68, ptr inttoptr (i64 4611686018427387904 to ptr), 1 + store %"github.com/goplus/llgo/internal/runtime.eface" %69, ptr %66, align 8 + %70 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %65, i64 1 + %71 = load ptr, ptr @_llgo_float64, align 8 + %72 = bitcast double %64 to i64 + %73 = inttoptr i64 %72 to ptr + %74 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %71, 0 + %75 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %74, ptr %73, 1 + store %"github.com/goplus/llgo/internal/runtime.eface" %75, ptr %70, align 8 + %76 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" undef, ptr %65, 0 + %77 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %76, i64 2, 1 + %78 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %77, i64 2, 2 + %79 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Printf(%"github.com/goplus/llgo/internal/runtime.String" { ptr @6, i64 13 }, %"github.com/goplus/llgo/internal/runtime.Slice" %78) + %80 = call double @main._Cfunc_log(double 2.000000e+00) + %81 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 32) + %82 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %81, i64 0 + %83 = load ptr, ptr @_llgo_float64, align 8 + %84 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %83, 0 + %85 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %84, ptr inttoptr (i64 4611686018427387904 to ptr), 1 + store %"github.com/goplus/llgo/internal/runtime.eface" %85, ptr %82, align 8 + %86 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %81, i64 1 + %87 = load ptr, ptr @_llgo_float64, align 8 + %88 = bitcast double %80 to i64 + %89 = inttoptr i64 %88 to ptr + %90 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %87, 0 + %91 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %90, ptr %89, 1 + store %"github.com/goplus/llgo/internal/runtime.eface" %91, ptr %86, align 8 + %92 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" undef, ptr %81, 0 + %93 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %92, i64 2, 1 + %94 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %93, i64 2, 2 + %95 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Printf(%"github.com/goplus/llgo/internal/runtime.String" { ptr @7, i64 13 }, %"github.com/goplus/llgo/internal/runtime.Slice" %94) + %96 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 8) + %97 = getelementptr inbounds { ptr }, ptr %96, i32 0, i32 0 + store ptr %2, ptr %97, align 8 + %98 = insertvalue { ptr, ptr } { ptr @"main.main$3", ptr undef }, ptr %96, 1 + %99 = extractvalue { ptr, ptr } %98, 1 + %100 = extractvalue { ptr, ptr } %98, 0 + call void %100(ptr %99) + %101 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 8) + %102 = getelementptr inbounds { ptr }, ptr %101, i32 0, i32 0 + store ptr %15, ptr %102, align 8 + %103 = insertvalue { ptr, ptr } { ptr @"main.main$4", ptr undef }, ptr %101, 1 + %104 = extractvalue { ptr, ptr } %103, 1 + %105 = extractvalue { ptr, ptr } %103, 0 + call void %105(ptr %104) + ret i32 0 +} + +define ptr @"main.main$1"(ptr %0) { +_llgo_0: + %1 = load { ptr }, ptr %0, align 8 + %2 = extractvalue { ptr } %1, 0 + %3 = load %"github.com/goplus/llgo/internal/runtime.Slice", ptr %2, align 8 + %4 = load ptr, ptr @_llgo_byte, align 8 + %5 = load ptr, ptr @"[]_llgo_byte", align 8 + %6 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 24) + store %"github.com/goplus/llgo/internal/runtime.Slice" %3, ptr %6, align 8 + %7 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %5, 0 + %8 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %7, ptr %6, 1 + %9 = call ptr @"github.com/goplus/llgo/internal/runtime.CBytes"(%"github.com/goplus/llgo/internal/runtime.Slice" %3) + ret ptr %9 +} + +define %"github.com/goplus/llgo/internal/runtime.Slice" @"main.main$2"(ptr %0) { +_llgo_0: + %1 = load { ptr }, ptr %0, align 8 + %2 = extractvalue { ptr } %1, 0 + %3 = load ptr, ptr %2, align 8 + %4 = load ptr, ptr @_llgo_Pointer, align 8 + %5 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %4, 0 + %6 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %5, ptr %3, 1 + %7 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.GoBytes"(ptr %3, i32 4) + ret %"github.com/goplus/llgo/internal/runtime.Slice" %7 +} + +define void @"main.main$3"(ptr %0) { +_llgo_0: + %1 = load { ptr }, ptr %0, align 8 + %2 = extractvalue { ptr } %1, 0 + %3 = load ptr, ptr %2, align 8 + %4 = load ptr, ptr @_llgo_Pointer, align 8 + %5 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %4, 0 + %6 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %5, ptr %3, 1 + %7 = call [0 x i8] @main._Cfunc_free(ptr %3) + ret void +} + +define void @"main.main$4"(ptr %0) { +_llgo_0: + %1 = load { ptr }, ptr %0, align 8 + %2 = extractvalue { ptr } %1, 0 + %3 = load ptr, ptr %2, align 8 + %4 = load ptr, ptr @_llgo_Pointer, align 8 + %5 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %4, 0 + %6 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %5, ptr %3, 1 + %7 = call [0 x i8] @main._Cfunc_free(ptr %3) + ret void +} + +declare void @runtime.throw(%"github.com/goplus/llgo/internal/runtime.String") + +declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64) + +declare void @syscall.init() + +declare void @fmt.init() + +declare void @"github.com/goplus/llgo/internal/runtime.init"() + +declare ptr @"github.com/goplus/llgo/internal/runtime.CString"(%"github.com/goplus/llgo/internal/runtime.String") + +declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64) + +declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.GoString"(ptr) + +declare void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String") + +declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8) + +declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.GoStringN"(ptr, i64) + +declare void @"github.com/goplus/llgo/internal/runtime.PrintSlice"(%"github.com/goplus/llgo/internal/runtime.Slice") + +define void @"main.init$after"() { +_llgo_0: + %0 = load ptr, ptr @_llgo_float64, align 8 + %1 = icmp eq ptr %0, null + br i1 %1, label %_llgo_1, label %_llgo_2 + +_llgo_1: ; preds = %_llgo_0 + %2 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 46) + store ptr %2, ptr @_llgo_float64, align 8 + br label %_llgo_2 + +_llgo_2: ; preds = %_llgo_1, %_llgo_0 + %3 = load ptr, ptr @_llgo_byte, align 8 + %4 = icmp eq ptr %3, null + br i1 %4, label %_llgo_3, label %_llgo_4 + +_llgo_3: ; preds = %_llgo_2 + %5 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 40) + store ptr %5, ptr @_llgo_byte, align 8 + br label %_llgo_4 + +_llgo_4: ; preds = %_llgo_3, %_llgo_2 + %6 = load ptr, ptr @"[]_llgo_byte", align 8 + %7 = icmp eq ptr %6, null + br i1 %7, label %_llgo_5, label %_llgo_6 + +_llgo_5: ; preds = %_llgo_4 + %8 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 40) + %9 = call ptr @"github.com/goplus/llgo/internal/runtime.SliceOf"(ptr %8) + store ptr %9, ptr @"[]_llgo_byte", align 8 + br label %_llgo_6 + +_llgo_6: ; preds = %_llgo_5, %_llgo_4 + %10 = load ptr, ptr @_llgo_Pointer, align 8 + %11 = icmp eq ptr %10, null + br i1 %11, label %_llgo_7, label %_llgo_8 + +_llgo_7: ; preds = %_llgo_6 + %12 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 58) + call void @"github.com/goplus/llgo/internal/runtime.SetDirectIface"(ptr %12) + store ptr %12, ptr @_llgo_Pointer, align 8 + br label %_llgo_8 + +_llgo_8: ; preds = %_llgo_7, %_llgo_6 + ret void +} + +declare ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64) + +declare { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Printf(%"github.com/goplus/llgo/internal/runtime.String", %"github.com/goplus/llgo/internal/runtime.Slice") + +declare ptr @"github.com/goplus/llgo/internal/runtime.SliceOf"(ptr) + +declare ptr @"github.com/goplus/llgo/internal/runtime.CBytes"(%"github.com/goplus/llgo/internal/runtime.Slice") + +declare void @"github.com/goplus/llgo/internal/runtime.SetDirectIface"(ptr) + +declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.GoBytes"(ptr, i64) diff --git a/_demo/cgocfiles/cgocfiles.go b/cl/_testgo/cgocfiles/cgocfiles.go similarity index 100% rename from _demo/cgocfiles/cgocfiles.go rename to cl/_testgo/cgocfiles/cgocfiles.go diff --git a/_demo/cgocfiles/in.c b/cl/_testgo/cgocfiles/in.c similarity index 100% rename from _demo/cgocfiles/in.c rename to cl/_testgo/cgocfiles/in.c diff --git a/_demo/cgocfiles/in.h b/cl/_testgo/cgocfiles/in.h similarity index 100% rename from _demo/cgocfiles/in.h rename to cl/_testgo/cgocfiles/in.h diff --git a/cl/_testgo/cgocfiles/out.ll b/cl/_testgo/cgocfiles/out.ll new file mode 100644 index 000000000..ea10f13e3 --- /dev/null +++ b/cl/_testgo/cgocfiles/out.ll @@ -0,0 +1,180 @@ +; ModuleID = 'main' +source_filename = "main" + +%"github.com/goplus/llgo/internal/runtime.eface" = type { ptr, ptr } +%main._Ctype_struct___3 = type { i32 } +%main._Ctype_struct___4 = type { i32, i32 } +%main._Ctype_struct___0 = type { i32, i32, i32 } +%main._Ctype_struct___1 = type { i32, i32, i32, i32 } +%main._Ctype_struct___2 = type { i32, i32, i32, i32, i32 } +%"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 } +%"github.com/goplus/llgo/internal/runtime.iface" = type { ptr, ptr } +%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 } + +@"github.com/goplus/llgo/internal/runtime.cgoAlwaysFalse" = external global i1, align 1 +@_cgo_35faaf752e93_Cfunc_test_structs = external global i8, align 1 +@main._cgo_35faaf752e93_Cfunc_test_structs = global ptr null, align 8 +@"main.init$guard" = global i1 false, align 1 +@__llgo_argc = global i32 0, align 4 +@__llgo_argv = global ptr null, align 8 +@_llgo_main._Ctype_int = linkonce global ptr null, align 8 +@0 = private unnamed_addr constant [15 x i8] c"main._Ctype_int", align 1 +@_llgo_int32 = linkonce global ptr null, align 8 +@1 = private unnamed_addr constant [4 x i8] c"main", align 1 +@2 = private unnamed_addr constant [10 x i8] c"_Ctype_int", align 1 +@3 = private unnamed_addr constant [19 x i8] c"test_structs failed", align 1 +@_llgo_string = linkonce global ptr null, align 8 + +define i32 @main._Cfunc_test_structs(ptr %0, ptr %1, ptr %2, ptr %3, ptr %4) { +_llgo_0: + %5 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %6 = load ptr, ptr @main._cgo_35faaf752e93_Cfunc_test_structs, align 8 + %7 = load ptr, ptr %6, align 8 + %8 = call i32 %7(ptr %0, ptr %1, ptr %2, ptr %3, ptr %4) + ret i32 %8 +} + +define ptr @main._Cgo_ptr(ptr %0) { +_llgo_0: + ret ptr %0 +} + +declare void @runtime.cgoUse(%"github.com/goplus/llgo/internal/runtime.eface") + +declare void @runtime.cgoCheckResult(%"github.com/goplus/llgo/internal/runtime.eface") + +define void @main.init() { +_llgo_0: + %0 = load i1, ptr @"main.init$guard", align 1 + br i1 %0, label %_llgo_2, label %_llgo_1 + +_llgo_1: ; preds = %_llgo_0 + store i1 true, ptr @"main.init$guard", align 1 + call void @syscall.init() + call void @fmt.init() + call void @"main.init$after"() + store ptr @_cgo_35faaf752e93_Cfunc_test_structs, ptr @main._cgo_35faaf752e93_Cfunc_test_structs, align 8 + br label %_llgo_2 + +_llgo_2: ; preds = %_llgo_1, %_llgo_0 + ret void +} + +define i32 @main(i32 %0, ptr %1) { +_llgo_0: + store i32 %0, ptr @__llgo_argc, align 4 + store ptr %1, ptr @__llgo_argv, align 8 + call void @"github.com/goplus/llgo/internal/runtime.init"() + call void @main.init() + %2 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 4) + %3 = getelementptr inbounds %main._Ctype_struct___3, ptr %2, i32 0, i32 0 + store i32 1, ptr %3, align 4 + %4 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8) + %5 = getelementptr inbounds %main._Ctype_struct___4, ptr %4, i32 0, i32 0 + %6 = getelementptr inbounds %main._Ctype_struct___4, ptr %4, i32 0, i32 1 + store i32 1, ptr %5, align 4 + store i32 2, ptr %6, align 4 + %7 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 12) + %8 = getelementptr inbounds %main._Ctype_struct___0, ptr %7, i32 0, i32 0 + %9 = getelementptr inbounds %main._Ctype_struct___0, ptr %7, i32 0, i32 1 + %10 = getelementptr inbounds %main._Ctype_struct___0, ptr %7, i32 0, i32 2 + store i32 1, ptr %8, align 4 + store i32 2, ptr %9, align 4 + store i32 3, ptr %10, align 4 + %11 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 16) + %12 = getelementptr inbounds %main._Ctype_struct___1, ptr %11, i32 0, i32 0 + %13 = getelementptr inbounds %main._Ctype_struct___1, ptr %11, i32 0, i32 1 + %14 = getelementptr inbounds %main._Ctype_struct___1, ptr %11, i32 0, i32 2 + %15 = getelementptr inbounds %main._Ctype_struct___1, ptr %11, i32 0, i32 3 + store i32 1, ptr %12, align 4 + store i32 2, ptr %13, align 4 + store i32 3, ptr %14, align 4 + store i32 4, ptr %15, align 4 + %16 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 20) + %17 = getelementptr inbounds %main._Ctype_struct___2, ptr %16, i32 0, i32 0 + %18 = getelementptr inbounds %main._Ctype_struct___2, ptr %16, i32 0, i32 1 + %19 = getelementptr inbounds %main._Ctype_struct___2, ptr %16, i32 0, i32 2 + %20 = getelementptr inbounds %main._Ctype_struct___2, ptr %16, i32 0, i32 3 + %21 = getelementptr inbounds %main._Ctype_struct___2, ptr %16, i32 0, i32 4 + store i32 1, ptr %17, align 4 + store i32 2, ptr %18, align 4 + store i32 3, ptr %19, align 4 + store i32 4, ptr %20, align 4 + store i32 5, ptr %21, align 4 + %22 = call i32 @main._Cfunc_test_structs(ptr %2, ptr %4, ptr %7, ptr %11, ptr %16) + %23 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 16) + %24 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %23, i64 0 + %25 = load ptr, ptr @_llgo_main._Ctype_int, align 8 + %26 = sext i32 %22 to i64 + %27 = inttoptr i64 %26 to ptr + %28 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %25, 0 + %29 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %28, ptr %27, 1 + store %"github.com/goplus/llgo/internal/runtime.eface" %29, ptr %24, align 8 + %30 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" undef, ptr %23, 0 + %31 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %30, i64 1, 1 + %32 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %31, i64 1, 2 + %33 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Println(%"github.com/goplus/llgo/internal/runtime.Slice" %32) + %34 = icmp ne i32 %22, 35 + br i1 %34, label %_llgo_1, label %_llgo_2 + +_llgo_1: ; preds = %_llgo_0 + %35 = load ptr, ptr @_llgo_string, align 8 + %36 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 16) + store %"github.com/goplus/llgo/internal/runtime.String" { ptr @3, i64 19 }, ptr %36, align 8 + %37 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %35, 0 + %38 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %37, ptr %36, 1 + call void @"github.com/goplus/llgo/internal/runtime.Panic"(%"github.com/goplus/llgo/internal/runtime.eface" %38) + unreachable + +_llgo_2: ; preds = %_llgo_0 + ret i32 0 +} + +declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64) + +declare void @syscall.init() + +declare void @fmt.init() + +declare void @"github.com/goplus/llgo/internal/runtime.init"() + +define void @"main.init$after"() { +_llgo_0: + %0 = call ptr @"github.com/goplus/llgo/internal/runtime.NewNamed"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @0, i64 15 }, i64 5, i64 4, i64 0, i64 0) + store ptr %0, ptr @_llgo_main._Ctype_int, align 8 + %1 = load ptr, ptr @_llgo_int32, align 8 + %2 = icmp eq ptr %1, null + br i1 %2, label %_llgo_1, label %_llgo_2 + +_llgo_1: ; preds = %_llgo_0 + %3 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 37) + store ptr %3, ptr @_llgo_int32, align 8 + br label %_llgo_2 + +_llgo_2: ; preds = %_llgo_1, %_llgo_0 + %4 = load ptr, ptr @_llgo_int32, align 8 + call void @"github.com/goplus/llgo/internal/runtime.InitNamed"(ptr %0, %"github.com/goplus/llgo/internal/runtime.String" { ptr @1, i64 4 }, %"github.com/goplus/llgo/internal/runtime.String" { ptr @2, i64 10 }, ptr %4, { ptr, i64, i64 } zeroinitializer, { ptr, i64, i64 } zeroinitializer) + %5 = load ptr, ptr @_llgo_string, align 8 + %6 = icmp eq ptr %5, null + br i1 %6, label %_llgo_3, label %_llgo_4 + +_llgo_3: ; preds = %_llgo_2 + %7 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 24) + store ptr %7, ptr @_llgo_string, align 8 + br label %_llgo_4 + +_llgo_4: ; preds = %_llgo_3, %_llgo_2 + ret void +} + +declare ptr @"github.com/goplus/llgo/internal/runtime.NewNamed"(%"github.com/goplus/llgo/internal/runtime.String", i64, i64, i64, i64) + +declare ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64) + +declare void @"github.com/goplus/llgo/internal/runtime.InitNamed"(ptr, %"github.com/goplus/llgo/internal/runtime.String", %"github.com/goplus/llgo/internal/runtime.String", ptr, %"github.com/goplus/llgo/internal/runtime.Slice", %"github.com/goplus/llgo/internal/runtime.Slice") + +declare { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Println(%"github.com/goplus/llgo/internal/runtime.Slice") + +declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64) + +declare void @"github.com/goplus/llgo/internal/runtime.Panic"(%"github.com/goplus/llgo/internal/runtime.eface") diff --git a/_demo/cgodefer/cgodefer.go b/cl/_testgo/cgodefer/cgodefer.go similarity index 100% rename from _demo/cgodefer/cgodefer.go rename to cl/_testgo/cgodefer/cgodefer.go diff --git a/cl/_testgo/cgodefer/out.ll b/cl/_testgo/cgodefer/out.ll new file mode 100644 index 000000000..1c8a0e797 --- /dev/null +++ b/cl/_testgo/cgodefer/out.ll @@ -0,0 +1 @@ +; \ No newline at end of file diff --git a/cl/_testgo/cgofull/cgofull.go b/cl/_testgo/cgofull/cgofull.go index 0380d127f..8315e20e8 100644 --- a/cl/_testgo/cgofull/cgofull.go +++ b/cl/_testgo/cgofull/cgofull.go @@ -103,8 +103,8 @@ import ( "fmt" "unsafe" - "github.com/goplus/llgo/_demo/cgofull/pymod1" - "github.com/goplus/llgo/_demo/cgofull/pymod2" + "github.com/goplus/llgo/cl/_testgo/cgofull/pymod1" + "github.com/goplus/llgo/cl/_testgo/cgofull/pymod2" ) //export go_callback diff --git a/_demo/cgomacro/cgomacro.go b/cl/_testgo/cgomacro/cgomacro.go similarity index 100% rename from _demo/cgomacro/cgomacro.go rename to cl/_testgo/cgomacro/cgomacro.go diff --git a/cl/_testgo/cgomacro/out.ll b/cl/_testgo/cgomacro/out.ll new file mode 100644 index 000000000..1c8a0e797 --- /dev/null +++ b/cl/_testgo/cgomacro/out.ll @@ -0,0 +1 @@ +; \ No newline at end of file diff --git a/_demo/cgopython/cgopython.go b/cl/_testgo/cgopython/cgopython.go similarity index 100% rename from _demo/cgopython/cgopython.go rename to cl/_testgo/cgopython/cgopython.go diff --git a/cl/_testgo/cgopython/out.ll b/cl/_testgo/cgopython/out.ll new file mode 100644 index 000000000..1c8a0e797 --- /dev/null +++ b/cl/_testgo/cgopython/out.ll @@ -0,0 +1 @@ +; \ No newline at end of file From c3407eac5e07951c65258a58755867d9eb66e1dc Mon Sep 17 00:00:00 2001 From: Li Jie Date: Wed, 27 Nov 2024 18:22:23 +0800 Subject: [PATCH 7/7] cgo: supports //export functions only calls in C --- cl/_testgo/cgofull/cgofull.go | 8 +++++ cl/compile.go | 56 ++++++++++------------------------- cl/import.go | 16 ++++++++-- internal/build/cgo.go | 52 +++++++++++++++++--------------- 4 files changed, 64 insertions(+), 68 deletions(-) diff --git a/cl/_testgo/cgofull/cgofull.go b/cl/_testgo/cgofull/cgofull.go index 8315e20e8..7d5fa45cd 100644 --- a/cl/_testgo/cgofull/cgofull.go +++ b/cl/_testgo/cgofull/cgofull.go @@ -94,8 +94,11 @@ static void test_callback(Cb cb) { printf("done\n"); } +extern int go_callback_not_use_in_go(int); + static void run_callback() { test_callback(c_callback); + test_callback(go_callback_not_use_in_go); } */ import "C" @@ -107,6 +110,11 @@ import ( "github.com/goplus/llgo/cl/_testgo/cgofull/pymod2" ) +//export go_callback_not_use_in_go +func go_callback_not_use_in_go(i C.int) C.int { + return i + 1 +} + //export go_callback func go_callback(i C.int) C.int { return i + 1 diff --git a/cl/compile.go b/cl/compile.go index eb4f1fcbf..62441e69e 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -110,10 +110,11 @@ type context struct { inCFunc bool skipall bool - cgoCalled bool - cgoArgs []llssa.Expr - cgoRet llssa.Expr - cgoFuncs map[string][]string + cgoCalled bool + cgoArgs []llssa.Expr + cgoRet llssa.Expr + cgoSymbols []string + cgoExports map[string]string } type pkgState byte @@ -415,14 +416,6 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, do callRuntimeInit(b, pkg) b.Call(pkg.FuncOf("main.init").Expr) } - fname := p.goProg.Fset.Position(block.Parent().Pos()).Filename - if p.cgoFuncs == nil { - p.cgoFuncs = make(map[string][]string) - } - var cgoFuncs []string - if funcs, ok := p.cgoFuncs[fname]; ok { - cgoFuncs = funcs - } fnName := block.Parent().Name() cgoReturned := false isCgoCfunc := isCgoCfunc(fnName) @@ -442,8 +435,7 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, do // load cgo function pointer varName := instr.X.Name() if instr.Op == token.MUL && strings.HasPrefix(varName, "_cgo_") { - cgoFuncs = append(cgoFuncs, varName) - p.cgoFuncs[fname] = cgoFuncs + p.cgoSymbols = append(p.cgoSymbols, varName) p.compileInstr(b, instr) } case *ssa.Call: @@ -925,13 +917,7 @@ func (p *context) compileValue(b llssa.Builder, v ssa.Value) llssa.Expr { varName := v.Name() val := p.varOf(b, v) if isCgoVar(varName) { - fname := p.fset.Position(v.Pos()).Filename - funcs, ok := p.cgoFuncs[fname] - if !ok { - funcs = make([]string, 0, 1) - } - funcs = append(funcs, val.Name()) - p.cgoFuncs[fname] = funcs + p.cgoSymbols = append(p.cgoSymbols, val.Name()) } if debugSymbols { pos := p.fset.Position(v.Pos()) @@ -1001,7 +987,7 @@ func NewPackage(prog llssa.Program, pkg *ssa.Package, files []*ast.File) (ret ll } // NewPackageEx compiles a Go package to LLVM IR package. -func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files []*ast.File) (ret llssa.Package, externs map[string][]string, err error) { +func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files []*ast.File) (ret llssa.Package, externs []string, err error) { pkgProg := pkg.Prog pkgTypes := pkg.Pkg oldTypes := pkgTypes @@ -1033,6 +1019,8 @@ func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files [ loaded: map[*types.Package]*pkgInfo{ types.Unsafe: {kind: PkgDeclOnly}, // TODO(xsw): PkgNoInit or PkgDeclOnly? }, + cgoExports: make(map[string]string), + cgoSymbols: make([]string, 0, 128), } ctx.initPyModule() ctx.initFiles(pkgPath, files) @@ -1066,25 +1054,11 @@ func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files [ ctx.initAfter = nil fn() } - externs = ctx.cgoFuncs - // TODO(lijie): read export name - for _, funcs := range externs { - for _, funcName := range funcs { - if strings.Contains(funcName, ".__cgo_") { - goFnName := strings.Replace(funcName, ".__cgo_", ".", 1) - idx := strings.LastIndex(funcName, ".__cgo_") - cfuncName := funcName[idx+len(".__cgo_"):] - v := ret.VarOf(funcName) - if fn := ret.FuncOf(goFnName); fn != nil { - // TODO(lijie): naive go:export, need better way from comment - fn.SetName(cfuncName) - // Replace symbol instead of static linking - v.ReplaceAllUsesWith(fn.Expr) - } else if fn := ret.FuncOf(cfuncName); fn != nil { - // Replace symbol instead of static linking - v.ReplaceAllUsesWith(fn.Expr) - } - } + externs = ctx.cgoSymbols + for fnName, exportName := range ctx.cgoExports { + fn := ret.FuncOf(fnName) + if fn != nil { + fn.SetName(exportName) } } return diff --git a/cl/import.go b/cl/import.go index 643950b91..8e163e9a4 100644 --- a/cl/import.go +++ b/cl/import.go @@ -246,9 +246,10 @@ func (p *context) initLinknameByDoc(doc *ast.CommentGroup, fullName, inPkgName s func (p *context) initLinkname(line string, f func(inPkgName string) (fullName string, isVar, ok bool)) { const ( - linkname = "//go:linkname " - llgolink = "//llgo:link " - llgolink2 = "// llgo:link " + linkname = "//go:linkname " + llgolink = "//llgo:link " + llgolink2 = "// llgo:link " + exportName = "//export " ) if strings.HasPrefix(line, linkname) { p.initLink(line, len(linkname), f) @@ -256,6 +257,15 @@ func (p *context) initLinkname(line string, f func(inPkgName string) (fullName s p.initLink(line, len(llgolink2), f) } else if strings.HasPrefix(line, llgolink) { p.initLink(line, len(llgolink), f) + } else if strings.HasPrefix(line, exportName) { + p.initCgoExport(line, len(exportName), f) + } +} + +func (p *context) initCgoExport(line string, prefix int, f func(inPkgName string) (fullName string, isVar, ok bool)) { + name := strings.TrimSpace(line[prefix:]) + if fullName, _, ok := f(name); ok { + p.cgoExports[fullName] = name } } diff --git a/internal/build/cgo.go b/internal/build/cgo.go index ac2b31e81..f53a6deee 100644 --- a/internal/build/cgo.go +++ b/internal/build/cgo.go @@ -53,7 +53,7 @@ static void* _Cmalloc(size_t size) { ` ) -func buildCgo(ctx *context, pkg *aPackage, files []*ast.File, externs map[string][]string, verbose bool) (cgoLdflags []string, err error) { +func buildCgo(ctx *context, pkg *aPackage, files []*ast.File, externs []string, verbose bool) (cgoLdflags []string, err error) { cfiles, preambles, cdecls, err := parseCgo_(pkg, files) if err != nil { return @@ -93,26 +93,24 @@ func buildCgo(ctx *context, pkg *aPackage, files []*ast.File, externs map[string re := regexp.MustCompile(`^(_cgo_[^_]+_(Cfunc|Cmacro)_)(.*)$`) cgoSymbols := make(map[string]string) mallocFix := false - for _, symbols := range externs { - for _, symbolName := range symbols { - lastPart := symbolName - lastDot := strings.LastIndex(symbolName, ".") - if lastDot != -1 { - lastPart = symbolName[lastDot+1:] - } - if strings.HasPrefix(lastPart, "__cgo_") { - // func ptr var: main.__cgo_func_name - cgoSymbols[symbolName] = lastPart - } else if m := re.FindStringSubmatch(symbolName); len(m) > 0 { - prefix := m[1] // _cgo_hash_(Cfunc|Cmacro)_ - name := m[3] // remaining part - cgoSymbols[symbolName] = name - // fix missing _cgo_9113e32b6599_Cfunc__Cmalloc - if !mallocFix && m[2] == "Cfunc" { - mallocName := prefix + "_Cmalloc" - cgoSymbols[mallocName] = "_Cmalloc" - mallocFix = true - } + for _, symbolName := range externs { + lastPart := symbolName + lastDot := strings.LastIndex(symbolName, ".") + if lastDot != -1 { + lastPart = symbolName[lastDot+1:] + } + if strings.HasPrefix(lastPart, "__cgo_") { + // func ptr var: main.__cgo_func_name + cgoSymbols[symbolName] = lastPart + } else if m := re.FindStringSubmatch(symbolName); len(m) > 0 { + prefix := m[1] // _cgo_hash_(Cfunc|Cmacro)_ + name := m[3] // remaining part + cgoSymbols[symbolName] = name + // fix missing _cgo_9113e32b6599_Cfunc__Cmalloc + if !mallocFix && m[2] == "Cfunc" { + mallocName := prefix + "_Cmalloc" + cgoSymbols[mallocName] = "_Cmalloc" + mallocFix = true } } } @@ -170,10 +168,16 @@ func genExternDeclsByClang(pkg *aPackage, src string, cflags []string, cgoSymbol var toRemove []string for cgoName, symbolName := range cgoSymbols { if strings.HasPrefix(symbolName, "__cgo_") { - cfuncName := symbolName[len("__cgo_"):] - cfn := pkg.LPkg.NewFunc(cfuncName, types.NewSignature(nil, nil, nil, false), llssa.InC) + gofuncName := strings.Replace(cgoName, ".__cgo_", ".", 1) + gofn := pkg.LPkg.FuncOf(gofuncName) cgoVar := pkg.LPkg.VarOf(cgoName) - cgoVar.ReplaceAllUsesWith(cfn.Expr) + if gofn != nil { + cgoVar.ReplaceAllUsesWith(gofn.Expr) + } else { + cfuncName := symbolName[len("__cgo_"):] + cfn := pkg.LPkg.NewFunc(cfuncName, types.NewSignatureType(nil, nil, nil, nil, nil, false), llssa.InC) + cgoVar.ReplaceAllUsesWith(cfn.Expr) + } toRemove = append(toRemove, cgoName) } else { usePtr := ""