github.com/cloudwego/iasm@v0.2.0/x86_64/assembler_test.go (about)

     1  //
     2  // Copyright 2024 CloudWeGo Authors
     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 x86_64
    18  
    19  import (
    20      `strings`
    21      `testing`
    22  
    23      `github.com/davecgh/go-spew/spew`
    24      `github.com/stretchr/testify/require`
    25  )
    26  
    27  func TestAssembler_Tokenizer(t *testing.T) {
    28      k := new(_Tokenizer)
    29      k.src = []rune(`movqorwhat $123, 123(%rax,%rbx), %rcx`)
    30      for {
    31          tk := k.next()
    32          if tk.tag == _T_end {
    33              break
    34          }
    35          spew.Dump(tk)
    36      }
    37  }
    38  
    39  func TestAssembler_Parser(t *testing.T) {
    40      p := new(Parser)
    41      v, e := p.Feed("movq " + strings.Join([]string {
    42          `$123`,
    43          `$-123`,
    44          `%rcx`,
    45          `(%rax)`,
    46          `123(%rax)`,
    47          `-123(%rax)`,
    48          `(%rax,%rbx,4)`,
    49          `1234(%rax,%rbx,4)`,
    50          `(,%rax,8)`,
    51          `1234(,%rax,8)`,
    52          `$(123 + 456)`,
    53          `(123 + 456)(%rax)`,
    54          `$'asd'`,
    55          `$'asdf'`,
    56          `$'asdfghj'`,
    57          `$'asdfghjk'`,
    58      }, ", "))
    59      require.NoError(t, e)
    60      spew.Dump(v)
    61  }
    62  
    63  func TestAssembler_RIPRelative(t *testing.T) {
    64      p := new(Assembler)
    65      e := p.Assemble(`leaq 0x1b(%rip), %rsi`)
    66      require.NoError(t, e)
    67      spew.Dump(p.Code())
    68      require.Equal(t, []byte { 0x48, 0x8d, 0x35, 0x1b, 0x00, 0x00, 0x00 }, p.Code())
    69  }
    70  
    71  func TestAssembler_AbsoluteAddressing(t *testing.T) {
    72      p := new(Assembler)
    73      e := p.Assemble(`
    74  movq 0x1234, %rbx
    75  movq %rcx, 0x1234
    76  `)
    77      require.NoError(t, e)
    78      spew.Dump(p.Code())
    79      require.Equal(t, []byte {
    80          0x48, 0x8b, 0x1c, 0x25, 0x34, 0x12, 0x00, 0x00,
    81          0x48, 0x89, 0x0c, 0x25, 0x34, 0x12, 0x00, 0x00,
    82      }, p.Code())
    83  }
    84  
    85  func TestAssembler_LockPrefix(t *testing.T) {
    86      p := new(Assembler)
    87      e := p.Assemble(`lock cmpxchgq %r9, (%rbx)`)
    88      require.NoError(t, e)
    89      spew.Dump(p.Code())
    90      require.Equal(t, []byte { 0xf0, 0x4c, 0x0f, 0xb1, 0x0b }, p.Code())
    91  }
    92  
    93  func TestAssembler_SegmentOverride(t *testing.T) {
    94      p := new(Assembler)
    95      e := p.Assemble(`
    96  movq gs:0x30, %rcx
    97  movq gs:10(%rax), %rcx
    98  movq fs:(%r9), %rcx
    99  `)
   100      require.NoError(t, e)
   101      spew.Dump(p.Code())
   102      require.Equal(t, []byte {
   103          0x65, 0x48, 0x8b, 0x0c, 0x25, 0x30, 0x00, 0x00, 0x00,
   104          0x65, 0x48, 0x8b, 0x48, 0x0a,
   105          0x64, 0x49, 0x8b, 0x09,
   106      }, p.Code())
   107  }