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 5package ssa 6 7import ( 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 22var CheckFunc = checkFunc 23var Opt = opt 24var Deadcode = deadcode 25var Copyelim = copyelim 26 27var 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 33func testConfig(tb testing.TB) *Conf { return testConfigArch(tb, "amd64") } 34func testConfigS390X(tb testing.TB) *Conf { return testConfigArch(tb, "s390x") } 35func testConfigARM64(tb testing.TB) *Conf { return testConfigArch(tb, "arm64") } 36 37func 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 52type Conf struct { 53 config *Config 54 tb testing.TB 55 fe Frontend 56} 57 58func (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 74func (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. 82type TestFrontend struct { 83 t testing.TB 84 ctxt *obj.Link 85 f *ir.Func 86} 87 88func (TestFrontend) StringData(s string) *obj.LSym { 89 return nil 90} 91func (d TestFrontend) SplitSlot(parent *LocalSlot, suffix string, offset int64, t *types.Type) LocalSlot { 92 return LocalSlot{N: parent.N, Type: t, Off: offset} 93} 94func (d TestFrontend) Syslook(s string) *obj.LSym { 95 return d.ctxt.Lookup(s) 96} 97func (TestFrontend) UseWriteBarrier() bool { 98 return true // only writebarrier_test cares 99} 100 101func (d TestFrontend) Logf(msg string, args ...interface{}) { d.t.Logf(msg, args...) } 102func (d TestFrontend) Log() bool { return true } 103 104func (d TestFrontend) Fatalf(_ src.XPos, msg string, args ...interface{}) { d.t.Fatalf(msg, args...) } 105func (d TestFrontend) Warnl(_ src.XPos, msg string, args ...interface{}) { d.t.Logf(msg, args...) } 106func (d TestFrontend) Debug_checknil() bool { return false } 107 108func (d TestFrontend) Func() *ir.Func { 109 return d.f 110} 111 112var testTypes Types 113 114func 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} 124