Implemented dynamic array indices

This commit is contained in:
Eduard Urbach 2025-01-24 22:47:28 +01:00
parent 791573bbfc
commit 169810e3a2
Signed by: eduard
GPG key ID: 49226B848C78F6C8
10 changed files with 393 additions and 21 deletions

View file

@ -0,0 +1,130 @@
package x64
import (
"encoding/binary"
"git.akyoto.dev/cli/q/src/cpu"
)
// StoreDynamicOffsetNumber stores a number into the memory address at `destination` with a register offset.
func StoreDynamicOffsetNumber(code []byte, destination cpu.Register, offset cpu.Register, length byte, number int) []byte {
var (
w = byte(0)
r = byte(0)
x = byte(0)
b = byte(0)
opCode = byte(0xC7)
mod = AddressMemory
)
if length == 1 {
opCode = 0xC6
}
if offset == RSP {
tmp := offset
offset = destination
destination = tmp
}
if length == 8 {
w = 1
}
if offset > 0b111 {
x = 1
offset &= 0b111
}
if destination > 0b111 {
b = 1
destination &= 0b111
}
if destination == RBP || destination == R13 {
mod = AddressMemoryOffset8
}
if length == 2 {
code = append(code, 0x66)
}
code = append(code, REX(w, r, x, b))
code = append(code, opCode)
code = append(code, ModRM(mod, 0b000, 0b100))
code = append(code, SIB(Scale1, byte(offset), byte(destination)))
if mod == AddressMemoryOffset8 {
code = append(code, 0x00)
}
switch length {
case 8, 4:
return binary.LittleEndian.AppendUint32(code, uint32(number))
case 2:
return binary.LittleEndian.AppendUint16(code, uint16(number))
}
return append(code, byte(number))
}
// StoreDynamicOffsetRegister stores the contents of the `source` register into the memory address at `destination` with a register offset.
func StoreDynamicOffsetRegister(code []byte, destination cpu.Register, offset cpu.Register, length byte, source cpu.Register) []byte {
var (
w = byte(0)
r = byte(0)
x = byte(0)
b = byte(0)
opCode = byte(0x89)
mod = AddressMemory
)
if length == 1 {
opCode = 0x88
}
if offset == RSP {
tmp := offset
offset = destination
destination = tmp
}
if length == 8 {
w = 1
}
if source > 0b111 {
r = 1
source &= 0b111
}
if offset > 0b111 {
x = 1
offset &= 0b111
}
if destination > 0b111 {
b = 1
destination &= 0b111
}
if destination == RBP || destination == R13 {
mod = AddressMemoryOffset8
}
if length == 2 {
code = append(code, 0x66)
}
code = append(code, REX(w, r, x, b))
code = append(code, opCode)
code = append(code, ModRM(mod, byte(source), 0b100))
code = append(code, SIB(Scale1, byte(offset), byte(destination)))
if mod == AddressMemoryOffset8 {
code = append(code, 0x00)
}
return code
}