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

     1  // Copyright 2023 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 loong64
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"internal/testenv"
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  )
    15  
    16  const genBufSize = (1024 * 1024 * 32) // 32MB
    17  
    18  // TestLargeBranch generates a large function with a very far conditional
    19  // branch, in order to ensure that it assembles successfully.
    20  func TestLargeBranch(t *testing.T) {
    21  	if testing.Short() {
    22  		t.Skip("Skipping test in short mode")
    23  	}
    24  	testenv.MustHaveGoBuild(t)
    25  
    26  	dir, err := os.MkdirTemp("", "testlargebranch")
    27  	if err != nil {
    28  		t.Fatalf("Could not create directory: %v", err)
    29  	}
    30  	defer os.RemoveAll(dir)
    31  
    32  	// Generate a very large function.
    33  	buf := bytes.NewBuffer(make([]byte, 0, genBufSize))
    34  	genLargeBranch(buf)
    35  
    36  	tmpfile := filepath.Join(dir, "x.s")
    37  	if err := os.WriteFile(tmpfile, buf.Bytes(), 0644); err != nil {
    38  		t.Fatalf("Failed to write file: %v", err)
    39  	}
    40  
    41  	// Assemble generated file.
    42  	cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "asm", "-o", filepath.Join(dir, "x.o"), tmpfile)
    43  	cmd.Env = append(os.Environ(), "GOARCH=loong64", "GOOS=linux")
    44  	out, err := cmd.CombinedOutput()
    45  	if err != nil {
    46  		t.Errorf("Build failed: %v, output: %s", err, out)
    47  	}
    48  }
    49  
    50  func genLargeBranch(buf *bytes.Buffer) {
    51  	genSize1 := (1 << 16) + 16
    52  	genSize2 := (1 << 21) + 16
    53  
    54  	fmt.Fprintln(buf, "TEXT f(SB),0,$0-0")
    55  	fmt.Fprintln(buf, "BEQ R5, R6, label18")
    56  	fmt.Fprintln(buf, "BNE R5, R6, label18")
    57  	fmt.Fprintln(buf, "BGE R5, R6, label18")
    58  
    59  	fmt.Fprintln(buf, "BGEU R5, R6, label18")
    60  	fmt.Fprintln(buf, "BLTU R5, R6, label18")
    61  
    62  	fmt.Fprintln(buf, "BLEZ R5, label18")
    63  	fmt.Fprintln(buf, "BGEZ R5, label18")
    64  	fmt.Fprintln(buf, "BLTZ R5, label18")
    65  	fmt.Fprintln(buf, "BGTZ R5, label18")
    66  
    67  	fmt.Fprintln(buf, "BFPT label23")
    68  	fmt.Fprintln(buf, "BFPF label23")
    69  
    70  	fmt.Fprintln(buf, "BEQ R5, label23")
    71  	fmt.Fprintln(buf, "BNE R5, label23")
    72  
    73  	for i := 0; i <= genSize1; i++ {
    74  		fmt.Fprintln(buf, "ADDV $0, R0, R0")
    75  	}
    76  
    77  	fmt.Fprintln(buf, "label18:")
    78  	for i := 0; i <= (genSize2 - genSize1); i++ {
    79  		fmt.Fprintln(buf, "ADDV $0, R0, R0")
    80  	}
    81  
    82  	fmt.Fprintln(buf, "label23:")
    83  	fmt.Fprintln(buf, "ADDV $0, R0, R0")
    84  	fmt.Fprintln(buf, "RET")
    85  }
    86  

View as plain text