Source file test/abi/double_nested_addressed_struct.go

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

View as plain text