Added more tests
This commit is contained in:
parent
77cfe9ff31
commit
668971808b
31
src/build/arch/x64/Jump_test.go
Normal file
31
src/build/arch/x64/Jump_test.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package x64_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.akyoto.dev/cli/q/src/build/arch/x64"
|
||||||
|
"git.akyoto.dev/go/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestJump8(t *testing.T) {
|
||||||
|
usagePatterns := []struct {
|
||||||
|
Offset int8
|
||||||
|
Code []byte
|
||||||
|
}{
|
||||||
|
{0, []byte{0xEB, 0x00}},
|
||||||
|
{1, []byte{0xEB, 0x01}},
|
||||||
|
{2, []byte{0xEB, 0x02}},
|
||||||
|
{3, []byte{0xEB, 0x03}},
|
||||||
|
{127, []byte{0xEB, 0x7F}},
|
||||||
|
{-1, []byte{0xEB, 0xFF}},
|
||||||
|
{-2, []byte{0xEB, 0xFE}},
|
||||||
|
{-3, []byte{0xEB, 0xFD}},
|
||||||
|
{-128, []byte{0xEB, 0x80}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, pattern := range usagePatterns {
|
||||||
|
t.Logf("jmp %x", pattern.Offset)
|
||||||
|
code := x64.Jump8(nil, pattern.Offset)
|
||||||
|
assert.DeepEqual(t, code, pattern.Code)
|
||||||
|
}
|
||||||
|
}
|
@ -2,8 +2,8 @@ package x64
|
|||||||
|
|
||||||
import "math"
|
import "math"
|
||||||
|
|
||||||
// sizeOf tells you how many bytes are needed to encode this number.
|
// SizeOf tells you how many bytes are needed to encode this number.
|
||||||
func sizeOf(number int) int {
|
func SizeOf(number int) int {
|
||||||
switch {
|
switch {
|
||||||
case number >= math.MinInt8 && number <= math.MaxInt8:
|
case number >= math.MinInt8 && number <= math.MaxInt8:
|
||||||
return 1
|
return 1
|
21
src/build/arch/x64/SizeOf_test.go
Normal file
21
src/build/arch/x64/SizeOf_test.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package x64_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.akyoto.dev/cli/q/src/build/arch/x64"
|
||||||
|
"git.akyoto.dev/go/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSizeOf(t *testing.T) {
|
||||||
|
assert.Equal(t, x64.SizeOf(0), 1)
|
||||||
|
assert.Equal(t, x64.SizeOf(math.MinInt8), 1)
|
||||||
|
assert.Equal(t, x64.SizeOf(math.MaxInt8), 1)
|
||||||
|
assert.Equal(t, x64.SizeOf(math.MinInt16), 2)
|
||||||
|
assert.Equal(t, x64.SizeOf(math.MaxInt16), 2)
|
||||||
|
assert.Equal(t, x64.SizeOf(math.MinInt32), 4)
|
||||||
|
assert.Equal(t, x64.SizeOf(math.MaxInt32), 4)
|
||||||
|
assert.Equal(t, x64.SizeOf(math.MinInt64), 8)
|
||||||
|
assert.Equal(t, x64.SizeOf(math.MaxInt64), 8)
|
||||||
|
}
|
@ -4,7 +4,7 @@ import "encoding/binary"
|
|||||||
|
|
||||||
// regRegNum encodes an instruction with up to two registers and a number parameter.
|
// regRegNum encodes an instruction with up to two registers and a number parameter.
|
||||||
func regRegNum(code []byte, reg byte, rm byte, number int, opCode8 byte, opCode32 byte) []byte {
|
func regRegNum(code []byte, reg byte, rm byte, number int, opCode8 byte, opCode32 byte) []byte {
|
||||||
if sizeOf(number) == 1 {
|
if SizeOf(number) == 1 {
|
||||||
code = regReg(code, reg, rm, opCode8)
|
code = regReg(code, reg, rm, opCode8)
|
||||||
return append(code, byte(number))
|
return append(code, byte(number))
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestX64(t *testing.T) {
|
func TestX64(t *testing.T) {
|
||||||
assert.DeepEqual(t, x64.Call([]byte{}, 1), []byte{0xe8, 0x01, 0x00, 0x00, 0x00})
|
assert.DeepEqual(t, x64.Call(nil, 1), []byte{0xe8, 0x01, 0x00, 0x00, 0x00})
|
||||||
assert.DeepEqual(t, x64.MoveRegisterNumber32([]byte{}, 0, 1), []byte{0xb8, 0x01, 0x00, 0x00, 0x00})
|
assert.DeepEqual(t, x64.MoveRegisterNumber32(nil, 0, 1), []byte{0xb8, 0x01, 0x00, 0x00, 0x00})
|
||||||
assert.DeepEqual(t, x64.MoveRegisterNumber32([]byte{}, 1, 1), []byte{0xb9, 0x01, 0x00, 0x00, 0x00})
|
assert.DeepEqual(t, x64.MoveRegisterNumber32(nil, 1, 1), []byte{0xb9, 0x01, 0x00, 0x00, 0x00})
|
||||||
assert.DeepEqual(t, x64.Return([]byte{}), []byte{0xc3})
|
assert.DeepEqual(t, x64.Return(nil), []byte{0xc3})
|
||||||
assert.DeepEqual(t, x64.Syscall([]byte{}), []byte{0x0f, 0x05})
|
assert.DeepEqual(t, x64.Syscall(nil), []byte{0x0f, 0x05})
|
||||||
|
assert.DeepEqual(t, x64.ExtendRAXToRDX(nil), []byte{0x48, 0x99})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user