gitee.com/quant1x/num@v0.3.2/asm/asm2plan9/asm2plan9s_amd64_test.go (about)

     1  /*
     2   * Minio Cloud Storage, (C) 2016-2017 Minio, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"io/ioutil"
    22  	"os"
    23  	"testing"
    24  )
    25  
    26  func TestInstruction(t *testing.T) {
    27  
    28  	ins := "                                 // VPADDQ  XMM0,XMM1,XMM8"
    29  	out := "    LONG $0xd471c1c4; BYTE $0xc0 // VPADDQ  XMM0,XMM1,XMM8"
    30  
    31  	result, _ := assemble([]string{ins}, false)
    32  
    33  	if result[0] != out {
    34  		t.Errorf("expected %s\ngot                     %s", out, result[0])
    35  	}
    36  }
    37  
    38  func TestInstructionPresent(t *testing.T) {
    39  
    40  	ins := "    LONG $0xd471c1c4; BYTE $0xc0 // VPADDQ  XMM0,XMM1,XMM8"
    41  	out := "    LONG $0xd471c1c4; BYTE $0xc0 // VPADDQ  XMM0,XMM1,XMM8"
    42  
    43  	result, _ := assemble([]string{ins}, false)
    44  
    45  	if result[0] != out {
    46  		t.Errorf("expected %s\ngot                     %s", out, result[0])
    47  	}
    48  }
    49  
    50  func TestInstructionWrongBytes(t *testing.T) {
    51  
    52  	ins := "    LONG $0x003377bb; BYTE $0xff // VPADDQ  XMM0,XMM1,XMM8"
    53  	out := "    LONG $0xd471c1c4; BYTE $0xc0 // VPADDQ  XMM0,XMM1,XMM8"
    54  
    55  	result, _ := assemble([]string{ins}, false)
    56  
    57  	if result[0] != out {
    58  		t.Errorf("expected %s\ngot                     %s", out, result[0])
    59  	}
    60  }
    61  
    62  func TestInstructionInDefine(t *testing.T) {
    63  
    64  	ins := `    LONG $0x00000000; BYTE $0xdd                               \ // VPADDQ  XMM0,XMM1,XMM8`
    65  	out := `    LONG $0xd471c1c4; BYTE $0xc0                               \ // VPADDQ  XMM0,XMM1,XMM8`
    66  
    67  	result, _ := assemble([]string{ins}, false)
    68  
    69  	if result[0] != out {
    70  		t.Errorf("expected %s\ngot                     %s", out, result[0])
    71  	}
    72  }
    73  
    74  func TestCompactMultipleInstructions(t *testing.T) {
    75  
    76  	ins1 := "                                 // VPADDQ  XMM0,XMM1,XMM8"
    77  	ins2 := "                                 // VPADDQ  XMM1,XMM2,XMM3"
    78  	ins3 := "                                 // VPADDQ  XMM4,XMM5,XMM6"
    79  	ins4 := "                                 // VPADDQ  XMM4,XMM5,XMM6"
    80  	ins5 := "                                 // VPADDQ  XMM4,XMM5,XMM6"
    81  	ins6 := "                                 // VPADDQ  XMM4,XMM5,XMM6"
    82  	ins7 := "                                 // VPADDQ  XMM4,XMM5,XMM6"
    83  	ins8 := "    MOVQ AX, BX"
    84  	ins9 := "                                 // VPADDQ  XMM0,XMM1,XMM8"
    85  	ins10 := "                                 // VPADDQ  XMM1,XMM2,XMM3"
    86  	ins11 := "    MOVQ BX, CX"
    87  	ins12 := "                                 // VPADDQ  XMM4,XMM5,XMM6"
    88  	ins13 := "                                 // VPADDQ  XMM5,XMM6,XMM0"
    89  	out0 := "    QUAD $0xd4e9c5c0d471c1c4; QUAD $0xd4d1c5e6d4d1c5cb; QUAD $0xd4d1c5e6d4d1c5e6; QUAD $0x71c1c4e6d4d1c5e6; WORD $0xc0d4"
    90  	out1 := "    MOVQ AX, BX"
    91  	out2 := "    QUAD $0xe6d4d1c5cbd4e9c5"
    92  	out3 := "    MOVQ BX, CX"
    93  	out4 := "    LONG $0xe8d4c9c5"
    94  	out := make([]string, 5)
    95  	out[0], out[1], out[2], out[3], out[4] = out0, out1, out2, out3, out4
    96  
    97  	result, _ := assemble([]string{ins1, ins2, ins3, ins4, ins5, ins6, ins7, ins8, ins9, ins10, ins11, ins12, ins13}, true)
    98  	if len(result) != len(out) {
    99  		t.Errorf("expected length %d\ngot             length %d", len(out), len(result))
   100  	}
   101  	for i := range result {
   102  		if result[i] != out[i] {
   103  			t.Errorf("expected %s\ngot                     %s", out[i], result[i])
   104  		}
   105  	}
   106  }
   107  
   108  func TestLongInstruction(t *testing.T) {
   109  
   110  	ins := "                                   // VPALIGNR XMM8, XMM12, XMM12, 0x8"
   111  	out := "    LONG $0x0f1943c4; WORD $0x08c4 // VPALIGNR XMM8, XMM12, XMM12, 0x8"
   112  
   113  	result, _ := assemble([]string{ins}, false)
   114  
   115  	if result[0] != out {
   116  		t.Errorf("expected %s\ngot                     %s", out, result[0])
   117  	}
   118  }
   119  
   120  func TestToPlan9sGasSingleLineEVEX(t *testing.T) {
   121  
   122  	ins := `1                    .intel_syntax noprefix
   123     2 0000 62D1F548       VPADDQ  ZMM0,ZMM1,ZMM8
   124     2      D4C0
   125     3              `
   126  
   127  	out := make([][]byte, 1, 1)
   128  	out[0] = []byte{98, 209, 245, 72, 212, 192}
   129  
   130  	tmpfile, err := ioutil.TempFile("", "test")
   131  	if err != nil {
   132  		return
   133  	}
   134  
   135  	if _, err := tmpfile.Write([]byte(ins)); err != nil {
   136  		return
   137  	}
   138  	if err := tmpfile.Close(); err != nil {
   139  		return
   140  	}
   141  	defer os.Remove(tmpfile.Name()) // clean up
   142  
   143  	result, _ := toPlan9sGas(tmpfile.Name())
   144  	if len(result) != len(out) || !bytes.Equal(result[0], out[0]) {
   145  		t.Errorf("expected %v\ngot                     %v", out, result)
   146  	}
   147  }
   148  
   149  func TestToPlan9sGasMultiLinesVEX(t *testing.T) {
   150  
   151  	ins := `   1                    .intel_syntax noprefix
   152     2 0000 C4C171D4       VPADDQ  XMM0,XMM1,XMM8
   153     2      C0
   154     3 0005 C4C169D4       VPADDQ  XMM1,XMM2,XMM9
   155     3      C9
   156     4 000a C4C161D4       VPADDQ  XMM2,XMM3,XMM10
   157     4      D2
   158     5          `
   159  
   160  	out := make([][]byte, 3, 3)
   161  	out[0] = []byte{196, 193, 113, 212, 192}
   162  	out[1] = []byte{196, 193, 105, 212, 201}
   163  	out[2] = []byte{196, 193, 97, 212, 210}
   164  
   165  	tmpfile, err := ioutil.TempFile("", "test")
   166  	if err != nil {
   167  		return
   168  	}
   169  
   170  	if _, err := tmpfile.Write([]byte(ins)); err != nil {
   171  		return
   172  	}
   173  	if err := tmpfile.Close(); err != nil {
   174  		return
   175  	}
   176  	defer os.Remove(tmpfile.Name()) // clean up
   177  
   178  	result, _ := toPlan9sGas(tmpfile.Name())
   179  	if len(result) != len(out) || !bytes.Equal(result[0], out[0]) ||
   180  		!bytes.Equal(result[1], out[1]) || !bytes.Equal(result[2], out[2]) {
   181  		t.Errorf("expected %v\ngot                     %v", out, result)
   182  	}
   183  }