llgo/ssa: PyList

This commit is contained in:
xushiwei
2024-05-15 14:49:00 +08:00
parent c1bf895674
commit 59d68c6438
9 changed files with 100 additions and 35 deletions

24
cl/_testpy/matrix/in.go Normal file
View File

@@ -0,0 +1,24 @@
package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/py"
"github.com/goplus/llgo/py/numpy"
)
func main() {
a := py.List(
py.List(1.0, 2.0, 3.0),
py.List(4.0, 5.0, 6.0),
py.List(7.0, 8.0, 9.0),
)
b := py.List(
py.List(9.0, 8.0, 7.0),
py.List(6.0, 5.0, 4.0),
py.List(3.0, 2.0, 1.0),
)
x := numpy.Add(a, b)
c.Printf(c.Str("a = %s\n"), a.Str().CStr())
c.Printf(c.Str("a = %s\n"), b.Str().CStr())
c.Printf(c.Str("a+b = %s\n"), x.Str().CStr())
}

0
cl/_testpy/matrix/out.ll Normal file
View File

View File

@@ -313,6 +313,8 @@ func (p *context) funcOf(fn *ssa.Function) (aFn llssa.Function, pyFn llssa.PyObj
ftype = llgoAllocaCStr
case "stringData":
ftype = llgoStringData
case "pyList":
ftype = llgoPyList
case "unreachable":
ftype = llgoUnreachable
default:
@@ -519,6 +521,14 @@ func (p *context) stringData(b llssa.Builder, args []ssa.Value) (ret llssa.Expr)
panic("stringData(s string): invalid arguments")
}
func (p *context) pyList(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
vals := make([]llssa.Expr, len(args))
for i, arg := range args {
vals[i] = p.compileValue(b, arg)
}
return b.PyList(vals...)
}
func isPhi(i ssa.Instruction) bool {
_, ok := i.(*ssa.Phi)
return ok
@@ -615,6 +625,8 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
ret = p.allocaCStr(b, args)
case llgoStringData:
ret = p.stringData(b, args)
case llgoPyList:
ret = p.pyList(b, args)
case llgoUnreachable: // func unreachable()
b.Unreachable()
default:

View File

@@ -29,7 +29,7 @@ func testCompile(t *testing.T, src, expected string) {
}
func TestFromTestpy(t *testing.T) {
cltest.FromDir(t, "", "./_testpy", false)
cltest.FromDir(t, "matrix", "./_testpy", false)
}
func TestFromTestlibc(t *testing.T) {

View File

@@ -318,6 +318,7 @@ const (
llgoAdvance = llgoInstrBase + 4
llgoIndex = llgoInstrBase + 5
llgoStringData = llgoInstrBase + 6
llgoPyList = llgoInstrBase + 7
)
func (p *context) funcName(fn *ssa.Function, ignore bool) (*types.Package, string, int) {