Skip to content

Commit 806f8b3

Browse files
committed
runtime: typelist init
1 parent bdeab2f commit 806f8b3

3 files changed

Lines changed: 136 additions & 678 deletions

File tree

runtime/internal/lib/reflect/type.go

Lines changed: 131 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"github.com/goplus/llgo/runtime/abi"
3030
clite "github.com/goplus/llgo/runtime/internal/clite"
3131
"github.com/goplus/llgo/runtime/internal/lib/sync"
32-
"github.com/goplus/llgo/runtime/internal/runtime"
32+
_ "github.com/goplus/llgo/runtime/internal/runtime"
3333
"github.com/goplus/llgo/runtime/internal/runtime/goarch"
3434
)
3535

@@ -2110,31 +2110,6 @@ func StructOf(fields []StructField) Type {
21102110
return addToCache(toType(&typ.Type))
21112111
}
21122112

2113-
func eqFields(s1, s2 []abi.StructField) bool {
2114-
n := len(s1)
2115-
if n != len(s2) {
2116-
return false
2117-
}
2118-
for i := 0; i < n; i++ {
2119-
f1, f2 := s1[i], s2[i]
2120-
if f1.Name_ != f2.Name_ || f1.Embedded_ != f2.Embedded_ || f1.Typ != f2.Typ {
2121-
return false
2122-
}
2123-
}
2124-
return true
2125-
}
2126-
2127-
func findClosure(fields []abi.StructField) *abi.Type {
2128-
for _, typ := range runtime.TypeList() {
2129-
if typ.Kind() == abi.Struct && typ.Size() == pointerSize {
2130-
if st := typ.StructType(); st.IsClosure() && eqFields(st.Fields, fields) {
2131-
return typ
2132-
}
2133-
}
2134-
}
2135-
return nil
2136-
}
2137-
21382113
const (
21392114
pointerSize = unsafe.Sizeof(uintptr(0))
21402115
pointerAlign = uint8(unsafe.Alignof(uintptr(0)))
@@ -2218,9 +2193,6 @@ func closureOf(ftyp *abi.FuncType) *abi.Type {
22182193
return tt
22192194
}
22202195

2221-
// if tt := findClosure(fields); tt != nil {
2222-
// return addToCache(tt)
2223-
// }
22242196
for _, tt := range typesByString(str) {
22252197
if haveIdenticalUnderlyingType(&st.Type, tt, true) {
22262198
return addToCache(tt)
@@ -2372,11 +2344,140 @@ func runtimeStructField(field StructField) (structField, string) {
23722344
return f, field.PkgPath
23732345
}
23742346

2347+
//go:linkname typelist github.com/goplus/llgo/runtime/internal/runtime.typelist
2348+
var typelist []*abi.Type
2349+
23752350
func typesByString(s string) (typs []*abi.Type) {
2376-
for _, t := range runtime.TypeList() {
2351+
for _, t := range typelist {
23772352
if t.String() == s {
23782353
typs = append(typs, t)
23792354
}
23802355
}
23812356
return
23822357
}
2358+
2359+
func findNamed(pkgPath string, name string) *abi.Type {
2360+
for _, typ := range typelist {
2361+
if typ.TFlag&(abi.TFlagNamed|abi.TFlagUncommon) != 0 &&
2362+
typ.Str_ == name && typ.Uncommon().PkgPath_ == pkgPath {
2363+
return typ
2364+
}
2365+
}
2366+
return nil
2367+
}
2368+
2369+
func findElem(kind abi.Kind, elem *abi.Type, extra uintptr) *abi.Type {
2370+
for _, typ := range typelist {
2371+
if typ.Kind() == kind && typ.Elem() == elem {
2372+
switch kind {
2373+
case abi.Chan:
2374+
if uintptr(typ.ChanDir()) == extra {
2375+
return typ
2376+
}
2377+
case abi.Array:
2378+
if uintptr(typ.Len()) == extra {
2379+
return typ
2380+
}
2381+
default:
2382+
return typ
2383+
}
2384+
}
2385+
}
2386+
return nil
2387+
}
2388+
2389+
func findMap(key, elem *abi.Type) *abi.Type {
2390+
for _, typ := range typelist {
2391+
if typ.Kind() == abi.Map {
2392+
if mt := typ.MapType(); mt.Key == key && mt.Elem == elem {
2393+
return typ
2394+
}
2395+
}
2396+
}
2397+
return nil
2398+
}
2399+
2400+
func eqFields(s1, s2 []abi.StructField) bool {
2401+
n := len(s1)
2402+
if n != len(s2) {
2403+
return false
2404+
}
2405+
for i := 0; i < n; i++ {
2406+
f1, f2 := s1[i], s2[i]
2407+
if f1.Name_ != f2.Name_ || f1.Embedded_ != f2.Embedded_ || f1.Typ != f2.Typ {
2408+
return false
2409+
}
2410+
}
2411+
return true
2412+
}
2413+
2414+
func findStruct(pkgPath string, size uintptr, fields []abi.StructField) *abi.Type {
2415+
for _, typ := range typelist {
2416+
if typ.Kind() == abi.Struct && typ.Size() == size {
2417+
if st := typ.StructType(); (st.PkgPath_ == pkgPath) && eqFields(st.Fields, fields) {
2418+
return typ
2419+
}
2420+
}
2421+
}
2422+
return nil
2423+
}
2424+
2425+
func eqImethods(s1, s2 []abi.Imethod) bool {
2426+
n := len(s1)
2427+
if n != len(s2) {
2428+
return false
2429+
}
2430+
for i := 0; i < n; i++ {
2431+
f1, f2 := s1[i], s2[i]
2432+
if f1.Name_ != f2.Name_ || f1.Typ_ != f2.Typ_ {
2433+
return false
2434+
}
2435+
}
2436+
return true
2437+
}
2438+
2439+
func findInterface(pkgPath string, methods []abi.Imethod) *abi.InterfaceType {
2440+
for _, typ := range typelist {
2441+
if typ.Kind() == abi.Interface {
2442+
if it := typ.InterfaceType(); (it.PkgPath_ == pkgPath) && eqImethods(it.Methods, methods) {
2443+
return it
2444+
}
2445+
}
2446+
}
2447+
return nil
2448+
}
2449+
2450+
func eqTypes(s1, s2 []*abi.Type) bool {
2451+
n := len(s1)
2452+
if n != len(s2) {
2453+
return false
2454+
}
2455+
for i := 0; i < n; i++ {
2456+
if s1[i] != s2[i] {
2457+
return false
2458+
}
2459+
}
2460+
return true
2461+
}
2462+
2463+
func findFunc(in, out []*abi.Type, variadic bool) *abi.FuncType {
2464+
for _, typ := range typelist {
2465+
if typ.Kind() == abi.Func {
2466+
if ft := typ.FuncType(); ft.Variadic() == variadic && eqTypes(ft.In, in) && eqTypes(ft.Out, out) {
2467+
return ft
2468+
}
2469+
}
2470+
}
2471+
return nil
2472+
}
2473+
2474+
func findClosure(fields []abi.StructField) *abi.Type {
2475+
for _, typ := range typelist {
2476+
if typ.Kind() == abi.Struct && typ.Size() == pointerSize {
2477+
if st := typ.StructType(); st.IsClosure() && eqFields(st.Fields, fields) {
2478+
return typ
2479+
}
2480+
}
2481+
}
2482+
return nil
2483+
}

0 commit comments

Comments
 (0)