Source file src/cmd/compile/internal/ssacompile/export_test.go

     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 ssacompile
     6  
     7  import (
     8  	"testing"
     9  
    10  	"cmd/compile/internal/base"
    11  	"cmd/compile/internal/ir"
    12  	"cmd/compile/internal/ssa"
    13  	"cmd/compile/internal/typecheck"
    14  	"cmd/compile/internal/types"
    15  	"cmd/internal/obj"
    16  	"cmd/internal/obj/arm64"
    17  	"cmd/internal/obj/s390x"
    18  	"cmd/internal/obj/x86"
    19  	"cmd/internal/src"
    20  	"cmd/internal/sys"
    21  )
    22  
    23  var CheckFunc = checkFunc
    24  var Opt = opt
    25  var Deadcode = deadcode
    26  var Copyelim = copyelim
    27  
    28  var testCtxts = map[string]*obj.Link{
    29  	"amd64": obj.Linknew(&x86.Linkamd64),
    30  	"s390x": obj.Linknew(&s390x.Links390x),
    31  	"arm64": obj.Linknew(&arm64.Linkarm64),
    32  }
    33  
    34  func testConfig(tb testing.TB) *Conf      { return testConfigArch(tb, "amd64") }
    35  func testConfigS390X(tb testing.TB) *Conf { return testConfigArch(tb, "s390x") }
    36  func testConfigARM64(tb testing.TB) *Conf { return testConfigArch(tb, "arm64") }
    37  
    38  func testConfigArch(tb testing.TB, arch string) *Conf {
    39  	ctxt, ok := testCtxts[arch]
    40  	if !ok {
    41  		tb.Fatalf("unknown arch %s", arch)
    42  	}
    43  	if ctxt.Arch.PtrSize != 8 {
    44  		tb.Fatal("testTypes is 64-bit only")
    45  	}
    46  	c := &Conf{
    47  		config: newConfig(arch, testTypes, ctxt, true, false),
    48  		tb:     tb,
    49  	}
    50  	return c
    51  }
    52  
    53  type Conf struct {
    54  	config *ssa.Config
    55  	tb     testing.TB
    56  	fe     ssa.Frontend
    57  }
    58  
    59  func (c *Conf) Frontend() ssa.Frontend {
    60  	if c.fe == nil {
    61  		pkg := types.NewPkg("my/import/path", "path")
    62  		fn := ir.NewFunc(src.NoXPos, src.NoXPos, pkg.Lookup("function"), types.NewSignature(nil, nil, nil))
    63  		fn.DeclareParams(true)
    64  		fn.LSym = &obj.LSym{Name: "my/import/path.function"}
    65  
    66  		c.fe = TestFrontend{
    67  			t:    c.tb,
    68  			ctxt: c.config.Ctxt,
    69  			f:    fn,
    70  		}
    71  	}
    72  	return c.fe
    73  }
    74  
    75  func (c *Conf) Temp(typ *types.Type) *ir.Name {
    76  	n := ir.NewNameAt(src.NoXPos, &types.Sym{Name: "aFakeAuto"}, typ)
    77  	n.Class = ir.PAUTO
    78  	return n
    79  }
    80  
    81  // TestFrontend is a test-only frontend.
    82  // It assumes 64 bit integers and pointers.
    83  type TestFrontend struct {
    84  	t    testing.TB
    85  	ctxt *obj.Link
    86  	f    *ir.Func
    87  }
    88  
    89  func (TestFrontend) StringData(s string) *obj.LSym {
    90  	return nil
    91  }
    92  func (d TestFrontend) SplitSlot(parent *ssa.LocalSlot, suffix string, offset int64, t *types.Type) ssa.LocalSlot {
    93  	return ssa.LocalSlot{N: parent.N, Type: t, Off: offset}
    94  }
    95  func (d TestFrontend) Syslook(s string) *obj.LSym {
    96  	return d.ctxt.Lookup(s)
    97  }
    98  func (TestFrontend) UseWriteBarrier() bool {
    99  	return true // only writebarrier_test cares
   100  }
   101  
   102  func (d TestFrontend) Logf(msg string, args ...any) { d.t.Logf(msg, args...) }
   103  func (d TestFrontend) Log() bool                    { return true }
   104  
   105  func (d TestFrontend) Fatalf(_ src.XPos, msg string, args ...any) { d.t.Fatalf(msg, args...) }
   106  func (d TestFrontend) Warnl(_ src.XPos, msg string, args ...any)  { d.t.Logf(msg, args...) }
   107  func (d TestFrontend) Debug_checknil() bool                       { return false }
   108  
   109  func (d TestFrontend) Func() *ir.Func {
   110  	return d.f
   111  }
   112  
   113  var testTypes ssa.Types
   114  
   115  func init() {
   116  	// TODO(mdempsky): Push into types.InitUniverse or typecheck.InitUniverse.
   117  	types.PtrSize = 8
   118  	types.RegSize = 8
   119  	types.MaxWidth = 1 << 50
   120  
   121  	base.Ctxt = &obj.Link{Arch: &obj.LinkArch{Arch: &sys.Arch{Alignment: 1, CanMergeLoads: true}}}
   122  	typecheck.InitUniverse()
   123  	testTypes.SetTypPtrs()
   124  }
   125  

View as plain text