#include "spl_ir2vm.h" static int emit_instr(spl_ir2vm_t *ctx, spl_ir_func_t *fn, spl_ir_node_t *node) { int pos = 0; switch (node->kind) { case SPL_IR_TYPE_CONST: break; case SPL_IR_CONTROL_RET: spl_type_node_t *tn = spl_type_node(ctx->type, node->control_ret.tid); spl_ir_node_t *ret_node = &vec_at(fn->nodes, node->control_ret.val); switch (tn->kind) { case SPL_TYPE_INT: break; default: UNREACHABLE(); } // if (is_agg(ty, tid)) { // if (n->control_ret.val) { // emit_value(fc, n->control_ret.val); // emit_laddr(fc, fc->sret_off); // emit_load_insn(fc, SPL_PTR); // add_insn(fc, SPL_SWAP, SPL_VOID, 0); // emit_memcpy(fc, type_size(ty, tid)); // } // add_insn(fc, SPL_RET, SPL_VOID, 0); // } else if (!is_void(ty, tid)) { // if (n->control_ret.val) // emit_value(fc, n->control_ret.val); // else // emit_push(fc, 0); /* 空体函数:返回未定义0 */ // add_insn(fc, SPL_RET, vm_tag(ty, tid), 0); // } else { // add_insn(fc, SPL_RET, SPL_VOID, 0); // } spl_prog_add_instr(&ctx->prog, SPL_PUSH, SPL_I32, 0); spl_prog_add_instr(&ctx->prog, SPL_RET, SPL_I32, 0); break; default: break; } return pos; } void spl_ir2vm_init(spl_ir2vm_t *ctx, const spl_ir_t *ir, const spl_type_t *type) { ctx->ir = ir; ctx->type = type; spl_prog_init(&ctx->prog); } void spl_ir2vm_drop(spl_ir2vm_t *ctx) { (void)ctx; } int spl_ir2vm_run(spl_ir2vm_t *ctx) { int addr = 0; vec_for(ctx->ir->funcs, i) { if (i == 0) { continue; } spl_ir_func_t *fn = &vec_at(ctx->ir->funcs, i); Assert(fn != NULL); spl_vm_func_t vm_fn = {0}; vm_fn.name = (char *)fn->name; // FIXME vm_fn.address = addr; vm_fn.nargs = 0; vm_fn.ninsns = 0; vec_for(fn->nodes, j) { spl_ir_node_t *node = &vec_at(fn->nodes, j); emit_instr(ctx, fn, node); } spl_prog_add_func(&ctx->prog, &vm_fn); } return 0; } void spl_ir2vm_dump(spl_ir2vm_t *ctx) { (void)ctx; }