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  // ------------------- //
    70  //     Map Clear       //
    71  // ------------------- //
    72  
    73  // Optimization of map clear idiom (Issue #20138).
    74  
    75  func 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  
    83  func 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  
    92  func 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  
   100  func 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  
   108  func 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  
   116  func 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  
   126  func MapLiteralSizing(x int) (map[int]int, map[int]int) {
   127  	// amd64:"MOVL\t[$]10,"
   128  	m := map[int]int{
   129  		0: 0,
   130  		1: 1,
   131  		2: 2,
   132  		3: 3,
   133  		4: 4,
   134  		5: 5,
   135  		6: 6,
   136  		7: 7,
   137  		8: 8,
   138  		9: 9,
   139  	}
   140  	// amd64:"MOVL\t[$]10,"
   141  	n := map[int]int{
   142  		0: x,
   143  		1: x,
   144  		2: x,
   145  		3: x,
   146  		4: x,
   147  		5: x,
   148  		6: x,
   149  		7: x,
   150  		8: x,
   151  		9: x,
   152  	}
   153  	return m, n
   154  }
   155  

View as plain text