Improved build performance

This commit is contained in:
Eduard Urbach 2023-10-29 16:16:36 +01:00
parent fbe6aa80bb
commit 5fe83543fd
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
18 changed files with 122 additions and 120 deletions

2
go.mod
View File

@ -1,5 +1,3 @@
module git.akyoto.dev/cli/q module git.akyoto.dev/cli/q
go 1.21 go 1.21
require git.akyoto.dev/go/assert v0.1.3

2
go.sum
View File

@ -1,2 +0,0 @@
git.akyoto.dev/go/assert v0.1.3 h1:QwCUbmG4aZYsNk/OuRBz1zWVKmGlDUHhOnnDBfn8Qw8=
git.akyoto.dev/go/assert v0.1.3/go.mod h1:0GzMaM0eURuDwtGkJJkCsI7r2aUKr+5GmWNTFPgDocM=

View File

@ -7,7 +7,6 @@ import (
"git.akyoto.dev/cli/q/src/cli" "git.akyoto.dev/cli/q/src/cli"
"git.akyoto.dev/cli/q/src/log" "git.akyoto.dev/cli/q/src/log"
"git.akyoto.dev/go/assert"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
@ -28,14 +27,18 @@ func TestCLI(t *testing.T) {
{[]string{"system"}, 0}, {[]string{"system"}, 0},
{[]string{"build", "non-existing-directory"}, 1}, {[]string{"build", "non-existing-directory"}, 1},
{[]string{"build", "examples/hello/hello.q"}, 1}, {[]string{"build", "examples/hello/hello.q"}, 1},
{[]string{"build", "examples/hello", "--dry"}, 0},
{[]string{"build", "examples/hello", "--invalid"}, 2}, {[]string{"build", "examples/hello", "--invalid"}, 2},
{[]string{"build", "examples/hello", "--dry"}, 0},
} }
for _, test := range tests { for _, test := range tests {
exitCode := cli.Main(test.arguments)
t.Log(test.arguments) t.Log(test.arguments)
assert.Equal(t, exitCode, test.expectedExitCode) exitCode := cli.Main(test.arguments)
if exitCode != test.expectedExitCode {
t.Errorf("exit code %d (expected %d)", exitCode, test.expectedExitCode)
t.FailNow()
}
} }
} }

View File

@ -4,4 +4,4 @@ package asm
type Address = uint32 type Address = uint32
// Index references an instruction by its position. // Index references an instruction by its position.
type Index = uint32 // type Index = uint32

View File

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

19
src/asm/Data.go Normal file
View 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
View 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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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))
}

View File

