cl: funcOf fix: call pyfunc multiple times

This commit is contained in:
xushiwei
2024-05-19 00:15:23 +08:00
parent 0787909045
commit 63e678928b
5 changed files with 110 additions and 13 deletions

View File

@@ -48,6 +48,7 @@ You can import a Python library in LLGo!
And you can import any Python library into `llgo` through a program called `llpyg` (see [Development tools](#development-tools)). The currently imported libraries include:
* [builtins](https://pkg.go.dev/github.com/goplus/llgo/py/std)
* [sys](https://pkg.go.dev/github.com/goplus/llgo/py/sys)
* [os](https://pkg.go.dev/github.com/goplus/llgo/py/os)
* [math](https://pkg.go.dev/github.com/goplus/llgo/py/math)
@@ -165,13 +166,6 @@ Here are some examples related to them:
## Go syntax support
The priority of `llgo` feature iteration is:
* Popular C/Python libraries
* Full Go syntax
* Go standard libraries
* Popular Go packages
Common Go syntax is already supported. Except for the following, which needs to be improved:
* interface (Limited support)
@@ -190,6 +184,14 @@ Here are some examples related to Go syntax:
* [genints](_demo/genints/genints.go): various forms of closure usage (including C function, recv.method and anonymous function)
## Go packages support
Here are the Go packages that can be imported correctly:
* [unsafe](https://pkg.go.dev/unsafe)
* [unicode/utf8](https://pkg.go.dev/unicode/utf8)
## How to install
Follow these steps to generate the `llgo` command (its usage is the same as the `go` command):

View File

@@ -9,7 +9,7 @@ func main() {
x := std.Max(py.Float(3.0), py.Float(9.0), py.Float(23.0), py.Float(100.0))
std.Print(x)
// y := py.List(3.0, 9.0, 23.0, 100.0)
// ymax := std.Max(std.Iter(y))
// std.Print(ymax)
list := py.List(3.0, 9.0, 23.0, 100.0)
y := std.Max(std.Iter(list))
std.Print(y)
}

15
cl/_testpy/max/in.go Normal file
View File

@@ -0,0 +1,15 @@
package main
import (
"github.com/goplus/llgo/py"
"github.com/goplus/llgo/py/std"
)
func main() {
x := std.Max(py.Float(3.0), py.Float(9.0), py.Float(23.0), py.Float(100.0))
std.Print(x)
list := py.List(3.0, 9.0, 23.0, 100.0)
y := std.Max(std.Iter(list))
std.Print(y)
}

80
cl/_testpy/max/out.ll Normal file
View File

@@ -0,0 +1,80 @@
; ModuleID = 'main'
source_filename = "main"
@"main.init$guard" = global ptr null
@__llgo_argc = global ptr null
@__llgo_argv = global ptr null
@__llgo_py.builtins.max = linkonce global ptr null
@__llgo_py.builtins.print = linkonce global ptr null
@__llgo_py.builtins.iter = linkonce global ptr null
@__llgo_py.builtins = external global ptr
@0 = private unnamed_addr constant [4 x i8] c"max\00", align 1
@1 = private unnamed_addr constant [6 x i8] c"print\00", align 1
@2 = private unnamed_addr constant [5 x i8] c"iter\00", align 1
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
call void @"github.com/goplus/llgo/py/std.init"()
%1 = load ptr, ptr @__llgo_py.builtins, align 8
call void (ptr, ...) @llgoLoadPyModSyms(ptr %1, ptr @0, ptr @__llgo_py.builtins.max, ptr @1, ptr @__llgo_py.builtins.print, ptr @2, ptr @__llgo_py.builtins.iter, ptr null)
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @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 3.000000e+00)
%3 = call ptr @PyFloat_FromDouble(double 9.000000e+00)
%4 = call ptr @PyFloat_FromDouble(double 2.300000e+01)
%5 = call ptr @PyFloat_FromDouble(double 1.000000e+02)
%6 = load ptr, ptr @__llgo_py.builtins.max, align 8
%7 = call ptr (ptr, ...) @PyObject_CallFunctionObjArgs(ptr %6, ptr %2, ptr %3, ptr %4, ptr %5, ptr null)
%8 = load ptr, ptr @__llgo_py.builtins.print, align 8
%9 = call ptr (ptr, ...) @PyObject_CallFunctionObjArgs(ptr %8, ptr %7, ptr null)
%10 = call ptr @PyList_New(i64 4)
%11 = call ptr @PyFloat_FromDouble(double 3.000000e+00)
%12 = call i32 @PyList_SetItem(ptr %10, i64 0, ptr %11)
%13 = call ptr @PyFloat_FromDouble(double 9.000000e+00)
%14 = call i32 @PyList_SetItem(ptr %10, i64 1, ptr %13)
%15 = call ptr @PyFloat_FromDouble(double 2.300000e+01)
%16 = call i32 @PyList_SetItem(ptr %10, i64 2, ptr %15)
%17 = call ptr @PyFloat_FromDouble(double 1.000000e+02)
%18 = call i32 @PyList_SetItem(ptr %10, i64 3, ptr %17)
%19 = load ptr, ptr @__llgo_py.builtins.iter, align 8
%20 = call ptr @PyObject_CallOneArg(ptr %19, ptr %10)
%21 = load ptr, ptr @__llgo_py.builtins.max, align 8
%22 = call ptr (ptr, ...) @PyObject_CallFunctionObjArgs(ptr %21, ptr %20, ptr null)
%23 = load ptr, ptr @__llgo_py.builtins.print, align 8
%24 = call ptr (ptr, ...) @PyObject_CallFunctionObjArgs(ptr %23, ptr %22, ptr null)
ret i32 0
}
declare void @"github.com/goplus/llgo/py/std.init"()
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @PyFloat_FromDouble(double)
declare ptr @PyObject_CallFunctionObjArgs(ptr, ...)
declare ptr @PyList_New(i64)
declare i32 @PyList_SetItem(ptr, i64, ptr)
declare ptr @PyObject_CallOneArg(ptr, ptr)
declare void @llgoLoadPyModSyms(ptr, ...)
declare void @Py_Initialize()

View File

@@ -301,8 +301,8 @@ func (p *context) funcOf(fn *ssa.Function) (aFn llssa.Function, pyFn llssa.PyObj
fnName := pysymPrefix + mod + "." + name
if pyFn = pkg.PyObjOf(fnName); pyFn == nil {
pyFn = pkg.PyNewFunc(fnName, fn.Signature, true)
return
}
return
}
ftype = ignoredFunc
case llgoInstr:
@@ -629,7 +629,7 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
case llgoUnreachable: // func unreachable()
b.Unreachable()
default:
panic("todo")
log.Panicln("unknown ftype:", ftype)
}
default:
fn := p.compileValue(b, cv)
@@ -812,7 +812,7 @@ func (p *context) compileInstr(b llssa.Builder, instr ssa.Instruction) {
}
func (p *context) compileFunction(v *ssa.Function) (goFn llssa.Function, pyFn llssa.PyObjRef, kind int) {
// v.Pkg == nil: means auto generated function?
// TODO(xsw) v.Pkg == nil: means auto generated function?
if v.Pkg == p.goPkg || v.Pkg == nil {
// function in this package
goFn, pyFn, kind = p.compileFuncDecl(p.pkg, v)