Added asmc package

This commit is contained in:
2025-02-06 23:26:10 +01:00
parent cae3092df7
commit 78aee7999b
14 changed files with 464 additions and 386 deletions

32
src/asmc/Finalize.go Normal file
View File

@ -0,0 +1,32 @@
package asmc
import (
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/config"
"git.akyoto.dev/cli/q/src/dll"
)
// Finalize generates the final machine code.
func Finalize(a asm.Assembler, dlls dll.List) ([]byte, []byte) {
data, dataLabels := a.Data.Finalize()
if config.TargetOS == config.Windows && len(data) == 0 {
data = []byte{0}
}
c := compiler{
code: make([]byte, 0, len(a.Instructions)*8),
codeLabels: map[string]Address{},
codeStart: codeOffset(),
data: data,
dataLabels: dataLabels,
dlls: dlls,
}
for _, x := range a.Instructions {
c.compile(x)
}
c.resolvePointers()
return c.code, c.data
}

31
src/asmc/call.go Normal file
View File

@ -0,0 +1,31 @@
package asmc
import (
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/x86"
)
func (c *compiler) call(x asm.Instruction) {
c.code = x86.Call(c.code, 0x00_00_00_00)
size := 4
label := x.Data.(*asm.Label)
pointer := &pointer{
Position: Address(len(c.code) - size),
OpSize: 1,
Size: uint8(size),
}
pointer.Resolve = func() Address {
destination, exists := c.codeLabels[label.Name]
if !exists {
panic("unknown jump label")
}
distance := destination - (pointer.Position + Address(pointer.Size))
return Address(distance)
}
c.codePointers = append(c.codePointers, pointer)
}

26
src/asmc/codeOffset.go Normal file
View File

@ -0,0 +1,26 @@
package asmc
import (
"git.akyoto.dev/cli/q/src/config"
"git.akyoto.dev/cli/q/src/elf"
"git.akyoto.dev/cli/q/src/fs"
"git.akyoto.dev/cli/q/src/macho"
"git.akyoto.dev/cli/q/src/pe"
)
// codeOffset returns the file offset of the code section.
func codeOffset() Address {
headerEnd := Address(0)
switch config.TargetOS {
case config.Linux:
headerEnd = elf.HeaderEnd
case config.Mac:
headerEnd = macho.HeaderEnd
case config.Windows:
headerEnd = pe.HeaderEnd
}
offset, _ := fs.Align(headerEnd, config.Align)
return offset
}

162
src/asmc/compile.go Normal file
View File

