Skip to content

Commit a56a560

Browse files
Instruction counter
1 parent e6d6fc0 commit a56a560

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

compiler/main.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@ type Options struct {
2020
LogIR bool
2121
}
2222

23+
func countInstructionTotal(commands []AssemblyCommand) int {
24+
total := 0
25+
for _, cmd := range commands {
26+
if cmd.Type == Instruction && cmd.Name != "" {
27+
total++
28+
}
29+
}
30+
return total
31+
}
32+
2333
func Compile(executable *os.File, options Options) []byte {
2434
/* prepare */
25-
var writer = &OutputWriter{
26-
Buffer: []byte(""),
27-
MemoryDevelopmentPointer: 0,
28-
MaxPC: 1,
29-
Options: options,
30-
MemoryMap: make(map[string]int),
31-
}
35+
writer := newOutputWriter(options)
3236

3337
elf, err := elf.NewFile(executable)
3438
if err != nil {
@@ -46,11 +50,18 @@ func Compile(executable *os.File, options Options) []byte {
4650
writer.Commands = ParseFromElf(elf)
4751
}
4852

53+
writer.InstructionTotal = countInstructionTotal(writer.Commands)
54+
4955
/* compilation */
5056
BeforeCompilation(writer)
5157
for _, command := range writer.Commands {
5258
CompileInstruction(writer, command)
59+
if command.Type == Instruction && command.Name != "" {
60+
writer.InstructionProcessed++
61+
}
62+
writer.updateProgress()
5363
}
64+
writer.finishProgress()
5465
if writer.Options.LogIR {
5566
dumpIRAsJSON(writer)
5667
}

compiler/writer.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ type OutputWriter struct {
2929
MemoryMap map[string]int /* map static data keys to addresses */
3030
Options Options /* user specified options */
3131
InlineCounter int
32+
InstructionTotal int
33+
InstructionProcessed int
34+
}
35+
36+
func newOutputWriter(options Options) *OutputWriter {
37+
return &OutputWriter{
38+
Buffer: []byte(""),
39+
MemoryDevelopmentPointer: 0,
40+
MaxPC: 1,
41+
Options: options,
42+
MemoryMap: make(map[string]int),
43+
}
3244
}
3345

3446
func (w *OutputWriter) nextInlineTemp() string {
@@ -44,3 +56,17 @@ func WriteIndentedString(writer *OutputWriter, format string, args ...any) {
4456
indent := strings.Repeat("\t", writer.Depth)
4557
writer.Buffer = append(writer.Buffer, indent+fmt.Sprintf(format, args...)...)
4658
}
59+
60+
func (w *OutputWriter) updateProgress() {
61+
if w.InstructionTotal == 0 {
62+
return
63+
}
64+
fmt.Printf("\rInstruction %d/%d", w.InstructionProcessed, w.InstructionTotal)
65+
}
66+
67+
func (w *OutputWriter) finishProgress() {
68+
if w.InstructionTotal == 0 {
69+
return
70+
}
71+
fmt.Print("\n")
72+
}

0 commit comments

Comments
 (0)