Merge pull request #149 from xushiwei/q

PyInit
This commit is contained in:
xushiwei
2024-05-12 11:28:04 +08:00
committed by GitHub
15 changed files with 189 additions and 56 deletions

View File

@@ -4,8 +4,8 @@ source_filename = "main"
@"main.init$guard" = global ptr null
@__llgo_argc = global ptr null
@__llgo_argv = global ptr null
@sqrt = external global ptr
@getcwd = external global ptr
@__llgo_py.math.sqrt = linkonce global ptr null
@__llgo_py.os.getcwd = linkonce global ptr null
@0 = private unnamed_addr constant [14 x i8] c"sqrt(2) = %f\0A\00", align 1
@1 = private unnamed_addr constant [10 x i8] c"cwd = %s\0A\00", align 1
@@ -26,13 +26,14 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
define void @main(i32 %0, ptr %1) {
_llgo_0:
call void @Py_Initialize()
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%2 = call ptr @PyFloat_FromDouble(double 2.000000e+00)
%3 = call ptr @PyObject_CallOneArg(ptr @sqrt, ptr %2)
%4 = call ptr @PyObject_CallNoArg(ptr @getcwd)
%3 = call ptr @PyObject_CallOneArg(ptr @__llgo_py.math.sqrt, ptr %2)
%4 = call ptr @PyObject_CallNoArgs(ptr @__llgo_py.os.getcwd)
%5 = call double @PyFloat_AsDouble()
%6 = call i32 (ptr, ...) @printf(ptr @0, double %5)
%7 = call ptr @PyBytes_AsString()
@@ -50,10 +51,12 @@ declare ptr @PyFloat_FromDouble(double)
declare ptr @PyObject_CallOneArg(ptr, ptr)
declare ptr @PyObject_CallNoArg(ptr)
declare ptr @PyObject_CallNoArgs(ptr)
declare double @PyFloat_AsDouble()
declare i32 @printf(ptr, ...)
declare ptr @PyBytes_AsString()
declare void @Py_Initialize()

View File

@@ -1,7 +1,7 @@
; ModuleID = 'math'
source_filename = "math"
@__llgo_py.math.sqrt = external global ptr
@__llgo_py.math.sqrt = linkonce global ptr null
@"math.init$guard" = global ptr null
@__llgo_py.math = linkonce global ptr null
@0 = private unnamed_addr constant [5 x i8] c"math\00", align 1

View File

@@ -159,6 +159,11 @@ func TestCompileEx(t *testing.T, src any, fname, expected string) {
if err != nil {
t.Fatal("cl.NewPackage failed:", err)
}
if prog.NeedPyInit() { // call PyInit if needed
ret.PyInit()
}
if v := ret.String(); v != expected {
t.Fatalf("\n==> got:\n%s\n==> expected:\n%s\n", v, expected)
}

View File

@@ -285,13 +285,18 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
// funcOf returns a function by name and set ftype = goFunc, cFunc, etc.
// or returns nil and set ftype = llgoCstr, llgoAlloca, llgoUnreachable, etc.
func (p *context) funcOf(fn *ssa.Function) (aFn llssa.Function, pyFn llssa.PyFunction, ftype int) {
_, name, ftype := p.funcName(fn, false)
pkgTypes, name, ftype := p.funcName(fn, false)
switch ftype {
case pyFunc:
pkg := p.pkg
if pyFn = pkg.PyFuncOf(name); pyFn == nil {
pyFn = pkg.NewPyFunc(name, fn.Signature)
if kind, mod := pkgKindByScope(pkgTypes.Scope()); kind == PkgPyModule {
pkg := p.pkg
fnName := pysymPrefix + mod + "." + name
if pyFn = pkg.PyFuncOf(fnName); pyFn == nil {
pyFn = pkg.NewPyFunc(fnName, fn.Signature)
return
}
}
ftype = ignoredFunc
case llgoInstr:
switch name {
case "cstr":
@@ -371,8 +376,7 @@ const (
)
func callRuntimeInit(b llssa.Builder, pkg llssa.Package) {
sig := types.NewSignatureType(nil, nil, nil, nil, nil, false)
fn := pkg.NewFunc(RuntimeInit, sig, llssa.InC) // don't need to convert runtime.init
fn := pkg.NewFunc(RuntimeInit, llssa.NoArgsNoRet, llssa.InC) // don't need to convert runtime.init
b.Call(fn.Expr)
}

View File

@@ -33,7 +33,7 @@ func TestFromTestpy(t *testing.T) {
}
func TestFromTestlibc(t *testing.T) {
cltest.FromDir(t, "", "./_testlibc", false)
cltest.FromDir(t, "", "./_testlibc", true)
}
func TestFromTestrt(t *testing.T) {

View File

@@ -214,7 +214,7 @@ func (p *context) initLink(line string, prefix int, f func(inPkgName string) (fu
} else {
panic(line + ": no specified call convention. eg. //go:linkname Printf C.printf")
}
} else {
} else if c := inPkgName[0]; c >= 'A' && c <= 'Z' {
fmt.Fprintln(os.Stderr, "==>", line)
fmt.Fprintf(os.Stderr, "llgo: linkname %s not found and ignored\n", inPkgName)
}