@ -0,0 +1,162 @@
package asmc
import (
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/x86"
)
func (c *compiler) compile(x asm.Instruction) {
switch x.Mnemonic {
case asm.ADD:
switch operands := x.Data.(type) {
case *asm.RegisterNumber:
c.code = x86.AddRegisterNumber(c.code, operands.Register, operands.Number)
case *asm.RegisterRegister:
c.code = x86.AddRegisterRegister(c.code, operands.Destination, operands.Source)
}
case asm.AND:
switch operands := x.Data.(type) {
case *asm.RegisterNumber:
c.code = x86.AndRegisterNumber(c.code, operands.Register, operands.Number)
case *asm.RegisterRegister:
c.code = x86.AndRegisterRegister(c.code, operands.Destination, operands.Source)
}
case asm.SUB:
switch operands := x.Data.(type) {
case *asm.RegisterNumber:
c.code = x86.SubRegisterNumber(c.code, operands.Register, operands.Number)
case *asm.RegisterRegister:
c.code = x86.SubRegisterRegister(c.code, operands.Destination, operands.Source)
}
case asm.MUL:
switch operands := x.Data.(type) {
case *asm.RegisterNumber:
c.code = x86.MulRegisterNumber(c.code, operands.Register, operands.Number)
case *asm.RegisterRegister:
c.code = x86.MulRegisterRegister(c.code, operands.Destination, operands.Source)
}
case asm.DIV:
switch operands := x.Data.(type) {
case *asm.RegisterRegister:
if operands.Destination != x86.RAX {
c.code = x86.MoveRegisterRegister(c.code, x86.RAX, operands.Destination)
}
c.code = x86.ExtendRAXToRDX(c.code)
c.code = x86.DivRegister(c.code, operands.Source)
if operands.Destination != x86.RAX {
c.code = x86.MoveRegisterRegister(c.code, operands.Destination, x86.RAX)
}
}
case asm.MODULO:
switch operands := x.Data.(type) {
case *asm.RegisterRegister:
if operands.Destination != x86.RAX {
c.code = x86.MoveRegisterRegister(c.code, x86.RAX, operands.Destination)
}
c.code = x86.ExtendRAXToRDX(c.code)
c.code = x86.DivRegister(c.code, operands.Source)
if operands.Destination != x86.RDX {
c.code = x86.MoveRegisterRegister(c.code, operands.Destination, x86.RDX)
}
}
case asm.CALL:
c.call(x)
case asm.COMMENT:
return
case asm.COMPARE:
switch operands := x.Data.(type) {
case *asm.RegisterNumber:
c.code = x86.CompareRegisterNumber(c.code, operands.Register, operands.Number)
case *asm.RegisterRegister:
c.code = x86.CompareRegisterRegister(c.code, operands.Destination, operands.Source)
}
case asm.DLLCALL:
c.dllCall(x)
case asm.JE, asm.JNE, asm.JG, asm.JGE, asm.JL, asm.JLE, asm.JUMP:
c.jump(x)
case asm.LABEL:
c.codeLabels[x.Data.(*asm.Label).Name] = Address(len(c.code))
case asm.LOAD:
switch operands := x.Data.(type) {
case *asm.MemoryRegister:
c.code = x86.LoadRegister(c.code, operands.Register, operands.Address.Offset, operands.Address.Length, operands.Address.Base)
}
case asm.MOVE:
c.move(x)
case asm.NEGATE:
switch operands := x.Data.(type) {
case *asm.Register:
c.code = x86.NegateRegister(c.code, operands.Register)
}
case asm.OR:
switch operands := x.Data.(type) {
case *asm.RegisterNumber:
c.code = x86.OrRegisterNumber(c.code, operands.Register, operands.Number)
case *asm.RegisterRegister:
c.code = x86.OrRegisterRegister(c.code, operands.Destination, operands.Source)
}
case asm.POP:
switch operands := x.Data.(type) {
case *asm.Register:
c.code = x86.PopRegister(c.code, operands.Register)
}
case asm.PUSH:
switch operands := x.Data.(type) {
case *asm.Register:
c.code = x86.PushRegister(c.code, operands.Register)
}
case asm.RETURN:
c.code = x86.Return(c.code)
case asm.SHIFTL:
switch operands := x.Data.(type) {
case *asm.RegisterNumber:
c.code = x86.ShiftLeftNumber(c.code, operands.Register, byte(operands.Number)&0b111111)
}
case asm.SHIFTRS:
switch operands := x.Data.(type) {
case *asm.RegisterNumber:
c.code = x86.ShiftRightSignedNumber(c.code, operands.Register, byte(operands.Number)&0b111111)
}
case asm.STORE:
c.store(x)
case asm.SYSCALL:
c.code = x86.Syscall(c.code)
case asm.XOR:
switch operands := x.Data.(type) {
case *asm.RegisterNumber:
c.code = x86.XorRegisterNumber(c.code, operands.Register, operands.Number)
case *asm.RegisterRegister:
c.code = x86.XorRegisterRegister(c.code, operands.Destination, operands.Source)
}
default:
panic("unknown mnemonic: " + x.Mnemonic.String())
}
}

15
src/asmc/compiler.go Normal file
View File

@ -0,0 +1,15 @@
package asmc
import "git.akyoto.dev/cli/q/src/dll"
type compiler struct {
code []byte
data []byte
codeLabels map[string]Address
dataLabels map[string]Address
codePointers []*pointer
dataPointers []*pointer
codeStart Address
dlls dll.List
dllPointers []*pointer
}

41
src/asmc/dllCall.go Normal file
View File

