Skip to content

Commit 2e6312e

Browse files
authored
Merge pull request #366 from xushiwei/q
patch: syscall
2 parents 686186d + 1566a83 commit 2e6312e

File tree

9 files changed

+78
-9
lines changed

9 files changed

+78
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ Here are the Go packages that can be imported correctly:
208208
* [unicode/utf16](https://pkg.go.dev/unicode/utf16)
209209
* [math/bits](https://pkg.go.dev/math/bits)
210210
* [math](https://pkg.go.dev/math)
211+
* [syscall](https://pkg.go.dev/syscall) (partially)
211212
* [sync](https://pkg.go.dev/sync) (partially)
212213
* [sync/atomic](https://pkg.go.dev/sync/atomic) (partially)
213214

_demo/syscall/syscall.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import "syscall"
4+
5+
func main() {
6+
wd, err := syscall.Getwd()
7+
if err != nil {
8+
panic(err)
9+
}
10+
println("cwd:", wd)
11+
}

cl/_testrt/strlen/out.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ _llgo_0:
1313

1414
_llgo_1: ; preds = %_llgo_0
1515
store i1 true, ptr @"main.init$guard", align 1
16+
call void @syscall.init()
1617
store i8 72, ptr @main.format, align 1
1718
store i8 101, ptr getelementptr inbounds (i8, ptr @main.format, i64 1), align 1
1819
store i8 108, ptr getelementptr inbounds (i8, ptr @main.format, i64 2), align 1
@@ -40,6 +41,8 @@ _llgo_0:
4041
ret i32 0
4142
}
4243

44+
declare void @syscall.init()
45+
4346
declare void @"github.com/goplus/llgo/internal/runtime.init"()
4447

4548
declare i32 @strlen(ptr)

cl/_testrt/struct/out.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ _llgo_0:
4141

4242
_llgo_1: ; preds = %_llgo_0
4343
store i1 true, ptr @"main.init$guard", align 1
44+
call void @syscall.init()
4445
store i8 72, ptr @main.format, align 1
4546
store i8 101, ptr getelementptr inbounds (i8, ptr @main.format, i64 1), align 1
4647
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)
7879

7980
declare void @printf(ptr, ...)
8081

82+
declare void @syscall.init()
83+
8184
declare void @"github.com/goplus/llgo/internal/runtime.init"()

cl/_testrt/typalias/out.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ _llgo_0:
2929

3030
_llgo_1: ; preds = %_llgo_0
3131
store i1 true, ptr @"main.init$guard", align 1
32+
call void @syscall.init()
3233
store i8 72, ptr @main.format, align 1
3334
store i8 101, ptr getelementptr inbounds (i8, ptr @main.format, i64 1), align 1
3435
store i8 108, ptr getelementptr inbounds (i8, ptr @main.format, i64 2), align 1
@@ -62,6 +63,8 @@ _llgo_0:
6263

6364
declare void @printf(ptr, ...)
6465

66+
declare void @syscall.init()
67+
6568
declare void @"github.com/goplus/llgo/internal/runtime.init"()
6669

6770
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)

cl/import.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ func (p *context) ensureLoaded(pkgTypes *types.Package) *types.Package {
498498

499499
func pkgKindByPath(pkgPath string) int {
500500
switch pkgPath {
501-
case "syscall", "runtime/cgo", "unsafe":
501+
case "runtime/cgo", "unsafe":
502502
return PkgDeclOnly
503503
}
504504
return PkgNormal
@@ -520,9 +520,8 @@ func ignoreName(name string) bool {
520520
*/
521521
return strings.HasPrefix(name, "internal/") || strings.HasPrefix(name, "crypto/") ||
522522
strings.HasPrefix(name, "arena.") || strings.HasPrefix(name, "maps.") ||
523-
strings.HasPrefix(name, "time.") || strings.HasPrefix(name, "syscall.") ||
524-
strings.HasPrefix(name, "plugin.") || strings.HasPrefix(name, "reflect.") ||
525-
strings.HasPrefix(name, "runtime/")
523+
strings.HasPrefix(name, "time.") || strings.HasPrefix(name, "runtime/") ||
524+
strings.HasPrefix(name, "plugin.") || strings.HasPrefix(name, "reflect.")
526525
}
527526

528527
// -----------------------------------------------------------------------------

internal/build/build.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ var hasAltPkg = map[string]none{
724724
"math": {},
725725
"sync": {},
726726
"sync/atomic": {},
727+
"syscall": {},
727728
"os": {},
728729
"runtime": {},
729730
}

internal/lib/syscall/syscall.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package syscall
18+
19+
// llgo:skipall
20+
import (
21+
"unsafe"
22+
23+
"github.com/goplus/llgo/c"
24+
"github.com/goplus/llgo/c/os"
25+
)
26+
27+
func errnoErr(errno c.Int) error {
28+
panic("todo")
29+
}
30+
31+
func Getcwd(buf []byte) (n int, err error) {
32+
ptr := unsafe.Pointer(unsafe.SliceData(buf))
33+
ret := os.Getcwd(ptr, uintptr(len(buf)))
34+
if ret != nil {
35+
return int(c.Strlen(ret)), nil
36+
}
37+
return 0, errnoErr(os.Errno)
38+
}
39+
40+
func Getwd() (string, error) {
41+
wd := os.Getcwd(c.Alloca(os.PATH_MAX), os.PATH_MAX)
42+
if wd != nil {
43+
return c.GoString(wd), nil
44+
}
45+
return "", errnoErr(os.Errno)
46+
}

ssa/expr.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -883,11 +883,6 @@ func (b Builder) Do(da DoAction, fn Expr, args ...Expr) (ret Expr) {
883883
// Go spec (excluding "make" and "new").
884884
func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) {
885885
switch fn {
886-
case "String": // unsafe.String
887-
return b.unsafeString(args[0].impl, args[1].impl)
888-
case "Slice": // unsafe.Slice
889-
size := args[1].impl
890-
return b.unsafeSlice(args[0], size, size)
891886
case "len":
892887
if len(args) == 1 {
893888
arg := args[0]
@@ -946,6 +941,13 @@ func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) {
946941
return b.Recover()
947942
case "print", "println":
948943
return b.PrintEx(fn == "println", args...)
944+
case "String": // unsafe.String
945+
return b.unsafeString(args[0].impl, args[1].impl)
946+
case "Slice": // unsafe.Slice
947+
size := args[1].impl
948+
return b.unsafeSlice(args[0], size, size)
949+
case "SliceData":
950+
return b.SliceData(args[0]) // TODO(xsw): check return type
949951
}
950952
panic("todo: " + fn)
951953
}

0 commit comments

Comments
 (0)