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

View as plain text