THE ADD FUNCTION COMPILES!!!!!

This commit is contained in:
SpookyDervish
2025-09-10 20:45:22 +10:00
parent a23bcf7823
commit 2ce71e7abb
5 changed files with 90 additions and 44 deletions

View File

@@ -132,39 +132,82 @@ class X86_64Generator(Generator):
scope = scope or self.current_var_scope
var_pos = self.get_var_pos(var_name, scope)
if type(new_value) == IntNode: # we're changing a variable to a number
lines.append(f"mov QWORD [rsp + {var_pos}], {new_value.value}\n\t")
scope.table[var_name]["type"] = IntNode
if isinstance(var_pos, str):
if type(new_value) == IntNode: # we're changing a variable to a number
#lines.append(f"mov QWORD [rsp + {var_pos}], {new_value.value}\n\t")
lines.append(f"mov {var_pos}, {new_value.value}\n\t")
scope.table[var_name]["type"] = IntNode
elif type(new_value) == VarRefNode: # we're changing a variable to the value of another variable
var_type = self.get_variable(lines, new_value.var_name, "rax")
lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t")
scope.table[var_name]["type"] = var_type
elif type(new_value) == VarRefNode: # we're changing a variable to the value of another variable
var_type = self.get_variable(lines, new_value.var_name, "rax")
#lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t")
lines.append(f"mov {var_pos}, rax\n\t")
scope.table[var_name]["type"] = var_type
elif type(new_value) == StringNode: # we're changing a variable to a string
lines.append(f"mov QWORD [rsp + {var_pos}], 0\n\t")
elif type(new_value) == StringNode: # we're changing a variable to a string
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)
#lines.append(f"lea QWORD [rsp + {var_pos}], {string_pointer}\n\t")
##lines.append(f"mov QWORD [rsp + {var_pos + 8}], {string_len[1:-1]}\n\t")
string_pointer = self.add_constant(new_value.value)
string_len = self.add_constant(f"equ $ - {string_pointer[1:-1]}", no_string=True)
#lines.append(f"lea QWORD [rsp + {var_pos}], {string_pointer}\n\t")
##lines.append(f"mov QWORD [rsp + {var_pos + 8}], {string_len[1:-1]}\n\t")
scope.table[var_name]["stack_loc"] = self.stack_size
lines.append(f"lea rax, {string_pointer}\n\t")
self.push("rax", lines)
lines.append(f"mov rax, {string_len[1:-1]}\n\t")
self.push("rax", lines)
scope.table[var_name]["stack_loc"] = self.stack_size
lines.append(f"lea rax, {string_pointer}\n\t")
self.push("rax", lines)
lines.append(f"mov rax, {string_len[1:-1]}\n\t")
self.push("rax", lines)
scope.table[var_name]["type"] = StringNode
scope.table[var_name]["type"] = StringNode
elif type(new_value) == BoolNode:
lines.append(f"mov QWORD [rsp + {var_pos}], {'1' if new_value.value else '0'}\n\t")
scope.table[var_name]["type"] = BoolNode
elif type(new_value) == BoolNode:
#lines.append(f"mov QWORD [rsp + {var_pos}], {'1' if new_value.value else '0'}\n\t")
lines.append(f"mov {var_pos}, {int(new_value.value)}\n\t")
scope.table[var_name]["type"] = BoolNode
elif type(new_value) == str: # we're changing a variable to the value of a register
lines.append(f"mov QWORD [rsp + {var_pos}], {new_value}\n\t")
scope.table[var_name]["type"] = IntNode
elif type(new_value) == str: # we're changing a variable to the value of a register
#lines.append(f"mov QWORD [rsp + {var_pos}], {new_value}\n\t")
lines.append(f"mov {var_pos}, {new_value}\n\t")
scope.table[var_name]["type"] = IntNode
else:
if type(new_value) == IntNode: # we're changing a variable to a number
lines.append(f"mov QWORD [rsp + {var_pos}], {new_value.value}\n\t")
scope.table[var_name]["type"] = IntNode
elif type(new_value) == VarRefNode: # we're changing a variable to the value of another variable
var_type = self.get_variable(lines, new_value.var_name, "rax")
lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t")
scope.table[var_name]["type"] = var_type
elif type(new_value) == StringNode: # we're changing a variable to a string
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)
#lines.append(f"lea QWORD [rsp + {var_pos}], {string_pointer}\n\t")
##lines.append(f"mov QWORD [rsp + {var_pos + 8}], {string_len[1:-1]}\n\t")
scope.table[var_name]["stack_loc"] = self.stack_size
lines.append(f"lea rax, {string_pointer}\n\t")
self.push("rax", lines)
lines.append(f"mov rax, {string_len[1:-1]}\n\t")
self.push("rax", lines)
scope.table[var_name]["type"] = StringNode
elif type(new_value) == BoolNode:
lines.append(f"mov QWORD [rsp + {var_pos}], {'1' if new_value.value else '0'}\n\t")
scope.table[var_name]["type"] = BoolNode
elif type(new_value) == str: # we're changing a variable to the value of a register
lines.append(f"mov QWORD [rsp + {var_pos}], {new_value}\n\t")
scope.table[var_name]["type"] = IntNode
def generate_LabelDecNode(self, node: LabelDecNode, lines):
self.labels.append(node.name)

View File

@@ -132,5 +132,13 @@ class X86_64Optimizer(Optimizer):
Instruction("mov", ["rax", "y"]),
Instruction("cmp", ["rax", "x"])
]
),
PeepholeRule(
match=[
Instruction("mov", ["x", "y"]),
Instruction("mov", ["y", "x"])
],
replace=[]
)
]

BIN
out

Binary file not shown.

20
out.asm
View File

@@ -1,29 +1,23 @@
; ~~~ Auto generated by the GroundPY compiler for Linux x86_64 targets. ~~~
section .data
.LC0: db "this was called by a function!!!!", 10, 0
.LC1: equ $ - .LC0
section .text
global _start
_start:
sub rsp, 8
mov rdi, 115
call test
add rsp, 8
mov rdi, 2
mov rsi, 3
call add
add rsp, 16
push rax
mov rax, 60
mov rdi, [rsp + 0]
syscall
test:
add:
push rbp
mov rbp, rsp
mov rsi, .LC0
mov rdx, .LC1
push rdi
mov rax, 1
mov rdi, 1
syscall
pop rdi
mov rax, rdi
mov rbx, rsi
add rax, rbx
pop rbp
ret

View File

@@ -1,8 +1,9 @@
fun -int !test -int &arg1
stdout "this was called by a function!!!!\n"
return $arg1
fun -int !add -int &x -int &y
add $x $y &x
return $x
endfun
pusharg 115
call !test &var
pusharg 2
pusharg 3
call !add &var
end $var