]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.cc] runtime/cgo: convert from C to Go
authorRuss Cox <rsc@golang.org>
Tue, 11 Nov 2014 22:05:37 +0000 (17:05 -0500)
committerRuss Cox <rsc@golang.org>
Tue, 11 Nov 2014 22:05:37 +0000 (17:05 -0500)
The conversion was done with an automated tool and then
modified only as necessary to make it compile and run.

[This CL is part of the removal of C code from package runtime.
See golang.org/s/dev.cc for an overview.]

LGTM=r
R=r
CC=austin, dvyukov, golang-codereviews, iant, khr
https://golang.org/cl/168500043

12 files changed:
src/runtime/cgo.go [new file with mode: 0644]
src/runtime/cgo/callbacks.c [deleted file]
src/runtime/cgo/callbacks.go [new file with mode: 0644]
src/runtime/cgo/dragonfly.go [moved from src/runtime/cgo/dragonfly.c with 64% similarity]
src/runtime/cgo/freebsd.go [moved from src/runtime/cgo/freebsd.c with 64% similarity]
src/runtime/cgo/iscgo.go [moved from src/runtime/cgo/iscgo.c with 72% similarity]
src/runtime/cgo/netbsd.go [moved from src/runtime/cgo/netbsd.c with 64% similarity]
src/runtime/cgo/openbsd.go [moved from src/runtime/cgo/openbsd.c with 55% similarity]
src/runtime/cgo/setenv.c [deleted file]
src/runtime/cgo/setenv.go [new file with mode: 0644]
src/runtime/cgocall.go
src/runtime/env_posix.go

