github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/compile/internal/gc/zerorange_test.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gc
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  var glob = 3
    12  var globp *int64
    13  
    14  // Testing compilation of arch.ZeroRange of various sizes.
    15  
    16  // By storing a pointer to an int64 output param in a global, the compiler must
    17  // ensure that output param is allocated on the heap. Also, since there is a
    18  // defer, the pointer to each output param must be zeroed in the prologue (see
    19  // plive.go:epilogue()). So, we will get a block of one or more stack slots that
    20  // need to be zeroed. Hence, we are testing compilation completes successfully when
    21  // zerorange calls of various sizes (8-136 bytes) are generated. We are not
    22  // testing runtime correctness (which is hard to do for the current uses of
    23  // ZeroRange).
    24  
    25  func TestZeroRange(t *testing.T) {
    26  	testZeroRange8(t)
    27  	testZeroRange16(t)
    28  	testZeroRange32(t)
    29  	testZeroRange64(t)
    30  	testZeroRange136(t)
    31  }
    32  
    33  func testZeroRange8(t *testing.T) (r int64) {
    34  	defer func() {
    35  		glob = 4
    36  	}()
    37  	globp = &r
    38  	return
    39  }
    40  
    41  func testZeroRange16(t *testing.T) (r, s int64) {
    42  	defer func() {
    43  		glob = 4
    44  	}()
    45  	globp = &r
    46  	globp = &s
    47  	return
    48  }
    49  
    50  func testZeroRange32(t *testing.T) (r, s, t2, u int64) {
    51  	defer func() {
    52  		glob = 4
    53  	}()
    54  	globp = &r
    55  	globp = &s
    56  	globp = &t2
    57  	globp = &u
    58  	return
    59  }
    60  
    61  func testZeroRange64(t *testing.T) (r, s, t2, u, v, w, x, y int64) {
    62  	defer func() {
    63  		glob = 4
    64  	}()
    65  	globp = &r
    66  	globp = &s
    67  	globp = &t2
    68  	globp = &u
    69  	globp = &v
    70  	globp = &w
    71  	globp = &x
    72  	globp = &y
    73  	return
    74  }
    75  
    76  func testZeroRange136(t *testing.T) (r, s, t2, u, v, w, x, y, r1, s1, t1, u1, v1, w1, x1, y1, z1 int64) {
    77  	defer func() {
    78  		glob = 4
    79  	}()
    80  	globp = &r
    81  	globp = &s
    82  	globp = &t2
    83  	globp = &u
    84  	globp = &v
    85  	globp = &w
    86  	globp = &x
    87  	globp = &y
    88  	globp = &r1
    89  	globp = &s1
    90  	globp = &t1
    91  	globp = &u1
    92  	globp = &v1
    93  	globp = &w1
    94  	globp = &x1
    95  	globp = &y1
    96  	globp = &z1
    97  	return
    98  }