Source file src/cmd/cgo/internal/testshared/testdata/global/main.go

     1  // Copyright 2017 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 main
     6  
     7  import (
     8  	"testshared/globallib"
     9  )
    10  
    11  //go:noinline
    12  func testLoop() {
    13  	for i, s := range globallib.Data {
    14  		if s != int64(i) {
    15  			panic("testLoop: mismatch")
    16  		}
    17  	}
    18  }
    19  
    20  //go:noinline
    21  func ptrData() *[1<<20 + 10]int64 {
    22  	return &globallib.Data
    23  }
    24  
    25  //go:noinline
    26  func testMediumOffset() {
    27  	for i, s := range globallib.Data[1<<16-2:] {
    28  		if s != int64(i)+1<<16-2 {
    29  			panic("testMediumOffset: index mismatch")
    30  		}
    31  	}
    32  
    33  	x := globallib.Data[1<<16-1]
    34  	if x != 1<<16-1 {
    35  		panic("testMediumOffset: direct mismatch")
    36  	}
    37  
    38  	y := &globallib.Data[1<<16-3]
    39  	if y != &ptrData()[1<<16-3] {
    40  		panic("testMediumOffset: address mismatch")
    41  	}
    42  }
    43  
    44  //go:noinline
    45  func testLargeOffset() {
    46  	for i, s := range globallib.Data[1<<20:] {
    47  		if s != int64(i)+1<<20 {
    48  			panic("testLargeOffset: index mismatch")
    49  		}
    50  	}
    51  
    52  	x := globallib.Data[1<<20+1]
    53  	if x != 1<<20+1 {
    54  		panic("testLargeOffset: direct mismatch")
    55  	}
    56  
    57  	y := &globallib.Data[1<<20+2]
    58  	if y != &ptrData()[1<<20+2] {
    59  		panic("testLargeOffset: address mismatch")
    60  	}
    61  }
    62  
    63  func main() {
    64  	testLoop()
    65  
    66  	// SSA rules commonly merge offsets into addresses. These
    67  	// tests access global data in different ways to try
    68  	// and exercise different SSA rules.
    69  	testMediumOffset()
    70  	testLargeOffset()
    71  }
    72  

View as plain text