diff --git a/a.out b/a.out new file mode 100644 index 0000000..dd7131b Binary files /dev/null and b/a.out differ diff --git a/generators/generator.py b/generators/generator.py index 48d9582..7e01c80 100644 --- a/generators/generator.py +++ b/generators/generator.py @@ -13,10 +13,10 @@ class Generator: self.constants_reverse = {} self.constant_counter = 0 - def add_constant(self, value: Any): + def add_constant(self, value: Any, no_string: bool = False): existing_constant_name = self.constants_reverse.get(value, None) if existing_constant_name != None: return f"[.{existing_constant_name}]" - self.constants["LC" + str(self.constant_counter)] = value + self.constants["LC" + str(self.constant_counter)] = {"value": value, "no_string": no_string} self.constants_reverse[value] = "LC" + str(self.constant_counter) self.constant_counter += 1 return "[.LC" + str(self.constant_counter-1) + "]" diff --git a/generators/x86_64.py b/generators/x86_64.py index 54e1f6e..a05caf5 100644 --- a/generators/x86_64.py +++ b/generators/x86_64.py @@ -23,12 +23,12 @@ class X86_64Generator(Generator): self.lines.append("pop " + reg + "\n\t") self.stack_size -= 1 - def get_variable(self, var_name: str, reg: str, float: bool = False): + def get_variable(self, var_name: str, reg: str, float: bool = False, offset: int = 0, no_stack_pop: bool = False): var = self.variables.get(var_name, None) var_pos = self.get_var_pos(var_name) try: #print(var["type"]) - if var["type"] == FloatNode or float: + if var["type"] == FloatNode: conversion = { "rax": "xmm0", "rbx": "xmm1", @@ -36,14 +36,17 @@ class X86_64Generator(Generator): # ... } - self.lines.append(f"movsd {conversion[reg]}, [rsp + {var_pos}]\n\t") + self.lines.append(f"movsd {conversion[reg]}, [rsp + {var_pos + offset}]\n\t") self.lines.append("add rsp, 8\n\t") #self.stack_size -= 1 - elif var["type"] == IntNode: - self.push( - f"QWORD [rsp + {var_pos}]" - ) - self.pop(reg) + elif var["type"] in [IntNode,StringNode]: + if no_stack_pop: + self.lines.append(f"mov {reg}, [rsp + {var_pos + offset}]\n\t") + else: + self.push( + f"QWORD [rsp + {var_pos + offset}]" + ) + self.pop(reg) except TypeError: # variable doesnt exist traceback(self.code, "NameError", f"\"{var_name}\" is not defined.") @@ -76,6 +79,14 @@ class X86_64Generator(Generator): self.lines.append("movsd [rsp], xmm0\n\t") self.stack_size += 1 + elif type(starting_value) == StringNode: + string_pointer = self.add_constant(starting_value.value) + 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.push("rax") + self.lines.append(f"lea rax, {string_len[1:-1]}\n\t") + self.push("rax") + elif type(starting_value) == str: if starting_value.startswith("xmm"): # floating point stuff self.lines.append("sub rsp, 8\n\t") # make space @@ -99,6 +110,7 @@ class X86_64Generator(Generator): self.lines.append(f"mov QWORD [rsp + {var_pos}], {new_value}\n\t") def generate_InstructionNode(self, node: InstructionNode): + ### MISC ### if node.instruction == "end": if len(node.arguments) == 0: # example: "end" traceback(self.code, "TypeError", "end expects atleast 1 argument.") @@ -111,7 +123,7 @@ class X86_64Generator(Generator): if isinstance(node.arguments[0], IntNode): self.lines.append("mov rdi, " + str(node.arguments[0].value) + "\n\t") elif isinstance(node.arguments[0], VarRefNode): - var_type = self.get_variable(node.arguments[0].var_name, "rdi") + var_type = self.get_variable(node.arguments[0].var_name, "rdi", no_stack_pop=True) if var_type == FloatNode: self.lines.append("cvttsd2si rdi, xmm0\n\t") else: @@ -120,6 +132,7 @@ class X86_64Generator(Generator): #self.lines.append("mov rdi, " + str(self.get_variable(node.arguments[0].var_name)) + "\n\t") self.lines.append("syscall\n\t") + ### VARIABLE INSTRUCTIONS ### elif node.instruction == "set": if len(node.arguments) < 2: # example: "set" or "set &hi" traceback(self.code, "TypeError", "set expects atleast 2 arguments.") @@ -127,7 +140,7 @@ class X86_64Generator(Generator): traceback(self.code, "TypeError", "set expects only 2 arguments.") if not isinstance(node.arguments[0], VarPointerNode): traceback(self.code, "TypeError", f"the first argument of set should be a variable pointer, not \"{node.arguments[0]}\"") - if type(node.arguments[1]) not in [IntNode, VarRefNode, FloatNode]: + if type(node.arguments[1]) not in [IntNode, VarRefNode, FloatNode, StringNode]: traceback(self.code, "TypeError", f"variables can't be of type \"{type(node.arguments[1])}\"") variable_exists = self.variables.get(node.arguments[0].var_name, None) != None @@ -137,6 +150,7 @@ class X86_64Generator(Generator): else: # modify the existing one self.change_variable(node.arguments[0].var_name, node.arguments[1]) + ### MATH INSTRUCTIONS ### elif node.instruction == "add": if len(node.arguments) < 3: # example: "add" or "add 1" or "add 1 2" traceback(self.code, "TypeError", "add expects atleast 3 arguments.") @@ -191,7 +205,6 @@ class X86_64Generator(Generator): self.create_variable(node.arguments[2].var_name, starting_reg, IntNode if is_integer else FloatNode) else: self.change_variable(node.arguments[2].var_name, starting_reg) - elif node.instruction == "subtract": if len(node.arguments) < 3: # example: "subtract" or "subtract 1" or "subtract 1 2" traceback(self.code, "TypeError", "subtract expects atleast 3 arguments.") @@ -246,6 +259,147 @@ class X86_64Generator(Generator): self.create_variable(node.arguments[2].var_name, starting_reg, IntNode if is_integer else FloatNode) else: self.change_variable(node.arguments[2].var_name, starting_reg) + elif node.instruction == "multiply": + if len(node.arguments) < 3: # example: "multiply" or "multiply 1" or "multiply 1 2" + traceback(self.code, "TypeError", "multiply expects atleast 3 arguments.") + elif len(node.arguments) > 3: # example: "multiply 1 2 3 4" + traceback(self.code, "TypeError", "multiply expects only 3 arguments.") + elif type(node.arguments[2]) != VarPointerNode: + traceback(self.code, "TypeError", f"the destination of the multiply command must be a variable pointer, not \"{node.arguments[2]}\"") + + # bro this entire god damn instruction is just error handling 😔 + number1_type = None + number2_type = None + arg2 = "rbx" + if isinstance(node.arguments[0], VarRefNode): + number1_type = self.get_variable(node.arguments[0].var_name, "rax", isinstance(node.arguments[1], FloatNode)) + elif isinstance(node.arguments[0], FloatNode) or isinstance(node.arguments[1], FloatNode): + number1_type = FloatNode + constant_name = self.add_constant(node.arguments[0].value) + self.lines.append(f"movsd xmm0, {constant_name}\n\t") + elif isinstance(node.arguments[0], IntNode): + number1_type = IntNode + #arg1 = node.arguments[0].value + self.lines.append(f"mov rax, {node.arguments[0].value}\n\t") + else: + traceback(self.code, "TypeError", f"expected a variable reference or number for argument 1 of multiply, got {node.arguments[0]}") + + if isinstance(node.arguments[1], VarRefNode): + number2_type = self.get_variable(node.arguments[1].var_name, "rbx", number1_type == FloatNode) + elif number1_type == FloatNode or isinstance(node.arguments[1], FloatNode): + number2_type = FloatNode + constant_name = self.add_constant(node.arguments[1].value) + self.lines.append(f"movsd xmm1, {constant_name}\n\t") + elif isinstance(node.arguments[1], IntNode): + number2_type = IntNode + arg2 = node.arguments[1].value + #self.lines.append(f"mov rbx, {node.arguments[1].value}\n\t") + else: + traceback(self.code, "TypeError", f"expected a variable reference or number for argument 2 of multiply, got {node.arguments[1]}") + + # TODO: numbers can be added to numbers, but numbers cant be added to strings. but strings can be added to strings, etc... + if number1_type not in [IntNode, FloatNode] or number2_type not in [IntNode, FloatNode]: + traceback(self.code, "TypeError", f"Unsupported operation \"multiply\" for \"{node.arguments[0]}\" and \"{node.arguments[1]}\".") + + if number1_type == IntNode and number2_type == IntNode: + self.lines.append(f"imul rax, {arg2}\n\t") + else: + self.lines.append(f"mulsd xmm0, xmm1\n\t") + + is_integer = number1_type == IntNode and number2_type == IntNode + starting_reg = "rax" if is_integer else "xmm0" + + if self.variables.get(node.arguments[2].var_name, None) == None: # we need to create a variable for the destination + self.create_variable(node.arguments[2].var_name, starting_reg, IntNode if is_integer else FloatNode) + else: + self.change_variable(node.arguments[2].var_name, starting_reg) + elif node.instruction == "divide": + if len(node.arguments) < 3: # example: "divide" or "divide 1" or "divide 1 2" + traceback(self.code, "TypeError", "divide expects atleast 3 arguments.") + elif len(node.arguments) > 3: # example: "divide 1 2 3 4" + traceback(self.code, "TypeError", "divide expects only 3 arguments.") + elif type(node.arguments[2]) != VarPointerNode: + traceback(self.code, "TypeError", f"the destination of the divide command must be a variable pointer, not \"{node.arguments[2]}\"") + + # bro this entire god damn instruction is just error handling 😔 + arg2 = "xmm1" + number1_type = None + number2_type = None + + if isinstance(node.arguments[0], VarRefNode): + number1_type = self.get_variable(node.arguments[0].var_name, "rax", True) + elif type(node.arguments[0]) in [IntNode, FloatNode]: + number1_type = FloatNode + constant_name = self.add_constant(node.arguments[0].value) + self.lines.append(f"movsd xmm0, {constant_name}\n\t") + else: + traceback(self.code, "TypeError", f"expected a variable reference or number for argument 1 of divide, got {node.arguments[0]}") + + if isinstance(node.arguments[1], VarRefNode): + number2_type = self.get_variable(node.arguments[1].var_name, "rbx", True) + elif type(node.arguments[1]) in [IntNode, FloatNode]: + number2_type = FloatNode + constant_name = self.add_constant(node.arguments[1].value) + self.lines.append(f"movsd xmm1, {constant_name}\n\t") + else: + traceback(self.code, "TypeError", f"expected a variable reference or number for argument 2 of divide, got {node.arguments[1]}") + + # TODO: numbers can be added to numbers, but numbers cant be added to strings. but strings can be added to strings, etc... + if number1_type not in [IntNode, FloatNode] or number2_type not in [IntNode, FloatNode]: + traceback(self.code, "TypeError", f"Unsupported operation \"divide\" for \"{node.arguments[0]}\" and \"{node.arguments[1]}\".") + + self.lines.append(f"divsd xmm0, xmm1\n\t") + + if self.variables.get(node.arguments[2].var_name, None) == None: # we need to create a variable for the destination + self.create_variable(node.arguments[2].var_name, "xmm0", FloatNode) + else: + self.change_variable(node.arguments[2].var_name, "xmm0") + + elif node.instruction == "stdout": + if len(node.arguments) < 1: # example: "stdout" + traceback(self.code, "TypeError", "stdout expects atleast 1 argument.") + elif len(node.arguments) > 1: # example: "stdout "hi" 123" + traceback(self.code, "TypeError", "stdout expects at most 1 argument.") + + arg = node.arguments[0] + + printed_value = arg.__str__() + if isinstance(arg, VarRefNode): + self.get_variable(arg.var_name, "rax") + string_pointer = "rax" + else: + string_pointer = self.add_constant(printed_value)[1:-1] + string_len = self.add_constant(f"equ $ - {string_pointer}", True)[1:-1] + + self.lines.append("mov rax, 1\n\t") # sys_write syscall + self.lines.append("mov rdi, 1\n\t") # a file descriptor of 1 is stdout + self.lines.append(f"mov rsi, {string_pointer}\n\t") # pointer + self.lines.append(f"mov rdx, {string_len}\n\t") # length + self.lines.append("syscall\n\t") + + elif node.instruction == "stdlnout": + if len(node.arguments) < 1: # example: "stdlnout" + traceback(self.code, "TypeError", "stdlnout expects atleast 1 argument.") + elif len(node.arguments) > 1: # example: "stdlnout "hi" 123" + traceback(self.code, "TypeError", "stdlnout expects at most 1 argument.") + + arg = node.arguments[0] + + printed_value = arg.__str__() + "\n" + + if isinstance(arg, VarRefNode): + self.get_variable(arg.var_name, "rsi", False, 0, True) + self.get_variable(arg.var_name, "rdx", False, 8, True) + else: + string_pointer = self.add_constant(printed_value)[1:-1] + string_len = self.add_constant(f"equ $ - {string_pointer}", True)[1:-1] + self.lines.append(f"mov rsi, {string_pointer}\n\t") + self.lines.append(f"mov rdx, {string_len}\n\t") # length + print(string_pointer) + + self.lines.append("mov rax, 1\n\t") # sys_write syscall + self.lines.append("mov rdi, 1\n\t") # a file descriptor of 1 is stdout + self.lines.append("syscall\n\t") else: self.lines.append("; FUCK\n\t") @@ -255,11 +409,15 @@ class X86_64Generator(Generator): with open(self.output_path + ".asm", "w") as f: f.write("section .data\n") - for name, value in self.constants.items(): + for name, const in self.constants.items(): + value = const["value"] f.write("." + name + ": ") value_type = type(value) if value_type == str: - f.write(f"db \"{value}\", 0") + if not const["no_string"]: + f.write(f"db \"{value.replace("\n","\", 10, \"")}\", 0".replace(", \"\", ", ", ")) + else: + f.write(value) elif value_type == float or value_type == int: f.write(f"dq {float(value)}") f.write("\n") diff --git a/ground_ast.py b/ground_ast.py index 9dc87e0..db0ef7d 100644 --- a/ground_ast.py +++ b/ground_ast.py @@ -18,6 +18,8 @@ class StringNode: value: str def __repr__(self): return "String" + def __str__(self): + return self.value @dataclass class IntNode: value: float diff --git a/out b/out index 717b80d..449ddc9 100644 Binary files a/out and b/out differ diff --git a/out.asm b/out.asm index fefaa5c..aef4134 100644 --- a/out.asm +++ b/out.asm @@ -1,9 +1,27 @@ section .data +.LC0: db "Hello, World!", 0 +.LC1: equ $ - .LC0 section .text global _start _start: - ; InstructionNode(instruction='divide', parent=RootNode(statements=[...]), arguments=[]) - ; FUCK + ; 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] + push rax + lea rax, .LC1 + 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]) + mov rax, 123 + 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]) + mov rsi, [rsp + 16] + mov rdx, [rsp + 24] + mov rax, 1 + mov rdi, 1 + syscall + ; InstructionNode(instruction='end', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='stdlnout', parent=..., arguments=[VariableReference]), ...]), arguments=[VariableReference]) + mov rax, 60 + mov rdi, [rsp + 0] + syscall \ No newline at end of file diff --git a/test2.grnd b/test2.grnd index 54d243b..6b4aab7 100644 --- a/test2.grnd +++ b/test2.grnd @@ -1 +1,4 @@ -divide \ No newline at end of file +set &x "Hello, World!" +set &y 123 +stdlnout $x +end $y \ No newline at end of file