From 0d68066086ce79444efcf19b9f44b2bcbc38f240 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sun, 28 Apr 2024 10:29:06 +0800 Subject: [PATCH] runtime: MakeAnyString --- cl/compile.go | 16 +++++++--------- internal/runtime/z_iface.go | 7 +++++++ internal/runtime/z_type.go | 2 ++ ssa/expr.go | 18 +++++++++++------- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/cl/compile.go b/cl/compile.go index e6a5ed3d..086408cb 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -245,14 +245,12 @@ func (p *context) isVArgs(vx ssa.Value) (ret []llssa.Expr, ok bool) { return } -func (p *context) checkVArgs(v *ssa.Alloc, t types.Type) bool { +func (p *context) checkVArgs(v *ssa.Alloc, t *types.Pointer) bool { if v.Comment == "varargs" { // this is a varargs allocation - if t, ok := t.(*types.Pointer); ok { - if arr, ok := t.Elem().(*types.Array); ok { - if isAny(arr.Elem()) { - p.vargs[v] = make([]llssa.Expr, arr.Len()) - return true - } + if arr, ok := t.Elem().(*types.Array); ok { + if isAny(arr.Elem()) { + p.vargs[v] = make([]llssa.Expr, arr.Len()) + return true } } } @@ -322,11 +320,11 @@ func (p *context) compileInstrAndValue(b llssa.Builder, iv instrAndValue) (ret l } panic("todo") case *ssa.Alloc: - t := v.Type() + t := v.Type().(*types.Pointer) if p.checkVArgs(v, t) { // varargs: this is a varargs allocation return } - ret = b.Alloc(p.prog.Type(t), v.Heap) + ret = b.Alloc(t, v.Heap) case *ssa.MakeInterface: const ( delayExpr = true // varargs: don't need to convert an expr to any diff --git a/internal/runtime/z_iface.go b/internal/runtime/z_iface.go index e2a07fea..a8ed5590 100644 --- a/internal/runtime/z_iface.go +++ b/internal/runtime/z_iface.go @@ -37,6 +37,13 @@ func MakeAnyInt(typ *Type, data uintptr) Interface { } } +func MakeAnyString(data string) Interface { + return Interface{ + tab: &itab{inter: TyAny, _type: Basic(abi.String), hash: 0, fun: [1]uintptr{0}}, + data: unsafe.Pointer(&data), + } +} + func MakeAny(typ *Type, data unsafe.Pointer) Interface { return Interface{ tab: &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}}, diff --git a/internal/runtime/z_type.go b/internal/runtime/z_type.go index 02b31b2d..3e8adb26 100644 --- a/internal/runtime/z_type.go +++ b/internal/runtime/z_type.go @@ -47,6 +47,7 @@ var ( abi.Float64: basicType(abi.Float64), abi.Complex64: basicType(abi.Complex64), abi.Complex128: basicType(abi.Complex128), + abi.String: basicType(abi.String), } ) @@ -68,6 +69,7 @@ var ( abi.Float64: 8, abi.Complex64: 8, abi.Complex128: 16, + abi.String: unsafe.Sizeof(String{}), } ) diff --git a/ssa/expr.go b/ssa/expr.go index b1a2fe54..165f1a21 100644 --- a/ssa/expr.go +++ b/ssa/expr.go @@ -403,20 +403,21 @@ func (b Builder) IndexAddr(x, idx Expr) Expr { // // t0 = local int // t1 = new int -func (b Builder) Alloc(t Type, heap bool) (ret Expr) { +func (b Builder) Alloc(t *types.Pointer, heap bool) (ret Expr) { if debugInstr { - log.Printf("Alloc %v, %v\n", t.t, heap) + log.Printf("Alloc %v, %v\n", t, heap) } - telem := b.prog.Elem(t) + prog := b.prog + telem := t.Elem() if heap { - ret.impl = llvm.CreateAlloca(b.impl, telem.ll) + ret.impl = llvm.CreateAlloca(b.impl, prog.Type(telem).ll) } else { pkg := b.fn.pkg - size := unsafe.Sizeof(t.t) - ret = b.Call(pkg.rtFunc("Alloc"), b.prog.Val(size)) + size := unsafe.Sizeof(telem) + ret = b.Call(pkg.rtFunc("Alloc"), prog.Val(size)) } // TODO(xsw): zero-initialize - ret.Type = t + ret.Type = prog.Type(t) return } @@ -535,6 +536,9 @@ func (b Builder) MakeInterface(inter types.Type, x Expr, mayDelay bool) (ret Exp case vkSigned, vkUnsigned, vkFloat: fn := pkg.rtFunc("MakeAnyInt") return b.InlineCall(fn, x) + case vkString: + fn := pkg.rtFunc("MakeAnyString") + return b.InlineCall(fn, x) } panic("todo") }