Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/internal/obj/riscv/asm_test.go

Documentation: cmd/internal/obj/riscv

     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  	"internal/testenv"
    11  	"io/ioutil"
    12  	"os"
    13  	"os/exec"
    14  	"path/filepath"
    15  	"runtime"
    16  	"testing"
    17  )
    18  
    19  // TestLarge generates a very large file to verify that large
    20  // program builds successfully, in particular, too-far
    21  // conditional branches are fixed.
    22  func TestLarge(t *testing.T) {
    23  	if testing.Short() {
    24  		t.Skip("Skip in short mode")
    25  	}
    26  	testenv.MustHaveGoBuild(t)
    27  
    28  	dir, err := ioutil.TempDir("", "testlarge")
    29  	if err != nil {
    30  		t.Fatalf("could not create directory: %v", err)
    31  	}
    32  	defer os.RemoveAll(dir)
    33  
    34  	// Generate a very large function.
    35  	buf := bytes.NewBuffer(make([]byte, 0, 7000000))
    36  	gen(buf)
    37  
    38  	tmpfile := filepath.Join(dir, "x.s")
    39  	err = ioutil.WriteFile(tmpfile, buf.Bytes(), 0644)
    40  	if err != nil {
    41  		t.Fatalf("can't write output: %v\n", err)
    42  	}
    43  
    44  	// Build generated file.
    45  	cmd := exec.Command(testenv.GoToolPath(t), "tool", "asm", "-o", filepath.Join(dir, "x.o"), tmpfile)
    46  	cmd.Env = append(os.Environ(), "GOARCH=riscv64", "GOOS=linux")
    47  	out, err := cmd.CombinedOutput()
    48  	if err != nil {
    49  		t.Errorf("Build failed: %v, output: %s", err, out)
    50  	}
    51  }
    52  
    53  // gen generates a very large program, with a very far conditional branch.
    54  func gen(buf *bytes.Buffer) {
    55  	fmt.Fprintln(buf, "TEXT f(SB),0,$0-0")
    56  	fmt.Fprintln(buf, "BEQ X0, X0, label")
    57  	for i := 0; i < 1<<19; i++ {
    58  		fmt.Fprintln(buf, "ADD $0, X0, X0")
    59  	}
    60  	fmt.Fprintln(buf, "label:")
    61  	fmt.Fprintln(buf, "ADD $0, X0, X0")
    62  }
    63  
    64  // Issue 20348.
    65  func TestNoRet(t *testing.T) {
    66  	dir, err := ioutil.TempDir("", "testnoret")
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	defer os.RemoveAll(dir)
    71  	tmpfile := filepath.Join(dir, "x.s")
    72  	if err := ioutil.WriteFile(tmpfile, []byte("TEXT ·stub(SB),$0-0\nNOP\n"), 0644); err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	cmd := exec.Command(testenv.GoToolPath(t), "tool", "asm", "-o", filepath.Join(dir, "x.o"), tmpfile)
    76  	cmd.Env = append(os.Environ(), "GOARCH=riscv64", "GOOS=linux")
    77  	if out, err := cmd.CombinedOutput(); err != nil {
    78  		t.Errorf("%v\n%s", err, out)
    79  	}
    80  }
    81  
    82  func TestImmediateSplitting(t *testing.T) {
    83  	dir, err := ioutil.TempDir("", "testimmsplit")
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	defer os.RemoveAll(dir)
    88  	tmpfile := filepath.Join(dir, "x.s")
    89  	asm := `
    90  TEXT _stub(SB),$0-0
    91  	LB	4096(X5), X6
    92  	LH	4096(X5), X6
    93  	LW	4096(X5), X6
    94  	LD	4096(X5), X6
    95  	LBU	4096(X5), X6
    96  	LHU	4096(X5), X6
    97  	LWU	4096(X5), X6
    98  	SB	X6, 4096(X5)
    99  	SH	X6, 4096(X5)
   100  	SW	X6, 4096(X5)
   101  	SD	X6, 4096(X5)
   102  
   103  	FLW	4096(X5), F6
   104  	FLD	4096(X5), F6
   105  	FSW	F6, 4096(X5)
   106  	FSD	F6, 4096(X5)
   107  
   108  	MOVB	4096(X5), X6
   109  	MOVH	4096(X5), X6
   110  	MOVW	4096(X5), X6
   111  	MOV	4096(X5), X6
   112  	MOVBU	4096(X5), X6
   113  	MOVHU	4096(X5), X6
   114  	MOVWU	4096(X5), X6
   115  
   116  	MOVB	X6, 4096(X5)
   117  	MOVH	X6, 4096(X5)
   118  	MOVW	X6, 4096(X5)
   119  	MOV	X6, 4096(X5)
   120  
   121  	MOVF	4096(X5), F6
   122  	MOVD	4096(X5), F6
   123  	MOVF	F6, 4096(X5)
   124  	MOVD	F6, 4096(X5)
   125  `
   126  	if err := ioutil.WriteFile(tmpfile, []byte(asm), 0644); err != nil {
   127  		t.Fatal(err)
   128  	}
   129  	cmd := exec.Command(testenv.GoToolPath(t), "tool", "asm", "-o", filepath.Join(dir, "x.o"), tmpfile)
   130  	cmd.Env = append(os.Environ(), "GOARCH=riscv64", "GOOS=linux")
   131  	if out, err := cmd.CombinedOutput(); err != nil {
   132  		t.Errorf("%v\n%s", err, out)
   133  	}
   134  }
   135  
   136  func TestBranch(t *testing.T) {
   137  	if testing.Short() {
   138  		t.Skip("Skipping in short mode")
   139  	}
   140  	if runtime.GOARCH != "riscv64" {
   141  		t.Skip("Requires riscv64 to run")
   142  	}
   143  
   144  	testenv.MustHaveGoBuild(t)
   145  
   146  	cmd := exec.Command(testenv.GoToolPath(t), "test")
   147  	cmd.Dir = "testdata/testbranch"
   148  	if out, err := testenv.CleanCmdEnv(cmd).CombinedOutput(); err != nil {
   149  		t.Errorf("Branch test failed: %v\n%s", err, out)
   150  	}
   151  }
   152  

View as plain text