Skip to content
Merged
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
11 changes: 11 additions & 0 deletions cl/_testpy/list/in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"github.com/goplus/lib/py"
"github.com/goplus/lib/py/std"
)

func main() {
x := py.List(true, false, 1, float32(2.1), 3.1, uint(4), 1+2i, complex64(3+4i), "hello", []byte("world"))
std.Print(x)
}
85 changes: 85 additions & 0 deletions cl/_testpy/list/out.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
; ModuleID = 'github.com/goplus/llgo/cl/_testpy/list'
source_filename = "github.com/goplus/llgo/cl/_testpy/list"

%"github.com/goplus/llgo/runtime/internal/runtime.Slice" = type { ptr, i64, i64 }
%"github.com/goplus/llgo/runtime/internal/runtime.String" = type { ptr, i64 }

@"github.com/goplus/llgo/cl/_testpy/list.init$guard" = global i1 false, align 1
@0 = private unnamed_addr constant [5 x i8] c"world", align 1
@1 = private unnamed_addr constant [5 x i8] c"hello", 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

define void @"github.com/goplus/llgo/cl/_testpy/list.init"() {
_llgo_0:
%0 = load i1, ptr @"github.com/goplus/llgo/cl/_testpy/list.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1

_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"github.com/goplus/llgo/cl/_testpy/list.init$guard", align 1
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)
br label %_llgo_2

_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}

define void @"github.com/goplus/llgo/cl/_testpy/list.main"() {
_llgo_0:
%0 = call %"github.com/goplus/llgo/runtime/internal/runtime.Slice" @"github.com/goplus/llgo/runtime/internal/runtime.StringToBytes"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @0, i64 5 })
%1 = call ptr @PyList_New(i64 10)
%2 = call ptr @PyBool_FromLong(i32 -1)
%3 = call i32 @PyList_SetItem(ptr %1, i64 0, ptr %2)
%4 = call ptr @PyBool_FromLong(i32 0)
%5 = call i32 @PyList_SetItem(ptr %1, i64 1, ptr %4)
%6 = call ptr @PyLong_FromLongLong(i64 1)
%7 = call i32 @PyList_SetItem(ptr %1, i64 2, ptr %6)
%8 = call ptr @PyFloat_FromDouble(double 0x4000CCCCC0000000)
%9 = call i32 @PyList_SetItem(ptr %1, i64 3, ptr %8)
%10 = call ptr @PyFloat_FromDouble(double 3.100000e+00)
%11 = call i32 @PyList_SetItem(ptr %1, i64 4, ptr %10)
%12 = call ptr @PyLong_FromUnsignedLongLong(i64 4)
%13 = call i32 @PyList_SetItem(ptr %1, i64 5, ptr %12)
%14 = call ptr @PyComplex_FromDoubles(double 1.000000e+00, double 2.000000e+00)
%15 = call i32 @PyList_SetItem(ptr %1, i64 6, ptr %14)
%16 = call ptr @PyComplex_FromDoubles(double 3.000000e+00, double 4.000000e+00)
%17 = call i32 @PyList_SetItem(ptr %1, i64 7, ptr %16)
%18 = call ptr @PyUnicode_FromStringAndSize(ptr @1, i64 5)
%19 = call i32 @PyList_SetItem(ptr %1, i64 8, ptr %18)
%20 = extractvalue %"github.com/goplus/llgo/runtime/internal/runtime.Slice" %0, 0
%21 = extractvalue %"github.com/goplus/llgo/runtime/internal/runtime.Slice" %0, 1
%22 = call ptr @PyByteArray_FromStringAndSize(ptr %20, i64 %21)
%23 = call i32 @PyList_SetItem(ptr %1, i64 9, ptr %22)
%24 = load ptr, ptr @__llgo_py.builtins.print, align 8
%25 = call ptr (ptr, ...) @PyObject_CallFunctionObjArgs(ptr %24, ptr %1, ptr null)
ret void
}

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

declare %"github.com/goplus/llgo/runtime/internal/runtime.Slice" @"github.com/goplus/llgo/runtime/internal/runtime.StringToBytes"(%"github.com/goplus/llgo/runtime/internal/runtime.String")

declare ptr @PyList_New(i64)

declare ptr @PyBool_FromLong(i32)

declare i32 @PyList_SetItem(ptr, i64, ptr)

declare ptr @PyLong_FromLongLong(i64)

declare ptr @PyFloat_FromDouble(double)

declare ptr @PyLong_FromUnsignedLongLong(i64)

declare ptr @PyComplex_FromDoubles(double, double)

declare ptr @PyUnicode_FromStringAndSize(ptr, i64)

declare ptr @PyByteArray_FromStringAndSize(ptr, i64)

