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

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

View as plain text