doing some set up for when i implement a peephole optimizer

This commit is contained in:
SpookyDervish
2025-09-08 07:49:33 +10:00
parent ae1b9aa43d
commit f78d0e41b0
8 changed files with 38 additions and 46 deletions

View File

@@ -45,4 +45,4 @@ class Generator:
def write(self): def write(self):
with open(self.output_path + ".asm", "w") as f: with open(self.output_path + ".asm", "w") as f:
f.writelines(self.lines) f.write(self.lines)

View File

@@ -1,6 +1,7 @@
from generators.generator import Generator from generators.generator import Generator
from ground_ast import * from ground_ast import *
from error import traceback from error import traceback
from optimizers.x86_64 import X86_64Optimizer
class X86_64Generator(Generator): class X86_64Generator(Generator):
def __init__(self, ast, code, output_path): def __init__(self, ast, code, output_path):
@@ -426,4 +427,6 @@ class X86_64Generator(Generator):
f.write(f"dq {float(value)}") f.write(f"dq {float(value)}")
f.write("\n") f.write("\n")
f.write("section .text\n") f.write("section .text\n")
f.writelines(self.lines)
optimizer = X86_64Optimizer(self.lines)
f.write(optimizer.peephole())

11
optimizer.py Normal file
View File

@@ -0,0 +1,11 @@
class Optimizer:
def __init__(self, lines: list[str]):
self.lines = lines
self.final_str = ""
# sliding window pos
self.pos = 0
def peephole(self):
pass

11
optimizers/x86_64.py Normal file
View File

@@ -0,0 +1,11 @@
from optimizer import Optimizer
class X86_64Optimizer(Optimizer):
def peephole(self):
lines = []
for line in self.lines:
lines.append(line)
self.final_str = ''.join(lines)
return self.final_str

BIN
out

Binary file not shown.

12
out.asm
View File

@@ -6,22 +6,22 @@ global _start
_start: _start:
mov rax, 0 mov rax, 0
push rax push rax
push 1 mov rax, 0
push rax
.loop: .loop:
mov rax, 1 mov rax, [rsp + 8]
mov rbx, [rsp + 8] add rax, 1
add rax, rbx
mov QWORD [rsp + 8], rax mov QWORD [rsp + 8], rax
mov rax, 100000000 mov rax, 100000000
mov rbx, [rsp + 8] mov rbx, [rsp + 8]
cmp rax, rbx cmp rax, rbx
setne al setg al
movzx rax, al movzx rax, al
mov QWORD [rsp + 0], rax mov QWORD [rsp + 0], rax
mov eax, [rsp + 0] mov eax, [rsp + 0]
test eax, eax test eax, eax
jnz .loop jnz .loop
mov rax, 60 mov rax, 60
mov rdi, [rsp + 8] mov rdi, [rsp + 0]
syscall syscall

33
test.o
View File

@@ -1,33 +0,0 @@
.file "test.c"
.text
.section .text.startup,"ax",@progbits
.p2align 4
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
endbr64
movl $100000000, %eax
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
.section .note.GNU-stack,"",@progbits
.section .note.gnu.property,"a"
.align 8
.long 1f - 0f
.long 4f - 1f
.long 5
0:
.string "GNU"
1:
.align 8
.long 0xc0000002
.long 3f - 2f
2:
.long 0x3
3:
.align 8
4:

View File

@@ -1,7 +1,7 @@
set &var 0 set &x 0
set &cond true set &cond 0
@loop @loop
add 1 $var &var add $x 1 &x
inequal 100000000 $var &cond greater 100000000 $x &cond
if $cond %loop if $cond %loop
end $var end $cond