mirror of
https://github.com/golang/go
synced 2024-11-25 01:08:02 -07:00
cgo: add basic gccgo support.
R=rsc, iant CC=golang-dev, remy https://golang.org/cl/5485070
This commit is contained in:
parent
d89b7173c2
commit
076ebed0d8
@ -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 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 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
|
var goarch, goos string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -46,7 +46,9 @@ func (p *Package) writeDefs() {
|
|||||||
fmt.Fprintf(fgo2, "package %s\n\n", p.PackageName)
|
fmt.Fprintf(fgo2, "package %s\n\n", p.PackageName)
|
||||||
fmt.Fprintf(fgo2, "import \"unsafe\"\n\n")
|
fmt.Fprintf(fgo2, "import \"unsafe\"\n\n")
|
||||||
fmt.Fprintf(fgo2, "import \"syscall\"\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, "type _ unsafe.Pointer\n\n")
|
||||||
fmt.Fprintf(fgo2, "func _Cerrno(dst *error, x int) { *dst = syscall.Errno(x) }\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(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)
|
cVars := make(map[string]bool)
|
||||||
for _, n := range p.Name {
|
for _, n := range p.Name {
|
||||||
@ -238,13 +244,22 @@ func (p *Package) writeDefsFunc(fc, fgo2 *os.File, n *Name) {
|
|||||||
Type: gtype,
|
Type: gtype,
|
||||||
}
|
}
|
||||||
printer.Fprint(fgo2, fset, d)
|
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" {
|
if name == "CString" || name == "GoString" || name == "GoStringN" || name == "GoBytes" {
|
||||||
// The builtins are already defined in the C prolog.
|
// The builtins are already defined in the C prolog.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// gccgo does not require a wrapper unless an error must be returned.
|
||||||
|
if *gccgo && !n.AddError {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var argSize int64
|
var argSize int64
|
||||||
_, argSize = p.structType(n)
|
_, argSize = p.structType(n)
|
||||||
|
|
||||||
@ -730,6 +745,42 @@ void
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const cPrologGccgo = `
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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 = `
|
const gccExportHeaderProlog = `
|
||||||
typedef unsigned int uint;
|
typedef unsigned int uint;
|
||||||
typedef signed char schar;
|
typedef signed char schar;
|
||||||
|
Loading…
Reference in New Issue
Block a user