github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/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/compile/internal/types" 9 "cmd/internal/obj" 10 "cmd/internal/obj/s390x" 11 "cmd/internal/obj/x86" 12 "cmd/internal/src" 13 "fmt" 14 "testing" 15 ) 16 17 var CheckFunc = checkFunc 18 var Opt = opt 19 var Deadcode = deadcode 20 var Copyelim = copyelim 21 22 var testCtxts = map[string]*obj.Link{ 23 "amd64": obj.Linknew(&x86.Linkamd64), 24 "s390x": obj.Linknew(&s390x.Links390x), 25 } 26 27 func testConfig(tb testing.TB) *Conf { return testConfigArch(tb, "amd64") } 28 func testConfigS390X(tb testing.TB) *Conf { return testConfigArch(tb, "s390x") } 29 30 func testConfigArch(tb testing.TB, arch string) *Conf { 31 ctxt, ok := testCtxts[arch] 32 if !ok { 33 tb.Fatalf("unknown arch %s", arch) 34 } 35 if ctxt.Arch.PtrSize != 8 { 36 tb.Fatal("dummyTypes is 64-bit only") 37 } 38 c := &Conf{ 39 config: NewConfig(arch, dummyTypes, ctxt, true), 40 tb: tb, 41 } 42 return c 43 } 44 45 type Conf struct { 46 config *Config 47 tb testing.TB 48 fe Frontend 49 } 50 51 func (c *Conf) Frontend() Frontend { 52 if c.fe == nil { 53 c.fe = DummyFrontend{t: c.tb, ctxt: c.config.ctxt} 54 } 55 return c.fe 56 } 57 58 // DummyFrontend is a test-only frontend. 59 // It assumes 64 bit integers and pointers. 60 type DummyFrontend struct { 61 t testing.TB 62 ctxt *obj.Link 63 } 64 65 type DummyAuto struct { 66 t *types.Type 67 s string 68 } 69 70 func (d *DummyAuto) Typ() *types.Type { 71 return d.t 72 } 73 74 func (d *DummyAuto) String() string { 75 return d.s 76 } 77 78 func (DummyFrontend) StringData(s string) interface{} { 79 return nil 80 } 81 func (DummyFrontend) Auto(pos src.XPos, t *types.Type) GCNode { 82 return &DummyAuto{t: t, s: "aDummyAuto"} 83 } 84 func (d DummyFrontend) SplitString(s LocalSlot) (LocalSlot, LocalSlot) { 85 return LocalSlot{s.N, dummyTypes.BytePtr, s.Off}, LocalSlot{s.N, dummyTypes.Int, s.Off + 8} 86 } 87 func (d DummyFrontend) SplitInterface(s LocalSlot) (LocalSlot, LocalSlot) { 88 return LocalSlot{s.N, dummyTypes.BytePtr, s.Off}, LocalSlot{s.N, dummyTypes.BytePtr, s.Off + 8} 89 } 90 func (d DummyFrontend) SplitSlice(s LocalSlot) (LocalSlot, LocalSlot, LocalSlot) { 91 return LocalSlot{s.N, s.Type.ElemType().PtrTo(), s.Off}, 92 LocalSlot{s.N, dummyTypes.Int, s.Off + 8}, 93 LocalSlot{s.N, dummyTypes.Int, s.Off + 16} 94 } 95 func (d DummyFrontend) SplitComplex(s LocalSlot) (LocalSlot, LocalSlot) { 96 if s.Type.Size() == 16 { 97 return LocalSlot{s.N, dummyTypes.Float64, s.Off}, LocalSlot{s.N, dummyTypes.Float64, s.Off + 8} 98 } 99 return LocalSlot{s.N, dummyTypes.Float32, s.Off}, LocalSlot{s.N, dummyTypes.Float32, s.Off + 4} 100 } 101 func (d DummyFrontend) SplitInt64(s LocalSlot) (LocalSlot, LocalSlot) { 102 if s.Type.IsSigned() { 103 return LocalSlot{s.N, dummyTypes.Int32, s.Off + 4}, LocalSlot{s.N, dummyTypes.UInt32, s.Off} 104 } 105 return LocalSlot{s.N, dummyTypes.UInt32, s.Off + 4}, LocalSlot{s.N, dummyTypes.UInt32, s.Off} 106 } 107 func (d DummyFrontend) SplitStruct(s LocalSlot, i int) LocalSlot { 108 return LocalSlot{s.N, s.Type.FieldType(i), s.Off + s.Type.FieldOff(i)} 109 } 110 func (d DummyFrontend) SplitArray(s LocalSlot) LocalSlot { 111 return LocalSlot{s.N, s.Type.ElemType(), s.Off} 112 } 113 func (DummyFrontend) Line(_ src.XPos) string { 114 return "unknown.go:0" 115 } 116 func (DummyFrontend) AllocFrame(f *Func) { 117 } 118 func (d DummyFrontend) Syslook(s string) *obj.LSym { 119 return d.ctxt.Lookup(s) 120 } 121 func (DummyFrontend) UseWriteBarrier() bool { 122 return true // only writebarrier_test cares 123 } 124 125 func (d DummyFrontend) Logf(msg string, args ...interface{}) { d.t.Logf(msg, args...) } 126 func (d DummyFrontend) Log() bool { return true } 127 128 func (d DummyFrontend) Fatalf(_ src.XPos, msg string, args ...interface{}) { d.t.Fatalf(msg, args...) } 129 func (d DummyFrontend) Warnl(_ src.XPos, msg string, args ...interface{}) { d.t.Logf(msg, args...) } 130 func (d DummyFrontend) Debug_checknil() bool { return false } 131 func (d DummyFrontend) Debug_wb() bool { return false } 132 133 var dummyTypes Types 134 135 func init() { 136 // Initialize just enough of the universe and the types package to make our tests function. 137 // TODO(josharian): move universe initialization to the types package, 138 // so this test setup can share it. 139 140 types.Tconv = func(t *types.Type, flag, mode, depth int) string { 141 return t.Etype.String() 142 } 143 types.Sconv = func(s *types.Sym, flag, mode int) string { 144 return "sym" 145 } 146 types.FormatSym = func(sym *types.Sym, s fmt.State, verb rune, mode int) { 147 fmt.Fprintf(s, "sym") 148 } 149 types.FormatType = func(t *types.Type, s fmt.State, verb rune, mode int) { 150 fmt.Fprintf(s, "%v", t.Etype) 151 } 152 types.Dowidth = func(t *types.Type) {} 153 154 types.Tptr = types.TPTR64 155 for _, typ := range [...]struct { 156 width int64 157 et types.EType 158 }{ 159 {1, types.TINT8}, 160 {1, types.TUINT8}, 161 {1, types.TBOOL}, 162 {2, types.TINT16}, 163 {2, types.TUINT16}, 164 {4, types.TINT32}, 165 {4, types.TUINT32}, 166 {4, types.TFLOAT32}, 167 {4, types.TFLOAT64}, 168 {8, types.TUINT64}, 169 {8, types.TINT64}, 170 {8, types.TINT}, 171 {8, types.TUINTPTR}, 172 } { 173 t := types.New(typ.et) 174 t.Width = typ.width 175 t.Align = uint8(typ.width) 176 types.Types[typ.et] = t 177 } 178 179 dummyTypes = Types{ 180 Bool: types.Types[types.TBOOL], 181 Int8: types.Types[types.TINT8], 182 Int16: types.Types[types.TINT16], 183 Int32: types.Types[types.TINT32], 184 Int64: types.Types[types.TINT64], 185 UInt8: types.Types[types.TUINT8], 186 UInt16: types.Types[types.TUINT16], 187 UInt32: types.Types[types.TUINT32], 188 UInt64: types.Types[types.TUINT64], 189 Float32: types.Types[types.TFLOAT32], 190 Float64: types.Types[types.TFLOAT64], 191 Int: types.Types[types.TINT], 192 Uintptr: types.Types[types.TUINTPTR], 193 String: types.Types[types.TSTRING], 194 BytePtr: types.NewPtr(types.Types[types.TUINT8]), 195 Int32Ptr: types.NewPtr(types.Types[types.TINT32]), 196 UInt32Ptr: types.NewPtr(types.Types[types.TUINT32]), 197 IntPtr: types.NewPtr(types.Types[types.TINT]), 198 UintptrPtr: types.NewPtr(types.Types[types.TUINTPTR]), 199 Float32Ptr: types.NewPtr(types.Types[types.TFLOAT32]), 200 Float64Ptr: types.NewPtr(types.Types[types.TFLOAT64]), 201 BytePtrPtr: types.NewPtr(types.NewPtr(types.Types[types.TUINT8])), 202 } 203 } 204 205 func (d DummyFrontend) DerefItab(sym *obj.LSym, off int64) *obj.LSym { return nil } 206 207 func (d DummyFrontend) CanSSA(t *types.Type) bool { 208 // There are no un-SSAable types in dummy land. 209 return true 210 }