github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/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/internal/obj"
     9  	"cmd/internal/obj/s390x"
    10  	"cmd/internal/obj/x86"
    11  	"cmd/internal/src"
    12  	"testing"
    13  )
    14  
    15  var CheckFunc = checkFunc
    16  var Opt = opt
    17  var Deadcode = deadcode
    18  var Copyelim = copyelim
    19  
    20  var testCtxts = map[string]*obj.Link{
    21  	"amd64": obj.Linknew(&x86.Linkamd64),
    22  	"s390x": obj.Linknew(&s390x.Links390x),
    23  }
    24  
    25  func testConfig(tb testing.TB) *Conf      { return testConfigArch(tb, "amd64") }
    26  func testConfigS390X(tb testing.TB) *Conf { return testConfigArch(tb, "s390x") }
    27  
    28  func testConfigArch(tb testing.TB, arch string) *Conf {
    29  	ctxt, ok := testCtxts[arch]
    30  	if !ok {
    31  		tb.Fatalf("unknown arch %s", arch)
    32  	}
    33  	if ctxt.Arch.IntSize != 8 {
    34  		tb.Fatal("dummyTypes is 64-bit only")
    35  	}
    36  	c := &Conf{
    37  		config: NewConfig(arch, dummyTypes, ctxt, true),
    38  		tb:     tb,
    39  	}
    40  	return c
    41  }
    42  
    43  type Conf struct {
    44  	config *Config
    45  	tb     testing.TB
    46  	fe     Frontend
    47  }
    48  
    49  func (c *Conf) Frontend() Frontend {
    50  	if c.fe == nil {
    51  		c.fe = DummyFrontend{t: c.tb, ctxt: c.config.ctxt}
    52  	}
    53  	return c.fe
    54  }
    55  
    56  // DummyFrontend is a test-only frontend.
    57  // It assumes 64 bit integers and pointers.
    58  type DummyFrontend struct {
    59  	t    testing.TB
    60  	ctxt *obj.Link
    61  }
    62  
    63  type DummyAuto struct {
    64  	t Type
    65  	s string
    66  }
    67  
    68  func (d *DummyAuto) Typ() Type {
    69  	return d.t
    70  }
    71  
    72  func (d *DummyAuto) String() string {
    73  	return d.s
    74  }
    75  
    76  func (DummyFrontend) StringData(s string) interface{} {
    77  	return nil
    78  }
    79  func (DummyFrontend) Auto(pos src.XPos, t Type) GCNode {
    80  	return &DummyAuto{t: t, s: "aDummyAuto"}
    81  }
    82  func (d DummyFrontend) SplitString(s LocalSlot) (LocalSlot, LocalSlot) {
    83  	return LocalSlot{s.N, dummyTypes.BytePtr, s.Off}, LocalSlot{s.N, dummyTypes.Int, s.Off + 8}
    84  }
    85  func (d DummyFrontend) SplitInterface(s LocalSlot) (LocalSlot, LocalSlot) {
    86  	return LocalSlot{s.N, dummyTypes.BytePtr, s.Off}, LocalSlot{s.N, dummyTypes.BytePtr, s.Off + 8}
    87  }
    88  func (d DummyFrontend) SplitSlice(s LocalSlot) (LocalSlot, LocalSlot, LocalSlot) {
    89  	return LocalSlot{s.N, s.Type.ElemType().PtrTo(), s.Off},
    90  		LocalSlot{s.N, dummyTypes.Int, s.Off + 8},
    91  		LocalSlot{s.N, dummyTypes.Int, s.Off + 16}
    92  }
    93  func (d DummyFrontend) SplitComplex(s LocalSlot) (LocalSlot, LocalSlot) {
    94  	if s.Type.Size() == 16 {
    95  		return LocalSlot{s.N, dummyTypes.Float64, s.Off}, LocalSlot{s.N, dummyTypes.Float64, s.Off + 8}
    96  	}
    97  	return LocalSlot{s.N, dummyTypes.Float32, s.Off}, LocalSlot{s.N, dummyTypes.Float32, s.Off + 4}
    98  }
    99  func (d DummyFrontend) SplitInt64(s LocalSlot) (LocalSlot, LocalSlot) {
   100  	if s.Type.IsSigned() {
   101  		return LocalSlot{s.N, dummyTypes.Int32, s.Off + 4}, LocalSlot{s.N, dummyTypes.UInt32, s.Off}
   102  	}
   103  	return LocalSlot{s.N, dummyTypes.UInt32, s.Off + 4}, LocalSlot{s.N, dummyTypes.UInt32, s.Off}
   104  }
   105  func (d DummyFrontend) SplitStruct(s LocalSlot, i int) LocalSlot {
   106  	return LocalSlot{s.N, s.Type.FieldType(i), s.Off + s.Type.FieldOff(i)}
   107  }
   108  func (d DummyFrontend) SplitArray(s LocalSlot) LocalSlot {
   109  	return LocalSlot{s.N, s.Type.ElemType(), s.Off}
   110  }
   111  func (DummyFrontend) Line(_ src.XPos) string {
   112  	return "unknown.go:0"
   113  }
   114  func (DummyFrontend) AllocFrame(f *Func) {
   115  }
   116  func (d DummyFrontend) Syslook(s string) *obj.LSym {
   117  	return d.ctxt.Lookup(s, 0)
   118  }
   119  func (DummyFrontend) UseWriteBarrier() bool {
   120  	return true // only writebarrier_test cares
   121  }
   122  
   123  func (d DummyFrontend) Logf(msg string, args ...interface{}) { d.t.Logf(msg, args...) }
   124  func (d DummyFrontend) Log() bool                            { return true }
   125  
   126  func (d DummyFrontend) Fatalf(_ src.XPos, msg string, args ...interface{}) { d.t.Fatalf(msg, args...) }
   127  func (d DummyFrontend) Warnl(_ src.XPos, msg string, args ...interface{})  { d.t.Logf(msg, args...) }
   128  func (d DummyFrontend) Debug_checknil() bool                               { return false }
   129  func (d DummyFrontend) Debug_wb() bool                                     { return false }
   130  
   131  var dummyTypes = Types{
   132  	Bool:       TypeBool,
   133  	Int8:       TypeInt8,
   134  	Int16:      TypeInt16,
   135  	Int32:      TypeInt32,
   136  	Int64:      TypeInt64,
   137  	UInt8:      TypeUInt8,
   138  	UInt16:     TypeUInt16,
   139  	UInt32:     TypeUInt32,
   140  	UInt64:     TypeUInt64,
   141  	Float32:    TypeFloat32,
   142  	Float64:    TypeFloat64,
   143  	Int:        TypeInt64,
   144  	Uintptr:    TypeUInt64,
   145  	String:     nil,
   146  	BytePtr:    TypeBytePtr,
   147  	Int32Ptr:   TypeInt32.PtrTo(),
   148  	UInt32Ptr:  TypeUInt32.PtrTo(),
   149  	IntPtr:     TypeInt64.PtrTo(),
   150  	UintptrPtr: TypeUInt64.PtrTo(),
   151  	Float32Ptr: TypeFloat32.PtrTo(),
   152  	Float64Ptr: TypeFloat64.PtrTo(),
   153  	BytePtrPtr: TypeBytePtr.PtrTo(),
   154  }
   155  
   156  func (d DummyFrontend) DerefItab(sym *obj.LSym, off int64) *obj.LSym { return nil }
   157  
   158  func (d DummyFrontend) CanSSA(t Type) bool {
   159  	// There are no un-SSAable types in dummy land.
   160  	return true
   161  }