Source file test/abi/uglyfib.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 "fmt"
    16  
    17  // This test is designed to provoke a stack growth
    18  // in a way that very likely leaves junk in the
    19  // parameter save area if they aren't saved or spilled
    20  // there, as appropriate.
    21  
    22  //go:registerparams
    23  //go:noinline
    24  func f(x int, xm1, xm2, p *int) {
    25  	var y = [2]int{x - 4, 0}
    26  	if x < 2 {
    27  		*p += x
    28  		return
    29  	}
    30  	x -= 3
    31  	g(*xm1, xm2, &x, p)   // xm1 is no longer live.
    32  	h(*xm2, &x, &y[0], p) // xm2 is no longer live, but was spilled.
    33  }
    34  
    35  //go:registerparams
    36  //go:noinline
    37  func g(x int, xm1, xm2, p *int) {
    38  	var y = [3]int{x - 4, 0, 0}
    39  	if x < 2 {
    40  		*p += x
    41  		return
    42  	}
    43  	x -= 3
    44  	k(*xm2, &x, &y[0], p)
    45  	h(*xm1, xm2, &x, p)
    46  }
    47  
    48  //go:registerparams
    49  //go:noinline
    50  func h(x int, xm1, xm2, p *int) {
    51  	var y = [4]int{x - 4, 0, 0, 0}
    52  	if x < 2 {
    53  		*p += x
    54  		return
    55  	}
    56  	x -= 3
    57  	k(*xm1, xm2, &x, p)
    58  	f(*xm2, &x, &y[0], p)
    59  }
    60  
    61  //go:registerparams
    62  //go:noinline
    63  func k(x int, xm1, xm2, p *int) {
    64  	var y = [5]int{x - 4, 0, 0, 0, 0}
    65  	if x < 2 {
    66  		*p += x
    67  		return
    68  	}
    69  	x -= 3
    70  	f(*xm2, &x, &y[0], p)
    71  	g(*xm1, xm2, &x, p)
    72  }
    73  
    74  func main() {
    75  	x := 40
    76  	var y int
    77  	xm1 := x - 1
    78  	xm2 := x - 2
    79  	f(x, &xm1, &xm2, &y)
    80  
    81  	fmt.Printf("Fib(%d)=%d\n", x, y)
    82  }
    83  

View as plain text