From 607deaa3c47dc7504e1cc76fa3de82b2e3db2078 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Thu, 20 Jun 2024 02:55:26 +0800 Subject: [PATCH 1/2] patch: syscall --- README.md | 1 + _demo/syscall/syscall.go | 11 ++++++++ cl/import.go | 7 +++-- internal/build/build.go | 1 + internal/lib/syscall/syscall.go | 46 +++++++++++++++++++++++++++++++++ ssa/expr.go | 12 +++++---- 6 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 _demo/syscall/syscall.go create mode 100644 internal/lib/syscall/syscall.go diff --git a/README.md b/README.md index 4c35c98b2..b2ee0dab8 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,7 @@ Here are the Go packages that can be imported correctly: * [unicode/utf16](https://pkg.go.dev/unicode/utf16) * [math/bits](https://pkg.go.dev/math/bits) * [math](https://pkg.go.dev/math) +* [syscall](https://pkg.go.dev/syscall) (partially) * [sync](https://pkg.go.dev/sync) (partially) * [sync/atomic](https://pkg.go.dev/sync/atomic) (partially) diff --git a/_demo/syscall/syscall.go b/_demo/syscall/syscall.go new file mode 100644 index 000000000..889e59f02 --- /dev/null +++ b/_demo/syscall/syscall.go @@ -0,0 +1,11 @@ +package main + +import "syscall" + +func main() { + wd, err := syscall.Getwd() + if err != nil { + panic(err) + } + println("cwd:", wd) +} diff --git a/cl/import.go b/cl/import.go index 1d28858ef..ae85ed511 100644 --- a/cl/import.go +++ b/cl/import.go @@ -498,7 +498,7 @@ func (p *context) ensureLoaded(pkgTypes *types.Package) *types.Package { func pkgKindByPath(pkgPath string) int { switch pkgPath { - case "syscall", "runtime/cgo", "unsafe": + case "runtime/cgo", "unsafe": return PkgDeclOnly } return PkgNormal @@ -520,9 +520,8 @@ func ignoreName(name string) bool { */ return strings.HasPrefix(name, "internal/") || strings.HasPrefix(name, "crypto/") || strings.HasPrefix(name, "arena.") || strings.HasPrefix(name, "maps.") || - strings.HasPrefix(name, "time.") || strings.HasPrefix(name, "syscall.") || - strings.HasPrefix(name, "plugin.") || strings.HasPrefix(name, "reflect.") || - strings.HasPrefix(name, "runtime/") + strings.HasPrefix(name, "time.") || strings.HasPrefix(name, "runtime/") || + strings.HasPrefix(name, "plugin.") || strings.HasPrefix(name, "reflect.") } // ----------------------------------------------------------------------------- diff --git a/internal/build/build.go b/internal/build/build.go index cfcd715fb..eec95fe1a 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -724,6 +724,7 @@ var hasAltPkg = map[string]none{ "math": {}, "sync": {}, "sync/atomic": {}, + "syscall": {}, "os": {}, "runtime": {}, } diff --git a/internal/lib/syscall/syscall.go b/internal/lib/syscall/syscall.go new file mode 100644 index 000000000..d4e3fedfc --- /dev/null +++ b/internal/lib/syscall/syscall.go @@ -0,0 +1,46 @@ +/* + * 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 syscall + +// llgo:skipall +import ( + "unsafe" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/os" +) + +func errnoErr(errno c.Int) error { + panic("todo") +} + +func Getcwd(buf []byte) (n int, err error) { + ptr := unsafe.Pointer(unsafe.SliceData(buf)) + ret := os.Getcwd(ptr, uintptr(len(buf))) + if ret != nil { + return int(c.Strlen(ret)), nil + } + return 0, errnoErr(os.Errno) +} + +func Getwd() (string, error) { + wd := os.Getcwd(c.Alloca(os.PATH_MAX), os.PATH_MAX) + if wd != nil { + return c.GoString(wd), nil + } + return "", errnoErr(os.Errno) +} diff --git a/ssa/expr.go b/ssa/expr.go index b6912a34a..7dbeb4fab 100644 --- a/ssa/expr.go +++ b/ssa/expr.go @@ -883,11 +883,6 @@ func (b Builder) Do(da DoAction, fn Expr, args ...Expr) (ret Expr) { // Go spec (excluding "make" and "new"). func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) { switch fn { - case "String": // unsafe.String - return b.unsafeString(args[0].impl, args[1].impl) - case "Slice": // unsafe.Slice - size := args[1].impl - return b.unsafeSlice(args[0], size, size) case "len": if len(args) == 1 { arg := args[0] @@ -946,6 +941,13 @@ func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) { return b.Recover() case "print", "println": return b.PrintEx(fn == "println", args...) + case "String": // unsafe.String + return b.unsafeString(args[0].impl, args[1].impl) + case "Slice": // unsafe.Slice + size := args[1].impl + return b.unsafeSlice(args[0], size, size) + case "SliceData": + return b.SliceData(args[0]) // TODO(xsw): check return type } panic("todo: " + fn) } From 1566a834e1f2273d183bc12f9edfb9534d893a0e Mon Sep 17 00:00:00 2001 From: xushiwei Date: Thu, 20 Jun 2024 03:00:12 +0800 Subject: [PATCH 2/2] x --- cl/_testrt/strlen/out.ll | 3 +++ cl/_testrt/struct/out.ll | 3 +++ cl/_testrt/typalias/out.ll | 3 +++ 3 files changed, 9 insertions(+) diff --git a/cl/_testrt/strlen/out.ll b/cl/_testrt/strlen/out.ll index b4bf2fe15..917e6bd9a 100644 --- a/cl/_testrt/strlen/out.ll +++ b/cl/_testrt/strlen/out.ll @@ -13,6 +13,7 @@ _llgo_0: _llgo_1: ; preds = %_llgo_0 store i1 true, ptr @"main.init$guard", align 1 + call void @syscall.init() store i8 72, ptr @main.format, align 1 store i8 101, ptr getelementptr inbounds (i8, ptr @main.format, i64 1), align 1 store i8 108, ptr getelementptr inbounds (i8, ptr @main.format, i64 2), align 1 @@ -40,6 +41,8 @@ _llgo_0: ret i32 0 } +declare void @syscall.init() + declare void @"github.com/goplus/llgo/internal/runtime.init"() declare i32 @strlen(ptr) diff --git a/cl/_testrt/struct/out.ll b/cl/_testrt/struct/out.ll index 8388292ed..6c060f966 100644 --- a/cl/_testrt/struct/out.ll +++ b/cl/_testrt/struct/out.ll @@ -41,6 +41,7 @@ _llgo_0: _llgo_1: ; preds = %_llgo_0 store i1 true, ptr @"main.init$guard", align 1 + call void @syscall.init() store i8 72, ptr @main.format, align 1 store i8 101, ptr getelementptr inbounds (i8, ptr @main.format, i64 1), align 1 store i8 108, ptr getelementptr inbounds (i8, ptr @main.format, i64 2), align 1 @@ -78,4 +79,6 @@ declare ptr @"github.com/goplus/llgo/internal/runtime.Zeroinit"(ptr, i64) declare void @printf(ptr, ...) +declare void @syscall.init() + declare void @"github.com/goplus/llgo/internal/runtime.init"() diff --git a/cl/_testrt/typalias/out.ll b/cl/_testrt/typalias/out.ll index e77b0d59a..778f876d7 100644 --- a/cl/_testrt/typalias/out.ll +++ b/cl/_testrt/typalias/out.ll @@ -29,6 +29,7 @@ _llgo_0: _llgo_1: ; preds = %_llgo_0 store i1 true, ptr @"main.init$guard", align 1 + call void @syscall.init() store i8 72, ptr @main.format, align 1 store i8 101, ptr getelementptr inbounds (i8, ptr @main.format, i64 1), align 1 store i8 108, ptr getelementptr inbounds (i8, ptr @main.format, i64 2), align 1 @@ -62,6 +63,8 @@ _llgo_0: declare void @printf(ptr, ...) +declare void @syscall.init() + declare void @"github.com/goplus/llgo/internal/runtime.init"() declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)