diff --git a/cl/_testcgo/allocstr/in.go b/cl/_testcgo/allocstr/in.go new file mode 100644 index 00000000..41bcdd61 --- /dev/null +++ b/cl/_testcgo/allocstr/in.go @@ -0,0 +1,13 @@ +package main + +import ( + "github.com/goplus/llgo/internal/runtime/c" +) + +func hello() string { + return "Hello world\n" +} + +func main() { + c.Printf(c.AllocaCStr(hello())) +} diff --git a/cl/_testcgo/allocstr/out.ll b/cl/_testcgo/allocstr/out.ll new file mode 100644 index 00000000..ddb4b10e --- /dev/null +++ b/cl/_testcgo/allocstr/out.ll @@ -0,0 +1,43 @@ +; ModuleID = 'main' +source_filename = "main" + +%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 } +%"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 } + +@"main.init$guard" = global ptr null + +define %"github.com/goplus/llgo/internal/runtime.String" @main.hello() { +_llgo_0: + ret [13 x i8] c"Hello world\0A\00" +} + +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 + br label %_llgo_2 + +_llgo_2: ; preds = %_llgo_1, %_llgo_0 + ret void +} + +define void @main() { +_llgo_0: + call void @main.init() + %0 = call %"github.com/goplus/llgo/internal/runtime.String" @main.hello() + %1 = call i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.String" %0) + %2 = add i64 %1, 1 + %3 = alloca i8, i64 %2, align 1 + %4 = call ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr %3, %"github.com/goplus/llgo/internal/runtime.String" %0) + %5 = call i32 (ptr, ...) @printf(ptr %4) + ret void +} + +declare i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.Slice") + +declare ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr, %"github.com/goplus/llgo/internal/runtime.String") + +declare i32 @printf(ptr, ...) diff --git a/cl/compile.go b/cl/compile.go index b0a954be..a6e5b88f 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -275,6 +275,7 @@ func (p *context) checkVArgs(v *ssa.Alloc, t *types.Pointer) bool { return false } +// func cstr(string) *int8 func cstr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) { if len(args) == 1 { if c, ok := args[0].(*ssa.Const); ok { @@ -287,6 +288,7 @@ func cstr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) { panic("cstr(): invalid arguments") } +// func alloca(size uintptr) unsafe.Pointer func (p *context) alloca(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) { if len(args) == 1 { n := p.compileValue(b, args[0]) @@ -295,6 +297,15 @@ func (p *context) alloca(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) { panic("alloca(size uintptr): invalid arguments") } +// func allocaCStr(s string) *int8 +func (p *context) allocaCStr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) { + if len(args) == 1 { + s := p.compileValue(b, args[0]) + return b.AllocaCStr(s) + } + panic("allocaCStr(s string): invalid arguments") +} + func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue bool) (ret llssa.Expr) { if asValue { if v, ok := p.bvals[iv]; ok { @@ -334,7 +345,9 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue ret = cstr(b, call.Args) case llgoAlloca: ret = p.alloca(b, call.Args) - case llgoUnreachable: + case llgoAllocaCStr: + ret = p.allocaCStr(b, call.Args) + case llgoUnreachable: // func unreachable() b.Unreachable() default: panic("todo") diff --git a/cl/compile_test.go b/cl/compile_test.go index 62f7bedb..c56355fa 100644 --- a/cl/compile_test.go +++ b/cl/compile_test.go @@ -29,7 +29,7 @@ func testCompile(t *testing.T, src, expected string) { } func TestFromTestcgo(t *testing.T) { - cltest.FromDir(t, "", "./_testcgo", true) + cltest.FromDir(t, "allocstr", "./_testcgo", true) } func TestFromTestdata(t *testing.T) { diff --git a/cl/import.go b/cl/import.go index 7cf32f7e..a2a380f7 100644 --- a/cl/import.go +++ b/cl/import.go @@ -178,9 +178,10 @@ const ( llgoInstr = -1 llgoInstrBase = 0x80 + llgoUnreachable = llgoInstrBase + 0 llgoCstr = llgoInstrBase + 1 llgoAlloca = llgoInstrBase + 2 - llgoUnreachable = llgoInstrBase + 3 + llgoAllocaCStr = llgoInstrBase + 3 ) func (p *context) funcName(pkg *types.Package, fn *ssa.Function, ignore bool) (string, int) { @@ -212,6 +213,8 @@ func (p *context) funcOf(fn *ssa.Function) (ret llssa.Function, ftype int) { ftype = llgoCstr case "alloca": ftype = llgoAlloca + case "allocaCStr": + ftype = llgoAllocaCStr case "unreachable": ftype = llgoUnreachable default: diff --git a/internal/runtime/c/c.go b/internal/runtime/c/c.go index 2bbccdd3..8605b157 100644 --- a/internal/runtime/c/c.go +++ b/internal/runtime/c/c.go @@ -29,6 +29,9 @@ func Str(string) *int8 //go:linkname Alloca llgo.alloca func Alloca(size uintptr) unsafe.Pointer +//go:linkname AllocaCStr llgo.allocaCStr +func AllocaCStr(s string) *int8 + //go:linkname Unreachable llgo.unreachable func Unreachable() diff --git a/internal/runtime/llgo_autogen.ll b/internal/runtime/llgo_autogen.ll index 22268057..e60e4268 100644 --- a/internal/runtime/llgo_autogen.ll +++ b/internal/runtime/llgo_autogen.ll @@ -1,9 +1,9 @@ ; ModuleID = 'github.com/goplus/llgo/internal/runtime' source_filename = "github.com/goplus/llgo/internal/runtime" +%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 } %"github.com/goplus/llgo/internal/runtime.iface" = type { ptr, ptr } %"github.com/goplus/llgo/internal/runtime.itab" = type { ptr, ptr, i32, [4 x i8], [1 x i64] } -%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 } %"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 } %"github.com/goplus/llgo/internal/abi.Type" = type { i64, i64, i32, i8, i8, i8, i8, ptr, ptr, i32, i32 } @@ -25,6 +25,33 @@ _llgo_0: ret ptr %2 } +define ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr %0, %"github.com/goplus/llgo/internal/runtime.String" %1) { +_llgo_0: + %2 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + store %"github.com/goplus/llgo/internal/runtime.String" %1, ptr %2, align 8 + %3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %2, i32 0, i32 1 + %4 = load i64, ptr %3, align 4 + %5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %2, i32 0, i32 0 + %6 = load ptr, ptr %5, align 8 + %7 = call ptr @memcpy(ptr %0, ptr %6, i64 %4) + %8 = getelementptr inbounds i8, ptr %0, i64 %4 + store i8 0, ptr %8, align 1 + ret ptr %0 +} + +define ptr @"github.com/goplus/llgo/internal/runtime.CStrDup"(%"github.com/goplus/llgo/internal/runtime.String" %0) { +_llgo_0: + %1 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + store %"github.com/goplus/llgo/internal/runtime.String" %0, ptr %1, align 8 + %2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %1, i32 0, i32 1 + %3 = load i64, ptr %2, align 4 + %4 = add i64 %3, 1 + %5 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 %4) + %6 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %1, align 8 + %7 = call ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr %5, %"github.com/goplus/llgo/internal/runtime.String" %6) + ret ptr %7 +} + define { i64, i1 } @"github.com/goplus/llgo/internal/runtime.CheckI2Int"(%"github.com/goplus/llgo/internal/runtime.iface" %0, ptr %1) { _llgo_0: %2 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8 @@ -216,6 +243,24 @@ _llgo_0: ret i64 %3 } +define ptr @"github.com/goplus/llgo/internal/runtime.StringData"(%"github.com/goplus/llgo/internal/runtime.String" %0) { +_llgo_0: + %1 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + store %"github.com/goplus/llgo/internal/runtime.String" %0, ptr %1, align 8 + %2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %1, i32 0, i32 0 + %3 = load ptr, ptr %2, align 8 + ret ptr %3 +} + +define i64 @"github.com/goplus/llgo/internal/runtime.StringLen"(%"github.com/goplus/llgo/internal/runtime.Slice" %0) { +_llgo_0: + %1 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8 + store %"github.com/goplus/llgo/internal/runtime.Slice" %0, ptr %1, align 8 + %2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %1, i32 0, i32 1 + %3 = load i64, ptr %2, align 4 + ret i64 %3 +} + define ptr @"github.com/goplus/llgo/internal/runtime.basicType"(i64 %0) { _llgo_0: %1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) @@ -298,4 +343,6 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0 declare ptr @malloc(i64) +declare ptr @memcpy(ptr, ptr, i64) + declare void @"github.com/goplus/llgo/internal/abi.init"() diff --git a/internal/runtime/z_string.go b/internal/runtime/z_string.go index a31f991a..d968d0d8 100644 --- a/internal/runtime/z_string.go +++ b/internal/runtime/z_string.go @@ -18,6 +18,8 @@ package runtime import ( "unsafe" + + "github.com/goplus/llgo/internal/runtime/c" ) // ----------------------------------------------------------------------------- @@ -38,4 +40,28 @@ func EmptyString() String { return String{nil, 0} } +// StringLen returns the length of a string. +func StringLen(s Slice) int { + return s.len +} + +// StringData returns the data pointer of a string. +func StringData(s String) unsafe.Pointer { + return s.data +} + +// CStrCopy copies a Go string to a C string buffer and returns it. +func CStrCopy(dest unsafe.Pointer, s String) *int8 { + n := s.len + c.Memcpy(dest, s.data, uintptr(n)) + arr := (*[1 << 30]int8)(dest) + arr[n] = 0 + return (*int8)(dest) +} + +func CStrDup(s String) *int8 { + dest := Alloc(uintptr(s.len + 1)) + return CStrCopy(dest, s) +} + // ----------------------------------------------------------------------------- diff --git a/ssa/expr.go b/ssa/expr.go index 55803500..79651d0f 100644 --- a/ssa/expr.go +++ b/ssa/expr.go @@ -537,6 +537,18 @@ func (b Builder) ArrayAlloca(telem Type, n Expr) (ret Expr) { } */ +// AllocaCStr allocates space for copy it from a Go string. +func (b Builder) AllocaCStr(gostr Expr) (ret Expr) { + if debugInstr { + log.Printf("AllocaCStr %v\n", gostr.impl) + } + pkg := b.fn.pkg + n := b.InlineCall(pkg.rtFunc("StringLen"), gostr) + n1 := b.BinOp(token.ADD, n, b.Prog.Val(1)) + cstr := b.Alloca(n1) + return b.InlineCall(pkg.rtFunc("CStrCopy"), cstr, gostr) +} + // ----------------------------------------------------------------------------- // The ChangeType instruction applies to X a value-preserving type