Implemented dead code elimination

This commit is contained in:
2024-06-27 17:36:45 +02:00
parent d86d411959
commit 526b92aa09
7 changed files with 51 additions and 17 deletions

View File

@ -8,7 +8,8 @@ import (
// Result contains all the compiled functions in a build.
type Result struct {
Functions map[string]*Function
Used []*Function
Unused map[string]*Function
instructionCount int
}
@ -27,13 +28,8 @@ func (r Result) Finalize() ([]byte, []byte) {
final.RegisterNumber(asm.MOVE, x64.SyscallRegisters[1], 0)
final.Syscall()
// Place the `main` function immediately after the entry point.
main := r.Functions["main"]
delete(r.Functions, "main")
final.Merge(&main.Assembler)
// Merge all the remaining functions.
for _, f := range r.Functions {
// Merge all the called functions.
for _, f := range r.Used {
final.Merge(&f.Assembler)
}