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

View as plain text