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"] return var["type"]
def get_var_pos(self, var_name: str): def get_var_pos(self, var_name: str):
try:
return (self.stack_size - self.variables.get(var_name)['stack_loc'] - 1) * 8 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): def create_variable(self, var_name: str, starting_value, var_type: Any = None):
if var_type == None: if var_type == None:
@@ -102,15 +105,37 @@ class X86_64Generator(Generator):
def change_variable(self, var_name: str, new_value): def change_variable(self, var_name: str, new_value):
var_pos = self.get_var_pos(var_name) 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 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.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 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.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 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.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): def generate_InstructionNode(self, node: InstructionNode):
### MISC ### ### MISC ###
@@ -369,8 +394,11 @@ class X86_64Generator(Generator):
printed_value = arg.__str__() printed_value = arg.__str__()
if isinstance(arg, VarRefNode): if isinstance(arg, VarRefNode):
self.get_variable(arg.var_name, "rsi", False, 0, True) var_type = self.get_variable(arg.var_name, "rsi", offset=0, no_stack_pop=True)
self.get_variable(arg.var_name, "rdx", False, -8, 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: 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]

View File

@@ -52,6 +52,8 @@ class TypeNode:
value: str value: str
def __repr__(self): def __repr__(self):
return "Type" return "Type"
def __str__(self):
return "-" + self.value
@dataclass @dataclass
class ArgNode: class ArgNode:
arg_type: str arg_type: str
@@ -84,6 +86,8 @@ class BoolNode:
value: bool value: bool
def __repr__(self): def __repr__(self):
return "Boolean" return "Boolean"
def __str__(self):
return str(self.value).lower()
def generate_ast(tokens: list[Token], code: str) -> RootNode: def generate_ast(tokens: list[Token], code: str) -> RootNode:
root_node = RootNode([]) root_node = RootNode([])

BIN
out

Binary file not shown.

57
out.asm
View File

@@ -1,66 +1,37 @@
; ~~~ Auto generated by the GroundPY compiler for Linux x86_64 targets. ~~~ ; ~~~ Auto generated by the GroundPY compiler for Linux x86_64 targets. ~~~
section .data section .data
.LC0: db "123", 0 .LC0: db "hiii", 0
.LC1: equ $ - .LC0 .LC1: equ $ - .LC0
.LC2: db "", 10, "WE CAN PRINT FUCKING NUMBERS AND SHITT", 10, 0 .LC2: db "", 10, 0
.LC3: equ $ - .LC2 .LC3: equ $ - .LC2
.LC4: db "LESS GOOOOO", 10, 0
.LC5: equ $ - .LC4
.LC6: db "this was printed using a variable!!!!!", 10, 0
.LC7: equ $ - .LC6
.LC8: db "", 10, 0
.LC9: equ $ - .LC8
section .text section .text
global _start global _start
_start: _start:
; InstructionNode(instruction='stdout', parent=RootNode(statements=[..., InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='end', parent=..., arguments=[Int])]), arguments=[Int]) ; InstructionNode(instruction='set', parent=RootNode(statements=[..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='end', parent=..., arguments=[Int])]), arguments=[VariablePointer, Int])
mov rsi, .LC0 mov rax, 123
mov rdx, .LC1
mov rax, 1
mov rdi, 1
syscall
; InstructionNode(instruction='stdout', parent=RootNode(statements=[InstructionNode(instruction='stdout', parent=..., arguments=[Int]), ..., InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='end', parent=..., arguments=[Int])]), arguments=[String])
mov rsi, .LC2
mov rdx, .LC3
mov rax, 1
mov rdi, 1
syscall
; InstructionNode(instruction='stdout', parent=RootNode(statements=[InstructionNode(instruction='stdout', parent=..., arguments=[Int]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), ..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='end', parent=..., arguments=[Int])]), arguments=[String])
mov rsi, .LC4
mov rdx, .LC5
mov rax, 1
mov rdi, 1
syscall
; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='stdout', parent=..., arguments=[Int]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), ..., InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='end', parent=..., arguments=[Int])]), arguments=[VariablePointer, String])
lea rax, [.LC6]
push rax push rax
mov rax, .LC7 ; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), ..., InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='end', parent=..., arguments=[Int])]), arguments=[VariablePointer, String])
mov QWORD [rsp + 0], 0
lea rax, [.LC0]
push rax push rax
; InstructionNode(instruction='stdout', parent=RootNode(statements=[InstructionNode(instruction='stdout', parent=..., arguments=[Int]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), ..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='end', parent=..., arguments=[Int])]), arguments=[VariableReference]) mov rax, .LC1
push rax
; InstructionNode(instruction='stdout', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), ..., InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='end', parent=..., arguments=[Int])]), arguments=[VariableReference])
mov rsi, [rsp + 8] mov rsi, [rsp + 8]
mov rdx, [rsp + 0] mov rdx, [rsp + 0]
mov rax, 1 mov rax, 1
mov rdi, 1 mov rdi, 1
syscall syscall
; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='stdout', parent=..., arguments=[Int]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), ..., InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='end', parent=..., arguments=[Int])]), arguments=[VariablePointer, Int]) ; InstructionNode(instruction='stdout', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), ..., InstructionNode(instruction='end', parent=..., arguments=[Int])]), arguments=[String])
mov rax, 123 mov rsi, .LC2
push rax mov rdx, .LC3
; InstructionNode(instruction='stdout', parent=RootNode(statements=[InstructionNode(instruction='stdout', parent=..., arguments=[Int]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), ..., InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='end', parent=..., arguments=[Int])]), arguments=[VariableReference])
mov rsi, [rsp + 0]
mov rdx, [rsp + -8]
mov rax, 1 mov rax, 1
mov rdi, 1 mov rdi, 1
syscall syscall
; InstructionNode(instruction='stdout', parent=RootNode(statements=[InstructionNode(instruction='stdout', parent=..., arguments=[Int]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), ..., InstructionNode(instruction='end', parent=..., arguments=[Int])]), arguments=[String]) ; InstructionNode(instruction='end', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), ...]), arguments=[Int])
mov rsi, .LC8
mov rdx, .LC9
mov rax, 1
mov rdi, 1
syscall
; InstructionNode(instruction='end', parent=RootNode(statements=[InstructionNode(instruction='stdout', parent=..., arguments=[Int]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='stdout', parent=..., arguments=[String]), ...]), arguments=[Int])
mov rax, 60 mov rax, 60
mov rdi, 0 mov rdi, 0
syscall syscall

View File

@@ -1,9 +1,5 @@
stdout 123 set &x 123
stdout "\nWE CAN PRINT FUCKING NUMBERS AND SHITT\n" set &x "hiii"
stdout "LESS GOOOOO\n"
set &x "this was printed using a variable!!!!!\n"
stdout $x stdout $x
set &y 123
stdout $y
stdout "\n" stdout "\n"
end 0 end 0