Arithmetic ops and pushes for all types

This commit is contained in:
Krzosa Karol
2022-06-20 20:14:34 +02:00
parent 5a8f36b16a
commit fd66781afb
3 changed files with 1084 additions and 361 deletions

View File

@@ -5,44 +5,75 @@ 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"]]
#
# Generate enum
#
if False:
for T in types:
for op in enum:
result += f" INS_{op}_{T},\n"
#
# Generate utility functions
#
if False:
if True:
for T in types:
t = T.lower()
result += f"""
force_inline void
stack_push_{t}(Bc *b, {T} value){{
U64 allocation_size = 2*sizeof(U64);
auto data = (U8 *)arena_push_size(&b->stack, allocation_size);
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({T}, data) = value;
data += sizeof(U64);
C(U64, data) = TYPE_{T};
C(U64, data) = TYPE_{T};
}}
b->stack_pointer += allocation_size;
}}
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};
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;
}}
{T} *value = ({T} *)(instruction + 1);
*value = emit_value;
}}
"""
for symbol, OP in operations:
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 True:
if False:
for T in types:
t = T.lower()
for symbol, op_name in operations:
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 += f"""
case INS_PUSH_{T}:{{
// Fetch value from instruction
@@ -50,10 +81,11 @@ case INS_PUSH_{T}:{{
// move pointer by the type size
auto value = ({T} *)b->ins_pointer;
b->ins_pointer += sizeof({T});
stack_push_{t}(b, *value);
ins_push_{t}(b, *value);
}} break;
"""
result += """
//
// **End** of generated using code_generating_script.py