Added build statistics

This commit is contained in:
Eduard Urbach 2025-02-05 23:10:50 +01:00
parent 64bd2bae59
commit f558a69366
Signed by: eduard
GPG key ID: 49226B848C78F6C8
6 changed files with 54 additions and 18 deletions

View file

@ -105,6 +105,7 @@ func Compile(files <-chan *fs.File, functions <-chan *core.Function, structs <-c
result.Main = main
result.Functions = allFunctions
result.finalize()
return result, nil
}

View file

@ -2,6 +2,7 @@ package compiler
import (
"bufio"
"fmt"
"io"
"os"
@ -13,6 +14,7 @@ import (
"git.akyoto.dev/cli/q/src/macho"
"git.akyoto.dev/cli/q/src/pe"
"git.akyoto.dev/cli/q/src/x64"
"git.akyoto.dev/go/color/ansi"
)
// Result contains all the compiled functions in a build.
@ -21,10 +23,13 @@ type Result struct {
Functions map[string]*core.Function
InstructionCount int
DataCount int
Code []byte
Data []byte
DLLs dll.List
}
// finalize generates the final machine code.
func (r *Result) finalize() ([]byte, []byte, dll.List) {
func (r *Result) finalize() {
// This will be the entry point of the executable.
// The only job of the entry function is to call `main` and exit cleanly.
// The reason we call `main` instead of using `main` itself is to place
@ -50,7 +55,7 @@ func (r *Result) finalize() ([]byte, []byte, dll.List) {
final.DLLCall("kernel32.ExitProcess")
}
dlls := dll.List{
r.DLLs = dll.List{
{Name: "kernel32", Functions: []string{"ExitProcess"}},
}
@ -61,7 +66,7 @@ func (r *Result) finalize() ([]byte, []byte, dll.List) {
for _, library := range f.DLLs {
for _, fn := range library.Functions {
dlls = dlls.Append(library.Name, fn)
r.DLLs = r.DLLs.Append(library.Name, fn)
}
}
})
@ -82,8 +87,7 @@ func (r *Result) finalize() ([]byte, []byte, dll.List) {
final.DLLCall("kernel32.ExitProcess")
}
code, data := final.Finalize(dlls)
return code, data, dlls
r.Code, r.Data = final.Finalize(r.DLLs)
}
// eachFunction recursively finds all the calls to external functions.
@ -119,10 +123,17 @@ func (r *Result) PrintInstructions() {
})
}
// PrintStatistics shows the statistics.
func (r *Result) PrintStatistics() {
ansi.Dim.Println("╭──────────────────────────────────────────────────────────────────────────────╮")
ansi.Dim.Printf("│ %-44s%-32s │\n", "Code:", fmt.Sprintf("%d bytes", len(r.Code)))
ansi.Dim.Printf("│ %-44s%-32s │\n", "Data:", fmt.Sprintf("%d bytes", len(r.Data)))
ansi.Dim.Println("╰──────────────────────────────────────────────────────────────────────────────╯")
}
// Write writes the executable to the given writer.
func (r *Result) Write(writer io.Writer) error {
code, data, dlls := r.finalize()
return write(writer, code, data, dlls)
return write(writer, r.Code, r.Data, r.DLLs)
}
// Write writes an executable file to disk.