WE'RE REFORMATTING THE STACK LESS GOOO

This commit is contained in:
SpookyDervish
2025-09-07 07:25:11 +10:00
parent 85a547780d
commit bee2087ab4
5 changed files with 52 additions and 53 deletions

View File

@@ -54,7 +54,10 @@ class X86_64Generator(Generator):
return var["type"]
def get_var_pos(self, var_name: str):
return (self.stack_size - self.variables.get(var_name)['stack_loc'] - 1) * 8
try:
return (self.stack_size - self.variables.get(var_name)['stack_loc'] - 1) * 8
except TypeError: # not defined
traceback(self.code, "TypeError", f"\"{var_name}\" is not defined.")
def create_variable(self, var_name: str, starting_value, var_type: Any = None):
if var_type == None:
@@ -102,15 +105,37 @@ class X86_64Generator(Generator):
def change_variable(self, var_name: str, new_value):
var_pos = self.get_var_pos(var_name)
print(self.variables[var_name]["type"])
if type(new_value) == IntNode: # we're changing a variable to a number
self.lines.append(f"mov QWORD [rsp + {var_pos}], {new_value.value}\n\t")
self.variables[var_name]["type"] = IntNode
elif type(new_value) == VarRefNode: # we're changing a variable to the value of another variable
self.get_variable(new_value.var_name, "rax")
var_type = self.get_variable(new_value.var_name, "rax")
self.lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t")
self.variables[var_name]["type"] = var_type
elif type(new_value) == StringNode: # we're changing a variable to a string
self.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)
#self.lines.append(f"lea QWORD [rsp + {var_pos}], {string_pointer}\n\t")
##self.lines.append(f"mov QWORD [rsp + {var_pos + 8}], {string_len[1:-1]}\n\t")
self.variables[var_name]["stack_loc"] = self.stack_size
self.lines.append(f"lea rax, {string_pointer}\n\t")
self.push("rax")
self.lines.append(f"mov rax, {string_len[1:-1]}\n\t")
self.push("rax")
self.variables[var_name]["type"] = StringNode
elif type(new_value) == str: # we're changing a variable to the value of a register
self.lines.append(f"mov QWORD [rsp + {var_pos}], {new_value}\n\t")
self.variables[var_name]["type"] = IntNode
print(self.variables[var_name]["type"])
def generate_InstructionNode(self, node: InstructionNode):
### MISC ###
@@ -369,8 +394,11 @@ class X86_64Generator(Generator):
printed_value = arg.__str__()
if isinstance(arg, VarRefNode):
self.get_variable(arg.var_name, "rsi", False, 0, True)
self.get_variable(arg.var_name, "rdx", False, -8, True)
var_type = self.get_variable(arg.var_name, "rsi", offset=0, no_stack_pop=True)
if var_type == StringNode:
self.get_variable(arg.var_name, "rdx", offset=-8, no_stack_pop=True)
else:
traceback(self.code, "TypeError", f"You can't print \"{var_type(None).__repr__()}\", try converting it to a string first.")
else:
string_pointer = self.add_constant(printed_value)[1:-1]
string_len = self.add_constant(f"equ $ - {string_pointer}", True)[1:-1]