Improved x64 encoder

This commit is contained in:
Eduard Urbach 2024-06-24 21:54:03 +02:00
parent c1d3e0d11b
commit 81fcb50e77
Signed by: eduard
GPG key ID: 49226B848C78F6C8
5 changed files with 46 additions and 23 deletions

View file

@ -1,24 +1,27 @@
package x64
import (
"math"
// numRegReg encodes an instruction with up to two registers and a number parameter.
func numRegReg(code []byte, opCode8 byte, opCode32 byte, reg byte, rm byte, number int) []byte {
w := byte(1) // Indicates a 64-bit register.
r := byte(0) // Extension to the "reg" field in ModRM.
x := byte(0) // Extension to the SIB index field.
b := byte(0) // Extension to the "rm" field in ModRM or the SIB base (r8 up to r15 use this).
mod := byte(0b11) // Direct addressing mode, no register offsets.
"git.akyoto.dev/cli/q/src/build/cpu"
)
// numberToRegister encodes an instruction with a register and a number parameter.
func numberToRegister(opCode8 byte, opCode32 byte, reg byte, code []byte, destination cpu.Register, number int) []byte {
b := byte(0)
if destination >= 8 {
b = 1
destination -= 8
if reg > 0b111 {
r = 1
reg &= 0b111
}
rex := REX(1, 0, 0, b)
modRM := ModRM(0b11, reg, byte(destination))
if rm > 0b111 {
b = 1
rm &= 0b111
}
if number >= math.MinInt8 && number <= math.MaxInt8 {
rex := REX(w, r, x, b)
modRM := ModRM(mod, reg, rm)
if sizeOf(number) == 1 {
return append(
code,
rex,