// File: /workspace/github_c_makefile_star2_O1/Liutos[P]Eloquent/vm.c
// Function: vm_execute
void vm_execute(vm_t *vm, ins_t *ins)
{
    value_t *_fun_, *_val_, *_val2_;
    value_t **_addr_; // Used only by BC_ADDR
    vm->ip = 0;
    while (vm_hasnext(vm, ins)) {
        bytecode_t *bc = vm_nextins(vm, ins);
        switch (BC_OPCODE(bc)) {
            case BC_ADDR:
                _addr_ = env_getaddr(vm->env, BC_ADDR_NAME(bc));
                if (_addr_ == NULL)
                    _addr_ = env_getaddr(vm->global_env, BC_ADDR_NAME(bc));
                if (_addr_ == NULL) {
                    stack_push(vm->stack, value_error_newf("Undefined variable: %s", BC_ADDR_NAME(bc)));
                    goto __check_exception;
                }
                stack_push(vm->stack, value_ref_new(_addr_));
                break;
            case BC_ARGS:
                for (int i = BC_ARGS_ARITY(bc) - 1; i >= 0; i--) {
                    value_t *obj = (value_t *)stack_nth(vm->stack, i);
                    env_set(vm->env, NULL, obj);
                }
                stack_shrink(vm->stack, BC_ARGS_ARITY(bc));
                break;
            case BC_ASET:
                _val_ = (value_t *)stack_pop(vm->stack);
                _val2_ = (value_t *)stack_top(vm->stack);
                if (elo_type(_val_) != VALUE_REF) {
                    stack_push(vm->stack, value_error_new("Operand of `valof' must be a reference"));
                    goto __check_exception;
                }
                *VALUE_REF_ADDR(_val_) = _val2_;
                break;
            case BC_CALL:
                _fun_ = (value_t *)stack_pop(vm->stack);
                if (BC_CALL_NARGS(bc) != VALUE_FUNC_ARITY(_fun_)) {
                    stack_push(vm->stack, value_error_newf("Incorrect number of arguments. Expecting %d but get %d ones", VALUE_FUNC_ARITY(_fun_), BC_CALL_NARGS(bc)));
                    break;
                }

                if (VALUE_FUNC_ISBIF(_fun_))
                    vm_execute_bif(vm, _fun_);
                else {
                    vm_trace_callucf(vm, _fun_);
                    vm_save(vm, ins, _fun_);
                    vm->depth++;
                    /* 初始化环境、字节码序列和指令指针 */
                    vm->env = env_new(VALUE_FUNC_ENV(_fun_));
                    vm->denv = env_new(vm->denv);
                    vm->fun = _fun_;
                    ins = VALUE_UCF_CODE(_fun_);
                    vm_goto(vm, 0);
                    /* 开始执行新的字节码指令 */
                }
                break;
__check_exception:
            case BC_CHKEX:
                _val_ = (value_t *)stack_top(vm->stack);
                if (elo_ERRORP(_val_)) {
                    if (!stack_isempty(vm->sys_stack)) {
                        vm_restore(vm, &ins, &_fun_);
                        /* 将运行时错误压回参数栈 */
                        stack_push(vm->stack, _val_);
                    } else
                        return;
                }
                break;
            case BC_DGET:
                _val_ = env_get(vm->denv, BC_DGET_NAME(bc));
                if (_val_ == NULL) {
                    stack_push(vm->stack, value_error_newf("Undefined dynamic variable: %s", BC_DGET_NAME(bc)));
                    goto __check_exception;
                }

                stack_push(vm->stack, _val_);
                break;
            case BC_DSET:
                env_set(vm->denv, BC_DSET_NAME(bc), (value_t *)stack_top(vm->stack));
                break;
            case BC_FJUMP:
                _val_ = (value_t *)stack_pop(vm->stack);
                if (_val_->kind == VALUE_INT && VALUE_INT_VALUE(_val_) == 0)
                    vm_goto(vm, BC_FJUMP_INDEX(bc));
                break;
            case BC_GREF:
                _val_ = env_ref(vm->global_env, BC_REF_I(bc), BC_REF_J(bc));
                if (_val_ == NULL) {
                    stack_push(vm->stack, value_error_newf("Undefined variable: %s", BC_REF_NAME(bc)));
                    goto __check_exception;
                }

                stack_push(vm->stack, _val_);
                break;
            case BC_GSET:
                _val_ = (value_t *)stack_top(vm->stack);
                vm_env_set(vm->global_env, BC_SET_I(bc), BC_SET_J(bc), BC_SET_NAME(bc), _val_);
                break;
            case BC_FUNC:
                _val_ = (value_t *)stack_top(vm->stack);
                VALUE_FUNC_ENV(_val_) = vm->env;
                break;
            case BC_JUMP:
                vm_goto(vm, BC_JUMP_INDEX(bc));
                break;
            case BC_NOPE:
                break;
            case BC_POP:
                stack_pop(vm->stack);
                break;
            case BC_PRINT:
                _val_ = (value_t *)stack_top(vm->stack);
                vm_value_print(vm, _val_);
                break;
            case BC_PUSH:
                stack_push(vm->stack, BC_PUSH_PTR(bc));
                break;
            case BC_RETURN:
                vm->depth--;
                vm_restore(vm, &ins, &_fun_);
                vm_trace_retucf(vm, _fun_, (value_t *)stack_top(vm->stack));
                /* 开始执行旧的字节码指令 */
                break;
            case BC_REF:
                _val_ = env_ref(vm->env, BC_REF_I(bc), BC_REF_J(bc));
                if (_val_ == NULL) {
                    stack_push(vm->stack, value_error_newf("Undefined variable: %s", BC_REF_NAME(bc)));
                    goto __check_exception;
                }

                stack_push(vm->stack, _val_);
                break;
            case BC_SET:
                _val_ = (value_t *)stack_top(vm->stack);
                vm_env_set(vm->env, BC_SET_I(bc), BC_SET_J(bc), BC_SET_NAME(bc), _val_);
                break;
            case BC_TRACE: {
                _val_ = (value_t *)stack_top(vm->stack);
                ident_t *id = BC_TRACE_VAR(bc);
                if (id == NULL) {
                    char name[32] = {0};
                    snprintf(name, sizeof(name), "#<%p>", _val_);
                    id = ident_intern(name);
                }
                hash_table_set(vm->traced_objs, _val_, id);
            }
                break;
            case BC_VALOF:
                _val_ = (value_t *)stack_pop(vm->stack);
                if (elo_type(_val_) != VALUE_REF) {
                    stack_push(vm->stack, value_error_new("Argument of `valof' must be a reference"));
                    goto __check_exception;
                }
                stack_push(vm->stack, *VALUE_REF_ADDR(_val_));
                break;
            default :
                fprintf(stderr, "Not support: %s\n", bc_name(bc));
                exit(0);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, (%rsp)
movl $0x0, 0x4(%rdi)
cmpq $0x0, 0x10(%rsi)
je 0x3031
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x46b8(%rip), %r12      # 0x7118
movq 0x8569(%rip), %r13      # 0xafd0
xorl %esi, %esi
movq %r15, %rdi
callq 0x637e
incl 0x4(%rbx)
movq %rax, %rbp
movl (%rax), %eax
cmpq $0x15, %rax
ja 0x3040
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq 0x10(%rbx), %rdi
movq 0x8(%rbp), %rax
movq (%rax), %rsi
callq 0x58c6
testq %rax, %rax
jne 0x2ab1
movq 0x18(%rbx), %rdi
movq 0x8(%rbp), %rax
movq (%rax), %rsi
callq 0x58c6
testq %rax, %rax
je 0x2e16
movq 0x30(%rbx), %r14
movq %rax, %rdi
callq 0x2692
jmp 0x2df9
movq 0x18(%rbx), %rdi
jmp 0x2c62
movq 0x30(%rbx), %rdi
callq 0x6369
cmpl $0x6, (%rax)
movq 0x30(%rbx), %r14
jne 0x2dde
movq 0x8(%rax), %rax
movq (%rax), %rsi
movq %r14, %rdi
jmp 0x2e85
movq 0x30(%rbx), %rdi
callq 0x6369
cmpl $0x5, (%rax)
jne 0x2e8a
cmpl $0x0, 0x8(%rax)
jne 0x2e8a
movl 0x10(%rbp), %eax
movl %eax, 0x4(%rbx)
jmp 0x2e8a
movq 0x30(%rbx), %rdi
callq 0x6386
movq %rax, %r14
movq 0x8(%rbp), %rdx
testq %rdx, %rdx
jne 0x2b6b
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movl $0x20, %esi
leaq 0x10(%rsp), %r15
movq %r15, %rdi
leaq 0x45b8(%rip), %rdx      # 0x710e
movq %r14, %rcx
xorl %eax, %eax
callq 0x2050
movq %r15, %rdi
callq 0x28f8
movq %rax, %rdx
movq 0x28(%rbx), %rdi
movq %r14, %rsi
callq 0x5db0
jmp 0x2e8a
movq 0x30(%rbx), %rdi
callq 0x6386
movq 0x18(%rbx), %rdi
jmp 0x2da8
movq 0x30(%rbx), %rdi
callq 0x6386
movq 0x10(%rbx), %rcx
movq %rcx, 0x18(%rax)
jmp 0x2e8a
movq 0x30(%rbx), %rdi
callq 0x6369
jmp 0x2e8a
decl (%rbx)
movq %rbx, %rdi
movq %rsp, %rsi
leaq 0x8(%rsp), %rdx
callq 0x304f
movq 0x8(%rsp), %r14
movq 0x30(%rbx), %rdi
callq 0x6386
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x3242
jmp 0x2e8a
movq 0x30(%rbx), %rdi
callq 0x6386
cmpl $0x4, (%rax)
jne 0x2dbf
cmpl $0x0, 0xc(%rax)
je 0x2dbf
movq 0x20(%rax), %r14
cmpq $0x0, 0x10(%r14)
je 0x2e8a
xorl %r15d, %r15d
movq %r14, %rdi
movq %r15, %rsi
callq 0x637e
movq %rax, %rbp
movq (%r13), %rdi
leaq 0x4658(%rip), %rsi      # 0x7284
movl %r15d, %edx
xorl %eax, %eax
callq 0x20d0
movq (%r13), %rsi
movq %rbp, %rdi
callq 0x48a8
movq (%r13), %rsi
movl $0xa, %edi
callq 0x20a0
incq %r15
cmpq %r15, 0x10(%r14)
ja 0x2c13
jmp 0x2e8a
movq 0x10(%rbx), %rdi
movl 0x8(%rbp), %esi
movl 0xc(%rbp), %edx
callq 0x587f
testq %rax, %rax
jne 0x2d6b
movq 0x30(%rbx), %r14
movq 0x10(%rbp), %rsi
jmp 0x2e21
movq 0x30(%rbx), %rdi
movq 0x8(%rbp), %rsi
jmp 0x2e85
movq 0x30(%rbx), %rdi
callq 0x6369
movq %rax, 0x8(%rsp)
movl 0x8(%rbp), %edx
movl 0x10(%rax), %esi
cmpl %esi, %edx
jne 0x2de7
movq %rax, %r14
cmpl $0x0, 0x8(%rax)
je 0x2ea1
cmpl $0x2, %esi
je 0x2fd8
cmpl $0x1, %esi
je 0x2fad
testl %esi, %esi
jne 0x3048
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x30d2
movq 0x8(%rbx), %rdi
callq *0x20(%r14)
jmp 0x3014
movl 0x8(%rbp), %r14d
testl %r14d, %r14d
jle 0x2d1f
incl %r14d
leal -0x2(%r14), %esi
movq 0x30(%rbx), %rdi
callq 0x6354
movq 0x10(%rbx), %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x567a
decl %r14d
cmpl $0x1, %r14d
jg 0x2cfb
movq 0x30(%rbx), %rdi
movslq 0x8(%rbp), %rsi
callq 0x62b5
jmp 0x2e8a
movq 0x8(%rbp), %r14
movq 0x8(%rbx), %r15
movq 0x30(%rbx), %rdi
callq 0x6386
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x567a
jmp 0x2e8a
movq 0x8(%rbx), %rdi
movq 0x8(%rbp), %rsi
callq 0x57f3
testq %rax, %rax
je 0x2e2a
movq 0x30(%rbx), %rdi
jmp 0x2dfc
movq 0x30(%rbx), %rdi
callq 0x6369
movq %rax, %r14
movq 0x30(%rbx), %rdi
callq 0x6386
cmpl $0x6, (%r14)
jne 0x2e04
movq 0x8(%r14), %rcx
movq %rax, (%rcx)
jmp 0x2e8a
movq 0x30(%rbx), %rdi
callq 0x6386
movq 0x10(%rbx), %rdi
movl 0x8(%rbp), %esi
movl 0xc(%rbp), %edx
movq 0x10(%rbp), %rcx
movq %rax, %r8
callq 0x30a1
jmp 0x2e8a
movq (%r13), %rsi
movq %rax, %rdi
callq 0x2755
movq (%r13), %rsi
movl $0xa, %edi
callq 0x20a0
jmp 0x2e8a
leaq 0x4424(%rip), %rdi      # 0x7209
jmp 0x2e0f
movq 0x30(%rbx), %r14
leaq 0x43bc(%rip), %rdi      # 0x71ae
xorl %eax, %eax
callq 0x2596
movq %r14, %rdi
movq %rax, %rsi
jmp 0x2e85
movq 0x30(%rbx), %r14
leaq 0x4378(%rip), %rdi      # 0x7187
callq 0x255d
jmp 0x2e40
movq 0x30(%rbx), %r14
movq 0x8(%rbp), %rax
movq (%rax), %rsi
leaq 0x4348(%rip), %rdi      # 0x7170
jmp 0x2e39
movq 0x30(%rbx), %r14
movq 0x8(%rbp), %rsi
leaq 0x43b1(%rip), %rdi      # 0x71ea
xorl %eax, %eax
callq 0x2596
movq %r14, %rdi
movq %rax, %rsi
callq 0x62ba
movq 0x30(%rbx), %rdi
callq 0x6386
cmpl $0x2, (%rax)
jne 0x2e8a
movq %rax, %r14
movq 0x38(%rbx), %rdi
callq 0x6393
testq %rax, %rax
je 0x3031
movq %rbx, %rdi
movq %rsp, %rsi
leaq 0x8(%rsp), %rdx
callq 0x304f
movq 0x30(%rbx), %rdi
movq %r14, %rsi
callq 0x62ba
movq (%rsp), %r15
movslq 0x4(%rbx), %rsi
cmpq %rsi, 0x10(%r15)
ja 0x2a69
jmp 0x3031
movq 0x28(%rbx), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x5e20
testq %rax, %rax
je 0x2f3a
movq %rax, %rbp
movq %rbx, %rdi
callq 0x32bb
movq (%r13), %rdi
movl (%rbx), %edx
movq (%rbp), %rcx
leaq 0x438c(%rip), %rsi      # 0x7260
xorl %eax, %eax
callq 0x20d0
movl 0x10(%r14), %ebp
decl %ebp
js 0x2f20
movq 0x30(%rbx), %rdi
movl %ebp, %esi
callq 0x6354
movq (%r13), %rsi
movq %rax, %rdi
callq 0x2755
subl $0x1, %ebp
jb 0x2f19
movq (%r13), %rcx
movl $0x2, %esi
movl $0x1, %edx
leaq 0x4355(%rip), %rdi      # 0x7269
callq 0x2140
leal 0x1(%rbp), %eax
testl %eax, %eax
jg 0x2ee3
movq (%r13), %rcx
movl $0x2, %esi
movl $0x1, %edx
leaq 0x4337(%rip), %rdi      # 0x726c
callq 0x2140
movq 0x8(%rbx), %rsi
movq 0x38(%rbx), %rdi
callq 0x62ba
movq 0x10(%rbx), %rsi
movq 0x38(%rbx), %rdi
callq 0x62ba
movq 0x38(%rbx), %rdi
movq %r15, %rsi
callq 0x62ba
movq 0x38(%rbx), %rdi
movslq 0x4(%rbx), %rsi
callq 0x62ba
movq 0x38(%rbx), %rdi
movq %r14, %rsi
callq 0x62ba
incl (%rbx)
movq 0x18(%r14), %rdi
callq 0x55f7
movq %rax, 0x10(%rbx)
movq 0x8(%rbx), %rdi
callq 0x55f7
movq %rax, 0x8(%rbx)
movq %r14, 0x40(%rbx)
movq 0x20(%r14), %rax
movq %rax, (%rsp)
movl $0x0, 0x4(%rbx)
jmp 0x2e8a
movq 0x30(%rbx), %rdi
callq 0x6369
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x30d2
movq 0x8(%rbx), %rdi
movq %r15, %rsi
callq *0x20(%r14)
jmp 0x3014
movq 0x30(%rbx), %rdi
callq 0x6369
movq %rax, %r15
movq 0x30(%rbx), %rdi
callq 0x6369
movq %rax, %rbp
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x30d2
movq 0x8(%rbx), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq *0x20(%r14)
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x3242
movq 0x30(%rbx), %rdi
movq %r15, %rsi
jmp 0x2e85
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %rdi
callq 0x21b5
movl %esi, %edi
callq 0x2193