Arithmetic ops and pushes for all types
This commit is contained in:
@@ -2,22 +2,77 @@
|
|||||||
// Instructions
|
// Instructions
|
||||||
//
|
//
|
||||||
enum{
|
enum{
|
||||||
INS_PUSH_S64,
|
INS_END,
|
||||||
INS_PUSH_S32,
|
|
||||||
INS_PUSH_S16,
|
|
||||||
INS_PUSH_S8 ,
|
|
||||||
INS_PUSH_U64,
|
|
||||||
INS_PUSH_U32,
|
|
||||||
INS_PUSH_U16,
|
|
||||||
INS_PUSH_U8 ,
|
|
||||||
INS_PUSH_F64 ,
|
|
||||||
INS_PUSH_F32 ,
|
|
||||||
INS_POP,
|
INS_POP,
|
||||||
|
|
||||||
INS_ADD,
|
//
|
||||||
INS_SUB,
|
// Generated using code_generating_script.py
|
||||||
INS_MUL,
|
//
|
||||||
INS_DIV,
|
INS_ADD_S64,
|
||||||
|
INS_SUB_S64,
|
||||||
|
INS_DIV_S64,
|
||||||
|
INS_MUL_S64,
|
||||||
|
INS_MOD_S64,
|
||||||
|
INS_PUSH_S64,
|
||||||
|
INS_ADD_S32,
|
||||||
|
INS_SUB_S32,
|
||||||
|
INS_DIV_S32,
|
||||||
|
INS_MUL_S32,
|
||||||
|
INS_MOD_S32,
|
||||||
|
INS_PUSH_S32,
|
||||||
|
INS_ADD_S16,
|
||||||
|
INS_SUB_S16,
|
||||||
|
INS_DIV_S16,
|
||||||
|
INS_MUL_S16,
|
||||||
|
INS_MOD_S16,
|
||||||
|
INS_PUSH_S16,
|
||||||
|
INS_ADD_S8,
|
||||||
|
INS_SUB_S8,
|
||||||
|
INS_DIV_S8,
|
||||||
|
INS_MUL_S8,
|
||||||
|
INS_MOD_S8,
|
||||||
|
INS_PUSH_S8,
|
||||||
|
INS_ADD_U64,
|
||||||
|
INS_SUB_U64,
|
||||||
|
INS_DIV_U64,
|
||||||
|
INS_MUL_U64,
|
||||||
|
INS_MOD_U64,
|
||||||
|
INS_PUSH_U64,
|
||||||
|
INS_ADD_U32,
|
||||||
|
INS_SUB_U32,
|
||||||
|
INS_DIV_U32,
|
||||||
|
INS_MUL_U32,
|
||||||
|
INS_MOD_U32,
|
||||||
|
INS_PUSH_U32,
|
||||||
|
INS_ADD_U16,
|
||||||
|
INS_SUB_U16,
|
||||||
|
INS_DIV_U16,
|
||||||
|
INS_MUL_U16,
|
||||||
|
INS_MOD_U16,
|
||||||
|
INS_PUSH_U16,
|
||||||
|
INS_ADD_U8,
|
||||||
|
INS_SUB_U8,
|
||||||
|
INS_DIV_U8,
|
||||||
|
INS_MUL_U8,
|
||||||
|
INS_MOD_U8,
|
||||||
|
INS_PUSH_U8,
|
||||||
|
INS_ADD_F32,
|
||||||
|
INS_SUB_F32,
|
||||||
|
INS_DIV_F32,
|
||||||
|
INS_MUL_F32,
|
||||||
|
INS_MOD_F32,
|
||||||
|
INS_PUSH_F32,
|
||||||
|
INS_ADD_F64,
|
||||||
|
INS_SUB_F64,
|
||||||
|
INS_DIV_F64,
|
||||||
|
INS_MUL_F64,
|
||||||
|
INS_MOD_F64,
|
||||||
|
INS_PUSH_F64,
|
||||||
|
|
||||||
|
//
|
||||||
|
// **End** of generated using code_generating_script.py
|
||||||
|
//
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -50,9 +105,8 @@ create_bytecode_interp(){
|
|||||||
arena_init(&b.stack, "Bytecode stack"_s);
|
arena_init(&b.stack, "Bytecode stack"_s);
|
||||||
b.stack.alignment = 8;
|
b.stack.alignment = 8;
|
||||||
|
|
||||||
// Commit
|
// Setup a 4 kilobyte stack
|
||||||
arena_push_size(&b.stack, 16);
|
arena_push_size(&b.stack, kib(4));
|
||||||
arena_clear(&b.stack);
|
|
||||||
b.stack_pointer = b.stack_bottom = b.stack.memory.data;
|
b.stack_pointer = b.stack_bottom = b.stack.memory.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,12 +119,20 @@ emit_pop(Bc *bc){
|
|||||||
*instruction = INS_POP;
|
*instruction = INS_POP;
|
||||||
}
|
}
|
||||||
|
|
||||||
force_inline void *
|
force_inline void
|
||||||
stack_pop(Bc *b){
|
emit_end(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ins_pop_t(b, T) (*((T *)ins_pop(b)))
|
||||||
|
static void *
|
||||||
|
ins_pop(Bc *b){
|
||||||
assert_msg(b->stack_pointer != b->stack_bottom, "Reached bottom of bytecode interpreter stack");
|
assert_msg(b->stack_pointer != b->stack_bottom, "Reached bottom of bytecode interpreter stack");
|
||||||
b->stack_pointer -= sizeof(U64);
|
b->stack_pointer -= sizeof(U64);
|
||||||
// @warning we don't do anything with type for now
|
// @warning we don't do anything with type for now
|
||||||
Ast_Type_Kind *type = (Ast_Type_Kind *)b->stack_pointer;
|
Ast_Type_Kind *type = (Ast_Type_Kind *)b->stack_pointer;
|
||||||
|
unused(type);
|
||||||
b->stack_pointer -= sizeof(U64);
|
b->stack_pointer -= sizeof(U64);
|
||||||
return b->stack_pointer;
|
return b->stack_pointer;
|
||||||
}
|
}
|
||||||
@@ -82,23 +144,100 @@ run_bytecode_interp(Bc *b){
|
|||||||
switch(instruction){
|
switch(instruction){
|
||||||
|
|
||||||
case INS_POP:{
|
case INS_POP:{
|
||||||
void *value = stack_pop(b);
|
void *value = ins_pop(b);
|
||||||
S64 *s64 = (S64 *)value;
|
S64 *s64 = (S64 *)value;
|
||||||
F64 *f64 = (F64 *)value;
|
F64 *f64 = (F64 *)value;
|
||||||
U64 *u64 = (U64 *)value;
|
U64 *u64 = (U64 *)value;
|
||||||
|
unused(s64);
|
||||||
|
unused(f64);
|
||||||
|
unused(u64);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case INS_END:{
|
||||||
|
goto interp_loop_breakout;
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Generated using code_generating_script.py
|
// Generated using code_generating_script.py
|
||||||
//
|
//
|
||||||
|
|
||||||
|
case INS_ADD_S64:{
|
||||||
|
S64 l = ins_pop_t(b, S64);
|
||||||
|
S64 r = ins_pop_t(b, S64);
|
||||||
|
S64 result = l + r;
|
||||||
|
ins_push_s64(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_SUB_S64:{
|
||||||
|
S64 l = ins_pop_t(b, S64);
|
||||||
|
S64 r = ins_pop_t(b, S64);
|
||||||
|
S64 result = l - r;
|
||||||
|
ins_push_s64(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_DIV_S64:{
|
||||||
|
S64 l = ins_pop_t(b, S64);
|
||||||
|
S64 r = ins_pop_t(b, S64);
|
||||||
|
S64 result = l / r;
|
||||||
|
ins_push_s64(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MUL_S64:{
|
||||||
|
S64 l = ins_pop_t(b, S64);
|
||||||
|
S64 r = ins_pop_t(b, S64);
|
||||||
|
S64 result = l * r;
|
||||||
|
ins_push_s64(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MOD_S64:{
|
||||||
|
S64 l = ins_pop_t(b, S64);
|
||||||
|
S64 r = ins_pop_t(b, S64);
|
||||||
|
S64 result = l % r;
|
||||||
|
ins_push_s64(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
case INS_PUSH_S64:{
|
case INS_PUSH_S64:{
|
||||||
// Fetch value from instruction
|
// Fetch value from instruction
|
||||||
// instructions are tightly packed so we
|
// instructions are tightly packed so we
|
||||||
// move pointer by the type size
|
// move pointer by the type size
|
||||||
auto value = (S64 *)b->ins_pointer;
|
auto value = (S64 *)b->ins_pointer;
|
||||||
b->ins_pointer += sizeof(S64);
|
b->ins_pointer += sizeof(S64);
|
||||||
stack_push_s64(b, *value);
|
ins_push_s64(b, *value);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case INS_ADD_S32:{
|
||||||
|
S32 l = ins_pop_t(b, S32);
|
||||||
|
S32 r = ins_pop_t(b, S32);
|
||||||
|
S32 result = l + r;
|
||||||
|
ins_push_s32(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_SUB_S32:{
|
||||||
|
S32 l = ins_pop_t(b, S32);
|
||||||
|
S32 r = ins_pop_t(b, S32);
|
||||||
|
S32 result = l - r;
|
||||||
|
ins_push_s32(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_DIV_S32:{
|
||||||
|
S32 l = ins_pop_t(b, S32);
|
||||||
|
S32 r = ins_pop_t(b, S32);
|
||||||
|
S32 result = l / r;
|
||||||
|
ins_push_s32(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MUL_S32:{
|
||||||
|
S32 l = ins_pop_t(b, S32);
|
||||||
|
S32 r = ins_pop_t(b, S32);
|
||||||
|
S32 result = l * r;
|
||||||
|
ins_push_s32(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MOD_S32:{
|
||||||
|
S32 l = ins_pop_t(b, S32);
|
||||||
|
S32 r = ins_pop_t(b, S32);
|
||||||
|
S32 result = l % r;
|
||||||
|
ins_push_s32(b, result);
|
||||||
}break;
|
}break;
|
||||||
|
|
||||||
case INS_PUSH_S32:{
|
case INS_PUSH_S32:{
|
||||||
@@ -107,7 +246,42 @@ run_bytecode_interp(Bc *b){
|
|||||||
// move pointer by the type size
|
// move pointer by the type size
|
||||||
auto value = (S32 *)b->ins_pointer;
|
auto value = (S32 *)b->ins_pointer;
|
||||||
b->ins_pointer += sizeof(S32);
|
b->ins_pointer += sizeof(S32);
|
||||||
stack_push_s32(b, *value);
|
ins_push_s32(b, *value);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case INS_ADD_S16:{
|
||||||
|
S16 l = ins_pop_t(b, S16);
|
||||||
|
S16 r = ins_pop_t(b, S16);
|
||||||
|
S16 result = l + r;
|
||||||
|
ins_push_s16(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_SUB_S16:{
|
||||||
|
S16 l = ins_pop_t(b, S16);
|
||||||
|
S16 r = ins_pop_t(b, S16);
|
||||||
|
S16 result = l - r;
|
||||||
|
ins_push_s16(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_DIV_S16:{
|
||||||
|
S16 l = ins_pop_t(b, S16);
|
||||||
|
S16 r = ins_pop_t(b, S16);
|
||||||
|
S16 result = l / r;
|
||||||
|
ins_push_s16(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MUL_S16:{
|
||||||
|
S16 l = ins_pop_t(b, S16);
|
||||||
|
S16 r = ins_pop_t(b, S16);
|
||||||
|
S16 result = l * r;
|
||||||
|
ins_push_s16(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MOD_S16:{
|
||||||
|
S16 l = ins_pop_t(b, S16);
|
||||||
|
S16 r = ins_pop_t(b, S16);
|
||||||
|
S16 result = l % r;
|
||||||
|
ins_push_s16(b, result);
|
||||||
}break;
|
}break;
|
||||||
|
|
||||||
case INS_PUSH_S16:{
|
case INS_PUSH_S16:{
|
||||||
@@ -116,7 +290,42 @@ run_bytecode_interp(Bc *b){
|
|||||||
// move pointer by the type size
|
// move pointer by the type size
|
||||||
auto value = (S16 *)b->ins_pointer;
|
auto value = (S16 *)b->ins_pointer;
|
||||||
b->ins_pointer += sizeof(S16);
|
b->ins_pointer += sizeof(S16);
|
||||||
stack_push_s16(b, *value);
|
ins_push_s16(b, *value);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case INS_ADD_S8:{
|
||||||
|
S8 l = ins_pop_t(b, S8);
|
||||||
|
S8 r = ins_pop_t(b, S8);
|
||||||
|
S8 result = l + r;
|
||||||
|
ins_push_s8(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_SUB_S8:{
|
||||||
|
S8 l = ins_pop_t(b, S8);
|
||||||
|
S8 r = ins_pop_t(b, S8);
|
||||||
|
S8 result = l - r;
|
||||||
|
ins_push_s8(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_DIV_S8:{
|
||||||
|
S8 l = ins_pop_t(b, S8);
|
||||||
|
S8 r = ins_pop_t(b, S8);
|
||||||
|
S8 result = l / r;
|
||||||
|
ins_push_s8(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MUL_S8:{
|
||||||
|
S8 l = ins_pop_t(b, S8);
|
||||||
|
S8 r = ins_pop_t(b, S8);
|
||||||
|
S8 result = l * r;
|
||||||
|
ins_push_s8(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MOD_S8:{
|
||||||
|
S8 l = ins_pop_t(b, S8);
|
||||||
|
S8 r = ins_pop_t(b, S8);
|
||||||
|
S8 result = l % r;
|
||||||
|
ins_push_s8(b, result);
|
||||||
}break;
|
}break;
|
||||||
|
|
||||||
case INS_PUSH_S8:{
|
case INS_PUSH_S8:{
|
||||||
@@ -125,7 +334,42 @@ run_bytecode_interp(Bc *b){
|
|||||||
// move pointer by the type size
|
// move pointer by the type size
|
||||||
auto value = (S8 *)b->ins_pointer;
|
auto value = (S8 *)b->ins_pointer;
|
||||||
b->ins_pointer += sizeof(S8);
|
b->ins_pointer += sizeof(S8);
|
||||||
stack_push_s8(b, *value);
|
ins_push_s8(b, *value);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case INS_ADD_U64:{
|
||||||
|
U64 l = ins_pop_t(b, U64);
|
||||||
|
U64 r = ins_pop_t(b, U64);
|
||||||
|
U64 result = l + r;
|
||||||
|
ins_push_u64(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_SUB_U64:{
|
||||||
|
U64 l = ins_pop_t(b, U64);
|
||||||
|
U64 r = ins_pop_t(b, U64);
|
||||||
|
U64 result = l - r;
|
||||||
|
ins_push_u64(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_DIV_U64:{
|
||||||
|
U64 l = ins_pop_t(b, U64);
|
||||||
|
U64 r = ins_pop_t(b, U64);
|
||||||
|
U64 result = l / r;
|
||||||
|
ins_push_u64(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MUL_U64:{
|
||||||
|
U64 l = ins_pop_t(b, U64);
|
||||||
|
U64 r = ins_pop_t(b, U64);
|
||||||
|
U64 result = l * r;
|
||||||
|
ins_push_u64(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MOD_U64:{
|
||||||
|
U64 l = ins_pop_t(b, U64);
|
||||||
|
U64 r = ins_pop_t(b, U64);
|
||||||
|
U64 result = l % r;
|
||||||
|
ins_push_u64(b, result);
|
||||||
}break;
|
}break;
|
||||||
|
|
||||||
case INS_PUSH_U64:{
|
case INS_PUSH_U64:{
|
||||||
@@ -134,7 +378,42 @@ run_bytecode_interp(Bc *b){
|
|||||||
// move pointer by the type size
|
// move pointer by the type size
|
||||||
auto value = (U64 *)b->ins_pointer;
|
auto value = (U64 *)b->ins_pointer;
|
||||||
b->ins_pointer += sizeof(U64);
|
b->ins_pointer += sizeof(U64);
|
||||||
stack_push_u64(b, *value);
|
ins_push_u64(b, *value);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case INS_ADD_U32:{
|
||||||
|
U32 l = ins_pop_t(b, U32);
|
||||||
|
U32 r = ins_pop_t(b, U32);
|
||||||
|
U32 result = l + r;
|
||||||
|
ins_push_u32(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_SUB_U32:{
|
||||||
|
U32 l = ins_pop_t(b, U32);
|
||||||
|
U32 r = ins_pop_t(b, U32);
|
||||||
|
U32 result = l - r;
|
||||||
|
ins_push_u32(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_DIV_U32:{
|
||||||
|
U32 l = ins_pop_t(b, U32);
|
||||||
|
U32 r = ins_pop_t(b, U32);
|
||||||
|
U32 result = l / r;
|
||||||
|
ins_push_u32(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MUL_U32:{
|
||||||
|
U32 l = ins_pop_t(b, U32);
|
||||||
|
U32 r = ins_pop_t(b, U32);
|
||||||
|
U32 result = l * r;
|
||||||
|
ins_push_u32(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MOD_U32:{
|
||||||
|
U32 l = ins_pop_t(b, U32);
|
||||||
|
U32 r = ins_pop_t(b, U32);
|
||||||
|
U32 result = l % r;
|
||||||
|
ins_push_u32(b, result);
|
||||||
}break;
|
}break;
|
||||||
|
|
||||||
case INS_PUSH_U32:{
|
case INS_PUSH_U32:{
|
||||||
@@ -143,7 +422,42 @@ run_bytecode_interp(Bc *b){
|
|||||||
// move pointer by the type size
|
// move pointer by the type size
|
||||||
auto value = (U32 *)b->ins_pointer;
|
auto value = (U32 *)b->ins_pointer;
|
||||||
b->ins_pointer += sizeof(U32);
|
b->ins_pointer += sizeof(U32);
|
||||||
stack_push_u32(b, *value);
|
ins_push_u32(b, *value);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case INS_ADD_U16:{
|
||||||
|
U16 l = ins_pop_t(b, U16);
|
||||||
|
U16 r = ins_pop_t(b, U16);
|
||||||
|
U16 result = l + r;
|
||||||
|
ins_push_u16(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_SUB_U16:{
|
||||||
|
U16 l = ins_pop_t(b, U16);
|
||||||
|
U16 r = ins_pop_t(b, U16);
|
||||||
|
U16 result = l - r;
|
||||||
|
ins_push_u16(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_DIV_U16:{
|
||||||
|
U16 l = ins_pop_t(b, U16);
|
||||||
|
U16 r = ins_pop_t(b, U16);
|
||||||
|
U16 result = l / r;
|
||||||
|
ins_push_u16(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MUL_U16:{
|
||||||
|
U16 l = ins_pop_t(b, U16);
|
||||||
|
U16 r = ins_pop_t(b, U16);
|
||||||
|
U16 result = l * r;
|
||||||
|
ins_push_u16(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MOD_U16:{
|
||||||
|
U16 l = ins_pop_t(b, U16);
|
||||||
|
U16 r = ins_pop_t(b, U16);
|
||||||
|
U16 result = l % r;
|
||||||
|
ins_push_u16(b, result);
|
||||||
}break;
|
}break;
|
||||||
|
|
||||||
case INS_PUSH_U16:{
|
case INS_PUSH_U16:{
|
||||||
@@ -152,7 +466,42 @@ run_bytecode_interp(Bc *b){
|
|||||||
// move pointer by the type size
|
// move pointer by the type size
|
||||||
auto value = (U16 *)b->ins_pointer;
|
auto value = (U16 *)b->ins_pointer;
|
||||||
b->ins_pointer += sizeof(U16);
|
b->ins_pointer += sizeof(U16);
|
||||||
stack_push_u16(b, *value);
|
ins_push_u16(b, *value);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case INS_ADD_U8:{
|
||||||
|
U8 l = ins_pop_t(b, U8);
|
||||||
|
U8 r = ins_pop_t(b, U8);
|
||||||
|
U8 result = l + r;
|
||||||
|
ins_push_u8(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_SUB_U8:{
|
||||||
|
U8 l = ins_pop_t(b, U8);
|
||||||
|
U8 r = ins_pop_t(b, U8);
|
||||||
|
U8 result = l - r;
|
||||||
|
ins_push_u8(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_DIV_U8:{
|
||||||
|
U8 l = ins_pop_t(b, U8);
|
||||||
|
U8 r = ins_pop_t(b, U8);
|
||||||
|
U8 result = l / r;
|
||||||
|
ins_push_u8(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MUL_U8:{
|
||||||
|
U8 l = ins_pop_t(b, U8);
|
||||||
|
U8 r = ins_pop_t(b, U8);
|
||||||
|
U8 result = l * r;
|
||||||
|
ins_push_u8(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MOD_U8:{
|
||||||
|
U8 l = ins_pop_t(b, U8);
|
||||||
|
U8 r = ins_pop_t(b, U8);
|
||||||
|
U8 result = l % r;
|
||||||
|
ins_push_u8(b, result);
|
||||||
}break;
|
}break;
|
||||||
|
|
||||||
case INS_PUSH_U8:{
|
case INS_PUSH_U8:{
|
||||||
@@ -161,7 +510,35 @@ run_bytecode_interp(Bc *b){
|
|||||||
// move pointer by the type size
|
// move pointer by the type size
|
||||||
auto value = (U8 *)b->ins_pointer;
|
auto value = (U8 *)b->ins_pointer;
|
||||||
b->ins_pointer += sizeof(U8);
|
b->ins_pointer += sizeof(U8);
|
||||||
stack_push_u8(b, *value);
|
ins_push_u8(b, *value);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case INS_ADD_F32:{
|
||||||
|
F32 l = ins_pop_t(b, F32);
|
||||||
|
F32 r = ins_pop_t(b, F32);
|
||||||
|
F32 result = l + r;
|
||||||
|
ins_push_f32(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_SUB_F32:{
|
||||||
|
F32 l = ins_pop_t(b, F32);
|
||||||
|
F32 r = ins_pop_t(b, F32);
|
||||||
|
F32 result = l - r;
|
||||||
|
ins_push_f32(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_DIV_F32:{
|
||||||
|
F32 l = ins_pop_t(b, F32);
|
||||||
|
F32 r = ins_pop_t(b, F32);
|
||||||
|
F32 result = l / r;
|
||||||
|
ins_push_f32(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MUL_F32:{
|
||||||
|
F32 l = ins_pop_t(b, F32);
|
||||||
|
F32 r = ins_pop_t(b, F32);
|
||||||
|
F32 result = l * r;
|
||||||
|
ins_push_f32(b, result);
|
||||||
}break;
|
}break;
|
||||||
|
|
||||||
case INS_PUSH_F32:{
|
case INS_PUSH_F32:{
|
||||||
@@ -170,7 +547,35 @@ run_bytecode_interp(Bc *b){
|
|||||||
// move pointer by the type size
|
// move pointer by the type size
|
||||||
auto value = (F32 *)b->ins_pointer;
|
auto value = (F32 *)b->ins_pointer;
|
||||||
b->ins_pointer += sizeof(F32);
|
b->ins_pointer += sizeof(F32);
|
||||||
stack_push_f32(b, *value);
|
ins_push_f32(b, *value);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case INS_ADD_F64:{
|
||||||
|
F64 l = ins_pop_t(b, F64);
|
||||||
|
F64 r = ins_pop_t(b, F64);
|
||||||
|
F64 result = l + r;
|
||||||
|
ins_push_f64(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_SUB_F64:{
|
||||||
|
F64 l = ins_pop_t(b, F64);
|
||||||
|
F64 r = ins_pop_t(b, F64);
|
||||||
|
F64 result = l - r;
|
||||||
|
ins_push_f64(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_DIV_F64:{
|
||||||
|
F64 l = ins_pop_t(b, F64);
|
||||||
|
F64 r = ins_pop_t(b, F64);
|
||||||
|
F64 result = l / r;
|
||||||
|
ins_push_f64(b, result);
|
||||||
|
}break;
|
||||||
|
|
||||||
|
case INS_MUL_F64:{
|
||||||
|
F64 l = ins_pop_t(b, F64);
|
||||||
|
F64 r = ins_pop_t(b, F64);
|
||||||
|
F64 result = l * r;
|
||||||
|
ins_push_f64(b, result);
|
||||||
}break;
|
}break;
|
||||||
|
|
||||||
case INS_PUSH_F64:{
|
case INS_PUSH_F64:{
|
||||||
@@ -179,7 +584,7 @@ run_bytecode_interp(Bc *b){
|
|||||||
// move pointer by the type size
|
// move pointer by the type size
|
||||||
auto value = (F64 *)b->ins_pointer;
|
auto value = (F64 *)b->ins_pointer;
|
||||||
b->ins_pointer += sizeof(F64);
|
b->ins_pointer += sizeof(F64);
|
||||||
stack_push_f64(b, *value);
|
ins_push_f64(b, *value);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -188,21 +593,23 @@ run_bytecode_interp(Bc *b){
|
|||||||
|
|
||||||
default: invalid_codepath;
|
default: invalid_codepath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
interp_loop_breakout:;
|
||||||
}
|
}
|
||||||
|
|
||||||
function void
|
function void
|
||||||
test_interpreter(){
|
test_interpreter(){
|
||||||
Bc b = create_bytecode_interp();
|
Bc b = create_bytecode_interp();
|
||||||
emit_push_s64(&b, 1);
|
emit_push_s64(&b, 1);
|
||||||
emit_push_f64(&b, 2.42f);
|
|
||||||
emit_push_s8(&b, 3);
|
emit_push_s8(&b, 3);
|
||||||
emit_push_u16(&b, 4);
|
emit_add_s64(&b);
|
||||||
emit_pop(&b);
|
|
||||||
emit_pop(&b);
|
|
||||||
emit_pop(&b);
|
|
||||||
emit_pop(&b);
|
emit_pop(&b);
|
||||||
|
// emit_push_f64(&b, 2.42f);
|
||||||
|
// emit_push_u16(&b, 4);
|
||||||
|
// emit_pop(&b);
|
||||||
|
// emit_pop(&b);
|
||||||
|
// emit_pop(&b);
|
||||||
|
// emit_pop(&b);
|
||||||
|
emit_end(&b);
|
||||||
run_bytecode_interp(&b);
|
run_bytecode_interp(&b);
|
||||||
}
|
}
|
||||||
@@ -5,16 +5,14 @@
|
|||||||
|
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
stack_push_s64(Bc *b, S64 value){
|
ins_push_s64(Bc *b, S64 value){
|
||||||
U64 allocation_size = 2*sizeof(U64);
|
auto data = b->stack_pointer;
|
||||||
auto data = (U8 *)arena_push_size(&b->stack, allocation_size);
|
b->stack_pointer += 2*sizeof(U64);
|
||||||
|
|
||||||
C(S64, data) = value;
|
C(S64, data) = value;
|
||||||
data += sizeof(U64);
|
data += sizeof(U64);
|
||||||
|
|
||||||
C(U64, data) = TYPE_S64;
|
C(U64, data) = TYPE_S64;
|
||||||
|
|
||||||
b->stack_pointer += allocation_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
@@ -26,18 +24,46 @@
|
|||||||
*value = emit_value;
|
*value = emit_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_add_s64(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_ADD_S64;
|
||||||
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
stack_push_s32(Bc *b, S32 value){
|
emit_sub_s64(Bc *bc){
|
||||||
U64 allocation_size = 2*sizeof(U64);
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
auto data = (U8 *)arena_push_size(&b->stack, allocation_size);
|
*instruction = INS_SUB_S64;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_div_s64(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_DIV_S64;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mul_s64(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MUL_S64;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mod_s64(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MOD_S64;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
ins_push_s32(Bc *b, S32 value){
|
||||||
|
auto data = b->stack_pointer;
|
||||||
|
b->stack_pointer += 2*sizeof(U64);
|
||||||
|
|
||||||
C(S32, data) = value;
|
C(S32, data) = value;
|
||||||
data += sizeof(U64);
|
data += sizeof(U64);
|
||||||
|
|
||||||
C(U64, data) = TYPE_S32;
|
C(U64, data) = TYPE_S32;
|
||||||
|
|
||||||
b->stack_pointer += allocation_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
@@ -49,18 +75,46 @@
|
|||||||
*value = emit_value;
|
*value = emit_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_add_s32(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_ADD_S32;
|
||||||
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
stack_push_s16(Bc *b, S16 value){
|
emit_sub_s32(Bc *bc){
|
||||||
U64 allocation_size = 2*sizeof(U64);
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
auto data = (U8 *)arena_push_size(&b->stack, allocation_size);
|
*instruction = INS_SUB_S32;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_div_s32(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_DIV_S32;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mul_s32(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MUL_S32;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mod_s32(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MOD_S32;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
ins_push_s16(Bc *b, S16 value){
|
||||||
|
auto data = b->stack_pointer;
|
||||||
|
b->stack_pointer += 2*sizeof(U64);
|
||||||
|
|
||||||
C(S16, data) = value;
|
C(S16, data) = value;
|
||||||
data += sizeof(U64);
|
data += sizeof(U64);
|
||||||
|
|
||||||
C(U64, data) = TYPE_S16;
|
C(U64, data) = TYPE_S16;
|
||||||
|
|
||||||
b->stack_pointer += allocation_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
@@ -72,18 +126,46 @@
|
|||||||
*value = emit_value;
|
*value = emit_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_add_s16(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_ADD_S16;
|
||||||
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
stack_push_s8(Bc *b, S8 value){
|
emit_sub_s16(Bc *bc){
|
||||||
U64 allocation_size = 2*sizeof(U64);
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
auto data = (U8 *)arena_push_size(&b->stack, allocation_size);
|
*instruction = INS_SUB_S16;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_div_s16(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_DIV_S16;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mul_s16(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MUL_S16;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mod_s16(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MOD_S16;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
ins_push_s8(Bc *b, S8 value){
|
||||||
|
auto data = b->stack_pointer;
|
||||||
|
b->stack_pointer += 2*sizeof(U64);
|
||||||
|
|
||||||
C(S8, data) = value;
|
C(S8, data) = value;
|
||||||
data += sizeof(U64);
|
data += sizeof(U64);
|
||||||
|
|
||||||
C(U64, data) = TYPE_S8;
|
C(U64, data) = TYPE_S8;
|
||||||
|
|
||||||
b->stack_pointer += allocation_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
@@ -95,18 +177,46 @@
|
|||||||
*value = emit_value;
|
*value = emit_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_add_s8(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_ADD_S8;
|
||||||
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
stack_push_u64(Bc *b, U64 value){
|
emit_sub_s8(Bc *bc){
|
||||||
U64 allocation_size = 2*sizeof(U64);
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
auto data = (U8 *)arena_push_size(&b->stack, allocation_size);
|
*instruction = INS_SUB_S8;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_div_s8(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_DIV_S8;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mul_s8(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MUL_S8;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mod_s8(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MOD_S8;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
ins_push_u64(Bc *b, U64 value){
|
||||||
|
auto data = b->stack_pointer;
|
||||||
|
b->stack_pointer += 2*sizeof(U64);
|
||||||
|
|
||||||
C(U64, data) = value;
|
C(U64, data) = value;
|
||||||
data += sizeof(U64);
|
data += sizeof(U64);
|
||||||
|
|
||||||
C(U64, data) = TYPE_U64;
|
C(U64, data) = TYPE_U64;
|
||||||
|
|
||||||
b->stack_pointer += allocation_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
@@ -118,18 +228,46 @@
|
|||||||
*value = emit_value;
|
*value = emit_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_add_u64(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_ADD_U64;
|
||||||
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
stack_push_u32(Bc *b, U32 value){
|
emit_sub_u64(Bc *bc){
|
||||||
U64 allocation_size = 2*sizeof(U64);
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
auto data = (U8 *)arena_push_size(&b->stack, allocation_size);
|
*instruction = INS_SUB_U64;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_div_u64(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_DIV_U64;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mul_u64(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MUL_U64;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mod_u64(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MOD_U64;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
ins_push_u32(Bc *b, U32 value){
|
||||||
|
auto data = b->stack_pointer;
|
||||||
|
b->stack_pointer += 2*sizeof(U64);
|
||||||
|
|
||||||
C(U32, data) = value;
|
C(U32, data) = value;
|
||||||
data += sizeof(U64);
|
data += sizeof(U64);
|
||||||
|
|
||||||
C(U64, data) = TYPE_U32;
|
C(U64, data) = TYPE_U32;
|
||||||
|
|
||||||
b->stack_pointer += allocation_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
@@ -141,18 +279,46 @@
|
|||||||
*value = emit_value;
|
*value = emit_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_add_u32(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_ADD_U32;
|
||||||
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
stack_push_u16(Bc *b, U16 value){
|
emit_sub_u32(Bc *bc){
|
||||||
U64 allocation_size = 2*sizeof(U64);
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
auto data = (U8 *)arena_push_size(&b->stack, allocation_size);
|
*instruction = INS_SUB_U32;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_div_u32(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_DIV_U32;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mul_u32(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MUL_U32;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mod_u32(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MOD_U32;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
ins_push_u16(Bc *b, U16 value){
|
||||||
|
auto data = b->stack_pointer;
|
||||||
|
b->stack_pointer += 2*sizeof(U64);
|
||||||
|
|
||||||
C(U16, data) = value;
|
C(U16, data) = value;
|
||||||
data += sizeof(U64);
|
data += sizeof(U64);
|
||||||
|
|
||||||
C(U64, data) = TYPE_U16;
|
C(U64, data) = TYPE_U16;
|
||||||
|
|
||||||
b->stack_pointer += allocation_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
@@ -164,18 +330,46 @@
|
|||||||
*value = emit_value;
|
*value = emit_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_add_u16(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_ADD_U16;
|
||||||
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
stack_push_u8(Bc *b, U8 value){
|
emit_sub_u16(Bc *bc){
|
||||||
U64 allocation_size = 2*sizeof(U64);
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
auto data = (U8 *)arena_push_size(&b->stack, allocation_size);
|
*instruction = INS_SUB_U16;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_div_u16(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_DIV_U16;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mul_u16(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MUL_U16;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mod_u16(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MOD_U16;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
ins_push_u8(Bc *b, U8 value){
|
||||||
|
auto data = b->stack_pointer;
|
||||||
|
b->stack_pointer += 2*sizeof(U64);
|
||||||
|
|
||||||
C(U8, data) = value;
|
C(U8, data) = value;
|
||||||
data += sizeof(U64);
|
data += sizeof(U64);
|
||||||
|
|
||||||
C(U64, data) = TYPE_U8;
|
C(U64, data) = TYPE_U8;
|
||||||
|
|
||||||
b->stack_pointer += allocation_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
@@ -187,18 +381,46 @@
|
|||||||
*value = emit_value;
|
*value = emit_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_add_u8(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_ADD_U8;
|
||||||
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
stack_push_f32(Bc *b, F32 value){
|
emit_sub_u8(Bc *bc){
|
||||||
U64 allocation_size = 2*sizeof(U64);
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
auto data = (U8 *)arena_push_size(&b->stack, allocation_size);
|
*instruction = INS_SUB_U8;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_div_u8(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_DIV_U8;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mul_u8(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MUL_U8;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mod_u8(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MOD_U8;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
ins_push_f32(Bc *b, F32 value){
|
||||||
|
auto data = b->stack_pointer;
|
||||||
|
b->stack_pointer += 2*sizeof(U64);
|
||||||
|
|
||||||
C(F32, data) = value;
|
C(F32, data) = value;
|
||||||
data += sizeof(U64);
|
data += sizeof(U64);
|
||||||
|
|
||||||
C(U64, data) = TYPE_F32;
|
C(U64, data) = TYPE_F32;
|
||||||
|
|
||||||
b->stack_pointer += allocation_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
@@ -210,18 +432,46 @@
|
|||||||
*value = emit_value;
|
*value = emit_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_add_f32(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_ADD_F32;
|
||||||
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
stack_push_f64(Bc *b, F64 value){
|
emit_sub_f32(Bc *bc){
|
||||||
U64 allocation_size = 2*sizeof(U64);
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
auto data = (U8 *)arena_push_size(&b->stack, allocation_size);
|
*instruction = INS_SUB_F32;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_div_f32(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_DIV_F32;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mul_f32(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MUL_F32;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mod_f32(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MOD_F32;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
ins_push_f64(Bc *b, F64 value){
|
||||||
|
auto data = b->stack_pointer;
|
||||||
|
b->stack_pointer += 2*sizeof(U64);
|
||||||
|
|
||||||
C(F64, data) = value;
|
C(F64, data) = value;
|
||||||
data += sizeof(U64);
|
data += sizeof(U64);
|
||||||
|
|
||||||
C(U64, data) = TYPE_F64;
|
C(U64, data) = TYPE_F64;
|
||||||
|
|
||||||
b->stack_pointer += allocation_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
@@ -232,3 +482,37 @@
|
|||||||
F64 *value = (F64 *)(instruction + 1);
|
F64 *value = (F64 *)(instruction + 1);
|
||||||
*value = emit_value;
|
*value = emit_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_add_f64(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_ADD_F64;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_sub_f64(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_SUB_F64;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_div_f64(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_DIV_F64;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mul_f64(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MUL_F64;
|
||||||
|
}
|
||||||
|
|
||||||
|
force_inline void
|
||||||
|
emit_mod_f64(Bc *bc){
|
||||||
|
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
|
||||||
|
*instruction = INS_MOD_F64;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// **End** of generated using code_generating_script.py
|
||||||
|
//
|
||||||
|
|||||||
@@ -5,26 +5,35 @@ result = """
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
types = ["S64", "S32", "S16", "S8", "U64", "U32", "U16", "U8", "F32", "F64"]
|
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
|
# Generate utility functions
|
||||||
#
|
#
|
||||||
if False:
|
if True:
|
||||||
for T in types:
|
for T in types:
|
||||||
t = T.lower()
|
t = T.lower()
|
||||||
result += f"""
|
result += f"""
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
stack_push_{t}(Bc *b, {T} value){{
|
ins_push_{t}(Bc *b, {T} value){{
|
||||||
U64 allocation_size = 2*sizeof(U64);
|
auto data = b->stack_pointer;
|
||||||
auto data = (U8 *)arena_push_size(&b->stack, allocation_size);
|
b->stack_pointer += 2*sizeof(U64);
|
||||||
|
|
||||||
C({T}, data) = value;
|
C({T}, data) = value;
|
||||||
data += sizeof(U64);
|
data += sizeof(U64);
|
||||||
|
|
||||||
C(U64, data) = TYPE_{T};
|
C(U64, data) = TYPE_{T};
|
||||||
|
|
||||||
b->stack_pointer += allocation_size;
|
|
||||||
}}
|
}}
|
||||||
|
|
||||||
force_inline void
|
force_inline void
|
||||||
@@ -37,12 +46,34 @@ if False:
|
|||||||
}}
|
}}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
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
|
# Generate switch cases
|
||||||
#
|
#
|
||||||
if True:
|
if False:
|
||||||
for T in types:
|
for T in types:
|
||||||
t = T.lower()
|
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"""
|
result += f"""
|
||||||
case INS_PUSH_{T}:{{
|
case INS_PUSH_{T}:{{
|
||||||
// Fetch value from instruction
|
// Fetch value from instruction
|
||||||
@@ -50,10 +81,11 @@ case INS_PUSH_{T}:{{
|
|||||||
// move pointer by the type size
|
// move pointer by the type size
|
||||||
auto value = ({T} *)b->ins_pointer;
|
auto value = ({T} *)b->ins_pointer;
|
||||||
b->ins_pointer += sizeof({T});
|
b->ins_pointer += sizeof({T});
|
||||||
stack_push_{t}(b, *value);
|
ins_push_{t}(b, *value);
|
||||||
}} break;
|
}} break;
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
result += """
|
result += """
|
||||||
//
|
//
|
||||||
// **End** of generated using code_generating_script.py
|
// **End** of generated using code_generating_script.py
|
||||||
|
|||||||
Reference in New Issue
Block a user