COUNTING TO 100 MILLION TEST

This commit is contained in:
SpookyDervish
2025-09-07 13:38:16 +10:00
parent cf1ea42232
commit 47e3e503b8
10 changed files with 198 additions and 59 deletions

View File

@@ -23,7 +23,7 @@ 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, offset: int = 0, no_stack_pop: bool = False):
def get_variable(self, var_name: str, reg: str, float: bool = False, offset: int = 0, no_stack_pop: bool = True):
var = self.variables.get(var_name, None)
var_pos = self.get_var_pos(var_name)
try:
@@ -47,6 +47,14 @@ class X86_64Generator(Generator):
f"QWORD [rsp + {var_pos + offset}]"
)
self.pop(reg)
elif var["type"] == BoolNode:
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.")
@@ -93,6 +101,9 @@ class X86_64Generator(Generator):
self.lines.append(f"mov rax, {string_len[1:-1]}\n\t")
self.push("rax")
elif type(starting_value) == BoolNode:
self.push("1" if starting_value.value else "0")
elif type(starting_value) == str:
if starting_value.startswith("xmm"): # floating point stuff
self.lines.append("sub rsp, 8\n\t") # make space
@@ -131,6 +142,10 @@ class X86_64Generator(Generator):
self.variables[var_name]["type"] = StringNode
elif type(new_value) == BoolNode:
self.lines.append(f"mov QWORD [rsp + {var_pos}], {"1" if new_value.value else "0"}\n\t")
self.variables[var_name]["type"] = BoolNode
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
@@ -150,14 +165,14 @@ class X86_64Generator(Generator):
traceback(self.code, "TypeError", f"end expects an integer, not {node.arguments[0]}")
self.lines.append("mov rax, 60\n\t")
if isinstance(node.arguments[0], IntNode):
if type(node.arguments[0]) in [IntNode,BoolNode]:
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", no_stack_pop=True)
if var_type == FloatNode:
self.lines.append("cvttsd2si rdi, xmm0\n\t")
else:
if var_type != IntNode:
if var_type not in [IntNode,BoolNode]:
traceback(self.code, "TypeError", f"end expects an integer, not \"{var_type}\"")
#self.lines.append("mov rdi, " + str(self.get_variable(node.arguments[0].var_name)) + "\n\t")
self.lines.append("syscall\n\t")
@@ -170,7 +185,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, StringNode]:
if type(node.arguments[1]) not in [IntNode, VarRefNode, FloatNode, StringNode, BoolNode]:
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
@@ -420,10 +435,138 @@ class X86_64Generator(Generator):
traceback(self.code, "TypeError", f"jump expects a label reference as the first argument, not \"{node.arguments[0]}\"")
self.lines.append(f"jmp .{node.arguments[0].name}\n\t")
elif node.instruction == "if":
if len(node.arguments) != 2: # example: "jump" or "jump $bool"
traceback(self.code, "TypeError", "if expects exactly 2 arguments.")
elif not type(node.arguments[0]) in [VarRefNode,BoolNode,StringNode,FloatNode,IntNode]:
traceback(self.code, "TypeError", f"if expects a value or variable refernce as the first argument, not \"{node.arguments[0]}\"")
elif not isinstance(node.arguments[1], LabelRefNode):
traceback(self.code, "TypeError", f"if expects a label reference as the second argument, not \"{node.arguments[1]}\"")
if isinstance(node.arguments[0], BoolNode):
if node.arguments[0].value:
self.lines.append(f"jmp .{node.arguments[1].name}\n\t")
#self.lines.append("mov eax, 1")
#self.lines.append(f"cmp eax, {1 if node.arguments[0].value else 0}")
elif type(node.arguments[0]) in [IntNode,FloatNode]:
if node.arguments[0].value != 0:
self.lines.append(f"jmp .{node.arguments[1].name}\n\t")
elif isinstance(node.arguments[0], VarRefNode):
self.get_variable(node.arguments[0].var_name, "eax")
self.lines.append(f"test eax, eax\n\t")
self.lines.append(f"jnz .{node.arguments[1].name}\n\t")
elif node.instruction == "equal":
if len(node.arguments) != 3: # example: "equal" or "equal $bool"
traceback(self.code, "TypeError", "equal expects exactly 3 arguments.")
elif not type(node.arguments[0]) in [VarRefNode,BoolNode,FloatNode,IntNode]:
traceback(self.code, "TypeError", f"equal expects a value or variable refernce as the first argument, not \"{node.arguments[0].__repr__()}\"")
elif not type(node.arguments[1]) in [VarRefNode,BoolNode,FloatNode,IntNode]:
traceback(self.code, "TypeError", f"equal expects a value or variable refernce as the second argument, not \"{node.arguments[1].__repr__()}\"")
elif not isinstance(node.arguments[2], VarPointerNode):
traceback(self.code, "TypeError", f"the third argument of equal should be a variable pointer, not \"{node.arguments[2].__repr__()}\"")
arg1 = None
arg2 = None
if isinstance(node.arguments[0], BoolNode):
self.lines.append(f"mov rax, {int(node.arguments[0].value)}\n\t")
arg1 = "rax"
elif isinstance(node.arguments[0], IntNode):
self.lines.append(f"mov rax, {node.arguments[0].value}\n\t")
arg1 = "rax"
elif isinstance(node.arguments[0], FloatNode):
const_name = self.add_constant(node.arguments[0].value)
self.lines.append(f"movsd xmm0, {const_name}\n\t")
arg1 = "xmm0"
elif isinstance(node.arguments[0], VarRefNode):
self.get_variable(node.arguments[0].var_name, "rax")
arg1 = "rax"
if isinstance(node.arguments[1], BoolNode):
self.lines.append(f"mov rbx, {int(node.arguments[1].value)}\n\t")
arg2 = "rbx"
elif isinstance(node.arguments[1], IntNode):
self.lines.append(f"mov rbx, {node.arguments[1].value}\n\t")
arg2 = "rbx"
elif isinstance(node.arguments[1], FloatNode):
const_name = self.add_constant(node.arguments[1].value)
self.lines.append(f"movsd xmm1, {const_name}\n\t")
arg2 = "xmm1"
elif isinstance(node.arguments[1], VarRefNode):
self.get_variable(node.arguments[1].var_name, "rbx")
arg2 = "rbx"
self.lines.append(f"cmp {arg1}, {arg2}\n\t")
self.lines.append("sete al\n\t")
self.lines.append("movzx rax, al\n\t")
var_name = node.arguments[2].var_name
if self.variables.get(var_name, None) == None:
self.create_variable(var_name, "rax", BoolNode)
else:
self.change_variable(var_name, "rax")
elif node.instruction == "inequal":
if len(node.arguments) != 3: # example: "inequal" or "inequal $bool"
traceback(self.code, "TypeError", "inequal expects exactly 3 arguments.")
elif not type(node.arguments[0]) in [VarRefNode,BoolNode,FloatNode,IntNode]:
traceback(self.code, "TypeError", f"inequal expects a value or variable refernce as the first argument, not \"{node.arguments[0].__repr__()}\"")
elif not type(node.arguments[1]) in [VarRefNode,BoolNode,FloatNode,IntNode]:
traceback(self.code, "TypeError", f"inequal expects a value or variable refernce as the second argument, not \"{node.arguments[1].__repr__()}\"")
elif not isinstance(node.arguments[2], VarPointerNode):
traceback(self.code, "TypeError", f"the third argument of inequal should be a variable pointer, not \"{node.arguments[2].__repr__()}\"")
arg1 = None
arg2 = None
if isinstance(node.arguments[0], BoolNode):
self.lines.append(f"mov rax, {int(node.arguments[0].value)}\n\t")
arg1 = "rax"
elif isinstance(node.arguments[0], IntNode):
self.lines.append(f"mov rax, {node.arguments[0].value}\n\t")
arg1 = "rax"
elif isinstance(node.arguments[0], FloatNode):
const_name = self.add_constant(node.arguments[0].value)
self.lines.append(f"movsd xmm0, {const_name}\n\t")
arg1 = "xmm0"
elif isinstance(node.arguments[0], VarRefNode):
self.get_variable(node.arguments[0].var_name, "rax")
arg1 = "rax"
if isinstance(node.arguments[1], BoolNode):
self.lines.append(f"mov rbx, {int(node.arguments[1].value)}\n\t")
arg2 = "rbx"
elif isinstance(node.arguments[1], IntNode):
self.lines.append(f"mov rbx, {node.arguments[1].value}\n\t")
arg2 = "rbx"
elif isinstance(node.arguments[1], FloatNode):
const_name = self.add_constant(node.arguments[1].value)
self.lines.append(f"movsd xmm1, {const_name}\n\t")
arg2 = "xmm1"
elif isinstance(node.arguments[1], VarRefNode):
self.get_variable(node.arguments[1].var_name, "rbx")
arg2 = "rbx"
self.lines.append(f"cmp {arg1}, {arg2}\n\t")
self.lines.append("setne al\n\t")
self.lines.append("movzx rax, al\n\t")
var_name = node.arguments[2].var_name
if self.variables.get(var_name, None) == None:
self.create_variable(var_name, "rax", BoolNode)
else:
self.change_variable(var_name, "rax")
else:
self.lines.append("; FUCK\n\t")
#raise NotImplementedError(f"A generate method hasn't been made for the \"{node.instruction}\" instruction.")
def write(self):
with open(self.output_path + ".asm", "w") as f: