Bytecode interpreter init

This commit is contained in:
Krzosa Karol
2022-06-20 19:27:22 +02:00
parent 9f7d51ff02
commit 5a8f36b16a
5 changed files with 514 additions and 1 deletions

68
code_generating_script.py Normal file
View File

@@ -0,0 +1,68 @@
result = """
//
// Generated using code_generating_script.py
//
"""
types = ["S64", "S32", "S16", "S8", "U64", "U32", "U16", "U8", "F32", "F64"]
#
# Generate utility functions
#
if False:
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);
C({T}, data) = value;
data += sizeof(U64);
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};
{T} *value = ({T} *)(instruction + 1);
*value = emit_value;
}}
"""
#
# Generate switch cases
#
if True:
for T in types:
t = T.lower()
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});
stack_push_{t}(b, *value);
}} break;
"""
result += """
//
// **End** of generated using code_generating_script.py
//
"""
#
# Copy to **WINDOWS** clipboard
#
import subprocess
subprocess.run("clip", universal_newlines=True, input=result)