Switching to register based VM
This commit is contained in:
@@ -7,11 +7,10 @@ result = """
|
||||
types = ["S64", "U64", "F64"]
|
||||
operations = [
|
||||
["+", "ADD"], ["-", "SUB"], ["/", "DIV"], ["*", "MUL"], ["%", "MOD"],
|
||||
[">>", "SHR"], ["<<", "SHL"], ["&", "BITAND"], ["|", "BITOR"], ["|", "BITXOR"],
|
||||
[">>", "SHR"], ["<<", "SHL"], ["&", "BITAND"], ["|", "BITOR"], ["^", "BITXOR"],
|
||||
["~", "BITNOT"], ["==", "EQ"], ["!=", "NEQ"], [">", "GT"], ["<", "LT"], ["||", "OR"],
|
||||
[">=", "GTE"], ["<=", "LTE"]
|
||||
]
|
||||
enum = ["PUSH"]
|
||||
|
||||
def should_skip(T, op):
|
||||
if T == "F64":
|
||||
@@ -19,110 +18,44 @@ def should_skip(T, op):
|
||||
and op != "GT" and op != "LT" and op != "GTE" and op != "LTE":
|
||||
return True
|
||||
|
||||
|
||||
#
|
||||
# Generate enum
|
||||
#
|
||||
if True:
|
||||
if False:
|
||||
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:
|
||||
if True:
|
||||
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;
|
||||
"""
|
||||
case INS_{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}:{{
|
||||
{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;
|
||||
case INS_{op_name}_{T}:{{
|
||||
{T} left = b->registers[instr->left].{t};
|
||||
{T} right = b->registers[instr->right].{t};
|
||||
b->registers[instr->dst].{t} = left {symbol} right; break;
|
||||
}}break;
|
||||
"""
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user