Source file test/abi/double_nested_addressed_struct.go

     1  // run
     2  
     3  //go:build !wasm
     4  // +build !wasm
     5  
     6  // Copyright 2021 The Go Authors. All rights reserved.
     7  // Use of this source code is governed by a BSD-style
     8  // license that can be found in the LICENSE file.
     9  
    10  // wasm is excluded because the compiler chatter about register abi pragma ends up
    11  // on stdout, and causes the expected output to not match.
    12  
    13  package main
    14  
    15  import (
    16  	"fmt"
    17  )
    18  
    19  var sink *string
    20  
    21  type stringPair struct {
    22  	a, b string
    23  }
    24  
    25  type stringPairPair struct {
    26  	x, y stringPair
    27  }
    28  
    29  // The goal of this test is to be sure that the call arg/result expander works correctly
    30  // for a corner case of passing a 2-nested struct that fits in registers to/from calls.
    31  // AND, the struct has its address taken.
    32  
    33  //go:registerparams
    34  //go:noinline
    35  func H(spp stringPairPair) string {
    36  	F(&spp)
    37  	return spp.x.a + " " + spp.x.b + " " + spp.y.a + " " + spp.y.b
    38  }
    39  
    40  //go:registerparams
    41  //go:noinline
    42  func G(d, c, b, a string) stringPairPair {
    43  	return stringPairPair{stringPair{a, b}, stringPair{c, d}}
    44  }
    45  
    46  //go:registerparams
    47  //go:noinline
    48  func F(spp *stringPairPair) {
    49  	spp.x.a, spp.x.b, spp.y.a, spp.y.b = spp.y.b, spp.y.a, spp.x.b, spp.x.a
    50  }
    51  
    52  func main() {
    53  	spp := G("this", "is", "a", "test")
    54  	s := H(spp)
    55  	gotVsWant(s, "this is a test")
    56  }
    57  
    58  func gotVsWant(got, want string) {
    59  	if got != want {
    60  		fmt.Printf("FAIL, got %s, wanted %s\n", got, want)
    61  	}
    62  }
    63  

View as plain text