71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
package core
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
|
|
"git.akyoto.dev/cli/q/src/build/asm"
|
|
"git.akyoto.dev/cli/q/src/build/cpu"
|
|
"git.akyoto.dev/go/color/ansi"
|
|
)
|
|
|
|
// 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
|
|
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.
|
|
type counter struct {
|
|
loop int
|
|
branch int
|
|
subBranch int
|
|
}
|
|
|
|
// PrintInstructions shows the assembly instructions.
|
|
func (s *state) PrintInstructions() {
|
|
ansi.Dim.Println("╭──────────────────────────────────────────────────────────────────────────────╮")
|
|
|
|
for i, x := range s.assembler.Instructions {
|
|
ansi.Dim.Print("│ ")
|
|
|
|
switch x.Mnemonic {
|
|
case asm.LABEL:
|
|
ansi.Yellow.Printf("%-44s", x.Data.String()+":")
|
|
|
|
case asm.COMMENT:
|
|
ansi.Dim.Printf("%-44s", x.Data.String())
|
|
|
|
default:
|
|
ansi.Green.Printf("%-12s", x.Mnemonic.String())
|
|
|
|
if x.Data != nil {
|
|
fmt.Printf("%-32s", x.Data.String())
|
|
} else {
|
|
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("╰──────────────────────────────────────────────────────────────────────────────╯")
|
|
}
|