OPTIMIZER IS WORKING
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from typing import List, Dict, NamedTuple, Union
|
||||
import re
|
||||
from console import console
|
||||
|
||||
|
||||
class Instruction(NamedTuple):
|
||||
@@ -11,7 +12,7 @@ class PeepholeRule(NamedTuple):
|
||||
replace: List[Instruction]
|
||||
|
||||
class Optimizer:
|
||||
def __init__(self, lines: list[str], window_size: int = 5):
|
||||
def __init__(self, lines: list[str], window_size: int = 6):
|
||||
self.lines = lines
|
||||
self.final_str = ""
|
||||
|
||||
@@ -20,6 +21,7 @@ class Optimizer:
|
||||
self.rules: list[PeepholeRule] = []
|
||||
|
||||
def match_instruction(self, pattern: Instruction, instr: Instruction, bindings: Dict[str, str]) -> bool:
|
||||
#print(pattern.opcode == instr.opcode, instr.opcode, instr.operands, pattern.opcode)
|
||||
if pattern.opcode != instr.opcode:
|
||||
return False
|
||||
if len(pattern.operands) != len(instr.operands):
|
||||
@@ -59,6 +61,17 @@ class Optimizer:
|
||||
operands = [bindings.get(op, op) for op in pattern.operands]
|
||||
result.append(Instruction(pattern.opcode, operands))
|
||||
return result
|
||||
|
||||
def optimize_until_stable(self):
|
||||
while True:
|
||||
passes = 1
|
||||
old_instructions = self.lines
|
||||
new_instructions = self.peephole()
|
||||
if old_instructions == new_instructions:
|
||||
print(f"Optimized in {passes} passes.")
|
||||
return new_instructions
|
||||
old_instructions = new_instructions
|
||||
passes += 1
|
||||
|
||||
def peephole(self):
|
||||
self.lines = [self.parse_instruction(line) for line in self.lines]
|
||||
|
@@ -17,6 +17,20 @@ class X86_64Optimizer(Optimizer):
|
||||
Instruction("jg", ["y"])
|
||||
]
|
||||
),
|
||||
PeepholeRule(
|
||||
match=[
|
||||
Instruction("setg",["al"]),
|
||||
Instruction("movzx", ["rax", "al"]),
|
||||
Instruction("mov", ["z", "rax"]),
|
||||
Instruction("mov", ["eax", "x"]),
|
||||
Instruction("test", ["eax", "eax"]),
|
||||
Instruction("jnz", ["y"])
|
||||
],
|
||||
replace=[
|
||||
Instruction("jg", ["y"])
|
||||
]
|
||||
),
|
||||
|
||||
PeepholeRule(
|
||||
match=[
|
||||
Instruction("setl",["al"]),
|
||||
@@ -30,6 +44,20 @@ class X86_64Optimizer(Optimizer):
|
||||
Instruction("jl", ["y"])
|
||||
]
|
||||
),
|
||||
PeepholeRule(
|
||||
match=[
|
||||
Instruction("setl",["al"]),
|
||||
Instruction("movzx", ["rax", "al"]),
|
||||
Instruction("mov", ["z", "rax"]),
|
||||
Instruction("mov", ["eax", "x"]),
|
||||
Instruction("test", ["eax", "eax"]),
|
||||
Instruction("jnz", ["y"])
|
||||
],
|
||||
replace=[
|
||||
Instruction("jl", ["y"])
|
||||
]
|
||||
),
|
||||
|
||||
PeepholeRule(
|
||||
match=[
|
||||
Instruction("sete",["al"]),
|
||||
@@ -43,6 +71,20 @@ class X86_64Optimizer(Optimizer):
|
||||
Instruction("je", ["y"])
|
||||
]
|
||||
),
|
||||
PeepholeRule(
|
||||
match=[
|
||||
Instruction("sete",["al"]),
|
||||
Instruction("movzx", ["rax", "al"]),
|
||||
Instruction("mov", ["z", "rax"]),
|
||||
Instruction("mov", ["eax", "x"]),
|
||||
Instruction("test", ["eax", "eax"]),
|
||||
Instruction("jnz", ["y"])
|
||||
],
|
||||
replace=[
|
||||
Instruction("je", ["y"])
|
||||
]
|
||||
),
|
||||
|
||||
PeepholeRule(
|
||||
match=[
|
||||
Instruction("setne",["al"]),
|
||||
@@ -56,4 +98,27 @@ class X86_64Optimizer(Optimizer):
|
||||
Instruction("jne", ["y"])
|
||||
]
|
||||
),
|
||||
PeepholeRule(
|
||||
match=[
|
||||
Instruction("setne",["al"]),
|
||||
Instruction("movzx", ["rax", "al"]),
|
||||
Instruction("mov", ["z", "rax"]),
|
||||
Instruction("mov", ["eax", "x"]),
|
||||
Instruction("test", ["eax", "eax"]),
|
||||
Instruction("jnz", ["y"])
|
||||
],
|
||||
replace=[
|
||||
Instruction("jne", ["y"])
|
||||
]
|
||||
),
|
||||
|
||||
PeepholeRule(
|
||||
match=[
|
||||
Instruction("mov", ["rax", "x"]),
|
||||
Instruction("push", ["rax"])
|
||||
],
|
||||
replace=[
|
||||
Instruction("push", ["x"])
|
||||
]
|
||||
)
|
||||
]
|
Reference in New Issue
Block a user