llgo/ssa: pkgName

This commit is contained in:
xushiwei
2024-05-26 09:34:43 +08:00
parent 914a0c60b0
commit 91c9b4e168
4 changed files with 15 additions and 16 deletions

View File

@@ -12,11 +12,9 @@ import (
type _type = abi.Type type _type = abi.Type
type interfacetype = abi.InterfaceType /*
type maptype = abi.MapType type maptype = abi.MapType
/*
type arraytype = abi.ArrayType type arraytype = abi.ArrayType
type chantype = abi.ChanType type chantype = abi.ChanType

View File

@@ -21,6 +21,8 @@ type iface struct {
data unsafe.Pointer data unsafe.Pointer
} }
type interfacetype = abi.InterfaceType
// layout of Itab known to compilers // layout of Itab known to compilers
// allocated in non-garbage-collected memory // allocated in non-garbage-collected memory
// Needs to be in sync with // Needs to be in sync with
@@ -126,12 +128,10 @@ func Named(pkgPath, name Name, underlying *Type, methods []abi.Method) *Type {
} }
uncommon := (*abi.UncommonType)(c.Advance(ptr, int(typeHdrSize))) uncommon := (*abi.UncommonType)(c.Advance(ptr, int(typeHdrSize)))
*uncommon = abi.UncommonType{ uncommon.PkgPath_ = pkgPath
PkgPath_: pkgPath, uncommon.Mcount = uint16(n)
Mcount: uint16(n), uncommon.Xcount = uint16(xcount)
Xcount: uint16(xcount), uncommon.Moff = uint32(uncommonTypeHdrSize)
Moff: uint32(uncommonTypeHdrSize),
}
data := (*abi.Method)(c.Advance(ptr, int(typeHdrSize+uncommonTypeHdrSize))) data := (*abi.Method)(c.Advance(ptr, int(typeHdrSize+uncommonTypeHdrSize)))
copy(unsafe.Slice(data, n), methods) copy(unsafe.Slice(data, n), methods)
@@ -139,18 +139,14 @@ func Named(pkgPath, name Name, underlying *Type, methods []abi.Method) *Type {
} }
// Interface returns an interface type. // Interface returns an interface type.
func Interface(pkgPath string, methods []abi.Imethod) *Type { func Interface(pkgPath Name, methods []abi.Imethod) *Type {
var npkg abi.Name
if len(pkgPath) > 0 {
npkg = abi.NewName(pkgPath, "", false, false)
}
ret := &abi.InterfaceType{ ret := &abi.InterfaceType{
Type: Type{ Type: Type{
Size_: unsafe.Sizeof(eface{}), Size_: unsafe.Sizeof(eface{}),
Hash: uint32(abi.Interface), // TODO(xsw): hash Hash: uint32(abi.Interface), // TODO(xsw): hash
Kind_: uint8(abi.Interface), Kind_: uint8(abi.Interface),
}, },
PkgPath: npkg, PkgPath: pkgPath,
Methods: methods, Methods: methods,
} }
return &ret.Type return &ret.Type

View File

@@ -221,6 +221,11 @@ func (b Builder) Str(v string) (ret Expr) {
return Expr{aggregateValue(b.impl, prog.rtString(), data, size), prog.String()} return Expr{aggregateValue(b.impl, prog.rtString(), data, size), prog.String()}
} }
func (b Builder) pkgName(pkgPath string) Expr {
// TODO(xsw): use a global cache
return b.Call(b.Pkg.rtFunc("NewPkgName"), b.Str(pkgPath))
}
// unsafeString(data *byte, size int) string // unsafeString(data *byte, size int) string
func (b Builder) unsafeString(data, size llvm.Value) Expr { func (b Builder) unsafeString(data, size llvm.Value) Expr {
prog := b.Prog prog := b.Prog

View File

@@ -84,7 +84,7 @@ func (b Builder) abiStructOf(t *types.Struct) Expr {
off := uintptr(prog.OffsetOf(typ, i)) off := uintptr(prog.OffsetOf(typ, i))
flds[i] = b.structField(sfAbi, prog, f, off, t.Tag(i)) flds[i] = b.structField(sfAbi, prog, f, off, t.Tag(i))
} }
pkgPath := b.Str(pkg.Path()) pkgPath := b.pkgName(pkg.Path())
params := strucAbi.raw.Type.(*types.Signature).Params() params := strucAbi.raw.Type.(*types.Signature).Params()
tSlice := prog.rawType(params.At(params.Len() - 1).Type().(*types.Slice)) tSlice := prog.rawType(params.At(params.Len() - 1).Type().(*types.Slice))
fldSlice := b.SliceLit(tSlice, flds...) fldSlice := b.SliceLit(tSlice, flds...)