Merge remote-tracking branch 'gop/main' into q

This commit is contained in:
xushiwei
2024-06-30 11:16:53 +08:00
26 changed files with 759 additions and 37 deletions

View File

@@ -38,8 +38,8 @@ import (
"github.com/goplus/llgo/internal/packages"
"github.com/goplus/llgo/internal/typepatch"
"github.com/goplus/llgo/ssa/abi"
"github.com/goplus/llgo/xtool/clang"
"github.com/goplus/llgo/xtool/env"
"github.com/goplus/llgo/xtool/env/llvm"
llssa "github.com/goplus/llgo/ssa"
clangCheck "github.com/goplus/llgo/xtool/clang/check"
@@ -309,21 +309,29 @@ func linkMainPkg(pkg *packages.Package, pkgs []*aPackage, llFiles []string, conf
if app == "" {
app = filepath.Join(conf.BinPath, name+conf.AppExt)
}
const N = 6
args := make([]string, N, len(pkg.Imports)+len(llFiles)+(N+1))
args[0] = "-o"
args[1] = app
args[2] = "-Wno-override-module"
args[3] = "-Xlinker"
if runtime.GOOS == "darwin" { // ld64.lld (macOS)
args[4] = "-dead_strip"
args[5] = "" // It's ok to leave it empty, as we can assume libpthread is built-in on macOS.
} else { // ld.lld (Unix), lld-link (Windows), wasm-ld (WebAssembly)
args[4] = "--gc-sections"
args[5] = "-lpthread" // libpthread is built-in since glibc 2.34 (2021-08-01); we need to support earlier versions.
args := make([]string, 0, len(pkg.Imports)+len(llFiles)+10)
args = append(
args,
"-o", app,
"-fuse-ld=lld",
"-Wno-override-module",
// "-O2", // FIXME: This will cause TestFinalizer in _test/bdwgc.go to fail on macOS.
)
switch runtime.GOOS {
case "darwin": // ld64.lld (macOS)
args = append(
args,
"-Xlinker", "-dead_strip",
)
case "windows": // lld-link (Windows)
// TODO: Add options for Windows.
default: // ld.lld (Unix), wasm-ld (WebAssembly)
args = append(
args,
"-Xlinker", "--gc-sections",
"-lpthread", // libpthread is built-in since glibc 2.34 (2021-08-01); we need to support earlier versions.
)
}
//args[6] = "-fuse-ld=lld" // TODO(xsw): to check lld exists or not
//args[7] = "-O2"
needRuntime := false
needPyInit := false
packages.Visit([]*packages.Package{pkg}, nil, func(p *packages.Package) {
@@ -379,7 +387,7 @@ func linkMainPkg(pkg *packages.Package, pkgs []*aPackage, llFiles []string, conf
if verbose {
fmt.Fprintln(os.Stderr, "clang", args)
}
err := clang.New("").Exec(args...)
err := llvm.New("").Clang().Exec(args...)
check(err)
switch mode {
@@ -647,7 +655,7 @@ func clFile(cFile, expFile string, procFile func(linkFile string), verbose bool)
if verbose {
fmt.Fprintln(os.Stderr, "clang", args)
}
err := clang.New("").Exec(args...)
err := llvm.New("").Clang().Exec(args...)
check(err)
procFile(llFile)
}

View File

@@ -21,6 +21,7 @@ import (
"unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/internal/runtime"
)
func IndexByte(b []byte, ch byte) int {
@@ -40,3 +41,67 @@ func IndexByteString(s string, ch byte) int {
}
return -1
}
func Count(b []byte, c byte) (n int) {
for _, x := range b {
if x == c {
n++
}
}
return
}
func CountString(s string, c byte) (n int) {
for i := 0; i < len(s); i++ {
if s[i] == c {
n++
}
}
return
}
// Index returns the index of the first instance of b in a, or -1 if b is not present in a.
// Requires 2 <= len(b) <= MaxLen.
func Index(a, b []byte) int {
for i := 0; i <= len(a)-len(b); i++ {
if equal(a[i:i+len(b)], b) {
return i
}
}
return -1
}
func equal(a, b []byte) bool {
if n := len(a); n == len(b) {
return c.Memcmp(unsafe.Pointer(unsafe.SliceData(a)), unsafe.Pointer(unsafe.SliceData(b)), uintptr(n)) == 0
}
return false
}
// IndexString returns the index of the first instance of b in a, or -1 if b is not present in a.
// Requires 2 <= len(b) <= MaxLen.
func IndexString(a, b string) int {
for i := 0; i <= len(a)-len(b); i++ {
if a[i:i+len(b)] == b {
return i
}
}
return -1
}
// MakeNoZero makes a slice of length and capacity n without zeroing the bytes.
// It is the caller's responsibility to ensure uninitialized bytes
// do not leak to the end user.
func MakeNoZero(n int) (r []byte) {
s := (*sliceHead)(unsafe.Pointer(&r))
s.data = runtime.AllocU(uintptr(n))
s.len = n
s.cap = n
return
}
type sliceHead struct {
data unsafe.Pointer
len int
cap int
}