diff --git a/src/runtime/cgo.go b/src/runtime/cgo.go
new file mode 100644 (file)
index 0000000..7e6b253
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright 2014 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package runtime
+
+import "unsafe"
+
+//go:cgo_export_static main
+
+// Filled in by runtime/cgo when linked into binary.
+
+//go:linkname _cgo_init _cgo_init
+//go:linkname _cgo_malloc _cgo_malloc
+//go:linkname _cgo_free _cgo_free
+//go:linkname _cgo_thread_start _cgo_thread_start
+
+var (
+       _cgo_init         unsafe.Pointer
+       _cgo_malloc       unsafe.Pointer
+       _cgo_free         unsafe.Pointer
+       _cgo_thread_start unsafe.Pointer
+)
diff --git a/src/runtime/cgo/callbacks.c b/src/runtime/cgo/callbacks.c
deleted file mode 100644 (file)
index 282beee..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-// Copyright 2011 The Go Authors.  All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-#include "../runtime.h"
-#include "../cgocall.h"
-#include "textflag.h"
-
-// These utility functions are available to be called from code
-// compiled with gcc via crosscall2.
-
-// The declaration of crosscall2 is:
-//   void crosscall2(void (*fn)(void *, int), void *, int);
-// 
-// We need to export the symbol crosscall2 in order to support
-// callbacks from shared libraries. This applies regardless of
-// linking mode.
-#pragma cgo_export_static crosscall2
-#pragma cgo_export_dynamic crosscall2
-
-// Allocate memory.  This allocates the requested number of bytes in
-// memory controlled by the Go runtime.  The allocated memory will be
-// zeroed.  You are responsible for ensuring that the Go garbage
-// collector can see a pointer to the allocated memory for as long as
-// it is valid, e.g., by storing a pointer in a local variable in your
-// C function, or in memory allocated by the Go runtime.  If the only
-// pointers are in a C global variable or in memory allocated via
-// malloc, then the Go garbage collector may collect the memory.
-
-// Call like this in code compiled with gcc:
-//   struct { size_t len; void *ret; } a;
-//   a.len = /* number of bytes to allocate */;
-//   crosscall2(_cgo_allocate, &a, sizeof a);
-//   /* Here a.ret is a pointer to the allocated memory.  */
-
-void runtime·_cgo_allocate_internal(void);
-
-#pragma cgo_export_static _cgo_allocate
-#pragma cgo_export_dynamic _cgo_allocate
-#pragma textflag NOSPLIT
-void
-_cgo_allocate(void *a, int32 n)
-{
-       runtime·cgocallback((void(*)(void))runtime·_cgo_allocate_internal, a, n);
-}
-
-// Panic.  The argument is converted into a Go string.
-
-// Call like this in code compiled with gcc:
-//   struct { const char *p; } a;
-//   a.p = /* string to pass to panic */;
-//   crosscall2(_cgo_panic, &a, sizeof a);
-//   /* The function call will not return.  */
-
-void runtime·_cgo_panic_internal(void);
-
-#pragma cgo_export_static _cgo_panic
-#pragma cgo_export_dynamic _cgo_panic
-#pragma textflag NOSPLIT
-void
-_cgo_panic(void *a, int32 n)
-{
-       runtime·cgocallback((void(*)(void))runtime·_cgo_panic_internal, a, n);
-}
-
-#pragma cgo_import_static x_cgo_init
-extern void x_cgo_init(G*);
-void (*_cgo_init)(G*) = x_cgo_init;
-
-#pragma cgo_import_static x_cgo_malloc
-extern void x_cgo_malloc(void*);
-void (*_cgo_malloc)(void*) = x_cgo_malloc;
-
-#pragma cgo_import_static x_cgo_free
-extern void x_cgo_free(void*);
-void (*_cgo_free)(void*) = x_cgo_free;
-
-#pragma cgo_import_static x_cgo_thread_start
-extern void x_cgo_thread_start(void*);
-void (*_cgo_thread_start)(void*) = x_cgo_thread_start;
-
-#pragma cgo_export_static _cgo_topofstack
-#pragma cgo_export_dynamic _cgo_topofstack
diff --git a/src/runtime/cgo/callbacks.go b/src/runtime/cgo/callbacks.go
new file mode 100644 (file)
index 0000000..1e8b590
--- /dev/null
@@ -0,0 +1,95 @@
+// Copyright 2011 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package cgo
+
+import "unsafe"
+
+// These utility functions are available to be called from code
+// compiled with gcc via crosscall2.
+
+// cgocallback is defined in runtime
+//go:linkname _runtime_cgocallback runtime.cgocallback
+func _runtime_cgocallback(unsafe.Pointer, unsafe.Pointer, uintptr)
+
+// The declaration of crosscall2 is:
+//   void crosscall2(void (*fn)(void *, int), void *, int);
+//
+// We need to export the symbol crosscall2 in order to support
+// callbacks from shared libraries. This applies regardless of
+// linking mode.
+//go:cgo_export_static crosscall2
+//go:cgo_export_dynamic crosscall2
+
+// Allocate memory.  This allocates the requested number of bytes in
+// memory controlled by the Go runtime.  The allocated memory will be
+// zeroed.  You are responsible for ensuring that the Go garbage
+// collector can see a pointer to the allocated memory for as long as
+// it is valid, e.g., by storing a pointer in a local variable in your
+// C function, or in memory allocated by the Go runtime.  If the only
+// pointers are in a C global variable or in memory allocated via
+// malloc, then the Go garbage collector may collect the memory.
+
+// Call like this in code compiled with gcc:
+//   struct { size_t len; void *ret; } a;
+//   a.len = /* number of bytes to allocate */;
+//   crosscall2(_cgo_allocate, &a, sizeof a);
+//   /* Here a.ret is a pointer to the allocated memory.  */
+
+//go:linkname _runtime_cgo_allocate_internal runtime._cgo_allocate_internal
+var _runtime_cgo_allocate_internal byte
+
+//go:linkname _cgo_allocate _cgo_allocate
+//go:cgo_export_static _cgo_allocate
+//go:cgo_export_dynamic _cgo_allocate
+//go:nosplit
+func _cgo_allocate(a unsafe.Pointer, n int32) {
+       _runtime_cgocallback(unsafe.Pointer(&_runtime_cgo_allocate_internal), a, uintptr(n))
+}
+
+// Panic.  The argument is converted into a Go string.
+
+// Call like this in code compiled with gcc:
+//   struct { const char *p; } a;
+//   a.p = /* string to pass to panic */;
+//   crosscall2(_cgo_panic, &a, sizeof a);
+//   /* The function call will not return.  */
+
+//go:linkname _runtime_cgo_panic_internal runtime._cgo_panic_internal
+var _runtime_cgo_panic_internal byte
+
+//go:linkname _cgo_panic _cgo_panic
+//go:cgo_export_static _cgo_panic
+//go:cgo_export_dynamic _cgo_panic
+//go:nosplit
+func _cgo_panic(a unsafe.Pointer, n int32) {
+       _runtime_cgocallback(unsafe.Pointer(&_runtime_cgo_panic_internal), a, uintptr(n))
+}
+
+//go:cgo_import_static x_cgo_init
+//go:linkname x_cgo_init x_cgo_init
+//go:linkname _cgo_init _cgo_init
+var x_cgo_init byte
+var _cgo_init = &x_cgo_init
+
+//go:cgo_import_static x_cgo_malloc
+//go:linkname x_cgo_malloc x_cgo_malloc
+//go:linkname _cgo_malloc _cgo_malloc
+var x_cgo_malloc byte
+var _cgo_malloc = &x_cgo_malloc
+
+//go:cgo_import_static x_cgo_free
+//go:linkname x_cgo_free x_cgo_free
+//go:linkname _cgo_free _cgo_free
+var x_cgo_free byte
+var _cgo_free = &x_cgo_free
+
+//go:cgo_import_static x_cgo_thread_start
+//go:linkname x_cgo_thread_start x_cgo_thread_start
+//go:linkname _cgo_thread_start _cgo_thread_start
+var x_cgo_thread_start byte
+var _cgo_thread_start = &x_cgo_thread_start
+
+//go:cgo_export_static _cgo_topofstack
+//go:cgo_export_dynamic _cgo_topofstack
similarity index 64%
rename from src/runtime/cgo/dragonfly.c
rename to src/runtime/cgo/dragonfly.go
index c233c8ba9a3c14e85b31f00aabbf4271150ae7fc..96eb8660e06e70a7f123a6ff52c896ec9b4b0d39 100644 (file)
@@ -4,16 +4,16 @@
 
 // +build dragonfly
 
