diff --git a/04_semantics_and_running/main.py b/04_semantics_and_running/main.py index 9a935fb..2b41778 100644 --- a/04_semantics_and_running/main.py +++ b/04_semantics_and_running/main.py @@ -341,6 +341,7 @@ class CompileData: continue instructions.pop(i) changed = True + if changed: continue i = 0 # Optimize movq to register followed by pushq register @@ -355,6 +356,7 @@ class CompileData: instructions.pop(i + 1) i -= 1 changed = True + if changed: continue i = 0 # Optimize movq to rax followed by movq from rax @@ -375,6 +377,34 @@ class CompileData: instructions.pop(i + 1) i -= 1 changed = True + if changed: continue + + i = 0 + # Optimize addq/subq for immediate 1 + while i < len(instructions): + if instructions[i].opcode not in ['addq', 'subq']: + i += 1 + continue + if instructions[i].operands[0] != '$1': + i += 1 + continue + new_opcode = 'incq' if instructions[i].opcode == 'addq' else 'decq' + instructions[i] = Instruction(new_opcode, [instructions[i].operands[1]]) + changed = True + if changed: continue + + i = 0 + # Optimize zeroing of register + while i < len(instructions): + if instructions[i].opcode != 'movq': + i += 1 + continue + if instructions[i].operands[0] != '$0' or instructions[i].operands[1][0] != '%': + i += 1 + continue + instructions[i] = Instruction('xorq', [instructions[i].operands[1], instructions[i].operands[1]]) + changed = True + if changed: continue def add_builtin_functions(self) -> None: today = []