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