Implemented jumps and jump related optimizations
All checks were successful
/ test (push) Successful in 15s

This commit is contained in:
Eduard Urbach 2025-06-27 15:01:49 +02:00
parent abbc4d9ae2
commit 312e3b2e31
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
5 changed files with 120 additions and 6 deletions

View file

@ -3,6 +3,7 @@ package asm
import (
"encoding/binary"
"git.urbach.dev/cli/q/src/sizeof"
"git.urbach.dev/cli/q/src/x86"
)
@ -26,6 +27,27 @@ func (c *compilerX86) Compile(instr Instruction) {
offset := address - end
binary.LittleEndian.PutUint32(c.code[end-4:end], uint32(offset))
})
case *FunctionStart:
case *FunctionEnd:
case *Jump:
c.code = x86.Jump8(c.code, 0)
end := len(c.code)
c.Defer(func() {
address, exists := c.labels[instr.Label]
if !exists {
panic("unknown label: " + instr.Label)
}
offset := address - end
if sizeof.Signed(offset) > 1 {
panic("not implemented: long jumps")
}
c.code[end-1] = byte(offset)
})
case *Label:
c.labels[instr.Name] = len(c.code)
case *MoveRegisterLabel:
@ -50,5 +72,7 @@ func (c *compilerX86) Compile(instr Instruction) {
c.code = x86.Return(c.code)
case *Syscall:
c.code = x86.Syscall(c.code)
default:
panic("unknown instruction")
}
}