I FIXED THE STACK ISSUES LESS GOOO

This commit is contained in:
SpookyDervish
2025-09-06 21:08:14 +10:00
parent 85331b4067
commit daf42eba09
3 changed files with 6 additions and 5 deletions

View File

@@ -38,7 +38,7 @@ class X86_64Generator(Generator):
self.lines.append(f"movsd {conversion[reg]}, [rsp + {var_pos + offset}]\n\t") self.lines.append(f"movsd {conversion[reg]}, [rsp + {var_pos + offset}]\n\t")
self.lines.append("add rsp, 8\n\t") self.lines.append("add rsp, 8\n\t")
#self.stack_size -= 1 self.stack_size += 1
elif var["type"] in [IntNode,StringNode]: elif var["type"] in [IntNode,StringNode]:
if no_stack_pop: if no_stack_pop:
self.lines.append(f"mov {reg}, [rsp + {var_pos + offset}]\n\t") self.lines.append(f"mov {reg}, [rsp + {var_pos + offset}]\n\t")
@@ -50,6 +50,7 @@ class X86_64Generator(Generator):
except TypeError: # variable doesnt exist except TypeError: # variable doesnt exist
traceback(self.code, "NameError", f"\"{var_name}\" is not defined.") traceback(self.code, "NameError", f"\"{var_name}\" is not defined.")
print(var, self.stack_size)
return var["type"] return var["type"]
def get_var_pos(self, var_name: str): def get_var_pos(self, var_name: str):
@@ -84,7 +85,7 @@ class X86_64Generator(Generator):
string_len = self.add_constant(f"equ $ - {string_pointer[1:-1]}", no_string=True) string_len = self.add_constant(f"equ $ - {string_pointer[1:-1]}", no_string=True)
self.lines.append(f"lea rax, {string_pointer}\n\t") self.lines.append(f"lea rax, {string_pointer}\n\t")
self.push("rax") self.push("rax")
self.lines.append(f"lea rax, {string_len[1:-1]}\n\t") self.lines.append(f"mov rax, {string_len[1:-1]}\n\t")
self.push("rax") self.push("rax")
elif type(starting_value) == str: elif type(starting_value) == str:
@@ -389,7 +390,7 @@ class X86_64Generator(Generator):
if isinstance(arg, VarRefNode): if isinstance(arg, VarRefNode):
self.get_variable(arg.var_name, "rsi", False, 0, True) self.get_variable(arg.var_name, "rsi", False, 0, True)
self.get_variable(arg.var_name, "rdx", False, 8, True) self.get_variable(arg.var_name, "rdx", False, -8, True)
else: else:
string_pointer = self.add_constant(printed_value)[1:-1] string_pointer = self.add_constant(printed_value)[1:-1]
string_len = self.add_constant(f"equ $ - {string_pointer}", True)[1:-1] string_len = self.add_constant(f"equ $ - {string_pointer}", True)[1:-1]

BIN
out

Binary file not shown.

View File

@@ -9,14 +9,14 @@ _start:
; InstructionNode(instruction='set', parent=RootNode(statements=[..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='stdlnout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, String]) ; InstructionNode(instruction='set', parent=RootNode(statements=[..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='stdlnout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, String])
lea rax, [.LC0] lea rax, [.LC0]
push rax push rax
lea rax, .LC1 mov rax, .LC1
push rax push rax
; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), ..., InstructionNode(instruction='stdlnout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Int]) ; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), ..., InstructionNode(instruction='stdlnout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Int])
mov rax, 123 mov rax, 123
push rax push rax
; InstructionNode(instruction='stdlnout', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), ..., InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariableReference]) ; InstructionNode(instruction='stdlnout', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), ..., InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariableReference])
mov rsi, [rsp + 16] mov rsi, [rsp + 16]
mov rdx, [rsp + 24] mov rdx, [rsp + 8]
mov rax, 1 mov rax, 1
mov rdi, 1 mov rdi, 1
syscall syscall