Adding debug logging to interpreter

This commit is contained in:
Krzosa Karol
2022-06-21 13:35:20 +02:00
parent a2b06d23d5
commit 3f384a60ec
3 changed files with 287 additions and 137 deletions

View File

@@ -4,7 +4,9 @@ result = """
//
"""
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"],
@@ -23,41 +25,70 @@ def should_skip(T, op):
# Generate enum
#
if False:
enum_members = []
enum_members.append("BC_END_OF_INSTRUCTIONS")
enum_members.append("BC_POP_STACK")
enum_members.append("BC_PUSH_STACK")
enum_members.append("BC_LOAD_CONSTANT")
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
result += f" INS_{op}_{T},\n"
enum_members.append(f"BC_{op}_{T}")
result += "\n"
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"
#
# Generate switch cases
#
if True:
for T in types:
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 INS_{op_name}_{T}:{{
case BC_{op_name}_{T}:{{
{T} left = ({T})b->registers[instr->left].{t};
{T} *dst = b->registers[instr->dst].pointer_{t};
*dst = {symbol}left;
}}break;
"""
continue
# Binary operation
################################
################################
# Binary operation
result += f"""
case INS_{op_name}_{T}:{{
case BC_{op_name}_{T}:{{
{T} left = b->registers[instr->left].{t};
{T} right = b->registers[instr->right].{t};
bc_log("{sign} {symbol} {sign}", left, right);
b->registers[instr->dst].{t} = left {symbol} right; break;
}}break;
"""
################################
result += """
//