-#include "textflag.h"
+package cgo
+
+import _ "unsafe"
 
 // Supply environ and __progname, because we don't
 // link against the standard DragonFly crt0.o and the
 // libc dynamic library needs them.
 
-#pragma dataflag NOPTR
-char *environ[1];
-#pragma dataflag NOPTR
-char *__progname;
+//go:linkname _environ environ
+//go:linkname _progname __progname
 
-#pragma dynexport environ environ
-#pragma dynexport __progname __progname
+var _environ uintptr
+var _progname uintptr
similarity index 64%
rename from src/runtime/cgo/freebsd.c
rename to src/runtime/cgo/freebsd.go
index 4876b2abe47641df4ccd17ba9cf581b5414c341f..09ffa2377a775a781a95dddc90e1de65990fcf00 100644 (file)
@@ -4,16 +4,14 @@
 
 // +build freebsd
 
-#include "textflag.h"
+package cgo
 
 // Supply environ and __progname, because we don't
 // link against the standard FreeBSD crt0.o and the
 // libc dynamic library needs them.
 
-#pragma dataflag NOPTR
-char *environ[1];
-#pragma dataflag NOPTR
-char *__progname;
+//go:linkname _environ environ
+//go:linkname _progname __progname
 
-#pragma dynexport environ environ
-#pragma dynexport __progname __progname
+var _environ uintptr
+var _progname uintptr
similarity index 72%
rename from src/runtime/cgo/iscgo.c
rename to src/runtime/cgo/iscgo.go
index 0907a19581006d33358775484641c3674334deca..5544fd1e30787d24985206bffe166d078860b728 100644 (file)
@@ -9,7 +9,12 @@
 // correctly, and sometimes they break.  This variable is a
 // backup: it depends only on old C style static linking rules.
 
