diff --git a/internal/build/build.go b/internal/build/build.go index d3f16c6f..d115f7c6 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -19,6 +19,7 @@ package build import ( "archive/zip" "fmt" + "go/constant" "go/token" "go/types" "io" @@ -147,7 +148,7 @@ func Do(args []string, conf *Config) { progSSA := ssa.NewProgram(initial[0].Fset, ssaBuildMode) patches := make(cl.Patches, len(altPkgPaths)) - altSSAPkgs(progSSA, patches, altPkgs[1:]) + altSSAPkgs(progSSA, patches, altPkgs[1:], verbose) ctx := &context{progSSA, prog, dedup, patches, make(map[string]none), mode, verbose} pkgs := buildAllPkgs(ctx, initial) @@ -234,9 +235,8 @@ func buildAllPkgs(ctx *context, initial []*packages.Package) (pkgs []*aPackage) // and set no export file pkg.ExportFile = "" case cl.PkgLinkIR, cl.PkgLinkExtern, cl.PkgPyModule: - pkgPath := pkg.PkgPath - if isPkgInLLGo(pkgPath) { - pkg.ExportFile = concatPkgLinkFiles(pkgPath) + if isPkgInLLGo(pkg.PkgPath) { + pkg.ExportFile = concatPkgLinkFiles(pkg, ctx.verbose) } else { // panic("todo") // TODO(xsw): support packages out of llgo @@ -414,14 +414,14 @@ func altPkgs(initial []*packages.Package, alts ...string) []string { return alts } -func altSSAPkgs(prog *ssa.Program, patches cl.Patches, alts []*packages.Package) { +func altSSAPkgs(prog *ssa.Program, patches cl.Patches, alts []*packages.Package, verbose bool) { packages.Visit(alts, nil, func(p *packages.Package) { if p.Types != nil && !p.IllTyped { pkgSSA := prog.CreatePackage(p.Types, p.Syntax, p.TypesInfo, true) if strings.HasPrefix(p.PkgPath, altPkgPathPrefix) { path := p.PkgPath[len(altPkgPathPrefix):] patches[path] = pkgSSA - if debugBuild { + if debugBuild || verbose { log.Println("==> Patching", path) } } @@ -563,11 +563,11 @@ func isSingleLinkFile(ret string) bool { return len(ret) > 0 && ret[0] != ' ' } -func concatPkgLinkFiles(pkgPath string) string { +func concatPkgLinkFiles(pkg *packages.Package, verbose bool) string { var b strings.Builder var ret string var n int - llgoPkgLinkFiles(pkgPath, "", func(linkFile string) { + llgoPkgLinkFiles(pkg, "", func(linkFile string) { if n == 0 { ret = linkFile } else { @@ -575,7 +575,7 @@ func concatPkgLinkFiles(pkgPath string) string { b.WriteString(linkFile) } n++ - }) + }, verbose) if n > 1 { b.WriteByte(' ') b.WriteString(ret) @@ -584,7 +584,39 @@ func concatPkgLinkFiles(pkgPath string) string { return ret } -func llgoPkgLinkFiles(pkgPath string, llFile string, procFile func(linkFile string)) { +// const LLGoFiles = "file1; file2; ..." +func llgoPkgLinkFiles(pkg *packages.Package, llFile string, procFile func(linkFile string), verbose bool) { + if o := pkg.Types.Scope().Lookup("LLGoFiles"); o != nil { + val := o.(*types.Const).Val() + if val.Kind() == constant.String { + clFiles(constant.StringVal(val), pkg, procFile, verbose) + } + } + unzipPkgLinkFiles(pkg.PkgPath, llFile, procFile) +} + +// files = "file1; file2; ..." +func clFiles(files string, pkg *packages.Package, procFile func(linkFile string), verbose bool) { + dir := filepath.Dir(pkg.CompiledGoFiles[0]) + expFile := pkg.ExportFile + for _, file := range strings.Split(files, ";") { + cFile := filepath.Join(dir, strings.TrimSpace(file)) + clFile(cFile, expFile, procFile, verbose) + } +} + +func clFile(cFile, expFile string, procFile func(linkFile string), verbose bool) { + llFile := expFile + filepath.Base(cFile) + ".ll" + args := []string{"-emit-llvm", "-S", "-o", llFile, "-c", cFile} + if verbose { + fmt.Fprintln(os.Stderr, "clang", args) + } + err := clang.New("").Exec(args...) + check(err) + procFile(llFile) +} + +func unzipPkgLinkFiles(pkgPath string, llFile string, procFile func(linkFile string)) { dir := llgoRoot() + pkgPath[len(llgoModPath):] + "/" if llFile == "" { llFile = "llgo_autogen.ll" diff --git a/py/llgo.cfg b/py/llgo.cfg deleted file mode 100644 index dcc7c16d..00000000 --- a/py/llgo.cfg +++ /dev/null @@ -1,7 +0,0 @@ -{ - "cl": [ - "clang -emit-llvm -S -o module.ll -c _pyg/module.c", - "llgen .", - "rm llgo_autogen.lla; zip llgo_autogen.lla llgo_autogen.ll module.ll", - ] -} diff --git a/py/llgo_autogen.lla b/py/llgo_autogen.lla index 36aace83..0bbf8a00 100644 Binary files a/py/llgo_autogen.lla and b/py/llgo_autogen.lla differ diff --git a/py/module.ll b/py/module.ll deleted file mode 100644 index d11c7450..00000000 --- a/py/module.ll +++ /dev/null @@ -1,82 +0,0 @@ -; ModuleID = '_pyg/module.c' -source_filename = "_pyg/module.c" - -%struct.PyObject = type opaque - -; Function Attrs: noinline nounwind optnone ssp uwtable(sync) -define void @llgoLoadPyModSyms(%struct.PyObject* noundef %0, ...) #0 { - %2 = alloca %struct.PyObject*, align 8 - %3 = alloca i8*, align 8 - %4 = alloca i8*, align 8 - %5 = alloca i8*, align 8 - %6 = alloca %struct.PyObject**, align 8 - %7 = alloca %struct.PyObject**, align 8 - store %struct.PyObject* %0, %struct.PyObject** %2, align 8 - %8 = bitcast i8** %3 to i8* - call void @llvm.va_start(i8* %8) - br label %9 - -9: ; preds = %26, %1 - %10 = va_arg i8** %3, i8* - store i8* %10, i8** %5, align 8 - %11 = load i8*, i8** %5, align 8 - store i8* %11, i8** %4, align 8 - %12 = load i8*, i8** %4, align 8 - %13 = icmp eq i8* %12, null - br i1 %13, label %14, label %15 - -14: ; preds = %9 - br label %27 - -15: ; preds = %9 - %16 = va_arg i8** %3, %struct.PyObject** - store %struct.PyObject** %16, %struct.PyObject*** %7, align 8 - %17 = load %struct.PyObject**, %struct.PyObject*** %7, align 8 - store %struct.PyObject** %17, %struct.PyObject*** %6, align 8 - %18 = load %struct.PyObject**, %struct.PyObject*** %6, align 8 - %19 = load %struct.PyObject*, %struct.PyObject** %18, align 8 - %20 = icmp eq %struct.PyObject* %19, null - br i1 %20, label %21, label %26 - -21: ; preds = %15 - %22 = load %struct.PyObject*, %struct.PyObject** %2, align 8 - %23 = load i8*, i8** %4, align 8 - %24 = call %struct.PyObject* @PyObject_GetAttrString(%struct.PyObject* noundef %22, i8* noundef %23) - %25 = load %struct.PyObject**, %struct.PyObject*** %6, align 8 - store %struct.PyObject* %24, %struct.PyObject** %25, align 8 - br label %26 - -26: ; preds = %21, %15 - br label %9 - -27: ; preds = %14 - %28 = bitcast i8** %3 to i8* - call void @llvm.va_end(i8* %28) - ret void -} - -; Function Attrs: nocallback nofree nosync nounwind willreturn -declare void @llvm.va_start(i8*) #1 - -declare %struct.PyObject* @PyObject_GetAttrString(%struct.PyObject* noundef, i8* noundef) #2 - -; Function Attrs: nocallback nofree nosync nounwind willreturn -declare void @llvm.va_end(i8*) #1 - -attributes #0 = { noinline nounwind optnone ssp uwtable(sync) "frame-pointer"="non-leaf" "min-legal-vector-width"="0" "no-trapping-math"="true" "probe-stack"="__chkstk_darwin" "stack-protector-buffer-size"="8" "target-cpu"="apple-m1" "target-features"="+aes,+crc,+crypto,+dotprod,+fp-armv8,+fp16fml,+fullfp16,+lse,+neon,+ras,+rcpc,+rdm,+sha2,+sha3,+sm4,+v8.5a,+zcm,+zcz" } -attributes #1 = { nocallback nofree nosync nounwind willreturn } -attributes #2 = { "frame-pointer"="non-leaf" "no-trapping-math"="true" "probe-stack"="__chkstk_darwin" "stack-protector-buffer-size"="8" "target-cpu"="apple-m1" "target-features"="+aes,+crc,+crypto,+dotprod,+fp-armv8,+fp16fml,+fullfp16,+lse,+neon,+ras,+rcpc,+rdm,+sha2,+sha3,+sm4,+v8.5a,+zcm,+zcz" } - -!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} -!llvm.ident = !{!9} - -!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 13, i32 3]} -!1 = !{i32 1, !"wchar_size", i32 4} -!2 = !{i32 8, !"branch-target-enforcement", i32 0} -!3 = !{i32 8, !"sign-return-address", i32 0} -!4 = !{i32 8, !"sign-return-address-all", i32 0} -!5 = !{i32 8, !"sign-return-address-with-bkey", i32 0} -!6 = !{i32 7, !"PIC Level", i32 2} -!7 = !{i32 7, !"uwtable", i32 1} -!8 = !{i32 7, !"frame-pointer", i32 1} -!9 = !{!"Apple clang version 14.0.3 (clang-1403.0.22.14.1)"} diff --git a/py/python.go b/py/python.go index 78330505..0d20a685 100644 --- a/py/python.go +++ b/py/python.go @@ -23,6 +23,7 @@ import ( ) const ( + LLGoFiles = "_pyg/module.c" LLGoPackage = "link: $LLGO_LIB_PYTHON; $(pkg-config --libs python3-embed)" )