Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/asm/internal/asm/pseudo_test.go

Documentation: cmd/asm/internal/asm

     1  // Copyright 2015 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 asm
     6  
     7  import (
     8  	"bytes"
     9  	"strings"
    10  	"testing"
    11  
    12  	"cmd/asm/internal/lex"
    13  )
    14  
    15  func tokenize(s string) [][]lex.Token {
    16  	res := [][]lex.Token{}
    17  	if len(s) == 0 {
    18  		return res
    19  	}
    20  	for _, o := range strings.Split(s, ",") {
    21  		res = append(res, lex.Tokenize(o))
    22  	}
    23  	return res
    24  }
    25  
    26  func TestErroneous(t *testing.T) {
    27  
    28  	type errtest struct {
    29  		pseudo   string
    30  		operands string
    31  		expected string
    32  	}
    33  
    34  	nonRuntimeTests := []errtest{
    35  		{"TEXT", "", "expect two or three operands for TEXT"},
    36  		{"TEXT", "%", "expect two or three operands for TEXT"},
    37  		{"TEXT", "1, 1", "TEXT symbol \"<erroneous symbol>\" must be a symbol(SB)"},
    38  		{"TEXT", "$\"foo\", 0, $1", "TEXT symbol \"<erroneous symbol>\" must be a symbol(SB)"},
    39  		{"TEXT", "$0É:0, 0, $1", "expected end of operand, found É"}, // Issue #12467.
    40  		{"TEXT", "$:0:(SB, 0, $1", "expected '(', found 0"},          // Issue 12468.
    41  		{"TEXT", "@B(SB),0,$0", "expected '(', found B"},             // Issue 23580.
    42  		{"TEXT", "foo<ABIInternal>(SB),0", "ABI selector only permitted when compiling runtime, reference was to \"foo\""},
    43  		{"FUNCDATA", "", "expect two operands for FUNCDATA"},
    44  		{"FUNCDATA", "(SB ", "expect two operands for FUNCDATA"},
    45  		{"DATA", "", "expect two operands for DATA"},
    46  		{"DATA", "0", "expect two operands for DATA"},
    47  		{"DATA", "(0), 1", "expect /size for DATA argument"},
    48  		{"DATA", "@B(SB)/4,0", "expected '(', found B"}, // Issue 23580.
    49  		{"DATA", "·A(SB)/4,0", "DATA value must be an immediate constant or address"},
    50  		{"DATA", "·B(SB)/4,$0", ""},
    51  		{"DATA", "·C(SB)/5,$0", "bad int size for DATA argument: 5"},
    52  		{"DATA", "·D(SB)/5,$0.0", "bad float size for DATA argument: 5"},
    53  		{"DATA", "·E(SB)/4,$·A(SB)", "bad addr size for DATA argument: 4"},
    54  		{"DATA", "·F(SB)/8,$·A(SB)", ""},
    55  		{"DATA", "·G(SB)/5,$\"abcde\"", ""},
    56  		{"GLOBL", "", "expect two or three operands for GLOBL"},
    57  		{"GLOBL", "0,1", "GLOBL symbol \"<erroneous symbol>\" must be a symbol(SB)"},
    58  		{"GLOBL", "@B(SB), 0", "expected '(', found B"}, // Issue 23580.
    59  		{"PCDATA", "", "expect two operands for PCDATA"},
    60  		{"PCDATA", "1", "expect two operands for PCDATA"},
    61  	}
    62  
    63  	runtimeTests := []errtest{
    64  		{"TEXT", "foo<ABIInternal>(SB),0", "TEXT \"foo\": ABIInternal requires NOSPLIT"},
    65  	}
    66  
    67  	testcats := []struct {
    68  		compilingRuntime bool
    69  		tests            []errtest
    70  	}{
    71  		{
    72  			compilingRuntime: false,
    73  			tests:            nonRuntimeTests,
    74  		},
    75  		{
    76  			compilingRuntime: true,
    77  			tests:            runtimeTests,
    78  		},
    79  	}
    80  
    81  	// Note these errors should be independent of the architecture.
    82  	// Just run the test with amd64.
    83  	parser := newParser("amd64")
    84  	var buf bytes.Buffer
    85  	parser.errorWriter = &buf
    86  
    87  	for _, cat := range testcats {
    88  		for _, test := range cat.tests {
    89  			parser.compilingRuntime = cat.compilingRuntime
    90  			parser.errorCount = 0
    91  			parser.lineNum++
    92  			if !parser.pseudo(test.pseudo, tokenize(test.operands)) {
    93  				t.Fatalf("Wrong pseudo-instruction: %s", test.pseudo)
    94  			}
    95  			errorLine := buf.String()
    96  			if test.expected != errorLine {
    97  				t.Errorf("Unexpected error %q; expected %q", errorLine, test.expected)
    98  			}
    99  			buf.Reset()
   100  		}
   101  	}
   102  
   103  }
   104  

View as plain text