-#include "../runtime.h"
+package cgo
 
-bool runtime·iscgo = 1;
-uint32 runtime·needextram = 1;  // create an extra M on first cgo call
+import _ "unsafe"
+
+//go:linkname _iscgo runtime.iscgo
+var _iscgo bool = true
+
+//go:linkname _needextram runtime.needextram
+var _needextram uint32 = 1 // create an extra M on first cgo call
similarity index 64%
rename from src/runtime/cgo/netbsd.c
rename to src/runtime/cgo/netbsd.go
index 076cc87f12a7c70798c8f1fe0aca00630ac2d22a..9088e35d477bd836cd9fbda5c87725841a9613b7 100644 (file)
@@ -4,16 +4,14 @@
 
 // +build netbsd
 
-#include "textflag.h"
+package cgo
 
 // Supply environ and __progname, because we don't
 // link against the standard NetBSD crt0.o and the
 // libc dynamic library needs them.
 
-#pragma dataflag NOPTR
-char *environ[1];
-#pragma dataflag NOPTR
-char *__progname;
+//go:linkname _environ environ
+//go:linkname _progname __progname
 
-#pragma dynexport environ environ
-#pragma dynexport __progname __progname
+var _environ uintptr
+var _progname uintptr
similarity index 55%
rename from src/runtime/cgo/openbsd.c
rename to src/runtime/cgo/openbsd.go
index 476649544dff3cc27eb0111a0bec89940ed0872b..b59a7c2c2a8151f04b4dedc1e18edd267d50f57e 100644 (file)
@@ -4,24 +4,26 @@
 
 // +build openbsd
 
-#include "textflag.h"
+package cgo
 
 // Supply environ, __progname and __guard_local, because
 // we don't link against the standard OpenBSD crt0.o and
 // the libc dynamic library needs them.
 
-#pragma dataflag NOPTR
-char *environ[1];
-#pragma dataflag NOPTR
-char *__progname;
-long __guard_local;
+//go:linkname _environ environ
+//go:linkname _progname __progname
+//go:linkname _guard_local __guard_local
 
-#pragma dynexport environ environ
-#pragma dynexport __progname __progname
+var _environ uintptr
+var _progname uintptr
+var _guard_local uintptr
+
+//go:cgo_export_dynamic environ environ
+//go:cgo_export_dynamic __progname __progname
 
 // This is normally marked as hidden and placed in the
 // .openbsd.randomdata section.
-#pragma dynexport __guard_local __guard_local
+//go:cgo_export_dynamic __guard_local __guard_local
 
 // We override pthread_create to support PT_TLS.
-#pragma dynexport pthread_create pthread_create
+//go:cgo_export_dynamic pthread_create pthread_create
diff --git a/src/runtime/cgo/setenv.c b/src/runtime/cgo/setenv.c
deleted file mode 100644 (file)
index 76d88cb..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-// Copyright 2011 The Go Authors.  All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// +build darwin dragonfly freebsd linux netbsd openbsd
-
-#pragma cgo_import_static x_cgo_setenv
-#pragma cgo_import_static x_cgo_unsetenv
-
-void x_cgo_setenv(char**);
-void (*runtime·_cgo_setenv)(char**) = x_cgo_setenv;
-void x_cgo_unsetenv(char**);
-void (*runtime·_cgo_unsetenv)(char**) = x_cgo_unsetenv;
diff --git a/src/runtime/cgo/setenv.go b/src/runtime/cgo/setenv.go
new file mode 100644 (file)
index 0000000..1612f87
--- /dev/null
@@ -0,0 +1,21 @@
+// Copyright 2011 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build darwin dragonfly freebsd linux netbsd openbsd
+
+package cgo
+
+import _ "unsafe"
+
+//go:cgo_import_static x_cgo_setenv
+//go:linkname x_cgo_setenv x_cgo_setenv
+//go:linkname _cgo_setenv runtime._cgo_setenv
+var x_cgo_setenv byte
+var _cgo_setenv = &x_cgo_setenv
+
+//go:cgo_import_static x_cgo_unsetenv
+//go:linkname x_cgo_unsetenv x_cgo_unsetenv
+//go:linkname _cgo_unsetenv runtime._cgo_unsetenv
+var x_cgo_unsetenv byte
+var _cgo_unsetenv = &x_cgo_unsetenv
index 7fd91469eb17038f2f1ff90086afbed29d508c1f..a1fc06d39d22deb6cd7a72975e355fc36a641fc4 100644 (file)
@@ -127,9 +127,9 @@ func cgocall_errno(fn, arg unsafe.Pointer) int32 {
         * so it is safe to call while "in a system call", outside
         * the $GOMAXPROCS accounting.
         */
-       entersyscall()
+       entersyscall(0)
        errno := asmcgocall_errno(fn, arg)
-       exitsyscall()
+       exitsyscall(0)
 
        return errno
 }
