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