Source file
test/decomposeaddr.go
1
2
3
4
5
6
7
8
9 package main
10
11 import "unsafe"
12
13 type point struct {
14 x, y int
15 }
16
17 func simpleStruct(c bool) int {
18 p := point{10, 20}
19 ptr := &p
20 if c {
21 ptr.x += 1
22 } else {
23 ptr.y += 1
24 }
25 return ptr.x + ptr.y
26 }
27
28 type inner struct {
29 a, b int
30 }
31
32 type outer struct {
33 in inner
34 c int
35 }
36
37 func nestedStruct(c bool) int {
38 o := outer{inner{1, 2}, 3}
39 ptr := &o
40 if c {
41 ptr.in.a += 10
42 } else {
43 ptr.in.b += 20
44 }
45 return ptr.in.a + ptr.in.b + ptr.c
46 }
47
48 func array(c bool) int {
49 var a [3]int
50 pa := &a
51 pa[0] = 10
52 pa[1] = 20
53 pa[2] = 30
54 if c {
55 pa[1] += 5
56 }
57 return pa[0] + pa[1] + pa[2]
58 }
59
60 func zeroStruct(c bool) int {
61 var p point
62 ptr := &p
63 if c {
64 ptr.x = 10
65 } else {
66 ptr.y = 20
67 }
68 return ptr.x + ptr.y
69 }
70
71 func moveStruct(x, y int, c bool) int {
72 p1 := point{x, y}
73 var p2 point
74 ptr1 := &p1
75 ptr2 := &p2
76 if c {
77 *ptr2 = *ptr1
78 } else {
79 *ptr2 = point{30, 40}
80 }
81 return ptr2.x + ptr2.y + ptr1.x
82 }
83
84 type arrayStruct struct {
85 a [2]int
86 b int
87 }
88
89 func arrayStructAddr(c bool) int {
90 s := arrayStruct{[2]int{1, 2}, 3}
91 ps := &s
92 if c {
93 ps.a[0] += 10
94 } else {
95 ps.b += 20
96 }
97 return ps.a[0] + ps.a[1] + ps.b
98 }
99
100 func partialZero() int {
101 type T struct {
102 a int
103 b [2]int
104 c int
105 }
106
107 t := T{a: 9, c: 9}
108 p := &t.b
109 *p = [2]int{}
110 return t.a
111 }
112
113 func partialMove() int {
114 type T struct {
115 a int
116 b [2]int
117 c int
118 }
119
120 b2 := [2]int{1, 2}
121
122 t := T{a: 9, c: 9}
123 p := &t.b
124 *p = b2
125 return t.a
126 }
127
128 type bigStruct struct {
129 a, b, c, d, e int
130 f [30]int
131 g, h, i, j, k int
132 }
133
134 func partialActiveBigStruct(branch bool) {
135
136 var x, y bigStruct
137 xp := &x
138 yp := &y
139 if branch {
140 xp.a = 1
141 xp.b = 2
142 } else {
143 xp.a = 4
144 xp.b = 5
145 }
146 xp.f = [30]int{3, 4}
147 xp.c = xp.a + xp.b
148 if branch {
149 yp.c = 1
150 yp.d = 2
151 yp.e = 3
152 yp.f[5] = 4
153 yp.g = 5
154 } else {
155 yp.c = 4
156 yp.d = 5
157 yp.e = 6
158 yp.f[5] = 7
159 yp.g = 8
160 }
161 yp.h = yp.c + yp.d + yp.e + yp.f[5] + yp.g
162 }
163
164
165
166
167
168 func unsafeAliasMoveOrder(c bool) int {
169 var x [4]byte
170 x[0], x[1], x[2], x[3] = 1, 2, 3, 4
171 var y [2]uint16
172 y = *(*[2]uint16)(unsafe.Pointer(&x))
173 if c {
174 y[0]++
175 }
176 return int(y[0]) + int(y[1])
177 }
178
179 type paddedA struct {
180 a uint8
181 b, c, d, e, f uint32
182 }
183
184 type paddedB struct {
185 x, y uint8
186 b, c, d, e, f uint32
187 }
188
189
190
191
192
193
194 func unsafeAliasMovePadding(v uint8, c bool) (uint8, uint32) {
195 var a paddedA
196 a.a = v
197 a.b, a.c = 1, 2
198 var b paddedB
199 b.x, b.y = 7, 9
200 b = *(*paddedB)(unsafe.Pointer(&a))
201 b.b++
202 if c {
203 b.c++
204 }
205 return b.y, b.b + b.c
206 }
207
208 type stringStruct struct {
209 s string
210 i int
211 }
212
213 func stringField(c bool) int {
214
215 ss := stringStruct{"hello", 10}
216 p := &ss
217 if c {
218 p.i += 5
219 }
220 return len(p.s) + p.i
221 }
222
223 type sliceStruct struct {
224 sl []int
225 i int
226 }
227
228 func sliceField(c bool, sl []int) int {
229
230 ss := sliceStruct{sl, 10}
231 p := &ss
232 if c {
233 p.i += 5
234 }
235 return len(p.sl) + cap(p.sl) + p.i
236 }
237
238 type ifaceStruct struct {
239 e any
240 i int
241 }
242
243 func ifaceField(c bool, e any) int {
244
245 is := ifaceStruct{e, 10}
246 p := &is
247 if c {
248 p.i += 5
249 }
250 if p.e != nil {
251 p.i++
252 }
253 return p.i
254 }
255
256 type complexStruct struct {
257 z complex64
258 i int
259 }
260
261 func complexField(c bool) float32 {
262
263
264
265 cs := complexStruct{complex(3, 4), 10}
266 p := &cs
267 if c {
268 p.i += 5
269 }
270 return real(p.z) + imag(p.z) + float32(p.i)
271 }
272
273 func main() {}
274
View as plain text