Skip to content
Open
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
1 change: 1 addition & 0 deletions cl/_testpy/callpy/expect.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
sqrt(2) = 1.414214
cwd ok = 1
5 changes: 2 additions & 3 deletions cl/_testpy/callpy/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
"github.com/goplus/lib/py"
"github.com/goplus/lib/py/math"
"github.com/goplus/lib/py/os"
"github.com/goplus/lib/py/std"
)

func main() {
x := math.Sqrt(py.Float(2))
wd := os.Getcwd()
c.Printf(c.Str("sqrt(2) = %f\n"), x.Float64())
std.Print(py.Str("cwd ="), wd)
c.Printf(c.Str("sqrt(2) = %.6f\n"), x.Float64())
c.Printf(c.Str("cwd ok = %d\n"), int(wd.IsTrue()))
}
34 changes: 12 additions & 22 deletions cl/_testpy/callpy/out.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ source_filename = "github.com/goplus/llgo/cl/_testpy/callpy"
@"github.com/goplus/llgo/cl/_testpy/callpy.init$guard" = global i1 false, align 1
@__llgo_py.math.sqrt = linkonce global ptr null, align 8
@__llgo_py.os.getcwd = linkonce global ptr null, align 8
@0 = private unnamed_addr constant [14 x i8] c"sqrt(2) = %f\0A\00", align 1
@1 = private unnamed_addr constant [6 x i8] c"cwd =\00", align 1
@__llgo_py.builtins.print = linkonce global ptr null, align 8
@__llgo_py.builtins = external global ptr, align 8
@2 = private unnamed_addr constant [6 x i8] c"print\00", align 1
@0 = private unnamed_addr constant [16 x i8] c"sqrt(2) = %.6f\0A\00", align 1
@1 = private unnamed_addr constant [13 x i8] c"cwd ok = %d\0A\00", align 1
@__llgo_py.math = external global ptr, align 8
@3 = private unnamed_addr constant [5 x i8] c"sqrt\00", align 1
@2 = private unnamed_addr constant [5 x i8] c"sqrt\00", align 1
@__llgo_py.os = external global ptr, align 8
@4 = private unnamed_addr constant [7 x i8] c"getcwd\00", align 1
@3 = private unnamed_addr constant [7 x i8] c"getcwd\00", align 1

define void @"github.com/goplus/llgo/cl/_testpy/callpy.init"() {
_llgo_0:
Expand All @@ -23,13 +20,10 @@ _llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"github.com/goplus/llgo/cl/_testpy/callpy.init$guard", align 1
call void @"github.com/goplus/lib/py/math.init"()
call void @"github.com/goplus/lib/py/os.init"()
call void @"github.com/goplus/lib/py/std.init"()
%1 = load ptr, ptr @__llgo_py.builtins, align 8
call void (ptr, ...) @llgoLoadPyModSyms(ptr %1, ptr @2, ptr @__llgo_py.builtins.print, ptr null)
%2 = load ptr, ptr @__llgo_py.math, align 8
call void (ptr, ...) @llgoLoadPyModSyms(ptr %2, ptr @3, ptr @__llgo_py.math.sqrt, ptr null)
%3 = load ptr, ptr @__llgo_py.os, align 8
call void (ptr, ...) @llgoLoadPyModSyms(ptr %3, ptr @4, ptr @__llgo_py.os.getcwd, ptr null)
%1 = load ptr, ptr @__llgo_py.math, align 8
call void (ptr, ...) @llgoLoadPyModSyms(ptr %1, ptr @2, ptr @__llgo_py.math.sqrt, ptr null)
%2 = load ptr, ptr @__llgo_py.os, align 8
call void (ptr, ...) @llgoLoadPyModSyms(ptr %2, ptr @3, ptr @__llgo_py.os.getcwd, ptr null)
br label %_llgo_2

_llgo_2: ; preds = %_llgo_1, %_llgo_0
Expand All @@ -45,18 +39,16 @@ _llgo_0:
%4 = call ptr @PyObject_CallNoArgs(ptr %3)
%5 = call double @PyFloat_AsDouble(ptr %2)
%6 = call i32 (ptr, ...) @printf(ptr @0, double %5)
%7 = call ptr @PyUnicode_FromString(ptr @1)
%8 = load ptr, ptr @__llgo_py.builtins.print, align 8
%9 = call ptr (ptr, ...) @PyObject_CallFunctionObjArgs(ptr %8, ptr %7, ptr %4, ptr null)
%7 = call i32 @PyObject_IsTrue(ptr %4)
%8 = sext i32 %7 to i64
%9 = call i32 (ptr, ...) @printf(ptr @1, i64 %8)
ret void
}

