cleaning up some stuff and optimising
This commit is contained in:
@@ -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])
|
||||
|
||||
|
28
out.asm
28
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
|
||||
|
@@ -1,4 +1,6 @@
|
||||
set &x 66
|
||||
set &y 69
|
||||
set &x 123
|
||||
end $x
|
||||
set &x $x
|
||||
set &y $x
|
||||
set &x 0
|
||||
set &y $x
|
||||
end $y
|
Reference in New Issue
Block a user