From db2a06a9bb4a16850511294b7418fca6422e668b Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sat, 22 Jun 2024 17:56:29 +0200 Subject: [PATCH] internal/abi: implement initial version of this package This package can never be a full version as seen in upstream Go, because TinyGo is very different. But it is necessary to define so that no code can accidentally use this package (now or in the future). It currently defines: - NoEscape which is needed by strings.Builder since Go 1.23. - FuncPCABI* which is needed by internal/syscall/unix on MacOS. --- compiler/symbol.go | 2 ++ loader/goroot.go | 1 + src/internal/abi/abi.go | 2 ++ src/internal/abi/escape.go | 10 ++++++++++ src/internal/abi/funcpc.go | 10 ++++++++++ 5 files changed, 25 insertions(+) create mode 100644 src/internal/abi/abi.go create mode 100644 src/internal/abi/escape.go create mode 100644 src/internal/abi/funcpc.go diff --git a/compiler/symbol.go b/compiler/symbol.go index 83bb4ccb2..37c987859 100644 --- a/compiler/symbol.go +++ b/compiler/symbol.go @@ -139,6 +139,8 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value) // On *nix systems, the "abort" functuion in libc is used to handle fatal panics. // Mark it as noreturn so LLVM can optimize away code. llvmFn.AddFunctionAttr(c.ctx.CreateEnumAttribute(llvm.AttributeKindID("noreturn"), 0)) + case "internal/abi.NoEscape": + llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0)) case "runtime.alloc": // Tell the optimizer that runtime.alloc is an allocator, meaning that it // returns values that are never null and never alias to an existing value. diff --git a/loader/goroot.go b/loader/goroot.go index 7325db5b4..09edab320 100644 --- a/loader/goroot.go +++ b/loader/goroot.go @@ -236,6 +236,7 @@ func pathsToOverride(goMinor int, needsSyscallPackage bool) map[string]bool { "device/": false, "examples/": false, "internal/": true, + "internal/abi/": false, "internal/binary/": false, "internal/bytealg/": false, "internal/cm/": false, diff --git a/src/internal/abi/abi.go b/src/internal/abi/abi.go new file mode 100644 index 000000000..ee8e212a8 --- /dev/null +++ b/src/internal/abi/abi.go @@ -0,0 +1,2 @@ +// Package abi exposes low-level details of the Go compiler/runtime +package abi diff --git a/src/internal/abi/escape.go b/src/internal/abi/escape.go new file mode 100644 index 000000000..0ecdf8030 --- /dev/null +++ b/src/internal/abi/escape.go @@ -0,0 +1,10 @@ +package abi + +import "unsafe" + +// Tell the compiler the given pointer doesn't escape. +// The compiler knows about this function and will give the nocapture parameter +// attribute. +func NoEscape(p unsafe.Pointer) unsafe.Pointer { + return p +} diff --git a/src/internal/abi/funcpc.go b/src/internal/abi/funcpc.go new file mode 100644 index 000000000..1f99b8095 --- /dev/null +++ b/src/internal/abi/funcpc.go @@ -0,0 +1,10 @@ +package abi + +// These two signatures are present to satisfy the expectation of some programs +// (in particular internal/syscall/unix on MacOS). They do not currently have an +// implementation, in part because TinyGo doesn't use ABI0 or ABIInternal (it +// uses a C-like calling convention). + +func FuncPCABI0(f interface{}) uintptr + +func FuncPCABIInternal(f interface{}) uintptr