Improved build performance
This commit is contained in:
@ -4,4 +4,4 @@ package asm
|
||||
type Address = uint32
|
||||
|
||||
// Index references an instruction by its position.
|
||||
type Index = uint32
|
||||
// type Index = uint32
|
||||
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
19
src/asm/Data.go
Normal file
19
src/asm/Data.go
Normal file
@ -0,0 +1,19 @@
|
||||
package asm
|
||||
|
||||
import "bytes"
|
||||
|
||||
// Data represents the static read-only data.
|
||||
type Data []byte
|
||||
|
||||
// Add adds the given bytes to the data block and returns the address relative to the start of the data section.
|
||||
func (data *Data) Add(block []byte) Address {
|
||||
position := bytes.Index(*data, block)
|
||||
|
||||
if position != -1 {
|
||||
return Address(position)
|
||||
}
|
||||
|
||||
address := Address(len(*data))
|
||||
*data = append(*data, block...)
|
||||
return address
|
||||
}
|
9
src/asm/Pointer.go
Normal file
9
src/asm/Pointer.go
Normal file
@ -0,0 +1,9 @@
|
||||
package asm
|
||||
|
||||
// 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.
|
||||
// Address: The offset inside the section.
|
||||
type Pointer struct {
|
||||
Position uint32
|
||||
Address uint32
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package asm
|
||||
|
||||
import "bytes"
|
||||
|
||||
// Result is the compilation result and contains the machine code as well as the data.
|
||||
type Result struct {
|
||||
Code []byte
|
||||
Data []byte
|
||||
}
|
||||
|
||||
// AddData adds the given bytes to the data block and returns the address relative to the start of the data section.
|
||||
func (result *Result) AddData(block []byte) Address {
|
||||
position := bytes.Index(result.Data, block)
|
||||
|
||||
if position != -1 {
|
||||
return Address(position)
|
||||
}
|
||||
|
||||
address := Address(len(result.Data))
|
||||
result.Data = append(result.Data, block...)
|
||||
return address
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
package x64
|
||||
|
||||
import "io"
|
||||
|
||||
// Call places the return address on the top of the stack and continues
|
||||
// program flow at the new address. The address is relative to the next instruction.
|
||||
func Call(w io.ByteWriter, address uint32) {
|
||||
w.WriteByte(0xe8)
|
||||
appendUint32(w, address)
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package x64
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// MoveRegNum32 moves a 32 bit integer into the given register.
|
||||
func MoveRegNum32(w io.ByteWriter, register uint8, number uint32) {
|
||||
w.WriteByte(0xb8 + register)
|
||||
appendUint32(w, number)
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package x64
|
||||
|
||||
import "io"
|
||||
|
||||
// Return transfers program control to a return address located on the top of the stack.
|
||||
// The address is usually placed on the stack by a Call instruction.
|
||||
func Return(w io.ByteWriter) {
|
||||
w.WriteByte(0xc3)
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package x64
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// Syscall is the primary way to communicate with the OS kernel.
|
||||
func Syscall(w io.ByteWriter) {
|
||||
w.WriteByte(0x0f)
|
||||
w.WriteByte(0x05)
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package x64
|
||||
|
||||
import "io"
|
||||
|
||||
// appendUint32 appends a 32 bit integer in Little Endian to the given writer.
|
||||
func appendUint32(w io.ByteWriter, number uint32) {
|
||||
w.WriteByte(byte(number))
|
||||
w.WriteByte(byte(number >> 8))
|
||||
w.WriteByte(byte(number >> 16))
|
||||
w.WriteByte(byte(number >> 24))
|
||||
}
|
Reference in New Issue
Block a user