From ae0906d3229352e2a477505a62c95b899667ae46 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Tue, 30 Apr 2024 18:22:56 +0800 Subject: [PATCH 1/4] llgo/ssa: allocaCStr; runtime: String --- cl/_testcgo/allocstr/in.go | 13 +++++++++ cl/_testcgo/allocstr/out.ll | 43 ++++++++++++++++++++++++++++ cl/compile.go | 15 +++++++++- cl/compile_test.go | 2 +- cl/import.go | 5 +++- internal/runtime/c/c.go | 3 ++ internal/runtime/llgo_autogen.ll | 49 +++++++++++++++++++++++++++++++- internal/runtime/z_string.go | 26 +++++++++++++++++ ssa/expr.go | 12 ++++++++ 9 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 cl/_testcgo/allocstr/in.go create mode 100644 cl/_testcgo/allocstr/out.ll 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 From d62bf858ddd6466b7b17272e75129aebae97da59 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Tue, 30 Apr 2024 18:37:31 +0800 Subject: [PATCH 2/4] llgo/ssa: Go const string --- internal/runtime/z_string.go | 7 +++++++ ssa/expr.go | 16 +++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/internal/runtime/z_string.go b/internal/runtime/z_string.go index d968d0d8..1ee8109a 100644 --- a/internal/runtime/z_string.go +++ b/internal/runtime/z_string.go @@ -40,6 +40,11 @@ func EmptyString() String { return String{nil, 0} } +// NewString creates a new string. +func NewString(data unsafe.Pointer, len int) String { + return String{data, len} +} + // StringLen returns the length of a string. func StringLen(s Slice) int { return s.len @@ -50,6 +55,8 @@ 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 diff --git a/ssa/expr.go b/ssa/expr.go index 79651d0f..e5c585e8 100644 --- a/ssa/expr.go +++ b/ssa/expr.go @@ -77,14 +77,6 @@ func (p Program) Null(t Type) Expr { return Expr{llvm.ConstNull(t.ll), t} } -// StringVal returns string constant expression. -func (p Program) StringVal(v string) Expr { - t := p.String() - cstr := llvm.ConstString(v, true) - // TODO(xsw): cstr => gostring - return Expr{cstr, t} -} - // BoolVal returns a boolean constant expression. func (p Program) BoolVal(v bool) Expr { t := p.Bool() @@ -149,7 +141,7 @@ func (b Builder) Const(v constant.Value, typ Type) Expr { return prog.FloatVal(v, typ) } case kind == types.String: - return prog.StringVal(constant.StringVal(v)) + return b.Str(constant.StringVal(v)) } } panic(fmt.Sprintf("unsupported Const: %v, %v", v, typ.t)) @@ -160,6 +152,12 @@ func (b Builder) CStr(v string) Expr { return Expr{llvm.CreateGlobalStringPtr(b.impl, v), b.Prog.CStr()} } +// Str returns a Go string constant expression. +func (b Builder) Str(v string) Expr { + cstr := b.CStr(v) + return b.InlineCall(b.fn.pkg.rtFunc("NewString"), cstr, b.Prog.Val(len(v))) +} + // ----------------------------------------------------------------------------- const ( From f7a54e33775fb1093fd35f4ae6ce5b06e23ef616 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Tue, 30 Apr 2024 18:42:40 +0800 Subject: [PATCH 3/4] llgo/ssa: builder.Str bugfix; runtime: NewString --- internal/runtime/llgo_autogen.ll | 15 ++++++++++++++- ssa/expr.go | 7 +++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/internal/runtime/llgo_autogen.ll b/internal/runtime/llgo_autogen.ll index e60e4268..15cc0d08 100644 --- a/internal/runtime/llgo_autogen.ll +++ b/internal/runtime/llgo_autogen.ll @@ -11,6 +11,7 @@ source_filename = "github.com/goplus/llgo/internal/runtime" @"github.com/goplus/llgo/internal/runtime.basicTypes" = global ptr null @"github.com/goplus/llgo/internal/runtime.init$guard" = global ptr null @"github.com/goplus/llgo/internal/runtime.sizeBasicTypes" = global ptr null +@0 = private unnamed_addr constant [21 x i8] c"I2Int: type mismatch\00", align 1 define ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 %0) { _llgo_0: @@ -104,7 +105,8 @@ _llgo_1: ; preds = %_llgo_0 ret i64 %10 _llgo_2: ; preds = %_llgo_0 - %11 = call %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyString"([21 x i8] c"I2Int: type mismatch\00") + %11 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @0, i64 20) + %12 = call %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyString"(%"github.com/goplus/llgo/internal/runtime.String" %11) unreachable } @@ -212,6 +214,17 @@ _llgo_0: ret %"github.com/goplus/llgo/internal/runtime.Slice" %7 } +define %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr %0, i64 %1) { +_llgo_0: + %2 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 + %3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %2, i32 0, i32 0 + %4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %2, i32 0, i32 1 + store ptr %0, ptr %3, align 8 + store i64 %1, ptr %4, align 4 + %5 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %2, align 8 + ret %"github.com/goplus/llgo/internal/runtime.String" %5 +} + define %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NilSlice"() { _llgo_0: %0 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8 diff --git a/ssa/expr.go b/ssa/expr.go index e5c585e8..545489e2 100644 --- a/ssa/expr.go +++ b/ssa/expr.go @@ -153,9 +153,12 @@ func (b Builder) CStr(v string) Expr { } // Str returns a Go string constant expression. -func (b Builder) Str(v string) Expr { +func (b Builder) Str(v string) (ret Expr) { + prog := b.Prog cstr := b.CStr(v) - return b.InlineCall(b.fn.pkg.rtFunc("NewString"), cstr, b.Prog.Val(len(v))) + ret = b.InlineCall(b.fn.pkg.rtFunc("NewString"), cstr, prog.Val(len(v))) + ret.Type = prog.String() + return } // ----------------------------------------------------------------------------- From d3fddfb634d527c7c9d24edfb1ba5069b51c0581 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Tue, 30 Apr 2024 23:18:18 +0800 Subject: [PATCH 4/4] mv _testcgo => _testrt --- cl/{_testcgo => _testrt}/alloca/in.go | 0 cl/{_testcgo => _testrt}/alloca/out.ll | 0 cl/{_testcgo => _testrt}/allocstr/in.go | 0 cl/{_testcgo => _testrt}/allocstr/out.ll | 6 +++++- cl/{_testcgo => _testrt}/any/in.go | 0 cl/{_testcgo => _testrt}/any/out.ll | 0 cl/{_testdata => _testrt}/builtin/in.go | 0 cl/{_testdata => _testrt}/builtin/out.ll | 0 cl/{_testcgo => _testrt}/cstr/in.go | 0 cl/{_testcgo => _testrt}/cstr/out.ll | 0 cl/{_testcgo => _testrt}/hello/in.go | 0 cl/{_testcgo => _testrt}/hello/out.ll | 0 cl/{_testcgo => _testrt}/strlen/in.go | 0 cl/{_testcgo => _testrt}/strlen/out.ll | 0 cl/{_testcgo => _testrt}/struct/in.go | 0 cl/{_testcgo => _testrt}/struct/out.ll | 0 cl/{_testcgo => _testrt}/sum/in.go | 0 cl/{_testcgo => _testrt}/sum/out.ll | 0 cl/{_testcgo => _testrt}/typalias/in.go | 0 cl/{_testcgo => _testrt}/typalias/out.ll | 0 cl/{_testcgo => _testrt}/unreachable/in.go | 0 cl/{_testcgo => _testrt}/unreachable/out.ll | 0 cl/compile_test.go | 4 ++-- ssa/cl_test.go | 4 ++-- 24 files changed, 9 insertions(+), 5 deletions(-) rename cl/{_testcgo => _testrt}/alloca/in.go (100%) rename cl/{_testcgo => _testrt}/alloca/out.ll (100%) rename cl/{_testcgo => _testrt}/allocstr/in.go (100%) rename cl/{_testcgo => _testrt}/allocstr/out.ll (78%) rename cl/{_testcgo => _testrt}/any/in.go (100%) rename cl/{_testcgo => _testrt}/any/out.ll (100%) rename cl/{_testdata => _testrt}/builtin/in.go (100%) rename cl/{_testdata => _testrt}/builtin/out.ll (100%) rename cl/{_testcgo => _testrt}/cstr/in.go (100%) rename cl/{_testcgo => _testrt}/cstr/out.ll (100%) rename cl/{_testcgo => _testrt}/hello/in.go (100%) rename cl/{_testcgo => _testrt}/hello/out.ll (100%) rename cl/{_testcgo => _testrt}/strlen/in.go (100%) rename cl/{_testcgo => _testrt}/strlen/out.ll (100%) rename cl/{_testcgo => _testrt}/struct/in.go (100%) rename cl/{_testcgo => _testrt}/struct/out.ll (100%) rename cl/{_testcgo => _testrt}/sum/in.go (100%) rename cl/{_testcgo => _testrt}/sum/out.ll (100%) rename cl/{_testcgo => _testrt}/typalias/in.go (100%) rename cl/{_testcgo => _testrt}/typalias/out.ll (100%) rename cl/{_testcgo => _testrt}/unreachable/in.go (100%) rename cl/{_testcgo => _testrt}/unreachable/out.ll (100%) diff --git a/cl/_testcgo/alloca/in.go b/cl/_testrt/alloca/in.go similarity index 100% rename from cl/_testcgo/alloca/in.go rename to cl/_testrt/alloca/in.go diff --git a/cl/_testcgo/alloca/out.ll b/cl/_testrt/alloca/out.ll similarity index 100% rename from cl/_testcgo/alloca/out.ll rename to cl/_testrt/alloca/out.ll diff --git a/cl/_testcgo/allocstr/in.go b/cl/_testrt/allocstr/in.go similarity index 100% rename from cl/_testcgo/allocstr/in.go rename to cl/_testrt/allocstr/in.go diff --git a/cl/_testcgo/allocstr/out.ll b/cl/_testrt/allocstr/out.ll similarity index 78% rename from cl/_testcgo/allocstr/out.ll rename to cl/_testrt/allocstr/out.ll index ddb4b10e..5a3a68b3 100644 --- a/cl/_testcgo/allocstr/out.ll +++ b/cl/_testrt/allocstr/out.ll @@ -5,10 +5,12 @@ source_filename = "main" %"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 } @"main.init$guard" = global ptr null +@0 = private unnamed_addr constant [13 x i8] c"Hello world\0A\00", align 1 define %"github.com/goplus/llgo/internal/runtime.String" @main.hello() { _llgo_0: - ret [13 x i8] c"Hello world\0A\00" + %0 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @0, i64 12) + ret %"github.com/goplus/llgo/internal/runtime.String" %0 } define void @main.init() { @@ -36,6 +38,8 @@ _llgo_0: ret void } +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.Slice") declare ptr @"github.com/goplus/llgo/internal/runtime.CStrCopy"(ptr, %"github.com/goplus/llgo/internal/runtime.String") diff --git a/cl/_testcgo/any/in.go b/cl/_testrt/any/in.go similarity index 100% rename from cl/_testcgo/any/in.go rename to cl/_testrt/any/in.go diff --git a/cl/_testcgo/any/out.ll b/cl/_testrt/any/out.ll similarity index 100% rename from cl/_testcgo/any/out.ll rename to cl/_testrt/any/out.ll diff --git a/cl/_testdata/builtin/in.go b/cl/_testrt/builtin/in.go similarity index 100% rename from cl/_testdata/builtin/in.go rename to cl/_testrt/builtin/in.go diff --git a/cl/_testdata/builtin/out.ll b/cl/_testrt/builtin/out.ll similarity index 100% rename from cl/_testdata/builtin/out.ll rename to cl/_testrt/builtin/out.ll diff --git a/cl/_testcgo/cstr/in.go b/cl/_testrt/cstr/in.go similarity index 100% rename from cl/_testcgo/cstr/in.go rename to cl/_testrt/cstr/in.go diff --git a/cl/_testcgo/cstr/out.ll b/cl/_testrt/cstr/out.ll similarity index 100% rename from cl/_testcgo/cstr/out.ll rename to cl/_testrt/cstr/out.ll diff --git a/cl/_testcgo/hello/in.go b/cl/_testrt/hello/in.go similarity index 100% rename from cl/_testcgo/hello/in.go rename to cl/_testrt/hello/in.go diff --git a/cl/_testcgo/hello/out.ll b/cl/_testrt/hello/out.ll similarity index 100% rename from cl/_testcgo/hello/out.ll rename to cl/_testrt/hello/out.ll diff --git a/cl/_testcgo/strlen/in.go b/cl/_testrt/strlen/in.go similarity index 100% rename from cl/_testcgo/strlen/in.go rename to cl/_testrt/strlen/in.go diff --git a/cl/_testcgo/strlen/out.ll b/cl/_testrt/strlen/out.ll similarity index 100% rename from cl/_testcgo/strlen/out.ll rename to cl/_testrt/strlen/out.ll diff --git a/cl/_testcgo/struct/in.go b/cl/_testrt/struct/in.go similarity index 100% rename from cl/_testcgo/struct/in.go rename to cl/_testrt/struct/in.go diff --git a/cl/_testcgo/struct/out.ll b/cl/_testrt/struct/out.ll similarity index 100% rename from cl/_testcgo/struct/out.ll rename to cl/_testrt/struct/out.ll diff --git a/cl/_testcgo/sum/in.go b/cl/_testrt/sum/in.go similarity index 100% rename from cl/_testcgo/sum/in.go rename to cl/_testrt/sum/in.go diff --git a/cl/_testcgo/sum/out.ll b/cl/_testrt/sum/out.ll similarity index 100% rename from cl/_testcgo/sum/out.ll rename to cl/_testrt/sum/out.ll diff --git a/cl/_testcgo/typalias/in.go b/cl/_testrt/typalias/in.go similarity index 100% rename from cl/_testcgo/typalias/in.go rename to cl/_testrt/typalias/in.go diff --git a/cl/_testcgo/typalias/out.ll b/cl/_testrt/typalias/out.ll similarity index 100% rename from cl/_testcgo/typalias/out.ll rename to cl/_testrt/typalias/out.ll diff --git a/cl/_testcgo/unreachable/in.go b/cl/_testrt/unreachable/in.go similarity index 100% rename from cl/_testcgo/unreachable/in.go rename to cl/_testrt/unreachable/in.go diff --git a/cl/_testcgo/unreachable/out.ll b/cl/_testrt/unreachable/out.ll similarity index 100% rename from cl/_testcgo/unreachable/out.ll rename to cl/_testrt/unreachable/out.ll diff --git a/cl/compile_test.go b/cl/compile_test.go index c56355fa..b9c3443f 100644 --- a/cl/compile_test.go +++ b/cl/compile_test.go @@ -28,8 +28,8 @@ func testCompile(t *testing.T, src, expected string) { cltest.TestCompileEx(t, src, "foo.go", expected) } -func TestFromTestcgo(t *testing.T) { - cltest.FromDir(t, "allocstr", "./_testcgo", true) +func TestFromTestrt(t *testing.T) { + cltest.FromDir(t, "", "./_testrt", true) } func TestFromTestdata(t *testing.T) { diff --git a/ssa/cl_test.go b/ssa/cl_test.go index 8690bc6c..b5630353 100644 --- a/ssa/cl_test.go +++ b/ssa/cl_test.go @@ -22,8 +22,8 @@ import ( "github.com/goplus/llgo/cl/cltest" ) -func TestFromTestcgo(t *testing.T) { - cltest.FromDir(t, "", "../cl/_testcgo", true) +func TestFromTestrt(t *testing.T) { + cltest.FromDir(t, "", "../cl/_testrt", true) } func TestFromTestdata(t *testing.T) {