]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: implement SizesFor convenience function
authorRobert Griesemer <gri@golang.org>
Tue, 28 Feb 2017 21:18:55 +0000 (13:18 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 28 Feb 2017 21:25:01 +0000 (21:25 +0000)
SizesFor returns a Sizes implementation for a supported architecture.
Use functionality in srcimporter.

Change-Id: I197e641b419c678030dfaab5c5b8c569fd0410f3
Reviewed-on: https://go-review.googlesource.com/37583
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
src/go/internal/srcimporter/srcimporter.go
src/go/types/api.go
src/go/types/sizes.go

index 0892e906f1c5de9d6500cfa807c73d2d84350f37..9e20a23cdb3a7e62fd4766a6ad126037f0c3e1de 100644 (file)
@@ -34,7 +34,7 @@ func New(ctxt *build.Context, fset *token.FileSet, packages map[string]*types.Pa
        return &Importer{
                ctxt:     ctxt,
                fset:     fset,
-               sizes:    archSizes[ctxt.GOARCH], // use go/types default if GOARCH not found (map access returns nil)
+               sizes:    types.SizesFor(ctxt.GOARCH), // uses go/types default if GOARCH not found
                packages: packages,
        }
 }
@@ -180,20 +180,3 @@ func (p *Importer) joinPath(elem ...string) string {
        }
        return filepath.Join(elem...)
 }
-
-// common architecture word sizes and alignments
-// TODO(gri) consider making this available via go/types
-var archSizes = map[string]*types.StdSizes{
-       "386":      {WordSize: 4, MaxAlign: 4},
-       "arm":      {WordSize: 4, MaxAlign: 4},
-       "arm64":    {WordSize: 8, MaxAlign: 8},
-       "amd64":    {WordSize: 8, MaxAlign: 8},
-       "amd64p32": {WordSize: 4, MaxAlign: 8},
-       "mips":     {WordSize: 4, MaxAlign: 4},
-       "mipsle":   {WordSize: 4, MaxAlign: 4},
-       "mips64":   {WordSize: 8, MaxAlign: 8},
-       "mips64le": {WordSize: 8, MaxAlign: 8},
-       "ppc64":    {WordSize: 8, MaxAlign: 8},
-       "ppc64le":  {WordSize: 8, MaxAlign: 8},
-       "s390x":    {WordSize: 8, MaxAlign: 8},
-}
index 5b911cb96cccbe2ca960012bdb46e0608d2cf710..cd8b19f024e17e8be491f269ce22efc54d81c493 100644 (file)
@@ -121,7 +121,7 @@ type Config struct {
        Importer Importer
 
        // If Sizes != nil, it provides the sizing functions for package unsafe.
-       // Otherwise &StdSizes{WordSize: 8, MaxAlign: 8} is used instead.
+       // Otherwise SizesFor("amd64") is used instead.
        Sizes Sizes
 
        // If DisableUnusedImportCheck is set, packages are not checked
index 3bbe5aee40ae6c3bc0c231c5d71f3e80e39f0d3e..67df9180f9bdbacc8b0efd8beeb032cb3d6e6593 100644 (file)
@@ -153,8 +153,34 @@ func (s *StdSizes) Sizeof(T Type) int64 {
        return s.WordSize // catch-all
 }
 
+// common architecture word sizes and alignments
+var archSizes = map[string]*StdSizes{
+       "386":      {4, 4},
+       "arm":      {4, 4},
+       "arm64":    {8, 8},
+       "amd64":    {8, 8},
+       "amd64p32": {4, 8},
+       "mips":     {4, 4},
+       "mipsle":   {4, 4},
+       "mips64":   {8, 8},
+       "mips64le": {8, 8},
+       "ppc64":    {8, 8},
+       "ppc64le":  {8, 8},
+       "s390x":    {8, 8},
+       // When adding more architectures here,
+       // update the doc string of SizesFor below.
+}
+
+// SizesFor returns the Sizes for one of these architectures:
+// "386", "arm", "arm64", "amd64", "amd64p32", "mips", "mipsle",
+// "mips64", "mips64le", "ppc64", "ppc64le", "s390x".
+// The result is nil if an architecture is not known.
+func SizesFor(arch string) Sizes {
+       return archSizes[arch]
+}
+
 // stdSizes is used if Config.Sizes == nil.
-var stdSizes = StdSizes{8, 8}
+var stdSizes = SizesFor("amd64")
 
 func (conf *Config) alignof(T Type) int64 {
        if s := conf.Sizes; s != nil {