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

     1  // Copyright 2019 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 riscv
     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=riscv64", "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, "BEQ X0, X0, label")
    56  	for i := 0; i < 1<<19; i++ {
    57  		fmt.Fprintln(buf, "ADD $0, X0, X0")
    58  	}
    59  	fmt.Fprintln(buf, "label:")
    60  	fmt.Fprintln(buf, "ADD $0, X0, X0")
    61  }
    62  
    63  // Issue 20348.
    64  func TestNoRet(t *testing.T) {
    65  	dir, err := ioutil.TempDir("", "testnoret")
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	defer os.RemoveAll(dir)
    70  	tmpfile := filepath.Join(dir, "x.s")
    71  	if err := ioutil.WriteFile(tmpfile, []byte("TEXT ·stub(SB),$0-0\nNOP\n"), 0644); err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	cmd := exec.Command(testenv.GoToolPath(t), "tool", "asm", "-o", filepath.Join(dir, "x.o"), tmpfile)
    75  	cmd.Env = append(os.Environ(), "GOARCH=riscv64", "GOOS=linux")
    76  	if out, err := cmd.CombinedOutput(); err != nil {
    77  		t.Errorf("%v\n%s", err, out)
    78  	}
    79  }
    80  
    81  func TestImmediateSplitting(t *testing.T) {
    82  	dir, err := ioutil.TempDir("", "testimmsplit")
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	defer os.RemoveAll(dir)
    87  	tmpfile := filepath.Join(dir, "x.s")
    88  	asm := `
    89  TEXT _stub(SB),$0-0
    90  	LB	4096(X5), X6
    91  	LH	4096(X5), X6
    92  	LW	4096(X5), X6
    93  	LD	4096(X5), X6
    94  	LBU	4096(X5), X6
    95  	LHU	4096(X5), X6
    96  	LWU	4096(X5), X6
    97  	SB	X6, 4096(X5)
    98  	SH	X6, 4096(X5)
    99  	SW	X6, 4096(X5)
   100  	SD	X6, 4096(X5)
   101  
   102  	FLW	4096(X5), F6
   103  	FLD	4096(X5), F6
   104  	FSW	F6, 4096(X5)
   105  	FSD	F6, 4096(X5)
   106  
   107  	MOVB	4096(X5), X6
   108  	MOVH	4096(X5), X6
   109  	MOVW	4096(X5), X6
   110  	MOV	4096(X5), X6
   111  	MOVBU	4096(X5), X6
   112  	MOVHU	4096(X5), X6
   113  	MOVWU	4096(X5), X6
   114  
   115  	MOVB	X6, 4096(X5)
   116  	MOVH	X6, 4096(X5)
   117  	MOVW	X6, 4096(X5)
   118  	MOV	X6, 4096(X5)
   119  
   120  	MOVF	4096(X5), F6
   121  	MOVD	4096(X5), F6
   122  	MOVF	F6, 4096(X5)
   123  	MOVD	F6, 4096(X5)
   124  `
   125  	if err := ioutil.WriteFile(tmpfile, []byte(asm), 0644); err != nil {
   126  		t.Fatal(err)
   127  	}
   128  	cmd := exec.Command(testenv.GoToolPath(t), "tool", "asm", "-o", filepath.Join(dir, "x.o"), tmpfile)
   129  	cmd.Env = append(os.Environ(), "GOARCH=riscv64", "GOOS=linux")
   130  	if out, err := cmd.CombinedOutput(); err != nil {
   131  		t.Errorf("%v\n%s", err, out)
   132  	}
   133  }