Added memory address patching
This commit is contained in:
@ -1,37 +1,78 @@
|
||||
package asm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/asm/x64"
|
||||
"git.akyoto.dev/cli/q/src/config"
|
||||
"git.akyoto.dev/cli/q/src/log"
|
||||
"git.akyoto.dev/cli/q/src/register"
|
||||
)
|
||||
|
||||
// Assembler contains a list of instructions.
|
||||
type Assembler struct {
|
||||
instructions []Instruction
|
||||
labels map[string]int
|
||||
labels map[string]Index
|
||||
verbose bool
|
||||
}
|
||||
|
||||
// New creates a new assembler.
|
||||
func New() *Assembler {
|
||||
return &Assembler{
|
||||
instructions: make([]Instruction, 0, 8),
|
||||
labels: map[string]int{},
|
||||
labels: map[string]Index{},
|
||||
verbose: true,
|
||||
}
|
||||
}
|
||||
|
||||
// Finalize generates the final machine code.
|
||||
func (a *Assembler) Finalize() *Result {
|
||||
final := Result{}
|
||||
result := &Result{}
|
||||
pointers := map[Address]Address{}
|
||||
code := bytes.NewBuffer(result.Code)
|
||||
|
||||
for _, instr := range a.instructions {
|
||||
instr.Write(&final.Code)
|
||||
for _, x := range a.instructions {
|
||||
if a.verbose {
|
||||
log.Info.Println(x.String())
|
||||
}
|
||||
|
||||
switch x.Mnemonic {
|
||||
case MOV:
|
||||
x64.MoveRegNum32(code, uint8(x.Destination), uint32(x.Number))
|
||||
|
||||
case MOVDATA:
|
||||
position := result.AddData(x.Data)
|
||||
x64.MoveRegNum32(code, uint8(x.Destination), 0)
|
||||
pointers[Address(code.Len()-4)] = position
|
||||
|
||||
case SYSCALL:
|
||||
x64.Syscall(code)
|
||||
}
|
||||
}
|
||||
|
||||
return &final
|
||||
result.Code = code.Bytes()
|
||||
dataStart := config.BaseAddress + config.CodeOffset + Address(len(result.Code))
|
||||
|
||||
for codePos, dataPos := range pointers {
|
||||
binary.LittleEndian.PutUint32(result.Code[codePos:codePos+4], dataStart+dataPos)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// AddLabel creates a new label at the current position.
|
||||
func (a *Assembler) AddLabel(name string) {
|
||||
a.labels[name] = len(a.instructions)
|
||||
a.labels[name] = Index(len(a.instructions))
|
||||
}
|
||||
|
||||
// MoveRegisterData moves a data section address into the given register.
|
||||
func (a *Assembler) MoveRegisterData(reg register.ID, data []byte) {
|
||||
a.instructions = append(a.instructions, Instruction{
|
||||
Mnemonic: MOVDATA,
|
||||
Destination: reg,
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// MoveRegisterNumber moves a number into the given register.
|
||||
|
Reference in New Issue
Block a user