1// asmcheck
2
3// Copyright 2018 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7package codegen
8
9// This file contains code generation tests related to the handling of
10// map types.
11
12// ------------------- //
13//     Access Const    //
14// ------------------- //
15
16// Direct use of constants in fast map access calls (Issue #19015).
17
18func AccessInt1(m map[int]int) int {
19	// amd64:"MOV[LQ]\t[$]5"
20	return m[5]
21}
22
23func AccessInt2(m map[int]int) bool {
24	// amd64:"MOV[LQ]\t[$]5"
25	_, ok := m[5]
26	return ok
27}
28
29func AccessString1(m map[string]int) int {
30	// amd64:`.*"abc"`
31	return m["abc"]
32}
33
34func AccessString2(m map[string]int) bool {
35	// amd64:`.*"abc"`
36	_, ok := m["abc"]
37	return ok
38}
39
40// ------------------- //
41//  String Conversion  //
42// ------------------- //
43
44func LookupStringConversionSimple(m map[string]int, bytes []byte) int {
45	// amd64:-`.*runtime\.slicebytetostring\(`
46	return m[string(bytes)]
47}
48
49func LookupStringConversionStructLit(m map[struct{ string }]int, bytes []byte) int {
50	// amd64:-`.*runtime\.slicebytetostring\(`
51	return m[struct{ string }{string(bytes)}]
52}
53
54func LookupStringConversionArrayLit(m map[[2]string]int, bytes []byte) int {
55	// amd64:-`.*runtime\.slicebytetostring\(`
56	return m[[2]string{string(bytes), string(bytes)}]
57}
58
59func LookupStringConversionNestedLit(m map[[1]struct{ s [1]string }]int, bytes []byte) int {
60	// amd64:-`.*runtime\.slicebytetostring\(`
61	return m[[1]struct{ s [1]string }{struct{ s [1]string }{s: [1]string{string(bytes)}}}]
62}
63
64func LookupStringConversionKeyedArrayLit(m map[[2]string]int, bytes []byte) int {
65	// amd64:-`.*runtime\.slicebytetostring\(`
66	return m[[2]string{0: string(bytes)}]
67}
68
69// ------------------- //
70//     Map Clear       //
71// ------------------- //
72
73// Optimization of map clear idiom (Issue #20138).
74
75func MapClearReflexive(m map[int]int) {
76	// amd64:`.*runtime\.mapclear`
77	// amd64:-`.*runtime\.mapiterinit`
78	for k := range m {
79		delete(m, k)
80	}
81}
82
83func MapClearIndirect(m map[int]int) {
84	s := struct{ m map[int]int }{m: m}
85	// amd64:`.*runtime\.mapclear`
86	// amd64:-`.*runtime\.mapiterinit`
87	for k := range s.m {
88		delete(s.m, k)
89	}
90}
91
92func MapClearPointer(m map[*byte]int) {
93	// amd64:`.*runtime\.mapclear`
94	// amd64:-`.*runtime\.mapiterinit`
95	for k := range m {
96		delete(m, k)
97	}
98}
99
100func MapClearNotReflexive(m map[float64]int) {
101	// amd64:`.*runtime\.mapiterinit`
102	// amd64:-`.*runtime\.mapclear`
103	for k := range m {
104		delete(m, k)
105	}
106}
107
108func MapClearInterface(m map[interface{}]int) {
109	// amd64:`.*runtime\.mapiterinit`
110	// amd64:-`.*runtime\.mapclear`
111	for k := range m {
112		delete(m, k)
113	}
114}
115
116func MapClearSideEffect(m map[int]int) int {
117	k := 0
118	// amd64:`.*runtime\.mapiterinit`
119	// amd64:-`.*runtime\.mapclear`
120	for k = range m {
121		delete(m, k)
122	}
123	return k
124}
125
126func MapLiteralSizing(x int) (map[int]int, map[int]int) {
127	// This is tested for internal/abi/maps.go:MapBucketCountBits={3,4,5}
128	// amd64:"MOVL\t[$]33,"
129	m := map[int]int{
130		0:  0,
131		1:  1,
132		2:  2,
133		3:  3,
134		4:  4,
135		5:  5,
136		6:  6,
137		7:  7,
138		8:  8,
139		9:  9,
140		10: 10,
141		11: 11,
142		12: 12,
143		13: 13,
144		14: 14,
145		15: 15,
146		16: 16,
147		17: 17,
148		18: 18,
149		19: 19,
150		20: 20,
151		21: 21,
152		22: 22,
153		23: 23,
154		24: 24,
155		25: 25,
156		26: 26,
157		27: 27,
158		28: 28,
159		29: 29,
160		30: 30,
161		31: 32,
162		32: 32,
163	}
164	// amd64:"MOVL\t[$]33,"
165	n := map[int]int{
166		0:  0,
167		1:  1,
168		2:  2,
169		3:  3,
170		4:  4,
171		5:  5,
172		6:  6,
173		7:  7,
174		8:  8,
175		9:  9,
176		10: 10,
177		11: 11,
178		12: 12,
179		13: 13,
180		14: 14,
181		15: 15,
182		16: 16,
183		17: 17,
184		18: 18,
185		19: 19,
186		20: 20,
187		21: 21,
188		22: 22,
189		23: 23,
190		24: 24,
191		25: 25,
192		26: 26,
193		27: 27,
194		28: 28,
195		29: 29,
196		30: 30,
197		31: 32,
198		32: 32,
199	}
200	return m, n
201}
202