Skip to content

Commit

Permalink
runtime: use -fgo-c-header to build C header file
Browse files Browse the repository at this point in the history
    
    Use the new -fgo-c-header option to build a header file for the Go
    runtime code in libgo/go/runtime, and use the new header file in the C
    runtime code in libgo/runtime.  This will ensure that the Go code and C
    code share the same data structures as we convert the runtime from C to
    Go.
    
    The new file libgo/go/runtime/runtime2.go is copied from the Go 1.7
    release, and then edited to remove unnecessary data structures and
    modify others for use with libgo.
    
    The new file libgo/go/runtime/mcache.go is an initial version of the
    same files in the Go 1.7 release, and will be replaced by the Go 1.7
    file when we convert to the new memory allocator.
    
    The new file libgo/go/runtime/type.go describes the gccgo version of the
    reflection data structures, and replaces the Go 1.7 runtime file which
    describes the gc version of those structures.
    
    Using the new header file means changing a number of struct fields to
    use Go naming conventions (that is, no underscores) and to rename
    constants to have a leading underscore so that they are not exported
    from the Go package.  These names were updated in the C code.
    
    The C code was also changed to drop the thread-local variable m, as was
    done some time ago in the gc sources.  Now the m field is always
    accessed using g->m, where g is the single remaining thread-local
    variable.  This in turn required some adjustments to set g->m correctly
    in all cases.
    
    Also pass the new -fgo-compiling-runtime option when compiling the
    runtime package, although that option doesn't do anything yet.
    
    Reviewed-on: https://go-review.googlesource.com/28051


git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@239872 138bc75d-0d04-0410-961f-82ee72b054a4
  • Loading branch information
ian committed Aug 30, 2016
1 parent c96743f commit 9ac07c1
Show file tree
Hide file tree
Showing 33 changed files with 1,757 additions and 1,000 deletions.
2 changes: 1 addition & 1 deletion gcc/go/gofrontend/MERGE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
9c91e7eeb404b5b639cd6e80e2a38da948bb35ec
394486a1cec9bbb81216311ed153179d9fe1c2c5

The first line of this file holds the git revision number of the last
merge done from the gofrontend repository.
46 changes: 34 additions & 12 deletions libgo/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,16 @@ s-version: Makefile
$(SHELL) $(srcdir)/mvifdiff.sh version.go.tmp version.go
$(STAMP) $@

runtime_sysinfo.go: s-runtime_sysinfo; @true
s-runtime_sysinfo: sysinfo.go
rm -f tmp-runtime_sysinfo.go
echo 'package runtime' > tmp-runtime_sysinfo.go
echo >> tmp-runtime_sysinfo.go
grep 'const _sizeof_ucontext_t ' sysinfo.go >> tmp-runtime_sysinfo.go
grep 'type _sigset_t ' sysinfo.go >> tmp-runtime_sysinfo.go
$(SHELL) $(srcdir)/mvifdiff.sh tmp-runtime_sysinfo.go runtime_sysinfo.go
$(STAMP) $@

noinst_DATA = zstdpkglist.go

# Generate the list of go std packages that were included in libgo
Expand Down Expand Up @@ -877,6 +887,13 @@ libgolibbegin_a_CFLAGS = $(AM_CFLAGS) -fPIC
libnetgo_a_SOURCES =
libnetgo_a_LIBADD = netgo.o

# Make sure runtime.inc is built before compiling any .c file.
$(libgo_la_OBJECTS): runtime.inc
$(libgo_llgo_la_OBJECTS): runtime.inc
$(libgobegin_a_OBJECTS): runtime.inc
$(libgobegin_llgo_a_OBJECTS): runtime.inc
$(libgolibbegin_a_OBJECTS): runtime.inc

LTLDFLAGS = $(shell $(SHELL) $(top_srcdir)/../libtool-ldflags $(LDFLAGS))

GOCFLAGS = $(CFLAGS)
Expand Down Expand Up @@ -904,7 +921,7 @@ BUILDDEPS = \
BUILDPACKAGE = \
$(MKDIR_P) $(@D); \
files=`echo $^ | sed -e 's/[^ ]*\.gox//g' -e 's/[^ ]*\.dep//'`; \
$(LTGOCOMPILE) -I . -c -fgo-pkgpath=`echo $@ | sed -e 's/.lo$$//' -e 's/-go$$//'` -o $@ $$files
$(LTGOCOMPILE) -I . -c -fgo-pkgpath=`echo $@ | sed -e 's/.lo$$//' -e 's/-go$$//'` $($(subst -,_,$(subst .,_,$(subst /,_,$@)))_GOCFLAGS) -o $@ $$files