@ -0,0 +1,41 @@
package asmc
import (
"strings"
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/x86"
)
func (c *compiler) dllCall(x asm.Instruction) {
size := 4
// TODO: R15 could be in use.
c.code = x86.MoveRegisterRegister(c.code, x86.R15, x86.RSP)
c.code = x86.AlignStack(c.code)
c.code = x86.SubRegisterNumber(c.code, x86.RSP, 32)
c.code = x86.CallAtAddress(c.code, 0x00_00_00_00)
position := len(c.code) - size
c.code = x86.MoveRegisterRegister(c.code, x86.RSP, x86.R15)
label := x.Data.(*asm.Label)
pointer := &pointer{
Position: Address(position),
OpSize: 2,
Size: uint8(size),
}
pointer.Resolve = func() Address {
dot := strings.Index(label.Name, ".")
library := label.Name[:dot]
funcName := label.Name[dot+1:]
index := c.dlls.Index(library, funcName)
if index == -1 {
panic("unknown DLL function " + label.Name)
}
return Address(index * 8)
}
c.dllPointers = append(c.dllPointers, pointer)
}

47
src/asmc/jump.go Normal file
View File

@ -0,0 +1,47 @@
package asmc
import (
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/x86"
)
func (c *compiler) jump(x asm.Instruction) {
switch x.Mnemonic {
case asm.JE:
c.code = x86.Jump8IfEqual(c.code, 0x00)
case asm.JNE:
c.code = x86.Jump8IfNotEqual(c.code, 0x00)
case asm.JG:
c.code = x86.Jump8IfGreater(c.code, 0x00)
case asm.JGE:
c.code = x86.Jump8IfGreaterOrEqual(c.code, 0x00)
case asm.JL:
c.code = x86.Jump8IfLess(c.code, 0x00)
case asm.JLE:
c.code = x86.Jump8IfLessOrEqual(c.code, 0x00)
case asm.JUMP:
c.code = x86.Jump8(c.code, 0x00)
}
size := 1
label := x.Data.(*asm.Label)
pointer := &pointer{
Position: Address(len(c.code) - size),
OpSize: 1,
Size: uint8(size),
}
pointer.Resolve = func() Address {
destination, exists := c.codeLabels[label.Name]
if !exists {
panic("unknown jump label")
}
distance := destination - (pointer.Position + Address(pointer.Size))
return Address(distance)
}
c.codePointers = append(c.codePointers, pointer)
}

58
src/asmc/move.go Normal file
View File

@ -0,0 +1,58 @@
package asmc
import (
"strings"
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/config"
"git.akyoto.dev/cli/q/src/x86"
)
func (c *compiler) move(x asm.Instruction) {
switch operands := x.Data.(type) {
case *asm.RegisterNumber:
c.code = x86.MoveRegisterNumber(c.code, operands.Register, operands.Number)
case *asm.RegisterRegister:
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)
if strings.HasPrefix(regLabel.Label, "data_") {
c.dataPointers = append(c.dataPointers, &pointer{
Position: Address(len(c.code) - size),
OpSize: uint8(opSize),
Size: uint8(size),
Resolve: func() Address {
destination, exists := c.dataLabels[regLabel.Label]
if !exists {
panic("unknown label")
}
return Address(destination)
},
})
} else {
c.codePointers = append(c.codePointers, &pointer{
Position: Address(len(c.code) - size),
OpSize: uint8(opSize),
Size: uint8(size),
Resolve: func() Address {
destination, exists := c.codeLabels[regLabel.Label]
if !exists {
panic("unknown label")
}
return config.BaseAddress + c.codeStart + destination
},
})
}
}
}

14
src/asmc/pointer.go Normal file
View File

@ -0,0 +1,14 @@
package asmc
// Address represents a memory address.
type Address = int32
// pointer stores a relative memory address that we can later turn into an absolute one.
// Position: The machine code offset where the address was inserted.
// Resolve: The function that will return the final address.
type pointer struct {
Resolve func() Address
Position Address
OpSize uint8
Size uint8
}

View File

