Skip to content

Commit

Permalink
Remove redundant instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
terminationshock committed Feb 18, 2023
1 parent b8825be commit 03d8f86
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/assembly.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,23 @@ func Assembly(code []*Command, file string, stackSize int, verbose bool) (string
break
default:
if c.MultiplyLoop != nil {
prevFactor := 0
for _, m := range c.MultiplyLoop {
if m.Factor > 1 || m.Factor < -1 {
program += fmt.Sprintf("imulq $%d, (%%r12), %%rax", abs(m.Factor)) + br
} else {
program += "movq (%r12), %rax" + br
if m.Factor != prevFactor {
if m.Factor > 1 || m.Factor < -1 {
program += fmt.Sprintf("imulq $%d, (%%r12), %%rax", abs(m.Factor)) + br
} else {
program += "movq (%r12), %rax" + br
}
inst++
}
prevFactor = m.Factor
if m.Factor > 0 {
program += fmt.Sprintf("addq %%rax, %d(%%r12)", -8 * m.Offset) + br
} else {
program += fmt.Sprintf("subq %%rax, %d(%%r12)", -8 * m.Offset) + br
}
inst += 2
inst++
}
if c.Offset != 0 {
program += fmt.Sprintf("movq $0, %d(%%r12)", -8 * c.Offset) + br
Expand All @@ -154,7 +159,9 @@ func Assembly(code []*Command, file string, stackSize int, verbose bool) (string
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 verbose {
fmt.Printf("%d instructions, %d loops\n", inst, loopId + 1)
}

if stackSize % 2 > 0 {
stackSize++
Expand Down

0 comments on commit 03d8f86

Please sign in to comment.