From a32598f7f05508102050362b8ddb109d561e155b Mon Sep 17 00:00:00 2001 From: SpookyDervish <78246495+SpookyDervish@users.noreply.github.com> Date: Tue, 2 Sep 2025 16:57:56 +1000 Subject: [PATCH] cleaning up some stuff and optimising --- generators/x86_64.py | 34 ++++++++++++++++++++++++---------- out | Bin 4672 -> 4680 bytes out.asm | 28 ++++++++++++++++++---------- test2.grnd | 8 +++++--- 4 files changed, 47 insertions(+), 23 deletions(-) diff --git a/generators/x86_64.py b/generators/x86_64.py index 8e47d02..1dca390 100644 --- a/generators/x86_64.py +++ b/generators/x86_64.py @@ -24,17 +24,34 @@ class X86_64Generator(Generator): self.stack_size -= 1 def get_variable(self, var_name: str, reg: str): - self.push( - f"QWORD [rsp + {(self.stack_size - self.variables.get(var_name)['stack_loc'] - 1) * 8}]" - ) + try: + self.push( + f"QWORD [rsp + {(self.stack_size - self.variables.get(var_name, None)['stack_loc'] - 1) * 8}]" + ) + except TypeError: # variable doesnt exist + traceback(self.code, "NameError", f"\"{var_name}\" is not defined.") self.pop(reg) + def get_var_pos(self, var_name: str): + return (self.stack_size - self.variables.get(var_name)['stack_loc'] - 1) * 8 + + def create_variable(self, var_name: str, starting_value): + new_var = {"stack_loc": (self.stack_size), "type": type(starting_value)} + if type(starting_value) == NumberNode: + self.lines.append(f"mov rax, {starting_value.value}\n\t") + self.push("rax") + elif type(starting_value) == VarRefNode: + self.get_variable(starting_value.var_name, "rax") + #self.lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t") + self.push("rax") + self.variables[var_name] = new_var + def change_variable(self, var_name: str, new_value): - var_pos = (self.stack_size - self.variables.get(var_name)['stack_loc'] - 1) * 8 + var_pos = self.get_var_pos(var_name) if type(new_value) == NumberNode: # we're changing a variable to a number - self.lines.append(f"mov rax, {new_value.value}\n\t") - self.lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t") + #self.lines.append(f"mov rax, {new_value.value}\n\t") + self.lines.append(f"mov QWORD [rsp + {var_pos}], {new_value.value}\n\t") elif type(new_value) == VarRefNode: # we're changing a variable to the value of another variable self.get_variable(new_value.var_name, "rax") @@ -70,10 +87,7 @@ class X86_64Generator(Generator): variable_exists = self.variables.get(node.arguments[0].var_name, None) != None if not variable_exists: # create a new variable - self.variables[node.arguments[0].var_name] = {"stack_loc": (self.stack_size), "type": type(node.arguments[1])} - if type(node.arguments[1]) == NumberNode: - self.lines.append(f"mov rax, {node.arguments[1].value}\n\t") - self.push("rax") + self.create_variable(node.arguments[0].var_name, node.arguments[1]) else: # modify the existing one self.change_variable(node.arguments[0].var_name, node.arguments[1]) diff --git a/out b/out index 71a78818b26994a1d023cc5a6c73b49d549591de..19e868773aea551c1a0a7df3c3b93d44e9d7de0f 100644 GIT binary patch delta 103 zcmX@0azbT-1|!Er&3sM+1_&_NxG;fVry9r!_-~>T;nB&W0-^&vj=QLEfRy|%QQ-g! o@2~;!OjIVz3rKHv5Kv%bG?<(yXbvQ23feOsn0ymRJ_M5L0B?I7TL1t6 delta 101 zcmX@1azJH*1|!2n&3sNJ1_)5vxG;fV(Fw>2*x?E$tAV6Pr;7^54jUlre~Ai5JU{Ei mjdGhk1QZxKVEQI5l$hKhD8Z;Pd7+>^f#gjfsSW_5UKuX{ diff --git a/out.asm b/out.asm index 843d179..9b05ca8 100644 --- a/out.asm +++ b/out.asm @@ -1,18 +1,26 @@ global _start _start: - ; InstructionNode(instruction='set', parent=RootNode(statements=[..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Number]) - mov rax, 66 - push rax - ; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), ..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Number]) - mov rax, 69 - push rax - ; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), ..., InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Number]) + ; InstructionNode(instruction='set', parent=RootNode(statements=[..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Number]) mov rax, 123 - mov QWORD [rsp + 8], rax - ; InstructionNode(instruction='end', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), ...]), arguments=[VariableReference]) - mov rax, 60 + push rax + ; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), ..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, VariableReference]) + push QWORD [rsp + 0] + pop rax + mov QWORD [rsp + 0], rax + ; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), ..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, VariableReference]) + push QWORD [rsp + 0] + pop rax + push rax + ; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), ..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Number]) + mov QWORD [rsp + 8], 0 + ; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), ..., InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, VariableReference]) push QWORD [rsp + 8] + pop rax + mov QWORD [rsp + 0], rax + ; InstructionNode(instruction='end', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), ...]), arguments=[VariableReference]) + mov rax, 60 + push QWORD [rsp + 0] pop rdi syscall \ No newline at end of file diff --git a/test2.grnd b/test2.grnd index 69e671d..2c3d173 100644 --- a/test2.grnd +++ b/test2.grnd @@ -1,4 +1,6 @@ -set &x 66 -set &y 69 set &x 123 -end $x \ No newline at end of file +set &x $x +set &y $x +set &x 0 +set &y $x +end $y \ No newline at end of file