Source file test/codegen/maps.go

     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  
     7  package 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  
    18  func AccessInt1(m map[int]int) int {
    19  	// amd64:"MOV[LQ] [$]5"
    20  	return m[5]
    21  }
    22  
    23  func AccessInt2(m map[int]int) bool {
    24  	// amd64:"MOV[LQ] [$]5"
    25  	_, ok := m[5]
    26  	return ok
    27  }
    28  
    29  func AccessString1(m map[string]int) int {
    30  	// amd64:`.*"abc"`
    31  	return m["abc"]
    32  }
    33  
    34  func AccessString2(m map[string]int) bool {
    35  	// amd64:`.*"abc"`
    36  	_, ok := m["abc"]
    37  	return ok
    38  }
    39  
    40  func AccessStringIntArray2(m map[string][16]int, k string) bool {
    41  	// amd64:-"MOVUPS"
    42  	_, ok := m[k]
    43  	return ok
    44  }
    45  
    46  type Struct struct {
    47  	A, B, C, D, E, F, G, H, I, J int
    48  }
    49  
    50  func AccessStringStruct2(m map[string]Struct, k string) bool {
    51  	// amd64:-"MOVUPS"
    52  	_, ok := m[k]
    53  	return ok
    54  }
    55  
    56  func AccessIntArrayLarge2(m map[int][512]int, k int) bool {
    57  	// amd64:-"REP",-"MOVSQ"
    58  	_, ok := m[k]
    59  	return ok
    60  }
    61  
    62  // ------------------- //
    63  //  String Conversion  //
    64  // ------------------- //
    65  
    66  func LookupStringConversionSimple(m map[string]int, bytes []byte) int {
    67  	// amd64:-`.*runtime\.slicebytetostring\(`
    68  	return m[string(bytes)]
    69  }
    70  
    71  func LookupStringConversionStructLit(m map[struct{ string }]int, bytes []byte) int {
    72  	// amd64:-`.*runtime\.slicebytetostring\(`
    73  	return m[struct{ string }{string(bytes)}]
    74  }
    75  
    76  func LookupStringConversionArrayLit(m map[[2]string]int, bytes []byte) int {
    77  	// amd64:-`.*runtime\.slicebytetostring\(`
    78  	return m[[2]string{string(bytes), string(bytes)}]
    79  }
    80  
    81  func LookupStringConversionNestedLit(m map[[1]struct{ s [1]string }]int, bytes []byte) int {
    82  	// amd64:-`.*runtime\.slicebytetostring\(`
    83  	return m[[1]struct{ s [1]string }{struct{ s [1]string }{s: [1]string{string(bytes)}}}]
    84  }
    85  
    86  func LookupStringConversionKeyedArrayLit(m map[[2]string]int, bytes []byte) int {
    87  	// amd64:-`.*runtime\.slicebytetostring\(`
    88  	return m[[2]string{0: string(bytes)}]
    89  }
    90  
    91  func LookupStringConversion1(m map[string]int, bytes []byte) int {
    92  	// amd64:-`.*runtime\.slicebytetostring\(`
    93  	s := string(bytes)
    94  	return m[s]
    95  }
    96  func LookupStringConversion2(m *map[string]int, bytes []byte) int {
    97  	// amd64:-`.*runtime\.slicebytetostring\(`
    98  	s := string(bytes)
    99  	return (*m)[s]
   100  }
   101  func LookupStringConversion3(m map[string]int, bytes []byte) (int, bool) {
   102  	// amd64:-`.*runtime\.slicebytetostring\(`
   103  	s := string(bytes)
   104  	r, ok := m[s]
   105  	return r, ok
   106  }
   107  func DeleteStringConversion(m map[string]int, bytes []byte) {
   108  	// amd64:-`.*runtime\.slicebytetostring\(`
   109  	s := string(bytes)
   110  	delete(m, s)
   111  }
   112  
   113  // ------------------- //
   114  //     Map Clear       //
   115  // ------------------- //
   116  
   117  // Optimization of map clear idiom (Issue #20138).
   118  
   119  func MapClearReflexive(m map[int]int) {
   120  	// amd64:`.*runtime\.mapclear`
   121  	// amd64:-`.*runtime\.(mapiterinit|mapIterStart)`
   122  	for k := range m {
   123  		delete(m, k)
   124  	}
   125  }
   126  
   127  func MapClearIndirect(m map[int]int) {
   128  	s := struct{ m map[int]int }{m: m}
   129  	// amd64:`.*runtime\.mapclear`
   130  	// amd64:-`.*runtime\.(mapiterinit|mapIterStart)`
   131  	for k := range s.m {
   132  		delete(s.m, k)
   133  	}
   134  }
   135  
   136  func MapClearPointer(m map[*byte]int) {
   137  	// amd64:`.*runtime\.mapclear`
   138  	// amd64:-`.*runtime\.(mapiterinit|mapIterStart)`
   139  	for k := range m {
   140  		delete(m, k)
   141  	}
   142  }
   143  
   144  func MapClearNotReflexive(m map[float64]int) {
   145  	// amd64:`.*runtime\.(mapiterinit|mapIterStart)`
   146  	// amd64:-`.*runtime\.mapclear`
   147  	for k := range m {
   148  		delete(m, k)
   149  	}
   150  }
   151  
   152  func MapClearInterface(m map[interface{}]int) {
   153  	// amd64:`.*runtime\.(mapiterinit|mapIterStart)`
   154  	// amd64:-`.*runtime\.mapclear`
   155  	for k := range m {
   156  		delete(m, k)
   157  	}
   158  }
   159  
   160  func MapClearSideEffect(m map[int]int) int {
   161  	k := 0
   162  	// amd64:`.*runtime\.(mapiterinit|mapIterStart)`
   163  	// amd64:-`.*runtime\.mapclear`
   164  	for k = range m {
   165  		delete(m, k)
   166  	}
   167  	return k
   168  }
   169  
   170  func MapLiteralSizing(x int) (map[int]int, map[int]int) {
   171  	// This is tested for internal/abi/maps.go:MapBucketCountBits={3,4,5}
   172  	// amd64:"MOVL [$]33,"
   173  	m := map[int]int{
   174  		0:  0,
   175  		1:  1,
   176  		2:  2,
   177  		3:  3,
   178  		4:  4,
   179  		5:  5,
   180  		6:  6,
   181  		7:  7,
   182  		8:  8,
   183  		9:  9,
   184  		10: 10,
   185  		11: 11,
   186  		12: 12,
   187  		13: 13,
   188  		14: 14,
   189  		15: 15,
   190  		16: 16,
   191  		17: 17,
   192  		18: 18,
   193  		19: 19,
   194  		20: 20,
   195  		21: 21,
   196  		22: 22,
   197  		23: 23,
   198  		24: 24,
   199  		25: 25,
   200  		26: 26,
   201  		27: 27,
   202  		28: 28,
   203  		29: 29,
   204  		30: 30,
   205  		31: 32,
   206  		32: 32,
   207  	}
   208  	// amd64:"MOVL [$]33,"
   209  	n := map[int]int{
   210  		0:  0,
   211  		1:  1,
   212  		2:  2,
   213  		3:  3,
   214  		4:  4,
   215  		5:  5,
   216  		6:  6,
   217  		7:  7,
   218  		8:  8,
   219  		9:  9,
   220  		10: 10,
   221  		11: 11,
   222  		12: 12,
   223  		13: 13,
   224  		14: 14,
   225  		15: 15,
   226  		16: 16,
   227  		17: 17,
   228  		18: 18,
   229  		19: 19,
   230  		20: 20,
   231  		21: 21,
   232  		22: 22,
   233  		23: 23,
   234  		24: 24,
   235  		25: 25,
   236  		26: 26,
   237  		27: 27,
   238  		28: 28,
   239  		29: 29,
   240  		30: 30,
   241  		31: 32,
   242  		32: 32,
   243  	}
   244  	return m, n
   245  }
   246  

View as plain text