AllocCStr allocates on heap

This commit is contained in:
Li Jie
2025-04-11 15:17:37 +08:00
parent 7284042823
commit 8512395985
4 changed files with 36 additions and 9 deletions

View File

@@ -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

View File

@@ -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:

View File

@@ -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

View File

@@ -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 {