libgo: update to go1.9
[official-gcc.git] / libgo / go / runtime / stubs.go
blob84fa1c79689d927c27947bf23a43dc26a263c739
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package runtime
7 import (
8 "runtime/internal/atomic"
9 "runtime/internal/sys"
10 "unsafe"
13 // Should be a built-in for unsafe.Pointer?
14 //go:nosplit
15 func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
16 return unsafe.Pointer(uintptr(p) + x)
19 // getg returns the pointer to the current g.
20 // The compiler rewrites calls to this function into instructions
21 // that fetch the g directly (from TLS or from the dedicated register).
22 func getg() *g
24 // mcall switches from the g to the g0 stack and invokes fn(g),
25 // where g is the goroutine that made the call.
26 // mcall saves g's current PC/SP in g->sched so that it can be restored later.
27 // It is up to fn to arrange for that later execution, typically by recording
28 // g in a data structure, causing something to call ready(g) later.
29 // mcall returns to the original goroutine g later, when g has been rescheduled.
30 // fn must not return at all; typically it ends by calling schedule, to let the m
31 // run other goroutines.
33 // mcall can only be called from g stacks (not g0, not gsignal).
35 // This must NOT be go:noescape: if fn is a stack-allocated closure,
36 // fn puts g on a run queue, and g executes before fn returns, the
37 // closure will be invalidated while it is still executing.
38 func mcall(fn func(*g))
40 // systemstack runs fn on a system stack.
42 // It is common to use a func literal as the argument, in order
43 // to share inputs and outputs with the code around the call
44 // to system stack:
46 // ... set up y ...
47 // systemstack(func() {
48 // x = bigcall(y)
49 // })
50 // ... use x ...
52 // For the gc toolchain this permits running a function that requires
53 // additional stack space in a context where the stack can not be
54 // split. We don't really need additional stack space in gccgo, since
55 // stack splitting is handled separately. But to keep things looking
56 // the same, we do switch to the g0 stack here if necessary.
57 func systemstack(fn func()) {
58 gp := getg()
59 mp := gp.m
60 if gp == mp.g0 || gp == mp.gsignal {
61 fn()
62 } else if gp == mp.curg {
63 mcall(func(origg *g) {
64 fn()
65 gogo(origg)
67 } else {
68 badsystemstack()
72 func badsystemstack() {
73 throw("systemstack called from unexpected goroutine")
76 // memclrNoHeapPointers clears n bytes starting at ptr.
78 // Usually you should use typedmemclr. memclrNoHeapPointers should be
79 // used only when the caller knows that *ptr contains no heap pointers
80 // because either:
82 // 1. *ptr is initialized memory and its type is pointer-free.
84 // 2. *ptr is uninitialized memory (e.g., memory that's being reused
85 // for a new allocation) and hence contains only "junk".
87 // in memclr_*.s
88 //go:noescape
89 func memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr)
91 //go:linkname reflect_memclrNoHeapPointers reflect.memclrNoHeapPointers
92 func reflect_memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr) {
93 memclrNoHeapPointers(ptr, n)
96 // memmove copies n bytes from "from" to "to".
97 //go:noescape
98 func memmove(to, from unsafe.Pointer, n uintptr)
100 //go:linkname reflect_memmove reflect.memmove
101 func reflect_memmove(to, from unsafe.Pointer, n uintptr) {
102 memmove(to, from, n)
105 //go:noescape
106 //extern __builtin_memcmp
107 func memcmp(a, b unsafe.Pointer, size uintptr) int32
109 // exported value for testing
110 var hashLoad = loadFactor
112 //go:nosplit
113 func fastrand() uint32 {
114 mp := getg().m
115 fr := mp.fastrand
116 mx := uint32(int32(fr)>>31) & 0xa8888eef
117 fr = fr<<1 ^ mx
118 mp.fastrand = fr
119 return fr
122 //go:nosplit
123 func fastrandn(n uint32) uint32 {
124 // This is similar to fastrand() % n, but faster.
125 // See http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
126 return uint32(uint64(fastrand()) * uint64(n) >> 32)
129 //go:linkname sync_fastrand sync.fastrand
130 func sync_fastrand() uint32 { return fastrand() }
132 // in asm_*.s
133 //go:noescape
134 func memequal(a, b unsafe.Pointer, size uintptr) bool
136 // noescape hides a pointer from escape analysis. noescape is
137 // the identity function but escape analysis doesn't think the
138 // output depends on the input. noescape is inlined and currently
139 // compiles down to zero instructions.
140 // USE CAREFULLY!
141 //go:nosplit
142 func noescape(p unsafe.Pointer) unsafe.Pointer {
143 x := uintptr(p)
144 return unsafe.Pointer(x ^ 0)
147 //go:noescape
148 func jmpdefer(fv *funcval, argp uintptr)
149 func exit1(code int32)
150 func setg(gg *g)
152 //extern __builtin_trap
153 func breakpoint()
155 func asminit() {}
157 //go:linkname reflectcall reflect.call
158 func reflectcall(fntype *functype, fn *funcval, isInterface, isMethod bool, params, results *unsafe.Pointer)
160 func procyield(cycles uint32)
162 type neverCallThisFunction struct{}
164 // goexit is the return stub at the top of every goroutine call stack.
165 // Each goroutine stack is constructed as if goexit called the
166 // goroutine's entry point function, so that when the entry point
167 // function returns, it will return to goexit, which will call goexit1
168 // to perform the actual exit.
170 // This function must never be called directly. Call goexit1 instead.
171 // gentraceback assumes that goexit terminates the stack. A direct
172 // call on the stack will cause gentraceback to stop walking the stack
173 // prematurely and if there is leftover state it may panic.
174 func goexit(neverCallThisFunction)
176 // publicationBarrier performs a store/store barrier (a "publication"
177 // or "export" barrier). Some form of synchronization is required
178 // between initializing an object and making that object accessible to
179 // another processor. Without synchronization, the initialization
180 // writes and the "publication" write may be reordered, allowing the
181 // other processor to follow the pointer and observe an uninitialized
182 // object. In general, higher-level synchronization should be used,
183 // such as locking or an atomic pointer write. publicationBarrier is
184 // for when those aren't an option, such as in the implementation of
185 // the memory manager.
187 // There's no corresponding barrier for the read side because the read
188 // side naturally has a data dependency order. All architectures that
189 // Go supports or seems likely to ever support automatically enforce
190 // data dependency ordering.
191 func publicationBarrier()
193 // getcallerpc returns the program counter (PC) of its caller's caller.
194 // getcallersp returns the stack pointer (SP) of its caller's caller.
195 // For both, the argp must be a pointer to the caller's first function argument.
196 // The implementation may or may not use argp, depending on
197 // the architecture.
199 // For example:
201 // func f(arg1, arg2, arg3 int) {
202 // pc := getcallerpc(unsafe.Pointer(&arg1))
203 // sp := getcallersp(unsafe.Pointer(&arg1))
204 // }
206 // These two lines find the PC and SP immediately following
207 // the call to f (where f will return).
209 // The call to getcallerpc and getcallersp must be done in the
210 // frame being asked about. It would not be correct for f to pass &arg1
211 // to another function g and let g call getcallerpc/getcallersp.
212 // The call inside g might return information about g's caller or
213 // information about f's caller or complete garbage.
215 // The result of getcallersp is correct at the time of the return,
216 // but it may be invalidated by any subsequent call to a function
217 // that might relocate the stack in order to grow or shrink it.
218 // A general rule is that the result of getcallersp should be used
219 // immediately and can only be passed to nosplit functions.
221 //go:noescape
222 func getcallerpc(argp unsafe.Pointer) uintptr
224 //go:noescape
225 func getcallersp(argp unsafe.Pointer) uintptr
227 func asmcgocall(fn, arg unsafe.Pointer) int32 {
228 throw("asmcgocall")
229 return 0
232 // argp used in Defer structs when there is no argp.
233 const _NoArgs = ^uintptr(0)
235 //extern __builtin_prefetch
236 func prefetch(addr unsafe.Pointer, rw int32, locality int32)
238 func prefetcht0(addr uintptr) {
239 prefetch(unsafe.Pointer(addr), 0, 3)
242 func prefetcht1(addr uintptr) {
243 prefetch(unsafe.Pointer(addr), 0, 2)
246 func prefetcht2(addr uintptr) {
247 prefetch(unsafe.Pointer(addr), 0, 1)
250 func prefetchnta(addr uintptr) {
251 prefetch(unsafe.Pointer(addr), 0, 0)
254 // round n up to a multiple of a. a must be a power of 2.
255 func round(n, a uintptr) uintptr {
256 return (n + a - 1) &^ (a - 1)
259 // checkASM returns whether assembly runtime checks have passed.
260 func checkASM() bool {
261 return true
264 func eqstring(x, y string) bool {
265 a := stringStructOf(&x)
266 b := stringStructOf(&y)
267 if a.len != b.len {
268 return false
270 if a.str == b.str {
271 return true
273 return memequal(a.str, b.str, uintptr(a.len))
276 // For gccgo this is in the C code.
277 func osyield()
279 // For gccgo this can be called directly.
280 //extern syscall
281 func syscall(trap uintptr, a1, a2, a3, a4, a5, a6 uintptr) uintptr
283 // For gccgo, to communicate from the C code to the Go code.
284 //go:linkname setIsCgo runtime.setIsCgo
285 func setIsCgo() {
286 iscgo = true
289 // For gccgo, to communicate from the C code to the Go code.
290 //go:linkname setCpuidECX runtime.setCpuidECX
291 func setCpuidECX(v uint32) {
292 cpuid_ecx = v
295 // For gccgo, to communicate from the C code to the Go code.
296 //go:linkname setSupportAES runtime.setSupportAES
297 func setSupportAES(v bool) {
298 support_aes = v
301 // Here for gccgo until we port atomic_pointer.go and mgc.go.
302 //go:nosplit
303 func casp(ptr *unsafe.Pointer, old, new unsafe.Pointer) bool {
304 if !atomic.Casp1((*unsafe.Pointer)(noescape(unsafe.Pointer(ptr))), noescape(old), new) {
305 return false
307 return true
310 // Here for gccgo until we port lock_*.go.
311 func lock(l *mutex)
312 func unlock(l *mutex)
314 // Here for gccgo.
315 func errno() int
317 // Temporary for gccgo until we port proc.go.
318 func entersyscall(int32)
319 func entersyscallblock(int32)
321 // Here for gccgo until we port mgc.go.
322 func GC()
324 // For gccgo to call from C code, so that the C code and the Go code
325 // can share the memstats variable for now.
326 //go:linkname getMstats runtime.getMstats
327 func getMstats() *mstats {
328 return &memstats
331 // Temporary for gccgo until we port mem_GOOS.go.
332 func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer
333 func sysFree(v unsafe.Pointer, n uintptr, sysStat *uint64)
335 // Temporary for gccgo until we port malloc.go
336 func persistentalloc(size, align uintptr, sysStat *uint64) unsafe.Pointer
338 // Temporary for gccgo until we port mheap.go
339 func setprofilebucket(p unsafe.Pointer, b *bucket)
341 // Temporary for gccgo until we port atomic_pointer.go.
342 //go:nosplit
343 func atomicstorep(ptr unsafe.Pointer, new unsafe.Pointer) {
344 atomic.StorepNoWB(noescape(ptr), new)
347 // Get signal trampoline, written in C.
348 func getSigtramp() uintptr
350 // The sa_handler field is generally hidden in a union, so use C accessors.
351 func getSigactionHandler(*_sigaction) uintptr
352 func setSigactionHandler(*_sigaction, uintptr)
354 // Retrieve fields from the siginfo_t and ucontext_t pointers passed
355 // to a signal handler using C, as they are often hidden in a union.
356 // Returns and, if available, PC where signal occurred.
357 func getSiginfo(*_siginfo_t, unsafe.Pointer) (sigaddr uintptr, sigpc uintptr)
359 // Implemented in C for gccgo.
360 func dumpregs(*_siginfo_t, unsafe.Pointer)
362 // Temporary for gccgo until we port proc.go.
363 //go:linkname getsched runtime.getsched
364 func getsched() *schedt {
365 return &sched
368 // Temporary for gccgo until we port proc.go.
369 //go:linkname getCgoHasExtraM runtime.getCgoHasExtraM
370 func getCgoHasExtraM() *bool {
371 return &cgoHasExtraM
374 // Temporary for gccgo until we port proc.go.
375 //go:linkname getAllP runtime.getAllP
376 func getAllP() **p {
377 return &allp[0]
380 // Temporary for gccgo until we port proc.go.
381 //go:linkname allocg runtime.allocg
382 func allocg() *g {
383 return new(g)
386 // Temporary for gccgo until we port the garbage collector.
387 //go:linkname getallglen runtime.getallglen
388 func getallglen() uintptr {
389 return allglen
392 // Temporary for gccgo until we port the garbage collector.
393 //go:linkname getallg runtime.getallg
394 func getallg(i int) *g {
395 return allgs[i]
398 // Temporary for gccgo until we port the garbage collector.
399 //go:linkname getallm runtime.getallm
400 func getallm() *m {
401 return allm
404 // Throw and rethrow an exception.
405 func throwException()
406 func rethrowException()
408 // Fetch the size and required alignment of the _Unwind_Exception type
409 // used by the stack unwinder.
410 func unwindExceptionSize() uintptr
412 // Temporary for gccgo until C code no longer needs it.
413 //go:nosplit
414 //go:linkname getPanicking runtime.getPanicking
415 func getPanicking() uint32 {
416 return panicking
419 // Called by C code to set the number of CPUs.
420 //go:linkname setncpu runtime.setncpu
421 func setncpu(n int32) {
422 ncpu = n
425 // Called by C code to set the page size.
426 //go:linkname setpagesize runtime.setpagesize
427 func setpagesize(s uintptr) {
428 if physPageSize == 0 {
429 physPageSize = s
433 // Temporary for gccgo until we port mgc.go.
434 //go:linkname runtime_m0 runtime.runtime_m0
435 func runtime_m0() *m {
436 return &m0
439 // Temporary for gccgo until we port mgc.go.
440 //go:linkname runtime_g0 runtime.runtime_g0
441 func runtime_g0() *g {
442 return &g0
445 const uintptrMask = 1<<(8*sys.PtrSize) - 1
447 type bitvector struct {
448 n int32 // # of bits
449 bytedata *uint8
452 // bool2int returns 0 if x is false or 1 if x is true.
453 func bool2int(x bool) int {
454 if x {
455 return 1
457 return 0