From 2ce71e7abb96429c5950c8d8c011d2e5bc21b277 Mon Sep 17 00:00:00 2001 From: SpookyDervish <78246495+SpookyDervish@users.noreply.github.com> Date: Wed, 10 Sep 2025 20:45:22 +1000 Subject: [PATCH] THE ADD FUNCTION COMPILES!!!!! --- generators/x86_64.py | 95 +++++++++++++++++++++++++++++++------------ optimizers/x86_64.py | 8 ++++ out | Bin 8928 -> 4712 bytes out.asm | 20 ++++----- test2.grnd | 11 ++--- 5 files changed, 90 insertions(+), 44 deletions(-) diff --git a/generators/x86_64.py b/generators/x86_64.py index a817fa8..8d2d069 100644 --- a/generators/x86_64.py +++ b/generators/x86_64.py @@ -132,39 +132,82 @@ class X86_64Generator(Generator): scope = scope or self.current_var_scope var_pos = self.get_var_pos(var_name, scope) - if type(new_value) == IntNode: # we're changing a variable to a number - lines.append(f"mov QWORD [rsp + {var_pos}], {new_value.value}\n\t") - scope.table[var_name]["type"] = IntNode + if isinstance(var_pos, str): + if type(new_value) == IntNode: # we're changing a variable to a number + #lines.append(f"mov QWORD [rsp + {var_pos}], {new_value.value}\n\t") + lines.append(f"mov {var_pos}, {new_value.value}\n\t") + scope.table[var_name]["type"] = IntNode - elif type(new_value) == VarRefNode: # we're changing a variable to the value of another variable - var_type = self.get_variable(lines, new_value.var_name, "rax") - lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t") - scope.table[var_name]["type"] = var_type + elif type(new_value) == VarRefNode: # we're changing a variable to the value of another variable + var_type = self.get_variable(lines, new_value.var_name, "rax") + #lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t") + lines.append(f"mov {var_pos}, rax\n\t") + scope.table[var_name]["type"] = var_type - elif type(new_value) == StringNode: # we're changing a variable to a string - lines.append(f"mov QWORD [rsp + {var_pos}], 0\n\t") - + elif type(new_value) == StringNode: # we're changing a variable to a string + lines.append(f"mov QWORD [rsp + {var_pos}], 0\n\t") + - string_pointer = self.add_constant(new_value.value) - string_len = self.add_constant(f"equ $ - {string_pointer[1:-1]}", no_string=True) - #lines.append(f"lea QWORD [rsp + {var_pos}], {string_pointer}\n\t") - ##lines.append(f"mov QWORD [rsp + {var_pos + 8}], {string_len[1:-1]}\n\t") + string_pointer = self.add_constant(new_value.value) + string_len = self.add_constant(f"equ $ - {string_pointer[1:-1]}", no_string=True) + #lines.append(f"lea QWORD [rsp + {var_pos}], {string_pointer}\n\t") + ##lines.append(f"mov QWORD [rsp + {var_pos + 8}], {string_len[1:-1]}\n\t") - scope.table[var_name]["stack_loc"] = self.stack_size - lines.append(f"lea rax, {string_pointer}\n\t") - self.push("rax", lines) - lines.append(f"mov rax, {string_len[1:-1]}\n\t") - self.push("rax", lines) + scope.table[var_name]["stack_loc"] = self.stack_size + lines.append(f"lea rax, {string_pointer}\n\t") + self.push("rax", lines) + lines.append(f"mov rax, {string_len[1:-1]}\n\t") + self.push("rax", lines) - scope.table[var_name]["type"] = StringNode + scope.table[var_name]["type"] = StringNode - elif type(new_value) == BoolNode: - lines.append(f"mov QWORD [rsp + {var_pos}], {'1' if new_value.value else '0'}\n\t") - scope.table[var_name]["type"] = BoolNode + elif type(new_value) == BoolNode: + #lines.append(f"mov QWORD [rsp + {var_pos}], {'1' if new_value.value else '0'}\n\t") + lines.append(f"mov {var_pos}, {int(new_value.value)}\n\t") + scope.table[var_name]["type"] = BoolNode - elif type(new_value) == str: # we're changing a variable to the value of a register - lines.append(f"mov QWORD [rsp + {var_pos}], {new_value}\n\t") - scope.table[var_name]["type"] = IntNode + elif type(new_value) == str: # we're changing a variable to the value of a register + #lines.append(f"mov QWORD [rsp + {var_pos}], {new_value}\n\t") + lines.append(f"mov {var_pos}, {new_value}\n\t") + scope.table[var_name]["type"] = IntNode + + + else: + + + if type(new_value) == IntNode: # we're changing a variable to a number + lines.append(f"mov QWORD [rsp + {var_pos}], {new_value.value}\n\t") + scope.table[var_name]["type"] = IntNode + + elif type(new_value) == VarRefNode: # we're changing a variable to the value of another variable + var_type = self.get_variable(lines, new_value.var_name, "rax") + lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t") + scope.table[var_name]["type"] = var_type + + elif type(new_value) == StringNode: # we're changing a variable to a string + lines.append(f"mov QWORD [rsp + {var_pos}], 0\n\t") + + + string_pointer = self.add_constant(new_value.value) + string_len = self.add_constant(f"equ $ - {string_pointer[1:-1]}", no_string=True) + #lines.append(f"lea QWORD [rsp + {var_pos}], {string_pointer}\n\t") + ##lines.append(f"mov QWORD [rsp + {var_pos + 8}], {string_len[1:-1]}\n\t") + + scope.table[var_name]["stack_loc"] = self.stack_size + lines.append(f"lea rax, {string_pointer}\n\t") + self.push("rax", lines) + lines.append(f"mov rax, {string_len[1:-1]}\n\t") + self.push("rax", lines) + + scope.table[var_name]["type"] = StringNode + + elif type(new_value) == BoolNode: + lines.append(f"mov QWORD [rsp + {var_pos}], {'1' if new_value.value else '0'}\n\t") + scope.table[var_name]["type"] = BoolNode + + elif type(new_value) == str: # we're changing a variable to the value of a register + lines.append(f"mov QWORD [rsp + {var_pos}], {new_value}\n\t") + scope.table[var_name]["type"] = IntNode def generate_LabelDecNode(self, node: LabelDecNode, lines): self.labels.append(node.name) diff --git a/optimizers/x86_64.py b/optimizers/x86_64.py index 48c2f82..a1c0aa0 100644 --- a/optimizers/x86_64.py +++ b/optimizers/x86_64.py @@ -132,5 +132,13 @@ class X86_64Optimizer(Optimizer): Instruction("mov", ["rax", "y"]), Instruction("cmp", ["rax", "x"]) ] + ), + + PeepholeRule( + match=[ + Instruction("mov", ["x", "y"]), + Instruction("mov", ["y", "x"]) + ], + replace=[] ) ] \ No newline at end of file diff --git a/out b/out index 11ce66fa0046ff18d5bdd23cadd3a19c4066711e..7e88acafc7885d808ab36ba76002fb3a6346b1ec 100644 GIT binary patch delta 287 zcmaFh`a)%bhPZ|x0~|OoSTHa#I54m>uuQa9$13}aImH-PFRb62CQ61 zVG$u91cZPP5CTF#2nYcoAOwVf5D)_Yl0eXT&E0Ez>o0f^4?2&zo!%OHH*0efncblK z9Nh8Q`W6q>dp$gY-{UZm34`w2{)2VJgn$qb0zyCt2mv7=1cZPP5CTF#2nc~cOu!tU zXmuHBbr_wVHtMR@R*^btH4n|nd9z?`{O60u{{hm_AH=i9Tp&66K2Hf#PWyx5_j!uX zI8DGT_W@9R-fAAZ3}SKR;z=m-_kYS?`X;_g@x@p^oVQFV(q~{A+LWpr%j~Q;2g0zX zb*N2rVIXYOBNJI^)&WYoeP*JXRdeC>m_5_DGEj24bXC6>tvJKLaRO2kTQD;QLoT5PuG7&LhQI`xHOfqVl09TERN(=g84~niSuI;ocN;f){43 Z>}5FOejjbpPQI!yQ~konWb4Q4-vOrAP#ORL diff --git a/out.asm b/out.asm index 8f2a7fc..bc1b03f 100644 --- a/out.asm +++ b/out.asm @@ -1,29 +1,23 @@ ; ~~~ Auto generated by the GroundPY compiler for Linux x86_64 targets. ~~~ section .data -.LC0: db "this was called by a function!!!!", 10, 0 -.LC1: equ $ - .LC0 section .text global _start _start: sub rsp, 8 -mov rdi, 115 -call test -add rsp, 8 +mov rdi, 2 +mov rsi, 3 +call add +add rsp, 16 push rax mov rax, 60 mov rdi, [rsp + 0] syscall -test: +add: push rbp mov rbp, rsp -mov rsi, .LC0 -mov rdx, .LC1 -push rdi -mov rax, 1 -mov rdi, 1 -syscall -pop rdi mov rax, rdi +mov rbx, rsi +add rax, rbx pop rbp ret diff --git a/test2.grnd b/test2.grnd index a53d978..093e48d 100644 --- a/test2.grnd +++ b/test2.grnd @@ -1,8 +1,9 @@ -fun -int !test -int &arg1 - stdout "this was called by a function!!!!\n" - return $arg1 +fun -int !add -int &x -int &y + add $x $y &x + return $x endfun -pusharg 115 -call !test &var +pusharg 2 +pusharg 3 +call !add &var end $var \ No newline at end of file