# Build deps for netgo.o.
BUILDNETGODEPS = \
Expand Down Expand Up @@ -1006,7 +1023,7 @@ bytes.lo.dep: $(srcdir)/go/bytes/*.go
$(BUILDDEPS)
bytes.lo:
$(BUILDPACKAGE)
bytes/index.lo: go/bytes/indexbyte.c
bytes/index.lo: go/bytes/indexbyte.c runtime.inc
@$(MKDIR_P) bytes
$(LTCOMPILE) -c -o bytes/index.lo $(srcdir)/go/bytes/indexbyte.c
bytes/check: $(CHECK_DEPS)
Expand Down Expand Up @@ -1191,7 +1208,7 @@ reflect-go.lo:
$(BUILDPACKAGE)
reflect/check: $(CHECK_DEPS)
@$(CHECK)
reflect/makefunc_ffi_c.lo: go/reflect/makefunc_ffi_c.c
reflect/makefunc_ffi_c.lo: go/reflect/makefunc_ffi_c.c runtime.inc
@$(MKDIR_P) reflect
$(LTCOMPILE) -c -o $@ $<
.PHONY: reflect/check
Expand All @@ -1205,13 +1222,18 @@ regexp/check: $(CHECK_DEPS)
@$(CHECK)
.PHONY: regexp/check

extra_go_files_runtime = version.go
extra_go_files_runtime = runtime_sysinfo.go version.go

@go_include@ runtime-go.lo.dep
runtime-go.lo.dep: $(srcdir)/go/runtime/*.go
runtime-go.lo.dep: $(srcdir)/go/runtime/*.go $(extra_go_files_runtime)
$(BUILDDEPS)
runtime_go_lo_GOCFLAGS = -fgo-c-header=runtime.inc.tmp -fgo-compiling-runtime
runtime-go.lo:
$(BUILDPACKAGE)
runtime.inc: s-runtime-inc; @true
s-runtime-inc: runtime-go.lo
$(SHELL) $(srcdir)/mvifdiff.sh runtime.inc.tmp runtime.inc
$(STAMP) $@
runtime/check: $(CHECK_DEPS)
@$(CHECK)
.PHONY: runtime/check
Expand Down Expand Up @@ -1239,7 +1261,7 @@ strings.lo.dep: $(srcdir)/go/strings/*.go
$(BUILDDEPS)
strings.lo:
$(BUILDPACKAGE)
strings/index.lo: go/strings/indexbyte.c
strings/index.lo: go/strings/indexbyte.c runtime.inc
@$(MKDIR_P) strings
$(LTCOMPILE) -c -o strings/index.lo $(srcdir)/go/strings/indexbyte.c
strings/check: $(CHECK_DEPS)
Expand Down Expand Up @@ -2062,7 +2084,7 @@ log/syslog.lo.dep: $(srcdir)/go/log/syslog/*.go
$(BUILDDEPS)
log/syslog.lo:
$(BUILDPACKAGE)
log/syslog/syslog_c.lo: go/log/syslog/syslog_c.c log/syslog.lo
log/syslog/syslog_c.lo: go/log/syslog/syslog_c.c runtime.inc log/syslog.lo
@$(MKDIR_P) log/syslog
$(LTCOMPILE) -c -o $@ $(srcdir)/go/log/syslog/syslog_c.c
log/syslog/check: $(CHECK_DEPS)
Expand Down Expand Up @@ -2348,7 +2370,7 @@ sync/atomic.lo.dep: $(srcdir)/go/sync/atomic/*.go
$(BUILDDEPS)
sync/atomic.lo:
$(BUILDPACKAGE)
sync/atomic_c.lo: go/sync/atomic/atomic.c sync/atomic.lo
sync/atomic_c.lo: go/sync/atomic/atomic.c runtime.inc sync/atomic.lo
$(LTCOMPILE) -c -o $@ $(srcdir)/go/sync/atomic/atomic.c
sync/atomic/check: $(CHECK_DEPS)
@$(CHECK)
Expand Down Expand Up @@ -2427,17 +2449,17 @@ unicode/utf8/check: $(CHECK_DEPS)
.PHONY: unicode/utf8/check

@go_include@ syscall.lo.dep
syscall.lo.dep: $(srcdir)/go/syscall/*.go
syscall.lo.dep: $(srcdir)/go/syscall/*.go $(extra_go_files_syscall)
$(BUILDDEPS)
syscall.lo:
$(BUILDPACKAGE)
syscall/errno.lo: go/syscall/errno.c
syscall/errno.lo: go/syscall/errno.c runtime.inc
@$(MKDIR_P) syscall
$(LTCOMPILE) -c -o $@ $<
syscall/signame.lo: go/syscall/signame.c
syscall/signame.lo: go/syscall/signame.c runtime.inc
@$(MKDIR_P) syscall
$(LTCOMPILE) -c -o $@ $<
syscall/wait.lo: go/syscall/wait.c
syscall/wait.lo: go/syscall/wait.c runtime.inc
@$(MKDIR_P) syscall
$(LTCOMPILE) -c -o $@ $<
syscall/check: $(CHECK_DEPS)
Expand Down
46 changes: 34 additions & 12 deletions libgo/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ BUILDDEPS = \
BUILDPACKAGE = \
$(MKDIR_P) $(@D); \
files=`echo $^ | sed -e 's/[^ ]*\.gox//g' -e 's/[^ ]*\.dep//'`; \
$(LTGOCOMPILE) -I . -c -fgo-pkgpath=`echo $@ | sed -e 's/.lo$$//' -e 's/-go$$//'` -o $@ $$files
$(LTGOCOMPILE) -I . -c -fgo-pkgpath=`echo $@ | sed -e 's/.lo$$//' -e 's/-go$$//'` $($(subst -,_,$(subst .,_,$(subst /,_,$@)))_GOCFLAGS) -o $@ $$files


# Build deps for netgo.o.
Expand Down Expand Up @@ -1235,7 +1235,8 @@ CHECK_DEPS = $(toolexeclibgo_DATA) $(toolexeclibgoarchive_DATA) \
@HAVE_STAT_TIMESPEC_FALSE@@LIBGO_IS_SOLARIS_TRUE@matchargs_os =
@HAVE_STAT_TIMESPEC_TRUE@@LIBGO_IS_SOLARIS_TRUE@matchargs_os = --tag=solaristag
@LIBGO_IS_SOLARIS_FALSE@matchargs_os =
extra_go_files_runtime = version.go
extra_go_files_runtime = runtime_sysinfo.go version.go
runtime_go_lo_GOCFLAGS = -fgo-c-header=runtime.inc.tmp -fgo-compiling-runtime
@LIBGO_IS_BSD_TRUE@golang_org_x_net_route_lo = \
@LIBGO_IS_BSD_TRUE@ golang_org/x/net/route/route.lo

Expand Down Expand Up @@ -3570,6 +3571,16 @@ s-version: Makefile
$(SHELL) $(srcdir)/mvifdiff.sh version.go.tmp version.go
$(STAMP) $@

runtime_sysinfo.go: s-runtime_sysinfo; @true
s-runtime_sysinfo: sysinfo.go
rm -f tmp-runtime_sysinfo.go
echo 'package runtime' > tmp-runtime_sysinfo.go
echo >> tmp-runtime_sysinfo.go
grep 'const _sizeof_ucontext_t ' sysinfo.go >> tmp-runtime_sysinfo.go
grep 'type _sigset_t ' sysinfo.go >> tmp-runtime_sysinfo.go
$(SHELL) $(srcdir)/mvifdiff.sh tmp-runtime_sysinfo.go runtime_sysinfo.go
$(STAMP) $@

# Generate the list of go std packages that were included in libgo
zstdpkglist.go: s-zstdpkglist; @true
s-zstdpkglist: Makefile
Expand Down Expand Up @@ -3639,6 +3650,13 @@ s-epoll: Makefile
$(SHELL) $(srcdir)/mvifdiff.sh epoll.go.tmp epoll.go
$(STAMP) $@

# Make sure runtime.inc is built before compiling any .c file.
$(libgo_la_OBJECTS): runtime.inc
$(libgo_llgo_la_OBJECTS): runtime.inc
$(libgobegin_a_OBJECTS): runtime.inc
$(libgobegin_llgo_a_OBJECTS): runtime.inc
$(libgolibbegin_a_OBJECTS): runtime.inc

@go_include@ bufio.lo.dep
bufio.lo.dep: $(srcdir)/go/bufio/*.go
$(BUILDDEPS)
Expand All @@ -3653,7 +3671,7 @@ bytes.lo.dep: $(srcdir)/go/bytes/*.go
$(BUILDDEPS)
bytes.lo:
$(BUILDPACKAGE)
bytes/index.lo: go/bytes/indexbyte.c
bytes/index.lo: go/bytes/indexbyte.c runtime.inc
@$(MKDIR_P) bytes
$(LTCOMPILE) -c -o bytes/index.lo $(srcdir)/go/bytes/indexbyte.c
bytes/check: $(CHECK_DEPS)
Expand Down Expand Up @@ -3828,7 +3846,7 @@ reflect-go.lo:
$(BUILDPACKAGE)
reflect/check: $(CHECK_DEPS)
@$(CHECK)
reflect/makefunc_ffi_c.lo: go/reflect/makefunc_ffi_c.c
reflect/makefunc_ffi_c.lo: go/reflect/makefunc_ffi_c.c runtime.inc
@$(MKDIR_P) reflect
$(LTCOMPILE) -c -o $@ $<
.PHONY: reflect/check
Expand All @@ -3843,10 +3861,14 @@ regexp/check: $(CHECK_DEPS)
.PHONY: regexp/check

@go_include@ runtime-go.lo.dep
runtime-go.lo.dep: $(srcdir)/go/runtime/*.go
runtime-go.lo.dep: $(srcdir)/go/runtime/*.go $(extra_go_files_runtime)
$(BUILDDEPS)
runtime-go.lo:
$(BUILDPACKAGE)
runtime.inc: s-runtime-inc; @true
s-runtime-inc: runtime-go.lo
$(SHELL) $(srcdir)/mvifdiff.sh runtime.inc.tmp runtime.inc
$(STAMP) $@
runtime/check: $(CHECK_DEPS)
@$(CHECK)
.PHONY: runtime/check
Expand Down Expand Up @@ -3874,7 +3896,7 @@ strings.lo.dep: $(srcdir)/go/strings/*.go
$(BUILDDEPS)
strings.lo:
$(BUILDPACKAGE)
strings/index.lo: go/strings/indexbyte.c
strings/index.lo: go/strings/indexbyte.c runtime.inc
@$(MKDIR_P) strings
$(LTCOMPILE) -c -o strings/index.lo $(srcdir)/go/strings/indexbyte.c
strings/check: $(CHECK_DEPS)
Expand Down Expand Up @@ -4688,7 +4710,7 @@ log/syslog.lo.dep: $(srcdir)/go/log/syslog/*.go
$(BUILDDEPS)
log/syslog.lo:
$(BUILDPACKAGE)
log/syslog/syslog_c.lo: go/log/syslog/syslog_c.c log/syslog.lo
log/syslog/syslog_c.lo: go/log/syslog/syslog_c.c runtime.inc log/syslog.lo
@$(MKDIR_P) log/syslog
$(LTCOMPILE) -c -o $@ $(srcdir)/go/log/syslog/syslog_c.c
log/syslog/check: $(CHECK_DEPS)
Expand Down Expand Up @@ -4970,7 +4992,7 @@ sync/atomic.lo.dep: $(srcdir)/go/sync/atomic/*.go
$(BUILDDEPS)
sync/atomic.lo:
$(BUILDPACKAGE)
sync/atomic_c.lo: go/sync/atomic/atomic.c sync/atomic.lo
sync/atomic_c.lo: go/sync/atomic/atomic.c runtime.inc sync/atomic.lo
$(LTCOMPILE) -c -o $@ $(srcdir)/go/sync/atomic/atomic.c
sync/atomic/check: $(CHECK_DEPS)
@$(CHECK)
Expand Down Expand Up @@ -5049,17 +5071,17 @@ unicode/utf8/check: $(CHECK_DEPS)
.PHONY: unicode/utf8/check

@go_include@ syscall.lo.dep
syscall.lo.dep: $(srcdir)/go/syscall/*.go
syscall.lo.dep: $(srcdir)/go/syscall/*.go $(extra_go_files_syscall)
$(BUILDDEPS)
syscall.lo:
$(BUILDPACKAGE)
syscall/errno.lo: go/syscall/errno.c
syscall/errno.lo: go/syscall/errno.c runtime.inc
@$(MKDIR_P) syscall
$(LTCOMPILE) -c -o $@ $<
syscall/signame.lo: go/syscall/signame.c
syscall/signame.lo: go/syscall/signame.c runtime.inc
@$(MKDIR_P) syscall
$(LTCOMPILE) -c -o $@ $<
syscall/wait.lo: go/syscall/wait.c
syscall/wait.lo: go/syscall/wait.c runtime.inc
@$(MKDIR_P) syscall
$(LTCOMPILE) -c -o $@ $<
syscall/check: $(CHECK_DEPS)
Expand Down
102 changes: 102 additions & 0 deletions libgo/go/runtime/mcache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2009 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

// This is a temporary mcache.go for gccgo.
// At some point it will be replaced by the one in the gc runtime package.

import "unsafe"

const (
// Computed constant. The definition of MaxSmallSize and the
// algorithm in msize.go produces some number of different allocation
// size classes. NumSizeClasses is that number. It's needed here
// because there are static arrays of this length; when msize runs its
// size choosing algorithm it double-checks that NumSizeClasses agrees.
_NumSizeClasses = 67
)

type mcachelist struct {
list *mlink
nlist uint32
}

// Per-thread (in Go, per-P) cache for small objects.
// No locking needed because it is per-thread (per-P).
//
// mcaches are allocated from non-GC'd memory, so any heap pointers
// must be specially handled.
type mcache struct {
// The following members are accessed on every malloc,
// so they are grouped here for better caching.
next_sample int32 // trigger heap sample after allocating this many bytes
local_cachealloc uintptr // bytes allocated (or freed) from cache since last lock of heap

// Allocator cache for tiny objects w/o pointers.
// See "Tiny allocator" comment in malloc.go.

// tiny points to the beginning of the current tiny block, or
// nil if there is no current tiny block.
//
// tiny is a heap pointer. Since mcache is in non-GC'd memory,
// we handle it by clearing it in releaseAll during mark
// termination.
tiny unsafe.Pointer
tinysize uintptr

// The rest is not accessed on every malloc.
alloc [_NumSizeClasses]*mspan // spans to allocate from
free [_NumSizeClasses]mcachelist // lists of explicitly freed objects

// Local allocator stats, flushed during GC.
local_nlookup uintptr // number of pointer lookups
local_largefree uintptr // bytes freed for large objects (>maxsmallsize)
local_nlargefree uintptr // number of frees for large objects (>maxsmallsize)
local_nsmallfree [_NumSizeClasses]uintptr // number of frees for small objects (<=maxsmallsize)
}

type mtypes struct {
compression byte
data uintptr
}

type special struct {
next *special
offset uint16
kind byte
}

type mspan struct {
next *mspan // next span in list, or nil if none
prev *mspan // previous span's next field, or list head's first field if none
start uintptr
npages uintptr // number of pages in span
freelist *mlink

// sweep generation:
// if sweepgen == h->sweepgen - 2, the span needs sweeping
// if sweepgen == h->sweepgen - 1, the span is currently being swept
// if sweepgen == h->sweepgen, the span is swept and ready to use
// h->sweepgen is incremented by 2 after every GC

sweepgen uint32
ref uint16
sizeclass uint8 // size class
incache bool // being used by an mcache
state uint8 // mspaninuse etc
needzero uint8 // needs to be zeroed before allocation
elemsize uintptr // computed from sizeclass or from npages
unusedsince int64 // first time spotted by gc in mspanfree state
npreleased uintptr // number of pages released to the os
limit uintptr // end of data in span
types mtypes
speciallock mutex // guards specials list
specials *special // linked list of special records sorted by offset.
freebuf *mlink
}

type mlink struct {
next *mlink
}
Loading

0 comments on commit 9ac07c1

Please sign in to comment.