Source file test/abi/named_return_stuff.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  var y int
    21  
    22  //go:registerparams
    23  //go:noinline
    24  func F(a, b, c *int) (x int) {
    25  	x = *a
    26  	G(&x)
    27  	x += *b
    28  	G(&x)
    29  	x += *c
    30  	G(&x)
    31  	return
    32  }
    33  
    34  //go:registerparams
    35  //go:noinline
    36  func G(x *int) {
    37  	y += *x
    38  	fmt.Println("y = ", y)
    39  }
    40  
    41  //go:registerparams
    42  //go:noinline
    43  func X() {
    44  	*sink += " !!!!!!!!!!!!!!!"
    45  }
    46  
    47  //go:registerparams
    48  //go:noinline
    49  func H(s, t string) (result string) { // result leaks to heap
    50  	result = "Aloha! " + s + " " + t
    51  	sink = &result
    52  	r := ""
    53  	if len(s) <= len(t) {
    54  		r = "OKAY! "
    55  		X()
    56  	}
    57  	return r + result
    58  }
    59  
    60  //go:registerparams
    61  //go:noinline
    62  func K(s, t string) (result string) { // result spills
    63  	result = "Aloha! " + s + " " + t
    64  	r := ""
    65  	if len(s) <= len(t) {
    66  		r = "OKAY! "
    67  		X()
    68  	}
    69  	return r + result
    70  }
    71  
    72  func main() {
    73  	a, b, c := 1, 4, 16
    74  	x := F(&a, &b, &c)
    75  	fmt.Printf("x = %d\n", x)
    76  
    77  	y := H("Hello", "World!")
    78  	fmt.Println("len(y) =", len(y))
    79  	fmt.Println("y =", y)
    80  	z := H("Hello", "Pal!")
    81  	fmt.Println("len(z) =", len(z))
    82  	fmt.Println("z =", z)
    83  
    84  	fmt.Println()
    85  
    86  	y = K("Hello", "World!")
    87  	fmt.Println("len(y) =", len(y))
    88  	fmt.Println("y =", y)
    89  	z = K("Hello", "Pal!")
    90  	fmt.Println("len(z) =", len(z))
    91  	fmt.Println("z =", z)
    92  
    93  }
    94  

View as plain text