llgo/ssa: allocaCStr; runtime: String

This commit is contained in:
xushiwei
2024-04-30 18:22:56 +08:00
parent c6cb2931e1
commit ae0906d322
9 changed files with 164 additions and 4 deletions

View File

@@ -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()))
}

View File

@@ -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, ...)

View File

@@ -275,6 +275,7 @@ func (p *context) checkVArgs(v *ssa.Alloc, t *types.Pointer) bool {
return false return false
} }
// func cstr(string) *int8
func cstr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) { func cstr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
if len(args) == 1 { if len(args) == 1 {
if c, ok := args[0].(*ssa.Const); ok { 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(<string-literal>): invalid arguments") panic("cstr(<string-literal>): invalid arguments")
} }
// func alloca(size uintptr) unsafe.Pointer
func (p *context) alloca(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) { func (p *context) alloca(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
if len(args) == 1 { if len(args) == 1 {
n := p.compileValue(b, args[0]) 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") 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) { func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue bool) (ret llssa.Expr) {
if asValue { if asValue {
if v, ok := p.bvals[iv]; ok { 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) ret = cstr(b, call.Args)
case llgoAlloca: case llgoAlloca:
ret = p.alloca(b, call.Args) ret = p.alloca(b, call.Args)
case llgoUnreachable: case llgoAllocaCStr:
ret = p.allocaCStr(b, call.Args)
case llgoUnreachable: // func unreachable()
b.Unreachable() b.Unreachable()
default: default:
panic("todo") panic("todo")

View File

@@ -29,7 +29,7 @@ func testCompile(t *testing.T, src, expected string) {
} }
func TestFromTestcgo(t *testing.T) { func TestFromTestcgo(t *testing.T) {
cltest.FromDir(t, "", "./_testcgo", true) cltest.FromDir(t, "allocstr", "./_testcgo", true)
} }
func TestFromTestdata(t *testing.T) { func TestFromTestdata(t *testing.T) {

View File

@@ -178,9 +178,10 @@ const (
llgoInstr = -1 llgoInstr = -1
llgoInstrBase = 0x80 llgoInstrBase = 0x80
llgoUnreachable = llgoInstrBase + 0
llgoCstr = llgoInstrBase + 1 llgoCstr = llgoInstrBase + 1
llgoAlloca = llgoInstrBase + 2 llgoAlloca = llgoInstrBase + 2
llgoUnreachable = llgoInstrBase + 3 llgoAllocaCStr = llgoInstrBase + 3
) )
func (p *context) funcName(pkg *types.Package, fn *ssa.Function, ignore bool) (string, int) { 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 ftype = llgoCstr
case "alloca": case "alloca":
ftype = llgoAlloca ftype = llgoAlloca
case "allocaCStr":
ftype = llgoAllocaCStr
case "unreachable": case "unreachable":
ftype = llgoUnreachable ftype = llgoUnreachable
default: default:

View File

@@ -29,6 +29,9 @@ func Str(string) *int8
//go:linkname Alloca llgo.alloca //go:linkname Alloca llgo.alloca
func Alloca(size uintptr) unsafe.Pointer func Alloca(size uintptr) unsafe.Pointer
//go:linkname AllocaCStr llgo.allocaCStr
func AllocaCStr(s string) *int8
//go:linkname Unreachable llgo.unreachable //go:linkname Unreachable llgo.unreachable
func Unreachable() func Unreachable()

View File

@@ -1,9 +1,9 @@
; ModuleID = 'github.com/goplus/llgo/internal/runtime' ; ModuleID = 'github.com/goplus/llgo/internal/runtime'
source_filename = "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.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.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/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 } %"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 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) { define { i64, i1 } @"github.com/goplus/llgo/internal/runtime.CheckI2Int"(%"github.com/goplus/llgo/internal/runtime.iface" %0, ptr %1) {
_llgo_0: _llgo_0:
%2 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8 %2 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8
@@ -216,6 +243,24 @@ _llgo_0:
ret i64 %3 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) { define ptr @"github.com/goplus/llgo/internal/runtime.basicType"(i64 %0) {
_llgo_0: _llgo_0:
%1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) %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 @malloc(i64)
declare ptr @memcpy(ptr, ptr, i64)
declare void @"github.com/goplus/llgo/internal/abi.init"() declare void @"github.com/goplus/llgo/internal/abi.init"()

View File

@@ -18,6 +18,8 @@ package runtime
import ( import (
"unsafe" "unsafe"
"github.com/goplus/llgo/internal/runtime/c"
) )
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@@ -38,4 +40,28 @@ func EmptyString() String {
return String{nil, 0} 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)
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@@ -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 // The ChangeType instruction applies to X a value-preserving type