@ -10,27 +10,31 @@ import (
"git.akyoto.dev/cli/q/src/directory" "git.akyoto.dev/cli/q/src/directory"
"git.akyoto.dev/cli/q/src/elf" "git.akyoto.dev/cli/q/src/elf"
"git.akyoto.dev/cli/q/src/errors" "git.akyoto.dev/cli/q/src/errors"
"git.akyoto.dev/cli/q/src/linux"
"git.akyoto.dev/cli/q/src/log" "git.akyoto.dev/cli/q/src/log"
"git.akyoto.dev/cli/q/src/register" "git.akyoto.dev/cli/q/src/register"
"git.akyoto.dev/cli/q/src/syscall"
) )
// Build describes a compiler build. // Build describes a compiler build.
type Build struct { type Build struct {
Name string
Directory string Directory string
Verbose bool
WriteExecutable bool WriteExecutable bool
} }
// New creates a new build. // New creates a new build.
func New(directory string) *Build { func New(directory string) *Build {
return &Build{ return &Build{
Name: filepath.Base(directory),
Directory: directory, Directory: directory,
WriteExecutable: true, WriteExecutable: true,
} }
} }
var (
hello = []byte("Hello\n")
world = []byte("World\n")
)
// Run parses the input files and generates an executable file. // Run parses the input files and generates an executable file.
func (build *Build) Run() error { func (build *Build) Run() error {
// err := build.Compile() // err := build.Compile()
@ -39,33 +43,34 @@ func (build *Build) Run() error {
// return err // return err
// } // }
a := asm.New() a := asm.Assembler{
Instructions: make([]asm.Instruction, 0, 8),
Verbose: build.Verbose,
}
a.MoveRegisterNumber(register.Syscall0, syscall.Write) a.MoveRegisterNumber(register.Syscall0, linux.Write)
a.MoveRegisterNumber(register.Syscall1, 1) a.MoveRegisterNumber(register.Syscall1, 1)
hello := []byte("Hello\n")
a.MoveRegisterData(register.Syscall2, hello) a.MoveRegisterData(register.Syscall2, hello)
a.MoveRegisterNumber(register.Syscall3, uint64(len(hello))) a.MoveRegisterNumber(register.Syscall3, uint64(len(hello)))
a.Syscall() a.Syscall()
a.MoveRegisterNumber(register.Syscall0, syscall.Write) a.MoveRegisterNumber(register.Syscall0, linux.Write)
a.MoveRegisterNumber(register.Syscall1, 1) a.MoveRegisterNumber(register.Syscall1, 1)
world := []byte("World\n")
a.MoveRegisterData(register.Syscall2, world) a.MoveRegisterData(register.Syscall2, world)
a.MoveRegisterNumber(register.Syscall3, uint64(len(world))) a.MoveRegisterNumber(register.Syscall3, uint64(len(world)))
a.Syscall() a.Syscall()
a.MoveRegisterNumber(register.Syscall0, syscall.Exit) a.MoveRegisterNumber(register.Syscall0, linux.Exit)
a.MoveRegisterNumber(register.Syscall1, 0) a.MoveRegisterNumber(register.Syscall1, 0)
a.Syscall() a.Syscall()
result := a.Finalize() code, data := a.Finalize()
if !build.WriteExecutable { if !build.WriteExecutable {
return nil return nil
} }
return writeToDisk(build.Executable(), result.Code, result.Data) return writeToDisk(build.Executable(), code, data)
} }
// Compile compiles all the functions. // Compile compiles all the functions.
@ -93,7 +98,7 @@ func (build *Build) Compile() error {
// Executable returns the path to the executable. // Executable returns the path to the executable.
func (build *Build) Executable() string { func (build *Build) Executable() string {
return filepath.Join(build.Directory, build.Name) return filepath.Join(build.Directory, filepath.Base(build.Directory))
} }
// writeToDisk writes the executable file to disk. // writeToDisk writes the executable file to disk.

View File

@ -20,6 +20,9 @@ func Build(args []string) int {
case "--dry": case "--dry":
b.WriteExecutable = false b.WriteExecutable = false
case "--verbose", "-v":
b.Verbose = true
default: default:
log.Error.Printf("Unknown parameter: %s\n", args[i]) log.Error.Printf("Unknown parameter: %s\n", args[i])
return 2 return 2

View File

@ -1,6 +1,5 @@
package syscall package linux
// Linux syscalls
const ( const (
Read = iota Read = iota
Write Write

View File

@ -1,10 +1,14 @@
package x64 package x64
import "io"
// Call places the return address on the top of the stack and continues // 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. // program flow at the new address. The address is relative to the next instruction.
func Call(w io.ByteWriter, address uint32) { func Call(code []byte, address uint32) []byte {
w.WriteByte(0xe8) return append(
appendUint32(w, address) code,
0xe8,
byte(address),
byte(address>>8),
byte(address>>16),
byte(address>>24),
)
} }

13
src/x64/Move.go Normal file
View File

@ -0,0 +1,13 @@
package x64
// MoveRegNum32 moves a 32 bit integer into the given register.
func MoveRegNum32(code []byte, register uint8, number uint32) []byte {
return append(
code,
0xb8+register,
byte(number),
byte(number>>8),
byte(number>>16),
byte(number>>24),
)
}

View File

@ -1,9 +1,7 @@
package x64 package x64
import "io"
// Return transfers program control to a return address located on the top of the stack. // 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. // The address is usually placed on the stack by a Call instruction.
func Return(w io.ByteWriter) { func Return(code []byte) []byte {
w.WriteByte(0xc3) return append(code, 0xc3)
} }

6
src/x64/Syscall.go Normal file
View File

@ -0,0 +1,6 @@
package x64
// Syscall is the primary way to communicate with the OS kernel.
func Syscall(code []byte) []byte {
return append(code, 0x0f, 0x05)
}