github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/internal/obj/arm64/asm_test.go (about)

     1  // Copyright 2016 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  package arm64
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"github.com/gagliardetto/golang-go/not-internal/testenv"
    11  	"io/ioutil"
    12  	"os"
    13  	"os/exec"
    14  	"path/filepath"
    15  	"testing"
    16  )
    17  
    18  // TestLarge generates a very large file to verify that large
    19  // program builds successfully, in particular, too-far
    20  // conditional branches are fixed.
    21  func TestLarge(t *testing.T) {
    22  	if testing.Short() {
    23  		t.Skip("Skip in short mode")
    24  	}
    25  	testenv.MustHaveGoBuild(t)
    26  
    27  	dir, err := ioutil.TempDir("", "testlarge")
    28  	if err != nil {
    29  		t.Fatalf("could not create directory: %v", err)
    30  	}
    31  	defer os.RemoveAll(dir)
    32  
    33  	// generate a very large function
    34  	buf := bytes.NewBuffer(make([]byte, 0, 7000000))
    35  	gen(buf)
    36  
    37  	tmpfile := filepath.Join(dir, "x.s")
    38  	err = ioutil.WriteFile(tmpfile, buf.Bytes(), 0644)
    39  	if err != nil {
    40  		t.Fatalf("can't write output: %v\n", err)
    41  	}
    42  
    43  	// build generated file
    44  	cmd := exec.Command(testenv.GoToolPath(t), "tool", "asm", "-o", filepath.Join(dir, "x.o"), tmpfile)
    45  	cmd.Env = append(os.Environ(), "GOARCH=arm64", "GOOS=linux")
    46  	out, err := cmd.CombinedOutput()
    47  	if err != nil {
    48  		t.Errorf("Build failed: %v, output: %s", err, out)
    49  	}
    50  }
    51  
    52  // gen generates a very large program, with a very far conditional branch.
    53  func gen(buf *bytes.Buffer) {
    54  	fmt.Fprintln(buf, "TEXT f(SB),0,$0-0")
    55  	fmt.Fprintln(buf, "TBZ $5, R0, label")
    56  	fmt.Fprintln(buf, "CBZ R0, label")
    57  	fmt.Fprintln(buf, "BEQ label")
    58  	for i := 0; i < 1<<19; i++ {
    59  		fmt.Fprintln(buf, "MOVD R0, R1")
    60  	}
    61  	fmt.Fprintln(buf, "label:")
    62  	fmt.Fprintln(buf, "RET")
    63  }
    64  
    65  // Issue 20348.
    66  func TestNoRet(t *testing.T) {
    67  	dir, err := ioutil.TempDir("", "testnoret")
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	defer os.RemoveAll(dir)
    72  	tmpfile := filepath.Join(dir, "x.s")
    73  	if err := ioutil.WriteFile(tmpfile, []byte("TEXT ·stub(SB),$0-0\nNOP\n"), 0644); err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	cmd := exec.Command(testenv.GoToolPath(t), "tool", "asm", "-o", filepath.Join(dir, "x.o"), tmpfile)
    77  	cmd.Env = append(os.Environ(), "GOARCH=arm64", "GOOS=linux")
    78  	if out, err := cmd.CombinedOutput(); err != nil {
    79  		t.Errorf("%v\n%s", err, out)
    80  	}
    81  }