diff --git a/.gitignore b/.gitignore index 18a368ba..ff4e3cde 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ *.dylib test.db -llgo_autogen.ll +llgo_autogen*.ll stories*.bin .DS_Store err.log diff --git a/c/c.go b/c/c.go index e557e829..da64e03d 100644 --- a/c/c.go +++ b/c/c.go @@ -97,13 +97,13 @@ func Qsort(base Pointer, count, elem uintptr, compar func(a, b Pointer) Int) // ----------------------------------------------------------------------------- -//go:linkname Stdin __stdinp +//go:linkname Stdin llgo_stdin var Stdin FilePtr -//go:linkname Stdout __stdoutp +//go:linkname Stdout llgo_stdout var Stdout FilePtr -//go:linkname Stderr __stderrp +//go:linkname Stderr llgo_stderr var Stderr FilePtr //go:linkname Printf C.printf diff --git a/cl/_testrt/concat/out.ll b/cl/_testrt/concat/out.ll index 1ef2685d..9480aeb1 100644 --- a/cl/_testrt/concat/out.ll +++ b/cl/_testrt/concat/out.ll @@ -13,7 +13,7 @@ source_filename = "main" @3 = private unnamed_addr constant [6 x i8] c"Hello\00", align 1 @4 = private unnamed_addr constant [2 x i8] c" \00", align 1 @5 = private unnamed_addr constant [6 x i8] c"World\00", align 1 -@__stderrp = external global ptr +@llgo_stderr = external global ptr @6 = private unnamed_addr constant [8 x i8] c"Hi, %s\0A\00", align 1 define %"github.com/goplus/llgo/internal/runtime.String" @main.concat(%"github.com/goplus/llgo/internal/runtime.Slice" %0) { @@ -119,7 +119,7 @@ _llgo_0: store i64 3, ptr %21, align 4 %22 = load %"github.com/goplus/llgo/internal/runtime.Slice", ptr %18, align 8 %23 = call %"github.com/goplus/llgo/internal/runtime.String" @main.concat(%"github.com/goplus/llgo/internal/runtime.Slice" %22) - %24 = load ptr, ptr @__stderrp, align 8 + %24 = load ptr, ptr @llgo_stderr, align 8 %25 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %23, 1 %26 = add i64 %25, 1 %27 = alloca i8, i64 %26, align 1 diff --git a/cl/cltest/cltest.go b/cl/cltest/cltest.go index a0d0b750..04d1036b 100644 --- a/cl/cltest/cltest.go +++ b/cl/cltest/cltest.go @@ -26,6 +26,7 @@ import ( "log" "os" "path" + "path/filepath" "strings" "testing" @@ -74,7 +75,7 @@ func decodeLinkFile(llFile string) (data []byte, err error) { return } defer zipf.Close() - f, err := zipf.Open("llgo_autogen.ll") + f, err := zipf.Open(filepath.Base(llFile)) if err != nil { return } diff --git a/cl/import.go b/cl/import.go index 165b2f8c..35ef5317 100644 --- a/cl/import.go +++ b/cl/import.go @@ -105,6 +105,8 @@ func pkgKind(v string) (int, string) { return PkgLinkExtern, v[5:] } else if strings.HasPrefix(v, "py.") { // "py." return PkgPyModule, v[3:] + } else if strings.HasPrefix(v, "decl:") { // "decl: " + return PkgDeclOnly, v[5:] } } return PkgLLGo, "" diff --git a/internal/build/build.go b/internal/build/build.go index 32345acd..117b7dc7 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -449,8 +449,13 @@ func allLinkFiles(rt []*packages.Package) (outFiles []string) { outFiles = make([]string, 0, len(rt)) packages.Visit(rt, nil, func(p *packages.Package) { pkgPath := p.PkgPath + kind, param := cl.PkgKindOf(p.Types) if isRuntimePkg(pkgPath) { - llgoPkgLinkFiles(pkgPath, func(linkFile string) { + exptFile := "" + if kind == cl.PkgLinkIR || kind == cl.PkgDeclOnly { + exptFile = strings.TrimSpace(param) + } + llgoPkgLinkFiles(pkgPath, exptFile, func(linkFile string) { outFiles = append(outFiles, linkFile) }) } @@ -459,13 +464,14 @@ func allLinkFiles(rt []*packages.Package) (outFiles []string) { } const ( - pkgAbi = llgoModPath + "/internal/abi" - pkgRuntime = llgoModPath + "/internal/runtime" + pkgAbi = llgoModPath + "/internal/abi" + pkgRuntime = llgoModPath + "/internal/runtime" + pkgRuntimeC = llgoModPath + "/internal/runtime/c" ) func isRuntimePkg(pkgPath string) bool { switch pkgPath { - case pkgRuntime, pkgAbi: + case pkgRuntime, pkgAbi, pkgRuntimeC: return true } return false @@ -501,7 +507,7 @@ func concatPkgLinkFiles(pkgPath string) string { var b strings.Builder var ret string var n int - llgoPkgLinkFiles(pkgPath, func(linkFile string) { + llgoPkgLinkFiles(pkgPath, "", func(linkFile string) { if n == 0 { ret = linkFile } else { @@ -518,13 +524,16 @@ func concatPkgLinkFiles(pkgPath string) string { return ret } -func llgoPkgLinkFiles(pkgPath string, procFile func(linkFile string)) { +func llgoPkgLinkFiles(pkgPath string, llFile string, procFile func(linkFile string)) { dir := llgoRoot() + pkgPath[len(llgoModPath):] + "/" - llFile := dir + "llgo_autogen.ll" - llaFile := llFile + "a" - zipf, err := zip.OpenReader(llaFile) + if llFile == "" { + llFile = "llgo_autogen.ll" + } + llPath := dir + llFile + llaPath := llPath + "a" + zipf, err := zip.OpenReader(llaPath) if err != nil { - procFile(llFile) + procFile(llPath) return } defer zipf.Close() @@ -532,7 +541,7 @@ func llgoPkgLinkFiles(pkgPath string, procFile func(linkFile string)) { for _, f := range zipf.File { procFile(dir + f.Name) } - if _, err := os.Stat(llFile); os.IsNotExist(err) { + if _, err := os.Stat(llPath); os.IsNotExist(err) { for _, f := range zipf.File { decodeFile(dir+f.Name, f) } diff --git a/internal/runtime/c/c.go b/internal/runtime/c/c.go index 3d200e36..a81e95f2 100644 --- a/internal/runtime/c/c.go +++ b/internal/runtime/c/c.go @@ -19,10 +19,6 @@ package c import "C" import "unsafe" -const ( - LLGoPackage = "decl" -) - type ( Char = int8 Int = C.int @@ -30,13 +26,13 @@ type ( FilePtr = unsafe.Pointer ) -//go:linkname Stdin __stdinp +//go:linkname Stdin llgo_stdin var Stdin FilePtr -//go:linkname Stdout __stdoutp +//go:linkname Stdout llgo_stdout var Stdout FilePtr -//go:linkname Stderr __stderrp +//go:linkname Stderr llgo_stderr var Stderr FilePtr //go:linkname Str llgo.cstr diff --git a/internal/runtime/c/c_default.go b/internal/runtime/c/c_default.go new file mode 100644 index 00000000..fd3e7261 --- /dev/null +++ b/internal/runtime/c/c_default.go @@ -0,0 +1,8 @@ +//go:build !linux +// +build !linux + +package c + +const ( + LLGoPackage = "decl: stdio_default.ll" +) diff --git a/internal/runtime/c/c_default.ll b/internal/runtime/c/c_default.ll new file mode 100644 index 00000000..208dd270 --- /dev/null +++ b/internal/runtime/c/c_default.ll @@ -0,0 +1,9 @@ +; ModuleID = 'github.com/goplus/llgo/internal/runtime/c' +source_filename = "github.com/goplus/llgo/internal/runtime/c" + +@__stderrp = external global ptr +@__stdinp = external global ptr +@__stdoutp = external global ptr +@llgo_stdin = alias i8*, i8** @__stdinp +@llgo_stdout = alias i8*, i8** @__stdoutp +@llgo_stderr = alias i8*, i8** @__stderrp diff --git a/internal/runtime/c/c_linux.go b/internal/runtime/c/c_linux.go new file mode 100644 index 00000000..6f22e8de --- /dev/null +++ b/internal/runtime/c/c_linux.go @@ -0,0 +1,8 @@ +//go:build linux +// +build linux + +package c + +const ( + LLGoPackage = "decl: stdio_linux.ll" +) diff --git a/internal/runtime/c/c_linux.ll b/internal/runtime/c/c_linux.ll new file mode 100644 index 00000000..4fc23ebf --- /dev/null +++ b/internal/runtime/c/c_linux.ll @@ -0,0 +1,9 @@ +; ModuleID = 'github.com/goplus/llgo/internal/runtime/c' +source_filename = "github.com/goplus/llgo/internal/runtime/c" + +@stderr = external global ptr +@stdin = external global ptr +@stdout = external global ptr +@llgo_stdin = alias i8*, i8** @stdin +@llgo_stdout = alias i8*, i8** @stdout +@llgo_stderr = alias i8*, i8** @stderr diff --git a/internal/runtime/llgo_autogen.lla b/internal/runtime/llgo_autogen.lla index 4cb3ec4a..64fd5020 100644 Binary files a/internal/runtime/llgo_autogen.lla and b/internal/runtime/llgo_autogen.lla differ