declare void @"github.com/goplus/lib/py/math.init"()

declare void @"github.com/goplus/lib/py/os.init"()

declare void @"github.com/goplus/lib/py/std.init"()

declare ptr @PyFloat_FromDouble(double)

declare ptr @PyObject_CallOneArg(ptr, ptr)
Expand All @@ -67,8 +59,6 @@ declare double @PyFloat_AsDouble(ptr)

declare i32 @printf(ptr, ...)

declare ptr @PyUnicode_FromString(ptr)

declare ptr @PyObject_CallFunctionObjArgs(ptr, ...)
declare i32 @PyObject_IsTrue(ptr)

declare void @llgoLoadPyModSyms(ptr, ...)
3 changes: 3 additions & 0 deletions cl/_testpy/list/expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lens = 14 3
ptrs = 1 1
pi = 3.14159265358979
5 changes: 4 additions & 1 deletion cl/_testpy/list/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"unsafe"

"github.com/goplus/lib/c"
"github.com/goplus/lib/py"
"github.com/goplus/lib/py/math"
"github.com/goplus/lib/py/std"
Expand All @@ -13,5 +14,7 @@ func main() {
x := py.List(true, false, 1, float32(2.1), 3.1, uint(4), 1+2i, complex64(3+4i),
"hello", []byte("world"), [...]byte{1, 2, 3}, [...]byte{}, &v, unsafe.Pointer(&v))
y := py.List(std.Abs, std.Print, math.Pi)
std.Print(x, y)
c.Printf(c.Str("lens = %d %d\n"), x.ListLen(), y.ListLen())
c.Printf(c.Str("ptrs = %d %d\n"), x.ListItem(12).IsTrue(), x.ListItem(13).IsTrue())
c.Printf(c.Str("pi = %.15g\n"), y.ListItem(2).Float64())
}
32 changes: 26 additions & 6 deletions cl/_testpy/list/out.ll
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ source_filename = "github.com/goplus/llgo/cl/_testpy/list"
@2 = private unnamed_addr constant [3 x i8] c"pi\00", align 1
@__llgo_py.builtins.abs = linkonce global ptr null, align 8
@__llgo_py.builtins.print = linkonce global ptr null, align 8
@3 = private unnamed_addr constant [14 x i8] c"lens = %d %d\0A\00", align 1
@4 = private unnamed_addr constant [14 x i8] c"ptrs = %d %d\0A\00", align 1
@5 = private unnamed_addr constant [12 x i8] c"pi = %.15g\0A\00", align 1
@__llgo_py.builtins = external global ptr, align 8
@3 = private unnamed_addr constant [4 x i8] c"abs\00", align 1
@4 = private unnamed_addr constant [6 x i8] c"print\00", align 1
@6 = private unnamed_addr constant [4 x i8] c"abs\00", align 1
@7 = private unnamed_addr constant [6 x i8] c"print\00", align 1

