Black Lives Matter. Support the Equal Justice Initiative.

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

Documentation: cmd/internal/obj/ppc64

     1  // Copyright 2020 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 ppc64
     6  
     7  import (
     8  	"internal/testenv"
     9  	"io/ioutil"
    10  	"os"
    11  	"os/exec"
    12  	"path/filepath"
    13  	"regexp"
    14  	"strings"
    15  	"testing"
    16  )
    17  
    18  var invalidPCAlignSrc = `
    19  TEXT test(SB),0,$0-0
    20  ADD $2, R3
    21  PCALIGN $64
    22  RET
    23  `
    24  
    25  var validPCAlignSrc = `
    26  TEXT test(SB),0,$0-0
    27  ADD $2, R3
    28  PCALIGN $16
    29  MOVD $8, R16
    30  ADD $8, R4
    31  PCALIGN $32
    32  ADD $8, R3
    33  PCALIGN $8
    34  ADD $4, R8
    35  RET
    36  `
    37  
    38  // TestPCalign generates two asm files containing the
    39  // PCALIGN directive, to verify correct values are and
    40  // accepted, and incorrect values are flagged in error.
    41  func TestPCalign(t *testing.T) {
    42  	var pattern8 = `0x...8\s.*ADD\s..,\sR8`
    43  	var pattern16 = `0x...[80]\s.*MOVD\s..,\sR16`
    44  	var pattern32 = `0x...0\s.*ADD\s..,\sR3`
    45  
    46  	testenv.MustHaveGoBuild(t)
    47  
    48  	dir, err := ioutil.TempDir("", "testpcalign")
    49  	if err != nil {
    50  		t.Fatalf("could not create directory: %v", err)
    51  	}
    52  	defer os.RemoveAll(dir)
    53  
    54  	// generate a test with valid uses of PCALIGN
    55  
    56  	tmpfile := filepath.Join(dir, "x.s")
    57  	err = ioutil.WriteFile(tmpfile, []byte(validPCAlignSrc), 0644)
    58  	if err != nil {
    59  		t.Fatalf("can't write output: %v\n", err)
    60  	}
    61  
    62  	// build generated file without errors and assemble it
    63  	cmd := exec.Command(testenv.GoToolPath(t), "tool", "asm", "-o", filepath.Join(dir, "x.o"), "-S", tmpfile)
    64  	cmd.Env = append(os.Environ(), "GOARCH=ppc64le", "GOOS=linux")
    65  	out, err := cmd.CombinedOutput()
    66  	if err != nil {
    67  		t.Errorf("Build failed: %v, output: %s", err, out)
    68  	}
    69  
    70  	matched, err := regexp.MatchString(pattern8, string(out))
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	if !matched {
    75  		t.Errorf("The 8 byte alignment is not correct: %t, output:%s\n", matched, out)
    76  	}
    77  
    78  	matched, err = regexp.MatchString(pattern16, string(out))
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	if !matched {
    83  		t.Errorf("The 16 byte alignment is not correct: %t, output:%s\n", matched, out)
    84  	}
    85  
    86  	matched, err = regexp.MatchString(pattern32, string(out))
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	if !matched {
    91  		t.Errorf("The 32 byte alignment is not correct: %t, output:%s\n", matched, out)
    92  	}
    93  
    94  	// generate a test with invalid use of PCALIGN
    95  
    96  	tmpfile = filepath.Join(dir, "xi.s")
    97  	err = ioutil.WriteFile(tmpfile, []byte(invalidPCAlignSrc), 0644)
    98  	if err != nil {
    99  		t.Fatalf("can't write output: %v\n", err)
   100  	}
   101  
   102  	// build test with errors and check for messages
   103  	cmd = exec.Command(testenv.GoToolPath(t), "tool", "asm", "-o", filepath.Join(dir, "xi.o"), "-S", tmpfile)
   104  	cmd.Env = append(os.Environ(), "GOARCH=ppc64le", "GOOS=linux")
   105  	out, err = cmd.CombinedOutput()
   106  	if !strings.Contains(string(out), "Unexpected alignment") {
   107  		t.Errorf("Invalid alignment not detected for PCALIGN\n")
   108  	}
   109  }
   110  

View as plain text