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