@ -0,0 +1,98 @@
package asmc
import (
"encoding/binary"
"fmt"
"slices"
"git.akyoto.dev/cli/q/src/config"
"git.akyoto.dev/cli/q/src/fs"
"git.akyoto.dev/cli/q/src/sizeof"
)
// resolvePointers resolves the addresses of all pointers within the code and writes the correct addresses to the code slice.
func (c *compiler) resolvePointers() {
restart:
for i, pointer := range c.codePointers {
address := pointer.Resolve()
if sizeof.Signed(int64(address)) > int(pointer.Size) {
left := c.code[:pointer.Position-Address(pointer.OpSize)]
right := c.code[pointer.Position+Address(pointer.Size):]
size := pointer.Size + pointer.OpSize
opCode := c.code[pointer.Position-Address(pointer.OpSize)]
var jump []byte
switch opCode {
case 0x74: // JE
jump = []byte{0x0F, 0x84}
case 0x75: // JNE
jump = []byte{0x0F, 0x85}
case 0x7C: // JL
jump = []byte{0x0F, 0x8C}
case 0x7D: // JGE
jump = []byte{0x0F, 0x8D}
case 0x7E: // JLE
jump = []byte{0x0F, 0x8E}
case 0x7F: // JG
jump = []byte{0x0F, 0x8F}
case 0xEB: // JMP
jump = []byte{0xE9}
default:
panic(fmt.Errorf("failed to increase pointer size for instruction 0x%x", opCode))
}
pointer.Position += Address(len(jump) - int(pointer.OpSize))
pointer.OpSize = uint8(len(jump))
pointer.Size = 4
jump = binary.LittleEndian.AppendUint32(jump, uint32(address))
offset := Address(len(jump)) - Address(size)
for _, following := range c.codePointers[i+1:] {
following.Position += offset
}
for key, address := range c.codeLabels {
if address > pointer.Position {
c.codeLabels[key] += offset
}
}
c.code = slices.Concat(left, jump, right)
goto restart
}
slice := c.code[pointer.Position : pointer.Position+Address(pointer.Size)]
switch pointer.Size {
case 1:
slice[0] = uint8(address)
case 2:
binary.LittleEndian.PutUint16(slice, uint16(address))
case 4:
binary.LittleEndian.PutUint32(slice, uint32(address))
case 8:
binary.LittleEndian.PutUint64(slice, uint64(address))
}
}
dataStart, _ := fs.Align(c.codeStart+Address(len(c.code)), config.Align)
for _, pointer := range c.dataPointers {
address := config.BaseAddress + Address(dataStart) + pointer.Resolve()
slice := c.code[pointer.Position : pointer.Position+4]
binary.LittleEndian.PutUint32(slice, uint32(address))
}
if config.TargetOS == config.Windows {
importsStart, _ := fs.Align(dataStart+Address(len(c.data)), config.Align)
for _, pointer := range c.dllPointers {
destination := Address(importsStart) + pointer.Resolve()
delta := destination - Address(c.codeStart+pointer.Position+Address(pointer.Size))
slice := c.code[pointer.Position : pointer.Position+4]
binary.LittleEndian.PutUint32(slice, uint32(delta))
}
}
}

53
src/asmc/store.go Normal file
View File

@ -0,0 +1,53 @@
package asmc
import (
"math"
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/config"
"git.akyoto.dev/cli/q/src/x86"
)
func (c *compiler) store(x asm.Instruction) {
switch operands := x.Data.(type) {
case *asm.MemoryNumber:
if operands.Address.OffsetRegister == math.MaxUint8 {
c.code = x86.StoreNumber(c.code, operands.Address.Base, operands.Address.Offset, operands.Address.Length, operands.Number)
} else {
c.code = x86.StoreDynamicNumber(c.code, operands.Address.Base, operands.Address.OffsetRegister, operands.Address.Length, operands.Number)
}
case *asm.MemoryLabel:
start := len(c.code)
if operands.Address.OffsetRegister == math.MaxUint8 {
c.code = x86.StoreNumber(c.code, operands.Address.Base, operands.Address.Offset, operands.Address.Length, 0b00_00_00_00)
} else {
c.code = x86.StoreDynamicNumber(c.code, operands.Address.Base, operands.Address.OffsetRegister, operands.Address.Length, 0b00_00_00_00)
}
size := 4
opSize := len(c.code) - size - start
memLabel := x.Data.(*asm.MemoryLabel)
c.codePointers = append(c.codePointers, &pointer{
Position: Address(len(c.code) - size),
OpSize: uint8(opSize),
Size: uint8(size),
Resolve: func() Address {
destination, exists := c.codeLabels[memLabel.Label]
if !exists {
panic("unknown label")
}
return config.BaseAddress + c.codeStart + destination
},
})
case *asm.MemoryRegister:
if operands.Address.OffsetRegister == math.MaxUint8 {
c.code = x86.StoreRegister(c.code, operands.Address.Base, operands.Address.Offset, operands.Address.Length, operands.Register)
} else {
c.code = x86.StoreDynamicRegister(c.code, operands.Address.Base, operands.Address.OffsetRegister, operands.Address.Length, operands.Register)
}
}
}