build: fix unsafe.Sizeof for llgo:type C
This commit is contained in:
@@ -236,6 +236,10 @@ func (p Program) SetRuntime(runtime any) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p Program) SetTypeBackground(fullName string, bg Background) {
|
||||
p.gocvt.typbg[fullName] = bg
|
||||
}
|
||||
|
||||
func (p Program) runtime() *types.Package {
|
||||
if p.rt == nil {
|
||||
p.rt = p.rtget()
|
||||
|
||||
22
ssa/type.go
22
ssa/type.go
@@ -103,7 +103,7 @@ func (p *goProgram) Offsetsof(fields []*types.Var) (ret []int64) {
|
||||
ret = p.sizes.Offsetsof(fields)
|
||||
for i, f := range fields {
|
||||
ret[i] += extra
|
||||
extra += extraSize(f.Type(), ptrSize)
|
||||
extra += p.extraSize(f.Type(), ptrSize)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -114,26 +114,34 @@ func (p *goProgram) Offsetsof(fields []*types.Var) (ret []int64) {
|
||||
func (p *goProgram) Sizeof(T types.Type) int64 {
|
||||
prog := Program(p)
|
||||
ptrSize := int64(prog.PointerSize())
|
||||
baseSize := prog.sizes.Sizeof(T) + extraSize(T, ptrSize)
|
||||
if _, ok := T.Underlying().(*types.Struct); ok {
|
||||
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
|
||||
}
|
||||
|
||||
func extraSize(t types.Type, ptrSize int64) (ret int64) {
|
||||
switch t := t.Underlying().(type) {
|
||||
func (p *goProgram) extraSize(typ types.Type, ptrSize int64) (ret int64) {
|
||||
retry:
|
||||
switch t := typ.(type) {
|
||||
case *types.Named:
|
||||
if p.gocvt.typbg[t.String()] == InC {
|
||||
return 0
|
||||
}
|
||||
typ = t.Underlying()
|
||||
goto retry
|
||||
case *types.Signature:
|
||||
return ptrSize
|
||||
case *types.Struct:
|
||||
n := t.NumFields()
|
||||
for i := 0; i < n; i++ {
|
||||
f := t.Field(i)
|
||||
ret += extraSize(f.Type(), ptrSize)
|
||||
ret += p.extraSize(f.Type(), ptrSize)
|
||||
}
|
||||
return
|
||||
case *types.Array:
|
||||
return extraSize(t.Elem(), ptrSize) * t.Len()
|
||||
return p.extraSize(t.Elem(), ptrSize) * t.Len()
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -26,12 +26,14 @@ import (
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
type goTypes struct {
|
||||
typs map[unsafe.Pointer]unsafe.Pointer
|
||||
typs map[unsafe.Pointer]unsafe.Pointer
|
||||
typbg map[string]Background
|
||||
}
|
||||
|
||||
func newGoTypes() goTypes {
|
||||
typs := make(map[unsafe.Pointer]unsafe.Pointer)
|
||||
return goTypes{typs}
|
||||
typbk := make(map[string]Background)
|
||||
return goTypes{typs, typbk}
|
||||
}
|
||||
|
||||
type Background int
|
||||
@@ -93,6 +95,9 @@ func (p goTypes) cvtType(typ types.Type) (raw types.Type, cvt bool) {
|
||||
case *types.Struct:
|
||||
return p.cvtStruct(t)
|
||||
case *types.Named:
|
||||
if p.typbg[t.String()] == InC {
|
||||
break
|
||||
}
|
||||
return p.cvtNamed(t)
|
||||
case *types.Signature:
|
||||
return p.cvtClosure(t), true
|
||||
|
||||
Reference in New Issue
Block a user