2024-05-28 07:47:07 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
2024-05-26 08:59:10 +08:00
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
|
|
"github.com/goplus/llgo/internal/abi"
|
|
|
|
|
"github.com/goplus/llgo/internal/runtime/c"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type eface struct {
|
|
|
|
|
_type *_type
|
|
|
|
|
data unsafe.Pointer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type iface struct {
|
|
|
|
|
tab *itab
|
|
|
|
|
data unsafe.Pointer
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 09:34:43 +08:00
|
|
|
type interfacetype = abi.InterfaceType
|
|
|
|
|
|
2024-05-26 08:59:10 +08:00
|
|
|
// layout of Itab known to compilers
|
|
|
|
|
// allocated in non-garbage-collected memory
|
|
|
|
|
// Needs to be in sync with
|
|
|
|
|
// ../cmd/compile/internal/reflectdata/reflect.go:/^func.WriteTabs.
|
|
|
|
|
type itab struct {
|
|
|
|
|
inter *interfacetype
|
|
|
|
|
_type *_type
|
|
|
|
|
hash uint32 // copy of _type.hash. Used for type switches.
|
|
|
|
|
_ [4]byte
|
|
|
|
|
fun [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
|
Eface = eface
|
|
|
|
|
Iface = iface
|
|
|
|
|
Itab = itab
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-27 08:07:34 +08:00
|
|
|
type Imethod = abi.Imethod
|
|
|
|
|
type Method = abi.Method
|
2024-05-26 08:59:10 +08:00
|
|
|
type FuncType = abi.FuncType
|
|
|
|
|
type InterfaceType = abi.InterfaceType
|
|
|
|
|
|
|
|
|
|
// ToEface converts an iface to an eface.
|
|
|
|
|
func ToEface(i Iface) Eface {
|
|
|
|
|
return Eface{i.tab._type, i.data}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
const (
|
2024-05-27 08:07:34 +08:00
|
|
|
typeHdrSize = unsafe.Sizeof(abi.Type{})
|
|
|
|
|
arrayTypeHdrSize = unsafe.Sizeof(abi.ArrayType{})
|
|
|
|
|
chanTypeHdrSize = unsafe.Sizeof(abi.ChanType{})
|
|
|
|
|
funcTypeHdrSize = unsafe.Sizeof(abi.FuncType{})
|
|
|
|
|
interfaceTypeHdrSize = unsafe.Sizeof(abi.InterfaceType{})
|
|
|
|
|
mapTypeHdrSize = unsafe.Sizeof(abi.MapType{})
|
|
|
|
|
ptrTypeHdrSize = unsafe.Sizeof(abi.PtrType{})
|
|
|
|
|
sliceTypeHdrSize = unsafe.Sizeof(abi.SliceType{})
|
|
|
|
|
structTypeHdrSize = unsafe.Sizeof(abi.StructType{})
|
|
|
|
|
uncommonTypeHdrSize = unsafe.Sizeof(abi.UncommonType{})
|
|
|
|
|
methodSize = unsafe.Sizeof(abi.Method{})
|
|
|
|
|
pointerSize = unsafe.Sizeof(uintptr(0))
|
|
|
|
|
itabHdrSize = unsafe.Sizeof(itab{}) - pointerSize
|
2024-05-26 08:59:10 +08:00
|
|
|
)
|
|
|
|
|
|
2024-05-27 08:07:34 +08:00
|
|
|
var hdrSizes = [...]uintptr{
|
|
|
|
|
arrayTypeHdrSize,
|
|
|
|
|
chanTypeHdrSize,
|
|
|
|
|
funcTypeHdrSize,
|
|
|
|
|
interfaceTypeHdrSize,
|
|
|
|
|
mapTypeHdrSize,
|
|
|
|
|
ptrTypeHdrSize,
|
|
|
|
|
sliceTypeHdrSize,
|
|
|
|
|
typeHdrSize,
|
|
|
|
|
structTypeHdrSize,
|
2024-05-26 08:59:10 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-27 08:07:34 +08:00
|
|
|
func hdrSizeOf(kind abi.Kind) uintptr {
|
|
|
|
|
if kind >= abi.Array && kind <= abi.Struct {
|
|
|
|
|
return hdrSizes[kind-abi.Array]
|
2024-05-26 08:59:10 +08:00
|
|
|
}
|
2024-05-27 08:07:34 +08:00
|
|
|
return typeHdrSize
|
2024-05-26 08:59:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Named returns a named type.
|
2024-05-28 07:47:07 +08:00
|
|
|
func Named(pkgPath, name string, underlying *Type, methods []Method) *Type {
|
2024-05-26 08:59:10 +08:00
|
|
|
tflag := underlying.TFlag
|
2024-05-27 08:07:34 +08:00
|
|
|
if tflag&abi.TFlagUncommon != 0 {
|
|
|
|
|
panic("runtime: underlying type is already named")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kind := underlying.Kind()
|
2024-05-26 08:59:10 +08:00
|
|
|
n := len(methods)
|
2024-05-27 08:07:34 +08:00
|
|
|
if kind == abi.Interface {
|
|
|
|
|
if n > 0 {
|
|
|
|
|
panic("runtime: interface type cannot have methods")
|
|
|
|
|
}
|
|
|
|
|
ret := *underlying.InterfaceType()
|
|
|
|
|
ret.PkgPath_ = pkgPath
|
|
|
|
|
ret.Str_ = name
|
|
|
|
|
return &ret.Type
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
baseSize := hdrSizeOf(kind)
|
|
|
|
|
extraSize := uintptr(0)
|
|
|
|
|
if kind == abi.Func {
|
|
|
|
|
f := underlying.FuncType()
|
|
|
|
|
extraSize = uintptr(f.In()+f.Out()) * pointerSize
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size := baseSize + extraSize
|
2024-05-26 17:53:29 +08:00
|
|
|
if n > 0 || pkgPath != "" {
|
2024-05-26 08:59:10 +08:00
|
|
|
size += uncommonTypeHdrSize + uintptr(n)*methodSize
|
|
|
|
|
tflag |= abi.TFlagUncommon
|
|
|
|
|
}
|
2024-05-27 08:07:34 +08:00
|
|
|
|
2024-05-26 08:59:10 +08:00
|
|
|
ptr := AllocU(size)
|
2024-05-27 08:07:34 +08:00
|
|
|
c.Memcpy(ptr, unsafe.Pointer(underlying), baseSize)
|
2024-05-26 08:59:10 +08:00
|
|
|
|
|
|
|
|
ret := (*Type)(ptr)
|
|
|
|
|
ret.TFlag = tflag | abi.TFlagNamed
|
|
|
|
|
ret.Str_ = name
|
|
|
|
|
|
|
|
|
|
xcount := 0
|
|
|
|
|
for _, m := range methods {
|
|
|
|
|
if !m.Exported() {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
xcount++
|
|
|
|
|
}
|
2024-05-27 08:07:34 +08:00
|
|
|
uncommon := (*abi.UncommonType)(c.Advance(ptr, int(baseSize)))
|
2024-05-26 09:34:43 +08:00
|
|
|
uncommon.PkgPath_ = pkgPath
|
|
|
|
|
uncommon.Mcount = uint16(n)
|
|
|
|
|
uncommon.Xcount = uint16(xcount)
|
2024-05-27 08:07:34 +08:00
|
|
|
uncommon.Moff = uint32(uncommonTypeHdrSize + extraSize)
|
|
|
|
|
|
|
|
|
|
extraOff := int(baseSize + uncommonTypeHdrSize)
|
|
|
|
|
if extraSize > 0 {
|
|
|
|
|
src := c.Advance(unsafe.Pointer(underlying), int(baseSize))
|
|
|
|
|
dest := c.Advance(unsafe.Pointer(ptr), extraOff)
|
|
|
|
|
c.Memcpy(dest, src, extraSize)
|
|
|
|
|
extraOff += int(extraSize)
|
|
|
|
|
}
|
2024-05-26 08:59:10 +08:00
|
|
|
|
2024-05-27 08:07:34 +08:00
|
|
|
data := (*abi.Method)(c.Advance(ptr, extraOff))
|
2024-05-26 08:59:10 +08:00
|
|
|
copy(unsafe.Slice(data, n), methods)
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-27 08:07:34 +08:00
|
|
|
// Func returns a function type.
|
|
|
|
|
func Func(in, out []*Type, variadic bool) *FuncType {
|
|
|
|
|
n := len(in) + len(out)
|
|
|
|
|
ptr := AllocU(funcTypeHdrSize + uintptr(n)*pointerSize)
|
|
|
|
|
c.Memset(ptr, 0, funcTypeHdrSize)
|
|
|
|
|
|
|
|
|
|
ret := (*abi.FuncType)(ptr)
|
|
|
|
|
ret.Size_ = pointerSize
|
|
|
|
|
ret.Hash = uint32(abi.Func) // TODO(xsw): hash
|
|
|
|
|
ret.Kind_ = uint8(abi.Func)
|
|
|
|
|
ret.InCount = uint16(len(in))
|
|
|
|
|
ret.OutCount = uint16(len(out))
|
|
|
|
|
if variadic {
|
|
|
|
|
ret.OutCount |= 1 << 15
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data := (**Type)(c.Advance(ptr, int(funcTypeHdrSize)))
|
|
|
|
|
params := unsafe.Slice(data, n)
|
|
|
|
|
copy(params, in)
|
|
|
|
|
copy(params[len(in):], out)
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 08:59:10 +08:00
|
|
|
// Interface returns an interface type.
|
2024-05-28 07:47:07 +08:00
|
|
|
func Interface(pkgPath string, methods []Imethod) *InterfaceType {
|
2024-05-26 08:59:10 +08:00
|
|
|
ret := &abi.InterfaceType{
|
|
|
|
|
Type: Type{
|
|
|
|
|
Size_: unsafe.Sizeof(eface{}),
|
|
|
|
|
Hash: uint32(abi.Interface), // TODO(xsw): hash
|
|
|
|
|
Kind_: uint8(abi.Interface),
|
|
|
|
|
},
|
2024-05-26 17:53:29 +08:00
|
|
|
PkgPath_: pkgPath,
|
|
|
|
|
Methods: methods,
|
2024-05-26 08:59:10 +08:00
|
|
|
}
|
2024-05-27 08:07:34 +08:00
|
|
|
return ret
|
2024-05-26 08:59:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewItab returns a new itab.
|
|
|
|
|
func NewItab(inter *InterfaceType, typ *Type) *Itab {
|
|
|
|
|
n := len(inter.Methods)
|
|
|
|
|
size := itabHdrSize + uintptr(n)*pointerSize
|
|
|
|
|
ptr := AllocU(size)
|
|
|
|
|
|
2024-05-27 08:07:34 +08:00
|
|
|
ret := (*itab)(ptr)
|
2024-05-26 08:59:10 +08:00
|
|
|
ret.inter = inter
|
|
|
|
|
ret._type = typ
|
|
|
|
|
ret.hash = typ.Hash
|
|
|
|
|
|
|
|
|
|
u := typ.Uncommon()
|
|
|
|
|
if u == nil {
|
|
|
|
|
ret.fun[0] = 0
|
|
|
|
|
} else {
|
|
|
|
|
data := (*uintptr)(c.Advance(ptr, int(itabHdrSize)))
|
2024-05-26 17:53:29 +08:00
|
|
|
mthds := methods(u, inter.PkgPath_)
|
2024-05-26 08:59:10 +08:00
|
|
|
for i, m := range inter.Methods {
|
|
|
|
|
fn := findMethod(mthds, m)
|
|
|
|
|
if fn == nil {
|
|
|
|
|
ret.fun[0] = 0
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
*c.Advance(data, i) = uintptr(fn)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func findMethod(mthds []abi.Method, im abi.Imethod) abi.Text {
|
2024-05-26 17:53:29 +08:00
|
|
|
imName := im.Name_
|
2024-05-26 08:59:10 +08:00
|
|
|
for _, m := range mthds {
|
2024-05-26 17:53:29 +08:00
|
|
|
mName := m.Name_
|
2024-05-26 08:59:10 +08:00
|
|
|
if mName >= imName {
|
|
|
|
|
if mName == imName && m.Mtyp_ == im.Typ_ {
|
2024-05-28 07:47:07 +08:00
|
|
|
println("==> findMethod", mName, m.Ifn_)
|
2024-05-26 08:59:10 +08:00
|
|
|
return m.Ifn_
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 17:53:29 +08:00
|
|
|
func methods(u *abi.UncommonType, from string) []abi.Method {
|
|
|
|
|
if u.PkgPath_ == from {
|
2024-05-26 08:59:10 +08:00
|
|
|
return u.Methods()
|
|
|
|
|
}
|
|
|
|
|
return u.ExportedMethods()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|