github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/compile/internal/ssa/testdata/hist.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This is the input program for an end-to-end test of the DWARF produced
     6  // by the compiler. It is compiled with various flags, then the resulting
     7  // binary is "debugged" under the control of a harness.  Because the compile+debug
     8  // step is time-consuming, the tests for different bugs are all accumulated here
     9  // so that their cost is only the time to "n" through the additional code.
    10  
    11  package main
    12  
    13  import (
    14  	"bufio"
    15  	"fmt"
    16  	"io"
    17  	"os"
    18  	"strconv"
    19  	"strings"
    20  )
    21  
    22  type point struct {
    23  	x, y int
    24  }
    25  
    26  type line struct {
    27  	begin, end point
    28  }
    29  
    30  var zero int
    31  var sink int
    32  
    33  //go:noinline
    34  func tinycall() {
    35  }
    36  
    37  func ensure(n int, sl []int) []int {
    38  	for len(sl) <= n {
    39  		sl = append(sl, 0)
    40  	}
    41  	return sl
    42  }
    43  
    44  var cannedInput string = `1
    45  1
    46  1
    47  2
    48  2
    49  2
    50  4
    51  4
    52  5
    53  `
    54  
    55  func test() {
    56  	// For #19868
    57  	l := line{point{1 + zero, 2 + zero}, point{3 + zero, 4 + zero}}
    58  	tinycall()                // this forces l etc to stack
    59  	dx := l.end.x - l.begin.x //gdb-dbg=(l.begin.x,l.end.y)//gdb-opt=(l,dx/O,dy/O)
    60  	dy := l.end.y - l.begin.y //gdb-opt=(dx,dy/O)
    61  	sink = dx + dy            //gdb-opt=(dx,dy)
    62  	// For #21098
    63  	hist := make([]int, 7)                                //gdb-opt=(dx/O,dy/O) // TODO sink is missing if this code is in 'test' instead of 'main'
    64  	var reader io.Reader = strings.NewReader(cannedInput) //gdb-dbg=(hist/A) // TODO cannedInput/A is missing if this code is in 'test' instead of 'main'
    65  	if len(os.Args) > 1 {
    66  		var err error
    67  		reader, err = os.Open(os.Args[1])
    68  		if err != nil {
    69  			fmt.Fprintf(os.Stderr, "There was an error opening %s: %v\n", os.Args[1], err)
    70  			return
    71  		}
    72  	}
    73  	scanner := bufio.NewScanner(reader)
    74  	for scanner.Scan() { //gdb-opt=(scanner/A)
    75  		s := scanner.Text()
    76  		i, err := strconv.ParseInt(s, 10, 64)
    77  		if err != nil { //gdb-dbg=(i) //gdb-opt=(err,hist,i)
    78  			fmt.Fprintf(os.Stderr, "There was an error: %v\n", err)
    79  			return
    80  		}
    81  		hist = ensure(int(i), hist)
    82  		hist[int(i)]++
    83  	}
    84  	t := 0
    85  	n := 0
    86  	for i, a := range hist {
    87  		if a == 0 { //gdb-opt=(a,n,t)
    88  			continue
    89  		}
    90  		t += i * a
    91  		n += a
    92  		fmt.Fprintf(os.Stderr, "%d\t%d\t%d\t%d\t%d\n", i, a, n, i*a, t) //gdb-dbg=(n,i,t)
    93  	}
    94  }
    95  
    96  func main() {
    97  	growstack() // Use stack early to prevent growth during test, which confuses gdb
    98  	test()
    99  }
   100  
   101  var snk string
   102  
   103  //go:noinline
   104  func growstack() {
   105  	snk = fmt.Sprintf("%#v,%#v,%#v", 1, true, "cat")
   106  }