Implemented move with sign extension

This commit is contained in:
Eduard Urbach 2024-07-28 21:09:42 +02:00
parent 32d7455c38
commit f1d4e65c1b
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
5 changed files with 45 additions and 5 deletions

View File

@ -9,12 +9,17 @@ import (
// MoveRegisterNumber moves an integer into the given register. // MoveRegisterNumber moves an integer into the given register.
func MoveRegisterNumber(code []byte, destination cpu.Register, number int) []byte { func MoveRegisterNumber(code []byte, destination cpu.Register, number int) []byte {
w := byte(0) w := byte(0)
b := byte(0)
if SizeOf(int64(number)) == 8 { if SizeOf(int64(number)) == 8 {
w = 1 w = 1
} }
if w == 0 && number < 0 {
return MoveRegisterNumber32(code, destination, number)
}
b := byte(0)
if destination > 0b111 { if destination > 0b111 {
b = 1 b = 1
destination &= 0b111 destination &= 0b111
@ -33,6 +38,21 @@ func MoveRegisterNumber(code []byte, destination cpu.Register, number int) []byt
} }
} }
// MoveRegisterNumber32 moves an integer into the given register and sign-extends the register.
func MoveRegisterNumber32(code []byte, destination cpu.Register, number int) []byte {
b := byte(0)
if destination > 0b111 {
b = 1
destination &= 0b111
}
code = append(code, REX(1, 0, 0, b))
code = append(code, 0xC7)
code = append(code, ModRM(AddressDirect, 0, byte(destination)))
return binary.LittleEndian.AppendUint32(code, uint32(number))
}
// MoveRegisterRegister moves a register value into another register. // MoveRegisterRegister moves a register value into another register.
func MoveRegisterRegister(code []byte, destination cpu.Register, source cpu.Register) []byte { func MoveRegisterRegister(code []byte, destination cpu.Register, source cpu.Register) []byte {
return encode(code, AddressDirect, source, destination, 8, 0x89) return encode(code, AddressDirect, source, destination, 8, 0x89)

View File

@ -14,6 +14,7 @@ func TestMoveRegisterNumber(t *testing.T) {
Number int Number int
Code []byte Code []byte
}{ }{
// 32 bits
{x64.RAX, 0x7FFFFFFF, []byte{0xB8, 0xFF, 0xFF, 0xFF, 0x7F}}, {x64.RAX, 0x7FFFFFFF, []byte{0xB8, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RCX, 0x7FFFFFFF, []byte{0xB9, 0xFF, 0xFF, 0xFF, 0x7F}}, {x64.RCX, 0x7FFFFFFF, []byte{0xB9, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RDX, 0x7FFFFFFF, []byte{0xBA, 0xFF, 0xFF, 0xFF, 0x7F}}, {x64.RDX, 0x7FFFFFFF, []byte{0xBA, 0xFF, 0xFF, 0xFF, 0x7F}},
@ -31,6 +32,7 @@ func TestMoveRegisterNumber(t *testing.T) {
{x64.R14, 0x7FFFFFFF, []byte{0x41, 0xBE, 0xFF, 0xFF, 0xFF, 0x7F}}, {x64.R14, 0x7FFFFFFF, []byte{0x41, 0xBE, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.R15, 0x7FFFFFFF, []byte{0x41, 0xBF, 0xFF, 0xFF, 0xFF, 0x7F}}, {x64.R15, 0x7FFFFFFF, []byte{0x41, 0xBF, 0xFF, 0xFF, 0xFF, 0x7F}},
// 64 bits
{x64.RAX, 0x7FFFFFFFFFFFFFFF, []byte{0x48, 0xB8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}}, {x64.RAX, 0x7FFFFFFFFFFFFFFF, []byte{0x48, 0xB8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RCX, 0x7FFFFFFFFFFFFFFF, []byte{0x48, 0xB9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}}, {x64.RCX, 0x7FFFFFFFFFFFFFFF, []byte{0x48, 0xB9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.RDX, 0x7FFFFFFFFFFFFFFF, []byte{0x48, 0xBA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}}, {x64.RDX, 0x7FFFFFFFFFFFFFFF, []byte{0x48, 0xBA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}},
@ -47,6 +49,24 @@ func TestMoveRegisterNumber(t *testing.T) {
{x64.R13, 0x7FFFFFFFFFFFFFFF, []byte{0x49, 0xBD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}}, {x64.R13, 0x7FFFFFFFFFFFFFFF, []byte{0x49, 0xBD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.R14, 0x7FFFFFFFFFFFFFFF, []byte{0x49, 0xBE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}}, {x64.R14, 0x7FFFFFFFFFFFFFFF, []byte{0x49, 0xBE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}},
{x64.R15, 0x7FFFFFFFFFFFFFFF, []byte{0x49, 0xBF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}}, {x64.R15, 0x7FFFFFFFFFFFFFFF, []byte{0x49, 0xBF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}},
// Negative numbers
{x64.RAX, -1, []byte{0x48, 0xC7, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.RCX, -1, []byte{0x48, 0xC7, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.RDX, -1, []byte{0x48, 0xC7, 0xC2, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.RBX, -1, []byte{0x48, 0xC7, 0xC3, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.RSP, -1, []byte{0x48, 0xC7, 0xC4, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.RBP, -1, []byte{0x48, 0xC7, 0xC5, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.RSI, -1, []byte{0x48, 0xC7, 0xC6, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.RDI, -1, []byte{0x48, 0xC7, 0xC7, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.R8, -1, []byte{0x49, 0xC7, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.R9, -1, []byte{0x49, 0xC7, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.R10, -1, []byte{0x49, 0xC7, 0xC2, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.R11, -1, []byte{0x49, 0xC7, 0xC3, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.R12, -1, []byte{0x49, 0xC7, 0xC4, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.R13, -1, []byte{0x49, 0xC7, 0xC5, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.R14, -1, []byte{0x49, 0xC7, 0xC6, 0xFF, 0xFF, 0xFF, 0xFF}},
{x64.R15, -1, []byte{0x49, 0xC7, 0xC7, 0xFF, 0xFF, 0xFF, 0xFF}},
} }
for _, pattern := range usagePatterns { for _, pattern := range usagePatterns {

View File

@ -1,5 +1,5 @@
main() { main() {
syscall(60, f(-32)) assert f(-32) == 32
} }
f(x) { f(x) {

View File

@ -1,5 +1,5 @@
main() { main() {
a := -32 a := -32
b := 64 b := 64
syscall(60, a + b) assert a + b == 32
} }

View File

@ -31,8 +31,8 @@ var programs = []struct {
{"modulo", "", "", 0}, {"modulo", "", "", 0},
{"modulo-assign", "", "", 0}, {"modulo-assign", "", "", 0},
{"division-split", "", "", 0}, {"division-split", "", "", 0},
{"negative", "", "", 32}, {"negative", "", "", 0},
{"negation", "", "", 32}, {"negation", "", "", 0},
{"square-sum", "", "", 0}, {"square-sum", "", "", 0},
{"chained-calls", "", "", 0}, {"chained-calls", "", "", 0},
{"nested-calls", "", "", 0}, {"nested-calls", "", "", 0},