declare ptr @PyObject_CallFunctionObjArgs(ptr, ...)

declare void @llgoLoadPyModSyms(ptr, ...)
7 changes: 7 additions & 0 deletions ssa/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ type aProgram struct {
getAttrStr *types.Signature
pyUniStr *types.Signature

pyBoolFromInt32 *types.Signature
pyLongFromInt64 *types.Signature
pyLongFromUint64 *types.Signature
pyUniFromStrAndSize *types.Signature
pyComplexFromDbs *types.Signature
pyBytesFromStrAndSize *types.Signature

mallocTy *types.Signature
freeTy *types.Signature
memsetInlineTy *types.Signature
Expand Down
152 changes: 147 additions & 5 deletions ssa/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ func (p Program) tyNewTuple() *types.Signature {
return p.pyNewTuple
}

// func(int32) *Object
func (p Program) tyBoolFromLong() *types.Signature {
if p.pyBoolFromInt32 == nil {
paramObjPtr := p.paramObjPtr()
paramFloat := types.NewParam(token.NoPos, nil, "", p.Int32().raw.Type)
params := types.NewTuple(paramFloat)
results := types.NewTuple(paramObjPtr)
p.pyBoolFromInt32 = types.NewSignatureType(nil, nil, nil, params, results, false)
}
return p.pyBoolFromInt32
}

// func(float64) *Object
func (p Program) tyFloatFromDouble() *types.Signature {
if p.floatFromDbl == nil {
Expand All @@ -198,6 +210,30 @@ func (p Program) tyFloatFromDouble() *types.Signature {
return p.floatFromDbl
}

// func(int64) *Object
func (p Program) tyLongFromInt64() *types.Signature {
if p.pyLongFromInt64 == nil {
paramObjPtr := p.paramObjPtr()
paramInt := types.NewParam(token.NoPos, nil, "", p.Int64().raw.Type)
params := types.NewTuple(paramInt)
results := types.NewTuple(paramObjPtr)
p.pyLongFromInt64 = types.NewSignatureType(nil, nil, nil, params, results, false)
}
return p.pyLongFromInt64
}

// func(uint64) *Object
func (p Program) tyLongFromUint64() *types.Signature {
if p.pyLongFromUint64 == nil {
paramObjPtr := p.paramObjPtr()
paramInt := types.NewParam(token.NoPos, nil, "", p.Uint64().raw.Type)
params := types.NewTuple(paramInt)
results := types.NewTuple(paramObjPtr)
p.pyLongFromUint64 = types.NewSignatureType(nil, nil, nil, params, results, false)
}
return p.pyLongFromUint64
}

// func(*Object, ...)
func (p Program) tyLoadPyModSyms() *types.Signature {
if p.loadPyModS == nil {
Expand All @@ -219,6 +255,38 @@ func (p Program) tyPyUnicodeFromString() *types.Signature {
return p.pyUniStr
}

// func(*char, int) *Object
func (p Program) tyPyUnicodeFromStringAndSize() *types.Signature {
if p.pyUniFromStrAndSize == nil {
charPtr := types.NewPointer(types.Typ[types.Int8])
params := types.NewTuple(types.NewParam(token.NoPos, nil, "", charPtr), types.NewParam(token.NoPos, nil, "", types.Typ[types.Int]))
results := types.NewTuple(p.paramObjPtr())
p.pyUniFromStrAndSize = types.NewSignatureType(nil, nil, nil, params, results, false)
}
return p.pyUniFromStrAndSize
}

// func(float64, float64) *Object
func (p Program) tyPyComplexFromDoubles() *types.Signature {
if p.pyComplexFromDbs == nil {
params := types.NewTuple(types.NewParam(token.NoPos, nil, "", types.Typ[types.Float64]), types.NewParam(token.NoPos, nil, "", types.Typ[types.Float64]))
results := types.NewTuple(p.paramObjPtr())
p.pyComplexFromDbs = types.NewSignatureType(nil, nil, nil, params, results, false)
}
return p.pyComplexFromDbs
}

// func(*char, int) *Object
func (p Program) tyPyByteArrayFromStringAndSize() *types.Signature {
if p.pyBytesFromStrAndSize == nil {
charPtr := types.NewPointer(types.Typ[types.Int8])
params := types.NewTuple(types.NewParam(token.NoPos, nil, "", charPtr), types.NewParam(token.NoPos, nil, "", types.Typ[types.Int]))
results := types.NewTuple(p.paramObjPtr())
p.pyBytesFromStrAndSize = types.NewSignatureType(nil, nil, nil, params, results, false)
}
return p.pyBytesFromStrAndSize
}

// func(*Objecg, *char) *Object
func (p Program) tyGetAttrString() *types.Signature {
if p.getAttrStr == nil {
Expand Down Expand Up @@ -356,17 +424,54 @@ func (b Builder) PyTuple(args ...Expr) (ret Expr) {

// PyVal(v any) *Object
func (b Builder) PyVal(v Expr) (ret Expr) {
switch t := v.raw.Type.(type) {
switch t := v.raw.Type.Underlying().(type) {
case *types.Basic:
switch t.Kind() {
case types.Bool:
return b.PyBool(v)
case types.Float32:
typ := b.Prog.Float64()
return b.PyFloat(Expr{castFloat(b, v.impl, typ), typ})
case types.Float64:
return b.PyFloat(v)
default:
panic("PyVal: todo")
case types.Int, types.Int8, types.Int16, types.Int32, types.Int64:
typ := b.Prog.Int64().ll
if b.Prog.td.TypeAllocSize(v.ll) < b.Prog.td.TypeAllocSize(typ) {
v.impl = llvm.CreateSExt(b.impl, v.impl, typ)
v.ll = typ
}
return b.PyInt64(v)
case types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64, types.Uintptr:
typ := b.Prog.Uint64().ll
if b.Prog.td.TypeAllocSize(v.ll) < b.Prog.td.TypeAllocSize(typ) {
v.impl = llvm.CreateZExt(b.impl, v.impl, typ)
v.ll = typ
}
return b.PyUint64(v)
case types.String:
return b.PyStrExpr(v)
case types.Complex64:
return b.PyComplex64(v)
case types.Complex128:
return b.PyComplex128(v)
}
case *types.Slice:
if elem, ok := t.Elem().(*types.Basic); ok && elem.Kind() == types.Byte {
return b.PyByteArray(v)
}
case *types.Pointer:
if v.Type == b.Prog.PyObjectPtr() {
return v
}
default:
return v
}
panic("PyVal: todo " + v.raw.Type.String())
}

// PyBool(bVal bool) *Object
func (b Builder) PyBool(bVal Expr) (ret Expr) {
fn := b.Pkg.pyFunc("PyBool_FromLong", b.Prog.tyBoolFromLong())
typ := b.Prog.Int32()
return b.Call(fn, Expr{castInt(b, bVal.impl, typ), typ})
}

// PyFloat(fltVal float64) *Object
Expand All @@ -375,12 +480,49 @@ func (b Builder) PyFloat(fltVal Expr) (ret Expr) {
return b.Call(fn, fltVal)
}

// PyInt64(val int64) *Object
func (b Builder) PyInt64(intVal Expr) (ret Expr) {
fn := b.Pkg.pyFunc("PyLong_FromLongLong", b.Prog.tyLongFromInt64())
return b.Call(fn, intVal)
}

// PyUint64(val uint64) *Object
func (b Builder) PyUint64(uintVal Expr) (ret Expr) {
fn := b.Pkg.pyFunc("PyLong_FromUnsignedLongLong", b.Prog.tyLongFromUint64())
return b.Call(fn, uintVal)
}

// PyStr returns a py-style string constant expression.
func (b Builder) PyStr(v string) Expr {
fn := b.Pkg.pyFunc("PyUnicode_FromString", b.Prog.tyPyUnicodeFromString())
return b.Call(fn, b.CStr(v))
}

// PyStrExpr(str string) *Object
func (b Builder) PyStrExpr(v Expr) Expr {
fn := b.Pkg.pyFunc("PyUnicode_FromStringAndSize", b.Prog.tyPyUnicodeFromStringAndSize())
return b.Call(fn, b.StringData(v), b.StringLen(v))
}

// PyComplex128(val complex128) *Object
func (b Builder) PyComplex128(v Expr) Expr {
fn := b.Pkg.pyFunc("PyComplex_FromDoubles", b.Prog.tyPyComplexFromDoubles())
return b.Call(fn, b.getField(v, 0), b.getField(v, 1))
}

// PyComplex64(val complex64) *Object
func (b Builder) PyComplex64(v Expr) Expr {
fn := b.Pkg.pyFunc("PyComplex_FromDoubles", b.Prog.tyPyComplexFromDoubles())
typ := b.Prog.Float64()
return b.Call(fn, Expr{castFloat(b, b.getField(v, 0).impl, typ), typ}, Expr{castFloat(b, b.getField(v, 1).impl, typ), typ})
}

// PyByteArray(val []byte) *Object
func (b Builder) PyByteArray(v Expr) Expr {
fn := b.Pkg.pyFunc("PyByteArray_FromStringAndSize", b.Prog.tyPyByteArrayFromStringAndSize())
return b.Call(fn, b.SliceData(v), b.SliceLen(v))
}

// -----------------------------------------------------------------------------

type aPyGlobal struct {
Expand Down
Loading