q/src/asmc/move.go
2025-03-13 16:57:13 +01:00

61 lines
1.5 KiB
Go

package asmc
import (
"git.urbach.dev/cli/q/src/asm"
"git.urbach.dev/cli/q/src/x86"
)
func (c *compiler) move(x asm.Instruction) {
switch x.Type {
case asm.TypeRegisterNumber:
operands := c.assembler.Param.RegisterNumber[x.Index]
c.code = x86.MoveRegisterNumber(c.code, operands.Register, operands.Number)
case asm.TypeRegisterRegister:
operands := c.assembler.Param.RegisterRegister[x.Index]
c.code = x86.MoveRegisterRegister(c.code, operands.Destination, operands.Source)
case asm.TypeRegisterLabel:
operands := c.assembler.Param.RegisterLabel[x.Index]
start := Address(len(c.code))
c.code = x86.LoadAddress(c.code, operands.Register, 0x00_00_00_00)
end := Address(len(c.code))
position := end - 4
opSize := position - start
if operands.Label.Type == asm.DataLabel {
c.dataPointers = append(c.dataPointers, &pointer{
Position: position,
OpSize: uint8(opSize),
Size: uint8(4),
Resolve: func() Address {
destination, exists := c.dataLabels[operands.Label.Name]
if !exists {
panic("unknown label")
}
destination += c.dataStart - c.codeStart
distance := destination - end
return distance + 8
},
})
} else {
c.codePointers = append(c.codePointers, &pointer{
Position: position,
OpSize: uint8(opSize),
Size: uint8(4),
Resolve: func() Address {
destination, exists := c.codeLabels[operands.Label.Name]
if !exists {
panic("unknown label")
}
return destination - end
},
})
}
}
}