github.com/mattn/go@v0.0.0-20171011075504-07f7db3ea99f/src/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  
   129  func (d DummyFrontend) Logf(msg string, args ...interface{}) { d.t.Logf(msg, args...) }
   130  func (d DummyFrontend) Log() bool                            { return true }
   131  
   132  func (d DummyFrontend) Fatalf(_ src.XPos, msg string, args ...interface{}) { d.t.Fatalf(msg, args...) }
   133  func (d DummyFrontend) Warnl(_ src.XPos, msg string, args ...interface{})  { d.t.Logf(msg, args...) }
   134  func (d DummyFrontend) Debug_checknil() bool                               { return false }
   135  func (d DummyFrontend) Debug_wb() bool                                     { return false }
   136  
   137  var dummyTypes Types
   138  
   139  func init() {
   140  	// Initialize just enough of the universe and the types package to make our tests function.
   141  	// TODO(josharian): move universe initialization to the types package,
   142  	// so this test setup can share it.
   143  
   144  	types.Tconv = func(t *types.Type, flag, mode, depth int) string {
   145  		return t.Etype.String()
   146  	}
   147  	types.Sconv = func(s *types.Sym, flag, mode int) string {
   148  		return "sym"
   149  	}
   150  	types.FormatSym = func(sym *types.Sym, s fmt.State, verb rune, mode int) {
   151  		fmt.Fprintf(s, "sym")
   152  	}
   153  	types.FormatType = func(t *types.Type, s fmt.State, verb rune, mode int) {
   154  		fmt.Fprintf(s, "%v", t.Etype)
   155  	}
   156  	types.Dowidth = func(t *types.Type) {}
   157  
   158  	types.Tptr = types.TPTR64
   159  	for _, typ := range [...]struct {
   160  		width int64
   161  		et    types.EType
   162  	}{
   163  		{1, types.TINT8},
   164  		{1, types.TUINT8},
   165  		{1, types.TBOOL},
   166  		{2, types.TINT16},
   167  		{2, types.TUINT16},
   168  		{4, types.TINT32},
   169  		{4, types.TUINT32},
   170  		{4, types.TFLOAT32},
   171  		{4, types.TFLOAT64},
   172  		{8, types.TUINT64},
   173  		{8, types.TINT64},
   174  		{8, types.TINT},
   175  		{8, types.TUINTPTR},
   176  	} {
   177  		t := types.New(typ.et)
   178  		t.Width = typ.width
   179  		t.Align = uint8(typ.width)
   180  		types.Types[typ.et] = t
   181  	}
   182  
   183  	dummyTypes = Types{
   184  		Bool:       types.Types[types.TBOOL],
   185  		Int8:       types.Types[types.TINT8],
   186  		Int16:      types.Types[types.TINT16],
   187  		Int32:      types.Types[types.TINT32],
   188  		Int64:      types.Types[types.TINT64],
   189  		UInt8:      types.Types[types.TUINT8],
   190  		UInt16:     types.Types[types.TUINT16],
   191  		UInt32:     types.Types[types.TUINT32],
   192  		UInt64:     types.Types[types.TUINT64],
   193  		Float32:    types.Types[types.TFLOAT32],
   194  		Float64:    types.Types[types.TFLOAT64],
   195  		Int:        types.Types[types.TINT],
   196  		Uintptr:    types.Types[types.TUINTPTR],
   197  		String:     types.Types[types.TSTRING],
   198  		BytePtr:    types.NewPtr(types.Types[types.TUINT8]),
   199  		Int32Ptr:   types.NewPtr(types.Types[types.TINT32]),
   200  		UInt32Ptr:  types.NewPtr(types.Types[types.TUINT32]),
   201  		IntPtr:     types.NewPtr(types.Types[types.TINT]),
   202  		UintptrPtr: types.NewPtr(types.Types[types.TUINTPTR]),
   203  		Float32Ptr: types.NewPtr(types.Types[types.TFLOAT32]),
   204  		Float64Ptr: types.NewPtr(types.Types[types.TFLOAT64]),
   205  		BytePtrPtr: types.NewPtr(types.NewPtr(types.Types[types.TUINT8])),
   206  	}
   207  }
   208  
   209  func (d DummyFrontend) DerefItab(sym *obj.LSym, off int64) *obj.LSym { return nil }
   210  
   211  func (d DummyFrontend) CanSSA(t *types.Type) bool {
   212  	// There are no un-SSAable types in dummy land.
   213  	return true
   214  }