Files
llgo/ssa/type.go

316 lines
6.9 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 (
"go/types"
2024-04-20 13:50:48 +08:00
"log"
2024-04-15 04:00:38 +08:00
2024-04-15 05:48:48 +08:00
"github.com/goplus/llvm"
2024-04-15 04:00:38 +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
vkFunc
vkTuple
)
// -----------------------------------------------------------------------------
2024-04-20 13:50:48 +08:00
const (
nameValist = "__llgo_va_list"
)
func VArg() *types.Var {
return types.NewParam(0, nil, nameValist, types.Typ[types.Invalid])
}
func IsVArg(arg *types.Var) bool {
return arg.Name() == nameValist
}
func HasVArg(t *types.Tuple, n int) bool {
return n > 0 && IsVArg(t.At(n-1))
}
2024-04-21 15:12:57 +08:00
func indexType(t types.Type) types.Type {
switch t := t.(type) {
case *types.Slice:
return t.Elem()
case *types.Pointer:
switch t := t.Elem().(type) {
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-04-18 15:03:10 +08:00
type aType struct {
ll llvm.Type
t types.Type
kind valueKind
2024-04-15 04:00:38 +08:00
}
2024-04-18 15:03:10 +08:00
type Type = *aType
2024-04-21 15:12:57 +08:00
func (p Program) Pointer(typ Type) Type {
return p.Type(types.NewPointer(typ.t))
}
func (p Program) Elem(typ Type) Type {
elem := typ.t.(*types.Pointer).Elem()
return p.Type(elem)
}
func (p Program) Index(typ Type) Type {
return p.Type(indexType(typ.t))
}
func (p Program) Type(typ types.Type) Type {
2024-04-15 04:00:38 +08:00
if v := p.typs.At(typ); v != nil {
2024-04-18 15:03:10 +08:00
return v.(Type)
2024-04-15 04:00:38 +08:00
}
2024-04-16 03:05:20 +08:00
ret := p.toLLVMType(typ)
2024-04-15 04:00:38 +08:00
p.typs.Set(typ, ret)
return ret
}
2024-04-18 15:03:10 +08:00
func (p Program) llvmSignature(sig *types.Signature) Type {
2024-04-17 00:01:42 +08:00
if v := p.typs.At(sig); v != nil {
2024-04-18 15:03:10 +08:00
return v.(Type)
2024-04-17 00:01:42 +08:00
}
ret := p.toLLVMFunc(sig)
p.typs.Set(sig, ret)
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-04-18 15:03:10 +08:00
func (p Program) toLLVMType(typ types.Type) Type {
2024-04-15 04:00:38 +08:00
switch t := typ.(type) {
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:
case types.Complex128:
case types.String:
case types.UnsafePointer:
2024-04-18 15:03:10 +08:00
return &aType{p.tyVoidPtr(), typ, vkInvalid}
2024-04-15 04:00:38 +08:00
}
2024-04-16 03:05:20 +08:00
case *types.Pointer:
2024-04-21 15:12:57 +08:00
elem := p.Type(t.Elem())
2024-04-18 15:03:10 +08:00
return &aType{llvm.PointerType(elem.ll, 0), typ, vkInvalid}
2024-04-16 03:05:20 +08:00
case *types.Slice:
case *types.Map:
case *types.Struct:
return p.toLLVMStruct(t)
case *types.Named:
return p.toLLVMNamed(t)
case *types.Signature:
return p.toLLVMFunc(t)
case *types.Array:
2024-04-21 15:12:57 +08:00
elem := p.Type(t.Elem())
2024-04-18 15:03:10 +08:00
return &aType{llvm.ArrayType(elem.ll, int(t.Len())), typ, vkInvalid}
2024-04-16 03:05:20 +08:00
case *types.Chan:
2024-04-15 04:00:38 +08:00
}
2024-04-20 13:50:48 +08:00
log.Println("toLLVMType: todo -", typ)
2024-04-15 04:00:38 +08:00
panic("todo")
}
2024-04-18 15:03:10 +08:00
func (p Program) toLLVMNamedStruct(name string, typ *types.Struct) llvm.Type {
2024-04-16 03:05:20 +08:00
t := p.ctx.StructCreateNamed(name)
fields := p.toLLVMFields(typ)
t.StructSetBody(fields, false)
return t
}
2024-04-18 15:03:10 +08:00
func (p Program) toLLVMStruct(typ *types.Struct) Type {
2024-04-16 03:05:20 +08:00
fields := p.toLLVMFields(typ)
2024-04-18 15:03:10 +08:00
return &aType{p.ctx.StructType(fields, false), typ, vkInvalid}
2024-04-16 03:05:20 +08:00
}
2024-04-20 13:50:48 +08:00
func (p Program) toLLVMFields(typ *types.Struct) (fields []llvm.Type) {
2024-04-16 03:05:20 +08:00
n := typ.NumFields()
2024-04-20 13:50:48 +08:00
if n > 0 {
fields = make([]llvm.Type, n)
for i := 0; i < n; i++ {
2024-04-21 15:12:57 +08:00
fields[i] = p.Type(typ.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-04-21 15:12:57 +08:00
ret[i] = p.Type(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-04-18 15:03:10 +08:00
func (p Program) toLLVMFunc(sig *types.Signature) Type {
2024-04-20 13:50:48 +08:00
tParams := sig.Params()
n := tParams.Len()
hasVArg := HasVArg(tParams, n)
if hasVArg {
n--
}
params := p.toLLVMTypes(tParams, n)
2024-04-19 00:05:57 +08:00
out := sig.Results()
2024-04-16 03:05:20 +08:00
var ret llvm.Type
2024-04-19 00:05:57 +08:00
switch nret := out.Len(); nret {
2024-04-16 03:05:20 +08:00
case 0:
ret = p.tyVoid()
case 1:
2024-04-21 15:12:57 +08:00
ret = p.Type(out.At(0).Type()).ll
2024-04-16 03:05:20 +08:00
default:
2024-04-19 00:05:57 +08:00
ret = p.toLLVMTuple(out)
2024-04-16 03:05:20 +08:00
}
2024-04-20 13:50:48 +08:00
ft := llvm.FunctionType(ret, params, hasVArg)
2024-04-18 15:03:10 +08:00
return &aType{ft, sig, vkFunc}
2024-04-16 03:05:20 +08:00
}
2024-04-19 00:05:57 +08:00
func (p Program) retType(sig *types.Signature) Type {
out := sig.Results()
switch n := out.Len(); n {
case 0:
return p.Void()
case 1:
2024-04-21 15:12:57 +08:00
return p.Type(out.At(0).Type())
2024-04-19 00:05:57 +08:00
default:
return &aType{p.toLLVMTuple(out), out, vkTuple}
}
}
2024-04-18 15:03:10 +08:00
func (p Program) toLLVMNamed(typ *types.Named) Type {
2024-04-16 03:05:20 +08:00
name := typ.Obj().Name()
switch typ := typ.Underlying().(type) {
case *types.Struct:
2024-04-18 15:03:10 +08:00
return &aType{p.toLLVMNamedStruct(name, typ), typ, vkInvalid}
2024-04-16 03:05:20 +08:00
}
panic("todo")
}
2024-04-18 15:03:10 +08:00
// -----------------------------------------------------------------------------