From 4a1712f4cd2fd55da943a7c501985852296d1740 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Thu, 20 Jun 2024 00:30:36 +0800 Subject: [PATCH] llgo.string (llgo/ssa.MakeString) fix --- cl/_testlibc/once/in.go | 2 ++ cl/builtin_test.go | 1 + ssa/datastruct.go | 12 ++++++++---- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/cl/_testlibc/once/in.go b/cl/_testlibc/once/in.go index e72471220..216c8381d 100644 --- a/cl/_testlibc/once/in.go +++ b/cl/_testlibc/once/in.go @@ -14,6 +14,8 @@ func f() { } func main() { + println(c.GoString(c.Str("sync.Once demo\n"), 9)) + println(c.GoString(c.Str("sync.Once demo\n"))) f() f() } diff --git a/cl/builtin_test.go b/cl/builtin_test.go index de46687e1..8030d37e4 100644 --- a/cl/builtin_test.go +++ b/cl/builtin_test.go @@ -137,6 +137,7 @@ func TestErrBuiltin(t *testing.T) { test("advance", func(ctx *context) { ctx.advance(nil, nil) }) test("alloca", func(ctx *context) { ctx.alloca(nil, nil) }) test("allocaCStr", func(ctx *context) { ctx.allocaCStr(nil, nil) }) + test("string", func(ctx *context) { ctx.string(nil, nil) }) test("stringData", func(ctx *context) { ctx.stringData(nil, nil) }) test("sigsetjmp", func(ctx *context) { ctx.sigsetjmp(nil, nil) }) test("siglongjmp", func(ctx *context) { ctx.siglongjmp(nil, nil) }) diff --git a/ssa/datastruct.go b/ssa/datastruct.go index aed82763a..aba66b9d8 100644 --- a/ssa/datastruct.go +++ b/ssa/datastruct.go @@ -67,16 +67,20 @@ func (b Builder) getField(x Expr, idx int) Expr { // ----------------------------------------------------------------------------- // MakeString creates a new string from a C string pointer and length. -func (b Builder) MakeString(cstr Expr, n ...Expr) Expr { +func (b Builder) MakeString(cstr Expr, n ...Expr) (ret Expr) { if debugInstr { log.Printf("MakeString %v\n", cstr.impl) } pkg := b.Pkg + prog := b.Prog + ret.Type = prog.String() if len(n) == 0 { - return b.Call(pkg.rtFunc("StringFromCStr"), cstr) + ret.impl = b.Call(pkg.rtFunc("StringFromCStr"), cstr).impl + } else { + // TODO(xsw): remove Convert + ret.impl = b.Call(pkg.rtFunc("StringFrom"), cstr, b.Convert(prog.Int(), n[0])).impl } - // TODO(xsw): remove Convert - return b.Call(pkg.rtFunc("StringFrom"), cstr, b.Convert(b.Prog.Int(), n[0])) + return } // StringData returns the data pointer of a string.