Implemented data labels

This commit is contained in:
2024-07-13 00:13:13 +02:00
parent 7b18056006
commit 9df899cb52
18 changed files with 428 additions and 327 deletions

View File

@ -1,268 +1,15 @@
package asm
import (
"encoding/binary"
"fmt"
"git.akyoto.dev/cli/q/src/build/arch/x64"
)
import "maps"
// Assembler contains a list of instructions.
type Assembler struct {
Instructions []Instruction
}
// Finalize generates the final machine code.
func (a Assembler) Finalize() ([]byte, []byte) {
code := make([]byte, 0, len(a.Instructions)*8)
data := make([]byte, 0, 16)
labels := map[string]Address{}
pointers := []*Pointer{}
for _, x := range a.Instructions {
switch x.Mnemonic {
case ADD:
switch operands := x.Data.(type) {
case *RegisterNumber:
code = x64.AddRegisterNumber(code, operands.Register, operands.Number)
case *RegisterRegister:
code = x64.AddRegisterRegister(code, operands.Destination, operands.Source)
}
case SUB:
switch operands := x.Data.(type) {
case *RegisterNumber:
code = x64.SubRegisterNumber(code, operands.Register, operands.Number)
case *RegisterRegister:
code = x64.SubRegisterRegister(code, operands.Destination, operands.Source)
}
case MUL:
switch operands := x.Data.(type) {
case *RegisterNumber:
code = x64.MulRegisterNumber(code, operands.Register, operands.Number)
case *RegisterRegister:
code = x64.MulRegisterRegister(code, operands.Destination, operands.Source)
}
case DIV:
code = divide(code, x.Data)
case CALL:
code = x64.Call(code, 0x00_00_00_00)
size := 4
label := x.Data.(*Label)
nextInstructionAddress := Address(len(code))
pointers = append(pointers, &Pointer{
Position: Address(len(code) - size),
OpSize: 1,
Size: uint8(size),
Resolve: func() Address {
destination, exists := labels[label.Name]
if !exists {
panic("unknown call label")
}
distance := destination - nextInstructionAddress
return Address(distance)
},
})
case COMMENT:
continue
case COMPARE:
switch operands := x.Data.(type) {
case *RegisterNumber:
code = x64.CompareRegisterNumber(code, operands.Register, operands.Number)
case *RegisterRegister:
code = x64.CompareRegisterRegister(code, operands.Destination, operands.Source)
}
case JE, JNE, JG, JGE, JL, JLE, JUMP:
switch x.Mnemonic {
case JE:
code = x64.Jump8IfEqual(code, 0x00)
case JNE:
code = x64.Jump8IfNotEqual(code, 0x00)
case JG:
code = x64.Jump8IfGreater(code, 0x00)
case JGE:
code = x64.Jump8IfGreaterOrEqual(code, 0x00)
case JL:
code = x64.Jump8IfLess(code, 0x00)
case JLE:
code = x64.Jump8IfLessOrEqual(code, 0x00)
case JUMP:
code = x64.Jump8(code, 0x00)
}
size := 1
label := x.Data.(*Label)
nextInstructionAddress := Address(len(code))
pointers = append(pointers, &Pointer{
Position: Address(len(code) - size),
OpSize: 1,
Size: uint8(size),
Resolve: func() Address {
destination, exists := labels[label.Name]
if !exists {
panic("unknown jump label")
}
distance := destination - nextInstructionAddress
return Address(distance)
},
})
case LABEL:
labels[x.Data.(*Label).Name] = Address(len(code))
case MOVE:
switch operands := x.Data.(type) {
case *RegisterNumber:
code = x64.MoveRegisterNumber32(code, operands.Register, uint32(operands.Number))
case *RegisterRegister:
code = x64.MoveRegisterRegister64(code, operands.Destination, operands.Source)
}
case POP:
switch operands := x.Data.(type) {
case *Register:
code = x64.PopRegister(code, operands.Register)
}
case PUSH:
switch operands := x.Data.(type) {
case *Register:
code = x64.PushRegister(code, operands.Register)
}
case RETURN:
code = x64.Return(code)
case SYSCALL:
code = x64.Syscall(code)
default:
panic("Unknown mnemonic: " + x.Mnemonic.String())
}
}
// dataStart := config.BaseAddress + config.CodeOffset + Address(len(code))
restart:
for i, pointer := range pointers {
address := pointer.Resolve()
if x64.SizeOf(int64(address)) > int(pointer.Size) {
left := code[:pointer.Position-Address(pointer.OpSize)]
right := code[pointer.Position+Address(pointer.Size):]
size := pointer.Size + pointer.OpSize
opCode := 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 pointers[i+1:] {
following.Position += offset
}
code = append(left, jump...)
code = append(code, right...)
goto restart
}
slice := 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))
}
}
return code, data
Data map[string][]byte
}
// Merge combines the contents of this assembler with another one.
func (a *Assembler) Merge(b Assembler) {
a.Instructions = append(a.Instructions, b.Instructions...)
}
// divide implements the division on x64 machines.
func divide(code []byte, data any) []byte {
code = x64.PushRegister(code, x64.RDX)
switch operands := data.(type) {
case *RegisterNumber:
if operands.Register == x64.RAX {
code = x64.PushRegister(code, x64.RCX)
code = x64.MoveRegisterNumber32(code, x64.RCX, uint32(operands.Number))
code = x64.ExtendRAXToRDX(code)
code = x64.DivRegister(code, x64.RCX)
code = x64.PopRegister(code, x64.RCX)
} else {
code = x64.PushRegister(code, x64.RAX)
code = x64.MoveRegisterRegister64(code, x64.RAX, operands.Register)
code = x64.MoveRegisterNumber32(code, operands.Register, uint32(operands.Number))
code = x64.ExtendRAXToRDX(code)
code = x64.DivRegister(code, operands.Register)
code = x64.MoveRegisterRegister64(code, operands.Register, x64.RAX)
code = x64.PopRegister(code, x64.RAX)
}
case *RegisterRegister:
if operands.Destination == x64.RAX {
code = x64.ExtendRAXToRDX(code)
code = x64.DivRegister(code, operands.Source)
} else {
code = x64.PushRegister(code, x64.RAX)
code = x64.MoveRegisterRegister64(code, x64.RAX, operands.Destination)
code = x64.ExtendRAXToRDX(code)
code = x64.DivRegister(code, operands.Source)
code = x64.MoveRegisterRegister64(code, operands.Destination, x64.RAX)
code = x64.PopRegister(code, x64.RAX)
}
}
code = x64.PopRegister(code, x64.RDX)
return code
maps.Copy(a.Data, b.Data)
}