55 lines
1.6 KiB
C++
55 lines
1.6 KiB
C++
|
|
function Register_Index
|
|
bc_emit_expr(Bc *b, Ast *ast){
|
|
if(!ast) return REG_NULL;
|
|
if(!is_flag_set(ast->flags, AST_EXPR))
|
|
compiler_error(ast->pos, "Internal compiler error: Trying to emit expression but it doesn't have appropriate flag");
|
|
switch(ast->kind){
|
|
CASE(VALUE, Atom){
|
|
Register_Index dst = allocate_register(b);
|
|
U64 value = bigint_as_unsigned(&node->big_int_val);
|
|
emit_load_constant_s64(b, dst, value);
|
|
return dst;
|
|
BREAK();
|
|
}
|
|
CASE(BINARY, Binary){
|
|
Register_Index left = bc_emit_expr(b, node->left);
|
|
Register_Index right = bc_emit_expr(b, node->right);
|
|
emit_arithmetic(b, BC_ADD_S64, left, right, left);
|
|
release_register(b, right);
|
|
return left;
|
|
BREAK();
|
|
}
|
|
default:{}
|
|
}
|
|
return REG_NULL;
|
|
}
|
|
|
|
function void
|
|
compile_to_bc(){
|
|
Bc bc = create_bytecode_interp();
|
|
Bc *b = &bc;
|
|
For_Named(pctx->ordered_decls, ast){
|
|
switch(ast->kind){
|
|
CASE(LAMBDA, Decl){
|
|
unused(node);
|
|
BREAK();
|
|
}
|
|
CASE(VAR, Decl){
|
|
node->bytecode_data_position = exp_alloc(&b->memory, node->type->size, AF_ZeroMemory);
|
|
if(node->expr){
|
|
Register_Index expression_index = bc_emit_expr(b, node->expr);
|
|
Register_Index address_index = allocate_register(b);
|
|
emit_load_constant_address(b, address_index, node->bytecode_data_position);
|
|
emit_memory(b, BC_STORE_TO_MEMORY64, address_index, expression_index);
|
|
release_register(b, expression_index);
|
|
}
|
|
BREAK();
|
|
}
|
|
default: {}
|
|
}
|
|
}
|
|
emit_end(b);
|
|
run_bytecode_interp(b);
|
|
destroy_bytecode_interp(b);
|
|
} |