Improved x64 encoder
This commit is contained in:
52
src/build/arch/x64/Store.go
Normal file
52
src/build/arch/x64/Store.go
Normal file
@ -0,0 +1,52 @@
|
||||
package x64
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/build/cpu"
|
||||
)
|
||||
|
||||
// StoreNumber stores a number into the memory address included in the given register.
|
||||
func StoreNumber(code []byte, register cpu.Register, offset byte, byteCount byte, number int) []byte {
|
||||
if byteCount == 2 {
|
||||
code = append(code, 0x66)
|
||||
}
|
||||
|
||||
opCode := byte(0xC7)
|
||||
|
||||
if byteCount == 1 {
|
||||
opCode = 0xC6
|
||||
}
|
||||
|
||||
mod := AddressMemory
|
||||
|
||||
if offset != 0 || register == RBP || register == R13 {
|
||||
mod = AddressMemoryOffset8
|
||||
}
|
||||
|
||||
is64Bit := byte(0)
|
||||
|
||||
if byteCount == 8 {
|
||||
is64Bit = 1
|
||||
}
|
||||
|
||||
code = encode(code, is64Bit, mod, 0b000, byte(register), opCode)
|
||||
|
||||
if register == RSP || register == R12 {
|
||||
code = append(code, SIB(0b00, 0b100, 0b100))
|
||||
}
|
||||
|
||||
if mod == AddressMemoryOffset8 {
|
||||
code = append(code, offset)
|
||||
}
|
||||
|
||||
switch byteCount {
|
||||
case 8, 4:
|
||||
return binary.LittleEndian.AppendUint32(code, uint32(number))
|
||||
|
||||
case 2:
|
||||
return binary.LittleEndian.AppendUint16(code, uint16(number))
|
||||
}
|
||||
|
||||
return append(code, byte(number))
|
||||
}
|
Reference in New Issue
Block a user