github.com/corona10/go@v0.0.0-20180224231303-7a218942be57/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 (d *DummyAuto) IsSynthetic() bool {
    83  	return false
    84  }
    85  
    86  func (DummyFrontend) StringData(s string) interface{} {
    87  	return nil
    88  }
    89  func (DummyFrontend) Auto(pos src.XPos, t *types.Type) GCNode {
    90  	return &DummyAuto{t: t, s: "aDummyAuto"}
    91  }
    92  func (d DummyFrontend) SplitString(s LocalSlot) (LocalSlot, LocalSlot) {
    93  	return LocalSlot{N: s.N, Type: dummyTypes.BytePtr, Off: s.Off}, LocalSlot{N: s.N, Type: dummyTypes.Int, Off: s.Off + 8}
    94  }
    95  func (d DummyFrontend) SplitInterface(s LocalSlot) (LocalSlot, LocalSlot) {
    96  	return LocalSlot{N: s.N, Type: dummyTypes.BytePtr, Off: s.Off}, LocalSlot{N: s.N, Type: dummyTypes.BytePtr, Off: s.Off + 8}
    97  }
    98  func (d DummyFrontend) SplitSlice(s LocalSlot) (LocalSlot, LocalSlot, LocalSlot) {
    99  	return LocalSlot{N: s.N, Type: s.Type.ElemType().PtrTo(), Off: s.Off},
   100  		LocalSlot{N: s.N, Type: dummyTypes.Int, Off: s.Off + 8},
   101  		LocalSlot{N: s.N, Type: dummyTypes.Int, Off: s.Off + 16}
   102  }
   103  func (d DummyFrontend) SplitComplex(s LocalSlot) (LocalSlot, LocalSlot) {
   104  	if s.Type.Size() == 16 {
   105  		return LocalSlot{N: s.N, Type: dummyTypes.Float64, Off: s.Off}, LocalSlot{N: s.N, Type: dummyTypes.Float64, Off: s.Off + 8}
   106  	}
   107  	return LocalSlot{N: s.N, Type: dummyTypes.Float32, Off: s.Off}, LocalSlot{N: s.N, Type: dummyTypes.Float32, Off: s.Off + 4}
   108  }
   109  func (d DummyFrontend) SplitInt64(s LocalSlot) (LocalSlot, LocalSlot) {
   110  	if s.Type.IsSigned() {
   111  		return LocalSlot{N: s.N, Type: dummyTypes.Int32, Off: s.Off + 4}, LocalSlot{N: s.N, Type: dummyTypes.UInt32, Off: s.Off}
   112  	}
   113  	return LocalSlot{N: s.N, Type: dummyTypes.UInt32, Off: s.Off + 4}, LocalSlot{N: s.N, Type: dummyTypes.UInt32, Off: s.Off}
   114  }
   115  func (d DummyFrontend) SplitStruct(s LocalSlot, i int) LocalSlot {
   116  	return LocalSlot{N: s.N, Type: s.Type.FieldType(i), Off: s.Off + s.Type.FieldOff(i)}
   117  }
   118  func (d DummyFrontend) SplitArray(s LocalSlot) LocalSlot {
   119  	return LocalSlot{N: s.N, Type: s.Type.ElemType(), Off: s.Off}
   120  }
   121  func (DummyFrontend) Line(_ src.XPos) string {
   122  	return "unknown.go:0"
   123  }
   124  func (DummyFrontend) AllocFrame(f *Func) {
   125  }
   126  func (d DummyFrontend) Syslook(s string) *obj.LSym {
   127  	return d.ctxt.Lookup(s)
   128  }
   129  func (DummyFrontend) UseWriteBarrier() bool {
   130  	return true // only writebarrier_test cares
   131  }
   132  func (DummyFrontend) SetWBPos(pos src.XPos) {
   133  }
   134  
   135  func (d DummyFrontend) Logf(msg string, args ...interface{}) { d.t.Logf(msg, args...) }
   136  func (d DummyFrontend) Log() bool                            { return true }
   137  
   138  func (d DummyFrontend) Fatalf(_ src.XPos, msg string, args ...interface{}) { d.t.Fatalf(msg, args...) }
   139  func (d DummyFrontend) Warnl(_ src.XPos, msg string, args ...interface{})  { d.t.Logf(msg, args...) }
   140  func (d DummyFrontend) Debug_checknil() bool                               { return false }
   141  
   142  var dummyTypes Types
   143  
   144  func init() {
   145  	// Initialize just enough of the universe and the types package to make our tests function.
   146  	// TODO(josharian): move universe initialization to the types package,
   147  	// so this test setup can share it.
   148  
   149  	types.Tconv = func(t *types.Type, flag, mode, depth int) string {
   150  		return t.Etype.String()
   151  	}
   152  	types.Sconv = func(s *types.Sym, flag, mode int) string {
   153  		return "sym"
   154  	}
   155  	types.FormatSym = func(sym *types.Sym, s fmt.State, verb rune, mode int) {
   156  		fmt.Fprintf(s, "sym")
   157  	}
   158  	types.FormatType = func(t *types.Type, s fmt.State, verb rune, mode int) {
   159  		fmt.Fprintf(s, "%v", t.Etype)
   160  	}
   161  	types.Dowidth = func(t *types.Type) {}
   162  
   163  	types.Tptr = types.TPTR64
   164  	for _, typ := range [...]struct {
   165  		width int64
   166  		et    types.EType
   167  	}{
   168  		{1, types.TINT8},
   169  		{1, types.TUINT8},
   170  		{1, types.TBOOL},
   171  		{2, types.TINT16},
   172  		{2, types.TUINT16},
   173  		{4, types.TINT32},
   174  		{4, types.TUINT32},
   175  		{4, types.TFLOAT32},
   176  		{4, types.TFLOAT64},
   177  		{8, types.TUINT64},
   178  		{8, types.TINT64},
   179  		{8, types.TINT},
   180  		{8, types.TUINTPTR},
   181  	} {
   182  		t := types.New(typ.et)
   183  		t.Width = typ.width
   184  		t.Align = uint8(typ.width)
   185  		types.Types[typ.et] = t
   186  	}
   187  
   188  	dummyTypes = Types{
   189  		Bool:       types.Types[types.TBOOL],
   190  		Int8:       types.Types[types.TINT8],
   191  		Int16:      types.Types[types.TINT16],
   192  		Int32:      types.Types[types.TINT32],
   193  		Int64:      types.Types[types.TINT64],
   194  		UInt8:      types.Types[types.TUINT8],
   195  		UInt16:     types.Types[types.TUINT16],
   196  		UInt32:     types.Types[types.TUINT32],
   197  		UInt64:     types.Types[types.TUINT64],
   198  		Float32:    types.Types[types.TFLOAT32],
   199  		Float64:    types.Types[types.TFLOAT64],
   200  		Int:        types.Types[types.TINT],
   201  		Uintptr:    types.Types[types.TUINTPTR],
   202  		String:     types.Types[types.TSTRING],
   203  		BytePtr:    types.NewPtr(types.Types[types.TUINT8]),
   204  		Int32Ptr:   types.NewPtr(types.Types[types.TINT32]),
   205  		UInt32Ptr:  types.NewPtr(types.Types[types.TUINT32]),
   206  		IntPtr:     types.NewPtr(types.Types[types.TINT]),
   207  		UintptrPtr: types.NewPtr(types.Types[types.TUINTPTR]),
   208  		Float32Ptr: types.NewPtr(types.Types[types.TFLOAT32]),
   209  		Float64Ptr: types.NewPtr(types.Types[types.TFLOAT64]),
   210  		BytePtrPtr: types.NewPtr(types.NewPtr(types.Types[types.TUINT8])),
   211  	}
   212  }
   213  
   214  func (d DummyFrontend) DerefItab(sym *obj.LSym, off int64) *obj.LSym { return nil }
   215  
   216  func (d DummyFrontend) CanSSA(t *types.Type) bool {
   217  	// There are no un-SSAable types in dummy land.
   218  	return true
   219  }