llgo/ssa: llvmSignature/castPtr/castInt bugfix; link: runtime

This commit is contained in:
xushiwei
2024-04-29 00:16:00 +08:00
parent 78d7f984d1
commit c30ed1b3c8
6 changed files with 715 additions and 27 deletions

View File

@@ -89,7 +89,7 @@ const (
func Do(args []string, conf *Config) {
flags, patterns, verbose := ParseArgs(args, buildFlags)
cfg := &packages.Config{
Mode: loadSyntax | packages.NeedDeps | packages.NeedExportFile,
Mode: loadSyntax | packages.NeedDeps | packages.NeedModule | packages.NeedExportFile,
BuildFlags: flags,
}
@@ -119,16 +119,14 @@ func Do(args []string, conf *Config) {
}
buildAllPkgs(prog, initial, mode, verbose)
var runtime *packages.Package
var runtimeFiles []string
if rt != nil {
buildAllPkgs(prog, rt, mode, verbose)
runtime = rt[0]
runtimeFiles = allLinkFiles(rt)
}
if mode != ModeBuild {
for _, pkg := range initial {
if pkg.Name == "main" {
linkMainPkg(pkg, runtime, conf, mode, verbose)
linkMainPkg(pkg, runtimeFiles, conf, mode, verbose)
}
}
}
@@ -146,7 +144,7 @@ func buildAllPkgs(prog llssa.Program, initial []*packages.Package, mode Mode, ve
}
}
func linkMainPkg(pkg, runtime *packages.Package, conf *Config, mode Mode, verbose bool) {
func linkMainPkg(pkg *packages.Package, runtimeFiles []string, conf *Config, mode Mode, verbose bool) {
pkgPath := pkg.PkgPath
name := path.Base(pkgPath)
app := conf.OutFile
@@ -154,12 +152,12 @@ func linkMainPkg(pkg, runtime *packages.Package, conf *Config, mode Mode, verbos
app = filepath.Join(conf.BinPath, name+conf.AppExt)
}
const N = 3
args := make([]string, N, len(pkg.Imports)+(N+2))
args := make([]string, N, len(pkg.Imports)+len(runtimeFiles)+(N+1))
args[0] = "-o"
args[1] = app
args[2] = "-Wno-override-module"
if runtime != nil {
args = append(args, runtime.ExportFile+".ll")
if runtimeFiles != nil {
args = append(args, runtimeFiles...)
}
packages.Visit([]*packages.Package{pkg}, nil, func(p *packages.Package) {
if p.PkgPath != "unsafe" { // TODO(xsw): maybe can remove this special case
@@ -282,6 +280,39 @@ func checkFlag(arg string, i *int, verbose *bool, swflags map[string]bool) {
}
}
func allLinkFiles(rt []*packages.Package) (outFiles []string) {
outFiles = make([]string, 0, len(rt))
root := rootLLGo(rt[0])
packages.Visit(rt, nil, func(p *packages.Package) {
if isPkgInLLGo(p.PkgPath) {
outFile := filepath.Join(root+p.PkgPath[len(llgoModPath):], "llgo_autogen.ll")
outFiles = append(outFiles, outFile)
}
})
return
}
// TODO(xsw): llgo root dir
func rootLLGo(runtime *packages.Package) string {
return runtime.Module.Dir
}
const (
llgoModPath = "github.com/goplus/llgo"
)
func isPkgInLLGo(pkgPath string) bool {
return isPkgInMod(pkgPath, llgoModPath)
}
func isPkgInMod(pkgPath, modPath string) bool {
if strings.HasPrefix(pkgPath, modPath) {
suffix := pkgPath[len(modPath):]
return suffix == "" || suffix[0] == '/'
}
return false
}
func check(err error) {
if err != nil {
panic(err)