Implemented an assembler
All checks were successful
/ test (push) Successful in 28s

This commit is contained in:
Eduard Urbach 2025-06-24 12:55:26 +02:00
parent 2b703e9af2
commit 70c2da4a4d
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
40 changed files with 821 additions and 117 deletions

54
src/asm/compilerX86.go Normal file
View file

@ -0,0 +1,54 @@
package asm
import (
"encoding/binary"
"git.urbach.dev/cli/q/src/x86"
)
type compilerX86 struct {
*compiler
}
func (c *compilerX86) Compile(instr Instruction) {
switch instr := instr.(type) {
case *Call:
c.code = x86.Call(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
binary.LittleEndian.PutUint32(c.code[end-4:end], uint32(offset))
})
case *Label:
c.labels[instr.Name] = len(c.code)
case *MoveRegisterLabel:
c.code = x86.LoadAddress(c.code, instr.Destination, 0)
end := len(c.code)
c.Defer(func() {
address, exists := c.labels[instr.Label]
if !exists {
panic("unknown label: " + instr.Label)
}
offset := address - end
binary.LittleEndian.PutUint32(c.code[end-4:end], uint32(offset))
})
case *MoveRegisterNumber:
c.code = x86.MoveRegisterNumber(c.code, instr.Destination, instr.Number)
case *MoveRegisterRegister:
c.code = x86.MoveRegisterRegister(c.code, instr.Destination, instr.Source)
case *Return:
c.code = x86.Return(c.code)
case *Syscall:
c.code = x86.Syscall(c.code)
}
}