@@ -153,17 +153,13 @@ func endcgo(mp *m) {
 
 // Helper functions for cgo code.
 
-// Filled by schedinit from corresponding C variables,
-// which are in turn filled in by dynamic linker when Cgo is available.
-var cgoMalloc, cgoFree unsafe.Pointer
-
 func cmalloc(n uintptr) unsafe.Pointer {
        var args struct {
                n   uint64
                ret unsafe.Pointer
        }
        args.n = uint64(n)
-       cgocall(cgoMalloc, unsafe.Pointer(&args))
+       cgocall(_cgo_malloc, unsafe.Pointer(&args))
        if args.ret == nil {
                gothrow("C malloc failed")
        }
@@ -171,7 +167,7 @@ func cmalloc(n uintptr) unsafe.Pointer {
 }
 
 func cfree(p unsafe.Pointer) {
-       cgocall(cgoFree, p)
+       cgocall(_cgo_free, p)
 }
 
 // Call from C back to Go.
@@ -189,10 +185,10 @@ func cgocallbackg() {
        // save syscall* and let reentersyscall restore them.
        savedsp := unsafe.Pointer(gp.syscallsp)
        savedpc := gp.syscallpc
-       exitsyscall() // coming out of cgo call
+       exitsyscall(0) // coming out of cgo call
        cgocallbackg1()
        // going back to cgo call
-       reentersyscall(savedpc, savedsp)
+       reentersyscall(savedpc, uintptr(savedsp))
 }
 
 func cgocallbackg1() {
index dd57872d7c7affff8e893101cf899350424cbebf..03c7a5a4af265a1718f2ce6ab60dd64567d4821f 100644 (file)
@@ -8,8 +8,6 @@ package runtime
 
 import "unsafe"
 
-func environ() []string
-
 func getenv(s *byte) *byte {
        val := gogetenv(gostringnocopy(s))
        if val == "" {
@@ -32,13 +30,13 @@ func gogetenv(key string) string {
        return ""
 }
 
-var _cgo_setenv uintptr   // pointer to C function
-var _cgo_unsetenv uintptr // pointer to C function
+var _cgo_setenv unsafe.Pointer   // pointer to C function
+var _cgo_unsetenv unsafe.Pointer // pointer to C function
 
 // Update the C environment if cgo is loaded.
 // Called from syscall.Setenv.
 func syscall_setenv_c(k string, v string) {
-       if _cgo_setenv == 0 {
+       if _cgo_setenv == nil {
                return
        }
        arg := [2]unsafe.Pointer{cstring(k), cstring(v)}
@@ -48,7 +46,7 @@ func syscall_setenv_c(k string, v string) {
 // Update the C environment if cgo is loaded.
 // Called from syscall.unsetenv.
 func syscall_unsetenv_c(k string) {
-       if _cgo_unsetenv == 0 {
+       if _cgo_unsetenv == nil {
                return
        }
        arg := [1]unsafe.Pointer{cstring(k)}