From 887ee0fd419d3b84b1d2ccadc7322d039593edd6 Mon Sep 17 00:00:00 2001 From: visualfc Date: Wed, 26 Jun 2024 20:09:38 +0800 Subject: [PATCH 1/4] ssa: fix slice high --- internal/lib/internal/bytealg/bytealg.go | 67 ++++++++++++++++++++++++ ssa/datastruct.go | 3 +- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/internal/lib/internal/bytealg/bytealg.go b/internal/lib/internal/bytealg/bytealg.go index c7cf2ac3..bf35e55c 100644 --- a/internal/lib/internal/bytealg/bytealg.go +++ b/internal/lib/internal/bytealg/bytealg.go @@ -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,69 @@ func IndexByteString(s string, ch byte) int { } return -1 } + +func Count(b []byte, c byte) int { + n := 0 + for _, x := range b { + if x == c { + n++ + } + } + return n +} + +func CountString(s string, c byte) int { + n := 0 + for i := 0; i < len(s); i++ { + if s[i] == c { + n++ + } + } + return n +} + +func Index(a, b []byte) int { + for i := 0; i <= len(b)-len(a); i++ { + if equal(a, b[i:i+len(a)]) { + return i + } + } + return -1 +} + +// 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 +} + +func equal(a, b []byte) bool { + s1 := (*sliceHead)(unsafe.Pointer(&a)) + s2 := (*sliceHead)(unsafe.Pointer(&b)) + if s1.len == s2.len { + return c.Memcmp(s1.data, s2.data, uintptr(s1.len)) == 0 + } + return false +} + +type sliceHead struct { + data unsafe.Pointer + len int + cap int +} diff --git a/ssa/datastruct.go b/ssa/datastruct.go index 82706e0a..7ec0e1ec 100644 --- a/ssa/datastruct.go +++ b/ssa/datastruct.go @@ -333,6 +333,7 @@ func (b Builder) Slice(x, low, high, max Expr) (ret Expr) { if debugInstr { log.Printf("Slice %v, %v, %v\n", x.impl, low.impl, high.impl) } + prog := b.Prog var nCap Expr var nEltSize Expr @@ -356,7 +357,7 @@ func (b Builder) Slice(x, low, high, max Expr) (ret Expr) { nEltSize = SizeOf(prog, prog.Index(x.Type)) nCap = b.SliceCap(x) if high.IsNil() { - high = b.SliceCap(x) + high = b.SliceLen(x) } ret.Type = x.Type base = b.SliceData(x) From e217d39882893407a9177c983093d03bcea786ab Mon Sep 17 00:00:00 2001 From: visualfc Date: Wed, 26 Jun 2024 20:13:43 +0800 Subject: [PATCH 2/4] internal/lib/internal/bytealg: count index --- internal/lib/internal/bytealg/bytealg.go | 32 +++++++++++------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/internal/lib/internal/bytealg/bytealg.go b/internal/lib/internal/bytealg/bytealg.go index bf35e55c..26979f76 100644 --- a/internal/lib/internal/bytealg/bytealg.go +++ b/internal/lib/internal/bytealg/bytealg.go @@ -42,35 +42,42 @@ func IndexByteString(s string, ch byte) int { return -1 } -func Count(b []byte, c byte) int { - n := 0 +func Count(b []byte, c byte) (n int) { for _, x := range b { if x == c { n++ } } - return n + return } -func CountString(s string, c byte) int { - n := 0 +func CountString(s string, c byte) (n int) { for i := 0; i < len(s); i++ { if s[i] == c { n++ } } - return 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(b)-len(a); i++ { - if equal(a, b[i:i+len(a)]) { + 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 { @@ -93,15 +100,6 @@ func MakeNoZero(n int) (r []byte) { return } -func equal(a, b []byte) bool { - s1 := (*sliceHead)(unsafe.Pointer(&a)) - s2 := (*sliceHead)(unsafe.Pointer(&b)) - if s1.len == s2.len { - return c.Memcmp(s1.data, s2.data, uintptr(s1.len)) == 0 - } - return false -} - type sliceHead struct { data unsafe.Pointer len int From 8169d8509ff6e59101cbf3554cf01ee2dba24e6c Mon Sep 17 00:00:00 2001 From: visualfc Date: Wed, 26 Jun 2024 20:30:00 +0800 Subject: [PATCH 3/4] cl/_testlibgo: bytes strings --- cl/_testlibgo/bytes/in.go | 15 ++++ cl/_testlibgo/bytes/out.ll | 111 ++++++++++++++++++++++++ cl/_testlibgo/strings/in.go | 20 +++++ cl/_testlibgo/strings/out.ll | 164 +++++++++++++++++++++++++++++++++++ cl/_testrt/builtin/out.ll | 4 +- ssa/datastruct.go | 1 - 6 files changed, 312 insertions(+), 3 deletions(-) create mode 100644 cl/_testlibgo/bytes/in.go create mode 100644 cl/_testlibgo/bytes/out.ll create mode 100644 cl/_testlibgo/strings/in.go create mode 100644 cl/_testlibgo/strings/out.ll diff --git a/cl/_testlibgo/bytes/in.go b/cl/_testlibgo/bytes/in.go new file mode 100644 index 00000000..5cf8f939 --- /dev/null +++ b/cl/_testlibgo/bytes/in.go @@ -0,0 +1,15 @@ +package main + +import ( + "bytes" +) + +func main() { + var b bytes.Buffer // A Buffer needs no initialization. + b.Write([]byte("Hello ")) + b.WriteString("World") + + println("buf", b.Bytes(), b.String()) + + println(bytes.EqualFold([]byte("Go"), []byte("go"))) +} diff --git a/cl/_testlibgo/bytes/out.ll b/cl/_testlibgo/bytes/out.ll new file mode 100644 index 00000000..f585a785 --- /dev/null +++ b/cl/_testlibgo/bytes/out.ll @@ -0,0 +1,111 @@ +; ModuleID = 'main' +source_filename = "main" + +%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 } +%"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 } +%"github.com/goplus/llgo/internal/runtime.iface" = type { ptr, ptr } + +@"main.init$guard" = global i1 false, align 1 +@__llgo_argc = global i32 0, align 4 +@__llgo_argv = global ptr null, align 8 +@0 = private unnamed_addr constant [6 x i8] c"Hello ", align 1 +@1 = private unnamed_addr constant [5 x i8] c"World", align 1 +@2 = private unnamed_addr constant [3 x i8] c"buf", align 1 +@3 = private unnamed_addr constant [2 x i8] c"Go", align 1 +@4 = private unnamed_addr constant [2 x i8] c"go", align 1 + +define void @main.init() { +_llgo_0: + %0 = load i1, ptr @"main.init$guard", align 1 + br i1 %0, label %_llgo_2, label %_llgo_1 + +_llgo_1: ; preds = %_llgo_0 + store i1 true, ptr @"main.init$guard", align 1 + call void @bytes.init() + br label %_llgo_2 + +_llgo_2: ; preds = %_llgo_1, %_llgo_0 + ret void +} + +define i32 @main(i32 %0, ptr %1) { +_llgo_0: + store i32 %0, ptr @__llgo_argc, align 4 + store ptr %1, ptr @__llgo_argv, align 8 + call void @"github.com/goplus/llgo/internal/runtime.init"() + call void @main.init() + %2 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 40) + %3 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %3, i32 0, i32 0 + store ptr @0, ptr %4, align 8 + %5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %3, i32 0, i32 1 + store i64 6, ptr %5, align 4 + %6 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %3, align 8 + %7 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.StringToBytes"(%"github.com/goplus/llgo/internal/runtime.String" %6) + %8 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @"bytes.(*Buffer).Write"(ptr %2, %"github.com/goplus/llgo/internal/runtime.Slice" %7) + %9 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %10 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %9, i32 0, i32 0 + store ptr @1, ptr %10, align 8 + %11 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %9, i32 0, i32 1 + store i64 5, ptr %11, align 4 + %12 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %9, align 8 + %13 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @"bytes.(*Buffer).WriteString"(ptr %2, %"github.com/goplus/llgo/internal/runtime.String" %12) + %14 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"bytes.(*Buffer).Bytes"(ptr %2) + %15 = call %"github.com/goplus/llgo/internal/runtime.String" @"bytes.(*Buffer).String"(ptr %2) + %16 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %17 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %16, i32 0, i32 0 + store ptr @2, ptr %17, align 8 + %18 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %16, i32 0, i32 1 + store i64 3, ptr %18, align 4 + %19 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %16, align 8 + call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %19) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/internal/runtime.PrintSlice"(%"github.com/goplus/llgo/internal/runtime.Slice" %14) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %15) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10) + %20 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %21 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %20, i32 0, i32 0 + store ptr @3, ptr %21, align 8 + %22 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %20, i32 0, i32 1 + store i64 2, ptr %22, align 4 + %23 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %20, align 8 + %24 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.StringToBytes"(%"github.com/goplus/llgo/internal/runtime.String" %23) + %25 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %26 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %25, i32 0, i32 0 + store ptr @4, ptr %26, align 8 + %27 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %25, i32 0, i32 1 + store i64 2, ptr %27, align 4 + %28 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %25, align 8 + %29 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.StringToBytes"(%"github.com/goplus/llgo/internal/runtime.String" %28) + %30 = call i1 @bytes.EqualFold(%"github.com/goplus/llgo/internal/runtime.Slice" %24, %"github.com/goplus/llgo/internal/runtime.Slice" %29) + call void @"github.com/goplus/llgo/internal/runtime.PrintBool"(i1 %30) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10) + ret i32 0 +} + +declare void @bytes.init() + +declare void @"github.com/goplus/llgo/internal/runtime.init"() + +declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64) + +declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.StringToBytes"(%"github.com/goplus/llgo/internal/runtime.String") + +declare { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @"bytes.(*Buffer).Write"(ptr, %"github.com/goplus/llgo/internal/runtime.Slice") + +declare { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @"bytes.(*Buffer).WriteString"(ptr, %"github.com/goplus/llgo/internal/runtime.String") + +declare %"github.com/goplus/llgo/internal/runtime.Slice" @"bytes.(*Buffer).Bytes"(ptr) + +declare %"github.com/goplus/llgo/internal/runtime.String" @"bytes.(*Buffer).String"(ptr) + +declare void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String") + +declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8) + +declare void @"github.com/goplus/llgo/internal/runtime.PrintSlice"(%"github.com/goplus/llgo/internal/runtime.Slice") + +declare i1 @bytes.EqualFold(%"github.com/goplus/llgo/internal/runtime.Slice", %"github.com/goplus/llgo/internal/runtime.Slice") + +declare void @"github.com/goplus/llgo/internal/runtime.PrintBool"(i1) diff --git a/cl/_testlibgo/strings/in.go b/cl/_testlibgo/strings/in.go new file mode 100644 index 00000000..4ce09a4a --- /dev/null +++ b/cl/_testlibgo/strings/in.go @@ -0,0 +1,20 @@ +package main + +import ( + "strings" + "unicode" +) + +func main() { + var b strings.Builder + b.Write([]byte("Hello ")) + b.WriteString("World") + + println("len:", b.Len(), "cap:", b.Cap(), "string:", b.String()) + + f := func(c rune) bool { + return unicode.Is(unicode.Han, c) + } + println(strings.IndexFunc("Hello, 世界", f)) + println(strings.IndexFunc("Hello, world", f)) +} diff --git a/cl/_testlibgo/strings/out.ll b/cl/_testlibgo/strings/out.ll new file mode 100644 index 00000000..ef8e08fb --- /dev/null +++ b/cl/_testlibgo/strings/out.ll @@ -0,0 +1,164 @@ +; ModuleID = 'main' +source_filename = "main" + +%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 } +%"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 } +%"github.com/goplus/llgo/internal/runtime.iface" = type { ptr, ptr } + +@"main.init$guard" = global i1 false, align 1 +@__llgo_argc = global i32 0, align 4 +@__llgo_argv = global ptr null, align 8 +@0 = private unnamed_addr constant [6 x i8] c"Hello ", align 1 +@1 = private unnamed_addr constant [5 x i8] c"World", align 1 +@2 = private unnamed_addr constant [4 x i8] c"len:", align 1 +@3 = private unnamed_addr constant [4 x i8] c"cap:", align 1 +@4 = private unnamed_addr constant [7 x i8] c"string:", align 1 +@5 = private unnamed_addr constant [13 x i8] c"Hello, \E4\B8\96\E7\95\8C", align 1 +@6 = private unnamed_addr constant [12 x i8] c"Hello, world", align 1 +@unicode.Han = external global ptr, align 8 + +define void @main.init() { +_llgo_0: + %0 = load i1, ptr @"main.init$guard", align 1 + br i1 %0, label %_llgo_2, label %_llgo_1 + +_llgo_1: ; preds = %_llgo_0 + store i1 true, ptr @"main.init$guard", align 1 + call void @strings.init() + call void @unicode.init() + br label %_llgo_2 + +_llgo_2: ; preds = %_llgo_1, %_llgo_0 + ret void +} + +define i32 @main(i32 %0, ptr %1) { +_llgo_0: + store i32 %0, ptr @__llgo_argc, align 4 + store ptr %1, ptr @__llgo_argv, align 8 + call void @"github.com/goplus/llgo/internal/runtime.init"() + call void @main.init() + %2 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 32) + %3 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %3, i32 0, i32 0 + store ptr @0, ptr %4, align 8 + %5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %3, i32 0, i32 1 + store i64 6, ptr %5, align 4 + %6 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %3, align 8 + %7 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.StringToBytes"(%"github.com/goplus/llgo/internal/runtime.String" %6) + %8 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @"strings.(*Builder).Write"(ptr %2, %"github.com/goplus/llgo/internal/runtime.Slice" %7) + %9 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %10 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %9, i32 0, i32 0 + store ptr @1, ptr %10, align 8 + %11 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %9, i32 0, i32 1 + store i64 5, ptr %11, align 4 + %12 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %9, align 8 + %13 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @"strings.(*Builder).WriteString"(ptr %2, %"github.com/goplus/llgo/internal/runtime.String" %12) + %14 = call i64 @"strings.(*Builder).Len"(ptr %2) + %15 = call i64 @"strings.(*Builder).Cap"(ptr %2) + %16 = call %"github.com/goplus/llgo/internal/runtime.String" @"strings.(*Builder).String"(ptr %2) + %17 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %18 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %17, i32 0, i32 0 + store ptr @2, ptr %18, align 8 + %19 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %17, i32 0, i32 1 + store i64 4, ptr %19, align 4 + %20 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %17, align 8 + %21 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %22 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %21, i32 0, i32 0 + store ptr @3, ptr %22, align 8 + %23 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %21, i32 0, i32 1 + store i64 4, ptr %23, align 4 + %24 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %21, align 8 + %25 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %26 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %25, i32 0, i32 0 + store ptr @4, ptr %26, align 8 + %27 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %25, i32 0, i32 1 + store i64 7, ptr %27, align 4 + %28 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %25, align 8 + call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %20) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %14) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %24) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %15) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %28) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %16) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10) + %29 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %30 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %29, i32 0, i32 0 + store ptr @5, ptr %30, align 8 + %31 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %29, i32 0, i32 1 + store i64 13, ptr %31, align 4 + %32 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %29, align 8 + %33 = alloca { ptr, ptr }, align 8 + %34 = getelementptr inbounds { ptr, ptr }, ptr %33, i32 0, i32 0 + store ptr @"__llgo_stub.main.main$1", ptr %34, align 8 + %35 = getelementptr inbounds { ptr, ptr }, ptr %33, i32 0, i32 1 + store ptr null, ptr %35, align 8 + %36 = load { ptr, ptr }, ptr %33, align 8 + %37 = call i64 @strings.IndexFunc(%"github.com/goplus/llgo/internal/runtime.String" %32, { ptr, ptr } %36) + call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %37) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10) + %38 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %39 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %38, i32 0, i32 0 + store ptr @6, ptr %39, align 8 + %40 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %38, i32 0, i32 1 + store i64 12, ptr %40, align 4 + %41 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %38, align 8 + %42 = alloca { ptr, ptr }, align 8 + %43 = getelementptr inbounds { ptr, ptr }, ptr %42, i32 0, i32 0 + store ptr @"__llgo_stub.main.main$1", ptr %43, align 8 + %44 = getelementptr inbounds { ptr, ptr }, ptr %42, i32 0, i32 1 + store ptr null, ptr %44, align 8 + %45 = load { ptr, ptr }, ptr %42, align 8 + %46 = call i64 @strings.IndexFunc(%"github.com/goplus/llgo/internal/runtime.String" %41, { ptr, ptr } %45) + call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %46) + call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10) + ret i32 0 +} + +define i1 @"main.main$1"(i32 %0) { +_llgo_0: + %1 = load ptr, ptr @unicode.Han, align 8 + %2 = call i1 @unicode.Is(ptr %1, i32 %0) + ret i1 %2 +} + +declare void @strings.init() + +declare void @unicode.init() + +declare void @"github.com/goplus/llgo/internal/runtime.init"() + +declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64) + +declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.StringToBytes"(%"github.com/goplus/llgo/internal/runtime.String") + +declare { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @"strings.(*Builder).Write"(ptr, %"github.com/goplus/llgo/internal/runtime.Slice") + +declare { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @"strings.(*Builder).WriteString"(ptr, %"github.com/goplus/llgo/internal/runtime.String") + +declare i64 @"strings.(*Builder).Len"(ptr) + +declare i64 @"strings.(*Builder).Cap"(ptr) + +declare %"github.com/goplus/llgo/internal/runtime.String" @"strings.(*Builder).String"(ptr) + +declare void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String") + +declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8) + +declare void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64) + +declare i64 @strings.IndexFunc(%"github.com/goplus/llgo/internal/runtime.String", { ptr, ptr }) + +define linkonce i1 @"__llgo_stub.main.main$1"(ptr %0, i32 %1) { +_llgo_0: + %2 = tail call i1 @"main.main$1"(i32 %1) + ret i1 %2 +} + +declare i1 @unicode.Is(ptr, i32) diff --git a/cl/_testrt/builtin/out.ll b/cl/_testrt/builtin/out.ll index 824c5cd1..e78ddf0d 100644 --- a/cl/_testrt/builtin/out.ll +++ b/cl/_testrt/builtin/out.ll @@ -161,12 +161,12 @@ _llgo_0: call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 4) call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10) %34 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %11, 2 - %35 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %11, 2 + %35 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %11, 1 %36 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %11, 0 %37 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice3"(ptr %36, i64 8, i64 %34, i64 1, i64 %35, i64 %34) %38 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %37, 1 %39 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %11, 2 - %40 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %11, 2 + %40 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %11, 1 %41 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %11, 0 %42 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice3"(ptr %41, i64 8, i64 %39, i64 1, i64 %40, i64 %39) %43 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %42, 2 diff --git a/ssa/datastruct.go b/ssa/datastruct.go index 7ec0e1ec..2f45c7b6 100644 --- a/ssa/datastruct.go +++ b/ssa/datastruct.go @@ -333,7 +333,6 @@ func (b Builder) Slice(x, low, high, max Expr) (ret Expr) { if debugInstr { log.Printf("Slice %v, %v, %v\n", x.impl, low.impl, high.impl) } - prog := b.Prog var nCap Expr var nEltSize Expr From 43a6837e81514adb6d2cd26e58a8169280d3dcf6 Mon Sep 17 00:00:00 2001 From: Aofei Sheng Date: Tue, 25 Jun 2024 18:16:46 +0800 Subject: [PATCH 4/4] xtool: utilize llvm-config to find LLVM executables --- chore/nmdump/nmdump.go | 2 +- chore/nmindex/nmindex.go | 2 +- internal/build/build.go | 42 ++++++----- xtool/env/env.go | 16 ++++ xtool/env/llvm/llvm.go | 73 +++++++++++++++---- xtool/env/llvm/llvm_config_byollvm.go | 26 +++++++ .../llvm/llvm_config_darwin_amd64_llvm14.go | 21 ++++++ .../llvm/llvm_config_darwin_amd64_llvm15.go | 21 ++++++ .../llvm/llvm_config_darwin_amd64_llvm16.go | 21 ++++++ .../llvm/llvm_config_darwin_amd64_llvm17.go | 21 ++++++ xtool/env/llvm/llvm_config_darwin_llvm14.go | 21 ++++++ xtool/env/llvm/llvm_config_darwin_llvm15.go | 21 ++++++ xtool/env/llvm/llvm_config_darwin_llvm16.go | 21 ++++++ xtool/env/llvm/llvm_config_darwin_llvm17.go | 21 ++++++ xtool/env/llvm/llvm_config_linux_llvm14.go | 21 ++++++ xtool/env/llvm/llvm_config_linux_llvm15.go | 21 ++++++ xtool/env/llvm/llvm_config_linux_llvm16.go | 21 ++++++ xtool/env/llvm/llvm_config_linux_llvm17.go | 21 ++++++ xtool/llvm/llvmlink/link.go | 2 +- 19 files changed, 381 insertions(+), 34 deletions(-) create mode 100644 xtool/env/llvm/llvm_config_byollvm.go create mode 100644 xtool/env/llvm/llvm_config_darwin_amd64_llvm14.go create mode 100644 xtool/env/llvm/llvm_config_darwin_amd64_llvm15.go create mode 100644 xtool/env/llvm/llvm_config_darwin_amd64_llvm16.go create mode 100644 xtool/env/llvm/llvm_config_darwin_amd64_llvm17.go create mode 100644 xtool/env/llvm/llvm_config_darwin_llvm14.go create mode 100644 xtool/env/llvm/llvm_config_darwin_llvm15.go create mode 100644 xtool/env/llvm/llvm_config_darwin_llvm16.go create mode 100644 xtool/env/llvm/llvm_config_darwin_llvm17.go create mode 100644 xtool/env/llvm/llvm_config_linux_llvm14.go create mode 100644 xtool/env/llvm/llvm_config_linux_llvm15.go create mode 100644 xtool/env/llvm/llvm_config_linux_llvm16.go create mode 100644 xtool/env/llvm/llvm_config_linux_llvm17.go diff --git a/chore/nmdump/nmdump.go b/chore/nmdump/nmdump.go index f9b46d58..cdb36554 100644 --- a/chore/nmdump/nmdump.go +++ b/chore/nmdump/nmdump.go @@ -30,7 +30,7 @@ func main() { return } - nm := llvm.New().Nm() + nm := llvm.New("").Nm() items, err := nm.List(os.Args[1]) for _, item := range items { if item.File != "" { diff --git a/chore/nmindex/nmindex.go b/chore/nmindex/nmindex.go index e8f9f254..43d34118 100644 --- a/chore/nmindex/nmindex.go +++ b/chore/nmindex/nmindex.go @@ -54,7 +54,7 @@ The commands are: } func makeIndex() { - env := llvm.New() + env := llvm.New("") idxDir := indexDir() os.MkdirAll(idxDir, 0755) diff --git a/internal/build/build.go b/internal/build/build.go index de0b52c0..050a2cd3 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -37,8 +37,8 @@ import ( "github.com/goplus/llgo/cl" "github.com/goplus/llgo/internal/packages" "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" @@ -308,21 +308,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) { @@ -378,7 +386,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 { @@ -646,7 +654,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) } diff --git a/xtool/env/env.go b/xtool/env/env.go index 803bdd3e..87beba9d 100644 --- a/xtool/env/env.go +++ b/xtool/env/env.go @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package env import ( diff --git a/xtool/env/llvm/llvm.go b/xtool/env/llvm/llvm.go index cb5343ca..06fc35c1 100644 --- a/xtool/env/llvm/llvm.go +++ b/xtool/env/llvm/llvm.go @@ -18,28 +18,73 @@ package llvm import ( "os" + "os/exec" + "path/filepath" + "strings" + "github.com/goplus/llgo/xtool/clang" + "github.com/goplus/llgo/xtool/llvm/llvmlink" "github.com/goplus/llgo/xtool/nm" ) -type Env struct { - root string - nmprefix string -} +// ----------------------------------------------------------------------------- -func New() *Env { - var nmprefix string - var root = os.Getenv("LLGO_LLVM_ROOT") - if root != "" { - nmprefix = root + "/bin/llvm-" +// defaultLLVMConfigBin returns the default path to the llvm-config binary. It +// checks the LLVM_CONFIG environment variable first, then searches in PATH. If +// not found, it returns [ldLLVMConfigBin] as a last resort. +func defaultLLVMConfigBin() string { + bin := os.Getenv("LLVM_CONFIG") + if bin != "" { + return bin } - return &Env{root, nmprefix} + bin, _ = exec.LookPath("llvm-config") + if bin != "" { + return bin + } + return ldLLVMConfigBin } -func (p *Env) Root() string { - return p.root +// ----------------------------------------------------------------------------- + +// Env represents an LLVM installation. +type Env struct { + binDir string } -func (p *Env) Nm() *nm.Cmd { - return nm.New(p.nmprefix + "nm") +// New creates a new [Env] instance. +func New(llvmConfigBin string) *Env { + if llvmConfigBin == "" { + llvmConfigBin = defaultLLVMConfigBin() + } + + // Note that an empty binDir is acceptable. In this case, LLVM + // executables are assumed to be in PATH. + binDir, _ := exec.Command(llvmConfigBin, "--bindir").Output() + + e := &Env{binDir: strings.TrimSpace(string(binDir))} + return e } + +// BinDir returns the directory containing LLVM executables. An empty string +// means LLVM executables are assumed to be in PATH. +func (e *Env) BinDir() string { return e.binDir } + +// Clang returns a new [clang.Cmd] instance. +func (e *Env) Clang() *clang.Cmd { + bin := filepath.Join(e.BinDir(), "clang") + return clang.New(bin) +} + +// Link returns a new [llvmlink.Cmd] instance. +func (e *Env) Link() *llvmlink.Cmd { + bin := filepath.Join(e.BinDir(), "llvm-link") + return llvmlink.New(bin) +} + +// Nm returns a new [nm.Cmd] instance. +func (e *Env) Nm() *nm.Cmd { + bin := filepath.Join(e.BinDir(), "llvm-nm") + return nm.New(bin) +} + +// ----------------------------------------------------------------------------- diff --git a/xtool/env/llvm/llvm_config_byollvm.go b/xtool/env/llvm/llvm_config_byollvm.go new file mode 100644 index 00000000..54aeedd3 --- /dev/null +++ b/xtool/env/llvm/llvm_config_byollvm.go @@ -0,0 +1,26 @@ +//go:build byollvm + +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// The word "byollvm" stands for "Bring Your Own LLVM" and is a build tag used +// to indicate that the package is being built with a custom LLVM installation. + +package llvm + +// ldLLVMConfigBin is the path to the llvm-config binary. It shoud be set via +// -ldflags when building the package. +var ldLLVMConfigBin string diff --git a/xtool/env/llvm/llvm_config_darwin_amd64_llvm14.go b/xtool/env/llvm/llvm_config_darwin_amd64_llvm14.go new file mode 100644 index 00000000..0fd03691 --- /dev/null +++ b/xtool/env/llvm/llvm_config_darwin_amd64_llvm14.go @@ -0,0 +1,21 @@ +//go:build !byollvm && darwin && amd64 && llvm14 + +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package llvm + +const ldLLVMConfigBin = "/usr/local/opt/llvm@14/bin/llvm-config" diff --git a/xtool/env/llvm/llvm_config_darwin_amd64_llvm15.go b/xtool/env/llvm/llvm_config_darwin_amd64_llvm15.go new file mode 100644 index 00000000..1d4d04a1 --- /dev/null +++ b/xtool/env/llvm/llvm_config_darwin_amd64_llvm15.go @@ -0,0 +1,21 @@ +//go:build !byollvm && darwin && amd64 && llvm15 + +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package llvm + +const ldLLVMConfigBin = "/usr/local/opt/llvm@15/bin/llvm-config" diff --git a/xtool/env/llvm/llvm_config_darwin_amd64_llvm16.go b/xtool/env/llvm/llvm_config_darwin_amd64_llvm16.go new file mode 100644 index 00000000..7dc84ec0 --- /dev/null +++ b/xtool/env/llvm/llvm_config_darwin_amd64_llvm16.go @@ -0,0 +1,21 @@ +//go:build !byollvm && darwin && amd64 && llvm16 + +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package llvm + +const ldLLVMConfigBin = "/usr/local/opt/llvm@16/bin/llvm-config" diff --git a/xtool/env/llvm/llvm_config_darwin_amd64_llvm17.go b/xtool/env/llvm/llvm_config_darwin_amd64_llvm17.go new file mode 100644 index 00000000..84096511 --- /dev/null +++ b/xtool/env/llvm/llvm_config_darwin_amd64_llvm17.go @@ -0,0 +1,21 @@ +//go:build !byollvm && darwin && amd64 && !llvm14 && !llvm15 && !llvm16 + +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package llvm + +const ldLLVMConfigBin = "/usr/local/opt/llvm@17/bin/llvm-config" diff --git a/xtool/env/llvm/llvm_config_darwin_llvm14.go b/xtool/env/llvm/llvm_config_darwin_llvm14.go new file mode 100644 index 00000000..ac271e53 --- /dev/null +++ b/xtool/env/llvm/llvm_config_darwin_llvm14.go @@ -0,0 +1,21 @@ +//go:build !byollvm && darwin && !amd64 && llvm14 + +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package llvm + +const ldLLVMConfigBin = "/opt/homebrew/opt/llvm@14/bin/llvm-config" diff --git a/xtool/env/llvm/llvm_config_darwin_llvm15.go b/xtool/env/llvm/llvm_config_darwin_llvm15.go new file mode 100644 index 00000000..a0df2cad --- /dev/null +++ b/xtool/env/llvm/llvm_config_darwin_llvm15.go @@ -0,0 +1,21 @@ +//go:build !byollvm && darwin && !amd64 && llvm15 + +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package llvm + +const ldLLVMConfigBin = "/opt/homebrew/opt/llvm@15/bin/llvm-config" diff --git a/xtool/env/llvm/llvm_config_darwin_llvm16.go b/xtool/env/llvm/llvm_config_darwin_llvm16.go new file mode 100644 index 00000000..bdb91b5f --- /dev/null +++ b/xtool/env/llvm/llvm_config_darwin_llvm16.go @@ -0,0 +1,21 @@ +//go:build !byollvm && darwin && !amd64 && llvm16 + +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package llvm + +const ldLLVMConfigBin = "/opt/homebrew/opt/llvm@16/bin/llvm-config" diff --git a/xtool/env/llvm/llvm_config_darwin_llvm17.go b/xtool/env/llvm/llvm_config_darwin_llvm17.go new file mode 100644 index 00000000..b9597045 --- /dev/null +++ b/xtool/env/llvm/llvm_config_darwin_llvm17.go @@ -0,0 +1,21 @@ +//go:build !byollvm && darwin && !amd64 && !llvm14 && !llvm15 && !llvm16 + +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package llvm + +const ldLLVMConfigBin = "/opt/homebrew/opt/llvm@17/bin/llvm-config" diff --git a/xtool/env/llvm/llvm_config_linux_llvm14.go b/xtool/env/llvm/llvm_config_linux_llvm14.go new file mode 100644 index 00000000..8c0330f2 --- /dev/null +++ b/xtool/env/llvm/llvm_config_linux_llvm14.go @@ -0,0 +1,21 @@ +//go:build !byollvm && linux && llvm14 + +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package llvm + +const ldLLVMConfigBin = "/usr/lib/llvm-14/bin/llvm-config" diff --git a/xtool/env/llvm/llvm_config_linux_llvm15.go b/xtool/env/llvm/llvm_config_linux_llvm15.go new file mode 100644 index 00000000..9e29d745 --- /dev/null +++ b/xtool/env/llvm/llvm_config_linux_llvm15.go @@ -0,0 +1,21 @@ +//go:build !byollvm && linux && llvm15 + +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package llvm + +const ldLLVMConfigBin = "/usr/lib/llvm-15/bin/llvm-config" diff --git a/xtool/env/llvm/llvm_config_linux_llvm16.go b/xtool/env/llvm/llvm_config_linux_llvm16.go new file mode 100644 index 00000000..71abb184 --- /dev/null +++ b/xtool/env/llvm/llvm_config_linux_llvm16.go @@ -0,0 +1,21 @@ +//go:build !byollvm && linux && llvm16 + +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package llvm + +const ldLLVMConfigBin = "/usr/lib/llvm-16/bin/llvm-config" diff --git a/xtool/env/llvm/llvm_config_linux_llvm17.go b/xtool/env/llvm/llvm_config_linux_llvm17.go new file mode 100644 index 00000000..0982e764 --- /dev/null +++ b/xtool/env/llvm/llvm_config_linux_llvm17.go @@ -0,0 +1,21 @@ +//go:build !byollvm && linux && !llvm14 && !llvm15 && !llvm16 + +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package llvm + +const ldLLVMConfigBin = "/usr/lib/llvm-17/bin/llvm-config" diff --git a/xtool/llvm/llvmlink/link.go b/xtool/llvm/llvmlink/link.go index 3079affa..b52f3c4c 100644 --- a/xtool/llvm/llvmlink/link.go +++ b/xtool/llvm/llvmlink/link.go @@ -35,7 +35,7 @@ type Cmd struct { // New creates a new llvm-link command. func New(app string) *Cmd { if app == "" { - app = os.Getenv("LLGO_LLVM_ROOT") + "/bin/llvm-link" + app = "llvm-link" } return &Cmd{app, os.Stdout, os.Stderr} }