142 lines
2.8 KiB
Python
142 lines
2.8 KiB
Python
result = """
|
|
//
|
|
// Generated using code_generating_script.py
|
|
//
|
|
"""
|
|
|
|
types = ["S64", "U64", "F64"]
|
|
operations = [
|
|
["+", "ADD"], ["-", "SUB"], ["/", "DIV"], ["*", "MUL"], ["%", "MOD"],
|
|
[">>", "SHR"], ["<<", "SHL"], ["&", "BITAND"], ["|", "BITOR"], ["|", "BITXOR"],
|
|
["~", "BITNOT"], ["==", "EQ"], ["!=", "NEQ"], [">", "GT"], ["<", "LT"], ["||", "OR"],
|
|
[">=", "GTE"], ["<=", "LTE"]
|
|
]
|
|
enum = ["PUSH"]
|
|
|
|
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
|
|
|
|
#
|
|
# Generate enum
|
|
#
|
|
if True:
|
|
for T in types:
|
|
for op in enum:
|
|
result += f" INS_{op}_{T},\n"
|
|
for _, op in operations:
|
|
if should_skip(T, op):
|
|
continue
|
|
result += f" INS_{op}_{T},\n"
|
|
result += "\n"
|
|
|
|
#
|
|
# Generate utility functions
|
|
#
|
|
if False:
|
|
for T in types:
|
|
t = T.lower()
|
|
result += f"""
|
|
|
|
force_inline void
|
|
ins_push_{t}(Bc *b, {T} value){{
|
|
auto data = b->stack_pointer;
|
|
b->stack_pointer += 2*sizeof(U64);
|
|
|
|
C({T}, data) = value;
|
|
data += sizeof(U64);
|
|
|
|
C(U64, data) = TYPE_{T};
|
|
}}
|
|
|
|
force_inline void
|
|
emit_push_{t}(Bc *bc, {T} emit_value){{
|
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8)+sizeof({T}));
|
|
*instruction = INS_PUSH_{T};
|
|
|
|
{T} *value = ({T} *)(instruction + 1);
|
|
*value = emit_value;
|
|
}}
|
|
"""
|
|
|
|
for symbol, OP in operations:
|
|
if should_skip(T, OP):
|
|
continue
|
|
op = OP.lower()
|
|
result += f"""
|
|
force_inline void
|
|
emit_{op}_{t}(Bc *bc){{
|
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
|
*instruction = INS_{OP}_{T};
|
|
}}
|
|
"""
|
|
|
|
|
|
#
|
|
# Generate switch cases
|
|
#
|
|
if False:
|
|
for T in types:
|
|
t = T.lower()
|
|
|
|
|
|
#
|
|
# Push operation for type
|
|
#
|
|
result += f"""
|
|
case INS_PUSH_{T}:{{
|
|
// Fetch value from the instruction.
|
|
// instructions are tightly packed so we
|
|
// move pointer by the type size
|
|
auto value = ({T} *)b->ins_pointer;
|
|
b->ins_pointer += sizeof({T});
|
|
ins_push_{t}(b, *value);
|
|
}} break;
|
|
"""
|
|
|
|
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}:{{
|
|
{T} l = ins_pop_t(b, {T});
|
|
{T} result = {symbol}l;
|
|
ins_push_{t}(b, result);
|
|
}}break;
|
|
"""
|
|
continue
|
|
|
|
#
|
|
# Binary operation
|
|
#
|
|
result += f"""
|
|
case INS_{op_name}_{T}:{{
|
|
{T} l = ins_pop_t(b, {T});
|
|
{T} r = ins_pop_t(b, {T});
|
|
{T} result = l {symbol} r;
|
|
ins_push_{t}(b, result);
|
|
}}break;
|
|
"""
|
|
|
|
|
|
result += """
|
|
//
|
|
// **End** of generated using code_generating_script.py
|
|
//
|
|
"""
|
|
|
|
|
|
#
|
|
# Copy to **WINDOWS** clipboard
|
|
#
|
|
import subprocess
|
|
subprocess.run("clip", universal_newlines=True, input=result)
|
|
|