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

View as plain text