llgo/ssa: AfterInit

This commit is contained in:
xushiwei
2024-05-24 02:09:57 +08:00
parent b195656900
commit b66827998d
3 changed files with 97 additions and 11 deletions

View File

@@ -43,11 +43,12 @@ func (b Builder) abiStruct(t *types.Struct) Expr {
prog := b.Prog
g = pkg.doNewVar(name, prog.AbiTypePtrPtr())
g.Init(prog.Null(g.Type))
g.impl.SetLinkage(llvm.LinkOnceAnyLinkage)
pkg.ainits = append(pkg.ainits, func() {
tabi := b.structOf(t)
b.Store(g.Expr, tabi)
})
}
pkg.abitys = append(pkg.abitys, func() {
tabi := b.structOf(t)
b.Store(g.Expr, tabi)
})
return b.Load(g.Expr)
}
@@ -65,7 +66,7 @@ func (b Builder) structOf(t *types.Struct) Expr {
off := uintptr(prog.OffsetOf(typ, i))
flds[i] = b.structField(sfAbi, prog, f, off, t.Tag(i))
}
pkgPath := prog.Val(pkg.abi.Pkg)
pkgPath := b.Str(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...)
@@ -74,11 +75,11 @@ func (b Builder) structOf(t *types.Struct) Expr {
// 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())
name := b.Str(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)
return b.Call(sfAbi, name, typ, prog.Val(offset), b.Str(tag), exported, embedded)
}
// abiType returns the abi type of the specified type.

View File

@@ -490,7 +490,7 @@ func (p Program) Uint64() Type {
type aPackage struct {
mod llvm.Module
abi abi.Builder
abitys []func()
ainits []func()
vars map[string]Global
fns map[string]Function
stubs map[string]Function
@@ -555,9 +555,12 @@ func (p Package) String() 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()
doAfterInit := len(p.ainits) > 0 || p.pyHasModSyms()
if doAfterInit {
b.SetBlockEx(ret, afterInit)
for _, afterInit := range p.ainits {
afterInit()
}
p.pyLoadModSyms(b)
}
}