Implemented position independent addresses for x86

This commit is contained in:
2025-03-07 11:39:13 +01:00
parent 0ac7fc9a85
commit 0bf299d007
3 changed files with 68 additions and 13 deletions

View File

@ -4,7 +4,6 @@ import (
"strings"
"git.urbach.dev/cli/q/src/asm"
"git.urbach.dev/cli/q/src/config"
"git.urbach.dev/cli/q/src/x86"
)
@ -17,40 +16,43 @@ func (c *compiler) move(x asm.Instruction) {
c.code = x86.MoveRegisterRegister(c.code, operands.Destination, operands.Source)
case *asm.RegisterLabel:
start := len(c.code)
c.code = x86.MoveRegisterNumber(c.code, operands.Register, 0x00_00_00_00)
size := 4
opSize := len(c.code) - size - start
regLabel := x.Data.(*asm.RegisterLabel)
start := Address(len(c.code))
c.code = x86.LoadAddress(c.code, operands.Register, 0x00_00_00_00)
end := Address(len(c.code))
size := uint32(4)
position := end - size
opSize := position - start
if strings.HasPrefix(regLabel.Label, "data ") {
if strings.HasPrefix(operands.Label, "data ") {
c.dataPointers = append(c.dataPointers, &pointer{
Position: Address(len(c.code) - size),
Position: position,
OpSize: uint8(opSize),
Size: uint8(size),
Resolve: func() Address {
destination, exists := c.dataLabels[regLabel.Label]
destination, exists := c.dataLabels[operands.Label]
if !exists {
panic("unknown label")
}
return config.BaseAddress + c.dataStart + destination + 8
destination += c.dataStart - c.codeStart
distance := destination - end
return distance + 8
},
})
} else {
c.codePointers = append(c.codePointers, &pointer{
Position: Address(len(c.code) - size),
Position: position,
OpSize: uint8(opSize),
Size: uint8(size),
Resolve: func() Address {
destination, exists := c.codeLabels[regLabel.Label]
destination, exists := c.codeLabels[operands.Label]
if !exists {
panic("unknown label")
}
return config.BaseAddress + c.codeStart + destination
return destination - end
},
})
}