162 lines
4.7 KiB
Python
162 lines
4.7 KiB
Python
sizes = ["64", "32", "16", "8"]
|
|
types = ["S64", "U64", "F64"]
|
|
print_sign = ["%lld", "%llu", "%f"]
|
|
operations = [
|
|
["+", "ADD"], ["-", "SUB"], ["/", "DIV"], ["*", "MUL"], ["%", "MOD"],
|
|
[">>", "SHR"], ["<<", "SHL"], ["&", "BITAND"], ["|", "BITOR"], ["^", "BITXOR"],
|
|
["~", "BITNOT"], ["==", "EQ"], ["!=", "NEQ"], [">", "GT"], ["<", "LT"], ["||", "OR"],
|
|
[">=", "GTE"], ["<=", "LTE"]
|
|
]
|
|
|
|
def should_skip(T, op):
|
|
if T == "F64":
|
|
if op != "DIV" and op != "SUB" and op != "ADD" and op != "MUL" and op != "EQ" and op != "NEQ"\
|
|
and op != "GT" and op != "LT" and op != "GTE" and op != "LTE":
|
|
return True
|
|
|
|
# This function inserts generated code into the specified file
|
|
# it requires a specific comment to be triggered.
|
|
# Lossing data would be terrible so we also backup that file
|
|
# in case something bad happens
|
|
def update_file(filename, comment_name, data_to_write):
|
|
begin = f"""//
|
|
// *Begin* of {comment_name} generated using code_generating_script.py
|
|
//
|
|
"""
|
|
end = f"""
|
|
//
|
|
// *End* of {comment_name} generated using code_generating_script.py
|
|
//"""
|
|
|
|
with open(filename, 'r+') as file:
|
|
data = file.read()
|
|
file.seek(0)
|
|
|
|
begin_index = data.find(begin)
|
|
end_index = data.find(end)
|
|
if begin_index == -1 or end_index == -1:
|
|
print(f"Error: Couldn't find comment: {comment_name}")
|
|
exit(0)
|
|
|
|
end_index += len(end)
|
|
|
|
with open('backup', 'a') as backup:
|
|
backup.write(f"\n*** FILE = {filename} NAME = {comment_name} ***")
|
|
backup.write(f"\n*** FILE = {filename} NAME = {comment_name} ***")
|
|
backup.write(f"\n*** FILE = {filename} NAME = {comment_name} ***")
|
|
backup.write(data)
|
|
|
|
a_part = data[0:begin_index]
|
|
b_part = data[begin_index:end_index]
|
|
c_part = data[end_index:len(data)]
|
|
|
|
data_to_write = begin + data_to_write + end
|
|
|
|
file.write(a_part + data_to_write + c_part)
|
|
file.truncate()
|
|
|
|
|
|
|
|
if True:
|
|
result = ""
|
|
enum_members = [
|
|
"BC_END_OF_INSTRUCTIONS",
|
|
"BC_POP_STACK",
|
|
"BC_PUSH_STACK",
|
|
"BC_LOAD_CONSTANT",
|
|
"BC_STORE_CONSTANT",
|
|
"BC_CALL",
|
|
"BC_CALL_RETURN",
|
|
]
|
|
|
|
load_store = ["LOAD_FROM_MEMORY", "STORE_TO_MEMORY"]
|
|
for op in load_store:
|
|
for size in sizes:
|
|
enum_members.append(f"BC_{op}{size}")
|
|
|
|
for T in types:
|
|
for _, op in operations:
|
|
if should_skip(T, op):
|
|
continue
|
|
enum_members.append(f"BC_{op}_{T}")
|
|
|
|
result += "enum Operation: U16{\n"
|
|
for i in enum_members:
|
|
result += f" {i},\n"
|
|
result += "};\n"
|
|
|
|
result += "const char *op_name[] = {\n"
|
|
for i in enum_members:
|
|
result += f" \"{i}\",\n"
|
|
result += "};\n"
|
|
update_file("bytecode_interpreter.cpp", "enum", result)
|
|
|
|
|
|
|
|
#
|
|
# Generate switch cases
|
|
#
|
|
if True:
|
|
result = ""
|
|
|
|
for size in sizes:
|
|
result += f"""
|
|
case BC_LOAD_FROM_MEMORY{size}:{{
|
|
U{size} *load_address = R(instr->index_a).pointer_u{size};
|
|
R(instr->index_c).u{size} = *load_address;
|
|
bc_log("load_address[r%u, %llx] dst[r%u] [0x%llx|%lld|%f]", instr->index_a, load_address, instr->index_c, R(instr->index_c).u{size}, R(instr->index_c).u{size}, R(instr->index_c).u{size});
|
|
}}break;
|
|
|
|
case BC_STORE_TO_MEMORY{size}:{{
|
|
U{size} *store_address = R(instr->index_c).pointer_u{size};
|
|
*store_address = R(instr->index_a).u{size};
|
|
bc_log("src[r%u] store_address[r%u, %llx] value_written[0x%llx|%lld|%f]", instr->index_a, instr->index_c, store_address, *store_address, *store_address, *store_address);
|
|
}}break;
|
|
"""
|
|
|
|
for sign, T in zip(print_sign, types):
|
|
t = T.lower()
|
|
|
|
# Generate arithmetic
|
|
for symbol, op_name in operations:
|
|
if should_skip(T, op_name):
|
|
continue
|
|
|
|
###################################
|
|
# Unary operator special case
|
|
if symbol == "~":
|
|
result += f"""
|
|
case BC_{op_name}_{T}:{{
|
|
{T} left = ({T})R(instr->index_a).{t};
|
|
{T} result = {symbol}left;
|
|
{T} *dst = R(instr->index_c).pointer_{t};
|
|
*dst = result;
|
|
bc_log("{symbol} [{sign}] = [{sign}]", left, result);
|
|
}}break;
|
|
"""
|
|
continue
|
|
################################
|
|
|
|
################################
|
|
# Binary operation
|
|
result += f"""
|
|
case BC_{op_name}_{T}:{{
|
|
{T} left = R(instr->index_a).{t};
|
|
{T} right = R(instr->index_b).{t};
|
|
{T} result = left {symbol} right;
|
|
R(instr->index_c).{t} = result;
|
|
bc_log("[r%s, {sign}] {symbol} [r%s, {sign}] = [r%s, {sign}]", instr->index_a, left, instr->index_b, right, instr->index_c, result);
|
|
}}break;
|
|
"""
|
|
################################
|
|
update_file("bytecode_interpreter.cpp", "switch_cases", result)
|
|
|
|
|
|
|
|
|
|
# #
|
|
# # Copy backup to **WINDOWS** clipboard
|
|
# #
|
|
# import subprocess
|
|
# subprocess.run("clip", universal_newlines=True, input=data)
|