OPTIMIZER IS WORKING
This commit is contained in:
@@ -429,4 +429,4 @@ class X86_64Generator(Generator):
|
|||||||
f.write("section .text\n")
|
f.write("section .text\n")
|
||||||
|
|
||||||
optimizer = X86_64Optimizer(self.lines)
|
optimizer = X86_64Optimizer(self.lines)
|
||||||
f.writelines(optimizer.peephole())
|
f.writelines(optimizer.optimize_until_stable())
|
@@ -1,5 +1,6 @@
|
|||||||
from typing import List, Dict, NamedTuple, Union
|
from typing import List, Dict, NamedTuple, Union
|
||||||
import re
|
import re
|
||||||
|
from console import console
|
||||||
|
|
||||||
|
|
||||||
class Instruction(NamedTuple):
|
class Instruction(NamedTuple):
|
||||||
@@ -11,7 +12,7 @@ class PeepholeRule(NamedTuple):
|
|||||||
replace: List[Instruction]
|
replace: List[Instruction]
|
||||||
|
|
||||||
class Optimizer:
|
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.lines = lines
|
||||||
self.final_str = ""
|
self.final_str = ""
|
||||||
|
|
||||||
@@ -20,6 +21,7 @@ class Optimizer:
|
|||||||
self.rules: list[PeepholeRule] = []
|
self.rules: list[PeepholeRule] = []
|
||||||
|
|
||||||
def match_instruction(self, pattern: Instruction, instr: Instruction, bindings: Dict[str, str]) -> bool:
|
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:
|
if pattern.opcode != instr.opcode:
|
||||||
return False
|
return False
|
||||||
if len(pattern.operands) != len(instr.operands):
|
if len(pattern.operands) != len(instr.operands):
|
||||||
@@ -60,6 +62,17 @@ class Optimizer:
|
|||||||
result.append(Instruction(pattern.opcode, operands))
|
result.append(Instruction(pattern.opcode, operands))
|
||||||
return result
|
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):
|
def peephole(self):
|
||||||
self.lines = [self.parse_instruction(line) for line in self.lines]
|
self.lines = [self.parse_instruction(line) for line in self.lines]
|
||||||
|
|
||||||
|
@@ -17,6 +17,20 @@ class X86_64Optimizer(Optimizer):
|
|||||||
Instruction("jg", ["y"])
|
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(
|
PeepholeRule(
|
||||||
match=[
|
match=[
|
||||||
Instruction("setl",["al"]),
|
Instruction("setl",["al"]),
|
||||||
@@ -30,6 +44,20 @@ class X86_64Optimizer(Optimizer):
|
|||||||
Instruction("jl", ["y"])
|
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(
|
PeepholeRule(
|
||||||
match=[
|
match=[
|
||||||
Instruction("sete",["al"]),
|
Instruction("sete",["al"]),
|
||||||
@@ -43,6 +71,20 @@ class X86_64Optimizer(Optimizer):
|
|||||||
Instruction("je", ["y"])
|
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(
|
PeepholeRule(
|
||||||
match=[
|
match=[
|
||||||
Instruction("setne",["al"]),
|
Instruction("setne",["al"]),
|
||||||
@@ -56,4 +98,27 @@ class X86_64Optimizer(Optimizer):
|
|||||||
Instruction("jne", ["y"])
|
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"])
|
||||||
|
]
|
||||||
|
)
|
||||||
]
|
]
|
33
out.asm
33
out.asm
@@ -1,34 +1,19 @@
|
|||||||
; ~~~ Auto generated by the GroundPY compiler for Linux x86_64 targets. ~~~
|
; ~~~ Auto generated by the GroundPY compiler for Linux x86_64 targets. ~~~
|
||||||
|
|
||||||
section .data
|
section .data
|
||||||
.LC0: db "yes", 10, 0
|
|
||||||
.LC1: equ $ - .LC0
|
|
||||||
.LC2: db "no", 10, 0
|
|
||||||
.LC3: equ $ - .LC2
|
|
||||||
section .text
|
section .text
|
||||||
global _start
|
global _start
|
||||||
_start:
|
_start:
|
||||||
mov rax, 123
|
push 0
|
||||||
push rax
|
push 1
|
||||||
|
.loop:
|
||||||
|
mov rax, [rsp + 8]
|
||||||
|
add rax, 1
|
||||||
|
mov QWORD [rsp + 8], rax
|
||||||
mov rax, 100000000
|
mov rax, 100000000
|
||||||
mov rbx, [rsp + 0]
|
mov rbx, [rsp + 8]
|
||||||
cmp rax, rbx
|
cmp rax, rbx
|
||||||
jg .yes
|
jg .loop
|
||||||
.yes:
|
|
||||||
mov rsi, .LC0
|
|
||||||
mov rdx, .LC1
|
|
||||||
mov rax, 1
|
|
||||||
mov rdi, 1
|
|
||||||
syscall
|
|
||||||
mov rax, 60
|
mov rax, 60
|
||||||
mov rdi, 0
|
mov rdi, [rsp + 8]
|
||||||
syscall
|
|
||||||
.no:
|
|
||||||
mov rsi, .LC2
|
|
||||||
mov rdx, .LC3
|
|
||||||
mov rax, 1
|
|
||||||
mov rdi, 1
|
|
||||||
syscall
|
|
||||||
mov rax, 60
|
|
||||||
mov rdi, 0
|
|
||||||
syscall
|
syscall
|
||||||
|
16
test2.grnd
16
test2.grnd
@@ -1,9 +1,7 @@
|
|||||||
set &x 123
|
set &var 0
|
||||||
greater 100000000 $x &cond
|
set &cond true
|
||||||
if $cond %yes
|
@loop
|
||||||
@yes
|
add $var 1 &var
|
||||||
stdout "yes\n"
|
greater 100000000 $var &cond
|
||||||
end 0
|
if $cond %loop
|
||||||
@no
|
end $var
|
||||||
stdout "no\n"
|
|
||||||
end 0
|
|
Reference in New Issue
Block a user