From 076ebed0d87e1c9678ece352986d8b1ad877b440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Oudompheng?= Date: Wed, 14 Dec 2011 15:40:35 -0800 Subject: [PATCH] cgo: add basic gccgo support. R=rsc, iant CC=golang-dev, remy https://golang.org/cl/5485070 --- src/cmd/cgo/main.go | 2 ++ src/cmd/cgo/out.go | 57 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/cmd/cgo/main.go b/src/cmd/cgo/main.go index 2749ca74f28..96d304ab761 100644 --- a/src/cmd/cgo/main.go +++ b/src/cmd/cgo/main.go @@ -130,6 +130,8 @@ var dynobj = flag.String("dynimport", "", "if non-empty, print dynamic import da var godefs = flag.Bool("godefs", false, "for bootstrap: write Go definitions for C file to standard output") var cdefs = flag.Bool("cdefs", false, "for bootstrap: write C definitions for C file to standard output") +var gccgo = flag.Bool("gccgo", false, "generate files for use with gccgo") + var goarch, goos string func main() { diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index e4709ae54c1..2e0a56987ac 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -46,7 +46,9 @@ func (p *Package) writeDefs() { fmt.Fprintf(fgo2, "package %s\n\n", p.PackageName) fmt.Fprintf(fgo2, "import \"unsafe\"\n\n") fmt.Fprintf(fgo2, "import \"syscall\"\n\n") - fmt.Fprintf(fgo2, "import _ \"runtime/cgo\"\n\n") + if !*gccgo { + fmt.Fprintf(fgo2, "import _ \"runtime/cgo\"\n\n") + } fmt.Fprintf(fgo2, "type _ unsafe.Pointer\n\n") fmt.Fprintf(fgo2, "func _Cerrno(dst *error, x int) { *dst = syscall.Errno(x) }\n") @@ -57,7 +59,11 @@ func (p *Package) writeDefs() { } fmt.Fprintf(fgo2, "type _Ctype_void [0]byte\n") - fmt.Fprintf(fc, cProlog) + if *gccgo { + fmt.Fprintf(fc, cPrologGccgo) + } else { + fmt.Fprintf(fc, cProlog) + } cVars := make(map[string]bool) for _, n := range p.Name { @@ -238,13 +244,22 @@ func (p *Package) writeDefsFunc(fc, fgo2 *os.File, n *Name) { Type: gtype, } printer.Fprint(fgo2, fset, d) - fmt.Fprintf(fgo2, "\n") + if *gccgo { + fmt.Fprintf(fgo2, " __asm__(\"%s\")\n", n.C) + } else { + fmt.Fprintf(fgo2, "\n") + } if name == "CString" || name == "GoString" || name == "GoStringN" || name == "GoBytes" { // The builtins are already defined in the C prolog. return } + // gccgo does not require a wrapper unless an error must be returned. + if *gccgo && !n.AddError { + return + } + var argSize int64 _, argSize = p.structType(n) @@ -730,6 +745,42 @@ void } ` +const cPrologGccgo = ` +#include +#include + +struct __go_string { + const unsigned char *__data; + int __length; +}; + +typedef struct __go_open_array { + void* __values; + int __count; + int __capacity; +} Slice; + +struct __go_string __go_byte_array_to_string(const void* p, int len); +struct __go_open_array __go_string_to_byte_array (struct __go_string str); + +const char *CString(struct __go_string s) { + return strndup(s.__data, s.__length); +} + +struct __go_string GoString(char *p) { + return __go_byte_array_to_string(p, strlen(p)); +} + +struct __go_string GoStringN(char *p, int n) { + return __go_byte_array_to_string(p, n); +} + +Slice GoBytes(char *p, int n) { + struct __go_string s = { p, n }; + return __go_string_to_byte_array(s); +} +` + const gccExportHeaderProlog = ` typedef unsigned int uint; typedef signed char schar;