From 85123959859fe6e1df72d671a87e6e52328cae95 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Fri, 11 Apr 2025 15:17:37 +0800 Subject: [PATCH] AllocCStr allocates on heap --- cl/import.go | 19 ++++++++++--------- cl/instr.go | 12 ++++++++++++ runtime/internal/clite/c.go | 3 +++ ssa/memory.go | 11 +++++++++++ 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/cl/import.go b/cl/import.go index c704e09c..eab19a21 100644 --- a/cl/import.go +++ b/cl/import.go @@ -446,17 +446,18 @@ const ( llgoAlloca = llgoInstrBase + 2 llgoAllocaCStr = llgoInstrBase + 3 llgoAllocaCStrs = llgoInstrBase + 4 - llgoAdvance = llgoInstrBase + 5 - llgoIndex = llgoInstrBase + 6 - llgoStringData = llgoInstrBase + 7 - llgoString = llgoInstrBase + 8 - llgoDeferData = llgoInstrBase + 9 + llgoAllocCStr = llgoInstrBase + 5 + llgoAdvance = llgoInstrBase + 6 + llgoIndex = llgoInstrBase + 7 + llgoStringData = llgoInstrBase + 8 + llgoString = llgoInstrBase + 9 + llgoDeferData = llgoInstrBase + 0xa - llgoSigjmpbuf = llgoInstrBase + 0xa - llgoSigsetjmp = llgoInstrBase + 0xb - llgoSiglongjmp = llgoInstrBase + 0xc + llgoSigjmpbuf = llgoInstrBase + 0xb + llgoSigsetjmp = llgoInstrBase + 0xc + llgoSiglongjmp = llgoInstrBase + 0xd - llgoFuncAddr = llgoInstrBase + 0xd + llgoFuncAddr = llgoInstrBase + 0xe llgoPyList = llgoInstrBase + 0x10 llgoPyStr = llgoInstrBase + 0x11 diff --git a/cl/instr.go b/cl/instr.go index 1a725e69..aebc8968 100644 --- a/cl/instr.go +++ b/cl/instr.go @@ -165,6 +165,15 @@ func (p *context) allocaCStr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) panic("allocaCStr(s string): invalid arguments") } +// func allocCStr(s string) *int8 +func (p *context) allocCStr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) { + if len(args) == 1 { + s := p.compileValue(b, args[0]) + return b.AllocCStr(s) + } + panic("allocCStr(s string): invalid arguments") +} + // func allocaCStrs(strs []string, endWithNil bool) **int8 func (p *context) allocaCStrs(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) { if len(args) == 2 { @@ -281,6 +290,7 @@ var llgoInstrs = map[string]int{ "advance": llgoAdvance, "index": llgoIndex, "alloca": llgoAlloca, + "allocCStr": llgoAllocCStr, "allocaCStr": llgoAllocaCStr, "allocaCStrs": llgoAllocaCStrs, "string": llgoString, @@ -470,6 +480,8 @@ func (p *context) call(b llssa.Builder, act llssa.DoAction, call *ssa.CallCommon ret = p.alloca(b, args) case llgoAllocaCStr: ret = p.allocaCStr(b, args) + case llgoAllocCStr: + ret = p.allocCStr(b, args) case llgoAllocaCStrs: ret = p.allocaCStrs(b, args) case llgoString: diff --git a/runtime/internal/clite/c.go b/runtime/internal/clite/c.go index 8408dd97..9d551872 100644 --- a/runtime/internal/clite/c.go +++ b/runtime/internal/clite/c.go @@ -92,6 +92,9 @@ func Alloca(size uintptr) Pointer //go:linkname AllocaCStr llgo.allocaCStr func AllocaCStr(s string) *Char +//go:linkname AllocCStr llgo.allocCStr +func AllocCStr(s string) *Char + //go:linkname AllocaCStrs llgo.allocaCStrs func AllocaCStrs(strs []string, endWithNil bool) **Char diff --git a/ssa/memory.go b/ssa/memory.go index 0b04cef6..cc487123 100644 --- a/ssa/memory.go +++ b/ssa/memory.go @@ -176,6 +176,17 @@ func (b Builder) AllocaCStr(gostr Expr) (ret Expr) { return b.InlineCall(b.Pkg.rtFunc("CStrCopy"), cstr, gostr) } +// AllocCStr allocates space on the heap for copy it from a Go string. +func (b Builder) AllocCStr(gostr Expr) (ret Expr) { + if debugInstr { + log.Printf("AllocCStr %v\n", gostr.impl) + } + n := b.StringLen(gostr) + n1 := b.BinOp(token.ADD, n, b.Prog.Val(1)) + cstr := b.allocUninited(n1) + return b.InlineCall(b.Pkg.rtFunc("CStrCopy"), cstr, gostr) +} + // func allocaCStrs(strs []string, endWithNil bool) **int8 func (b Builder) AllocaCStrs(strs Expr, endWithNil bool) (cstrs Expr) { if debugInstr {