
This has several advantages, among them: - Many passes (heap-to-stack, dead arg elimination, inlining) do not work with function pointer calls. Making them normal function calls improves their effectiveness. - Goroutine lowering to LLVM coroutines does not currently support function pointers. By eliminating function pointers, coroutine lowering gets support for them for free. This is especially useful for WebAssembly. Because of the second point, this work is currently only enabled for the WebAssembly target.
28 строки
1 КиБ
Go
28 строки
1 КиБ
Go
package runtime
|
|
|
|
// This file implements some data types that may be useful for some
|
|
// implementations of func values.
|
|
|
|
import (
|
|
"unsafe"
|
|
)
|
|
|
|
// funcValue is the underlying type of func values, depending on which func
|
|
// value representation was used.
|
|
type funcValue struct {
|
|
context unsafe.Pointer // function context, for closures and bound methods
|
|
id uintptr // ptrtoint of *funcValueWithSignature before lowering, opaque index (non-0) after lowering
|
|
}
|
|
|
|
// funcValueWithSignature is used before the func lowering pass.
|
|
type funcValueWithSignature struct {
|
|
funcPtr uintptr // ptrtoint of the actual function pointer
|
|
signature *uint8 // pointer to identify this signature (the value is undef)
|
|
}
|
|
|
|
// getFuncPtr is a dummy function that may be used if the func lowering pass is
|
|
// not used. It is generally too slow but may be a useful fallback to debug the
|
|
// func lowering pass.
|
|
func getFuncPtr(val funcValue, signature *uint8) uintptr {
|
|
return (*funcValueWithSignature)(unsafe.Pointer(val.id)).funcPtr
|
|
}
|