doing some set up for when i implement a peephole optimizer
This commit is contained in:
@@ -45,4 +45,4 @@ class Generator:
|
||||
|
||||
def write(self):
|
||||
with open(self.output_path + ".asm", "w") as f:
|
||||
f.writelines(self.lines)
|
||||
f.write(self.lines)
|
@@ -1,6 +1,7 @@
|
||||
from generators.generator import Generator
|
||||
from ground_ast import *
|
||||
from error import traceback
|
||||
from optimizers.x86_64 import X86_64Optimizer
|
||||
|
||||
class X86_64Generator(Generator):
|
||||
def __init__(self, ast, code, output_path):
|
||||
@@ -426,4 +427,6 @@ class X86_64Generator(Generator):
|
||||
f.write(f"dq {float(value)}")
|
||||
f.write("\n")
|
||||
f.write("section .text\n")
|
||||
f.writelines(self.lines)
|
||||
|
||||
optimizer = X86_64Optimizer(self.lines)
|
||||
f.write(optimizer.peephole())
|
11
optimizer.py
Normal file
11
optimizer.py
Normal 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
11
optimizers/x86_64.py
Normal 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
|
12
out.asm
12
out.asm
@@ -6,22 +6,22 @@ global _start
|
||||
_start:
|
||||
mov rax, 0
|
||||
push rax
|
||||
push 1
|
||||
mov rax, 0
|
||||
push rax
|
||||
.loop:
|
||||
mov rax, 1
|
||||
mov rbx, [rsp + 8]
|
||||
add rax, rbx
|
||||
mov rax, [rsp + 8]
|
||||
add rax, 1
|
||||
mov QWORD [rsp + 8], rax
|
||||
mov rax, 100000000
|
||||
mov rbx, [rsp + 8]
|
||||
cmp rax, rbx
|
||||
setne al
|
||||
setg al
|
||||
movzx rax, al
|
||||
mov QWORD [rsp + 0], rax
|
||||
mov eax, [rsp + 0]
|
||||
test eax, eax
|
||||
jnz .loop
|
||||
mov rax, 60
|
||||
mov rdi, [rsp + 8]
|
||||
mov rdi, [rsp + 0]
|
||||
syscall
|
||||
|
33
test.o
33
test.o
@@ -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:
|
10
test2.grnd
10
test2.grnd
@@ -1,7 +1,7 @@
|
||||
set &var 0
|
||||
set &cond true
|
||||
set &x 0
|
||||
set &cond 0
|
||||
@loop
|
||||
add 1 $var &var
|
||||
inequal 100000000 $var &cond
|
||||
add $x 1 &x
|
||||
greater 100000000 $x &cond
|
||||
if $cond %loop
|
||||
end $var
|
||||
end $cond
|
Reference in New Issue
Block a user