Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch: io, io/fs, os; llgo.string; c string library; demo: getcwd; abi.TypeName fix: error interface is public #364

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions _demo/getcwd/getcwd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/os"
)

func main() {
wd := os.Getcwd(c.Alloca(os.PATH_MAX), os.PATH_MAX)
c.Printf(c.Str("cwd: %s\n"), wd)
}
59 changes: 59 additions & 0 deletions c/c.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,65 @@ func Memset(s Pointer, c Int, n uintptr) Pointer

// -----------------------------------------------------------------------------

//go:linkname Strlen C.strlen
func Strlen(s *Char) uintptr

//go:linkname Strcpy C.strcpy
func Strcpy(dst, src *Char) *Char

//go:linkname Strncpy C.strncpy
func Strncpy(dst, src *Char, n uintptr) *Char

//go:linkname Strcat C.strcat
func Strcat(dst, src *Char) *Char

//go:linkname Strncat C.strncat
func Strncat(dst, src *Char, n uintptr) *Char

//go:linkname Strcmp C.strcmp
func Strcmp(s1, s2 *Char) Int

//go:linkname Strncmp C.strncmp
func Strncmp(s1, s2 *Char, n uintptr) Int

//go:linkname Strchr C.strchr
func Strchr(s *Char, c Int) *Char

//go:linkname Strrchr C.strrchr
func Strrchr(s *Char, c Int) *Char

//go:linkname Strstr C.strstr
func Strstr(s1, s2 *Char) *Char

//go:linkname Strdup C.strdup
func Strdup(s *Char) *Char

//go:linkname Strndup C.strndup
func Strndup(s *Char, n uintptr) *Char

//go:linkname Strtok C.strtok
func Strtok(s, delim *Char) *Char

//go:linkname Strerror C.strerror
func Strerror(errnum Int) *Char

//go:linkname Sprintf C.sprintf
func Sprintf(s *Char, format *Char, __llgo_va_list ...any) Int

//go:linkname Snprintf C.snprintf
func Snprintf(s *Char, n uintptr, format *Char, __llgo_va_list ...any) Int

//go:linkname Vsnprintf C.vsnprintf
func Vsnprintf(s *Char, n uintptr, format *Char, ap Pointer) Int

// -----------------------------------------------------------------------------

// GoString converts a C string to a Go string.
// TODO(xsw): any => int
//
//go:linkname GoString llgo.string
func GoString(cstr *Char, __llgo_va_list /* n */ ...any) string

//go:linkname GoStringData llgo.stringData
func GoStringData(string) *Char

Expand Down
225 changes: 225 additions & 0 deletions c/os/os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
/*
* 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 os

// #include <sys/stat.h>
// #include <limits.h>
import "C"

import (
_ "unsafe"

"github.com/goplus/llgo/c"
)

const (
LLGoPackage = "decl"
)

const (
PATH_MAX = C.PATH_MAX
)

type (
ModeT C.mode_t
UidT C.uid_t
GidT C.gid_t
OffT C.off_t
DevT C.dev_t
StatT C.struct_stat
)

//go:linkname Errno errno
var Errno c.Int

//go:linkname Umask C.umask
func Umask(cmask ModeT) ModeT

//go:linkname Mkdir C.mkdir
func Mkdir(path *c.Char, mode ModeT) c.Int

//go:linkname Rmdir C.rmdir
func Rmdir(path *c.Char) c.Int

//go:linkname Link C.link
func Link(oldpath *c.Char, newpath *c.Char) c.Int

//go:linkname Symlink C.symlink
func Symlink(target *c.Char, linkpath *c.Char) c.Int

//go:linkname Readlink C.readlink
func Readlink(path *c.Char, buf c.Pointer, bufsize uintptr) int

//go:linkname Unlink C.unlink
func Unlink(path *c.Char) c.Int

//go:linkname Remove C.remove
func Remove(path *c.Char) c.Int

//go:linkname Rename C.rename
func Rename(oldpath *c.Char, newpath *c.Char) c.Int

//go:linkname Stat C.stat
func Stat(path *c.Char, buf *StatT) c.Int

//go:linkname Lstat C.lstat
func Lstat(path *c.Char, buf *StatT) c.Int

//go:linkname Truncate C.truncate
func Truncate(path *c.Char, length OffT) c.Int

//go:linkname Chmod C.chmod
func Chmod(path *c.Char, mode ModeT) c.Int

//go:linkname Chown C.chown
func Chown(path *c.Char, owner UidT, group GidT) c.Int

//go:linkname Lchown C.lchown
func Lchown(path *c.Char, owner UidT, group GidT) c.Int

// -----------------------------------------------------------------------------

//go:linkname Getcwd C.getcwd
func Getcwd(buffer c.Pointer, size uintptr) *c.Char

//go:linkname Chdir C.chdir
func Chdir(path *c.Char) c.Int

//go:linkname Chroot C.chroot
func Chroot(path *c.Char) c.Int

// -----------------------------------------------------------------------------

//go:linkname Environ C.environ
func Environ() **c.Char

//go:linkname Getenv C.getenv
func Getenv(name *c.Char) *c.Char

//go:linkname Setenv C.setenv
func Setenv(name *c.Char, value *c.Char, overwrite c.Int) c.Int

//go:linkname Putenv C.putenv
func Putenv(env *c.Char) c.Int

//go:linkname Unsetenv C.unsetenv
func Unsetenv(name *c.Char) c.Int

//go:linkname Clearenv C.clearenv
func Clearenv()

// -----------------------------------------------------------------------------

//go:linkname Fchdir C.fchdir
func Fchdir(dirfd c.Int) c.Int

//go:linkname Faccessat C.faccessat
func Faccessat(dirfd c.Int, path *c.Char, mode c.Int, flags c.Int) c.Int

//go:linkname Fchmodat C.fchmodat
func Fchmodat(dirfd c.Int, path *c.Char, mode ModeT, flags c.Int) c.Int

//go:linkname Fchownat C.fchownat
func Fchownat(dirfd c.Int, path *c.Char, owner UidT, group GidT, flags c.Int) c.Int

//go:linkname Fstatat C.fstatat
func Fstatat(dirfd c.Int, path *c.Char, buf *StatT, flags c.Int) c.Int

// -----------------------------------------------------------------------------

//go:linkname Open C.open
func Open(path *c.Char, flags c.Int, mode ModeT) c.Int

//go:linkname Creat C.creat
func Creat(path *c.Char, mode ModeT) c.Int

//go:linkname Dup C.dup
func Dup(fd c.Int) c.Int

//go:linkname Dup2 C.dup2
func Dup2(oldfd c.Int, newfd c.Int) c.Int

/* TODO(xsw):
On Alpha, IA-64, MIPS, SuperH, and SPARC/SPARC64, pipe() has the following prototype:
struct fd_pair {
long fd[2];
};
struct fd_pair pipe(void);
*/
//go:linkname Pipe C.pipe
func Pipe(fds *[2]c.Int) c.Int

