llgo/ssa: AfterInit/SliceLit/InterfaceData, unsafe.Slice; ssa/abi: Basic/Struct

This commit is contained in:
xushiwei
2024-05-22 10:07:21 +08:00
parent 556939139b
commit c19786bdfb
18 changed files with 361 additions and 202 deletions

View File

@@ -84,14 +84,12 @@ const (
UnsafePointer
)
/*
const (
// TODO (khr, drchase) why aren't these in TFlag? Investigate, fix if possible.
KindDirectIface = 1 << 5
KindGCProg = 1 << 6 // Type.gc points to GC program
KindMask = (1 << 5) - 1
)
*/
// TFlag is used by a Type to signal what extra type information is
// available in the memory directly following the Type value.
@@ -229,7 +227,7 @@ type Imethod struct {
Typ TypeOff // .(*FuncType) underneath
}
func (t *Type) Kind() Kind { return Kind(t.Kind_) }
func (t *Type) Kind() Kind { return Kind(t.Kind_ & KindMask) }
// Size returns the size of data with type t.
func (t *Type) Size() uintptr { return t.Size_ }

View File

@@ -8,21 +8,20 @@ import (
"unsafe"
)
type iface struct {
tab *itab
data unsafe.Pointer
}
/*
type eface struct {
_type *_type
data unsafe.Pointer
}
/*
func efaceOf(ep *any) *eface {
return (*eface)(unsafe.Pointer(ep))
}
*/
type iface struct {
tab *itab
data unsafe.Pointer
}
// layout of Itab known to compilers
// allocated in non-garbage-collected memory
@@ -35,3 +34,11 @@ type itab struct {
_ [4]byte
fun [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter.
}
func MakeInterface(inter *InterfaceType, typ *Type, data unsafe.Pointer) Interface {
tab := &itab{inter: inter, _type: typ, hash: 0, fun: [1]uintptr{0}}
return Interface{
tab: tab, data: data,
}
}
*/

View File

@@ -40,8 +40,8 @@ func Zeroinit(p c.Pointer, size uintptr) c.Pointer {
}
// TracePanic prints panic message.
func TracePanic(v Interface) {
kind := abi.Kind(v.tab._type.Kind_)
func TracePanic(v Eface) {
kind := abi.Kind(v._type.Kind_)
switch {
case kind == abi.String:
stringTracef(c.Stderr, c.Str("panic: %s\n"), *(*String)(v.data))

View File

@@ -0,0 +1,76 @@
/*
* 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.
*/
package runtime
import (
"unsafe"
"github.com/goplus/llgo/internal/abi"
)
type Eface = eface
// -----------------------------------------------------------------------------
func MakeAnyIntptr(typ *Type, data uintptr) Eface {
return eface{
_type: typ, data: unsafe.Pointer(data),
}
}
func MakeAnyString(data string) Eface {
typ := basicTypes[abi.String]
return eface{
_type: typ, data: unsafe.Pointer(&data),
}
}
func MakeAny(typ *Type, data unsafe.Pointer) Eface {
return eface{
_type: typ, data: data,
}
}
func I2Int(v Eface, t *Type) uintptr {
if v._type == t {
return uintptr(v.data)
}
panic("I2Int: type mismatch")
}
func CheckI2Int(v Eface, t *Type) (uintptr, bool) {
if v._type == t {
return uintptr(v.data), true
}
return 0, false
}
func I2String(v Eface) string {
if v._type.Kind() == abi.String {
return *(*string)(v.data)
}
panic("I2String: type mismatch")
}
func CheckI2String(v Eface) (string, bool) {
if v._type.Kind() == abi.String {
return *(*string)(v.data), true
}
return "", false
}
// -----------------------------------------------------------------------------

View File

@@ -1,94 +0,0 @@
/*
* 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.
*/
package runtime
import (
"unsafe"
"github.com/goplus/llgo/internal/abi"
)
// -----------------------------------------------------------------------------
type InterfaceType = abi.InterfaceType
var (
TyAny = &InterfaceType{}
)
// -----------------------------------------------------------------------------
type Interface = iface
func MakeAnyIntptr(typ *Type, data uintptr) Interface {
tab := &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}}
return Interface{
tab: tab, data: unsafe.Pointer(data),
}
}
func MakeAnyString(data string) Interface {
typ := basicTypes[abi.String]
tab := &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}}
return Interface{
tab: tab, data: unsafe.Pointer(&data),
}
}
func MakeAny(typ *Type, data unsafe.Pointer) Interface {
tab := &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}}
return Interface{
tab: tab, data: data,
}
}
func MakeInterface(inter *InterfaceType, typ *Type, data unsafe.Pointer) Interface {
tab := &itab{inter: inter, _type: typ, hash: 0, fun: [1]uintptr{0}}
return Interface{
tab: tab, data: data,
}
}
func I2Int(v Interface, t *Type) uintptr {
if v.tab._type == t {
return uintptr(v.data)
}
panic("I2Int: type mismatch")
}
func CheckI2Int(v Interface, t *Type) (uintptr, bool) {
if v.tab._type == t {
return uintptr(v.data), true
}
return 0, false
}
func I2String(v Interface, t *Type) string {
if v.tab._type == t {
return *(*string)(v.data)
}
panic("I2String: type mismatch")
}
func CheckI2String(v Interface, t *Type) (string, bool) {
if v.tab._type == t {
return *(*string)(v.data), true
}
return "", false
}
// -----------------------------------------------------------------------------

View File

@@ -66,6 +66,6 @@ func PrintSlice(s Slice) {
print("[", s.len, "/", s.cap, "]", s.data)
}
func PrintIface(i Interface) {
print("(", i.tab, ",", i.data, ")")
func PrintEface(e Eface) {
print("(", e._type, ",", e.data, ")")
}