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

View as plain text