Improved code generation

This commit is contained in:
Eduard Urbach 2024-07-09 17:00:04 +02:00
parent 6e22febc01
commit 3c189a025f
Signed by: eduard
GPG key ID: 49226B848C78F6C8
23 changed files with 201 additions and 95 deletions

View file

@ -1,6 +1,7 @@
package core
import (
"bytes"
"fmt"
"git.akyoto.dev/cli/q/src/build/asm"
@ -10,13 +11,14 @@ import (
// state is the data structure we embed in each function to preserve compilation state.
type state struct {
err error
variables map[string]*Variable
functions map[string]*Function
finished chan struct{}
assembler asm.Assembler
cpu cpu.CPU
count counter
err error
variables map[string]*Variable
functions map[string]*Function
registerHistory []uint64
finished chan struct{}
assembler asm.Assembler
cpu cpu.CPU
count counter
}
// counter stores how often a certain statement appeared so we can generate a unique label from it.
@ -28,30 +30,42 @@ type counter struct {
// PrintInstructions shows the assembly instructions.
func (s *state) PrintInstructions() {
ansi.Dim.Println("╭────────────────────────────────────────────────────╮")
ansi.Dim.Println("╭──────────────────────────────────────────────────────────────────────────────╮")
for _, x := range s.assembler.Instructions {
for i, x := range s.assembler.Instructions {
ansi.Dim.Print("│ ")
switch x.Mnemonic {
case asm.LABEL:
ansi.Yellow.Printf("%-50s", x.Data.String()+":")
ansi.Yellow.Printf("%-44s", x.Data.String()+":")
case asm.COMMENT:
ansi.Dim.Printf("%-50s", x.Data.String())
ansi.Dim.Printf("%-44s", x.Data.String())
default:
ansi.Green.Printf("%-12s", x.Mnemonic.String())
if x.Data != nil {
fmt.Printf("%-38s", x.Data.String())
fmt.Printf("%-32s", x.Data.String())
} else {
fmt.Printf("%-38s", "")
fmt.Printf("%-32s", "")
}
}
registers := bytes.Buffer{}
used := s.registerHistory[i]
for _, reg := range s.cpu.All {
if used&(1<<reg) != 0 {
registers.WriteString("⬤ ")
} else {
registers.WriteString("◯ ")
}
}
ansi.Dim.Print(registers.String())
ansi.Dim.Print(" │\n")
}
ansi.Dim.Println("╰────────────────────────────────────────────────────╯")
ansi.Dim.Println("╰──────────────────────────────────────────────────────────────────────────────╯")
}