| |
| |
| |
|
|
| package main |
|
|
| import ( |
| "bytes" |
| "cmd/internal/pkgpath" |
| "debug/elf" |
| "debug/macho" |
| "debug/pe" |
| "fmt" |
| "go/ast" |
| "go/printer" |
| "go/token" |
| "internal/xcoff" |
| "io" |
| "os" |
| "os/exec" |
| "path/filepath" |
| "regexp" |
| "sort" |
| "strings" |
| "unicode" |
| ) |
|
|
| var ( |
| conf = printer.Config{Mode: printer.SourcePos, Tabwidth: 8} |
| noSourceConf = printer.Config{Tabwidth: 8} |
| ) |
|
|
| |
| func (p *Package) writeDefs() { |
| var fgo2, fc io.Writer |
| f := creat(*objDir + "_cgo_gotypes.go") |
| defer f.Close() |
| fgo2 = f |
| if *gccgo { |
| f := creat(*objDir + "_cgo_defun.c") |
| defer f.Close() |
| fc = f |
| } |
| fm := creat(*objDir + "_cgo_main.c") |
|
|
| var gccgoInit strings.Builder |
|
|
| if !*gccgo { |
| for _, arg := range p.LdFlags { |
| fmt.Fprintf(fgo2, "//go:cgo_ldflag %q\n", arg) |
| } |
| } else { |
| fflg := creat(*objDir + "_cgo_flags") |
| for _, arg := range p.LdFlags { |
| fmt.Fprintf(fflg, "_CGO_LDFLAGS=%s\n", arg) |
| } |
| fflg.Close() |
| } |
|
|
| |
| fmt.Fprintf(fm, "#include <stddef.h>\n") |
| fmt.Fprintf(fm, "int main(int argc __attribute__((unused)), char **argv __attribute__((unused))) { return 0; }\n") |
| if *importRuntimeCgo { |
| fmt.Fprintf(fm, "void crosscall2(void(*fn)(void*) __attribute__((unused)), void *a __attribute__((unused)), int c __attribute__((unused)), size_t ctxt __attribute__((unused))) { }\n") |
| fmt.Fprintf(fm, "size_t _cgo_wait_runtime_init_done(void) { return 0; }\n") |
| fmt.Fprintf(fm, "void _cgo_release_context(size_t ctxt __attribute__((unused))) { }\n") |
| fmt.Fprintf(fm, "char* _cgo_topofstack(void) { return (char*)0; }\n") |
| } else { |
| |
| |
| fmt.Fprintf(fm, "void crosscall2(void(*fn)(void*), void *a, int c, size_t ctxt);\n") |
| fmt.Fprintf(fm, "size_t _cgo_wait_runtime_init_done(void);\n") |
| fmt.Fprintf(fm, "void _cgo_release_context(size_t);\n") |
| } |
| fmt.Fprintf(fm, "void _cgo_allocate(void *a __attribute__((unused)), int c __attribute__((unused))) { }\n") |
| fmt.Fprintf(fm, "void _cgo_panic(void *a __attribute__((unused)), int c __attribute__((unused))) { }\n") |
| fmt.Fprintf(fm, "void _cgo_reginit(void) { }\n") |
|
|
| |
| |
| |
| fmt.Fprintf(fgo2, "// Code generated by cmd/cgo; DO NOT EDIT.\n\n") |
| fmt.Fprintf(fgo2, "package %s\n\n", p.PackageName) |
| fmt.Fprintf(fgo2, "import \"unsafe\"\n\n") |
| if *importSyscall { |
| fmt.Fprintf(fgo2, "import \"syscall\"\n\n") |
| } |
| if *importRuntimeCgo { |
| if !*gccgoDefineCgoIncomplete { |
| fmt.Fprintf(fgo2, "import _cgopackage \"runtime/cgo\"\n\n") |
| fmt.Fprintf(fgo2, "type _ _cgopackage.Incomplete\n") |
| } else { |
| fmt.Fprintf(fgo2, "//go:notinheap\n") |
| fmt.Fprintf(fgo2, "type _cgopackage_Incomplete struct{ _ struct{ _ struct{} } }\n") |
| } |
| } |
| if *importSyscall { |
| fmt.Fprintf(fgo2, "var _ syscall.Errno\n") |
| } |
| fmt.Fprintf(fgo2, "func _Cgo_ptr(ptr unsafe.Pointer) unsafe.Pointer { return ptr }\n\n") |
|
|
| if !*gccgo { |
| fmt.Fprintf(fgo2, "//go:linkname _Cgo_always_false runtime.cgoAlwaysFalse\n") |
| fmt.Fprintf(fgo2, "var _Cgo_always_false bool\n") |
| fmt.Fprintf(fgo2, "//go:linkname _Cgo_use runtime.cgoUse\n") |
| fmt.Fprintf(fgo2, "func _Cgo_use(interface{})\n") |
| fmt.Fprintf(fgo2, "//go:linkname _Cgo_keepalive runtime.cgoKeepAlive\n") |
| fmt.Fprintf(fgo2, "//go:noescape\n") |
| fmt.Fprintf(fgo2, "func _Cgo_keepalive(interface{})\n") |
| } |
| fmt.Fprintf(fgo2, "//go:linkname _Cgo_no_callback runtime.cgoNoCallback\n") |
| fmt.Fprintf(fgo2, "func _Cgo_no_callback(bool)\n") |
|
|
| typedefNames := make([]string, 0, len(typedef)) |
| for name := range typedef { |
| if name == "_Ctype_void" { |
| |
| |
| continue |
| } |
| typedefNames = append(typedefNames, name) |
| } |
| sort.Strings(typedefNames) |
| for _, name := range typedefNames { |
| def := typedef[name] |
| fmt.Fprintf(fgo2, "type %s ", name) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| var buf bytes.Buffer |
| noSourceConf.Fprint(&buf, fset, def.Go) |
| if bytes.HasPrefix(buf.Bytes(), []byte("_Ctype_")) || |
| strings.HasPrefix(name, "_Ctype_enum_") || |
| strings.HasPrefix(name, "_Ctype_union_") { |
| |
| fmt.Fprintf(fgo2, "= ") |
| } |
| fmt.Fprintf(fgo2, "%s", buf.Bytes()) |
| fmt.Fprintf(fgo2, "\n\n") |
| } |
| if *gccgo { |
| fmt.Fprintf(fgo2, "type _Ctype_void byte\n") |
| } else { |
| fmt.Fprintf(fgo2, "type _Ctype_void [0]byte\n") |
| } |
|
|
| if *gccgo { |
| fmt.Fprint(fgo2, gccgoGoProlog) |
| fmt.Fprint(fc, p.cPrologGccgo()) |
| } else { |
| fmt.Fprint(fgo2, goProlog) |
| } |
|
|
| if fc != nil { |
| fmt.Fprintf(fc, "#line 1 \"cgo-generated-wrappers\"\n") |
| } |
| if fm != nil { |
| fmt.Fprintf(fm, "#line 1 \"cgo-generated-wrappers\"\n") |
| } |
|
|
| gccgoSymbolPrefix := p.gccgoSymbolPrefix() |
|
|
| cVars := make(map[string]bool) |
| for _, key := range nameKeys(p.Name) { |
| n := p.Name[key] |
| if !n.IsVar() { |
| continue |
| } |
|
|
| if !cVars[n.C] { |
| if *gccgo { |
| fmt.Fprintf(fc, "extern byte *%s;\n", n.C) |
| } else { |
| |
| |
| |
| |
| |
| |
| if n.Kind == "fpvar" { |
| fmt.Fprintf(fm, "extern void %s();\n", n.C) |
| } else { |
| fmt.Fprintf(fm, "extern char %s[];\n", n.C) |
| fmt.Fprintf(fm, "void *_cgohack_%s = %s;\n\n", n.C, n.C) |
| } |
| fmt.Fprintf(fgo2, "//go:linkname __cgo_%s %s\n", n.C, n.C) |
| fmt.Fprintf(fgo2, "//go:cgo_import_static %s\n", n.C) |
| fmt.Fprintf(fgo2, "var __cgo_%s byte\n", n.C) |
| } |
| cVars[n.C] = true |
| } |
|
|
| var node ast.Node |
| if n.Kind == "var" { |
| node = &ast.StarExpr{X: n.Type.Go} |
| } else if n.Kind == "fpvar" { |
| node = n.Type.Go |
| } else { |
| panic(fmt.Errorf("invalid var kind %q", n.Kind)) |
| } |
| if *gccgo { |
| fmt.Fprintf(fc, `extern void *%s __asm__("%s.%s");`, n.Mangle, gccgoSymbolPrefix, gccgoToSymbol(n.Mangle)) |
| fmt.Fprintf(&gccgoInit, "\t%s = &%s;\n", n.Mangle, n.C) |
| fmt.Fprintf(fc, "\n") |
| } |
|
|
| fmt.Fprintf(fgo2, "var %s ", n.Mangle) |
| conf.Fprint(fgo2, fset, node) |
| if !*gccgo { |
| fmt.Fprintf(fgo2, " = (") |
| conf.Fprint(fgo2, fset, node) |
| fmt.Fprintf(fgo2, ")(unsafe.Pointer(&__cgo_%s))", n.C) |
| } |
| fmt.Fprintf(fgo2, "\n") |
| } |
| if *gccgo { |
| fmt.Fprintf(fc, "\n") |
| } |
|
|
| for _, key := range nameKeys(p.Name) { |
| n := p.Name[key] |
| if n.Const != "" { |
| fmt.Fprintf(fgo2, "const %s = %s\n", n.Mangle, n.Const) |
| } |
| } |
| fmt.Fprintf(fgo2, "\n") |
|
|
| callsMalloc := false |
| for _, key := range nameKeys(p.Name) { |
| n := p.Name[key] |
| if n.FuncType != nil { |
| p.writeDefsFunc(fgo2, n, &callsMalloc) |
| } |
| } |
|
|
| fgcc := creat(*objDir + "_cgo_export.c") |
| fgcch := creat(*objDir + "_cgo_export.h") |
| if *gccgo { |
| p.writeGccgoExports(fgo2, fm, fgcc, fgcch) |
| } else { |
| p.writeExports(fgo2, fm, fgcc, fgcch) |
| } |
|
|
| if callsMalloc && !*gccgo { |
| fmt.Fprint(fgo2, strings.ReplaceAll(cMallocDefGo, "PREFIX", cPrefix)) |
| fmt.Fprint(fgcc, strings.ReplaceAll(strings.Replace(cMallocDefC, "PREFIX", cPrefix, -1), "PACKED", p.packedAttribute())) |
| } |
|
|
| if err := fgcc.Close(); err != nil { |
| fatalf("%s", err) |
| } |
| if err := fgcch.Close(); err != nil { |
| fatalf("%s", err) |
| } |
|
|
| if *exportHeader != "" && len(p.ExpFunc) > 0 { |
| fexp := creat(*exportHeader) |
| fgcch, err := os.Open(*objDir + "_cgo_export.h") |
| if err != nil { |
| fatalf("%s", err) |
| } |
| defer fgcch.Close() |
| _, err = io.Copy(fexp, fgcch) |
| if err != nil { |
| fatalf("%s", err) |
| } |
| if err = fexp.Close(); err != nil { |
| fatalf("%s", err) |
| } |
| } |
|
|
| init := gccgoInit.String() |
| if init != "" { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| fmt.Fprintln(fc, "static void init(void) __attribute__ ((constructor, no_split_stack));") |
| fmt.Fprintln(fc, "static void init(void) {") |
| fmt.Fprint(fc, init) |
| fmt.Fprintln(fc, "}") |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func elfImportedSymbols(f *elf.File) []elf.ImportedSymbol { |
| syms, _ := f.DynamicSymbols() |
| var imports []elf.ImportedSymbol |
| for _, s := range syms { |
| if (elf.ST_BIND(s.Info) == elf.STB_GLOBAL || elf.ST_BIND(s.Info) == elf.STB_WEAK) && s.Section == elf.SHN_UNDEF { |
| imports = append(imports, elf.ImportedSymbol{ |
| Name: s.Name, |
| Library: s.Library, |
| Version: s.Version, |
| }) |
| } |
| } |
| return imports |
| } |
|
|
| func dynimport(obj string) { |
| stdout := os.Stdout |
| if *dynout != "" { |
| f, err := os.Create(*dynout) |
| if err != nil { |
| fatalf("%s", err) |
| } |
| defer func() { |
| if err = f.Close(); err != nil { |
| fatalf("error closing %s: %v", *dynout, err) |
| } |
| }() |
|
|
| stdout = f |
| } |
|
|
| fmt.Fprintf(stdout, "package %s\n", *dynpackage) |
|
|
| if f, err := elf.Open(obj); err == nil { |
| defer f.Close() |
| if *dynlinker { |
| |
| if sec := f.Section(".interp"); sec != nil { |
| if data, err := sec.Data(); err == nil && len(data) > 1 { |
| |
| fmt.Fprintf(stdout, "//go:cgo_dynamic_linker %q\n", string(data[:len(data)-1])) |
| } |
| } |
| } |
| sym := elfImportedSymbols(f) |
| for _, s := range sym { |
| targ := s.Name |
| if s.Version != "" { |
| targ += "#" + s.Version |
| } |
| checkImportSymName(s.Name) |
| checkImportSymName(targ) |
| fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s.Name, targ, s.Library) |
| } |
| lib, _ := f.ImportedLibraries() |
| for _, l := range lib { |
| fmt.Fprintf(stdout, "//go:cgo_import_dynamic _ _ %q\n", l) |
| } |
| return |
| } |
|
|
| if f, err := macho.Open(obj); err == nil { |
| defer f.Close() |
| sym, _ := f.ImportedSymbols() |
| for _, s := range sym { |
| s = strings.TrimPrefix(s, "_") |
| checkImportSymName(s) |
| fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s, s, "") |
| } |
| lib, _ := f.ImportedLibraries() |
| for _, l := range lib { |
| fmt.Fprintf(stdout, "//go:cgo_import_dynamic _ _ %q\n", l) |
| } |
| return |
| } |
|
|
| if f, err := pe.Open(obj); err == nil { |
| defer f.Close() |
| sym, _ := f.ImportedSymbols() |
| for _, s := range sym { |
| ss := strings.Split(s, ":") |
| name := strings.Split(ss[0], "@")[0] |
| checkImportSymName(name) |
| checkImportSymName(ss[0]) |
| fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", name, ss[0], strings.ToLower(ss[1])) |
| } |
| return |
| } |
|
|
| if f, err := xcoff.Open(obj); err == nil { |
| defer f.Close() |
| sym, err := f.ImportedSymbols() |
| if err != nil { |
| fatalf("cannot load imported symbols from XCOFF file %s: %v", obj, err) |
| } |
| for _, s := range sym { |
| if s.Name == "runtime_rt0_go" || s.Name == "_rt0_ppc64_aix_lib" { |
| |
| |
| |
| continue |
| } |
| checkImportSymName(s.Name) |
| fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s.Name, s.Name, s.Library) |
| } |
| lib, err := f.ImportedLibraries() |
| if err != nil { |
| fatalf("cannot load imported libraries from XCOFF file %s: %v", obj, err) |
| } |
| for _, l := range lib { |
| fmt.Fprintf(stdout, "//go:cgo_import_dynamic _ _ %q\n", l) |
| } |
| return |
| } |
|
|
| fatalf("cannot parse %s as ELF, Mach-O, PE or XCOFF", obj) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| func checkImportSymName(s string) { |
| for _, c := range s { |
| if !unicode.IsGraphic(c) || unicode.IsSpace(c) { |
| fatalf("dynamic symbol %q contains unsupported character", s) |
| } |
| } |
| if strings.Contains(s, "//") || strings.Contains(s, "/*") { |
| fatalf("dynamic symbol %q contains Go comment", s) |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| func (p *Package) structType(n *Name) (string, int64) { |
| |
| |
| |
| |
| |
| stripConst := func(s string) string { |
| i := strings.LastIndex(s, "const") |
| if i == -1 { |
| return s |
| } |
|
|
| |
| if r, ok := strings.CutSuffix(s, "const"); ok { |
| return strings.TrimSpace(r) |
| } |
|
|
| var nonConst []string |
| for _, f := range strings.Fields(s[i:]) { |
| switch f { |
| case "const": |
| case "restrict", "volatile": |
| nonConst = append(nonConst, f) |
| default: |
| return s |
| } |
| } |
|
|
| return strings.TrimSpace(s[:i]) + " " + strings.Join(nonConst, " ") |
| } |
|
|
| var buf strings.Builder |
| fmt.Fprint(&buf, "struct {\n") |
| off := int64(0) |
| for i, t := range n.FuncType.Params { |
| if off%t.Align != 0 { |
| pad := t.Align - off%t.Align |
| fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad) |
| off += pad |
| } |
| c := t.Typedef |
| if c == "" { |
| c = stripConst(t.C.String()) |
| } |
| fmt.Fprintf(&buf, "\t\t%s p%d;\n", c, i) |
| off += t.Size |
| } |
| if off%p.PtrSize != 0 { |
| pad := p.PtrSize - off%p.PtrSize |
| fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad) |
| off += pad |
| } |
| if t := n.FuncType.Result; t != nil { |
| if off%t.Align != 0 { |
| pad := t.Align - off%t.Align |
| fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad) |
| off += pad |
| } |
| fmt.Fprintf(&buf, "\t\t%s r;\n", stripConst(t.C.String())) |
| off += t.Size |
| } |
| if off%p.PtrSize != 0 { |
| pad := p.PtrSize - off%p.PtrSize |
| fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad) |
| off += pad |
| } |
| if off == 0 { |
| fmt.Fprintf(&buf, "\t\tchar unused;\n") |
| } |
| fmt.Fprintf(&buf, "\t}") |
| return buf.String(), off |
| } |
|
|
| func (p *Package) writeDefsFunc(fgo2 io.Writer, n *Name, callsMalloc *bool) { |
| name := n.Go |
| gtype := n.FuncType.Go |
| void := gtype.Results == nil || len(gtype.Results.List) == 0 |
| if n.AddError { |
| |
| |
| err := &ast.Field{Type: ast.NewIdent("error")} |
| l := gtype.Results.List |
| if len(l) == 0 { |
| l = []*ast.Field{err} |
| } else { |
| l = []*ast.Field{l[0], err} |
| } |
| t := new(ast.FuncType) |
| *t = *gtype |
| t.Results = &ast.FieldList{List: l} |
| gtype = t |
| } |
|
|
| |
| d := &ast.FuncDecl{ |
| Name: ast.NewIdent(n.Mangle), |
| Type: gtype, |
| } |
|
|
| |
| inProlog := builtinDefs[name] != "" |
| cname := fmt.Sprintf("_cgo%s%s", cPrefix, n.Mangle) |
| paramnames := []string(nil) |
| if d.Type.Params != nil { |
| for i, param := range d.Type.Params.List { |
| paramName := fmt.Sprintf("p%d", i) |
| param.Names = []*ast.Ident{ast.NewIdent(paramName)} |
| paramnames = append(paramnames, paramName) |
| } |
| } |
|
|
| if *gccgo { |
| |
| fmt.Fprint(fgo2, "\n") |
| conf.Fprint(fgo2, fset, d) |
| fmt.Fprint(fgo2, " {\n") |
| if !inProlog { |
| fmt.Fprint(fgo2, "\tdefer syscall.CgocallDone()\n") |
| fmt.Fprint(fgo2, "\tsyscall.Cgocall()\n") |
| } |
| if n.AddError { |
| fmt.Fprint(fgo2, "\tsyscall.SetErrno(0)\n") |
| } |
| fmt.Fprint(fgo2, "\t") |
| if !void { |
| fmt.Fprint(fgo2, "r := ") |
| } |
| fmt.Fprintf(fgo2, "%s(%s)\n", cname, strings.Join(paramnames, ", ")) |
|
|
| if n.AddError { |
| fmt.Fprint(fgo2, "\te := syscall.GetErrno()\n") |
| fmt.Fprint(fgo2, "\tif e != 0 {\n") |
| fmt.Fprint(fgo2, "\t\treturn ") |
| if !void { |
| fmt.Fprint(fgo2, "r, ") |
| } |
| fmt.Fprint(fgo2, "e\n") |
| fmt.Fprint(fgo2, "\t}\n") |
| fmt.Fprint(fgo2, "\treturn ") |
| if !void { |
| fmt.Fprint(fgo2, "r, ") |
| } |
| fmt.Fprint(fgo2, "nil\n") |
| } else if !void { |
| fmt.Fprint(fgo2, "\treturn r\n") |
| } |
|
|
| fmt.Fprint(fgo2, "}\n") |
|
|
| |
| fmt.Fprintf(fgo2, "//extern %s\n", cname) |
| d.Name = ast.NewIdent(cname) |
| if n.AddError { |
| l := d.Type.Results.List |
| d.Type.Results.List = l[:len(l)-1] |
| } |
| conf.Fprint(fgo2, fset, d) |
| fmt.Fprint(fgo2, "\n") |
|
|
| return |
| } |
|
|
| if inProlog { |
| fmt.Fprint(fgo2, builtinDefs[name]) |
| if strings.Contains(builtinDefs[name], "_cgo_cmalloc") { |
| *callsMalloc = true |
| } |
| return |
| } |
|
|
| |
| fmt.Fprintf(fgo2, "//go:cgo_import_static %s\n", cname) |
| fmt.Fprintf(fgo2, "//go:linkname __cgofn_%s %s\n", cname, cname) |
| fmt.Fprintf(fgo2, "var __cgofn_%s byte\n", cname) |
| fmt.Fprintf(fgo2, "var %s = unsafe.Pointer(&__cgofn_%s)\n", cname, cname) |
|
|
| nret := 0 |
| if !void { |
| d.Type.Results.List[0].Names = []*ast.Ident{ast.NewIdent("r1")} |
| nret = 1 |
| } |
| if n.AddError { |
| d.Type.Results.List[nret].Names = []*ast.Ident{ast.NewIdent("r2")} |
| } |
|
|
| fmt.Fprint(fgo2, "\n") |
| fmt.Fprint(fgo2, "//go:cgo_unsafe_args\n") |
| conf.Fprint(fgo2, fset, d) |
| fmt.Fprint(fgo2, " {\n") |
|
|
| |
| arg := "0" |
| if len(paramnames) > 0 { |
| arg = "uintptr(unsafe.Pointer(&p0))" |
| } else if !void { |
| arg = "uintptr(unsafe.Pointer(&r1))" |
| } |
|
|
| noCallback := p.noCallbacks[n.C] |
| if noCallback { |
| |
| fmt.Fprintf(fgo2, "\t_Cgo_no_callback(true)\n") |
| } |
|
|
| prefix := "" |
| if n.AddError { |
| prefix = "errno := " |
| } |
| fmt.Fprintf(fgo2, "\t%s_cgo_runtime_cgocall(%s, %s)\n", prefix, cname, arg) |
| if n.AddError { |
| fmt.Fprintf(fgo2, "\tif errno != 0 { r2 = syscall.Errno(errno) }\n") |
| } |
| if noCallback { |
| fmt.Fprintf(fgo2, "\t_Cgo_no_callback(false)\n") |
| } |
|
|
| |
| |
| |
| touchFunc := "_Cgo_use" |
| if p.noEscapes[n.C] && p.noCallbacks[n.C] { |
| touchFunc = "_Cgo_keepalive" |
| } |
|
|
| if len(paramnames) > 0 { |
| fmt.Fprintf(fgo2, "\tif _Cgo_always_false {\n") |
| for _, name := range paramnames { |
| fmt.Fprintf(fgo2, "\t\t%s(%s)\n", touchFunc, name) |
| } |
| fmt.Fprintf(fgo2, "\t}\n") |
| } |
|
|
| fmt.Fprintf(fgo2, "\treturn\n") |
| fmt.Fprintf(fgo2, "}\n") |
| } |
|
|
| |
| func (p *Package) writeOutput(f *File, srcfile string) { |
| base := srcfile |
| base = strings.TrimSuffix(base, ".go") |
| base = filepath.Base(base) |
| fgo1 := creat(*objDir + base + ".cgo1.go") |
| fgcc := creat(*objDir + base + ".cgo2.c") |
|
|
| p.GoFiles = append(p.GoFiles, base+".cgo1.go") |
| p.GccFiles = append(p.GccFiles, base+".cgo2.c") |
|
|
| |
| fmt.Fprintf(fgo1, "// Code generated by cmd/cgo; DO NOT EDIT.\n\n") |
| if strings.ContainsAny(srcfile, "\r\n") { |
| |
| |
| fatalf("internal error: writeOutput: srcfile contains unexpected newline character: %q", srcfile) |
| } |
| fmt.Fprintf(fgo1, "//line %s:1:1\n", srcfile) |
| fgo1.Write(f.Edit.Bytes()) |
|
|
| |
| |
| fmt.Fprintf(fgcc, "%s\n", builtinProlog) |
| fmt.Fprintf(fgcc, "%s\n", f.Preamble) |
| fmt.Fprintf(fgcc, "%s\n", gccProlog) |
| fmt.Fprintf(fgcc, "%s\n", tsanProlog) |
| fmt.Fprintf(fgcc, "%s\n", msanProlog) |
|
|
| for _, key := range nameKeys(f.Name) { |
| n := f.Name[key] |
| if n.FuncType != nil { |
| p.writeOutputFunc(fgcc, n) |
| } |
| } |
|
|
| fgo1.Close() |
| fgcc.Close() |
| } |
|
|
| |
| |
| |
| func fixGo(name string) string { |
| if name == "_CMalloc" { |
| return "malloc" |
| } |
| return name |
| } |
|
|
| var isBuiltin = map[string]bool{ |
| "_Cfunc_CString": true, |
| "_Cfunc_CBytes": true, |
| "_Cfunc_GoString": true, |
| "_Cfunc_GoStringN": true, |
| "_Cfunc_GoBytes": true, |
| "_Cfunc__CMalloc": true, |
| } |
|
|
| func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) { |
| name := n.Mangle |
| if isBuiltin[name] || p.Written[name] { |
| |
| |
| return |
| } |
| p.Written[name] = true |
|
|
| if *gccgo { |
| p.writeGccgoOutputFunc(fgcc, n) |
| return |
| } |
|
|
| ctype, _ := p.structType(n) |
|
|
| |
| |
| fmt.Fprintf(fgcc, "CGO_NO_SANITIZE_THREAD\n") |
| if n.AddError { |
| fmt.Fprintf(fgcc, "int\n") |
| } else { |
| fmt.Fprintf(fgcc, "void\n") |
| } |
| fmt.Fprintf(fgcc, "_cgo%s%s(void *v)\n", cPrefix, n.Mangle) |
| fmt.Fprintf(fgcc, "{\n") |
| if n.AddError { |
| fmt.Fprintf(fgcc, "\tint _cgo_errno;\n") |
| } |
| |
| |
| |
| tr := n.FuncType.Result |
| if (n.Kind != "macro" && len(n.FuncType.Params) > 0) || tr != nil { |
| fmt.Fprintf(fgcc, "\t%s %v *_cgo_a = v;\n", ctype, p.packedAttribute()) |
| } |
| if tr != nil { |
| |
| fmt.Fprintf(fgcc, "\tchar *_cgo_stktop = _cgo_topofstack();\n") |
| fmt.Fprintf(fgcc, "\t__typeof__(_cgo_a->r) _cgo_r;\n") |
| } |
| fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n") |
| if n.AddError { |
| fmt.Fprintf(fgcc, "\terrno = 0;\n") |
| } |
| fmt.Fprintf(fgcc, "\t") |
| if tr != nil { |
| fmt.Fprintf(fgcc, "_cgo_r = ") |
| if c := tr.C.String(); c[len(c)-1] == '*' { |
| fmt.Fprint(fgcc, "(__typeof__(_cgo_a->r)) ") |
| } |
| } |
| if n.Kind == "macro" { |
| fmt.Fprintf(fgcc, "%s;\n", n.C) |
| } else { |
| fmt.Fprintf(fgcc, "%s(", n.C) |
| for i := range n.FuncType.Params { |
| if i > 0 { |
| fmt.Fprintf(fgcc, ", ") |
| } |
| fmt.Fprintf(fgcc, "_cgo_a->p%d", i) |
| } |
| fmt.Fprintf(fgcc, ");\n") |
| } |
| if n.AddError { |
| fmt.Fprintf(fgcc, "\t_cgo_errno = errno;\n") |
| } |
| fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n") |
| if tr != nil { |
| |
| |
| fmt.Fprintf(fgcc, "\t_cgo_a = (void*)((char*)_cgo_a + (_cgo_topofstack() - _cgo_stktop));\n") |
| |
| fmt.Fprintf(fgcc, "\t_cgo_a->r = _cgo_r;\n") |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| fmt.Fprintf(fgcc, "\t_cgo_msan_write(&_cgo_a->r, sizeof(_cgo_a->r));\n") |
| } |
| if n.AddError { |
| fmt.Fprintf(fgcc, "\treturn _cgo_errno;\n") |
| } |
| fmt.Fprintf(fgcc, "}\n") |
| fmt.Fprintf(fgcc, "\n") |
| } |
|
|
| |
| |
| |
| |
| |
| func (p *Package) writeGccgoOutputFunc(fgcc *os.File, n *Name) { |
| fmt.Fprintf(fgcc, "CGO_NO_SANITIZE_THREAD\n") |
| if t := n.FuncType.Result; t != nil { |
| fmt.Fprintf(fgcc, "%s\n", t.C.String()) |
| } else { |
| fmt.Fprintf(fgcc, "void\n") |
| } |
| fmt.Fprintf(fgcc, "_cgo%s%s(", cPrefix, n.Mangle) |
| for i, t := range n.FuncType.Params { |
| if i > 0 { |
| fmt.Fprintf(fgcc, ", ") |
| } |
| c := t.Typedef |
| if c == "" { |
| c = t.C.String() |
| } |
| fmt.Fprintf(fgcc, "%s p%d", c, i) |
| } |
| fmt.Fprintf(fgcc, ")\n") |
| fmt.Fprintf(fgcc, "{\n") |
| if t := n.FuncType.Result; t != nil { |
| fmt.Fprintf(fgcc, "\t%s _cgo_r;\n", t.C.String()) |
| } |
| fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n") |
| fmt.Fprintf(fgcc, "\t") |
| if t := n.FuncType.Result; t != nil { |
| fmt.Fprintf(fgcc, "_cgo_r = ") |
| |
| if c := t.C.String(); c[len(c)-1] == '*' { |
| fmt.Fprintf(fgcc, "(void*)") |
| } |
| } |
| if n.Kind == "macro" { |
| fmt.Fprintf(fgcc, "%s;\n", n.C) |
| } else { |
| fmt.Fprintf(fgcc, "%s(", n.C) |
| for i := range n.FuncType.Params { |
| if i > 0 { |
| fmt.Fprintf(fgcc, ", ") |
| } |
| fmt.Fprintf(fgcc, "p%d", i) |
| } |
| fmt.Fprintf(fgcc, ");\n") |
| } |
| fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n") |
| if t := n.FuncType.Result; t != nil { |
| fmt.Fprintf(fgcc, "\treturn ") |
| |
| |
| if c := t.C.String(); c[len(c)-1] == '*' { |
| fmt.Fprintf(fgcc, "(void*)") |
| } |
| fmt.Fprintf(fgcc, "_cgo_r;\n") |
| } |
| fmt.Fprintf(fgcc, "}\n") |
| fmt.Fprintf(fgcc, "\n") |
| } |
|
|
| |
| |
| |
| |
| |
| func (p *Package) packedAttribute() string { |
| s := "__attribute__((__packed__" |
| if !p.GccIsClang && (goarch == "amd64" || goarch == "386") { |
| s += ", __gcc_struct__" |
| } |
| return s + "))" |
| } |
|
|
| |
| |
| |
| |
| |
| func exportParamName(param string, position int) string { |
| if param == "" { |
| return fmt.Sprintf("p%d", position) |
| } |
|
|
| pname := param |
|
|
| for i := 0; i < len(param); i++ { |
| if param[i] > unicode.MaxASCII { |
| pname = fmt.Sprintf("p%d", position) |
| break |
| } |
| } |
|
|
| return pname |
| } |
|
|
| |
| |
| func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) { |
| p.writeExportHeader(fgcch) |
|
|
| fmt.Fprintf(fgcc, "/* Code generated by cmd/cgo; DO NOT EDIT. */\n\n") |
| fmt.Fprintf(fgcc, "#include <stdlib.h>\n") |
| fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n\n") |
|
|
| |
| |
| |
| fmt.Fprintf(fgcc, "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n") |
| fmt.Fprintf(fgcc, "#pragma GCC diagnostic ignored \"-Wpragmas\"\n") |
| fmt.Fprintf(fgcc, "#pragma GCC diagnostic ignored \"-Waddress-of-packed-member\"\n") |
| fmt.Fprintf(fgcc, "#pragma GCC diagnostic ignored \"-Wunknown-warning-option\"\n") |
| fmt.Fprintf(fgcc, "#pragma GCC diagnostic ignored \"-Wunaligned-access\"\n") |
|
|
| fmt.Fprintf(fgcc, "extern void crosscall2(void (*fn)(void *), void *, int, size_t);\n") |
| fmt.Fprintf(fgcc, "extern size_t _cgo_wait_runtime_init_done(void);\n") |
| fmt.Fprintf(fgcc, "extern void _cgo_release_context(size_t);\n\n") |
| fmt.Fprintf(fgcc, "extern char* _cgo_topofstack(void);") |
| fmt.Fprintf(fgcc, "%s\n", tsanProlog) |
| fmt.Fprintf(fgcc, "%s\n", msanProlog) |
|
|
| for _, exp := range p.ExpFunc { |
| fn := exp.Func |
|
|
| |
| |
| |
| |
| |
| var ctype strings.Builder |
| const start = "struct {\n" |
| ctype.WriteString(start) |
| gotype := new(bytes.Buffer) |
| fmt.Fprintf(gotype, "struct {\n") |
| off := int64(0) |
| npad := 0 |
| |
| maxAlign := int64(1) |
| argField := func(typ ast.Expr, namePat string, args ...any) { |
| name := fmt.Sprintf(namePat, args...) |
| t := p.cgoType(typ) |
| if off%t.Align != 0 { |
| pad := t.Align - off%t.Align |
| fmt.Fprintf(&ctype, "\t\tchar __pad%d[%d];\n", npad, pad) |
| off += pad |
| npad++ |
| } |
| fmt.Fprintf(&ctype, "\t\t%s %s;\n", t.C, name) |
| fmt.Fprintf(gotype, "\t\t%s ", name) |
| noSourceConf.Fprint(gotype, fset, typ) |
| fmt.Fprintf(gotype, "\n") |
| off += t.Size |
| |
| |
| if t.Align > maxAlign { |
| maxAlign = t.Align |
| } |
| } |
| if fn.Recv != nil { |
| argField(fn.Recv.List[0].Type, "recv") |
| } |
| fntype := fn.Type |
| forFieldList(fntype.Params, |
| func(i int, aname string, atype ast.Expr) { |
| argField(atype, "p%d", i) |
| }) |
| forFieldList(fntype.Results, |
| func(i int, aname string, atype ast.Expr) { |
| argField(atype, "r%d", i) |
| }) |
| if ctype.Len() == len(start) { |
| ctype.WriteString("\t\tchar unused;\n") |
| } |
| ctype.WriteString("\t}") |
| fmt.Fprintf(gotype, "\t}") |
|
|
| |
| |
| gccResult := "" |
| if fntype.Results == nil || len(fntype.Results.List) == 0 { |
| gccResult = "void" |
| } else if len(fntype.Results.List) == 1 && len(fntype.Results.List[0].Names) <= 1 { |
| gccResult = p.cgoType(fntype.Results.List[0].Type).C.String() |
| } else { |
| fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName) |
| fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName) |
| forFieldList(fntype.Results, |
| func(i int, aname string, atype ast.Expr) { |
| fmt.Fprintf(fgcch, "\t%s r%d;", p.cgoType(atype).C, i) |
| if len(aname) > 0 { |
| fmt.Fprintf(fgcch, " /* %s */", aname) |
| } |
| fmt.Fprint(fgcch, "\n") |
| }) |
| fmt.Fprintf(fgcch, "};\n") |
| gccResult = "struct " + exp.ExpName + "_return" |
| } |
|
|
| |
| var s strings.Builder |
| fmt.Fprintf(&s, "%s %s(", gccResult, exp.ExpName) |
| if fn.Recv != nil { |
| s.WriteString(p.cgoType(fn.Recv.List[0].Type).C.String()) |
| s.WriteString(" recv") |
| } |
|
|
| if len(fntype.Params.List) > 0 { |
| forFieldList(fntype.Params, |
| func(i int, aname string, atype ast.Expr) { |
| if i > 0 || fn.Recv != nil { |
| s.WriteString(", ") |
| } |
| fmt.Fprintf(&s, "%s %s", p.cgoType(atype).C, exportParamName(aname, i)) |
| }) |
| } else { |
| s.WriteString("void") |
| } |
| s.WriteByte(')') |
|
|
| if len(exp.Doc) > 0 { |
| fmt.Fprintf(fgcch, "\n%s", exp.Doc) |
| if !strings.HasSuffix(exp.Doc, "\n") { |
| fmt.Fprint(fgcch, "\n") |
| } |
| } |
| fmt.Fprintf(fgcch, "extern %s;\n", s.String()) |
|
|
| fmt.Fprintf(fgcc, "extern void _cgoexp%s_%s(void *);\n", cPrefix, exp.ExpName) |
| fmt.Fprintf(fgcc, "\nCGO_NO_SANITIZE_THREAD") |
| fmt.Fprintf(fgcc, "\n%s\n", s.String()) |
| fmt.Fprintf(fgcc, "{\n") |
| fmt.Fprintf(fgcc, "\tsize_t _cgo_ctxt = _cgo_wait_runtime_init_done();\n") |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| fmt.Fprintf(fgcc, "\ttypedef %s %v __attribute__((aligned(%d))) _cgo_argtype;\n", ctype.String(), p.packedAttribute(), maxAlign) |
| fmt.Fprintf(fgcc, "\tstatic _cgo_argtype _cgo_zero;\n") |
| fmt.Fprintf(fgcc, "\t_cgo_argtype _cgo_a = _cgo_zero;\n") |
| if gccResult != "void" && (len(fntype.Results.List) > 1 || len(fntype.Results.List[0].Names) > 1) { |
| fmt.Fprintf(fgcc, "\t%s r;\n", gccResult) |
| } |
| if fn.Recv != nil { |
| fmt.Fprintf(fgcc, "\t_cgo_a.recv = recv;\n") |
| } |
| forFieldList(fntype.Params, |
| func(i int, aname string, atype ast.Expr) { |
| fmt.Fprintf(fgcc, "\t_cgo_a.p%d = %s;\n", i, exportParamName(aname, i)) |
| }) |
| fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n") |
| fmt.Fprintf(fgcc, "\tcrosscall2(_cgoexp%s_%s, &_cgo_a, %d, _cgo_ctxt);\n", cPrefix, exp.ExpName, off) |
| fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n") |
| fmt.Fprintf(fgcc, "\t_cgo_release_context(_cgo_ctxt);\n") |
| if gccResult != "void" { |
| if len(fntype.Results.List) == 1 && len(fntype.Results.List[0].Names) <= 1 { |
| fmt.Fprintf(fgcc, "\treturn _cgo_a.r0;\n") |
| } else { |
| forFieldList(fntype.Results, |
| func(i int, aname string, atype ast.Expr) { |
| fmt.Fprintf(fgcc, "\tr.r%d = _cgo_a.r%d;\n", i, i) |
| }) |
| fmt.Fprintf(fgcc, "\treturn r;\n") |
| } |
| } |
| fmt.Fprintf(fgcc, "}\n") |
|
|
| |
| |
| |
| |
| |
| |
| fmt.Fprintf(fgo2, "//go:cgo_export_dynamic %s\n", exp.ExpName) |
| |
| |
| fmt.Fprintf(fgo2, "//go:linkname _cgoexp%s_%s _cgoexp%s_%s\n", cPrefix, exp.ExpName, cPrefix, exp.ExpName) |
| |
| |
| |
| |
| |
| fmt.Fprintf(fgo2, "//go:cgo_export_static _cgoexp%s_%s\n", cPrefix, exp.ExpName) |
|
|
| |
| |
| fmt.Fprintf(fgo2, "func _cgoexp%s_%s(a *%s) {\n", cPrefix, exp.ExpName, gotype) |
|
|
| fmt.Fprintf(fm, "void _cgoexp%s_%s(void* p __attribute__((unused))){}\n", cPrefix, exp.ExpName) |
|
|
| fmt.Fprintf(fgo2, "\t") |
|
|
| if gccResult != "void" { |
| |
| forFieldList(fntype.Results, |
| func(i int, aname string, atype ast.Expr) { |
| if i > 0 { |
| fmt.Fprintf(fgo2, ", ") |
| } |
| fmt.Fprintf(fgo2, "a.r%d", i) |
| }) |
| fmt.Fprintf(fgo2, " = ") |
| } |
| if fn.Recv != nil { |
| fmt.Fprintf(fgo2, "a.recv.") |
| } |
| fmt.Fprintf(fgo2, "%s(", exp.Func.Name) |
| forFieldList(fntype.Params, |
| func(i int, aname string, atype ast.Expr) { |
| if i > 0 { |
| fmt.Fprint(fgo2, ", ") |
| } |
| fmt.Fprintf(fgo2, "a.p%d", i) |
| }) |
| fmt.Fprint(fgo2, ")\n") |
| if gccResult != "void" { |
| |
| |
| forFieldList(fntype.Results, |
| func(i int, aname string, atype ast.Expr) { |
| if !p.hasPointer(nil, atype, false) { |
| return |
| } |
|
|
| |
| pos := fset.Position(exp.Func.Pos()) |
| fmt.Fprintf(fgo2, "//line %s:%d\n", pos.Filename, pos.Line) |
| fmt.Fprintf(fgo2, "\t_cgoCheckResult(a.r%d)\n", i) |
| }) |
| } |
| fmt.Fprint(fgo2, "}\n") |
| } |
|
|
| fmt.Fprintf(fgcch, "%s", gccExportHeaderEpilog) |
| } |
|
|
| |
| func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) { |
| gccgoSymbolPrefix := p.gccgoSymbolPrefix() |
|
|
| p.writeExportHeader(fgcch) |
|
|
| fmt.Fprintf(fgcc, "/* Code generated by cmd/cgo; DO NOT EDIT. */\n\n") |
| fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n") |
|
|
| fmt.Fprintf(fgcc, "%s\n", gccgoExportFileProlog) |
| fmt.Fprintf(fgcc, "%s\n", tsanProlog) |
| fmt.Fprintf(fgcc, "%s\n", msanProlog) |
|
|
| for _, exp := range p.ExpFunc { |
| fn := exp.Func |
| fntype := fn.Type |
|
|
| cdeclBuf := new(strings.Builder) |
| resultCount := 0 |
| forFieldList(fntype.Results, |
| func(i int, aname string, atype ast.Expr) { resultCount++ }) |
| switch resultCount { |
| case 0: |
| fmt.Fprintf(cdeclBuf, "void") |
| case 1: |
| forFieldList(fntype.Results, |
| func(i int, aname string, atype ast.Expr) { |
| t := p.cgoType(atype) |
| fmt.Fprintf(cdeclBuf, "%s", t.C) |
| }) |
| default: |
| |
| fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName) |
| fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName) |
| forFieldList(fntype.Results, |
| func(i int, aname string, atype ast.Expr) { |
| t := p.cgoType(atype) |
| fmt.Fprintf(fgcch, "\t%s r%d;", t.C, i) |
| if len(aname) > 0 { |
| fmt.Fprintf(fgcch, " /* %s */", aname) |
| } |
| fmt.Fprint(fgcch, "\n") |
| }) |
| fmt.Fprintf(fgcch, "};\n") |
| fmt.Fprintf(cdeclBuf, "struct %s_return", exp.ExpName) |
| } |
|
|
| cRet := cdeclBuf.String() |
|
|
| cdeclBuf = new(strings.Builder) |
| fmt.Fprintf(cdeclBuf, "(") |
| if fn.Recv != nil { |
| fmt.Fprintf(cdeclBuf, "%s recv", p.cgoType(fn.Recv.List[0].Type).C.String()) |
| } |
| |
| forFieldList(fntype.Params, |
| func(i int, aname string, atype ast.Expr) { |
| if i > 0 || fn.Recv != nil { |
| fmt.Fprintf(cdeclBuf, ", ") |
| } |
| t := p.cgoType(atype) |
| fmt.Fprintf(cdeclBuf, "%s p%d", t.C, i) |
| }) |
| fmt.Fprintf(cdeclBuf, ")") |
| cParams := cdeclBuf.String() |
|
|
| if len(exp.Doc) > 0 { |
| fmt.Fprintf(fgcch, "\n%s", exp.Doc) |
| } |
|
|
| fmt.Fprintf(fgcch, "extern %s %s%s;\n", cRet, exp.ExpName, cParams) |
|
|
| |
| |
| |
| |
| goName := "Cgoexp_" + exp.ExpName |
| fmt.Fprintf(fgcc, `extern %s %s %s __asm__("%s.%s");`, cRet, goName, cParams, gccgoSymbolPrefix, gccgoToSymbol(goName)) |
| fmt.Fprint(fgcc, "\n") |
|
|
| fmt.Fprint(fgcc, "\nCGO_NO_SANITIZE_THREAD\n") |
| fmt.Fprintf(fgcc, "%s %s %s {\n", cRet, exp.ExpName, cParams) |
| if resultCount > 0 { |
| fmt.Fprintf(fgcc, "\t%s r;\n", cRet) |
| } |
| fmt.Fprintf(fgcc, "\tif(_cgo_wait_runtime_init_done)\n") |
| fmt.Fprintf(fgcc, "\t\t_cgo_wait_runtime_init_done();\n") |
| fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n") |
| fmt.Fprint(fgcc, "\t") |
| if resultCount > 0 { |
| fmt.Fprint(fgcc, "r = ") |
| } |
| fmt.Fprintf(fgcc, "%s(", goName) |
| if fn.Recv != nil { |
| fmt.Fprint(fgcc, "recv") |
| } |
| forFieldList(fntype.Params, |
| func(i int, aname string, atype ast.Expr) { |
| if i > 0 || fn.Recv != nil { |
| fmt.Fprintf(fgcc, ", ") |
| } |
| fmt.Fprintf(fgcc, "p%d", i) |
| }) |
| fmt.Fprint(fgcc, ");\n") |
| fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n") |
| if resultCount > 0 { |
| fmt.Fprint(fgcc, "\treturn r;\n") |
| } |
| fmt.Fprint(fgcc, "}\n") |
|
|
| |
| fmt.Fprintf(fm, `char %s[1] __asm__("%s.%s");`, goName, gccgoSymbolPrefix, gccgoToSymbol(goName)) |
| fmt.Fprint(fm, "\n") |
|
|
| |
| |
|
|
| |
| |
| |
| fmt.Fprint(fgo2, "\n") |
| fmt.Fprintf(fgo2, "func %s(", goName) |
| if fn.Recv != nil { |
| fmt.Fprint(fgo2, "recv ") |
| printer.Fprint(fgo2, fset, fn.Recv.List[0].Type) |
| } |
| forFieldList(fntype.Params, |
| func(i int, aname string, atype ast.Expr) { |
| if i > 0 || fn.Recv != nil { |
| fmt.Fprintf(fgo2, ", ") |
| } |
| fmt.Fprintf(fgo2, "p%d ", i) |
| printer.Fprint(fgo2, fset, atype) |
| }) |
| fmt.Fprintf(fgo2, ")") |
| if resultCount > 0 { |
| fmt.Fprintf(fgo2, " (") |
| forFieldList(fntype.Results, |
| func(i int, aname string, atype ast.Expr) { |
| if i > 0 { |
| fmt.Fprint(fgo2, ", ") |
| } |
| printer.Fprint(fgo2, fset, atype) |
| }) |
| fmt.Fprint(fgo2, ")") |
| } |
| fmt.Fprint(fgo2, " {\n") |
| fmt.Fprint(fgo2, "\tsyscall.CgocallBack()\n") |
| fmt.Fprint(fgo2, "\tdefer syscall.CgocallBackDone()\n") |
| fmt.Fprint(fgo2, "\t") |
| if resultCount > 0 { |
| fmt.Fprint(fgo2, "return ") |
| } |
| if fn.Recv != nil { |
| fmt.Fprint(fgo2, "recv.") |
| } |
| fmt.Fprintf(fgo2, "%s(", exp.Func.Name) |
| forFieldList(fntype.Params, |
| func(i int, aname string, atype ast.Expr) { |
| if i > 0 { |
| fmt.Fprint(fgo2, ", ") |
| } |
| fmt.Fprintf(fgo2, "p%d", i) |
| }) |
| fmt.Fprint(fgo2, ")\n") |
| fmt.Fprint(fgo2, "}\n") |
| } |
|
|
| fmt.Fprintf(fgcch, "%s", gccExportHeaderEpilog) |
| } |
|
|
| |
| func (p *Package) writeExportHeader(fgcch io.Writer) { |
| fmt.Fprintf(fgcch, "/* Code generated by cmd/cgo; DO NOT EDIT. */\n\n") |
| pkg := *importPath |
| if pkg == "" { |
| pkg = p.PackagePath |
| } |
| fmt.Fprintf(fgcch, "/* package %s */\n\n", pkg) |
| fmt.Fprintf(fgcch, "%s\n", builtinExportProlog) |
|
|
| |
| |
| |
| |
| re := regexp.MustCompile(`(?m)^(#line\s+\d+\s+")[^"]*[/\\]([^"]*")`) |
| preamble := re.ReplaceAllString(p.Preamble, "$1$2") |
|
|
| fmt.Fprintf(fgcch, "/* Start of preamble from import \"C\" comments. */\n\n") |
| fmt.Fprintf(fgcch, "%s\n", preamble) |
| fmt.Fprintf(fgcch, "\n/* End of preamble from import \"C\" comments. */\n\n") |
|
|
| fmt.Fprintf(fgcch, "%s\n", p.gccExportHeaderProlog()) |
| } |
|
|
| |
| func gccgoToSymbol(ppath string) string { |
| if gccgoMangler == nil { |
| var err error |
| cmd := os.Getenv("GCCGO") |
| if cmd == "" { |
| cmd, err = exec.LookPath("gccgo") |
| if err != nil { |
| fatalf("unable to locate gccgo: %v", err) |
| } |
| } |
| gccgoMangler, err = pkgpath.ToSymbolFunc(cmd, *objDir) |
| if err != nil { |
| fatalf("%v", err) |
| } |
| } |
| return gccgoMangler(ppath) |
| } |
|
|
| |
| func (p *Package) gccgoSymbolPrefix() string { |
| if !*gccgo { |
| return "" |
| } |
|
|
| if *gccgopkgpath != "" { |
| return gccgoToSymbol(*gccgopkgpath) |
| } |
| if *gccgoprefix == "" && p.PackageName == "main" { |
| return "main" |
| } |
| prefix := gccgoToSymbol(*gccgoprefix) |
| if prefix == "" { |
| prefix = "go" |
| } |
| return prefix + "." + p.PackageName |
| } |
|
|
| |
| |
| func forFieldList(fl *ast.FieldList, fn func(int, string, ast.Expr)) { |
| if fl == nil { |
| return |
| } |
| i := 0 |
| for _, r := range fl.List { |
| if r.Names == nil { |
| fn(i, "", r.Type) |
| i++ |
| } else { |
| for _, n := range r.Names { |
| fn(i, n.Name, r.Type) |
| i++ |
| } |
| } |
| } |
| } |
|
|
| func c(repr string, args ...any) *TypeRepr { |
| return &TypeRepr{repr, args} |
| } |
|
|
| |
| var goTypes = map[string]*Type{ |
| "bool": {Size: 1, Align: 1, C: c("GoUint8")}, |
| "byte": {Size: 1, Align: 1, C: c("GoUint8")}, |
| "int": {Size: 0, Align: 0, C: c("GoInt")}, |
| "uint": {Size: 0, Align: 0, C: c("GoUint")}, |
| "rune": {Size: 4, Align: 4, C: c("GoInt32")}, |
| "int8": {Size: 1, Align: 1, C: c("GoInt8")}, |
| "uint8": {Size: 1, Align: 1, C: c("GoUint8")}, |
| "int16": {Size: 2, Align: 2, C: c("GoInt16")}, |
| "uint16": {Size: 2, Align: 2, C: c("GoUint16")}, |
| "int32": {Size: 4, Align: 4, C: c("GoInt32")}, |
| "uint32": {Size: 4, Align: 4, C: c("GoUint32")}, |
| "int64": {Size: 8, Align: 8, C: c("GoInt64")}, |
| "uint64": {Size: 8, Align: 8, C: c("GoUint64")}, |
| "float32": {Size: 4, Align: 4, C: c("GoFloat32")}, |
| "float64": {Size: 8, Align: 8, C: c("GoFloat64")}, |
| "complex64": {Size: 8, Align: 4, C: c("GoComplex64")}, |
| "complex128": {Size: 16, Align: 8, C: c("GoComplex128")}, |
| } |
|
|
| |
| func (p *Package) cgoType(e ast.Expr) *Type { |
| return p.doCgoType(e, make(map[ast.Expr]bool)) |
| } |
|
|
| |
| func (p *Package) doCgoType(e ast.Expr, m map[ast.Expr]bool) *Type { |
| if m[e] { |
| fatalf("%s: invalid recursive type", fset.Position(e.Pos())) |
| } |
| m[e] = true |
| switch t := e.(type) { |
| case *ast.StarExpr: |
| x := p.doCgoType(t.X, m) |
| return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("%s*", x.C)} |
| case *ast.ArrayType: |
| if t.Len == nil { |
| |
| return &Type{Size: p.PtrSize * 3, Align: p.PtrSize, C: c("GoSlice")} |
| } |
| |
| case *ast.StructType: |
| |
| case *ast.FuncType: |
| return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("void*")} |
| case *ast.InterfaceType: |
| return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoInterface")} |
| case *ast.MapType: |
| return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("GoMap")} |
| case *ast.ChanType: |
| return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("GoChan")} |
| case *ast.Ident: |
| goTypesFixup := func(r *Type) *Type { |
| if r.Size == 0 { |
| rr := new(Type) |
| *rr = *r |
| rr.Size = p.IntSize |
| rr.Align = p.IntSize |
| r = rr |
| } |
| if r.Align > p.PtrSize { |
| r.Align = p.PtrSize |
| } |
| return r |
| } |
| |
| |
| for _, d := range p.Decl { |
| gd, ok := d.(*ast.GenDecl) |
| if !ok || gd.Tok != token.TYPE { |
| continue |
| } |
| for _, spec := range gd.Specs { |
| ts, ok := spec.(*ast.TypeSpec) |
| if !ok { |
| continue |
| } |
| if ts.Name.Name == t.Name { |
| |
| |
| if m[ts.Type] { |
| fatalf("%s: invalid recursive type: %s refers to itself", fset.Position(e.Pos()), t.Name) |
| } |
| return p.doCgoType(ts.Type, m) |
| } |
| } |
| } |
| if def := typedef[t.Name]; def != nil { |
| if defgo, ok := def.Go.(*ast.Ident); ok { |
| switch defgo.Name { |
| case "complex64", "complex128": |
| |
| |
| |
| |
| |
| return goTypesFixup(goTypes[defgo.Name]) |
| } |
| } |
| return def |
| } |
| if t.Name == "uintptr" { |
| return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("GoUintptr")} |
| } |
| if t.Name == "string" { |
| |
| return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoString")} |
| } |
| if t.Name == "error" { |
| return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoInterface")} |
| } |
| if t.Name == "any" { |
| return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoInterface")} |
| } |
| if r, ok := goTypes[t.Name]; ok { |
| return goTypesFixup(r) |
| } |
| error_(e.Pos(), "unrecognized Go type %s", t.Name) |
| return &Type{Size: 4, Align: 4, C: c("int")} |
| case *ast.SelectorExpr: |
| id, ok := t.X.(*ast.Ident) |
| if ok && id.Name == "unsafe" && t.Sel.Name == "Pointer" { |
| return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("void*")} |
| } |
| } |
| error_(e.Pos(), "Go type not supported in export: %s", gofmt(e)) |
| return &Type{Size: 4, Align: 4, C: c("int")} |
| } |
|
|
| const gccProlog = ` |
| #line 1 "cgo-gcc-prolog" |
| /* |
| If x and y are not equal, the type will be invalid |
| (have a negative array count) and an inscrutable error will come |
| out of the compiler and hopefully mention "name". |
| */ |
| #define __cgo_compile_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2UL+1UL]; |
| |
| /* Check at compile time that the sizes we use match our expectations. */ |
| #define __cgo_size_assert(t, n) __cgo_compile_assert_eq(sizeof(t), (size_t)n, _cgo_sizeof_##t##_is_not_##n) |
| |
| __cgo_size_assert(char, 1) |
| __cgo_size_assert(short, 2) |
| __cgo_size_assert(int, 4) |
| typedef long long __cgo_long_long; |
| __cgo_size_assert(__cgo_long_long, 8) |
| __cgo_size_assert(float, 4) |
| __cgo_size_assert(double, 8) |
| |
| extern char* _cgo_topofstack(void); |
| |
| /* |
| We use packed structs, but they are always aligned. |
| The pragmas and address-of-packed-member are only recognized as warning |
| groups in clang 4.0+, so ignore unknown pragmas first. |
| */ |
| #pragma GCC diagnostic ignored "-Wunknown-pragmas" |
| #pragma GCC diagnostic ignored "-Wpragmas" |
| #pragma GCC diagnostic ignored "-Waddress-of-packed-member" |
| #pragma GCC diagnostic ignored "-Wunknown-warning-option" |
| #pragma GCC diagnostic ignored "-Wunaligned-access" |
| |
| #include <errno.h> |
| #include <string.h> |
| ` |
|
|
| |
| const noTsanProlog = ` |
| #define CGO_NO_SANITIZE_THREAD |
| #define _cgo_tsan_acquire() |
| #define _cgo_tsan_release() |
| ` |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const yesTsanProlog = ` |
| #line 1 "cgo-tsan-prolog" |
| #define CGO_NO_SANITIZE_THREAD __attribute__ ((no_sanitize_thread)) |
| |
| long long _cgo_sync __attribute__ ((common)); |
| |
| extern void __tsan_acquire(void*); |
| extern void __tsan_release(void*); |
| |
| __attribute__ ((unused)) |
| static void _cgo_tsan_acquire() { |
| __tsan_acquire(&_cgo_sync); |
| } |
| |
| __attribute__ ((unused)) |
| static void _cgo_tsan_release() { |
| __tsan_release(&_cgo_sync); |
| } |
| ` |
|
|
| |
| var tsanProlog = noTsanProlog |
|
|
| |
| |
| const noMsanProlog = ` |
| #define _cgo_msan_write(addr, sz) |
| ` |
|
|
| |
| |
| |
| const yesMsanProlog = ` |
| extern void __msan_unpoison(const volatile void *, size_t); |
| |
| #define _cgo_msan_write(addr, sz) __msan_unpoison((addr), (sz)) |
| ` |
|
|
| |
| |
| var msanProlog = noMsanProlog |
|
|
| const builtinProlog = ` |
| #line 1 "cgo-builtin-prolog" |
| #include <stddef.h> |
| |
| /* Define intgo when compiling with GCC. */ |
| typedef ptrdiff_t intgo; |
| |
| #define GO_CGO_GOSTRING_TYPEDEF |
| typedef struct { const char *p; intgo n; } _GoString_; |
| typedef struct { char *p; intgo n; intgo c; } _GoBytes_; |
| _GoString_ GoString(char *p); |
| _GoString_ GoStringN(char *p, int l); |
| _GoBytes_ GoBytes(void *p, int n); |
| char *CString(_GoString_); |
| void *CBytes(_GoBytes_); |
| void *_CMalloc(size_t); |
| |
| __attribute__ ((unused)) |
| static size_t _GoStringLen(_GoString_ s) { return (size_t)s.n; } |
| |
| __attribute__ ((unused)) |
| static const char *_GoStringPtr(_GoString_ s) { return s.p; } |
| ` |
|
|
| const goProlog = ` |
| //go:linkname _cgo_runtime_cgocall runtime.cgocall |
| func _cgo_runtime_cgocall(unsafe.Pointer, uintptr) int32 |
| |
| //go:linkname _cgoCheckPointer runtime.cgoCheckPointer |
| //go:noescape |
| func _cgoCheckPointer(interface{}, interface{}) |
| |
| //go:linkname _cgoCheckResult runtime.cgoCheckResult |
| //go:noescape |
| func _cgoCheckResult(interface{}) |
| ` |
|
|
| const gccgoGoProlog = ` |
| func _cgoCheckPointer(interface{}, interface{}) |
| |
| func _cgoCheckResult(interface{}) |
| ` |
|
|
| const goStringDef = ` |
| //go:linkname _cgo_runtime_gostring runtime.gostring |
| func _cgo_runtime_gostring(*_Ctype_char) string |
| |
| // GoString converts the C string p into a Go string. |
| func _Cfunc_GoString(p *_Ctype_char) string { |
| return _cgo_runtime_gostring(p) |
| } |
| ` |
|
|
| const goStringNDef = ` |
| //go:linkname _cgo_runtime_gostringn runtime.gostringn |
| func _cgo_runtime_gostringn(*_Ctype_char, int) string |
| |
| // GoStringN converts the C data p with explicit length l to a Go string. |
| func _Cfunc_GoStringN(p *_Ctype_char, l _Ctype_int) string { |
| return _cgo_runtime_gostringn(p, int(l)) |
| } |
| ` |
|
|
| const goBytesDef = ` |
| //go:linkname _cgo_runtime_gobytes runtime.gobytes |
| func _cgo_runtime_gobytes(unsafe.Pointer, int) []byte |
| |
| // GoBytes converts the C data p with explicit length l to a Go []byte. |
| func _Cfunc_GoBytes(p unsafe.Pointer, l _Ctype_int) []byte { |
| return _cgo_runtime_gobytes(p, int(l)) |
| } |
| ` |
|
|
| const cStringDef = ` |
| // CString converts the Go string s to a C string. |
| // |
| // The C string is allocated in the C heap using malloc. |
| // It is the caller's responsibility to arrange for it to be |
| // freed, such as by calling C.free (be sure to include stdlib.h |
| // if C.free is needed). |
| func _Cfunc_CString(s string) *_Ctype_char { |
| if len(s)+1 <= 0 { |
| panic("string too large") |
| } |
| p := _cgo_cmalloc(uint64(len(s)+1)) |
| sliceHeader := struct { |
| p unsafe.Pointer |
| len int |
| cap int |
| }{p, len(s)+1, len(s)+1} |
| b := *(*[]byte)(unsafe.Pointer(&sliceHeader)) |
| copy(b, s) |
| b[len(s)] = 0 |
| return (*_Ctype_char)(p) |
| } |
| ` |
|
|
| const cBytesDef = ` |
| // CBytes converts the Go []byte slice b to a C array. |
| // |
| // The C array is allocated in the C heap using malloc. |
| // It is the caller's responsibility to arrange for it to be |
| // freed, such as by calling C.free (be sure to include stdlib.h |
| // if C.free is needed). |
| func _Cfunc_CBytes(b []byte) unsafe.Pointer { |
| p := _cgo_cmalloc(uint64(len(b))) |
| sliceHeader := struct { |
| p unsafe.Pointer |
| len int |
| cap int |
| }{p, len(b), len(b)} |
| s := *(*[]byte)(unsafe.Pointer(&sliceHeader)) |
| copy(s, b) |
| return p |
| } |
| ` |
|
|
| const cMallocDef = ` |
| func _Cfunc__CMalloc(n _Ctype_size_t) unsafe.Pointer { |
| return _cgo_cmalloc(uint64(n)) |
| } |
| ` |
|
|
| var builtinDefs = map[string]string{ |
| "GoString": goStringDef, |
| "GoStringN": goStringNDef, |
| "GoBytes": goBytesDef, |
| "CString": cStringDef, |
| "CBytes": cBytesDef, |
| "_CMalloc": cMallocDef, |
| } |
|
|
| |
| |
| |
| |
|
|
| const cMallocDefGo = ` |
| //go:cgo_import_static _cgoPREFIX_Cfunc__Cmalloc |
| //go:linkname __cgofn__cgoPREFIX_Cfunc__Cmalloc _cgoPREFIX_Cfunc__Cmalloc |
| var __cgofn__cgoPREFIX_Cfunc__Cmalloc byte |
| var _cgoPREFIX_Cfunc__Cmalloc = unsafe.Pointer(&__cgofn__cgoPREFIX_Cfunc__Cmalloc) |
| |
| //go:linkname runtime_throw runtime.throw |
| func runtime_throw(string) |
| |
| //go:cgo_unsafe_args |
| func _cgo_cmalloc(p0 uint64) (r1 unsafe.Pointer) { |
| _cgo_runtime_cgocall(_cgoPREFIX_Cfunc__Cmalloc, uintptr(unsafe.Pointer(&p0))) |
| if r1 == nil { |
| runtime_throw("runtime: C malloc failed") |
| } |
| return |
| } |
| ` |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| const cMallocDefC = ` |
| CGO_NO_SANITIZE_THREAD |
| void _cgoPREFIX_Cfunc__Cmalloc(void *v) { |
| struct { |
| unsigned long long p0; |
| void *r1; |
| } PACKED *a = v; |
| void *ret; |
| _cgo_tsan_acquire(); |
| ret = malloc(a->p0); |
| if (ret == NULL && a->p0 == 0) { |
| ret = malloc(1); |
| } |
| a->r1 = ret; |
| _cgo_tsan_release(); |
| } |
| ` |
|
|
| func (p *Package) cPrologGccgo() string { |
| r := strings.NewReplacer( |
| "PREFIX", cPrefix, |
| "GCCGOSYMBOLPREF", p.gccgoSymbolPrefix(), |
| "_cgoCheckPointer", gccgoToSymbol("_cgoCheckPointer"), |
| "_cgoCheckResult", gccgoToSymbol("_cgoCheckResult")) |
| return r.Replace(cPrologGccgo) |
| } |
|
|
| const cPrologGccgo = ` |
| #line 1 "cgo-c-prolog-gccgo" |
| #include <stdint.h> |
| #include <stdlib.h> |
| #include <string.h> |
| |
| typedef unsigned char byte; |
| typedef intptr_t intgo; |
| |
| struct __go_string { |
| const unsigned char *__data; |
| intgo __length; |
| }; |
| |
| typedef struct __go_open_array { |
| void* __values; |
| intgo __count; |
| intgo __capacity; |
| } Slice; |
| |
| struct __go_string __go_byte_array_to_string(const void* p, intgo len); |
| struct __go_open_array __go_string_to_byte_array (struct __go_string str); |
| |
| extern void runtime_throw(const char *); |
| |
| const char *_cgoPREFIX_Cfunc_CString(struct __go_string s) { |
| char *p = malloc(s.__length+1); |
| if(p == NULL) |
| runtime_throw("runtime: C malloc failed"); |
| memmove(p, s.__data, s.__length); |
| p[s.__length] = 0; |
| return p; |
| } |
| |
| void *_cgoPREFIX_Cfunc_CBytes(struct __go_open_array b) { |
| char *p = malloc(b.__count); |
| if(p == NULL) |
| runtime_throw("runtime: C malloc failed"); |
| memmove(p, b.__values, b.__count); |
| return p; |
| } |
| |
| struct __go_string _cgoPREFIX_Cfunc_GoString(char *p) { |
| intgo len = (p != NULL) ? strlen(p) : 0; |
| return __go_byte_array_to_string(p, len); |
| } |
| |
| struct __go_string _cgoPREFIX_Cfunc_GoStringN(char *p, int32_t n) { |
| return __go_byte_array_to_string(p, n); |
| } |
| |
| Slice _cgoPREFIX_Cfunc_GoBytes(char *p, int32_t n) { |
| struct __go_string s = { (const unsigned char *)p, n }; |
| return __go_string_to_byte_array(s); |
| } |
| |
| void *_cgoPREFIX_Cfunc__CMalloc(size_t n) { |
| void *p = malloc(n); |
| if(p == NULL && n == 0) |
| p = malloc(1); |
| if(p == NULL) |
| runtime_throw("runtime: C malloc failed"); |
| return p; |
| } |
| |
| struct __go_type_descriptor; |
| typedef struct __go_empty_interface { |
| const struct __go_type_descriptor *__type_descriptor; |
| void *__object; |
| } Eface; |
| |
| extern void runtimeCgoCheckPointer(Eface, Eface) |
| __asm__("runtime.cgoCheckPointer") |
| __attribute__((weak)); |
| |
| extern void localCgoCheckPointer(Eface, Eface) |
| __asm__("GCCGOSYMBOLPREF._cgoCheckPointer"); |
| |
| void localCgoCheckPointer(Eface ptr, Eface arg) { |
| if(runtimeCgoCheckPointer) { |
| runtimeCgoCheckPointer(ptr, arg); |
| } |
| } |
| |
| extern void runtimeCgoCheckResult(Eface) |
| __asm__("runtime.cgoCheckResult") |
| __attribute__((weak)); |
| |
| extern void localCgoCheckResult(Eface) |
| __asm__("GCCGOSYMBOLPREF._cgoCheckResult"); |
| |
| void localCgoCheckResult(Eface val) { |
| if(runtimeCgoCheckResult) { |
| runtimeCgoCheckResult(val); |
| } |
| } |
| ` |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const builtinExportProlog = ` |
| #line 1 "cgo-builtin-export-prolog" |
| |
| #include <stddef.h> |
| |
| #ifndef GO_CGO_EXPORT_PROLOGUE_H |
| #define GO_CGO_EXPORT_PROLOGUE_H |
| |
| #ifndef GO_CGO_GOSTRING_TYPEDEF |
| typedef struct { const char *p; ptrdiff_t n; } _GoString_; |
| extern size_t _GoStringLen(_GoString_ s); |
| extern const char *_GoStringPtr(_GoString_ s); |
| #endif |
| |
| #endif |
| ` |
|
|
| func (p *Package) gccExportHeaderProlog() string { |
| return strings.ReplaceAll(gccExportHeaderProlog, "GOINTBITS", fmt.Sprint(8*p.IntSize)) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const gccExportHeaderProlog = ` |
| /* Start of boilerplate cgo prologue. */ |
| #line 1 "cgo-gcc-export-header-prolog" |
| |
| #ifndef GO_CGO_PROLOGUE_H |
| #define GO_CGO_PROLOGUE_H |
| |
| typedef signed char GoInt8; |
| typedef unsigned char GoUint8; |
| typedef short GoInt16; |
| typedef unsigned short GoUint16; |
| typedef int GoInt32; |
| typedef unsigned int GoUint32; |
| typedef long long GoInt64; |
| typedef unsigned long long GoUint64; |
| typedef GoIntGOINTBITS GoInt; |
| typedef GoUintGOINTBITS GoUint; |
| typedef size_t GoUintptr; |
| typedef float GoFloat32; |
| typedef double GoFloat64; |
| #ifdef _MSC_VER |
| #if !defined(__cplusplus) || _MSVC_LANG <= 201402L |
| #include <complex.h> |
| typedef _Fcomplex GoComplex64; |
| typedef _Dcomplex GoComplex128; |
| #else |
| #include <complex> |
| typedef std::complex<float> GoComplex64; |
| typedef std::complex<double> GoComplex128; |
| #endif |
| #else |
| typedef float _Complex GoComplex64; |
| typedef double _Complex GoComplex128; |
| #endif |
| |
| /* |
| static assertion to make sure the file is being used on architecture |
| at least with matching size of GoInt. |
| */ |
| typedef char _check_for_GOINTBITS_bit_pointer_matching_GoInt[sizeof(void*)==GOINTBITS/8 ? 1:-1]; |
| |
| #ifndef GO_CGO_GOSTRING_TYPEDEF |
| typedef _GoString_ GoString; |
| #endif |
| typedef void *GoMap; |
| typedef void *GoChan; |
| typedef struct { void *t; void *v; } GoInterface; |
| typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; |
| |
| #endif |
| |
| /* End of boilerplate cgo prologue. */ |
| |
| #ifdef __cplusplus |
| extern "C" { |
| #endif |
| ` |
|
|
| |
| const gccExportHeaderEpilog = ` |
| #ifdef __cplusplus |
| } |
| #endif |
| ` |
|
|
| |
| |
| |
| |
| const gccgoExportFileProlog = ` |
| #line 1 "cgo-gccgo-export-file-prolog" |
| extern _Bool runtime_iscgo __attribute__ ((weak)); |
| |
| static void GoInit(void) __attribute__ ((constructor)); |
| static void GoInit(void) { |
| if(&runtime_iscgo) |
| runtime_iscgo = 1; |
| } |
| |
| extern size_t _cgo_wait_runtime_init_done(void) __attribute__ ((weak)); |
| ` |
|
|