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

@@ -0,0 +1,18 @@
package main
import (
"github.com/goplus/llgo/_demo/interf/foo"
)
func main() {
if x, ok := foo.Bar().(struct{ V int }); ok {
println(x.V)
} else {
println("Bar: not ok")
}
if x, ok := foo.F().(struct{ v int }); ok {
println(x.v)
} else {
println("F: not ok")
}
}

View File

View File

@@ -352,9 +352,9 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, do
last = len(instrs) - 1 last = len(instrs) - 1
instrs = instrs[:last] instrs = instrs[:last]
} else { } else {
// TODO(xsw): confirm pyMod don't need to call LoadPyModSyms // TODO(xsw): confirm pyMod don't need to call AfterInit
p.inits = append(p.inits, func() { p.inits = append(p.inits, func() {
pkg.PyLoadModSyms(b, ret) pkg.AfterInit(b, ret)
}) })
} }
} else if doMainInit { } else if doMainInit {

View File

@@ -28,11 +28,9 @@ func testCompile(t *testing.T, src, expected string) {
cltest.TestCompileEx(t, src, "foo.go", expected) cltest.TestCompileEx(t, src, "foo.go", expected)
} }
/*
func TestFromTestgo(t *testing.T) { func TestFromTestgo(t *testing.T) {
cltest.FromDir(t, "strucintf", "./_testgo", false) cltest.FromDir(t, "strucintf", "./_testgo", false)
} }
*/
func TestFromTestpy(t *testing.T) { func TestFromTestpy(t *testing.T) {
cltest.FromDir(t, "", "./_testpy", false) cltest.FromDir(t, "", "./_testpy", false)

9
cl/internal/foo/foo.go Normal file
View File

@@ -0,0 +1,9 @@
package foo
func Bar() any {
return struct{ V int }{1}
}
func F() any {
return struct{ v int }{1}
}

View File

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

View File

@@ -8,21 +8,20 @@ import (
"unsafe" "unsafe"
) )
type iface struct {
tab *itab
data unsafe.Pointer
}
/*
type eface struct { type eface struct {
_type *_type _type *_type
data unsafe.Pointer data unsafe.Pointer
} }
/*
func efaceOf(ep *any) *eface { func efaceOf(ep *any) *eface {
return (*eface)(unsafe.Pointer(ep)) return (*eface)(unsafe.Pointer(ep))
} }
*/
type iface struct {
tab *itab
data unsafe.Pointer
}
// layout of Itab known to compilers // layout of Itab known to compilers
// allocated in non-garbage-collected memory // allocated in non-garbage-collected memory
@@ -35,3 +34,11 @@ type itab struct {
_ [4]byte _ [4]byte
fun [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter. 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. // TracePanic prints panic message.
func TracePanic(v Interface) { func TracePanic(v Eface) {
kind := abi.Kind(v.tab._type.Kind_) kind := abi.Kind(v._type.Kind_)
switch { switch {
case kind == abi.String: case kind == abi.String:
stringTracef(c.Stderr, c.Str("panic: %s\n"), *(*String)(v.data)) 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) print("[", s.len, "/", s.cap, "]", s.data)
} }
func PrintIface(i Interface) { func PrintEface(e Eface) {
print("(", i.tab, ",", i.data, ")") print("(", e._type, ",", e.data, ")")
} }

View File

@@ -28,46 +28,70 @@ import (
type Builder struct { type Builder struct {
h hash.Hash h hash.Hash
buf []byte buf []byte
Pkg string
} }
// New creates a new ABI type Builder. // New creates a new ABI type Builder.
func New() *Builder { func New(pkg string) *Builder {
h := sha256.New() ret := new(Builder)
buf := make([]byte, sha256.Size) ret.Init(pkg)
return &Builder{h, buf} return ret
}
func (b *Builder) Init(pkg string) {
b.Pkg = pkg
b.h = sha256.New()
b.buf = make([]byte, sha256.Size)
} }
// TypeName returns the ABI type name for the specified type. // TypeName returns the ABI type name for the specified type.
func (b *Builder) TypeName(t types.Type) string { func (b *Builder) TypeName(t types.Type) (ret string, private bool) {
switch t := t.(type) { switch t := t.(type) {
case *types.Basic: case *types.Basic:
return t.Name() return BasicName(t), false
case *types.Pointer: case *types.Pointer:
return "*" + b.TypeName(t.Elem()) ret, private = b.TypeName(t.Elem())
return "*" + ret, private
case *types.Struct: case *types.Struct:
return b.StructName(t) return b.StructName(t)
} }
panic("todo") panic("todo")
} }
// StructName returns the ABI type name for the specified struct type. func BasicName(t *types.Basic) string {
func (b *Builder) StructName(t *types.Struct) string { return "_llgo_" + t.Name()
hash := b.structHash(t)
return "struct$" + base64.RawURLEncoding.EncodeToString(hash)
} }
func (b *Builder) structHash(t *types.Struct) []byte { // StructName returns the ABI type name for the specified struct type.
func (b *Builder) StructName(t *types.Struct) (ret string, private bool) {
hash, private := b.structHash(t)
hashStr := base64.RawURLEncoding.EncodeToString(hash)
if private {
return b.Pkg + ".struct$" + hashStr, true
}
return "_llgo_struct$" + hashStr, false
}
func (b *Builder) structHash(t *types.Struct) (ret []byte, private bool) {
h := b.h h := b.h
h.Reset() h.Reset()
n := t.NumFields() n := t.NumFields()
fmt.Fprintln(h, "struct", n) fmt.Fprintln(h, "struct", n)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
f := t.Field(i) f := t.Field(i)
if !f.Exported() {
private = true
}
name := f.Name() name := f.Name()
if f.Embedded() { if f.Embedded() {
name = "-" name = "-"
} }
fmt.Fprintln(h, name, b.TypeName(f.Type())) ft, fpriv := b.TypeName(f.Type())
if fpriv {
private = true
} }
return h.Sum(b.buf[:0]) fmt.Fprintln(h, name, ft)
}
ret = h.Sum(b.buf[:0])
return
} }

View File

@@ -70,9 +70,8 @@ func (b Builder) StringData(x Expr) Expr {
if debugInstr { if debugInstr {
log.Printf("StringData %v\n", x.impl) log.Printf("StringData %v\n", x.impl)
} }
prog := b.Prog
ptr := llvm.CreateExtractValue(b.impl, x.impl, 0) ptr := llvm.CreateExtractValue(b.impl, x.impl, 0)
return Expr{ptr, prog.CStr()} return Expr{ptr, b.Prog.CStr()}
} }
// StringLen returns the length of a string. // StringLen returns the length of a string.
@@ -80,9 +79,8 @@ func (b Builder) StringLen(x Expr) Expr {
if debugInstr { if debugInstr {
log.Printf("StringLen %v\n", x.impl) log.Printf("StringLen %v\n", x.impl)
} }
prog := b.Prog
ptr := llvm.CreateExtractValue(b.impl, x.impl, 1) ptr := llvm.CreateExtractValue(b.impl, x.impl, 1)
return Expr{ptr, prog.Int()} return Expr{ptr, b.Prog.Int()}
} }
// SliceData returns the data pointer of a slice. // SliceData returns the data pointer of a slice.
@@ -90,9 +88,8 @@ func (b Builder) SliceData(x Expr) Expr {
if debugInstr { if debugInstr {
log.Printf("SliceData %v\n", x.impl) log.Printf("SliceData %v\n", x.impl)
} }
prog := b.Prog
ptr := llvm.CreateExtractValue(b.impl, x.impl, 0) ptr := llvm.CreateExtractValue(b.impl, x.impl, 0)
return Expr{ptr, prog.CStr()} return Expr{ptr, b.Prog.VoidPtr()}
} }
// SliceLen returns the length of a slice. // SliceLen returns the length of a slice.
@@ -100,9 +97,8 @@ func (b Builder) SliceLen(x Expr) Expr {
if debugInstr { if debugInstr {
log.Printf("SliceLen %v\n", x.impl) log.Printf("SliceLen %v\n", x.impl)
} }
prog := b.Prog
ptr := llvm.CreateExtractValue(b.impl, x.impl, 1) ptr := llvm.CreateExtractValue(b.impl, x.impl, 1)
return Expr{ptr, prog.Int()} return Expr{ptr, b.Prog.Int()}
} }
// SliceCap returns the length of a slice cap. // SliceCap returns the length of a slice cap.
@@ -110,9 +106,8 @@ func (b Builder) SliceCap(x Expr) Expr {
if debugInstr { if debugInstr {
log.Printf("SliceCap %v\n", x.impl) log.Printf("SliceCap %v\n", x.impl)
} }
prog := b.Prog
ptr := llvm.CreateExtractValue(b.impl, x.impl, 2) ptr := llvm.CreateExtractValue(b.impl, x.impl, 2)
return Expr{ptr, prog.Int()} return Expr{ptr, b.Prog.Int()}
} }
// The IndexAddr instruction yields the address of the element at // The IndexAddr instruction yields the address of the element at
@@ -188,7 +183,7 @@ func (b Builder) Index(x, idx Expr, addr func(Expr) Expr) Expr {
if addr != nil { if addr != nil {
ptr = addr(x) ptr = addr(x)
} else { } else {
size := b.SizeOf(telem, t.Len()) size := SizeOf(prog, telem, t.Len())
ptr = b.Alloca(size) ptr = b.Alloca(size)
b.Store(ptr, x) b.Store(ptr, x)
} }
@@ -255,7 +250,7 @@ func (b Builder) Slice(x, low, high, max Expr) (ret Expr) {
ret.impl = b.InlineCall(b.Pkg.rtFunc("NewStringSlice"), x, low, high).impl ret.impl = b.InlineCall(b.Pkg.rtFunc("NewStringSlice"), x, low, high).impl
return return
case *types.Slice: case *types.Slice:
nEltSize = b.SizeOf(prog.Index(x.Type)) nEltSize = SizeOf(prog, prog.Index(x.Type))
nCap = b.SliceCap(x) nCap = b.SliceCap(x)
if high.IsNil() { if high.IsNil() {
high = b.SliceCap(x) high = b.SliceCap(x)
@@ -268,7 +263,7 @@ func (b Builder) Slice(x, low, high, max Expr) (ret Expr) {
case *types.Array: case *types.Array:
elem := prog.rawType(te.Elem()) elem := prog.rawType(te.Elem())
ret.Type = prog.Slice(elem) ret.Type = prog.Slice(elem)
nEltSize = b.SizeOf(elem) nEltSize = SizeOf(prog, elem)
nCap = prog.IntVal(uint64(te.Len()), prog.Int()) nCap = prog.IntVal(uint64(te.Len()), prog.Int())
if high.IsNil() { if high.IsNil() {
high = nCap high = nCap
@@ -283,6 +278,18 @@ func (b Builder) Slice(x, low, high, max Expr) (ret Expr) {
return return
} }
// SliceLit creates a new slice with the specified elements.
func (b Builder) SliceLit(t Type, elts ...Expr) Expr {
prog := b.Prog
telem := prog.Index(t)
ptr := b.AllocU(telem, int64(len(elts)))
for i, elt := range elts {
b.Store(b.Advance(ptr, prog.Val(i)), elt)
}
size := llvm.ConstInt(prog.tyInt(), uint64(len(elts)), false)
return b.unsafeSlice(ptr, size, size)
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// The MakeMap instruction creates a new hash-table-based map object // The MakeMap instruction creates a new hash-table-based map object
@@ -323,10 +330,11 @@ func (b Builder) MakeSlice(t Type, len, cap Expr) (ret Expr) {
log.Printf("MakeSlice %v, %v, %v\n", t.RawType(), len.impl, cap.impl) log.Printf("MakeSlice %v, %v, %v\n", t.RawType(), len.impl, cap.impl)
} }
pkg := b.Pkg pkg := b.Pkg
prog := b.Prog
if cap.IsNil() { if cap.IsNil() {
cap = len cap = len
} }
elemSize := b.SizeOf(b.Prog.Index(t)) elemSize := SizeOf(prog, prog.Index(t))
size := b.BinOp(token.MUL, cap, elemSize) size := b.BinOp(token.MUL, cap, elemSize)
ptr := b.InlineCall(pkg.rtFunc("AllocZ"), size) ptr := b.InlineCall(pkg.rtFunc("AllocZ"), size)
ret.impl = b.InlineCall(pkg.rtFunc("NewSlice"), ptr, len, cap).impl ret.impl = b.InlineCall(pkg.rtFunc("NewSlice"), ptr, len, cap).impl

View File

@@ -58,6 +58,13 @@ type aNamedConst struct {
// it augments with the name and position of its 'const' declaration. // it augments with the name and position of its 'const' declaration.
type NamedConst = *aNamedConst type NamedConst = *aNamedConst
/*
// NewConst creates a new named constant.
func (p Package) NewConst(name string, val constant.Value) NamedConst {
return &aNamedConst{}
}
*/
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
type aGlobal struct { type aGlobal struct {
@@ -75,9 +82,21 @@ func (p Package) NewVar(name string, typ types.Type, bg Background) Global {
return v return v
} }
t := p.Prog.Type(typ, bg) t := p.Prog.Type(typ, bg)
return p.doNewVar(name, t)
}
// NewVarFrom creates a new global variable.
func (p Package) NewVarFrom(name string, t Type) Global {
if v, ok := p.vars[name]; ok {
return v
}
return p.doNewVar(name, t)
}
func (p Package) doNewVar(name string, t Type) Global {
var gbl llvm.Value var gbl llvm.Value
var array bool var array bool
if t.kind == vkPtr && p.Prog.Elem(t).kind == vkArray { if t.kind == vkPtr && p.Prog.Elem(t).kind == vkArray { // TODO(xsw): check this code
typ := p.Prog.Elem(t).ll typ := p.Prog.Elem(t).ll
gbl = llvm.AddGlobal(p.mod, typ, name) gbl = llvm.AddGlobal(p.mod, typ, name)
gbl.SetInitializer(llvm.Undef(typ)) gbl.SetInitializer(llvm.Undef(typ))
@@ -97,7 +116,7 @@ func (p Package) VarOf(name string) Global {
// Init initializes the global variable with the given value. // Init initializes the global variable with the given value.
func (g Global) Init(v Expr) { func (g Global) Init(v Expr) {
if g.array && v.kind == vkPtr { if g.array && v.kind == vkPtr { // TODO(xsw): check this code
return return
} }
g.impl.SetInitializer(v.impl) g.impl.SetInitializer(v.impl)
@@ -350,15 +369,14 @@ func (p Package) PyObjOf(name string) PyObjRef {
return p.pyobjs[name] return p.pyobjs[name]
} }
// PyLoadModSyms loads module symbols used in this package. func (p Package) pyHasModSyms() bool {
func (p Package) PyLoadModSyms(b Builder, ret BasicBlock) { return len(p.pyobjs) > 0
objs := p.pyobjs
n := len(objs)
if n == 0 {
return
} }
names := make([]string, 0, n) // pyLoadModSyms loads module symbols used in this package.
func (p Package) pyLoadModSyms(b Builder) {
objs := p.pyobjs
names := make([]string, 0, len(objs))
for name := range objs { for name := range objs {
names = append(names, name) names = append(names, name)
} }
@@ -376,7 +394,6 @@ func (p Package) PyLoadModSyms(b Builder, ret BasicBlock) {
} }
} }
b.SetBlockEx(ret, afterInit)
for _, modName := range modNames { for _, modName := range modNames {
objs := mods[modName] objs := mods[modName]
b.PyLoadModSyms(modName, objs...) b.PyLoadModSyms(modName, objs...)

View File

@@ -180,13 +180,6 @@ func (b Builder) Const(v constant.Value, typ Type) Expr {
panic(fmt.Sprintf("unsupported Const: %v, %v", v, raw)) panic(fmt.Sprintf("unsupported Const: %v, %v", v, raw))
} }
// SizeOf returns the size of a type.
func (b Builder) SizeOf(t Type, n ...int64) Expr {
prog := b.Prog
size := prog.SizeOf(t, n...)
return prog.IntVal(size, prog.Uintptr())
}
// CStr returns a c-style string constant expression. // CStr returns a c-style string constant expression.
func (b Builder) CStr(v string) Expr { func (b Builder) CStr(v string) Expr {
return Expr{llvm.CreateGlobalStringPtr(b.impl, v), b.Prog.CStr()} return Expr{llvm.CreateGlobalStringPtr(b.impl, v), b.Prog.CStr()}
@@ -200,10 +193,17 @@ func (b Builder) Str(v string) (ret Expr) {
return Expr{aggregateValue(b.impl, prog.rtString(), data, size), prog.String()} return Expr{aggregateValue(b.impl, prog.rtString(), data, size), prog.String()}
} }
// unsafe.String(data *byte, size int) string // unsafeString(data *byte, size int) string
func (b Builder) String(data, size Expr) Expr { func (b Builder) unsafeString(data, size llvm.Value) Expr {
prog := b.Prog prog := b.Prog
return Expr{aggregateValue(b.impl, prog.rtString(), data.impl, size.impl), prog.String()} return Expr{aggregateValue(b.impl, prog.rtString(), data, size), prog.String()}
}
// unsafeSlice(data *T, size, cap int) []T
func (b Builder) unsafeSlice(data Expr, size, cap llvm.Value) Expr {
prog := b.Prog
tslice := prog.Slice(prog.Elem(data.Type))
return Expr{aggregateValue(b.impl, prog.rtSlice(), data.impl, size, cap), tslice}
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@@ -667,7 +667,7 @@ func (b Builder) Alloc(elem Type, heap bool) (ret Expr) {
} }
prog := b.Prog prog := b.Prog
pkg := b.Pkg pkg := b.Pkg
size := b.SizeOf(elem) size := SizeOf(prog, elem)
if heap { if heap {
ret = b.InlineCall(pkg.rtFunc("AllocZ"), size) ret = b.InlineCall(pkg.rtFunc("AllocZ"), size)
} else { } else {
@@ -683,9 +683,10 @@ func (b Builder) AllocU(elem Type, n ...int64) (ret Expr) {
if debugInstr { if debugInstr {
log.Printf("AllocU %v, %v\n", elem.raw.Type, n) log.Printf("AllocU %v, %v\n", elem.raw.Type, n)
} }
size := b.SizeOf(elem, n...) prog := b.Prog
size := SizeOf(prog, elem, n...)
ret = b.InlineCall(b.Pkg.rtFunc("AllocU"), size) ret = b.InlineCall(b.Pkg.rtFunc("AllocU"), size)
ret.Type = b.Prog.Pointer(elem) ret.Type = prog.Pointer(elem)
return return
} }
@@ -1035,8 +1036,8 @@ func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) {
typ = prog.VoidPtr() typ = prog.VoidPtr()
case vkString: case vkString:
fn = "PrintString" fn = "PrintString"
case vkInterface: case vkEface:
fn = "PrintIface" fn = "PrintEface"
// case vkComplex: // case vkComplex:
// fn = "PrintComplex" // fn = "PrintComplex"
default: default:
@@ -1067,7 +1068,10 @@ func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) {
} }
} }
case "String": // unsafe.String case "String": // unsafe.String
return b.String(args[0], args[1]) return b.unsafeString(args[0].impl, args[1].impl)
case "Slice": // unsafe.Slice
size := args[1].impl
return b.unsafeSlice(args[0], size, size)
} }
panic("todo: " + fn) panic("todo: " + fn)
} }

View File

@@ -21,31 +21,72 @@ import (
"go/types" "go/types"
"log" "log"
"github.com/goplus/llgo/internal/abi" "github.com/goplus/llgo/ssa/abi"
"github.com/goplus/llvm" "github.com/goplus/llvm"
) )
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// abiBasic returns the abi type of the specified basic kind. // abiBasic returns the abi type of the specified basic kind.
func (b Builder) abiBasic(kind types.BasicKind) Expr { func (b Builder) abiBasic(t *types.Basic) Expr {
return b.InlineCall(b.Pkg.rtFunc("Basic"), b.Prog.Val(int(kind))) name := abi.BasicName(t)
g := b.Pkg.NewVarFrom(name, b.Prog.AbiTypePtrPtr())
return g.Expr
} }
/*
// abiStruct returns the abi type of the specified struct type. // abiStruct returns the abi type of the specified struct type.
func (b Builder) abiStruct(t *types.Struct) Expr { func (b Builder) abiStruct(t *types.Struct) Expr {
// name := "__llgo_" + b.Prog.abi.StructName(t) pkg := b.Pkg
name, _ := pkg.abi.StructName(t)
if v := pkg.VarOf(name); v != nil {
return v.Expr
}
prog := b.Prog
g := pkg.doNewVar(name, prog.AbiTypePtrPtr())
g.Init(prog.Null(g.Type))
pkg.abitys = append(pkg.abitys, func() {
b.structOf(t)
})
return g.Expr
} }
*/
// AbiType returns the abi type of the specified type. // func Struct(size uintptr, pkgPath string, fields []abi.StructField) *abi.Type
func (b Builder) AbiType(t Type) Expr { func (b Builder) structOf(t *types.Struct) Expr {
switch tx := t.raw.Type.(type) { pkg := b.Pkg
prog := b.Prog
n := t.NumFields()
flds := make([]Expr, n)
strucAbi := pkg.rtFunc("Struct")
sfAbi := pkg.rtFunc("StructField")
typ := prog.rawType(t)
for i := 0; i < n; i++ {
f := t.Field(i)
off := uintptr(prog.OffsetOf(typ, i))
flds[i] = b.structField(sfAbi, prog, f, off, t.Tag(i))
}
pkgPath := prog.Val(pkg.abi.Pkg)
params := strucAbi.raw.Type.(*types.Signature).Params()
tSlice := prog.rawType(params.At(params.Len() - 1).Type().(*types.Slice))
fldSlice := b.SliceLit(tSlice, flds...)
return b.Call(strucAbi, pkgPath, fldSlice)
}
// func StructField(name string, typ *abi.Type, off uintptr, tag string, exported, embedded bool) abi.StructField
func (b Builder) structField(sfAbi Expr, prog Program, f *types.Var, offset uintptr, tag string) Expr {
name := prog.Val(f.Name())
typ := b.abiType(f.Type())
exported := prog.Val(f.Exported())
embedded := prog.Val(f.Embedded())
return b.Call(sfAbi, name, typ, prog.Val(offset), prog.Val(tag), exported, embedded)
}
// abiType returns the abi type of the specified type.
func (b Builder) abiType(raw types.Type) Expr {
switch tx := raw.(type) {
case *types.Basic: case *types.Basic:
return b.abiBasic(tx.Kind()) return b.abiBasic(tx)
//case *types.Struct: case *types.Struct:
// return b.abiStruct(tx) return b.abiStruct(tx)
} }
panic("todo") panic("todo")
} }
@@ -94,14 +135,14 @@ func (b Builder) MakeInterface(tinter Type, x Expr) (ret Expr) {
case kind == types.String: case kind == types.String:
return Expr{b.InlineCall(b.Pkg.rtFunc("MakeAnyString"), x).impl, tinter} return Expr{b.InlineCall(b.Pkg.rtFunc("MakeAnyString"), x).impl, tinter}
} }
/* case *types.Struct: case *types.Struct:
size := int(prog.SizeOf(typ)) size := int(prog.SizeOf(typ))
if size > prog.PointerSize() { if size > prog.PointerSize() {
return b.makeIntfAlloc(tinter, rawIntf, typ, x) return b.makeIntfAlloc(tinter, rawIntf, typ, x)
} }
tv := prog.ctx.IntType(size * 8) tv := prog.ctx.IntType(size * 8)
iv := llvm.CreateBitCast(b.impl, x.impl, tv) iv := llvm.CreateBitCast(b.impl, x.impl, tv)
return b.makeIntfByIntptr(tinter, rawIntf, typ, iv) */ return b.makeIntfByIntptr(tinter, rawIntf, typ, iv)
} }
panic("todo") panic("todo")
} }
@@ -114,7 +155,7 @@ func (b Builder) makeIntfAlloc(tinter Type, rawIntf *types.Interface, typ Type,
func (b Builder) makeIntfByPtr(tinter Type, rawIntf *types.Interface, typ Type, vptr Expr) (ret Expr) { func (b Builder) makeIntfByPtr(tinter Type, rawIntf *types.Interface, typ Type, vptr Expr) (ret Expr) {
if rawIntf.Empty() { if rawIntf.Empty() {
ret = b.InlineCall(b.Pkg.rtFunc("MakeAny"), b.AbiType(typ), vptr) ret = b.InlineCall(b.Pkg.rtFunc("MakeAny"), b.abiType(typ.raw.Type), vptr)
ret.Type = tinter ret.Type = tinter
return return
} }
@@ -125,7 +166,7 @@ func (b Builder) makeIntfByIntptr(tinter Type, rawIntf *types.Interface, typ Typ
if rawIntf.Empty() { if rawIntf.Empty() {
tptr := b.Prog.Uintptr() tptr := b.Prog.Uintptr()
x = llvm.CreateIntCast(b.impl, x, tptr.ll) x = llvm.CreateIntCast(b.impl, x, tptr.ll)
impl := b.InlineCall(b.Pkg.rtFunc("MakeAnyIntptr"), b.AbiType(typ), Expr{x, tptr}).impl impl := b.InlineCall(b.Pkg.rtFunc("MakeAnyIntptr"), b.abiType(typ.raw.Type), Expr{x, tptr}).impl
return Expr{impl, tinter} return Expr{impl, tinter}
} }
panic("todo") panic("todo")
@@ -183,13 +224,14 @@ func (b Builder) TypeAssert(x Expr, assertedTyp Type, commaOk bool) (ret Expr) {
} }
fn := pkg.rtFunc(fnName) fn := pkg.rtFunc(fnName)
var kind types.BasicKind var kind types.BasicKind
var typ Expr
switch t := assertedTyp.raw.Type.(type) { switch t := assertedTyp.raw.Type.(type) {
case *types.Basic: case *types.Basic:
kind = t.Kind() kind = t.Kind()
typ = b.abiBasic(t)
default: default:
panic("todo") panic("todo")
} }
typ := b.InlineCall(pkg.rtFunc("Basic"), b.Prog.Val(int(kind)))
ret = b.InlineCall(fn, x, typ) ret = b.InlineCall(fn, x, typ)
if kind != types.Uintptr { if kind != types.Uintptr {
conv := func(v llvm.Value) llvm.Value { conv := func(v llvm.Value) llvm.Value {
@@ -226,11 +268,21 @@ func (b Builder) TypeAssert(x Expr, assertedTyp Type, commaOk bool) (ret Expr) {
if commaOk { if commaOk {
fnName = "CheckI2String" fnName = "CheckI2String"
} }
fn := pkg.rtFunc(fnName) return b.InlineCall(pkg.rtFunc(fnName), x)
typ := b.InlineCall(pkg.rtFunc("Basic"), b.Prog.Val(int(abi.String))) case vkStruct:
return b.InlineCall(fn, x, typ)
} }
panic("todo") panic("todo")
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// InterfaceData returns the data pointer of an interface.
func (b Builder) InterfaceData(x Expr) Expr {
if debugInstr {
log.Printf("InterfaceData %v\n", x.impl)
}
ptr := llvm.CreateExtractValue(b.impl, x.impl, 1)
return Expr{ptr, b.Prog.VoidPtr()}
}
// -----------------------------------------------------------------------------

View File

@@ -20,6 +20,7 @@ import (
"go/token" "go/token"
"go/types" "go/types"
"github.com/goplus/llgo/ssa/abi"
"github.com/goplus/llvm" "github.com/goplus/llvm"
"golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/go/types/typeutil"
) )
@@ -99,7 +100,6 @@ type aProgram struct {
ctx llvm.Context ctx llvm.Context
typs typeutil.Map // rawType -> Type typs typeutil.Map // rawType -> Type
gocvt goTypes gocvt goTypes
//abi *abi.Builder
rt *types.Package rt *types.Package
rtget func() *types.Package rtget func() *types.Package
@@ -144,6 +144,7 @@ type aProgram struct {
u64Ty Type u64Ty Type
pyObjPtr Type pyObjPtr Type
pyObjPPtr Type pyObjPPtr Type
abiTyptr Type
pyImpTy *types.Signature pyImpTy *types.Signature
pyNewList *types.Signature pyNewList *types.Signature
@@ -184,7 +185,7 @@ func NewProgram(target *Target) Program {
*/ */
is32Bits := (td.PointerSize() == 4 || target.GOARCH == "x86") // TODO(xsw): remove temp code is32Bits := (td.PointerSize() == 4 || target.GOARCH == "x86") // TODO(xsw): remove temp code
return &aProgram{ return &aProgram{
ctx: ctx, gocvt: newGoTypes(), // abi: abi.New(), ctx: ctx, gocvt: newGoTypes(),
target: target, td: td, is32Bits: is32Bits, target: target, td: td, is32Bits: is32Bits,
named: make(map[string]llvm.Type), named: make(map[string]llvm.Type),
} }
@@ -243,9 +244,9 @@ func (p Program) rtType(name string) Type {
return p.rawType(p.rtNamed(name)) return p.rawType(p.rtNamed(name))
} }
func (p Program) rtIface() llvm.Type { func (p Program) rtEface() llvm.Type {
if p.rtIfaceTy.IsNil() { if p.rtIfaceTy.IsNil() {
p.rtIfaceTy = p.rtType("Interface").ll p.rtIfaceTy = p.rtType("Eface").ll
} }
return p.rtIfaceTy return p.rtIfaceTy
} }
@@ -284,7 +285,23 @@ func (p Program) NewPackage(name, pkgPath string) Package {
p.NeedRuntime = false p.NeedRuntime = false
// Don't need reset p.needPyInit here // Don't need reset p.needPyInit here
// p.needPyInit = false // p.needPyInit = false
return &aPackage{mod, gbls, fns, stubs, pyobjs, pymods, p} ret := &aPackage{
mod: mod, vars: gbls, fns: fns, stubs: stubs,
pyobjs: pyobjs, pymods: pymods, Prog: p}
return ret
}
// AbiTypePtr returns *abi.Type.
func (p Program) AbiTypePtr() Type {
if p.abiTyptr == nil {
p.abiTyptr = p.rawType(types.NewPointer(p.rtNamed("Type")))
}
return p.abiTyptr
}
// AbiTypePtr returns **abi.Type.
func (p Program) AbiTypePtrPtr() Type {
return p.Pointer(p.AbiTypePtr())
} }
// PyObjectPtrPtr returns the **py.Object type. // PyObjectPtrPtr returns the **py.Object type.
@@ -440,6 +457,8 @@ func (p Program) Uint64() Type {
// and unspecified other things too. // and unspecified other things too.
type aPackage struct { type aPackage struct {
mod llvm.Module mod llvm.Module
abi abi.Builder
abitys []func()
vars map[string]Global vars map[string]Global
fns map[string]Function fns map[string]Function
stubs map[string]Function stubs map[string]Function
@@ -450,13 +469,6 @@ type aPackage struct {
type Package = *aPackage type Package = *aPackage
/*
// NewConst creates a new named constant.
func (p Package) NewConst(name string, val constant.Value) NamedConst {
return &aNamedConst{}
}
*/
func (p Package) rtFunc(fnName string) Expr { func (p Package) rtFunc(fnName string) Expr {
fn := p.Prog.runtime().Scope().Lookup(fnName).(*types.Func) fn := p.Prog.runtime().Scope().Lookup(fnName).(*types.Func)
name := FullName(fn.Pkg(), fnName) name := FullName(fn.Pkg(), fnName)
@@ -509,6 +521,15 @@ func (p Package) String() string {
return p.mod.String() return p.mod.String()
} }
// AfterInit is called after the package is initialized (init all packages that depends on).
func (p Package) AfterInit(b Builder, ret BasicBlock) {
doAfterInit := p.pyHasModSyms()
if doAfterInit {
b.SetBlockEx(ret, afterInit)
p.pyLoadModSyms(b)
}
}
/* /*
type CodeGenFileType = llvm.CodeGenFileType type CodeGenFileType = llvm.CodeGenFileType

View File

@@ -49,7 +49,9 @@ const (
vkSlice vkSlice
vkArray vkArray
vkMap vkMap
vkInterface vkEface
vkIface
vkStruct
vkPhisExpr = -1 vkPhisExpr = -1
) )
@@ -99,6 +101,22 @@ func (p Program) SizeOf(typ Type, n ...int64) uint64 {
return size 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 { func (p Program) PointerSize() int {
return p.td.PointerSize() return p.td.PointerSize()
} }
@@ -113,7 +131,6 @@ func (p Program) Pointer(typ Type) Type {
func (p Program) Elem(typ Type) Type { func (p Program) Elem(typ Type) Type {
elem := typ.raw.Type.(interface { elem := typ.raw.Type.(interface {
types.Type
Elem() types.Type Elem() types.Type
}).Elem() }).Elem()
return p.rawType(elem) return p.rawType(elem)
@@ -247,7 +264,9 @@ func (p Program) toType(raw types.Type) Type {
elem := p.rawType(t.Elem()) elem := p.rawType(t.Elem())
return &aType{llvm.PointerType(elem.ll, 0), typ, vkPtr} return &aType{llvm.PointerType(elem.ll, 0), typ, vkPtr}
case *types.Interface: case *types.Interface:
return &aType{p.rtIface(), typ, vkInterface} if t.Empty() {
return &aType{p.rtEface(), typ, vkEface}
}
case *types.Slice: case *types.Slice:
return &aType{p.rtSlice(), typ, vkSlice} return &aType{p.rtSlice(), typ, vkSlice}
case *types.Map: case *types.Map:
@@ -283,6 +302,8 @@ func (p Program) toLLVMStruct(raw *types.Struct) (ret llvm.Type, kind valueKind)
ret = p.ctx.StructType(fields, false) ret = p.ctx.StructType(fields, false)
if isClosure(raw) { if isClosure(raw) {
kind = vkClosure kind = vkClosure
} else {
kind = vkStruct
} }
return return
} }