Skip to content

Commit

Permalink
Print number of assembly instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
terminationshock committed Feb 18, 2023
1 parent 24844dc commit b8825be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/assembly.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Loop struct {
Col int
}

func Assembly(code []*Command, file string, stackSize int) (string, error) {
func Assembly(code []*Command, file string, stackSize int, verbose bool) (string, error) {
if stackSize < 1 {
return "", errors.New("Invalid stack size")
}
Expand All @@ -45,29 +45,34 @@ func Assembly(code []*Command, file string, stackSize int) (string, error) {
loops := []*Loop{}

program := ""
inst := 13

for _, c := range code {
program += fmt.Sprintf("# %s at (%d,%d)", strings.Repeat(c.String, c.Count), c.Row, c.Col) + br
switch c.String {
case ">":
program += fmt.Sprintf("subq $%d, %%r12", 8 * c.Count) + br
inst++
break
case "<":
program += fmt.Sprintf("addq $%d, %%r12", 8 * c.Count) + br
inst++
break
case "+":
if c.Offset != 0 {
program += fmt.Sprintf("addq $%d, %d(%%r12)", c.Count, -8 * c.Offset) + br
} else {
program += fmt.Sprintf("addq $%d, (%%r12)", c.Count) + br
}
inst++
break
case "-":
if c.Offset != 0 {
program += fmt.Sprintf("subq $%d, %d(%%r12)", c.Count, -8 * c.Offset) + br
} else {
program += fmt.Sprintf("subq $%d, (%%r12)", c.Count) + br
}
inst++
break
case ".":
if c.Offset != 0 {
Expand All @@ -76,6 +81,7 @@ func Assembly(code []*Command, file string, stackSize int) (string, error) {
program += "movq (%r12), %rdi" + br
}
program += "call putchar" + br
inst += 2
break
case ",":
program += "call getchar" + br
Expand All @@ -84,6 +90,7 @@ func Assembly(code []*Command, file string, stackSize int) (string, error) {
} else {
program += "movq %rax, (%r12)" + br
}
inst += 2
break
case "[":
loopId++
Expand All @@ -95,6 +102,7 @@ func Assembly(code []*Command, file string, stackSize int) (string, error) {
program += fmt.Sprintf(".loop%d:", loopId) + br
program += "cmpq $0, (%r12)" + br
program += "je " + fmt.Sprintf(".break%d", loopId) + br
inst += 2
break
case "]":
n := len(loops) - 1
Expand All @@ -113,6 +121,7 @@ func Assembly(code []*Command, file string, stackSize int) (string, error) {
program = strings.Replace(program, fmt.Sprintf(".break%d", loops[n].Number), mergedBreak, -1)
}
loops = loops[:n]
inst++
break
default:
if c.MultiplyLoop != nil {
Expand All @@ -127,12 +136,14 @@ func Assembly(code []*Command, file string, stackSize int) (string, error) {
} else {
program += fmt.Sprintf("subq %%rax, %d(%%r12)", -8 * m.Offset) + br
}
inst += 2
}
if c.Offset != 0 {
program += fmt.Sprintf("movq $0, %d(%%r12)", -8 * c.Offset) + br
} else {
program += "movq $0, (%r12)" + br
}
inst++
}
break
}
Expand All @@ -143,6 +154,8 @@ func Assembly(code []*Command, file string, stackSize int) (string, error) {
return "", errors.New(fmt.Sprintf("No matching loop end at %s:%d:%d", file, loops[n].Row, loops[n].Col))
}

fmt.Printf("%d instructions created\n", inst)

if stackSize % 2 > 0 {
stackSize++
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
code = Optimize(code, cli.Verbose)
}

assembly, err := Assembly(code, cli.File, cli.StackSize)
assembly, err := Assembly(code, cli.File, cli.StackSize, cli.Verbose)
ctx.FatalIfErrorf(err)

err = CompileAndLink(assembly, cli.Output, cli.Assembly, cli.Verbose)
Expand Down

0 comments on commit b8825be

Please sign in to comment.