define void @"github.com/goplus/llgo/cl/_testpy/list.init"() {
_llgo_0:
Expand All @@ -25,7 +28,7 @@ _llgo_1: ; preds = %_llgo_0
call void @"github.com/goplus/lib/py/math.init"()
call void @"github.com/goplus/lib/py/std.init"()
%1 = load ptr, ptr @__llgo_py.builtins, align 8
call void (ptr, ...) @llgoLoadPyModSyms(ptr %1, ptr @3, ptr @__llgo_py.builtins.abs, ptr @4, ptr @__llgo_py.builtins.print, ptr null)
call void (ptr, ...) @llgoLoadPyModSyms(ptr %1, ptr @6, ptr @__llgo_py.builtins.abs, ptr @7, ptr @__llgo_py.builtins.print, ptr null)
br label %_llgo_2

_llgo_2: ; preds = %_llgo_1, %_llgo_0
Expand Down Expand Up @@ -91,8 +94,17 @@ _llgo_0:
%47 = load ptr, ptr @__llgo_py.builtins.print, align 8
%48 = call i32 @PyList_SetItem(ptr %44, i64 1, ptr %47)
%49 = call i32 @PyList_SetItem(ptr %44, i64 2, ptr %43)
%50 = load ptr, ptr @__llgo_py.builtins.print, align 8
%51 = call ptr (ptr, ...) @PyObject_CallFunctionObjArgs(ptr %50, ptr %7, ptr %44, ptr null)
%50 = call i64 @PyList_Size(ptr %7)
%51 = call i64 @PyList_Size(ptr %44)
%52 = call i32 (ptr, ...) @printf(ptr @3, i64 %50, i64 %51)
%53 = call ptr @PyList_GetItem(ptr %7, i64 12)
%54 = call i32 @PyObject_IsTrue(ptr %53)
%55 = call ptr @PyList_GetItem(ptr %7, i64 13)
%56 = call i32 @PyObject_IsTrue(ptr %55)
%57 = call i32 (ptr, ...) @printf(ptr @4, i32 %54, i32 %56)
%58 = call ptr @PyList_GetItem(ptr %44, i64 2)
%59 = call double @PyFloat_AsDouble(ptr %58)
%60 = call i32 (ptr, ...) @printf(ptr @5, double %59)
ret void
}

Expand Down Expand Up @@ -129,7 +141,15 @@ declare ptr @PyBytes_FromStringAndSize(ptr, i64)

declare ptr @PyObject_GetAttrString(ptr, ptr)

declare ptr @PyObject_CallFunctionObjArgs(ptr, ...)
declare i64 @PyList_Size(ptr)

declare i32 @printf(ptr, ...)

declare ptr @PyList_GetItem(ptr, i64)

declare i32 @PyObject_IsTrue(ptr)

declare double @PyFloat_AsDouble(ptr)

declare void @llgoLoadPyModSyms(ptr, ...)

