woops, optimization

This commit is contained in:
SpookyDervish
2025-09-07 14:08:45 +10:00
parent 47e3e503b8
commit 35ee183768
5 changed files with 51 additions and 21 deletions

View File

@@ -26,7 +26,7 @@ class Generator:
pass pass
def generate_node(self, node): def generate_node(self, node):
self.lines.append(f"; {node}\n\t") #self.lines.append(f"; {node}\n\t")
node_type = str(type(node))[19:-2] node_type = str(type(node))[19:-2]
if not hasattr(self, f"generate_{node_type}"): if not hasattr(self, f"generate_{node_type}"):
raise NotImplementedError(f"Generator has no generate method for {node_type}.") raise NotImplementedError(f"Generator has no generate method for {node_type}.")

BIN
out

Binary file not shown.

19
out.asm
View File

@@ -4,38 +4,23 @@ section .data
section .text section .text
global _start global _start
_start: _start:
; InstructionNode(instruction='set', parent=RootNode(statements=[..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Boolean]), LabelDecleration, InstructionNode(instruction='add', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='add', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='inequal', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='if', parent=..., arguments=[VariableReference, LabelReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Int])
mov rax, 0 mov rax, 0
push rax push rax
; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), ..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Boolean]), LabelDecleration, InstructionNode(instruction='add', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='add', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='inequal', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='if', parent=..., arguments=[VariableReference, LabelReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Int])
mov rax, 0
push rax
; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), ..., LabelDecleration, InstructionNode(instruction='add', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='add', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='inequal', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='if', parent=..., arguments=[VariableReference, LabelReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Boolean])
push 1 push 1
; LabelDecleration
.loop: .loop:
; InstructionNode(instruction='add', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Boolean]), LabelDecleration, ..., InstructionNode(instruction='add', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='inequal', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='if', parent=..., arguments=[VariableReference, LabelReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[Int, VariableReference, VariablePointer])
mov rax, 1
mov rbx, [rsp + 16]
add rax, rbx
mov QWORD [rsp + 16], rax
; InstructionNode(instruction='add', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Boolean]), LabelDecleration, InstructionNode(instruction='add', parent=..., arguments=[Int, VariableReference, VariablePointer]), ..., InstructionNode(instruction='inequal', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='if', parent=..., arguments=[VariableReference, LabelReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[Int, VariableReference, VariablePointer])
mov rax, 1 mov rax, 1
mov rbx, [rsp + 8] mov rbx, [rsp + 8]
add rax, rbx add rax, rbx
mov QWORD [rsp + 8], rax mov QWORD [rsp + 8], rax
; InstructionNode(instruction='inequal', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Boolean]), LabelDecleration, InstructionNode(instruction='add', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='add', parent=..., arguments=[Int, VariableReference, VariablePointer]), ..., InstructionNode(instruction='if', parent=..., arguments=[VariableReference, LabelReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[Int, VariableReference, VariablePointer]) mov rax, 100000000
mov rax, 100000 mov rbx, [rsp + 8]
mov rbx, [rsp + 16]
cmp rax, rbx cmp rax, rbx
setne al setne al
movzx rax, al movzx rax, al
mov QWORD [rsp + 0], rax mov QWORD [rsp + 0], rax
; InstructionNode(instruction='if', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Boolean]), LabelDecleration, InstructionNode(instruction='add', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='add', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='inequal', parent=..., arguments=[Int, VariableReference, VariablePointer]), ..., InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariableReference, LabelReference])
mov eax, [rsp + 0] mov eax, [rsp + 0]
test eax, eax test eax, eax
jnz .loop jnz .loop
; InstructionNode(instruction='end', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Boolean]), LabelDecleration, InstructionNode(instruction='add', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='add', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='inequal', parent=..., arguments=[Int, VariableReference, VariablePointer]), InstructionNode(instruction='if', parent=..., arguments=[VariableReference, LabelReference]), ...]), arguments=[VariableReference])
mov rax, 60 mov rax, 60
mov rdi, [rsp + 8] mov rdi, [rsp + 8]
syscall syscall

47
test.s Normal file
View File

@@ -0,0 +1,47 @@
.file "test.c"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $0, -8(%rbp)
movl $0, -4(%rbp)
jmp .L2
.L3:
addl $1, -8(%rbp)
addl $1, -4(%rbp)
.L2:
cmpl $100000000, -4(%rbp)
jne .L3
movl -8(%rbp), %eax
popq %rbp
.cfi_def_cfa 7, 8
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,9 +1,7 @@
set &var 0 set &var 0
set &lol 0
set &cond true set &cond true
@loop @loop
add 1 $var &var add 1 $var &var
add 1 $lol &lol
inequal 100000000 $var &cond inequal 100000000 $var &cond
if $cond %loop if $cond %loop
end $lol end $var