Improved build performance
This commit is contained in:
@ -1,74 +1,76 @@
|
||||
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"
|
||||
"git.akyoto.dev/cli/q/src/x64"
|
||||
)
|
||||
|
||||
// Assembler contains a list of instructions.
|
||||
type Assembler struct {
|
||||
instructions []Instruction
|
||||
labels map[string]Index
|
||||
verbose bool
|
||||
Instructions []Instruction
|
||||
// labels map[string]Index
|
||||
Verbose bool
|
||||
}
|
||||
|
||||
// New creates a new assembler.
|
||||
func New() *Assembler {
|
||||
return &Assembler{
|
||||
instructions: make([]Instruction, 0, 8),
|
||||
labels: map[string]Index{},
|
||||
verbose: true,
|
||||
Instructions: make([]Instruction, 0, 8),
|
||||
// labels: map[string]Index{},
|
||||
}
|
||||
}
|
||||
|
||||
// Finalize generates the final machine code.
|
||||
func (a *Assembler) Finalize() *Result {
|
||||
result := &Result{}
|
||||
pointers := map[Address]Address{}
|
||||
code := bytes.NewBuffer(result.Code)
|
||||
|
||||
for _, x := range a.instructions {
|
||||
if a.verbose {
|
||||
log.Info.Println(x.String())
|
||||
}
|
||||
func (a *Assembler) Finalize() ([]byte, []byte) {
|
||||
code := make([]byte, 0, len(a.Instructions)*8)
|
||||
data := make(Data, 0, 16)
|
||||
pointers := []Pointer{}
|
||||
|
||||
for _, x := range a.Instructions {
|
||||
switch x.Mnemonic {
|
||||
case MOV:
|
||||
x64.MoveRegNum32(code, uint8(x.Destination), uint32(x.Number))
|
||||
code = 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
|
||||
code = x64.MoveRegNum32(code, uint8(x.Destination), 0)
|
||||
|
||||
pointers = append(pointers, Pointer{
|
||||
Position: Address(len(code) - 4),
|
||||
Address: data.Add(x.Data),
|
||||
})
|
||||
|
||||
case SYSCALL:
|
||||
x64.Syscall(code)
|
||||
code = x64.Syscall(code)
|
||||
}
|
||||
|
||||
if a.Verbose {
|
||||
log.Info.Println(x.String())
|
||||
}
|
||||
}
|
||||
|
||||
result.Code = code.Bytes()
|
||||
dataStart := config.BaseAddress + config.CodeOffset + Address(len(result.Code))
|
||||
dataStart := config.BaseAddress + config.CodeOffset + Address(len(code))
|
||||
|
||||
for codePos, dataPos := range pointers {
|
||||
binary.LittleEndian.PutUint32(result.Code[codePos:codePos+4], dataStart+dataPos)
|
||||
for _, pointer := range pointers {
|
||||
slice := code[pointer.Position : pointer.Position+4]
|
||||
address := dataStart + pointer.Address
|
||||
binary.LittleEndian.PutUint32(slice, address)
|
||||
}
|
||||
|
||||
return result
|
||||
return code, data
|
||||
}
|
||||
|
||||
// AddLabel creates a new label at the current position.
|
||||
func (a *Assembler) AddLabel(name string) {
|
||||
a.labels[name] = Index(len(a.instructions))
|
||||
}
|
||||
// func (a *Assembler) AddLabel(name string) {
|
||||
// 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{
|
||||
a.Instructions = append(a.Instructions, Instruction{
|
||||
Mnemonic: MOVDATA,
|
||||
Destination: reg,
|
||||
Data: data,
|
||||
@ -77,7 +79,7 @@ func (a *Assembler) MoveRegisterData(reg register.ID, data []byte) {
|
||||
|
||||
// MoveRegisterNumber moves a number into the given register.
|
||||
func (a *Assembler) MoveRegisterNumber(reg register.ID, number uint64) {
|
||||
a.instructions = append(a.instructions, Instruction{
|
||||
a.Instructions = append(a.Instructions, Instruction{
|
||||
Mnemonic: MOV,
|
||||
Destination: reg,
|
||||
Number: number,
|
||||
@ -86,7 +88,7 @@ func (a *Assembler) MoveRegisterNumber(reg register.ID, number uint64) {
|
||||
|
||||
// Syscall executes a kernel function.
|
||||
func (a *Assembler) Syscall() {
|
||||
a.instructions = append(a.instructions, Instruction{
|
||||
a.Instructions = append(a.Instructions, Instruction{
|
||||
Mnemonic: SYSCALL,
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user