llgo/ssa: StringData/StringLen
This commit is contained in:
2
c/c.go
2
c/c.go
@@ -66,7 +66,7 @@ func Memset(s Pointer, c Int, n uintptr) Pointer
|
|||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
//go:linkname GoStringData github.com/goplus/llgo/internal/runtime.StringData
|
//go:linkname GoStringData llgo.stringData
|
||||||
func GoStringData(string) *Char
|
func GoStringData(string) *Char
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -311,7 +311,7 @@ _llgo_0:
|
|||||||
_llgo_1: ; preds = %_llgo_3
|
_llgo_1: ; preds = %_llgo_3
|
||||||
%2 = urem i64 %14, 16
|
%2 = urem i64 %14, 16
|
||||||
%3 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @5, i64 16)
|
%3 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @5, i64 16)
|
||||||
%4 = call ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String" %3)
|
%4 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %3, 0
|
||||||
%5 = getelementptr inbounds i8, ptr %4, i64 %2
|
%5 = getelementptr inbounds i8, ptr %4, i64 %2
|
||||||
%6 = load i8, ptr %5, align 1
|
%6 = load i8, ptr %5, align 1
|
||||||
%7 = getelementptr inbounds i8, ptr %1, i64 %15
|
%7 = getelementptr inbounds i8, ptr %1, i64 %15
|
||||||
@@ -601,8 +601,6 @@ declare %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llg
|
|||||||
|
|
||||||
declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice3"(ptr, i64, i64, i64, i64, i64)
|
declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice3"(ptr, i64, i64, i64, i64, i64)
|
||||||
|
|
||||||
declare ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String")
|
|
||||||
|
|
||||||
declare { i64, i1 } @"github.com/goplus/llgo/internal/runtime.CheckI2Int"(%"github.com/goplus/llgo/internal/runtime.iface", ptr)
|
declare { i64, i1 } @"github.com/goplus/llgo/internal/runtime.CheckI2Int"(%"github.com/goplus/llgo/internal/runtime.iface", ptr)
|
||||||
|
|
||||||
declare { float, i1 } @"github.com/goplus/llgo/internal/runtime.CheckI2Float32"(%"github.com/goplus/llgo/internal/runtime.iface", ptr)
|
declare { float, i1 } @"github.com/goplus/llgo/internal/runtime.CheckI2Float32"(%"github.com/goplus/llgo/internal/runtime.iface", ptr)
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ _llgo_0:
|
|||||||
call void @"github.com/goplus/llgo/internal/runtime.init"()
|
call void @"github.com/goplus/llgo/internal/runtime.init"()
|
||||||
call void @main.init()
|
call void @main.init()
|
||||||
%2 = call %"github.com/goplus/llgo/internal/runtime.String" @main.hello()
|
%2 = call %"github.com/goplus/llgo/internal/runtime.String" @main.hello()
|
||||||
%3 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %2)
|
%3 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %2, 1
|
||||||
%4 = add i64 %3, 1
|
%4 = add i64 %3, 1
|
||||||
%5 = alloca i8, i64 %4, align 1
|
%5 = alloca i8, i64 %4, align 1
|
||||||
%6 = call ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr %5, %"github.com/goplus/llgo/internal/runtime.String" %2)
|
%6 = call ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr %5, %"github.com/goplus/llgo/internal/runtime.String" %2)
|
||||||
@@ -46,8 +46,6 @@ declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/ll
|
|||||||
|
|
||||||
declare void @"github.com/goplus/llgo/internal/runtime.init"()
|
declare void @"github.com/goplus/llgo/internal/runtime.init"()
|
||||||
|
|
||||||
declare i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String")
|
|
||||||
|
|
||||||
declare ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr, %"github.com/goplus/llgo/internal/runtime.String")
|
declare ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr, %"github.com/goplus/llgo/internal/runtime.String")
|
||||||
|
|
||||||
declare i32 @printf(ptr, ...)
|
declare i32 @printf(ptr, ...)
|
||||||
|
|||||||
@@ -131,14 +131,14 @@ _llgo_0:
|
|||||||
%60 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @0, i64 5)
|
%60 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @0, i64 5)
|
||||||
call void @main.string_len(%"github.com/goplus/llgo/internal/runtime.String" %60)
|
call void @main.string_len(%"github.com/goplus/llgo/internal/runtime.String" %60)
|
||||||
%61 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @1, i64 5)
|
%61 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @1, i64 5)
|
||||||
%62 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %61)
|
%62 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %61, 1
|
||||||
%63 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewStringSlice"(%"github.com/goplus/llgo/internal/runtime.String" %61, i64 1, i64 %62)
|
%63 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewStringSlice"(%"github.com/goplus/llgo/internal/runtime.String" %61, i64 1, i64 %62)
|
||||||
call void @main.string_len(%"github.com/goplus/llgo/internal/runtime.String" %63)
|
call void @main.string_len(%"github.com/goplus/llgo/internal/runtime.String" %63)
|
||||||
%64 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @2, i64 5)
|
%64 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @2, i64 5)
|
||||||
%65 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewStringSlice"(%"github.com/goplus/llgo/internal/runtime.String" %64, i64 1, i64 2)
|
%65 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewStringSlice"(%"github.com/goplus/llgo/internal/runtime.String" %64, i64 1, i64 2)
|
||||||
call void @main.string_len(%"github.com/goplus/llgo/internal/runtime.String" %65)
|
call void @main.string_len(%"github.com/goplus/llgo/internal/runtime.String" %65)
|
||||||
%66 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @3, i64 5)
|
%66 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @3, i64 5)
|
||||||
%67 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %66)
|
%67 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %66, 1
|
||||||
%68 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewStringSlice"(%"github.com/goplus/llgo/internal/runtime.String" %66, i64 5, i64 %67)
|
%68 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewStringSlice"(%"github.com/goplus/llgo/internal/runtime.String" %66, i64 5, i64 %67)
|
||||||
call void @main.string_len(%"github.com/goplus/llgo/internal/runtime.String" %68)
|
call void @main.string_len(%"github.com/goplus/llgo/internal/runtime.String" %68)
|
||||||
ret void
|
ret void
|
||||||
@@ -152,7 +152,7 @@ _llgo_0:
|
|||||||
|
|
||||||
define void @main.string_len(%"github.com/goplus/llgo/internal/runtime.String" %0) {
|
define void @main.string_len(%"github.com/goplus/llgo/internal/runtime.String" %0) {
|
||||||
_llgo_0:
|
_llgo_0:
|
||||||
%1 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %0)
|
%1 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %0, 1
|
||||||
call void @main.out(i64 %1)
|
call void @main.out(i64 %1)
|
||||||
ret void
|
ret void
|
||||||
}
|
}
|
||||||
@@ -171,8 +171,6 @@ declare ptr @"github.com/goplus/llgo/internal/runtime.SliceData"(%"github.com/go
|
|||||||
|
|
||||||
declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr, i64)
|
declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr, i64)
|
||||||
|
|
||||||
declare i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String")
|
|
||||||
|
|
||||||
declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewStringSlice"(%"github.com/goplus/llgo/internal/runtime.String", i64, i64)
|
declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewStringSlice"(%"github.com/goplus/llgo/internal/runtime.String", i64, i64)
|
||||||
|
|
||||||
declare i32 @printf(ptr, ...)
|
declare i32 @printf(ptr, ...)
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ _llgo_0:
|
|||||||
%9 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice3"(ptr %2, i64 16, i64 3, i64 0, i64 3, i64 3)
|
%9 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice3"(ptr %2, i64 16, i64 3, i64 0, i64 3, i64 3)
|
||||||
%10 = call %"github.com/goplus/llgo/internal/runtime.String" @main.concat(%"github.com/goplus/llgo/internal/runtime.Slice" %9)
|
%10 = call %"github.com/goplus/llgo/internal/runtime.String" @main.concat(%"github.com/goplus/llgo/internal/runtime.Slice" %9)
|
||||||
%11 = load ptr, ptr @__stderrp, align 8
|
%11 = load ptr, ptr @__stderrp, align 8
|
||||||
%12 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %10)
|
%12 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %10, 1
|
||||||
%13 = add i64 %12, 1
|
%13 = add i64 %12, 1
|
||||||
%14 = alloca i8, i64 %13, align 1
|
%14 = alloca i8, i64 %13, align 1
|
||||||
%15 = call ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr %14, %"github.com/goplus/llgo/internal/runtime.String" %10)
|
%15 = call ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr %14, %"github.com/goplus/llgo/internal/runtime.String" %10)
|
||||||
@@ -103,8 +103,6 @@ declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
|
|||||||
|
|
||||||
declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice3"(ptr, i64, i64, i64, i64, i64)
|
declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice3"(ptr, i64, i64, i64, i64, i64)
|
||||||
|
|
||||||
declare i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String")
|
|
||||||
|
|
||||||
declare ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr, %"github.com/goplus/llgo/internal/runtime.String")
|
declare ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr, %"github.com/goplus/llgo/internal/runtime.String")
|
||||||
|
|
||||||
declare i32 @fprintf(ptr, ptr, ...)
|
declare i32 @fprintf(ptr, ptr, ...)
|
||||||
|
|||||||
@@ -119,12 +119,12 @@ _llgo_0:
|
|||||||
%61 = load i64, ptr %60, align 4
|
%61 = load i64, ptr %60, align 4
|
||||||
%62 = call i32 (ptr, ...) @printf(ptr @3, i64 %61)
|
%62 = call i32 (ptr, ...) @printf(ptr @3, i64 %61)
|
||||||
%63 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @5, i64 6)
|
%63 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @5, i64 6)
|
||||||
%64 = call ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String" %63)
|
%64 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %63, 0
|
||||||
%65 = getelementptr inbounds i8, ptr %64, i64 2
|
%65 = getelementptr inbounds i8, ptr %64, i64 2
|
||||||
%66 = load i8, ptr %65, align 1
|
%66 = load i8, ptr %65, align 1
|
||||||
%67 = call i32 (ptr, ...) @printf(ptr @4, i8 %66)
|
%67 = call i32 (ptr, ...) @printf(ptr @4, i8 %66)
|
||||||
%68 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @7, i64 6)
|
%68 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @7, i64 6)
|
||||||
%69 = call ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String" %68)
|
%69 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %68, 0
|
||||||
%70 = getelementptr inbounds i8, ptr %69, i64 1
|
%70 = getelementptr inbounds i8, ptr %69, i64 1
|
||||||
%71 = load i8, ptr %70, align 1
|
%71 = load i8, ptr %70, align 1
|
||||||
%72 = call i32 (ptr, ...) @printf(ptr @6, i8 %71)
|
%72 = call i32 (ptr, ...) @printf(ptr @6, i8 %71)
|
||||||
@@ -138,5 +138,3 @@ declare ptr @"github.com/goplus/llgo/internal/runtime.Zeroinit"(ptr, i64)
|
|||||||
declare i32 @printf(ptr, ...)
|
declare i32 @printf(ptr, ...)
|
||||||
|
|
||||||
declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr, i64)
|
declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr, i64)
|
||||||
|
|
||||||
declare ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String")
|
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ func TestCompileEx(t *testing.T, src any, fname, expected string) {
|
|||||||
t.Fatal("cl.NewPackage failed:", err)
|
t.Fatal("cl.NewPackage failed:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if prog.NeedPyInit() { // call PyInit if needed
|
if prog.NeedPyInit { // call PyInit if needed
|
||||||
ret.PyInit()
|
ret.PyInit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -309,6 +309,8 @@ func (p *context) funcOf(fn *ssa.Function) (aFn llssa.Function, pyFn llssa.PyFun
|
|||||||
ftype = llgoAlloca
|
ftype = llgoAlloca
|
||||||
case "allocaCStr":
|
case "allocaCStr":
|
||||||
ftype = llgoAllocaCStr
|
ftype = llgoAllocaCStr
|
||||||
|
case "stringData":
|
||||||
|
ftype = llgoStringData
|
||||||
case "unreachable":
|
case "unreachable":
|
||||||
ftype = llgoUnreachable
|
ftype = llgoUnreachable
|
||||||
case "bitCastTo64F":
|
case "bitCastTo64F":
|
||||||
@@ -479,6 +481,15 @@ func (p *context) allocaCStr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr)
|
|||||||
panic("allocaCStr(s string): invalid arguments")
|
panic("allocaCStr(s string): invalid arguments")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func stringData(s string) *int8
|
||||||
|
func (p *context) stringData(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
|
||||||
|
if len(args) == 1 {
|
||||||
|
s := p.compileValue(b, args[0])
|
||||||
|
return b.StringData(s)
|
||||||
|
}
|
||||||
|
panic("stringData(s string): invalid arguments")
|
||||||
|
}
|
||||||
|
|
||||||
func isPhi(i ssa.Instruction) bool {
|
func isPhi(i ssa.Instruction) bool {
|
||||||
_, ok := i.(*ssa.Phi)
|
_, ok := i.(*ssa.Phi)
|
||||||
return ok
|
return ok
|
||||||
@@ -573,6 +584,8 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
|
|||||||
ret = p.alloca(b, args)
|
ret = p.alloca(b, args)
|
||||||
case llgoAllocaCStr:
|
case llgoAllocaCStr:
|
||||||
ret = p.allocaCStr(b, args)
|
ret = p.allocaCStr(b, args)
|
||||||
|
case llgoStringData:
|
||||||
|
ret = p.stringData(b, args)
|
||||||
case llgoUnreachable: // func unreachable()
|
case llgoUnreachable: // func unreachable()
|
||||||
b.Unreachable()
|
b.Unreachable()
|
||||||
case llgoBitCastTo32F:
|
case llgoBitCastTo32F:
|
||||||
|
|||||||
@@ -319,6 +319,7 @@ const (
|
|||||||
llgoIndex = llgoInstrBase + 5
|
llgoIndex = llgoInstrBase + 5
|
||||||
llgoBitCastTo32F = llgoInstrBase + 6
|
llgoBitCastTo32F = llgoInstrBase + 6
|
||||||
llgoBitCastTo64F = llgoInstrBase + 7
|
llgoBitCastTo64F = llgoInstrBase + 7
|
||||||
|
llgoStringData = llgoInstrBase + 8
|
||||||
)
|
)
|
||||||
|
|
||||||
func (p *context) funcName(fn *ssa.Function, ignore bool) (*types.Package, string, int) {
|
func (p *context) funcName(fn *ssa.Function, ignore bool) (*types.Package, string, int) {
|
||||||
|
|||||||
@@ -217,7 +217,7 @@ func buildAllPkgs(prog llssa.Program, initial []*packages.Package, mode Mode, ve
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
buildPkg(prog, aPkg, mode, verbose)
|
buildPkg(prog, aPkg, mode, verbose)
|
||||||
setNeedRuntimeOrPyInit(pkg, prog.NeedRuntime(), prog.NeedPyInit())
|
setNeedRuntimeOrPyInit(pkg, prog.NeedRuntime, prog.NeedPyInit)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ func Gen(pkgPath, inFile string, src any) string {
|
|||||||
ret, err := cl.NewPackage(prog, ssaPkg, files)
|
ret, err := cl.NewPackage(prog, ssaPkg, files)
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
if prog.NeedPyInit() { // call PyInit if needed
|
if prog.NeedPyInit { // call PyInit if needed
|
||||||
ret.PyInit()
|
ret.PyInit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ func GenFrom(fileOrPkg string) string {
|
|||||||
ret, err := cl.NewPackage(prog, ssaPkg, pkg.Syntax)
|
ret, err := cl.NewPackage(prog, ssaPkg, pkg.Syntax)
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
if prog.NeedPyInit() { // call PyInit if needed
|
if prog.NeedPyInit { // call PyInit if needed
|
||||||
ret.PyInit()
|
ret.PyInit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -45,16 +45,6 @@ func NewString(data unsafe.Pointer, len int) String {
|
|||||||
return String{data, len}
|
return String{data, len}
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringLen returns the length of a string.
|
|
||||||
func StringLen(s String) int {
|
|
||||||
return s.len
|
|
||||||
}
|
|
||||||
|
|
||||||
// StringData returns the data pointer of a string.
|
|
||||||
func StringData(s String) unsafe.Pointer {
|
|
||||||
return s.data
|
|
||||||
}
|
|
||||||
|
|
||||||
// StringCat concatenates two strings.
|
// StringCat concatenates two strings.
|
||||||
func StringCat(a, b String) String {
|
func StringCat(a, b String) String {
|
||||||
n := a.len + b.len
|
n := a.len + b.len
|
||||||
|
|||||||
14
py/bytes.go
14
py/bytes.go
@@ -26,17 +26,17 @@ import (
|
|||||||
|
|
||||||
// String returns a new bytes object from a C string.
|
// String returns a new bytes object from a C string.
|
||||||
//
|
//
|
||||||
//go:linkname StringFrom C.PyBytes_FromString
|
//go:linkname FromCStr C.PyBytes_FromString
|
||||||
func StringFrom(s *c.Char) *Object
|
func FromCStr(s *c.Char) *Object
|
||||||
|
|
||||||
|
// FromString returns a new bytes object from a Go string.
|
||||||
|
func FromString(s string) *Object {
|
||||||
|
return stringFromStringAndSize(c.GoStringData(s), uintptr(len(s)))
|
||||||
|
}
|
||||||
|
|
||||||
//go:linkname stringFromStringAndSize C.PyBytes_FromStringAndSize
|
//go:linkname stringFromStringAndSize C.PyBytes_FromStringAndSize
|
||||||
func stringFromStringAndSize(s *c.Char, size uintptr) *Object
|
func stringFromStringAndSize(s *c.Char, size uintptr) *Object
|
||||||
|
|
||||||
// String returns a new bytes object from a Go string.
|
|
||||||
func String(s string) *Object {
|
|
||||||
return stringFromStringAndSize(c.GoStringData(s), uintptr(len(s)))
|
|
||||||
}
|
|
||||||
|
|
||||||
// CStr returns the content of a bytes object as a C string.
|
// CStr returns the content of a bytes object as a C string.
|
||||||
//
|
//
|
||||||
// llgo:link (*Object).CStr C.PyBytes_AsString
|
// llgo:link (*Object).CStr C.PyBytes_AsString
|
||||||
|
|||||||
Binary file not shown.
@@ -299,7 +299,7 @@ func (p Package) NewPyFunc(name string, sig *types.Signature, doInit bool) PyFun
|
|||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
prog := p.Prog
|
prog := p.Prog
|
||||||
prog.needPyInit = true
|
prog.NeedPyInit = true
|
||||||
obj := p.NewVar(name, prog.PyObjectPtrPtr().RawType(), InC)
|
obj := p.NewVar(name, prog.PyObjectPtrPtr().RawType(), InC)
|
||||||
if doInit {
|
if doInit {
|
||||||
obj.Init(prog.Null(obj.Type))
|
obj.Init(prog.Null(obj.Type))
|
||||||
|
|||||||
29
ssa/expr.go
29
ssa/expr.go
@@ -598,6 +598,26 @@ func (b Builder) getField(x Expr, idx int) Expr {
|
|||||||
return Expr{fld, tfld}
|
return Expr{fld, tfld}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StringData returns the data pointer of a string.
|
||||||
|
func (b Builder) StringData(x Expr) Expr {
|
||||||
|
if debugInstr {
|
||||||
|
log.Printf("StringData %v\n", x.impl)
|
||||||
|
}
|
||||||
|
prog := b.Prog
|
||||||
|
ptr := llvm.CreateExtractValue(b.impl, x.impl, 0)
|
||||||
|
return Expr{ptr, prog.CStr()}
|
||||||
|
}
|
||||||
|
|
||||||
|
// StringLen returns the length of a string.
|
||||||
|
func (b Builder) StringLen(x Expr) Expr {
|
||||||
|
if debugInstr {
|
||||||
|
log.Printf("StringLen %v\n", x.impl)
|
||||||
|
}
|
||||||
|
prog := b.Prog
|
||||||
|
ptr := llvm.CreateExtractValue(b.impl, x.impl, 1)
|
||||||
|
return Expr{ptr, prog.Int()}
|
||||||
|
}
|
||||||
|
|
||||||
// The IndexAddr instruction yields the address of the element at
|
// The IndexAddr instruction yields the address of the element at
|
||||||
// index `idx` of collection `x`. `idx` is an integer expression.
|
// index `idx` of collection `x`. `idx` is an integer expression.
|
||||||
//
|
//
|
||||||
@@ -649,8 +669,7 @@ func (b Builder) Index(x, idx Expr, addr func(Expr) Expr) Expr {
|
|||||||
panic(fmt.Errorf("invalid operation: cannot index %v", t))
|
panic(fmt.Errorf("invalid operation: cannot index %v", t))
|
||||||
}
|
}
|
||||||
telem = prog.rawType(types.Typ[types.Byte])
|
telem = prog.rawType(types.Typ[types.Byte])
|
||||||
pkg := b.Func.Pkg
|
ptr = b.StringData(x)
|
||||||
ptr = b.InlineCall(pkg.rtFunc("StringData"), x)
|
|
||||||
case *types.Array:
|
case *types.Array:
|
||||||
telem = prog.Index(x.Type)
|
telem = prog.Index(x.Type)
|
||||||
if addr != nil {
|
if addr != nil {
|
||||||
@@ -717,7 +736,7 @@ func (b Builder) Slice(x, low, high, max Expr) (ret Expr) {
|
|||||||
panic(fmt.Errorf("invalid operation: cannot slice %v", t))
|
panic(fmt.Errorf("invalid operation: cannot slice %v", t))
|
||||||
}
|
}
|
||||||
if high.IsNil() {
|
if high.IsNil() {
|
||||||
high = b.InlineCall(pkg.rtFunc("StringLen"), x)
|
high = b.StringLen(x)
|
||||||
}
|
}
|
||||||
ret.Type = x.Type
|
ret.Type = x.Type
|
||||||
ret.impl = b.InlineCall(pkg.rtFunc("NewStringSlice"), x, low, high).impl
|
ret.impl = b.InlineCall(pkg.rtFunc("NewStringSlice"), x, low, high).impl
|
||||||
@@ -871,7 +890,7 @@ func (b Builder) AllocaCStr(gostr Expr) (ret Expr) {
|
|||||||
log.Printf("AllocaCStr %v\n", gostr.impl)
|
log.Printf("AllocaCStr %v\n", gostr.impl)
|
||||||
}
|
}
|
||||||
pkg := b.Func.Pkg
|
pkg := b.Func.Pkg
|
||||||
n := b.InlineCall(pkg.rtFunc("StringLen"), gostr)
|
n := b.StringLen(gostr)
|
||||||
n1 := b.BinOp(token.ADD, n, b.Prog.Val(1))
|
n1 := b.BinOp(token.ADD, n, b.Prog.Val(1))
|
||||||
cstr := b.Alloca(n1)
|
cstr := b.Alloca(n1)
|
||||||
return b.InlineCall(pkg.rtFunc("CStrCopy"), cstr, gostr)
|
return b.InlineCall(pkg.rtFunc("CStrCopy"), cstr, gostr)
|
||||||
@@ -1250,7 +1269,7 @@ func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) {
|
|||||||
return b.InlineCall(b.Func.Pkg.rtFunc("SliceLen"), arg)
|
return b.InlineCall(b.Func.Pkg.rtFunc("SliceLen"), arg)
|
||||||
case *types.Basic:
|
case *types.Basic:
|
||||||
if t.Kind() == types.String {
|
if t.Kind() == types.String {
|
||||||
return b.InlineCall(b.Func.Pkg.rtFunc("StringLen"), arg)
|
return b.StringLen(arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -140,8 +140,8 @@ type aProgram struct {
|
|||||||
callNoArgs *types.Signature
|
callNoArgs *types.Signature
|
||||||
callOneArg *types.Signature
|
callOneArg *types.Signature
|
||||||
|
|
||||||
needRuntime bool
|
NeedRuntime bool
|
||||||
needPyInit bool
|
NeedPyInit bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// A Program presents a program.
|
// A Program presents a program.
|
||||||
@@ -189,21 +189,11 @@ func (p Program) SetRuntime(runtime any) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NeedRuntime returns if the current package needs runtime.
|
|
||||||
func (p Program) NeedRuntime() bool {
|
|
||||||
return p.needRuntime
|
|
||||||
}
|
|
||||||
|
|
||||||
// NeedPyInit returns if the current package needs Python initialization.
|
|
||||||
func (p Program) NeedPyInit() bool {
|
|
||||||
return p.needPyInit
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p Program) runtime() *types.Package {
|
func (p Program) runtime() *types.Package {
|
||||||
if p.rt == nil {
|
if p.rt == nil {
|
||||||
p.rt = p.rtget()
|
p.rt = p.rtget()
|
||||||
}
|
}
|
||||||
p.needRuntime = true
|
p.NeedRuntime = true
|
||||||
return p.rt
|
return p.rt
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,7 +258,7 @@ func (p Program) NewPackage(name, pkgPath string) Package {
|
|||||||
stubs := make(map[string]Function)
|
stubs := make(map[string]Function)
|
||||||
pyfns := make(map[string]PyFunction)
|
pyfns := make(map[string]PyFunction)
|
||||||
pymods := make(map[string]Global)
|
pymods := make(map[string]Global)
|
||||||
p.needRuntime = false
|
p.NeedRuntime = false
|
||||||
// Don't need reset p.needPyInit here
|
// Don't need reset p.needPyInit here
|
||||||
// p.needPyInit = false
|
// p.needPyInit = false
|
||||||
return &aPackage{mod, gbls, fns, stubs, pyfns, pymods, p}
|
return &aPackage{mod, gbls, fns, stubs, pyfns, pymods, p}
|
||||||
@@ -397,7 +387,7 @@ func (p Package) rtFunc(fnName string) Expr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p Package) pyFunc(fullName string, sig *types.Signature) Expr {
|
func (p Package) pyFunc(fullName string, sig *types.Signature) Expr {
|
||||||
p.Prog.needPyInit = true
|
p.Prog.NeedPyInit = true
|
||||||
return p.NewFunc(fullName, sig, InC).Expr
|
return p.NewFunc(fullName, sig, InC).Expr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -199,7 +199,7 @@ source_filename = "foo/bar"
|
|||||||
|
|
||||||
@a = external global {}
|
@a = external global {}
|
||||||
`)
|
`)
|
||||||
if prog.NeedRuntime() {
|
if prog.NeedRuntime {
|
||||||
t.Fatal("NeedRuntime?")
|
t.Fatal("NeedRuntime?")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
@@ -35,8 +35,8 @@ _llgo_0:
|
|||||||
define { ptr, i32 } @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).Prepare"(ptr %0, %"github.com/goplus/llgo/internal/runtime.String" %1, ptr %2) {
|
define { ptr, i32 } @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).Prepare"(ptr %0, %"github.com/goplus/llgo/internal/runtime.String" %1, ptr %2) {
|
||||||
_llgo_0:
|
_llgo_0:
|
||||||
%3 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
|
%3 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
|
||||||
%4 = call ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String" %1)
|
%4 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %1, 0
|
||||||
%5 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %1)
|
%5 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %1, 1
|
||||||
%6 = trunc i64 %5 to i32
|
%6 = trunc i64 %5 to i32
|
||||||
%7 = call i32 @sqlite3_prepare(ptr %0, ptr %4, i32 %6, ptr %3, ptr %2)
|
%7 = call i32 @sqlite3_prepare(ptr %0, ptr %4, i32 %6, ptr %3, ptr %2)
|
||||||
%8 = load ptr, ptr %3, align 8
|
%8 = load ptr, ptr %3, align 8
|
||||||
@@ -48,8 +48,8 @@ _llgo_0:
|
|||||||
define { ptr, i32 } @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).PrepareV2"(ptr %0, %"github.com/goplus/llgo/internal/runtime.String" %1, ptr %2) {
|
define { ptr, i32 } @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).PrepareV2"(ptr %0, %"github.com/goplus/llgo/internal/runtime.String" %1, ptr %2) {
|
||||||
_llgo_0:
|
_llgo_0:
|
||||||
%3 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
|
%3 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
|
||||||
%4 = call ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String" %1)
|
%4 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %1, 0
|
||||||
%5 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %1)
|
%5 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %1, 1
|
||||||
%6 = trunc i64 %5 to i32
|
%6 = trunc i64 %5 to i32
|
||||||
%7 = call i32 @sqlite3_prepare_v2(ptr %0, ptr %4, i32 %6, ptr %3, ptr %2)
|
%7 = call i32 @sqlite3_prepare_v2(ptr %0, ptr %4, i32 %6, ptr %3, ptr %2)
|
||||||
%8 = load ptr, ptr %3, align 8
|
%8 = load ptr, ptr %3, align 8
|
||||||
@@ -61,8 +61,8 @@ _llgo_0:
|
|||||||
define { ptr, i32 } @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).PrepareV3"(ptr %0, %"github.com/goplus/llgo/internal/runtime.String" %1, i32 %2, ptr %3) {
|
define { ptr, i32 } @"(*github.com/goplus/llgo/x/sqlite.Sqlite3).PrepareV3"(ptr %0, %"github.com/goplus/llgo/internal/runtime.String" %1, i32 %2, ptr %3) {
|
||||||
_llgo_0:
|
_llgo_0:
|
||||||
%4 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
|
%4 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
|
||||||
%5 = call ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String" %1)
|
%5 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %1, 0
|
||||||
%6 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %1)
|
%6 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %1, 1
|
||||||
%7 = trunc i64 %6 to i32
|
%7 = trunc i64 %6 to i32
|
||||||
%8 = call i32 @sqlite3_prepare_v3(ptr %0, ptr %5, i32 %7, i32 %2, ptr %4, ptr %3)
|
%8 = call i32 @sqlite3_prepare_v3(ptr %0, ptr %5, i32 %7, i32 %2, ptr %4, ptr %3)
|
||||||
%9 = load ptr, ptr %4, align 8
|
%9 = load ptr, ptr %4, align 8
|
||||||
@@ -92,10 +92,6 @@ declare i32 @sqlite3_open(ptr, ptr)
|
|||||||
|
|
||||||
declare i32 @sqlite3_open_v2(ptr, ptr, i32, ptr)
|
declare i32 @sqlite3_open_v2(ptr, ptr, i32, ptr)
|
||||||
|
|
||||||
declare ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String")
|
|
||||||
|
|
||||||
declare i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String")
|
|
||||||
|
|
||||||
declare i32 @sqlite3_prepare(ptr, i32, ptr, ptr)
|
declare i32 @sqlite3_prepare(ptr, i32, ptr, ptr)
|
||||||
|
|
||||||
declare i32 @sqlite3_prepare_v2(ptr, i32, ptr, ptr)
|
declare i32 @sqlite3_prepare_v2(ptr, i32, ptr, ptr)
|
||||||
|
|||||||
Reference in New Issue
Block a user