github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/cmd/compile/internal/ssa/export_test.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ssa
     6  
     7  import (
     8  	"cmd/compile/internal/types"
     9  	"cmd/internal/obj"
    10  	"cmd/internal/obj/s390x"
    11  	"cmd/internal/obj/x86"
    12  	"cmd/internal/src"
    13  	"fmt"
    14  	"testing"
    15  )
    16  
    17  var CheckFunc = checkFunc
    18  var Opt = opt
    19  var Deadcode = deadcode
    20  var Copyelim = copyelim
    21  
    22  var testCtxts = map[string]*obj.Link{
    23  	"amd64": obj.Linknew(&x86.Linkamd64),
    24  	"s390x": obj.Linknew(&s390x.Links390x),
    25  }
    26  
    27  func testConfig(tb testing.TB) *Conf      { return testConfigArch(tb, "amd64") }
    28  func testConfigS390X(tb testing.TB) *Conf { return testConfigArch(tb, "s390x") }
    29  
    30  func testConfigArch(tb testing.TB, arch string) *Conf {
    31  	ctxt, ok := testCtxts[arch]
    32  	if !ok {
    33  		tb.Fatalf("unknown arch %s", arch)
    34  	}
    35  	if ctxt.Arch.PtrSize != 8 {
    36  		tb.Fatal("dummyTypes is 64-bit only")
    37  	}
    38  	c := &Conf{
    39  		config: NewConfig(arch, dummyTypes, ctxt, true),
    40  		tb:     tb,
    41  	}
    42  	return c
    43  }
    44  
    45  type Conf struct {
    46  	config *Config
    47  	tb     testing.TB
    48  	fe     Frontend
    49  }
    50  
    51  func (c *Conf) Frontend() Frontend {
    52  	if c.fe == nil {
    53  		c.fe = DummyFrontend{t: c.tb, ctxt: c.config.ctxt}
    54  	}
    55  	return c.fe
    56  }
    57  
    58  // DummyFrontend is a test-only frontend.
    59  // It assumes 64 bit integers and pointers.
    60  type DummyFrontend struct {
    61  	t    testing.TB
    62  	ctxt *obj.Link
    63  }
    64  
    65  type DummyAuto struct {
    66  	t *types.Type
    67  	s string
    68  }
    69  
    70  func (d *DummyAuto) Typ() *types.Type {
    71  	return d.t
    72  }
    73  
    74  func (d *DummyAuto) String() string {
    75  	return d.s
    76  }
    77  
    78  func (d *DummyAuto) StorageClass() StorageClass {
    79  	return ClassAuto
    80  }
    81  
    82  func (DummyFrontend) StringData(s string) interface{} {
    83  	return nil
    84  }
    85  func (DummyFrontend) Auto(pos src.XPos, t *types.Type) GCNode {
    86  	return &DummyAuto{t: t, s: "aDummyAuto"}
    87  }
    88  func (d DummyFrontend) SplitString(s LocalSlot) (LocalSlot, LocalSlot) {
    89  	return LocalSlot{N: s.N, Type: dummyTypes.BytePtr, Off: s.Off}, LocalSlot{N: s.N, Type: dummyTypes.Int, Off: s.Off + 8}
    90  }
    91  func (d DummyFrontend) SplitInterface(s LocalSlot) (LocalSlot, LocalSlot) {
    92  	return LocalSlot{N: s.N, Type: dummyTypes.BytePtr, Off: s.Off}, LocalSlot{N: s.N, Type: dummyTypes.BytePtr, Off: s.Off + 8}
    93  }
    94  func (d DummyFrontend) SplitSlice(s LocalSlot) (LocalSlot, LocalSlot, LocalSlot) {
    95  	return LocalSlot{N: s.N, Type: s.Type.ElemType().PtrTo(), Off: s.Off},
    96  		LocalSlot{N: s.N, Type: dummyTypes.Int, Off: s.Off + 8},
    97  		LocalSlot{N: s.N, Type: dummyTypes.Int, Off: s.Off + 16}
    98  }
    99  func (d DummyFrontend) SplitComplex(s LocalSlot) (LocalSlot, LocalSlot) {
   100  	if s.Type.Size() == 16 {
   101  		return LocalSlot{N: s.N, Type: dummyTypes.Float64, Off: s.Off}, LocalSlot{N: s.N, Type: dummyTypes.Float64, Off: s.Off + 8}
   102  	}
   103  	return LocalSlot{N: s.N, Type: dummyTypes.Float32, Off: s.Off}, LocalSlot{N: s.N, Type: dummyTypes.Float32, Off: s.Off + 4}
   104  }
   105  func (d DummyFrontend) SplitInt64(s LocalSlot) (LocalSlot, LocalSlot) {
   106  	if s.Type.IsSigned() {
   107  		return LocalSlot{N: s.N, Type: dummyTypes.Int32, Off: s.Off + 4}, LocalSlot{N: s.N, Type: dummyTypes.UInt32, Off: s.Off}
   108  	}
   109  	return LocalSlot{N: s.N, Type: dummyTypes.UInt32, Off: s.Off + 4}, LocalSlot{N: s.N, Type: dummyTypes.UInt32, Off: s.Off}
   110  }
   111  func (d DummyFrontend) SplitStruct(s LocalSlot, i int) LocalSlot {
   112  	return LocalSlot{N: s.N, Type: s.Type.FieldType(i), Off: s.Off + s.Type.FieldOff(i)}
   113  }
   114  func (d DummyFrontend) SplitArray(s LocalSlot) LocalSlot {
   115  	return LocalSlot{N: s.N, Type: s.Type.ElemType(), Off: s.Off}
   116  }
   117  func (DummyFrontend) Line(_ src.XPos) string {
   118  	return "unknown.go:0"
   119  }
   120  func (DummyFrontend) AllocFrame(f *Func) {
   121  }
   122  func (d DummyFrontend) Syslook(s string) *obj.LSym {
   123  	return d.ctxt.Lookup(s)
   124  }
   125  func (DummyFrontend) UseWriteBarrier() bool {
   126  	return true // only writebarrier_test cares
   127  }
   128  func (DummyFrontend) SetWBPos(pos src.XPos) {
   129  }
   130  
   131  func (d DummyFrontend) Logf(msg string, args ...interface{}) { d.t.Logf(msg, args...) }
   132  func (d DummyFrontend) Log() bool                            { return true }
   133  
   134  func (d DummyFrontend) Fatalf(_ src.XPos, msg string, args ...interface{}) { d.t.Fatalf(msg, args...) }
   135  func (d DummyFrontend) Warnl(_ src.XPos, msg string, args ...interface{})  { d.t.Logf(msg, args...) }
   136  func (d DummyFrontend) Debug_checknil() bool                               { return false }
   137  func (d DummyFrontend) Debug_eagerwb() bool                                { return false }
   138  
   139  var dummyTypes Types
   140  
   141  func init() {
   142  	// Initialize just enough of the universe and the types package to make our tests function.
   143  	// TODO(josharian): move universe initialization to the types package,
   144  	// so this test setup can share it.
   145  
   146  	types.Tconv = func(t *types.Type, flag, mode, depth int) string {
   147  		return t.Etype.String()
   148  	}
   149  	types.Sconv = func(s *types.Sym, flag, mode int) string {
   150  		return "sym"
   151  	}
   152  	types.FormatSym = func(sym *types.Sym, s fmt.State, verb rune, mode int) {
   153  		fmt.Fprintf(s, "sym")
   154  	}
   155  	types.FormatType = func(t *types.Type, s fmt.State, verb rune, mode int) {
   156  		fmt.Fprintf(s, "%v", t.Etype)
   157  	}
   158  	types.Dowidth = func(t *types.Type) {}
   159  
   160  	types.Tptr = types.TPTR64
   161  	for _, typ := range [...]struct {
   162  		width int64
   163  		et    types.EType
   164  	}{
   165  		{1, types.TINT8},
   166  		{1, types.TUINT8},
   167  		{1, types.TBOOL},
   168  		{2, types.TINT16},
   169  		{2, types.TUINT16},
   170  		{4, types.TINT32},
   171  		{4, types.TUINT32},
   172  		{4, types.TFLOAT32},
   173  		{4, types.TFLOAT64},
   174  		{8, types.TUINT64},
   175  		{8, types.TINT64},
   176  		{8, types.TINT},
   177  		{8, types.TUINTPTR},
   178  	} {
   179  		t := types.New(typ.et)
   180  		t.Width = typ.width
   181  		t.Align = uint8(typ.width)
   182  		types.Types[typ.et] = t
   183  	}
   184  
   185  	dummyTypes = Types{
   186  		Bool:       types.Types[types.TBOOL],
   187  		Int8:       types.Types[types.TINT8],
   188  		Int16:      types.Types[types.TINT16],
   189  		Int32:      types.Types[types.TINT32],
   190  		Int64:      types.Types[types.TINT64],
   191  		UInt8:      types.Types[types.TUINT8],
   192  		UInt16:     types.Types[types.TUINT16],
   193  		UInt32:     types.Types[types.TUINT32],
   194  		UInt64:     types.Types[types.TUINT64],
   195  		Float32:    types.Types[types.TFLOAT32],
   196  		Float64:    types.Types[types.TFLOAT64],
   197  		Int:        types.Types[types.TINT],
   198  		Uintptr:    types.Types[types.TUINTPTR],
   199  		String:     types.Types[types.TSTRING],
   200  		BytePtr:    types.NewPtr(types.Types[types.TUINT8]),
   201  		Int32Ptr:   types.NewPtr(types.Types[types.TINT32]),
   202  		UInt32Ptr:  types.NewPtr(types.Types[types.TUINT32]),
   203  		IntPtr:     types.NewPtr(types.Types[types.TINT]),
   204  		UintptrPtr: types.NewPtr(types.Types[types.TUINTPTR]),
   205  		Float32Ptr: types.NewPtr(types.Types[types.TFLOAT32]),
   206  		Float64Ptr: types.NewPtr(types.Types[types.TFLOAT64]),
   207  		BytePtrPtr: types.NewPtr(types.NewPtr(types.Types[types.TUINT8])),
   208  	}
   209  }
   210  
   211  func (d DummyFrontend) DerefItab(sym *obj.LSym, off int64) *obj.LSym { return nil }
   212  
   213  func (d DummyFrontend) CanSSA(t *types.Type) bool {
   214  	// There are no un-SSAable types in dummy land.
   215  	return true
   216  }