Files
llgo/ssa/type.go

574 lines
13 KiB
Go
Raw Normal View History

2024-04-15 04:00:38 +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-04-15 17:12:30 +08:00
package ssa
2024-04-15 04:00:38 +08:00
import (
2024-04-25 21:44:23 +08:00
"fmt"
2024-04-15 04:00:38 +08:00
"go/types"
"unsafe"
2024-04-15 04:00:38 +08:00
2025-04-03 15:52:18 +08:00
"github.com/goplus/llgo/ssa/abi"
2024-04-15 05:48:48 +08:00
"github.com/goplus/llvm"
2024-04-15 04:00:38 +08:00
)
2024-04-27 13:57:21 +08:00
var (
tyAny = types.NewInterfaceType(nil, nil)
2024-06-05 16:29:58 +08:00
NoArgsNoRet = types.NewSignatureType(nil, nil, nil, nil, nil, false)
2024-04-27 13:57:21 +08:00
)
2024-04-18 15:03:10 +08:00
// -----------------------------------------------------------------------------
2024-04-19 00:05:57 +08:00
type valueKind = int
const (
vkInvalid valueKind = iota
vkSigned
vkUnsigned
vkFloat
vkComplex
vkString
vkBool
2024-04-28 07:08:01 +08:00
vkPtr
vkFuncDecl
vkFuncPtr
2024-05-05 12:11:51 +08:00
vkClosure
2024-06-01 17:18:17 +08:00
vkBuiltin
2024-05-12 23:08:44 +08:00
vkPyFuncRef
2024-05-15 18:32:50 +08:00
vkPyVarRef
2024-04-19 00:05:57 +08:00
vkTuple
2024-05-15 10:29:06 +08:00
vkSlice
2024-05-15 21:26:53 +08:00
vkArray
vkMap
vkEface
vkIface
vkStruct
2024-07-02 20:12:12 +08:00
vkChan
2024-04-19 00:05:57 +08:00
)
// -----------------------------------------------------------------------------
2024-04-21 15:12:57 +08:00
func indexType(t types.Type) types.Type {
2024-06-16 22:18:02 +08:00
typ := t
retry:
switch t := typ.(type) {
case *types.Named:
typ = t.Underlying()
goto retry
2024-04-21 15:12:57 +08:00
case *types.Slice:
return t.Elem()
case *types.Pointer:
2024-06-16 22:18:02 +08:00
switch t := t.Elem().Underlying().(type) {
2024-04-21 15:12:57 +08:00
case *types.Array:
return t.Elem()
}
case *types.Array:
return t.Elem()
}
panic("index: type doesn't support index - " + t.String())
}
2024-04-20 13:50:48 +08:00
// -----------------------------------------------------------------------------
2024-05-28 16:59:47 +08:00
type goProgram aProgram
// Alignof returns the alignment of a variable of type T.
// Alignof must implement the alignment guarantees required by the spec.
// The result must be >= 1.
func (p *goProgram) Alignof(T types.Type) int64 {
return p.sizes.Alignof(T)
}
// Offsetsof returns the offsets of the given struct fields, in bytes.
// Offsetsof must implement the offset guarantees required by the spec.
// A negative entry in the result indicates that the struct is too large.
2024-05-28 17:32:09 +08:00
func (p *goProgram) Offsetsof(fields []*types.Var) (ret []int64) {
prog := Program(unsafe.Pointer(p))
2024-05-28 17:32:09 +08:00
ptrSize := int64(prog.PointerSize())
extra := int64(0)
ret = p.sizes.Offsetsof(fields)
for i, f := range fields {
ret[i] += extra
extra += p.extraSize(f.Type(), ptrSize)
2024-05-28 17:32:09 +08:00
}
return
2024-05-28 16:59:47 +08:00
}
// Sizeof returns the size of a variable of type T.
// Sizeof must implement the size guarantees required by the spec.
// A negative result indicates that T is too large.
func (p *goProgram) Sizeof(T types.Type) int64 {
prog := Program(unsafe.Pointer(p))
2024-05-28 17:32:09 +08:00
ptrSize := int64(prog.PointerSize())
baseSize := prog.sizes.Sizeof(T) + p.extraSize(T, ptrSize)
switch T.Underlying().(type) {
case *types.Struct, *types.Array:
return align(baseSize, prog.sizes.Alignof(T))
}
return baseSize
2024-05-28 17:32:09 +08:00
}
func (p *goProgram) extraSize(typ types.Type, ptrSize int64) (ret int64) {
retry:
switch t := typ.(type) {
case *types.Named:
if v, ok := p.gocvt.typbg.Load(namedLinkname(t)); ok && v.(Background) == InC {
return 0
}
typ = t.Underlying()
goto retry
2024-05-28 17:32:09 +08:00
case *types.Signature:
return ptrSize
case *types.Struct:
n := t.NumFields()
for i := 0; i < n; i++ {
f := t.Field(i)
ret += p.extraSize(f.Type(), ptrSize)
2024-05-28 17:32:09 +08:00
}
return
case *types.Array:
return p.extraSize(t.Elem(), ptrSize) * t.Len()
2024-05-28 17:32:09 +08:00
}
return 0
2024-05-28 16:59:47 +08:00
}
func align(x, a int64) int64 {
return (x + a - 1) &^ (a - 1)
}
2024-05-28 16:59:47 +08:00
// -----------------------------------------------------------------------------
2024-05-05 12:11:51 +08:00
type rawType struct {
2024-05-23 01:34:48 +08:00
Type types.Type
2024-05-05 12:11:51 +08:00
}
2024-04-18 15:03:10 +08:00
type aType struct {
ll llvm.Type
2024-05-05 12:11:51 +08:00
raw rawType
kind valueKind // value kind of llvm.Type
2024-04-15 04:00:38 +08:00
}
2024-04-18 15:03:10 +08:00
type Type = *aType
2024-05-05 12:11:51 +08:00
// RawType returns the raw type.
func (t Type) RawType() types.Type {
return t.raw.Type
}
// TypeSizes returns the sizes of the types.
2024-05-28 16:59:47 +08:00
func (p Program) TypeSizes(sizes types.Sizes) types.Sizes {
p.sizes = sizes
return (*goProgram)(unsafe.Pointer(p))
}
2024-05-03 23:10:02 +08:00
// TODO(xsw):
// how to generate platform independent code?
func (p Program) SizeOf(typ Type, n ...int64) uint64 {
2024-05-04 07:21:07 +08:00
size := p.td.TypeAllocSize(typ.ll)
2024-05-03 23:10:02 +08:00
if len(n) != 0 {
size *= uint64(n[0])
}
return size
}
// OffsetOf returns the offset of a field in a struct.
func (p Program) OffsetOf(typ Type, i int) uint64 {
return p.td.ElementOffset(typ.ll, i)
}
// SizeOf returns the size of a type.
func SizeOf(prog Program, t Type, n ...int64) Expr {
size := prog.SizeOf(t, n...)
return prog.IntVal(size, prog.Uintptr())
}
/*
func OffsetOf(prog Program, t Type, i int) Expr {
offset := prog.OffsetOf(t, i)
return prog.IntVal(offset, prog.Uintptr())
}
*/
func (p Program) PointerSize() int {
2024-05-28 17:55:26 +08:00
return p.ptrSize
}
2024-04-30 08:23:55 +08:00
func (p Program) Slice(typ Type) Type {
2024-05-05 12:11:51 +08:00
return p.rawType(types.NewSlice(typ.raw.Type))
2024-04-30 08:23:55 +08:00
}
2024-04-21 15:12:57 +08:00
func (p Program) Pointer(typ Type) Type {
2024-05-05 12:11:51 +08:00
return p.rawType(types.NewPointer(typ.raw.Type))
2024-04-21 15:12:57 +08:00
}
func (p Program) Elem(typ Type) Type {
2024-06-16 22:18:02 +08:00
elem := typ.raw.Type.Underlying().(interface {
2024-05-15 10:29:06 +08:00
Elem() types.Type
}).Elem()
2024-05-05 12:11:51 +08:00
return p.rawType(elem)
2024-04-21 15:12:57 +08:00
}
func (p Program) Index(typ Type) Type {
2024-05-05 12:11:51 +08:00
return p.rawType(indexType(typ.raw.Type))
2024-04-21 15:12:57 +08:00
}
2024-04-27 07:47:10 +08:00
func (p Program) Field(typ Type, i int) Type {
var fld *types.Var
2024-06-20 15:52:56 +08:00
switch t := typ.raw.Type.Underlying().(type) {
case *types.Tuple:
fld = t.At(i)
case *types.Basic:
switch t.Kind() {
case types.Complex128:
return p.Float64()
case types.Complex64:
return p.Float32()
}
panic("Field: basic type doesn't have fields")
default:
2024-06-20 15:52:56 +08:00
fld = t.(*types.Struct).Field(i)
}
return p.rawType(fld.Type())
2024-04-27 07:47:10 +08:00
}
2024-05-05 12:11:51 +08:00
func (p Program) rawType(raw types.Type) Type {
if v := p.typs.At(raw); v != nil {
2024-04-18 15:03:10 +08:00
return v.(Type)
2024-04-15 04:00:38 +08:00
}
2024-05-05 12:11:51 +08:00
ret := p.toType(raw)
p.typs.Set(raw, ret)
2024-04-15 04:00:38 +08:00
return ret
}
2024-04-18 15:03:10 +08:00
func (p Program) tyVoidPtr() llvm.Type {
2024-04-16 03:05:20 +08:00
if p.voidPtrTy.IsNil() {
p.voidPtrTy = llvm.PointerType(p.tyVoid(), 0)
}
return p.voidPtrTy
}
2024-04-18 15:03:10 +08:00
func (p Program) tyVoid() llvm.Type {
2024-04-16 03:05:20 +08:00
if p.voidType.IsNil() {
p.voidType = p.ctx.VoidType()
}
return p.voidType
}
2024-04-18 15:03:10 +08:00
func (p Program) tyInt1() llvm.Type {
if p.int1Type.IsNil() {
p.int1Type = p.ctx.Int1Type()
}
return p.int1Type
}
func (p Program) tyInt() llvm.Type {
2024-04-15 04:00:38 +08:00
if p.intType.IsNil() {
p.intType = llvmIntType(p.ctx, p.td.PointerSize())
}
return p.intType
}
2024-04-18 15:03:10 +08:00
func llvmIntType(ctx llvm.Context, size int) llvm.Type {
if size <= 4 {
return ctx.Int32Type()
}
return ctx.Int64Type()
}
func (p Program) tyInt8() llvm.Type {
2024-04-15 04:00:38 +08:00
if p.int8Type.IsNil() {
p.int8Type = p.ctx.Int8Type()
}
return p.int8Type
}
2024-04-18 15:03:10 +08:00
func (p Program) tyInt16() llvm.Type {
2024-04-15 04:00:38 +08:00
if p.int16Type.IsNil() {
p.int16Type = p.ctx.Int16Type()
}
return p.int16Type
}
2024-04-18 15:03:10 +08:00
func (p Program) tyInt32() llvm.Type {
2024-04-15 04:00:38 +08:00
if p.int32Type.IsNil() {
p.int32Type = p.ctx.Int32Type()
}
return p.int32Type
}
2024-04-18 15:03:10 +08:00
func (p Program) tyInt64() llvm.Type {
2024-04-15 04:00:38 +08:00
if p.int64Type.IsNil() {
p.int64Type = p.ctx.Int64Type()
}
return p.int64Type
}
/*
2024-05-10 13:25:36 +08:00
func (p Program) toTuple(typ *types.Tuple) Type {
return &aType{p.toLLVMTuple(typ), rawType{typ}, vkTuple}
}
*/
2024-05-10 13:25:36 +08:00
2024-05-05 12:11:51 +08:00
func (p Program) toType(raw types.Type) Type {
typ := rawType{raw}
switch t := raw.(type) {
2024-04-15 04:00:38 +08:00
case *types.Basic:
switch t.Kind() {
2024-04-18 15:03:10 +08:00
case types.Int:
return &aType{p.tyInt(), typ, vkSigned}
case types.Uint, types.Uintptr:
return &aType{p.tyInt(), typ, vkUnsigned}
case types.Bool:
return &aType{p.tyInt1(), typ, vkBool}
case types.Uint8:
return &aType{p.tyInt8(), typ, vkUnsigned}
case types.Int8:
return &aType{p.tyInt8(), typ, vkSigned}
case types.Int16:
return &aType{p.tyInt16(), typ, vkSigned}
case types.Uint16:
return &aType{p.tyInt16(), typ, vkUnsigned}
case types.Int32:
return &aType{p.tyInt32(), typ, vkSigned}
case types.Uint32:
return &aType{p.tyInt32(), typ, vkUnsigned}
case types.Int64:
return &aType{p.tyInt64(), typ, vkSigned}
case types.Uint64:
return &aType{p.tyInt64(), typ, vkUnsigned}
2024-04-15 04:00:38 +08:00
case types.Float32:
2024-04-18 15:03:10 +08:00
return &aType{p.ctx.FloatType(), typ, vkFloat}
2024-04-15 04:00:38 +08:00
case types.Float64:
2024-04-18 15:03:10 +08:00
return &aType{p.ctx.DoubleType(), typ, vkFloat}
2024-04-15 04:00:38 +08:00
case types.Complex64:
return &aType{p.tyComplex64(), typ, vkComplex}
2024-04-15 04:00:38 +08:00
case types.Complex128:
return &aType{p.tyComplex128(), typ, vkComplex}
2024-04-15 04:00:38 +08:00
case types.String:
return &aType{p.rtString(), typ, vkString}
2024-04-15 04:00:38 +08:00
case types.UnsafePointer:
2024-04-28 07:08:01 +08:00
return &aType{p.tyVoidPtr(), typ, vkPtr}
2024-04-15 04:00:38 +08:00
}
2024-04-16 03:05:20 +08:00
case *types.Pointer:
2024-05-05 12:11:51 +08:00
elem := p.rawType(t.Elem())
2024-04-28 07:08:01 +08:00
return &aType{llvm.PointerType(elem.ll, 0), typ, vkPtr}
2024-04-27 13:57:21 +08:00
case *types.Interface:
if t.Empty() {
return &aType{p.rtEface(), typ, vkEface}
}
return &aType{p.rtIface(), typ, vkIface}
2024-04-16 03:05:20 +08:00
case *types.Slice:
2024-05-15 10:29:06 +08:00
return &aType{p.rtSlice(), typ, vkSlice}
2024-04-16 03:05:20 +08:00
case *types.Map:
2024-07-02 21:14:36 +08:00
return &aType{llvm.PointerType(p.rtMap(), 0), typ, vkMap}
2024-04-16 03:05:20 +08:00
case *types.Struct:
2024-05-05 13:29:20 +08:00
ll, kind := p.toLLVMStruct(t)
return &aType{ll, typ, kind}
2024-04-16 03:05:20 +08:00
case *types.Named:
2024-05-05 12:11:51 +08:00
return p.toNamed(t)
case *types.Signature: // represents a C function pointer in raw type
return &aType{p.toLLVMFuncPtr(t), typ, vkFuncPtr}
2024-05-23 01:34:48 +08:00
case *types.Tuple:
return &aType{p.toLLVMTuple(t), typ, vkTuple}
2024-04-16 03:05:20 +08:00
case *types.Array:
2024-05-05 12:11:51 +08:00
elem := p.rawType(t.Elem())
2024-05-15 21:26:53 +08:00
return &aType{llvm.ArrayType(elem.ll, int(t.Len())), typ, vkArray}
2024-04-16 03:05:20 +08:00
case *types.Chan:
2024-07-02 20:12:12 +08:00
return &aType{llvm.PointerType(p.rtChan(), 0), typ, vkChan}
2025-01-08 11:41:32 +08:00
case *types.Alias:
return p.toType(types.Unalias(t))
2025-01-19 19:02:58 +08:00
case *types.TypeParam:
return p.toType(t.Constraint())
2024-04-15 04:00:38 +08:00
}
2024-05-23 01:34:48 +08:00
panic(fmt.Sprintf("toLLVMType: todo - %T\n", raw))
2024-04-15 04:00:38 +08:00
}
2024-05-05 12:11:51 +08:00
func (p Program) toLLVMNamedStruct(name string, raw *types.Struct) llvm.Type {
2024-05-11 12:03:02 +08:00
if typ, ok := p.named[name]; ok {
return typ
}
2024-04-16 03:05:20 +08:00
t := p.ctx.StructCreateNamed(name)
2024-05-11 12:03:02 +08:00
p.named[name] = t
2024-05-05 12:11:51 +08:00
fields := p.toLLVMFields(raw)
2024-04-16 03:05:20 +08:00
t.StructSetBody(fields, false)
return t
}
2024-05-05 13:29:20 +08:00
func (p Program) toLLVMStruct(raw *types.Struct) (ret llvm.Type, kind valueKind) {
2024-05-05 12:11:51 +08:00
fields := p.toLLVMFields(raw)
2024-05-05 13:29:20 +08:00
ret = p.ctx.StructType(fields, false)
if isClosure(raw) {
kind = vkClosure
} else {
kind = vkStruct
2024-05-05 13:29:20 +08:00
}
return
}
func isClosure(raw *types.Struct) bool {
n := raw.NumFields()
if n == 2 {
f1, f2 := raw.Field(0), raw.Field(1)
if _, ok := f1.Type().(*types.Signature); ok && f1.Name() == "$f" {
return f2.Type() == types.Typ[types.UnsafePointer] && f2.Name() == "$data"
2024-05-05 13:29:20 +08:00
}
}
return false
2024-04-16 03:05:20 +08:00
}
2024-05-05 12:11:51 +08:00
func (p Program) toLLVMFields(raw *types.Struct) (fields []llvm.Type) {
n := raw.NumFields()
2024-04-20 13:50:48 +08:00
if n > 0 {
fields = make([]llvm.Type, n)
for i := 0; i < n; i++ {
2024-11-25 11:17:06 +08:00
fields[i] = p.rawType(p.patch(raw.Field(i).Type())).ll
2024-04-20 13:50:48 +08:00
}
2024-04-16 03:05:20 +08:00
}
2024-04-20 13:50:48 +08:00
return
2024-04-16 03:05:20 +08:00
}
2024-04-18 15:03:10 +08:00
func (p Program) toLLVMTuple(t *types.Tuple) llvm.Type {
2024-04-20 13:50:48 +08:00
return p.ctx.StructType(p.toLLVMTypes(t, t.Len()), false)
2024-04-16 03:05:20 +08:00
}
2024-04-20 13:50:48 +08:00
func (p Program) toLLVMTypes(t *types.Tuple, n int) (ret []llvm.Type) {
if n > 0 {
ret = make([]llvm.Type, n)
for i := 0; i < n; i++ {
2024-11-25 11:17:06 +08:00
ret[i] = p.rawType(p.patch(t.At(i).Type())).ll
2024-04-20 13:50:48 +08:00
}
2024-04-16 03:05:20 +08:00
}
2024-04-20 13:50:48 +08:00
return
2024-04-16 03:05:20 +08:00
}
2024-05-05 12:11:51 +08:00
func (p Program) toLLVMFunc(sig *types.Signature) llvm.Type {
tParams := sig.Params()
n := tParams.Len()
2024-11-25 13:28:18 +08:00
hasVArg := hasNameValist(sig)
2024-05-05 12:11:51 +08:00
if hasVArg {
n--
}
2024-05-05 12:11:51 +08:00
params := p.toLLVMTypes(tParams, n)
out := sig.Results()
var ret llvm.Type
switch nret := out.Len(); nret {
case 0:
ret = p.tyVoid()
case 1:
ret = p.rawType(out.At(0).Type()).ll
default:
ret = p.toLLVMTuple(out)
2024-05-04 07:21:07 +08:00
}
2024-05-05 12:11:51 +08:00
return llvm.FunctionType(ret, params, hasVArg)
2024-04-16 03:05:20 +08:00
}
2024-05-05 12:11:51 +08:00
func (p Program) toLLVMFuncPtr(sig *types.Signature) llvm.Type {
ft := p.toLLVMFunc(sig)
return llvm.PointerType(ft, 0)
}
func (p Program) retType(raw *types.Signature) Type {
out := raw.Results()
2024-04-19 00:05:57 +08:00
switch n := out.Len(); n {
case 0:
return p.Void()
case 1:
2024-05-05 12:11:51 +08:00
return p.rawType(out.At(0).Type())
2024-04-19 00:05:57 +08:00
default:
2024-05-05 12:11:51 +08:00
return &aType{p.toLLVMTuple(out), rawType{out}, vkTuple}
2024-04-19 00:05:57 +08:00
}
}
2024-05-31 14:22:17 +08:00
func (p Program) llvmNameOf(named *types.Named) (name string) {
name = NameOf(named)
if obj := named.Obj(); obj != nil && obj.Parent() != nil && obj.Parent() != obj.Pkg().Scope() {
index := p.fnnamed[name]
p.fnnamed[name] = index + 1
name += fmt.Sprintf("#%v", index)
}
return name
}
2024-05-05 12:11:51 +08:00
func (p Program) toNamed(raw *types.Named) Type {
switch t := raw.Underlying().(type) {
2024-04-16 03:05:20 +08:00
case *types.Struct:
2024-05-31 14:22:17 +08:00
name := p.llvmNameOf(raw)
2024-07-04 10:53:33 +08:00
kind := vkStruct
if isClosure(t) {
kind = vkClosure
}
return &aType{p.toLLVMNamedStruct(name, t), rawType{raw}, kind}
2024-04-25 21:44:23 +08:00
default:
2024-05-24 07:38:06 +08:00
typ := p.rawType(t)
return &aType{typ.ll, rawType{raw}, typ.kind}
2024-04-16 03:05:20 +08:00
}
}
2024-04-18 15:03:10 +08:00
2024-05-25 01:11:35 +08:00
// NameOf returns the full name of a named type.
2024-04-27 07:47:10 +08:00
func NameOf(typ *types.Named) string {
2024-08-09 22:23:48 +08:00
return abi.FullName(typ.Obj().Pkg(), abi.NamedName(typ))
2024-04-27 07:47:10 +08:00
}
2024-05-25 01:11:35 +08:00
// FullName returns the full name of a package member.
2024-04-27 07:47:10 +08:00
func FullName(pkg *types.Package, name string) string {
2024-05-25 01:11:35 +08:00
return abi.FullName(pkg, name)
2024-04-27 07:47:10 +08:00
}
2024-05-25 01:11:35 +08:00
// PathOf returns the package path of the specified package.
2024-04-27 07:47:10 +08:00
func PathOf(pkg *types.Package) string {
2024-05-25 01:11:35 +08:00
return abi.PathOf(pkg)
2024-04-27 07:47:10 +08:00
}
// FuncName:
// - func: pkg.name
2024-05-29 09:36:53 +08:00
// - method: pkg.T.name, pkg.(*T).name
2024-08-09 22:23:48 +08:00
func FuncName(pkg *types.Package, name string, recv *types.Var, org bool) string {
if recv != nil {
2025-05-04 21:33:53 +08:00
named, ptr := recvNamed(recv.Type())
var tName string
2024-08-09 22:23:48 +08:00
if org {
2025-05-04 21:33:53 +08:00
tName = named.Obj().Name()
2024-05-29 09:36:53 +08:00
} else {
2025-05-04 21:33:53 +08:00
tName = abi.NamedName(named)
}
if ptr {
tName = "(*" + tName + ")"
}
2024-05-29 09:36:53 +08:00
return PathOf(pkg) + "." + tName + "." + name
}
ret := FullName(pkg, name)
return ret
}
2025-05-04 21:33:53 +08:00
func recvNamed(t types.Type) (typ *types.Named, ptr bool) {
if tp, ok := t.(*types.Pointer); ok {
t = tp.Elem()
ptr = true
}
if _, ok := t.(*types.Alias); ok {
t = types.Unalias(t)
}
typ, _ = t.(*types.Named)
return
}
2024-08-09 22:23:48 +08:00
func TypeArgs(typeArgs []types.Type) string {
return abi.TypeArgs(typeArgs)
}
2024-04-18 15:03:10 +08:00
// -----------------------------------------------------------------------------