Expand Down
3 changes: 3 additions & 0 deletions cl/_testpy/max/expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
100.0
100.0
3.0
9 changes: 7 additions & 2 deletions internal/build/main_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ func genMainModule(ctx *context, rtPkgPath string, pkg *packages.Package, needRu
defineWeakNoArgStub(mainPkg, "syscall.init")

var pyInit llssa.Function
var pyFinalize llssa.Function
if needPyInit {
pyInit = declareNoArgFunc(mainPkg, "Py_Initialize")
pyFinalize = declareNoArgFunc(mainPkg, "Py_Finalize")
}

var rtInit llssa.Function
Expand All @@ -90,7 +92,7 @@ func genMainModule(ctx *context, rtPkgPath string, pkg *packages.Package, needRu
mainInit := declareNoArgFunc(mainPkg, pkg.PkgPath+".init")
mainMain := declareNoArgFunc(mainPkg, pkg.PkgPath+".main")

entryFn := defineEntryFunction(ctx, mainPkg, argcVar, argvVar, argvValueType, runtimeStub, mainInit, mainMain, pyInit, rtInit, abiInit)
entryFn := defineEntryFunction(ctx, mainPkg, argcVar, argvVar, argvValueType, runtimeStub, mainInit, mainMain, pyInit, pyFinalize, rtInit, abiInit)

if needStart(ctx) {
defineStart(mainPkg, entryFn, argvValueType)
Expand All @@ -106,7 +108,7 @@ func genMainModule(ctx *context, rtPkgPath string, pkg *packages.Package, needRu
// The entry stores argc/argv, optionally disables stdio buffering, runs
// initialization hooks (Python, runtime, package init), and finally calls
// main.main before returning 0.
Comment on lines 108 to 110
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment no longer reflects the full behavior — the function now also calls Py_Finalize() after main.main before returning 0.

Suggested change
// The entry stores argc/argv, optionally disables stdio buffering, runs
// initialization hooks (Python, runtime, package init), and finally calls
// main.main before returning 0.
// The entry stores argc/argv, optionally disables stdio buffering, runs
// initialization hooks (Python, runtime, package init), calls main.main,
// finalizes embedded Python if initialized, and returns 0.

func defineEntryFunction(ctx *context, pkg llssa.Package, argcVar, argvVar llssa.Global, argvType llssa.Type, runtimeStub, mainInit, mainMain llssa.Function, pyInit, rtInit, abiInit llssa.Function) llssa.Function {
func defineEntryFunction(ctx *context, pkg llssa.Package, argcVar, argvVar llssa.Global, argvType llssa.Type, runtimeStub, mainInit, mainMain llssa.Function, pyInit, pyFinalize, rtInit, abiInit llssa.Function) llssa.Function {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This function now has 11 parameters, which can make it hard to read and maintain. Consider grouping the initialization and finalization functions into a struct to improve clarity.

For example:

type entryHooks struct {
    runtimeStub llssa.Function
    mainInit    llssa.Function
    mainMain    llssa.Function
    pyInit      llssa.Function
    pyFinalize  llssa.Function
    rtInit      llssa.Function
    abiInit     llssa.Function
}

func defineEntryFunction(ctx *context, pkg llssa.Package, argcVar, argvVar llssa.Global, argvType llssa.Type, hooks *entryHooks) llssa.Function {
    // ...
    if hooks.pyInit != nil {
        b.Call(hooks.pyInit.Expr)
    }
    // ...
}

This would make the function signature more manageable and easier to extend in the future.

prog := pkg.Prog
entryName := "main"
if !needStart(ctx) && isWasmTarget(ctx.buildConf.Goos) {
Expand Down Expand Up @@ -137,6 +139,9 @@ func defineEntryFunction(ctx *context, pkg llssa.Package, argcVar, argvVar llssa
b.Call(runtimeStub.Expr)
b.Call(mainInit.Expr)
b.Call(mainMain.Expr)
if pyFinalize != nil {
b.Call(pyFinalize.Expr)
}
b.Return(prog.IntVal(0, prog.Int32()))
return fn
}
Expand Down
1 change: 1 addition & 0 deletions internal/build/main_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestGenMainModuleExecutable(t *testing.T) {
checks := []string{
"define i32 @main(",
"call void @Py_Initialize()",
"call void @Py_Finalize()",
"call void @\"example.com/foo.init\"()",
"define weak void @_start()",
}
Comment on lines 37 to 43
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The test checks that each substring is present in the IR, but doesn't verify call ordering. An accidental swap (e.g., Py_Finalize before main.main) would still pass. Consider adding an index-based ordering check:

initIdx := strings.Index(ir, "call void @Py_Initialize()")
mainIdx := strings.Index(ir, `call void @"example.com/foo.main"()`)
finalIdx := strings.Index(ir, "call void @Py_Finalize()")
if !(initIdx < mainIdx && mainIdx < finalIdx) {
    t.Fatalf("incorrect call ordering: init@%d, main@%d, finalize@%d", initIdx, mainIdx, finalIdx)
}

Expand Down
Loading