//go:linkname Mkfifo C.mkfifo
func Mkfifo(path *c.Char, mode ModeT) c.Int

//go:linkname Mknod C.mknod
func Mknod(path *c.Char, mode ModeT, dev DevT) c.Int

//go:linkname Close C.close
func Close(fd c.Int) c.Int

//go:linkname Read C.read
func Read(fd c.Int, buf c.Pointer, count uintptr) int

//go:linkname Write C.write
func Write(fd c.Int, buf c.Pointer, count uintptr) int

//go:linkname Lseek C.lseek
func Lseek(fd c.Int, offset OffT, whence c.Int) OffT

//go:linkname Fsync C.fsync
func Fsync(fd c.Int) c.Int

//go:linkname Ftruncate C.ftruncate
func Ftruncate(fd c.Int, length OffT) c.Int

//go:linkname Fchmod C.fchmod
func Fchmod(fd c.Int, mode ModeT) c.Int

//go:linkname Fchown C.fchown
func Fchown(fd c.Int, owner UidT, group GidT) c.Int

//go:linkname Fstat C.fstat
func Fstat(fd c.Int, buf *StatT) c.Int

//go:linkname Isatty C.isatty
func Isatty(fd c.Int) c.Int

// -----------------------------------------------------------------------------

//go:linkname Exit C.exit
func Exit(c.Int)

//go:linkname Getpid C.getpid
func Getpid() c.Int

//go:linkname Getppid C.getppid
func Getppid() c.Int

//go:linkname Getuid C.getuid
func Getuid() UidT

//go:linkname Geteuid C.geteuid
func Geteuid() UidT

//go:linkname Getgid C.getgid
func Getgid() GidT

//go:linkname Getegid C.getegid
func Getegid() GidT

// -----------------------------------------------------------------------------
8 changes: 4 additions & 4 deletions c/pthread/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func (o *Once) Do(f func()) c.Int { return 0 }
type MutexType c.Int

const (
MutexNormal MutexType = C.PTHREAD_MUTEX_NORMAL
MutexErrorCheck MutexType = C.PTHREAD_MUTEX_ERRORCHECK
MutexRecursive MutexType = C.PTHREAD_MUTEX_RECURSIVE
MutexDefault MutexType = C.PTHREAD_MUTEX_DEFAULT
MUTEX_NORMAL MutexType = C.PTHREAD_MUTEX_NORMAL
MUTEX_ERRORCHECK MutexType = C.PTHREAD_MUTEX_ERRORCHECK
MUTEX_RECURSIVE MutexType = C.PTHREAD_MUTEX_RECURSIVE
MUTEX_DEFAULT MutexType = C.PTHREAD_MUTEX_DEFAULT
)

// MutexAttr is a mutex attribute object.
Expand Down
4 changes: 2 additions & 2 deletions cl/_testdata/utf8/out.ll
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ _llgo_1: ; preds = %_llgo_3
store i64 7, ptr %4, align 4
%5 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %2, align 8
%6 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %5, 1
%7 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewStringSlice"(%"github.com/goplus/llgo/internal/runtime.String" %5, i64 %15, i64 %6)
%7 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.StringSlice"(%"github.com/goplus/llgo/internal/runtime.String" %5, i64 %15, i64 %6)
%8 = call { i32, i64 } @"unicode/utf8.DecodeRuneInString"(%"github.com/goplus/llgo/internal/runtime.String" %7)
%9 = extractvalue { i32, i64 } %8, 0
%10 = extractvalue { i32, i64 } %8, 1
Expand Down Expand Up @@ -95,7 +95,7 @@ declare void @"unicode/utf8.init"()

declare void @"github.com/goplus/llgo/internal/runtime.init"()

declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewStringSlice"(%"github.com/goplus/llgo/internal/runtime.String", i64, i64)
declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.StringSlice"(%"github.com/goplus/llgo/internal/runtime.String", i64, i64)

declare { i32, i64 } @"unicode/utf8.DecodeRuneInString"(%"github.com/goplus/llgo/internal/runtime.String")

Expand Down
Loading
Loading