TypeSizes: goProgram

This commit is contained in:
xushiwei
2024-05-28 16:59:47 +08:00
parent 3da1aa3ec6
commit fc504e18d2
5 changed files with 37 additions and 10 deletions

View File

@@ -89,7 +89,7 @@ const (
func Do(args []string, conf *Config) { func Do(args []string, conf *Config) {
prog := llssa.NewProgram(nil) prog := llssa.NewProgram(nil)
sizes := prog.TypeSizes() sizes := prog.TypeSizes
flags, patterns, verbose := ParseArgs(args, buildFlags) flags, patterns, verbose := ParseArgs(args, buildFlags)
cfg := &packages.Config{ cfg := &packages.Config{

View File

@@ -43,7 +43,7 @@ func initRtAndPy(prog llssa.Program, cfg *packages.Config) {
load := func() []*packages.Package { load := func() []*packages.Package {
if pkgRtAndPy == nil { if pkgRtAndPy == nil {
var err error var err error
pkgRtAndPy, err = packages.LoadEx(prog.TypeSizes(), cfg, llssa.PkgRuntime, llssa.PkgPython) pkgRtAndPy, err = packages.LoadEx(prog.TypeSizes, cfg, llssa.PkgRuntime, llssa.PkgPython)
check(err) check(err)
} }
return pkgRtAndPy return pkgRtAndPy
@@ -65,7 +65,7 @@ func GenFrom(fileOrPkg string) string {
cfg := &packages.Config{ cfg := &packages.Config{
Mode: loadSyntax | packages.NeedDeps, Mode: loadSyntax | packages.NeedDeps,
} }
initial, err := packages.LoadEx(prog.TypeSizes(), cfg, fileOrPkg) initial, err := packages.LoadEx(prog.TypeSizes, cfg, fileOrPkg)
check(err) check(err)
_, pkgs := ssautil.AllPackages(initial, ssa.SanityCheckFunctions) _, pkgs := ssautil.AllPackages(initial, ssa.SanityCheckFunctions)

View File

@@ -101,17 +101,14 @@ func refine(ld *loader, response *packages.DriverResponse) ([]*Package, error)
// return an error. Clients may need to handle such errors before // return an error. Clients may need to handle such errors before
// proceeding with further analysis. The PrintErrors function is // proceeding with further analysis. The PrintErrors function is
// provided for convenient display of all errors. // provided for convenient display of all errors.
func LoadEx(sizes types.Sizes, cfg *Config, patterns ...string) ([]*Package, error) { func LoadEx(sizes func(types.Sizes) types.Sizes, cfg *Config, patterns ...string) ([]*Package, error) {
ld := newLoader(cfg) ld := newLoader(cfg)
response, external, err := defaultDriver(&ld.Config, patterns...) response, external, err := defaultDriver(&ld.Config, patterns...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if sizes == nil { ld.sizes = types.SizesFor(response.Compiler, response.Arch)
sizes = types.SizesFor(response.Compiler, response.Arch)
}
ld.sizes = sizes
if ld.sizes == nil && ld.Config.Mode&(NeedTypes|NeedTypesSizes|NeedTypesInfo) != 0 { if ld.sizes == nil && ld.Config.Mode&(NeedTypes|NeedTypesSizes|NeedTypesInfo) != 0 {
// Type size information is needed but unavailable. // Type size information is needed but unavailable.
if external { if external {
@@ -130,6 +127,9 @@ func LoadEx(sizes types.Sizes, cfg *Config, patterns ...string) ([]*Package, err
} }
} }
if sizes != nil {
ld.sizes = sizes(ld.sizes)
}
return refine(ld, response) return refine(ld, response)
} }

View File

@@ -101,6 +101,7 @@ func Initialize(flags InitFlags) {
type aProgram struct { type aProgram struct {
ctx llvm.Context ctx llvm.Context
typs typeutil.Map // rawType -> Type typs typeutil.Map // rawType -> Type
sizes types.Sizes // provided by Go compiler
gocvt goTypes gocvt goTypes
rt *types.Package rt *types.Package

View File

@@ -74,6 +74,31 @@ func indexType(t types.Type) types.Type {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
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.
func (p *goProgram) Offsetsof(fields []*types.Var) []int64 {
return p.sizes.Offsetsof(fields)
}
// 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 {
return p.sizes.Sizeof(T)
}
// -----------------------------------------------------------------------------
type rawType struct { type rawType struct {
Type types.Type Type types.Type
} }
@@ -92,8 +117,9 @@ func (t Type) RawType() types.Type {
} }
// TypeSizes returns the sizes of the types. // TypeSizes returns the sizes of the types.
func (p Program) TypeSizes() types.Sizes { func (p Program) TypeSizes(sizes types.Sizes) types.Sizes {
return nil // TODO(xsw) p.sizes = sizes
return (*goProgram)(p)
} }
// TODO(xsw): // TODO(xsw):