Rewrite VM, only capable to work on 64bit arithmetic

This commit is contained in:
Krzosa Karol
2022-06-20 22:14:46 +02:00
parent fd66781afb
commit 101970f62e
4 changed files with 436 additions and 651 deletions

View File

@@ -4,23 +4,38 @@ result = """
//
"""
types = ["S64", "S32", "S16", "S8", "U64", "U32", "U16", "U8", "F32", "F64"]
operations = [["+", "ADD"], ["-", "SUB"], ["/", "DIV"], ["*", "MUL"], ["%", "MOD"]]
enum = ["ADD", "SUB", "DIV", "MUL", "MOD", "PUSH"]
excludes = [["F64", "MOD"], ["F32", "MOD"]]
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 False:
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 True:
if False:
for T in types:
t = T.lower()
result += f"""
@@ -47,6 +62,8 @@ emit_push_{t}(Bc *bc, {T} emit_value){{
"""
for symbol, OP in operations:
if should_skip(T, OP):
continue
op = OP.lower()
result += f"""
force_inline void
@@ -63,8 +80,42 @@ emit_{op}_{t}(Bc *bc){{
if False:
for T in types:
t = T.lower()
for symbol, op_name in operations:
#
# 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});
@@ -74,17 +125,6 @@ case INS_{op_name}_{T}:{{
}}break;
"""
result += f"""
case INS_PUSH_{T}:{{
// Fetch value from 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;
"""
result += """
//
@@ -92,6 +132,7 @@ result += """
//
"""
#
# Copy to **WINDOWS** clipboard
#